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