namespace.c 7.7 KB

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