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