3 * Generic IO operations on quotafiles
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5 * Aditya Kali <adityakali@google.com> - Ported to e2fsprogs
15 #include <sys/types.h>
22 static const char * const extensions[MAXQUOTAS] = {"user", "group"};
23 static const char * const basenames[] = {
25 "quota", /* QFMT_VFS_OLD */
26 "aquota", /* QFMT_VFS_V0 */
28 "aquota" /* QFMT_VFS_V1 */
31 /* Header in all newer quotafiles */
32 struct disk_dqheader {
35 } __attribute__ ((packed));
38 * Convert type of quota to written representation
40 const char *type2name(int type)
42 return extensions[type];
46 * Creates a quota file name for given type and format.
48 const char *quota_get_qf_name(int type, int fmt, char *buf)
52 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
53 basenames[fmt], extensions[type]);
58 const char *quota_get_qf_path(const char *mntpt, int qtype, int fmt,
59 char *path_buf, size_t path_buf_size)
61 char qf_name[QUOTA_NAME_LEN];
63 if (!mntpt || !path_buf || !path_buf_size)
66 strncpy(path_buf, mntpt, path_buf_size);
67 strncat(path_buf, "/", 1);
68 strncat(path_buf, quota_get_qf_name(qtype, fmt, qf_name),
69 path_buf_size - strlen(path_buf));
75 * Set grace time if needed
77 void update_grace_times(struct dquot *q)
82 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
83 q->dq_dqb.dqb_bsoftlimit) {
84 if (!q->dq_dqb.dqb_btime)
86 now + q->dq_h->qh_info.dqi_bgrace;
88 q->dq_dqb.dqb_btime = 0;
91 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
92 q->dq_dqb.dqb_isoftlimit) {
93 if (!q->dq_dqb.dqb_itime)
95 now + q->dq_h->qh_info.dqi_igrace;
97 q->dq_dqb.dqb_itime = 0;
101 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
102 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
103 blk64_t ref_block EXT2FS_ATTR((unused)),
104 int ref_offset EXT2FS_ATTR((unused)),
107 blk64_t *num_blocks = private;
113 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
115 struct ext2_inode inode;
118 if ((err = ext2fs_read_inode(fs, ino, &inode)))
121 if ((ino == EXT4_USR_QUOTA_INO) || (ino == EXT4_GRP_QUOTA_INO)) {
122 inode.i_dtime = fs->now ? fs->now : time(0);
123 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
125 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
128 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
129 memset(&inode, 0, sizeof(struct ext2_inode));
131 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
133 err = ext2fs_write_inode(fs, ino, &inode);
137 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
139 blk64_t num_blocks = 0;
141 ext2fs_block_iterate3(fs, ino,
142 BLOCK_FLAG_READ_ONLY,
144 compute_num_blocks_proc,
146 return num_blocks * fs->blocksize;
149 /* Functions to read/write quota file. */
150 static unsigned int quota_write_nomount(struct quota_file *qf,
152 void *buf, unsigned int size)
154 ext2_file_t e2_file = qf->e2_file;
155 unsigned int bytes_written = 0;
158 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
160 log_err("ext2fs_file_llseek failed: %ld", err);
164 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
166 log_err("ext2fs_file_write failed: %ld", err);
170 /* Correct inode.i_size is set in end_io. */
171 return bytes_written;
174 static unsigned int quota_read_nomount(struct quota_file *qf,
176 void *buf, unsigned int size)
178 ext2_file_t e2_file = qf->e2_file;
179 unsigned int bytes_read = 0;
182 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
184 log_err("ext2fs_file_llseek failed: %ld", err);
188 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
190 log_err("ext2fs_file_read failed: %ld", err);
198 * Detect quota format and initialize quota IO
200 errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs,
201 ext2_ino_t qf_ino, int type, int fmt, int flags)
209 err = ext2fs_read_bitmaps(fs);
213 log_debug("Opening quota ino=%lu, type=%d", qf_ino, type);
214 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
216 log_err("ext2fs_file_open failed: %s", error_message(err));
219 h->qh_qf.e2_file = e2_file;
222 h->qh_qf.ino = qf_ino;
223 h->e2fs_write = quota_write_nomount;
224 h->e2fs_read = quota_read_nomount;
228 memset(&h->qh_info, 0, sizeof(h->qh_info));
229 h->qh_ops = "afile_ops_2;
231 if (h->qh_ops->check_file &&
232 (h->qh_ops->check_file(h, type, fmt) == 0)) {
233 log_err("qh_ops->check_file failed");
234 ext2fs_file_close(e2_file);
238 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
239 log_err("qh_ops->init_io failed");
240 ext2fs_file_close(e2_file);
247 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
249 struct ext2_inode inode;
252 err = ext2fs_read_inode(fs, ino, &inode);
254 log_err("ex2fs_read_inode failed");
258 if (EXT2_I_SIZE(&inode))
259 quota_inode_truncate(fs, ino);
261 memset(&inode, 0, sizeof(struct ext2_inode));
262 ext2fs_iblk_set(fs, &inode, 0);
263 inode.i_atime = inode.i_mtime =
264 inode.i_ctime = fs->now ? fs->now : time(0);
265 inode.i_links_count = 1;
266 inode.i_mode = LINUX_S_IFREG | 0600;
267 inode.i_flags |= EXT2_IMMUTABLE_FL;
268 if (fs->super->s_feature_incompat &
269 EXT3_FEATURE_INCOMPAT_EXTENTS)
270 inode.i_flags |= EXT4_EXTENTS_FL;
272 err = ext2fs_write_new_inode(fs, ino, &inode);
274 log_err("ext2fs_write_new_inode failed: %ld", err);
281 * Create new quotafile of specified format on given filesystem
283 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, int type, int fmt)
287 unsigned long qf_inum;
293 if (type == USRQUOTA)
294 qf_inum = EXT4_USR_QUOTA_INO;
295 else if (type == GRPQUOTA)
296 qf_inum = EXT4_GRP_QUOTA_INO;
300 err = ext2fs_read_bitmaps(fs);
304 err = quota_inode_init_new(fs, qf_inum);
306 log_err("init_new_quota_inode failed");
309 h->qh_qf.ino = qf_inum;
310 h->e2fs_write = quota_write_nomount;
311 h->e2fs_read = quota_read_nomount;
313 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
314 err = ext2fs_file_open(fs, qf_inum,
315 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file);
317 log_err("ext2fs_file_open failed: %d", err);
320 h->qh_qf.e2_file = e2_file;
325 memset(&h->qh_info, 0, sizeof(h->qh_info));
326 h->qh_ops = "afile_ops_2;
328 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
329 log_err("qh_ops->new_io failed");
336 ext2fs_file_close(e2_file);
340 quota_inode_truncate(fs, qf_inum);
346 * Close quotafile and release handle
348 errcode_t quota_file_close(struct quota_handle *h)
350 if (h->qh_io_flags & IOFL_INFODIRTY) {
351 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
353 h->qh_io_flags &= ~IOFL_INFODIRTY;
356 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
358 if (h->qh_qf.e2_file) {
359 ext2fs_file_flush(h->qh_qf.e2_file);
360 ext2fs_file_set_size2(h->qh_qf.e2_file,
361 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino));
362 ext2fs_file_close(h->qh_qf.e2_file);
369 * Create empty quota structure
371 struct dquot *get_empty_dquot(void)
375 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
376 log_err("Failed to allocate dquot");