Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Implementation of operations over global quota file
0004  */
0005 #include <linux/spinlock.h>
0006 #include <linux/fs.h>
0007 #include <linux/slab.h>
0008 #include <linux/quota.h>
0009 #include <linux/quotaops.h>
0010 #include <linux/dqblk_qtree.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/writeback.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/llist.h>
0015 #include <linux/iversion.h>
0016 
0017 #include <cluster/masklog.h>
0018 
0019 #include "ocfs2_fs.h"
0020 #include "ocfs2.h"
0021 #include "alloc.h"
0022 #include "blockcheck.h"
0023 #include "inode.h"
0024 #include "journal.h"
0025 #include "file.h"
0026 #include "sysfile.h"
0027 #include "dlmglue.h"
0028 #include "uptodate.h"
0029 #include "super.h"
0030 #include "buffer_head_io.h"
0031 #include "quota.h"
0032 #include "ocfs2_trace.h"
0033 
0034 /*
0035  * Locking of quotas with OCFS2 is rather complex. Here are rules that
0036  * should be obeyed by all the functions:
0037  * - any write of quota structure (either to local or global file) is protected
0038  *   by dqio_sem or dquot->dq_lock.
0039  * - any modification of global quota file holds inode cluster lock, i_rwsem,
0040  *   and ip_alloc_sem of the global quota file (achieved by
0041  *   ocfs2_lock_global_qf). It also has to hold qinfo_lock.
0042  * - an allocation of new blocks for local quota file is protected by
0043  *   its ip_alloc_sem
0044  *
0045  * A rough sketch of locking dependencies (lf = local file, gf = global file):
0046  * Normal filesystem operation:
0047  *   start_trans -> dqio_sem -> write to lf
0048  * Syncing of local and global file:
0049  *   ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
0050  *     write to gf
0051  *                             -> write to lf
0052  * Acquire dquot for the first time:
0053  *   dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf
0054  *                   -> alloc space for gf
0055  *                   -> start_trans -> qinfo_lock -> write to gf
0056  *       -> ip_alloc_sem of lf -> alloc space for lf
0057  *       -> write to lf
0058  * Release last reference to dquot:
0059  *   dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf
0060  *       -> write to lf
0061  * Note that all the above operations also hold the inode cluster lock of lf.
0062  * Recovery:
0063  *   inode cluster lock of recovered lf
0064  *     -> read bitmaps -> ip_alloc_sem of lf
0065  *     -> ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
0066  *        write to gf
0067  */
0068 
0069 static void qsync_work_fn(struct work_struct *work);
0070 
0071 static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
0072 {
0073     struct ocfs2_global_disk_dqblk *d = dp;
0074     struct mem_dqblk *m = &dquot->dq_dqb;
0075 
0076     /* Update from disk only entries not set by the admin */
0077     if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
0078         m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
0079         m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
0080     }
0081     if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
0082         m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
0083     if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
0084         m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
0085         m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
0086     }
0087     if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
0088         m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
0089     if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
0090         m->dqb_btime = le64_to_cpu(d->dqb_btime);
0091     if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
0092         m->dqb_itime = le64_to_cpu(d->dqb_itime);
0093     OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
0094 }
0095 
0096 static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
0097 {
0098     struct ocfs2_global_disk_dqblk *d = dp;
0099     struct mem_dqblk *m = &dquot->dq_dqb;
0100 
0101     d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
0102     d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
0103     d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
0104     d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
0105     d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
0106     d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
0107     d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
0108     d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
0109     d->dqb_btime = cpu_to_le64(m->dqb_btime);
0110     d->dqb_itime = cpu_to_le64(m->dqb_itime);
0111     d->dqb_pad1 = d->dqb_pad2 = 0;
0112 }
0113 
0114 static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
0115 {
0116     struct ocfs2_global_disk_dqblk *d = dp;
0117     struct ocfs2_mem_dqinfo *oinfo =
0118             sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
0119 
0120     if (qtree_entry_unused(&oinfo->dqi_gi, dp))
0121         return 0;
0122 
0123     return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
0124                 le32_to_cpu(d->dqb_id)),
0125               dquot->dq_id);
0126 }
0127 
0128 const struct qtree_fmt_operations ocfs2_global_ops = {
0129     .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
0130     .disk2mem_dqblk = ocfs2_global_disk2memdqb,
0131     .is_id = ocfs2_global_is_id,
0132 };
0133 
0134 int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh)
0135 {
0136     struct ocfs2_disk_dqtrailer *dqt =
0137         ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
0138 
0139     trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr);
0140 
0141     BUG_ON(!buffer_uptodate(bh));
0142 
0143     /*
0144      * If the ecc fails, we return the error but otherwise
0145      * leave the filesystem running.  We know any error is
0146      * local to this block.
0147      */
0148     return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
0149 }
0150 
0151 int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
0152                 struct buffer_head **bhp)
0153 {
0154     int rc;
0155 
0156     *bhp = NULL;
0157     rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
0158                    ocfs2_validate_quota_block);
0159     if (rc)
0160         mlog_errno(rc);
0161     return rc;
0162 }
0163 
0164 /* Read data from global quotafile - avoid pagecache and such because we cannot
0165  * afford acquiring the locks... We use quota cluster lock to serialize
0166  * operations. Caller is responsible for acquiring it. */
0167 ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
0168              size_t len, loff_t off)
0169 {
0170     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0171     struct inode *gqinode = oinfo->dqi_gqinode;
0172     loff_t i_size = i_size_read(gqinode);
0173     int offset = off & (sb->s_blocksize - 1);
0174     sector_t blk = off >> sb->s_blocksize_bits;
0175     int err = 0;
0176     struct buffer_head *bh;
0177     size_t toread, tocopy;
0178     u64 pblock = 0, pcount = 0;
0179 
0180     if (off > i_size)
0181         return 0;
0182     if (off + len > i_size)
0183         len = i_size - off;
0184     toread = len;
0185     while (toread > 0) {
0186         tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
0187         if (!pcount) {
0188             err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock,
0189                               &pcount, NULL);
0190             if (err) {
0191                 mlog_errno(err);
0192                 return err;
0193             }
0194         } else {
0195             pcount--;
0196             pblock++;
0197         }
0198         bh = NULL;
0199         err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
0200         if (err) {
0201             mlog_errno(err);
0202             return err;
0203         }
0204         memcpy(data, bh->b_data + offset, tocopy);
0205         brelse(bh);
0206         offset = 0;
0207         toread -= tocopy;
0208         data += tocopy;
0209         blk++;
0210     }
0211     return len;
0212 }
0213 
0214 /* Write to quotafile (we know the transaction is already started and has
0215  * enough credits) */
0216 ssize_t ocfs2_quota_write(struct super_block *sb, int type,
0217               const char *data, size_t len, loff_t off)
0218 {
0219     struct mem_dqinfo *info = sb_dqinfo(sb, type);
0220     struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
0221     struct inode *gqinode = oinfo->dqi_gqinode;
0222     int offset = off & (sb->s_blocksize - 1);
0223     sector_t blk = off >> sb->s_blocksize_bits;
0224     int err = 0, new = 0, ja_type;
0225     struct buffer_head *bh = NULL;
0226     handle_t *handle = journal_current_handle();
0227     u64 pblock, pcount;
0228 
0229     if (!handle) {
0230         mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
0231              "because transaction was not started.\n",
0232              (unsigned long long)off, (unsigned long long)len);
0233         return -EIO;
0234     }
0235     if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
0236         WARN_ON(1);
0237         len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
0238     }
0239 
0240     if (i_size_read(gqinode) < off + len) {
0241         loff_t rounded_end =
0242                 ocfs2_align_bytes_to_blocks(sb, off + len);
0243 
0244         /* Space is already allocated in ocfs2_acquire_dquot() */
0245         err = ocfs2_simple_size_update(gqinode,
0246                            oinfo->dqi_gqi_bh,
0247                            rounded_end);
0248         if (err < 0)
0249             goto out;
0250         new = 1;
0251     }
0252     err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL);
0253     if (err) {
0254         mlog_errno(err);
0255         goto out;
0256     }
0257     /* Not rewriting whole block? */
0258     if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
0259         !new) {
0260         err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
0261         ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
0262     } else {
0263         bh = sb_getblk(sb, pblock);
0264         if (!bh)
0265             err = -ENOMEM;
0266         ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
0267     }
0268     if (err) {
0269         mlog_errno(err);
0270         goto out;
0271     }
0272     lock_buffer(bh);
0273     if (new)
0274         memset(bh->b_data, 0, sb->s_blocksize);
0275     memcpy(bh->b_data + offset, data, len);
0276     flush_dcache_page(bh->b_page);
0277     set_buffer_uptodate(bh);
0278     unlock_buffer(bh);
0279     ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
0280     err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
0281                       ja_type);
0282     if (err < 0) {
0283         brelse(bh);
0284         goto out;
0285     }
0286     ocfs2_journal_dirty(handle, bh);
0287     brelse(bh);
0288 out:
0289     if (err) {
0290         mlog_errno(err);
0291         return err;
0292     }
0293     inode_inc_iversion(gqinode);
0294     ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
0295     return len;
0296 }
0297 
0298 int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
0299 {
0300     int status;
0301     struct buffer_head *bh = NULL;
0302 
0303     status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
0304     if (status < 0)
0305         return status;
0306     spin_lock(&dq_data_lock);
0307     if (!oinfo->dqi_gqi_count++)
0308         oinfo->dqi_gqi_bh = bh;
0309     else
0310         WARN_ON(bh != oinfo->dqi_gqi_bh);
0311     spin_unlock(&dq_data_lock);
0312     if (ex) {
0313         inode_lock(oinfo->dqi_gqinode);
0314         down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
0315     } else {
0316         down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
0317     }
0318     return 0;
0319 }
0320 
0321 void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
0322 {
0323     if (ex) {
0324         up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
0325         inode_unlock(oinfo->dqi_gqinode);
0326     } else {
0327         up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
0328     }
0329     ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
0330     brelse(oinfo->dqi_gqi_bh);
0331     spin_lock(&dq_data_lock);
0332     if (!--oinfo->dqi_gqi_count)
0333         oinfo->dqi_gqi_bh = NULL;
0334     spin_unlock(&dq_data_lock);
0335 }
0336 
0337 /* Read information header from global quota file */
0338 int ocfs2_global_read_info(struct super_block *sb, int type)
0339 {
0340     unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
0341                           GROUP_QUOTA_SYSTEM_INODE };
0342     struct ocfs2_global_disk_dqinfo dinfo;
0343     struct mem_dqinfo *info = sb_dqinfo(sb, type);
0344     struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
0345     u64 pcount;
0346     int status;
0347 
0348     oinfo->dqi_gi.dqi_sb = sb;
0349     oinfo->dqi_gi.dqi_type = type;
0350     ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
0351     oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
0352     oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
0353     oinfo->dqi_gqi_bh = NULL;
0354     oinfo->dqi_gqi_count = 0;
0355 
0356     /* Read global header */
0357     oinfo->dqi_gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
0358             OCFS2_INVALID_SLOT);
0359     if (!oinfo->dqi_gqinode) {
0360         mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
0361             type);
0362         status = -EINVAL;
0363         goto out_err;
0364     }
0365 
0366     status = ocfs2_lock_global_qf(oinfo, 0);
0367     if (status < 0) {
0368         mlog_errno(status);
0369         goto out_err;
0370     }
0371 
0372     status = ocfs2_extent_map_get_blocks(oinfo->dqi_gqinode, 0, &oinfo->dqi_giblk,
0373                          &pcount, NULL);
0374     if (status < 0)
0375         goto out_unlock;
0376 
0377     status = ocfs2_qinfo_lock(oinfo, 0);
0378     if (status < 0)
0379         goto out_unlock;
0380     status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
0381                       sizeof(struct ocfs2_global_disk_dqinfo),
0382                       OCFS2_GLOBAL_INFO_OFF);
0383     ocfs2_qinfo_unlock(oinfo, 0);
0384     ocfs2_unlock_global_qf(oinfo, 0);
0385     if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
0386         mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
0387              status);
0388         if (status >= 0)
0389             status = -EIO;
0390         mlog_errno(status);
0391         goto out_err;
0392     }
0393     info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
0394     info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
0395     oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
0396     oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
0397     oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
0398     oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
0399     oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
0400     oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
0401                         OCFS2_QBLK_RESERVED_SPACE;
0402     oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
0403     INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
0404     schedule_delayed_work(&oinfo->dqi_sync_work,
0405                   msecs_to_jiffies(oinfo->dqi_syncms));
0406 
0407 out_err:
0408     return status;
0409 out_unlock:
0410     ocfs2_unlock_global_qf(oinfo, 0);
0411     mlog_errno(status);
0412     goto out_err;
0413 }
0414 
0415 /* Write information to global quota file. Expects exclusive lock on quota
0416  * file inode and quota info */
0417 static int __ocfs2_global_write_info(struct super_block *sb, int type)
0418 {
0419     struct mem_dqinfo *info = sb_dqinfo(sb, type);
0420     struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
0421     struct ocfs2_global_disk_dqinfo dinfo;
0422     ssize_t size;
0423 
0424     spin_lock(&dq_data_lock);
0425     info->dqi_flags &= ~DQF_INFO_DIRTY;
0426     dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
0427     dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
0428     spin_unlock(&dq_data_lock);
0429     dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
0430     dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
0431     dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
0432     dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
0433     size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
0434                      sizeof(struct ocfs2_global_disk_dqinfo),
0435                      OCFS2_GLOBAL_INFO_OFF);
0436     if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
0437         mlog(ML_ERROR, "Cannot write global quota info structure\n");
0438         if (size >= 0)
0439             size = -EIO;
0440         return size;
0441     }
0442     return 0;
0443 }
0444 
0445 int ocfs2_global_write_info(struct super_block *sb, int type)
0446 {
0447     int err;
0448     struct quota_info *dqopt = sb_dqopt(sb);
0449     struct ocfs2_mem_dqinfo *info = dqopt->info[type].dqi_priv;
0450 
0451     down_write(&dqopt->dqio_sem);
0452     err = ocfs2_qinfo_lock(info, 1);
0453     if (err < 0)
0454         goto out_sem;
0455     err = __ocfs2_global_write_info(sb, type);
0456     ocfs2_qinfo_unlock(info, 1);
0457 out_sem:
0458     up_write(&dqopt->dqio_sem);
0459     return err;
0460 }
0461 
0462 static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
0463 {
0464     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0465 
0466     /*
0467      * We may need to allocate tree blocks and a leaf block but not the
0468      * root block
0469      */
0470     return oinfo->dqi_gi.dqi_qtree_depth;
0471 }
0472 
0473 static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
0474 {
0475     /* We modify all the allocated blocks, tree root, info block and
0476      * the inode */
0477     return (ocfs2_global_qinit_alloc(sb, type) + 2) *
0478             OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1;
0479 }
0480 
0481 /* Sync local information about quota modifications with global quota file.
0482  * Caller must have started the transaction and obtained exclusive lock for
0483  * global quota file inode */
0484 int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
0485 {
0486     int err, err2;
0487     struct super_block *sb = dquot->dq_sb;
0488     int type = dquot->dq_id.type;
0489     struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
0490     struct ocfs2_global_disk_dqblk dqblk;
0491     s64 spacechange, inodechange;
0492     time64_t olditime, oldbtime;
0493 
0494     err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
0495                    sizeof(struct ocfs2_global_disk_dqblk),
0496                    dquot->dq_off);
0497     if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
0498         if (err >= 0) {
0499             mlog(ML_ERROR, "Short read from global quota file "
0500                        "(%u read)\n", err);
0501             err = -EIO;
0502         }
0503         goto out;
0504     }
0505 
0506     /* Update space and inode usage. Get also other information from
0507      * global quota file so that we don't overwrite any changes there.
0508      * We are */
0509     spin_lock(&dquot->dq_dqb_lock);
0510     spacechange = dquot->dq_dqb.dqb_curspace -
0511                     OCFS2_DQUOT(dquot)->dq_origspace;
0512     inodechange = dquot->dq_dqb.dqb_curinodes -
0513                     OCFS2_DQUOT(dquot)->dq_originodes;
0514     olditime = dquot->dq_dqb.dqb_itime;
0515     oldbtime = dquot->dq_dqb.dqb_btime;
0516     ocfs2_global_disk2memdqb(dquot, &dqblk);
0517     trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id),
0518                    dquot->dq_dqb.dqb_curspace,
0519                    (long long)spacechange,
0520                    dquot->dq_dqb.dqb_curinodes,
0521                    (long long)inodechange);
0522     if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
0523         dquot->dq_dqb.dqb_curspace += spacechange;
0524     if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
0525         dquot->dq_dqb.dqb_curinodes += inodechange;
0526     /* Set properly space grace time... */
0527     if (dquot->dq_dqb.dqb_bsoftlimit &&
0528         dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
0529         if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
0530             oldbtime > 0) {
0531             if (dquot->dq_dqb.dqb_btime > 0)
0532                 dquot->dq_dqb.dqb_btime =
0533                     min(dquot->dq_dqb.dqb_btime, oldbtime);
0534             else
0535                 dquot->dq_dqb.dqb_btime = oldbtime;
0536         }
0537     } else {
0538         dquot->dq_dqb.dqb_btime = 0;
0539         clear_bit(DQ_BLKS_B, &dquot->dq_flags);
0540     }
0541     /* Set properly inode grace time... */
0542     if (dquot->dq_dqb.dqb_isoftlimit &&
0543         dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
0544         if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
0545             olditime > 0) {
0546             if (dquot->dq_dqb.dqb_itime > 0)
0547                 dquot->dq_dqb.dqb_itime =
0548                     min(dquot->dq_dqb.dqb_itime, olditime);
0549             else
0550                 dquot->dq_dqb.dqb_itime = olditime;
0551         }
0552     } else {
0553         dquot->dq_dqb.dqb_itime = 0;
0554         clear_bit(DQ_INODES_B, &dquot->dq_flags);
0555     }
0556     /* All information is properly updated, clear the flags */
0557     __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
0558     __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
0559     __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
0560     __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
0561     __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
0562     __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
0563     OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
0564     OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
0565     spin_unlock(&dquot->dq_dqb_lock);
0566     err = ocfs2_qinfo_lock(info, freeing);
0567     if (err < 0) {
0568         mlog(ML_ERROR, "Failed to lock quota info, losing quota write"
0569                    " (type=%d, id=%u)\n", dquot->dq_id.type,
0570                    (unsigned)from_kqid(&init_user_ns, dquot->dq_id));
0571         goto out;
0572     }
0573     if (freeing)
0574         OCFS2_DQUOT(dquot)->dq_use_count--;
0575     err = qtree_write_dquot(&info->dqi_gi, dquot);
0576     if (err < 0)
0577         goto out_qlock;
0578     if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
0579         err = qtree_release_dquot(&info->dqi_gi, dquot);
0580         if (info_dirty(sb_dqinfo(sb, type))) {
0581             err2 = __ocfs2_global_write_info(sb, type);
0582             if (!err)
0583                 err = err2;
0584         }
0585     }
0586 out_qlock:
0587     ocfs2_qinfo_unlock(info, freeing);
0588 out:
0589     if (err < 0)
0590         mlog_errno(err);
0591     return err;
0592 }
0593 
0594 /*
0595  *  Functions for periodic syncing of dquots with global file
0596  */
0597 static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
0598 {
0599     handle_t *handle;
0600     struct super_block *sb = dquot->dq_sb;
0601     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0602     struct ocfs2_super *osb = OCFS2_SB(sb);
0603     int status = 0;
0604 
0605     trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id),
0606                       dquot->dq_id.type,
0607                       type, sb->s_id);
0608     if (type != dquot->dq_id.type)
0609         goto out;
0610     status = ocfs2_lock_global_qf(oinfo, 1);
0611     if (status < 0)
0612         goto out;
0613 
0614     handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
0615     if (IS_ERR(handle)) {
0616         status = PTR_ERR(handle);
0617         mlog_errno(status);
0618         goto out_ilock;
0619     }
0620     down_write(&sb_dqopt(sb)->dqio_sem);
0621     status = ocfs2_sync_dquot(dquot);
0622     if (status < 0)
0623         mlog_errno(status);
0624     /* We have to write local structure as well... */
0625     status = ocfs2_local_write_dquot(dquot);
0626     if (status < 0)
0627         mlog_errno(status);
0628     up_write(&sb_dqopt(sb)->dqio_sem);
0629     ocfs2_commit_trans(osb, handle);
0630 out_ilock:
0631     ocfs2_unlock_global_qf(oinfo, 1);
0632 out:
0633     return status;
0634 }
0635 
0636 static void qsync_work_fn(struct work_struct *work)
0637 {
0638     struct ocfs2_mem_dqinfo *oinfo = container_of(work,
0639                               struct ocfs2_mem_dqinfo,
0640                               dqi_sync_work.work);
0641     struct super_block *sb = oinfo->dqi_gqinode->i_sb;
0642 
0643     /*
0644      * We have to be careful here not to deadlock on s_umount as umount
0645      * disabling quotas may be in progress and it waits for this work to
0646      * complete. If trylock fails, we'll do the sync next time...
0647      */
0648     if (down_read_trylock(&sb->s_umount)) {
0649         dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
0650         up_read(&sb->s_umount);
0651     }
0652     schedule_delayed_work(&oinfo->dqi_sync_work,
0653                   msecs_to_jiffies(oinfo->dqi_syncms));
0654 }
0655 
0656 /*
0657  *  Wrappers for generic quota functions
0658  */
0659 
0660 static int ocfs2_write_dquot(struct dquot *dquot)
0661 {
0662     handle_t *handle;
0663     struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
0664     int status = 0;
0665 
0666     trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id),
0667                 dquot->dq_id.type);
0668 
0669     handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
0670     if (IS_ERR(handle)) {
0671         status = PTR_ERR(handle);
0672         mlog_errno(status);
0673         goto out;
0674     }
0675     down_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
0676     status = ocfs2_local_write_dquot(dquot);
0677     up_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
0678     ocfs2_commit_trans(osb, handle);
0679 out:
0680     return status;
0681 }
0682 
0683 static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
0684 {
0685     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0686     /*
0687      * We modify tree, leaf block, global info, local chunk header,
0688      * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
0689      * accounts for inode update
0690      */
0691     return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
0692            OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
0693            OCFS2_QINFO_WRITE_CREDITS +
0694            OCFS2_INODE_UPDATE_CREDITS;
0695 }
0696 
0697 void ocfs2_drop_dquot_refs(struct work_struct *work)
0698 {
0699     struct ocfs2_super *osb = container_of(work, struct ocfs2_super,
0700                            dquot_drop_work);
0701     struct llist_node *list;
0702     struct ocfs2_dquot *odquot, *next_odquot;
0703 
0704     list = llist_del_all(&osb->dquot_drop_list);
0705     llist_for_each_entry_safe(odquot, next_odquot, list, list) {
0706         /* Drop the reference we acquired in ocfs2_dquot_release() */
0707         dqput(&odquot->dq_dquot);
0708     }
0709 }
0710 
0711 /*
0712  * Called when the last reference to dquot is dropped. If we are called from
0713  * downconvert thread, we cannot do all the handling here because grabbing
0714  * quota lock could deadlock (the node holding the quota lock could need some
0715  * other cluster lock to proceed but with blocked downconvert thread we cannot
0716  * release any lock).
0717  */
0718 static int ocfs2_release_dquot(struct dquot *dquot)
0719 {
0720     handle_t *handle;
0721     struct ocfs2_mem_dqinfo *oinfo =
0722             sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
0723     struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
0724     int status = 0;
0725 
0726     trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id),
0727                   dquot->dq_id.type);
0728 
0729     mutex_lock(&dquot->dq_lock);
0730     /* Check whether we are not racing with some other dqget() */
0731     if (dquot_is_busy(dquot))
0732         goto out;
0733     /* Running from downconvert thread? Postpone quota processing to wq */
0734     if (current == osb->dc_task) {
0735         /*
0736          * Grab our own reference to dquot and queue it for delayed
0737          * dropping.  Quota code rechecks after calling
0738          * ->release_dquot() and won't free dquot structure.
0739          */
0740         dqgrab(dquot);
0741         /* First entry on list -> queue work */
0742         if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list))
0743             queue_work(osb->ocfs2_wq, &osb->dquot_drop_work);
0744         goto out;
0745     }
0746     status = ocfs2_lock_global_qf(oinfo, 1);
0747     if (status < 0)
0748         goto out;
0749     handle = ocfs2_start_trans(osb,
0750         ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type));
0751     if (IS_ERR(handle)) {
0752         status = PTR_ERR(handle);
0753         mlog_errno(status);
0754         goto out_ilock;
0755     }
0756 
0757     status = ocfs2_global_release_dquot(dquot);
0758     if (status < 0) {
0759         mlog_errno(status);
0760         goto out_trans;
0761     }
0762     status = ocfs2_local_release_dquot(handle, dquot);
0763     /*
0764      * If we fail here, we cannot do much as global structure is
0765      * already released. So just complain...
0766      */
0767     if (status < 0)
0768         mlog_errno(status);
0769     /*
0770      * Clear dq_off so that we search for the structure in quota file next
0771      * time we acquire it. The structure might be deleted and reallocated
0772      * elsewhere by another node while our dquot structure is on freelist.
0773      */
0774     dquot->dq_off = 0;
0775     clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
0776 out_trans:
0777     ocfs2_commit_trans(osb, handle);
0778 out_ilock:
0779     ocfs2_unlock_global_qf(oinfo, 1);
0780 out:
0781     mutex_unlock(&dquot->dq_lock);
0782     if (status)
0783         mlog_errno(status);
0784     return status;
0785 }
0786 
0787 /*
0788  * Read global dquot structure from disk or create it if it does
0789  * not exist. Also update use count of the global structure and
0790  * create structure in node-local quota file.
0791  */
0792 static int ocfs2_acquire_dquot(struct dquot *dquot)
0793 {
0794     int status = 0, err;
0795     int ex = 0;
0796     struct super_block *sb = dquot->dq_sb;
0797     struct ocfs2_super *osb = OCFS2_SB(sb);
0798     int type = dquot->dq_id.type;
0799     struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
0800     struct inode *gqinode = info->dqi_gqinode;
0801     int need_alloc = ocfs2_global_qinit_alloc(sb, type);
0802     handle_t *handle;
0803 
0804     trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id),
0805                   type);
0806     mutex_lock(&dquot->dq_lock);
0807     /*
0808      * We need an exclusive lock, because we're going to update use count
0809      * and instantiate possibly new dquot structure
0810      */
0811     status = ocfs2_lock_global_qf(info, 1);
0812     if (status < 0)
0813         goto out;
0814     status = ocfs2_qinfo_lock(info, 0);
0815     if (status < 0)
0816         goto out_dq;
0817     /*
0818      * We always want to read dquot structure from disk because we don't
0819      * know what happened with it while it was on freelist.
0820      */
0821     status = qtree_read_dquot(&info->dqi_gi, dquot);
0822     ocfs2_qinfo_unlock(info, 0);
0823     if (status < 0)
0824         goto out_dq;
0825 
0826     OCFS2_DQUOT(dquot)->dq_use_count++;
0827     OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
0828     OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
0829     if (!dquot->dq_off) {   /* No real quota entry? */
0830         ex = 1;
0831         /*
0832          * Add blocks to quota file before we start a transaction since
0833          * locking allocators ranks above a transaction start
0834          */
0835         WARN_ON(journal_current_handle());
0836         status = ocfs2_extend_no_holes(gqinode, NULL,
0837             i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits),
0838             i_size_read(gqinode));
0839         if (status < 0)
0840             goto out_dq;
0841     }
0842 
0843     handle = ocfs2_start_trans(osb,
0844                    ocfs2_calc_global_qinit_credits(sb, type));
0845     if (IS_ERR(handle)) {
0846         status = PTR_ERR(handle);
0847         goto out_dq;
0848     }
0849     status = ocfs2_qinfo_lock(info, ex);
0850     if (status < 0)
0851         goto out_trans;
0852     status = qtree_write_dquot(&info->dqi_gi, dquot);
0853     if (ex && info_dirty(sb_dqinfo(sb, type))) {
0854         err = __ocfs2_global_write_info(sb, type);
0855         if (!status)
0856             status = err;
0857     }
0858     ocfs2_qinfo_unlock(info, ex);
0859 out_trans:
0860     ocfs2_commit_trans(osb, handle);
0861 out_dq:
0862     ocfs2_unlock_global_qf(info, 1);
0863     if (status < 0)
0864         goto out;
0865 
0866     status = ocfs2_create_local_dquot(dquot);
0867     if (status < 0)
0868         goto out;
0869     set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
0870 out:
0871     mutex_unlock(&dquot->dq_lock);
0872     if (status)
0873         mlog_errno(status);
0874     return status;
0875 }
0876 
0877 static int ocfs2_get_next_id(struct super_block *sb, struct kqid *qid)
0878 {
0879     int type = qid->type;
0880     struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
0881     int status = 0;
0882 
0883     trace_ocfs2_get_next_id(from_kqid(&init_user_ns, *qid), type);
0884     if (!sb_has_quota_loaded(sb, type)) {
0885         status = -ESRCH;
0886         goto out;
0887     }
0888     status = ocfs2_lock_global_qf(info, 0);
0889     if (status < 0)
0890         goto out;
0891     status = ocfs2_qinfo_lock(info, 0);
0892     if (status < 0)
0893         goto out_global;
0894     status = qtree_get_next_id(&info->dqi_gi, qid);
0895     ocfs2_qinfo_unlock(info, 0);
0896 out_global:
0897     ocfs2_unlock_global_qf(info, 0);
0898 out:
0899     /*
0900      * Avoid logging ENOENT since it just means there isn't next ID and
0901      * ESRCH which means quota isn't enabled for the filesystem.
0902      */
0903     if (status && status != -ENOENT && status != -ESRCH)
0904         mlog_errno(status);
0905     return status;
0906 }
0907 
0908 static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
0909 {
0910     unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
0911                  (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
0912                  (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
0913                  (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
0914                  (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
0915                  (1 << (DQ_LASTSET_B + QIF_ITIME_B));
0916     int sync = 0;
0917     int status;
0918     struct super_block *sb = dquot->dq_sb;
0919     int type = dquot->dq_id.type;
0920     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0921     handle_t *handle;
0922     struct ocfs2_super *osb = OCFS2_SB(sb);
0923 
0924     trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id),
0925                      type);
0926 
0927     /* In case user set some limits, sync dquot immediately to global
0928      * quota file so that information propagates quicker */
0929     spin_lock(&dquot->dq_dqb_lock);
0930     if (dquot->dq_flags & mask)
0931         sync = 1;
0932     spin_unlock(&dquot->dq_dqb_lock);
0933     /* This is a slight hack but we can't afford getting global quota
0934      * lock if we already have a transaction started. */
0935     if (!sync || journal_current_handle()) {
0936         status = ocfs2_write_dquot(dquot);
0937         goto out;
0938     }
0939     status = ocfs2_lock_global_qf(oinfo, 1);
0940     if (status < 0)
0941         goto out;
0942     handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
0943     if (IS_ERR(handle)) {
0944         status = PTR_ERR(handle);
0945         mlog_errno(status);
0946         goto out_ilock;
0947     }
0948     down_write(&sb_dqopt(sb)->dqio_sem);
0949     status = ocfs2_sync_dquot(dquot);
0950     if (status < 0) {
0951         mlog_errno(status);
0952         goto out_dlock;
0953     }
0954     /* Now write updated local dquot structure */
0955     status = ocfs2_local_write_dquot(dquot);
0956 out_dlock:
0957     up_write(&sb_dqopt(sb)->dqio_sem);
0958     ocfs2_commit_trans(osb, handle);
0959 out_ilock:
0960     ocfs2_unlock_global_qf(oinfo, 1);
0961 out:
0962     if (status)
0963         mlog_errno(status);
0964     return status;
0965 }
0966 
0967 /* This should happen only after set_dqinfo(). */
0968 static int ocfs2_write_info(struct super_block *sb, int type)
0969 {
0970     handle_t *handle;
0971     int status = 0;
0972     struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
0973 
0974     status = ocfs2_lock_global_qf(oinfo, 1);
0975     if (status < 0)
0976         goto out;
0977     handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
0978     if (IS_ERR(handle)) {
0979         status = PTR_ERR(handle);
0980         mlog_errno(status);
0981         goto out_ilock;
0982     }
0983     status = dquot_commit_info(sb, type);
0984     ocfs2_commit_trans(OCFS2_SB(sb), handle);
0985 out_ilock:
0986     ocfs2_unlock_global_qf(oinfo, 1);
0987 out:
0988     if (status)
0989         mlog_errno(status);
0990     return status;
0991 }
0992 
0993 static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
0994 {
0995     struct ocfs2_dquot *dquot =
0996                 kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
0997 
0998     if (!dquot)
0999         return NULL;
1000     return &dquot->dq_dquot;
1001 }
1002 
1003 static void ocfs2_destroy_dquot(struct dquot *dquot)
1004 {
1005     kmem_cache_free(ocfs2_dquot_cachep, dquot);
1006 }
1007 
1008 const struct dquot_operations ocfs2_quota_operations = {
1009     /* We never make dquot dirty so .write_dquot is never called */
1010     .acquire_dquot  = ocfs2_acquire_dquot,
1011     .release_dquot  = ocfs2_release_dquot,
1012     .mark_dirty = ocfs2_mark_dquot_dirty,
1013     .write_info = ocfs2_write_info,
1014     .alloc_dquot    = ocfs2_alloc_dquot,
1015     .destroy_dquot  = ocfs2_destroy_dquot,
1016     .get_next_id    = ocfs2_get_next_id,
1017 };