namespace.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(d_inode(path->dentry));
  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. static int
  165. nfs_namespace_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  166. {
  167. if (NFS_FH(d_inode(dentry))->size != 0)
  168. return nfs_getattr(mnt, dentry, stat);
  169. generic_fillattr(d_inode(dentry), stat);
  170. return 0;
  171. }
  172. static int
  173. nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
  174. {
  175. if (NFS_FH(d_inode(dentry))->size != 0)
  176. return nfs_setattr(dentry, attr);
  177. return -EACCES;
  178. }
  179. const struct inode_operations nfs_mountpoint_inode_operations = {
  180. .getattr = nfs_getattr,
  181. .setattr = nfs_setattr,
  182. };
  183. const struct inode_operations nfs_referral_inode_operations = {
  184. .getattr = nfs_namespace_getattr,
  185. .setattr = nfs_namespace_setattr,
  186. };
  187. static void nfs_expire_automounts(struct work_struct *work)
  188. {
  189. struct list_head *list = &nfs_automount_list;
  190. mark_mounts_for_expiry(list);
  191. if (!list_empty(list))
  192. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  193. }
  194. void nfs_release_automount_timer(void)
  195. {
  196. if (list_empty(&nfs_automount_list))
  197. cancel_delayed_work(&nfs_automount_task);
  198. }
  199. /*
  200. * Clone a mountpoint of the appropriate type
  201. */
  202. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  203. const char *devname,
  204. struct nfs_clone_mount *mountdata)
  205. {
  206. return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
  207. }
  208. /**
  209. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  210. * @dentry - parent directory
  211. * @fh - filehandle for new root dentry
  212. * @fattr - attributes for new root inode
  213. * @authflavor - security flavor to use when performing the mount
  214. *
  215. */
  216. struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
  217. struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
  218. {
  219. struct nfs_clone_mount mountdata = {
  220. .sb = dentry->d_sb,
  221. .dentry = dentry,
  222. .fh = fh,
  223. .fattr = fattr,
  224. .authflavor = authflavor,
  225. };
  226. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  227. char *page = (char *) __get_free_page(GFP_USER);
  228. char *devname;
  229. dprintk("--> nfs_do_submount()\n");
  230. dprintk("%s: submounting on %pd2\n", __func__,
  231. dentry);
  232. if (page == NULL)
  233. goto out;
  234. devname = nfs_devname(dentry, page, PAGE_SIZE);
  235. mnt = (struct vfsmount *)devname;
  236. if (IS_ERR(devname))
  237. goto free_page;
  238. mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
  239. free_page:
  240. free_page((unsigned long)page);
  241. out:
  242. dprintk("%s: done\n", __func__);
  243. dprintk("<-- nfs_do_submount() = %p\n", mnt);
  244. return mnt;
  245. }
  246. EXPORT_SYMBOL_GPL(nfs_do_submount);
  247. struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
  248. struct nfs_fh *fh, struct nfs_fattr *fattr)
  249. {
  250. int err;
  251. struct dentry *parent = dget_parent(dentry);
  252. /* Look it up again to get its attributes */
  253. err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
  254. dput(parent);
  255. if (err != 0)
  256. return ERR_PTR(err);
  257. return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
  258. }
  259. EXPORT_SYMBOL_GPL(nfs_submount);