Blame view

fs/ocfs2/quota_global.c 28.5 KB
9e33d69f5   Jan Kara   ocfs2: Implementa...
1
2
3
  /*
   *  Implementation of operations over global quota file
   */
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
4
  #include <linux/spinlock.h>
9e33d69f5   Jan Kara   ocfs2: Implementa...
5
  #include <linux/fs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
6
  #include <linux/slab.h>
9e33d69f5   Jan Kara   ocfs2: Implementa...
7
8
9
  #include <linux/quota.h>
  #include <linux/quotaops.h>
  #include <linux/dqblk_qtree.h>
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
10
11
12
  #include <linux/jiffies.h>
  #include <linux/writeback.h>
  #include <linux/workqueue.h>
e3a767b60   Jan Kara   ocfs2: implement ...
13
  #include <linux/llist.h>
9e33d69f5   Jan Kara   ocfs2: Implementa...
14

9e33d69f5   Jan Kara   ocfs2: Implementa...
15
16
17
18
19
  #include <cluster/masklog.h>
  
  #include "ocfs2_fs.h"
  #include "ocfs2.h"
  #include "alloc.h"
d6b32bbb3   Joel Becker   ocfs2: block read...
20
  #include "blockcheck.h"
9e33d69f5   Jan Kara   ocfs2: Implementa...
21
22
23
24
25
26
  #include "inode.h"
  #include "journal.h"
  #include "file.h"
  #include "sysfile.h"
  #include "dlmglue.h"
  #include "uptodate.h"
ada508274   Jan Kara   ocfs2: Handle quo...
27
  #include "super.h"
f64dd44eb   Jan Kara   ocfs2: Do not map...
28
  #include "buffer_head_io.h"
9e33d69f5   Jan Kara   ocfs2: Implementa...
29
  #include "quota.h"
1db986a83   Tao Ma   ocfs2: Remove mas...
30
  #include "ocfs2_trace.h"
9e33d69f5   Jan Kara   ocfs2: Implementa...
31

fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /*
   * Locking of quotas with OCFS2 is rather complex. Here are rules that
   * should be obeyed by all the functions:
   * - any write of quota structure (either to local or global file) is protected
   *   by dqio_mutex or dquot->dq_lock.
   * - any modification of global quota file holds inode cluster lock, i_mutex,
   *   and ip_alloc_sem of the global quota file (achieved by
   *   ocfs2_lock_global_qf). It also has to hold qinfo_lock.
   * - an allocation of new blocks for local quota file is protected by
   *   its ip_alloc_sem
   *
   * A rough sketch of locking dependencies (lf = local file, gf = global file):
   * Normal filesystem operation:
   *   start_trans -> dqio_mutex -> write to lf
   * Syncing of local and global file:
   *   ocfs2_lock_global_qf -> start_trans -> dqio_mutex -> qinfo_lock ->
   *     write to gf
   *						       -> write to lf
   * Acquire dquot for the first time:
   *   dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf
   *				     -> alloc space for gf
   *				     -> start_trans -> qinfo_lock -> write to gf
   *	     -> ip_alloc_sem of lf -> alloc space for lf
   *	     -> write to lf
   * Release last reference to dquot:
   *   dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf
   *	     -> write to lf
   * Note that all the above operations also hold the inode cluster lock of lf.
   * Recovery:
   *   inode cluster lock of recovered lf
   *     -> read bitmaps -> ip_alloc_sem of lf
   *     -> ocfs2_lock_global_qf -> start_trans -> dqio_mutex -> qinfo_lock ->
   *        write to gf
   */
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
66
  static void qsync_work_fn(struct work_struct *work);
9e33d69f5   Jan Kara   ocfs2: Implementa...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
  {
  	struct ocfs2_global_disk_dqblk *d = dp;
  	struct mem_dqblk *m = &dquot->dq_dqb;
  
  	/* Update from disk only entries not set by the admin */
  	if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
  		m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  		m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  	}
  	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
  		m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  	if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
  		m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
  		m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
  	}
  	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
  		m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  	if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
  		m->dqb_btime = le64_to_cpu(d->dqb_btime);
  	if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
  		m->dqb_itime = le64_to_cpu(d->dqb_itime);
  	OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
  }
  
  static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
  {
  	struct ocfs2_global_disk_dqblk *d = dp;
  	struct mem_dqblk *m = &dquot->dq_dqb;
4c376dcae   Eric W. Biederman   userns: Convert s...
96
  	d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
9e33d69f5   Jan Kara   ocfs2: Implementa...
97
98
99
100
101
102
103
104
105
  	d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
  	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  	d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
  	d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
  	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  	d->dqb_btime = cpu_to_le64(m->dqb_btime);
  	d->dqb_itime = cpu_to_le64(m->dqb_itime);
7669f54c5   Jan Kara   ocfs2: Zero out p...
106
  	d->dqb_pad1 = d->dqb_pad2 = 0;
9e33d69f5   Jan Kara   ocfs2: Implementa...
107
108
109
110
111
112
  }
  
  static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
  {
  	struct ocfs2_global_disk_dqblk *d = dp;
  	struct ocfs2_mem_dqinfo *oinfo =
4c376dcae   Eric W. Biederman   userns: Convert s...
113
  			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
9e33d69f5   Jan Kara   ocfs2: Implementa...
114
115
116
  
  	if (qtree_entry_unused(&oinfo->dqi_gi, dp))
  		return 0;
4c376dcae   Eric W. Biederman   userns: Convert s...
117
118
119
120
  
  	return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  				le32_to_cpu(d->dqb_id)),
  		      dquot->dq_id);
