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