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>
17 #include <asm/byteorder.h>
22 static const char extensions[MAXQUOTAS + 2][20] = INITQFNAMES;
23 static const char * const basenames[] = {
25 "quota", /* QFMT_VFS_OLD */
26 "aquota", /* QFMT_VFS_V0 */
28 "aquota" /* QFMT_VFS_V1 */
31 static const char * const fmtnames[] = {
39 /* Header in all newer quotafiles */
40 struct disk_dqheader {
42 u_int32_t dqh_version;
43 } __attribute__ ((packed));
46 * Convert type of quota to written representation
48 const char *type2name(int type)
50 return extensions[type];
54 * Creates a quota file name for given type and format.
56 const char *get_qf_name(int type, int fmt, char *buf)
59 snprintf(buf, PATH_MAX, "%s.%s",
60 basenames[fmt], extensions[type]);
65 const char *get_qf_path(const char *mntpt, int qtype, int fmt,
66 char *path_buf, size_t path_buf_size)
69 char qf_name[PATH_MAX] = {0};
73 BUG_ON(!path_buf_size);
75 strncpy(path_buf, mntpt, path_buf_size);
76 strncat(path_buf, "/", 1);
77 strncat(path_buf, get_qf_name(qtype, fmt, qf_name),
78 path_buf_size - strlen(path_buf));
84 * Set grace time if needed
86 void update_grace_times(struct dquot *q)
91 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
92 q->dq_dqb.dqb_bsoftlimit) {
93 if (!q->dq_dqb.dqb_btime)
95 now + q->dq_h->qh_info.dqi_bgrace;
97 q->dq_dqb.dqb_btime = 0;
100 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
101 q->dq_dqb.dqb_isoftlimit) {
102 if (!q->dq_dqb.dqb_itime)
103 q->dq_dqb.dqb_itime =
104 now + q->dq_h->qh_info.dqi_igrace;
106 q->dq_dqb.dqb_itime = 0;
110 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
111 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
112 blk64_t ref_block EXT2FS_ATTR((unused)),
113 int ref_offset EXT2FS_ATTR((unused)),
114 void *private EXT2FS_ATTR((unused)))
119 ext2fs_block_alloc_stats2(fs, block, -1);
123 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
124 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
125 blk64_t ref_block EXT2FS_ATTR((unused)),
126 int ref_offset EXT2FS_ATTR((unused)),
130 blk64_t *num_blocks = private;
136 void truncate_quota_inode(ext2_filsys fs, ext2_ino_t ino)
138 struct ext2_inode inode;
141 if (ext2fs_read_inode(fs, ino, &inode))
144 inode.i_dtime = fs->now ? fs->now : time(0);
145 if (!ext2fs_inode_has_valid_blocks(&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 ext2fs_write_inode(fs, ino, &inode);
155 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
157 struct ext2_inode inode;
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, loff_t offset,
170 void *buf, unsigned int size)
172 ext2_file_t e2_file = qf->e2_file;
173 unsigned int bytes_written = 0;
176 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
178 log_err("ext2fs_file_llseek failed: %ld", err);
182 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
184 log_err("ext2fs_file_write failed: %ld", err);
188 /* Correct inode.i_size is set in end_io. */
189 return bytes_written;
192 static unsigned int quota_read_nomount(struct quota_file *qf, loff_t offset,
193 void *buf, unsigned int size)
195 ext2_file_t e2_file = qf->e2_file;
196 unsigned int bytes_read = 0;
199 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
201 log_err("ext2fs_file_llseek failed: %ld", err);
205 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
207 log_err("ext2fs_file_read failed: %ld", err);
215 * Detect quota format and initialize quota IO
217 struct quota_handle *init_io(ext2_filsys fs, const char *mntpt, int type,
220 log_err("Not Implemented.", "");
225 static errcode_t init_new_quota_inode(ext2_filsys fs, ext2_ino_t ino)
227 struct ext2_inode inode;
230 err = ext2fs_read_inode(fs, ino, &inode);
232 log_err("ex2fs_read_inode failed", "");
236 if (EXT2_I_SIZE(&inode))
237 truncate_quota_inode(fs, ino);
239 memset(&inode, 0, sizeof(struct ext2_inode));
240 ext2fs_iblk_set(fs, &inode, 0);
241 inode.i_atime = inode.i_mtime =
242 inode.i_ctime = fs->now ? fs->now : time(0);
243 inode.i_links_count = 1;
244 inode.i_mode = LINUX_S_IFREG | 0600;
245 inode.i_flags |= EXT2_IMMUTABLE_FL;
246 if (fs->super->s_feature_incompat &
247 EXT3_FEATURE_INCOMPAT_EXTENTS)
248 inode.i_flags |= EXT4_EXTENTS_FL;
250 err = ext2fs_write_new_inode(fs, ino, &inode);
252 log_err("ext2fs_write_new_inode failed: %ld", err);
259 * Create new quotafile of specified format on given filesystem
261 int new_io(struct quota_handle *h, ext2_filsys fs, int type, int fmt)
265 const char *mnt_fsname;
266 char qf_name[PATH_MAX];
268 struct ext2_inode inode;
269 unsigned long qf_inum;
276 if (type == USRQUOTA)
277 qf_inum = EXT4_USR_QUOTA_INO;
278 else if (type == GRPQUOTA)
279 qf_inum = EXT4_GRP_QUOTA_INO;
282 err = ext2fs_read_bitmaps(fs);
286 err = init_new_quota_inode(fs, qf_inum);
288 log_err("init_new_quota_inode failed", "");
291 h->qh_qf.ino = qf_inum;
292 h->e2fs_write = quota_write_nomount;
293 h->e2fs_read = quota_read_nomount;
295 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
296 err = ext2fs_file_open(fs, qf_inum,
297 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file);
299 log_err("ext2fs_file_open failed: %d", err);
302 h->qh_qf.e2_file = e2_file;
307 memset(&h->qh_info, 0, sizeof(h->qh_info));
308 h->qh_ops = "afile_ops_2;
310 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
311 log_err("qh_ops->new_io failed", "");
318 ext2fs_file_close(e2_file);
322 truncate_quota_inode(fs, qf_inum);
328 * Close quotafile and release handle
330 int end_io(struct quota_handle *h)
332 if (h->qh_io_flags & IOFL_INFODIRTY) {
333 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
335 h->qh_io_flags &= ~IOFL_INFODIRTY;
338 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
340 if (h->qh_qf.e2_file) {
341 ext2fs_file_flush(h->qh_qf.e2_file);
342 ext2fs_file_set_size2(h->qh_qf.e2_file,
343 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino));
344 ext2fs_file_close(h->qh_qf.e2_file);
351 * Create empty quota structure
353 struct dquot *get_empty_dquot(void)
355 struct dquot *dquot = smalloc(sizeof(struct dquot));
357 memset(dquot, 0, sizeof(*dquot));