namespace.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_in - pointer to dentry
  29. * @buffer - result buffer
  30. * @buflen_in - 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_in, char *buffer,
  46. ssize_t buflen_in, unsigned flags)
  47. {
  48. char *end;
  49. int namelen;
  50. unsigned seq;
  51. const char *base;
  52. struct dentry *dentry;
  53. ssize_t buflen;
  54. rename_retry:
  55. buflen = buflen_in;
  56. dentry = dentry_in;
  57. end = buffer+buflen;
  58. *--end = '\0';
  59. buflen--;
  60. seq = read_seqbegin(&rename_lock);
  61. rcu_read_lock();
  62. while (1) {
  63. spin_lock(&dentry->d_lock);
  64. if (IS_ROOT(dentry))
  65. break;
  66. namelen = dentry->d_name.len;
  67. buflen -= namelen + 1;
  68. if (buflen < 0)
  69. goto Elong_unlock;
  70. end -= namelen;
  71. memcpy(end, dentry->d_name.name, namelen);
  72. *--end = '/';
  73. spin_unlock(&dentry->d_lock);
  74. dentry = dentry->d_parent;
  75. }
  76. if (read_seqretry(&rename_lock, seq)) {
  77. spin_unlock(&dentry->d_lock);
  78. rcu_read_unlock();
  79. goto rename_retry;
  80. }
  81. if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
  82. if (--buflen < 0) {
  83. spin_unlock(&dentry->d_lock);
  84. rcu_read_unlock();
  85. goto Elong;
  86. }
  87. *--end = '/';
  88. }
  89. *p = end;
  90. base = dentry->d_fsdata;
  91. if (!base) {
  92. spin_unlock(&dentry->d_lock);
  93. rcu_read_unlock();
  94. WARN_ON(1);
  95. return end;
  96. }
  97. namelen = strlen(base);
  98. if (*end == '/') {
  99. /* Strip off excess slashes in base string */
  100. while (namelen > 0 && base[namelen - 1] == '/')
  101. namelen--;
  102. }
  103. buflen -= namelen;
  104. if (buflen < 0) {
  105. spin_unlock(&dentry->d_lock);
  106. rcu_read_unlock();
  107. goto Elong;
  108. }
  109. end -= namelen;
  110. memcpy(end, base, namelen);
  111. spin_unlock(&dentry->d_lock);
  112. rcu_read_unlock();
  113. return end;
  114. Elong_unlock:
  115. spin_unlock(&dentry->d_lock);
  116. rcu_read_unlock();
  117. if (read_seqretry(&rename_lock, seq))
  118. goto rename_retry;
  119. Elong:
  120. return ERR_PTR(-ENAMETOOLONG);
  121. }
  122. EXPORT_SYMBOL_GPL(nfs_path);
  123. /*
  124. * nfs_d_automount - Handle crossing a mountpoint on the server
  125. * @path - The mountpoint
  126. *
  127. * When we encounter a mountpoint on the server, we want to set up
  128. * a mountpoint on the client too, to prevent inode numbers from
  129. * colliding, and to allow "df" to work properly.
  130. * On NFSv4, we also want to allow for the fact that different
  131. * filesystems may be migrated to different servers in a failover
  132. * situation, and that different filesystems may want to use
  133. * different security flavours.
  134. */
  135. struct vfsmount *nfs_d_automount(struct path *path)
  136. {
  137. struct vfsmount *mnt;
  138. struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
  139. struct nfs_fh *fh = NULL;
  140. struct nfs_fattr *fattr = NULL;
  141. if (IS_ROOT(path->dentry))
  142. return ERR_PTR(-ESTALE);
  143. mnt = ERR_PTR(-ENOMEM);
  144. fh = nfs_alloc_fhandle();
  145. fattr = nfs_alloc_fattr();
  146. if (fh == NULL || fattr == NULL)
  147. goto out;
  148. mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
  149. if (IS_ERR(mnt))
  150. goto out;
  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. return mnt;
  158. }
  159. static int
  160. nfs_namespace_getattr(const struct path *path, struct kstat *stat,
  161. u32 request_mask, unsigned int query_flags)
  162. {
  163. if (NFS_FH(d_inode(path->dentry))->size != 0)
  164. return nfs_getattr(path, stat, request_mask, query_flags);
  165. generic_fillattr(d_inode(path->dentry), stat);
  166. return 0;
  167. }
  168. static int
  169. nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
  170. {
  171. if (NFS_FH(d_inode(dentry))->size != 0)
  172. return nfs_setattr(dentry, attr);
  173. return -EACCES;
  174. }
  175. const struct inode_operations nfs_mountpoint_inode_operations = {
  176. .getattr = nfs_getattr,
  177. .setattr = nfs_setattr,
  178. };
  179. const struct inode_operations nfs_referral_inode_operations = {
  180. .getattr = nfs_namespace_getattr,
  181. .setattr = nfs_namespace_setattr,
  182. };
  183. static void nfs_expire_automounts(struct work_struct *work)
  184. {
  185. struct list_head *list = &nfs_automount_list;
  186. mark_mounts_for_expiry(list);
  187. if (!list_empty(list))
  188. schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  189. }
  190. void nfs_release_automount_timer(void)
  191. {
  192. if (list_empty(&nfs_automount_list))
  193. cancel_delayed_work(&nfs_automount_task);
  194. }
  195. /*
  196. * Clone a mountpoint of the appropriate type
  197. */
  198. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  199. const char *devname,
  200. struct nfs_clone_mount *mountdata)
  201. {
  202. return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
  203. }
  204. /**
  205. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  206. * @dentry - parent directory
  207. * @fh - filehandle for new root dentry
  208. * @fattr - attributes for new root inode
  209. * @authflavor - security flavor to use when performing the mount
  210. *
  211. */
  212. struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
  213. struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
  214. {
  215. struct nfs_clone_mount mountdata = {
  216. .sb = dentry->d_sb,
  217. .dentry = dentry,
  218. .fh = fh,
  219. .fattr = fattr,
  220. .authflavor = authflavor,
  221. };
  222. struct vfsmount *mnt;
  223. char *page = (char *) __get_free_page(GFP_USER);
  224. char *devname;
  225. if (page == NULL)
  226. return ERR_PTR(-ENOMEM);
  227. devname = nfs_devname(dentry, page, PAGE_SIZE);
  228. if (IS_ERR(devname))
  229. mnt = ERR_CAST(devname);
  230. else
  231. mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
  232. free_page((unsigned long)page);
  233. return mnt;
  234. }
  235. EXPORT_SYMBOL_GPL(nfs_do_submount);
  236. struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
  237. struct nfs_fh *fh, struct nfs_fattr *fattr)
  238. {
  239. int err;
  240. struct dentry *parent = dget_parent(dentry);
  241. /* Look it up again to get its attributes */
  242. err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
  243. dput(parent);
  244. if (err != 0)
  245. return ERR_PTR(err);
  246. return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
  247. }
  248. EXPORT_SYMBOL_GPL(nfs_submount);