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
14 #include <sys/types.h>
21 static const char extensions[MAXQUOTAS + 2][20] = INITQFNAMES;
22 static const char * const basenames[] = {
24 "quota", /* QFMT_VFS_OLD */
25 "aquota", /* QFMT_VFS_V0 */
27 "aquota" /* QFMT_VFS_V1 */
30 static const char * const fmtnames[] = {
38 /* Header in all newer quotafiles */
39 struct disk_dqheader {
41 u_int32_t dqh_version;
42 } __attribute__ ((packed));
45 * Convert type of quota to written representation
47 const char *type2name(int type)
49 return extensions[type];
53 * Creates a quota file name for given type and format.
55 const char *quota_get_qf_name(int type, int fmt, char *buf)
59 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
60 basenames[fmt], extensions[type]);
65 const char *quota_get_qf_path(const char *mntpt, int qtype, int fmt,
66 char *path_buf, size_t path_buf_size)
69 char qf_name[QUOTA_NAME_LEN];
71 if (!mntpt || !path_buf || !path_buf_size)
74 strncpy(path_buf, mntpt, path_buf_size);
75 strncat(path_buf, "/", 1);
76 strncat(path_buf, quota_get_qf_name(qtype, fmt, qf_name),
77 path_buf_size - strlen(path_buf));
83 * Set grace time if needed
85 void update_grace_times(struct dquot *q)
90 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
91 q->dq_dqb.dqb_bsoftlimit) {
92 if (!q->dq_dqb.dqb_btime)
94 now + q->dq_h->qh_info.dqi_bgrace;
96 q->dq_dqb.dqb_btime = 0;
99 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
100 q->dq_dqb.dqb_isoftlimit) {
101 if (!q->dq_dqb.dqb_itime)
102 q->dq_dqb.dqb_itime =
103 now + q->dq_h->qh_info.dqi_igrace;
105 q->dq_dqb.dqb_itime = 0;
109 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
110 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
111 blk64_t ref_block EXT2FS_ATTR((unused)),
112 int ref_offset EXT2FS_ATTR((unused)),
113 void *private EXT2FS_ATTR((unused)))
118 ext2fs_block_alloc_stats2(fs, block, -1);
122 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
123 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
124 blk64_t ref_block EXT2FS_ATTR((unused)),
125 int ref_offset EXT2FS_ATTR((unused)),
129 blk64_t *num_blocks = private;
135 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
137 struct ext2_inode inode;
141 if ((err = ext2fs_read_inode(fs, ino, &inode)))
144 inode.i_dtime = fs->now ? fs->now : time(0);
145 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
148 ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
149 release_blocks_proc, NULL);
151 memset(&inode, 0, sizeof(struct ext2_inode));
152 err = ext2fs_write_inode(fs, ino, &inode);
156 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
158 blk64_t num_blocks = 0;
160 ext2fs_block_iterate3(fs, ino,
161 BLOCK_FLAG_READ_ONLY,
163 compute_num_blocks_proc,
165 return num_blocks * fs->blocksize;
168 /* Functions to read/write quota file. */
169 static unsigned int quota_write_nomount(struct quota_file *qf,
171 void *buf, unsigned int size)
173 ext2_file_t e2_file = qf->e2_file;
174 unsigned int bytes_written = 0;
177 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
179 log_err("ext2fs_file_llseek failed: %ld", err);
183 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
185 log_err("ext2fs_file_write failed: %ld", err);
189 /* Correct inode.i_size is set in end_io. */
190 return bytes_written;
193 static unsigned int quota_read_nomount(struct quota_file *qf,
195 void *buf, unsigned int size)
197 ext2_file_t e2_file = qf->e2_file;
198 unsigned int bytes_read = 0;
201 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
203 log_err("ext2fs_file_llseek failed: %ld", err);
207 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
209 log_err("ext2fs_file_read failed: %ld", err);
217 * Detect quota format and initialize quota IO
219 errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs,
220 ext2_ino_t qf_ino, int type, int fmt, int flags)
228 err = ext2fs_read_bitmaps(fs);
232 log_debug("Opening quota ino=%lu, type=%d", qf_ino, type);
233 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
235 log_err("ext2fs_file_open failed: %s", error_message(err));
238 h->qh_qf.e2_file = e2_file;
241 h->qh_qf.ino = qf_ino;
242 h->e2fs_write = quota_write_nomount;
243 h->e2fs_read = quota_read_nomount;
247 memset(&h->qh_info, 0, sizeof(h->qh_info));
248 h->qh_ops = "afile_ops_2;
250 if (h->qh_ops->check_file &&
251 (h->qh_ops->check_file(h, type, fmt) == 0)) {
252 log_err("qh_ops->check_file failed", "");
253 ext2fs_file_close(e2_file);
257 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
258 log_err("qh_ops->init_io failed", "");
259 ext2fs_file_close(e2_file);
266 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
268 struct ext2_inode inode;
271 err = ext2fs_read_inode(fs, ino, &inode);
273 log_err("ex2fs_read_inode failed", "");
277 if (EXT2_I_SIZE(&inode))
278 quota_inode_truncate(fs, ino);
280 memset(&inode, 0, sizeof(struct ext2_inode));
281 ext2fs_iblk_set(fs, &inode, 0);
282 inode.i_atime = inode.i_mtime =
283 inode.i_ctime = fs->now ? fs->now : time(0);
284 inode.i_links_count = 1;
285 inode.i_mode = LINUX_S_IFREG | 0600;
286 inode.i_flags |= EXT2_IMMUTABLE_FL;
287 if (fs->super->s_feature_incompat &
288 EXT3_FEATURE_INCOMPAT_EXTENTS)
289 inode.i_flags |= EXT4_EXTENTS_FL;
291 err = ext2fs_write_new_inode(fs, ino, &inode);
293 log_err("ext2fs_write_new_inode failed: %ld", err);
300 * Create new quotafile of specified format on given filesystem
302 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, int type, int fmt)
306 unsigned long qf_inum;
312 if (type == USRQUOTA)
313 qf_inum = EXT4_USR_QUOTA_INO;
314 else if (type == GRPQUOTA)
315 qf_inum = EXT4_GRP_QUOTA_INO;
319 err = ext2fs_read_bitmaps(fs);
323 err = quota_inode_init_new(fs, qf_inum);
325 log_err("init_new_quota_inode failed", "");
328 h->qh_qf.ino = qf_inum;
329 h->e2fs_write = quota_write_nomount;
330 h->e2fs_read = quota_read_nomount;
332 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
333 err = ext2fs_file_open(fs, qf_inum,
334 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file);
336 log_err("ext2fs_file_open failed: %d", err);
339 h->qh_qf.e2_file = e2_file;
344 memset(&h->qh_info, 0, sizeof(h->qh_info));
345 h->qh_ops = "afile_ops_2;
347 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
348 log_err("qh_ops->new_io failed", "");
355 ext2fs_file_close(e2_file);
359 quota_inode_truncate(fs, qf_inum);
365 * Close quotafile and release handle
367 errcode_t quota_file_close(struct quota_handle *h)
369 if (h->qh_io_flags & IOFL_INFODIRTY) {
370 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
372 h->qh_io_flags &= ~IOFL_INFODIRTY;
375 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
377 if (h->qh_qf.e2_file) {
378 ext2fs_file_flush(h->qh_qf.e2_file);
379 ext2fs_file_set_size2(h->qh_qf.e2_file,
380 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino));
381 ext2fs_file_close(h->qh_qf.e2_file);
388 * Create empty quota structure
390 struct dquot *get_empty_dquot(void)
394 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
395 log_err("Failed to allocate dquot", "");