9e33d69f5   Jan Kara   ocfs2: Implementa...
121
122
123
124
125
126
127
  }
  
  struct qtree_fmt_operations ocfs2_global_ops = {
  	.mem2disk_dqblk = ocfs2_global_mem2diskdqb,
  	.disk2mem_dqblk = ocfs2_global_disk2memdqb,
  	.is_id = ocfs2_global_is_id,
  };
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
128
  int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh)
684ef2783   Joel Becker   ocfs2: Add a vali...
129
  {
d6b32bbb3   Joel Becker   ocfs2: block read...
130
131
  	struct ocfs2_disk_dqtrailer *dqt =
  		ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
684ef2783   Joel Becker   ocfs2: Add a vali...
132

1db986a83   Tao Ma   ocfs2: Remove mas...
133
  	trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr);
684ef2783   Joel Becker   ocfs2: Add a vali...
134

d6b32bbb3   Joel Becker   ocfs2: block read...
135
136
137
138
139
140
141
142
  	BUG_ON(!buffer_uptodate(bh));
  
  	/*
  	 * If the ecc fails, we return the error but otherwise
  	 * leave the filesystem running.  We know any error is
  	 * local to this block.
  	 */
  	return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
684ef2783   Joel Becker   ocfs2: Add a vali...
143
  }
f64dd44eb   Jan Kara   ocfs2: Do not map...
144
145
146
147
148
149
150
151
152
153
154
155
  int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
  				struct buffer_head **bhp)
  {
  	int rc;
  
  	*bhp = NULL;
  	rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
  			       ocfs2_validate_quota_block);
  	if (rc)
  		mlog_errno(rc);
  	return rc;
  }
9e33d69f5   Jan Kara   ocfs2: Implementa...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  /* Read data from global quotafile - avoid pagecache and such because we cannot
   * afford acquiring the locks... We use quota cluster lock to serialize
   * operations. Caller is responsible for acquiring it. */
  ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
  			 size_t len, loff_t off)
  {
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  	struct inode *gqinode = oinfo->dqi_gqinode;
  	loff_t i_size = i_size_read(gqinode);
  	int offset = off & (sb->s_blocksize - 1);
  	sector_t blk = off >> sb->s_blocksize_bits;
  	int err = 0;
  	struct buffer_head *bh;
  	size_t toread, tocopy;
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
170
  	u64 pblock = 0, pcount = 0;
9e33d69f5   Jan Kara   ocfs2: Implementa...
171
172
173
174
175
176
177
  
  	if (off > i_size)
  		return 0;
  	if (off + len > i_size)
  		len = i_size - off;
  	toread = len;
  	while (toread > 0) {
dad7d975e   Mark Fasheh   ocfs2: use min_t ...
178
  		tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
179
180
181
182
183
184
185
186
187
188
189
  		if (!pcount) {
  			err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock,
  							  &pcount, NULL);
  			if (err) {
  				mlog_errno(err);
  				return err;
  			}
  		} else {
  			pcount--;
  			pblock++;
  		}
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
190
  		bh = NULL;
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
191
  		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
192
  		if (err) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  			mlog_errno(err);
  			return err;
  		}
  		memcpy(data, bh->b_data + offset, tocopy);
  		brelse(bh);
  		offset = 0;
  		toread -= tocopy;
  		data += tocopy;
  		blk++;
  	}
  	return len;
  }
  
  /* Write to quotafile (we know the transaction is already started and has
   * enough credits) */
  ssize_t ocfs2_quota_write(struct super_block *sb, int type,
  			  const char *data, size_t len, loff_t off)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct inode *gqinode = oinfo->dqi_gqinode;
  	int offset = off & (sb->s_blocksize - 1);
  	sector_t blk = off >> sb->s_blocksize_bits;
af09e51b6   Jan Kara   ocfs2: Fix oops w...
216
  	int err = 0, new = 0, ja_type;
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
217
  	struct buffer_head *bh = NULL;
9e33d69f5   Jan Kara   ocfs2: Implementa...
218
  	handle_t *handle = journal_current_handle();
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
219
  	u64 pblock, pcount;
