namespace.c 5.9 KB

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