namespace.c 6.7 KB

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