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