blob: 40e82b483136fccd8c38d6af4b3fc6119ed72948 [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"
23
24/* Number of local quota structures per block */
25static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
26{
27 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
28 sizeof(struct ocfs2_local_disk_dqblk));
29}
30
31/* Number of blocks with entries in one chunk */
32static inline unsigned int ol_chunk_blocks(struct super_block *sb)
33{
34 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
35 OCFS2_QBLK_RESERVED_SPACE) << 3) /
36 ol_quota_entries_per_block(sb);
37}
38
39/* Number of entries in a chunk bitmap */
40static unsigned int ol_chunk_entries(struct super_block *sb)
41{
42 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
43}
44
45/* Offset of the chunk in quota file */
46static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
47{
48 /* 1 block for local quota file info, 1 block per chunk for chunk info */
49 return 1 + (ol_chunk_blocks(sb) + 1) * c;
50}
51
52/* Offset of the dquot structure in the quota file */
53static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
54{
55 int epb = ol_quota_entries_per_block(sb);
56
57 return ((ol_quota_chunk_block(sb, c) + 1 + off / epb)
58 << sb->s_blocksize_bits) +
59 (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
60}
61
62/* Compute block number from given offset */
63static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
64{
65 return off >> sb->s_blocksize_bits;
66}
67
68static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
69{
70 return off & ((1 << sb->s_blocksize_bits) - 1);
71}
72
73/* Compute offset in the chunk of a structure with the given offset */
74static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
75{
76 int epb = ol_quota_entries_per_block(sb);
77
78 return ((off >> sb->s_blocksize_bits) -
79 ol_quota_chunk_block(sb, c) - 1) * epb
80 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
81 sizeof(struct ocfs2_local_disk_dqblk);
82}
83
84/* Write bufferhead into the fs */
85static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
86 void (*modify)(struct buffer_head *, void *), void *private)
87{
88 struct super_block *sb = inode->i_sb;
89 handle_t *handle;
90 int status;
91
92 handle = ocfs2_start_trans(OCFS2_SB(sb), 1);
93 if (IS_ERR(handle)) {
94 status = PTR_ERR(handle);
95 mlog_errno(status);
96 return status;
97 }
98 status = ocfs2_journal_access(handle, inode, bh,
99 OCFS2_JOURNAL_ACCESS_WRITE);
100 if (status < 0) {
101 mlog_errno(status);
102 ocfs2_commit_trans(OCFS2_SB(sb), handle);
103 return status;
104 }
105 lock_buffer(bh);
106 modify(bh, private);
107 unlock_buffer(bh);
108 status = ocfs2_journal_dirty(handle, bh);
109 if (status < 0) {
110 mlog_errno(status);
111 ocfs2_commit_trans(OCFS2_SB(sb), handle);
112 return status;
113 }
114 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
115 if (status < 0) {
116 mlog_errno(status);
117 return status;
118 }
119 return 0;
120}
121
122/* Check whether we understand format of quota files */
123static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
124{
125 unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
126 unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
127 unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
128 unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
129 unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
130 GROUP_QUOTA_SYSTEM_INODE };
131 struct buffer_head *bh;
132 struct inode *linode = sb_dqopt(sb)->files[type];
133 struct inode *ginode = NULL;
134 struct ocfs2_disk_dqheader *dqhead;
135 int status, ret = 0;
136
137 /* First check whether we understand local quota file */
138 bh = ocfs2_read_quota_block(linode, 0, &status);
139 if (!bh) {
140 mlog_errno(status);
141 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
142 type);
143 goto out_err;
144 }
145 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
146 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
147 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
148 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
149 lmagics[type], type);
150 goto out_err;
151 }
152 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
153 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
154 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
155 lversions[type], type);
156 goto out_err;
157 }
158 brelse(bh);
159 bh = NULL;
160
161 /* Next check whether we understand global quota file */
162 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
163 OCFS2_INVALID_SLOT);
164 if (!ginode) {
165 mlog(ML_ERROR, "cannot get global quota file inode "
166 "(type=%d)\n", type);
167 goto out_err;
168 }
169 /* Since the header is read only, we don't care about locking */
170 bh = ocfs2_read_quota_block(ginode, 0, &status);
171 if (!bh) {
172 mlog_errno(status);
173 mlog(ML_ERROR, "failed to read global quota file header "
174 "(type=%d)\n", type);
175 goto out_err;
176 }
177 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
178 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
179 mlog(ML_ERROR, "global quota file magic does not match "
180 "(%u != %u), type=%d\n",
181 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
182 goto out_err;
183 }
184 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
185 mlog(ML_ERROR, "global quota file version does not match "
186 "(%u != %u), type=%d\n",
187 le32_to_cpu(dqhead->dqh_version), gversions[type],
188 type);
189 goto out_err;
190 }
191
192 ret = 1;
193out_err:
194 brelse(bh);
195 iput(ginode);
196 return ret;
197}
198
199/* Release given list of quota file chunks */
200static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
201{
202 struct ocfs2_quota_chunk *pos, *next;
203
204 list_for_each_entry_safe(pos, next, head, qc_chunk) {
205 list_del(&pos->qc_chunk);
206 brelse(pos->qc_headerbh);
207 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
208 }
209}
210
211/* Load quota bitmaps into memory */
212static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
213 struct ocfs2_local_disk_dqinfo *ldinfo,
214 struct list_head *head)
215{
216 struct ocfs2_quota_chunk *newchunk;
217 int i, status;
218
219 INIT_LIST_HEAD(head);
220 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
221 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
222 if (!newchunk) {
223 ocfs2_release_local_quota_bitmaps(head);
224 return -ENOMEM;
225 }
226 newchunk->qc_num = i;
227 newchunk->qc_headerbh = ocfs2_read_quota_block(inode,
228 ol_quota_chunk_block(inode->i_sb, i),
229 &status);
230 if (!newchunk->qc_headerbh) {
231 mlog_errno(status);
232 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
233 ocfs2_release_local_quota_bitmaps(head);
234 return status;
235 }
236 list_add_tail(&newchunk->qc_chunk, head);
237 }
238 return 0;
239}
240
241static void olq_update_info(struct buffer_head *bh, void *private)
242{
243 struct mem_dqinfo *info = private;
244 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
245 struct ocfs2_local_disk_dqinfo *ldinfo;
246
247 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
248 OCFS2_LOCAL_INFO_OFF);
249 spin_lock(&dq_data_lock);
250 ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
251 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
252 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
253 spin_unlock(&dq_data_lock);
254}
255
256/* Read information header from quota file */
257static int ocfs2_local_read_info(struct super_block *sb, int type)
258{
259 struct ocfs2_local_disk_dqinfo *ldinfo;
260 struct mem_dqinfo *info = sb_dqinfo(sb, type);
261 struct ocfs2_mem_dqinfo *oinfo;
262 struct inode *lqinode = sb_dqopt(sb)->files[type];
263 int status;
264 struct buffer_head *bh = NULL;
265 int locked = 0;
266
267 info->dqi_maxblimit = 0x7fffffffffffffffLL;
268 info->dqi_maxilimit = 0x7fffffffffffffffLL;
269 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
270 if (!oinfo) {
271 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
272 " info.");
273 goto out_err;
274 }
275 info->dqi_priv = oinfo;
276 oinfo->dqi_type = type;
277 INIT_LIST_HEAD(&oinfo->dqi_chunk);
278 oinfo->dqi_lqi_bh = NULL;
279 oinfo->dqi_ibh = NULL;
280
281 status = ocfs2_global_read_info(sb, type);
282 if (status < 0)
283 goto out_err;
284
285 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
286 if (status < 0) {
287 mlog_errno(status);
288 goto out_err;
289 }
290 locked = 1;
291
292 /* Now read local header */
293 bh = ocfs2_read_quota_block(lqinode, 0, &status);
294 if (!bh) {
295 mlog_errno(status);
296 mlog(ML_ERROR, "failed to read quota file info header "
297 "(type=%d)\n", type);
298 goto out_err;
299 }
300 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
301 OCFS2_LOCAL_INFO_OFF);
302 info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
303 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
304 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
305 oinfo->dqi_ibh = bh;
306
307 /* We crashed when using local quota file? */
308 if (!(info->dqi_flags & OLQF_CLEAN))
309 goto out_err; /* So far we just bail out. Later we should resync here */
310
311 status = ocfs2_load_local_quota_bitmaps(sb_dqopt(sb)->files[type],
312 ldinfo,
313 &oinfo->dqi_chunk);
314 if (status < 0) {
315 mlog_errno(status);
316 goto out_err;
317 }
318
319 /* Now mark quota file as used */
320 info->dqi_flags &= ~OLQF_CLEAN;
321 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
322 if (status < 0) {
323 mlog_errno(status);
324 goto out_err;
325 }
326
327 return 0;
328out_err:
329 if (oinfo) {
330 iput(oinfo->dqi_gqinode);
331 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
332 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
333 brelse(oinfo->dqi_lqi_bh);
334 if (locked)
335 ocfs2_inode_unlock(lqinode, 1);
336 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
337 kfree(oinfo);
338 }
339 brelse(bh);
340 return -1;
341}
342
343/* Write local info to quota file */
344static int ocfs2_local_write_info(struct super_block *sb, int type)
345{
346 struct mem_dqinfo *info = sb_dqinfo(sb, type);
347 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
348 ->dqi_ibh;
349 int status;
350
351 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
352 info);
353 if (status < 0) {
354 mlog_errno(status);
355 return -1;
356 }
357
358 return 0;
359}
360
361/* Release info from memory */
362static int ocfs2_local_free_info(struct super_block *sb, int type)
363{
364 struct mem_dqinfo *info = sb_dqinfo(sb, type);
365 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
366 struct ocfs2_quota_chunk *chunk;
367 struct ocfs2_local_disk_chunk *dchunk;
368 int mark_clean = 1, len;
369 int status;
370
Mark Fasheh171bf932008-10-20 15:36:47 +0200371 /* At this point we know there are no more dquots and thus
372 * even if there's some sync in the pdflush queue, it won't
373 * find any dquots and return without doing anything */
374 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Jan Kara9e33d692008-08-25 19:56:50 +0200375 iput(oinfo->dqi_gqinode);
376 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
377 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
378 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
379 dchunk = (struct ocfs2_local_disk_chunk *)
380 (chunk->qc_headerbh->b_data);
381 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
382 len = ol_chunk_entries(sb);
383 } else {
384 len = (oinfo->dqi_blocks -
385 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
386 * ol_quota_entries_per_block(sb);
387 }
388 /* Not all entries free? Bug! */
389 if (le32_to_cpu(dchunk->dqc_free) != len) {
390 mlog(ML_ERROR, "releasing quota file with used "
391 "entries (type=%d)\n", type);
392 mark_clean = 0;
393 }
394 }
395 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
396
397 if (!mark_clean)
398 goto out;
399
400 /* Mark local file as clean */
401 info->dqi_flags |= OLQF_CLEAN;
402 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
403 oinfo->dqi_ibh,
404 olq_update_info,
405 info);
406 if (status < 0) {
407 mlog_errno(status);
408 goto out;
409 }
410
411out:
412 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
413 brelse(oinfo->dqi_ibh);
414 brelse(oinfo->dqi_lqi_bh);
415 kfree(oinfo);
416 return 0;
417}
418
419static void olq_set_dquot(struct buffer_head *bh, void *private)
420{
421 struct ocfs2_dquot *od = private;
422 struct ocfs2_local_disk_dqblk *dqblk;
423 struct super_block *sb = od->dq_dquot.dq_sb;
424
425 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
426 + ol_dqblk_block_offset(sb, od->dq_local_off));
427
428 dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
429 spin_lock(&dq_data_lock);
430 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
431 od->dq_origspace);
432 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
433 od->dq_originodes);
434 spin_unlock(&dq_data_lock);
435 mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
436 od->dq_dquot.dq_id, dqblk->dqb_spacemod, dqblk->dqb_inodemod);
437}
438
439/* Write dquot to local quota file */
440static int ocfs2_local_write_dquot(struct dquot *dquot)
441{
442 struct super_block *sb = dquot->dq_sb;
443 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
444 struct buffer_head *bh;
445 int status;
446
447 bh = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
448 ol_dqblk_file_block(sb, od->dq_local_off),
449 &status);
450 if (!bh) {
451 mlog_errno(status);
452 goto out;
453 }
454 status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
455 olq_set_dquot, od);
456 if (status < 0) {
457 mlog_errno(status);
458 goto out;
459 }
460out:
461 brelse(bh);
462 return status;
463}
464
465/* Find free entry in local quota file */
466static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
467 int type,
468 int *offset)
469{
470 struct mem_dqinfo *info = sb_dqinfo(sb, type);
471 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
472 struct ocfs2_quota_chunk *chunk;
473 struct ocfs2_local_disk_chunk *dchunk;
474 int found = 0, len;
475
476 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
477 dchunk = (struct ocfs2_local_disk_chunk *)
478 chunk->qc_headerbh->b_data;
479 if (le32_to_cpu(dchunk->dqc_free) > 0) {
480 found = 1;
481 break;
482 }
483 }
484 if (!found)
485 return NULL;
486
487 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
488 len = ol_chunk_entries(sb);
489 } else {
490 len = (oinfo->dqi_blocks -
491 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
492 * ol_quota_entries_per_block(sb);
493 }
494
495 found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
496 /* We failed? */
497 if (found == len) {
498 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
499 " entries free (type=%d)\n", chunk->qc_num,
500 le32_to_cpu(dchunk->dqc_free), type);
501 return ERR_PTR(-EIO);
502 }
503 *offset = found;
504 return chunk;
505}
506
507/* Add new chunk to the local quota file */
508static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
509 struct super_block *sb,
510 int type,
511 int *offset)
512{
513 struct mem_dqinfo *info = sb_dqinfo(sb, type);
514 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
515 struct inode *lqinode = sb_dqopt(sb)->files[type];
516 struct ocfs2_quota_chunk *chunk = NULL;
517 struct ocfs2_local_disk_chunk *dchunk;
518 int status;
519 handle_t *handle;
520 struct buffer_head *bh = NULL;
521 u64 p_blkno;
522
523 /* We are protected by dqio_sem so no locking needed */
524 status = ocfs2_extend_no_holes(lqinode,
525 lqinode->i_size + 2 * sb->s_blocksize,
526 lqinode->i_size);
527 if (status < 0) {
528 mlog_errno(status);
529 goto out;
530 }
531 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
532 lqinode->i_size + 2 * sb->s_blocksize);
533 if (status < 0) {
534 mlog_errno(status);
535 goto out;
536 }
537
538 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
539 if (!chunk) {
540 status = -ENOMEM;
541 mlog_errno(status);
542 goto out;
543 }
544
545 down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
546 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
547 &p_blkno, NULL, NULL);
548 up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
549 if (status < 0) {
550 mlog_errno(status);
551 goto out;
552 }
553 bh = sb_getblk(sb, p_blkno);
554 if (!bh) {
555 status = -ENOMEM;
556 mlog_errno(status);
557 goto out;
558 }
559 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
560
561 handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
562 if (IS_ERR(handle)) {
563 status = PTR_ERR(handle);
564 mlog_errno(status);
565 goto out;
566 }
567
568 status = ocfs2_journal_access(handle, lqinode, bh,
569 OCFS2_JOURNAL_ACCESS_WRITE);
570 if (status < 0) {
571 mlog_errno(status);
572 goto out_trans;
573 }
574 lock_buffer(bh);
575 dchunk->dqc_free = ol_quota_entries_per_block(sb);
576 memset(dchunk->dqc_bitmap, 0,
577 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
578 OCFS2_QBLK_RESERVED_SPACE);
579 set_buffer_uptodate(bh);
580 unlock_buffer(bh);
581 status = ocfs2_journal_dirty(handle, bh);
582 if (status < 0) {
583 mlog_errno(status);
584 goto out_trans;
585 }
586
587 oinfo->dqi_blocks += 2;
588 oinfo->dqi_chunks++;
589 status = ocfs2_local_write_info(sb, type);
590 if (status < 0) {
591 mlog_errno(status);
592 goto out_trans;
593 }
594 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
595 if (status < 0) {
596 mlog_errno(status);
597 goto out;
598 }
599
600 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
601 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
602 struct ocfs2_quota_chunk,
603 qc_chunk)->qc_num + 1;
604 chunk->qc_headerbh = bh;
605 *offset = 0;
606 return chunk;
607out_trans:
608 ocfs2_commit_trans(OCFS2_SB(sb), handle);
609out:
610 brelse(bh);
611 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
612 return ERR_PTR(status);
613}
614
615/* Find free entry in local quota file */
616static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
617 struct super_block *sb,
618 int type,
619 int *offset)
620{
621 struct mem_dqinfo *info = sb_dqinfo(sb, type);
622 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
623 struct ocfs2_quota_chunk *chunk;
624 struct inode *lqinode = sb_dqopt(sb)->files[type];
625 struct ocfs2_local_disk_chunk *dchunk;
626 int epb = ol_quota_entries_per_block(sb);
627 unsigned int chunk_blocks;
628 int status;
629 handle_t *handle;
630
631 if (list_empty(&oinfo->dqi_chunk))
632 return ocfs2_local_quota_add_chunk(sb, type, offset);
633 /* Is the last chunk full? */
634 chunk = list_entry(oinfo->dqi_chunk.prev,
635 struct ocfs2_quota_chunk, qc_chunk);
636 chunk_blocks = oinfo->dqi_blocks -
637 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
638 if (ol_chunk_blocks(sb) == chunk_blocks)
639 return ocfs2_local_quota_add_chunk(sb, type, offset);
640
641 /* We are protected by dqio_sem so no locking needed */
642 status = ocfs2_extend_no_holes(lqinode,
643 lqinode->i_size + sb->s_blocksize,
644 lqinode->i_size);
645 if (status < 0) {
646 mlog_errno(status);
647 goto out;
648 }
649 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
650 lqinode->i_size + sb->s_blocksize);
651 if (status < 0) {
652 mlog_errno(status);
653 goto out;
654 }
655 handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
656 if (IS_ERR(handle)) {
657 status = PTR_ERR(handle);
658 mlog_errno(status);
659 goto out;
660 }
661 status = ocfs2_journal_access(handle, lqinode, chunk->qc_headerbh,
662 OCFS2_JOURNAL_ACCESS_WRITE);
663 if (status < 0) {
664 mlog_errno(status);
665 goto out_trans;
666 }
667
668 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
669 lock_buffer(chunk->qc_headerbh);
670 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
671 unlock_buffer(chunk->qc_headerbh);
672 status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
673 if (status < 0) {
674 mlog_errno(status);
675 goto out_trans;
676 }
677 oinfo->dqi_blocks++;
678 status = ocfs2_local_write_info(sb, type);
679 if (status < 0) {
680 mlog_errno(status);
681 goto out_trans;
682 }
683
684 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
685 if (status < 0) {
686 mlog_errno(status);
687 goto out;
688 }
689 *offset = chunk_blocks * epb;
690 return chunk;
691out_trans:
692 ocfs2_commit_trans(OCFS2_SB(sb), handle);
693out:
694 return ERR_PTR(status);
695}
696
697void olq_alloc_dquot(struct buffer_head *bh, void *private)
698{
699 int *offset = private;
700 struct ocfs2_local_disk_chunk *dchunk;
701
702 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
703 ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
704 le32_add_cpu(&dchunk->dqc_free, -1);
705}
706
707/* Create dquot in the local file for given id */
708static int ocfs2_create_local_dquot(struct dquot *dquot)
709{
710 struct super_block *sb = dquot->dq_sb;
711 int type = dquot->dq_type;
712 struct inode *lqinode = sb_dqopt(sb)->files[type];
713 struct ocfs2_quota_chunk *chunk;
714 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
715 int offset;
716 int status;
717
718 chunk = ocfs2_find_free_entry(sb, type, &offset);
719 if (!chunk) {
720 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
721 if (IS_ERR(chunk))
722 return PTR_ERR(chunk);
723 } else if (IS_ERR(chunk)) {
724 return PTR_ERR(chunk);
725 }
726 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
727 od->dq_chunk = chunk;
728
729 /* Initialize dquot structure on disk */
730 status = ocfs2_local_write_dquot(dquot);
731 if (status < 0) {
732 mlog_errno(status);
733 goto out;
734 }
735
736 /* Mark structure as allocated */
737 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
738 &offset);
739 if (status < 0) {
740 mlog_errno(status);
741 goto out;
742 }
743out:
744 return status;
745}
746
747/* Create entry in local file for dquot, load data from the global file */
748static int ocfs2_local_read_dquot(struct dquot *dquot)
749{
750 int status;
751
752 mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
753
754 status = ocfs2_global_read_dquot(dquot);
755 if (status < 0) {
756 mlog_errno(status);
757 goto out_err;
758 }
759
760 /* Now create entry in the local quota file */
761 status = ocfs2_create_local_dquot(dquot);
762 if (status < 0) {
763 mlog_errno(status);
764 goto out_err;
765 }
766 mlog_exit(0);
767 return 0;
768out_err:
769 mlog_exit(status);
770 return status;
771}
772
773/* Release dquot structure from local quota file. ocfs2_release_dquot() has
774 * already started a transaction and obtained exclusive lock for global
775 * quota file. */
776static int ocfs2_local_release_dquot(struct dquot *dquot)
777{
778 int status;
779 int type = dquot->dq_type;
780 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
781 struct super_block *sb = dquot->dq_sb;
782 struct ocfs2_local_disk_chunk *dchunk;
783 int offset;
784 handle_t *handle = journal_current_handle();
785
786 BUG_ON(!handle);
787 /* First write all local changes to global file */
788 status = ocfs2_global_release_dquot(dquot);
789 if (status < 0) {
790 mlog_errno(status);
791 goto out;
792 }
793
794 status = ocfs2_journal_access(handle, sb_dqopt(sb)->files[type],
795 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
796 if (status < 0) {
797 mlog_errno(status);
798 goto out;
799 }
800 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
801 od->dq_local_off);
802 dchunk = (struct ocfs2_local_disk_chunk *)
803 (od->dq_chunk->qc_headerbh->b_data);
804 /* Mark structure as freed */
805 lock_buffer(od->dq_chunk->qc_headerbh);
806 ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
807 le32_add_cpu(&dchunk->dqc_free, 1);
808 unlock_buffer(od->dq_chunk->qc_headerbh);
809 status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
810 if (status < 0) {
811 mlog_errno(status);
812 goto out;
813 }
814 status = 0;
815out:
816 /* Clear the read bit so that next time someone uses this
817 * dquot he reads fresh info from disk and allocates local
818 * dquot structure */
819 clear_bit(DQ_READ_B, &dquot->dq_flags);
820 return status;
821}
822
823static struct quota_format_ops ocfs2_format_ops = {
824 .check_quota_file = ocfs2_local_check_quota_file,
825 .read_file_info = ocfs2_local_read_info,
826 .write_file_info = ocfs2_global_write_info,
827 .free_file_info = ocfs2_local_free_info,
828 .read_dqblk = ocfs2_local_read_dquot,
829 .commit_dqblk = ocfs2_local_write_dquot,
830 .release_dqblk = ocfs2_local_release_dquot,
831};
832
833struct quota_format_type ocfs2_quota_format = {
834 .qf_fmt_id = QFMT_OCFS2,
835 .qf_ops = &ocfs2_format_ops,
836 .qf_owner = THIS_MODULE
837};