blob: b1a8b046f4c22a990af8c8bb26ecb709394dddf0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jan Kara9e33d692008-08-25 19:56:50 +02002/*
3 * Implementation of operations over local quota file
4 */
5
6#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Jan Kara9e33d692008-08-25 19:56:50 +02008#include <linux/quota.h>
9#include <linux/quotaops.h>
10#include <linux/module.h>
11
Jan Kara9e33d692008-08-25 19:56:50 +020012#include <cluster/masklog.h>
13
14#include "ocfs2_fs.h"
15#include "ocfs2.h"
16#include "inode.h"
17#include "alloc.h"
18#include "file.h"
19#include "buffer_head_io.h"
20#include "journal.h"
21#include "sysfile.h"
22#include "dlmglue.h"
23#include "quota.h"
Jan Kara4b3fa192009-07-22 13:17:16 +020024#include "uptodate.h"
Jan Karafb8dd8d2010-03-31 16:25:37 +020025#include "super.h"
Tao Ma38877a42011-02-23 22:12:48 +080026#include "ocfs2_trace.h"
Jan Kara9e33d692008-08-25 19:56:50 +020027
28/* Number of local quota structures per block */
29static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
30{
31 return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
32 sizeof(struct ocfs2_local_disk_dqblk));
33}
34
35/* Number of blocks with entries in one chunk */
36static inline unsigned int ol_chunk_blocks(struct super_block *sb)
37{
38 return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
39 OCFS2_QBLK_RESERVED_SPACE) << 3) /
40 ol_quota_entries_per_block(sb);
41}
42
43/* Number of entries in a chunk bitmap */
44static unsigned int ol_chunk_entries(struct super_block *sb)
45{
46 return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
47}
48
49/* Offset of the chunk in quota file */
50static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
51{
52 /* 1 block for local quota file info, 1 block per chunk for chunk info */
53 return 1 + (ol_chunk_blocks(sb) + 1) * c;
54}
55
Jan Kara22053632008-10-20 23:50:38 +020056static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
Jan Kara9e33d692008-08-25 19:56:50 +020057{
58 int epb = ol_quota_entries_per_block(sb);
59
Jan Kara22053632008-10-20 23:50:38 +020060 return ol_quota_chunk_block(sb, c) + 1 + off / epb;
61}
62
63static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
64{
65 int epb = ol_quota_entries_per_block(sb);
66
67 return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
68}
69
70/* Offset of the dquot structure in the quota file */
71static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
72{
73 return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
74 ol_dqblk_block_off(sb, c, off);
Jan Kara9e33d692008-08-25 19:56:50 +020075}
76
Jan Kara9e33d692008-08-25 19:56:50 +020077static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
78{
79 return off & ((1 << sb->s_blocksize_bits) - 1);
80}
81
82/* Compute offset in the chunk of a structure with the given offset */
83static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
84{
85 int epb = ol_quota_entries_per_block(sb);
86
87 return ((off >> sb->s_blocksize_bits) -
88 ol_quota_chunk_block(sb, c) - 1) * epb
89 + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
90 sizeof(struct ocfs2_local_disk_dqblk);
91}
92
93/* Write bufferhead into the fs */
94static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
95 void (*modify)(struct buffer_head *, void *), void *private)
96{
97 struct super_block *sb = inode->i_sb;
98 handle_t *handle;
99 int status;
100
Jan Kara05849742009-07-22 13:17:21 +0200101 handle = ocfs2_start_trans(OCFS2_SB(sb),
102 OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +0200103 if (IS_ERR(handle)) {
104 status = PTR_ERR(handle);
105 mlog_errno(status);
106 return status;
107 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800108 status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700109 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara9e33d692008-08-25 19:56:50 +0200110 if (status < 0) {
111 mlog_errno(status);
112 ocfs2_commit_trans(OCFS2_SB(sb), handle);
113 return status;
114 }
115 lock_buffer(bh);
116 modify(bh, private);
117 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700118 ocfs2_journal_dirty(handle, bh);
119
Jan Kara9e33d692008-08-25 19:56:50 +0200120 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
121 if (status < 0) {
122 mlog_errno(status);
123 return status;
124 }
125 return 0;
126}
127
Jan Karafb8dd8d2010-03-31 16:25:37 +0200128/*
129 * Read quota block from a given logical offset.
130 *
131 * This function acquires ip_alloc_sem and thus it must not be called with a
132 * transaction started.
133 */
134static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
135 struct buffer_head **bh)
136{
137 int rc = 0;
138 struct buffer_head *tmp = *bh;
139
Jun Piao93f59202018-08-17 15:44:24 -0700140 if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block)
141 return ocfs2_error(inode->i_sb,
142 "Quota file %llu is probably corrupted! Requested 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
Jan Karafb8dd8d2010-03-31 16:25:37 +0200147 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
148 ocfs2_validate_quota_block);
149 if (rc)
150 mlog_errno(rc);
151
152 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
153 if (!rc && !*bh)
154 *bh = tmp;
155
156 return rc;
157}
158
Jan Kara9e33d692008-08-25 19:56:50 +0200159/* Check whether we understand format of quota files */
160static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
161{
Jan Kara52362812014-09-10 21:06:39 +0200162 unsigned int lmagics[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
163 unsigned int lversions[OCFS2_MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
164 unsigned int gmagics[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
165 unsigned int gversions[OCFS2_MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
166 unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
167 GROUP_QUOTA_SYSTEM_INODE };
Joel Becker85eb8b72008-11-25 15:31:27 +0100168 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200169 struct inode *linode = sb_dqopt(sb)->files[type];
170 struct inode *ginode = NULL;
171 struct ocfs2_disk_dqheader *dqhead;
172 int status, ret = 0;
173
174 /* First check whether we understand local quota file */
Joel Becker85eb8b72008-11-25 15:31:27 +0100175 status = ocfs2_read_quota_block(linode, 0, &bh);
176 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200177 mlog_errno(status);
178 mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
179 type);
180 goto out_err;
181 }
182 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
183 if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
184 mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
185 " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
186 lmagics[type], type);
187 goto out_err;
188 }
189 if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
190 mlog(ML_ERROR, "quota file version does not match (%u != %u),"
191 " type=%d\n", le32_to_cpu(dqhead->dqh_version),
192 lversions[type], type);
193 goto out_err;
194 }
195 brelse(bh);
196 bh = NULL;
197
198 /* Next check whether we understand global quota file */
199 ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
200 OCFS2_INVALID_SLOT);
201 if (!ginode) {
202 mlog(ML_ERROR, "cannot get global quota file inode "
203 "(type=%d)\n", type);
204 goto out_err;
205 }
206 /* Since the header is read only, we don't care about locking */
Joel Becker85eb8b72008-11-25 15:31:27 +0100207 status = ocfs2_read_quota_block(ginode, 0, &bh);
208 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200209 mlog_errno(status);
210 mlog(ML_ERROR, "failed to read global quota file header "
211 "(type=%d)\n", type);
212 goto out_err;
213 }
214 dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
215 if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
216 mlog(ML_ERROR, "global quota file magic does not match "
217 "(%u != %u), type=%d\n",
218 le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
219 goto out_err;
220 }
221 if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
222 mlog(ML_ERROR, "global quota file version does not match "
223 "(%u != %u), type=%d\n",
224 le32_to_cpu(dqhead->dqh_version), gversions[type],
225 type);
226 goto out_err;
227 }
228
229 ret = 1;
230out_err:
231 brelse(bh);
232 iput(ginode);
233 return ret;
234}
235
236/* Release given list of quota file chunks */
237static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
238{
239 struct ocfs2_quota_chunk *pos, *next;
240
241 list_for_each_entry_safe(pos, next, head, qc_chunk) {
242 list_del(&pos->qc_chunk);
243 brelse(pos->qc_headerbh);
244 kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
245 }
246}
247
248/* Load quota bitmaps into memory */
249static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
250 struct ocfs2_local_disk_dqinfo *ldinfo,
251 struct list_head *head)
252{
253 struct ocfs2_quota_chunk *newchunk;
254 int i, status;
255
256 INIT_LIST_HEAD(head);
257 for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
258 newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
259 if (!newchunk) {
260 ocfs2_release_local_quota_bitmaps(head);
261 return -ENOMEM;
262 }
263 newchunk->qc_num = i;
Joel Becker85eb8b72008-11-25 15:31:27 +0100264 newchunk->qc_headerbh = NULL;
265 status = ocfs2_read_quota_block(inode,
Jan Kara9e33d692008-08-25 19:56:50 +0200266 ol_quota_chunk_block(inode->i_sb, i),
Joel Becker85eb8b72008-11-25 15:31:27 +0100267 &newchunk->qc_headerbh);
268 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200269 mlog_errno(status);
270 kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
271 ocfs2_release_local_quota_bitmaps(head);
272 return status;
273 }
274 list_add_tail(&newchunk->qc_chunk, head);
275 }
276 return 0;
277}
278
279static void olq_update_info(struct buffer_head *bh, void *private)
280{
281 struct mem_dqinfo *info = private;
282 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
283 struct ocfs2_local_disk_dqinfo *ldinfo;
284
285 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
286 OCFS2_LOCAL_INFO_OFF);
287 spin_lock(&dq_data_lock);
Jan Kara96827ad2014-11-19 10:41:41 +0100288 ldinfo->dqi_flags = cpu_to_le32(oinfo->dqi_flags);
Jan Kara9e33d692008-08-25 19:56:50 +0200289 ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
290 ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
291 spin_unlock(&dq_data_lock);
292}
293
Jan Kara22053632008-10-20 23:50:38 +0200294static int ocfs2_add_recovery_chunk(struct super_block *sb,
295 struct ocfs2_local_disk_chunk *dchunk,
296 int chunk,
297 struct list_head *head)
298{
299 struct ocfs2_recovery_chunk *rc;
300
301 rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
302 if (!rc)
303 return -ENOMEM;
304 rc->rc_chunk = chunk;
305 rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
306 if (!rc->rc_bitmap) {
307 kfree(rc);
308 return -ENOMEM;
309 }
310 memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
311 (ol_chunk_entries(sb) + 7) >> 3);
312 list_add_tail(&rc->rc_list, head);
313 return 0;
314}
315
316static void free_recovery_list(struct list_head *head)
317{
318 struct ocfs2_recovery_chunk *next;
319 struct ocfs2_recovery_chunk *rchunk;
320
321 list_for_each_entry_safe(rchunk, next, head, rc_list) {
322 list_del(&rchunk->rc_list);
323 kfree(rchunk->rc_bitmap);
324 kfree(rchunk);
325 }
326}
327
328void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
329{
330 int type;
331
Jan Kara52362812014-09-10 21:06:39 +0200332 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara22053632008-10-20 23:50:38 +0200333 free_recovery_list(&(rec->r_list[type]));
334 kfree(rec);
335}
336
337/* Load entries in our quota file we have to recover*/
338static int ocfs2_recovery_load_quota(struct inode *lqinode,
339 struct ocfs2_local_disk_dqinfo *ldinfo,
340 int type,
341 struct list_head *head)
342{
343 struct super_block *sb = lqinode->i_sb;
344 struct buffer_head *hbh;
345 struct ocfs2_local_disk_chunk *dchunk;
346 int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
347 int status = 0;
348
349 for (i = 0; i < chunks; i++) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100350 hbh = NULL;
351 status = ocfs2_read_quota_block(lqinode,
352 ol_quota_chunk_block(sb, i),
353 &hbh);
354 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200355 mlog_errno(status);
356 break;
357 }
358 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
359 if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
360 status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
361 brelse(hbh);
362 if (status < 0)
363 break;
364 }
365 if (status < 0)
366 free_recovery_list(head);
367 return status;
368}
369
370static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
371{
372 int type;
373 struct ocfs2_quota_recovery *rec;
374
375 rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
376 if (!rec)
377 return NULL;
Jan Kara52362812014-09-10 21:06:39 +0200378 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara22053632008-10-20 23:50:38 +0200379 INIT_LIST_HEAD(&(rec->r_list[type]));
380 return rec;
381}
382
383/* Load information we need for quota recovery into memory */
384struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
385 struct ocfs2_super *osb,
386 int slot_num)
387{
Jan Kara52362812014-09-10 21:06:39 +0200388 unsigned int feature[OCFS2_MAXQUOTAS] = {
389 OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
390 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
391 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
392 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
Jan Kara22053632008-10-20 23:50:38 +0200393 struct super_block *sb = osb->sb;
394 struct ocfs2_local_disk_dqinfo *ldinfo;
395 struct inode *lqinode;
396 struct buffer_head *bh;
397 int type;
398 int status = 0;
399 struct ocfs2_quota_recovery *rec;
400
Sunil Mushran619c2002011-07-24 10:34:54 -0700401 printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
402 "slot %u\n", osb->dev_str, slot_num);
403
Jan Kara22053632008-10-20 23:50:38 +0200404 rec = ocfs2_alloc_quota_recovery();
405 if (!rec)
406 return ERR_PTR(-ENOMEM);
407 /* First init... */
408
Jan Kara52362812014-09-10 21:06:39 +0200409 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara22053632008-10-20 23:50:38 +0200410 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
411 continue;
412 /* At this point, journal of the slot is already replayed so
413 * we can trust metadata and data of the quota file */
414 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
415 if (!lqinode) {
416 status = -ENOENT;
417 goto out;
418 }
419 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
420 OCFS2_META_LOCK_RECOVERY);
421 if (status < 0) {
422 mlog_errno(status);
423 goto out_put;
424 }
425 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100426 bh = NULL;
427 status = ocfs2_read_quota_block(lqinode, 0, &bh);
428 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200429 mlog_errno(status);
430 mlog(ML_ERROR, "failed to read quota file info header "
431 "(slot=%d type=%d)\n", slot_num, type);
432 goto out_lock;
433 }
434 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
435 OCFS2_LOCAL_INFO_OFF);
436 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
437 &rec->r_list[type]);
438 brelse(bh);
439out_lock:
440 ocfs2_inode_unlock(lqinode, 1);
441out_put:
442 iput(lqinode);
443 if (status < 0)
444 break;
445 }
446out:
447 if (status < 0) {
448 ocfs2_free_quota_recovery(rec);
449 rec = ERR_PTR(status);
450 }
451 return rec;
452}
453
454/* Sync changes in local quota file into global quota file and
455 * reinitialize local quota file.
456 * The function expects local quota file to be already locked and
Jan Kara5f530de2016-11-23 14:35:26 +0100457 * s_umount locked in shared mode. */
Jan Kara22053632008-10-20 23:50:38 +0200458static int ocfs2_recover_local_quota_file(struct inode *lqinode,
459 int type,
460 struct ocfs2_quota_recovery *rec)
461{
462 struct super_block *sb = lqinode->i_sb;
463 struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
464 struct ocfs2_local_disk_chunk *dchunk;
465 struct ocfs2_local_disk_dqblk *dqblk;
466 struct dquot *dquot;
467 handle_t *handle;
468 struct buffer_head *hbh = NULL, *qbh = NULL;
469 int status = 0;
470 int bit, chunk;
471 struct ocfs2_recovery_chunk *rchunk, *next;
472 qsize_t spacechange, inodechange;
473
Tao Ma38877a42011-02-23 22:12:48 +0800474 trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
Jan Kara22053632008-10-20 23:50:38 +0200475
Jan Kara22053632008-10-20 23:50:38 +0200476 list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
477 chunk = rchunk->rc_chunk;
Joel Becker85eb8b72008-11-25 15:31:27 +0100478 hbh = NULL;
479 status = ocfs2_read_quota_block(lqinode,
480 ol_quota_chunk_block(sb, chunk),
481 &hbh);
482 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200483 mlog_errno(status);
484 break;
485 }
486 dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
Akinobu Mita984b3f52010-03-05 13:41:37 -0800487 for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
Joel Becker85eb8b72008-11-25 15:31:27 +0100488 qbh = NULL;
489 status = ocfs2_read_quota_block(lqinode,
Jan Kara22053632008-10-20 23:50:38 +0200490 ol_dqblk_block(sb, chunk, bit),
Joel Becker85eb8b72008-11-25 15:31:27 +0100491 &qbh);
492 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200493 mlog_errno(status);
494 break;
495 }
496 dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
497 ol_dqblk_block_off(sb, chunk, bit));
Eric W. Biedermanaca645a2012-09-16 03:11:50 -0700498 dquot = dqget(sb,
499 make_kqid(&init_user_ns, type,
500 le64_to_cpu(dqblk->dqb_id)));
Jan Kara6184fc02015-06-24 18:07:02 +0200501 if (IS_ERR(dquot)) {
502 status = PTR_ERR(dquot);
Jan Kara22053632008-10-20 23:50:38 +0200503 mlog(ML_ERROR, "Failed to get quota structure "
504 "for id %u, type %d. Cannot finish quota "
505 "file recovery.\n",
506 (unsigned)le64_to_cpu(dqblk->dqb_id),
507 type);
508 goto out_put_bh;
509 }
Jan Kara80d73f12009-06-02 14:24:02 +0200510 status = ocfs2_lock_global_qf(oinfo, 1);
511 if (status < 0) {
512 mlog_errno(status);
513 goto out_put_dquot;
514 }
515
Jan Kara22053632008-10-20 23:50:38 +0200516 handle = ocfs2_start_trans(OCFS2_SB(sb),
517 OCFS2_QSYNC_CREDITS);
518 if (IS_ERR(handle)) {
519 status = PTR_ERR(handle);
520 mlog_errno(status);
Jan Kara80d73f12009-06-02 14:24:02 +0200521 goto out_drop_lock;
Jan Kara22053632008-10-20 23:50:38 +0200522 }
Jan Karabc8230e2017-06-08 14:39:48 +0200523 down_write(&sb_dqopt(sb)->dqio_sem);
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200524 spin_lock(&dquot->dq_dqb_lock);
Jan Kara22053632008-10-20 23:50:38 +0200525 /* Add usage from quota entry into quota changes
526 * of our node. Auxiliary variables are important
527 * due to signedness */
528 spacechange = le64_to_cpu(dqblk->dqb_spacemod);
529 inodechange = le64_to_cpu(dqblk->dqb_inodemod);
530 dquot->dq_dqb.dqb_curspace += spacechange;
531 dquot->dq_dqb.dqb_curinodes += inodechange;
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200532 spin_unlock(&dquot->dq_dqb_lock);
Jan Kara22053632008-10-20 23:50:38 +0200533 /* We want to drop reference held by the crashed
534 * node. Since we have our own reference we know
535 * global structure actually won't be freed. */
536 status = ocfs2_global_release_dquot(dquot);
537 if (status < 0) {
538 mlog_errno(status);
539 goto out_commit;
540 }
541 /* Release local quota file entry */
Joel Becker0cf2f762009-02-12 16:41:25 -0800542 status = ocfs2_journal_access_dq(handle,
543 INODE_CACHE(lqinode),
Jan Kara22053632008-10-20 23:50:38 +0200544 qbh, OCFS2_JOURNAL_ACCESS_WRITE);
545 if (status < 0) {
546 mlog_errno(status);
547 goto out_commit;
548 }
549 lock_buffer(qbh);
Akinobu Mita93925572011-11-15 14:56:34 -0800550 WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
551 ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
Jan Kara22053632008-10-20 23:50:38 +0200552 le32_add_cpu(&dchunk->dqc_free, 1);
553 unlock_buffer(qbh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700554 ocfs2_journal_dirty(handle, qbh);
Jan Kara22053632008-10-20 23:50:38 +0200555out_commit:
Jan Karabc8230e2017-06-08 14:39:48 +0200556 up_write(&sb_dqopt(sb)->dqio_sem);
Jan Kara22053632008-10-20 23:50:38 +0200557 ocfs2_commit_trans(OCFS2_SB(sb), handle);
Jan Kara80d73f12009-06-02 14:24:02 +0200558out_drop_lock:
559 ocfs2_unlock_global_qf(oinfo, 1);
Jan Kara22053632008-10-20 23:50:38 +0200560out_put_dquot:
561 dqput(dquot);
562out_put_bh:
563 brelse(qbh);
564 if (status < 0)
565 break;
566 }
567 brelse(hbh);
568 list_del(&rchunk->rc_list);
569 kfree(rchunk->rc_bitmap);
570 kfree(rchunk);
571 if (status < 0)
572 break;
573 }
Jan Kara22053632008-10-20 23:50:38 +0200574 if (status < 0)
575 free_recovery_list(&(rec->r_list[type]));
Tao Mac1e8d352011-03-07 16:43:21 +0800576 if (status)
577 mlog_errno(status);
Jan Kara22053632008-10-20 23:50:38 +0200578 return status;
579}
580
581/* Recover local quota files for given node different from us */
582int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
583 struct ocfs2_quota_recovery *rec,
584 int slot_num)
585{
Jan Kara52362812014-09-10 21:06:39 +0200586 unsigned int ino[OCFS2_MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
587 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
Jan Kara22053632008-10-20 23:50:38 +0200588 struct super_block *sb = osb->sb;
589 struct ocfs2_local_disk_dqinfo *ldinfo;
590 struct buffer_head *bh;
591 handle_t *handle;
592 int type;
593 int status = 0;
594 struct inode *lqinode;
595 unsigned int flags;
596
Sunil Mushran619c2002011-07-24 10:34:54 -0700597 printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
598 "slot %u\n", osb->dev_str, slot_num);
599
Jan Kara5f530de2016-11-23 14:35:26 +0100600 down_read(&sb->s_umount);
Jan Kara52362812014-09-10 21:06:39 +0200601 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara22053632008-10-20 23:50:38 +0200602 if (list_empty(&(rec->r_list[type])))
603 continue;
Tao Ma38877a42011-02-23 22:12:48 +0800604 trace_ocfs2_finish_quota_recovery(slot_num);
Jan Kara22053632008-10-20 23:50:38 +0200605 lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
606 if (!lqinode) {
607 status = -ENOENT;
608 goto out;
609 }
610 status = ocfs2_inode_lock_full(lqinode, NULL, 1,
611 OCFS2_META_LOCK_NOQUEUE);
612 /* Someone else is holding the lock? Then he must be
613 * doing the recovery. Just skip the file... */
614 if (status == -EAGAIN) {
Sunil Mushran619c2002011-07-24 10:34:54 -0700615 printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
616 "device (%s) for slot %d because quota file is "
617 "locked.\n", osb->dev_str, slot_num);
Jan Kara22053632008-10-20 23:50:38 +0200618 status = 0;
619 goto out_put;
620 } else if (status < 0) {
621 mlog_errno(status);
622 goto out_put;
623 }
624 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100625 bh = NULL;
626 status = ocfs2_read_quota_block(lqinode, 0, &bh);
627 if (status) {
Jan Kara22053632008-10-20 23:50:38 +0200628 mlog_errno(status);
629 mlog(ML_ERROR, "failed to read quota file info header "
630 "(slot=%d type=%d)\n", slot_num, type);
631 goto out_lock;
632 }
633 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
634 OCFS2_LOCAL_INFO_OFF);
635 /* Is recovery still needed? */
636 flags = le32_to_cpu(ldinfo->dqi_flags);
637 if (!(flags & OLQF_CLEAN))
638 status = ocfs2_recover_local_quota_file(lqinode,
639 type,
640 rec);
641 /* We don't want to mark file as clean when it is actually
642 * active */
643 if (slot_num == osb->slot_num)
644 goto out_bh;
645 /* Mark quota file as clean if we are recovering quota file of
646 * some other node. */
Jan Kara05849742009-07-22 13:17:21 +0200647 handle = ocfs2_start_trans(osb,
648 OCFS2_LOCAL_QINFO_WRITE_CREDITS);
Jan Kara22053632008-10-20 23:50:38 +0200649 if (IS_ERR(handle)) {
650 status = PTR_ERR(handle);
651 mlog_errno(status);
652 goto out_bh;
653 }
Joel Becker0cf2f762009-02-12 16:41:25 -0800654 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
655 bh,
Joel Becker13723d02008-10-17 19:25:01 -0700656 OCFS2_JOURNAL_ACCESS_WRITE);
Jan Kara22053632008-10-20 23:50:38 +0200657 if (status < 0) {
658 mlog_errno(status);
659 goto out_trans;
660 }
661 lock_buffer(bh);
662 ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
663 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -0700664 ocfs2_journal_dirty(handle, bh);
Jan Kara22053632008-10-20 23:50:38 +0200665out_trans:
666 ocfs2_commit_trans(osb, handle);
667out_bh:
668 brelse(bh);
669out_lock:
670 ocfs2_inode_unlock(lqinode, 1);
671out_put:
672 iput(lqinode);
673 if (status < 0)
674 break;
675 }
676out:
Jan Kara5f530de2016-11-23 14:35:26 +0100677 up_read(&sb->s_umount);
Jan Kara22053632008-10-20 23:50:38 +0200678 kfree(rec);
679 return status;
680}
681
Jan Kara9e33d692008-08-25 19:56:50 +0200682/* Read information header from quota file */
683static int ocfs2_local_read_info(struct super_block *sb, int type)
684{
685 struct ocfs2_local_disk_dqinfo *ldinfo;
686 struct mem_dqinfo *info = sb_dqinfo(sb, type);
687 struct ocfs2_mem_dqinfo *oinfo;
688 struct inode *lqinode = sb_dqopt(sb)->files[type];
689 int status;
690 struct buffer_head *bh = NULL;
Jan Kara22053632008-10-20 23:50:38 +0200691 struct ocfs2_quota_recovery *rec;
Jan Kara9e33d692008-08-25 19:56:50 +0200692 int locked = 0;
693
Jan Karab10a0812014-10-09 16:54:13 +0200694 info->dqi_max_spc_limit = 0x7fffffffffffffffLL;
695 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
Jan Kara9e33d692008-08-25 19:56:50 +0200696 oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
697 if (!oinfo) {
698 mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
699 " info.");
700 goto out_err;
701 }
702 info->dqi_priv = oinfo;
703 oinfo->dqi_type = type;
704 INIT_LIST_HEAD(&oinfo->dqi_chunk);
Jan Kara22053632008-10-20 23:50:38 +0200705 oinfo->dqi_rec = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200706 oinfo->dqi_lqi_bh = NULL;
Jan Karaae4f6ef2010-04-28 19:04:29 +0200707 oinfo->dqi_libh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200708
709 status = ocfs2_global_read_info(sb, type);
710 if (status < 0)
711 goto out_err;
712
713 status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
714 if (status < 0) {
715 mlog_errno(status);
716 goto out_err;
717 }
718 locked = 1;
719
720 /* Now read local header */
Joel Becker85eb8b72008-11-25 15:31:27 +0100721 status = ocfs2_read_quota_block(lqinode, 0, &bh);
722 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200723 mlog_errno(status);
724 mlog(ML_ERROR, "failed to read quota file info header "
725 "(type=%d)\n", type);
726 goto out_err;
727 }
728 ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
729 OCFS2_LOCAL_INFO_OFF);
Jan Kara96827ad2014-11-19 10:41:41 +0100730 oinfo->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
Jan Kara9e33d692008-08-25 19:56:50 +0200731 oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
732 oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200733 oinfo->dqi_libh = bh;
Jan Kara9e33d692008-08-25 19:56:50 +0200734
735 /* We crashed when using local quota file? */
Jan Kara96827ad2014-11-19 10:41:41 +0100736 if (!(oinfo->dqi_flags & OLQF_CLEAN)) {
Jan Kara22053632008-10-20 23:50:38 +0200737 rec = OCFS2_SB(sb)->quota_rec;
738 if (!rec) {
739 rec = ocfs2_alloc_quota_recovery();
740 if (!rec) {
741 status = -ENOMEM;
742 mlog_errno(status);
743 goto out_err;
744 }
745 OCFS2_SB(sb)->quota_rec = rec;
746 }
Jan Kara9e33d692008-08-25 19:56:50 +0200747
Jan Kara22053632008-10-20 23:50:38 +0200748 status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
749 &rec->r_list[type]);
750 if (status < 0) {
751 mlog_errno(status);
752 goto out_err;
753 }
754 }
755
756 status = ocfs2_load_local_quota_bitmaps(lqinode,
Jan Kara9e33d692008-08-25 19:56:50 +0200757 ldinfo,
758 &oinfo->dqi_chunk);
759 if (status < 0) {
760 mlog_errno(status);
761 goto out_err;
762 }
763
764 /* Now mark quota file as used */
Jan Kara96827ad2014-11-19 10:41:41 +0100765 oinfo->dqi_flags &= ~OLQF_CLEAN;
Jan Kara9e33d692008-08-25 19:56:50 +0200766 status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
767 if (status < 0) {
768 mlog_errno(status);
769 goto out_err;
770 }
771
772 return 0;
773out_err:
774 if (oinfo) {
775 iput(oinfo->dqi_gqinode);
776 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
777 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
778 brelse(oinfo->dqi_lqi_bh);
779 if (locked)
780 ocfs2_inode_unlock(lqinode, 1);
781 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
782 kfree(oinfo);
783 }
784 brelse(bh);
785 return -1;
786}
787
788/* Write local info to quota file */
789static int ocfs2_local_write_info(struct super_block *sb, int type)
790{
791 struct mem_dqinfo *info = sb_dqinfo(sb, type);
792 struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
Jan Karaae4f6ef2010-04-28 19:04:29 +0200793 ->dqi_libh;
Jan Kara9e33d692008-08-25 19:56:50 +0200794 int status;
795
796 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
797 info);
798 if (status < 0) {
799 mlog_errno(status);
800 return -1;
801 }
802
803 return 0;
804}
805
806/* Release info from memory */
807static int ocfs2_local_free_info(struct super_block *sb, int type)
808{
809 struct mem_dqinfo *info = sb_dqinfo(sb, type);
810 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
811 struct ocfs2_quota_chunk *chunk;
812 struct ocfs2_local_disk_chunk *dchunk;
813 int mark_clean = 1, len;
814 int status;
815
Jan Kara9e33d692008-08-25 19:56:50 +0200816 iput(oinfo->dqi_gqinode);
817 ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
818 ocfs2_lock_res_free(&oinfo->dqi_gqlock);
819 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
820 dchunk = (struct ocfs2_local_disk_chunk *)
821 (chunk->qc_headerbh->b_data);
822 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
823 len = ol_chunk_entries(sb);
824 } else {
825 len = (oinfo->dqi_blocks -
826 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
827 * ol_quota_entries_per_block(sb);
828 }
829 /* Not all entries free? Bug! */
830 if (le32_to_cpu(dchunk->dqc_free) != len) {
831 mlog(ML_ERROR, "releasing quota file with used "
832 "entries (type=%d)\n", type);
833 mark_clean = 0;
834 }
835 }
836 ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
837
Jan Kara5f530de2016-11-23 14:35:26 +0100838 /*
839 * s_umount held in exclusive mode protects us against racing with
840 * recovery thread...
841 */
Jan Kara22053632008-10-20 23:50:38 +0200842 if (oinfo->dqi_rec) {
843 ocfs2_free_quota_recovery(oinfo->dqi_rec);
844 mark_clean = 0;
845 }
846
Jan Kara9e33d692008-08-25 19:56:50 +0200847 if (!mark_clean)
848 goto out;
849
850 /* Mark local file as clean */
Jan Kara96827ad2014-11-19 10:41:41 +0100851 oinfo->dqi_flags |= OLQF_CLEAN;
Jan Kara9e33d692008-08-25 19:56:50 +0200852 status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
Jan Karaae4f6ef2010-04-28 19:04:29 +0200853 oinfo->dqi_libh,
Jan Kara9e33d692008-08-25 19:56:50 +0200854 olq_update_info,
855 info);
856 if (status < 0) {
857 mlog_errno(status);
858 goto out;
859 }
860
861out:
862 ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
Jan Karaae4f6ef2010-04-28 19:04:29 +0200863 brelse(oinfo->dqi_libh);
Jan Kara9e33d692008-08-25 19:56:50 +0200864 brelse(oinfo->dqi_lqi_bh);
865 kfree(oinfo);
866 return 0;
867}
868
869static void olq_set_dquot(struct buffer_head *bh, void *private)
870{
871 struct ocfs2_dquot *od = private;
872 struct ocfs2_local_disk_dqblk *dqblk;
873 struct super_block *sb = od->dq_dquot.dq_sb;
874
875 dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
876 + ol_dqblk_block_offset(sb, od->dq_local_off));
877
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700878 dqblk->dqb_id = cpu_to_le64(from_kqid(&init_user_ns,
879 od->dq_dquot.dq_id));
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200880 spin_lock(&od->dq_dquot.dq_dqb_lock);
Jan Kara9e33d692008-08-25 19:56:50 +0200881 dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
882 od->dq_origspace);
883 dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
884 od->dq_originodes);
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200885 spin_unlock(&od->dq_dquot.dq_dqb_lock);
Tao Ma38877a42011-02-23 22:12:48 +0800886 trace_olq_set_dquot(
887 (unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
888 (unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700889 from_kqid(&init_user_ns, od->dq_dquot.dq_id));
Jan Kara9e33d692008-08-25 19:56:50 +0200890}
891
892/* Write dquot to local quota file */
Jan Kara741e1282010-05-13 18:05:15 +0200893int ocfs2_local_write_dquot(struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +0200894{
895 struct super_block *sb = dquot->dq_sb;
896 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
Jan Karaf64dd442010-04-28 00:22:30 +0200897 struct buffer_head *bh;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700898 struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_id.type];
Jan Kara9e33d692008-08-25 19:56:50 +0200899 int status;
900
Jan Karaf64dd442010-04-28 00:22:30 +0200901 status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
902 &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +0100903 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +0200904 mlog_errno(status);
905 goto out;
906 }
Jan Karaf64dd442010-04-28 00:22:30 +0200907 status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
Jan Kara9e33d692008-08-25 19:56:50 +0200908 if (status < 0) {
909 mlog_errno(status);
910 goto out;
911 }
912out:
913 brelse(bh);
914 return status;
915}
916
917/* Find free entry in local quota file */
918static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
919 int type,
920 int *offset)
921{
922 struct mem_dqinfo *info = sb_dqinfo(sb, type);
923 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
924 struct ocfs2_quota_chunk *chunk;
925 struct ocfs2_local_disk_chunk *dchunk;
926 int found = 0, len;
927
928 list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
929 dchunk = (struct ocfs2_local_disk_chunk *)
930 chunk->qc_headerbh->b_data;
931 if (le32_to_cpu(dchunk->dqc_free) > 0) {
932 found = 1;
933 break;
934 }
935 }
936 if (!found)
937 return NULL;
938
939 if (chunk->qc_num < oinfo->dqi_chunks - 1) {
940 len = ol_chunk_entries(sb);
941 } else {
942 len = (oinfo->dqi_blocks -
943 ol_quota_chunk_block(sb, chunk->qc_num) - 1)
944 * ol_quota_entries_per_block(sb);
945 }
946
Akinobu Mita93925572011-11-15 14:56:34 -0800947 found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
Jan Kara9e33d692008-08-25 19:56:50 +0200948 /* We failed? */
949 if (found == len) {
950 mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
951 " entries free (type=%d)\n", chunk->qc_num,
952 le32_to_cpu(dchunk->dqc_free), type);
953 return ERR_PTR(-EIO);
954 }
955 *offset = found;
956 return chunk;
957}
958
959/* Add new chunk to the local quota file */
960static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
961 struct super_block *sb,
962 int type,
963 int *offset)
964{
965 struct mem_dqinfo *info = sb_dqinfo(sb, type);
966 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
967 struct inode *lqinode = sb_dqopt(sb)->files[type];
968 struct ocfs2_quota_chunk *chunk = NULL;
969 struct ocfs2_local_disk_chunk *dchunk;
970 int status;
971 handle_t *handle;
Jan Kara0e7f3872009-07-22 13:17:17 +0200972 struct buffer_head *bh = NULL, *dbh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +0200973 u64 p_blkno;
974
975 /* We are protected by dqio_sem so no locking needed */
Joel Becker56934862010-07-01 15:13:31 -0700976 status = ocfs2_extend_no_holes(lqinode, NULL,
Junxiao Bif17c20d2013-09-11 14:19:45 -0700977 i_size_read(lqinode) + 2 * sb->s_blocksize,
978 i_size_read(lqinode));
Jan Kara9e33d692008-08-25 19:56:50 +0200979 if (status < 0) {
980 mlog_errno(status);
981 goto out;
982 }
983 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
Junxiao Bif17c20d2013-09-11 14:19:45 -0700984 i_size_read(lqinode) + 2 * sb->s_blocksize);
Jan Kara9e33d692008-08-25 19:56:50 +0200985 if (status < 0) {
986 mlog_errno(status);
987 goto out;
988 }
989
990 chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
991 if (!chunk) {
992 status = -ENOMEM;
993 mlog_errno(status);
994 goto out;
995 }
Jan Kara05849742009-07-22 13:17:21 +0200996 /* Local quota info and two new blocks we initialize */
997 handle = ocfs2_start_trans(OCFS2_SB(sb),
998 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
999 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001000 if (IS_ERR(handle)) {
1001 status = PTR_ERR(handle);
1002 mlog_errno(status);
1003 goto out;
1004 }
1005
Jan Kara0e7f3872009-07-22 13:17:17 +02001006 /* Initialize chunk header */
Jan Kara0e7f3872009-07-22 13:17:17 +02001007 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1008 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001009 if (status < 0) {
1010 mlog_errno(status);
1011 goto out_trans;
1012 }
1013 bh = sb_getblk(sb, p_blkno);
1014 if (!bh) {
1015 status = -ENOMEM;
1016 mlog_errno(status);
1017 goto out_trans;
1018 }
1019 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Joel Becker8cb471e2009-02-10 20:00:41 -08001020 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001021 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001022 OCFS2_JOURNAL_ACCESS_CREATE);
Jan Kara9e33d692008-08-25 19:56:50 +02001023 if (status < 0) {
1024 mlog_errno(status);
1025 goto out_trans;
1026 }
1027 lock_buffer(bh);
Tao Madf32b332008-11-25 07:21:36 +08001028 dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
Jan Kara9e33d692008-08-25 19:56:50 +02001029 memset(dchunk->dqc_bitmap, 0,
1030 sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
1031 OCFS2_QBLK_RESERVED_SPACE);
Jan Kara9e33d692008-08-25 19:56:50 +02001032 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001033 ocfs2_journal_dirty(handle, bh);
Jan Kara9e33d692008-08-25 19:56:50 +02001034
Jan Kara0e7f3872009-07-22 13:17:17 +02001035 /* Initialize new block with structures */
Jan Kara0e7f3872009-07-22 13:17:17 +02001036 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
1037 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001038 if (status < 0) {
1039 mlog_errno(status);
1040 goto out_trans;
1041 }
1042 dbh = sb_getblk(sb, p_blkno);
1043 if (!dbh) {
1044 status = -ENOMEM;
1045 mlog_errno(status);
1046 goto out_trans;
1047 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001048 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
Joel Becker0cf2f762009-02-12 16:41:25 -08001049 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001050 OCFS2_JOURNAL_ACCESS_CREATE);
1051 if (status < 0) {
1052 mlog_errno(status);
1053 goto out_trans;
1054 }
1055 lock_buffer(dbh);
1056 memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
1057 unlock_buffer(dbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001058 ocfs2_journal_dirty(handle, dbh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001059
1060 /* Update local quotafile info */
Jan Kara9e33d692008-08-25 19:56:50 +02001061 oinfo->dqi_blocks += 2;
1062 oinfo->dqi_chunks++;
1063 status = ocfs2_local_write_info(sb, type);
1064 if (status < 0) {
1065 mlog_errno(status);
1066 goto out_trans;
1067 }
1068 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1069 if (status < 0) {
1070 mlog_errno(status);
1071 goto out;
1072 }
1073
1074 list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
1075 chunk->qc_num = list_entry(chunk->qc_chunk.prev,
1076 struct ocfs2_quota_chunk,
1077 qc_chunk)->qc_num + 1;
1078 chunk->qc_headerbh = bh;
1079 *offset = 0;
1080 return chunk;
1081out_trans:
1082 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1083out:
1084 brelse(bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001085 brelse(dbh);
Jan Kara9e33d692008-08-25 19:56:50 +02001086 kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
1087 return ERR_PTR(status);
1088}
1089
1090/* Find free entry in local quota file */
1091static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
1092 struct super_block *sb,
1093 int type,
1094 int *offset)
1095{
1096 struct mem_dqinfo *info = sb_dqinfo(sb, type);
1097 struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
1098 struct ocfs2_quota_chunk *chunk;
1099 struct inode *lqinode = sb_dqopt(sb)->files[type];
1100 struct ocfs2_local_disk_chunk *dchunk;
1101 int epb = ol_quota_entries_per_block(sb);
1102 unsigned int chunk_blocks;
Jan Kara0e7f3872009-07-22 13:17:17 +02001103 struct buffer_head *bh;
1104 u64 p_blkno;
Jan Kara9e33d692008-08-25 19:56:50 +02001105 int status;
1106 handle_t *handle;
1107
1108 if (list_empty(&oinfo->dqi_chunk))
1109 return ocfs2_local_quota_add_chunk(sb, type, offset);
1110 /* Is the last chunk full? */
1111 chunk = list_entry(oinfo->dqi_chunk.prev,
1112 struct ocfs2_quota_chunk, qc_chunk);
1113 chunk_blocks = oinfo->dqi_blocks -
1114 ol_quota_chunk_block(sb, chunk->qc_num) - 1;
1115 if (ol_chunk_blocks(sb) == chunk_blocks)
1116 return ocfs2_local_quota_add_chunk(sb, type, offset);
1117
1118 /* We are protected by dqio_sem so no locking needed */
Joel Becker56934862010-07-01 15:13:31 -07001119 status = ocfs2_extend_no_holes(lqinode, NULL,
Junxiao Bif17c20d2013-09-11 14:19:45 -07001120 i_size_read(lqinode) + sb->s_blocksize,
1121 i_size_read(lqinode));
Jan Kara9e33d692008-08-25 19:56:50 +02001122 if (status < 0) {
1123 mlog_errno(status);
1124 goto out;
1125 }
1126 status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
Junxiao Bif17c20d2013-09-11 14:19:45 -07001127 i_size_read(lqinode) + sb->s_blocksize);
Jan Kara9e33d692008-08-25 19:56:50 +02001128 if (status < 0) {
1129 mlog_errno(status);
1130 goto out;
1131 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001132
1133 /* Get buffer from the just added block */
Jan Kara0e7f3872009-07-22 13:17:17 +02001134 status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
1135 &p_blkno, NULL, NULL);
Jan Kara0e7f3872009-07-22 13:17:17 +02001136 if (status < 0) {
1137 mlog_errno(status);
1138 goto out;
1139 }
1140 bh = sb_getblk(sb, p_blkno);
1141 if (!bh) {
1142 status = -ENOMEM;
1143 mlog_errno(status);
1144 goto out;
1145 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001146 ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
Jan Kara0e7f3872009-07-22 13:17:17 +02001147
Jan Kara05849742009-07-22 13:17:21 +02001148 /* Local quota info, chunk header and the new block we initialize */
1149 handle = ocfs2_start_trans(OCFS2_SB(sb),
1150 OCFS2_LOCAL_QINFO_WRITE_CREDITS +
1151 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
Jan Kara9e33d692008-08-25 19:56:50 +02001152 if (IS_ERR(handle)) {
1153 status = PTR_ERR(handle);
1154 mlog_errno(status);
1155 goto out;
1156 }
Jan Kara0e7f3872009-07-22 13:17:17 +02001157 /* Zero created block */
Joel Becker0cf2f762009-02-12 16:41:25 -08001158 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
Jan Kara0e7f3872009-07-22 13:17:17 +02001159 OCFS2_JOURNAL_ACCESS_CREATE);
1160 if (status < 0) {
1161 mlog_errno(status);
1162 goto out_trans;
1163 }
1164 lock_buffer(bh);
1165 memset(bh->b_data, 0, sb->s_blocksize);
1166 unlock_buffer(bh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001167 ocfs2_journal_dirty(handle, bh);
1168
Jan Kara0e7f3872009-07-22 13:17:17 +02001169 /* Update chunk header */
Joel Becker0cf2f762009-02-12 16:41:25 -08001170 status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
1171 chunk->qc_headerbh,
Jan Kara9e33d692008-08-25 19:56:50 +02001172 OCFS2_JOURNAL_ACCESS_WRITE);
1173 if (status < 0) {
1174 mlog_errno(status);
1175 goto out_trans;
1176 }
1177
1178 dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
1179 lock_buffer(chunk->qc_headerbh);
1180 le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
1181 unlock_buffer(chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001182 ocfs2_journal_dirty(handle, chunk->qc_headerbh);
1183
Jan Kara0e7f3872009-07-22 13:17:17 +02001184 /* Update file header */
Jan Kara9e33d692008-08-25 19:56:50 +02001185 oinfo->dqi_blocks++;
1186 status = ocfs2_local_write_info(sb, type);
1187 if (status < 0) {
1188 mlog_errno(status);
1189 goto out_trans;
1190 }
1191
1192 status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
1193 if (status < 0) {
1194 mlog_errno(status);
1195 goto out;
1196 }
1197 *offset = chunk_blocks * epb;
1198 return chunk;
1199out_trans:
1200 ocfs2_commit_trans(OCFS2_SB(sb), handle);
1201out:
1202 return ERR_PTR(status);
1203}
1204
Tao Madf32b332008-11-25 07:21:36 +08001205static void olq_alloc_dquot(struct buffer_head *bh, void *private)
Jan Kara9e33d692008-08-25 19:56:50 +02001206{
1207 int *offset = private;
1208 struct ocfs2_local_disk_chunk *dchunk;
1209
1210 dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
Akinobu Mita93925572011-11-15 14:56:34 -08001211 ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
Jan Kara9e33d692008-08-25 19:56:50 +02001212 le32_add_cpu(&dchunk->dqc_free, -1);
1213}
1214
1215/* Create dquot in the local file for given id */
Jan Karafb8dd8d2010-03-31 16:25:37 +02001216int ocfs2_create_local_dquot(struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001217{
1218 struct super_block *sb = dquot->dq_sb;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001219 int type = dquot->dq_id.type;
Jan Kara9e33d692008-08-25 19:56:50 +02001220 struct inode *lqinode = sb_dqopt(sb)->files[type];
1221 struct ocfs2_quota_chunk *chunk;
1222 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1223 int offset;
1224 int status;
Jan Karaf64dd442010-04-28 00:22:30 +02001225 u64 pcount;
Jan Kara9e33d692008-08-25 19:56:50 +02001226
Jan Karaf64dd442010-04-28 00:22:30 +02001227 down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001228 chunk = ocfs2_find_free_entry(sb, type, &offset);
1229 if (!chunk) {
1230 chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
Jan Karaf64dd442010-04-28 00:22:30 +02001231 if (IS_ERR(chunk)) {
1232 status = PTR_ERR(chunk);
1233 goto out;
1234 }
Jan Kara9e33d692008-08-25 19:56:50 +02001235 } else if (IS_ERR(chunk)) {
Jan Karaf64dd442010-04-28 00:22:30 +02001236 status = PTR_ERR(chunk);
1237 goto out;
Jan Kara9e33d692008-08-25 19:56:50 +02001238 }
1239 od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
1240 od->dq_chunk = chunk;
Jan Karaf64dd442010-04-28 00:22:30 +02001241 status = ocfs2_extent_map_get_blocks(lqinode,
1242 ol_dqblk_block(sb, chunk->qc_num, offset),
1243 &od->dq_local_phys_blk,
1244 &pcount,
1245 NULL);
Jan Kara9e33d692008-08-25 19:56:50 +02001246
1247 /* Initialize dquot structure on disk */
1248 status = ocfs2_local_write_dquot(dquot);
1249 if (status < 0) {
1250 mlog_errno(status);
1251 goto out;
1252 }
1253
1254 /* Mark structure as allocated */
1255 status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
1256 &offset);
1257 if (status < 0) {
1258 mlog_errno(status);
1259 goto out;
1260 }
1261out:
Jan Karaf64dd442010-04-28 00:22:30 +02001262 up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
Jan Kara9e33d692008-08-25 19:56:50 +02001263 return status;
1264}
1265
Jan Karafb8dd8d2010-03-31 16:25:37 +02001266/*
1267 * Release dquot structure from local quota file. ocfs2_release_dquot() has
1268 * already started a transaction and written all changes to global quota file
1269 */
1270int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
Jan Kara9e33d692008-08-25 19:56:50 +02001271{
1272 int status;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07001273 int type = dquot->dq_id.type;
Jan Kara9e33d692008-08-25 19:56:50 +02001274 struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
1275 struct super_block *sb = dquot->dq_sb;
1276 struct ocfs2_local_disk_chunk *dchunk;
1277 int offset;
Jan Kara9e33d692008-08-25 19:56:50 +02001278
Joel Becker0cf2f762009-02-12 16:41:25 -08001279 status = ocfs2_journal_access_dq(handle,
1280 INODE_CACHE(sb_dqopt(sb)->files[type]),
Jan Kara9e33d692008-08-25 19:56:50 +02001281 od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
1282 if (status < 0) {
1283 mlog_errno(status);
1284 goto out;
1285 }
1286 offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
1287 od->dq_local_off);
1288 dchunk = (struct ocfs2_local_disk_chunk *)
1289 (od->dq_chunk->qc_headerbh->b_data);
1290 /* Mark structure as freed */
1291 lock_buffer(od->dq_chunk->qc_headerbh);
Akinobu Mita93925572011-11-15 14:56:34 -08001292 ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
Jan Kara9e33d692008-08-25 19:56:50 +02001293 le32_add_cpu(&dchunk->dqc_free, 1);
1294 unlock_buffer(od->dq_chunk->qc_headerbh);
Joel Beckerec20cec2010-03-19 14:13:52 -07001295 ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
1296
Jan Kara9e33d692008-08-25 19:56:50 +02001297out:
Jan Kara9e33d692008-08-25 19:56:50 +02001298 return status;
1299}
1300
Alexey Dobriyan1472da52009-10-16 15:26:03 +04001301static const struct quota_format_ops ocfs2_format_ops = {
Jan Kara9e33d692008-08-25 19:56:50 +02001302 .check_quota_file = ocfs2_local_check_quota_file,
1303 .read_file_info = ocfs2_local_read_info,
1304 .write_file_info = ocfs2_global_write_info,
1305 .free_file_info = ocfs2_local_free_info,
Jan Kara9e33d692008-08-25 19:56:50 +02001306};
1307
1308struct quota_format_type ocfs2_quota_format = {
1309 .qf_fmt_id = QFMT_OCFS2,
1310 .qf_ops = &ocfs2_format_ops,
1311 .qf_owner = THIS_MODULE
1312};