mntpt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* mountpoint management
  3. *
  4. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/gfp.h>
  15. #include <linux/fs_context.h>
  16. #include "internal.h"
  17. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  18. struct dentry *dentry,
  19. unsigned int flags);
  20. static int afs_mntpt_open(struct inode *inode, struct file *file);
  21. static void afs_mntpt_expiry_timed_out(struct work_struct *work);
  22. const struct file_operations afs_mntpt_file_operations = {
  23. .open = afs_mntpt_open,
  24. .llseek = noop_llseek,
  25. };
  26. const struct inode_operations afs_mntpt_inode_operations = {
  27. .lookup = afs_mntpt_lookup,
  28. .readlink = page_readlink,
  29. .getattr = afs_getattr,
  30. };
  31. const struct inode_operations afs_autocell_inode_operations = {
  32. .getattr = afs_getattr,
  33. };
  34. static LIST_HEAD(afs_vfsmounts);
  35. static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
  36. static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
  37. static const char afs_root_volume[] = "root.cell";
  38. /*
  39. * no valid lookup procedure on this sort of dir
  40. */
  41. static struct dentry *afs_mntpt_lookup(struct inode *dir,
  42. struct dentry *dentry,
  43. unsigned int flags)
  44. {
  45. _enter("%p,%p{%pd2}", dir, dentry, dentry);
  46. return ERR_PTR(-EREMOTE);
  47. }
  48. /*
  49. * no valid open procedure on this sort of dir
  50. */
  51. static int afs_mntpt_open(struct inode *inode, struct file *file)
  52. {
  53. _enter("%p,%p{%pD2}", inode, file, file);
  54. return -EREMOTE;
  55. }
  56. /*
  57. * Set the parameters for the proposed superblock.
  58. */
  59. static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
  60. {
  61. struct afs_fs_context *ctx = fc->fs_private;
  62. struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
  63. struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
  64. struct afs_cell *cell;
  65. const char *p;
  66. int ret;
  67. if (fc->net_ns != src_as->net_ns) {
  68. put_net(fc->net_ns);
  69. fc->net_ns = get_net(src_as->net_ns);
  70. }
  71. if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
  72. ctx->type = AFSVL_RWVOL;
  73. ctx->force = true;
  74. }
  75. if (ctx->cell) {
  76. afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt);
  77. ctx->cell = NULL;
  78. }
  79. if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
  80. /* if the directory is a pseudo directory, use the d_name */
  81. unsigned size = mntpt->d_name.len;
  82. if (size < 2)
  83. return -ENOENT;
  84. p = mntpt->d_name.name;
  85. if (mntpt->d_name.name[0] == '.') {
  86. size--;
  87. p++;
  88. ctx->type = AFSVL_RWVOL;
  89. ctx->force = true;
  90. }
  91. if (size > AFS_MAXCELLNAME)
  92. return -ENAMETOOLONG;
  93. cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
  94. if (IS_ERR(cell)) {
  95. pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
  96. return PTR_ERR(cell);
  97. }
  98. ctx->cell = cell;
  99. ctx->volname = afs_root_volume;
  100. ctx->volnamesz = sizeof(afs_root_volume) - 1;
  101. } else {
  102. /* read the contents of the AFS special symlink */
  103. struct page *page;
  104. loff_t size = i_size_read(d_inode(mntpt));
  105. char *buf;
  106. if (src_as->cell)
  107. ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
  108. if (size < 2 || size > PAGE_SIZE - 1)
  109. return -EINVAL;
  110. page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
  111. if (IS_ERR(page))
  112. return PTR_ERR(page);
  113. buf = kmap(page);
  114. ret = -EINVAL;
  115. if (buf[size - 1] == '.')
  116. ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
  117. kunmap(page);
  118. put_page(page);
  119. if (ret < 0)
  120. return ret;
  121. /* Don't cross a backup volume mountpoint from a backup volume */
  122. if (src_as->volume && src_as->volume->type == AFSVL_BACKVOL &&
  123. ctx->type == AFSVL_BACKVOL)
  124. return -ENODEV;
  125. }
  126. return 0;
  127. }
  128. /*
  129. * create a vfsmount to be automounted
  130. */
  131. static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
  132. {
  133. struct fs_context *fc;
  134. struct vfsmount *mnt;
  135. int ret;
  136. BUG_ON(!d_inode(mntpt));
  137. fc = fs_context_for_submount(&afs_fs_type, mntpt);
  138. if (IS_ERR(fc))
  139. return ERR_CAST(fc);
  140. ret = afs_mntpt_set_params(fc, mntpt);
  141. if (!ret)
  142. mnt = fc_mount(fc);
  143. else
  144. mnt = ERR_PTR(ret);
  145. put_fs_context(fc);
  146. return mnt;
  147. }
  148. /*
  149. * handle an automount point
  150. */
  151. struct vfsmount *afs_d_automount(struct path *path)
  152. {
  153. struct vfsmount *newmnt;
  154. _enter("{%pd}", path->dentry);
  155. newmnt = afs_mntpt_do_automount(path->dentry);
  156. if (IS_ERR(newmnt))
  157. return newmnt;
  158. mntget(newmnt); /* prevent immediate expiration */
  159. mnt_set_expiry(newmnt, &afs_vfsmounts);
  160. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  161. afs_mntpt_expiry_timeout * HZ);
  162. _leave(" = %p", newmnt);
  163. return newmnt;
  164. }
  165. /*
  166. * handle mountpoint expiry timer going off
  167. */
  168. static void afs_mntpt_expiry_timed_out(struct work_struct *work)
  169. {
  170. _enter("");
  171. if (!list_empty(&afs_vfsmounts)) {
  172. mark_mounts_for_expiry(&afs_vfsmounts);
  173. queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
  174. afs_mntpt_expiry_timeout * HZ);
  175. }
  176. _leave("");
  177. }
  178. /*
  179. * kill the AFS mountpoint timer if it's still running
  180. */
  181. void afs_mntpt_kill_timer(void)
  182. {
  183. _enter("");
  184. ASSERT(list_empty(&afs_vfsmounts));
  185. cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
  186. }