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]);
76 * Set grace time if needed
78 void update_grace_times(struct dquot *q)
83 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
84 q->dq_dqb.dqb_bsoftlimit) {
85 if (!q->dq_dqb.dqb_btime)
87 now + q->dq_h->qh_info.dqi_bgrace;
89 q->dq_dqb.dqb_btime = 0;
92 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
93 q->dq_dqb.dqb_isoftlimit) {
94 if (!q->dq_dqb.dqb_itime)
96 now + q->dq_h->qh_info.dqi_igrace;
98 q->dq_dqb.dqb_itime = 0;
102 static int compute_num_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
103 blk64_t *blocknr EXT2FS_ATTR((unused)),
104 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
105 blk64_t ref_block EXT2FS_ATTR((unused)),
106 int ref_offset EXT2FS_ATTR((unused)),
109 blk64_t *num_blocks = private;
115 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
117 struct ext2_inode inode;
119 enum quota_type qtype;
121 if ((err = ext2fs_read_inode(fs, ino, &inode)))
124 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
125 if (ino == quota_type2inum(qtype, fs->super))
128 if (qtype != MAXQUOTAS) {
129 inode.i_dtime = fs->now ? fs->now : time(0);
130 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
132 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
135 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
136 memset(&inode, 0, sizeof(struct ext2_inode));
138 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
140 err = ext2fs_write_inode(fs, ino, &inode);
144 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
146 blk64_t num_blocks = 0;
148 ext2fs_block_iterate3(fs, ino,
149 BLOCK_FLAG_READ_ONLY,
151 compute_num_blocks_proc,
153 return num_blocks * fs->blocksize;
156 /* Functions to read/write quota file. */
157 static unsigned int quota_write_nomount(struct quota_file *qf,
159 void *buf, unsigned int size)
161 ext2_file_t e2_file = qf->e2_file;
162 unsigned int bytes_written = 0;
165 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
167 log_err("ext2fs_file_llseek failed: %ld", err);
171 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
173 log_err("ext2fs_file_write failed: %ld", err);
177 /* Correct inode.i_size is set in end_io. */
178 return bytes_written;
181 static unsigned int quota_read_nomount(struct quota_file *qf,
183 void *buf, unsigned int size)
185 ext2_file_t e2_file = qf->e2_file;
186 unsigned int bytes_read = 0;
189 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
191 log_err("ext2fs_file_llseek failed: %ld", err);
195 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
197 log_err("ext2fs_file_read failed: %ld", err);
205 * Detect quota format and initialize quota IO
207 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
208 ext2_ino_t qf_ino, enum quota_type qtype,
211 ext2_filsys fs = qctx->fs;
214 int allocated_handle = 0;
216 if (qtype >= MAXQUOTAS)
222 err = ext2fs_read_bitmaps(fs);
227 qf_ino = *quota_sb_inump(fs->super, qtype)
229 log_debug("Opening quota ino=%lu, type=%d", qf_ino, qtype);
230 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
232 log_err("ext2fs_file_open failed: %s", error_message(err));
237 if (qctx->quota_file[qtype]) {
238 h = qctx->quota_file[qtype];
239 if (((flags & EXT2_FILE_WRITE) == 0) ||
240 (h->qh_file_flags & EXT2_FILE_WRITE))
242 (void) quota_file_close(qctx, h);
244 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
246 log_err("Unable to allocate quota handle");
249 allocated_handle = 1;
252 h->qh_qf.e2_file = e2_file;
254 h->qh_qf.ino = qf_ino;
255 h->e2fs_write = quota_write_nomount;
256 h->e2fs_read = quota_read_nomount;
257 h->qh_file_flags = flags;
261 memset(&h->qh_info, 0, sizeof(h->qh_info));
262 h->qh_ops = "afile_ops_2;
264 if (h->qh_ops->check_file &&
265 (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
266 log_err("qh_ops->check_file failed");
270 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
271 log_err("qh_ops->init_io failed");
274 if (allocated_handle)
275 qctx->quota_file[qtype] = h;
279 ext2fs_file_close(e2_file);
280 if (allocated_handle)
285 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
287 struct ext2_inode inode;
290 err = ext2fs_read_inode(fs, ino, &inode);
292 log_err("ex2fs_read_inode failed");
296 if (EXT2_I_SIZE(&inode))
297 quota_inode_truncate(fs, ino);
299 memset(&inode, 0, sizeof(struct ext2_inode));
300 ext2fs_iblk_set(fs, &inode, 0);
301 inode.i_atime = inode.i_mtime =
302 inode.i_ctime = fs->now ? fs->now : time(0);
303 inode.i_links_count = 1;
304 inode.i_mode = LINUX_S_IFREG | 0600;
305 inode.i_flags |= EXT2_IMMUTABLE_FL;
306 if (ext2fs_has_feature_extents(fs->super))
307 inode.i_flags |= EXT4_EXTENTS_FL;
309 err = ext2fs_write_new_inode(fs, ino, &inode);
311 log_err("ext2fs_write_new_inode failed: %ld", err);
318 * Create new quotafile of specified format on given filesystem
320 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
321 enum quota_type qtype, int fmt)
325 unsigned long qf_inum;
331 qf_inum = quota_type2inum(qtype, fs->super);
335 err = ext2fs_read_bitmaps(fs);
339 err = quota_inode_init_new(fs, qf_inum);
341 log_err("init_new_quota_inode failed");
344 h->qh_qf.ino = qf_inum;
345 h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
346 h->e2fs_write = quota_write_nomount;
347 h->e2fs_read = quota_read_nomount;
349 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
350 err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
352 log_err("ext2fs_file_open failed: %d", err);
355 h->qh_qf.e2_file = e2_file;
360 memset(&h->qh_info, 0, sizeof(h->qh_info));
361 h->qh_ops = "afile_ops_2;
363 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
364 log_err("qh_ops->new_io failed");
371 ext2fs_file_close(e2_file);
375 quota_inode_truncate(fs, qf_inum);
381 * Close quotafile and release handle
383 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
385 if (h->qh_io_flags & IOFL_INFODIRTY) {
386 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
388 h->qh_io_flags &= ~IOFL_INFODIRTY;
391 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
393 if (h->qh_qf.e2_file) {
394 __u64 new_size, size;
396 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
397 ext2fs_file_flush(h->qh_qf.e2_file);
398 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
400 if (size != new_size)
401 ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
402 ext2fs_file_close(h->qh_qf.e2_file);
404 if (qctx->quota_file[h->qh_type] == h)
405 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
410 * Create empty quota structure
412 struct dquot *get_empty_dquot(void)
416 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
417 log_err("Failed to allocate dquot");