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 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
110 struct ext2_inode inode;
112 enum quota_type qtype;
114 if ((err = ext2fs_read_inode(fs, ino, &inode)))
117 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
118 if (ino == quota_type2inum(qtype, fs->super))
121 if (qtype != MAXQUOTAS) {
122 ext2fs_set_dtime(fs, &inode);
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 /* Functions to read/write quota file. */
138 static unsigned int quota_write_nomount(struct quota_file *qf,
140 void *buf, unsigned int size)
142 ext2_file_t e2_file = qf->e2_file;
143 unsigned int bytes_written = 0;
146 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
148 log_err("ext2fs_file_llseek failed: %ld", err);
152 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
154 log_err("ext2fs_file_write failed: %ld", err);
158 /* Correct inode.i_size is set in end_io. */
159 return bytes_written;
162 static unsigned int quota_read_nomount(struct quota_file *qf,
164 void *buf, unsigned int size)
166 ext2_file_t e2_file = qf->e2_file;
167 unsigned int bytes_read = 0;
170 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
172 log_err("ext2fs_file_llseek failed: %ld", err);
176 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
178 log_err("ext2fs_file_read failed: %ld", err);
186 * Detect quota format and initialize quota IO
188 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
189 ext2_ino_t qf_ino, enum quota_type qtype,
192 ext2_filsys fs = qctx->fs;
195 int allocated_handle = 0;
197 if (qtype >= MAXQUOTAS)
203 err = ext2fs_read_bitmaps(fs);
208 qf_ino = *quota_sb_inump(fs->super, qtype);
210 log_debug("Opening quota ino=%u, type=%d", qf_ino, qtype);
211 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
213 log_err("ext2fs_file_open failed: %s", error_message(err));
218 if (qctx->quota_file[qtype]) {
219 h = qctx->quota_file[qtype];
220 if (((flags & EXT2_FILE_WRITE) == 0) ||
221 (h->qh_file_flags & EXT2_FILE_WRITE)) {
222 ext2fs_file_close(e2_file);
225 (void) quota_file_close(qctx, h);
227 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
229 log_err("Unable to allocate quota handle");
230 ext2fs_file_close(e2_file);
233 allocated_handle = 1;
236 h->qh_qf.e2_file = e2_file;
238 h->qh_qf.ino = qf_ino;
239 h->e2fs_write = quota_write_nomount;
240 h->e2fs_read = quota_read_nomount;
241 h->qh_file_flags = flags;
245 memset(&h->qh_info, 0, sizeof(h->qh_info));
246 h->qh_ops = "afile_ops_2;
248 if (h->qh_ops->check_file &&
249 (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
250 log_err("qh_ops->check_file failed");
255 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
256 log_err("qh_ops->init_io failed");
260 if (allocated_handle)
261 qctx->quota_file[qtype] = h;
265 ext2fs_file_close(e2_file);
266 if (allocated_handle)
271 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
273 struct ext2_inode inode;
277 err = ext2fs_read_inode(fs, ino, &inode);
279 log_err("ex2fs_read_inode failed");
283 if (EXT2_I_SIZE(&inode)) {
284 err = quota_inode_truncate(fs, ino);
289 memset(&inode, 0, sizeof(struct ext2_inode));
290 ext2fs_iblk_set(fs, &inode, 0);
291 now = fs->now ? fs->now : time(0);
292 ext2fs_inode_xtime_set(&inode, i_atime, now);
293 ext2fs_inode_xtime_set(&inode, i_ctime, now);
294 ext2fs_inode_xtime_set(&inode, i_mtime, now);
295 inode.i_links_count = 1;
296 inode.i_mode = LINUX_S_IFREG | 0600;
297 inode.i_flags |= EXT2_IMMUTABLE_FL;
298 if (ext2fs_has_feature_extents(fs->super))
299 inode.i_flags |= EXT4_EXTENTS_FL;
301 err = ext2fs_write_new_inode(fs, ino, &inode);
303 log_err("ext2fs_write_new_inode failed: %ld", err);
310 * Create new quotafile of specified format on given filesystem
312 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
313 enum quota_type qtype, int fmt)
317 ext2_ino_t qf_inum = 0;
323 qf_inum = quota_type2inum(qtype, fs->super);
324 if (qf_inum == 0 && qtype == PRJQUOTA) {
325 err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
329 ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0);
330 ext2fs_mark_ib_dirty(fs);
331 } else if (qf_inum == 0) {
332 return EXT2_ET_BAD_INODE_NUM;
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=%u, type=%d", qf_inum, qtype);
350 err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
352 log_err("ext2fs_file_open failed: %ld", 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");
372 ext2fs_file_close(e2_file);
376 quota_inode_truncate(fs, qf_inum);
382 * Close quotafile and release handle
384 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
386 if (h->qh_io_flags & IOFL_INFODIRTY) {
387 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
389 h->qh_io_flags &= ~IOFL_INFODIRTY;
392 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
394 if (h->qh_qf.e2_file)
395 ext2fs_file_close(h->qh_qf.e2_file);
396 if (qctx->quota_file[h->qh_type] == h)
397 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
402 * Create empty quota structure
404 struct dquot *get_empty_dquot(void)
408 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
409 log_err("Failed to allocate dquot");