namespace.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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->nfs_client->rpc_ops->lookup(parent->d_inode,
  99. &nd->dentry->d_name,
  100. &fh, &fattr);
  101. dput(parent);
  102. if (err != 0)
  103. goto out_err;
  104. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
  105. mnt = nfs_do_refmount(nd->mnt, nd->dentry);
  106. else
  107. mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr);
  108. err = PTR_ERR(mnt);
  109. if (IS_ERR(mnt))
  110. goto out_err;
  111. mntget(mnt);
  112. err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list);
  113. if (err < 0) {
  114. mntput(mnt);
  115. if (err == -EBUSY)
  116. goto out_follow;
  117. goto out_err;
  118. }
  119. mntput(nd->mnt);
  120. dput(nd->dentry);
  121. nd->mnt = mnt;
  122. nd->dentry = dget(mnt->mnt_root);
  123. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  124. out:
  125. dprintk("%s: done, returned %d\n", __FUNCTION__, err);
  126. return ERR_PTR(err);
  127. out_err:
  128. path_release(nd);
  129. goto out;
  130. out_follow:
  131. while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
  132. ;
  133. err = 0;
  134. goto out;
  135. }
  136. struct inode_operations nfs_mountpoint_inode_operations = {
  137. .follow_link = nfs_follow_mountpoint,
  138. .getattr = nfs_getattr,
  139. };
  140. struct inode_operations nfs_referral_inode_operations = {
  141. .follow_link = nfs_follow_mountpoint,
  142. };
  143. static void nfs_expire_automounts(void *data)
  144. {
  145. struct list_head *list = (struct list_head *)data;
  146. mark_mounts_for_expiry(list);
  147. if (!list_empty(list))
  148. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  149. }
  150. void nfs_release_automount_timer(void)
  151. {
  152. if (list_empty(&nfs_automount_list)) {
  153. cancel_delayed_work(&nfs_automount_task);
  154. flush_scheduled_work();
  155. }
  156. }
  157. /*
  158. * Clone a mountpoint of the appropriate type
  159. */
  160. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  161. const char *devname,
  162. struct nfs_clone_mount *mountdata)
  163. {
  164. #ifdef CONFIG_NFS_V4
  165. struct vfsmount *mnt = NULL;
  166. switch (server->nfs_client->cl_nfsversion) {
  167. case 2:
  168. case 3:
  169. mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
  170. break;
  171. case 4:
  172. mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, mountdata);
  173. }
  174. return mnt;
  175. #else
  176. return vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
  177. #endif
  178. }
  179. /**
  180. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  181. * @mnt_parent - mountpoint of parent directory
  182. * @dentry - parent directory
  183. * @fh - filehandle for new root dentry
  184. * @fattr - attributes for new root inode
  185. *
  186. */
  187. struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
  188. const struct dentry *dentry, struct nfs_fh *fh,
  189. struct nfs_fattr *fattr)
  190. {
  191. struct nfs_clone_mount mountdata = {
  192. .sb = mnt_parent->mnt_sb,
  193. .dentry = dentry,
  194. .fh = fh,
  195. .fattr = fattr,
  196. };
  197. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  198. char *page = (char *) __get_free_page(GFP_USER);
  199. char *devname;
  200. dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
  201. dentry->d_parent->d_name.name,
  202. dentry->d_name.name);
  203. if (page == NULL)
  204. goto out;
  205. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  206. mnt = (struct vfsmount *)devname;
  207. if (IS_ERR(devname))
  208. goto free_page;
  209. mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
  210. free_page:
  211. free_page((unsigned long)page);
  212. out:
  213. dprintk("%s: done\n", __FUNCTION__);
  214. return mnt;
  215. }