Commit b3f8802c authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] simplify mqueue_inode_info->messages allocation

From: Chris Wright <chrisw@osdl.org>

Currently, if a user creates an mqueue and passes an mq_attr, the
info->messages will be created twice (and the extra one is properly freed).
This patch simply delays the allocation so that it only ever happens once. 
The relevant mq_attr data is passed to lower levels via the dentry->d_fsdata
fs private data.  This also helps isolate the areas we'd need to touch to do
rlimits on mqueues.
parent e37a41af
...@@ -97,7 +97,8 @@ static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode) ...@@ -97,7 +97,8 @@ static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
return container_of(inode, struct mqueue_inode_info, vfs_inode); return container_of(inode, struct mqueue_inode_info, vfs_inode);
} }
static struct inode *mqueue_get_inode(struct super_block *sb, int mode) static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
struct mq_attr *attr)
{ {
struct inode *inode; struct inode *inode;
...@@ -127,7 +128,11 @@ static struct inode *mqueue_get_inode(struct super_block *sb, int mode) ...@@ -127,7 +128,11 @@ static struct inode *mqueue_get_inode(struct super_block *sb, int mode)
memset(&info->attr, 0, sizeof(info->attr)); memset(&info->attr, 0, sizeof(info->attr));
info->attr.mq_maxmsg = DFLT_MSGMAX; info->attr.mq_maxmsg = DFLT_MSGMAX;
info->attr.mq_msgsize = DFLT_MSGSIZEMAX; info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
info->messages = kmalloc(DFLT_MSGMAX * sizeof(struct msg_msg *), GFP_KERNEL); if (attr) {
info->attr.mq_maxmsg = attr->mq_maxmsg;
info->attr.mq_msgsize = attr->mq_msgsize;
}
info->messages = kmalloc(info->attr.mq_maxmsg * sizeof(struct msg_msg *), GFP_KERNEL);
if (!info->messages) { if (!info->messages) {
make_bad_inode(inode); make_bad_inode(inode);
iput(inode); iput(inode);
...@@ -153,7 +158,7 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent) ...@@ -153,7 +158,7 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
sb->s_magic = MQUEUE_MAGIC; sb->s_magic = MQUEUE_MAGIC;
sb->s_op = &mqueue_super_ops; sb->s_op = &mqueue_super_ops;
inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO); inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
if (!inode) if (!inode)
return -ENOMEM; return -ENOMEM;
...@@ -226,6 +231,7 @@ static int mqueue_create(struct inode *dir, struct dentry *dentry, ...@@ -226,6 +231,7 @@ static int mqueue_create(struct inode *dir, struct dentry *dentry,
int mode, struct nameidata *nd) int mode, struct nameidata *nd)
{ {
struct inode *inode; struct inode *inode;
struct mq_attr *attr = dentry->d_fsdata;
int error; int error;
spin_lock(&mq_lock); spin_lock(&mq_lock);
...@@ -236,7 +242,7 @@ static int mqueue_create(struct inode *dir, struct dentry *dentry, ...@@ -236,7 +242,7 @@ static int mqueue_create(struct inode *dir, struct dentry *dentry,
queues_count++; queues_count++;
spin_unlock(&mq_lock); spin_unlock(&mq_lock);
inode = mqueue_get_inode(dir->i_sb, mode); inode = mqueue_get_inode(dir->i_sb, mode, attr);
if (!inode) { if (!inode) {
error = -ENOMEM; error = -ENOMEM;
spin_lock(&mq_lock); spin_lock(&mq_lock);
...@@ -535,9 +541,6 @@ static struct file *do_create(struct dentry *dir, struct dentry *dentry, ...@@ -535,9 +541,6 @@ static struct file *do_create(struct dentry *dir, struct dentry *dentry,
int oflag, mode_t mode, struct mq_attr __user *u_attr) int oflag, mode_t mode, struct mq_attr __user *u_attr)
{ {
struct file *filp; struct file *filp;
struct inode *inode;
struct mqueue_inode_info *info;
struct msg_msg **msgs = NULL;
struct mq_attr attr; struct mq_attr attr;
int ret; int ret;
...@@ -555,28 +558,14 @@ static struct file *do_create(struct dentry *dir, struct dentry *dentry, ...@@ -555,28 +558,14 @@ static struct file *do_create(struct dentry *dir, struct dentry *dentry,
attr.mq_msgsize > msgsize_max) attr.mq_msgsize > msgsize_max)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
msgs = kmalloc(attr.mq_maxmsg * sizeof(*msgs), GFP_KERNEL); /* store for use during create */
if (!msgs) dentry->d_fsdata = &attr;
return ERR_PTR(-ENOMEM);
} else {
msgs = NULL;
} }
ret = vfs_create(dir->d_inode, dentry, mode, NULL); ret = vfs_create(dir->d_inode, dentry, mode, NULL);
if (ret) { dentry->d_fsdata = NULL;
kfree(msgs); if (ret)
return ERR_PTR(ret); return ERR_PTR(ret);
}
inode = dentry->d_inode;
info = MQUEUE_I(inode);
if (msgs) {
info->attr.mq_maxmsg = attr.mq_maxmsg;
info->attr.mq_msgsize = attr.mq_msgsize;
kfree(info->messages);
info->messages = msgs;
}
filp = dentry_open(dentry, mqueue_mnt, oflag); filp = dentry_open(dentry, mqueue_mnt, oflag);
if (!IS_ERR(filp)) if (!IS_ERR(filp))
......
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