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 "ctree.h"
22 #include "transaction.h"
23 #include "disk-io.h"
24 #include "locking.h"
25 #include "print-tree.h"
26 #include "compat.h"
27 #include "tree-log.h"
28
29 /* magic values for the inode_only field in btrfs_log_inode:
30 *
31 * LOG_INODE_ALL means to log everything
32 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 * during log replay
34 */
35 #define LOG_INODE_ALL 0
36 #define LOG_INODE_EXISTS 1
37
38 /*
39 * directory trouble cases
40 *
41 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
42 * log, we must force a full commit before doing an fsync of the directory
43 * where the unlink was done.
44 * ---> record transid of last unlink/rename per directory
45 *
46 * mkdir foo/some_dir
47 * normal commit
48 * rename foo/some_dir foo2/some_dir
49 * mkdir foo/some_dir
50 * fsync foo/some_dir/some_file
51 *
52 * The fsync above will unlink the original some_dir without recording
53 * it in its new location (foo2). After a crash, some_dir will be gone
54 * unless the fsync of some_file forces a full commit
55 *
56 * 2) we must log any new names for any file or dir that is in the fsync
57 * log. ---> check inode while renaming/linking.
58 *
59 * 2a) we must log any new names for any file or dir during rename
60 * when the directory they are being removed from was logged.
61 * ---> check inode and old parent dir during rename
62 *
63 * 2a is actually the more important variant. With the extra logging
64 * a crash might unlink the old name without recreating the new one
65 *
66 * 3) after a crash, we must go through any directories with a link count
67 * of zero and redo the rm -rf
68 *
69 * mkdir f1/foo
70 * normal commit
71 * rm -rf f1/foo
72 * fsync(f1)
73 *
74 * The directory f1 was fully removed from the FS, but fsync was never
75 * called on f1, only its parent dir. After a crash the rm -rf must
76 * be replayed. This must be able to recurse down the entire
77 * directory tree. The inode link count fixup code takes care of the
78 * ugly details.
79 */
80
81 /*
82 * stages for the tree walking. The first
83 * stage (0) is to only pin down the blocks we find
84 * the second stage (1) is to make sure that all the inodes
85 * we find in the log are created in the subvolume.
86 *
87 * The last stage is to deal with directories and links and extents
88 * and all the other fun semantics
89 */
90 #define LOG_WALK_PIN_ONLY 0
91 #define LOG_WALK_REPLAY_INODES 1
92 #define LOG_WALK_REPLAY_ALL 2
93
94 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root, struct inode *inode,
96 int inode_only);
97 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_path *path, u64 objectid);
100 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_root *log,
103 struct btrfs_path *path,
104 u64 dirid, int del_all);
105
106 /*
107 * tree logging is a special write ahead log used to make sure that
108 * fsyncs and O_SYNCs can happen without doing full tree commits.
109 *
110 * Full tree commits are expensive because they require commonly
111 * modified blocks to be recowed, creating many dirty pages in the
112 * extent tree an 4x-6x higher write load than ext3.
113 *
114 * Instead of doing a tree commit on every fsync, we use the
115 * key ranges and transaction ids to find items for a given file or directory
116 * that have changed in this transaction. Those items are copied into
117 * a special tree (one per subvolume root), that tree is written to disk
118 * and then the fsync is considered complete.
119 *
120 * After a crash, items are copied out of the log-tree back into the
121 * subvolume tree. Any file data extents found are recorded in the extent
122 * allocation tree, and the log-tree freed.
123 *
124 * The log tree is read three times, once to pin down all the extents it is
125 * using in ram and once, once to create all the inodes logged in the tree
126 * and once to do all the other items.
127 */
128
129 /*
130 * start a sub transaction and setup the log tree
131 * this increments the log tree writer count to make the people
132 * syncing the tree wait for us to finish
133 */
start_log_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root)134 static int start_log_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root)
136 {
137 int ret;
138 int err = 0;
139
140 mutex_lock(&root->log_mutex);
141 if (root->log_root) {
142 if (!root->log_start_pid) {
143 root->log_start_pid = current->pid;
144 root->log_multiple_pids = false;
145 } else if (root->log_start_pid != current->pid) {
146 root->log_multiple_pids = true;
147 }
148
149 root->log_batch++;
150 atomic_inc(&root->log_writers);
151 mutex_unlock(&root->log_mutex);
152 return 0;
153 }
154 root->log_multiple_pids = false;
155 root->log_start_pid = current->pid;
156 mutex_lock(&root->fs_info->tree_log_mutex);
157 if (!root->fs_info->log_root_tree) {
158 ret = btrfs_init_log_root_tree(trans, root->fs_info);
159 if (ret)
160 err = ret;
161 }
162 if (err == 0 && !root->log_root) {
163 ret = btrfs_add_log_tree(trans, root);
164 if (ret)
165 err = ret;
166 }
167 mutex_unlock(&root->fs_info->tree_log_mutex);
168 root->log_batch++;
169 atomic_inc(&root->log_writers);
170 mutex_unlock(&root->log_mutex);
171 return err;
172 }
173
174 /*
175 * returns 0 if there was a log transaction running and we were able
176 * to join, or returns -ENOENT if there were not transactions
177 * in progress
178 */
join_running_log_trans(struct btrfs_root * root)179 static int join_running_log_trans(struct btrfs_root *root)
180 {
181 int ret = -ENOENT;
182
183 smp_mb();
184 if (!root->log_root)
185 return -ENOENT;
186
187 mutex_lock(&root->log_mutex);
188 if (root->log_root) {
189 ret = 0;
190 atomic_inc(&root->log_writers);
191 }
192 mutex_unlock(&root->log_mutex);
193 return ret;
194 }
195
196 /*
197 * This either makes the current running log transaction wait
198 * until you call btrfs_end_log_trans() or it makes any future
199 * log transactions wait until you call btrfs_end_log_trans()
200 */
btrfs_pin_log_trans(struct btrfs_root * root)201 int btrfs_pin_log_trans(struct btrfs_root *root)
202 {
203 int ret = -ENOENT;
204
205 mutex_lock(&root->log_mutex);
206 atomic_inc(&root->log_writers);
207 mutex_unlock(&root->log_mutex);
208 return ret;
209 }
210
211 /*
212 * indicate we're done making changes to the log tree
213 * and wake up anyone waiting to do a sync
214 */
btrfs_end_log_trans(struct btrfs_root * root)215 void btrfs_end_log_trans(struct btrfs_root *root)
216 {
217 if (atomic_dec_and_test(&root->log_writers)) {
218 smp_mb();
219 if (waitqueue_active(&root->log_writer_wait))
220 wake_up(&root->log_writer_wait);
221 }
222 }
223
224
225 /*
226 * the walk control struct is used to pass state down the chain when
227 * processing the log tree. The stage field tells us which part
228 * of the log tree processing we are currently doing. The others
229 * are state fields used for that specific part
230 */
231 struct walk_control {
232 /* should we free the extent on disk when done? This is used
233 * at transaction commit time while freeing a log tree
234 */
235 int free;
236
237 /* should we write out the extent buffer? This is used
238 * while flushing the log tree to disk during a sync
239 */
240 int write;
241
242 /* should we wait for the extent buffer io to finish? Also used
243 * while flushing the log tree to disk for a sync
244 */
245 int wait;
246
247 /* pin only walk, we record which extents on disk belong to the
248 * log trees
249 */
250 int pin;
251
252 /* what stage of the replay code we're currently in */
253 int stage;
254
255 /* the root we are currently replaying */
256 struct btrfs_root *replay_dest;
257
258 /* the trans handle for the current replay */
259 struct btrfs_trans_handle *trans;
260
261 /* the function that gets used to process blocks we find in the
262 * tree. Note the extent_buffer might not be up to date when it is
263 * passed in, and it must be checked or read if you need the data
264 * inside it
265 */
266 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
267 struct walk_control *wc, u64 gen);
268 };
269
270 /*
271 * process_func used to pin down extents, write them or wait on them
272 */
process_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen)273 static int process_one_buffer(struct btrfs_root *log,
274 struct extent_buffer *eb,
275 struct walk_control *wc, u64 gen)
276 {
277 if (wc->pin)
278 btrfs_pin_extent_for_log_replay(wc->trans,
279 log->fs_info->extent_root,
280 eb->start, eb->len);
281
282 if (btrfs_buffer_uptodate(eb, gen, 0)) {
283 if (wc->write)
284 btrfs_write_tree_block(eb);
285 if (wc->wait)
286 btrfs_wait_tree_block_writeback(eb);
287 }
288 return 0;
289 }
290
291 /*
292 * Item overwrite used by replay and tree logging. eb, slot and key all refer
293 * to the src data we are copying out.
294 *
295 * root is the tree we are copying into, and path is a scratch
296 * path for use in this function (it should be released on entry and
297 * will be released on exit).
298 *
299 * If the key is already in the destination tree the existing item is
300 * overwritten. If the existing item isn't big enough, it is extended.
301 * If it is too large, it is truncated.
302 *
303 * If the key isn't in the destination yet, a new item is inserted.
304 */
overwrite_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)305 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path,
308 struct extent_buffer *eb, int slot,
309 struct btrfs_key *key)
310 {
311 int ret;
312 u32 item_size;
313 u64 saved_i_size = 0;
314 int save_old_i_size = 0;
315 unsigned long src_ptr;
316 unsigned long dst_ptr;
317 int overwrite_root = 0;
318 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
319
320 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
321 overwrite_root = 1;
322
323 item_size = btrfs_item_size_nr(eb, slot);
324 src_ptr = btrfs_item_ptr_offset(eb, slot);
325
326 /* look for the key in the destination tree */
327 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
328 if (ret < 0)
329 return ret;
330
331 if (ret == 0) {
332 char *src_copy;
333 char *dst_copy;
334 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
335 path->slots[0]);
336 if (dst_size != item_size)
337 goto insert;
338
339 if (item_size == 0) {
340 btrfs_release_path(path);
341 return 0;
342 }
343 dst_copy = kmalloc(item_size, GFP_NOFS);
344 src_copy = kmalloc(item_size, GFP_NOFS);
345 if (!dst_copy || !src_copy) {
346 btrfs_release_path(path);
347 kfree(dst_copy);
348 kfree(src_copy);
349 return -ENOMEM;
350 }
351
352 read_extent_buffer(eb, src_copy, src_ptr, item_size);
353
354 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
355 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
356 item_size);
357 ret = memcmp(dst_copy, src_copy, item_size);
358
359 kfree(dst_copy);
360 kfree(src_copy);
361 /*
362 * they have the same contents, just return, this saves
363 * us from cowing blocks in the destination tree and doing
364 * extra writes that may not have been done by a previous
365 * sync
366 */
367 if (ret == 0) {
368 btrfs_release_path(path);
369 return 0;
370 }
371
372 /*
373 * We need to load the old nbytes into the inode so when we
374 * replay the extents we've logged we get the right nbytes.
375 */
376 if (inode_item) {
377 struct btrfs_inode_item *item;
378 u64 nbytes;
379
380 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
381 struct btrfs_inode_item);
382 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
383 item = btrfs_item_ptr(eb, slot,
384 struct btrfs_inode_item);
385 btrfs_set_inode_nbytes(eb, item, nbytes);
386 }
387 } else if (inode_item) {
388 struct btrfs_inode_item *item;
389
390 /*
391 * New inode, set nbytes to 0 so that the nbytes comes out
392 * properly when we replay the extents.
393 */
394 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
395 btrfs_set_inode_nbytes(eb, item, 0);
396 }
397 insert:
398 btrfs_release_path(path);
399 /* try to insert the key into the destination tree */
400 ret = btrfs_insert_empty_item(trans, root, path,
401 key, item_size);
402
403 /* make sure any existing item is the correct size */
404 if (ret == -EEXIST) {
405 u32 found_size;
406 found_size = btrfs_item_size_nr(path->nodes[0],
407 path->slots[0]);
408 if (found_size > item_size)
409 btrfs_truncate_item(trans, root, path, item_size, 1);
410 else if (found_size < item_size)
411 btrfs_extend_item(trans, root, path,
412 item_size - found_size);
413 } else if (ret) {
414 return ret;
415 }
416 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
417 path->slots[0]);
418
419 /* don't overwrite an existing inode if the generation number
420 * was logged as zero. This is done when the tree logging code
421 * is just logging an inode to make sure it exists after recovery.
422 *
423 * Also, don't overwrite i_size on directories during replay.
424 * log replay inserts and removes directory items based on the
425 * state of the tree found in the subvolume, and i_size is modified
426 * as it goes
427 */
428 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
429 struct btrfs_inode_item *src_item;
430 struct btrfs_inode_item *dst_item;
431
432 src_item = (struct btrfs_inode_item *)src_ptr;
433 dst_item = (struct btrfs_inode_item *)dst_ptr;
434
435 if (btrfs_inode_generation(eb, src_item) == 0)
436 goto no_copy;
437
438 if (overwrite_root &&
439 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
440 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
441 save_old_i_size = 1;
442 saved_i_size = btrfs_inode_size(path->nodes[0],
443 dst_item);
444 }
445 }
446
447 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
448 src_ptr, item_size);
449
450 if (save_old_i_size) {
451 struct btrfs_inode_item *dst_item;
452 dst_item = (struct btrfs_inode_item *)dst_ptr;
453 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
454 }
455
456 /* make sure the generation is filled in */
457 if (key->type == BTRFS_INODE_ITEM_KEY) {
458 struct btrfs_inode_item *dst_item;
459 dst_item = (struct btrfs_inode_item *)dst_ptr;
460 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
461 btrfs_set_inode_generation(path->nodes[0], dst_item,
462 trans->transid);
463 }
464 }
465 no_copy:
466 btrfs_mark_buffer_dirty(path->nodes[0]);
467 btrfs_release_path(path);
468 return 0;
469 }
470
471 /*
472 * simple helper to read an inode off the disk from a given root
473 * This can only be called for subvolume roots and not for the log
474 */
read_one_inode(struct btrfs_root * root,u64 objectid)475 static noinline struct inode *read_one_inode(struct btrfs_root *root,
476 u64 objectid)
477 {
478 struct btrfs_key key;
479 struct inode *inode;
480
481 key.objectid = objectid;
482 key.type = BTRFS_INODE_ITEM_KEY;
483 key.offset = 0;
484 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
485 if (IS_ERR(inode)) {
486 inode = NULL;
487 } else if (is_bad_inode(inode)) {
488 iput(inode);
489 inode = NULL;
490 }
491 return inode;
492 }
493
494 /* replays a single extent in 'eb' at 'slot' with 'key' into the
495 * subvolume 'root'. path is released on entry and should be released
496 * on exit.
497 *
498 * extents in the log tree have not been allocated out of the extent
499 * tree yet. So, this completes the allocation, taking a reference
500 * as required if the extent already exists or creating a new extent
501 * if it isn't in the extent allocation tree yet.
502 *
503 * The extent is inserted into the file, dropping any existing extents
504 * from the file that overlap the new one.
505 */
replay_one_extent(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)506 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 struct extent_buffer *eb, int slot,
510 struct btrfs_key *key)
511 {
512 int found_type;
513 u64 mask = root->sectorsize - 1;
514 u64 extent_end;
515 u64 alloc_hint;
516 u64 start = key->offset;
517 u64 nbytes = 0;
518 struct btrfs_file_extent_item *item;
519 struct inode *inode = NULL;
520 unsigned long size;
521 int ret = 0;
522
523 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
524 found_type = btrfs_file_extent_type(eb, item);
525
526 if (found_type == BTRFS_FILE_EXTENT_REG ||
527 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
528 nbytes = btrfs_file_extent_num_bytes(eb, item);
529 extent_end = start + nbytes;
530
531 /*
532 * We don't add to the inodes nbytes if we are prealloc or a
533 * hole.
534 */
535 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
536 nbytes = 0;
537 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
538 size = btrfs_file_extent_inline_len(eb, item);
539 nbytes = btrfs_file_extent_ram_bytes(eb, item);
540 extent_end = (start + size + mask) & ~mask;
541 } else {
542 ret = 0;
543 goto out;
544 }
545
546 inode = read_one_inode(root, key->objectid);
547 if (!inode) {
548 ret = -EIO;
549 goto out;
550 }
551
552 /*
553 * first check to see if we already have this extent in the
554 * file. This must be done before the btrfs_drop_extents run
555 * so we don't try to drop this extent.
556 */
557 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
558 start, 0);
559
560 if (ret == 0 &&
561 (found_type == BTRFS_FILE_EXTENT_REG ||
562 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
563 struct btrfs_file_extent_item cmp1;
564 struct btrfs_file_extent_item cmp2;
565 struct btrfs_file_extent_item *existing;
566 struct extent_buffer *leaf;
567
568 leaf = path->nodes[0];
569 existing = btrfs_item_ptr(leaf, path->slots[0],
570 struct btrfs_file_extent_item);
571
572 read_extent_buffer(eb, &cmp1, (unsigned long)item,
573 sizeof(cmp1));
574 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
575 sizeof(cmp2));
576
577 /*
578 * we already have a pointer to this exact extent,
579 * we don't have to do anything
580 */
581 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
582 btrfs_release_path(path);
583 goto out;
584 }
585 }
586 btrfs_release_path(path);
587
588 /* drop any overlapping extents */
589 ret = btrfs_drop_extents(trans, inode, start, extent_end,
590 &alloc_hint, 1);
591 BUG_ON(ret);
592
593 if (found_type == BTRFS_FILE_EXTENT_REG ||
594 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
595 u64 offset;
596 unsigned long dest_offset;
597 struct btrfs_key ins;
598
599 ret = btrfs_insert_empty_item(trans, root, path, key,
600 sizeof(*item));
601 BUG_ON(ret);
602 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
603 path->slots[0]);
604 copy_extent_buffer(path->nodes[0], eb, dest_offset,
605 (unsigned long)item, sizeof(*item));
606
607 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
608 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
609 ins.type = BTRFS_EXTENT_ITEM_KEY;
610 offset = key->offset - btrfs_file_extent_offset(eb, item);
611
612 if (ins.objectid > 0) {
613 u64 csum_start;
614 u64 csum_end;
615 LIST_HEAD(ordered_sums);
616 /*
617 * is this extent already allocated in the extent
618 * allocation tree? If so, just add a reference
619 */
620 ret = btrfs_lookup_extent(root, ins.objectid,
621 ins.offset);
622 if (ret == 0) {
623 ret = btrfs_inc_extent_ref(trans, root,
624 ins.objectid, ins.offset,
625 0, root->root_key.objectid,
626 key->objectid, offset, 0);
627 BUG_ON(ret);
628 } else {
629 /*
630 * insert the extent pointer in the extent
631 * allocation tree
632 */
633 ret = btrfs_alloc_logged_file_extent(trans,
634 root, root->root_key.objectid,
635 key->objectid, offset, &ins);
636 BUG_ON(ret);
637 }
638 btrfs_release_path(path);
639
640 if (btrfs_file_extent_compression(eb, item)) {
641 csum_start = ins.objectid;
642 csum_end = csum_start + ins.offset;
643 } else {
644 csum_start = ins.objectid +
645 btrfs_file_extent_offset(eb, item);
646 csum_end = csum_start +
647 btrfs_file_extent_num_bytes(eb, item);
648 }
649
650 ret = btrfs_lookup_csums_range(root->log_root,
651 csum_start, csum_end - 1,
652 &ordered_sums, 0);
653 BUG_ON(ret);
654 while (!list_empty(&ordered_sums)) {
655 struct btrfs_ordered_sum *sums;
656 sums = list_entry(ordered_sums.next,
657 struct btrfs_ordered_sum,
658 list);
659 ret = btrfs_csum_file_blocks(trans,
660 root->fs_info->csum_root,
661 sums);
662 BUG_ON(ret);
663 list_del(&sums->list);
664 kfree(sums);
665 }
666 } else {
667 btrfs_release_path(path);
668 }
669 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
670 /* inline extents are easy, we just overwrite them */
671 ret = overwrite_item(trans, root, path, eb, slot, key);
672 BUG_ON(ret);
673 }
674
675 inode_add_bytes(inode, nbytes);
676 btrfs_update_inode(trans, root, inode);
677 out:
678 if (inode)
679 iput(inode);
680 return ret;
681 }
682
683 /*
684 * when cleaning up conflicts between the directory names in the
685 * subvolume, directory names in the log and directory names in the
686 * inode back references, we may have to unlink inodes from directories.
687 *
688 * This is a helper function to do the unlink of a specific directory
689 * item
690 */
drop_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct inode * dir,struct btrfs_dir_item * di)691 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
692 struct btrfs_root *root,
693 struct btrfs_path *path,
694 struct inode *dir,
695 struct btrfs_dir_item *di)
696 {
697 struct inode *inode;
698 char *name;
699 int name_len;
700 struct extent_buffer *leaf;
701 struct btrfs_key location;
702 int ret;
703
704 leaf = path->nodes[0];
705
706 btrfs_dir_item_key_to_cpu(leaf, di, &location);
707 name_len = btrfs_dir_name_len(leaf, di);
708 name = kmalloc(name_len, GFP_NOFS);
709 if (!name)
710 return -ENOMEM;
711
712 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
713 btrfs_release_path(path);
714
715 inode = read_one_inode(root, location.objectid);
716 if (!inode) {
717 kfree(name);
718 return -EIO;
719 }
720
721 ret = link_to_fixup_dir(trans, root, path, location.objectid);
722 BUG_ON(ret);
723
724 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
725 BUG_ON(ret);
726 kfree(name);
727
728 iput(inode);
729
730 btrfs_run_delayed_items(trans, root);
731 return ret;
732 }
733
734 /*
735 * helper function to see if a given name and sequence number found
736 * in an inode back reference are already in a directory and correctly
737 * point to this inode
738 */
inode_in_dir(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 objectid,u64 index,const char * name,int name_len)739 static noinline int inode_in_dir(struct btrfs_root *root,
740 struct btrfs_path *path,
741 u64 dirid, u64 objectid, u64 index,
742 const char *name, int name_len)
743 {
744 struct btrfs_dir_item *di;
745 struct btrfs_key location;
746 int match = 0;
747
748 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
749 index, name, name_len, 0);
750 if (di && !IS_ERR(di)) {
751 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
752 if (location.objectid != objectid)
753 goto out;
754 } else
755 goto out;
756 btrfs_release_path(path);
757
758 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
759 if (di && !IS_ERR(di)) {
760 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
761 if (location.objectid != objectid)
762 goto out;
763 } else
764 goto out;
765 match = 1;
766 out:
767 btrfs_release_path(path);
768 return match;
769 }
770
771 /*
772 * helper function to check a log tree for a named back reference in
773 * an inode. This is used to decide if a back reference that is
774 * found in the subvolume conflicts with what we find in the log.
775 *
776 * inode backreferences may have multiple refs in a single item,
777 * during replay we process one reference at a time, and we don't
778 * want to delete valid links to a file from the subvolume if that
779 * link is also in the log.
780 */
backref_in_log(struct btrfs_root * log,struct btrfs_key * key,char * name,int namelen)781 static noinline int backref_in_log(struct btrfs_root *log,
782 struct btrfs_key *key,
783 char *name, int namelen)
784 {
785 struct btrfs_path *path;
786 struct btrfs_inode_ref *ref;
787 unsigned long ptr;
788 unsigned long ptr_end;
789 unsigned long name_ptr;
790 int found_name_len;
791 int item_size;
792 int ret;
793 int match = 0;
794
795 path = btrfs_alloc_path();
796 if (!path)
797 return -ENOMEM;
798
799 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
800 if (ret != 0)
801 goto out;
802
803 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
804 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
805 ptr_end = ptr + item_size;
806 while (ptr < ptr_end) {
807 ref = (struct btrfs_inode_ref *)ptr;
808 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
809 if (found_name_len == namelen) {
810 name_ptr = (unsigned long)(ref + 1);
811 ret = memcmp_extent_buffer(path->nodes[0], name,
812 name_ptr, namelen);
813 if (ret == 0) {
814 match = 1;
815 goto out;
816 }
817 }
818 ptr = (unsigned long)(ref + 1) + found_name_len;
819 }
820 out:
821 btrfs_free_path(path);
822 return match;
823 }
824
825
826 /*
827 * replay one inode back reference item found in the log tree.
828 * eb, slot and key refer to the buffer and key found in the log tree.
829 * root is the destination we are replaying into, and path is for temp
830 * use by this function. (it should be released on return).
831 */
add_inode_ref(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)832 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct btrfs_root *log,
835 struct btrfs_path *path,
836 struct extent_buffer *eb, int slot,
837 struct btrfs_key *key)
838 {
839 struct btrfs_inode_ref *ref;
840 struct btrfs_dir_item *di;
841 struct inode *dir;
842 struct inode *inode;
843 unsigned long ref_ptr;
844 unsigned long ref_end;
845 char *name;
846 int namelen;
847 int ret;
848 int search_done = 0;
849
850 /*
851 * it is possible that we didn't log all the parent directories
852 * for a given inode. If we don't find the dir, just don't
853 * copy the back ref in. The link count fixup code will take
854 * care of the rest
855 */
856 dir = read_one_inode(root, key->offset);
857 if (!dir)
858 return -ENOENT;
859
860 inode = read_one_inode(root, key->objectid);
861 if (!inode) {
862 iput(dir);
863 return -EIO;
864 }
865
866 ref_ptr = btrfs_item_ptr_offset(eb, slot);
867 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
868
869 again:
870 ref = (struct btrfs_inode_ref *)ref_ptr;
871
872 namelen = btrfs_inode_ref_name_len(eb, ref);
873 name = kmalloc(namelen, GFP_NOFS);
874 BUG_ON(!name);
875
876 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
877
878 /* if we already have a perfect match, we're done */
879 if (inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
880 btrfs_inode_ref_index(eb, ref),
881 name, namelen)) {
882 goto out;
883 }
884
885 /*
886 * look for a conflicting back reference in the metadata.
887 * if we find one we have to unlink that name of the file
888 * before we add our new link. Later on, we overwrite any
889 * existing back reference, and we don't want to create
890 * dangling pointers in the directory.
891 */
892
893 if (search_done)
894 goto insert;
895
896 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
897 if (ret == 0) {
898 char *victim_name;
899 int victim_name_len;
900 struct btrfs_inode_ref *victim_ref;
901 unsigned long ptr;
902 unsigned long ptr_end;
903 struct extent_buffer *leaf = path->nodes[0];
904
905 /* are we trying to overwrite a back ref for the root directory
906 * if so, just jump out, we're done
907 */
908 if (key->objectid == key->offset)
909 goto out_nowrite;
910
911 /* check all the names in this back reference to see
912 * if they are in the log. if so, we allow them to stay
913 * otherwise they must be unlinked as a conflict
914 */
915 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
916 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
917 while (ptr < ptr_end) {
918 victim_ref = (struct btrfs_inode_ref *)ptr;
919 victim_name_len = btrfs_inode_ref_name_len(leaf,
920 victim_ref);
921 victim_name = kmalloc(victim_name_len, GFP_NOFS);
922 BUG_ON(!victim_name);
923
924 read_extent_buffer(leaf, victim_name,
925 (unsigned long)(victim_ref + 1),
926 victim_name_len);
927
928 if (!backref_in_log(log, key, victim_name,
929 victim_name_len)) {
930 btrfs_inc_nlink(inode);
931 btrfs_release_path(path);
932
933 ret = btrfs_unlink_inode(trans, root, dir,
934 inode, victim_name,
935 victim_name_len);
936 btrfs_run_delayed_items(trans, root);
937 }
938 kfree(victim_name);
939 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
940 }
941 BUG_ON(ret);
942
943 /*
944 * NOTE: we have searched root tree and checked the
945 * coresponding ref, it does not need to check again.
946 */
947 search_done = 1;
948 }
949 btrfs_release_path(path);
950
951 /* look for a conflicting sequence number */
952 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
953 btrfs_inode_ref_index(eb, ref),
954 name, namelen, 0);
955 if (di && !IS_ERR(di)) {
956 ret = drop_one_dir_item(trans, root, path, dir, di);
957 BUG_ON(ret);
958 }
959 btrfs_release_path(path);
960
961 /* look for a conflicing name */
962 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
963 name, namelen, 0);
964 if (di && !IS_ERR(di)) {
965 ret = drop_one_dir_item(trans, root, path, dir, di);
966 BUG_ON(ret);
967 }
968 btrfs_release_path(path);
969
970 insert:
971 /* insert our name */
972 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
973 btrfs_inode_ref_index(eb, ref));
974 BUG_ON(ret);
975
976 btrfs_update_inode(trans, root, inode);
977
978 out:
979 ref_ptr = (unsigned long)(ref + 1) + namelen;
980 kfree(name);
981 if (ref_ptr < ref_end)
982 goto again;
983
984 /* finally write the back reference in the inode */
985 ret = overwrite_item(trans, root, path, eb, slot, key);
986 BUG_ON(ret);
987
988 out_nowrite:
989 btrfs_release_path(path);
990 iput(dir);
991 iput(inode);
992 return 0;
993 }
994
insert_orphan_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 offset)995 static int insert_orphan_item(struct btrfs_trans_handle *trans,
996 struct btrfs_root *root, u64 offset)
997 {
998 int ret;
999 ret = btrfs_find_orphan_item(root, offset);
1000 if (ret > 0)
1001 ret = btrfs_insert_orphan_item(trans, root, offset);
1002 return ret;
1003 }
1004
1005
1006 /*
1007 * There are a few corners where the link count of the file can't
1008 * be properly maintained during replay. So, instead of adding
1009 * lots of complexity to the log code, we just scan the backrefs
1010 * for any file that has been through replay.
1011 *
1012 * The scan will update the link count on the inode to reflect the
1013 * number of back refs found. If it goes down to zero, the iput
1014 * will free the inode.
1015 */
fixup_inode_link_count(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode)1016 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root,
1018 struct inode *inode)
1019 {
1020 struct btrfs_path *path;
1021 int ret;
1022 struct btrfs_key key;
1023 u64 nlink = 0;
1024 unsigned long ptr;
1025 unsigned long ptr_end;
1026 int name_len;
1027 u64 ino = btrfs_ino(inode);
1028
1029 key.objectid = ino;
1030 key.type = BTRFS_INODE_REF_KEY;
1031 key.offset = (u64)-1;
1032
1033 path = btrfs_alloc_path();
1034 if (!path)
1035 return -ENOMEM;
1036
1037 while (1) {
1038 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1039 if (ret < 0)
1040 break;
1041 if (ret > 0) {
1042 if (path->slots[0] == 0)
1043 break;
1044 path->slots[0]--;
1045 }
1046 btrfs_item_key_to_cpu(path->nodes[0], &key,
1047 path->slots[0]);
1048 if (key.objectid != ino ||
1049 key.type != BTRFS_INODE_REF_KEY)
1050 break;
1051 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1052 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1053 path->slots[0]);
1054 while (ptr < ptr_end) {
1055 struct btrfs_inode_ref *ref;
1056
1057 ref = (struct btrfs_inode_ref *)ptr;
1058 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1059 ref);
1060 ptr = (unsigned long)(ref + 1) + name_len;
1061 nlink++;
1062 }
1063
1064 if (key.offset == 0)
1065 break;
1066 key.offset--;
1067 btrfs_release_path(path);
1068 }
1069 btrfs_release_path(path);
1070 if (nlink != inode->i_nlink) {
1071 set_nlink(inode, nlink);
1072 btrfs_update_inode(trans, root, inode);
1073 }
1074 BTRFS_I(inode)->index_cnt = (u64)-1;
1075
1076 if (inode->i_nlink == 0) {
1077 if (S_ISDIR(inode->i_mode)) {
1078 ret = replay_dir_deletes(trans, root, NULL, path,
1079 ino, 1);
1080 BUG_ON(ret);
1081 }
1082 ret = insert_orphan_item(trans, root, ino);
1083 BUG_ON(ret);
1084 }
1085 btrfs_free_path(path);
1086
1087 return 0;
1088 }
1089
fixup_inode_link_counts(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path)1090 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1091 struct btrfs_root *root,
1092 struct btrfs_path *path)
1093 {
1094 int ret;
1095 struct btrfs_key key;
1096 struct inode *inode;
1097
1098 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1099 key.type = BTRFS_ORPHAN_ITEM_KEY;
1100 key.offset = (u64)-1;
1101 while (1) {
1102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103 if (ret < 0)
1104 break;
1105
1106 if (ret == 1) {
1107 if (path->slots[0] == 0)
1108 break;
1109 path->slots[0]--;
1110 }
1111
1112 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1113 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1114 key.type != BTRFS_ORPHAN_ITEM_KEY)
1115 break;
1116
1117 ret = btrfs_del_item(trans, root, path);
1118 if (ret)
1119 goto out;
1120
1121 btrfs_release_path(path);
1122 inode = read_one_inode(root, key.offset);
1123 if (!inode)
1124 return -EIO;
1125
1126 ret = fixup_inode_link_count(trans, root, inode);
1127 BUG_ON(ret);
1128
1129 iput(inode);
1130
1131 /*
1132 * fixup on a directory may create new entries,
1133 * make sure we always look for the highset possible
1134 * offset
1135 */
1136 key.offset = (u64)-1;
1137 }
1138 ret = 0;
1139 out:
1140 btrfs_release_path(path);
1141 return ret;
1142 }
1143
1144
1145 /*
1146 * record a given inode in the fixup dir so we can check its link
1147 * count when replay is done. The link count is incremented here
1148 * so the inode won't go away until we check it
1149 */
link_to_fixup_dir(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 objectid)1150 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1151 struct btrfs_root *root,
1152 struct btrfs_path *path,
1153 u64 objectid)
1154 {
1155 struct btrfs_key key;
1156 int ret = 0;
1157 struct inode *inode;
1158
1159 inode = read_one_inode(root, objectid);
1160 if (!inode)
1161 return -EIO;
1162
1163 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1164 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1165 key.offset = objectid;
1166
1167 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1168
1169 btrfs_release_path(path);
1170 if (ret == 0) {
1171 btrfs_inc_nlink(inode);
1172 btrfs_update_inode(trans, root, inode);
1173 } else if (ret == -EEXIST) {
1174 ret = 0;
1175 } else {
1176 BUG();
1177 }
1178 iput(inode);
1179
1180 return ret;
1181 }
1182
1183 /*
1184 * when replaying the log for a directory, we only insert names
1185 * for inodes that actually exist. This means an fsync on a directory
1186 * does not implicitly fsync all the new files in it
1187 */
insert_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 dirid,u64 index,char * name,int name_len,u8 type,struct btrfs_key * location)1188 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 struct btrfs_path *path,
1191 u64 dirid, u64 index,
1192 char *name, int name_len, u8 type,
1193 struct btrfs_key *location)
1194 {
1195 struct inode *inode;
1196 struct inode *dir;
1197 int ret;
1198
1199 inode = read_one_inode(root, location->objectid);
1200 if (!inode)
1201 return -ENOENT;
1202
1203 dir = read_one_inode(root, dirid);
1204 if (!dir) {
1205 iput(inode);
1206 return -EIO;
1207 }
1208 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1209
1210 /* FIXME, put inode into FIXUP list */
1211
1212 iput(inode);
1213 iput(dir);
1214 return ret;
1215 }
1216
1217 /*
1218 * take a single entry in a log directory item and replay it into
1219 * the subvolume.
1220 *
1221 * if a conflicting item exists in the subdirectory already,
1222 * the inode it points to is unlinked and put into the link count
1223 * fix up tree.
1224 *
1225 * If a name from the log points to a file or directory that does
1226 * not exist in the FS, it is skipped. fsyncs on directories
1227 * do not force down inodes inside that directory, just changes to the
1228 * names or unlinks in a directory.
1229 */
replay_one_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,struct btrfs_dir_item * di,struct btrfs_key * key)1230 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1231 struct btrfs_root *root,
1232 struct btrfs_path *path,
1233 struct extent_buffer *eb,
1234 struct btrfs_dir_item *di,
1235 struct btrfs_key *key)
1236 {
1237 char *name;
1238 int name_len;
1239 struct btrfs_dir_item *dst_di;
1240 struct btrfs_key found_key;
1241 struct btrfs_key log_key;
1242 struct inode *dir;
1243 u8 log_type;
1244 int exists;
1245 int ret;
1246
1247 dir = read_one_inode(root, key->objectid);
1248 if (!dir)
1249 return -EIO;
1250
1251 name_len = btrfs_dir_name_len(eb, di);
1252 name = kmalloc(name_len, GFP_NOFS);
1253 if (!name)
1254 return -ENOMEM;
1255
1256 log_type = btrfs_dir_type(eb, di);
1257 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1258 name_len);
1259
1260 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1261 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1262 if (exists == 0)
1263 exists = 1;
1264 else
1265 exists = 0;
1266 btrfs_release_path(path);
1267
1268 if (key->type == BTRFS_DIR_ITEM_KEY) {
1269 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1270 name, name_len, 1);
1271 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1272 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1273 key->objectid,
1274 key->offset, name,
1275 name_len, 1);
1276 } else {
1277 BUG();
1278 }
1279 if (IS_ERR_OR_NULL(dst_di)) {
1280 /* we need a sequence number to insert, so we only
1281 * do inserts for the BTRFS_DIR_INDEX_KEY types
1282 */
1283 if (key->type != BTRFS_DIR_INDEX_KEY)
1284 goto out;
1285 goto insert;
1286 }
1287
1288 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1289 /* the existing item matches the logged item */
1290 if (found_key.objectid == log_key.objectid &&
1291 found_key.type == log_key.type &&
1292 found_key.offset == log_key.offset &&
1293 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1294 goto out;
1295 }
1296
1297 /*
1298 * don't drop the conflicting directory entry if the inode
1299 * for the new entry doesn't exist
1300 */
1301 if (!exists)
1302 goto out;
1303
1304 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1305 BUG_ON(ret);
1306
1307 if (key->type == BTRFS_DIR_INDEX_KEY)
1308 goto insert;
1309 out:
1310 btrfs_release_path(path);
1311 kfree(name);
1312 iput(dir);
1313 return 0;
1314
1315 insert:
1316 btrfs_release_path(path);
1317 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1318 name, name_len, log_type, &log_key);
1319
1320 BUG_ON(ret && ret != -ENOENT);
1321 goto out;
1322 }
1323
1324 /*
1325 * find all the names in a directory item and reconcile them into
1326 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1327 * one name in a directory item, but the same code gets used for
1328 * both directory index types
1329 */
replay_one_dir_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * eb,int slot,struct btrfs_key * key)1330 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 struct extent_buffer *eb, int slot,
1334 struct btrfs_key *key)
1335 {
1336 int ret;
1337 u32 item_size = btrfs_item_size_nr(eb, slot);
1338 struct btrfs_dir_item *di;
1339 int name_len;
1340 unsigned long ptr;
1341 unsigned long ptr_end;
1342
1343 ptr = btrfs_item_ptr_offset(eb, slot);
1344 ptr_end = ptr + item_size;
1345 while (ptr < ptr_end) {
1346 di = (struct btrfs_dir_item *)ptr;
1347 if (verify_dir_item(root, eb, di))
1348 return -EIO;
1349 name_len = btrfs_dir_name_len(eb, di);
1350 ret = replay_one_name(trans, root, path, eb, di, key);
1351 BUG_ON(ret);
1352 ptr = (unsigned long)(di + 1);
1353 ptr += name_len;
1354 }
1355 return 0;
1356 }
1357
1358 /*
1359 * directory replay has two parts. There are the standard directory
1360 * items in the log copied from the subvolume, and range items
1361 * created in the log while the subvolume was logged.
1362 *
1363 * The range items tell us which parts of the key space the log
1364 * is authoritative for. During replay, if a key in the subvolume
1365 * directory is in a logged range item, but not actually in the log
1366 * that means it was deleted from the directory before the fsync
1367 * and should be removed.
1368 */
find_dir_range(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,int key_type,u64 * start_ret,u64 * end_ret)1369 static noinline int find_dir_range(struct btrfs_root *root,
1370 struct btrfs_path *path,
1371 u64 dirid, int key_type,
1372 u64 *start_ret, u64 *end_ret)
1373 {
1374 struct btrfs_key key;
1375 u64 found_end;
1376 struct btrfs_dir_log_item *item;
1377 int ret;
1378 int nritems;
1379
1380 if (*start_ret == (u64)-1)
1381 return 1;
1382
1383 key.objectid = dirid;
1384 key.type = key_type;
1385 key.offset = *start_ret;
1386
1387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1388 if (ret < 0)
1389 goto out;
1390 if (ret > 0) {
1391 if (path->slots[0] == 0)
1392 goto out;
1393 path->slots[0]--;
1394 }
1395 if (ret != 0)
1396 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1397
1398 if (key.type != key_type || key.objectid != dirid) {
1399 ret = 1;
1400 goto next;
1401 }
1402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1403 struct btrfs_dir_log_item);
1404 found_end = btrfs_dir_log_end(path->nodes[0], item);
1405
1406 if (*start_ret >= key.offset && *start_ret <= found_end) {
1407 ret = 0;
1408 *start_ret = key.offset;
1409 *end_ret = found_end;
1410 goto out;
1411 }
1412 ret = 1;
1413 next:
1414 /* check the next slot in the tree to see if it is a valid item */
1415 nritems = btrfs_header_nritems(path->nodes[0]);
1416 if (path->slots[0] >= nritems) {
1417 ret = btrfs_next_leaf(root, path);
1418 if (ret)
1419 goto out;
1420 } else {
1421 path->slots[0]++;
1422 }
1423
1424 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1425
1426 if (key.type != key_type || key.objectid != dirid) {
1427 ret = 1;
1428 goto out;
1429 }
1430 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1431 struct btrfs_dir_log_item);
1432 found_end = btrfs_dir_log_end(path->nodes[0], item);
1433 *start_ret = key.offset;
1434 *end_ret = found_end;
1435 ret = 0;
1436 out:
1437 btrfs_release_path(path);
1438 return ret;
1439 }
1440
1441 /*
1442 * this looks for a given directory item in the log. If the directory
1443 * item is not in the log, the item is removed and the inode it points
1444 * to is unlinked
1445 */
check_item_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,struct btrfs_path * log_path,struct inode * dir,struct btrfs_key * dir_key)1446 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1447 struct btrfs_root *root,
1448 struct btrfs_root *log,
1449 struct btrfs_path *path,
1450 struct btrfs_path *log_path,
1451 struct inode *dir,
1452 struct btrfs_key *dir_key)
1453 {
1454 int ret;
1455 struct extent_buffer *eb;
1456 int slot;
1457 u32 item_size;
1458 struct btrfs_dir_item *di;
1459 struct btrfs_dir_item *log_di;
1460 int name_len;
1461 unsigned long ptr;
1462 unsigned long ptr_end;
1463 char *name;
1464 struct inode *inode;
1465 struct btrfs_key location;
1466
1467 again:
1468 eb = path->nodes[0];
1469 slot = path->slots[0];
1470 item_size = btrfs_item_size_nr(eb, slot);
1471 ptr = btrfs_item_ptr_offset(eb, slot);
1472 ptr_end = ptr + item_size;
1473 while (ptr < ptr_end) {
1474 di = (struct btrfs_dir_item *)ptr;
1475 if (verify_dir_item(root, eb, di)) {
1476 ret = -EIO;
1477 goto out;
1478 }
1479
1480 name_len = btrfs_dir_name_len(eb, di);
1481 name = kmalloc(name_len, GFP_NOFS);
1482 if (!name) {
1483 ret = -ENOMEM;
1484 goto out;
1485 }
1486 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1487 name_len);
1488 log_di = NULL;
1489 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
1490 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1491 dir_key->objectid,
1492 name, name_len, 0);
1493 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
1494 log_di = btrfs_lookup_dir_index_item(trans, log,
1495 log_path,
1496 dir_key->objectid,
1497 dir_key->offset,
1498 name, name_len, 0);
1499 }
1500 if (IS_ERR_OR_NULL(log_di)) {
1501 btrfs_dir_item_key_to_cpu(eb, di, &location);
1502 btrfs_release_path(path);
1503 btrfs_release_path(log_path);
1504 inode = read_one_inode(root, location.objectid);
1505 if (!inode) {
1506 kfree(name);
1507 return -EIO;
1508 }
1509
1510 ret = link_to_fixup_dir(trans, root,
1511 path, location.objectid);
1512 BUG_ON(ret);
1513 btrfs_inc_nlink(inode);
1514 ret = btrfs_unlink_inode(trans, root, dir, inode,
1515 name, name_len);
1516 BUG_ON(ret);
1517
1518 btrfs_run_delayed_items(trans, root);
1519
1520 kfree(name);
1521 iput(inode);
1522
1523 /* there might still be more names under this key
1524 * check and repeat if required
1525 */
1526 ret = btrfs_search_slot(NULL, root, dir_key, path,
1527 0, 0);
1528 if (ret == 0)
1529 goto again;
1530 ret = 0;
1531 goto out;
1532 }
1533 btrfs_release_path(log_path);
1534 kfree(name);
1535
1536 ptr = (unsigned long)(di + 1);
1537 ptr += name_len;
1538 }
1539 ret = 0;
1540 out:
1541 btrfs_release_path(path);
1542 btrfs_release_path(log_path);
1543 return ret;
1544 }
1545
1546 /*
1547 * deletion replay happens before we copy any new directory items
1548 * out of the log or out of backreferences from inodes. It
1549 * scans the log to find ranges of keys that log is authoritative for,
1550 * and then scans the directory to find items in those ranges that are
1551 * not present in the log.
1552 *
1553 * Anything we don't find in the log is unlinked and removed from the
1554 * directory.
1555 */
replay_dir_deletes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_root * log,struct btrfs_path * path,u64 dirid,int del_all)1556 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1557 struct btrfs_root *root,
1558 struct btrfs_root *log,
1559 struct btrfs_path *path,
1560 u64 dirid, int del_all)
1561 {
1562 u64 range_start;
1563 u64 range_end;
1564 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1565 int ret = 0;
1566 struct btrfs_key dir_key;
1567 struct btrfs_key found_key;
1568 struct btrfs_path *log_path;
1569 struct inode *dir;
1570
1571 dir_key.objectid = dirid;
1572 dir_key.type = BTRFS_DIR_ITEM_KEY;
1573 log_path = btrfs_alloc_path();
1574 if (!log_path)
1575 return -ENOMEM;
1576
1577 dir = read_one_inode(root, dirid);
1578 /* it isn't an error if the inode isn't there, that can happen
1579 * because we replay the deletes before we copy in the inode item
1580 * from the log
1581 */
1582 if (!dir) {
1583 btrfs_free_path(log_path);
1584 return 0;
1585 }
1586 again:
1587 range_start = 0;
1588 range_end = 0;
1589 while (1) {
1590 if (del_all)
1591 range_end = (u64)-1;
1592 else {
1593 ret = find_dir_range(log, path, dirid, key_type,
1594 &range_start, &range_end);
1595 if (ret != 0)
1596 break;
1597 }
1598
1599 dir_key.offset = range_start;
1600 while (1) {
1601 int nritems;
1602 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1603 0, 0);
1604 if (ret < 0)
1605 goto out;
1606
1607 nritems = btrfs_header_nritems(path->nodes[0]);
1608 if (path->slots[0] >= nritems) {
1609 ret = btrfs_next_leaf(root, path);
1610 if (ret)
1611 break;
1612 }
1613 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1614 path->slots[0]);
1615 if (found_key.objectid != dirid ||
1616 found_key.type != dir_key.type)
1617 goto next_type;
1618
1619 if (found_key.offset > range_end)
1620 break;
1621
1622 ret = check_item_in_log(trans, root, log, path,
1623 log_path, dir,
1624 &found_key);
1625 BUG_ON(ret);
1626 if (found_key.offset == (u64)-1)
1627 break;
1628 dir_key.offset = found_key.offset + 1;
1629 }
1630 btrfs_release_path(path);
1631 if (range_end == (u64)-1)
1632 break;
1633 range_start = range_end + 1;
1634 }
1635
1636 next_type:
1637 ret = 0;
1638 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1639 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1640 dir_key.type = BTRFS_DIR_INDEX_KEY;
1641 btrfs_release_path(path);
1642 goto again;
1643 }
1644 out:
1645 btrfs_release_path(path);
1646 btrfs_free_path(log_path);
1647 iput(dir);
1648 return ret;
1649 }
1650
1651 /*
1652 * the process_func used to replay items from the log tree. This
1653 * gets called in two different stages. The first stage just looks
1654 * for inodes and makes sure they are all copied into the subvolume.
1655 *
1656 * The second stage copies all the other item types from the log into
1657 * the subvolume. The two stage approach is slower, but gets rid of
1658 * lots of complexity around inodes referencing other inodes that exist
1659 * only in the log (references come from either directory items or inode
1660 * back refs).
1661 */
replay_one_buffer(struct btrfs_root * log,struct extent_buffer * eb,struct walk_control * wc,u64 gen)1662 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1663 struct walk_control *wc, u64 gen)
1664 {
1665 int nritems;
1666 struct btrfs_path *path;
1667 struct btrfs_root *root = wc->replay_dest;
1668 struct btrfs_key key;
1669 int level;
1670 int i;
1671 int ret;
1672
1673 btrfs_read_buffer(eb, gen);
1674
1675 level = btrfs_header_level(eb);
1676
1677 if (level != 0)
1678 return 0;
1679
1680 path = btrfs_alloc_path();
1681 if (!path)
1682 return -ENOMEM;
1683
1684 nritems = btrfs_header_nritems(eb);
1685 for (i = 0; i < nritems; i++) {
1686 btrfs_item_key_to_cpu(eb, &key, i);
1687
1688 /* inode keys are done during the first stage */
1689 if (key.type == BTRFS_INODE_ITEM_KEY &&
1690 wc->stage == LOG_WALK_REPLAY_INODES) {
1691 struct btrfs_inode_item *inode_item;
1692 u32 mode;
1693
1694 inode_item = btrfs_item_ptr(eb, i,
1695 struct btrfs_inode_item);
1696 mode = btrfs_inode_mode(eb, inode_item);
1697 if (S_ISDIR(mode)) {
1698 ret = replay_dir_deletes(wc->trans,
1699 root, log, path, key.objectid, 0);
1700 BUG_ON(ret);
1701 }
1702 ret = overwrite_item(wc->trans, root, path,
1703 eb, i, &key);
1704 BUG_ON(ret);
1705
1706 /* for regular files, make sure corresponding
1707 * orhpan item exist. extents past the new EOF
1708 * will be truncated later by orphan cleanup.
1709 */
1710 if (S_ISREG(mode)) {
1711 ret = insert_orphan_item(wc->trans, root,
1712 key.objectid);
1713 BUG_ON(ret);
1714 }
1715
1716 ret = link_to_fixup_dir(wc->trans, root,
1717 path, key.objectid);
1718 BUG_ON(ret);
1719 }
1720 if (wc->stage < LOG_WALK_REPLAY_ALL)
1721 continue;
1722
1723 /* these keys are simply copied */
1724 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1725 ret = overwrite_item(wc->trans, root, path,
1726 eb, i, &key);
1727 BUG_ON(ret);
1728 } else if (key.type == BTRFS_INODE_REF_KEY) {
1729 ret = add_inode_ref(wc->trans, root, log, path,
1730 eb, i, &key);
1731 BUG_ON(ret && ret != -ENOENT);
1732 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1733 ret = replay_one_extent(wc->trans, root, path,
1734 eb, i, &key);
1735 BUG_ON(ret);
1736 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1737 key.type == BTRFS_DIR_INDEX_KEY) {
1738 ret = replay_one_dir_item(wc->trans, root, path,
1739 eb, i, &key);
1740 BUG_ON(ret);
1741 }
1742 }
1743 btrfs_free_path(path);
1744 return 0;
1745 }
1746
walk_down_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)1747 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
1748 struct btrfs_root *root,
1749 struct btrfs_path *path, int *level,
1750 struct walk_control *wc)
1751 {
1752 u64 root_owner;
1753 u64 bytenr;
1754 u64 ptr_gen;
1755 struct extent_buffer *next;
1756 struct extent_buffer *cur;
1757 struct extent_buffer *parent;
1758 u32 blocksize;
1759 int ret = 0;
1760
1761 WARN_ON(*level < 0);
1762 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1763
1764 while (*level > 0) {
1765 WARN_ON(*level < 0);
1766 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1767 cur = path->nodes[*level];
1768
1769 if (btrfs_header_level(cur) != *level)
1770 WARN_ON(1);
1771
1772 if (path->slots[*level] >=
1773 btrfs_header_nritems(cur))
1774 break;
1775
1776 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1777 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1778 blocksize = btrfs_level_size(root, *level - 1);
1779
1780 parent = path->nodes[*level];
1781 root_owner = btrfs_header_owner(parent);
1782
1783 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
1784 if (!next)
1785 return -ENOMEM;
1786
1787 if (*level == 1) {
1788 ret = wc->process_func(root, next, wc, ptr_gen);
1789 if (ret)
1790 return ret;
1791
1792 path->slots[*level]++;
1793 if (wc->free) {
1794 btrfs_read_buffer(next, ptr_gen);
1795
1796 btrfs_tree_lock(next);
1797 btrfs_set_lock_blocking(next);
1798 clean_tree_block(trans, root, next);
1799 btrfs_wait_tree_block_writeback(next);
1800 btrfs_tree_unlock(next);
1801
1802 WARN_ON(root_owner !=
1803 BTRFS_TREE_LOG_OBJECTID);
1804 ret = btrfs_free_and_pin_reserved_extent(root,
1805 bytenr, blocksize);
1806 BUG_ON(ret); /* -ENOMEM or logic errors */
1807 }
1808 free_extent_buffer(next);
1809 continue;
1810 }
1811 btrfs_read_buffer(next, ptr_gen);
1812
1813 WARN_ON(*level <= 0);
1814 if (path->nodes[*level-1])
1815 free_extent_buffer(path->nodes[*level-1]);
1816 path->nodes[*level-1] = next;
1817 *level = btrfs_header_level(next);
1818 path->slots[*level] = 0;
1819 cond_resched();
1820 }
1821 WARN_ON(*level < 0);
1822 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1823
1824 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1825
1826 cond_resched();
1827 return 0;
1828 }
1829
walk_up_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int * level,struct walk_control * wc)1830 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 struct btrfs_path *path, int *level,
1833 struct walk_control *wc)
1834 {
1835 u64 root_owner;
1836 int i;
1837 int slot;
1838 int ret;
1839
1840 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1841 slot = path->slots[i];
1842 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
1843 path->slots[i]++;
1844 *level = i;
1845 WARN_ON(*level == 0);
1846 return 0;
1847 } else {
1848 struct extent_buffer *parent;
1849 if (path->nodes[*level] == root->node)
1850 parent = path->nodes[*level];
1851 else
1852 parent = path->nodes[*level + 1];
1853
1854 root_owner = btrfs_header_owner(parent);
1855 ret = wc->process_func(root, path->nodes[*level], wc,
1856 btrfs_header_generation(path->nodes[*level]));
1857 if (ret)
1858 return ret;
1859
1860 if (wc->free) {
1861 struct extent_buffer *next;
1862
1863 next = path->nodes[*level];
1864
1865 btrfs_tree_lock(next);
1866 btrfs_set_lock_blocking(next);
1867 clean_tree_block(trans, root, next);
1868 btrfs_wait_tree_block_writeback(next);
1869 btrfs_tree_unlock(next);
1870
1871 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
1872 ret = btrfs_free_and_pin_reserved_extent(root,
1873 path->nodes[*level]->start,
1874 path->nodes[*level]->len);
1875 BUG_ON(ret);
1876 }
1877 free_extent_buffer(path->nodes[*level]);
1878 path->nodes[*level] = NULL;
1879 *level = i + 1;
1880 }
1881 }
1882 return 1;
1883 }
1884
1885 /*
1886 * drop the reference count on the tree rooted at 'snap'. This traverses
1887 * the tree freeing any blocks that have a ref count of zero after being
1888 * decremented.
1889 */
walk_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct walk_control * wc)1890 static int walk_log_tree(struct btrfs_trans_handle *trans,
1891 struct btrfs_root *log, struct walk_control *wc)
1892 {
1893 int ret = 0;
1894 int wret;
1895 int level;
1896 struct btrfs_path *path;
1897 int i;
1898 int orig_level;
1899
1900 path = btrfs_alloc_path();
1901 if (!path)
1902 return -ENOMEM;
1903
1904 level = btrfs_header_level(log->node);
1905 orig_level = level;
1906 path->nodes[level] = log->node;
1907 extent_buffer_get(log->node);
1908 path->slots[level] = 0;
1909
1910 while (1) {
1911 wret = walk_down_log_tree(trans, log, path, &level, wc);
1912 if (wret > 0)
1913 break;
1914 if (wret < 0) {
1915 ret = wret;
1916 goto out;
1917 }
1918
1919 wret = walk_up_log_tree(trans, log, path, &level, wc);
1920 if (wret > 0)
1921 break;
1922 if (wret < 0) {
1923 ret = wret;
1924 goto out;
1925 }
1926 }
1927
1928 /* was the root node processed? if not, catch it here */
1929 if (path->nodes[orig_level]) {
1930 ret = wc->process_func(log, path->nodes[orig_level], wc,
1931 btrfs_header_generation(path->nodes[orig_level]));
1932 if (ret)
1933 goto out;
1934 if (wc->free) {
1935 struct extent_buffer *next;
1936
1937 next = path->nodes[orig_level];
1938
1939 btrfs_tree_lock(next);
1940 btrfs_set_lock_blocking(next);
1941 clean_tree_block(trans, log, next);
1942 btrfs_wait_tree_block_writeback(next);
1943 btrfs_tree_unlock(next);
1944
1945 WARN_ON(log->root_key.objectid !=
1946 BTRFS_TREE_LOG_OBJECTID);
1947 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
1948 next->len);
1949 BUG_ON(ret); /* -ENOMEM or logic errors */
1950 }
1951 }
1952
1953 out:
1954 for (i = 0; i <= orig_level; i++) {
1955 if (path->nodes[i]) {
1956 free_extent_buffer(path->nodes[i]);
1957 path->nodes[i] = NULL;
1958 }
1959 }
1960 btrfs_free_path(path);
1961 return ret;
1962 }
1963
1964 /*
1965 * helper function to update the item for a given subvolumes log root
1966 * in the tree of log roots
1967 */
update_log_root(struct btrfs_trans_handle * trans,struct btrfs_root * log)1968 static int update_log_root(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *log)
1970 {
1971 int ret;
1972
1973 if (log->log_transid == 1) {
1974 /* insert root item on the first sync */
1975 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1976 &log->root_key, &log->root_item);
1977 } else {
1978 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1979 &log->root_key, &log->root_item);
1980 }
1981 return ret;
1982 }
1983
wait_log_commit(struct btrfs_trans_handle * trans,struct btrfs_root * root,unsigned long transid)1984 static int wait_log_commit(struct btrfs_trans_handle *trans,
1985 struct btrfs_root *root, unsigned long transid)
1986 {
1987 DEFINE_WAIT(wait);
1988 int index = transid % 2;
1989
1990 /*
1991 * we only allow two pending log transactions at a time,
1992 * so we know that if ours is more than 2 older than the
1993 * current transaction, we're done
1994 */
1995 do {
1996 prepare_to_wait(&root->log_commit_wait[index],
1997 &wait, TASK_UNINTERRUPTIBLE);
1998 mutex_unlock(&root->log_mutex);
1999
2000 if (root->fs_info->last_trans_log_full_commit !=
2001 trans->transid && root->log_transid < transid + 2 &&
2002 atomic_read(&root->log_commit[index]))
2003 schedule();
2004
2005 finish_wait(&root->log_commit_wait[index], &wait);
2006 mutex_lock(&root->log_mutex);
2007 } while (root->fs_info->last_trans_log_full_commit !=
2008 trans->transid && root->log_transid < transid + 2 &&
2009 atomic_read(&root->log_commit[index]));
2010 return 0;
2011 }
2012
wait_for_writer(struct btrfs_trans_handle * trans,struct btrfs_root * root)2013 static void wait_for_writer(struct btrfs_trans_handle *trans,
2014 struct btrfs_root *root)
2015 {
2016 DEFINE_WAIT(wait);
2017 while (root->fs_info->last_trans_log_full_commit !=
2018 trans->transid && atomic_read(&root->log_writers)) {
2019 prepare_to_wait(&root->log_writer_wait,
2020 &wait, TASK_UNINTERRUPTIBLE);
2021 mutex_unlock(&root->log_mutex);
2022 if (root->fs_info->last_trans_log_full_commit !=
2023 trans->transid && atomic_read(&root->log_writers))
2024 schedule();
2025 mutex_lock(&root->log_mutex);
2026 finish_wait(&root->log_writer_wait, &wait);
2027 }
2028 }
2029
2030 /*
2031 * btrfs_sync_log does sends a given tree log down to the disk and
2032 * updates the super blocks to record it. When this call is done,
2033 * you know that any inodes previously logged are safely on disk only
2034 * if it returns 0.
2035 *
2036 * Any other return value means you need to call btrfs_commit_transaction.
2037 * Some of the edge cases for fsyncing directories that have had unlinks
2038 * or renames done in the past mean that sometimes the only safe
2039 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2040 * that has happened.
2041 */
btrfs_sync_log(struct btrfs_trans_handle * trans,struct btrfs_root * root)2042 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2043 struct btrfs_root *root)
2044 {
2045 int index1;
2046 int index2;
2047 int mark;
2048 int ret;
2049 struct btrfs_root *log = root->log_root;
2050 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
2051 unsigned long log_transid = 0;
2052
2053 mutex_lock(&root->log_mutex);
2054 index1 = root->log_transid % 2;
2055 if (atomic_read(&root->log_commit[index1])) {
2056 wait_log_commit(trans, root, root->log_transid);
2057 mutex_unlock(&root->log_mutex);
2058 return 0;
2059 }
2060 atomic_set(&root->log_commit[index1], 1);
2061
2062 /* wait for previous tree log sync to complete */
2063 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2064 wait_log_commit(trans, root, root->log_transid - 1);
2065 while (1) {
2066 unsigned long batch = root->log_batch;
2067 /* when we're on an ssd, just kick the log commit out */
2068 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
2069 mutex_unlock(&root->log_mutex);
2070 schedule_timeout_uninterruptible(1);
2071 mutex_lock(&root->log_mutex);
2072 }
2073 wait_for_writer(trans, root);
2074 if (batch == root->log_batch)
2075 break;
2076 }
2077
2078 /* bail out if we need to do a full commit */
2079 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2080 ret = -EAGAIN;
2081 mutex_unlock(&root->log_mutex);
2082 goto out;
2083 }
2084
2085 log_transid = root->log_transid;
2086 if (log_transid % 2 == 0)
2087 mark = EXTENT_DIRTY;
2088 else
2089 mark = EXTENT_NEW;
2090
2091 /* we start IO on all the marked extents here, but we don't actually
2092 * wait for them until later.
2093 */
2094 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
2095 if (ret) {
2096 btrfs_abort_transaction(trans, root, ret);
2097 mutex_unlock(&root->log_mutex);
2098 goto out;
2099 }
2100
2101 btrfs_set_root_node(&log->root_item, log->node);
2102
2103 root->log_batch = 0;
2104 root->log_transid++;
2105 log->log_transid = root->log_transid;
2106 root->log_start_pid = 0;
2107 smp_mb();
2108 /*
2109 * IO has been started, blocks of the log tree have WRITTEN flag set
2110 * in their headers. new modifications of the log will be written to
2111 * new positions. so it's safe to allow log writers to go in.
2112 */
2113 mutex_unlock(&root->log_mutex);
2114
2115 mutex_lock(&log_root_tree->log_mutex);
2116 log_root_tree->log_batch++;
2117 atomic_inc(&log_root_tree->log_writers);
2118 mutex_unlock(&log_root_tree->log_mutex);
2119
2120 ret = update_log_root(trans, log);
2121
2122 mutex_lock(&log_root_tree->log_mutex);
2123 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2124 smp_mb();
2125 if (waitqueue_active(&log_root_tree->log_writer_wait))
2126 wake_up(&log_root_tree->log_writer_wait);
2127 }
2128
2129 if (ret) {
2130 if (ret != -ENOSPC) {
2131 btrfs_abort_transaction(trans, root, ret);
2132 mutex_unlock(&log_root_tree->log_mutex);
2133 goto out;
2134 }
2135 root->fs_info->last_trans_log_full_commit = trans->transid;
2136 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2137 mutex_unlock(&log_root_tree->log_mutex);
2138 ret = -EAGAIN;
2139 goto out;
2140 }
2141
2142 index2 = log_root_tree->log_transid % 2;
2143 if (atomic_read(&log_root_tree->log_commit[index2])) {
2144 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2145 wait_log_commit(trans, log_root_tree,
2146 log_root_tree->log_transid);
2147 mutex_unlock(&log_root_tree->log_mutex);
2148 ret = 0;
2149 goto out;
2150 }
2151 atomic_set(&log_root_tree->log_commit[index2], 1);
2152
2153 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2154 wait_log_commit(trans, log_root_tree,
2155 log_root_tree->log_transid - 1);
2156 }
2157
2158 wait_for_writer(trans, log_root_tree);
2159
2160 /*
2161 * now that we've moved on to the tree of log tree roots,
2162 * check the full commit flag again
2163 */
2164 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2165 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2166 mutex_unlock(&log_root_tree->log_mutex);
2167 ret = -EAGAIN;
2168 goto out_wake_log_root;
2169 }
2170
2171 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
2172 &log_root_tree->dirty_log_pages,
2173 EXTENT_DIRTY | EXTENT_NEW);
2174 if (ret) {
2175 btrfs_abort_transaction(trans, root, ret);
2176 mutex_unlock(&log_root_tree->log_mutex);
2177 goto out_wake_log_root;
2178 }
2179 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2180
2181 btrfs_set_super_log_root(root->fs_info->super_for_commit,
2182 log_root_tree->node->start);
2183 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
2184 btrfs_header_level(log_root_tree->node));
2185
2186 log_root_tree->log_batch = 0;
2187 log_root_tree->log_transid++;
2188 smp_mb();
2189
2190 mutex_unlock(&log_root_tree->log_mutex);
2191
2192 /*
2193 * nobody else is going to jump in and write the the ctree
2194 * super here because the log_commit atomic below is protecting
2195 * us. We must be called with a transaction handle pinning
2196 * the running transaction open, so a full commit can't hop
2197 * in and cause problems either.
2198 */
2199 btrfs_scrub_pause_super(root);
2200 write_ctree_super(trans, root->fs_info->tree_root, 1);
2201 btrfs_scrub_continue_super(root);
2202 ret = 0;
2203
2204 mutex_lock(&root->log_mutex);
2205 if (root->last_log_commit < log_transid)
2206 root->last_log_commit = log_transid;
2207 mutex_unlock(&root->log_mutex);
2208
2209 out_wake_log_root:
2210 atomic_set(&log_root_tree->log_commit[index2], 0);
2211 smp_mb();
2212 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2213 wake_up(&log_root_tree->log_commit_wait[index2]);
2214 out:
2215 atomic_set(&root->log_commit[index1], 0);
2216 smp_mb();
2217 if (waitqueue_active(&root->log_commit_wait[index1]))
2218 wake_up(&root->log_commit_wait[index1]);
2219 return ret;
2220 }
2221
free_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * log)2222 static void free_log_tree(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *log)
2224 {
2225 int ret;
2226 u64 start;
2227 u64 end;
2228 struct walk_control wc = {
2229 .free = 1,
2230 .process_func = process_one_buffer
2231 };
2232
2233 ret = walk_log_tree(trans, log, &wc);
2234 BUG_ON(ret);
2235
2236 while (1) {
2237 ret = find_first_extent_bit(&log->dirty_log_pages,
2238 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
2239 if (ret)
2240 break;
2241
2242 clear_extent_bits(&log->dirty_log_pages, start, end,
2243 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
2244 }
2245
2246 free_extent_buffer(log->node);
2247 kfree(log);
2248 }
2249
2250 /*
2251 * free all the extents used by the tree log. This should be called
2252 * at commit time of the full transaction
2253 */
btrfs_free_log(struct btrfs_trans_handle * trans,struct btrfs_root * root)2254 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2255 {
2256 if (root->log_root) {
2257 free_log_tree(trans, root->log_root);
2258 root->log_root = NULL;
2259 }
2260 return 0;
2261 }
2262
btrfs_free_log_root_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)2263 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2264 struct btrfs_fs_info *fs_info)
2265 {
2266 if (fs_info->log_root_tree) {
2267 free_log_tree(trans, fs_info->log_root_tree);
2268 fs_info->log_root_tree = NULL;
2269 }
2270 return 0;
2271 }
2272
2273 /*
2274 * If both a file and directory are logged, and unlinks or renames are
2275 * mixed in, we have a few interesting corners:
2276 *
2277 * create file X in dir Y
2278 * link file X to X.link in dir Y
2279 * fsync file X
2280 * unlink file X but leave X.link
2281 * fsync dir Y
2282 *
2283 * After a crash we would expect only X.link to exist. But file X
2284 * didn't get fsync'd again so the log has back refs for X and X.link.
2285 *
2286 * We solve this by removing directory entries and inode backrefs from the
2287 * log when a file that was logged in the current transaction is
2288 * unlinked. Any later fsync will include the updated log entries, and
2289 * we'll be able to reconstruct the proper directory items from backrefs.
2290 *
2291 * This optimizations allows us to avoid relogging the entire inode
2292 * or the entire directory.
2293 */
btrfs_del_dir_entries_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const char * name,int name_len,struct inode * dir,u64 index)2294 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2295 struct btrfs_root *root,
2296 const char *name, int name_len,
2297 struct inode *dir, u64 index)
2298 {
2299 struct btrfs_root *log;
2300 struct btrfs_dir_item *di;
2301 struct btrfs_path *path;
2302 int ret;
2303 int err = 0;
2304 int bytes_del = 0;
2305 u64 dir_ino = btrfs_ino(dir);
2306
2307 if (BTRFS_I(dir)->logged_trans < trans->transid)
2308 return 0;
2309
2310 ret = join_running_log_trans(root);
2311 if (ret)
2312 return 0;
2313
2314 mutex_lock(&BTRFS_I(dir)->log_mutex);
2315
2316 log = root->log_root;
2317 path = btrfs_alloc_path();
2318 if (!path) {
2319 err = -ENOMEM;
2320 goto out_unlock;
2321 }
2322
2323 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
2324 name, name_len, -1);
2325 if (IS_ERR(di)) {
2326 err = PTR_ERR(di);
2327 goto fail;
2328 }
2329 if (di) {
2330 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2331 bytes_del += name_len;
2332 BUG_ON(ret);
2333 }
2334 btrfs_release_path(path);
2335 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
2336 index, name, name_len, -1);
2337 if (IS_ERR(di)) {
2338 err = PTR_ERR(di);
2339 goto fail;
2340 }
2341 if (di) {
2342 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2343 bytes_del += name_len;
2344 BUG_ON(ret);
2345 }
2346
2347 /* update the directory size in the log to reflect the names
2348 * we have removed
2349 */
2350 if (bytes_del) {
2351 struct btrfs_key key;
2352
2353 key.objectid = dir_ino;
2354 key.offset = 0;
2355 key.type = BTRFS_INODE_ITEM_KEY;
2356 btrfs_release_path(path);
2357
2358 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
2359 if (ret < 0) {
2360 err = ret;
2361 goto fail;
2362 }
2363 if (ret == 0) {
2364 struct btrfs_inode_item *item;
2365 u64 i_size;
2366
2367 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2368 struct btrfs_inode_item);
2369 i_size = btrfs_inode_size(path->nodes[0], item);
2370 if (i_size > bytes_del)
2371 i_size -= bytes_del;
2372 else
2373 i_size = 0;
2374 btrfs_set_inode_size(path->nodes[0], item, i_size);
2375 btrfs_mark_buffer_dirty(path->nodes[0]);
2376 } else
2377 ret = 0;
2378 btrfs_release_path(path);
2379 }
2380 fail:
2381 btrfs_free_path(path);
2382 out_unlock:
2383 mutex_unlock(&BTRFS_I(dir)->log_mutex);
2384 if (ret == -ENOSPC) {
2385 root->fs_info->last_trans_log_full_commit = trans->transid;
2386 ret = 0;
2387 } else if (ret < 0)
2388 btrfs_abort_transaction(trans, root, ret);
2389
2390 btrfs_end_log_trans(root);
2391
2392 return err;
2393 }
2394
2395 /* see comments for btrfs_del_dir_entries_in_log */
btrfs_del_inode_ref_in_log(struct btrfs_trans_handle * trans,struct btrfs_root * root,const char * name,int name_len,struct inode * inode,u64 dirid)2396 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2397 struct btrfs_root *root,
2398 const char *name, int name_len,
2399 struct inode *inode, u64 dirid)
2400 {
2401 struct btrfs_root *log;
2402 u64 index;
2403 int ret;
2404
2405 if (BTRFS_I(inode)->logged_trans < trans->transid)
2406 return 0;
2407
2408 ret = join_running_log_trans(root);
2409 if (ret)
2410 return 0;
2411 log = root->log_root;
2412 mutex_lock(&BTRFS_I(inode)->log_mutex);
2413
2414 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
2415 dirid, &index);
2416 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2417 if (ret == -ENOSPC) {
2418 root->fs_info->last_trans_log_full_commit = trans->transid;
2419 ret = 0;
2420 } else if (ret < 0 && ret != -ENOENT)
2421 btrfs_abort_transaction(trans, root, ret);
2422 btrfs_end_log_trans(root);
2423
2424 return ret;
2425 }
2426
2427 /*
2428 * creates a range item in the log for 'dirid'. first_offset and
2429 * last_offset tell us which parts of the key space the log should
2430 * be considered authoritative for.
2431 */
insert_dir_log_key(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,int key_type,u64 dirid,u64 first_offset,u64 last_offset)2432 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2433 struct btrfs_root *log,
2434 struct btrfs_path *path,
2435 int key_type, u64 dirid,
2436 u64 first_offset, u64 last_offset)
2437 {
2438 int ret;
2439 struct btrfs_key key;
2440 struct btrfs_dir_log_item *item;
2441
2442 key.objectid = dirid;
2443 key.offset = first_offset;
2444 if (key_type == BTRFS_DIR_ITEM_KEY)
2445 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2446 else
2447 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2448 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
2449 if (ret)
2450 return ret;
2451
2452 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2453 struct btrfs_dir_log_item);
2454 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2455 btrfs_mark_buffer_dirty(path->nodes[0]);
2456 btrfs_release_path(path);
2457 return 0;
2458 }
2459
2460 /*
2461 * log all the items included in the current transaction for a given
2462 * directory. This also creates the range items in the log tree required
2463 * to replay anything deleted before the fsync
2464 */
log_dir_items(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path,int key_type,u64 min_offset,u64 * last_offset_ret)2465 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root, struct inode *inode,
2467 struct btrfs_path *path,
2468 struct btrfs_path *dst_path, int key_type,
2469 u64 min_offset, u64 *last_offset_ret)
2470 {
2471 struct btrfs_key min_key;
2472 struct btrfs_key max_key;
2473 struct btrfs_root *log = root->log_root;
2474 struct extent_buffer *src;
2475 int err = 0;
2476 int ret;
2477 int i;
2478 int nritems;
2479 u64 first_offset = min_offset;
2480 u64 last_offset = (u64)-1;
2481 u64 ino = btrfs_ino(inode);
2482
2483 log = root->log_root;
2484 max_key.objectid = ino;
2485 max_key.offset = (u64)-1;
2486 max_key.type = key_type;
2487
2488 min_key.objectid = ino;
2489 min_key.type = key_type;
2490 min_key.offset = min_offset;
2491
2492 path->keep_locks = 1;
2493
2494 ret = btrfs_search_forward(root, &min_key, &max_key,
2495 path, 0, trans->transid);
2496
2497 /*
2498 * we didn't find anything from this transaction, see if there
2499 * is anything at all
2500 */
2501 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2502 min_key.objectid = ino;
2503 min_key.type = key_type;
2504 min_key.offset = (u64)-1;
2505 btrfs_release_path(path);
2506 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2507 if (ret < 0) {
2508 btrfs_release_path(path);
2509 return ret;
2510 }
2511 ret = btrfs_previous_item(root, path, ino, key_type);
2512
2513 /* if ret == 0 there are items for this type,
2514 * create a range to tell us the last key of this type.
2515 * otherwise, there are no items in this directory after
2516 * *min_offset, and we create a range to indicate that.
2517 */
2518 if (ret == 0) {
2519 struct btrfs_key tmp;
2520 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2521 path->slots[0]);
2522 if (key_type == tmp.type)
2523 first_offset = max(min_offset, tmp.offset) + 1;
2524 }
2525 goto done;
2526 }
2527
2528 /* go backward to find any previous key */
2529 ret = btrfs_previous_item(root, path, ino, key_type);
2530 if (ret == 0) {
2531 struct btrfs_key tmp;
2532 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2533 if (key_type == tmp.type) {
2534 first_offset = tmp.offset;
2535 ret = overwrite_item(trans, log, dst_path,
2536 path->nodes[0], path->slots[0],
2537 &tmp);
2538 if (ret) {
2539 err = ret;
2540 goto done;
2541 }
2542 }
2543 }
2544 btrfs_release_path(path);
2545
2546 /* find the first key from this transaction again */
2547 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2548 if (ret != 0) {
2549 WARN_ON(1);
2550 goto done;
2551 }
2552
2553 /*
2554 * we have a block from this transaction, log every item in it
2555 * from our directory
2556 */
2557 while (1) {
2558 struct btrfs_key tmp;
2559 src = path->nodes[0];
2560 nritems = btrfs_header_nritems(src);
2561 for (i = path->slots[0]; i < nritems; i++) {
2562 btrfs_item_key_to_cpu(src, &min_key, i);
2563
2564 if (min_key.objectid != ino || min_key.type != key_type)
2565 goto done;
2566 ret = overwrite_item(trans, log, dst_path, src, i,
2567 &min_key);
2568 if (ret) {
2569 err = ret;
2570 goto done;
2571 }
2572 }
2573 path->slots[0] = nritems;
2574
2575 /*
2576 * look ahead to the next item and see if it is also
2577 * from this directory and from this transaction
2578 */
2579 ret = btrfs_next_leaf(root, path);
2580 if (ret == 1) {
2581 last_offset = (u64)-1;
2582 goto done;
2583 }
2584 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2585 if (tmp.objectid != ino || tmp.type != key_type) {
2586 last_offset = (u64)-1;
2587 goto done;
2588 }
2589 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2590 ret = overwrite_item(trans, log, dst_path,
2591 path->nodes[0], path->slots[0],
2592 &tmp);
2593 if (ret)
2594 err = ret;
2595 else
2596 last_offset = tmp.offset;
2597 goto done;
2598 }
2599 }
2600 done:
2601 btrfs_release_path(path);
2602 btrfs_release_path(dst_path);
2603
2604 if (err == 0) {
2605 *last_offset_ret = last_offset;
2606 /*
2607 * insert the log range keys to indicate where the log
2608 * is valid
2609 */
2610 ret = insert_dir_log_key(trans, log, path, key_type,
2611 ino, first_offset, last_offset);
2612 if (ret)
2613 err = ret;
2614 }
2615 return err;
2616 }
2617
2618 /*
2619 * logging directories is very similar to logging inodes, We find all the items
2620 * from the current transaction and write them to the log.
2621 *
2622 * The recovery code scans the directory in the subvolume, and if it finds a
2623 * key in the range logged that is not present in the log tree, then it means
2624 * that dir entry was unlinked during the transaction.
2625 *
2626 * In order for that scan to work, we must include one key smaller than
2627 * the smallest logged by this transaction and one key larger than the largest
2628 * key logged by this transaction.
2629 */
log_directory_changes(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode,struct btrfs_path * path,struct btrfs_path * dst_path)2630 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2631 struct btrfs_root *root, struct inode *inode,
2632 struct btrfs_path *path,
2633 struct btrfs_path *dst_path)
2634 {
2635 u64 min_key;
2636 u64 max_key;
2637 int ret;
2638 int key_type = BTRFS_DIR_ITEM_KEY;
2639
2640 again:
2641 min_key = 0;
2642 max_key = 0;
2643 while (1) {
2644 ret = log_dir_items(trans, root, inode, path,
2645 dst_path, key_type, min_key,
2646 &max_key);
2647 if (ret)
2648 return ret;
2649 if (max_key == (u64)-1)
2650 break;
2651 min_key = max_key + 1;
2652 }
2653
2654 if (key_type == BTRFS_DIR_ITEM_KEY) {
2655 key_type = BTRFS_DIR_INDEX_KEY;
2656 goto again;
2657 }
2658 return 0;
2659 }
2660
2661 /*
2662 * a helper function to drop items from the log before we relog an
2663 * inode. max_key_type indicates the highest item type to remove.
2664 * This cannot be run for file data extents because it does not
2665 * free the extents they point to.
2666 */
drop_objectid_items(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * path,u64 objectid,int max_key_type)2667 static int drop_objectid_items(struct btrfs_trans_handle *trans,
2668 struct btrfs_root *log,
2669 struct btrfs_path *path,
2670 u64 objectid, int max_key_type)
2671 {
2672 int ret;
2673 struct btrfs_key key;
2674 struct btrfs_key found_key;
2675
2676 key.objectid = objectid;
2677 key.type = max_key_type;
2678 key.offset = (u64)-1;
2679
2680 while (1) {
2681 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
2682 BUG_ON(ret == 0);
2683 if (ret < 0)
2684 break;
2685
2686 if (path->slots[0] == 0)
2687 break;
2688
2689 path->slots[0]--;
2690 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2691 path->slots[0]);
2692
2693 if (found_key.objectid != objectid)
2694 break;
2695
2696 ret = btrfs_del_item(trans, log, path);
2697 if (ret)
2698 break;
2699 btrfs_release_path(path);
2700 }
2701 btrfs_release_path(path);
2702 return ret;
2703 }
2704
copy_items(struct btrfs_trans_handle * trans,struct btrfs_root * log,struct btrfs_path * dst_path,struct extent_buffer * src,int start_slot,int nr,int inode_only)2705 static noinline int copy_items(struct btrfs_trans_handle *trans,
2706 struct btrfs_root *log,
2707 struct btrfs_path *dst_path,
2708 struct extent_buffer *src,
2709 int start_slot, int nr, int inode_only)
2710 {
2711 unsigned long src_offset;
2712 unsigned long dst_offset;
2713 struct btrfs_file_extent_item *extent;
2714 struct btrfs_inode_item *inode_item;
2715 int ret;
2716 struct btrfs_key *ins_keys;
2717 u32 *ins_sizes;
2718 char *ins_data;
2719 int i;
2720 struct list_head ordered_sums;
2721
2722 INIT_LIST_HEAD(&ordered_sums);
2723
2724 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2725 nr * sizeof(u32), GFP_NOFS);
2726 if (!ins_data)
2727 return -ENOMEM;
2728
2729 ins_sizes = (u32 *)ins_data;
2730 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2731
2732 for (i = 0; i < nr; i++) {
2733 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2734 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2735 }
2736 ret = btrfs_insert_empty_items(trans, log, dst_path,
2737 ins_keys, ins_sizes, nr);
2738 if (ret) {
2739 kfree(ins_data);
2740 return ret;
2741 }
2742
2743 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
2744 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2745 dst_path->slots[0]);
2746
2747 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2748
2749 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2750 src_offset, ins_sizes[i]);
2751
2752 if (inode_only == LOG_INODE_EXISTS &&
2753 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2754 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2755 dst_path->slots[0],
2756 struct btrfs_inode_item);
2757 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2758
2759 /* set the generation to zero so the recover code
2760 * can tell the difference between an logging
2761 * just to say 'this inode exists' and a logging
2762 * to say 'update this inode with these values'
2763 */
2764 btrfs_set_inode_generation(dst_path->nodes[0],
2765 inode_item, 0);
2766 }
2767 /* take a reference on file data extents so that truncates
2768 * or deletes of this inode don't have to relog the inode
2769 * again
2770 */
2771 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2772 int found_type;
2773 extent = btrfs_item_ptr(src, start_slot + i,
2774 struct btrfs_file_extent_item);
2775
2776 if (btrfs_file_extent_generation(src, extent) < trans->transid)
2777 continue;
2778
2779 found_type = btrfs_file_extent_type(src, extent);
2780 if (found_type == BTRFS_FILE_EXTENT_REG ||
2781 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
2782 u64 ds, dl, cs, cl;
2783 ds = btrfs_file_extent_disk_bytenr(src,
2784 extent);
2785 /* ds == 0 is a hole */
2786 if (ds == 0)
2787 continue;
2788
2789 dl = btrfs_file_extent_disk_num_bytes(src,
2790 extent);
2791 cs = btrfs_file_extent_offset(src, extent);
2792 cl = btrfs_file_extent_num_bytes(src,
2793 extent);
2794 if (btrfs_file_extent_compression(src,
2795 extent)) {
2796 cs = 0;
2797 cl = dl;
2798 }
2799
2800 ret = btrfs_lookup_csums_range(
2801 log->fs_info->csum_root,
2802 ds + cs, ds + cs + cl - 1,
2803 &ordered_sums, 0);
2804 BUG_ON(ret);
2805 }
2806 }
2807 }
2808
2809 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2810 btrfs_release_path(dst_path);
2811 kfree(ins_data);
2812
2813 /*
2814 * we have to do this after the loop above to avoid changing the
2815 * log tree while trying to change the log tree.
2816 */
2817 ret = 0;
2818 while (!list_empty(&ordered_sums)) {
2819 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2820 struct btrfs_ordered_sum,
2821 list);
2822 if (!ret)
2823 ret = btrfs_csum_file_blocks(trans, log, sums);
2824 list_del(&sums->list);
2825 kfree(sums);
2826 }
2827 return ret;
2828 }
2829
2830 /* log a single inode in the tree log.
2831 * At least one parent directory for this inode must exist in the tree
2832 * or be logged already.
2833 *
2834 * Any items from this inode changed by the current transaction are copied
2835 * to the log tree. An extra reference is taken on any extents in this
2836 * file, allowing us to avoid a whole pile of corner cases around logging
2837 * blocks that have been removed from the tree.
2838 *
2839 * See LOG_INODE_ALL and related defines for a description of what inode_only
2840 * does.
2841 *
2842 * This handles both files and directories.
2843 */
btrfs_log_inode(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode,int inode_only)2844 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
2845 struct btrfs_root *root, struct inode *inode,
2846 int inode_only)
2847 {
2848 struct btrfs_path *path;
2849 struct btrfs_path *dst_path;
2850 struct btrfs_key min_key;
2851 struct btrfs_key max_key;
2852 struct btrfs_root *log = root->log_root;
2853 struct extent_buffer *src = NULL;
2854 int err = 0;
2855 int ret;
2856 int nritems;
2857 int ins_start_slot = 0;
2858 int ins_nr;
2859 u64 ino = btrfs_ino(inode);
2860
2861 log = root->log_root;
2862
2863 path = btrfs_alloc_path();
2864 if (!path)
2865 return -ENOMEM;
2866 dst_path = btrfs_alloc_path();
2867 if (!dst_path) {
2868 btrfs_free_path(path);
2869 return -ENOMEM;
2870 }
2871
2872 min_key.objectid = ino;
2873 min_key.type = BTRFS_INODE_ITEM_KEY;
2874 min_key.offset = 0;
2875
2876 max_key.objectid = ino;
2877
2878 /* today the code can only do partial logging of directories */
2879 if (!S_ISDIR(inode->i_mode))
2880 inode_only = LOG_INODE_ALL;
2881
2882 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2883 max_key.type = BTRFS_XATTR_ITEM_KEY;
2884 else
2885 max_key.type = (u8)-1;
2886 max_key.offset = (u64)-1;
2887
2888 ret = btrfs_commit_inode_delayed_items(trans, inode);
2889 if (ret) {
2890 btrfs_free_path(path);
2891 btrfs_free_path(dst_path);
2892 return ret;
2893 }
2894
2895 mutex_lock(&BTRFS_I(inode)->log_mutex);
2896
2897 /*
2898 * a brute force approach to making sure we get the most uptodate
2899 * copies of everything.
2900 */
2901 if (S_ISDIR(inode->i_mode)) {
2902 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2903
2904 if (inode_only == LOG_INODE_EXISTS)
2905 max_key_type = BTRFS_XATTR_ITEM_KEY;
2906 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
2907 } else {
2908 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2909 }
2910 if (ret) {
2911 err = ret;
2912 goto out_unlock;
2913 }
2914 path->keep_locks = 1;
2915
2916 while (1) {
2917 ins_nr = 0;
2918 ret = btrfs_search_forward(root, &min_key, &max_key,
2919 path, 0, trans->transid);
2920 if (ret != 0)
2921 break;
2922 again:
2923 /* note, ins_nr might be > 0 here, cleanup outside the loop */
2924 if (min_key.objectid != ino)
2925 break;
2926 if (min_key.type > max_key.type)
2927 break;
2928
2929 src = path->nodes[0];
2930 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2931 ins_nr++;
2932 goto next_slot;
2933 } else if (!ins_nr) {
2934 ins_start_slot = path->slots[0];
2935 ins_nr = 1;
2936 goto next_slot;
2937 }
2938
2939 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2940 ins_nr, inode_only);
2941 if (ret) {
2942 err = ret;
2943 goto out_unlock;
2944 }
2945 ins_nr = 1;
2946 ins_start_slot = path->slots[0];
2947 next_slot:
2948
2949 nritems = btrfs_header_nritems(path->nodes[0]);
2950 path->slots[0]++;
2951 if (path->slots[0] < nritems) {
2952 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2953 path->slots[0]);
2954 goto again;
2955 }
2956 if (ins_nr) {
2957 ret = copy_items(trans, log, dst_path, src,
2958 ins_start_slot,
2959 ins_nr, inode_only);
2960 if (ret) {
2961 err = ret;
2962 goto out_unlock;
2963 }
2964 ins_nr = 0;
2965 }
2966 btrfs_release_path(path);
2967
2968 if (min_key.offset < (u64)-1)
2969 min_key.offset++;
2970 else if (min_key.type < (u8)-1)
2971 min_key.type++;
2972 else if (min_key.objectid < (u64)-1)
2973 min_key.objectid++;
2974 else
2975 break;
2976 }
2977 if (ins_nr) {
2978 ret = copy_items(trans, log, dst_path, src,
2979 ins_start_slot,
2980 ins_nr, inode_only);
2981 if (ret) {
2982 err = ret;
2983 goto out_unlock;
2984 }
2985 ins_nr = 0;
2986 }
2987 WARN_ON(ins_nr);
2988 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
2989 btrfs_release_path(path);
2990 btrfs_release_path(dst_path);
2991 ret = log_directory_changes(trans, root, inode, path, dst_path);
2992 if (ret) {
2993 err = ret;
2994 goto out_unlock;
2995 }
2996 }
2997 BTRFS_I(inode)->logged_trans = trans->transid;
2998 out_unlock:
2999 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3000
3001 btrfs_free_path(path);
3002 btrfs_free_path(dst_path);
3003 return err;
3004 }
3005
3006 /*
3007 * follow the dentry parent pointers up the chain and see if any
3008 * of the directories in it require a full commit before they can
3009 * be logged. Returns zero if nothing special needs to be done or 1 if
3010 * a full commit is required.
3011 */
check_parent_dirs_for_sync(struct btrfs_trans_handle * trans,struct inode * inode,struct dentry * parent,struct super_block * sb,u64 last_committed)3012 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3013 struct inode *inode,
3014 struct dentry *parent,
3015 struct super_block *sb,
3016 u64 last_committed)
3017 {
3018 int ret = 0;
3019 struct btrfs_root *root;
3020 struct dentry *old_parent = NULL;
3021
3022 /*
3023 * for regular files, if its inode is already on disk, we don't
3024 * have to worry about the parents at all. This is because
3025 * we can use the last_unlink_trans field to record renames
3026 * and other fun in this file.
3027 */
3028 if (S_ISREG(inode->i_mode) &&
3029 BTRFS_I(inode)->generation <= last_committed &&
3030 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3031 goto out;
3032
3033 if (!S_ISDIR(inode->i_mode)) {
3034 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3035 goto out;
3036 inode = parent->d_inode;
3037 }
3038
3039 while (1) {
3040 BTRFS_I(inode)->logged_trans = trans->transid;
3041 smp_mb();
3042
3043 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3044 root = BTRFS_I(inode)->root;
3045
3046 /*
3047 * make sure any commits to the log are forced
3048 * to be full commits
3049 */
3050 root->fs_info->last_trans_log_full_commit =
3051 trans->transid;
3052 ret = 1;
3053 break;
3054 }
3055
3056 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3057 break;
3058
3059 if (IS_ROOT(parent))
3060 break;
3061
3062 parent = dget_parent(parent);
3063 dput(old_parent);
3064 old_parent = parent;
3065 inode = parent->d_inode;
3066
3067 }
3068 dput(old_parent);
3069 out:
3070 return ret;
3071 }
3072
inode_in_log(struct btrfs_trans_handle * trans,struct inode * inode)3073 static int inode_in_log(struct btrfs_trans_handle *trans,
3074 struct inode *inode)
3075 {
3076 struct btrfs_root *root = BTRFS_I(inode)->root;
3077 int ret = 0;
3078
3079 mutex_lock(&root->log_mutex);
3080 if (BTRFS_I(inode)->logged_trans == trans->transid &&
3081 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
3082 ret = 1;
3083 mutex_unlock(&root->log_mutex);
3084 return ret;
3085 }
3086
3087
3088 /*
3089 * helper function around btrfs_log_inode to make sure newly created
3090 * parent directories also end up in the log. A minimal inode and backref
3091 * only logging is done of any parent directories that are older than
3092 * the last committed transaction
3093 */
btrfs_log_inode_parent(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct inode * inode,struct dentry * parent,int exists_only)3094 int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3095 struct btrfs_root *root, struct inode *inode,
3096 struct dentry *parent, int exists_only)
3097 {
3098 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
3099 struct super_block *sb;
3100 struct dentry *old_parent = NULL;
3101 int ret = 0;
3102 u64 last_committed = root->fs_info->last_trans_committed;
3103
3104 sb = inode->i_sb;
3105
3106 if (btrfs_test_opt(root, NOTREELOG)) {
3107 ret = 1;
3108 goto end_no_trans;
3109 }
3110
3111 if (root->fs_info->last_trans_log_full_commit >
3112 root->fs_info->last_trans_committed) {
3113 ret = 1;
3114 goto end_no_trans;
3115 }
3116
3117 if (root != BTRFS_I(inode)->root ||
3118 btrfs_root_refs(&root->root_item) == 0) {
3119 ret = 1;
3120 goto end_no_trans;
3121 }
3122
3123 ret = check_parent_dirs_for_sync(trans, inode, parent,
3124 sb, last_committed);
3125 if (ret)
3126 goto end_no_trans;
3127
3128 if (inode_in_log(trans, inode)) {
3129 ret = BTRFS_NO_LOG_SYNC;
3130 goto end_no_trans;
3131 }
3132
3133 ret = start_log_trans(trans, root);
3134 if (ret)
3135 goto end_trans;
3136
3137 ret = btrfs_log_inode(trans, root, inode, inode_only);
3138 if (ret)
3139 goto end_trans;
3140
3141 /*
3142 * for regular files, if its inode is already on disk, we don't
3143 * have to worry about the parents at all. This is because
3144 * we can use the last_unlink_trans field to record renames
3145 * and other fun in this file.
3146 */
3147 if (S_ISREG(inode->i_mode) &&
3148 BTRFS_I(inode)->generation <= last_committed &&
3149 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3150 ret = 0;
3151 goto end_trans;
3152 }
3153
3154 inode_only = LOG_INODE_EXISTS;
3155 while (1) {
3156 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3157 break;
3158
3159 inode = parent->d_inode;
3160 if (root != BTRFS_I(inode)->root)
3161 break;
3162
3163 if (BTRFS_I(inode)->generation >
3164 root->fs_info->last_trans_committed) {
3165 ret = btrfs_log_inode(trans, root, inode, inode_only);
3166 if (ret)
3167 goto end_trans;
3168 }
3169 if (IS_ROOT(parent))
3170 break;
3171
3172 parent = dget_parent(parent);
3173 dput(old_parent);
3174 old_parent = parent;
3175 }
3176 ret = 0;
3177 end_trans:
3178 dput(old_parent);
3179 if (ret < 0) {
3180 BUG_ON(ret != -ENOSPC);
3181 root->fs_info->last_trans_log_full_commit = trans->transid;
3182 ret = 1;
3183 }
3184 btrfs_end_log_trans(root);
3185 end_no_trans:
3186 return ret;
3187 }
3188
3189 /*
3190 * it is not safe to log dentry if the chunk root has added new
3191 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3192 * If this returns 1, you must commit the transaction to safely get your
3193 * data on disk.
3194 */
btrfs_log_dentry_safe(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct dentry * dentry)3195 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3196 struct btrfs_root *root, struct dentry *dentry)
3197 {
3198 struct dentry *parent = dget_parent(dentry);
3199 int ret;
3200
3201 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3202 dput(parent);
3203
3204 return ret;
3205 }
3206
3207 /*
3208 * should be called during mount to recover any replay any log trees
3209 * from the FS
3210 */
btrfs_recover_log_trees(struct btrfs_root * log_root_tree)3211 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3212 {
3213 int ret;
3214 struct btrfs_path *path;
3215 struct btrfs_trans_handle *trans;
3216 struct btrfs_key key;
3217 struct btrfs_key found_key;
3218 struct btrfs_key tmp_key;
3219 struct btrfs_root *log;
3220 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3221 struct walk_control wc = {
3222 .process_func = process_one_buffer,
3223 .stage = 0,
3224 };
3225
3226 path = btrfs_alloc_path();
3227 if (!path)
3228 return -ENOMEM;
3229
3230 fs_info->log_root_recovering = 1;
3231
3232 trans = btrfs_start_transaction(fs_info->tree_root, 0);
3233 if (IS_ERR(trans)) {
3234 ret = PTR_ERR(trans);
3235 goto error;
3236 }
3237
3238 wc.trans = trans;
3239 wc.pin = 1;
3240
3241 ret = walk_log_tree(trans, log_root_tree, &wc);
3242 if (ret) {
3243 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3244 "recovering log root tree.");
3245 goto error;
3246 }
3247
3248 again:
3249 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3250 key.offset = (u64)-1;
3251 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3252
3253 while (1) {
3254 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3255
3256 if (ret < 0) {
3257 btrfs_error(fs_info, ret,
3258 "Couldn't find tree log root.");
3259 goto error;
3260 }
3261 if (ret > 0) {
3262 if (path->slots[0] == 0)
3263 break;
3264 path->slots[0]--;
3265 }
3266 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3267 path->slots[0]);
3268 btrfs_release_path(path);
3269 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3270 break;
3271
3272 log = btrfs_read_fs_root_no_radix(log_root_tree,
3273 &found_key);
3274 if (IS_ERR(log)) {
3275 ret = PTR_ERR(log);
3276 btrfs_error(fs_info, ret,
3277 "Couldn't read tree log root.");
3278 goto error;
3279 }
3280
3281 tmp_key.objectid = found_key.offset;
3282 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3283 tmp_key.offset = (u64)-1;
3284
3285 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
3286 if (IS_ERR(wc.replay_dest)) {
3287 ret = PTR_ERR(wc.replay_dest);
3288 btrfs_error(fs_info, ret, "Couldn't read target root "
3289 "for tree log recovery.");
3290 goto error;
3291 }
3292
3293 wc.replay_dest->log_root = log;
3294 btrfs_record_root_in_trans(trans, wc.replay_dest);
3295 ret = walk_log_tree(trans, log, &wc);
3296 BUG_ON(ret);
3297
3298 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3299 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3300 path);
3301 BUG_ON(ret);
3302 }
3303
3304 key.offset = found_key.offset - 1;
3305 wc.replay_dest->log_root = NULL;
3306 free_extent_buffer(log->node);
3307 free_extent_buffer(log->commit_root);
3308 kfree(log);
3309
3310 if (found_key.offset == 0)
3311 break;
3312 }
3313 btrfs_release_path(path);
3314
3315 /* step one is to pin it all, step two is to replay just inodes */
3316 if (wc.pin) {
3317 wc.pin = 0;
3318 wc.process_func = replay_one_buffer;
3319 wc.stage = LOG_WALK_REPLAY_INODES;
3320 goto again;
3321 }
3322 /* step three is to replay everything */
3323 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3324 wc.stage++;
3325 goto again;
3326 }
3327
3328 btrfs_free_path(path);
3329
3330 free_extent_buffer(log_root_tree->node);
3331 log_root_tree->log_root = NULL;
3332 fs_info->log_root_recovering = 0;
3333
3334 /* step 4: commit the transaction, which also unpins the blocks */
3335 btrfs_commit_transaction(trans, fs_info->tree_root);
3336
3337 kfree(log_root_tree);
3338 return 0;
3339
3340 error:
3341 btrfs_free_path(path);
3342 return ret;
3343 }
3344
3345 /*
3346 * there are some corner cases where we want to force a full
3347 * commit instead of allowing a directory to be logged.
3348 *
3349 * They revolve around files there were unlinked from the directory, and
3350 * this function updates the parent directory so that a full commit is
3351 * properly done if it is fsync'd later after the unlinks are done.
3352 */
btrfs_record_unlink_dir(struct btrfs_trans_handle * trans,struct inode * dir,struct inode * inode,int for_rename)3353 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3354 struct inode *dir, struct inode *inode,
3355 int for_rename)
3356 {
3357 /*
3358 * when we're logging a file, if it hasn't been renamed
3359 * or unlinked, and its inode is fully committed on disk,
3360 * we don't have to worry about walking up the directory chain
3361 * to log its parents.
3362 *
3363 * So, we use the last_unlink_trans field to put this transid
3364 * into the file. When the file is logged we check it and
3365 * don't log the parents if the file is fully on disk.
3366 */
3367 if (S_ISREG(inode->i_mode))
3368 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3369
3370 /*
3371 * if this directory was already logged any new
3372 * names for this file/dir will get recorded
3373 */
3374 smp_mb();
3375 if (BTRFS_I(dir)->logged_trans == trans->transid)
3376 return;
3377
3378 /*
3379 * if the inode we're about to unlink was logged,
3380 * the log will be properly updated for any new names
3381 */
3382 if (BTRFS_I(inode)->logged_trans == trans->transid)
3383 return;
3384
3385 /*
3386 * when renaming files across directories, if the directory
3387 * there we're unlinking from gets fsync'd later on, there's
3388 * no way to find the destination directory later and fsync it
3389 * properly. So, we have to be conservative and force commits
3390 * so the new name gets discovered.
3391 */
3392 if (for_rename)
3393 goto record;
3394
3395 /* we can safely do the unlink without any special recording */
3396 return;
3397
3398 record:
3399 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3400 }
3401
3402 /*
3403 * Call this after adding a new name for a file and it will properly
3404 * update the log to reflect the new name.
3405 *
3406 * It will return zero if all goes well, and it will return 1 if a
3407 * full transaction commit is required.
3408 */
btrfs_log_new_name(struct btrfs_trans_handle * trans,struct inode * inode,struct inode * old_dir,struct dentry * parent)3409 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3410 struct inode *inode, struct inode *old_dir,
3411 struct dentry *parent)
3412 {
3413 struct btrfs_root * root = BTRFS_I(inode)->root;
3414
3415 /*
3416 * this will force the logging code to walk the dentry chain
3417 * up for the file
3418 */
3419 if (S_ISREG(inode->i_mode))
3420 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3421
3422 /*
3423 * if this inode hasn't been logged and directory we're renaming it
3424 * from hasn't been logged, we don't need to log it
3425 */
3426 if (BTRFS_I(inode)->logged_trans <=
3427 root->fs_info->last_trans_committed &&
3428 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3429 root->fs_info->last_trans_committed))
3430 return 0;
3431
3432 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3433 }
3434
3435