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] = {
26 [PRJQUOTA] = "project",
28 static const char * const basenames[] = {
30 "quota", /* QFMT_VFS_OLD */
31 "aquota", /* QFMT_VFS_V0 */
33 "aquota" /* QFMT_VFS_V1 */
36 /* Header in all newer quotafiles */
37 struct disk_dqheader {
40 } __attribute__ ((packed));
43 * Convert type of quota to written representation
45 const char *quota_type2name(enum quota_type qtype)
47 if (qtype >= MAXQUOTAS)
49 return extensions[qtype];
52 ext2_ino_t quota_type2inum(enum quota_type qtype,
53 struct ext2_super_block *sb)
57 return EXT4_USR_QUOTA_INO;
59 return EXT4_GRP_QUOTA_INO;
61 return sb->s_prj_quota_inum;
69 * Creates a quota file name for given type and format.
71 const char *quota_get_qf_name(enum quota_type type, int fmt, char *buf)
75 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
76 basenames[fmt], extensions[type]);
82 * Set grace time if needed
84 void update_grace_times(struct dquot *q)
89 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
90 q->dq_dqb.dqb_bsoftlimit) {
91 if (!q->dq_dqb.dqb_btime)
93 now + q->dq_h->qh_info.dqi_bgrace;
95 q->dq_dqb.dqb_btime = 0;
98 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
99 q->dq_dqb.dqb_isoftlimit) {
100 if (!q->dq_dqb.dqb_itime)
101 q->dq_dqb.dqb_itime =
102 now + q->dq_h->qh_info.dqi_igrace;
104 q->dq_dqb.dqb_itime = 0;
108 static int compute_num_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
109 blk64_t *blocknr EXT2FS_ATTR((unused)),
110 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
111 blk64_t ref_block EXT2FS_ATTR((unused)),
112 int ref_offset EXT2FS_ATTR((unused)),
115 blk64_t *num_blocks = private;
121 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
123 struct ext2_inode inode;
125 enum quota_type qtype;
127 if ((err = ext2fs_read_inode(fs, ino, &inode)))
130 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
131 if (ino == quota_type2inum(qtype, fs->super))
134 if (qtype != MAXQUOTAS) {
135 inode.i_dtime = fs->now ? fs->now : time(0);
136 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
138 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
141 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
142 memset(&inode, 0, sizeof(struct ext2_inode));
144 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
146 err = ext2fs_write_inode(fs, ino, &inode);
150 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
152 blk64_t num_blocks = 0;
154 ext2fs_block_iterate3(fs, ino,
155 BLOCK_FLAG_READ_ONLY,
157 compute_num_blocks_proc,
159 return num_blocks * fs->blocksize;
162 /* Functions to read/write quota file. */
163 static unsigned int quota_write_nomount(struct quota_file *qf,
165 void *buf, unsigned int size)
167 ext2_file_t e2_file = qf->e2_file;
168 unsigned int bytes_written = 0;
171 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
173 log_err("ext2fs_file_llseek failed: %ld", err);
177 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
179 log_err("ext2fs_file_write failed: %ld", err);
183 /* Correct inode.i_size is set in end_io. */
184 return bytes_written;
187 static unsigned int quota_read_nomount(struct quota_file *qf,
189 void *buf, unsigned int size)
191 ext2_file_t e2_file = qf->e2_file;
192 unsigned int bytes_read = 0;
195 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
197 log_err("ext2fs_file_llseek failed: %ld", err);
201 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
203 log_err("ext2fs_file_read failed: %ld", err);
211 * Detect quota format and initialize quota IO
213 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
214 ext2_ino_t qf_ino, enum quota_type qtype,
217 ext2_filsys fs = qctx->fs;
220 int allocated_handle = 0;
222 if (qtype >= MAXQUOTAS)
228 err = ext2fs_read_bitmaps(fs);
233 qf_ino = *quota_sb_inump(fs->super, qtype);
235 log_debug("Opening quota ino=%u, type=%d", qf_ino, qtype);
236 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
238 log_err("ext2fs_file_open failed: %s", error_message(err));
243 if (qctx->quota_file[qtype]) {
244 h = qctx->quota_file[qtype];
245 if (((flags & EXT2_FILE_WRITE) == 0) ||
246 (h->qh_file_flags & EXT2_FILE_WRITE)) {
247 ext2fs_file_close(e2_file);
250 (void) quota_file_close(qctx, h);
252 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
254 log_err("Unable to allocate quota handle");
255 ext2fs_file_close(e2_file);
258 allocated_handle = 1;
261 h->qh_qf.e2_file = e2_file;
263 h->qh_qf.ino = qf_ino;
264 h->e2fs_write = quota_write_nomount;
265 h->e2fs_read = quota_read_nomount;
266 h->qh_file_flags = flags;
270 memset(&h->qh_info, 0, sizeof(h->qh_info));
271 h->qh_ops = "afile_ops_2;
273 if (h->qh_ops->check_file &&
274 (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
275 log_err("qh_ops->check_file failed");
280 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
281 log_err("qh_ops->init_io failed");
285 if (allocated_handle)
286 qctx->quota_file[qtype] = h;
290 ext2fs_file_close(e2_file);
291 if (allocated_handle)
296 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
298 struct ext2_inode inode;
301 err = ext2fs_read_inode(fs, ino, &inode);
303 log_err("ex2fs_read_inode failed");
307 if (EXT2_I_SIZE(&inode)) {
308 err = quota_inode_truncate(fs, ino);
313 memset(&inode, 0, sizeof(struct ext2_inode));
314 ext2fs_iblk_set(fs, &inode, 0);
315 inode.i_atime = inode.i_mtime =
316 inode.i_ctime = fs->now ? fs->now : time(0);
317 inode.i_links_count = 1;
318 inode.i_mode = LINUX_S_IFREG | 0600;
319 inode.i_flags |= EXT2_IMMUTABLE_FL;
320 if (ext2fs_has_feature_extents(fs->super))
321 inode.i_flags |= EXT4_EXTENTS_FL;
323 err = ext2fs_write_new_inode(fs, ino, &inode);
325 log_err("ext2fs_write_new_inode failed: %ld", err);
332 * Create new quotafile of specified format on given filesystem
334 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
335 enum quota_type qtype, int fmt)
339 ext2_ino_t qf_inum = 0;
345 qf_inum = quota_type2inum(qtype, fs->super);
346 if (qf_inum == 0 && qtype == PRJQUOTA) {
347 err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
351 ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0);
352 ext2fs_mark_ib_dirty(fs);
353 } else if (qf_inum == 0) {
354 return EXT2_ET_BAD_INODE_NUM;
357 err = ext2fs_read_bitmaps(fs);
361 err = quota_inode_init_new(fs, qf_inum);
363 log_err("init_new_quota_inode failed");
366 h->qh_qf.ino = qf_inum;
367 h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
368 h->e2fs_write = quota_write_nomount;
369 h->e2fs_read = quota_read_nomount;
371 log_debug("Creating quota ino=%u, type=%d", qf_inum, qtype);
372 err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
374 log_err("ext2fs_file_open failed: %ld", err);
377 h->qh_qf.e2_file = e2_file;
382 memset(&h->qh_info, 0, sizeof(h->qh_info));
383 h->qh_ops = "afile_ops_2;
385 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
386 log_err("qh_ops->new_io failed");
394 ext2fs_file_close(e2_file);
398 quota_inode_truncate(fs, qf_inum);
404 * Close quotafile and release handle
406 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
408 if (h->qh_io_flags & IOFL_INFODIRTY) {
409 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
411 h->qh_io_flags &= ~IOFL_INFODIRTY;
414 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
416 if (h->qh_qf.e2_file) {
417 __u64 new_size, size;
419 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
420 ext2fs_file_flush(h->qh_qf.e2_file);
421 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
423 if (size != new_size)
424 ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
425 ext2fs_file_close(h->qh_qf.e2_file);
427 if (qctx->quota_file[h->qh_type] == h)
428 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
433 * Create empty quota structure
435 struct dquot *get_empty_dquot(void)
439 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
440 log_err("Failed to allocate dquot");