GFS2: Extend the life of the reservations structure
Hi,
On Thu, 2012-04-12 at 11:43 -0400, Bob Peterson wrote:
> Hi,
>
> This patch lengthens the life of the reservations structure.
> See notes below.
>
> Regards,
>
> Bob Peterson
> Red Hat GFS
>
> Signed-off-by: Bob Peterson <rpeterso@redhat.com>
> --
> Author: Bob Peterson <rpeterso@redhat.com>
> Date: Thu Apr 12 11:37:24 2012 -0500
>
> GFS2: Extend the life of the reservations structure
>
> This patch lengthens the lifespan of the reservations structure for
> inodes. Before, they were allocated and deallocated for every write
> operation. With this patch, they are allocated when the first write
> occurs, and deallocated when the last process closes the file.
> It's more efficient to do it this way because it saves GFS2 a lot of
> unnecessary allocates and frees. It also gives us more flexibility
> for the future: (1) we can now fold the qadata structure back into
> the structure and save those alloc/frees, (2) we can use this for
> multi-block reservations.
>
> diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
> index 56dc1f0..a793957 100644
> --- a/fs/gfs2/aops.c
> +++ b/fs/gfs2/aops.c
> @@ -879,7 +879,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
> brelse(dibh);
> failed:
> gfs2_trans_end(sdp);
> - if (ip->i_res)
> + if (gfs2_mb_reserved(ip))
> gfs2_inplace_release(ip);
> if (qa) {
> gfs2_quota_unlock(ip);
> diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
> index 31b199f..3a1a60a 100644
> --- a/fs/gfs2/file.c
> +++ b/fs/gfs2/file.c
> @@ -384,6 +384,10 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
> set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
> set_bit(GIF_SW_PAGED, &ip->i_flags);
>
> + ret = gfs2_rs_alloc(ip);
> + if (ret)
> + goto out_unlock;
> +
I don't think the locking is quite right here... at least I can't see
any locking at all in this case.
>
> if (file->f_flags & O_APPEND) {
> - struct dentry *dentry = file->f_dentry;
> - struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
> struct gfs2_holder gh;
> - int ret;
>
> ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
> if (ret)
> diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
> index a9ba244..127b1a4 100644
> --- a/fs/gfs2/inode.c
> +++ b/fs/gfs2/inode.c
> @@ -681,6 +681,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (error)
> goto fail_gunlock;
>
> + error = gfs2_rs_alloc(dip);
> + if (error)
> + goto fail_gunlock;
> +
> error = alloc_dinode(dip, &inum.no_addr, &generation);
> if (error)
> goto fail_gunlock;
> @@ -704,6 +708,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (error)
> goto fail_gunlock2;
>
> + /* the new inode needs a reservation so it can allocate xattrs. */
> + error = gfs2_rs_alloc(GFS2_I(inode));
> + if (error)
> + goto fail_gunlock2;
> +
> error = gfs2_acl_create(dip, inode);
> if (error)
> goto fail_gunlock2;
> @@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> gfs2_trans_end(sdp);
> /* Check if we reserved space in the rgrp. Function link_dinode may
> not, depending on whether alloc is required. */
> - if (dip->i_res)
> + if (gfs2_mb_reserved(dip))
> gfs2_inplace_release(dip);
> gfs2_quota_unlock(dip);
> gfs2_qadata_put(dip);
> @@ -734,6 +743,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> fail_gunlock2:
> gfs2_glock_dq_uninit(ghs + 1);
> fail_gunlock:
> + gfs2_rs_delete(dip);
> gfs2_glock_dq_uninit(ghs);
> if (inode && !IS_ERR(inode)) {
> set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
> diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
> index 146c3d2..af590de 100644
> --- a/fs/gfs2/rgrp.c
> +++ b/fs/gfs2/rgrp.c
> @@ -418,6 +418,34 @@ void gfs2_free_clones(struct gfs2_rgrpd *rgd)
> }
> }
>
> +/**
> + * gfs2_rs_alloc - make sure we have a reservation assigned to the inode
> + * @ip: the inode for this reservation
> + */
> +int gfs2_rs_alloc(struct gfs2_inode *ip)
> +{
> + if (ip->i_res)
> + return 0;
> + ip->i_res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
> + if (!ip->i_res)
> + return -ENOMEM;
> + return 0;
> +}
> +
I wonder whether it would be better to do this at open/create time,
assuming that the file is being opened or created for writing. That
means fewer cases to catch. The only issue I can see with that is
catching files that are created and then never opened for writing - not
sure if that is possible, but worth checking I think.
So I think the locking needs sorting out here, but otherwise it looks
good,
Steve.
> +/**
> + * gfs2_rs_delete - delete a reservation
> + * @ip: The inode for this reservation
> + *
> + */
> +void gfs2_rs_delete(struct gfs2_inode *ip)
> +{
> + if (ip->i_res == NULL)
> + return;
> +
> + kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
> + ip->i_res = NULL;
> +}
> +
> void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
> {
> struct rb_node *n;
> @@ -1001,22 +1029,6 @@ struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip)
> }
>
> /**
> - * gfs2_blkrsv_get - get the struct gfs2_blkreserv structure for an inode
> - * @ip: the incore GFS2 inode structure
> - *
> - * Returns: the struct gfs2_qadata
> - */
> -
> -static int gfs2_blkrsv_get(struct gfs2_inode *ip)
> -{
> - BUG_ON(ip->i_res != NULL);
> - ip->i_res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
> - if (!ip->i_res)
> - return -ENOMEM;
> - return 0;
> -}
> -
> -/**
> * try_rgrp_fit - See if a given reservation will fit in a given RG
> * @rgd: the RG data
> * @ip: the inode
> @@ -1170,13 +1182,6 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
> return -ENOSPC;
> }
>
> -static void gfs2_blkrsv_put(struct gfs2_inode *ip)
> -{
> - BUG_ON(ip->i_res == NULL);
> - kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
> - ip->i_res = NULL;
> -}
> -
> /**
> * gfs2_inplace_reserve - Reserve space in the filesystem
> * @ip: the inode to reserve space for
> @@ -1189,14 +1194,10 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
> {
> struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
> struct gfs2_blkreserv *rs;
> - int error;
> + int error = 0;
> u64 last_unlinked = NO_BLOCK;
> int tries = 0;
>
> - error = gfs2_blkrsv_get(ip);
> - if (error)
> - return error;
> -
> rs = ip->i_res;
> rs->rs_requested = requested;
> if (gfs2_assert_warn(sdp, requested)) {
> @@ -1221,7 +1222,7 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
>
> out:
> if (error)
> - gfs2_blkrsv_put(ip);
> + rs->rs_requested = 0;
> return error;
> }
>
> @@ -1238,7 +1239,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
>
> if (rs->rs_rgd_gh.gh_gl)
> gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
> - gfs2_blkrsv_put(ip);
> + rs->rs_requested = 0;
> }
>
> /**
> @@ -1506,7 +1507,7 @@ int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
> /* Only happens if there is a bug in gfs2, return something distinctive
> * to ensure that it is noticed.
> */
> - if (ip->i_res == NULL)
> + if (ip->i_res->rs_requested == 0)
> return -ECANCELED;
>
> rgd = ip->i_rgd;
> diff --git a/fs/gfs2/rgrp.h b/fs/gfs2/rgrp.h
> index b4b10f4..d9eda5f 100644
> --- a/fs/gfs2/rgrp.h
> +++ b/fs/gfs2/rgrp.h
> @@ -43,6 +43,8 @@ extern void gfs2_inplace_release(struct gfs2_inode *ip);
> extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
> bool dinode, u64 *generation);
>
> +extern int gfs2_rs_alloc(struct gfs2_inode *ip);
> +extern void gfs2_rs_delete(struct gfs2_inode *ip);
> extern void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta);
> extern void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);
> extern void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip);
> @@ -68,4 +70,12 @@ extern int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
> const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed);
> extern int gfs2_fitrim(struct file *filp, void __user *argp);
>
> +/* This is how to tell if a reservation is "inplace" reserved: */
> +static inline int gfs2_mb_reserved(struct gfs2_inode *ip)
> +{
> + if (ip->i_res && ip->i_res->rs_requested)
> + return 1;
> + return 0;
> +}
> +
> #endif /* __RGRP_DOT_H__ */
> diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
> index 6172fa7..a42df66 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1554,6 +1554,7 @@ out_unlock:
> out:
> /* Case 3 starts here */
> truncate_inode_pages(&inode->i_data, 0);
> + gfs2_rs_delete(ip);
> end_writeback(inode);
> gfs2_dir_hash_inval(ip);
> ip->i_gl->gl_object = NULL;
> @@ -1576,6 +1577,7 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
> ip->i_flags = 0;
> ip->i_gl = NULL;
> ip->i_rgd = NULL;
> + ip->i_res = NULL;
> }
> return &ip->i_inode;
> }
> diff --git a/fs/gfs2/trans.h b/fs/gfs2/trans.h
> index 125d457..41f42cd 100644
> --- a/fs/gfs2/trans.h
> +++ b/fs/gfs2/trans.h
> @@ -31,7 +31,7 @@ struct gfs2_glock;
> static inline unsigned int gfs2_rg_blocks(const struct gfs2_inode *ip)
> {
> const struct gfs2_blkreserv *rs = ip->i_res;
> - if (rs->rs_requested < ip->i_rgd->rd_length)
> + if (rs && rs->rs_requested < ip->i_rgd->rd_length)
> return rs->rs_requested + 1;
> return ip->i_rgd->rd_length;
> }
>
05-11-2012, 09:36 AM
Steven Whitehouse
GFS2: Extend the life of the reservations structure
Hi,
On Wed, 2012-05-09 at 12:22 -0400, Bob Peterson wrote:
> Hi,
>
> This is a second attempt at this patch. Explanation is below.
>
> The idea here is to extend the lifespan of the block reservations
> structure. This has three benefits: First, for performance, GFS2
> spends less time allocating and freeing memory for this purpose
> and can re-use the structure already attached to the inode.
> Second, it allows us to re-combine the quota structure in a
> to save the time we spend allocating and freeing that, in a future
> patch. Third, it sets us up for the forthcoming multi-block
> reservations patch.
>
> There was an issue with the predecessor patch not doing any locking
> during the allocating and freeing of the reservations for an inode.
> This patch re-purposes the gfs2 i_rw_mutex (to save memory) to
> protect the allocations and deletes. I couldn't see any places where
> this could conflict or deadlock with existing uses of the mutex.
>
> Regards,
>
> Bob Peterson
> Red Hat File Systems
>
> Signed-off-by: Bob Peterson <rpeterso@redhat.com>
> ---
> Author: Bob Peterson <rpeterso@redhat.com>
> Date: Thu Apr 12 11:37:24 2012 -0500
>
> GFS2: Extend the life of the reservations structure
>
> This patch lengthens the lifespan of the reservations structure for
> inodes. Before, they were allocated and deallocated for every write
> operation. With this patch, they are allocated when the first write
> occurs, and deallocated when the last process closes the file.
> It's more efficient to do it this way because it saves GFS2 a lot of
> unnecessary allocates and frees. It also gives us more flexibility
> for the future: (1) we can now fold the qadata structure back into
> the structure and save those alloc/frees, (2) we can use this for
> multi-block reservations.
>
> diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
> index e80a464..aba77b5 100644
> --- a/fs/gfs2/aops.c
> +++ b/fs/gfs2/aops.c
> @@ -878,7 +878,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
> brelse(dibh);
> failed:
> gfs2_trans_end(sdp);
> - if (ip->i_res)
> + if (gfs2_mb_reserved(ip))
> gfs2_inplace_release(ip);
> if (qa) {
> gfs2_quota_unlock(ip);
> diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
> index 31b199f..3790617 100644
> --- a/fs/gfs2/file.c
> +++ b/fs/gfs2/file.c
> @@ -376,6 +376,10 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
> */
> vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
>
> + ret = gfs2_rs_alloc(ip);
> + if (ret)
> + return ret;
> +
> gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
> ret = gfs2_glock_nq(&gh);
> if (ret)
> @@ -569,10 +573,15 @@ static int gfs2_release(struct inode *inode, struct file *file)
> {
> struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
> struct gfs2_file *fp;
> + struct gfs2_inode *ip = GFS2_I(inode);
>
> fp = file->private_data;
> file->private_data = NULL;
>
> + if ((file->f_mode & FMODE_WRITE) && ip->i_res &&
> + (atomic_read(&inode->i_writecount) == 1))
> + gfs2_rs_delete(ip);
> +
> if (gfs2_assert_warn(sdp, fp))
> return -EIO;
>
> @@ -653,12 +662,16 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
> unsigned long nr_segs, loff_t pos)
> {
> struct file *file = iocb->ki_filp;
> + struct dentry *dentry = file->f_dentry;
> + struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
> + int ret;
> +
> + ret = gfs2_rs_alloc(ip);
> + if (ret)
> + return ret;
>
> if (file->f_flags & O_APPEND) {
> - struct dentry *dentry = file->f_dentry;
> - struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
> struct gfs2_holder gh;
> - int ret;
>
> ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
> if (ret)
> @@ -774,6 +787,10 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
> if (bytes == 0)
> bytes = sdp->sd_sb.sb_bsize;
>
> + error = gfs2_rs_alloc(ip);
> + if (error)
> + return error;
> +
> gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
> error = gfs2_glock_nq(&ip->i_gh);
> if (unlikely(error))
> diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
> index a9ba244..6a46417 100644
> --- a/fs/gfs2/inode.c
> +++ b/fs/gfs2/inode.c
> @@ -667,6 +667,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (!name->len || name->len > GFS2_FNAMESIZE)
> return -ENAMETOOLONG;
>
> + error = gfs2_rs_alloc(dip);
> + if (error)
> + return error;
> +
> error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
> if (error)
> goto fail;
> @@ -704,6 +708,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (error)
> goto fail_gunlock2;
>
> + /* the new inode needs a reservation so it can allocate xattrs. */
> + error = gfs2_rs_alloc(GFS2_I(inode));
> + if (error)
> + goto fail_gunlock2;
> +
> error = gfs2_acl_create(dip, inode);
> if (error)
> goto fail_gunlock2;
> @@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> gfs2_trans_end(sdp);
> /* Check if we reserved space in the rgrp. Function link_dinode may
> not, depending on whether alloc is required. */
> - if (dip->i_res)
> + if (gfs2_mb_reserved(dip))
> gfs2_inplace_release(dip);
> gfs2_quota_unlock(dip);
> gfs2_qadata_put(dip);
> @@ -740,6 +749,7 @@ fail_gunlock:
> iput(inode);
> }
> fail:
> + gfs2_rs_delete(dip);
Should we really be dropping the ip->i_res here I wonder? I'm not sure
that we want to forget this information just because (for example)
someone has tried to create a new inode and it has been failed for some
reason or other.
Otherwise I think this looks good,
Steve.
05-14-2012, 01:03 PM
Steven Whitehouse
GFS2: Extend the life of the reservations structure
Hi,
On Mon, 2012-05-14 at 08:58 -0400, Bob Peterson wrote:
> ----- Original Message -----
> | > @@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir,
> | > struct dentry *dentry,
> | > gfs2_trans_end(sdp);
> | > /* Check if we reserved space in the rgrp. Function link_dinode
> | > may
> | > not, depending on whether alloc is required. */
> | > - if (dip->i_res)
> | > + if (gfs2_mb_reserved(dip))
> | > gfs2_inplace_release(dip);
> | > gfs2_quota_unlock(dip);
> | > gfs2_qadata_put(dip);
> | > @@ -740,6 +749,7 @@ fail_gunlock:
> | > iput(inode);
> | > }
> | > fail:
> | > + gfs2_rs_delete(dip);
> |
> | Should we really be dropping the ip->i_res here I wonder? I'm not
> | sure
> | that we want to forget this information just because (for example)
> | someone has tried to create a new inode and it has been failed for
> | some
> | reason or other.
> |
> | Otherwise I think this looks good,
> |
> | Steve.
>
> Hi,
>
> In this implementation, function gfs2_inplace_release just unlocks the
> rgrp glock and sets rs_requested to zero. In fact, the reservation still
> hangs around, attached to the inode until function gfs2_rs_delete is called
> from gfs2_release or gfs2_evict_inode. So instead of having two functions
> for allocation and deallocation, we now have four:
>
That is all ok I think, but I'm referring to the call to
gfs2_rs_delete() in this case. We should probably call the combined
structure something like struct gfs2_alloc, since it deals with
allocation and not just reservations. However, the issue here was
dropping the reservation from the directory in the case that the
allocation has failed for some reason (maybe permisions, or something
like that)
Steve.
> gfs2_rs_alloc: kmem_cache_zalloc
> gfs2_inplace_reserve: hold rgrp glock, rs_requested = reservation needed
> gfs2_inplace_release: dq rgrp glock, rs_requested = 0
> gfs2_rs_delete: kmem_cache_free
>
> I can change the names (suggestions welcome), because "gfs2_inplace_reserve"
> and its counterpart have changed their roles over the years. I'm just
> conditioned to look for those function names from years of conditioning.
>
> Something more like "get_local_rgrp" is a more accurate reflection of
> what's happening, but that name is already used. I've never liked how
> get_local_rgrp and gfs2_inplace_reserve both have retry loops.
> Since they're now pretty small functions, and one calls the other, perhaps
> I should combine them into one function by the name "get_local_rgrp"?
> And perhaps that should be done in a separate patch?
>
> Regards,
>
> Bob Peterson
> Red Hat File Systems
05-15-2012, 11:41 AM
Steven Whitehouse
GFS2: Extend the life of the reservations structure
Hi,
That doesn't appear to work for me, unfortunately:
I suspect that quotad needs its own call to set up the new structure. I
wonder if there are other places where we do internal writes which also
need attention?
Steve.
On Mon, 2012-05-14 at 12:15 -0400, Bob Peterson wrote:
> ----- Original Message -----
> | Hi,
> |
> | On Mon, 2012-05-14 at 08:58 -0400, Bob Peterson wrote:
> | > | > fail:
> | > | > + gfs2_rs_delete(dip);
> | > |
> | > | Should we really be dropping the ip->i_res here I wonder? I'm not
> | > | sure
> | > | that we want to forget this information just because (for
> | > | example)
> | > | someone has tried to create a new inode and it has been failed
> | > | for
> | > | some
> | > | reason or other.
> | > |
> | > | Otherwise I think this looks good,
> | > |
> | > | Steve.
> | >
> | > Hi,
> | >
> | > In this implementation, function gfs2_inplace_release just unlocks
> | > the
> | > rgrp glock and sets rs_requested to zero. In fact, the reservation
> | > still
> | > hangs around, attached to the inode until function gfs2_rs_delete
> | > is called
> | > from gfs2_release or gfs2_evict_inode. So instead of having two
> | > functions
> | > for allocation and deallocation, we now have four:
> | >
> | That is all ok I think, but I'm referring to the call to
> | gfs2_rs_delete() in this case. We should probably call the combined
> | structure something like struct gfs2_alloc, since it deals with
> | allocation and not just reservations. However, the issue here was
> | dropping the reservation from the directory in the case that the
> | allocation has failed for some reason (maybe permisions, or something
> | like that)
> |
> | Steve.
>
> Hi,
>
> Ah, I misunderstood. I see your point now. Here's the revised version.
>
> Regards,
>
> Bob Peterson
> Red Hat File Systems
>
> Signed-off-by: Bob Peterson <rpeterso@redhat.com>
> ---
> Author: Bob Peterson <rpeterso@redhat.com>
> Date: Thu Apr 12 11:37:24 2012 -0500
>
> GFS2: Extend the life of the reservations structure
>
> This patch lengthens the lifespan of the reservations structure for
> inodes. Before, they were allocated and deallocated for every write
> operation. With this patch, they are allocated when the first write
> occurs, and deallocated when the last process closes the file.
> It's more efficient to do it this way because it saves GFS2 a lot of
> unnecessary allocates and frees. It also gives us more flexibility
> for the future: (1) we can now fold the qadata structure back into
> the structure and save those alloc/frees, (2) we can use this for
> multi-block reservations.
>
> diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
> index e80a464..aba77b5 100644
> --- a/fs/gfs2/aops.c
> +++ b/fs/gfs2/aops.c
> @@ -878,7 +878,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
> brelse(dibh);
> failed:
> gfs2_trans_end(sdp);
> - if (ip->i_res)
> + if (gfs2_mb_reserved(ip))
> gfs2_inplace_release(ip);
> if (qa) {
> gfs2_quota_unlock(ip);
> diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
> index 31b199f..3790617 100644
> --- a/fs/gfs2/file.c
> +++ b/fs/gfs2/file.c
> @@ -376,6 +376,10 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
> */
> vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
>
> + ret = gfs2_rs_alloc(ip);
> + if (ret)
> + return ret;
> +
> gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
> ret = gfs2_glock_nq(&gh);
> if (ret)
> @@ -569,10 +573,15 @@ static int gfs2_release(struct inode *inode, struct file *file)
> {
> struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
> struct gfs2_file *fp;
> + struct gfs2_inode *ip = GFS2_I(inode);
>
> fp = file->private_data;
> file->private_data = NULL;
>
> + if ((file->f_mode & FMODE_WRITE) && ip->i_res &&
> + (atomic_read(&inode->i_writecount) == 1))
> + gfs2_rs_delete(ip);
> +
> if (gfs2_assert_warn(sdp, fp))
> return -EIO;
>
> @@ -653,12 +662,16 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
> unsigned long nr_segs, loff_t pos)
> {
> struct file *file = iocb->ki_filp;
> + struct dentry *dentry = file->f_dentry;
> + struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
> + int ret;
> +
> + ret = gfs2_rs_alloc(ip);
> + if (ret)
> + return ret;
>
> if (file->f_flags & O_APPEND) {
> - struct dentry *dentry = file->f_dentry;
> - struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
> struct gfs2_holder gh;
> - int ret;
>
> ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
> if (ret)
> @@ -774,6 +787,10 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
> if (bytes == 0)
> bytes = sdp->sd_sb.sb_bsize;
>
> + error = gfs2_rs_alloc(ip);
> + if (error)
> + return error;
> +
> gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
> error = gfs2_glock_nq(&ip->i_gh);
> if (unlikely(error))
> diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
> index a9ba244..ec7d93b 100644
> --- a/fs/gfs2/inode.c
> +++ b/fs/gfs2/inode.c
> @@ -667,6 +667,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (!name->len || name->len > GFS2_FNAMESIZE)
> return -ENAMETOOLONG;
>
> + error = gfs2_rs_alloc(dip);
> + if (error)
> + return error;
> +
> error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
> if (error)
> goto fail;
> @@ -704,6 +708,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> if (error)
> goto fail_gunlock2;
>
> + /* the new inode needs a reservation so it can allocate xattrs. */
> + error = gfs2_rs_alloc(GFS2_I(inode));
> + if (error)
> + goto fail_gunlock2;
> +
> error = gfs2_acl_create(dip, inode);
> if (error)
> goto fail_gunlock2;
> @@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
> gfs2_trans_end(sdp);
> /* Check if we reserved space in the rgrp. Function link_dinode may
> not, depending on whether alloc is required. */
> - if (dip->i_res)
> + if (gfs2_mb_reserved(dip))
> gfs2_inplace_release(dip);
> gfs2_quota_unlock(dip);
> gfs2_qadata_put(dip);
> diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
> index f74fb9b..e944fef 100644
> --- a/fs/gfs2/rgrp.c
> +++ b/fs/gfs2/rgrp.c
> @@ -417,6 +417,39 @@ void gfs2_free_clones(struct gfs2_rgrpd *rgd)
> }
> }
>
> +/**
> + * gfs2_rs_alloc - make sure we have a reservation assigned to the inode
> + * @ip: the inode for this reservation
> + */
> +int gfs2_rs_alloc(struct gfs2_inode *ip)
> +{
> + int error = 0;
> +
> + down_write(&ip->i_rw_mutex);
> + if (!ip->i_res) {
> + ip->i_res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
> + if (!ip->i_res)
> + error = -ENOMEM;
> + }
> + up_write(&ip->i_rw_mutex);
> + return error;
> +}
> +
> +/**
> + * gfs2_rs_delete - delete a reservation
> + * @ip: The inode for this reservation
> + *
> + */
> +void gfs2_rs_delete(struct gfs2_inode *ip)
> +{
> + down_write(&ip->i_rw_mutex);
> + if (ip->i_res) {
> + kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
> + ip->i_res = NULL;
> + }
> + up_write(&ip->i_rw_mutex);
> +}
> +
> void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
> {
> struct rb_node *n;
> @@ -993,22 +1026,6 @@ struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip)
> }
>
> /**
> - * gfs2_blkrsv_get - get the struct gfs2_blkreserv structure for an inode
> - * @ip: the incore GFS2 inode structure
> - *
> - * Returns: the struct gfs2_qadata
> - */
> -
> -static int gfs2_blkrsv_get(struct gfs2_inode *ip)
> -{
> - BUG_ON(ip->i_res != NULL);
> - ip->i_res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
> - if (!ip->i_res)
> - return -ENOMEM;
> - return 0;
> -}
> -
> -/**
> * try_rgrp_fit - See if a given reservation will fit in a given RG
> * @rgd: the RG data
> * @ip: the inode
> @@ -1162,13 +1179,6 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
> return -ENOSPC;
> }
>
> -static void gfs2_blkrsv_put(struct gfs2_inode *ip)
> -{
> - BUG_ON(ip->i_res == NULL);
> - kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
> - ip->i_res = NULL;
> -}
> -
> /**
> * gfs2_inplace_reserve - Reserve space in the filesystem
> * @ip: the inode to reserve space for
> @@ -1181,14 +1191,10 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
> {
> struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
> struct gfs2_blkreserv *rs;
> - int error;
> + int error = 0;
> u64 last_unlinked = NO_BLOCK;
> int tries = 0;
>
> - error = gfs2_blkrsv_get(ip);
> - if (error)
> - return error;
> -
> rs = ip->i_res;
> rs->rs_requested = requested;
> if (gfs2_assert_warn(sdp, requested)) {
> @@ -1213,7 +1219,7 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
>
> out:
> if (error)
> - gfs2_blkrsv_put(ip);
> + rs->rs_requested = 0;
> return error;
> }
>
> @@ -1230,7 +1236,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
>
> if (rs->rs_rgd_gh.gh_gl)
> gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
> - gfs2_blkrsv_put(ip);
> + rs->rs_requested = 0;
> }
>
> /**
> @@ -1496,7 +1502,7 @@ int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
> /* Only happens if there is a bug in gfs2, return something distinctive
> * to ensure that it is noticed.
> */
> - if (ip->i_res == NULL)
> + if (ip->i_res->rs_requested == 0)
> return -ECANCELED;
>
> rgd = ip->i_rgd;
> diff --git a/fs/gfs2/rgrp.h b/fs/gfs2/rgrp.h
> index b4b10f4..d9eda5f 100644
> --- a/fs/gfs2/rgrp.h
> +++ b/fs/gfs2/rgrp.h
> @@ -43,6 +43,8 @@ extern void gfs2_inplace_release(struct gfs2_inode *ip);
> extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
> bool dinode, u64 *generation);
>
> +extern int gfs2_rs_alloc(struct gfs2_inode *ip);
> +extern void gfs2_rs_delete(struct gfs2_inode *ip);
> extern void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta);
> extern void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);
> extern void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip);
> @@ -68,4 +70,12 @@ extern int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
> const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed);
> extern int gfs2_fitrim(struct file *filp, void __user *argp);
>
> +/* This is how to tell if a reservation is "inplace" reserved: */
> +static inline int gfs2_mb_reserved(struct gfs2_inode *ip)
> +{
> + if (ip->i_res && ip->i_res->rs_requested)
> + return 1;
> + return 0;
> +}
> +
> #endif /* __RGRP_DOT_H__ */
> diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
> index 6172fa7..a42df66 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1554,6 +1554,7 @@ out_unlock:
> out:
> /* Case 3 starts here */
> truncate_inode_pages(&inode->i_data, 0);
> + gfs2_rs_delete(ip);
> end_writeback(inode);
> gfs2_dir_hash_inval(ip);
> ip->i_gl->gl_object = NULL;
> @@ -1576,6 +1577,7 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
> ip->i_flags = 0;
> ip->i_gl = NULL;
> ip->i_rgd = NULL;
> + ip->i_res = NULL;
> }
> return &ip->i_inode;
> }
> diff --git a/fs/gfs2/trans.h b/fs/gfs2/trans.h
> index 125d457..41f42cd 100644
> --- a/fs/gfs2/trans.h
> +++ b/fs/gfs2/trans.h
> @@ -31,7 +31,7 @@ struct gfs2_glock;
> static inline unsigned int gfs2_rg_blocks(const struct gfs2_inode *ip)
> {
> const struct gfs2_blkreserv *rs = ip->i_res;
> - if (rs->rs_requested < ip->i_rgd->rd_length)
> + if (rs && rs->rs_requested < ip->i_rgd->rd_length)
> return rs->rs_requested + 1;
> return ip->i_rgd->rd_length;
> }