aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorDave Hansen <haveblue@us.ibm.com>2007-10-17 02:31:13 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-10-17 11:43:04 -0400
commitce8d2cdf3d2b73e346c82e6f0a46da331df6364c (patch)
treebf3597f2d4f57d6e30a7703d7fce0dbf8c757962 /fs
parent348366b963e4e1462c8354827a9cb910aa865bf2 (diff)
r/o bind mounts: filesystem helpers for custom 'struct file's
Why do we need r/o bind mounts? This feature allows a read-only view into a read-write filesystem. In the process of doing that, it also provides infrastructure for keeping track of the number of writers to any given mount. This has a number of uses. It allows chroots to have parts of filesystems writable. It will be useful for containers in the future because users may have root inside a container, but should not be allowed to write to somefilesystems. This also replaces patches that vserver has had out of the tree for several years. It allows security enhancement by making sure that parts of your filesystem read-only (such as when you don't trust your FTP server), when you don't want to have entire new filesystems mounted, or when you want atime selectively updated. I've been using the following script to test that the feature is working as desired. It takes a directory and makes a regular bind and a r/o bind mount of it. It then performs some normal filesystem operations on the three directories, including ones that are expected to fail, like creating a file on the r/o mount. This patch: Some filesystems forego the vfs and may_open() and create their own 'struct file's. This patch creates a couple of helper functions which can be used by these filesystems, and will provide a unified place which the r/o bind mount code may patch. Also, rename an existing, static-scope init_file() to a less generic name. Signed-off-by: Dave Hansen <haveblue@us.ibm.com> Cc: Christoph Hellwig <hch@lst.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/configfs/dir.c5
-rw-r--r--fs/file_table.c60
-rw-r--r--fs/hugetlbfs/inode.c22
3 files changed, 72 insertions, 15 deletions
diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index 2f436d4f1d6d..50ed691098bc 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -142,7 +142,7 @@ static int init_dir(struct inode * inode)
142 return 0; 142 return 0;
143} 143}
144 144
145static int init_file(struct inode * inode) 145static int configfs_init_file(struct inode * inode)
146{ 146{
147 inode->i_size = PAGE_SIZE; 147 inode->i_size = PAGE_SIZE;
148 inode->i_fop = &configfs_file_operations; 148 inode->i_fop = &configfs_file_operations;
@@ -283,7 +283,8 @@ static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * den
283 283
284 dentry->d_fsdata = configfs_get(sd); 284 dentry->d_fsdata = configfs_get(sd);
285 sd->s_dentry = dentry; 285 sd->s_dentry = dentry;
286 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file); 286 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
287 configfs_init_file);
287 if (error) { 288 if (error) {
288 configfs_put(sd); 289 configfs_put(sd);
289 return error; 290 return error;
diff --git a/fs/file_table.c b/fs/file_table.c
index ce3f39a4798a..3176fefc92e1 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -137,6 +137,66 @@ fail:
137 137
138EXPORT_SYMBOL(get_empty_filp); 138EXPORT_SYMBOL(get_empty_filp);
139 139
140/**
141 * alloc_file - allocate and initialize a 'struct file'
142 * @mnt: the vfsmount on which the file will reside
143 * @dentry: the dentry representing the new file
144 * @mode: the mode with which the new file will be opened
145 * @fop: the 'struct file_operations' for the new file
146 *
147 * Use this instead of get_empty_filp() to get a new
148 * 'struct file'. Do so because of the same initialization
149 * pitfalls reasons listed for init_file(). This is a
150 * preferred interface to using init_file().
151 *
152 * If all the callers of init_file() are eliminated, its
153 * code should be moved into this function.
154 */
155struct file *alloc_file(struct vfsmount *mnt, struct dentry *dentry,
156 mode_t mode, const struct file_operations *fop)
157{
158 struct file *file;
159 struct path;
160
161 file = get_empty_filp();
162 if (!file)
163 return NULL;
164
165 init_file(file, mnt, dentry, mode, fop);
166 return file;
167}
168EXPORT_SYMBOL(alloc_file);
169
170/**
171 * init_file - initialize a 'struct file'
172 * @file: the already allocated 'struct file' to initialized
173 * @mnt: the vfsmount on which the file resides
174 * @dentry: the dentry representing this file
175 * @mode: the mode the file is opened with
176 * @fop: the 'struct file_operations' for this file
177 *
178 * Use this instead of setting the members directly. Doing so
179 * avoids making mistakes like forgetting the mntget() or
180 * forgetting to take a write on the mnt.
181 *
182 * Note: This is a crappy interface. It is here to make
183 * merging with the existing users of get_empty_filp()
184 * who have complex failure logic easier. All users
185 * of this should be moving to alloc_file().
186 */
187int init_file(struct file *file, struct vfsmount *mnt, struct dentry *dentry,
188 mode_t mode, const struct file_operations *fop)
189{
190 int error = 0;
191 file->f_path.dentry = dentry;
192 file->f_path.mnt = mntget(mnt);
193 file->f_mapping = dentry->d_inode->i_mapping;
194 file->f_mode = mode;
195 file->f_op = fop;
196 return error;
197}
198EXPORT_SYMBOL(init_file);
199
140void fastcall fput(struct file *file) 200void fastcall fput(struct file *file)
141{ 201{
142 if (atomic_dec_and_test(&file->f_count)) 202 if (atomic_dec_and_test(&file->f_count))
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 0f5df73dbb73..12aca8ed605f 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -933,16 +933,11 @@ struct file *hugetlb_file_setup(const char *name, size_t size)
933 if (!dentry) 933 if (!dentry)
934 goto out_shm_unlock; 934 goto out_shm_unlock;
935 935
936 error = -ENFILE;
937 file = get_empty_filp();
938 if (!file)
939 goto out_dentry;
940
941 error = -ENOSPC; 936 error = -ENOSPC;
942 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid, 937 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
943 current->fsgid, S_IFREG | S_IRWXUGO, 0); 938 current->fsgid, S_IFREG | S_IRWXUGO, 0);
944 if (!inode) 939 if (!inode)
945 goto out_file; 940 goto out_dentry;
946 941
947 error = -ENOMEM; 942 error = -ENOMEM;
948 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT)) 943 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT))
@@ -951,17 +946,18 @@ struct file *hugetlb_file_setup(const char *name, size_t size)
951 d_instantiate(dentry, inode); 946 d_instantiate(dentry, inode);
952 inode->i_size = size; 947 inode->i_size = size;
953 inode->i_nlink = 0; 948 inode->i_nlink = 0;
954 file->f_path.mnt = mntget(hugetlbfs_vfsmount); 949
955 file->f_path.dentry = dentry; 950 error = -ENFILE;
956 file->f_mapping = inode->i_mapping; 951 file = alloc_file(hugetlbfs_vfsmount, dentry,
957 file->f_op = &hugetlbfs_file_operations; 952 FMODE_WRITE | FMODE_READ,
958 file->f_mode = FMODE_WRITE | FMODE_READ; 953 &hugetlbfs_file_operations);
954 if (!file)
955 goto out_inode;
956
959 return file; 957 return file;
960 958
961out_inode: 959out_inode:
962 iput(inode); 960 iput(inode);
963out_file:
964 put_filp(file);
965out_dentry: 961out_dentry:
966 dput(dentry); 962 dput(dentry);
967out_shm_unlock: 963out_shm_unlock: