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