2 * Implementation of operations over global quota file
4 #include <linux/spinlock.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>
20 #include "blockcheck.h"
28 #include "buffer_head_io.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
))
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
)
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
));
129 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, 0,
130 ocfs2_validate_quota_block
);
134 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
141 int ocfs2_read_quota_phys_block(struct inode
*inode
, u64 p_block
,
142 struct buffer_head
**bhp
)
147 rc
= ocfs2_read_blocks(INODE_CACHE(inode
), p_block
, 1, bhp
, 0,
148 ocfs2_validate_quota_block
);
154 static int ocfs2_get_quota_block(struct inode
*inode
, int block
,
155 struct buffer_head
**bh
)
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
);
167 *bh
= sb_getblk(inode
->i_sb
, pblock
);
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
;
187 struct buffer_head
*bh
;
188 size_t toread
, tocopy
;
192 if (off
+ len
> i_size
)
196 tocopy
= min_t(size_t, (sb
->s_blocksize
- offset
), toread
);
198 err
= ocfs2_read_quota_block(gqinode
, blk
, &bh
);
203 memcpy(data
, bh
->b_data
+ offset
, tocopy
);
213 /* Write to quotafile (we know the transaction is already started and has
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();
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
);
233 if (len
> sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
- offset
) {
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
) {
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
,
251 /* Not rewriting whole block? */
252 if ((offset
|| len
< sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
) &&
254 err
= ocfs2_read_quota_block(gqinode
, blk
, &bh
);
255 ja_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
257 err
= ocfs2_get_quota_block(gqinode
, blk
, &bh
);
258 ja_type
= OCFS2_JOURNAL_ACCESS_CREATE
;
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
);
271 ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode
), bh
);
272 err
= ocfs2_journal_access_dq(handle
, INODE_CACHE(gqinode
), bh
,
278 ocfs2_journal_dirty(handle
, bh
);
282 mutex_unlock(&gqinode
->i_mutex
);
286 gqinode
->i_version
++;
287 ocfs2_mark_inode_dirty(handle
, gqinode
, oinfo
->dqi_gqi_bh
);
288 mutex_unlock(&gqinode
->i_mutex
);
292 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
295 struct buffer_head
*bh
= NULL
;
297 status
= ocfs2_inode_lock(oinfo
->dqi_gqinode
, &bh
, ex
);
300 spin_lock(&dq_data_lock
);
301 if (!oinfo
->dqi_gqi_count
++)
302 oinfo
->dqi_gqi_bh
= bh
;
304 WARN_ON(bh
!= oinfo
->dqi_gqi_bh
);
305 spin_unlock(&dq_data_lock
);
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
;
333 /* Read global header */
334 gqinode
= ocfs2_get_system_file_inode(OCFS2_SB(sb
), ino
[type
],
337 mlog(ML_ERROR
, "failed to get global quota inode (type=%d)\n",
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);
356 status
= ocfs2_extent_map_get_blocks(gqinode
, 0, &oinfo
->dqi_giblk
,
361 status
= ocfs2_qinfo_lock(oinfo
, 0);
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",
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
));
395 ocfs2_unlock_global_qf(oinfo
, 0);
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
;
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");
430 int ocfs2_global_write_info(struct super_block
*sb
, int type
)
433 struct ocfs2_mem_dqinfo
*info
= sb_dqinfo(sb
, type
)->dqi_priv
;
435 err
= ocfs2_qinfo_lock(info
, 1);
438 err
= __ocfs2_global_write_info(sb
, type
);
439 ocfs2_qinfo_unlock(info
, 1);
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
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);
477 err
= qtree_read_dquot(&info
->dqi_gi
, dquot
);
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? */
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
),
496 up_write(&OCFS2_I(gqinode
)->ip_alloc_sem
);
501 handle
= ocfs2_start_trans(osb
,
502 ocfs2_calc_global_qinit_credits(sb
, type
));
503 if (IS_ERR(handle
)) {
504 err
= PTR_ERR(handle
);
507 err
= ocfs2_qinfo_lock(info
, ex
);
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
);
518 ocfs2_qinfo_unlock(info
, 1);
520 ocfs2_qinfo_unlock(info
, 0);
523 ocfs2_commit_trans(osb
, handle
);
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
)
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
),
546 if (err
!= sizeof(struct ocfs2_global_disk_dqblk
)) {
548 mlog(ML_ERROR
, "Short read from global quota file "
555 /* Update space and inode usage. Get also other information from
556 * global quota file so that we don't overwrite any changes there.
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
) &&
578 if (dquot
->dq_dqb
.dqb_btime
> 0)
579 dquot
->dq_dqb
.dqb_btime
=
580 min(dquot
->dq_dqb
.dqb_btime
, oldbtime
);
582 dquot
->dq_dqb
.dqb_btime
= oldbtime
;
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
) &&
593 if (dquot
->dq_dqb
.dqb_itime
> 0)
594 dquot
->dq_dqb
.dqb_itime
=
595 min(dquot
->dq_dqb
.dqb_itime
, olditime
);
597 dquot
->dq_dqb
.dqb_itime
= olditime
;
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
);
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
);
621 OCFS2_DQUOT(dquot
)->dq_use_count
--;
622 err
= qtree_write_dquot(&info
->dqi_gi
, dquot
);
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
);
634 ocfs2_qinfo_unlock(info
, freeing
);
642 * Functions for periodic syncing of dquots with global file
644 static int ocfs2_sync_dquot_helper(struct dquot
*dquot
, unsigned long type
)
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
);
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
)
656 status
= ocfs2_lock_global_qf(oinfo
, 1);
660 handle
= ocfs2_start_trans(osb
, OCFS2_QSYNC_CREDITS
);
661 if (IS_ERR(handle
)) {
662 status
= PTR_ERR(handle
);
666 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
667 status
= ocfs2_sync_dquot(dquot
);
668 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
671 /* We have to write local structure as well... */
672 dquot_mark_dquot_dirty(dquot
);
673 status
= dquot_commit(dquot
);
676 ocfs2_commit_trans(osb
, handle
);
678 ocfs2_unlock_global_qf(oinfo
, 1);
684 static void qsync_work_fn(struct work_struct
*work
)
686 struct ocfs2_mem_dqinfo
*oinfo
= container_of(work
,
687 struct ocfs2_mem_dqinfo
,
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
)
703 struct ocfs2_super
*osb
= OCFS2_SB(dquot
->dq_sb
);
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
);
714 status
= dquot_commit(dquot
);
715 ocfs2_commit_trans(osb
, handle
);
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
)
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
);
743 mlog_entry("id=%u, type=%d", dquot
->dq_id
, dquot
->dq_type
);
745 status
= ocfs2_lock_global_qf(oinfo
, 1);
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
);
755 status
= dquot_release(dquot
);
756 ocfs2_commit_trans(osb
, handle
);
758 ocfs2_unlock_global_qf(oinfo
, 1);
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
;
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);
776 status
= dquot_acquire(dquot
);
777 ocfs2_unlock_global_qf(oinfo
, 1);
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
));
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
;
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
)
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
);
814 status
= ocfs2_lock_global_qf(oinfo
, 1);
817 handle
= ocfs2_start_trans(osb
, OCFS2_QSYNC_CREDITS
);
818 if (IS_ERR(handle
)) {
819 status
= PTR_ERR(handle
);
823 status
= ocfs2_sync_dquot(dquot
);
828 /* Now write updated local dquot structure */
829 status
= dquot_commit(dquot
);
831 ocfs2_commit_trans(osb
, handle
);
833 ocfs2_unlock_global_qf(oinfo
, 1);
839 /* This should happen only after set_dqinfo(). */
840 static int ocfs2_write_info(struct super_block
*sb
, int type
)
844 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
848 status
= ocfs2_lock_global_qf(oinfo
, 1);
851 handle
= ocfs2_start_trans(OCFS2_SB(sb
), OCFS2_QINFO_WRITE_CREDITS
);
852 if (IS_ERR(handle
)) {
853 status
= PTR_ERR(handle
);
857 status
= dquot_commit_info(sb
, type
);
858 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
860 ocfs2_unlock_global_qf(oinfo
, 1);
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
);
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");
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
;