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