namespace.c 6.6 KB

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