9e33d69f5   Jan Kara   ocfs2: Implementa...
220
221
222
223
224
225
226
227
228
229
230
231
  
  	if (!handle) {
  		mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
  		     "because transaction was not started.
  ",
  		     (unsigned long long)off, (unsigned long long)len);
  		return -EIO;
  	}
  	if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
  		WARN_ON(1);
  		len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
  	}
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
232
  	if (i_size_read(gqinode) < off + len) {
b57ac2c43   Jan Kara   ocfs2: Make globa...
233
234
  		loff_t rounded_end =
  				ocfs2_align_bytes_to_blocks(sb, off + len);
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
235
  		/* Space is already allocated in ocfs2_acquire_dquot() */
9e33d69f5   Jan Kara   ocfs2: Implementa...
236
237
  		err = ocfs2_simple_size_update(gqinode,
  					       oinfo->dqi_gqi_bh,
b57ac2c43   Jan Kara   ocfs2: Make globa...
238
  					       rounded_end);
9e33d69f5   Jan Kara   ocfs2: Implementa...
239
240
241
242
  		if (err < 0)
  			goto out;
  		new = 1;
  	}
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
243
244
245
246
247
  	err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL);
  	if (err) {
  		mlog_errno(err);
  		goto out;
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
248
249
250
  	/* Not rewriting whole block? */
  	if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
  	    !new) {
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
251
  		err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
af09e51b6   Jan Kara   ocfs2: Fix oops w...
252
  		ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
9e33d69f5   Jan Kara   ocfs2: Implementa...
253
  	} else {
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
254
255
256
  		bh = sb_getblk(sb, pblock);
  		if (!bh)
  			err = -ENOMEM;
af09e51b6   Jan Kara   ocfs2: Fix oops w...
257
  		ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
9e33d69f5   Jan Kara   ocfs2: Implementa...
258
  	}
af09e51b6   Jan Kara   ocfs2: Fix oops w...
259
260
  	if (err) {
  		mlog_errno(err);
e9956fae7   Tao Ma   ocfs2/quota: Rele...
261
  		goto out;
9e33d69f5   Jan Kara   ocfs2: Implementa...
262
263
264
265
266
267
  	}
  	lock_buffer(bh);
  	if (new)
  		memset(bh->b_data, 0, sb->s_blocksize);
  	memcpy(bh->b_data + offset, data, len);
  	flush_dcache_page(bh->b_page);
af09e51b6   Jan Kara   ocfs2: Fix oops w...
268
  	set_buffer_uptodate(bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
269
  	unlock_buffer(bh);
8cb471e8f   Joel Becker   ocfs2: Take the i...
270
  	ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
0cf2f7632   Joel Becker   ocfs2: Pass struc...
271
272
  	err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
  				      ja_type);
af09e51b6   Jan Kara   ocfs2: Fix oops w...
273
274
275
276
  	if (err < 0) {
  		brelse(bh);
  		goto out;
  	}
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
277
  	ocfs2_journal_dirty(handle, bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
278
  	brelse(bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
279
280
  out:
  	if (err) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
281
282
283
284
285
  		mlog_errno(err);
  		return err;
  	}
  	gqinode->i_version++;
  	ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  	return len;
  }
  
  int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
  {
  	int status;
  	struct buffer_head *bh = NULL;
  
  	status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
  	if (status < 0)
  		return status;
  	spin_lock(&dq_data_lock);
  	if (!oinfo->dqi_gqi_count++)
  		oinfo->dqi_gqi_bh = bh;
  	else
  		WARN_ON(bh != oinfo->dqi_gqi_bh);
  	spin_unlock(&dq_data_lock);
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
303
304
305
306
307
308
  	if (ex) {
  		mutex_lock(&oinfo->dqi_gqinode->i_mutex);
  		down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
  	} else {
  		down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
309
310
311
312
313
  	return 0;
  }
  
  void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
  {
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
314
315
316
317
318
319
  	if (ex) {
  		up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
  		mutex_unlock(&oinfo->dqi_gqinode->i_mutex);
  	} else {
  		up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
320
321
322
323
324
325
326
327
328
329
330
331
  	ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
  	brelse(oinfo->dqi_gqi_bh);
  	spin_lock(&dq_data_lock);
  	if (!--oinfo->dqi_gqi_count)
  		oinfo->dqi_gqi_bh = NULL;
  	spin_unlock(&dq_data_lock);
  }
  
  /* Read information header from global quota file */
  int ocfs2_global_read_info(struct super_block *sb, int type)
  {
  	struct inode *gqinode = NULL;
52362810b   Jan Kara   ocfs2: Don't use ...
332
333
  	unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
  					      GROUP_QUOTA_SYSTEM_INODE };
9e33d69f5   Jan Kara   ocfs2: Implementa...
334
335
336
  	struct ocfs2_global_disk_dqinfo dinfo;
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
337
  	u64 pcount;
9e33d69f5   Jan Kara   ocfs2: Implementa...
338
  	int status;
9e33d69f5   Jan Kara   ocfs2: Implementa...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
  	/* Read global header */
  	gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
  			OCFS2_INVALID_SLOT);
  	if (!gqinode) {
  		mlog(ML_ERROR, "failed to get global quota inode (type=%d)
  ",
  			type);
  		status = -EINVAL;
  		goto out_err;
  	}
  	oinfo->dqi_gi.dqi_sb = sb;
  	oinfo->dqi_gi.dqi_type = type;
  	ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
  	oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
  	oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
  	oinfo->dqi_gqi_bh = NULL;
  	oinfo->dqi_gqi_count = 0;
  	oinfo->dqi_gqinode = gqinode;
  	status = ocfs2_lock_global_qf(oinfo, 0);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_err;
  	}
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
362
363
364
365
366
367
368
369
370
  
  	status = ocfs2_extent_map_get_blocks(gqinode, 0, &oinfo->dqi_giblk,
  					     &pcount, NULL);
  	if (status < 0)
  		goto out_unlock;
  
  	status = ocfs2_qinfo_lock(oinfo, 0);
  	if (status < 0)
  		goto out_unlock;
