mntpt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. const 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. size_t size;
  56. char *buf;
  57. int ret;
  58. _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
  59. /* read the contents of the symlink into the pagecache */
  60. page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
  61. if (IS_ERR(page)) {
  62. ret = PTR_ERR(page);
  63. goto out;
  64. }
  65. ret = -EIO;
  66. wait_on_page_locked(page);
  67. buf = kmap(page);
  68. if (!PageUptodate(page))
  69. goto out_free;
  70. if (PageError(page))
  71. goto out_free;
  72. /* examine the symlink's contents */
  73. size = vnode->status.size;
  74. _debug("symlink to %*.*s", size, (int) size, buf);
  75. if (size > 2 &&
  76. (buf[0] == '%' || buf[0] == '#') &&
  77. buf[size - 1] == '.'
  78. ) {
  79. _debug("symlink is a mountpoint");
  80. spin_lock(&vnode->lock);
  81. vnode->flags |= AFS_VNODE_MOUNTPOINT;
  82. spin_unlock(&vnode->lock);
  83. }
  84. ret = 0;
  85. out_free:
  86. kunmap(page);
  87. page_cache_release(page);
  88. out:
  89. _leave(" = %d", ret);
  90. return ret;
  91. } /* end afs_mntpt_check_symlink() */
  92. /*****************************************************************************/
  93. /*
  94. * no valid lookup procedure on this sort of dir
  95. */
  96. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  97. struct dentry *dentry,
  98. struct nameidata *nd)
  99. {
  100. kenter("%p,%p{%p{%s},%s}",
  101. dir,
  102. dentry,
  103. dentry->d_parent,
  104. dentry->d_parent ?
  105. dentry->d_parent->d_name.name : (const unsigned char *) "",
  106. dentry->d_name.name);
  107. return ERR_PTR(-EREMOTE);
  108. } /* end afs_mntpt_lookup() */
  109. /*****************************************************************************/
  110. /*
  111. * no valid open procedure on this sort of dir
  112. */
  113. static int afs_mntpt_open(struct inode *inode, struct file *file)
  114. {
  115. kenter("%p,%p{%p{%s},%s}",
  116. inode, file,
  117. file->f_dentry->d_parent,
  118. file->f_dentry->d_parent ?
  119. file->f_dentry->d_parent->d_name.name :
  120. (const unsigned char *) "",
  121. file->f_dentry->d_name.name);
  122. return -EREMOTE;
  123. } /* end afs_mntpt_open() */
  124. /*****************************************************************************/
  125. /*
  126. * create a vfsmount to be automounted
  127. */
  128. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  129. {
  130. struct afs_super_info *super;
  131. struct vfsmount *mnt;
  132. struct page *page = NULL;
  133. size_t size;
  134. char *buf, *devname = NULL, *options = NULL;
  135. int ret;
  136. kenter("{%s}", mntpt->d_name.name);
  137. BUG_ON(!mntpt->d_inode);
  138. ret = -EINVAL;
  139. size = mntpt->d_inode->i_size;
  140. if (size > PAGE_SIZE - 1)
  141. goto error;
  142. ret = -ENOMEM;
  143. devname = (char *) get_zeroed_page(GFP_KERNEL);
  144. if (!devname)
  145. goto error;
  146. options = (char *) get_zeroed_page(GFP_KERNEL);
  147. if (!options)
  148. goto error;
  149. /* read the contents of the AFS special symlink */
  150. page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
  151. if (IS_ERR(page)) {
  152. ret = PTR_ERR(page);
  153. goto error;
  154. }
  155. ret = -EIO;
  156. wait_on_page_locked(page);
  157. if (!PageUptodate(page) || PageError(page))
  158. goto error;
  159. buf = kmap(page);
  160. memcpy(devname, buf, size);
  161. kunmap(page);
  162. page_cache_release(page);
  163. page = NULL;
  164. /* work out what options we want */
  165. super = AFS_FS_S(mntpt->d_sb);
  166. memcpy(options, "cell=", 5);
  167. strcpy(options + 5, super->volume->cell->name);
  168. if (super->volume->type == AFSVL_RWVOL)
  169. strcat(options, ",rwpath");
  170. /* try and do the mount */
  171. kdebug("--- attempting mount %s -o %s ---", devname, options);
  172. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  173. kdebug("--- mount result %p ---", mnt);
  174. free_page((unsigned long) devname);
  175. free_page((unsigned long) options);
  176. kleave(" = %p", mnt);
  177. return mnt;
  178. error:
  179. if (page)
  180. page_cache_release(page);
  181. if (devname)
  182. free_page((unsigned long) devname);
  183. if (options)
  184. free_page((unsigned long) options);
  185. kleave(" = %d", ret);
  186. return ERR_PTR(ret);
  187. } /* end afs_mntpt_do_automount() */
  188. /*****************************************************************************/
  189. /*
  190. * follow a link from a mountpoint directory, thus causing it to be mounted
  191. */
  192. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
  193. {
  194. struct vfsmount *newmnt;
  195. struct dentry *old_dentry;
  196. int err;
  197. kenter("%p{%s},{%s:%p{%s}}",
  198. dentry,
  199. dentry->d_name.name,
  200. nd->mnt->mnt_devname,
  201. dentry,
  202. nd->dentry->d_name.name);
  203. newmnt = afs_mntpt_do_automount(dentry);
  204. if (IS_ERR(newmnt)) {
  205. path_release(nd);
  206. return (void *)newmnt;
  207. }
  208. old_dentry = nd->dentry;
  209. nd->dentry = dentry;
  210. err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
  211. nd->dentry = old_dentry;
  212. path_release(nd);
  213. if (!err) {
  214. mntget(newmnt);
  215. nd->mnt = newmnt;
  216. dget(newmnt->mnt_root);
  217. nd->dentry = newmnt->mnt_root;
  218. }
  219. kleave(" = %d", err);
  220. return ERR_PTR(err);
  221. } /* end afs_mntpt_follow_link() */
  222. /*****************************************************************************/
  223. /*
  224. * handle mountpoint expiry timer going off
  225. */
  226. static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
  227. {
  228. kenter("");
  229. mark_mounts_for_expiry(&afs_vfsmounts);
  230. afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
  231. afs_mntpt_expiry_timeout * HZ);
  232. kleave("");
  233. } /* end afs_mntpt_expiry_timed_out() */