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>
23 static const char * const extensions[MAXQUOTAS] = {"user", "group"};
24 static const char * const basenames[] = {
26 "quota", /* QFMT_VFS_OLD */
27 "aquota", /* QFMT_VFS_V0 */
29 "aquota" /* QFMT_VFS_V1 */
32 /* Header in all newer quotafiles */
33 struct disk_dqheader {
36 } __attribute__ ((packed));
39 * Convert type of quota to written representation
41 const char *quota_type2name(enum quota_type qtype)
43 if (qtype < 0 || qtype >= MAXQUOTAS)
45 return extensions[qtype];
48 ext2_ino_t quota_type2inum(enum quota_type qtype,
49 struct ext2_super_block *sb)
53 return EXT4_USR_QUOTA_INO;
55 return EXT4_GRP_QUOTA_INO;
63 * Creates a quota file name for given type and format.
65 const char *quota_get_qf_name(enum quota_type type, int fmt, char *buf)
69 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
70 basenames[fmt], extensions[type]);
75 const char *quota_get_qf_path(const char *mntpt, enum quota_type qtype, int fmt,
76 char *path_buf, size_t path_buf_size)
78 char qf_name[QUOTA_NAME_LEN];
80 if (!mntpt || !path_buf || !path_buf_size)
83 strncpy(path_buf, mntpt, path_buf_size);
84 strncat(path_buf, "/", 1);
85 strncat(path_buf, quota_get_qf_name(qtype, fmt, qf_name),
86 path_buf_size - strlen(path_buf));
92 * Set grace time if needed
94 void update_grace_times(struct dquot *q)
99 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
100 q->dq_dqb.dqb_bsoftlimit) {
101 if (!q->dq_dqb.dqb_btime)
102 q->dq_dqb.dqb_btime =
103 now + q->dq_h->qh_info.dqi_bgrace;
105 q->dq_dqb.dqb_btime = 0;
108 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
109 q->dq_dqb.dqb_isoftlimit) {
110 if (!q->dq_dqb.dqb_itime)
111 q->dq_dqb.dqb_itime =
112 now + q->dq_h->qh_info.dqi_igrace;
114 q->dq_dqb.dqb_itime = 0;
118 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
119 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
120 blk64_t ref_block EXT2FS_ATTR((unused)),
121 int ref_offset EXT2FS_ATTR((unused)),
124 blk64_t *num_blocks = private;
130 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
132 struct ext2_inode inode;
134 enum quota_type qtype;
136 if ((err = ext2fs_read_inode(fs, ino, &inode)))
139 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
140 if (ino == quota_type2inum(qtype, fs->super))
143 if (qtype != MAXQUOTAS) {
144 inode.i_dtime = fs->now ? fs->now : time(0);
145 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
147 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
150 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
151 memset(&inode, 0, sizeof(struct ext2_inode));
153 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
155 err = ext2fs_write_inode(fs, ino, &inode);
159 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
161 blk64_t num_blocks = 0;
163 ext2fs_block_iterate3(fs, ino,
164 BLOCK_FLAG_READ_ONLY,
166 compute_num_blocks_proc,
168 return num_blocks * fs->blocksize;
171 /* Functions to read/write quota file. */
172 static unsigned int quota_write_nomount(struct quota_file *qf,
174 void *buf, unsigned int size)
176 ext2_file_t e2_file = qf->e2_file;
177 unsigned int bytes_written = 0;
180 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
182 log_err("ext2fs_file_llseek failed: %ld", err);
186 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
188 log_err("ext2fs_file_write failed: %ld", err);
192 /* Correct inode.i_size is set in end_io. */
193 return bytes_written;
196 static unsigned int quota_read_nomount(struct quota_file *qf,
198 void *buf, unsigned int size)
200 ext2_file_t e2_file = qf->e2_file;
201 unsigned int bytes_read = 0;
204 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
206 log_err("ext2fs_file_llseek failed: %ld", err);
210 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
212 log_err("ext2fs_file_read failed: %ld", err);
220 * Detect quota format and initialize quota IO
222 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
223 ext2_ino_t qf_ino, enum quota_type qtype,
226 ext2_filsys fs = qctx->fs;
229 int allocated_handle = 0;
231 if (qtype >= MAXQUOTAS)
237 err = ext2fs_read_bitmaps(fs);
242 qf_ino = *quota_sb_inump(fs->super, qtype)
244 log_debug("Opening quota ino=%lu, type=%d", qf_ino, qtype);
245 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
247 log_err("ext2fs_file_open failed: %s", error_message(err));
252 if (qctx->quota_file[qtype]) {
253 h = qctx->quota_file[qtype];
254 if (((flags & EXT2_FILE_WRITE) == 0) ||
255 (h->qh_file_flags & EXT2_FILE_WRITE))
257 (void) quota_file_close(qctx, h);
259 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
261 log_err("Unable to allocate quota handle");
264 allocated_handle = 1;
267 h->qh_qf.e2_file = e2_file;
269 h->qh_qf.ino = qf_ino;
270 h->e2fs_write = quota_write_nomount;
271 h->e2fs_read = quota_read_nomount;
272 h->qh_file_flags = flags;
276 memset(&h->qh_info, 0, sizeof(h->qh_info));
277 h->qh_ops = "afile_ops_2;
279 if (h->qh_ops->check_file &&
280 (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
281 log_err("qh_ops->check_file failed");
285 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
286 log_err("qh_ops->init_io failed");
289 if (allocated_handle)
290 qctx->quota_file[qtype] = h;
294 ext2fs_file_close(e2_file);
295 if (allocated_handle)
300 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
302 struct ext2_inode inode;
305 err = ext2fs_read_inode(fs, ino, &inode);
307 log_err("ex2fs_read_inode failed");
311 if (EXT2_I_SIZE(&inode))
312 quota_inode_truncate(fs, ino);
314 memset(&inode, 0, sizeof(struct ext2_inode));
315 ext2fs_iblk_set(fs, &inode, 0);
316 inode.i_atime = inode.i_mtime =
317 inode.i_ctime = fs->now ? fs->now : time(0);
318 inode.i_links_count = 1;
319 inode.i_mode = LINUX_S_IFREG | 0600;
320 inode.i_flags |= EXT2_IMMUTABLE_FL;
321 if (fs->super->s_feature_incompat &
322 EXT3_FEATURE_INCOMPAT_EXTENTS)
323 inode.i_flags |= EXT4_EXTENTS_FL;
325 err = ext2fs_write_new_inode(fs, ino, &inode);
327 log_err("ext2fs_write_new_inode failed: %ld", err);
334 * Create new quotafile of specified format on given filesystem
336 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
337 enum quota_type qtype, int fmt)
341 unsigned long qf_inum;
347 qf_inum = quota_type2inum(qtype, fs->super);
351 err = ext2fs_read_bitmaps(fs);
355 err = quota_inode_init_new(fs, qf_inum);
357 log_err("init_new_quota_inode failed");
360 h->qh_qf.ino = qf_inum;
361 h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
362 h->e2fs_write = quota_write_nomount;
363 h->e2fs_read = quota_read_nomount;
365 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
366 err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
368 log_err("ext2fs_file_open failed: %d", err);
371 h->qh_qf.e2_file = e2_file;
376 memset(&h->qh_info, 0, sizeof(h->qh_info));
377 h->qh_ops = "afile_ops_2;
379 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
380 log_err("qh_ops->new_io failed");
387 ext2fs_file_close(e2_file);
391 quota_inode_truncate(fs, qf_inum);
397 * Close quotafile and release handle
399 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
401 if (h->qh_io_flags & IOFL_INFODIRTY) {
402 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
404 h->qh_io_flags &= ~IOFL_INFODIRTY;
407 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
409 if (h->qh_qf.e2_file) {
410 __u64 new_size, size;
412 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
413 ext2fs_file_flush(h->qh_qf.e2_file);
414 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
416 if (size != new_size)
417 ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
418 ext2fs_file_close(h->qh_qf.e2_file);
420 if (qctx->quota_file[h->qh_type] == h)
421 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
426 * Create empty quota structure
428 struct dquot *get_empty_dquot(void)
432 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
433 log_err("Failed to allocate dquot");