namespace.c 6.2 KB

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