Commit 0a305e49 authored by Bob Peterson's avatar Bob Peterson Committed by Steven Whitehouse

GFS2: Extend the life of the reservations

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.
Signed-off-by: default avatarBob Peterson <rpeterso@redhat.com>
Signed-off-by: default avatarSteven Whitehouse <swhiteho@redhat.com>
parent eea5b551
...@@ -878,7 +878,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping, ...@@ -878,7 +878,7 @@ static int gfs2_write_end(struct file *file, struct address_space *mapping,
brelse(dibh); brelse(dibh);
failed: failed:
gfs2_trans_end(sdp); gfs2_trans_end(sdp);
if (ip->i_res) if (gfs2_mb_reserved(ip))
gfs2_inplace_release(ip); gfs2_inplace_release(ip);
if (qa) { if (qa) {
gfs2_quota_unlock(ip); gfs2_quota_unlock(ip);
......
...@@ -376,6 +376,10 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) ...@@ -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); 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); gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
ret = gfs2_glock_nq(&gh); ret = gfs2_glock_nq(&gh);
if (ret) if (ret)
...@@ -569,10 +573,15 @@ static int gfs2_release(struct inode *inode, struct file *file) ...@@ -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_sbd *sdp = inode->i_sb->s_fs_info;
struct gfs2_file *fp; struct gfs2_file *fp;
struct gfs2_inode *ip = GFS2_I(inode);
fp = file->private_data; fp = file->private_data;
file->private_data = NULL; 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)) if (gfs2_assert_warn(sdp, fp))
return -EIO; return -EIO;
...@@ -653,13 +662,17 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov, ...@@ -653,13 +662,17 @@ static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
unsigned long nr_segs, loff_t pos) unsigned long nr_segs, loff_t pos)
{ {
struct file *file = iocb->ki_filp; struct file *file = iocb->ki_filp;
if (file->f_flags & O_APPEND) {
struct dentry *dentry = file->f_dentry; struct dentry *dentry = file->f_dentry;
struct gfs2_inode *ip = GFS2_I(dentry->d_inode); struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
struct gfs2_holder gh;
int ret; int ret;
ret = gfs2_rs_alloc(ip);
if (ret)
return ret;
if (file->f_flags & O_APPEND) {
struct gfs2_holder gh;
ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
if (ret) if (ret)
return ret; return ret;
...@@ -774,6 +787,10 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset, ...@@ -774,6 +787,10 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
if (bytes == 0) if (bytes == 0)
bytes = sdp->sd_sb.sb_bsize; 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); gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
error = gfs2_glock_nq(&ip->i_gh); error = gfs2_glock_nq(&ip->i_gh);
if (unlikely(error)) if (unlikely(error))
......
...@@ -667,6 +667,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, ...@@ -667,6 +667,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
if (!name->len || name->len > GFS2_FNAMESIZE) if (!name->len || name->len > GFS2_FNAMESIZE)
return -ENAMETOOLONG; return -ENAMETOOLONG;
error = gfs2_rs_alloc(dip);
if (error)
return error;
error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
if (error) if (error)
goto fail; goto fail;
...@@ -704,6 +708,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, ...@@ -704,6 +708,11 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
if (error) if (error)
goto fail_gunlock2; 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); error = gfs2_acl_create(dip, inode);
if (error) if (error)
goto fail_gunlock2; goto fail_gunlock2;
...@@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, ...@@ -722,7 +731,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
gfs2_trans_end(sdp); gfs2_trans_end(sdp);
/* Check if we reserved space in the rgrp. Function link_dinode may /* Check if we reserved space in the rgrp. Function link_dinode may
not, depending on whether alloc is required. */ not, depending on whether alloc is required. */
if (dip->i_res) if (gfs2_mb_reserved(dip))
gfs2_inplace_release(dip); gfs2_inplace_release(dip);
gfs2_quota_unlock(dip); gfs2_quota_unlock(dip);
gfs2_qadata_put(dip); gfs2_qadata_put(dip);
...@@ -819,6 +828,10 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir, ...@@ -819,6 +828,10 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
if (S_ISDIR(inode->i_mode)) if (S_ISDIR(inode->i_mode))
return -EPERM; return -EPERM;
error = gfs2_rs_alloc(dip);
if (error)
return error;
gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
...@@ -1234,6 +1247,10 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry, ...@@ -1234,6 +1247,10 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
if (error) if (error)
return error; return error;
error = gfs2_rs_alloc(ndip);
if (error)
return error;
if (odip != ndip) { if (odip != ndip) {
error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
0, &r_gh); 0, &r_gh);
...@@ -1644,6 +1661,10 @@ static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1644,6 +1661,10 @@ static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
struct gfs2_holder i_gh; struct gfs2_holder i_gh;
int error; int error;
error = gfs2_rs_alloc(ip);
if (error)
return error;
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
if (error) if (error)
return error; return error;
......
...@@ -764,6 +764,10 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda) ...@@ -764,6 +764,10 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
unsigned int nalloc = 0, blocks; unsigned int nalloc = 0, blocks;
int error; int error;
error = gfs2_rs_alloc(ip);
if (error)
return error;
gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
&data_blocks, &ind_blocks); &data_blocks, &ind_blocks);
...@@ -1549,10 +1553,14 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id, ...@@ -1549,10 +1553,14 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
if (error) if (error)
return error; return error;
error = gfs2_rs_alloc(ip);
if (error)
goto out_put;
mutex_lock(&ip->i_inode.i_mutex); mutex_lock(&ip->i_inode.i_mutex);
error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh); error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
if (error) if (error)
goto out_put; goto out_unlockput;
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
if (error) if (error)
goto out_q; goto out_q;
...@@ -1609,8 +1617,9 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id, ...@@ -1609,8 +1617,9 @@ static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
gfs2_glock_dq_uninit(&i_gh); gfs2_glock_dq_uninit(&i_gh);
out_q: out_q:
gfs2_glock_dq_uninit(&q_gh); gfs2_glock_dq_uninit(&q_gh);
out_put: out_unlockput:
mutex_unlock(&ip->i_inode.i_mutex); mutex_unlock(&ip->i_inode.i_mutex);
out_put:
qd_put(qd); qd_put(qd);
return error; return error;
} }
......
...@@ -417,6 +417,39 @@ void gfs2_free_clones(struct gfs2_rgrpd *rgd) ...@@ -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) void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
{ {
struct rb_node *n; struct rb_node *n;
...@@ -992,22 +1025,6 @@ struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip) ...@@ -992,22 +1025,6 @@ struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip)
return ip->i_qadata; return ip->i_qadata;
} }
/**
* 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 * try_rgrp_fit - See if a given reservation will fit in a given RG
* @rgd: the RG data * @rgd: the RG data
...@@ -1162,13 +1179,6 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked) ...@@ -1162,13 +1179,6 @@ static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
return -ENOSPC; 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 * gfs2_inplace_reserve - Reserve space in the filesystem
* @ip: the inode to reserve space for * @ip: the inode to reserve space for
...@@ -1181,14 +1191,10 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested) ...@@ -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_sbd *sdp = GFS2_SB(&ip->i_inode);
struct gfs2_blkreserv *rs; struct gfs2_blkreserv *rs;
int error; int error = 0;
u64 last_unlinked = NO_BLOCK; u64 last_unlinked = NO_BLOCK;
int tries = 0; int tries = 0;
error = gfs2_blkrsv_get(ip);
if (error)
return error;
rs = ip->i_res; rs = ip->i_res;
rs->rs_requested = requested; rs->rs_requested = requested;
if (gfs2_assert_warn(sdp, requested)) { if (gfs2_assert_warn(sdp, requested)) {
...@@ -1213,7 +1219,7 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested) ...@@ -1213,7 +1219,7 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
out: out:
if (error) if (error)
gfs2_blkrsv_put(ip); rs->rs_requested = 0;
return error; return error;
} }
...@@ -1230,7 +1236,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip) ...@@ -1230,7 +1236,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
if (rs->rs_rgd_gh.gh_gl) if (rs->rs_rgd_gh.gh_gl)
gfs2_glock_dq_uninit(&rs->rs_rgd_gh); 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, ...@@ -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 /* Only happens if there is a bug in gfs2, return something distinctive
* to ensure that it is noticed. * to ensure that it is noticed.
*/ */
if (ip->i_res == NULL) if (ip->i_res->rs_requested == 0)
return -ECANCELED; return -ECANCELED;
rgd = ip->i_rgd; rgd = ip->i_rgd;
......
...@@ -43,6 +43,8 @@ extern void gfs2_inplace_release(struct gfs2_inode *ip); ...@@ -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, extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
bool dinode, u64 *generation); 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_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_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);
extern void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip); 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, ...@@ -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); const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed);
extern int gfs2_fitrim(struct file *filp, void __user *argp); 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__ */ #endif /* __RGRP_DOT_H__ */
...@@ -1554,6 +1554,7 @@ static void gfs2_evict_inode(struct inode *inode) ...@@ -1554,6 +1554,7 @@ static void gfs2_evict_inode(struct inode *inode)
out: out:
/* Case 3 starts here */ /* Case 3 starts here */
truncate_inode_pages(&inode->i_data, 0); truncate_inode_pages(&inode->i_data, 0);
gfs2_rs_delete(ip);
clear_inode(inode); clear_inode(inode);
gfs2_dir_hash_inval(ip); gfs2_dir_hash_inval(ip);
ip->i_gl->gl_object = NULL; ip->i_gl->gl_object = NULL;
...@@ -1576,6 +1577,7 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb) ...@@ -1576,6 +1577,7 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
ip->i_flags = 0; ip->i_flags = 0;
ip->i_gl = NULL; ip->i_gl = NULL;
ip->i_rgd = NULL; ip->i_rgd = NULL;
ip->i_res = NULL;
} }
return &ip->i_inode; return &ip->i_inode;
} }
......
...@@ -31,7 +31,7 @@ struct gfs2_glock; ...@@ -31,7 +31,7 @@ struct gfs2_glock;
static inline unsigned int gfs2_rg_blocks(const struct gfs2_inode *ip) static inline unsigned int gfs2_rg_blocks(const struct gfs2_inode *ip)
{ {
const struct gfs2_blkreserv *rs = ip->i_res; 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 rs->rs_requested + 1;
return ip->i_rgd->rd_length; return ip->i_rgd->rd_length;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment