xref: /linux/fs/bcachefs/fs-common.c (revision dd83757f6e686a2188997cb58b5975f744bb7786)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "acl.h"
5 #include "btree_update.h"
6 #include "dirent.h"
7 #include "fs-common.h"
8 #include "inode.h"
9 #include "subvolume.h"
10 #include "xattr.h"
11 
12 #include <linux/posix_acl.h>
13 
is_subdir_for_nlink(struct bch_inode_unpacked * inode)14 static inline int is_subdir_for_nlink(struct bch_inode_unpacked *inode)
15 {
16 	return S_ISDIR(inode->bi_mode) && !inode->bi_subvol;
17 }
18 
bch2_create_trans(struct btree_trans * trans,subvol_inum dir,struct bch_inode_unpacked * dir_u,struct bch_inode_unpacked * new_inode,const struct qstr * name,uid_t uid,gid_t gid,umode_t mode,dev_t rdev,struct posix_acl * default_acl,struct posix_acl * acl,subvol_inum snapshot_src,unsigned flags)19 int bch2_create_trans(struct btree_trans *trans,
20 		      subvol_inum dir,
21 		      struct bch_inode_unpacked *dir_u,
22 		      struct bch_inode_unpacked *new_inode,
23 		      const struct qstr *name,
24 		      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
25 		      struct posix_acl *default_acl,
26 		      struct posix_acl *acl,
27 		      subvol_inum snapshot_src,
28 		      unsigned flags)
29 {
30 	struct bch_fs *c = trans->c;
31 	struct btree_iter dir_iter = { NULL };
32 	struct btree_iter inode_iter = { NULL };
33 	subvol_inum new_inum = dir;
34 	u64 now = bch2_current_time(c);
35 	u64 cpu = raw_smp_processor_id();
36 	u64 dir_target;
37 	u32 snapshot;
38 	unsigned dir_type = mode_to_type(mode);
39 	int ret;
40 
41 	ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
42 	if (ret)
43 		goto err;
44 
45 	ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir,
46 			      BTREE_ITER_intent|BTREE_ITER_with_updates);
47 	if (ret)
48 		goto err;
49 
50 	if (!(flags & BCH_CREATE_SNAPSHOT)) {
51 		/* Normal create path - allocate a new inode: */
52 		bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
53 
54 		if (flags & BCH_CREATE_TMPFILE)
55 			new_inode->bi_flags |= BCH_INODE_unlinked;
56 
57 		ret = bch2_inode_create(trans, &inode_iter, new_inode, snapshot, cpu);
58 		if (ret)
59 			goto err;
60 
61 		snapshot_src = (subvol_inum) { 0 };
62 	} else {
63 		/*
64 		 * Creating a snapshot - we're not allocating a new inode, but
65 		 * we do have to lookup the root inode of the subvolume we're
66 		 * snapshotting and update it (in the new snapshot):
67 		 */
68 
69 		if (!snapshot_src.inum) {
70 			/* Inode wasn't specified, just snapshot: */
71 			struct bch_subvolume s;
72 			ret = bch2_subvolume_get(trans, snapshot_src.subvol, true, &s);
73 			if (ret)
74 				goto err;
75 
76 			snapshot_src.inum = le64_to_cpu(s.inode);
77 		}
78 
79 		ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src,
80 				      BTREE_ITER_intent);
81 		if (ret)
82 			goto err;
83 
84 		if (new_inode->bi_subvol != snapshot_src.subvol) {
85 			/* Not a subvolume root: */
86 			ret = -EINVAL;
87 			goto err;
88 		}
89 
90 		/*
91 		 * If we're not root, we have to own the subvolume being
92 		 * snapshotted:
93 		 */
94 		if (uid && new_inode->bi_uid != uid) {
95 			ret = -EPERM;
96 			goto err;
97 		}
98 
99 		flags |= BCH_CREATE_SUBVOL;
100 	}
101 
102 	new_inum.inum	= new_inode->bi_inum;
103 	dir_target	= new_inode->bi_inum;
104 
105 	if (flags & BCH_CREATE_SUBVOL) {
106 		u32 new_subvol, dir_snapshot;
107 
108 		ret = bch2_subvolume_create(trans, new_inode->bi_inum,
109 					    dir.subvol,
110 					    snapshot_src.subvol,
111 					    &new_subvol, &snapshot,
112 					    (flags & BCH_CREATE_SNAPSHOT_RO) != 0);
113 		if (ret)
114 			goto err;
115 
116 		new_inode->bi_parent_subvol	= dir.subvol;
117 		new_inode->bi_subvol		= new_subvol;
118 		new_inum.subvol			= new_subvol;
119 		dir_target			= new_subvol;
120 		dir_type			= DT_SUBVOL;
121 
122 		ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot);
123 		if (ret)
124 			goto err;
125 
126 		bch2_btree_iter_set_snapshot(&dir_iter, dir_snapshot);
127 		ret = bch2_btree_iter_traverse(&dir_iter);
128 		if (ret)
129 			goto err;
130 	}
131 
132 	if (!(flags & BCH_CREATE_SNAPSHOT)) {
133 		if (default_acl) {
134 			ret = bch2_set_acl_trans(trans, new_inum, new_inode,
135 						 default_acl, ACL_TYPE_DEFAULT);
136 			if (ret)
137 				goto err;
138 		}
139 
140 		if (acl) {
141 			ret = bch2_set_acl_trans(trans, new_inum, new_inode,
142 						 acl, ACL_TYPE_ACCESS);
143 			if (ret)
144 				goto err;
145 		}
146 	}
147 
148 	if (!(flags & BCH_CREATE_TMPFILE)) {
149 		struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
150 		u64 dir_offset;
151 
152 		if (is_subdir_for_nlink(new_inode))
153 			dir_u->bi_nlink++;
154 		dir_u->bi_mtime = dir_u->bi_ctime = now;
155 
156 		ret = bch2_inode_write(trans, &dir_iter, dir_u);
157 		if (ret)
158 			goto err;
159 
160 		ret = bch2_dirent_create(trans, dir, &dir_hash,
161 					 dir_type,
162 					 name,
163 					 dir_target,
164 					 &dir_offset,
165 					 STR_HASH_must_create|BTREE_ITER_with_updates);
166 		if (ret)
167 			goto err;
168 
169 		new_inode->bi_dir		= dir_u->bi_inum;
170 		new_inode->bi_dir_offset	= dir_offset;
171 	}
172 
173 	if (S_ISDIR(mode) &&
174 	    !new_inode->bi_subvol)
175 		new_inode->bi_depth = dir_u->bi_depth + 1;
176 
177 	inode_iter.flags &= ~BTREE_ITER_all_snapshots;
178 	bch2_btree_iter_set_snapshot(&inode_iter, snapshot);
179 
180 	ret   = bch2_btree_iter_traverse(&inode_iter) ?:
181 		bch2_inode_write(trans, &inode_iter, new_inode);
182 err:
183 	bch2_trans_iter_exit(trans, &inode_iter);
184 	bch2_trans_iter_exit(trans, &dir_iter);
185 	return ret;
186 }
187 
bch2_link_trans(struct btree_trans * trans,subvol_inum dir,struct bch_inode_unpacked * dir_u,subvol_inum inum,struct bch_inode_unpacked * inode_u,const struct qstr * name)188 int bch2_link_trans(struct btree_trans *trans,
189 		    subvol_inum dir,  struct bch_inode_unpacked *dir_u,
190 		    subvol_inum inum, struct bch_inode_unpacked *inode_u,
191 		    const struct qstr *name)
192 {
193 	struct bch_fs *c = trans->c;
194 	struct btree_iter dir_iter = { NULL };
195 	struct btree_iter inode_iter = { NULL };
196 	struct bch_hash_info dir_hash;
197 	u64 now = bch2_current_time(c);
198 	u64 dir_offset = 0;
199 	int ret;
200 
201 	if (dir.subvol != inum.subvol)
202 		return -EXDEV;
203 
204 	ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
205 	if (ret)
206 		return ret;
207 
208 	inode_u->bi_ctime = now;
209 	ret = bch2_inode_nlink_inc(inode_u);
210 	if (ret)
211 		goto err;
212 
213 	ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
214 	if (ret)
215 		goto err;
216 
217 	if (bch2_reinherit_attrs(inode_u, dir_u)) {
218 		ret = -EXDEV;
219 		goto err;
220 	}
221 
222 	dir_u->bi_mtime = dir_u->bi_ctime = now;
223 
224 	dir_hash = bch2_hash_info_init(c, dir_u);
225 
226 	ret = bch2_dirent_create(trans, dir, &dir_hash,
227 				 mode_to_type(inode_u->bi_mode),
228 				 name, inum.inum, &dir_offset,
229 				 STR_HASH_must_create);
230 	if (ret)
231 		goto err;
232 
233 	inode_u->bi_dir		= dir.inum;
234 	inode_u->bi_dir_offset	= dir_offset;
235 
236 	ret =   bch2_inode_write(trans, &dir_iter, dir_u) ?:
237 		bch2_inode_write(trans, &inode_iter, inode_u);
238 err:
239 	bch2_trans_iter_exit(trans, &dir_iter);
240 	bch2_trans_iter_exit(trans, &inode_iter);
241 	return ret;
242 }
243 
bch2_unlink_trans(struct btree_trans * trans,subvol_inum dir,struct bch_inode_unpacked * dir_u,struct bch_inode_unpacked * inode_u,const struct qstr * name,bool deleting_subvol)244 int bch2_unlink_trans(struct btree_trans *trans,
245 		      subvol_inum dir,
246 		      struct bch_inode_unpacked *dir_u,
247 		      struct bch_inode_unpacked *inode_u,
248 		      const struct qstr *name,
249 		      bool deleting_subvol)
250 {
251 	struct bch_fs *c = trans->c;
252 	struct btree_iter dir_iter = { NULL };
253 	struct btree_iter dirent_iter = { NULL };
254 	struct btree_iter inode_iter = { NULL };
255 	struct bch_hash_info dir_hash;
256 	subvol_inum inum;
257 	u64 now = bch2_current_time(c);
258 	struct bkey_s_c k;
259 	int ret;
260 
261 	ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
262 	if (ret)
263 		goto err;
264 
265 	dir_hash = bch2_hash_info_init(c, dir_u);
266 
267 	ret = bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash,
268 				       name, &inum, BTREE_ITER_intent);
269 	if (ret)
270 		goto err;
271 
272 	ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum,
273 			      BTREE_ITER_intent);
274 	if (ret)
275 		goto err;
276 
277 	if (!deleting_subvol && S_ISDIR(inode_u->bi_mode)) {
278 		ret = bch2_empty_dir_trans(trans, inum);
279 		if (ret)
280 			goto err;
281 	}
282 
283 	if (deleting_subvol && !inode_u->bi_subvol) {
284 		ret = -BCH_ERR_ENOENT_not_subvol;
285 		goto err;
286 	}
287 
288 	if (inode_u->bi_subvol) {
289 		/* Recursive subvolume destroy not allowed (yet?) */
290 		ret = bch2_subvol_has_children(trans, inode_u->bi_subvol);
291 		if (ret)
292 			goto err;
293 	}
294 
295 	if (deleting_subvol || inode_u->bi_subvol) {
296 		ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol);
297 		if (ret)
298 			goto err;
299 
300 		k = bch2_btree_iter_peek_slot(&dirent_iter);
301 		ret = bkey_err(k);
302 		if (ret)
303 			goto err;
304 
305 		/*
306 		 * If we're deleting a subvolume, we need to really delete the
307 		 * dirent, not just emit a whiteout in the current snapshot:
308 		 */
309 		bch2_btree_iter_set_snapshot(&dirent_iter, k.k->p.snapshot);
310 		ret = bch2_btree_iter_traverse(&dirent_iter);
311 		if (ret)
312 			goto err;
313 	} else {
314 		bch2_inode_nlink_dec(trans, inode_u);
315 	}
316 
317 	if (inode_u->bi_dir		== dirent_iter.pos.inode &&
318 	    inode_u->bi_dir_offset	== dirent_iter.pos.offset) {
319 		inode_u->bi_dir		= 0;
320 		inode_u->bi_dir_offset	= 0;
321 	}
322 
323 	dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
324 	dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
325 
326 	ret =   bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
327 				    &dir_hash, &dirent_iter,
328 				    BTREE_UPDATE_internal_snapshot_node) ?:
329 		bch2_inode_write(trans, &dir_iter, dir_u) ?:
330 		bch2_inode_write(trans, &inode_iter, inode_u);
331 err:
332 	bch2_trans_iter_exit(trans, &inode_iter);
333 	bch2_trans_iter_exit(trans, &dirent_iter);
334 	bch2_trans_iter_exit(trans, &dir_iter);
335 	return ret;
336 }
337 
bch2_reinherit_attrs(struct bch_inode_unpacked * dst_u,struct bch_inode_unpacked * src_u)338 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
339 			  struct bch_inode_unpacked *src_u)
340 {
341 	u64 src, dst;
342 	unsigned id;
343 	bool ret = false;
344 
345 	for (id = 0; id < Inode_opt_nr; id++) {
346 		/* Skip attributes that were explicitly set on this inode */
347 		if (dst_u->bi_fields_set & (1 << id))
348 			continue;
349 
350 		src = bch2_inode_opt_get(src_u, id);
351 		dst = bch2_inode_opt_get(dst_u, id);
352 
353 		if (src == dst)
354 			continue;
355 
356 		bch2_inode_opt_set(dst_u, id, src);
357 		ret = true;
358 	}
359 
360 	return ret;
361 }
362 
subvol_update_parent(struct btree_trans * trans,u32 subvol,u32 new_parent)363 static int subvol_update_parent(struct btree_trans *trans, u32 subvol, u32 new_parent)
364 {
365 	struct btree_iter iter;
366 	struct bkey_i_subvolume *s =
367 		bch2_bkey_get_mut_typed(trans, &iter,
368 			BTREE_ID_subvolumes, POS(0, subvol),
369 			BTREE_ITER_cached, subvolume);
370 	int ret = PTR_ERR_OR_ZERO(s);
371 	if (ret)
372 		return ret;
373 
374 	s->v.fs_path_parent = cpu_to_le32(new_parent);
375 	bch2_trans_iter_exit(trans, &iter);
376 	return 0;
377 }
378 
bch2_rename_trans(struct btree_trans * trans,subvol_inum src_dir,struct bch_inode_unpacked * src_dir_u,subvol_inum dst_dir,struct bch_inode_unpacked * dst_dir_u,struct bch_inode_unpacked * src_inode_u,struct bch_inode_unpacked * dst_inode_u,const struct qstr * src_name,const struct qstr * dst_name,enum bch_rename_mode mode)379 int bch2_rename_trans(struct btree_trans *trans,
380 		      subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u,
381 		      subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u,
382 		      struct bch_inode_unpacked *src_inode_u,
383 		      struct bch_inode_unpacked *dst_inode_u,
384 		      const struct qstr *src_name,
385 		      const struct qstr *dst_name,
386 		      enum bch_rename_mode mode)
387 {
388 	struct bch_fs *c = trans->c;
389 	struct btree_iter src_dir_iter = { NULL };
390 	struct btree_iter dst_dir_iter = { NULL };
391 	struct btree_iter src_inode_iter = { NULL };
392 	struct btree_iter dst_inode_iter = { NULL };
393 	struct bch_hash_info src_hash, dst_hash;
394 	subvol_inum src_inum, dst_inum;
395 	u64 src_offset, dst_offset;
396 	u64 now = bch2_current_time(c);
397 	int ret;
398 
399 	ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir,
400 			      BTREE_ITER_intent);
401 	if (ret)
402 		goto err;
403 
404 	src_hash = bch2_hash_info_init(c, src_dir_u);
405 
406 	if (dst_dir.inum	!= src_dir.inum ||
407 	    dst_dir.subvol	!= src_dir.subvol) {
408 		ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir,
409 				      BTREE_ITER_intent);
410 		if (ret)
411 			goto err;
412 
413 		dst_hash = bch2_hash_info_init(c, dst_dir_u);
414 	} else {
415 		dst_dir_u = src_dir_u;
416 		dst_hash = src_hash;
417 	}
418 
419 	ret = bch2_dirent_rename(trans,
420 				 src_dir, &src_hash,
421 				 dst_dir, &dst_hash,
422 				 src_name, &src_inum, &src_offset,
423 				 dst_name, &dst_inum, &dst_offset,
424 				 mode);
425 	if (ret)
426 		goto err;
427 
428 	ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum,
429 			      BTREE_ITER_intent);
430 	if (ret)
431 		goto err;
432 
433 	if (dst_inum.inum) {
434 		ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum,
435 				      BTREE_ITER_intent);
436 		if (ret)
437 			goto err;
438 	}
439 
440 	if (src_inode_u->bi_subvol &&
441 	    dst_dir.subvol != src_inode_u->bi_parent_subvol) {
442 		ret = subvol_update_parent(trans, src_inode_u->bi_subvol, dst_dir.subvol);
443 		if (ret)
444 			goto err;
445 	}
446 
447 	if (mode == BCH_RENAME_EXCHANGE &&
448 	    dst_inode_u->bi_subvol &&
449 	    src_dir.subvol != dst_inode_u->bi_parent_subvol) {
450 		ret = subvol_update_parent(trans, dst_inode_u->bi_subvol, src_dir.subvol);
451 		if (ret)
452 			goto err;
453 	}
454 
455 	/* Can't move across subvolumes, unless it's a subvolume root: */
456 	if (src_dir.subvol != dst_dir.subvol &&
457 	    (!src_inode_u->bi_subvol ||
458 	     (dst_inum.inum && !dst_inode_u->bi_subvol))) {
459 		ret = -EXDEV;
460 		goto err;
461 	}
462 
463 	if (src_inode_u->bi_parent_subvol)
464 		src_inode_u->bi_parent_subvol = dst_dir.subvol;
465 
466 	if ((mode == BCH_RENAME_EXCHANGE) &&
467 	    dst_inode_u->bi_parent_subvol)
468 		dst_inode_u->bi_parent_subvol = src_dir.subvol;
469 
470 	src_inode_u->bi_dir		= dst_dir_u->bi_inum;
471 	src_inode_u->bi_dir_offset	= dst_offset;
472 
473 	if (mode == BCH_RENAME_EXCHANGE) {
474 		dst_inode_u->bi_dir		= src_dir_u->bi_inum;
475 		dst_inode_u->bi_dir_offset	= src_offset;
476 	}
477 
478 	if (mode == BCH_RENAME_OVERWRITE &&
479 	    dst_inode_u->bi_dir		== dst_dir_u->bi_inum &&
480 	    dst_inode_u->bi_dir_offset	== src_offset) {
481 		dst_inode_u->bi_dir		= 0;
482 		dst_inode_u->bi_dir_offset	= 0;
483 	}
484 
485 	if (mode == BCH_RENAME_OVERWRITE) {
486 		if (S_ISDIR(src_inode_u->bi_mode) !=
487 		    S_ISDIR(dst_inode_u->bi_mode)) {
488 			ret = -ENOTDIR;
489 			goto err;
490 		}
491 
492 		if (S_ISDIR(dst_inode_u->bi_mode)) {
493 			ret = bch2_empty_dir_trans(trans, dst_inum);
494 			if (ret)
495 				goto err;
496 		}
497 	}
498 
499 	if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
500 	    S_ISDIR(src_inode_u->bi_mode)) {
501 		ret = -EXDEV;
502 		goto err;
503 	}
504 
505 	if (mode == BCH_RENAME_EXCHANGE &&
506 	    bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
507 	    S_ISDIR(dst_inode_u->bi_mode)) {
508 		ret = -EXDEV;
509 		goto err;
510 	}
511 
512 	if (is_subdir_for_nlink(src_inode_u)) {
513 		src_dir_u->bi_nlink--;
514 		dst_dir_u->bi_nlink++;
515 	}
516 
517 	if (S_ISDIR(src_inode_u->bi_mode) &&
518 	    !src_inode_u->bi_subvol)
519 		src_inode_u->bi_depth = dst_dir_u->bi_depth + 1;
520 
521 	if (mode == BCH_RENAME_EXCHANGE &&
522 	    S_ISDIR(dst_inode_u->bi_mode) &&
523 	    !dst_inode_u->bi_subvol)
524 		dst_inode_u->bi_depth = src_dir_u->bi_depth + 1;
525 
526 	if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) {
527 		dst_dir_u->bi_nlink--;
528 		src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
529 	}
530 
531 	if (mode == BCH_RENAME_OVERWRITE)
532 		bch2_inode_nlink_dec(trans, dst_inode_u);
533 
534 	src_dir_u->bi_mtime		= now;
535 	src_dir_u->bi_ctime		= now;
536 
537 	if (src_dir.inum != dst_dir.inum) {
538 		dst_dir_u->bi_mtime	= now;
539 		dst_dir_u->bi_ctime	= now;
540 	}
541 
542 	src_inode_u->bi_ctime		= now;
543 
544 	if (dst_inum.inum)
545 		dst_inode_u->bi_ctime	= now;
546 
547 	ret =   bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?:
548 		(src_dir.inum != dst_dir.inum
549 		 ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u)
550 		 : 0) ?:
551 		bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?:
552 		(dst_inum.inum
553 		 ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u)
554 		 : 0);
555 err:
556 	bch2_trans_iter_exit(trans, &dst_inode_iter);
557 	bch2_trans_iter_exit(trans, &src_inode_iter);
558 	bch2_trans_iter_exit(trans, &dst_dir_iter);
559 	bch2_trans_iter_exit(trans, &src_dir_iter);
560 	return ret;
561 }
562 
prt_bytes_reversed(struct printbuf * out,const void * b,unsigned n)563 static inline void prt_bytes_reversed(struct printbuf *out, const void *b, unsigned n)
564 {
565 	bch2_printbuf_make_room(out, n);
566 
567 	unsigned can_print = min(n, printbuf_remaining(out));
568 
569 	b += n;
570 
571 	for (unsigned i = 0; i < can_print; i++)
572 		out->buf[out->pos++] = *((char *) --b);
573 
574 	printbuf_nul_terminate(out);
575 }
576 
prt_str_reversed(struct printbuf * out,const char * s)577 static inline void prt_str_reversed(struct printbuf *out, const char *s)
578 {
579 	prt_bytes_reversed(out, s, strlen(s));
580 }
581 
reverse_bytes(void * b,size_t n)582 static inline void reverse_bytes(void *b, size_t n)
583 {
584 	char *e = b + n, *s = b;
585 
586 	while (s < e) {
587 		--e;
588 		swap(*s, *e);
589 		s++;
590 	}
591 }
592 
593 /* XXX: we don't yet attempt to print paths when we don't know the subvol */
bch2_inum_to_path(struct btree_trans * trans,subvol_inum inum,struct printbuf * path)594 int bch2_inum_to_path(struct btree_trans *trans, subvol_inum inum, struct printbuf *path)
595 {
596 	unsigned orig_pos = path->pos;
597 	int ret = 0;
598 
599 	while (!(inum.subvol == BCACHEFS_ROOT_SUBVOL &&
600 		 inum.inum   == BCACHEFS_ROOT_INO)) {
601 		struct bch_inode_unpacked inode;
602 		ret = bch2_inode_find_by_inum_trans(trans, inum, &inode);
603 		if (ret)
604 			goto disconnected;
605 
606 		if (!inode.bi_dir && !inode.bi_dir_offset) {
607 			ret = -BCH_ERR_ENOENT_inode_no_backpointer;
608 			goto disconnected;
609 		}
610 
611 		inum.subvol	= inode.bi_parent_subvol ?: inum.subvol;
612 		inum.inum	= inode.bi_dir;
613 
614 		u32 snapshot;
615 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
616 		if (ret)
617 			goto disconnected;
618 
619 		struct btree_iter d_iter;
620 		struct bkey_s_c_dirent d = bch2_bkey_get_iter_typed(trans, &d_iter,
621 				BTREE_ID_dirents, SPOS(inode.bi_dir, inode.bi_dir_offset, snapshot),
622 				0, dirent);
623 		ret = bkey_err(d.s_c);
624 		if (ret)
625 			goto disconnected;
626 
627 		struct qstr dirent_name = bch2_dirent_get_name(d);
628 		prt_bytes_reversed(path, dirent_name.name, dirent_name.len);
629 
630 		prt_char(path, '/');
631 
632 		bch2_trans_iter_exit(trans, &d_iter);
633 	}
634 
635 	if (orig_pos == path->pos)
636 		prt_char(path, '/');
637 out:
638 	ret = path->allocation_failure ? -ENOMEM : 0;
639 	if (ret)
640 		goto err;
641 
642 	reverse_bytes(path->buf + orig_pos, path->pos - orig_pos);
643 	return 0;
644 err:
645 	return ret;
646 disconnected:
647 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
648 		goto err;
649 
650 	prt_str_reversed(path, "(disconnected)");
651 	goto out;
652 }
653