blob: 778947f0e951af0000a0d4edad9da7c9d3cc7ca6 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Jan Kara9e33d692008-08-25 19:56:50 +02007#include <linux/quota.h>
8#include <linux/quotaops.h>
9#include <linux/module.h>
10
11#define MLOG_MASK_PREFIX ML_QUOTA
12#include <cluster/masklog.h>
13
14#include "ocfs2_fs.h"
15#include "ocfs2.h"
16#include "inode.h"
17#include "alloc.h"
18#include "file.h"
19#include "buffer_head_io.h"
20#include "journal.h"
21#include "sysfile.h"
22#include "dlmglue.h"
23#include "quota.h"
Jan Kara4b3fa192009-07-22 13:17:16 +020024#include "uptodate.h"
Jan Karafb8dd8d2010-03-31 16:25:37 +020025#include "super.h"
Jan Kara9e33d692008-08-25 19:56:50 +020026
27/* Number of local quota structures per block */
28static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
29{
30 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
31 sizeof(struct ocfs2_local_disk_dqblk));
32}
33
34/* Number of blocks with entries in one chunk */
35static inline unsigned int ol_chunk_blocks(struct super_block *sb)
36{
37 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
38 OCFS2_QBLK_RESERVED_SPACE) << 3) /
39 ol_quota_entries_per_block(sb);
40}
41
42/* Number of entries in a chunk bitmap */
43static unsigned int ol_chunk_entries(struct super_block *sb)
44{
45 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
46}
47
48/* Offset of the chunk in quota file */
49static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
50{
51 /* 1 block for local quota file info, 1 block per chunk for chunk info */
52 return 1 + (ol_chunk_blocks(sb) + 1) * c;
53}
54
Jan Kara22053632008-10-20 23:50:38 +020055static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
Jan Kara9e33d692008-08-25 19:56:50 +020056{
57 int epb = ol_quota_entries_per_block(sb);
58
Jan Kara22053632008-10-20 23:50:38 +020059 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
60}
61
62static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
63{
64 int epb = ol_quota_entries_per_block(sb);
65
66 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
67}
68
69/* Offset of the dquot structure in the quota file */
70static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
71{
72 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
73 ol_dqblk_block_off(sb, c, off);
Jan Kara9e33d692008-08-25 19:56:50 +020074}
75
76/* Compute block number from given offset */
77static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
78{
79 return off >> sb->s_blocksize_bits;
80}
81
82static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
83{
84 return off & ((1 << sb->s_blocksize_bits) - 1);
85}
86
87/* Compute offset in the chunk of a structure with the given offset */
88static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
89{
90 int epb = ol_quota_entries_per_block(sb);
91
92 return ((off >> sb->s_blocksize_bits) -
93 ol_quota_chunk_block(sb, c) - 1) * epb
94 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
95 sizeof(struct ocfs2_local_disk_dqblk);
96}
97
98/* Write bufferhead into the fs */
99static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
100 void (*modify)(struct buffer_head *, void *), void *private)
101{
102 struct super_block *sb = inode->i_sb;
103 handle_t *handle;
104 int status;
105
Jan Kara05849742009-07-22 13:17:21 +0200106 handle = ocfs2_start_trans(OCFS2_SB(sb),
107 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +0200108 if (IS_ERR(handle)) {
109 status = PTR_ERR(handle);
110 mlog_errno(status);
111 return status;
112 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800113 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700114 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara9e33d692008-08-25 19:56:50 +0200115 if (status < 0) {
116 mlog_errno(status);
117 ocfs2_commit_trans(OCFS2_SB(sb), handle);
118 return status;
119 }
120 lock_buffer(bh);
121 modify(bh, private);
122 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700123 ocfs2_journal_dirty(handle, bh);
124
Jan Kara9e33d692008-08-25 19:56:50 +0200125 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
126 if (status < 0) {
127 mlog_errno(status);
128 return status;
129 }
130 return 0;
131}
132
Jan Karafb8dd8d2010-03-31 16:25:37 +0200133/*
134 * Read quota block from a given logical offset.
135 *
136 * This function acquires ip_alloc_sem and thus it must not be called with a
137 * transaction started.
138 */
139static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
140 struct buffer_head **bh)
141{
142 int rc = 0;
143 struct buffer_head *tmp = *bh;
144
145 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
146 ocfs2_error(inode->i_sb,
147 "Quota file %llu is probably corrupted! Requested "
148 "to read block %Lu but file has size only %Lu\n",
149 (unsigned long long)OCFS2_I(inode)->ip_blkno,
150 (unsigned long long)v_block,
151 (unsigned long long)i_size_read(inode));
152 return -EIO;
153 }
154 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
155 ocfs2_validate_quota_block);
156 if (rc)
157 mlog_errno(rc);
158
159 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
160 if (!rc && !*bh)
161 *bh = tmp;
162
163 return rc;
164}
165
Jan Kara9e33d692008-08-25 19:56:50 +0200166/* Check whether we understand format of quota files */
167static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
168{
169 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
170 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
171 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
172 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
173 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
174 GROUP_QUOTA_SYSTEM_INODE };
Joel Becker85eb8b72008-11-25 15:31:27 +0100175 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200176 struct inode *linode = sb_dqopt(sb)->files[type];
177 struct inode *ginode = NULL;
178 struct ocfs2_disk_dqheader *dqhead;
179 int status, ret = 0;
180
181 /* First check whether we understand local quota file */
Joel Becker85eb8b72008-11-25 15:31:27 +0100182 status = ocfs2_read_quota_block(linode, 0, &bh);
183 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200184 mlog_errno(status);
185 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
186 type);
187 goto out_err;
188 }
189 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
190 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
191 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
192 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
193 lmagics[type], type);
194 goto out_err;
195 }
196 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
197 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
198 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
199 lversions[type], type);
200 goto out_err;
201 }
202 brelse(bh);
203 bh = NULL;
204
205 /* Next check whether we understand global quota file */
206 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
207 OCFS2_INVALID_SLOT);
208 if (!ginode) {
209 mlog(ML_ERROR, "cannot get global quota file inode "
210 "(type=%d)\n", type);
211 goto out_err;
212 }
213 /* Since the header is read only, we don't care about locking */
Joel Becker85eb8b72008-11-25 15:31:27 +0100214 status = ocfs2_read_quota_block(ginode, 0, &bh);
215 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200216 mlog_errno(status);
217 mlog(ML_ERROR, "failed to read global quota file header "
218 "(type=%d)\n", type);
219 goto out_err;
220 }
221 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
222 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
223 mlog(ML_ERROR, "global quota file magic does not match "
224 "(%u != %u), type=%d\n",
225 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
226 goto out_err;
227 }
228 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
229 mlog(ML_ERROR, "global quota file version does not match "
230 "(%u != %u), type=%d\n",
231 le32_to_cpu(dqhead->dqh_version), gversions[type],
232 type);
233 goto out_err;
234 }
235
236 ret = 1;
237out_err:
238 brelse(bh);
239 iput(ginode);
240 return ret;
241}
242
243/* Release given list of quota file chunks */
244static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
245{
246 struct ocfs2_quota_chunk *pos, *next;
247
248 list_for_each_entry_safe(pos, next, head, qc_chunk) {
249 list_del(&pos->qc_chunk);
250 brelse(pos->qc_headerbh);
251 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
252 }
253}
254
255/* Load quota bitmaps into memory */
256static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
257 struct ocfs2_local_disk_dqinfo *ldinfo,
258 struct list_head *head)
259{
260 struct ocfs2_quota_chunk *newchunk;
261 int i, status;
262
263 INIT_LIST_HEAD(head);
264 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
265 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
266 if (!newchunk) {
267 ocfs2_release_local_quota_bitmaps(head);
268 return -ENOMEM;
269 }
270 newchunk->qc_num = i;
Joel Becker85eb8b72008-11-25 15:31:27 +0100271 newchunk->qc_headerbh = NULL;
272 status = ocfs2_read_quota_block(inode,
Jan Kara9e33d692008-08-25 19:56:50 +0200273 ol_quota_chunk_block(inode->i_sb, i),
Joel Becker85eb8b72008-11-25 15:31:27 +0100274 &newchunk->qc_headerbh);
275 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200276 mlog_errno(status);
277 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
278 ocfs2_release_local_quota_bitmaps(head);
279 return status;
280 }
281 list_add_tail(&newchunk->qc_chunk, head);
282 }
283 return 0;
284}
285
286static void olq_update_info(struct buffer_head *bh, void *private)
287{
288 struct mem_dqinfo *info = private;
289 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
290 struct ocfs2_local_disk_dqinfo *ldinfo;
291
292 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
293 OCFS2_LOCAL_INFO_OFF);
294 spin_lock(&dq_data_lock);
295 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
296 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
297 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
298 spin_unlock(&dq_data_lock);
299}
300
Jan Kara22053632008-10-20 23:50:38 +0200301static int ocfs2_add_recovery_chunk(struct super_block *sb,
302 struct ocfs2_local_disk_chunk *dchunk,
303 int chunk,
304 struct list_head *head)
305{
306 struct ocfs2_recovery_chunk *rc;
307
308 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
309 if (!rc)
310 return -ENOMEM;
311 rc->rc_chunk = chunk;
312 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
313 if (!rc->rc_bitmap) {
314 kfree(rc);
315 return -ENOMEM;
316 }
317 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
318 (ol_chunk_entries(sb) + 7) >> 3);
319 list_add_tail(&rc->rc_list, head);
320 return 0;
321}
322
323static void free_recovery_list(struct list_head *head)
324{
325 struct ocfs2_recovery_chunk *next;
326 struct ocfs2_recovery_chunk *rchunk;
327
328 list_for_each_entry_safe(rchunk, next, head, rc_list) {
329 list_del(&rchunk->rc_list);
330 kfree(rchunk->rc_bitmap);
331 kfree(rchunk);
332 }
333}
334
335void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
336{
337 int type;
338
339 for (type = 0; type < MAXQUOTAS; type++)
340 free_recovery_list(&(rec->r_list[type]));
341 kfree(rec);
342}
343
344/* Load entries in our quota file we have to recover*/
345static int ocfs2_recovery_load_quota(struct inode *lqinode,
346 struct ocfs2_local_disk_dqinfo *ldinfo,
347 int type,
348 struct list_head *head)
349{
350 struct super_block *sb = lqinode->i_sb;
351 struct buffer_head *hbh;
352 struct ocfs2_local_disk_chunk *dchunk;
353 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
354 int status = 0;
355
356 for (i = 0; i < chunks; i++) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100357 hbh = NULL;
358 status = ocfs2_read_quota_block(lqinode,
359 ol_quota_chunk_block(sb, i),
360 &hbh);
361 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200362 mlog_errno(status);
363 break;
364 }
365 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
366 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
367 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
368 brelse(hbh);
369 if (status < 0)
370 break;
371 }
372 if (status < 0)
373 free_recovery_list(head);
374 return status;
375}
376
377static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
378{
379 int type;
380 struct ocfs2_quota_recovery *rec;
381
382 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
383 if (!rec)
384 return NULL;
385 for (type = 0; type < MAXQUOTAS; type++)
386 INIT_LIST_HEAD(&(rec->r_list[type]));
387 return rec;
388}
389
390/* Load information we need for quota recovery into memory */
391struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
392 struct ocfs2_super *osb,
393 int slot_num)
394{
395 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
396 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
397 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
398 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
399 struct super_block *sb = osb->sb;
400 struct ocfs2_local_disk_dqinfo *ldinfo;
401 struct inode *lqinode;
402 struct buffer_head *bh;
403 int type;
404 int status = 0;
405 struct ocfs2_quota_recovery *rec;
406
407 mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
408 rec = ocfs2_alloc_quota_recovery();
409 if (!rec)
410 return ERR_PTR(-ENOMEM);
411 /* First init... */
412
413 for (type = 0; type < MAXQUOTAS; type++) {
414 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
415 continue;
416 /* At this point, journal of the slot is already replayed so
417 * we can trust metadata and data of the quota file */
418 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
419 if (!lqinode) {
420 status = -ENOENT;
421 goto out;
422 }
423 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
424 OCFS2_META_LOCK_RECOVERY);
425 if (status < 0) {
426 mlog_errno(status);
427 goto out_put;
428 }
429 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100430 bh = NULL;
431 status = ocfs2_read_quota_block(lqinode, 0, &bh);
432 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200433 mlog_errno(status);
434 mlog(ML_ERROR, "failed to read quota file info header "
435 "(slot=%d type=%d)\n", slot_num, type);
436 goto out_lock;
437 }
438 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
439 OCFS2_LOCAL_INFO_OFF);
440 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
441 &rec->r_list[type]);
442 brelse(bh);
443out_lock:
444 ocfs2_inode_unlock(lqinode, 1);
445out_put:
446 iput(lqinode);
447 if (status < 0)
448 break;
449 }
450out:
451 if (status < 0) {
452 ocfs2_free_quota_recovery(rec);
453 rec = ERR_PTR(status);
454 }
455 return rec;
456}
457
458/* Sync changes in local quota file into global quota file and
459 * reinitialize local quota file.
460 * The function expects local quota file to be already locked and
461 * dqonoff_mutex locked. */
462static int ocfs2_recover_local_quota_file(struct inode *lqinode,
463 int type,
464 struct ocfs2_quota_recovery *rec)
465{
466 struct super_block *sb = lqinode->i_sb;
467 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
468 struct ocfs2_local_disk_chunk *dchunk;
469 struct ocfs2_local_disk_dqblk *dqblk;
470 struct dquot *dquot;
471 handle_t *handle;
472 struct buffer_head *hbh = NULL, *qbh = NULL;
473 int status = 0;
474 int bit, chunk;
475 struct ocfs2_recovery_chunk *rchunk, *next;
476 qsize_t spacechange, inodechange;
477
478 mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
479
Jan Kara22053632008-10-20 23:50:38 +0200480 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
481 chunk = rchunk->rc_chunk;
Joel Becker85eb8b72008-11-25 15:31:27 +0100482 hbh = NULL;
483 status = ocfs2_read_quota_block(lqinode,
484 ol_quota_chunk_block(sb, chunk),
485 &hbh);
486 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200487 mlog_errno(status);
488 break;
489 }
490 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
Akinobu Mita984b3f52010-03-05 13:41:37 -0800491 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100492 qbh = NULL;
493 status = ocfs2_read_quota_block(lqinode,
Jan Kara22053632008-10-20 23:50:38 +0200494 ol_dqblk_block(sb, chunk, bit),
Joel Becker85eb8b72008-11-25 15:31:27 +0100495 &qbh);
496 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200497 mlog_errno(status);
498 break;
499 }
500 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
501 ol_dqblk_block_off(sb, chunk, bit));
502 dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
503 if (!dquot) {
504 status = -EIO;
505 mlog(ML_ERROR, "Failed to get quota structure "
506 "for id %u, type %d. Cannot finish quota "
507 "file recovery.\n",
508 (unsigned)le64_to_cpu(dqblk->dqb_id),
509 type);
510 goto out_put_bh;
511 }
Jan Kara80d73f12009-06-02 14:24:02 +0200512 status = ocfs2_lock_global_qf(oinfo, 1);
513 if (status < 0) {
514 mlog_errno(status);
515 goto out_put_dquot;
516 }
517
Jan Kara22053632008-10-20 23:50:38 +0200518 handle = ocfs2_start_trans(OCFS2_SB(sb),
519 OCFS2_QSYNC_CREDITS);
520 if (IS_ERR(handle)) {
521 status = PTR_ERR(handle);
522 mlog_errno(status);
Jan Kara80d73f12009-06-02 14:24:02 +0200523 goto out_drop_lock;
Jan Kara22053632008-10-20 23:50:38 +0200524 }
525 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
526 spin_lock(&dq_data_lock);
527 /* Add usage from quota entry into quota changes
528 * of our node. Auxiliary variables are important
529 * due to signedness */
530 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
531 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
532 dquot->dq_dqb.dqb_curspace += spacechange;
533 dquot->dq_dqb.dqb_curinodes += inodechange;
534 spin_unlock(&dq_data_lock);
535 /* We want to drop reference held by the crashed
536 * node. Since we have our own reference we know
537 * global structure actually won't be freed. */
538 status = ocfs2_global_release_dquot(dquot);
539 if (status < 0) {
540 mlog_errno(status);
541 goto out_commit;
542 }
543 /* Release local quota file entry */
Joel Becker0cf2f762009-02-12 16:41:25 -0800544 status = ocfs2_journal_access_dq(handle,
545 INODE_CACHE(lqinode),
Jan Kara22053632008-10-20 23:50:38 +0200546 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
547 if (status < 0) {
548 mlog_errno(status);
549 goto out_commit;
550 }
551 lock_buffer(qbh);
552 WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
553 ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
554 le32_add_cpu(&dchunk->dqc_free, 1);
555 unlock_buffer(qbh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700556 ocfs2_journal_dirty(handle, qbh);
Jan Kara22053632008-10-20 23:50:38 +0200557out_commit:
558 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
559 ocfs2_commit_trans(OCFS2_SB(sb), handle);
Jan Kara80d73f12009-06-02 14:24:02 +0200560out_drop_lock:
561 ocfs2_unlock_global_qf(oinfo, 1);
Jan Kara22053632008-10-20 23:50:38 +0200562out_put_dquot:
563 dqput(dquot);
564out_put_bh:
565 brelse(qbh);
566 if (status < 0)
567 break;
568 }
569 brelse(hbh);
570 list_del(&rchunk->rc_list);
571 kfree(rchunk->rc_bitmap);
572 kfree(rchunk);
573 if (status < 0)
574 break;
575 }
Jan Kara22053632008-10-20 23:50:38 +0200576 if (status < 0)
577 free_recovery_list(&(rec->r_list[type]));
578 mlog_exit(status);
579 return status;
580}
581
582/* Recover local quota files for given node different from us */
583int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
584 struct ocfs2_quota_recovery *rec,
585 int slot_num)
586{
587 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
588 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
589 struct super_block *sb = osb->sb;
590 struct ocfs2_local_disk_dqinfo *ldinfo;
591 struct buffer_head *bh;
592 handle_t *handle;
593 int type;
594 int status = 0;
595 struct inode *lqinode;
596 unsigned int flags;
597
598 mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
599 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
600 for (type = 0; type < MAXQUOTAS; type++) {
601 if (list_empty(&(rec->r_list[type])))
602 continue;
603 mlog(0, "Recovering quota in slot %d\n", slot_num);
604 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
605 if (!lqinode) {
606 status = -ENOENT;
607 goto out;
608 }
609 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
610 OCFS2_META_LOCK_NOQUEUE);
611 /* Someone else is holding the lock? Then he must be
612 * doing the recovery. Just skip the file... */
613 if (status == -EAGAIN) {
614 mlog(ML_NOTICE, "skipping quota recovery for slot %d "
615 "because quota file is locked.\n", slot_num);
616 status = 0;
617 goto out_put;
618 } else if (status < 0) {
619 mlog_errno(status);
620 goto out_put;
621 }
622 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100623 bh = NULL;
624 status = ocfs2_read_quota_block(lqinode, 0, &bh);
625 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200626 mlog_errno(status);
627 mlog(ML_ERROR, "failed to read quota file info header "
628 "(slot=%d type=%d)\n", slot_num, type);
629 goto out_lock;
630 }
631 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
632 OCFS2_LOCAL_INFO_OFF);
633 /* Is recovery still needed? */
634 flags = le32_to_cpu(ldinfo->dqi_flags);
635 if (!(flags & OLQF_CLEAN))
636 status = ocfs2_recover_local_quota_file(lqinode,
637 type,
638 rec);
639 /* We don't want to mark file as clean when it is actually
640 * active */
641 if (slot_num == osb->slot_num)
642 goto out_bh;
643 /* Mark quota file as clean if we are recovering quota file of
644 * some other node. */
Jan Kara05849742009-07-22 13:17:21 +0200645 handle = ocfs2_start_trans(osb,
646 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
Jan Kara22053632008-10-20 23:50:38 +0200647 if (IS_ERR(handle)) {
648 status = PTR_ERR(handle);
649 mlog_errno(status);
650 goto out_bh;
651 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800652 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
653 bh,
Joel Becker13723d02008-10-17 19:25:01 -0700654 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara22053632008-10-20 23:50:38 +0200655 if (status < 0) {
656 mlog_errno(status);
657 goto out_trans;
658 }
659 lock_buffer(bh);
660 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
661 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700662 ocfs2_journal_dirty(handle, bh);
Jan Kara22053632008-10-20 23:50:38 +0200663out_trans:
664 ocfs2_commit_trans(osb, handle);
665out_bh:
666 brelse(bh);
667out_lock:
668 ocfs2_inode_unlock(lqinode, 1);
669out_put:
670 iput(lqinode);
671 if (status < 0)
672 break;
673 }
674out:
675 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
676 kfree(rec);
677 return status;
678}
679
Jan Kara9e33d692008-08-25 19:56:50 +0200680/* Read information header from quota file */
681static int ocfs2_local_read_info(struct super_block *sb, int type)
682{
683 struct ocfs2_local_disk_dqinfo *ldinfo;
684 struct mem_dqinfo *info = sb_dqinfo(sb, type);
685 struct ocfs2_mem_dqinfo *oinfo;
686 struct inode *lqinode = sb_dqopt(sb)->files[type];
687 int status;
688 struct buffer_head *bh = NULL;
Jan Kara22053632008-10-20 23:50:38 +0200689 struct ocfs2_quota_recovery *rec;
Jan Kara9e33d692008-08-25 19:56:50 +0200690 int locked = 0;
691
Jan Karab4c30de2009-06-02 14:24:00 +0200692 /* We don't need the lock and we have to acquire quota file locks
693 * which will later depend on this lock */
694 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200695 info->dqi_maxblimit = 0x7fffffffffffffffLL;
696 info->dqi_maxilimit = 0x7fffffffffffffffLL;
697 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
698 if (!oinfo) {
699 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
700 " info.");
701 goto out_err;
702 }
703 info->dqi_priv = oinfo;
704 oinfo->dqi_type = type;
705 INIT_LIST_HEAD(&oinfo->dqi_chunk);
Jan Kara22053632008-10-20 23:50:38 +0200706 oinfo->dqi_rec = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200707 oinfo->dqi_lqi_bh = NULL;
Jan Karaae4f6ef2010-04-28 19:04:29 +0200708 oinfo->dqi_libh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200709
710 status = ocfs2_global_read_info(sb, type);
711 if (status < 0)
712 goto out_err;
713
714 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
715 if (status < 0) {
716 mlog_errno(status);
717 goto out_err;
718 }
719 locked = 1;
720
721 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100722 status = ocfs2_read_quota_block(lqinode, 0, &bh);
723 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200724 mlog_errno(status);
725 mlog(ML_ERROR, "failed to read quota file info header "
726 "(type=%d)\n", type);
727 goto out_err;
728 }
729 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
730 OCFS2_LOCAL_INFO_OFF);
731 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
732 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
733 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200734 oinfo->dqi_libh = bh;
Jan Kara9e33d692008-08-25 19:56:50 +0200735
736 /* We crashed when using local quota file? */
Jan Kara22053632008-10-20 23:50:38 +0200737 if (!(info->dqi_flags & OLQF_CLEAN)) {
738 rec = OCFS2_SB(sb)->quota_rec;
739 if (!rec) {
740 rec = ocfs2_alloc_quota_recovery();
741 if (!rec) {
742 status = -ENOMEM;
743 mlog_errno(status);
744 goto out_err;
745 }
746 OCFS2_SB(sb)->quota_rec = rec;
747 }
Jan Kara9e33d692008-08-25 19:56:50 +0200748
Jan Kara22053632008-10-20 23:50:38 +0200749 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
750 &rec->r_list[type]);
751 if (status < 0) {
752 mlog_errno(status);
753 goto out_err;
754 }
755 }
756
757 status = ocfs2_load_local_quota_bitmaps(lqinode,
Jan Kara9e33d692008-08-25 19:56:50 +0200758 ldinfo,
759 &oinfo->dqi_chunk);
760 if (status < 0) {
761 mlog_errno(status);
762 goto out_err;
763 }
764
765 /* Now mark quota file as used */
766 info->dqi_flags &= ~OLQF_CLEAN;
767 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
768 if (status < 0) {
769 mlog_errno(status);
770 goto out_err;
771 }
772
Jan Karab4c30de2009-06-02 14:24:00 +0200773 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200774 return 0;
775out_err:
776 if (oinfo) {
777 iput(oinfo->dqi_gqinode);
778 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
779 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
780 brelse(oinfo->dqi_lqi_bh);
781 if (locked)
782 ocfs2_inode_unlock(lqinode, 1);
783 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
784 kfree(oinfo);
785 }
786 brelse(bh);
Jan Karab4c30de2009-06-02 14:24:00 +0200787 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200788 return -1;
789}
790
791/* Write local info to quota file */
792static int ocfs2_local_write_info(struct super_block *sb, int type)
793{
794 struct mem_dqinfo *info = sb_dqinfo(sb, type);
795 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
Jan Karaae4f6ef2010-04-28 19:04:29 +0200796 ->dqi_libh;
Jan Kara9e33d692008-08-25 19:56:50 +0200797 int status;
798
799 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
800 info);
801 if (status < 0) {
802 mlog_errno(status);
803 return -1;
804 }
805
806 return 0;
807}
808
809/* Release info from memory */
810static int ocfs2_local_free_info(struct super_block *sb, int type)
811{
812 struct mem_dqinfo *info = sb_dqinfo(sb, type);
813 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
814 struct ocfs2_quota_chunk *chunk;
815 struct ocfs2_local_disk_chunk *dchunk;
816 int mark_clean = 1, len;
817 int status;
818
Mark Fasheh171bf932008-10-20 15:36:47 +0200819 /* At this point we know there are no more dquots and thus
820 * even if there's some sync in the pdflush queue, it won't
821 * find any dquots and return without doing anything */
822 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Jan Kara9e33d692008-08-25 19:56:50 +0200823 iput(oinfo->dqi_gqinode);
824 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
825 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
826 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
827 dchunk = (struct ocfs2_local_disk_chunk *)
828 (chunk->qc_headerbh->b_data);
829 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
830 len = ol_chunk_entries(sb);
831 } else {
832 len = (oinfo->dqi_blocks -
833 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
834 * ol_quota_entries_per_block(sb);
835 }
836 /* Not all entries free? Bug! */
837 if (le32_to_cpu(dchunk->dqc_free) != len) {
838 mlog(ML_ERROR, "releasing quota file with used "
839 "entries (type=%d)\n", type);
840 mark_clean = 0;
841 }
842 }
843 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
844
Jan Kara22053632008-10-20 23:50:38 +0200845 /* dqonoff_mutex protects us against racing with recovery thread... */
846 if (oinfo->dqi_rec) {
847 ocfs2_free_quota_recovery(oinfo->dqi_rec);
848 mark_clean = 0;
849 }
850
Jan Kara9e33d692008-08-25 19:56:50 +0200851 if (!mark_clean)
852 goto out;
853
854 /* Mark local file as clean */
855 info->dqi_flags |= OLQF_CLEAN;
856 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
Jan Karaae4f6ef2010-04-28 19:04:29 +0200857 oinfo->dqi_libh,
Jan Kara9e33d692008-08-25 19:56:50 +0200858 olq_update_info,
859 info);
860 if (status < 0) {
861 mlog_errno(status);
862 goto out;
863 }
864
865out:
866 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200867 brelse(oinfo->dqi_libh);
Jan Kara9e33d692008-08-25 19:56:50 +0200868 brelse(oinfo->dqi_lqi_bh);
869 kfree(oinfo);
870 return 0;
871}
872
873static void olq_set_dquot(struct buffer_head *bh, void *private)
874{
875 struct ocfs2_dquot *od = private;
876 struct ocfs2_local_disk_dqblk *dqblk;
877 struct super_block *sb = od->dq_dquot.dq_sb;
878
879 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
880 + ol_dqblk_block_offset(sb, od->dq_local_off));
881
882 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
883 spin_lock(&dq_data_lock);
884 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
885 od->dq_origspace);
886 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
887 od->dq_originodes);
888 spin_unlock(&dq_data_lock);
889 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
Jan Kara9a2f3862008-11-25 15:31:30 +0100890 od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
891 (long long)le64_to_cpu(dqblk->dqb_inodemod));
Jan Kara9e33d692008-08-25 19:56:50 +0200892}
893
894/* Write dquot to local quota file */
895static int ocfs2_local_write_dquot(struct dquot *dquot)
896{
897 struct super_block *sb = dquot->dq_sb;
898 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
Jan Karaf64dd442010-04-28 00:22:30 +0200899 struct buffer_head *bh;
900 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_type];
Jan Kara9e33d692008-08-25 19:56:50 +0200901 int status;
902
Jan Karaf64dd442010-04-28 00:22:30 +0200903 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
904 &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +0100905 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200906 mlog_errno(status);
907 goto out;
908 }
Jan Karaf64dd442010-04-28 00:22:30 +0200909 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
Jan Kara9e33d692008-08-25 19:56:50 +0200910 if (status < 0) {
911 mlog_errno(status);
912 goto out;
913 }
914out:
915 brelse(bh);
916 return status;
917}
918
919/* Find free entry in local quota file */
920static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
921 int type,
922 int *offset)
923{
924 struct mem_dqinfo *info = sb_dqinfo(sb, type);
925 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
926 struct ocfs2_quota_chunk *chunk;
927 struct ocfs2_local_disk_chunk *dchunk;
928 int found = 0, len;
929
930 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
931 dchunk = (struct ocfs2_local_disk_chunk *)
932 chunk->qc_headerbh->b_data;
933 if (le32_to_cpu(dchunk->dqc_free) > 0) {
934 found = 1;
935 break;
936 }
937 }
938 if (!found)
939 return NULL;
940
941 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
942 len = ol_chunk_entries(sb);
943 } else {
944 len = (oinfo->dqi_blocks -
945 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
946 * ol_quota_entries_per_block(sb);
947 }
948
949 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
950 /* We failed? */
951 if (found == len) {
952 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
953 " entries free (type=%d)\n", chunk->qc_num,
954 le32_to_cpu(dchunk->dqc_free), type);
955 return ERR_PTR(-EIO);
956 }
957 *offset = found;
958 return chunk;
959}
960
961/* Add new chunk to the local quota file */
962static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
963 struct super_block *sb,
964 int type,
965 int *offset)
966{
967 struct mem_dqinfo *info = sb_dqinfo(sb, type);
968 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
969 struct inode *lqinode = sb_dqopt(sb)->files[type];
970 struct ocfs2_quota_chunk *chunk = NULL;
971 struct ocfs2_local_disk_chunk *dchunk;
972 int status;
973 handle_t *handle;
Jan Kara0e7f3872009-07-22 13:17:17 +0200974 struct buffer_head *bh = NULL, *dbh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200975 u64 p_blkno;
976
977 /* We are protected by dqio_sem so no locking needed */
978 status = ocfs2_extend_no_holes(lqinode,
979 lqinode->i_size + 2 * sb->s_blocksize,
980 lqinode->i_size);
981 if (status < 0) {
982 mlog_errno(status);
983 goto out;
984 }
985 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
986 lqinode->i_size + 2 * sb->s_blocksize);
987 if (status < 0) {
988 mlog_errno(status);
989 goto out;
990 }
991
992 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
993 if (!chunk) {
994 status = -ENOMEM;
995 mlog_errno(status);
996 goto out;
997 }
Jan Kara05849742009-07-22 13:17:21 +0200998 /* Local quota info and two new blocks we initialize */
999 handle = ocfs2_start_trans(OCFS2_SB(sb),
1000 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1001 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001002 if (IS_ERR(handle)) {
1003 status = PTR_ERR(handle);
1004 mlog_errno(status);
1005 goto out;
1006 }
1007
Jan Kara0e7f3872009-07-22 13:17:17 +02001008 /* Initialize chunk header */
Jan Kara0e7f3872009-07-22 13:17:17 +02001009 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1010 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001011 if (status < 0) {
1012 mlog_errno(status);
1013 goto out_trans;
1014 }
1015 bh = sb_getblk(sb, p_blkno);
1016 if (!bh) {
1017 status = -ENOMEM;
1018 mlog_errno(status);
1019 goto out_trans;
1020 }
1021 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Joel Becker8cb471e2009-02-10 20:00:41 -08001022 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001023 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001024 OCFS2_JOURNAL_ACCESS_CREATE);
Jan Kara9e33d692008-08-25 19:56:50 +02001025 if (status < 0) {
1026 mlog_errno(status);
1027 goto out_trans;
1028 }
1029 lock_buffer(bh);
Tao Madf32b332008-11-25 07:21:36 +08001030 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
Jan Kara9e33d692008-08-25 19:56:50 +02001031 memset(dchunk->dqc_bitmap, 0,
1032 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1033 OCFS2_QBLK_RESERVED_SPACE);
Jan Kara9e33d692008-08-25 19:56:50 +02001034 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001035 ocfs2_journal_dirty(handle, bh);
Jan Kara9e33d692008-08-25 19:56:50 +02001036
Jan Kara0e7f3872009-07-22 13:17:17 +02001037 /* Initialize new block with structures */
Jan Kara0e7f3872009-07-22 13:17:17 +02001038 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1039 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001040 if (status < 0) {
1041 mlog_errno(status);
1042 goto out_trans;
1043 }
1044 dbh = sb_getblk(sb, p_blkno);
1045 if (!dbh) {
1046 status = -ENOMEM;
1047 mlog_errno(status);
1048 goto out_trans;
1049 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001050 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001051 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001052 OCFS2_JOURNAL_ACCESS_CREATE);
1053 if (status < 0) {
1054 mlog_errno(status);
1055 goto out_trans;
1056 }
1057 lock_buffer(dbh);
1058 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1059 unlock_buffer(dbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001060 ocfs2_journal_dirty(handle, dbh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001061
1062 /* Update local quotafile info */
Jan Kara9e33d692008-08-25 19:56:50 +02001063 oinfo->dqi_blocks += 2;
1064 oinfo->dqi_chunks++;
1065 status = ocfs2_local_write_info(sb, type);
1066 if (status < 0) {
1067 mlog_errno(status);
1068 goto out_trans;
1069 }
1070 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1071 if (status < 0) {
1072 mlog_errno(status);
1073 goto out;
1074 }
1075
1076 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1077 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1078 struct ocfs2_quota_chunk,
1079 qc_chunk)->qc_num + 1;
1080 chunk->qc_headerbh = bh;
1081 *offset = 0;
1082 return chunk;
1083out_trans:
1084 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1085out:
1086 brelse(bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001087 brelse(dbh);
Jan Kara9e33d692008-08-25 19:56:50 +02001088 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1089 return ERR_PTR(status);
1090}
1091
1092/* Find free entry in local quota file */
1093static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1094 struct super_block *sb,
1095 int type,
1096 int *offset)
1097{
1098 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1099 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1100 struct ocfs2_quota_chunk *chunk;
1101 struct inode *lqinode = sb_dqopt(sb)->files[type];
1102 struct ocfs2_local_disk_chunk *dchunk;
1103 int epb = ol_quota_entries_per_block(sb);
1104 unsigned int chunk_blocks;
Jan Kara0e7f3872009-07-22 13:17:17 +02001105 struct buffer_head *bh;
1106 u64 p_blkno;
Jan Kara9e33d692008-08-25 19:56:50 +02001107 int status;
1108 handle_t *handle;
1109
1110 if (list_empty(&oinfo->dqi_chunk))
1111 return ocfs2_local_quota_add_chunk(sb, type, offset);
1112 /* Is the last chunk full? */
1113 chunk = list_entry(oinfo->dqi_chunk.prev,
1114 struct ocfs2_quota_chunk, qc_chunk);
1115 chunk_blocks = oinfo->dqi_blocks -
1116 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1117 if (ol_chunk_blocks(sb) == chunk_blocks)
1118 return ocfs2_local_quota_add_chunk(sb, type, offset);
1119
1120 /* We are protected by dqio_sem so no locking needed */
1121 status = ocfs2_extend_no_holes(lqinode,
1122 lqinode->i_size + sb->s_blocksize,
1123 lqinode->i_size);
1124 if (status < 0) {
1125 mlog_errno(status);
1126 goto out;
1127 }
1128 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1129 lqinode->i_size + sb->s_blocksize);
1130 if (status < 0) {
1131 mlog_errno(status);
1132 goto out;
1133 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001134
1135 /* Get buffer from the just added block */
Jan Kara0e7f3872009-07-22 13:17:17 +02001136 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1137 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001138 if (status < 0) {
1139 mlog_errno(status);
1140 goto out;
1141 }
1142 bh = sb_getblk(sb, p_blkno);
1143 if (!bh) {
1144 status = -ENOMEM;
1145 mlog_errno(status);
1146 goto out;
1147 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001148 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001149
Jan Kara05849742009-07-22 13:17:21 +02001150 /* Local quota info, chunk header and the new block we initialize */
1151 handle = ocfs2_start_trans(OCFS2_SB(sb),
1152 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1153 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001154 if (IS_ERR(handle)) {
1155 status = PTR_ERR(handle);
1156 mlog_errno(status);
1157 goto out;
1158 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001159 /* Zero created block */
Joel Becker0cf2f762009-02-12 16:41:25 -08001160 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001161 OCFS2_JOURNAL_ACCESS_CREATE);
1162 if (status < 0) {
1163 mlog_errno(status);
1164 goto out_trans;
1165 }
1166 lock_buffer(bh);
1167 memset(bh->b_data, 0, sb->s_blocksize);
1168 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001169 ocfs2_journal_dirty(handle, bh);
1170
Jan Kara0e7f3872009-07-22 13:17:17 +02001171 /* Update chunk header */
Joel Becker0cf2f762009-02-12 16:41:25 -08001172 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1173 chunk->qc_headerbh,
Jan Kara9e33d692008-08-25 19:56:50 +02001174 OCFS2_JOURNAL_ACCESS_WRITE);
1175 if (status < 0) {
1176 mlog_errno(status);
1177 goto out_trans;
1178 }
1179
1180 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1181 lock_buffer(chunk->qc_headerbh);
1182 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1183 unlock_buffer(chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001184 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1185
Jan Kara0e7f3872009-07-22 13:17:17 +02001186 /* Update file header */
Jan Kara9e33d692008-08-25 19:56:50 +02001187 oinfo->dqi_blocks++;
1188 status = ocfs2_local_write_info(sb, type);
1189 if (status < 0) {
1190 mlog_errno(status);
1191 goto out_trans;
1192 }
1193
1194 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1195 if (status < 0) {
1196 mlog_errno(status);
1197 goto out;
1198 }
1199 *offset = chunk_blocks * epb;
1200 return chunk;
1201out_trans:
1202 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1203out:
1204 return ERR_PTR(status);
1205}
1206
Tao Madf32b332008-11-25 07:21:36 +08001207static void olq_alloc_dquot(struct buffer_head *bh, void *private)
Jan Kara9e33d692008-08-25 19:56:50 +02001208{
1209 int *offset = private;
1210 struct ocfs2_local_disk_chunk *dchunk;
1211
1212 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
1213 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
1214 le32_add_cpu(&dchunk->dqc_free, -1);
1215}
1216
1217/* Create dquot in the local file for given id */
Jan Karafb8dd8d2010-03-31 16:25:37 +02001218int ocfs2_create_local_dquot(struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001219{
1220 struct super_block *sb = dquot->dq_sb;
1221 int type = dquot->dq_type;
1222 struct inode *lqinode = sb_dqopt(sb)->files[type];
1223 struct ocfs2_quota_chunk *chunk;
1224 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1225 int offset;
1226 int status;
Jan Karaf64dd442010-04-28 00:22:30 +02001227 u64 pcount;
Jan Kara9e33d692008-08-25 19:56:50 +02001228
Jan Karaf64dd442010-04-28 00:22:30 +02001229 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001230 chunk = ocfs2_find_free_entry(sb, type, &offset);
1231 if (!chunk) {
1232 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
Jan Karaf64dd442010-04-28 00:22:30 +02001233 if (IS_ERR(chunk)) {
1234 status = PTR_ERR(chunk);
1235 goto out;
1236 }
Jan Kara9e33d692008-08-25 19:56:50 +02001237 } else if (IS_ERR(chunk)) {
Jan Karaf64dd442010-04-28 00:22:30 +02001238 status = PTR_ERR(chunk);
1239 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +02001240 }
1241 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1242 od->dq_chunk = chunk;
Jan Karaf64dd442010-04-28 00:22:30 +02001243 status = ocfs2_extent_map_get_blocks(lqinode,
1244 ol_dqblk_block(sb, chunk->qc_num, offset),
1245 &od->dq_local_phys_blk,
1246 &pcount,
1247 NULL);
Jan Kara9e33d692008-08-25 19:56:50 +02001248
1249 /* Initialize dquot structure on disk */
1250 status = ocfs2_local_write_dquot(dquot);
1251 if (status < 0) {
1252 mlog_errno(status);
1253 goto out;
1254 }
1255
1256 /* Mark structure as allocated */
1257 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1258 &offset);
1259 if (status < 0) {
1260 mlog_errno(status);
1261 goto out;
1262 }
1263out:
Jan Karaf64dd442010-04-28 00:22:30 +02001264 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001265 return status;
1266}
1267
Jan Karafb8dd8d2010-03-31 16:25:37 +02001268/*
1269 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1270 * already started a transaction and written all changes to global quota file
1271 */
1272int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001273{
1274 int status;
1275 int type = dquot->dq_type;
1276 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1277 struct super_block *sb = dquot->dq_sb;
1278 struct ocfs2_local_disk_chunk *dchunk;
1279 int offset;
Jan Kara9e33d692008-08-25 19:56:50 +02001280
Joel Becker0cf2f762009-02-12 16:41:25 -08001281 status = ocfs2_journal_access_dq(handle,
1282 INODE_CACHE(sb_dqopt(sb)->files[type]),
Jan Kara9e33d692008-08-25 19:56:50 +02001283 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1284 if (status < 0) {
1285 mlog_errno(status);
1286 goto out;
1287 }
1288 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1289 od->dq_local_off);
1290 dchunk = (struct ocfs2_local_disk_chunk *)
1291 (od->dq_chunk->qc_headerbh->b_data);
1292 /* Mark structure as freed */
1293 lock_buffer(od->dq_chunk->qc_headerbh);
1294 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
1295 le32_add_cpu(&dchunk->dqc_free, 1);
1296 unlock_buffer(od->dq_chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001297 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1298
Jan Kara9e33d692008-08-25 19:56:50 +02001299out:
1300 /* Clear the read bit so that next time someone uses this
1301 * dquot he reads fresh info from disk and allocates local
1302 * dquot structure */
1303 clear_bit(DQ_READ_B, &dquot->dq_flags);
1304 return status;
1305}
1306
Alexey Dobriyan1472da52009-10-16 15:26:03 +04001307static const struct quota_format_ops ocfs2_format_ops = {
Jan Kara9e33d692008-08-25 19:56:50 +02001308 .check_quota_file = ocfs2_local_check_quota_file,
1309 .read_file_info = ocfs2_local_read_info,
1310 .write_file_info = ocfs2_global_write_info,
1311 .free_file_info = ocfs2_local_free_info,
Jan Kara9e33d692008-08-25 19:56:50 +02001312 .commit_dqblk = ocfs2_local_write_dquot,
Jan Kara9e33d692008-08-25 19:56:50 +02001313};
1314
1315struct quota_format_type ocfs2_quota_format = {
1316 .qf_fmt_id = QFMT_OCFS2,
1317 .qf_ops = &ocfs2_format_ops,
1318 .qf_owner = THIS_MODULE
1319};