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 "inode.h"
8 #include "namei.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 = {};
32 	struct btree_iter inode_iter = {};
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(trans, &dir_iter, dir_snapshot);
127 		ret = bch2_btree_iter_traverse(trans, &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_dirent_create(trans, dir, &dir_hash,
157 					   dir_type,
158 					   name,
159 					   dir_target,
160 					   &dir_offset,
161 					   STR_HASH_must_create|BTREE_ITER_with_updates) ?:
162 			bch2_inode_write(trans, &dir_iter, dir_u);
163 		if (ret)
164 			goto err;
165 
166 		new_inode->bi_dir		= dir_u->bi_inum;
167 		new_inode->bi_dir_offset	= dir_offset;
168 	}
169 
170 	if (S_ISDIR(mode) &&
171 	    !new_inode->bi_subvol)
172 		new_inode->bi_depth = dir_u->bi_depth + 1;
173 
174 	inode_iter.flags &= ~BTREE_ITER_all_snapshots;
175 	bch2_btree_iter_set_snapshot(trans, &inode_iter, snapshot);
176 
177 	ret   = bch2_btree_iter_traverse(trans, &inode_iter) ?:
178 		bch2_inode_write(trans, &inode_iter, new_inode);
179 err:
180 	bch2_trans_iter_exit(trans, &inode_iter);
181 	bch2_trans_iter_exit(trans, &dir_iter);
182 	return ret;
183 }
184 
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)185 int bch2_link_trans(struct btree_trans *trans,
186 		    subvol_inum dir,  struct bch_inode_unpacked *dir_u,
187 		    subvol_inum inum, struct bch_inode_unpacked *inode_u,
188 		    const struct qstr *name)
189 {
190 	struct bch_fs *c = trans->c;
191 	struct btree_iter dir_iter = {};
192 	struct btree_iter inode_iter = {};
193 	struct bch_hash_info dir_hash;
194 	u64 now = bch2_current_time(c);
195 	u64 dir_offset = 0;
196 	int ret;
197 
198 	if (dir.subvol != inum.subvol)
199 		return -EXDEV;
200 
201 	ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
202 	if (ret)
203 		return ret;
204 
205 	inode_u->bi_ctime = now;
206 	ret = bch2_inode_nlink_inc(inode_u);
207 	if (ret)
208 		goto err;
209 
210 	ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
211 	if (ret)
212 		goto err;
213 
214 	if (bch2_reinherit_attrs(inode_u, dir_u)) {
215 		ret = -EXDEV;
216 		goto err;
217 	}
218 
219 	dir_u->bi_mtime = dir_u->bi_ctime = now;
220 
221 	dir_hash = bch2_hash_info_init(c, dir_u);
222 
223 	ret = bch2_dirent_create(trans, dir, &dir_hash,
224 				 mode_to_type(inode_u->bi_mode),
225 				 name, inum.inum,
226 				 &dir_offset,
227 				 STR_HASH_must_create);
228 	if (ret)
229 		goto err;
230 
231 	inode_u->bi_dir		= dir.inum;
232 	inode_u->bi_dir_offset	= dir_offset;
233 
234 	ret =   bch2_inode_write(trans, &dir_iter, dir_u) ?:
235 		bch2_inode_write(trans, &inode_iter, inode_u);
236 err:
237 	bch2_trans_iter_exit(trans, &dir_iter);
238 	bch2_trans_iter_exit(trans, &inode_iter);
239 	return ret;
240 }
241 
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)242 int bch2_unlink_trans(struct btree_trans *trans,
243 		      subvol_inum dir,
244 		      struct bch_inode_unpacked *dir_u,
245 		      struct bch_inode_unpacked *inode_u,
246 		      const struct qstr *name,
247 		      bool deleting_subvol)
248 {
249 	struct bch_fs *c = trans->c;
250 	struct btree_iter dir_iter = {};
251 	struct btree_iter dirent_iter = {};
252 	struct btree_iter inode_iter = {};
253 	struct bch_hash_info dir_hash;
254 	subvol_inum inum;
255 	u64 now = bch2_current_time(c);
256 	struct bkey_s_c k;
257 	int ret;
258 
259 	ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
260 	if (ret)
261 		goto err;
262 
263 	dir_hash = bch2_hash_info_init(c, dir_u);
264 
265 	ret = bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash,
266 				       name, &inum, BTREE_ITER_intent);
267 	if (ret)
268 		goto err;
269 
270 	ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum,
271 			      BTREE_ITER_intent);
272 	if (ret)
273 		goto err;
274 
275 	if (!deleting_subvol && S_ISDIR(inode_u->bi_mode)) {
276 		ret = bch2_empty_dir_trans(trans, inum);
277 		if (ret)
278 			goto err;
279 	}
280 
281 	if (deleting_subvol && !inode_u->bi_subvol) {
282 		ret = -BCH_ERR_ENOENT_not_subvol;
283 		goto err;
284 	}
285 
286 	if (inode_u->bi_subvol) {
287 		/* Recursive subvolume destroy not allowed (yet?) */
288 		ret = bch2_subvol_has_children(trans, inode_u->bi_subvol);
289 		if (ret)
290 			goto err;
291 	}
292 
293 	if (deleting_subvol || inode_u->bi_subvol) {
294 		ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol);
295 		if (ret)
296 			goto err;
297 
298 		k = bch2_btree_iter_peek_slot(trans, &dirent_iter);
299 		ret = bkey_err(k);
300 		if (ret)
301 			goto err;
302 
303 		/*
304 		 * If we're deleting a subvolume, we need to really delete the
305 		 * dirent, not just emit a whiteout in the current snapshot:
306 		 */
307 		bch2_btree_iter_set_snapshot(trans, &dirent_iter, k.k->p.snapshot);
308 		ret = bch2_btree_iter_traverse(trans, &dirent_iter);
309 		if (ret)
310 			goto err;
311 	} else {
312 		bch2_inode_nlink_dec(trans, inode_u);
313 	}
314 
315 	if (inode_u->bi_dir		== dirent_iter.pos.inode &&
316 	    inode_u->bi_dir_offset	== dirent_iter.pos.offset) {
317 		inode_u->bi_dir		= 0;
318 		inode_u->bi_dir_offset	= 0;
319 	}
320 
321 	dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
322 	dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
323 
324 	ret =   bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
325 				    &dir_hash, &dirent_iter,
326 				    BTREE_UPDATE_internal_snapshot_node) ?:
327 		bch2_inode_write(trans, &dir_iter, dir_u) ?:
328 		bch2_inode_write(trans, &inode_iter, inode_u);
329 err:
330 	bch2_trans_iter_exit(trans, &inode_iter);
331 	bch2_trans_iter_exit(trans, &dirent_iter);
332 	bch2_trans_iter_exit(trans, &dir_iter);
333 	return ret;
334 }
335 
bch2_reinherit_attrs(struct bch_inode_unpacked * dst_u,struct bch_inode_unpacked * src_u)336 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
337 			  struct bch_inode_unpacked *src_u)
338 {
339 	u64 src, dst;
340 	unsigned id;
341 	bool ret = false;
342 
343 	for (id = 0; id < Inode_opt_nr; id++) {
344 		if (!S_ISDIR(dst_u->bi_mode) && id == Inode_opt_casefold)
345 			continue;
346 
347 		/* Skip attributes that were explicitly set on this inode */
348 		if (dst_u->bi_fields_set & (1 << id))
349 			continue;
350 
351 		src = bch2_inode_opt_get(src_u, id);
352 		dst = bch2_inode_opt_get(dst_u, id);
353 
354 		if (src == dst)
355 			continue;
356 
357 		bch2_inode_opt_set(dst_u, id, src);
358 		ret = true;
359 	}
360 
361 	return ret;
362 }
363 
subvol_update_parent(struct btree_trans * trans,u32 subvol,u32 new_parent)364 static int subvol_update_parent(struct btree_trans *trans, u32 subvol, u32 new_parent)
365 {
366 	struct btree_iter iter;
367 	struct bkey_i_subvolume *s =
368 		bch2_bkey_get_mut_typed(trans, &iter,
369 			BTREE_ID_subvolumes, POS(0, subvol),
370 			BTREE_ITER_cached, subvolume);
371 	int ret = PTR_ERR_OR_ZERO(s);
372 	if (ret)
373 		return ret;
374 
375 	s->v.fs_path_parent = cpu_to_le32(new_parent);
376 	bch2_trans_iter_exit(trans, &iter);
377 	return 0;
378 }
379 
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)380 int bch2_rename_trans(struct btree_trans *trans,
381 		      subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u,
382 		      subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u,
383 		      struct bch_inode_unpacked *src_inode_u,
384 		      struct bch_inode_unpacked *dst_inode_u,
385 		      const struct qstr *src_name,
386 		      const struct qstr *dst_name,
387 		      enum bch_rename_mode mode)
388 {
389 	struct bch_fs *c = trans->c;
390 	struct btree_iter src_dir_iter = {};
391 	struct btree_iter dst_dir_iter = {};
392 	struct btree_iter src_inode_iter = {};
393 	struct btree_iter dst_inode_iter = {};
394 	struct bch_hash_info src_hash, dst_hash;
395 	subvol_inum src_inum, dst_inum;
396 	u64 src_offset, dst_offset;
397 	u64 now = bch2_current_time(c);
398 	int ret;
399 
400 	ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir,
401 			      BTREE_ITER_intent);
402 	if (ret)
403 		goto err;
404 
405 	src_hash = bch2_hash_info_init(c, src_dir_u);
406 
407 	if (dst_dir.inum	!= src_dir.inum ||
408 	    dst_dir.subvol	!= src_dir.subvol) {
409 		ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir,
410 				      BTREE_ITER_intent);
411 		if (ret)
412 			goto err;
413 
414 		dst_hash = bch2_hash_info_init(c, dst_dir_u);
415 	} else {
416 		dst_dir_u = src_dir_u;
417 		dst_hash = src_hash;
418 	}
419 
420 	ret = bch2_dirent_rename(trans,
421 				 src_dir, &src_hash, &src_dir_u->bi_size,
422 				 dst_dir, &dst_hash, &dst_dir_u->bi_size,
423 				 src_name, &src_inum, &src_offset,
424 				 dst_name, &dst_inum, &dst_offset,
425 				 mode);
426 	if (ret)
427 		goto err;
428 
429 	ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum,
430 			      BTREE_ITER_intent);
431 	if (ret)
432 		goto err;
433 
434 	if (dst_inum.inum) {
435 		ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum,
436 				      BTREE_ITER_intent);
437 		if (ret)
438 			goto err;
439 	}
440 
441 	if (src_inode_u->bi_subvol &&
442 	    dst_dir.subvol != src_inode_u->bi_parent_subvol) {
443 		ret = subvol_update_parent(trans, src_inode_u->bi_subvol, dst_dir.subvol);
444 		if (ret)
445 			goto err;
446 	}
447 
448 	if (mode == BCH_RENAME_EXCHANGE &&
449 	    dst_inode_u->bi_subvol &&
450 	    src_dir.subvol != dst_inode_u->bi_parent_subvol) {
451 		ret = subvol_update_parent(trans, dst_inode_u->bi_subvol, src_dir.subvol);
452 		if (ret)
453 			goto err;
454 	}
455 
456 	/* Can't move across subvolumes, unless it's a subvolume root: */
457 	if (src_dir.subvol != dst_dir.subvol &&
458 	    (!src_inode_u->bi_subvol ||
459 	     (dst_inum.inum && !dst_inode_u->bi_subvol))) {
460 		ret = -EXDEV;
461 		goto err;
462 	}
463 
464 	if (src_inode_u->bi_parent_subvol)
465 		src_inode_u->bi_parent_subvol = dst_dir.subvol;
466 
467 	if ((mode == BCH_RENAME_EXCHANGE) &&
468 	    dst_inode_u->bi_parent_subvol)
469 		dst_inode_u->bi_parent_subvol = src_dir.subvol;
470 
471 	src_inode_u->bi_dir		= dst_dir_u->bi_inum;
472 	src_inode_u->bi_dir_offset	= dst_offset;
473 
474 	if (mode == BCH_RENAME_EXCHANGE) {
475 		dst_inode_u->bi_dir		= src_dir_u->bi_inum;
476 		dst_inode_u->bi_dir_offset	= src_offset;
477 	}
478 
479 	if (mode == BCH_RENAME_OVERWRITE &&
480 	    dst_inode_u->bi_dir		== dst_dir_u->bi_inum &&
481 	    dst_inode_u->bi_dir_offset	== src_offset) {
482 		dst_inode_u->bi_dir		= 0;
483 		dst_inode_u->bi_dir_offset	= 0;
484 	}
485 
486 	if (mode == BCH_RENAME_OVERWRITE) {
487 		if (S_ISDIR(src_inode_u->bi_mode) !=
488 		    S_ISDIR(dst_inode_u->bi_mode)) {
489 			ret = -ENOTDIR;
490 			goto err;
491 		}
492 
493 		if (S_ISDIR(dst_inode_u->bi_mode)) {
494 			ret = bch2_empty_dir_trans(trans, dst_inum);
495 			if (ret)
496 				goto err;
497 		}
498 	}
499 
500 	if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
501 	    S_ISDIR(src_inode_u->bi_mode)) {
502 		ret = -EXDEV;
503 		goto err;
504 	}
505 
506 	if (mode == BCH_RENAME_EXCHANGE &&
507 	    bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
508 	    S_ISDIR(dst_inode_u->bi_mode)) {
509 		ret = -EXDEV;
510 		goto err;
511 	}
512 
513 	if (is_subdir_for_nlink(src_inode_u)) {
514 		src_dir_u->bi_nlink--;
515 		dst_dir_u->bi_nlink++;
516 	}
517 
518 	if (S_ISDIR(src_inode_u->bi_mode) &&
519 	    !src_inode_u->bi_subvol)
520 		src_inode_u->bi_depth = dst_dir_u->bi_depth + 1;
521 
522 	if (mode == BCH_RENAME_EXCHANGE &&
523 	    S_ISDIR(dst_inode_u->bi_mode) &&
524 	    !dst_inode_u->bi_subvol)
525 		dst_inode_u->bi_depth = src_dir_u->bi_depth + 1;
526 
527 	if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) {
528 		dst_dir_u->bi_nlink--;
529 		src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
530 	}
531 
532 	if (mode == BCH_RENAME_OVERWRITE)
533 		bch2_inode_nlink_dec(trans, dst_inode_u);
534 
535 	src_dir_u->bi_mtime		= now;
536 	src_dir_u->bi_ctime		= now;
537 
538 	if (src_dir.inum != dst_dir.inum) {
539 		dst_dir_u->bi_mtime	= now;
540 		dst_dir_u->bi_ctime	= now;
541 	}
542 
543 	src_inode_u->bi_ctime		= now;
544 
545 	if (dst_inum.inum)
546 		dst_inode_u->bi_ctime	= now;
547 
548 	ret =   bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?:
549 		(src_dir.inum != dst_dir.inum
550 		 ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u)
551 		 : 0) ?:
552 		bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?:
553 		(dst_inum.inum
554 		 ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u)
555 		 : 0);
556 err:
557 	bch2_trans_iter_exit(trans, &dst_inode_iter);
558 	bch2_trans_iter_exit(trans, &src_inode_iter);
559 	bch2_trans_iter_exit(trans, &dst_dir_iter);
560 	bch2_trans_iter_exit(trans, &src_dir_iter);
561 	return ret;
562 }
563 
564 /* inum_to_path */
565 
prt_bytes_reversed(struct printbuf * out,const void * b,unsigned n)566 static inline void prt_bytes_reversed(struct printbuf *out, const void *b, unsigned n)
567 {
568 	bch2_printbuf_make_room(out, n);
569 
570 	unsigned can_print = min(n, printbuf_remaining(out));
571 
572 	b += n;
573 
574 	for (unsigned i = 0; i < can_print; i++)
575 		out->buf[out->pos++] = *((char *) --b);
576 
577 	printbuf_nul_terminate(out);
578 }
579 
prt_str_reversed(struct printbuf * out,const char * s)580 static inline void prt_str_reversed(struct printbuf *out, const char *s)
581 {
582 	prt_bytes_reversed(out, s, strlen(s));
583 }
584 
reverse_bytes(void * b,size_t n)585 static inline void reverse_bytes(void *b, size_t n)
586 {
587 	char *e = b + n, *s = b;
588 
589 	while (s < e) {
590 		--e;
591 		swap(*s, *e);
592 		s++;
593 	}
594 }
595 
596 /* 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)597 int bch2_inum_to_path(struct btree_trans *trans, subvol_inum inum, struct printbuf *path)
598 {
599 	unsigned orig_pos = path->pos;
600 	int ret = 0;
601 
602 	while (!(inum.subvol == BCACHEFS_ROOT_SUBVOL &&
603 		 inum.inum   == BCACHEFS_ROOT_INO)) {
604 		struct bch_inode_unpacked inode;
605 		ret = bch2_inode_find_by_inum_trans(trans, inum, &inode);
606 		if (ret)
607 			goto disconnected;
608 
609 		if (!inode.bi_dir && !inode.bi_dir_offset) {
610 			ret = -BCH_ERR_ENOENT_inode_no_backpointer;
611 			goto disconnected;
612 		}
613 
614 		inum.subvol	= inode.bi_parent_subvol ?: inum.subvol;
615 		inum.inum	= inode.bi_dir;
616 
617 		u32 snapshot;
618 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
619 		if (ret)
620 			goto disconnected;
621 
622 		struct btree_iter d_iter;
623 		struct bkey_s_c_dirent d = bch2_bkey_get_iter_typed(trans, &d_iter,
624 				BTREE_ID_dirents, SPOS(inode.bi_dir, inode.bi_dir_offset, snapshot),
625 				0, dirent);
626 		ret = bkey_err(d.s_c);
627 		if (ret)
628 			goto disconnected;
629 
630 		struct qstr dirent_name = bch2_dirent_get_name(d);
631 		prt_bytes_reversed(path, dirent_name.name, dirent_name.len);
632 
633 		prt_char(path, '/');
634 
635 		bch2_trans_iter_exit(trans, &d_iter);
636 	}
637 
638 	if (orig_pos == path->pos)
639 		prt_char(path, '/');
640 out:
641 	ret = path->allocation_failure ? -ENOMEM : 0;
642 	if (ret)
643 		goto err;
644 
645 	reverse_bytes(path->buf + orig_pos, path->pos - orig_pos);
646 	return 0;
647 err:
648 	return ret;
649 disconnected:
650 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
651 		goto err;
652 
653 	prt_str_reversed(path, "(disconnected)");
654 	goto out;
655 }
656 
657 /* fsck */
658 
bch2_check_dirent_inode_dirent(struct btree_trans * trans,struct bkey_s_c_dirent d,struct bch_inode_unpacked * target,bool in_fsck)659 static int bch2_check_dirent_inode_dirent(struct btree_trans *trans,
660 					  struct bkey_s_c_dirent d,
661 					  struct bch_inode_unpacked *target,
662 					  bool in_fsck)
663 {
664 	struct bch_fs *c = trans->c;
665 	struct printbuf buf = PRINTBUF;
666 	struct btree_iter bp_iter = {};
667 	int ret = 0;
668 
669 	if (inode_points_to_dirent(target, d))
670 		return 0;
671 
672 	if (!target->bi_dir &&
673 	    !target->bi_dir_offset) {
674 		fsck_err_on(S_ISDIR(target->bi_mode),
675 			    trans, inode_dir_missing_backpointer,
676 			    "directory with missing backpointer\n%s",
677 			    (printbuf_reset(&buf),
678 			     bch2_bkey_val_to_text(&buf, c, d.s_c),
679 			     prt_printf(&buf, "\n"),
680 			     bch2_inode_unpacked_to_text(&buf, target),
681 			     buf.buf));
682 
683 		fsck_err_on(target->bi_flags & BCH_INODE_unlinked,
684 			    trans, inode_unlinked_but_has_dirent,
685 			    "inode unlinked but has dirent\n%s",
686 			    (printbuf_reset(&buf),
687 			     bch2_bkey_val_to_text(&buf, c, d.s_c),
688 			     prt_printf(&buf, "\n"),
689 			     bch2_inode_unpacked_to_text(&buf, target),
690 			     buf.buf));
691 
692 		target->bi_flags &= ~BCH_INODE_unlinked;
693 		target->bi_dir		= d.k->p.inode;
694 		target->bi_dir_offset	= d.k->p.offset;
695 		return __bch2_fsck_write_inode(trans, target);
696 	}
697 
698 	if (bch2_inode_should_have_single_bp(target) &&
699 	    !fsck_err(trans, inode_wrong_backpointer,
700 		      "dirent points to inode that does not point back:\n%s",
701 		      (bch2_bkey_val_to_text(&buf, c, d.s_c),
702 		       prt_newline(&buf),
703 		       bch2_inode_unpacked_to_text(&buf, target),
704 		       buf.buf)))
705 		goto err;
706 
707 	struct bkey_s_c_dirent bp_dirent =
708 		bch2_bkey_get_iter_typed(trans, &bp_iter, BTREE_ID_dirents,
709 			      SPOS(target->bi_dir, target->bi_dir_offset, target->bi_snapshot),
710 			      0, dirent);
711 	ret = bkey_err(bp_dirent);
712 	if (ret && !bch2_err_matches(ret, ENOENT))
713 		goto err;
714 
715 	bool backpointer_exists = !ret;
716 	ret = 0;
717 
718 	if (!backpointer_exists) {
719 		if (fsck_err(trans, inode_wrong_backpointer,
720 			     "inode %llu:%u has wrong backpointer:\n"
721 			     "got       %llu:%llu\n"
722 			     "should be %llu:%llu",
723 			     target->bi_inum, target->bi_snapshot,
724 			     target->bi_dir,
725 			     target->bi_dir_offset,
726 			     d.k->p.inode,
727 			     d.k->p.offset)) {
728 			target->bi_dir		= d.k->p.inode;
729 			target->bi_dir_offset	= d.k->p.offset;
730 			ret = __bch2_fsck_write_inode(trans, target);
731 		}
732 	} else {
733 		bch2_bkey_val_to_text(&buf, c, d.s_c);
734 		prt_newline(&buf);
735 		bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
736 
737 		if (S_ISDIR(target->bi_mode) || target->bi_subvol) {
738 			/*
739 			 * XXX: verify connectivity of the other dirent
740 			 * up to the root before removing this one
741 			 *
742 			 * Additionally, bch2_lookup would need to cope with the
743 			 * dirent it found being removed - or should we remove
744 			 * the other one, even though the inode points to it?
745 			 */
746 			if (in_fsck) {
747 				if (fsck_err(trans, inode_dir_multiple_links,
748 					     "%s %llu:%u with multiple links\n%s",
749 					     S_ISDIR(target->bi_mode) ? "directory" : "subvolume",
750 					     target->bi_inum, target->bi_snapshot, buf.buf))
751 					ret = bch2_fsck_remove_dirent(trans, d.k->p);
752 			} else {
753 				bch2_fs_inconsistent(c,
754 						"%s %llu:%u with multiple links\n%s",
755 						S_ISDIR(target->bi_mode) ? "directory" : "subvolume",
756 						target->bi_inum, target->bi_snapshot, buf.buf);
757 			}
758 
759 			goto out;
760 		} else {
761 			/*
762 			 * hardlinked file with nlink 0:
763 			 * We're just adjusting nlink here so check_nlinks() will pick
764 			 * it up, it ignores inodes with nlink 0
765 			 */
766 			if (fsck_err_on(!target->bi_nlink,
767 					trans, inode_multiple_links_but_nlink_0,
768 					"inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
769 					target->bi_inum, target->bi_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
770 				target->bi_nlink++;
771 				target->bi_flags &= ~BCH_INODE_unlinked;
772 				ret = __bch2_fsck_write_inode(trans, target);
773 				if (ret)
774 					goto err;
775 			}
776 		}
777 	}
778 out:
779 err:
780 fsck_err:
781 	bch2_trans_iter_exit(trans, &bp_iter);
782 	printbuf_exit(&buf);
783 	bch_err_fn(c, ret);
784 	return ret;
785 }
786 
__bch2_check_dirent_target(struct btree_trans * trans,struct btree_iter * dirent_iter,struct bkey_s_c_dirent d,struct bch_inode_unpacked * target,bool in_fsck)787 int __bch2_check_dirent_target(struct btree_trans *trans,
788 			       struct btree_iter *dirent_iter,
789 			       struct bkey_s_c_dirent d,
790 			       struct bch_inode_unpacked *target,
791 			       bool in_fsck)
792 {
793 	struct bch_fs *c = trans->c;
794 	struct printbuf buf = PRINTBUF;
795 	int ret = 0;
796 
797 	ret = bch2_check_dirent_inode_dirent(trans, d, target, in_fsck);
798 	if (ret)
799 		goto err;
800 
801 	if (fsck_err_on(d.v->d_type != inode_d_type(target),
802 			trans, dirent_d_type_wrong,
803 			"incorrect d_type: got %s, should be %s:\n%s",
804 			bch2_d_type_str(d.v->d_type),
805 			bch2_d_type_str(inode_d_type(target)),
806 			(printbuf_reset(&buf),
807 			 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
808 		struct bkey_i_dirent *n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
809 		ret = PTR_ERR_OR_ZERO(n);
810 		if (ret)
811 			goto err;
812 
813 		bkey_reassemble(&n->k_i, d.s_c);
814 		n->v.d_type = inode_d_type(target);
815 		if (n->v.d_type == DT_SUBVOL) {
816 			n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
817 			n->v.d_child_subvol = cpu_to_le32(target->bi_subvol);
818 		} else {
819 			n->v.d_inum = cpu_to_le64(target->bi_inum);
820 		}
821 
822 		ret = bch2_trans_update(trans, dirent_iter, &n->k_i, 0);
823 		if (ret)
824 			goto err;
825 	}
826 err:
827 fsck_err:
828 	printbuf_exit(&buf);
829 	bch_err_fn(c, ret);
830 	return ret;
831 }
832