blob: faaa4d072c9ab940c02c7b8e9fd136c411bfdef2 [file] [log] [blame]
Jan Kara9e33d692008-08-25 19:56:50 +02001/*
2 * Implementation of operations over global quota file
3 */
Mark Fasheh171bf932008-10-20 15:36:47 +02004#include <linux/spinlock.h>
Jan Kara9e33d692008-08-25 19:56:50 +02005#include <linux/fs.h>
6#include <linux/quota.h>
7#include <linux/quotaops.h>
8#include <linux/dqblk_qtree.h>
Mark Fasheh171bf932008-10-20 15:36:47 +02009#include <linux/jiffies.h>
10#include <linux/writeback.h>
11#include <linux/workqueue.h>
Jan Kara9e33d692008-08-25 19:56:50 +020012
13#define MLOG_MASK_PREFIX ML_QUOTA
14#include <cluster/masklog.h>
15
16#include "ocfs2_fs.h"
17#include "ocfs2.h"
18#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070019#include "blockcheck.h"
Jan Kara9e33d692008-08-25 19:56:50 +020020#include "inode.h"
21#include "journal.h"
22#include "file.h"
23#include "sysfile.h"
24#include "dlmglue.h"
25#include "uptodate.h"
Jan Karaada50822009-08-03 18:24:21 +020026#include "super.h"
Jan Kara9e33d692008-08-25 19:56:50 +020027#include "quota.h"
28
Mark Fasheh171bf932008-10-20 15:36:47 +020029static struct workqueue_struct *ocfs2_quota_wq = NULL;
30
31static void qsync_work_fn(struct work_struct *work);
32
Jan Kara9e33d692008-08-25 19:56:50 +020033static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
34{
35 struct ocfs2_global_disk_dqblk *d = dp;
36 struct mem_dqblk *m = &dquot->dq_dqb;
37
38 /* Update from disk only entries not set by the admin */
39 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
40 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
41 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
42 }
43 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
44 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
45 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
46 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
47 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
48 }
49 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
50 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
51 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
52 m->dqb_btime = le64_to_cpu(d->dqb_btime);
53 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
54 m->dqb_itime = le64_to_cpu(d->dqb_itime);
55 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
56}
57
58static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
59{
60 struct ocfs2_global_disk_dqblk *d = dp;
61 struct mem_dqblk *m = &dquot->dq_dqb;
62
63 d->dqb_id = cpu_to_le32(dquot->dq_id);
64 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
65 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
66 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
67 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
68 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
69 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
70 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
71 d->dqb_btime = cpu_to_le64(m->dqb_btime);
72 d->dqb_itime = cpu_to_le64(m->dqb_itime);
Jan Kara7669f542009-07-22 13:17:18 +020073 d->dqb_pad1 = d->dqb_pad2 = 0;
Jan Kara9e33d692008-08-25 19:56:50 +020074}
75
76static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
77{
78 struct ocfs2_global_disk_dqblk *d = dp;
79 struct ocfs2_mem_dqinfo *oinfo =
80 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
81
82 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
83 return 0;
84 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
85}
86
87struct qtree_fmt_operations ocfs2_global_ops = {
88 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
89 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
90 .is_id = ocfs2_global_is_id,
91};
92
Joel Becker684ef272008-12-02 17:44:05 -080093static int ocfs2_validate_quota_block(struct super_block *sb,
94 struct buffer_head *bh)
95{
Joel Beckerd6b32bb2008-10-17 14:55:01 -070096 struct ocfs2_disk_dqtrailer *dqt =
97 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
Joel Becker684ef272008-12-02 17:44:05 -080098
99 mlog(0, "Validating quota block %llu\n",
100 (unsigned long long)bh->b_blocknr);
101
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700102 BUG_ON(!buffer_uptodate(bh));
103
104 /*
105 * If the ecc fails, we return the error but otherwise
106 * leave the filesystem running. We know any error is
107 * local to this block.
108 */
109 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
Joel Becker684ef272008-12-02 17:44:05 -0800110}
111
Joel Becker85eb8b72008-11-25 15:31:27 +0100112int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
113 struct buffer_head **bh)
Jan Kara9e33d692008-08-25 19:56:50 +0200114{
Joel Becker85eb8b72008-11-25 15:31:27 +0100115 int rc = 0;
116 struct buffer_head *tmp = *bh;
Jan Kara9e33d692008-08-25 19:56:50 +0200117
Jan Karaada50822009-08-03 18:24:21 +0200118 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
119 ocfs2_error(inode->i_sb,
120 "Quota file %llu is probably corrupted! Requested "
121 "to read block %Lu but file has size only %Lu\n",
122 (unsigned long long)OCFS2_I(inode)->ip_blkno,
123 (unsigned long long)v_block,
124 (unsigned long long)i_size_read(inode));
125 return -EIO;
126 }
Joel Becker684ef272008-12-02 17:44:05 -0800127 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
128 ocfs2_validate_quota_block);
Joel Becker85eb8b72008-11-25 15:31:27 +0100129 if (rc)
130 mlog_errno(rc);
Jan Kara9e33d692008-08-25 19:56:50 +0200131
Joel Becker85eb8b72008-11-25 15:31:27 +0100132 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
133 if (!rc && !*bh)
134 *bh = tmp;
135
136 return rc;
Jan Kara9e33d692008-08-25 19:56:50 +0200137}
138
Jan Kara53a36042008-11-25 15:31:29 +0100139static int ocfs2_get_quota_block(struct inode *inode, int block,
140 struct buffer_head **bh)
Jan Kara9e33d692008-08-25 19:56:50 +0200141{
142 u64 pblock, pcount;
Jan Kara53a36042008-11-25 15:31:29 +0100143 int err;
Jan Kara9e33d692008-08-25 19:56:50 +0200144
145 down_read(&OCFS2_I(inode)->ip_alloc_sem);
Jan Kara53a36042008-11-25 15:31:29 +0100146 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
Jan Kara9e33d692008-08-25 19:56:50 +0200147 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Jan Kara53a36042008-11-25 15:31:29 +0100148 if (err) {
149 mlog_errno(err);
150 return err;
Jan Kara9e33d692008-08-25 19:56:50 +0200151 }
Jan Kara53a36042008-11-25 15:31:29 +0100152 *bh = sb_getblk(inode->i_sb, pblock);
153 if (!*bh) {
154 err = -EIO;
155 mlog_errno(err);
Jan Kara9e33d692008-08-25 19:56:50 +0200156 }
Joe Perchesa419aef2009-08-18 11:18:35 -0700157 return err;
Jan Kara9e33d692008-08-25 19:56:50 +0200158}
159
160/* Read data from global quotafile - avoid pagecache and such because we cannot
161 * afford acquiring the locks... We use quota cluster lock to serialize
162 * operations. Caller is responsible for acquiring it. */
163ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
164 size_t len, loff_t off)
165{
166 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
167 struct inode *gqinode = oinfo->dqi_gqinode;
168 loff_t i_size = i_size_read(gqinode);
169 int offset = off & (sb->s_blocksize - 1);
170 sector_t blk = off >> sb->s_blocksize_bits;
171 int err = 0;
172 struct buffer_head *bh;
173 size_t toread, tocopy;
174
175 if (off > i_size)
176 return 0;
177 if (off + len > i_size)
178 len = i_size - off;
179 toread = len;
180 while (toread > 0) {
Mark Fashehdad7d972008-12-24 16:33:08 -0800181 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
Joel Becker85eb8b72008-11-25 15:31:27 +0100182 bh = NULL;
183 err = ocfs2_read_quota_block(gqinode, blk, &bh);
184 if (err) {
Jan Kara9e33d692008-08-25 19:56:50 +0200185 mlog_errno(err);
186 return err;
187 }
188 memcpy(data, bh->b_data + offset, tocopy);
189 brelse(bh);
190 offset = 0;
191 toread -= tocopy;
192 data += tocopy;
193 blk++;
194 }
195 return len;
196}
197
198/* Write to quotafile (we know the transaction is already started and has
199 * enough credits) */
200ssize_t ocfs2_quota_write(struct super_block *sb, int type,
201 const char *data, size_t len, loff_t off)
202{
203 struct mem_dqinfo *info = sb_dqinfo(sb, type);
204 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
205 struct inode *gqinode = oinfo->dqi_gqinode;
206 int offset = off & (sb->s_blocksize - 1);
207 sector_t blk = off >> sb->s_blocksize_bits;
Jan Karaaf09e512008-11-25 15:31:28 +0100208 int err = 0, new = 0, ja_type;
Joel Becker85eb8b72008-11-25 15:31:27 +0100209 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200210 handle_t *handle = journal_current_handle();
211
212 if (!handle) {
213 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
214 "because transaction was not started.\n",
215 (unsigned long long)off, (unsigned long long)len);
216 return -EIO;
217 }
218 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
219 WARN_ON(1);
220 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
221 }
222
223 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
224 if (gqinode->i_size < off + len) {
Jan Karab57ac2c2009-07-22 13:17:15 +0200225 loff_t rounded_end =
226 ocfs2_align_bytes_to_blocks(sb, off + len);
227
Jan Karab409d7a2009-08-06 23:29:34 +0200228 /* Space is already allocated in ocfs2_global_read_dquot() */
Jan Kara9e33d692008-08-25 19:56:50 +0200229 err = ocfs2_simple_size_update(gqinode,
230 oinfo->dqi_gqi_bh,
Jan Karab57ac2c2009-07-22 13:17:15 +0200231 rounded_end);
Jan Kara9e33d692008-08-25 19:56:50 +0200232 if (err < 0)
233 goto out;
234 new = 1;
235 }
236 /* Not rewriting whole block? */
237 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
238 !new) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100239 err = ocfs2_read_quota_block(gqinode, blk, &bh);
Jan Karaaf09e512008-11-25 15:31:28 +0100240 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
Jan Kara9e33d692008-08-25 19:56:50 +0200241 } else {
Jan Kara53a36042008-11-25 15:31:29 +0100242 err = ocfs2_get_quota_block(gqinode, blk, &bh);
Jan Karaaf09e512008-11-25 15:31:28 +0100243 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
Jan Kara9e33d692008-08-25 19:56:50 +0200244 }
Jan Karaaf09e512008-11-25 15:31:28 +0100245 if (err) {
246 mlog_errno(err);
Tao Mae9956fa2009-07-30 16:07:10 +0800247 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +0200248 }
249 lock_buffer(bh);
250 if (new)
251 memset(bh->b_data, 0, sb->s_blocksize);
252 memcpy(bh->b_data + offset, data, len);
253 flush_dcache_page(bh->b_page);
Jan Karaaf09e512008-11-25 15:31:28 +0100254 set_buffer_uptodate(bh);
Jan Kara9e33d692008-08-25 19:56:50 +0200255 unlock_buffer(bh);
Joel Becker8cb471e2009-02-10 20:00:41 -0800256 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
Joel Becker0cf2f762009-02-12 16:41:25 -0800257 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
258 ja_type);
Jan Karaaf09e512008-11-25 15:31:28 +0100259 if (err < 0) {
260 brelse(bh);
261 goto out;
262 }
Joel Beckerec20cec2010-03-19 14:13:52 -0700263 ocfs2_journal_dirty(handle, bh);
Jan Kara9e33d692008-08-25 19:56:50 +0200264 brelse(bh);
Jan Kara9e33d692008-08-25 19:56:50 +0200265out:
266 if (err) {
267 mutex_unlock(&gqinode->i_mutex);
268 mlog_errno(err);
269 return err;
270 }
271 gqinode->i_version++;
272 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
273 mutex_unlock(&gqinode->i_mutex);
274 return len;
275}
276
277int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
278{
279 int status;
280 struct buffer_head *bh = NULL;
281
282 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
283 if (status < 0)
284 return status;
285 spin_lock(&dq_data_lock);
286 if (!oinfo->dqi_gqi_count++)
287 oinfo->dqi_gqi_bh = bh;
288 else
289 WARN_ON(bh != oinfo->dqi_gqi_bh);
290 spin_unlock(&dq_data_lock);
291 return 0;
292}
293
294void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
295{
296 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
297 brelse(oinfo->dqi_gqi_bh);
298 spin_lock(&dq_data_lock);
299 if (!--oinfo->dqi_gqi_count)
300 oinfo->dqi_gqi_bh = NULL;
301 spin_unlock(&dq_data_lock);
302}
303
304/* Read information header from global quota file */
305int ocfs2_global_read_info(struct super_block *sb, int type)
306{
307 struct inode *gqinode = NULL;
308 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
309 GROUP_QUOTA_SYSTEM_INODE };
310 struct ocfs2_global_disk_dqinfo dinfo;
311 struct mem_dqinfo *info = sb_dqinfo(sb, type);
312 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
313 int status;
314
315 mlog_entry_void();
316
317 /* Read global header */
318 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
319 OCFS2_INVALID_SLOT);
320 if (!gqinode) {
321 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
322 type);
323 status = -EINVAL;
324 goto out_err;
325 }
326 oinfo->dqi_gi.dqi_sb = sb;
327 oinfo->dqi_gi.dqi_type = type;
328 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
329 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
330 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
331 oinfo->dqi_gqi_bh = NULL;
332 oinfo->dqi_gqi_count = 0;
333 oinfo->dqi_gqinode = gqinode;
334 status = ocfs2_lock_global_qf(oinfo, 0);
335 if (status < 0) {
336 mlog_errno(status);
337 goto out_err;
338 }
339 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
340 sizeof(struct ocfs2_global_disk_dqinfo),
341 OCFS2_GLOBAL_INFO_OFF);
342 ocfs2_unlock_global_qf(oinfo, 0);
343 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
344 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
345 status);
346 if (status >= 0)
347 status = -EIO;
348 mlog_errno(status);
349 goto out_err;
350 }
351 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
352 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
353 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
354 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
355 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
356 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
357 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
358 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
359 OCFS2_QBLK_RESERVED_SPACE;
360 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
Mark Fasheh171bf932008-10-20 15:36:47 +0200361 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
362 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
Jan Kara4539f1d2009-07-22 13:17:20 +0200363 msecs_to_jiffies(oinfo->dqi_syncms));
Mark Fasheh171bf932008-10-20 15:36:47 +0200364
Jan Kara9e33d692008-08-25 19:56:50 +0200365out_err:
366 mlog_exit(status);
367 return status;
368}
369
370/* Write information to global quota file. Expects exlusive lock on quota
371 * file inode and quota info */
372static int __ocfs2_global_write_info(struct super_block *sb, int type)
373{
374 struct mem_dqinfo *info = sb_dqinfo(sb, type);
375 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
376 struct ocfs2_global_disk_dqinfo dinfo;
377 ssize_t size;
378
379 spin_lock(&dq_data_lock);
380 info->dqi_flags &= ~DQF_INFO_DIRTY;
381 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
382 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
383 spin_unlock(&dq_data_lock);
384 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
385 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
386 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
387 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
388 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
389 sizeof(struct ocfs2_global_disk_dqinfo),
390 OCFS2_GLOBAL_INFO_OFF);
391 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
392 mlog(ML_ERROR, "Cannot write global quota info structure\n");
393 if (size >= 0)
394 size = -EIO;
395 return size;
396 }
397 return 0;
398}
399
400int ocfs2_global_write_info(struct super_block *sb, int type)
401{
402 int err;
403 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
404
405 err = ocfs2_qinfo_lock(info, 1);
406 if (err < 0)
407 return err;
408 err = __ocfs2_global_write_info(sb, type);
409 ocfs2_qinfo_unlock(info, 1);
410 return err;
411}
412
Jan Karab409d7a2009-08-06 23:29:34 +0200413static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
414{
415 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
416
417 /*
418 * We may need to allocate tree blocks and a leaf block but not the
419 * root block
420 */
421 return oinfo->dqi_gi.dqi_qtree_depth;
422}
423
424static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
425{
426 /* We modify all the allocated blocks, tree root, and info block */
427 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
428 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
429}
430
Jan Kara9e33d692008-08-25 19:56:50 +0200431/* Read in information from global quota file and acquire a reference to it.
432 * dquot_acquire() has already started the transaction and locked quota file */
433int ocfs2_global_read_dquot(struct dquot *dquot)
434{
435 int err, err2, ex = 0;
Jan Karab409d7a2009-08-06 23:29:34 +0200436 struct super_block *sb = dquot->dq_sb;
437 int type = dquot->dq_type;
438 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
439 struct ocfs2_super *osb = OCFS2_SB(sb);
440 struct inode *gqinode = info->dqi_gqinode;
441 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
442 handle_t *handle = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200443
444 err = ocfs2_qinfo_lock(info, 0);
445 if (err < 0)
446 goto out;
447 err = qtree_read_dquot(&info->dqi_gi, dquot);
448 if (err < 0)
449 goto out_qlock;
450 OCFS2_DQUOT(dquot)->dq_use_count++;
451 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
452 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
Jan Karab409d7a2009-08-06 23:29:34 +0200453 ocfs2_qinfo_unlock(info, 0);
454
Jan Kara9e33d692008-08-25 19:56:50 +0200455 if (!dquot->dq_off) { /* No real quota entry? */
Jan Kara9e33d692008-08-25 19:56:50 +0200456 ex = 1;
Jan Karab409d7a2009-08-06 23:29:34 +0200457 /*
458 * Add blocks to quota file before we start a transaction since
459 * locking allocators ranks above a transaction start
460 */
461 WARN_ON(journal_current_handle());
462 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
463 err = ocfs2_extend_no_holes(gqinode,
464 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
465 gqinode->i_size);
466 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
467 if (err < 0)
468 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +0200469 }
Jan Karab409d7a2009-08-06 23:29:34 +0200470
471 handle = ocfs2_start_trans(osb,
472 ocfs2_calc_global_qinit_credits(sb, type));
473 if (IS_ERR(handle)) {
474 err = PTR_ERR(handle);
475 goto out;
476 }
477 err = ocfs2_qinfo_lock(info, ex);
478 if (err < 0)
479 goto out_trans;
Jan Kara9e33d692008-08-25 19:56:50 +0200480 err = qtree_write_dquot(&info->dqi_gi, dquot);
481 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
482 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
483 if (!err)
484 err = err2;
485 }
486out_qlock:
487 if (ex)
488 ocfs2_qinfo_unlock(info, 1);
Jan Kara4e8a3012009-06-02 14:23:59 +0200489 else
490 ocfs2_qinfo_unlock(info, 0);
Jan Karab409d7a2009-08-06 23:29:34 +0200491out_trans:
492 if (handle)
493 ocfs2_commit_trans(osb, handle);
Jan Kara9e33d692008-08-25 19:56:50 +0200494out:
495 if (err < 0)
496 mlog_errno(err);
497 return err;
498}
499
500/* Sync local information about quota modifications with global quota file.
501 * Caller must have started the transaction and obtained exclusive lock for
502 * global quota file inode */
503int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
504{
505 int err, err2;
506 struct super_block *sb = dquot->dq_sb;
507 int type = dquot->dq_type;
508 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
509 struct ocfs2_global_disk_dqblk dqblk;
510 s64 spacechange, inodechange;
511 time_t olditime, oldbtime;
512
513 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
514 sizeof(struct ocfs2_global_disk_dqblk),
515 dquot->dq_off);
516 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
517 if (err >= 0) {
518 mlog(ML_ERROR, "Short read from global quota file "
519 "(%u read)\n", err);
520 err = -EIO;
521 }
522 goto out;
523 }
524
525 /* Update space and inode usage. Get also other information from
526 * global quota file so that we don't overwrite any changes there.
527 * We are */
528 spin_lock(&dq_data_lock);
529 spacechange = dquot->dq_dqb.dqb_curspace -
530 OCFS2_DQUOT(dquot)->dq_origspace;
531 inodechange = dquot->dq_dqb.dqb_curinodes -
532 OCFS2_DQUOT(dquot)->dq_originodes;
533 olditime = dquot->dq_dqb.dqb_itime;
534 oldbtime = dquot->dq_dqb.dqb_btime;
535 ocfs2_global_disk2memdqb(dquot, &dqblk);
Jan Kara9a2f3862008-11-25 15:31:30 +0100536 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
537 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
538 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
Jan Kara9e33d692008-08-25 19:56:50 +0200539 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
540 dquot->dq_dqb.dqb_curspace += spacechange;
541 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
542 dquot->dq_dqb.dqb_curinodes += inodechange;
543 /* Set properly space grace time... */
544 if (dquot->dq_dqb.dqb_bsoftlimit &&
545 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
546 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
547 oldbtime > 0) {
548 if (dquot->dq_dqb.dqb_btime > 0)
549 dquot->dq_dqb.dqb_btime =
550 min(dquot->dq_dqb.dqb_btime, oldbtime);
551 else
552 dquot->dq_dqb.dqb_btime = oldbtime;
553 }
554 } else {
555 dquot->dq_dqb.dqb_btime = 0;
556 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
557 }
558 /* Set properly inode grace time... */
559 if (dquot->dq_dqb.dqb_isoftlimit &&
560 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
561 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
562 olditime > 0) {
563 if (dquot->dq_dqb.dqb_itime > 0)
564 dquot->dq_dqb.dqb_itime =
565 min(dquot->dq_dqb.dqb_itime, olditime);
566 else
567 dquot->dq_dqb.dqb_itime = olditime;
568 }
569 } else {
570 dquot->dq_dqb.dqb_itime = 0;
571 clear_bit(DQ_INODES_B, &dquot->dq_flags);
572 }
573 /* All information is properly updated, clear the flags */
574 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
575 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
576 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
577 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
578 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
579 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
580 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
581 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
582 spin_unlock(&dq_data_lock);
583 err = ocfs2_qinfo_lock(info, freeing);
584 if (err < 0) {
585 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
586 " (type=%d, id=%u)\n", dquot->dq_type,
587 (unsigned)dquot->dq_id);
588 goto out;
589 }
590 if (freeing)
591 OCFS2_DQUOT(dquot)->dq_use_count--;
592 err = qtree_write_dquot(&info->dqi_gi, dquot);
593 if (err < 0)
594 goto out_qlock;
595 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
596 err = qtree_release_dquot(&info->dqi_gi, dquot);
597 if (info_dirty(sb_dqinfo(sb, type))) {
598 err2 = __ocfs2_global_write_info(sb, type);
599 if (!err)
600 err = err2;
601 }
602 }
603out_qlock:
604 ocfs2_qinfo_unlock(info, freeing);
605out:
606 if (err < 0)
607 mlog_errno(err);
608 return err;
609}
610
611/*
Mark Fasheh171bf932008-10-20 15:36:47 +0200612 * Functions for periodic syncing of dquots with global file
613 */
614static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
615{
616 handle_t *handle;
617 struct super_block *sb = dquot->dq_sb;
618 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
619 struct ocfs2_super *osb = OCFS2_SB(sb);
620 int status = 0;
621
622 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
623 dquot->dq_type, type, sb->s_id);
624 if (type != dquot->dq_type)
625 goto out;
626 status = ocfs2_lock_global_qf(oinfo, 1);
627 if (status < 0)
628 goto out;
629
630 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
631 if (IS_ERR(handle)) {
632 status = PTR_ERR(handle);
633 mlog_errno(status);
634 goto out_ilock;
635 }
636 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
637 status = ocfs2_sync_dquot(dquot);
638 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
639 if (status < 0)
640 mlog_errno(status);
641 /* We have to write local structure as well... */
642 dquot_mark_dquot_dirty(dquot);
643 status = dquot_commit(dquot);
644 if (status < 0)
645 mlog_errno(status);
646 ocfs2_commit_trans(osb, handle);
647out_ilock:
648 ocfs2_unlock_global_qf(oinfo, 1);
649out:
650 mlog_exit(status);
651 return status;
652}
653
654static void qsync_work_fn(struct work_struct *work)
655{
656 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
657 struct ocfs2_mem_dqinfo,
658 dqi_sync_work.work);
659 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
660
661 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
662 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
Jan Kara4539f1d2009-07-22 13:17:20 +0200663 msecs_to_jiffies(oinfo->dqi_syncms));
Mark Fasheh171bf932008-10-20 15:36:47 +0200664}
665
666/*
Jan Kara9e33d692008-08-25 19:56:50 +0200667 * Wrappers for generic quota functions
668 */
669
670static int ocfs2_write_dquot(struct dquot *dquot)
671{
672 handle_t *handle;
673 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
674 int status = 0;
675
676 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
677
678 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
679 if (IS_ERR(handle)) {
680 status = PTR_ERR(handle);
681 mlog_errno(status);
682 goto out;
683 }
684 status = dquot_commit(dquot);
685 ocfs2_commit_trans(osb, handle);
686out:
687 mlog_exit(status);
688 return status;
689}
690
Jan Karab409d7a2009-08-06 23:29:34 +0200691static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
Jan Kara9e33d692008-08-25 19:56:50 +0200692{
Jan Karab409d7a2009-08-06 23:29:34 +0200693 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
Jan Kara05849742009-07-22 13:17:21 +0200694 /*
695 * We modify tree, leaf block, global info, local chunk header,
696 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
697 * accounts for inode update
698 */
Jan Karab409d7a2009-08-06 23:29:34 +0200699 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
700 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
Jan Kara05849742009-07-22 13:17:21 +0200701 OCFS2_QINFO_WRITE_CREDITS +
Jan Kara05849742009-07-22 13:17:21 +0200702 OCFS2_INODE_UPDATE_CREDITS;
Jan Kara9e33d692008-08-25 19:56:50 +0200703}
704
705static int ocfs2_release_dquot(struct dquot *dquot)
706{
707 handle_t *handle;
708 struct ocfs2_mem_dqinfo *oinfo =
709 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
710 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
711 int status = 0;
712
713 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
714
715 status = ocfs2_lock_global_qf(oinfo, 1);
716 if (status < 0)
717 goto out;
718 handle = ocfs2_start_trans(osb,
719 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
720 if (IS_ERR(handle)) {
721 status = PTR_ERR(handle);
722 mlog_errno(status);
723 goto out_ilock;
724 }
725 status = dquot_release(dquot);
726 ocfs2_commit_trans(osb, handle);
727out_ilock:
728 ocfs2_unlock_global_qf(oinfo, 1);
729out:
730 mlog_exit(status);
731 return status;
732}
733
Jan Kara9e33d692008-08-25 19:56:50 +0200734static int ocfs2_acquire_dquot(struct dquot *dquot)
735{
Jan Kara9e33d692008-08-25 19:56:50 +0200736 struct ocfs2_mem_dqinfo *oinfo =
737 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
Jan Kara9e33d692008-08-25 19:56:50 +0200738 int status = 0;
739
740 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
741 /* We need an exclusive lock, because we're going to update use count
742 * and instantiate possibly new dquot structure */
743 status = ocfs2_lock_global_qf(oinfo, 1);
744 if (status < 0)
745 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +0200746 status = dquot_acquire(dquot);
Jan Kara9e33d692008-08-25 19:56:50 +0200747 ocfs2_unlock_global_qf(oinfo, 1);
748out:
749 mlog_exit(status);
750 return status;
751}
752
753static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
754{
755 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
756 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
757 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
758 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
759 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
760 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
761 int sync = 0;
762 int status;
763 struct super_block *sb = dquot->dq_sb;
764 int type = dquot->dq_type;
765 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
766 handle_t *handle;
767 struct ocfs2_super *osb = OCFS2_SB(sb);
768
769 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
770 dquot_mark_dquot_dirty(dquot);
771
772 /* In case user set some limits, sync dquot immediately to global
773 * quota file so that information propagates quicker */
774 spin_lock(&dq_data_lock);
775 if (dquot->dq_flags & mask)
776 sync = 1;
777 spin_unlock(&dq_data_lock);
Jan Karaf8afead2009-01-12 23:20:32 +0100778 /* This is a slight hack but we can't afford getting global quota
779 * lock if we already have a transaction started. */
780 if (!sync || journal_current_handle()) {
Jan Kara9e33d692008-08-25 19:56:50 +0200781 status = ocfs2_write_dquot(dquot);
782 goto out;
783 }
784 status = ocfs2_lock_global_qf(oinfo, 1);
785 if (status < 0)
786 goto out;
787 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
788 if (IS_ERR(handle)) {
789 status = PTR_ERR(handle);
790 mlog_errno(status);
791 goto out_ilock;
792 }
793 status = ocfs2_sync_dquot(dquot);
794 if (status < 0) {
795 mlog_errno(status);
796 goto out_trans;
797 }
798 /* Now write updated local dquot structure */
799 status = dquot_commit(dquot);
800out_trans:
801 ocfs2_commit_trans(osb, handle);
802out_ilock:
803 ocfs2_unlock_global_qf(oinfo, 1);
804out:
805 mlog_exit(status);
806 return status;
807}
808
809/* This should happen only after set_dqinfo(). */
810static int ocfs2_write_info(struct super_block *sb, int type)
811{
812 handle_t *handle;
813 int status = 0;
814 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
815
816 mlog_entry_void();
817
818 status = ocfs2_lock_global_qf(oinfo, 1);
819 if (status < 0)
820 goto out;
821 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
822 if (IS_ERR(handle)) {
823 status = PTR_ERR(handle);
824 mlog_errno(status);
825 goto out_ilock;
826 }
827 status = dquot_commit_info(sb, type);
828 ocfs2_commit_trans(OCFS2_SB(sb), handle);
829out_ilock:
830 ocfs2_unlock_global_qf(oinfo, 1);
831out:
832 mlog_exit(status);
833 return status;
834}
835
Jan Kara9e33d692008-08-25 19:56:50 +0200836static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
837{
838 struct ocfs2_dquot *dquot =
839 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
840
841 if (!dquot)
842 return NULL;
843 return &dquot->dq_dquot;
844}
845
846static void ocfs2_destroy_dquot(struct dquot *dquot)
847{
848 kmem_cache_free(ocfs2_dquot_cachep, dquot);
849}
850
Alexey Dobriyan61e225d2009-09-21 17:01:08 -0700851const struct dquot_operations ocfs2_quota_operations = {
Jan Kara9e33d692008-08-25 19:56:50 +0200852 .write_dquot = ocfs2_write_dquot,
853 .acquire_dquot = ocfs2_acquire_dquot,
854 .release_dquot = ocfs2_release_dquot,
855 .mark_dirty = ocfs2_mark_dquot_dirty,
856 .write_info = ocfs2_write_info,
857 .alloc_dquot = ocfs2_alloc_dquot,
858 .destroy_dquot = ocfs2_destroy_dquot,
859};
Mark Fasheh171bf932008-10-20 15:36:47 +0200860
861int ocfs2_quota_setup(void)
862{
863 ocfs2_quota_wq = create_workqueue("o2quot");
864 if (!ocfs2_quota_wq)
865 return -ENOMEM;
866 return 0;
867}
868
869void ocfs2_quota_shutdown(void)
870{
871 if (ocfs2_quota_wq) {
872 flush_workqueue(ocfs2_quota_wq);
873 destroy_workqueue(ocfs2_quota_wq);
874 ocfs2_quota_wq = NULL;
875 }
876}