dynroot.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS dynamic root handling
  3. *
  4. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/dns_resolver.h>
  10. #include "internal.h"
  11. static atomic_t afs_autocell_ino;
  12. /*
  13. * iget5() comparator for inode created by autocell operations
  14. *
  15. * These pseudo inodes don't match anything.
  16. */
  17. static int afs_iget5_pseudo_test(struct inode *inode, void *opaque)
  18. {
  19. return 0;
  20. }
  21. /*
  22. * iget5() inode initialiser
  23. */
  24. static int afs_iget5_pseudo_set(struct inode *inode, void *opaque)
  25. {
  26. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  27. struct afs_vnode *vnode = AFS_FS_I(inode);
  28. struct afs_fid *fid = opaque;
  29. vnode->volume = as->volume;
  30. vnode->fid = *fid;
  31. inode->i_ino = fid->vnode;
  32. inode->i_generation = fid->unique;
  33. return 0;
  34. }
  35. /*
  36. * Create an inode for a dynamic root directory or an autocell dynamic
  37. * automount dir.
  38. */
  39. struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
  40. {
  41. struct afs_super_info *as = AFS_FS_S(sb);
  42. struct afs_vnode *vnode;
  43. struct inode *inode;
  44. struct afs_fid fid = {};
  45. _enter("");
  46. if (as->volume)
  47. fid.vid = as->volume->vid;
  48. if (root) {
  49. fid.vnode = 1;
  50. fid.unique = 1;
  51. } else {
  52. fid.vnode = atomic_inc_return(&afs_autocell_ino);
  53. fid.unique = 0;
  54. }
  55. inode = iget5_locked(sb, fid.vnode,
  56. afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
  57. if (!inode) {
  58. _leave(" = -ENOMEM");
  59. return ERR_PTR(-ENOMEM);
  60. }
  61. _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
  62. inode, inode->i_ino, fid.vid, fid.vnode, fid.unique);
  63. vnode = AFS_FS_I(inode);
  64. /* there shouldn't be an existing inode */
  65. BUG_ON(!(inode->i_state & I_NEW));
  66. netfs_inode_init(&vnode->netfs, NULL);
  67. inode->i_size = 0;
  68. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  69. if (root) {
  70. inode->i_op = &afs_dynroot_inode_operations;
  71. inode->i_fop = &simple_dir_operations;
  72. } else {
  73. inode->i_op = &afs_autocell_inode_operations;
  74. }
  75. set_nlink(inode, 2);
  76. inode->i_uid = GLOBAL_ROOT_UID;
  77. inode->i_gid = GLOBAL_ROOT_GID;
  78. inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode);
  79. inode->i_blocks = 0;
  80. inode->i_generation = 0;
  81. set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
  82. if (!root) {
  83. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  84. inode->i_flags |= S_AUTOMOUNT;
  85. }
  86. inode->i_flags |= S_NOATIME;
  87. unlock_new_inode(inode);
  88. _leave(" = %p", inode);
  89. return inode;
  90. }
  91. /*
  92. * Probe to see if a cell may exist. This prevents positive dentries from
  93. * being created unnecessarily.
  94. */
  95. static int afs_probe_cell_name(struct dentry *dentry)
  96. {
  97. struct afs_cell *cell;
  98. struct afs_net *net = afs_d2net(dentry);
  99. const char *name = dentry->d_name.name;
  100. size_t len = dentry->d_name.len;
  101. int ret;
  102. /* Names prefixed with a dot are R/W mounts. */
  103. if (name[0] == '.') {
  104. if (len == 1)
  105. return -EINVAL;
  106. name++;
  107. len--;
  108. }
  109. cell = afs_find_cell(net, name, len, afs_cell_trace_use_probe);
  110. if (!IS_ERR(cell)) {
  111. afs_unuse_cell(net, cell, afs_cell_trace_unuse_probe);
  112. return 0;
  113. }
  114. ret = dns_query(net->net, "afsdb", name, len, "srv=1",
  115. NULL, NULL, false);
  116. if (ret == -ENODATA || ret == -ENOKEY)
  117. ret = -ENOENT;
  118. return ret;
  119. }
  120. /*
  121. * Try to auto mount the mountpoint with pseudo directory, if the autocell
  122. * operation is setted.
  123. */
  124. struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
  125. {
  126. struct afs_vnode *vnode = AFS_FS_I(dir);
  127. struct inode *inode;
  128. int ret = -ENOENT;
  129. _enter("%p{%pd}, {%llx:%llu}",
  130. dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
  131. if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
  132. goto out;
  133. ret = afs_probe_cell_name(dentry);
  134. if (ret < 0)
  135. goto out;
  136. inode = afs_iget_pseudo_dir(dir->i_sb, false);
  137. if (IS_ERR(inode)) {
  138. ret = PTR_ERR(inode);
  139. goto out;
  140. }
  141. _leave("= %p", inode);
  142. return inode;
  143. out:
  144. _leave("= %d", ret);
  145. return ret == -ENOENT ? NULL : ERR_PTR(ret);
  146. }
  147. /*
  148. * Look up @cell in a dynroot directory. This is a substitution for the
  149. * local cell name for the net namespace.
  150. */
  151. static struct dentry *afs_lookup_atcell(struct dentry *dentry)
  152. {
  153. struct afs_cell *cell;
  154. struct afs_net *net = afs_d2net(dentry);
  155. struct dentry *ret;
  156. char *name;
  157. int len;
  158. if (!net->ws_cell)
  159. return ERR_PTR(-ENOENT);
  160. ret = ERR_PTR(-ENOMEM);
  161. name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
  162. if (!name)
  163. goto out_p;
  164. down_read(&net->cells_lock);
  165. cell = net->ws_cell;
  166. if (cell) {
  167. len = cell->name_len;
  168. memcpy(name, cell->name, len + 1);
  169. }
  170. up_read(&net->cells_lock);
  171. ret = ERR_PTR(-ENOENT);
  172. if (!cell)
  173. goto out_n;
  174. ret = lookup_one_len(name, dentry->d_parent, len);
  175. /* We don't want to d_add() the @cell dentry here as we don't want to
  176. * the cached dentry to hide changes to the local cell name.
  177. */
  178. out_n:
  179. kfree(name);
  180. out_p:
  181. return ret;
  182. }
  183. /*
  184. * Look up an entry in a dynroot directory.
  185. */
  186. static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
  187. unsigned int flags)
  188. {
  189. _enter("%pd", dentry);
  190. ASSERTCMP(d_inode(dentry), ==, NULL);
  191. if (flags & LOOKUP_CREATE)
  192. return ERR_PTR(-EOPNOTSUPP);
  193. if (dentry->d_name.len >= AFSNAMEMAX) {
  194. _leave(" = -ENAMETOOLONG");
  195. return ERR_PTR(-ENAMETOOLONG);
  196. }
  197. if (dentry->d_name.len == 5 &&
  198. memcmp(dentry->d_name.name, "@cell", 5) == 0)
  199. return afs_lookup_atcell(dentry);
  200. return d_splice_alias(afs_try_auto_mntpt(dentry, dir), dentry);
  201. }
  202. const struct inode_operations afs_dynroot_inode_operations = {
  203. .lookup = afs_dynroot_lookup,
  204. };
  205. /*
  206. * Dirs in the dynamic root don't need revalidation.
  207. */
  208. static int afs_dynroot_d_revalidate(struct dentry *dentry, unsigned int flags)
  209. {
  210. return 1;
  211. }
  212. /*
  213. * Allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  214. * sleep)
  215. * - called from dput() when d_count is going to 0.
  216. * - return 1 to request dentry be unhashed, 0 otherwise
  217. */
  218. static int afs_dynroot_d_delete(const struct dentry *dentry)
  219. {
  220. return d_really_is_positive(dentry);
  221. }
  222. const struct dentry_operations afs_dynroot_dentry_operations = {
  223. .d_revalidate = afs_dynroot_d_revalidate,
  224. .d_delete = afs_dynroot_d_delete,
  225. .d_release = afs_d_release,
  226. .d_automount = afs_d_automount,
  227. };
  228. /*
  229. * Create a manually added cell mount directory.
  230. * - The caller must hold net->proc_cells_lock
  231. */
  232. int afs_dynroot_mkdir(struct afs_net *net, struct afs_cell *cell)
  233. {
  234. struct super_block *sb = net->dynroot_sb;
  235. struct dentry *root, *subdir;
  236. int ret;
  237. if (!sb || atomic_read(&sb->s_active) == 0)
  238. return 0;
  239. /* Let the ->lookup op do the creation */
  240. root = sb->s_root;
  241. inode_lock(root->d_inode);
  242. subdir = lookup_one_len(cell->name, root, cell->name_len);
  243. if (IS_ERR(subdir)) {
  244. ret = PTR_ERR(subdir);
  245. goto unlock;
  246. }
  247. /* Note that we're retaining an extra ref on the dentry */
  248. subdir->d_fsdata = (void *)1UL;
  249. ret = 0;
  250. unlock:
  251. inode_unlock(root->d_inode);
  252. return ret;
  253. }
  254. /*
  255. * Remove a manually added cell mount directory.
  256. * - The caller must hold net->proc_cells_lock
  257. */
  258. void afs_dynroot_rmdir(struct afs_net *net, struct afs_cell *cell)
  259. {
  260. struct super_block *sb = net->dynroot_sb;
  261. struct dentry *root, *subdir;
  262. if (!sb || atomic_read(&sb->s_active) == 0)
  263. return;
  264. root = sb->s_root;
  265. inode_lock(root->d_inode);
  266. /* Don't want to trigger a lookup call, which will re-add the cell */
  267. subdir = try_lookup_one_len(cell->name, root, cell->name_len);
  268. if (IS_ERR_OR_NULL(subdir)) {
  269. _debug("lookup %ld", PTR_ERR(subdir));
  270. goto no_dentry;
  271. }
  272. _debug("rmdir %pd %u", subdir, d_count(subdir));
  273. if (subdir->d_fsdata) {
  274. _debug("unpin %u", d_count(subdir));
  275. subdir->d_fsdata = NULL;
  276. dput(subdir);
  277. }
  278. dput(subdir);
  279. no_dentry:
  280. inode_unlock(root->d_inode);
  281. _leave("");
  282. }
  283. /*
  284. * Populate a newly created dynamic root with cell names.
  285. */
  286. int afs_dynroot_populate(struct super_block *sb)
  287. {
  288. struct afs_cell *cell;
  289. struct afs_net *net = afs_sb2net(sb);
  290. int ret;
  291. mutex_lock(&net->proc_cells_lock);
  292. net->dynroot_sb = sb;
  293. hlist_for_each_entry(cell, &net->proc_cells, proc_link) {
  294. ret = afs_dynroot_mkdir(net, cell);
  295. if (ret < 0)
  296. goto error;
  297. }
  298. ret = 0;
  299. out:
  300. mutex_unlock(&net->proc_cells_lock);
  301. return ret;
  302. error:
  303. net->dynroot_sb = NULL;
  304. goto out;
  305. }
  306. /*
  307. * When a dynamic root that's in the process of being destroyed, depopulate it
  308. * of pinned directories.
  309. */
  310. void afs_dynroot_depopulate(struct super_block *sb)
  311. {
  312. struct afs_net *net = afs_sb2net(sb);
  313. struct dentry *root = sb->s_root, *subdir, *tmp;
  314. /* Prevent more subdirs from being created */
  315. mutex_lock(&net->proc_cells_lock);
  316. if (net->dynroot_sb == sb)
  317. net->dynroot_sb = NULL;
  318. mutex_unlock(&net->proc_cells_lock);
  319. if (root) {
  320. inode_lock(root->d_inode);
  321. /* Remove all the pins for dirs created for manually added cells */
  322. list_for_each_entry_safe(subdir, tmp, &root->d_subdirs, d_child) {
  323. if (subdir->d_fsdata) {
  324. subdir->d_fsdata = NULL;
  325. dput(subdir);
  326. }
  327. }
  328. inode_unlock(root->d_inode);
  329. }
  330. }