Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-block.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
93f5920d
JP
140 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block)
141 return ocfs2_error(inode->i_sb,
142 "Quota file %llu is probably corrupted! Requested to read block %Lu but file has size only %Lu\n",
143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
144 (unsigned long long)v_block,
145 (unsigned long long)i_size_read(inode));
146
fb8dd8d7
JK
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
5f530de6 457 * s_umount locked in shared mode. */
2205363d
JK
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;
6c5026c1 473 unsigned int memalloc;
2205363d 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);
6c5026c1 525 memalloc = memalloc_nofs_save();
7b9ca4c6 526 spin_lock(&dquot->dq_dqb_lock);
2205363d
JK
527 /* Add usage from quota entry into quota changes
528 * of our node. Auxiliary variables are important
529 * due to signedness */
530 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
531 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
532 dquot->dq_dqb.dqb_curspace += spacechange;
533 dquot->dq_dqb.dqb_curinodes += inodechange;
7b9ca4c6 534 spin_unlock(&dquot->dq_dqb_lock);
2205363d
JK
535 /* We want to drop reference held by the crashed
536 * node. Since we have our own reference we know
537 * global structure actually won't be freed. */
538 status = ocfs2_global_release_dquot(dquot);
539 if (status < 0) {
540 mlog_errno(status);
541 goto out_commit;
542 }
543 /* Release local quota file entry */
0cf2f763
JB
544 status = ocfs2_journal_access_dq(handle,
545 INODE_CACHE(lqinode),
2205363d
JK
546 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
547 if (status < 0) {
548 mlog_errno(status);
549 goto out_commit;
550 }
551 lock_buffer(qbh);
93925579
AM
552 WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
553 ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
2205363d
JK
554 le32_add_cpu(&dchunk->dqc_free, 1);
555 unlock_buffer(qbh);
ec20cec7 556 ocfs2_journal_dirty(handle, qbh);
2205363d 557out_commit:
6c5026c1 558 memalloc_nofs_restore(memalloc);
bc8230ee 559 up_write(&sb_dqopt(sb)->dqio_sem);
2205363d 560 ocfs2_commit_trans(OCFS2_SB(sb), handle);
80d73f15
JK
561out_drop_lock:
562 ocfs2_unlock_global_qf(oinfo, 1);
2205363d
JK
563out_put_dquot:
564 dqput(dquot);
565out_put_bh:
566 brelse(qbh);
567 if (status < 0)
568 break;
569 }
570 brelse(hbh);
571 list_del(&rchunk->rc_list);
572 kfree(rchunk->rc_bitmap);
573 kfree(rchunk);
574 if (status < 0)
575 break;
576 }
2205363d
JK
577 if (status < 0)
578 free_recovery_list(&(rec->r_list[type]));
c1e8d35e
TM
579 if (status)
580 mlog_errno(status);
2205363d
JK
581 return status;
582}
583
584/* Recover local quota files for given node different from us */
585int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
586 struct ocfs2_quota_recovery *rec,
587 int slot_num)
588{
52362810
JK
589 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
590 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
2205363d
JK
591 struct super_block *sb = osb->sb;
592 struct ocfs2_local_disk_dqinfo *ldinfo;
593 struct buffer_head *bh;
594 handle_t *handle;
595 int type;
596 int status = 0;
597 struct inode *lqinode;
598 unsigned int flags;
599
619c200d
SM
600 printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
601 "slot %u\n", osb->dev_str, slot_num);
602
5f530de6 603 down_read(&sb->s_umount);
52362810 604 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
2205363d
JK
605 if (list_empty(&(rec->r_list[type])))
606 continue;
38877a43 607 trace_ocfs2_finish_quota_recovery(slot_num);
2205363d
JK
608 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
609 if (!lqinode) {
610 status = -ENOENT;
611 goto out;
612 }
613 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
614 OCFS2_META_LOCK_NOQUEUE);
615 /* Someone else is holding the lock? Then he must be
616 * doing the recovery. Just skip the file... */
617 if (status == -EAGAIN) {
619c200d
SM
618 printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
619 "device (%s) for slot %d because quota file is "
620 "locked.\n", osb->dev_str, slot_num);
2205363d
JK
621 status = 0;
622 goto out_put;
623 } else if (status < 0) {
624 mlog_errno(status);
625 goto out_put;
626 }
627 /* Now read local header */
85eb8b73
JB
628 bh = NULL;
629 status = ocfs2_read_quota_block(lqinode, 0, &bh);
630 if (status) {
2205363d
JK
631 mlog_errno(status);
632 mlog(ML_ERROR, "failed to read quota file info header "
633 "(slot=%d type=%d)\n", slot_num, type);
634 goto out_lock;
635 }
636 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
637 OCFS2_LOCAL_INFO_OFF);
638 /* Is recovery still needed? */
639 flags = le32_to_cpu(ldinfo->dqi_flags);
640 if (!(flags & OLQF_CLEAN))
641 status = ocfs2_recover_local_quota_file(lqinode,
642 type,
643 rec);
644 /* We don't want to mark file as clean when it is actually
645 * active */
646 if (slot_num == osb->slot_num)
647 goto out_bh;
648 /* Mark quota file as clean if we are recovering quota file of
649 * some other node. */
0584974a
JK
650 handle = ocfs2_start_trans(osb,
651 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
2205363d
JK
652 if (IS_ERR(handle)) {
653 status = PTR_ERR(handle);
654 mlog_errno(status);
655 goto out_bh;
656 }
0cf2f763
JB
657 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
658 bh,
13723d00 659 OCFS2_JOURNAL_ACCESS_WRITE);
2205363d
JK
660 if (status < 0) {
661 mlog_errno(status);
662 goto out_trans;
663 }
664 lock_buffer(bh);
665 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
666 unlock_buffer(bh);
ec20cec7 667 ocfs2_journal_dirty(handle, bh);
2205363d
JK
668out_trans:
669 ocfs2_commit_trans(osb, handle);
670out_bh:
671 brelse(bh);
672out_lock:
673 ocfs2_inode_unlock(lqinode, 1);
674out_put:
675 iput(lqinode);
676 if (status < 0)
677 break;
678 }
679out:
5f530de6 680 up_read(&sb->s_umount);
2205363d
JK
681 kfree(rec);
682 return status;
683}
684
9e33d69f
JK
685/* Read information header from quota file */
686static int ocfs2_local_read_info(struct super_block *sb, int type)
687{
688 struct ocfs2_local_disk_dqinfo *ldinfo;
689 struct mem_dqinfo *info = sb_dqinfo(sb, type);
690 struct ocfs2_mem_dqinfo *oinfo;
691 struct inode *lqinode = sb_dqopt(sb)->files[type];
692 int status;
693 struct buffer_head *bh = NULL;
2205363d 694 struct ocfs2_quota_recovery *rec;
35fccce2 695 int locked = 0, global_read = 0;
9e33d69f 696
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.");
35fccce2 703 status = -ENOMEM;
9e33d69f
JK
704 goto out_err;
705 }
706 info->dqi_priv = oinfo;
707 oinfo->dqi_type = type;
708 INIT_LIST_HEAD(&oinfo->dqi_chunk);
2205363d 709 oinfo->dqi_rec = NULL;
9e33d69f 710 oinfo->dqi_lqi_bh = NULL;
ae4f6ef1 711 oinfo->dqi_libh = NULL;
9e33d69f
JK
712
713 status = ocfs2_global_read_info(sb, type);
714 if (status < 0)
715 goto out_err;
35fccce2 716 global_read = 1;
9e33d69f
JK
717
718 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
719 if (status < 0) {
720 mlog_errno(status);
721 goto out_err;
722 }
723 locked = 1;
724
725 /* Now read local header */
85eb8b73
JB
726 status = ocfs2_read_quota_block(lqinode, 0, &bh);
727 if (status) {
9e33d69f
JK
728 mlog_errno(status);
729 mlog(ML_ERROR, "failed to read quota file info header "
730 "(type=%d)\n", type);
731 goto out_err;
732 }
733 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
734 OCFS2_LOCAL_INFO_OFF);
96827adc 735 oinfo->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
9e33d69f
JK
736 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
737 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
ae4f6ef1 738 oinfo->dqi_libh = bh;
9e33d69f
JK
739
740 /* We crashed when using local quota file? */
96827adc 741 if (!(oinfo->dqi_flags & OLQF_CLEAN)) {
2205363d
JK
742 rec = OCFS2_SB(sb)->quota_rec;
743 if (!rec) {
744 rec = ocfs2_alloc_quota_recovery();
745 if (!rec) {
746 status = -ENOMEM;
747 mlog_errno(status);
748 goto out_err;
749 }
750 OCFS2_SB(sb)->quota_rec = rec;
751 }
9e33d69f 752
2205363d
JK
753 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
754 &rec->r_list[type]);
755 if (status < 0) {
756 mlog_errno(status);
757 goto out_err;
758 }
759 }
760
761 status = ocfs2_load_local_quota_bitmaps(lqinode,
9e33d69f
JK
762 ldinfo,
763 &oinfo->dqi_chunk);
764 if (status < 0) {
765 mlog_errno(status);
766 goto out_err;
767 }
768
769 /* Now mark quota file as used */
96827adc 770 oinfo->dqi_flags &= ~OLQF_CLEAN;
9e33d69f
JK
771 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
772 if (status < 0) {
773 mlog_errno(status);
774 goto out_err;
775 }
776
777 return 0;
778out_err:
779 if (oinfo) {
780 iput(oinfo->dqi_gqinode);
781 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
782 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
783 brelse(oinfo->dqi_lqi_bh);
784 if (locked)
785 ocfs2_inode_unlock(lqinode, 1);
786 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
35fccce2
JQ
787 if (global_read)
788 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
9e33d69f
JK
789 kfree(oinfo);
790 }
791 brelse(bh);
35fccce2 792 return status;
9e33d69f
JK
793}
794
795/* Write local info to quota file */
796static int ocfs2_local_write_info(struct super_block *sb, int type)
797{
798 struct mem_dqinfo *info = sb_dqinfo(sb, type);
799 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
ae4f6ef1 800 ->dqi_libh;
9e33d69f
JK
801 int status;
802
803 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
804 info);
805 if (status < 0) {
806 mlog_errno(status);
807 return -1;
808 }
809
810 return 0;
811}
812
813/* Release info from memory */
814static int ocfs2_local_free_info(struct super_block *sb, int type)
815{
816 struct mem_dqinfo *info = sb_dqinfo(sb, type);
817 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
818 struct ocfs2_quota_chunk *chunk;
819 struct ocfs2_local_disk_chunk *dchunk;
820 int mark_clean = 1, len;
d32840ad 821 int status = 0;
9e33d69f
JK
822
823 iput(oinfo->dqi_gqinode);
824 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
825 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
826 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
827 dchunk = (struct ocfs2_local_disk_chunk *)
828 (chunk->qc_headerbh->b_data);
829 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
830 len = ol_chunk_entries(sb);
831 } else {
832 len = (oinfo->dqi_blocks -
833 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
834 * ol_quota_entries_per_block(sb);
835 }
836 /* Not all entries free? Bug! */
837 if (le32_to_cpu(dchunk->dqc_free) != len) {
838 mlog(ML_ERROR, "releasing quota file with used "
839 "entries (type=%d)\n", type);
840 mark_clean = 0;
841 }
842 }
843 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
844
5f530de6
JK
845 /*
846 * s_umount held in exclusive mode protects us against racing with
847 * recovery thread...
848 */
2205363d
JK
849 if (oinfo->dqi_rec) {
850 ocfs2_free_quota_recovery(oinfo->dqi_rec);
851 mark_clean = 0;
852 }
853
9e33d69f
JK
854 if (!mark_clean)
855 goto out;
856
857 /* Mark local file as clean */
96827adc 858 oinfo->dqi_flags |= OLQF_CLEAN;
9e33d69f 859 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
ae4f6ef1 860 oinfo->dqi_libh,
9e33d69f
JK
861 olq_update_info,
862 info);
d32840ad 863 if (status < 0)
9e33d69f 864 mlog_errno(status);
9e33d69f
JK
865out:
866 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
ae4f6ef1 867 brelse(oinfo->dqi_libh);
9e33d69f
JK
868 brelse(oinfo->dqi_lqi_bh);
869 kfree(oinfo);
d32840ad 870 return status;
9e33d69f
JK
871}
872
873static void olq_set_dquot(struct buffer_head *bh, void *private)
874{
875 struct ocfs2_dquot *od = private;
876 struct ocfs2_local_disk_dqblk *dqblk;
877 struct super_block *sb = od->dq_dquot.dq_sb;
878
879 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
880 + ol_dqblk_block_offset(sb, od->dq_local_off));
881
4c376dca
EB
882 dqblk->dqb_id = cpu_to_le64(from_kqid(&init_user_ns,
883 od->dq_dquot.dq_id));
7b9ca4c6 884 spin_lock(&od->dq_dquot.dq_dqb_lock);
9e33d69f
JK
885 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
886 od->dq_origspace);
887 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
888 od->dq_originodes);
7b9ca4c6 889 spin_unlock(&od->dq_dquot.dq_dqb_lock);
38877a43
TM
890 trace_olq_set_dquot(
891 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
892 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
4c376dca 893 from_kqid(&init_user_ns, od->dq_dquot.dq_id));
9e33d69f
JK
894}
895
896/* Write dquot to local quota file */
741e1289 897int ocfs2_local_write_dquot(struct dquot *dquot)
9e33d69f
JK
898{
899 struct super_block *sb = dquot->dq_sb;
900 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
f64dd44e 901 struct buffer_head *bh;
4c376dca 902 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_id.type];
9e33d69f
JK
903 int status;
904
f64dd44e
JK
905 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
906 &bh);
85eb8b73 907 if (status) {
9e33d69f
JK
908 mlog_errno(status);
909 goto out;
910 }
f64dd44e 911 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
9e33d69f
JK
912 if (status < 0) {
913 mlog_errno(status);
914 goto out;
915 }
916out:
917 brelse(bh);
918 return status;
919}
920
921/* Find free entry in local quota file */
922static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
923 int type,
924 int *offset)
925{
926 struct mem_dqinfo *info = sb_dqinfo(sb, type);
927 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
81cd1ae9 928 struct ocfs2_quota_chunk *chunk = NULL, *iter;
9e33d69f
JK
929 struct ocfs2_local_disk_chunk *dchunk;
930 int found = 0, len;
931
81cd1ae9 932 list_for_each_entry(iter, &oinfo->dqi_chunk, qc_chunk) {
9e33d69f 933 dchunk = (struct ocfs2_local_disk_chunk *)
81cd1ae9 934 iter->qc_headerbh->b_data;
9e33d69f 935 if (le32_to_cpu(dchunk->dqc_free) > 0) {
81cd1ae9 936 chunk = iter;
9e33d69f
JK
937 break;
938 }
939 }
81cd1ae9 940 if (!chunk)
9e33d69f
JK
941 return NULL;
942
943 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
944 len = ol_chunk_entries(sb);
945 } else {
946 len = (oinfo->dqi_blocks -
947 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
948 * ol_quota_entries_per_block(sb);
949 }
950
93925579 951 found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
9e33d69f
JK
952 /* We failed? */
953 if (found == len) {
954 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
955 " entries free (type=%d)\n", chunk->qc_num,
956 le32_to_cpu(dchunk->dqc_free), type);
957 return ERR_PTR(-EIO);
958 }
959 *offset = found;
960 return chunk;
961}
962
963/* Add new chunk to the local quota file */
964static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
965 struct super_block *sb,
966 int type,
967 int *offset)
968{
969 struct mem_dqinfo *info = sb_dqinfo(sb, type);
970 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
971 struct inode *lqinode = sb_dqopt(sb)->files[type];
972 struct ocfs2_quota_chunk *chunk = NULL;
973 struct ocfs2_local_disk_chunk *dchunk;
974 int status;
975 handle_t *handle;
0e7f387b 976 struct buffer_head *bh = NULL, *dbh = NULL;
9e33d69f
JK
977 u64 p_blkno;
978
979 /* We are protected by dqio_sem so no locking needed */
5693486b 980 status = ocfs2_extend_no_holes(lqinode, NULL,
f17c20dd
JB
981 i_size_read(lqinode) + 2 * sb->s_blocksize,
982 i_size_read(lqinode));
9e33d69f
JK
983 if (status < 0) {
984 mlog_errno(status);
985 goto out;
986 }
987 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
f17c20dd 988 i_size_read(lqinode) + 2 * sb->s_blocksize);
9e33d69f
JK
989 if (status < 0) {
990 mlog_errno(status);
991 goto out;
992 }
993
994 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
995 if (!chunk) {
996 status = -ENOMEM;
997 mlog_errno(status);
998 goto out;
999 }
0584974a
JK
1000 /* Local quota info and two new blocks we initialize */
1001 handle = ocfs2_start_trans(OCFS2_SB(sb),
1002 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1003 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
0e7f387b
JK
1004 if (IS_ERR(handle)) {
1005 status = PTR_ERR(handle);
1006 mlog_errno(status);
1007 goto out;
1008 }
9e33d69f 1009
0e7f387b 1010 /* Initialize chunk header */
9e33d69f
JK
1011 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1012 &p_blkno, NULL, NULL);
9e33d69f
JK
1013 if (status < 0) {
1014 mlog_errno(status);
0e7f387b 1015 goto out_trans;
9e33d69f
JK
1016 }
1017 bh = sb_getblk(sb, p_blkno);
1018 if (!bh) {
1019 status = -ENOMEM;
1020 mlog_errno(status);
0e7f387b 1021 goto out_trans;
9e33d69f
JK
1022 }
1023 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
8cb471e8 1024 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0cf2f763 1025 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387b 1026 OCFS2_JOURNAL_ACCESS_CREATE);
9e33d69f
JK
1027 if (status < 0) {
1028 mlog_errno(status);
1029 goto out_trans;
1030 }
1031 lock_buffer(bh);
df32b334 1032 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
9e33d69f
JK
1033 memset(dchunk->dqc_bitmap, 0,
1034 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1035 OCFS2_QBLK_RESERVED_SPACE);
9e33d69f 1036 unlock_buffer(bh);
ec20cec7 1037 ocfs2_journal_dirty(handle, bh);
9e33d69f 1038
0e7f387b 1039 /* Initialize new block with structures */
0e7f387b
JK
1040 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1041 &p_blkno, NULL, NULL);
0e7f387b
JK
1042 if (status < 0) {
1043 mlog_errno(status);
1044 goto out_trans;
1045 }
1046 dbh = sb_getblk(sb, p_blkno);
1047 if (!dbh) {
1048 status = -ENOMEM;
1049 mlog_errno(status);
1050 goto out_trans;
1051 }
8cb471e8 1052 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
0cf2f763 1053 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
0e7f387b
JK
1054 OCFS2_JOURNAL_ACCESS_CREATE);
1055 if (status < 0) {
1056 mlog_errno(status);
1057 goto out_trans;
1058 }
1059 lock_buffer(dbh);
1060 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1061 unlock_buffer(dbh);
ec20cec7 1062 ocfs2_journal_dirty(handle, dbh);
0e7f387b
JK
1063
1064 /* Update local quotafile info */
9e33d69f
JK
1065 oinfo->dqi_blocks += 2;
1066 oinfo->dqi_chunks++;
1067 status = ocfs2_local_write_info(sb, type);
1068 if (status < 0) {
1069 mlog_errno(status);
1070 goto out_trans;
1071 }
1072 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1073 if (status < 0) {
1074 mlog_errno(status);
1075 goto out;
1076 }
1077
1078 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1079 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1080 struct ocfs2_quota_chunk,
1081 qc_chunk)->qc_num + 1;
1082 chunk->qc_headerbh = bh;
1083 *offset = 0;
1084 return chunk;
1085out_trans:
1086 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1087out:
1088 brelse(bh);
0e7f387b 1089 brelse(dbh);
9e33d69f
JK
1090 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1091 return ERR_PTR(status);
1092}
1093
1094/* Find free entry in local quota file */
1095static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1096 struct super_block *sb,
1097 int type,
1098 int *offset)
1099{
1100 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1101 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1102 struct ocfs2_quota_chunk *chunk;
1103 struct inode *lqinode = sb_dqopt(sb)->files[type];
1104 struct ocfs2_local_disk_chunk *dchunk;
1105 int epb = ol_quota_entries_per_block(sb);
1106 unsigned int chunk_blocks;
0e7f387b
JK
1107 struct buffer_head *bh;
1108 u64 p_blkno;
9e33d69f
JK
1109 int status;
1110 handle_t *handle;
1111
1112 if (list_empty(&oinfo->dqi_chunk))
1113 return ocfs2_local_quota_add_chunk(sb, type, offset);
1114 /* Is the last chunk full? */
1115 chunk = list_entry(oinfo->dqi_chunk.prev,
1116 struct ocfs2_quota_chunk, qc_chunk);
1117 chunk_blocks = oinfo->dqi_blocks -
1118 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1119 if (ol_chunk_blocks(sb) == chunk_blocks)
1120 return ocfs2_local_quota_add_chunk(sb, type, offset);
1121
1122 /* We are protected by dqio_sem so no locking needed */
5693486b 1123 status = ocfs2_extend_no_holes(lqinode, NULL,
f17c20dd
JB
1124 i_size_read(lqinode) + sb->s_blocksize,
1125 i_size_read(lqinode));
9e33d69f
JK
1126 if (status < 0) {
1127 mlog_errno(status);
1128 goto out;
1129 }
1130 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
f17c20dd 1131 i_size_read(lqinode) + sb->s_blocksize);
9e33d69f
JK
1132 if (status < 0) {
1133 mlog_errno(status);
1134 goto out;
1135 }
0e7f387b
JK
1136
1137 /* Get buffer from the just added block */
0e7f387b
JK
1138 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1139 &p_blkno, NULL, NULL);
0e7f387b
JK
1140 if (status < 0) {
1141 mlog_errno(status);
1142 goto out;
1143 }
1144 bh = sb_getblk(sb, p_blkno);
1145 if (!bh) {
1146 status = -ENOMEM;
1147 mlog_errno(status);
1148 goto out;
1149 }
8cb471e8 1150 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0e7f387b 1151
0584974a
JK
1152 /* Local quota info, chunk header and the new block we initialize */
1153 handle = ocfs2_start_trans(OCFS2_SB(sb),
1154 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1155 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
9e33d69f
JK
1156 if (IS_ERR(handle)) {
1157 status = PTR_ERR(handle);
1158 mlog_errno(status);
1159 goto out;
1160 }
0e7f387b 1161 /* Zero created block */
0cf2f763 1162 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387b
JK
1163 OCFS2_JOURNAL_ACCESS_CREATE);
1164 if (status < 0) {
1165 mlog_errno(status);
1166 goto out_trans;
1167 }
1168 lock_buffer(bh);
1169 memset(bh->b_data, 0, sb->s_blocksize);
1170 unlock_buffer(bh);
ec20cec7
JB
1171 ocfs2_journal_dirty(handle, bh);
1172
0e7f387b 1173 /* Update chunk header */
0cf2f763
JB
1174 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1175 chunk->qc_headerbh,
9e33d69f
JK
1176 OCFS2_JOURNAL_ACCESS_WRITE);
1177 if (status < 0) {
1178 mlog_errno(status);
1179 goto out_trans;
1180 }
1181
1182 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1183 lock_buffer(chunk->qc_headerbh);
1184 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1185 unlock_buffer(chunk->qc_headerbh);
ec20cec7
JB
1186 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1187
0e7f387b 1188 /* Update file header */
9e33d69f
JK
1189 oinfo->dqi_blocks++;
1190 status = ocfs2_local_write_info(sb, type);
1191 if (status < 0) {
1192 mlog_errno(status);
1193 goto out_trans;
1194 }
1195
1196 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1197 if (status < 0) {
1198 mlog_errno(status);
1199 goto out;
1200 }
1201 *offset = chunk_blocks * epb;
1202 return chunk;
1203out_trans:
1204 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1205out:
1206 return ERR_PTR(status);
1207}
1208
df32b334 1209static void olq_alloc_dquot(struct buffer_head *bh, void *private)
9e33d69f
JK
1210{
1211 int *offset = private;
1212 struct ocfs2_local_disk_chunk *dchunk;
1213
1214 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
93925579 1215 ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
9e33d69f
JK
1216 le32_add_cpu(&dchunk->dqc_free, -1);
1217}
1218
1219/* Create dquot in the local file for given id */
fb8dd8d7 1220int ocfs2_create_local_dquot(struct dquot *dquot)
9e33d69f
JK
1221{
1222 struct super_block *sb = dquot->dq_sb;
4c376dca 1223 int type = dquot->dq_id.type;
9e33d69f
JK
1224 struct inode *lqinode = sb_dqopt(sb)->files[type];
1225 struct ocfs2_quota_chunk *chunk;
1226 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1227 int offset;
1228 int status;
f64dd44e 1229 u64 pcount;
9e33d69f 1230
f64dd44e 1231 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f
JK
1232 chunk = ocfs2_find_free_entry(sb, type, &offset);
1233 if (!chunk) {
1234 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
f64dd44e
JK
1235 if (IS_ERR(chunk)) {
1236 status = PTR_ERR(chunk);
1237 goto out;
1238 }
9e33d69f 1239 } else if (IS_ERR(chunk)) {
f64dd44e
JK
1240 status = PTR_ERR(chunk);
1241 goto out;
9e33d69f
JK
1242 }
1243 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1244 od->dq_chunk = chunk;
f64dd44e
JK
1245 status = ocfs2_extent_map_get_blocks(lqinode,
1246 ol_dqblk_block(sb, chunk->qc_num, offset),
1247 &od->dq_local_phys_blk,
1248 &pcount,
1249 NULL);
1b13a703
AC
1250 if (status < 0) {
1251 mlog_errno(status);
1252 goto out;
1253 }
9e33d69f
JK
1254
1255 /* Initialize dquot structure on disk */
1256 status = ocfs2_local_write_dquot(dquot);
1257 if (status < 0) {
1258 mlog_errno(status);
1259 goto out;
1260 }
1261
1262 /* Mark structure as allocated */
1263 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1264 &offset);
1265 if (status < 0) {
1266 mlog_errno(status);
1267 goto out;
1268 }
1269out:
f64dd44e 1270 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f
JK
1271 return status;
1272}
1273
fb8dd8d7
JK
1274/*
1275 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1276 * already started a transaction and written all changes to global quota file
1277 */
1278int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
9e33d69f
JK
1279{
1280 int status;
4c376dca 1281 int type = dquot->dq_id.type;
9e33d69f
JK
1282 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1283 struct super_block *sb = dquot->dq_sb;
1284 struct ocfs2_local_disk_chunk *dchunk;
1285 int offset;
9e33d69f 1286
0cf2f763
JB
1287 status = ocfs2_journal_access_dq(handle,
1288 INODE_CACHE(sb_dqopt(sb)->files[type]),
9e33d69f
JK
1289 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1290 if (status < 0) {
1291 mlog_errno(status);
1292 goto out;
1293 }
1294 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1295 od->dq_local_off);
1296 dchunk = (struct ocfs2_local_disk_chunk *)
1297 (od->dq_chunk->qc_headerbh->b_data);
1298 /* Mark structure as freed */
1299 lock_buffer(od->dq_chunk->qc_headerbh);
93925579 1300 ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
9e33d69f
JK
1301 le32_add_cpu(&dchunk->dqc_free, 1);
1302 unlock_buffer(od->dq_chunk->qc_headerbh);
ec20cec7
JB
1303 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1304
9e33d69f 1305out:
9e33d69f
JK
1306 return status;
1307}
1308
1472da5f 1309static const struct quota_format_ops ocfs2_format_ops = {
9e33d69f
JK
1310 .check_quota_file = ocfs2_local_check_quota_file,
1311 .read_file_info = ocfs2_local_read_info,
1312 .write_file_info = ocfs2_global_write_info,
1313 .free_file_info = ocfs2_local_free_info,
9e33d69f
JK
1314};
1315
1316struct quota_format_type ocfs2_quota_format = {
1317 .qf_fmt_id = QFMT_OCFS2,
1318 .qf_ops = &ocfs2_format_ops,
1319 .qf_owner = THIS_MODULE
1320};