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