9e33d69f5   Jan Kara   ocfs2: Implementa...
371
372
373
  	status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
  				      sizeof(struct ocfs2_global_disk_dqinfo),
  				      OCFS2_GLOBAL_INFO_OFF);
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
374
  	ocfs2_qinfo_unlock(oinfo, 0);
9e33d69f5   Jan Kara   ocfs2: Implementa...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  	ocfs2_unlock_global_qf(oinfo, 0);
  	if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
  		mlog(ML_ERROR, "Cannot read global quota info (%d).
  ",
  		     status);
  		if (status >= 0)
  			status = -EIO;
  		mlog_errno(status);
  		goto out_err;
  	}
  	info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  	info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  	oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
  	oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  	oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  	oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  	oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
  	oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
  						OCFS2_QBLK_RESERVED_SPACE;
  	oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
395
  	INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
316873c95   Tejun Heo   ocfs2: use system...
396
397
  	schedule_delayed_work(&oinfo->dqi_sync_work,
  			      msecs_to_jiffies(oinfo->dqi_syncms));
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
398

9e33d69f5   Jan Kara   ocfs2: Implementa...
399
  out_err:
9e33d69f5   Jan Kara   ocfs2: Implementa...
400
  	return status;
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
401
402
403
404
  out_unlock:
  	ocfs2_unlock_global_qf(oinfo, 0);
  	mlog_errno(status);
  	goto out_err;
9e33d69f5   Jan Kara   ocfs2: Implementa...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  }
  
  /* Write information to global quota file. Expects exlusive lock on quota
   * file inode and quota info */
  static int __ocfs2_global_write_info(struct super_block *sb, int type)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct ocfs2_global_disk_dqinfo dinfo;
  	ssize_t size;
  
  	spin_lock(&dq_data_lock);
  	info->dqi_flags &= ~DQF_INFO_DIRTY;
  	dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  	dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  	spin_unlock(&dq_data_lock);
  	dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
  	dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
  	dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
  	dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
  	size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  				     sizeof(struct ocfs2_global_disk_dqinfo),
  				     OCFS2_GLOBAL_INFO_OFF);
  	if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
  		mlog(ML_ERROR, "Cannot write global quota info structure
  ");
  		if (size >= 0)
  			size = -EIO;
  		return size;
  	}
  	return 0;
  }
  
  int ocfs2_global_write_info(struct super_block *sb, int type)
  {
  	int err;
  	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
  
  	err = ocfs2_qinfo_lock(info, 1);
  	if (err < 0)
  		return err;
  	err = __ocfs2_global_write_info(sb, type);
  	ocfs2_qinfo_unlock(info, 1);
  	return err;
  }
b409d7a0a   Jan Kara   ocfs2: Fix possib...
450
451
452
453
454
455
456
457
458
459
460
461
462
  static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
  {
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  
  	/*
  	 * We may need to allocate tree blocks and a leaf block but not the
  	 * root block
  	 */
  	return oinfo->dqi_gi.dqi_qtree_depth;
  }
  
  static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
  {
832d09cf1   Jan Kara   ocfs2: Fix estima...
463
464
  	/* We modify all the allocated blocks, tree root, info block and
  	 * the inode */
b409d7a0a   Jan Kara   ocfs2: Fix possib...
465
  	return (ocfs2_global_qinit_alloc(sb, type) + 2) *
832d09cf1   Jan Kara   ocfs2: Fix estima...
466
  			OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1;
b409d7a0a   Jan Kara   ocfs2: Fix possib...
467
  }
9e33d69f5   Jan Kara   ocfs2: Implementa...
468
469
470
471
472
473
474
  /* Sync local information about quota modifications with global quota file.
   * Caller must have started the transaction and obtained exclusive lock for
   * global quota file inode */
  int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
  {
  	int err, err2;
  	struct super_block *sb = dquot->dq_sb;
4c376dcae   Eric W. Biederman   userns: Convert s...
475
  	int type = dquot->dq_id.type;
9e33d69f5   Jan Kara   ocfs2: Implementa...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
  	struct ocfs2_global_disk_dqblk dqblk;
  	s64 spacechange, inodechange;
  	time_t olditime, oldbtime;
  
  	err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
  				   sizeof(struct ocfs2_global_disk_dqblk),
  				   dquot->dq_off);
  	if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
  		if (err >= 0) {
  			mlog(ML_ERROR, "Short read from global quota file "
  				       "(%u read)
  ", err);
  			err = -EIO;
  		}
  		goto out;
  	}
  
  	/* Update space and inode usage. Get also other information from
  	 * global quota file so that we don't overwrite any changes there.
  	 * We are */
  	spin_lock(&dq_data_lock);
  	spacechange = dquot->dq_dqb.dqb_curspace -
  					OCFS2_DQUOT(dquot)->dq_origspace;
  	inodechange = dquot->dq_dqb.dqb_curinodes -
  					OCFS2_DQUOT(dquot)->dq_originodes;
  	olditime = dquot->dq_dqb.dqb_itime;
  	oldbtime = dquot->dq_dqb.dqb_btime;
  	ocfs2_global_disk2memdqb(dquot, &dqblk);
