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