vfs_super.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/file.h>
  12. #include <linux/stat.h>
  13. #include <linux/string.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mount.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/statfs.h>
  19. #include <linux/magic.h>
  20. #include <linux/fscache.h>
  21. #include <linux/fs_context.h>
  22. #include <net/9p/9p.h>
  23. #include <net/9p/client.h>
  24. #include "v9fs.h"
  25. #include "v9fs_vfs.h"
  26. #include "fid.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. static const struct super_operations v9fs_super_ops, v9fs_super_ops_dotl;
  30. static int v9fs_fill_super(struct super_block *sb)
  31. {
  32. int ret;
  33. struct v9fs_session_info *v9ses = v9ses = sb->s_fs_info;
  34. sb->s_maxbytes = MAX_LFS_FILESIZE;
  35. sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
  36. sb->s_blocksize = 1 << sb->s_blocksize_bits;
  37. sb->s_magic = V9FS_MAGIC;
  38. if (v9fs_proto_dotl(v9ses)) {
  39. sb->s_op = &v9fs_super_ops_dotl;
  40. if (!(v9ses->flags & V9FS_NO_XATTR))
  41. sb->s_xattr = v9fs_xattr_handlers;
  42. } else {
  43. sb->s_op = &v9fs_super_ops;
  44. sb->s_time_max = U32_MAX;
  45. }
  46. sb->s_time_min = 0;
  47. ret = super_setup_bdi(sb);
  48. if (ret)
  49. return ret;
  50. if (!v9ses->cache) {
  51. sb->s_bdi->ra_pages = 0;
  52. sb->s_bdi->io_pages = 0;
  53. } else {
  54. sb->s_bdi->ra_pages = v9ses->maxdata >> PAGE_SHIFT;
  55. sb->s_bdi->io_pages = v9ses->maxdata >> PAGE_SHIFT;
  56. }
  57. sb->s_flags |= SB_ACTIVE;
  58. #ifdef CONFIG_9P_FS_POSIX_ACL
  59. if ((v9ses->flags & V9FS_ACL_MASK) == V9FS_POSIX_ACL)
  60. sb->s_flags |= SB_POSIXACL;
  61. #endif
  62. return 0;
  63. }
  64. /**
  65. * v9fs_get_tree - create the mountable root and superblock
  66. * @fc: the filesystem context
  67. *
  68. */
  69. static int v9fs_get_tree(struct fs_context *fc)
  70. {
  71. struct super_block *sb = NULL;
  72. struct inode *inode = NULL;
  73. struct dentry *root = NULL;
  74. struct v9fs_session_info *v9ses = NULL;
  75. struct p9_fid *fid;
  76. int retval = 0;
  77. p9_debug(P9_DEBUG_VFS, "\n");
  78. v9ses = kzalloc_obj(struct v9fs_session_info);
  79. if (!v9ses)
  80. return -ENOMEM;
  81. fid = v9fs_session_init(v9ses, fc);
  82. if (IS_ERR(fid)) {
  83. retval = PTR_ERR(fid);
  84. goto free_session;
  85. }
  86. fc->s_fs_info = v9ses;
  87. sb = sget_fc(fc, NULL, set_anon_super_fc);
  88. if (IS_ERR(sb)) {
  89. retval = PTR_ERR(sb);
  90. goto clunk_fid;
  91. }
  92. retval = v9fs_fill_super(sb);
  93. if (retval)
  94. goto release_sb;
  95. if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
  96. set_default_d_op(sb, &v9fs_cached_dentry_operations);
  97. } else {
  98. set_default_d_op(sb, &v9fs_dentry_operations);
  99. sb->s_d_flags |= DCACHE_DONTCACHE;
  100. }
  101. inode = v9fs_get_new_inode_from_fid(v9ses, fid, sb);
  102. if (IS_ERR(inode)) {
  103. retval = PTR_ERR(inode);
  104. goto release_sb;
  105. }
  106. root = d_make_root(inode);
  107. if (!root) {
  108. retval = -ENOMEM;
  109. goto release_sb;
  110. }
  111. sb->s_root = root;
  112. retval = v9fs_get_acl(inode, fid);
  113. if (retval)
  114. goto release_sb;
  115. v9fs_fid_add(root, &fid);
  116. p9_debug(P9_DEBUG_VFS, " simple set mount, return 0\n");
  117. fc->root = dget(sb->s_root);
  118. return 0;
  119. clunk_fid:
  120. p9_fid_put(fid);
  121. v9fs_session_close(v9ses);
  122. free_session:
  123. kfree(v9ses);
  124. return retval;
  125. release_sb:
  126. /*
  127. * we will do the session_close and root dentry release
  128. * in the below call. But we need to clunk fid, because we haven't
  129. * attached the fid to dentry so it won't get clunked
  130. * automatically.
  131. */
  132. p9_fid_put(fid);
  133. deactivate_locked_super(sb);
  134. return retval;
  135. }
  136. /**
  137. * v9fs_kill_super - Kill Superblock
  138. * @s: superblock
  139. *
  140. */
  141. static void v9fs_kill_super(struct super_block *s)
  142. {
  143. struct v9fs_session_info *v9ses = s->s_fs_info;
  144. p9_debug(P9_DEBUG_VFS, " %p\n", s);
  145. kill_anon_super(s);
  146. v9fs_session_cancel(v9ses);
  147. v9fs_session_close(v9ses);
  148. kfree(v9ses);
  149. s->s_fs_info = NULL;
  150. p9_debug(P9_DEBUG_VFS, "exiting kill_super\n");
  151. }
  152. static void
  153. v9fs_umount_begin(struct super_block *sb)
  154. {
  155. struct v9fs_session_info *v9ses;
  156. v9ses = sb->s_fs_info;
  157. v9fs_session_begin_cancel(v9ses);
  158. }
  159. static int v9fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  160. {
  161. struct v9fs_session_info *v9ses;
  162. struct p9_fid *fid;
  163. struct p9_rstatfs rs;
  164. int res;
  165. fid = v9fs_fid_lookup(dentry);
  166. if (IS_ERR(fid)) {
  167. res = PTR_ERR(fid);
  168. goto done;
  169. }
  170. v9ses = v9fs_dentry2v9ses(dentry);
  171. if (v9fs_proto_dotl(v9ses)) {
  172. res = p9_client_statfs(fid, &rs);
  173. if (res == 0) {
  174. buf->f_type = rs.type;
  175. buf->f_bsize = rs.bsize;
  176. buf->f_blocks = rs.blocks;
  177. buf->f_bfree = rs.bfree;
  178. buf->f_bavail = rs.bavail;
  179. buf->f_files = rs.files;
  180. buf->f_ffree = rs.ffree;
  181. buf->f_fsid = u64_to_fsid(rs.fsid);
  182. buf->f_namelen = rs.namelen;
  183. }
  184. if (res != -ENOSYS)
  185. goto done;
  186. }
  187. res = simple_statfs(dentry, buf);
  188. done:
  189. p9_fid_put(fid);
  190. return res;
  191. }
  192. static int v9fs_drop_inode(struct inode *inode)
  193. {
  194. struct v9fs_session_info *v9ses;
  195. v9ses = v9fs_inode2v9ses(inode);
  196. if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
  197. return inode_generic_drop(inode);
  198. /*
  199. * in case of non cached mode always drop the
  200. * inode because we want the inode attribute
  201. * to always match that on the server.
  202. */
  203. return 1;
  204. }
  205. static int v9fs_write_inode(struct inode *inode,
  206. struct writeback_control *wbc)
  207. {
  208. /*
  209. * send an fsync request to server irrespective of
  210. * wbc->sync_mode.
  211. */
  212. p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
  213. return netfs_unpin_writeback(inode, wbc);
  214. }
  215. static int v9fs_write_inode_dotl(struct inode *inode,
  216. struct writeback_control *wbc)
  217. {
  218. p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
  219. return netfs_unpin_writeback(inode, wbc);
  220. }
  221. static const struct super_operations v9fs_super_ops = {
  222. .alloc_inode = v9fs_alloc_inode,
  223. .free_inode = v9fs_free_inode,
  224. .statfs = simple_statfs,
  225. .drop_inode = v9fs_drop_inode,
  226. .evict_inode = v9fs_evict_inode,
  227. .show_options = v9fs_show_options,
  228. .umount_begin = v9fs_umount_begin,
  229. .write_inode = v9fs_write_inode,
  230. };
  231. static const struct super_operations v9fs_super_ops_dotl = {
  232. .alloc_inode = v9fs_alloc_inode,
  233. .free_inode = v9fs_free_inode,
  234. .statfs = v9fs_statfs,
  235. .drop_inode = v9fs_drop_inode,
  236. .evict_inode = v9fs_evict_inode,
  237. .show_options = v9fs_show_options,
  238. .umount_begin = v9fs_umount_begin,
  239. .write_inode = v9fs_write_inode_dotl,
  240. };
  241. static void v9fs_free_fc(struct fs_context *fc)
  242. {
  243. struct v9fs_context *ctx = fc->fs_private;
  244. if (!ctx)
  245. return;
  246. /* These should be NULL by now but guard against leaks */
  247. kfree(ctx->session_opts.uname);
  248. kfree(ctx->session_opts.aname);
  249. #ifdef CONFIG_9P_FSCACHE
  250. kfree(ctx->session_opts.cachetag);
  251. #endif
  252. if (ctx->client_opts.trans_mod)
  253. v9fs_put_trans(ctx->client_opts.trans_mod);
  254. kfree(ctx);
  255. }
  256. static const struct fs_context_operations v9fs_context_ops = {
  257. .parse_param = v9fs_parse_param,
  258. .get_tree = v9fs_get_tree,
  259. .free = v9fs_free_fc,
  260. };
  261. static int v9fs_init_fs_context(struct fs_context *fc)
  262. {
  263. struct v9fs_context *ctx;
  264. ctx = kzalloc_obj(*ctx);
  265. if (!ctx)
  266. return -ENOMEM;
  267. /* initialize core options */
  268. ctx->session_opts.afid = ~0;
  269. ctx->session_opts.cache = CACHE_NONE;
  270. ctx->session_opts.session_lock_timeout = P9_LOCK_TIMEOUT;
  271. ctx->session_opts.uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
  272. if (!ctx->session_opts.uname)
  273. goto error;
  274. ctx->session_opts.aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
  275. if (!ctx->session_opts.aname)
  276. goto error;
  277. ctx->session_opts.uid = INVALID_UID;
  278. ctx->session_opts.dfltuid = V9FS_DEFUID;
  279. ctx->session_opts.dfltgid = V9FS_DEFGID;
  280. /* initialize client options */
  281. ctx->client_opts.proto_version = p9_proto_2000L;
  282. ctx->client_opts.msize = DEFAULT_MSIZE;
  283. /* initialize fd transport options */
  284. ctx->fd_opts.port = P9_FD_PORT;
  285. ctx->fd_opts.rfd = ~0;
  286. ctx->fd_opts.wfd = ~0;
  287. ctx->fd_opts.privport = false;
  288. /* initialize rdma transport options */
  289. ctx->rdma_opts.port = P9_RDMA_PORT;
  290. ctx->rdma_opts.sq_depth = P9_RDMA_SQ_DEPTH;
  291. ctx->rdma_opts.rq_depth = P9_RDMA_RQ_DEPTH;
  292. ctx->rdma_opts.timeout = P9_RDMA_TIMEOUT;
  293. ctx->rdma_opts.privport = false;
  294. fc->ops = &v9fs_context_ops;
  295. fc->fs_private = ctx;
  296. return 0;
  297. error:
  298. fc->need_free = 1;
  299. return -ENOMEM;
  300. }
  301. struct file_system_type v9fs_fs_type = {
  302. .name = "9p",
  303. .kill_sb = v9fs_kill_super,
  304. .owner = THIS_MODULE,
  305. .fs_flags = FS_RENAME_DOES_D_MOVE,
  306. .init_fs_context = v9fs_init_fs_context,
  307. .parameters = v9fs_param_spec,
  308. };
  309. MODULE_ALIAS_FS("9p");