mntpt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/fs.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/gfp.h>
  19. #include "internal.h"
  20. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  21. struct dentry *dentry,
  22. struct nameidata *nd);
  23. static int afs_mntpt_open(struct inode *inode, struct file *file);
  24. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
  25. static void afs_mntpt_expiry_timed_out(struct work_struct *work);
  26. const struct file_operations afs_mntpt_file_operations = {
  27. .open = afs_mntpt_open,
  28. };
  29. const struct inode_operations afs_mntpt_inode_operations = {
  30. .lookup = afs_mntpt_lookup,
  31. .follow_link = afs_mntpt_follow_link,
  32. .readlink = page_readlink,
  33. .getattr = afs_getattr,
  34. };
  35. const struct inode_operations afs_autocell_inode_operations = {
  36. .follow_link = afs_mntpt_follow_link,
  37. .getattr = afs_getattr,
  38. };
  39. static LIST_HEAD(afs_vfsmounts);
  40. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  41. static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  42. /*
  43. * check a symbolic link to see whether it actually encodes a mountpoint
  44. * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
  45. */
  46. int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
  47. {
  48. struct page *page;
  49. size_t size;
  50. char *buf;
  51. int ret;
  52. _enter("{%x:%u,%u}",
  53. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  54. /* read the contents of the symlink into the pagecache */
  55. page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
  56. afs_page_filler, key);
  57. if (IS_ERR(page)) {
  58. ret = PTR_ERR(page);
  59. goto out;
  60. }
  61. ret = -EIO;
  62. if (PageError(page))
  63. goto out_free;
  64. buf = kmap(page);
  65. /* examine the symlink's contents */
  66. size = vnode->status.size;
  67. _debug("symlink to %*.*s", (int) size, (int) size, buf);
  68. if (size > 2 &&
  69. (buf[0] == '%' || buf[0] == '#') &&
  70. buf[size - 1] == '.'
  71. ) {
  72. _debug("symlink is a mountpoint");
  73. spin_lock(&vnode->lock);
  74. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  75. spin_unlock(&vnode->lock);
  76. }
  77. ret = 0;
  78. kunmap(page);
  79. out_free:
  80. page_cache_release(page);
  81. out:
  82. _leave(" = %d", ret);
  83. return ret;
  84. }
  85. /*
  86. * no valid lookup procedure on this sort of dir
  87. */
  88. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  89. struct dentry *dentry,
  90. struct nameidata *nd)
  91. {
  92. _enter("%p,%p{%p{%s},%s}",
  93. dir,
  94. dentry,
  95. dentry->d_parent,
  96. dentry->d_parent ?
  97. dentry->d_parent->d_name.name : (const unsigned char *) "",
  98. dentry->d_name.name);
  99. return ERR_PTR(-EREMOTE);
  100. }
  101. /*
  102. * no valid open procedure on this sort of dir
  103. */
  104. static int afs_mntpt_open(struct inode *inode, struct file *file)
  105. {
  106. _enter("%p,%p{%p{%s},%s}",
  107. inode, file,
  108. file->f_path.dentry->d_parent,
  109. file->f_path.dentry->d_parent ?
  110. file->f_path.dentry->d_parent->d_name.name :
  111. (const unsigned char *) "",
  112. file->f_path.dentry->d_name.name);
  113. return -EREMOTE;
  114. }
  115. /*
  116. * create a vfsmount to be automounted
  117. */
  118. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  119. {
  120. struct afs_super_info *super;
  121. struct vfsmount *mnt;
  122. struct afs_vnode *vnode;
  123. struct page *page;
  124. char *devname, *options;
  125. bool rwpath = false;
  126. int ret;
  127. _enter("{%s}", mntpt->d_name.name);
  128. BUG_ON(!mntpt->d_inode);
  129. ret = -ENOMEM;
  130. devname = (char *) get_zeroed_page(GFP_KERNEL);
  131. if (!devname)
  132. goto error_no_devname;
  133. options = (char *) get_zeroed_page(GFP_KERNEL);
  134. if (!options)
  135. goto error_no_options;
  136. vnode = AFS_FS_I(mntpt->d_inode);
  137. if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
  138. /* if the directory is a pseudo directory, use the d_name */
  139. static const char afs_root_cell[] = ":root.cell.";
  140. unsigned size = mntpt->d_name.len;
  141. ret = -ENOENT;
  142. if (size < 2 || size > AFS_MAXCELLNAME)
  143. goto error_no_page;
  144. if (mntpt->d_name.name[0] == '.') {
  145. devname[0] = '#';
  146. memcpy(devname + 1, mntpt->d_name.name, size - 1);
  147. memcpy(devname + size, afs_root_cell,
  148. sizeof(afs_root_cell));
  149. rwpath = true;
  150. } else {
  151. devname[0] = '%';
  152. memcpy(devname + 1, mntpt->d_name.name, size);
  153. memcpy(devname + size + 1, afs_root_cell,
  154. sizeof(afs_root_cell));
  155. }
  156. } else {
  157. /* read the contents of the AFS special symlink */
  158. loff_t size = i_size_read(mntpt->d_inode);
  159. char *buf;
  160. ret = -EINVAL;
  161. if (size > PAGE_SIZE - 1)
  162. goto error_no_page;
  163. page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
  164. if (IS_ERR(page)) {
  165. ret = PTR_ERR(page);
  166. goto error_no_page;
  167. }
  168. ret = -EIO;
  169. if (PageError(page))
  170. goto error;
  171. buf = kmap_atomic(page, KM_USER0);
  172. memcpy(devname, buf, size);
  173. kunmap_atomic(buf, KM_USER0);
  174. page_cache_release(page);
  175. page = NULL;
  176. }
  177. /* work out what options we want */
  178. super = AFS_FS_S(mntpt->d_sb);
  179. memcpy(options, "cell=", 5);
  180. strcpy(options + 5, super->volume->cell->name);
  181. if (super->volume->type == AFSVL_RWVOL || rwpath)
  182. strcat(options, ",rwpath");
  183. /* try and do the mount */
  184. _debug("--- attempting mount %s -o %s ---", devname, options);
  185. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  186. _debug("--- mount result %p ---", mnt);
  187. free_page((unsigned long) devname);
  188. free_page((unsigned long) options);
  189. _leave(" = %p", mnt);
  190. return mnt;
  191. error:
  192. page_cache_release(page);
  193. error_no_page:
  194. free_page((unsigned long) options);
  195. error_no_options:
  196. free_page((unsigned long) devname);
  197. error_no_devname:
  198. _leave(" = %d", ret);
  199. return ERR_PTR(ret);
  200. }
  201. /*
  202. * follow a link from a mountpoint directory, thus causing it to be mounted
  203. */
  204. static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
  205. {
  206. struct vfsmount *newmnt;
  207. int err;
  208. _enter("%p{%s},{%s:%p{%s},}",
  209. dentry,
  210. dentry->d_name.name,
  211. nd->path.mnt->mnt_devname,
  212. dentry,
  213. nd->path.dentry->d_name.name);
  214. dput(nd->path.dentry);
  215. nd->path.dentry = dget(dentry);
  216. newmnt = afs_mntpt_do_automount(nd->path.dentry);
  217. if (IS_ERR(newmnt)) {
  218. path_put(&nd->path);
  219. return (void *)newmnt;
  220. }
  221. mntget(newmnt);
  222. err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
  223. switch (err) {
  224. case 0:
  225. path_put(&nd->path);
  226. nd->path.mnt = newmnt;
  227. nd->path.dentry = dget(newmnt->mnt_root);
  228. schedule_delayed_work(&afs_mntpt_expiry_timer,
  229. afs_mntpt_expiry_timeout * HZ);
  230. break;
  231. case -EBUSY:
  232. /* someone else made a mount here whilst we were busy */
  233. while (d_mountpoint(nd->path.dentry) &&
  234. follow_down(&nd->path))
  235. ;
  236. err = 0;
  237. default:
  238. mntput(newmnt);
  239. break;
  240. }
  241. _leave(" = %d", err);
  242. return ERR_PTR(err);
  243. }
  244. /*
  245. * handle mountpoint expiry timer going off
  246. */
  247. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  248. {
  249. _enter("");
  250. if (!list_empty(&afs_vfsmounts)) {
  251. mark_mounts_for_expiry(&afs_vfsmounts);
  252. schedule_delayed_work(&afs_mntpt_expiry_timer,
  253. afs_mntpt_expiry_timeout * HZ);
  254. }
  255. _leave("");
  256. }
  257. /*
  258. * kill the AFS mountpoint timer if it's still running
  259. */
  260. void afs_mntpt_kill_timer(void)
  261. {
  262. _enter("");
  263. ASSERT(list_empty(&afs_vfsmounts));
  264. cancel_delayed_work(&afs_mntpt_expiry_timer);
  265. flush_scheduled_work();
  266. }