Whamcloud - gitweb
LU-4017 e2fsprogs: clean up codes for adding new quota type
[tools/e2fsprogs.git] / lib / quota / quotaio.c
1 /** quotaio.c
2  *
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
6  */
7
8 #include "config.h"
9 #include <stdio.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/file.h>
18 #include <assert.h>
19
20 #include "common.h"
21 #include "quotaio.h"
22
23 static const char * const extensions[MAXQUOTAS] = {"user", "group"};
24 static const char * const basenames[] = {
25         "",             /* undefined */
26         "quota",        /* QFMT_VFS_OLD */
27         "aquota",       /* QFMT_VFS_V0 */
28         "",             /* QFMT_OCFS2 */
29         "aquota"        /* QFMT_VFS_V1 */
30 };
31
32 /* Header in all newer quotafiles */
33 struct disk_dqheader {
34         __u32 dqh_magic;
35         __u32 dqh_version;
36 } __attribute__ ((packed));
37
38 /**
39  * Convert type of quota to written representation
40  */
41 const char *quota_type2name(enum quota_type qtype)
42 {
43         if (qtype < 0 || qtype >= MAXQUOTAS)
44                 return "unknown";
45         return extensions[qtype];
46 }
47
48 ext2_ino_t quota_type2inum(enum quota_type qtype,
49                            struct ext2_super_block *sb)
50 {
51         switch (qtype) {
52         case USRQUOTA:
53                 return EXT4_USR_QUOTA_INO;
54         case GRPQUOTA:
55                 return EXT4_GRP_QUOTA_INO;
56         default:
57                 return 0;
58         }
59         return 0;
60 }
61
62 /**
63  * Creates a quota file name for given type and format.
64  */
65 const char *quota_get_qf_name(enum quota_type type, int fmt, char *buf)
66 {
67         if (!buf)
68                 return NULL;
69         snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
70                  basenames[fmt], extensions[type]);
71
72         return buf;
73 }
74
75 const char *quota_get_qf_path(const char *mntpt, enum quota_type qtype, int fmt,
76                               char *path_buf, size_t path_buf_size)
77 {
78         char qf_name[QUOTA_NAME_LEN];
79
80         if (!mntpt || !path_buf || !path_buf_size)
81                 return NULL;
82
83         strncpy(path_buf, mntpt, path_buf_size);
84         strncat(path_buf, "/", 1);
85         strncat(path_buf, quota_get_qf_name(qtype, fmt, qf_name),
86                 path_buf_size - strlen(path_buf));
87
88         return path_buf;
89 }
90
91 /*
92  * Set grace time if needed
93  */
94 void update_grace_times(struct dquot *q)
95 {
96         time_t now;
97
98         time(&now);
99         if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
100                         q->dq_dqb.dqb_bsoftlimit) {
101                 if (!q->dq_dqb.dqb_btime)
102                         q->dq_dqb.dqb_btime =
103                                 now + q->dq_h->qh_info.dqi_bgrace;
104         } else {
105                 q->dq_dqb.dqb_btime = 0;
106         }
107
108         if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
109                         q->dq_dqb.dqb_isoftlimit) {
110                 if (!q->dq_dqb.dqb_itime)
111                                 q->dq_dqb.dqb_itime =
112                                         now + q->dq_h->qh_info.dqi_igrace;
113         } else {
114                 q->dq_dqb.dqb_itime = 0;
115         }
116 }
117
118 static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
119                                e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
120                                blk64_t ref_block EXT2FS_ATTR((unused)),
121                                int ref_offset EXT2FS_ATTR((unused)),
122                                void *private)
123 {
124         blk64_t *num_blocks = private;
125
126         *num_blocks += 1;
127         return 0;
128 }
129
130 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
131 {
132         struct ext2_inode inode;
133         errcode_t err;
134         enum quota_type qtype;
135
136         if ((err = ext2fs_read_inode(fs, ino, &inode)))
137                 return err;
138
139         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
140                 if (ino == quota_type2inum(qtype, fs->super))
141                         break;
142
143         if (qtype != MAXQUOTAS) {
144                 inode.i_dtime = fs->now ? fs->now : time(0);
145                 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
146                         return 0;
147                 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
148                 if (err)
149                         return err;
150                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
151                 memset(&inode, 0, sizeof(struct ext2_inode));
152         } else {
153                 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
154         }
155         err = ext2fs_write_inode(fs, ino, &inode);
156         return err;
157 }
158
159 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
160 {
161         blk64_t num_blocks = 0;
162
163         ext2fs_block_iterate3(fs, ino,
164                               BLOCK_FLAG_READ_ONLY,
165                               NULL,
166                               compute_num_blocks_proc,
167                               &num_blocks);
168         return num_blocks * fs->blocksize;
169 }
170
171 /* Functions to read/write quota file. */
172 static unsigned int quota_write_nomount(struct quota_file *qf,
173                                         ext2_loff_t offset,
174                                         void *buf, unsigned int size)
175 {
176         ext2_file_t     e2_file = qf->e2_file;
177         unsigned int    bytes_written = 0;
178         errcode_t       err;
179
180         err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
181         if (err) {
182                 log_err("ext2fs_file_llseek failed: %ld", err);
183                 return 0;
184         }
185
186         err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
187         if (err) {
188                 log_err("ext2fs_file_write failed: %ld", err);
189                 return 0;
190         }
191
192         /* Correct inode.i_size is set in end_io. */
193         return bytes_written;
194 }
195
196 static unsigned int quota_read_nomount(struct quota_file *qf,
197                                        ext2_loff_t offset,
198                                        void *buf, unsigned int size)
199 {
200         ext2_file_t     e2_file = qf->e2_file;
201         unsigned int    bytes_read = 0;
202         errcode_t       err;
203
204         err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
205         if (err) {
206                 log_err("ext2fs_file_llseek failed: %ld", err);
207                 return 0;
208         }
209
210         err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
211         if (err) {
212                 log_err("ext2fs_file_read failed: %ld", err);
213                 return 0;
214         }
215
216         return bytes_read;
217 }
218
219 /*
220  * Detect quota format and initialize quota IO
221  */
222 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
223                           ext2_ino_t qf_ino, enum quota_type qtype,
224                           int fmt, int flags)
225 {
226         ext2_filsys fs = qctx->fs;
227         ext2_file_t e2_file;
228         errcode_t err;
229         int allocated_handle = 0;
230
231         if (qtype >= MAXQUOTAS)
232                 return EINVAL;
233
234         if (fmt == -1)
235                 fmt = QFMT_VFS_V1;
236
237         err = ext2fs_read_bitmaps(fs);
238         if (err)
239                 return err;
240
241         if (qf_ino == 0)
242                 qf_ino = *quota_sb_inump(fs->super, qtype)
243
244         log_debug("Opening quota ino=%lu, type=%d", qf_ino, qtype);
245         err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
246         if (err) {
247                 log_err("ext2fs_file_open failed: %s", error_message(err));
248                 return err;
249         }
250
251         if (!h) {
252                 if (qctx->quota_file[qtype]) {
253                         h = qctx->quota_file[qtype];
254                         if (((flags & EXT2_FILE_WRITE) == 0) ||
255                             (h->qh_file_flags & EXT2_FILE_WRITE))
256                                 return 0;
257                         (void) quota_file_close(qctx, h);
258                 }
259                 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
260                 if (err) {
261                         log_err("Unable to allocate quota handle");
262                         return err;
263                 }
264                 allocated_handle = 1;
265         }
266
267         h->qh_qf.e2_file = e2_file;
268         h->qh_qf.fs = fs;
269         h->qh_qf.ino = qf_ino;
270         h->e2fs_write = quota_write_nomount;
271         h->e2fs_read = quota_read_nomount;
272         h->qh_file_flags = flags;
273         h->qh_io_flags = 0;
274         h->qh_type = qtype;
275         h->qh_fmt = fmt;
276         memset(&h->qh_info, 0, sizeof(h->qh_info));
277         h->qh_ops = &quotafile_ops_2;
278
279         if (h->qh_ops->check_file &&
280             (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
281                 log_err("qh_ops->check_file failed");
282                 goto errout;
283         }
284
285         if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
286                 log_err("qh_ops->init_io failed");
287                 goto errout;
288         }
289         if (allocated_handle)
290                 qctx->quota_file[qtype] = h;
291
292         return 0;
293 errout:
294         ext2fs_file_close(e2_file);
295         if (allocated_handle)
296                 ext2fs_free_mem(&h);
297         return -1;
298 }
299
300 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
301 {
302         struct ext2_inode inode;
303         errcode_t err = 0;
304
305         err = ext2fs_read_inode(fs, ino, &inode);
306         if (err) {
307                 log_err("ex2fs_read_inode failed");
308                 return err;
309         }
310
311         if (EXT2_I_SIZE(&inode))
312                 quota_inode_truncate(fs, ino);
313
314         memset(&inode, 0, sizeof(struct ext2_inode));
315         ext2fs_iblk_set(fs, &inode, 0);
316         inode.i_atime = inode.i_mtime =
317                 inode.i_ctime = fs->now ? fs->now : time(0);
318         inode.i_links_count = 1;
319         inode.i_mode = LINUX_S_IFREG | 0600;
320         inode.i_flags |= EXT2_IMMUTABLE_FL;
321         if (fs->super->s_feature_incompat &
322                         EXT3_FEATURE_INCOMPAT_EXTENTS)
323                 inode.i_flags |= EXT4_EXTENTS_FL;
324
325         err = ext2fs_write_new_inode(fs, ino, &inode);
326         if (err) {
327                 log_err("ext2fs_write_new_inode failed: %ld", err);
328                 return err;
329         }
330         return err;
331 }
332
333 /*
334  * Create new quotafile of specified format on given filesystem
335  */
336 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
337                             enum quota_type qtype, int fmt)
338 {
339         ext2_file_t e2_file;
340         int err;
341         unsigned long qf_inum;
342
343         if (fmt == -1)
344                 fmt = QFMT_VFS_V1;
345
346         h->qh_qf.fs = fs;
347         qf_inum = quota_type2inum(qtype, fs->super);
348         if (qf_inum == 0)
349                 return -1;
350
351         err = ext2fs_read_bitmaps(fs);
352         if (err)
353                 goto out_err;
354
355         err = quota_inode_init_new(fs, qf_inum);
356         if (err) {
357                 log_err("init_new_quota_inode failed");
358                 goto out_err;
359         }
360         h->qh_qf.ino = qf_inum;
361         h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
362         h->e2fs_write = quota_write_nomount;
363         h->e2fs_read = quota_read_nomount;
364
365         log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
366         err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
367         if (err) {
368                 log_err("ext2fs_file_open failed: %d", err);
369                 goto out_err;
370         }
371         h->qh_qf.e2_file = e2_file;
372
373         h->qh_io_flags = 0;
374         h->qh_type = qtype;
375         h->qh_fmt = fmt;
376         memset(&h->qh_info, 0, sizeof(h->qh_info));
377         h->qh_ops = &quotafile_ops_2;
378
379         if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
380                 log_err("qh_ops->new_io failed");
381                 goto out_err1;
382         }
383
384         return 0;
385
386 out_err1:
387         ext2fs_file_close(e2_file);
388 out_err:
389
390         if (qf_inum)
391                 quota_inode_truncate(fs, qf_inum);
392
393         return -1;
394 }
395
396 /*
397  * Close quotafile and release handle
398  */
399 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
400 {
401         if (h->qh_io_flags & IOFL_INFODIRTY) {
402                 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
403                         return -1;
404                 h->qh_io_flags &= ~IOFL_INFODIRTY;
405         }
406
407         if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
408                 return -1;
409         if (h->qh_qf.e2_file) {
410                 __u64 new_size, size;
411
412                 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
413                 ext2fs_file_flush(h->qh_qf.e2_file);
414                 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
415                         new_size = 0;
416                 if (size != new_size)
417                         ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
418                 ext2fs_file_close(h->qh_qf.e2_file);
419         }
420         if (qctx->quota_file[h->qh_type] == h)
421                 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
422         return 0;
423 }
424
425 /*
426  * Create empty quota structure
427  */
428 struct dquot *get_empty_dquot(void)
429 {
430         struct dquot *dquot;
431
432         if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
433                 log_err("Failed to allocate dquot");
434                 return NULL;
435         }
436
437         dquot->dq_id = -1;
438         return dquot;
439 }