Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.git] / fs / ocfs2 / quota_local.c
1 /*
2  *  Implementation of operations over local quota file
3  */
4
5 #include <linux/fs.h>
6 #include <linux/slab.h>
7 #include <linux/quota.h>
8 #include <linux/quotaops.h>
9 #include <linux/module.h>
10
11 #include <cluster/masklog.h>
12
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"
23 #include "uptodate.h"
24 #include "super.h"
25 #include "ocfs2_trace.h"
26
27 /* Number of local quota structures per block */
28 static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
29 {
30         return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
31                 sizeof(struct ocfs2_local_disk_dqblk));
32 }
33
34 /* Number of blocks with entries in one chunk */
35 static inline unsigned int ol_chunk_blocks(struct super_block *sb)
36 {
37         return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
38                  OCFS2_QBLK_RESERVED_SPACE) << 3) /
39                ol_quota_entries_per_block(sb);
40 }
41
42 /* Number of entries in a chunk bitmap */
43 static unsigned int ol_chunk_entries(struct super_block *sb)
44 {
45         return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
46 }
47
48 /* Offset of the chunk in quota file */
49 static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
50 {
51         /* 1 block for local quota file info, 1 block per chunk for chunk info */
52         return 1 + (ol_chunk_blocks(sb) + 1) * c;
53 }
54
55 static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
56 {
57         int epb = ol_quota_entries_per_block(sb);
58
59         return ol_quota_chunk_block(sb, c) + 1 + off / epb;
60 }
61
62 static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
63 {
64         int epb = ol_quota_entries_per_block(sb);
65
66         return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
67 }
68
69 /* Offset of the dquot structure in the quota file */
70 static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
71 {
72         return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
73                ol_dqblk_block_off(sb, c, off);
74 }
75
76 /* Compute block number from given offset */
77 static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
78 {
79         return off >> sb->s_blocksize_bits;
80 }
81
82 static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
83 {
84         return off & ((1 << sb->s_blocksize_bits) - 1);
85 }
86
87 /* Compute offset in the chunk of a structure with the given offset */
88 static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
89 {
90         int epb = ol_quota_entries_per_block(sb);
91
92         return ((off >> sb->s_blocksize_bits) -
93                         ol_quota_chunk_block(sb, c) - 1) * epb
94                + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
95                  sizeof(struct ocfs2_local_disk_dqblk);
96 }
97
98 /* Write bufferhead into the fs */
99 static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
100                 void (*modify)(struct buffer_head *, void *), void *private)
101 {
102         struct super_block *sb = inode->i_sb;
103         handle_t *handle;
104         int status;
105
106         handle = ocfs2_start_trans(OCFS2_SB(sb),
107                                    OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
108         if (IS_ERR(handle)) {
109                 status = PTR_ERR(handle);
110                 mlog_errno(status);
111                 return status;
112         }
113         status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
114                                          OCFS2_JOURNAL_ACCESS_WRITE);
115         if (status < 0) {
116                 mlog_errno(status);
117                 ocfs2_commit_trans(OCFS2_SB(sb), handle);
118                 return status;
119         }
120         lock_buffer(bh);
121         modify(bh, private);
122         unlock_buffer(bh);
123         ocfs2_journal_dirty(handle, bh);
124
125         status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
126         if (status < 0) {
127                 mlog_errno(status);
128                 return status;
129         }
130         return 0;
131 }
132
133 /*
134  * Read quota block from a given logical offset.
135  *
136  * This function acquires ip_alloc_sem and thus it must not be called with a
137  * transaction started.
138  */
139 static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
140                                   struct buffer_head **bh)
141 {
142         int rc = 0;
143         struct buffer_head *tmp = *bh;
144
145         if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
146                 ocfs2_error(inode->i_sb,
147                             "Quota file %llu is probably corrupted! Requested "
148                             "to read block %Lu but file has size only %Lu\n",
149                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
150                             (unsigned long long)v_block,
151                             (unsigned long long)i_size_read(inode));
152                 return -EIO;
153         }
154         rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
155                                     ocfs2_validate_quota_block);
156         if (rc)
157                 mlog_errno(rc);
158
159         /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
160         if (!rc && !*bh)
161                 *bh = tmp;
162
163         return rc;
164 }
165
166 /* Check whether we understand format of quota files */
167 static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
168 {
169         unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
170         unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
171         unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
172         unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
173         unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
174                                         GROUP_QUOTA_SYSTEM_INODE };
175         struct buffer_head *bh = NULL;
176         struct inode *linode = sb_dqopt(sb)->files[type];
177         struct inode *ginode = NULL;
178         struct ocfs2_disk_dqheader *dqhead;
179         int status, ret = 0;
180
181         /* First check whether we understand local quota file */
182         status = ocfs2_read_quota_block(linode, 0, &bh);
183         if (status) {
184                 mlog_errno(status);
185                 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
186                         type);
187                 goto out_err;
188         }
189         dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
190         if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
191                 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
192                         " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
193                         lmagics[type], type);
194                 goto out_err;
195         }
196         if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
197                 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
198                         " type=%d\n", le32_to_cpu(dqhead->dqh_version),
199                         lversions[type], type);
200                 goto out_err;
201         }
202         brelse(bh);
203         bh = NULL;
204
205         /* Next check whether we understand global quota file */
206         ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
207                                                 OCFS2_INVALID_SLOT);
208         if (!ginode) {
209                 mlog(ML_ERROR, "cannot get global quota file inode "
210                                 "(type=%d)\n", type);
211                 goto out_err;
212         }
213         /* Since the header is read only, we don't care about locking */
214         status = ocfs2_read_quota_block(ginode, 0, &bh);
215         if (status) {
216                 mlog_errno(status);
217                 mlog(ML_ERROR, "failed to read global quota file header "
218                                 "(type=%d)\n", type);
219                 goto out_err;
220         }
221         dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
222         if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
223                 mlog(ML_ERROR, "global quota file magic does not match "
224                         "(%u != %u), type=%d\n",
225                         le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
226                 goto out_err;
227         }
228         if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
229                 mlog(ML_ERROR, "global quota file version does not match "
230                         "(%u != %u), type=%d\n",
231                         le32_to_cpu(dqhead->dqh_version), gversions[type],
232                         type);
233                 goto out_err;
234         }
235
236         ret = 1;
237 out_err:
238         brelse(bh);
239         iput(ginode);
240         return ret;
241 }
242
243 /* Release given list of quota file chunks */
244 static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
245 {
246         struct ocfs2_quota_chunk *pos, *next;
247
248         list_for_each_entry_safe(pos, next, head, qc_chunk) {
249                 list_del(&pos->qc_chunk);
250                 brelse(pos->qc_headerbh);
251                 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
252         }
253 }
254
255 /* Load quota bitmaps into memory */
256 static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
257                         struct ocfs2_local_disk_dqinfo *ldinfo,
258                         struct list_head *head)
259 {
260         struct ocfs2_quota_chunk *newchunk;
261         int i, status;
262
263         INIT_LIST_HEAD(head);
264         for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
265                 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
266                 if (!newchunk) {
267                         ocfs2_release_local_quota_bitmaps(head);
268                         return -ENOMEM;
269                 }
270                 newchunk->qc_num = i;
271                 newchunk->qc_headerbh = NULL;
272                 status = ocfs2_read_quota_block(inode,
273                                 ol_quota_chunk_block(inode->i_sb, i),
274                                 &newchunk->qc_headerbh);
275                 if (status) {
276                         mlog_errno(status);
277                         kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
278                         ocfs2_release_local_quota_bitmaps(head);
279                         return status;
280                 }
281                 list_add_tail(&newchunk->qc_chunk, head);
282         }
283         return 0;
284 }
285
286 static void olq_update_info(struct buffer_head *bh, void *private)
287 {
288         struct mem_dqinfo *info = private;
289         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
290         struct ocfs2_local_disk_dqinfo *ldinfo;
291
292         ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
293                                                 OCFS2_LOCAL_INFO_OFF);
294         spin_lock(&dq_data_lock);
295         ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
296         ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
297         ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
298         spin_unlock(&dq_data_lock);
299 }
300
301 static int ocfs2_add_recovery_chunk(struct super_block *sb,
302                                     struct ocfs2_local_disk_chunk *dchunk,
303                                     int chunk,
304                                     struct list_head *head)
305 {
306         struct ocfs2_recovery_chunk *rc;
307
308         rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
309         if (!rc)
310                 return -ENOMEM;
311         rc->rc_chunk = chunk;
312         rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
313         if (!rc->rc_bitmap) {
314                 kfree(rc);
315                 return -ENOMEM;
316         }
317         memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
318                (ol_chunk_entries(sb) + 7) >> 3);
319         list_add_tail(&rc->rc_list, head);
320         return 0;
321 }
322
323 static void free_recovery_list(struct list_head *head)
324 {
325         struct ocfs2_recovery_chunk *next;
326         struct ocfs2_recovery_chunk *rchunk;
327
328         list_for_each_entry_safe(rchunk, next, head, rc_list) {
329                 list_del(&rchunk->rc_list);
330                 kfree(rchunk->rc_bitmap);
331                 kfree(rchunk);
332         }
333 }
334
335 void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
336 {
337         int type;
338
339         for (type = 0; type < MAXQUOTAS; type++)
340                 free_recovery_list(&(rec->r_list[type]));
341         kfree(rec);
342 }
343
344 /* Load entries in our quota file we have to recover*/
345 static int ocfs2_recovery_load_quota(struct inode *lqinode,
346                                      struct ocfs2_local_disk_dqinfo *ldinfo,
347                                      int type,
348                                      struct list_head *head)
349 {
350         struct super_block *sb = lqinode->i_sb;
351         struct buffer_head *hbh;
352         struct ocfs2_local_disk_chunk *dchunk;
353         int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
354         int status = 0;
355
356         for (i = 0; i < chunks; i++) {
357                 hbh = NULL;
358                 status = ocfs2_read_quota_block(lqinode,
359                                                 ol_quota_chunk_block(sb, i),
360                                                 &hbh);
361                 if (status) {
362                         mlog_errno(status);
363                         break;
364                 }
365                 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
366                 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
367                         status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
368                 brelse(hbh);
369                 if (status < 0)
370                         break;
371         }
372         if (status < 0)
373                 free_recovery_list(head);
374         return status;
375 }
376
377 static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
378 {
379         int type;
380         struct ocfs2_quota_recovery *rec;
381
382         rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
383         if (!rec)
384                 return NULL;
385         for (type = 0; type < MAXQUOTAS; type++)
386                 INIT_LIST_HEAD(&(rec->r_list[type]));
387         return rec;
388 }
389
390 /* Load information we need for quota recovery into memory */
391 struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
392                                                 struct ocfs2_super *osb,
393                                                 int slot_num)
394 {
395         unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
396                                             OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
397         unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
398                                         LOCAL_GROUP_QUOTA_SYSTEM_INODE };
399         struct super_block *sb = osb->sb;
400         struct ocfs2_local_disk_dqinfo *ldinfo;
401         struct inode *lqinode;
402         struct buffer_head *bh;
403         int type;
404         int status = 0;
405         struct ocfs2_quota_recovery *rec;
406
407         printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
408                "slot %u\n", osb->dev_str, slot_num);
409
410         rec = ocfs2_alloc_quota_recovery();
411         if (!rec)
412                 return ERR_PTR(-ENOMEM);
413         /* First init... */
414
415         for (type = 0; type < MAXQUOTAS; type++) {
416                 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
417                         continue;
418                 /* At this point, journal of the slot is already replayed so
419                  * we can trust metadata and data of the quota file */
420                 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
421                 if (!lqinode) {
422                         status = -ENOENT;
423                         goto out;
424                 }
425                 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
426                                                OCFS2_META_LOCK_RECOVERY);
427                 if (status < 0) {
428                         mlog_errno(status);
429                         goto out_put;
430                 }
431                 /* Now read local header */
432                 bh = NULL;
433                 status = ocfs2_read_quota_block(lqinode, 0, &bh);
434                 if (status) {
435                         mlog_errno(status);
436                         mlog(ML_ERROR, "failed to read quota file info header "
437                                 "(slot=%d type=%d)\n", slot_num, type);
438                         goto out_lock;
439                 }
440                 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
441                                                         OCFS2_LOCAL_INFO_OFF);
442                 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
443                                                    &rec->r_list[type]);
444                 brelse(bh);
445 out_lock:
446                 ocfs2_inode_unlock(lqinode, 1);
447 out_put:
448                 iput(lqinode);
449                 if (status < 0)
450                         break;
451         }
452 out:
453         if (status < 0) {
454                 ocfs2_free_quota_recovery(rec);
455                 rec = ERR_PTR(status);
456         }
457         return rec;
458 }
459
460 /* Sync changes in local quota file into global quota file and
461  * reinitialize local quota file.
462  * The function expects local quota file to be already locked and
463  * dqonoff_mutex locked. */
464 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
465                                           int type,
466                                           struct ocfs2_quota_recovery *rec)
467 {
468         struct super_block *sb = lqinode->i_sb;
469         struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
470         struct ocfs2_local_disk_chunk *dchunk;
471         struct ocfs2_local_disk_dqblk *dqblk;
472         struct dquot *dquot;
473         handle_t *handle;
474         struct buffer_head *hbh = NULL, *qbh = NULL;
475         int status = 0;
476         int bit, chunk;
477         struct ocfs2_recovery_chunk *rchunk, *next;
478         qsize_t spacechange, inodechange;
479
480         trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
481
482         list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
483                 chunk = rchunk->rc_chunk;
484                 hbh = NULL;
485                 status = ocfs2_read_quota_block(lqinode,
486                                                 ol_quota_chunk_block(sb, chunk),
487                                                 &hbh);
488                 if (status) {
489                         mlog_errno(status);
490                         break;
491                 }
492                 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
493                 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
494                         qbh = NULL;
495                         status = ocfs2_read_quota_block(lqinode,
496                                                 ol_dqblk_block(sb, chunk, bit),
497                                                 &qbh);
498                         if (status) {
499                                 mlog_errno(status);
500                                 break;
501                         }
502                         dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
503                                 ol_dqblk_block_off(sb, chunk, bit));
504                         dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
505                         if (!dquot) {
506                                 status = -EIO;
507                                 mlog(ML_ERROR, "Failed to get quota structure "
508                                      "for id %u, type %d. Cannot finish quota "
509                                      "file recovery.\n",
510                                      (unsigned)le64_to_cpu(dqblk->dqb_id),
511                                      type);
512                                 goto out_put_bh;
513                         }
514                         status = ocfs2_lock_global_qf(oinfo, 1);
515                         if (status < 0) {
516                                 mlog_errno(status);
517                                 goto out_put_dquot;
518                         }
519
520                         handle = ocfs2_start_trans(OCFS2_SB(sb),
521                                                    OCFS2_QSYNC_CREDITS);
522                         if (IS_ERR(handle)) {
523                                 status = PTR_ERR(handle);
524                                 mlog_errno(status);
525                                 goto out_drop_lock;
526                         }
527                         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
528                         spin_lock(&dq_data_lock);
529                         /* Add usage from quota entry into quota changes
530                          * of our node. Auxiliary variables are important
531                          * due to signedness */
532                         spacechange = le64_to_cpu(dqblk->dqb_spacemod);
533                         inodechange = le64_to_cpu(dqblk->dqb_inodemod);
534                         dquot->dq_dqb.dqb_curspace += spacechange;
535                         dquot->dq_dqb.dqb_curinodes += inodechange;
536                         spin_unlock(&dq_data_lock);
537                         /* We want to drop reference held by the crashed
538                          * node. Since we have our own reference we know
539                          * global structure actually won't be freed. */
540                         status = ocfs2_global_release_dquot(dquot);
541                         if (status < 0) {
542                                 mlog_errno(status);
543                                 goto out_commit;
544                         }
545                         /* Release local quota file entry */
546                         status = ocfs2_journal_access_dq(handle,
547                                         INODE_CACHE(lqinode),
548                                         qbh, OCFS2_JOURNAL_ACCESS_WRITE);
549                         if (status < 0) {
550                                 mlog_errno(status);
551                                 goto out_commit;
552                         }
553                         lock_buffer(qbh);
554                         WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
555                         ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
556                         le32_add_cpu(&dchunk->dqc_free, 1);
557                         unlock_buffer(qbh);
558                         ocfs2_journal_dirty(handle, qbh);
559 out_commit:
560                         mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
561                         ocfs2_commit_trans(OCFS2_SB(sb), handle);
562 out_drop_lock:
563                         ocfs2_unlock_global_qf(oinfo, 1);
564 out_put_dquot:
565                         dqput(dquot);
566 out_put_bh:
567                         brelse(qbh);
568                         if (status < 0)
569                                 break;
570                 }
571                 brelse(hbh);
572                 list_del(&rchunk->rc_list);
573                 kfree(rchunk->rc_bitmap);
574                 kfree(rchunk);
575                 if (status < 0)
576                         break;
577         }
578         if (status < 0)
579                 free_recovery_list(&(rec->r_list[type]));
580         if (status)
581                 mlog_errno(status);
582         return status;
583 }
584
585 /* Recover local quota files for given node different from us */
586 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
587                                 struct ocfs2_quota_recovery *rec,
588                                 int slot_num)
589 {
590         unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
591                                         LOCAL_GROUP_QUOTA_SYSTEM_INODE };
592         struct super_block *sb = osb->sb;
593         struct ocfs2_local_disk_dqinfo *ldinfo;
594         struct buffer_head *bh;
595         handle_t *handle;
596         int type;
597         int status = 0;
598         struct inode *lqinode;
599         unsigned int flags;
600
601         printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
602                "slot %u\n", osb->dev_str, slot_num);
603
604         mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
605         for (type = 0; type < MAXQUOTAS; type++) {
606                 if (list_empty(&(rec->r_list[type])))
607                         continue;
608                 trace_ocfs2_finish_quota_recovery(slot_num);
609                 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
610                 if (!lqinode) {
611                         status = -ENOENT;
612                         goto out;
613                 }
614                 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
615                                                        OCFS2_META_LOCK_NOQUEUE);
616                 /* Someone else is holding the lock? Then he must be
617                  * doing the recovery. Just skip the file... */
618                 if (status == -EAGAIN) {
619                         printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
620                                "device (%s) for slot %d because quota file is "
621                                "locked.\n", osb->dev_str, slot_num);
622                         status = 0;
623                         goto out_put;
624                 } else if (status < 0) {
625                         mlog_errno(status);
626                         goto out_put;
627                 }
628                 /* Now read local header */
629                 bh = NULL;
630                 status = ocfs2_read_quota_block(lqinode, 0, &bh);
631                 if (status) {
632                         mlog_errno(status);
633                         mlog(ML_ERROR, "failed to read quota file info header "
634                                 "(slot=%d type=%d)\n", slot_num, type);
635                         goto out_lock;
636                 }
637                 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
638                                                         OCFS2_LOCAL_INFO_OFF);
639                 /* Is recovery still needed? */
640                 flags = le32_to_cpu(ldinfo->dqi_flags);
641                 if (!(flags & OLQF_CLEAN))
642                         status = ocfs2_recover_local_quota_file(lqinode,
643                                                                 type,
644                                                                 rec);
645                 /* We don't want to mark file as clean when it is actually
646                  * active */
647                 if (slot_num == osb->slot_num)
648                         goto out_bh;
649                 /* Mark quota file as clean if we are recovering quota file of
650                  * some other node. */
651                 handle = ocfs2_start_trans(osb,
652                                            OCFS2_LOCAL_QINFO_WRITE_CREDITS);
653                 if (IS_ERR(handle)) {
654                         status = PTR_ERR(handle);
655                         mlog_errno(status);
656                         goto out_bh;
657                 }
658                 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
659                                                  bh,
660                                                  OCFS2_JOURNAL_ACCESS_WRITE);
661                 if (status < 0) {
662                         mlog_errno(status);
663                         goto out_trans;
664                 }
665                 lock_buffer(bh);
666                 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
667                 unlock_buffer(bh);
668                 ocfs2_journal_dirty(handle, bh);
669 out_trans:
670                 ocfs2_commit_trans(osb, handle);
671 out_bh:
672                 brelse(bh);
673 out_lock:
674                 ocfs2_inode_unlock(lqinode, 1);
675 out_put:
676                 iput(lqinode);
677                 if (status < 0)
678                         break;
679         }
680 out:
681         mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
682         kfree(rec);
683         return status;
684 }
685
686 /* Read information header from quota file */
687 static int ocfs2_local_read_info(struct super_block *sb, int type)
688 {
689         struct ocfs2_local_disk_dqinfo *ldinfo;
690         struct mem_dqinfo *info = sb_dqinfo(sb, type);
691         struct ocfs2_mem_dqinfo *oinfo;
692         struct inode *lqinode = sb_dqopt(sb)->files[type];
693         int status;
694         struct buffer_head *bh = NULL;
695         struct ocfs2_quota_recovery *rec;
696         int locked = 0;
697
698         /* We don't need the lock and we have to acquire quota file locks
699          * which will later depend on this lock */
700         mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
701         info->dqi_maxblimit = 0x7fffffffffffffffLL;
702         info->dqi_maxilimit = 0x7fffffffffffffffLL;
703         oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
704         if (!oinfo) {
705                 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
706                                " info.");
707                 goto out_err;
708         }
709         info->dqi_priv = oinfo;
710         oinfo->dqi_type = type;
711         INIT_LIST_HEAD(&oinfo->dqi_chunk);
712         oinfo->dqi_rec = NULL;
713         oinfo->dqi_lqi_bh = NULL;
714         oinfo->dqi_libh = NULL;
715
716         status = ocfs2_global_read_info(sb, type);
717         if (status < 0)
718                 goto out_err;
719
720         status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
721         if (status < 0) {
722                 mlog_errno(status);
723                 goto out_err;
724         }
725         locked = 1;
726
727         /* Now read local header */
728         status = ocfs2_read_quota_block(lqinode, 0, &bh);
729         if (status) {
730                 mlog_errno(status);
731                 mlog(ML_ERROR, "failed to read quota file info header "
732                         "(type=%d)\n", type);
733                 goto out_err;
734         }
735         ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
736                                                 OCFS2_LOCAL_INFO_OFF);
737         info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
738         oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
739         oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
740         oinfo->dqi_libh = bh;
741
742         /* We crashed when using local quota file? */
743         if (!(info->dqi_flags & OLQF_CLEAN)) {
744                 rec = OCFS2_SB(sb)->quota_rec;
745                 if (!rec) {
746                         rec = ocfs2_alloc_quota_recovery();
747                         if (!rec) {
748                                 status = -ENOMEM;
749                                 mlog_errno(status);
750                                 goto out_err;
751                         }
752                         OCFS2_SB(sb)->quota_rec = rec;
753                 }
754
755                 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
756                                                    &rec->r_list[type]);
757                 if (status < 0) {
758                         mlog_errno(status);
759                         goto out_err;
760                 }
761         }
762
763         status = ocfs2_load_local_quota_bitmaps(lqinode,
764                                                 ldinfo,
765                                                 &oinfo->dqi_chunk);
766         if (status < 0) {
767                 mlog_errno(status);
768                 goto out_err;
769         }
770
771         /* Now mark quota file as used */
772         info->dqi_flags &= ~OLQF_CLEAN;
773         status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
774         if (status < 0) {
775                 mlog_errno(status);
776                 goto out_err;
777         }
778
779         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
780         return 0;
781 out_err:
782         if (oinfo) {
783                 iput(oinfo->dqi_gqinode);
784                 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
785                 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
786                 brelse(oinfo->dqi_lqi_bh);
787                 if (locked)
788                         ocfs2_inode_unlock(lqinode, 1);
789                 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
790                 kfree(oinfo);
791         }
792         brelse(bh);
793         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
794         return -1;
795 }
796
797 /* Write local info to quota file */
798 static int ocfs2_local_write_info(struct super_block *sb, int type)
799 {
800         struct mem_dqinfo *info = sb_dqinfo(sb, type);
801         struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
802                                                 ->dqi_libh;
803         int status;
804
805         status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
806                                  info);
807         if (status < 0) {
808                 mlog_errno(status);
809                 return -1;
810         }
811
812         return 0;
813 }
814
815 /* Release info from memory */
816 static int ocfs2_local_free_info(struct super_block *sb, int type)
817 {
818         struct mem_dqinfo *info = sb_dqinfo(sb, type);
819         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
820         struct ocfs2_quota_chunk *chunk;
821         struct ocfs2_local_disk_chunk *dchunk;
822         int mark_clean = 1, len;
823         int status;
824
825         iput(oinfo->dqi_gqinode);
826         ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
827         ocfs2_lock_res_free(&oinfo->dqi_gqlock);
828         list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
829                 dchunk = (struct ocfs2_local_disk_chunk *)
830                                         (chunk->qc_headerbh->b_data);
831                 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
832                         len = ol_chunk_entries(sb);
833                 } else {
834                         len = (oinfo->dqi_blocks -
835                                ol_quota_chunk_block(sb, chunk->qc_num) - 1)
836                               * ol_quota_entries_per_block(sb);
837                 }
838                 /* Not all entries free? Bug! */
839                 if (le32_to_cpu(dchunk->dqc_free) != len) {
840                         mlog(ML_ERROR, "releasing quota file with used "
841                                         "entries (type=%d)\n", type);
842                         mark_clean = 0;
843                 }
844         }
845         ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
846
847         /* dqonoff_mutex protects us against racing with recovery thread... */
848         if (oinfo->dqi_rec) {
849                 ocfs2_free_quota_recovery(oinfo->dqi_rec);
850                 mark_clean = 0;
851         }
852
853         if (!mark_clean)
854                 goto out;
855
856         /* Mark local file as clean */
857         info->dqi_flags |= OLQF_CLEAN;
858         status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
859                                  oinfo->dqi_libh,
860                                  olq_update_info,
861                                  info);
862         if (status < 0) {
863                 mlog_errno(status);
864                 goto out;
865         }
866
867 out:
868         ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
869         brelse(oinfo->dqi_libh);
870         brelse(oinfo->dqi_lqi_bh);
871         kfree(oinfo);
872         return 0;
873 }
874
875 static void olq_set_dquot(struct buffer_head *bh, void *private)
876 {
877         struct ocfs2_dquot *od = private;
878         struct ocfs2_local_disk_dqblk *dqblk;
879         struct super_block *sb = od->dq_dquot.dq_sb;
880
881         dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
882                 + ol_dqblk_block_offset(sb, od->dq_local_off));
883
884         dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
885         spin_lock(&dq_data_lock);
886         dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
887                                           od->dq_origspace);
888         dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
889                                           od->dq_originodes);
890         spin_unlock(&dq_data_lock);
891         trace_olq_set_dquot(
892                 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
893                 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
894                 od->dq_dquot.dq_id);
895 }
896
897 /* Write dquot to local quota file */
898 int ocfs2_local_write_dquot(struct dquot *dquot)
899 {
900         struct super_block *sb = dquot->dq_sb;
901         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
902         struct buffer_head *bh;
903         struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_type];
904         int status;
905
906         status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
907                                              &bh);
908         if (status) {
909                 mlog_errno(status);
910                 goto out;
911         }
912         status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
913         if (status < 0) {
914                 mlog_errno(status);
915                 goto out;
916         }
917 out:
918         brelse(bh);
919         return status;
920 }
921
922 /* Find free entry in local quota file */
923 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
924                                                        int type,
925                                                        int *offset)
926 {
927         struct mem_dqinfo *info = sb_dqinfo(sb, type);
928         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
929         struct ocfs2_quota_chunk *chunk;
930         struct ocfs2_local_disk_chunk *dchunk;
931         int found = 0, len;
932
933         list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
934                 dchunk = (struct ocfs2_local_disk_chunk *)
935                                                 chunk->qc_headerbh->b_data;
936                 if (le32_to_cpu(dchunk->dqc_free) > 0) {
937                         found = 1;
938                         break;
939                 }
940         }
941         if (!found)
942                 return NULL;
943
944         if (chunk->qc_num < oinfo->dqi_chunks - 1) {
945                 len = ol_chunk_entries(sb);
946         } else {
947                 len = (oinfo->dqi_blocks -
948                        ol_quota_chunk_block(sb, chunk->qc_num) - 1)
949                       * ol_quota_entries_per_block(sb);
950         }
951
952         found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
953         /* We failed? */
954         if (found == len) {
955                 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
956                      " entries free (type=%d)\n", chunk->qc_num,
957                      le32_to_cpu(dchunk->dqc_free), type);
958                 return ERR_PTR(-EIO);
959         }
960         *offset = found;
961         return chunk;
962 }
963
964 /* Add new chunk to the local quota file */
965 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
966                                                         struct super_block *sb,
967                                                         int type,
968                                                         int *offset)
969 {
970         struct mem_dqinfo *info = sb_dqinfo(sb, type);
971         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
972         struct inode *lqinode = sb_dqopt(sb)->files[type];
973         struct ocfs2_quota_chunk *chunk = NULL;
974         struct ocfs2_local_disk_chunk *dchunk;
975         int status;
976         handle_t *handle;
977         struct buffer_head *bh = NULL, *dbh = NULL;
978         u64 p_blkno;
979
980         /* We are protected by dqio_sem so no locking needed */
981         status = ocfs2_extend_no_holes(lqinode, NULL,
982                                        lqinode->i_size + 2 * sb->s_blocksize,
983                                        lqinode->i_size);
984         if (status < 0) {
985                 mlog_errno(status);
986                 goto out;
987         }
988         status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
989                                           lqinode->i_size + 2 * sb->s_blocksize);
990         if (status < 0) {
991                 mlog_errno(status);
992                 goto out;
993         }
994
995         chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
996         if (!chunk) {
997                 status = -ENOMEM;
998                 mlog_errno(status);
999                 goto out;
1000         }
1001         /* Local quota info and two new blocks we initialize */
1002         handle = ocfs2_start_trans(OCFS2_SB(sb),
1003                         OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1004                         2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1005         if (IS_ERR(handle)) {
1006                 status = PTR_ERR(handle);
1007                 mlog_errno(status);
1008                 goto out;
1009         }
1010
1011         /* Initialize chunk header */
1012         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1013                                              &p_blkno, NULL, NULL);
1014         if (status < 0) {
1015                 mlog_errno(status);
1016                 goto out_trans;
1017         }
1018         bh = sb_getblk(sb, p_blkno);
1019         if (!bh) {
1020                 status = -ENOMEM;
1021                 mlog_errno(status);
1022                 goto out_trans;
1023         }
1024         dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1025         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1026         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1027                                          OCFS2_JOURNAL_ACCESS_CREATE);
1028         if (status < 0) {
1029                 mlog_errno(status);
1030                 goto out_trans;
1031         }
1032         lock_buffer(bh);
1033         dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
1034         memset(dchunk->dqc_bitmap, 0,
1035                sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1036                OCFS2_QBLK_RESERVED_SPACE);
1037         unlock_buffer(bh);
1038         ocfs2_journal_dirty(handle, bh);
1039
1040         /* Initialize new block with structures */
1041         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1042                                              &p_blkno, NULL, NULL);
1043         if (status < 0) {
1044                 mlog_errno(status);
1045                 goto out_trans;
1046         }
1047         dbh = sb_getblk(sb, p_blkno);
1048         if (!dbh) {
1049                 status = -ENOMEM;
1050                 mlog_errno(status);
1051                 goto out_trans;
1052         }
1053         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1054         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1055                                          OCFS2_JOURNAL_ACCESS_CREATE);
1056         if (status < 0) {
1057                 mlog_errno(status);
1058                 goto out_trans;
1059         }
1060         lock_buffer(dbh);
1061         memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1062         unlock_buffer(dbh);
1063         ocfs2_journal_dirty(handle, dbh);
1064
1065         /* Update local quotafile info */
1066         oinfo->dqi_blocks += 2;
1067         oinfo->dqi_chunks++;
1068         status = ocfs2_local_write_info(sb, type);
1069         if (status < 0) {
1070                 mlog_errno(status);
1071                 goto out_trans;
1072         }
1073         status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1074         if (status < 0) {
1075                 mlog_errno(status);
1076                 goto out;
1077         }
1078
1079         list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1080         chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1081                                    struct ocfs2_quota_chunk,
1082                                    qc_chunk)->qc_num + 1;
1083         chunk->qc_headerbh = bh;
1084         *offset = 0;
1085         return chunk;
1086 out_trans:
1087         ocfs2_commit_trans(OCFS2_SB(sb), handle);
1088 out:
1089         brelse(bh);
1090         brelse(dbh);
1091         kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1092         return ERR_PTR(status);
1093 }
1094
1095 /* Find free entry in local quota file */
1096 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1097                                                        struct super_block *sb,
1098                                                        int type,
1099                                                        int *offset)
1100 {
1101         struct mem_dqinfo *info = sb_dqinfo(sb, type);
1102         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1103         struct ocfs2_quota_chunk *chunk;
1104         struct inode *lqinode = sb_dqopt(sb)->files[type];
1105         struct ocfs2_local_disk_chunk *dchunk;
1106         int epb = ol_quota_entries_per_block(sb);
1107         unsigned int chunk_blocks;
1108         struct buffer_head *bh;
1109         u64 p_blkno;
1110         int status;
1111         handle_t *handle;
1112
1113         if (list_empty(&oinfo->dqi_chunk))
1114                 return ocfs2_local_quota_add_chunk(sb, type, offset);
1115         /* Is the last chunk full? */
1116         chunk = list_entry(oinfo->dqi_chunk.prev,
1117                         struct ocfs2_quota_chunk, qc_chunk);
1118         chunk_blocks = oinfo->dqi_blocks -
1119                         ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1120         if (ol_chunk_blocks(sb) == chunk_blocks)
1121                 return ocfs2_local_quota_add_chunk(sb, type, offset);
1122
1123         /* We are protected by dqio_sem so no locking needed */
1124         status = ocfs2_extend_no_holes(lqinode, NULL,
1125                                        lqinode->i_size + sb->s_blocksize,
1126                                        lqinode->i_size);
1127         if (status < 0) {
1128                 mlog_errno(status);
1129                 goto out;
1130         }
1131         status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1132                                           lqinode->i_size + sb->s_blocksize);
1133         if (status < 0) {
1134                 mlog_errno(status);
1135                 goto out;
1136         }
1137
1138         /* Get buffer from the just added block */
1139         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1140                                              &p_blkno, NULL, NULL);
1141         if (status < 0) {
1142                 mlog_errno(status);
1143                 goto out;
1144         }
1145         bh = sb_getblk(sb, p_blkno);
1146         if (!bh) {
1147                 status = -ENOMEM;
1148                 mlog_errno(status);
1149                 goto out;
1150         }
1151         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1152
1153         /* Local quota info, chunk header and the new block we initialize */
1154         handle = ocfs2_start_trans(OCFS2_SB(sb),
1155                         OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1156                         2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1157         if (IS_ERR(handle)) {
1158                 status = PTR_ERR(handle);
1159                 mlog_errno(status);
1160                 goto out;
1161         }
1162         /* Zero created block */
1163         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1164                                  OCFS2_JOURNAL_ACCESS_CREATE);
1165         if (status < 0) {
1166                 mlog_errno(status);
1167                 goto out_trans;
1168         }
1169         lock_buffer(bh);
1170         memset(bh->b_data, 0, sb->s_blocksize);
1171         unlock_buffer(bh);
1172         ocfs2_journal_dirty(handle, bh);
1173
1174         /* Update chunk header */
1175         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1176                                          chunk->qc_headerbh,
1177                                  OCFS2_JOURNAL_ACCESS_WRITE);
1178         if (status < 0) {
1179                 mlog_errno(status);
1180                 goto out_trans;
1181         }
1182
1183         dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1184         lock_buffer(chunk->qc_headerbh);
1185         le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1186         unlock_buffer(chunk->qc_headerbh);
1187         ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1188
1189         /* Update file header */
1190         oinfo->dqi_blocks++;
1191         status = ocfs2_local_write_info(sb, type);
1192         if (status < 0) {
1193                 mlog_errno(status);
1194                 goto out_trans;
1195         }
1196
1197         status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1198         if (status < 0) {
1199                 mlog_errno(status);
1200                 goto out;
1201         }
1202         *offset = chunk_blocks * epb;
1203         return chunk;
1204 out_trans:
1205         ocfs2_commit_trans(OCFS2_SB(sb), handle);
1206 out:
1207         return ERR_PTR(status);
1208 }
1209
1210 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1211 {
1212         int *offset = private;
1213         struct ocfs2_local_disk_chunk *dchunk;
1214
1215         dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1216         ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
1217         le32_add_cpu(&dchunk->dqc_free, -1);
1218 }
1219
1220 /* Create dquot in the local file for given id */
1221 int ocfs2_create_local_dquot(struct dquot *dquot)
1222 {
1223         struct super_block *sb = dquot->dq_sb;
1224         int type = dquot->dq_type;
1225         struct inode *lqinode = sb_dqopt(sb)->files[type];
1226         struct ocfs2_quota_chunk *chunk;
1227         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1228         int offset;
1229         int status;
1230         u64 pcount;
1231
1232         down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1233         chunk = ocfs2_find_free_entry(sb, type, &offset);
1234         if (!chunk) {
1235                 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1236                 if (IS_ERR(chunk)) {
1237                         status = PTR_ERR(chunk);
1238                         goto out;
1239                 }
1240         } else if (IS_ERR(chunk)) {
1241                 status = PTR_ERR(chunk);
1242                 goto out;
1243         }
1244         od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1245         od->dq_chunk = chunk;
1246         status = ocfs2_extent_map_get_blocks(lqinode,
1247                                      ol_dqblk_block(sb, chunk->qc_num, offset),
1248                                      &od->dq_local_phys_blk,
1249                                      &pcount,
1250                                      NULL);
1251
1252         /* Initialize dquot structure on disk */
1253         status = ocfs2_local_write_dquot(dquot);
1254         if (status < 0) {
1255                 mlog_errno(status);
1256                 goto out;
1257         }
1258
1259         /* Mark structure as allocated */
1260         status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1261                                  &offset);
1262         if (status < 0) {
1263                 mlog_errno(status);
1264                 goto out;
1265         }
1266 out:
1267         up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1268         return status;
1269 }
1270
1271 /*
1272  * Release dquot structure from local quota file. ocfs2_release_dquot() has
1273  * already started a transaction and written all changes to global quota file
1274  */
1275 int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
1276 {
1277         int status;
1278         int type = dquot->dq_type;
1279         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1280         struct super_block *sb = dquot->dq_sb;
1281         struct ocfs2_local_disk_chunk *dchunk;
1282         int offset;
1283
1284         status = ocfs2_journal_access_dq(handle,
1285                         INODE_CACHE(sb_dqopt(sb)->files[type]),
1286                         od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1287         if (status < 0) {
1288                 mlog_errno(status);
1289                 goto out;
1290         }
1291         offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1292                                              od->dq_local_off);
1293         dchunk = (struct ocfs2_local_disk_chunk *)
1294                         (od->dq_chunk->qc_headerbh->b_data);
1295         /* Mark structure as freed */
1296         lock_buffer(od->dq_chunk->qc_headerbh);
1297         ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
1298         le32_add_cpu(&dchunk->dqc_free, 1);
1299         unlock_buffer(od->dq_chunk->qc_headerbh);
1300         ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1301
1302 out:
1303         /* Clear the read bit so that next time someone uses this
1304          * dquot he reads fresh info from disk and allocates local
1305          * dquot structure */
1306         clear_bit(DQ_READ_B, &dquot->dq_flags);
1307         return status;
1308 }
1309
1310 static const struct quota_format_ops ocfs2_format_ops = {
1311         .check_quota_file       = ocfs2_local_check_quota_file,
1312         .read_file_info         = ocfs2_local_read_info,
1313         .write_file_info        = ocfs2_global_write_info,
1314         .free_file_info         = ocfs2_local_free_info,
1315 };
1316
1317 struct quota_format_type ocfs2_quota_format = {
1318         .qf_fmt_id = QFMT_OCFS2,
1319         .qf_ops = &ocfs2_format_ops,
1320         .qf_owner = THIS_MODULE
1321 };