4c376dcae   Eric W. Biederman   userns: Convert s...
505
506
  	trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id),
  			       dquot->dq_dqb.dqb_curspace,
1db986a83   Tao Ma   ocfs2: Remove mas...
507
508
509
  			       (long long)spacechange,
  			       dquot->dq_dqb.dqb_curinodes,
  			       (long long)inodechange);
9e33d69f5   Jan Kara   ocfs2: Implementa...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
  		dquot->dq_dqb.dqb_curspace += spacechange;
  	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
  		dquot->dq_dqb.dqb_curinodes += inodechange;
  	/* Set properly space grace time... */
  	if (dquot->dq_dqb.dqb_bsoftlimit &&
  	    dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
  		if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
  		    oldbtime > 0) {
  			if (dquot->dq_dqb.dqb_btime > 0)
  				dquot->dq_dqb.dqb_btime =
  					min(dquot->dq_dqb.dqb_btime, oldbtime);
  			else
  				dquot->dq_dqb.dqb_btime = oldbtime;
  		}
  	} else {
  		dquot->dq_dqb.dqb_btime = 0;
  		clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  	}
  	/* Set properly inode grace time... */
  	if (dquot->dq_dqb.dqb_isoftlimit &&
  	    dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
  		if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
  		    olditime > 0) {
  			if (dquot->dq_dqb.dqb_itime > 0)
  				dquot->dq_dqb.dqb_itime =
  					min(dquot->dq_dqb.dqb_itime, olditime);
  			else
  				dquot->dq_dqb.dqb_itime = olditime;
  		}
  	} else {
  		dquot->dq_dqb.dqb_itime = 0;
  		clear_bit(DQ_INODES_B, &dquot->dq_flags);
  	}
  	/* All information is properly updated, clear the flags */
  	__clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  	__clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  	__clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  	__clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  	__clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  	__clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
  	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
  	spin_unlock(&dq_data_lock);
  	err = ocfs2_qinfo_lock(info, freeing);
  	if (err < 0) {
25985edce   Lucas De Marchi   Fix common misspe...
556
  		mlog(ML_ERROR, "Failed to lock quota info, losing quota write"
4c376dcae   Eric W. Biederman   userns: Convert s...
557
558
559
  			       " (type=%d, id=%u)
  ", dquot->dq_id.type,
  			       (unsigned)from_kqid(&init_user_ns, dquot->dq_id));
9e33d69f5   Jan Kara   ocfs2: Implementa...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  		goto out;
  	}
  	if (freeing)
  		OCFS2_DQUOT(dquot)->dq_use_count--;
  	err = qtree_write_dquot(&info->dqi_gi, dquot);
  	if (err < 0)
  		goto out_qlock;
  	if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
  		err = qtree_release_dquot(&info->dqi_gi, dquot);
  		if (info_dirty(sb_dqinfo(sb, type))) {
  			err2 = __ocfs2_global_write_info(sb, type);
  			if (!err)
  				err = err2;
  		}
  	}
  out_qlock:
  	ocfs2_qinfo_unlock(info, freeing);
  out:
  	if (err < 0)
  		mlog_errno(err);
  	return err;
  }
  
  /*
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
584
585
586
587
588
589
590
591
592
   *  Functions for periodic syncing of dquots with global file
   */
  static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
  {
  	handle_t *handle;
  	struct super_block *sb = dquot->dq_sb;
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  	struct ocfs2_super *osb = OCFS2_SB(sb);
  	int status = 0;
4c376dcae   Eric W. Biederman   userns: Convert s...
593
594
  	trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id),
  				      dquot->dq_id.type,
1db986a83   Tao Ma   ocfs2: Remove mas...
595
  				      type, sb->s_id);
4c376dcae   Eric W. Biederman   userns: Convert s...
596
  	if (type != dquot->dq_id.type)
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
597
598
599
600
601
602
603
604
605
606
607
608
609
  		goto out;
  	status = ocfs2_lock_global_qf(oinfo, 1);
  	if (status < 0)
  		goto out;
  
  	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out_ilock;
  	}
  	mutex_lock(&sb_dqopt(sb)->dqio_mutex);
  	status = ocfs2_sync_dquot(dquot);
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
610
611
612
  	if (status < 0)
  		mlog_errno(status);
  	/* We have to write local structure as well... */
741e12893   Jan Kara   ocfs2: Fix NULL p...
613
  	status = ocfs2_local_write_dquot(dquot);
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
614
615
  	if (status < 0)
  		mlog_errno(status);
741e12893   Jan Kara   ocfs2: Fix NULL p...
616
  	mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
