]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/tree-log.c
Btrfs: kfree()ing ERR_PTRs
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / tree-log.c
CommitLineData
e02119d5
CM
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>
5a0e3ad6 20#include <linux/slab.h>
c6adc9cc 21#include <linux/blkdev.h>
5dc562c5 22#include <linux/list_sort.h>
995946dd 23#include "tree-log.h"
e02119d5
CM
24#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
f186373f 27#include "backref.h"
f186373f 28#include "hash.h"
e02119d5
CM
29
30/* magic values for the inode_only field in btrfs_log_inode:
31 *
32 * LOG_INODE_ALL means to log everything
33 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 * during log replay
35 */
36#define LOG_INODE_ALL 0
37#define LOG_INODE_EXISTS 1
38
12fcfd22
CM
39/*
40 * directory trouble cases
41 *
42 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
43 * log, we must force a full commit before doing an fsync of the directory
44 * where the unlink was done.
45 * ---> record transid of last unlink/rename per directory
46 *
47 * mkdir foo/some_dir
48 * normal commit
49 * rename foo/some_dir foo2/some_dir
50 * mkdir foo/some_dir
51 * fsync foo/some_dir/some_file
52 *
53 * The fsync above will unlink the original some_dir without recording
54 * it in its new location (foo2). After a crash, some_dir will be gone
55 * unless the fsync of some_file forces a full commit
56 *
57 * 2) we must log any new names for any file or dir that is in the fsync
58 * log. ---> check inode while renaming/linking.
59 *
60 * 2a) we must log any new names for any file or dir during rename
61 * when the directory they are being removed from was logged.
62 * ---> check inode and old parent dir during rename
63 *
64 * 2a is actually the more important variant. With the extra logging
65 * a crash might unlink the old name without recreating the new one
66 *
67 * 3) after a crash, we must go through any directories with a link count
68 * of zero and redo the rm -rf
69 *
70 * mkdir f1/foo
71 * normal commit
72 * rm -rf f1/foo
73 * fsync(f1)
74 *
75 * The directory f1 was fully removed from the FS, but fsync was never
76 * called on f1, only its parent dir. After a crash the rm -rf must
77 * be replayed. This must be able to recurse down the entire
78 * directory tree. The inode link count fixup code takes care of the
79 * ugly details.
80 */
81
e02119d5
CM
82/*
83 * stages for the tree walking. The first
84 * stage (0) is to only pin down the blocks we find
85 * the second stage (1) is to make sure that all the inodes
86 * we find in the log are created in the subvolume.
87 *
88 * The last stage is to deal with directories and links and extents
89 * and all the other fun semantics
90 */
91#define LOG_WALK_PIN_ONLY 0
92#define LOG_WALK_REPLAY_INODES 1
dd8e7217
JB
93#define LOG_WALK_REPLAY_DIR_INDEX 2
94#define LOG_WALK_REPLAY_ALL 3
e02119d5 95
12fcfd22 96static int btrfs_log_inode(struct btrfs_trans_handle *trans,
e02119d5
CM
97 struct btrfs_root *root, struct inode *inode,
98 int inode_only);
ec051c0f
YZ
99static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
100 struct btrfs_root *root,
101 struct btrfs_path *path, u64 objectid);
12fcfd22
CM
102static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct btrfs_root *log,
105 struct btrfs_path *path,
106 u64 dirid, int del_all);
e02119d5
CM
107
108/*
109 * tree logging is a special write ahead log used to make sure that
110 * fsyncs and O_SYNCs can happen without doing full tree commits.
111 *
112 * Full tree commits are expensive because they require commonly
113 * modified blocks to be recowed, creating many dirty pages in the
114 * extent tree an 4x-6x higher write load than ext3.
115 *
116 * Instead of doing a tree commit on every fsync, we use the
117 * key ranges and transaction ids to find items for a given file or directory
118 * that have changed in this transaction. Those items are copied into
119 * a special tree (one per subvolume root), that tree is written to disk
120 * and then the fsync is considered complete.
121 *
122 * After a crash, items are copied out of the log-tree back into the
123 * subvolume tree. Any file data extents found are recorded in the extent
124 * allocation tree, and the log-tree freed.
125 *
126 * The log tree is read three times, once to pin down all the extents it is
127 * using in ram and once, once to create all the inodes logged in the tree
128 * and once to do all the other items.
129 */
130
e02119d5
CM
131/*
132 * start a sub transaction and setup the log tree
133 * this increments the log tree writer count to make the people
134 * syncing the tree wait for us to finish
135 */
136static int start_log_trans(struct btrfs_trans_handle *trans,
8b050d35
MX
137 struct btrfs_root *root,
138 struct btrfs_log_ctx *ctx)
e02119d5 139{
8b050d35 140 int index;
e02119d5 141 int ret;
7237f183
YZ
142
143 mutex_lock(&root->log_mutex);
144 if (root->log_root) {
995946dd 145 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
50471a38
MX
146 ret = -EAGAIN;
147 goto out;
148 }
ff782e0a
JB
149 if (!root->log_start_pid) {
150 root->log_start_pid = current->pid;
27cdeb70 151 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
ff782e0a 152 } else if (root->log_start_pid != current->pid) {
27cdeb70 153 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
ff782e0a
JB
154 }
155
2ecb7923 156 atomic_inc(&root->log_batch);
7237f183 157 atomic_inc(&root->log_writers);
8b050d35
MX
158 if (ctx) {
159 index = root->log_transid % 2;
160 list_add_tail(&ctx->list, &root->log_ctxs[index]);
d1433deb 161 ctx->log_transid = root->log_transid;
8b050d35 162 }
7237f183
YZ
163 mutex_unlock(&root->log_mutex);
164 return 0;
165 }
e87ac136
MX
166
167 ret = 0;
e02119d5 168 mutex_lock(&root->fs_info->tree_log_mutex);
e87ac136 169 if (!root->fs_info->log_root_tree)
e02119d5 170 ret = btrfs_init_log_root_tree(trans, root->fs_info);
e87ac136
MX
171 mutex_unlock(&root->fs_info->tree_log_mutex);
172 if (ret)
173 goto out;
174
175 if (!root->log_root) {
e02119d5 176 ret = btrfs_add_log_tree(trans, root);
4a500fd1 177 if (ret)
e87ac136 178 goto out;
e02119d5 179 }
27cdeb70 180 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
e87ac136 181 root->log_start_pid = current->pid;
2ecb7923 182 atomic_inc(&root->log_batch);
7237f183 183 atomic_inc(&root->log_writers);
8b050d35
MX
184 if (ctx) {
185 index = root->log_transid % 2;
186 list_add_tail(&ctx->list, &root->log_ctxs[index]);
d1433deb 187 ctx->log_transid = root->log_transid;
8b050d35 188 }
e87ac136 189out:
7237f183 190 mutex_unlock(&root->log_mutex);
e87ac136 191 return ret;
e02119d5
CM
192}
193
194/*
195 * returns 0 if there was a log transaction running and we were able
196 * to join, or returns -ENOENT if there were not transactions
197 * in progress
198 */
199static int join_running_log_trans(struct btrfs_root *root)
200{
201 int ret = -ENOENT;
202
203 smp_mb();
204 if (!root->log_root)
205 return -ENOENT;
206
7237f183 207 mutex_lock(&root->log_mutex);
e02119d5
CM
208 if (root->log_root) {
209 ret = 0;
7237f183 210 atomic_inc(&root->log_writers);
e02119d5 211 }
7237f183 212 mutex_unlock(&root->log_mutex);
e02119d5
CM
213 return ret;
214}
215
12fcfd22
CM
216/*
217 * This either makes the current running log transaction wait
218 * until you call btrfs_end_log_trans() or it makes any future
219 * log transactions wait until you call btrfs_end_log_trans()
220 */
221int btrfs_pin_log_trans(struct btrfs_root *root)
222{
223 int ret = -ENOENT;
224
225 mutex_lock(&root->log_mutex);
226 atomic_inc(&root->log_writers);
227 mutex_unlock(&root->log_mutex);
228 return ret;
229}
230
e02119d5
CM
231/*
232 * indicate we're done making changes to the log tree
233 * and wake up anyone waiting to do a sync
234 */
143bede5 235void btrfs_end_log_trans(struct btrfs_root *root)
e02119d5 236{
7237f183
YZ
237 if (atomic_dec_and_test(&root->log_writers)) {
238 smp_mb();
239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
241 }
e02119d5
CM
242}
243
244
245/*
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
250 */
251struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
254 */
255 int free;
256
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
259 */
260 int write;
261
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
264 */
265 int wait;
266
267 /* pin only walk, we record which extents on disk belong to the
268 * log trees
269 */
270 int pin;
271
272 /* what stage of the replay code we're currently in */
273 int stage;
274
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
277
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
280
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
284 * inside it
285 */
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
288};
289
290/*
291 * process_func used to pin down extents, write them or wait on them
292 */
293static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
296{
b50c6e25
JB
297 int ret = 0;
298
8c2a1a30
JB
299 /*
300 * If this fs is mixed then we need to be able to process the leaves to
301 * pin down any logged extents, so we have to read the block.
302 */
303 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
304 ret = btrfs_read_buffer(eb, gen);
305 if (ret)
306 return ret;
307 }
308
04018de5 309 if (wc->pin)
b50c6e25
JB
310 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
311 eb->start, eb->len);
e02119d5 312
b50c6e25 313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
8c2a1a30
JB
314 if (wc->pin && btrfs_header_level(eb) == 0)
315 ret = btrfs_exclude_logged_extents(log, eb);
e02119d5
CM
316 if (wc->write)
317 btrfs_write_tree_block(eb);
318 if (wc->wait)
319 btrfs_wait_tree_block_writeback(eb);
320 }
b50c6e25 321 return ret;
e02119d5
CM
322}
323
324/*
325 * Item overwrite used by replay and tree logging. eb, slot and key all refer
326 * to the src data we are copying out.
327 *
328 * root is the tree we are copying into, and path is a scratch
329 * path for use in this function (it should be released on entry and
330 * will be released on exit).
331 *
332 * If the key is already in the destination tree the existing item is
333 * overwritten. If the existing item isn't big enough, it is extended.
334 * If it is too large, it is truncated.
335 *
336 * If the key isn't in the destination yet, a new item is inserted.
337 */
338static noinline int overwrite_item(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root,
340 struct btrfs_path *path,
341 struct extent_buffer *eb, int slot,
342 struct btrfs_key *key)
343{
344 int ret;
345 u32 item_size;
346 u64 saved_i_size = 0;
347 int save_old_i_size = 0;
348 unsigned long src_ptr;
349 unsigned long dst_ptr;
350 int overwrite_root = 0;
4bc4bee4 351 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
e02119d5
CM
352
353 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
354 overwrite_root = 1;
355
356 item_size = btrfs_item_size_nr(eb, slot);
357 src_ptr = btrfs_item_ptr_offset(eb, slot);
358
359 /* look for the key in the destination tree */
360 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
4bc4bee4
JB
361 if (ret < 0)
362 return ret;
363
e02119d5
CM
364 if (ret == 0) {
365 char *src_copy;
366 char *dst_copy;
367 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
368 path->slots[0]);
369 if (dst_size != item_size)
370 goto insert;
371
372 if (item_size == 0) {
b3b4aa74 373 btrfs_release_path(path);
e02119d5
CM
374 return 0;
375 }
376 dst_copy = kmalloc(item_size, GFP_NOFS);
377 src_copy = kmalloc(item_size, GFP_NOFS);
2a29edc6 378 if (!dst_copy || !src_copy) {
b3b4aa74 379 btrfs_release_path(path);
2a29edc6 380 kfree(dst_copy);
381 kfree(src_copy);
382 return -ENOMEM;
383 }
e02119d5
CM
384
385 read_extent_buffer(eb, src_copy, src_ptr, item_size);
386
387 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
388 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
389 item_size);
390 ret = memcmp(dst_copy, src_copy, item_size);
391
392 kfree(dst_copy);
393 kfree(src_copy);
394 /*
395 * they have the same contents, just return, this saves
396 * us from cowing blocks in the destination tree and doing
397 * extra writes that may not have been done by a previous
398 * sync
399 */
400 if (ret == 0) {
b3b4aa74 401 btrfs_release_path(path);
e02119d5
CM
402 return 0;
403 }
404
4bc4bee4
JB
405 /*
406 * We need to load the old nbytes into the inode so when we
407 * replay the extents we've logged we get the right nbytes.
408 */
409 if (inode_item) {
410 struct btrfs_inode_item *item;
411 u64 nbytes;
d555438b 412 u32 mode;
4bc4bee4
JB
413
414 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
415 struct btrfs_inode_item);
416 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
417 item = btrfs_item_ptr(eb, slot,
418 struct btrfs_inode_item);
419 btrfs_set_inode_nbytes(eb, item, nbytes);
d555438b
JB
420
421 /*
422 * If this is a directory we need to reset the i_size to
423 * 0 so that we can set it up properly when replaying
424 * the rest of the items in this log.
425 */
426 mode = btrfs_inode_mode(eb, item);
427 if (S_ISDIR(mode))
428 btrfs_set_inode_size(eb, item, 0);
4bc4bee4
JB
429 }
430 } else if (inode_item) {
431 struct btrfs_inode_item *item;
d555438b 432 u32 mode;
4bc4bee4
JB
433
434 /*
435 * New inode, set nbytes to 0 so that the nbytes comes out
436 * properly when we replay the extents.
437 */
438 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
439 btrfs_set_inode_nbytes(eb, item, 0);
d555438b
JB
440
441 /*
442 * If this is a directory we need to reset the i_size to 0 so
443 * that we can set it up properly when replaying the rest of
444 * the items in this log.
445 */
446 mode = btrfs_inode_mode(eb, item);
447 if (S_ISDIR(mode))
448 btrfs_set_inode_size(eb, item, 0);
e02119d5
CM
449 }
450insert:
b3b4aa74 451 btrfs_release_path(path);
e02119d5
CM
452 /* try to insert the key into the destination tree */
453 ret = btrfs_insert_empty_item(trans, root, path,
454 key, item_size);
455
456 /* make sure any existing item is the correct size */
457 if (ret == -EEXIST) {
458 u32 found_size;
459 found_size = btrfs_item_size_nr(path->nodes[0],
460 path->slots[0]);
143bede5 461 if (found_size > item_size)
afe5fea7 462 btrfs_truncate_item(root, path, item_size, 1);
143bede5 463 else if (found_size < item_size)
4b90c680 464 btrfs_extend_item(root, path,
143bede5 465 item_size - found_size);
e02119d5 466 } else if (ret) {
4a500fd1 467 return ret;
e02119d5
CM
468 }
469 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
470 path->slots[0]);
471
472 /* don't overwrite an existing inode if the generation number
473 * was logged as zero. This is done when the tree logging code
474 * is just logging an inode to make sure it exists after recovery.
475 *
476 * Also, don't overwrite i_size on directories during replay.
477 * log replay inserts and removes directory items based on the
478 * state of the tree found in the subvolume, and i_size is modified
479 * as it goes
480 */
481 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
482 struct btrfs_inode_item *src_item;
483 struct btrfs_inode_item *dst_item;
484
485 src_item = (struct btrfs_inode_item *)src_ptr;
486 dst_item = (struct btrfs_inode_item *)dst_ptr;
487
488 if (btrfs_inode_generation(eb, src_item) == 0)
489 goto no_copy;
490
491 if (overwrite_root &&
492 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
493 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
494 save_old_i_size = 1;
495 saved_i_size = btrfs_inode_size(path->nodes[0],
496 dst_item);
497 }
498 }
499
500 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
501 src_ptr, item_size);
502
503 if (save_old_i_size) {
504 struct btrfs_inode_item *dst_item;
505 dst_item = (struct btrfs_inode_item *)dst_ptr;
506 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
507 }
508
509 /* make sure the generation is filled in */
510 if (key->type == BTRFS_INODE_ITEM_KEY) {
511 struct btrfs_inode_item *dst_item;
512 dst_item = (struct btrfs_inode_item *)dst_ptr;
513 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
514 btrfs_set_inode_generation(path->nodes[0], dst_item,
515 trans->transid);
516 }
517 }
518no_copy:
519 btrfs_mark_buffer_dirty(path->nodes[0]);
b3b4aa74 520 btrfs_release_path(path);
e02119d5
CM
521 return 0;
522}
523
524/*
525 * simple helper to read an inode off the disk from a given root
526 * This can only be called for subvolume roots and not for the log
527 */
528static noinline struct inode *read_one_inode(struct btrfs_root *root,
529 u64 objectid)
530{
5d4f98a2 531 struct btrfs_key key;
e02119d5 532 struct inode *inode;
e02119d5 533
5d4f98a2
YZ
534 key.objectid = objectid;
535 key.type = BTRFS_INODE_ITEM_KEY;
536 key.offset = 0;
73f73415 537 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
5d4f98a2
YZ
538 if (IS_ERR(inode)) {
539 inode = NULL;
540 } else if (is_bad_inode(inode)) {
e02119d5
CM
541 iput(inode);
542 inode = NULL;
543 }
544 return inode;
545}
546
547/* replays a single extent in 'eb' at 'slot' with 'key' into the
548 * subvolume 'root'. path is released on entry and should be released
549 * on exit.
550 *
551 * extents in the log tree have not been allocated out of the extent
552 * tree yet. So, this completes the allocation, taking a reference
553 * as required if the extent already exists or creating a new extent
554 * if it isn't in the extent allocation tree yet.
555 *
556 * The extent is inserted into the file, dropping any existing extents
557 * from the file that overlap the new one.
558 */
559static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
560 struct btrfs_root *root,
561 struct btrfs_path *path,
562 struct extent_buffer *eb, int slot,
563 struct btrfs_key *key)
564{
565 int found_type;
e02119d5 566 u64 extent_end;
e02119d5 567 u64 start = key->offset;
4bc4bee4 568 u64 nbytes = 0;
e02119d5
CM
569 struct btrfs_file_extent_item *item;
570 struct inode *inode = NULL;
571 unsigned long size;
572 int ret = 0;
573
574 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
575 found_type = btrfs_file_extent_type(eb, item);
576
d899e052 577 if (found_type == BTRFS_FILE_EXTENT_REG ||
4bc4bee4
JB
578 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
579 nbytes = btrfs_file_extent_num_bytes(eb, item);
580 extent_end = start + nbytes;
581
582 /*
583 * We don't add to the inodes nbytes if we are prealloc or a
584 * hole.
585 */
586 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
587 nbytes = 0;
588 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
514ac8ad 589 size = btrfs_file_extent_inline_len(eb, slot, item);
4bc4bee4 590 nbytes = btrfs_file_extent_ram_bytes(eb, item);
fda2832f 591 extent_end = ALIGN(start + size, root->sectorsize);
e02119d5
CM
592 } else {
593 ret = 0;
594 goto out;
595 }
596
597 inode = read_one_inode(root, key->objectid);
598 if (!inode) {
599 ret = -EIO;
600 goto out;
601 }
602
603 /*
604 * first check to see if we already have this extent in the
605 * file. This must be done before the btrfs_drop_extents run
606 * so we don't try to drop this extent.
607 */
33345d01 608 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
e02119d5
CM
609 start, 0);
610
d899e052
YZ
611 if (ret == 0 &&
612 (found_type == BTRFS_FILE_EXTENT_REG ||
613 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
e02119d5
CM
614 struct btrfs_file_extent_item cmp1;
615 struct btrfs_file_extent_item cmp2;
616 struct btrfs_file_extent_item *existing;
617 struct extent_buffer *leaf;
618
619 leaf = path->nodes[0];
620 existing = btrfs_item_ptr(leaf, path->slots[0],
621 struct btrfs_file_extent_item);
622
623 read_extent_buffer(eb, &cmp1, (unsigned long)item,
624 sizeof(cmp1));
625 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
626 sizeof(cmp2));
627
628 /*
629 * we already have a pointer to this exact extent,
630 * we don't have to do anything
631 */
632 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
b3b4aa74 633 btrfs_release_path(path);
e02119d5
CM
634 goto out;
635 }
636 }
b3b4aa74 637 btrfs_release_path(path);
e02119d5
CM
638
639 /* drop any overlapping extents */
2671485d 640 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
3650860b
JB
641 if (ret)
642 goto out;
e02119d5 643
07d400a6
YZ
644 if (found_type == BTRFS_FILE_EXTENT_REG ||
645 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5d4f98a2 646 u64 offset;
07d400a6
YZ
647 unsigned long dest_offset;
648 struct btrfs_key ins;
649
650 ret = btrfs_insert_empty_item(trans, root, path, key,
651 sizeof(*item));
3650860b
JB
652 if (ret)
653 goto out;
07d400a6
YZ
654 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
655 path->slots[0]);
656 copy_extent_buffer(path->nodes[0], eb, dest_offset,
657 (unsigned long)item, sizeof(*item));
658
659 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
660 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
661 ins.type = BTRFS_EXTENT_ITEM_KEY;
5d4f98a2 662 offset = key->offset - btrfs_file_extent_offset(eb, item);
07d400a6
YZ
663
664 if (ins.objectid > 0) {
665 u64 csum_start;
666 u64 csum_end;
667 LIST_HEAD(ordered_sums);
668 /*
669 * is this extent already allocated in the extent
670 * allocation tree? If so, just add a reference
671 */
672 ret = btrfs_lookup_extent(root, ins.objectid,
673 ins.offset);
674 if (ret == 0) {
675 ret = btrfs_inc_extent_ref(trans, root,
676 ins.objectid, ins.offset,
5d4f98a2 677 0, root->root_key.objectid,
66d7e7f0 678 key->objectid, offset, 0);
b50c6e25
JB
679 if (ret)
680 goto out;
07d400a6
YZ
681 } else {
682 /*
683 * insert the extent pointer in the extent
684 * allocation tree
685 */
5d4f98a2
YZ
686 ret = btrfs_alloc_logged_file_extent(trans,
687 root, root->root_key.objectid,
688 key->objectid, offset, &ins);
b50c6e25
JB
689 if (ret)
690 goto out;
07d400a6 691 }
b3b4aa74 692 btrfs_release_path(path);
07d400a6
YZ
693
694 if (btrfs_file_extent_compression(eb, item)) {
695 csum_start = ins.objectid;
696 csum_end = csum_start + ins.offset;
697 } else {
698 csum_start = ins.objectid +
699 btrfs_file_extent_offset(eb, item);
700 csum_end = csum_start +
701 btrfs_file_extent_num_bytes(eb, item);
702 }
703
704 ret = btrfs_lookup_csums_range(root->log_root,
705 csum_start, csum_end - 1,
a2de733c 706 &ordered_sums, 0);
3650860b
JB
707 if (ret)
708 goto out;
07d400a6
YZ
709 while (!list_empty(&ordered_sums)) {
710 struct btrfs_ordered_sum *sums;
711 sums = list_entry(ordered_sums.next,
712 struct btrfs_ordered_sum,
713 list);
3650860b
JB
714 if (!ret)
715 ret = btrfs_csum_file_blocks(trans,
07d400a6
YZ
716 root->fs_info->csum_root,
717 sums);
07d400a6
YZ
718 list_del(&sums->list);
719 kfree(sums);
720 }
3650860b
JB
721 if (ret)
722 goto out;
07d400a6 723 } else {
b3b4aa74 724 btrfs_release_path(path);
07d400a6
YZ
725 }
726 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
727 /* inline extents are easy, we just overwrite them */
728 ret = overwrite_item(trans, root, path, eb, slot, key);
3650860b
JB
729 if (ret)
730 goto out;
07d400a6 731 }
e02119d5 732
4bc4bee4 733 inode_add_bytes(inode, nbytes);
b9959295 734 ret = btrfs_update_inode(trans, root, inode);
e02119d5
CM
735out:
736 if (inode)
737 iput(inode);
738 return ret;
739}
740
741/*
742 * when cleaning up conflicts between the directory names in the
743 * subvolume, directory names in the log and directory names in the
744 * inode back references, we may have to unlink inodes from directories.
745 *
746 * This is a helper function to do the unlink of a specific directory
747 * item
748 */
749static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
750 struct btrfs_root *root,
751 struct btrfs_path *path,
752 struct inode *dir,
753 struct btrfs_dir_item *di)
754{
755 struct inode *inode;
756 char *name;
757 int name_len;
758 struct extent_buffer *leaf;
759 struct btrfs_key location;
760 int ret;
761
762 leaf = path->nodes[0];
763
764 btrfs_dir_item_key_to_cpu(leaf, di, &location);
765 name_len = btrfs_dir_name_len(leaf, di);
766 name = kmalloc(name_len, GFP_NOFS);
2a29edc6 767 if (!name)
768 return -ENOMEM;
769
e02119d5 770 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
b3b4aa74 771 btrfs_release_path(path);
e02119d5
CM
772
773 inode = read_one_inode(root, location.objectid);
c00e9493 774 if (!inode) {
3650860b
JB
775 ret = -EIO;
776 goto out;
c00e9493 777 }
e02119d5 778
ec051c0f 779 ret = link_to_fixup_dir(trans, root, path, location.objectid);
3650860b
JB
780 if (ret)
781 goto out;
12fcfd22 782
e02119d5 783 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3650860b
JB
784 if (ret)
785 goto out;
ada9af21
FDBM
786 else
787 ret = btrfs_run_delayed_items(trans, root);
3650860b 788out:
e02119d5 789 kfree(name);
e02119d5
CM
790 iput(inode);
791 return ret;
792}
793
794/*
795 * helper function to see if a given name and sequence number found
796 * in an inode back reference are already in a directory and correctly
797 * point to this inode
798 */
799static noinline int inode_in_dir(struct btrfs_root *root,
800 struct btrfs_path *path,
801 u64 dirid, u64 objectid, u64 index,
802 const char *name, int name_len)
803{
804 struct btrfs_dir_item *di;
805 struct btrfs_key location;
806 int match = 0;
807
808 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
809 index, name, name_len, 0);
810 if (di && !IS_ERR(di)) {
811 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
812 if (location.objectid != objectid)
813 goto out;
814 } else
815 goto out;
b3b4aa74 816 btrfs_release_path(path);
e02119d5
CM
817
818 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
819 if (di && !IS_ERR(di)) {
820 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
821 if (location.objectid != objectid)
822 goto out;
823 } else
824 goto out;
825 match = 1;
826out:
b3b4aa74 827 btrfs_release_path(path);
e02119d5
CM
828 return match;
829}
830
831/*
832 * helper function to check a log tree for a named back reference in
833 * an inode. This is used to decide if a back reference that is
834 * found in the subvolume conflicts with what we find in the log.
835 *
836 * inode backreferences may have multiple refs in a single item,
837 * during replay we process one reference at a time, and we don't
838 * want to delete valid links to a file from the subvolume if that
839 * link is also in the log.
840 */
841static noinline int backref_in_log(struct btrfs_root *log,
842 struct btrfs_key *key,
f186373f 843 u64 ref_objectid,
e02119d5
CM
844 char *name, int namelen)
845{
846 struct btrfs_path *path;
847 struct btrfs_inode_ref *ref;
848 unsigned long ptr;
849 unsigned long ptr_end;
850 unsigned long name_ptr;
851 int found_name_len;
852 int item_size;
853 int ret;
854 int match = 0;
855
856 path = btrfs_alloc_path();
2a29edc6 857 if (!path)
858 return -ENOMEM;
859
e02119d5
CM
860 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
861 if (ret != 0)
862 goto out;
863
e02119d5 864 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
f186373f
MF
865
866 if (key->type == BTRFS_INODE_EXTREF_KEY) {
867 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
868 name, namelen, NULL))
869 match = 1;
870
871 goto out;
872 }
873
874 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
e02119d5
CM
875 ptr_end = ptr + item_size;
876 while (ptr < ptr_end) {
877 ref = (struct btrfs_inode_ref *)ptr;
878 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
879 if (found_name_len == namelen) {
880 name_ptr = (unsigned long)(ref + 1);
881 ret = memcmp_extent_buffer(path->nodes[0], name,
882 name_ptr, namelen);
883 if (ret == 0) {
884 match = 1;
885 goto out;
886 }
887 }
888 ptr = (unsigned long)(ref + 1) + found_name_len;
889 }
890out:
891 btrfs_free_path(path);
892 return match;
893}
894
5a1d7843 895static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
e02119d5 896 struct btrfs_root *root,
e02119d5 897 struct btrfs_path *path,
5a1d7843
JS
898 struct btrfs_root *log_root,
899 struct inode *dir, struct inode *inode,
5a1d7843 900 struct extent_buffer *eb,
f186373f
MF
901 u64 inode_objectid, u64 parent_objectid,
902 u64 ref_index, char *name, int namelen,
903 int *search_done)
e02119d5 904{
34f3e4f2 905 int ret;
f186373f
MF
906 char *victim_name;
907 int victim_name_len;
908 struct extent_buffer *leaf;
5a1d7843 909 struct btrfs_dir_item *di;
f186373f
MF
910 struct btrfs_key search_key;
911 struct btrfs_inode_extref *extref;
c622ae60 912
f186373f
MF
913again:
914 /* Search old style refs */
915 search_key.objectid = inode_objectid;
916 search_key.type = BTRFS_INODE_REF_KEY;
917 search_key.offset = parent_objectid;
918 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
e02119d5 919 if (ret == 0) {
e02119d5
CM
920 struct btrfs_inode_ref *victim_ref;
921 unsigned long ptr;
922 unsigned long ptr_end;
f186373f
MF
923
924 leaf = path->nodes[0];
e02119d5
CM
925
926 /* are we trying to overwrite a back ref for the root directory
927 * if so, just jump out, we're done
928 */
f186373f 929 if (search_key.objectid == search_key.offset)
5a1d7843 930 return 1;
e02119d5
CM
931
932 /* check all the names in this back reference to see
933 * if they are in the log. if so, we allow them to stay
934 * otherwise they must be unlinked as a conflict
935 */
936 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
937 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
d397712b 938 while (ptr < ptr_end) {
e02119d5
CM
939 victim_ref = (struct btrfs_inode_ref *)ptr;
940 victim_name_len = btrfs_inode_ref_name_len(leaf,
941 victim_ref);
942 victim_name = kmalloc(victim_name_len, GFP_NOFS);
3650860b
JB
943 if (!victim_name)
944 return -ENOMEM;
e02119d5
CM
945
946 read_extent_buffer(leaf, victim_name,
947 (unsigned long)(victim_ref + 1),
948 victim_name_len);
949
f186373f
MF
950 if (!backref_in_log(log_root, &search_key,
951 parent_objectid,
952 victim_name,
e02119d5 953 victim_name_len)) {
8b558c5f 954 inc_nlink(inode);
b3b4aa74 955 btrfs_release_path(path);
12fcfd22 956
e02119d5
CM
957 ret = btrfs_unlink_inode(trans, root, dir,
958 inode, victim_name,
959 victim_name_len);
f186373f 960 kfree(victim_name);
3650860b
JB
961 if (ret)
962 return ret;
ada9af21
FDBM
963 ret = btrfs_run_delayed_items(trans, root);
964 if (ret)
965 return ret;
f186373f
MF
966 *search_done = 1;
967 goto again;
e02119d5
CM
968 }
969 kfree(victim_name);
f186373f 970
e02119d5
CM
971 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
972 }
e02119d5 973
c622ae60 974 /*
975 * NOTE: we have searched root tree and checked the
976 * coresponding ref, it does not need to check again.
977 */
5a1d7843 978 *search_done = 1;
e02119d5 979 }
b3b4aa74 980 btrfs_release_path(path);
e02119d5 981
f186373f
MF
982 /* Same search but for extended refs */
983 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
984 inode_objectid, parent_objectid, 0,
985 0);
986 if (!IS_ERR_OR_NULL(extref)) {
987 u32 item_size;
988 u32 cur_offset = 0;
989 unsigned long base;
990 struct inode *victim_parent;
991
992 leaf = path->nodes[0];
993
994 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
995 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
996
997 while (cur_offset < item_size) {
998 extref = (struct btrfs_inode_extref *)base + cur_offset;
999
1000 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1001
1002 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1003 goto next;
1004
1005 victim_name = kmalloc(victim_name_len, GFP_NOFS);
3650860b
JB
1006 if (!victim_name)
1007 return -ENOMEM;
f186373f
MF
1008 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1009 victim_name_len);
1010
1011 search_key.objectid = inode_objectid;
1012 search_key.type = BTRFS_INODE_EXTREF_KEY;
1013 search_key.offset = btrfs_extref_hash(parent_objectid,
1014 victim_name,
1015 victim_name_len);
1016 ret = 0;
1017 if (!backref_in_log(log_root, &search_key,
1018 parent_objectid, victim_name,
1019 victim_name_len)) {
1020 ret = -ENOENT;
1021 victim_parent = read_one_inode(root,
1022 parent_objectid);
1023 if (victim_parent) {
8b558c5f 1024 inc_nlink(inode);
f186373f
MF
1025 btrfs_release_path(path);
1026
1027 ret = btrfs_unlink_inode(trans, root,
1028 victim_parent,
1029 inode,
1030 victim_name,
1031 victim_name_len);
ada9af21
FDBM
1032 if (!ret)
1033 ret = btrfs_run_delayed_items(
1034 trans, root);
f186373f 1035 }
f186373f
MF
1036 iput(victim_parent);
1037 kfree(victim_name);
3650860b
JB
1038 if (ret)
1039 return ret;
f186373f
MF
1040 *search_done = 1;
1041 goto again;
1042 }
1043 kfree(victim_name);
3650860b
JB
1044 if (ret)
1045 return ret;
f186373f
MF
1046next:
1047 cur_offset += victim_name_len + sizeof(*extref);
1048 }
1049 *search_done = 1;
1050 }
1051 btrfs_release_path(path);
1052
34f3e4f2 1053 /* look for a conflicting sequence number */
1054 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
f186373f 1055 ref_index, name, namelen, 0);
34f3e4f2 1056 if (di && !IS_ERR(di)) {
1057 ret = drop_one_dir_item(trans, root, path, dir, di);
3650860b
JB
1058 if (ret)
1059 return ret;
34f3e4f2 1060 }
1061 btrfs_release_path(path);
1062
1063 /* look for a conflicing name */
1064 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1065 name, namelen, 0);
1066 if (di && !IS_ERR(di)) {
1067 ret = drop_one_dir_item(trans, root, path, dir, di);
3650860b
JB
1068 if (ret)
1069 return ret;
34f3e4f2 1070 }
1071 btrfs_release_path(path);
1072
5a1d7843
JS
1073 return 0;
1074}
e02119d5 1075
f186373f
MF
1076static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1077 u32 *namelen, char **name, u64 *index,
1078 u64 *parent_objectid)
1079{
1080 struct btrfs_inode_extref *extref;
1081
1082 extref = (struct btrfs_inode_extref *)ref_ptr;
1083
1084 *namelen = btrfs_inode_extref_name_len(eb, extref);
1085 *name = kmalloc(*namelen, GFP_NOFS);
1086 if (*name == NULL)
1087 return -ENOMEM;
1088
1089 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1090 *namelen);
1091
1092 *index = btrfs_inode_extref_index(eb, extref);
1093 if (parent_objectid)
1094 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1095
1096 return 0;
1097}
1098
1099static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1100 u32 *namelen, char **name, u64 *index)
1101{
1102 struct btrfs_inode_ref *ref;
1103
1104 ref = (struct btrfs_inode_ref *)ref_ptr;
1105
1106 *namelen = btrfs_inode_ref_name_len(eb, ref);
1107 *name = kmalloc(*namelen, GFP_NOFS);
1108 if (*name == NULL)
1109 return -ENOMEM;
1110
1111 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1112
1113 *index = btrfs_inode_ref_index(eb, ref);
1114
1115 return 0;
1116}
1117
5a1d7843
JS
1118/*
1119 * replay one inode back reference item found in the log tree.
1120 * eb, slot and key refer to the buffer and key found in the log tree.
1121 * root is the destination we are replaying into, and path is for temp
1122 * use by this function. (it should be released on return).
1123 */
1124static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1125 struct btrfs_root *root,
1126 struct btrfs_root *log,
1127 struct btrfs_path *path,
1128 struct extent_buffer *eb, int slot,
1129 struct btrfs_key *key)
1130{
03b2f08b
GB
1131 struct inode *dir = NULL;
1132 struct inode *inode = NULL;
5a1d7843
JS
1133 unsigned long ref_ptr;
1134 unsigned long ref_end;
03b2f08b 1135 char *name = NULL;
5a1d7843
JS
1136 int namelen;
1137 int ret;
1138 int search_done = 0;
f186373f
MF
1139 int log_ref_ver = 0;
1140 u64 parent_objectid;
1141 u64 inode_objectid;
f46dbe3d 1142 u64 ref_index = 0;
f186373f
MF
1143 int ref_struct_size;
1144
1145 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1146 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1147
1148 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1149 struct btrfs_inode_extref *r;
1150
1151 ref_struct_size = sizeof(struct btrfs_inode_extref);
1152 log_ref_ver = 1;
1153 r = (struct btrfs_inode_extref *)ref_ptr;
1154 parent_objectid = btrfs_inode_extref_parent(eb, r);
1155 } else {
1156 ref_struct_size = sizeof(struct btrfs_inode_ref);
1157 parent_objectid = key->offset;
1158 }
1159 inode_objectid = key->objectid;
e02119d5 1160
5a1d7843
JS
1161 /*
1162 * it is possible that we didn't log all the parent directories
1163 * for a given inode. If we don't find the dir, just don't
1164 * copy the back ref in. The link count fixup code will take
1165 * care of the rest
1166 */
f186373f 1167 dir = read_one_inode(root, parent_objectid);
03b2f08b
GB
1168 if (!dir) {
1169 ret = -ENOENT;
1170 goto out;
1171 }
5a1d7843 1172
f186373f 1173 inode = read_one_inode(root, inode_objectid);
5a1d7843 1174 if (!inode) {
03b2f08b
GB
1175 ret = -EIO;
1176 goto out;
5a1d7843
JS
1177 }
1178
5a1d7843 1179 while (ref_ptr < ref_end) {
f186373f
MF
1180 if (log_ref_ver) {
1181 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1182 &ref_index, &parent_objectid);
1183 /*
1184 * parent object can change from one array
1185 * item to another.
1186 */
1187 if (!dir)
1188 dir = read_one_inode(root, parent_objectid);
03b2f08b
GB
1189 if (!dir) {
1190 ret = -ENOENT;
1191 goto out;
1192 }
f186373f
MF
1193 } else {
1194 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1195 &ref_index);
1196 }
1197 if (ret)
03b2f08b 1198 goto out;
5a1d7843
JS
1199
1200 /* if we already have a perfect match, we're done */
1201 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
f186373f 1202 ref_index, name, namelen)) {
5a1d7843
JS
1203 /*
1204 * look for a conflicting back reference in the
1205 * metadata. if we find one we have to unlink that name
1206 * of the file before we add our new link. Later on, we
1207 * overwrite any existing back reference, and we don't
1208 * want to create dangling pointers in the directory.
1209 */
1210
1211 if (!search_done) {
1212 ret = __add_inode_ref(trans, root, path, log,
f186373f
MF
1213 dir, inode, eb,
1214 inode_objectid,
1215 parent_objectid,
1216 ref_index, name, namelen,
5a1d7843 1217 &search_done);
03b2f08b
GB
1218 if (ret) {
1219 if (ret == 1)
1220 ret = 0;
3650860b
JB
1221 goto out;
1222 }
5a1d7843
JS
1223 }
1224
1225 /* insert our name */
1226 ret = btrfs_add_link(trans, dir, inode, name, namelen,
f186373f 1227 0, ref_index);
3650860b
JB
1228 if (ret)
1229 goto out;
5a1d7843
JS
1230
1231 btrfs_update_inode(trans, root, inode);
1232 }
1233
f186373f 1234 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
5a1d7843 1235 kfree(name);
03b2f08b 1236 name = NULL;
f186373f
MF
1237 if (log_ref_ver) {
1238 iput(dir);
1239 dir = NULL;
1240 }
5a1d7843 1241 }
e02119d5
CM
1242
1243 /* finally write the back reference in the inode */
1244 ret = overwrite_item(trans, root, path, eb, slot, key);
5a1d7843 1245out:
b3b4aa74 1246 btrfs_release_path(path);
03b2f08b 1247 kfree(name);
e02119d5
CM
1248 iput(dir);
1249 iput(inode);
3650860b 1250 return ret;
e02119d5
CM
1251}
1252
c71bf099
YZ
1253static int insert_orphan_item(struct btrfs_trans_handle *trans,
1254 struct btrfs_root *root, u64 offset)
1255{
1256 int ret;
3f870c28
KN
1257 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1258 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
c71bf099
YZ
1259 if (ret > 0)
1260 ret = btrfs_insert_orphan_item(trans, root, offset);
1261 return ret;
1262}
1263
f186373f
MF
1264static int count_inode_extrefs(struct btrfs_root *root,
1265 struct inode *inode, struct btrfs_path *path)
1266{
1267 int ret = 0;
1268 int name_len;
1269 unsigned int nlink = 0;
1270 u32 item_size;
1271 u32 cur_offset = 0;
1272 u64 inode_objectid = btrfs_ino(inode);
1273 u64 offset = 0;
1274 unsigned long ptr;
1275 struct btrfs_inode_extref *extref;
1276 struct extent_buffer *leaf;
1277
1278 while (1) {
1279 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1280 &extref, &offset);
1281 if (ret)
1282 break;
c71bf099 1283
f186373f
MF
1284 leaf = path->nodes[0];
1285 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1286 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1287
1288 while (cur_offset < item_size) {
1289 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1290 name_len = btrfs_inode_extref_name_len(leaf, extref);
1291
1292 nlink++;
1293
1294 cur_offset += name_len + sizeof(*extref);
1295 }
1296
1297 offset++;
1298 btrfs_release_path(path);
1299 }
1300 btrfs_release_path(path);
1301
1302 if (ret < 0)
1303 return ret;
1304 return nlink;
1305}
1306
1307static int count_inode_refs(struct btrfs_root *root,
1308 struct inode *inode, struct btrfs_path *path)
e02119d5 1309{
e02119d5
CM
1310 int ret;
1311 struct btrfs_key key;
f186373f 1312 unsigned int nlink = 0;
e02119d5
CM
1313 unsigned long ptr;
1314 unsigned long ptr_end;
1315 int name_len;
33345d01 1316 u64 ino = btrfs_ino(inode);
e02119d5 1317
33345d01 1318 key.objectid = ino;
e02119d5
CM
1319 key.type = BTRFS_INODE_REF_KEY;
1320 key.offset = (u64)-1;
1321
d397712b 1322 while (1) {
e02119d5
CM
1323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1324 if (ret < 0)
1325 break;
1326 if (ret > 0) {
1327 if (path->slots[0] == 0)
1328 break;
1329 path->slots[0]--;
1330 }
e93ae26f 1331process_slot:
e02119d5
CM
1332 btrfs_item_key_to_cpu(path->nodes[0], &key,
1333 path->slots[0]);
33345d01 1334 if (key.objectid != ino ||
e02119d5
CM
1335 key.type != BTRFS_INODE_REF_KEY)
1336 break;
1337 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1338 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1339 path->slots[0]);
d397712b 1340 while (ptr < ptr_end) {
e02119d5
CM
1341 struct btrfs_inode_ref *ref;
1342
1343 ref = (struct btrfs_inode_ref *)ptr;
1344 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1345 ref);
1346 ptr = (unsigned long)(ref + 1) + name_len;
1347 nlink++;
1348 }
1349
1350 if (key.offset == 0)
1351 break;
e93ae26f
FDBM
1352 if (path->slots[0] > 0) {
1353 path->slots[0]--;
1354 goto process_slot;
1355 }
e02119d5 1356 key.offset--;
b3b4aa74 1357 btrfs_release_path(path);
e02119d5 1358 }
b3b4aa74 1359 btrfs_release_path(path);
f186373f
MF
1360
1361 return nlink;
1362}
1363
1364/*
1365 * There are a few corners where the link count of the file can't
1366 * be properly maintained during replay. So, instead of adding
1367 * lots of complexity to the log code, we just scan the backrefs
1368 * for any file that has been through replay.
1369 *
1370 * The scan will update the link count on the inode to reflect the
1371 * number of back refs found. If it goes down to zero, the iput
1372 * will free the inode.
1373 */
1374static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1375 struct btrfs_root *root,
1376 struct inode *inode)
1377{
1378 struct btrfs_path *path;
1379 int ret;
1380 u64 nlink = 0;
1381 u64 ino = btrfs_ino(inode);
1382
1383 path = btrfs_alloc_path();
1384 if (!path)
1385 return -ENOMEM;
1386
1387 ret = count_inode_refs(root, inode, path);
1388 if (ret < 0)
1389 goto out;
1390
1391 nlink = ret;
1392
1393 ret = count_inode_extrefs(root, inode, path);
1394 if (ret == -ENOENT)
1395 ret = 0;
1396
1397 if (ret < 0)
1398 goto out;
1399
1400 nlink += ret;
1401
1402 ret = 0;
1403
e02119d5 1404 if (nlink != inode->i_nlink) {
bfe86848 1405 set_nlink(inode, nlink);
e02119d5
CM
1406 btrfs_update_inode(trans, root, inode);
1407 }
8d5bf1cb 1408 BTRFS_I(inode)->index_cnt = (u64)-1;
e02119d5 1409
c71bf099
YZ
1410 if (inode->i_nlink == 0) {
1411 if (S_ISDIR(inode->i_mode)) {
1412 ret = replay_dir_deletes(trans, root, NULL, path,
33345d01 1413 ino, 1);
3650860b
JB
1414 if (ret)
1415 goto out;
c71bf099 1416 }
33345d01 1417 ret = insert_orphan_item(trans, root, ino);
12fcfd22 1418 }
12fcfd22 1419
f186373f
MF
1420out:
1421 btrfs_free_path(path);
1422 return ret;
e02119d5
CM
1423}
1424
1425static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1426 struct btrfs_root *root,
1427 struct btrfs_path *path)
1428{
1429 int ret;
1430 struct btrfs_key key;
1431 struct inode *inode;
1432
1433 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1434 key.type = BTRFS_ORPHAN_ITEM_KEY;
1435 key.offset = (u64)-1;
d397712b 1436 while (1) {
e02119d5
CM
1437 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1438 if (ret < 0)
1439 break;
1440
1441 if (ret == 1) {
1442 if (path->slots[0] == 0)
1443 break;
1444 path->slots[0]--;
1445 }
1446
1447 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1448 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1449 key.type != BTRFS_ORPHAN_ITEM_KEY)
1450 break;
1451
1452 ret = btrfs_del_item(trans, root, path);
65a246c5
TI
1453 if (ret)
1454 goto out;
e02119d5 1455
b3b4aa74 1456 btrfs_release_path(path);
e02119d5 1457 inode = read_one_inode(root, key.offset);
c00e9493
TI
1458 if (!inode)
1459 return -EIO;
e02119d5
CM
1460
1461 ret = fixup_inode_link_count(trans, root, inode);
e02119d5 1462 iput(inode);
3650860b
JB
1463 if (ret)
1464 goto out;
e02119d5 1465
12fcfd22
CM
1466 /*
1467 * fixup on a directory may create new entries,
1468 * make sure we always look for the highset possible
1469 * offset
1470 */
1471 key.offset = (u64)-1;
e02119d5 1472 }
65a246c5
TI
1473 ret = 0;
1474out:
b3b4aa74 1475 btrfs_release_path(path);
65a246c5 1476 return ret;
e02119d5
CM
1477}
1478
1479
1480/*
1481 * record a given inode in the fixup dir so we can check its link
1482 * count when replay is done. The link count is incremented here
1483 * so the inode won't go away until we check it
1484 */
1485static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1486 struct btrfs_root *root,
1487 struct btrfs_path *path,
1488 u64 objectid)
1489{
1490 struct btrfs_key key;
1491 int ret = 0;
1492 struct inode *inode;
1493
1494 inode = read_one_inode(root, objectid);
c00e9493
TI
1495 if (!inode)
1496 return -EIO;
e02119d5
CM
1497
1498 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1499 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1500 key.offset = objectid;
1501
1502 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1503
b3b4aa74 1504 btrfs_release_path(path);
e02119d5 1505 if (ret == 0) {
9bf7a489
JB
1506 if (!inode->i_nlink)
1507 set_nlink(inode, 1);
1508 else
8b558c5f 1509 inc_nlink(inode);
b9959295 1510 ret = btrfs_update_inode(trans, root, inode);
e02119d5
CM
1511 } else if (ret == -EEXIST) {
1512 ret = 0;
1513 } else {
3650860b 1514 BUG(); /* Logic Error */
e02119d5
CM
1515 }
1516 iput(inode);
1517
1518 return ret;
1519}
1520
1521/*
1522 * when replaying the log for a directory, we only insert names
1523 * for inodes that actually exist. This means an fsync on a directory
1524 * does not implicitly fsync all the new files in it
1525 */
1526static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *root,
1528 struct btrfs_path *path,
1529 u64 dirid, u64 index,
1530 char *name, int name_len, u8 type,
1531 struct btrfs_key *location)
1532{
1533 struct inode *inode;
1534 struct inode *dir;
1535 int ret;
1536
1537 inode = read_one_inode(root, location->objectid);
1538 if (!inode)
1539 return -ENOENT;
1540
1541 dir = read_one_inode(root, dirid);
1542 if (!dir) {
1543 iput(inode);
1544 return -EIO;
1545 }
d555438b 1546
e02119d5
CM
1547 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1548
1549 /* FIXME, put inode into FIXUP list */
1550
1551 iput(inode);
1552 iput(dir);
1553 return ret;
1554}
1555
1556/*
1557 * take a single entry in a log directory item and replay it into
1558 * the subvolume.
1559 *
1560 * if a conflicting item exists in the subdirectory already,
1561 * the inode it points to is unlinked and put into the link count
1562 * fix up tree.
1563 *
1564 * If a name from the log points to a file or directory that does
1565 * not exist in the FS, it is skipped. fsyncs on directories
1566 * do not force down inodes inside that directory, just changes to the
1567 * names or unlinks in a directory.
1568 */
1569static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1570 struct btrfs_root *root,
1571 struct btrfs_path *path,
1572 struct extent_buffer *eb,
1573 struct btrfs_dir_item *di,
1574 struct btrfs_key *key)
1575{
1576 char *name;
1577 int name_len;
1578 struct btrfs_dir_item *dst_di;
1579 struct btrfs_key found_key;
1580 struct btrfs_key log_key;
1581 struct inode *dir;
e02119d5 1582 u8 log_type;
4bef0848 1583 int exists;
3650860b 1584 int ret = 0;
d555438b 1585 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
e02119d5
CM
1586
1587 dir = read_one_inode(root, key->objectid);
c00e9493
TI
1588 if (!dir)
1589 return -EIO;
e02119d5
CM
1590
1591 name_len = btrfs_dir_name_len(eb, di);
1592 name = kmalloc(name_len, GFP_NOFS);
2bac325e
FDBM
1593 if (!name) {
1594 ret = -ENOMEM;
1595 goto out;
1596 }
2a29edc6 1597
e02119d5
CM
1598 log_type = btrfs_dir_type(eb, di);
1599 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1600 name_len);
1601
1602 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
4bef0848
CM
1603 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1604 if (exists == 0)
1605 exists = 1;
1606 else
1607 exists = 0;
b3b4aa74 1608 btrfs_release_path(path);
4bef0848 1609
e02119d5
CM
1610 if (key->type == BTRFS_DIR_ITEM_KEY) {
1611 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1612 name, name_len, 1);
d397712b 1613 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
e02119d5
CM
1614 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1615 key->objectid,
1616 key->offset, name,
1617 name_len, 1);
1618 } else {
3650860b
JB
1619 /* Corruption */
1620 ret = -EINVAL;
1621 goto out;
e02119d5 1622 }
c704005d 1623 if (IS_ERR_OR_NULL(dst_di)) {
e02119d5
CM
1624 /* we need a sequence number to insert, so we only
1625 * do inserts for the BTRFS_DIR_INDEX_KEY types
1626 */
1627 if (key->type != BTRFS_DIR_INDEX_KEY)
1628 goto out;
1629 goto insert;
1630 }
1631
1632 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1633 /* the existing item matches the logged item */
1634 if (found_key.objectid == log_key.objectid &&
1635 found_key.type == log_key.type &&
1636 found_key.offset == log_key.offset &&
1637 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1638 goto out;
1639 }
1640
1641 /*
1642 * don't drop the conflicting directory entry if the inode
1643 * for the new entry doesn't exist
1644 */
4bef0848 1645 if (!exists)
e02119d5
CM
1646 goto out;
1647
e02119d5 1648 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
3650860b
JB
1649 if (ret)
1650 goto out;
e02119d5
CM
1651
1652 if (key->type == BTRFS_DIR_INDEX_KEY)
1653 goto insert;
1654out:
b3b4aa74 1655 btrfs_release_path(path);
d555438b
JB
1656 if (!ret && update_size) {
1657 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1658 ret = btrfs_update_inode(trans, root, dir);
1659 }
e02119d5
CM
1660 kfree(name);
1661 iput(dir);
3650860b 1662 return ret;
e02119d5
CM
1663
1664insert:
b3b4aa74 1665 btrfs_release_path(path);
e02119d5
CM
1666 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1667 name, name_len, log_type, &log_key);
3650860b
JB
1668 if (ret && ret != -ENOENT)
1669 goto out;
d555438b 1670 update_size = false;
3650860b 1671 ret = 0;
e02119d5
CM
1672 goto out;
1673}
1674
1675/*
1676 * find all the names in a directory item and reconcile them into
1677 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1678 * one name in a directory item, but the same code gets used for
1679 * both directory index types
1680 */
1681static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
1683 struct btrfs_path *path,
1684 struct extent_buffer *eb, int slot,
1685 struct btrfs_key *key)
1686{
1687 int ret;
1688 u32 item_size = btrfs_item_size_nr(eb, slot);
1689 struct btrfs_dir_item *di;
1690 int name_len;
1691 unsigned long ptr;
1692 unsigned long ptr_end;
1693
1694 ptr = btrfs_item_ptr_offset(eb, slot);
1695 ptr_end = ptr + item_size;
d397712b 1696 while (ptr < ptr_end) {
e02119d5 1697 di = (struct btrfs_dir_item *)ptr;
22a94d44
JB
1698 if (verify_dir_item(root, eb, di))
1699 return -EIO;
e02119d5
CM
1700 name_len = btrfs_dir_name_len(eb, di);
1701 ret = replay_one_name(trans, root, path, eb, di, key);
3650860b
JB
1702 if (ret)
1703 return ret;
e02119d5
CM
1704 ptr = (unsigned long)(di + 1);
1705 ptr += name_len;
1706 }
1707 return 0;
1708}
1709
1710/*
1711 * directory replay has two parts. There are the standard directory
1712 * items in the log copied from the subvolume, and range items
1713 * created in the log while the subvolume was logged.
1714 *
1715 * The range items tell us which parts of the key space the log
1716 * is authoritative for. During replay, if a key in the subvolume
1717 * directory is in a logged range item, but not actually in the log
1718 * that means it was deleted from the directory before the fsync
1719 * and should be removed.
1720 */
1721static noinline int find_dir_range(struct btrfs_root *root,
1722 struct btrfs_path *path,
1723 u64 dirid, int key_type,
1724 u64 *start_ret, u64 *end_ret)
1725{
1726 struct btrfs_key key;
1727 u64 found_end;
1728 struct btrfs_dir_log_item *item;
1729 int ret;
1730 int nritems;
1731
1732 if (*start_ret == (u64)-1)
1733 return 1;
1734
1735 key.objectid = dirid;
1736 key.type = key_type;
1737 key.offset = *start_ret;
1738
1739 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1740 if (ret < 0)
1741 goto out;
1742 if (ret > 0) {
1743 if (path->slots[0] == 0)
1744 goto out;
1745 path->slots[0]--;
1746 }
1747 if (ret != 0)
1748 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1749
1750 if (key.type != key_type || key.objectid != dirid) {
1751 ret = 1;
1752 goto next;
1753 }
1754 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1755 struct btrfs_dir_log_item);
1756 found_end = btrfs_dir_log_end(path->nodes[0], item);
1757
1758 if (*start_ret >= key.offset && *start_ret <= found_end) {
1759 ret = 0;
1760 *start_ret = key.offset;
1761 *end_ret = found_end;
1762 goto out;
1763 }
1764 ret = 1;
1765next:
1766 /* check the next slot in the tree to see if it is a valid item */
1767 nritems = btrfs_header_nritems(path->nodes[0]);
1768 if (path->slots[0] >= nritems) {
1769 ret = btrfs_next_leaf(root, path);
1770 if (ret)
1771 goto out;
1772 } else {
1773 path->slots[0]++;
1774 }
1775
1776 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1777
1778 if (key.type != key_type || key.objectid != dirid) {
1779 ret = 1;
1780 goto out;
1781 }
1782 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1783 struct btrfs_dir_log_item);
1784 found_end = btrfs_dir_log_end(path->nodes[0], item);
1785 *start_ret = key.offset;
1786 *end_ret = found_end;
1787 ret = 0;
1788out:
b3b4aa74 1789 btrfs_release_path(path);
e02119d5
CM
1790 return ret;
1791}
1792
1793/*
1794 * this looks for a given directory item in the log. If the directory
1795 * item is not in the log, the item is removed and the inode it points
1796 * to is unlinked
1797 */
1798static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1799 struct btrfs_root *root,
1800 struct btrfs_root *log,
1801 struct btrfs_path *path,
1802 struct btrfs_path *log_path,
1803 struct inode *dir,
1804 struct btrfs_key *dir_key)
1805{
1806 int ret;
1807 struct extent_buffer *eb;
1808 int slot;
1809 u32 item_size;
1810 struct btrfs_dir_item *di;
1811 struct btrfs_dir_item *log_di;
1812 int name_len;
1813 unsigned long ptr;
1814 unsigned long ptr_end;
1815 char *name;
1816 struct inode *inode;
1817 struct btrfs_key location;
1818
1819again:
1820 eb = path->nodes[0];
1821 slot = path->slots[0];
1822 item_size = btrfs_item_size_nr(eb, slot);
1823 ptr = btrfs_item_ptr_offset(eb, slot);
1824 ptr_end = ptr + item_size;
d397712b 1825 while (ptr < ptr_end) {
e02119d5 1826 di = (struct btrfs_dir_item *)ptr;
22a94d44
JB
1827 if (verify_dir_item(root, eb, di)) {
1828 ret = -EIO;
1829 goto out;
1830 }
1831
e02119d5
CM
1832 name_len = btrfs_dir_name_len(eb, di);
1833 name = kmalloc(name_len, GFP_NOFS);
1834 if (!name) {
1835 ret = -ENOMEM;
1836 goto out;
1837 }
1838 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1839 name_len);
1840 log_di = NULL;
12fcfd22 1841 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
e02119d5
CM
1842 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1843 dir_key->objectid,
1844 name, name_len, 0);
12fcfd22 1845 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
e02119d5
CM
1846 log_di = btrfs_lookup_dir_index_item(trans, log,
1847 log_path,
1848 dir_key->objectid,
1849 dir_key->offset,
1850 name, name_len, 0);
1851 }
269d040f 1852 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
e02119d5 1853 btrfs_dir_item_key_to_cpu(eb, di, &location);
b3b4aa74
DS
1854 btrfs_release_path(path);
1855 btrfs_release_path(log_path);
e02119d5 1856 inode = read_one_inode(root, location.objectid);
c00e9493
TI
1857 if (!inode) {
1858 kfree(name);
1859 return -EIO;
1860 }
e02119d5
CM
1861
1862 ret = link_to_fixup_dir(trans, root,
1863 path, location.objectid);
3650860b
JB
1864 if (ret) {
1865 kfree(name);
1866 iput(inode);
1867 goto out;
1868 }
1869
8b558c5f 1870 inc_nlink(inode);
e02119d5
CM
1871 ret = btrfs_unlink_inode(trans, root, dir, inode,
1872 name, name_len);
3650860b 1873 if (!ret)
ada9af21 1874 ret = btrfs_run_delayed_items(trans, root);
e02119d5
CM
1875 kfree(name);
1876 iput(inode);
3650860b
JB
1877 if (ret)
1878 goto out;
e02119d5
CM
1879
1880 /* there might still be more names under this key
1881 * check and repeat if required
1882 */
1883 ret = btrfs_search_slot(NULL, root, dir_key, path,
1884 0, 0);
1885 if (ret == 0)
1886 goto again;
1887 ret = 0;
1888 goto out;
269d040f
FDBM
1889 } else if (IS_ERR(log_di)) {
1890 kfree(name);
1891 return PTR_ERR(log_di);
e02119d5 1892 }
b3b4aa74 1893 btrfs_release_path(log_path);
e02119d5
CM
1894 kfree(name);
1895
1896 ptr = (unsigned long)(di + 1);
1897 ptr += name_len;
1898 }
1899 ret = 0;
1900out:
b3b4aa74
DS
1901 btrfs_release_path(path);
1902 btrfs_release_path(log_path);
e02119d5
CM
1903 return ret;
1904}
1905
1906/*
1907 * deletion replay happens before we copy any new directory items
1908 * out of the log or out of backreferences from inodes. It
1909 * scans the log to find ranges of keys that log is authoritative for,
1910 * and then scans the directory to find items in those ranges that are
1911 * not present in the log.
1912 *
1913 * Anything we don't find in the log is unlinked and removed from the
1914 * directory.
1915 */
1916static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *root,
1918 struct btrfs_root *log,
1919 struct btrfs_path *path,
12fcfd22 1920 u64 dirid, int del_all)
e02119d5
CM
1921{
1922 u64 range_start;
1923 u64 range_end;
1924 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1925 int ret = 0;
1926 struct btrfs_key dir_key;
1927 struct btrfs_key found_key;
1928 struct btrfs_path *log_path;
1929 struct inode *dir;
1930
1931 dir_key.objectid = dirid;
1932 dir_key.type = BTRFS_DIR_ITEM_KEY;
1933 log_path = btrfs_alloc_path();
1934 if (!log_path)
1935 return -ENOMEM;
1936
1937 dir = read_one_inode(root, dirid);
1938 /* it isn't an error if the inode isn't there, that can happen
1939 * because we replay the deletes before we copy in the inode item
1940 * from the log
1941 */
1942 if (!dir) {
1943 btrfs_free_path(log_path);
1944 return 0;
1945 }
1946again:
1947 range_start = 0;
1948 range_end = 0;
d397712b 1949 while (1) {
12fcfd22
CM
1950 if (del_all)
1951 range_end = (u64)-1;
1952 else {
1953 ret = find_dir_range(log, path, dirid, key_type,
1954 &range_start, &range_end);
1955 if (ret != 0)
1956 break;
1957 }
e02119d5
CM
1958
1959 dir_key.offset = range_start;
d397712b 1960 while (1) {
e02119d5
CM
1961 int nritems;
1962 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1963 0, 0);
1964 if (ret < 0)
1965 goto out;
1966
1967 nritems = btrfs_header_nritems(path->nodes[0]);
1968 if (path->slots[0] >= nritems) {
1969 ret = btrfs_next_leaf(root, path);
1970 if (ret)
1971 break;
1972 }
1973 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1974 path->slots[0]);
1975 if (found_key.objectid != dirid ||
1976 found_key.type != dir_key.type)
1977 goto next_type;
1978
1979 if (found_key.offset > range_end)
1980 break;
1981
1982 ret = check_item_in_log(trans, root, log, path,
12fcfd22
CM
1983 log_path, dir,
1984 &found_key);
3650860b
JB
1985 if (ret)
1986 goto out;
e02119d5
CM
1987 if (found_key.offset == (u64)-1)
1988 break;
1989 dir_key.offset = found_key.offset + 1;
1990 }
b3b4aa74 1991 btrfs_release_path(path);
e02119d5
CM
1992 if (range_end == (u64)-1)
1993 break;
1994 range_start = range_end + 1;
1995 }
1996
1997next_type:
1998 ret = 0;
1999 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2000 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2001 dir_key.type = BTRFS_DIR_INDEX_KEY;
b3b4aa74 2002 btrfs_release_path(path);
e02119d5
CM
2003 goto again;
2004 }
2005out:
b3b4aa74 2006 btrfs_release_path(path);
e02119d5
CM
2007 btrfs_free_path(log_path);
2008 iput(dir);
2009 return ret;
2010}
2011
2012/*
2013 * the process_func used to replay items from the log tree. This
2014 * gets called in two different stages. The first stage just looks
2015 * for inodes and makes sure they are all copied into the subvolume.
2016 *
2017 * The second stage copies all the other item types from the log into
2018 * the subvolume. The two stage approach is slower, but gets rid of
2019 * lots of complexity around inodes referencing other inodes that exist
2020 * only in the log (references come from either directory items or inode
2021 * back refs).
2022 */
2023static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2024 struct walk_control *wc, u64 gen)
2025{
2026 int nritems;
2027 struct btrfs_path *path;
2028 struct btrfs_root *root = wc->replay_dest;
2029 struct btrfs_key key;
e02119d5
CM
2030 int level;
2031 int i;
2032 int ret;
2033
018642a1
TI
2034 ret = btrfs_read_buffer(eb, gen);
2035 if (ret)
2036 return ret;
e02119d5
CM
2037
2038 level = btrfs_header_level(eb);
2039
2040 if (level != 0)
2041 return 0;
2042
2043 path = btrfs_alloc_path();
1e5063d0
MF
2044 if (!path)
2045 return -ENOMEM;
e02119d5
CM
2046
2047 nritems = btrfs_header_nritems(eb);
2048 for (i = 0; i < nritems; i++) {
2049 btrfs_item_key_to_cpu(eb, &key, i);
e02119d5
CM
2050
2051 /* inode keys are done during the first stage */
2052 if (key.type == BTRFS_INODE_ITEM_KEY &&
2053 wc->stage == LOG_WALK_REPLAY_INODES) {
e02119d5
CM
2054 struct btrfs_inode_item *inode_item;
2055 u32 mode;
2056
2057 inode_item = btrfs_item_ptr(eb, i,
2058 struct btrfs_inode_item);
2059 mode = btrfs_inode_mode(eb, inode_item);
2060 if (S_ISDIR(mode)) {
2061 ret = replay_dir_deletes(wc->trans,
12fcfd22 2062 root, log, path, key.objectid, 0);
b50c6e25
JB
2063 if (ret)
2064 break;
e02119d5
CM
2065 }
2066 ret = overwrite_item(wc->trans, root, path,
2067 eb, i, &key);
b50c6e25
JB
2068 if (ret)
2069 break;
e02119d5 2070
c71bf099
YZ
2071 /* for regular files, make sure corresponding
2072 * orhpan item exist. extents past the new EOF
2073 * will be truncated later by orphan cleanup.
e02119d5
CM
2074 */
2075 if (S_ISREG(mode)) {
c71bf099
YZ
2076 ret = insert_orphan_item(wc->trans, root,
2077 key.objectid);
b50c6e25
JB
2078 if (ret)
2079 break;
e02119d5 2080 }
c71bf099 2081
e02119d5
CM
2082 ret = link_to_fixup_dir(wc->trans, root,
2083 path, key.objectid);
b50c6e25
JB
2084 if (ret)
2085 break;
e02119d5 2086 }
dd8e7217
JB
2087
2088 if (key.type == BTRFS_DIR_INDEX_KEY &&
2089 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2090 ret = replay_one_dir_item(wc->trans, root, path,
2091 eb, i, &key);
2092 if (ret)
2093 break;
2094 }
2095
e02119d5
CM
2096 if (wc->stage < LOG_WALK_REPLAY_ALL)
2097 continue;
2098
2099 /* these keys are simply copied */
2100 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2101 ret = overwrite_item(wc->trans, root, path,
2102 eb, i, &key);
b50c6e25
JB
2103 if (ret)
2104 break;
2da1c669
LB
2105 } else if (key.type == BTRFS_INODE_REF_KEY ||
2106 key.type == BTRFS_INODE_EXTREF_KEY) {
f186373f
MF
2107 ret = add_inode_ref(wc->trans, root, log, path,
2108 eb, i, &key);
b50c6e25
JB
2109 if (ret && ret != -ENOENT)
2110 break;
2111 ret = 0;
e02119d5
CM
2112 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2113 ret = replay_one_extent(wc->trans, root, path,
2114 eb, i, &key);
b50c6e25
JB
2115 if (ret)
2116 break;
dd8e7217 2117 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
e02119d5
CM
2118 ret = replay_one_dir_item(wc->trans, root, path,
2119 eb, i, &key);
b50c6e25
JB
2120 if (ret)
2121 break;
e02119d5
CM
2122 }
2123 }
2124 btrfs_free_path(path);
b50c6e25 2125 return ret;
e02119d5
CM
2126}
2127
d397712b 2128static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
e02119d5
CM
2129 struct btrfs_root *root,
2130 struct btrfs_path *path, int *level,
2131 struct walk_control *wc)
2132{
2133 u64 root_owner;
e02119d5
CM
2134 u64 bytenr;
2135 u64 ptr_gen;
2136 struct extent_buffer *next;
2137 struct extent_buffer *cur;
2138 struct extent_buffer *parent;
2139 u32 blocksize;
2140 int ret = 0;
2141
2142 WARN_ON(*level < 0);
2143 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2144
d397712b 2145 while (*level > 0) {
e02119d5
CM
2146 WARN_ON(*level < 0);
2147 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2148 cur = path->nodes[*level];
2149
fae7f21c 2150 WARN_ON(btrfs_header_level(cur) != *level);
e02119d5
CM
2151
2152 if (path->slots[*level] >=
2153 btrfs_header_nritems(cur))
2154 break;
2155
2156 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2157 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2158 blocksize = btrfs_level_size(root, *level - 1);
2159
2160 parent = path->nodes[*level];
2161 root_owner = btrfs_header_owner(parent);
e02119d5
CM
2162
2163 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
2a29edc6 2164 if (!next)
2165 return -ENOMEM;
e02119d5 2166
e02119d5 2167 if (*level == 1) {
1e5063d0 2168 ret = wc->process_func(root, next, wc, ptr_gen);
b50c6e25
JB
2169 if (ret) {
2170 free_extent_buffer(next);
1e5063d0 2171 return ret;
b50c6e25 2172 }
4a500fd1 2173
e02119d5
CM
2174 path->slots[*level]++;
2175 if (wc->free) {
018642a1
TI
2176 ret = btrfs_read_buffer(next, ptr_gen);
2177 if (ret) {
2178 free_extent_buffer(next);
2179 return ret;
2180 }
e02119d5 2181
681ae509
JB
2182 if (trans) {
2183 btrfs_tree_lock(next);
2184 btrfs_set_lock_blocking(next);
2185 clean_tree_block(trans, root, next);
2186 btrfs_wait_tree_block_writeback(next);
2187 btrfs_tree_unlock(next);
2188 }
e02119d5 2189
e02119d5
CM
2190 WARN_ON(root_owner !=
2191 BTRFS_TREE_LOG_OBJECTID);
e688b725 2192 ret = btrfs_free_and_pin_reserved_extent(root,
d00aff00 2193 bytenr, blocksize);
3650860b
JB
2194 if (ret) {
2195 free_extent_buffer(next);
2196 return ret;
2197 }
e02119d5
CM
2198 }
2199 free_extent_buffer(next);
2200 continue;
2201 }
018642a1
TI
2202 ret = btrfs_read_buffer(next, ptr_gen);
2203 if (ret) {
2204 free_extent_buffer(next);
2205 return ret;
2206 }
e02119d5
CM
2207
2208 WARN_ON(*level <= 0);
2209 if (path->nodes[*level-1])
2210 free_extent_buffer(path->nodes[*level-1]);
2211 path->nodes[*level-1] = next;
2212 *level = btrfs_header_level(next);
2213 path->slots[*level] = 0;
2214 cond_resched();
2215 }
2216 WARN_ON(*level < 0);
2217 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2218
4a500fd1 2219 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
e02119d5
CM
2220
2221 cond_resched();
2222 return 0;
2223}
2224
d397712b 2225static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
e02119d5
CM
2226 struct btrfs_root *root,
2227 struct btrfs_path *path, int *level,
2228 struct walk_control *wc)
2229{
2230 u64 root_owner;
e02119d5
CM
2231 int i;
2232 int slot;
2233 int ret;
2234
d397712b 2235 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
e02119d5 2236 slot = path->slots[i];
4a500fd1 2237 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
e02119d5
CM
2238 path->slots[i]++;
2239 *level = i;
2240 WARN_ON(*level == 0);
2241 return 0;
2242 } else {
31840ae1
ZY
2243 struct extent_buffer *parent;
2244 if (path->nodes[*level] == root->node)
2245 parent = path->nodes[*level];
2246 else
2247 parent = path->nodes[*level + 1];
2248
2249 root_owner = btrfs_header_owner(parent);
1e5063d0 2250 ret = wc->process_func(root, path->nodes[*level], wc,
e02119d5 2251 btrfs_header_generation(path->nodes[*level]));
1e5063d0
MF
2252 if (ret)
2253 return ret;
2254
e02119d5
CM
2255 if (wc->free) {
2256 struct extent_buffer *next;
2257
2258 next = path->nodes[*level];
2259
681ae509
JB
2260 if (trans) {
2261 btrfs_tree_lock(next);
2262 btrfs_set_lock_blocking(next);
2263 clean_tree_block(trans, root, next);
2264 btrfs_wait_tree_block_writeback(next);
2265 btrfs_tree_unlock(next);
2266 }
e02119d5 2267
e02119d5 2268 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
e688b725 2269 ret = btrfs_free_and_pin_reserved_extent(root,
e02119d5 2270 path->nodes[*level]->start,
d00aff00 2271 path->nodes[*level]->len);
3650860b
JB
2272 if (ret)
2273 return ret;
e02119d5
CM
2274 }
2275 free_extent_buffer(path->nodes[*level]);
2276 path->nodes[*level] = NULL;
2277 *level = i + 1;
2278 }
2279 }
2280 return 1;
2281}
2282
2283/*
2284 * drop the reference count on the tree rooted at 'snap'. This traverses
2285 * the tree freeing any blocks that have a ref count of zero after being
2286 * decremented.
2287 */
2288static int walk_log_tree(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *log, struct walk_control *wc)
2290{
2291 int ret = 0;
2292 int wret;
2293 int level;
2294 struct btrfs_path *path;
e02119d5
CM
2295 int orig_level;
2296
2297 path = btrfs_alloc_path();
db5b493a
TI
2298 if (!path)
2299 return -ENOMEM;
e02119d5
CM
2300
2301 level = btrfs_header_level(log->node);
2302 orig_level = level;
2303 path->nodes[level] = log->node;
2304 extent_buffer_get(log->node);
2305 path->slots[level] = 0;
2306
d397712b 2307 while (1) {
e02119d5
CM
2308 wret = walk_down_log_tree(trans, log, path, &level, wc);
2309 if (wret > 0)
2310 break;
79787eaa 2311 if (wret < 0) {
e02119d5 2312 ret = wret;
79787eaa
JM
2313 goto out;
2314 }
e02119d5
CM
2315
2316 wret = walk_up_log_tree(trans, log, path, &level, wc);
2317 if (wret > 0)
2318 break;
79787eaa 2319 if (wret < 0) {
e02119d5 2320 ret = wret;
79787eaa
JM
2321 goto out;
2322 }
e02119d5
CM
2323 }
2324
2325 /* was the root node processed? if not, catch it here */
2326 if (path->nodes[orig_level]) {
79787eaa 2327 ret = wc->process_func(log, path->nodes[orig_level], wc,
e02119d5 2328 btrfs_header_generation(path->nodes[orig_level]));
79787eaa
JM
2329 if (ret)
2330 goto out;
e02119d5
CM
2331 if (wc->free) {
2332 struct extent_buffer *next;
2333
2334 next = path->nodes[orig_level];
2335
681ae509
JB
2336 if (trans) {
2337 btrfs_tree_lock(next);
2338 btrfs_set_lock_blocking(next);
2339 clean_tree_block(trans, log, next);
2340 btrfs_wait_tree_block_writeback(next);
2341 btrfs_tree_unlock(next);
2342 }
e02119d5 2343
e02119d5
CM
2344 WARN_ON(log->root_key.objectid !=
2345 BTRFS_TREE_LOG_OBJECTID);
e688b725 2346 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
d00aff00 2347 next->len);
3650860b
JB
2348 if (ret)
2349 goto out;
e02119d5
CM
2350 }
2351 }
2352
79787eaa 2353out:
e02119d5 2354 btrfs_free_path(path);
e02119d5
CM
2355 return ret;
2356}
2357
7237f183
YZ
2358/*
2359 * helper function to update the item for a given subvolumes log root
2360 * in the tree of log roots
2361 */
2362static int update_log_root(struct btrfs_trans_handle *trans,
2363 struct btrfs_root *log)
2364{
2365 int ret;
2366
2367 if (log->log_transid == 1) {
2368 /* insert root item on the first sync */
2369 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2370 &log->root_key, &log->root_item);
2371 } else {
2372 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2373 &log->root_key, &log->root_item);
2374 }
2375 return ret;
2376}
2377
8b050d35
MX
2378static void wait_log_commit(struct btrfs_trans_handle *trans,
2379 struct btrfs_root *root, int transid)
e02119d5
CM
2380{
2381 DEFINE_WAIT(wait);
7237f183 2382 int index = transid % 2;
e02119d5 2383
7237f183
YZ
2384 /*
2385 * we only allow two pending log transactions at a time,
2386 * so we know that if ours is more than 2 older than the
2387 * current transaction, we're done
2388 */
e02119d5 2389 do {
7237f183
YZ
2390 prepare_to_wait(&root->log_commit_wait[index],
2391 &wait, TASK_UNINTERRUPTIBLE);
2392 mutex_unlock(&root->log_mutex);
12fcfd22 2393
d1433deb 2394 if (root->log_transid_committed < transid &&
7237f183
YZ
2395 atomic_read(&root->log_commit[index]))
2396 schedule();
12fcfd22 2397
7237f183
YZ
2398 finish_wait(&root->log_commit_wait[index], &wait);
2399 mutex_lock(&root->log_mutex);
d1433deb 2400 } while (root->log_transid_committed < transid &&
7237f183 2401 atomic_read(&root->log_commit[index]));
7237f183
YZ
2402}
2403
143bede5
JM
2404static void wait_for_writer(struct btrfs_trans_handle *trans,
2405 struct btrfs_root *root)
7237f183
YZ
2406{
2407 DEFINE_WAIT(wait);
8b050d35
MX
2408
2409 while (atomic_read(&root->log_writers)) {
7237f183
YZ
2410 prepare_to_wait(&root->log_writer_wait,
2411 &wait, TASK_UNINTERRUPTIBLE);
2412 mutex_unlock(&root->log_mutex);
8b050d35 2413 if (atomic_read(&root->log_writers))
e02119d5 2414 schedule();
7237f183
YZ
2415 mutex_lock(&root->log_mutex);
2416 finish_wait(&root->log_writer_wait, &wait);
2417 }
e02119d5
CM
2418}
2419
8b050d35
MX
2420static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2421 struct btrfs_log_ctx *ctx)
2422{
2423 if (!ctx)
2424 return;
2425
2426 mutex_lock(&root->log_mutex);
2427 list_del_init(&ctx->list);
2428 mutex_unlock(&root->log_mutex);
2429}
2430
2431/*
2432 * Invoked in log mutex context, or be sure there is no other task which
2433 * can access the list.
2434 */
2435static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2436 int index, int error)
2437{
2438 struct btrfs_log_ctx *ctx;
2439
2440 if (!error) {
2441 INIT_LIST_HEAD(&root->log_ctxs[index]);
2442 return;
2443 }
2444
2445 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2446 ctx->log_ret = error;
2447
2448 INIT_LIST_HEAD(&root->log_ctxs[index]);
2449}
2450
e02119d5
CM
2451/*
2452 * btrfs_sync_log does sends a given tree log down to the disk and
2453 * updates the super blocks to record it. When this call is done,
12fcfd22
CM
2454 * you know that any inodes previously logged are safely on disk only
2455 * if it returns 0.
2456 *
2457 * Any other return value means you need to call btrfs_commit_transaction.
2458 * Some of the edge cases for fsyncing directories that have had unlinks
2459 * or renames done in the past mean that sometimes the only safe
2460 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2461 * that has happened.
e02119d5
CM
2462 */
2463int btrfs_sync_log(struct btrfs_trans_handle *trans,
8b050d35 2464 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
e02119d5 2465{
7237f183
YZ
2466 int index1;
2467 int index2;
8cef4e16 2468 int mark;
e02119d5 2469 int ret;
e02119d5 2470 struct btrfs_root *log = root->log_root;
7237f183 2471 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
bb14a59b 2472 int log_transid = 0;
8b050d35 2473 struct btrfs_log_ctx root_log_ctx;
c6adc9cc 2474 struct blk_plug plug;
e02119d5 2475
7237f183 2476 mutex_lock(&root->log_mutex);
d1433deb
MX
2477 log_transid = ctx->log_transid;
2478 if (root->log_transid_committed >= log_transid) {
2479 mutex_unlock(&root->log_mutex);
2480 return ctx->log_ret;
2481 }
2482
2483 index1 = log_transid % 2;
7237f183 2484 if (atomic_read(&root->log_commit[index1])) {
d1433deb 2485 wait_log_commit(trans, root, log_transid);
7237f183 2486 mutex_unlock(&root->log_mutex);
8b050d35 2487 return ctx->log_ret;
e02119d5 2488 }
d1433deb 2489 ASSERT(log_transid == root->log_transid);
7237f183
YZ
2490 atomic_set(&root->log_commit[index1], 1);
2491
2492 /* wait for previous tree log sync to complete */
2493 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
d1433deb 2494 wait_log_commit(trans, root, log_transid - 1);
48cab2e0 2495
86df7eb9 2496 while (1) {
2ecb7923 2497 int batch = atomic_read(&root->log_batch);
cd354ad6 2498 /* when we're on an ssd, just kick the log commit out */
27cdeb70
MX
2499 if (!btrfs_test_opt(root, SSD) &&
2500 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
86df7eb9
YZ
2501 mutex_unlock(&root->log_mutex);
2502 schedule_timeout_uninterruptible(1);
2503 mutex_lock(&root->log_mutex);
2504 }
12fcfd22 2505 wait_for_writer(trans, root);
2ecb7923 2506 if (batch == atomic_read(&root->log_batch))
e02119d5
CM
2507 break;
2508 }
e02119d5 2509
12fcfd22 2510 /* bail out if we need to do a full commit */
995946dd 2511 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
12fcfd22 2512 ret = -EAGAIN;
2ab28f32 2513 btrfs_free_logged_extents(log, log_transid);
12fcfd22
CM
2514 mutex_unlock(&root->log_mutex);
2515 goto out;
2516 }
2517
8cef4e16
YZ
2518 if (log_transid % 2 == 0)
2519 mark = EXTENT_DIRTY;
2520 else
2521 mark = EXTENT_NEW;
2522
690587d1
CM
2523 /* we start IO on all the marked extents here, but we don't actually
2524 * wait for them until later.
2525 */
c6adc9cc 2526 blk_start_plug(&plug);
8cef4e16 2527 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
79787eaa 2528 if (ret) {
c6adc9cc 2529 blk_finish_plug(&plug);
79787eaa 2530 btrfs_abort_transaction(trans, root, ret);
2ab28f32 2531 btrfs_free_logged_extents(log, log_transid);
995946dd 2532 btrfs_set_log_full_commit(root->fs_info, trans);
79787eaa
JM
2533 mutex_unlock(&root->log_mutex);
2534 goto out;
2535 }
7237f183 2536
5d4f98a2 2537 btrfs_set_root_node(&log->root_item, log->node);
7237f183 2538
7237f183
YZ
2539 root->log_transid++;
2540 log->log_transid = root->log_transid;
ff782e0a 2541 root->log_start_pid = 0;
7237f183 2542 /*
8cef4e16
YZ
2543 * IO has been started, blocks of the log tree have WRITTEN flag set
2544 * in their headers. new modifications of the log will be written to
2545 * new positions. so it's safe to allow log writers to go in.
7237f183
YZ
2546 */
2547 mutex_unlock(&root->log_mutex);
2548
d1433deb
MX
2549 btrfs_init_log_ctx(&root_log_ctx);
2550
7237f183 2551 mutex_lock(&log_root_tree->log_mutex);
2ecb7923 2552 atomic_inc(&log_root_tree->log_batch);
7237f183 2553 atomic_inc(&log_root_tree->log_writers);
d1433deb
MX
2554
2555 index2 = log_root_tree->log_transid % 2;
2556 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2557 root_log_ctx.log_transid = log_root_tree->log_transid;
2558
7237f183
YZ
2559 mutex_unlock(&log_root_tree->log_mutex);
2560
2561 ret = update_log_root(trans, log);
7237f183
YZ
2562
2563 mutex_lock(&log_root_tree->log_mutex);
2564 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2565 smp_mb();
2566 if (waitqueue_active(&log_root_tree->log_writer_wait))
2567 wake_up(&log_root_tree->log_writer_wait);
2568 }
2569
4a500fd1 2570 if (ret) {
d1433deb
MX
2571 if (!list_empty(&root_log_ctx.list))
2572 list_del_init(&root_log_ctx.list);
2573
c6adc9cc 2574 blk_finish_plug(&plug);
995946dd
MX
2575 btrfs_set_log_full_commit(root->fs_info, trans);
2576
79787eaa
JM
2577 if (ret != -ENOSPC) {
2578 btrfs_abort_transaction(trans, root, ret);
2579 mutex_unlock(&log_root_tree->log_mutex);
2580 goto out;
2581 }
4a500fd1 2582 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2ab28f32 2583 btrfs_free_logged_extents(log, log_transid);
4a500fd1
YZ
2584 mutex_unlock(&log_root_tree->log_mutex);
2585 ret = -EAGAIN;
2586 goto out;
2587 }
2588
d1433deb
MX
2589 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2590 mutex_unlock(&log_root_tree->log_mutex);
2591 ret = root_log_ctx.log_ret;
2592 goto out;
2593 }
8b050d35 2594
d1433deb 2595 index2 = root_log_ctx.log_transid % 2;
7237f183 2596 if (atomic_read(&log_root_tree->log_commit[index2])) {
c6adc9cc 2597 blk_finish_plug(&plug);
8cef4e16 2598 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
8b050d35 2599 wait_log_commit(trans, log_root_tree,
d1433deb 2600 root_log_ctx.log_transid);
2ab28f32 2601 btrfs_free_logged_extents(log, log_transid);
7237f183 2602 mutex_unlock(&log_root_tree->log_mutex);
8b050d35 2603 ret = root_log_ctx.log_ret;
7237f183
YZ
2604 goto out;
2605 }
d1433deb 2606 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
7237f183
YZ
2607 atomic_set(&log_root_tree->log_commit[index2], 1);
2608
12fcfd22
CM
2609 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2610 wait_log_commit(trans, log_root_tree,
d1433deb 2611 root_log_ctx.log_transid - 1);
12fcfd22
CM
2612 }
2613
2614 wait_for_writer(trans, log_root_tree);
7237f183 2615
12fcfd22
CM
2616 /*
2617 * now that we've moved on to the tree of log tree roots,
2618 * check the full commit flag again
2619 */
995946dd 2620 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
c6adc9cc 2621 blk_finish_plug(&plug);
8cef4e16 2622 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2ab28f32 2623 btrfs_free_logged_extents(log, log_transid);
12fcfd22
CM
2624 mutex_unlock(&log_root_tree->log_mutex);
2625 ret = -EAGAIN;
2626 goto out_wake_log_root;
2627 }
7237f183 2628
c6adc9cc
MX
2629 ret = btrfs_write_marked_extents(log_root_tree,
2630 &log_root_tree->dirty_log_pages,
2631 EXTENT_DIRTY | EXTENT_NEW);
2632 blk_finish_plug(&plug);
79787eaa 2633 if (ret) {
995946dd 2634 btrfs_set_log_full_commit(root->fs_info, trans);
79787eaa 2635 btrfs_abort_transaction(trans, root, ret);
2ab28f32 2636 btrfs_free_logged_extents(log, log_transid);
79787eaa
JM
2637 mutex_unlock(&log_root_tree->log_mutex);
2638 goto out_wake_log_root;
2639 }
8cef4e16 2640 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
c6adc9cc
MX
2641 btrfs_wait_marked_extents(log_root_tree,
2642 &log_root_tree->dirty_log_pages,
2643 EXTENT_NEW | EXTENT_DIRTY);
2ab28f32 2644 btrfs_wait_logged_extents(log, log_transid);
e02119d5 2645
6c41761f 2646 btrfs_set_super_log_root(root->fs_info->super_for_commit,
7237f183 2647 log_root_tree->node->start);
6c41761f 2648 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
7237f183 2649 btrfs_header_level(log_root_tree->node));
e02119d5 2650
7237f183 2651 log_root_tree->log_transid++;
7237f183
YZ
2652 mutex_unlock(&log_root_tree->log_mutex);
2653
2654 /*
2655 * nobody else is going to jump in and write the the ctree
2656 * super here because the log_commit atomic below is protecting
2657 * us. We must be called with a transaction handle pinning
2658 * the running transaction open, so a full commit can't hop
2659 * in and cause problems either.
2660 */
5af3e8cc 2661 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
5af3e8cc 2662 if (ret) {
995946dd 2663 btrfs_set_log_full_commit(root->fs_info, trans);
5af3e8cc
SB
2664 btrfs_abort_transaction(trans, root, ret);
2665 goto out_wake_log_root;
2666 }
7237f183 2667
257c62e1
CM
2668 mutex_lock(&root->log_mutex);
2669 if (root->last_log_commit < log_transid)
2670 root->last_log_commit = log_transid;
2671 mutex_unlock(&root->log_mutex);
2672
12fcfd22 2673out_wake_log_root:
8b050d35
MX
2674 /*
2675 * We needn't get log_mutex here because we are sure all
2676 * the other tasks are blocked.
2677 */
2678 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2679
d1433deb
MX
2680 mutex_lock(&log_root_tree->log_mutex);
2681 log_root_tree->log_transid_committed++;
7237f183 2682 atomic_set(&log_root_tree->log_commit[index2], 0);
d1433deb
MX
2683 mutex_unlock(&log_root_tree->log_mutex);
2684
7237f183
YZ
2685 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2686 wake_up(&log_root_tree->log_commit_wait[index2]);
e02119d5 2687out:
8b050d35
MX
2688 /* See above. */
2689 btrfs_remove_all_log_ctxs(root, index1, ret);
2690
d1433deb
MX
2691 mutex_lock(&root->log_mutex);
2692 root->log_transid_committed++;
7237f183 2693 atomic_set(&root->log_commit[index1], 0);
d1433deb 2694 mutex_unlock(&root->log_mutex);
8b050d35 2695
7237f183
YZ
2696 if (waitqueue_active(&root->log_commit_wait[index1]))
2697 wake_up(&root->log_commit_wait[index1]);
b31eabd8 2698 return ret;
e02119d5
CM
2699}
2700
4a500fd1
YZ
2701static void free_log_tree(struct btrfs_trans_handle *trans,
2702 struct btrfs_root *log)
e02119d5
CM
2703{
2704 int ret;
d0c803c4
CM
2705 u64 start;
2706 u64 end;
e02119d5
CM
2707 struct walk_control wc = {
2708 .free = 1,
2709 .process_func = process_one_buffer
2710 };
2711
681ae509
JB
2712 ret = walk_log_tree(trans, log, &wc);
2713 /* I don't think this can happen but just in case */
2714 if (ret)
2715 btrfs_abort_transaction(trans, log, ret);
e02119d5 2716
d397712b 2717 while (1) {
d0c803c4 2718 ret = find_first_extent_bit(&log->dirty_log_pages,
e6138876
JB
2719 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2720 NULL);
d0c803c4
CM
2721 if (ret)
2722 break;
2723
8cef4e16
YZ
2724 clear_extent_bits(&log->dirty_log_pages, start, end,
2725 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
d0c803c4
CM
2726 }
2727
2ab28f32
JB
2728 /*
2729 * We may have short-circuited the log tree with the full commit logic
2730 * and left ordered extents on our list, so clear these out to keep us
2731 * from leaking inodes and memory.
2732 */
2733 btrfs_free_logged_extents(log, 0);
2734 btrfs_free_logged_extents(log, 1);
2735
7237f183
YZ
2736 free_extent_buffer(log->node);
2737 kfree(log);
4a500fd1
YZ
2738}
2739
2740/*
2741 * free all the extents used by the tree log. This should be called
2742 * at commit time of the full transaction
2743 */
2744int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2745{
2746 if (root->log_root) {
2747 free_log_tree(trans, root->log_root);
2748 root->log_root = NULL;
2749 }
2750 return 0;
2751}
2752
2753int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2754 struct btrfs_fs_info *fs_info)
2755{
2756 if (fs_info->log_root_tree) {
2757 free_log_tree(trans, fs_info->log_root_tree);
2758 fs_info->log_root_tree = NULL;
2759 }
e02119d5
CM
2760 return 0;
2761}
2762
e02119d5
CM
2763/*
2764 * If both a file and directory are logged, and unlinks or renames are
2765 * mixed in, we have a few interesting corners:
2766 *
2767 * create file X in dir Y
2768 * link file X to X.link in dir Y
2769 * fsync file X
2770 * unlink file X but leave X.link
2771 * fsync dir Y
2772 *
2773 * After a crash we would expect only X.link to exist. But file X
2774 * didn't get fsync'd again so the log has back refs for X and X.link.
2775 *
2776 * We solve this by removing directory entries and inode backrefs from the
2777 * log when a file that was logged in the current transaction is
2778 * unlinked. Any later fsync will include the updated log entries, and
2779 * we'll be able to reconstruct the proper directory items from backrefs.
2780 *
2781 * This optimizations allows us to avoid relogging the entire inode
2782 * or the entire directory.
2783 */
2784int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2785 struct btrfs_root *root,
2786 const char *name, int name_len,
2787 struct inode *dir, u64 index)
2788{
2789 struct btrfs_root *log;
2790 struct btrfs_dir_item *di;
2791 struct btrfs_path *path;
2792 int ret;
4a500fd1 2793 int err = 0;
e02119d5 2794 int bytes_del = 0;
33345d01 2795 u64 dir_ino = btrfs_ino(dir);
e02119d5 2796
3a5f1d45
CM
2797 if (BTRFS_I(dir)->logged_trans < trans->transid)
2798 return 0;
2799
e02119d5
CM
2800 ret = join_running_log_trans(root);
2801 if (ret)
2802 return 0;
2803
2804 mutex_lock(&BTRFS_I(dir)->log_mutex);
2805
2806 log = root->log_root;
2807 path = btrfs_alloc_path();
a62f44a5
TI
2808 if (!path) {
2809 err = -ENOMEM;
2810 goto out_unlock;
2811 }
2a29edc6 2812
33345d01 2813 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
e02119d5 2814 name, name_len, -1);
4a500fd1
YZ
2815 if (IS_ERR(di)) {
2816 err = PTR_ERR(di);
2817 goto fail;
2818 }
2819 if (di) {
e02119d5
CM
2820 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2821 bytes_del += name_len;
3650860b
JB
2822 if (ret) {
2823 err = ret;
2824 goto fail;
2825 }
e02119d5 2826 }
b3b4aa74 2827 btrfs_release_path(path);
33345d01 2828 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
e02119d5 2829 index, name, name_len, -1);
4a500fd1
YZ
2830 if (IS_ERR(di)) {
2831 err = PTR_ERR(di);
2832 goto fail;
2833 }
2834 if (di) {
e02119d5
CM
2835 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2836 bytes_del += name_len;
3650860b
JB
2837 if (ret) {
2838 err = ret;
2839 goto fail;
2840 }
e02119d5
CM
2841 }
2842
2843 /* update the directory size in the log to reflect the names
2844 * we have removed
2845 */
2846 if (bytes_del) {
2847 struct btrfs_key key;
2848
33345d01 2849 key.objectid = dir_ino;
e02119d5
CM
2850 key.offset = 0;
2851 key.type = BTRFS_INODE_ITEM_KEY;
b3b4aa74 2852 btrfs_release_path(path);
e02119d5
CM
2853
2854 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
4a500fd1
YZ
2855 if (ret < 0) {
2856 err = ret;
2857 goto fail;
2858 }
e02119d5
CM
2859 if (ret == 0) {
2860 struct btrfs_inode_item *item;
2861 u64 i_size;
2862
2863 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2864 struct btrfs_inode_item);
2865 i_size = btrfs_inode_size(path->nodes[0], item);
2866 if (i_size > bytes_del)
2867 i_size -= bytes_del;
2868 else
2869 i_size = 0;
2870 btrfs_set_inode_size(path->nodes[0], item, i_size);
2871 btrfs_mark_buffer_dirty(path->nodes[0]);
2872 } else
2873 ret = 0;
b3b4aa74 2874 btrfs_release_path(path);
e02119d5 2875 }
4a500fd1 2876fail:
e02119d5 2877 btrfs_free_path(path);
a62f44a5 2878out_unlock:
e02119d5 2879 mutex_unlock(&BTRFS_I(dir)->log_mutex);
4a500fd1 2880 if (ret == -ENOSPC) {
995946dd 2881 btrfs_set_log_full_commit(root->fs_info, trans);
4a500fd1 2882 ret = 0;
79787eaa
JM
2883 } else if (ret < 0)
2884 btrfs_abort_transaction(trans, root, ret);
2885
12fcfd22 2886 btrfs_end_log_trans(root);
e02119d5 2887
411fc6bc 2888 return err;
e02119d5
CM
2889}
2890
2891/* see comments for btrfs_del_dir_entries_in_log */
2892int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2893 struct btrfs_root *root,
2894 const char *name, int name_len,
2895 struct inode *inode, u64 dirid)
2896{
2897 struct btrfs_root *log;
2898 u64 index;
2899 int ret;
2900
3a5f1d45
CM
2901 if (BTRFS_I(inode)->logged_trans < trans->transid)
2902 return 0;
2903
e02119d5
CM
2904 ret = join_running_log_trans(root);
2905 if (ret)
2906 return 0;
2907 log = root->log_root;
2908 mutex_lock(&BTRFS_I(inode)->log_mutex);
2909
33345d01 2910 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
e02119d5
CM
2911 dirid, &index);
2912 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4a500fd1 2913 if (ret == -ENOSPC) {
995946dd 2914 btrfs_set_log_full_commit(root->fs_info, trans);
4a500fd1 2915 ret = 0;
79787eaa
JM
2916 } else if (ret < 0 && ret != -ENOENT)
2917 btrfs_abort_transaction(trans, root, ret);
12fcfd22 2918 btrfs_end_log_trans(root);
e02119d5 2919
e02119d5
CM
2920 return ret;
2921}
2922
2923/*
2924 * creates a range item in the log for 'dirid'. first_offset and
2925 * last_offset tell us which parts of the key space the log should
2926 * be considered authoritative for.
2927 */
2928static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2929 struct btrfs_root *log,
2930 struct btrfs_path *path,
2931 int key_type, u64 dirid,
2932 u64 first_offset, u64 last_offset)
2933{
2934 int ret;
2935 struct btrfs_key key;
2936 struct btrfs_dir_log_item *item;
2937
2938 key.objectid = dirid;
2939 key.offset = first_offset;
2940 if (key_type == BTRFS_DIR_ITEM_KEY)
2941 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2942 else
2943 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2944 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
4a500fd1
YZ
2945 if (ret)
2946 return ret;
e02119d5
CM
2947
2948 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2949 struct btrfs_dir_log_item);
2950 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2951 btrfs_mark_buffer_dirty(path->nodes[0]);
b3b4aa74 2952 btrfs_release_path(path);
e02119d5
CM
2953 return 0;
2954}
2955
2956/*
2957 * log all the items included in the current transaction for a given
2958 * directory. This also creates the range items in the log tree required
2959 * to replay anything deleted before the fsync
2960 */
2961static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root, struct inode *inode,
2963 struct btrfs_path *path,
2964 struct btrfs_path *dst_path, int key_type,
2965 u64 min_offset, u64 *last_offset_ret)
2966{
2967 struct btrfs_key min_key;
e02119d5
CM
2968 struct btrfs_root *log = root->log_root;
2969 struct extent_buffer *src;
4a500fd1 2970 int err = 0;
e02119d5
CM
2971 int ret;
2972 int i;
2973 int nritems;
2974 u64 first_offset = min_offset;
2975 u64 last_offset = (u64)-1;
33345d01 2976 u64 ino = btrfs_ino(inode);
e02119d5
CM
2977
2978 log = root->log_root;
e02119d5 2979
33345d01 2980 min_key.objectid = ino;
e02119d5
CM
2981 min_key.type = key_type;
2982 min_key.offset = min_offset;
2983
2984 path->keep_locks = 1;
2985
6174d3cb 2986 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
e02119d5
CM
2987
2988 /*
2989 * we didn't find anything from this transaction, see if there
2990 * is anything at all
2991 */
33345d01
LZ
2992 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2993 min_key.objectid = ino;
e02119d5
CM
2994 min_key.type = key_type;
2995 min_key.offset = (u64)-1;
b3b4aa74 2996 btrfs_release_path(path);
e02119d5
CM
2997 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2998 if (ret < 0) {
b3b4aa74 2999 btrfs_release_path(path);
e02119d5
CM
3000 return ret;
3001 }
33345d01 3002 ret = btrfs_previous_item(root, path, ino, key_type);
e02119d5
CM
3003
3004 /* if ret == 0 there are items for this type,
3005 * create a range to tell us the last key of this type.
3006 * otherwise, there are no items in this directory after
3007 * *min_offset, and we create a range to indicate that.
3008 */
3009 if (ret == 0) {
3010 struct btrfs_key tmp;
3011 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3012 path->slots[0]);
d397712b 3013 if (key_type == tmp.type)
e02119d5 3014 first_offset = max(min_offset, tmp.offset) + 1;
e02119d5
CM
3015 }
3016 goto done;
3017 }
3018
3019 /* go backward to find any previous key */
33345d01 3020 ret = btrfs_previous_item(root, path, ino, key_type);
e02119d5
CM
3021 if (ret == 0) {
3022 struct btrfs_key tmp;
3023 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3024 if (key_type == tmp.type) {
3025 first_offset = tmp.offset;
3026 ret = overwrite_item(trans, log, dst_path,
3027 path->nodes[0], path->slots[0],
3028 &tmp);
4a500fd1
YZ
3029 if (ret) {
3030 err = ret;
3031 goto done;
3032 }
e02119d5
CM
3033 }
3034 }
b3b4aa74 3035 btrfs_release_path(path);
e02119d5
CM
3036
3037 /* find the first key from this transaction again */
3038 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
fae7f21c 3039 if (WARN_ON(ret != 0))
e02119d5 3040 goto done;
e02119d5
CM
3041
3042 /*
3043 * we have a block from this transaction, log every item in it
3044 * from our directory
3045 */
d397712b 3046 while (1) {
e02119d5
CM
3047 struct btrfs_key tmp;
3048 src = path->nodes[0];
3049 nritems = btrfs_header_nritems(src);
3050 for (i = path->slots[0]; i < nritems; i++) {
3051 btrfs_item_key_to_cpu(src, &min_key, i);
3052
33345d01 3053 if (min_key.objectid != ino || min_key.type != key_type)
e02119d5
CM
3054 goto done;
3055 ret = overwrite_item(trans, log, dst_path, src, i,
3056 &min_key);
4a500fd1
YZ
3057 if (ret) {
3058 err = ret;
3059 goto done;
3060 }
e02119d5
CM
3061 }
3062 path->slots[0] = nritems;
3063
3064 /*
3065 * look ahead to the next item and see if it is also
3066 * from this directory and from this transaction
3067 */
3068 ret = btrfs_next_leaf(root, path);
3069 if (ret == 1) {
3070 last_offset = (u64)-1;
3071 goto done;
3072 }
3073 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
33345d01 3074 if (tmp.objectid != ino || tmp.type != key_type) {
e02119d5
CM
3075 last_offset = (u64)-1;
3076 goto done;
3077 }
3078 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3079 ret = overwrite_item(trans, log, dst_path,
3080 path->nodes[0], path->slots[0],
3081 &tmp);
4a500fd1
YZ
3082 if (ret)
3083 err = ret;
3084 else
3085 last_offset = tmp.offset;
e02119d5
CM
3086 goto done;
3087 }
3088 }
3089done:
b3b4aa74
DS
3090 btrfs_release_path(path);
3091 btrfs_release_path(dst_path);
e02119d5 3092
4a500fd1
YZ
3093 if (err == 0) {
3094 *last_offset_ret = last_offset;
3095 /*
3096 * insert the log range keys to indicate where the log
3097 * is valid
3098 */
3099 ret = insert_dir_log_key(trans, log, path, key_type,
33345d01 3100 ino, first_offset, last_offset);
4a500fd1
YZ
3101 if (ret)
3102 err = ret;
3103 }
3104 return err;
e02119d5
CM
3105}
3106
3107/*
3108 * logging directories is very similar to logging inodes, We find all the items
3109 * from the current transaction and write them to the log.
3110 *
3111 * The recovery code scans the directory in the subvolume, and if it finds a
3112 * key in the range logged that is not present in the log tree, then it means
3113 * that dir entry was unlinked during the transaction.
3114 *
3115 * In order for that scan to work, we must include one key smaller than
3116 * the smallest logged by this transaction and one key larger than the largest
3117 * key logged by this transaction.
3118 */
3119static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3120 struct btrfs_root *root, struct inode *inode,
3121 struct btrfs_path *path,
3122 struct btrfs_path *dst_path)
3123{
3124 u64 min_key;
3125 u64 max_key;
3126 int ret;
3127 int key_type = BTRFS_DIR_ITEM_KEY;
3128
3129again:
3130 min_key = 0;
3131 max_key = 0;
d397712b 3132 while (1) {
e02119d5
CM
3133 ret = log_dir_items(trans, root, inode, path,
3134 dst_path, key_type, min_key,
3135 &max_key);
4a500fd1
YZ
3136 if (ret)
3137 return ret;
e02119d5
CM
3138 if (max_key == (u64)-1)
3139 break;
3140 min_key = max_key + 1;
3141 }
3142
3143 if (key_type == BTRFS_DIR_ITEM_KEY) {
3144 key_type = BTRFS_DIR_INDEX_KEY;
3145 goto again;
3146 }
3147 return 0;
3148}
3149
3150/*
3151 * a helper function to drop items from the log before we relog an
3152 * inode. max_key_type indicates the highest item type to remove.
3153 * This cannot be run for file data extents because it does not
3154 * free the extents they point to.
3155 */
3156static int drop_objectid_items(struct btrfs_trans_handle *trans,
3157 struct btrfs_root *log,
3158 struct btrfs_path *path,
3159 u64 objectid, int max_key_type)
3160{
3161 int ret;
3162 struct btrfs_key key;
3163 struct btrfs_key found_key;
18ec90d6 3164 int start_slot;
e02119d5
CM
3165
3166 key.objectid = objectid;
3167 key.type = max_key_type;
3168 key.offset = (u64)-1;
3169
d397712b 3170 while (1) {
e02119d5 3171 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3650860b 3172 BUG_ON(ret == 0); /* Logic error */
4a500fd1 3173 if (ret < 0)
e02119d5
CM
3174 break;
3175
3176 if (path->slots[0] == 0)
3177 break;
3178
3179 path->slots[0]--;
3180 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3181 path->slots[0]);
3182
3183 if (found_key.objectid != objectid)
3184 break;
3185
18ec90d6
JB
3186 found_key.offset = 0;
3187 found_key.type = 0;
3188 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3189 &start_slot);
3190
3191 ret = btrfs_del_items(trans, log, path, start_slot,
3192 path->slots[0] - start_slot + 1);
3193 /*
3194 * If start slot isn't 0 then we don't need to re-search, we've
3195 * found the last guy with the objectid in this tree.
3196 */
3197 if (ret || start_slot != 0)
65a246c5 3198 break;
b3b4aa74 3199 btrfs_release_path(path);
e02119d5 3200 }
b3b4aa74 3201 btrfs_release_path(path);
5bdbeb21
JB
3202 if (ret > 0)
3203 ret = 0;
4a500fd1 3204 return ret;
e02119d5
CM
3205}
3206
94edf4ae
JB
3207static void fill_inode_item(struct btrfs_trans_handle *trans,
3208 struct extent_buffer *leaf,
3209 struct btrfs_inode_item *item,
3210 struct inode *inode, int log_inode_only)
3211{
0b1c6cca
JB
3212 struct btrfs_map_token token;
3213
3214 btrfs_init_map_token(&token);
94edf4ae
JB
3215
3216 if (log_inode_only) {
3217 /* set the generation to zero so the recover code
3218 * can tell the difference between an logging
3219 * just to say 'this inode exists' and a logging
3220 * to say 'update this inode with these values'
3221 */
0b1c6cca
JB
3222 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3223 btrfs_set_token_inode_size(leaf, item, 0, &token);
94edf4ae 3224 } else {
0b1c6cca
JB
3225 btrfs_set_token_inode_generation(leaf, item,
3226 BTRFS_I(inode)->generation,
3227 &token);
3228 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3229 }
3230
3231 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3232 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3233 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3234 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3235
3236 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3237 inode->i_atime.tv_sec, &token);
3238 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3239 inode->i_atime.tv_nsec, &token);
3240
3241 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3242 inode->i_mtime.tv_sec, &token);
3243 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3244 inode->i_mtime.tv_nsec, &token);
3245
3246 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3247 inode->i_ctime.tv_sec, &token);
3248 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3249 inode->i_ctime.tv_nsec, &token);
3250
3251 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3252 &token);
3253
3254 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3255 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3256 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3257 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3258 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
94edf4ae
JB
3259}
3260
a95249b3
JB
3261static int log_inode_item(struct btrfs_trans_handle *trans,
3262 struct btrfs_root *log, struct btrfs_path *path,
3263 struct inode *inode)
3264{
3265 struct btrfs_inode_item *inode_item;
a95249b3
JB
3266 int ret;
3267
efd0c405
FDBM
3268 ret = btrfs_insert_empty_item(trans, log, path,
3269 &BTRFS_I(inode)->location,
a95249b3
JB
3270 sizeof(*inode_item));
3271 if (ret && ret != -EEXIST)
3272 return ret;
3273 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3274 struct btrfs_inode_item);
3275 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3276 btrfs_release_path(path);
3277 return 0;
3278}
3279
31ff1cd2 3280static noinline int copy_items(struct btrfs_trans_handle *trans,
d2794405 3281 struct inode *inode,
31ff1cd2 3282 struct btrfs_path *dst_path,
16e7549f 3283 struct btrfs_path *src_path, u64 *last_extent,
31ff1cd2
CM
3284 int start_slot, int nr, int inode_only)
3285{
3286 unsigned long src_offset;
3287 unsigned long dst_offset;
d2794405 3288 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
31ff1cd2
CM
3289 struct btrfs_file_extent_item *extent;
3290 struct btrfs_inode_item *inode_item;
16e7549f
JB
3291 struct extent_buffer *src = src_path->nodes[0];
3292 struct btrfs_key first_key, last_key, key;
31ff1cd2
CM
3293 int ret;
3294 struct btrfs_key *ins_keys;
3295 u32 *ins_sizes;
3296 char *ins_data;
3297 int i;
d20f7043 3298 struct list_head ordered_sums;
d2794405 3299 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
16e7549f 3300 bool has_extents = false;
74121f7c 3301 bool need_find_last_extent = true;
16e7549f 3302 bool done = false;
d20f7043
CM
3303
3304 INIT_LIST_HEAD(&ordered_sums);
31ff1cd2
CM
3305
3306 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3307 nr * sizeof(u32), GFP_NOFS);
2a29edc6 3308 if (!ins_data)
3309 return -ENOMEM;
3310
16e7549f
JB
3311 first_key.objectid = (u64)-1;
3312
31ff1cd2
CM
3313 ins_sizes = (u32 *)ins_data;
3314 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3315
3316 for (i = 0; i < nr; i++) {
3317 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3318 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3319 }
3320 ret = btrfs_insert_empty_items(trans, log, dst_path,
3321 ins_keys, ins_sizes, nr);
4a500fd1
YZ
3322 if (ret) {
3323 kfree(ins_data);
3324 return ret;
3325 }
31ff1cd2 3326
5d4f98a2 3327 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
31ff1cd2
CM
3328 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3329 dst_path->slots[0]);
3330
3331 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3332
16e7549f
JB
3333 if ((i == (nr - 1)))
3334 last_key = ins_keys[i];
3335
94edf4ae 3336 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
31ff1cd2
CM
3337 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3338 dst_path->slots[0],
3339 struct btrfs_inode_item);
94edf4ae
JB
3340 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3341 inode, inode_only == LOG_INODE_EXISTS);
3342 } else {
3343 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3344 src_offset, ins_sizes[i]);
31ff1cd2 3345 }
94edf4ae 3346
16e7549f
JB
3347 /*
3348 * We set need_find_last_extent here in case we know we were
3349 * processing other items and then walk into the first extent in
3350 * the inode. If we don't hit an extent then nothing changes,
3351 * we'll do the last search the next time around.
3352 */
3353 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3354 has_extents = true;
74121f7c 3355 if (first_key.objectid == (u64)-1)
16e7549f
JB
3356 first_key = ins_keys[i];
3357 } else {
3358 need_find_last_extent = false;
3359 }
3360
31ff1cd2
CM
3361 /* take a reference on file data extents so that truncates
3362 * or deletes of this inode don't have to relog the inode
3363 * again
3364 */
d2794405
LB
3365 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3366 !skip_csum) {
31ff1cd2
CM
3367 int found_type;
3368 extent = btrfs_item_ptr(src, start_slot + i,
3369 struct btrfs_file_extent_item);
3370
8e531cdf 3371 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3372 continue;
3373
31ff1cd2 3374 found_type = btrfs_file_extent_type(src, extent);
6f1fed77 3375 if (found_type == BTRFS_FILE_EXTENT_REG) {
5d4f98a2
YZ
3376 u64 ds, dl, cs, cl;
3377 ds = btrfs_file_extent_disk_bytenr(src,
3378 extent);
3379 /* ds == 0 is a hole */
3380 if (ds == 0)
3381 continue;
3382
3383 dl = btrfs_file_extent_disk_num_bytes(src,
3384 extent);
3385 cs = btrfs_file_extent_offset(src, extent);
3386 cl = btrfs_file_extent_num_bytes(src,
a419aef8 3387 extent);
580afd76
CM
3388 if (btrfs_file_extent_compression(src,
3389 extent)) {
3390 cs = 0;
3391 cl = dl;
3392 }
5d4f98a2
YZ
3393
3394 ret = btrfs_lookup_csums_range(
3395 log->fs_info->csum_root,
3396 ds + cs, ds + cs + cl - 1,
a2de733c 3397 &ordered_sums, 0);
3650860b
JB
3398 if (ret) {
3399 btrfs_release_path(dst_path);
3400 kfree(ins_data);
3401 return ret;
3402 }
31ff1cd2
CM
3403 }
3404 }
31ff1cd2
CM
3405 }
3406
3407 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
b3b4aa74 3408 btrfs_release_path(dst_path);
31ff1cd2 3409 kfree(ins_data);
d20f7043
CM
3410
3411 /*
3412 * we have to do this after the loop above to avoid changing the
3413 * log tree while trying to change the log tree.
3414 */
4a500fd1 3415 ret = 0;
d397712b 3416 while (!list_empty(&ordered_sums)) {
d20f7043
CM
3417 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3418 struct btrfs_ordered_sum,
3419 list);
4a500fd1
YZ
3420 if (!ret)
3421 ret = btrfs_csum_file_blocks(trans, log, sums);
d20f7043
CM
3422 list_del(&sums->list);
3423 kfree(sums);
3424 }
16e7549f
JB
3425
3426 if (!has_extents)
3427 return ret;
3428
74121f7c
FM
3429 if (need_find_last_extent && *last_extent == first_key.offset) {
3430 /*
3431 * We don't have any leafs between our current one and the one
3432 * we processed before that can have file extent items for our
3433 * inode (and have a generation number smaller than our current
3434 * transaction id).
3435 */
3436 need_find_last_extent = false;
3437 }
3438
16e7549f
JB
3439 /*
3440 * Because we use btrfs_search_forward we could skip leaves that were
3441 * not modified and then assume *last_extent is valid when it really
3442 * isn't. So back up to the previous leaf and read the end of the last
3443 * extent before we go and fill in holes.
3444 */
3445 if (need_find_last_extent) {
3446 u64 len;
3447
3448 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3449 if (ret < 0)
3450 return ret;
3451 if (ret)
3452 goto fill_holes;
3453 if (src_path->slots[0])
3454 src_path->slots[0]--;
3455 src = src_path->nodes[0];
3456 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3457 if (key.objectid != btrfs_ino(inode) ||
3458 key.type != BTRFS_EXTENT_DATA_KEY)
3459 goto fill_holes;
3460 extent = btrfs_item_ptr(src, src_path->slots[0],
3461 struct btrfs_file_extent_item);
3462 if (btrfs_file_extent_type(src, extent) ==
3463 BTRFS_FILE_EXTENT_INLINE) {
514ac8ad
CM
3464 len = btrfs_file_extent_inline_len(src,
3465 src_path->slots[0],
3466 extent);
16e7549f
JB
3467 *last_extent = ALIGN(key.offset + len,
3468 log->sectorsize);
3469 } else {
3470 len = btrfs_file_extent_num_bytes(src, extent);
3471 *last_extent = key.offset + len;
3472 }
3473 }
3474fill_holes:
3475 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3476 * things could have happened
3477 *
3478 * 1) A merge could have happened, so we could currently be on a leaf
3479 * that holds what we were copying in the first place.
3480 * 2) A split could have happened, and now not all of the items we want
3481 * are on the same leaf.
3482 *
3483 * So we need to adjust how we search for holes, we need to drop the
3484 * path and re-search for the first extent key we found, and then walk
3485 * forward until we hit the last one we copied.
3486 */
3487 if (need_find_last_extent) {
3488 /* btrfs_prev_leaf could return 1 without releasing the path */
3489 btrfs_release_path(src_path);
3490 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3491 src_path, 0, 0);
3492 if (ret < 0)
3493 return ret;
3494 ASSERT(ret == 0);
3495 src = src_path->nodes[0];
3496 i = src_path->slots[0];
3497 } else {
3498 i = start_slot;
3499 }
3500
3501 /*
3502 * Ok so here we need to go through and fill in any holes we may have
3503 * to make sure that holes are punched for those areas in case they had
3504 * extents previously.
3505 */
3506 while (!done) {
3507 u64 offset, len;
3508 u64 extent_end;
3509
3510 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3511 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3512 if (ret < 0)
3513 return ret;
3514 ASSERT(ret == 0);
3515 src = src_path->nodes[0];
3516 i = 0;
3517 }
3518
3519 btrfs_item_key_to_cpu(src, &key, i);
3520 if (!btrfs_comp_cpu_keys(&key, &last_key))
3521 done = true;
3522 if (key.objectid != btrfs_ino(inode) ||
3523 key.type != BTRFS_EXTENT_DATA_KEY) {
3524 i++;
3525 continue;
3526 }
3527 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3528 if (btrfs_file_extent_type(src, extent) ==
3529 BTRFS_FILE_EXTENT_INLINE) {
514ac8ad 3530 len = btrfs_file_extent_inline_len(src, i, extent);
16e7549f
JB
3531 extent_end = ALIGN(key.offset + len, log->sectorsize);
3532 } else {
3533 len = btrfs_file_extent_num_bytes(src, extent);
3534 extent_end = key.offset + len;
3535 }
3536 i++;
3537
3538 if (*last_extent == key.offset) {
3539 *last_extent = extent_end;
3540 continue;
3541 }
3542 offset = *last_extent;
3543 len = key.offset - *last_extent;
3544 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3545 offset, 0, 0, len, 0, len, 0,
3546 0, 0);
3547 if (ret)
3548 break;
74121f7c 3549 *last_extent = extent_end;
16e7549f
JB
3550 }
3551 /*
3552 * Need to let the callers know we dropped the path so they should
3553 * re-search.
3554 */
3555 if (!ret && need_find_last_extent)
3556 ret = 1;
4a500fd1 3557 return ret;
31ff1cd2
CM
3558}
3559
5dc562c5
JB
3560static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3561{
3562 struct extent_map *em1, *em2;
3563
3564 em1 = list_entry(a, struct extent_map, list);
3565 em2 = list_entry(b, struct extent_map, list);
3566
3567 if (em1->start < em2->start)
3568 return -1;
3569 else if (em1->start > em2->start)
3570 return 1;
3571 return 0;
3572}
3573
5dc562c5
JB
3574static int log_one_extent(struct btrfs_trans_handle *trans,
3575 struct inode *inode, struct btrfs_root *root,
827463c4
MX
3576 struct extent_map *em, struct btrfs_path *path,
3577 struct list_head *logged_list)
5dc562c5
JB
3578{
3579 struct btrfs_root *log = root->log_root;
70c8a91c
JB
3580 struct btrfs_file_extent_item *fi;
3581 struct extent_buffer *leaf;
2ab28f32 3582 struct btrfs_ordered_extent *ordered;
70c8a91c 3583 struct list_head ordered_sums;
0b1c6cca 3584 struct btrfs_map_token token;
5dc562c5 3585 struct btrfs_key key;
2ab28f32
JB
3586 u64 mod_start = em->mod_start;
3587 u64 mod_len = em->mod_len;
3588 u64 csum_offset;
3589 u64 csum_len;
70c8a91c
JB
3590 u64 extent_offset = em->start - em->orig_start;
3591 u64 block_len;
5dc562c5 3592 int ret;
70c8a91c 3593 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1acae57b 3594 int extent_inserted = 0;
09a2a8f9 3595
70c8a91c 3596 INIT_LIST_HEAD(&ordered_sums);
0b1c6cca 3597 btrfs_init_map_token(&token);
70c8a91c 3598
1acae57b
FDBM
3599 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3600 em->start + em->len, NULL, 0, 1,
3601 sizeof(*fi), &extent_inserted);
09a2a8f9 3602 if (ret)
70c8a91c 3603 return ret;
1acae57b
FDBM
3604
3605 if (!extent_inserted) {
3606 key.objectid = btrfs_ino(inode);
3607 key.type = BTRFS_EXTENT_DATA_KEY;
3608 key.offset = em->start;
3609
3610 ret = btrfs_insert_empty_item(trans, log, path, &key,
3611 sizeof(*fi));
3612 if (ret)
3613 return ret;
3614 }
70c8a91c
JB
3615 leaf = path->nodes[0];
3616 fi = btrfs_item_ptr(leaf, path->slots[0],
3617 struct btrfs_file_extent_item);
124fe663 3618
0b1c6cca
JB
3619 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3620 &token);
70c8a91c
JB
3621 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3622 skip_csum = true;
0b1c6cca
JB
3623 btrfs_set_token_file_extent_type(leaf, fi,
3624 BTRFS_FILE_EXTENT_PREALLOC,
3625 &token);
70c8a91c 3626 } else {
0b1c6cca
JB
3627 btrfs_set_token_file_extent_type(leaf, fi,
3628 BTRFS_FILE_EXTENT_REG,
3629 &token);
ed9e8af8 3630 if (em->block_start == EXTENT_MAP_HOLE)
70c8a91c
JB
3631 skip_csum = true;
3632 }
3633
3634 block_len = max(em->block_len, em->orig_block_len);
3635 if (em->compress_type != BTRFS_COMPRESS_NONE) {
0b1c6cca
JB
3636 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3637 em->block_start,
3638 &token);
3639 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3640 &token);
70c8a91c 3641 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
0b1c6cca
JB
3642 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3643 em->block_start -
3644 extent_offset, &token);
3645 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3646 &token);
70c8a91c 3647 } else {
0b1c6cca
JB
3648 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3649 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3650 &token);
3651 }
3652
3653 btrfs_set_token_file_extent_offset(leaf, fi,
3654 em->start - em->orig_start,
3655 &token);
3656 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
cc95bef6 3657 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
0b1c6cca
JB
3658 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3659 &token);
3660 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3661 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
70c8a91c 3662 btrfs_mark_buffer_dirty(leaf);
0aa4a17d 3663
70c8a91c 3664 btrfs_release_path(path);
70c8a91c
JB
3665 if (ret) {
3666 return ret;
3667 }
0aa4a17d 3668
70c8a91c
JB
3669 if (skip_csum)
3670 return 0;
5dc562c5 3671
2ab28f32
JB
3672 /*
3673 * First check and see if our csums are on our outstanding ordered
3674 * extents.
3675 */
827463c4 3676 list_for_each_entry(ordered, logged_list, log_list) {
2ab28f32
JB
3677 struct btrfs_ordered_sum *sum;
3678
3679 if (!mod_len)
3680 break;
3681
2ab28f32
JB
3682 if (ordered->file_offset + ordered->len <= mod_start ||
3683 mod_start + mod_len <= ordered->file_offset)
3684 continue;
3685
3686 /*
3687 * We are going to copy all the csums on this ordered extent, so
3688 * go ahead and adjust mod_start and mod_len in case this
3689 * ordered extent has already been logged.
3690 */
3691 if (ordered->file_offset > mod_start) {
3692 if (ordered->file_offset + ordered->len >=
3693 mod_start + mod_len)
3694 mod_len = ordered->file_offset - mod_start;
3695 /*
3696 * If we have this case
3697 *
3698 * |--------- logged extent ---------|
3699 * |----- ordered extent ----|
3700 *
3701 * Just don't mess with mod_start and mod_len, we'll
3702 * just end up logging more csums than we need and it
3703 * will be ok.
3704 */
3705 } else {
3706 if (ordered->file_offset + ordered->len <
3707 mod_start + mod_len) {
3708 mod_len = (mod_start + mod_len) -
3709 (ordered->file_offset + ordered->len);
3710 mod_start = ordered->file_offset +
3711 ordered->len;
3712 } else {
3713 mod_len = 0;
3714 }
3715 }
3716
3717 /*
3718 * To keep us from looping for the above case of an ordered
3719 * extent that falls inside of the logged extent.
3720 */
3721 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3722 &ordered->flags))
3723 continue;
2ab28f32 3724
23c671a5
MX
3725 if (ordered->csum_bytes_left) {
3726 btrfs_start_ordered_extent(inode, ordered, 0);
3727 wait_event(ordered->wait,
3728 ordered->csum_bytes_left == 0);
3729 }
2ab28f32
JB
3730
3731 list_for_each_entry(sum, &ordered->list, list) {
3732 ret = btrfs_csum_file_blocks(trans, log, sum);
827463c4 3733 if (ret)
2ab28f32 3734 goto unlocked;
2ab28f32 3735 }
2ab28f32
JB
3736
3737 }
2ab28f32
JB
3738unlocked:
3739
3740 if (!mod_len || ret)
3741 return ret;
3742
488111aa
FDBM
3743 if (em->compress_type) {
3744 csum_offset = 0;
3745 csum_len = block_len;
3746 } else {
3747 csum_offset = mod_start - em->start;
3748 csum_len = mod_len;
3749 }
2ab28f32 3750
70c8a91c
JB
3751 /* block start is already adjusted for the file extent offset. */
3752 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3753 em->block_start + csum_offset,
3754 em->block_start + csum_offset +
3755 csum_len - 1, &ordered_sums, 0);
3756 if (ret)
3757 return ret;
5dc562c5 3758
70c8a91c
JB
3759 while (!list_empty(&ordered_sums)) {
3760 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3761 struct btrfs_ordered_sum,
3762 list);
3763 if (!ret)
3764 ret = btrfs_csum_file_blocks(trans, log, sums);
3765 list_del(&sums->list);
3766 kfree(sums);
5dc562c5
JB
3767 }
3768
70c8a91c 3769 return ret;
5dc562c5
JB
3770}
3771
3772static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3773 struct btrfs_root *root,
3774 struct inode *inode,
827463c4
MX
3775 struct btrfs_path *path,
3776 struct list_head *logged_list)
5dc562c5 3777{
5dc562c5
JB
3778 struct extent_map *em, *n;
3779 struct list_head extents;
3780 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3781 u64 test_gen;
3782 int ret = 0;
2ab28f32 3783 int num = 0;
5dc562c5
JB
3784
3785 INIT_LIST_HEAD(&extents);
3786
5dc562c5
JB
3787 write_lock(&tree->lock);
3788 test_gen = root->fs_info->last_trans_committed;
3789
3790 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3791 list_del_init(&em->list);
2ab28f32
JB
3792
3793 /*
3794 * Just an arbitrary number, this can be really CPU intensive
3795 * once we start getting a lot of extents, and really once we
3796 * have a bunch of extents we just want to commit since it will
3797 * be faster.
3798 */
3799 if (++num > 32768) {
3800 list_del_init(&tree->modified_extents);
3801 ret = -EFBIG;
3802 goto process;
3803 }
3804
5dc562c5
JB
3805 if (em->generation <= test_gen)
3806 continue;
ff44c6e3
JB
3807 /* Need a ref to keep it from getting evicted from cache */
3808 atomic_inc(&em->refs);
3809 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
5dc562c5 3810 list_add_tail(&em->list, &extents);
2ab28f32 3811 num++;
5dc562c5
JB
3812 }
3813
3814 list_sort(NULL, &extents, extent_cmp);
3815
2ab28f32 3816process:
5dc562c5
JB
3817 while (!list_empty(&extents)) {
3818 em = list_entry(extents.next, struct extent_map, list);
3819
3820 list_del_init(&em->list);
3821
3822 /*
3823 * If we had an error we just need to delete everybody from our
3824 * private list.
3825 */
ff44c6e3 3826 if (ret) {
201a9038 3827 clear_em_logging(tree, em);
ff44c6e3 3828 free_extent_map(em);
5dc562c5 3829 continue;
ff44c6e3
JB
3830 }
3831
3832 write_unlock(&tree->lock);
5dc562c5 3833
827463c4 3834 ret = log_one_extent(trans, inode, root, em, path, logged_list);
ff44c6e3 3835 write_lock(&tree->lock);
201a9038
JB
3836 clear_em_logging(tree, em);
3837 free_extent_map(em);
5dc562c5 3838 }
ff44c6e3
JB
3839 WARN_ON(!list_empty(&extents));
3840 write_unlock(&tree->lock);
5dc562c5 3841
5dc562c5 3842 btrfs_release_path(path);
5dc562c5
JB
3843 return ret;
3844}
3845
e02119d5
CM
3846/* log a single inode in the tree log.
3847 * At least one parent directory for this inode must exist in the tree
3848 * or be logged already.
3849 *
3850 * Any items from this inode changed by the current transaction are copied
3851 * to the log tree. An extra reference is taken on any extents in this
3852 * file, allowing us to avoid a whole pile of corner cases around logging
3853 * blocks that have been removed from the tree.
3854 *
3855 * See LOG_INODE_ALL and related defines for a description of what inode_only
3856 * does.
3857 *
3858 * This handles both files and directories.
3859 */
12fcfd22 3860static int btrfs_log_inode(struct btrfs_trans_handle *trans,
e02119d5
CM
3861 struct btrfs_root *root, struct inode *inode,
3862 int inode_only)
3863{
3864 struct btrfs_path *path;
3865 struct btrfs_path *dst_path;
3866 struct btrfs_key min_key;
3867 struct btrfs_key max_key;
3868 struct btrfs_root *log = root->log_root;
31ff1cd2 3869 struct extent_buffer *src = NULL;
827463c4 3870 LIST_HEAD(logged_list);
16e7549f 3871 u64 last_extent = 0;
4a500fd1 3872 int err = 0;
e02119d5 3873 int ret;
3a5f1d45 3874 int nritems;
31ff1cd2
CM
3875 int ins_start_slot = 0;
3876 int ins_nr;
5dc562c5 3877 bool fast_search = false;
33345d01 3878 u64 ino = btrfs_ino(inode);
e02119d5 3879
e02119d5 3880 path = btrfs_alloc_path();
5df67083
TI
3881 if (!path)
3882 return -ENOMEM;
e02119d5 3883 dst_path = btrfs_alloc_path();
5df67083
TI
3884 if (!dst_path) {
3885 btrfs_free_path(path);
3886 return -ENOMEM;
3887 }
e02119d5 3888
33345d01 3889 min_key.objectid = ino;
e02119d5
CM
3890 min_key.type = BTRFS_INODE_ITEM_KEY;
3891 min_key.offset = 0;
3892
33345d01 3893 max_key.objectid = ino;
12fcfd22 3894
12fcfd22 3895
5dc562c5 3896 /* today the code can only do partial logging of directories */
5269b67e
MX
3897 if (S_ISDIR(inode->i_mode) ||
3898 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3899 &BTRFS_I(inode)->runtime_flags) &&
3900 inode_only == LOG_INODE_EXISTS))
e02119d5
CM
3901 max_key.type = BTRFS_XATTR_ITEM_KEY;
3902 else
3903 max_key.type = (u8)-1;
3904 max_key.offset = (u64)-1;
3905
94edf4ae
JB
3906 /* Only run delayed items if we are a dir or a new file */
3907 if (S_ISDIR(inode->i_mode) ||
3908 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3909 ret = btrfs_commit_inode_delayed_items(trans, inode);
3910 if (ret) {
3911 btrfs_free_path(path);
3912 btrfs_free_path(dst_path);
3913 return ret;
3914 }
16cdcec7
MX
3915 }
3916
e02119d5
CM
3917 mutex_lock(&BTRFS_I(inode)->log_mutex);
3918
827463c4 3919 btrfs_get_logged_extents(inode, &logged_list);
2ab28f32 3920
e02119d5
CM
3921 /*
3922 * a brute force approach to making sure we get the most uptodate
3923 * copies of everything.
3924 */
3925 if (S_ISDIR(inode->i_mode)) {
3926 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3927
3928 if (inode_only == LOG_INODE_EXISTS)
3929 max_key_type = BTRFS_XATTR_ITEM_KEY;
33345d01 3930 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
e02119d5 3931 } else {
5dc562c5
JB
3932 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3933 &BTRFS_I(inode)->runtime_flags)) {
e9976151
JB
3934 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3935 &BTRFS_I(inode)->runtime_flags);
5dc562c5
JB
3936 ret = btrfs_truncate_inode_items(trans, log,
3937 inode, 0, 0);
a95249b3 3938 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
6cfab851
JB
3939 &BTRFS_I(inode)->runtime_flags) ||
3940 inode_only == LOG_INODE_EXISTS) {
183f37fa
LB
3941 if (inode_only == LOG_INODE_ALL)
3942 fast_search = true;
a95249b3 3943 max_key.type = BTRFS_XATTR_ITEM_KEY;
5dc562c5 3944 ret = drop_objectid_items(trans, log, path, ino,
e9976151 3945 max_key.type);
a95249b3
JB
3946 } else {
3947 if (inode_only == LOG_INODE_ALL)
3948 fast_search = true;
3949 ret = log_inode_item(trans, log, dst_path, inode);
3950 if (ret) {
3951 err = ret;
3952 goto out_unlock;
3953 }
3954 goto log_extents;
5dc562c5 3955 }
a95249b3 3956
e02119d5 3957 }
4a500fd1
YZ
3958 if (ret) {
3959 err = ret;
3960 goto out_unlock;
3961 }
e02119d5
CM
3962 path->keep_locks = 1;
3963
d397712b 3964 while (1) {
31ff1cd2 3965 ins_nr = 0;
6174d3cb 3966 ret = btrfs_search_forward(root, &min_key,
de78b51a 3967 path, trans->transid);
e02119d5
CM
3968 if (ret != 0)
3969 break;
3a5f1d45 3970again:
31ff1cd2 3971 /* note, ins_nr might be > 0 here, cleanup outside the loop */
33345d01 3972 if (min_key.objectid != ino)
e02119d5
CM
3973 break;
3974 if (min_key.type > max_key.type)
3975 break;
31ff1cd2 3976
e02119d5 3977 src = path->nodes[0];
31ff1cd2
CM
3978 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3979 ins_nr++;
3980 goto next_slot;
3981 } else if (!ins_nr) {
3982 ins_start_slot = path->slots[0];
3983 ins_nr = 1;
3984 goto next_slot;
e02119d5
CM
3985 }
3986
16e7549f
JB
3987 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3988 ins_start_slot, ins_nr, inode_only);
3989 if (ret < 0) {
4a500fd1
YZ
3990 err = ret;
3991 goto out_unlock;
16e7549f
JB
3992 } if (ret) {
3993 ins_nr = 0;
3994 btrfs_release_path(path);
3995 continue;
4a500fd1 3996 }
31ff1cd2
CM
3997 ins_nr = 1;
3998 ins_start_slot = path->slots[0];
3999next_slot:
e02119d5 4000
3a5f1d45
CM
4001 nritems = btrfs_header_nritems(path->nodes[0]);
4002 path->slots[0]++;
4003 if (path->slots[0] < nritems) {
4004 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4005 path->slots[0]);
4006 goto again;
4007 }
31ff1cd2 4008 if (ins_nr) {
16e7549f
JB
4009 ret = copy_items(trans, inode, dst_path, path,
4010 &last_extent, ins_start_slot,
31ff1cd2 4011 ins_nr, inode_only);
16e7549f 4012 if (ret < 0) {
4a500fd1
YZ
4013 err = ret;
4014 goto out_unlock;
4015 }
16e7549f 4016 ret = 0;
31ff1cd2
CM
4017 ins_nr = 0;
4018 }
b3b4aa74 4019 btrfs_release_path(path);
3a5f1d45 4020
3d41d702 4021 if (min_key.offset < (u64)-1) {
e02119d5 4022 min_key.offset++;
3d41d702 4023 } else if (min_key.type < max_key.type) {
e02119d5 4024 min_key.type++;
3d41d702
FDBM
4025 min_key.offset = 0;
4026 } else {
e02119d5 4027 break;
3d41d702 4028 }
e02119d5 4029 }
31ff1cd2 4030 if (ins_nr) {
16e7549f
JB
4031 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4032 ins_start_slot, ins_nr, inode_only);
4033 if (ret < 0) {
4a500fd1
YZ
4034 err = ret;
4035 goto out_unlock;
4036 }
16e7549f 4037 ret = 0;
31ff1cd2
CM
4038 ins_nr = 0;
4039 }
5dc562c5 4040
a95249b3 4041log_extents:
f3b15ccd
JB
4042 btrfs_release_path(path);
4043 btrfs_release_path(dst_path);
5dc562c5 4044 if (fast_search) {
827463c4
MX
4045 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4046 &logged_list);
5dc562c5
JB
4047 if (ret) {
4048 err = ret;
4049 goto out_unlock;
4050 }
d006a048 4051 } else if (inode_only == LOG_INODE_ALL) {
06d3d22b
LB
4052 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4053 struct extent_map *em, *n;
4054
bbe14267 4055 write_lock(&tree->lock);
06d3d22b
LB
4056 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
4057 list_del_init(&em->list);
bbe14267 4058 write_unlock(&tree->lock);
5dc562c5
JB
4059 }
4060
9623f9a3 4061 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
e02119d5 4062 ret = log_directory_changes(trans, root, inode, path, dst_path);
4a500fd1
YZ
4063 if (ret) {
4064 err = ret;
4065 goto out_unlock;
4066 }
e02119d5 4067 }
3a5f1d45 4068 BTRFS_I(inode)->logged_trans = trans->transid;
46d8bc34 4069 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
4a500fd1 4070out_unlock:
827463c4
MX
4071 if (unlikely(err))
4072 btrfs_put_logged_extents(&logged_list);
4073 else
4074 btrfs_submit_logged_extents(&logged_list, log);
e02119d5
CM
4075 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4076
4077 btrfs_free_path(path);
4078 btrfs_free_path(dst_path);
4a500fd1 4079 return err;
e02119d5
CM
4080}
4081
12fcfd22
CM
4082/*
4083 * follow the dentry parent pointers up the chain and see if any
4084 * of the directories in it require a full commit before they can
4085 * be logged. Returns zero if nothing special needs to be done or 1 if
4086 * a full commit is required.
4087 */
4088static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4089 struct inode *inode,
4090 struct dentry *parent,
4091 struct super_block *sb,
4092 u64 last_committed)
e02119d5 4093{
12fcfd22
CM
4094 int ret = 0;
4095 struct btrfs_root *root;
6a912213 4096 struct dentry *old_parent = NULL;
de2b530b 4097 struct inode *orig_inode = inode;
e02119d5 4098
af4176b4
CM
4099 /*
4100 * for regular files, if its inode is already on disk, we don't
4101 * have to worry about the parents at all. This is because
4102 * we can use the last_unlink_trans field to record renames
4103 * and other fun in this file.
4104 */
4105 if (S_ISREG(inode->i_mode) &&
4106 BTRFS_I(inode)->generation <= last_committed &&
4107 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4108 goto out;
4109
12fcfd22
CM
4110 if (!S_ISDIR(inode->i_mode)) {
4111 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4112 goto out;
4113 inode = parent->d_inode;
4114 }
4115
4116 while (1) {
de2b530b
JB
4117 /*
4118 * If we are logging a directory then we start with our inode,
4119 * not our parents inode, so we need to skipp setting the
4120 * logged_trans so that further down in the log code we don't
4121 * think this inode has already been logged.
4122 */
4123 if (inode != orig_inode)
4124 BTRFS_I(inode)->logged_trans = trans->transid;
12fcfd22
CM
4125 smp_mb();
4126
4127 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4128 root = BTRFS_I(inode)->root;
4129
4130 /*
4131 * make sure any commits to the log are forced
4132 * to be full commits
4133 */
995946dd 4134 btrfs_set_log_full_commit(root->fs_info, trans);
12fcfd22
CM
4135 ret = 1;
4136 break;
4137 }
4138
4139 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4140 break;
4141
76dda93c 4142 if (IS_ROOT(parent))
12fcfd22
CM
4143 break;
4144
6a912213
JB
4145 parent = dget_parent(parent);
4146 dput(old_parent);
4147 old_parent = parent;
12fcfd22
CM
4148 inode = parent->d_inode;
4149
4150 }
6a912213 4151 dput(old_parent);
12fcfd22 4152out:
e02119d5
CM
4153 return ret;
4154}
4155
4156/*
4157 * helper function around btrfs_log_inode to make sure newly created
4158 * parent directories also end up in the log. A minimal inode and backref
4159 * only logging is done of any parent directories that are older than
4160 * the last committed transaction
4161 */
48a3b636
ES
4162static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4163 struct btrfs_root *root, struct inode *inode,
8b050d35
MX
4164 struct dentry *parent, int exists_only,
4165 struct btrfs_log_ctx *ctx)
e02119d5 4166{
12fcfd22 4167 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
e02119d5 4168 struct super_block *sb;
6a912213 4169 struct dentry *old_parent = NULL;
12fcfd22
CM
4170 int ret = 0;
4171 u64 last_committed = root->fs_info->last_trans_committed;
4172
4173 sb = inode->i_sb;
4174
3a5e1404
SW
4175 if (btrfs_test_opt(root, NOTREELOG)) {
4176 ret = 1;
4177 goto end_no_trans;
4178 }
4179
995946dd
MX
4180 /*
4181 * The prev transaction commit doesn't complete, we need do
4182 * full commit by ourselves.
4183 */
12fcfd22
CM
4184 if (root->fs_info->last_trans_log_full_commit >
4185 root->fs_info->last_trans_committed) {
4186 ret = 1;
4187 goto end_no_trans;
4188 }
4189
76dda93c
YZ
4190 if (root != BTRFS_I(inode)->root ||
4191 btrfs_root_refs(&root->root_item) == 0) {
4192 ret = 1;
4193 goto end_no_trans;
4194 }
4195
12fcfd22
CM
4196 ret = check_parent_dirs_for_sync(trans, inode, parent,
4197 sb, last_committed);
4198 if (ret)
4199 goto end_no_trans;
e02119d5 4200
22ee6985 4201 if (btrfs_inode_in_log(inode, trans->transid)) {
257c62e1
CM
4202 ret = BTRFS_NO_LOG_SYNC;
4203 goto end_no_trans;
4204 }
4205
8b050d35 4206 ret = start_log_trans(trans, root, ctx);
4a500fd1 4207 if (ret)
e87ac136 4208 goto end_no_trans;
e02119d5 4209
12fcfd22 4210 ret = btrfs_log_inode(trans, root, inode, inode_only);
4a500fd1
YZ
4211 if (ret)
4212 goto end_trans;
12fcfd22 4213
af4176b4
CM
4214 /*
4215 * for regular files, if its inode is already on disk, we don't
4216 * have to worry about the parents at all. This is because
4217 * we can use the last_unlink_trans field to record renames
4218 * and other fun in this file.
4219 */
4220 if (S_ISREG(inode->i_mode) &&
4221 BTRFS_I(inode)->generation <= last_committed &&
4a500fd1
YZ
4222 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4223 ret = 0;
4224 goto end_trans;
4225 }
af4176b4
CM
4226
4227 inode_only = LOG_INODE_EXISTS;
12fcfd22
CM
4228 while (1) {
4229 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
e02119d5
CM
4230 break;
4231
12fcfd22 4232 inode = parent->d_inode;
76dda93c
YZ
4233 if (root != BTRFS_I(inode)->root)
4234 break;
4235
12fcfd22
CM
4236 if (BTRFS_I(inode)->generation >
4237 root->fs_info->last_trans_committed) {
4238 ret = btrfs_log_inode(trans, root, inode, inode_only);
4a500fd1
YZ
4239 if (ret)
4240 goto end_trans;
12fcfd22 4241 }
76dda93c 4242 if (IS_ROOT(parent))
e02119d5 4243 break;
12fcfd22 4244
6a912213
JB
4245 parent = dget_parent(parent);
4246 dput(old_parent);
4247 old_parent = parent;
e02119d5 4248 }
12fcfd22 4249 ret = 0;
4a500fd1 4250end_trans:
6a912213 4251 dput(old_parent);
4a500fd1 4252 if (ret < 0) {
995946dd 4253 btrfs_set_log_full_commit(root->fs_info, trans);
4a500fd1
YZ
4254 ret = 1;
4255 }
8b050d35
MX
4256
4257 if (ret)
4258 btrfs_remove_log_ctx(root, ctx);
12fcfd22
CM
4259 btrfs_end_log_trans(root);
4260end_no_trans:
4261 return ret;
e02119d5
CM
4262}
4263
4264/*
4265 * it is not safe to log dentry if the chunk root has added new
4266 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4267 * If this returns 1, you must commit the transaction to safely get your
4268 * data on disk.
4269 */
4270int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
8b050d35
MX
4271 struct btrfs_root *root, struct dentry *dentry,
4272 struct btrfs_log_ctx *ctx)
e02119d5 4273{
6a912213
JB
4274 struct dentry *parent = dget_parent(dentry);
4275 int ret;
4276
8b050d35
MX
4277 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
4278 0, ctx);
6a912213
JB
4279 dput(parent);
4280
4281 return ret;
e02119d5
CM
4282}
4283
4284/*
4285 * should be called during mount to recover any replay any log trees
4286 * from the FS
4287 */
4288int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4289{
4290 int ret;
4291 struct btrfs_path *path;
4292 struct btrfs_trans_handle *trans;
4293 struct btrfs_key key;
4294 struct btrfs_key found_key;
4295 struct btrfs_key tmp_key;
4296 struct btrfs_root *log;
4297 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4298 struct walk_control wc = {
4299 .process_func = process_one_buffer,
4300 .stage = 0,
4301 };
4302
e02119d5 4303 path = btrfs_alloc_path();
db5b493a
TI
4304 if (!path)
4305 return -ENOMEM;
4306
4307 fs_info->log_root_recovering = 1;
e02119d5 4308
4a500fd1 4309 trans = btrfs_start_transaction(fs_info->tree_root, 0);
79787eaa
JM
4310 if (IS_ERR(trans)) {
4311 ret = PTR_ERR(trans);
4312 goto error;
4313 }
e02119d5
CM
4314
4315 wc.trans = trans;
4316 wc.pin = 1;
4317
db5b493a 4318 ret = walk_log_tree(trans, log_root_tree, &wc);
79787eaa
JM
4319 if (ret) {
4320 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4321 "recovering log root tree.");
4322 goto error;
4323 }
e02119d5
CM
4324
4325again:
4326 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4327 key.offset = (u64)-1;
4328 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4329
d397712b 4330 while (1) {
e02119d5 4331 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
79787eaa
JM
4332
4333 if (ret < 0) {
4334 btrfs_error(fs_info, ret,
4335 "Couldn't find tree log root.");
4336 goto error;
4337 }
e02119d5
CM
4338 if (ret > 0) {
4339 if (path->slots[0] == 0)
4340 break;
4341 path->slots[0]--;
4342 }
4343 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4344 path->slots[0]);
b3b4aa74 4345 btrfs_release_path(path);
e02119d5
CM
4346 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4347 break;
4348
cb517eab 4349 log = btrfs_read_fs_root(log_root_tree, &found_key);
79787eaa
JM
4350 if (IS_ERR(log)) {
4351 ret = PTR_ERR(log);
4352 btrfs_error(fs_info, ret,
4353 "Couldn't read tree log root.");
4354 goto error;
4355 }
e02119d5
CM
4356
4357 tmp_key.objectid = found_key.offset;
4358 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4359 tmp_key.offset = (u64)-1;
4360
4361 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
79787eaa
JM
4362 if (IS_ERR(wc.replay_dest)) {
4363 ret = PTR_ERR(wc.replay_dest);
b50c6e25
JB
4364 free_extent_buffer(log->node);
4365 free_extent_buffer(log->commit_root);
4366 kfree(log);
79787eaa
JM
4367 btrfs_error(fs_info, ret, "Couldn't read target root "
4368 "for tree log recovery.");
4369 goto error;
4370 }
e02119d5 4371
07d400a6 4372 wc.replay_dest->log_root = log;
5d4f98a2 4373 btrfs_record_root_in_trans(trans, wc.replay_dest);
e02119d5 4374 ret = walk_log_tree(trans, log, &wc);
e02119d5 4375
b50c6e25 4376 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
e02119d5
CM
4377 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4378 path);
e02119d5
CM
4379 }
4380
4381 key.offset = found_key.offset - 1;
07d400a6 4382 wc.replay_dest->log_root = NULL;
e02119d5 4383 free_extent_buffer(log->node);
b263c2c8 4384 free_extent_buffer(log->commit_root);
e02119d5
CM
4385 kfree(log);
4386
b50c6e25
JB
4387 if (ret)
4388 goto error;
4389
e02119d5
CM
4390 if (found_key.offset == 0)
4391 break;
4392 }
b3b4aa74 4393 btrfs_release_path(path);
e02119d5
CM
4394
4395 /* step one is to pin it all, step two is to replay just inodes */
4396 if (wc.pin) {
4397 wc.pin = 0;
4398 wc.process_func = replay_one_buffer;
4399 wc.stage = LOG_WALK_REPLAY_INODES;
4400 goto again;
4401 }
4402 /* step three is to replay everything */
4403 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4404 wc.stage++;
4405 goto again;
4406 }
4407
4408 btrfs_free_path(path);
4409
abefa55a
JB
4410 /* step 4: commit the transaction, which also unpins the blocks */
4411 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4412 if (ret)
4413 return ret;
4414
e02119d5
CM
4415 free_extent_buffer(log_root_tree->node);
4416 log_root_tree->log_root = NULL;
4417 fs_info->log_root_recovering = 0;
e02119d5 4418 kfree(log_root_tree);
79787eaa 4419
abefa55a 4420 return 0;
79787eaa 4421error:
b50c6e25
JB
4422 if (wc.trans)
4423 btrfs_end_transaction(wc.trans, fs_info->tree_root);
79787eaa
JM
4424 btrfs_free_path(path);
4425 return ret;
e02119d5 4426}
12fcfd22
CM
4427
4428/*
4429 * there are some corner cases where we want to force a full
4430 * commit instead of allowing a directory to be logged.
4431 *
4432 * They revolve around files there were unlinked from the directory, and
4433 * this function updates the parent directory so that a full commit is
4434 * properly done if it is fsync'd later after the unlinks are done.
4435 */
4436void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4437 struct inode *dir, struct inode *inode,
4438 int for_rename)
4439{
af4176b4
CM
4440 /*
4441 * when we're logging a file, if it hasn't been renamed
4442 * or unlinked, and its inode is fully committed on disk,
4443 * we don't have to worry about walking up the directory chain
4444 * to log its parents.
4445 *
4446 * So, we use the last_unlink_trans field to put this transid
4447 * into the file. When the file is logged we check it and
4448 * don't log the parents if the file is fully on disk.
4449 */
4450 if (S_ISREG(inode->i_mode))
4451 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4452
12fcfd22
CM
4453 /*
4454 * if this directory was already logged any new
4455 * names for this file/dir will get recorded
4456 */
4457 smp_mb();
4458 if (BTRFS_I(dir)->logged_trans == trans->transid)
4459 return;
4460
4461 /*
4462 * if the inode we're about to unlink was logged,
4463 * the log will be properly updated for any new names
4464 */
4465 if (BTRFS_I(inode)->logged_trans == trans->transid)
4466 return;
4467
4468 /*
4469 * when renaming files across directories, if the directory
4470 * there we're unlinking from gets fsync'd later on, there's
4471 * no way to find the destination directory later and fsync it
4472 * properly. So, we have to be conservative and force commits
4473 * so the new name gets discovered.
4474 */
4475 if (for_rename)
4476 goto record;
4477
4478 /* we can safely do the unlink without any special recording */
4479 return;
4480
4481record:
4482 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4483}
4484
4485/*
4486 * Call this after adding a new name for a file and it will properly
4487 * update the log to reflect the new name.
4488 *
4489 * It will return zero if all goes well, and it will return 1 if a
4490 * full transaction commit is required.
4491 */
4492int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4493 struct inode *inode, struct inode *old_dir,
4494 struct dentry *parent)
4495{
4496 struct btrfs_root * root = BTRFS_I(inode)->root;
4497
af4176b4
CM
4498 /*
4499 * this will force the logging code to walk the dentry chain
4500 * up for the file
4501 */
4502 if (S_ISREG(inode->i_mode))
4503 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4504
12fcfd22
CM
4505 /*
4506 * if this inode hasn't been logged and directory we're renaming it
4507 * from hasn't been logged, we don't need to log it
4508 */
4509 if (BTRFS_I(inode)->logged_trans <=
4510 root->fs_info->last_trans_committed &&
4511 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4512 root->fs_info->last_trans_committed))
4513 return 0;
4514
8b050d35 4515 return btrfs_log_inode_parent(trans, root, inode, parent, 1, NULL);
12fcfd22
CM
4516}
4517