mntpt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. unsigned int flags);
  23. static int afs_mntpt_open(struct inode *inode, struct file *file);
  24. static void afs_mntpt_expiry_timed_out(struct work_struct *work);
  25. const struct file_operations afs_mntpt_file_operations = {
  26. .open = afs_mntpt_open,
  27. .llseek = noop_llseek,
  28. };
  29. const struct inode_operations afs_mntpt_inode_operations = {
  30. .lookup = afs_mntpt_lookup,
  31. .readlink = page_readlink,
  32. .getattr = afs_getattr,
  33. };
  34. const struct inode_operations afs_autocell_inode_operations = {
  35. .getattr = afs_getattr,
  36. };
  37. static LIST_HEAD(afs_vfsmounts);
  38. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  39. static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  40. /*
  41. * check a symbolic link to see whether it actually encodes a mountpoint
  42. * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
  43. */
  44. int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
  45. {
  46. struct page *page;
  47. size_t size;
  48. char *buf;
  49. int ret;
  50. _enter("{%x:%u,%u}",
  51. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  52. /* read the contents of the symlink into the pagecache */
  53. page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
  54. afs_page_filler, key);
  55. if (IS_ERR(page)) {
  56. ret = PTR_ERR(page);
  57. goto out;
  58. }
  59. ret = -EIO;
  60. if (PageError(page))
  61. goto out_free;
  62. buf = kmap(page);
  63. /* examine the symlink's contents */
  64. size = vnode->status.size;
  65. _debug("symlink to %*.*s", (int) size, (int) size, buf);
  66. if (size > 2 &&
  67. (buf[0] == '%' || buf[0] == '#') &&
  68. buf[size - 1] == '.'
  69. ) {
  70. _debug("symlink is a mountpoint");
  71. spin_lock(&vnode->lock);
  72. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  73. vnode->vfs_inode.i_flags |= S_AUTOMOUNT;
  74. spin_unlock(&vnode->lock);
  75. }
  76. ret = 0;
  77. kunmap(page);
  78. out_free:
  79. page_cache_release(page);
  80. out:
  81. _leave(" = %d", ret);
  82. return ret;
  83. }
  84. /*
  85. * no valid lookup procedure on this sort of dir
  86. */
  87. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  88. struct dentry *dentry,
  89. unsigned int flags)
  90. {
  91. _enter("%p,%p{%pd2}", dir, dentry, dentry);
  92. return ERR_PTR(-EREMOTE);
  93. }
  94. /*
  95. * no valid open procedure on this sort of dir
  96. */
  97. static int afs_mntpt_open(struct inode *inode, struct file *file)
  98. {
  99. _enter("%p,%p{%pD2}", inode, file, file);
  100. return -EREMOTE;
  101. }
  102. /*
  103. * create a vfsmount to be automounted
  104. */
  105. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  106. {
  107. struct afs_super_info *super;
  108. struct vfsmount *mnt;
  109. struct afs_vnode *vnode;
  110. struct page *page;
  111. char *devname, *options;
  112. bool rwpath = false;
  113. int ret;
  114. _enter("{%pd}", mntpt);
  115. BUG_ON(!d_inode(mntpt));
  116. ret = -ENOMEM;
  117. devname = (char *) get_zeroed_page(GFP_KERNEL);
  118. if (!devname)
  119. goto error_no_devname;
  120. options = (char *) get_zeroed_page(GFP_KERNEL);
  121. if (!options)
  122. goto error_no_options;
  123. vnode = AFS_FS_I(d_inode(mntpt));
  124. if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
  125. /* if the directory is a pseudo directory, use the d_name */
  126. static const char afs_root_cell[] = ":root.cell.";
  127. unsigned size = mntpt->d_name.len;
  128. ret = -ENOENT;
  129. if (size < 2 || size > AFS_MAXCELLNAME)
  130. goto error_no_page;
  131. if (mntpt->d_name.name[0] == '.') {
  132. devname[0] = '#';
  133. memcpy(devname + 1, mntpt->d_name.name, size - 1);
  134. memcpy(devname + size, afs_root_cell,
  135. sizeof(afs_root_cell));
  136. rwpath = true;
  137. } else {
  138. devname[0] = '%';
  139. memcpy(devname + 1, mntpt->d_name.name, size);
  140. memcpy(devname + size + 1, afs_root_cell,
  141. sizeof(afs_root_cell));
  142. }
  143. } else {
  144. /* read the contents of the AFS special symlink */
  145. loff_t size = i_size_read(d_inode(mntpt));
  146. char *buf;
  147. ret = -EINVAL;
  148. if (size > PAGE_SIZE - 1)
  149. goto error_no_page;
  150. page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
  151. if (IS_ERR(page)) {
  152. ret = PTR_ERR(page);
  153. goto error_no_page;
  154. }
  155. ret = -EIO;
  156. if (PageError(page))
  157. goto error;
  158. buf = kmap_atomic(page);
  159. memcpy(devname, buf, size);
  160. kunmap_atomic(buf);
  161. page_cache_release(page);
  162. page = NULL;
  163. }
  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 || rwpath)
  169. strcat(options, ",rwpath");
  170. /* try and do the mount */
  171. _debug("--- attempting mount %s -o %s ---", devname, options);
  172. mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
  173. _debug("--- mount result %p ---", mnt);
  174. free_page((unsigned long) devname);
  175. free_page((unsigned long) options);
  176. _leave(" = %p", mnt);
  177. return mnt;
  178. error:
  179. page_cache_release(page);
  180. error_no_page:
  181. free_page((unsigned long) options);
  182. error_no_options:
  183. free_page((unsigned long) devname);
  184. error_no_devname:
  185. _leave(" = %d", ret);
  186. return ERR_PTR(ret);
  187. }
  188. /*
  189. * handle an automount point
  190. */
  191. struct vfsmount *afs_d_automount(struct path *path)
  192. {
  193. struct vfsmount *newmnt;
  194. _enter("{%pd}", path->dentry);
  195. newmnt = afs_mntpt_do_automount(path->dentry);
  196. if (IS_ERR(newmnt))
  197. return newmnt;
  198. mntget(newmnt); /* prevent immediate expiration */
  199. mnt_set_expiry(newmnt, &afs_vfsmounts);
  200. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  201. afs_mntpt_expiry_timeout * HZ);
  202. _leave(" = %p", newmnt);
  203. return newmnt;
  204. }
  205. /*
  206. * handle mountpoint expiry timer going off
  207. */
  208. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  209. {
  210. _enter("");
  211. if (!list_empty(&afs_vfsmounts)) {
  212. mark_mounts_for_expiry(&afs_vfsmounts);
  213. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  214. afs_mntpt_expiry_timeout * HZ);
  215. }
  216. _leave("");
  217. }
  218. /*
  219. * kill the AFS mountpoint timer if it's still running
  220. */
  221. void afs_mntpt_kill_timer(void)
  222. {
  223. _enter("");
  224. ASSERT(list_empty(&afs_vfsmounts));
  225. cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
  226. }