nfs4super.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
  3. */
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/nfs_idmap.h>
  7. #include <linux/nfs4_mount.h>
  8. #include <linux/nfs_fs.h>
  9. #include "internal.h"
  10. #include "nfs4_fs.h"
  11. #define NFSDBG_FACILITY NFSDBG_VFS
  12. static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
  13. int flags, const char *dev_name, void *raw_data);
  14. static struct dentry *nfs4_xdev_mount(struct file_system_type *fs_type,
  15. int flags, const char *dev_name, void *raw_data);
  16. static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
  17. int flags, const char *dev_name, void *raw_data);
  18. static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
  19. int flags, const char *dev_name, void *raw_data);
  20. static struct file_system_type nfs4_fs_type = {
  21. .owner = THIS_MODULE,
  22. .name = "nfs4",
  23. .mount = nfs_fs_mount,
  24. .kill_sb = nfs_kill_super,
  25. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  26. };
  27. static struct file_system_type nfs4_remote_fs_type = {
  28. .owner = THIS_MODULE,
  29. .name = "nfs4",
  30. .mount = nfs4_remote_mount,
  31. .kill_sb = nfs_kill_super,
  32. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  33. };
  34. struct file_system_type nfs4_xdev_fs_type = {
  35. .owner = THIS_MODULE,
  36. .name = "nfs4",
  37. .mount = nfs4_xdev_mount,
  38. .kill_sb = nfs_kill_super,
  39. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  40. };
  41. static struct file_system_type nfs4_remote_referral_fs_type = {
  42. .owner = THIS_MODULE,
  43. .name = "nfs4",
  44. .mount = nfs4_remote_referral_mount,
  45. .kill_sb = nfs_kill_super,
  46. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  47. };
  48. struct file_system_type nfs4_referral_fs_type = {
  49. .owner = THIS_MODULE,
  50. .name = "nfs4",
  51. .mount = nfs4_referral_mount,
  52. .kill_sb = nfs_kill_super,
  53. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  54. };
  55. static const struct super_operations nfs4_sops = {
  56. .alloc_inode = nfs_alloc_inode,
  57. .destroy_inode = nfs_destroy_inode,
  58. .write_inode = nfs4_write_inode,
  59. .put_super = nfs_put_super,
  60. .statfs = nfs_statfs,
  61. .evict_inode = nfs4_evict_inode,
  62. .umount_begin = nfs_umount_begin,
  63. .show_options = nfs_show_options,
  64. .show_devname = nfs_show_devname,
  65. .show_path = nfs_show_path,
  66. .show_stats = nfs_show_stats,
  67. .remount_fs = nfs_remount,
  68. };
  69. /*
  70. * Set up an NFS4 superblock
  71. */
  72. static void nfs4_fill_super(struct super_block *sb,
  73. struct nfs_mount_info *mount_info)
  74. {
  75. sb->s_time_gran = 1;
  76. sb->s_op = &nfs4_sops;
  77. /*
  78. * The VFS shouldn't apply the umask to mode bits. We will do
  79. * so ourselves when necessary.
  80. */
  81. sb->s_flags |= MS_POSIXACL;
  82. sb->s_xattr = nfs4_xattr_handlers;
  83. nfs_initialise_sb(sb);
  84. }
  85. /*
  86. * Get the superblock for the NFS4 root partition
  87. */
  88. static struct dentry *
  89. nfs4_remote_mount(struct file_system_type *fs_type, int flags,
  90. const char *dev_name, void *info)
  91. {
  92. struct nfs_mount_info *mount_info = info;
  93. struct nfs_server *server;
  94. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  95. mount_info->fill_super = nfs4_fill_super;
  96. mount_info->set_security = nfs_set_sb_security;
  97. /* Get a volume representation */
  98. server = nfs4_create_server(mount_info->parsed, mount_info->mntfh);
  99. if (IS_ERR(server)) {
  100. mntroot = ERR_CAST(server);
  101. goto out;
  102. }
  103. mntroot = nfs_fs_mount_common(fs_type, server, flags, dev_name, mount_info);
  104. out:
  105. return mntroot;
  106. }
  107. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  108. int flags, void *data, const char *hostname)
  109. {
  110. struct vfsmount *root_mnt;
  111. char *root_devname;
  112. size_t len;
  113. len = strlen(hostname) + 5;
  114. root_devname = kmalloc(len, GFP_KERNEL);
  115. if (root_devname == NULL)
  116. return ERR_PTR(-ENOMEM);
  117. /* Does hostname needs to be enclosed in brackets? */
  118. if (strchr(hostname, ':'))
  119. snprintf(root_devname, len, "[%s]:/", hostname);
  120. else
  121. snprintf(root_devname, len, "%s:/", hostname);
  122. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  123. kfree(root_devname);
  124. return root_mnt;
  125. }
  126. struct nfs_referral_count {
  127. struct list_head list;
  128. const struct task_struct *task;
  129. unsigned int referral_count;
  130. };
  131. static LIST_HEAD(nfs_referral_count_list);
  132. static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
  133. static struct nfs_referral_count *nfs_find_referral_count(void)
  134. {
  135. struct nfs_referral_count *p;
  136. list_for_each_entry(p, &nfs_referral_count_list, list) {
  137. if (p->task == current)
  138. return p;
  139. }
  140. return NULL;
  141. }
  142. #define NFS_MAX_NESTED_REFERRALS 2
  143. static int nfs_referral_loop_protect(void)
  144. {
  145. struct nfs_referral_count *p, *new;
  146. int ret = -ENOMEM;
  147. new = kmalloc(sizeof(*new), GFP_KERNEL);
  148. if (!new)
  149. goto out;
  150. new->task = current;
  151. new->referral_count = 1;
  152. ret = 0;
  153. spin_lock(&nfs_referral_count_list_lock);
  154. p = nfs_find_referral_count();
  155. if (p != NULL) {
  156. if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
  157. ret = -ELOOP;
  158. else
  159. p->referral_count++;
  160. } else {
  161. list_add(&new->list, &nfs_referral_count_list);
  162. new = NULL;
  163. }
  164. spin_unlock(&nfs_referral_count_list_lock);
  165. kfree(new);
  166. out:
  167. return ret;
  168. }
  169. static void nfs_referral_loop_unprotect(void)
  170. {
  171. struct nfs_referral_count *p;
  172. spin_lock(&nfs_referral_count_list_lock);
  173. p = nfs_find_referral_count();
  174. p->referral_count--;
  175. if (p->referral_count == 0)
  176. list_del(&p->list);
  177. else
  178. p = NULL;
  179. spin_unlock(&nfs_referral_count_list_lock);
  180. kfree(p);
  181. }
  182. static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
  183. const char *export_path)
  184. {
  185. struct dentry *dentry;
  186. int err;
  187. if (IS_ERR(root_mnt))
  188. return ERR_CAST(root_mnt);
  189. err = nfs_referral_loop_protect();
  190. if (err) {
  191. mntput(root_mnt);
  192. return ERR_PTR(err);
  193. }
  194. dentry = mount_subtree(root_mnt, export_path);
  195. nfs_referral_loop_unprotect();
  196. return dentry;
  197. }
  198. struct dentry *nfs4_try_mount(int flags, const char *dev_name,
  199. struct nfs_mount_info *mount_info)
  200. {
  201. char *export_path;
  202. struct vfsmount *root_mnt;
  203. struct dentry *res;
  204. struct nfs_parsed_mount_data *data = mount_info->parsed;
  205. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  206. mount_info->fill_super = nfs4_fill_super;
  207. export_path = data->nfs_server.export_path;
  208. data->nfs_server.export_path = "/";
  209. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
  210. data->nfs_server.hostname);
  211. data->nfs_server.export_path = export_path;
  212. res = nfs_follow_remote_path(root_mnt, export_path);
  213. dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
  214. IS_ERR(res) ? PTR_ERR(res) : 0,
  215. IS_ERR(res) ? " [error]" : "");
  216. return res;
  217. }
  218. /*
  219. * Clone an NFS4 server record on xdev traversal (FSID-change)
  220. */
  221. static struct dentry *
  222. nfs4_xdev_mount(struct file_system_type *fs_type, int flags,
  223. const char *dev_name, void *raw_data)
  224. {
  225. struct nfs_mount_info mount_info = {
  226. .fill_super = nfs_clone_super,
  227. .set_security = nfs_clone_sb_security,
  228. .cloned = raw_data,
  229. };
  230. return nfs_xdev_mount_common(&nfs4_fs_type, flags, dev_name, &mount_info);
  231. }
  232. static struct dentry *
  233. nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
  234. const char *dev_name, void *raw_data)
  235. {
  236. struct nfs_mount_info mount_info = {
  237. .fill_super = nfs4_fill_super,
  238. .set_security = nfs_clone_sb_security,
  239. .cloned = raw_data,
  240. };
  241. struct nfs_server *server;
  242. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  243. dprintk("--> nfs4_referral_get_sb()\n");
  244. mount_info.mntfh = nfs_alloc_fhandle();
  245. if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
  246. goto out;
  247. /* create a new volume representation */
  248. server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
  249. if (IS_ERR(server)) {
  250. mntroot = ERR_CAST(server);
  251. goto out;
  252. }
  253. mntroot = nfs_fs_mount_common(&nfs4_fs_type, server, flags, dev_name, &mount_info);
  254. out:
  255. nfs_free_fhandle(mount_info.mntfh);
  256. return mntroot;
  257. }
  258. /*
  259. * Create an NFS4 server record on referral traversal
  260. */
  261. static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
  262. int flags, const char *dev_name, void *raw_data)
  263. {
  264. struct nfs_clone_mount *data = raw_data;
  265. char *export_path;
  266. struct vfsmount *root_mnt;
  267. struct dentry *res;
  268. dprintk("--> nfs4_referral_mount()\n");
  269. export_path = data->mnt_path;
  270. data->mnt_path = "/";
  271. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  272. flags, data, data->hostname);
  273. data->mnt_path = export_path;
  274. res = nfs_follow_remote_path(root_mnt, export_path);
  275. dprintk("<-- nfs4_referral_mount() = %ld%s\n",
  276. IS_ERR(res) ? PTR_ERR(res) : 0,
  277. IS_ERR(res) ? " [error]" : "");
  278. return res;
  279. }
  280. int __init init_nfs_v4(void)
  281. {
  282. int err;
  283. err = nfs_idmap_init();
  284. if (err)
  285. goto out;
  286. err = nfs4_register_sysctl();
  287. if (err)
  288. goto out1;
  289. err = register_filesystem(&nfs4_fs_type);
  290. if (err < 0)
  291. goto out2;
  292. return 0;
  293. out2:
  294. nfs4_unregister_sysctl();
  295. out1:
  296. nfs_idmap_quit();
  297. out:
  298. return err;
  299. }
  300. void exit_nfs_v4(void)
  301. {
  302. unregister_filesystem(&nfs4_fs_type);
  303. nfs4_unregister_sysctl();
  304. nfs_idmap_quit();
  305. }