xref: /openbmc/linux/fs/ocfs2/quota_global.c (revision 233eb8d6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29e33d69fSJan Kara /*
39e33d69fSJan Kara  *  Implementation of operations over global quota file
49e33d69fSJan Kara  */
5171bf93cSMark Fasheh #include <linux/spinlock.h>
69e33d69fSJan Kara #include <linux/fs.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
89e33d69fSJan Kara #include <linux/quota.h>
99e33d69fSJan Kara #include <linux/quotaops.h>
109e33d69fSJan Kara #include <linux/dqblk_qtree.h>
11171bf93cSMark Fasheh #include <linux/jiffies.h>
12171bf93cSMark Fasheh #include <linux/writeback.h>
13171bf93cSMark Fasheh #include <linux/workqueue.h>
14e3a767b6SJan Kara #include <linux/llist.h>
15cc56c33eSJeff Layton #include <linux/iversion.h>
169e33d69fSJan Kara 
179e33d69fSJan Kara #include <cluster/masklog.h>
189e33d69fSJan Kara 
199e33d69fSJan Kara #include "ocfs2_fs.h"
209e33d69fSJan Kara #include "ocfs2.h"
219e33d69fSJan Kara #include "alloc.h"
22d6b32bbbSJoel Becker #include "blockcheck.h"
239e33d69fSJan Kara #include "inode.h"
249e33d69fSJan Kara #include "journal.h"
259e33d69fSJan Kara #include "file.h"
269e33d69fSJan Kara #include "sysfile.h"
279e33d69fSJan Kara #include "dlmglue.h"
289e33d69fSJan Kara #include "uptodate.h"
29ada50827SJan Kara #include "super.h"
30f64dd44eSJan Kara #include "buffer_head_io.h"
319e33d69fSJan Kara #include "quota.h"
321db986a8STao Ma #include "ocfs2_trace.h"
339e33d69fSJan Kara 
34fb8dd8d7SJan Kara /*
35fb8dd8d7SJan Kara  * Locking of quotas with OCFS2 is rather complex. Here are rules that
36fb8dd8d7SJan Kara  * should be obeyed by all the functions:
37fb8dd8d7SJan Kara  * - any write of quota structure (either to local or global file) is protected
38bc8230eeSJan Kara  *   by dqio_sem or dquot->dq_lock.
39137cebf9Shongnanli  * - any modification of global quota file holds inode cluster lock, i_rwsem,
40fb8dd8d7SJan Kara  *   and ip_alloc_sem of the global quota file (achieved by
41fb8dd8d7SJan Kara  *   ocfs2_lock_global_qf). It also has to hold qinfo_lock.
42fb8dd8d7SJan Kara  * - an allocation of new blocks for local quota file is protected by
43fb8dd8d7SJan Kara  *   its ip_alloc_sem
44fb8dd8d7SJan Kara  *
45fb8dd8d7SJan Kara  * A rough sketch of locking dependencies (lf = local file, gf = global file):
46fb8dd8d7SJan Kara  * Normal filesystem operation:
47bc8230eeSJan Kara  *   start_trans -> dqio_sem -> write to lf
48fb8dd8d7SJan Kara  * Syncing of local and global file:
49bc8230eeSJan Kara  *   ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
50fb8dd8d7SJan Kara  *     write to gf
51fb8dd8d7SJan Kara  *						       -> write to lf
52fb8dd8d7SJan Kara  * Acquire dquot for the first time:
53fb8dd8d7SJan Kara  *   dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf
54fb8dd8d7SJan Kara  *				     -> alloc space for gf
55fb8dd8d7SJan Kara  *				     -> start_trans -> qinfo_lock -> write to gf
56fb8dd8d7SJan Kara  *	     -> ip_alloc_sem of lf -> alloc space for lf
57fb8dd8d7SJan Kara  *	     -> write to lf
58fb8dd8d7SJan Kara  * Release last reference to dquot:
59fb8dd8d7SJan Kara  *   dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf
60fb8dd8d7SJan Kara  *	     -> write to lf
61fb8dd8d7SJan Kara  * Note that all the above operations also hold the inode cluster lock of lf.
62fb8dd8d7SJan Kara  * Recovery:
63fb8dd8d7SJan Kara  *   inode cluster lock of recovered lf
64fb8dd8d7SJan Kara  *     -> read bitmaps -> ip_alloc_sem of lf
65bc8230eeSJan Kara  *     -> ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
66fb8dd8d7SJan Kara  *        write to gf
67fb8dd8d7SJan Kara  */
68fb8dd8d7SJan Kara 
69171bf93cSMark Fasheh static void qsync_work_fn(struct work_struct *work);
70171bf93cSMark Fasheh 
ocfs2_global_disk2memdqb(struct dquot * dquot,void * dp)719e33d69fSJan Kara static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
729e33d69fSJan Kara {
739e33d69fSJan Kara 	struct ocfs2_global_disk_dqblk *d = dp;
749e33d69fSJan Kara 	struct mem_dqblk *m = &dquot->dq_dqb;
759e33d69fSJan Kara 
769e33d69fSJan Kara 	/* Update from disk only entries not set by the admin */
779e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
789e33d69fSJan Kara 		m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
799e33d69fSJan Kara 		m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
809e33d69fSJan Kara 	}
819e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
829e33d69fSJan Kara 		m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
839e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
849e33d69fSJan Kara 		m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
859e33d69fSJan Kara 		m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
869e33d69fSJan Kara 	}
879e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
889e33d69fSJan Kara 		m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
899e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
909e33d69fSJan Kara 		m->dqb_btime = le64_to_cpu(d->dqb_btime);
919e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
929e33d69fSJan Kara 		m->dqb_itime = le64_to_cpu(d->dqb_itime);
939e33d69fSJan Kara 	OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
949e33d69fSJan Kara }
959e33d69fSJan Kara 
ocfs2_global_mem2diskdqb(void * dp,struct dquot * dquot)969e33d69fSJan Kara static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
979e33d69fSJan Kara {
989e33d69fSJan Kara 	struct ocfs2_global_disk_dqblk *d = dp;
999e33d69fSJan Kara 	struct mem_dqblk *m = &dquot->dq_dqb;
1009e33d69fSJan Kara 
1014c376dcaSEric W. Biederman 	d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
1029e33d69fSJan Kara 	d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
1039e33d69fSJan Kara 	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
1049e33d69fSJan Kara 	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
1059e33d69fSJan Kara 	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
1069e33d69fSJan Kara 	d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
1079e33d69fSJan Kara 	d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
1089e33d69fSJan Kara 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
1099e33d69fSJan Kara 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
1109e33d69fSJan Kara 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
1117669f54cSJan Kara 	d->dqb_pad1 = d->dqb_pad2 = 0;
1129e33d69fSJan Kara }
1139e33d69fSJan Kara 
ocfs2_global_is_id(void * dp,struct dquot * dquot)1149e33d69fSJan Kara static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
1159e33d69fSJan Kara {
1169e33d69fSJan Kara 	struct ocfs2_global_disk_dqblk *d = dp;
1179e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo =
1184c376dcaSEric W. Biederman 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
1199e33d69fSJan Kara 
1209e33d69fSJan Kara 	if (qtree_entry_unused(&oinfo->dqi_gi, dp))
1219e33d69fSJan Kara 		return 0;
1224c376dcaSEric W. Biederman 
1234c376dcaSEric W. Biederman 	return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
1244c376dcaSEric W. Biederman 				le32_to_cpu(d->dqb_id)),
1254c376dcaSEric W. Biederman 		      dquot->dq_id);
1269e33d69fSJan Kara }
1279e33d69fSJan Kara 
128d1b98c23SJulia Lawall const struct qtree_fmt_operations ocfs2_global_ops = {
1299e33d69fSJan Kara 	.mem2disk_dqblk = ocfs2_global_mem2diskdqb,
1309e33d69fSJan Kara 	.disk2mem_dqblk = ocfs2_global_disk2memdqb,
1319e33d69fSJan Kara 	.is_id = ocfs2_global_is_id,
1329e33d69fSJan Kara };
1339e33d69fSJan Kara 
ocfs2_validate_quota_block(struct super_block * sb,struct buffer_head * bh)134fb8dd8d7SJan Kara int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh)
135684ef278SJoel Becker {
136d6b32bbbSJoel Becker 	struct ocfs2_disk_dqtrailer *dqt =
137d6b32bbbSJoel Becker 		ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
138684ef278SJoel Becker 
1391db986a8STao Ma 	trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr);
140684ef278SJoel Becker 
141d6b32bbbSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
142d6b32bbbSJoel Becker 
143d6b32bbbSJoel Becker 	/*
144d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
145d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
146d6b32bbbSJoel Becker 	 * local to this block.
147d6b32bbbSJoel Becker 	 */
148d6b32bbbSJoel Becker 	return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
149684ef278SJoel Becker }
150684ef278SJoel Becker 
ocfs2_read_quota_phys_block(struct inode * inode,u64 p_block,struct buffer_head ** bhp)151f64dd44eSJan Kara int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
152f64dd44eSJan Kara 				struct buffer_head **bhp)
153f64dd44eSJan Kara {
154f64dd44eSJan Kara 	int rc;
155f64dd44eSJan Kara 
156f64dd44eSJan Kara 	*bhp = NULL;
157f64dd44eSJan Kara 	rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
158f64dd44eSJan Kara 			       ocfs2_validate_quota_block);
159f64dd44eSJan Kara 	if (rc)
160f64dd44eSJan Kara 		mlog_errno(rc);
161f64dd44eSJan Kara 	return rc;
162f64dd44eSJan Kara }
163f64dd44eSJan Kara 
1649e33d69fSJan Kara /* Read data from global quotafile - avoid pagecache and such because we cannot
1659e33d69fSJan Kara  * afford acquiring the locks... We use quota cluster lock to serialize
1669e33d69fSJan Kara  * operations. Caller is responsible for acquiring it. */
ocfs2_quota_read(struct super_block * sb,int type,char * data,size_t len,loff_t off)1679e33d69fSJan Kara ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
1689e33d69fSJan Kara 			 size_t len, loff_t off)
1699e33d69fSJan Kara {
1709e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
1719e33d69fSJan Kara 	struct inode *gqinode = oinfo->dqi_gqinode;
1729e33d69fSJan Kara 	loff_t i_size = i_size_read(gqinode);
1739e33d69fSJan Kara 	int offset = off & (sb->s_blocksize - 1);
1749e33d69fSJan Kara 	sector_t blk = off >> sb->s_blocksize_bits;
1759e33d69fSJan Kara 	int err = 0;
1769e33d69fSJan Kara 	struct buffer_head *bh;
1779e33d69fSJan Kara 	size_t toread, tocopy;
178fb8dd8d7SJan Kara 	u64 pblock = 0, pcount = 0;
1799e33d69fSJan Kara 
1809e33d69fSJan Kara 	if (off > i_size)
1819e33d69fSJan Kara 		return 0;
1829e33d69fSJan Kara 	if (off + len > i_size)
1839e33d69fSJan Kara 		len = i_size - off;
1849e33d69fSJan Kara 	toread = len;
1859e33d69fSJan Kara 	while (toread > 0) {
186dad7d975SMark Fasheh 		tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
187fb8dd8d7SJan Kara 		if (!pcount) {
188fb8dd8d7SJan Kara 			err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock,
189fb8dd8d7SJan Kara 							  &pcount, NULL);
190fb8dd8d7SJan Kara 			if (err) {
191fb8dd8d7SJan Kara 				mlog_errno(err);
192fb8dd8d7SJan Kara 				return err;
193fb8dd8d7SJan Kara 			}
194fb8dd8d7SJan Kara 		} else {
195fb8dd8d7SJan Kara 			pcount--;
196fb8dd8d7SJan Kara 			pblock++;
197fb8dd8d7SJan Kara 		}
19885eb8b73SJoel Becker 		bh = NULL;
199fb8dd8d7SJan Kara 		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
20085eb8b73SJoel Becker 		if (err) {
2019e33d69fSJan Kara 			mlog_errno(err);
2029e33d69fSJan Kara 			return err;
2039e33d69fSJan Kara 		}
2049e33d69fSJan Kara 		memcpy(data, bh->b_data + offset, tocopy);
2059e33d69fSJan Kara 		brelse(bh);
2069e33d69fSJan Kara 		offset = 0;
2079e33d69fSJan Kara 		toread -= tocopy;
2089e33d69fSJan Kara 		data += tocopy;
2099e33d69fSJan Kara 		blk++;
2109e33d69fSJan Kara 	}
2119e33d69fSJan Kara 	return len;
2129e33d69fSJan Kara }
2139e33d69fSJan Kara 
2149e33d69fSJan Kara /* Write to quotafile (we know the transaction is already started and has
2159e33d69fSJan Kara  * enough credits) */
ocfs2_quota_write(struct super_block * sb,int type,const char * data,size_t len,loff_t off)2169e33d69fSJan Kara ssize_t ocfs2_quota_write(struct super_block *sb, int type,
2179e33d69fSJan Kara 			  const char *data, size_t len, loff_t off)
2189e33d69fSJan Kara {
2199e33d69fSJan Kara 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
2209e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
2219e33d69fSJan Kara 	struct inode *gqinode = oinfo->dqi_gqinode;
2229e33d69fSJan Kara 	int offset = off & (sb->s_blocksize - 1);
2239e33d69fSJan Kara 	sector_t blk = off >> sb->s_blocksize_bits;
224af09e51bSJan Kara 	int err = 0, new = 0, ja_type;
22585eb8b73SJoel Becker 	struct buffer_head *bh = NULL;
2269e33d69fSJan Kara 	handle_t *handle = journal_current_handle();
227fb8dd8d7SJan Kara 	u64 pblock, pcount;
2289e33d69fSJan Kara 
2299e33d69fSJan Kara 	if (!handle) {
2309e33d69fSJan Kara 		mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
2319e33d69fSJan Kara 		     "because transaction was not started.\n",
2329e33d69fSJan Kara 		     (unsigned long long)off, (unsigned long long)len);
2339e33d69fSJan Kara 		return -EIO;
2349e33d69fSJan Kara 	}
2359e33d69fSJan Kara 	if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
2369e33d69fSJan Kara 		WARN_ON(1);
2379e33d69fSJan Kara 		len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
2389e33d69fSJan Kara 	}
2399e33d69fSJan Kara 
240f17c20ddSJunxiao Bi 	if (i_size_read(gqinode) < off + len) {
241b57ac2c4SJan Kara 		loff_t rounded_end =
242b57ac2c4SJan Kara 				ocfs2_align_bytes_to_blocks(sb, off + len);
243b57ac2c4SJan Kara 
244fb8dd8d7SJan Kara 		/* Space is already allocated in ocfs2_acquire_dquot() */
2459e33d69fSJan Kara 		err = ocfs2_simple_size_update(gqinode,
2469e33d69fSJan Kara 					       oinfo->dqi_gqi_bh,
247b57ac2c4SJan Kara 					       rounded_end);
2489e33d69fSJan Kara 		if (err < 0)
2499e33d69fSJan Kara 			goto out;
2509e33d69fSJan Kara 		new = 1;
2519e33d69fSJan Kara 	}
252fb8dd8d7SJan Kara 	err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL);
253fb8dd8d7SJan Kara 	if (err) {
254fb8dd8d7SJan Kara 		mlog_errno(err);
255fb8dd8d7SJan Kara 		goto out;
256fb8dd8d7SJan Kara 	}
2579e33d69fSJan Kara 	/* Not rewriting whole block? */
2589e33d69fSJan Kara 	if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
2599e33d69fSJan Kara 	    !new) {
260fb8dd8d7SJan Kara 		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
261af09e51bSJan Kara 		ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
262af09e51bSJan Kara 	} else {
263fb8dd8d7SJan Kara 		bh = sb_getblk(sb, pblock);
264fb8dd8d7SJan Kara 		if (!bh)
265fb8dd8d7SJan Kara 			err = -ENOMEM;
266af09e51bSJan Kara 		ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
267af09e51bSJan Kara 	}
26885eb8b73SJoel Becker 	if (err) {
2699e33d69fSJan Kara 		mlog_errno(err);
270e9956faeSTao Ma 		goto out;
2719e33d69fSJan Kara 	}
2729e33d69fSJan Kara 	lock_buffer(bh);
2739e33d69fSJan Kara 	if (new)
2749e33d69fSJan Kara 		memset(bh->b_data, 0, sb->s_blocksize);
2759e33d69fSJan Kara 	memcpy(bh->b_data + offset, data, len);
2769e33d69fSJan Kara 	flush_dcache_page(bh->b_page);
277af09e51bSJan Kara 	set_buffer_uptodate(bh);
2789e33d69fSJan Kara 	unlock_buffer(bh);
2798cb471e8SJoel Becker 	ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
2800cf2f763SJoel Becker 	err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
2810cf2f763SJoel Becker 				      ja_type);
282af09e51bSJan Kara 	if (err < 0) {
283af09e51bSJan Kara 		brelse(bh);
284af09e51bSJan Kara 		goto out;
285af09e51bSJan Kara 	}
286ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
2879e33d69fSJan Kara 	brelse(bh);
2889e33d69fSJan Kara out:
2899e33d69fSJan Kara 	if (err) {
2909e33d69fSJan Kara 		mlog_errno(err);
2919e33d69fSJan Kara 		return err;
2929e33d69fSJan Kara 	}
293cc56c33eSJeff Layton 	inode_inc_iversion(gqinode);
2949e33d69fSJan Kara 	ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
2959e33d69fSJan Kara 	return len;
2969e33d69fSJan Kara }
2979e33d69fSJan Kara 
ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo * oinfo,int ex)2989e33d69fSJan Kara int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
2999e33d69fSJan Kara {
3009e33d69fSJan Kara 	int status;
3019e33d69fSJan Kara 	struct buffer_head *bh = NULL;
3029e33d69fSJan Kara 
3039e33d69fSJan Kara 	status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
3049e33d69fSJan Kara 	if (status < 0)
3059e33d69fSJan Kara 		return status;
3069e33d69fSJan Kara 	spin_lock(&dq_data_lock);
3079e33d69fSJan Kara 	if (!oinfo->dqi_gqi_count++)
3089e33d69fSJan Kara 		oinfo->dqi_gqi_bh = bh;
3099e33d69fSJan Kara 	else
3109e33d69fSJan Kara 		WARN_ON(bh != oinfo->dqi_gqi_bh);
3119e33d69fSJan Kara 	spin_unlock(&dq_data_lock);
312fb8dd8d7SJan Kara 	if (ex) {
3135955102cSAl Viro 		inode_lock(oinfo->dqi_gqinode);
314fb8dd8d7SJan Kara 		down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
315fb8dd8d7SJan Kara 	} else {
316fb8dd8d7SJan Kara 		down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
317fb8dd8d7SJan Kara 	}
3189e33d69fSJan Kara 	return 0;
3199e33d69fSJan Kara }
3209e33d69fSJan Kara 
ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo * oinfo,int ex)3219e33d69fSJan Kara void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
3229e33d69fSJan Kara {
323fb8dd8d7SJan Kara 	if (ex) {
324fb8dd8d7SJan Kara 		up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
3255955102cSAl Viro 		inode_unlock(oinfo->dqi_gqinode);
326fb8dd8d7SJan Kara 	} else {
327fb8dd8d7SJan Kara 		up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
328fb8dd8d7SJan Kara 	}
3299e33d69fSJan Kara 	ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
3309e33d69fSJan Kara 	brelse(oinfo->dqi_gqi_bh);
3319e33d69fSJan Kara 	spin_lock(&dq_data_lock);
3329e33d69fSJan Kara 	if (!--oinfo->dqi_gqi_count)
3339e33d69fSJan Kara 		oinfo->dqi_gqi_bh = NULL;
3349e33d69fSJan Kara 	spin_unlock(&dq_data_lock);
3359e33d69fSJan Kara }
3369e33d69fSJan Kara 
3379e33d69fSJan Kara /* Read information header from global quota file */
ocfs2_global_read_info(struct super_block * sb,int type)3389e33d69fSJan Kara int ocfs2_global_read_info(struct super_block *sb, int type)
3399e33d69fSJan Kara {
34052362810SJan Kara 	unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
3419e33d69fSJan Kara 					      GROUP_QUOTA_SYSTEM_INODE };
3429e33d69fSJan Kara 	struct ocfs2_global_disk_dqinfo dinfo;
3439e33d69fSJan Kara 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
3449e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
345ae4f6ef1SJan Kara 	u64 pcount;
3469e33d69fSJan Kara 	int status;
3479e33d69fSJan Kara 
348de194334SJoseph Qi 	oinfo->dqi_gi.dqi_sb = sb;
349de194334SJoseph Qi 	oinfo->dqi_gi.dqi_type = type;
350de194334SJoseph Qi 	ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
351de194334SJoseph Qi 	oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
352de194334SJoseph Qi 	oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
353de194334SJoseph Qi 	oinfo->dqi_gqi_bh = NULL;
354de194334SJoseph Qi 	oinfo->dqi_gqi_count = 0;
355de194334SJoseph Qi 
3569e33d69fSJan Kara 	/* Read global header */
357de194334SJoseph Qi 	oinfo->dqi_gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
3589e33d69fSJan Kara 			OCFS2_INVALID_SLOT);
359de194334SJoseph Qi 	if (!oinfo->dqi_gqinode) {
3609e33d69fSJan Kara 		mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
3619e33d69fSJan Kara 			type);
3629e33d69fSJan Kara 		status = -EINVAL;
3639e33d69fSJan Kara 		goto out_err;
3649e33d69fSJan Kara 	}
365de194334SJoseph Qi 
3669e33d69fSJan Kara 	status = ocfs2_lock_global_qf(oinfo, 0);
3679e33d69fSJan Kara 	if (status < 0) {
3689e33d69fSJan Kara 		mlog_errno(status);
3699e33d69fSJan Kara 		goto out_err;
3709e33d69fSJan Kara 	}
371ae4f6ef1SJan Kara 
372de194334SJoseph Qi 	status = ocfs2_extent_map_get_blocks(oinfo->dqi_gqinode, 0, &oinfo->dqi_giblk,
373ae4f6ef1SJan Kara 					     &pcount, NULL);
374ae4f6ef1SJan Kara 	if (status < 0)
375ae4f6ef1SJan Kara 		goto out_unlock;
376ae4f6ef1SJan Kara 
377ae4f6ef1SJan Kara 	status = ocfs2_qinfo_lock(oinfo, 0);
378ae4f6ef1SJan Kara 	if (status < 0)
379ae4f6ef1SJan Kara 		goto out_unlock;
3809e33d69fSJan Kara 	status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
3819e33d69fSJan Kara 				      sizeof(struct ocfs2_global_disk_dqinfo),
3829e33d69fSJan Kara 				      OCFS2_GLOBAL_INFO_OFF);
383ae4f6ef1SJan Kara 	ocfs2_qinfo_unlock(oinfo, 0);
3849e33d69fSJan Kara 	ocfs2_unlock_global_qf(oinfo, 0);
3859e33d69fSJan Kara 	if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
3869e33d69fSJan Kara 		mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
3879e33d69fSJan Kara 		     status);
3889e33d69fSJan Kara 		if (status >= 0)
3899e33d69fSJan Kara 			status = -EIO;
3909e33d69fSJan Kara 		mlog_errno(status);
3919e33d69fSJan Kara 		goto out_err;
3929e33d69fSJan Kara 	}
3939e33d69fSJan Kara 	info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
3949e33d69fSJan Kara 	info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
3959e33d69fSJan Kara 	oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
3969e33d69fSJan Kara 	oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
3979e33d69fSJan Kara 	oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
3989e33d69fSJan Kara 	oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
3999e33d69fSJan Kara 	oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
4009e33d69fSJan Kara 	oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
4019e33d69fSJan Kara 						OCFS2_QBLK_RESERVED_SPACE;
4029e33d69fSJan Kara 	oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
403171bf93cSMark Fasheh 	INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
404316873c9STejun Heo 	schedule_delayed_work(&oinfo->dqi_sync_work,
4054539f1dfSJan Kara 			      msecs_to_jiffies(oinfo->dqi_syncms));
406171bf93cSMark Fasheh 
4079e33d69fSJan Kara out_err:
4089e33d69fSJan Kara 	return status;
409ae4f6ef1SJan Kara out_unlock:
410ae4f6ef1SJan Kara 	ocfs2_unlock_global_qf(oinfo, 0);
411ae4f6ef1SJan Kara 	mlog_errno(status);
412ae4f6ef1SJan Kara 	goto out_err;
4139e33d69fSJan Kara }
4149e33d69fSJan Kara 
415*233eb8d6SJiangshan Yi /* Write information to global quota file. Expects exclusive lock on quota
4169e33d69fSJan Kara  * file inode and quota info */
__ocfs2_global_write_info(struct super_block * sb,int type)4179e33d69fSJan Kara static int __ocfs2_global_write_info(struct super_block *sb, int type)
4189e33d69fSJan Kara {
4199e33d69fSJan Kara 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
4209e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
4219e33d69fSJan Kara 	struct ocfs2_global_disk_dqinfo dinfo;
4229e33d69fSJan Kara 	ssize_t size;
4239e33d69fSJan Kara 
4249e33d69fSJan Kara 	spin_lock(&dq_data_lock);
4259e33d69fSJan Kara 	info->dqi_flags &= ~DQF_INFO_DIRTY;
4269e33d69fSJan Kara 	dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
4279e33d69fSJan Kara 	dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
4289e33d69fSJan Kara 	spin_unlock(&dq_data_lock);
4299e33d69fSJan Kara 	dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
4309e33d69fSJan Kara 	dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
4319e33d69fSJan Kara 	dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
4329e33d69fSJan Kara 	dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
4339e33d69fSJan Kara 	size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
4349e33d69fSJan Kara 				     sizeof(struct ocfs2_global_disk_dqinfo),
4359e33d69fSJan Kara 				     OCFS2_GLOBAL_INFO_OFF);
4369e33d69fSJan Kara 	if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
4379e33d69fSJan Kara 		mlog(ML_ERROR, "Cannot write global quota info structure\n");
4389e33d69fSJan Kara 		if (size >= 0)
4399e33d69fSJan Kara 			size = -EIO;
4409e33d69fSJan Kara 		return size;
4419e33d69fSJan Kara 	}
4429e33d69fSJan Kara 	return 0;
4439e33d69fSJan Kara }
4449e33d69fSJan Kara 
ocfs2_global_write_info(struct super_block * sb,int type)4459e33d69fSJan Kara int ocfs2_global_write_info(struct super_block *sb, int type)
4469e33d69fSJan Kara {
4479e33d69fSJan Kara 	int err;
4489a8ae30eSJan Kara 	struct quota_info *dqopt = sb_dqopt(sb);
4499a8ae30eSJan Kara 	struct ocfs2_mem_dqinfo *info = dqopt->info[type].dqi_priv;
4509e33d69fSJan Kara 
4519a8ae30eSJan Kara 	down_write(&dqopt->dqio_sem);
4529e33d69fSJan Kara 	err = ocfs2_qinfo_lock(info, 1);
4539e33d69fSJan Kara 	if (err < 0)
4549a8ae30eSJan Kara 		goto out_sem;
4559e33d69fSJan Kara 	err = __ocfs2_global_write_info(sb, type);
4569e33d69fSJan Kara 	ocfs2_qinfo_unlock(info, 1);
4579a8ae30eSJan Kara out_sem:
4589a8ae30eSJan Kara 	up_write(&dqopt->dqio_sem);
4599e33d69fSJan Kara 	return err;
4609e33d69fSJan Kara }
4619e33d69fSJan Kara 
ocfs2_global_qinit_alloc(struct super_block * sb,int type)462b409d7a0SJan Kara static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
463b409d7a0SJan Kara {
464b409d7a0SJan Kara 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
465b409d7a0SJan Kara 
466b409d7a0SJan Kara 	/*
467b409d7a0SJan Kara 	 * We may need to allocate tree blocks and a leaf block but not the
468b409d7a0SJan Kara 	 * root block
469b409d7a0SJan Kara 	 */
470b409d7a0SJan Kara 	return oinfo->dqi_gi.dqi_qtree_depth;
471b409d7a0SJan Kara }
472b409d7a0SJan Kara 
ocfs2_calc_global_qinit_credits(struct super_block * sb,int type)473b409d7a0SJan Kara static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
474b409d7a0SJan Kara {
475832d09cfSJan Kara 	/* We modify all the allocated blocks, tree root, info block and
476832d09cfSJan Kara 	 * the inode */
477b409d7a0SJan Kara 	return (ocfs2_global_qinit_alloc(sb, type) + 2) *
478832d09cfSJan Kara 			OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1;
479b409d7a0SJan Kara }
480b409d7a0SJan Kara 
4819e33d69fSJan Kara /* Sync local information about quota modifications with global quota file.
4829e33d69fSJan Kara  * Caller must have started the transaction and obtained exclusive lock for
4839e33d69fSJan Kara  * global quota file inode */
__ocfs2_sync_dquot(struct dquot * dquot,int freeing)4849e33d69fSJan Kara int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
4859e33d69fSJan Kara {
4869e33d69fSJan Kara 	int err, err2;
4879e33d69fSJan Kara 	struct super_block *sb = dquot->dq_sb;
4884c376dcaSEric W. Biederman 	int type = dquot->dq_id.type;
4899e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
4909e33d69fSJan Kara 	struct ocfs2_global_disk_dqblk dqblk;
4919e33d69fSJan Kara 	s64 spacechange, inodechange;
492e008bb61SArnd Bergmann 	time64_t olditime, oldbtime;
4939e33d69fSJan Kara 
4949e33d69fSJan Kara 	err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
4959e33d69fSJan Kara 				   sizeof(struct ocfs2_global_disk_dqblk),
4969e33d69fSJan Kara 				   dquot->dq_off);
4979e33d69fSJan Kara 	if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
4989e33d69fSJan Kara 		if (err >= 0) {
4999e33d69fSJan Kara 			mlog(ML_ERROR, "Short read from global quota file "
5009e33d69fSJan Kara 				       "(%u read)\n", err);
5019e33d69fSJan Kara 			err = -EIO;
5029e33d69fSJan Kara 		}
5039e33d69fSJan Kara 		goto out;
5049e33d69fSJan Kara 	}
5059e33d69fSJan Kara 
5069e33d69fSJan Kara 	/* Update space and inode usage. Get also other information from
5079e33d69fSJan Kara 	 * global quota file so that we don't overwrite any changes there.
5089e33d69fSJan Kara 	 * We are */
5097b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5109e33d69fSJan Kara 	spacechange = dquot->dq_dqb.dqb_curspace -
5119e33d69fSJan Kara 					OCFS2_DQUOT(dquot)->dq_origspace;
5129e33d69fSJan Kara 	inodechange = dquot->dq_dqb.dqb_curinodes -
5139e33d69fSJan Kara 					OCFS2_DQUOT(dquot)->dq_originodes;
5149e33d69fSJan Kara 	olditime = dquot->dq_dqb.dqb_itime;
5159e33d69fSJan Kara 	oldbtime = dquot->dq_dqb.dqb_btime;
5169e33d69fSJan Kara 	ocfs2_global_disk2memdqb(dquot, &dqblk);
5174c376dcaSEric W. Biederman 	trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id),
5184c376dcaSEric W. Biederman 			       dquot->dq_dqb.dqb_curspace,
5191db986a8STao Ma 			       (long long)spacechange,
5201db986a8STao Ma 			       dquot->dq_dqb.dqb_curinodes,
5211db986a8STao Ma 			       (long long)inodechange);
5229e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
5239e33d69fSJan Kara 		dquot->dq_dqb.dqb_curspace += spacechange;
5249e33d69fSJan Kara 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
5259e33d69fSJan Kara 		dquot->dq_dqb.dqb_curinodes += inodechange;
5269e33d69fSJan Kara 	/* Set properly space grace time... */
5279e33d69fSJan Kara 	if (dquot->dq_dqb.dqb_bsoftlimit &&
5289e33d69fSJan Kara 	    dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
5299e33d69fSJan Kara 		if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
5309e33d69fSJan Kara 		    oldbtime > 0) {
5319e33d69fSJan Kara 			if (dquot->dq_dqb.dqb_btime > 0)
5329e33d69fSJan Kara 				dquot->dq_dqb.dqb_btime =
5339e33d69fSJan Kara 					min(dquot->dq_dqb.dqb_btime, oldbtime);
5349e33d69fSJan Kara 			else
5359e33d69fSJan Kara 				dquot->dq_dqb.dqb_btime = oldbtime;
5369e33d69fSJan Kara 		}
5379e33d69fSJan Kara 	} else {
5389e33d69fSJan Kara 		dquot->dq_dqb.dqb_btime = 0;
5399e33d69fSJan Kara 		clear_bit(DQ_BLKS_B, &dquot->dq_flags);
5409e33d69fSJan Kara 	}
5419e33d69fSJan Kara 	/* Set properly inode grace time... */
5429e33d69fSJan Kara 	if (dquot->dq_dqb.dqb_isoftlimit &&
5439e33d69fSJan Kara 	    dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
5449e33d69fSJan Kara 		if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
5459e33d69fSJan Kara 		    olditime > 0) {
5469e33d69fSJan Kara 			if (dquot->dq_dqb.dqb_itime > 0)
5479e33d69fSJan Kara 				dquot->dq_dqb.dqb_itime =
5489e33d69fSJan Kara 					min(dquot->dq_dqb.dqb_itime, olditime);
5499e33d69fSJan Kara 			else
5509e33d69fSJan Kara 				dquot->dq_dqb.dqb_itime = olditime;
5519e33d69fSJan Kara 		}
5529e33d69fSJan Kara 	} else {
5539e33d69fSJan Kara 		dquot->dq_dqb.dqb_itime = 0;
5549e33d69fSJan Kara 		clear_bit(DQ_INODES_B, &dquot->dq_flags);
5559e33d69fSJan Kara 	}
5569e33d69fSJan Kara 	/* All information is properly updated, clear the flags */
5579e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
5589e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
5599e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
5609e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
5619e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
5629e33d69fSJan Kara 	__clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
5639e33d69fSJan Kara 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
5649e33d69fSJan Kara 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
5657b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5669e33d69fSJan Kara 	err = ocfs2_qinfo_lock(info, freeing);
5679e33d69fSJan Kara 	if (err < 0) {
56825985edcSLucas De Marchi 		mlog(ML_ERROR, "Failed to lock quota info, losing quota write"
5694c376dcaSEric W. Biederman 			       " (type=%d, id=%u)\n", dquot->dq_id.type,
5704c376dcaSEric W. Biederman 			       (unsigned)from_kqid(&init_user_ns, dquot->dq_id));
5719e33d69fSJan Kara 		goto out;
5729e33d69fSJan Kara 	}
5739e33d69fSJan Kara 	if (freeing)
5749e33d69fSJan Kara 		OCFS2_DQUOT(dquot)->dq_use_count--;
5759e33d69fSJan Kara 	err = qtree_write_dquot(&info->dqi_gi, dquot);
5769e33d69fSJan Kara 	if (err < 0)
5779e33d69fSJan Kara 		goto out_qlock;
5789e33d69fSJan Kara 	if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
5799e33d69fSJan Kara 		err = qtree_release_dquot(&info->dqi_gi, dquot);
5809e33d69fSJan Kara 		if (info_dirty(sb_dqinfo(sb, type))) {
5819e33d69fSJan Kara 			err2 = __ocfs2_global_write_info(sb, type);
5829e33d69fSJan Kara 			if (!err)
5839e33d69fSJan Kara 				err = err2;
5849e33d69fSJan Kara 		}
5859e33d69fSJan Kara 	}
5869e33d69fSJan Kara out_qlock:
5879e33d69fSJan Kara 	ocfs2_qinfo_unlock(info, freeing);
5889e33d69fSJan Kara out:
5899e33d69fSJan Kara 	if (err < 0)
5909e33d69fSJan Kara 		mlog_errno(err);
5919e33d69fSJan Kara 	return err;
5929e33d69fSJan Kara }
5939e33d69fSJan Kara 
5949e33d69fSJan Kara /*
595171bf93cSMark Fasheh  *  Functions for periodic syncing of dquots with global file
596171bf93cSMark Fasheh  */
ocfs2_sync_dquot_helper(struct dquot * dquot,unsigned long type)597171bf93cSMark Fasheh static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
598171bf93cSMark Fasheh {
599171bf93cSMark Fasheh 	handle_t *handle;
600171bf93cSMark Fasheh 	struct super_block *sb = dquot->dq_sb;
601171bf93cSMark Fasheh 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
602171bf93cSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(sb);
603171bf93cSMark Fasheh 	int status = 0;
604171bf93cSMark Fasheh 
6054c376dcaSEric W. Biederman 	trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id),
6064c376dcaSEric W. Biederman 				      dquot->dq_id.type,
6071db986a8STao Ma 				      type, sb->s_id);
6084c376dcaSEric W. Biederman 	if (type != dquot->dq_id.type)
609171bf93cSMark Fasheh 		goto out;
610171bf93cSMark Fasheh 	status = ocfs2_lock_global_qf(oinfo, 1);
611171bf93cSMark Fasheh 	if (status < 0)
612171bf93cSMark Fasheh 		goto out;
613171bf93cSMark Fasheh 
614171bf93cSMark Fasheh 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
615171bf93cSMark Fasheh 	if (IS_ERR(handle)) {
616171bf93cSMark Fasheh 		status = PTR_ERR(handle);
617171bf93cSMark Fasheh 		mlog_errno(status);
618171bf93cSMark Fasheh 		goto out_ilock;
619171bf93cSMark Fasheh 	}
620bc8230eeSJan Kara 	down_write(&sb_dqopt(sb)->dqio_sem);
621171bf93cSMark Fasheh 	status = ocfs2_sync_dquot(dquot);
622171bf93cSMark Fasheh 	if (status < 0)
623171bf93cSMark Fasheh 		mlog_errno(status);
624171bf93cSMark Fasheh 	/* We have to write local structure as well... */
625741e1289SJan Kara 	status = ocfs2_local_write_dquot(dquot);
626171bf93cSMark Fasheh 	if (status < 0)
627171bf93cSMark Fasheh 		mlog_errno(status);
628bc8230eeSJan Kara 	up_write(&sb_dqopt(sb)->dqio_sem);
629171bf93cSMark Fasheh 	ocfs2_commit_trans(osb, handle);
630171bf93cSMark Fasheh out_ilock:
631171bf93cSMark Fasheh 	ocfs2_unlock_global_qf(oinfo, 1);
632171bf93cSMark Fasheh out:
633171bf93cSMark Fasheh 	return status;
634171bf93cSMark Fasheh }
635171bf93cSMark Fasheh 
qsync_work_fn(struct work_struct * work)636171bf93cSMark Fasheh static void qsync_work_fn(struct work_struct *work)
637171bf93cSMark Fasheh {
638171bf93cSMark Fasheh 	struct ocfs2_mem_dqinfo *oinfo = container_of(work,
639171bf93cSMark Fasheh 						      struct ocfs2_mem_dqinfo,
640171bf93cSMark Fasheh 						      dqi_sync_work.work);
641171bf93cSMark Fasheh 	struct super_block *sb = oinfo->dqi_gqinode->i_sb;
642171bf93cSMark Fasheh 
6432a64f80eSJan Kara 	/*
6442a64f80eSJan Kara 	 * We have to be careful here not to deadlock on s_umount as umount
6452a64f80eSJan Kara 	 * disabling quotas may be in progress and it waits for this work to
6462a64f80eSJan Kara 	 * complete. If trylock fails, we'll do the sync next time...
6472a64f80eSJan Kara 	 */
6482a64f80eSJan Kara 	if (down_read_trylock(&sb->s_umount)) {
649171bf93cSMark Fasheh 		dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
6502a64f80eSJan Kara 		up_read(&sb->s_umount);
6512a64f80eSJan Kara 	}
652316873c9STejun Heo 	schedule_delayed_work(&oinfo->dqi_sync_work,
6534539f1dfSJan Kara 			      msecs_to_jiffies(oinfo->dqi_syncms));
654171bf93cSMark Fasheh }
655171bf93cSMark Fasheh 
656171bf93cSMark Fasheh /*
6579e33d69fSJan Kara  *  Wrappers for generic quota functions
6589e33d69fSJan Kara  */
6599e33d69fSJan Kara 
ocfs2_write_dquot(struct dquot * dquot)6609e33d69fSJan Kara static int ocfs2_write_dquot(struct dquot *dquot)
6619e33d69fSJan Kara {
6629e33d69fSJan Kara 	handle_t *handle;
6639e33d69fSJan Kara 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
6649e33d69fSJan Kara 	int status = 0;
6659e33d69fSJan Kara 
6664c376dcaSEric W. Biederman 	trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id),
6674c376dcaSEric W. Biederman 				dquot->dq_id.type);
6689e33d69fSJan Kara 
6699e33d69fSJan Kara 	handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
6709e33d69fSJan Kara 	if (IS_ERR(handle)) {
6719e33d69fSJan Kara 		status = PTR_ERR(handle);
6729e33d69fSJan Kara 		mlog_errno(status);
6739e33d69fSJan Kara 		goto out;
6749e33d69fSJan Kara 	}
675bc8230eeSJan Kara 	down_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
676741e1289SJan Kara 	status = ocfs2_local_write_dquot(dquot);
677bc8230eeSJan Kara 	up_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
6789e33d69fSJan Kara 	ocfs2_commit_trans(osb, handle);
6799e33d69fSJan Kara out:
6809e33d69fSJan Kara 	return status;
6819e33d69fSJan Kara }
6829e33d69fSJan Kara 
ocfs2_calc_qdel_credits(struct super_block * sb,int type)683b409d7a0SJan Kara static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
6849e33d69fSJan Kara {
685b409d7a0SJan Kara 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
6860584974aSJan Kara 	/*
6870584974aSJan Kara 	 * We modify tree, leaf block, global info, local chunk header,
6880584974aSJan Kara 	 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
6890584974aSJan Kara 	 * accounts for inode update
6900584974aSJan Kara 	 */
691b409d7a0SJan Kara 	return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
692b409d7a0SJan Kara 	       OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
6930584974aSJan Kara 	       OCFS2_QINFO_WRITE_CREDITS +
6940584974aSJan Kara 	       OCFS2_INODE_UPDATE_CREDITS;
6959e33d69fSJan Kara }
6969e33d69fSJan Kara 
ocfs2_drop_dquot_refs(struct work_struct * work)697e3a767b6SJan Kara void ocfs2_drop_dquot_refs(struct work_struct *work)
698e3a767b6SJan Kara {
699e3a767b6SJan Kara 	struct ocfs2_super *osb = container_of(work, struct ocfs2_super,
700e3a767b6SJan Kara 					       dquot_drop_work);
701e3a767b6SJan Kara 	struct llist_node *list;
702e3a767b6SJan Kara 	struct ocfs2_dquot *odquot, *next_odquot;
703e3a767b6SJan Kara 
704e3a767b6SJan Kara 	list = llist_del_all(&osb->dquot_drop_list);
705e3a767b6SJan Kara 	llist_for_each_entry_safe(odquot, next_odquot, list, list) {
706e3a767b6SJan Kara 		/* Drop the reference we acquired in ocfs2_dquot_release() */
707e3a767b6SJan Kara 		dqput(&odquot->dq_dquot);
708e3a767b6SJan Kara 	}
709e3a767b6SJan Kara }
710e3a767b6SJan Kara 
711e3a767b6SJan Kara /*
712e3a767b6SJan Kara  * Called when the last reference to dquot is dropped. If we are called from
713e3a767b6SJan Kara  * downconvert thread, we cannot do all the handling here because grabbing
714e3a767b6SJan Kara  * quota lock could deadlock (the node holding the quota lock could need some
715e3a767b6SJan Kara  * other cluster lock to proceed but with blocked downconvert thread we cannot
716e3a767b6SJan Kara  * release any lock).
717e3a767b6SJan Kara  */
ocfs2_release_dquot(struct dquot * dquot)7189e33d69fSJan Kara static int ocfs2_release_dquot(struct dquot *dquot)
7199e33d69fSJan Kara {
7209e33d69fSJan Kara 	handle_t *handle;
7219e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo =
7224c376dcaSEric W. Biederman 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
7239e33d69fSJan Kara 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
7249e33d69fSJan Kara 	int status = 0;
7259e33d69fSJan Kara 
7264c376dcaSEric W. Biederman 	trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id),
7274c376dcaSEric W. Biederman 				  dquot->dq_id.type);
7289e33d69fSJan Kara 
729fb8dd8d7SJan Kara 	mutex_lock(&dquot->dq_lock);
730fb8dd8d7SJan Kara 	/* Check whether we are not racing with some other dqget() */
731df4bb5d1SDmitry Monakhov 	if (dquot_is_busy(dquot))
732fb8dd8d7SJan Kara 		goto out;
733e3a767b6SJan Kara 	/* Running from downconvert thread? Postpone quota processing to wq */
734e3a767b6SJan Kara 	if (current == osb->dc_task) {
735e3a767b6SJan Kara 		/*
736e3a767b6SJan Kara 		 * Grab our own reference to dquot and queue it for delayed
737e3a767b6SJan Kara 		 * dropping.  Quota code rechecks after calling
738e3a767b6SJan Kara 		 * ->release_dquot() and won't free dquot structure.
739e3a767b6SJan Kara 		 */
740e3a767b6SJan Kara 		dqgrab(dquot);
741e3a767b6SJan Kara 		/* First entry on list -> queue work */
742e3a767b6SJan Kara 		if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list))
74335ddf78eSjiangyiwen 			queue_work(osb->ocfs2_wq, &osb->dquot_drop_work);
744e3a767b6SJan Kara 		goto out;
745e3a767b6SJan Kara 	}
7469e33d69fSJan Kara 	status = ocfs2_lock_global_qf(oinfo, 1);
7479e33d69fSJan Kara 	if (status < 0)
7489e33d69fSJan Kara 		goto out;
7499e33d69fSJan Kara 	handle = ocfs2_start_trans(osb,
7504c376dcaSEric W. Biederman 		ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type));
7519e33d69fSJan Kara 	if (IS_ERR(handle)) {
7529e33d69fSJan Kara 		status = PTR_ERR(handle);
7539e33d69fSJan Kara 		mlog_errno(status);
7549e33d69fSJan Kara 		goto out_ilock;
7559e33d69fSJan Kara 	}
756fb8dd8d7SJan Kara 
757fb8dd8d7SJan Kara 	status = ocfs2_global_release_dquot(dquot);
758fb8dd8d7SJan Kara 	if (status < 0) {
759fb8dd8d7SJan Kara 		mlog_errno(status);
760fb8dd8d7SJan Kara 		goto out_trans;
761fb8dd8d7SJan Kara 	}
762fb8dd8d7SJan Kara 	status = ocfs2_local_release_dquot(handle, dquot);
763fb8dd8d7SJan Kara 	/*
764fb8dd8d7SJan Kara 	 * If we fail here, we cannot do much as global structure is
765fb8dd8d7SJan Kara 	 * already released. So just complain...
766fb8dd8d7SJan Kara 	 */
767fb8dd8d7SJan Kara 	if (status < 0)
768fb8dd8d7SJan Kara 		mlog_errno(status);
76915c34a76SJan Kara 	/*
77015c34a76SJan Kara 	 * Clear dq_off so that we search for the structure in quota file next
77115c34a76SJan Kara 	 * time we acquire it. The structure might be deleted and reallocated
77215c34a76SJan Kara 	 * elsewhere by another node while our dquot structure is on freelist.
77315c34a76SJan Kara 	 */
77415c34a76SJan Kara 	dquot->dq_off = 0;
775fb8dd8d7SJan Kara 	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
776fb8dd8d7SJan Kara out_trans:
7779e33d69fSJan Kara 	ocfs2_commit_trans(osb, handle);
7789e33d69fSJan Kara out_ilock:
7799e33d69fSJan Kara 	ocfs2_unlock_global_qf(oinfo, 1);
7809e33d69fSJan Kara out:
781fb8dd8d7SJan Kara 	mutex_unlock(&dquot->dq_lock);
782c1e8d35eSTao Ma 	if (status)
783c1e8d35eSTao Ma 		mlog_errno(status);
7849e33d69fSJan Kara 	return status;
7859e33d69fSJan Kara }
7869e33d69fSJan Kara 
787fb8dd8d7SJan Kara /*
788fb8dd8d7SJan Kara  * Read global dquot structure from disk or create it if it does
789fb8dd8d7SJan Kara  * not exist. Also update use count of the global structure and
790fb8dd8d7SJan Kara  * create structure in node-local quota file.
791fb8dd8d7SJan Kara  */
ocfs2_acquire_dquot(struct dquot * dquot)7929e33d69fSJan Kara static int ocfs2_acquire_dquot(struct dquot *dquot)
7939e33d69fSJan Kara {
794fb8dd8d7SJan Kara 	int status = 0, err;
795fb8dd8d7SJan Kara 	int ex = 0;
796fb8dd8d7SJan Kara 	struct super_block *sb = dquot->dq_sb;
797fb8dd8d7SJan Kara 	struct ocfs2_super *osb = OCFS2_SB(sb);
7984c376dcaSEric W. Biederman 	int type = dquot->dq_id.type;
799fb8dd8d7SJan Kara 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
800fb8dd8d7SJan Kara 	struct inode *gqinode = info->dqi_gqinode;
801fb8dd8d7SJan Kara 	int need_alloc = ocfs2_global_qinit_alloc(sb, type);
802fb8dd8d7SJan Kara 	handle_t *handle;
8039e33d69fSJan Kara 
8044c376dcaSEric W. Biederman 	trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id),
8054c376dcaSEric W. Biederman 				  type);
806fb8dd8d7SJan Kara 	mutex_lock(&dquot->dq_lock);
807fb8dd8d7SJan Kara 	/*
808fb8dd8d7SJan Kara 	 * We need an exclusive lock, because we're going to update use count
809fb8dd8d7SJan Kara 	 * and instantiate possibly new dquot structure
810fb8dd8d7SJan Kara 	 */
811fb8dd8d7SJan Kara 	status = ocfs2_lock_global_qf(info, 1);
8129e33d69fSJan Kara 	if (status < 0)
8139e33d69fSJan Kara 		goto out;
814fb8dd8d7SJan Kara 	status = ocfs2_qinfo_lock(info, 0);
815fb8dd8d7SJan Kara 	if (status < 0)
816fb8dd8d7SJan Kara 		goto out_dq;
81715c34a76SJan Kara 	/*
81815c34a76SJan Kara 	 * We always want to read dquot structure from disk because we don't
81915c34a76SJan Kara 	 * know what happened with it while it was on freelist.
82015c34a76SJan Kara 	 */
821fb8dd8d7SJan Kara 	status = qtree_read_dquot(&info->dqi_gi, dquot);
822fb8dd8d7SJan Kara 	ocfs2_qinfo_unlock(info, 0);
823fb8dd8d7SJan Kara 	if (status < 0)
824fb8dd8d7SJan Kara 		goto out_dq;
825fb8dd8d7SJan Kara 
826fb8dd8d7SJan Kara 	OCFS2_DQUOT(dquot)->dq_use_count++;
827fb8dd8d7SJan Kara 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
828fb8dd8d7SJan Kara 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
829fb8dd8d7SJan Kara 	if (!dquot->dq_off) {	/* No real quota entry? */
830fb8dd8d7SJan Kara 		ex = 1;
831fb8dd8d7SJan Kara 		/*
832fb8dd8d7SJan Kara 		 * Add blocks to quota file before we start a transaction since
833fb8dd8d7SJan Kara 		 * locking allocators ranks above a transaction start
834fb8dd8d7SJan Kara 		 */
835fb8dd8d7SJan Kara 		WARN_ON(journal_current_handle());
8365693486bSJoel Becker 		status = ocfs2_extend_no_holes(gqinode, NULL,
837f17c20ddSJunxiao Bi 			i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits),
838f17c20ddSJunxiao Bi 			i_size_read(gqinode));
839fb8dd8d7SJan Kara 		if (status < 0)
840fb8dd8d7SJan Kara 			goto out_dq;
841fb8dd8d7SJan Kara 	}
842fb8dd8d7SJan Kara 
843fb8dd8d7SJan Kara 	handle = ocfs2_start_trans(osb,
844fb8dd8d7SJan Kara 				   ocfs2_calc_global_qinit_credits(sb, type));
845fb8dd8d7SJan Kara 	if (IS_ERR(handle)) {
846fb8dd8d7SJan Kara 		status = PTR_ERR(handle);
847fb8dd8d7SJan Kara 		goto out_dq;
848fb8dd8d7SJan Kara 	}
849fb8dd8d7SJan Kara 	status = ocfs2_qinfo_lock(info, ex);
850fb8dd8d7SJan Kara 	if (status < 0)
851fb8dd8d7SJan Kara 		goto out_trans;
852fb8dd8d7SJan Kara 	status = qtree_write_dquot(&info->dqi_gi, dquot);
853fb8dd8d7SJan Kara 	if (ex && info_dirty(sb_dqinfo(sb, type))) {
854fb8dd8d7SJan Kara 		err = __ocfs2_global_write_info(sb, type);
855fb8dd8d7SJan Kara 		if (!status)
856fb8dd8d7SJan Kara 			status = err;
857fb8dd8d7SJan Kara 	}
858fb8dd8d7SJan Kara 	ocfs2_qinfo_unlock(info, ex);
859fb8dd8d7SJan Kara out_trans:
860fb8dd8d7SJan Kara 	ocfs2_commit_trans(osb, handle);
861fb8dd8d7SJan Kara out_dq:
862fb8dd8d7SJan Kara 	ocfs2_unlock_global_qf(info, 1);
863fb8dd8d7SJan Kara 	if (status < 0)
864fb8dd8d7SJan Kara 		goto out;
865fb8dd8d7SJan Kara 
866fb8dd8d7SJan Kara 	status = ocfs2_create_local_dquot(dquot);
867fb8dd8d7SJan Kara 	if (status < 0)
868fb8dd8d7SJan Kara 		goto out;
869fb8dd8d7SJan Kara 	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
8709e33d69fSJan Kara out:
871fb8dd8d7SJan Kara 	mutex_unlock(&dquot->dq_lock);
872c1e8d35eSTao Ma 	if (status)
873c1e8d35eSTao Ma 		mlog_errno(status);
8749e33d69fSJan Kara 	return status;
8759e33d69fSJan Kara }
8769e33d69fSJan Kara 
ocfs2_get_next_id(struct super_block * sb,struct kqid * qid)8775a9530e4SJan Kara static int ocfs2_get_next_id(struct super_block *sb, struct kqid *qid)
8785a9530e4SJan Kara {
8795a9530e4SJan Kara 	int type = qid->type;
8805a9530e4SJan Kara 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
8815a9530e4SJan Kara 	int status = 0;
8825a9530e4SJan Kara 
8835a9530e4SJan Kara 	trace_ocfs2_get_next_id(from_kqid(&init_user_ns, *qid), type);
8848f9e8f5fSJan Kara 	if (!sb_has_quota_loaded(sb, type)) {
8858f9e8f5fSJan Kara 		status = -ESRCH;
8868f9e8f5fSJan Kara 		goto out;
8878f9e8f5fSJan Kara 	}
8885a9530e4SJan Kara 	status = ocfs2_lock_global_qf(info, 0);
8895a9530e4SJan Kara 	if (status < 0)
8905a9530e4SJan Kara 		goto out;
8915a9530e4SJan Kara 	status = ocfs2_qinfo_lock(info, 0);
8925a9530e4SJan Kara 	if (status < 0)
8935a9530e4SJan Kara 		goto out_global;
8945a9530e4SJan Kara 	status = qtree_get_next_id(&info->dqi_gi, qid);
8955a9530e4SJan Kara 	ocfs2_qinfo_unlock(info, 0);
8965a9530e4SJan Kara out_global:
8975a9530e4SJan Kara 	ocfs2_unlock_global_qf(info, 0);
8985a9530e4SJan Kara out:
8998f9e8f5fSJan Kara 	/*
9008f9e8f5fSJan Kara 	 * Avoid logging ENOENT since it just means there isn't next ID and
9018f9e8f5fSJan Kara 	 * ESRCH which means quota isn't enabled for the filesystem.
9028f9e8f5fSJan Kara 	 */
9038f9e8f5fSJan Kara 	if (status && status != -ENOENT && status != -ESRCH)
9045a9530e4SJan Kara 		mlog_errno(status);
9055a9530e4SJan Kara 	return status;
9065a9530e4SJan Kara }
9075a9530e4SJan Kara 
ocfs2_mark_dquot_dirty(struct dquot * dquot)9089e33d69fSJan Kara static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
9099e33d69fSJan Kara {
9109e33d69fSJan Kara 	unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
9119e33d69fSJan Kara 			     (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
9129e33d69fSJan Kara 			     (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
9139e33d69fSJan Kara 			     (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
9149e33d69fSJan Kara 			     (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
9159e33d69fSJan Kara 			     (1 << (DQ_LASTSET_B + QIF_ITIME_B));
9169e33d69fSJan Kara 	int sync = 0;
9179e33d69fSJan Kara 	int status;
9189e33d69fSJan Kara 	struct super_block *sb = dquot->dq_sb;
9194c376dcaSEric W. Biederman 	int type = dquot->dq_id.type;
9209e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
9219e33d69fSJan Kara 	handle_t *handle;
9229e33d69fSJan Kara 	struct ocfs2_super *osb = OCFS2_SB(sb);
9239e33d69fSJan Kara 
9244c376dcaSEric W. Biederman 	trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id),
9254c376dcaSEric W. Biederman 				     type);
9269e33d69fSJan Kara 
9279e33d69fSJan Kara 	/* In case user set some limits, sync dquot immediately to global
9289e33d69fSJan Kara 	 * quota file so that information propagates quicker */
9297b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
9309e33d69fSJan Kara 	if (dquot->dq_flags & mask)
9319e33d69fSJan Kara 		sync = 1;
9327b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
933f8afead7SJan Kara 	/* This is a slight hack but we can't afford getting global quota
934f8afead7SJan Kara 	 * lock if we already have a transaction started. */
935f8afead7SJan Kara 	if (!sync || journal_current_handle()) {
9369e33d69fSJan Kara 		status = ocfs2_write_dquot(dquot);
9379e33d69fSJan Kara 		goto out;
9389e33d69fSJan Kara 	}
9399e33d69fSJan Kara 	status = ocfs2_lock_global_qf(oinfo, 1);
9409e33d69fSJan Kara 	if (status < 0)
9419e33d69fSJan Kara 		goto out;
9429e33d69fSJan Kara 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
9439e33d69fSJan Kara 	if (IS_ERR(handle)) {
9449e33d69fSJan Kara 		status = PTR_ERR(handle);
9459e33d69fSJan Kara 		mlog_errno(status);
9469e33d69fSJan Kara 		goto out_ilock;
9479e33d69fSJan Kara 	}
948bc8230eeSJan Kara 	down_write(&sb_dqopt(sb)->dqio_sem);
9499e33d69fSJan Kara 	status = ocfs2_sync_dquot(dquot);
9509e33d69fSJan Kara 	if (status < 0) {
9519e33d69fSJan Kara 		mlog_errno(status);
952741e1289SJan Kara 		goto out_dlock;
9539e33d69fSJan Kara 	}
9549e33d69fSJan Kara 	/* Now write updated local dquot structure */
955741e1289SJan Kara 	status = ocfs2_local_write_dquot(dquot);
956741e1289SJan Kara out_dlock:
957bc8230eeSJan Kara 	up_write(&sb_dqopt(sb)->dqio_sem);
9589e33d69fSJan Kara 	ocfs2_commit_trans(osb, handle);
9599e33d69fSJan Kara out_ilock:
9609e33d69fSJan Kara 	ocfs2_unlock_global_qf(oinfo, 1);
9619e33d69fSJan Kara out:
962c1e8d35eSTao Ma 	if (status)
963c1e8d35eSTao Ma 		mlog_errno(status);
9649e33d69fSJan Kara 	return status;
9659e33d69fSJan Kara }
9669e33d69fSJan Kara 
9679e33d69fSJan Kara /* This should happen only after set_dqinfo(). */
ocfs2_write_info(struct super_block * sb,int type)9689e33d69fSJan Kara static int ocfs2_write_info(struct super_block *sb, int type)
9699e33d69fSJan Kara {
9709e33d69fSJan Kara 	handle_t *handle;
9719e33d69fSJan Kara 	int status = 0;
9729e33d69fSJan Kara 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
9739e33d69fSJan Kara 
9749e33d69fSJan Kara 	status = ocfs2_lock_global_qf(oinfo, 1);
9759e33d69fSJan Kara 	if (status < 0)
9769e33d69fSJan Kara 		goto out;
9779e33d69fSJan Kara 	handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
9789e33d69fSJan Kara 	if (IS_ERR(handle)) {
9799e33d69fSJan Kara 		status = PTR_ERR(handle);
9809e33d69fSJan Kara 		mlog_errno(status);
9819e33d69fSJan Kara 		goto out_ilock;
9829e33d69fSJan Kara 	}
9839e33d69fSJan Kara 	status = dquot_commit_info(sb, type);
9849e33d69fSJan Kara 	ocfs2_commit_trans(OCFS2_SB(sb), handle);
9859e33d69fSJan Kara out_ilock:
9869e33d69fSJan Kara 	ocfs2_unlock_global_qf(oinfo, 1);
9879e33d69fSJan Kara out:
988c1e8d35eSTao Ma 	if (status)
989c1e8d35eSTao Ma 		mlog_errno(status);
9909e33d69fSJan Kara 	return status;
9919e33d69fSJan Kara }
9929e33d69fSJan Kara 
ocfs2_alloc_dquot(struct super_block * sb,int type)9939e33d69fSJan Kara static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
9949e33d69fSJan Kara {
9959e33d69fSJan Kara 	struct ocfs2_dquot *dquot =
9969e33d69fSJan Kara 				kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
9979e33d69fSJan Kara 
9989e33d69fSJan Kara 	if (!dquot)
9999e33d69fSJan Kara 		return NULL;
10009e33d69fSJan Kara 	return &dquot->dq_dquot;
10019e33d69fSJan Kara }
10029e33d69fSJan Kara 
ocfs2_destroy_dquot(struct dquot * dquot)10039e33d69fSJan Kara static void ocfs2_destroy_dquot(struct dquot *dquot)
10049e33d69fSJan Kara {
10059e33d69fSJan Kara 	kmem_cache_free(ocfs2_dquot_cachep, dquot);
10069e33d69fSJan Kara }
10079e33d69fSJan Kara 
100861e225dcSAlexey Dobriyan const struct dquot_operations ocfs2_quota_operations = {
1009741e1289SJan Kara 	/* We never make dquot dirty so .write_dquot is never called */
10109e33d69fSJan Kara 	.acquire_dquot	= ocfs2_acquire_dquot,
10119e33d69fSJan Kara 	.release_dquot	= ocfs2_release_dquot,
10129e33d69fSJan Kara 	.mark_dirty	= ocfs2_mark_dquot_dirty,
10139e33d69fSJan Kara 	.write_info	= ocfs2_write_info,
10149e33d69fSJan Kara 	.alloc_dquot	= ocfs2_alloc_dquot,
10159e33d69fSJan Kara 	.destroy_dquot	= ocfs2_destroy_dquot,
10165a9530e4SJan Kara 	.get_next_id	= ocfs2_get_next_id,
10179e33d69fSJan Kara };
1018