ath10k: prevent pci tx/rx starvation
[cascardo/linux.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[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
170         unsigned int lversions[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
171         unsigned int gmagics[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
172         unsigned int gversions[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
173         unsigned int ino[OCFS2_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 < OCFS2_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 < OCFS2_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[OCFS2_MAXQUOTAS] = {
396                                         OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
397                                         OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
398         unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
399                                               LOCAL_GROUP_QUOTA_SYSTEM_INODE };
400         struct super_block *sb = osb->sb;
401         struct ocfs2_local_disk_dqinfo *ldinfo;
402         struct inode *lqinode;
403         struct buffer_head *bh;
404         int type;
405         int status = 0;
406         struct ocfs2_quota_recovery *rec;
407
408         printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
409                "slot %u\n", osb->dev_str, slot_num);
410
411         rec = ocfs2_alloc_quota_recovery();
412         if (!rec)
413                 return ERR_PTR(-ENOMEM);
414         /* First init... */
415
416         for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
417                 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
418                         continue;
419                 /* At this point, journal of the slot is already replayed so
420                  * we can trust metadata and data of the quota file */
421                 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
422                 if (!lqinode) {
423                         status = -ENOENT;
424                         goto out;
425                 }
426                 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
427                                                OCFS2_META_LOCK_RECOVERY);
428                 if (status < 0) {
429                         mlog_errno(status);
430                         goto out_put;
431                 }
432                 /* Now read local header */
433                 bh = NULL;
434                 status = ocfs2_read_quota_block(lqinode, 0, &bh);
435                 if (status) {
436                         mlog_errno(status);
437                         mlog(ML_ERROR, "failed to read quota file info header "
438                                 "(slot=%d type=%d)\n", slot_num, type);
439                         goto out_lock;
440                 }
441                 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
442                                                         OCFS2_LOCAL_INFO_OFF);
443                 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
444                                                    &rec->r_list[type]);
445                 brelse(bh);
446 out_lock:
447                 ocfs2_inode_unlock(lqinode, 1);
448 out_put:
449                 iput(lqinode);
450                 if (status < 0)
451                         break;
452         }
453 out:
454         if (status < 0) {
455                 ocfs2_free_quota_recovery(rec);
456                 rec = ERR_PTR(status);
457         }
458         return rec;
459 }
460
461 /* Sync changes in local quota file into global quota file and
462  * reinitialize local quota file.
463  * The function expects local quota file to be already locked and
464  * dqonoff_mutex locked. */
465 static int ocfs2_recover_local_quota_file(struct inode *lqinode,
466                                           int type,
467                                           struct ocfs2_quota_recovery *rec)
468 {
469         struct super_block *sb = lqinode->i_sb;
470         struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
471         struct ocfs2_local_disk_chunk *dchunk;
472         struct ocfs2_local_disk_dqblk *dqblk;
473         struct dquot *dquot;
474         handle_t *handle;
475         struct buffer_head *hbh = NULL, *qbh = NULL;
476         int status = 0;
477         int bit, chunk;
478         struct ocfs2_recovery_chunk *rchunk, *next;
479         qsize_t spacechange, inodechange;
480
481         trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
482
483         list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
484                 chunk = rchunk->rc_chunk;
485                 hbh = NULL;
486                 status = ocfs2_read_quota_block(lqinode,
487                                                 ol_quota_chunk_block(sb, chunk),
488                                                 &hbh);
489                 if (status) {
490                         mlog_errno(status);
491                         break;
492                 }
493                 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
494                 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
495                         qbh = NULL;
496                         status = ocfs2_read_quota_block(lqinode,
497                                                 ol_dqblk_block(sb, chunk, bit),
498                                                 &qbh);
499                         if (status) {
500                                 mlog_errno(status);
501                                 break;
502                         }
503                         dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
504                                 ol_dqblk_block_off(sb, chunk, bit));
505                         dquot = dqget(sb,
506                                       make_kqid(&init_user_ns, type,
507                                                 le64_to_cpu(dqblk->dqb_id)));
508                         if (!dquot) {
509                                 status = -EIO;
510                                 mlog(ML_ERROR, "Failed to get quota structure "
511                                      "for id %u, type %d. Cannot finish quota "
512                                      "file recovery.\n",
513                                      (unsigned)le64_to_cpu(dqblk->dqb_id),
514                                      type);
515                                 goto out_put_bh;
516                         }
517                         status = ocfs2_lock_global_qf(oinfo, 1);
518                         if (status < 0) {
519                                 mlog_errno(status);
520                                 goto out_put_dquot;
521                         }
522
523                         handle = ocfs2_start_trans(OCFS2_SB(sb),
524                                                    OCFS2_QSYNC_CREDITS);
525                         if (IS_ERR(handle)) {
526                                 status = PTR_ERR(handle);
527                                 mlog_errno(status);
528                                 goto out_drop_lock;
529                         }
530                         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
531                         spin_lock(&dq_data_lock);
532                         /* Add usage from quota entry into quota changes
533                          * of our node. Auxiliary variables are important
534                          * due to signedness */
535                         spacechange = le64_to_cpu(dqblk->dqb_spacemod);
536                         inodechange = le64_to_cpu(dqblk->dqb_inodemod);
537                         dquot->dq_dqb.dqb_curspace += spacechange;
538                         dquot->dq_dqb.dqb_curinodes += inodechange;
539                         spin_unlock(&dq_data_lock);
540                         /* We want to drop reference held by the crashed
541                          * node. Since we have our own reference we know
542                          * global structure actually won't be freed. */
543                         status = ocfs2_global_release_dquot(dquot);
544                         if (status < 0) {
545                                 mlog_errno(status);
546                                 goto out_commit;
547                         }
548                         /* Release local quota file entry */
549                         status = ocfs2_journal_access_dq(handle,
550                                         INODE_CACHE(lqinode),
551                                         qbh, OCFS2_JOURNAL_ACCESS_WRITE);
552                         if (status < 0) {
553                                 mlog_errno(status);
554                                 goto out_commit;
555                         }
556                         lock_buffer(qbh);
557                         WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
558                         ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
559                         le32_add_cpu(&dchunk->dqc_free, 1);
560                         unlock_buffer(qbh);
561                         ocfs2_journal_dirty(handle, qbh);
562 out_commit:
563                         mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
564                         ocfs2_commit_trans(OCFS2_SB(sb), handle);
565 out_drop_lock:
566                         ocfs2_unlock_global_qf(oinfo, 1);
567 out_put_dquot:
568                         dqput(dquot);
569 out_put_bh:
570                         brelse(qbh);
571                         if (status < 0)
572                                 break;
573                 }
574                 brelse(hbh);
575                 list_del(&rchunk->rc_list);
576                 kfree(rchunk->rc_bitmap);
577                 kfree(rchunk);
578                 if (status < 0)
579                         break;
580         }
581         if (status < 0)
582                 free_recovery_list(&(rec->r_list[type]));
583         if (status)
584                 mlog_errno(status);
585         return status;
586 }
587
588 /* Recover local quota files for given node different from us */
589 int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
590                                 struct ocfs2_quota_recovery *rec,
591                                 int slot_num)
592 {
593         unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
594                                               LOCAL_GROUP_QUOTA_SYSTEM_INODE };
595         struct super_block *sb = osb->sb;
596         struct ocfs2_local_disk_dqinfo *ldinfo;
597         struct buffer_head *bh;
598         handle_t *handle;
599         int type;
600         int status = 0;
601         struct inode *lqinode;
602         unsigned int flags;
603
604         printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
605                "slot %u\n", osb->dev_str, slot_num);
606
607         mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
608         for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
609                 if (list_empty(&(rec->r_list[type])))
610                         continue;
611                 trace_ocfs2_finish_quota_recovery(slot_num);
612                 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
613                 if (!lqinode) {
614                         status = -ENOENT;
615                         goto out;
616                 }
617                 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
618                                                        OCFS2_META_LOCK_NOQUEUE);
619                 /* Someone else is holding the lock? Then he must be
620                  * doing the recovery. Just skip the file... */
621                 if (status == -EAGAIN) {
622                         printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
623                                "device (%s) for slot %d because quota file is "
624                                "locked.\n", osb->dev_str, slot_num);
625                         status = 0;
626                         goto out_put;
627                 } else if (status < 0) {
628                         mlog_errno(status);
629                         goto out_put;
630                 }
631                 /* Now read local header */
632                 bh = NULL;
633                 status = ocfs2_read_quota_block(lqinode, 0, &bh);
634                 if (status) {
635                         mlog_errno(status);
636                         mlog(ML_ERROR, "failed to read quota file info header "
637                                 "(slot=%d type=%d)\n", slot_num, type);
638                         goto out_lock;
639                 }
640                 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
641                                                         OCFS2_LOCAL_INFO_OFF);
642                 /* Is recovery still needed? */
643                 flags = le32_to_cpu(ldinfo->dqi_flags);
644                 if (!(flags & OLQF_CLEAN))
645                         status = ocfs2_recover_local_quota_file(lqinode,
646                                                                 type,
647                                                                 rec);
648                 /* We don't want to mark file as clean when it is actually
649                  * active */
650                 if (slot_num == osb->slot_num)
651                         goto out_bh;
652                 /* Mark quota file as clean if we are recovering quota file of
653                  * some other node. */
654                 handle = ocfs2_start_trans(osb,
655                                            OCFS2_LOCAL_QINFO_WRITE_CREDITS);
656                 if (IS_ERR(handle)) {
657                         status = PTR_ERR(handle);
658                         mlog_errno(status);
659                         goto out_bh;
660                 }
661                 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
662                                                  bh,
663                                                  OCFS2_JOURNAL_ACCESS_WRITE);
664                 if (status < 0) {
665                         mlog_errno(status);
666                         goto out_trans;
667                 }
668                 lock_buffer(bh);
669                 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
670                 unlock_buffer(bh);
671                 ocfs2_journal_dirty(handle, bh);
672 out_trans:
673                 ocfs2_commit_trans(osb, handle);
674 out_bh:
675                 brelse(bh);
676 out_lock:
677                 ocfs2_inode_unlock(lqinode, 1);
678 out_put:
679                 iput(lqinode);
680                 if (status < 0)
681                         break;
682         }
683 out:
684         mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
685         kfree(rec);
686         return status;
687 }
688
689 /* Read information header from quota file */
690 static int ocfs2_local_read_info(struct super_block *sb, int type)
691 {
692         struct ocfs2_local_disk_dqinfo *ldinfo;
693         struct mem_dqinfo *info = sb_dqinfo(sb, type);
694         struct ocfs2_mem_dqinfo *oinfo;
695         struct inode *lqinode = sb_dqopt(sb)->files[type];
696         int status;
697         struct buffer_head *bh = NULL;
698         struct ocfs2_quota_recovery *rec;
699         int locked = 0;
700
701         /* We don't need the lock and we have to acquire quota file locks
702          * which will later depend on this lock */
703         mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
704         info->dqi_maxblimit = 0x7fffffffffffffffLL;
705         info->dqi_maxilimit = 0x7fffffffffffffffLL;
706         oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
707         if (!oinfo) {
708                 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
709                                " info.");
710                 goto out_err;
711         }
712         info->dqi_priv = oinfo;
713         oinfo->dqi_type = type;
714         INIT_LIST_HEAD(&oinfo->dqi_chunk);
715         oinfo->dqi_rec = NULL;
716         oinfo->dqi_lqi_bh = NULL;
717         oinfo->dqi_libh = NULL;
718
719         status = ocfs2_global_read_info(sb, type);
720         if (status < 0)
721                 goto out_err;
722
723         status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
724         if (status < 0) {
725                 mlog_errno(status);
726                 goto out_err;
727         }
728         locked = 1;
729
730         /* Now read local header */
731         status = ocfs2_read_quota_block(lqinode, 0, &bh);
732         if (status) {
733                 mlog_errno(status);
734                 mlog(ML_ERROR, "failed to read quota file info header "
735                         "(type=%d)\n", type);
736                 goto out_err;
737         }
738         ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
739                                                 OCFS2_LOCAL_INFO_OFF);
740         info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
741         oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
742         oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
743         oinfo->dqi_libh = bh;
744
745         /* We crashed when using local quota file? */
746         if (!(info->dqi_flags & OLQF_CLEAN)) {
747                 rec = OCFS2_SB(sb)->quota_rec;
748                 if (!rec) {
749                         rec = ocfs2_alloc_quota_recovery();
750                         if (!rec) {
751                                 status = -ENOMEM;
752                                 mlog_errno(status);
753                                 goto out_err;
754                         }
755                         OCFS2_SB(sb)->quota_rec = rec;
756                 }
757
758                 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
759                                                    &rec->r_list[type]);
760                 if (status < 0) {
761                         mlog_errno(status);
762                         goto out_err;
763                 }
764         }
765
766         status = ocfs2_load_local_quota_bitmaps(lqinode,
767                                                 ldinfo,
768                                                 &oinfo->dqi_chunk);
769         if (status < 0) {
770                 mlog_errno(status);
771                 goto out_err;
772         }
773
774         /* Now mark quota file as used */
775         info->dqi_flags &= ~OLQF_CLEAN;
776         status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
777         if (status < 0) {
778                 mlog_errno(status);
779                 goto out_err;
780         }
781
782         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
783         return 0;
784 out_err:
785         if (oinfo) {
786                 iput(oinfo->dqi_gqinode);
787                 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
788                 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
789                 brelse(oinfo->dqi_lqi_bh);
790                 if (locked)
791                         ocfs2_inode_unlock(lqinode, 1);
792                 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
793                 kfree(oinfo);
794         }
795         brelse(bh);
796         mutex_lock(&sb_dqopt(sb)->dqio_mutex);
797         return -1;
798 }
799
800 /* Write local info to quota file */
801 static int ocfs2_local_write_info(struct super_block *sb, int type)
802 {
803         struct mem_dqinfo *info = sb_dqinfo(sb, type);
804         struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
805                                                 ->dqi_libh;
806         int status;
807
808         status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
809                                  info);
810         if (status < 0) {
811                 mlog_errno(status);
812                 return -1;
813         }
814
815         return 0;
816 }
817
818 /* Release info from memory */
819 static int ocfs2_local_free_info(struct super_block *sb, int type)
820 {
821         struct mem_dqinfo *info = sb_dqinfo(sb, type);
822         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
823         struct ocfs2_quota_chunk *chunk;
824         struct ocfs2_local_disk_chunk *dchunk;
825         int mark_clean = 1, len;
826         int status;
827
828         iput(oinfo->dqi_gqinode);
829         ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
830         ocfs2_lock_res_free(&oinfo->dqi_gqlock);
831         list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
832                 dchunk = (struct ocfs2_local_disk_chunk *)
833                                         (chunk->qc_headerbh->b_data);
834                 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
835                         len = ol_chunk_entries(sb);
836                 } else {
837                         len = (oinfo->dqi_blocks -
838                                ol_quota_chunk_block(sb, chunk->qc_num) - 1)
839                               * ol_quota_entries_per_block(sb);
840                 }
841                 /* Not all entries free? Bug! */
842                 if (le32_to_cpu(dchunk->dqc_free) != len) {
843                         mlog(ML_ERROR, "releasing quota file with used "
844                                         "entries (type=%d)\n", type);
845                         mark_clean = 0;
846                 }
847         }
848         ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
849
850         /* dqonoff_mutex protects us against racing with recovery thread... */
851         if (oinfo->dqi_rec) {
852                 ocfs2_free_quota_recovery(oinfo->dqi_rec);
853                 mark_clean = 0;
854         }
855
856         if (!mark_clean)
857                 goto out;
858
859         /* Mark local file as clean */
860         info->dqi_flags |= OLQF_CLEAN;
861         status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
862                                  oinfo->dqi_libh,
863                                  olq_update_info,
864                                  info);
865         if (status < 0) {
866                 mlog_errno(status);
867                 goto out;
868         }
869
870 out:
871         ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
872         brelse(oinfo->dqi_libh);
873         brelse(oinfo->dqi_lqi_bh);
874         kfree(oinfo);
875         return 0;
876 }
877
878 static void olq_set_dquot(struct buffer_head *bh, void *private)
879 {
880         struct ocfs2_dquot *od = private;
881         struct ocfs2_local_disk_dqblk *dqblk;
882         struct super_block *sb = od->dq_dquot.dq_sb;
883
884         dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
885                 + ol_dqblk_block_offset(sb, od->dq_local_off));
886
887         dqblk->dqb_id = cpu_to_le64(from_kqid(&init_user_ns,
888                                               od->dq_dquot.dq_id));
889         spin_lock(&dq_data_lock);
890         dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
891                                           od->dq_origspace);
892         dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
893                                           od->dq_originodes);
894         spin_unlock(&dq_data_lock);
895         trace_olq_set_dquot(
896                 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
897                 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
898                 from_kqid(&init_user_ns, od->dq_dquot.dq_id));
899 }
900
901 /* Write dquot to local quota file */
902 int ocfs2_local_write_dquot(struct dquot *dquot)
903 {
904         struct super_block *sb = dquot->dq_sb;
905         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
906         struct buffer_head *bh;
907         struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_id.type];
908         int status;
909
910         status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
911                                              &bh);
912         if (status) {
913                 mlog_errno(status);
914                 goto out;
915         }
916         status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
917         if (status < 0) {
918                 mlog_errno(status);
919                 goto out;
920         }
921 out:
922         brelse(bh);
923         return status;
924 }
925
926 /* Find free entry in local quota file */
927 static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
928                                                        int type,
929                                                        int *offset)
930 {
931         struct mem_dqinfo *info = sb_dqinfo(sb, type);
932         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
933         struct ocfs2_quota_chunk *chunk;
934         struct ocfs2_local_disk_chunk *dchunk;
935         int found = 0, len;
936
937         list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
938                 dchunk = (struct ocfs2_local_disk_chunk *)
939                                                 chunk->qc_headerbh->b_data;
940                 if (le32_to_cpu(dchunk->dqc_free) > 0) {
941                         found = 1;
942                         break;
943                 }
944         }
945         if (!found)
946                 return NULL;
947
948         if (chunk->qc_num < oinfo->dqi_chunks - 1) {
949                 len = ol_chunk_entries(sb);
950         } else {
951                 len = (oinfo->dqi_blocks -
952                        ol_quota_chunk_block(sb, chunk->qc_num) - 1)
953                       * ol_quota_entries_per_block(sb);
954         }
955
956         found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
957         /* We failed? */
958         if (found == len) {
959                 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
960                      " entries free (type=%d)\n", chunk->qc_num,
961                      le32_to_cpu(dchunk->dqc_free), type);
962                 return ERR_PTR(-EIO);
963         }
964         *offset = found;
965         return chunk;
966 }
967
968 /* Add new chunk to the local quota file */
969 static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
970                                                         struct super_block *sb,
971                                                         int type,
972                                                         int *offset)
973 {
974         struct mem_dqinfo *info = sb_dqinfo(sb, type);
975         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
976         struct inode *lqinode = sb_dqopt(sb)->files[type];
977         struct ocfs2_quota_chunk *chunk = NULL;
978         struct ocfs2_local_disk_chunk *dchunk;
979         int status;
980         handle_t *handle;
981         struct buffer_head *bh = NULL, *dbh = NULL;
982         u64 p_blkno;
983
984         /* We are protected by dqio_sem so no locking needed */
985         status = ocfs2_extend_no_holes(lqinode, NULL,
986                                        i_size_read(lqinode) + 2 * sb->s_blocksize,
987                                        i_size_read(lqinode));
988         if (status < 0) {
989                 mlog_errno(status);
990                 goto out;
991         }
992         status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
993                                           i_size_read(lqinode) + 2 * sb->s_blocksize);
994         if (status < 0) {
995                 mlog_errno(status);
996                 goto out;
997         }
998
999         chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
1000         if (!chunk) {
1001                 status = -ENOMEM;
1002                 mlog_errno(status);
1003                 goto out;
1004         }
1005         /* Local quota info and two new blocks we initialize */
1006         handle = ocfs2_start_trans(OCFS2_SB(sb),
1007                         OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1008                         2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1009         if (IS_ERR(handle)) {
1010                 status = PTR_ERR(handle);
1011                 mlog_errno(status);
1012                 goto out;
1013         }
1014
1015         /* Initialize chunk header */
1016         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1017                                              &p_blkno, NULL, NULL);
1018         if (status < 0) {
1019                 mlog_errno(status);
1020                 goto out_trans;
1021         }
1022         bh = sb_getblk(sb, p_blkno);
1023         if (!bh) {
1024                 status = -ENOMEM;
1025                 mlog_errno(status);
1026                 goto out_trans;
1027         }
1028         dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1029         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1030         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1031                                          OCFS2_JOURNAL_ACCESS_CREATE);
1032         if (status < 0) {
1033                 mlog_errno(status);
1034                 goto out_trans;
1035         }
1036         lock_buffer(bh);
1037         dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
1038         memset(dchunk->dqc_bitmap, 0,
1039                sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1040                OCFS2_QBLK_RESERVED_SPACE);
1041         unlock_buffer(bh);
1042         ocfs2_journal_dirty(handle, bh);
1043
1044         /* Initialize new block with structures */
1045         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1046                                              &p_blkno, NULL, NULL);
1047         if (status < 0) {
1048                 mlog_errno(status);
1049                 goto out_trans;
1050         }
1051         dbh = sb_getblk(sb, p_blkno);
1052         if (!dbh) {
1053                 status = -ENOMEM;
1054                 mlog_errno(status);
1055                 goto out_trans;
1056         }
1057         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
1058         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
1059                                          OCFS2_JOURNAL_ACCESS_CREATE);
1060         if (status < 0) {
1061                 mlog_errno(status);
1062                 goto out_trans;
1063         }
1064         lock_buffer(dbh);
1065         memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1066         unlock_buffer(dbh);
1067         ocfs2_journal_dirty(handle, dbh);
1068
1069         /* Update local quotafile info */
1070         oinfo->dqi_blocks += 2;
1071         oinfo->dqi_chunks++;
1072         status = ocfs2_local_write_info(sb, type);
1073         if (status < 0) {
1074                 mlog_errno(status);
1075                 goto out_trans;
1076         }
1077         status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1078         if (status < 0) {
1079                 mlog_errno(status);
1080                 goto out;
1081         }
1082
1083         list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1084         chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1085                                    struct ocfs2_quota_chunk,
1086                                    qc_chunk)->qc_num + 1;
1087         chunk->qc_headerbh = bh;
1088         *offset = 0;
1089         return chunk;
1090 out_trans:
1091         ocfs2_commit_trans(OCFS2_SB(sb), handle);
1092 out:
1093         brelse(bh);
1094         brelse(dbh);
1095         kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1096         return ERR_PTR(status);
1097 }
1098
1099 /* Find free entry in local quota file */
1100 static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1101                                                        struct super_block *sb,
1102                                                        int type,
1103                                                        int *offset)
1104 {
1105         struct mem_dqinfo *info = sb_dqinfo(sb, type);
1106         struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1107         struct ocfs2_quota_chunk *chunk;
1108         struct inode *lqinode = sb_dqopt(sb)->files[type];
1109         struct ocfs2_local_disk_chunk *dchunk;
1110         int epb = ol_quota_entries_per_block(sb);
1111         unsigned int chunk_blocks;
1112         struct buffer_head *bh;
1113         u64 p_blkno;
1114         int status;
1115         handle_t *handle;
1116
1117         if (list_empty(&oinfo->dqi_chunk))
1118                 return ocfs2_local_quota_add_chunk(sb, type, offset);
1119         /* Is the last chunk full? */
1120         chunk = list_entry(oinfo->dqi_chunk.prev,
1121                         struct ocfs2_quota_chunk, qc_chunk);
1122         chunk_blocks = oinfo->dqi_blocks -
1123                         ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1124         if (ol_chunk_blocks(sb) == chunk_blocks)
1125                 return ocfs2_local_quota_add_chunk(sb, type, offset);
1126
1127         /* We are protected by dqio_sem so no locking needed */
1128         status = ocfs2_extend_no_holes(lqinode, NULL,
1129                                        i_size_read(lqinode) + sb->s_blocksize,
1130                                        i_size_read(lqinode));
1131         if (status < 0) {
1132                 mlog_errno(status);
1133                 goto out;
1134         }
1135         status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1136                                           i_size_read(lqinode) + sb->s_blocksize);
1137         if (status < 0) {
1138                 mlog_errno(status);
1139                 goto out;
1140         }
1141
1142         /* Get buffer from the just added block */
1143         status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1144                                              &p_blkno, NULL, NULL);
1145         if (status < 0) {
1146                 mlog_errno(status);
1147                 goto out;
1148         }
1149         bh = sb_getblk(sb, p_blkno);
1150         if (!bh) {
1151                 status = -ENOMEM;
1152                 mlog_errno(status);
1153                 goto out;
1154         }
1155         ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
1156
1157         /* Local quota info, chunk header and the new block we initialize */
1158         handle = ocfs2_start_trans(OCFS2_SB(sb),
1159                         OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1160                         2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
1161         if (IS_ERR(handle)) {
1162                 status = PTR_ERR(handle);
1163                 mlog_errno(status);
1164                 goto out;
1165         }
1166         /* Zero created block */
1167         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
1168                                  OCFS2_JOURNAL_ACCESS_CREATE);
1169         if (status < 0) {
1170                 mlog_errno(status);
1171                 goto out_trans;
1172         }
1173         lock_buffer(bh);
1174         memset(bh->b_data, 0, sb->s_blocksize);
1175         unlock_buffer(bh);
1176         ocfs2_journal_dirty(handle, bh);
1177
1178         /* Update chunk header */
1179         status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1180                                          chunk->qc_headerbh,
1181                                  OCFS2_JOURNAL_ACCESS_WRITE);
1182         if (status < 0) {
1183                 mlog_errno(status);
1184                 goto out_trans;
1185         }
1186
1187         dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1188         lock_buffer(chunk->qc_headerbh);
1189         le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1190         unlock_buffer(chunk->qc_headerbh);
1191         ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1192
1193         /* Update file header */
1194         oinfo->dqi_blocks++;
1195         status = ocfs2_local_write_info(sb, type);
1196         if (status < 0) {
1197                 mlog_errno(status);
1198                 goto out_trans;
1199         }
1200
1201         status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1202         if (status < 0) {
1203                 mlog_errno(status);
1204                 goto out;
1205         }
1206         *offset = chunk_blocks * epb;
1207         return chunk;
1208 out_trans:
1209         ocfs2_commit_trans(OCFS2_SB(sb), handle);
1210 out:
1211         return ERR_PTR(status);
1212 }
1213
1214 static void olq_alloc_dquot(struct buffer_head *bh, void *private)
1215 {
1216         int *offset = private;
1217         struct ocfs2_local_disk_chunk *dchunk;
1218
1219         dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1220         ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
1221         le32_add_cpu(&dchunk->dqc_free, -1);
1222 }
1223
1224 /* Create dquot in the local file for given id */
1225 int ocfs2_create_local_dquot(struct dquot *dquot)
1226 {
1227         struct super_block *sb = dquot->dq_sb;
1228         int type = dquot->dq_id.type;
1229         struct inode *lqinode = sb_dqopt(sb)->files[type];
1230         struct ocfs2_quota_chunk *chunk;
1231         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1232         int offset;
1233         int status;
1234         u64 pcount;
1235
1236         down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1237         chunk = ocfs2_find_free_entry(sb, type, &offset);
1238         if (!chunk) {
1239                 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1240                 if (IS_ERR(chunk)) {
1241                         status = PTR_ERR(chunk);
1242                         goto out;
1243                 }
1244         } else if (IS_ERR(chunk)) {
1245                 status = PTR_ERR(chunk);
1246                 goto out;
1247         }
1248         od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1249         od->dq_chunk = chunk;
1250         status = ocfs2_extent_map_get_blocks(lqinode,
1251                                      ol_dqblk_block(sb, chunk->qc_num, offset),
1252                                      &od->dq_local_phys_blk,
1253                                      &pcount,
1254                                      NULL);
1255
1256         /* Initialize dquot structure on disk */
1257         status = ocfs2_local_write_dquot(dquot);
1258         if (status < 0) {
1259                 mlog_errno(status);
1260                 goto out;
1261         }
1262
1263         /* Mark structure as allocated */
1264         status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1265                                  &offset);
1266         if (status < 0) {
1267                 mlog_errno(status);
1268                 goto out;
1269         }
1270 out:
1271         up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
1272         return status;
1273 }
1274
1275 /*
1276  * Release dquot structure from local quota file. ocfs2_release_dquot() has
1277  * already started a transaction and written all changes to global quota file
1278  */
1279 int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
1280 {
1281         int status;
1282         int type = dquot->dq_id.type;
1283         struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1284         struct super_block *sb = dquot->dq_sb;
1285         struct ocfs2_local_disk_chunk *dchunk;
1286         int offset;
1287
1288         status = ocfs2_journal_access_dq(handle,
1289                         INODE_CACHE(sb_dqopt(sb)->files[type]),
1290                         od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1291         if (status < 0) {
1292                 mlog_errno(status);
1293                 goto out;
1294         }
1295         offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1296                                              od->dq_local_off);
1297         dchunk = (struct ocfs2_local_disk_chunk *)
1298                         (od->dq_chunk->qc_headerbh->b_data);
1299         /* Mark structure as freed */
1300         lock_buffer(od->dq_chunk->qc_headerbh);
1301         ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
1302         le32_add_cpu(&dchunk->dqc_free, 1);
1303         unlock_buffer(od->dq_chunk->qc_headerbh);
1304         ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1305
1306 out:
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 };