Staging: rt3070: remove dead MULTIPLE_CARD_SUPPORT code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / quota_local.c
blob5a460fa8255384f615d2dd7ca8836d3091fc58b5
1 /*
2 * Implementation of operations over local quota file
3 */
5 #include <linux/fs.h>
6 #include <linux/quota.h>
7 #include <linux/quotaops.h>
8 #include <linux/module.h>
10 #define MLOG_MASK_PREFIX ML_QUOTA
11 #include <cluster/masklog.h>
13 #include "ocfs2_fs.h"
14 #include "ocfs2.h"
15 #include "inode.h"
16 #include "alloc.h"
17 #include "file.h"
18 #include "buffer_head_io.h"
19 #include "journal.h"
20 #include "sysfile.h"
21 #include "dlmglue.h"
22 #include "quota.h"
24 /* Number of local quota structures per block */
25 static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
27 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
28 sizeof(struct ocfs2_local_disk_dqblk));
31 /* Number of blocks with entries in one chunk */
32 static inline unsigned int ol_chunk_blocks(struct super_block *sb)
34 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
35 OCFS2_QBLK_RESERVED_SPACE) << 3) /
36 ol_quota_entries_per_block(sb);
39 /* Number of entries in a chunk bitmap */
40 static unsigned int ol_chunk_entries(struct super_block *sb)
42 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
45 /* Offset of the chunk in quota file */
46 static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
48 /* 1 block for local quota file info, 1 block per chunk for chunk info */
49 return 1 + (ol_chunk_blocks(sb) + 1) * c;
52 static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
54 int epb = ol_quota_entries_per_block(sb);
56 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
59 static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
61 int epb = ol_quota_entries_per_block(sb);
63 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
66 /* Offset of the dquot structure in the quota file */
67 static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
69 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
70 ol_dqblk_block_off(sb, c, off);
73 /* Compute block number from given offset */
74 static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
76 return off >> sb->s_blocksize_bits;
79 static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
81 return off & ((1 << sb->s_blocksize_bits) - 1);
84 /* Compute offset in the chunk of a structure with the given offset */
85 static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
87 int epb = ol_quota_entries_per_block(sb);
89 return ((off >> sb->s_blocksize_bits) -
90 ol_quota_chunk_block(sb, c) - 1) * epb
91 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
92 sizeof(struct ocfs2_local_disk_dqblk);
95 /* Write bufferhead into the fs */
96 static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
97 void (*modify)(struct buffer_head *, void *), void *private)
99 struct super_block *sb = inode->i_sb;
100 handle_t *handle;
101 int status;
103 handle = ocfs2_start_trans(OCFS2_SB(sb), 1);
104 if (IS_ERR(handle)) {
105 status = PTR_ERR(handle);
106 mlog_errno(status);
107 return status;
109 status = ocfs2_journal_access_dq(handle, inode, bh,
110 OCFS2_JOURNAL_ACCESS_WRITE);
111 if (status < 0) {
112 mlog_errno(status);
113 ocfs2_commit_trans(OCFS2_SB(sb), handle);
114 return status;
116 lock_buffer(bh);
117 modify(bh, private);
118 unlock_buffer(bh);
119 status = ocfs2_journal_dirty(handle, bh);
120 if (status < 0) {
121 mlog_errno(status);
122 ocfs2_commit_trans(OCFS2_SB(sb), handle);
123 return status;
125 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
126 if (status < 0) {
127 mlog_errno(status);
128 return status;
130 return 0;
133 /* Check whether we understand format of quota files */
134 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
136 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
137 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
138 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
139 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
140 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
141 GROUP_QUOTA_SYSTEM_INODE };
142 struct buffer_head *bh = NULL;
143 struct inode *linode = sb_dqopt(sb)->files[type];
144 struct inode *ginode = NULL;
145 struct ocfs2_disk_dqheader *dqhead;
146 int status, ret = 0;
148 /* First check whether we understand local quota file */
149 status = ocfs2_read_quota_block(linode, 0, &bh);
150 if (status) {
151 mlog_errno(status);
152 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
153 type);
154 goto out_err;
156 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
157 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
158 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
159 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
160 lmagics[type], type);
161 goto out_err;
163 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
164 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
165 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
166 lversions[type], type);
167 goto out_err;
169 brelse(bh);
170 bh = NULL;
172 /* Next check whether we understand global quota file */
173 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
174 OCFS2_INVALID_SLOT);
175 if (!ginode) {
176 mlog(ML_ERROR, "cannot get global quota file inode "
177 "(type=%d)\n", type);
178 goto out_err;
180 /* Since the header is read only, we don't care about locking */
181 status = ocfs2_read_quota_block(ginode, 0, &bh);
182 if (status) {
183 mlog_errno(status);
184 mlog(ML_ERROR, "failed to read global quota file header "
185 "(type=%d)\n", type);
186 goto out_err;
188 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
189 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
190 mlog(ML_ERROR, "global quota file magic does not match "
191 "(%u != %u), type=%d\n",
192 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
193 goto out_err;
195 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
196 mlog(ML_ERROR, "global quota file version does not match "
197 "(%u != %u), type=%d\n",
198 le32_to_cpu(dqhead->dqh_version), gversions[type],
199 type);
200 goto out_err;
203 ret = 1;
204 out_err:
205 brelse(bh);
206 iput(ginode);
207 return ret;
210 /* Release given list of quota file chunks */
211 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
213 struct ocfs2_quota_chunk *pos, *next;
215 list_for_each_entry_safe(pos, next, head, qc_chunk) {
216 list_del(&pos->qc_chunk);
217 brelse(pos->qc_headerbh);
218 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
222 /* Load quota bitmaps into memory */
223 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
224 struct ocfs2_local_disk_dqinfo *ldinfo,
225 struct list_head *head)
227 struct ocfs2_quota_chunk *newchunk;
228 int i, status;
230 INIT_LIST_HEAD(head);
231 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
232 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
233 if (!newchunk) {
234 ocfs2_release_local_quota_bitmaps(head);
235 return -ENOMEM;
237 newchunk->qc_num = i;
238 newchunk->qc_headerbh = NULL;
239 status = ocfs2_read_quota_block(inode,
240 ol_quota_chunk_block(inode->i_sb, i),
241 &newchunk->qc_headerbh);
242 if (status) {
243 mlog_errno(status);
244 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
245 ocfs2_release_local_quota_bitmaps(head);
246 return status;
248 list_add_tail(&newchunk->qc_chunk, head);
250 return 0;
253 static void olq_update_info(struct buffer_head *bh, void *private)
255 struct mem_dqinfo *info = private;
256 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
257 struct ocfs2_local_disk_dqinfo *ldinfo;
259 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
260 OCFS2_LOCAL_INFO_OFF);
261 spin_lock(&dq_data_lock);
262 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
263 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
264 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
265 spin_unlock(&dq_data_lock);
268 static int ocfs2_add_recovery_chunk(struct super_block *sb,
269 struct ocfs2_local_disk_chunk *dchunk,
270 int chunk,
271 struct list_head *head)
273 struct ocfs2_recovery_chunk *rc;
275 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
276 if (!rc)
277 return -ENOMEM;
278 rc->rc_chunk = chunk;
279 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
280 if (!rc->rc_bitmap) {
281 kfree(rc);
282 return -ENOMEM;
284 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
285 (ol_chunk_entries(sb) + 7) >> 3);
286 list_add_tail(&rc->rc_list, head);
287 return 0;
290 static void free_recovery_list(struct list_head *head)
292 struct ocfs2_recovery_chunk *next;
293 struct ocfs2_recovery_chunk *rchunk;
295 list_for_each_entry_safe(rchunk, next, head, rc_list) {
296 list_del(&rchunk->rc_list);
297 kfree(rchunk->rc_bitmap);
298 kfree(rchunk);
302 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
304 int type;
306 for (type = 0; type < MAXQUOTAS; type++)
307 free_recovery_list(&(rec->r_list[type]));
308 kfree(rec);
311 /* Load entries in our quota file we have to recover*/
312 static int ocfs2_recovery_load_quota(struct inode *lqinode,
313 struct ocfs2_local_disk_dqinfo *ldinfo,
314 int type,
315 struct list_head *head)
317 struct super_block *sb = lqinode->i_sb;
318 struct buffer_head *hbh;
319 struct ocfs2_local_disk_chunk *dchunk;
320 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
321 int status = 0;
323 for (i = 0; i < chunks; i++) {
324 hbh = NULL;
325 status = ocfs2_read_quota_block(lqinode,
326 ol_quota_chunk_block(sb, i),
327 &hbh);
328 if (status) {
329 mlog_errno(status);
330 break;
332 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
333 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
334 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
335 brelse(hbh);
336 if (status < 0)
337 break;
339 if (status < 0)
340 free_recovery_list(head);
341 return status;
344 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
346 int type;
347 struct ocfs2_quota_recovery *rec;
349 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
350 if (!rec)
351 return NULL;
352 for (type = 0; type < MAXQUOTAS; type++)
353 INIT_LIST_HEAD(&(rec->r_list[type]));
354 return rec;
357 /* Load information we need for quota recovery into memory */
358 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
359 struct ocfs2_super *osb,
360 int slot_num)
362 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
363 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
364 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
365 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
366 struct super_block *sb = osb->sb;
367 struct ocfs2_local_disk_dqinfo *ldinfo;
368 struct inode *lqinode;
369 struct buffer_head *bh;
370 int type;
371 int status = 0;
372 struct ocfs2_quota_recovery *rec;
374 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
375 rec = ocfs2_alloc_quota_recovery();
376 if (!rec)
377 return ERR_PTR(-ENOMEM);
378 /* First init... */
380 for (type = 0; type < MAXQUOTAS; type++) {
381 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
382 continue;
383 /* At this point, journal of the slot is already replayed so
384 * we can trust metadata and data of the quota file */
385 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
386 if (!lqinode) {
387 status = -ENOENT;
388 goto out;
390 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
391 OCFS2_META_LOCK_RECOVERY);
392 if (status < 0) {
393 mlog_errno(status);
394 goto out_put;
396 /* Now read local header */
397 bh = NULL;
398 status = ocfs2_read_quota_block(lqinode, 0, &bh);
399 if (status) {
400 mlog_errno(status);
401 mlog(ML_ERROR, "failed to read quota file info header "
402 "(slot=%d type=%d)\n", slot_num, type);
403 goto out_lock;
405 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
406 OCFS2_LOCAL_INFO_OFF);
407 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
408 &rec->r_list[type]);
409 brelse(bh);
410 out_lock:
411 ocfs2_inode_unlock(lqinode, 1);
412 out_put:
413 iput(lqinode);
414 if (status < 0)
415 break;
417 out:
418 if (status < 0) {
419 ocfs2_free_quota_recovery(rec);
420 rec = ERR_PTR(status);
422 return rec;
425 /* Sync changes in local quota file into global quota file and
426 * reinitialize local quota file.
427 * The function expects local quota file to be already locked and
428 * dqonoff_mutex locked. */
429 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
430 int type,
431 struct ocfs2_quota_recovery *rec)
433 struct super_block *sb = lqinode->i_sb;
434 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
435 struct ocfs2_local_disk_chunk *dchunk;
436 struct ocfs2_local_disk_dqblk *dqblk;
437 struct dquot *dquot;
438 handle_t *handle;
439 struct buffer_head *hbh = NULL, *qbh = NULL;
440 int status = 0;
441 int bit, chunk;
442 struct ocfs2_recovery_chunk *rchunk, *next;
443 qsize_t spacechange, inodechange;
445 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
447 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
448 chunk = rchunk->rc_chunk;
449 hbh = NULL;
450 status = ocfs2_read_quota_block(lqinode,
451 ol_quota_chunk_block(sb, chunk),
452 &hbh);
453 if (status) {
454 mlog_errno(status);
455 break;
457 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
458 for_each_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
459 qbh = NULL;
460 status = ocfs2_read_quota_block(lqinode,
461 ol_dqblk_block(sb, chunk, bit),
462 &qbh);
463 if (status) {
464 mlog_errno(status);
465 break;
467 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
468 ol_dqblk_block_off(sb, chunk, bit));
469 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
470 if (!dquot) {
471 status = -EIO;
472 mlog(ML_ERROR, "Failed to get quota structure "
473 "for id %u, type %d. Cannot finish quota "
474 "file recovery.\n",
475 (unsigned)le64_to_cpu(dqblk->dqb_id),
476 type);
477 goto out_put_bh;
479 status = ocfs2_lock_global_qf(oinfo, 1);
480 if (status < 0) {
481 mlog_errno(status);
482 goto out_put_dquot;
485 handle = ocfs2_start_trans(OCFS2_SB(sb),
486 OCFS2_QSYNC_CREDITS);
487 if (IS_ERR(handle)) {
488 status = PTR_ERR(handle);
489 mlog_errno(status);
490 goto out_drop_lock;
492 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
493 spin_lock(&dq_data_lock);
494 /* Add usage from quota entry into quota changes
495 * of our node. Auxiliary variables are important
496 * due to signedness */
497 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
498 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
499 dquot->dq_dqb.dqb_curspace += spacechange;
500 dquot->dq_dqb.dqb_curinodes += inodechange;
501 spin_unlock(&dq_data_lock);
502 /* We want to drop reference held by the crashed
503 * node. Since we have our own reference we know
504 * global structure actually won't be freed. */
505 status = ocfs2_global_release_dquot(dquot);
506 if (status < 0) {
507 mlog_errno(status);
508 goto out_commit;
510 /* Release local quota file entry */
511 status = ocfs2_journal_access_dq(handle, lqinode,
512 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
513 if (status < 0) {
514 mlog_errno(status);
515 goto out_commit;
517 lock_buffer(qbh);
518 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
519 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
520 le32_add_cpu(&dchunk->dqc_free, 1);
521 unlock_buffer(qbh);
522 status = ocfs2_journal_dirty(handle, qbh);
523 if (status < 0)
524 mlog_errno(status);
525 out_commit:
526 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
527 ocfs2_commit_trans(OCFS2_SB(sb), handle);
528 out_drop_lock:
529 ocfs2_unlock_global_qf(oinfo, 1);
530 out_put_dquot:
531 dqput(dquot);
532 out_put_bh:
533 brelse(qbh);
534 if (status < 0)
535 break;
537 brelse(hbh);
538 list_del(&rchunk->rc_list);
539 kfree(rchunk->rc_bitmap);
540 kfree(rchunk);
541 if (status < 0)
542 break;
544 if (status < 0)
545 free_recovery_list(&(rec->r_list[type]));
546 mlog_exit(status);
547 return status;
550 /* Recover local quota files for given node different from us */
551 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
552 struct ocfs2_quota_recovery *rec,
553 int slot_num)
555 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
556 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
557 struct super_block *sb = osb->sb;
558 struct ocfs2_local_disk_dqinfo *ldinfo;
559 struct buffer_head *bh;
560 handle_t *handle;
561 int type;
562 int status = 0;
563 struct inode *lqinode;
564 unsigned int flags;
566 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
567 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
568 for (type = 0; type < MAXQUOTAS; type++) {
569 if (list_empty(&(rec->r_list[type])))
570 continue;
571 mlog(0, "Recovering quota in slot %d\n", slot_num);
572 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
573 if (!lqinode) {
574 status = -ENOENT;
575 goto out;
577 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
578 OCFS2_META_LOCK_NOQUEUE);
579 /* Someone else is holding the lock? Then he must be
580 * doing the recovery. Just skip the file... */
581 if (status == -EAGAIN) {
582 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
583 "because quota file is locked.\n", slot_num);
584 status = 0;
585 goto out_put;
586 } else if (status < 0) {
587 mlog_errno(status);
588 goto out_put;
590 /* Now read local header */
591 bh = NULL;
592 status = ocfs2_read_quota_block(lqinode, 0, &bh);
593 if (status) {
594 mlog_errno(status);
595 mlog(ML_ERROR, "failed to read quota file info header "
596 "(slot=%d type=%d)\n", slot_num, type);
597 goto out_lock;
599 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
600 OCFS2_LOCAL_INFO_OFF);
601 /* Is recovery still needed? */
602 flags = le32_to_cpu(ldinfo->dqi_flags);
603 if (!(flags & OLQF_CLEAN))
604 status = ocfs2_recover_local_quota_file(lqinode,
605 type,
606 rec);
607 /* We don't want to mark file as clean when it is actually
608 * active */
609 if (slot_num == osb->slot_num)
610 goto out_bh;
611 /* Mark quota file as clean if we are recovering quota file of
612 * some other node. */
613 handle = ocfs2_start_trans(osb, 1);
614 if (IS_ERR(handle)) {
615 status = PTR_ERR(handle);
616 mlog_errno(status);
617 goto out_bh;
619 status = ocfs2_journal_access_dq(handle, lqinode, bh,
620 OCFS2_JOURNAL_ACCESS_WRITE);
621 if (status < 0) {
622 mlog_errno(status);
623 goto out_trans;
625 lock_buffer(bh);
626 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
627 unlock_buffer(bh);
628 status = ocfs2_journal_dirty(handle, bh);
629 if (status < 0)
630 mlog_errno(status);
631 out_trans:
632 ocfs2_commit_trans(osb, handle);
633 out_bh:
634 brelse(bh);
635 out_lock:
636 ocfs2_inode_unlock(lqinode, 1);
637 out_put:
638 iput(lqinode);
639 if (status < 0)
640 break;
642 out:
643 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
644 kfree(rec);
645 return status;
648 /* Read information header from quota file */
649 static int ocfs2_local_read_info(struct super_block *sb, int type)
651 struct ocfs2_local_disk_dqinfo *ldinfo;
652 struct mem_dqinfo *info = sb_dqinfo(sb, type);
653 struct ocfs2_mem_dqinfo *oinfo;
654 struct inode *lqinode = sb_dqopt(sb)->files[type];
655 int status;
656 struct buffer_head *bh = NULL;
657 struct ocfs2_quota_recovery *rec;
658 int locked = 0;
660 /* We don't need the lock and we have to acquire quota file locks
661 * which will later depend on this lock */
662 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
663 info->dqi_maxblimit = 0x7fffffffffffffffLL;
664 info->dqi_maxilimit = 0x7fffffffffffffffLL;
665 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
666 if (!oinfo) {
667 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
668 " info.");
669 goto out_err;
671 info->dqi_priv = oinfo;
672 oinfo->dqi_type = type;
673 INIT_LIST_HEAD(&oinfo->dqi_chunk);
674 oinfo->dqi_rec = NULL;
675 oinfo->dqi_lqi_bh = NULL;
676 oinfo->dqi_ibh = NULL;
678 status = ocfs2_global_read_info(sb, type);
679 if (status < 0)
680 goto out_err;
682 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
683 if (status < 0) {
684 mlog_errno(status);
685 goto out_err;
687 locked = 1;
689 /* Now read local header */
690 status = ocfs2_read_quota_block(lqinode, 0, &bh);
691 if (status) {
692 mlog_errno(status);
693 mlog(ML_ERROR, "failed to read quota file info header "
694 "(type=%d)\n", type);
695 goto out_err;
697 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
698 OCFS2_LOCAL_INFO_OFF);
699 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
700 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
701 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
702 oinfo->dqi_ibh = bh;
704 /* We crashed when using local quota file? */
705 if (!(info->dqi_flags & OLQF_CLEAN)) {
706 rec = OCFS2_SB(sb)->quota_rec;
707 if (!rec) {
708 rec = ocfs2_alloc_quota_recovery();
709 if (!rec) {
710 status = -ENOMEM;
711 mlog_errno(status);
712 goto out_err;
714 OCFS2_SB(sb)->quota_rec = rec;
717 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
718 &rec->r_list[type]);
719 if (status < 0) {
720 mlog_errno(status);
721 goto out_err;
725 status = ocfs2_load_local_quota_bitmaps(lqinode,
726 ldinfo,
727 &oinfo->dqi_chunk);
728 if (status < 0) {
729 mlog_errno(status);
730 goto out_err;
733 /* Now mark quota file as used */
734 info->dqi_flags &= ~OLQF_CLEAN;
735 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
736 if (status < 0) {
737 mlog_errno(status);
738 goto out_err;
741 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
742 return 0;
743 out_err:
744 if (oinfo) {
745 iput(oinfo->dqi_gqinode);
746 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
747 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
748 brelse(oinfo->dqi_lqi_bh);
749 if (locked)
750 ocfs2_inode_unlock(lqinode, 1);
751 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
752 kfree(oinfo);
754 brelse(bh);
755 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
756 return -1;
759 /* Write local info to quota file */
760 static int ocfs2_local_write_info(struct super_block *sb, int type)
762 struct mem_dqinfo *info = sb_dqinfo(sb, type);
763 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
764 ->dqi_ibh;
765 int status;
767 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
768 info);
769 if (status < 0) {
770 mlog_errno(status);
771 return -1;
774 return 0;
777 /* Release info from memory */
778 static int ocfs2_local_free_info(struct super_block *sb, int type)
780 struct mem_dqinfo *info = sb_dqinfo(sb, type);
781 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
782 struct ocfs2_quota_chunk *chunk;
783 struct ocfs2_local_disk_chunk *dchunk;
784 int mark_clean = 1, len;
785 int status;
787 /* At this point we know there are no more dquots and thus
788 * even if there's some sync in the pdflush queue, it won't
789 * find any dquots and return without doing anything */
790 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
791 iput(oinfo->dqi_gqinode);
792 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
793 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
794 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
795 dchunk = (struct ocfs2_local_disk_chunk *)
796 (chunk->qc_headerbh->b_data);
797 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
798 len = ol_chunk_entries(sb);
799 } else {
800 len = (oinfo->dqi_blocks -
801 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
802 * ol_quota_entries_per_block(sb);
804 /* Not all entries free? Bug! */
805 if (le32_to_cpu(dchunk->dqc_free) != len) {
806 mlog(ML_ERROR, "releasing quota file with used "
807 "entries (type=%d)\n", type);
808 mark_clean = 0;
811 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
813 /* dqonoff_mutex protects us against racing with recovery thread... */
814 if (oinfo->dqi_rec) {
815 ocfs2_free_quota_recovery(oinfo->dqi_rec);
816 mark_clean = 0;
819 if (!mark_clean)
820 goto out;
822 /* Mark local file as clean */
823 info->dqi_flags |= OLQF_CLEAN;
824 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
825 oinfo->dqi_ibh,
826 olq_update_info,
827 info);
828 if (status < 0) {
829 mlog_errno(status);
830 goto out;
833 out:
834 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
835 brelse(oinfo->dqi_ibh);
836 brelse(oinfo->dqi_lqi_bh);
837 kfree(oinfo);
838 return 0;
841 static void olq_set_dquot(struct buffer_head *bh, void *private)
843 struct ocfs2_dquot *od = private;
844 struct ocfs2_local_disk_dqblk *dqblk;
845 struct super_block *sb = od->dq_dquot.dq_sb;
847 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
848 + ol_dqblk_block_offset(sb, od->dq_local_off));
850 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
851 spin_lock(&dq_data_lock);
852 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
853 od->dq_origspace);
854 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
855 od->dq_originodes);
856 spin_unlock(&dq_data_lock);
857 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
858 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
859 (long long)le64_to_cpu(dqblk->dqb_inodemod));
862 /* Write dquot to local quota file */
863 static int ocfs2_local_write_dquot(struct dquot *dquot)
865 struct super_block *sb = dquot->dq_sb;
866 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
867 struct buffer_head *bh = NULL;
868 int status;
870 status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
871 ol_dqblk_file_block(sb, od->dq_local_off),
872 &bh);
873 if (status) {
874 mlog_errno(status);
875 goto out;
877 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
878 olq_set_dquot, od);
879 if (status < 0) {
880 mlog_errno(status);
881 goto out;
883 out:
884 brelse(bh);
885 return status;
888 /* Find free entry in local quota file */
889 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
890 int type,
891 int *offset)
893 struct mem_dqinfo *info = sb_dqinfo(sb, type);
894 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
895 struct ocfs2_quota_chunk *chunk;
896 struct ocfs2_local_disk_chunk *dchunk;
897 int found = 0, len;
899 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
900 dchunk = (struct ocfs2_local_disk_chunk *)
901 chunk->qc_headerbh->b_data;
902 if (le32_to_cpu(dchunk->dqc_free) > 0) {
903 found = 1;
904 break;
907 if (!found)
908 return NULL;
910 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
911 len = ol_chunk_entries(sb);
912 } else {
913 len = (oinfo->dqi_blocks -
914 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
915 * ol_quota_entries_per_block(sb);
918 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
919 /* We failed? */
920 if (found == len) {
921 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
922 " entries free (type=%d)\n", chunk->qc_num,
923 le32_to_cpu(dchunk->dqc_free), type);
924 return ERR_PTR(-EIO);
926 *offset = found;
927 return chunk;
930 /* Add new chunk to the local quota file */
931 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
932 struct super_block *sb,
933 int type,
934 int *offset)
936 struct mem_dqinfo *info = sb_dqinfo(sb, type);
937 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
938 struct inode *lqinode = sb_dqopt(sb)->files[type];
939 struct ocfs2_quota_chunk *chunk = NULL;
940 struct ocfs2_local_disk_chunk *dchunk;
941 int status;
942 handle_t *handle;
943 struct buffer_head *bh = NULL;
944 u64 p_blkno;
946 /* We are protected by dqio_sem so no locking needed */
947 status = ocfs2_extend_no_holes(lqinode,
948 lqinode->i_size + 2 * sb->s_blocksize,
949 lqinode->i_size);
950 if (status < 0) {
951 mlog_errno(status);
952 goto out;
954 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
955 lqinode->i_size + 2 * sb->s_blocksize);
956 if (status < 0) {
957 mlog_errno(status);
958 goto out;
961 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
962 if (!chunk) {
963 status = -ENOMEM;
964 mlog_errno(status);
965 goto out;
968 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
969 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
970 &p_blkno, NULL, NULL);
971 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
972 if (status < 0) {
973 mlog_errno(status);
974 goto out;
976 bh = sb_getblk(sb, p_blkno);
977 if (!bh) {
978 status = -ENOMEM;
979 mlog_errno(status);
980 goto out;
982 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
984 handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
985 if (IS_ERR(handle)) {
986 status = PTR_ERR(handle);
987 mlog_errno(status);
988 goto out;
991 status = ocfs2_journal_access_dq(handle, lqinode, bh,
992 OCFS2_JOURNAL_ACCESS_WRITE);
993 if (status < 0) {
994 mlog_errno(status);
995 goto out_trans;
997 lock_buffer(bh);
998 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
999 memset(dchunk->dqc_bitmap, 0,
1000 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1001 OCFS2_QBLK_RESERVED_SPACE);
1002 set_buffer_uptodate(bh);
1003 unlock_buffer(bh);
1004 status = ocfs2_journal_dirty(handle, bh);
1005 if (status < 0) {
1006 mlog_errno(status);
1007 goto out_trans;
1010 oinfo->dqi_blocks += 2;
1011 oinfo->dqi_chunks++;
1012 status = ocfs2_local_write_info(sb, type);
1013 if (status < 0) {
1014 mlog_errno(status);
1015 goto out_trans;
1017 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1018 if (status < 0) {
1019 mlog_errno(status);
1020 goto out;
1023 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1024 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1025 struct ocfs2_quota_chunk,
1026 qc_chunk)->qc_num + 1;
1027 chunk->qc_headerbh = bh;
1028 *offset = 0;
1029 return chunk;
1030 out_trans:
1031 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1032 out:
1033 brelse(bh);
1034 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1035 return ERR_PTR(status);
1038 /* Find free entry in local quota file */
1039 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1040 struct super_block *sb,
1041 int type,
1042 int *offset)
1044 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1045 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1046 struct ocfs2_quota_chunk *chunk;
1047 struct inode *lqinode = sb_dqopt(sb)->files[type];
1048 struct ocfs2_local_disk_chunk *dchunk;
1049 int epb = ol_quota_entries_per_block(sb);
1050 unsigned int chunk_blocks;
1051 int status;
1052 handle_t *handle;
1054 if (list_empty(&oinfo->dqi_chunk))
1055 return ocfs2_local_quota_add_chunk(sb, type, offset);
1056 /* Is the last chunk full? */
1057 chunk = list_entry(oinfo->dqi_chunk.prev,
1058 struct ocfs2_quota_chunk, qc_chunk);
1059 chunk_blocks = oinfo->dqi_blocks -
1060 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1061 if (ol_chunk_blocks(sb) == chunk_blocks)
1062 return ocfs2_local_quota_add_chunk(sb, type, offset);
1064 /* We are protected by dqio_sem so no locking needed */
1065 status = ocfs2_extend_no_holes(lqinode,
1066 lqinode->i_size + sb->s_blocksize,
1067 lqinode->i_size);
1068 if (status < 0) {
1069 mlog_errno(status);
1070 goto out;
1072 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1073 lqinode->i_size + sb->s_blocksize);
1074 if (status < 0) {
1075 mlog_errno(status);
1076 goto out;
1078 handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
1079 if (IS_ERR(handle)) {
1080 status = PTR_ERR(handle);
1081 mlog_errno(status);
1082 goto out;
1084 status = ocfs2_journal_access_dq(handle, lqinode, chunk->qc_headerbh,
1085 OCFS2_JOURNAL_ACCESS_WRITE);
1086 if (status < 0) {
1087 mlog_errno(status);
1088 goto out_trans;
1091 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1092 lock_buffer(chunk->qc_headerbh);
1093 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1094 unlock_buffer(chunk->qc_headerbh);
1095 status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1096 if (status < 0) {
1097 mlog_errno(status);
1098 goto out_trans;
1100 oinfo->dqi_blocks++;
1101 status = ocfs2_local_write_info(sb, type);
1102 if (status < 0) {
1103 mlog_errno(status);
1104 goto out_trans;
1107 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1108 if (status < 0) {
1109 mlog_errno(status);
1110 goto out;
1112 *offset = chunk_blocks * epb;
1113 return chunk;
1114 out_trans:
1115 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1116 out:
1117 return ERR_PTR(status);
1120 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1122 int *offset = private;
1123 struct ocfs2_local_disk_chunk *dchunk;
1125 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1126 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
1127 le32_add_cpu(&dchunk->dqc_free, -1);
1130 /* Create dquot in the local file for given id */
1131 static int ocfs2_create_local_dquot(struct dquot *dquot)
1133 struct super_block *sb = dquot->dq_sb;
1134 int type = dquot->dq_type;
1135 struct inode *lqinode = sb_dqopt(sb)->files[type];
1136 struct ocfs2_quota_chunk *chunk;
1137 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1138 int offset;
1139 int status;
1141 chunk = ocfs2_find_free_entry(sb, type, &offset);
1142 if (!chunk) {
1143 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1144 if (IS_ERR(chunk))
1145 return PTR_ERR(chunk);
1146 } else if (IS_ERR(chunk)) {
1147 return PTR_ERR(chunk);
1149 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1150 od->dq_chunk = chunk;
1152 /* Initialize dquot structure on disk */
1153 status = ocfs2_local_write_dquot(dquot);
1154 if (status < 0) {
1155 mlog_errno(status);
1156 goto out;
1159 /* Mark structure as allocated */
1160 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1161 &offset);
1162 if (status < 0) {
1163 mlog_errno(status);
1164 goto out;
1166 out:
1167 return status;
1170 /* Create entry in local file for dquot, load data from the global file */
1171 static int ocfs2_local_read_dquot(struct dquot *dquot)
1173 int status;
1175 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
1177 status = ocfs2_global_read_dquot(dquot);
1178 if (status < 0) {
1179 mlog_errno(status);
1180 goto out_err;
1183 /* Now create entry in the local quota file */
1184 status = ocfs2_create_local_dquot(dquot);
1185 if (status < 0) {
1186 mlog_errno(status);
1187 goto out_err;
1189 mlog_exit(0);
1190 return 0;
1191 out_err:
1192 mlog_exit(status);
1193 return status;
1196 /* Release dquot structure from local quota file. ocfs2_release_dquot() has
1197 * already started a transaction and obtained exclusive lock for global
1198 * quota file. */
1199 static int ocfs2_local_release_dquot(struct dquot *dquot)
1201 int status;
1202 int type = dquot->dq_type;
1203 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1204 struct super_block *sb = dquot->dq_sb;
1205 struct ocfs2_local_disk_chunk *dchunk;
1206 int offset;
1207 handle_t *handle = journal_current_handle();
1209 BUG_ON(!handle);
1210 /* First write all local changes to global file */
1211 status = ocfs2_global_release_dquot(dquot);
1212 if (status < 0) {
1213 mlog_errno(status);
1214 goto out;
1217 status = ocfs2_journal_access_dq(handle, sb_dqopt(sb)->files[type],
1218 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1219 if (status < 0) {
1220 mlog_errno(status);
1221 goto out;
1223 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1224 od->dq_local_off);
1225 dchunk = (struct ocfs2_local_disk_chunk *)
1226 (od->dq_chunk->qc_headerbh->b_data);
1227 /* Mark structure as freed */
1228 lock_buffer(od->dq_chunk->qc_headerbh);
1229 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1230 le32_add_cpu(&dchunk->dqc_free, 1);
1231 unlock_buffer(od->dq_chunk->qc_headerbh);
1232 status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1233 if (status < 0) {
1234 mlog_errno(status);
1235 goto out;
1237 status = 0;
1238 out:
1239 /* Clear the read bit so that next time someone uses this
1240 * dquot he reads fresh info from disk and allocates local
1241 * dquot structure */
1242 clear_bit(DQ_READ_B, &dquot->dq_flags);
1243 return status;
1246 static struct quota_format_ops ocfs2_format_ops = {
1247 .check_quota_file = ocfs2_local_check_quota_file,
1248 .read_file_info = ocfs2_local_read_info,
1249 .write_file_info = ocfs2_global_write_info,
1250 .free_file_info = ocfs2_local_free_info,
1251 .read_dqblk = ocfs2_local_read_dquot,
1252 .commit_dqblk = ocfs2_local_write_dquot,
1253 .release_dqblk = ocfs2_local_release_dquot,
1256 struct quota_format_type ocfs2_quota_format = {
1257 .qf_fmt_id = QFMT_OCFS2,
1258 .qf_ops = &ocfs2_format_ops,
1259 .qf_owner = THIS_MODULE