namespace.c 6.8 KB

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