617
618
619
620
  	ocfs2_commit_trans(osb, handle);
  out_ilock:
  	ocfs2_unlock_global_qf(oinfo, 1);
  out:
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
621
622
623
624
625
626
627
628
629
630
631
  	return status;
  }
  
  static void qsync_work_fn(struct work_struct *work)
  {
  	struct ocfs2_mem_dqinfo *oinfo = container_of(work,
  						      struct ocfs2_mem_dqinfo,
  						      dqi_sync_work.work);
  	struct super_block *sb = oinfo->dqi_gqinode->i_sb;
  
  	dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
316873c95   Tejun Heo   ocfs2: use system...
632
633
  	schedule_delayed_work(&oinfo->dqi_sync_work,
  			      msecs_to_jiffies(oinfo->dqi_syncms));
171bf93ce   Mark Fasheh   ocfs2: Periodic q...
634
635
636
  }
  
  /*
9e33d69f5   Jan Kara   ocfs2: Implementa...
637
638
639
640
641
642
643
644
   *  Wrappers for generic quota functions
   */
  
  static int ocfs2_write_dquot(struct dquot *dquot)
  {
  	handle_t *handle;
  	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
  	int status = 0;
4c376dcae   Eric W. Biederman   userns: Convert s...
645
646
  	trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id),
  				dquot->dq_id.type);
9e33d69f5   Jan Kara   ocfs2: Implementa...
647
648
649
650
651
652
653
  
  	handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out;
  	}
741e12893   Jan Kara   ocfs2: Fix NULL p...
654
655
656
  	mutex_lock(&sb_dqopt(dquot->dq_sb)->dqio_mutex);
  	status = ocfs2_local_write_dquot(dquot);
  	mutex_unlock(&sb_dqopt(dquot->dq_sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
657
658
  	ocfs2_commit_trans(osb, handle);
  out:
9e33d69f5   Jan Kara   ocfs2: Implementa...
659
660
  	return status;
  }
b409d7a0a   Jan Kara   ocfs2: Fix possib...
661
  static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
9e33d69f5   Jan Kara   ocfs2: Implementa...
662
  {
b409d7a0a   Jan Kara   ocfs2: Fix possib...
663
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0584974a7   Jan Kara   ocfs2: Define cre...
664
665
666
667
668
  	/*
  	 * We modify tree, leaf block, global info, local chunk header,
  	 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
  	 * accounts for inode update
  	 */
b409d7a0a   Jan Kara   ocfs2: Fix possib...
669
670
  	return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
  	       OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
0584974a7   Jan Kara   ocfs2: Define cre...
671
  	       OCFS2_QINFO_WRITE_CREDITS +
0584974a7   Jan Kara   ocfs2: Define cre...
672
  	       OCFS2_INODE_UPDATE_CREDITS;
9e33d69f5   Jan Kara   ocfs2: Implementa...
673
  }
e3a767b60   Jan Kara   ocfs2: implement ...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
  void ocfs2_drop_dquot_refs(struct work_struct *work)
  {
  	struct ocfs2_super *osb = container_of(work, struct ocfs2_super,
  					       dquot_drop_work);
  	struct llist_node *list;
  	struct ocfs2_dquot *odquot, *next_odquot;
  
  	list = llist_del_all(&osb->dquot_drop_list);
  	llist_for_each_entry_safe(odquot, next_odquot, list, list) {
  		/* Drop the reference we acquired in ocfs2_dquot_release() */
  		dqput(&odquot->dq_dquot);
  	}
  }
  
  /*
   * Called when the last reference to dquot is dropped. If we are called from
   * downconvert thread, we cannot do all the handling here because grabbing
   * quota lock could deadlock (the node holding the quota lock could need some
   * other cluster lock to proceed but with blocked downconvert thread we cannot
   * release any lock).
   */
9e33d69f5   Jan Kara   ocfs2: Implementa...
695
696
697
698
  static int ocfs2_release_dquot(struct dquot *dquot)
  {
  	handle_t *handle;
  	struct ocfs2_mem_dqinfo *oinfo =
4c376dcae   Eric W. Biederman   userns: Convert s...
699
  			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
9e33d69f5   Jan Kara   ocfs2: Implementa...
700
701
  	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
  	int status = 0;
4c376dcae   Eric W. Biederman   userns: Convert s...
702
703
  	trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id),
  				  dquot->dq_id.type);
9e33d69f5   Jan Kara   ocfs2: Implementa...
704

fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
705
706
707
708
  	mutex_lock(&dquot->dq_lock);
  	/* Check whether we are not racing with some other dqget() */
  	if (atomic_read(&dquot->dq_count) > 1)
  		goto out;
