blob: 8ddb92a7226891e867e8cad67320c879095d72da [file] [log] [blame]
Aditya Kalif239fef2011-07-20 11:40:02 -07001/** 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
Theodore Ts'od1154eb2011-09-18 17:34:37 -04008#include "config.h"
Aditya Kalif239fef2011-07-20 11:40:02 -07009#include <stdio.h>
10#include <errno.h>
11#include <string.h>
12#include <unistd.h>
13#include <stdlib.h>
Mike Frysinger88f8f332012-05-28 10:21:19 -040014#include <time.h>
Aditya Kalif239fef2011-07-20 11:40:02 -070015#include <sys/types.h>
16#include <sys/stat.h>
17#include <sys/file.h>
Aditya Kalif239fef2011-07-20 11:40:02 -070018
19#include "common.h"
20#include "quotaio.h"
21
Jan Kara2ae58b62012-06-04 12:51:55 -040022static const char * const extensions[MAXQUOTAS] = {"user", "group"};
Aditya Kalif239fef2011-07-20 11:40:02 -070023static 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
Aditya Kalif239fef2011-07-20 11:40:02 -070031/* Header in all newer quotafiles */
32struct disk_dqheader {
33 u_int32_t dqh_magic;
34 u_int32_t dqh_version;
35} __attribute__ ((packed));
36
37/**
38 * Convert type of quota to written representation
39 */
40const char *type2name(int type)
41{
42 return extensions[type];
43}
44
Aditya Kalia86d55d2011-11-14 10:55:54 -050045/**
46 * Creates a quota file name for given type and format.
47 */
48const char *quota_get_qf_name(int type, int fmt, char *buf)
49{
50 if (!buf)
51 return NULL;
Theodore Ts'o2f7d8552011-11-27 20:31:36 -050052 snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
Aditya Kalia86d55d2011-11-14 10:55:54 -050053 basenames[fmt], extensions[type]);
54
55 return buf;
56}
57
58const char *quota_get_qf_path(const char *mntpt, int qtype, int fmt,
59 char *path_buf, size_t path_buf_size)
60{
Theodore Ts'o2f7d8552011-11-27 20:31:36 -050061 char qf_name[QUOTA_NAME_LEN];
Aditya Kalia86d55d2011-11-14 10:55:54 -050062
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
Aditya Kalif239fef2011-07-20 11:40:02 -070074/*
75 * Set grace time if needed
76 */
77void 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
101static int release_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 EXT2FS_ATTR((unused)))
106{
107 blk64_t block;
108
109 block = *blocknr;
110 ext2fs_block_alloc_stats2(fs, block, -1);
111 return 0;
112}
113
114static int compute_num_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
115 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
116 blk64_t ref_block EXT2FS_ATTR((unused)),
117 int ref_offset EXT2FS_ATTR((unused)),
118 void *private)
119{
Aditya Kalif239fef2011-07-20 11:40:02 -0700120 blk64_t *num_blocks = private;
121
122 *num_blocks += 1;
123 return 0;
124}
125
Aditya Kalia86d55d2011-11-14 10:55:54 -0500126errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
Aditya Kalif239fef2011-07-20 11:40:02 -0700127{
128 struct ext2_inode inode;
Aditya Kalia86d55d2011-11-14 10:55:54 -0500129 errcode_t err;
Aditya Kalif239fef2011-07-20 11:40:02 -0700130
Aditya Kalia86d55d2011-11-14 10:55:54 -0500131 if ((err = ext2fs_read_inode(fs, ino, &inode)))
132 return err;
Aditya Kalif239fef2011-07-20 11:40:02 -0700133
Aditya Kaliad649022012-07-13 15:25:07 -0700134 if ((ino == EXT4_USR_QUOTA_INO) || (ino == EXT4_GRP_QUOTA_INO)) {
135 inode.i_dtime = fs->now ? fs->now : time(0);
136 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
137 return 0;
Aditya Kalif239fef2011-07-20 11:40:02 -0700138
Aditya Kaliad649022012-07-13 15:25:07 -0700139 ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
140 release_blocks_proc, NULL);
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 }
Aditya Kalia86d55d2011-11-14 10:55:54 -0500146 err = ext2fs_write_inode(fs, ino, &inode);
147 return err;
Aditya Kalif239fef2011-07-20 11:40:02 -0700148}
149
150static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
151{
Aditya Kalif239fef2011-07-20 11:40:02 -0700152 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. */
Theodore Ts'od6120a22011-10-04 11:46:21 -0400163static unsigned int quota_write_nomount(struct quota_file *qf,
164 ext2_loff_t offset,
Aditya Kalif239fef2011-07-20 11:40:02 -0700165 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
Theodore Ts'od6120a22011-10-04 11:46:21 -0400187static unsigned int quota_read_nomount(struct quota_file *qf,
188 ext2_loff_t offset,
189 void *buf, unsigned int size)
Aditya Kalif239fef2011-07-20 11:40:02 -0700190{
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 */
Aditya Kalia86d55d2011-11-14 10:55:54 -0500213errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs,
Niu198d20f2011-11-14 10:58:28 -0500214 ext2_ino_t qf_ino, int type, int fmt, int flags)
Aditya Kalif239fef2011-07-20 11:40:02 -0700215{
Niu198d20f2011-11-14 10:58:28 -0500216 ext2_file_t e2_file;
217 errcode_t err;
218
219 if (fmt == -1)
220 fmt = QFMT_VFS_V1;
221
222 err = ext2fs_read_bitmaps(fs);
223 if (err)
224 return err;
225
226 log_debug("Opening quota ino=%lu, type=%d", qf_ino, type);
227 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
228 if (err) {
Theodore Ts'o2b8d6832011-11-14 13:33:17 -0500229 log_err("ext2fs_file_open failed: %s", error_message(err));
Niu198d20f2011-11-14 10:58:28 -0500230 return err;
231 }
232 h->qh_qf.e2_file = e2_file;
233
234 h->qh_qf.fs = fs;
235 h->qh_qf.ino = qf_ino;
236 h->e2fs_write = quota_write_nomount;
237 h->e2fs_read = quota_read_nomount;
238 h->qh_io_flags = 0;
239 h->qh_type = type;
240 h->qh_fmt = fmt;
241 memset(&h->qh_info, 0, sizeof(h->qh_info));
242 h->qh_ops = &quotafile_ops_2;
243
244 if (h->qh_ops->check_file &&
245 (h->qh_ops->check_file(h, type, fmt) == 0)) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700246 log_err("qh_ops->check_file failed");
Niu198d20f2011-11-14 10:58:28 -0500247 ext2fs_file_close(e2_file);
248 return -1;
249 }
250
251 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700252 log_err("qh_ops->init_io failed");
Niu198d20f2011-11-14 10:58:28 -0500253 ext2fs_file_close(e2_file);
254 return -1;
255 }
256
257 return 0;
Aditya Kalif239fef2011-07-20 11:40:02 -0700258}
259
Aditya Kalia86d55d2011-11-14 10:55:54 -0500260static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
Aditya Kalif239fef2011-07-20 11:40:02 -0700261{
262 struct ext2_inode inode;
263 errcode_t err = 0;
264
265 err = ext2fs_read_inode(fs, ino, &inode);
266 if (err) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700267 log_err("ex2fs_read_inode failed");
Aditya Kalif239fef2011-07-20 11:40:02 -0700268 return err;
269 }
270
271 if (EXT2_I_SIZE(&inode))
Aditya Kalia86d55d2011-11-14 10:55:54 -0500272 quota_inode_truncate(fs, ino);
Aditya Kalif239fef2011-07-20 11:40:02 -0700273
274 memset(&inode, 0, sizeof(struct ext2_inode));
275 ext2fs_iblk_set(fs, &inode, 0);
276 inode.i_atime = inode.i_mtime =
277 inode.i_ctime = fs->now ? fs->now : time(0);
278 inode.i_links_count = 1;
279 inode.i_mode = LINUX_S_IFREG | 0600;
280 inode.i_flags |= EXT2_IMMUTABLE_FL;
281 if (fs->super->s_feature_incompat &
282 EXT3_FEATURE_INCOMPAT_EXTENTS)
283 inode.i_flags |= EXT4_EXTENTS_FL;
284
285 err = ext2fs_write_new_inode(fs, ino, &inode);
286 if (err) {
287 log_err("ext2fs_write_new_inode failed: %ld", err);
288 return err;
289 }
290 return err;
291}
292
293/*
294 * Create new quotafile of specified format on given filesystem
295 */
Aditya Kalia86d55d2011-11-14 10:55:54 -0500296errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, int type, int fmt)
Aditya Kalif239fef2011-07-20 11:40:02 -0700297{
Aditya Kalif239fef2011-07-20 11:40:02 -0700298 ext2_file_t e2_file;
Aditya Kalif239fef2011-07-20 11:40:02 -0700299 int err;
Aditya Kalif239fef2011-07-20 11:40:02 -0700300 unsigned long qf_inum;
Aditya Kalif239fef2011-07-20 11:40:02 -0700301
302 if (fmt == -1)
303 fmt = QFMT_VFS_V1;
304
305 h->qh_qf.fs = fs;
306 if (type == USRQUOTA)
307 qf_inum = EXT4_USR_QUOTA_INO;
308 else if (type == GRPQUOTA)
309 qf_inum = EXT4_GRP_QUOTA_INO;
310 else
Aditya Kalia86d55d2011-11-14 10:55:54 -0500311 return -1;
312
Aditya Kalif239fef2011-07-20 11:40:02 -0700313 err = ext2fs_read_bitmaps(fs);
314 if (err)
315 goto out_err;
316
Aditya Kalia86d55d2011-11-14 10:55:54 -0500317 err = quota_inode_init_new(fs, qf_inum);
Aditya Kalif239fef2011-07-20 11:40:02 -0700318 if (err) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700319 log_err("init_new_quota_inode failed");
Aditya Kalif239fef2011-07-20 11:40:02 -0700320 goto out_err;
321 }
322 h->qh_qf.ino = qf_inum;
323 h->e2fs_write = quota_write_nomount;
324 h->e2fs_read = quota_read_nomount;
325
326 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
327 err = ext2fs_file_open(fs, qf_inum,
328 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file);
329 if (err) {
330 log_err("ext2fs_file_open failed: %d", err);
331 goto out_err;
332 }
333 h->qh_qf.e2_file = e2_file;
334
335 h->qh_io_flags = 0;
336 h->qh_type = type;
337 h->qh_fmt = fmt;
338 memset(&h->qh_info, 0, sizeof(h->qh_info));
339 h->qh_ops = &quotafile_ops_2;
340
341 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700342 log_err("qh_ops->new_io failed");
Aditya Kalif239fef2011-07-20 11:40:02 -0700343 goto out_err1;
344 }
345
346 return 0;
347
348out_err1:
349 ext2fs_file_close(e2_file);
350out_err:
351
352 if (qf_inum)
Aditya Kalia86d55d2011-11-14 10:55:54 -0500353 quota_inode_truncate(fs, qf_inum);
Aditya Kalif239fef2011-07-20 11:40:02 -0700354
355 return -1;
356}
357
358/*
359 * Close quotafile and release handle
360 */
Aditya Kalia86d55d2011-11-14 10:55:54 -0500361errcode_t quota_file_close(struct quota_handle *h)
Aditya Kalif239fef2011-07-20 11:40:02 -0700362{
363 if (h->qh_io_flags & IOFL_INFODIRTY) {
364 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
365 return -1;
366 h->qh_io_flags &= ~IOFL_INFODIRTY;
367 }
368
369 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
370 return -1;
371 if (h->qh_qf.e2_file) {
372 ext2fs_file_flush(h->qh_qf.e2_file);
373 ext2fs_file_set_size2(h->qh_qf.e2_file,
374 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino));
375 ext2fs_file_close(h->qh_qf.e2_file);
376 }
377
378 return 0;
379}
380
381/*
382 * Create empty quota structure
383 */
384struct dquot *get_empty_dquot(void)
385{
Aditya Kalia86d55d2011-11-14 10:55:54 -0500386 struct dquot *dquot;
Aditya Kalif239fef2011-07-20 11:40:02 -0700387
Aditya Kalia86d55d2011-11-14 10:55:54 -0500388 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
Andreas Dilger0ce01722012-11-29 05:47:54 -0700389 log_err("Failed to allocate dquot");
Aditya Kalia86d55d2011-11-14 10:55:54 -0500390 return NULL;
391 }
392
Aditya Kalif239fef2011-07-20 11:40:02 -0700393 dquot->dq_id = -1;
394 return dquot;
395}