userns: Modify dqget to take struct kqid
[deliverable/linux.git] / fs / ocfs2 / quota_local.c
CommitLineData
9e33d69f
JK
1/*
2 * Implementation of operations over local quota file
3 */
4
5#include <linux/fs.h>
5a0e3ad6 6#include <linux/slab.h>
9e33d69f
JK
7#include <linux/quota.h>
8#include <linux/quotaops.h>
9#include <linux/module.h>
10
9e33d69f
JK
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"
4b3fa190 23#include "uptodate.h"
fb8dd8d7 24#include "super.h"
38877a43 25#include "ocfs2_trace.h"
9e33d69f
JK
26
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
2205363d
JK
55static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
56{
57 int epb = ol_quota_entries_per_block(sb);
58
59 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)
9e33d69f
JK
63{
64 int epb = ol_quota_entries_per_block(sb);
65
2205363d
JK
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);
9e33d69f
JK
74}
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
0584974a
JK
106 handle = ocfs2_start_trans(OCFS2_SB(sb),
107 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
9e33d69f
JK
108 if (IS_ERR(handle)) {
109 status = PTR_ERR(handle);
110 mlog_errno(status);
111 return status;
112 }
0cf2f763 113 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
13723d00 114 OCFS2_JOURNAL_ACCESS_WRITE);
9e33d69f
JK
115 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);
ec20cec7
JB
123 ocfs2_journal_dirty(handle, bh);
124
9e33d69f
JK
125 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
fb8dd8d7
JK
133/*
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
9e33d69f
JK
166/* 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 };
85eb8b73 175 struct buffer_head *bh = NULL;
9e33d69f
JK
176 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 */
85eb8b73
JB
182 status = ocfs2_read_quota_block(linode, 0, &bh);
183 if (status) {
9e33d69f
JK
184 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 */
85eb8b73
JB
214 status = ocfs2_read_quota_block(ginode, 0, &bh);
215 if (status) {
9e33d69f
JK
216 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;
85eb8b73
JB
271 newchunk->qc_headerbh = NULL;
272 status = ocfs2_read_quota_block(inode,
9e33d69f 273 ol_quota_chunk_block(inode->i_sb, i),
85eb8b73
JB
274 &newchunk->qc_headerbh);
275 if (status) {
9e33d69f
JK
276 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
2205363d
JK
301static 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++) {
85eb8b73
JB
357 hbh = NULL;
358 status = ocfs2_read_quota_block(lqinode,
359 ol_quota_chunk_block(sb, i),
360 &hbh);
361 if (status) {
2205363d
JK
362 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
619c200d
SM
407 printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
408 "slot %u\n", osb->dev_str, slot_num);
409
2205363d
JK
410 rec = ocfs2_alloc_quota_recovery();
411 if (!rec)
412 return ERR_PTR(-ENOMEM);
413 /* First init... */
414
415 for (type = 0; type < MAXQUOTAS; type++) {
416 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
417 continue;
418 /* At this point, journal of the slot is already replayed so
419 * we can trust metadata and data of the quota file */
420 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
421 if (!lqinode) {
422 status = -ENOENT;
423 goto out;
424 }
425 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
426 OCFS2_META_LOCK_RECOVERY);
427 if (status < 0) {
428 mlog_errno(status);
429 goto out_put;
430 }
431 /* Now read local header */
85eb8b73
JB
432 bh = NULL;
433 status = ocfs2_read_quota_block(lqinode, 0, &bh);
434 if (status) {
2205363d
JK
435 mlog_errno(status);
436 mlog(ML_ERROR, "failed to read quota file info header "
437 "(slot=%d type=%d)\n", slot_num, type);
438 goto out_lock;
439 }
440 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
441 OCFS2_LOCAL_INFO_OFF);
442 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
443 &rec->r_list[type]);
444 brelse(bh);
445out_lock:
446 ocfs2_inode_unlock(lqinode, 1);
447out_put:
448 iput(lqinode);
449 if (status < 0)
450 break;
451 }
452out:
453 if (status < 0) {
454 ocfs2_free_quota_recovery(rec);
455 rec = ERR_PTR(status);
456 }
457 return rec;
458}
459
460/* Sync changes in local quota file into global quota file and
461 * reinitialize local quota file.
462 * The function expects local quota file to be already locked and
463 * dqonoff_mutex locked. */
464static int ocfs2_recover_local_quota_file(struct inode *lqinode,
465 int type,
466 struct ocfs2_quota_recovery *rec)
467{
468 struct super_block *sb = lqinode->i_sb;
469 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
470 struct ocfs2_local_disk_chunk *dchunk;
471 struct ocfs2_local_disk_dqblk *dqblk;
472 struct dquot *dquot;
473 handle_t *handle;
474 struct buffer_head *hbh = NULL, *qbh = NULL;
475 int status = 0;
476 int bit, chunk;
477 struct ocfs2_recovery_chunk *rchunk, *next;
478 qsize_t spacechange, inodechange;
479
38877a43 480 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
2205363d 481
2205363d
JK
482 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
483 chunk = rchunk->rc_chunk;
85eb8b73
JB
484 hbh = NULL;
485 status = ocfs2_read_quota_block(lqinode,
486 ol_quota_chunk_block(sb, chunk),
487 &hbh);
488 if (status) {
2205363d
JK
489 mlog_errno(status);
490 break;
491 }
492 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
984b3f57 493 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
85eb8b73
JB
494 qbh = NULL;
495 status = ocfs2_read_quota_block(lqinode,
2205363d 496 ol_dqblk_block(sb, chunk, bit),
85eb8b73
JB
497 &qbh);
498 if (status) {
2205363d
JK
499 mlog_errno(status);
500 break;
501 }
502 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
503 ol_dqblk_block_off(sb, chunk, bit));
aca645a6
EB
504 dquot = dqget(sb,
505 make_kqid(&init_user_ns, type,
506 le64_to_cpu(dqblk->dqb_id)));
2205363d
JK
507 if (!dquot) {
508 status = -EIO;
509 mlog(ML_ERROR, "Failed to get quota structure "
510 "for id %u, type %d. Cannot finish quota "
511 "file recovery.\n",
512 (unsigned)le64_to_cpu(dqblk->dqb_id),
513 type);
514 goto out_put_bh;
515 }
80d73f15
JK
516 status = ocfs2_lock_global_qf(oinfo, 1);
517 if (status < 0) {
518 mlog_errno(status);
519 goto out_put_dquot;
520 }
521
2205363d
JK
522 handle = ocfs2_start_trans(OCFS2_SB(sb),
523 OCFS2_QSYNC_CREDITS);
524 if (IS_ERR(handle)) {
525 status = PTR_ERR(handle);
526 mlog_errno(status);
80d73f15 527 goto out_drop_lock;
2205363d
JK
528 }
529 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
530 spin_lock(&dq_data_lock);
531 /* Add usage from quota entry into quota changes
532 * of our node. Auxiliary variables are important
533 * due to signedness */
534 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
535 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
536 dquot->dq_dqb.dqb_curspace += spacechange;
537 dquot->dq_dqb.dqb_curinodes += inodechange;
538 spin_unlock(&dq_data_lock);
539 /* We want to drop reference held by the crashed
540 * node. Since we have our own reference we know
541 * global structure actually won't be freed. */
542 status = ocfs2_global_release_dquot(dquot);
543 if (status < 0) {
544 mlog_errno(status);
545 goto out_commit;
546 }
547 /* Release local quota file entry */
0cf2f763
JB
548 status = ocfs2_journal_access_dq(handle,
549 INODE_CACHE(lqinode),
2205363d
JK
550 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
551 if (status < 0) {
552 mlog_errno(status);
553 goto out_commit;
554 }
555 lock_buffer(qbh);
93925579
AM
556 WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
557 ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
2205363d
JK
558 le32_add_cpu(&dchunk->dqc_free, 1);
559 unlock_buffer(qbh);
ec20cec7 560 ocfs2_journal_dirty(handle, qbh);
2205363d
JK
561out_commit:
562 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
563 ocfs2_commit_trans(OCFS2_SB(sb), handle);
80d73f15
JK
564out_drop_lock:
565 ocfs2_unlock_global_qf(oinfo, 1);
2205363d
JK
566out_put_dquot:
567 dqput(dquot);
568out_put_bh:
569 brelse(qbh);
570 if (status < 0)
571 break;
572 }
573 brelse(hbh);
574 list_del(&rchunk->rc_list);
575 kfree(rchunk->rc_bitmap);
576 kfree(rchunk);
577 if (status < 0)
578 break;
579 }
2205363d
JK
580 if (status < 0)
581 free_recovery_list(&(rec->r_list[type]));
c1e8d35e
TM
582 if (status)
583 mlog_errno(status);
2205363d
JK
584 return status;
585}
586
587/* Recover local quota files for given node different from us */
588int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
589 struct ocfs2_quota_recovery *rec,
590 int slot_num)
591{
592 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
593 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
594 struct super_block *sb = osb->sb;
595 struct ocfs2_local_disk_dqinfo *ldinfo;
596 struct buffer_head *bh;
597 handle_t *handle;
598 int type;
599 int status = 0;
600 struct inode *lqinode;
601 unsigned int flags;
602
619c200d
SM
603 printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
604 "slot %u\n", osb->dev_str, slot_num);
605
2205363d
JK
606 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
607 for (type = 0; type < MAXQUOTAS; type++) {
608 if (list_empty(&(rec->r_list[type])))
609 continue;
38877a43 610 trace_ocfs2_finish_quota_recovery(slot_num);
2205363d
JK
611 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
612 if (!lqinode) {
613 status = -ENOENT;
614 goto out;
615 }
616 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
617 OCFS2_META_LOCK_NOQUEUE);
618 /* Someone else is holding the lock? Then he must be
619 * doing the recovery. Just skip the file... */
620 if (status == -EAGAIN) {
619c200d
SM
621 printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
622 "device (%s) for slot %d because quota file is "
623 "locked.\n", osb->dev_str, slot_num);
2205363d
JK
624 status = 0;
625 goto out_put;
626 } else if (status < 0) {
627 mlog_errno(status);
628 goto out_put;
629 }
630 /* Now read local header */
85eb8b73
JB
631 bh = NULL;
632 status = ocfs2_read_quota_block(lqinode, 0, &bh);
633 if (status) {
2205363d
JK
634 mlog_errno(status);
635 mlog(ML_ERROR, "failed to read quota file info header "
636 "(slot=%d type=%d)\n", slot_num, type);
637 goto out_lock;
638 }
639 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
640 OCFS2_LOCAL_INFO_OFF);
641 /* Is recovery still needed? */
642 flags = le32_to_cpu(ldinfo->dqi_flags);
643 if (!(flags & OLQF_CLEAN))
644 status = ocfs2_recover_local_quota_file(lqinode,
645 type,
646 rec);
647 /* We don't want to mark file as clean when it is actually
648 * active */
649 if (slot_num == osb->slot_num)
650 goto out_bh;
651 /* Mark quota file as clean if we are recovering quota file of
652 * some other node. */
0584974a
JK
653 handle = ocfs2_start_trans(osb,
654 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
2205363d
JK
655 if (IS_ERR(handle)) {
656 status = PTR_ERR(handle);
657 mlog_errno(status);
658 goto out_bh;
659 }
0cf2f763
JB
660 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
661 bh,
13723d00 662 OCFS2_JOURNAL_ACCESS_WRITE);
2205363d
JK
663 if (status < 0) {
664 mlog_errno(status);
665 goto out_trans;
666 }
667 lock_buffer(bh);
668 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
669 unlock_buffer(bh);
ec20cec7 670 ocfs2_journal_dirty(handle, bh);
2205363d
JK
671out_trans:
672 ocfs2_commit_trans(osb, handle);
673out_bh:
674 brelse(bh);
675out_lock:
676 ocfs2_inode_unlock(lqinode, 1);
677out_put:
678 iput(lqinode);
679 if (status < 0)
680 break;
681 }
682out:
683 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
684 kfree(rec);
685 return status;
686}
687
9e33d69f
JK
688/* Read information header from quota file */
689static int ocfs2_local_read_info(struct super_block *sb, int type)
690{
691 struct ocfs2_local_disk_dqinfo *ldinfo;
692 struct mem_dqinfo *info = sb_dqinfo(sb, type);
693 struct ocfs2_mem_dqinfo *oinfo;
694 struct inode *lqinode = sb_dqopt(sb)->files[type];
695 int status;
696 struct buffer_head *bh = NULL;
2205363d 697 struct ocfs2_quota_recovery *rec;
9e33d69f
JK
698 int locked = 0;
699
b4c30de3
JK
700 /* We don't need the lock and we have to acquire quota file locks
701 * which will later depend on this lock */
702 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f
JK
703 info->dqi_maxblimit = 0x7fffffffffffffffLL;
704 info->dqi_maxilimit = 0x7fffffffffffffffLL;
705 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
706 if (!oinfo) {
707 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
708 " info.");
709 goto out_err;
710 }
711 info->dqi_priv = oinfo;
712 oinfo->dqi_type = type;
713 INIT_LIST_HEAD(&oinfo->dqi_chunk);
2205363d 714 oinfo->dqi_rec = NULL;
9e33d69f 715 oinfo->dqi_lqi_bh = NULL;
ae4f6ef1 716 oinfo->dqi_libh = NULL;
9e33d69f
JK
717
718 status = ocfs2_global_read_info(sb, type);
719 if (status < 0)
720 goto out_err;
721
722 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
723 if (status < 0) {
724 mlog_errno(status);
725 goto out_err;
726 }
727 locked = 1;
728
729 /* Now read local header */
85eb8b73
JB
730 status = ocfs2_read_quota_block(lqinode, 0, &bh);
731 if (status) {
9e33d69f
JK
732 mlog_errno(status);
733 mlog(ML_ERROR, "failed to read quota file info header "
734 "(type=%d)\n", type);
735 goto out_err;
736 }
737 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
738 OCFS2_LOCAL_INFO_OFF);
739 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
740 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
741 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
ae4f6ef1 742 oinfo->dqi_libh = bh;
9e33d69f
JK
743
744 /* We crashed when using local quota file? */
2205363d
JK
745 if (!(info->dqi_flags & OLQF_CLEAN)) {
746 rec = OCFS2_SB(sb)->quota_rec;
747 if (!rec) {
748 rec = ocfs2_alloc_quota_recovery();
749 if (!rec) {
750 status = -ENOMEM;
751 mlog_errno(status);
752 goto out_err;
753 }
754 OCFS2_SB(sb)->quota_rec = rec;
755 }
9e33d69f 756
2205363d
JK
757 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
758 &rec->r_list[type]);
759 if (status < 0) {
760 mlog_errno(status);
761 goto out_err;
762 }
763 }
764
765 status = ocfs2_load_local_quota_bitmaps(lqinode,
9e33d69f
JK
766 ldinfo,
767 &oinfo->dqi_chunk);
768 if (status < 0) {
769 mlog_errno(status);
770 goto out_err;
771 }
772
773 /* Now mark quota file as used */
774 info->dqi_flags &= ~OLQF_CLEAN;
775 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
776 if (status < 0) {
777 mlog_errno(status);
778 goto out_err;
779 }
780
b4c30de3 781 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f
JK
782 return 0;
783out_err:
784 if (oinfo) {
785 iput(oinfo->dqi_gqinode);
786 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
787 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
788 brelse(oinfo->dqi_lqi_bh);
789 if (locked)
790 ocfs2_inode_unlock(lqinode, 1);
791 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
792 kfree(oinfo);
793 }
794 brelse(bh);
b4c30de3 795 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f
JK
796 return -1;
797}
798
799/* Write local info to quota file */
800static int ocfs2_local_write_info(struct super_block *sb, int type)
801{
802 struct mem_dqinfo *info = sb_dqinfo(sb, type);
803 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
ae4f6ef1 804 ->dqi_libh;
9e33d69f
JK
805 int status;
806
807 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
808 info);
809 if (status < 0) {
810 mlog_errno(status);
811 return -1;
812 }
813
814 return 0;
815}
816
817/* Release info from memory */
818static int ocfs2_local_free_info(struct super_block *sb, int type)
819{
820 struct mem_dqinfo *info = sb_dqinfo(sb, type);
821 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
822 struct ocfs2_quota_chunk *chunk;
823 struct ocfs2_local_disk_chunk *dchunk;
824 int mark_clean = 1, len;
825 int status;
826
827 iput(oinfo->dqi_gqinode);
828 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
829 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
830 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
831 dchunk = (struct ocfs2_local_disk_chunk *)
832 (chunk->qc_headerbh->b_data);
833 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
834 len = ol_chunk_entries(sb);
835 } else {
836 len = (oinfo->dqi_blocks -
837 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
838 * ol_quota_entries_per_block(sb);
839 }
840 /* Not all entries free? Bug! */
841 if (le32_to_cpu(dchunk->dqc_free) != len) {
842 mlog(ML_ERROR, "releasing quota file with used "
843 "entries (type=%d)\n", type);
844 mark_clean = 0;
845 }
846 }
847 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
848
2205363d
JK
849 /* dqonoff_mutex protects us against racing with recovery thread... */
850 if (oinfo->dqi_rec) {
851 ocfs2_free_quota_recovery(oinfo->dqi_rec);
852 mark_clean = 0;
853 }
854
9e33d69f
JK
855 if (!mark_clean)
856 goto out;
857
858 /* Mark local file as clean */
859 info->dqi_flags |= OLQF_CLEAN;
860 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
ae4f6ef1 861 oinfo->dqi_libh,
9e33d69f
JK
862 olq_update_info,
863 info);
864 if (status < 0) {
865 mlog_errno(status);
866 goto out;
867 }
868
869out:
870 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
ae4f6ef1 871 brelse(oinfo->dqi_libh);
9e33d69f
JK
872 brelse(oinfo->dqi_lqi_bh);
873 kfree(oinfo);
874 return 0;
875}
876
877static void olq_set_dquot(struct buffer_head *bh, void *private)
878{
879 struct ocfs2_dquot *od = private;
880 struct ocfs2_local_disk_dqblk *dqblk;
881 struct super_block *sb = od->dq_dquot.dq_sb;
882
883 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
884 + ol_dqblk_block_offset(sb, od->dq_local_off));
885
886 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
887 spin_lock(&dq_data_lock);
888 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
889 od->dq_origspace);
890 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
891 od->dq_originodes);
892 spin_unlock(&dq_data_lock);
38877a43
TM
893 trace_olq_set_dquot(
894 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
895 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
896 od->dq_dquot.dq_id);
9e33d69f
JK
897}
898
899/* Write dquot to local quota file */
741e1289 900int ocfs2_local_write_dquot(struct dquot *dquot)
9e33d69f
JK
901{
902 struct super_block *sb = dquot->dq_sb;
903 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
f64dd44e
JK
904 struct buffer_head *bh;
905 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_type];
9e33d69f
JK
906 int status;
907
f64dd44e
JK
908 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
909 &bh);
85eb8b73 910 if (status) {
9e33d69f
JK
911 mlog_errno(status);
912 goto out;
913 }
f64dd44e 914 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
9e33d69f
JK
915 if (status < 0) {
916 mlog_errno(status);
917 goto out;
918 }
919out:
920 brelse(bh);
921 return status;
922}
923
924/* Find free entry in local quota file */
925static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
926 int type,
927 int *offset)
928{
929 struct mem_dqinfo *info = sb_dqinfo(sb, type);
930 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
931 struct ocfs2_quota_chunk *chunk;
932 struct ocfs2_local_disk_chunk *dchunk;
933 int found = 0, len;
934
935 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
936 dchunk = (struct ocfs2_local_disk_chunk *)
937 chunk->qc_headerbh->b_data;
938 if (le32_to_cpu(dchunk->dqc_free) > 0) {
939 found = 1;
940 break;
941 }
942 }
943 if (!found)
944 return NULL;
945
946 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
947 len = ol_chunk_entries(sb);
948 } else {
949 len = (oinfo->dqi_blocks -
950 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
951 * ol_quota_entries_per_block(sb);
952 }
953
93925579 954 found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
9e33d69f
JK
955 /* We failed? */
956 if (found == len) {
957 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
958 " entries free (type=%d)\n", chunk->qc_num,
959 le32_to_cpu(dchunk->dqc_free), type);
960 return ERR_PTR(-EIO);
961 }
962 *offset = found;
963 return chunk;
964}
965
966/* Add new chunk to the local quota file */
967static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
968 struct super_block *sb,
969 int type,
970 int *offset)
971{
972 struct mem_dqinfo *info = sb_dqinfo(sb, type);
973 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
974 struct inode *lqinode = sb_dqopt(sb)->files[type];
975 struct ocfs2_quota_chunk *chunk = NULL;
976 struct ocfs2_local_disk_chunk *dchunk;
977 int status;
978 handle_t *handle;
0e7f387b 979 struct buffer_head *bh = NULL, *dbh = NULL;
9e33d69f
JK
980 u64 p_blkno;
981
982 /* We are protected by dqio_sem so no locking needed */
5693486b 983 status = ocfs2_extend_no_holes(lqinode, NULL,
9e33d69f
JK
984 lqinode->i_size + 2 * sb->s_blocksize,
985 lqinode->i_size);
986 if (status < 0) {
987 mlog_errno(status);
988 goto out;
989 }
990 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
991 lqinode->i_size + 2 * sb->s_blocksize);
992 if (status < 0) {
993 mlog_errno(status);
994 goto out;
995 }
996
997 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
998 if (!chunk) {
999 status = -ENOMEM;
1000 mlog_errno(status);
1001 goto out;
1002 }
0584974a
JK
1003 /* Local quota info and two new blocks we initialize */
1004 handle = ocfs2_start_trans(OCFS2_SB(sb),
1005 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1006 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
0e7f387b
JK
1007 if (IS_ERR(handle)) {
1008 status = PTR_ERR(handle);
1009 mlog_errno(status);
1010 goto out;
1011 }
9e33d69f 1012
0e7f387b 1013 /* Initialize chunk header */
9e33d69f
JK
1014 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1015 &p_blkno, NULL, NULL);
9e33d69f
JK
1016 if (status < 0) {
1017 mlog_errno(status);
0e7f387b 1018 goto out_trans;
9e33d69f
JK
1019 }
1020 bh = sb_getblk(sb, p_blkno);
1021 if (!bh) {
1022 status = -ENOMEM;
1023 mlog_errno(status);
0e7f387b 1024 goto out_trans;
9e33d69f
JK
1025 }
1026 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
8cb471e8 1027 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0cf2f763 1028 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387b 1029 OCFS2_JOURNAL_ACCESS_CREATE);
9e33d69f
JK
1030 if (status < 0) {
1031 mlog_errno(status);
1032 goto out_trans;
1033 }
1034 lock_buffer(bh);
df32b334 1035 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
9e33d69f
JK
1036 memset(dchunk->dqc_bitmap, 0,
1037 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1038 OCFS2_QBLK_RESERVED_SPACE);
9e33d69f 1039 unlock_buffer(bh);
ec20cec7 1040 ocfs2_journal_dirty(handle, bh);
9e33d69f 1041
0e7f387b 1042 /* Initialize new block with structures */
0e7f387b
JK
1043 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1044 &p_blkno, NULL, NULL);
0e7f387b
JK
1045 if (status < 0) {
1046 mlog_errno(status);
1047 goto out_trans;
1048 }
1049 dbh = sb_getblk(sb, p_blkno);
1050 if (!dbh) {
1051 status = -ENOMEM;
1052 mlog_errno(status);
1053 goto out_trans;
1054 }
8cb471e8 1055 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
0cf2f763 1056 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
0e7f387b
JK
1057 OCFS2_JOURNAL_ACCESS_CREATE);
1058 if (status < 0) {
1059 mlog_errno(status);
1060 goto out_trans;
1061 }
1062 lock_buffer(dbh);
1063 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1064 unlock_buffer(dbh);
ec20cec7 1065 ocfs2_journal_dirty(handle, dbh);
0e7f387b
JK
1066
1067 /* Update local quotafile info */
9e33d69f
JK
1068 oinfo->dqi_blocks += 2;
1069 oinfo->dqi_chunks++;
1070 status = ocfs2_local_write_info(sb, type);
1071 if (status < 0) {
1072 mlog_errno(status);
1073 goto out_trans;
1074 }
1075 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1076 if (status < 0) {
1077 mlog_errno(status);
1078 goto out;
1079 }
1080
1081 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1082 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1083 struct ocfs2_quota_chunk,
1084 qc_chunk)->qc_num + 1;
1085 chunk->qc_headerbh = bh;
1086 *offset = 0;
1087 return chunk;
1088out_trans:
1089 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1090out:
1091 brelse(bh);
0e7f387b 1092 brelse(dbh);
9e33d69f
JK
1093 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1094 return ERR_PTR(status);
1095}
1096
1097/* Find free entry in local quota file */
1098static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1099 struct super_block *sb,
1100 int type,
1101 int *offset)
1102{
1103 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1104 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1105 struct ocfs2_quota_chunk *chunk;
1106 struct inode *lqinode = sb_dqopt(sb)->files[type];
1107 struct ocfs2_local_disk_chunk *dchunk;
1108 int epb = ol_quota_entries_per_block(sb);
1109 unsigned int chunk_blocks;
0e7f387b
JK
1110 struct buffer_head *bh;
1111 u64 p_blkno;
9e33d69f
JK
1112 int status;
1113 handle_t *handle;
1114
1115 if (list_empty(&oinfo->dqi_chunk))
1116 return ocfs2_local_quota_add_chunk(sb, type, offset);
1117 /* Is the last chunk full? */
1118 chunk = list_entry(oinfo->dqi_chunk.prev,
1119 struct ocfs2_quota_chunk, qc_chunk);
1120 chunk_blocks = oinfo->dqi_blocks -
1121 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1122 if (ol_chunk_blocks(sb) == chunk_blocks)
1123 return ocfs2_local_quota_add_chunk(sb, type, offset);
1124
1125 /* We are protected by dqio_sem so no locking needed */
5693486b 1126 status = ocfs2_extend_no_holes(lqinode, NULL,
9e33d69f
JK
1127 lqinode->i_size + sb->s_blocksize,
1128 lqinode->i_size);
1129 if (status < 0) {
1130 mlog_errno(status);
1131 goto out;
1132 }
1133 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
1134 lqinode->i_size + sb->s_blocksize);
1135 if (status < 0) {
1136 mlog_errno(status);
1137 goto out;
1138 }
0e7f387b
JK
1139
1140 /* Get buffer from the just added block */
0e7f387b
JK
1141 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1142 &p_blkno, NULL, NULL);
0e7f387b
JK
1143 if (status < 0) {
1144 mlog_errno(status);
1145 goto out;
1146 }
1147 bh = sb_getblk(sb, p_blkno);
1148 if (!bh) {
1149 status = -ENOMEM;
1150 mlog_errno(status);
1151 goto out;
1152 }
8cb471e8 1153 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0e7f387b 1154
0584974a
JK
1155 /* Local quota info, chunk header and the new block we initialize */
1156 handle = ocfs2_start_trans(OCFS2_SB(sb),
1157 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1158 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
9e33d69f
JK
1159 if (IS_ERR(handle)) {
1160 status = PTR_ERR(handle);
1161 mlog_errno(status);
1162 goto out;
1163 }
0e7f387b 1164 /* Zero created block */
0cf2f763 1165 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387b
JK
1166 OCFS2_JOURNAL_ACCESS_CREATE);
1167 if (status < 0) {
1168 mlog_errno(status);
1169 goto out_trans;
1170 }
1171 lock_buffer(bh);
1172 memset(bh->b_data, 0, sb->s_blocksize);
1173 unlock_buffer(bh);
ec20cec7
JB
1174 ocfs2_journal_dirty(handle, bh);
1175
0e7f387b 1176 /* Update chunk header */
0cf2f763
JB
1177 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1178 chunk->qc_headerbh,
9e33d69f
JK
1179 OCFS2_JOURNAL_ACCESS_WRITE);
1180 if (status < 0) {
1181 mlog_errno(status);
1182 goto out_trans;
1183 }
1184
1185 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1186 lock_buffer(chunk->qc_headerbh);
1187 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1188 unlock_buffer(chunk->qc_headerbh);
ec20cec7
JB
1189 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1190
0e7f387b 1191 /* Update file header */
9e33d69f
JK
1192 oinfo->dqi_blocks++;
1193 status = ocfs2_local_write_info(sb, type);
1194 if (status < 0) {
1195 mlog_errno(status);
1196 goto out_trans;
1197 }
1198
1199 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1200 if (status < 0) {
1201 mlog_errno(status);
1202 goto out;
1203 }
1204 *offset = chunk_blocks * epb;
1205 return chunk;
1206out_trans:
1207 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1208out:
1209 return ERR_PTR(status);
1210}
1211
df32b334 1212static void olq_alloc_dquot(struct buffer_head *bh, void *private)
9e33d69f
JK
1213{
1214 int *offset = private;
1215 struct ocfs2_local_disk_chunk *dchunk;
1216
1217 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
93925579 1218 ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
9e33d69f
JK
1219 le32_add_cpu(&dchunk->dqc_free, -1);
1220}
1221
1222/* Create dquot in the local file for given id */
fb8dd8d7 1223int ocfs2_create_local_dquot(struct dquot *dquot)
9e33d69f
JK
1224{
1225 struct super_block *sb = dquot->dq_sb;
1226 int type = dquot->dq_type;
1227 struct inode *lqinode = sb_dqopt(sb)->files[type];
1228 struct ocfs2_quota_chunk *chunk;
1229 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1230 int offset;
1231 int status;
f64dd44e 1232 u64 pcount;
9e33d69f 1233
f64dd44e 1234 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f
JK
1235 chunk = ocfs2_find_free_entry(sb, type, &offset);
1236 if (!chunk) {
1237 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
f64dd44e
JK
1238 if (IS_ERR(chunk)) {
1239 status = PTR_ERR(chunk);
1240 goto out;
1241 }
9e33d69f 1242 } else if (IS_ERR(chunk)) {
f64dd44e
JK
1243 status = PTR_ERR(chunk);
1244 goto out;
9e33d69f
JK
1245 }
1246 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1247 od->dq_chunk = chunk;
f64dd44e
JK
1248 status = ocfs2_extent_map_get_blocks(lqinode,
1249 ol_dqblk_block(sb, chunk->qc_num, offset),
1250 &od->dq_local_phys_blk,
1251 &pcount,
1252 NULL);
9e33d69f
JK
1253
1254 /* Initialize dquot structure on disk */
1255 status = ocfs2_local_write_dquot(dquot);
1256 if (status < 0) {
1257 mlog_errno(status);
1258 goto out;
1259 }
1260
1261 /* Mark structure as allocated */
1262 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1263 &offset);
1264 if (status < 0) {
1265 mlog_errno(status);
1266 goto out;
1267 }
1268out:
f64dd44e 1269 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f
JK
1270 return status;
1271}
1272
fb8dd8d7
JK
1273/*
1274 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1275 * already started a transaction and written all changes to global quota file
1276 */
1277int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
9e33d69f
JK
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;
9e33d69f 1285
0cf2f763
JB
1286 status = ocfs2_journal_access_dq(handle,
1287 INODE_CACHE(sb_dqopt(sb)->files[type]),
9e33d69f
JK
1288 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1289 if (status < 0) {
1290 mlog_errno(status);
1291 goto out;
1292 }
1293 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1294 od->dq_local_off);
1295 dchunk = (struct ocfs2_local_disk_chunk *)
1296 (od->dq_chunk->qc_headerbh->b_data);
1297 /* Mark structure as freed */
1298 lock_buffer(od->dq_chunk->qc_headerbh);
93925579 1299 ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
9e33d69f
JK
1300 le32_add_cpu(&dchunk->dqc_free, 1);
1301 unlock_buffer(od->dq_chunk->qc_headerbh);
ec20cec7
JB
1302 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1303
9e33d69f
JK
1304out:
1305 /* Clear the read bit so that next time someone uses this
1306 * dquot he reads fresh info from disk and allocates local
1307 * dquot structure */
1308 clear_bit(DQ_READ_B, &dquot->dq_flags);
1309 return status;
1310}
1311
1472da5f 1312static const struct quota_format_ops ocfs2_format_ops = {
9e33d69f
JK
1313 .check_quota_file = ocfs2_local_check_quota_file,
1314 .read_file_info = ocfs2_local_read_info,
1315 .write_file_info = ocfs2_global_write_info,
1316 .free_file_info = ocfs2_local_free_info,
9e33d69f
JK
1317};
1318
1319struct quota_format_type ocfs2_quota_format = {
1320 .qf_fmt_id = QFMT_OCFS2,
1321 .qf_ops = &ocfs2_format_ops,
1322 .qf_owner = THIS_MODULE
1323};
This page took 0.313427 seconds and 5 git commands to generate.