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