xref: /openbmc/linux/fs/9p/vfs_super.c (revision 1f9c976e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
5  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/file.h>
13 #include <linux/stat.h>
14 #include <linux/string.h>
15 #include <linux/pagemap.h>
16 #include <linux/mount.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/statfs.h>
20 #include <linux/magic.h>
21 #include <linux/fscache.h>
22 #include <net/9p/9p.h>
23 #include <net/9p/client.h>
24 
25 #include "v9fs.h"
26 #include "v9fs_vfs.h"
27 #include "fid.h"
28 #include "xattr.h"
29 #include "acl.h"
30 
31 static const struct super_operations v9fs_super_ops, v9fs_super_ops_dotl;
32 
33 /**
34  * v9fs_set_super - set the superblock
35  * @s: super block
36  * @data: file system specific data
37  *
38  */
39 
v9fs_set_super(struct super_block * s,void * data)40 static int v9fs_set_super(struct super_block *s, void *data)
41 {
42 	s->s_fs_info = data;
43 	return set_anon_super(s, data);
44 }
45 
46 /**
47  * v9fs_fill_super - populate superblock with info
48  * @sb: superblock
49  * @v9ses: session information
50  * @flags: flags propagated from v9fs_mount()
51  *
52  */
53 
54 static int
v9fs_fill_super(struct super_block * sb,struct v9fs_session_info * v9ses,int flags)55 v9fs_fill_super(struct super_block *sb, struct v9fs_session_info *v9ses,
56 		int flags)
57 {
58 	int ret;
59 
60 	sb->s_maxbytes = MAX_LFS_FILESIZE;
61 	sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
62 	sb->s_blocksize = 1 << sb->s_blocksize_bits;
63 	sb->s_magic = V9FS_MAGIC;
64 	if (v9fs_proto_dotl(v9ses)) {
65 		sb->s_op = &v9fs_super_ops_dotl;
66 		if (!(v9ses->flags & V9FS_NO_XATTR))
67 			sb->s_xattr = v9fs_xattr_handlers;
68 	} else {
69 		sb->s_op = &v9fs_super_ops;
70 		sb->s_time_max = U32_MAX;
71 	}
72 
73 	sb->s_time_min = 0;
74 
75 	ret = super_setup_bdi(sb);
76 	if (ret)
77 		return ret;
78 
79 	if (!v9ses->cache) {
80 		sb->s_bdi->ra_pages = 0;
81 		sb->s_bdi->io_pages = 0;
82 	} else {
83 		sb->s_bdi->ra_pages = v9ses->maxdata >> PAGE_SHIFT;
84 		sb->s_bdi->io_pages = v9ses->maxdata >> PAGE_SHIFT;
85 	}
86 
87 	sb->s_flags |= SB_ACTIVE;
88 
89 #ifdef CONFIG_9P_FS_POSIX_ACL
90 	if ((v9ses->flags & V9FS_ACL_MASK) == V9FS_POSIX_ACL)
91 		sb->s_flags |= SB_POSIXACL;
92 #endif
93 
94 	return 0;
95 }
96 
97 /**
98  * v9fs_mount - mount a superblock
99  * @fs_type: file system type
100  * @flags: mount flags
101  * @dev_name: device name that was mounted
102  * @data: mount options
103  *
104  */
105 
v9fs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)106 static struct dentry *v9fs_mount(struct file_system_type *fs_type, int flags,
107 		       const char *dev_name, void *data)
108 {
109 	struct super_block *sb = NULL;
110 	struct inode *inode = NULL;
111 	struct dentry *root = NULL;
112 	struct v9fs_session_info *v9ses = NULL;
113 	umode_t mode = 0777 | S_ISVTX;
114 	struct p9_fid *fid;
115 	int retval = 0;
116 
117 	p9_debug(P9_DEBUG_VFS, "\n");
118 
119 	v9ses = kzalloc(sizeof(struct v9fs_session_info), GFP_KERNEL);
120 	if (!v9ses)
121 		return ERR_PTR(-ENOMEM);
122 
123 	fid = v9fs_session_init(v9ses, dev_name, data);
124 	if (IS_ERR(fid)) {
125 		retval = PTR_ERR(fid);
126 		goto free_session;
127 	}
128 
129 	sb = sget(fs_type, NULL, v9fs_set_super, flags, v9ses);
130 	if (IS_ERR(sb)) {
131 		retval = PTR_ERR(sb);
132 		goto clunk_fid;
133 	}
134 	retval = v9fs_fill_super(sb, v9ses, flags);
135 	if (retval)
136 		goto release_sb;
137 
138 	if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
139 		sb->s_d_op = &v9fs_cached_dentry_operations;
140 	else
141 		sb->s_d_op = &v9fs_dentry_operations;
142 
143 	inode = v9fs_get_inode(sb, S_IFDIR | mode, 0);
144 	if (IS_ERR(inode)) {
145 		retval = PTR_ERR(inode);
146 		goto release_sb;
147 	}
148 
149 	root = d_make_root(inode);
150 	if (!root) {
151 		retval = -ENOMEM;
152 		goto release_sb;
153 	}
154 	sb->s_root = root;
155 	if (v9fs_proto_dotl(v9ses)) {
156 		struct p9_stat_dotl *st = NULL;
157 
158 		st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
159 		if (IS_ERR(st)) {
160 			retval = PTR_ERR(st);
161 			goto release_sb;
162 		}
163 		d_inode(root)->i_ino = v9fs_qid2ino(&st->qid);
164 		v9fs_stat2inode_dotl(st, d_inode(root), 0);
165 		kfree(st);
166 	} else {
167 		struct p9_wstat *st = NULL;
168 
169 		st = p9_client_stat(fid);
170 		if (IS_ERR(st)) {
171 			retval = PTR_ERR(st);
172 			goto release_sb;
173 		}
174 
175 		d_inode(root)->i_ino = v9fs_qid2ino(&st->qid);
176 		v9fs_stat2inode(st, d_inode(root), sb, 0);
177 
178 		p9stat_free(st);
179 		kfree(st);
180 	}
181 	retval = v9fs_get_acl(inode, fid);
182 	if (retval)
183 		goto release_sb;
184 	v9fs_fid_add(root, &fid);
185 
186 	p9_debug(P9_DEBUG_VFS, " simple set mount, return 0\n");
187 	return dget(sb->s_root);
188 
189 clunk_fid:
190 	p9_fid_put(fid);
191 	v9fs_session_close(v9ses);
192 free_session:
193 	kfree(v9ses);
194 	return ERR_PTR(retval);
195 
196 release_sb:
197 	/*
198 	 * we will do the session_close and root dentry release
199 	 * in the below call. But we need to clunk fid, because we haven't
200 	 * attached the fid to dentry so it won't get clunked
201 	 * automatically.
202 	 */
203 	p9_fid_put(fid);
204 	deactivate_locked_super(sb);
205 	return ERR_PTR(retval);
206 }
207 
208 /**
209  * v9fs_kill_super - Kill Superblock
210  * @s: superblock
211  *
212  */
213 
v9fs_kill_super(struct super_block * s)214 static void v9fs_kill_super(struct super_block *s)
215 {
216 	struct v9fs_session_info *v9ses = s->s_fs_info;
217 
218 	p9_debug(P9_DEBUG_VFS, " %p\n", s);
219 
220 	kill_anon_super(s);
221 
222 	v9fs_session_cancel(v9ses);
223 	v9fs_session_close(v9ses);
224 	kfree(v9ses);
225 	s->s_fs_info = NULL;
226 	p9_debug(P9_DEBUG_VFS, "exiting kill_super\n");
227 }
228 
229 static void
v9fs_umount_begin(struct super_block * sb)230 v9fs_umount_begin(struct super_block *sb)
231 {
232 	struct v9fs_session_info *v9ses;
233 
234 	v9ses = sb->s_fs_info;
235 	v9fs_session_begin_cancel(v9ses);
236 }
237 
v9fs_statfs(struct dentry * dentry,struct kstatfs * buf)238 static int v9fs_statfs(struct dentry *dentry, struct kstatfs *buf)
239 {
240 	struct v9fs_session_info *v9ses;
241 	struct p9_fid *fid;
242 	struct p9_rstatfs rs;
243 	int res;
244 
245 	fid = v9fs_fid_lookup(dentry);
246 	if (IS_ERR(fid)) {
247 		res = PTR_ERR(fid);
248 		goto done;
249 	}
250 
251 	v9ses = v9fs_dentry2v9ses(dentry);
252 	if (v9fs_proto_dotl(v9ses)) {
253 		res = p9_client_statfs(fid, &rs);
254 		if (res == 0) {
255 			buf->f_type = rs.type;
256 			buf->f_bsize = rs.bsize;
257 			buf->f_blocks = rs.blocks;
258 			buf->f_bfree = rs.bfree;
259 			buf->f_bavail = rs.bavail;
260 			buf->f_files = rs.files;
261 			buf->f_ffree = rs.ffree;
262 			buf->f_fsid = u64_to_fsid(rs.fsid);
263 			buf->f_namelen = rs.namelen;
264 		}
265 		if (res != -ENOSYS)
266 			goto done;
267 	}
268 	res = simple_statfs(dentry, buf);
269 done:
270 	p9_fid_put(fid);
271 	return res;
272 }
273 
v9fs_drop_inode(struct inode * inode)274 static int v9fs_drop_inode(struct inode *inode)
275 {
276 	struct v9fs_session_info *v9ses;
277 
278 	v9ses = v9fs_inode2v9ses(inode);
279 	if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
280 		return generic_drop_inode(inode);
281 	/*
282 	 * in case of non cached mode always drop the
283 	 * inode because we want the inode attribute
284 	 * to always match that on the server.
285 	 */
286 	return 1;
287 }
288 
v9fs_write_inode(struct inode * inode,struct writeback_control * wbc)289 static int v9fs_write_inode(struct inode *inode,
290 			    struct writeback_control *wbc)
291 {
292 	struct v9fs_inode *v9inode;
293 
294 	/*
295 	 * send an fsync request to server irrespective of
296 	 * wbc->sync_mode.
297 	 */
298 	p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
299 
300 	v9inode = V9FS_I(inode);
301 	fscache_unpin_writeback(wbc, v9fs_inode_cookie(v9inode));
302 
303 	return 0;
304 }
305 
v9fs_write_inode_dotl(struct inode * inode,struct writeback_control * wbc)306 static int v9fs_write_inode_dotl(struct inode *inode,
307 				 struct writeback_control *wbc)
308 {
309 	struct v9fs_inode *v9inode;
310 
311 	v9inode = V9FS_I(inode);
312 	p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
313 
314 	fscache_unpin_writeback(wbc, v9fs_inode_cookie(v9inode));
315 
316 	return 0;
317 }
318 
319 static const struct super_operations v9fs_super_ops = {
320 	.alloc_inode = v9fs_alloc_inode,
321 	.free_inode = v9fs_free_inode,
322 	.statfs = simple_statfs,
323 	.drop_inode = v9fs_drop_inode,
324 	.evict_inode = v9fs_evict_inode,
325 	.show_options = v9fs_show_options,
326 	.umount_begin = v9fs_umount_begin,
327 	.write_inode = v9fs_write_inode,
328 };
329 
330 static const struct super_operations v9fs_super_ops_dotl = {
331 	.alloc_inode = v9fs_alloc_inode,
332 	.free_inode = v9fs_free_inode,
333 	.statfs = v9fs_statfs,
334 	.drop_inode = v9fs_drop_inode,
335 	.evict_inode = v9fs_evict_inode,
336 	.show_options = v9fs_show_options,
337 	.umount_begin = v9fs_umount_begin,
338 	.write_inode = v9fs_write_inode_dotl,
339 };
340 
341 struct file_system_type v9fs_fs_type = {
342 	.name = "9p",
343 	.mount = v9fs_mount,
344 	.kill_sb = v9fs_kill_super,
345 	.owner = THIS_MODULE,
346 	.fs_flags = FS_RENAME_DOES_D_MOVE,
347 };
348 MODULE_ALIAS_FS("9p");
349