mntpt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* mntpt.c: mountpoint management
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/fs.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/mount.h>
  19. #include <linux/namei.h>
  20. #include <linux/namespace.h>
  21. #include "super.h"
  22. #include "cell.h"
  23. #include "volume.h"
  24. #include "vnode.h"
  25. #include "internal.h"
  26. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  27. struct dentry *dentry,
  28. struct nameidata *nd);
  29. static int afs_mntpt_open(struct inode *inode, struct file *file);
  30. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
  31. struct file_operations afs_mntpt_file_operations = {
  32. .open = afs_mntpt_open,
  33. };
  34. struct inode_operations afs_mntpt_inode_operations = {
  35. .lookup = afs_mntpt_lookup,
  36. .follow_link = afs_mntpt_follow_link,
  37. .readlink = page_readlink,
  38. .getattr = afs_inode_getattr,
  39. };
  40. static LIST_HEAD(afs_vfsmounts);
  41. static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
  42. struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
  43. .timed_out = afs_mntpt_expiry_timed_out,
  44. };
  45. struct afs_timer afs_mntpt_expiry_timer;
  46. unsigned long afs_mntpt_expiry_timeout = 20;
  47. /*****************************************************************************/
  48. /*
  49. * check a symbolic link to see whether it actually encodes a mountpoint
  50. * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
  51. */
  52. int afs_mntpt_check_symlink(struct afs_vnode *vnode)
  53. {
  54. struct page *page;
  55. filler_t *filler;
  56. size_t size;
  57. char *buf;
  58. int ret;
  59. _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
  60. /* read the contents of the symlink into the pagecache */
  61. filler = (filler_t *) AFS_VNODE_TO_I(vnode)->i_mapping->a_ops->readpage;
  62. page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
  63. filler, NULL);
  64. if (IS_ERR(page)) {
  65. ret = PTR_ERR(page);
  66. goto out;
  67. }
  68. ret = -EIO;
  69. wait_on_page_locked(page);
  70. buf = kmap(page);
  71. if (!PageUptodate(page))
  72. goto out_free;
  73. if (PageError(page))
  74. goto out_free;
  75. /* examine the symlink's contents */
  76. size = vnode->status.size;
  77. _debug("symlink to %*.*s", size, (int) size, buf);
  78. if (size > 2 &&
  79. (buf[0] == '%' || buf[0] == '#') &&
  80. buf[size - 1] == '.'
  81. ) {
  82. _debug("symlink is a mountpoint");
  83. spin_lock(&vnode->lock);
  84. vnode->flags |= AFS_VNODE_MOUNTPOINT;
  85. spin_unlock(&vnode->lock);
  86. }
  87. ret = 0;
  88. out_free:
  89. kunmap(page);
  90. page_cache_release(page);
  91. out:
  92. _leave(" = %d", ret);
  93. return ret;
  94. } /* end afs_mntpt_check_symlink() */
  95. /*****************************************************************************/
  96. /*
  97. * no valid lookup procedure on this sort of dir
  98. */
  99. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  100. struct dentry *dentry,
  101. struct nameidata *nd)
  102. {
  103. kenter("%p,%p{%p{%s},%s}",
  104. dir,
  105. dentry,
  106. dentry->d_parent,
  107. dentry->d_parent ?
  108. dentry->d_parent->d_name.name : (const unsigned char *) "",
  109. dentry->d_name.name);
  110. return ERR_PTR(-EREMOTE);
  111. } /* end afs_mntpt_lookup() */
  112. /*****************************************************************************/
  113. /*
  114. * no valid open procedure on this sort of dir
  115. */
  116. static int afs_mntpt_open(struct inode *inode, struct file *file)
  117. {
  118. kenter("%p,%p{%p{%s},%s}",
  119. inode, file,
  120. file->f_dentry->d_parent,
  121. file->f_dentry->d_parent ?
  122. file->f_dentry->d_parent->d_name.name :
  123. (const unsigned char *) "",
  124. file->f_dentry->d_name.name);
  125. return -EREMOTE;
  126. } /* end afs_mntpt_open() */
  127. /*****************************************************************************/
  128. /*
  129. * create a vfsmount to be automounted
  130. */
  131. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  132. {
  133. struct afs_super_info *super;
  134. struct vfsmount *mnt;
  135. struct page *page = NULL;
  136. size_t size;
  137. char *buf, *devname = NULL, *options = NULL;
  138. filler_t *filler;
  139. int ret;
  140. kenter("{%s}", mntpt->d_name.name);
  141. BUG_ON(!mntpt->d_inode);
  142. ret = -EINVAL;
  143. size = mntpt->d_inode->i_size;
  144. if (size > PAGE_SIZE - 1)
  145. goto error;
  146. ret = -ENOMEM;
  147. devname = (char *) get_zeroed_page(GFP_KERNEL);
  148. if (!devname)
  149. goto error;
  150. options = (char *) get_zeroed_page(GFP_KERNEL);
  151. if (!options)
  152. goto error;
  153. /* read the contents of the AFS special symlink */
  154. filler = (filler_t *)mntpt->d_inode->i_mapping->a_ops->readpage;
  155. page = read_cache_page(mntpt->d_inode->i_mapping, 0, filler, NULL);
  156. if (IS_ERR(page)) {
  157. ret = PTR_ERR(page);
  158. goto error;
  159. }
  160. ret = -EIO;
  161. wait_on_page_locked(page);
  162. if (!PageUptodate(page) || PageError(page))
  163. goto error;
  164. buf = kmap(page);
  165. memcpy(devname, buf, size);
  166. kunmap(page);
  167. page_cache_release(page);
  168. page = NULL;
  169. /* work out what options we want */
  170. super = AFS_FS_S(mntpt->d_sb);
  171. memcpy(options, "cell=", 5);
  172. strcpy(options + 5, super->volume->cell->name);
  173. if (super->volume->type == AFSVL_RWVOL)
  174. strcat(options, ",rwpath");
  175. /* try and do the mount */
  176. kdebug("--- attempting mount %s -o %s ---", devname, options);
  177. mnt = do_kern_mount("afs", 0, devname, options);
  178. kdebug("--- mount result %p ---", mnt);
  179. free_page((unsigned long) devname);
  180. free_page((unsigned long) options);
  181. kleave(" = %p", mnt);
  182. return mnt;
  183. error:
  184. if (page)
  185. page_cache_release(page);
  186. if (devname)
  187. free_page((unsigned long) devname);
  188. if (options)
  189. free_page((unsigned long) options);
  190. kleave(" = %d", ret);
  191. return ERR_PTR(ret);
  192. } /* end afs_mntpt_do_automount() */
  193. /*****************************************************************************/
  194. /*
  195. * follow a link from a mountpoint directory, thus causing it to be mounted
  196. */
  197. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
  198. {
  199. struct vfsmount *newmnt;
  200. struct dentry *old_dentry;
  201. int err;
  202. kenter("%p{%s},{%s:%p{%s}}",
  203. dentry,
  204. dentry->d_name.name,
  205. nd->mnt->mnt_devname,
  206. dentry,
  207. nd->dentry->d_name.name);
  208. newmnt = afs_mntpt_do_automount(dentry);
  209. if (IS_ERR(newmnt)) {
  210. path_release(nd);
  211. return (void *)newmnt;
  212. }
  213. old_dentry = nd->dentry;
  214. nd->dentry = dentry;
  215. err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
  216. nd->dentry = old_dentry;
  217. path_release(nd);
  218. if (!err) {
  219. mntget(newmnt);
  220. nd->mnt = newmnt;
  221. dget(newmnt->mnt_root);
  222. nd->dentry = newmnt->mnt_root;
  223. }
  224. kleave(" = %d", err);
  225. return ERR_PTR(err);
  226. } /* end afs_mntpt_follow_link() */
  227. /*****************************************************************************/
  228. /*
  229. * handle mountpoint expiry timer going off
  230. */
  231. static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
  232. {
  233. kenter("");
  234. mark_mounts_for_expiry(&afs_vfsmounts);
  235. afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
  236. afs_mntpt_expiry_timeout * HZ);
  237. kleave("");
  238. } /* end afs_mntpt_expiry_timed_out() */