vfs_super.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <net/9p/9p.h>
  22. #include <net/9p/client.h>
  23. #include "v9fs.h"
  24. #include "v9fs_vfs.h"
  25. #include "fid.h"
  26. #include "xattr.h"
  27. #include "acl.h"
  28. static const struct super_operations v9fs_super_ops, v9fs_super_ops_dotl;
  29. /**
  30. * v9fs_set_super - set the superblock
  31. * @s: super block
  32. * @data: file system specific data
  33. *
  34. */
  35. static int v9fs_set_super(struct super_block *s, void *data)
  36. {
  37. s->s_fs_info = data;
  38. return set_anon_super(s, data);
  39. }
  40. /**
  41. * v9fs_fill_super - populate superblock with info
  42. * @sb: superblock
  43. * @v9ses: session information
  44. * @flags: flags propagated from v9fs_mount()
  45. *
  46. */
  47. static int
  48. v9fs_fill_super(struct super_block *sb, struct v9fs_session_info *v9ses,
  49. int flags)
  50. {
  51. int ret;
  52. sb->s_maxbytes = MAX_LFS_FILESIZE;
  53. sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
  54. sb->s_blocksize = 1 << sb->s_blocksize_bits;
  55. sb->s_magic = V9FS_MAGIC;
  56. if (v9fs_proto_dotl(v9ses)) {
  57. sb->s_op = &v9fs_super_ops_dotl;
  58. if (!(v9ses->flags & V9FS_NO_XATTR))
  59. sb->s_xattr = v9fs_xattr_handlers;
  60. } else {
  61. sb->s_op = &v9fs_super_ops;
  62. sb->s_time_max = U32_MAX;
  63. }
  64. sb->s_time_min = 0;
  65. ret = super_setup_bdi(sb);
  66. if (ret)
  67. return ret;
  68. if (!v9ses->cache) {
  69. sb->s_bdi->ra_pages = 0;
  70. sb->s_bdi->io_pages = 0;
  71. } else {
  72. sb->s_bdi->ra_pages = v9ses->maxdata >> PAGE_SHIFT;
  73. sb->s_bdi->io_pages = v9ses->maxdata >> PAGE_SHIFT;
  74. }
  75. sb->s_flags |= SB_ACTIVE;
  76. #ifdef CONFIG_9P_FS_POSIX_ACL
  77. if ((v9ses->flags & V9FS_ACL_MASK) == V9FS_POSIX_ACL)
  78. sb->s_flags |= SB_POSIXACL;
  79. #endif
  80. return 0;
  81. }
  82. /**
  83. * v9fs_mount - mount a superblock
  84. * @fs_type: file system type
  85. * @flags: mount flags
  86. * @dev_name: device name that was mounted
  87. * @data: mount options
  88. *
  89. */
  90. static struct dentry *v9fs_mount(struct file_system_type *fs_type, int flags,
  91. const char *dev_name, void *data)
  92. {
  93. struct super_block *sb = NULL;
  94. struct inode *inode = NULL;
  95. struct dentry *root = NULL;
  96. struct v9fs_session_info *v9ses = NULL;
  97. struct p9_fid *fid;
  98. int retval = 0;
  99. p9_debug(P9_DEBUG_VFS, "\n");
  100. v9ses = kzalloc(sizeof(struct v9fs_session_info), GFP_KERNEL);
  101. if (!v9ses)
  102. return ERR_PTR(-ENOMEM);
  103. fid = v9fs_session_init(v9ses, dev_name, data);
  104. if (IS_ERR(fid)) {
  105. retval = PTR_ERR(fid);
  106. goto free_session;
  107. }
  108. sb = sget(fs_type, NULL, v9fs_set_super, flags, v9ses);
  109. if (IS_ERR(sb)) {
  110. retval = PTR_ERR(sb);
  111. goto clunk_fid;
  112. }
  113. retval = v9fs_fill_super(sb, v9ses, flags);
  114. if (retval)
  115. goto release_sb;
  116. if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
  117. sb->s_d_op = &v9fs_cached_dentry_operations;
  118. else
  119. sb->s_d_op = &v9fs_dentry_operations;
  120. inode = v9fs_get_new_inode_from_fid(v9ses, fid, sb);
  121. if (IS_ERR(inode)) {
  122. retval = PTR_ERR(inode);
  123. goto release_sb;
  124. }
  125. root = d_make_root(inode);
  126. if (!root) {
  127. retval = -ENOMEM;
  128. goto release_sb;
  129. }
  130. sb->s_root = root;
  131. retval = v9fs_get_acl(inode, fid);
  132. if (retval)
  133. goto release_sb;
  134. v9fs_fid_add(root, &fid);
  135. p9_debug(P9_DEBUG_VFS, " simple set mount, return 0\n");
  136. return dget(sb->s_root);
  137. clunk_fid:
  138. p9_fid_put(fid);
  139. v9fs_session_close(v9ses);
  140. free_session:
  141. kfree(v9ses);
  142. return ERR_PTR(retval);
  143. release_sb:
  144. /*
  145. * we will do the session_close and root dentry release
  146. * in the below call. But we need to clunk fid, because we haven't
  147. * attached the fid to dentry so it won't get clunked
  148. * automatically.
  149. */
  150. p9_fid_put(fid);
  151. deactivate_locked_super(sb);
  152. return ERR_PTR(retval);
  153. }
  154. /**
  155. * v9fs_kill_super - Kill Superblock
  156. * @s: superblock
  157. *
  158. */
  159. static void v9fs_kill_super(struct super_block *s)
  160. {
  161. struct v9fs_session_info *v9ses = s->s_fs_info;
  162. p9_debug(P9_DEBUG_VFS, " %p\n", s);
  163. kill_anon_super(s);
  164. v9fs_session_cancel(v9ses);
  165. v9fs_session_close(v9ses);
  166. kfree(v9ses);
  167. s->s_fs_info = NULL;
  168. p9_debug(P9_DEBUG_VFS, "exiting kill_super\n");
  169. }
  170. static void
  171. v9fs_umount_begin(struct super_block *sb)
  172. {
  173. struct v9fs_session_info *v9ses;
  174. v9ses = sb->s_fs_info;
  175. v9fs_session_begin_cancel(v9ses);
  176. }
  177. static int v9fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  178. {
  179. struct v9fs_session_info *v9ses;
  180. struct p9_fid *fid;
  181. struct p9_rstatfs rs;
  182. int res;
  183. fid = v9fs_fid_lookup(dentry);
  184. if (IS_ERR(fid)) {
  185. res = PTR_ERR(fid);
  186. goto done;
  187. }
  188. v9ses = v9fs_dentry2v9ses(dentry);
  189. if (v9fs_proto_dotl(v9ses)) {
  190. res = p9_client_statfs(fid, &rs);
  191. if (res == 0) {
  192. buf->f_type = rs.type;
  193. buf->f_bsize = rs.bsize;
  194. buf->f_blocks = rs.blocks;
  195. buf->f_bfree = rs.bfree;
  196. buf->f_bavail = rs.bavail;
  197. buf->f_files = rs.files;
  198. buf->f_ffree = rs.ffree;
  199. buf->f_fsid = u64_to_fsid(rs.fsid);
  200. buf->f_namelen = rs.namelen;
  201. }
  202. if (res != -ENOSYS)
  203. goto done;
  204. }
  205. res = simple_statfs(dentry, buf);
  206. done:
  207. p9_fid_put(fid);
  208. return res;
  209. }
  210. static int v9fs_drop_inode(struct inode *inode)
  211. {
  212. struct v9fs_session_info *v9ses;
  213. v9ses = v9fs_inode2v9ses(inode);
  214. if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
  215. return generic_drop_inode(inode);
  216. /*
  217. * in case of non cached mode always drop the
  218. * inode because we want the inode attribute
  219. * to always match that on the server.
  220. */
  221. return 1;
  222. }
  223. static int v9fs_write_inode(struct inode *inode,
  224. struct writeback_control *wbc)
  225. {
  226. /*
  227. * send an fsync request to server irrespective of
  228. * wbc->sync_mode.
  229. */
  230. p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
  231. return netfs_unpin_writeback(inode, wbc);
  232. }
  233. static int v9fs_write_inode_dotl(struct inode *inode,
  234. struct writeback_control *wbc)
  235. {
  236. p9_debug(P9_DEBUG_VFS, "%s: inode %p\n", __func__, inode);
  237. return netfs_unpin_writeback(inode, wbc);
  238. }
  239. static const struct super_operations v9fs_super_ops = {
  240. .alloc_inode = v9fs_alloc_inode,
  241. .free_inode = v9fs_free_inode,
  242. .statfs = simple_statfs,
  243. .drop_inode = v9fs_drop_inode,
  244. .evict_inode = v9fs_evict_inode,
  245. .show_options = v9fs_show_options,
  246. .umount_begin = v9fs_umount_begin,
  247. .write_inode = v9fs_write_inode,
  248. };
  249. static const struct super_operations v9fs_super_ops_dotl = {
  250. .alloc_inode = v9fs_alloc_inode,
  251. .free_inode = v9fs_free_inode,
  252. .statfs = v9fs_statfs,
  253. .drop_inode = v9fs_drop_inode,
  254. .evict_inode = v9fs_evict_inode,
  255. .show_options = v9fs_show_options,
  256. .umount_begin = v9fs_umount_begin,
  257. .write_inode = v9fs_write_inode_dotl,
  258. };
  259. struct file_system_type v9fs_fs_type = {
  260. .name = "9p",
  261. .mount = v9fs_mount,
  262. .kill_sb = v9fs_kill_super,
  263. .owner = THIS_MODULE,
  264. .fs_flags = FS_RENAME_DOES_D_MOVE,
  265. };
  266. MODULE_ALIAS_FS("9p");