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