mntpt.c 6.8 KB

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