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 * const extensions[MAXQUOTAS] = {"user", "group"};
22 static const char * const basenames[] = {
24 "quota", /* QFMT_VFS_OLD */
25 "aquota", /* QFMT_VFS_V0 */
27 "aquota" /* QFMT_VFS_V1 */
30 /* Header in all newer quotafiles */
31 struct disk_dqheader {
33 u_int32_t dqh_version;
34 } __attribute__ ((packed));
37 * Convert type of quota to written representation
39 const char *type2name(int type)
41 return extensions[type];
45 * Creates a quota file name for given type and format.
47 const char *quota_get_qf_name(int type, int fmt, char *buf)
51 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
52 basenames[fmt], extensions[type]);
57 const char *quota_get_qf_path(const char *mntpt, int qtype, int fmt,
58 char *path_buf, size_t path_buf_size)
60 char qf_name[QUOTA_NAME_LEN];
62 if (!mntpt || !path_buf || !path_buf_size)
65 strncpy(path_buf, mntpt, path_buf_size);
66 strncat(path_buf, "/", 1);
67 strncat(path_buf, quota_get_qf_name(qtype, fmt, qf_name),
68 path_buf_size - strlen(path_buf));
74 * Set grace time if needed
76 void update_grace_times(struct dquot *q)
81 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
82 q->dq_dqb.dqb_bsoftlimit) {
83 if (!q->dq_dqb.dqb_btime)
85 now + q->dq_h->qh_info.dqi_bgrace;
87 q->dq_dqb.dqb_btime = 0;
90 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
91 q->dq_dqb.dqb_isoftlimit) {
92 if (!q->dq_dqb.dqb_itime)
94 now + q->dq_h->qh_info.dqi_igrace;
96 q->dq_dqb.dqb_itime = 0;
100 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
101 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
102 blk64_t ref_block EXT2FS_ATTR((unused)),
103 int ref_offset EXT2FS_ATTR((unused)),
106 blk64_t *num_blocks = private;
112 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
114 struct ext2_inode inode;
117 if ((err = ext2fs_read_inode(fs, ino, &inode)))
120 if ((ino == EXT4_USR_QUOTA_INO) || (ino == EXT4_GRP_QUOTA_INO)) {
121 inode.i_dtime = fs->now ? fs->now : time(0);
122 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
124 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
127 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
128 memset(&inode, 0, sizeof(struct ext2_inode));
130 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
132 err = ext2fs_write_inode(fs, ino, &inode);
136 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
138 blk64_t num_blocks = 0;
140 ext2fs_block_iterate3(fs, ino,
141 BLOCK_FLAG_READ_ONLY,
143 compute_num_blocks_proc,
145 return num_blocks * fs->blocksize;
148 /* Functions to read/write quota file. */
149 static unsigned int quota_write_nomount(struct quota_file *qf,
151 void *buf, unsigned int size)
153 ext2_file_t e2_file = qf->e2_file;
154 unsigned int bytes_written = 0;
157 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
159 log_err("ext2fs_file_llseek failed: %ld", err);
163 err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
165 log_err("ext2fs_file_write failed: %ld", err);
169 /* Correct inode.i_size is set in end_io. */
170 return bytes_written;
173 static unsigned int quota_read_nomount(struct quota_file *qf,
175 void *buf, unsigned int size)
177 ext2_file_t e2_file = qf->e2_file;
178 unsigned int bytes_read = 0;
181 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
183 log_err("ext2fs_file_llseek failed: %ld", err);
187 err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
189 log_err("ext2fs_file_read failed: %ld", err);
197 * Detect quota format and initialize quota IO
199 errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs,
200 ext2_ino_t qf_ino, int type, int fmt, int flags)
208 err = ext2fs_read_bitmaps(fs);
212 log_debug("Opening quota ino=%lu, type=%d", qf_ino, type);
213 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
215 log_err("ext2fs_file_open failed: %s", error_message(err));
218 h->qh_qf.e2_file = e2_file;
221 h->qh_qf.ino = qf_ino;
222 h->e2fs_write = quota_write_nomount;
223 h->e2fs_read = quota_read_nomount;
227 memset(&h->qh_info, 0, sizeof(h->qh_info));
228 h->qh_ops = "afile_ops_2;
230 if (h->qh_ops->check_file &&
231 (h->qh_ops->check_file(h, type, fmt) == 0)) {
232 log_err("qh_ops->check_file failed");
233 ext2fs_file_close(e2_file);
237 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
238 log_err("qh_ops->init_io failed");
239 ext2fs_file_close(e2_file);
246 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
248 struct ext2_inode inode;
251 err = ext2fs_read_inode(fs, ino, &inode);
253 log_err("ex2fs_read_inode failed");
257 if (EXT2_I_SIZE(&inode))
258 quota_inode_truncate(fs, ino);
260 memset(&inode, 0, sizeof(struct ext2_inode));
261 ext2fs_iblk_set(fs, &inode, 0);
262 inode.i_atime = inode.i_mtime =
263 inode.i_ctime = fs->now ? fs->now : time(0);
264 inode.i_links_count = 1;
265 inode.i_mode = LINUX_S_IFREG | 0600;
266 inode.i_flags |= EXT2_IMMUTABLE_FL;
267 if (fs->super->s_feature_incompat &
268 EXT3_FEATURE_INCOMPAT_EXTENTS)
269 inode.i_flags |= EXT4_EXTENTS_FL;
271 err = ext2fs_write_new_inode(fs, ino, &inode);
273 log_err("ext2fs_write_new_inode failed: %ld", err);
280 * Create new quotafile of specified format on given filesystem
282 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, int type, int fmt)
286 unsigned long qf_inum;
292 if (type == USRQUOTA)
293 qf_inum = EXT4_USR_QUOTA_INO;
294 else if (type == GRPQUOTA)
295 qf_inum = EXT4_GRP_QUOTA_INO;
299 err = ext2fs_read_bitmaps(fs);
303 err = quota_inode_init_new(fs, qf_inum);
305 log_err("init_new_quota_inode failed");
308 h->qh_qf.ino = qf_inum;
309 h->e2fs_write = quota_write_nomount;
310 h->e2fs_read = quota_read_nomount;
312 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
313 err = ext2fs_file_open(fs, qf_inum,
314 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file);
316 log_err("ext2fs_file_open failed: %d", err);
319 h->qh_qf.e2_file = e2_file;
324 memset(&h->qh_info, 0, sizeof(h->qh_info));
325 h->qh_ops = "afile_ops_2;
327 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
328 log_err("qh_ops->new_io failed");
335 ext2fs_file_close(e2_file);
339 quota_inode_truncate(fs, qf_inum);
345 * Close quotafile and release handle
347 errcode_t quota_file_close(struct quota_handle *h)
349 if (h->qh_io_flags & IOFL_INFODIRTY) {
350 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
352 h->qh_io_flags &= ~IOFL_INFODIRTY;
355 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
357 if (h->qh_qf.e2_file) {
358 ext2fs_file_flush(h->qh_qf.e2_file);
359 ext2fs_file_set_size2(h->qh_qf.e2_file,
360 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino));
361 ext2fs_file_close(h->qh_qf.e2_file);
368 * Create empty quota structure
370 struct dquot *get_empty_dquot(void)
374 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
375 log_err("Failed to allocate dquot");