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