dynroot.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 (dhowells@redhat.com)
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/dns_resolver.h>
  10. #include "internal.h"
  11. #define AFS_MIN_DYNROOT_CELL_INO 4 /* Allow for ., .., @cell, .@cell */
  12. #define AFS_MAX_DYNROOT_CELL_INO ((unsigned int)INT_MAX)
  13. static struct dentry *afs_lookup_atcell(struct inode *dir, struct dentry *dentry, ino_t ino);
  14. /*
  15. * iget5() comparator for inode created by autocell operations
  16. */
  17. static int afs_iget5_pseudo_test(struct inode *inode, void *opaque)
  18. {
  19. struct afs_fid *fid = opaque;
  20. return inode->i_ino == fid->vnode;
  21. }
  22. /*
  23. * iget5() inode initialiser
  24. */
  25. static int afs_iget5_pseudo_set(struct inode *inode, void *opaque)
  26. {
  27. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  28. struct afs_vnode *vnode = AFS_FS_I(inode);
  29. struct afs_fid *fid = opaque;
  30. vnode->volume = as->volume;
  31. vnode->fid = *fid;
  32. inode->i_ino = fid->vnode;
  33. inode->i_generation = fid->unique;
  34. return 0;
  35. }
  36. /*
  37. * Create an inode for an autocell dynamic automount dir.
  38. */
  39. static struct inode *afs_iget_pseudo_dir(struct super_block *sb, ino_t ino)
  40. {
  41. struct afs_vnode *vnode;
  42. struct inode *inode;
  43. struct afs_fid fid = { .vnode = ino, .unique = 1, };
  44. _enter("");
  45. inode = iget5_locked(sb, fid.vnode,
  46. afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
  47. if (!inode) {
  48. _leave(" = -ENOMEM");
  49. return ERR_PTR(-ENOMEM);
  50. }
  51. _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
  52. inode, inode->i_ino, fid.vid, fid.vnode, fid.unique);
  53. vnode = AFS_FS_I(inode);
  54. if (inode_state_read_once(inode) & I_NEW) {
  55. netfs_inode_init(&vnode->netfs, NULL, false);
  56. simple_inode_init_ts(inode);
  57. set_nlink(inode, 2);
  58. inode->i_size = 0;
  59. inode->i_mode = S_IFDIR | 0555;
  60. inode->i_op = &afs_autocell_inode_operations;
  61. inode->i_uid = GLOBAL_ROOT_UID;
  62. inode->i_gid = GLOBAL_ROOT_GID;
  63. inode->i_blocks = 0;
  64. inode->i_generation = 0;
  65. inode->i_flags |= S_AUTOMOUNT | S_NOATIME;
  66. set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
  67. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  68. unlock_new_inode(inode);
  69. }
  70. _leave(" = %p", inode);
  71. return inode;
  72. }
  73. /*
  74. * Try to automount the mountpoint with pseudo directory, if the autocell
  75. * option is set.
  76. */
  77. static struct dentry *afs_dynroot_lookup_cell(struct inode *dir, struct dentry *dentry,
  78. unsigned int flags)
  79. {
  80. struct afs_cell *cell = NULL;
  81. struct afs_net *net = afs_d2net(dentry);
  82. struct inode *inode = NULL;
  83. const char *name = dentry->d_name.name;
  84. size_t len = dentry->d_name.len;
  85. bool dotted = false;
  86. int ret = -ENOENT;
  87. /* Names prefixed with a dot are R/W mounts. */
  88. if (name[0] == '.') {
  89. name++;
  90. len--;
  91. dotted = true;
  92. }
  93. cell = afs_lookup_cell(net, name, len, NULL,
  94. AFS_LOOKUP_CELL_DYNROOT,
  95. afs_cell_trace_use_lookup_dynroot);
  96. if (IS_ERR(cell)) {
  97. ret = PTR_ERR(cell);
  98. goto out_no_cell;
  99. }
  100. inode = afs_iget_pseudo_dir(dir->i_sb, cell->dynroot_ino * 2 + dotted);
  101. if (IS_ERR(inode)) {
  102. ret = PTR_ERR(inode);
  103. goto out;
  104. }
  105. dentry->d_fsdata = cell;
  106. return d_splice_alias(inode, dentry);
  107. out:
  108. afs_unuse_cell(cell, afs_cell_trace_unuse_lookup_dynroot);
  109. out_no_cell:
  110. if (!inode)
  111. return d_splice_alias(inode, dentry);
  112. return ret == -ENOENT ? NULL : ERR_PTR(ret);
  113. }
  114. /*
  115. * Look up an entry in a dynroot directory.
  116. */
  117. static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
  118. unsigned int flags)
  119. {
  120. _enter("%pd", dentry);
  121. if (flags & LOOKUP_CREATE)
  122. return ERR_PTR(-EOPNOTSUPP);
  123. if (dentry->d_name.len >= AFSNAMEMAX) {
  124. _leave(" = -ENAMETOOLONG");
  125. return ERR_PTR(-ENAMETOOLONG);
  126. }
  127. if (dentry->d_name.len == 5 &&
  128. memcmp(dentry->d_name.name, "@cell", 5) == 0)
  129. return afs_lookup_atcell(dir, dentry, 2);
  130. if (dentry->d_name.len == 6 &&
  131. memcmp(dentry->d_name.name, ".@cell", 6) == 0)
  132. return afs_lookup_atcell(dir, dentry, 3);
  133. return afs_dynroot_lookup_cell(dir, dentry, flags);
  134. }
  135. const struct inode_operations afs_dynroot_inode_operations = {
  136. .lookup = afs_dynroot_lookup,
  137. };
  138. static void afs_dynroot_d_release(struct dentry *dentry)
  139. {
  140. struct afs_cell *cell = dentry->d_fsdata;
  141. afs_unuse_cell(cell, afs_cell_trace_unuse_dynroot_mntpt);
  142. }
  143. /*
  144. * Keep @cell symlink dentries around, but only keep cell autodirs when they're
  145. * being used.
  146. */
  147. static int afs_dynroot_delete_dentry(const struct dentry *dentry)
  148. {
  149. const struct qstr *name = &dentry->d_name;
  150. if (name->len == 5 && memcmp(name->name, "@cell", 5) == 0)
  151. return 0;
  152. if (name->len == 6 && memcmp(name->name, ".@cell", 6) == 0)
  153. return 0;
  154. return 1;
  155. }
  156. const struct dentry_operations afs_dynroot_dentry_operations = {
  157. .d_delete = afs_dynroot_delete_dentry,
  158. .d_release = afs_dynroot_d_release,
  159. .d_automount = afs_d_automount,
  160. };
  161. static void afs_atcell_delayed_put_cell(void *arg)
  162. {
  163. struct afs_cell *cell = arg;
  164. afs_put_cell(cell, afs_cell_trace_put_atcell);
  165. }
  166. /*
  167. * Read @cell or .@cell symlinks.
  168. */
  169. static const char *afs_atcell_get_link(struct dentry *dentry, struct inode *inode,
  170. struct delayed_call *done)
  171. {
  172. struct afs_vnode *vnode = AFS_FS_I(inode);
  173. struct afs_cell *cell;
  174. struct afs_net *net = afs_i2net(inode);
  175. const char *name;
  176. bool dotted = vnode->fid.vnode == 3;
  177. if (!rcu_access_pointer(net->ws_cell))
  178. return ERR_PTR(-ENOENT);
  179. if (!dentry) {
  180. /* We're in RCU-pathwalk. */
  181. cell = rcu_dereference(net->ws_cell);
  182. if (dotted)
  183. name = cell->name - 1;
  184. else
  185. name = cell->name;
  186. /* Shouldn't need to set a delayed call. */
  187. return name;
  188. }
  189. down_read(&net->cells_lock);
  190. cell = rcu_dereference_protected(net->ws_cell, lockdep_is_held(&net->cells_lock));
  191. if (dotted)
  192. name = cell->name - 1;
  193. else
  194. name = cell->name;
  195. afs_get_cell(cell, afs_cell_trace_get_atcell);
  196. set_delayed_call(done, afs_atcell_delayed_put_cell, cell);
  197. up_read(&net->cells_lock);
  198. return name;
  199. }
  200. static const struct inode_operations afs_atcell_inode_operations = {
  201. .get_link = afs_atcell_get_link,
  202. };
  203. /*
  204. * Create an inode for the @cell or .@cell symlinks.
  205. */
  206. static struct dentry *afs_lookup_atcell(struct inode *dir, struct dentry *dentry, ino_t ino)
  207. {
  208. struct afs_vnode *vnode;
  209. struct inode *inode;
  210. struct afs_fid fid = { .vnode = ino, .unique = 1, };
  211. inode = iget5_locked(dir->i_sb, fid.vnode,
  212. afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
  213. if (!inode)
  214. return ERR_PTR(-ENOMEM);
  215. vnode = AFS_FS_I(inode);
  216. if (inode_state_read_once(inode) & I_NEW) {
  217. netfs_inode_init(&vnode->netfs, NULL, false);
  218. simple_inode_init_ts(inode);
  219. set_nlink(inode, 1);
  220. inode->i_size = 0;
  221. inode->i_mode = S_IFLNK | 0555;
  222. inode->i_op = &afs_atcell_inode_operations;
  223. inode->i_uid = GLOBAL_ROOT_UID;
  224. inode->i_gid = GLOBAL_ROOT_GID;
  225. inode->i_blocks = 0;
  226. inode->i_generation = 0;
  227. inode->i_flags |= S_NOATIME;
  228. unlock_new_inode(inode);
  229. }
  230. return d_splice_alias(inode, dentry);
  231. }
  232. /*
  233. * Transcribe the cell database into readdir content under the RCU read lock.
  234. * Each cell produces two entries, one prefixed with a dot and one not.
  235. */
  236. static int afs_dynroot_readdir_cells(struct afs_net *net, struct dir_context *ctx)
  237. {
  238. const struct afs_cell *cell;
  239. loff_t newpos;
  240. _enter("%llu", ctx->pos);
  241. for (;;) {
  242. unsigned int ix = ctx->pos >> 1;
  243. cell = idr_get_next(&net->cells_dyn_ino, &ix);
  244. if (!cell)
  245. return 0;
  246. if (READ_ONCE(cell->state) == AFS_CELL_REMOVING ||
  247. READ_ONCE(cell->state) == AFS_CELL_DEAD) {
  248. ctx->pos += 2;
  249. ctx->pos &= ~1;
  250. continue;
  251. }
  252. newpos = ix << 1;
  253. if (newpos > ctx->pos)
  254. ctx->pos = newpos;
  255. _debug("pos %llu -> cell %u", ctx->pos, cell->dynroot_ino);
  256. if ((ctx->pos & 1) == 0) {
  257. if (!dir_emit(ctx, cell->name, cell->name_len,
  258. cell->dynroot_ino, DT_DIR))
  259. return 0;
  260. ctx->pos++;
  261. }
  262. if ((ctx->pos & 1) == 1) {
  263. if (!dir_emit(ctx, cell->name - 1, cell->name_len + 1,
  264. cell->dynroot_ino + 1, DT_DIR))
  265. return 0;
  266. ctx->pos++;
  267. }
  268. }
  269. return 0;
  270. }
  271. /*
  272. * Read the AFS dynamic root directory. This produces a list of cellnames,
  273. * dotted and undotted, along with @cell and .@cell links if configured.
  274. */
  275. static int afs_dynroot_readdir(struct file *file, struct dir_context *ctx)
  276. {
  277. struct afs_net *net = afs_d2net(file->f_path.dentry);
  278. int ret = 0;
  279. if (!dir_emit_dots(file, ctx))
  280. return 0;
  281. if (ctx->pos == 2) {
  282. if (rcu_access_pointer(net->ws_cell) &&
  283. !dir_emit(ctx, "@cell", 5, 2, DT_LNK))
  284. return 0;
  285. ctx->pos = 3;
  286. }
  287. if (ctx->pos == 3) {
  288. if (rcu_access_pointer(net->ws_cell) &&
  289. !dir_emit(ctx, ".@cell", 6, 3, DT_LNK))
  290. return 0;
  291. ctx->pos = 4;
  292. }
  293. if ((unsigned long long)ctx->pos <= AFS_MAX_DYNROOT_CELL_INO) {
  294. down_read(&net->cells_lock);
  295. ret = afs_dynroot_readdir_cells(net, ctx);
  296. up_read(&net->cells_lock);
  297. }
  298. return ret;
  299. }
  300. static const struct file_operations afs_dynroot_file_operations = {
  301. .llseek = generic_file_llseek,
  302. .read = generic_read_dir,
  303. .iterate_shared = afs_dynroot_readdir,
  304. .fsync = noop_fsync,
  305. };
  306. /*
  307. * Create an inode for a dynamic root directory.
  308. */
  309. struct inode *afs_dynroot_iget_root(struct super_block *sb)
  310. {
  311. struct afs_super_info *as = AFS_FS_S(sb);
  312. struct afs_vnode *vnode;
  313. struct inode *inode;
  314. struct afs_fid fid = { .vid = 0, .vnode = 1, .unique = 1,};
  315. if (as->volume)
  316. fid.vid = as->volume->vid;
  317. inode = iget5_locked(sb, fid.vnode,
  318. afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
  319. if (!inode)
  320. return ERR_PTR(-ENOMEM);
  321. vnode = AFS_FS_I(inode);
  322. /* there shouldn't be an existing inode */
  323. if (inode_state_read_once(inode) & I_NEW) {
  324. netfs_inode_init(&vnode->netfs, NULL, false);
  325. simple_inode_init_ts(inode);
  326. set_nlink(inode, 2);
  327. inode->i_size = 0;
  328. inode->i_mode = S_IFDIR | 0555;
  329. inode->i_op = &afs_dynroot_inode_operations;
  330. inode->i_fop = &afs_dynroot_file_operations;
  331. inode->i_uid = GLOBAL_ROOT_UID;
  332. inode->i_gid = GLOBAL_ROOT_GID;
  333. inode->i_blocks = 0;
  334. inode->i_generation = 0;
  335. inode->i_flags |= S_NOATIME;
  336. set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
  337. unlock_new_inode(inode);
  338. }
  339. _leave(" = %p", inode);
  340. return inode;
  341. }