namespace.c 5.8 KB

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