e3a767b60   Jan Kara   ocfs2: implement ...
709
710
711
712
713
714
715
716
717
718
719
720
721
  	/* Running from downconvert thread? Postpone quota processing to wq */
  	if (current == osb->dc_task) {
  		/*
  		 * Grab our own reference to dquot and queue it for delayed
  		 * dropping.  Quota code rechecks after calling
  		 * ->release_dquot() and won't free dquot structure.
  		 */
  		dqgrab(dquot);
  		/* First entry on list -> queue work */
  		if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list))
  			queue_work(ocfs2_wq, &osb->dquot_drop_work);
  		goto out;
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
722
723
724
725
  	status = ocfs2_lock_global_qf(oinfo, 1);
  	if (status < 0)
  		goto out;
  	handle = ocfs2_start_trans(osb,
4c376dcae   Eric W. Biederman   userns: Convert s...
726
  		ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type));
9e33d69f5   Jan Kara   ocfs2: Implementa...
727
728
729
730
731
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out_ilock;
  	}
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
732
733
734
735
736
737
738
739
740
741
742
743
744
  
  	status = ocfs2_global_release_dquot(dquot);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	status = ocfs2_local_release_dquot(handle, dquot);
  	/*
  	 * If we fail here, we cannot do much as global structure is
  	 * already released. So just complain...
  	 */
  	if (status < 0)
  		mlog_errno(status);
15c34a760   Jan Kara   ocfs2: fix quota ...
745
746
747
748
749
750
  	/*
  	 * Clear dq_off so that we search for the structure in quota file next
  	 * time we acquire it. The structure might be deleted and reallocated
  	 * elsewhere by another node while our dquot structure is on freelist.
  	 */
  	dquot->dq_off = 0;
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
751
752
  	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  out_trans:
9e33d69f5   Jan Kara   ocfs2: Implementa...
753
754
755
756
  	ocfs2_commit_trans(osb, handle);
  out_ilock:
  	ocfs2_unlock_global_qf(oinfo, 1);
  out:
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
757
  	mutex_unlock(&dquot->dq_lock);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
758
759
  	if (status)
  		mlog_errno(status);
9e33d69f5   Jan Kara   ocfs2: Implementa...
760
761
  	return status;
  }
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
762
763
764
765
766
  /*
   * Read global dquot structure from disk or create it if it does
   * not exist. Also update use count of the global structure and
   * create structure in node-local quota file.
   */
9e33d69f5   Jan Kara   ocfs2: Implementa...
767
768
  static int ocfs2_acquire_dquot(struct dquot *dquot)
  {
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
769
770
771
772
  	int status = 0, err;
  	int ex = 0;
  	struct super_block *sb = dquot->dq_sb;
  	struct ocfs2_super *osb = OCFS2_SB(sb);
4c376dcae   Eric W. Biederman   userns: Convert s...
773
  	int type = dquot->dq_id.type;
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
774
775
776
777
  	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
  	struct inode *gqinode = info->dqi_gqinode;
  	int need_alloc = ocfs2_global_qinit_alloc(sb, type);
  	handle_t *handle;
9e33d69f5   Jan Kara   ocfs2: Implementa...
778

4c376dcae   Eric W. Biederman   userns: Convert s...
779
780
  	trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id),
  				  type);
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
781
782
783
784
785
786
  	mutex_lock(&dquot->dq_lock);
  	/*
  	 * We need an exclusive lock, because we're going to update use count
  	 * and instantiate possibly new dquot structure
  	 */
  	status = ocfs2_lock_global_qf(info, 1);
9e33d69f5   Jan Kara   ocfs2: Implementa...
787
788
  	if (status < 0)
  		goto out;
15c34a760   Jan Kara   ocfs2: fix quota ...
789
790
791
792
793
794
795
796
797
798
799
  	status = ocfs2_qinfo_lock(info, 0);
  	if (status < 0)
  		goto out_dq;
  	/*
  	 * We always want to read dquot structure from disk because we don't
  	 * know what happened with it while it was on freelist.
  	 */
  	status = qtree_read_dquot(&info->dqi_gi, dquot);
  	ocfs2_qinfo_unlock(info, 0);
  	if (status < 0)
  		goto out_dq;
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
800
801
802
803
804
805
806
807
808
809
810
  
  	OCFS2_DQUOT(dquot)->dq_use_count++;
  	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
  	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
  	if (!dquot->dq_off) {	/* No real quota entry? */
  		ex = 1;
  		/*
  		 * Add blocks to quota file before we start a transaction since
  		 * locking allocators ranks above a transaction start
  		 */
  		WARN_ON(journal_current_handle());
5693486ba   Joel Becker   ocfs2: Zero the t...
811
  		status = ocfs2_extend_no_holes(gqinode, NULL,
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
812
813
  			i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits),
  			i_size_read(gqinode));
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  		if (status < 0)
  			goto out_dq;
  	}
  
  	handle = ocfs2_start_trans(osb,
  				   ocfs2_calc_global_qinit_credits(sb, type));
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		goto out_dq;
  	}
  	status = ocfs2_qinfo_lock(info, ex);
  	if (status < 0)
  		goto out_trans;
  	status = qtree_write_dquot(&info->dqi_gi, dquot);
  	if (ex && info_dirty(sb_dqinfo(sb, type))) {
  		err = __ocfs2_global_write_info(sb, type);
  		if (!status)
  			status = err;
  	}
  	ocfs2_qinfo_unlock(info, ex);
  out_trans:
  	ocfs2_commit_trans(osb, handle);
  out_dq:
  	ocfs2_unlock_global_qf(info, 1);
  	if (status < 0)
  		goto out;
  
  	status = ocfs2_create_local_dquot(dquot);
  	if (status < 0)
  		goto out;
  	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
