2 * Implementation of operations over global quota file
4 #include <linux/spinlock.h>
6 #include <linux/quota.h>
7 #include <linux/quotaops.h>
8 #include <linux/dqblk_qtree.h>
9 #include <linux/jiffies.h>
10 #include <linux/writeback.h>
11 #include <linux/workqueue.h>
13 #define MLOG_MASK_PREFIX ML_QUOTA
14 #include <cluster/masklog.h>
19 #include "blockcheck.h"
28 static struct workqueue_struct
*ocfs2_quota_wq
= NULL
;
30 static void qsync_work_fn(struct work_struct
*work
);
32 static void ocfs2_global_disk2memdqb(struct dquot
*dquot
, void *dp
)
34 struct ocfs2_global_disk_dqblk
*d
= dp
;
35 struct mem_dqblk
*m
= &dquot
->dq_dqb
;
37 /* Update from disk only entries not set by the admin */
38 if (!test_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
)) {
39 m
->dqb_ihardlimit
= le64_to_cpu(d
->dqb_ihardlimit
);
40 m
->dqb_isoftlimit
= le64_to_cpu(d
->dqb_isoftlimit
);
42 if (!test_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
))
43 m
->dqb_curinodes
= le64_to_cpu(d
->dqb_curinodes
);
44 if (!test_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
)) {
45 m
->dqb_bhardlimit
= le64_to_cpu(d
->dqb_bhardlimit
);
46 m
->dqb_bsoftlimit
= le64_to_cpu(d
->dqb_bsoftlimit
);
48 if (!test_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
))
49 m
->dqb_curspace
= le64_to_cpu(d
->dqb_curspace
);
50 if (!test_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
))
51 m
->dqb_btime
= le64_to_cpu(d
->dqb_btime
);
52 if (!test_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
))
53 m
->dqb_itime
= le64_to_cpu(d
->dqb_itime
);
54 OCFS2_DQUOT(dquot
)->dq_use_count
= le32_to_cpu(d
->dqb_use_count
);
57 static void ocfs2_global_mem2diskdqb(void *dp
, struct dquot
*dquot
)
59 struct ocfs2_global_disk_dqblk
*d
= dp
;
60 struct mem_dqblk
*m
= &dquot
->dq_dqb
;
62 d
->dqb_id
= cpu_to_le32(dquot
->dq_id
);
63 d
->dqb_use_count
= cpu_to_le32(OCFS2_DQUOT(dquot
)->dq_use_count
);
64 d
->dqb_ihardlimit
= cpu_to_le64(m
->dqb_ihardlimit
);
65 d
->dqb_isoftlimit
= cpu_to_le64(m
->dqb_isoftlimit
);
66 d
->dqb_curinodes
= cpu_to_le64(m
->dqb_curinodes
);
67 d
->dqb_bhardlimit
= cpu_to_le64(m
->dqb_bhardlimit
);
68 d
->dqb_bsoftlimit
= cpu_to_le64(m
->dqb_bsoftlimit
);
69 d
->dqb_curspace
= cpu_to_le64(m
->dqb_curspace
);
70 d
->dqb_btime
= cpu_to_le64(m
->dqb_btime
);
71 d
->dqb_itime
= cpu_to_le64(m
->dqb_itime
);
74 static int ocfs2_global_is_id(void *dp
, struct dquot
*dquot
)
76 struct ocfs2_global_disk_dqblk
*d
= dp
;
77 struct ocfs2_mem_dqinfo
*oinfo
=
78 sb_dqinfo(dquot
->dq_sb
, dquot
->dq_type
)->dqi_priv
;
80 if (qtree_entry_unused(&oinfo
->dqi_gi
, dp
))
82 return le32_to_cpu(d
->dqb_id
) == dquot
->dq_id
;
85 struct qtree_fmt_operations ocfs2_global_ops
= {
86 .mem2disk_dqblk
= ocfs2_global_mem2diskdqb
,
87 .disk2mem_dqblk
= ocfs2_global_disk2memdqb
,
88 .is_id
= ocfs2_global_is_id
,
91 static int ocfs2_validate_quota_block(struct super_block
*sb
,
92 struct buffer_head
*bh
)
94 struct ocfs2_disk_dqtrailer
*dqt
=
95 ocfs2_block_dqtrailer(sb
->s_blocksize
, bh
->b_data
);
97 mlog(0, "Validating quota block %llu\n",
98 (unsigned long long)bh
->b_blocknr
);
100 BUG_ON(!buffer_uptodate(bh
));
103 * If the ecc fails, we return the error but otherwise
104 * leave the filesystem running. We know any error is
105 * local to this block.
107 return ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &dqt
->dq_check
);
110 int ocfs2_read_quota_block(struct inode
*inode
, u64 v_block
,
111 struct buffer_head
**bh
)
114 struct buffer_head
*tmp
= *bh
;
116 rc
= ocfs2_read_virt_blocks(inode
, v_block
, 1, &tmp
, 0,
117 ocfs2_validate_quota_block
);
121 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
128 static int ocfs2_get_quota_block(struct inode
*inode
, int block
,
129 struct buffer_head
**bh
)
134 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
135 err
= ocfs2_extent_map_get_blocks(inode
, block
, &pblock
, &pcount
, NULL
);
136 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
141 *bh
= sb_getblk(inode
->i_sb
, pblock
);
149 /* Read data from global quotafile - avoid pagecache and such because we cannot
150 * afford acquiring the locks... We use quota cluster lock to serialize
151 * operations. Caller is responsible for acquiring it. */
152 ssize_t
ocfs2_quota_read(struct super_block
*sb
, int type
, char *data
,
153 size_t len
, loff_t off
)
155 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
156 struct inode
*gqinode
= oinfo
->dqi_gqinode
;
157 loff_t i_size
= i_size_read(gqinode
);
158 int offset
= off
& (sb
->s_blocksize
- 1);
159 sector_t blk
= off
>> sb
->s_blocksize_bits
;
161 struct buffer_head
*bh
;
162 size_t toread
, tocopy
;
166 if (off
+ len
> i_size
)
170 tocopy
= min_t(size_t, (sb
->s_blocksize
- offset
), toread
);
172 err
= ocfs2_read_quota_block(gqinode
, blk
, &bh
);
177 memcpy(data
, bh
->b_data
+ offset
, tocopy
);
187 /* Write to quotafile (we know the transaction is already started and has
189 ssize_t
ocfs2_quota_write(struct super_block
*sb
, int type
,
190 const char *data
, size_t len
, loff_t off
)
192 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
193 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
194 struct inode
*gqinode
= oinfo
->dqi_gqinode
;
195 int offset
= off
& (sb
->s_blocksize
- 1);
196 sector_t blk
= off
>> sb
->s_blocksize_bits
;
197 int err
= 0, new = 0, ja_type
;
198 struct buffer_head
*bh
= NULL
;
199 handle_t
*handle
= journal_current_handle();
202 mlog(ML_ERROR
, "Quota write (off=%llu, len=%llu) cancelled "
203 "because transaction was not started.\n",
204 (unsigned long long)off
, (unsigned long long)len
);
207 if (len
> sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
- offset
) {
209 len
= sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
- offset
;
212 mutex_lock_nested(&gqinode
->i_mutex
, I_MUTEX_QUOTA
);
213 if (gqinode
->i_size
< off
+ len
) {
214 down_write(&OCFS2_I(gqinode
)->ip_alloc_sem
);
215 err
= ocfs2_extend_no_holes(gqinode
, off
+ len
, off
);
216 up_write(&OCFS2_I(gqinode
)->ip_alloc_sem
);
219 err
= ocfs2_simple_size_update(gqinode
,
226 /* Not rewriting whole block? */
227 if ((offset
|| len
< sb
->s_blocksize
- OCFS2_QBLK_RESERVED_SPACE
) &&
229 err
= ocfs2_read_quota_block(gqinode
, blk
, &bh
);
230 ja_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
232 err
= ocfs2_get_quota_block(gqinode
, blk
, &bh
);
233 ja_type
= OCFS2_JOURNAL_ACCESS_CREATE
;
241 memset(bh
->b_data
, 0, sb
->s_blocksize
);
242 memcpy(bh
->b_data
+ offset
, data
, len
);
243 flush_dcache_page(bh
->b_page
);
244 set_buffer_uptodate(bh
);
246 ocfs2_set_buffer_uptodate(gqinode
, bh
);
247 err
= ocfs2_journal_access_dq(handle
, gqinode
, bh
, ja_type
);
252 err
= ocfs2_journal_dirty(handle
, bh
);
258 mutex_unlock(&gqinode
->i_mutex
);
262 gqinode
->i_version
++;
263 ocfs2_mark_inode_dirty(handle
, gqinode
, oinfo
->dqi_gqi_bh
);
264 mutex_unlock(&gqinode
->i_mutex
);
268 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
271 struct buffer_head
*bh
= NULL
;
273 status
= ocfs2_inode_lock(oinfo
->dqi_gqinode
, &bh
, ex
);
276 spin_lock(&dq_data_lock
);
277 if (!oinfo
->dqi_gqi_count
++)
278 oinfo
->dqi_gqi_bh
= bh
;
280 WARN_ON(bh
!= oinfo
->dqi_gqi_bh
);
281 spin_unlock(&dq_data_lock
);
285 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo
*oinfo
, int ex
)
287 ocfs2_inode_unlock(oinfo
->dqi_gqinode
, ex
);
288 brelse(oinfo
->dqi_gqi_bh
);
289 spin_lock(&dq_data_lock
);
290 if (!--oinfo
->dqi_gqi_count
)
291 oinfo
->dqi_gqi_bh
= NULL
;
292 spin_unlock(&dq_data_lock
);
295 /* Read information header from global quota file */
296 int ocfs2_global_read_info(struct super_block
*sb
, int type
)
298 struct inode
*gqinode
= NULL
;
299 unsigned int ino
[MAXQUOTAS
] = { USER_QUOTA_SYSTEM_INODE
,
300 GROUP_QUOTA_SYSTEM_INODE
};
301 struct ocfs2_global_disk_dqinfo dinfo
;
302 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
303 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
308 /* Read global header */
309 gqinode
= ocfs2_get_system_file_inode(OCFS2_SB(sb
), ino
[type
],
312 mlog(ML_ERROR
, "failed to get global quota inode (type=%d)\n",
317 oinfo
->dqi_gi
.dqi_sb
= sb
;
318 oinfo
->dqi_gi
.dqi_type
= type
;
319 ocfs2_qinfo_lock_res_init(&oinfo
->dqi_gqlock
, oinfo
);
320 oinfo
->dqi_gi
.dqi_entry_size
= sizeof(struct ocfs2_global_disk_dqblk
);
321 oinfo
->dqi_gi
.dqi_ops
= &ocfs2_global_ops
;
322 oinfo
->dqi_gqi_bh
= NULL
;
323 oinfo
->dqi_gqi_count
= 0;
324 oinfo
->dqi_gqinode
= gqinode
;
325 status
= ocfs2_lock_global_qf(oinfo
, 0);
330 status
= sb
->s_op
->quota_read(sb
, type
, (char *)&dinfo
,
331 sizeof(struct ocfs2_global_disk_dqinfo
),
332 OCFS2_GLOBAL_INFO_OFF
);
333 ocfs2_unlock_global_qf(oinfo
, 0);
334 if (status
!= sizeof(struct ocfs2_global_disk_dqinfo
)) {
335 mlog(ML_ERROR
, "Cannot read global quota info (%d).\n",
342 info
->dqi_bgrace
= le32_to_cpu(dinfo
.dqi_bgrace
);
343 info
->dqi_igrace
= le32_to_cpu(dinfo
.dqi_igrace
);
344 oinfo
->dqi_syncms
= le32_to_cpu(dinfo
.dqi_syncms
);
345 oinfo
->dqi_syncjiff
= msecs_to_jiffies(oinfo
->dqi_syncms
);
346 oinfo
->dqi_gi
.dqi_blocks
= le32_to_cpu(dinfo
.dqi_blocks
);
347 oinfo
->dqi_gi
.dqi_free_blk
= le32_to_cpu(dinfo
.dqi_free_blk
);
348 oinfo
->dqi_gi
.dqi_free_entry
= le32_to_cpu(dinfo
.dqi_free_entry
);
349 oinfo
->dqi_gi
.dqi_blocksize_bits
= sb
->s_blocksize_bits
;
350 oinfo
->dqi_gi
.dqi_usable_bs
= sb
->s_blocksize
-
351 OCFS2_QBLK_RESERVED_SPACE
;
352 oinfo
->dqi_gi
.dqi_qtree_depth
= qtree_depth(&oinfo
->dqi_gi
);
353 INIT_DELAYED_WORK(&oinfo
->dqi_sync_work
, qsync_work_fn
);
354 queue_delayed_work(ocfs2_quota_wq
, &oinfo
->dqi_sync_work
,
355 oinfo
->dqi_syncjiff
);
362 /* Write information to global quota file. Expects exlusive lock on quota
363 * file inode and quota info */
364 static int __ocfs2_global_write_info(struct super_block
*sb
, int type
)
366 struct mem_dqinfo
*info
= sb_dqinfo(sb
, type
);
367 struct ocfs2_mem_dqinfo
*oinfo
= info
->dqi_priv
;
368 struct ocfs2_global_disk_dqinfo dinfo
;
371 spin_lock(&dq_data_lock
);
372 info
->dqi_flags
&= ~DQF_INFO_DIRTY
;
373 dinfo
.dqi_bgrace
= cpu_to_le32(info
->dqi_bgrace
);
374 dinfo
.dqi_igrace
= cpu_to_le32(info
->dqi_igrace
);
375 spin_unlock(&dq_data_lock
);
376 dinfo
.dqi_syncms
= cpu_to_le32(oinfo
->dqi_syncms
);
377 dinfo
.dqi_blocks
= cpu_to_le32(oinfo
->dqi_gi
.dqi_blocks
);
378 dinfo
.dqi_free_blk
= cpu_to_le32(oinfo
->dqi_gi
.dqi_free_blk
);
379 dinfo
.dqi_free_entry
= cpu_to_le32(oinfo
->dqi_gi
.dqi_free_entry
);
380 size
= sb
->s_op
->quota_write(sb
, type
, (char *)&dinfo
,
381 sizeof(struct ocfs2_global_disk_dqinfo
),
382 OCFS2_GLOBAL_INFO_OFF
);
383 if (size
!= sizeof(struct ocfs2_global_disk_dqinfo
)) {
384 mlog(ML_ERROR
, "Cannot write global quota info structure\n");
392 int ocfs2_global_write_info(struct super_block
*sb
, int type
)
395 struct ocfs2_mem_dqinfo
*info
= sb_dqinfo(sb
, type
)->dqi_priv
;
397 err
= ocfs2_qinfo_lock(info
, 1);
400 err
= __ocfs2_global_write_info(sb
, type
);
401 ocfs2_qinfo_unlock(info
, 1);
405 /* Read in information from global quota file and acquire a reference to it.
406 * dquot_acquire() has already started the transaction and locked quota file */
407 int ocfs2_global_read_dquot(struct dquot
*dquot
)
409 int err
, err2
, ex
= 0;
410 struct ocfs2_mem_dqinfo
*info
=
411 sb_dqinfo(dquot
->dq_sb
, dquot
->dq_type
)->dqi_priv
;
413 err
= ocfs2_qinfo_lock(info
, 0);
416 err
= qtree_read_dquot(&info
->dqi_gi
, dquot
);
419 OCFS2_DQUOT(dquot
)->dq_use_count
++;
420 OCFS2_DQUOT(dquot
)->dq_origspace
= dquot
->dq_dqb
.dqb_curspace
;
421 OCFS2_DQUOT(dquot
)->dq_originodes
= dquot
->dq_dqb
.dqb_curinodes
;
422 if (!dquot
->dq_off
) { /* No real quota entry? */
423 /* Upgrade to exclusive lock for allocation */
424 ocfs2_qinfo_unlock(info
, 0);
425 err
= ocfs2_qinfo_lock(info
, 1);
430 err
= qtree_write_dquot(&info
->dqi_gi
, dquot
);
431 if (ex
&& info_dirty(sb_dqinfo(dquot
->dq_sb
, dquot
->dq_type
))) {
432 err2
= __ocfs2_global_write_info(dquot
->dq_sb
, dquot
->dq_type
);
438 ocfs2_qinfo_unlock(info
, 1);
440 ocfs2_qinfo_unlock(info
, 0);
447 /* Sync local information about quota modifications with global quota file.
448 * Caller must have started the transaction and obtained exclusive lock for
449 * global quota file inode */
450 int __ocfs2_sync_dquot(struct dquot
*dquot
, int freeing
)
453 struct super_block
*sb
= dquot
->dq_sb
;
454 int type
= dquot
->dq_type
;
455 struct ocfs2_mem_dqinfo
*info
= sb_dqinfo(sb
, type
)->dqi_priv
;
456 struct ocfs2_global_disk_dqblk dqblk
;
457 s64 spacechange
, inodechange
;
458 time_t olditime
, oldbtime
;
460 err
= sb
->s_op
->quota_read(sb
, type
, (char *)&dqblk
,
461 sizeof(struct ocfs2_global_disk_dqblk
),
463 if (err
!= sizeof(struct ocfs2_global_disk_dqblk
)) {
465 mlog(ML_ERROR
, "Short read from global quota file "
472 /* Update space and inode usage. Get also other information from
473 * global quota file so that we don't overwrite any changes there.
475 spin_lock(&dq_data_lock
);
476 spacechange
= dquot
->dq_dqb
.dqb_curspace
-
477 OCFS2_DQUOT(dquot
)->dq_origspace
;
478 inodechange
= dquot
->dq_dqb
.dqb_curinodes
-
479 OCFS2_DQUOT(dquot
)->dq_originodes
;
480 olditime
= dquot
->dq_dqb
.dqb_itime
;
481 oldbtime
= dquot
->dq_dqb
.dqb_btime
;
482 ocfs2_global_disk2memdqb(dquot
, &dqblk
);
483 mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
484 dquot
->dq_id
, dquot
->dq_dqb
.dqb_curspace
, (long long)spacechange
,
485 dquot
->dq_dqb
.dqb_curinodes
, (long long)inodechange
);
486 if (!test_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
))
487 dquot
->dq_dqb
.dqb_curspace
+= spacechange
;
488 if (!test_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
))
489 dquot
->dq_dqb
.dqb_curinodes
+= inodechange
;
490 /* Set properly space grace time... */
491 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
492 dquot
->dq_dqb
.dqb_curspace
> dquot
->dq_dqb
.dqb_bsoftlimit
) {
493 if (!test_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
) &&
495 if (dquot
->dq_dqb
.dqb_btime
> 0)
496 dquot
->dq_dqb
.dqb_btime
=
497 min(dquot
->dq_dqb
.dqb_btime
, oldbtime
);
499 dquot
->dq_dqb
.dqb_btime
= oldbtime
;
502 dquot
->dq_dqb
.dqb_btime
= 0;
503 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
505 /* Set properly inode grace time... */
506 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
507 dquot
->dq_dqb
.dqb_curinodes
> dquot
->dq_dqb
.dqb_isoftlimit
) {
508 if (!test_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
) &&
510 if (dquot
->dq_dqb
.dqb_itime
> 0)
511 dquot
->dq_dqb
.dqb_itime
=
512 min(dquot
->dq_dqb
.dqb_itime
, olditime
);
514 dquot
->dq_dqb
.dqb_itime
= olditime
;
517 dquot
->dq_dqb
.dqb_itime
= 0;
518 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
520 /* All information is properly updated, clear the flags */
521 __clear_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
522 __clear_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
523 __clear_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
524 __clear_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
525 __clear_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
526 __clear_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
527 OCFS2_DQUOT(dquot
)->dq_origspace
= dquot
->dq_dqb
.dqb_curspace
;
528 OCFS2_DQUOT(dquot
)->dq_originodes
= dquot
->dq_dqb
.dqb_curinodes
;
529 spin_unlock(&dq_data_lock
);
530 err
= ocfs2_qinfo_lock(info
, freeing
);
532 mlog(ML_ERROR
, "Failed to lock quota info, loosing quota write"
533 " (type=%d, id=%u)\n", dquot
->dq_type
,
534 (unsigned)dquot
->dq_id
);
538 OCFS2_DQUOT(dquot
)->dq_use_count
--;
539 err
= qtree_write_dquot(&info
->dqi_gi
, dquot
);
542 if (freeing
&& !OCFS2_DQUOT(dquot
)->dq_use_count
) {
543 err
= qtree_release_dquot(&info
->dqi_gi
, dquot
);
544 if (info_dirty(sb_dqinfo(sb
, type
))) {
545 err2
= __ocfs2_global_write_info(sb
, type
);
551 ocfs2_qinfo_unlock(info
, freeing
);
559 * Functions for periodic syncing of dquots with global file
561 static int ocfs2_sync_dquot_helper(struct dquot
*dquot
, unsigned long type
)
564 struct super_block
*sb
= dquot
->dq_sb
;
565 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
566 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
569 mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot
->dq_id
,
570 dquot
->dq_type
, type
, sb
->s_id
);
571 if (type
!= dquot
->dq_type
)
573 status
= ocfs2_lock_global_qf(oinfo
, 1);
577 handle
= ocfs2_start_trans(osb
, OCFS2_QSYNC_CREDITS
);
578 if (IS_ERR(handle
)) {
579 status
= PTR_ERR(handle
);
583 mutex_lock(&sb_dqopt(sb
)->dqio_mutex
);
584 status
= ocfs2_sync_dquot(dquot
);
585 mutex_unlock(&sb_dqopt(sb
)->dqio_mutex
);
588 /* We have to write local structure as well... */
589 dquot_mark_dquot_dirty(dquot
);
590 status
= dquot_commit(dquot
);
593 ocfs2_commit_trans(osb
, handle
);
595 ocfs2_unlock_global_qf(oinfo
, 1);
601 static void qsync_work_fn(struct work_struct
*work
)
603 struct ocfs2_mem_dqinfo
*oinfo
= container_of(work
,
604 struct ocfs2_mem_dqinfo
,
606 struct super_block
*sb
= oinfo
->dqi_gqinode
->i_sb
;
608 dquot_scan_active(sb
, ocfs2_sync_dquot_helper
, oinfo
->dqi_type
);
609 queue_delayed_work(ocfs2_quota_wq
, &oinfo
->dqi_sync_work
,
610 oinfo
->dqi_syncjiff
);
614 * Wrappers for generic quota functions
617 static int ocfs2_write_dquot(struct dquot
*dquot
)
620 struct ocfs2_super
*osb
= OCFS2_SB(dquot
->dq_sb
);
623 mlog_entry("id=%u, type=%d", dquot
->dq_id
, dquot
->dq_type
);
625 handle
= ocfs2_start_trans(osb
, OCFS2_QWRITE_CREDITS
);
626 if (IS_ERR(handle
)) {
627 status
= PTR_ERR(handle
);
631 status
= dquot_commit(dquot
);
632 ocfs2_commit_trans(osb
, handle
);
638 int ocfs2_calc_qdel_credits(struct super_block
*sb
, int type
)
640 struct ocfs2_mem_dqinfo
*oinfo
;
641 int features
[MAXQUOTAS
] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA
,
642 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA
};
644 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb
, features
[type
]))
647 oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
648 /* We modify tree, leaf block, global info, local chunk header,
649 * global and local inode */
650 return oinfo
->dqi_gi
.dqi_qtree_depth
+ 2 + 1 +
651 2 * OCFS2_INODE_UPDATE_CREDITS
;
654 static int ocfs2_release_dquot(struct dquot
*dquot
)
657 struct ocfs2_mem_dqinfo
*oinfo
=
658 sb_dqinfo(dquot
->dq_sb
, dquot
->dq_type
)->dqi_priv
;
659 struct ocfs2_super
*osb
= OCFS2_SB(dquot
->dq_sb
);
662 mlog_entry("id=%u, type=%d", dquot
->dq_id
, dquot
->dq_type
);
664 status
= ocfs2_lock_global_qf(oinfo
, 1);
667 handle
= ocfs2_start_trans(osb
,
668 ocfs2_calc_qdel_credits(dquot
->dq_sb
, dquot
->dq_type
));
669 if (IS_ERR(handle
)) {
670 status
= PTR_ERR(handle
);
674 status
= dquot_release(dquot
);
675 ocfs2_commit_trans(osb
, handle
);
677 ocfs2_unlock_global_qf(oinfo
, 1);
683 int ocfs2_calc_qinit_credits(struct super_block
*sb
, int type
)
685 struct ocfs2_mem_dqinfo
*oinfo
;
686 int features
[MAXQUOTAS
] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA
,
687 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA
};
688 struct ocfs2_dinode
*lfe
, *gfe
;
690 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb
, features
[type
]))
693 oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
694 gfe
= (struct ocfs2_dinode
*)oinfo
->dqi_gqi_bh
->b_data
;
695 lfe
= (struct ocfs2_dinode
*)oinfo
->dqi_lqi_bh
->b_data
;
696 /* We can extend local file + global file. In local file we
697 * can modify info, chunk header block and dquot block. In
698 * global file we can modify info, tree and leaf block */
699 return ocfs2_calc_extend_credits(sb
, &lfe
->id2
.i_list
, 0) +
700 ocfs2_calc_extend_credits(sb
, &gfe
->id2
.i_list
, 0) +
701 3 + oinfo
->dqi_gi
.dqi_qtree_depth
+ 2;
704 static int ocfs2_acquire_dquot(struct dquot
*dquot
)
707 struct ocfs2_mem_dqinfo
*oinfo
=
708 sb_dqinfo(dquot
->dq_sb
, dquot
->dq_type
)->dqi_priv
;
709 struct ocfs2_super
*osb
= OCFS2_SB(dquot
->dq_sb
);
712 mlog_entry("id=%u, type=%d", dquot
->dq_id
, dquot
->dq_type
);
713 /* We need an exclusive lock, because we're going to update use count
714 * and instantiate possibly new dquot structure */
715 status
= ocfs2_lock_global_qf(oinfo
, 1);
718 handle
= ocfs2_start_trans(osb
,
719 ocfs2_calc_qinit_credits(dquot
->dq_sb
, dquot
->dq_type
));
720 if (IS_ERR(handle
)) {
721 status
= PTR_ERR(handle
);
725 status
= dquot_acquire(dquot
);
726 ocfs2_commit_trans(osb
, handle
);
728 ocfs2_unlock_global_qf(oinfo
, 1);
734 static int ocfs2_mark_dquot_dirty(struct dquot
*dquot
)
736 unsigned long mask
= (1 << (DQ_LASTSET_B
+ QIF_ILIMITS_B
)) |
737 (1 << (DQ_LASTSET_B
+ QIF_BLIMITS_B
)) |
738 (1 << (DQ_LASTSET_B
+ QIF_INODES_B
)) |
739 (1 << (DQ_LASTSET_B
+ QIF_SPACE_B
)) |
740 (1 << (DQ_LASTSET_B
+ QIF_BTIME_B
)) |
741 (1 << (DQ_LASTSET_B
+ QIF_ITIME_B
));
744 struct super_block
*sb
= dquot
->dq_sb
;
745 int type
= dquot
->dq_type
;
746 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
748 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
750 mlog_entry("id=%u, type=%d", dquot
->dq_id
, type
);
751 dquot_mark_dquot_dirty(dquot
);
753 /* In case user set some limits, sync dquot immediately to global
754 * quota file so that information propagates quicker */
755 spin_lock(&dq_data_lock
);
756 if (dquot
->dq_flags
& mask
)
758 spin_unlock(&dq_data_lock
);
759 /* This is a slight hack but we can't afford getting global quota
760 * lock if we already have a transaction started. */
761 if (!sync
|| journal_current_handle()) {
762 status
= ocfs2_write_dquot(dquot
);
765 status
= ocfs2_lock_global_qf(oinfo
, 1);
768 handle
= ocfs2_start_trans(osb
, OCFS2_QSYNC_CREDITS
);
769 if (IS_ERR(handle
)) {
770 status
= PTR_ERR(handle
);
774 status
= ocfs2_sync_dquot(dquot
);
779 /* Now write updated local dquot structure */
780 status
= dquot_commit(dquot
);
782 ocfs2_commit_trans(osb
, handle
);
784 ocfs2_unlock_global_qf(oinfo
, 1);
790 /* This should happen only after set_dqinfo(). */
791 static int ocfs2_write_info(struct super_block
*sb
, int type
)
795 struct ocfs2_mem_dqinfo
*oinfo
= sb_dqinfo(sb
, type
)->dqi_priv
;
799 status
= ocfs2_lock_global_qf(oinfo
, 1);
802 handle
= ocfs2_start_trans(OCFS2_SB(sb
), OCFS2_QINFO_WRITE_CREDITS
);
803 if (IS_ERR(handle
)) {
804 status
= PTR_ERR(handle
);
808 status
= dquot_commit_info(sb
, type
);
809 ocfs2_commit_trans(OCFS2_SB(sb
), handle
);
811 ocfs2_unlock_global_qf(oinfo
, 1);
817 static struct dquot
*ocfs2_alloc_dquot(struct super_block
*sb
, int type
)
819 struct ocfs2_dquot
*dquot
=
820 kmem_cache_zalloc(ocfs2_dquot_cachep
, GFP_NOFS
);
824 return &dquot
->dq_dquot
;
827 static void ocfs2_destroy_dquot(struct dquot
*dquot
)
829 kmem_cache_free(ocfs2_dquot_cachep
, dquot
);
832 struct dquot_operations ocfs2_quota_operations
= {
833 .initialize
= dquot_initialize
,
835 .alloc_space
= dquot_alloc_space
,
836 .alloc_inode
= dquot_alloc_inode
,
837 .free_space
= dquot_free_space
,
838 .free_inode
= dquot_free_inode
,
839 .transfer
= dquot_transfer
,
840 .write_dquot
= ocfs2_write_dquot
,
841 .acquire_dquot
= ocfs2_acquire_dquot
,
842 .release_dquot
= ocfs2_release_dquot
,
843 .mark_dirty
= ocfs2_mark_dquot_dirty
,
844 .write_info
= ocfs2_write_info
,
845 .alloc_dquot
= ocfs2_alloc_dquot
,
846 .destroy_dquot
= ocfs2_destroy_dquot
,
849 int ocfs2_quota_setup(void)
851 ocfs2_quota_wq
= create_workqueue("o2quot");
857 void ocfs2_quota_shutdown(void)
859 if (ocfs2_quota_wq
) {
860 flush_workqueue(ocfs2_quota_wq
);
861 destroy_workqueue(ocfs2_quota_wq
);
862 ocfs2_quota_wq
= NULL
;