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