namespace.c 7.0 KB

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