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