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