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