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