From: Jan Kara Subject: [RFC] How to handle orphan list when journalling quota? (fwd) Date: Mon, 19 Jan 2004 23:00:38 +0100 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20040119220037.GA2861@atrey.karlin.mff.cuni.cz> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="SUOF0GtieIMvvwua" Return-path: Received: from atrey.karlin.mff.cuni.cz ([195.113.31.123]:44690 "EHLO atrey.karlin.mff.cuni.cz") by vger.kernel.org with ESMTP id S263130AbUASWAj (ORCPT ); Mon, 19 Jan 2004 17:00:39 -0500 To: linux-fsdevel@vger.kernel.org Content-Disposition: inline List-Id: linux-fsdevel.vger.kernel.org --SUOF0GtieIMvvwua Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Now the patch should be really attached :). Honza ----- Forwarded message from Jan Kara ----- Date: Mon, 19 Jan 2004 16:09:32 +0100 From: Jan Kara To: linux-fsdevel@vger.kernel.org Subject: [RFC] How to handle orphan list when journalling quota? User-Agent: Mutt/1.5.4i Hi, I'm working in journalled version of quotas so that you wouldn't need to run quotacheck after an unclean shutdown. I came across a following problem: When filesystem is shutdown in the middle of truncate the data are truncated later after replaying the log. Because at that time quotas are turned off they do not match. I have a few ideas how to solve this problem but I'd like to hear some opinions (expecially from ext3 people) on that: 1) filesystem would get (in mount options) the path to quota file and would turn on quotas while processing the orphan list 1b) the previous could be extended in such a way that ext3 would automatically turn on quotas when they are specified in mount options 2) ext3 will dump the changes of quotas to some file and quotaon would take care about the rest 3) the dump will be to some hidden inode and ext3 would care about syncing the quotas when quotaon(8) is run (and so the userspace told us the filename of quota file) 4) change the way we do quota journalling completely (currently I just journal the quota update in the same transaction as the i_blocks update) I like 1) probably the most but it needs some mount options passing which was not needed previously... Any opinions? Honza PS: I'm attaching the current version of quota journalling patch - I'd appreciate if someone who understands journalling code could have a look at it and tell me if there's something wrong... -- Jan Kara SuSE CR Labs ----- End forwarded message ----- -- Jan Kara SuSE CR Labs --SUOF0GtieIMvvwua Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="quota-2.6.0-1-jquota.diff" diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/dquot.c linux-2.6.0-um-1-jquota/fs/dquot.c --- linux-2.6.0-um/fs/dquot.c 2003-11-26 21:44:45.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/dquot.c 2004-01-18 20:33:25.000000000 +0100 @@ -259,6 +259,26 @@ up(&dquot->dq_lock); } +#define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot)) + +int dquot_mark_dquot_dirty(struct dquot *dquot) +{ + spin_lock(&dq_data_lock); + set_bit(DQF_ANY_DQUOT_DIRTY_B, &(sb_dqopt((dquot)->dq_sb)->info[(dquot)->dq_type].dqi_flags)); + set_bit(DQ_MOD_B, &(dquot)->dq_flags); + spin_unlock(&dq_data_lock); + return 0; +} + +int dquot_mark_info_dirty(struct super_block *sb, int type) +{ + spin_lock(&dq_data_lock); + set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags); + spin_unlock(&dq_data_lock); + return 0; +} + + static int read_dqblk(struct dquot *dquot) { int ret; @@ -272,13 +292,15 @@ return ret; } -static int commit_dqblk(struct dquot *dquot) +int commit_dqblk(struct dquot *dquot, int init) { - int ret; + int ret = 0; struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); down(&dqopt->dqio_sem); - ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); + clear_bit(DQ_MOD_B, &dquot->dq_flags); + if (!init || !dquot->dq_off) /* Isn't dquot already allocated? */ + ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot, init); up(&dqopt->dqio_sem); return ret; } @@ -314,7 +336,7 @@ spin_unlock(&dq_list_lock); } -static int vfs_quota_sync(struct super_block *sb, int type) +int vfs_quota_sync(struct super_block *sb, int type) { struct list_head *head; struct dquot *dquot; @@ -425,9 +447,11 @@ spin_unlock(&dq_list_lock); return; } + /* This should never happen when we are journalling quota */ if (dquot_dirty(dquot)) { spin_unlock(&dq_list_lock); - commit_dqblk(dquot); + printk(KERN_WARNING "Calling sync_dquot() from dqput().\n"); + dquot->dq_sb->dq_op->sync_dquot(dquot); goto we_slept; } atomic_dec(&dquot->dq_count); @@ -607,13 +631,11 @@ static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number) { dquot->dq_dqb.dqb_curinodes += number; - mark_dquot_dirty(dquot); } static inline void dquot_incr_space(struct dquot *dquot, qsize_t number) { dquot->dq_dqb.dqb_curspace += number; - mark_dquot_dirty(dquot); } static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number) @@ -625,7 +647,6 @@ if (dquot->dq_dqb.dqb_curinodes < dquot->dq_dqb.dqb_isoftlimit) dquot->dq_dqb.dqb_itime = (time_t) 0; clear_bit(DQ_INODES_B, &dquot->dq_flags); - mark_dquot_dirty(dquot); } static inline void dquot_decr_space(struct dquot *dquot, qsize_t number) @@ -637,7 +658,6 @@ if (toqb(dquot->dq_dqb.dqb_curspace) < dquot->dq_dqb.dqb_bsoftlimit) dquot->dq_dqb.dqb_btime = (time_t) 0; clear_bit(DQ_BLKS_B, &dquot->dq_flags); - mark_dquot_dirty(dquot); } static inline int need_print_warning(struct dquot *dquot) @@ -794,17 +814,19 @@ * * Note: this is a blocking operation. */ -void dquot_initialize(struct inode *inode, int type) +int dquot_initialize(struct inode *inode, int type) { unsigned int id = 0; - int cnt; + int cnt, ret = 0; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) + return 0; down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); /* Having dqptr_sem we know NOQUOTA flags can't be altered... */ - if (IS_NOQUOTA(inode)) { - up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); - return; - } + if (IS_NOQUOTA(inode)) + goto out_err; /* Build list of quotas to initialize... */ for (cnt = 0; cnt < MAXQUOTAS; cnt++) { if (type != -1 && cnt != type) @@ -819,18 +841,23 @@ break; } inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt); - if (inode->i_dquot[cnt]) + if (inode->i_dquot[cnt]) { inode->i_flags |= S_QUOTA; + /* Write dquot to allocate space in file if it's not allocated yet */ + commit_dqblk(inode->i_dquot[cnt], 1); + } } } +out_err: up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); + return ret; } /* * Remove references to quota from inode * This function needs dqptr_sem for writing */ -static void dquot_drop_iupdate(struct inode *inode, struct dquot **to_drop) +static int dquot_drop_iupdate(struct inode *inode, struct dquot **to_drop) { int cnt; @@ -839,22 +866,24 @@ to_drop[cnt] = inode->i_dquot[cnt]; inode->i_dquot[cnt] = NODQUOT; } + return 0; } /* * Release all quotas referenced by inode */ -void dquot_drop(struct inode *inode) +int dquot_drop(struct inode *inode) { struct dquot *to_drop[MAXQUOTAS]; - int cnt; + int cnt, ret; down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); - dquot_drop_iupdate(inode, to_drop); + ret = dquot_drop_iupdate(inode, to_drop); up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); for (cnt = 0; cnt < MAXQUOTAS; cnt++) if (to_drop[cnt] != NODQUOT) dqput(to_drop[cnt]); + return ret; } /* @@ -873,6 +902,15 @@ } /* + * Following four functions update i_blocks+i_bytes fields and + * quota information (togethter with appropriate checks) + * NOTE: We absolutely rely on the fact that caller dirtifies + * the inode (usually macros in quotaops.h care about this) and + * holds a handle for the current transaction so that dquot write and + * inode write go into the same transaction. + */ + +/* * This operation can block, but only after everything is updated */ int dquot_alloc_space(struct inode *inode, qsize_t number, int warn) @@ -880,13 +918,20 @@ int cnt, ret = NO_QUOTA; char warntype[MAXQUOTAS]; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) { +out_add: + inode_add_bytes(inode, number); + return QUOTA_OK; + } for (cnt = 0; cnt < MAXQUOTAS; cnt++) warntype[cnt] = NOWARN; down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); - if (IS_NOQUOTA(inode)) { + if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */ up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); - return QUOTA_OK; + goto out_add; } spin_lock(&dq_data_lock); for (cnt = 0; cnt < MAXQUOTAS; cnt++) { @@ -904,6 +949,11 @@ ret = QUOTA_OK; warn_put_all: spin_unlock(&dq_data_lock); + if (ret == QUOTA_OK) + /* Dirtify all the dquots - this can block when journalling */ + for (cnt = 0; cnt < MAXQUOTAS; cnt++) + if (inode->i_dquot[cnt]) + mark_dquot_dirty(inode->i_dquot[cnt]); flush_warnings(inode->i_dquot, warntype); up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); return ret; @@ -917,6 +967,10 @@ int cnt, ret = NO_QUOTA; char warntype[MAXQUOTAS]; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) + return QUOTA_OK; for (cnt = 0; cnt < MAXQUOTAS; cnt++) warntype[cnt] = NOWARN; down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); @@ -940,6 +994,11 @@ ret = QUOTA_OK; warn_put_all: spin_unlock(&dq_data_lock); + if (ret == QUOTA_OK) + /* Dirtify all the dquots - this can block when journalling */ + for (cnt = 0; cnt < MAXQUOTAS; cnt++) + if (inode->i_dquot[cnt]) + mark_dquot_dirty(inode->i_dquot[cnt]); flush_warnings((struct dquot **)inode->i_dquot, warntype); up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); return ret; @@ -948,14 +1007,21 @@ /* * This is a non-blocking operation. */ -void dquot_free_space(struct inode *inode, qsize_t number) +int dquot_free_space(struct inode *inode, qsize_t number) { unsigned int cnt; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) { +out_sub: + inode_sub_bytes(inode, number); + return QUOTA_OK; + } down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); if (IS_NOQUOTA(inode)) { up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); - return; + goto out_sub; } spin_lock(&dq_data_lock); for (cnt = 0; cnt < MAXQUOTAS; cnt++) { @@ -965,20 +1031,29 @@ } inode_sub_bytes(inode, number); spin_unlock(&dq_data_lock); + /* Dirtify all the dquots - this can block when journalling */ + for (cnt = 0; cnt < MAXQUOTAS; cnt++) + if (inode->i_dquot[cnt]) + mark_dquot_dirty(inode->i_dquot[cnt]); up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); + return QUOTA_OK; } /* * This is a non-blocking operation. */ -void dquot_free_inode(const struct inode *inode, unsigned long number) +int dquot_free_inode(const struct inode *inode, unsigned long number) { unsigned int cnt; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) + return QUOTA_OK; down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); if (IS_NOQUOTA(inode)) { up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); - return; + return QUOTA_OK; } spin_lock(&dq_data_lock); for (cnt = 0; cnt < MAXQUOTAS; cnt++) { @@ -987,7 +1062,12 @@ dquot_decr_inodes(inode->i_dquot[cnt], number); } spin_unlock(&dq_data_lock); + /* Dirtify all the dquots - this can block when journalling */ + for (cnt = 0; cnt < MAXQUOTAS; cnt++) + if (inode->i_dquot[cnt]) + mark_dquot_dirty(inode->i_dquot[cnt]); up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); + return QUOTA_OK; } /* @@ -1004,6 +1084,10 @@ chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid; char warntype[MAXQUOTAS]; + /* First test before acquiring semaphore - solves deadlocks when we + * re-enter the quota code and are already holding the semaphore */ + if (IS_NOQUOTA(inode)) + return QUOTA_OK; /* Clear the arrays */ for (cnt = 0; cnt < MAXQUOTAS; cnt++) { transfer_to[cnt] = transfer_from[cnt] = NODQUOT; @@ -1060,6 +1144,13 @@ ret = QUOTA_OK; warn_put_all: spin_unlock(&dq_data_lock); + /* Dirtify all the dquots - this can block when journalling */ + for (cnt = 0; cnt < MAXQUOTAS; cnt++) { + if (transfer_from[cnt]) + mark_dquot_dirty(transfer_from[cnt]); + if (transfer_to[cnt]) + mark_dquot_dirty(transfer_to[cnt]); + } flush_warnings(transfer_to, warntype); for (cnt = 0; cnt < MAXQUOTAS; cnt++) { @@ -1073,25 +1164,29 @@ } /* + * Write dquot to disk + */ +int dquot_commit(struct dquot *dquot) +{ + return commit_dqblk(dquot, 0); +} + +/* * Definitions of diskquota operations. */ struct dquot_operations dquot_operations = { - .initialize = dquot_initialize, /* mandatory */ - .drop = dquot_drop, /* mandatory */ + .initialize = dquot_initialize, + .drop = dquot_drop, .alloc_space = dquot_alloc_space, .alloc_inode = dquot_alloc_inode, .free_space = dquot_free_space, .free_inode = dquot_free_inode, .transfer = dquot_transfer, - .sync_dquot = commit_dqblk + .sync_dquot = dquot_commit, + .mark_dirty = dquot_mark_dquot_dirty, + .mark_info_dirty = dquot_mark_info_dirty }; -/* Function used by filesystems for initializing the dquot_operations structure */ -void init_dquot_operations(struct dquot_operations *fsdqops) -{ - memcpy(fsdqops, &dquot_operations, sizeof(dquot_operations)); -} - static inline void set_enable_flags(struct quota_info *dqopt, int type) { switch (type) { @@ -1116,6 +1211,19 @@ } } +/* + * Write info of quota file to disk + * This function must be called with dqio_sem held + */ +int dquot_commit_info(struct super_block *sb, int type) +{ + int ret; + struct quota_info *dqopt = sb_dqopt(sb); + + ret = dqopt->ops[type]->write_file_info(sb, type); + return ret; +} + /* Function in inode.c - remove pointers to dquots in icache */ extern void remove_dquot_ref(struct super_block *, int); @@ -1169,6 +1277,9 @@ return 0; } +/* + * Turn quotas on on a device + */ int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path) { struct file *f; @@ -1324,8 +1435,8 @@ clear_bit(DQ_FAKE_B, &dquot->dq_flags); else set_bit(DQ_FAKE_B, &dquot->dq_flags); - mark_dquot_dirty(dquot); spin_unlock(&dq_data_lock); + mark_dquot_dirty(dquot); } int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) @@ -1382,9 +1493,11 @@ mi->dqi_igrace = ii->dqi_igrace; if (ii->dqi_valid & IIF_FLAGS) mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK); - mark_info_dirty(mi); spin_unlock(&dq_data_lock); up_read(&sb_dqopt(sb)->dqptr_sem); + down(&sb_dqopt(sb)->dqio_sem); + mark_info_dirty(sb, type); + up(&sb_dqopt(sb)->dqio_sem); return 0; } diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/ext3/inode.c linux-2.6.0-um-1-jquota/fs/ext3/inode.c --- linux-2.6.0-um/fs/ext3/inode.c 2003-11-26 21:44:43.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/ext3/inode.c 2004-01-18 20:54:39.000000000 +0100 @@ -2772,9 +2772,25 @@ if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { + handle_t *handle; + + handle = ext3_journal_start(inode, 2*EXT3_QUOTA_TRANS_BLOCKS+3); + if (IS_ERR(handle)) { + error = PTR_ERR(handle); + goto err_out; + } error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0; - if (error) + if (error) { + ext3_journal_stop(handle); return error; + } + /* Update corresponding info in inode so that everything is in one transaction */ + if (attr->ia_valid & ATTR_UID) + inode->i_uid = attr->ia_uid; + if (attr->ia_valid & ATTR_GID) + inode->i_gid = attr->ia_gid; + error = ext3_mark_inode_dirty(handle, inode); + ext3_journal_stop(handle); } if (S_ISREG(inode->i_mode) && @@ -2853,7 +2869,10 @@ ret = 2 * (bpp + indirects) + 2; #ifdef CONFIG_QUOTA - ret += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS; + /* We can perform two writes to quota files (user + group files). + * We know that structure was already allocated during DQUOT_INIT so + * we will be only updating the data blocks + inodes */ + ret += 4; #endif return ret; @@ -3032,10 +3051,14 @@ * the inode's in-core data-journaling state flag now. */ - if (val) + if (val) { EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL; - else + ext3_set_aops(inode); + } + else { EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL; + ext3_set_aops(inode); + } journal_unlock_updates(journal); Binary files linux-2.6.0-um/fs/ext3/.inode.c.swp and linux-2.6.0-um-1-jquota/fs/ext3/.inode.c.swp differ diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/ext3/super.c linux-2.6.0-um-1-jquota/fs/ext3/super.c --- linux-2.6.0-um/fs/ext3/super.c 2003-11-26 21:45:32.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/ext3/super.c 2004-01-18 20:30:14.000000000 +0100 @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include #include #include "xattr.h" #include "acl.h" @@ -502,7 +505,34 @@ # define ext3_clear_inode NULL #endif -static struct dquot_operations ext3_qops; +#ifdef CONFIG_QUOTA +static int ext3_sync_dquot(struct dquot *dquot); +static int ext3_sync_info(struct super_block *sb, int type); +static int ext3_quota_on(struct super_block *sb, int type, int format_id, char *path); + +static struct dquot_operations ext3_quota_operations = { + .initialize = dquot_initialize, + .drop = dquot_drop, + .alloc_space = dquot_alloc_space, + .alloc_inode = dquot_alloc_inode, + .free_space = dquot_free_space, + .free_inode = dquot_free_inode, + .transfer = dquot_transfer, + .sync_dquot = ext3_sync_dquot, + .mark_dirty = ext3_sync_dquot, + .mark_info_dirty = ext3_sync_info +}; + +static struct quotactl_ops ext3_qctl_operations = { + .quota_on = ext3_quota_on, + .quota_off = vfs_quota_off, + .quota_sync = vfs_quota_sync, + .get_info = vfs_get_dqinfo, + .set_info = vfs_set_dqinfo, + .get_dqblk = vfs_get_dqblk, + .set_dqblk = vfs_set_dqblk +}; +#endif static struct super_operations ext3_sops = { .alloc_inode = ext3_alloc_inode, @@ -964,6 +994,7 @@ __FUNCTION__, inode->i_ino, inode->i_size); jbd_debug(2, "truncating inode %ld to %Ld bytes\n", inode->i_ino, inode->i_size); + printk(KERN_INFO "H: truncating %d blocks\n", inode->i_blocks); ext3_truncate(inode); nr_truncates++; } else { @@ -972,6 +1003,7 @@ __FUNCTION__, inode->i_ino); jbd_debug(2, "deleting unreferenced inode %ld\n", inode->i_ino); + printk(KERN_INFO "H: deleting %d blocks\n", inode->i_blocks); nr_orphans++; } iput(inode); /* The delete magic happens here! */ @@ -1291,7 +1323,10 @@ */ sb->s_op = &ext3_sops; sb->s_export_op = &ext3_export_ops; - sb->dq_op = &ext3_qops; +#ifdef CONFIG_QUOTA + sb->dq_op = &ext3_quota_operations; + sb->s_qcop = &ext3_qctl_operations; +#endif INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ sb->s_root = 0; @@ -1939,45 +1974,69 @@ #ifdef CONFIG_QUOTA -/* Blocks: (2 data blocks) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */ -#define EXT3_OLD_QFMT_BLOCKS 11 -/* Blocks: quota info + (4 pointer blocks + 1 entry block) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */ -#define EXT3_V0_QFMT_BLOCKS 27 +static inline int writes_to_blocks(int writes) +{ + return writes*5+2; +} -static int (*old_sync_dquot)(struct dquot *dquot); +static inline struct inode *dquot_to_inode(struct dquot *dquot) +{ + return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]->f_dentry->d_inode; +} static int ext3_sync_dquot(struct dquot *dquot) { - int nblocks; - int ret; - int err; + int ret, err; handle_t *handle; - struct quota_info *dqops = sb_dqopt(dquot->dq_sb); - struct inode *qinode; - switch (dqops->info[dquot->dq_type].dqi_format->qf_fmt_id) { - case QFMT_VFS_OLD: - nblocks = EXT3_OLD_QFMT_BLOCKS; - break; - case QFMT_VFS_V0: - nblocks = EXT3_V0_QFMT_BLOCKS; - break; - default: - nblocks = EXT3_MAX_TRANS_DATA; + if (!current->journal_info) + printk(KERN_INFO "H: transaction not started at ext3_sync_dquot()\n"); + handle = ext3_journal_start(dquot_to_inode(dquot), EXT3_QUOTA_TRANS_BLOCKS); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + goto out; } - qinode = dqops->files[dquot->dq_type]->f_dentry->d_inode; - handle = ext3_journal_start(qinode, nblocks); + ret = dquot_commit(dquot); + err = ext3_journal_stop(handle); + if (!ret) + ret = err; +out: + return ret; +} + +static int ext3_sync_info(struct super_block *sb, int type) +{ + int ret, err; + handle_t *handle; + + handle = ext3_journal_start(sb->s_root->d_inode, writes_to_blocks(1)); if (IS_ERR(handle)) { ret = PTR_ERR(handle); goto out; } - ret = old_sync_dquot(dquot); + ret = dquot_commit_info(sb, type); err = ext3_journal_stop(handle); - if (ret == 0) + if (!ret) ret = err; out: return ret; } + +static int ext3_quota_on(struct super_block *sb, int type, int format_id, char *path) +{ + int err; + struct nameidata nd; + + err = path_lookup(path, LOOKUP_FOLLOW, &nd); + if (err) + return err; + if (nd.mnt->mnt_sb != sb) /* Quotafile not on the same filesystem? */ + return -EXDEV; + if (!(EXT3_I(nd.dentry->d_inode)->i_flags & EXT3_JOURNAL_DATA_FL) && test_opt(sb, DATA_FLAGS) != EXT3_MOUNT_JOURNAL_DATA) + printk(KERN_WARNING "VFS: Quota file does not have data-journalling flag set. Journalled quota will not work.\n"); + return vfs_quota_on(sb, type, format_id, path); +} + #endif static struct super_block *ext3_get_sb(struct file_system_type *fs_type, @@ -2002,11 +2061,6 @@ err = init_inodecache(); if (err) goto out1; -#ifdef CONFIG_QUOTA - init_dquot_operations(&ext3_qops); - old_sync_dquot = ext3_qops.sync_dquot; - ext3_qops.sync_dquot = ext3_sync_dquot; -#endif err = register_filesystem(&ext3_fs_type); if (err) goto out; diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/quota_v1.c linux-2.6.0-um-1-jquota/fs/quota_v1.c --- linux-2.6.0-um/fs/quota_v1.c 2003-11-26 21:44:57.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/quota_v1.c 2003-12-21 21:46:35.000000000 +0100 @@ -60,13 +60,13 @@ v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk); if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 && dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0) - dquot->dq_flags |= DQ_FAKE; + set_bit(DQ_FAKE_B, &dquot->dq_flags); dqstats.reads++; return 0; } -static int v1_commit_dqblk(struct dquot *dquot) +static int v1_commit_dqblk(struct dquot *dquot, int init) { short type = dquot->dq_type; struct file *filp; @@ -80,12 +80,7 @@ fs = get_fs(); set_fs(KERNEL_DS); - /* - * Note: clear the DQ_MOD flag unconditionally, - * so we don't loop forever on failure. - */ v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb); - dquot->dq_flags &= ~DQ_MOD; if (dquot->dq_id == 0) { dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace; dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace; diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/quota_v2.c linux-2.6.0-um-1-jquota/fs/quota_v2.c --- linux-2.6.0-um/fs/quota_v2.c 2003-11-26 21:45:20.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/quota_v2.c 2004-01-05 20:21:52.000000000 +0100 @@ -173,9 +173,10 @@ } /* Remove empty block from list and return it */ -static int get_free_dqblk(struct file *filp, struct mem_dqinfo *info) +static int get_free_dqblk(struct file *filp, int type) { dqbuf_t buf = getdqbuf(); + struct mem_dqinfo *info = sb_dqinfo(filp->f_dentry->d_sb, type); struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf; int ret, blk; @@ -193,7 +194,7 @@ goto out_buf; blk = info->u.v2_i.dqi_blocks++; } - mark_info_dirty(info); + mark_info_dirty(filp->f_dentry->d_sb, type); ret = blk; out_buf: freedqbuf(buf); @@ -201,8 +202,9 @@ } /* Insert empty block to the list */ -static int put_free_dqblk(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk) +static int put_free_dqblk(struct file *filp, int type, dqbuf_t buf, uint blk) { + struct mem_dqinfo *info = sb_dqinfo(filp->f_dentry->d_sb, type); struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf; int err; @@ -210,16 +212,17 @@ dh->dqdh_prev_free = cpu_to_le32(0); dh->dqdh_entries = cpu_to_le16(0); info->u.v2_i.dqi_free_blk = blk; - mark_info_dirty(info); + mark_info_dirty(filp->f_dentry->d_sb, type); if ((err = write_blk(filp, blk, buf)) < 0) /* Some strange block. We had better leave it... */ return err; return 0; } /* Remove given block from the list of blocks with free entries */ -static int remove_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk) +static int remove_free_dqentry(struct file *filp, int type, dqbuf_t buf, uint blk) { dqbuf_t tmpbuf = getdqbuf(); + struct mem_dqinfo *info = sb_dqinfo(filp->f_dentry->d_sb, type); struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf; uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk = le32_to_cpu(dh->dqdh_prev_free); int err; @@ -242,7 +245,7 @@ } else { info->u.v2_i.dqi_free_entry = nextblk; - mark_info_dirty(info); + mark_info_dirty(filp->f_dentry->d_sb, type); } freedqbuf(tmpbuf); dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0); @@ -255,9 +258,10 @@ } /* Insert given block to the beginning of list with free entries */ -static int insert_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk) +static int insert_free_dqentry(struct file *filp, int type, dqbuf_t buf, uint blk) { dqbuf_t tmpbuf = getdqbuf(); + struct mem_dqinfo *info = sb_dqinfo(filp->f_dentry->d_sb, type); struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf; int err; @@ -276,7 +280,7 @@ } freedqbuf(tmpbuf); info->u.v2_i.dqi_free_entry = blk; - mark_info_dirty(info); + mark_info_dirty(filp->f_dentry->d_sb, type); return 0; out_buf: freedqbuf(tmpbuf); @@ -307,7 +311,7 @@ goto out_buf; } else { - blk = get_free_dqblk(filp, info); + blk = get_free_dqblk(filp, dquot->dq_type); if ((int)blk < 0) { *err = blk; freedqbuf(buf); @@ -315,10 +319,10 @@ } memset(buf, 0, V2_DQBLKSIZE); info->u.v2_i.dqi_free_entry = blk; /* This is enough as block is already zeroed and entry list is empty... */ - mark_info_dirty(info); + mark_info_dirty(dquot->dq_sb, dquot->dq_type); } if (le16_to_cpu(dh->dqdh_entries)+1 >= V2_DQSTRINBLK) /* Block will be full? */ - if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) { + if ((*err = remove_free_dqentry(filp, dquot->dq_type, buf, blk)) < 0) { printk(KERN_ERR "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n", blk); goto out_buf; } @@ -349,7 +353,6 @@ static int do_insert_tree(struct dquot *dquot, uint *treeblk, int depth) { struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]; - struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type; dqbuf_t buf; int ret = 0, newson = 0, newact = 0; u32 *ref; @@ -358,7 +361,7 @@ if (!(buf = getdqbuf())) return -ENOMEM; if (!*treeblk) { - ret = get_free_dqblk(filp, info); + ret = get_free_dqblk(filp, dquot->dq_type); if (ret < 0) goto out_buf; *treeblk = ret; @@ -392,7 +395,7 @@ ret = write_blk(filp, *treeblk, buf); } else if (newact && ret < 0) - put_free_dqblk(filp, info, buf, *treeblk); + put_free_dqblk(filp, dquot->dq_type, buf, *treeblk); out_buf: freedqbuf(buf); return ret; @@ -417,6 +420,7 @@ ssize_t ret; struct v2_disk_dqblk ddquot; + /* dq_off is guarded by dqio_sem */ if (!dquot->dq_off) if ((ret = dq_insert_tree(dquot)) < 0) { printk(KERN_ERR "VFS: Error %Zd occurred while creating quota.\n", ret); @@ -424,7 +428,9 @@ } filp = sb_dqopt(dquot->dq_sb)->files[type]; offset = dquot->dq_off; + spin_lock(&dq_data_lock); mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id); + spin_unlock(&dq_data_lock); fs = get_fs(); set_fs(KERNEL_DS); ret = filp->f_op->write(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset); @@ -445,7 +451,6 @@ static int free_dqentry(struct dquot *dquot, uint blk) { struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]; - struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type; struct v2_disk_dqdbheader *dh; dqbuf_t buf = getdqbuf(); int ret = 0; @@ -463,8 +468,8 @@ dh = (struct v2_disk_dqdbheader *)buf; dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)-1); if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */ - if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 || - (ret = put_free_dqblk(filp, info, buf, blk)) < 0) { + if ((ret = remove_free_dqentry(filp, dquot->dq_type, buf, blk)) < 0 || + (ret = put_free_dqblk(filp, dquot->dq_type, buf, blk)) < 0) { printk(KERN_ERR "VFS: Can't move quota data block (%u) to free list.\n", blk); goto out_buf; } @@ -473,7 +478,7 @@ memset(buf+(dquot->dq_off & ((1 << V2_DQBLKSIZE_BITS)-1)), 0, sizeof(struct v2_disk_dqblk)); if (le16_to_cpu(dh->dqdh_entries) == V2_DQSTRINBLK-1) { /* Insert will write block itself */ - if ((ret = insert_free_dqentry(filp, info, buf, blk)) < 0) { + if ((ret = insert_free_dqentry(filp, dquot->dq_type, buf, blk)) < 0) { printk(KERN_ERR "VFS: Can't insert quota data block (%u) to free entry list.\n", blk); goto out_buf; } @@ -494,7 +499,6 @@ static int remove_tree(struct dquot *dquot, uint *blk, int depth) { struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]; - struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type; dqbuf_t buf = getdqbuf(); int ret = 0; uint newblk; @@ -518,7 +522,7 @@ ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0); for (i = 0; i < V2_DQBLKSIZE && !buf[i]; i++); /* Block got empty? */ if (i == V2_DQBLKSIZE) { - put_free_dqblk(filp, info, buf, *blk); + put_free_dqblk(filp, dquot->dq_type, buf, *blk); *blk = 0; } else @@ -632,7 +636,7 @@ if (offset < 0) printk(KERN_ERR "VFS: Can't read quota structure for id %u.\n", dquot->dq_id); dquot->dq_off = 0; - dquot->dq_flags |= DQ_FAKE; + set_bit(DQ_FAKE_B, &dquot->dq_flags); memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk)); ret = offset; } @@ -657,11 +661,14 @@ } /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake one and user has no blocks... */ -static int v2_commit_dquot(struct dquot *dquot) +static int v2_commit_dquot(struct dquot *dquot, int init) { - /* We clear the flag everytime so we don't loop when there was an IO error... */ - dquot->dq_flags &= ~DQ_MOD; - if (dquot->dq_flags & DQ_FAKE && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace)) + int test; + + spin_lock(&dq_data_lock); + test = !init && test_bit(DQ_FAKE_B, &dquot->dq_flags) && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace); + spin_unlock(&dq_data_lock); + if (test) return v2_delete_dquot(dquot); else return v2_write_dquot(dquot); diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/fs/stat.c linux-2.6.0-um-1-jquota/fs/stat.c --- linux-2.6.0-um/fs/stat.c 2003-11-26 21:43:09.000000000 +0100 +++ linux-2.6.0-um-1-jquota/fs/stat.c 2004-01-18 19:43:46.000000000 +0100 @@ -397,6 +397,7 @@ void inode_set_bytes(struct inode *inode, loff_t bytes) { + /* Caller is here resposible for sufficient locking (ie. inode->i_lock) */ inode->i_blocks = bytes >> 9; inode->i_bytes = bytes & 511; } diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/include/linux/ext3_jbd.h linux-2.6.0-um-1-jquota/include/linux/ext3_jbd.h --- linux-2.6.0-um/include/linux/ext3_jbd.h 2003-11-26 21:42:56.000000000 +0100 +++ linux-2.6.0-um-1-jquota/include/linux/ext3_jbd.h 2004-01-11 23:15:51.000000000 +0100 @@ -42,8 +42,9 @@ * superblock only gets updated once, of course, so don't bother * counting that again for the quota updates. */ -#define EXT3_DATA_TRANS_BLOCKS (3 * EXT3_SINGLEDATA_TRANS_BLOCKS + \ - EXT3_XATTR_TRANS_BLOCKS - 2) +#define EXT3_DATA_TRANS_BLOCKS (EXT3_SINGLEDATA_TRANS_BLOCKS + \ + EXT3_XATTR_TRANS_BLOCKS - 2 + \ + 2*EXT3_QUOTA_TRANS_BLOCKS) extern int ext3_writepage_trans_blocks(struct inode *inode); @@ -72,6 +73,15 @@ #define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8 +#ifdef CONFIG_QUOTA +/* Amount of blocks needed for quota update. We are generous here as quota + * writes will use less than EXT3_SINGLEDATA_TRANS_BLOCKS but we probably + * don't care... */ +#define EXT3_QUOTA_TRANS_BLOCKS (EXT3_SINGLEDATA_TRANS_BLOCKS*DQUOT_MAX_WRITES) +#else +#define EXT3_QUOTA_TRANS_BLOCKS 0 +#endif + int ext3_mark_iloc_dirty(handle_t *handle, struct inode *inode, @@ -221,7 +231,7 @@ static inline int ext3_should_order_data(struct inode *inode) { - return (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA); + return (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA) && !(EXT3_I(inode)->i_flags & EXT3_JOURNAL_DATA_FL); } static inline int ext3_should_writeback_data(struct inode *inode) diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/include/linux/quota.h linux-2.6.0-um-1-jquota/include/linux/quota.h --- linux-2.6.0-um/include/linux/quota.h 2003-11-26 21:44:57.000000000 +0100 +++ linux-2.6.0-um-1-jquota/include/linux/quota.h 2004-01-18 20:43:02.000000000 +0100 @@ -138,6 +138,9 @@ #include #include +/* Maximal numbers of writes for quota operations (over all formats) */ +#define DQUOT_MAX_WRITES 7 /* Number of writes needed when quota structure is written */ + /* * Data for one user/group kept in memory */ @@ -174,16 +177,13 @@ #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */ #define DQF_ANY_DQUOT_DIRTY (1 << DQF_ANY_DQUOT_DIRTY_B) /* Is any dquot dirty? */ -extern inline void mark_info_dirty(struct mem_dqinfo *info) -{ - set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags); -} - +#define mark_info_dirty(sb, type) ((sb)->dq_op->mark_info_dirty((sb), (type))) #define info_dirty(info) test_bit(DQF_INFO_DIRTY_B, &(info)->dqi_flags) #define info_any_dquot_dirty(info) test_bit(DQF_ANY_DQUOT_DIRTY_B, &(info)->dqi_flags) #define info_any_dirty(info) (info_dirty(info) || info_any_dquot_dirty(info)) #define sb_dqopt(sb) (&(sb)->s_dquot) +#define sb_dqinfo(sb, type) (sb_dqopt(sb)->info+(type)) struct dqstats { int lookups; @@ -238,19 +238,21 @@ int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */ int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */ int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */ - int (*commit_dqblk)(struct dquot *dquot); /* Write (or delete) structure for one user */ + int (*commit_dqblk)(struct dquot *dquot, int init); /* Write (or delete) structure for one user */ }; /* Operations working with dquots */ struct dquot_operations { - void (*initialize) (struct inode *, int); - void (*drop) (struct inode *); + int (*initialize) (struct inode *, int); + int (*drop) (struct inode *); int (*alloc_space) (struct inode *, qsize_t, int); int (*alloc_inode) (const struct inode *, unsigned long); - void (*free_space) (struct inode *, qsize_t); - void (*free_inode) (const struct inode *, unsigned long); + int (*free_space) (struct inode *, qsize_t); + int (*free_inode) (const struct inode *, unsigned long); int (*transfer) (struct inode *, struct iattr *); int (*sync_dquot) (struct dquot *); + int (*mark_dirty) (struct dquot *); + int (*mark_info_dirty) (struct super_block *, int); }; /* Operations handling requests from userspace */ @@ -289,10 +291,7 @@ }; /* Inline would be better but we need to dereference super_block which is not defined yet */ -#define mark_dquot_dirty(dquot) do {\ - set_bit(DQF_ANY_DQUOT_DIRTY_B, &(sb_dqopt((dquot)->dq_sb)->info[(dquot)->dq_type].dqi_flags));\ - set_bit(DQ_MOD_B, &(dquot)->dq_flags);\ -} while (0) +int mark_dquot_dirty(struct dquot *dquot); #define dquot_dirty(dquot) test_bit(DQ_MOD_B, &(dquot)->dq_flags) @@ -304,7 +303,6 @@ int register_quota_format(struct quota_format_type *fmt); void unregister_quota_format(struct quota_format_type *fmt); -void init_dquot_operations(struct dquot_operations *fsdqops); struct quota_module_name { int qm_fmt_id; diff -ruNX /home/jack/.kerndiffexclude linux-2.6.0-um/include/linux/quotaops.h linux-2.6.0-um-1-jquota/include/linux/quotaops.h --- linux-2.6.0-um/include/linux/quotaops.h 2003-11-26 21:43:27.000000000 +0100 +++ linux-2.6.0-um-1-jquota/include/linux/quotaops.h 2004-01-18 19:49:37.000000000 +0100 @@ -22,16 +22,26 @@ */ extern void sync_dquots(struct super_block *sb, int type); -extern void dquot_initialize(struct inode *inode, int type); -extern void dquot_drop(struct inode *inode); +extern int dquot_initialize(struct inode *inode, int type); +extern int dquot_drop(struct inode *inode); -extern int dquot_alloc_space(struct inode *inode, qsize_t number, int prealloc); -extern int dquot_alloc_inode(const struct inode *inode, unsigned long number); +extern int dquot_alloc_space(struct inode *inode, qsize_t number, int prealloc); +extern int dquot_alloc_inode(const struct inode *inode, unsigned long number); -extern void dquot_free_space(struct inode *inode, qsize_t number); -extern void dquot_free_inode(const struct inode *inode, unsigned long number); +extern int dquot_free_space(struct inode *inode, qsize_t number); +extern int dquot_free_inode(const struct inode *inode, unsigned long number); -extern int dquot_transfer(struct inode *inode, struct iattr *iattr); +extern int dquot_transfer(struct inode *inode, struct iattr *iattr); +extern int dquot_commit(struct dquot *dquot); +extern int dquot_commit_info(struct super_block *sb, int type); + +extern int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path); +extern int vfs_quota_off(struct super_block *sb, int type); +extern int vfs_quota_sync(struct super_block *sb, int type); +extern int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii); +extern int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii); +extern int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di); +extern int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di); /* * Operations supported for diskquotas. @@ -66,11 +76,9 @@ if (inode->i_sb->dq_op->alloc_space(inode, nr, 1) == NO_QUOTA) return 1; } - else { - spin_lock(&dq_data_lock); + else + /* inode->i_lock will guard the update */ inode_add_bytes(inode, nr); - spin_unlock(&dq_data_lock); - } return 0; } @@ -89,11 +97,9 @@ if (inode->i_sb->dq_op->alloc_space(inode, nr, 0) == NO_QUOTA) return 1; } - else { - spin_lock(&dq_data_lock); + else + /* inode->i_lock will guard the update */ inode_add_bytes(inode, nr); - spin_unlock(&dq_data_lock); - } return 0; } @@ -119,11 +125,9 @@ { if (sb_any_quota_enabled(inode->i_sb)) inode->i_sb->dq_op->free_space(inode, nr); - else { - spin_lock(&dq_data_lock); + else + /* inode->i_lock will guard the update */ inode_sub_bytes(inode, nr); - spin_unlock(&dq_data_lock); - } } tatic __inline__ void DQUOT_FREE_SPACE(struct inode *inode, qsize_t nr) --SUOF0GtieIMvvwua--