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