ocfs2: Avoid unnecessary block mapping when refreshing quota info
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / quota_global.c
blobf391b11ea98c4ca968e2f857a32c23d0aab74d11
1 /*
2 * Implementation of operations over global quota file
3 */
4 #include <linux/spinlock.h>
5 #include <linux/fs.h>
6 #include <linux/slab.h>
7 #include <linux/quota.h>
8 #include <linux/quotaops.h>
9 #include <linux/dqblk_qtree.h>
10 #include <linux/jiffies.h>
11 #include <linux/writeback.h>
12 #include <linux/workqueue.h>
14 #define MLOG_MASK_PREFIX ML_QUOTA
15 #include <cluster/masklog.h>
17 #include "ocfs2_fs.h"
18 #include "ocfs2.h"
19 #include "alloc.h"
20 #include "blockcheck.h"
21 #include "inode.h"
22 #include "journal.h"
23 #include "file.h"
24 #include "sysfile.h"
25 #include "dlmglue.h"
26 #include "uptodate.h"
27 #include "super.h"
28 #include "buffer_head_io.h"
29 #include "quota.h"
31 static struct workqueue_struct *ocfs2_quota_wq = NULL;
33 static void qsync_work_fn(struct work_struct *work);
35 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
37 struct ocfs2_global_disk_dqblk *d = dp;
38 struct mem_dqblk *m = &dquot->dq_dqb;
40 /* Update from disk only entries not set by the admin */
41 if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
42 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
43 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
45 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
46 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
47 if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
48 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
49 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
51 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
52 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
53 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
54 m->dqb_btime = le64_to_cpu(d->dqb_btime);
55 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
56 m->dqb_itime = le64_to_cpu(d->dqb_itime);
57 OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
60 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
62 struct ocfs2_global_disk_dqblk *d = dp;
63 struct mem_dqblk *m = &dquot->dq_dqb;
65 d->dqb_id = cpu_to_le32(dquot->dq_id);
66 d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
67 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
68 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
69 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
70 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
71 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
72 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
73 d->dqb_btime = cpu_to_le64(m->dqb_btime);
74 d->dqb_itime = cpu_to_le64(m->dqb_itime);
75 d->dqb_pad1 = d->dqb_pad2 = 0;
78 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
80 struct ocfs2_global_disk_dqblk *d = dp;
81 struct ocfs2_mem_dqinfo *oinfo =
82 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
84 if (qtree_entry_unused(&oinfo->dqi_gi, dp))
85 return 0;
86 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
89 struct qtree_fmt_operations ocfs2_global_ops = {
90 .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
91 .disk2mem_dqblk = ocfs2_global_disk2memdqb,
92 .is_id = ocfs2_global_is_id,
95 static int ocfs2_validate_quota_block(struct super_block *sb,
96 struct buffer_head *bh)
98 struct ocfs2_disk_dqtrailer *dqt =
99 ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
101 mlog(0, "Validating quota block %llu\n",
102 (unsigned long long)bh->b_blocknr);
104 BUG_ON(!buffer_uptodate(bh));
107 * If the ecc fails, we return the error but otherwise
108 * leave the filesystem running. We know any error is
109 * local to this block.
111 return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
114 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
115 struct buffer_head **bh)
117 int rc = 0;
118 struct buffer_head *tmp = *bh;
120 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
121 ocfs2_error(inode->i_sb,
122 "Quota file %llu is probably corrupted! Requested "
123 "to read block %Lu but file has size only %Lu\n",
124 (unsigned long long)OCFS2_I(inode)->ip_blkno,
125 (unsigned long long)v_block,
126 (unsigned long long)i_size_read(inode));
127 return -EIO;
129 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
130 ocfs2_validate_quota_block);
131 if (rc)
132 mlog_errno(rc);
134 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
135 if (!rc && !*bh)
136 *bh = tmp;
138 return rc;
141 int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
142 struct buffer_head **bhp)
144 int rc;
146 *bhp = NULL;
147 rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
148 ocfs2_validate_quota_block);
149 if (rc)
150 mlog_errno(rc);
151 return rc;
154 static int ocfs2_get_quota_block(struct inode *inode, int block,
155 struct buffer_head **bh)
157 u64 pblock, pcount;
158 int err;
160 down_read(&OCFS2_I(inode)->ip_alloc_sem);
161 err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
162 up_read(&OCFS2_I(inode)->ip_alloc_sem);
163 if (err) {
164 mlog_errno(err);
165 return err;
167 *bh = sb_getblk(inode->i_sb, pblock);
168 if (!*bh) {
169 err = -EIO;
170 mlog_errno(err);
172 return err;
175 /* Read data from global quotafile - avoid pagecache and such because we cannot
176 * afford acquiring the locks... We use quota cluster lock to serialize
177 * operations. Caller is responsible for acquiring it. */
178 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
179 size_t len, loff_t off)
181 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
182 struct inode *gqinode = oinfo->dqi_gqinode;
183 loff_t i_size = i_size_read(gqinode);
184 int offset = off & (sb->s_blocksize - 1);
185 sector_t blk = off >> sb->s_blocksize_bits;
186 int err = 0;
187 struct buffer_head *bh;
188 size_t toread, tocopy;
190 if (off > i_size)
191 return 0;
192 if (off + len > i_size)
193 len = i_size - off;
194 toread = len;
195 while (toread > 0) {
196 tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
197 bh = NULL;
198 err = ocfs2_read_quota_block(gqinode, blk, &bh);
199 if (err) {
200 mlog_errno(err);
201 return err;
203 memcpy(data, bh->b_data + offset, tocopy);
204 brelse(bh);
205 offset = 0;
206 toread -= tocopy;
207 data += tocopy;
208 blk++;
210 return len;
213 /* Write to quotafile (we know the transaction is already started and has
214 * enough credits) */
215 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
216 const char *data, size_t len, loff_t off)
218 struct mem_dqinfo *info = sb_dqinfo(sb, type);
219 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
220 struct inode *gqinode = oinfo->dqi_gqinode;
221 int offset = off & (sb->s_blocksize - 1);
222 sector_t blk = off >> sb->s_blocksize_bits;
223 int err = 0, new = 0, ja_type;
224 struct buffer_head *bh = NULL;
225 handle_t *handle = journal_current_handle();
227 if (!handle) {
228 mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
229 "because transaction was not started.\n",
230 (unsigned long long)off, (unsigned long long)len);
231 return -EIO;
233 if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
234 WARN_ON(1);
235 len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
238 mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
239 if (gqinode->i_size < off + len) {
240 loff_t rounded_end =
241 ocfs2_align_bytes_to_blocks(sb, off + len);
243 /* Space is already allocated in ocfs2_global_read_dquot() */
244 err = ocfs2_simple_size_update(gqinode,
245 oinfo->dqi_gqi_bh,
246 rounded_end);
247 if (err < 0)
248 goto out;
249 new = 1;
251 /* Not rewriting whole block? */
252 if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
253 !new) {
254 err = ocfs2_read_quota_block(gqinode, blk, &bh);
255 ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
256 } else {
257 err = ocfs2_get_quota_block(gqinode, blk, &bh);
258 ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
260 if (err) {
261 mlog_errno(err);
262 goto out;
264 lock_buffer(bh);
265 if (new)
266 memset(bh->b_data, 0, sb->s_blocksize);
267 memcpy(bh->b_data + offset, data, len);
268 flush_dcache_page(bh->b_page);
269 set_buffer_uptodate(bh);
270 unlock_buffer(bh);
271 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
272 err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
273 ja_type);
274 if (err < 0) {
275 brelse(bh);
276 goto out;
278 ocfs2_journal_dirty(handle, bh);
279 brelse(bh);
280 out:
281 if (err) {
282 mutex_unlock(&gqinode->i_mutex);
283 mlog_errno(err);
284 return err;
286 gqinode->i_version++;
287 ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
288 mutex_unlock(&gqinode->i_mutex);
289 return len;
292 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
294 int status;
295 struct buffer_head *bh = NULL;
297 status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
298 if (status < 0)
299 return status;
300 spin_lock(&dq_data_lock);
301 if (!oinfo->dqi_gqi_count++)
302 oinfo->dqi_gqi_bh = bh;
303 else
304 WARN_ON(bh != oinfo->dqi_gqi_bh);
305 spin_unlock(&dq_data_lock);
306 return 0;
309 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
311 ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
312 brelse(oinfo->dqi_gqi_bh);
313 spin_lock(&dq_data_lock);
314 if (!--oinfo->dqi_gqi_count)
315 oinfo->dqi_gqi_bh = NULL;
316 spin_unlock(&dq_data_lock);
319 /* Read information header from global quota file */
320 int ocfs2_global_read_info(struct super_block *sb, int type)
322 struct inode *gqinode = NULL;
323 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
324 GROUP_QUOTA_SYSTEM_INODE };
325 struct ocfs2_global_disk_dqinfo dinfo;
326 struct mem_dqinfo *info = sb_dqinfo(sb, type);
327 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
328 u64 pcount;
329 int status;
331 mlog_entry_void();
333 /* Read global header */
334 gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
335 OCFS2_INVALID_SLOT);
336 if (!gqinode) {
337 mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
338 type);
339 status = -EINVAL;
340 goto out_err;
342 oinfo->dqi_gi.dqi_sb = sb;
343 oinfo->dqi_gi.dqi_type = type;
344 ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
345 oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
346 oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
347 oinfo->dqi_gqi_bh = NULL;
348 oinfo->dqi_gqi_count = 0;
349 oinfo->dqi_gqinode = gqinode;
350 status = ocfs2_lock_global_qf(oinfo, 0);
351 if (status < 0) {
352 mlog_errno(status);
353 goto out_err;
356 status = ocfs2_extent_map_get_blocks(gqinode, 0, &oinfo->dqi_giblk,
357 &pcount, NULL);
358 if (status < 0)
359 goto out_unlock;
361 status = ocfs2_qinfo_lock(oinfo, 0);
362 if (status < 0)
363 goto out_unlock;
364 status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
365 sizeof(struct ocfs2_global_disk_dqinfo),
366 OCFS2_GLOBAL_INFO_OFF);
367 ocfs2_qinfo_unlock(oinfo, 0);
368 ocfs2_unlock_global_qf(oinfo, 0);
369 if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
370 mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
371 status);
372 if (status >= 0)
373 status = -EIO;
374 mlog_errno(status);
375 goto out_err;
377 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
378 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
379 oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
380 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
381 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
382 oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
383 oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
384 oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
385 OCFS2_QBLK_RESERVED_SPACE;
386 oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
387 INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
388 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
389 msecs_to_jiffies(oinfo->dqi_syncms));
391 out_err:
392 mlog_exit(status);
393 return status;
394 out_unlock:
395 ocfs2_unlock_global_qf(oinfo, 0);
396 mlog_errno(status);
397 goto out_err;
400 /* Write information to global quota file. Expects exlusive lock on quota
401 * file inode and quota info */
402 static int __ocfs2_global_write_info(struct super_block *sb, int type)
404 struct mem_dqinfo *info = sb_dqinfo(sb, type);
405 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
406 struct ocfs2_global_disk_dqinfo dinfo;
407 ssize_t size;
409 spin_lock(&dq_data_lock);
410 info->dqi_flags &= ~DQF_INFO_DIRTY;
411 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
412 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
413 spin_unlock(&dq_data_lock);
414 dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
415 dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
416 dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
417 dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
418 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
419 sizeof(struct ocfs2_global_disk_dqinfo),
420 OCFS2_GLOBAL_INFO_OFF);
421 if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
422 mlog(ML_ERROR, "Cannot write global quota info structure\n");
423 if (size >= 0)
424 size = -EIO;
425 return size;
427 return 0;
430 int ocfs2_global_write_info(struct super_block *sb, int type)
432 int err;
433 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
435 err = ocfs2_qinfo_lock(info, 1);
436 if (err < 0)
437 return err;
438 err = __ocfs2_global_write_info(sb, type);
439 ocfs2_qinfo_unlock(info, 1);
440 return err;
443 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
445 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
448 * We may need to allocate tree blocks and a leaf block but not the
449 * root block
451 return oinfo->dqi_gi.dqi_qtree_depth;
454 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
456 /* We modify all the allocated blocks, tree root, and info block */
457 return (ocfs2_global_qinit_alloc(sb, type) + 2) *
458 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
461 /* Read in information from global quota file and acquire a reference to it.
462 * dquot_acquire() has already started the transaction and locked quota file */
463 int ocfs2_global_read_dquot(struct dquot *dquot)
465 int err, err2, ex = 0;
466 struct super_block *sb = dquot->dq_sb;
467 int type = dquot->dq_type;
468 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
469 struct ocfs2_super *osb = OCFS2_SB(sb);
470 struct inode *gqinode = info->dqi_gqinode;
471 int need_alloc = ocfs2_global_qinit_alloc(sb, type);
472 handle_t *handle = NULL;
474 err = ocfs2_qinfo_lock(info, 0);
475 if (err < 0)
476 goto out;
477 err = qtree_read_dquot(&info->dqi_gi, dquot);
478 if (err < 0)
479 goto out_qlock;
480 OCFS2_DQUOT(dquot)->dq_use_count++;
481 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
482 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
483 ocfs2_qinfo_unlock(info, 0);
485 if (!dquot->dq_off) { /* No real quota entry? */
486 ex = 1;
488 * Add blocks to quota file before we start a transaction since
489 * locking allocators ranks above a transaction start
491 WARN_ON(journal_current_handle());
492 down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
493 err = ocfs2_extend_no_holes(gqinode,
494 gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
495 gqinode->i_size);
496 up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
497 if (err < 0)
498 goto out;
501 handle = ocfs2_start_trans(osb,
502 ocfs2_calc_global_qinit_credits(sb, type));
503 if (IS_ERR(handle)) {
504 err = PTR_ERR(handle);
505 goto out;
507 err = ocfs2_qinfo_lock(info, ex);
508 if (err < 0)
509 goto out_trans;
510 err = qtree_write_dquot(&info->dqi_gi, dquot);
511 if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
512 err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
513 if (!err)
514 err = err2;
516 out_qlock:
517 if (ex)
518 ocfs2_qinfo_unlock(info, 1);
519 else
520 ocfs2_qinfo_unlock(info, 0);
521 out_trans:
522 if (handle)
523 ocfs2_commit_trans(osb, handle);
524 out:
525 if (err < 0)
526 mlog_errno(err);
527 return err;
530 /* Sync local information about quota modifications with global quota file.
531 * Caller must have started the transaction and obtained exclusive lock for
532 * global quota file inode */
533 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
535 int err, err2;
536 struct super_block *sb = dquot->dq_sb;
537 int type = dquot->dq_type;
538 struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
539 struct ocfs2_global_disk_dqblk dqblk;
540 s64 spacechange, inodechange;
541 time_t olditime, oldbtime;
543 err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
544 sizeof(struct ocfs2_global_disk_dqblk),
545 dquot->dq_off);
546 if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
547 if (err >= 0) {
548 mlog(ML_ERROR, "Short read from global quota file "
549 "(%u read)\n", err);
550 err = -EIO;
552 goto out;
555 /* Update space and inode usage. Get also other information from
556 * global quota file so that we don't overwrite any changes there.
557 * We are */
558 spin_lock(&dq_data_lock);
559 spacechange = dquot->dq_dqb.dqb_curspace -
560 OCFS2_DQUOT(dquot)->dq_origspace;
561 inodechange = dquot->dq_dqb.dqb_curinodes -
562 OCFS2_DQUOT(dquot)->dq_originodes;
563 olditime = dquot->dq_dqb.dqb_itime;
564 oldbtime = dquot->dq_dqb.dqb_btime;
565 ocfs2_global_disk2memdqb(dquot, &dqblk);
566 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
567 dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
568 dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
569 if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
570 dquot->dq_dqb.dqb_curspace += spacechange;
571 if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
572 dquot->dq_dqb.dqb_curinodes += inodechange;
573 /* Set properly space grace time... */
574 if (dquot->dq_dqb.dqb_bsoftlimit &&
575 dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
576 if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
577 oldbtime > 0) {
578 if (dquot->dq_dqb.dqb_btime > 0)
579 dquot->dq_dqb.dqb_btime =
580 min(dquot->dq_dqb.dqb_btime, oldbtime);
581 else
582 dquot->dq_dqb.dqb_btime = oldbtime;
584 } else {
585 dquot->dq_dqb.dqb_btime = 0;
586 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
588 /* Set properly inode grace time... */
589 if (dquot->dq_dqb.dqb_isoftlimit &&
590 dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
591 if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
592 olditime > 0) {
593 if (dquot->dq_dqb.dqb_itime > 0)
594 dquot->dq_dqb.dqb_itime =
595 min(dquot->dq_dqb.dqb_itime, olditime);
596 else
597 dquot->dq_dqb.dqb_itime = olditime;
599 } else {
600 dquot->dq_dqb.dqb_itime = 0;
601 clear_bit(DQ_INODES_B, &dquot->dq_flags);
603 /* All information is properly updated, clear the flags */
604 __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
605 __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
606 __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
607 __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
608 __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
609 __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
610 OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
611 OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
612 spin_unlock(&dq_data_lock);
613 err = ocfs2_qinfo_lock(info, freeing);
614 if (err < 0) {
615 mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
616 " (type=%d, id=%u)\n", dquot->dq_type,
617 (unsigned)dquot->dq_id);
618 goto out;
620 if (freeing)
621 OCFS2_DQUOT(dquot)->dq_use_count--;
622 err = qtree_write_dquot(&info->dqi_gi, dquot);
623 if (err < 0)
624 goto out_qlock;
625 if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
626 err = qtree_release_dquot(&info->dqi_gi, dquot);
627 if (info_dirty(sb_dqinfo(sb, type))) {
628 err2 = __ocfs2_global_write_info(sb, type);
629 if (!err)
630 err = err2;
633 out_qlock:
634 ocfs2_qinfo_unlock(info, freeing);
635 out:
636 if (err < 0)
637 mlog_errno(err);
638 return err;
642 * Functions for periodic syncing of dquots with global file
644 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
646 handle_t *handle;
647 struct super_block *sb = dquot->dq_sb;
648 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
649 struct ocfs2_super *osb = OCFS2_SB(sb);
650 int status = 0;
652 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
653 dquot->dq_type, type, sb->s_id);
654 if (type != dquot->dq_type)
655 goto out;
656 status = ocfs2_lock_global_qf(oinfo, 1);
657 if (status < 0)
658 goto out;
660 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
661 if (IS_ERR(handle)) {
662 status = PTR_ERR(handle);
663 mlog_errno(status);
664 goto out_ilock;
666 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
667 status = ocfs2_sync_dquot(dquot);
668 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
669 if (status < 0)
670 mlog_errno(status);
671 /* We have to write local structure as well... */
672 dquot_mark_dquot_dirty(dquot);
673 status = dquot_commit(dquot);
674 if (status < 0)
675 mlog_errno(status);
676 ocfs2_commit_trans(osb, handle);
677 out_ilock:
678 ocfs2_unlock_global_qf(oinfo, 1);
679 out:
680 mlog_exit(status);
681 return status;
684 static void qsync_work_fn(struct work_struct *work)
686 struct ocfs2_mem_dqinfo *oinfo = container_of(work,
687 struct ocfs2_mem_dqinfo,
688 dqi_sync_work.work);
689 struct super_block *sb = oinfo->dqi_gqinode->i_sb;
691 dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
692 queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
693 msecs_to_jiffies(oinfo->dqi_syncms));
697 * Wrappers for generic quota functions
700 static int ocfs2_write_dquot(struct dquot *dquot)
702 handle_t *handle;
703 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
704 int status = 0;
706 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
708 handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
709 if (IS_ERR(handle)) {
710 status = PTR_ERR(handle);
711 mlog_errno(status);
712 goto out;
714 status = dquot_commit(dquot);
715 ocfs2_commit_trans(osb, handle);
716 out:
717 mlog_exit(status);
718 return status;
721 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
723 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
725 * We modify tree, leaf block, global info, local chunk header,
726 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
727 * accounts for inode update
729 return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
730 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
731 OCFS2_QINFO_WRITE_CREDITS +
732 OCFS2_INODE_UPDATE_CREDITS;
735 static int ocfs2_release_dquot(struct dquot *dquot)
737 handle_t *handle;
738 struct ocfs2_mem_dqinfo *oinfo =
739 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
740 struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
741 int status = 0;
743 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
745 status = ocfs2_lock_global_qf(oinfo, 1);
746 if (status < 0)
747 goto out;
748 handle = ocfs2_start_trans(osb,
749 ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
750 if (IS_ERR(handle)) {
751 status = PTR_ERR(handle);
752 mlog_errno(status);
753 goto out_ilock;
755 status = dquot_release(dquot);
756 ocfs2_commit_trans(osb, handle);
757 out_ilock:
758 ocfs2_unlock_global_qf(oinfo, 1);
759 out:
760 mlog_exit(status);
761 return status;
764 static int ocfs2_acquire_dquot(struct dquot *dquot)
766 struct ocfs2_mem_dqinfo *oinfo =
767 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
768 int status = 0;
770 mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
771 /* We need an exclusive lock, because we're going to update use count
772 * and instantiate possibly new dquot structure */
773 status = ocfs2_lock_global_qf(oinfo, 1);
774 if (status < 0)
775 goto out;
776 status = dquot_acquire(dquot);
777 ocfs2_unlock_global_qf(oinfo, 1);
778 out:
779 mlog_exit(status);
780 return status;
783 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
785 unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
786 (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
787 (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
788 (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
789 (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
790 (1 << (DQ_LASTSET_B + QIF_ITIME_B));
791 int sync = 0;
792 int status;
793 struct super_block *sb = dquot->dq_sb;
794 int type = dquot->dq_type;
795 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
796 handle_t *handle;
797 struct ocfs2_super *osb = OCFS2_SB(sb);
799 mlog_entry("id=%u, type=%d", dquot->dq_id, type);
800 dquot_mark_dquot_dirty(dquot);
802 /* In case user set some limits, sync dquot immediately to global
803 * quota file so that information propagates quicker */
804 spin_lock(&dq_data_lock);
805 if (dquot->dq_flags & mask)
806 sync = 1;
807 spin_unlock(&dq_data_lock);
808 /* This is a slight hack but we can't afford getting global quota
809 * lock if we already have a transaction started. */
810 if (!sync || journal_current_handle()) {
811 status = ocfs2_write_dquot(dquot);
812 goto out;
814 status = ocfs2_lock_global_qf(oinfo, 1);
815 if (status < 0)
816 goto out;
817 handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
818 if (IS_ERR(handle)) {
819 status = PTR_ERR(handle);
820 mlog_errno(status);
821 goto out_ilock;
823 status = ocfs2_sync_dquot(dquot);
824 if (status < 0) {
825 mlog_errno(status);
826 goto out_trans;
828 /* Now write updated local dquot structure */
829 status = dquot_commit(dquot);
830 out_trans:
831 ocfs2_commit_trans(osb, handle);
832 out_ilock:
833 ocfs2_unlock_global_qf(oinfo, 1);
834 out:
835 mlog_exit(status);
836 return status;
839 /* This should happen only after set_dqinfo(). */
840 static int ocfs2_write_info(struct super_block *sb, int type)
842 handle_t *handle;
843 int status = 0;
844 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
846 mlog_entry_void();
848 status = ocfs2_lock_global_qf(oinfo, 1);
849 if (status < 0)
850 goto out;
851 handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
852 if (IS_ERR(handle)) {
853 status = PTR_ERR(handle);
854 mlog_errno(status);
855 goto out_ilock;
857 status = dquot_commit_info(sb, type);
858 ocfs2_commit_trans(OCFS2_SB(sb), handle);
859 out_ilock:
860 ocfs2_unlock_global_qf(oinfo, 1);
861 out:
862 mlog_exit(status);
863 return status;
866 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
868 struct ocfs2_dquot *dquot =
869 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
871 if (!dquot)
872 return NULL;
873 return &dquot->dq_dquot;
876 static void ocfs2_destroy_dquot(struct dquot *dquot)
878 kmem_cache_free(ocfs2_dquot_cachep, dquot);
881 const struct dquot_operations ocfs2_quota_operations = {
882 .write_dquot = ocfs2_write_dquot,
883 .acquire_dquot = ocfs2_acquire_dquot,
884 .release_dquot = ocfs2_release_dquot,
885 .mark_dirty = ocfs2_mark_dquot_dirty,
886 .write_info = ocfs2_write_info,
887 .alloc_dquot = ocfs2_alloc_dquot,
888 .destroy_dquot = ocfs2_destroy_dquot,
891 int ocfs2_quota_setup(void)
893 ocfs2_quota_wq = create_workqueue("o2quot");
894 if (!ocfs2_quota_wq)
895 return -ENOMEM;
896 return 0;
899 void ocfs2_quota_shutdown(void)
901 if (ocfs2_quota_wq) {
902 flush_workqueue(ocfs2_quota_wq);
903 destroy_workqueue(ocfs2_quota_wq);
904 ocfs2_quota_wq = NULL;