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