9e33d69f5   Jan Kara   ocfs2: Implementa...
845
  out:
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
846
  	mutex_unlock(&dquot->dq_lock);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
847
848
  	if (status)
  		mlog_errno(status);
9e33d69f5   Jan Kara   ocfs2: Implementa...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  	return status;
  }
  
  static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
  {
  	unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
  			     (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
  			     (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
  			     (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
  			     (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
  			     (1 << (DQ_LASTSET_B + QIF_ITIME_B));
  	int sync = 0;
  	int status;
  	struct super_block *sb = dquot->dq_sb;
4c376dcae   Eric W. Biederman   userns: Convert s...
863
  	int type = dquot->dq_id.type;
9e33d69f5   Jan Kara   ocfs2: Implementa...
864
865
866
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  	handle_t *handle;
  	struct ocfs2_super *osb = OCFS2_SB(sb);
4c376dcae   Eric W. Biederman   userns: Convert s...
867
868
  	trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id),
  				     type);
9e33d69f5   Jan Kara   ocfs2: Implementa...
869
870
871
872
873
874
875
  
  	/* In case user set some limits, sync dquot immediately to global
  	 * quota file so that information propagates quicker */
  	spin_lock(&dq_data_lock);
  	if (dquot->dq_flags & mask)
  		sync = 1;
  	spin_unlock(&dq_data_lock);
f8afead71   Jan Kara   ocfs2: Fix possib...
876
877
878
  	/* This is a slight hack but we can't afford getting global quota
  	 * lock if we already have a transaction started. */
  	if (!sync || journal_current_handle()) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
879
880
881
882
883
884
885
886
887
888
889
890
  		status = ocfs2_write_dquot(dquot);
  		goto out;
  	}
  	status = ocfs2_lock_global_qf(oinfo, 1);
  	if (status < 0)
  		goto out;
  	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out_ilock;
  	}
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
891
  	mutex_lock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
892
893
894
  	status = ocfs2_sync_dquot(dquot);
  	if (status < 0) {
  		mlog_errno(status);
741e12893   Jan Kara   ocfs2: Fix NULL p...
895
  		goto out_dlock;
9e33d69f5   Jan Kara   ocfs2: Implementa...
896
897
  	}
  	/* Now write updated local dquot structure */
741e12893   Jan Kara   ocfs2: Fix NULL p...
898
899
900
  	status = ocfs2_local_write_dquot(dquot);
  out_dlock:
  	mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
901
902
903
904
  	ocfs2_commit_trans(osb, handle);
  out_ilock:
  	ocfs2_unlock_global_qf(oinfo, 1);
  out:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
905
906
  	if (status)
  		mlog_errno(status);
9e33d69f5   Jan Kara   ocfs2: Implementa...
907
908
909
910
911
912
913
914
915
  	return status;
  }
  
  /* This should happen only after set_dqinfo(). */
  static int ocfs2_write_info(struct super_block *sb, int type)
  {
  	handle_t *handle;
  	int status = 0;
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
9e33d69f5   Jan Kara   ocfs2: Implementa...
916
917
918
919
920
921
922
923
924
925
926
927
928
929
  	status = ocfs2_lock_global_qf(oinfo, 1);
  	if (status < 0)
  		goto out;
  	handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out_ilock;
  	}
  	status = dquot_commit_info(sb, type);
  	ocfs2_commit_trans(OCFS2_SB(sb), handle);
  out_ilock:
  	ocfs2_unlock_global_qf(oinfo, 1);
  out:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
930
931
  	if (status)
  		mlog_errno(status);
9e33d69f5   Jan Kara   ocfs2: Implementa...
932
933
  	return status;
  }
9e33d69f5   Jan Kara   ocfs2: Implementa...
934
935
936
937
938
939
940
941
942
943
944
945
946
947
  static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
  {
  	struct ocfs2_dquot *dquot =
  				kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
  
  	if (!dquot)
  		return NULL;
  	return &dquot->dq_dquot;
  }
  
  static void ocfs2_destroy_dquot(struct dquot *dquot)
  {
  	kmem_cache_free(ocfs2_dquot_cachep, dquot);
  }
61e225dc3   Alexey Dobriyan   const: make struc...
948
  const struct dquot_operations ocfs2_quota_operations = {
741e12893   Jan Kara   ocfs2: Fix NULL p...
949
  	/* We never make dquot dirty so .write_dquot is never called */
9e33d69f5   Jan Kara   ocfs2: Implementa...
950
951
952
953
954
955
956
  	.acquire_dquot	= ocfs2_acquire_dquot,
  	.release_dquot	= ocfs2_release_dquot,
  	.mark_dirty	= ocfs2_mark_dquot_dirty,
  	.write_info	= ocfs2_write_info,
  	.alloc_dquot	= ocfs2_alloc_dquot,
  	.destroy_dquot	= ocfs2_destroy_dquot,
  };