19e33d69fSJan Kara /* 29e33d69fSJan Kara * Implementation of operations over global quota file 39e33d69fSJan Kara */ 4171bf93cSMark Fasheh #include <linux/spinlock.h> 59e33d69fSJan Kara #include <linux/fs.h> 65a0e3ad6STejun Heo #include <linux/slab.h> 79e33d69fSJan Kara #include <linux/quota.h> 89e33d69fSJan Kara #include <linux/quotaops.h> 99e33d69fSJan Kara #include <linux/dqblk_qtree.h> 10171bf93cSMark Fasheh #include <linux/jiffies.h> 11171bf93cSMark Fasheh #include <linux/writeback.h> 12171bf93cSMark Fasheh #include <linux/workqueue.h> 13e3a767b6SJan Kara #include <linux/llist.h> 149e33d69fSJan Kara 159e33d69fSJan Kara #include <cluster/masklog.h> 169e33d69fSJan Kara 179e33d69fSJan Kara #include "ocfs2_fs.h" 189e33d69fSJan Kara #include "ocfs2.h" 199e33d69fSJan Kara #include "alloc.h" 20d6b32bbbSJoel Becker #include "blockcheck.h" 219e33d69fSJan Kara #include "inode.h" 229e33d69fSJan Kara #include "journal.h" 239e33d69fSJan Kara #include "file.h" 249e33d69fSJan Kara #include "sysfile.h" 259e33d69fSJan Kara #include "dlmglue.h" 269e33d69fSJan Kara #include "uptodate.h" 27ada50827SJan Kara #include "super.h" 28f64dd44eSJan Kara #include "buffer_head_io.h" 299e33d69fSJan Kara #include "quota.h" 301db986a8STao Ma #include "ocfs2_trace.h" 319e33d69fSJan Kara 32fb8dd8d7SJan Kara /* 33fb8dd8d7SJan Kara * Locking of quotas with OCFS2 is rather complex. Here are rules that 34fb8dd8d7SJan Kara * should be obeyed by all the functions: 35fb8dd8d7SJan Kara * - any write of quota structure (either to local or global file) is protected 36fb8dd8d7SJan Kara * by dqio_mutex or dquot->dq_lock. 37fb8dd8d7SJan Kara * - any modification of global quota file holds inode cluster lock, i_mutex, 38fb8dd8d7SJan Kara * and ip_alloc_sem of the global quota file (achieved by 39fb8dd8d7SJan Kara * ocfs2_lock_global_qf). It also has to hold qinfo_lock. 40fb8dd8d7SJan Kara * - an allocation of new blocks for local quota file is protected by 41fb8dd8d7SJan Kara * its ip_alloc_sem 42fb8dd8d7SJan Kara * 43fb8dd8d7SJan Kara * A rough sketch of locking dependencies (lf = local file, gf = global file): 44fb8dd8d7SJan Kara * Normal filesystem operation: 45fb8dd8d7SJan Kara * start_trans -> dqio_mutex -> write to lf 46fb8dd8d7SJan Kara * Syncing of local and global file: 47fb8dd8d7SJan Kara * ocfs2_lock_global_qf -> start_trans -> dqio_mutex -> qinfo_lock -> 48fb8dd8d7SJan Kara * write to gf 49fb8dd8d7SJan Kara * -> write to lf 50fb8dd8d7SJan Kara * Acquire dquot for the first time: 51fb8dd8d7SJan Kara * dq_lock -> ocfs2_lock_global_qf -> qinfo_lock -> read from gf 52fb8dd8d7SJan Kara * -> alloc space for gf 53fb8dd8d7SJan Kara * -> start_trans -> qinfo_lock -> write to gf 54fb8dd8d7SJan Kara * -> ip_alloc_sem of lf -> alloc space for lf 55fb8dd8d7SJan Kara * -> write to lf 56fb8dd8d7SJan Kara * Release last reference to dquot: 57fb8dd8d7SJan Kara * dq_lock -> ocfs2_lock_global_qf -> start_trans -> qinfo_lock -> write to gf 58fb8dd8d7SJan Kara * -> write to lf 59fb8dd8d7SJan Kara * Note that all the above operations also hold the inode cluster lock of lf. 60fb8dd8d7SJan Kara * Recovery: 61fb8dd8d7SJan Kara * inode cluster lock of recovered lf 62fb8dd8d7SJan Kara * -> read bitmaps -> ip_alloc_sem of lf 63fb8dd8d7SJan Kara * -> ocfs2_lock_global_qf -> start_trans -> dqio_mutex -> qinfo_lock -> 64fb8dd8d7SJan Kara * write to gf 65fb8dd8d7SJan Kara */ 66fb8dd8d7SJan Kara 67171bf93cSMark Fasheh static void qsync_work_fn(struct work_struct *work); 68171bf93cSMark Fasheh 699e33d69fSJan Kara static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp) 709e33d69fSJan Kara { 719e33d69fSJan Kara struct ocfs2_global_disk_dqblk *d = dp; 729e33d69fSJan Kara struct mem_dqblk *m = &dquot->dq_dqb; 739e33d69fSJan Kara 749e33d69fSJan Kara /* Update from disk only entries not set by the admin */ 759e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) { 769e33d69fSJan Kara m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit); 779e33d69fSJan Kara m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit); 789e33d69fSJan Kara } 799e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags)) 809e33d69fSJan Kara m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes); 819e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) { 829e33d69fSJan Kara m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit); 839e33d69fSJan Kara m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit); 849e33d69fSJan Kara } 859e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags)) 869e33d69fSJan Kara m->dqb_curspace = le64_to_cpu(d->dqb_curspace); 879e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags)) 889e33d69fSJan Kara m->dqb_btime = le64_to_cpu(d->dqb_btime); 899e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags)) 909e33d69fSJan Kara m->dqb_itime = le64_to_cpu(d->dqb_itime); 919e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count); 929e33d69fSJan Kara } 939e33d69fSJan Kara 949e33d69fSJan Kara static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot) 959e33d69fSJan Kara { 969e33d69fSJan Kara struct ocfs2_global_disk_dqblk *d = dp; 979e33d69fSJan Kara struct mem_dqblk *m = &dquot->dq_dqb; 989e33d69fSJan Kara 994c376dcaSEric W. Biederman d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id)); 1009e33d69fSJan Kara d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count); 1019e33d69fSJan Kara d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit); 1029e33d69fSJan Kara d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit); 1039e33d69fSJan Kara d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes); 1049e33d69fSJan Kara d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit); 1059e33d69fSJan Kara d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit); 1069e33d69fSJan Kara d->dqb_curspace = cpu_to_le64(m->dqb_curspace); 1079e33d69fSJan Kara d->dqb_btime = cpu_to_le64(m->dqb_btime); 1089e33d69fSJan Kara d->dqb_itime = cpu_to_le64(m->dqb_itime); 1097669f54cSJan Kara d->dqb_pad1 = d->dqb_pad2 = 0; 1109e33d69fSJan Kara } 1119e33d69fSJan Kara 1129e33d69fSJan Kara static int ocfs2_global_is_id(void *dp, struct dquot *dquot) 1139e33d69fSJan Kara { 1149e33d69fSJan Kara struct ocfs2_global_disk_dqblk *d = dp; 1159e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = 1164c376dcaSEric W. Biederman sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; 1179e33d69fSJan Kara 1189e33d69fSJan Kara if (qtree_entry_unused(&oinfo->dqi_gi, dp)) 1199e33d69fSJan Kara return 0; 1204c376dcaSEric W. Biederman 1214c376dcaSEric W. Biederman return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type, 1224c376dcaSEric W. Biederman le32_to_cpu(d->dqb_id)), 1234c376dcaSEric W. Biederman dquot->dq_id); 1249e33d69fSJan Kara } 1259e33d69fSJan Kara 126d1b98c23SJulia Lawall const struct qtree_fmt_operations ocfs2_global_ops = { 1279e33d69fSJan Kara .mem2disk_dqblk = ocfs2_global_mem2diskdqb, 1289e33d69fSJan Kara .disk2mem_dqblk = ocfs2_global_disk2memdqb, 1299e33d69fSJan Kara .is_id = ocfs2_global_is_id, 1309e33d69fSJan Kara }; 1319e33d69fSJan Kara 132fb8dd8d7SJan Kara int ocfs2_validate_quota_block(struct super_block *sb, struct buffer_head *bh) 133684ef278SJoel Becker { 134d6b32bbbSJoel Becker struct ocfs2_disk_dqtrailer *dqt = 135d6b32bbbSJoel Becker ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data); 136684ef278SJoel Becker 1371db986a8STao Ma trace_ocfs2_validate_quota_block((unsigned long long)bh->b_blocknr); 138684ef278SJoel Becker 139d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 140d6b32bbbSJoel Becker 141d6b32bbbSJoel Becker /* 142d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 143d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 144d6b32bbbSJoel Becker * local to this block. 145d6b32bbbSJoel Becker */ 146d6b32bbbSJoel Becker return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check); 147684ef278SJoel Becker } 148684ef278SJoel Becker 149f64dd44eSJan Kara int ocfs2_read_quota_phys_block(struct inode *inode, u64 p_block, 150f64dd44eSJan Kara struct buffer_head **bhp) 151f64dd44eSJan Kara { 152f64dd44eSJan Kara int rc; 153f64dd44eSJan Kara 154f64dd44eSJan Kara *bhp = NULL; 155f64dd44eSJan Kara rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, 1, bhp, 0, 156f64dd44eSJan Kara ocfs2_validate_quota_block); 157f64dd44eSJan Kara if (rc) 158f64dd44eSJan Kara mlog_errno(rc); 159f64dd44eSJan Kara return rc; 160f64dd44eSJan Kara } 161f64dd44eSJan Kara 1629e33d69fSJan Kara /* Read data from global quotafile - avoid pagecache and such because we cannot 1639e33d69fSJan Kara * afford acquiring the locks... We use quota cluster lock to serialize 1649e33d69fSJan Kara * operations. Caller is responsible for acquiring it. */ 1659e33d69fSJan Kara ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data, 1669e33d69fSJan Kara size_t len, loff_t off) 1679e33d69fSJan Kara { 1689e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 1699e33d69fSJan Kara struct inode *gqinode = oinfo->dqi_gqinode; 1709e33d69fSJan Kara loff_t i_size = i_size_read(gqinode); 1719e33d69fSJan Kara int offset = off & (sb->s_blocksize - 1); 1729e33d69fSJan Kara sector_t blk = off >> sb->s_blocksize_bits; 1739e33d69fSJan Kara int err = 0; 1749e33d69fSJan Kara struct buffer_head *bh; 1759e33d69fSJan Kara size_t toread, tocopy; 176fb8dd8d7SJan Kara u64 pblock = 0, pcount = 0; 1779e33d69fSJan Kara 1789e33d69fSJan Kara if (off > i_size) 1799e33d69fSJan Kara return 0; 1809e33d69fSJan Kara if (off + len > i_size) 1819e33d69fSJan Kara len = i_size - off; 1829e33d69fSJan Kara toread = len; 1839e33d69fSJan Kara while (toread > 0) { 184dad7d975SMark Fasheh tocopy = min_t(size_t, (sb->s_blocksize - offset), toread); 185fb8dd8d7SJan Kara if (!pcount) { 186fb8dd8d7SJan Kara err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, 187fb8dd8d7SJan Kara &pcount, NULL); 188fb8dd8d7SJan Kara if (err) { 189fb8dd8d7SJan Kara mlog_errno(err); 190fb8dd8d7SJan Kara return err; 191fb8dd8d7SJan Kara } 192fb8dd8d7SJan Kara } else { 193fb8dd8d7SJan Kara pcount--; 194fb8dd8d7SJan Kara pblock++; 195fb8dd8d7SJan Kara } 19685eb8b73SJoel Becker bh = NULL; 197fb8dd8d7SJan Kara err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh); 19885eb8b73SJoel Becker if (err) { 1999e33d69fSJan Kara mlog_errno(err); 2009e33d69fSJan Kara return err; 2019e33d69fSJan Kara } 2029e33d69fSJan Kara memcpy(data, bh->b_data + offset, tocopy); 2039e33d69fSJan Kara brelse(bh); 2049e33d69fSJan Kara offset = 0; 2059e33d69fSJan Kara toread -= tocopy; 2069e33d69fSJan Kara data += tocopy; 2079e33d69fSJan Kara blk++; 2089e33d69fSJan Kara } 2099e33d69fSJan Kara return len; 2109e33d69fSJan Kara } 2119e33d69fSJan Kara 2129e33d69fSJan Kara /* Write to quotafile (we know the transaction is already started and has 2139e33d69fSJan Kara * enough credits) */ 2149e33d69fSJan Kara ssize_t ocfs2_quota_write(struct super_block *sb, int type, 2159e33d69fSJan Kara const char *data, size_t len, loff_t off) 2169e33d69fSJan Kara { 2179e33d69fSJan Kara struct mem_dqinfo *info = sb_dqinfo(sb, type); 2189e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; 2199e33d69fSJan Kara struct inode *gqinode = oinfo->dqi_gqinode; 2209e33d69fSJan Kara int offset = off & (sb->s_blocksize - 1); 2219e33d69fSJan Kara sector_t blk = off >> sb->s_blocksize_bits; 222af09e51bSJan Kara int err = 0, new = 0, ja_type; 22385eb8b73SJoel Becker struct buffer_head *bh = NULL; 2249e33d69fSJan Kara handle_t *handle = journal_current_handle(); 225fb8dd8d7SJan Kara u64 pblock, pcount; 2269e33d69fSJan Kara 2279e33d69fSJan Kara if (!handle) { 2289e33d69fSJan Kara mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled " 2299e33d69fSJan Kara "because transaction was not started.\n", 2309e33d69fSJan Kara (unsigned long long)off, (unsigned long long)len); 2319e33d69fSJan Kara return -EIO; 2329e33d69fSJan Kara } 2339e33d69fSJan Kara if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) { 2349e33d69fSJan Kara WARN_ON(1); 2359e33d69fSJan Kara len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset; 2369e33d69fSJan Kara } 2379e33d69fSJan Kara 238f17c20ddSJunxiao Bi if (i_size_read(gqinode) < off + len) { 239b57ac2c4SJan Kara loff_t rounded_end = 240b57ac2c4SJan Kara ocfs2_align_bytes_to_blocks(sb, off + len); 241b57ac2c4SJan Kara 242fb8dd8d7SJan Kara /* Space is already allocated in ocfs2_acquire_dquot() */ 2439e33d69fSJan Kara err = ocfs2_simple_size_update(gqinode, 2449e33d69fSJan Kara oinfo->dqi_gqi_bh, 245b57ac2c4SJan Kara rounded_end); 2469e33d69fSJan Kara if (err < 0) 2479e33d69fSJan Kara goto out; 2489e33d69fSJan Kara new = 1; 2499e33d69fSJan Kara } 250fb8dd8d7SJan Kara err = ocfs2_extent_map_get_blocks(gqinode, blk, &pblock, &pcount, NULL); 251fb8dd8d7SJan Kara if (err) { 252fb8dd8d7SJan Kara mlog_errno(err); 253fb8dd8d7SJan Kara goto out; 254fb8dd8d7SJan Kara } 2559e33d69fSJan Kara /* Not rewriting whole block? */ 2569e33d69fSJan Kara if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) && 2579e33d69fSJan Kara !new) { 258fb8dd8d7SJan Kara err = ocfs2_read_quota_phys_block(gqinode, pblock, &bh); 259af09e51bSJan Kara ja_type = OCFS2_JOURNAL_ACCESS_WRITE; 260af09e51bSJan Kara } else { 261fb8dd8d7SJan Kara bh = sb_getblk(sb, pblock); 262fb8dd8d7SJan Kara if (!bh) 263fb8dd8d7SJan Kara err = -ENOMEM; 264af09e51bSJan Kara ja_type = OCFS2_JOURNAL_ACCESS_CREATE; 265af09e51bSJan Kara } 26685eb8b73SJoel Becker if (err) { 2679e33d69fSJan Kara mlog_errno(err); 268e9956faeSTao Ma goto out; 2699e33d69fSJan Kara } 2709e33d69fSJan Kara lock_buffer(bh); 2719e33d69fSJan Kara if (new) 2729e33d69fSJan Kara memset(bh->b_data, 0, sb->s_blocksize); 2739e33d69fSJan Kara memcpy(bh->b_data + offset, data, len); 2749e33d69fSJan Kara flush_dcache_page(bh->b_page); 275af09e51bSJan Kara set_buffer_uptodate(bh); 2769e33d69fSJan Kara unlock_buffer(bh); 2778cb471e8SJoel Becker ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh); 2780cf2f763SJoel Becker err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh, 2790cf2f763SJoel Becker ja_type); 280af09e51bSJan Kara if (err < 0) { 281af09e51bSJan Kara brelse(bh); 282af09e51bSJan Kara goto out; 283af09e51bSJan Kara } 284ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh); 2859e33d69fSJan Kara brelse(bh); 2869e33d69fSJan Kara out: 2879e33d69fSJan Kara if (err) { 2889e33d69fSJan Kara mlog_errno(err); 2899e33d69fSJan Kara return err; 2909e33d69fSJan Kara } 2919e33d69fSJan Kara gqinode->i_version++; 2929e33d69fSJan Kara ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh); 2939e33d69fSJan Kara return len; 2949e33d69fSJan Kara } 2959e33d69fSJan Kara 2969e33d69fSJan Kara int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex) 2979e33d69fSJan Kara { 2989e33d69fSJan Kara int status; 2999e33d69fSJan Kara struct buffer_head *bh = NULL; 3009e33d69fSJan Kara 3019e33d69fSJan Kara status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex); 3029e33d69fSJan Kara if (status < 0) 3039e33d69fSJan Kara return status; 3049e33d69fSJan Kara spin_lock(&dq_data_lock); 3059e33d69fSJan Kara if (!oinfo->dqi_gqi_count++) 3069e33d69fSJan Kara oinfo->dqi_gqi_bh = bh; 3079e33d69fSJan Kara else 3089e33d69fSJan Kara WARN_ON(bh != oinfo->dqi_gqi_bh); 3099e33d69fSJan Kara spin_unlock(&dq_data_lock); 310fb8dd8d7SJan Kara if (ex) { 3115955102cSAl Viro inode_lock(oinfo->dqi_gqinode); 312fb8dd8d7SJan Kara down_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem); 313fb8dd8d7SJan Kara } else { 314fb8dd8d7SJan Kara down_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem); 315fb8dd8d7SJan Kara } 3169e33d69fSJan Kara return 0; 3179e33d69fSJan Kara } 3189e33d69fSJan Kara 3199e33d69fSJan Kara void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex) 3209e33d69fSJan Kara { 321fb8dd8d7SJan Kara if (ex) { 322fb8dd8d7SJan Kara up_write(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem); 3235955102cSAl Viro inode_unlock(oinfo->dqi_gqinode); 324fb8dd8d7SJan Kara } else { 325fb8dd8d7SJan Kara up_read(&OCFS2_I(oinfo->dqi_gqinode)->ip_alloc_sem); 326fb8dd8d7SJan Kara } 3279e33d69fSJan Kara ocfs2_inode_unlock(oinfo->dqi_gqinode, ex); 3289e33d69fSJan Kara brelse(oinfo->dqi_gqi_bh); 3299e33d69fSJan Kara spin_lock(&dq_data_lock); 3309e33d69fSJan Kara if (!--oinfo->dqi_gqi_count) 3319e33d69fSJan Kara oinfo->dqi_gqi_bh = NULL; 3329e33d69fSJan Kara spin_unlock(&dq_data_lock); 3339e33d69fSJan Kara } 3349e33d69fSJan Kara 3359e33d69fSJan Kara /* Read information header from global quota file */ 3369e33d69fSJan Kara int ocfs2_global_read_info(struct super_block *sb, int type) 3379e33d69fSJan Kara { 3389e33d69fSJan Kara struct inode *gqinode = NULL; 33952362810SJan Kara unsigned int ino[OCFS2_MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE, 3409e33d69fSJan Kara GROUP_QUOTA_SYSTEM_INODE }; 3419e33d69fSJan Kara struct ocfs2_global_disk_dqinfo dinfo; 3429e33d69fSJan Kara struct mem_dqinfo *info = sb_dqinfo(sb, type); 3439e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; 344ae4f6ef1SJan Kara u64 pcount; 3459e33d69fSJan Kara int status; 3469e33d69fSJan Kara 3479e33d69fSJan Kara /* Read global header */ 3489e33d69fSJan Kara gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type], 3499e33d69fSJan Kara OCFS2_INVALID_SLOT); 3509e33d69fSJan Kara if (!gqinode) { 3519e33d69fSJan Kara mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n", 3529e33d69fSJan Kara type); 3539e33d69fSJan Kara status = -EINVAL; 3549e33d69fSJan Kara goto out_err; 3559e33d69fSJan Kara } 3569e33d69fSJan Kara oinfo->dqi_gi.dqi_sb = sb; 3579e33d69fSJan Kara oinfo->dqi_gi.dqi_type = type; 3589e33d69fSJan Kara ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo); 3599e33d69fSJan Kara oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk); 3609e33d69fSJan Kara oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops; 3619e33d69fSJan Kara oinfo->dqi_gqi_bh = NULL; 3629e33d69fSJan Kara oinfo->dqi_gqi_count = 0; 3639e33d69fSJan Kara oinfo->dqi_gqinode = gqinode; 3649e33d69fSJan Kara status = ocfs2_lock_global_qf(oinfo, 0); 3659e33d69fSJan Kara if (status < 0) { 3669e33d69fSJan Kara mlog_errno(status); 3679e33d69fSJan Kara goto out_err; 3689e33d69fSJan Kara } 369ae4f6ef1SJan Kara 370ae4f6ef1SJan Kara status = ocfs2_extent_map_get_blocks(gqinode, 0, &oinfo->dqi_giblk, 371ae4f6ef1SJan Kara &pcount, NULL); 372ae4f6ef1SJan Kara if (status < 0) 373ae4f6ef1SJan Kara goto out_unlock; 374ae4f6ef1SJan Kara 375ae4f6ef1SJan Kara status = ocfs2_qinfo_lock(oinfo, 0); 376ae4f6ef1SJan Kara if (status < 0) 377ae4f6ef1SJan Kara goto out_unlock; 3789e33d69fSJan Kara status = sb->s_op->quota_read(sb, type, (char *)&dinfo, 3799e33d69fSJan Kara sizeof(struct ocfs2_global_disk_dqinfo), 3809e33d69fSJan Kara OCFS2_GLOBAL_INFO_OFF); 381ae4f6ef1SJan Kara ocfs2_qinfo_unlock(oinfo, 0); 3829e33d69fSJan Kara ocfs2_unlock_global_qf(oinfo, 0); 3839e33d69fSJan Kara if (status != sizeof(struct ocfs2_global_disk_dqinfo)) { 3849e33d69fSJan Kara mlog(ML_ERROR, "Cannot read global quota info (%d).\n", 3859e33d69fSJan Kara status); 3869e33d69fSJan Kara if (status >= 0) 3879e33d69fSJan Kara status = -EIO; 3889e33d69fSJan Kara mlog_errno(status); 3899e33d69fSJan Kara goto out_err; 3909e33d69fSJan Kara } 3919e33d69fSJan Kara info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace); 3929e33d69fSJan Kara info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace); 3939e33d69fSJan Kara oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms); 3949e33d69fSJan Kara oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks); 3959e33d69fSJan Kara oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk); 3969e33d69fSJan Kara oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry); 3979e33d69fSJan Kara oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits; 3989e33d69fSJan Kara oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize - 3999e33d69fSJan Kara OCFS2_QBLK_RESERVED_SPACE; 4009e33d69fSJan Kara oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi); 401171bf93cSMark Fasheh INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn); 402316873c9STejun Heo schedule_delayed_work(&oinfo->dqi_sync_work, 4034539f1dfSJan Kara msecs_to_jiffies(oinfo->dqi_syncms)); 404171bf93cSMark Fasheh 4059e33d69fSJan Kara out_err: 4069e33d69fSJan Kara return status; 407ae4f6ef1SJan Kara out_unlock: 408ae4f6ef1SJan Kara ocfs2_unlock_global_qf(oinfo, 0); 409ae4f6ef1SJan Kara mlog_errno(status); 410ae4f6ef1SJan Kara goto out_err; 4119e33d69fSJan Kara } 4129e33d69fSJan Kara 4139e33d69fSJan Kara /* Write information to global quota file. Expects exlusive lock on quota 4149e33d69fSJan Kara * file inode and quota info */ 4159e33d69fSJan Kara static int __ocfs2_global_write_info(struct super_block *sb, int type) 4169e33d69fSJan Kara { 4179e33d69fSJan Kara struct mem_dqinfo *info = sb_dqinfo(sb, type); 4189e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; 4199e33d69fSJan Kara struct ocfs2_global_disk_dqinfo dinfo; 4209e33d69fSJan Kara ssize_t size; 4219e33d69fSJan Kara 4229e33d69fSJan Kara spin_lock(&dq_data_lock); 4239e33d69fSJan Kara info->dqi_flags &= ~DQF_INFO_DIRTY; 4249e33d69fSJan Kara dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace); 4259e33d69fSJan Kara dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace); 4269e33d69fSJan Kara spin_unlock(&dq_data_lock); 4279e33d69fSJan Kara dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms); 4289e33d69fSJan Kara dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks); 4299e33d69fSJan Kara dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk); 4309e33d69fSJan Kara dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry); 4319e33d69fSJan Kara size = sb->s_op->quota_write(sb, type, (char *)&dinfo, 4329e33d69fSJan Kara sizeof(struct ocfs2_global_disk_dqinfo), 4339e33d69fSJan Kara OCFS2_GLOBAL_INFO_OFF); 4349e33d69fSJan Kara if (size != sizeof(struct ocfs2_global_disk_dqinfo)) { 4359e33d69fSJan Kara mlog(ML_ERROR, "Cannot write global quota info structure\n"); 4369e33d69fSJan Kara if (size >= 0) 4379e33d69fSJan Kara size = -EIO; 4389e33d69fSJan Kara return size; 4399e33d69fSJan Kara } 4409e33d69fSJan Kara return 0; 4419e33d69fSJan Kara } 4429e33d69fSJan Kara 4439e33d69fSJan Kara int ocfs2_global_write_info(struct super_block *sb, int type) 4449e33d69fSJan Kara { 4459e33d69fSJan Kara int err; 4469e33d69fSJan Kara struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; 4479e33d69fSJan Kara 4489e33d69fSJan Kara err = ocfs2_qinfo_lock(info, 1); 4499e33d69fSJan Kara if (err < 0) 4509e33d69fSJan Kara return err; 4519e33d69fSJan Kara err = __ocfs2_global_write_info(sb, type); 4529e33d69fSJan Kara ocfs2_qinfo_unlock(info, 1); 4539e33d69fSJan Kara return err; 4549e33d69fSJan Kara } 4559e33d69fSJan Kara 456b409d7a0SJan Kara static int ocfs2_global_qinit_alloc(struct super_block *sb, int type) 457b409d7a0SJan Kara { 458b409d7a0SJan Kara struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 459b409d7a0SJan Kara 460b409d7a0SJan Kara /* 461b409d7a0SJan Kara * We may need to allocate tree blocks and a leaf block but not the 462b409d7a0SJan Kara * root block 463b409d7a0SJan Kara */ 464b409d7a0SJan Kara return oinfo->dqi_gi.dqi_qtree_depth; 465b409d7a0SJan Kara } 466b409d7a0SJan Kara 467b409d7a0SJan Kara static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type) 468b409d7a0SJan Kara { 469832d09cfSJan Kara /* We modify all the allocated blocks, tree root, info block and 470832d09cfSJan Kara * the inode */ 471b409d7a0SJan Kara return (ocfs2_global_qinit_alloc(sb, type) + 2) * 472832d09cfSJan Kara OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 1; 473b409d7a0SJan Kara } 474b409d7a0SJan Kara 4759e33d69fSJan Kara /* Sync local information about quota modifications with global quota file. 4769e33d69fSJan Kara * Caller must have started the transaction and obtained exclusive lock for 4779e33d69fSJan Kara * global quota file inode */ 4789e33d69fSJan Kara int __ocfs2_sync_dquot(struct dquot *dquot, int freeing) 4799e33d69fSJan Kara { 4809e33d69fSJan Kara int err, err2; 4819e33d69fSJan Kara struct super_block *sb = dquot->dq_sb; 4824c376dcaSEric W. Biederman int type = dquot->dq_id.type; 4839e33d69fSJan Kara struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; 4849e33d69fSJan Kara struct ocfs2_global_disk_dqblk dqblk; 4859e33d69fSJan Kara s64 spacechange, inodechange; 486e008bb61SArnd Bergmann time64_t olditime, oldbtime; 4879e33d69fSJan Kara 4889e33d69fSJan Kara err = sb->s_op->quota_read(sb, type, (char *)&dqblk, 4899e33d69fSJan Kara sizeof(struct ocfs2_global_disk_dqblk), 4909e33d69fSJan Kara dquot->dq_off); 4919e33d69fSJan Kara if (err != sizeof(struct ocfs2_global_disk_dqblk)) { 4929e33d69fSJan Kara if (err >= 0) { 4939e33d69fSJan Kara mlog(ML_ERROR, "Short read from global quota file " 4949e33d69fSJan Kara "(%u read)\n", err); 4959e33d69fSJan Kara err = -EIO; 4969e33d69fSJan Kara } 4979e33d69fSJan Kara goto out; 4989e33d69fSJan Kara } 4999e33d69fSJan Kara 5009e33d69fSJan Kara /* Update space and inode usage. Get also other information from 5019e33d69fSJan Kara * global quota file so that we don't overwrite any changes there. 5029e33d69fSJan Kara * We are */ 5039e33d69fSJan Kara spin_lock(&dq_data_lock); 5049e33d69fSJan Kara spacechange = dquot->dq_dqb.dqb_curspace - 5059e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_origspace; 5069e33d69fSJan Kara inodechange = dquot->dq_dqb.dqb_curinodes - 5079e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_originodes; 5089e33d69fSJan Kara olditime = dquot->dq_dqb.dqb_itime; 5099e33d69fSJan Kara oldbtime = dquot->dq_dqb.dqb_btime; 5109e33d69fSJan Kara ocfs2_global_disk2memdqb(dquot, &dqblk); 5114c376dcaSEric W. Biederman trace_ocfs2_sync_dquot(from_kqid(&init_user_ns, dquot->dq_id), 5124c376dcaSEric W. Biederman dquot->dq_dqb.dqb_curspace, 5131db986a8STao Ma (long long)spacechange, 5141db986a8STao Ma dquot->dq_dqb.dqb_curinodes, 5151db986a8STao Ma (long long)inodechange); 5169e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags)) 5179e33d69fSJan Kara dquot->dq_dqb.dqb_curspace += spacechange; 5189e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags)) 5199e33d69fSJan Kara dquot->dq_dqb.dqb_curinodes += inodechange; 5209e33d69fSJan Kara /* Set properly space grace time... */ 5219e33d69fSJan Kara if (dquot->dq_dqb.dqb_bsoftlimit && 5229e33d69fSJan Kara dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) { 5239e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) && 5249e33d69fSJan Kara oldbtime > 0) { 5259e33d69fSJan Kara if (dquot->dq_dqb.dqb_btime > 0) 5269e33d69fSJan Kara dquot->dq_dqb.dqb_btime = 5279e33d69fSJan Kara min(dquot->dq_dqb.dqb_btime, oldbtime); 5289e33d69fSJan Kara else 5299e33d69fSJan Kara dquot->dq_dqb.dqb_btime = oldbtime; 5309e33d69fSJan Kara } 5319e33d69fSJan Kara } else { 5329e33d69fSJan Kara dquot->dq_dqb.dqb_btime = 0; 5339e33d69fSJan Kara clear_bit(DQ_BLKS_B, &dquot->dq_flags); 5349e33d69fSJan Kara } 5359e33d69fSJan Kara /* Set properly inode grace time... */ 5369e33d69fSJan Kara if (dquot->dq_dqb.dqb_isoftlimit && 5379e33d69fSJan Kara dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) { 5389e33d69fSJan Kara if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) && 5399e33d69fSJan Kara olditime > 0) { 5409e33d69fSJan Kara if (dquot->dq_dqb.dqb_itime > 0) 5419e33d69fSJan Kara dquot->dq_dqb.dqb_itime = 5429e33d69fSJan Kara min(dquot->dq_dqb.dqb_itime, olditime); 5439e33d69fSJan Kara else 5449e33d69fSJan Kara dquot->dq_dqb.dqb_itime = olditime; 5459e33d69fSJan Kara } 5469e33d69fSJan Kara } else { 5479e33d69fSJan Kara dquot->dq_dqb.dqb_itime = 0; 5489e33d69fSJan Kara clear_bit(DQ_INODES_B, &dquot->dq_flags); 5499e33d69fSJan Kara } 5509e33d69fSJan Kara /* All information is properly updated, clear the flags */ 5519e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags); 5529e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags); 5539e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags); 5549e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags); 5559e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags); 5569e33d69fSJan Kara __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags); 5579e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace; 5589e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes; 5599e33d69fSJan Kara spin_unlock(&dq_data_lock); 5609e33d69fSJan Kara err = ocfs2_qinfo_lock(info, freeing); 5619e33d69fSJan Kara if (err < 0) { 56225985edcSLucas De Marchi mlog(ML_ERROR, "Failed to lock quota info, losing quota write" 5634c376dcaSEric W. Biederman " (type=%d, id=%u)\n", dquot->dq_id.type, 5644c376dcaSEric W. Biederman (unsigned)from_kqid(&init_user_ns, dquot->dq_id)); 5659e33d69fSJan Kara goto out; 5669e33d69fSJan Kara } 5679e33d69fSJan Kara if (freeing) 5689e33d69fSJan Kara OCFS2_DQUOT(dquot)->dq_use_count--; 5699e33d69fSJan Kara err = qtree_write_dquot(&info->dqi_gi, dquot); 5709e33d69fSJan Kara if (err < 0) 5719e33d69fSJan Kara goto out_qlock; 5729e33d69fSJan Kara if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) { 5739e33d69fSJan Kara err = qtree_release_dquot(&info->dqi_gi, dquot); 5749e33d69fSJan Kara if (info_dirty(sb_dqinfo(sb, type))) { 5759e33d69fSJan Kara err2 = __ocfs2_global_write_info(sb, type); 5769e33d69fSJan Kara if (!err) 5779e33d69fSJan Kara err = err2; 5789e33d69fSJan Kara } 5799e33d69fSJan Kara } 5809e33d69fSJan Kara out_qlock: 5819e33d69fSJan Kara ocfs2_qinfo_unlock(info, freeing); 5829e33d69fSJan Kara out: 5839e33d69fSJan Kara if (err < 0) 5849e33d69fSJan Kara mlog_errno(err); 5859e33d69fSJan Kara return err; 5869e33d69fSJan Kara } 5879e33d69fSJan Kara 5889e33d69fSJan Kara /* 589171bf93cSMark Fasheh * Functions for periodic syncing of dquots with global file 590171bf93cSMark Fasheh */ 591171bf93cSMark Fasheh static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type) 592171bf93cSMark Fasheh { 593171bf93cSMark Fasheh handle_t *handle; 594171bf93cSMark Fasheh struct super_block *sb = dquot->dq_sb; 595171bf93cSMark Fasheh struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 596171bf93cSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(sb); 597171bf93cSMark Fasheh int status = 0; 598171bf93cSMark Fasheh 5994c376dcaSEric W. Biederman trace_ocfs2_sync_dquot_helper(from_kqid(&init_user_ns, dquot->dq_id), 6004c376dcaSEric W. Biederman dquot->dq_id.type, 6011db986a8STao Ma type, sb->s_id); 6024c376dcaSEric W. Biederman if (type != dquot->dq_id.type) 603171bf93cSMark Fasheh goto out; 604171bf93cSMark Fasheh status = ocfs2_lock_global_qf(oinfo, 1); 605171bf93cSMark Fasheh if (status < 0) 606171bf93cSMark Fasheh goto out; 607171bf93cSMark Fasheh 608171bf93cSMark Fasheh handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS); 609171bf93cSMark Fasheh if (IS_ERR(handle)) { 610171bf93cSMark Fasheh status = PTR_ERR(handle); 611171bf93cSMark Fasheh mlog_errno(status); 612171bf93cSMark Fasheh goto out_ilock; 613171bf93cSMark Fasheh } 614171bf93cSMark Fasheh mutex_lock(&sb_dqopt(sb)->dqio_mutex); 615171bf93cSMark Fasheh status = ocfs2_sync_dquot(dquot); 616171bf93cSMark Fasheh if (status < 0) 617171bf93cSMark Fasheh mlog_errno(status); 618171bf93cSMark Fasheh /* We have to write local structure as well... */ 619741e1289SJan Kara status = ocfs2_local_write_dquot(dquot); 620171bf93cSMark Fasheh if (status < 0) 621171bf93cSMark Fasheh mlog_errno(status); 622741e1289SJan Kara mutex_unlock(&sb_dqopt(sb)->dqio_mutex); 623171bf93cSMark Fasheh ocfs2_commit_trans(osb, handle); 624171bf93cSMark Fasheh out_ilock: 625171bf93cSMark Fasheh ocfs2_unlock_global_qf(oinfo, 1); 626171bf93cSMark Fasheh out: 627171bf93cSMark Fasheh return status; 628171bf93cSMark Fasheh } 629171bf93cSMark Fasheh 630171bf93cSMark Fasheh static void qsync_work_fn(struct work_struct *work) 631171bf93cSMark Fasheh { 632171bf93cSMark Fasheh struct ocfs2_mem_dqinfo *oinfo = container_of(work, 633171bf93cSMark Fasheh struct ocfs2_mem_dqinfo, 634171bf93cSMark Fasheh dqi_sync_work.work); 635171bf93cSMark Fasheh struct super_block *sb = oinfo->dqi_gqinode->i_sb; 636171bf93cSMark Fasheh 6372a64f80eSJan Kara /* 6382a64f80eSJan Kara * We have to be careful here not to deadlock on s_umount as umount 6392a64f80eSJan Kara * disabling quotas may be in progress and it waits for this work to 6402a64f80eSJan Kara * complete. If trylock fails, we'll do the sync next time... 6412a64f80eSJan Kara */ 6422a64f80eSJan Kara if (down_read_trylock(&sb->s_umount)) { 643171bf93cSMark Fasheh dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type); 6442a64f80eSJan Kara up_read(&sb->s_umount); 6452a64f80eSJan Kara } 646316873c9STejun Heo schedule_delayed_work(&oinfo->dqi_sync_work, 6474539f1dfSJan Kara msecs_to_jiffies(oinfo->dqi_syncms)); 648171bf93cSMark Fasheh } 649171bf93cSMark Fasheh 650171bf93cSMark Fasheh /* 6519e33d69fSJan Kara * Wrappers for generic quota functions 6529e33d69fSJan Kara */ 6539e33d69fSJan Kara 6549e33d69fSJan Kara static int ocfs2_write_dquot(struct dquot *dquot) 6559e33d69fSJan Kara { 6569e33d69fSJan Kara handle_t *handle; 6579e33d69fSJan Kara struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb); 6589e33d69fSJan Kara int status = 0; 6599e33d69fSJan Kara 6604c376dcaSEric W. Biederman trace_ocfs2_write_dquot(from_kqid(&init_user_ns, dquot->dq_id), 6614c376dcaSEric W. Biederman dquot->dq_id.type); 6629e33d69fSJan Kara 6639e33d69fSJan Kara handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS); 6649e33d69fSJan Kara if (IS_ERR(handle)) { 6659e33d69fSJan Kara status = PTR_ERR(handle); 6669e33d69fSJan Kara mlog_errno(status); 6679e33d69fSJan Kara goto out; 6689e33d69fSJan Kara } 669741e1289SJan Kara mutex_lock(&sb_dqopt(dquot->dq_sb)->dqio_mutex); 670741e1289SJan Kara status = ocfs2_local_write_dquot(dquot); 671741e1289SJan Kara mutex_unlock(&sb_dqopt(dquot->dq_sb)->dqio_mutex); 6729e33d69fSJan Kara ocfs2_commit_trans(osb, handle); 6739e33d69fSJan Kara out: 6749e33d69fSJan Kara return status; 6759e33d69fSJan Kara } 6769e33d69fSJan Kara 677b409d7a0SJan Kara static int ocfs2_calc_qdel_credits(struct super_block *sb, int type) 6789e33d69fSJan Kara { 679b409d7a0SJan Kara struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 6800584974aSJan Kara /* 6810584974aSJan Kara * We modify tree, leaf block, global info, local chunk header, 6820584974aSJan Kara * global and local inode; OCFS2_QINFO_WRITE_CREDITS already 6830584974aSJan Kara * accounts for inode update 6840584974aSJan Kara */ 685b409d7a0SJan Kara return (oinfo->dqi_gi.dqi_qtree_depth + 2) * 686b409d7a0SJan Kara OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + 6870584974aSJan Kara OCFS2_QINFO_WRITE_CREDITS + 6880584974aSJan Kara OCFS2_INODE_UPDATE_CREDITS; 6899e33d69fSJan Kara } 6909e33d69fSJan Kara 691e3a767b6SJan Kara void ocfs2_drop_dquot_refs(struct work_struct *work) 692e3a767b6SJan Kara { 693e3a767b6SJan Kara struct ocfs2_super *osb = container_of(work, struct ocfs2_super, 694e3a767b6SJan Kara dquot_drop_work); 695e3a767b6SJan Kara struct llist_node *list; 696e3a767b6SJan Kara struct ocfs2_dquot *odquot, *next_odquot; 697e3a767b6SJan Kara 698e3a767b6SJan Kara list = llist_del_all(&osb->dquot_drop_list); 699e3a767b6SJan Kara llist_for_each_entry_safe(odquot, next_odquot, list, list) { 700e3a767b6SJan Kara /* Drop the reference we acquired in ocfs2_dquot_release() */ 701e3a767b6SJan Kara dqput(&odquot->dq_dquot); 702e3a767b6SJan Kara } 703e3a767b6SJan Kara } 704e3a767b6SJan Kara 705e3a767b6SJan Kara /* 706e3a767b6SJan Kara * Called when the last reference to dquot is dropped. If we are called from 707e3a767b6SJan Kara * downconvert thread, we cannot do all the handling here because grabbing 708e3a767b6SJan Kara * quota lock could deadlock (the node holding the quota lock could need some 709e3a767b6SJan Kara * other cluster lock to proceed but with blocked downconvert thread we cannot 710e3a767b6SJan Kara * release any lock). 711e3a767b6SJan Kara */ 7129e33d69fSJan Kara static int ocfs2_release_dquot(struct dquot *dquot) 7139e33d69fSJan Kara { 7149e33d69fSJan Kara handle_t *handle; 7159e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = 7164c376dcaSEric W. Biederman sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; 7179e33d69fSJan Kara struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb); 7189e33d69fSJan Kara int status = 0; 7199e33d69fSJan Kara 7204c376dcaSEric W. Biederman trace_ocfs2_release_dquot(from_kqid(&init_user_ns, dquot->dq_id), 7214c376dcaSEric W. Biederman dquot->dq_id.type); 7229e33d69fSJan Kara 723fb8dd8d7SJan Kara mutex_lock(&dquot->dq_lock); 724fb8dd8d7SJan Kara /* Check whether we are not racing with some other dqget() */ 725fb8dd8d7SJan Kara if (atomic_read(&dquot->dq_count) > 1) 726fb8dd8d7SJan Kara goto out; 727e3a767b6SJan Kara /* Running from downconvert thread? Postpone quota processing to wq */ 728e3a767b6SJan Kara if (current == osb->dc_task) { 729e3a767b6SJan Kara /* 730e3a767b6SJan Kara * Grab our own reference to dquot and queue it for delayed 731e3a767b6SJan Kara * dropping. Quota code rechecks after calling 732e3a767b6SJan Kara * ->release_dquot() and won't free dquot structure. 733e3a767b6SJan Kara */ 734e3a767b6SJan Kara dqgrab(dquot); 735e3a767b6SJan Kara /* First entry on list -> queue work */ 736e3a767b6SJan Kara if (llist_add(&OCFS2_DQUOT(dquot)->list, &osb->dquot_drop_list)) 73735ddf78eSjiangyiwen queue_work(osb->ocfs2_wq, &osb->dquot_drop_work); 738e3a767b6SJan Kara goto out; 739e3a767b6SJan Kara } 7409e33d69fSJan Kara status = ocfs2_lock_global_qf(oinfo, 1); 7419e33d69fSJan Kara if (status < 0) 7429e33d69fSJan Kara goto out; 7439e33d69fSJan Kara handle = ocfs2_start_trans(osb, 7444c376dcaSEric W. Biederman ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_id.type)); 7459e33d69fSJan Kara if (IS_ERR(handle)) { 7469e33d69fSJan Kara status = PTR_ERR(handle); 7479e33d69fSJan Kara mlog_errno(status); 7489e33d69fSJan Kara goto out_ilock; 7499e33d69fSJan Kara } 750fb8dd8d7SJan Kara 751fb8dd8d7SJan Kara status = ocfs2_global_release_dquot(dquot); 752fb8dd8d7SJan Kara if (status < 0) { 753fb8dd8d7SJan Kara mlog_errno(status); 754fb8dd8d7SJan Kara goto out_trans; 755fb8dd8d7SJan Kara } 756fb8dd8d7SJan Kara status = ocfs2_local_release_dquot(handle, dquot); 757fb8dd8d7SJan Kara /* 758fb8dd8d7SJan Kara * If we fail here, we cannot do much as global structure is 759fb8dd8d7SJan Kara * already released. So just complain... 760fb8dd8d7SJan Kara */ 761fb8dd8d7SJan Kara if (status < 0) 762fb8dd8d7SJan Kara mlog_errno(status); 76315c34a76SJan Kara /* 76415c34a76SJan Kara * Clear dq_off so that we search for the structure in quota file next 76515c34a76SJan Kara * time we acquire it. The structure might be deleted and reallocated 76615c34a76SJan Kara * elsewhere by another node while our dquot structure is on freelist. 76715c34a76SJan Kara */ 76815c34a76SJan Kara dquot->dq_off = 0; 769fb8dd8d7SJan Kara clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); 770fb8dd8d7SJan Kara out_trans: 7719e33d69fSJan Kara ocfs2_commit_trans(osb, handle); 7729e33d69fSJan Kara out_ilock: 7739e33d69fSJan Kara ocfs2_unlock_global_qf(oinfo, 1); 7749e33d69fSJan Kara out: 775fb8dd8d7SJan Kara mutex_unlock(&dquot->dq_lock); 776c1e8d35eSTao Ma if (status) 777c1e8d35eSTao Ma mlog_errno(status); 7789e33d69fSJan Kara return status; 7799e33d69fSJan Kara } 7809e33d69fSJan Kara 781fb8dd8d7SJan Kara /* 782fb8dd8d7SJan Kara * Read global dquot structure from disk or create it if it does 783fb8dd8d7SJan Kara * not exist. Also update use count of the global structure and 784fb8dd8d7SJan Kara * create structure in node-local quota file. 785fb8dd8d7SJan Kara */ 7869e33d69fSJan Kara static int ocfs2_acquire_dquot(struct dquot *dquot) 7879e33d69fSJan Kara { 788fb8dd8d7SJan Kara int status = 0, err; 789fb8dd8d7SJan Kara int ex = 0; 790fb8dd8d7SJan Kara struct super_block *sb = dquot->dq_sb; 791fb8dd8d7SJan Kara struct ocfs2_super *osb = OCFS2_SB(sb); 7924c376dcaSEric W. Biederman int type = dquot->dq_id.type; 793fb8dd8d7SJan Kara struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; 794fb8dd8d7SJan Kara struct inode *gqinode = info->dqi_gqinode; 795fb8dd8d7SJan Kara int need_alloc = ocfs2_global_qinit_alloc(sb, type); 796fb8dd8d7SJan Kara handle_t *handle; 7979e33d69fSJan Kara 7984c376dcaSEric W. Biederman trace_ocfs2_acquire_dquot(from_kqid(&init_user_ns, dquot->dq_id), 7994c376dcaSEric W. Biederman type); 800fb8dd8d7SJan Kara mutex_lock(&dquot->dq_lock); 801fb8dd8d7SJan Kara /* 802fb8dd8d7SJan Kara * We need an exclusive lock, because we're going to update use count 803fb8dd8d7SJan Kara * and instantiate possibly new dquot structure 804fb8dd8d7SJan Kara */ 805fb8dd8d7SJan Kara status = ocfs2_lock_global_qf(info, 1); 8069e33d69fSJan Kara if (status < 0) 8079e33d69fSJan Kara goto out; 808fb8dd8d7SJan Kara status = ocfs2_qinfo_lock(info, 0); 809fb8dd8d7SJan Kara if (status < 0) 810fb8dd8d7SJan Kara goto out_dq; 81115c34a76SJan Kara /* 81215c34a76SJan Kara * We always want to read dquot structure from disk because we don't 81315c34a76SJan Kara * know what happened with it while it was on freelist. 81415c34a76SJan Kara */ 815fb8dd8d7SJan Kara status = qtree_read_dquot(&info->dqi_gi, dquot); 816fb8dd8d7SJan Kara ocfs2_qinfo_unlock(info, 0); 817fb8dd8d7SJan Kara if (status < 0) 818fb8dd8d7SJan Kara goto out_dq; 819fb8dd8d7SJan Kara 820fb8dd8d7SJan Kara OCFS2_DQUOT(dquot)->dq_use_count++; 821fb8dd8d7SJan Kara OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace; 822fb8dd8d7SJan Kara OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes; 823fb8dd8d7SJan Kara if (!dquot->dq_off) { /* No real quota entry? */ 824fb8dd8d7SJan Kara ex = 1; 825fb8dd8d7SJan Kara /* 826fb8dd8d7SJan Kara * Add blocks to quota file before we start a transaction since 827fb8dd8d7SJan Kara * locking allocators ranks above a transaction start 828fb8dd8d7SJan Kara */ 829fb8dd8d7SJan Kara WARN_ON(journal_current_handle()); 8305693486bSJoel Becker status = ocfs2_extend_no_holes(gqinode, NULL, 831f17c20ddSJunxiao Bi i_size_read(gqinode) + (need_alloc << sb->s_blocksize_bits), 832f17c20ddSJunxiao Bi i_size_read(gqinode)); 833fb8dd8d7SJan Kara if (status < 0) 834fb8dd8d7SJan Kara goto out_dq; 835fb8dd8d7SJan Kara } 836fb8dd8d7SJan Kara 837fb8dd8d7SJan Kara handle = ocfs2_start_trans(osb, 838fb8dd8d7SJan Kara ocfs2_calc_global_qinit_credits(sb, type)); 839fb8dd8d7SJan Kara if (IS_ERR(handle)) { 840fb8dd8d7SJan Kara status = PTR_ERR(handle); 841fb8dd8d7SJan Kara goto out_dq; 842fb8dd8d7SJan Kara } 843fb8dd8d7SJan Kara status = ocfs2_qinfo_lock(info, ex); 844fb8dd8d7SJan Kara if (status < 0) 845fb8dd8d7SJan Kara goto out_trans; 846fb8dd8d7SJan Kara status = qtree_write_dquot(&info->dqi_gi, dquot); 847fb8dd8d7SJan Kara if (ex && info_dirty(sb_dqinfo(sb, type))) { 848fb8dd8d7SJan Kara err = __ocfs2_global_write_info(sb, type); 849fb8dd8d7SJan Kara if (!status) 850fb8dd8d7SJan Kara status = err; 851fb8dd8d7SJan Kara } 852fb8dd8d7SJan Kara ocfs2_qinfo_unlock(info, ex); 853fb8dd8d7SJan Kara out_trans: 854fb8dd8d7SJan Kara ocfs2_commit_trans(osb, handle); 855fb8dd8d7SJan Kara out_dq: 856fb8dd8d7SJan Kara ocfs2_unlock_global_qf(info, 1); 857fb8dd8d7SJan Kara if (status < 0) 858fb8dd8d7SJan Kara goto out; 859fb8dd8d7SJan Kara 860fb8dd8d7SJan Kara status = ocfs2_create_local_dquot(dquot); 861fb8dd8d7SJan Kara if (status < 0) 862fb8dd8d7SJan Kara goto out; 863fb8dd8d7SJan Kara set_bit(DQ_ACTIVE_B, &dquot->dq_flags); 8649e33d69fSJan Kara out: 865fb8dd8d7SJan Kara mutex_unlock(&dquot->dq_lock); 866c1e8d35eSTao Ma if (status) 867c1e8d35eSTao Ma mlog_errno(status); 8689e33d69fSJan Kara return status; 8699e33d69fSJan Kara } 8709e33d69fSJan Kara 8715a9530e4SJan Kara static int ocfs2_get_next_id(struct super_block *sb, struct kqid *qid) 8725a9530e4SJan Kara { 8735a9530e4SJan Kara int type = qid->type; 8745a9530e4SJan Kara struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; 8755a9530e4SJan Kara int status = 0; 8765a9530e4SJan Kara 8775a9530e4SJan Kara trace_ocfs2_get_next_id(from_kqid(&init_user_ns, *qid), type); 8788f9e8f5fSJan Kara if (!sb_has_quota_loaded(sb, type)) { 8798f9e8f5fSJan Kara status = -ESRCH; 8808f9e8f5fSJan Kara goto out; 8818f9e8f5fSJan Kara } 8825a9530e4SJan Kara status = ocfs2_lock_global_qf(info, 0); 8835a9530e4SJan Kara if (status < 0) 8845a9530e4SJan Kara goto out; 8855a9530e4SJan Kara status = ocfs2_qinfo_lock(info, 0); 8865a9530e4SJan Kara if (status < 0) 8875a9530e4SJan Kara goto out_global; 8885a9530e4SJan Kara status = qtree_get_next_id(&info->dqi_gi, qid); 8895a9530e4SJan Kara ocfs2_qinfo_unlock(info, 0); 8905a9530e4SJan Kara out_global: 8915a9530e4SJan Kara ocfs2_unlock_global_qf(info, 0); 8925a9530e4SJan Kara out: 8938f9e8f5fSJan Kara /* 8948f9e8f5fSJan Kara * Avoid logging ENOENT since it just means there isn't next ID and 8958f9e8f5fSJan Kara * ESRCH which means quota isn't enabled for the filesystem. 8968f9e8f5fSJan Kara */ 8978f9e8f5fSJan Kara if (status && status != -ENOENT && status != -ESRCH) 8985a9530e4SJan Kara mlog_errno(status); 8995a9530e4SJan Kara return status; 9005a9530e4SJan Kara } 9015a9530e4SJan Kara 9029e33d69fSJan Kara static int ocfs2_mark_dquot_dirty(struct dquot *dquot) 9039e33d69fSJan Kara { 9049e33d69fSJan Kara unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) | 9059e33d69fSJan Kara (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) | 9069e33d69fSJan Kara (1 << (DQ_LASTSET_B + QIF_INODES_B)) | 9079e33d69fSJan Kara (1 << (DQ_LASTSET_B + QIF_SPACE_B)) | 9089e33d69fSJan Kara (1 << (DQ_LASTSET_B + QIF_BTIME_B)) | 9099e33d69fSJan Kara (1 << (DQ_LASTSET_B + QIF_ITIME_B)); 9109e33d69fSJan Kara int sync = 0; 9119e33d69fSJan Kara int status; 9129e33d69fSJan Kara struct super_block *sb = dquot->dq_sb; 9134c376dcaSEric W. Biederman int type = dquot->dq_id.type; 9149e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 9159e33d69fSJan Kara handle_t *handle; 9169e33d69fSJan Kara struct ocfs2_super *osb = OCFS2_SB(sb); 9179e33d69fSJan Kara 9184c376dcaSEric W. Biederman trace_ocfs2_mark_dquot_dirty(from_kqid(&init_user_ns, dquot->dq_id), 9194c376dcaSEric W. Biederman type); 9209e33d69fSJan Kara 9219e33d69fSJan Kara /* In case user set some limits, sync dquot immediately to global 9229e33d69fSJan Kara * quota file so that information propagates quicker */ 9239e33d69fSJan Kara spin_lock(&dq_data_lock); 9249e33d69fSJan Kara if (dquot->dq_flags & mask) 9259e33d69fSJan Kara sync = 1; 9269e33d69fSJan Kara spin_unlock(&dq_data_lock); 927f8afead7SJan Kara /* This is a slight hack but we can't afford getting global quota 928f8afead7SJan Kara * lock if we already have a transaction started. */ 929f8afead7SJan Kara if (!sync || journal_current_handle()) { 9309e33d69fSJan Kara status = ocfs2_write_dquot(dquot); 9319e33d69fSJan Kara goto out; 9329e33d69fSJan Kara } 9339e33d69fSJan Kara status = ocfs2_lock_global_qf(oinfo, 1); 9349e33d69fSJan Kara if (status < 0) 9359e33d69fSJan Kara goto out; 9369e33d69fSJan Kara handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS); 9379e33d69fSJan Kara if (IS_ERR(handle)) { 9389e33d69fSJan Kara status = PTR_ERR(handle); 9399e33d69fSJan Kara mlog_errno(status); 9409e33d69fSJan Kara goto out_ilock; 9419e33d69fSJan Kara } 942fb8dd8d7SJan Kara mutex_lock(&sb_dqopt(sb)->dqio_mutex); 9439e33d69fSJan Kara status = ocfs2_sync_dquot(dquot); 9449e33d69fSJan Kara if (status < 0) { 9459e33d69fSJan Kara mlog_errno(status); 946741e1289SJan Kara goto out_dlock; 9479e33d69fSJan Kara } 9489e33d69fSJan Kara /* Now write updated local dquot structure */ 949741e1289SJan Kara status = ocfs2_local_write_dquot(dquot); 950741e1289SJan Kara out_dlock: 951741e1289SJan Kara mutex_unlock(&sb_dqopt(sb)->dqio_mutex); 9529e33d69fSJan Kara ocfs2_commit_trans(osb, handle); 9539e33d69fSJan Kara out_ilock: 9549e33d69fSJan Kara ocfs2_unlock_global_qf(oinfo, 1); 9559e33d69fSJan Kara out: 956c1e8d35eSTao Ma if (status) 957c1e8d35eSTao Ma mlog_errno(status); 9589e33d69fSJan Kara return status; 9599e33d69fSJan Kara } 9609e33d69fSJan Kara 9619e33d69fSJan Kara /* This should happen only after set_dqinfo(). */ 9629e33d69fSJan Kara static int ocfs2_write_info(struct super_block *sb, int type) 9639e33d69fSJan Kara { 9649e33d69fSJan Kara handle_t *handle; 9659e33d69fSJan Kara int status = 0; 9669e33d69fSJan Kara struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; 9679e33d69fSJan Kara 9689e33d69fSJan Kara status = ocfs2_lock_global_qf(oinfo, 1); 9699e33d69fSJan Kara if (status < 0) 9709e33d69fSJan Kara goto out; 9719e33d69fSJan Kara handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS); 9729e33d69fSJan Kara if (IS_ERR(handle)) { 9739e33d69fSJan Kara status = PTR_ERR(handle); 9749e33d69fSJan Kara mlog_errno(status); 9759e33d69fSJan Kara goto out_ilock; 9769e33d69fSJan Kara } 9779e33d69fSJan Kara status = dquot_commit_info(sb, type); 9789e33d69fSJan Kara ocfs2_commit_trans(OCFS2_SB(sb), handle); 9799e33d69fSJan Kara out_ilock: 9809e33d69fSJan Kara ocfs2_unlock_global_qf(oinfo, 1); 9819e33d69fSJan Kara out: 982c1e8d35eSTao Ma if (status) 983c1e8d35eSTao Ma mlog_errno(status); 9849e33d69fSJan Kara return status; 9859e33d69fSJan Kara } 9869e33d69fSJan Kara 9879e33d69fSJan Kara static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type) 9889e33d69fSJan Kara { 9899e33d69fSJan Kara struct ocfs2_dquot *dquot = 9909e33d69fSJan Kara kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS); 9919e33d69fSJan Kara 9929e33d69fSJan Kara if (!dquot) 9939e33d69fSJan Kara return NULL; 9949e33d69fSJan Kara return &dquot->dq_dquot; 9959e33d69fSJan Kara } 9969e33d69fSJan Kara 9979e33d69fSJan Kara static void ocfs2_destroy_dquot(struct dquot *dquot) 9989e33d69fSJan Kara { 9999e33d69fSJan Kara kmem_cache_free(ocfs2_dquot_cachep, dquot); 10009e33d69fSJan Kara } 10019e33d69fSJan Kara 100261e225dcSAlexey Dobriyan const struct dquot_operations ocfs2_quota_operations = { 1003741e1289SJan Kara /* We never make dquot dirty so .write_dquot is never called */ 10049e33d69fSJan Kara .acquire_dquot = ocfs2_acquire_dquot, 10059e33d69fSJan Kara .release_dquot = ocfs2_release_dquot, 10069e33d69fSJan Kara .mark_dirty = ocfs2_mark_dquot_dirty, 10079e33d69fSJan Kara .write_info = ocfs2_write_info, 10089e33d69fSJan Kara .alloc_dquot = ocfs2_alloc_dquot, 10099e33d69fSJan Kara .destroy_dquot = ocfs2_destroy_dquot, 10105a9530e4SJan Kara .get_next_id = ocfs2_get_next_id, 10119e33d69fSJan Kara }; 1012