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