namespace.c 6.5 KB

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