xref: /openbmc/linux/fs/ocfs2/quota_global.c (revision f9bd71f2)
1 /*
2  *  Implementation of operations over global quota file
3  */
4 #include <linux/spinlock.h>
5 #include <linux/fs.h>
6 #include <linux/quota.h>
7 #include <linux/quotaops.h>
8 #include <linux/dqblk_qtree.h>
9 #include <linux/jiffies.h>
10 #include <linux/writeback.h>
11 #include <linux/workqueue.h>
12 
13 #define MLOG_MASK_PREFIX ML_QUOTA
14 #include <cluster/masklog.h>
15 
16 #include "ocfs2_fs.h"
17 #include "ocfs2.h"
18 #include "alloc.h"
19 #include "blockcheck.h"
20 #include "inode.h"
21 #include "journal.h"
22 #include "file.h"
23 #include "sysfile.h"
24 #include "dlmglue.h"
25 #include "uptodate.h"
26 #include "quota.h"
27 
28 static struct workqueue_struct *ocfs2_quota_wq = NULL;
29 
30 static void qsync_work_fn(struct work_struct *work);
31 
32 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
33 {
34 	struct ocfs2_global_disk_dqblk *d = dp;
35 	struct mem_dqblk *m = &dquot->dq_dqb;
36 
37 	/* Update from disk only entries not set by the admin */
38 	if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
39 		m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
40 		m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
41 	}
42 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
43 		m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
44 	if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
45 		m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
46 		m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
47 	}
48 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
49 		m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
50 	if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
51 		m->dqb_btime = le64_to_cpu(d->dqb_btime);
52 	if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
53 		m->dqb_itime = le64_to_cpu(d->dqb_itime);
54 	OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
55 }
56 
57 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
58 {
59 	struct ocfs2_global_disk_dqblk *d = dp;
60 	struct mem_dqblk *m = &dquot->dq_dqb;
61 
62 	d->dqb_id = cpu_to_le32(dquot->dq_id);
63 	d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
64 	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
65 	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
66 	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
67 	d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
68 	d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
69 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
70 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
71 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
72 	d->dqb_pad1 = d->dqb_pad2 = 0;
73 }
74 
75 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
76 {
77 	struct ocfs2_global_disk_dqblk *d = dp;
78 	struct ocfs2_mem_dqinfo *oinfo =
79 			sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
80 
81 	if (qtree_entry_unused(&oinfo->dqi_gi, dp))
82 		return 0;
83 	return le32_to_cpu(d->dqb_id) == dquot->dq_id;
84 }
85 
86 struct qtree_fmt_operations ocfs2_global_ops = {
87 	.mem2disk_dqblk = ocfs2_global_mem2diskdqb,
88 	.disk2mem_dqblk = ocfs2_global_disk2memdqb,
89 	.is_id = ocfs2_global_is_id,
90 };
91 
92 static int ocfs2_validate_quota_block(struct super_block *sb,
93 				      struct buffer_head *bh)
94 {
95 	struct ocfs2_disk_dqtrailer *dqt =
96 		ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
97 
98 	mlog(0, "Validating quota block %llu\n",
99 	     (unsigned long long)bh->b_blocknr);
100 
101 	BUG_ON(!buffer_uptodate(bh));
102 
103 	/*
104 	 * If the ecc fails, we return the error but otherwise
105 	 * leave the filesystem running.  We know any error is
106 	 * local to this block.
107 	 */
108 	return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
109 }
110 
111 int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
112 			   struct buffer_head **bh)
113 {
114 	int rc = 0;
115 	struct buffer_head *tmp = *bh;
116 
117 	rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
118 				    ocfs2_validate_quota_block);
119 	if (rc)
120 		mlog_errno(rc);
121 
122 	/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
123 	if (!rc && !*bh)
124 		*bh = tmp;
125 
126 	return rc;
127 }
128 
129 static int ocfs2_get_quota_block(struct inode *inode, int block,
130 				 struct buffer_head **bh)
131 {
132 	u64 pblock, pcount;
133 	int err;
134 
135 	down_read(&OCFS2_I(inode)->ip_alloc_sem);
136 	err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
137 	up_read(&OCFS2_I(inode)->ip_alloc_sem);
138 	if (err) {
139 		mlog_errno(err);
140 		return err;
141 	}
142 	*bh = sb_getblk(inode->i_sb, pblock);
143 	if (!*bh) {
144 		err = -EIO;
145 		mlog_errno(err);
146 	}
147 	return err;;
148 }
149 
150 /* Read data from global quotafile - avoid pagecache and such because we cannot
151  * afford acquiring the locks... We use quota cluster lock to serialize
152  * operations. Caller is responsible for acquiring it. */
153 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
154 			 size_t len, loff_t off)
155 {
156 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
157 	struct inode *gqinode = oinfo->dqi_gqinode;
158 	loff_t i_size = i_size_read(gqinode);
159 	int offset = off & (sb->s_blocksize - 1);
160 	sector_t blk = off >> sb->s_blocksize_bits;
161 	int err = 0;
162 	struct buffer_head *bh;
163 	size_t toread, tocopy;
164 
165 	if (off > i_size)
166 		return 0;
167 	if (off + len > i_size)
168 		len = i_size - off;
169 	toread = len;
170 	while (toread > 0) {
171 		tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
172 		bh = NULL;
173 		err = ocfs2_read_quota_block(gqinode, blk, &bh);
174 		if (err) {
175 			mlog_errno(err);
176 			return err;
177 		}
178 		memcpy(data, bh->b_data + offset, tocopy);
179 		brelse(bh);
180 		offset = 0;
181 		toread -= tocopy;
182 		data += tocopy;
183 		blk++;
184 	}
185 	return len;
186 }
187 
188 /* Write to quotafile (we know the transaction is already started and has
189  * enough credits) */
190 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
191 			  const char *data, size_t len, loff_t off)
192 {
193 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
194 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
195 	struct inode *gqinode = oinfo->dqi_gqinode;
196 	int offset = off & (sb->s_blocksize - 1);
197 	sector_t blk = off >> sb->s_blocksize_bits;
198 	int err = 0, new = 0, ja_type;
199 	struct buffer_head *bh = NULL;
200 	handle_t *handle = journal_current_handle();
201 
202 	if (!handle) {
203 		mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
204 		     "because transaction was not started.\n",
205 		     (unsigned long long)off, (unsigned long long)len);
206 		return -EIO;
207 	}
208 	if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
209 		WARN_ON(1);
210 		len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
211 	}
212 
213 	mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
214 	if (gqinode->i_size < off + len) {
215 		loff_t rounded_end =
216 				ocfs2_align_bytes_to_blocks(sb, off + len);
217 
218 		/* Space is already allocated in ocfs2_global_read_dquot() */
219 		err = ocfs2_simple_size_update(gqinode,
220 					       oinfo->dqi_gqi_bh,
221 					       rounded_end);
222 		if (err < 0)
223 			goto out;
224 		new = 1;
225 	}
226 	/* Not rewriting whole block? */
227 	if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
228 	    !new) {
229 		err = ocfs2_read_quota_block(gqinode, blk, &bh);
230 		ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
231 	} else {
232 		err = ocfs2_get_quota_block(gqinode, blk, &bh);
233 		ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
234 	}
235 	if (err) {
236 		mlog_errno(err);
237 		goto out;
238 	}
239 	lock_buffer(bh);
240 	if (new)
241 		memset(bh->b_data, 0, sb->s_blocksize);
242 	memcpy(bh->b_data + offset, data, len);
243 	flush_dcache_page(bh->b_page);
244 	set_buffer_uptodate(bh);
245 	unlock_buffer(bh);
246 	ocfs2_set_buffer_uptodate(gqinode, bh);
247 	err = ocfs2_journal_access_dq(handle, gqinode, bh, ja_type);
248 	if (err < 0) {
249 		brelse(bh);
250 		goto out;
251 	}
252 	err = ocfs2_journal_dirty(handle, bh);
253 	brelse(bh);
254 	if (err < 0)
255 		goto out;
256 out:
257 	if (err) {
258 		mutex_unlock(&gqinode->i_mutex);
259 		mlog_errno(err);
260 		return err;
261 	}
262 	gqinode->i_version++;
263 	ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
264 	mutex_unlock(&gqinode->i_mutex);
265 	return len;
266 }
267 
268 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
269 {
270 	int status;
271 	struct buffer_head *bh = NULL;
272 
273 	status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
274 	if (status < 0)
275 		return status;
276 	spin_lock(&dq_data_lock);
277 	if (!oinfo->dqi_gqi_count++)
278 		oinfo->dqi_gqi_bh = bh;
279 	else
280 		WARN_ON(bh != oinfo->dqi_gqi_bh);
281 	spin_unlock(&dq_data_lock);
282 	return 0;
283 }
284 
285 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
286 {
287 	ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
288 	brelse(oinfo->dqi_gqi_bh);
289 	spin_lock(&dq_data_lock);
290 	if (!--oinfo->dqi_gqi_count)
291 		oinfo->dqi_gqi_bh = NULL;
292 	spin_unlock(&dq_data_lock);
293 }
294 
295 /* Read information header from global quota file */
296 int ocfs2_global_read_info(struct super_block *sb, int type)
297 {
298 	struct inode *gqinode = NULL;
299 	unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
300 					GROUP_QUOTA_SYSTEM_INODE };
301 	struct ocfs2_global_disk_dqinfo dinfo;
302 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
303 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
304 	int status;
305 
306 	mlog_entry_void();
307 
308 	/* Read global header */
309 	gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
310 			OCFS2_INVALID_SLOT);
311 	if (!gqinode) {
312 		mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
313 			type);
314 		status = -EINVAL;
315 		goto out_err;
316 	}
317 	oinfo->dqi_gi.dqi_sb = sb;
318 	oinfo->dqi_gi.dqi_type = type;
319 	ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
320 	oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
321 	oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
322 	oinfo->dqi_gqi_bh = NULL;
323 	oinfo->dqi_gqi_count = 0;
324 	oinfo->dqi_gqinode = gqinode;
325 	status = ocfs2_lock_global_qf(oinfo, 0);
326 	if (status < 0) {
327 		mlog_errno(status);
328 		goto out_err;
329 	}
330 	status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
331 				      sizeof(struct ocfs2_global_disk_dqinfo),
332 				      OCFS2_GLOBAL_INFO_OFF);
333 	ocfs2_unlock_global_qf(oinfo, 0);
334 	if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
335 		mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
336 		     status);
337 		if (status >= 0)
338 			status = -EIO;
339 		mlog_errno(status);
340 		goto out_err;
341 	}
342 	info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
343 	info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
344 	oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
345 	oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
346 	oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
347 	oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
348 	oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
349 	oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
350 						OCFS2_QBLK_RESERVED_SPACE;
351 	oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
352 	INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
353 	queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
354 			   msecs_to_jiffies(oinfo->dqi_syncms));
355 
356 out_err:
357 	mlog_exit(status);
358 	return status;
359 }
360 
361 /* Write information to global quota file. Expects exlusive lock on quota
362  * file inode and quota info */
363 static int __ocfs2_global_write_info(struct super_block *sb, int type)
364 {
365 	struct mem_dqinfo *info = sb_dqinfo(sb, type);
366 	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
367 	struct ocfs2_global_disk_dqinfo dinfo;
368 	ssize_t size;
369 
370 	spin_lock(&dq_data_lock);
371 	info->dqi_flags &= ~DQF_INFO_DIRTY;
372 	dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
373 	dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
374 	spin_unlock(&dq_data_lock);
375 	dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
376 	dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
377 	dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
378 	dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
379 	size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
380 				     sizeof(struct ocfs2_global_disk_dqinfo),
381 				     OCFS2_GLOBAL_INFO_OFF);
382 	if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
383 		mlog(ML_ERROR, "Cannot write global quota info structure\n");
384 		if (size >= 0)
385 			size = -EIO;
386 		return size;
387 	}
388 	return 0;
389 }
390 
391 int ocfs2_global_write_info(struct super_block *sb, int type)
392 {
393 	int err;
394 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
395 
396 	err = ocfs2_qinfo_lock(info, 1);
397 	if (err < 0)
398 		return err;
399 	err = __ocfs2_global_write_info(sb, type);
400 	ocfs2_qinfo_unlock(info, 1);
401 	return err;
402 }
403 
404 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
405 {
406 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
407 
408 	/*
409 	 * We may need to allocate tree blocks and a leaf block but not the
410 	 * root block
411 	 */
412 	return oinfo->dqi_gi.dqi_qtree_depth;
413 }
414 
415 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
416 {
417 	/* We modify all the allocated blocks, tree root, and info block */
418 	return (ocfs2_global_qinit_alloc(sb, type) + 2) *
419 			OCFS2_QUOTA_BLOCK_UPDATE_CREDITS;
420 }
421 
422 /* Read in information from global quota file and acquire a reference to it.
423  * dquot_acquire() has already started the transaction and locked quota file */
424 int ocfs2_global_read_dquot(struct dquot *dquot)
425 {
426 	int err, err2, ex = 0;
427 	struct super_block *sb = dquot->dq_sb;
428 	int type = dquot->dq_type;
429 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
430 	struct ocfs2_super *osb = OCFS2_SB(sb);
431 	struct inode *gqinode = info->dqi_gqinode;
432 	int need_alloc = ocfs2_global_qinit_alloc(sb, type);
433 	handle_t *handle = NULL;
434 
435 	err = ocfs2_qinfo_lock(info, 0);
436 	if (err < 0)
437 		goto out;
438 	err = qtree_read_dquot(&info->dqi_gi, dquot);
439 	if (err < 0)
440 		goto out_qlock;
441 	OCFS2_DQUOT(dquot)->dq_use_count++;
442 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
443 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
444 	ocfs2_qinfo_unlock(info, 0);
445 
446 	if (!dquot->dq_off) {	/* No real quota entry? */
447 		ex = 1;
448 		/*
449 		 * Add blocks to quota file before we start a transaction since
450 		 * locking allocators ranks above a transaction start
451 		 */
452 		WARN_ON(journal_current_handle());
453 		down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
454 		err = ocfs2_extend_no_holes(gqinode,
455 			gqinode->i_size + (need_alloc << sb->s_blocksize_bits),
456 			gqinode->i_size);
457 		up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
458 		if (err < 0)
459 			goto out;
460 	}
461 
462 	handle = ocfs2_start_trans(osb,
463 				   ocfs2_calc_global_qinit_credits(sb, type));
464 	if (IS_ERR(handle)) {
465 		err = PTR_ERR(handle);
466 		goto out;
467 	}
468 	err = ocfs2_qinfo_lock(info, ex);
469 	if (err < 0)
470 		goto out_trans;
471 	err = qtree_write_dquot(&info->dqi_gi, dquot);
472 	if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
473 		err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
474 		if (!err)
475 			err = err2;
476 	}
477 out_qlock:
478 	if (ex)
479 		ocfs2_qinfo_unlock(info, 1);
480 	else
481 		ocfs2_qinfo_unlock(info, 0);
482 out_trans:
483 	if (handle)
484 		ocfs2_commit_trans(osb, handle);
485 out:
486 	if (err < 0)
487 		mlog_errno(err);
488 	return err;
489 }
490 
491 /* Sync local information about quota modifications with global quota file.
492  * Caller must have started the transaction and obtained exclusive lock for
493  * global quota file inode */
494 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
495 {
496 	int err, err2;
497 	struct super_block *sb = dquot->dq_sb;
498 	int type = dquot->dq_type;
499 	struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
500 	struct ocfs2_global_disk_dqblk dqblk;
501 	s64 spacechange, inodechange;
502 	time_t olditime, oldbtime;
503 
504 	err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
505 				   sizeof(struct ocfs2_global_disk_dqblk),
506 				   dquot->dq_off);
507 	if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
508 		if (err >= 0) {
509 			mlog(ML_ERROR, "Short read from global quota file "
510 				       "(%u read)\n", err);
511 			err = -EIO;
512 		}
513 		goto out;
514 	}
515 
516 	/* Update space and inode usage. Get also other information from
517 	 * global quota file so that we don't overwrite any changes there.
518 	 * We are */
519 	spin_lock(&dq_data_lock);
520 	spacechange = dquot->dq_dqb.dqb_curspace -
521 					OCFS2_DQUOT(dquot)->dq_origspace;
522 	inodechange = dquot->dq_dqb.dqb_curinodes -
523 					OCFS2_DQUOT(dquot)->dq_originodes;
524 	olditime = dquot->dq_dqb.dqb_itime;
525 	oldbtime = dquot->dq_dqb.dqb_btime;
526 	ocfs2_global_disk2memdqb(dquot, &dqblk);
527 	mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
528 	     dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
529 	     dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
530 	if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
531 		dquot->dq_dqb.dqb_curspace += spacechange;
532 	if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
533 		dquot->dq_dqb.dqb_curinodes += inodechange;
534 	/* Set properly space grace time... */
535 	if (dquot->dq_dqb.dqb_bsoftlimit &&
536 	    dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
537 		if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
538 		    oldbtime > 0) {
539 			if (dquot->dq_dqb.dqb_btime > 0)
540 				dquot->dq_dqb.dqb_btime =
541 					min(dquot->dq_dqb.dqb_btime, oldbtime);
542 			else
543 				dquot->dq_dqb.dqb_btime = oldbtime;
544 		}
545 	} else {
546 		dquot->dq_dqb.dqb_btime = 0;
547 		clear_bit(DQ_BLKS_B, &dquot->dq_flags);
548 	}
549 	/* Set properly inode grace time... */
550 	if (dquot->dq_dqb.dqb_isoftlimit &&
551 	    dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
552 		if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
553 		    olditime > 0) {
554 			if (dquot->dq_dqb.dqb_itime > 0)
555 				dquot->dq_dqb.dqb_itime =
556 					min(dquot->dq_dqb.dqb_itime, olditime);
557 			else
558 				dquot->dq_dqb.dqb_itime = olditime;
559 		}
560 	} else {
561 		dquot->dq_dqb.dqb_itime = 0;
562 		clear_bit(DQ_INODES_B, &dquot->dq_flags);
563 	}
564 	/* All information is properly updated, clear the flags */
565 	__clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
566 	__clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
567 	__clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
568 	__clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
569 	__clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
570 	__clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
571 	OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
572 	OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
573 	spin_unlock(&dq_data_lock);
574 	err = ocfs2_qinfo_lock(info, freeing);
575 	if (err < 0) {
576 		mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
577 			       " (type=%d, id=%u)\n", dquot->dq_type,
578 			       (unsigned)dquot->dq_id);
579 		goto out;
580 	}
581 	if (freeing)
582 		OCFS2_DQUOT(dquot)->dq_use_count--;
583 	err = qtree_write_dquot(&info->dqi_gi, dquot);
584 	if (err < 0)
585 		goto out_qlock;
586 	if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
587 		err = qtree_release_dquot(&info->dqi_gi, dquot);
588 		if (info_dirty(sb_dqinfo(sb, type))) {
589 			err2 = __ocfs2_global_write_info(sb, type);
590 			if (!err)
591 				err = err2;
592 		}
593 	}
594 out_qlock:
595 	ocfs2_qinfo_unlock(info, freeing);
596 out:
597 	if (err < 0)
598 		mlog_errno(err);
599 	return err;
600 }
601 
602 /*
603  *  Functions for periodic syncing of dquots with global file
604  */
605 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
606 {
607 	handle_t *handle;
608 	struct super_block *sb = dquot->dq_sb;
609 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
610 	struct ocfs2_super *osb = OCFS2_SB(sb);
611 	int status = 0;
612 
613 	mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
614 		   dquot->dq_type, type, sb->s_id);
615 	if (type != dquot->dq_type)
616 		goto out;
617 	status = ocfs2_lock_global_qf(oinfo, 1);
618 	if (status < 0)
619 		goto out;
620 
621 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
622 	if (IS_ERR(handle)) {
623 		status = PTR_ERR(handle);
624 		mlog_errno(status);
625 		goto out_ilock;
626 	}
627 	mutex_lock(&sb_dqopt(sb)->dqio_mutex);
628 	status = ocfs2_sync_dquot(dquot);
629 	mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
630 	if (status < 0)
631 		mlog_errno(status);
632 	/* We have to write local structure as well... */
633 	dquot_mark_dquot_dirty(dquot);
634 	status = dquot_commit(dquot);
635 	if (status < 0)
636 		mlog_errno(status);
637 	ocfs2_commit_trans(osb, handle);
638 out_ilock:
639 	ocfs2_unlock_global_qf(oinfo, 1);
640 out:
641 	mlog_exit(status);
642 	return status;
643 }
644 
645 static void qsync_work_fn(struct work_struct *work)
646 {
647 	struct ocfs2_mem_dqinfo *oinfo = container_of(work,
648 						      struct ocfs2_mem_dqinfo,
649 						      dqi_sync_work.work);
650 	struct super_block *sb = oinfo->dqi_gqinode->i_sb;
651 
652 	dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
653 	queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
654 			   msecs_to_jiffies(oinfo->dqi_syncms));
655 }
656 
657 /*
658  *  Wrappers for generic quota functions
659  */
660 
661 static int ocfs2_write_dquot(struct dquot *dquot)
662 {
663 	handle_t *handle;
664 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
665 	int status = 0;
666 
667 	mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
668 
669 	handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
670 	if (IS_ERR(handle)) {
671 		status = PTR_ERR(handle);
672 		mlog_errno(status);
673 		goto out;
674 	}
675 	status = dquot_commit(dquot);
676 	ocfs2_commit_trans(osb, handle);
677 out:
678 	mlog_exit(status);
679 	return status;
680 }
681 
682 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
683 {
684 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
685 	/*
686 	 * We modify tree, leaf block, global info, local chunk header,
687 	 * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
688 	 * accounts for inode update
689 	 */
690 	return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
691 	       OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
692 	       OCFS2_QINFO_WRITE_CREDITS +
693 	       OCFS2_INODE_UPDATE_CREDITS;
694 }
695 
696 static int ocfs2_release_dquot(struct dquot *dquot)
697 {
698 	handle_t *handle;
699 	struct ocfs2_mem_dqinfo *oinfo =
700 			sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
701 	struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
702 	int status = 0;
703 
704 	mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
705 
706 	status = ocfs2_lock_global_qf(oinfo, 1);
707 	if (status < 0)
708 		goto out;
709 	handle = ocfs2_start_trans(osb,
710 		ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
711 	if (IS_ERR(handle)) {
712 		status = PTR_ERR(handle);
713 		mlog_errno(status);
714 		goto out_ilock;
715 	}
716 	status = dquot_release(dquot);
717 	ocfs2_commit_trans(osb, handle);
718 out_ilock:
719 	ocfs2_unlock_global_qf(oinfo, 1);
720 out:
721 	mlog_exit(status);
722 	return status;
723 }
724 
725 static int ocfs2_acquire_dquot(struct dquot *dquot)
726 {
727 	struct ocfs2_mem_dqinfo *oinfo =
728 			sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
729 	int status = 0;
730 
731 	mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
732 	/* We need an exclusive lock, because we're going to update use count
733 	 * and instantiate possibly new dquot structure */
734 	status = ocfs2_lock_global_qf(oinfo, 1);
735 	if (status < 0)
736 		goto out;
737 	status = dquot_acquire(dquot);
738 	ocfs2_unlock_global_qf(oinfo, 1);
739 out:
740 	mlog_exit(status);
741 	return status;
742 }
743 
744 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
745 {
746 	unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
747 			     (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
748 			     (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
749 			     (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
750 			     (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
751 			     (1 << (DQ_LASTSET_B + QIF_ITIME_B));
752 	int sync = 0;
753 	int status;
754 	struct super_block *sb = dquot->dq_sb;
755 	int type = dquot->dq_type;
756 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
757 	handle_t *handle;
758 	struct ocfs2_super *osb = OCFS2_SB(sb);
759 
760 	mlog_entry("id=%u, type=%d", dquot->dq_id, type);
761 	dquot_mark_dquot_dirty(dquot);
762 
763 	/* In case user set some limits, sync dquot immediately to global
764 	 * quota file so that information propagates quicker */
765 	spin_lock(&dq_data_lock);
766 	if (dquot->dq_flags & mask)
767 		sync = 1;
768 	spin_unlock(&dq_data_lock);
769 	/* This is a slight hack but we can't afford getting global quota
770 	 * lock if we already have a transaction started. */
771 	if (!sync || journal_current_handle()) {
772 		status = ocfs2_write_dquot(dquot);
773 		goto out;
774 	}
775 	status = ocfs2_lock_global_qf(oinfo, 1);
776 	if (status < 0)
777 		goto out;
778 	handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
779 	if (IS_ERR(handle)) {
780 		status = PTR_ERR(handle);
781 		mlog_errno(status);
782 		goto out_ilock;
783 	}
784 	status = ocfs2_sync_dquot(dquot);
785 	if (status < 0) {
786 		mlog_errno(status);
787 		goto out_trans;
788 	}
789 	/* Now write updated local dquot structure */
790 	status = dquot_commit(dquot);
791 out_trans:
792 	ocfs2_commit_trans(osb, handle);
793 out_ilock:
794 	ocfs2_unlock_global_qf(oinfo, 1);
795 out:
796 	mlog_exit(status);
797 	return status;
798 }
799 
800 /* This should happen only after set_dqinfo(). */
801 static int ocfs2_write_info(struct super_block *sb, int type)
802 {
803 	handle_t *handle;
804 	int status = 0;
805 	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
806 
807 	mlog_entry_void();
808 
809 	status = ocfs2_lock_global_qf(oinfo, 1);
810 	if (status < 0)
811 		goto out;
812 	handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
813 	if (IS_ERR(handle)) {
814 		status = PTR_ERR(handle);
815 		mlog_errno(status);
816 		goto out_ilock;
817 	}
818 	status = dquot_commit_info(sb, type);
819 	ocfs2_commit_trans(OCFS2_SB(sb), handle);
820 out_ilock:
821 	ocfs2_unlock_global_qf(oinfo, 1);
822 out:
823 	mlog_exit(status);
824 	return status;
825 }
826 
827 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
828 {
829 	struct ocfs2_dquot *dquot =
830 				kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
831 
832 	if (!dquot)
833 		return NULL;
834 	return &dquot->dq_dquot;
835 }
836 
837 static void ocfs2_destroy_dquot(struct dquot *dquot)
838 {
839 	kmem_cache_free(ocfs2_dquot_cachep, dquot);
840 }
841 
842 struct dquot_operations ocfs2_quota_operations = {
843 	.initialize	= dquot_initialize,
844 	.drop		= dquot_drop,
845 	.alloc_space	= dquot_alloc_space,
846 	.alloc_inode	= dquot_alloc_inode,
847 	.free_space	= dquot_free_space,
848 	.free_inode	= dquot_free_inode,
849 	.transfer	= dquot_transfer,
850 	.write_dquot	= ocfs2_write_dquot,
851 	.acquire_dquot	= ocfs2_acquire_dquot,
852 	.release_dquot	= ocfs2_release_dquot,
853 	.mark_dirty	= ocfs2_mark_dquot_dirty,
854 	.write_info	= ocfs2_write_info,
855 	.alloc_dquot	= ocfs2_alloc_dquot,
856 	.destroy_dquot	= ocfs2_destroy_dquot,
857 };
858 
859 int ocfs2_quota_setup(void)
860 {
861 	ocfs2_quota_wq = create_workqueue("o2quot");
862 	if (!ocfs2_quota_wq)
863 		return -ENOMEM;
864 	return 0;
865 }
866 
867 void ocfs2_quota_shutdown(void)
868 {
869 	if (ocfs2_quota_wq) {
870 		flush_workqueue(ocfs2_quota_wq);
871 		destroy_workqueue(ocfs2_quota_wq);
872 		ocfs2_quota_wq = NULL;
873 	}
874 }
875