xref: /linux/fs/btrfs/tree-log.c (revision e3c94a539e767c7bf055be4ed6911246812fcb6e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2008 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/blkdev.h>
9 #include <linux/list_sort.h>
10 #include <linux/iversion.h>
11 #include "misc.h"
12 #include "ctree.h"
13 #include "tree-log.h"
14 #include "disk-io.h"
15 #include "locking.h"
16 #include "backref.h"
17 #include "compression.h"
18 #include "qgroup.h"
19 #include "block-group.h"
20 #include "space-info.h"
21 #include "inode-item.h"
22 #include "fs.h"
23 #include "accessors.h"
24 #include "extent-tree.h"
25 #include "root-tree.h"
26 #include "dir-item.h"
27 #include "file-item.h"
28 #include "file.h"
29 #include "orphan.h"
30 #include "tree-checker.h"
31 
32 #define MAX_CONFLICT_INODES 10
33 
34 /* magic values for the inode_only field in btrfs_log_inode:
35  *
36  * LOG_INODE_ALL means to log everything
37  * LOG_INODE_EXISTS means to log just enough to recreate the inode
38  * during log replay
39  */
40 enum {
41 	LOG_INODE_ALL,
42 	LOG_INODE_EXISTS,
43 };
44 
45 /*
46  * directory trouble cases
47  *
48  * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
49  * log, we must force a full commit before doing an fsync of the directory
50  * where the unlink was done.
51  * ---> record transid of last unlink/rename per directory
52  *
53  * mkdir foo/some_dir
54  * normal commit
55  * rename foo/some_dir foo2/some_dir
56  * mkdir foo/some_dir
57  * fsync foo/some_dir/some_file
58  *
59  * The fsync above will unlink the original some_dir without recording
60  * it in its new location (foo2).  After a crash, some_dir will be gone
61  * unless the fsync of some_file forces a full commit
62  *
63  * 2) we must log any new names for any file or dir that is in the fsync
64  * log. ---> check inode while renaming/linking.
65  *
66  * 2a) we must log any new names for any file or dir during rename
67  * when the directory they are being removed from was logged.
68  * ---> check inode and old parent dir during rename
69  *
70  *  2a is actually the more important variant.  With the extra logging
71  *  a crash might unlink the old name without recreating the new one
72  *
73  * 3) after a crash, we must go through any directories with a link count
74  * of zero and redo the rm -rf
75  *
76  * mkdir f1/foo
77  * normal commit
78  * rm -rf f1/foo
79  * fsync(f1)
80  *
81  * The directory f1 was fully removed from the FS, but fsync was never
82  * called on f1, only its parent dir.  After a crash the rm -rf must
83  * be replayed.  This must be able to recurse down the entire
84  * directory tree.  The inode link count fixup code takes care of the
85  * ugly details.
86  */
87 
88 /*
89  * stages for the tree walking.  The first
90  * stage (0) is to only pin down the blocks we find
91  * the second stage (1) is to make sure that all the inodes
92  * we find in the log are created in the subvolume.
93  *
94  * The last stage is to deal with directories and links and extents
95  * and all the other fun semantics
96  */
97 enum {
98 	LOG_WALK_PIN_ONLY,
99 	LOG_WALK_REPLAY_INODES,
100 	LOG_WALK_REPLAY_DIR_INDEX,
101 	LOG_WALK_REPLAY_ALL,
102 };
103 
104 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
105 			   struct btrfs_inode *inode,
106 			   int inode_only,
107 			   struct btrfs_log_ctx *ctx);
108 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
109 			     struct btrfs_root *root,
110 			     struct btrfs_path *path, u64 objectid);
111 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
112 				       struct btrfs_root *root,
113 				       struct btrfs_root *log,
114 				       struct btrfs_path *path,
115 				       u64 dirid, bool del_all);
116 static void wait_log_commit(struct btrfs_root *root, int transid);
117 
118 /*
119  * tree logging is a special write ahead log used to make sure that
120  * fsyncs and O_SYNCs can happen without doing full tree commits.
121  *
122  * Full tree commits are expensive because they require commonly
123  * modified blocks to be recowed, creating many dirty pages in the
124  * extent tree an 4x-6x higher write load than ext3.
125  *
126  * Instead of doing a tree commit on every fsync, we use the
127  * key ranges and transaction ids to find items for a given file or directory
128  * that have changed in this transaction.  Those items are copied into
129  * a special tree (one per subvolume root), that tree is written to disk
130  * and then the fsync is considered complete.
131  *
132  * After a crash, items are copied out of the log-tree back into the
133  * subvolume tree.  Any file data extents found are recorded in the extent
134  * allocation tree, and the log-tree freed.
135  *
136  * The log tree is read three times, once to pin down all the extents it is
137  * using in ram and once, once to create all the inodes logged in the tree
138  * and once to do all the other items.
139  */
140 
btrfs_iget_logging(u64 objectid,struct btrfs_root * root)141 static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
142 {
143 	unsigned int nofs_flag;
144 	struct btrfs_inode *inode;
145 
146 	/* Only meant to be called for subvolume roots and not for log roots. */
147 	ASSERT(btrfs_is_fstree(btrfs_root_id(root)));
148 
149 	/*
150 	 * We're holding a transaction handle whether we are logging or
151 	 * replaying a log tree, so we must make sure NOFS semantics apply
152 	 * because btrfs_alloc_inode() may be triggered and it uses GFP_KERNEL
153 	 * to allocate an inode, which can recurse back into the filesystem and
154 	 * attempt a transaction commit, resulting in a deadlock.
155 	 */
156 	nofs_flag = memalloc_nofs_save();
157 	inode = btrfs_iget(objectid, root);
158 	memalloc_nofs_restore(nofs_flag);
159 
160 	return inode;
161 }
162 
163 /*
164  * start a sub transaction and setup the log tree
165  * this increments the log tree writer count to make the people
166  * syncing the tree wait for us to finish
167  */
start_log_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)168 static int start_log_trans(struct btrfs_trans_handle *trans,
169 			   struct btrfs_root *root,
170 			   struct btrfs_log_ctx *ctx)
171 {
172 	struct btrfs_fs_info *fs_info = root->fs_info;
173 	struct btrfs_root *tree_root = fs_info->tree_root;
174 	const bool zoned = btrfs_is_zoned(fs_info);
175 	int ret = 0;
176 	bool created = false;
177 
178 	/*
179 	 * First check if the log root tree was already created. If not, create
180 	 * it before locking the root's log_mutex, just to keep lockdep happy.
181 	 */
182 	if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
183 		mutex_lock(&tree_root->log_mutex);
184 		if (!fs_info->log_root_tree) {
185 			ret = btrfs_init_log_root_tree(trans, fs_info);
186 			if (!ret) {
187 				set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
188 				created = true;
189 			}
190 		}
191 		mutex_unlock(&tree_root->log_mutex);
192 		if (ret)
193 			return ret;
194 	}
195 
196 	mutex_lock(&root->log_mutex);
197 
198 again:
199 	if (root->log_root) {
200 		int index = (root->log_transid + 1) % 2;
201 
202 		if (btrfs_need_log_full_commit(trans)) {
203 			ret = BTRFS_LOG_FORCE_COMMIT;
204 			goto out;
205 		}
206 
207 		if (zoned && atomic_read(&root->log_commit[index])) {
208 			wait_log_commit(root, root->log_transid - 1);
209 			goto again;
210 		}
211 
212 		if (!root->log_start_pid) {
213 			clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
214 			root->log_start_pid = current->pid;
215 		} else if (root->log_start_pid != current->pid) {
216 			set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
217 		}
218 	} else {
219 		/*
220 		 * This means fs_info->log_root_tree was already created
221 		 * for some other FS trees. Do the full commit not to mix
222 		 * nodes from multiple log transactions to do sequential
223 		 * writing.
224 		 */
225 		if (zoned && !created) {
226 			ret = BTRFS_LOG_FORCE_COMMIT;
227 			goto out;
228 		}
229 
230 		ret = btrfs_add_log_tree(trans, root);
231 		if (ret)
232 			goto out;
233 
234 		set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
235 		clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
236 		root->log_start_pid = current->pid;
237 	}
238 
239 	atomic_inc(&root->log_writers);
240 	if (!ctx->logging_new_name) {
241 		int index = root->log_transid % 2;
242 		list_add_tail(&ctx->list, &root->log_ctxs[index]);
243 		ctx->log_transid = root->log_transid;
244 	}
245 
246 out:
247 	mutex_unlock(&root->log_mutex);
248 	return ret;
249 }
250 
251 /*
252  * returns 0 if there was a log transaction running and we were able
253  * to join, or returns -ENOENT if there were not transactions
254  * in progress
255  */
join_running_log_trans(struct btrfs_root * root)256 static int join_running_log_trans(struct btrfs_root *root)
257 {
258 	const bool zoned = btrfs_is_zoned(root->fs_info);
259 	int ret = -ENOENT;
260 
261 	if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
262 		return ret;
263 
264 	mutex_lock(&root->log_mutex);
265 again:
266 	if (root->log_root) {
267 		int index = (root->log_transid + 1) % 2;
268 
269 		ret = 0;
270 		if (zoned && atomic_read(&root->log_commit[index])) {
271 			wait_log_commit(root, root->log_transid - 1);
272 			goto again;
273 		}
274 		atomic_inc(&root->log_writers);
275 	}
276 	mutex_unlock(&root->log_mutex);
277 	return ret;
278 }
279 
280 /*
281  * This either makes the current running log transaction wait
282  * until you call btrfs_end_log_trans() or it makes any future
283  * log transactions wait until you call btrfs_end_log_trans()
284  */
btrfs_pin_log_trans(struct btrfs_root * root)285 void btrfs_pin_log_trans(struct btrfs_root *root)
286 {
287 	atomic_inc(&root->log_writers);
288 }
289 
290 /*
291  * indicate we're done making changes to the log tree
292  * and wake up anyone waiting to do a sync
293  */
btrfs_end_log_trans(struct btrfs_root * root)294 void btrfs_end_log_trans(struct btrfs_root *root)
295 {
296 	if (atomic_dec_and_test(&root->log_writers)) {
297 		/* atomic_dec_and_test implies a barrier */
298 		cond_wake_up_nomb(&root->log_writer_wait);
299 	}
300 }
301 
302 /*
303  * the walk control struct is used to pass state down the chain when
304  * processing the log tree.  The stage field tells us which part
305  * of the log tree processing we are currently doing.  The others
306  * are state fields used for that specific part
307  */
308 struct walk_control {
309 	/* should we free the extent on disk when done?  This is used
310 	 * at transaction commit time while freeing a log tree
311 	 */
312 	int free;
313 
314 	/* pin only walk, we record which extents on disk belong to the
315 	 * log trees
316 	 */
317 	int pin;
318 
319 	/* what stage of the replay code we're currently in */
320 	int stage;
321 
322 	/*
323 	 * Ignore any items from the inode currently being processed. Needs
324 	 * to be set every time we find a BTRFS_INODE_ITEM_KEY.
325 	 */
326 	bool ignore_cur_inode;
327 
328 	/* the root we are currently replaying */
329 	struct btrfs_root *replay_dest;
330 
331 	/* the trans handle for the current replay */
332 	struct btrfs_trans_handle *trans;
333 
334 	/* the function that gets used to process blocks we find in the
335 	 * tree.  Note the extent_buffer might not be up to date when it is
336 	 * passed in, and it must be checked or read if you need the data
337 	 * inside it
338 	 */
339 	int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
340 			    struct walk_control *wc, u64 gen, int level);
341 };
342 
343 /*
344  * process_func used to pin down extents, write them or wait on them
345  */
process_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen,int level)346 static int process_one_buffer(struct btrfs_root *log,
347 			      struct extent_buffer *eb,
348 			      struct walk_control *wc, u64 gen, int level)
349 {
350 	struct btrfs_fs_info *fs_info = log->fs_info;
351 	int ret = 0;
352 
353 	/*
354 	 * If this fs is mixed then we need to be able to process the leaves to
355 	 * pin down any logged extents, so we have to read the block.
356 	 */
357 	if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
358 		struct btrfs_tree_parent_check check = {
359 			.level = level,
360 			.transid = gen
361 		};
362 
363 		ret = btrfs_read_extent_buffer(eb, &check);
364 		if (ret)
365 			return ret;
366 	}
367 
368 	if (wc->pin) {
369 		ret = btrfs_pin_extent_for_log_replay(wc->trans, eb);
370 		if (ret)
371 			return ret;
372 
373 		if (btrfs_buffer_uptodate(eb, gen, 0) &&
374 		    btrfs_header_level(eb) == 0)
375 			ret = btrfs_exclude_logged_extents(eb);
376 	}
377 	return ret;
378 }
379 
380 /*
381  * Item overwrite used by log replay. The given eb, slot and key all refer to
382  * the source data we are copying out.
383  *
384  * The given root is for the tree we are copying into, and path is a scratch
385  * path for use in this function (it should be released on entry and will be
386  * released on exit).
387  *
388  * If the key is already in the destination tree the existing item is
389  * overwritten.  If the existing item isn't big enough, it is extended.
390  * If it is too large, it is truncated.
391  *
392  * If the key isn't in the destination yet, a new item is inserted.
393  */
overwrite_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)394 static int overwrite_item(struct btrfs_trans_handle *trans,
395 			  struct btrfs_root *root,
396 			  struct btrfs_path *path,
397 			  struct extent_buffer *eb, int slot,
398 			  struct btrfs_key *key)
399 {
400 	int ret;
401 	u32 item_size;
402 	u64 saved_i_size = 0;
403 	int save_old_i_size = 0;
404 	unsigned long src_ptr;
405 	unsigned long dst_ptr;
406 	struct extent_buffer *dst_eb;
407 	int dst_slot;
408 	bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
409 
410 	/*
411 	 * This is only used during log replay, so the root is always from a
412 	 * fs/subvolume tree. In case we ever need to support a log root, then
413 	 * we'll have to clone the leaf in the path, release the path and use
414 	 * the leaf before writing into the log tree. See the comments at
415 	 * copy_items() for more details.
416 	 */
417 	ASSERT(btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID);
418 
419 	item_size = btrfs_item_size(eb, slot);
420 	src_ptr = btrfs_item_ptr_offset(eb, slot);
421 
422 	/* Look for the key in the destination tree. */
423 	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
424 	if (ret < 0)
425 		return ret;
426 
427 	dst_eb = path->nodes[0];
428 	dst_slot = path->slots[0];
429 
430 	if (ret == 0) {
431 		char *src_copy;
432 		const u32 dst_size = btrfs_item_size(dst_eb, dst_slot);
433 
434 		if (dst_size != item_size)
435 			goto insert;
436 
437 		if (item_size == 0) {
438 			btrfs_release_path(path);
439 			return 0;
440 		}
441 		src_copy = kmalloc(item_size, GFP_NOFS);
442 		if (!src_copy) {
443 			btrfs_release_path(path);
444 			return -ENOMEM;
445 		}
446 
447 		read_extent_buffer(eb, src_copy, src_ptr, item_size);
448 		dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
449 		ret = memcmp_extent_buffer(dst_eb, src_copy, dst_ptr, item_size);
450 
451 		kfree(src_copy);
452 		/*
453 		 * they have the same contents, just return, this saves
454 		 * us from cowing blocks in the destination tree and doing
455 		 * extra writes that may not have been done by a previous
456 		 * sync
457 		 */
458 		if (ret == 0) {
459 			btrfs_release_path(path);
460 			return 0;
461 		}
462 
463 		/*
464 		 * We need to load the old nbytes into the inode so when we
465 		 * replay the extents we've logged we get the right nbytes.
466 		 */
467 		if (inode_item) {
468 			struct btrfs_inode_item *item;
469 			u64 nbytes;
470 			u32 mode;
471 
472 			item = btrfs_item_ptr(dst_eb, dst_slot,
473 					      struct btrfs_inode_item);
474 			nbytes = btrfs_inode_nbytes(dst_eb, item);
475 			item = btrfs_item_ptr(eb, slot,
476 					      struct btrfs_inode_item);
477 			btrfs_set_inode_nbytes(eb, item, nbytes);
478 
479 			/*
480 			 * If this is a directory we need to reset the i_size to
481 			 * 0 so that we can set it up properly when replaying
482 			 * the rest of the items in this log.
483 			 */
484 			mode = btrfs_inode_mode(eb, item);
485 			if (S_ISDIR(mode))
486 				btrfs_set_inode_size(eb, item, 0);
487 		}
488 	} else if (inode_item) {
489 		struct btrfs_inode_item *item;
490 		u32 mode;
491 
492 		/*
493 		 * New inode, set nbytes to 0 so that the nbytes comes out
494 		 * properly when we replay the extents.
495 		 */
496 		item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
497 		btrfs_set_inode_nbytes(eb, item, 0);
498 
499 		/*
500 		 * If this is a directory we need to reset the i_size to 0 so
501 		 * that we can set it up properly when replaying the rest of
502 		 * the items in this log.
503 		 */
504 		mode = btrfs_inode_mode(eb, item);
505 		if (S_ISDIR(mode))
506 			btrfs_set_inode_size(eb, item, 0);
507 	}
508 insert:
509 	btrfs_release_path(path);
510 	/* try to insert the key into the destination tree */
511 	path->skip_release_on_error = 1;
512 	ret = btrfs_insert_empty_item(trans, root, path,
513 				      key, item_size);
514 	path->skip_release_on_error = 0;
515 
516 	dst_eb = path->nodes[0];
517 	dst_slot = path->slots[0];
518 
519 	/* make sure any existing item is the correct size */
520 	if (ret == -EEXIST || ret == -EOVERFLOW) {
521 		const u32 found_size = btrfs_item_size(dst_eb, dst_slot);
522 
523 		if (found_size > item_size)
524 			btrfs_truncate_item(trans, path, item_size, 1);
525 		else if (found_size < item_size)
526 			btrfs_extend_item(trans, path, item_size - found_size);
527 	} else if (ret) {
528 		return ret;
529 	}
530 	dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
531 
532 	/* don't overwrite an existing inode if the generation number
533 	 * was logged as zero.  This is done when the tree logging code
534 	 * is just logging an inode to make sure it exists after recovery.
535 	 *
536 	 * Also, don't overwrite i_size on directories during replay.
537 	 * log replay inserts and removes directory items based on the
538 	 * state of the tree found in the subvolume, and i_size is modified
539 	 * as it goes
540 	 */
541 	if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
542 		struct btrfs_inode_item *src_item;
543 		struct btrfs_inode_item *dst_item;
544 
545 		src_item = (struct btrfs_inode_item *)src_ptr;
546 		dst_item = (struct btrfs_inode_item *)dst_ptr;
547 
548 		if (btrfs_inode_generation(eb, src_item) == 0) {
549 			const u64 ino_size = btrfs_inode_size(eb, src_item);
550 
551 			/*
552 			 * For regular files an ino_size == 0 is used only when
553 			 * logging that an inode exists, as part of a directory
554 			 * fsync, and the inode wasn't fsynced before. In this
555 			 * case don't set the size of the inode in the fs/subvol
556 			 * tree, otherwise we would be throwing valid data away.
557 			 */
558 			if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
559 			    S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
560 			    ino_size != 0)
561 				btrfs_set_inode_size(dst_eb, dst_item, ino_size);
562 			goto no_copy;
563 		}
564 
565 		if (S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
566 		    S_ISDIR(btrfs_inode_mode(dst_eb, dst_item))) {
567 			save_old_i_size = 1;
568 			saved_i_size = btrfs_inode_size(dst_eb, dst_item);
569 		}
570 	}
571 
572 	copy_extent_buffer(dst_eb, eb, dst_ptr, src_ptr, item_size);
573 
574 	if (save_old_i_size) {
575 		struct btrfs_inode_item *dst_item;
576 
577 		dst_item = (struct btrfs_inode_item *)dst_ptr;
578 		btrfs_set_inode_size(dst_eb, dst_item, saved_i_size);
579 	}
580 
581 	/* make sure the generation is filled in */
582 	if (key->type == BTRFS_INODE_ITEM_KEY) {
583 		struct btrfs_inode_item *dst_item;
584 
585 		dst_item = (struct btrfs_inode_item *)dst_ptr;
586 		if (btrfs_inode_generation(dst_eb, dst_item) == 0)
587 			btrfs_set_inode_generation(dst_eb, dst_item, trans->transid);
588 	}
589 no_copy:
590 	btrfs_release_path(path);
591 	return 0;
592 }
593 
read_alloc_one_name(struct extent_buffer * eb,void * start,int len,struct fscrypt_str * name)594 static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len,
595 			       struct fscrypt_str *name)
596 {
597 	char *buf;
598 
599 	buf = kmalloc(len, GFP_NOFS);
600 	if (!buf)
601 		return -ENOMEM;
602 
603 	read_extent_buffer(eb, buf, (unsigned long)start, len);
604 	name->name = buf;
605 	name->len = len;
606 	return 0;
607 }
608 
609 /* replays a single extent in 'eb' at 'slot' with 'key' into the
610  * subvolume 'root'.  path is released on entry and should be released
611  * on exit.
612  *
613  * extents in the log tree have not been allocated out of the extent
614  * tree yet.  So, this completes the allocation, taking a reference
615  * as required if the extent already exists or creating a new extent
616  * if it isn't in the extent allocation tree yet.
617  *
618  * The extent is inserted into the file, dropping any existing extents
619  * from the file that overlap the new one.
620  */
replay_one_extent(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)621 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
622 				      struct btrfs_root *root,
623 				      struct btrfs_path *path,
624 				      struct extent_buffer *eb, int slot,
625 				      struct btrfs_key *key)
626 {
627 	struct btrfs_drop_extents_args drop_args = { 0 };
628 	struct btrfs_fs_info *fs_info = root->fs_info;
629 	int found_type;
630 	u64 extent_end;
631 	u64 start = key->offset;
632 	u64 nbytes = 0;
633 	struct btrfs_file_extent_item *item;
634 	struct btrfs_inode *inode = NULL;
635 	unsigned long size;
636 	int ret = 0;
637 
638 	item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
639 	found_type = btrfs_file_extent_type(eb, item);
640 
641 	if (found_type == BTRFS_FILE_EXTENT_REG ||
642 	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
643 		nbytes = btrfs_file_extent_num_bytes(eb, item);
644 		extent_end = start + nbytes;
645 
646 		/*
647 		 * We don't add to the inodes nbytes if we are prealloc or a
648 		 * hole.
649 		 */
650 		if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
651 			nbytes = 0;
652 	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
653 		size = btrfs_file_extent_ram_bytes(eb, item);
654 		nbytes = btrfs_file_extent_ram_bytes(eb, item);
655 		extent_end = ALIGN(start + size,
656 				   fs_info->sectorsize);
657 	} else {
658 		btrfs_err(fs_info,
659 		  "unexpected extent type=%d root=%llu inode=%llu offset=%llu",
660 			  found_type, btrfs_root_id(root), key->objectid, key->offset);
661 		return -EUCLEAN;
662 	}
663 
664 	inode = btrfs_iget_logging(key->objectid, root);
665 	if (IS_ERR(inode))
666 		return PTR_ERR(inode);
667 
668 	/*
669 	 * first check to see if we already have this extent in the
670 	 * file.  This must be done before the btrfs_drop_extents run
671 	 * so we don't try to drop this extent.
672 	 */
673 	ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), start, 0);
674 
675 	if (ret == 0 &&
676 	    (found_type == BTRFS_FILE_EXTENT_REG ||
677 	     found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
678 		struct btrfs_file_extent_item existing;
679 		unsigned long ptr;
680 
681 		ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
682 		read_extent_buffer(path->nodes[0], &existing, ptr, sizeof(existing));
683 
684 		/*
685 		 * we already have a pointer to this exact extent,
686 		 * we don't have to do anything
687 		 */
688 		if (memcmp_extent_buffer(eb, &existing, (unsigned long)item,
689 					 sizeof(existing)) == 0) {
690 			btrfs_release_path(path);
691 			goto out;
692 		}
693 	}
694 	btrfs_release_path(path);
695 
696 	/* drop any overlapping extents */
697 	drop_args.start = start;
698 	drop_args.end = extent_end;
699 	drop_args.drop_cache = true;
700 	ret = btrfs_drop_extents(trans, root, inode, &drop_args);
701 	if (ret)
702 		goto out;
703 
704 	if (found_type == BTRFS_FILE_EXTENT_REG ||
705 	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
706 		u64 offset;
707 		unsigned long dest_offset;
708 		struct btrfs_key ins;
709 
710 		if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
711 		    btrfs_fs_incompat(fs_info, NO_HOLES))
712 			goto update_inode;
713 
714 		ret = btrfs_insert_empty_item(trans, root, path, key,
715 					      sizeof(*item));
716 		if (ret)
717 			goto out;
718 		dest_offset = btrfs_item_ptr_offset(path->nodes[0],
719 						    path->slots[0]);
720 		copy_extent_buffer(path->nodes[0], eb, dest_offset,
721 				(unsigned long)item,  sizeof(*item));
722 
723 		ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
724 		ins.type = BTRFS_EXTENT_ITEM_KEY;
725 		ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
726 		offset = key->offset - btrfs_file_extent_offset(eb, item);
727 
728 		/*
729 		 * Manually record dirty extent, as here we did a shallow
730 		 * file extent item copy and skip normal backref update,
731 		 * but modifying extent tree all by ourselves.
732 		 * So need to manually record dirty extent for qgroup,
733 		 * as the owner of the file extent changed from log tree
734 		 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
735 		 */
736 		ret = btrfs_qgroup_trace_extent(trans,
737 				btrfs_file_extent_disk_bytenr(eb, item),
738 				btrfs_file_extent_disk_num_bytes(eb, item));
739 		if (ret < 0)
740 			goto out;
741 
742 		if (ins.objectid > 0) {
743 			u64 csum_start;
744 			u64 csum_end;
745 			LIST_HEAD(ordered_sums);
746 
747 			/*
748 			 * is this extent already allocated in the extent
749 			 * allocation tree?  If so, just add a reference
750 			 */
751 			ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
752 						ins.offset);
753 			if (ret < 0) {
754 				goto out;
755 			} else if (ret == 0) {
756 				struct btrfs_ref ref = {
757 					.action = BTRFS_ADD_DELAYED_REF,
758 					.bytenr = ins.objectid,
759 					.num_bytes = ins.offset,
760 					.owning_root = btrfs_root_id(root),
761 					.ref_root = btrfs_root_id(root),
762 				};
763 				btrfs_init_data_ref(&ref, key->objectid, offset,
764 						    0, false);
765 				ret = btrfs_inc_extent_ref(trans, &ref);
766 				if (ret)
767 					goto out;
768 			} else {
769 				/*
770 				 * insert the extent pointer in the extent
771 				 * allocation tree
772 				 */
773 				ret = btrfs_alloc_logged_file_extent(trans,
774 						btrfs_root_id(root),
775 						key->objectid, offset, &ins);
776 				if (ret)
777 					goto out;
778 			}
779 			btrfs_release_path(path);
780 
781 			if (btrfs_file_extent_compression(eb, item)) {
782 				csum_start = ins.objectid;
783 				csum_end = csum_start + ins.offset;
784 			} else {
785 				csum_start = ins.objectid +
786 					btrfs_file_extent_offset(eb, item);
787 				csum_end = csum_start +
788 					btrfs_file_extent_num_bytes(eb, item);
789 			}
790 
791 			ret = btrfs_lookup_csums_list(root->log_root,
792 						csum_start, csum_end - 1,
793 						&ordered_sums, false);
794 			if (ret < 0)
795 				goto out;
796 			ret = 0;
797 			/*
798 			 * Now delete all existing cums in the csum root that
799 			 * cover our range. We do this because we can have an
800 			 * extent that is completely referenced by one file
801 			 * extent item and partially referenced by another
802 			 * file extent item (like after using the clone or
803 			 * extent_same ioctls). In this case if we end up doing
804 			 * the replay of the one that partially references the
805 			 * extent first, and we do not do the csum deletion
806 			 * below, we can get 2 csum items in the csum tree that
807 			 * overlap each other. For example, imagine our log has
808 			 * the two following file extent items:
809 			 *
810 			 * key (257 EXTENT_DATA 409600)
811 			 *     extent data disk byte 12845056 nr 102400
812 			 *     extent data offset 20480 nr 20480 ram 102400
813 			 *
814 			 * key (257 EXTENT_DATA 819200)
815 			 *     extent data disk byte 12845056 nr 102400
816 			 *     extent data offset 0 nr 102400 ram 102400
817 			 *
818 			 * Where the second one fully references the 100K extent
819 			 * that starts at disk byte 12845056, and the log tree
820 			 * has a single csum item that covers the entire range
821 			 * of the extent:
822 			 *
823 			 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
824 			 *
825 			 * After the first file extent item is replayed, the
826 			 * csum tree gets the following csum item:
827 			 *
828 			 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
829 			 *
830 			 * Which covers the 20K sub-range starting at offset 20K
831 			 * of our extent. Now when we replay the second file
832 			 * extent item, if we do not delete existing csum items
833 			 * that cover any of its blocks, we end up getting two
834 			 * csum items in our csum tree that overlap each other:
835 			 *
836 			 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
837 			 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
838 			 *
839 			 * Which is a problem, because after this anyone trying
840 			 * to lookup up for the checksum of any block of our
841 			 * extent starting at an offset of 40K or higher, will
842 			 * end up looking at the second csum item only, which
843 			 * does not contain the checksum for any block starting
844 			 * at offset 40K or higher of our extent.
845 			 */
846 			while (!list_empty(&ordered_sums)) {
847 				struct btrfs_ordered_sum *sums;
848 				struct btrfs_root *csum_root;
849 
850 				sums = list_first_entry(&ordered_sums,
851 							struct btrfs_ordered_sum,
852 							list);
853 				csum_root = btrfs_csum_root(fs_info,
854 							    sums->logical);
855 				if (!ret)
856 					ret = btrfs_del_csums(trans, csum_root,
857 							      sums->logical,
858 							      sums->len);
859 				if (!ret)
860 					ret = btrfs_csum_file_blocks(trans,
861 								     csum_root,
862 								     sums);
863 				list_del(&sums->list);
864 				kfree(sums);
865 			}
866 			if (ret)
867 				goto out;
868 		} else {
869 			btrfs_release_path(path);
870 		}
871 	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
872 		/* inline extents are easy, we just overwrite them */
873 		ret = overwrite_item(trans, root, path, eb, slot, key);
874 		if (ret)
875 			goto out;
876 	}
877 
878 	ret = btrfs_inode_set_file_extent_range(inode, start, extent_end - start);
879 	if (ret)
880 		goto out;
881 
882 update_inode:
883 	btrfs_update_inode_bytes(inode, nbytes, drop_args.bytes_found);
884 	ret = btrfs_update_inode(trans, inode);
885 out:
886 	iput(&inode->vfs_inode);
887 	return ret;
888 }
889 
unlink_inode_for_log_replay(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_inode * inode,const struct fscrypt_str * name)890 static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans,
891 				       struct btrfs_inode *dir,
892 				       struct btrfs_inode *inode,
893 				       const struct fscrypt_str *name)
894 {
895 	int ret;
896 
897 	ret = btrfs_unlink_inode(trans, dir, inode, name);
898 	if (ret)
899 		return ret;
900 	/*
901 	 * Whenever we need to check if a name exists or not, we check the
902 	 * fs/subvolume tree. So after an unlink we must run delayed items, so
903 	 * that future checks for a name during log replay see that the name
904 	 * does not exists anymore.
905 	 */
906 	return btrfs_run_delayed_items(trans);
907 }
908 
909 /*
910  * when cleaning up conflicts between the directory names in the
911  * subvolume, directory names in the log and directory names in the
912  * inode back references, we may have to unlink inodes from directories.
913  *
914  * This is a helper function to do the unlink of a specific directory
915  * item
916  */
drop_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_inode * dir,struct btrfs_dir_item * di)917 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
918 				      struct btrfs_path *path,
919 				      struct btrfs_inode *dir,
920 				      struct btrfs_dir_item *di)
921 {
922 	struct btrfs_root *root = dir->root;
923 	struct btrfs_inode *inode;
924 	struct fscrypt_str name;
925 	struct extent_buffer *leaf;
926 	struct btrfs_key location;
927 	int ret;
928 
929 	leaf = path->nodes[0];
930 
931 	btrfs_dir_item_key_to_cpu(leaf, di, &location);
932 	ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name);
933 	if (ret)
934 		return -ENOMEM;
935 
936 	btrfs_release_path(path);
937 
938 	inode = btrfs_iget_logging(location.objectid, root);
939 	if (IS_ERR(inode)) {
940 		ret = PTR_ERR(inode);
941 		inode = NULL;
942 		goto out;
943 	}
944 
945 	ret = link_to_fixup_dir(trans, root, path, location.objectid);
946 	if (ret)
947 		goto out;
948 
949 	ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
950 out:
951 	kfree(name.name);
952 	if (inode)
953 		iput(&inode->vfs_inode);
954 	return ret;
955 }
956 
957 /*
958  * See if a given name and sequence number found in an inode back reference are
959  * already in a directory and correctly point to this inode.
960  *
961  * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
962  * exists.
963  */
inode_in_dir(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 objectid,u64 index,struct fscrypt_str * name)964 static noinline int inode_in_dir(struct btrfs_root *root,
965 				 struct btrfs_path *path,
966 				 u64 dirid, u64 objectid, u64 index,
967 				 struct fscrypt_str *name)
968 {
969 	struct btrfs_dir_item *di;
970 	struct btrfs_key location;
971 	int ret = 0;
972 
973 	di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
974 					 index, name, 0);
975 	if (IS_ERR(di)) {
976 		ret = PTR_ERR(di);
977 		goto out;
978 	} else if (di) {
979 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
980 		if (location.objectid != objectid)
981 			goto out;
982 	} else {
983 		goto out;
984 	}
985 
986 	btrfs_release_path(path);
987 	di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0);
988 	if (IS_ERR(di)) {
989 		ret = PTR_ERR(di);
990 		goto out;
991 	} else if (di) {
992 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
993 		if (location.objectid == objectid)
994 			ret = 1;
995 	}
996 out:
997 	btrfs_release_path(path);
998 	return ret;
999 }
1000 
1001 /*
1002  * helper function to check a log tree for a named back reference in
1003  * an inode.  This is used to decide if a back reference that is
1004  * found in the subvolume conflicts with what we find in the log.
1005  *
1006  * inode backreferences may have multiple refs in a single item,
1007  * during replay we process one reference at a time, and we don't
1008  * want to delete valid links to a file from the subvolume if that
1009  * link is also in the log.
1010  */
backref_in_log(struct btrfs_root * log,struct btrfs_key * key,u64 ref_objectid,const struct fscrypt_str * name)1011 static noinline int backref_in_log(struct btrfs_root *log,
1012 				   struct btrfs_key *key,
1013 				   u64 ref_objectid,
1014 				   const struct fscrypt_str *name)
1015 {
1016 	struct btrfs_path *path;
1017 	int ret;
1018 
1019 	path = btrfs_alloc_path();
1020 	if (!path)
1021 		return -ENOMEM;
1022 
1023 	ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
1024 	if (ret < 0) {
1025 		goto out;
1026 	} else if (ret == 1) {
1027 		ret = 0;
1028 		goto out;
1029 	}
1030 
1031 	if (key->type == BTRFS_INODE_EXTREF_KEY)
1032 		ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1033 						       path->slots[0],
1034 						       ref_objectid, name);
1035 	else
1036 		ret = !!btrfs_find_name_in_backref(path->nodes[0],
1037 						   path->slots[0], name);
1038 out:
1039 	btrfs_free_path(path);
1040 	return ret;
1041 }
1042 
unlink_refs_not_in_log(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_root * log_root,struct btrfs_key * search_key,struct btrfs_inode * dir,struct btrfs_inode * inode,u64 parent_objectid)1043 static int unlink_refs_not_in_log(struct btrfs_trans_handle *trans,
1044 				  struct btrfs_path *path,
1045 				  struct btrfs_root *log_root,
1046 				  struct btrfs_key *search_key,
1047 				  struct btrfs_inode *dir,
1048 				  struct btrfs_inode *inode,
1049 				  u64 parent_objectid)
1050 {
1051 	struct extent_buffer *leaf = path->nodes[0];
1052 	unsigned long ptr;
1053 	unsigned long ptr_end;
1054 
1055 	/*
1056 	 * Check all the names in this back reference to see if they are in the
1057 	 * log. If so, we allow them to stay otherwise they must be unlinked as
1058 	 * a conflict.
1059 	 */
1060 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1061 	ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]);
1062 	while (ptr < ptr_end) {
1063 		struct fscrypt_str victim_name;
1064 		struct btrfs_inode_ref *victim_ref;
1065 		int ret;
1066 
1067 		victim_ref = (struct btrfs_inode_ref *)ptr;
1068 		ret = read_alloc_one_name(leaf, (victim_ref + 1),
1069 					  btrfs_inode_ref_name_len(leaf, victim_ref),
1070 					  &victim_name);
1071 		if (ret)
1072 			return ret;
1073 
1074 		ret = backref_in_log(log_root, search_key, parent_objectid, &victim_name);
1075 		if (ret) {
1076 			kfree(victim_name.name);
1077 			if (ret < 0)
1078 				return ret;
1079 			ptr = (unsigned long)(victim_ref + 1) + victim_name.len;
1080 			continue;
1081 		}
1082 
1083 		inc_nlink(&inode->vfs_inode);
1084 		btrfs_release_path(path);
1085 
1086 		ret = unlink_inode_for_log_replay(trans, dir, inode, &victim_name);
1087 		kfree(victim_name.name);
1088 		if (ret)
1089 			return ret;
1090 		return -EAGAIN;
1091 	}
1092 
1093 	return 0;
1094 }
1095 
unlink_extrefs_not_in_log(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_root * root,struct btrfs_root * log_root,struct btrfs_key * search_key,struct btrfs_inode * inode,u64 inode_objectid,u64 parent_objectid)1096 static int unlink_extrefs_not_in_log(struct btrfs_trans_handle *trans,
1097 				     struct btrfs_path *path,
1098 				     struct btrfs_root *root,
1099 				     struct btrfs_root *log_root,
1100 				     struct btrfs_key *search_key,
1101 				     struct btrfs_inode *inode,
1102 				     u64 inode_objectid,
1103 				     u64 parent_objectid)
1104 {
1105 	struct extent_buffer *leaf = path->nodes[0];
1106 	const unsigned long base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1107 	const u32 item_size = btrfs_item_size(leaf, path->slots[0]);
1108 	u32 cur_offset = 0;
1109 
1110 	while (cur_offset < item_size) {
1111 		struct btrfs_inode_extref *extref;
1112 		struct btrfs_inode *victim_parent;
1113 		struct fscrypt_str victim_name;
1114 		int ret;
1115 
1116 		extref = (struct btrfs_inode_extref *)(base + cur_offset);
1117 		victim_name.len = btrfs_inode_extref_name_len(leaf, extref);
1118 
1119 		if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1120 			goto next;
1121 
1122 		ret = read_alloc_one_name(leaf, &extref->name, victim_name.len,
1123 					  &victim_name);
1124 		if (ret)
1125 			return ret;
1126 
1127 		search_key->objectid = inode_objectid;
1128 		search_key->type = BTRFS_INODE_EXTREF_KEY;
1129 		search_key->offset = btrfs_extref_hash(parent_objectid,
1130 						       victim_name.name,
1131 						       victim_name.len);
1132 		ret = backref_in_log(log_root, search_key, parent_objectid, &victim_name);
1133 		if (ret) {
1134 			kfree(victim_name.name);
1135 			if (ret < 0)
1136 				return ret;
1137 next:
1138 			cur_offset += victim_name.len + sizeof(*extref);
1139 			continue;
1140 		}
1141 
1142 		victim_parent = btrfs_iget_logging(parent_objectid, root);
1143 		if (IS_ERR(victim_parent)) {
1144 			kfree(victim_name.name);
1145 			return PTR_ERR(victim_parent);
1146 		}
1147 
1148 		inc_nlink(&inode->vfs_inode);
1149 		btrfs_release_path(path);
1150 
1151 		ret = unlink_inode_for_log_replay(trans, victim_parent, inode,
1152 						  &victim_name);
1153 		iput(&victim_parent->vfs_inode);
1154 		kfree(victim_name.name);
1155 		if (ret)
1156 			return ret;
1157 		return -EAGAIN;
1158 	}
1159 
1160 	return 0;
1161 }
1162 
__add_inode_ref(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_root * log_root,struct btrfs_inode * dir,struct btrfs_inode * inode,u64 inode_objectid,u64 parent_objectid,u64 ref_index,struct fscrypt_str * name)1163 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
1164 				  struct btrfs_root *root,
1165 				  struct btrfs_path *path,
1166 				  struct btrfs_root *log_root,
1167 				  struct btrfs_inode *dir,
1168 				  struct btrfs_inode *inode,
1169 				  u64 inode_objectid, u64 parent_objectid,
1170 				  u64 ref_index, struct fscrypt_str *name)
1171 {
1172 	int ret;
1173 	struct btrfs_dir_item *di;
1174 	struct btrfs_key search_key;
1175 	struct btrfs_inode_extref *extref;
1176 
1177 again:
1178 	/* Search old style refs */
1179 	search_key.objectid = inode_objectid;
1180 	search_key.type = BTRFS_INODE_REF_KEY;
1181 	search_key.offset = parent_objectid;
1182 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1183 	if (ret < 0) {
1184 		return ret;
1185 	} else if (ret == 0) {
1186 		/*
1187 		 * Are we trying to overwrite a back ref for the root directory?
1188 		 * If so, we're done.
1189 		 */
1190 		if (search_key.objectid == search_key.offset)
1191 			return 1;
1192 
1193 		ret = unlink_refs_not_in_log(trans, path, log_root, &search_key,
1194 					     dir, inode, parent_objectid);
1195 		if (ret == -EAGAIN)
1196 			goto again;
1197 		else if (ret)
1198 			return ret;
1199 	}
1200 	btrfs_release_path(path);
1201 
1202 	/* Same search but for extended refs */
1203 	extref = btrfs_lookup_inode_extref(root, path, name, inode_objectid, parent_objectid);
1204 	if (IS_ERR(extref)) {
1205 		return PTR_ERR(extref);
1206 	} else if (extref) {
1207 		ret = unlink_extrefs_not_in_log(trans, path, root, log_root,
1208 						&search_key, inode,
1209 						inode_objectid, parent_objectid);
1210 		if (ret == -EAGAIN)
1211 			goto again;
1212 		else if (ret)
1213 			return ret;
1214 	}
1215 	btrfs_release_path(path);
1216 
1217 	/* look for a conflicting sequence number */
1218 	di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1219 					 ref_index, name, 0);
1220 	if (IS_ERR(di)) {
1221 		return PTR_ERR(di);
1222 	} else if (di) {
1223 		ret = drop_one_dir_item(trans, path, dir, di);
1224 		if (ret)
1225 			return ret;
1226 	}
1227 	btrfs_release_path(path);
1228 
1229 	/* look for a conflicting name */
1230 	di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0);
1231 	if (IS_ERR(di)) {
1232 		return PTR_ERR(di);
1233 	} else if (di) {
1234 		ret = drop_one_dir_item(trans, path, dir, di);
1235 		if (ret)
1236 			return ret;
1237 	}
1238 	btrfs_release_path(path);
1239 
1240 	return 0;
1241 }
1242 
extref_get_fields(struct extent_buffer * eb,unsigned long ref_ptr,struct fscrypt_str * name,u64 * index,u64 * parent_objectid)1243 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1244 			     struct fscrypt_str *name, u64 *index,
1245 			     u64 *parent_objectid)
1246 {
1247 	struct btrfs_inode_extref *extref;
1248 	int ret;
1249 
1250 	extref = (struct btrfs_inode_extref *)ref_ptr;
1251 
1252 	ret = read_alloc_one_name(eb, &extref->name,
1253 				  btrfs_inode_extref_name_len(eb, extref), name);
1254 	if (ret)
1255 		return ret;
1256 
1257 	if (index)
1258 		*index = btrfs_inode_extref_index(eb, extref);
1259 	if (parent_objectid)
1260 		*parent_objectid = btrfs_inode_extref_parent(eb, extref);
1261 
1262 	return 0;
1263 }
1264 
ref_get_fields(struct extent_buffer * eb,unsigned long ref_ptr,struct fscrypt_str * name,u64 * index)1265 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1266 			  struct fscrypt_str *name, u64 *index)
1267 {
1268 	struct btrfs_inode_ref *ref;
1269 	int ret;
1270 
1271 	ref = (struct btrfs_inode_ref *)ref_ptr;
1272 
1273 	ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref),
1274 				  name);
1275 	if (ret)
1276 		return ret;
1277 
1278 	if (index)
1279 		*index = btrfs_inode_ref_index(eb, ref);
1280 
1281 	return 0;
1282 }
1283 
1284 /*
1285  * Take an inode reference item from the log tree and iterate all names from the
1286  * inode reference item in the subvolume tree with the same key (if it exists).
1287  * For any name that is not in the inode reference item from the log tree, do a
1288  * proper unlink of that name (that is, remove its entry from the inode
1289  * reference item and both dir index keys).
1290  */
unlink_old_inode_refs(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_inode * inode,struct extent_buffer * log_eb,int log_slot,struct btrfs_key * key)1291 static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1292 				 struct btrfs_root *root,
1293 				 struct btrfs_path *path,
1294 				 struct btrfs_inode *inode,
1295 				 struct extent_buffer *log_eb,
1296 				 int log_slot,
1297 				 struct btrfs_key *key)
1298 {
1299 	int ret;
1300 	unsigned long ref_ptr;
1301 	unsigned long ref_end;
1302 	struct extent_buffer *eb;
1303 
1304 again:
1305 	btrfs_release_path(path);
1306 	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1307 	if (ret > 0) {
1308 		ret = 0;
1309 		goto out;
1310 	}
1311 	if (ret < 0)
1312 		goto out;
1313 
1314 	eb = path->nodes[0];
1315 	ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1316 	ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]);
1317 	while (ref_ptr < ref_end) {
1318 		struct fscrypt_str name;
1319 		u64 parent_id;
1320 
1321 		if (key->type == BTRFS_INODE_EXTREF_KEY) {
1322 			ret = extref_get_fields(eb, ref_ptr, &name,
1323 						NULL, &parent_id);
1324 		} else {
1325 			parent_id = key->offset;
1326 			ret = ref_get_fields(eb, ref_ptr, &name, NULL);
1327 		}
1328 		if (ret)
1329 			goto out;
1330 
1331 		if (key->type == BTRFS_INODE_EXTREF_KEY)
1332 			ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1333 							       parent_id, &name);
1334 		else
1335 			ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name);
1336 
1337 		if (!ret) {
1338 			struct btrfs_inode *dir;
1339 
1340 			btrfs_release_path(path);
1341 			dir = btrfs_iget_logging(parent_id, root);
1342 			if (IS_ERR(dir)) {
1343 				ret = PTR_ERR(dir);
1344 				kfree(name.name);
1345 				goto out;
1346 			}
1347 			ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
1348 			kfree(name.name);
1349 			iput(&dir->vfs_inode);
1350 			if (ret)
1351 				goto out;
1352 			goto again;
1353 		}
1354 
1355 		kfree(name.name);
1356 		ref_ptr += name.len;
1357 		if (key->type == BTRFS_INODE_EXTREF_KEY)
1358 			ref_ptr += sizeof(struct btrfs_inode_extref);
1359 		else
1360 			ref_ptr += sizeof(struct btrfs_inode_ref);
1361 	}
1362 	ret = 0;
1363  out:
1364 	btrfs_release_path(path);
1365 	return ret;
1366 }
1367 
1368 /*
1369  * replay one inode back reference item found in the log tree.
1370  * eb, slot and key refer to the buffer and key found in the log tree.
1371  * root is the destination we are replaying into, and path is for temp
1372  * use by this function.  (it should be released on return).
1373  */
add_inode_ref(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)1374 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1375 				  struct btrfs_root *root,
1376 				  struct btrfs_root *log,
1377 				  struct btrfs_path *path,
1378 				  struct extent_buffer *eb, int slot,
1379 				  struct btrfs_key *key)
1380 {
1381 	struct btrfs_inode *dir = NULL;
1382 	struct btrfs_inode *inode = NULL;
1383 	unsigned long ref_ptr;
1384 	unsigned long ref_end;
1385 	struct fscrypt_str name = { 0 };
1386 	int ret;
1387 	const bool is_extref_item = (key->type == BTRFS_INODE_EXTREF_KEY);
1388 	u64 parent_objectid;
1389 	u64 inode_objectid;
1390 	u64 ref_index = 0;
1391 	int ref_struct_size;
1392 
1393 	ref_ptr = btrfs_item_ptr_offset(eb, slot);
1394 	ref_end = ref_ptr + btrfs_item_size(eb, slot);
1395 
1396 	if (is_extref_item) {
1397 		struct btrfs_inode_extref *r;
1398 
1399 		ref_struct_size = sizeof(struct btrfs_inode_extref);
1400 		r = (struct btrfs_inode_extref *)ref_ptr;
1401 		parent_objectid = btrfs_inode_extref_parent(eb, r);
1402 	} else {
1403 		ref_struct_size = sizeof(struct btrfs_inode_ref);
1404 		parent_objectid = key->offset;
1405 	}
1406 	inode_objectid = key->objectid;
1407 
1408 	/*
1409 	 * it is possible that we didn't log all the parent directories
1410 	 * for a given inode.  If we don't find the dir, just don't
1411 	 * copy the back ref in.  The link count fixup code will take
1412 	 * care of the rest
1413 	 */
1414 	dir = btrfs_iget_logging(parent_objectid, root);
1415 	if (IS_ERR(dir)) {
1416 		ret = PTR_ERR(dir);
1417 		if (ret == -ENOENT)
1418 			ret = 0;
1419 		dir = NULL;
1420 		goto out;
1421 	}
1422 
1423 	inode = btrfs_iget_logging(inode_objectid, root);
1424 	if (IS_ERR(inode)) {
1425 		ret = PTR_ERR(inode);
1426 		inode = NULL;
1427 		goto out;
1428 	}
1429 
1430 	while (ref_ptr < ref_end) {
1431 		if (is_extref_item) {
1432 			ret = extref_get_fields(eb, ref_ptr, &name,
1433 						&ref_index, &parent_objectid);
1434 			if (ret)
1435 				goto out;
1436 			/*
1437 			 * parent object can change from one array
1438 			 * item to another.
1439 			 */
1440 			if (!dir) {
1441 				dir = btrfs_iget_logging(parent_objectid, root);
1442 				if (IS_ERR(dir)) {
1443 					ret = PTR_ERR(dir);
1444 					dir = NULL;
1445 					/*
1446 					 * A new parent dir may have not been
1447 					 * logged and not exist in the subvolume
1448 					 * tree, see the comment above before
1449 					 * the loop when getting the first
1450 					 * parent dir.
1451 					 */
1452 					if (ret == -ENOENT) {
1453 						/*
1454 						 * The next extref may refer to
1455 						 * another parent dir that
1456 						 * exists, so continue.
1457 						 */
1458 						ret = 0;
1459 						goto next;
1460 					}
1461 					goto out;
1462 				}
1463 			}
1464 		} else {
1465 			ret = ref_get_fields(eb, ref_ptr, &name, &ref_index);
1466 			if (ret)
1467 				goto out;
1468 		}
1469 
1470 		ret = inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
1471 				   ref_index, &name);
1472 		if (ret < 0) {
1473 			goto out;
1474 		} else if (ret == 0) {
1475 			/*
1476 			 * look for a conflicting back reference in the
1477 			 * metadata. if we find one we have to unlink that name
1478 			 * of the file before we add our new link.  Later on, we
1479 			 * overwrite any existing back reference, and we don't
1480 			 * want to create dangling pointers in the directory.
1481 			 */
1482 			ret = __add_inode_ref(trans, root, path, log, dir, inode,
1483 					      inode_objectid, parent_objectid,
1484 					      ref_index, &name);
1485 			if (ret) {
1486 				if (ret == 1)
1487 					ret = 0;
1488 				goto out;
1489 			}
1490 
1491 			/* insert our name */
1492 			ret = btrfs_add_link(trans, dir, inode, &name, 0, ref_index);
1493 			if (ret)
1494 				goto out;
1495 
1496 			ret = btrfs_update_inode(trans, inode);
1497 			if (ret)
1498 				goto out;
1499 		}
1500 		/* Else, ret == 1, we already have a perfect match, we're done. */
1501 
1502 next:
1503 		ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len;
1504 		kfree(name.name);
1505 		name.name = NULL;
1506 		if (is_extref_item && dir) {
1507 			iput(&dir->vfs_inode);
1508 			dir = NULL;
1509 		}
1510 	}
1511 
1512 	/*
1513 	 * Before we overwrite the inode reference item in the subvolume tree
1514 	 * with the item from the log tree, we must unlink all names from the
1515 	 * parent directory that are in the subvolume's tree inode reference
1516 	 * item, otherwise we end up with an inconsistent subvolume tree where
1517 	 * dir index entries exist for a name but there is no inode reference
1518 	 * item with the same name.
1519 	 */
1520 	ret = unlink_old_inode_refs(trans, root, path, inode, eb, slot, key);
1521 	if (ret)
1522 		goto out;
1523 
1524 	/* finally write the back reference in the inode */
1525 	ret = overwrite_item(trans, root, path, eb, slot, key);
1526 out:
1527 	btrfs_release_path(path);
1528 	kfree(name.name);
1529 	if (dir)
1530 		iput(&dir->vfs_inode);
1531 	if (inode)
1532 		iput(&inode->vfs_inode);
1533 	return ret;
1534 }
1535 
count_inode_extrefs(struct btrfs_inode * inode,struct btrfs_path * path)1536 static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
1537 {
1538 	int ret = 0;
1539 	int name_len;
1540 	unsigned int nlink = 0;
1541 	u32 item_size;
1542 	u32 cur_offset = 0;
1543 	u64 inode_objectid = btrfs_ino(inode);
1544 	u64 offset = 0;
1545 	unsigned long ptr;
1546 	struct btrfs_inode_extref *extref;
1547 	struct extent_buffer *leaf;
1548 
1549 	while (1) {
1550 		ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
1551 					    path, &extref, &offset);
1552 		if (ret)
1553 			break;
1554 
1555 		leaf = path->nodes[0];
1556 		item_size = btrfs_item_size(leaf, path->slots[0]);
1557 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1558 		cur_offset = 0;
1559 
1560 		while (cur_offset < item_size) {
1561 			extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1562 			name_len = btrfs_inode_extref_name_len(leaf, extref);
1563 
1564 			nlink++;
1565 
1566 			cur_offset += name_len + sizeof(*extref);
1567 		}
1568 
1569 		offset++;
1570 		btrfs_release_path(path);
1571 	}
1572 	btrfs_release_path(path);
1573 
1574 	if (ret < 0 && ret != -ENOENT)
1575 		return ret;
1576 	return nlink;
1577 }
1578 
count_inode_refs(struct btrfs_inode * inode,struct btrfs_path * path)1579 static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
1580 {
1581 	int ret;
1582 	struct btrfs_key key;
1583 	unsigned int nlink = 0;
1584 	unsigned long ptr;
1585 	unsigned long ptr_end;
1586 	int name_len;
1587 	u64 ino = btrfs_ino(inode);
1588 
1589 	key.objectid = ino;
1590 	key.type = BTRFS_INODE_REF_KEY;
1591 	key.offset = (u64)-1;
1592 
1593 	while (1) {
1594 		ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
1595 		if (ret < 0)
1596 			break;
1597 		if (ret > 0) {
1598 			if (path->slots[0] == 0)
1599 				break;
1600 			path->slots[0]--;
1601 		}
1602 process_slot:
1603 		btrfs_item_key_to_cpu(path->nodes[0], &key,
1604 				      path->slots[0]);
1605 		if (key.objectid != ino ||
1606 		    key.type != BTRFS_INODE_REF_KEY)
1607 			break;
1608 		ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1609 		ptr_end = ptr + btrfs_item_size(path->nodes[0],
1610 						   path->slots[0]);
1611 		while (ptr < ptr_end) {
1612 			struct btrfs_inode_ref *ref;
1613 
1614 			ref = (struct btrfs_inode_ref *)ptr;
1615 			name_len = btrfs_inode_ref_name_len(path->nodes[0],
1616 							    ref);
1617 			ptr = (unsigned long)(ref + 1) + name_len;
1618 			nlink++;
1619 		}
1620 
1621 		if (key.offset == 0)
1622 			break;
1623 		if (path->slots[0] > 0) {
1624 			path->slots[0]--;
1625 			goto process_slot;
1626 		}
1627 		key.offset--;
1628 		btrfs_release_path(path);
1629 	}
1630 	btrfs_release_path(path);
1631 
1632 	return nlink;
1633 }
1634 
1635 /*
1636  * There are a few corners where the link count of the file can't
1637  * be properly maintained during replay.  So, instead of adding
1638  * lots of complexity to the log code, we just scan the backrefs
1639  * for any file that has been through replay.
1640  *
1641  * The scan will update the link count on the inode to reflect the
1642  * number of back refs found.  If it goes down to zero, the iput
1643  * will free the inode.
1644  */
fixup_inode_link_count(struct btrfs_trans_handle * trans,struct btrfs_inode * inode)1645 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1646 					   struct btrfs_inode *inode)
1647 {
1648 	struct btrfs_root *root = inode->root;
1649 	struct btrfs_path *path;
1650 	int ret;
1651 	u64 nlink = 0;
1652 	const u64 ino = btrfs_ino(inode);
1653 
1654 	path = btrfs_alloc_path();
1655 	if (!path)
1656 		return -ENOMEM;
1657 
1658 	ret = count_inode_refs(inode, path);
1659 	if (ret < 0)
1660 		goto out;
1661 
1662 	nlink = ret;
1663 
1664 	ret = count_inode_extrefs(inode, path);
1665 	if (ret < 0)
1666 		goto out;
1667 
1668 	nlink += ret;
1669 
1670 	ret = 0;
1671 
1672 	if (nlink != inode->vfs_inode.i_nlink) {
1673 		set_nlink(&inode->vfs_inode, nlink);
1674 		ret = btrfs_update_inode(trans, inode);
1675 		if (ret)
1676 			goto out;
1677 	}
1678 	if (S_ISDIR(inode->vfs_inode.i_mode))
1679 		inode->index_cnt = (u64)-1;
1680 
1681 	if (inode->vfs_inode.i_nlink == 0) {
1682 		if (S_ISDIR(inode->vfs_inode.i_mode)) {
1683 			ret = replay_dir_deletes(trans, root, NULL, path, ino, true);
1684 			if (ret)
1685 				goto out;
1686 		}
1687 		ret = btrfs_insert_orphan_item(trans, root, ino);
1688 		if (ret == -EEXIST)
1689 			ret = 0;
1690 	}
1691 
1692 out:
1693 	btrfs_free_path(path);
1694 	return ret;
1695 }
1696 
fixup_inode_link_counts(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path)1697 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1698 					    struct btrfs_root *root,
1699 					    struct btrfs_path *path)
1700 {
1701 	int ret;
1702 	struct btrfs_key key;
1703 
1704 	key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1705 	key.type = BTRFS_ORPHAN_ITEM_KEY;
1706 	key.offset = (u64)-1;
1707 	while (1) {
1708 		struct btrfs_inode *inode;
1709 
1710 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1711 		if (ret < 0)
1712 			break;
1713 
1714 		if (ret == 1) {
1715 			ret = 0;
1716 			if (path->slots[0] == 0)
1717 				break;
1718 			path->slots[0]--;
1719 		}
1720 
1721 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1722 		if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1723 		    key.type != BTRFS_ORPHAN_ITEM_KEY)
1724 			break;
1725 
1726 		ret = btrfs_del_item(trans, root, path);
1727 		if (ret)
1728 			break;
1729 
1730 		btrfs_release_path(path);
1731 		inode = btrfs_iget_logging(key.offset, root);
1732 		if (IS_ERR(inode)) {
1733 			ret = PTR_ERR(inode);
1734 			break;
1735 		}
1736 
1737 		ret = fixup_inode_link_count(trans, inode);
1738 		iput(&inode->vfs_inode);
1739 		if (ret)
1740 			break;
1741 
1742 		/*
1743 		 * fixup on a directory may create new entries,
1744 		 * make sure we always look for the highset possible
1745 		 * offset
1746 		 */
1747 		key.offset = (u64)-1;
1748 	}
1749 	btrfs_release_path(path);
1750 	return ret;
1751 }
1752 
1753 
1754 /*
1755  * record a given inode in the fixup dir so we can check its link
1756  * count when replay is done.  The link count is incremented here
1757  * so the inode won't go away until we check it
1758  */
link_to_fixup_dir(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 objectid)1759 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1760 				      struct btrfs_root *root,
1761 				      struct btrfs_path *path,
1762 				      u64 objectid)
1763 {
1764 	struct btrfs_key key;
1765 	int ret = 0;
1766 	struct btrfs_inode *inode;
1767 	struct inode *vfs_inode;
1768 
1769 	inode = btrfs_iget_logging(objectid, root);
1770 	if (IS_ERR(inode))
1771 		return PTR_ERR(inode);
1772 
1773 	vfs_inode = &inode->vfs_inode;
1774 	key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1775 	key.type = BTRFS_ORPHAN_ITEM_KEY;
1776 	key.offset = objectid;
1777 
1778 	ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1779 
1780 	btrfs_release_path(path);
1781 	if (ret == 0) {
1782 		if (!vfs_inode->i_nlink)
1783 			set_nlink(vfs_inode, 1);
1784 		else
1785 			inc_nlink(vfs_inode);
1786 		ret = btrfs_update_inode(trans, inode);
1787 	} else if (ret == -EEXIST) {
1788 		ret = 0;
1789 	}
1790 	iput(vfs_inode);
1791 
1792 	return ret;
1793 }
1794 
1795 /*
1796  * when replaying the log for a directory, we only insert names
1797  * for inodes that actually exist.  This means an fsync on a directory
1798  * does not implicitly fsync all the new files in it
1799  */
insert_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 dirid,u64 index,const struct fscrypt_str * name,struct btrfs_key * location)1800 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1801 				    struct btrfs_root *root,
1802 				    u64 dirid, u64 index,
1803 				    const struct fscrypt_str *name,
1804 				    struct btrfs_key *location)
1805 {
1806 	struct btrfs_inode *inode;
1807 	struct btrfs_inode *dir;
1808 	int ret;
1809 
1810 	inode = btrfs_iget_logging(location->objectid, root);
1811 	if (IS_ERR(inode))
1812 		return PTR_ERR(inode);
1813 
1814 	dir = btrfs_iget_logging(dirid, root);
1815 	if (IS_ERR(dir)) {
1816 		iput(&inode->vfs_inode);
1817 		return PTR_ERR(dir);
1818 	}
1819 
1820 	ret = btrfs_add_link(trans, dir, inode, name, 1, index);
1821 
1822 	/* FIXME, put inode into FIXUP list */
1823 
1824 	iput(&inode->vfs_inode);
1825 	iput(&dir->vfs_inode);
1826 	return ret;
1827 }
1828 
delete_conflicting_dir_entry(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_path * path,struct btrfs_dir_item * dst_di,const struct btrfs_key * log_key,u8 log_flags,bool exists)1829 static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans,
1830 					struct btrfs_inode *dir,
1831 					struct btrfs_path *path,
1832 					struct btrfs_dir_item *dst_di,
1833 					const struct btrfs_key *log_key,
1834 					u8 log_flags,
1835 					bool exists)
1836 {
1837 	struct btrfs_key found_key;
1838 
1839 	btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1840 	/* The existing dentry points to the same inode, don't delete it. */
1841 	if (found_key.objectid == log_key->objectid &&
1842 	    found_key.type == log_key->type &&
1843 	    found_key.offset == log_key->offset &&
1844 	    btrfs_dir_flags(path->nodes[0], dst_di) == log_flags)
1845 		return 1;
1846 
1847 	/*
1848 	 * Don't drop the conflicting directory entry if the inode for the new
1849 	 * entry doesn't exist.
1850 	 */
1851 	if (!exists)
1852 		return 0;
1853 
1854 	return drop_one_dir_item(trans, path, dir, dst_di);
1855 }
1856 
1857 /*
1858  * take a single entry in a log directory item and replay it into
1859  * the subvolume.
1860  *
1861  * if a conflicting item exists in the subdirectory already,
1862  * the inode it points to is unlinked and put into the link count
1863  * fix up tree.
1864  *
1865  * If a name from the log points to a file or directory that does
1866  * not exist in the FS, it is skipped.  fsyncs on directories
1867  * do not force down inodes inside that directory, just changes to the
1868  * names or unlinks in a directory.
1869  *
1870  * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1871  * non-existing inode) and 1 if the name was replayed.
1872  */
replay_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,struct btrfs_dir_item * di,struct btrfs_key * key)1873 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1874 				    struct btrfs_root *root,
1875 				    struct btrfs_path *path,
1876 				    struct extent_buffer *eb,
1877 				    struct btrfs_dir_item *di,
1878 				    struct btrfs_key *key)
1879 {
1880 	struct fscrypt_str name = { 0 };
1881 	struct btrfs_dir_item *dir_dst_di;
1882 	struct btrfs_dir_item *index_dst_di;
1883 	bool dir_dst_matches = false;
1884 	bool index_dst_matches = false;
1885 	struct btrfs_key log_key;
1886 	struct btrfs_key search_key;
1887 	struct btrfs_inode *dir;
1888 	u8 log_flags;
1889 	bool exists;
1890 	int ret;
1891 	bool update_size = true;
1892 	bool name_added = false;
1893 
1894 	dir = btrfs_iget_logging(key->objectid, root);
1895 	if (IS_ERR(dir))
1896 		return PTR_ERR(dir);
1897 
1898 	ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
1899 	if (ret)
1900 		goto out;
1901 
1902 	log_flags = btrfs_dir_flags(eb, di);
1903 	btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1904 	ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1905 	btrfs_release_path(path);
1906 	if (ret < 0)
1907 		goto out;
1908 	exists = (ret == 0);
1909 	ret = 0;
1910 
1911 	dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1912 					   &name, 1);
1913 	if (IS_ERR(dir_dst_di)) {
1914 		ret = PTR_ERR(dir_dst_di);
1915 		goto out;
1916 	} else if (dir_dst_di) {
1917 		ret = delete_conflicting_dir_entry(trans, dir, path, dir_dst_di,
1918 						   &log_key, log_flags, exists);
1919 		if (ret < 0)
1920 			goto out;
1921 		dir_dst_matches = (ret == 1);
1922 	}
1923 
1924 	btrfs_release_path(path);
1925 
1926 	index_dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1927 						   key->objectid, key->offset,
1928 						   &name, 1);
1929 	if (IS_ERR(index_dst_di)) {
1930 		ret = PTR_ERR(index_dst_di);
1931 		goto out;
1932 	} else if (index_dst_di) {
1933 		ret = delete_conflicting_dir_entry(trans, dir, path, index_dst_di,
1934 						   &log_key, log_flags, exists);
1935 		if (ret < 0)
1936 			goto out;
1937 		index_dst_matches = (ret == 1);
1938 	}
1939 
1940 	btrfs_release_path(path);
1941 
1942 	if (dir_dst_matches && index_dst_matches) {
1943 		ret = 0;
1944 		update_size = false;
1945 		goto out;
1946 	}
1947 
1948 	/*
1949 	 * Check if the inode reference exists in the log for the given name,
1950 	 * inode and parent inode
1951 	 */
1952 	search_key.objectid = log_key.objectid;
1953 	search_key.type = BTRFS_INODE_REF_KEY;
1954 	search_key.offset = key->objectid;
1955 	ret = backref_in_log(root->log_root, &search_key, 0, &name);
1956 	if (ret < 0) {
1957 	        goto out;
1958 	} else if (ret) {
1959 	        /* The dentry will be added later. */
1960 	        ret = 0;
1961 	        update_size = false;
1962 	        goto out;
1963 	}
1964 
1965 	search_key.objectid = log_key.objectid;
1966 	search_key.type = BTRFS_INODE_EXTREF_KEY;
1967 	search_key.offset = key->objectid;
1968 	ret = backref_in_log(root->log_root, &search_key, key->objectid, &name);
1969 	if (ret < 0) {
1970 		goto out;
1971 	} else if (ret) {
1972 		/* The dentry will be added later. */
1973 		ret = 0;
1974 		update_size = false;
1975 		goto out;
1976 	}
1977 	btrfs_release_path(path);
1978 	ret = insert_one_name(trans, root, key->objectid, key->offset,
1979 			      &name, &log_key);
1980 	if (ret && ret != -ENOENT && ret != -EEXIST)
1981 		goto out;
1982 	if (!ret)
1983 		name_added = true;
1984 	update_size = false;
1985 	ret = 0;
1986 
1987 out:
1988 	if (!ret && update_size) {
1989 		btrfs_i_size_write(dir, dir->vfs_inode.i_size + name.len * 2);
1990 		ret = btrfs_update_inode(trans, dir);
1991 	}
1992 	kfree(name.name);
1993 	iput(&dir->vfs_inode);
1994 	if (!ret && name_added)
1995 		ret = 1;
1996 	return ret;
1997 }
1998 
1999 /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */
replay_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)2000 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2001 					struct btrfs_root *root,
2002 					struct btrfs_path *path,
2003 					struct extent_buffer *eb, int slot,
2004 					struct btrfs_key *key)
2005 {
2006 	int ret;
2007 	struct btrfs_dir_item *di;
2008 
2009 	/* We only log dir index keys, which only contain a single dir item. */
2010 	ASSERT(key->type == BTRFS_DIR_INDEX_KEY);
2011 
2012 	di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2013 	ret = replay_one_name(trans, root, path, eb, di, key);
2014 	if (ret < 0)
2015 		return ret;
2016 
2017 	/*
2018 	 * If this entry refers to a non-directory (directories can not have a
2019 	 * link count > 1) and it was added in the transaction that was not
2020 	 * committed, make sure we fixup the link count of the inode the entry
2021 	 * points to. Otherwise something like the following would result in a
2022 	 * directory pointing to an inode with a wrong link that does not account
2023 	 * for this dir entry:
2024 	 *
2025 	 * mkdir testdir
2026 	 * touch testdir/foo
2027 	 * touch testdir/bar
2028 	 * sync
2029 	 *
2030 	 * ln testdir/bar testdir/bar_link
2031 	 * ln testdir/foo testdir/foo_link
2032 	 * xfs_io -c "fsync" testdir/bar
2033 	 *
2034 	 * <power failure>
2035 	 *
2036 	 * mount fs, log replay happens
2037 	 *
2038 	 * File foo would remain with a link count of 1 when it has two entries
2039 	 * pointing to it in the directory testdir. This would make it impossible
2040 	 * to ever delete the parent directory has it would result in stale
2041 	 * dentries that can never be deleted.
2042 	 */
2043 	if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) {
2044 		struct btrfs_path *fixup_path;
2045 		struct btrfs_key di_key;
2046 
2047 		fixup_path = btrfs_alloc_path();
2048 		if (!fixup_path)
2049 			return -ENOMEM;
2050 
2051 		btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2052 		ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid);
2053 		btrfs_free_path(fixup_path);
2054 	}
2055 
2056 	return ret;
2057 }
2058 
2059 /*
2060  * directory replay has two parts.  There are the standard directory
2061  * items in the log copied from the subvolume, and range items
2062  * created in the log while the subvolume was logged.
2063  *
2064  * The range items tell us which parts of the key space the log
2065  * is authoritative for.  During replay, if a key in the subvolume
2066  * directory is in a logged range item, but not actually in the log
2067  * that means it was deleted from the directory before the fsync
2068  * and should be removed.
2069  */
find_dir_range(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 * start_ret,u64 * end_ret)2070 static noinline int find_dir_range(struct btrfs_root *root,
2071 				   struct btrfs_path *path,
2072 				   u64 dirid,
2073 				   u64 *start_ret, u64 *end_ret)
2074 {
2075 	struct btrfs_key key;
2076 	u64 found_end;
2077 	struct btrfs_dir_log_item *item;
2078 	int ret;
2079 	int nritems;
2080 
2081 	if (*start_ret == (u64)-1)
2082 		return 1;
2083 
2084 	key.objectid = dirid;
2085 	key.type = BTRFS_DIR_LOG_INDEX_KEY;
2086 	key.offset = *start_ret;
2087 
2088 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2089 	if (ret < 0)
2090 		goto out;
2091 	if (ret > 0) {
2092 		if (path->slots[0] == 0)
2093 			goto out;
2094 		path->slots[0]--;
2095 	}
2096 	if (ret != 0)
2097 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2098 
2099 	if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
2100 		ret = 1;
2101 		goto next;
2102 	}
2103 	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2104 			      struct btrfs_dir_log_item);
2105 	found_end = btrfs_dir_log_end(path->nodes[0], item);
2106 
2107 	if (*start_ret >= key.offset && *start_ret <= found_end) {
2108 		ret = 0;
2109 		*start_ret = key.offset;
2110 		*end_ret = found_end;
2111 		goto out;
2112 	}
2113 	ret = 1;
2114 next:
2115 	/* check the next slot in the tree to see if it is a valid item */
2116 	nritems = btrfs_header_nritems(path->nodes[0]);
2117 	path->slots[0]++;
2118 	if (path->slots[0] >= nritems) {
2119 		ret = btrfs_next_leaf(root, path);
2120 		if (ret)
2121 			goto out;
2122 	}
2123 
2124 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2125 
2126 	if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
2127 		ret = 1;
2128 		goto out;
2129 	}
2130 	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2131 			      struct btrfs_dir_log_item);
2132 	found_end = btrfs_dir_log_end(path->nodes[0], item);
2133 	*start_ret = key.offset;
2134 	*end_ret = found_end;
2135 	ret = 0;
2136 out:
2137 	btrfs_release_path(path);
2138 	return ret;
2139 }
2140 
2141 /*
2142  * this looks for a given directory item in the log.  If the directory
2143  * item is not in the log, the item is removed and the inode it points
2144  * to is unlinked
2145  */
check_item_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_path * log_path,struct btrfs_inode * dir,struct btrfs_key * dir_key)2146 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2147 				      struct btrfs_root *log,
2148 				      struct btrfs_path *path,
2149 				      struct btrfs_path *log_path,
2150 				      struct btrfs_inode *dir,
2151 				      struct btrfs_key *dir_key)
2152 {
2153 	struct btrfs_root *root = dir->root;
2154 	int ret;
2155 	struct extent_buffer *eb;
2156 	int slot;
2157 	struct btrfs_dir_item *di;
2158 	struct fscrypt_str name = { 0 };
2159 	struct btrfs_inode *inode = NULL;
2160 	struct btrfs_key location;
2161 
2162 	/*
2163 	 * Currently we only log dir index keys. Even if we replay a log created
2164 	 * by an older kernel that logged both dir index and dir item keys, all
2165 	 * we need to do is process the dir index keys, we (and our caller) can
2166 	 * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY).
2167 	 */
2168 	ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY);
2169 
2170 	eb = path->nodes[0];
2171 	slot = path->slots[0];
2172 	di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2173 	ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
2174 	if (ret)
2175 		goto out;
2176 
2177 	if (log) {
2178 		struct btrfs_dir_item *log_di;
2179 
2180 		log_di = btrfs_lookup_dir_index_item(trans, log, log_path,
2181 						     dir_key->objectid,
2182 						     dir_key->offset, &name, 0);
2183 		if (IS_ERR(log_di)) {
2184 			ret = PTR_ERR(log_di);
2185 			goto out;
2186 		} else if (log_di) {
2187 			/* The dentry exists in the log, we have nothing to do. */
2188 			ret = 0;
2189 			goto out;
2190 		}
2191 	}
2192 
2193 	btrfs_dir_item_key_to_cpu(eb, di, &location);
2194 	btrfs_release_path(path);
2195 	btrfs_release_path(log_path);
2196 	inode = btrfs_iget_logging(location.objectid, root);
2197 	if (IS_ERR(inode)) {
2198 		ret = PTR_ERR(inode);
2199 		inode = NULL;
2200 		goto out;
2201 	}
2202 
2203 	ret = link_to_fixup_dir(trans, root, path, location.objectid);
2204 	if (ret)
2205 		goto out;
2206 
2207 	inc_nlink(&inode->vfs_inode);
2208 	ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
2209 	/*
2210 	 * Unlike dir item keys, dir index keys can only have one name (entry) in
2211 	 * them, as there are no key collisions since each key has a unique offset
2212 	 * (an index number), so we're done.
2213 	 */
2214 out:
2215 	btrfs_release_path(path);
2216 	btrfs_release_path(log_path);
2217 	kfree(name.name);
2218 	if (inode)
2219 		iput(&inode->vfs_inode);
2220 	return ret;
2221 }
2222 
replay_xattr_deletes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,const u64 ino)2223 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2224 			      struct btrfs_root *root,
2225 			      struct btrfs_root *log,
2226 			      struct btrfs_path *path,
2227 			      const u64 ino)
2228 {
2229 	struct btrfs_key search_key;
2230 	struct btrfs_path *log_path;
2231 	int i;
2232 	int nritems;
2233 	int ret;
2234 
2235 	log_path = btrfs_alloc_path();
2236 	if (!log_path)
2237 		return -ENOMEM;
2238 
2239 	search_key.objectid = ino;
2240 	search_key.type = BTRFS_XATTR_ITEM_KEY;
2241 	search_key.offset = 0;
2242 again:
2243 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2244 	if (ret < 0)
2245 		goto out;
2246 process_leaf:
2247 	nritems = btrfs_header_nritems(path->nodes[0]);
2248 	for (i = path->slots[0]; i < nritems; i++) {
2249 		struct btrfs_key key;
2250 		struct btrfs_dir_item *di;
2251 		struct btrfs_dir_item *log_di;
2252 		u32 total_size;
2253 		u32 cur;
2254 
2255 		btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2256 		if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2257 			ret = 0;
2258 			goto out;
2259 		}
2260 
2261 		di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2262 		total_size = btrfs_item_size(path->nodes[0], i);
2263 		cur = 0;
2264 		while (cur < total_size) {
2265 			u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2266 			u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2267 			u32 this_len = sizeof(*di) + name_len + data_len;
2268 			char *name;
2269 
2270 			name = kmalloc(name_len, GFP_NOFS);
2271 			if (!name) {
2272 				ret = -ENOMEM;
2273 				goto out;
2274 			}
2275 			read_extent_buffer(path->nodes[0], name,
2276 					   (unsigned long)(di + 1), name_len);
2277 
2278 			log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2279 						    name, name_len, 0);
2280 			btrfs_release_path(log_path);
2281 			if (!log_di) {
2282 				/* Doesn't exist in log tree, so delete it. */
2283 				btrfs_release_path(path);
2284 				di = btrfs_lookup_xattr(trans, root, path, ino,
2285 							name, name_len, -1);
2286 				kfree(name);
2287 				if (IS_ERR(di)) {
2288 					ret = PTR_ERR(di);
2289 					goto out;
2290 				}
2291 				ASSERT(di);
2292 				ret = btrfs_delete_one_dir_name(trans, root,
2293 								path, di);
2294 				if (ret)
2295 					goto out;
2296 				btrfs_release_path(path);
2297 				search_key = key;
2298 				goto again;
2299 			}
2300 			kfree(name);
2301 			if (IS_ERR(log_di)) {
2302 				ret = PTR_ERR(log_di);
2303 				goto out;
2304 			}
2305 			cur += this_len;
2306 			di = (struct btrfs_dir_item *)((char *)di + this_len);
2307 		}
2308 	}
2309 	ret = btrfs_next_leaf(root, path);
2310 	if (ret > 0)
2311 		ret = 0;
2312 	else if (ret == 0)
2313 		goto process_leaf;
2314 out:
2315 	btrfs_free_path(log_path);
2316 	btrfs_release_path(path);
2317 	return ret;
2318 }
2319 
2320 
2321 /*
2322  * deletion replay happens before we copy any new directory items
2323  * out of the log or out of backreferences from inodes.  It
2324  * scans the log to find ranges of keys that log is authoritative for,
2325  * and then scans the directory to find items in those ranges that are
2326  * not present in the log.
2327  *
2328  * Anything we don't find in the log is unlinked and removed from the
2329  * directory.
2330  */
replay_dir_deletes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,u64 dirid,bool del_all)2331 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2332 				       struct btrfs_root *root,
2333 				       struct btrfs_root *log,
2334 				       struct btrfs_path *path,
2335 				       u64 dirid, bool del_all)
2336 {
2337 	u64 range_start;
2338 	u64 range_end;
2339 	int ret = 0;
2340 	struct btrfs_key dir_key;
2341 	struct btrfs_key found_key;
2342 	struct btrfs_path *log_path;
2343 	struct btrfs_inode *dir;
2344 
2345 	dir_key.objectid = dirid;
2346 	dir_key.type = BTRFS_DIR_INDEX_KEY;
2347 	log_path = btrfs_alloc_path();
2348 	if (!log_path)
2349 		return -ENOMEM;
2350 
2351 	dir = btrfs_iget_logging(dirid, root);
2352 	/*
2353 	 * It isn't an error if the inode isn't there, that can happen because
2354 	 * we replay the deletes before we copy in the inode item from the log.
2355 	 */
2356 	if (IS_ERR(dir)) {
2357 		btrfs_free_path(log_path);
2358 		ret = PTR_ERR(dir);
2359 		if (ret == -ENOENT)
2360 			ret = 0;
2361 		return ret;
2362 	}
2363 
2364 	range_start = 0;
2365 	range_end = 0;
2366 	while (1) {
2367 		if (del_all)
2368 			range_end = (u64)-1;
2369 		else {
2370 			ret = find_dir_range(log, path, dirid,
2371 					     &range_start, &range_end);
2372 			if (ret < 0)
2373 				goto out;
2374 			else if (ret > 0)
2375 				break;
2376 		}
2377 
2378 		dir_key.offset = range_start;
2379 		while (1) {
2380 			int nritems;
2381 			ret = btrfs_search_slot(NULL, root, &dir_key, path,
2382 						0, 0);
2383 			if (ret < 0)
2384 				goto out;
2385 
2386 			nritems = btrfs_header_nritems(path->nodes[0]);
2387 			if (path->slots[0] >= nritems) {
2388 				ret = btrfs_next_leaf(root, path);
2389 				if (ret == 1)
2390 					break;
2391 				else if (ret < 0)
2392 					goto out;
2393 			}
2394 			btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2395 					      path->slots[0]);
2396 			if (found_key.objectid != dirid ||
2397 			    found_key.type != dir_key.type) {
2398 				ret = 0;
2399 				goto out;
2400 			}
2401 
2402 			if (found_key.offset > range_end)
2403 				break;
2404 
2405 			ret = check_item_in_log(trans, log, path,
2406 						log_path, dir,
2407 						&found_key);
2408 			if (ret)
2409 				goto out;
2410 			if (found_key.offset == (u64)-1)
2411 				break;
2412 			dir_key.offset = found_key.offset + 1;
2413 		}
2414 		btrfs_release_path(path);
2415 		if (range_end == (u64)-1)
2416 			break;
2417 		range_start = range_end + 1;
2418 	}
2419 	ret = 0;
2420 out:
2421 	btrfs_release_path(path);
2422 	btrfs_free_path(log_path);
2423 	iput(&dir->vfs_inode);
2424 	return ret;
2425 }
2426 
2427 /*
2428  * the process_func used to replay items from the log tree.  This
2429  * gets called in two different stages.  The first stage just looks
2430  * for inodes and makes sure they are all copied into the subvolume.
2431  *
2432  * The second stage copies all the other item types from the log into
2433  * the subvolume.  The two stage approach is slower, but gets rid of
2434  * lots of complexity around inodes referencing other inodes that exist
2435  * only in the log (references come from either directory items or inode
2436  * back refs).
2437  */
replay_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen,int level)2438 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2439 			     struct walk_control *wc, u64 gen, int level)
2440 {
2441 	int nritems;
2442 	struct btrfs_tree_parent_check check = {
2443 		.transid = gen,
2444 		.level = level
2445 	};
2446 	struct btrfs_path *path;
2447 	struct btrfs_root *root = wc->replay_dest;
2448 	struct btrfs_key key;
2449 	int i;
2450 	int ret;
2451 
2452 	ret = btrfs_read_extent_buffer(eb, &check);
2453 	if (ret)
2454 		return ret;
2455 
2456 	level = btrfs_header_level(eb);
2457 
2458 	if (level != 0)
2459 		return 0;
2460 
2461 	path = btrfs_alloc_path();
2462 	if (!path)
2463 		return -ENOMEM;
2464 
2465 	nritems = btrfs_header_nritems(eb);
2466 	for (i = 0; i < nritems; i++) {
2467 		struct btrfs_inode_item *inode_item;
2468 
2469 		btrfs_item_key_to_cpu(eb, &key, i);
2470 
2471 		if (key.type == BTRFS_INODE_ITEM_KEY) {
2472 			inode_item = btrfs_item_ptr(eb, i, struct btrfs_inode_item);
2473 			/*
2474 			 * An inode with no links is either:
2475 			 *
2476 			 * 1) A tmpfile (O_TMPFILE) that got fsync'ed and never
2477 			 *    got linked before the fsync, skip it, as replaying
2478 			 *    it is pointless since it would be deleted later.
2479 			 *    We skip logging tmpfiles, but it's always possible
2480 			 *    we are replaying a log created with a kernel that
2481 			 *    used to log tmpfiles;
2482 			 *
2483 			 * 2) A non-tmpfile which got its last link deleted
2484 			 *    while holding an open fd on it and later got
2485 			 *    fsynced through that fd. We always log the
2486 			 *    parent inodes when inode->last_unlink_trans is
2487 			 *    set to the current transaction, so ignore all the
2488 			 *    inode items for this inode. We will delete the
2489 			 *    inode when processing the parent directory with
2490 			 *    replay_dir_deletes().
2491 			 */
2492 			if (btrfs_inode_nlink(eb, inode_item) == 0) {
2493 				wc->ignore_cur_inode = true;
2494 				continue;
2495 			} else {
2496 				wc->ignore_cur_inode = false;
2497 			}
2498 		}
2499 
2500 		/* Inode keys are done during the first stage. */
2501 		if (key.type == BTRFS_INODE_ITEM_KEY &&
2502 		    wc->stage == LOG_WALK_REPLAY_INODES) {
2503 			u32 mode;
2504 
2505 			ret = replay_xattr_deletes(wc->trans, root, log, path, key.objectid);
2506 			if (ret)
2507 				break;
2508 			mode = btrfs_inode_mode(eb, inode_item);
2509 			if (S_ISDIR(mode)) {
2510 				ret = replay_dir_deletes(wc->trans, root, log, path,
2511 							 key.objectid, false);
2512 				if (ret)
2513 					break;
2514 			}
2515 			ret = overwrite_item(wc->trans, root, path,
2516 					     eb, i, &key);
2517 			if (ret)
2518 				break;
2519 
2520 			/*
2521 			 * Before replaying extents, truncate the inode to its
2522 			 * size. We need to do it now and not after log replay
2523 			 * because before an fsync we can have prealloc extents
2524 			 * added beyond the inode's i_size. If we did it after,
2525 			 * through orphan cleanup for example, we would drop
2526 			 * those prealloc extents just after replaying them.
2527 			 */
2528 			if (S_ISREG(mode)) {
2529 				struct btrfs_drop_extents_args drop_args = { 0 };
2530 				struct btrfs_inode *inode;
2531 				u64 from;
2532 
2533 				inode = btrfs_iget_logging(key.objectid, root);
2534 				if (IS_ERR(inode)) {
2535 					ret = PTR_ERR(inode);
2536 					break;
2537 				}
2538 				from = ALIGN(i_size_read(&inode->vfs_inode),
2539 					     root->fs_info->sectorsize);
2540 				drop_args.start = from;
2541 				drop_args.end = (u64)-1;
2542 				drop_args.drop_cache = true;
2543 				ret = btrfs_drop_extents(wc->trans, root, inode,
2544 							 &drop_args);
2545 				if (!ret) {
2546 					inode_sub_bytes(&inode->vfs_inode,
2547 							drop_args.bytes_found);
2548 					/* Update the inode's nbytes. */
2549 					ret = btrfs_update_inode(wc->trans, inode);
2550 				}
2551 				iput(&inode->vfs_inode);
2552 				if (ret)
2553 					break;
2554 			}
2555 
2556 			ret = link_to_fixup_dir(wc->trans, root,
2557 						path, key.objectid);
2558 			if (ret)
2559 				break;
2560 		}
2561 
2562 		if (wc->ignore_cur_inode)
2563 			continue;
2564 
2565 		if (key.type == BTRFS_DIR_INDEX_KEY &&
2566 		    wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2567 			ret = replay_one_dir_item(wc->trans, root, path,
2568 						  eb, i, &key);
2569 			if (ret)
2570 				break;
2571 		}
2572 
2573 		if (wc->stage < LOG_WALK_REPLAY_ALL)
2574 			continue;
2575 
2576 		/* these keys are simply copied */
2577 		if (key.type == BTRFS_XATTR_ITEM_KEY) {
2578 			ret = overwrite_item(wc->trans, root, path,
2579 					     eb, i, &key);
2580 			if (ret)
2581 				break;
2582 		} else if (key.type == BTRFS_INODE_REF_KEY ||
2583 			   key.type == BTRFS_INODE_EXTREF_KEY) {
2584 			ret = add_inode_ref(wc->trans, root, log, path,
2585 					    eb, i, &key);
2586 			if (ret)
2587 				break;
2588 		} else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2589 			ret = replay_one_extent(wc->trans, root, path,
2590 						eb, i, &key);
2591 			if (ret)
2592 				break;
2593 		}
2594 		/*
2595 		 * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the
2596 		 * BTRFS_DIR_INDEX_KEY items which we use to derive the
2597 		 * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an
2598 		 * older kernel with such keys, ignore them.
2599 		 */
2600 	}
2601 	btrfs_free_path(path);
2602 	return ret;
2603 }
2604 
2605 /*
2606  * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2607  */
unaccount_log_buffer(struct btrfs_fs_info * fs_info,u64 start)2608 static int unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2609 {
2610 	struct btrfs_block_group *cache;
2611 
2612 	cache = btrfs_lookup_block_group(fs_info, start);
2613 	if (!cache) {
2614 		btrfs_err(fs_info, "unable to find block group for %llu", start);
2615 		return -ENOENT;
2616 	}
2617 
2618 	spin_lock(&cache->space_info->lock);
2619 	spin_lock(&cache->lock);
2620 	cache->reserved -= fs_info->nodesize;
2621 	cache->space_info->bytes_reserved -= fs_info->nodesize;
2622 	spin_unlock(&cache->lock);
2623 	spin_unlock(&cache->space_info->lock);
2624 
2625 	btrfs_put_block_group(cache);
2626 
2627 	return 0;
2628 }
2629 
clean_log_buffer(struct btrfs_trans_handle * trans,struct extent_buffer * eb)2630 static int clean_log_buffer(struct btrfs_trans_handle *trans,
2631 			    struct extent_buffer *eb)
2632 {
2633 	btrfs_tree_lock(eb);
2634 	btrfs_clear_buffer_dirty(trans, eb);
2635 	wait_on_extent_buffer_writeback(eb);
2636 	btrfs_tree_unlock(eb);
2637 
2638 	if (trans)
2639 		return btrfs_pin_reserved_extent(trans, eb);
2640 
2641 	return unaccount_log_buffer(eb->fs_info, eb->start);
2642 }
2643 
walk_down_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)2644 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2645 				   struct btrfs_root *root,
2646 				   struct btrfs_path *path, int *level,
2647 				   struct walk_control *wc)
2648 {
2649 	struct btrfs_fs_info *fs_info = root->fs_info;
2650 	u64 bytenr;
2651 	u64 ptr_gen;
2652 	struct extent_buffer *next;
2653 	struct extent_buffer *cur;
2654 	int ret = 0;
2655 
2656 	while (*level > 0) {
2657 		struct btrfs_tree_parent_check check = { 0 };
2658 
2659 		cur = path->nodes[*level];
2660 
2661 		WARN_ON(btrfs_header_level(cur) != *level);
2662 
2663 		if (path->slots[*level] >=
2664 		    btrfs_header_nritems(cur))
2665 			break;
2666 
2667 		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2668 		ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2669 		check.transid = ptr_gen;
2670 		check.level = *level - 1;
2671 		check.has_first_key = true;
2672 		btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
2673 
2674 		next = btrfs_find_create_tree_block(fs_info, bytenr,
2675 						    btrfs_header_owner(cur),
2676 						    *level - 1);
2677 		if (IS_ERR(next))
2678 			return PTR_ERR(next);
2679 
2680 		if (*level == 1) {
2681 			ret = wc->process_func(root, next, wc, ptr_gen,
2682 					       *level - 1);
2683 			if (ret) {
2684 				free_extent_buffer(next);
2685 				return ret;
2686 			}
2687 
2688 			path->slots[*level]++;
2689 			if (wc->free) {
2690 				ret = btrfs_read_extent_buffer(next, &check);
2691 				if (ret) {
2692 					free_extent_buffer(next);
2693 					return ret;
2694 				}
2695 
2696 				ret = clean_log_buffer(trans, next);
2697 				if (ret) {
2698 					free_extent_buffer(next);
2699 					return ret;
2700 				}
2701 			}
2702 			free_extent_buffer(next);
2703 			continue;
2704 		}
2705 		ret = btrfs_read_extent_buffer(next, &check);
2706 		if (ret) {
2707 			free_extent_buffer(next);
2708 			return ret;
2709 		}
2710 
2711 		if (path->nodes[*level-1])
2712 			free_extent_buffer(path->nodes[*level-1]);
2713 		path->nodes[*level-1] = next;
2714 		*level = btrfs_header_level(next);
2715 		path->slots[*level] = 0;
2716 		cond_resched();
2717 	}
2718 	path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2719 
2720 	cond_resched();
2721 	return 0;
2722 }
2723 
walk_up_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)2724 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2725 				 struct btrfs_root *root,
2726 				 struct btrfs_path *path, int *level,
2727 				 struct walk_control *wc)
2728 {
2729 	int i;
2730 	int slot;
2731 	int ret;
2732 
2733 	for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2734 		slot = path->slots[i];
2735 		if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2736 			path->slots[i]++;
2737 			*level = i;
2738 			WARN_ON(*level == 0);
2739 			return 0;
2740 		} else {
2741 			ret = wc->process_func(root, path->nodes[*level], wc,
2742 				 btrfs_header_generation(path->nodes[*level]),
2743 				 *level);
2744 			if (ret)
2745 				return ret;
2746 
2747 			if (wc->free) {
2748 				ret = clean_log_buffer(trans, path->nodes[*level]);
2749 				if (ret)
2750 					return ret;
2751 			}
2752 			free_extent_buffer(path->nodes[*level]);
2753 			path->nodes[*level] = NULL;
2754 			*level = i + 1;
2755 		}
2756 	}
2757 	return 1;
2758 }
2759 
2760 /*
2761  * drop the reference count on the tree rooted at 'snap'.  This traverses
2762  * the tree freeing any blocks that have a ref count of zero after being
2763  * decremented.
2764  */
walk_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct walk_control * wc)2765 static int walk_log_tree(struct btrfs_trans_handle *trans,
2766 			 struct btrfs_root *log, struct walk_control *wc)
2767 {
2768 	int ret = 0;
2769 	int wret;
2770 	int level;
2771 	struct btrfs_path *path;
2772 	int orig_level;
2773 
2774 	path = btrfs_alloc_path();
2775 	if (!path)
2776 		return -ENOMEM;
2777 
2778 	level = btrfs_header_level(log->node);
2779 	orig_level = level;
2780 	path->nodes[level] = log->node;
2781 	refcount_inc(&log->node->refs);
2782 	path->slots[level] = 0;
2783 
2784 	while (1) {
2785 		wret = walk_down_log_tree(trans, log, path, &level, wc);
2786 		if (wret > 0)
2787 			break;
2788 		if (wret < 0) {
2789 			ret = wret;
2790 			goto out;
2791 		}
2792 
2793 		wret = walk_up_log_tree(trans, log, path, &level, wc);
2794 		if (wret > 0)
2795 			break;
2796 		if (wret < 0) {
2797 			ret = wret;
2798 			goto out;
2799 		}
2800 	}
2801 
2802 	/* was the root node processed? if not, catch it here */
2803 	if (path->nodes[orig_level]) {
2804 		ret = wc->process_func(log, path->nodes[orig_level], wc,
2805 			 btrfs_header_generation(path->nodes[orig_level]),
2806 			 orig_level);
2807 		if (ret)
2808 			goto out;
2809 		if (wc->free)
2810 			ret = clean_log_buffer(trans, path->nodes[orig_level]);
2811 	}
2812 
2813 out:
2814 	btrfs_free_path(path);
2815 	return ret;
2816 }
2817 
2818 /*
2819  * helper function to update the item for a given subvolumes log root
2820  * in the tree of log roots
2821  */
update_log_root(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_root_item * root_item)2822 static int update_log_root(struct btrfs_trans_handle *trans,
2823 			   struct btrfs_root *log,
2824 			   struct btrfs_root_item *root_item)
2825 {
2826 	struct btrfs_fs_info *fs_info = log->fs_info;
2827 	int ret;
2828 
2829 	if (log->log_transid == 1) {
2830 		/* insert root item on the first sync */
2831 		ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2832 				&log->root_key, root_item);
2833 	} else {
2834 		ret = btrfs_update_root(trans, fs_info->log_root_tree,
2835 				&log->root_key, root_item);
2836 	}
2837 	return ret;
2838 }
2839 
wait_log_commit(struct btrfs_root * root,int transid)2840 static void wait_log_commit(struct btrfs_root *root, int transid)
2841 {
2842 	DEFINE_WAIT(wait);
2843 	int index = transid % 2;
2844 
2845 	/*
2846 	 * we only allow two pending log transactions at a time,
2847 	 * so we know that if ours is more than 2 older than the
2848 	 * current transaction, we're done
2849 	 */
2850 	for (;;) {
2851 		prepare_to_wait(&root->log_commit_wait[index],
2852 				&wait, TASK_UNINTERRUPTIBLE);
2853 
2854 		if (!(root->log_transid_committed < transid &&
2855 		      atomic_read(&root->log_commit[index])))
2856 			break;
2857 
2858 		mutex_unlock(&root->log_mutex);
2859 		schedule();
2860 		mutex_lock(&root->log_mutex);
2861 	}
2862 	finish_wait(&root->log_commit_wait[index], &wait);
2863 }
2864 
wait_for_writer(struct btrfs_root * root)2865 static void wait_for_writer(struct btrfs_root *root)
2866 {
2867 	DEFINE_WAIT(wait);
2868 
2869 	for (;;) {
2870 		prepare_to_wait(&root->log_writer_wait, &wait,
2871 				TASK_UNINTERRUPTIBLE);
2872 		if (!atomic_read(&root->log_writers))
2873 			break;
2874 
2875 		mutex_unlock(&root->log_mutex);
2876 		schedule();
2877 		mutex_lock(&root->log_mutex);
2878 	}
2879 	finish_wait(&root->log_writer_wait, &wait);
2880 }
2881 
btrfs_init_log_ctx(struct btrfs_log_ctx * ctx,struct btrfs_inode * inode)2882 void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode)
2883 {
2884 	ctx->log_ret = 0;
2885 	ctx->log_transid = 0;
2886 	ctx->log_new_dentries = false;
2887 	ctx->logging_new_name = false;
2888 	ctx->logging_new_delayed_dentries = false;
2889 	ctx->logged_before = false;
2890 	ctx->inode = inode;
2891 	INIT_LIST_HEAD(&ctx->list);
2892 	INIT_LIST_HEAD(&ctx->ordered_extents);
2893 	INIT_LIST_HEAD(&ctx->conflict_inodes);
2894 	ctx->num_conflict_inodes = 0;
2895 	ctx->logging_conflict_inodes = false;
2896 	ctx->scratch_eb = NULL;
2897 }
2898 
btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx * ctx)2899 void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx)
2900 {
2901 	struct btrfs_inode *inode = ctx->inode;
2902 
2903 	if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
2904 	    !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
2905 		return;
2906 
2907 	/*
2908 	 * Don't care about allocation failure. This is just for optimization,
2909 	 * if we fail to allocate here, we will try again later if needed.
2910 	 */
2911 	ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0);
2912 }
2913 
btrfs_release_log_ctx_extents(struct btrfs_log_ctx * ctx)2914 void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx)
2915 {
2916 	struct btrfs_ordered_extent *ordered;
2917 	struct btrfs_ordered_extent *tmp;
2918 
2919 	btrfs_assert_inode_locked(ctx->inode);
2920 
2921 	list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
2922 		list_del_init(&ordered->log_list);
2923 		btrfs_put_ordered_extent(ordered);
2924 	}
2925 }
2926 
2927 
btrfs_remove_log_ctx(struct btrfs_root * root,struct btrfs_log_ctx * ctx)2928 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2929 					struct btrfs_log_ctx *ctx)
2930 {
2931 	mutex_lock(&root->log_mutex);
2932 	list_del_init(&ctx->list);
2933 	mutex_unlock(&root->log_mutex);
2934 }
2935 
2936 /*
2937  * Invoked in log mutex context, or be sure there is no other task which
2938  * can access the list.
2939  */
btrfs_remove_all_log_ctxs(struct btrfs_root * root,int index,int error)2940 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2941 					     int index, int error)
2942 {
2943 	struct btrfs_log_ctx *ctx;
2944 	struct btrfs_log_ctx *safe;
2945 
2946 	list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2947 		list_del_init(&ctx->list);
2948 		ctx->log_ret = error;
2949 	}
2950 }
2951 
2952 /*
2953  * Sends a given tree log down to the disk and updates the super blocks to
2954  * record it.  When this call is done, you know that any inodes previously
2955  * logged are safely on disk only if it returns 0.
2956  *
2957  * Any other return value means you need to call btrfs_commit_transaction.
2958  * Some of the edge cases for fsyncing directories that have had unlinks
2959  * or renames done in the past mean that sometimes the only safe
2960  * fsync is to commit the whole FS.  When btrfs_sync_log returns -EAGAIN,
2961  * that has happened.
2962  */
btrfs_sync_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)2963 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2964 		   struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2965 {
2966 	int index1;
2967 	int index2;
2968 	int mark;
2969 	int ret;
2970 	struct btrfs_fs_info *fs_info = root->fs_info;
2971 	struct btrfs_root *log = root->log_root;
2972 	struct btrfs_root *log_root_tree = fs_info->log_root_tree;
2973 	struct btrfs_root_item new_root_item;
2974 	int log_transid = 0;
2975 	struct btrfs_log_ctx root_log_ctx;
2976 	struct blk_plug plug;
2977 	u64 log_root_start;
2978 	u64 log_root_level;
2979 
2980 	mutex_lock(&root->log_mutex);
2981 	log_transid = ctx->log_transid;
2982 	if (root->log_transid_committed >= log_transid) {
2983 		mutex_unlock(&root->log_mutex);
2984 		return ctx->log_ret;
2985 	}
2986 
2987 	index1 = log_transid % 2;
2988 	if (atomic_read(&root->log_commit[index1])) {
2989 		wait_log_commit(root, log_transid);
2990 		mutex_unlock(&root->log_mutex);
2991 		return ctx->log_ret;
2992 	}
2993 	ASSERT(log_transid == root->log_transid);
2994 	atomic_set(&root->log_commit[index1], 1);
2995 
2996 	/* wait for previous tree log sync to complete */
2997 	if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2998 		wait_log_commit(root, log_transid - 1);
2999 
3000 	while (1) {
3001 		int batch = atomic_read(&root->log_batch);
3002 		/* when we're on an ssd, just kick the log commit out */
3003 		if (!btrfs_test_opt(fs_info, SSD) &&
3004 		    test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
3005 			mutex_unlock(&root->log_mutex);
3006 			schedule_timeout_uninterruptible(1);
3007 			mutex_lock(&root->log_mutex);
3008 		}
3009 		wait_for_writer(root);
3010 		if (batch == atomic_read(&root->log_batch))
3011 			break;
3012 	}
3013 
3014 	/* bail out if we need to do a full commit */
3015 	if (btrfs_need_log_full_commit(trans)) {
3016 		ret = BTRFS_LOG_FORCE_COMMIT;
3017 		mutex_unlock(&root->log_mutex);
3018 		goto out;
3019 	}
3020 
3021 	if (log_transid % 2 == 0)
3022 		mark = EXTENT_DIRTY_LOG1;
3023 	else
3024 		mark = EXTENT_DIRTY_LOG2;
3025 
3026 	/* we start IO on  all the marked extents here, but we don't actually
3027 	 * wait for them until later.
3028 	 */
3029 	blk_start_plug(&plug);
3030 	ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
3031 	/*
3032 	 * -EAGAIN happens when someone, e.g., a concurrent transaction
3033 	 *  commit, writes a dirty extent in this tree-log commit. This
3034 	 *  concurrent write will create a hole writing out the extents,
3035 	 *  and we cannot proceed on a zoned filesystem, requiring
3036 	 *  sequential writing. While we can bail out to a full commit
3037 	 *  here, but we can continue hoping the concurrent writing fills
3038 	 *  the hole.
3039 	 */
3040 	if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
3041 		ret = 0;
3042 	if (ret) {
3043 		blk_finish_plug(&plug);
3044 		btrfs_set_log_full_commit(trans);
3045 		mutex_unlock(&root->log_mutex);
3046 		goto out;
3047 	}
3048 
3049 	/*
3050 	 * We _must_ update under the root->log_mutex in order to make sure we
3051 	 * have a consistent view of the log root we are trying to commit at
3052 	 * this moment.
3053 	 *
3054 	 * We _must_ copy this into a local copy, because we are not holding the
3055 	 * log_root_tree->log_mutex yet.  This is important because when we
3056 	 * commit the log_root_tree we must have a consistent view of the
3057 	 * log_root_tree when we update the super block to point at the
3058 	 * log_root_tree bytenr.  If we update the log_root_tree here we'll race
3059 	 * with the commit and possibly point at the new block which we may not
3060 	 * have written out.
3061 	 */
3062 	btrfs_set_root_node(&log->root_item, log->node);
3063 	memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
3064 
3065 	btrfs_set_root_log_transid(root, root->log_transid + 1);
3066 	log->log_transid = root->log_transid;
3067 	root->log_start_pid = 0;
3068 	/*
3069 	 * IO has been started, blocks of the log tree have WRITTEN flag set
3070 	 * in their headers. new modifications of the log will be written to
3071 	 * new positions. so it's safe to allow log writers to go in.
3072 	 */
3073 	mutex_unlock(&root->log_mutex);
3074 
3075 	if (btrfs_is_zoned(fs_info)) {
3076 		mutex_lock(&fs_info->tree_root->log_mutex);
3077 		if (!log_root_tree->node) {
3078 			ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
3079 			if (ret) {
3080 				mutex_unlock(&fs_info->tree_root->log_mutex);
3081 				blk_finish_plug(&plug);
3082 				goto out;
3083 			}
3084 		}
3085 		mutex_unlock(&fs_info->tree_root->log_mutex);
3086 	}
3087 
3088 	btrfs_init_log_ctx(&root_log_ctx, NULL);
3089 
3090 	mutex_lock(&log_root_tree->log_mutex);
3091 
3092 	index2 = log_root_tree->log_transid % 2;
3093 	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3094 	root_log_ctx.log_transid = log_root_tree->log_transid;
3095 
3096 	/*
3097 	 * Now we are safe to update the log_root_tree because we're under the
3098 	 * log_mutex, and we're a current writer so we're holding the commit
3099 	 * open until we drop the log_mutex.
3100 	 */
3101 	ret = update_log_root(trans, log, &new_root_item);
3102 	if (ret) {
3103 		list_del_init(&root_log_ctx.list);
3104 		blk_finish_plug(&plug);
3105 		btrfs_set_log_full_commit(trans);
3106 		if (ret != -ENOSPC)
3107 			btrfs_err(fs_info,
3108 				  "failed to update log for root %llu ret %d",
3109 				  btrfs_root_id(root), ret);
3110 		btrfs_wait_tree_log_extents(log, mark);
3111 		mutex_unlock(&log_root_tree->log_mutex);
3112 		goto out;
3113 	}
3114 
3115 	if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3116 		blk_finish_plug(&plug);
3117 		list_del_init(&root_log_ctx.list);
3118 		mutex_unlock(&log_root_tree->log_mutex);
3119 		ret = root_log_ctx.log_ret;
3120 		goto out;
3121 	}
3122 
3123 	if (atomic_read(&log_root_tree->log_commit[index2])) {
3124 		blk_finish_plug(&plug);
3125 		ret = btrfs_wait_tree_log_extents(log, mark);
3126 		wait_log_commit(log_root_tree,
3127 				root_log_ctx.log_transid);
3128 		mutex_unlock(&log_root_tree->log_mutex);
3129 		if (!ret)
3130 			ret = root_log_ctx.log_ret;
3131 		goto out;
3132 	}
3133 	ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3134 	atomic_set(&log_root_tree->log_commit[index2], 1);
3135 
3136 	if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3137 		wait_log_commit(log_root_tree,
3138 				root_log_ctx.log_transid - 1);
3139 	}
3140 
3141 	/*
3142 	 * now that we've moved on to the tree of log tree roots,
3143 	 * check the full commit flag again
3144 	 */
3145 	if (btrfs_need_log_full_commit(trans)) {
3146 		blk_finish_plug(&plug);
3147 		btrfs_wait_tree_log_extents(log, mark);
3148 		mutex_unlock(&log_root_tree->log_mutex);
3149 		ret = BTRFS_LOG_FORCE_COMMIT;
3150 		goto out_wake_log_root;
3151 	}
3152 
3153 	ret = btrfs_write_marked_extents(fs_info,
3154 					 &log_root_tree->dirty_log_pages,
3155 					 EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
3156 	blk_finish_plug(&plug);
3157 	/*
3158 	 * As described above, -EAGAIN indicates a hole in the extents. We
3159 	 * cannot wait for these write outs since the waiting cause a
3160 	 * deadlock. Bail out to the full commit instead.
3161 	 */
3162 	if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
3163 		btrfs_set_log_full_commit(trans);
3164 		btrfs_wait_tree_log_extents(log, mark);
3165 		mutex_unlock(&log_root_tree->log_mutex);
3166 		goto out_wake_log_root;
3167 	} else if (ret) {
3168 		btrfs_set_log_full_commit(trans);
3169 		mutex_unlock(&log_root_tree->log_mutex);
3170 		goto out_wake_log_root;
3171 	}
3172 	ret = btrfs_wait_tree_log_extents(log, mark);
3173 	if (!ret)
3174 		ret = btrfs_wait_tree_log_extents(log_root_tree,
3175 						  EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
3176 	if (ret) {
3177 		btrfs_set_log_full_commit(trans);
3178 		mutex_unlock(&log_root_tree->log_mutex);
3179 		goto out_wake_log_root;
3180 	}
3181 
3182 	log_root_start = log_root_tree->node->start;
3183 	log_root_level = btrfs_header_level(log_root_tree->node);
3184 	log_root_tree->log_transid++;
3185 	mutex_unlock(&log_root_tree->log_mutex);
3186 
3187 	/*
3188 	 * Here we are guaranteed that nobody is going to write the superblock
3189 	 * for the current transaction before us and that neither we do write
3190 	 * our superblock before the previous transaction finishes its commit
3191 	 * and writes its superblock, because:
3192 	 *
3193 	 * 1) We are holding a handle on the current transaction, so no body
3194 	 *    can commit it until we release the handle;
3195 	 *
3196 	 * 2) Before writing our superblock we acquire the tree_log_mutex, so
3197 	 *    if the previous transaction is still committing, and hasn't yet
3198 	 *    written its superblock, we wait for it to do it, because a
3199 	 *    transaction commit acquires the tree_log_mutex when the commit
3200 	 *    begins and releases it only after writing its superblock.
3201 	 */
3202 	mutex_lock(&fs_info->tree_log_mutex);
3203 
3204 	/*
3205 	 * The previous transaction writeout phase could have failed, and thus
3206 	 * marked the fs in an error state.  We must not commit here, as we
3207 	 * could have updated our generation in the super_for_commit and
3208 	 * writing the super here would result in transid mismatches.  If there
3209 	 * is an error here just bail.
3210 	 */
3211 	if (BTRFS_FS_ERROR(fs_info)) {
3212 		ret = -EIO;
3213 		btrfs_set_log_full_commit(trans);
3214 		btrfs_abort_transaction(trans, ret);
3215 		mutex_unlock(&fs_info->tree_log_mutex);
3216 		goto out_wake_log_root;
3217 	}
3218 
3219 	btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
3220 	btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
3221 	ret = write_all_supers(fs_info, 1);
3222 	mutex_unlock(&fs_info->tree_log_mutex);
3223 	if (ret) {
3224 		btrfs_set_log_full_commit(trans);
3225 		btrfs_abort_transaction(trans, ret);
3226 		goto out_wake_log_root;
3227 	}
3228 
3229 	/*
3230 	 * We know there can only be one task here, since we have not yet set
3231 	 * root->log_commit[index1] to 0 and any task attempting to sync the
3232 	 * log must wait for the previous log transaction to commit if it's
3233 	 * still in progress or wait for the current log transaction commit if
3234 	 * someone else already started it. We use <= and not < because the
3235 	 * first log transaction has an ID of 0.
3236 	 */
3237 	ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid);
3238 	btrfs_set_root_last_log_commit(root, log_transid);
3239 
3240 out_wake_log_root:
3241 	mutex_lock(&log_root_tree->log_mutex);
3242 	btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3243 
3244 	log_root_tree->log_transid_committed++;
3245 	atomic_set(&log_root_tree->log_commit[index2], 0);
3246 	mutex_unlock(&log_root_tree->log_mutex);
3247 
3248 	/*
3249 	 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3250 	 * all the updates above are seen by the woken threads. It might not be
3251 	 * necessary, but proving that seems to be hard.
3252 	 */
3253 	cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3254 out:
3255 	mutex_lock(&root->log_mutex);
3256 	btrfs_remove_all_log_ctxs(root, index1, ret);
3257 	root->log_transid_committed++;
3258 	atomic_set(&root->log_commit[index1], 0);
3259 	mutex_unlock(&root->log_mutex);
3260 
3261 	/*
3262 	 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3263 	 * all the updates above are seen by the woken threads. It might not be
3264 	 * necessary, but proving that seems to be hard.
3265 	 */
3266 	cond_wake_up(&root->log_commit_wait[index1]);
3267 	return ret;
3268 }
3269 
free_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log)3270 static void free_log_tree(struct btrfs_trans_handle *trans,
3271 			  struct btrfs_root *log)
3272 {
3273 	int ret;
3274 	struct walk_control wc = {
3275 		.free = 1,
3276 		.process_func = process_one_buffer
3277 	};
3278 
3279 	if (log->node) {
3280 		ret = walk_log_tree(trans, log, &wc);
3281 		if (ret) {
3282 			/*
3283 			 * We weren't able to traverse the entire log tree, the
3284 			 * typical scenario is getting an -EIO when reading an
3285 			 * extent buffer of the tree, due to a previous writeback
3286 			 * failure of it.
3287 			 */
3288 			set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
3289 				&log->fs_info->fs_state);
3290 
3291 			/*
3292 			 * Some extent buffers of the log tree may still be dirty
3293 			 * and not yet written back to storage, because we may
3294 			 * have updates to a log tree without syncing a log tree,
3295 			 * such as during rename and link operations. So flush
3296 			 * them out and wait for their writeback to complete, so
3297 			 * that we properly cleanup their state and pages.
3298 			 */
3299 			btrfs_write_marked_extents(log->fs_info,
3300 						   &log->dirty_log_pages,
3301 						   EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
3302 			btrfs_wait_tree_log_extents(log,
3303 						    EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
3304 
3305 			if (trans)
3306 				btrfs_abort_transaction(trans, ret);
3307 			else
3308 				btrfs_handle_fs_error(log->fs_info, ret, NULL);
3309 		}
3310 	}
3311 
3312 	btrfs_extent_io_tree_release(&log->dirty_log_pages);
3313 	btrfs_extent_io_tree_release(&log->log_csum_range);
3314 
3315 	btrfs_put_root(log);
3316 }
3317 
3318 /*
3319  * free all the extents used by the tree log.  This should be called
3320  * at commit time of the full transaction
3321  */
btrfs_free_log(struct btrfs_trans_handle * trans,struct btrfs_root * root)3322 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3323 {
3324 	if (root->log_root) {
3325 		free_log_tree(trans, root->log_root);
3326 		root->log_root = NULL;
3327 		clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
3328 	}
3329 	return 0;
3330 }
3331 
btrfs_free_log_root_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)3332 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3333 			     struct btrfs_fs_info *fs_info)
3334 {
3335 	if (fs_info->log_root_tree) {
3336 		free_log_tree(trans, fs_info->log_root_tree);
3337 		fs_info->log_root_tree = NULL;
3338 		clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
3339 	}
3340 	return 0;
3341 }
3342 
mark_inode_as_not_logged(const struct btrfs_trans_handle * trans,struct btrfs_inode * inode)3343 static bool mark_inode_as_not_logged(const struct btrfs_trans_handle *trans,
3344 				     struct btrfs_inode *inode)
3345 {
3346 	bool ret = false;
3347 
3348 	/*
3349 	 * Do this only if ->logged_trans is still 0 to prevent races with
3350 	 * concurrent logging as we may see the inode not logged when
3351 	 * inode_logged() is called but it gets logged after inode_logged() did
3352 	 * not find it in the log tree and we end up setting ->logged_trans to a
3353 	 * value less than trans->transid after the concurrent logging task has
3354 	 * set it to trans->transid. As a consequence, subsequent rename, unlink
3355 	 * and link operations may end up not logging new names and removing old
3356 	 * names from the log.
3357 	 */
3358 	spin_lock(&inode->lock);
3359 	if (inode->logged_trans == 0)
3360 		inode->logged_trans = trans->transid - 1;
3361 	else if (inode->logged_trans == trans->transid)
3362 		ret = true;
3363 	spin_unlock(&inode->lock);
3364 
3365 	return ret;
3366 }
3367 
3368 /*
3369  * Check if an inode was logged in the current transaction. This correctly deals
3370  * with the case where the inode was logged but has a logged_trans of 0, which
3371  * happens if the inode is evicted and loaded again, as logged_trans is an in
3372  * memory only field (not persisted).
3373  *
3374  * Returns 1 if the inode was logged before in the transaction, 0 if it was not,
3375  * and < 0 on error.
3376  */
inode_logged(const struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path_in)3377 static int inode_logged(const struct btrfs_trans_handle *trans,
3378 			struct btrfs_inode *inode,
3379 			struct btrfs_path *path_in)
3380 {
3381 	struct btrfs_path *path = path_in;
3382 	struct btrfs_key key;
3383 	int ret;
3384 
3385 	/*
3386 	 * Quick lockless call, since once ->logged_trans is set to the current
3387 	 * transaction, we never set it to a lower value anywhere else.
3388 	 */
3389 	if (data_race(inode->logged_trans) == trans->transid)
3390 		return 1;
3391 
3392 	/*
3393 	 * If logged_trans is not 0 and not trans->transid, then we know the
3394 	 * inode was not logged in this transaction, so we can return false
3395 	 * right away. We take the lock to avoid a race caused by load/store
3396 	 * tearing with a concurrent btrfs_log_inode() call or a concurrent task
3397 	 * in this function further below - an update to trans->transid can be
3398 	 * teared into two 32 bits updates for example, in which case we could
3399 	 * see a positive value that is not trans->transid and assume the inode
3400 	 * was not logged when it was.
3401 	 */
3402 	spin_lock(&inode->lock);
3403 	if (inode->logged_trans == trans->transid) {
3404 		spin_unlock(&inode->lock);
3405 		return 1;
3406 	} else if (inode->logged_trans > 0) {
3407 		spin_unlock(&inode->lock);
3408 		return 0;
3409 	}
3410 	spin_unlock(&inode->lock);
3411 
3412 	/*
3413 	 * If no log tree was created for this root in this transaction, then
3414 	 * the inode can not have been logged in this transaction. In that case
3415 	 * set logged_trans to anything greater than 0 and less than the current
3416 	 * transaction's ID, to avoid the search below in a future call in case
3417 	 * a log tree gets created after this.
3418 	 */
3419 	if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
3420 		return mark_inode_as_not_logged(trans, inode);
3421 
3422 	/*
3423 	 * We have a log tree and the inode's logged_trans is 0. We can't tell
3424 	 * for sure if the inode was logged before in this transaction by looking
3425 	 * only at logged_trans. We could be pessimistic and assume it was, but
3426 	 * that can lead to unnecessarily logging an inode during rename and link
3427 	 * operations, and then further updating the log in followup rename and
3428 	 * link operations, specially if it's a directory, which adds latency
3429 	 * visible to applications doing a series of rename or link operations.
3430 	 *
3431 	 * A logged_trans of 0 here can mean several things:
3432 	 *
3433 	 * 1) The inode was never logged since the filesystem was mounted, and may
3434 	 *    or may have not been evicted and loaded again;
3435 	 *
3436 	 * 2) The inode was logged in a previous transaction, then evicted and
3437 	 *    then loaded again;
3438 	 *
3439 	 * 3) The inode was logged in the current transaction, then evicted and
3440 	 *    then loaded again.
3441 	 *
3442 	 * For cases 1) and 2) we don't want to return true, but we need to detect
3443 	 * case 3) and return true. So we do a search in the log root for the inode
3444 	 * item.
3445 	 */
3446 	key.objectid = btrfs_ino(inode);
3447 	key.type = BTRFS_INODE_ITEM_KEY;
3448 	key.offset = 0;
3449 
3450 	if (!path) {
3451 		path = btrfs_alloc_path();
3452 		if (!path)
3453 			return -ENOMEM;
3454 	}
3455 
3456 	ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
3457 
3458 	if (path_in)
3459 		btrfs_release_path(path);
3460 	else
3461 		btrfs_free_path(path);
3462 
3463 	/*
3464 	 * Logging an inode always results in logging its inode item. So if we
3465 	 * did not find the item we know the inode was not logged for sure.
3466 	 */
3467 	if (ret < 0) {
3468 		return ret;
3469 	} else if (ret > 0) {
3470 		/*
3471 		 * Set logged_trans to a value greater than 0 and less then the
3472 		 * current transaction to avoid doing the search in future calls.
3473 		 */
3474 		return mark_inode_as_not_logged(trans, inode);
3475 	}
3476 
3477 	/*
3478 	 * The inode was previously logged and then evicted, set logged_trans to
3479 	 * the current transacion's ID, to avoid future tree searches as long as
3480 	 * the inode is not evicted again.
3481 	 */
3482 	spin_lock(&inode->lock);
3483 	inode->logged_trans = trans->transid;
3484 	spin_unlock(&inode->lock);
3485 
3486 	return 1;
3487 }
3488 
3489 /*
3490  * Delete a directory entry from the log if it exists.
3491  *
3492  * Returns < 0 on error
3493  *           1 if the entry does not exists
3494  *           0 if the entry existed and was successfully deleted
3495  */
del_logged_dentry(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,u64 dir_ino,const struct fscrypt_str * name,u64 index)3496 static int del_logged_dentry(struct btrfs_trans_handle *trans,
3497 			     struct btrfs_root *log,
3498 			     struct btrfs_path *path,
3499 			     u64 dir_ino,
3500 			     const struct fscrypt_str *name,
3501 			     u64 index)
3502 {
3503 	struct btrfs_dir_item *di;
3504 
3505 	/*
3506 	 * We only log dir index items of a directory, so we don't need to look
3507 	 * for dir item keys.
3508 	 */
3509 	di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3510 					 index, name, -1);
3511 	if (IS_ERR(di))
3512 		return PTR_ERR(di);
3513 	else if (!di)
3514 		return 1;
3515 
3516 	/*
3517 	 * We do not need to update the size field of the directory's
3518 	 * inode item because on log replay we update the field to reflect
3519 	 * all existing entries in the directory (see overwrite_item()).
3520 	 */
3521 	return btrfs_del_item(trans, log, path);
3522 }
3523 
3524 /*
3525  * If both a file and directory are logged, and unlinks or renames are
3526  * mixed in, we have a few interesting corners:
3527  *
3528  * create file X in dir Y
3529  * link file X to X.link in dir Y
3530  * fsync file X
3531  * unlink file X but leave X.link
3532  * fsync dir Y
3533  *
3534  * After a crash we would expect only X.link to exist.  But file X
3535  * didn't get fsync'd again so the log has back refs for X and X.link.
3536  *
3537  * We solve this by removing directory entries and inode backrefs from the
3538  * log when a file that was logged in the current transaction is
3539  * unlinked.  Any later fsync will include the updated log entries, and
3540  * we'll be able to reconstruct the proper directory items from backrefs.
3541  *
3542  * This optimizations allows us to avoid relogging the entire inode
3543  * or the entire directory.
3544  */
btrfs_del_dir_entries_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct fscrypt_str * name,struct btrfs_inode * dir,u64 index)3545 void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3546 				  struct btrfs_root *root,
3547 				  const struct fscrypt_str *name,
3548 				  struct btrfs_inode *dir, u64 index)
3549 {
3550 	struct btrfs_path *path;
3551 	int ret;
3552 
3553 	ret = inode_logged(trans, dir, NULL);
3554 	if (ret == 0)
3555 		return;
3556 	else if (ret < 0) {
3557 		btrfs_set_log_full_commit(trans);
3558 		return;
3559 	}
3560 
3561 	path = btrfs_alloc_path();
3562 	if (!path) {
3563 		btrfs_set_log_full_commit(trans);
3564 		return;
3565 	}
3566 
3567 	ret = join_running_log_trans(root);
3568 	ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
3569 	if (WARN_ON(ret))
3570 		goto out;
3571 
3572 	mutex_lock(&dir->log_mutex);
3573 
3574 	ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir),
3575 				name, index);
3576 	mutex_unlock(&dir->log_mutex);
3577 	if (ret < 0)
3578 		btrfs_set_log_full_commit(trans);
3579 	btrfs_end_log_trans(root);
3580 out:
3581 	btrfs_free_path(path);
3582 }
3583 
3584 /* see comments for btrfs_del_dir_entries_in_log */
btrfs_del_inode_ref_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct fscrypt_str * name,struct btrfs_inode * inode,u64 dirid)3585 void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3586 				struct btrfs_root *root,
3587 				const struct fscrypt_str *name,
3588 				struct btrfs_inode *inode, u64 dirid)
3589 {
3590 	struct btrfs_root *log;
3591 	int ret;
3592 
3593 	ret = inode_logged(trans, inode, NULL);
3594 	if (ret == 0)
3595 		return;
3596 	else if (ret < 0) {
3597 		btrfs_set_log_full_commit(trans);
3598 		return;
3599 	}
3600 
3601 	ret = join_running_log_trans(root);
3602 	ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
3603 	if (WARN_ON(ret))
3604 		return;
3605 	log = root->log_root;
3606 	mutex_lock(&inode->log_mutex);
3607 
3608 	ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode), dirid, NULL);
3609 	mutex_unlock(&inode->log_mutex);
3610 	if (ret < 0 && ret != -ENOENT)
3611 		btrfs_set_log_full_commit(trans);
3612 	btrfs_end_log_trans(root);
3613 }
3614 
3615 /*
3616  * creates a range item in the log for 'dirid'.  first_offset and
3617  * last_offset tell us which parts of the key space the log should
3618  * be considered authoritative for.
3619  */
insert_dir_log_key(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,u64 dirid,u64 first_offset,u64 last_offset)3620 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3621 				       struct btrfs_root *log,
3622 				       struct btrfs_path *path,
3623 				       u64 dirid,
3624 				       u64 first_offset, u64 last_offset)
3625 {
3626 	int ret;
3627 	struct btrfs_key key;
3628 	struct btrfs_dir_log_item *item;
3629 
3630 	key.objectid = dirid;
3631 	key.type = BTRFS_DIR_LOG_INDEX_KEY;
3632 	key.offset = first_offset;
3633 	ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3634 	/*
3635 	 * -EEXIST is fine and can happen sporadically when we are logging a
3636 	 * directory and have concurrent insertions in the subvolume's tree for
3637 	 * items from other inodes and that result in pushing off some dir items
3638 	 * from one leaf to another in order to accommodate for the new items.
3639 	 * This results in logging the same dir index range key.
3640 	 */
3641 	if (ret && ret != -EEXIST)
3642 		return ret;
3643 
3644 	item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3645 			      struct btrfs_dir_log_item);
3646 	if (ret == -EEXIST) {
3647 		const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item);
3648 
3649 		/*
3650 		 * btrfs_del_dir_entries_in_log() might have been called during
3651 		 * an unlink between the initial insertion of this key and the
3652 		 * current update, or we might be logging a single entry deletion
3653 		 * during a rename, so set the new last_offset to the max value.
3654 		 */
3655 		last_offset = max(last_offset, curr_end);
3656 	}
3657 	btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3658 	btrfs_release_path(path);
3659 	return 0;
3660 }
3661 
flush_dir_items_batch(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct extent_buffer * src,struct btrfs_path * dst_path,int start_slot,int count)3662 static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
3663 				 struct btrfs_inode *inode,
3664 				 struct extent_buffer *src,
3665 				 struct btrfs_path *dst_path,
3666 				 int start_slot,
3667 				 int count)
3668 {
3669 	struct btrfs_root *log = inode->root->log_root;
3670 	char *ins_data = NULL;
3671 	struct btrfs_item_batch batch;
3672 	struct extent_buffer *dst;
3673 	unsigned long src_offset;
3674 	unsigned long dst_offset;
3675 	u64 last_index;
3676 	struct btrfs_key key;
3677 	u32 item_size;
3678 	int ret;
3679 	int i;
3680 
3681 	ASSERT(count > 0);
3682 	batch.nr = count;
3683 
3684 	if (count == 1) {
3685 		btrfs_item_key_to_cpu(src, &key, start_slot);
3686 		item_size = btrfs_item_size(src, start_slot);
3687 		batch.keys = &key;
3688 		batch.data_sizes = &item_size;
3689 		batch.total_data_size = item_size;
3690 	} else {
3691 		struct btrfs_key *ins_keys;
3692 		u32 *ins_sizes;
3693 
3694 		ins_data = kmalloc(count * sizeof(u32) +
3695 				   count * sizeof(struct btrfs_key), GFP_NOFS);
3696 		if (!ins_data)
3697 			return -ENOMEM;
3698 
3699 		ins_sizes = (u32 *)ins_data;
3700 		ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
3701 		batch.keys = ins_keys;
3702 		batch.data_sizes = ins_sizes;
3703 		batch.total_data_size = 0;
3704 
3705 		for (i = 0; i < count; i++) {
3706 			const int slot = start_slot + i;
3707 
3708 			btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
3709 			ins_sizes[i] = btrfs_item_size(src, slot);
3710 			batch.total_data_size += ins_sizes[i];
3711 		}
3712 	}
3713 
3714 	ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
3715 	if (ret)
3716 		goto out;
3717 
3718 	dst = dst_path->nodes[0];
3719 	/*
3720 	 * Copy all the items in bulk, in a single copy operation. Item data is
3721 	 * organized such that it's placed at the end of a leaf and from right
3722 	 * to left. For example, the data for the second item ends at an offset
3723 	 * that matches the offset where the data for the first item starts, the
3724 	 * data for the third item ends at an offset that matches the offset
3725 	 * where the data of the second items starts, and so on.
3726 	 * Therefore our source and destination start offsets for copy match the
3727 	 * offsets of the last items (highest slots).
3728 	 */
3729 	dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
3730 	src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
3731 	copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
3732 	btrfs_release_path(dst_path);
3733 
3734 	last_index = batch.keys[count - 1].offset;
3735 	ASSERT(last_index > inode->last_dir_index_offset);
3736 
3737 	/*
3738 	 * If for some unexpected reason the last item's index is not greater
3739 	 * than the last index we logged, warn and force a transaction commit.
3740 	 */
3741 	if (WARN_ON(last_index <= inode->last_dir_index_offset))
3742 		ret = BTRFS_LOG_FORCE_COMMIT;
3743 	else
3744 		inode->last_dir_index_offset = last_index;
3745 
3746 	if (btrfs_get_first_dir_index_to_log(inode) == 0)
3747 		btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset);
3748 out:
3749 	kfree(ins_data);
3750 
3751 	return ret;
3752 }
3753 
clone_leaf(struct btrfs_path * path,struct btrfs_log_ctx * ctx)3754 static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx)
3755 {
3756 	const int slot = path->slots[0];
3757 
3758 	if (ctx->scratch_eb) {
3759 		copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]);
3760 	} else {
3761 		ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]);
3762 		if (!ctx->scratch_eb)
3763 			return -ENOMEM;
3764 	}
3765 
3766 	btrfs_release_path(path);
3767 	path->nodes[0] = ctx->scratch_eb;
3768 	path->slots[0] = slot;
3769 	/*
3770 	 * Add extra ref to scratch eb so that it is not freed when callers
3771 	 * release the path, so we can reuse it later if needed.
3772 	 */
3773 	refcount_inc(&ctx->scratch_eb->refs);
3774 
3775 	return 0;
3776 }
3777 
process_dir_items_leaf(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx,u64 * last_old_dentry_offset)3778 static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
3779 				  struct btrfs_inode *inode,
3780 				  struct btrfs_path *path,
3781 				  struct btrfs_path *dst_path,
3782 				  struct btrfs_log_ctx *ctx,
3783 				  u64 *last_old_dentry_offset)
3784 {
3785 	struct btrfs_root *log = inode->root->log_root;
3786 	struct extent_buffer *src;
3787 	const int nritems = btrfs_header_nritems(path->nodes[0]);
3788 	const u64 ino = btrfs_ino(inode);
3789 	bool last_found = false;
3790 	int batch_start = 0;
3791 	int batch_size = 0;
3792 	int ret;
3793 
3794 	/*
3795 	 * We need to clone the leaf, release the read lock on it, and use the
3796 	 * clone before modifying the log tree. See the comment at copy_items()
3797 	 * about why we need to do this.
3798 	 */
3799 	ret = clone_leaf(path, ctx);
3800 	if (ret < 0)
3801 		return ret;
3802 
3803 	src = path->nodes[0];
3804 
3805 	for (int i = path->slots[0]; i < nritems; i++) {
3806 		struct btrfs_dir_item *di;
3807 		struct btrfs_key key;
3808 		int ret;
3809 
3810 		btrfs_item_key_to_cpu(src, &key, i);
3811 
3812 		if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) {
3813 			last_found = true;
3814 			break;
3815 		}
3816 
3817 		di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3818 
3819 		/*
3820 		 * Skip ranges of items that consist only of dir item keys created
3821 		 * in past transactions. However if we find a gap, we must log a
3822 		 * dir index range item for that gap, so that index keys in that
3823 		 * gap are deleted during log replay.
3824 		 */
3825 		if (btrfs_dir_transid(src, di) < trans->transid) {
3826 			if (key.offset > *last_old_dentry_offset + 1) {
3827 				ret = insert_dir_log_key(trans, log, dst_path,
3828 						 ino, *last_old_dentry_offset + 1,
3829 						 key.offset - 1);
3830 				if (ret < 0)
3831 					return ret;
3832 			}
3833 
3834 			*last_old_dentry_offset = key.offset;
3835 			continue;
3836 		}
3837 
3838 		/* If we logged this dir index item before, we can skip it. */
3839 		if (key.offset <= inode->last_dir_index_offset)
3840 			continue;
3841 
3842 		/*
3843 		 * We must make sure that when we log a directory entry, the
3844 		 * corresponding inode, after log replay, has a matching link
3845 		 * count. For example:
3846 		 *
3847 		 * touch foo
3848 		 * mkdir mydir
3849 		 * sync
3850 		 * ln foo mydir/bar
3851 		 * xfs_io -c "fsync" mydir
3852 		 * <crash>
3853 		 * <mount fs and log replay>
3854 		 *
3855 		 * Would result in a fsync log that when replayed, our file inode
3856 		 * would have a link count of 1, but we get two directory entries
3857 		 * pointing to the same inode. After removing one of the names,
3858 		 * it would not be possible to remove the other name, which
3859 		 * resulted always in stale file handle errors, and would not be
3860 		 * possible to rmdir the parent directory, since its i_size could
3861 		 * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
3862 		 * resulting in -ENOTEMPTY errors.
3863 		 */
3864 		if (!ctx->log_new_dentries) {
3865 			struct btrfs_key di_key;
3866 
3867 			btrfs_dir_item_key_to_cpu(src, di, &di_key);
3868 			if (di_key.type != BTRFS_ROOT_ITEM_KEY)
3869 				ctx->log_new_dentries = true;
3870 		}
3871 
3872 		if (batch_size == 0)
3873 			batch_start = i;
3874 		batch_size++;
3875 	}
3876 
3877 	if (batch_size > 0) {
3878 		int ret;
3879 
3880 		ret = flush_dir_items_batch(trans, inode, src, dst_path,
3881 					    batch_start, batch_size);
3882 		if (ret < 0)
3883 			return ret;
3884 	}
3885 
3886 	return last_found ? 1 : 0;
3887 }
3888 
3889 /*
3890  * log all the items included in the current transaction for a given
3891  * directory.  This also creates the range items in the log tree required
3892  * to replay anything deleted before the fsync
3893  */
log_dir_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx,u64 min_offset,u64 * last_offset_ret)3894 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3895 			  struct btrfs_inode *inode,
3896 			  struct btrfs_path *path,
3897 			  struct btrfs_path *dst_path,
3898 			  struct btrfs_log_ctx *ctx,
3899 			  u64 min_offset, u64 *last_offset_ret)
3900 {
3901 	struct btrfs_key min_key;
3902 	struct btrfs_root *root = inode->root;
3903 	struct btrfs_root *log = root->log_root;
3904 	int ret;
3905 	u64 last_old_dentry_offset = min_offset - 1;
3906 	u64 last_offset = (u64)-1;
3907 	u64 ino = btrfs_ino(inode);
3908 
3909 	min_key.objectid = ino;
3910 	min_key.type = BTRFS_DIR_INDEX_KEY;
3911 	min_key.offset = min_offset;
3912 
3913 	ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3914 
3915 	/*
3916 	 * we didn't find anything from this transaction, see if there
3917 	 * is anything at all
3918 	 */
3919 	if (ret != 0 || min_key.objectid != ino ||
3920 	    min_key.type != BTRFS_DIR_INDEX_KEY) {
3921 		min_key.objectid = ino;
3922 		min_key.type = BTRFS_DIR_INDEX_KEY;
3923 		min_key.offset = (u64)-1;
3924 		btrfs_release_path(path);
3925 		ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3926 		if (ret < 0) {
3927 			btrfs_release_path(path);
3928 			return ret;
3929 		}
3930 		ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
3931 
3932 		/* if ret == 0 there are items for this type,
3933 		 * create a range to tell us the last key of this type.
3934 		 * otherwise, there are no items in this directory after
3935 		 * *min_offset, and we create a range to indicate that.
3936 		 */
3937 		if (ret == 0) {
3938 			struct btrfs_key tmp;
3939 
3940 			btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3941 					      path->slots[0]);
3942 			if (tmp.type == BTRFS_DIR_INDEX_KEY)
3943 				last_old_dentry_offset = tmp.offset;
3944 		} else if (ret > 0) {
3945 			ret = 0;
3946 		}
3947 
3948 		goto done;
3949 	}
3950 
3951 	/* go backward to find any previous key */
3952 	ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
3953 	if (ret == 0) {
3954 		struct btrfs_key tmp;
3955 
3956 		btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3957 		/*
3958 		 * The dir index key before the first one we found that needs to
3959 		 * be logged might be in a previous leaf, and there might be a
3960 		 * gap between these keys, meaning that we had deletions that
3961 		 * happened. So the key range item we log (key type
3962 		 * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the
3963 		 * previous key's offset plus 1, so that those deletes are replayed.
3964 		 */
3965 		if (tmp.type == BTRFS_DIR_INDEX_KEY)
3966 			last_old_dentry_offset = tmp.offset;
3967 	} else if (ret < 0) {
3968 		goto done;
3969 	}
3970 
3971 	btrfs_release_path(path);
3972 
3973 	/*
3974 	 * Find the first key from this transaction again or the one we were at
3975 	 * in the loop below in case we had to reschedule. We may be logging the
3976 	 * directory without holding its VFS lock, which happen when logging new
3977 	 * dentries (through log_new_dir_dentries()) or in some cases when we
3978 	 * need to log the parent directory of an inode. This means a dir index
3979 	 * key might be deleted from the inode's root, and therefore we may not
3980 	 * find it anymore. If we can't find it, just move to the next key. We
3981 	 * can not bail out and ignore, because if we do that we will simply
3982 	 * not log dir index keys that come after the one that was just deleted
3983 	 * and we can end up logging a dir index range that ends at (u64)-1
3984 	 * (@last_offset is initialized to that), resulting in removing dir
3985 	 * entries we should not remove at log replay time.
3986 	 */
3987 search:
3988 	ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3989 	if (ret > 0) {
3990 		ret = btrfs_next_item(root, path);
3991 		if (ret > 0) {
3992 			/* There are no more keys in the inode's root. */
3993 			ret = 0;
3994 			goto done;
3995 		}
3996 	}
3997 	if (ret < 0)
3998 		goto done;
3999 
4000 	/*
4001 	 * we have a block from this transaction, log every item in it
4002 	 * from our directory
4003 	 */
4004 	while (1) {
4005 		ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx,
4006 					     &last_old_dentry_offset);
4007 		if (ret != 0) {
4008 			if (ret > 0)
4009 				ret = 0;
4010 			goto done;
4011 		}
4012 		path->slots[0] = btrfs_header_nritems(path->nodes[0]);
4013 
4014 		/*
4015 		 * look ahead to the next item and see if it is also
4016 		 * from this directory and from this transaction
4017 		 */
4018 		ret = btrfs_next_leaf(root, path);
4019 		if (ret) {
4020 			if (ret == 1) {
4021 				last_offset = (u64)-1;
4022 				ret = 0;
4023 			}
4024 			goto done;
4025 		}
4026 		btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
4027 		if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) {
4028 			last_offset = (u64)-1;
4029 			goto done;
4030 		}
4031 		if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
4032 			/*
4033 			 * The next leaf was not changed in the current transaction
4034 			 * and has at least one dir index key.
4035 			 * We check for the next key because there might have been
4036 			 * one or more deletions between the last key we logged and
4037 			 * that next key. So the key range item we log (key type
4038 			 * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's
4039 			 * offset minus 1, so that those deletes are replayed.
4040 			 */
4041 			last_offset = min_key.offset - 1;
4042 			goto done;
4043 		}
4044 		if (need_resched()) {
4045 			btrfs_release_path(path);
4046 			cond_resched();
4047 			goto search;
4048 		}
4049 	}
4050 done:
4051 	btrfs_release_path(path);
4052 	btrfs_release_path(dst_path);
4053 
4054 	if (ret == 0) {
4055 		*last_offset_ret = last_offset;
4056 		/*
4057 		 * In case the leaf was changed in the current transaction but
4058 		 * all its dir items are from a past transaction, the last item
4059 		 * in the leaf is a dir item and there's no gap between that last
4060 		 * dir item and the first one on the next leaf (which did not
4061 		 * change in the current transaction), then we don't need to log
4062 		 * a range, last_old_dentry_offset is == to last_offset.
4063 		 */
4064 		ASSERT(last_old_dentry_offset <= last_offset);
4065 		if (last_old_dentry_offset < last_offset)
4066 			ret = insert_dir_log_key(trans, log, path, ino,
4067 						 last_old_dentry_offset + 1,
4068 						 last_offset);
4069 	}
4070 
4071 	return ret;
4072 }
4073 
4074 /*
4075  * If the inode was logged before and it was evicted, then its
4076  * last_dir_index_offset is 0, so we don't know the value of the last index
4077  * key offset. If that's the case, search for it and update the inode. This
4078  * is to avoid lookups in the log tree every time we try to insert a dir index
4079  * key from a leaf changed in the current transaction, and to allow us to always
4080  * do batch insertions of dir index keys.
4081  */
update_last_dir_index_offset(struct btrfs_inode * inode,struct btrfs_path * path,const struct btrfs_log_ctx * ctx)4082 static int update_last_dir_index_offset(struct btrfs_inode *inode,
4083 					struct btrfs_path *path,
4084 					const struct btrfs_log_ctx *ctx)
4085 {
4086 	const u64 ino = btrfs_ino(inode);
4087 	struct btrfs_key key;
4088 	int ret;
4089 
4090 	lockdep_assert_held(&inode->log_mutex);
4091 
4092 	if (inode->last_dir_index_offset != 0)
4093 		return 0;
4094 
4095 	if (!ctx->logged_before) {
4096 		inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
4097 		return 0;
4098 	}
4099 
4100 	key.objectid = ino;
4101 	key.type = BTRFS_DIR_INDEX_KEY;
4102 	key.offset = (u64)-1;
4103 
4104 	ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
4105 	/*
4106 	 * An error happened or we actually have an index key with an offset
4107 	 * value of (u64)-1. Bail out, we're done.
4108 	 */
4109 	if (ret <= 0)
4110 		goto out;
4111 
4112 	ret = 0;
4113 	inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
4114 
4115 	/*
4116 	 * No dir index items, bail out and leave last_dir_index_offset with
4117 	 * the value right before the first valid index value.
4118 	 */
4119 	if (path->slots[0] == 0)
4120 		goto out;
4121 
4122 	/*
4123 	 * btrfs_search_slot() left us at one slot beyond the slot with the last
4124 	 * index key, or beyond the last key of the directory that is not an
4125 	 * index key. If we have an index key before, set last_dir_index_offset
4126 	 * to its offset value, otherwise leave it with a value right before the
4127 	 * first valid index value, as it means we have an empty directory.
4128 	 */
4129 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4130 	if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY)
4131 		inode->last_dir_index_offset = key.offset;
4132 
4133 out:
4134 	btrfs_release_path(path);
4135 
4136 	return ret;
4137 }
4138 
4139 /*
4140  * logging directories is very similar to logging inodes, We find all the items
4141  * from the current transaction and write them to the log.
4142  *
4143  * The recovery code scans the directory in the subvolume, and if it finds a
4144  * key in the range logged that is not present in the log tree, then it means
4145  * that dir entry was unlinked during the transaction.
4146  *
4147  * In order for that scan to work, we must include one key smaller than
4148  * the smallest logged by this transaction and one key larger than the largest
4149  * key logged by this transaction.
4150  */
log_directory_changes(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx)4151 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
4152 			  struct btrfs_inode *inode,
4153 			  struct btrfs_path *path,
4154 			  struct btrfs_path *dst_path,
4155 			  struct btrfs_log_ctx *ctx)
4156 {
4157 	u64 min_key;
4158 	u64 max_key;
4159 	int ret;
4160 
4161 	ret = update_last_dir_index_offset(inode, path, ctx);
4162 	if (ret)
4163 		return ret;
4164 
4165 	min_key = BTRFS_DIR_START_INDEX;
4166 	max_key = 0;
4167 
4168 	while (1) {
4169 		ret = log_dir_items(trans, inode, path, dst_path,
4170 				ctx, min_key, &max_key);
4171 		if (ret)
4172 			return ret;
4173 		if (max_key == (u64)-1)
4174 			break;
4175 		min_key = max_key + 1;
4176 	}
4177 
4178 	return 0;
4179 }
4180 
4181 /*
4182  * a helper function to drop items from the log before we relog an
4183  * inode.  max_key_type indicates the highest item type to remove.
4184  * This cannot be run for file data extents because it does not
4185  * free the extents they point to.
4186  */
drop_inode_items(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_inode * inode,int max_key_type)4187 static int drop_inode_items(struct btrfs_trans_handle *trans,
4188 				  struct btrfs_root *log,
4189 				  struct btrfs_path *path,
4190 				  struct btrfs_inode *inode,
4191 				  int max_key_type)
4192 {
4193 	int ret;
4194 	struct btrfs_key key;
4195 	struct btrfs_key found_key;
4196 	int start_slot;
4197 
4198 	key.objectid = btrfs_ino(inode);
4199 	key.type = max_key_type;
4200 	key.offset = (u64)-1;
4201 
4202 	while (1) {
4203 		ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
4204 		if (ret < 0) {
4205 			break;
4206 		} else if (ret > 0) {
4207 			if (path->slots[0] == 0)
4208 				break;
4209 			path->slots[0]--;
4210 		}
4211 
4212 		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4213 				      path->slots[0]);
4214 
4215 		if (found_key.objectid != key.objectid)
4216 			break;
4217 
4218 		found_key.offset = 0;
4219 		found_key.type = 0;
4220 		ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot);
4221 		if (ret < 0)
4222 			break;
4223 
4224 		ret = btrfs_del_items(trans, log, path, start_slot,
4225 				      path->slots[0] - start_slot + 1);
4226 		/*
4227 		 * If start slot isn't 0 then we don't need to re-search, we've
4228 		 * found the last guy with the objectid in this tree.
4229 		 */
4230 		if (ret || start_slot != 0)
4231 			break;
4232 		btrfs_release_path(path);
4233 	}
4234 	btrfs_release_path(path);
4235 	if (ret > 0)
4236 		ret = 0;
4237 	return ret;
4238 }
4239 
truncate_inode_items(struct btrfs_trans_handle * trans,struct btrfs_root * log_root,struct btrfs_inode * inode,u64 new_size,u32 min_type)4240 static int truncate_inode_items(struct btrfs_trans_handle *trans,
4241 				struct btrfs_root *log_root,
4242 				struct btrfs_inode *inode,
4243 				u64 new_size, u32 min_type)
4244 {
4245 	struct btrfs_truncate_control control = {
4246 		.new_size = new_size,
4247 		.ino = btrfs_ino(inode),
4248 		.min_type = min_type,
4249 		.skip_ref_updates = true,
4250 	};
4251 
4252 	return btrfs_truncate_inode_items(trans, log_root, &control);
4253 }
4254 
fill_inode_item(struct btrfs_trans_handle * trans,struct extent_buffer * leaf,struct btrfs_inode_item * item,struct inode * inode,int log_inode_only,u64 logged_isize)4255 static void fill_inode_item(struct btrfs_trans_handle *trans,
4256 			    struct extent_buffer *leaf,
4257 			    struct btrfs_inode_item *item,
4258 			    struct inode *inode, int log_inode_only,
4259 			    u64 logged_isize)
4260 {
4261 	u64 flags;
4262 
4263 	if (log_inode_only) {
4264 		/* set the generation to zero so the recover code
4265 		 * can tell the difference between an logging
4266 		 * just to say 'this inode exists' and a logging
4267 		 * to say 'update this inode with these values'
4268 		 */
4269 		btrfs_set_inode_generation(leaf, item, 0);
4270 		btrfs_set_inode_size(leaf, item, logged_isize);
4271 	} else {
4272 		btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
4273 		btrfs_set_inode_size(leaf, item, inode->i_size);
4274 	}
4275 
4276 	btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
4277 	btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
4278 	btrfs_set_inode_mode(leaf, item, inode->i_mode);
4279 	btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
4280 
4281 	btrfs_set_timespec_sec(leaf, &item->atime, inode_get_atime_sec(inode));
4282 	btrfs_set_timespec_nsec(leaf, &item->atime, inode_get_atime_nsec(inode));
4283 
4284 	btrfs_set_timespec_sec(leaf, &item->mtime, inode_get_mtime_sec(inode));
4285 	btrfs_set_timespec_nsec(leaf, &item->mtime, inode_get_mtime_nsec(inode));
4286 
4287 	btrfs_set_timespec_sec(leaf, &item->ctime, inode_get_ctime_sec(inode));
4288 	btrfs_set_timespec_nsec(leaf, &item->ctime, inode_get_ctime_nsec(inode));
4289 
4290 	btrfs_set_timespec_sec(leaf, &item->otime, BTRFS_I(inode)->i_otime_sec);
4291 	btrfs_set_timespec_nsec(leaf, &item->otime, BTRFS_I(inode)->i_otime_nsec);
4292 
4293 	/*
4294 	 * We do not need to set the nbytes field, in fact during a fast fsync
4295 	 * its value may not even be correct, since a fast fsync does not wait
4296 	 * for ordered extent completion, which is where we update nbytes, it
4297 	 * only waits for writeback to complete. During log replay as we find
4298 	 * file extent items and replay them, we adjust the nbytes field of the
4299 	 * inode item in subvolume tree as needed (see overwrite_item()).
4300 	 */
4301 
4302 	btrfs_set_inode_sequence(leaf, item, inode_peek_iversion(inode));
4303 	btrfs_set_inode_transid(leaf, item, trans->transid);
4304 	btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
4305 	flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
4306 					  BTRFS_I(inode)->ro_flags);
4307 	btrfs_set_inode_flags(leaf, item, flags);
4308 	btrfs_set_inode_block_group(leaf, item, 0);
4309 }
4310 
log_inode_item(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_inode * inode,bool inode_item_dropped)4311 static int log_inode_item(struct btrfs_trans_handle *trans,
4312 			  struct btrfs_root *log, struct btrfs_path *path,
4313 			  struct btrfs_inode *inode, bool inode_item_dropped)
4314 {
4315 	struct btrfs_inode_item *inode_item;
4316 	struct btrfs_key key;
4317 	int ret;
4318 
4319 	btrfs_get_inode_key(inode, &key);
4320 	/*
4321 	 * If we are doing a fast fsync and the inode was logged before in the
4322 	 * current transaction, then we know the inode was previously logged and
4323 	 * it exists in the log tree. For performance reasons, in this case use
4324 	 * btrfs_search_slot() directly with ins_len set to 0 so that we never
4325 	 * attempt a write lock on the leaf's parent, which adds unnecessary lock
4326 	 * contention in case there are concurrent fsyncs for other inodes of the
4327 	 * same subvolume. Using btrfs_insert_empty_item() when the inode item
4328 	 * already exists can also result in unnecessarily splitting a leaf.
4329 	 */
4330 	if (!inode_item_dropped && inode->logged_trans == trans->transid) {
4331 		ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
4332 		ASSERT(ret <= 0);
4333 		if (ret > 0)
4334 			ret = -ENOENT;
4335 	} else {
4336 		/*
4337 		 * This means it is the first fsync in the current transaction,
4338 		 * so the inode item is not in the log and we need to insert it.
4339 		 * We can never get -EEXIST because we are only called for a fast
4340 		 * fsync and in case an inode eviction happens after the inode was
4341 		 * logged before in the current transaction, when we load again
4342 		 * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
4343 		 * flags and set ->logged_trans to 0.
4344 		 */
4345 		ret = btrfs_insert_empty_item(trans, log, path, &key,
4346 					      sizeof(*inode_item));
4347 		ASSERT(ret != -EEXIST);
4348 	}
4349 	if (ret)
4350 		return ret;
4351 	inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4352 				    struct btrfs_inode_item);
4353 	fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
4354 			0, 0);
4355 	btrfs_release_path(path);
4356 	return 0;
4357 }
4358 
log_csums(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_root * log_root,struct btrfs_ordered_sum * sums)4359 static int log_csums(struct btrfs_trans_handle *trans,
4360 		     struct btrfs_inode *inode,
4361 		     struct btrfs_root *log_root,
4362 		     struct btrfs_ordered_sum *sums)
4363 {
4364 	const u64 lock_end = sums->logical + sums->len - 1;
4365 	struct extent_state *cached_state = NULL;
4366 	int ret;
4367 
4368 	/*
4369 	 * If this inode was not used for reflink operations in the current
4370 	 * transaction with new extents, then do the fast path, no need to
4371 	 * worry about logging checksum items with overlapping ranges.
4372 	 */
4373 	if (inode->last_reflink_trans < trans->transid)
4374 		return btrfs_csum_file_blocks(trans, log_root, sums);
4375 
4376 	/*
4377 	 * Serialize logging for checksums. This is to avoid racing with the
4378 	 * same checksum being logged by another task that is logging another
4379 	 * file which happens to refer to the same extent as well. Such races
4380 	 * can leave checksum items in the log with overlapping ranges.
4381 	 */
4382 	ret = btrfs_lock_extent(&log_root->log_csum_range, sums->logical, lock_end,
4383 				&cached_state);
4384 	if (ret)
4385 		return ret;
4386 	/*
4387 	 * Due to extent cloning, we might have logged a csum item that covers a
4388 	 * subrange of a cloned extent, and later we can end up logging a csum
4389 	 * item for a larger subrange of the same extent or the entire range.
4390 	 * This would leave csum items in the log tree that cover the same range
4391 	 * and break the searches for checksums in the log tree, resulting in
4392 	 * some checksums missing in the fs/subvolume tree. So just delete (or
4393 	 * trim and adjust) any existing csum items in the log for this range.
4394 	 */
4395 	ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len);
4396 	if (!ret)
4397 		ret = btrfs_csum_file_blocks(trans, log_root, sums);
4398 
4399 	btrfs_unlock_extent(&log_root->log_csum_range, sums->logical, lock_end,
4400 			    &cached_state);
4401 
4402 	return ret;
4403 }
4404 
copy_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * dst_path,struct btrfs_path * src_path,int start_slot,int nr,int inode_only,u64 logged_isize,struct btrfs_log_ctx * ctx)4405 static noinline int copy_items(struct btrfs_trans_handle *trans,
4406 			       struct btrfs_inode *inode,
4407 			       struct btrfs_path *dst_path,
4408 			       struct btrfs_path *src_path,
4409 			       int start_slot, int nr, int inode_only,
4410 			       u64 logged_isize, struct btrfs_log_ctx *ctx)
4411 {
4412 	struct btrfs_root *log = inode->root->log_root;
4413 	struct btrfs_file_extent_item *extent;
4414 	struct extent_buffer *src;
4415 	int ret;
4416 	struct btrfs_key *ins_keys;
4417 	u32 *ins_sizes;
4418 	struct btrfs_item_batch batch;
4419 	char *ins_data;
4420 	int dst_index;
4421 	const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM);
4422 	const u64 i_size = i_size_read(&inode->vfs_inode);
4423 
4424 	/*
4425 	 * To keep lockdep happy and avoid deadlocks, clone the source leaf and
4426 	 * use the clone. This is because otherwise we would be changing the log
4427 	 * tree, to insert items from the subvolume tree or insert csum items,
4428 	 * while holding a read lock on a leaf from the subvolume tree, which
4429 	 * creates a nasty lock dependency when COWing log tree nodes/leaves:
4430 	 *
4431 	 * 1) Modifying the log tree triggers an extent buffer allocation while
4432 	 *    holding a write lock on a parent extent buffer from the log tree.
4433 	 *    Allocating the pages for an extent buffer, or the extent buffer
4434 	 *    struct, can trigger inode eviction and finally the inode eviction
4435 	 *    will trigger a release/remove of a delayed node, which requires
4436 	 *    taking the delayed node's mutex;
4437 	 *
4438 	 * 2) Allocating a metadata extent for a log tree can trigger the async
4439 	 *    reclaim thread and make us wait for it to release enough space and
4440 	 *    unblock our reservation ticket. The reclaim thread can start
4441 	 *    flushing delayed items, and that in turn results in the need to
4442 	 *    lock delayed node mutexes and in the need to write lock extent
4443 	 *    buffers of a subvolume tree - all this while holding a write lock
4444 	 *    on the parent extent buffer in the log tree.
4445 	 *
4446 	 * So one task in scenario 1) running in parallel with another task in
4447 	 * scenario 2) could lead to a deadlock, one wanting to lock a delayed
4448 	 * node mutex while having a read lock on a leaf from the subvolume,
4449 	 * while the other is holding the delayed node's mutex and wants to
4450 	 * write lock the same subvolume leaf for flushing delayed items.
4451 	 */
4452 	ret = clone_leaf(src_path, ctx);
4453 	if (ret < 0)
4454 		return ret;
4455 
4456 	src = src_path->nodes[0];
4457 
4458 	ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
4459 			   nr * sizeof(u32), GFP_NOFS);
4460 	if (!ins_data)
4461 		return -ENOMEM;
4462 
4463 	ins_sizes = (u32 *)ins_data;
4464 	ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
4465 	batch.keys = ins_keys;
4466 	batch.data_sizes = ins_sizes;
4467 	batch.total_data_size = 0;
4468 	batch.nr = 0;
4469 
4470 	dst_index = 0;
4471 	for (int i = 0; i < nr; i++) {
4472 		const int src_slot = start_slot + i;
4473 		struct btrfs_root *csum_root;
4474 		struct btrfs_ordered_sum *sums;
4475 		struct btrfs_ordered_sum *sums_next;
4476 		LIST_HEAD(ordered_sums);
4477 		u64 disk_bytenr;
4478 		u64 disk_num_bytes;
4479 		u64 extent_offset;
4480 		u64 extent_num_bytes;
4481 		bool is_old_extent;
4482 
4483 		btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot);
4484 
4485 		if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY)
4486 			goto add_to_batch;
4487 
4488 		extent = btrfs_item_ptr(src, src_slot,
4489 					struct btrfs_file_extent_item);
4490 
4491 		is_old_extent = (btrfs_file_extent_generation(src, extent) <
4492 				 trans->transid);
4493 
4494 		/*
4495 		 * Don't copy extents from past generations. That would make us
4496 		 * log a lot more metadata for common cases like doing only a
4497 		 * few random writes into a file and then fsync it for the first
4498 		 * time or after the full sync flag is set on the inode. We can
4499 		 * get leaves full of extent items, most of which are from past
4500 		 * generations, so we can skip them - as long as the inode has
4501 		 * not been the target of a reflink operation in this transaction,
4502 		 * as in that case it might have had file extent items with old
4503 		 * generations copied into it. We also must always log prealloc
4504 		 * extents that start at or beyond eof, otherwise we would lose
4505 		 * them on log replay.
4506 		 */
4507 		if (is_old_extent &&
4508 		    ins_keys[dst_index].offset < i_size &&
4509 		    inode->last_reflink_trans < trans->transid)
4510 			continue;
4511 
4512 		if (skip_csum)
4513 			goto add_to_batch;
4514 
4515 		/* Only regular extents have checksums. */
4516 		if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG)
4517 			goto add_to_batch;
4518 
4519 		/*
4520 		 * If it's an extent created in a past transaction, then its
4521 		 * checksums are already accessible from the committed csum tree,
4522 		 * no need to log them.
4523 		 */
4524 		if (is_old_extent)
4525 			goto add_to_batch;
4526 
4527 		disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent);
4528 		/* If it's an explicit hole, there are no checksums. */
4529 		if (disk_bytenr == 0)
4530 			goto add_to_batch;
4531 
4532 		disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent);
4533 
4534 		if (btrfs_file_extent_compression(src, extent)) {
4535 			extent_offset = 0;
4536 			extent_num_bytes = disk_num_bytes;
4537 		} else {
4538 			extent_offset = btrfs_file_extent_offset(src, extent);
4539 			extent_num_bytes = btrfs_file_extent_num_bytes(src, extent);
4540 		}
4541 
4542 		csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr);
4543 		disk_bytenr += extent_offset;
4544 		ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
4545 					      disk_bytenr + extent_num_bytes - 1,
4546 					      &ordered_sums, false);
4547 		if (ret < 0)
4548 			goto out;
4549 		ret = 0;
4550 
4551 		list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) {
4552 			if (!ret)
4553 				ret = log_csums(trans, inode, log, sums);
4554 			list_del(&sums->list);
4555 			kfree(sums);
4556 		}
4557 		if (ret)
4558 			goto out;
4559 
4560 add_to_batch:
4561 		ins_sizes[dst_index] = btrfs_item_size(src, src_slot);
4562 		batch.total_data_size += ins_sizes[dst_index];
4563 		batch.nr++;
4564 		dst_index++;
4565 	}
4566 
4567 	/*
4568 	 * We have a leaf full of old extent items that don't need to be logged,
4569 	 * so we don't need to do anything.
4570 	 */
4571 	if (batch.nr == 0)
4572 		goto out;
4573 
4574 	ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
4575 	if (ret)
4576 		goto out;
4577 
4578 	dst_index = 0;
4579 	for (int i = 0; i < nr; i++) {
4580 		const int src_slot = start_slot + i;
4581 		const int dst_slot = dst_path->slots[0] + dst_index;
4582 		struct btrfs_key key;
4583 		unsigned long src_offset;
4584 		unsigned long dst_offset;
4585 
4586 		/*
4587 		 * We're done, all the remaining items in the source leaf
4588 		 * correspond to old file extent items.
4589 		 */
4590 		if (dst_index >= batch.nr)
4591 			break;
4592 
4593 		btrfs_item_key_to_cpu(src, &key, src_slot);
4594 
4595 		if (key.type != BTRFS_EXTENT_DATA_KEY)
4596 			goto copy_item;
4597 
4598 		extent = btrfs_item_ptr(src, src_slot,
4599 					struct btrfs_file_extent_item);
4600 
4601 		/* See the comment in the previous loop, same logic. */
4602 		if (btrfs_file_extent_generation(src, extent) < trans->transid &&
4603 		    key.offset < i_size &&
4604 		    inode->last_reflink_trans < trans->transid)
4605 			continue;
4606 
4607 copy_item:
4608 		dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot);
4609 		src_offset = btrfs_item_ptr_offset(src, src_slot);
4610 
4611 		if (key.type == BTRFS_INODE_ITEM_KEY) {
4612 			struct btrfs_inode_item *inode_item;
4613 
4614 			inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot,
4615 						    struct btrfs_inode_item);
4616 			fill_inode_item(trans, dst_path->nodes[0], inode_item,
4617 					&inode->vfs_inode,
4618 					inode_only == LOG_INODE_EXISTS,
4619 					logged_isize);
4620 		} else {
4621 			copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4622 					   src_offset, ins_sizes[dst_index]);
4623 		}
4624 
4625 		dst_index++;
4626 	}
4627 
4628 	btrfs_release_path(dst_path);
4629 out:
4630 	kfree(ins_data);
4631 
4632 	return ret;
4633 }
4634 
extent_cmp(void * priv,const struct list_head * a,const struct list_head * b)4635 static int extent_cmp(void *priv, const struct list_head *a,
4636 		      const struct list_head *b)
4637 {
4638 	const struct extent_map *em1, *em2;
4639 
4640 	em1 = list_entry(a, struct extent_map, list);
4641 	em2 = list_entry(b, struct extent_map, list);
4642 
4643 	if (em1->start < em2->start)
4644 		return -1;
4645 	else if (em1->start > em2->start)
4646 		return 1;
4647 	return 0;
4648 }
4649 
log_extent_csums(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_root * log_root,const struct extent_map * em,struct btrfs_log_ctx * ctx)4650 static int log_extent_csums(struct btrfs_trans_handle *trans,
4651 			    struct btrfs_inode *inode,
4652 			    struct btrfs_root *log_root,
4653 			    const struct extent_map *em,
4654 			    struct btrfs_log_ctx *ctx)
4655 {
4656 	struct btrfs_ordered_extent *ordered;
4657 	struct btrfs_root *csum_root;
4658 	u64 block_start;
4659 	u64 csum_offset;
4660 	u64 csum_len;
4661 	u64 mod_start = em->start;
4662 	u64 mod_len = em->len;
4663 	LIST_HEAD(ordered_sums);
4664 	int ret = 0;
4665 
4666 	if (inode->flags & BTRFS_INODE_NODATASUM ||
4667 	    (em->flags & EXTENT_FLAG_PREALLOC) ||
4668 	    em->disk_bytenr == EXTENT_MAP_HOLE)
4669 		return 0;
4670 
4671 	list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4672 		const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4673 		const u64 mod_end = mod_start + mod_len;
4674 		struct btrfs_ordered_sum *sums;
4675 
4676 		if (mod_len == 0)
4677 			break;
4678 
4679 		if (ordered_end <= mod_start)
4680 			continue;
4681 		if (mod_end <= ordered->file_offset)
4682 			break;
4683 
4684 		/*
4685 		 * We are going to copy all the csums on this ordered extent, so
4686 		 * go ahead and adjust mod_start and mod_len in case this ordered
4687 		 * extent has already been logged.
4688 		 */
4689 		if (ordered->file_offset > mod_start) {
4690 			if (ordered_end >= mod_end)
4691 				mod_len = ordered->file_offset - mod_start;
4692 			/*
4693 			 * If we have this case
4694 			 *
4695 			 * |--------- logged extent ---------|
4696 			 *       |----- ordered extent ----|
4697 			 *
4698 			 * Just don't mess with mod_start and mod_len, we'll
4699 			 * just end up logging more csums than we need and it
4700 			 * will be ok.
4701 			 */
4702 		} else {
4703 			if (ordered_end < mod_end) {
4704 				mod_len = mod_end - ordered_end;
4705 				mod_start = ordered_end;
4706 			} else {
4707 				mod_len = 0;
4708 			}
4709 		}
4710 
4711 		/*
4712 		 * To keep us from looping for the above case of an ordered
4713 		 * extent that falls inside of the logged extent.
4714 		 */
4715 		if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4716 			continue;
4717 
4718 		list_for_each_entry(sums, &ordered->list, list) {
4719 			ret = log_csums(trans, inode, log_root, sums);
4720 			if (ret)
4721 				return ret;
4722 		}
4723 	}
4724 
4725 	/* We're done, found all csums in the ordered extents. */
4726 	if (mod_len == 0)
4727 		return 0;
4728 
4729 	/* If we're compressed we have to save the entire range of csums. */
4730 	if (btrfs_extent_map_is_compressed(em)) {
4731 		csum_offset = 0;
4732 		csum_len = em->disk_num_bytes;
4733 	} else {
4734 		csum_offset = mod_start - em->start;
4735 		csum_len = mod_len;
4736 	}
4737 
4738 	/* block start is already adjusted for the file extent offset. */
4739 	block_start = btrfs_extent_map_block_start(em);
4740 	csum_root = btrfs_csum_root(trans->fs_info, block_start);
4741 	ret = btrfs_lookup_csums_list(csum_root, block_start + csum_offset,
4742 				      block_start + csum_offset + csum_len - 1,
4743 				      &ordered_sums, false);
4744 	if (ret < 0)
4745 		return ret;
4746 	ret = 0;
4747 
4748 	while (!list_empty(&ordered_sums)) {
4749 		struct btrfs_ordered_sum *sums = list_first_entry(&ordered_sums,
4750 								  struct btrfs_ordered_sum,
4751 								  list);
4752 		if (!ret)
4753 			ret = log_csums(trans, inode, log_root, sums);
4754 		list_del(&sums->list);
4755 		kfree(sums);
4756 	}
4757 
4758 	return ret;
4759 }
4760 
log_one_extent(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const struct extent_map * em,struct btrfs_path * path,struct btrfs_log_ctx * ctx)4761 static int log_one_extent(struct btrfs_trans_handle *trans,
4762 			  struct btrfs_inode *inode,
4763 			  const struct extent_map *em,
4764 			  struct btrfs_path *path,
4765 			  struct btrfs_log_ctx *ctx)
4766 {
4767 	struct btrfs_drop_extents_args drop_args = { 0 };
4768 	struct btrfs_root *log = inode->root->log_root;
4769 	struct btrfs_file_extent_item fi = { 0 };
4770 	struct extent_buffer *leaf;
4771 	struct btrfs_key key;
4772 	enum btrfs_compression_type compress_type;
4773 	u64 extent_offset = em->offset;
4774 	u64 block_start = btrfs_extent_map_block_start(em);
4775 	u64 block_len;
4776 	int ret;
4777 
4778 	btrfs_set_stack_file_extent_generation(&fi, trans->transid);
4779 	if (em->flags & EXTENT_FLAG_PREALLOC)
4780 		btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
4781 	else
4782 		btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
4783 
4784 	block_len = em->disk_num_bytes;
4785 	compress_type = btrfs_extent_map_compression(em);
4786 	if (compress_type != BTRFS_COMPRESS_NONE) {
4787 		btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start);
4788 		btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
4789 	} else if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
4790 		btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start - extent_offset);
4791 		btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
4792 	}
4793 
4794 	btrfs_set_stack_file_extent_offset(&fi, extent_offset);
4795 	btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
4796 	btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
4797 	btrfs_set_stack_file_extent_compression(&fi, compress_type);
4798 
4799 	ret = log_extent_csums(trans, inode, log, em, ctx);
4800 	if (ret)
4801 		return ret;
4802 
4803 	/*
4804 	 * If this is the first time we are logging the inode in the current
4805 	 * transaction, we can avoid btrfs_drop_extents(), which is expensive
4806 	 * because it does a deletion search, which always acquires write locks
4807 	 * for extent buffers at levels 2, 1 and 0. This not only wastes time
4808 	 * but also adds significant contention in a log tree, since log trees
4809 	 * are small, with a root at level 2 or 3 at most, due to their short
4810 	 * life span.
4811 	 */
4812 	if (ctx->logged_before) {
4813 		drop_args.path = path;
4814 		drop_args.start = em->start;
4815 		drop_args.end = em->start + em->len;
4816 		drop_args.replace_extent = true;
4817 		drop_args.extent_item_size = sizeof(fi);
4818 		ret = btrfs_drop_extents(trans, log, inode, &drop_args);
4819 		if (ret)
4820 			return ret;
4821 	}
4822 
4823 	if (!drop_args.extent_inserted) {
4824 		key.objectid = btrfs_ino(inode);
4825 		key.type = BTRFS_EXTENT_DATA_KEY;
4826 		key.offset = em->start;
4827 
4828 		ret = btrfs_insert_empty_item(trans, log, path, &key,
4829 					      sizeof(fi));
4830 		if (ret)
4831 			return ret;
4832 	}
4833 	leaf = path->nodes[0];
4834 	write_extent_buffer(leaf, &fi,
4835 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
4836 			    sizeof(fi));
4837 
4838 	btrfs_release_path(path);
4839 
4840 	return ret;
4841 }
4842 
4843 /*
4844  * Log all prealloc extents beyond the inode's i_size to make sure we do not
4845  * lose them after doing a full/fast fsync and replaying the log. We scan the
4846  * subvolume's root instead of iterating the inode's extent map tree because
4847  * otherwise we can log incorrect extent items based on extent map conversion.
4848  * That can happen due to the fact that extent maps are merged when they
4849  * are not in the extent map tree's list of modified extents.
4850  */
btrfs_log_prealloc_extents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_log_ctx * ctx)4851 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4852 				      struct btrfs_inode *inode,
4853 				      struct btrfs_path *path,
4854 				      struct btrfs_log_ctx *ctx)
4855 {
4856 	struct btrfs_root *root = inode->root;
4857 	struct btrfs_key key;
4858 	const u64 i_size = i_size_read(&inode->vfs_inode);
4859 	const u64 ino = btrfs_ino(inode);
4860 	struct btrfs_path *dst_path = NULL;
4861 	bool dropped_extents = false;
4862 	u64 truncate_offset = i_size;
4863 	struct extent_buffer *leaf;
4864 	int slot;
4865 	int ins_nr = 0;
4866 	int start_slot = 0;
4867 	int ret;
4868 
4869 	if (!(inode->flags & BTRFS_INODE_PREALLOC))
4870 		return 0;
4871 
4872 	key.objectid = ino;
4873 	key.type = BTRFS_EXTENT_DATA_KEY;
4874 	key.offset = i_size;
4875 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4876 	if (ret < 0)
4877 		goto out;
4878 
4879 	/*
4880 	 * We must check if there is a prealloc extent that starts before the
4881 	 * i_size and crosses the i_size boundary. This is to ensure later we
4882 	 * truncate down to the end of that extent and not to the i_size, as
4883 	 * otherwise we end up losing part of the prealloc extent after a log
4884 	 * replay and with an implicit hole if there is another prealloc extent
4885 	 * that starts at an offset beyond i_size.
4886 	 */
4887 	ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4888 	if (ret < 0)
4889 		goto out;
4890 
4891 	if (ret == 0) {
4892 		struct btrfs_file_extent_item *ei;
4893 
4894 		leaf = path->nodes[0];
4895 		slot = path->slots[0];
4896 		ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4897 
4898 		if (btrfs_file_extent_type(leaf, ei) ==
4899 		    BTRFS_FILE_EXTENT_PREALLOC) {
4900 			u64 extent_end;
4901 
4902 			btrfs_item_key_to_cpu(leaf, &key, slot);
4903 			extent_end = key.offset +
4904 				btrfs_file_extent_num_bytes(leaf, ei);
4905 
4906 			if (extent_end > i_size)
4907 				truncate_offset = extent_end;
4908 		}
4909 	} else {
4910 		ret = 0;
4911 	}
4912 
4913 	while (true) {
4914 		leaf = path->nodes[0];
4915 		slot = path->slots[0];
4916 
4917 		if (slot >= btrfs_header_nritems(leaf)) {
4918 			if (ins_nr > 0) {
4919 				ret = copy_items(trans, inode, dst_path, path,
4920 						 start_slot, ins_nr, 1, 0, ctx);
4921 				if (ret < 0)
4922 					goto out;
4923 				ins_nr = 0;
4924 			}
4925 			ret = btrfs_next_leaf(root, path);
4926 			if (ret < 0)
4927 				goto out;
4928 			if (ret > 0) {
4929 				ret = 0;
4930 				break;
4931 			}
4932 			continue;
4933 		}
4934 
4935 		btrfs_item_key_to_cpu(leaf, &key, slot);
4936 		if (key.objectid > ino)
4937 			break;
4938 		if (WARN_ON_ONCE(key.objectid < ino) ||
4939 		    key.type < BTRFS_EXTENT_DATA_KEY ||
4940 		    key.offset < i_size) {
4941 			path->slots[0]++;
4942 			continue;
4943 		}
4944 		/*
4945 		 * Avoid overlapping items in the log tree. The first time we
4946 		 * get here, get rid of everything from a past fsync. After
4947 		 * that, if the current extent starts before the end of the last
4948 		 * extent we copied, truncate the last one. This can happen if
4949 		 * an ordered extent completion modifies the subvolume tree
4950 		 * while btrfs_next_leaf() has the tree unlocked.
4951 		 */
4952 		if (!dropped_extents || key.offset < truncate_offset) {
4953 			ret = truncate_inode_items(trans, root->log_root, inode,
4954 						   min(key.offset, truncate_offset),
4955 						   BTRFS_EXTENT_DATA_KEY);
4956 			if (ret)
4957 				goto out;
4958 			dropped_extents = true;
4959 		}
4960 		truncate_offset = btrfs_file_extent_end(path);
4961 		if (ins_nr == 0)
4962 			start_slot = slot;
4963 		ins_nr++;
4964 		path->slots[0]++;
4965 		if (!dst_path) {
4966 			dst_path = btrfs_alloc_path();
4967 			if (!dst_path) {
4968 				ret = -ENOMEM;
4969 				goto out;
4970 			}
4971 		}
4972 	}
4973 	if (ins_nr > 0)
4974 		ret = copy_items(trans, inode, dst_path, path,
4975 				 start_slot, ins_nr, 1, 0, ctx);
4976 out:
4977 	btrfs_release_path(path);
4978 	btrfs_free_path(dst_path);
4979 	return ret;
4980 }
4981 
btrfs_log_changed_extents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_log_ctx * ctx)4982 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4983 				     struct btrfs_inode *inode,
4984 				     struct btrfs_path *path,
4985 				     struct btrfs_log_ctx *ctx)
4986 {
4987 	struct btrfs_ordered_extent *ordered;
4988 	struct btrfs_ordered_extent *tmp;
4989 	struct extent_map *em, *n;
4990 	LIST_HEAD(extents);
4991 	struct extent_map_tree *tree = &inode->extent_tree;
4992 	int ret = 0;
4993 	int num = 0;
4994 
4995 	write_lock(&tree->lock);
4996 
4997 	list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4998 		list_del_init(&em->list);
4999 		/*
5000 		 * Just an arbitrary number, this can be really CPU intensive
5001 		 * once we start getting a lot of extents, and really once we
5002 		 * have a bunch of extents we just want to commit since it will
5003 		 * be faster.
5004 		 */
5005 		if (++num > 32768) {
5006 			list_del_init(&tree->modified_extents);
5007 			ret = -EFBIG;
5008 			goto process;
5009 		}
5010 
5011 		if (em->generation < trans->transid)
5012 			continue;
5013 
5014 		/* We log prealloc extents beyond eof later. */
5015 		if ((em->flags & EXTENT_FLAG_PREALLOC) &&
5016 		    em->start >= i_size_read(&inode->vfs_inode))
5017 			continue;
5018 
5019 		/* Need a ref to keep it from getting evicted from cache */
5020 		refcount_inc(&em->refs);
5021 		em->flags |= EXTENT_FLAG_LOGGING;
5022 		list_add_tail(&em->list, &extents);
5023 		num++;
5024 	}
5025 
5026 	list_sort(NULL, &extents, extent_cmp);
5027 process:
5028 	while (!list_empty(&extents)) {
5029 		em = list_first_entry(&extents, struct extent_map, list);
5030 
5031 		list_del_init(&em->list);
5032 
5033 		/*
5034 		 * If we had an error we just need to delete everybody from our
5035 		 * private list.
5036 		 */
5037 		if (ret) {
5038 			btrfs_clear_em_logging(inode, em);
5039 			btrfs_free_extent_map(em);
5040 			continue;
5041 		}
5042 
5043 		write_unlock(&tree->lock);
5044 
5045 		ret = log_one_extent(trans, inode, em, path, ctx);
5046 		write_lock(&tree->lock);
5047 		btrfs_clear_em_logging(inode, em);
5048 		btrfs_free_extent_map(em);
5049 	}
5050 	WARN_ON(!list_empty(&extents));
5051 	write_unlock(&tree->lock);
5052 
5053 	if (!ret)
5054 		ret = btrfs_log_prealloc_extents(trans, inode, path, ctx);
5055 	if (ret)
5056 		return ret;
5057 
5058 	/*
5059 	 * We have logged all extents successfully, now make sure the commit of
5060 	 * the current transaction waits for the ordered extents to complete
5061 	 * before it commits and wipes out the log trees, otherwise we would
5062 	 * lose data if an ordered extents completes after the transaction
5063 	 * commits and a power failure happens after the transaction commit.
5064 	 */
5065 	list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
5066 		list_del_init(&ordered->log_list);
5067 		set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
5068 
5069 		if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
5070 			spin_lock_irq(&inode->ordered_tree_lock);
5071 			if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
5072 				set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
5073 				atomic_inc(&trans->transaction->pending_ordered);
5074 			}
5075 			spin_unlock_irq(&inode->ordered_tree_lock);
5076 		}
5077 		btrfs_put_ordered_extent(ordered);
5078 	}
5079 
5080 	return 0;
5081 }
5082 
logged_inode_size(struct btrfs_root * log,struct btrfs_inode * inode,struct btrfs_path * path,u64 * size_ret)5083 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
5084 			     struct btrfs_path *path, u64 *size_ret)
5085 {
5086 	struct btrfs_key key;
5087 	int ret;
5088 
5089 	key.objectid = btrfs_ino(inode);
5090 	key.type = BTRFS_INODE_ITEM_KEY;
5091 	key.offset = 0;
5092 
5093 	ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
5094 	if (ret < 0) {
5095 		return ret;
5096 	} else if (ret > 0) {
5097 		*size_ret = 0;
5098 	} else {
5099 		struct btrfs_inode_item *item;
5100 
5101 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5102 				      struct btrfs_inode_item);
5103 		*size_ret = btrfs_inode_size(path->nodes[0], item);
5104 		/*
5105 		 * If the in-memory inode's i_size is smaller then the inode
5106 		 * size stored in the btree, return the inode's i_size, so
5107 		 * that we get a correct inode size after replaying the log
5108 		 * when before a power failure we had a shrinking truncate
5109 		 * followed by addition of a new name (rename / new hard link).
5110 		 * Otherwise return the inode size from the btree, to avoid
5111 		 * data loss when replaying a log due to previously doing a
5112 		 * write that expands the inode's size and logging a new name
5113 		 * immediately after.
5114 		 */
5115 		if (*size_ret > inode->vfs_inode.i_size)
5116 			*size_ret = inode->vfs_inode.i_size;
5117 	}
5118 
5119 	btrfs_release_path(path);
5120 	return 0;
5121 }
5122 
5123 /*
5124  * At the moment we always log all xattrs. This is to figure out at log replay
5125  * time which xattrs must have their deletion replayed. If a xattr is missing
5126  * in the log tree and exists in the fs/subvol tree, we delete it. This is
5127  * because if a xattr is deleted, the inode is fsynced and a power failure
5128  * happens, causing the log to be replayed the next time the fs is mounted,
5129  * we want the xattr to not exist anymore (same behaviour as other filesystems
5130  * with a journal, ext3/4, xfs, f2fs, etc).
5131  */
btrfs_log_all_xattrs(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,struct btrfs_log_ctx * ctx)5132 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
5133 				struct btrfs_inode *inode,
5134 				struct btrfs_path *path,
5135 				struct btrfs_path *dst_path,
5136 				struct btrfs_log_ctx *ctx)
5137 {
5138 	struct btrfs_root *root = inode->root;
5139 	int ret;
5140 	struct btrfs_key key;
5141 	const u64 ino = btrfs_ino(inode);
5142 	int ins_nr = 0;
5143 	int start_slot = 0;
5144 	bool found_xattrs = false;
5145 
5146 	if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
5147 		return 0;
5148 
5149 	key.objectid = ino;
5150 	key.type = BTRFS_XATTR_ITEM_KEY;
5151 	key.offset = 0;
5152 
5153 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5154 	if (ret < 0)
5155 		return ret;
5156 
5157 	while (true) {
5158 		int slot = path->slots[0];
5159 		struct extent_buffer *leaf = path->nodes[0];
5160 		int nritems = btrfs_header_nritems(leaf);
5161 
5162 		if (slot >= nritems) {
5163 			if (ins_nr > 0) {
5164 				ret = copy_items(trans, inode, dst_path, path,
5165 						 start_slot, ins_nr, 1, 0, ctx);
5166 				if (ret < 0)
5167 					return ret;
5168 				ins_nr = 0;
5169 			}
5170 			ret = btrfs_next_leaf(root, path);
5171 			if (ret < 0)
5172 				return ret;
5173 			else if (ret > 0)
5174 				break;
5175 			continue;
5176 		}
5177 
5178 		btrfs_item_key_to_cpu(leaf, &key, slot);
5179 		if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
5180 			break;
5181 
5182 		if (ins_nr == 0)
5183 			start_slot = slot;
5184 		ins_nr++;
5185 		path->slots[0]++;
5186 		found_xattrs = true;
5187 		cond_resched();
5188 	}
5189 	if (ins_nr > 0) {
5190 		ret = copy_items(trans, inode, dst_path, path,
5191 				 start_slot, ins_nr, 1, 0, ctx);
5192 		if (ret < 0)
5193 			return ret;
5194 	}
5195 
5196 	if (!found_xattrs)
5197 		set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
5198 
5199 	return 0;
5200 }
5201 
5202 /*
5203  * When using the NO_HOLES feature if we punched a hole that causes the
5204  * deletion of entire leafs or all the extent items of the first leaf (the one
5205  * that contains the inode item and references) we may end up not processing
5206  * any extents, because there are no leafs with a generation matching the
5207  * current transaction that have extent items for our inode. So we need to find
5208  * if any holes exist and then log them. We also need to log holes after any
5209  * truncate operation that changes the inode's size.
5210  */
btrfs_log_holes(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path)5211 static int btrfs_log_holes(struct btrfs_trans_handle *trans,
5212 			   struct btrfs_inode *inode,
5213 			   struct btrfs_path *path)
5214 {
5215 	struct btrfs_root *root = inode->root;
5216 	struct btrfs_fs_info *fs_info = root->fs_info;
5217 	struct btrfs_key key;
5218 	const u64 ino = btrfs_ino(inode);
5219 	const u64 i_size = i_size_read(&inode->vfs_inode);
5220 	u64 prev_extent_end = 0;
5221 	int ret;
5222 
5223 	if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
5224 		return 0;
5225 
5226 	key.objectid = ino;
5227 	key.type = BTRFS_EXTENT_DATA_KEY;
5228 	key.offset = 0;
5229 
5230 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5231 	if (ret < 0)
5232 		return ret;
5233 
5234 	while (true) {
5235 		struct extent_buffer *leaf = path->nodes[0];
5236 
5237 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5238 			ret = btrfs_next_leaf(root, path);
5239 			if (ret < 0)
5240 				return ret;
5241 			if (ret > 0) {
5242 				ret = 0;
5243 				break;
5244 			}
5245 			leaf = path->nodes[0];
5246 		}
5247 
5248 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5249 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
5250 			break;
5251 
5252 		/* We have a hole, log it. */
5253 		if (prev_extent_end < key.offset) {
5254 			const u64 hole_len = key.offset - prev_extent_end;
5255 
5256 			/*
5257 			 * Release the path to avoid deadlocks with other code
5258 			 * paths that search the root while holding locks on
5259 			 * leafs from the log root.
5260 			 */
5261 			btrfs_release_path(path);
5262 			ret = btrfs_insert_hole_extent(trans, root->log_root,
5263 						       ino, prev_extent_end,
5264 						       hole_len);
5265 			if (ret < 0)
5266 				return ret;
5267 
5268 			/*
5269 			 * Search for the same key again in the root. Since it's
5270 			 * an extent item and we are holding the inode lock, the
5271 			 * key must still exist. If it doesn't just emit warning
5272 			 * and return an error to fall back to a transaction
5273 			 * commit.
5274 			 */
5275 			ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5276 			if (ret < 0)
5277 				return ret;
5278 			if (WARN_ON(ret > 0))
5279 				return -ENOENT;
5280 			leaf = path->nodes[0];
5281 		}
5282 
5283 		prev_extent_end = btrfs_file_extent_end(path);
5284 		path->slots[0]++;
5285 		cond_resched();
5286 	}
5287 
5288 	if (prev_extent_end < i_size) {
5289 		u64 hole_len;
5290 
5291 		btrfs_release_path(path);
5292 		hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
5293 		ret = btrfs_insert_hole_extent(trans, root->log_root, ino,
5294 					       prev_extent_end, hole_len);
5295 		if (ret < 0)
5296 			return ret;
5297 	}
5298 
5299 	return 0;
5300 }
5301 
5302 /*
5303  * When we are logging a new inode X, check if it doesn't have a reference that
5304  * matches the reference from some other inode Y created in a past transaction
5305  * and that was renamed in the current transaction. If we don't do this, then at
5306  * log replay time we can lose inode Y (and all its files if it's a directory):
5307  *
5308  * mkdir /mnt/x
5309  * echo "hello world" > /mnt/x/foobar
5310  * sync
5311  * mv /mnt/x /mnt/y
5312  * mkdir /mnt/x                 # or touch /mnt/x
5313  * xfs_io -c fsync /mnt/x
5314  * <power fail>
5315  * mount fs, trigger log replay
5316  *
5317  * After the log replay procedure, we would lose the first directory and all its
5318  * files (file foobar).
5319  * For the case where inode Y is not a directory we simply end up losing it:
5320  *
5321  * echo "123" > /mnt/foo
5322  * sync
5323  * mv /mnt/foo /mnt/bar
5324  * echo "abc" > /mnt/foo
5325  * xfs_io -c fsync /mnt/foo
5326  * <power fail>
5327  *
5328  * We also need this for cases where a snapshot entry is replaced by some other
5329  * entry (file or directory) otherwise we end up with an unreplayable log due to
5330  * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
5331  * if it were a regular entry:
5332  *
5333  * mkdir /mnt/x
5334  * btrfs subvolume snapshot /mnt /mnt/x/snap
5335  * btrfs subvolume delete /mnt/x/snap
5336  * rmdir /mnt/x
5337  * mkdir /mnt/x
5338  * fsync /mnt/x or fsync some new file inside it
5339  * <power fail>
5340  *
5341  * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
5342  * the same transaction.
5343  */
btrfs_check_ref_name_override(struct extent_buffer * eb,const int slot,const struct btrfs_key * key,struct btrfs_inode * inode,u64 * other_ino,u64 * other_parent)5344 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
5345 					 const int slot,
5346 					 const struct btrfs_key *key,
5347 					 struct btrfs_inode *inode,
5348 					 u64 *other_ino, u64 *other_parent)
5349 {
5350 	int ret;
5351 	struct btrfs_path *search_path;
5352 	char *name = NULL;
5353 	u32 name_len = 0;
5354 	u32 item_size = btrfs_item_size(eb, slot);
5355 	u32 cur_offset = 0;
5356 	unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
5357 
5358 	search_path = btrfs_alloc_path();
5359 	if (!search_path)
5360 		return -ENOMEM;
5361 	search_path->search_commit_root = 1;
5362 	search_path->skip_locking = 1;
5363 
5364 	while (cur_offset < item_size) {
5365 		u64 parent;
5366 		u32 this_name_len;
5367 		u32 this_len;
5368 		unsigned long name_ptr;
5369 		struct btrfs_dir_item *di;
5370 		struct fscrypt_str name_str;
5371 
5372 		if (key->type == BTRFS_INODE_REF_KEY) {
5373 			struct btrfs_inode_ref *iref;
5374 
5375 			iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
5376 			parent = key->offset;
5377 			this_name_len = btrfs_inode_ref_name_len(eb, iref);
5378 			name_ptr = (unsigned long)(iref + 1);
5379 			this_len = sizeof(*iref) + this_name_len;
5380 		} else {
5381 			struct btrfs_inode_extref *extref;
5382 
5383 			extref = (struct btrfs_inode_extref *)(ptr +
5384 							       cur_offset);
5385 			parent = btrfs_inode_extref_parent(eb, extref);
5386 			this_name_len = btrfs_inode_extref_name_len(eb, extref);
5387 			name_ptr = (unsigned long)&extref->name;
5388 			this_len = sizeof(*extref) + this_name_len;
5389 		}
5390 
5391 		if (this_name_len > name_len) {
5392 			char *new_name;
5393 
5394 			new_name = krealloc(name, this_name_len, GFP_NOFS);
5395 			if (!new_name) {
5396 				ret = -ENOMEM;
5397 				goto out;
5398 			}
5399 			name_len = this_name_len;
5400 			name = new_name;
5401 		}
5402 
5403 		read_extent_buffer(eb, name, name_ptr, this_name_len);
5404 
5405 		name_str.name = name;
5406 		name_str.len = this_name_len;
5407 		di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
5408 				parent, &name_str, 0);
5409 		if (di && !IS_ERR(di)) {
5410 			struct btrfs_key di_key;
5411 
5412 			btrfs_dir_item_key_to_cpu(search_path->nodes[0],
5413 						  di, &di_key);
5414 			if (di_key.type == BTRFS_INODE_ITEM_KEY) {
5415 				if (di_key.objectid != key->objectid) {
5416 					ret = 1;
5417 					*other_ino = di_key.objectid;
5418 					*other_parent = parent;
5419 				} else {
5420 					ret = 0;
5421 				}
5422 			} else {
5423 				ret = -EAGAIN;
5424 			}
5425 			goto out;
5426 		} else if (IS_ERR(di)) {
5427 			ret = PTR_ERR(di);
5428 			goto out;
5429 		}
5430 		btrfs_release_path(search_path);
5431 
5432 		cur_offset += this_len;
5433 	}
5434 	ret = 0;
5435 out:
5436 	btrfs_free_path(search_path);
5437 	kfree(name);
5438 	return ret;
5439 }
5440 
5441 /*
5442  * Check if we need to log an inode. This is used in contexts where while
5443  * logging an inode we need to log another inode (either that it exists or in
5444  * full mode). This is used instead of btrfs_inode_in_log() because the later
5445  * requires the inode to be in the log and have the log transaction committed,
5446  * while here we do not care if the log transaction was already committed - our
5447  * caller will commit the log later - and we want to avoid logging an inode
5448  * multiple times when multiple tasks have joined the same log transaction.
5449  */
need_log_inode(const struct btrfs_trans_handle * trans,struct btrfs_inode * inode)5450 static bool need_log_inode(const struct btrfs_trans_handle *trans,
5451 			   struct btrfs_inode *inode)
5452 {
5453 	/*
5454 	 * If a directory was not modified, no dentries added or removed, we can
5455 	 * and should avoid logging it.
5456 	 */
5457 	if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
5458 		return false;
5459 
5460 	/*
5461 	 * If this inode does not have new/updated/deleted xattrs since the last
5462 	 * time it was logged and is flagged as logged in the current transaction,
5463 	 * we can skip logging it. As for new/deleted names, those are updated in
5464 	 * the log by link/unlink/rename operations.
5465 	 * In case the inode was logged and then evicted and reloaded, its
5466 	 * logged_trans will be 0, in which case we have to fully log it since
5467 	 * logged_trans is a transient field, not persisted.
5468 	 */
5469 	if (inode_logged(trans, inode, NULL) == 1 &&
5470 	    !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
5471 		return false;
5472 
5473 	return true;
5474 }
5475 
5476 struct btrfs_dir_list {
5477 	u64 ino;
5478 	struct list_head list;
5479 };
5480 
5481 /*
5482  * Log the inodes of the new dentries of a directory.
5483  * See process_dir_items_leaf() for details about why it is needed.
5484  * This is a recursive operation - if an existing dentry corresponds to a
5485  * directory, that directory's new entries are logged too (same behaviour as
5486  * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5487  * the dentries point to we do not acquire their VFS lock, otherwise lockdep
5488  * complains about the following circular lock dependency / possible deadlock:
5489  *
5490  *        CPU0                                        CPU1
5491  *        ----                                        ----
5492  * lock(&type->i_mutex_dir_key#3/2);
5493  *                                            lock(sb_internal#2);
5494  *                                            lock(&type->i_mutex_dir_key#3/2);
5495  * lock(&sb->s_type->i_mutex_key#14);
5496  *
5497  * Where sb_internal is the lock (a counter that works as a lock) acquired by
5498  * sb_start_intwrite() in btrfs_start_transaction().
5499  * Not acquiring the VFS lock of the inodes is still safe because:
5500  *
5501  * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5502  *    that while logging the inode new references (names) are added or removed
5503  *    from the inode, leaving the logged inode item with a link count that does
5504  *    not match the number of logged inode reference items. This is fine because
5505  *    at log replay time we compute the real number of links and correct the
5506  *    link count in the inode item (see replay_one_buffer() and
5507  *    link_to_fixup_dir());
5508  *
5509  * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5510  *    while logging the inode's items new index items (key type
5511  *    BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item
5512  *    has a size that doesn't match the sum of the lengths of all the logged
5513  *    names - this is ok, not a problem, because at log replay time we set the
5514  *    directory's i_size to the correct value (see replay_one_name() and
5515  *    overwrite_item()).
5516  */
log_new_dir_dentries(struct btrfs_trans_handle * trans,struct btrfs_inode * start_inode,struct btrfs_log_ctx * ctx)5517 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5518 				struct btrfs_inode *start_inode,
5519 				struct btrfs_log_ctx *ctx)
5520 {
5521 	struct btrfs_root *root = start_inode->root;
5522 	struct btrfs_path *path;
5523 	LIST_HEAD(dir_list);
5524 	struct btrfs_dir_list *dir_elem;
5525 	u64 ino = btrfs_ino(start_inode);
5526 	struct btrfs_inode *curr_inode = start_inode;
5527 	int ret = 0;
5528 
5529 	/*
5530 	 * If we are logging a new name, as part of a link or rename operation,
5531 	 * don't bother logging new dentries, as we just want to log the names
5532 	 * of an inode and that any new parents exist.
5533 	 */
5534 	if (ctx->logging_new_name)
5535 		return 0;
5536 
5537 	path = btrfs_alloc_path();
5538 	if (!path)
5539 		return -ENOMEM;
5540 
5541 	/* Pairs with btrfs_add_delayed_iput below. */
5542 	ihold(&curr_inode->vfs_inode);
5543 
5544 	while (true) {
5545 		struct btrfs_key key;
5546 		struct btrfs_key found_key;
5547 		u64 next_index;
5548 		bool continue_curr_inode = true;
5549 		int iter_ret;
5550 
5551 		key.objectid = ino;
5552 		key.type = BTRFS_DIR_INDEX_KEY;
5553 		key.offset = btrfs_get_first_dir_index_to_log(curr_inode);
5554 		next_index = key.offset;
5555 again:
5556 		btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) {
5557 			struct extent_buffer *leaf = path->nodes[0];
5558 			struct btrfs_dir_item *di;
5559 			struct btrfs_key di_key;
5560 			struct btrfs_inode *di_inode;
5561 			int log_mode = LOG_INODE_EXISTS;
5562 			int type;
5563 
5564 			if (found_key.objectid != ino ||
5565 			    found_key.type != BTRFS_DIR_INDEX_KEY) {
5566 				continue_curr_inode = false;
5567 				break;
5568 			}
5569 
5570 			next_index = found_key.offset + 1;
5571 
5572 			di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
5573 			type = btrfs_dir_ftype(leaf, di);
5574 			if (btrfs_dir_transid(leaf, di) < trans->transid)
5575 				continue;
5576 			btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5577 			if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5578 				continue;
5579 
5580 			btrfs_release_path(path);
5581 			di_inode = btrfs_iget_logging(di_key.objectid, root);
5582 			if (IS_ERR(di_inode)) {
5583 				ret = PTR_ERR(di_inode);
5584 				goto out;
5585 			}
5586 
5587 			if (!need_log_inode(trans, di_inode)) {
5588 				btrfs_add_delayed_iput(di_inode);
5589 				break;
5590 			}
5591 
5592 			ctx->log_new_dentries = false;
5593 			if (type == BTRFS_FT_DIR)
5594 				log_mode = LOG_INODE_ALL;
5595 			ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
5596 			btrfs_add_delayed_iput(di_inode);
5597 			if (ret)
5598 				goto out;
5599 			if (ctx->log_new_dentries) {
5600 				dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5601 				if (!dir_elem) {
5602 					ret = -ENOMEM;
5603 					goto out;
5604 				}
5605 				dir_elem->ino = di_key.objectid;
5606 				list_add_tail(&dir_elem->list, &dir_list);
5607 			}
5608 			break;
5609 		}
5610 
5611 		btrfs_release_path(path);
5612 
5613 		if (iter_ret < 0) {
5614 			ret = iter_ret;
5615 			goto out;
5616 		} else if (iter_ret > 0) {
5617 			continue_curr_inode = false;
5618 		} else {
5619 			key = found_key;
5620 		}
5621 
5622 		if (continue_curr_inode && key.offset < (u64)-1) {
5623 			key.offset++;
5624 			goto again;
5625 		}
5626 
5627 		btrfs_set_first_dir_index_to_log(curr_inode, next_index);
5628 
5629 		if (list_empty(&dir_list))
5630 			break;
5631 
5632 		dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
5633 		ino = dir_elem->ino;
5634 		list_del(&dir_elem->list);
5635 		kfree(dir_elem);
5636 
5637 		btrfs_add_delayed_iput(curr_inode);
5638 
5639 		curr_inode = btrfs_iget_logging(ino, root);
5640 		if (IS_ERR(curr_inode)) {
5641 			ret = PTR_ERR(curr_inode);
5642 			curr_inode = NULL;
5643 			break;
5644 		}
5645 	}
5646 out:
5647 	btrfs_free_path(path);
5648 	if (curr_inode)
5649 		btrfs_add_delayed_iput(curr_inode);
5650 
5651 	if (ret) {
5652 		struct btrfs_dir_list *next;
5653 
5654 		list_for_each_entry_safe(dir_elem, next, &dir_list, list)
5655 			kfree(dir_elem);
5656 	}
5657 
5658 	return ret;
5659 }
5660 
5661 struct btrfs_ino_list {
5662 	u64 ino;
5663 	u64 parent;
5664 	struct list_head list;
5665 };
5666 
free_conflicting_inodes(struct btrfs_log_ctx * ctx)5667 static void free_conflicting_inodes(struct btrfs_log_ctx *ctx)
5668 {
5669 	struct btrfs_ino_list *curr;
5670 	struct btrfs_ino_list *next;
5671 
5672 	list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) {
5673 		list_del(&curr->list);
5674 		kfree(curr);
5675 	}
5676 }
5677 
conflicting_inode_is_dir(struct btrfs_root * root,u64 ino,struct btrfs_path * path)5678 static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino,
5679 				    struct btrfs_path *path)
5680 {
5681 	struct btrfs_key key;
5682 	int ret;
5683 
5684 	key.objectid = ino;
5685 	key.type = BTRFS_INODE_ITEM_KEY;
5686 	key.offset = 0;
5687 
5688 	path->search_commit_root = 1;
5689 	path->skip_locking = 1;
5690 
5691 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5692 	if (WARN_ON_ONCE(ret > 0)) {
5693 		/*
5694 		 * We have previously found the inode through the commit root
5695 		 * so this should not happen. If it does, just error out and
5696 		 * fallback to a transaction commit.
5697 		 */
5698 		ret = -ENOENT;
5699 	} else if (ret == 0) {
5700 		struct btrfs_inode_item *item;
5701 
5702 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5703 				      struct btrfs_inode_item);
5704 		if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item)))
5705 			ret = 1;
5706 	}
5707 
5708 	btrfs_release_path(path);
5709 	path->search_commit_root = 0;
5710 	path->skip_locking = 0;
5711 
5712 	return ret;
5713 }
5714 
add_conflicting_inode(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 ino,u64 parent,struct btrfs_log_ctx * ctx)5715 static int add_conflicting_inode(struct btrfs_trans_handle *trans,
5716 				 struct btrfs_root *root,
5717 				 struct btrfs_path *path,
5718 				 u64 ino, u64 parent,
5719 				 struct btrfs_log_ctx *ctx)
5720 {
5721 	struct btrfs_ino_list *ino_elem;
5722 	struct btrfs_inode *inode;
5723 
5724 	/*
5725 	 * It's rare to have a lot of conflicting inodes, in practice it is not
5726 	 * common to have more than 1 or 2. We don't want to collect too many,
5727 	 * as we could end up logging too many inodes (even if only in
5728 	 * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction
5729 	 * commits.
5730 	 */
5731 	if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES)
5732 		return BTRFS_LOG_FORCE_COMMIT;
5733 
5734 	inode = btrfs_iget_logging(ino, root);
5735 	/*
5736 	 * If the other inode that had a conflicting dir entry was deleted in
5737 	 * the current transaction then we either:
5738 	 *
5739 	 * 1) Log the parent directory (later after adding it to the list) if
5740 	 *    the inode is a directory. This is because it may be a deleted
5741 	 *    subvolume/snapshot or it may be a regular directory that had
5742 	 *    deleted subvolumes/snapshots (or subdirectories that had them),
5743 	 *    and at the moment we can't deal with dropping subvolumes/snapshots
5744 	 *    during log replay. So we just log the parent, which will result in
5745 	 *    a fallback to a transaction commit if we are dealing with those
5746 	 *    cases (last_unlink_trans will match the current transaction);
5747 	 *
5748 	 * 2) Do nothing if it's not a directory. During log replay we simply
5749 	 *    unlink the conflicting dentry from the parent directory and then
5750 	 *    add the dentry for our inode. Like this we can avoid logging the
5751 	 *    parent directory (and maybe fallback to a transaction commit in
5752 	 *    case it has a last_unlink_trans == trans->transid, due to moving
5753 	 *    some inode from it to some other directory).
5754 	 */
5755 	if (IS_ERR(inode)) {
5756 		int ret = PTR_ERR(inode);
5757 
5758 		if (ret != -ENOENT)
5759 			return ret;
5760 
5761 		ret = conflicting_inode_is_dir(root, ino, path);
5762 		/* Not a directory or we got an error. */
5763 		if (ret <= 0)
5764 			return ret;
5765 
5766 		/* Conflicting inode is a directory, so we'll log its parent. */
5767 		ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5768 		if (!ino_elem)
5769 			return -ENOMEM;
5770 		ino_elem->ino = ino;
5771 		ino_elem->parent = parent;
5772 		list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
5773 		ctx->num_conflict_inodes++;
5774 
5775 		return 0;
5776 	}
5777 
5778 	/*
5779 	 * If the inode was already logged skip it - otherwise we can hit an
5780 	 * infinite loop. Example:
5781 	 *
5782 	 * From the commit root (previous transaction) we have the following
5783 	 * inodes:
5784 	 *
5785 	 * inode 257 a directory
5786 	 * inode 258 with references "zz" and "zz_link" on inode 257
5787 	 * inode 259 with reference "a" on inode 257
5788 	 *
5789 	 * And in the current (uncommitted) transaction we have:
5790 	 *
5791 	 * inode 257 a directory, unchanged
5792 	 * inode 258 with references "a" and "a2" on inode 257
5793 	 * inode 259 with reference "zz_link" on inode 257
5794 	 * inode 261 with reference "zz" on inode 257
5795 	 *
5796 	 * When logging inode 261 the following infinite loop could
5797 	 * happen if we don't skip already logged inodes:
5798 	 *
5799 	 * - we detect inode 258 as a conflicting inode, with inode 261
5800 	 *   on reference "zz", and log it;
5801 	 *
5802 	 * - we detect inode 259 as a conflicting inode, with inode 258
5803 	 *   on reference "a", and log it;
5804 	 *
5805 	 * - we detect inode 258 as a conflicting inode, with inode 259
5806 	 *   on reference "zz_link", and log it - again! After this we
5807 	 *   repeat the above steps forever.
5808 	 *
5809 	 * Here we can use need_log_inode() because we only need to log the
5810 	 * inode in LOG_INODE_EXISTS mode and rename operations update the log,
5811 	 * so that the log ends up with the new name and without the old name.
5812 	 */
5813 	if (!need_log_inode(trans, inode)) {
5814 		btrfs_add_delayed_iput(inode);
5815 		return 0;
5816 	}
5817 
5818 	btrfs_add_delayed_iput(inode);
5819 
5820 	ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5821 	if (!ino_elem)
5822 		return -ENOMEM;
5823 	ino_elem->ino = ino;
5824 	ino_elem->parent = parent;
5825 	list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
5826 	ctx->num_conflict_inodes++;
5827 
5828 	return 0;
5829 }
5830 
log_conflicting_inodes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_log_ctx * ctx)5831 static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
5832 				  struct btrfs_root *root,
5833 				  struct btrfs_log_ctx *ctx)
5834 {
5835 	int ret = 0;
5836 
5837 	/*
5838 	 * Conflicting inodes are logged by the first call to btrfs_log_inode(),
5839 	 * otherwise we could have unbounded recursion of btrfs_log_inode()
5840 	 * calls. This check guarantees we can have only 1 level of recursion.
5841 	 */
5842 	if (ctx->logging_conflict_inodes)
5843 		return 0;
5844 
5845 	ctx->logging_conflict_inodes = true;
5846 
5847 	/*
5848 	 * New conflicting inodes may be found and added to the list while we
5849 	 * are logging a conflicting inode, so keep iterating while the list is
5850 	 * not empty.
5851 	 */
5852 	while (!list_empty(&ctx->conflict_inodes)) {
5853 		struct btrfs_ino_list *curr;
5854 		struct btrfs_inode *inode;
5855 		u64 ino;
5856 		u64 parent;
5857 
5858 		curr = list_first_entry(&ctx->conflict_inodes,
5859 					struct btrfs_ino_list, list);
5860 		ino = curr->ino;
5861 		parent = curr->parent;
5862 		list_del(&curr->list);
5863 		kfree(curr);
5864 
5865 		inode = btrfs_iget_logging(ino, root);
5866 		/*
5867 		 * If the other inode that had a conflicting dir entry was
5868 		 * deleted in the current transaction, we need to log its parent
5869 		 * directory. See the comment at add_conflicting_inode().
5870 		 */
5871 		if (IS_ERR(inode)) {
5872 			ret = PTR_ERR(inode);
5873 			if (ret != -ENOENT)
5874 				break;
5875 
5876 			inode = btrfs_iget_logging(parent, root);
5877 			if (IS_ERR(inode)) {
5878 				ret = PTR_ERR(inode);
5879 				break;
5880 			}
5881 
5882 			/*
5883 			 * Always log the directory, we cannot make this
5884 			 * conditional on need_log_inode() because the directory
5885 			 * might have been logged in LOG_INODE_EXISTS mode or
5886 			 * the dir index of the conflicting inode is not in a
5887 			 * dir index key range logged for the directory. So we
5888 			 * must make sure the deletion is recorded.
5889 			 */
5890 			ret = btrfs_log_inode(trans, inode, LOG_INODE_ALL, ctx);
5891 			btrfs_add_delayed_iput(inode);
5892 			if (ret)
5893 				break;
5894 			continue;
5895 		}
5896 
5897 		/*
5898 		 * Here we can use need_log_inode() because we only need to log
5899 		 * the inode in LOG_INODE_EXISTS mode and rename operations
5900 		 * update the log, so that the log ends up with the new name and
5901 		 * without the old name.
5902 		 *
5903 		 * We did this check at add_conflicting_inode(), but here we do
5904 		 * it again because if some other task logged the inode after
5905 		 * that, we can avoid doing it again.
5906 		 */
5907 		if (!need_log_inode(trans, inode)) {
5908 			btrfs_add_delayed_iput(inode);
5909 			continue;
5910 		}
5911 
5912 		/*
5913 		 * We are safe logging the other inode without acquiring its
5914 		 * lock as long as we log with the LOG_INODE_EXISTS mode. We
5915 		 * are safe against concurrent renames of the other inode as
5916 		 * well because during a rename we pin the log and update the
5917 		 * log with the new name before we unpin it.
5918 		 */
5919 		ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
5920 		btrfs_add_delayed_iput(inode);
5921 		if (ret)
5922 			break;
5923 	}
5924 
5925 	ctx->logging_conflict_inodes = false;
5926 	if (ret)
5927 		free_conflicting_inodes(ctx);
5928 
5929 	return ret;
5930 }
5931 
copy_inode_items_to_log(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_key * min_key,const struct btrfs_key * max_key,struct btrfs_path * path,struct btrfs_path * dst_path,const u64 logged_isize,const int inode_only,struct btrfs_log_ctx * ctx,bool * need_log_inode_item)5932 static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5933 				   struct btrfs_inode *inode,
5934 				   struct btrfs_key *min_key,
5935 				   const struct btrfs_key *max_key,
5936 				   struct btrfs_path *path,
5937 				   struct btrfs_path *dst_path,
5938 				   const u64 logged_isize,
5939 				   const int inode_only,
5940 				   struct btrfs_log_ctx *ctx,
5941 				   bool *need_log_inode_item)
5942 {
5943 	const u64 i_size = i_size_read(&inode->vfs_inode);
5944 	struct btrfs_root *root = inode->root;
5945 	int ins_start_slot = 0;
5946 	int ins_nr = 0;
5947 	int ret;
5948 
5949 	while (1) {
5950 		ret = btrfs_search_forward(root, min_key, path, trans->transid);
5951 		if (ret < 0)
5952 			return ret;
5953 		if (ret > 0) {
5954 			ret = 0;
5955 			break;
5956 		}
5957 again:
5958 		/* Note, ins_nr might be > 0 here, cleanup outside the loop */
5959 		if (min_key->objectid != max_key->objectid)
5960 			break;
5961 		if (min_key->type > max_key->type)
5962 			break;
5963 
5964 		if (min_key->type == BTRFS_INODE_ITEM_KEY) {
5965 			*need_log_inode_item = false;
5966 		} else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
5967 			   min_key->offset >= i_size) {
5968 			/*
5969 			 * Extents at and beyond eof are logged with
5970 			 * btrfs_log_prealloc_extents().
5971 			 * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
5972 			 * and no keys greater than that, so bail out.
5973 			 */
5974 			break;
5975 		} else if ((min_key->type == BTRFS_INODE_REF_KEY ||
5976 			    min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5977 			   (inode->generation == trans->transid ||
5978 			    ctx->logging_conflict_inodes)) {
5979 			u64 other_ino = 0;
5980 			u64 other_parent = 0;
5981 
5982 			ret = btrfs_check_ref_name_override(path->nodes[0],
5983 					path->slots[0], min_key, inode,
5984 					&other_ino, &other_parent);
5985 			if (ret < 0) {
5986 				return ret;
5987 			} else if (ret > 0 &&
5988 				   other_ino != btrfs_ino(ctx->inode)) {
5989 				if (ins_nr > 0) {
5990 					ins_nr++;
5991 				} else {
5992 					ins_nr = 1;
5993 					ins_start_slot = path->slots[0];
5994 				}
5995 				ret = copy_items(trans, inode, dst_path, path,
5996 						 ins_start_slot, ins_nr,
5997 						 inode_only, logged_isize, ctx);
5998 				if (ret < 0)
5999 					return ret;
6000 				ins_nr = 0;
6001 
6002 				btrfs_release_path(path);
6003 				ret = add_conflicting_inode(trans, root, path,
6004 							    other_ino,
6005 							    other_parent, ctx);
6006 				if (ret)
6007 					return ret;
6008 				goto next_key;
6009 			}
6010 		} else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
6011 			/* Skip xattrs, logged later with btrfs_log_all_xattrs() */
6012 			if (ins_nr == 0)
6013 				goto next_slot;
6014 			ret = copy_items(trans, inode, dst_path, path,
6015 					 ins_start_slot,
6016 					 ins_nr, inode_only, logged_isize, ctx);
6017 			if (ret < 0)
6018 				return ret;
6019 			ins_nr = 0;
6020 			goto next_slot;
6021 		}
6022 
6023 		if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
6024 			ins_nr++;
6025 			goto next_slot;
6026 		} else if (!ins_nr) {
6027 			ins_start_slot = path->slots[0];
6028 			ins_nr = 1;
6029 			goto next_slot;
6030 		}
6031 
6032 		ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
6033 				 ins_nr, inode_only, logged_isize, ctx);
6034 		if (ret < 0)
6035 			return ret;
6036 		ins_nr = 1;
6037 		ins_start_slot = path->slots[0];
6038 next_slot:
6039 		path->slots[0]++;
6040 		if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
6041 			btrfs_item_key_to_cpu(path->nodes[0], min_key,
6042 					      path->slots[0]);
6043 			goto again;
6044 		}
6045 		if (ins_nr) {
6046 			ret = copy_items(trans, inode, dst_path, path,
6047 					 ins_start_slot, ins_nr, inode_only,
6048 					 logged_isize, ctx);
6049 			if (ret < 0)
6050 				return ret;
6051 			ins_nr = 0;
6052 		}
6053 		btrfs_release_path(path);
6054 next_key:
6055 		if (min_key->offset < (u64)-1) {
6056 			min_key->offset++;
6057 		} else if (min_key->type < max_key->type) {
6058 			min_key->type++;
6059 			min_key->offset = 0;
6060 		} else {
6061 			break;
6062 		}
6063 
6064 		/*
6065 		 * We may process many leaves full of items for our inode, so
6066 		 * avoid monopolizing a cpu for too long by rescheduling while
6067 		 * not holding locks on any tree.
6068 		 */
6069 		cond_resched();
6070 	}
6071 	if (ins_nr) {
6072 		ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
6073 				 ins_nr, inode_only, logged_isize, ctx);
6074 		if (ret)
6075 			return ret;
6076 	}
6077 
6078 	if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
6079 		/*
6080 		 * Release the path because otherwise we might attempt to double
6081 		 * lock the same leaf with btrfs_log_prealloc_extents() below.
6082 		 */
6083 		btrfs_release_path(path);
6084 		ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx);
6085 	}
6086 
6087 	return ret;
6088 }
6089 
insert_delayed_items_batch(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,const struct btrfs_item_batch * batch,const struct btrfs_delayed_item * first_item)6090 static int insert_delayed_items_batch(struct btrfs_trans_handle *trans,
6091 				      struct btrfs_root *log,
6092 				      struct btrfs_path *path,
6093 				      const struct btrfs_item_batch *batch,
6094 				      const struct btrfs_delayed_item *first_item)
6095 {
6096 	const struct btrfs_delayed_item *curr = first_item;
6097 	int ret;
6098 
6099 	ret = btrfs_insert_empty_items(trans, log, path, batch);
6100 	if (ret)
6101 		return ret;
6102 
6103 	for (int i = 0; i < batch->nr; i++) {
6104 		char *data_ptr;
6105 
6106 		data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
6107 		write_extent_buffer(path->nodes[0], &curr->data,
6108 				    (unsigned long)data_ptr, curr->data_len);
6109 		curr = list_next_entry(curr, log_list);
6110 		path->slots[0]++;
6111 	}
6112 
6113 	btrfs_release_path(path);
6114 
6115 	return 0;
6116 }
6117 
log_delayed_insertion_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_ins_list,struct btrfs_log_ctx * ctx)6118 static int log_delayed_insertion_items(struct btrfs_trans_handle *trans,
6119 				       struct btrfs_inode *inode,
6120 				       struct btrfs_path *path,
6121 				       const struct list_head *delayed_ins_list,
6122 				       struct btrfs_log_ctx *ctx)
6123 {
6124 	/* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */
6125 	const int max_batch_size = 195;
6126 	const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info);
6127 	const u64 ino = btrfs_ino(inode);
6128 	struct btrfs_root *log = inode->root->log_root;
6129 	struct btrfs_item_batch batch = {
6130 		.nr = 0,
6131 		.total_data_size = 0,
6132 	};
6133 	const struct btrfs_delayed_item *first = NULL;
6134 	const struct btrfs_delayed_item *curr;
6135 	char *ins_data;
6136 	struct btrfs_key *ins_keys;
6137 	u32 *ins_sizes;
6138 	u64 curr_batch_size = 0;
6139 	int batch_idx = 0;
6140 	int ret;
6141 
6142 	/* We are adding dir index items to the log tree. */
6143 	lockdep_assert_held(&inode->log_mutex);
6144 
6145 	/*
6146 	 * We collect delayed items before copying index keys from the subvolume
6147 	 * to the log tree. However just after we collected them, they may have
6148 	 * been flushed (all of them or just some of them), and therefore we
6149 	 * could have copied them from the subvolume tree to the log tree.
6150 	 * So find the first delayed item that was not yet logged (they are
6151 	 * sorted by index number).
6152 	 */
6153 	list_for_each_entry(curr, delayed_ins_list, log_list) {
6154 		if (curr->index > inode->last_dir_index_offset) {
6155 			first = curr;
6156 			break;
6157 		}
6158 	}
6159 
6160 	/* Empty list or all delayed items were already logged. */
6161 	if (!first)
6162 		return 0;
6163 
6164 	ins_data = kmalloc(max_batch_size * sizeof(u32) +
6165 			   max_batch_size * sizeof(struct btrfs_key), GFP_NOFS);
6166 	if (!ins_data)
6167 		return -ENOMEM;
6168 	ins_sizes = (u32 *)ins_data;
6169 	batch.data_sizes = ins_sizes;
6170 	ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32));
6171 	batch.keys = ins_keys;
6172 
6173 	curr = first;
6174 	while (!list_entry_is_head(curr, delayed_ins_list, log_list)) {
6175 		const u32 curr_size = curr->data_len + sizeof(struct btrfs_item);
6176 
6177 		if (curr_batch_size + curr_size > leaf_data_size ||
6178 		    batch.nr == max_batch_size) {
6179 			ret = insert_delayed_items_batch(trans, log, path,
6180 							 &batch, first);
6181 			if (ret)
6182 				goto out;
6183 			batch_idx = 0;
6184 			batch.nr = 0;
6185 			batch.total_data_size = 0;
6186 			curr_batch_size = 0;
6187 			first = curr;
6188 		}
6189 
6190 		ins_sizes[batch_idx] = curr->data_len;
6191 		ins_keys[batch_idx].objectid = ino;
6192 		ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY;
6193 		ins_keys[batch_idx].offset = curr->index;
6194 		curr_batch_size += curr_size;
6195 		batch.total_data_size += curr->data_len;
6196 		batch.nr++;
6197 		batch_idx++;
6198 		curr = list_next_entry(curr, log_list);
6199 	}
6200 
6201 	ASSERT(batch.nr >= 1);
6202 	ret = insert_delayed_items_batch(trans, log, path, &batch, first);
6203 
6204 	curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item,
6205 			       log_list);
6206 	inode->last_dir_index_offset = curr->index;
6207 out:
6208 	kfree(ins_data);
6209 
6210 	return ret;
6211 }
6212 
log_delayed_deletions_full(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)6213 static int log_delayed_deletions_full(struct btrfs_trans_handle *trans,
6214 				      struct btrfs_inode *inode,
6215 				      struct btrfs_path *path,
6216 				      const struct list_head *delayed_del_list,
6217 				      struct btrfs_log_ctx *ctx)
6218 {
6219 	const u64 ino = btrfs_ino(inode);
6220 	const struct btrfs_delayed_item *curr;
6221 
6222 	curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
6223 				log_list);
6224 
6225 	while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
6226 		u64 first_dir_index = curr->index;
6227 		u64 last_dir_index;
6228 		const struct btrfs_delayed_item *next;
6229 		int ret;
6230 
6231 		/*
6232 		 * Find a range of consecutive dir index items to delete. Like
6233 		 * this we log a single dir range item spanning several contiguous
6234 		 * dir items instead of logging one range item per dir index item.
6235 		 */
6236 		next = list_next_entry(curr, log_list);
6237 		while (!list_entry_is_head(next, delayed_del_list, log_list)) {
6238 			if (next->index != curr->index + 1)
6239 				break;
6240 			curr = next;
6241 			next = list_next_entry(next, log_list);
6242 		}
6243 
6244 		last_dir_index = curr->index;
6245 		ASSERT(last_dir_index >= first_dir_index);
6246 
6247 		ret = insert_dir_log_key(trans, inode->root->log_root, path,
6248 					 ino, first_dir_index, last_dir_index);
6249 		if (ret)
6250 			return ret;
6251 		curr = list_next_entry(curr, log_list);
6252 	}
6253 
6254 	return 0;
6255 }
6256 
batch_delete_dir_index_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,const struct btrfs_delayed_item * first,const struct btrfs_delayed_item ** last_ret)6257 static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans,
6258 					struct btrfs_inode *inode,
6259 					struct btrfs_path *path,
6260 					const struct list_head *delayed_del_list,
6261 					const struct btrfs_delayed_item *first,
6262 					const struct btrfs_delayed_item **last_ret)
6263 {
6264 	const struct btrfs_delayed_item *next;
6265 	struct extent_buffer *leaf = path->nodes[0];
6266 	const int last_slot = btrfs_header_nritems(leaf) - 1;
6267 	int slot = path->slots[0] + 1;
6268 	const u64 ino = btrfs_ino(inode);
6269 
6270 	next = list_next_entry(first, log_list);
6271 
6272 	while (slot < last_slot &&
6273 	       !list_entry_is_head(next, delayed_del_list, log_list)) {
6274 		struct btrfs_key key;
6275 
6276 		btrfs_item_key_to_cpu(leaf, &key, slot);
6277 		if (key.objectid != ino ||
6278 		    key.type != BTRFS_DIR_INDEX_KEY ||
6279 		    key.offset != next->index)
6280 			break;
6281 
6282 		slot++;
6283 		*last_ret = next;
6284 		next = list_next_entry(next, log_list);
6285 	}
6286 
6287 	return btrfs_del_items(trans, inode->root->log_root, path,
6288 			       path->slots[0], slot - path->slots[0]);
6289 }
6290 
log_delayed_deletions_incremental(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)6291 static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans,
6292 					     struct btrfs_inode *inode,
6293 					     struct btrfs_path *path,
6294 					     const struct list_head *delayed_del_list,
6295 					     struct btrfs_log_ctx *ctx)
6296 {
6297 	struct btrfs_root *log = inode->root->log_root;
6298 	const struct btrfs_delayed_item *curr;
6299 	u64 last_range_start = 0;
6300 	u64 last_range_end = 0;
6301 	struct btrfs_key key;
6302 
6303 	key.objectid = btrfs_ino(inode);
6304 	key.type = BTRFS_DIR_INDEX_KEY;
6305 	curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
6306 				log_list);
6307 
6308 	while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
6309 		const struct btrfs_delayed_item *last = curr;
6310 		u64 first_dir_index = curr->index;
6311 		u64 last_dir_index;
6312 		bool deleted_items = false;
6313 		int ret;
6314 
6315 		key.offset = curr->index;
6316 		ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
6317 		if (ret < 0) {
6318 			return ret;
6319 		} else if (ret == 0) {
6320 			ret = batch_delete_dir_index_items(trans, inode, path,
6321 							   delayed_del_list, curr,
6322 							   &last);
6323 			if (ret)
6324 				return ret;
6325 			deleted_items = true;
6326 		}
6327 
6328 		btrfs_release_path(path);
6329 
6330 		/*
6331 		 * If we deleted items from the leaf, it means we have a range
6332 		 * item logging their range, so no need to add one or update an
6333 		 * existing one. Otherwise we have to log a dir range item.
6334 		 */
6335 		if (deleted_items)
6336 			goto next_batch;
6337 
6338 		last_dir_index = last->index;
6339 		ASSERT(last_dir_index >= first_dir_index);
6340 		/*
6341 		 * If this range starts right after where the previous one ends,
6342 		 * then we want to reuse the previous range item and change its
6343 		 * end offset to the end of this range. This is just to minimize
6344 		 * leaf space usage, by avoiding adding a new range item.
6345 		 */
6346 		if (last_range_end != 0 && first_dir_index == last_range_end + 1)
6347 			first_dir_index = last_range_start;
6348 
6349 		ret = insert_dir_log_key(trans, log, path, key.objectid,
6350 					 first_dir_index, last_dir_index);
6351 		if (ret)
6352 			return ret;
6353 
6354 		last_range_start = first_dir_index;
6355 		last_range_end = last_dir_index;
6356 next_batch:
6357 		curr = list_next_entry(last, log_list);
6358 	}
6359 
6360 	return 0;
6361 }
6362 
log_delayed_deletion_items(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_path * path,const struct list_head * delayed_del_list,struct btrfs_log_ctx * ctx)6363 static int log_delayed_deletion_items(struct btrfs_trans_handle *trans,
6364 				      struct btrfs_inode *inode,
6365 				      struct btrfs_path *path,
6366 				      const struct list_head *delayed_del_list,
6367 				      struct btrfs_log_ctx *ctx)
6368 {
6369 	/*
6370 	 * We are deleting dir index items from the log tree or adding range
6371 	 * items to it.
6372 	 */
6373 	lockdep_assert_held(&inode->log_mutex);
6374 
6375 	if (list_empty(delayed_del_list))
6376 		return 0;
6377 
6378 	if (ctx->logged_before)
6379 		return log_delayed_deletions_incremental(trans, inode, path,
6380 							 delayed_del_list, ctx);
6381 
6382 	return log_delayed_deletions_full(trans, inode, path, delayed_del_list,
6383 					  ctx);
6384 }
6385 
6386 /*
6387  * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed
6388  * items instead of the subvolume tree.
6389  */
log_new_delayed_dentries(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,const struct list_head * delayed_ins_list,struct btrfs_log_ctx * ctx)6390 static int log_new_delayed_dentries(struct btrfs_trans_handle *trans,
6391 				    struct btrfs_inode *inode,
6392 				    const struct list_head *delayed_ins_list,
6393 				    struct btrfs_log_ctx *ctx)
6394 {
6395 	const bool orig_log_new_dentries = ctx->log_new_dentries;
6396 	struct btrfs_delayed_item *item;
6397 	int ret = 0;
6398 
6399 	/*
6400 	 * No need for the log mutex, plus to avoid potential deadlocks or
6401 	 * lockdep annotations due to nesting of delayed inode mutexes and log
6402 	 * mutexes.
6403 	 */
6404 	lockdep_assert_not_held(&inode->log_mutex);
6405 
6406 	ASSERT(!ctx->logging_new_delayed_dentries);
6407 	ctx->logging_new_delayed_dentries = true;
6408 
6409 	list_for_each_entry(item, delayed_ins_list, log_list) {
6410 		struct btrfs_dir_item *dir_item;
6411 		struct btrfs_inode *di_inode;
6412 		struct btrfs_key key;
6413 		int log_mode = LOG_INODE_EXISTS;
6414 
6415 		dir_item = (struct btrfs_dir_item *)item->data;
6416 		btrfs_disk_key_to_cpu(&key, &dir_item->location);
6417 
6418 		if (key.type == BTRFS_ROOT_ITEM_KEY)
6419 			continue;
6420 
6421 		di_inode = btrfs_iget_logging(key.objectid, inode->root);
6422 		if (IS_ERR(di_inode)) {
6423 			ret = PTR_ERR(di_inode);
6424 			break;
6425 		}
6426 
6427 		if (!need_log_inode(trans, di_inode)) {
6428 			btrfs_add_delayed_iput(di_inode);
6429 			continue;
6430 		}
6431 
6432 		if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR)
6433 			log_mode = LOG_INODE_ALL;
6434 
6435 		ctx->log_new_dentries = false;
6436 		ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
6437 
6438 		if (!ret && ctx->log_new_dentries)
6439 			ret = log_new_dir_dentries(trans, di_inode, ctx);
6440 
6441 		btrfs_add_delayed_iput(di_inode);
6442 
6443 		if (ret)
6444 			break;
6445 	}
6446 
6447 	ctx->log_new_dentries = orig_log_new_dentries;
6448 	ctx->logging_new_delayed_dentries = false;
6449 
6450 	return ret;
6451 }
6452 
6453 /* log a single inode in the tree log.
6454  * At least one parent directory for this inode must exist in the tree
6455  * or be logged already.
6456  *
6457  * Any items from this inode changed by the current transaction are copied
6458  * to the log tree.  An extra reference is taken on any extents in this
6459  * file, allowing us to avoid a whole pile of corner cases around logging
6460  * blocks that have been removed from the tree.
6461  *
6462  * See LOG_INODE_ALL and related defines for a description of what inode_only
6463  * does.
6464  *
6465  * This handles both files and directories.
6466  */
btrfs_log_inode(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,int inode_only,struct btrfs_log_ctx * ctx)6467 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
6468 			   struct btrfs_inode *inode,
6469 			   int inode_only,
6470 			   struct btrfs_log_ctx *ctx)
6471 {
6472 	struct btrfs_path *path;
6473 	struct btrfs_path *dst_path;
6474 	struct btrfs_key min_key;
6475 	struct btrfs_key max_key;
6476 	struct btrfs_root *log = inode->root->log_root;
6477 	int ret;
6478 	bool fast_search = false;
6479 	u64 ino = btrfs_ino(inode);
6480 	struct extent_map_tree *em_tree = &inode->extent_tree;
6481 	u64 logged_isize = 0;
6482 	bool need_log_inode_item = true;
6483 	bool xattrs_logged = false;
6484 	bool inode_item_dropped = true;
6485 	bool full_dir_logging = false;
6486 	LIST_HEAD(delayed_ins_list);
6487 	LIST_HEAD(delayed_del_list);
6488 
6489 	path = btrfs_alloc_path();
6490 	if (!path)
6491 		return -ENOMEM;
6492 	dst_path = btrfs_alloc_path();
6493 	if (!dst_path) {
6494 		btrfs_free_path(path);
6495 		return -ENOMEM;
6496 	}
6497 
6498 	min_key.objectid = ino;
6499 	min_key.type = BTRFS_INODE_ITEM_KEY;
6500 	min_key.offset = 0;
6501 
6502 	max_key.objectid = ino;
6503 
6504 
6505 	/* today the code can only do partial logging of directories */
6506 	if (S_ISDIR(inode->vfs_inode.i_mode) ||
6507 	    (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6508 		       &inode->runtime_flags) &&
6509 	     inode_only >= LOG_INODE_EXISTS))
6510 		max_key.type = BTRFS_XATTR_ITEM_KEY;
6511 	else
6512 		max_key.type = (u8)-1;
6513 	max_key.offset = (u64)-1;
6514 
6515 	if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL)
6516 		full_dir_logging = true;
6517 
6518 	/*
6519 	 * If we are logging a directory while we are logging dentries of the
6520 	 * delayed items of some other inode, then we need to flush the delayed
6521 	 * items of this directory and not log the delayed items directly. This
6522 	 * is to prevent more than one level of recursion into btrfs_log_inode()
6523 	 * by having something like this:
6524 	 *
6525 	 *     $ mkdir -p a/b/c/d/e/f/g/h/...
6526 	 *     $ xfs_io -c "fsync" a
6527 	 *
6528 	 * Where all directories in the path did not exist before and are
6529 	 * created in the current transaction.
6530 	 * So in such a case we directly log the delayed items of the main
6531 	 * directory ("a") without flushing them first, while for each of its
6532 	 * subdirectories we flush their delayed items before logging them.
6533 	 * This prevents a potential unbounded recursion like this:
6534 	 *
6535 	 * btrfs_log_inode()
6536 	 *   log_new_delayed_dentries()
6537 	 *      btrfs_log_inode()
6538 	 *        log_new_delayed_dentries()
6539 	 *          btrfs_log_inode()
6540 	 *            log_new_delayed_dentries()
6541 	 *              (...)
6542 	 *
6543 	 * We have thresholds for the maximum number of delayed items to have in
6544 	 * memory, and once they are hit, the items are flushed asynchronously.
6545 	 * However the limit is quite high, so lets prevent deep levels of
6546 	 * recursion to happen by limiting the maximum depth to be 1.
6547 	 */
6548 	if (full_dir_logging && ctx->logging_new_delayed_dentries) {
6549 		ret = btrfs_commit_inode_delayed_items(trans, inode);
6550 		if (ret)
6551 			goto out;
6552 	}
6553 
6554 	mutex_lock(&inode->log_mutex);
6555 
6556 	/*
6557 	 * For symlinks, we must always log their content, which is stored in an
6558 	 * inline extent, otherwise we could end up with an empty symlink after
6559 	 * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
6560 	 * one attempts to create an empty symlink).
6561 	 * We don't need to worry about flushing delalloc, because when we create
6562 	 * the inline extent when the symlink is created (we never have delalloc
6563 	 * for symlinks).
6564 	 */
6565 	if (S_ISLNK(inode->vfs_inode.i_mode))
6566 		inode_only = LOG_INODE_ALL;
6567 
6568 	/*
6569 	 * Before logging the inode item, cache the value returned by
6570 	 * inode_logged(), because after that we have the need to figure out if
6571 	 * the inode was previously logged in this transaction.
6572 	 */
6573 	ret = inode_logged(trans, inode, path);
6574 	if (ret < 0)
6575 		goto out_unlock;
6576 	ctx->logged_before = (ret == 1);
6577 	ret = 0;
6578 
6579 	/*
6580 	 * This is for cases where logging a directory could result in losing a
6581 	 * a file after replaying the log. For example, if we move a file from a
6582 	 * directory A to a directory B, then fsync directory A, we have no way
6583 	 * to known the file was moved from A to B, so logging just A would
6584 	 * result in losing the file after a log replay.
6585 	 */
6586 	if (full_dir_logging && inode->last_unlink_trans >= trans->transid) {
6587 		ret = BTRFS_LOG_FORCE_COMMIT;
6588 		goto out_unlock;
6589 	}
6590 
6591 	/*
6592 	 * a brute force approach to making sure we get the most uptodate
6593 	 * copies of everything.
6594 	 */
6595 	if (S_ISDIR(inode->vfs_inode.i_mode)) {
6596 		clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
6597 		if (ctx->logged_before)
6598 			ret = drop_inode_items(trans, log, path, inode,
6599 					       BTRFS_XATTR_ITEM_KEY);
6600 	} else {
6601 		if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) {
6602 			/*
6603 			 * Make sure the new inode item we write to the log has
6604 			 * the same isize as the current one (if it exists).
6605 			 * This is necessary to prevent data loss after log
6606 			 * replay, and also to prevent doing a wrong expanding
6607 			 * truncate - for e.g. create file, write 4K into offset
6608 			 * 0, fsync, write 4K into offset 4096, add hard link,
6609 			 * fsync some other file (to sync log), power fail - if
6610 			 * we use the inode's current i_size, after log replay
6611 			 * we get a 8Kb file, with the last 4Kb extent as a hole
6612 			 * (zeroes), as if an expanding truncate happened,
6613 			 * instead of getting a file of 4Kb only.
6614 			 */
6615 			ret = logged_inode_size(log, inode, path, &logged_isize);
6616 			if (ret)
6617 				goto out_unlock;
6618 		}
6619 		if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6620 			     &inode->runtime_flags)) {
6621 			if (inode_only == LOG_INODE_EXISTS) {
6622 				max_key.type = BTRFS_XATTR_ITEM_KEY;
6623 				if (ctx->logged_before)
6624 					ret = drop_inode_items(trans, log, path,
6625 							       inode, max_key.type);
6626 			} else {
6627 				clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
6628 					  &inode->runtime_flags);
6629 				clear_bit(BTRFS_INODE_COPY_EVERYTHING,
6630 					  &inode->runtime_flags);
6631 				if (ctx->logged_before)
6632 					ret = truncate_inode_items(trans, log,
6633 								   inode, 0, 0);
6634 			}
6635 		} else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
6636 					      &inode->runtime_flags) ||
6637 			   inode_only == LOG_INODE_EXISTS) {
6638 			if (inode_only == LOG_INODE_ALL)
6639 				fast_search = true;
6640 			max_key.type = BTRFS_XATTR_ITEM_KEY;
6641 			if (ctx->logged_before)
6642 				ret = drop_inode_items(trans, log, path, inode,
6643 						       max_key.type);
6644 		} else {
6645 			if (inode_only == LOG_INODE_ALL)
6646 				fast_search = true;
6647 			inode_item_dropped = false;
6648 			goto log_extents;
6649 		}
6650 
6651 	}
6652 	if (ret)
6653 		goto out_unlock;
6654 
6655 	/*
6656 	 * If we are logging a directory in full mode, collect the delayed items
6657 	 * before iterating the subvolume tree, so that we don't miss any new
6658 	 * dir index items in case they get flushed while or right after we are
6659 	 * iterating the subvolume tree.
6660 	 */
6661 	if (full_dir_logging && !ctx->logging_new_delayed_dentries)
6662 		btrfs_log_get_delayed_items(inode, &delayed_ins_list,
6663 					    &delayed_del_list);
6664 
6665 	/*
6666 	 * If we are fsyncing a file with 0 hard links, then commit the delayed
6667 	 * inode because the last inode ref (or extref) item may still be in the
6668 	 * subvolume tree and if we log it the file will still exist after a log
6669 	 * replay. So commit the delayed inode to delete that last ref and we
6670 	 * skip logging it.
6671 	 */
6672 	if (inode->vfs_inode.i_nlink == 0) {
6673 		ret = btrfs_commit_inode_delayed_inode(inode);
6674 		if (ret)
6675 			goto out_unlock;
6676 	}
6677 
6678 	ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
6679 				      path, dst_path, logged_isize,
6680 				      inode_only, ctx,
6681 				      &need_log_inode_item);
6682 	if (ret)
6683 		goto out_unlock;
6684 
6685 	btrfs_release_path(path);
6686 	btrfs_release_path(dst_path);
6687 	ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
6688 	if (ret)
6689 		goto out_unlock;
6690 	xattrs_logged = true;
6691 	if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
6692 		btrfs_release_path(path);
6693 		btrfs_release_path(dst_path);
6694 		ret = btrfs_log_holes(trans, inode, path);
6695 		if (ret)
6696 			goto out_unlock;
6697 	}
6698 log_extents:
6699 	btrfs_release_path(path);
6700 	btrfs_release_path(dst_path);
6701 	if (need_log_inode_item) {
6702 		ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
6703 		if (ret)
6704 			goto out_unlock;
6705 		/*
6706 		 * If we are doing a fast fsync and the inode was logged before
6707 		 * in this transaction, we don't need to log the xattrs because
6708 		 * they were logged before. If xattrs were added, changed or
6709 		 * deleted since the last time we logged the inode, then we have
6710 		 * already logged them because the inode had the runtime flag
6711 		 * BTRFS_INODE_COPY_EVERYTHING set.
6712 		 */
6713 		if (!xattrs_logged && inode->logged_trans < trans->transid) {
6714 			ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
6715 			if (ret)
6716 				goto out_unlock;
6717 			btrfs_release_path(path);
6718 		}
6719 	}
6720 	if (fast_search) {
6721 		ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
6722 		if (ret)
6723 			goto out_unlock;
6724 	} else if (inode_only == LOG_INODE_ALL) {
6725 		struct extent_map *em, *n;
6726 
6727 		write_lock(&em_tree->lock);
6728 		list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
6729 			list_del_init(&em->list);
6730 		write_unlock(&em_tree->lock);
6731 	}
6732 
6733 	if (full_dir_logging) {
6734 		ret = log_directory_changes(trans, inode, path, dst_path, ctx);
6735 		if (ret)
6736 			goto out_unlock;
6737 		ret = log_delayed_insertion_items(trans, inode, path,
6738 						  &delayed_ins_list, ctx);
6739 		if (ret)
6740 			goto out_unlock;
6741 		ret = log_delayed_deletion_items(trans, inode, path,
6742 						 &delayed_del_list, ctx);
6743 		if (ret)
6744 			goto out_unlock;
6745 	}
6746 
6747 	spin_lock(&inode->lock);
6748 	inode->logged_trans = trans->transid;
6749 	/*
6750 	 * Don't update last_log_commit if we logged that an inode exists.
6751 	 * We do this for three reasons:
6752 	 *
6753 	 * 1) We might have had buffered writes to this inode that were
6754 	 *    flushed and had their ordered extents completed in this
6755 	 *    transaction, but we did not previously log the inode with
6756 	 *    LOG_INODE_ALL. Later the inode was evicted and after that
6757 	 *    it was loaded again and this LOG_INODE_EXISTS log operation
6758 	 *    happened. We must make sure that if an explicit fsync against
6759 	 *    the inode is performed later, it logs the new extents, an
6760 	 *    updated inode item, etc, and syncs the log. The same logic
6761 	 *    applies to direct IO writes instead of buffered writes.
6762 	 *
6763 	 * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
6764 	 *    is logged with an i_size of 0 or whatever value was logged
6765 	 *    before. If later the i_size of the inode is increased by a
6766 	 *    truncate operation, the log is synced through an fsync of
6767 	 *    some other inode and then finally an explicit fsync against
6768 	 *    this inode is made, we must make sure this fsync logs the
6769 	 *    inode with the new i_size, the hole between old i_size and
6770 	 *    the new i_size, and syncs the log.
6771 	 *
6772 	 * 3) If we are logging that an ancestor inode exists as part of
6773 	 *    logging a new name from a link or rename operation, don't update
6774 	 *    its last_log_commit - otherwise if an explicit fsync is made
6775 	 *    against an ancestor, the fsync considers the inode in the log
6776 	 *    and doesn't sync the log, resulting in the ancestor missing after
6777 	 *    a power failure unless the log was synced as part of an fsync
6778 	 *    against any other unrelated inode.
6779 	 */
6780 	if (inode_only != LOG_INODE_EXISTS)
6781 		inode->last_log_commit = inode->last_sub_trans;
6782 	spin_unlock(&inode->lock);
6783 
6784 	/*
6785 	 * Reset the last_reflink_trans so that the next fsync does not need to
6786 	 * go through the slower path when logging extents and their checksums.
6787 	 */
6788 	if (inode_only == LOG_INODE_ALL)
6789 		inode->last_reflink_trans = 0;
6790 
6791 out_unlock:
6792 	mutex_unlock(&inode->log_mutex);
6793 out:
6794 	btrfs_free_path(path);
6795 	btrfs_free_path(dst_path);
6796 
6797 	if (ret)
6798 		free_conflicting_inodes(ctx);
6799 	else
6800 		ret = log_conflicting_inodes(trans, inode->root, ctx);
6801 
6802 	if (full_dir_logging && !ctx->logging_new_delayed_dentries) {
6803 		if (!ret)
6804 			ret = log_new_delayed_dentries(trans, inode,
6805 						       &delayed_ins_list, ctx);
6806 
6807 		btrfs_log_put_delayed_items(inode, &delayed_ins_list,
6808 					    &delayed_del_list);
6809 	}
6810 
6811 	return ret;
6812 }
6813 
btrfs_log_all_parents(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct btrfs_log_ctx * ctx)6814 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
6815 				 struct btrfs_inode *inode,
6816 				 struct btrfs_log_ctx *ctx)
6817 {
6818 	int ret;
6819 	struct btrfs_path *path;
6820 	struct btrfs_key key;
6821 	struct btrfs_root *root = inode->root;
6822 	const u64 ino = btrfs_ino(inode);
6823 
6824 	path = btrfs_alloc_path();
6825 	if (!path)
6826 		return -ENOMEM;
6827 	path->skip_locking = 1;
6828 	path->search_commit_root = 1;
6829 
6830 	key.objectid = ino;
6831 	key.type = BTRFS_INODE_REF_KEY;
6832 	key.offset = 0;
6833 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6834 	if (ret < 0)
6835 		goto out;
6836 
6837 	while (true) {
6838 		struct extent_buffer *leaf = path->nodes[0];
6839 		int slot = path->slots[0];
6840 		u32 cur_offset = 0;
6841 		u32 item_size;
6842 		unsigned long ptr;
6843 
6844 		if (slot >= btrfs_header_nritems(leaf)) {
6845 			ret = btrfs_next_leaf(root, path);
6846 			if (ret < 0)
6847 				goto out;
6848 			else if (ret > 0)
6849 				break;
6850 			continue;
6851 		}
6852 
6853 		btrfs_item_key_to_cpu(leaf, &key, slot);
6854 		/* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
6855 		if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
6856 			break;
6857 
6858 		item_size = btrfs_item_size(leaf, slot);
6859 		ptr = btrfs_item_ptr_offset(leaf, slot);
6860 		while (cur_offset < item_size) {
6861 			struct btrfs_key inode_key;
6862 			struct btrfs_inode *dir_inode;
6863 
6864 			inode_key.type = BTRFS_INODE_ITEM_KEY;
6865 			inode_key.offset = 0;
6866 
6867 			if (key.type == BTRFS_INODE_EXTREF_KEY) {
6868 				struct btrfs_inode_extref *extref;
6869 
6870 				extref = (struct btrfs_inode_extref *)
6871 					(ptr + cur_offset);
6872 				inode_key.objectid = btrfs_inode_extref_parent(
6873 					leaf, extref);
6874 				cur_offset += sizeof(*extref);
6875 				cur_offset += btrfs_inode_extref_name_len(leaf,
6876 					extref);
6877 			} else {
6878 				inode_key.objectid = key.offset;
6879 				cur_offset = item_size;
6880 			}
6881 
6882 			dir_inode = btrfs_iget_logging(inode_key.objectid, root);
6883 			/*
6884 			 * If the parent inode was deleted, return an error to
6885 			 * fallback to a transaction commit. This is to prevent
6886 			 * getting an inode that was moved from one parent A to
6887 			 * a parent B, got its former parent A deleted and then
6888 			 * it got fsync'ed, from existing at both parents after
6889 			 * a log replay (and the old parent still existing).
6890 			 * Example:
6891 			 *
6892 			 * mkdir /mnt/A
6893 			 * mkdir /mnt/B
6894 			 * touch /mnt/B/bar
6895 			 * sync
6896 			 * mv /mnt/B/bar /mnt/A/bar
6897 			 * mv -T /mnt/A /mnt/B
6898 			 * fsync /mnt/B/bar
6899 			 * <power fail>
6900 			 *
6901 			 * If we ignore the old parent B which got deleted,
6902 			 * after a log replay we would have file bar linked
6903 			 * at both parents and the old parent B would still
6904 			 * exist.
6905 			 */
6906 			if (IS_ERR(dir_inode)) {
6907 				ret = PTR_ERR(dir_inode);
6908 				goto out;
6909 			}
6910 
6911 			if (!need_log_inode(trans, dir_inode)) {
6912 				btrfs_add_delayed_iput(dir_inode);
6913 				continue;
6914 			}
6915 
6916 			ctx->log_new_dentries = false;
6917 			ret = btrfs_log_inode(trans, dir_inode, LOG_INODE_ALL, ctx);
6918 			if (!ret && ctx->log_new_dentries)
6919 				ret = log_new_dir_dentries(trans, dir_inode, ctx);
6920 			btrfs_add_delayed_iput(dir_inode);
6921 			if (ret)
6922 				goto out;
6923 		}
6924 		path->slots[0]++;
6925 	}
6926 	ret = 0;
6927 out:
6928 	btrfs_free_path(path);
6929 	return ret;
6930 }
6931 
log_new_ancestors(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_log_ctx * ctx)6932 static int log_new_ancestors(struct btrfs_trans_handle *trans,
6933 			     struct btrfs_root *root,
6934 			     struct btrfs_path *path,
6935 			     struct btrfs_log_ctx *ctx)
6936 {
6937 	struct btrfs_key found_key;
6938 
6939 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
6940 
6941 	while (true) {
6942 		struct extent_buffer *leaf;
6943 		int slot;
6944 		struct btrfs_key search_key;
6945 		struct btrfs_inode *inode;
6946 		u64 ino;
6947 		int ret = 0;
6948 
6949 		btrfs_release_path(path);
6950 
6951 		ino = found_key.offset;
6952 
6953 		search_key.objectid = found_key.offset;
6954 		search_key.type = BTRFS_INODE_ITEM_KEY;
6955 		search_key.offset = 0;
6956 		inode = btrfs_iget_logging(ino, root);
6957 		if (IS_ERR(inode))
6958 			return PTR_ERR(inode);
6959 
6960 		if (inode->generation >= trans->transid &&
6961 		    need_log_inode(trans, inode))
6962 			ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
6963 		btrfs_add_delayed_iput(inode);
6964 		if (ret)
6965 			return ret;
6966 
6967 		if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
6968 			break;
6969 
6970 		search_key.type = BTRFS_INODE_REF_KEY;
6971 		ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6972 		if (ret < 0)
6973 			return ret;
6974 
6975 		leaf = path->nodes[0];
6976 		slot = path->slots[0];
6977 		if (slot >= btrfs_header_nritems(leaf)) {
6978 			ret = btrfs_next_leaf(root, path);
6979 			if (ret < 0)
6980 				return ret;
6981 			else if (ret > 0)
6982 				return -ENOENT;
6983 			leaf = path->nodes[0];
6984 			slot = path->slots[0];
6985 		}
6986 
6987 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
6988 		if (found_key.objectid != search_key.objectid ||
6989 		    found_key.type != BTRFS_INODE_REF_KEY)
6990 			return -ENOENT;
6991 	}
6992 	return 0;
6993 }
6994 
log_new_ancestors_fast(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,struct btrfs_log_ctx * ctx)6995 static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
6996 				  struct btrfs_inode *inode,
6997 				  struct dentry *parent,
6998 				  struct btrfs_log_ctx *ctx)
6999 {
7000 	struct btrfs_root *root = inode->root;
7001 	struct dentry *old_parent = NULL;
7002 	struct super_block *sb = inode->vfs_inode.i_sb;
7003 	int ret = 0;
7004 
7005 	while (true) {
7006 		if (!parent || d_really_is_negative(parent) ||
7007 		    sb != parent->d_sb)
7008 			break;
7009 
7010 		inode = BTRFS_I(d_inode(parent));
7011 		if (root != inode->root)
7012 			break;
7013 
7014 		if (inode->generation >= trans->transid &&
7015 		    need_log_inode(trans, inode)) {
7016 			ret = btrfs_log_inode(trans, inode,
7017 					      LOG_INODE_EXISTS, ctx);
7018 			if (ret)
7019 				break;
7020 		}
7021 		if (IS_ROOT(parent))
7022 			break;
7023 
7024 		parent = dget_parent(parent);
7025 		dput(old_parent);
7026 		old_parent = parent;
7027 	}
7028 	dput(old_parent);
7029 
7030 	return ret;
7031 }
7032 
log_all_new_ancestors(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,struct btrfs_log_ctx * ctx)7033 static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
7034 				 struct btrfs_inode *inode,
7035 				 struct dentry *parent,
7036 				 struct btrfs_log_ctx *ctx)
7037 {
7038 	struct btrfs_root *root = inode->root;
7039 	const u64 ino = btrfs_ino(inode);
7040 	struct btrfs_path *path;
7041 	struct btrfs_key search_key;
7042 	int ret;
7043 
7044 	/*
7045 	 * For a single hard link case, go through a fast path that does not
7046 	 * need to iterate the fs/subvolume tree.
7047 	 */
7048 	if (inode->vfs_inode.i_nlink < 2)
7049 		return log_new_ancestors_fast(trans, inode, parent, ctx);
7050 
7051 	path = btrfs_alloc_path();
7052 	if (!path)
7053 		return -ENOMEM;
7054 
7055 	search_key.objectid = ino;
7056 	search_key.type = BTRFS_INODE_REF_KEY;
7057 	search_key.offset = 0;
7058 again:
7059 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
7060 	if (ret < 0)
7061 		goto out;
7062 	if (ret == 0)
7063 		path->slots[0]++;
7064 
7065 	while (true) {
7066 		struct extent_buffer *leaf = path->nodes[0];
7067 		int slot = path->slots[0];
7068 		struct btrfs_key found_key;
7069 
7070 		if (slot >= btrfs_header_nritems(leaf)) {
7071 			ret = btrfs_next_leaf(root, path);
7072 			if (ret < 0)
7073 				goto out;
7074 			else if (ret > 0)
7075 				break;
7076 			continue;
7077 		}
7078 
7079 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
7080 		if (found_key.objectid != ino ||
7081 		    found_key.type > BTRFS_INODE_EXTREF_KEY)
7082 			break;
7083 
7084 		/*
7085 		 * Don't deal with extended references because they are rare
7086 		 * cases and too complex to deal with (we would need to keep
7087 		 * track of which subitem we are processing for each item in
7088 		 * this loop, etc). So just return some error to fallback to
7089 		 * a transaction commit.
7090 		 */
7091 		if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
7092 			ret = -EMLINK;
7093 			goto out;
7094 		}
7095 
7096 		/*
7097 		 * Logging ancestors needs to do more searches on the fs/subvol
7098 		 * tree, so it releases the path as needed to avoid deadlocks.
7099 		 * Keep track of the last inode ref key and resume from that key
7100 		 * after logging all new ancestors for the current hard link.
7101 		 */
7102 		memcpy(&search_key, &found_key, sizeof(search_key));
7103 
7104 		ret = log_new_ancestors(trans, root, path, ctx);
7105 		if (ret)
7106 			goto out;
7107 		btrfs_release_path(path);
7108 		goto again;
7109 	}
7110 	ret = 0;
7111 out:
7112 	btrfs_free_path(path);
7113 	return ret;
7114 }
7115 
7116 /*
7117  * helper function around btrfs_log_inode to make sure newly created
7118  * parent directories also end up in the log.  A minimal inode and backref
7119  * only logging is done of any parent directories that are older than
7120  * the last committed transaction
7121  */
btrfs_log_inode_parent(struct btrfs_trans_handle * trans,struct btrfs_inode * inode,struct dentry * parent,int inode_only,struct btrfs_log_ctx * ctx)7122 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
7123 				  struct btrfs_inode *inode,
7124 				  struct dentry *parent,
7125 				  int inode_only,
7126 				  struct btrfs_log_ctx *ctx)
7127 {
7128 	struct btrfs_root *root = inode->root;
7129 	struct btrfs_fs_info *fs_info = root->fs_info;
7130 	int ret = 0;
7131 	bool log_dentries;
7132 
7133 	if (btrfs_test_opt(fs_info, NOTREELOG))
7134 		return BTRFS_LOG_FORCE_COMMIT;
7135 
7136 	if (btrfs_root_refs(&root->root_item) == 0)
7137 		return BTRFS_LOG_FORCE_COMMIT;
7138 
7139 	/*
7140 	 * If we're logging an inode from a subvolume created in the current
7141 	 * transaction we must force a commit since the root is not persisted.
7142 	 */
7143 	if (btrfs_root_generation(&root->root_item) == trans->transid)
7144 		return BTRFS_LOG_FORCE_COMMIT;
7145 
7146 	/* Skip already logged inodes and without new extents. */
7147 	if (btrfs_inode_in_log(inode, trans->transid) &&
7148 	    list_empty(&ctx->ordered_extents))
7149 		return BTRFS_NO_LOG_SYNC;
7150 
7151 	ret = start_log_trans(trans, root, ctx);
7152 	if (ret)
7153 		return ret;
7154 
7155 	ret = btrfs_log_inode(trans, inode, inode_only, ctx);
7156 	if (ret)
7157 		goto end_trans;
7158 
7159 	/*
7160 	 * for regular files, if its inode is already on disk, we don't
7161 	 * have to worry about the parents at all.  This is because
7162 	 * we can use the last_unlink_trans field to record renames
7163 	 * and other fun in this file.
7164 	 */
7165 	if (S_ISREG(inode->vfs_inode.i_mode) &&
7166 	    inode->generation < trans->transid &&
7167 	    inode->last_unlink_trans < trans->transid) {
7168 		ret = 0;
7169 		goto end_trans;
7170 	}
7171 
7172 	/*
7173 	 * Track if we need to log dentries because ctx->log_new_dentries can
7174 	 * be modified in the call chains below.
7175 	 */
7176 	log_dentries = ctx->log_new_dentries;
7177 
7178 	/*
7179 	 * On unlink we must make sure all our current and old parent directory
7180 	 * inodes are fully logged. This is to prevent leaving dangling
7181 	 * directory index entries in directories that were our parents but are
7182 	 * not anymore. Not doing this results in old parent directory being
7183 	 * impossible to delete after log replay (rmdir will always fail with
7184 	 * error -ENOTEMPTY).
7185 	 *
7186 	 * Example 1:
7187 	 *
7188 	 * mkdir testdir
7189 	 * touch testdir/foo
7190 	 * ln testdir/foo testdir/bar
7191 	 * sync
7192 	 * unlink testdir/bar
7193 	 * xfs_io -c fsync testdir/foo
7194 	 * <power failure>
7195 	 * mount fs, triggers log replay
7196 	 *
7197 	 * If we don't log the parent directory (testdir), after log replay the
7198 	 * directory still has an entry pointing to the file inode using the bar
7199 	 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
7200 	 * the file inode has a link count of 1.
7201 	 *
7202 	 * Example 2:
7203 	 *
7204 	 * mkdir testdir
7205 	 * touch foo
7206 	 * ln foo testdir/foo2
7207 	 * ln foo testdir/foo3
7208 	 * sync
7209 	 * unlink testdir/foo3
7210 	 * xfs_io -c fsync foo
7211 	 * <power failure>
7212 	 * mount fs, triggers log replay
7213 	 *
7214 	 * Similar as the first example, after log replay the parent directory
7215 	 * testdir still has an entry pointing to the inode file with name foo3
7216 	 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
7217 	 * and has a link count of 2.
7218 	 */
7219 	if (inode->last_unlink_trans >= trans->transid) {
7220 		ret = btrfs_log_all_parents(trans, inode, ctx);
7221 		if (ret)
7222 			goto end_trans;
7223 	}
7224 
7225 	ret = log_all_new_ancestors(trans, inode, parent, ctx);
7226 	if (ret)
7227 		goto end_trans;
7228 
7229 	if (log_dentries)
7230 		ret = log_new_dir_dentries(trans, inode, ctx);
7231 end_trans:
7232 	if (ret < 0) {
7233 		btrfs_set_log_full_commit(trans);
7234 		ret = BTRFS_LOG_FORCE_COMMIT;
7235 	}
7236 
7237 	if (ret)
7238 		btrfs_remove_log_ctx(root, ctx);
7239 	btrfs_end_log_trans(root);
7240 
7241 	return ret;
7242 }
7243 
7244 /*
7245  * it is not safe to log dentry if the chunk root has added new
7246  * chunks.  This returns 0 if the dentry was logged, and 1 otherwise.
7247  * If this returns 1, you must commit the transaction to safely get your
7248  * data on disk.
7249  */
btrfs_log_dentry_safe(struct btrfs_trans_handle * trans,struct dentry * dentry,struct btrfs_log_ctx * ctx)7250 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
7251 			  struct dentry *dentry,
7252 			  struct btrfs_log_ctx *ctx)
7253 {
7254 	struct dentry *parent = dget_parent(dentry);
7255 	int ret;
7256 
7257 	ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
7258 				     LOG_INODE_ALL, ctx);
7259 	dput(parent);
7260 
7261 	return ret;
7262 }
7263 
7264 /*
7265  * should be called during mount to recover any replay any log trees
7266  * from the FS
7267  */
btrfs_recover_log_trees(struct btrfs_root * log_root_tree)7268 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
7269 {
7270 	int ret;
7271 	struct btrfs_path *path;
7272 	struct btrfs_trans_handle *trans;
7273 	struct btrfs_key key;
7274 	struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
7275 	struct walk_control wc = {
7276 		.process_func = process_one_buffer,
7277 		.stage = LOG_WALK_PIN_ONLY,
7278 	};
7279 
7280 	path = btrfs_alloc_path();
7281 	if (!path)
7282 		return -ENOMEM;
7283 
7284 	set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
7285 
7286 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
7287 	if (IS_ERR(trans)) {
7288 		ret = PTR_ERR(trans);
7289 		goto error;
7290 	}
7291 
7292 	wc.trans = trans;
7293 	wc.pin = 1;
7294 
7295 	ret = walk_log_tree(trans, log_root_tree, &wc);
7296 	if (ret) {
7297 		btrfs_abort_transaction(trans, ret);
7298 		goto error;
7299 	}
7300 
7301 again:
7302 	key.objectid = BTRFS_TREE_LOG_OBJECTID;
7303 	key.type = BTRFS_ROOT_ITEM_KEY;
7304 	key.offset = (u64)-1;
7305 
7306 	while (1) {
7307 		struct btrfs_root *log;
7308 		struct btrfs_key found_key;
7309 
7310 		ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
7311 
7312 		if (ret < 0) {
7313 			btrfs_abort_transaction(trans, ret);
7314 			goto error;
7315 		}
7316 		if (ret > 0) {
7317 			if (path->slots[0] == 0)
7318 				break;
7319 			path->slots[0]--;
7320 		}
7321 		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
7322 				      path->slots[0]);
7323 		btrfs_release_path(path);
7324 		if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
7325 			break;
7326 
7327 		log = btrfs_read_tree_root(log_root_tree, &found_key);
7328 		if (IS_ERR(log)) {
7329 			ret = PTR_ERR(log);
7330 			btrfs_abort_transaction(trans, ret);
7331 			goto error;
7332 		}
7333 
7334 		wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
7335 						   true);
7336 		if (IS_ERR(wc.replay_dest)) {
7337 			ret = PTR_ERR(wc.replay_dest);
7338 			wc.replay_dest = NULL;
7339 			if (ret != -ENOENT) {
7340 				btrfs_put_root(log);
7341 				btrfs_abort_transaction(trans, ret);
7342 				goto error;
7343 			}
7344 
7345 			/*
7346 			 * We didn't find the subvol, likely because it was
7347 			 * deleted.  This is ok, simply skip this log and go to
7348 			 * the next one.
7349 			 *
7350 			 * We need to exclude the root because we can't have
7351 			 * other log replays overwriting this log as we'll read
7352 			 * it back in a few more times.  This will keep our
7353 			 * block from being modified, and we'll just bail for
7354 			 * each subsequent pass.
7355 			 */
7356 			ret = btrfs_pin_extent_for_log_replay(trans, log->node);
7357 			if (ret) {
7358 				btrfs_put_root(log);
7359 				btrfs_abort_transaction(trans, ret);
7360 				goto error;
7361 			}
7362 			goto next;
7363 		}
7364 
7365 		wc.replay_dest->log_root = log;
7366 		ret = btrfs_record_root_in_trans(trans, wc.replay_dest);
7367 		if (ret) {
7368 			btrfs_abort_transaction(trans, ret);
7369 			goto next;
7370 		}
7371 
7372 		ret = walk_log_tree(trans, log, &wc);
7373 		if (ret) {
7374 			btrfs_abort_transaction(trans, ret);
7375 			goto next;
7376 		}
7377 
7378 		if (wc.stage == LOG_WALK_REPLAY_ALL) {
7379 			struct btrfs_root *root = wc.replay_dest;
7380 
7381 			ret = fixup_inode_link_counts(trans, wc.replay_dest, path);
7382 			if (ret) {
7383 				btrfs_abort_transaction(trans, ret);
7384 				goto next;
7385 			}
7386 			/*
7387 			 * We have just replayed everything, and the highest
7388 			 * objectid of fs roots probably has changed in case
7389 			 * some inode_item's got replayed.
7390 			 *
7391 			 * root->objectid_mutex is not acquired as log replay
7392 			 * could only happen during mount.
7393 			 */
7394 			ret = btrfs_init_root_free_objectid(root);
7395 			if (ret) {
7396 				btrfs_abort_transaction(trans, ret);
7397 				goto next;
7398 			}
7399 		}
7400 next:
7401 		if (wc.replay_dest) {
7402 			wc.replay_dest->log_root = NULL;
7403 			btrfs_put_root(wc.replay_dest);
7404 		}
7405 		btrfs_put_root(log);
7406 
7407 		if (ret)
7408 			goto error;
7409 		if (found_key.offset == 0)
7410 			break;
7411 		key.offset = found_key.offset - 1;
7412 	}
7413 	btrfs_release_path(path);
7414 
7415 	/* step one is to pin it all, step two is to replay just inodes */
7416 	if (wc.pin) {
7417 		wc.pin = 0;
7418 		wc.process_func = replay_one_buffer;
7419 		wc.stage = LOG_WALK_REPLAY_INODES;
7420 		goto again;
7421 	}
7422 	/* step three is to replay everything */
7423 	if (wc.stage < LOG_WALK_REPLAY_ALL) {
7424 		wc.stage++;
7425 		goto again;
7426 	}
7427 
7428 	btrfs_free_path(path);
7429 
7430 	/* step 4: commit the transaction, which also unpins the blocks */
7431 	ret = btrfs_commit_transaction(trans);
7432 	if (ret)
7433 		return ret;
7434 
7435 	log_root_tree->log_root = NULL;
7436 	clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
7437 	btrfs_put_root(log_root_tree);
7438 
7439 	return 0;
7440 error:
7441 	if (wc.trans)
7442 		btrfs_end_transaction(wc.trans);
7443 	clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
7444 	btrfs_free_path(path);
7445 	return ret;
7446 }
7447 
7448 /*
7449  * there are some corner cases where we want to force a full
7450  * commit instead of allowing a directory to be logged.
7451  *
7452  * They revolve around files there were unlinked from the directory, and
7453  * this function updates the parent directory so that a full commit is
7454  * properly done if it is fsync'd later after the unlinks are done.
7455  *
7456  * Must be called before the unlink operations (updates to the subvolume tree,
7457  * inodes, etc) are done.
7458  */
btrfs_record_unlink_dir(struct btrfs_trans_handle * trans,struct btrfs_inode * dir,struct btrfs_inode * inode,bool for_rename)7459 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
7460 			     struct btrfs_inode *dir, struct btrfs_inode *inode,
7461 			     bool for_rename)
7462 {
7463 	/*
7464 	 * when we're logging a file, if it hasn't been renamed
7465 	 * or unlinked, and its inode is fully committed on disk,
7466 	 * we don't have to worry about walking up the directory chain
7467 	 * to log its parents.
7468 	 *
7469 	 * So, we use the last_unlink_trans field to put this transid
7470 	 * into the file.  When the file is logged we check it and
7471 	 * don't log the parents if the file is fully on disk.
7472 	 */
7473 	mutex_lock(&inode->log_mutex);
7474 	inode->last_unlink_trans = trans->transid;
7475 	mutex_unlock(&inode->log_mutex);
7476 
7477 	if (!for_rename)
7478 		return;
7479 
7480 	/*
7481 	 * If this directory was already logged, any new names will be logged
7482 	 * with btrfs_log_new_name() and old names will be deleted from the log
7483 	 * tree with btrfs_del_dir_entries_in_log() or with
7484 	 * btrfs_del_inode_ref_in_log().
7485 	 */
7486 	if (inode_logged(trans, dir, NULL) == 1)
7487 		return;
7488 
7489 	/*
7490 	 * If the inode we're about to unlink was logged before, the log will be
7491 	 * properly updated with the new name with btrfs_log_new_name() and the
7492 	 * old name removed with btrfs_del_dir_entries_in_log() or with
7493 	 * btrfs_del_inode_ref_in_log().
7494 	 */
7495 	if (inode_logged(trans, inode, NULL) == 1)
7496 		return;
7497 
7498 	/*
7499 	 * when renaming files across directories, if the directory
7500 	 * there we're unlinking from gets fsync'd later on, there's
7501 	 * no way to find the destination directory later and fsync it
7502 	 * properly.  So, we have to be conservative and force commits
7503 	 * so the new name gets discovered.
7504 	 */
7505 	mutex_lock(&dir->log_mutex);
7506 	dir->last_unlink_trans = trans->transid;
7507 	mutex_unlock(&dir->log_mutex);
7508 }
7509 
7510 /*
7511  * Make sure that if someone attempts to fsync the parent directory of a deleted
7512  * snapshot, it ends up triggering a transaction commit. This is to guarantee
7513  * that after replaying the log tree of the parent directory's root we will not
7514  * see the snapshot anymore and at log replay time we will not see any log tree
7515  * corresponding to the deleted snapshot's root, which could lead to replaying
7516  * it after replaying the log tree of the parent directory (which would replay
7517  * the snapshot delete operation).
7518  *
7519  * Must be called before the actual snapshot destroy operation (updates to the
7520  * parent root and tree of tree roots trees, etc) are done.
7521  */
btrfs_record_snapshot_destroy(struct btrfs_trans_handle * trans,struct btrfs_inode * dir)7522 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
7523 				   struct btrfs_inode *dir)
7524 {
7525 	mutex_lock(&dir->log_mutex);
7526 	dir->last_unlink_trans = trans->transid;
7527 	mutex_unlock(&dir->log_mutex);
7528 }
7529 
7530 /*
7531  * Call this when creating a subvolume in a directory.
7532  * Because we don't commit a transaction when creating a subvolume, we can't
7533  * allow the directory pointing to the subvolume to be logged with an entry that
7534  * points to an unpersisted root if we are still in the transaction used to
7535  * create the subvolume, so make any attempt to log the directory to result in a
7536  * full log sync.
7537  * Also we don't need to worry with renames, since btrfs_rename() marks the log
7538  * for full commit when renaming a subvolume.
7539  *
7540  * Must be called before creating the subvolume entry in its parent directory.
7541  */
btrfs_record_new_subvolume(const struct btrfs_trans_handle * trans,struct btrfs_inode * dir)7542 void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
7543 				struct btrfs_inode *dir)
7544 {
7545 	mutex_lock(&dir->log_mutex);
7546 	dir->last_unlink_trans = trans->transid;
7547 	mutex_unlock(&dir->log_mutex);
7548 }
7549 
7550 /*
7551  * Update the log after adding a new name for an inode.
7552  *
7553  * @trans:              Transaction handle.
7554  * @old_dentry:         The dentry associated with the old name and the old
7555  *                      parent directory.
7556  * @old_dir:            The inode of the previous parent directory for the case
7557  *                      of a rename. For a link operation, it must be NULL.
7558  * @old_dir_index:      The index number associated with the old name, meaningful
7559  *                      only for rename operations (when @old_dir is not NULL).
7560  *                      Ignored for link operations.
7561  * @parent:             The dentry associated with the directory under which the
7562  *                      new name is located.
7563  *
7564  * Call this after adding a new name for an inode, as a result of a link or
7565  * rename operation, and it will properly update the log to reflect the new name.
7566  */
btrfs_log_new_name(struct btrfs_trans_handle * trans,struct dentry * old_dentry,struct btrfs_inode * old_dir,u64 old_dir_index,struct dentry * parent)7567 void btrfs_log_new_name(struct btrfs_trans_handle *trans,
7568 			struct dentry *old_dentry, struct btrfs_inode *old_dir,
7569 			u64 old_dir_index, struct dentry *parent)
7570 {
7571 	struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry));
7572 	struct btrfs_root *root = inode->root;
7573 	struct btrfs_log_ctx ctx;
7574 	bool log_pinned = false;
7575 	int ret;
7576 
7577 	btrfs_init_log_ctx(&ctx, inode);
7578 	ctx.logging_new_name = true;
7579 
7580 	/*
7581 	 * this will force the logging code to walk the dentry chain
7582 	 * up for the file
7583 	 */
7584 	if (!S_ISDIR(inode->vfs_inode.i_mode))
7585 		inode->last_unlink_trans = trans->transid;
7586 
7587 	/*
7588 	 * if this inode hasn't been logged and directory we're renaming it
7589 	 * from hasn't been logged, we don't need to log it
7590 	 */
7591 	ret = inode_logged(trans, inode, NULL);
7592 	if (ret < 0) {
7593 		goto out;
7594 	} else if (ret == 0) {
7595 		if (!old_dir)
7596 			return;
7597 		/*
7598 		 * If the inode was not logged and we are doing a rename (old_dir is not
7599 		 * NULL), check if old_dir was logged - if it was not we can return and
7600 		 * do nothing.
7601 		 */
7602 		ret = inode_logged(trans, old_dir, NULL);
7603 		if (ret < 0)
7604 			goto out;
7605 		else if (ret == 0)
7606 			return;
7607 	}
7608 	ret = 0;
7609 
7610 	/*
7611 	 * Now that we know we need to update the log, allocate the scratch eb
7612 	 * for the context before joining a log transaction below, as this can
7613 	 * take time and therefore we could delay log commits from other tasks.
7614 	 */
7615 	btrfs_init_log_ctx_scratch_eb(&ctx);
7616 
7617 	/*
7618 	 * If we are doing a rename (old_dir is not NULL) from a directory that
7619 	 * was previously logged, make sure that on log replay we get the old
7620 	 * dir entry deleted. This is needed because we will also log the new
7621 	 * name of the renamed inode, so we need to make sure that after log
7622 	 * replay we don't end up with both the new and old dir entries existing.
7623 	 */
7624 	if (old_dir && old_dir->logged_trans == trans->transid) {
7625 		struct btrfs_root *log = old_dir->root->log_root;
7626 		struct btrfs_path *path;
7627 		struct fscrypt_name fname;
7628 
7629 		ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX);
7630 
7631 		ret = fscrypt_setup_filename(&old_dir->vfs_inode,
7632 					     &old_dentry->d_name, 0, &fname);
7633 		if (ret)
7634 			goto out;
7635 
7636 		path = btrfs_alloc_path();
7637 		if (!path) {
7638 			ret = -ENOMEM;
7639 			fscrypt_free_filename(&fname);
7640 			goto out;
7641 		}
7642 
7643 		/*
7644 		 * We have two inodes to update in the log, the old directory and
7645 		 * the inode that got renamed, so we must pin the log to prevent
7646 		 * anyone from syncing the log until we have updated both inodes
7647 		 * in the log.
7648 		 */
7649 		ret = join_running_log_trans(root);
7650 		/*
7651 		 * At least one of the inodes was logged before, so this should
7652 		 * not fail, but if it does, it's not serious, just bail out and
7653 		 * mark the log for a full commit.
7654 		 */
7655 		if (WARN_ON_ONCE(ret < 0)) {
7656 			btrfs_free_path(path);
7657 			fscrypt_free_filename(&fname);
7658 			goto out;
7659 		}
7660 
7661 		log_pinned = true;
7662 
7663 		/*
7664 		 * Other concurrent task might be logging the old directory,
7665 		 * as it can be triggered when logging other inode that had or
7666 		 * still has a dentry in the old directory. We lock the old
7667 		 * directory's log_mutex to ensure the deletion of the old
7668 		 * name is persisted, because during directory logging we
7669 		 * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of
7670 		 * the old name's dir index item is in the delayed items, so
7671 		 * it could be missed by an in progress directory logging.
7672 		 */
7673 		mutex_lock(&old_dir->log_mutex);
7674 		ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir),
7675 					&fname.disk_name, old_dir_index);
7676 		if (ret > 0) {
7677 			/*
7678 			 * The dentry does not exist in the log, so record its
7679 			 * deletion.
7680 			 */
7681 			btrfs_release_path(path);
7682 			ret = insert_dir_log_key(trans, log, path,
7683 						 btrfs_ino(old_dir),
7684 						 old_dir_index, old_dir_index);
7685 		}
7686 		mutex_unlock(&old_dir->log_mutex);
7687 
7688 		btrfs_free_path(path);
7689 		fscrypt_free_filename(&fname);
7690 		if (ret < 0)
7691 			goto out;
7692 	}
7693 
7694 	/*
7695 	 * We don't care about the return value. If we fail to log the new name
7696 	 * then we know the next attempt to sync the log will fallback to a full
7697 	 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
7698 	 * we don't need to worry about getting a log committed that has an
7699 	 * inconsistent state after a rename operation.
7700 	 */
7701 	btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
7702 	ASSERT(list_empty(&ctx.conflict_inodes));
7703 out:
7704 	/*
7705 	 * If an error happened mark the log for a full commit because it's not
7706 	 * consistent and up to date or we couldn't find out if one of the
7707 	 * inodes was logged before in this transaction. Do it before unpinning
7708 	 * the log, to avoid any races with someone else trying to commit it.
7709 	 */
7710 	if (ret < 0)
7711 		btrfs_set_log_full_commit(trans);
7712 	if (log_pinned)
7713 		btrfs_end_log_trans(root);
7714 	free_extent_buffer(ctx.scratch_eb);
7715 }
7716 
7717