namespace.c 6.5 KB

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