blob: 62784b99a80741bfecc9ae40d7fffc27cd34f099 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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>
David Brazdil0f672f62019-12-10 10:32:29 +000011#include "misc.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include "ctree.h"
13#include "tree-log.h"
14#include "disk-io.h"
15#include "locking.h"
16#include "print-tree.h"
17#include "backref.h"
18#include "compression.h"
19#include "qgroup.h"
20#include "inode-map.h"
Olivier Deprez157378f2022-04-04 15:47:50 +020021#include "block-group.h"
22#include "space-info.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023
24/* magic values for the inode_only field in btrfs_log_inode:
25 *
26 * LOG_INODE_ALL means to log everything
27 * LOG_INODE_EXISTS means to log just enough to recreate the inode
28 * during log replay
29 */
David Brazdil0f672f62019-12-10 10:32:29 +000030enum {
31 LOG_INODE_ALL,
32 LOG_INODE_EXISTS,
33 LOG_OTHER_INODE,
34 LOG_OTHER_INODE_ALL,
35};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
37/*
38 * directory trouble cases
39 *
40 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
41 * log, we must force a full commit before doing an fsync of the directory
42 * where the unlink was done.
43 * ---> record transid of last unlink/rename per directory
44 *
45 * mkdir foo/some_dir
46 * normal commit
47 * rename foo/some_dir foo2/some_dir
48 * mkdir foo/some_dir
49 * fsync foo/some_dir/some_file
50 *
51 * The fsync above will unlink the original some_dir without recording
52 * it in its new location (foo2). After a crash, some_dir will be gone
53 * unless the fsync of some_file forces a full commit
54 *
55 * 2) we must log any new names for any file or dir that is in the fsync
56 * log. ---> check inode while renaming/linking.
57 *
58 * 2a) we must log any new names for any file or dir during rename
59 * when the directory they are being removed from was logged.
60 * ---> check inode and old parent dir during rename
61 *
62 * 2a is actually the more important variant. With the extra logging
63 * a crash might unlink the old name without recreating the new one
64 *
65 * 3) after a crash, we must go through any directories with a link count
66 * of zero and redo the rm -rf
67 *
68 * mkdir f1/foo
69 * normal commit
70 * rm -rf f1/foo
71 * fsync(f1)
72 *
73 * The directory f1 was fully removed from the FS, but fsync was never
74 * called on f1, only its parent dir. After a crash the rm -rf must
75 * be replayed. This must be able to recurse down the entire
76 * directory tree. The inode link count fixup code takes care of the
77 * ugly details.
78 */
79
80/*
81 * stages for the tree walking. The first
82 * stage (0) is to only pin down the blocks we find
83 * the second stage (1) is to make sure that all the inodes
84 * we find in the log are created in the subvolume.
85 *
86 * The last stage is to deal with directories and links and extents
87 * and all the other fun semantics
88 */
David Brazdil0f672f62019-12-10 10:32:29 +000089enum {
90 LOG_WALK_PIN_ONLY,
91 LOG_WALK_REPLAY_INODES,
92 LOG_WALK_REPLAY_DIR_INDEX,
93 LOG_WALK_REPLAY_ALL,
94};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095
96static int btrfs_log_inode(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, struct btrfs_inode *inode,
98 int inode_only,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 struct btrfs_log_ctx *ctx);
100static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_path *path, u64 objectid);
103static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root,
105 struct btrfs_root *log,
106 struct btrfs_path *path,
107 u64 dirid, int del_all);
108
109/*
110 * tree logging is a special write ahead log used to make sure that
111 * fsyncs and O_SYNCs can happen without doing full tree commits.
112 *
113 * Full tree commits are expensive because they require commonly
114 * modified blocks to be recowed, creating many dirty pages in the
115 * extent tree an 4x-6x higher write load than ext3.
116 *
117 * Instead of doing a tree commit on every fsync, we use the
118 * key ranges and transaction ids to find items for a given file or directory
119 * that have changed in this transaction. Those items are copied into
120 * a special tree (one per subvolume root), that tree is written to disk
121 * and then the fsync is considered complete.
122 *
123 * After a crash, items are copied out of the log-tree back into the
124 * subvolume tree. Any file data extents found are recorded in the extent
125 * allocation tree, and the log-tree freed.
126 *
127 * The log tree is read three times, once to pin down all the extents it is
128 * using in ram and once, once to create all the inodes logged in the tree
129 * and once to do all the other items.
130 */
131
132/*
133 * start a sub transaction and setup the log tree
134 * this increments the log tree writer count to make the people
135 * syncing the tree wait for us to finish
136 */
137static int start_log_trans(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root,
139 struct btrfs_log_ctx *ctx)
140{
141 struct btrfs_fs_info *fs_info = root->fs_info;
142 int ret = 0;
143
144 mutex_lock(&root->log_mutex);
145
146 if (root->log_root) {
David Brazdil0f672f62019-12-10 10:32:29 +0000147 if (btrfs_need_log_full_commit(trans)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 ret = -EAGAIN;
149 goto out;
150 }
151
152 if (!root->log_start_pid) {
153 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
154 root->log_start_pid = current->pid;
155 } else if (root->log_start_pid != current->pid) {
156 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
157 }
158 } else {
159 mutex_lock(&fs_info->tree_log_mutex);
160 if (!fs_info->log_root_tree)
161 ret = btrfs_init_log_root_tree(trans, fs_info);
162 mutex_unlock(&fs_info->tree_log_mutex);
163 if (ret)
164 goto out;
165
166 ret = btrfs_add_log_tree(trans, root);
167 if (ret)
168 goto out;
169
Olivier Deprez0e641232021-09-23 10:07:05 +0200170 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
172 root->log_start_pid = current->pid;
173 }
174
175 atomic_inc(&root->log_batch);
176 atomic_inc(&root->log_writers);
Olivier Deprez0e641232021-09-23 10:07:05 +0200177 if (ctx && !ctx->logging_new_name) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 int index = root->log_transid % 2;
179 list_add_tail(&ctx->list, &root->log_ctxs[index]);
180 ctx->log_transid = root->log_transid;
181 }
182
183out:
184 mutex_unlock(&root->log_mutex);
185 return ret;
186}
187
188/*
189 * returns 0 if there was a log transaction running and we were able
190 * to join, or returns -ENOENT if there were not transactions
191 * in progress
192 */
193static int join_running_log_trans(struct btrfs_root *root)
194{
195 int ret = -ENOENT;
196
Olivier Deprez0e641232021-09-23 10:07:05 +0200197 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
198 return ret;
199
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 mutex_lock(&root->log_mutex);
201 if (root->log_root) {
202 ret = 0;
203 atomic_inc(&root->log_writers);
204 }
205 mutex_unlock(&root->log_mutex);
206 return ret;
207}
208
209/*
210 * This either makes the current running log transaction wait
211 * until you call btrfs_end_log_trans() or it makes any future
212 * log transactions wait until you call btrfs_end_log_trans()
213 */
David Brazdil0f672f62019-12-10 10:32:29 +0000214void btrfs_pin_log_trans(struct btrfs_root *root)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 atomic_inc(&root->log_writers);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217}
218
219/*
220 * indicate we're done making changes to the log tree
221 * and wake up anyone waiting to do a sync
222 */
223void btrfs_end_log_trans(struct btrfs_root *root)
224{
225 if (atomic_dec_and_test(&root->log_writers)) {
226 /* atomic_dec_and_test implies a barrier */
227 cond_wake_up_nomb(&root->log_writer_wait);
228 }
229}
230
David Brazdil0f672f62019-12-10 10:32:29 +0000231static int btrfs_write_tree_block(struct extent_buffer *buf)
232{
233 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
234 buf->start + buf->len - 1);
235}
236
237static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
238{
239 filemap_fdatawait_range(buf->pages[0]->mapping,
240 buf->start, buf->start + buf->len - 1);
241}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242
243/*
244 * the walk control struct is used to pass state down the chain when
245 * processing the log tree. The stage field tells us which part
246 * of the log tree processing we are currently doing. The others
247 * are state fields used for that specific part
248 */
249struct walk_control {
250 /* should we free the extent on disk when done? This is used
251 * at transaction commit time while freeing a log tree
252 */
253 int free;
254
255 /* should we write out the extent buffer? This is used
256 * while flushing the log tree to disk during a sync
257 */
258 int write;
259
260 /* should we wait for the extent buffer io to finish? Also used
261 * while flushing the log tree to disk for a sync
262 */
263 int wait;
264
265 /* pin only walk, we record which extents on disk belong to the
266 * log trees
267 */
268 int pin;
269
270 /* what stage of the replay code we're currently in */
271 int stage;
272
273 /*
274 * Ignore any items from the inode currently being processed. Needs
275 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
276 * the LOG_WALK_REPLAY_INODES stage.
277 */
278 bool ignore_cur_inode;
279
280 /* the root we are currently replaying */
281 struct btrfs_root *replay_dest;
282
283 /* the trans handle for the current replay */
284 struct btrfs_trans_handle *trans;
285
286 /* the function that gets used to process blocks we find in the
287 * tree. Note the extent_buffer might not be up to date when it is
288 * passed in, and it must be checked or read if you need the data
289 * inside it
290 */
291 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
292 struct walk_control *wc, u64 gen, int level);
293};
294
295/*
296 * process_func used to pin down extents, write them or wait on them
297 */
298static int process_one_buffer(struct btrfs_root *log,
299 struct extent_buffer *eb,
300 struct walk_control *wc, u64 gen, int level)
301{
302 struct btrfs_fs_info *fs_info = log->fs_info;
303 int ret = 0;
304
305 /*
306 * If this fs is mixed then we need to be able to process the leaves to
307 * pin down any logged extents, so we have to read the block.
308 */
309 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
310 ret = btrfs_read_buffer(eb, gen, level, NULL);
311 if (ret)
312 return ret;
313 }
314
315 if (wc->pin)
Olivier Deprez157378f2022-04-04 15:47:50 +0200316 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317 eb->len);
318
319 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
320 if (wc->pin && btrfs_header_level(eb) == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000321 ret = btrfs_exclude_logged_extents(eb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 if (wc->write)
323 btrfs_write_tree_block(eb);
324 if (wc->wait)
325 btrfs_wait_tree_block_writeback(eb);
326 }
327 return ret;
328}
329
330/*
331 * Item overwrite used by replay and tree logging. eb, slot and key all refer
332 * to the src data we are copying out.
333 *
334 * root is the tree we are copying into, and path is a scratch
335 * path for use in this function (it should be released on entry and
336 * will be released on exit).
337 *
338 * If the key is already in the destination tree the existing item is
339 * overwritten. If the existing item isn't big enough, it is extended.
340 * If it is too large, it is truncated.
341 *
342 * If the key isn't in the destination yet, a new item is inserted.
343 */
344static noinline int overwrite_item(struct btrfs_trans_handle *trans,
345 struct btrfs_root *root,
346 struct btrfs_path *path,
347 struct extent_buffer *eb, int slot,
348 struct btrfs_key *key)
349{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350 int ret;
351 u32 item_size;
352 u64 saved_i_size = 0;
353 int save_old_i_size = 0;
354 unsigned long src_ptr;
355 unsigned long dst_ptr;
356 int overwrite_root = 0;
357 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
358
359 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
360 overwrite_root = 1;
361
362 item_size = btrfs_item_size_nr(eb, slot);
363 src_ptr = btrfs_item_ptr_offset(eb, slot);
364
365 /* look for the key in the destination tree */
366 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
367 if (ret < 0)
368 return ret;
369
370 if (ret == 0) {
371 char *src_copy;
372 char *dst_copy;
373 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
374 path->slots[0]);
375 if (dst_size != item_size)
376 goto insert;
377
378 if (item_size == 0) {
379 btrfs_release_path(path);
380 return 0;
381 }
382 dst_copy = kmalloc(item_size, GFP_NOFS);
383 src_copy = kmalloc(item_size, GFP_NOFS);
384 if (!dst_copy || !src_copy) {
385 btrfs_release_path(path);
386 kfree(dst_copy);
387 kfree(src_copy);
388 return -ENOMEM;
389 }
390
391 read_extent_buffer(eb, src_copy, src_ptr, item_size);
392
393 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
394 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
395 item_size);
396 ret = memcmp(dst_copy, src_copy, item_size);
397
398 kfree(dst_copy);
399 kfree(src_copy);
400 /*
401 * they have the same contents, just return, this saves
402 * us from cowing blocks in the destination tree and doing
403 * extra writes that may not have been done by a previous
404 * sync
405 */
406 if (ret == 0) {
407 btrfs_release_path(path);
408 return 0;
409 }
410
411 /*
412 * We need to load the old nbytes into the inode so when we
413 * replay the extents we've logged we get the right nbytes.
414 */
415 if (inode_item) {
416 struct btrfs_inode_item *item;
417 u64 nbytes;
418 u32 mode;
419
420 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
421 struct btrfs_inode_item);
422 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
423 item = btrfs_item_ptr(eb, slot,
424 struct btrfs_inode_item);
425 btrfs_set_inode_nbytes(eb, item, nbytes);
426
427 /*
428 * If this is a directory we need to reset the i_size to
429 * 0 so that we can set it up properly when replaying
430 * the rest of the items in this log.
431 */
432 mode = btrfs_inode_mode(eb, item);
433 if (S_ISDIR(mode))
434 btrfs_set_inode_size(eb, item, 0);
435 }
436 } else if (inode_item) {
437 struct btrfs_inode_item *item;
438 u32 mode;
439
440 /*
441 * New inode, set nbytes to 0 so that the nbytes comes out
442 * properly when we replay the extents.
443 */
444 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
445 btrfs_set_inode_nbytes(eb, item, 0);
446
447 /*
448 * If this is a directory we need to reset the i_size to 0 so
449 * that we can set it up properly when replaying the rest of
450 * the items in this log.
451 */
452 mode = btrfs_inode_mode(eb, item);
453 if (S_ISDIR(mode))
454 btrfs_set_inode_size(eb, item, 0);
455 }
456insert:
457 btrfs_release_path(path);
458 /* try to insert the key into the destination tree */
459 path->skip_release_on_error = 1;
460 ret = btrfs_insert_empty_item(trans, root, path,
461 key, item_size);
462 path->skip_release_on_error = 0;
463
464 /* make sure any existing item is the correct size */
465 if (ret == -EEXIST || ret == -EOVERFLOW) {
466 u32 found_size;
467 found_size = btrfs_item_size_nr(path->nodes[0],
468 path->slots[0]);
469 if (found_size > item_size)
David Brazdil0f672f62019-12-10 10:32:29 +0000470 btrfs_truncate_item(path, item_size, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000471 else if (found_size < item_size)
David Brazdil0f672f62019-12-10 10:32:29 +0000472 btrfs_extend_item(path, item_size - found_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 } else if (ret) {
474 return ret;
475 }
476 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
477 path->slots[0]);
478
479 /* don't overwrite an existing inode if the generation number
480 * was logged as zero. This is done when the tree logging code
481 * is just logging an inode to make sure it exists after recovery.
482 *
483 * Also, don't overwrite i_size on directories during replay.
484 * log replay inserts and removes directory items based on the
485 * state of the tree found in the subvolume, and i_size is modified
486 * as it goes
487 */
488 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
489 struct btrfs_inode_item *src_item;
490 struct btrfs_inode_item *dst_item;
491
492 src_item = (struct btrfs_inode_item *)src_ptr;
493 dst_item = (struct btrfs_inode_item *)dst_ptr;
494
495 if (btrfs_inode_generation(eb, src_item) == 0) {
496 struct extent_buffer *dst_eb = path->nodes[0];
497 const u64 ino_size = btrfs_inode_size(eb, src_item);
498
499 /*
500 * For regular files an ino_size == 0 is used only when
501 * logging that an inode exists, as part of a directory
502 * fsync, and the inode wasn't fsynced before. In this
503 * case don't set the size of the inode in the fs/subvol
504 * tree, otherwise we would be throwing valid data away.
505 */
506 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
507 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200508 ino_size != 0)
509 btrfs_set_inode_size(dst_eb, dst_item, ino_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 goto no_copy;
511 }
512
513 if (overwrite_root &&
514 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
515 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
516 save_old_i_size = 1;
517 saved_i_size = btrfs_inode_size(path->nodes[0],
518 dst_item);
519 }
520 }
521
522 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
523 src_ptr, item_size);
524
525 if (save_old_i_size) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
529 }
530
531 /* make sure the generation is filled in */
532 if (key->type == BTRFS_INODE_ITEM_KEY) {
533 struct btrfs_inode_item *dst_item;
534 dst_item = (struct btrfs_inode_item *)dst_ptr;
535 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
536 btrfs_set_inode_generation(path->nodes[0], dst_item,
537 trans->transid);
538 }
539 }
540no_copy:
541 btrfs_mark_buffer_dirty(path->nodes[0]);
542 btrfs_release_path(path);
543 return 0;
544}
545
546/*
547 * simple helper to read an inode off the disk from a given root
548 * This can only be called for subvolume roots and not for the log
549 */
550static noinline struct inode *read_one_inode(struct btrfs_root *root,
551 u64 objectid)
552{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553 struct inode *inode;
554
Olivier Deprez157378f2022-04-04 15:47:50 +0200555 inode = btrfs_iget(root->fs_info->sb, objectid, root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000556 if (IS_ERR(inode))
557 inode = NULL;
558 return inode;
559}
560
561/* replays a single extent in 'eb' at 'slot' with 'key' into the
562 * subvolume 'root'. path is released on entry and should be released
563 * on exit.
564 *
565 * extents in the log tree have not been allocated out of the extent
566 * tree yet. So, this completes the allocation, taking a reference
567 * as required if the extent already exists or creating a new extent
568 * if it isn't in the extent allocation tree yet.
569 *
570 * The extent is inserted into the file, dropping any existing extents
571 * from the file that overlap the new one.
572 */
573static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
574 struct btrfs_root *root,
575 struct btrfs_path *path,
576 struct extent_buffer *eb, int slot,
577 struct btrfs_key *key)
578{
579 struct btrfs_fs_info *fs_info = root->fs_info;
580 int found_type;
581 u64 extent_end;
582 u64 start = key->offset;
583 u64 nbytes = 0;
584 struct btrfs_file_extent_item *item;
585 struct inode *inode = NULL;
586 unsigned long size;
587 int ret = 0;
588
589 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
590 found_type = btrfs_file_extent_type(eb, item);
591
592 if (found_type == BTRFS_FILE_EXTENT_REG ||
593 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
594 nbytes = btrfs_file_extent_num_bytes(eb, item);
595 extent_end = start + nbytes;
596
597 /*
598 * We don't add to the inodes nbytes if we are prealloc or a
599 * hole.
600 */
601 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
602 nbytes = 0;
603 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
604 size = btrfs_file_extent_ram_bytes(eb, item);
605 nbytes = btrfs_file_extent_ram_bytes(eb, item);
606 extent_end = ALIGN(start + size,
607 fs_info->sectorsize);
608 } else {
609 ret = 0;
610 goto out;
611 }
612
613 inode = read_one_inode(root, key->objectid);
614 if (!inode) {
615 ret = -EIO;
616 goto out;
617 }
618
619 /*
620 * first check to see if we already have this extent in the
621 * file. This must be done before the btrfs_drop_extents run
622 * so we don't try to drop this extent.
623 */
624 ret = btrfs_lookup_file_extent(trans, root, path,
625 btrfs_ino(BTRFS_I(inode)), start, 0);
626
627 if (ret == 0 &&
628 (found_type == BTRFS_FILE_EXTENT_REG ||
629 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
630 struct btrfs_file_extent_item cmp1;
631 struct btrfs_file_extent_item cmp2;
632 struct btrfs_file_extent_item *existing;
633 struct extent_buffer *leaf;
634
635 leaf = path->nodes[0];
636 existing = btrfs_item_ptr(leaf, path->slots[0],
637 struct btrfs_file_extent_item);
638
639 read_extent_buffer(eb, &cmp1, (unsigned long)item,
640 sizeof(cmp1));
641 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
642 sizeof(cmp2));
643
644 /*
645 * we already have a pointer to this exact extent,
646 * we don't have to do anything
647 */
648 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
649 btrfs_release_path(path);
650 goto out;
651 }
652 }
653 btrfs_release_path(path);
654
655 /* drop any overlapping extents */
656 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
657 if (ret)
658 goto out;
659
660 if (found_type == BTRFS_FILE_EXTENT_REG ||
661 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
662 u64 offset;
663 unsigned long dest_offset;
664 struct btrfs_key ins;
665
666 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
667 btrfs_fs_incompat(fs_info, NO_HOLES))
668 goto update_inode;
669
670 ret = btrfs_insert_empty_item(trans, root, path, key,
671 sizeof(*item));
672 if (ret)
673 goto out;
674 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
675 path->slots[0]);
676 copy_extent_buffer(path->nodes[0], eb, dest_offset,
677 (unsigned long)item, sizeof(*item));
678
679 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
680 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
681 ins.type = BTRFS_EXTENT_ITEM_KEY;
682 offset = key->offset - btrfs_file_extent_offset(eb, item);
683
684 /*
685 * Manually record dirty extent, as here we did a shallow
686 * file extent item copy and skip normal backref update,
687 * but modifying extent tree all by ourselves.
688 * So need to manually record dirty extent for qgroup,
689 * as the owner of the file extent changed from log tree
690 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
691 */
692 ret = btrfs_qgroup_trace_extent(trans,
693 btrfs_file_extent_disk_bytenr(eb, item),
694 btrfs_file_extent_disk_num_bytes(eb, item),
695 GFP_NOFS);
696 if (ret < 0)
697 goto out;
698
699 if (ins.objectid > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000700 struct btrfs_ref ref = { 0 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701 u64 csum_start;
702 u64 csum_end;
703 LIST_HEAD(ordered_sums);
David Brazdil0f672f62019-12-10 10:32:29 +0000704
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705 /*
706 * is this extent already allocated in the extent
707 * allocation tree? If so, just add a reference
708 */
709 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
710 ins.offset);
Olivier Deprez0e641232021-09-23 10:07:05 +0200711 if (ret < 0) {
712 goto out;
713 } else if (ret == 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000714 btrfs_init_generic_ref(&ref,
715 BTRFS_ADD_DELAYED_REF,
716 ins.objectid, ins.offset, 0);
717 btrfs_init_data_ref(&ref,
718 root->root_key.objectid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719 key->objectid, offset);
David Brazdil0f672f62019-12-10 10:32:29 +0000720 ret = btrfs_inc_extent_ref(trans, &ref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 if (ret)
722 goto out;
723 } else {
724 /*
725 * insert the extent pointer in the extent
726 * allocation tree
727 */
728 ret = btrfs_alloc_logged_file_extent(trans,
729 root->root_key.objectid,
730 key->objectid, offset, &ins);
731 if (ret)
732 goto out;
733 }
734 btrfs_release_path(path);
735
736 if (btrfs_file_extent_compression(eb, item)) {
737 csum_start = ins.objectid;
738 csum_end = csum_start + ins.offset;
739 } else {
740 csum_start = ins.objectid +
741 btrfs_file_extent_offset(eb, item);
742 csum_end = csum_start +
743 btrfs_file_extent_num_bytes(eb, item);
744 }
745
746 ret = btrfs_lookup_csums_range(root->log_root,
747 csum_start, csum_end - 1,
748 &ordered_sums, 0);
749 if (ret)
750 goto out;
751 /*
752 * Now delete all existing cums in the csum root that
753 * cover our range. We do this because we can have an
754 * extent that is completely referenced by one file
755 * extent item and partially referenced by another
756 * file extent item (like after using the clone or
757 * extent_same ioctls). In this case if we end up doing
758 * the replay of the one that partially references the
759 * extent first, and we do not do the csum deletion
760 * below, we can get 2 csum items in the csum tree that
761 * overlap each other. For example, imagine our log has
762 * the two following file extent items:
763 *
764 * key (257 EXTENT_DATA 409600)
765 * extent data disk byte 12845056 nr 102400
766 * extent data offset 20480 nr 20480 ram 102400
767 *
768 * key (257 EXTENT_DATA 819200)
769 * extent data disk byte 12845056 nr 102400
770 * extent data offset 0 nr 102400 ram 102400
771 *
772 * Where the second one fully references the 100K extent
773 * that starts at disk byte 12845056, and the log tree
774 * has a single csum item that covers the entire range
775 * of the extent:
776 *
777 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
778 *
779 * After the first file extent item is replayed, the
780 * csum tree gets the following csum item:
781 *
782 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
783 *
784 * Which covers the 20K sub-range starting at offset 20K
785 * of our extent. Now when we replay the second file
786 * extent item, if we do not delete existing csum items
787 * that cover any of its blocks, we end up getting two
788 * csum items in our csum tree that overlap each other:
789 *
790 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
791 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
792 *
793 * Which is a problem, because after this anyone trying
794 * to lookup up for the checksum of any block of our
795 * extent starting at an offset of 40K or higher, will
796 * end up looking at the second csum item only, which
797 * does not contain the checksum for any block starting
798 * at offset 40K or higher of our extent.
799 */
800 while (!list_empty(&ordered_sums)) {
801 struct btrfs_ordered_sum *sums;
802 sums = list_entry(ordered_sums.next,
803 struct btrfs_ordered_sum,
804 list);
805 if (!ret)
Olivier Deprez0e641232021-09-23 10:07:05 +0200806 ret = btrfs_del_csums(trans,
807 fs_info->csum_root,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000808 sums->bytenr,
809 sums->len);
810 if (!ret)
811 ret = btrfs_csum_file_blocks(trans,
812 fs_info->csum_root, sums);
813 list_del(&sums->list);
814 kfree(sums);
815 }
816 if (ret)
817 goto out;
818 } else {
819 btrfs_release_path(path);
820 }
821 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
822 /* inline extents are easy, we just overwrite them */
823 ret = overwrite_item(trans, root, path, eb, slot, key);
824 if (ret)
825 goto out;
826 }
827
Olivier Deprez157378f2022-04-04 15:47:50 +0200828 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
829 extent_end - start);
830 if (ret)
831 goto out;
832
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000833 inode_add_bytes(inode, nbytes);
834update_inode:
835 ret = btrfs_update_inode(trans, root, inode);
836out:
837 if (inode)
838 iput(inode);
839 return ret;
840}
841
842/*
843 * when cleaning up conflicts between the directory names in the
844 * subvolume, directory names in the log and directory names in the
845 * inode back references, we may have to unlink inodes from directories.
846 *
847 * This is a helper function to do the unlink of a specific directory
848 * item
849 */
850static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
851 struct btrfs_root *root,
852 struct btrfs_path *path,
853 struct btrfs_inode *dir,
854 struct btrfs_dir_item *di)
855{
856 struct inode *inode;
857 char *name;
858 int name_len;
859 struct extent_buffer *leaf;
860 struct btrfs_key location;
861 int ret;
862
863 leaf = path->nodes[0];
864
865 btrfs_dir_item_key_to_cpu(leaf, di, &location);
866 name_len = btrfs_dir_name_len(leaf, di);
867 name = kmalloc(name_len, GFP_NOFS);
868 if (!name)
869 return -ENOMEM;
870
871 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
872 btrfs_release_path(path);
873
874 inode = read_one_inode(root, location.objectid);
875 if (!inode) {
876 ret = -EIO;
877 goto out;
878 }
879
880 ret = link_to_fixup_dir(trans, root, path, location.objectid);
881 if (ret)
882 goto out;
883
884 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
885 name_len);
886 if (ret)
887 goto out;
888 else
889 ret = btrfs_run_delayed_items(trans);
890out:
891 kfree(name);
892 iput(inode);
893 return ret;
894}
895
896/*
Olivier Deprez157378f2022-04-04 15:47:50 +0200897 * See if a given name and sequence number found in an inode back reference are
898 * already in a directory and correctly point to this inode.
899 *
900 * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
901 * exists.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000902 */
903static noinline int inode_in_dir(struct btrfs_root *root,
904 struct btrfs_path *path,
905 u64 dirid, u64 objectid, u64 index,
906 const char *name, int name_len)
907{
908 struct btrfs_dir_item *di;
909 struct btrfs_key location;
Olivier Deprez157378f2022-04-04 15:47:50 +0200910 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911
912 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
913 index, name, name_len, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200914 if (IS_ERR(di)) {
915 if (PTR_ERR(di) != -ENOENT)
916 ret = PTR_ERR(di);
917 goto out;
918 } else if (di) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
920 if (location.objectid != objectid)
921 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200922 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200924 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925
Olivier Deprez157378f2022-04-04 15:47:50 +0200926 btrfs_release_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200928 if (IS_ERR(di)) {
929 ret = PTR_ERR(di);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200931 } else if (di) {
932 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
933 if (location.objectid == objectid)
934 ret = 1;
935 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936out:
937 btrfs_release_path(path);
Olivier Deprez157378f2022-04-04 15:47:50 +0200938 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939}
940
941/*
942 * helper function to check a log tree for a named back reference in
943 * an inode. This is used to decide if a back reference that is
944 * found in the subvolume conflicts with what we find in the log.
945 *
946 * inode backreferences may have multiple refs in a single item,
947 * during replay we process one reference at a time, and we don't
948 * want to delete valid links to a file from the subvolume if that
949 * link is also in the log.
950 */
951static noinline int backref_in_log(struct btrfs_root *log,
952 struct btrfs_key *key,
953 u64 ref_objectid,
954 const char *name, int namelen)
955{
956 struct btrfs_path *path;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958
959 path = btrfs_alloc_path();
960 if (!path)
961 return -ENOMEM;
962
963 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +0200964 if (ret < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000965 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200966 } else if (ret == 1) {
967 ret = 0;
968 goto out;
969 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000970
Olivier Deprez157378f2022-04-04 15:47:50 +0200971 if (key->type == BTRFS_INODE_EXTREF_KEY)
972 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
973 path->slots[0],
974 ref_objectid,
975 name, namelen);
976 else
977 ret = !!btrfs_find_name_in_backref(path->nodes[0],
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000978 path->slots[0],
Olivier Deprez157378f2022-04-04 15:47:50 +0200979 name, namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980out:
981 btrfs_free_path(path);
Olivier Deprez157378f2022-04-04 15:47:50 +0200982 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000983}
984
985static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
988 struct btrfs_root *log_root,
989 struct btrfs_inode *dir,
990 struct btrfs_inode *inode,
991 u64 inode_objectid, u64 parent_objectid,
992 u64 ref_index, char *name, int namelen,
993 int *search_done)
994{
995 int ret;
996 char *victim_name;
997 int victim_name_len;
998 struct extent_buffer *leaf;
999 struct btrfs_dir_item *di;
1000 struct btrfs_key search_key;
1001 struct btrfs_inode_extref *extref;
1002
1003again:
1004 /* Search old style refs */
1005 search_key.objectid = inode_objectid;
1006 search_key.type = BTRFS_INODE_REF_KEY;
1007 search_key.offset = parent_objectid;
1008 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1009 if (ret == 0) {
1010 struct btrfs_inode_ref *victim_ref;
1011 unsigned long ptr;
1012 unsigned long ptr_end;
1013
1014 leaf = path->nodes[0];
1015
1016 /* are we trying to overwrite a back ref for the root directory
1017 * if so, just jump out, we're done
1018 */
1019 if (search_key.objectid == search_key.offset)
1020 return 1;
1021
1022 /* check all the names in this back reference to see
1023 * if they are in the log. if so, we allow them to stay
1024 * otherwise they must be unlinked as a conflict
1025 */
1026 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1027 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1028 while (ptr < ptr_end) {
1029 victim_ref = (struct btrfs_inode_ref *)ptr;
1030 victim_name_len = btrfs_inode_ref_name_len(leaf,
1031 victim_ref);
1032 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1033 if (!victim_name)
1034 return -ENOMEM;
1035
1036 read_extent_buffer(leaf, victim_name,
1037 (unsigned long)(victim_ref + 1),
1038 victim_name_len);
1039
Olivier Deprez157378f2022-04-04 15:47:50 +02001040 ret = backref_in_log(log_root, &search_key,
1041 parent_objectid, victim_name,
1042 victim_name_len);
1043 if (ret < 0) {
1044 kfree(victim_name);
1045 return ret;
1046 } else if (!ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047 inc_nlink(&inode->vfs_inode);
1048 btrfs_release_path(path);
1049
1050 ret = btrfs_unlink_inode(trans, root, dir, inode,
1051 victim_name, victim_name_len);
1052 kfree(victim_name);
1053 if (ret)
1054 return ret;
1055 ret = btrfs_run_delayed_items(trans);
1056 if (ret)
1057 return ret;
1058 *search_done = 1;
1059 goto again;
1060 }
1061 kfree(victim_name);
1062
1063 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1064 }
1065
1066 /*
1067 * NOTE: we have searched root tree and checked the
1068 * corresponding ref, it does not need to check again.
1069 */
1070 *search_done = 1;
1071 }
1072 btrfs_release_path(path);
1073
1074 /* Same search but for extended refs */
1075 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1076 inode_objectid, parent_objectid, 0,
1077 0);
1078 if (!IS_ERR_OR_NULL(extref)) {
1079 u32 item_size;
1080 u32 cur_offset = 0;
1081 unsigned long base;
1082 struct inode *victim_parent;
1083
1084 leaf = path->nodes[0];
1085
1086 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1087 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1088
1089 while (cur_offset < item_size) {
1090 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1091
1092 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1093
1094 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1095 goto next;
1096
1097 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1098 if (!victim_name)
1099 return -ENOMEM;
1100 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1101 victim_name_len);
1102
1103 search_key.objectid = inode_objectid;
1104 search_key.type = BTRFS_INODE_EXTREF_KEY;
1105 search_key.offset = btrfs_extref_hash(parent_objectid,
1106 victim_name,
1107 victim_name_len);
Olivier Deprez157378f2022-04-04 15:47:50 +02001108 ret = backref_in_log(log_root, &search_key,
1109 parent_objectid, victim_name,
1110 victim_name_len);
1111 if (ret < 0) {
1112 kfree(victim_name);
1113 return ret;
1114 } else if (!ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115 ret = -ENOENT;
1116 victim_parent = read_one_inode(root,
1117 parent_objectid);
1118 if (victim_parent) {
1119 inc_nlink(&inode->vfs_inode);
1120 btrfs_release_path(path);
1121
1122 ret = btrfs_unlink_inode(trans, root,
1123 BTRFS_I(victim_parent),
1124 inode,
1125 victim_name,
1126 victim_name_len);
1127 if (!ret)
1128 ret = btrfs_run_delayed_items(
1129 trans);
1130 }
1131 iput(victim_parent);
1132 kfree(victim_name);
1133 if (ret)
1134 return ret;
1135 *search_done = 1;
1136 goto again;
1137 }
1138 kfree(victim_name);
1139next:
1140 cur_offset += victim_name_len + sizeof(*extref);
1141 }
1142 *search_done = 1;
1143 }
1144 btrfs_release_path(path);
1145
1146 /* look for a conflicting sequence number */
1147 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1148 ref_index, name, namelen, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +02001149 if (IS_ERR(di)) {
1150 if (PTR_ERR(di) != -ENOENT)
1151 return PTR_ERR(di);
1152 } else if (di) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001153 ret = drop_one_dir_item(trans, root, path, dir, di);
1154 if (ret)
1155 return ret;
1156 }
1157 btrfs_release_path(path);
1158
David Brazdil0f672f62019-12-10 10:32:29 +00001159 /* look for a conflicting name */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1161 name, namelen, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +02001162 if (IS_ERR(di)) {
1163 return PTR_ERR(di);
1164 } else if (di) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001165 ret = drop_one_dir_item(trans, root, path, dir, di);
1166 if (ret)
1167 return ret;
1168 }
1169 btrfs_release_path(path);
1170
1171 return 0;
1172}
1173
1174static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1175 u32 *namelen, char **name, u64 *index,
1176 u64 *parent_objectid)
1177{
1178 struct btrfs_inode_extref *extref;
1179
1180 extref = (struct btrfs_inode_extref *)ref_ptr;
1181
1182 *namelen = btrfs_inode_extref_name_len(eb, extref);
1183 *name = kmalloc(*namelen, GFP_NOFS);
1184 if (*name == NULL)
1185 return -ENOMEM;
1186
1187 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1188 *namelen);
1189
1190 if (index)
1191 *index = btrfs_inode_extref_index(eb, extref);
1192 if (parent_objectid)
1193 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1194
1195 return 0;
1196}
1197
1198static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1199 u32 *namelen, char **name, u64 *index)
1200{
1201 struct btrfs_inode_ref *ref;
1202
1203 ref = (struct btrfs_inode_ref *)ref_ptr;
1204
1205 *namelen = btrfs_inode_ref_name_len(eb, ref);
1206 *name = kmalloc(*namelen, GFP_NOFS);
1207 if (*name == NULL)
1208 return -ENOMEM;
1209
1210 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1211
1212 if (index)
1213 *index = btrfs_inode_ref_index(eb, ref);
1214
1215 return 0;
1216}
1217
1218/*
1219 * Take an inode reference item from the log tree and iterate all names from the
1220 * inode reference item in the subvolume tree with the same key (if it exists).
1221 * For any name that is not in the inode reference item from the log tree, do a
1222 * proper unlink of that name (that is, remove its entry from the inode
1223 * reference item and both dir index keys).
1224 */
1225static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1226 struct btrfs_root *root,
1227 struct btrfs_path *path,
1228 struct btrfs_inode *inode,
1229 struct extent_buffer *log_eb,
1230 int log_slot,
1231 struct btrfs_key *key)
1232{
1233 int ret;
1234 unsigned long ref_ptr;
1235 unsigned long ref_end;
1236 struct extent_buffer *eb;
1237
1238again:
1239 btrfs_release_path(path);
1240 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1241 if (ret > 0) {
1242 ret = 0;
1243 goto out;
1244 }
1245 if (ret < 0)
1246 goto out;
1247
1248 eb = path->nodes[0];
1249 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1250 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1251 while (ref_ptr < ref_end) {
1252 char *name = NULL;
1253 int namelen;
1254 u64 parent_id;
1255
1256 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1257 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1258 NULL, &parent_id);
1259 } else {
1260 parent_id = key->offset;
1261 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1262 NULL);
1263 }
1264 if (ret)
1265 goto out;
1266
1267 if (key->type == BTRFS_INODE_EXTREF_KEY)
David Brazdil0f672f62019-12-10 10:32:29 +00001268 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1269 parent_id, name,
1270 namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001271 else
David Brazdil0f672f62019-12-10 10:32:29 +00001272 ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
1273 name, namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001274
1275 if (!ret) {
1276 struct inode *dir;
1277
1278 btrfs_release_path(path);
1279 dir = read_one_inode(root, parent_id);
1280 if (!dir) {
1281 ret = -ENOENT;
1282 kfree(name);
1283 goto out;
1284 }
1285 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1286 inode, name, namelen);
1287 kfree(name);
1288 iput(dir);
Olivier Deprez157378f2022-04-04 15:47:50 +02001289 /*
1290 * Whenever we need to check if a name exists or not, we
1291 * check the subvolume tree. So after an unlink we must
1292 * run delayed items, so that future checks for a name
1293 * during log replay see that the name does not exists
1294 * anymore.
1295 */
1296 if (!ret)
1297 ret = btrfs_run_delayed_items(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001298 if (ret)
1299 goto out;
1300 goto again;
1301 }
1302
1303 kfree(name);
1304 ref_ptr += namelen;
1305 if (key->type == BTRFS_INODE_EXTREF_KEY)
1306 ref_ptr += sizeof(struct btrfs_inode_extref);
1307 else
1308 ref_ptr += sizeof(struct btrfs_inode_ref);
1309 }
1310 ret = 0;
1311 out:
1312 btrfs_release_path(path);
1313 return ret;
1314}
1315
1316static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1317 const u8 ref_type, const char *name,
1318 const int namelen)
1319{
1320 struct btrfs_key key;
1321 struct btrfs_path *path;
1322 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1323 int ret;
1324
1325 path = btrfs_alloc_path();
1326 if (!path)
1327 return -ENOMEM;
1328
1329 key.objectid = btrfs_ino(BTRFS_I(inode));
1330 key.type = ref_type;
1331 if (key.type == BTRFS_INODE_REF_KEY)
1332 key.offset = parent_id;
1333 else
1334 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1335
1336 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1337 if (ret < 0)
1338 goto out;
1339 if (ret > 0) {
1340 ret = 0;
1341 goto out;
1342 }
1343 if (key.type == BTRFS_INODE_EXTREF_KEY)
David Brazdil0f672f62019-12-10 10:32:29 +00001344 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1345 path->slots[0], parent_id, name, namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001346 else
David Brazdil0f672f62019-12-10 10:32:29 +00001347 ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1348 name, namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349
1350out:
1351 btrfs_free_path(path);
1352 return ret;
1353}
1354
David Brazdil0f672f62019-12-10 10:32:29 +00001355static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1356 struct inode *dir, struct inode *inode, const char *name,
1357 int namelen, u64 ref_index)
1358{
1359 struct btrfs_dir_item *dir_item;
1360 struct btrfs_key key;
1361 struct btrfs_path *path;
1362 struct inode *other_inode = NULL;
1363 int ret;
1364
1365 path = btrfs_alloc_path();
1366 if (!path)
1367 return -ENOMEM;
1368
1369 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1370 btrfs_ino(BTRFS_I(dir)),
1371 name, namelen, 0);
1372 if (!dir_item) {
1373 btrfs_release_path(path);
1374 goto add_link;
1375 } else if (IS_ERR(dir_item)) {
1376 ret = PTR_ERR(dir_item);
1377 goto out;
1378 }
1379
1380 /*
1381 * Our inode's dentry collides with the dentry of another inode which is
1382 * in the log but not yet processed since it has a higher inode number.
1383 * So delete that other dentry.
1384 */
1385 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1386 btrfs_release_path(path);
1387 other_inode = read_one_inode(root, key.objectid);
1388 if (!other_inode) {
1389 ret = -ENOENT;
1390 goto out;
1391 }
1392 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1393 name, namelen);
1394 if (ret)
1395 goto out;
1396 /*
1397 * If we dropped the link count to 0, bump it so that later the iput()
1398 * on the inode will not free it. We will fixup the link count later.
1399 */
1400 if (other_inode->i_nlink == 0)
1401 inc_nlink(other_inode);
1402
1403 ret = btrfs_run_delayed_items(trans);
1404 if (ret)
1405 goto out;
1406add_link:
1407 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1408 name, namelen, 0, ref_index);
1409out:
1410 iput(other_inode);
1411 btrfs_free_path(path);
1412
1413 return ret;
1414}
1415
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001416/*
1417 * replay one inode back reference item found in the log tree.
1418 * eb, slot and key refer to the buffer and key found in the log tree.
1419 * root is the destination we are replaying into, and path is for temp
1420 * use by this function. (it should be released on return).
1421 */
1422static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1423 struct btrfs_root *root,
1424 struct btrfs_root *log,
1425 struct btrfs_path *path,
1426 struct extent_buffer *eb, int slot,
1427 struct btrfs_key *key)
1428{
1429 struct inode *dir = NULL;
1430 struct inode *inode = NULL;
1431 unsigned long ref_ptr;
1432 unsigned long ref_end;
1433 char *name = NULL;
1434 int namelen;
1435 int ret;
1436 int search_done = 0;
1437 int log_ref_ver = 0;
1438 u64 parent_objectid;
1439 u64 inode_objectid;
1440 u64 ref_index = 0;
1441 int ref_struct_size;
1442
1443 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1444 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1445
1446 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1447 struct btrfs_inode_extref *r;
1448
1449 ref_struct_size = sizeof(struct btrfs_inode_extref);
1450 log_ref_ver = 1;
1451 r = (struct btrfs_inode_extref *)ref_ptr;
1452 parent_objectid = btrfs_inode_extref_parent(eb, r);
1453 } else {
1454 ref_struct_size = sizeof(struct btrfs_inode_ref);
1455 parent_objectid = key->offset;
1456 }
1457 inode_objectid = key->objectid;
1458
1459 /*
1460 * it is possible that we didn't log all the parent directories
1461 * for a given inode. If we don't find the dir, just don't
1462 * copy the back ref in. The link count fixup code will take
1463 * care of the rest
1464 */
1465 dir = read_one_inode(root, parent_objectid);
1466 if (!dir) {
1467 ret = -ENOENT;
1468 goto out;
1469 }
1470
1471 inode = read_one_inode(root, inode_objectid);
1472 if (!inode) {
1473 ret = -EIO;
1474 goto out;
1475 }
1476
1477 while (ref_ptr < ref_end) {
1478 if (log_ref_ver) {
1479 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1480 &ref_index, &parent_objectid);
1481 /*
1482 * parent object can change from one array
1483 * item to another.
1484 */
1485 if (!dir)
1486 dir = read_one_inode(root, parent_objectid);
1487 if (!dir) {
1488 ret = -ENOENT;
1489 goto out;
1490 }
1491 } else {
1492 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1493 &ref_index);
1494 }
1495 if (ret)
1496 goto out;
1497
Olivier Deprez157378f2022-04-04 15:47:50 +02001498 ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1499 btrfs_ino(BTRFS_I(inode)), ref_index,
1500 name, namelen);
1501 if (ret < 0) {
1502 goto out;
1503 } else if (ret == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504 /*
1505 * look for a conflicting back reference in the
1506 * metadata. if we find one we have to unlink that name
1507 * of the file before we add our new link. Later on, we
1508 * overwrite any existing back reference, and we don't
1509 * want to create dangling pointers in the directory.
1510 */
1511
1512 if (!search_done) {
1513 ret = __add_inode_ref(trans, root, path, log,
1514 BTRFS_I(dir),
1515 BTRFS_I(inode),
1516 inode_objectid,
1517 parent_objectid,
1518 ref_index, name, namelen,
1519 &search_done);
1520 if (ret) {
1521 if (ret == 1)
1522 ret = 0;
1523 goto out;
1524 }
1525 }
1526
1527 /*
1528 * If a reference item already exists for this inode
1529 * with the same parent and name, but different index,
1530 * drop it and the corresponding directory index entries
1531 * from the parent before adding the new reference item
1532 * and dir index entries, otherwise we would fail with
1533 * -EEXIST returned from btrfs_add_link() below.
1534 */
1535 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1536 name, namelen);
1537 if (ret > 0) {
1538 ret = btrfs_unlink_inode(trans, root,
1539 BTRFS_I(dir),
1540 BTRFS_I(inode),
1541 name, namelen);
1542 /*
1543 * If we dropped the link count to 0, bump it so
1544 * that later the iput() on the inode will not
1545 * free it. We will fixup the link count later.
1546 */
1547 if (!ret && inode->i_nlink == 0)
1548 inc_nlink(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02001549 /*
1550 * Whenever we need to check if a name exists or
1551 * not, we check the subvolume tree. So after an
1552 * unlink we must run delayed items, so that future
1553 * checks for a name during log replay see that the
1554 * name does not exists anymore.
1555 */
1556 if (!ret)
1557 ret = btrfs_run_delayed_items(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001558 }
1559 if (ret < 0)
1560 goto out;
1561
1562 /* insert our name */
David Brazdil0f672f62019-12-10 10:32:29 +00001563 ret = add_link(trans, root, dir, inode, name, namelen,
1564 ref_index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001565 if (ret)
1566 goto out;
1567
1568 btrfs_update_inode(trans, root, inode);
1569 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001570 /* Else, ret == 1, we already have a perfect match, we're done. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001571
1572 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1573 kfree(name);
1574 name = NULL;
1575 if (log_ref_ver) {
1576 iput(dir);
1577 dir = NULL;
1578 }
1579 }
1580
1581 /*
1582 * Before we overwrite the inode reference item in the subvolume tree
1583 * with the item from the log tree, we must unlink all names from the
1584 * parent directory that are in the subvolume's tree inode reference
1585 * item, otherwise we end up with an inconsistent subvolume tree where
1586 * dir index entries exist for a name but there is no inode reference
1587 * item with the same name.
1588 */
1589 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1590 key);
1591 if (ret)
1592 goto out;
1593
1594 /* finally write the back reference in the inode */
1595 ret = overwrite_item(trans, root, path, eb, slot, key);
1596out:
1597 btrfs_release_path(path);
1598 kfree(name);
1599 iput(dir);
1600 iput(inode);
1601 return ret;
1602}
1603
1604static int insert_orphan_item(struct btrfs_trans_handle *trans,
1605 struct btrfs_root *root, u64 ino)
1606{
1607 int ret;
1608
1609 ret = btrfs_insert_orphan_item(trans, root, ino);
1610 if (ret == -EEXIST)
1611 ret = 0;
1612
1613 return ret;
1614}
1615
1616static int count_inode_extrefs(struct btrfs_root *root,
1617 struct btrfs_inode *inode, struct btrfs_path *path)
1618{
1619 int ret = 0;
1620 int name_len;
1621 unsigned int nlink = 0;
1622 u32 item_size;
1623 u32 cur_offset = 0;
1624 u64 inode_objectid = btrfs_ino(inode);
1625 u64 offset = 0;
1626 unsigned long ptr;
1627 struct btrfs_inode_extref *extref;
1628 struct extent_buffer *leaf;
1629
1630 while (1) {
1631 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1632 &extref, &offset);
1633 if (ret)
1634 break;
1635
1636 leaf = path->nodes[0];
1637 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1638 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1639 cur_offset = 0;
1640
1641 while (cur_offset < item_size) {
1642 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1643 name_len = btrfs_inode_extref_name_len(leaf, extref);
1644
1645 nlink++;
1646
1647 cur_offset += name_len + sizeof(*extref);
1648 }
1649
1650 offset++;
1651 btrfs_release_path(path);
1652 }
1653 btrfs_release_path(path);
1654
1655 if (ret < 0 && ret != -ENOENT)
1656 return ret;
1657 return nlink;
1658}
1659
1660static int count_inode_refs(struct btrfs_root *root,
1661 struct btrfs_inode *inode, struct btrfs_path *path)
1662{
1663 int ret;
1664 struct btrfs_key key;
1665 unsigned int nlink = 0;
1666 unsigned long ptr;
1667 unsigned long ptr_end;
1668 int name_len;
1669 u64 ino = btrfs_ino(inode);
1670
1671 key.objectid = ino;
1672 key.type = BTRFS_INODE_REF_KEY;
1673 key.offset = (u64)-1;
1674
1675 while (1) {
1676 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1677 if (ret < 0)
1678 break;
1679 if (ret > 0) {
1680 if (path->slots[0] == 0)
1681 break;
1682 path->slots[0]--;
1683 }
1684process_slot:
1685 btrfs_item_key_to_cpu(path->nodes[0], &key,
1686 path->slots[0]);
1687 if (key.objectid != ino ||
1688 key.type != BTRFS_INODE_REF_KEY)
1689 break;
1690 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1691 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1692 path->slots[0]);
1693 while (ptr < ptr_end) {
1694 struct btrfs_inode_ref *ref;
1695
1696 ref = (struct btrfs_inode_ref *)ptr;
1697 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1698 ref);
1699 ptr = (unsigned long)(ref + 1) + name_len;
1700 nlink++;
1701 }
1702
1703 if (key.offset == 0)
1704 break;
1705 if (path->slots[0] > 0) {
1706 path->slots[0]--;
1707 goto process_slot;
1708 }
1709 key.offset--;
1710 btrfs_release_path(path);
1711 }
1712 btrfs_release_path(path);
1713
1714 return nlink;
1715}
1716
1717/*
1718 * There are a few corners where the link count of the file can't
1719 * be properly maintained during replay. So, instead of adding
1720 * lots of complexity to the log code, we just scan the backrefs
1721 * for any file that has been through replay.
1722 *
1723 * The scan will update the link count on the inode to reflect the
1724 * number of back refs found. If it goes down to zero, the iput
1725 * will free the inode.
1726 */
1727static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1728 struct btrfs_root *root,
1729 struct inode *inode)
1730{
1731 struct btrfs_path *path;
1732 int ret;
1733 u64 nlink = 0;
1734 u64 ino = btrfs_ino(BTRFS_I(inode));
1735
1736 path = btrfs_alloc_path();
1737 if (!path)
1738 return -ENOMEM;
1739
1740 ret = count_inode_refs(root, BTRFS_I(inode), path);
1741 if (ret < 0)
1742 goto out;
1743
1744 nlink = ret;
1745
1746 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1747 if (ret < 0)
1748 goto out;
1749
1750 nlink += ret;
1751
1752 ret = 0;
1753
1754 if (nlink != inode->i_nlink) {
1755 set_nlink(inode, nlink);
1756 btrfs_update_inode(trans, root, inode);
1757 }
1758 BTRFS_I(inode)->index_cnt = (u64)-1;
1759
1760 if (inode->i_nlink == 0) {
1761 if (S_ISDIR(inode->i_mode)) {
1762 ret = replay_dir_deletes(trans, root, NULL, path,
1763 ino, 1);
1764 if (ret)
1765 goto out;
1766 }
1767 ret = insert_orphan_item(trans, root, ino);
1768 }
1769
1770out:
1771 btrfs_free_path(path);
1772 return ret;
1773}
1774
1775static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1776 struct btrfs_root *root,
1777 struct btrfs_path *path)
1778{
1779 int ret;
1780 struct btrfs_key key;
1781 struct inode *inode;
1782
1783 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1784 key.type = BTRFS_ORPHAN_ITEM_KEY;
1785 key.offset = (u64)-1;
1786 while (1) {
1787 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1788 if (ret < 0)
1789 break;
1790
1791 if (ret == 1) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001792 ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001793 if (path->slots[0] == 0)
1794 break;
1795 path->slots[0]--;
1796 }
1797
1798 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1799 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1800 key.type != BTRFS_ORPHAN_ITEM_KEY)
1801 break;
1802
1803 ret = btrfs_del_item(trans, root, path);
1804 if (ret)
Olivier Deprez0e641232021-09-23 10:07:05 +02001805 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001806
1807 btrfs_release_path(path);
1808 inode = read_one_inode(root, key.offset);
Olivier Deprez0e641232021-09-23 10:07:05 +02001809 if (!inode) {
1810 ret = -EIO;
1811 break;
1812 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813
1814 ret = fixup_inode_link_count(trans, root, inode);
1815 iput(inode);
1816 if (ret)
Olivier Deprez0e641232021-09-23 10:07:05 +02001817 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001818
1819 /*
1820 * fixup on a directory may create new entries,
1821 * make sure we always look for the highset possible
1822 * offset
1823 */
1824 key.offset = (u64)-1;
1825 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001826 btrfs_release_path(path);
1827 return ret;
1828}
1829
1830
1831/*
1832 * record a given inode in the fixup dir so we can check its link
1833 * count when replay is done. The link count is incremented here
1834 * so the inode won't go away until we check it
1835 */
1836static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1837 struct btrfs_root *root,
1838 struct btrfs_path *path,
1839 u64 objectid)
1840{
1841 struct btrfs_key key;
1842 int ret = 0;
1843 struct inode *inode;
1844
1845 inode = read_one_inode(root, objectid);
1846 if (!inode)
1847 return -EIO;
1848
1849 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1850 key.type = BTRFS_ORPHAN_ITEM_KEY;
1851 key.offset = objectid;
1852
1853 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1854
1855 btrfs_release_path(path);
1856 if (ret == 0) {
1857 if (!inode->i_nlink)
1858 set_nlink(inode, 1);
1859 else
1860 inc_nlink(inode);
1861 ret = btrfs_update_inode(trans, root, inode);
1862 } else if (ret == -EEXIST) {
1863 ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 }
1865 iput(inode);
1866
1867 return ret;
1868}
1869
1870/*
1871 * when replaying the log for a directory, we only insert names
1872 * for inodes that actually exist. This means an fsync on a directory
1873 * does not implicitly fsync all the new files in it
1874 */
1875static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *root,
1877 u64 dirid, u64 index,
1878 char *name, int name_len,
1879 struct btrfs_key *location)
1880{
1881 struct inode *inode;
1882 struct inode *dir;
1883 int ret;
1884
1885 inode = read_one_inode(root, location->objectid);
1886 if (!inode)
1887 return -ENOENT;
1888
1889 dir = read_one_inode(root, dirid);
1890 if (!dir) {
1891 iput(inode);
1892 return -EIO;
1893 }
1894
1895 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1896 name_len, 1, index);
1897
1898 /* FIXME, put inode into FIXUP list */
1899
1900 iput(inode);
1901 iput(dir);
1902 return ret;
1903}
1904
1905/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001906 * take a single entry in a log directory item and replay it into
1907 * the subvolume.
1908 *
1909 * if a conflicting item exists in the subdirectory already,
1910 * the inode it points to is unlinked and put into the link count
1911 * fix up tree.
1912 *
1913 * If a name from the log points to a file or directory that does
1914 * not exist in the FS, it is skipped. fsyncs on directories
1915 * do not force down inodes inside that directory, just changes to the
1916 * names or unlinks in a directory.
1917 *
1918 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1919 * non-existing inode) and 1 if the name was replayed.
1920 */
1921static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1922 struct btrfs_root *root,
1923 struct btrfs_path *path,
1924 struct extent_buffer *eb,
1925 struct btrfs_dir_item *di,
1926 struct btrfs_key *key)
1927{
1928 char *name;
1929 int name_len;
1930 struct btrfs_dir_item *dst_di;
1931 struct btrfs_key found_key;
1932 struct btrfs_key log_key;
1933 struct inode *dir;
1934 u8 log_type;
Olivier Deprez157378f2022-04-04 15:47:50 +02001935 bool exists;
1936 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001937 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1938 bool name_added = false;
1939
1940 dir = read_one_inode(root, key->objectid);
1941 if (!dir)
1942 return -EIO;
1943
1944 name_len = btrfs_dir_name_len(eb, di);
1945 name = kmalloc(name_len, GFP_NOFS);
1946 if (!name) {
1947 ret = -ENOMEM;
1948 goto out;
1949 }
1950
1951 log_type = btrfs_dir_type(eb, di);
1952 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1953 name_len);
1954
1955 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Olivier Deprez157378f2022-04-04 15:47:50 +02001956 ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957 btrfs_release_path(path);
Olivier Deprez157378f2022-04-04 15:47:50 +02001958 if (ret < 0)
1959 goto out;
1960 exists = (ret == 0);
1961 ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001962
1963 if (key->type == BTRFS_DIR_ITEM_KEY) {
1964 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1965 name, name_len, 1);
1966 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1967 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1968 key->objectid,
1969 key->offset, name,
1970 name_len, 1);
1971 } else {
1972 /* Corruption */
1973 ret = -EINVAL;
1974 goto out;
1975 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001976
1977 if (dst_di == ERR_PTR(-ENOENT))
1978 dst_di = NULL;
1979
1980 if (IS_ERR(dst_di)) {
1981 ret = PTR_ERR(dst_di);
1982 goto out;
1983 } else if (!dst_di) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001984 /* we need a sequence number to insert, so we only
1985 * do inserts for the BTRFS_DIR_INDEX_KEY types
1986 */
1987 if (key->type != BTRFS_DIR_INDEX_KEY)
1988 goto out;
1989 goto insert;
1990 }
1991
1992 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1993 /* the existing item matches the logged item */
1994 if (found_key.objectid == log_key.objectid &&
1995 found_key.type == log_key.type &&
1996 found_key.offset == log_key.offset &&
1997 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1998 update_size = false;
1999 goto out;
2000 }
2001
2002 /*
2003 * don't drop the conflicting directory entry if the inode
2004 * for the new entry doesn't exist
2005 */
2006 if (!exists)
2007 goto out;
2008
2009 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
2010 if (ret)
2011 goto out;
2012
2013 if (key->type == BTRFS_DIR_INDEX_KEY)
2014 goto insert;
2015out:
2016 btrfs_release_path(path);
2017 if (!ret && update_size) {
2018 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
2019 ret = btrfs_update_inode(trans, root, dir);
2020 }
2021 kfree(name);
2022 iput(dir);
2023 if (!ret && name_added)
2024 ret = 1;
2025 return ret;
2026
2027insert:
Olivier Deprez157378f2022-04-04 15:47:50 +02002028 /*
2029 * Check if the inode reference exists in the log for the given name,
2030 * inode and parent inode
2031 */
2032 found_key.objectid = log_key.objectid;
2033 found_key.type = BTRFS_INODE_REF_KEY;
2034 found_key.offset = key->objectid;
2035 ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
2036 if (ret < 0) {
2037 goto out;
2038 } else if (ret) {
2039 /* The dentry will be added later. */
2040 ret = 0;
2041 update_size = false;
2042 goto out;
2043 }
2044
2045 found_key.objectid = log_key.objectid;
2046 found_key.type = BTRFS_INODE_EXTREF_KEY;
2047 found_key.offset = key->objectid;
2048 ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
2049 name_len);
2050 if (ret < 0) {
2051 goto out;
2052 } else if (ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002053 /* The dentry will be added later. */
2054 ret = 0;
2055 update_size = false;
2056 goto out;
2057 }
2058 btrfs_release_path(path);
2059 ret = insert_one_name(trans, root, key->objectid, key->offset,
2060 name, name_len, &log_key);
2061 if (ret && ret != -ENOENT && ret != -EEXIST)
2062 goto out;
2063 if (!ret)
2064 name_added = true;
2065 update_size = false;
2066 ret = 0;
2067 goto out;
2068}
2069
2070/*
2071 * find all the names in a directory item and reconcile them into
2072 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2073 * one name in a directory item, but the same code gets used for
2074 * both directory index types
2075 */
2076static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2077 struct btrfs_root *root,
2078 struct btrfs_path *path,
2079 struct extent_buffer *eb, int slot,
2080 struct btrfs_key *key)
2081{
2082 int ret = 0;
2083 u32 item_size = btrfs_item_size_nr(eb, slot);
2084 struct btrfs_dir_item *di;
2085 int name_len;
2086 unsigned long ptr;
2087 unsigned long ptr_end;
2088 struct btrfs_path *fixup_path = NULL;
2089
2090 ptr = btrfs_item_ptr_offset(eb, slot);
2091 ptr_end = ptr + item_size;
2092 while (ptr < ptr_end) {
2093 di = (struct btrfs_dir_item *)ptr;
2094 name_len = btrfs_dir_name_len(eb, di);
2095 ret = replay_one_name(trans, root, path, eb, di, key);
2096 if (ret < 0)
2097 break;
2098 ptr = (unsigned long)(di + 1);
2099 ptr += name_len;
2100
2101 /*
2102 * If this entry refers to a non-directory (directories can not
2103 * have a link count > 1) and it was added in the transaction
2104 * that was not committed, make sure we fixup the link count of
2105 * the inode it the entry points to. Otherwise something like
2106 * the following would result in a directory pointing to an
2107 * inode with a wrong link that does not account for this dir
2108 * entry:
2109 *
2110 * mkdir testdir
2111 * touch testdir/foo
2112 * touch testdir/bar
2113 * sync
2114 *
2115 * ln testdir/bar testdir/bar_link
2116 * ln testdir/foo testdir/foo_link
2117 * xfs_io -c "fsync" testdir/bar
2118 *
2119 * <power failure>
2120 *
2121 * mount fs, log replay happens
2122 *
2123 * File foo would remain with a link count of 1 when it has two
2124 * entries pointing to it in the directory testdir. This would
2125 * make it impossible to ever delete the parent directory has
2126 * it would result in stale dentries that can never be deleted.
2127 */
2128 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2129 struct btrfs_key di_key;
2130
2131 if (!fixup_path) {
2132 fixup_path = btrfs_alloc_path();
2133 if (!fixup_path) {
2134 ret = -ENOMEM;
2135 break;
2136 }
2137 }
2138
2139 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2140 ret = link_to_fixup_dir(trans, root, fixup_path,
2141 di_key.objectid);
2142 if (ret)
2143 break;
2144 }
2145 ret = 0;
2146 }
2147 btrfs_free_path(fixup_path);
2148 return ret;
2149}
2150
2151/*
2152 * directory replay has two parts. There are the standard directory
2153 * items in the log copied from the subvolume, and range items
2154 * created in the log while the subvolume was logged.
2155 *
2156 * The range items tell us which parts of the key space the log
2157 * is authoritative for. During replay, if a key in the subvolume
2158 * directory is in a logged range item, but not actually in the log
2159 * that means it was deleted from the directory before the fsync
2160 * and should be removed.
2161 */
2162static noinline int find_dir_range(struct btrfs_root *root,
2163 struct btrfs_path *path,
2164 u64 dirid, int key_type,
2165 u64 *start_ret, u64 *end_ret)
2166{
2167 struct btrfs_key key;
2168 u64 found_end;
2169 struct btrfs_dir_log_item *item;
2170 int ret;
2171 int nritems;
2172
2173 if (*start_ret == (u64)-1)
2174 return 1;
2175
2176 key.objectid = dirid;
2177 key.type = key_type;
2178 key.offset = *start_ret;
2179
2180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2181 if (ret < 0)
2182 goto out;
2183 if (ret > 0) {
2184 if (path->slots[0] == 0)
2185 goto out;
2186 path->slots[0]--;
2187 }
2188 if (ret != 0)
2189 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2190
2191 if (key.type != key_type || key.objectid != dirid) {
2192 ret = 1;
2193 goto next;
2194 }
2195 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2196 struct btrfs_dir_log_item);
2197 found_end = btrfs_dir_log_end(path->nodes[0], item);
2198
2199 if (*start_ret >= key.offset && *start_ret <= found_end) {
2200 ret = 0;
2201 *start_ret = key.offset;
2202 *end_ret = found_end;
2203 goto out;
2204 }
2205 ret = 1;
2206next:
2207 /* check the next slot in the tree to see if it is a valid item */
2208 nritems = btrfs_header_nritems(path->nodes[0]);
2209 path->slots[0]++;
2210 if (path->slots[0] >= nritems) {
2211 ret = btrfs_next_leaf(root, path);
2212 if (ret)
2213 goto out;
2214 }
2215
2216 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2217
2218 if (key.type != key_type || key.objectid != dirid) {
2219 ret = 1;
2220 goto out;
2221 }
2222 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2223 struct btrfs_dir_log_item);
2224 found_end = btrfs_dir_log_end(path->nodes[0], item);
2225 *start_ret = key.offset;
2226 *end_ret = found_end;
2227 ret = 0;
2228out:
2229 btrfs_release_path(path);
2230 return ret;
2231}
2232
2233/*
2234 * this looks for a given directory item in the log. If the directory
2235 * item is not in the log, the item is removed and the inode it points
2236 * to is unlinked
2237 */
2238static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2239 struct btrfs_root *root,
2240 struct btrfs_root *log,
2241 struct btrfs_path *path,
2242 struct btrfs_path *log_path,
2243 struct inode *dir,
2244 struct btrfs_key *dir_key)
2245{
2246 int ret;
2247 struct extent_buffer *eb;
2248 int slot;
2249 u32 item_size;
2250 struct btrfs_dir_item *di;
2251 struct btrfs_dir_item *log_di;
2252 int name_len;
2253 unsigned long ptr;
2254 unsigned long ptr_end;
2255 char *name;
2256 struct inode *inode;
2257 struct btrfs_key location;
2258
2259again:
2260 eb = path->nodes[0];
2261 slot = path->slots[0];
2262 item_size = btrfs_item_size_nr(eb, slot);
2263 ptr = btrfs_item_ptr_offset(eb, slot);
2264 ptr_end = ptr + item_size;
2265 while (ptr < ptr_end) {
2266 di = (struct btrfs_dir_item *)ptr;
2267 name_len = btrfs_dir_name_len(eb, di);
2268 name = kmalloc(name_len, GFP_NOFS);
2269 if (!name) {
2270 ret = -ENOMEM;
2271 goto out;
2272 }
2273 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2274 name_len);
2275 log_di = NULL;
2276 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2277 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2278 dir_key->objectid,
2279 name, name_len, 0);
2280 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2281 log_di = btrfs_lookup_dir_index_item(trans, log,
2282 log_path,
2283 dir_key->objectid,
2284 dir_key->offset,
2285 name, name_len, 0);
2286 }
2287 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
2288 btrfs_dir_item_key_to_cpu(eb, di, &location);
2289 btrfs_release_path(path);
2290 btrfs_release_path(log_path);
2291 inode = read_one_inode(root, location.objectid);
2292 if (!inode) {
2293 kfree(name);
2294 return -EIO;
2295 }
2296
2297 ret = link_to_fixup_dir(trans, root,
2298 path, location.objectid);
2299 if (ret) {
2300 kfree(name);
2301 iput(inode);
2302 goto out;
2303 }
2304
2305 inc_nlink(inode);
2306 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2307 BTRFS_I(inode), name, name_len);
2308 if (!ret)
2309 ret = btrfs_run_delayed_items(trans);
2310 kfree(name);
2311 iput(inode);
2312 if (ret)
2313 goto out;
2314
2315 /* there might still be more names under this key
2316 * check and repeat if required
2317 */
2318 ret = btrfs_search_slot(NULL, root, dir_key, path,
2319 0, 0);
2320 if (ret == 0)
2321 goto again;
2322 ret = 0;
2323 goto out;
2324 } else if (IS_ERR(log_di)) {
2325 kfree(name);
2326 return PTR_ERR(log_di);
2327 }
2328 btrfs_release_path(log_path);
2329 kfree(name);
2330
2331 ptr = (unsigned long)(di + 1);
2332 ptr += name_len;
2333 }
2334 ret = 0;
2335out:
2336 btrfs_release_path(path);
2337 btrfs_release_path(log_path);
2338 return ret;
2339}
2340
2341static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2342 struct btrfs_root *root,
2343 struct btrfs_root *log,
2344 struct btrfs_path *path,
2345 const u64 ino)
2346{
2347 struct btrfs_key search_key;
2348 struct btrfs_path *log_path;
2349 int i;
2350 int nritems;
2351 int ret;
2352
2353 log_path = btrfs_alloc_path();
2354 if (!log_path)
2355 return -ENOMEM;
2356
2357 search_key.objectid = ino;
2358 search_key.type = BTRFS_XATTR_ITEM_KEY;
2359 search_key.offset = 0;
2360again:
2361 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2362 if (ret < 0)
2363 goto out;
2364process_leaf:
2365 nritems = btrfs_header_nritems(path->nodes[0]);
2366 for (i = path->slots[0]; i < nritems; i++) {
2367 struct btrfs_key key;
2368 struct btrfs_dir_item *di;
2369 struct btrfs_dir_item *log_di;
2370 u32 total_size;
2371 u32 cur;
2372
2373 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2374 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2375 ret = 0;
2376 goto out;
2377 }
2378
2379 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2380 total_size = btrfs_item_size_nr(path->nodes[0], i);
2381 cur = 0;
2382 while (cur < total_size) {
2383 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2384 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2385 u32 this_len = sizeof(*di) + name_len + data_len;
2386 char *name;
2387
2388 name = kmalloc(name_len, GFP_NOFS);
2389 if (!name) {
2390 ret = -ENOMEM;
2391 goto out;
2392 }
2393 read_extent_buffer(path->nodes[0], name,
2394 (unsigned long)(di + 1), name_len);
2395
2396 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2397 name, name_len, 0);
2398 btrfs_release_path(log_path);
2399 if (!log_di) {
2400 /* Doesn't exist in log tree, so delete it. */
2401 btrfs_release_path(path);
2402 di = btrfs_lookup_xattr(trans, root, path, ino,
2403 name, name_len, -1);
2404 kfree(name);
2405 if (IS_ERR(di)) {
2406 ret = PTR_ERR(di);
2407 goto out;
2408 }
2409 ASSERT(di);
2410 ret = btrfs_delete_one_dir_name(trans, root,
2411 path, di);
2412 if (ret)
2413 goto out;
2414 btrfs_release_path(path);
2415 search_key = key;
2416 goto again;
2417 }
2418 kfree(name);
2419 if (IS_ERR(log_di)) {
2420 ret = PTR_ERR(log_di);
2421 goto out;
2422 }
2423 cur += this_len;
2424 di = (struct btrfs_dir_item *)((char *)di + this_len);
2425 }
2426 }
2427 ret = btrfs_next_leaf(root, path);
2428 if (ret > 0)
2429 ret = 0;
2430 else if (ret == 0)
2431 goto process_leaf;
2432out:
2433 btrfs_free_path(log_path);
2434 btrfs_release_path(path);
2435 return ret;
2436}
2437
2438
2439/*
2440 * deletion replay happens before we copy any new directory items
2441 * out of the log or out of backreferences from inodes. It
2442 * scans the log to find ranges of keys that log is authoritative for,
2443 * and then scans the directory to find items in those ranges that are
2444 * not present in the log.
2445 *
2446 * Anything we don't find in the log is unlinked and removed from the
2447 * directory.
2448 */
2449static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2450 struct btrfs_root *root,
2451 struct btrfs_root *log,
2452 struct btrfs_path *path,
2453 u64 dirid, int del_all)
2454{
2455 u64 range_start;
2456 u64 range_end;
2457 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2458 int ret = 0;
2459 struct btrfs_key dir_key;
2460 struct btrfs_key found_key;
2461 struct btrfs_path *log_path;
2462 struct inode *dir;
2463
2464 dir_key.objectid = dirid;
2465 dir_key.type = BTRFS_DIR_ITEM_KEY;
2466 log_path = btrfs_alloc_path();
2467 if (!log_path)
2468 return -ENOMEM;
2469
2470 dir = read_one_inode(root, dirid);
2471 /* it isn't an error if the inode isn't there, that can happen
2472 * because we replay the deletes before we copy in the inode item
2473 * from the log
2474 */
2475 if (!dir) {
2476 btrfs_free_path(log_path);
2477 return 0;
2478 }
2479again:
2480 range_start = 0;
2481 range_end = 0;
2482 while (1) {
2483 if (del_all)
2484 range_end = (u64)-1;
2485 else {
2486 ret = find_dir_range(log, path, dirid, key_type,
2487 &range_start, &range_end);
Olivier Deprez157378f2022-04-04 15:47:50 +02002488 if (ret < 0)
2489 goto out;
2490 else if (ret > 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002491 break;
2492 }
2493
2494 dir_key.offset = range_start;
2495 while (1) {
2496 int nritems;
2497 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2498 0, 0);
2499 if (ret < 0)
2500 goto out;
2501
2502 nritems = btrfs_header_nritems(path->nodes[0]);
2503 if (path->slots[0] >= nritems) {
2504 ret = btrfs_next_leaf(root, path);
2505 if (ret == 1)
2506 break;
2507 else if (ret < 0)
2508 goto out;
2509 }
2510 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2511 path->slots[0]);
2512 if (found_key.objectid != dirid ||
2513 found_key.type != dir_key.type)
2514 goto next_type;
2515
2516 if (found_key.offset > range_end)
2517 break;
2518
2519 ret = check_item_in_log(trans, root, log, path,
2520 log_path, dir,
2521 &found_key);
2522 if (ret)
2523 goto out;
2524 if (found_key.offset == (u64)-1)
2525 break;
2526 dir_key.offset = found_key.offset + 1;
2527 }
2528 btrfs_release_path(path);
2529 if (range_end == (u64)-1)
2530 break;
2531 range_start = range_end + 1;
2532 }
2533
2534next_type:
2535 ret = 0;
2536 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2537 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2538 dir_key.type = BTRFS_DIR_INDEX_KEY;
2539 btrfs_release_path(path);
2540 goto again;
2541 }
2542out:
2543 btrfs_release_path(path);
2544 btrfs_free_path(log_path);
2545 iput(dir);
2546 return ret;
2547}
2548
2549/*
2550 * the process_func used to replay items from the log tree. This
2551 * gets called in two different stages. The first stage just looks
2552 * for inodes and makes sure they are all copied into the subvolume.
2553 *
2554 * The second stage copies all the other item types from the log into
2555 * the subvolume. The two stage approach is slower, but gets rid of
2556 * lots of complexity around inodes referencing other inodes that exist
2557 * only in the log (references come from either directory items or inode
2558 * back refs).
2559 */
2560static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2561 struct walk_control *wc, u64 gen, int level)
2562{
2563 int nritems;
2564 struct btrfs_path *path;
2565 struct btrfs_root *root = wc->replay_dest;
2566 struct btrfs_key key;
2567 int i;
2568 int ret;
2569
2570 ret = btrfs_read_buffer(eb, gen, level, NULL);
2571 if (ret)
2572 return ret;
2573
2574 level = btrfs_header_level(eb);
2575
2576 if (level != 0)
2577 return 0;
2578
2579 path = btrfs_alloc_path();
2580 if (!path)
2581 return -ENOMEM;
2582
2583 nritems = btrfs_header_nritems(eb);
2584 for (i = 0; i < nritems; i++) {
2585 btrfs_item_key_to_cpu(eb, &key, i);
2586
2587 /* inode keys are done during the first stage */
2588 if (key.type == BTRFS_INODE_ITEM_KEY &&
2589 wc->stage == LOG_WALK_REPLAY_INODES) {
2590 struct btrfs_inode_item *inode_item;
2591 u32 mode;
2592
2593 inode_item = btrfs_item_ptr(eb, i,
2594 struct btrfs_inode_item);
2595 /*
2596 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2597 * and never got linked before the fsync, skip it, as
2598 * replaying it is pointless since it would be deleted
2599 * later. We skip logging tmpfiles, but it's always
2600 * possible we are replaying a log created with a kernel
2601 * that used to log tmpfiles.
2602 */
2603 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2604 wc->ignore_cur_inode = true;
2605 continue;
2606 } else {
2607 wc->ignore_cur_inode = false;
2608 }
2609 ret = replay_xattr_deletes(wc->trans, root, log,
2610 path, key.objectid);
2611 if (ret)
2612 break;
2613 mode = btrfs_inode_mode(eb, inode_item);
2614 if (S_ISDIR(mode)) {
2615 ret = replay_dir_deletes(wc->trans,
2616 root, log, path, key.objectid, 0);
2617 if (ret)
2618 break;
2619 }
2620 ret = overwrite_item(wc->trans, root, path,
2621 eb, i, &key);
2622 if (ret)
2623 break;
2624
2625 /*
2626 * Before replaying extents, truncate the inode to its
2627 * size. We need to do it now and not after log replay
2628 * because before an fsync we can have prealloc extents
2629 * added beyond the inode's i_size. If we did it after,
2630 * through orphan cleanup for example, we would drop
2631 * those prealloc extents just after replaying them.
2632 */
2633 if (S_ISREG(mode)) {
2634 struct inode *inode;
2635 u64 from;
2636
2637 inode = read_one_inode(root, key.objectid);
2638 if (!inode) {
2639 ret = -EIO;
2640 break;
2641 }
2642 from = ALIGN(i_size_read(inode),
2643 root->fs_info->sectorsize);
2644 ret = btrfs_drop_extents(wc->trans, root, inode,
2645 from, (u64)-1, 1);
2646 if (!ret) {
2647 /* Update the inode's nbytes. */
2648 ret = btrfs_update_inode(wc->trans,
2649 root, inode);
2650 }
2651 iput(inode);
2652 if (ret)
2653 break;
2654 }
2655
2656 ret = link_to_fixup_dir(wc->trans, root,
2657 path, key.objectid);
2658 if (ret)
2659 break;
2660 }
2661
2662 if (wc->ignore_cur_inode)
2663 continue;
2664
2665 if (key.type == BTRFS_DIR_INDEX_KEY &&
2666 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2667 ret = replay_one_dir_item(wc->trans, root, path,
2668 eb, i, &key);
2669 if (ret)
2670 break;
2671 }
2672
2673 if (wc->stage < LOG_WALK_REPLAY_ALL)
2674 continue;
2675
2676 /* these keys are simply copied */
2677 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2678 ret = overwrite_item(wc->trans, root, path,
2679 eb, i, &key);
2680 if (ret)
2681 break;
2682 } else if (key.type == BTRFS_INODE_REF_KEY ||
2683 key.type == BTRFS_INODE_EXTREF_KEY) {
2684 ret = add_inode_ref(wc->trans, root, log, path,
2685 eb, i, &key);
2686 if (ret && ret != -ENOENT)
2687 break;
2688 ret = 0;
2689 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2690 ret = replay_one_extent(wc->trans, root, path,
2691 eb, i, &key);
2692 if (ret)
2693 break;
2694 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2695 ret = replay_one_dir_item(wc->trans, root, path,
2696 eb, i, &key);
2697 if (ret)
2698 break;
2699 }
2700 }
2701 btrfs_free_path(path);
2702 return ret;
2703}
2704
Olivier Deprez157378f2022-04-04 15:47:50 +02002705/*
2706 * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2707 */
2708static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2709{
2710 struct btrfs_block_group *cache;
2711
2712 cache = btrfs_lookup_block_group(fs_info, start);
2713 if (!cache) {
2714 btrfs_err(fs_info, "unable to find block group for %llu", start);
2715 return;
2716 }
2717
2718 spin_lock(&cache->space_info->lock);
2719 spin_lock(&cache->lock);
2720 cache->reserved -= fs_info->nodesize;
2721 cache->space_info->bytes_reserved -= fs_info->nodesize;
2722 spin_unlock(&cache->lock);
2723 spin_unlock(&cache->space_info->lock);
2724
2725 btrfs_put_block_group(cache);
2726}
2727
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002728static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2729 struct btrfs_root *root,
2730 struct btrfs_path *path, int *level,
2731 struct walk_control *wc)
2732{
2733 struct btrfs_fs_info *fs_info = root->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002734 u64 bytenr;
2735 u64 ptr_gen;
2736 struct extent_buffer *next;
2737 struct extent_buffer *cur;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002738 u32 blocksize;
2739 int ret = 0;
2740
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002741 while (*level > 0) {
2742 struct btrfs_key first_key;
2743
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002744 cur = path->nodes[*level];
2745
2746 WARN_ON(btrfs_header_level(cur) != *level);
2747
2748 if (path->slots[*level] >=
2749 btrfs_header_nritems(cur))
2750 break;
2751
2752 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2753 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2754 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
2755 blocksize = fs_info->nodesize;
2756
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002757 next = btrfs_find_create_tree_block(fs_info, bytenr);
2758 if (IS_ERR(next))
2759 return PTR_ERR(next);
2760
2761 if (*level == 1) {
2762 ret = wc->process_func(root, next, wc, ptr_gen,
2763 *level - 1);
2764 if (ret) {
2765 free_extent_buffer(next);
2766 return ret;
2767 }
2768
2769 path->slots[*level]++;
2770 if (wc->free) {
2771 ret = btrfs_read_buffer(next, ptr_gen,
2772 *level - 1, &first_key);
2773 if (ret) {
2774 free_extent_buffer(next);
2775 return ret;
2776 }
2777
2778 if (trans) {
2779 btrfs_tree_lock(next);
David Brazdil0f672f62019-12-10 10:32:29 +00002780 btrfs_set_lock_blocking_write(next);
2781 btrfs_clean_tree_block(next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002782 btrfs_wait_tree_block_writeback(next);
2783 btrfs_tree_unlock(next);
Olivier Deprez157378f2022-04-04 15:47:50 +02002784 ret = btrfs_pin_reserved_extent(trans,
2785 bytenr, blocksize);
2786 if (ret) {
2787 free_extent_buffer(next);
2788 return ret;
2789 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002790 } else {
2791 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2792 clear_extent_buffer_dirty(next);
Olivier Deprez157378f2022-04-04 15:47:50 +02002793 unaccount_log_buffer(fs_info, bytenr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002794 }
2795 }
2796 free_extent_buffer(next);
2797 continue;
2798 }
2799 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
2800 if (ret) {
2801 free_extent_buffer(next);
2802 return ret;
2803 }
2804
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002805 if (path->nodes[*level-1])
2806 free_extent_buffer(path->nodes[*level-1]);
2807 path->nodes[*level-1] = next;
2808 *level = btrfs_header_level(next);
2809 path->slots[*level] = 0;
2810 cond_resched();
2811 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002812 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2813
2814 cond_resched();
2815 return 0;
2816}
2817
2818static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2819 struct btrfs_root *root,
2820 struct btrfs_path *path, int *level,
2821 struct walk_control *wc)
2822{
2823 struct btrfs_fs_info *fs_info = root->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002824 int i;
2825 int slot;
2826 int ret;
2827
2828 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2829 slot = path->slots[i];
2830 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2831 path->slots[i]++;
2832 *level = i;
2833 WARN_ON(*level == 0);
2834 return 0;
2835 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002836 ret = wc->process_func(root, path->nodes[*level], wc,
2837 btrfs_header_generation(path->nodes[*level]),
2838 *level);
2839 if (ret)
2840 return ret;
2841
2842 if (wc->free) {
2843 struct extent_buffer *next;
2844
2845 next = path->nodes[*level];
2846
2847 if (trans) {
2848 btrfs_tree_lock(next);
David Brazdil0f672f62019-12-10 10:32:29 +00002849 btrfs_set_lock_blocking_write(next);
2850 btrfs_clean_tree_block(next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002851 btrfs_wait_tree_block_writeback(next);
2852 btrfs_tree_unlock(next);
Olivier Deprez157378f2022-04-04 15:47:50 +02002853 ret = btrfs_pin_reserved_extent(trans,
2854 path->nodes[*level]->start,
2855 path->nodes[*level]->len);
2856 if (ret)
2857 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002858 } else {
2859 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2860 clear_extent_buffer_dirty(next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002861
Olivier Deprez157378f2022-04-04 15:47:50 +02002862 unaccount_log_buffer(fs_info,
2863 path->nodes[*level]->start);
2864 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002865 }
2866 free_extent_buffer(path->nodes[*level]);
2867 path->nodes[*level] = NULL;
2868 *level = i + 1;
2869 }
2870 }
2871 return 1;
2872}
2873
2874/*
2875 * drop the reference count on the tree rooted at 'snap'. This traverses
2876 * the tree freeing any blocks that have a ref count of zero after being
2877 * decremented.
2878 */
2879static int walk_log_tree(struct btrfs_trans_handle *trans,
2880 struct btrfs_root *log, struct walk_control *wc)
2881{
2882 struct btrfs_fs_info *fs_info = log->fs_info;
2883 int ret = 0;
2884 int wret;
2885 int level;
2886 struct btrfs_path *path;
2887 int orig_level;
2888
2889 path = btrfs_alloc_path();
2890 if (!path)
2891 return -ENOMEM;
2892
2893 level = btrfs_header_level(log->node);
2894 orig_level = level;
2895 path->nodes[level] = log->node;
Olivier Deprez157378f2022-04-04 15:47:50 +02002896 atomic_inc(&log->node->refs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002897 path->slots[level] = 0;
2898
2899 while (1) {
2900 wret = walk_down_log_tree(trans, log, path, &level, wc);
2901 if (wret > 0)
2902 break;
2903 if (wret < 0) {
2904 ret = wret;
2905 goto out;
2906 }
2907
2908 wret = walk_up_log_tree(trans, log, path, &level, wc);
2909 if (wret > 0)
2910 break;
2911 if (wret < 0) {
2912 ret = wret;
2913 goto out;
2914 }
2915 }
2916
2917 /* was the root node processed? if not, catch it here */
2918 if (path->nodes[orig_level]) {
2919 ret = wc->process_func(log, path->nodes[orig_level], wc,
2920 btrfs_header_generation(path->nodes[orig_level]),
2921 orig_level);
2922 if (ret)
2923 goto out;
2924 if (wc->free) {
2925 struct extent_buffer *next;
2926
2927 next = path->nodes[orig_level];
2928
2929 if (trans) {
2930 btrfs_tree_lock(next);
David Brazdil0f672f62019-12-10 10:32:29 +00002931 btrfs_set_lock_blocking_write(next);
2932 btrfs_clean_tree_block(next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002933 btrfs_wait_tree_block_writeback(next);
2934 btrfs_tree_unlock(next);
Olivier Deprez157378f2022-04-04 15:47:50 +02002935 ret = btrfs_pin_reserved_extent(trans,
2936 next->start, next->len);
2937 if (ret)
2938 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002939 } else {
2940 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2941 clear_extent_buffer_dirty(next);
Olivier Deprez157378f2022-04-04 15:47:50 +02002942 unaccount_log_buffer(fs_info, next->start);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002943 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002944 }
2945 }
2946
2947out:
2948 btrfs_free_path(path);
2949 return ret;
2950}
2951
2952/*
2953 * helper function to update the item for a given subvolumes log root
2954 * in the tree of log roots
2955 */
2956static int update_log_root(struct btrfs_trans_handle *trans,
David Brazdil0f672f62019-12-10 10:32:29 +00002957 struct btrfs_root *log,
2958 struct btrfs_root_item *root_item)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002959{
2960 struct btrfs_fs_info *fs_info = log->fs_info;
2961 int ret;
2962
2963 if (log->log_transid == 1) {
2964 /* insert root item on the first sync */
2965 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
David Brazdil0f672f62019-12-10 10:32:29 +00002966 &log->root_key, root_item);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002967 } else {
2968 ret = btrfs_update_root(trans, fs_info->log_root_tree,
David Brazdil0f672f62019-12-10 10:32:29 +00002969 &log->root_key, root_item);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002970 }
2971 return ret;
2972}
2973
2974static void wait_log_commit(struct btrfs_root *root, int transid)
2975{
2976 DEFINE_WAIT(wait);
2977 int index = transid % 2;
2978
2979 /*
2980 * we only allow two pending log transactions at a time,
2981 * so we know that if ours is more than 2 older than the
2982 * current transaction, we're done
2983 */
2984 for (;;) {
2985 prepare_to_wait(&root->log_commit_wait[index],
2986 &wait, TASK_UNINTERRUPTIBLE);
2987
2988 if (!(root->log_transid_committed < transid &&
2989 atomic_read(&root->log_commit[index])))
2990 break;
2991
2992 mutex_unlock(&root->log_mutex);
2993 schedule();
2994 mutex_lock(&root->log_mutex);
2995 }
2996 finish_wait(&root->log_commit_wait[index], &wait);
2997}
2998
2999static void wait_for_writer(struct btrfs_root *root)
3000{
3001 DEFINE_WAIT(wait);
3002
3003 for (;;) {
3004 prepare_to_wait(&root->log_writer_wait, &wait,
3005 TASK_UNINTERRUPTIBLE);
3006 if (!atomic_read(&root->log_writers))
3007 break;
3008
3009 mutex_unlock(&root->log_mutex);
3010 schedule();
3011 mutex_lock(&root->log_mutex);
3012 }
3013 finish_wait(&root->log_writer_wait, &wait);
3014}
3015
3016static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
3017 struct btrfs_log_ctx *ctx)
3018{
3019 if (!ctx)
3020 return;
3021
3022 mutex_lock(&root->log_mutex);
3023 list_del_init(&ctx->list);
3024 mutex_unlock(&root->log_mutex);
3025}
3026
3027/*
3028 * Invoked in log mutex context, or be sure there is no other task which
3029 * can access the list.
3030 */
3031static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
3032 int index, int error)
3033{
3034 struct btrfs_log_ctx *ctx;
3035 struct btrfs_log_ctx *safe;
3036
3037 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
3038 list_del_init(&ctx->list);
3039 ctx->log_ret = error;
3040 }
3041
3042 INIT_LIST_HEAD(&root->log_ctxs[index]);
3043}
3044
3045/*
3046 * btrfs_sync_log does sends a given tree log down to the disk and
3047 * updates the super blocks to record it. When this call is done,
3048 * you know that any inodes previously logged are safely on disk only
3049 * if it returns 0.
3050 *
3051 * Any other return value means you need to call btrfs_commit_transaction.
3052 * Some of the edge cases for fsyncing directories that have had unlinks
3053 * or renames done in the past mean that sometimes the only safe
3054 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3055 * that has happened.
3056 */
3057int btrfs_sync_log(struct btrfs_trans_handle *trans,
3058 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
3059{
3060 int index1;
3061 int index2;
3062 int mark;
3063 int ret;
3064 struct btrfs_fs_info *fs_info = root->fs_info;
3065 struct btrfs_root *log = root->log_root;
3066 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
David Brazdil0f672f62019-12-10 10:32:29 +00003067 struct btrfs_root_item new_root_item;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003068 int log_transid = 0;
3069 struct btrfs_log_ctx root_log_ctx;
3070 struct blk_plug plug;
3071
3072 mutex_lock(&root->log_mutex);
3073 log_transid = ctx->log_transid;
3074 if (root->log_transid_committed >= log_transid) {
3075 mutex_unlock(&root->log_mutex);
3076 return ctx->log_ret;
3077 }
3078
3079 index1 = log_transid % 2;
3080 if (atomic_read(&root->log_commit[index1])) {
3081 wait_log_commit(root, log_transid);
3082 mutex_unlock(&root->log_mutex);
3083 return ctx->log_ret;
3084 }
3085 ASSERT(log_transid == root->log_transid);
3086 atomic_set(&root->log_commit[index1], 1);
3087
3088 /* wait for previous tree log sync to complete */
3089 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
3090 wait_log_commit(root, log_transid - 1);
3091
3092 while (1) {
3093 int batch = atomic_read(&root->log_batch);
3094 /* when we're on an ssd, just kick the log commit out */
3095 if (!btrfs_test_opt(fs_info, SSD) &&
3096 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
3097 mutex_unlock(&root->log_mutex);
3098 schedule_timeout_uninterruptible(1);
3099 mutex_lock(&root->log_mutex);
3100 }
3101 wait_for_writer(root);
3102 if (batch == atomic_read(&root->log_batch))
3103 break;
3104 }
3105
3106 /* bail out if we need to do a full commit */
David Brazdil0f672f62019-12-10 10:32:29 +00003107 if (btrfs_need_log_full_commit(trans)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003108 ret = -EAGAIN;
3109 mutex_unlock(&root->log_mutex);
3110 goto out;
3111 }
3112
3113 if (log_transid % 2 == 0)
3114 mark = EXTENT_DIRTY;
3115 else
3116 mark = EXTENT_NEW;
3117
3118 /* we start IO on all the marked extents here, but we don't actually
3119 * wait for them until later.
3120 */
3121 blk_start_plug(&plug);
3122 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
3123 if (ret) {
3124 blk_finish_plug(&plug);
3125 btrfs_abort_transaction(trans, ret);
David Brazdil0f672f62019-12-10 10:32:29 +00003126 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003127 mutex_unlock(&root->log_mutex);
3128 goto out;
3129 }
3130
David Brazdil0f672f62019-12-10 10:32:29 +00003131 /*
3132 * We _must_ update under the root->log_mutex in order to make sure we
3133 * have a consistent view of the log root we are trying to commit at
3134 * this moment.
3135 *
3136 * We _must_ copy this into a local copy, because we are not holding the
3137 * log_root_tree->log_mutex yet. This is important because when we
3138 * commit the log_root_tree we must have a consistent view of the
3139 * log_root_tree when we update the super block to point at the
3140 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3141 * with the commit and possibly point at the new block which we may not
3142 * have written out.
3143 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003144 btrfs_set_root_node(&log->root_item, log->node);
David Brazdil0f672f62019-12-10 10:32:29 +00003145 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003146
3147 root->log_transid++;
3148 log->log_transid = root->log_transid;
3149 root->log_start_pid = 0;
3150 /*
3151 * IO has been started, blocks of the log tree have WRITTEN flag set
3152 * in their headers. new modifications of the log will be written to
3153 * new positions. so it's safe to allow log writers to go in.
3154 */
3155 mutex_unlock(&root->log_mutex);
3156
3157 btrfs_init_log_ctx(&root_log_ctx, NULL);
3158
3159 mutex_lock(&log_root_tree->log_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003160
3161 index2 = log_root_tree->log_transid % 2;
3162 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3163 root_log_ctx.log_transid = log_root_tree->log_transid;
3164
David Brazdil0f672f62019-12-10 10:32:29 +00003165 /*
3166 * Now we are safe to update the log_root_tree because we're under the
3167 * log_mutex, and we're a current writer so we're holding the commit
3168 * open until we drop the log_mutex.
3169 */
3170 ret = update_log_root(trans, log, &new_root_item);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003171 if (ret) {
3172 if (!list_empty(&root_log_ctx.list))
3173 list_del_init(&root_log_ctx.list);
3174
3175 blk_finish_plug(&plug);
David Brazdil0f672f62019-12-10 10:32:29 +00003176 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003177
3178 if (ret != -ENOSPC) {
3179 btrfs_abort_transaction(trans, ret);
3180 mutex_unlock(&log_root_tree->log_mutex);
3181 goto out;
3182 }
3183 btrfs_wait_tree_log_extents(log, mark);
3184 mutex_unlock(&log_root_tree->log_mutex);
3185 ret = -EAGAIN;
3186 goto out;
3187 }
3188
3189 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3190 blk_finish_plug(&plug);
3191 list_del_init(&root_log_ctx.list);
3192 mutex_unlock(&log_root_tree->log_mutex);
3193 ret = root_log_ctx.log_ret;
3194 goto out;
3195 }
3196
3197 index2 = root_log_ctx.log_transid % 2;
3198 if (atomic_read(&log_root_tree->log_commit[index2])) {
3199 blk_finish_plug(&plug);
3200 ret = btrfs_wait_tree_log_extents(log, mark);
3201 wait_log_commit(log_root_tree,
3202 root_log_ctx.log_transid);
3203 mutex_unlock(&log_root_tree->log_mutex);
3204 if (!ret)
3205 ret = root_log_ctx.log_ret;
3206 goto out;
3207 }
3208 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3209 atomic_set(&log_root_tree->log_commit[index2], 1);
3210
3211 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3212 wait_log_commit(log_root_tree,
3213 root_log_ctx.log_transid - 1);
3214 }
3215
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003216 /*
3217 * now that we've moved on to the tree of log tree roots,
3218 * check the full commit flag again
3219 */
David Brazdil0f672f62019-12-10 10:32:29 +00003220 if (btrfs_need_log_full_commit(trans)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003221 blk_finish_plug(&plug);
3222 btrfs_wait_tree_log_extents(log, mark);
3223 mutex_unlock(&log_root_tree->log_mutex);
3224 ret = -EAGAIN;
3225 goto out_wake_log_root;
3226 }
3227
3228 ret = btrfs_write_marked_extents(fs_info,
3229 &log_root_tree->dirty_log_pages,
3230 EXTENT_DIRTY | EXTENT_NEW);
3231 blk_finish_plug(&plug);
3232 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00003233 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003234 btrfs_abort_transaction(trans, ret);
3235 mutex_unlock(&log_root_tree->log_mutex);
3236 goto out_wake_log_root;
3237 }
3238 ret = btrfs_wait_tree_log_extents(log, mark);
3239 if (!ret)
3240 ret = btrfs_wait_tree_log_extents(log_root_tree,
3241 EXTENT_NEW | EXTENT_DIRTY);
3242 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00003243 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003244 mutex_unlock(&log_root_tree->log_mutex);
3245 goto out_wake_log_root;
3246 }
3247
3248 btrfs_set_super_log_root(fs_info->super_for_commit,
3249 log_root_tree->node->start);
3250 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3251 btrfs_header_level(log_root_tree->node));
3252
3253 log_root_tree->log_transid++;
3254 mutex_unlock(&log_root_tree->log_mutex);
3255
3256 /*
David Brazdil0f672f62019-12-10 10:32:29 +00003257 * Nobody else is going to jump in and write the ctree
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003258 * super here because the log_commit atomic below is protecting
3259 * us. We must be called with a transaction handle pinning
3260 * the running transaction open, so a full commit can't hop
3261 * in and cause problems either.
3262 */
3263 ret = write_all_supers(fs_info, 1);
3264 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00003265 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003266 btrfs_abort_transaction(trans, ret);
3267 goto out_wake_log_root;
3268 }
3269
3270 mutex_lock(&root->log_mutex);
3271 if (root->last_log_commit < log_transid)
3272 root->last_log_commit = log_transid;
3273 mutex_unlock(&root->log_mutex);
3274
3275out_wake_log_root:
3276 mutex_lock(&log_root_tree->log_mutex);
3277 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3278
3279 log_root_tree->log_transid_committed++;
3280 atomic_set(&log_root_tree->log_commit[index2], 0);
3281 mutex_unlock(&log_root_tree->log_mutex);
3282
3283 /*
3284 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3285 * all the updates above are seen by the woken threads. It might not be
3286 * necessary, but proving that seems to be hard.
3287 */
3288 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3289out:
3290 mutex_lock(&root->log_mutex);
3291 btrfs_remove_all_log_ctxs(root, index1, ret);
3292 root->log_transid_committed++;
3293 atomic_set(&root->log_commit[index1], 0);
3294 mutex_unlock(&root->log_mutex);
3295
3296 /*
3297 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3298 * all the updates above are seen by the woken threads. It might not be
3299 * necessary, but proving that seems to be hard.
3300 */
3301 cond_wake_up(&root->log_commit_wait[index1]);
3302 return ret;
3303}
3304
3305static void free_log_tree(struct btrfs_trans_handle *trans,
3306 struct btrfs_root *log)
3307{
3308 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003309 struct walk_control wc = {
3310 .free = 1,
3311 .process_func = process_one_buffer
3312 };
3313
3314 ret = walk_log_tree(trans, log, &wc);
3315 if (ret) {
3316 if (trans)
3317 btrfs_abort_transaction(trans, ret);
3318 else
3319 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3320 }
3321
David Brazdil0f672f62019-12-10 10:32:29 +00003322 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3323 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Olivier Deprez157378f2022-04-04 15:47:50 +02003324 extent_io_tree_release(&log->log_csum_range);
3325 btrfs_put_root(log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003326}
3327
3328/*
3329 * free all the extents used by the tree log. This should be called
3330 * at commit time of the full transaction
3331 */
3332int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3333{
3334 if (root->log_root) {
3335 free_log_tree(trans, root->log_root);
3336 root->log_root = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +02003337 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003338 }
3339 return 0;
3340}
3341
3342int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3343 struct btrfs_fs_info *fs_info)
3344{
3345 if (fs_info->log_root_tree) {
3346 free_log_tree(trans, fs_info->log_root_tree);
3347 fs_info->log_root_tree = NULL;
3348 }
3349 return 0;
3350}
3351
3352/*
David Brazdil0f672f62019-12-10 10:32:29 +00003353 * Check if an inode was logged in the current transaction. We can't always rely
3354 * on an inode's logged_trans value, because it's an in-memory only field and
3355 * therefore not persisted. This means that its value is lost if the inode gets
3356 * evicted and loaded again from disk (in which case it has a value of 0, and
3357 * certainly it is smaller then any possible transaction ID), when that happens
3358 * the full_sync flag is set in the inode's runtime flags, so on that case we
3359 * assume eviction happened and ignore the logged_trans value, assuming the
3360 * worst case, that the inode was logged before in the current transaction.
3361 */
3362static bool inode_logged(struct btrfs_trans_handle *trans,
3363 struct btrfs_inode *inode)
3364{
3365 if (inode->logged_trans == trans->transid)
3366 return true;
3367
3368 if (inode->last_trans == trans->transid &&
3369 test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3370 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3371 return true;
3372
3373 return false;
3374}
3375
3376/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003377 * If both a file and directory are logged, and unlinks or renames are
3378 * mixed in, we have a few interesting corners:
3379 *
3380 * create file X in dir Y
3381 * link file X to X.link in dir Y
3382 * fsync file X
3383 * unlink file X but leave X.link
3384 * fsync dir Y
3385 *
3386 * After a crash we would expect only X.link to exist. But file X
3387 * didn't get fsync'd again so the log has back refs for X and X.link.
3388 *
3389 * We solve this by removing directory entries and inode backrefs from the
3390 * log when a file that was logged in the current transaction is
3391 * unlinked. Any later fsync will include the updated log entries, and
3392 * we'll be able to reconstruct the proper directory items from backrefs.
3393 *
3394 * This optimizations allows us to avoid relogging the entire inode
3395 * or the entire directory.
3396 */
3397int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3398 struct btrfs_root *root,
3399 const char *name, int name_len,
3400 struct btrfs_inode *dir, u64 index)
3401{
3402 struct btrfs_root *log;
3403 struct btrfs_dir_item *di;
3404 struct btrfs_path *path;
3405 int ret;
3406 int err = 0;
3407 int bytes_del = 0;
3408 u64 dir_ino = btrfs_ino(dir);
3409
David Brazdil0f672f62019-12-10 10:32:29 +00003410 if (!inode_logged(trans, dir))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003411 return 0;
3412
3413 ret = join_running_log_trans(root);
3414 if (ret)
3415 return 0;
3416
3417 mutex_lock(&dir->log_mutex);
3418
3419 log = root->log_root;
3420 path = btrfs_alloc_path();
3421 if (!path) {
3422 err = -ENOMEM;
3423 goto out_unlock;
3424 }
3425
3426 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3427 name, name_len, -1);
3428 if (IS_ERR(di)) {
3429 err = PTR_ERR(di);
3430 goto fail;
3431 }
3432 if (di) {
3433 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3434 bytes_del += name_len;
3435 if (ret) {
3436 err = ret;
3437 goto fail;
3438 }
3439 }
3440 btrfs_release_path(path);
3441 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3442 index, name, name_len, -1);
3443 if (IS_ERR(di)) {
3444 err = PTR_ERR(di);
3445 goto fail;
3446 }
3447 if (di) {
3448 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3449 bytes_del += name_len;
3450 if (ret) {
3451 err = ret;
3452 goto fail;
3453 }
3454 }
3455
3456 /* update the directory size in the log to reflect the names
3457 * we have removed
3458 */
3459 if (bytes_del) {
3460 struct btrfs_key key;
3461
3462 key.objectid = dir_ino;
3463 key.offset = 0;
3464 key.type = BTRFS_INODE_ITEM_KEY;
3465 btrfs_release_path(path);
3466
3467 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3468 if (ret < 0) {
3469 err = ret;
3470 goto fail;
3471 }
3472 if (ret == 0) {
3473 struct btrfs_inode_item *item;
3474 u64 i_size;
3475
3476 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3477 struct btrfs_inode_item);
3478 i_size = btrfs_inode_size(path->nodes[0], item);
3479 if (i_size > bytes_del)
3480 i_size -= bytes_del;
3481 else
3482 i_size = 0;
3483 btrfs_set_inode_size(path->nodes[0], item, i_size);
3484 btrfs_mark_buffer_dirty(path->nodes[0]);
3485 } else
3486 ret = 0;
3487 btrfs_release_path(path);
3488 }
3489fail:
3490 btrfs_free_path(path);
3491out_unlock:
3492 mutex_unlock(&dir->log_mutex);
Olivier Deprez0e641232021-09-23 10:07:05 +02003493 if (err == -ENOSPC) {
David Brazdil0f672f62019-12-10 10:32:29 +00003494 btrfs_set_log_full_commit(trans);
Olivier Deprez0e641232021-09-23 10:07:05 +02003495 err = 0;
3496 } else if (err < 0 && err != -ENOENT) {
3497 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3498 btrfs_abort_transaction(trans, err);
3499 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003500
3501 btrfs_end_log_trans(root);
3502
3503 return err;
3504}
3505
3506/* see comments for btrfs_del_dir_entries_in_log */
3507int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3508 struct btrfs_root *root,
3509 const char *name, int name_len,
3510 struct btrfs_inode *inode, u64 dirid)
3511{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003512 struct btrfs_root *log;
3513 u64 index;
3514 int ret;
3515
David Brazdil0f672f62019-12-10 10:32:29 +00003516 if (!inode_logged(trans, inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003517 return 0;
3518
3519 ret = join_running_log_trans(root);
3520 if (ret)
3521 return 0;
3522 log = root->log_root;
3523 mutex_lock(&inode->log_mutex);
3524
3525 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3526 dirid, &index);
3527 mutex_unlock(&inode->log_mutex);
3528 if (ret == -ENOSPC) {
David Brazdil0f672f62019-12-10 10:32:29 +00003529 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003530 ret = 0;
3531 } else if (ret < 0 && ret != -ENOENT)
3532 btrfs_abort_transaction(trans, ret);
3533 btrfs_end_log_trans(root);
3534
3535 return ret;
3536}
3537
3538/*
3539 * creates a range item in the log for 'dirid'. first_offset and
3540 * last_offset tell us which parts of the key space the log should
3541 * be considered authoritative for.
3542 */
3543static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3544 struct btrfs_root *log,
3545 struct btrfs_path *path,
3546 int key_type, u64 dirid,
3547 u64 first_offset, u64 last_offset)
3548{
3549 int ret;
3550 struct btrfs_key key;
3551 struct btrfs_dir_log_item *item;
3552
3553 key.objectid = dirid;
3554 key.offset = first_offset;
3555 if (key_type == BTRFS_DIR_ITEM_KEY)
3556 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3557 else
3558 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3559 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3560 if (ret)
3561 return ret;
3562
3563 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3564 struct btrfs_dir_log_item);
3565 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3566 btrfs_mark_buffer_dirty(path->nodes[0]);
3567 btrfs_release_path(path);
3568 return 0;
3569}
3570
3571/*
3572 * log all the items included in the current transaction for a given
3573 * directory. This also creates the range items in the log tree required
3574 * to replay anything deleted before the fsync
3575 */
3576static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3577 struct btrfs_root *root, struct btrfs_inode *inode,
3578 struct btrfs_path *path,
3579 struct btrfs_path *dst_path, int key_type,
3580 struct btrfs_log_ctx *ctx,
3581 u64 min_offset, u64 *last_offset_ret)
3582{
3583 struct btrfs_key min_key;
3584 struct btrfs_root *log = root->log_root;
3585 struct extent_buffer *src;
3586 int err = 0;
3587 int ret;
3588 int i;
3589 int nritems;
3590 u64 first_offset = min_offset;
3591 u64 last_offset = (u64)-1;
3592 u64 ino = btrfs_ino(inode);
3593
3594 log = root->log_root;
3595
3596 min_key.objectid = ino;
3597 min_key.type = key_type;
3598 min_key.offset = min_offset;
3599
3600 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3601
3602 /*
3603 * we didn't find anything from this transaction, see if there
3604 * is anything at all
3605 */
3606 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3607 min_key.objectid = ino;
3608 min_key.type = key_type;
3609 min_key.offset = (u64)-1;
3610 btrfs_release_path(path);
3611 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3612 if (ret < 0) {
3613 btrfs_release_path(path);
3614 return ret;
3615 }
3616 ret = btrfs_previous_item(root, path, ino, key_type);
3617
3618 /* if ret == 0 there are items for this type,
3619 * create a range to tell us the last key of this type.
3620 * otherwise, there are no items in this directory after
3621 * *min_offset, and we create a range to indicate that.
3622 */
3623 if (ret == 0) {
3624 struct btrfs_key tmp;
3625 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3626 path->slots[0]);
3627 if (key_type == tmp.type)
3628 first_offset = max(min_offset, tmp.offset) + 1;
3629 }
3630 goto done;
3631 }
3632
3633 /* go backward to find any previous key */
3634 ret = btrfs_previous_item(root, path, ino, key_type);
3635 if (ret == 0) {
3636 struct btrfs_key tmp;
3637 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3638 if (key_type == tmp.type) {
3639 first_offset = tmp.offset;
3640 ret = overwrite_item(trans, log, dst_path,
3641 path->nodes[0], path->slots[0],
3642 &tmp);
3643 if (ret) {
3644 err = ret;
3645 goto done;
3646 }
3647 }
3648 }
3649 btrfs_release_path(path);
3650
David Brazdil0f672f62019-12-10 10:32:29 +00003651 /*
3652 * Find the first key from this transaction again. See the note for
3653 * log_new_dir_dentries, if we're logging a directory recursively we
3654 * won't be holding its i_mutex, which means we can modify the directory
3655 * while we're logging it. If we remove an entry between our first
3656 * search and this search we'll not find the key again and can just
3657 * bail.
3658 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003659search:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003660 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00003661 if (ret != 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003662 goto done;
3663
3664 /*
3665 * we have a block from this transaction, log every item in it
3666 * from our directory
3667 */
3668 while (1) {
3669 struct btrfs_key tmp;
3670 src = path->nodes[0];
3671 nritems = btrfs_header_nritems(src);
3672 for (i = path->slots[0]; i < nritems; i++) {
3673 struct btrfs_dir_item *di;
3674
3675 btrfs_item_key_to_cpu(src, &min_key, i);
3676
3677 if (min_key.objectid != ino || min_key.type != key_type)
3678 goto done;
Olivier Deprez0e641232021-09-23 10:07:05 +02003679
3680 if (need_resched()) {
3681 btrfs_release_path(path);
3682 cond_resched();
3683 goto search;
3684 }
3685
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003686 ret = overwrite_item(trans, log, dst_path, src, i,
3687 &min_key);
3688 if (ret) {
3689 err = ret;
3690 goto done;
3691 }
3692
3693 /*
3694 * We must make sure that when we log a directory entry,
3695 * the corresponding inode, after log replay, has a
3696 * matching link count. For example:
3697 *
3698 * touch foo
3699 * mkdir mydir
3700 * sync
3701 * ln foo mydir/bar
3702 * xfs_io -c "fsync" mydir
3703 * <crash>
3704 * <mount fs and log replay>
3705 *
3706 * Would result in a fsync log that when replayed, our
3707 * file inode would have a link count of 1, but we get
3708 * two directory entries pointing to the same inode.
3709 * After removing one of the names, it would not be
3710 * possible to remove the other name, which resulted
3711 * always in stale file handle errors, and would not
3712 * be possible to rmdir the parent directory, since
3713 * its i_size could never decrement to the value
3714 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3715 */
3716 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3717 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3718 if (ctx &&
3719 (btrfs_dir_transid(src, di) == trans->transid ||
3720 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3721 tmp.type != BTRFS_ROOT_ITEM_KEY)
3722 ctx->log_new_dentries = true;
3723 }
3724 path->slots[0] = nritems;
3725
3726 /*
3727 * look ahead to the next item and see if it is also
3728 * from this directory and from this transaction
3729 */
3730 ret = btrfs_next_leaf(root, path);
3731 if (ret) {
3732 if (ret == 1)
3733 last_offset = (u64)-1;
3734 else
3735 err = ret;
3736 goto done;
3737 }
3738 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3739 if (tmp.objectid != ino || tmp.type != key_type) {
3740 last_offset = (u64)-1;
3741 goto done;
3742 }
3743 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3744 ret = overwrite_item(trans, log, dst_path,
3745 path->nodes[0], path->slots[0],
3746 &tmp);
3747 if (ret)
3748 err = ret;
3749 else
3750 last_offset = tmp.offset;
3751 goto done;
3752 }
3753 }
3754done:
3755 btrfs_release_path(path);
3756 btrfs_release_path(dst_path);
3757
3758 if (err == 0) {
3759 *last_offset_ret = last_offset;
3760 /*
3761 * insert the log range keys to indicate where the log
3762 * is valid
3763 */
3764 ret = insert_dir_log_key(trans, log, path, key_type,
3765 ino, first_offset, last_offset);
3766 if (ret)
3767 err = ret;
3768 }
3769 return err;
3770}
3771
3772/*
3773 * logging directories is very similar to logging inodes, We find all the items
3774 * from the current transaction and write them to the log.
3775 *
3776 * The recovery code scans the directory in the subvolume, and if it finds a
3777 * key in the range logged that is not present in the log tree, then it means
3778 * that dir entry was unlinked during the transaction.
3779 *
3780 * In order for that scan to work, we must include one key smaller than
3781 * the smallest logged by this transaction and one key larger than the largest
3782 * key logged by this transaction.
3783 */
3784static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3785 struct btrfs_root *root, struct btrfs_inode *inode,
3786 struct btrfs_path *path,
3787 struct btrfs_path *dst_path,
3788 struct btrfs_log_ctx *ctx)
3789{
3790 u64 min_key;
3791 u64 max_key;
3792 int ret;
3793 int key_type = BTRFS_DIR_ITEM_KEY;
3794
3795again:
3796 min_key = 0;
3797 max_key = 0;
3798 while (1) {
3799 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3800 ctx, min_key, &max_key);
3801 if (ret)
3802 return ret;
3803 if (max_key == (u64)-1)
3804 break;
3805 min_key = max_key + 1;
3806 }
3807
3808 if (key_type == BTRFS_DIR_ITEM_KEY) {
3809 key_type = BTRFS_DIR_INDEX_KEY;
3810 goto again;
3811 }
3812 return 0;
3813}
3814
3815/*
3816 * a helper function to drop items from the log before we relog an
3817 * inode. max_key_type indicates the highest item type to remove.
3818 * This cannot be run for file data extents because it does not
3819 * free the extents they point to.
3820 */
3821static int drop_objectid_items(struct btrfs_trans_handle *trans,
3822 struct btrfs_root *log,
3823 struct btrfs_path *path,
3824 u64 objectid, int max_key_type)
3825{
3826 int ret;
3827 struct btrfs_key key;
3828 struct btrfs_key found_key;
3829 int start_slot;
3830
3831 key.objectid = objectid;
3832 key.type = max_key_type;
3833 key.offset = (u64)-1;
3834
3835 while (1) {
3836 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3837 BUG_ON(ret == 0); /* Logic error */
3838 if (ret < 0)
3839 break;
3840
3841 if (path->slots[0] == 0)
3842 break;
3843
3844 path->slots[0]--;
3845 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3846 path->slots[0]);
3847
3848 if (found_key.objectid != objectid)
3849 break;
3850
3851 found_key.offset = 0;
3852 found_key.type = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003853 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
David Brazdil0f672f62019-12-10 10:32:29 +00003854 if (ret < 0)
3855 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003856
3857 ret = btrfs_del_items(trans, log, path, start_slot,
3858 path->slots[0] - start_slot + 1);
3859 /*
3860 * If start slot isn't 0 then we don't need to re-search, we've
3861 * found the last guy with the objectid in this tree.
3862 */
3863 if (ret || start_slot != 0)
3864 break;
3865 btrfs_release_path(path);
3866 }
3867 btrfs_release_path(path);
3868 if (ret > 0)
3869 ret = 0;
3870 return ret;
3871}
3872
3873static void fill_inode_item(struct btrfs_trans_handle *trans,
3874 struct extent_buffer *leaf,
3875 struct btrfs_inode_item *item,
3876 struct inode *inode, int log_inode_only,
3877 u64 logged_isize)
3878{
3879 struct btrfs_map_token token;
3880
David Brazdil0f672f62019-12-10 10:32:29 +00003881 btrfs_init_map_token(&token, leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003882
3883 if (log_inode_only) {
3884 /* set the generation to zero so the recover code
3885 * can tell the difference between an logging
3886 * just to say 'this inode exists' and a logging
3887 * to say 'update this inode with these values'
3888 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003889 btrfs_set_token_inode_generation(&token, item, 0);
3890 btrfs_set_token_inode_size(&token, item, logged_isize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003891 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02003892 btrfs_set_token_inode_generation(&token, item,
3893 BTRFS_I(inode)->generation);
3894 btrfs_set_token_inode_size(&token, item, inode->i_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003895 }
3896
Olivier Deprez157378f2022-04-04 15:47:50 +02003897 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3898 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3899 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3900 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003901
Olivier Deprez157378f2022-04-04 15:47:50 +02003902 btrfs_set_token_timespec_sec(&token, &item->atime,
3903 inode->i_atime.tv_sec);
3904 btrfs_set_token_timespec_nsec(&token, &item->atime,
3905 inode->i_atime.tv_nsec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003906
Olivier Deprez157378f2022-04-04 15:47:50 +02003907 btrfs_set_token_timespec_sec(&token, &item->mtime,
3908 inode->i_mtime.tv_sec);
3909 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3910 inode->i_mtime.tv_nsec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003911
Olivier Deprez157378f2022-04-04 15:47:50 +02003912 btrfs_set_token_timespec_sec(&token, &item->ctime,
3913 inode->i_ctime.tv_sec);
3914 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3915 inode->i_ctime.tv_nsec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003916
Olivier Deprez157378f2022-04-04 15:47:50 +02003917 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003918
Olivier Deprez157378f2022-04-04 15:47:50 +02003919 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3920 btrfs_set_token_inode_transid(&token, item, trans->transid);
3921 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3922 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3923 btrfs_set_token_inode_block_group(&token, item, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003924}
3925
3926static int log_inode_item(struct btrfs_trans_handle *trans,
3927 struct btrfs_root *log, struct btrfs_path *path,
3928 struct btrfs_inode *inode)
3929{
3930 struct btrfs_inode_item *inode_item;
3931 int ret;
3932
3933 ret = btrfs_insert_empty_item(trans, log, path,
3934 &inode->location, sizeof(*inode_item));
3935 if (ret && ret != -EEXIST)
3936 return ret;
3937 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3938 struct btrfs_inode_item);
3939 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3940 0, 0);
3941 btrfs_release_path(path);
3942 return 0;
3943}
3944
Olivier Deprez0e641232021-09-23 10:07:05 +02003945static int log_csums(struct btrfs_trans_handle *trans,
Olivier Deprez157378f2022-04-04 15:47:50 +02003946 struct btrfs_inode *inode,
Olivier Deprez0e641232021-09-23 10:07:05 +02003947 struct btrfs_root *log_root,
3948 struct btrfs_ordered_sum *sums)
3949{
Olivier Deprez157378f2022-04-04 15:47:50 +02003950 const u64 lock_end = sums->bytenr + sums->len - 1;
3951 struct extent_state *cached_state = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +02003952 int ret;
3953
3954 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003955 * If this inode was not used for reflink operations in the current
3956 * transaction with new extents, then do the fast path, no need to
3957 * worry about logging checksum items with overlapping ranges.
3958 */
3959 if (inode->last_reflink_trans < trans->transid)
3960 return btrfs_csum_file_blocks(trans, log_root, sums);
3961
3962 /*
3963 * Serialize logging for checksums. This is to avoid racing with the
3964 * same checksum being logged by another task that is logging another
3965 * file which happens to refer to the same extent as well. Such races
3966 * can leave checksum items in the log with overlapping ranges.
3967 */
3968 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
3969 lock_end, &cached_state);
3970 if (ret)
3971 return ret;
3972 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02003973 * Due to extent cloning, we might have logged a csum item that covers a
3974 * subrange of a cloned extent, and later we can end up logging a csum
3975 * item for a larger subrange of the same extent or the entire range.
3976 * This would leave csum items in the log tree that cover the same range
3977 * and break the searches for checksums in the log tree, resulting in
3978 * some checksums missing in the fs/subvolume tree. So just delete (or
3979 * trim and adjust) any existing csum items in the log for this range.
3980 */
3981 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
Olivier Deprez157378f2022-04-04 15:47:50 +02003982 if (!ret)
3983 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Olivier Deprez0e641232021-09-23 10:07:05 +02003984
Olivier Deprez157378f2022-04-04 15:47:50 +02003985 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
3986 &cached_state);
3987
3988 return ret;
Olivier Deprez0e641232021-09-23 10:07:05 +02003989}
3990
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003991static noinline int copy_items(struct btrfs_trans_handle *trans,
3992 struct btrfs_inode *inode,
3993 struct btrfs_path *dst_path,
Olivier Deprez0e641232021-09-23 10:07:05 +02003994 struct btrfs_path *src_path,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003995 int start_slot, int nr, int inode_only,
3996 u64 logged_isize)
3997{
3998 struct btrfs_fs_info *fs_info = trans->fs_info;
3999 unsigned long src_offset;
4000 unsigned long dst_offset;
4001 struct btrfs_root *log = inode->root->log_root;
4002 struct btrfs_file_extent_item *extent;
4003 struct btrfs_inode_item *inode_item;
4004 struct extent_buffer *src = src_path->nodes[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004005 int ret;
4006 struct btrfs_key *ins_keys;
4007 u32 *ins_sizes;
4008 char *ins_data;
4009 int i;
4010 struct list_head ordered_sums;
4011 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004012
4013 INIT_LIST_HEAD(&ordered_sums);
4014
4015 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
4016 nr * sizeof(u32), GFP_NOFS);
4017 if (!ins_data)
4018 return -ENOMEM;
4019
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004020 ins_sizes = (u32 *)ins_data;
4021 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
4022
4023 for (i = 0; i < nr; i++) {
4024 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
4025 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
4026 }
4027 ret = btrfs_insert_empty_items(trans, log, dst_path,
4028 ins_keys, ins_sizes, nr);
4029 if (ret) {
4030 kfree(ins_data);
4031 return ret;
4032 }
4033
4034 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
4035 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
4036 dst_path->slots[0]);
4037
4038 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
4039
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004040 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
4041 inode_item = btrfs_item_ptr(dst_path->nodes[0],
4042 dst_path->slots[0],
4043 struct btrfs_inode_item);
4044 fill_inode_item(trans, dst_path->nodes[0], inode_item,
4045 &inode->vfs_inode,
4046 inode_only == LOG_INODE_EXISTS,
4047 logged_isize);
4048 } else {
4049 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4050 src_offset, ins_sizes[i]);
4051 }
4052
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004053 /* take a reference on file data extents so that truncates
4054 * or deletes of this inode don't have to relog the inode
4055 * again
4056 */
4057 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
4058 !skip_csum) {
4059 int found_type;
4060 extent = btrfs_item_ptr(src, start_slot + i,
4061 struct btrfs_file_extent_item);
4062
4063 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4064 continue;
4065
4066 found_type = btrfs_file_extent_type(src, extent);
4067 if (found_type == BTRFS_FILE_EXTENT_REG) {
4068 u64 ds, dl, cs, cl;
4069 ds = btrfs_file_extent_disk_bytenr(src,
4070 extent);
4071 /* ds == 0 is a hole */
4072 if (ds == 0)
4073 continue;
4074
4075 dl = btrfs_file_extent_disk_num_bytes(src,
4076 extent);
4077 cs = btrfs_file_extent_offset(src, extent);
4078 cl = btrfs_file_extent_num_bytes(src,
4079 extent);
4080 if (btrfs_file_extent_compression(src,
4081 extent)) {
4082 cs = 0;
4083 cl = dl;
4084 }
4085
4086 ret = btrfs_lookup_csums_range(
4087 fs_info->csum_root,
4088 ds + cs, ds + cs + cl - 1,
4089 &ordered_sums, 0);
Olivier Deprez0e641232021-09-23 10:07:05 +02004090 if (ret)
4091 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004092 }
4093 }
4094 }
4095
4096 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
4097 btrfs_release_path(dst_path);
4098 kfree(ins_data);
4099
4100 /*
4101 * we have to do this after the loop above to avoid changing the
4102 * log tree while trying to change the log tree.
4103 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004104 while (!list_empty(&ordered_sums)) {
4105 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4106 struct btrfs_ordered_sum,
4107 list);
4108 if (!ret)
Olivier Deprez157378f2022-04-04 15:47:50 +02004109 ret = log_csums(trans, inode, log, sums);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004110 list_del(&sums->list);
4111 kfree(sums);
4112 }
4113
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004114 return ret;
4115}
4116
Olivier Deprez157378f2022-04-04 15:47:50 +02004117static int extent_cmp(void *priv, const struct list_head *a,
4118 const struct list_head *b)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004119{
4120 struct extent_map *em1, *em2;
4121
4122 em1 = list_entry(a, struct extent_map, list);
4123 em2 = list_entry(b, struct extent_map, list);
4124
4125 if (em1->start < em2->start)
4126 return -1;
4127 else if (em1->start > em2->start)
4128 return 1;
4129 return 0;
4130}
4131
4132static int log_extent_csums(struct btrfs_trans_handle *trans,
4133 struct btrfs_inode *inode,
4134 struct btrfs_root *log_root,
Olivier Deprez157378f2022-04-04 15:47:50 +02004135 const struct extent_map *em,
4136 struct btrfs_log_ctx *ctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004137{
Olivier Deprez157378f2022-04-04 15:47:50 +02004138 struct btrfs_ordered_extent *ordered;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004139 u64 csum_offset;
4140 u64 csum_len;
Olivier Deprez157378f2022-04-04 15:47:50 +02004141 u64 mod_start = em->mod_start;
4142 u64 mod_len = em->mod_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004143 LIST_HEAD(ordered_sums);
4144 int ret = 0;
4145
4146 if (inode->flags & BTRFS_INODE_NODATASUM ||
4147 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4148 em->block_start == EXTENT_MAP_HOLE)
4149 return 0;
4150
Olivier Deprez157378f2022-04-04 15:47:50 +02004151 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4152 const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4153 const u64 mod_end = mod_start + mod_len;
4154 struct btrfs_ordered_sum *sums;
4155
4156 if (mod_len == 0)
4157 break;
4158
4159 if (ordered_end <= mod_start)
4160 continue;
4161 if (mod_end <= ordered->file_offset)
4162 break;
4163
4164 /*
4165 * We are going to copy all the csums on this ordered extent, so
4166 * go ahead and adjust mod_start and mod_len in case this ordered
4167 * extent has already been logged.
4168 */
4169 if (ordered->file_offset > mod_start) {
4170 if (ordered_end >= mod_end)
4171 mod_len = ordered->file_offset - mod_start;
4172 /*
4173 * If we have this case
4174 *
4175 * |--------- logged extent ---------|
4176 * |----- ordered extent ----|
4177 *
4178 * Just don't mess with mod_start and mod_len, we'll
4179 * just end up logging more csums than we need and it
4180 * will be ok.
4181 */
4182 } else {
4183 if (ordered_end < mod_end) {
4184 mod_len = mod_end - ordered_end;
4185 mod_start = ordered_end;
4186 } else {
4187 mod_len = 0;
4188 }
4189 }
4190
4191 /*
4192 * To keep us from looping for the above case of an ordered
4193 * extent that falls inside of the logged extent.
4194 */
4195 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4196 continue;
4197
4198 list_for_each_entry(sums, &ordered->list, list) {
4199 ret = log_csums(trans, inode, log_root, sums);
4200 if (ret)
4201 return ret;
4202 }
4203 }
4204
4205 /* We're done, found all csums in the ordered extents. */
4206 if (mod_len == 0)
4207 return 0;
4208
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004209 /* If we're compressed we have to save the entire range of csums. */
4210 if (em->compress_type) {
4211 csum_offset = 0;
4212 csum_len = max(em->block_len, em->orig_block_len);
4213 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02004214 csum_offset = mod_start - em->start;
4215 csum_len = mod_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004216 }
4217
4218 /* block start is already adjusted for the file extent offset. */
4219 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
4220 em->block_start + csum_offset,
4221 em->block_start + csum_offset +
4222 csum_len - 1, &ordered_sums, 0);
4223 if (ret)
4224 return ret;
4225
4226 while (!list_empty(&ordered_sums)) {
4227 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4228 struct btrfs_ordered_sum,
4229 list);
4230 if (!ret)
Olivier Deprez157378f2022-04-04 15:47:50 +02004231 ret = log_csums(trans, inode, log_root, sums);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004232 list_del(&sums->list);
4233 kfree(sums);
4234 }
4235
4236 return ret;
4237}
4238
4239static int log_one_extent(struct btrfs_trans_handle *trans,
4240 struct btrfs_inode *inode, struct btrfs_root *root,
4241 const struct extent_map *em,
4242 struct btrfs_path *path,
4243 struct btrfs_log_ctx *ctx)
4244{
4245 struct btrfs_root *log = root->log_root;
4246 struct btrfs_file_extent_item *fi;
4247 struct extent_buffer *leaf;
4248 struct btrfs_map_token token;
4249 struct btrfs_key key;
4250 u64 extent_offset = em->start - em->orig_start;
4251 u64 block_len;
4252 int ret;
4253 int extent_inserted = 0;
4254
Olivier Deprez157378f2022-04-04 15:47:50 +02004255 ret = log_extent_csums(trans, inode, log, em, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004256 if (ret)
4257 return ret;
4258
Olivier Deprez157378f2022-04-04 15:47:50 +02004259 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004260 em->start + em->len, NULL, 0, 1,
4261 sizeof(*fi), &extent_inserted);
4262 if (ret)
4263 return ret;
4264
4265 if (!extent_inserted) {
4266 key.objectid = btrfs_ino(inode);
4267 key.type = BTRFS_EXTENT_DATA_KEY;
4268 key.offset = em->start;
4269
4270 ret = btrfs_insert_empty_item(trans, log, path, &key,
4271 sizeof(*fi));
4272 if (ret)
4273 return ret;
4274 }
4275 leaf = path->nodes[0];
David Brazdil0f672f62019-12-10 10:32:29 +00004276 btrfs_init_map_token(&token, leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004277 fi = btrfs_item_ptr(leaf, path->slots[0],
4278 struct btrfs_file_extent_item);
4279
Olivier Deprez157378f2022-04-04 15:47:50 +02004280 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004281 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Olivier Deprez157378f2022-04-04 15:47:50 +02004282 btrfs_set_token_file_extent_type(&token, fi,
4283 BTRFS_FILE_EXTENT_PREALLOC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004284 else
Olivier Deprez157378f2022-04-04 15:47:50 +02004285 btrfs_set_token_file_extent_type(&token, fi,
4286 BTRFS_FILE_EXTENT_REG);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004287
4288 block_len = max(em->block_len, em->orig_block_len);
4289 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004290 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4291 em->block_start);
4292 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004293 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004294 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004295 em->block_start -
Olivier Deprez157378f2022-04-04 15:47:50 +02004296 extent_offset);
4297 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004298 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02004299 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4300 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004301 }
4302
Olivier Deprez157378f2022-04-04 15:47:50 +02004303 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4304 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4305 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4306 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4307 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4308 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004309 btrfs_mark_buffer_dirty(leaf);
4310
4311 btrfs_release_path(path);
4312
4313 return ret;
4314}
4315
4316/*
4317 * Log all prealloc extents beyond the inode's i_size to make sure we do not
Olivier Deprez157378f2022-04-04 15:47:50 +02004318 * lose them after doing a full/fast fsync and replaying the log. We scan the
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004319 * subvolume's root instead of iterating the inode's extent map tree because
4320 * otherwise we can log incorrect extent items based on extent map conversion.
4321 * That can happen due to the fact that extent maps are merged when they
4322 * are not in the extent map tree's list of modified extents.
4323 */
4324static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4325 struct btrfs_inode *inode,
4326 struct btrfs_path *path)
4327{
4328 struct btrfs_root *root = inode->root;
4329 struct btrfs_key key;
4330 const u64 i_size = i_size_read(&inode->vfs_inode);
4331 const u64 ino = btrfs_ino(inode);
4332 struct btrfs_path *dst_path = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +02004333 bool dropped_extents = false;
4334 u64 truncate_offset = i_size;
4335 struct extent_buffer *leaf;
4336 int slot;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004337 int ins_nr = 0;
4338 int start_slot;
4339 int ret;
4340
4341 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4342 return 0;
4343
4344 key.objectid = ino;
4345 key.type = BTRFS_EXTENT_DATA_KEY;
4346 key.offset = i_size;
4347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4348 if (ret < 0)
4349 goto out;
4350
Olivier Deprez0e641232021-09-23 10:07:05 +02004351 /*
4352 * We must check if there is a prealloc extent that starts before the
4353 * i_size and crosses the i_size boundary. This is to ensure later we
4354 * truncate down to the end of that extent and not to the i_size, as
4355 * otherwise we end up losing part of the prealloc extent after a log
4356 * replay and with an implicit hole if there is another prealloc extent
4357 * that starts at an offset beyond i_size.
4358 */
4359 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4360 if (ret < 0)
4361 goto out;
4362
4363 if (ret == 0) {
4364 struct btrfs_file_extent_item *ei;
4365
4366 leaf = path->nodes[0];
4367 slot = path->slots[0];
4368 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4369
4370 if (btrfs_file_extent_type(leaf, ei) ==
4371 BTRFS_FILE_EXTENT_PREALLOC) {
4372 u64 extent_end;
4373
4374 btrfs_item_key_to_cpu(leaf, &key, slot);
4375 extent_end = key.offset +
4376 btrfs_file_extent_num_bytes(leaf, ei);
4377
4378 if (extent_end > i_size)
4379 truncate_offset = extent_end;
4380 }
4381 } else {
4382 ret = 0;
4383 }
4384
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004385 while (true) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004386 leaf = path->nodes[0];
4387 slot = path->slots[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004388
4389 if (slot >= btrfs_header_nritems(leaf)) {
4390 if (ins_nr > 0) {
4391 ret = copy_items(trans, inode, dst_path, path,
Olivier Deprez0e641232021-09-23 10:07:05 +02004392 start_slot, ins_nr, 1, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004393 if (ret < 0)
4394 goto out;
4395 ins_nr = 0;
4396 }
4397 ret = btrfs_next_leaf(root, path);
4398 if (ret < 0)
4399 goto out;
4400 if (ret > 0) {
4401 ret = 0;
4402 break;
4403 }
4404 continue;
4405 }
4406
4407 btrfs_item_key_to_cpu(leaf, &key, slot);
4408 if (key.objectid > ino)
4409 break;
4410 if (WARN_ON_ONCE(key.objectid < ino) ||
4411 key.type < BTRFS_EXTENT_DATA_KEY ||
4412 key.offset < i_size) {
4413 path->slots[0]++;
4414 continue;
4415 }
Olivier Deprez0e641232021-09-23 10:07:05 +02004416 if (!dropped_extents) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004417 /*
4418 * Avoid logging extent items logged in past fsync calls
4419 * and leading to duplicate keys in the log tree.
4420 */
4421 do {
4422 ret = btrfs_truncate_inode_items(trans,
4423 root->log_root,
4424 &inode->vfs_inode,
Olivier Deprez0e641232021-09-23 10:07:05 +02004425 truncate_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004426 BTRFS_EXTENT_DATA_KEY);
4427 } while (ret == -EAGAIN);
4428 if (ret)
4429 goto out;
Olivier Deprez0e641232021-09-23 10:07:05 +02004430 dropped_extents = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004431 }
4432 if (ins_nr == 0)
4433 start_slot = slot;
4434 ins_nr++;
4435 path->slots[0]++;
4436 if (!dst_path) {
4437 dst_path = btrfs_alloc_path();
4438 if (!dst_path) {
4439 ret = -ENOMEM;
4440 goto out;
4441 }
4442 }
4443 }
Olivier Deprez157378f2022-04-04 15:47:50 +02004444 if (ins_nr > 0)
Olivier Deprez0e641232021-09-23 10:07:05 +02004445 ret = copy_items(trans, inode, dst_path, path,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004446 start_slot, ins_nr, 1, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004447out:
4448 btrfs_release_path(path);
4449 btrfs_free_path(dst_path);
4450 return ret;
4451}
4452
4453static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4454 struct btrfs_root *root,
4455 struct btrfs_inode *inode,
4456 struct btrfs_path *path,
Olivier Deprez157378f2022-04-04 15:47:50 +02004457 struct btrfs_log_ctx *ctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004458{
Olivier Deprez157378f2022-04-04 15:47:50 +02004459 struct btrfs_ordered_extent *ordered;
4460 struct btrfs_ordered_extent *tmp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004461 struct extent_map *em, *n;
4462 struct list_head extents;
4463 struct extent_map_tree *tree = &inode->extent_tree;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004464 u64 test_gen;
4465 int ret = 0;
4466 int num = 0;
4467
4468 INIT_LIST_HEAD(&extents);
4469
4470 write_lock(&tree->lock);
4471 test_gen = root->fs_info->last_trans_committed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004472
4473 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004474 list_del_init(&em->list);
4475 /*
4476 * Just an arbitrary number, this can be really CPU intensive
4477 * once we start getting a lot of extents, and really once we
4478 * have a bunch of extents we just want to commit since it will
4479 * be faster.
4480 */
4481 if (++num > 32768) {
4482 list_del_init(&tree->modified_extents);
4483 ret = -EFBIG;
4484 goto process;
4485 }
4486
4487 if (em->generation <= test_gen)
4488 continue;
4489
4490 /* We log prealloc extents beyond eof later. */
4491 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4492 em->start >= i_size_read(&inode->vfs_inode))
4493 continue;
4494
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004495 /* Need a ref to keep it from getting evicted from cache */
4496 refcount_inc(&em->refs);
4497 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4498 list_add_tail(&em->list, &extents);
4499 num++;
4500 }
4501
4502 list_sort(NULL, &extents, extent_cmp);
4503process:
4504 while (!list_empty(&extents)) {
4505 em = list_entry(extents.next, struct extent_map, list);
4506
4507 list_del_init(&em->list);
4508
4509 /*
4510 * If we had an error we just need to delete everybody from our
4511 * private list.
4512 */
4513 if (ret) {
4514 clear_em_logging(tree, em);
4515 free_extent_map(em);
4516 continue;
4517 }
4518
4519 write_unlock(&tree->lock);
4520
4521 ret = log_one_extent(trans, inode, root, em, path, ctx);
4522 write_lock(&tree->lock);
4523 clear_em_logging(tree, em);
4524 free_extent_map(em);
4525 }
4526 WARN_ON(!list_empty(&extents));
4527 write_unlock(&tree->lock);
4528
4529 btrfs_release_path(path);
4530 if (!ret)
4531 ret = btrfs_log_prealloc_extents(trans, inode, path);
Olivier Deprez157378f2022-04-04 15:47:50 +02004532 if (ret)
4533 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004534
Olivier Deprez157378f2022-04-04 15:47:50 +02004535 /*
4536 * We have logged all extents successfully, now make sure the commit of
4537 * the current transaction waits for the ordered extents to complete
4538 * before it commits and wipes out the log trees, otherwise we would
4539 * lose data if an ordered extents completes after the transaction
4540 * commits and a power failure happens after the transaction commit.
4541 */
4542 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
4543 list_del_init(&ordered->log_list);
4544 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4545
4546 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4547 spin_lock_irq(&inode->ordered_tree.lock);
4548 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4549 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4550 atomic_inc(&trans->transaction->pending_ordered);
4551 }
4552 spin_unlock_irq(&inode->ordered_tree.lock);
4553 }
4554 btrfs_put_ordered_extent(ordered);
4555 }
4556
4557 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004558}
4559
4560static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
4561 struct btrfs_path *path, u64 *size_ret)
4562{
4563 struct btrfs_key key;
4564 int ret;
4565
4566 key.objectid = btrfs_ino(inode);
4567 key.type = BTRFS_INODE_ITEM_KEY;
4568 key.offset = 0;
4569
4570 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4571 if (ret < 0) {
4572 return ret;
4573 } else if (ret > 0) {
4574 *size_ret = 0;
4575 } else {
4576 struct btrfs_inode_item *item;
4577
4578 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4579 struct btrfs_inode_item);
4580 *size_ret = btrfs_inode_size(path->nodes[0], item);
David Brazdil0f672f62019-12-10 10:32:29 +00004581 /*
4582 * If the in-memory inode's i_size is smaller then the inode
4583 * size stored in the btree, return the inode's i_size, so
4584 * that we get a correct inode size after replaying the log
4585 * when before a power failure we had a shrinking truncate
4586 * followed by addition of a new name (rename / new hard link).
4587 * Otherwise return the inode size from the btree, to avoid
4588 * data loss when replaying a log due to previously doing a
4589 * write that expands the inode's size and logging a new name
4590 * immediately after.
4591 */
4592 if (*size_ret > inode->vfs_inode.i_size)
4593 *size_ret = inode->vfs_inode.i_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004594 }
4595
4596 btrfs_release_path(path);
4597 return 0;
4598}
4599
4600/*
4601 * At the moment we always log all xattrs. This is to figure out at log replay
4602 * time which xattrs must have their deletion replayed. If a xattr is missing
4603 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4604 * because if a xattr is deleted, the inode is fsynced and a power failure
4605 * happens, causing the log to be replayed the next time the fs is mounted,
4606 * we want the xattr to not exist anymore (same behaviour as other filesystems
4607 * with a journal, ext3/4, xfs, f2fs, etc).
4608 */
4609static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4610 struct btrfs_root *root,
4611 struct btrfs_inode *inode,
4612 struct btrfs_path *path,
4613 struct btrfs_path *dst_path)
4614{
4615 int ret;
4616 struct btrfs_key key;
4617 const u64 ino = btrfs_ino(inode);
4618 int ins_nr = 0;
4619 int start_slot = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02004620 bool found_xattrs = false;
4621
4622 if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
4623 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004624
4625 key.objectid = ino;
4626 key.type = BTRFS_XATTR_ITEM_KEY;
4627 key.offset = 0;
4628
4629 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4630 if (ret < 0)
4631 return ret;
4632
4633 while (true) {
4634 int slot = path->slots[0];
4635 struct extent_buffer *leaf = path->nodes[0];
4636 int nritems = btrfs_header_nritems(leaf);
4637
4638 if (slot >= nritems) {
4639 if (ins_nr > 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004640 ret = copy_items(trans, inode, dst_path, path,
Olivier Deprez0e641232021-09-23 10:07:05 +02004641 start_slot, ins_nr, 1, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004642 if (ret < 0)
4643 return ret;
4644 ins_nr = 0;
4645 }
4646 ret = btrfs_next_leaf(root, path);
4647 if (ret < 0)
4648 return ret;
4649 else if (ret > 0)
4650 break;
4651 continue;
4652 }
4653
4654 btrfs_item_key_to_cpu(leaf, &key, slot);
4655 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4656 break;
4657
4658 if (ins_nr == 0)
4659 start_slot = slot;
4660 ins_nr++;
4661 path->slots[0]++;
Olivier Deprez157378f2022-04-04 15:47:50 +02004662 found_xattrs = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004663 cond_resched();
4664 }
4665 if (ins_nr > 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004666 ret = copy_items(trans, inode, dst_path, path,
Olivier Deprez0e641232021-09-23 10:07:05 +02004667 start_slot, ins_nr, 1, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004668 if (ret < 0)
4669 return ret;
4670 }
4671
Olivier Deprez157378f2022-04-04 15:47:50 +02004672 if (!found_xattrs)
4673 set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
4674
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004675 return 0;
4676}
4677
4678/*
Olivier Deprez0e641232021-09-23 10:07:05 +02004679 * When using the NO_HOLES feature if we punched a hole that causes the
4680 * deletion of entire leafs or all the extent items of the first leaf (the one
4681 * that contains the inode item and references) we may end up not processing
4682 * any extents, because there are no leafs with a generation matching the
4683 * current transaction that have extent items for our inode. So we need to find
4684 * if any holes exist and then log them. We also need to log holes after any
4685 * truncate operation that changes the inode's size.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004686 */
Olivier Deprez0e641232021-09-23 10:07:05 +02004687static int btrfs_log_holes(struct btrfs_trans_handle *trans,
4688 struct btrfs_root *root,
4689 struct btrfs_inode *inode,
4690 struct btrfs_path *path)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004691{
4692 struct btrfs_fs_info *fs_info = root->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004693 struct btrfs_key key;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004694 const u64 ino = btrfs_ino(inode);
4695 const u64 i_size = i_size_read(&inode->vfs_inode);
Olivier Deprez0e641232021-09-23 10:07:05 +02004696 u64 prev_extent_end = 0;
4697 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004698
Olivier Deprez0e641232021-09-23 10:07:05 +02004699 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004700 return 0;
4701
4702 key.objectid = ino;
4703 key.type = BTRFS_EXTENT_DATA_KEY;
Olivier Deprez0e641232021-09-23 10:07:05 +02004704 key.offset = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004705
4706 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004707 if (ret < 0)
4708 return ret;
4709
Olivier Deprez0e641232021-09-23 10:07:05 +02004710 while (true) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004711 struct extent_buffer *leaf = path->nodes[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004712
Olivier Deprez0e641232021-09-23 10:07:05 +02004713 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4714 ret = btrfs_next_leaf(root, path);
4715 if (ret < 0)
4716 return ret;
4717 if (ret > 0) {
4718 ret = 0;
4719 break;
4720 }
4721 leaf = path->nodes[0];
4722 }
4723
4724 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4725 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
4726 break;
4727
4728 /* We have a hole, log it. */
4729 if (prev_extent_end < key.offset) {
4730 const u64 hole_len = key.offset - prev_extent_end;
4731
4732 /*
4733 * Release the path to avoid deadlocks with other code
4734 * paths that search the root while holding locks on
4735 * leafs from the log root.
4736 */
4737 btrfs_release_path(path);
4738 ret = btrfs_insert_file_extent(trans, root->log_root,
4739 ino, prev_extent_end, 0,
4740 0, hole_len, 0, hole_len,
4741 0, 0, 0);
4742 if (ret < 0)
4743 return ret;
4744
4745 /*
4746 * Search for the same key again in the root. Since it's
4747 * an extent item and we are holding the inode lock, the
4748 * key must still exist. If it doesn't just emit warning
4749 * and return an error to fall back to a transaction
4750 * commit.
4751 */
4752 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4753 if (ret < 0)
4754 return ret;
4755 if (WARN_ON(ret > 0))
4756 return -ENOENT;
4757 leaf = path->nodes[0];
4758 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004759
Olivier Deprez157378f2022-04-04 15:47:50 +02004760 prev_extent_end = btrfs_file_extent_end(path);
Olivier Deprez0e641232021-09-23 10:07:05 +02004761 path->slots[0]++;
4762 cond_resched();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004763 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004764
Olivier Deprez0e641232021-09-23 10:07:05 +02004765 if (prev_extent_end < i_size) {
4766 u64 hole_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004767
Olivier Deprez0e641232021-09-23 10:07:05 +02004768 btrfs_release_path(path);
4769 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
4770 ret = btrfs_insert_file_extent(trans, root->log_root,
4771 ino, prev_extent_end, 0, 0,
4772 hole_len, 0, hole_len,
4773 0, 0, 0);
4774 if (ret < 0)
4775 return ret;
4776 }
4777
4778 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004779}
4780
4781/*
4782 * When we are logging a new inode X, check if it doesn't have a reference that
4783 * matches the reference from some other inode Y created in a past transaction
4784 * and that was renamed in the current transaction. If we don't do this, then at
4785 * log replay time we can lose inode Y (and all its files if it's a directory):
4786 *
4787 * mkdir /mnt/x
4788 * echo "hello world" > /mnt/x/foobar
4789 * sync
4790 * mv /mnt/x /mnt/y
4791 * mkdir /mnt/x # or touch /mnt/x
4792 * xfs_io -c fsync /mnt/x
4793 * <power fail>
4794 * mount fs, trigger log replay
4795 *
4796 * After the log replay procedure, we would lose the first directory and all its
4797 * files (file foobar).
4798 * For the case where inode Y is not a directory we simply end up losing it:
4799 *
4800 * echo "123" > /mnt/foo
4801 * sync
4802 * mv /mnt/foo /mnt/bar
4803 * echo "abc" > /mnt/foo
4804 * xfs_io -c fsync /mnt/foo
4805 * <power fail>
4806 *
4807 * We also need this for cases where a snapshot entry is replaced by some other
4808 * entry (file or directory) otherwise we end up with an unreplayable log due to
4809 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4810 * if it were a regular entry:
4811 *
4812 * mkdir /mnt/x
4813 * btrfs subvolume snapshot /mnt /mnt/x/snap
4814 * btrfs subvolume delete /mnt/x/snap
4815 * rmdir /mnt/x
4816 * mkdir /mnt/x
4817 * fsync /mnt/x or fsync some new file inside it
4818 * <power fail>
4819 *
4820 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4821 * the same transaction.
4822 */
4823static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4824 const int slot,
4825 const struct btrfs_key *key,
4826 struct btrfs_inode *inode,
David Brazdil0f672f62019-12-10 10:32:29 +00004827 u64 *other_ino, u64 *other_parent)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004828{
4829 int ret;
4830 struct btrfs_path *search_path;
4831 char *name = NULL;
4832 u32 name_len = 0;
4833 u32 item_size = btrfs_item_size_nr(eb, slot);
4834 u32 cur_offset = 0;
4835 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4836
4837 search_path = btrfs_alloc_path();
4838 if (!search_path)
4839 return -ENOMEM;
4840 search_path->search_commit_root = 1;
4841 search_path->skip_locking = 1;
4842
4843 while (cur_offset < item_size) {
4844 u64 parent;
4845 u32 this_name_len;
4846 u32 this_len;
4847 unsigned long name_ptr;
4848 struct btrfs_dir_item *di;
4849
4850 if (key->type == BTRFS_INODE_REF_KEY) {
4851 struct btrfs_inode_ref *iref;
4852
4853 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4854 parent = key->offset;
4855 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4856 name_ptr = (unsigned long)(iref + 1);
4857 this_len = sizeof(*iref) + this_name_len;
4858 } else {
4859 struct btrfs_inode_extref *extref;
4860
4861 extref = (struct btrfs_inode_extref *)(ptr +
4862 cur_offset);
4863 parent = btrfs_inode_extref_parent(eb, extref);
4864 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4865 name_ptr = (unsigned long)&extref->name;
4866 this_len = sizeof(*extref) + this_name_len;
4867 }
4868
4869 if (this_name_len > name_len) {
4870 char *new_name;
4871
4872 new_name = krealloc(name, this_name_len, GFP_NOFS);
4873 if (!new_name) {
4874 ret = -ENOMEM;
4875 goto out;
4876 }
4877 name_len = this_name_len;
4878 name = new_name;
4879 }
4880
4881 read_extent_buffer(eb, name, name_ptr, this_name_len);
4882 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4883 parent, name, this_name_len, 0);
4884 if (di && !IS_ERR(di)) {
4885 struct btrfs_key di_key;
4886
4887 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4888 di, &di_key);
4889 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
David Brazdil0f672f62019-12-10 10:32:29 +00004890 if (di_key.objectid != key->objectid) {
4891 ret = 1;
4892 *other_ino = di_key.objectid;
4893 *other_parent = parent;
4894 } else {
4895 ret = 0;
4896 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004897 } else {
4898 ret = -EAGAIN;
4899 }
4900 goto out;
4901 } else if (IS_ERR(di)) {
4902 ret = PTR_ERR(di);
4903 goto out;
4904 }
4905 btrfs_release_path(search_path);
4906
4907 cur_offset += this_len;
4908 }
4909 ret = 0;
4910out:
4911 btrfs_free_path(search_path);
4912 kfree(name);
4913 return ret;
4914}
4915
David Brazdil0f672f62019-12-10 10:32:29 +00004916struct btrfs_ino_list {
4917 u64 ino;
4918 u64 parent;
4919 struct list_head list;
4920};
4921
4922static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
4923 struct btrfs_root *root,
4924 struct btrfs_path *path,
4925 struct btrfs_log_ctx *ctx,
4926 u64 ino, u64 parent)
4927{
4928 struct btrfs_ino_list *ino_elem;
4929 LIST_HEAD(inode_list);
4930 int ret = 0;
4931
4932 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
4933 if (!ino_elem)
4934 return -ENOMEM;
4935 ino_elem->ino = ino;
4936 ino_elem->parent = parent;
4937 list_add_tail(&ino_elem->list, &inode_list);
4938
4939 while (!list_empty(&inode_list)) {
4940 struct btrfs_fs_info *fs_info = root->fs_info;
4941 struct btrfs_key key;
4942 struct inode *inode;
4943
4944 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
4945 list);
4946 ino = ino_elem->ino;
4947 parent = ino_elem->parent;
4948 list_del(&ino_elem->list);
4949 kfree(ino_elem);
4950 if (ret)
4951 continue;
4952
4953 btrfs_release_path(path);
4954
Olivier Deprez157378f2022-04-04 15:47:50 +02004955 inode = btrfs_iget(fs_info->sb, ino, root);
David Brazdil0f672f62019-12-10 10:32:29 +00004956 /*
4957 * If the other inode that had a conflicting dir entry was
4958 * deleted in the current transaction, we need to log its parent
4959 * directory.
4960 */
4961 if (IS_ERR(inode)) {
4962 ret = PTR_ERR(inode);
4963 if (ret == -ENOENT) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004964 inode = btrfs_iget(fs_info->sb, parent, root);
David Brazdil0f672f62019-12-10 10:32:29 +00004965 if (IS_ERR(inode)) {
4966 ret = PTR_ERR(inode);
4967 } else {
4968 ret = btrfs_log_inode(trans, root,
4969 BTRFS_I(inode),
4970 LOG_OTHER_INODE_ALL,
Olivier Deprez157378f2022-04-04 15:47:50 +02004971 ctx);
David Brazdil0f672f62019-12-10 10:32:29 +00004972 btrfs_add_delayed_iput(inode);
4973 }
4974 }
4975 continue;
4976 }
4977 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02004978 * If the inode was already logged skip it - otherwise we can
4979 * hit an infinite loop. Example:
4980 *
4981 * From the commit root (previous transaction) we have the
4982 * following inodes:
4983 *
4984 * inode 257 a directory
4985 * inode 258 with references "zz" and "zz_link" on inode 257
4986 * inode 259 with reference "a" on inode 257
4987 *
4988 * And in the current (uncommitted) transaction we have:
4989 *
4990 * inode 257 a directory, unchanged
4991 * inode 258 with references "a" and "a2" on inode 257
4992 * inode 259 with reference "zz_link" on inode 257
4993 * inode 261 with reference "zz" on inode 257
4994 *
4995 * When logging inode 261 the following infinite loop could
4996 * happen if we don't skip already logged inodes:
4997 *
4998 * - we detect inode 258 as a conflicting inode, with inode 261
4999 * on reference "zz", and log it;
5000 *
5001 * - we detect inode 259 as a conflicting inode, with inode 258
5002 * on reference "a", and log it;
5003 *
5004 * - we detect inode 258 as a conflicting inode, with inode 259
5005 * on reference "zz_link", and log it - again! After this we
5006 * repeat the above steps forever.
5007 */
5008 spin_lock(&BTRFS_I(inode)->lock);
5009 /*
5010 * Check the inode's logged_trans only instead of
5011 * btrfs_inode_in_log(). This is because the last_log_commit of
5012 * the inode is not updated when we only log that it exists and
5013 * it has the full sync bit set (see btrfs_log_inode()).
5014 */
5015 if (BTRFS_I(inode)->logged_trans == trans->transid) {
5016 spin_unlock(&BTRFS_I(inode)->lock);
5017 btrfs_add_delayed_iput(inode);
5018 continue;
5019 }
5020 spin_unlock(&BTRFS_I(inode)->lock);
5021 /*
David Brazdil0f672f62019-12-10 10:32:29 +00005022 * We are safe logging the other inode without acquiring its
5023 * lock as long as we log with the LOG_INODE_EXISTS mode. We
5024 * are safe against concurrent renames of the other inode as
5025 * well because during a rename we pin the log and update the
5026 * log with the new name before we unpin it.
5027 */
5028 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
Olivier Deprez157378f2022-04-04 15:47:50 +02005029 LOG_OTHER_INODE, ctx);
David Brazdil0f672f62019-12-10 10:32:29 +00005030 if (ret) {
5031 btrfs_add_delayed_iput(inode);
5032 continue;
5033 }
5034
5035 key.objectid = ino;
5036 key.type = BTRFS_INODE_REF_KEY;
5037 key.offset = 0;
5038 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5039 if (ret < 0) {
5040 btrfs_add_delayed_iput(inode);
5041 continue;
5042 }
5043
5044 while (true) {
5045 struct extent_buffer *leaf = path->nodes[0];
5046 int slot = path->slots[0];
5047 u64 other_ino = 0;
5048 u64 other_parent = 0;
5049
5050 if (slot >= btrfs_header_nritems(leaf)) {
5051 ret = btrfs_next_leaf(root, path);
5052 if (ret < 0) {
5053 break;
5054 } else if (ret > 0) {
5055 ret = 0;
5056 break;
5057 }
5058 continue;
5059 }
5060
5061 btrfs_item_key_to_cpu(leaf, &key, slot);
5062 if (key.objectid != ino ||
5063 (key.type != BTRFS_INODE_REF_KEY &&
5064 key.type != BTRFS_INODE_EXTREF_KEY)) {
5065 ret = 0;
5066 break;
5067 }
5068
5069 ret = btrfs_check_ref_name_override(leaf, slot, &key,
5070 BTRFS_I(inode), &other_ino,
5071 &other_parent);
5072 if (ret < 0)
5073 break;
5074 if (ret > 0) {
5075 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5076 if (!ino_elem) {
5077 ret = -ENOMEM;
5078 break;
5079 }
5080 ino_elem->ino = other_ino;
5081 ino_elem->parent = other_parent;
5082 list_add_tail(&ino_elem->list, &inode_list);
5083 ret = 0;
5084 }
5085 path->slots[0]++;
5086 }
5087 btrfs_add_delayed_iput(inode);
5088 }
5089
5090 return ret;
5091}
5092
Olivier Deprez0e641232021-09-23 10:07:05 +02005093static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5094 struct btrfs_inode *inode,
5095 struct btrfs_key *min_key,
5096 const struct btrfs_key *max_key,
5097 struct btrfs_path *path,
5098 struct btrfs_path *dst_path,
5099 const u64 logged_isize,
5100 const bool recursive_logging,
5101 const int inode_only,
5102 struct btrfs_log_ctx *ctx,
5103 bool *need_log_inode_item)
5104{
Olivier Deprez157378f2022-04-04 15:47:50 +02005105 const u64 i_size = i_size_read(&inode->vfs_inode);
Olivier Deprez0e641232021-09-23 10:07:05 +02005106 struct btrfs_root *root = inode->root;
5107 int ins_start_slot = 0;
5108 int ins_nr = 0;
5109 int ret;
5110
5111 while (1) {
5112 ret = btrfs_search_forward(root, min_key, path, trans->transid);
5113 if (ret < 0)
5114 return ret;
5115 if (ret > 0) {
5116 ret = 0;
5117 break;
5118 }
5119again:
5120 /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5121 if (min_key->objectid != max_key->objectid)
5122 break;
5123 if (min_key->type > max_key->type)
5124 break;
5125
Olivier Deprez157378f2022-04-04 15:47:50 +02005126 if (min_key->type == BTRFS_INODE_ITEM_KEY) {
Olivier Deprez0e641232021-09-23 10:07:05 +02005127 *need_log_inode_item = false;
Olivier Deprez157378f2022-04-04 15:47:50 +02005128 } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
5129 min_key->offset >= i_size) {
5130 /*
5131 * Extents at and beyond eof are logged with
5132 * btrfs_log_prealloc_extents().
5133 * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
5134 * and no keys greater than that, so bail out.
5135 */
5136 break;
5137 } else if ((min_key->type == BTRFS_INODE_REF_KEY ||
5138 min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5139 inode->generation == trans->transid &&
5140 !recursive_logging) {
Olivier Deprez0e641232021-09-23 10:07:05 +02005141 u64 other_ino = 0;
5142 u64 other_parent = 0;
5143
5144 ret = btrfs_check_ref_name_override(path->nodes[0],
5145 path->slots[0], min_key, inode,
5146 &other_ino, &other_parent);
5147 if (ret < 0) {
5148 return ret;
5149 } else if (ret > 0 && ctx &&
5150 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5151 if (ins_nr > 0) {
5152 ins_nr++;
5153 } else {
5154 ins_nr = 1;
5155 ins_start_slot = path->slots[0];
5156 }
5157 ret = copy_items(trans, inode, dst_path, path,
5158 ins_start_slot, ins_nr,
5159 inode_only, logged_isize);
5160 if (ret < 0)
5161 return ret;
5162 ins_nr = 0;
5163
5164 ret = log_conflicting_inodes(trans, root, path,
5165 ctx, other_ino, other_parent);
5166 if (ret)
5167 return ret;
5168 btrfs_release_path(path);
5169 goto next_key;
5170 }
Olivier Deprez157378f2022-04-04 15:47:50 +02005171 } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5172 /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
Olivier Deprez0e641232021-09-23 10:07:05 +02005173 if (ins_nr == 0)
5174 goto next_slot;
5175 ret = copy_items(trans, inode, dst_path, path,
5176 ins_start_slot,
5177 ins_nr, inode_only, logged_isize);
5178 if (ret < 0)
5179 return ret;
5180 ins_nr = 0;
5181 goto next_slot;
5182 }
5183
5184 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5185 ins_nr++;
5186 goto next_slot;
5187 } else if (!ins_nr) {
5188 ins_start_slot = path->slots[0];
5189 ins_nr = 1;
5190 goto next_slot;
5191 }
5192
5193 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5194 ins_nr, inode_only, logged_isize);
5195 if (ret < 0)
5196 return ret;
5197 ins_nr = 1;
5198 ins_start_slot = path->slots[0];
5199next_slot:
5200 path->slots[0]++;
5201 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5202 btrfs_item_key_to_cpu(path->nodes[0], min_key,
5203 path->slots[0]);
5204 goto again;
5205 }
5206 if (ins_nr) {
5207 ret = copy_items(trans, inode, dst_path, path,
5208 ins_start_slot, ins_nr, inode_only,
5209 logged_isize);
5210 if (ret < 0)
5211 return ret;
5212 ins_nr = 0;
5213 }
5214 btrfs_release_path(path);
5215next_key:
5216 if (min_key->offset < (u64)-1) {
5217 min_key->offset++;
5218 } else if (min_key->type < max_key->type) {
5219 min_key->type++;
5220 min_key->offset = 0;
5221 } else {
5222 break;
5223 }
5224 }
Olivier Deprez157378f2022-04-04 15:47:50 +02005225 if (ins_nr) {
Olivier Deprez0e641232021-09-23 10:07:05 +02005226 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5227 ins_nr, inode_only, logged_isize);
Olivier Deprez157378f2022-04-04 15:47:50 +02005228 if (ret)
5229 return ret;
5230 }
5231
5232 if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
5233 /*
5234 * Release the path because otherwise we might attempt to double
5235 * lock the same leaf with btrfs_log_prealloc_extents() below.
5236 */
5237 btrfs_release_path(path);
5238 ret = btrfs_log_prealloc_extents(trans, inode, dst_path);
5239 }
Olivier Deprez0e641232021-09-23 10:07:05 +02005240
5241 return ret;
5242}
5243
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005244/* log a single inode in the tree log.
5245 * At least one parent directory for this inode must exist in the tree
5246 * or be logged already.
5247 *
5248 * Any items from this inode changed by the current transaction are copied
5249 * to the log tree. An extra reference is taken on any extents in this
5250 * file, allowing us to avoid a whole pile of corner cases around logging
5251 * blocks that have been removed from the tree.
5252 *
5253 * See LOG_INODE_ALL and related defines for a description of what inode_only
5254 * does.
5255 *
5256 * This handles both files and directories.
5257 */
5258static int btrfs_log_inode(struct btrfs_trans_handle *trans,
5259 struct btrfs_root *root, struct btrfs_inode *inode,
5260 int inode_only,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005261 struct btrfs_log_ctx *ctx)
5262{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005263 struct btrfs_path *path;
5264 struct btrfs_path *dst_path;
5265 struct btrfs_key min_key;
5266 struct btrfs_key max_key;
5267 struct btrfs_root *log = root->log_root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005268 int err = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02005269 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005270 bool fast_search = false;
5271 u64 ino = btrfs_ino(inode);
5272 struct extent_map_tree *em_tree = &inode->extent_tree;
5273 u64 logged_isize = 0;
5274 bool need_log_inode_item = true;
5275 bool xattrs_logged = false;
David Brazdil0f672f62019-12-10 10:32:29 +00005276 bool recursive_logging = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005277
5278 path = btrfs_alloc_path();
5279 if (!path)
5280 return -ENOMEM;
5281 dst_path = btrfs_alloc_path();
5282 if (!dst_path) {
5283 btrfs_free_path(path);
5284 return -ENOMEM;
5285 }
5286
5287 min_key.objectid = ino;
5288 min_key.type = BTRFS_INODE_ITEM_KEY;
5289 min_key.offset = 0;
5290
5291 max_key.objectid = ino;
5292
5293
5294 /* today the code can only do partial logging of directories */
5295 if (S_ISDIR(inode->vfs_inode.i_mode) ||
5296 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5297 &inode->runtime_flags) &&
5298 inode_only >= LOG_INODE_EXISTS))
5299 max_key.type = BTRFS_XATTR_ITEM_KEY;
5300 else
5301 max_key.type = (u8)-1;
5302 max_key.offset = (u64)-1;
5303
5304 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02005305 * Only run delayed items if we are a directory. We want to make sure
5306 * all directory indexes hit the fs/subvolume tree so we can find them
5307 * and figure out which index ranges have to be logged.
5308 *
5309 * Otherwise commit the delayed inode only if the full sync flag is set,
5310 * as we want to make sure an up to date version is in the subvolume
5311 * tree so copy_inode_items_to_log() / copy_items() can find it and copy
5312 * it to the log tree. For a non full sync, we always log the inode item
5313 * based on the in-memory struct btrfs_inode which is always up to date.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005314 */
Olivier Deprez0e641232021-09-23 10:07:05 +02005315 if (S_ISDIR(inode->vfs_inode.i_mode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005316 ret = btrfs_commit_inode_delayed_items(trans, inode);
Olivier Deprez0e641232021-09-23 10:07:05 +02005317 else if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005318 ret = btrfs_commit_inode_delayed_inode(inode);
5319
5320 if (ret) {
5321 btrfs_free_path(path);
5322 btrfs_free_path(dst_path);
5323 return ret;
5324 }
5325
David Brazdil0f672f62019-12-10 10:32:29 +00005326 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5327 recursive_logging = true;
5328 if (inode_only == LOG_OTHER_INODE)
5329 inode_only = LOG_INODE_EXISTS;
5330 else
5331 inode_only = LOG_INODE_ALL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005332 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
5333 } else {
5334 mutex_lock(&inode->log_mutex);
5335 }
5336
5337 /*
5338 * a brute force approach to making sure we get the most uptodate
5339 * copies of everything.
5340 */
5341 if (S_ISDIR(inode->vfs_inode.i_mode)) {
5342 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5343
5344 if (inode_only == LOG_INODE_EXISTS)
5345 max_key_type = BTRFS_XATTR_ITEM_KEY;
5346 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
5347 } else {
5348 if (inode_only == LOG_INODE_EXISTS) {
5349 /*
5350 * Make sure the new inode item we write to the log has
5351 * the same isize as the current one (if it exists).
5352 * This is necessary to prevent data loss after log
5353 * replay, and also to prevent doing a wrong expanding
5354 * truncate - for e.g. create file, write 4K into offset
5355 * 0, fsync, write 4K into offset 4096, add hard link,
5356 * fsync some other file (to sync log), power fail - if
5357 * we use the inode's current i_size, after log replay
5358 * we get a 8Kb file, with the last 4Kb extent as a hole
5359 * (zeroes), as if an expanding truncate happened,
5360 * instead of getting a file of 4Kb only.
5361 */
5362 err = logged_inode_size(log, inode, path, &logged_isize);
5363 if (err)
5364 goto out_unlock;
5365 }
5366 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5367 &inode->runtime_flags)) {
5368 if (inode_only == LOG_INODE_EXISTS) {
5369 max_key.type = BTRFS_XATTR_ITEM_KEY;
5370 ret = drop_objectid_items(trans, log, path, ino,
5371 max_key.type);
5372 } else {
5373 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5374 &inode->runtime_flags);
5375 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5376 &inode->runtime_flags);
5377 while(1) {
5378 ret = btrfs_truncate_inode_items(trans,
5379 log, &inode->vfs_inode, 0, 0);
5380 if (ret != -EAGAIN)
5381 break;
5382 }
5383 }
5384 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5385 &inode->runtime_flags) ||
5386 inode_only == LOG_INODE_EXISTS) {
5387 if (inode_only == LOG_INODE_ALL)
5388 fast_search = true;
5389 max_key.type = BTRFS_XATTR_ITEM_KEY;
5390 ret = drop_objectid_items(trans, log, path, ino,
5391 max_key.type);
5392 } else {
5393 if (inode_only == LOG_INODE_ALL)
5394 fast_search = true;
5395 goto log_extents;
5396 }
5397
5398 }
5399 if (ret) {
5400 err = ret;
5401 goto out_unlock;
5402 }
5403
Olivier Deprez0e641232021-09-23 10:07:05 +02005404 err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
5405 path, dst_path, logged_isize,
5406 recursive_logging, inode_only, ctx,
5407 &need_log_inode_item);
5408 if (err)
5409 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005410
5411 btrfs_release_path(path);
5412 btrfs_release_path(dst_path);
5413 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
5414 if (err)
5415 goto out_unlock;
5416 xattrs_logged = true;
5417 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5418 btrfs_release_path(path);
5419 btrfs_release_path(dst_path);
Olivier Deprez0e641232021-09-23 10:07:05 +02005420 err = btrfs_log_holes(trans, root, inode, path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005421 if (err)
5422 goto out_unlock;
5423 }
5424log_extents:
5425 btrfs_release_path(path);
5426 btrfs_release_path(dst_path);
5427 if (need_log_inode_item) {
5428 err = log_inode_item(trans, log, dst_path, inode);
5429 if (!err && !xattrs_logged) {
5430 err = btrfs_log_all_xattrs(trans, root, inode, path,
5431 dst_path);
5432 btrfs_release_path(path);
5433 }
5434 if (err)
5435 goto out_unlock;
5436 }
5437 if (fast_search) {
5438 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Olivier Deprez157378f2022-04-04 15:47:50 +02005439 ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005440 if (ret) {
5441 err = ret;
5442 goto out_unlock;
5443 }
5444 } else if (inode_only == LOG_INODE_ALL) {
5445 struct extent_map *em, *n;
5446
5447 write_lock(&em_tree->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02005448 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
5449 list_del_init(&em->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005450 write_unlock(&em_tree->lock);
5451 }
5452
5453 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5454 ret = log_directory_changes(trans, root, inode, path, dst_path,
5455 ctx);
5456 if (ret) {
5457 err = ret;
5458 goto out_unlock;
5459 }
5460 }
5461
David Brazdil0f672f62019-12-10 10:32:29 +00005462 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02005463 * If we are logging that an ancestor inode exists as part of logging a
5464 * new name from a link or rename operation, don't mark the inode as
5465 * logged - otherwise if an explicit fsync is made against an ancestor,
5466 * the fsync considers the inode in the log and doesn't sync the log,
5467 * resulting in the ancestor missing after a power failure unless the
5468 * log was synced as part of an fsync against any other unrelated inode.
5469 * So keep it simple for this case and just don't flag the ancestors as
5470 * logged.
David Brazdil0f672f62019-12-10 10:32:29 +00005471 */
Olivier Deprez0e641232021-09-23 10:07:05 +02005472 if (!ctx ||
5473 !(S_ISDIR(inode->vfs_inode.i_mode) && ctx->logging_new_name &&
5474 &inode->vfs_inode != ctx->inode)) {
5475 spin_lock(&inode->lock);
5476 inode->logged_trans = trans->transid;
5477 /*
5478 * Don't update last_log_commit if we logged that an inode exists
5479 * after it was loaded to memory (full_sync bit set).
5480 * This is to prevent data loss when we do a write to the inode,
5481 * then the inode gets evicted after all delalloc was flushed,
5482 * then we log it exists (due to a rename for example) and then
5483 * fsync it. This last fsync would do nothing (not logging the
5484 * extents previously written).
5485 */
5486 if (inode_only != LOG_INODE_EXISTS ||
5487 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5488 inode->last_log_commit = inode->last_sub_trans;
5489 spin_unlock(&inode->lock);
5490 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005491out_unlock:
5492 mutex_unlock(&inode->log_mutex);
5493
5494 btrfs_free_path(path);
5495 btrfs_free_path(dst_path);
5496 return err;
5497}
5498
5499/*
5500 * Check if we must fallback to a transaction commit when logging an inode.
5501 * This must be called after logging the inode and is used only in the context
5502 * when fsyncing an inode requires the need to log some other inode - in which
5503 * case we can't lock the i_mutex of each other inode we need to log as that
5504 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5505 * log inodes up or down in the hierarchy) or rename operations for example. So
5506 * we take the log_mutex of the inode after we have logged it and then check for
5507 * its last_unlink_trans value - this is safe because any task setting
5508 * last_unlink_trans must take the log_mutex and it must do this before it does
5509 * the actual unlink operation, so if we do this check before a concurrent task
5510 * sets last_unlink_trans it means we've logged a consistent version/state of
5511 * all the inode items, otherwise we are not sure and must do a transaction
5512 * commit (the concurrent task might have only updated last_unlink_trans before
5513 * we logged the inode or it might have also done the unlink).
5514 */
5515static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5516 struct btrfs_inode *inode)
5517{
5518 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5519 bool ret = false;
5520
5521 mutex_lock(&inode->log_mutex);
5522 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
5523 /*
5524 * Make sure any commits to the log are forced to be full
5525 * commits.
5526 */
David Brazdil0f672f62019-12-10 10:32:29 +00005527 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005528 ret = true;
5529 }
5530 mutex_unlock(&inode->log_mutex);
5531
5532 return ret;
5533}
5534
5535/*
5536 * follow the dentry parent pointers up the chain and see if any
5537 * of the directories in it require a full commit before they can
5538 * be logged. Returns zero if nothing special needs to be done or 1 if
5539 * a full commit is required.
5540 */
5541static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5542 struct btrfs_inode *inode,
5543 struct dentry *parent,
5544 struct super_block *sb,
5545 u64 last_committed)
5546{
5547 int ret = 0;
5548 struct dentry *old_parent = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005549
5550 /*
5551 * for regular files, if its inode is already on disk, we don't
5552 * have to worry about the parents at all. This is because
5553 * we can use the last_unlink_trans field to record renames
5554 * and other fun in this file.
5555 */
5556 if (S_ISREG(inode->vfs_inode.i_mode) &&
5557 inode->generation <= last_committed &&
5558 inode->last_unlink_trans <= last_committed)
5559 goto out;
5560
5561 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
5562 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5563 goto out;
5564 inode = BTRFS_I(d_inode(parent));
5565 }
5566
5567 while (1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005568 if (btrfs_must_commit_transaction(trans, inode)) {
5569 ret = 1;
5570 break;
5571 }
5572
5573 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5574 break;
5575
5576 if (IS_ROOT(parent)) {
5577 inode = BTRFS_I(d_inode(parent));
5578 if (btrfs_must_commit_transaction(trans, inode))
5579 ret = 1;
5580 break;
5581 }
5582
5583 parent = dget_parent(parent);
5584 dput(old_parent);
5585 old_parent = parent;
5586 inode = BTRFS_I(d_inode(parent));
5587
5588 }
5589 dput(old_parent);
5590out:
5591 return ret;
5592}
5593
5594struct btrfs_dir_list {
5595 u64 ino;
5596 struct list_head list;
5597};
5598
5599/*
5600 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5601 * details about the why it is needed.
5602 * This is a recursive operation - if an existing dentry corresponds to a
5603 * directory, that directory's new entries are logged too (same behaviour as
5604 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5605 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5606 * complains about the following circular lock dependency / possible deadlock:
5607 *
5608 * CPU0 CPU1
5609 * ---- ----
5610 * lock(&type->i_mutex_dir_key#3/2);
5611 * lock(sb_internal#2);
5612 * lock(&type->i_mutex_dir_key#3/2);
5613 * lock(&sb->s_type->i_mutex_key#14);
5614 *
5615 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5616 * sb_start_intwrite() in btrfs_start_transaction().
5617 * Not locking i_mutex of the inodes is still safe because:
5618 *
5619 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5620 * that while logging the inode new references (names) are added or removed
5621 * from the inode, leaving the logged inode item with a link count that does
5622 * not match the number of logged inode reference items. This is fine because
5623 * at log replay time we compute the real number of links and correct the
5624 * link count in the inode item (see replay_one_buffer() and
5625 * link_to_fixup_dir());
5626 *
5627 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5628 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5629 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5630 * has a size that doesn't match the sum of the lengths of all the logged
5631 * names. This does not result in a problem because if a dir_item key is
5632 * logged but its matching dir_index key is not logged, at log replay time we
5633 * don't use it to replay the respective name (see replay_one_name()). On the
5634 * other hand if only the dir_index key ends up being logged, the respective
5635 * name is added to the fs/subvol tree with both the dir_item and dir_index
5636 * keys created (see replay_one_name()).
5637 * The directory's inode item with a wrong i_size is not a problem as well,
5638 * since we don't use it at log replay time to set the i_size in the inode
5639 * item of the fs/subvol tree (see overwrite_item()).
5640 */
5641static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5642 struct btrfs_root *root,
5643 struct btrfs_inode *start_inode,
5644 struct btrfs_log_ctx *ctx)
5645{
5646 struct btrfs_fs_info *fs_info = root->fs_info;
5647 struct btrfs_root *log = root->log_root;
5648 struct btrfs_path *path;
5649 LIST_HEAD(dir_list);
5650 struct btrfs_dir_list *dir_elem;
5651 int ret = 0;
5652
5653 path = btrfs_alloc_path();
5654 if (!path)
5655 return -ENOMEM;
5656
5657 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5658 if (!dir_elem) {
5659 btrfs_free_path(path);
5660 return -ENOMEM;
5661 }
5662 dir_elem->ino = btrfs_ino(start_inode);
5663 list_add_tail(&dir_elem->list, &dir_list);
5664
5665 while (!list_empty(&dir_list)) {
5666 struct extent_buffer *leaf;
5667 struct btrfs_key min_key;
5668 int nritems;
5669 int i;
5670
5671 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5672 list);
5673 if (ret)
5674 goto next_dir_inode;
5675
5676 min_key.objectid = dir_elem->ino;
5677 min_key.type = BTRFS_DIR_ITEM_KEY;
5678 min_key.offset = 0;
5679again:
5680 btrfs_release_path(path);
5681 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5682 if (ret < 0) {
5683 goto next_dir_inode;
5684 } else if (ret > 0) {
5685 ret = 0;
5686 goto next_dir_inode;
5687 }
5688
5689process_leaf:
5690 leaf = path->nodes[0];
5691 nritems = btrfs_header_nritems(leaf);
5692 for (i = path->slots[0]; i < nritems; i++) {
5693 struct btrfs_dir_item *di;
5694 struct btrfs_key di_key;
5695 struct inode *di_inode;
5696 struct btrfs_dir_list *new_dir_elem;
5697 int log_mode = LOG_INODE_EXISTS;
5698 int type;
5699
5700 btrfs_item_key_to_cpu(leaf, &min_key, i);
5701 if (min_key.objectid != dir_elem->ino ||
5702 min_key.type != BTRFS_DIR_ITEM_KEY)
5703 goto next_dir_inode;
5704
5705 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5706 type = btrfs_dir_type(leaf, di);
5707 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5708 type != BTRFS_FT_DIR)
5709 continue;
5710 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5711 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5712 continue;
5713
5714 btrfs_release_path(path);
Olivier Deprez157378f2022-04-04 15:47:50 +02005715 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005716 if (IS_ERR(di_inode)) {
5717 ret = PTR_ERR(di_inode);
5718 goto next_dir_inode;
5719 }
5720
5721 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
David Brazdil0f672f62019-12-10 10:32:29 +00005722 btrfs_add_delayed_iput(di_inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005723 break;
5724 }
5725
5726 ctx->log_new_dentries = false;
5727 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
5728 log_mode = LOG_INODE_ALL;
5729 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Olivier Deprez157378f2022-04-04 15:47:50 +02005730 log_mode, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005731 if (!ret &&
5732 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
5733 ret = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00005734 btrfs_add_delayed_iput(di_inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005735 if (ret)
5736 goto next_dir_inode;
5737 if (ctx->log_new_dentries) {
5738 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5739 GFP_NOFS);
5740 if (!new_dir_elem) {
5741 ret = -ENOMEM;
5742 goto next_dir_inode;
5743 }
5744 new_dir_elem->ino = di_key.objectid;
5745 list_add_tail(&new_dir_elem->list, &dir_list);
5746 }
5747 break;
5748 }
5749 if (i == nritems) {
5750 ret = btrfs_next_leaf(log, path);
5751 if (ret < 0) {
5752 goto next_dir_inode;
5753 } else if (ret > 0) {
5754 ret = 0;
5755 goto next_dir_inode;
5756 }
5757 goto process_leaf;
5758 }
5759 if (min_key.offset < (u64)-1) {
5760 min_key.offset++;
5761 goto again;
5762 }
5763next_dir_inode:
5764 list_del(&dir_elem->list);
5765 kfree(dir_elem);
5766 }
5767
5768 btrfs_free_path(path);
5769 return ret;
5770}
5771
5772static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5773 struct btrfs_inode *inode,
5774 struct btrfs_log_ctx *ctx)
5775{
5776 struct btrfs_fs_info *fs_info = trans->fs_info;
5777 int ret;
5778 struct btrfs_path *path;
5779 struct btrfs_key key;
5780 struct btrfs_root *root = inode->root;
5781 const u64 ino = btrfs_ino(inode);
5782
5783 path = btrfs_alloc_path();
5784 if (!path)
5785 return -ENOMEM;
5786 path->skip_locking = 1;
5787 path->search_commit_root = 1;
5788
5789 key.objectid = ino;
5790 key.type = BTRFS_INODE_REF_KEY;
5791 key.offset = 0;
5792 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5793 if (ret < 0)
5794 goto out;
5795
5796 while (true) {
5797 struct extent_buffer *leaf = path->nodes[0];
5798 int slot = path->slots[0];
5799 u32 cur_offset = 0;
5800 u32 item_size;
5801 unsigned long ptr;
5802
5803 if (slot >= btrfs_header_nritems(leaf)) {
5804 ret = btrfs_next_leaf(root, path);
5805 if (ret < 0)
5806 goto out;
5807 else if (ret > 0)
5808 break;
5809 continue;
5810 }
5811
5812 btrfs_item_key_to_cpu(leaf, &key, slot);
5813 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5814 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5815 break;
5816
5817 item_size = btrfs_item_size_nr(leaf, slot);
5818 ptr = btrfs_item_ptr_offset(leaf, slot);
5819 while (cur_offset < item_size) {
5820 struct btrfs_key inode_key;
5821 struct inode *dir_inode;
5822
5823 inode_key.type = BTRFS_INODE_ITEM_KEY;
5824 inode_key.offset = 0;
5825
5826 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5827 struct btrfs_inode_extref *extref;
5828
5829 extref = (struct btrfs_inode_extref *)
5830 (ptr + cur_offset);
5831 inode_key.objectid = btrfs_inode_extref_parent(
5832 leaf, extref);
5833 cur_offset += sizeof(*extref);
5834 cur_offset += btrfs_inode_extref_name_len(leaf,
5835 extref);
5836 } else {
5837 inode_key.objectid = key.offset;
5838 cur_offset = item_size;
5839 }
5840
Olivier Deprez157378f2022-04-04 15:47:50 +02005841 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
5842 root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005843 /*
5844 * If the parent inode was deleted, return an error to
5845 * fallback to a transaction commit. This is to prevent
5846 * getting an inode that was moved from one parent A to
5847 * a parent B, got its former parent A deleted and then
5848 * it got fsync'ed, from existing at both parents after
5849 * a log replay (and the old parent still existing).
5850 * Example:
5851 *
5852 * mkdir /mnt/A
5853 * mkdir /mnt/B
5854 * touch /mnt/B/bar
5855 * sync
5856 * mv /mnt/B/bar /mnt/A/bar
5857 * mv -T /mnt/A /mnt/B
5858 * fsync /mnt/B/bar
5859 * <power fail>
5860 *
5861 * If we ignore the old parent B which got deleted,
5862 * after a log replay we would have file bar linked
5863 * at both parents and the old parent B would still
5864 * exist.
5865 */
5866 if (IS_ERR(dir_inode)) {
5867 ret = PTR_ERR(dir_inode);
5868 goto out;
5869 }
5870
5871 if (ctx)
5872 ctx->log_new_dentries = false;
5873 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Olivier Deprez157378f2022-04-04 15:47:50 +02005874 LOG_INODE_ALL, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005875 if (!ret &&
5876 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
5877 ret = 1;
5878 if (!ret && ctx && ctx->log_new_dentries)
5879 ret = log_new_dir_dentries(trans, root,
5880 BTRFS_I(dir_inode), ctx);
David Brazdil0f672f62019-12-10 10:32:29 +00005881 btrfs_add_delayed_iput(dir_inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005882 if (ret)
5883 goto out;
5884 }
5885 path->slots[0]++;
5886 }
5887 ret = 0;
5888out:
5889 btrfs_free_path(path);
5890 return ret;
5891}
5892
David Brazdil0f672f62019-12-10 10:32:29 +00005893static int log_new_ancestors(struct btrfs_trans_handle *trans,
5894 struct btrfs_root *root,
5895 struct btrfs_path *path,
5896 struct btrfs_log_ctx *ctx)
5897{
5898 struct btrfs_key found_key;
5899
5900 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5901
5902 while (true) {
5903 struct btrfs_fs_info *fs_info = root->fs_info;
5904 const u64 last_committed = fs_info->last_trans_committed;
5905 struct extent_buffer *leaf = path->nodes[0];
5906 int slot = path->slots[0];
5907 struct btrfs_key search_key;
5908 struct inode *inode;
Olivier Deprez157378f2022-04-04 15:47:50 +02005909 u64 ino;
David Brazdil0f672f62019-12-10 10:32:29 +00005910 int ret = 0;
5911
5912 btrfs_release_path(path);
5913
Olivier Deprez157378f2022-04-04 15:47:50 +02005914 ino = found_key.offset;
5915
David Brazdil0f672f62019-12-10 10:32:29 +00005916 search_key.objectid = found_key.offset;
5917 search_key.type = BTRFS_INODE_ITEM_KEY;
5918 search_key.offset = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02005919 inode = btrfs_iget(fs_info->sb, ino, root);
David Brazdil0f672f62019-12-10 10:32:29 +00005920 if (IS_ERR(inode))
5921 return PTR_ERR(inode);
5922
5923 if (BTRFS_I(inode)->generation > last_committed)
5924 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
Olivier Deprez157378f2022-04-04 15:47:50 +02005925 LOG_INODE_EXISTS, ctx);
David Brazdil0f672f62019-12-10 10:32:29 +00005926 btrfs_add_delayed_iput(inode);
5927 if (ret)
5928 return ret;
5929
5930 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
5931 break;
5932
5933 search_key.type = BTRFS_INODE_REF_KEY;
5934 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5935 if (ret < 0)
5936 return ret;
5937
5938 leaf = path->nodes[0];
5939 slot = path->slots[0];
5940 if (slot >= btrfs_header_nritems(leaf)) {
5941 ret = btrfs_next_leaf(root, path);
5942 if (ret < 0)
5943 return ret;
5944 else if (ret > 0)
5945 return -ENOENT;
5946 leaf = path->nodes[0];
5947 slot = path->slots[0];
5948 }
5949
5950 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5951 if (found_key.objectid != search_key.objectid ||
5952 found_key.type != BTRFS_INODE_REF_KEY)
5953 return -ENOENT;
5954 }
5955 return 0;
5956}
5957
5958static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
5959 struct btrfs_inode *inode,
5960 struct dentry *parent,
5961 struct btrfs_log_ctx *ctx)
5962{
5963 struct btrfs_root *root = inode->root;
5964 struct btrfs_fs_info *fs_info = root->fs_info;
5965 struct dentry *old_parent = NULL;
5966 struct super_block *sb = inode->vfs_inode.i_sb;
5967 int ret = 0;
5968
5969 while (true) {
5970 if (!parent || d_really_is_negative(parent) ||
5971 sb != parent->d_sb)
5972 break;
5973
5974 inode = BTRFS_I(d_inode(parent));
5975 if (root != inode->root)
5976 break;
5977
5978 if (inode->generation > fs_info->last_trans_committed) {
5979 ret = btrfs_log_inode(trans, root, inode,
Olivier Deprez157378f2022-04-04 15:47:50 +02005980 LOG_INODE_EXISTS, ctx);
David Brazdil0f672f62019-12-10 10:32:29 +00005981 if (ret)
5982 break;
5983 }
5984 if (IS_ROOT(parent))
5985 break;
5986
5987 parent = dget_parent(parent);
5988 dput(old_parent);
5989 old_parent = parent;
5990 }
5991 dput(old_parent);
5992
5993 return ret;
5994}
5995
5996static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
5997 struct btrfs_inode *inode,
5998 struct dentry *parent,
5999 struct btrfs_log_ctx *ctx)
6000{
6001 struct btrfs_root *root = inode->root;
6002 const u64 ino = btrfs_ino(inode);
6003 struct btrfs_path *path;
6004 struct btrfs_key search_key;
6005 int ret;
6006
6007 /*
6008 * For a single hard link case, go through a fast path that does not
6009 * need to iterate the fs/subvolume tree.
6010 */
6011 if (inode->vfs_inode.i_nlink < 2)
6012 return log_new_ancestors_fast(trans, inode, parent, ctx);
6013
6014 path = btrfs_alloc_path();
6015 if (!path)
6016 return -ENOMEM;
6017
6018 search_key.objectid = ino;
6019 search_key.type = BTRFS_INODE_REF_KEY;
6020 search_key.offset = 0;
6021again:
6022 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
6023 if (ret < 0)
6024 goto out;
6025 if (ret == 0)
6026 path->slots[0]++;
6027
6028 while (true) {
6029 struct extent_buffer *leaf = path->nodes[0];
6030 int slot = path->slots[0];
6031 struct btrfs_key found_key;
6032
6033 if (slot >= btrfs_header_nritems(leaf)) {
6034 ret = btrfs_next_leaf(root, path);
6035 if (ret < 0)
6036 goto out;
6037 else if (ret > 0)
6038 break;
6039 continue;
6040 }
6041
6042 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6043 if (found_key.objectid != ino ||
6044 found_key.type > BTRFS_INODE_EXTREF_KEY)
6045 break;
6046
6047 /*
6048 * Don't deal with extended references because they are rare
6049 * cases and too complex to deal with (we would need to keep
6050 * track of which subitem we are processing for each item in
6051 * this loop, etc). So just return some error to fallback to
6052 * a transaction commit.
6053 */
6054 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
6055 ret = -EMLINK;
6056 goto out;
6057 }
6058
6059 /*
6060 * Logging ancestors needs to do more searches on the fs/subvol
6061 * tree, so it releases the path as needed to avoid deadlocks.
6062 * Keep track of the last inode ref key and resume from that key
6063 * after logging all new ancestors for the current hard link.
6064 */
6065 memcpy(&search_key, &found_key, sizeof(search_key));
6066
6067 ret = log_new_ancestors(trans, root, path, ctx);
6068 if (ret)
6069 goto out;
6070 btrfs_release_path(path);
6071 goto again;
6072 }
6073 ret = 0;
6074out:
6075 btrfs_free_path(path);
6076 return ret;
6077}
6078
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006079/*
6080 * helper function around btrfs_log_inode to make sure newly created
6081 * parent directories also end up in the log. A minimal inode and backref
6082 * only logging is done of any parent directories that are older than
6083 * the last committed transaction
6084 */
6085static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
6086 struct btrfs_inode *inode,
6087 struct dentry *parent,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006088 int inode_only,
6089 struct btrfs_log_ctx *ctx)
6090{
6091 struct btrfs_root *root = inode->root;
6092 struct btrfs_fs_info *fs_info = root->fs_info;
6093 struct super_block *sb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006094 int ret = 0;
6095 u64 last_committed = fs_info->last_trans_committed;
6096 bool log_dentries = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006097
6098 sb = inode->vfs_inode.i_sb;
6099
6100 if (btrfs_test_opt(fs_info, NOTREELOG)) {
6101 ret = 1;
6102 goto end_no_trans;
6103 }
6104
6105 /*
6106 * The prev transaction commit doesn't complete, we need do
6107 * full commit by ourselves.
6108 */
6109 if (fs_info->last_trans_log_full_commit >
6110 fs_info->last_trans_committed) {
6111 ret = 1;
6112 goto end_no_trans;
6113 }
6114
6115 if (btrfs_root_refs(&root->root_item) == 0) {
6116 ret = 1;
6117 goto end_no_trans;
6118 }
6119
6120 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
6121 last_committed);
6122 if (ret)
6123 goto end_no_trans;
6124
6125 /*
6126 * Skip already logged inodes or inodes corresponding to tmpfiles
6127 * (since logging them is pointless, a link count of 0 means they
6128 * will never be accessible).
6129 */
Olivier Deprez157378f2022-04-04 15:47:50 +02006130 if ((btrfs_inode_in_log(inode, trans->transid) &&
6131 list_empty(&ctx->ordered_extents)) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006132 inode->vfs_inode.i_nlink == 0) {
6133 ret = BTRFS_NO_LOG_SYNC;
6134 goto end_no_trans;
6135 }
6136
6137 ret = start_log_trans(trans, root, ctx);
6138 if (ret)
6139 goto end_no_trans;
6140
Olivier Deprez157378f2022-04-04 15:47:50 +02006141 ret = btrfs_log_inode(trans, root, inode, inode_only, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006142 if (ret)
6143 goto end_trans;
6144
6145 /*
6146 * for regular files, if its inode is already on disk, we don't
6147 * have to worry about the parents at all. This is because
6148 * we can use the last_unlink_trans field to record renames
6149 * and other fun in this file.
6150 */
6151 if (S_ISREG(inode->vfs_inode.i_mode) &&
6152 inode->generation <= last_committed &&
6153 inode->last_unlink_trans <= last_committed) {
6154 ret = 0;
6155 goto end_trans;
6156 }
6157
6158 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
6159 log_dentries = true;
6160
6161 /*
6162 * On unlink we must make sure all our current and old parent directory
6163 * inodes are fully logged. This is to prevent leaving dangling
6164 * directory index entries in directories that were our parents but are
6165 * not anymore. Not doing this results in old parent directory being
6166 * impossible to delete after log replay (rmdir will always fail with
6167 * error -ENOTEMPTY).
6168 *
6169 * Example 1:
6170 *
6171 * mkdir testdir
6172 * touch testdir/foo
6173 * ln testdir/foo testdir/bar
6174 * sync
6175 * unlink testdir/bar
6176 * xfs_io -c fsync testdir/foo
6177 * <power failure>
6178 * mount fs, triggers log replay
6179 *
6180 * If we don't log the parent directory (testdir), after log replay the
6181 * directory still has an entry pointing to the file inode using the bar
6182 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6183 * the file inode has a link count of 1.
6184 *
6185 * Example 2:
6186 *
6187 * mkdir testdir
6188 * touch foo
6189 * ln foo testdir/foo2
6190 * ln foo testdir/foo3
6191 * sync
6192 * unlink testdir/foo3
6193 * xfs_io -c fsync foo
6194 * <power failure>
6195 * mount fs, triggers log replay
6196 *
6197 * Similar as the first example, after log replay the parent directory
6198 * testdir still has an entry pointing to the inode file with name foo3
6199 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6200 * and has a link count of 2.
6201 */
6202 if (inode->last_unlink_trans > last_committed) {
David Brazdil0f672f62019-12-10 10:32:29 +00006203 ret = btrfs_log_all_parents(trans, inode, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006204 if (ret)
6205 goto end_trans;
6206 }
6207
David Brazdil0f672f62019-12-10 10:32:29 +00006208 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6209 if (ret)
6210 goto end_trans;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006211
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006212 if (log_dentries)
David Brazdil0f672f62019-12-10 10:32:29 +00006213 ret = log_new_dir_dentries(trans, root, inode, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006214 else
6215 ret = 0;
6216end_trans:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006217 if (ret < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00006218 btrfs_set_log_full_commit(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006219 ret = 1;
6220 }
6221
6222 if (ret)
6223 btrfs_remove_log_ctx(root, ctx);
6224 btrfs_end_log_trans(root);
6225end_no_trans:
6226 return ret;
6227}
6228
6229/*
6230 * it is not safe to log dentry if the chunk root has added new
6231 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6232 * If this returns 1, you must commit the transaction to safely get your
6233 * data on disk.
6234 */
6235int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
6236 struct dentry *dentry,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006237 struct btrfs_log_ctx *ctx)
6238{
6239 struct dentry *parent = dget_parent(dentry);
6240 int ret;
6241
6242 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
Olivier Deprez157378f2022-04-04 15:47:50 +02006243 LOG_INODE_ALL, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006244 dput(parent);
6245
6246 return ret;
6247}
6248
6249/*
6250 * should be called during mount to recover any replay any log trees
6251 * from the FS
6252 */
6253int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6254{
6255 int ret;
6256 struct btrfs_path *path;
6257 struct btrfs_trans_handle *trans;
6258 struct btrfs_key key;
6259 struct btrfs_key found_key;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006260 struct btrfs_root *log;
6261 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6262 struct walk_control wc = {
6263 .process_func = process_one_buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00006264 .stage = LOG_WALK_PIN_ONLY,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006265 };
6266
6267 path = btrfs_alloc_path();
6268 if (!path)
6269 return -ENOMEM;
6270
6271 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6272
6273 trans = btrfs_start_transaction(fs_info->tree_root, 0);
6274 if (IS_ERR(trans)) {
6275 ret = PTR_ERR(trans);
6276 goto error;
6277 }
6278
6279 wc.trans = trans;
6280 wc.pin = 1;
6281
6282 ret = walk_log_tree(trans, log_root_tree, &wc);
6283 if (ret) {
6284 btrfs_handle_fs_error(fs_info, ret,
6285 "Failed to pin buffers while recovering log root tree.");
6286 goto error;
6287 }
6288
6289again:
6290 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6291 key.offset = (u64)-1;
6292 key.type = BTRFS_ROOT_ITEM_KEY;
6293
6294 while (1) {
6295 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
6296
6297 if (ret < 0) {
6298 btrfs_handle_fs_error(fs_info, ret,
6299 "Couldn't find tree log root.");
6300 goto error;
6301 }
6302 if (ret > 0) {
6303 if (path->slots[0] == 0)
6304 break;
6305 path->slots[0]--;
6306 }
6307 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6308 path->slots[0]);
6309 btrfs_release_path(path);
6310 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6311 break;
6312
Olivier Deprez157378f2022-04-04 15:47:50 +02006313 log = btrfs_read_tree_root(log_root_tree, &found_key);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006314 if (IS_ERR(log)) {
6315 ret = PTR_ERR(log);
6316 btrfs_handle_fs_error(fs_info, ret,
6317 "Couldn't read tree log root.");
6318 goto error;
6319 }
6320
Olivier Deprez157378f2022-04-04 15:47:50 +02006321 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6322 true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006323 if (IS_ERR(wc.replay_dest)) {
6324 ret = PTR_ERR(wc.replay_dest);
Olivier Deprez0e641232021-09-23 10:07:05 +02006325
6326 /*
6327 * We didn't find the subvol, likely because it was
6328 * deleted. This is ok, simply skip this log and go to
6329 * the next one.
6330 *
6331 * We need to exclude the root because we can't have
6332 * other log replays overwriting this log as we'll read
6333 * it back in a few more times. This will keep our
6334 * block from being modified, and we'll just bail for
6335 * each subsequent pass.
6336 */
6337 if (ret == -ENOENT)
Olivier Deprez157378f2022-04-04 15:47:50 +02006338 ret = btrfs_pin_extent_for_log_replay(trans,
Olivier Deprez0e641232021-09-23 10:07:05 +02006339 log->node->start,
6340 log->node->len);
Olivier Deprez157378f2022-04-04 15:47:50 +02006341 btrfs_put_root(log);
Olivier Deprez0e641232021-09-23 10:07:05 +02006342
6343 if (!ret)
6344 goto next;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006345 btrfs_handle_fs_error(fs_info, ret,
6346 "Couldn't read target root for tree log recovery.");
6347 goto error;
6348 }
6349
6350 wc.replay_dest->log_root = log;
6351 btrfs_record_root_in_trans(trans, wc.replay_dest);
6352 ret = walk_log_tree(trans, log, &wc);
6353
6354 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6355 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6356 path);
6357 }
6358
6359 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6360 struct btrfs_root *root = wc.replay_dest;
6361
6362 btrfs_release_path(path);
6363
6364 /*
6365 * We have just replayed everything, and the highest
6366 * objectid of fs roots probably has changed in case
6367 * some inode_item's got replayed.
6368 *
6369 * root->objectid_mutex is not acquired as log replay
6370 * could only happen during mount.
6371 */
6372 ret = btrfs_find_highest_objectid(root,
6373 &root->highest_objectid);
6374 }
6375
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006376 wc.replay_dest->log_root = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02006377 btrfs_put_root(wc.replay_dest);
6378 btrfs_put_root(log);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006379
6380 if (ret)
6381 goto error;
Olivier Deprez0e641232021-09-23 10:07:05 +02006382next:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006383 if (found_key.offset == 0)
6384 break;
Olivier Deprez0e641232021-09-23 10:07:05 +02006385 key.offset = found_key.offset - 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006386 }
6387 btrfs_release_path(path);
6388
6389 /* step one is to pin it all, step two is to replay just inodes */
6390 if (wc.pin) {
6391 wc.pin = 0;
6392 wc.process_func = replay_one_buffer;
6393 wc.stage = LOG_WALK_REPLAY_INODES;
6394 goto again;
6395 }
6396 /* step three is to replay everything */
6397 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6398 wc.stage++;
6399 goto again;
6400 }
6401
6402 btrfs_free_path(path);
6403
6404 /* step 4: commit the transaction, which also unpins the blocks */
6405 ret = btrfs_commit_transaction(trans);
6406 if (ret)
6407 return ret;
6408
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006409 log_root_tree->log_root = NULL;
6410 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02006411 btrfs_put_root(log_root_tree);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006412
6413 return 0;
6414error:
6415 if (wc.trans)
6416 btrfs_end_transaction(wc.trans);
Olivier Deprez0e641232021-09-23 10:07:05 +02006417 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006418 btrfs_free_path(path);
6419 return ret;
6420}
6421
6422/*
6423 * there are some corner cases where we want to force a full
6424 * commit instead of allowing a directory to be logged.
6425 *
6426 * They revolve around files there were unlinked from the directory, and
6427 * this function updates the parent directory so that a full commit is
6428 * properly done if it is fsync'd later after the unlinks are done.
6429 *
6430 * Must be called before the unlink operations (updates to the subvolume tree,
6431 * inodes, etc) are done.
6432 */
6433void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
6434 struct btrfs_inode *dir, struct btrfs_inode *inode,
6435 int for_rename)
6436{
6437 /*
6438 * when we're logging a file, if it hasn't been renamed
6439 * or unlinked, and its inode is fully committed on disk,
6440 * we don't have to worry about walking up the directory chain
6441 * to log its parents.
6442 *
6443 * So, we use the last_unlink_trans field to put this transid
6444 * into the file. When the file is logged we check it and
6445 * don't log the parents if the file is fully on disk.
6446 */
6447 mutex_lock(&inode->log_mutex);
6448 inode->last_unlink_trans = trans->transid;
6449 mutex_unlock(&inode->log_mutex);
6450
6451 /*
6452 * if this directory was already logged any new
6453 * names for this file/dir will get recorded
6454 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006455 if (dir->logged_trans == trans->transid)
6456 return;
6457
6458 /*
6459 * if the inode we're about to unlink was logged,
6460 * the log will be properly updated for any new names
6461 */
6462 if (inode->logged_trans == trans->transid)
6463 return;
6464
6465 /*
6466 * when renaming files across directories, if the directory
6467 * there we're unlinking from gets fsync'd later on, there's
6468 * no way to find the destination directory later and fsync it
6469 * properly. So, we have to be conservative and force commits
6470 * so the new name gets discovered.
6471 */
6472 if (for_rename)
6473 goto record;
6474
6475 /* we can safely do the unlink without any special recording */
6476 return;
6477
6478record:
6479 mutex_lock(&dir->log_mutex);
6480 dir->last_unlink_trans = trans->transid;
6481 mutex_unlock(&dir->log_mutex);
6482}
6483
6484/*
6485 * Make sure that if someone attempts to fsync the parent directory of a deleted
6486 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6487 * that after replaying the log tree of the parent directory's root we will not
6488 * see the snapshot anymore and at log replay time we will not see any log tree
6489 * corresponding to the deleted snapshot's root, which could lead to replaying
6490 * it after replaying the log tree of the parent directory (which would replay
6491 * the snapshot delete operation).
6492 *
6493 * Must be called before the actual snapshot destroy operation (updates to the
6494 * parent root and tree of tree roots trees, etc) are done.
6495 */
6496void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
6497 struct btrfs_inode *dir)
6498{
6499 mutex_lock(&dir->log_mutex);
6500 dir->last_unlink_trans = trans->transid;
6501 mutex_unlock(&dir->log_mutex);
6502}
6503
6504/*
6505 * Call this after adding a new name for a file and it will properly
6506 * update the log to reflect the new name.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006507 */
Olivier Deprez0e641232021-09-23 10:07:05 +02006508void btrfs_log_new_name(struct btrfs_trans_handle *trans,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006509 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Olivier Deprez0e641232021-09-23 10:07:05 +02006510 struct dentry *parent)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006511{
Olivier Deprez0e641232021-09-23 10:07:05 +02006512 struct btrfs_log_ctx ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006513
6514 /*
6515 * this will force the logging code to walk the dentry chain
6516 * up for the file
6517 */
6518 if (!S_ISDIR(inode->vfs_inode.i_mode))
6519 inode->last_unlink_trans = trans->transid;
6520
6521 /*
6522 * if this inode hasn't been logged and directory we're renaming it
6523 * from hasn't been logged, we don't need to log it
6524 */
Olivier Deprez0e641232021-09-23 10:07:05 +02006525 if (!inode_logged(trans, inode) &&
6526 (!old_dir || !inode_logged(trans, old_dir)))
6527 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006528
Olivier Deprez0e641232021-09-23 10:07:05 +02006529 btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
6530 ctx.logging_new_name = true;
6531 /*
6532 * We don't care about the return value. If we fail to log the new name
6533 * then we know the next attempt to sync the log will fallback to a full
6534 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
6535 * we don't need to worry about getting a log committed that has an
6536 * inconsistent state after a rename operation.
6537 */
Olivier Deprez157378f2022-04-04 15:47:50 +02006538 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006539}
6540