mntpt.c 6.1 KB

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