Whamcloud - gitweb
ba3a42291c5bf8ef7084a837ffcf16f5128a5311
[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 < 0 || 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=%lu, 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                                 return 0;
248                         (void) quota_file_close(qctx, h);
249                 }
250                 err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
251                 if (err) {
252                         log_err("Unable to allocate quota handle");
253                         return err;
254                 }
255                 allocated_handle = 1;
256         }
257
258         h->qh_qf.e2_file = e2_file;
259         h->qh_qf.fs = fs;
260         h->qh_qf.ino = qf_ino;
261         h->e2fs_write = quota_write_nomount;
262         h->e2fs_read = quota_read_nomount;
263         h->qh_file_flags = flags;
264         h->qh_io_flags = 0;
265         h->qh_type = qtype;
266         h->qh_fmt = fmt;
267         memset(&h->qh_info, 0, sizeof(h->qh_info));
268         h->qh_ops = &quotafile_ops_2;
269
270         if (h->qh_ops->check_file &&
271             (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
272                 log_err("qh_ops->check_file failed");
273                 goto errout;
274         }
275
276         if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
277                 log_err("qh_ops->init_io failed");
278                 goto errout;
279         }
280         if (allocated_handle)
281                 qctx->quota_file[qtype] = h;
282
283         return 0;
284 errout:
285         ext2fs_file_close(e2_file);
286         if (allocated_handle)
287                 ext2fs_free_mem(&h);
288         return -1;
289 }
290
291 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
292 {
293         struct ext2_inode inode;
294         errcode_t err = 0;
295
296         err = ext2fs_read_inode(fs, ino, &inode);
297         if (err) {
298                 log_err("ex2fs_read_inode failed");
299                 return err;
300         }
301
302         if (EXT2_I_SIZE(&inode))
303                 quota_inode_truncate(fs, ino);
304
305         memset(&inode, 0, sizeof(struct ext2_inode));
306         ext2fs_iblk_set(fs, &inode, 0);
307         inode.i_atime = inode.i_mtime =
308                 inode.i_ctime = fs->now ? fs->now : time(0);
309         inode.i_links_count = 1;
310         inode.i_mode = LINUX_S_IFREG | 0600;
311         inode.i_flags |= EXT2_IMMUTABLE_FL;
312         if (ext2fs_has_feature_extents(fs->super))
313                 inode.i_flags |= EXT4_EXTENTS_FL;
314
315         err = ext2fs_write_new_inode(fs, ino, &inode);
316         if (err) {
317                 log_err("ext2fs_write_new_inode failed: %ld", err);
318                 return err;
319         }
320         return err;
321 }
322
323 /*
324  * Create new quotafile of specified format on given filesystem
325  */
326 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
327                             enum quota_type qtype, int fmt)
328 {
329         ext2_file_t e2_file;
330         int err;
331         unsigned long qf_inum = 0;
332
333         if (fmt == -1)
334                 fmt = QFMT_VFS_V1;
335
336         h->qh_qf.fs = fs;
337         qf_inum = quota_type2inum(qtype, fs->super);
338         if (qf_inum == 0 && qtype == PRJQUOTA) {
339                 err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
340                                        0, &qf_inum);
341                 if (err)
342                         return -1;
343                 ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0);
344                 ext2fs_mark_ib_dirty(fs);
345         } else if (qf_inum == 0) {
346                 return -1;
347         }
348
349         err = ext2fs_read_bitmaps(fs);
350         if (err)
351                 goto out_err;
352
353         err = quota_inode_init_new(fs, qf_inum);
354         if (err) {
355                 log_err("init_new_quota_inode failed");
356                 goto out_err;
357         }
358         h->qh_qf.ino = qf_inum;
359         h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
360         h->e2fs_write = quota_write_nomount;
361         h->e2fs_read = quota_read_nomount;
362
363         log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
364         err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
365         if (err) {
366                 log_err("ext2fs_file_open failed: %d", err);
367                 goto out_err;
368         }
369         h->qh_qf.e2_file = e2_file;
370
371         h->qh_io_flags = 0;
372         h->qh_type = qtype;
373         h->qh_fmt = fmt;
374         memset(&h->qh_info, 0, sizeof(h->qh_info));
375         h->qh_ops = &quotafile_ops_2;
376
377         if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
378                 log_err("qh_ops->new_io failed");
379                 goto out_err1;
380         }
381
382         return 0;
383
384 out_err1:
385         ext2fs_file_close(e2_file);
386 out_err:
387
388         if (qf_inum)
389                 quota_inode_truncate(fs, qf_inum);
390
391         return -1;
392 }
393
394 /*
395  * Close quotafile and release handle
396  */
397 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
398 {
399         if (h->qh_io_flags & IOFL_INFODIRTY) {
400                 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
401                         return -1;
402                 h->qh_io_flags &= ~IOFL_INFODIRTY;
403         }
404
405         if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
406                 return -1;
407         if (h->qh_qf.e2_file) {
408                 __u64 new_size, size;
409
410                 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
411                 ext2fs_file_flush(h->qh_qf.e2_file);
412                 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
413                         new_size = 0;
414                 if (size != new_size)
415                         ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
416                 ext2fs_file_close(h->qh_qf.e2_file);
417         }
418         if (qctx->quota_file[h->qh_type] == h)
419                 ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
420         return 0;
421 }
422
423 /*
424  * Create empty quota structure
425  */
426 struct dquot *get_empty_dquot(void)
427 {
428         struct dquot *dquot;
429
430         if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
431                 log_err("Failed to allocate dquot");
432                 return NULL;
433         }
434
435         dquot->dq_id = -1;
436         return dquot;
437 }