blob: 3d0b63d34225e8b93a79cb46d67a796e5dcd7323 [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
Jan Kara9e33d692008-08-25 19:56:50 +020011#include <cluster/masklog.h>
12
13#include "ocfs2_fs.h"
14#include "ocfs2.h"
15#include "inode.h"
16#include "alloc.h"
17#include "file.h"
18#include "buffer_head_io.h"
19#include "journal.h"
20#include "sysfile.h"
21#include "dlmglue.h"
22#include "quota.h"
Jan Kara4b3fa192009-07-22 13:17:16 +020023#include "uptodate.h"
Jan Karafb8dd8d2010-03-31 16:25:37 +020024#include "super.h"
Tao Ma38877a42011-02-23 22:12:48 +080025#include "ocfs2_trace.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
Jan Kara9e33d692008-08-25 19:56:50 +020076static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
77{
78 return off & ((1 << sb->s_blocksize_bits) - 1);
79}
80
81/* Compute offset in the chunk of a structure with the given offset */
82static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
83{
84 int epb = ol_quota_entries_per_block(sb);
85
86 return ((off >> sb->s_blocksize_bits) -
87 ol_quota_chunk_block(sb, c) - 1) * epb
88 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
89 sizeof(struct ocfs2_local_disk_dqblk);
90}
91
92/* Write bufferhead into the fs */
93static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
94 void (*modify)(struct buffer_head *, void *), void *private)
95{
96 struct super_block *sb = inode->i_sb;
97 handle_t *handle;
98 int status;
99
Jan Kara05849742009-07-22 13:17:21 +0200100 handle = ocfs2_start_trans(OCFS2_SB(sb),
101 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +0200102 if (IS_ERR(handle)) {
103 status = PTR_ERR(handle);
104 mlog_errno(status);
105 return status;
106 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800107 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700108 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara9e33d692008-08-25 19:56:50 +0200109 if (status < 0) {
110 mlog_errno(status);
111 ocfs2_commit_trans(OCFS2_SB(sb), handle);
112 return status;
113 }
114 lock_buffer(bh);
115 modify(bh, private);
116 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700117 ocfs2_journal_dirty(handle, bh);
118
Jan Kara9e33d692008-08-25 19:56:50 +0200119 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
120 if (status < 0) {
121 mlog_errno(status);
122 return status;
123 }
124 return 0;
125}
126
Jan Karafb8dd8d2010-03-31 16:25:37 +0200127/*
128 * Read quota block from a given logical offset.
129 *
130 * This function acquires ip_alloc_sem and thus it must not be called with a
131 * transaction started.
132 */
133static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
134 struct buffer_head **bh)
135{
136 int rc = 0;
137 struct buffer_head *tmp = *bh;
138
139 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
140 ocfs2_error(inode->i_sb,
141 "Quota file %llu is probably corrupted! Requested "
142 "to read block %Lu but file has size only %Lu\n",
143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
144 (unsigned long long)v_block,
145 (unsigned long long)i_size_read(inode));
146 return -EIO;
147 }
148 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
149 ocfs2_validate_quota_block);
150 if (rc)
151 mlog_errno(rc);
152
153 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
154 if (!rc && !*bh)
155 *bh = tmp;
156
157 return rc;
158}
159
Jan Kara9e33d692008-08-25 19:56:50 +0200160/* Check whether we understand format of quota files */
161static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
162{
Jan Kara52362812014-09-10 21:06:39 +0200163 unsigned int lmagics[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
164 unsigned int lversions[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
165 unsigned int gmagics[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
166 unsigned int gversions[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
167 unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
168 GROUP_QUOTA_SYSTEM_INODE };
Joel Becker85eb8b72008-11-25 15:31:27 +0100169 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200170 struct inode *linode = sb_dqopt(sb)->files[type];
171 struct inode *ginode = NULL;
172 struct ocfs2_disk_dqheader *dqhead;
173 int status, ret = 0;
174
175 /* First check whether we understand local quota file */
Joel Becker85eb8b72008-11-25 15:31:27 +0100176 status = ocfs2_read_quota_block(linode, 0, &bh);
177 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200178 mlog_errno(status);
179 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
180 type);
181 goto out_err;
182 }
183 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
184 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
185 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
186 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
187 lmagics[type], type);
188 goto out_err;
189 }
190 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
191 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
192 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
193 lversions[type], type);
194 goto out_err;
195 }
196 brelse(bh);
197 bh = NULL;
198
199 /* Next check whether we understand global quota file */
200 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
201 OCFS2_INVALID_SLOT);
202 if (!ginode) {
203 mlog(ML_ERROR, "cannot get global quota file inode "
204 "(type=%d)\n", type);
205 goto out_err;
206 }
207 /* Since the header is read only, we don't care about locking */
Joel Becker85eb8b72008-11-25 15:31:27 +0100208 status = ocfs2_read_quota_block(ginode, 0, &bh);
209 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200210 mlog_errno(status);
211 mlog(ML_ERROR, "failed to read global quota file header "
212 "(type=%d)\n", type);
213 goto out_err;
214 }
215 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
216 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
217 mlog(ML_ERROR, "global quota file magic does not match "
218 "(%u != %u), type=%d\n",
219 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
220 goto out_err;
221 }
222 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
223 mlog(ML_ERROR, "global quota file version does not match "
224 "(%u != %u), type=%d\n",
225 le32_to_cpu(dqhead->dqh_version), gversions[type],
226 type);
227 goto out_err;
228 }
229
230 ret = 1;
231out_err:
232 brelse(bh);
233 iput(ginode);
234 return ret;
235}
236
237/* Release given list of quota file chunks */
238static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
239{
240 struct ocfs2_quota_chunk *pos, *next;
241
242 list_for_each_entry_safe(pos, next, head, qc_chunk) {
243 list_del(&pos->qc_chunk);
244 brelse(pos->qc_headerbh);
245 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
246 }
247}
248
249/* Load quota bitmaps into memory */
250static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
251 struct ocfs2_local_disk_dqinfo *ldinfo,
252 struct list_head *head)
253{
254 struct ocfs2_quota_chunk *newchunk;
255 int i, status;
256
257 INIT_LIST_HEAD(head);
258 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
259 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
260 if (!newchunk) {
261 ocfs2_release_local_quota_bitmaps(head);
262 return -ENOMEM;
263 }
264 newchunk->qc_num = i;
Joel Becker85eb8b72008-11-25 15:31:27 +0100265 newchunk->qc_headerbh = NULL;
266 status = ocfs2_read_quota_block(inode,
Jan Kara9e33d692008-08-25 19:56:50 +0200267 ol_quota_chunk_block(inode->i_sb, i),
Joel Becker85eb8b72008-11-25 15:31:27 +0100268 &newchunk->qc_headerbh);
269 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200270 mlog_errno(status);
271 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
272 ocfs2_release_local_quota_bitmaps(head);
273 return status;
274 }
275 list_add_tail(&newchunk->qc_chunk, head);
276 }
277 return 0;
278}
279
280static void olq_update_info(struct buffer_head *bh, void *private)
281{
282 struct mem_dqinfo *info = private;
283 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
284 struct ocfs2_local_disk_dqinfo *ldinfo;
285
286 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
287 OCFS2_LOCAL_INFO_OFF);
288 spin_lock(&dq_data_lock);
Jan Kara96827ad2014-11-19 10:41:41 +0100289 ldinfo->dqi_flags = cpu_to_le32(oinfo->dqi_flags);
Jan Kara9e33d692008-08-25 19:56:50 +0200290 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
291 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
292 spin_unlock(&dq_data_lock);
293}
294
Jan Kara22053632008-10-20 23:50:38 +0200295static int ocfs2_add_recovery_chunk(struct super_block *sb,
296 struct ocfs2_local_disk_chunk *dchunk,
297 int chunk,
298 struct list_head *head)
299{
300 struct ocfs2_recovery_chunk *rc;
301
302 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
303 if (!rc)
304 return -ENOMEM;
305 rc->rc_chunk = chunk;
306 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
307 if (!rc->rc_bitmap) {
308 kfree(rc);
309 return -ENOMEM;
310 }
311 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
312 (ol_chunk_entries(sb) + 7) >> 3);
313 list_add_tail(&rc->rc_list, head);
314 return 0;
315}
316
317static void free_recovery_list(struct list_head *head)
318{
319 struct ocfs2_recovery_chunk *next;
320 struct ocfs2_recovery_chunk *rchunk;
321
322 list_for_each_entry_safe(rchunk, next, head, rc_list) {
323 list_del(&rchunk->rc_list);
324 kfree(rchunk->rc_bitmap);
325 kfree(rchunk);
326 }
327}
328
329void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
330{
331 int type;
332
Jan Kara52362812014-09-10 21:06:39 +0200333 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara22053632008-10-20 23:50:38 +0200334 free_recovery_list(&(rec->r_list[type]));
335 kfree(rec);
336}
337
338/* Load entries in our quota file we have to recover*/
339static int ocfs2_recovery_load_quota(struct inode *lqinode,
340 struct ocfs2_local_disk_dqinfo *ldinfo,
341 int type,
342 struct list_head *head)
343{
344 struct super_block *sb = lqinode->i_sb;
345 struct buffer_head *hbh;
346 struct ocfs2_local_disk_chunk *dchunk;
347 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
348 int status = 0;
349
350 for (i = 0; i < chunks; i++) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100351 hbh = NULL;
352 status = ocfs2_read_quota_block(lqinode,
353 ol_quota_chunk_block(sb, i),
354 &hbh);
355 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200356 mlog_errno(status);
357 break;
358 }
359 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
360 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
361 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
362 brelse(hbh);
363 if (status < 0)
364 break;
365 }
366 if (status < 0)
367 free_recovery_list(head);
368 return status;
369}
370
371static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
372{
373 int type;
374 struct ocfs2_quota_recovery *rec;
375
376 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
377 if (!rec)
378 return NULL;
Jan Kara52362812014-09-10 21:06:39 +0200379 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara22053632008-10-20 23:50:38 +0200380 INIT_LIST_HEAD(&(rec->r_list[type]));
381 return rec;
382}
383
384/* Load information we need for quota recovery into memory */
385struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
386 struct ocfs2_super *osb,
387 int slot_num)
388{
Jan Kara52362812014-09-10 21:06:39 +0200389 unsigned int feature[OCFS2_MAXQUOTAS] = {
390 OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
391 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
392 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
393 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
Jan Kara22053632008-10-20 23:50:38 +0200394 struct super_block *sb = osb->sb;
395 struct ocfs2_local_disk_dqinfo *ldinfo;
396 struct inode *lqinode;
397 struct buffer_head *bh;
398 int type;
399 int status = 0;
400 struct ocfs2_quota_recovery *rec;
401
Sunil Mushran619c2002011-07-24 10:34:54 -0700402 printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
403 "slot %u\n", osb->dev_str, slot_num);
404
Jan Kara22053632008-10-20 23:50:38 +0200405 rec = ocfs2_alloc_quota_recovery();
406 if (!rec)
407 return ERR_PTR(-ENOMEM);
408 /* First init... */
409
Jan Kara52362812014-09-10 21:06:39 +0200410 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara22053632008-10-20 23:50:38 +0200411 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
412 continue;
413 /* At this point, journal of the slot is already replayed so
414 * we can trust metadata and data of the quota file */
415 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
416 if (!lqinode) {
417 status = -ENOENT;
418 goto out;
419 }
420 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
421 OCFS2_META_LOCK_RECOVERY);
422 if (status < 0) {
423 mlog_errno(status);
424 goto out_put;
425 }
426 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100427 bh = NULL;
428 status = ocfs2_read_quota_block(lqinode, 0, &bh);
429 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200430 mlog_errno(status);
431 mlog(ML_ERROR, "failed to read quota file info header "
432 "(slot=%d type=%d)\n", slot_num, type);
433 goto out_lock;
434 }
435 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
436 OCFS2_LOCAL_INFO_OFF);
437 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
438 &rec->r_list[type]);
439 brelse(bh);
440out_lock:
441 ocfs2_inode_unlock(lqinode, 1);
442out_put:
443 iput(lqinode);
444 if (status < 0)
445 break;
446 }
447out:
448 if (status < 0) {
449 ocfs2_free_quota_recovery(rec);
450 rec = ERR_PTR(status);
451 }
452 return rec;
453}
454
455/* Sync changes in local quota file into global quota file and
456 * reinitialize local quota file.
457 * The function expects local quota file to be already locked and
458 * dqonoff_mutex locked. */
459static int ocfs2_recover_local_quota_file(struct inode *lqinode,
460 int type,
461 struct ocfs2_quota_recovery *rec)
462{
463 struct super_block *sb = lqinode->i_sb;
464 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
465 struct ocfs2_local_disk_chunk *dchunk;
466 struct ocfs2_local_disk_dqblk *dqblk;
467 struct dquot *dquot;
468 handle_t *handle;
469 struct buffer_head *hbh = NULL, *qbh = NULL;
470 int status = 0;
471 int bit, chunk;
472 struct ocfs2_recovery_chunk *rchunk, *next;
473 qsize_t spacechange, inodechange;
474
Tao Ma38877a42011-02-23 22:12:48 +0800475 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
Jan Kara22053632008-10-20 23:50:38 +0200476
Jan Kara22053632008-10-20 23:50:38 +0200477 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
478 chunk = rchunk->rc_chunk;
Joel Becker85eb8b72008-11-25 15:31:27 +0100479 hbh = NULL;
480 status = ocfs2_read_quota_block(lqinode,
481 ol_quota_chunk_block(sb, chunk),
482 &hbh);
483 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200484 mlog_errno(status);
485 break;
486 }
487 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
Akinobu Mita984b3f52010-03-05 13:41:37 -0800488 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100489 qbh = NULL;
490 status = ocfs2_read_quota_block(lqinode,
Jan Kara22053632008-10-20 23:50:38 +0200491 ol_dqblk_block(sb, chunk, bit),
Joel Becker85eb8b72008-11-25 15:31:27 +0100492 &qbh);
493 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200494 mlog_errno(status);
495 break;
496 }
497 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
498 ol_dqblk_block_off(sb, chunk, bit));
Eric W. Biedermanaca645a2012-09-16 03:11:50 -0700499 dquot = dqget(sb,
500 make_kqid(&init_user_ns, type,
501 le64_to_cpu(dqblk->dqb_id)));
Jan Kara22053632008-10-20 23:50:38 +0200502 if (!dquot) {
503 status = -EIO;
504 mlog(ML_ERROR, "Failed to get quota structure "
505 "for id %u, type %d. Cannot finish quota "
506 "file recovery.\n",
507 (unsigned)le64_to_cpu(dqblk->dqb_id),
508 type);
509 goto out_put_bh;
510 }
Jan Kara80d73f12009-06-02 14:24:02 +0200511 status = ocfs2_lock_global_qf(oinfo, 1);
512 if (status < 0) {
513 mlog_errno(status);
514 goto out_put_dquot;
515 }
516
Jan Kara22053632008-10-20 23:50:38 +0200517 handle = ocfs2_start_trans(OCFS2_SB(sb),
518 OCFS2_QSYNC_CREDITS);
519 if (IS_ERR(handle)) {
520 status = PTR_ERR(handle);
521 mlog_errno(status);
Jan Kara80d73f12009-06-02 14:24:02 +0200522 goto out_drop_lock;
Jan Kara22053632008-10-20 23:50:38 +0200523 }
524 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
525 spin_lock(&dq_data_lock);
526 /* Add usage from quota entry into quota changes
527 * of our node. Auxiliary variables are important
528 * due to signedness */
529 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
530 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
531 dquot->dq_dqb.dqb_curspace += spacechange;
532 dquot->dq_dqb.dqb_curinodes += inodechange;
533 spin_unlock(&dq_data_lock);
534 /* We want to drop reference held by the crashed
535 * node. Since we have our own reference we know
536 * global structure actually won't be freed. */
537 status = ocfs2_global_release_dquot(dquot);
538 if (status < 0) {
539 mlog_errno(status);
540 goto out_commit;
541 }
542 /* Release local quota file entry */
Joel Becker0cf2f762009-02-12 16:41:25 -0800543 status = ocfs2_journal_access_dq(handle,
544 INODE_CACHE(lqinode),
Jan Kara22053632008-10-20 23:50:38 +0200545 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
546 if (status < 0) {
547 mlog_errno(status);
548 goto out_commit;
549 }
550 lock_buffer(qbh);
Akinobu Mita93925572011-11-15 14:56:34 -0800551 WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
552 ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
Jan Kara22053632008-10-20 23:50:38 +0200553 le32_add_cpu(&dchunk->dqc_free, 1);
554 unlock_buffer(qbh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700555 ocfs2_journal_dirty(handle, qbh);
Jan Kara22053632008-10-20 23:50:38 +0200556out_commit:
557 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
558 ocfs2_commit_trans(OCFS2_SB(sb), handle);
Jan Kara80d73f12009-06-02 14:24:02 +0200559out_drop_lock:
560 ocfs2_unlock_global_qf(oinfo, 1);
Jan Kara22053632008-10-20 23:50:38 +0200561out_put_dquot:
562 dqput(dquot);
563out_put_bh:
564 brelse(qbh);
565 if (status < 0)
566 break;
567 }
568 brelse(hbh);
569 list_del(&rchunk->rc_list);
570 kfree(rchunk->rc_bitmap);
571 kfree(rchunk);
572 if (status < 0)
573 break;
574 }
Jan Kara22053632008-10-20 23:50:38 +0200575 if (status < 0)
576 free_recovery_list(&(rec->r_list[type]));
Tao Mac1e8d352011-03-07 16:43:21 +0800577 if (status)
578 mlog_errno(status);
Jan Kara22053632008-10-20 23:50:38 +0200579 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{
Jan Kara52362812014-09-10 21:06:39 +0200587 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
588 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
Jan Kara22053632008-10-20 23:50:38 +0200589 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
Sunil Mushran619c2002011-07-24 10:34:54 -0700598 printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
599 "slot %u\n", osb->dev_str, slot_num);
600
Jan Kara22053632008-10-20 23:50:38 +0200601 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Jan Kara52362812014-09-10 21:06:39 +0200602 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara22053632008-10-20 23:50:38 +0200603 if (list_empty(&(rec->r_list[type])))
604 continue;
Tao Ma38877a42011-02-23 22:12:48 +0800605 trace_ocfs2_finish_quota_recovery(slot_num);
Jan Kara22053632008-10-20 23:50:38 +0200606 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
607 if (!lqinode) {
608 status = -ENOENT;
609 goto out;
610 }
611 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
612 OCFS2_META_LOCK_NOQUEUE);
613 /* Someone else is holding the lock? Then he must be
614 * doing the recovery. Just skip the file... */
615 if (status == -EAGAIN) {
Sunil Mushran619c2002011-07-24 10:34:54 -0700616 printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
617 "device (%s) for slot %d because quota file is "
618 "locked.\n", osb->dev_str, slot_num);
Jan Kara22053632008-10-20 23:50:38 +0200619 status = 0;
620 goto out_put;
621 } else if (status < 0) {
622 mlog_errno(status);
623 goto out_put;
624 }
625 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100626 bh = NULL;
627 status = ocfs2_read_quota_block(lqinode, 0, &bh);
628 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200629 mlog_errno(status);
630 mlog(ML_ERROR, "failed to read quota file info header "
631 "(slot=%d type=%d)\n", slot_num, type);
632 goto out_lock;
633 }
634 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
635 OCFS2_LOCAL_INFO_OFF);
636 /* Is recovery still needed? */
637 flags = le32_to_cpu(ldinfo->dqi_flags);
638 if (!(flags & OLQF_CLEAN))
639 status = ocfs2_recover_local_quota_file(lqinode,
640 type,
641 rec);
642 /* We don't want to mark file as clean when it is actually
643 * active */
644 if (slot_num == osb->slot_num)
645 goto out_bh;
646 /* Mark quota file as clean if we are recovering quota file of
647 * some other node. */
Jan Kara05849742009-07-22 13:17:21 +0200648 handle = ocfs2_start_trans(osb,
649 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
Jan Kara22053632008-10-20 23:50:38 +0200650 if (IS_ERR(handle)) {
651 status = PTR_ERR(handle);
652 mlog_errno(status);
653 goto out_bh;
654 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800655 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
656 bh,
Joel Becker13723d02008-10-17 19:25:01 -0700657 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara22053632008-10-20 23:50:38 +0200658 if (status < 0) {
659 mlog_errno(status);
660 goto out_trans;
661 }
662 lock_buffer(bh);
663 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
664 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700665 ocfs2_journal_dirty(handle, bh);
Jan Kara22053632008-10-20 23:50:38 +0200666out_trans:
667 ocfs2_commit_trans(osb, handle);
668out_bh:
669 brelse(bh);
670out_lock:
671 ocfs2_inode_unlock(lqinode, 1);
672out_put:
673 iput(lqinode);
674 if (status < 0)
675 break;
676 }
677out:
678 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
679 kfree(rec);
680 return status;
681}
682
Jan Kara9e33d692008-08-25 19:56:50 +0200683/* Read information header from quota file */
684static int ocfs2_local_read_info(struct super_block *sb, int type)
685{
686 struct ocfs2_local_disk_dqinfo *ldinfo;
687 struct mem_dqinfo *info = sb_dqinfo(sb, type);
688 struct ocfs2_mem_dqinfo *oinfo;
689 struct inode *lqinode = sb_dqopt(sb)->files[type];
690 int status;
691 struct buffer_head *bh = NULL;
Jan Kara22053632008-10-20 23:50:38 +0200692 struct ocfs2_quota_recovery *rec;
Jan Kara9e33d692008-08-25 19:56:50 +0200693 int locked = 0;
694
Jan Karab4c30de2009-06-02 14:24:00 +0200695 /* We don't need the lock and we have to acquire quota file locks
696 * which will later depend on this lock */
697 mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
Jan Karab10a0812014-10-09 16:54:13 +0200698 info->dqi_max_spc_limit = 0x7fffffffffffffffLL;
699 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
Jan Kara9e33d692008-08-25 19:56:50 +0200700 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
701 if (!oinfo) {
702 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
703 " info.");
704 goto out_err;
705 }
706 info->dqi_priv = oinfo;
707 oinfo->dqi_type = type;
708 INIT_LIST_HEAD(&oinfo->dqi_chunk);
Jan Kara22053632008-10-20 23:50:38 +0200709 oinfo->dqi_rec = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200710 oinfo->dqi_lqi_bh = NULL;
Jan Karaae4f6ef2010-04-28 19:04:29 +0200711 oinfo->dqi_libh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200712
713 status = ocfs2_global_read_info(sb, type);
714 if (status < 0)
715 goto out_err;
716
717 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
718 if (status < 0) {
719 mlog_errno(status);
720 goto out_err;
721 }
722 locked = 1;
723
724 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100725 status = ocfs2_read_quota_block(lqinode, 0, &bh);
726 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200727 mlog_errno(status);
728 mlog(ML_ERROR, "failed to read quota file info header "
729 "(type=%d)\n", type);
730 goto out_err;
731 }
732 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
733 OCFS2_LOCAL_INFO_OFF);
Jan Kara96827ad2014-11-19 10:41:41 +0100734 oinfo->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
Jan Kara9e33d692008-08-25 19:56:50 +0200735 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
736 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200737 oinfo->dqi_libh = bh;
Jan Kara9e33d692008-08-25 19:56:50 +0200738
739 /* We crashed when using local quota file? */
Jan Kara96827ad2014-11-19 10:41:41 +0100740 if (!(oinfo->dqi_flags & OLQF_CLEAN)) {
Jan Kara22053632008-10-20 23:50:38 +0200741 rec = OCFS2_SB(sb)->quota_rec;
742 if (!rec) {
743 rec = ocfs2_alloc_quota_recovery();
744 if (!rec) {
745 status = -ENOMEM;
746 mlog_errno(status);
747 goto out_err;
748 }
749 OCFS2_SB(sb)->quota_rec = rec;
750 }
Jan Kara9e33d692008-08-25 19:56:50 +0200751
Jan Kara22053632008-10-20 23:50:38 +0200752 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
753 &rec->r_list[type]);
754 if (status < 0) {
755 mlog_errno(status);
756 goto out_err;
757 }
758 }
759
760 status = ocfs2_load_local_quota_bitmaps(lqinode,
Jan Kara9e33d692008-08-25 19:56:50 +0200761 ldinfo,
762 &oinfo->dqi_chunk);
763 if (status < 0) {
764 mlog_errno(status);
765 goto out_err;
766 }
767
768 /* Now mark quota file as used */
Jan Kara96827ad2014-11-19 10:41:41 +0100769 oinfo->dqi_flags &= ~OLQF_CLEAN;
Jan Kara9e33d692008-08-25 19:56:50 +0200770 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
771 if (status < 0) {
772 mlog_errno(status);
773 goto out_err;
774 }
775
Jan Karab4c30de2009-06-02 14:24:00 +0200776 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200777 return 0;
778out_err:
779 if (oinfo) {
780 iput(oinfo->dqi_gqinode);
781 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
782 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
783 brelse(oinfo->dqi_lqi_bh);
784 if (locked)
785 ocfs2_inode_unlock(lqinode, 1);
786 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
787 kfree(oinfo);
788 }
789 brelse(bh);
Jan Karab4c30de2009-06-02 14:24:00 +0200790 mutex_lock(&sb_dqopt(sb)->dqio_mutex);
Jan Kara9e33d692008-08-25 19:56:50 +0200791 return -1;
792}
793
794/* Write local info to quota file */
795static int ocfs2_local_write_info(struct super_block *sb, int type)
796{
797 struct mem_dqinfo *info = sb_dqinfo(sb, type);
798 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
Jan Karaae4f6ef2010-04-28 19:04:29 +0200799 ->dqi_libh;
Jan Kara9e33d692008-08-25 19:56:50 +0200800 int status;
801
802 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
803 info);
804 if (status < 0) {
805 mlog_errno(status);
806 return -1;
807 }
808
809 return 0;
810}
811
812/* Release info from memory */
813static int ocfs2_local_free_info(struct super_block *sb, int type)
814{
815 struct mem_dqinfo *info = sb_dqinfo(sb, type);
816 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
817 struct ocfs2_quota_chunk *chunk;
818 struct ocfs2_local_disk_chunk *dchunk;
819 int mark_clean = 1, len;
820 int status;
821
Jan Kara9e33d692008-08-25 19:56:50 +0200822 iput(oinfo->dqi_gqinode);
823 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
824 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
825 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
826 dchunk = (struct ocfs2_local_disk_chunk *)
827 (chunk->qc_headerbh->b_data);
828 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
829 len = ol_chunk_entries(sb);
830 } else {
831 len = (oinfo->dqi_blocks -
832 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
833 * ol_quota_entries_per_block(sb);
834 }
835 /* Not all entries free? Bug! */
836 if (le32_to_cpu(dchunk->dqc_free) != len) {
837 mlog(ML_ERROR, "releasing quota file with used "
838 "entries (type=%d)\n", type);
839 mark_clean = 0;
840 }
841 }
842 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
843
Jan Kara22053632008-10-20 23:50:38 +0200844 /* dqonoff_mutex protects us against racing with recovery thread... */
845 if (oinfo->dqi_rec) {
846 ocfs2_free_quota_recovery(oinfo->dqi_rec);
847 mark_clean = 0;
848 }
849
Jan Kara9e33d692008-08-25 19:56:50 +0200850 if (!mark_clean)
851 goto out;
852
853 /* Mark local file as clean */
Jan Kara96827ad2014-11-19 10:41:41 +0100854 oinfo->dqi_flags |= OLQF_CLEAN;
Jan Kara9e33d692008-08-25 19:56:50 +0200855 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
Jan Karaae4f6ef2010-04-28 19:04:29 +0200856 oinfo->dqi_libh,
Jan Kara9e33d692008-08-25 19:56:50 +0200857 olq_update_info,
858 info);
859 if (status < 0) {
860 mlog_errno(status);
861 goto out;
862 }
863
864out:
865 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200866 brelse(oinfo->dqi_libh);
Jan Kara9e33d692008-08-25 19:56:50 +0200867 brelse(oinfo->dqi_lqi_bh);
868 kfree(oinfo);
869 return 0;
870}
871
872static void olq_set_dquot(struct buffer_head *bh, void *private)
873{
874 struct ocfs2_dquot *od = private;
875 struct ocfs2_local_disk_dqblk *dqblk;
876 struct super_block *sb = od->dq_dquot.dq_sb;
877
878 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
879 + ol_dqblk_block_offset(sb, od->dq_local_off));
880
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700881 dqblk->dqb_id = cpu_to_le64(from_kqid(&init_user_ns,
882 od->dq_dquot.dq_id));
Jan Kara9e33d692008-08-25 19:56:50 +0200883 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);
Tao Ma38877a42011-02-23 22:12:48 +0800889 trace_olq_set_dquot(
890 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
891 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700892 from_kqid(&init_user_ns, od->dq_dquot.dq_id));
Jan Kara9e33d692008-08-25 19:56:50 +0200893}
894
895/* Write dquot to local quota file */
Jan Kara741e1282010-05-13 18:05:15 +0200896int ocfs2_local_write_dquot(struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +0200897{
898 struct super_block *sb = dquot->dq_sb;
899 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
Jan Karaf64dd442010-04-28 00:22:30 +0200900 struct buffer_head *bh;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700901 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_id.type];
Jan Kara9e33d692008-08-25 19:56:50 +0200902 int status;
903
Jan Karaf64dd442010-04-28 00:22:30 +0200904 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
905 &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +0100906 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200907 mlog_errno(status);
908 goto out;
909 }
Jan Karaf64dd442010-04-28 00:22:30 +0200910 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
Jan Kara9e33d692008-08-25 19:56:50 +0200911 if (status < 0) {
912 mlog_errno(status);
913 goto out;
914 }
915out:
916 brelse(bh);
917 return status;
918}
919
920/* Find free entry in local quota file */
921static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
922 int type,
923 int *offset)
924{
925 struct mem_dqinfo *info = sb_dqinfo(sb, type);
926 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
927 struct ocfs2_quota_chunk *chunk;
928 struct ocfs2_local_disk_chunk *dchunk;
929 int found = 0, len;
930
931 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
932 dchunk = (struct ocfs2_local_disk_chunk *)
933 chunk->qc_headerbh->b_data;
934 if (le32_to_cpu(dchunk->dqc_free) > 0) {
935 found = 1;
936 break;
937 }
938 }
939 if (!found)
940 return NULL;
941
942 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
943 len = ol_chunk_entries(sb);
944 } else {
945 len = (oinfo->dqi_blocks -
946 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
947 * ol_quota_entries_per_block(sb);
948 }
949
Akinobu Mita93925572011-11-15 14:56:34 -0800950 found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
Jan Kara9e33d692008-08-25 19:56:50 +0200951 /* We failed? */
952 if (found == len) {
953 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
954 " entries free (type=%d)\n", chunk->qc_num,
955 le32_to_cpu(dchunk->dqc_free), type);
956 return ERR_PTR(-EIO);
957 }
958 *offset = found;
959 return chunk;
960}
961
962/* Add new chunk to the local quota file */
963static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
964 struct super_block *sb,
965 int type,
966 int *offset)
967{
968 struct mem_dqinfo *info = sb_dqinfo(sb, type);
969 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
970 struct inode *lqinode = sb_dqopt(sb)->files[type];
971 struct ocfs2_quota_chunk *chunk = NULL;
972 struct ocfs2_local_disk_chunk *dchunk;
973 int status;
974 handle_t *handle;
Jan Kara0e7f3872009-07-22 13:17:17 +0200975 struct buffer_head *bh = NULL, *dbh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200976 u64 p_blkno;
977
978 /* We are protected by dqio_sem so no locking needed */
Joel Becker56934862010-07-01 15:13:31 -0700979 status = ocfs2_extend_no_holes(lqinode, NULL,
Junxiao Bif17c20d2013-09-11 14:19:45 -0700980 i_size_read(lqinode) + 2 * sb->s_blocksize,
981 i_size_read(lqinode));
Jan Kara9e33d692008-08-25 19:56:50 +0200982 if (status < 0) {
983 mlog_errno(status);
984 goto out;
985 }
986 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
Junxiao Bif17c20d2013-09-11 14:19:45 -0700987 i_size_read(lqinode) + 2 * sb->s_blocksize);
Jan Kara9e33d692008-08-25 19:56:50 +0200988 if (status < 0) {
989 mlog_errno(status);
990 goto out;
991 }
992
993 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
994 if (!chunk) {
995 status = -ENOMEM;
996 mlog_errno(status);
997 goto out;
998 }
Jan Kara05849742009-07-22 13:17:21 +0200999 /* Local quota info and two new blocks we initialize */
1000 handle = ocfs2_start_trans(OCFS2_SB(sb),
1001 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1002 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001003 if (IS_ERR(handle)) {
1004 status = PTR_ERR(handle);
1005 mlog_errno(status);
1006 goto out;
1007 }
1008
Jan Kara0e7f3872009-07-22 13:17:17 +02001009 /* Initialize chunk header */
Jan Kara0e7f3872009-07-22 13:17:17 +02001010 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1011 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001012 if (status < 0) {
1013 mlog_errno(status);
1014 goto out_trans;
1015 }
1016 bh = sb_getblk(sb, p_blkno);
1017 if (!bh) {
1018 status = -ENOMEM;
1019 mlog_errno(status);
1020 goto out_trans;
1021 }
1022 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Joel Becker8cb471e2009-02-10 20:00:41 -08001023 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001024 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001025 OCFS2_JOURNAL_ACCESS_CREATE);
Jan Kara9e33d692008-08-25 19:56:50 +02001026 if (status < 0) {
1027 mlog_errno(status);
1028 goto out_trans;
1029 }
1030 lock_buffer(bh);
Tao Madf32b332008-11-25 07:21:36 +08001031 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
Jan Kara9e33d692008-08-25 19:56:50 +02001032 memset(dchunk->dqc_bitmap, 0,
1033 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1034 OCFS2_QBLK_RESERVED_SPACE);
Jan Kara9e33d692008-08-25 19:56:50 +02001035 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001036 ocfs2_journal_dirty(handle, bh);
Jan Kara9e33d692008-08-25 19:56:50 +02001037
Jan Kara0e7f3872009-07-22 13:17:17 +02001038 /* Initialize new block with structures */
Jan Kara0e7f3872009-07-22 13:17:17 +02001039 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1040 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001041 if (status < 0) {
1042 mlog_errno(status);
1043 goto out_trans;
1044 }
1045 dbh = sb_getblk(sb, p_blkno);
1046 if (!dbh) {
1047 status = -ENOMEM;
1048 mlog_errno(status);
1049 goto out_trans;
1050 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001051 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001052 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001053 OCFS2_JOURNAL_ACCESS_CREATE);
1054 if (status < 0) {
1055 mlog_errno(status);
1056 goto out_trans;
1057 }
1058 lock_buffer(dbh);
1059 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1060 unlock_buffer(dbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001061 ocfs2_journal_dirty(handle, dbh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001062
1063 /* Update local quotafile info */
Jan Kara9e33d692008-08-25 19:56:50 +02001064 oinfo->dqi_blocks += 2;
1065 oinfo->dqi_chunks++;
1066 status = ocfs2_local_write_info(sb, type);
1067 if (status < 0) {
1068 mlog_errno(status);
1069 goto out_trans;
1070 }
1071 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1072 if (status < 0) {
1073 mlog_errno(status);
1074 goto out;
1075 }
1076
1077 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1078 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1079 struct ocfs2_quota_chunk,
1080 qc_chunk)->qc_num + 1;
1081 chunk->qc_headerbh = bh;
1082 *offset = 0;
1083 return chunk;
1084out_trans:
1085 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1086out:
1087 brelse(bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001088 brelse(dbh);
Jan Kara9e33d692008-08-25 19:56:50 +02001089 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1090 return ERR_PTR(status);
1091}
1092
1093/* Find free entry in local quota file */
1094static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1095 struct super_block *sb,
1096 int type,
1097 int *offset)
1098{
1099 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1100 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1101 struct ocfs2_quota_chunk *chunk;
1102 struct inode *lqinode = sb_dqopt(sb)->files[type];
1103 struct ocfs2_local_disk_chunk *dchunk;
1104 int epb = ol_quota_entries_per_block(sb);
1105 unsigned int chunk_blocks;
Jan Kara0e7f3872009-07-22 13:17:17 +02001106 struct buffer_head *bh;
1107 u64 p_blkno;
Jan Kara9e33d692008-08-25 19:56:50 +02001108 int status;
1109 handle_t *handle;
1110
1111 if (list_empty(&oinfo->dqi_chunk))
1112 return ocfs2_local_quota_add_chunk(sb, type, offset);
1113 /* Is the last chunk full? */
1114 chunk = list_entry(oinfo->dqi_chunk.prev,
1115 struct ocfs2_quota_chunk, qc_chunk);
1116 chunk_blocks = oinfo->dqi_blocks -
1117 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1118 if (ol_chunk_blocks(sb) == chunk_blocks)
1119 return ocfs2_local_quota_add_chunk(sb, type, offset);
1120
1121 /* We are protected by dqio_sem so no locking needed */
Joel Becker56934862010-07-01 15:13:31 -07001122 status = ocfs2_extend_no_holes(lqinode, NULL,
Junxiao Bif17c20d2013-09-11 14:19:45 -07001123 i_size_read(lqinode) + sb->s_blocksize,
1124 i_size_read(lqinode));
Jan Kara9e33d692008-08-25 19:56:50 +02001125 if (status < 0) {
1126 mlog_errno(status);
1127 goto out;
1128 }
1129 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
Junxiao Bif17c20d2013-09-11 14:19:45 -07001130 i_size_read(lqinode) + sb->s_blocksize);
Jan Kara9e33d692008-08-25 19:56:50 +02001131 if (status < 0) {
1132 mlog_errno(status);
1133 goto out;
1134 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001135
1136 /* Get buffer from the just added block */
Jan Kara0e7f3872009-07-22 13:17:17 +02001137 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1138 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001139 if (status < 0) {
1140 mlog_errno(status);
1141 goto out;
1142 }
1143 bh = sb_getblk(sb, p_blkno);
1144 if (!bh) {
1145 status = -ENOMEM;
1146 mlog_errno(status);
1147 goto out;
1148 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001149 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001150
Jan Kara05849742009-07-22 13:17:21 +02001151 /* Local quota info, chunk header and the new block we initialize */
1152 handle = ocfs2_start_trans(OCFS2_SB(sb),
1153 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1154 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001155 if (IS_ERR(handle)) {
1156 status = PTR_ERR(handle);
1157 mlog_errno(status);
1158 goto out;
1159 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001160 /* Zero created block */
Joel Becker0cf2f762009-02-12 16:41:25 -08001161 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001162 OCFS2_JOURNAL_ACCESS_CREATE);
1163 if (status < 0) {
1164 mlog_errno(status);
1165 goto out_trans;
1166 }
1167 lock_buffer(bh);
1168 memset(bh->b_data, 0, sb->s_blocksize);
1169 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001170 ocfs2_journal_dirty(handle, bh);
1171
Jan Kara0e7f3872009-07-22 13:17:17 +02001172 /* Update chunk header */
Joel Becker0cf2f762009-02-12 16:41:25 -08001173 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1174 chunk->qc_headerbh,
Jan Kara9e33d692008-08-25 19:56:50 +02001175 OCFS2_JOURNAL_ACCESS_WRITE);
1176 if (status < 0) {
1177 mlog_errno(status);
1178 goto out_trans;
1179 }
1180
1181 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1182 lock_buffer(chunk->qc_headerbh);
1183 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1184 unlock_buffer(chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001185 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1186
Jan Kara0e7f3872009-07-22 13:17:17 +02001187 /* Update file header */
Jan Kara9e33d692008-08-25 19:56:50 +02001188 oinfo->dqi_blocks++;
1189 status = ocfs2_local_write_info(sb, type);
1190 if (status < 0) {
1191 mlog_errno(status);
1192 goto out_trans;
1193 }
1194
1195 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1196 if (status < 0) {
1197 mlog_errno(status);
1198 goto out;
1199 }
1200 *offset = chunk_blocks * epb;
1201 return chunk;
1202out_trans:
1203 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1204out:
1205 return ERR_PTR(status);
1206}
1207
Tao Madf32b332008-11-25 07:21:36 +08001208static void olq_alloc_dquot(struct buffer_head *bh, void *private)
Jan Kara9e33d692008-08-25 19:56:50 +02001209{
1210 int *offset = private;
1211 struct ocfs2_local_disk_chunk *dchunk;
1212
1213 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Akinobu Mita93925572011-11-15 14:56:34 -08001214 ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
Jan Kara9e33d692008-08-25 19:56:50 +02001215 le32_add_cpu(&dchunk->dqc_free, -1);
1216}
1217
1218/* Create dquot in the local file for given id */
Jan Karafb8dd8d2010-03-31 16:25:37 +02001219int ocfs2_create_local_dquot(struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001220{
1221 struct super_block *sb = dquot->dq_sb;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001222 int type = dquot->dq_id.type;
Jan Kara9e33d692008-08-25 19:56:50 +02001223 struct inode *lqinode = sb_dqopt(sb)->files[type];
1224 struct ocfs2_quota_chunk *chunk;
1225 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1226 int offset;
1227 int status;
Jan Karaf64dd442010-04-28 00:22:30 +02001228 u64 pcount;
Jan Kara9e33d692008-08-25 19:56:50 +02001229
Jan Karaf64dd442010-04-28 00:22:30 +02001230 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001231 chunk = ocfs2_find_free_entry(sb, type, &offset);
1232 if (!chunk) {
1233 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
Jan Karaf64dd442010-04-28 00:22:30 +02001234 if (IS_ERR(chunk)) {
1235 status = PTR_ERR(chunk);
1236 goto out;
1237 }
Jan Kara9e33d692008-08-25 19:56:50 +02001238 } else if (IS_ERR(chunk)) {
Jan Karaf64dd442010-04-28 00:22:30 +02001239 status = PTR_ERR(chunk);
1240 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +02001241 }
1242 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1243 od->dq_chunk = chunk;
Jan Karaf64dd442010-04-28 00:22:30 +02001244 status = ocfs2_extent_map_get_blocks(lqinode,
1245 ol_dqblk_block(sb, chunk->qc_num, offset),
1246 &od->dq_local_phys_blk,
1247 &pcount,
1248 NULL);
Jan Kara9e33d692008-08-25 19:56:50 +02001249
1250 /* Initialize dquot structure on disk */
1251 status = ocfs2_local_write_dquot(dquot);
1252 if (status < 0) {
1253 mlog_errno(status);
1254 goto out;
1255 }
1256
1257 /* Mark structure as allocated */
1258 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1259 &offset);
1260 if (status < 0) {
1261 mlog_errno(status);
1262 goto out;
1263 }
1264out:
Jan Karaf64dd442010-04-28 00:22:30 +02001265 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001266 return status;
1267}
1268
Jan Karafb8dd8d2010-03-31 16:25:37 +02001269/*
1270 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1271 * already started a transaction and written all changes to global quota file
1272 */
1273int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001274{
1275 int status;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001276 int type = dquot->dq_id.type;
Jan Kara9e33d692008-08-25 19:56:50 +02001277 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1278 struct super_block *sb = dquot->dq_sb;
1279 struct ocfs2_local_disk_chunk *dchunk;
1280 int offset;
Jan Kara9e33d692008-08-25 19:56:50 +02001281
Joel Becker0cf2f762009-02-12 16:41:25 -08001282 status = ocfs2_journal_access_dq(handle,
1283 INODE_CACHE(sb_dqopt(sb)->files[type]),
Jan Kara9e33d692008-08-25 19:56:50 +02001284 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1285 if (status < 0) {
1286 mlog_errno(status);
1287 goto out;
1288 }
1289 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1290 od->dq_local_off);
1291 dchunk = (struct ocfs2_local_disk_chunk *)
1292 (od->dq_chunk->qc_headerbh->b_data);
1293 /* Mark structure as freed */
1294 lock_buffer(od->dq_chunk->qc_headerbh);
Akinobu Mita93925572011-11-15 14:56:34 -08001295 ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
Jan Kara9e33d692008-08-25 19:56:50 +02001296 le32_add_cpu(&dchunk->dqc_free, 1);
1297 unlock_buffer(od->dq_chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001298 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1299
Jan Kara9e33d692008-08-25 19:56:50 +02001300out:
Jan Kara9e33d692008-08-25 19:56:50 +02001301 return status;
1302}
1303
Alexey Dobriyan1472da52009-10-16 15:26:03 +04001304static const struct quota_format_ops ocfs2_format_ops = {
Jan Kara9e33d692008-08-25 19:56:50 +02001305 .check_quota_file = ocfs2_local_check_quota_file,
1306 .read_file_info = ocfs2_local_read_info,
1307 .write_file_info = ocfs2_global_write_info,
1308 .free_file_info = ocfs2_local_free_info,
Jan Kara9e33d692008-08-25 19:56:50 +02001309};
1310
1311struct quota_format_type ocfs2_quota_format = {
1312 .qf_fmt_id = QFMT_OCFS2,
1313 .qf_ops = &ocfs2_format_ops,
1314 .qf_owner = THIS_MODULE
1315};