xref: /openbmc/linux/fs/ocfs2/quota_global.c (revision 22f01029)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Implementation of operations over global quota file
4  */
5 #include <linux/spinlock.h>
6 #include <linux/fs.h>
7 #include <linux/slab.h>
8 #include <linux/quota.h>
9 #include <linux/quotaops.h>
10 #include <linux/dqblk_qtree.h>
11 #include <linux/jiffies.h>
12 #include <linux/writeback.h>
13 #include <linux/workqueue.h>
14 #include <linux/llist.h>
15 #include <linux/iversion.h>
16 
17 #include <cluster/masklog.h>
18 
19 #include "ocfs2_fs.h"
20 #include "ocfs2.h"
21 #include "alloc.h"
22 #include "blockcheck.h"
23 #include "inode.h"
24 #include "journal.h"
25 #include "file.h"
26 #include "sysfile.h"
27 #include "dlmglue.h"
28 #include "uptodate.h"
29 #include "super.h"
30 #include "buffer_head_io.h"
31 #include "quota.h"
32 #include "ocfs2_trace.h"
33 
34 /*
35  * Locking of quotas with OCFS2 is rather complex. Here are rules that
36  * should be obeyed by all the functions:
37  * - any write of quota structure (either to local or global file) is protected
38  *   by dqio_sem or dquot->dq_lock.
39  * - any modification of global quota file holds inode cluster lock, i_mutex,
40  *   and ip_alloc_sem of the global quota file (achieved by
41  *   ocfs2_lock_global_qf). It also has to hold qinfo_lock.
42  * - an allocation of new blocks for local quota file is protected by
43  *   its ip_alloc_sem
44  *
45  * A rough sketch of locking dependencies (lf = local file, gf = global file):
46  * Normal filesystem operation:
47  *   start_trans -> dqio_sem -> write to lf
48  * Syncing of local and global file:
49  *   ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
50  *     write to gf
51  *						       -> write to lf
52  * Acquire dquot for the first time:
53  *   dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf
54  *				     -> alloc space for gf
55  *				     -> start_trans -> qinfo_lock -> write to gf
56  *	     -> ip_alloc_sem of lf -> alloc space for lf
57  *	     -> write to lf
58  * Release last reference to dquot:
59  *   dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf
60  *	     -> write to lf
61  * Note that all the above operations also hold the inode cluster lock of lf.
62  * Recovery:
63  *   inode cluster lock of recovered lf
64  *     -> read bitmaps -> ip_alloc_sem of lf
65  *     -> ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
66  *        write to gf
67  */
68 
69 static void qsync_work_fn(struct work_struct *work);
70 
71 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
72 {
73 	struct ocfs2_global_disk_dqblk *d = dp;
74 	struct mem_dqblk *m = &dquot->dq_dqb;
75 
76 	/* Update from disk only entries not set by the admin */
77 	if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
78 		m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
79 		m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
80 	}
81 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
82 		m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
83 	if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
84 		m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
85 		m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
86 	}
87 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
88 		m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
89 	if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
90 		m->dqb_btime = le64_to_cpu(d->dqb_btime);
91 	if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
92 		m->dqb_itime = le64_to_cpu(d->dqb_itime);
93 	OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
94 }
95 
96 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
97 {
98 	struct ocfs2_global_disk_dqblk *d = dp;
99 	struct mem_dqblk *m = &dquot->dq_dqb;
100 
101 	d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
102 	d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
103 	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
104 	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
105 	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
106 	d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
107 	d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
108 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
109 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
110 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
111 	d->dqb_pad1 = d->dqb_pad2 = 0;
112 }
113 
114 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
115 {
116 	struct ocfs2_global_disk_dqblk *d = dp;
117 	struct ocfs2_mem_dqinfo *oinfo =
118 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
119 
120 	if (qtree_entry_unused(&oinfo->dqi_gi, dp))
121 		return 0;
122 
123 	return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
124 				le32_to_cpu(d->dqb_id)),
125 		      dquot->dq_id);
126 }
127 
128 const struct qtree_fmt_operations ocfs2_global_ops = {
129 	.mem2disk_dqblk = ocfs2_global_mem2diskdqb,
130 	.disk2mem_dqblk = ocfs2_global_disk2memdqb,
131 	.is_id = ocfs2_global_is_id,
132 };
133 
134 int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh)
135 {
136 	struct ocfs2_disk_dqtrailer *dqt =
137 		ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
138 
139 	trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr);
140 
141 	BUG_ON(!buffer_uptodate(bh));
142 
143 	/*
144 	 * If the ecc fails, we return the error but otherwise
145 	 * leave the filesystem running.  We know any error is
146 	 * local to this block.
147 	 */
148 	return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
149 }
150 
151 int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
152 				struct buffer_head **bhp)
153 {
154 	int rc;
155 
156 	*bhp = NULL;
157 	rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
158 			       ocfs2_validate_quota_block);
159 	if (rc)
160 		mlog_errno(rc);
161 	return rc;
162 }
163 
164 /* Read data from global quotafile - avoid pagecache and such because we cannot
165  * afford acquiring the locks... We use quota cluster lock to serialize
166  * operations. Caller is responsible for acquiring it. */
167 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
168 			 size_t len, loff_t off)
169 {
170 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
171 	struct inode *gqinode = oinfo->dqi_gqinode;
172 	loff_t i_size = i_size_read(gqinode);
173 	int offset = off & (sb->s_blocksize - 1);
174 	sector_t blk = off >> sb->s_blocksize_bits;
175 	int err = 0;
176 	struct buffer_head *bh;
177 	size_t toread, tocopy;
178 	u64 pblock = 0, pcount = 0;
179 
180 	if (off > i_size)
181 		return 0;
182 	if (off + len > i_size)
183 		len = i_size - off;
184 	toread = len;
185 	while (toread > 0) {
186 		tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
187 		if (!pcount) {
188 			err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock,
189 							  &pcount, NULL);
190 			if (err) {
191 				mlog_errno(err);
192 				return err;
193 			}
194 		} else {
195 			pcount--;
196 			pblock++;
197 		}
198 		bh = NULL;
199 		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
200 		if (err) {
201 			mlog_errno(err);
202 			return err;
203 		}
204 		memcpy(data, bh->b_data + offset, tocopy);
205 		brelse(bh);
206 		offset = 0;
207 		toread -= tocopy;
208 		data += tocopy;
209 		blk++;
210 	}
211 	return len;
212 }
213 
214 /* Write to quotafile (we know the transaction is already started and has
215  * enough credits) */
216 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
217 			  const char *data, size_t len, loff_t off)
218 {
219 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
220 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
221 	struct inode *gqinode = oinfo->dqi_gqinode;
222 	int offset = off & (sb->s_blocksize - 1);
223 	sector_t blk = off >> sb->s_blocksize_bits;
224 	int err = 0, new = 0, ja_type;
225 	struct buffer_head *bh = NULL;
226 	handle_t *handle = journal_current_handle();
227 	u64 pblock, pcount;
228 
229 	if (!handle) {
230 		mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
231 		     "because transaction was not started.\n",
232 		     (unsigned long long)off, (unsigned long long)len);
233 		return -EIO;
234 	}
235 	if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
236 		WARN_ON(1);
237 		len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
238 	}
239 
240 	if (i_size_read(gqinode) < off + len) {
241 		loff_t rounded_end =
242 				ocfs2_align_bytes_to_blocks(sb, off + len);
243 
244 		/* Space is already allocated in ocfs2_acquire_dquot() */
245 		err = ocfs2_simple_size_update(gqinode,
246 					       oinfo->dqi_gqi_bh,
247 					       rounded_end);
248 		if (err < 0)
249 			goto out;
250 		new = 1;
251 	}
252 	err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL);
253 	if (err) {
254 		mlog_errno(err);
255 		goto out;
256 	}
257 	/* Not rewriting whole block? */
258 	if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
259 	    !new) {
260 		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
261 		ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
262 	} else {
263 		bh = sb_getblk(sb, pblock);
264 		if (!bh)
265 			err = -ENOMEM;
266 		ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
267 	}
268 	if (err) {
269 		mlog_errno(err);
270 		goto out;
271 	}
272 	lock_buffer(bh);
273 	if (new)
274 		memset(bh->b_data, 0, sb->s_blocksize);
275 	memcpy(bh->b_data + offset, data, len);
276 	flush_dcache_page(bh->b_page);
277 	set_buffer_uptodate(bh);
278 	unlock_buffer(bh);
279 	ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
280 	err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
281 				      ja_type);
282 	if (err < 0) {
283 		brelse(bh);
284 		goto out;
285 	}
286 	ocfs2_journal_dirty(handle, bh);
287 	brelse(bh);
288 out:
289 	if (err) {
290 		mlog_errno(err);
291 		return err;
292 	}
293 	inode_inc_iversion(gqinode);
294 	ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
295 	return len;
296 }
297 
298 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
299 {
300 	int status;
301 	struct buffer_head *bh = NULL;
302 
303 	status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
304 	if (status < 0)
305 		return status;
306 	spin_lock(&dq_data_lock);
307 	if (!oinfo->dqi_gqi_count++)
308 		oinfo->dqi_gqi_bh = bh;
309 	else
310 		WARN_ON(bh != oinfo->dqi_gqi_bh);
311 	spin_unlock(&dq_data_lock);
312 	if (ex) {
313 		inode_lock(oinfo->dqi_gqinode);
314 		down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
315 	} else {
316 		down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
317 	}
318 	return 0;
319 }
320 
321 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
322 {
323 	if (ex) {
324 		up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
325 		inode_unlock(oinfo->dqi_gqinode);
326 	} else {
327 		up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
328 	}
329 	ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
330 	brelse(oinfo->dqi_gqi_bh);
331 	spin_lock(&dq_data_lock);
332 	if (!--oinfo->dqi_gqi_count)
333 		oinfo->dqi_gqi_bh = NULL;
334 	spin_unlock(&dq_data_lock);
335 }
336 
337 /* Read information header from global quota file */
338 int ocfs2_global_read_info(struct super_block *sb, int type)
339 {
340 	struct inode *gqinode = NULL;
341 	unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
342 					      GROUP_QUOTA_SYSTEM_INODE };
343 	struct ocfs2_global_disk_dqinfo dinfo;
344 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
345 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
346 	u64 pcount;
347 	int status;
348 
349 	/* Read global header */
350 	gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
351 			OCFS2_INVALID_SLOT);
352 	if (!gqinode) {
353 		mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
354 			type);
355 		status = -EINVAL;
356 		goto out_err;
357 	}
358 	oinfo->dqi_gi.dqi_sb = sb;
359 	oinfo->dqi_gi.dqi_type = type;
360 	oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
361 	oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
362 	oinfo->dqi_gqi_bh = NULL;
363 	oinfo->dqi_gqi_count = 0;
364 	oinfo->dqi_gqinode = gqinode;
365 	status = ocfs2_lock_global_qf(oinfo, 0);
366 	if (status < 0) {
367 		mlog_errno(status);
368 		goto out_err;
369 	}
370 
371 	status = ocfs2_extent_map_get_blocks(gqinode, 0, &oinfo->dqi_giblk,
372 					     &pcount, NULL);
373 	if (status < 0)
374 		goto out_unlock;
375 
376 	status = ocfs2_qinfo_lock(oinfo, 0);
377 	if (status < 0)
378 		goto out_unlock;
379 	status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
380 				      sizeof(struct ocfs2_global_disk_dqinfo),
381 				      OCFS2_GLOBAL_INFO_OFF);
382 	ocfs2_qinfo_unlock(oinfo, 0);
383 	ocfs2_unlock_global_qf(oinfo, 0);
384 	if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
385 		mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
386 		     status);
387 		if (status >= 0)
388 			status = -EIO;
389 		mlog_errno(status);
390 		goto out_err;
391 	}
392 	info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
393 	info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
394 	oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
395 	oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
396 	oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
397 	oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
398 	oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
399 	oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
400 						OCFS2_QBLK_RESERVED_SPACE;
401 	oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
402 	INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
403 	schedule_delayed_work(&oinfo->dqi_sync_work,
404 			      msecs_to_jiffies(oinfo->dqi_syncms));
405 
406 out_err:
407 	return status;
408 out_unlock:
409 	ocfs2_unlock_global_qf(oinfo, 0);
410 	mlog_errno(status);
411 	goto out_err;
412 }
413 
414 /* Write information to global quota file. Expects exlusive lock on quota
415  * file inode and quota info */
416 static int __ocfs2_global_write_info(struct super_block *sb, int type)
417 {
418 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
419 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
420 	struct ocfs2_global_disk_dqinfo dinfo;
421 	ssize_t size;
422 
423 	spin_lock(&dq_data_lock);
424 	info->dqi_flags &= ~DQF_INFO_DIRTY;
425 	dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
426 	dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
427 	spin_unlock(&dq_data_lock);
428 	dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
429 	dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
430 	dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
431 	dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
432 	size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
433 				     sizeof(struct ocfs2_global_disk_dqinfo),
434 				     OCFS2_GLOBAL_INFO_OFF);
435 	if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
436 		mlog(ML_ERROR, "Cannot write global quota info structure\n");
437 		if (size >= 0)
438 			size = -EIO;
439 		return size;
440 	}
441 	return 0;
442 }
443 
444 int ocfs2_global_write_info(struct super_block *sb, int type)
445 {
446 	int err;
447 	struct quota_info *dqopt = sb_dqopt(sb);
448 	struct ocfs2_mem_dqinfo *info = dqopt->info[type].dqi_priv;
449 
450 	down_write(&dqopt->dqio_sem);
451 	err = ocfs2_qinfo_lock(info, 1);
452 	if (err < 0)
453 		goto out_sem;
454 	err = __ocfs2_global_write_info(sb, type);
455 	ocfs2_qinfo_unlock(info, 1);
456 out_sem:
457 	up_write(&dqopt->dqio_sem);
458 	return err;
459 }
460 
461 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
462 {
463 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
464 
465 	/*
466 	 * We may need to allocate tree blocks and a leaf block but not the
467 	 * root block
468 	 */
469 	return oinfo->dqi_gi.dqi_qtree_depth;
470 }
471 
472 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
473 {
474 	/* We modify all the allocated blocks, tree root, info block and
475 	 * the inode */
476 	return (ocfs2_global_qinit_alloc(sb, type) + 2) *
477 			OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1;
478 }
479 
480 /* Sync local information about quota modifications with global quota file.
481  * Caller must have started the transaction and obtained exclusive lock for
482  * global quota file inode */
483 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
484 {
485 	int err, err2;
486 	struct super_block *sb = dquot->dq_sb;
487 	int type = dquot->dq_id.type;
488 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
489 	struct ocfs2_global_disk_dqblk dqblk;
490 	s64 spacechange, inodechange;
491 	time64_t olditime, oldbtime;
492 
493 	err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
494 				   sizeof(struct ocfs2_global_disk_dqblk),
495 				   dquot->dq_off);
496 	if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
497 		if (err >= 0) {
498 			mlog(ML_ERROR, "Short read from global quota file "
499 				       "(%u read)\n", err);
500 			err = -EIO;
501 		}
502 		goto out;
503 	}
504 
505 	/* Update space and inode usage. Get also other information from
506 	 * global quota file so that we don't overwrite any changes there.
507 	 * We are */
508 	spin_lock(&dquot->dq_dqb_lock);
509 	spacechange = dquot->dq_dqb.dqb_curspace -
510 					OCFS2_DQUOT(dquot)->dq_origspace;
511 	inodechange = dquot->dq_dqb.dqb_curinodes -
512 					OCFS2_DQUOT(dquot)->dq_originodes;
513 	olditime = dquot->dq_dqb.dqb_itime;
514 	oldbtime = dquot->dq_dqb.dqb_btime;
515 	ocfs2_global_disk2memdqb(dquot, &dqblk);
516 	trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id),
517 			       dquot->dq_dqb.dqb_curspace,
518 			       (long long)spacechange,
519 			       dquot->dq_dqb.dqb_curinodes,
520 			       (long long)inodechange);
521 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
522 		dquot->dq_dqb.dqb_curspace += spacechange;
523 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
524 		dquot->dq_dqb.dqb_curinodes += inodechange;
525 	/* Set properly space grace time... */
526 	if (dquot->dq_dqb.dqb_bsoftlimit &&
527 	    dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
528 		if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
529 		    oldbtime > 0) {
530 			if (dquot->dq_dqb.dqb_btime > 0)
531 				dquot->dq_dqb.dqb_btime =
532 					min(dquot->dq_dqb.dqb_btime, oldbtime);
533 			else
534 				dquot->dq_dqb.dqb_btime = oldbtime;
535 		}
536 	} else {
537 		dquot->dq_dqb.dqb_btime = 0;
538 		clear_bit(DQ_BLKS_B, &dquot->dq_flags);
539 	}
540 	/* Set properly inode grace time... */
541 	if (dquot->dq_dqb.dqb_isoftlimit &&
542 	    dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
543 		if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
544 		    olditime > 0) {
545 			if (dquot->dq_dqb.dqb_itime > 0)
546 				dquot->dq_dqb.dqb_itime =
547 					min(dquot->dq_dqb.dqb_itime, olditime);
548 			else
549 				dquot->dq_dqb.dqb_itime = olditime;
550 		}
551 	} else {
552 		dquot->dq_dqb.dqb_itime = 0;
553 		clear_bit(DQ_INODES_B, &dquot->dq_flags);
554 	}
555 	/* All information is properly updated, clear the flags */
556 	__clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
557 	__clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
558 	__clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
559 	__clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
560 	__clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
561 	__clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
562 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
563 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
564 	spin_unlock(&dquot->dq_dqb_lock);
565 	err = ocfs2_qinfo_lock(info, freeing);
566 	if (err < 0) {
567 		mlog(ML_ERROR, "Failed to lock quota info, losing quota write"
568 			       " (type=%d, id=%u)\n", dquot->dq_id.type,
569 			       (unsigned)from_kqid(&init_user_ns, dquot->dq_id));
570 		goto out;
571 	}
572 	if (freeing)
573 		OCFS2_DQUOT(dquot)->dq_use_count--;
574 	err = qtree_write_dquot(&info->dqi_gi, dquot);
575 	if (err < 0)
576 		goto out_qlock;
577 	if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
578 		err = qtree_release_dquot(&info->dqi_gi, dquot);
579 		if (info_dirty(sb_dqinfo(sb, type))) {
580 			err2 = __ocfs2_global_write_info(sb, type);
581 			if (!err)
582 				err = err2;
583 		}
584 	}
585 out_qlock:
586 	ocfs2_qinfo_unlock(info, freeing);
587 out:
588 	if (err < 0)
589 		mlog_errno(err);
590 	return err;
591 }
592 
593 /*
594  *  Functions for periodic syncing of dquots with global file
595  */
596 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
597 {
598 	handle_t *handle;
599 	struct super_block *sb = dquot->dq_sb;
600 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
601 	struct ocfs2_super *osb = OCFS2_SB(sb);
602 	int status = 0;
603 
604 	trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id),
605 				      dquot->dq_id.type,
606 				      type, sb->s_id);
607 	if (type != dquot->dq_id.type)
608 		goto out;
609 	status = ocfs2_lock_global_qf(oinfo, 1);
610 	if (status < 0)
611 		goto out;
612 
613 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
614 	if (IS_ERR(handle)) {
615 		status = PTR_ERR(handle);
616 		mlog_errno(status);
617 		goto out_ilock;
618 	}
619 	down_write(&sb_dqopt(sb)->dqio_sem);
620 	status = ocfs2_sync_dquot(dquot);
621 	if (status < 0)
622 		mlog_errno(status);
623 	/* We have to write local structure as well... */
624 	status = ocfs2_local_write_dquot(dquot);
625 	if (status < 0)
626 		mlog_errno(status);
627 	up_write(&sb_dqopt(sb)->dqio_sem);
628 	ocfs2_commit_trans(osb, handle);
629 out_ilock:
630 	ocfs2_unlock_global_qf(oinfo, 1);
631 out:
632 	return status;
633 }
634 
635 static void qsync_work_fn(struct work_struct *work)
636 {
637 	struct ocfs2_mem_dqinfo *oinfo = container_of(work,
638 						      struct ocfs2_mem_dqinfo,
639 						      dqi_sync_work.work);
640 	struct super_block *sb = oinfo->dqi_gqinode->i_sb;
641 
642 	/*
643 	 * We have to be careful here not to deadlock on s_umount as umount
644 	 * disabling quotas may be in progress and it waits for this work to
645 	 * complete. If trylock fails, we'll do the sync next time...
646 	 */
647 	if (down_read_trylock(&sb->s_umount)) {
648 		dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
649 		up_read(&sb->s_umount);
650 	}
651 	schedule_delayed_work(&oinfo->dqi_sync_work,
652 			      msecs_to_jiffies(oinfo->dqi_syncms));
653 }
654 
655 /*
656  *  Wrappers for generic quota functions
657  */
658 
659 static int ocfs2_write_dquot(struct dquot *dquot)
660 {
661 	handle_t *handle;
662 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
663 	int status = 0;
664 
665 	trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id),
666 				dquot->dq_id.type);
667 
668 	handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
669 	if (IS_ERR(handle)) {
670 		status = PTR_ERR(handle);
671 		mlog_errno(status);
672 		goto out;
673 	}
674 	down_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
675 	status = ocfs2_local_write_dquot(dquot);
676 	up_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
677 	ocfs2_commit_trans(osb, handle);
678 out:
679 	return status;
680 }
681 
682 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
683 {
684 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
685 	/*
686 	 * We modify tree, leaf block, global info, local chunk header,
687 	 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
688 	 * accounts for inode update
689 	 */
690 	return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
691 	       OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
692 	       OCFS2_QINFO_WRITE_CREDITS +
693 	       OCFS2_INODE_UPDATE_CREDITS;
694 }
695 
696 void ocfs2_drop_dquot_refs(struct work_struct *work)
697 {
698 	struct ocfs2_super *osb = container_of(work, struct ocfs2_super,
699 					       dquot_drop_work);
700 	struct llist_node *list;
701 	struct ocfs2_dquot *odquot, *next_odquot;
702 
703 	list = llist_del_all(&osb->dquot_drop_list);
704 	llist_for_each_entry_safe(odquot, next_odquot, list, list) {
705 		/* Drop the reference we acquired in ocfs2_dquot_release() */
706 		dqput(&odquot->dq_dquot);
707 	}
708 }
709 
710 /*
711  * Called when the last reference to dquot is dropped. If we are called from
712  * downconvert thread, we cannot do all the handling here because grabbing
713  * quota lock could deadlock (the node holding the quota lock could need some
714  * other cluster lock to proceed but with blocked downconvert thread we cannot
715  * release any lock).
716  */
717 static int ocfs2_release_dquot(struct dquot *dquot)
718 {
719 	handle_t *handle;
720 	struct ocfs2_mem_dqinfo *oinfo =
721 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
722 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
723 	int status = 0;
724 
725 	trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id),
726 				  dquot->dq_id.type);
727 
728 	mutex_lock(&dquot->dq_lock);
729 	/* Check whether we are not racing with some other dqget() */
730 	if (dquot_is_busy(dquot))
731 		goto out;
732 	/* Running from downconvert thread? Postpone quota processing to wq */
733 	if (current == osb->dc_task) {
734 		/*
735 		 * Grab our own reference to dquot and queue it for delayed
736 		 * dropping.  Quota code rechecks after calling
737 		 * ->release_dquot() and won't free dquot structure.
738 		 */
739 		dqgrab(dquot);
740 		/* First entry on list -> queue work */
741 		if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list))
742 			queue_work(osb->ocfs2_wq, &osb->dquot_drop_work);
743 		goto out;
744 	}
745 	status = ocfs2_lock_global_qf(oinfo, 1);
746 	if (status < 0)
747 		goto out;
748 	handle = ocfs2_start_trans(osb,
749 		ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type));
750 	if (IS_ERR(handle)) {
751 		status = PTR_ERR(handle);
752 		mlog_errno(status);
753 		goto out_ilock;
754 	}
755 
756 	status = ocfs2_global_release_dquot(dquot);
757 	if (status < 0) {
758 		mlog_errno(status);
759 		goto out_trans;
760 	}
761 	status = ocfs2_local_release_dquot(handle, dquot);
762 	/*
763 	 * If we fail here, we cannot do much as global structure is
764 	 * already released. So just complain...
765 	 */
766 	if (status < 0)
767 		mlog_errno(status);
768 	/*
769 	 * Clear dq_off so that we search for the structure in quota file next
770 	 * time we acquire it. The structure might be deleted and reallocated
771 	 * elsewhere by another node while our dquot structure is on freelist.
772 	 */
773 	dquot->dq_off = 0;
774 	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
775 out_trans:
776 	ocfs2_commit_trans(osb, handle);
777 out_ilock:
778 	ocfs2_unlock_global_qf(oinfo, 1);
779 out:
780 	mutex_unlock(&dquot->dq_lock);
781 	if (status)
782 		mlog_errno(status);
783 	return status;
784 }
785 
786 /*
787  * Read global dquot structure from disk or create it if it does
788  * not exist. Also update use count of the global structure and
789  * create structure in node-local quota file.
790  */
791 static int ocfs2_acquire_dquot(struct dquot *dquot)
792 {
793 	int status = 0, err;
794 	int ex = 0;
795 	struct super_block *sb = dquot->dq_sb;
796 	struct ocfs2_super *osb = OCFS2_SB(sb);
797 	int type = dquot->dq_id.type;
798 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
799 	struct inode *gqinode = info->dqi_gqinode;
800 	int need_alloc = ocfs2_global_qinit_alloc(sb, type);
801 	handle_t *handle;
802 
803 	trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id),
804 				  type);
805 	mutex_lock(&dquot->dq_lock);
806 	/*
807 	 * We need an exclusive lock, because we're going to update use count
808 	 * and instantiate possibly new dquot structure
809 	 */
810 	status = ocfs2_lock_global_qf(info, 1);
811 	if (status < 0)
812 		goto out;
813 	status = ocfs2_qinfo_lock(info, 0);
814 	if (status < 0)
815 		goto out_dq;
816 	/*
817 	 * We always want to read dquot structure from disk because we don't
818 	 * know what happened with it while it was on freelist.
819 	 */
820 	status = qtree_read_dquot(&info->dqi_gi, dquot);
821 	ocfs2_qinfo_unlock(info, 0);
822 	if (status < 0)
823 		goto out_dq;
824 
825 	OCFS2_DQUOT(dquot)->dq_use_count++;
826 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
827 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
828 	if (!dquot->dq_off) {	/* No real quota entry? */
829 		ex = 1;
830 		/*
831 		 * Add blocks to quota file before we start a transaction since
832 		 * locking allocators ranks above a transaction start
833 		 */
834 		WARN_ON(journal_current_handle());
835 		status = ocfs2_extend_no_holes(gqinode, NULL,
836 			i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits),
837 			i_size_read(gqinode));
838 		if (status < 0)
839 			goto out_dq;
840 	}
841 
842 	handle = ocfs2_start_trans(osb,
843 				   ocfs2_calc_global_qinit_credits(sb, type));
844 	if (IS_ERR(handle)) {
845 		status = PTR_ERR(handle);
846 		goto out_dq;
847 	}
848 	status = ocfs2_qinfo_lock(info, ex);
849 	if (status < 0)
850 		goto out_trans;
851 	status = qtree_write_dquot(&info->dqi_gi, dquot);
852 	if (ex && info_dirty(sb_dqinfo(sb, type))) {
853 		err = __ocfs2_global_write_info(sb, type);
854 		if (!status)
855 			status = err;
856 	}
857 	ocfs2_qinfo_unlock(info, ex);
858 out_trans:
859 	ocfs2_commit_trans(osb, handle);
860 out_dq:
861 	ocfs2_unlock_global_qf(info, 1);
862 	if (status < 0)
863 		goto out;
864 
865 	status = ocfs2_create_local_dquot(dquot);
866 	if (status < 0)
867 		goto out;
868 	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
869 out:
870 	mutex_unlock(&dquot->dq_lock);
871 	if (status)
872 		mlog_errno(status);
873 	return status;
874 }
875 
876 static int ocfs2_get_next_id(struct super_block *sb, struct kqid *qid)
877 {
878 	int type = qid->type;
879 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
880 	int status = 0;
881 
882 	trace_ocfs2_get_next_id(from_kqid(&init_user_ns, *qid), type);
883 	if (!sb_has_quota_loaded(sb, type)) {
884 		status = -ESRCH;
885 		goto out;
886 	}
887 	status = ocfs2_lock_global_qf(info, 0);
888 	if (status < 0)
889 		goto out;
890 	status = ocfs2_qinfo_lock(info, 0);
891 	if (status < 0)
892 		goto out_global;
893 	status = qtree_get_next_id(&info->dqi_gi, qid);
894 	ocfs2_qinfo_unlock(info, 0);
895 out_global:
896 	ocfs2_unlock_global_qf(info, 0);
897 out:
898 	/*
899 	 * Avoid logging ENOENT since it just means there isn't next ID and
900 	 * ESRCH which means quota isn't enabled for the filesystem.
901 	 */
902 	if (status && status != -ENOENT && status != -ESRCH)
903 		mlog_errno(status);
904 	return status;
905 }
906 
907 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
908 {
909 	unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
910 			     (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
911 			     (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
912 			     (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
913 			     (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
914 			     (1 << (DQ_LASTSET_B + QIF_ITIME_B));
915 	int sync = 0;
916 	int status;
917 	struct super_block *sb = dquot->dq_sb;
918 	int type = dquot->dq_id.type;
919 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
920 	handle_t *handle;
921 	struct ocfs2_super *osb = OCFS2_SB(sb);
922 
923 	trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id),
924 				     type);
925 
926 	/* In case user set some limits, sync dquot immediately to global
927 	 * quota file so that information propagates quicker */
928 	spin_lock(&dquot->dq_dqb_lock);
929 	if (dquot->dq_flags & mask)
930 		sync = 1;
931 	spin_unlock(&dquot->dq_dqb_lock);
932 	/* This is a slight hack but we can't afford getting global quota
933 	 * lock if we already have a transaction started. */
934 	if (!sync || journal_current_handle()) {
935 		status = ocfs2_write_dquot(dquot);
936 		goto out;
937 	}
938 	status = ocfs2_lock_global_qf(oinfo, 1);
939 	if (status < 0)
940 		goto out;
941 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
942 	if (IS_ERR(handle)) {
943 		status = PTR_ERR(handle);
944 		mlog_errno(status);
945 		goto out_ilock;
946 	}
947 	down_write(&sb_dqopt(sb)->dqio_sem);
948 	status = ocfs2_sync_dquot(dquot);
949 	if (status < 0) {
950 		mlog_errno(status);
951 		goto out_dlock;
952 	}
953 	/* Now write updated local dquot structure */
954 	status = ocfs2_local_write_dquot(dquot);
955 out_dlock:
956 	up_write(&sb_dqopt(sb)->dqio_sem);
957 	ocfs2_commit_trans(osb, handle);
958 out_ilock:
959 	ocfs2_unlock_global_qf(oinfo, 1);
960 out:
961 	if (status)
962 		mlog_errno(status);
963 	return status;
964 }
965 
966 /* This should happen only after set_dqinfo(). */
967 static int ocfs2_write_info(struct super_block *sb, int type)
968 {
969 	handle_t *handle;
970 	int status = 0;
971 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
972 
973 	status = ocfs2_lock_global_qf(oinfo, 1);
974 	if (status < 0)
975 		goto out;
976 	handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
977 	if (IS_ERR(handle)) {
978 		status = PTR_ERR(handle);
979 		mlog_errno(status);
980 		goto out_ilock;
981 	}
982 	status = dquot_commit_info(sb, type);
983 	ocfs2_commit_trans(OCFS2_SB(sb), handle);
984 out_ilock:
985 	ocfs2_unlock_global_qf(oinfo, 1);
986 out:
987 	if (status)
988 		mlog_errno(status);
989 	return status;
990 }
991 
992 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
993 {
994 	struct ocfs2_dquot *dquot =
995 				kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
996 
997 	if (!dquot)
998 		return NULL;
999 	return &dquot->dq_dquot;
1000 }
1001 
1002 static void ocfs2_destroy_dquot(struct dquot *dquot)
1003 {
1004 	kmem_cache_free(ocfs2_dquot_cachep, dquot);
1005 }
1006 
1007 const struct dquot_operations ocfs2_quota_operations = {
1008 	/* We never make dquot dirty so .write_dquot is never called */
1009 	.acquire_dquot	= ocfs2_acquire_dquot,
1010 	.release_dquot	= ocfs2_release_dquot,
1011 	.mark_dirty	= ocfs2_mark_dquot_dirty,
1012 	.write_info	= ocfs2_write_info,
1013 	.alloc_dquot	= ocfs2_alloc_dquot,
1014 	.destroy_dquot	= ocfs2_destroy_dquot,
1015 	.get_next_id	= ocfs2_get_next_id,
1016 };
1017