blob: 5a4e10b9ac3e9e12ff5b4a0675b1796fdfd4c81f [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Chris Masone02119d2008-09-05 16:13:11 -040023#include "ctree.h"
24#include "transaction.h"
25#include "disk-io.h"
26#include "locking.h"
27#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "backref.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050029#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070030#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
40
41/*
Chris Mason12fcfd22009-03-24 10:24:20 -040042 * directory trouble cases
43 *
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
48 *
49 * mkdir foo/some_dir
50 * normal commit
51 * rename foo/some_dir foo2/some_dir
52 * mkdir foo/some_dir
53 * fsync foo/some_dir/some_file
54 *
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
58 *
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
61 *
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
65 *
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
68 *
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
71 *
72 * mkdir f1/foo
73 * normal commit
74 * rm -rf f1/foo
75 * fsync(f1)
76 *
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
81 * ugly details.
82 */
83
84/*
Chris Masone02119d2008-09-05 16:13:11 -040085 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
89 *
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
92 */
93#define LOG_WALK_PIN_ONLY 0
94#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040095#define LOG_WALK_REPLAY_DIR_INDEX 2
96#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040097
Chris Mason12fcfd22009-03-24 10:24:20 -040098static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040099 struct btrfs_root *root, struct inode *inode,
100 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400104static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_root *log,
107 struct btrfs_path *path,
108 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root)
140{
141 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400142 int err = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -0500143
144 mutex_lock(&root->log_mutex);
145 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
147 root->log_start_pid = current->pid;
148 root->log_multiple_pids = false;
149 } else if (root->log_start_pid != current->pid) {
150 root->log_multiple_pids = true;
151 }
152
Miao Xie2ecb7922012-09-06 04:04:27 -0600153 atomic_inc(&root->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -0500154 atomic_inc(&root->log_writers);
155 mutex_unlock(&root->log_mutex);
156 return 0;
157 }
Josef Bacikff782e02009-10-08 15:30:04 -0400158 root->log_multiple_pids = false;
159 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400160 mutex_lock(&root->fs_info->tree_log_mutex);
161 if (!root->fs_info->log_root_tree) {
162 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400163 if (ret)
164 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400165 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400166 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400167 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400168 if (ret)
169 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400170 }
Chris Masone02119d2008-09-05 16:13:11 -0400171 mutex_unlock(&root->fs_info->tree_log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -0600172 atomic_inc(&root->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -0500173 atomic_inc(&root->log_writers);
174 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400175 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400176}
177
178/*
179 * returns 0 if there was a log transaction running and we were able
180 * to join, or returns -ENOENT if there were not transactions
181 * in progress
182 */
183static int join_running_log_trans(struct btrfs_root *root)
184{
185 int ret = -ENOENT;
186
187 smp_mb();
188 if (!root->log_root)
189 return -ENOENT;
190
Yan Zheng7237f1832009-01-21 12:54:03 -0500191 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400192 if (root->log_root) {
193 ret = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -0500194 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400195 }
Yan Zheng7237f1832009-01-21 12:54:03 -0500196 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400197 return ret;
198}
199
200/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400201 * This either makes the current running log transaction wait
202 * until you call btrfs_end_log_trans() or it makes any future
203 * log transactions wait until you call btrfs_end_log_trans()
204 */
205int btrfs_pin_log_trans(struct btrfs_root *root)
206{
207 int ret = -ENOENT;
208
209 mutex_lock(&root->log_mutex);
210 atomic_inc(&root->log_writers);
211 mutex_unlock(&root->log_mutex);
212 return ret;
213}
214
215/*
Chris Masone02119d2008-09-05 16:13:11 -0400216 * indicate we're done making changes to the log tree
217 * and wake up anyone waiting to do a sync
218 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100219void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400220{
Yan Zheng7237f1832009-01-21 12:54:03 -0500221 if (atomic_dec_and_test(&root->log_writers)) {
222 smp_mb();
223 if (waitqueue_active(&root->log_writer_wait))
224 wake_up(&root->log_writer_wait);
225 }
Chris Masone02119d2008-09-05 16:13:11 -0400226}
227
228
229/*
230 * the walk control struct is used to pass state down the chain when
231 * processing the log tree. The stage field tells us which part
232 * of the log tree processing we are currently doing. The others
233 * are state fields used for that specific part
234 */
235struct walk_control {
236 /* should we free the extent on disk when done? This is used
237 * at transaction commit time while freeing a log tree
238 */
239 int free;
240
241 /* should we write out the extent buffer? This is used
242 * while flushing the log tree to disk during a sync
243 */
244 int write;
245
246 /* should we wait for the extent buffer io to finish? Also used
247 * while flushing the log tree to disk for a sync
248 */
249 int wait;
250
251 /* pin only walk, we record which extents on disk belong to the
252 * log trees
253 */
254 int pin;
255
256 /* what stage of the replay code we're currently in */
257 int stage;
258
259 /* the root we are currently replaying */
260 struct btrfs_root *replay_dest;
261
262 /* the trans handle for the current replay */
263 struct btrfs_trans_handle *trans;
264
265 /* the function that gets used to process blocks we find in the
266 * tree. Note the extent_buffer might not be up to date when it is
267 * passed in, and it must be checked or read if you need the data
268 * inside it
269 */
270 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
271 struct walk_control *wc, u64 gen);
272};
273
274/*
275 * process_func used to pin down extents, write them or wait on them
276 */
277static int process_one_buffer(struct btrfs_root *log,
278 struct extent_buffer *eb,
279 struct walk_control *wc, u64 gen)
280{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400281 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400282
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400283 /*
284 * If this fs is mixed then we need to be able to process the leaves to
285 * pin down any logged extents, so we have to read the block.
286 */
287 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
288 ret = btrfs_read_buffer(eb, gen);
289 if (ret)
290 return ret;
291 }
292
Josef Bacikb50c6e22013-04-25 15:55:30 -0400293 if (wc->pin)
294 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
295 eb->start, eb->len);
296
297 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400298 if (wc->pin && btrfs_header_level(eb) == 0)
299 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400300 if (wc->write)
301 btrfs_write_tree_block(eb);
302 if (wc->wait)
303 btrfs_wait_tree_block_writeback(eb);
304 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400305 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400306}
307
308/*
309 * Item overwrite used by replay and tree logging. eb, slot and key all refer
310 * to the src data we are copying out.
311 *
312 * root is the tree we are copying into, and path is a scratch
313 * path for use in this function (it should be released on entry and
314 * will be released on exit).
315 *
316 * If the key is already in the destination tree the existing item is
317 * overwritten. If the existing item isn't big enough, it is extended.
318 * If it is too large, it is truncated.
319 *
320 * If the key isn't in the destination yet, a new item is inserted.
321 */
322static noinline int overwrite_item(struct btrfs_trans_handle *trans,
323 struct btrfs_root *root,
324 struct btrfs_path *path,
325 struct extent_buffer *eb, int slot,
326 struct btrfs_key *key)
327{
328 int ret;
329 u32 item_size;
330 u64 saved_i_size = 0;
331 int save_old_i_size = 0;
332 unsigned long src_ptr;
333 unsigned long dst_ptr;
334 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000335 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400336
337 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
338 overwrite_root = 1;
339
340 item_size = btrfs_item_size_nr(eb, slot);
341 src_ptr = btrfs_item_ptr_offset(eb, slot);
342
343 /* look for the key in the destination tree */
344 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000345 if (ret < 0)
346 return ret;
347
Chris Masone02119d2008-09-05 16:13:11 -0400348 if (ret == 0) {
349 char *src_copy;
350 char *dst_copy;
351 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
352 path->slots[0]);
353 if (dst_size != item_size)
354 goto insert;
355
356 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200357 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400358 return 0;
359 }
360 dst_copy = kmalloc(item_size, GFP_NOFS);
361 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000362 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200363 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000364 kfree(dst_copy);
365 kfree(src_copy);
366 return -ENOMEM;
367 }
Chris Masone02119d2008-09-05 16:13:11 -0400368
369 read_extent_buffer(eb, src_copy, src_ptr, item_size);
370
371 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
372 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
373 item_size);
374 ret = memcmp(dst_copy, src_copy, item_size);
375
376 kfree(dst_copy);
377 kfree(src_copy);
378 /*
379 * they have the same contents, just return, this saves
380 * us from cowing blocks in the destination tree and doing
381 * extra writes that may not have been done by a previous
382 * sync
383 */
384 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200385 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400386 return 0;
387 }
388
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000389 /*
390 * We need to load the old nbytes into the inode so when we
391 * replay the extents we've logged we get the right nbytes.
392 */
393 if (inode_item) {
394 struct btrfs_inode_item *item;
395 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400396 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000397
398 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
399 struct btrfs_inode_item);
400 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
401 item = btrfs_item_ptr(eb, slot,
402 struct btrfs_inode_item);
403 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400404
405 /*
406 * If this is a directory we need to reset the i_size to
407 * 0 so that we can set it up properly when replaying
408 * the rest of the items in this log.
409 */
410 mode = btrfs_inode_mode(eb, item);
411 if (S_ISDIR(mode))
412 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000413 }
414 } else if (inode_item) {
415 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400416 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000417
418 /*
419 * New inode, set nbytes to 0 so that the nbytes comes out
420 * properly when we replay the extents.
421 */
422 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
423 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400424
425 /*
426 * If this is a directory we need to reset the i_size to 0 so
427 * that we can set it up properly when replaying the rest of
428 * the items in this log.
429 */
430 mode = btrfs_inode_mode(eb, item);
431 if (S_ISDIR(mode))
432 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400433 }
434insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200435 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400436 /* try to insert the key into the destination tree */
437 ret = btrfs_insert_empty_item(trans, root, path,
438 key, item_size);
439
440 /* make sure any existing item is the correct size */
441 if (ret == -EEXIST) {
442 u32 found_size;
443 found_size = btrfs_item_size_nr(path->nodes[0],
444 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100445 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000446 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100447 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000448 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100449 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400450 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400451 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400452 }
453 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
454 path->slots[0]);
455
456 /* don't overwrite an existing inode if the generation number
457 * was logged as zero. This is done when the tree logging code
458 * is just logging an inode to make sure it exists after recovery.
459 *
460 * Also, don't overwrite i_size on directories during replay.
461 * log replay inserts and removes directory items based on the
462 * state of the tree found in the subvolume, and i_size is modified
463 * as it goes
464 */
465 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
466 struct btrfs_inode_item *src_item;
467 struct btrfs_inode_item *dst_item;
468
469 src_item = (struct btrfs_inode_item *)src_ptr;
470 dst_item = (struct btrfs_inode_item *)dst_ptr;
471
472 if (btrfs_inode_generation(eb, src_item) == 0)
473 goto no_copy;
474
475 if (overwrite_root &&
476 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
477 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
478 save_old_i_size = 1;
479 saved_i_size = btrfs_inode_size(path->nodes[0],
480 dst_item);
481 }
482 }
483
484 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
485 src_ptr, item_size);
486
487 if (save_old_i_size) {
488 struct btrfs_inode_item *dst_item;
489 dst_item = (struct btrfs_inode_item *)dst_ptr;
490 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
491 }
492
493 /* make sure the generation is filled in */
494 if (key->type == BTRFS_INODE_ITEM_KEY) {
495 struct btrfs_inode_item *dst_item;
496 dst_item = (struct btrfs_inode_item *)dst_ptr;
497 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
498 btrfs_set_inode_generation(path->nodes[0], dst_item,
499 trans->transid);
500 }
501 }
502no_copy:
503 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200504 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400505 return 0;
506}
507
508/*
509 * simple helper to read an inode off the disk from a given root
510 * This can only be called for subvolume roots and not for the log
511 */
512static noinline struct inode *read_one_inode(struct btrfs_root *root,
513 u64 objectid)
514{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400515 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400516 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400517
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400518 key.objectid = objectid;
519 key.type = BTRFS_INODE_ITEM_KEY;
520 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000521 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400522 if (IS_ERR(inode)) {
523 inode = NULL;
524 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400525 iput(inode);
526 inode = NULL;
527 }
528 return inode;
529}
530
531/* replays a single extent in 'eb' at 'slot' with 'key' into the
532 * subvolume 'root'. path is released on entry and should be released
533 * on exit.
534 *
535 * extents in the log tree have not been allocated out of the extent
536 * tree yet. So, this completes the allocation, taking a reference
537 * as required if the extent already exists or creating a new extent
538 * if it isn't in the extent allocation tree yet.
539 *
540 * The extent is inserted into the file, dropping any existing extents
541 * from the file that overlap the new one.
542 */
543static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
544 struct btrfs_root *root,
545 struct btrfs_path *path,
546 struct extent_buffer *eb, int slot,
547 struct btrfs_key *key)
548{
549 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400550 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400551 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000552 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400553 struct btrfs_file_extent_item *item;
554 struct inode *inode = NULL;
555 unsigned long size;
556 int ret = 0;
557
558 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
559 found_type = btrfs_file_extent_type(eb, item);
560
Yan Zhengd899e052008-10-30 14:25:28 -0400561 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000562 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
563 nbytes = btrfs_file_extent_num_bytes(eb, item);
564 extent_end = start + nbytes;
565
566 /*
567 * We don't add to the inodes nbytes if we are prealloc or a
568 * hole.
569 */
570 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
571 nbytes = 0;
572 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800573 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000574 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000575 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400576 } else {
577 ret = 0;
578 goto out;
579 }
580
581 inode = read_one_inode(root, key->objectid);
582 if (!inode) {
583 ret = -EIO;
584 goto out;
585 }
586
587 /*
588 * first check to see if we already have this extent in the
589 * file. This must be done before the btrfs_drop_extents run
590 * so we don't try to drop this extent.
591 */
Li Zefan33345d012011-04-20 10:31:50 +0800592 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400593 start, 0);
594
Yan Zhengd899e052008-10-30 14:25:28 -0400595 if (ret == 0 &&
596 (found_type == BTRFS_FILE_EXTENT_REG ||
597 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400598 struct btrfs_file_extent_item cmp1;
599 struct btrfs_file_extent_item cmp2;
600 struct btrfs_file_extent_item *existing;
601 struct extent_buffer *leaf;
602
603 leaf = path->nodes[0];
604 existing = btrfs_item_ptr(leaf, path->slots[0],
605 struct btrfs_file_extent_item);
606
607 read_extent_buffer(eb, &cmp1, (unsigned long)item,
608 sizeof(cmp1));
609 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
610 sizeof(cmp2));
611
612 /*
613 * we already have a pointer to this exact extent,
614 * we don't have to do anything
615 */
616 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200617 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400618 goto out;
619 }
620 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200621 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400622
623 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400624 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400625 if (ret)
626 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400627
Yan Zheng07d400a2009-01-06 11:42:00 -0500628 if (found_type == BTRFS_FILE_EXTENT_REG ||
629 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500631 unsigned long dest_offset;
632 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400633
Yan Zheng07d400a2009-01-06 11:42:00 -0500634 ret = btrfs_insert_empty_item(trans, root, path, key,
635 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400636 if (ret)
637 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500638 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
639 path->slots[0]);
640 copy_extent_buffer(path->nodes[0], eb, dest_offset,
641 (unsigned long)item, sizeof(*item));
642
643 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
644 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
645 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400646 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500647
648 if (ins.objectid > 0) {
649 u64 csum_start;
650 u64 csum_end;
651 LIST_HEAD(ordered_sums);
652 /*
653 * is this extent already allocated in the extent
654 * allocation tree? If so, just add a reference
655 */
656 ret = btrfs_lookup_extent(root, ins.objectid,
657 ins.offset);
658 if (ret == 0) {
659 ret = btrfs_inc_extent_ref(trans, root,
660 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200662 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400663 if (ret)
664 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500665 } else {
666 /*
667 * insert the extent pointer in the extent
668 * allocation tree
669 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 ret = btrfs_alloc_logged_file_extent(trans,
671 root, root->root_key.objectid,
672 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400673 if (ret)
674 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500675 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200676 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500677
678 if (btrfs_file_extent_compression(eb, item)) {
679 csum_start = ins.objectid;
680 csum_end = csum_start + ins.offset;
681 } else {
682 csum_start = ins.objectid +
683 btrfs_file_extent_offset(eb, item);
684 csum_end = csum_start +
685 btrfs_file_extent_num_bytes(eb, item);
686 }
687
688 ret = btrfs_lookup_csums_range(root->log_root,
689 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100690 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400691 if (ret)
692 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500693 while (!list_empty(&ordered_sums)) {
694 struct btrfs_ordered_sum *sums;
695 sums = list_entry(ordered_sums.next,
696 struct btrfs_ordered_sum,
697 list);
Josef Bacik36508602013-04-25 16:23:32 -0400698 if (!ret)
699 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500700 root->fs_info->csum_root,
701 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500702 list_del(&sums->list);
703 kfree(sums);
704 }
Josef Bacik36508602013-04-25 16:23:32 -0400705 if (ret)
706 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200708 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500709 }
710 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
711 /* inline extents are easy, we just overwrite them */
712 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400713 if (ret)
714 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500715 }
716
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000717 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600718 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400719out:
720 if (inode)
721 iput(inode);
722 return ret;
723}
724
725/*
726 * when cleaning up conflicts between the directory names in the
727 * subvolume, directory names in the log and directory names in the
728 * inode back references, we may have to unlink inodes from directories.
729 *
730 * This is a helper function to do the unlink of a specific directory
731 * item
732 */
733static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root,
735 struct btrfs_path *path,
736 struct inode *dir,
737 struct btrfs_dir_item *di)
738{
739 struct inode *inode;
740 char *name;
741 int name_len;
742 struct extent_buffer *leaf;
743 struct btrfs_key location;
744 int ret;
745
746 leaf = path->nodes[0];
747
748 btrfs_dir_item_key_to_cpu(leaf, di, &location);
749 name_len = btrfs_dir_name_len(leaf, di);
750 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000751 if (!name)
752 return -ENOMEM;
753
Chris Masone02119d2008-09-05 16:13:11 -0400754 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200755 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400756
757 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000758 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400759 ret = -EIO;
760 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000761 }
Chris Masone02119d2008-09-05 16:13:11 -0400762
Yan Zhengec051c02009-01-05 15:43:42 -0500763 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400764 if (ret)
765 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400766
Chris Masone02119d2008-09-05 16:13:11 -0400767 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400768 if (ret)
769 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100770 else
771 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400772out:
773 kfree(name);
774 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400775 return ret;
776}
777
778/*
779 * helper function to see if a given name and sequence number found
780 * in an inode back reference are already in a directory and correctly
781 * point to this inode
782 */
783static noinline int inode_in_dir(struct btrfs_root *root,
784 struct btrfs_path *path,
785 u64 dirid, u64 objectid, u64 index,
786 const char *name, int name_len)
787{
788 struct btrfs_dir_item *di;
789 struct btrfs_key location;
790 int match = 0;
791
792 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
793 index, name, name_len, 0);
794 if (di && !IS_ERR(di)) {
795 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
796 if (location.objectid != objectid)
797 goto out;
798 } else
799 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200800 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400801
802 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
803 if (di && !IS_ERR(di)) {
804 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
805 if (location.objectid != objectid)
806 goto out;
807 } else
808 goto out;
809 match = 1;
810out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200811 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400812 return match;
813}
814
815/*
816 * helper function to check a log tree for a named back reference in
817 * an inode. This is used to decide if a back reference that is
818 * found in the subvolume conflicts with what we find in the log.
819 *
820 * inode backreferences may have multiple refs in a single item,
821 * during replay we process one reference at a time, and we don't
822 * want to delete valid links to a file from the subvolume if that
823 * link is also in the log.
824 */
825static noinline int backref_in_log(struct btrfs_root *log,
826 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700827 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400828 char *name, int namelen)
829{
830 struct btrfs_path *path;
831 struct btrfs_inode_ref *ref;
832 unsigned long ptr;
833 unsigned long ptr_end;
834 unsigned long name_ptr;
835 int found_name_len;
836 int item_size;
837 int ret;
838 int match = 0;
839
840 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000841 if (!path)
842 return -ENOMEM;
843
Chris Masone02119d2008-09-05 16:13:11 -0400844 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
845 if (ret != 0)
846 goto out;
847
Chris Masone02119d2008-09-05 16:13:11 -0400848 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700849
850 if (key->type == BTRFS_INODE_EXTREF_KEY) {
851 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
852 name, namelen, NULL))
853 match = 1;
854
855 goto out;
856 }
857
858 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400859 ptr_end = ptr + item_size;
860 while (ptr < ptr_end) {
861 ref = (struct btrfs_inode_ref *)ptr;
862 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
863 if (found_name_len == namelen) {
864 name_ptr = (unsigned long)(ref + 1);
865 ret = memcmp_extent_buffer(path->nodes[0], name,
866 name_ptr, namelen);
867 if (ret == 0) {
868 match = 1;
869 goto out;
870 }
871 }
872 ptr = (unsigned long)(ref + 1) + found_name_len;
873 }
874out:
875 btrfs_free_path(path);
876 return match;
877}
878
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700879static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 struct btrfs_path *path,
882 struct btrfs_root *log_root,
883 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700884 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700885 u64 inode_objectid, u64 parent_objectid,
886 u64 ref_index, char *name, int namelen,
887 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700888{
889 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700890 char *victim_name;
891 int victim_name_len;
892 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700893 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700894 struct btrfs_key search_key;
895 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700896
Mark Fashehf1863732012-08-08 11:32:27 -0700897again:
898 /* Search old style refs */
899 search_key.objectid = inode_objectid;
900 search_key.type = BTRFS_INODE_REF_KEY;
901 search_key.offset = parent_objectid;
902 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700903 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700904 struct btrfs_inode_ref *victim_ref;
905 unsigned long ptr;
906 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700907
908 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700909
910 /* are we trying to overwrite a back ref for the root directory
911 * if so, just jump out, we're done
912 */
Mark Fashehf1863732012-08-08 11:32:27 -0700913 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700914 return 1;
915
916 /* check all the names in this back reference to see
917 * if they are in the log. if so, we allow them to stay
918 * otherwise they must be unlinked as a conflict
919 */
920 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
921 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
922 while (ptr < ptr_end) {
923 victim_ref = (struct btrfs_inode_ref *)ptr;
924 victim_name_len = btrfs_inode_ref_name_len(leaf,
925 victim_ref);
926 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400927 if (!victim_name)
928 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700929
930 read_extent_buffer(leaf, victim_name,
931 (unsigned long)(victim_ref + 1),
932 victim_name_len);
933
Mark Fashehf1863732012-08-08 11:32:27 -0700934 if (!backref_in_log(log_root, &search_key,
935 parent_objectid,
936 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700937 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700938 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700939 btrfs_release_path(path);
940
941 ret = btrfs_unlink_inode(trans, root, dir,
942 inode, victim_name,
943 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700944 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400945 if (ret)
946 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100947 ret = btrfs_run_delayed_items(trans, root);
948 if (ret)
949 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700950 *search_done = 1;
951 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700952 }
953 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700954
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700955 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
956 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700957
958 /*
959 * NOTE: we have searched root tree and checked the
960 * coresponding ref, it does not need to check again.
961 */
962 *search_done = 1;
963 }
964 btrfs_release_path(path);
965
Mark Fashehf1863732012-08-08 11:32:27 -0700966 /* Same search but for extended refs */
967 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
968 inode_objectid, parent_objectid, 0,
969 0);
970 if (!IS_ERR_OR_NULL(extref)) {
971 u32 item_size;
972 u32 cur_offset = 0;
973 unsigned long base;
974 struct inode *victim_parent;
975
976 leaf = path->nodes[0];
977
978 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
979 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
980
981 while (cur_offset < item_size) {
982 extref = (struct btrfs_inode_extref *)base + cur_offset;
983
984 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
985
986 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
987 goto next;
988
989 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400990 if (!victim_name)
991 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -0700992 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
993 victim_name_len);
994
995 search_key.objectid = inode_objectid;
996 search_key.type = BTRFS_INODE_EXTREF_KEY;
997 search_key.offset = btrfs_extref_hash(parent_objectid,
998 victim_name,
999 victim_name_len);
1000 ret = 0;
1001 if (!backref_in_log(log_root, &search_key,
1002 parent_objectid, victim_name,
1003 victim_name_len)) {
1004 ret = -ENOENT;
1005 victim_parent = read_one_inode(root,
1006 parent_objectid);
1007 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001008 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001009 btrfs_release_path(path);
1010
1011 ret = btrfs_unlink_inode(trans, root,
1012 victim_parent,
1013 inode,
1014 victim_name,
1015 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001016 if (!ret)
1017 ret = btrfs_run_delayed_items(
1018 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001019 }
Mark Fashehf1863732012-08-08 11:32:27 -07001020 iput(victim_parent);
1021 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001022 if (ret)
1023 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001024 *search_done = 1;
1025 goto again;
1026 }
1027 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001028 if (ret)
1029 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001030next:
1031 cur_offset += victim_name_len + sizeof(*extref);
1032 }
1033 *search_done = 1;
1034 }
1035 btrfs_release_path(path);
1036
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001037 /* look for a conflicting sequence number */
1038 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001039 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001040 if (di && !IS_ERR(di)) {
1041 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001042 if (ret)
1043 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 }
1045 btrfs_release_path(path);
1046
1047 /* look for a conflicing name */
1048 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1049 name, namelen, 0);
1050 if (di && !IS_ERR(di)) {
1051 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001052 if (ret)
1053 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001054 }
1055 btrfs_release_path(path);
1056
1057 return 0;
1058}
Chris Masone02119d2008-09-05 16:13:11 -04001059
Mark Fashehf1863732012-08-08 11:32:27 -07001060static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1061 u32 *namelen, char **name, u64 *index,
1062 u64 *parent_objectid)
1063{
1064 struct btrfs_inode_extref *extref;
1065
1066 extref = (struct btrfs_inode_extref *)ref_ptr;
1067
1068 *namelen = btrfs_inode_extref_name_len(eb, extref);
1069 *name = kmalloc(*namelen, GFP_NOFS);
1070 if (*name == NULL)
1071 return -ENOMEM;
1072
1073 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1074 *namelen);
1075
1076 *index = btrfs_inode_extref_index(eb, extref);
1077 if (parent_objectid)
1078 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1079
1080 return 0;
1081}
1082
1083static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1084 u32 *namelen, char **name, u64 *index)
1085{
1086 struct btrfs_inode_ref *ref;
1087
1088 ref = (struct btrfs_inode_ref *)ref_ptr;
1089
1090 *namelen = btrfs_inode_ref_name_len(eb, ref);
1091 *name = kmalloc(*namelen, GFP_NOFS);
1092 if (*name == NULL)
1093 return -ENOMEM;
1094
1095 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1096
1097 *index = btrfs_inode_ref_index(eb, ref);
1098
1099 return 0;
1100}
1101
Chris Masone02119d2008-09-05 16:13:11 -04001102/*
1103 * replay one inode back reference item found in the log tree.
1104 * eb, slot and key refer to the buffer and key found in the log tree.
1105 * root is the destination we are replaying into, and path is for temp
1106 * use by this function. (it should be released on return).
1107 */
1108static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct btrfs_root *log,
1111 struct btrfs_path *path,
1112 struct extent_buffer *eb, int slot,
1113 struct btrfs_key *key)
1114{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001115 struct inode *dir = NULL;
1116 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001117 unsigned long ref_ptr;
1118 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001119 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001120 int namelen;
1121 int ret;
liuboc622ae62011-03-26 08:01:12 -04001122 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001123 int log_ref_ver = 0;
1124 u64 parent_objectid;
1125 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001126 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001127 int ref_struct_size;
1128
1129 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1130 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1131
1132 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1133 struct btrfs_inode_extref *r;
1134
1135 ref_struct_size = sizeof(struct btrfs_inode_extref);
1136 log_ref_ver = 1;
1137 r = (struct btrfs_inode_extref *)ref_ptr;
1138 parent_objectid = btrfs_inode_extref_parent(eb, r);
1139 } else {
1140 ref_struct_size = sizeof(struct btrfs_inode_ref);
1141 parent_objectid = key->offset;
1142 }
1143 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001144
Chris Masone02119d2008-09-05 16:13:11 -04001145 /*
1146 * it is possible that we didn't log all the parent directories
1147 * for a given inode. If we don't find the dir, just don't
1148 * copy the back ref in. The link count fixup code will take
1149 * care of the rest
1150 */
Mark Fashehf1863732012-08-08 11:32:27 -07001151 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001152 if (!dir) {
1153 ret = -ENOENT;
1154 goto out;
1155 }
Chris Masone02119d2008-09-05 16:13:11 -04001156
Mark Fashehf1863732012-08-08 11:32:27 -07001157 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001158 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001159 ret = -EIO;
1160 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001161 }
Chris Masone02119d2008-09-05 16:13:11 -04001162
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001163 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001164 if (log_ref_ver) {
1165 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1166 &ref_index, &parent_objectid);
1167 /*
1168 * parent object can change from one array
1169 * item to another.
1170 */
1171 if (!dir)
1172 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001173 if (!dir) {
1174 ret = -ENOENT;
1175 goto out;
1176 }
Mark Fashehf1863732012-08-08 11:32:27 -07001177 } else {
1178 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1179 &ref_index);
1180 }
1181 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001182 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001183
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001184 /* if we already have a perfect match, we're done */
1185 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001186 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001187 /*
1188 * look for a conflicting back reference in the
1189 * metadata. if we find one we have to unlink that name
1190 * of the file before we add our new link. Later on, we
1191 * overwrite any existing back reference, and we don't
1192 * want to create dangling pointers in the directory.
1193 */
Chris Masone02119d2008-09-05 16:13:11 -04001194
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001195 if (!search_done) {
1196 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001197 dir, inode, eb,
1198 inode_objectid,
1199 parent_objectid,
1200 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001201 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001202 if (ret) {
1203 if (ret == 1)
1204 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001205 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001206 }
Chris Masone02119d2008-09-05 16:13:11 -04001207 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001208
1209 /* insert our name */
1210 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001211 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001212 if (ret)
1213 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001214
1215 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001216 }
liuboc622ae62011-03-26 08:01:12 -04001217
Mark Fashehf1863732012-08-08 11:32:27 -07001218 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001219 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001220 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001221 if (log_ref_ver) {
1222 iput(dir);
1223 dir = NULL;
1224 }
Chris Masone02119d2008-09-05 16:13:11 -04001225 }
Chris Masone02119d2008-09-05 16:13:11 -04001226
1227 /* finally write the back reference in the inode */
1228 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001229out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001230 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001231 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001232 iput(dir);
1233 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001234 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001235}
1236
Yan, Zhengc71bf092009-11-12 09:34:40 +00001237static int insert_orphan_item(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root, u64 offset)
1239{
1240 int ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08001241 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1242 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001243 if (ret > 0)
1244 ret = btrfs_insert_orphan_item(trans, root, offset);
1245 return ret;
1246}
1247
Mark Fashehf1863732012-08-08 11:32:27 -07001248static int count_inode_extrefs(struct btrfs_root *root,
1249 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001250{
Mark Fashehf1863732012-08-08 11:32:27 -07001251 int ret = 0;
1252 int name_len;
1253 unsigned int nlink = 0;
1254 u32 item_size;
1255 u32 cur_offset = 0;
1256 u64 inode_objectid = btrfs_ino(inode);
1257 u64 offset = 0;
1258 unsigned long ptr;
1259 struct btrfs_inode_extref *extref;
1260 struct extent_buffer *leaf;
1261
1262 while (1) {
1263 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1264 &extref, &offset);
1265 if (ret)
1266 break;
1267
1268 leaf = path->nodes[0];
1269 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1270 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1271
1272 while (cur_offset < item_size) {
1273 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1274 name_len = btrfs_inode_extref_name_len(leaf, extref);
1275
1276 nlink++;
1277
1278 cur_offset += name_len + sizeof(*extref);
1279 }
1280
1281 offset++;
1282 btrfs_release_path(path);
1283 }
1284 btrfs_release_path(path);
1285
1286 if (ret < 0)
1287 return ret;
1288 return nlink;
1289}
1290
1291static int count_inode_refs(struct btrfs_root *root,
1292 struct inode *inode, struct btrfs_path *path)
1293{
Chris Masone02119d2008-09-05 16:13:11 -04001294 int ret;
1295 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001296 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001297 unsigned long ptr;
1298 unsigned long ptr_end;
1299 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001300 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001301
Li Zefan33345d012011-04-20 10:31:50 +08001302 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001303 key.type = BTRFS_INODE_REF_KEY;
1304 key.offset = (u64)-1;
1305
Chris Masond3977122009-01-05 21:25:51 -05001306 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1308 if (ret < 0)
1309 break;
1310 if (ret > 0) {
1311 if (path->slots[0] == 0)
1312 break;
1313 path->slots[0]--;
1314 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001315process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001316 btrfs_item_key_to_cpu(path->nodes[0], &key,
1317 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001318 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001319 key.type != BTRFS_INODE_REF_KEY)
1320 break;
1321 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1322 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1323 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001324 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001325 struct btrfs_inode_ref *ref;
1326
1327 ref = (struct btrfs_inode_ref *)ptr;
1328 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1329 ref);
1330 ptr = (unsigned long)(ref + 1) + name_len;
1331 nlink++;
1332 }
1333
1334 if (key.offset == 0)
1335 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001336 if (path->slots[0] > 0) {
1337 path->slots[0]--;
1338 goto process_slot;
1339 }
Chris Masone02119d2008-09-05 16:13:11 -04001340 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001341 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001342 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001344
1345 return nlink;
1346}
1347
1348/*
1349 * There are a few corners where the link count of the file can't
1350 * be properly maintained during replay. So, instead of adding
1351 * lots of complexity to the log code, we just scan the backrefs
1352 * for any file that has been through replay.
1353 *
1354 * The scan will update the link count on the inode to reflect the
1355 * number of back refs found. If it goes down to zero, the iput
1356 * will free the inode.
1357 */
1358static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root,
1360 struct inode *inode)
1361{
1362 struct btrfs_path *path;
1363 int ret;
1364 u64 nlink = 0;
1365 u64 ino = btrfs_ino(inode);
1366
1367 path = btrfs_alloc_path();
1368 if (!path)
1369 return -ENOMEM;
1370
1371 ret = count_inode_refs(root, inode, path);
1372 if (ret < 0)
1373 goto out;
1374
1375 nlink = ret;
1376
1377 ret = count_inode_extrefs(root, inode, path);
1378 if (ret == -ENOENT)
1379 ret = 0;
1380
1381 if (ret < 0)
1382 goto out;
1383
1384 nlink += ret;
1385
1386 ret = 0;
1387
Chris Masone02119d2008-09-05 16:13:11 -04001388 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001389 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001390 btrfs_update_inode(trans, root, inode);
1391 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001392 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001393
Yan, Zhengc71bf092009-11-12 09:34:40 +00001394 if (inode->i_nlink == 0) {
1395 if (S_ISDIR(inode->i_mode)) {
1396 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001397 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001398 if (ret)
1399 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001400 }
Li Zefan33345d012011-04-20 10:31:50 +08001401 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001402 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001403
Mark Fashehf1863732012-08-08 11:32:27 -07001404out:
1405 btrfs_free_path(path);
1406 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001407}
1408
1409static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1410 struct btrfs_root *root,
1411 struct btrfs_path *path)
1412{
1413 int ret;
1414 struct btrfs_key key;
1415 struct inode *inode;
1416
1417 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1418 key.type = BTRFS_ORPHAN_ITEM_KEY;
1419 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001420 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001421 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1422 if (ret < 0)
1423 break;
1424
1425 if (ret == 1) {
1426 if (path->slots[0] == 0)
1427 break;
1428 path->slots[0]--;
1429 }
1430
1431 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1432 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1433 key.type != BTRFS_ORPHAN_ITEM_KEY)
1434 break;
1435
1436 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001437 if (ret)
1438 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001439
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001441 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001442 if (!inode)
1443 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001444
1445 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001446 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001447 if (ret)
1448 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001449
Chris Mason12fcfd22009-03-24 10:24:20 -04001450 /*
1451 * fixup on a directory may create new entries,
1452 * make sure we always look for the highset possible
1453 * offset
1454 */
1455 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001456 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001457 ret = 0;
1458out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001460 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001461}
1462
1463
1464/*
1465 * record a given inode in the fixup dir so we can check its link
1466 * count when replay is done. The link count is incremented here
1467 * so the inode won't go away until we check it
1468 */
1469static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1470 struct btrfs_root *root,
1471 struct btrfs_path *path,
1472 u64 objectid)
1473{
1474 struct btrfs_key key;
1475 int ret = 0;
1476 struct inode *inode;
1477
1478 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001479 if (!inode)
1480 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001481
1482 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1483 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1484 key.offset = objectid;
1485
1486 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1487
David Sterbab3b4aa72011-04-21 01:20:15 +02001488 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001489 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001490 if (!inode->i_nlink)
1491 set_nlink(inode, 1);
1492 else
Zach Brown8b558c52013-10-16 12:10:34 -07001493 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001494 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001495 } else if (ret == -EEXIST) {
1496 ret = 0;
1497 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001498 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001499 }
1500 iput(inode);
1501
1502 return ret;
1503}
1504
1505/*
1506 * when replaying the log for a directory, we only insert names
1507 * for inodes that actually exist. This means an fsync on a directory
1508 * does not implicitly fsync all the new files in it
1509 */
1510static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1511 struct btrfs_root *root,
1512 struct btrfs_path *path,
1513 u64 dirid, u64 index,
1514 char *name, int name_len, u8 type,
1515 struct btrfs_key *location)
1516{
1517 struct inode *inode;
1518 struct inode *dir;
1519 int ret;
1520
1521 inode = read_one_inode(root, location->objectid);
1522 if (!inode)
1523 return -ENOENT;
1524
1525 dir = read_one_inode(root, dirid);
1526 if (!dir) {
1527 iput(inode);
1528 return -EIO;
1529 }
Josef Bacikd5554382013-09-11 14:17:00 -04001530
Chris Masone02119d2008-09-05 16:13:11 -04001531 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1532
1533 /* FIXME, put inode into FIXUP list */
1534
1535 iput(inode);
1536 iput(dir);
1537 return ret;
1538}
1539
1540/*
1541 * take a single entry in a log directory item and replay it into
1542 * the subvolume.
1543 *
1544 * if a conflicting item exists in the subdirectory already,
1545 * the inode it points to is unlinked and put into the link count
1546 * fix up tree.
1547 *
1548 * If a name from the log points to a file or directory that does
1549 * not exist in the FS, it is skipped. fsyncs on directories
1550 * do not force down inodes inside that directory, just changes to the
1551 * names or unlinks in a directory.
1552 */
1553static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1554 struct btrfs_root *root,
1555 struct btrfs_path *path,
1556 struct extent_buffer *eb,
1557 struct btrfs_dir_item *di,
1558 struct btrfs_key *key)
1559{
1560 char *name;
1561 int name_len;
1562 struct btrfs_dir_item *dst_di;
1563 struct btrfs_key found_key;
1564 struct btrfs_key log_key;
1565 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001566 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001567 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001568 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001569 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001570
1571 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001572 if (!dir)
1573 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001574
1575 name_len = btrfs_dir_name_len(eb, di);
1576 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001577 if (!name) {
1578 ret = -ENOMEM;
1579 goto out;
1580 }
liubo2a29edc2011-01-26 06:22:08 +00001581
Chris Masone02119d2008-09-05 16:13:11 -04001582 log_type = btrfs_dir_type(eb, di);
1583 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1584 name_len);
1585
1586 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001587 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1588 if (exists == 0)
1589 exists = 1;
1590 else
1591 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001592 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001593
Chris Masone02119d2008-09-05 16:13:11 -04001594 if (key->type == BTRFS_DIR_ITEM_KEY) {
1595 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1596 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001597 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001598 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1599 key->objectid,
1600 key->offset, name,
1601 name_len, 1);
1602 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001603 /* Corruption */
1604 ret = -EINVAL;
1605 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001606 }
David Sterbac7040052011-04-19 18:00:01 +02001607 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001608 /* we need a sequence number to insert, so we only
1609 * do inserts for the BTRFS_DIR_INDEX_KEY types
1610 */
1611 if (key->type != BTRFS_DIR_INDEX_KEY)
1612 goto out;
1613 goto insert;
1614 }
1615
1616 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1617 /* the existing item matches the logged item */
1618 if (found_key.objectid == log_key.objectid &&
1619 found_key.type == log_key.type &&
1620 found_key.offset == log_key.offset &&
1621 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1622 goto out;
1623 }
1624
1625 /*
1626 * don't drop the conflicting directory entry if the inode
1627 * for the new entry doesn't exist
1628 */
Chris Mason4bef0842008-09-08 11:18:08 -04001629 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001630 goto out;
1631
Chris Masone02119d2008-09-05 16:13:11 -04001632 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001633 if (ret)
1634 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001635
1636 if (key->type == BTRFS_DIR_INDEX_KEY)
1637 goto insert;
1638out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001639 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001640 if (!ret && update_size) {
1641 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1642 ret = btrfs_update_inode(trans, root, dir);
1643 }
Chris Masone02119d2008-09-05 16:13:11 -04001644 kfree(name);
1645 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001646 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001647
1648insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001650 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1651 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001652 if (ret && ret != -ENOENT)
1653 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001654 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001655 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001656 goto out;
1657}
1658
1659/*
1660 * find all the names in a directory item and reconcile them into
1661 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1662 * one name in a directory item, but the same code gets used for
1663 * both directory index types
1664 */
1665static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1666 struct btrfs_root *root,
1667 struct btrfs_path *path,
1668 struct extent_buffer *eb, int slot,
1669 struct btrfs_key *key)
1670{
1671 int ret;
1672 u32 item_size = btrfs_item_size_nr(eb, slot);
1673 struct btrfs_dir_item *di;
1674 int name_len;
1675 unsigned long ptr;
1676 unsigned long ptr_end;
1677
1678 ptr = btrfs_item_ptr_offset(eb, slot);
1679 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001680 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001681 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001682 if (verify_dir_item(root, eb, di))
1683 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001684 name_len = btrfs_dir_name_len(eb, di);
1685 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001686 if (ret)
1687 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001688 ptr = (unsigned long)(di + 1);
1689 ptr += name_len;
1690 }
1691 return 0;
1692}
1693
1694/*
1695 * directory replay has two parts. There are the standard directory
1696 * items in the log copied from the subvolume, and range items
1697 * created in the log while the subvolume was logged.
1698 *
1699 * The range items tell us which parts of the key space the log
1700 * is authoritative for. During replay, if a key in the subvolume
1701 * directory is in a logged range item, but not actually in the log
1702 * that means it was deleted from the directory before the fsync
1703 * and should be removed.
1704 */
1705static noinline int find_dir_range(struct btrfs_root *root,
1706 struct btrfs_path *path,
1707 u64 dirid, int key_type,
1708 u64 *start_ret, u64 *end_ret)
1709{
1710 struct btrfs_key key;
1711 u64 found_end;
1712 struct btrfs_dir_log_item *item;
1713 int ret;
1714 int nritems;
1715
1716 if (*start_ret == (u64)-1)
1717 return 1;
1718
1719 key.objectid = dirid;
1720 key.type = key_type;
1721 key.offset = *start_ret;
1722
1723 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1724 if (ret < 0)
1725 goto out;
1726 if (ret > 0) {
1727 if (path->slots[0] == 0)
1728 goto out;
1729 path->slots[0]--;
1730 }
1731 if (ret != 0)
1732 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1733
1734 if (key.type != key_type || key.objectid != dirid) {
1735 ret = 1;
1736 goto next;
1737 }
1738 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1739 struct btrfs_dir_log_item);
1740 found_end = btrfs_dir_log_end(path->nodes[0], item);
1741
1742 if (*start_ret >= key.offset && *start_ret <= found_end) {
1743 ret = 0;
1744 *start_ret = key.offset;
1745 *end_ret = found_end;
1746 goto out;
1747 }
1748 ret = 1;
1749next:
1750 /* check the next slot in the tree to see if it is a valid item */
1751 nritems = btrfs_header_nritems(path->nodes[0]);
1752 if (path->slots[0] >= nritems) {
1753 ret = btrfs_next_leaf(root, path);
1754 if (ret)
1755 goto out;
1756 } else {
1757 path->slots[0]++;
1758 }
1759
1760 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1761
1762 if (key.type != key_type || key.objectid != dirid) {
1763 ret = 1;
1764 goto out;
1765 }
1766 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1767 struct btrfs_dir_log_item);
1768 found_end = btrfs_dir_log_end(path->nodes[0], item);
1769 *start_ret = key.offset;
1770 *end_ret = found_end;
1771 ret = 0;
1772out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001773 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001774 return ret;
1775}
1776
1777/*
1778 * this looks for a given directory item in the log. If the directory
1779 * item is not in the log, the item is removed and the inode it points
1780 * to is unlinked
1781 */
1782static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1783 struct btrfs_root *root,
1784 struct btrfs_root *log,
1785 struct btrfs_path *path,
1786 struct btrfs_path *log_path,
1787 struct inode *dir,
1788 struct btrfs_key *dir_key)
1789{
1790 int ret;
1791 struct extent_buffer *eb;
1792 int slot;
1793 u32 item_size;
1794 struct btrfs_dir_item *di;
1795 struct btrfs_dir_item *log_di;
1796 int name_len;
1797 unsigned long ptr;
1798 unsigned long ptr_end;
1799 char *name;
1800 struct inode *inode;
1801 struct btrfs_key location;
1802
1803again:
1804 eb = path->nodes[0];
1805 slot = path->slots[0];
1806 item_size = btrfs_item_size_nr(eb, slot);
1807 ptr = btrfs_item_ptr_offset(eb, slot);
1808 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001809 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001810 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001811 if (verify_dir_item(root, eb, di)) {
1812 ret = -EIO;
1813 goto out;
1814 }
1815
Chris Masone02119d2008-09-05 16:13:11 -04001816 name_len = btrfs_dir_name_len(eb, di);
1817 name = kmalloc(name_len, GFP_NOFS);
1818 if (!name) {
1819 ret = -ENOMEM;
1820 goto out;
1821 }
1822 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1823 name_len);
1824 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001825 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001826 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1827 dir_key->objectid,
1828 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001829 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001830 log_di = btrfs_lookup_dir_index_item(trans, log,
1831 log_path,
1832 dir_key->objectid,
1833 dir_key->offset,
1834 name, name_len, 0);
1835 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001836 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001837 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001838 btrfs_release_path(path);
1839 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001840 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001841 if (!inode) {
1842 kfree(name);
1843 return -EIO;
1844 }
Chris Masone02119d2008-09-05 16:13:11 -04001845
1846 ret = link_to_fixup_dir(trans, root,
1847 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001848 if (ret) {
1849 kfree(name);
1850 iput(inode);
1851 goto out;
1852 }
1853
Zach Brown8b558c52013-10-16 12:10:34 -07001854 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001855 ret = btrfs_unlink_inode(trans, root, dir, inode,
1856 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001857 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001858 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001859 kfree(name);
1860 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001861 if (ret)
1862 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001863
1864 /* there might still be more names under this key
1865 * check and repeat if required
1866 */
1867 ret = btrfs_search_slot(NULL, root, dir_key, path,
1868 0, 0);
1869 if (ret == 0)
1870 goto again;
1871 ret = 0;
1872 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001873 } else if (IS_ERR(log_di)) {
1874 kfree(name);
1875 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001876 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001877 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001878 kfree(name);
1879
1880 ptr = (unsigned long)(di + 1);
1881 ptr += name_len;
1882 }
1883 ret = 0;
1884out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001885 btrfs_release_path(path);
1886 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001887 return ret;
1888}
1889
1890/*
1891 * deletion replay happens before we copy any new directory items
1892 * out of the log or out of backreferences from inodes. It
1893 * scans the log to find ranges of keys that log is authoritative for,
1894 * and then scans the directory to find items in those ranges that are
1895 * not present in the log.
1896 *
1897 * Anything we don't find in the log is unlinked and removed from the
1898 * directory.
1899 */
1900static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1901 struct btrfs_root *root,
1902 struct btrfs_root *log,
1903 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001904 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001905{
1906 u64 range_start;
1907 u64 range_end;
1908 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1909 int ret = 0;
1910 struct btrfs_key dir_key;
1911 struct btrfs_key found_key;
1912 struct btrfs_path *log_path;
1913 struct inode *dir;
1914
1915 dir_key.objectid = dirid;
1916 dir_key.type = BTRFS_DIR_ITEM_KEY;
1917 log_path = btrfs_alloc_path();
1918 if (!log_path)
1919 return -ENOMEM;
1920
1921 dir = read_one_inode(root, dirid);
1922 /* it isn't an error if the inode isn't there, that can happen
1923 * because we replay the deletes before we copy in the inode item
1924 * from the log
1925 */
1926 if (!dir) {
1927 btrfs_free_path(log_path);
1928 return 0;
1929 }
1930again:
1931 range_start = 0;
1932 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001933 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001934 if (del_all)
1935 range_end = (u64)-1;
1936 else {
1937 ret = find_dir_range(log, path, dirid, key_type,
1938 &range_start, &range_end);
1939 if (ret != 0)
1940 break;
1941 }
Chris Masone02119d2008-09-05 16:13:11 -04001942
1943 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001944 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001945 int nritems;
1946 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1947 0, 0);
1948 if (ret < 0)
1949 goto out;
1950
1951 nritems = btrfs_header_nritems(path->nodes[0]);
1952 if (path->slots[0] >= nritems) {
1953 ret = btrfs_next_leaf(root, path);
1954 if (ret)
1955 break;
1956 }
1957 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1958 path->slots[0]);
1959 if (found_key.objectid != dirid ||
1960 found_key.type != dir_key.type)
1961 goto next_type;
1962
1963 if (found_key.offset > range_end)
1964 break;
1965
1966 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001967 log_path, dir,
1968 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001969 if (ret)
1970 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001971 if (found_key.offset == (u64)-1)
1972 break;
1973 dir_key.offset = found_key.offset + 1;
1974 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001975 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001976 if (range_end == (u64)-1)
1977 break;
1978 range_start = range_end + 1;
1979 }
1980
1981next_type:
1982 ret = 0;
1983 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1984 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1985 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001986 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001987 goto again;
1988 }
1989out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001990 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001991 btrfs_free_path(log_path);
1992 iput(dir);
1993 return ret;
1994}
1995
1996/*
1997 * the process_func used to replay items from the log tree. This
1998 * gets called in two different stages. The first stage just looks
1999 * for inodes and makes sure they are all copied into the subvolume.
2000 *
2001 * The second stage copies all the other item types from the log into
2002 * the subvolume. The two stage approach is slower, but gets rid of
2003 * lots of complexity around inodes referencing other inodes that exist
2004 * only in the log (references come from either directory items or inode
2005 * back refs).
2006 */
2007static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2008 struct walk_control *wc, u64 gen)
2009{
2010 int nritems;
2011 struct btrfs_path *path;
2012 struct btrfs_root *root = wc->replay_dest;
2013 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002014 int level;
2015 int i;
2016 int ret;
2017
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002018 ret = btrfs_read_buffer(eb, gen);
2019 if (ret)
2020 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002021
2022 level = btrfs_header_level(eb);
2023
2024 if (level != 0)
2025 return 0;
2026
2027 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002028 if (!path)
2029 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002030
2031 nritems = btrfs_header_nritems(eb);
2032 for (i = 0; i < nritems; i++) {
2033 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002034
2035 /* inode keys are done during the first stage */
2036 if (key.type == BTRFS_INODE_ITEM_KEY &&
2037 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002038 struct btrfs_inode_item *inode_item;
2039 u32 mode;
2040
2041 inode_item = btrfs_item_ptr(eb, i,
2042 struct btrfs_inode_item);
2043 mode = btrfs_inode_mode(eb, inode_item);
2044 if (S_ISDIR(mode)) {
2045 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002046 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002047 if (ret)
2048 break;
Chris Masone02119d2008-09-05 16:13:11 -04002049 }
2050 ret = overwrite_item(wc->trans, root, path,
2051 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002052 if (ret)
2053 break;
Chris Masone02119d2008-09-05 16:13:11 -04002054
Yan, Zhengc71bf092009-11-12 09:34:40 +00002055 /* for regular files, make sure corresponding
2056 * orhpan item exist. extents past the new EOF
2057 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002058 */
2059 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002060 ret = insert_orphan_item(wc->trans, root,
2061 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002062 if (ret)
2063 break;
Chris Masone02119d2008-09-05 16:13:11 -04002064 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002065
Chris Masone02119d2008-09-05 16:13:11 -04002066 ret = link_to_fixup_dir(wc->trans, root,
2067 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002068 if (ret)
2069 break;
Chris Masone02119d2008-09-05 16:13:11 -04002070 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002071
2072 if (key.type == BTRFS_DIR_INDEX_KEY &&
2073 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2074 ret = replay_one_dir_item(wc->trans, root, path,
2075 eb, i, &key);
2076 if (ret)
2077 break;
2078 }
2079
Chris Masone02119d2008-09-05 16:13:11 -04002080 if (wc->stage < LOG_WALK_REPLAY_ALL)
2081 continue;
2082
2083 /* these keys are simply copied */
2084 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2085 ret = overwrite_item(wc->trans, root, path,
2086 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002087 if (ret)
2088 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002089 } else if (key.type == BTRFS_INODE_REF_KEY ||
2090 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002091 ret = add_inode_ref(wc->trans, root, log, path,
2092 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002093 if (ret && ret != -ENOENT)
2094 break;
2095 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002096 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2097 ret = replay_one_extent(wc->trans, root, path,
2098 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002099 if (ret)
2100 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002101 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002102 ret = replay_one_dir_item(wc->trans, root, path,
2103 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002104 if (ret)
2105 break;
Chris Masone02119d2008-09-05 16:13:11 -04002106 }
2107 }
2108 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002109 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002110}
2111
Chris Masond3977122009-01-05 21:25:51 -05002112static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002113 struct btrfs_root *root,
2114 struct btrfs_path *path, int *level,
2115 struct walk_control *wc)
2116{
2117 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002118 u64 bytenr;
2119 u64 ptr_gen;
2120 struct extent_buffer *next;
2121 struct extent_buffer *cur;
2122 struct extent_buffer *parent;
2123 u32 blocksize;
2124 int ret = 0;
2125
2126 WARN_ON(*level < 0);
2127 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2128
Chris Masond3977122009-01-05 21:25:51 -05002129 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002130 WARN_ON(*level < 0);
2131 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2132 cur = path->nodes[*level];
2133
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302134 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002135
2136 if (path->slots[*level] >=
2137 btrfs_header_nritems(cur))
2138 break;
2139
2140 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2141 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2142 blocksize = btrfs_level_size(root, *level - 1);
2143
2144 parent = path->nodes[*level];
2145 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002146
2147 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002148 if (!next)
2149 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002150
Chris Masone02119d2008-09-05 16:13:11 -04002151 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002152 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002153 if (ret) {
2154 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002155 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002156 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002157
Chris Masone02119d2008-09-05 16:13:11 -04002158 path->slots[*level]++;
2159 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002160 ret = btrfs_read_buffer(next, ptr_gen);
2161 if (ret) {
2162 free_extent_buffer(next);
2163 return ret;
2164 }
Chris Masone02119d2008-09-05 16:13:11 -04002165
Josef Bacik681ae502013-10-07 15:11:00 -04002166 if (trans) {
2167 btrfs_tree_lock(next);
2168 btrfs_set_lock_blocking(next);
2169 clean_tree_block(trans, root, next);
2170 btrfs_wait_tree_block_writeback(next);
2171 btrfs_tree_unlock(next);
2172 }
Chris Masone02119d2008-09-05 16:13:11 -04002173
Chris Masone02119d2008-09-05 16:13:11 -04002174 WARN_ON(root_owner !=
2175 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002176 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002177 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002178 if (ret) {
2179 free_extent_buffer(next);
2180 return ret;
2181 }
Chris Masone02119d2008-09-05 16:13:11 -04002182 }
2183 free_extent_buffer(next);
2184 continue;
2185 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002186 ret = btrfs_read_buffer(next, ptr_gen);
2187 if (ret) {
2188 free_extent_buffer(next);
2189 return ret;
2190 }
Chris Masone02119d2008-09-05 16:13:11 -04002191
2192 WARN_ON(*level <= 0);
2193 if (path->nodes[*level-1])
2194 free_extent_buffer(path->nodes[*level-1]);
2195 path->nodes[*level-1] = next;
2196 *level = btrfs_header_level(next);
2197 path->slots[*level] = 0;
2198 cond_resched();
2199 }
2200 WARN_ON(*level < 0);
2201 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2202
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002203 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002204
2205 cond_resched();
2206 return 0;
2207}
2208
Chris Masond3977122009-01-05 21:25:51 -05002209static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002210 struct btrfs_root *root,
2211 struct btrfs_path *path, int *level,
2212 struct walk_control *wc)
2213{
2214 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002215 int i;
2216 int slot;
2217 int ret;
2218
Chris Masond3977122009-01-05 21:25:51 -05002219 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002220 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002221 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002222 path->slots[i]++;
2223 *level = i;
2224 WARN_ON(*level == 0);
2225 return 0;
2226 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002227 struct extent_buffer *parent;
2228 if (path->nodes[*level] == root->node)
2229 parent = path->nodes[*level];
2230 else
2231 parent = path->nodes[*level + 1];
2232
2233 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002234 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002235 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002236 if (ret)
2237 return ret;
2238
Chris Masone02119d2008-09-05 16:13:11 -04002239 if (wc->free) {
2240 struct extent_buffer *next;
2241
2242 next = path->nodes[*level];
2243
Josef Bacik681ae502013-10-07 15:11:00 -04002244 if (trans) {
2245 btrfs_tree_lock(next);
2246 btrfs_set_lock_blocking(next);
2247 clean_tree_block(trans, root, next);
2248 btrfs_wait_tree_block_writeback(next);
2249 btrfs_tree_unlock(next);
2250 }
Chris Masone02119d2008-09-05 16:13:11 -04002251
Chris Masone02119d2008-09-05 16:13:11 -04002252 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002253 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002254 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002255 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002256 if (ret)
2257 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002258 }
2259 free_extent_buffer(path->nodes[*level]);
2260 path->nodes[*level] = NULL;
2261 *level = i + 1;
2262 }
2263 }
2264 return 1;
2265}
2266
2267/*
2268 * drop the reference count on the tree rooted at 'snap'. This traverses
2269 * the tree freeing any blocks that have a ref count of zero after being
2270 * decremented.
2271 */
2272static int walk_log_tree(struct btrfs_trans_handle *trans,
2273 struct btrfs_root *log, struct walk_control *wc)
2274{
2275 int ret = 0;
2276 int wret;
2277 int level;
2278 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002279 int orig_level;
2280
2281 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002282 if (!path)
2283 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002284
2285 level = btrfs_header_level(log->node);
2286 orig_level = level;
2287 path->nodes[level] = log->node;
2288 extent_buffer_get(log->node);
2289 path->slots[level] = 0;
2290
Chris Masond3977122009-01-05 21:25:51 -05002291 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002292 wret = walk_down_log_tree(trans, log, path, &level, wc);
2293 if (wret > 0)
2294 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002296 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 goto out;
2298 }
Chris Masone02119d2008-09-05 16:13:11 -04002299
2300 wret = walk_up_log_tree(trans, log, path, &level, wc);
2301 if (wret > 0)
2302 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002303 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002304 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002305 goto out;
2306 }
Chris Masone02119d2008-09-05 16:13:11 -04002307 }
2308
2309 /* was the root node processed? if not, catch it here */
2310 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002311 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002312 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002313 if (ret)
2314 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002315 if (wc->free) {
2316 struct extent_buffer *next;
2317
2318 next = path->nodes[orig_level];
2319
Josef Bacik681ae502013-10-07 15:11:00 -04002320 if (trans) {
2321 btrfs_tree_lock(next);
2322 btrfs_set_lock_blocking(next);
2323 clean_tree_block(trans, log, next);
2324 btrfs_wait_tree_block_writeback(next);
2325 btrfs_tree_unlock(next);
2326 }
Chris Masone02119d2008-09-05 16:13:11 -04002327
Chris Masone02119d2008-09-05 16:13:11 -04002328 WARN_ON(log->root_key.objectid !=
2329 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002330 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002331 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002332 if (ret)
2333 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002334 }
2335 }
2336
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002337out:
Chris Masone02119d2008-09-05 16:13:11 -04002338 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002339 return ret;
2340}
2341
Yan Zheng7237f1832009-01-21 12:54:03 -05002342/*
2343 * helper function to update the item for a given subvolumes log root
2344 * in the tree of log roots
2345 */
2346static int update_log_root(struct btrfs_trans_handle *trans,
2347 struct btrfs_root *log)
2348{
2349 int ret;
2350
2351 if (log->log_transid == 1) {
2352 /* insert root item on the first sync */
2353 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2354 &log->root_key, &log->root_item);
2355 } else {
2356 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2357 &log->root_key, &log->root_item);
2358 }
2359 return ret;
2360}
2361
Chris Mason12fcfd22009-03-24 10:24:20 -04002362static int wait_log_commit(struct btrfs_trans_handle *trans,
2363 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002364{
2365 DEFINE_WAIT(wait);
Yan Zheng7237f1832009-01-21 12:54:03 -05002366 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002367
Yan Zheng7237f1832009-01-21 12:54:03 -05002368 /*
2369 * we only allow two pending log transactions at a time,
2370 * so we know that if ours is more than 2 older than the
2371 * current transaction, we're done
2372 */
Chris Masone02119d2008-09-05 16:13:11 -04002373 do {
Yan Zheng7237f1832009-01-21 12:54:03 -05002374 prepare_to_wait(&root->log_commit_wait[index],
2375 &wait, TASK_UNINTERRUPTIBLE);
2376 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002377
Miao Xie5c902ba2014-02-20 18:08:51 +08002378 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Chris Mason12fcfd22009-03-24 10:24:20 -04002379 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002380 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002381 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002382
Yan Zheng7237f1832009-01-21 12:54:03 -05002383 finish_wait(&root->log_commit_wait[index], &wait);
2384 mutex_lock(&root->log_mutex);
Miao Xie5c902ba2014-02-20 18:08:51 +08002385 } while (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Jan Kara6dd70ce2012-01-26 15:01:11 -05002386 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002387 atomic_read(&root->log_commit[index]));
2388 return 0;
2389}
2390
Jeff Mahoney143bede2012-03-01 14:56:26 +01002391static void wait_for_writer(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root)
Yan Zheng7237f1832009-01-21 12:54:03 -05002393{
2394 DEFINE_WAIT(wait);
Miao Xie5c902ba2014-02-20 18:08:51 +08002395 while (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Jan Kara6dd70ce2012-01-26 15:01:11 -05002396 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f1832009-01-21 12:54:03 -05002397 prepare_to_wait(&root->log_writer_wait,
2398 &wait, TASK_UNINTERRUPTIBLE);
2399 mutex_unlock(&root->log_mutex);
Miao Xie5c902ba2014-02-20 18:08:51 +08002400 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) !=
Chris Mason12fcfd22009-03-24 10:24:20 -04002401 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f1832009-01-21 12:54:03 -05002402 schedule();
2403 mutex_lock(&root->log_mutex);
2404 finish_wait(&root->log_writer_wait, &wait);
2405 }
Chris Masone02119d2008-09-05 16:13:11 -04002406}
2407
2408/*
2409 * btrfs_sync_log does sends a given tree log down to the disk and
2410 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002411 * you know that any inodes previously logged are safely on disk only
2412 * if it returns 0.
2413 *
2414 * Any other return value means you need to call btrfs_commit_transaction.
2415 * Some of the edge cases for fsyncing directories that have had unlinks
2416 * or renames done in the past mean that sometimes the only safe
2417 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2418 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002419 */
2420int btrfs_sync_log(struct btrfs_trans_handle *trans,
2421 struct btrfs_root *root)
2422{
Yan Zheng7237f1832009-01-21 12:54:03 -05002423 int index1;
2424 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002425 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002426 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002427 struct btrfs_root *log = root->log_root;
Yan Zheng7237f1832009-01-21 12:54:03 -05002428 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002429 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002430 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002431
Yan Zheng7237f1832009-01-21 12:54:03 -05002432 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002433 log_transid = root->log_transid;
Yan Zheng7237f1832009-01-21 12:54:03 -05002434 index1 = root->log_transid % 2;
2435 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002436 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002437 mutex_unlock(&root->log_mutex);
2438 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002439 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002440 atomic_set(&root->log_commit[index1], 1);
2441
2442 /* wait for previous tree log sync to complete */
2443 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002444 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002445 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002446 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002447 /* when we're on an ssd, just kick the log commit out */
2448 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002449 mutex_unlock(&root->log_mutex);
2450 schedule_timeout_uninterruptible(1);
2451 mutex_lock(&root->log_mutex);
2452 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002453 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002454 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002455 break;
2456 }
Chris Masond0c803c2008-09-11 16:17:57 -04002457
Chris Mason12fcfd22009-03-24 10:24:20 -04002458 /* bail out if we need to do a full commit */
Miao Xie5c902ba2014-02-20 18:08:51 +08002459 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2460 trans->transid) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002461 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002462 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002463 mutex_unlock(&root->log_mutex);
2464 goto out;
2465 }
2466
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002467 if (log_transid % 2 == 0)
2468 mark = EXTENT_DIRTY;
2469 else
2470 mark = EXTENT_NEW;
2471
Chris Mason690587d2009-10-13 13:29:19 -04002472 /* we start IO on all the marked extents here, but we don't actually
2473 * wait for them until later.
2474 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002475 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002476 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002478 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002479 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002480 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002481 mutex_unlock(&root->log_mutex);
2482 goto out;
2483 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002484
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002485 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f1832009-01-21 12:54:03 -05002486
Yan Zheng7237f1832009-01-21 12:54:03 -05002487 root->log_transid++;
2488 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002489 root->log_start_pid = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -05002490 smp_mb();
2491 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002492 * IO has been started, blocks of the log tree have WRITTEN flag set
2493 * in their headers. new modifications of the log will be written to
2494 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f1832009-01-21 12:54:03 -05002495 */
2496 mutex_unlock(&root->log_mutex);
2497
2498 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002499 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -05002500 atomic_inc(&log_root_tree->log_writers);
2501 mutex_unlock(&log_root_tree->log_mutex);
2502
2503 ret = update_log_root(trans, log);
Yan Zheng7237f1832009-01-21 12:54:03 -05002504
2505 mutex_lock(&log_root_tree->log_mutex);
2506 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2507 smp_mb();
2508 if (waitqueue_active(&log_root_tree->log_writer_wait))
2509 wake_up(&log_root_tree->log_writer_wait);
2510 }
2511
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002512 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002513 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002514 if (ret != -ENOSPC) {
2515 btrfs_abort_transaction(trans, root, ret);
2516 mutex_unlock(&log_root_tree->log_mutex);
2517 goto out;
2518 }
Miao Xie5c902ba2014-02-20 18:08:51 +08002519 ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) =
2520 trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002521 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002522 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002523 mutex_unlock(&log_root_tree->log_mutex);
2524 ret = -EAGAIN;
2525 goto out;
2526 }
2527
Yan Zheng7237f1832009-01-21 12:54:03 -05002528 index2 = log_root_tree->log_transid % 2;
2529 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002530 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002531 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002532 wait_log_commit(trans, log_root_tree,
2533 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002534 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002535 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002536 ret = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -05002537 goto out;
2538 }
2539 atomic_set(&log_root_tree->log_commit[index2], 1);
2540
Chris Mason12fcfd22009-03-24 10:24:20 -04002541 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2542 wait_log_commit(trans, log_root_tree,
2543 log_root_tree->log_transid - 1);
2544 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002545
Chris Mason12fcfd22009-03-24 10:24:20 -04002546 wait_for_writer(trans, log_root_tree);
2547
2548 /*
2549 * now that we've moved on to the tree of log tree roots,
2550 * check the full commit flag again
2551 */
Miao Xie5c902ba2014-02-20 18:08:51 +08002552 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2553 trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002554 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002555 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002556 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002557 mutex_unlock(&log_root_tree->log_mutex);
2558 ret = -EAGAIN;
2559 goto out_wake_log_root;
2560 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002561
Miao Xiec6adc9c2013-05-28 10:05:39 +00002562 ret = btrfs_write_marked_extents(log_root_tree,
2563 &log_root_tree->dirty_log_pages,
2564 EXTENT_DIRTY | EXTENT_NEW);
2565 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002566 if (ret) {
2567 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002568 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002569 mutex_unlock(&log_root_tree->log_mutex);
2570 goto out_wake_log_root;
2571 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002572 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002573 btrfs_wait_marked_extents(log_root_tree,
2574 &log_root_tree->dirty_log_pages,
2575 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002576 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002577
David Sterba6c417612011-04-13 15:41:04 +02002578 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002579 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002580 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002581 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002582
Yan Zheng7237f1832009-01-21 12:54:03 -05002583 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002584 smp_mb();
Yan Zheng7237f1832009-01-21 12:54:03 -05002585
2586 mutex_unlock(&log_root_tree->log_mutex);
2587
2588 /*
2589 * nobody else is going to jump in and write the the ctree
2590 * super here because the log_commit atomic below is protecting
2591 * us. We must be called with a transaction handle pinning
2592 * the running transaction open, so a full commit can't hop
2593 * in and cause problems either.
2594 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002595 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002596 if (ret) {
2597 btrfs_abort_transaction(trans, root, ret);
2598 goto out_wake_log_root;
2599 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002600
Chris Mason257c62e2009-10-13 13:21:08 -04002601 mutex_lock(&root->log_mutex);
2602 if (root->last_log_commit < log_transid)
2603 root->last_log_commit = log_transid;
2604 mutex_unlock(&root->log_mutex);
2605
Chris Mason12fcfd22009-03-24 10:24:20 -04002606out_wake_log_root:
Yan Zheng7237f1832009-01-21 12:54:03 -05002607 atomic_set(&log_root_tree->log_commit[index2], 0);
2608 smp_mb();
2609 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2610 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002611out:
Yan Zheng7237f1832009-01-21 12:54:03 -05002612 atomic_set(&root->log_commit[index1], 0);
2613 smp_mb();
2614 if (waitqueue_active(&root->log_commit_wait[index1]))
2615 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002616 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002617}
2618
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002619static void free_log_tree(struct btrfs_trans_handle *trans,
2620 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002621{
2622 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002623 u64 start;
2624 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002625 struct walk_control wc = {
2626 .free = 1,
2627 .process_func = process_one_buffer
2628 };
2629
Josef Bacik681ae502013-10-07 15:11:00 -04002630 ret = walk_log_tree(trans, log, &wc);
2631 /* I don't think this can happen but just in case */
2632 if (ret)
2633 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002634
Chris Masond3977122009-01-05 21:25:51 -05002635 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002636 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002637 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2638 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002639 if (ret)
2640 break;
2641
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002642 clear_extent_bits(&log->dirty_log_pages, start, end,
2643 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002644 }
2645
Josef Bacik2ab28f32012-10-12 15:27:49 -04002646 /*
2647 * We may have short-circuited the log tree with the full commit logic
2648 * and left ordered extents on our list, so clear these out to keep us
2649 * from leaking inodes and memory.
2650 */
2651 btrfs_free_logged_extents(log, 0);
2652 btrfs_free_logged_extents(log, 1);
2653
Yan Zheng7237f1832009-01-21 12:54:03 -05002654 free_extent_buffer(log->node);
2655 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002656}
2657
2658/*
2659 * free all the extents used by the tree log. This should be called
2660 * at commit time of the full transaction
2661 */
2662int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2663{
2664 if (root->log_root) {
2665 free_log_tree(trans, root->log_root);
2666 root->log_root = NULL;
2667 }
2668 return 0;
2669}
2670
2671int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2672 struct btrfs_fs_info *fs_info)
2673{
2674 if (fs_info->log_root_tree) {
2675 free_log_tree(trans, fs_info->log_root_tree);
2676 fs_info->log_root_tree = NULL;
2677 }
Chris Masone02119d2008-09-05 16:13:11 -04002678 return 0;
2679}
2680
2681/*
Chris Masone02119d2008-09-05 16:13:11 -04002682 * If both a file and directory are logged, and unlinks or renames are
2683 * mixed in, we have a few interesting corners:
2684 *
2685 * create file X in dir Y
2686 * link file X to X.link in dir Y
2687 * fsync file X
2688 * unlink file X but leave X.link
2689 * fsync dir Y
2690 *
2691 * After a crash we would expect only X.link to exist. But file X
2692 * didn't get fsync'd again so the log has back refs for X and X.link.
2693 *
2694 * We solve this by removing directory entries and inode backrefs from the
2695 * log when a file that was logged in the current transaction is
2696 * unlinked. Any later fsync will include the updated log entries, and
2697 * we'll be able to reconstruct the proper directory items from backrefs.
2698 *
2699 * This optimizations allows us to avoid relogging the entire inode
2700 * or the entire directory.
2701 */
2702int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2703 struct btrfs_root *root,
2704 const char *name, int name_len,
2705 struct inode *dir, u64 index)
2706{
2707 struct btrfs_root *log;
2708 struct btrfs_dir_item *di;
2709 struct btrfs_path *path;
2710 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002711 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002712 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002713 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002714
Chris Mason3a5f1d42008-09-11 15:53:37 -04002715 if (BTRFS_I(dir)->logged_trans < trans->transid)
2716 return 0;
2717
Chris Masone02119d2008-09-05 16:13:11 -04002718 ret = join_running_log_trans(root);
2719 if (ret)
2720 return 0;
2721
2722 mutex_lock(&BTRFS_I(dir)->log_mutex);
2723
2724 log = root->log_root;
2725 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002726 if (!path) {
2727 err = -ENOMEM;
2728 goto out_unlock;
2729 }
liubo2a29edc2011-01-26 06:22:08 +00002730
Li Zefan33345d012011-04-20 10:31:50 +08002731 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002732 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002733 if (IS_ERR(di)) {
2734 err = PTR_ERR(di);
2735 goto fail;
2736 }
2737 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002738 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2739 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002740 if (ret) {
2741 err = ret;
2742 goto fail;
2743 }
Chris Masone02119d2008-09-05 16:13:11 -04002744 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002745 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002746 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002747 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002748 if (IS_ERR(di)) {
2749 err = PTR_ERR(di);
2750 goto fail;
2751 }
2752 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002753 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2754 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002755 if (ret) {
2756 err = ret;
2757 goto fail;
2758 }
Chris Masone02119d2008-09-05 16:13:11 -04002759 }
2760
2761 /* update the directory size in the log to reflect the names
2762 * we have removed
2763 */
2764 if (bytes_del) {
2765 struct btrfs_key key;
2766
Li Zefan33345d012011-04-20 10:31:50 +08002767 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002768 key.offset = 0;
2769 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002770 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002771
2772 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002773 if (ret < 0) {
2774 err = ret;
2775 goto fail;
2776 }
Chris Masone02119d2008-09-05 16:13:11 -04002777 if (ret == 0) {
2778 struct btrfs_inode_item *item;
2779 u64 i_size;
2780
2781 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2782 struct btrfs_inode_item);
2783 i_size = btrfs_inode_size(path->nodes[0], item);
2784 if (i_size > bytes_del)
2785 i_size -= bytes_del;
2786 else
2787 i_size = 0;
2788 btrfs_set_inode_size(path->nodes[0], item, i_size);
2789 btrfs_mark_buffer_dirty(path->nodes[0]);
2790 } else
2791 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002792 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002793 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002794fail:
Chris Masone02119d2008-09-05 16:13:11 -04002795 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002796out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002797 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002798 if (ret == -ENOSPC) {
2799 root->fs_info->last_trans_log_full_commit = trans->transid;
2800 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002801 } else if (ret < 0)
2802 btrfs_abort_transaction(trans, root, ret);
2803
Chris Mason12fcfd22009-03-24 10:24:20 -04002804 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002805
Andi Kleen411fc6b2010-10-29 15:14:31 -04002806 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002807}
2808
2809/* see comments for btrfs_del_dir_entries_in_log */
2810int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2811 struct btrfs_root *root,
2812 const char *name, int name_len,
2813 struct inode *inode, u64 dirid)
2814{
2815 struct btrfs_root *log;
2816 u64 index;
2817 int ret;
2818
Chris Mason3a5f1d42008-09-11 15:53:37 -04002819 if (BTRFS_I(inode)->logged_trans < trans->transid)
2820 return 0;
2821
Chris Masone02119d2008-09-05 16:13:11 -04002822 ret = join_running_log_trans(root);
2823 if (ret)
2824 return 0;
2825 log = root->log_root;
2826 mutex_lock(&BTRFS_I(inode)->log_mutex);
2827
Li Zefan33345d012011-04-20 10:31:50 +08002828 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002829 dirid, &index);
2830 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002831 if (ret == -ENOSPC) {
2832 root->fs_info->last_trans_log_full_commit = trans->transid;
2833 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002834 } else if (ret < 0 && ret != -ENOENT)
2835 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002836 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002837
Chris Masone02119d2008-09-05 16:13:11 -04002838 return ret;
2839}
2840
2841/*
2842 * creates a range item in the log for 'dirid'. first_offset and
2843 * last_offset tell us which parts of the key space the log should
2844 * be considered authoritative for.
2845 */
2846static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *log,
2848 struct btrfs_path *path,
2849 int key_type, u64 dirid,
2850 u64 first_offset, u64 last_offset)
2851{
2852 int ret;
2853 struct btrfs_key key;
2854 struct btrfs_dir_log_item *item;
2855
2856 key.objectid = dirid;
2857 key.offset = first_offset;
2858 if (key_type == BTRFS_DIR_ITEM_KEY)
2859 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2860 else
2861 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2862 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002863 if (ret)
2864 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002865
2866 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2867 struct btrfs_dir_log_item);
2868 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2869 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002870 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002871 return 0;
2872}
2873
2874/*
2875 * log all the items included in the current transaction for a given
2876 * directory. This also creates the range items in the log tree required
2877 * to replay anything deleted before the fsync
2878 */
2879static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2880 struct btrfs_root *root, struct inode *inode,
2881 struct btrfs_path *path,
2882 struct btrfs_path *dst_path, int key_type,
2883 u64 min_offset, u64 *last_offset_ret)
2884{
2885 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002886 struct btrfs_root *log = root->log_root;
2887 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002888 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002889 int ret;
2890 int i;
2891 int nritems;
2892 u64 first_offset = min_offset;
2893 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002894 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002895
2896 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002897
Li Zefan33345d012011-04-20 10:31:50 +08002898 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002899 min_key.type = key_type;
2900 min_key.offset = min_offset;
2901
2902 path->keep_locks = 1;
2903
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002904 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002905
2906 /*
2907 * we didn't find anything from this transaction, see if there
2908 * is anything at all
2909 */
Li Zefan33345d012011-04-20 10:31:50 +08002910 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2911 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002912 min_key.type = key_type;
2913 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002914 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002915 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2916 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002918 return ret;
2919 }
Li Zefan33345d012011-04-20 10:31:50 +08002920 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002921
2922 /* if ret == 0 there are items for this type,
2923 * create a range to tell us the last key of this type.
2924 * otherwise, there are no items in this directory after
2925 * *min_offset, and we create a range to indicate that.
2926 */
2927 if (ret == 0) {
2928 struct btrfs_key tmp;
2929 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2930 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002931 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002932 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002933 }
2934 goto done;
2935 }
2936
2937 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002938 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002939 if (ret == 0) {
2940 struct btrfs_key tmp;
2941 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2942 if (key_type == tmp.type) {
2943 first_offset = tmp.offset;
2944 ret = overwrite_item(trans, log, dst_path,
2945 path->nodes[0], path->slots[0],
2946 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002947 if (ret) {
2948 err = ret;
2949 goto done;
2950 }
Chris Masone02119d2008-09-05 16:13:11 -04002951 }
2952 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002953 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002954
2955 /* find the first key from this transaction again */
2956 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302957 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04002958 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04002959
2960 /*
2961 * we have a block from this transaction, log every item in it
2962 * from our directory
2963 */
Chris Masond3977122009-01-05 21:25:51 -05002964 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002965 struct btrfs_key tmp;
2966 src = path->nodes[0];
2967 nritems = btrfs_header_nritems(src);
2968 for (i = path->slots[0]; i < nritems; i++) {
2969 btrfs_item_key_to_cpu(src, &min_key, i);
2970
Li Zefan33345d012011-04-20 10:31:50 +08002971 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002972 goto done;
2973 ret = overwrite_item(trans, log, dst_path, src, i,
2974 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002975 if (ret) {
2976 err = ret;
2977 goto done;
2978 }
Chris Masone02119d2008-09-05 16:13:11 -04002979 }
2980 path->slots[0] = nritems;
2981
2982 /*
2983 * look ahead to the next item and see if it is also
2984 * from this directory and from this transaction
2985 */
2986 ret = btrfs_next_leaf(root, path);
2987 if (ret == 1) {
2988 last_offset = (u64)-1;
2989 goto done;
2990 }
2991 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002992 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002993 last_offset = (u64)-1;
2994 goto done;
2995 }
2996 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2997 ret = overwrite_item(trans, log, dst_path,
2998 path->nodes[0], path->slots[0],
2999 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003000 if (ret)
3001 err = ret;
3002 else
3003 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003004 goto done;
3005 }
3006 }
3007done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003008 btrfs_release_path(path);
3009 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003010
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003011 if (err == 0) {
3012 *last_offset_ret = last_offset;
3013 /*
3014 * insert the log range keys to indicate where the log
3015 * is valid
3016 */
3017 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003018 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003019 if (ret)
3020 err = ret;
3021 }
3022 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003023}
3024
3025/*
3026 * logging directories is very similar to logging inodes, We find all the items
3027 * from the current transaction and write them to the log.
3028 *
3029 * The recovery code scans the directory in the subvolume, and if it finds a
3030 * key in the range logged that is not present in the log tree, then it means
3031 * that dir entry was unlinked during the transaction.
3032 *
3033 * In order for that scan to work, we must include one key smaller than
3034 * the smallest logged by this transaction and one key larger than the largest
3035 * key logged by this transaction.
3036 */
3037static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3038 struct btrfs_root *root, struct inode *inode,
3039 struct btrfs_path *path,
3040 struct btrfs_path *dst_path)
3041{
3042 u64 min_key;
3043 u64 max_key;
3044 int ret;
3045 int key_type = BTRFS_DIR_ITEM_KEY;
3046
3047again:
3048 min_key = 0;
3049 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003050 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003051 ret = log_dir_items(trans, root, inode, path,
3052 dst_path, key_type, min_key,
3053 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003054 if (ret)
3055 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003056 if (max_key == (u64)-1)
3057 break;
3058 min_key = max_key + 1;
3059 }
3060
3061 if (key_type == BTRFS_DIR_ITEM_KEY) {
3062 key_type = BTRFS_DIR_INDEX_KEY;
3063 goto again;
3064 }
3065 return 0;
3066}
3067
3068/*
3069 * a helper function to drop items from the log before we relog an
3070 * inode. max_key_type indicates the highest item type to remove.
3071 * This cannot be run for file data extents because it does not
3072 * free the extents they point to.
3073 */
3074static int drop_objectid_items(struct btrfs_trans_handle *trans,
3075 struct btrfs_root *log,
3076 struct btrfs_path *path,
3077 u64 objectid, int max_key_type)
3078{
3079 int ret;
3080 struct btrfs_key key;
3081 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003082 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003083
3084 key.objectid = objectid;
3085 key.type = max_key_type;
3086 key.offset = (u64)-1;
3087
Chris Masond3977122009-01-05 21:25:51 -05003088 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003089 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003090 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003091 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003092 break;
3093
3094 if (path->slots[0] == 0)
3095 break;
3096
3097 path->slots[0]--;
3098 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3099 path->slots[0]);
3100
3101 if (found_key.objectid != objectid)
3102 break;
3103
Josef Bacik18ec90d2012-09-28 11:56:28 -04003104 found_key.offset = 0;
3105 found_key.type = 0;
3106 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3107 &start_slot);
3108
3109 ret = btrfs_del_items(trans, log, path, start_slot,
3110 path->slots[0] - start_slot + 1);
3111 /*
3112 * If start slot isn't 0 then we don't need to re-search, we've
3113 * found the last guy with the objectid in this tree.
3114 */
3115 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003116 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003117 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003118 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003119 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003120 if (ret > 0)
3121 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003122 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003123}
3124
Josef Bacik94edf4a2012-09-25 14:56:25 -04003125static void fill_inode_item(struct btrfs_trans_handle *trans,
3126 struct extent_buffer *leaf,
3127 struct btrfs_inode_item *item,
3128 struct inode *inode, int log_inode_only)
3129{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003130 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003131
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003132 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003133
3134 if (log_inode_only) {
3135 /* set the generation to zero so the recover code
3136 * can tell the difference between an logging
3137 * just to say 'this inode exists' and a logging
3138 * to say 'update this inode with these values'
3139 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003140 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3141 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003142 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003143 btrfs_set_token_inode_generation(leaf, item,
3144 BTRFS_I(inode)->generation,
3145 &token);
3146 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003147 }
3148
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003149 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3150 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3151 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3152 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3153
3154 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3155 inode->i_atime.tv_sec, &token);
3156 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3157 inode->i_atime.tv_nsec, &token);
3158
3159 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3160 inode->i_mtime.tv_sec, &token);
3161 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3162 inode->i_mtime.tv_nsec, &token);
3163
3164 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3165 inode->i_ctime.tv_sec, &token);
3166 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3167 inode->i_ctime.tv_nsec, &token);
3168
3169 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3170 &token);
3171
3172 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3173 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3174 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3175 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3176 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003177}
3178
Josef Bacika95249b2012-10-11 16:17:34 -04003179static int log_inode_item(struct btrfs_trans_handle *trans,
3180 struct btrfs_root *log, struct btrfs_path *path,
3181 struct inode *inode)
3182{
3183 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003184 int ret;
3185
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003186 ret = btrfs_insert_empty_item(trans, log, path,
3187 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003188 sizeof(*inode_item));
3189 if (ret && ret != -EEXIST)
3190 return ret;
3191 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3192 struct btrfs_inode_item);
3193 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3194 btrfs_release_path(path);
3195 return 0;
3196}
3197
Chris Mason31ff1cd2008-09-11 16:17:57 -04003198static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003199 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003200 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003201 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003202 int start_slot, int nr, int inode_only)
3203{
3204 unsigned long src_offset;
3205 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003206 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003207 struct btrfs_file_extent_item *extent;
3208 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003209 struct extent_buffer *src = src_path->nodes[0];
3210 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003211 int ret;
3212 struct btrfs_key *ins_keys;
3213 u32 *ins_sizes;
3214 char *ins_data;
3215 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003216 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003217 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003218 bool has_extents = false;
3219 bool need_find_last_extent = (*last_extent == 0);
3220 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003221
3222 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003223
3224 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3225 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003226 if (!ins_data)
3227 return -ENOMEM;
3228
Josef Bacik16e75492013-10-22 12:18:51 -04003229 first_key.objectid = (u64)-1;
3230
Chris Mason31ff1cd2008-09-11 16:17:57 -04003231 ins_sizes = (u32 *)ins_data;
3232 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3233
3234 for (i = 0; i < nr; i++) {
3235 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3236 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3237 }
3238 ret = btrfs_insert_empty_items(trans, log, dst_path,
3239 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003240 if (ret) {
3241 kfree(ins_data);
3242 return ret;
3243 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003244
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003245 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003246 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3247 dst_path->slots[0]);
3248
3249 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3250
Josef Bacik16e75492013-10-22 12:18:51 -04003251 if ((i == (nr - 1)))
3252 last_key = ins_keys[i];
3253
Josef Bacik94edf4a2012-09-25 14:56:25 -04003254 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003255 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3256 dst_path->slots[0],
3257 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003258 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3259 inode, inode_only == LOG_INODE_EXISTS);
3260 } else {
3261 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3262 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003263 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003264
Josef Bacik16e75492013-10-22 12:18:51 -04003265 /*
3266 * We set need_find_last_extent here in case we know we were
3267 * processing other items and then walk into the first extent in
3268 * the inode. If we don't hit an extent then nothing changes,
3269 * we'll do the last search the next time around.
3270 */
3271 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3272 has_extents = true;
3273 if (need_find_last_extent &&
3274 first_key.objectid == (u64)-1)
3275 first_key = ins_keys[i];
3276 } else {
3277 need_find_last_extent = false;
3278 }
3279
Chris Mason31ff1cd2008-09-11 16:17:57 -04003280 /* take a reference on file data extents so that truncates
3281 * or deletes of this inode don't have to relog the inode
3282 * again
3283 */
Liu Bod2794402012-08-29 01:07:56 -06003284 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3285 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003286 int found_type;
3287 extent = btrfs_item_ptr(src, start_slot + i,
3288 struct btrfs_file_extent_item);
3289
liubo8e531cd2011-05-06 10:36:09 +08003290 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3291 continue;
3292
Chris Mason31ff1cd2008-09-11 16:17:57 -04003293 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003294 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003295 u64 ds, dl, cs, cl;
3296 ds = btrfs_file_extent_disk_bytenr(src,
3297 extent);
3298 /* ds == 0 is a hole */
3299 if (ds == 0)
3300 continue;
3301
3302 dl = btrfs_file_extent_disk_num_bytes(src,
3303 extent);
3304 cs = btrfs_file_extent_offset(src, extent);
3305 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003306 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003307 if (btrfs_file_extent_compression(src,
3308 extent)) {
3309 cs = 0;
3310 cl = dl;
3311 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003312
3313 ret = btrfs_lookup_csums_range(
3314 log->fs_info->csum_root,
3315 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003316 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003317 if (ret) {
3318 btrfs_release_path(dst_path);
3319 kfree(ins_data);
3320 return ret;
3321 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003322 }
3323 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003324 }
3325
3326 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003327 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003328 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003329
3330 /*
3331 * we have to do this after the loop above to avoid changing the
3332 * log tree while trying to change the log tree.
3333 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003334 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003335 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003336 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3337 struct btrfs_ordered_sum,
3338 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003339 if (!ret)
3340 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003341 list_del(&sums->list);
3342 kfree(sums);
3343 }
Josef Bacik16e75492013-10-22 12:18:51 -04003344
3345 if (!has_extents)
3346 return ret;
3347
3348 /*
3349 * Because we use btrfs_search_forward we could skip leaves that were
3350 * not modified and then assume *last_extent is valid when it really
3351 * isn't. So back up to the previous leaf and read the end of the last
3352 * extent before we go and fill in holes.
3353 */
3354 if (need_find_last_extent) {
3355 u64 len;
3356
3357 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3358 if (ret < 0)
3359 return ret;
3360 if (ret)
3361 goto fill_holes;
3362 if (src_path->slots[0])
3363 src_path->slots[0]--;
3364 src = src_path->nodes[0];
3365 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3366 if (key.objectid != btrfs_ino(inode) ||
3367 key.type != BTRFS_EXTENT_DATA_KEY)
3368 goto fill_holes;
3369 extent = btrfs_item_ptr(src, src_path->slots[0],
3370 struct btrfs_file_extent_item);
3371 if (btrfs_file_extent_type(src, extent) ==
3372 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003373 len = btrfs_file_extent_inline_len(src,
3374 src_path->slots[0],
3375 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003376 *last_extent = ALIGN(key.offset + len,
3377 log->sectorsize);
3378 } else {
3379 len = btrfs_file_extent_num_bytes(src, extent);
3380 *last_extent = key.offset + len;
3381 }
3382 }
3383fill_holes:
3384 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3385 * things could have happened
3386 *
3387 * 1) A merge could have happened, so we could currently be on a leaf
3388 * that holds what we were copying in the first place.
3389 * 2) A split could have happened, and now not all of the items we want
3390 * are on the same leaf.
3391 *
3392 * So we need to adjust how we search for holes, we need to drop the
3393 * path and re-search for the first extent key we found, and then walk
3394 * forward until we hit the last one we copied.
3395 */
3396 if (need_find_last_extent) {
3397 /* btrfs_prev_leaf could return 1 without releasing the path */
3398 btrfs_release_path(src_path);
3399 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3400 src_path, 0, 0);
3401 if (ret < 0)
3402 return ret;
3403 ASSERT(ret == 0);
3404 src = src_path->nodes[0];
3405 i = src_path->slots[0];
3406 } else {
3407 i = start_slot;
3408 }
3409
3410 /*
3411 * Ok so here we need to go through and fill in any holes we may have
3412 * to make sure that holes are punched for those areas in case they had
3413 * extents previously.
3414 */
3415 while (!done) {
3416 u64 offset, len;
3417 u64 extent_end;
3418
3419 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3420 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3421 if (ret < 0)
3422 return ret;
3423 ASSERT(ret == 0);
3424 src = src_path->nodes[0];
3425 i = 0;
3426 }
3427
3428 btrfs_item_key_to_cpu(src, &key, i);
3429 if (!btrfs_comp_cpu_keys(&key, &last_key))
3430 done = true;
3431 if (key.objectid != btrfs_ino(inode) ||
3432 key.type != BTRFS_EXTENT_DATA_KEY) {
3433 i++;
3434 continue;
3435 }
3436 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3437 if (btrfs_file_extent_type(src, extent) ==
3438 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003439 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003440 extent_end = ALIGN(key.offset + len, log->sectorsize);
3441 } else {
3442 len = btrfs_file_extent_num_bytes(src, extent);
3443 extent_end = key.offset + len;
3444 }
3445 i++;
3446
3447 if (*last_extent == key.offset) {
3448 *last_extent = extent_end;
3449 continue;
3450 }
3451 offset = *last_extent;
3452 len = key.offset - *last_extent;
3453 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3454 offset, 0, 0, len, 0, len, 0,
3455 0, 0);
3456 if (ret)
3457 break;
3458 *last_extent = offset + len;
3459 }
3460 /*
3461 * Need to let the callers know we dropped the path so they should
3462 * re-search.
3463 */
3464 if (!ret && need_find_last_extent)
3465 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003466 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003467}
3468
Josef Bacik5dc562c2012-08-17 13:14:17 -04003469static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3470{
3471 struct extent_map *em1, *em2;
3472
3473 em1 = list_entry(a, struct extent_map, list);
3474 em2 = list_entry(b, struct extent_map, list);
3475
3476 if (em1->start < em2->start)
3477 return -1;
3478 else if (em1->start > em2->start)
3479 return 1;
3480 return 0;
3481}
3482
Josef Bacik5dc562c2012-08-17 13:14:17 -04003483static int log_one_extent(struct btrfs_trans_handle *trans,
3484 struct inode *inode, struct btrfs_root *root,
Miao Xie827463c2014-01-14 20:31:51 +08003485 struct extent_map *em, struct btrfs_path *path,
3486 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003487{
3488 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003489 struct btrfs_file_extent_item *fi;
3490 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003491 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003492 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003493 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003494 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003495 u64 mod_start = em->mod_start;
3496 u64 mod_len = em->mod_len;
3497 u64 csum_offset;
3498 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003499 u64 extent_offset = em->start - em->orig_start;
3500 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003501 int ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003502 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003503 int extent_inserted = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003504
Josef Bacik70c8a912012-10-11 16:54:30 -04003505 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003506 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003507
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003508 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3509 em->start + em->len, NULL, 0, 1,
3510 sizeof(*fi), &extent_inserted);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003511 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003512 return ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003513
3514 if (!extent_inserted) {
3515 key.objectid = btrfs_ino(inode);
3516 key.type = BTRFS_EXTENT_DATA_KEY;
3517 key.offset = em->start;
3518
3519 ret = btrfs_insert_empty_item(trans, log, path, &key,
3520 sizeof(*fi));
3521 if (ret)
3522 return ret;
3523 }
Josef Bacik70c8a912012-10-11 16:54:30 -04003524 leaf = path->nodes[0];
3525 fi = btrfs_item_ptr(leaf, path->slots[0],
3526 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003527
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003528 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3529 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003530 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3531 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003532 btrfs_set_token_file_extent_type(leaf, fi,
3533 BTRFS_FILE_EXTENT_PREALLOC,
3534 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003535 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003536 btrfs_set_token_file_extent_type(leaf, fi,
3537 BTRFS_FILE_EXTENT_REG,
3538 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003539 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003540 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003541 }
3542
Josef Bacik70c8a912012-10-11 16:54:30 -04003543 block_len = max(em->block_len, em->orig_block_len);
3544 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003545 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3546 em->block_start,
3547 &token);
3548 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3549 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003550 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003551 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3552 em->block_start -
3553 extent_offset, &token);
3554 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3555 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003556 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003557 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3558 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3559 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003560 }
3561
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003562 btrfs_set_token_file_extent_offset(leaf, fi,
3563 em->start - em->orig_start,
3564 &token);
3565 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003566 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003567 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3568 &token);
3569 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3570 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003571 btrfs_mark_buffer_dirty(leaf);
3572
Josef Bacik70c8a912012-10-11 16:54:30 -04003573 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003574 if (ret) {
3575 return ret;
3576 }
3577
3578 if (skip_csum)
3579 return 0;
3580
Josef Bacik2ab28f32012-10-12 15:27:49 -04003581 /*
3582 * First check and see if our csums are on our outstanding ordered
3583 * extents.
3584 */
Miao Xie827463c2014-01-14 20:31:51 +08003585 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003586 struct btrfs_ordered_sum *sum;
3587
3588 if (!mod_len)
3589 break;
3590
Josef Bacik2ab28f32012-10-12 15:27:49 -04003591 if (ordered->file_offset + ordered->len <= mod_start ||
3592 mod_start + mod_len <= ordered->file_offset)
3593 continue;
3594
3595 /*
3596 * We are going to copy all the csums on this ordered extent, so
3597 * go ahead and adjust mod_start and mod_len in case this
3598 * ordered extent has already been logged.
3599 */
3600 if (ordered->file_offset > mod_start) {
3601 if (ordered->file_offset + ordered->len >=
3602 mod_start + mod_len)
3603 mod_len = ordered->file_offset - mod_start;
3604 /*
3605 * If we have this case
3606 *
3607 * |--------- logged extent ---------|
3608 * |----- ordered extent ----|
3609 *
3610 * Just don't mess with mod_start and mod_len, we'll
3611 * just end up logging more csums than we need and it
3612 * will be ok.
3613 */
3614 } else {
3615 if (ordered->file_offset + ordered->len <
3616 mod_start + mod_len) {
3617 mod_len = (mod_start + mod_len) -
3618 (ordered->file_offset + ordered->len);
3619 mod_start = ordered->file_offset +
3620 ordered->len;
3621 } else {
3622 mod_len = 0;
3623 }
3624 }
3625
3626 /*
3627 * To keep us from looping for the above case of an ordered
3628 * extent that falls inside of the logged extent.
3629 */
3630 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3631 &ordered->flags))
3632 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003633
Miao Xie23c671a2014-01-14 20:31:52 +08003634 if (ordered->csum_bytes_left) {
3635 btrfs_start_ordered_extent(inode, ordered, 0);
3636 wait_event(ordered->wait,
3637 ordered->csum_bytes_left == 0);
3638 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003639
3640 list_for_each_entry(sum, &ordered->list, list) {
3641 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003642 if (ret)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003643 goto unlocked;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003644 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003645
3646 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003647unlocked:
3648
3649 if (!mod_len || ret)
3650 return ret;
3651
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003652 if (em->compress_type) {
3653 csum_offset = 0;
3654 csum_len = block_len;
3655 } else {
3656 csum_offset = mod_start - em->start;
3657 csum_len = mod_len;
3658 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003659
Josef Bacik70c8a912012-10-11 16:54:30 -04003660 /* block start is already adjusted for the file extent offset. */
3661 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3662 em->block_start + csum_offset,
3663 em->block_start + csum_offset +
3664 csum_len - 1, &ordered_sums, 0);
3665 if (ret)
3666 return ret;
3667
3668 while (!list_empty(&ordered_sums)) {
3669 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3670 struct btrfs_ordered_sum,
3671 list);
3672 if (!ret)
3673 ret = btrfs_csum_file_blocks(trans, log, sums);
3674 list_del(&sums->list);
3675 kfree(sums);
3676 }
3677
3678 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003679}
3680
3681static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3682 struct btrfs_root *root,
3683 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003684 struct btrfs_path *path,
3685 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003686{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003687 struct extent_map *em, *n;
3688 struct list_head extents;
3689 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3690 u64 test_gen;
3691 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003692 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003693
3694 INIT_LIST_HEAD(&extents);
3695
Josef Bacik5dc562c2012-08-17 13:14:17 -04003696 write_lock(&tree->lock);
3697 test_gen = root->fs_info->last_trans_committed;
3698
3699 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3700 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003701
3702 /*
3703 * Just an arbitrary number, this can be really CPU intensive
3704 * once we start getting a lot of extents, and really once we
3705 * have a bunch of extents we just want to commit since it will
3706 * be faster.
3707 */
3708 if (++num > 32768) {
3709 list_del_init(&tree->modified_extents);
3710 ret = -EFBIG;
3711 goto process;
3712 }
3713
Josef Bacik5dc562c2012-08-17 13:14:17 -04003714 if (em->generation <= test_gen)
3715 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003716 /* Need a ref to keep it from getting evicted from cache */
3717 atomic_inc(&em->refs);
3718 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003719 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003720 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003721 }
3722
3723 list_sort(NULL, &extents, extent_cmp);
3724
Josef Bacik2ab28f32012-10-12 15:27:49 -04003725process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003726 while (!list_empty(&extents)) {
3727 em = list_entry(extents.next, struct extent_map, list);
3728
3729 list_del_init(&em->list);
3730
3731 /*
3732 * If we had an error we just need to delete everybody from our
3733 * private list.
3734 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003735 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003736 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003737 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003738 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003739 }
3740
3741 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003742
Miao Xie827463c2014-01-14 20:31:51 +08003743 ret = log_one_extent(trans, inode, root, em, path, logged_list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003744 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003745 clear_em_logging(tree, em);
3746 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003747 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003748 WARN_ON(!list_empty(&extents));
3749 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003750
Josef Bacik5dc562c2012-08-17 13:14:17 -04003751 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003752 return ret;
3753}
3754
Chris Masone02119d2008-09-05 16:13:11 -04003755/* log a single inode in the tree log.
3756 * At least one parent directory for this inode must exist in the tree
3757 * or be logged already.
3758 *
3759 * Any items from this inode changed by the current transaction are copied
3760 * to the log tree. An extra reference is taken on any extents in this
3761 * file, allowing us to avoid a whole pile of corner cases around logging
3762 * blocks that have been removed from the tree.
3763 *
3764 * See LOG_INODE_ALL and related defines for a description of what inode_only
3765 * does.
3766 *
3767 * This handles both files and directories.
3768 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003769static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003770 struct btrfs_root *root, struct inode *inode,
3771 int inode_only)
3772{
3773 struct btrfs_path *path;
3774 struct btrfs_path *dst_path;
3775 struct btrfs_key min_key;
3776 struct btrfs_key max_key;
3777 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003778 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003779 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003780 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003781 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003782 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003783 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003784 int ins_start_slot = 0;
3785 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003786 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003787 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003788
Chris Masone02119d2008-09-05 16:13:11 -04003789 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003790 if (!path)
3791 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003792 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003793 if (!dst_path) {
3794 btrfs_free_path(path);
3795 return -ENOMEM;
3796 }
Chris Masone02119d2008-09-05 16:13:11 -04003797
Li Zefan33345d012011-04-20 10:31:50 +08003798 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003799 min_key.type = BTRFS_INODE_ITEM_KEY;
3800 min_key.offset = 0;
3801
Li Zefan33345d012011-04-20 10:31:50 +08003802 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003803
Chris Mason12fcfd22009-03-24 10:24:20 -04003804
Josef Bacik5dc562c2012-08-17 13:14:17 -04003805 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003806 if (S_ISDIR(inode->i_mode) ||
3807 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3808 &BTRFS_I(inode)->runtime_flags) &&
3809 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003810 max_key.type = BTRFS_XATTR_ITEM_KEY;
3811 else
3812 max_key.type = (u8)-1;
3813 max_key.offset = (u64)-1;
3814
Josef Bacik94edf4a2012-09-25 14:56:25 -04003815 /* Only run delayed items if we are a dir or a new file */
3816 if (S_ISDIR(inode->i_mode) ||
3817 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3818 ret = btrfs_commit_inode_delayed_items(trans, inode);
3819 if (ret) {
3820 btrfs_free_path(path);
3821 btrfs_free_path(dst_path);
3822 return ret;
3823 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003824 }
3825
Chris Masone02119d2008-09-05 16:13:11 -04003826 mutex_lock(&BTRFS_I(inode)->log_mutex);
3827
Miao Xie827463c2014-01-14 20:31:51 +08003828 btrfs_get_logged_extents(inode, &logged_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003829
Chris Masone02119d2008-09-05 16:13:11 -04003830 /*
3831 * a brute force approach to making sure we get the most uptodate
3832 * copies of everything.
3833 */
3834 if (S_ISDIR(inode->i_mode)) {
3835 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3836
3837 if (inode_only == LOG_INODE_EXISTS)
3838 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003839 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003840 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003841 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3842 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003843 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3844 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003845 ret = btrfs_truncate_inode_items(trans, log,
3846 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003847 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05003848 &BTRFS_I(inode)->runtime_flags) ||
3849 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04003850 if (inode_only == LOG_INODE_ALL)
3851 fast_search = true;
3852 max_key.type = BTRFS_XATTR_ITEM_KEY;
3853 ret = drop_objectid_items(trans, log, path, ino,
3854 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003855 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003856 if (inode_only == LOG_INODE_ALL)
3857 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003858 ret = log_inode_item(trans, log, dst_path, inode);
3859 if (ret) {
3860 err = ret;
3861 goto out_unlock;
3862 }
3863 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003864 }
Josef Bacika95249b2012-10-11 16:17:34 -04003865
Chris Masone02119d2008-09-05 16:13:11 -04003866 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003867 if (ret) {
3868 err = ret;
3869 goto out_unlock;
3870 }
Chris Masone02119d2008-09-05 16:13:11 -04003871 path->keep_locks = 1;
3872
Chris Masond3977122009-01-05 21:25:51 -05003873 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003874 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003875 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003876 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003877 if (ret != 0)
3878 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003879again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003880 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003881 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003882 break;
3883 if (min_key.type > max_key.type)
3884 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003885
Chris Masone02119d2008-09-05 16:13:11 -04003886 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003887 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3888 ins_nr++;
3889 goto next_slot;
3890 } else if (!ins_nr) {
3891 ins_start_slot = path->slots[0];
3892 ins_nr = 1;
3893 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003894 }
3895
Josef Bacik16e75492013-10-22 12:18:51 -04003896 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3897 ins_start_slot, ins_nr, inode_only);
3898 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003899 err = ret;
3900 goto out_unlock;
Josef Bacik16e75492013-10-22 12:18:51 -04003901 } if (ret) {
3902 ins_nr = 0;
3903 btrfs_release_path(path);
3904 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003905 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003906 ins_nr = 1;
3907 ins_start_slot = path->slots[0];
3908next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003909
Chris Mason3a5f1d42008-09-11 15:53:37 -04003910 nritems = btrfs_header_nritems(path->nodes[0]);
3911 path->slots[0]++;
3912 if (path->slots[0] < nritems) {
3913 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3914 path->slots[0]);
3915 goto again;
3916 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003917 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003918 ret = copy_items(trans, inode, dst_path, path,
3919 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003920 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04003921 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003922 err = ret;
3923 goto out_unlock;
3924 }
Josef Bacik16e75492013-10-22 12:18:51 -04003925 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003926 ins_nr = 0;
3927 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003928 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003929
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003930 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003931 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003932 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003933 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003934 min_key.offset = 0;
3935 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003936 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003937 }
Chris Masone02119d2008-09-05 16:13:11 -04003938 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003939 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003940 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3941 ins_start_slot, ins_nr, inode_only);
3942 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003943 err = ret;
3944 goto out_unlock;
3945 }
Josef Bacik16e75492013-10-22 12:18:51 -04003946 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003947 ins_nr = 0;
3948 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003949
Josef Bacika95249b2012-10-11 16:17:34 -04003950log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003951 btrfs_release_path(path);
3952 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003953 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08003954 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
3955 &logged_list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003956 if (ret) {
3957 err = ret;
3958 goto out_unlock;
3959 }
Josef Bacikd006a042013-11-12 20:54:09 -05003960 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06003961 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3962 struct extent_map *em, *n;
3963
Miao Xiebbe14262012-11-01 07:34:54 +00003964 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003965 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3966 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003967 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003968 }
3969
Chris Mason9623f9a2008-09-11 17:42:42 -04003970 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003971 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003972 if (ret) {
3973 err = ret;
3974 goto out_unlock;
3975 }
Chris Masone02119d2008-09-05 16:13:11 -04003976 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003977 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003978 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003979out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08003980 if (unlikely(err))
3981 btrfs_put_logged_extents(&logged_list);
3982 else
3983 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04003984 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3985
3986 btrfs_free_path(path);
3987 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003988 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003989}
3990
Chris Mason12fcfd22009-03-24 10:24:20 -04003991/*
3992 * follow the dentry parent pointers up the chain and see if any
3993 * of the directories in it require a full commit before they can
3994 * be logged. Returns zero if nothing special needs to be done or 1 if
3995 * a full commit is required.
3996 */
3997static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3998 struct inode *inode,
3999 struct dentry *parent,
4000 struct super_block *sb,
4001 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004002{
Chris Mason12fcfd22009-03-24 10:24:20 -04004003 int ret = 0;
4004 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004005 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004006 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004007
Chris Masonaf4176b2009-03-24 10:24:31 -04004008 /*
4009 * for regular files, if its inode is already on disk, we don't
4010 * have to worry about the parents at all. This is because
4011 * we can use the last_unlink_trans field to record renames
4012 * and other fun in this file.
4013 */
4014 if (S_ISREG(inode->i_mode) &&
4015 BTRFS_I(inode)->generation <= last_committed &&
4016 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4017 goto out;
4018
Chris Mason12fcfd22009-03-24 10:24:20 -04004019 if (!S_ISDIR(inode->i_mode)) {
4020 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4021 goto out;
4022 inode = parent->d_inode;
4023 }
4024
4025 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004026 /*
4027 * If we are logging a directory then we start with our inode,
4028 * not our parents inode, so we need to skipp setting the
4029 * logged_trans so that further down in the log code we don't
4030 * think this inode has already been logged.
4031 */
4032 if (inode != orig_inode)
4033 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004034 smp_mb();
4035
4036 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4037 root = BTRFS_I(inode)->root;
4038
4039 /*
4040 * make sure any commits to the log are forced
4041 * to be full commits
4042 */
4043 root->fs_info->last_trans_log_full_commit =
4044 trans->transid;
4045 ret = 1;
4046 break;
4047 }
4048
4049 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4050 break;
4051
Yan, Zheng76dda932009-09-21 16:00:26 -04004052 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004053 break;
4054
Josef Bacik6a912212010-11-20 09:48:00 +00004055 parent = dget_parent(parent);
4056 dput(old_parent);
4057 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004058 inode = parent->d_inode;
4059
4060 }
Josef Bacik6a912212010-11-20 09:48:00 +00004061 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004062out:
Chris Masone02119d2008-09-05 16:13:11 -04004063 return ret;
4064}
4065
4066/*
4067 * helper function around btrfs_log_inode to make sure newly created
4068 * parent directories also end up in the log. A minimal inode and backref
4069 * only logging is done of any parent directories that are older than
4070 * the last committed transaction
4071 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004072static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4073 struct btrfs_root *root, struct inode *inode,
4074 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04004075{
Chris Mason12fcfd22009-03-24 10:24:20 -04004076 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004077 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004078 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004079 int ret = 0;
4080 u64 last_committed = root->fs_info->last_trans_committed;
4081
4082 sb = inode->i_sb;
4083
Sage Weil3a5e1402009-04-02 16:49:40 -04004084 if (btrfs_test_opt(root, NOTREELOG)) {
4085 ret = 1;
4086 goto end_no_trans;
4087 }
4088
Chris Mason12fcfd22009-03-24 10:24:20 -04004089 if (root->fs_info->last_trans_log_full_commit >
4090 root->fs_info->last_trans_committed) {
4091 ret = 1;
4092 goto end_no_trans;
4093 }
4094
Yan, Zheng76dda932009-09-21 16:00:26 -04004095 if (root != BTRFS_I(inode)->root ||
4096 btrfs_root_refs(&root->root_item) == 0) {
4097 ret = 1;
4098 goto end_no_trans;
4099 }
4100
Chris Mason12fcfd22009-03-24 10:24:20 -04004101 ret = check_parent_dirs_for_sync(trans, inode, parent,
4102 sb, last_committed);
4103 if (ret)
4104 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004105
Josef Bacik22ee6982012-05-29 16:57:49 -04004106 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004107 ret = BTRFS_NO_LOG_SYNC;
4108 goto end_no_trans;
4109 }
4110
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004111 ret = start_log_trans(trans, root);
4112 if (ret)
4113 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004114
4115 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004116 if (ret)
4117 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004118
Chris Masonaf4176b2009-03-24 10:24:31 -04004119 /*
4120 * for regular files, if its inode is already on disk, we don't
4121 * have to worry about the parents at all. This is because
4122 * we can use the last_unlink_trans field to record renames
4123 * and other fun in this file.
4124 */
4125 if (S_ISREG(inode->i_mode) &&
4126 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004127 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4128 ret = 0;
4129 goto end_trans;
4130 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004131
4132 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05004133 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004134 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004135 break;
4136
Chris Mason12fcfd22009-03-24 10:24:20 -04004137 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004138 if (root != BTRFS_I(inode)->root)
4139 break;
4140
Chris Mason12fcfd22009-03-24 10:24:20 -04004141 if (BTRFS_I(inode)->generation >
4142 root->fs_info->last_trans_committed) {
4143 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004144 if (ret)
4145 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004146 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004147 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004148 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004149
Josef Bacik6a912212010-11-20 09:48:00 +00004150 parent = dget_parent(parent);
4151 dput(old_parent);
4152 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004153 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004154 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004155end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004156 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004157 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004158 root->fs_info->last_trans_log_full_commit = trans->transid;
4159 ret = 1;
4160 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004161 btrfs_end_log_trans(root);
4162end_no_trans:
4163 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004164}
4165
4166/*
4167 * it is not safe to log dentry if the chunk root has added new
4168 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4169 * If this returns 1, you must commit the transaction to safely get your
4170 * data on disk.
4171 */
4172int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4173 struct btrfs_root *root, struct dentry *dentry)
4174{
Josef Bacik6a912212010-11-20 09:48:00 +00004175 struct dentry *parent = dget_parent(dentry);
4176 int ret;
4177
4178 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
4179 dput(parent);
4180
4181 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004182}
4183
4184/*
4185 * should be called during mount to recover any replay any log trees
4186 * from the FS
4187 */
4188int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4189{
4190 int ret;
4191 struct btrfs_path *path;
4192 struct btrfs_trans_handle *trans;
4193 struct btrfs_key key;
4194 struct btrfs_key found_key;
4195 struct btrfs_key tmp_key;
4196 struct btrfs_root *log;
4197 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4198 struct walk_control wc = {
4199 .process_func = process_one_buffer,
4200 .stage = 0,
4201 };
4202
Chris Masone02119d2008-09-05 16:13:11 -04004203 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004204 if (!path)
4205 return -ENOMEM;
4206
4207 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004208
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004209 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004210 if (IS_ERR(trans)) {
4211 ret = PTR_ERR(trans);
4212 goto error;
4213 }
Chris Masone02119d2008-09-05 16:13:11 -04004214
4215 wc.trans = trans;
4216 wc.pin = 1;
4217
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004218 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (ret) {
4220 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4221 "recovering log root tree.");
4222 goto error;
4223 }
Chris Masone02119d2008-09-05 16:13:11 -04004224
4225again:
4226 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4227 key.offset = (u64)-1;
4228 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4229
Chris Masond3977122009-01-05 21:25:51 -05004230 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004231 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232
4233 if (ret < 0) {
4234 btrfs_error(fs_info, ret,
4235 "Couldn't find tree log root.");
4236 goto error;
4237 }
Chris Masone02119d2008-09-05 16:13:11 -04004238 if (ret > 0) {
4239 if (path->slots[0] == 0)
4240 break;
4241 path->slots[0]--;
4242 }
4243 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4244 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004245 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004246 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4247 break;
4248
Miao Xiecb517ea2013-05-15 07:48:19 +00004249 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 if (IS_ERR(log)) {
4251 ret = PTR_ERR(log);
4252 btrfs_error(fs_info, ret,
4253 "Couldn't read tree log root.");
4254 goto error;
4255 }
Chris Masone02119d2008-09-05 16:13:11 -04004256
4257 tmp_key.objectid = found_key.offset;
4258 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4259 tmp_key.offset = (u64)-1;
4260
4261 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 if (IS_ERR(wc.replay_dest)) {
4263 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004264 free_extent_buffer(log->node);
4265 free_extent_buffer(log->commit_root);
4266 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267 btrfs_error(fs_info, ret, "Couldn't read target root "
4268 "for tree log recovery.");
4269 goto error;
4270 }
Chris Masone02119d2008-09-05 16:13:11 -04004271
Yan Zheng07d400a2009-01-06 11:42:00 -05004272 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004273 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004274 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004275
Josef Bacikb50c6e22013-04-25 15:55:30 -04004276 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004277 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4278 path);
Chris Masone02119d2008-09-05 16:13:11 -04004279 }
Chris Masone02119d2008-09-05 16:13:11 -04004280
4281 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004282 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004283 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004284 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004285 kfree(log);
4286
Josef Bacikb50c6e22013-04-25 15:55:30 -04004287 if (ret)
4288 goto error;
4289
Chris Masone02119d2008-09-05 16:13:11 -04004290 if (found_key.offset == 0)
4291 break;
4292 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004293 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004294
4295 /* step one is to pin it all, step two is to replay just inodes */
4296 if (wc.pin) {
4297 wc.pin = 0;
4298 wc.process_func = replay_one_buffer;
4299 wc.stage = LOG_WALK_REPLAY_INODES;
4300 goto again;
4301 }
4302 /* step three is to replay everything */
4303 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4304 wc.stage++;
4305 goto again;
4306 }
4307
4308 btrfs_free_path(path);
4309
Josef Bacikabefa552013-04-24 16:40:05 -04004310 /* step 4: commit the transaction, which also unpins the blocks */
4311 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4312 if (ret)
4313 return ret;
4314
Chris Masone02119d2008-09-05 16:13:11 -04004315 free_extent_buffer(log_root_tree->node);
4316 log_root_tree->log_root = NULL;
4317 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004318 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004319
Josef Bacikabefa552013-04-24 16:40:05 -04004320 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004322 if (wc.trans)
4323 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004324 btrfs_free_path(path);
4325 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004326}
Chris Mason12fcfd22009-03-24 10:24:20 -04004327
4328/*
4329 * there are some corner cases where we want to force a full
4330 * commit instead of allowing a directory to be logged.
4331 *
4332 * They revolve around files there were unlinked from the directory, and
4333 * this function updates the parent directory so that a full commit is
4334 * properly done if it is fsync'd later after the unlinks are done.
4335 */
4336void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4337 struct inode *dir, struct inode *inode,
4338 int for_rename)
4339{
4340 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004341 * when we're logging a file, if it hasn't been renamed
4342 * or unlinked, and its inode is fully committed on disk,
4343 * we don't have to worry about walking up the directory chain
4344 * to log its parents.
4345 *
4346 * So, we use the last_unlink_trans field to put this transid
4347 * into the file. When the file is logged we check it and
4348 * don't log the parents if the file is fully on disk.
4349 */
4350 if (S_ISREG(inode->i_mode))
4351 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4352
4353 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004354 * if this directory was already logged any new
4355 * names for this file/dir will get recorded
4356 */
4357 smp_mb();
4358 if (BTRFS_I(dir)->logged_trans == trans->transid)
4359 return;
4360
4361 /*
4362 * if the inode we're about to unlink was logged,
4363 * the log will be properly updated for any new names
4364 */
4365 if (BTRFS_I(inode)->logged_trans == trans->transid)
4366 return;
4367
4368 /*
4369 * when renaming files across directories, if the directory
4370 * there we're unlinking from gets fsync'd later on, there's
4371 * no way to find the destination directory later and fsync it
4372 * properly. So, we have to be conservative and force commits
4373 * so the new name gets discovered.
4374 */
4375 if (for_rename)
4376 goto record;
4377
4378 /* we can safely do the unlink without any special recording */
4379 return;
4380
4381record:
4382 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4383}
4384
4385/*
4386 * Call this after adding a new name for a file and it will properly
4387 * update the log to reflect the new name.
4388 *
4389 * It will return zero if all goes well, and it will return 1 if a
4390 * full transaction commit is required.
4391 */
4392int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4393 struct inode *inode, struct inode *old_dir,
4394 struct dentry *parent)
4395{
4396 struct btrfs_root * root = BTRFS_I(inode)->root;
4397
4398 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004399 * this will force the logging code to walk the dentry chain
4400 * up for the file
4401 */
4402 if (S_ISREG(inode->i_mode))
4403 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4404
4405 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004406 * if this inode hasn't been logged and directory we're renaming it
4407 * from hasn't been logged, we don't need to log it
4408 */
4409 if (BTRFS_I(inode)->logged_trans <=
4410 root->fs_info->last_trans_committed &&
4411 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4412 root->fs_info->last_trans_committed))
4413 return 0;
4414
4415 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4416}
4417