vfs_super.c 8.6 KB

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