namespace.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 = buffer+buflen;
  47. int namelen;
  48. *--end = '\0';
  49. buflen--;
  50. spin_lock(&dcache_lock);
  51. while (!IS_ROOT(dentry) && dentry != droot) {
  52. namelen = dentry->d_name.len;
  53. buflen -= namelen + 1;
  54. if (buflen < 0)
  55. goto Elong_unlock;
  56. end -= namelen;
  57. memcpy(end, dentry->d_name.name, namelen);
  58. *--end = '/';
  59. dentry = dentry->d_parent;
  60. }
  61. spin_unlock(&dcache_lock);
  62. if (*end != '/') {
  63. if (--buflen < 0)
  64. goto Elong;
  65. *--end = '/';
  66. }
  67. namelen = strlen(base);
  68. /* Strip off excess slashes in base string */
  69. while (namelen > 0 && base[namelen - 1] == '/')
  70. namelen--;
  71. buflen -= namelen;
  72. if (buflen < 0)
  73. goto Elong;
  74. end -= namelen;
  75. memcpy(end, base, namelen);
  76. return end;
  77. Elong_unlock:
  78. spin_unlock(&dcache_lock);
  79. Elong:
  80. return ERR_PTR(-ENAMETOOLONG);
  81. }
  82. /*
  83. * nfs_follow_mountpoint - handle crossing a mountpoint on the server
  84. * @dentry - dentry of mountpoint
  85. * @nd - nameidata info
  86. *
  87. * When we encounter a mountpoint on the server, we want to set up
  88. * a mountpoint on the client too, to prevent inode numbers from
  89. * colliding, and to allow "df" to work properly.
  90. * On NFSv4, we also want to allow for the fact that different
  91. * filesystems may be migrated to different servers in a failover
  92. * situation, and that different filesystems may want to use
  93. * different security flavours.
  94. */
  95. static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
  96. {
  97. struct vfsmount *mnt;
  98. struct nfs_server *server = NFS_SERVER(dentry->d_inode);
  99. struct dentry *parent;
  100. struct nfs_fh fh;
  101. struct nfs_fattr fattr;
  102. int err;
  103. dprintk("--> nfs_follow_mountpoint()\n");
  104. err = -ESTALE;
  105. if (IS_ROOT(dentry))
  106. goto out_err;
  107. dprintk("%s: enter\n", __func__);
  108. dput(nd->path.dentry);
  109. nd->path.dentry = dget(dentry);
  110. /* Look it up again */
  111. parent = dget_parent(nd->path.dentry);
  112. err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
  113. &nd->path.dentry->d_name,
  114. &fh, &fattr);
  115. dput(parent);
  116. if (err != 0)
  117. goto out_err;
  118. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
  119. mnt = nfs_do_refmount(nd->path.mnt, nd->path.dentry);
  120. else
  121. mnt = nfs_do_submount(nd->path.mnt, nd->path.dentry, &fh,
  122. &fattr);
  123. err = PTR_ERR(mnt);
  124. if (IS_ERR(mnt))
  125. goto out_err;
  126. mntget(mnt);
  127. err = do_add_mount(mnt, &nd->path, nd->path.mnt->mnt_flags|MNT_SHRINKABLE,
  128. &nfs_automount_list);
  129. if (err < 0) {
  130. mntput(mnt);
  131. if (err == -EBUSY)
  132. goto out_follow;
  133. goto out_err;
  134. }
  135. path_put(&nd->path);
  136. nd->path.mnt = mnt;
  137. nd->path.dentry = dget(mnt->mnt_root);
  138. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  139. out:
  140. dprintk("%s: done, returned %d\n", __func__, err);
  141. dprintk("<-- nfs_follow_mountpoint() = %d\n", err);
  142. return ERR_PTR(err);
  143. out_err:
  144. path_put(&nd->path);
  145. goto out;
  146. out_follow:
  147. while (d_mountpoint(nd->path.dentry) &&
  148. follow_down(&nd->path))
  149. ;
  150. err = 0;
  151. goto out;
  152. }
  153. const struct inode_operations nfs_mountpoint_inode_operations = {
  154. .follow_link = nfs_follow_mountpoint,
  155. .getattr = nfs_getattr,
  156. };
  157. const struct inode_operations nfs_referral_inode_operations = {
  158. .follow_link = nfs_follow_mountpoint,
  159. };
  160. static void nfs_expire_automounts(struct work_struct *work)
  161. {
  162. struct list_head *list = &nfs_automount_list;
  163. mark_mounts_for_expiry(list);
  164. if (!list_empty(list))
  165. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  166. }
  167. void nfs_release_automount_timer(void)
  168. {
  169. if (list_empty(&nfs_automount_list))
  170. cancel_delayed_work(&nfs_automount_task);
  171. }
  172. /*
  173. * Clone a mountpoint of the appropriate type
  174. */
  175. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  176. const char *devname,
  177. struct nfs_clone_mount *mountdata)
  178. {
  179. #ifdef CONFIG_NFS_V4
  180. struct vfsmount *mnt = ERR_PTR(-EINVAL);
  181. switch (server->nfs_client->rpc_ops->version) {
  182. case 2:
  183. case 3:
  184. mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  185. break;
  186. case 4:
  187. mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
  188. }
  189. return mnt;
  190. #else
  191. return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  192. #endif
  193. }
  194. /**
  195. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  196. * @mnt_parent - mountpoint of parent directory
  197. * @dentry - parent directory
  198. * @fh - filehandle for new root dentry
  199. * @fattr - attributes for new root inode
  200. *
  201. */
  202. static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
  203. const struct dentry *dentry,
  204. struct nfs_fh *fh,
  205. struct nfs_fattr *fattr)
  206. {
  207. struct nfs_clone_mount mountdata = {
  208. .sb = mnt_parent->mnt_sb,
  209. .dentry = dentry,
  210. .fh = fh,
  211. .fattr = fattr,
  212. };
  213. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  214. char *page = (char *) __get_free_page(GFP_USER);
  215. char *devname;
  216. dprintk("--> nfs_do_submount()\n");
  217. dprintk("%s: submounting on %s/%s\n", __func__,
  218. dentry->d_parent->d_name.name,
  219. dentry->d_name.name);
  220. if (page == NULL)
  221. goto out;
  222. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  223. mnt = (struct vfsmount *)devname;
  224. if (IS_ERR(devname))
  225. goto free_page;
  226. mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
  227. free_page:
  228. free_page((unsigned long)page);
  229. out:
  230. dprintk("%s: done\n", __func__);
  231. dprintk("<-- nfs_do_submount() = %p\n", mnt);
  232. return mnt;
  233. }