blob: a8f4cea1b824055ef24519829ded9347d2cf717f [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 Becker0cf2f762009-02-12 16:41:25 -0800111 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700112 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);
Joel Beckerec20cec2010-03-19 14:13:52 -0700121 ocfs2_journal_dirty(handle, bh);
122
Jan Kara9e33d692008-08-25 19:56:50 +0200123 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
124 if (status < 0) {
125 mlog_errno(status);
126 return status;
127 }
128 return 0;
129}
130
131/* Check whether we understand format of quota files */
132static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
133{
134 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
135 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
136 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
137 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
138 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
139 GROUP_QUOTA_SYSTEM_INODE };
Joel Becker85eb8b72008-11-25 15:31:27 +0100140 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200141 struct inode *linode = sb_dqopt(sb)->files[type];
142 struct inode *ginode = NULL;
143 struct ocfs2_disk_dqheader *dqhead;
144 int status, ret = 0;
145
146 /* First check whether we understand local quota file */
Joel Becker85eb8b72008-11-25 15:31:27 +0100147 status = ocfs2_read_quota_block(linode, 0, &bh);
148 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200149 mlog_errno(status);
150 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
151 type);
152 goto out_err;
153 }
154 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
155 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
156 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
157 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
158 lmagics[type], type);
159 goto out_err;
160 }
161 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
162 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
163 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
164 lversions[type], type);
165 goto out_err;
166 }
167 brelse(bh);
168 bh = NULL;
169
170 /* Next check whether we understand global quota file */
171 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
172 OCFS2_INVALID_SLOT);
173 if (!ginode) {
174 mlog(ML_ERROR, "cannot get global quota file inode "
175 "(type=%d)\n", type);
176 goto out_err;
177 }
178 /* Since the header is read only, we don't care about locking */
Joel Becker85eb8b72008-11-25 15:31:27 +0100179 status = ocfs2_read_quota_block(ginode, 0, &bh);
180 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200181 mlog_errno(status);
182 mlog(ML_ERROR, "failed to read global quota file header "
183 "(type=%d)\n", type);
184 goto out_err;
185 }
186 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
187 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
188 mlog(ML_ERROR, "global quota file magic does not match "
189 "(%u != %u), type=%d\n",
190 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
191 goto out_err;
192 }
193 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
194 mlog(ML_ERROR, "global quota file version does not match "
195 "(%u != %u), type=%d\n",
196 le32_to_cpu(dqhead->dqh_version), gversions[type],
197 type);
198 goto out_err;
199 }
200
201 ret = 1;
202out_err:
203 brelse(bh);
204 iput(ginode);
205 return ret;
206}
207
208/* Release given list of quota file chunks */
209static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
210{
211 struct ocfs2_quota_chunk *pos, *next;
212
213 list_for_each_entry_safe(pos, next, head, qc_chunk) {
214 list_del(&pos->qc_chunk);
215 brelse(pos->qc_headerbh);
216 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
217 }
218}
219
220/* Load quota bitmaps into memory */
221static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
222 struct ocfs2_local_disk_dqinfo *ldinfo,
223 struct list_head *head)
224{
225 struct ocfs2_quota_chunk *newchunk;
226 int i, status;
227
228 INIT_LIST_HEAD(head);
229 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
230 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
231 if (!newchunk) {
232 ocfs2_release_local_quota_bitmaps(head);
233 return -ENOMEM;
234 }
235 newchunk->qc_num = i;
Joel Becker85eb8b72008-11-25 15:31:27 +0100236 newchunk->qc_headerbh = NULL;
237 status = ocfs2_read_quota_block(inode,
Jan Kara9e33d692008-08-25 19:56:50 +0200238 ol_quota_chunk_block(inode->i_sb, i),
Joel Becker85eb8b72008-11-25 15:31:27 +0100239 &newchunk->qc_headerbh);
240 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200241 mlog_errno(status);
242 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
243 ocfs2_release_local_quota_bitmaps(head);
244 return status;
245 }
246 list_add_tail(&newchunk->qc_chunk, head);
247 }
248 return 0;
249}
250
251static void olq_update_info(struct buffer_head *bh, void *private)
252{
253 struct mem_dqinfo *info = private;
254 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
255 struct ocfs2_local_disk_dqinfo *ldinfo;
256
257 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
258 OCFS2_LOCAL_INFO_OFF);
259 spin_lock(&dq_data_lock);
260 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
261 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
262 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
263 spin_unlock(&dq_data_lock);
264}
265
Jan Kara22053632008-10-20 23:50:38 +0200266static int ocfs2_add_recovery_chunk(struct super_block *sb,
267 struct ocfs2_local_disk_chunk *dchunk,
268 int chunk,
269 struct list_head *head)
270{
271 struct ocfs2_recovery_chunk *rc;
272
273 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
274 if (!rc)
275 return -ENOMEM;
276 rc->rc_chunk = chunk;
277 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
278 if (!rc->rc_bitmap) {
279 kfree(rc);
280 return -ENOMEM;
281 }
282 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
283 (ol_chunk_entries(sb) + 7) >> 3);
284 list_add_tail(&rc->rc_list, head);
285 return 0;
286}
287
288static void free_recovery_list(struct list_head *head)
289{
290 struct ocfs2_recovery_chunk *next;
291 struct ocfs2_recovery_chunk *rchunk;
292
293 list_for_each_entry_safe(rchunk, next, head, rc_list) {
294 list_del(&rchunk->rc_list);
295 kfree(rchunk->rc_bitmap);
296 kfree(rchunk);
297 }
298}
299
300void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
301{
302 int type;
303
304 for (type = 0; type < MAXQUOTAS; type++)
305 free_recovery_list(&(rec->r_list[type]));
306 kfree(rec);
307}
308
309/* Load entries in our quota file we have to recover*/
310static int ocfs2_recovery_load_quota(struct inode *lqinode,
311 struct ocfs2_local_disk_dqinfo *ldinfo,
312 int type,
313 struct list_head *head)
314{
315 struct super_block *sb = lqinode->i_sb;
316 struct buffer_head *hbh;
317 struct ocfs2_local_disk_chunk *dchunk;
318 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
319 int status = 0;
320
321 for (i = 0; i < chunks; i++) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100322 hbh = NULL;
323 status = ocfs2_read_quota_block(lqinode,
324 ol_quota_chunk_block(sb, i),
325 &hbh);
326 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200327 mlog_errno(status);
328 break;
329 }
330 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
331 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
332 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
333 brelse(hbh);
334 if (status < 0)
335 break;
336 }
337 if (status < 0)
338 free_recovery_list(head);
339 return status;
340}
341
342static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
343{
344 int type;
345 struct ocfs2_quota_recovery *rec;
346
347 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
348 if (!rec)
349 return NULL;
350 for (type = 0; type < MAXQUOTAS; type++)
351 INIT_LIST_HEAD(&(rec->r_list[type]));
352 return rec;
353}
354
355/* Load information we need for quota recovery into memory */
356struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
357 struct ocfs2_super *osb,
358 int slot_num)
359{
360 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
361 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
362 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
363 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
364 struct super_block *sb = osb->sb;
365 struct ocfs2_local_disk_dqinfo *ldinfo;
366 struct inode *lqinode;
367 struct buffer_head *bh;
368 int type;
369 int status = 0;
370 struct ocfs2_quota_recovery *rec;
371
372 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
373 rec = ocfs2_alloc_quota_recovery();
374 if (!rec)
375 return ERR_PTR(-ENOMEM);
376 /* First init... */
377
378 for (type = 0; type < MAXQUOTAS; type++) {
379 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
380 continue;
381 /* At this point, journal of the slot is already replayed so
382 * we can trust metadata and data of the quota file */
383 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
384 if (!lqinode) {
385 status = -ENOENT;
386 goto out;
387 }
388 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
389 OCFS2_META_LOCK_RECOVERY);
390 if (status < 0) {
391 mlog_errno(status);
392 goto out_put;
393 }
394 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100395 bh = NULL;
396 status = ocfs2_read_quota_block(lqinode, 0, &bh);
397 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200398 mlog_errno(status);
399 mlog(ML_ERROR, "failed to read quota file info header "
400 "(slot=%d type=%d)\n", slot_num, type);
401 goto out_lock;
402 }
403 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
404 OCFS2_LOCAL_INFO_OFF);
405 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
406 &rec->r_list[type]);
407 brelse(bh);
408out_lock:
409 ocfs2_inode_unlock(lqinode, 1);
410out_put:
411 iput(lqinode);
412 if (status < 0)
413 break;
414 }
415out:
416 if (status < 0) {
417 ocfs2_free_quota_recovery(rec);
418 rec = ERR_PTR(status);
419 }
420 return rec;
421}
422
423/* Sync changes in local quota file into global quota file and
424 * reinitialize local quota file.
425 * The function expects local quota file to be already locked and
426 * dqonoff_mutex locked. */
427static int ocfs2_recover_local_quota_file(struct inode *lqinode,
428 int type,
429 struct ocfs2_quota_recovery *rec)
430{
431 struct super_block *sb = lqinode->i_sb;
432 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
433 struct ocfs2_local_disk_chunk *dchunk;
434 struct ocfs2_local_disk_dqblk *dqblk;
435 struct dquot *dquot;
436 handle_t *handle;
437 struct buffer_head *hbh = NULL, *qbh = NULL;
438 int status = 0;
439 int bit, chunk;
440 struct ocfs2_recovery_chunk *rchunk, *next;
441 qsize_t spacechange, inodechange;
442
443 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
444
Jan Kara22053632008-10-20 23:50:38 +0200445 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
446 chunk = rchunk->rc_chunk;
Joel Becker85eb8b72008-11-25 15:31:27 +0100447 hbh = NULL;
448 status = ocfs2_read_quota_block(lqinode,
449 ol_quota_chunk_block(sb, chunk),
450 &hbh);
451 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200452 mlog_errno(status);
453 break;
454 }
455 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
Akinobu Mita984b3f52010-03-05 13:41:37 -0800456 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100457 qbh = NULL;
458 status = ocfs2_read_quota_block(lqinode,
Jan Kara22053632008-10-20 23:50:38 +0200459 ol_dqblk_block(sb, chunk, bit),
Joel Becker85eb8b72008-11-25 15:31:27 +0100460 &qbh);
461 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200462 mlog_errno(status);
463 break;
464 }
465 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
466 ol_dqblk_block_off(sb, chunk, bit));
467 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
468 if (!dquot) {
469 status = -EIO;
470 mlog(ML_ERROR, "Failed to get quota structure "
471 "for id %u, type %d. Cannot finish quota "
472 "file recovery.\n",
473 (unsigned)le64_to_cpu(dqblk->dqb_id),
474 type);
475 goto out_put_bh;
476 }
Jan Kara80d73f12009-06-02 14:24:02 +0200477 status = ocfs2_lock_global_qf(oinfo, 1);
478 if (status < 0) {
479 mlog_errno(status);
480 goto out_put_dquot;
481 }
482
Jan Kara22053632008-10-20 23:50:38 +0200483 handle = ocfs2_start_trans(OCFS2_SB(sb),
484 OCFS2_QSYNC_CREDITS);
485 if (IS_ERR(handle)) {
486 status = PTR_ERR(handle);
487 mlog_errno(status);
Jan Kara80d73f12009-06-02 14:24:02 +0200488 goto out_drop_lock;
Jan Kara22053632008-10-20 23:50:38 +0200489 }
490 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
491 spin_lock(&dq_data_lock);
492 /* Add usage from quota entry into quota changes
493 * of our node. Auxiliary variables are important
494 * due to signedness */
495 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
496 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
497 dquot->dq_dqb.dqb_curspace += spacechange;
498 dquot->dq_dqb.dqb_curinodes += inodechange;
499 spin_unlock(&dq_data_lock);
500 /* We want to drop reference held by the crashed
501 * node. Since we have our own reference we know
502 * global structure actually won't be freed. */
503 status = ocfs2_global_release_dquot(dquot);
504 if (status < 0) {
505 mlog_errno(status);
506 goto out_commit;
507 }
508 /* Release local quota file entry */
Joel Becker0cf2f762009-02-12 16:41:25 -0800509 status = ocfs2_journal_access_dq(handle,
510 INODE_CACHE(lqinode),
Jan Kara22053632008-10-20 23:50:38 +0200511 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
512 if (status < 0) {
513 mlog_errno(status);
514 goto out_commit;
515 }
516 lock_buffer(qbh);
517 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
518 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
519 le32_add_cpu(&dchunk->dqc_free, 1);
520 unlock_buffer(qbh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700521 ocfs2_journal_dirty(handle, qbh);
Jan Kara22053632008-10-20 23:50:38 +0200522out_commit:
523 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
524 ocfs2_commit_trans(OCFS2_SB(sb), handle);
Jan Kara80d73f12009-06-02 14:24:02 +0200525out_drop_lock:
526 ocfs2_unlock_global_qf(oinfo, 1);
Jan Kara22053632008-10-20 23:50:38 +0200527out_put_dquot:
528 dqput(dquot);
529out_put_bh:
530 brelse(qbh);
531 if (status < 0)
532 break;
533 }
534 brelse(hbh);
535 list_del(&rchunk->rc_list);
536 kfree(rchunk->rc_bitmap);
537 kfree(rchunk);
538 if (status < 0)
539 break;
540 }
Jan Kara22053632008-10-20 23:50:38 +0200541 if (status < 0)
542 free_recovery_list(&(rec->r_list[type]));
543 mlog_exit(status);
544 return status;
545}
546
547/* Recover local quota files for given node different from us */
548int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
549 struct ocfs2_quota_recovery *rec,
550 int slot_num)
551{
552 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
553 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
554 struct super_block *sb = osb->sb;
555 struct ocfs2_local_disk_dqinfo *ldinfo;
556 struct buffer_head *bh;
557 handle_t *handle;
558 int type;
559 int status = 0;
560 struct inode *lqinode;
561 unsigned int flags;
562
563 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
564 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
565 for (type = 0; type < MAXQUOTAS; type++) {
566 if (list_empty(&(rec->r_list[type])))
567 continue;
568 mlog(0, "Recovering quota in slot %d\n", slot_num);
569 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
570 if (!lqinode) {
571 status = -ENOENT;
572 goto out;
573 }
574 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
575 OCFS2_META_LOCK_NOQUEUE);
576 /* Someone else is holding the lock? Then he must be
577 * doing the recovery. Just skip the file... */
578 if (status == -EAGAIN) {
579 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
580 "because quota file is locked.\n", slot_num);
581 status = 0;
582 goto out_put;
583 } else if (status < 0) {
584 mlog_errno(status);
585 goto out_put;
586 }
587 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100588 bh = NULL;
589 status = ocfs2_read_quota_block(lqinode, 0, &bh);
590 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200591 mlog_errno(status);
592 mlog(ML_ERROR, "failed to read quota file info header "
593 "(slot=%d type=%d)\n", slot_num, type);
594 goto out_lock;
595 }
596 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
597 OCFS2_LOCAL_INFO_OFF);
598 /* Is recovery still needed? */
599 flags = le32_to_cpu(ldinfo->dqi_flags);
600 if (!(flags & OLQF_CLEAN))
601 status = ocfs2_recover_local_quota_file(lqinode,
602 type,
603 rec);
604 /* We don't want to mark file as clean when it is actually
605 * active */
606 if (slot_num == osb->slot_num)
607 goto out_bh;
608 /* Mark quota file as clean if we are recovering quota file of
609 * some other node. */
Jan Kara05849742009-07-22 13:17:21 +0200610 handle = ocfs2_start_trans(osb,
611 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
Jan Kara22053632008-10-20 23:50:38 +0200612 if (IS_ERR(handle)) {
613 status = PTR_ERR(handle);
614 mlog_errno(status);
615 goto out_bh;
616 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800617 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
618 bh,
Joel Becker13723d02008-10-17 19:25:01 -0700619 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara22053632008-10-20 23:50:38 +0200620 if (status < 0) {
621 mlog_errno(status);
622 goto out_trans;
623 }
624 lock_buffer(bh);
625 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
626 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700627 ocfs2_journal_dirty(handle, bh);
Jan Kara22053632008-10-20 23:50:38 +0200628out_trans:
629 ocfs2_commit_trans(osb, handle);
630out_bh:
631 brelse(bh);
632out_lock:
633 ocfs2_inode_unlock(lqinode, 1);
634out_put:
635 iput(lqinode);
636 if (status < 0)
637 break;
638 }
639out:
640 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
641 kfree(rec);
642 return status;
643}
644
Jan Kara9e33d692008-08-25 19:56:50 +0200645/* Read information header from quota file */
646static int ocfs2_local_read_info(struct super_block *sb, int type)
647{
648 struct ocfs2_local_disk_dqinfo *ldinfo;
649 struct mem_dqinfo *info = sb_dqinfo(sb, type);
650 struct ocfs2_mem_dqinfo *oinfo;
651 struct inode *lqinode = sb_dqopt(sb)->files[type];
652 int status;
653 struct buffer_head *bh = NULL;
Jan Kara22053632008-10-20 23:50:38 +0200654 struct ocfs2_quota_recovery *rec;
Jan Kara9e33d692008-08-25 19:56:50 +0200655 int locked = 0;
656
Jan Karab4c30de2009-06-02 14:24:00 +0200657 /* We don't need the lock and we have to acquire quota file locks
658 * which will later depend on this lock */
659 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200660 info->dqi_maxblimit = 0x7fffffffffffffffLL;
661 info->dqi_maxilimit = 0x7fffffffffffffffLL;
662 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
663 if (!oinfo) {
664 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
665 " info.");
666 goto out_err;
667 }
668 info->dqi_priv = oinfo;
669 oinfo->dqi_type = type;
670 INIT_LIST_HEAD(&oinfo->dqi_chunk);
Jan Kara22053632008-10-20 23:50:38 +0200671 oinfo->dqi_rec = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200672 oinfo->dqi_lqi_bh = NULL;
673 oinfo->dqi_ibh = NULL;
674
675 status = ocfs2_global_read_info(sb, type);
676 if (status < 0)
677 goto out_err;
678
679 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
680 if (status < 0) {
681 mlog_errno(status);
682 goto out_err;
683 }
684 locked = 1;
685
686 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100687 status = ocfs2_read_quota_block(lqinode, 0, &bh);
688 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200689 mlog_errno(status);
690 mlog(ML_ERROR, "failed to read quota file info header "
691 "(type=%d)\n", type);
692 goto out_err;
693 }
694 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
695 OCFS2_LOCAL_INFO_OFF);
696 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
697 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
698 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
699 oinfo->dqi_ibh = bh;
700
701 /* We crashed when using local quota file? */
Jan Kara22053632008-10-20 23:50:38 +0200702 if (!(info->dqi_flags & OLQF_CLEAN)) {
703 rec = OCFS2_SB(sb)->quota_rec;
704 if (!rec) {
705 rec = ocfs2_alloc_quota_recovery();
706 if (!rec) {
707 status = -ENOMEM;
708 mlog_errno(status);
709 goto out_err;
710 }
711 OCFS2_SB(sb)->quota_rec = rec;
712 }
Jan Kara9e33d692008-08-25 19:56:50 +0200713
Jan Kara22053632008-10-20 23:50:38 +0200714 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
715 &rec->r_list[type]);
716 if (status < 0) {
717 mlog_errno(status);
718 goto out_err;
719 }
720 }
721
722 status = ocfs2_load_local_quota_bitmaps(lqinode,
Jan Kara9e33d692008-08-25 19:56:50 +0200723 ldinfo,
724 &oinfo->dqi_chunk);
725 if (status < 0) {
726 mlog_errno(status);
727 goto out_err;
728 }
729
730 /* Now mark quota file as used */
731 info->dqi_flags &= ~OLQF_CLEAN;
732 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
733 if (status < 0) {
734 mlog_errno(status);
735 goto out_err;
736 }
737
Jan Karab4c30de2009-06-02 14:24:00 +0200738 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200739 return 0;
740out_err:
741 if (oinfo) {
742 iput(oinfo->dqi_gqinode);
743 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
744 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
745 brelse(oinfo->dqi_lqi_bh);
746 if (locked)
747 ocfs2_inode_unlock(lqinode, 1);
748 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
749 kfree(oinfo);
750 }
751 brelse(bh);
Jan Karab4c30de2009-06-02 14:24:00 +0200752 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200753 return -1;
754}
755
756/* Write local info to quota file */
757static int ocfs2_local_write_info(struct super_block *sb, int type)
758{
759 struct mem_dqinfo *info = sb_dqinfo(sb, type);
760 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
761 ->dqi_ibh;
762 int status;
763
764 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
765 info);
766 if (status < 0) {
767 mlog_errno(status);
768 return -1;
769 }
770
771 return 0;
772}
773
774/* Release info from memory */
775static int ocfs2_local_free_info(struct super_block *sb, int type)
776{
777 struct mem_dqinfo *info = sb_dqinfo(sb, type);
778 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
779 struct ocfs2_quota_chunk *chunk;
780 struct ocfs2_local_disk_chunk *dchunk;
781 int mark_clean = 1, len;
782 int status;
783
Mark Fasheh171bf932008-10-20 15:36:47 +0200784 /* At this point we know there are no more dquots and thus
785 * even if there's some sync in the pdflush queue, it won't
786 * find any dquots and return without doing anything */
787 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Jan Kara9e33d692008-08-25 19:56:50 +0200788 iput(oinfo->dqi_gqinode);
789 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
790 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
791 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
792 dchunk = (struct ocfs2_local_disk_chunk *)
793 (chunk->qc_headerbh->b_data);
794 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
795 len = ol_chunk_entries(sb);
796 } else {
797 len = (oinfo->dqi_blocks -
798 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
799 * ol_quota_entries_per_block(sb);
800 }
801 /* Not all entries free? Bug! */
802 if (le32_to_cpu(dchunk->dqc_free) != len) {
803 mlog(ML_ERROR, "releasing quota file with used "
804 "entries (type=%d)\n", type);
805 mark_clean = 0;
806 }
807 }
808 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
809
Jan Kara22053632008-10-20 23:50:38 +0200810 /* dqonoff_mutex protects us against racing with recovery thread... */
811 if (oinfo->dqi_rec) {
812 ocfs2_free_quota_recovery(oinfo->dqi_rec);
813 mark_clean = 0;
814 }
815
Jan Kara9e33d692008-08-25 19:56:50 +0200816 if (!mark_clean)
817 goto out;
818
819 /* Mark local file as clean */
820 info->dqi_flags |= OLQF_CLEAN;
821 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
822 oinfo->dqi_ibh,
823 olq_update_info,
824 info);
825 if (status < 0) {
826 mlog_errno(status);
827 goto out;
828 }
829
830out:
831 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
832 brelse(oinfo->dqi_ibh);
833 brelse(oinfo->dqi_lqi_bh);
834 kfree(oinfo);
835 return 0;
836}
837
838static void olq_set_dquot(struct buffer_head *bh, void *private)
839{
840 struct ocfs2_dquot *od = private;
841 struct ocfs2_local_disk_dqblk *dqblk;
842 struct super_block *sb = od->dq_dquot.dq_sb;
843
844 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
845 + ol_dqblk_block_offset(sb, od->dq_local_off));
846
847 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
848 spin_lock(&dq_data_lock);
849 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
850 od->dq_origspace);
851 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
852 od->dq_originodes);
853 spin_unlock(&dq_data_lock);
854 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
Jan Kara9a2f3862008-11-25 15:31:30 +0100855 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
856 (long long)le64_to_cpu(dqblk->dqb_inodemod));
Jan Kara9e33d692008-08-25 19:56:50 +0200857}
858
859/* Write dquot to local quota file */
860static int ocfs2_local_write_dquot(struct dquot *dquot)
861{
862 struct super_block *sb = dquot->dq_sb;
863 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
Joel Becker85eb8b72008-11-25 15:31:27 +0100864 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200865 int status;
866
Joel Becker85eb8b72008-11-25 15:31:27 +0100867 status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
Jan Kara9e33d692008-08-25 19:56:50 +0200868 ol_dqblk_file_block(sb, od->dq_local_off),
Joel Becker85eb8b72008-11-25 15:31:27 +0100869 &bh);
870 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200871 mlog_errno(status);
872 goto out;
873 }
874 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
875 olq_set_dquot, od);
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;
Jan Kara0e7f3872009-07-22 13:17:17 +0200940 struct buffer_head *bh = NULL, *dbh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200941 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 }
Jan Kara05849742009-07-22 13:17:21 +0200964 /* 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);
Jan Kara9e33d692008-08-25 19:56:50 +0200968 if (IS_ERR(handle)) {
969 status = PTR_ERR(handle);
970 mlog_errno(status);
971 goto out;
972 }
973
Jan Kara0e7f3872009-07-22 13:17:17 +0200974 /* Initialize chunk header */
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);
981 goto out_trans;
982 }
983 bh = sb_getblk(sb, p_blkno);
984 if (!bh) {
985 status = -ENOMEM;
986 mlog_errno(status);
987 goto out_trans;
988 }
989 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Joel Becker8cb471e2009-02-10 20:00:41 -0800990 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Joel Becker0cf2f762009-02-12 16:41:25 -0800991 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +0200992 OCFS2_JOURNAL_ACCESS_CREATE);
Jan Kara9e33d692008-08-25 19:56:50 +0200993 if (status < 0) {
994 mlog_errno(status);
995 goto out_trans;
996 }
997 lock_buffer(bh);
Tao Madf32b332008-11-25 07:21:36 +0800998 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
Jan Kara9e33d692008-08-25 19:56:50 +0200999 memset(dchunk->dqc_bitmap, 0,
1000 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1001 OCFS2_QBLK_RESERVED_SPACE);
Jan Kara9e33d692008-08-25 19:56:50 +02001002 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001003 ocfs2_journal_dirty(handle, bh);
Jan Kara9e33d692008-08-25 19:56:50 +02001004
Jan Kara0e7f3872009-07-22 13:17:17 +02001005 /* 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 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001020 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001021 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001022 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);
Joel Beckerec20cec2010-03-19 14:13:52 -07001030 ocfs2_journal_dirty(handle, dbh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001031
1032 /* Update local quotafile info */
Jan Kara9e33d692008-08-25 19:56:50 +02001033 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);
Jan Kara0e7f3872009-07-22 13:17:17 +02001057 brelse(dbh);
Jan Kara9e33d692008-08-25 19:56:50 +02001058 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;
Jan Kara0e7f3872009-07-22 13:17:17 +02001075 struct buffer_head *bh;
1076 u64 p_blkno;
Jan Kara9e33d692008-08-25 19:56:50 +02001077 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 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001104
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 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001120 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001121
Jan Kara05849742009-07-22 13:17:21 +02001122 /* 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);
Jan Kara9e33d692008-08-25 19:56:50 +02001126 if (IS_ERR(handle)) {
1127 status = PTR_ERR(handle);
1128 mlog_errno(status);
1129 goto out;
1130 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001131 /* Zero created block */
Joel Becker0cf2f762009-02-12 16:41:25 -08001132 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001133 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);
Joel Beckerec20cec2010-03-19 14:13:52 -07001141 ocfs2_journal_dirty(handle, bh);
1142
Jan Kara0e7f3872009-07-22 13:17:17 +02001143 /* Update chunk header */
Joel Becker0cf2f762009-02-12 16:41:25 -08001144 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1145 chunk->qc_headerbh,
Jan Kara9e33d692008-08-25 19:56:50 +02001146 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);
Joel Beckerec20cec2010-03-19 14:13:52 -07001156 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1157
Jan Kara0e7f3872009-07-22 13:17:17 +02001158 /* Update file header */
Jan Kara9e33d692008-08-25 19:56:50 +02001159 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
Tao Madf32b332008-11-25 07:21:36 +08001179static void olq_alloc_dquot(struct buffer_head *bh, void *private)
Jan Kara9e33d692008-08-25 19:56:50 +02001180{
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;
1199
1200 chunk = ocfs2_find_free_entry(sb, type, &offset);
1201 if (!chunk) {
1202 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
1203 if (IS_ERR(chunk))
1204 return PTR_ERR(chunk);
1205 } else if (IS_ERR(chunk)) {
1206 return PTR_ERR(chunk);
1207 }
1208 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1209 od->dq_chunk = chunk;
1210
1211 /* Initialize dquot structure on disk */
1212 status = ocfs2_local_write_dquot(dquot);
1213 if (status < 0) {
1214 mlog_errno(status);
1215 goto out;
1216 }
1217
1218 /* Mark structure as allocated */
1219 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1220 &offset);
1221 if (status < 0) {
1222 mlog_errno(status);
1223 goto out;
1224 }
1225out:
1226 return status;
1227}
1228
1229/* Create entry in local file for dquot, load data from the global file */
1230static int ocfs2_local_read_dquot(struct dquot *dquot)
1231{
1232 int status;
1233
1234 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
1235
1236 status = ocfs2_global_read_dquot(dquot);
1237 if (status < 0) {
1238 mlog_errno(status);
1239 goto out_err;
1240 }
1241
1242 /* Now create entry in the local quota file */
1243 status = ocfs2_create_local_dquot(dquot);
1244 if (status < 0) {
1245 mlog_errno(status);
1246 goto out_err;
1247 }
1248 mlog_exit(0);
1249 return 0;
1250out_err:
1251 mlog_exit(status);
1252 return status;
1253}
1254
1255/* Release dquot structure from local quota file. ocfs2_release_dquot() has
1256 * already started a transaction and obtained exclusive lock for global
1257 * quota file. */
1258static int ocfs2_local_release_dquot(struct dquot *dquot)
1259{
1260 int status;
1261 int type = dquot->dq_type;
1262 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1263 struct super_block *sb = dquot->dq_sb;
1264 struct ocfs2_local_disk_chunk *dchunk;
1265 int offset;
1266 handle_t *handle = journal_current_handle();
1267
1268 BUG_ON(!handle);
1269 /* First write all local changes to global file */
1270 status = ocfs2_global_release_dquot(dquot);
1271 if (status < 0) {
1272 mlog_errno(status);
1273 goto out;
1274 }
1275
Joel Becker0cf2f762009-02-12 16:41:25 -08001276 status = ocfs2_journal_access_dq(handle,
1277 INODE_CACHE(sb_dqopt(sb)->files[type]),
Jan Kara9e33d692008-08-25 19:56:50 +02001278 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1279 if (status < 0) {
1280 mlog_errno(status);
1281 goto out;
1282 }
1283 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1284 od->dq_local_off);
1285 dchunk = (struct ocfs2_local_disk_chunk *)
1286 (od->dq_chunk->qc_headerbh->b_data);
1287 /* Mark structure as freed */
1288 lock_buffer(od->dq_chunk->qc_headerbh);
1289 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1290 le32_add_cpu(&dchunk->dqc_free, 1);
1291 unlock_buffer(od->dq_chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001292 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1293
Jan Kara9e33d692008-08-25 19:56:50 +02001294out:
1295 /* Clear the read bit so that next time someone uses this
1296 * dquot he reads fresh info from disk and allocates local
1297 * dquot structure */
1298 clear_bit(DQ_READ_B, &dquot->dq_flags);
1299 return status;
1300}
1301
Alexey Dobriyan1472da52009-10-16 15:26:03 +04001302static const struct quota_format_ops ocfs2_format_ops = {
Jan Kara9e33d692008-08-25 19:56:50 +02001303 .check_quota_file = ocfs2_local_check_quota_file,
1304 .read_file_info = ocfs2_local_read_info,
1305 .write_file_info = ocfs2_global_write_info,
1306 .free_file_info = ocfs2_local_free_info,
1307 .read_dqblk = ocfs2_local_read_dquot,
1308 .commit_dqblk = ocfs2_local_write_dquot,
1309 .release_dqblk = ocfs2_local_release_dquot,
1310};
1311
1312struct quota_format_type ocfs2_quota_format = {
1313 .qf_fmt_id = QFMT_OCFS2,
1314 .qf_ops = &ocfs2_format_ops,
1315 .qf_owner = THIS_MODULE
1316};