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