namespace.c 6.4 KB

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