namespace.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * linux/fs/nfs/namespace.c
  3. *
  4. * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
  5. * - Modified by David Howells <dhowells@redhat.com>
  6. *
  7. * NFS namespace
  8. */
  9. #include <linux/dcache.h>
  10. #include <linux/gfp.h>
  11. #include <linux/mount.h>
  12. #include <linux/namei.h>
  13. #include <linux/nfs_fs.h>
  14. #include <linux/string.h>
  15. #include <linux/sunrpc/clnt.h>
  16. #include <linux/vfs.h>
  17. #include "internal.h"
  18. #define NFSDBG_FACILITY NFSDBG_VFS
  19. static void nfs_expire_automounts(struct work_struct *work);
  20. static LIST_HEAD(nfs_automount_list);
  21. static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
  22. int nfs_mountpoint_expiry_timeout = 500 * HZ;
  23. static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
  24. const struct dentry *dentry,
  25. struct nfs_fh *fh,
  26. struct nfs_fattr *fattr);
  27. /*
  28. * nfs_path - reconstruct the path given an arbitrary dentry
  29. * @base - arbitrary string to prepend to the path
  30. * @droot - pointer to root dentry for mountpoint
  31. * @dentry - pointer to dentry
  32. * @buffer - result buffer
  33. * @buflen - length of buffer
  34. *
  35. * Helper function for constructing the path from the
  36. * root dentry to an arbitrary hashed dentry.
  37. *
  38. * This is mainly for use in figuring out the path on the
  39. * server side when automounting on top of an existing partition.
  40. */
  41. char *nfs_path(const char *base,
  42. const struct dentry *droot,
  43. const struct dentry *dentry,
  44. char *buffer, ssize_t buflen)
  45. {
  46. char *end;
  47. int namelen;
  48. unsigned seq;
  49. rename_retry:
  50. end = buffer+buflen;
  51. *--end = '\0';
  52. buflen--;
  53. seq = read_seqbegin(&rename_lock);
  54. rcu_read_lock();
  55. while (!IS_ROOT(dentry) && dentry != droot) {
  56. namelen = dentry->d_name.len;
  57. buflen -= namelen + 1;
  58. if (buflen < 0)
  59. goto Elong_unlock;
  60. end -= namelen;
  61. memcpy(end, dentry->d_name.name, namelen);
  62. *--end = '/';
  63. dentry = dentry->d_parent;
  64. }
  65. rcu_read_unlock();
  66. if (read_seqretry(&rename_lock, seq))
  67. goto rename_retry;
  68. if (*end != '/') {
  69. if (--buflen < 0)
  70. goto Elong;
  71. *--end = '/';
  72. }
  73. namelen = strlen(base);
  74. /* Strip off excess slashes in base string */
  75. while (namelen > 0 && base[namelen - 1] == '/')
  76. namelen--;
  77. buflen -= namelen;
  78. if (buflen < 0)
  79. goto Elong;
  80. end -= namelen;
  81. memcpy(end, base, namelen);
  82. return end;
  83. Elong_unlock:
  84. rcu_read_unlock();
  85. if (read_seqretry(&rename_lock, seq))
  86. goto rename_retry;
  87. Elong:
  88. return ERR_PTR(-ENAMETOOLONG);
  89. }
  90. /*
  91. * nfs_follow_mountpoint - handle crossing a mountpoint on the server
  92. * @dentry - dentry of mountpoint
  93. * @nd - nameidata info
  94. *
  95. * When we encounter a mountpoint on the server, we want to set up
  96. * a mountpoint on the client too, to prevent inode numbers from
  97. * colliding, and to allow "df" to work properly.
  98. * On NFSv4, we also want to allow for the fact that different
  99. * filesystems may be migrated to different servers in a failover
  100. * situation, and that different filesystems may want to use
  101. * different security flavours.
  102. */
  103. static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
  104. {
  105. struct vfsmount *mnt;
  106. struct nfs_server *server = NFS_SERVER(dentry->d_inode);
  107. struct dentry *parent;
  108. struct nfs_fh *fh = NULL;
  109. struct nfs_fattr *fattr = NULL;
  110. int err;
  111. dprintk("--> nfs_follow_mountpoint()\n");
  112. err = -ESTALE;
  113. if (IS_ROOT(dentry))
  114. goto out_err;
  115. err = -ENOMEM;
  116. fh = nfs_alloc_fhandle();
  117. fattr = nfs_alloc_fattr();
  118. if (fh == NULL || fattr == NULL)
  119. goto out_err;
  120. dprintk("%s: enter\n", __func__);
  121. dput(nd->path.dentry);
  122. nd->path.dentry = dget(dentry);
  123. /* Look it up again */
  124. parent = dget_parent(nd->path.dentry);
  125. err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
  126. &nd->path.dentry->d_name,
  127. fh, fattr);
  128. dput(parent);
  129. if (err != 0)
  130. goto out_err;
  131. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  132. mnt = nfs_do_refmount(nd->path.mnt, nd->path.dentry);
  133. else
  134. mnt = nfs_do_submount(nd->path.mnt, nd->path.dentry, fh,
  135. fattr);
  136. err = PTR_ERR(mnt);
  137. if (IS_ERR(mnt))
  138. goto out_err;
  139. mntget(mnt);
  140. err = do_add_mount(mnt, &nd->path, nd->path.mnt->mnt_flags|MNT_SHRINKABLE,
  141. &nfs_automount_list);
  142. if (err < 0) {
  143. mntput(mnt);
  144. if (err == -EBUSY)
  145. goto out_follow;
  146. goto out_err;
  147. }
  148. path_put(&nd->path);
  149. nd->path.mnt = mnt;
  150. nd->path.dentry = dget(mnt->mnt_root);
  151. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  152. out:
  153. nfs_free_fattr(fattr);
  154. nfs_free_fhandle(fh);
  155. dprintk("%s: done, returned %d\n", __func__, err);
  156. dprintk("<-- nfs_follow_mountpoint() = %d\n", err);
  157. return ERR_PTR(err);
  158. out_err:
  159. path_put(&nd->path);
  160. goto out;
  161. out_follow:
  162. err = follow_down(&nd->path, false);
  163. goto out;
  164. }
  165. const struct inode_operations nfs_mountpoint_inode_operations = {
  166. .follow_link = nfs_follow_mountpoint,
  167. .getattr = nfs_getattr,
  168. };
  169. const struct inode_operations nfs_referral_inode_operations = {
  170. .follow_link = nfs_follow_mountpoint,
  171. };
  172. static void nfs_expire_automounts(struct work_struct *work)
  173. {
  174. struct list_head *list = &nfs_automount_list;
  175. mark_mounts_for_expiry(list);
  176. if (!list_empty(list))
  177. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  178. }
  179. void nfs_release_automount_timer(void)
  180. {
  181. if (list_empty(&nfs_automount_list))
  182. cancel_delayed_work(&nfs_automount_task);
  183. }
  184. /*
  185. * Clone a mountpoint of the appropriate type
  186. */
  187. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  188. const char *devname,
  189. struct nfs_clone_mount *mountdata)
  190. {
  191. #ifdef CONFIG_NFS_V4
  192. struct vfsmount *mnt = ERR_PTR(-EINVAL);
  193. switch (server->nfs_client->rpc_ops->version) {
  194. case 2:
  195. case 3:
  196. mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  197. break;
  198. case 4:
  199. mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
  200. }
  201. return mnt;
  202. #else
  203. return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  204. #endif
  205. }
  206. /**
  207. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  208. * @mnt_parent - mountpoint of parent directory
  209. * @dentry - parent directory
  210. * @fh - filehandle for new root dentry
  211. * @fattr - attributes for new root inode
  212. *
  213. */
  214. static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
  215. const struct dentry *dentry,
  216. struct nfs_fh *fh,
  217. struct nfs_fattr *fattr)
  218. {
  219. struct nfs_clone_mount mountdata = {
  220. .sb = mnt_parent->mnt_sb,
  221. .dentry = dentry,
  222. .fh = fh,
  223. .fattr = fattr,
  224. };
  225. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  226. char *page = (char *) __get_free_page(GFP_USER);
  227. char *devname;
  228. dprintk("--> nfs_do_submount()\n");
  229. dprintk("%s: submounting on %s/%s\n", __func__,
  230. dentry->d_parent->d_name.name,
  231. dentry->d_name.name);
  232. if (page == NULL)
  233. goto out;
  234. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  235. mnt = (struct vfsmount *)devname;
  236. if (IS_ERR(devname))
  237. goto free_page;
  238. mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
  239. free_page:
  240. free_page((unsigned long)page);
  241. out:
  242. dprintk("%s: done\n", __func__);
  243. dprintk("<-- nfs_do_submount() = %p\n", mnt);
  244. return mnt;
  245. }