namespace.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. EXPORT_SYMBOL_GPL(nfs_path);
  110. /*
  111. * nfs_d_automount - Handle crossing a mountpoint on the server
  112. * @path - The mountpoint
  113. *
  114. * When we encounter a mountpoint on the server, we want to set up
  115. * a mountpoint on the client too, to prevent inode numbers from
  116. * colliding, and to allow "df" to work properly.
  117. * On NFSv4, we also want to allow for the fact that different
  118. * filesystems may be migrated to different servers in a failover
  119. * situation, and that different filesystems may want to use
  120. * different security flavours.
  121. */
  122. struct vfsmount *nfs_d_automount(struct path *path)
  123. {
  124. struct vfsmount *mnt;
  125. struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
  126. struct nfs_fh *fh = NULL;
  127. struct nfs_fattr *fattr = NULL;
  128. dprintk("--> nfs_d_automount()\n");
  129. mnt = ERR_PTR(-ESTALE);
  130. if (IS_ROOT(path->dentry))
  131. goto out_nofree;
  132. mnt = ERR_PTR(-ENOMEM);
  133. fh = nfs_alloc_fhandle();
  134. fattr = nfs_alloc_fattr();
  135. if (fh == NULL || fattr == NULL)
  136. goto out;
  137. dprintk("%s: enter\n", __func__);
  138. mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
  139. if (IS_ERR(mnt))
  140. goto out;
  141. dprintk("%s: done, success\n", __func__);
  142. mntget(mnt); /* prevent immediate expiration */
  143. mnt_set_expiry(mnt, &nfs_automount_list);
  144. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  145. out:
  146. nfs_free_fattr(fattr);
  147. nfs_free_fhandle(fh);
  148. out_nofree:
  149. if (IS_ERR(mnt))
  150. dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
  151. else
  152. dprintk("<-- %s() = %p\n", __func__, mnt);
  153. return mnt;
  154. }
  155. const struct inode_operations nfs_mountpoint_inode_operations = {
  156. .getattr = nfs_getattr,
  157. };
  158. const struct inode_operations nfs_referral_inode_operations = {
  159. };
  160. static void nfs_expire_automounts(struct work_struct *work)
  161. {
  162. struct list_head *list = &nfs_automount_list;
  163. mark_mounts_for_expiry(list);
  164. if (!list_empty(list))
  165. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  166. }
  167. void nfs_release_automount_timer(void)
  168. {
  169. if (list_empty(&nfs_automount_list))
  170. cancel_delayed_work(&nfs_automount_task);
  171. }
  172. /*
  173. * Clone a mountpoint of the appropriate type
  174. */
  175. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  176. const char *devname,
  177. struct nfs_clone_mount *mountdata)
  178. {
  179. return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  180. }
  181. /**
  182. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  183. * @dentry - parent directory
  184. * @fh - filehandle for new root dentry
  185. * @fattr - attributes for new root inode
  186. * @authflavor - security flavor to use when performing the mount
  187. *
  188. */
  189. struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
  190. struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
  191. {
  192. struct nfs_clone_mount mountdata = {
  193. .sb = dentry->d_sb,
  194. .dentry = dentry,
  195. .fh = fh,
  196. .fattr = fattr,
  197. .authflavor = authflavor,
  198. };
  199. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  200. char *page = (char *) __get_free_page(GFP_USER);
  201. char *devname;
  202. dprintk("--> nfs_do_submount()\n");
  203. dprintk("%s: submounting on %s/%s\n", __func__,
  204. dentry->d_parent->d_name.name,
  205. dentry->d_name.name);
  206. if (page == NULL)
  207. goto out;
  208. devname = nfs_devname(dentry, page, PAGE_SIZE);
  209. mnt = (struct vfsmount *)devname;
  210. if (IS_ERR(devname))
  211. goto free_page;
  212. mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
  213. free_page:
  214. free_page((unsigned long)page);
  215. out:
  216. dprintk("%s: done\n", __func__);
  217. dprintk("<-- nfs_do_submount() = %p\n", mnt);
  218. return mnt;
  219. }
  220. EXPORT_SYMBOL_GPL(nfs_do_submount);
  221. struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
  222. struct nfs_fh *fh, struct nfs_fattr *fattr)
  223. {
  224. int err;
  225. struct dentry *parent = dget_parent(dentry);
  226. /* Look it up again to get its attributes */
  227. err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
  228. dput(parent);
  229. if (err != 0)
  230. return ERR_PTR(err);
  231. return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
  232. }
  233. EXPORT_SYMBOL_GPL(nfs_submount);