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