^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Implementation of operations over global quota file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/quota.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/dqblk_qtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/llist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "ocfs2_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "blockcheck.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "file.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "sysfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "dlmglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "uptodate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "buffer_head_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "ocfs2_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Locking of quotas with OCFS2 is rather complex. Here are rules that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * should be obeyed by all the functions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * - any write of quota structure (either to local or global file) is protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * by dqio_sem or dquot->dq_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * - any modification of global quota file holds inode cluster lock, i_mutex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * and ip_alloc_sem of the global quota file (achieved by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * ocfs2_lock_global_qf). It also has to hold qinfo_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * - an allocation of new blocks for local quota file is protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * its ip_alloc_sem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * A rough sketch of locking dependencies (lf = local file, gf = global file):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Normal filesystem operation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * start_trans -> dqio_sem -> write to lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Syncing of local and global file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * write to gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * -> write to lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Acquire dquot for the first time:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * -> alloc space for gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * -> start_trans -> qinfo_lock -> write to gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * -> ip_alloc_sem of lf -> alloc space for lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * -> write to lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Release last reference to dquot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * -> write to lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Note that all the above operations also hold the inode cluster lock of lf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Recovery:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * inode cluster lock of recovered lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * -> read bitmaps -> ip_alloc_sem of lf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * -> ocfs2_lock_global_qf -> start_trans -> dqio_sem -> qinfo_lock ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * write to gf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void qsync_work_fn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ocfs2_global_disk_dqblk *d = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct mem_dqblk *m = &dquot->dq_dqb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Update from disk only entries not set by the admin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) m->dqb_btime = le64_to_cpu(d->dqb_btime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) m->dqb_itime = le64_to_cpu(d->dqb_itime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct ocfs2_global_disk_dqblk *d = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct mem_dqblk *m = &dquot->dq_dqb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) d->dqb_btime = cpu_to_le64(m->dqb_btime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) d->dqb_itime = cpu_to_le64(m->dqb_itime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) d->dqb_pad1 = d->dqb_pad2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct ocfs2_global_disk_dqblk *d = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct ocfs2_mem_dqinfo *oinfo =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (qtree_entry_unused(&oinfo->dqi_gi, dp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) le32_to_cpu(d->dqb_id)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dquot->dq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) const struct qtree_fmt_operations ocfs2_global_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .disk2mem_dqblk = ocfs2_global_disk2memdqb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .is_id = ocfs2_global_is_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct ocfs2_disk_dqtrailer *dqt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) BUG_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * If the ecc fails, we return the error but otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * leave the filesystem running. We know any error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * local to this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct buffer_head **bhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *bhp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ocfs2_validate_quota_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) mlog_errno(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Read data from global quotafile - avoid pagecache and such because we cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * afford acquiring the locks... We use quota cluster lock to serialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * operations. Caller is responsible for acquiring it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) size_t len, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct inode *gqinode = oinfo->dqi_gqinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) loff_t i_size = i_size_read(gqinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int offset = off & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) sector_t blk = off >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) size_t toread, tocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u64 pblock = 0, pcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (off > i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (off + len > i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) len = i_size - off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) toread = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) while (toread > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!pcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) &pcount, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) memcpy(data, bh->b_data + offset, tocopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) toread -= tocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) data += tocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) blk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Write to quotafile (we know the transaction is already started and has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * enough credits) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ssize_t ocfs2_quota_write(struct super_block *sb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) const char *data, size_t len, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct mem_dqinfo *info = sb_dqinfo(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct inode *gqinode = oinfo->dqi_gqinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int offset = off & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) sector_t blk = off >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int err = 0, new = 0, ja_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) handle_t *handle = journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) u64 pblock, pcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) "because transaction was not started.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) (unsigned long long)off, (unsigned long long)len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (i_size_read(gqinode) < off + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) loff_t rounded_end =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ocfs2_align_bytes_to_blocks(sb, off + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Space is already allocated in ocfs2_acquire_dquot() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) err = ocfs2_simple_size_update(gqinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) oinfo->dqi_gqi_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) rounded_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) new = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Not rewriting whole block? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) !new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bh = sb_getblk(sb, pblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) memset(bh->b_data, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) memcpy(bh->b_data + offset, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) flush_dcache_page(bh->b_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ja_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ocfs2_journal_dirty(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) inode_inc_iversion(gqinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) spin_lock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!oinfo->dqi_gqi_count++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) oinfo->dqi_gqi_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) WARN_ON(bh != oinfo->dqi_gqi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) spin_unlock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) inode_lock(oinfo->dqi_gqinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) inode_unlock(oinfo->dqi_gqinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) brelse(oinfo->dqi_gqi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) spin_lock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!--oinfo->dqi_gqi_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) oinfo->dqi_gqi_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) spin_unlock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* Read information header from global quota file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int ocfs2_global_read_info(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct inode *gqinode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) GROUP_QUOTA_SYSTEM_INODE };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct ocfs2_global_disk_dqinfo dinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct mem_dqinfo *info = sb_dqinfo(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) u64 pcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /* Read global header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) OCFS2_INVALID_SLOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!gqinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) oinfo->dqi_gi.dqi_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) oinfo->dqi_gi.dqi_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) oinfo->dqi_gqi_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) oinfo->dqi_gqi_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) oinfo->dqi_gqinode = gqinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) status = ocfs2_lock_global_qf(oinfo, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) status = ocfs2_extent_map_get_blocks(gqinode, 0, &oinfo->dqi_giblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) &pcount, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) status = ocfs2_qinfo_lock(oinfo, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) sizeof(struct ocfs2_global_disk_dqinfo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) OCFS2_GLOBAL_INFO_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ocfs2_qinfo_unlock(oinfo, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ocfs2_unlock_global_qf(oinfo, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (status >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) status = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) OCFS2_QBLK_RESERVED_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) schedule_delayed_work(&oinfo->dqi_sync_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) msecs_to_jiffies(oinfo->dqi_syncms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ocfs2_unlock_global_qf(oinfo, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /* Write information to global quota file. Expects exlusive lock on quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * file inode and quota info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int __ocfs2_global_write_info(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct mem_dqinfo *info = sb_dqinfo(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct ocfs2_global_disk_dqinfo dinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) spin_lock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) info->dqi_flags &= ~DQF_INFO_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) spin_unlock(&dq_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) sizeof(struct ocfs2_global_disk_dqinfo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) OCFS2_GLOBAL_INFO_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) mlog(ML_ERROR, "Cannot write global quota info structure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (size >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) size = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int ocfs2_global_write_info(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct quota_info *dqopt = sb_dqopt(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct ocfs2_mem_dqinfo *info = dqopt->info[type].dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) down_write(&dqopt->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) err = ocfs2_qinfo_lock(info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) goto out_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) err = __ocfs2_global_write_info(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) ocfs2_qinfo_unlock(info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) out_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) up_write(&dqopt->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int ocfs2_global_qinit_alloc(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * We may need to allocate tree blocks and a leaf block but not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * root block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return oinfo->dqi_gi.dqi_qtree_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* We modify all the allocated blocks, tree root, info block and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return (ocfs2_global_qinit_alloc(sb, type) + 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* Sync local information about quota modifications with global quota file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * Caller must have started the transaction and obtained exclusive lock for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * global quota file inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int err, err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) int type = dquot->dq_id.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct ocfs2_global_disk_dqblk dqblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) s64 spacechange, inodechange;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) time64_t olditime, oldbtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sizeof(struct ocfs2_global_disk_dqblk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) dquot->dq_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (err >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) mlog(ML_ERROR, "Short read from global quota file "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) "(%u read)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /* Update space and inode usage. Get also other information from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * global quota file so that we don't overwrite any changes there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * We are */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) spin_lock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) spacechange = dquot->dq_dqb.dqb_curspace -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) OCFS2_DQUOT(dquot)->dq_origspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) inodechange = dquot->dq_dqb.dqb_curinodes -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) OCFS2_DQUOT(dquot)->dq_originodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) olditime = dquot->dq_dqb.dqb_itime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) oldbtime = dquot->dq_dqb.dqb_btime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) ocfs2_global_disk2memdqb(dquot, &dqblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) dquot->dq_dqb.dqb_curspace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) (long long)spacechange,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dquot->dq_dqb.dqb_curinodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) (long long)inodechange);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) dquot->dq_dqb.dqb_curspace += spacechange;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) dquot->dq_dqb.dqb_curinodes += inodechange;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /* Set properly space grace time... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (dquot->dq_dqb.dqb_bsoftlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) oldbtime > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (dquot->dq_dqb.dqb_btime > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) dquot->dq_dqb.dqb_btime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) min(dquot->dq_dqb.dqb_btime, oldbtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) dquot->dq_dqb.dqb_btime = oldbtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) dquot->dq_dqb.dqb_btime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) clear_bit(DQ_BLKS_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* Set properly inode grace time... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (dquot->dq_dqb.dqb_isoftlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) olditime > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (dquot->dq_dqb.dqb_itime > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) dquot->dq_dqb.dqb_itime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) min(dquot->dq_dqb.dqb_itime, olditime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dquot->dq_dqb.dqb_itime = olditime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) dquot->dq_dqb.dqb_itime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) clear_bit(DQ_INODES_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* All information is properly updated, clear the flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) spin_unlock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) err = ocfs2_qinfo_lock(info, freeing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) mlog(ML_ERROR, "Failed to lock quota info, losing quota write"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) " (type=%d, id=%u)\n", dquot->dq_id.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) (unsigned)from_kqid(&init_user_ns, dquot->dq_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (freeing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) OCFS2_DQUOT(dquot)->dq_use_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) err = qtree_write_dquot(&info->dqi_gi, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto out_qlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) err = qtree_release_dquot(&info->dqi_gi, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (info_dirty(sb_dqinfo(sb, type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) err2 = __ocfs2_global_write_info(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) out_qlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) ocfs2_qinfo_unlock(info, freeing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) mlog_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * Functions for periodic syncing of dquots with global file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct ocfs2_super *osb = OCFS2_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) dquot->dq_id.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) type, sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (type != dquot->dq_id.type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) status = ocfs2_lock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) goto out_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) down_write(&sb_dqopt(sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) status = ocfs2_sync_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /* We have to write local structure as well... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) status = ocfs2_local_write_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) up_write(&sb_dqopt(sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) out_ilock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) ocfs2_unlock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static void qsync_work_fn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct ocfs2_mem_dqinfo *oinfo = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct ocfs2_mem_dqinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) dqi_sync_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) struct super_block *sb = oinfo->dqi_gqinode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * We have to be careful here not to deadlock on s_umount as umount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * disabling quotas may be in progress and it waits for this work to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * complete. If trylock fails, we'll do the sync next time...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (down_read_trylock(&sb->s_umount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) up_read(&sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) schedule_delayed_work(&oinfo->dqi_sync_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) msecs_to_jiffies(oinfo->dqi_syncms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * Wrappers for generic quota functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int ocfs2_write_dquot(struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) dquot->dq_id.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) down_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) status = ocfs2_local_write_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) up_write(&sb_dqopt(dquot->dq_sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * We modify tree, leaf block, global info, local chunk header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * global and local inode; OCFS2_QINFO_WRITE_CREDITS already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * accounts for inode update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return (oinfo->dqi_gi.dqi_qtree_depth + 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) OCFS2_QUOTA_BLOCK_UPDATE_CREDITS +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) OCFS2_QINFO_WRITE_CREDITS +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) OCFS2_INODE_UPDATE_CREDITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) void ocfs2_drop_dquot_refs(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct ocfs2_super *osb = container_of(work, struct ocfs2_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) dquot_drop_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) struct llist_node *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) struct ocfs2_dquot *odquot, *next_odquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) list = llist_del_all(&osb->dquot_drop_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) llist_for_each_entry_safe(odquot, next_odquot, list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /* Drop the reference we acquired in ocfs2_dquot_release() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) dqput(&odquot->dq_dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * Called when the last reference to dquot is dropped. If we are called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * downconvert thread, we cannot do all the handling here because grabbing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * quota lock could deadlock (the node holding the quota lock could need some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * other cluster lock to proceed but with blocked downconvert thread we cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * release any lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static int ocfs2_release_dquot(struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct ocfs2_mem_dqinfo *oinfo =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) dquot->dq_id.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) mutex_lock(&dquot->dq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) /* Check whether we are not racing with some other dqget() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (dquot_is_busy(dquot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) /* Running from downconvert thread? Postpone quota processing to wq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (current == osb->dc_task) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * Grab our own reference to dquot and queue it for delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * dropping. Quota code rechecks after calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * ->release_dquot() and won't free dquot structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) dqgrab(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /* First entry on list -> queue work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) queue_work(osb->ocfs2_wq, &osb->dquot_drop_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) status = ocfs2_lock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) handle = ocfs2_start_trans(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) goto out_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) status = ocfs2_global_release_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) status = ocfs2_local_release_dquot(handle, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * If we fail here, we cannot do much as global structure is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * already released. So just complain...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * Clear dq_off so that we search for the structure in quota file next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * time we acquire it. The structure might be deleted and reallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * elsewhere by another node while our dquot structure is on freelist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) dquot->dq_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) out_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) out_ilock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) ocfs2_unlock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) mutex_unlock(&dquot->dq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * Read global dquot structure from disk or create it if it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * not exist. Also update use count of the global structure and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * create structure in node-local quota file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static int ocfs2_acquire_dquot(struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) int status = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) int ex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) struct ocfs2_super *osb = OCFS2_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) int type = dquot->dq_id.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) struct inode *gqinode = info->dqi_gqinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) int need_alloc = ocfs2_global_qinit_alloc(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) mutex_lock(&dquot->dq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * We need an exclusive lock, because we're going to update use count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * and instantiate possibly new dquot structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) status = ocfs2_lock_global_qf(info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) status = ocfs2_qinfo_lock(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) goto out_dq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * We always want to read dquot structure from disk because we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * know what happened with it while it was on freelist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) status = qtree_read_dquot(&info->dqi_gi, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) ocfs2_qinfo_unlock(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) goto out_dq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) OCFS2_DQUOT(dquot)->dq_use_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (!dquot->dq_off) { /* No real quota entry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) ex = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * Add blocks to quota file before we start a transaction since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * locking allocators ranks above a transaction start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) WARN_ON(journal_current_handle());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) status = ocfs2_extend_no_holes(gqinode, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) i_size_read(gqinode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) goto out_dq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) handle = ocfs2_start_trans(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) ocfs2_calc_global_qinit_credits(sb, type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) goto out_dq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) status = ocfs2_qinfo_lock(info, ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) status = qtree_write_dquot(&info->dqi_gi, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (ex && info_dirty(sb_dqinfo(sb, type))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) err = __ocfs2_global_write_info(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) status = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) ocfs2_qinfo_unlock(info, ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) out_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) out_dq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) ocfs2_unlock_global_qf(info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) status = ocfs2_create_local_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) mutex_unlock(&dquot->dq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) static int ocfs2_get_next_id(struct super_block *sb, struct kqid *qid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) int type = qid->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) trace_ocfs2_get_next_id(from_kqid(&init_user_ns, *qid), type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (!sb_has_quota_loaded(sb, type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) status = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) status = ocfs2_lock_global_qf(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) status = ocfs2_qinfo_lock(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) goto out_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) status = qtree_get_next_id(&info->dqi_gi, qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) ocfs2_qinfo_unlock(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) out_global:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) ocfs2_unlock_global_qf(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * Avoid logging ENOENT since it just means there isn't next ID and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * ESRCH which means quota isn't enabled for the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (status && status != -ENOENT && status != -ESRCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) (1 << (DQ_LASTSET_B + QIF_ITIME_B));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) int sync = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int type = dquot->dq_id.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct ocfs2_super *osb = OCFS2_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* In case user set some limits, sync dquot immediately to global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * quota file so that information propagates quicker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) spin_lock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (dquot->dq_flags & mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) sync = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) spin_unlock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) /* This is a slight hack but we can't afford getting global quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * lock if we already have a transaction started. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (!sync || journal_current_handle()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) status = ocfs2_write_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) status = ocfs2_lock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) goto out_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) down_write(&sb_dqopt(sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) status = ocfs2_sync_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) goto out_dlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) /* Now write updated local dquot structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) status = ocfs2_local_write_dquot(dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) out_dlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) up_write(&sb_dqopt(sb)->dqio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) out_ilock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) ocfs2_unlock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) /* This should happen only after set_dqinfo(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) static int ocfs2_write_info(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) status = ocfs2_lock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) goto out_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) status = dquot_commit_info(sb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) ocfs2_commit_trans(OCFS2_SB(sb), handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) out_ilock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) ocfs2_unlock_global_qf(oinfo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) struct ocfs2_dquot *dquot =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (!dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) return &dquot->dq_dquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) static void ocfs2_destroy_dquot(struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) kmem_cache_free(ocfs2_dquot_cachep, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) const struct dquot_operations ocfs2_quota_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) /* We never make dquot dirty so .write_dquot is never called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) .acquire_dquot = ocfs2_acquire_dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) .release_dquot = ocfs2_release_dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) .mark_dirty = ocfs2_mark_dquot_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) .write_info = ocfs2_write_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) .alloc_dquot = ocfs2_alloc_dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) .destroy_dquot = ocfs2_destroy_dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) .get_next_id = ocfs2_get_next_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) };