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