blob: f98002ed32b1890b9446cd9a0a47edf08a1eee2c [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 Zheng7237f182009-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 Zheng7237f182009-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 Zheng7237f182009-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 Zheng7237f182009-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 Zheng7237f182009-01-21 12:54:03 -0500194 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400195 }
Yan Zheng7237f182009-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 Zheng7237f182009-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 Masonc8b97812008-10-29 14:49:59 -0400573 size = btrfs_file_extent_inline_len(eb, 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;
1241 ret = btrfs_find_orphan_item(root, offset);
1242 if (ret > 0)
1243 ret = btrfs_insert_orphan_item(trans, root, offset);
1244 return ret;
1245}
1246
Mark Fashehf1863732012-08-08 11:32:27 -07001247static int count_inode_extrefs(struct btrfs_root *root,
1248 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001249{
Mark Fashehf1863732012-08-08 11:32:27 -07001250 int ret = 0;
1251 int name_len;
1252 unsigned int nlink = 0;
1253 u32 item_size;
1254 u32 cur_offset = 0;
1255 u64 inode_objectid = btrfs_ino(inode);
1256 u64 offset = 0;
1257 unsigned long ptr;
1258 struct btrfs_inode_extref *extref;
1259 struct extent_buffer *leaf;
1260
1261 while (1) {
1262 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1263 &extref, &offset);
1264 if (ret)
1265 break;
1266
1267 leaf = path->nodes[0];
1268 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1269 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1270
1271 while (cur_offset < item_size) {
1272 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1273 name_len = btrfs_inode_extref_name_len(leaf, extref);
1274
1275 nlink++;
1276
1277 cur_offset += name_len + sizeof(*extref);
1278 }
1279
1280 offset++;
1281 btrfs_release_path(path);
1282 }
1283 btrfs_release_path(path);
1284
1285 if (ret < 0)
1286 return ret;
1287 return nlink;
1288}
1289
1290static int count_inode_refs(struct btrfs_root *root,
1291 struct inode *inode, struct btrfs_path *path)
1292{
Chris Masone02119d2008-09-05 16:13:11 -04001293 int ret;
1294 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001295 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001296 unsigned long ptr;
1297 unsigned long ptr_end;
1298 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001299 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001300
Li Zefan33345d012011-04-20 10:31:50 +08001301 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001302 key.type = BTRFS_INODE_REF_KEY;
1303 key.offset = (u64)-1;
1304
Chris Masond3977122009-01-05 21:25:51 -05001305 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1307 if (ret < 0)
1308 break;
1309 if (ret > 0) {
1310 if (path->slots[0] == 0)
1311 break;
1312 path->slots[0]--;
1313 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001314process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001315 btrfs_item_key_to_cpu(path->nodes[0], &key,
1316 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001317 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001318 key.type != BTRFS_INODE_REF_KEY)
1319 break;
1320 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1321 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1322 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001323 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001324 struct btrfs_inode_ref *ref;
1325
1326 ref = (struct btrfs_inode_ref *)ptr;
1327 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1328 ref);
1329 ptr = (unsigned long)(ref + 1) + name_len;
1330 nlink++;
1331 }
1332
1333 if (key.offset == 0)
1334 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001335 if (path->slots[0] > 0) {
1336 path->slots[0]--;
1337 goto process_slot;
1338 }
Chris Masone02119d2008-09-05 16:13:11 -04001339 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001340 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001341 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001342 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001343
1344 return nlink;
1345}
1346
1347/*
1348 * There are a few corners where the link count of the file can't
1349 * be properly maintained during replay. So, instead of adding
1350 * lots of complexity to the log code, we just scan the backrefs
1351 * for any file that has been through replay.
1352 *
1353 * The scan will update the link count on the inode to reflect the
1354 * number of back refs found. If it goes down to zero, the iput
1355 * will free the inode.
1356 */
1357static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1358 struct btrfs_root *root,
1359 struct inode *inode)
1360{
1361 struct btrfs_path *path;
1362 int ret;
1363 u64 nlink = 0;
1364 u64 ino = btrfs_ino(inode);
1365
1366 path = btrfs_alloc_path();
1367 if (!path)
1368 return -ENOMEM;
1369
1370 ret = count_inode_refs(root, inode, path);
1371 if (ret < 0)
1372 goto out;
1373
1374 nlink = ret;
1375
1376 ret = count_inode_extrefs(root, inode, path);
1377 if (ret == -ENOENT)
1378 ret = 0;
1379
1380 if (ret < 0)
1381 goto out;
1382
1383 nlink += ret;
1384
1385 ret = 0;
1386
Chris Masone02119d2008-09-05 16:13:11 -04001387 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001388 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001389 btrfs_update_inode(trans, root, inode);
1390 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001391 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001392
Yan, Zhengc71bf092009-11-12 09:34:40 +00001393 if (inode->i_nlink == 0) {
1394 if (S_ISDIR(inode->i_mode)) {
1395 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001396 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001397 if (ret)
1398 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001399 }
Li Zefan33345d012011-04-20 10:31:50 +08001400 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001401 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001402
Mark Fashehf1863732012-08-08 11:32:27 -07001403out:
1404 btrfs_free_path(path);
1405 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001406}
1407
1408static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *root,
1410 struct btrfs_path *path)
1411{
1412 int ret;
1413 struct btrfs_key key;
1414 struct inode *inode;
1415
1416 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1417 key.type = BTRFS_ORPHAN_ITEM_KEY;
1418 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001419 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001420 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1421 if (ret < 0)
1422 break;
1423
1424 if (ret == 1) {
1425 if (path->slots[0] == 0)
1426 break;
1427 path->slots[0]--;
1428 }
1429
1430 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1431 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1432 key.type != BTRFS_ORPHAN_ITEM_KEY)
1433 break;
1434
1435 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001436 if (ret)
1437 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001438
David Sterbab3b4aa72011-04-21 01:20:15 +02001439 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001440 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001441 if (!inode)
1442 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001443
1444 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001445 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001446 if (ret)
1447 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001448
Chris Mason12fcfd22009-03-24 10:24:20 -04001449 /*
1450 * fixup on a directory may create new entries,
1451 * make sure we always look for the highset possible
1452 * offset
1453 */
1454 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001455 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001456 ret = 0;
1457out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001458 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001459 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001460}
1461
1462
1463/*
1464 * record a given inode in the fixup dir so we can check its link
1465 * count when replay is done. The link count is incremented here
1466 * so the inode won't go away until we check it
1467 */
1468static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1469 struct btrfs_root *root,
1470 struct btrfs_path *path,
1471 u64 objectid)
1472{
1473 struct btrfs_key key;
1474 int ret = 0;
1475 struct inode *inode;
1476
1477 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001478 if (!inode)
1479 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001480
1481 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1482 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1483 key.offset = objectid;
1484
1485 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1486
David Sterbab3b4aa72011-04-21 01:20:15 +02001487 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001488 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001489 if (!inode->i_nlink)
1490 set_nlink(inode, 1);
1491 else
Zach Brown8b558c52013-10-16 12:10:34 -07001492 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001493 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001494 } else if (ret == -EEXIST) {
1495 ret = 0;
1496 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001497 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001498 }
1499 iput(inode);
1500
1501 return ret;
1502}
1503
1504/*
1505 * when replaying the log for a directory, we only insert names
1506 * for inodes that actually exist. This means an fsync on a directory
1507 * does not implicitly fsync all the new files in it
1508 */
1509static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root,
1511 struct btrfs_path *path,
1512 u64 dirid, u64 index,
1513 char *name, int name_len, u8 type,
1514 struct btrfs_key *location)
1515{
1516 struct inode *inode;
1517 struct inode *dir;
1518 int ret;
1519
1520 inode = read_one_inode(root, location->objectid);
1521 if (!inode)
1522 return -ENOENT;
1523
1524 dir = read_one_inode(root, dirid);
1525 if (!dir) {
1526 iput(inode);
1527 return -EIO;
1528 }
Josef Bacikd5554382013-09-11 14:17:00 -04001529
Chris Masone02119d2008-09-05 16:13:11 -04001530 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1531
1532 /* FIXME, put inode into FIXUP list */
1533
1534 iput(inode);
1535 iput(dir);
1536 return ret;
1537}
1538
1539/*
1540 * take a single entry in a log directory item and replay it into
1541 * the subvolume.
1542 *
1543 * if a conflicting item exists in the subdirectory already,
1544 * the inode it points to is unlinked and put into the link count
1545 * fix up tree.
1546 *
1547 * If a name from the log points to a file or directory that does
1548 * not exist in the FS, it is skipped. fsyncs on directories
1549 * do not force down inodes inside that directory, just changes to the
1550 * names or unlinks in a directory.
1551 */
1552static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root,
1554 struct btrfs_path *path,
1555 struct extent_buffer *eb,
1556 struct btrfs_dir_item *di,
1557 struct btrfs_key *key)
1558{
1559 char *name;
1560 int name_len;
1561 struct btrfs_dir_item *dst_di;
1562 struct btrfs_key found_key;
1563 struct btrfs_key log_key;
1564 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001565 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001566 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001567 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001568 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001569
1570 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001571 if (!dir)
1572 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001573
1574 name_len = btrfs_dir_name_len(eb, di);
1575 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001576 if (!name) {
1577 ret = -ENOMEM;
1578 goto out;
1579 }
liubo2a29edc2011-01-26 06:22:08 +00001580
Chris Masone02119d2008-09-05 16:13:11 -04001581 log_type = btrfs_dir_type(eb, di);
1582 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1583 name_len);
1584
1585 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001586 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1587 if (exists == 0)
1588 exists = 1;
1589 else
1590 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001591 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001592
Chris Masone02119d2008-09-05 16:13:11 -04001593 if (key->type == BTRFS_DIR_ITEM_KEY) {
1594 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1595 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001596 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001597 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1598 key->objectid,
1599 key->offset, name,
1600 name_len, 1);
1601 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001602 /* Corruption */
1603 ret = -EINVAL;
1604 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001605 }
David Sterbac7040052011-04-19 18:00:01 +02001606 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001607 /* we need a sequence number to insert, so we only
1608 * do inserts for the BTRFS_DIR_INDEX_KEY types
1609 */
1610 if (key->type != BTRFS_DIR_INDEX_KEY)
1611 goto out;
1612 goto insert;
1613 }
1614
1615 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1616 /* the existing item matches the logged item */
1617 if (found_key.objectid == log_key.objectid &&
1618 found_key.type == log_key.type &&
1619 found_key.offset == log_key.offset &&
1620 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1621 goto out;
1622 }
1623
1624 /*
1625 * don't drop the conflicting directory entry if the inode
1626 * for the new entry doesn't exist
1627 */
Chris Mason4bef0842008-09-08 11:18:08 -04001628 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001629 goto out;
1630
Chris Masone02119d2008-09-05 16:13:11 -04001631 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001632 if (ret)
1633 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001634
1635 if (key->type == BTRFS_DIR_INDEX_KEY)
1636 goto insert;
1637out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001638 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001639 if (!ret && update_size) {
1640 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1641 ret = btrfs_update_inode(trans, root, dir);
1642 }
Chris Masone02119d2008-09-05 16:13:11 -04001643 kfree(name);
1644 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001645 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001646
1647insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001648 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001649 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1650 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001651 if (ret && ret != -ENOENT)
1652 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001653 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001654 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001655 goto out;
1656}
1657
1658/*
1659 * find all the names in a directory item and reconcile them into
1660 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1661 * one name in a directory item, but the same code gets used for
1662 * both directory index types
1663 */
1664static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1665 struct btrfs_root *root,
1666 struct btrfs_path *path,
1667 struct extent_buffer *eb, int slot,
1668 struct btrfs_key *key)
1669{
1670 int ret;
1671 u32 item_size = btrfs_item_size_nr(eb, slot);
1672 struct btrfs_dir_item *di;
1673 int name_len;
1674 unsigned long ptr;
1675 unsigned long ptr_end;
1676
1677 ptr = btrfs_item_ptr_offset(eb, slot);
1678 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001679 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001680 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001681 if (verify_dir_item(root, eb, di))
1682 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001683 name_len = btrfs_dir_name_len(eb, di);
1684 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001685 if (ret)
1686 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001687 ptr = (unsigned long)(di + 1);
1688 ptr += name_len;
1689 }
1690 return 0;
1691}
1692
1693/*
1694 * directory replay has two parts. There are the standard directory
1695 * items in the log copied from the subvolume, and range items
1696 * created in the log while the subvolume was logged.
1697 *
1698 * The range items tell us which parts of the key space the log
1699 * is authoritative for. During replay, if a key in the subvolume
1700 * directory is in a logged range item, but not actually in the log
1701 * that means it was deleted from the directory before the fsync
1702 * and should be removed.
1703 */
1704static noinline int find_dir_range(struct btrfs_root *root,
1705 struct btrfs_path *path,
1706 u64 dirid, int key_type,
1707 u64 *start_ret, u64 *end_ret)
1708{
1709 struct btrfs_key key;
1710 u64 found_end;
1711 struct btrfs_dir_log_item *item;
1712 int ret;
1713 int nritems;
1714
1715 if (*start_ret == (u64)-1)
1716 return 1;
1717
1718 key.objectid = dirid;
1719 key.type = key_type;
1720 key.offset = *start_ret;
1721
1722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1723 if (ret < 0)
1724 goto out;
1725 if (ret > 0) {
1726 if (path->slots[0] == 0)
1727 goto out;
1728 path->slots[0]--;
1729 }
1730 if (ret != 0)
1731 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1732
1733 if (key.type != key_type || key.objectid != dirid) {
1734 ret = 1;
1735 goto next;
1736 }
1737 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1738 struct btrfs_dir_log_item);
1739 found_end = btrfs_dir_log_end(path->nodes[0], item);
1740
1741 if (*start_ret >= key.offset && *start_ret <= found_end) {
1742 ret = 0;
1743 *start_ret = key.offset;
1744 *end_ret = found_end;
1745 goto out;
1746 }
1747 ret = 1;
1748next:
1749 /* check the next slot in the tree to see if it is a valid item */
1750 nritems = btrfs_header_nritems(path->nodes[0]);
1751 if (path->slots[0] >= nritems) {
1752 ret = btrfs_next_leaf(root, path);
1753 if (ret)
1754 goto out;
1755 } else {
1756 path->slots[0]++;
1757 }
1758
1759 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1760
1761 if (key.type != key_type || key.objectid != dirid) {
1762 ret = 1;
1763 goto out;
1764 }
1765 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1766 struct btrfs_dir_log_item);
1767 found_end = btrfs_dir_log_end(path->nodes[0], item);
1768 *start_ret = key.offset;
1769 *end_ret = found_end;
1770 ret = 0;
1771out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001773 return ret;
1774}
1775
1776/*
1777 * this looks for a given directory item in the log. If the directory
1778 * item is not in the log, the item is removed and the inode it points
1779 * to is unlinked
1780 */
1781static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1782 struct btrfs_root *root,
1783 struct btrfs_root *log,
1784 struct btrfs_path *path,
1785 struct btrfs_path *log_path,
1786 struct inode *dir,
1787 struct btrfs_key *dir_key)
1788{
1789 int ret;
1790 struct extent_buffer *eb;
1791 int slot;
1792 u32 item_size;
1793 struct btrfs_dir_item *di;
1794 struct btrfs_dir_item *log_di;
1795 int name_len;
1796 unsigned long ptr;
1797 unsigned long ptr_end;
1798 char *name;
1799 struct inode *inode;
1800 struct btrfs_key location;
1801
1802again:
1803 eb = path->nodes[0];
1804 slot = path->slots[0];
1805 item_size = btrfs_item_size_nr(eb, slot);
1806 ptr = btrfs_item_ptr_offset(eb, slot);
1807 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001808 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001809 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001810 if (verify_dir_item(root, eb, di)) {
1811 ret = -EIO;
1812 goto out;
1813 }
1814
Chris Masone02119d2008-09-05 16:13:11 -04001815 name_len = btrfs_dir_name_len(eb, di);
1816 name = kmalloc(name_len, GFP_NOFS);
1817 if (!name) {
1818 ret = -ENOMEM;
1819 goto out;
1820 }
1821 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1822 name_len);
1823 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001824 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001825 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1826 dir_key->objectid,
1827 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001828 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001829 log_di = btrfs_lookup_dir_index_item(trans, log,
1830 log_path,
1831 dir_key->objectid,
1832 dir_key->offset,
1833 name, name_len, 0);
1834 }
David Sterbac7040052011-04-19 18:00:01 +02001835 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001836 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001837 btrfs_release_path(path);
1838 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001839 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001840 if (!inode) {
1841 kfree(name);
1842 return -EIO;
1843 }
Chris Masone02119d2008-09-05 16:13:11 -04001844
1845 ret = link_to_fixup_dir(trans, root,
1846 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001847 if (ret) {
1848 kfree(name);
1849 iput(inode);
1850 goto out;
1851 }
1852
Zach Brown8b558c52013-10-16 12:10:34 -07001853 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001854 ret = btrfs_unlink_inode(trans, root, dir, inode,
1855 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001856 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001857 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001858 kfree(name);
1859 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001860 if (ret)
1861 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001862
1863 /* there might still be more names under this key
1864 * check and repeat if required
1865 */
1866 ret = btrfs_search_slot(NULL, root, dir_key, path,
1867 0, 0);
1868 if (ret == 0)
1869 goto again;
1870 ret = 0;
1871 goto out;
1872 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001873 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001874 kfree(name);
1875
1876 ptr = (unsigned long)(di + 1);
1877 ptr += name_len;
1878 }
1879 ret = 0;
1880out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001881 btrfs_release_path(path);
1882 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001883 return ret;
1884}
1885
1886/*
1887 * deletion replay happens before we copy any new directory items
1888 * out of the log or out of backreferences from inodes. It
1889 * scans the log to find ranges of keys that log is authoritative for,
1890 * and then scans the directory to find items in those ranges that are
1891 * not present in the log.
1892 *
1893 * Anything we don't find in the log is unlinked and removed from the
1894 * directory.
1895 */
1896static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1897 struct btrfs_root *root,
1898 struct btrfs_root *log,
1899 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001900 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001901{
1902 u64 range_start;
1903 u64 range_end;
1904 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1905 int ret = 0;
1906 struct btrfs_key dir_key;
1907 struct btrfs_key found_key;
1908 struct btrfs_path *log_path;
1909 struct inode *dir;
1910
1911 dir_key.objectid = dirid;
1912 dir_key.type = BTRFS_DIR_ITEM_KEY;
1913 log_path = btrfs_alloc_path();
1914 if (!log_path)
1915 return -ENOMEM;
1916
1917 dir = read_one_inode(root, dirid);
1918 /* it isn't an error if the inode isn't there, that can happen
1919 * because we replay the deletes before we copy in the inode item
1920 * from the log
1921 */
1922 if (!dir) {
1923 btrfs_free_path(log_path);
1924 return 0;
1925 }
1926again:
1927 range_start = 0;
1928 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001929 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001930 if (del_all)
1931 range_end = (u64)-1;
1932 else {
1933 ret = find_dir_range(log, path, dirid, key_type,
1934 &range_start, &range_end);
1935 if (ret != 0)
1936 break;
1937 }
Chris Masone02119d2008-09-05 16:13:11 -04001938
1939 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001940 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001941 int nritems;
1942 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1943 0, 0);
1944 if (ret < 0)
1945 goto out;
1946
1947 nritems = btrfs_header_nritems(path->nodes[0]);
1948 if (path->slots[0] >= nritems) {
1949 ret = btrfs_next_leaf(root, path);
1950 if (ret)
1951 break;
1952 }
1953 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1954 path->slots[0]);
1955 if (found_key.objectid != dirid ||
1956 found_key.type != dir_key.type)
1957 goto next_type;
1958
1959 if (found_key.offset > range_end)
1960 break;
1961
1962 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001963 log_path, dir,
1964 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001965 if (ret)
1966 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001967 if (found_key.offset == (u64)-1)
1968 break;
1969 dir_key.offset = found_key.offset + 1;
1970 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001971 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001972 if (range_end == (u64)-1)
1973 break;
1974 range_start = range_end + 1;
1975 }
1976
1977next_type:
1978 ret = 0;
1979 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1980 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1981 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001982 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001983 goto again;
1984 }
1985out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001986 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001987 btrfs_free_path(log_path);
1988 iput(dir);
1989 return ret;
1990}
1991
1992/*
1993 * the process_func used to replay items from the log tree. This
1994 * gets called in two different stages. The first stage just looks
1995 * for inodes and makes sure they are all copied into the subvolume.
1996 *
1997 * The second stage copies all the other item types from the log into
1998 * the subvolume. The two stage approach is slower, but gets rid of
1999 * lots of complexity around inodes referencing other inodes that exist
2000 * only in the log (references come from either directory items or inode
2001 * back refs).
2002 */
2003static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2004 struct walk_control *wc, u64 gen)
2005{
2006 int nritems;
2007 struct btrfs_path *path;
2008 struct btrfs_root *root = wc->replay_dest;
2009 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002010 int level;
2011 int i;
2012 int ret;
2013
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002014 ret = btrfs_read_buffer(eb, gen);
2015 if (ret)
2016 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002017
2018 level = btrfs_header_level(eb);
2019
2020 if (level != 0)
2021 return 0;
2022
2023 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002024 if (!path)
2025 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002026
2027 nritems = btrfs_header_nritems(eb);
2028 for (i = 0; i < nritems; i++) {
2029 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002030
2031 /* inode keys are done during the first stage */
2032 if (key.type == BTRFS_INODE_ITEM_KEY &&
2033 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002034 struct btrfs_inode_item *inode_item;
2035 u32 mode;
2036
2037 inode_item = btrfs_item_ptr(eb, i,
2038 struct btrfs_inode_item);
2039 mode = btrfs_inode_mode(eb, inode_item);
2040 if (S_ISDIR(mode)) {
2041 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002042 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002043 if (ret)
2044 break;
Chris Masone02119d2008-09-05 16:13:11 -04002045 }
2046 ret = overwrite_item(wc->trans, root, path,
2047 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002048 if (ret)
2049 break;
Chris Masone02119d2008-09-05 16:13:11 -04002050
Yan, Zhengc71bf092009-11-12 09:34:40 +00002051 /* for regular files, make sure corresponding
2052 * orhpan item exist. extents past the new EOF
2053 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002054 */
2055 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002056 ret = insert_orphan_item(wc->trans, root,
2057 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002058 if (ret)
2059 break;
Chris Masone02119d2008-09-05 16:13:11 -04002060 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002061
Chris Masone02119d2008-09-05 16:13:11 -04002062 ret = link_to_fixup_dir(wc->trans, root,
2063 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002064 if (ret)
2065 break;
Chris Masone02119d2008-09-05 16:13:11 -04002066 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002067
2068 if (key.type == BTRFS_DIR_INDEX_KEY &&
2069 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2070 ret = replay_one_dir_item(wc->trans, root, path,
2071 eb, i, &key);
2072 if (ret)
2073 break;
2074 }
2075
Chris Masone02119d2008-09-05 16:13:11 -04002076 if (wc->stage < LOG_WALK_REPLAY_ALL)
2077 continue;
2078
2079 /* these keys are simply copied */
2080 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2081 ret = overwrite_item(wc->trans, root, path,
2082 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002083 if (ret)
2084 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002085 } else if (key.type == BTRFS_INODE_REF_KEY ||
2086 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002087 ret = add_inode_ref(wc->trans, root, log, path,
2088 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002089 if (ret && ret != -ENOENT)
2090 break;
2091 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002092 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2093 ret = replay_one_extent(wc->trans, root, path,
2094 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002095 if (ret)
2096 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002097 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002098 ret = replay_one_dir_item(wc->trans, root, path,
2099 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002100 if (ret)
2101 break;
Chris Masone02119d2008-09-05 16:13:11 -04002102 }
2103 }
2104 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002105 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002106}
2107
Chris Masond3977122009-01-05 21:25:51 -05002108static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002109 struct btrfs_root *root,
2110 struct btrfs_path *path, int *level,
2111 struct walk_control *wc)
2112{
2113 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002114 u64 bytenr;
2115 u64 ptr_gen;
2116 struct extent_buffer *next;
2117 struct extent_buffer *cur;
2118 struct extent_buffer *parent;
2119 u32 blocksize;
2120 int ret = 0;
2121
2122 WARN_ON(*level < 0);
2123 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2124
Chris Masond3977122009-01-05 21:25:51 -05002125 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002126 WARN_ON(*level < 0);
2127 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2128 cur = path->nodes[*level];
2129
2130 if (btrfs_header_level(cur) != *level)
2131 WARN_ON(1);
2132
2133 if (path->slots[*level] >=
2134 btrfs_header_nritems(cur))
2135 break;
2136
2137 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2138 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2139 blocksize = btrfs_level_size(root, *level - 1);
2140
2141 parent = path->nodes[*level];
2142 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002143
2144 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002145 if (!next)
2146 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002147
Chris Masone02119d2008-09-05 16:13:11 -04002148 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002149 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002150 if (ret) {
2151 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002152 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002153 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002154
Chris Masone02119d2008-09-05 16:13:11 -04002155 path->slots[*level]++;
2156 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002157 ret = btrfs_read_buffer(next, ptr_gen);
2158 if (ret) {
2159 free_extent_buffer(next);
2160 return ret;
2161 }
Chris Masone02119d2008-09-05 16:13:11 -04002162
Josef Bacik681ae502013-10-07 15:11:00 -04002163 if (trans) {
2164 btrfs_tree_lock(next);
2165 btrfs_set_lock_blocking(next);
2166 clean_tree_block(trans, root, next);
2167 btrfs_wait_tree_block_writeback(next);
2168 btrfs_tree_unlock(next);
2169 }
Chris Masone02119d2008-09-05 16:13:11 -04002170
Chris Masone02119d2008-09-05 16:13:11 -04002171 WARN_ON(root_owner !=
2172 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002173 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002174 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002175 if (ret) {
2176 free_extent_buffer(next);
2177 return ret;
2178 }
Chris Masone02119d2008-09-05 16:13:11 -04002179 }
2180 free_extent_buffer(next);
2181 continue;
2182 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002183 ret = btrfs_read_buffer(next, ptr_gen);
2184 if (ret) {
2185 free_extent_buffer(next);
2186 return ret;
2187 }
Chris Masone02119d2008-09-05 16:13:11 -04002188
2189 WARN_ON(*level <= 0);
2190 if (path->nodes[*level-1])
2191 free_extent_buffer(path->nodes[*level-1]);
2192 path->nodes[*level-1] = next;
2193 *level = btrfs_header_level(next);
2194 path->slots[*level] = 0;
2195 cond_resched();
2196 }
2197 WARN_ON(*level < 0);
2198 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2199
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002200 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002201
2202 cond_resched();
2203 return 0;
2204}
2205
Chris Masond3977122009-01-05 21:25:51 -05002206static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002207 struct btrfs_root *root,
2208 struct btrfs_path *path, int *level,
2209 struct walk_control *wc)
2210{
2211 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002212 int i;
2213 int slot;
2214 int ret;
2215
Chris Masond3977122009-01-05 21:25:51 -05002216 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002217 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002218 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002219 path->slots[i]++;
2220 *level = i;
2221 WARN_ON(*level == 0);
2222 return 0;
2223 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002224 struct extent_buffer *parent;
2225 if (path->nodes[*level] == root->node)
2226 parent = path->nodes[*level];
2227 else
2228 parent = path->nodes[*level + 1];
2229
2230 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002231 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002232 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002233 if (ret)
2234 return ret;
2235
Chris Masone02119d2008-09-05 16:13:11 -04002236 if (wc->free) {
2237 struct extent_buffer *next;
2238
2239 next = path->nodes[*level];
2240
Josef Bacik681ae502013-10-07 15:11:00 -04002241 if (trans) {
2242 btrfs_tree_lock(next);
2243 btrfs_set_lock_blocking(next);
2244 clean_tree_block(trans, root, next);
2245 btrfs_wait_tree_block_writeback(next);
2246 btrfs_tree_unlock(next);
2247 }
Chris Masone02119d2008-09-05 16:13:11 -04002248
Chris Masone02119d2008-09-05 16:13:11 -04002249 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002250 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002251 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002252 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002253 if (ret)
2254 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002255 }
2256 free_extent_buffer(path->nodes[*level]);
2257 path->nodes[*level] = NULL;
2258 *level = i + 1;
2259 }
2260 }
2261 return 1;
2262}
2263
2264/*
2265 * drop the reference count on the tree rooted at 'snap'. This traverses
2266 * the tree freeing any blocks that have a ref count of zero after being
2267 * decremented.
2268 */
2269static int walk_log_tree(struct btrfs_trans_handle *trans,
2270 struct btrfs_root *log, struct walk_control *wc)
2271{
2272 int ret = 0;
2273 int wret;
2274 int level;
2275 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002276 int orig_level;
2277
2278 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002279 if (!path)
2280 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002281
2282 level = btrfs_header_level(log->node);
2283 orig_level = level;
2284 path->nodes[level] = log->node;
2285 extent_buffer_get(log->node);
2286 path->slots[level] = 0;
2287
Chris Masond3977122009-01-05 21:25:51 -05002288 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002289 wret = walk_down_log_tree(trans, log, path, &level, wc);
2290 if (wret > 0)
2291 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002293 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002294 goto out;
2295 }
Chris Masone02119d2008-09-05 16:13:11 -04002296
2297 wret = walk_up_log_tree(trans, log, path, &level, wc);
2298 if (wret > 0)
2299 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002300 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002301 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002302 goto out;
2303 }
Chris Masone02119d2008-09-05 16:13:11 -04002304 }
2305
2306 /* was the root node processed? if not, catch it here */
2307 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002308 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002309 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002310 if (ret)
2311 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002312 if (wc->free) {
2313 struct extent_buffer *next;
2314
2315 next = path->nodes[orig_level];
2316
Josef Bacik681ae502013-10-07 15:11:00 -04002317 if (trans) {
2318 btrfs_tree_lock(next);
2319 btrfs_set_lock_blocking(next);
2320 clean_tree_block(trans, log, next);
2321 btrfs_wait_tree_block_writeback(next);
2322 btrfs_tree_unlock(next);
2323 }
Chris Masone02119d2008-09-05 16:13:11 -04002324
Chris Masone02119d2008-09-05 16:13:11 -04002325 WARN_ON(log->root_key.objectid !=
2326 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002327 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002328 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002329 if (ret)
2330 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002331 }
2332 }
2333
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002334out:
Chris Masone02119d2008-09-05 16:13:11 -04002335 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002336 return ret;
2337}
2338
Yan Zheng7237f182009-01-21 12:54:03 -05002339/*
2340 * helper function to update the item for a given subvolumes log root
2341 * in the tree of log roots
2342 */
2343static int update_log_root(struct btrfs_trans_handle *trans,
2344 struct btrfs_root *log)
2345{
2346 int ret;
2347
2348 if (log->log_transid == 1) {
2349 /* insert root item on the first sync */
2350 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2351 &log->root_key, &log->root_item);
2352 } else {
2353 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2354 &log->root_key, &log->root_item);
2355 }
2356 return ret;
2357}
2358
Chris Mason12fcfd22009-03-24 10:24:20 -04002359static int wait_log_commit(struct btrfs_trans_handle *trans,
2360 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002361{
2362 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002363 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002364
Yan Zheng7237f182009-01-21 12:54:03 -05002365 /*
2366 * we only allow two pending log transactions at a time,
2367 * so we know that if ours is more than 2 older than the
2368 * current transaction, we're done
2369 */
Chris Masone02119d2008-09-05 16:13:11 -04002370 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002371 prepare_to_wait(&root->log_commit_wait[index],
2372 &wait, TASK_UNINTERRUPTIBLE);
2373 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002374
2375 if (root->fs_info->last_trans_log_full_commit !=
2376 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002377 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002378 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002379
Yan Zheng7237f182009-01-21 12:54:03 -05002380 finish_wait(&root->log_commit_wait[index], &wait);
2381 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002382 } while (root->fs_info->last_trans_log_full_commit !=
2383 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002384 atomic_read(&root->log_commit[index]));
2385 return 0;
2386}
2387
Jeff Mahoney143bede2012-03-01 14:56:26 +01002388static void wait_for_writer(struct btrfs_trans_handle *trans,
2389 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002390{
2391 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002392 while (root->fs_info->last_trans_log_full_commit !=
2393 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002394 prepare_to_wait(&root->log_writer_wait,
2395 &wait, TASK_UNINTERRUPTIBLE);
2396 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002397 if (root->fs_info->last_trans_log_full_commit !=
2398 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002399 schedule();
2400 mutex_lock(&root->log_mutex);
2401 finish_wait(&root->log_writer_wait, &wait);
2402 }
Chris Masone02119d2008-09-05 16:13:11 -04002403}
2404
2405/*
2406 * btrfs_sync_log does sends a given tree log down to the disk and
2407 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002408 * you know that any inodes previously logged are safely on disk only
2409 * if it returns 0.
2410 *
2411 * Any other return value means you need to call btrfs_commit_transaction.
2412 * Some of the edge cases for fsyncing directories that have had unlinks
2413 * or renames done in the past mean that sometimes the only safe
2414 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2415 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002416 */
2417int btrfs_sync_log(struct btrfs_trans_handle *trans,
2418 struct btrfs_root *root)
2419{
Yan Zheng7237f182009-01-21 12:54:03 -05002420 int index1;
2421 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002422 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002423 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002424 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002425 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002426 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002427 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002428
Yan Zheng7237f182009-01-21 12:54:03 -05002429 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002430 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002431 index1 = root->log_transid % 2;
2432 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002433 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002434 mutex_unlock(&root->log_mutex);
2435 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002436 }
Yan Zheng7237f182009-01-21 12:54:03 -05002437 atomic_set(&root->log_commit[index1], 1);
2438
2439 /* wait for previous tree log sync to complete */
2440 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002441 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002442 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002443 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002444 /* when we're on an ssd, just kick the log commit out */
2445 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002446 mutex_unlock(&root->log_mutex);
2447 schedule_timeout_uninterruptible(1);
2448 mutex_lock(&root->log_mutex);
2449 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002450 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002451 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002452 break;
2453 }
Chris Masond0c803c2008-09-11 16:17:57 -04002454
Chris Mason12fcfd22009-03-24 10:24:20 -04002455 /* bail out if we need to do a full commit */
2456 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2457 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002458 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002459 mutex_unlock(&root->log_mutex);
2460 goto out;
2461 }
2462
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002463 if (log_transid % 2 == 0)
2464 mark = EXTENT_DIRTY;
2465 else
2466 mark = EXTENT_NEW;
2467
Chris Mason690587d2009-10-13 13:29:19 -04002468 /* we start IO on all the marked extents here, but we don't actually
2469 * wait for them until later.
2470 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002471 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002472 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002473 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002474 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002475 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002476 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 mutex_unlock(&root->log_mutex);
2478 goto out;
2479 }
Yan Zheng7237f182009-01-21 12:54:03 -05002480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002482
Yan Zheng7237f182009-01-21 12:54:03 -05002483 root->log_transid++;
2484 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002485 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002486 smp_mb();
2487 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002488 * IO has been started, blocks of the log tree have WRITTEN flag set
2489 * in their headers. new modifications of the log will be written to
2490 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002491 */
2492 mutex_unlock(&root->log_mutex);
2493
2494 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002495 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002496 atomic_inc(&log_root_tree->log_writers);
2497 mutex_unlock(&log_root_tree->log_mutex);
2498
2499 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002500
2501 mutex_lock(&log_root_tree->log_mutex);
2502 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2503 smp_mb();
2504 if (waitqueue_active(&log_root_tree->log_writer_wait))
2505 wake_up(&log_root_tree->log_writer_wait);
2506 }
2507
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002508 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002509 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002510 if (ret != -ENOSPC) {
2511 btrfs_abort_transaction(trans, root, ret);
2512 mutex_unlock(&log_root_tree->log_mutex);
2513 goto out;
2514 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002515 root->fs_info->last_trans_log_full_commit = trans->transid;
2516 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002517 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002518 mutex_unlock(&log_root_tree->log_mutex);
2519 ret = -EAGAIN;
2520 goto out;
2521 }
2522
Yan Zheng7237f182009-01-21 12:54:03 -05002523 index2 = log_root_tree->log_transid % 2;
2524 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002525 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002526 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002527 wait_log_commit(trans, log_root_tree,
2528 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002529 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002530 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002531 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002532 goto out;
2533 }
2534 atomic_set(&log_root_tree->log_commit[index2], 1);
2535
Chris Mason12fcfd22009-03-24 10:24:20 -04002536 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2537 wait_log_commit(trans, log_root_tree,
2538 log_root_tree->log_transid - 1);
2539 }
Yan Zheng7237f182009-01-21 12:54:03 -05002540
Chris Mason12fcfd22009-03-24 10:24:20 -04002541 wait_for_writer(trans, log_root_tree);
2542
2543 /*
2544 * now that we've moved on to the tree of log tree roots,
2545 * check the full commit flag again
2546 */
2547 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002548 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002549 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002550 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002551 mutex_unlock(&log_root_tree->log_mutex);
2552 ret = -EAGAIN;
2553 goto out_wake_log_root;
2554 }
Yan Zheng7237f182009-01-21 12:54:03 -05002555
Miao Xiec6adc9c2013-05-28 10:05:39 +00002556 ret = btrfs_write_marked_extents(log_root_tree,
2557 &log_root_tree->dirty_log_pages,
2558 EXTENT_DIRTY | EXTENT_NEW);
2559 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002560 if (ret) {
2561 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002562 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002563 mutex_unlock(&log_root_tree->log_mutex);
2564 goto out_wake_log_root;
2565 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002566 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002567 btrfs_wait_marked_extents(log_root_tree,
2568 &log_root_tree->dirty_log_pages,
2569 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002570 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002571
David Sterba6c417612011-04-13 15:41:04 +02002572 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002573 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002574 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002575 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002576
Yan Zheng7237f182009-01-21 12:54:03 -05002577 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002578 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002579
2580 mutex_unlock(&log_root_tree->log_mutex);
2581
2582 /*
2583 * nobody else is going to jump in and write the the ctree
2584 * super here because the log_commit atomic below is protecting
2585 * us. We must be called with a transaction handle pinning
2586 * the running transaction open, so a full commit can't hop
2587 * in and cause problems either.
2588 */
Arne Jansena2de7332011-03-08 14:14:00 +01002589 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002590 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002591 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002592 if (ret) {
2593 btrfs_abort_transaction(trans, root, ret);
2594 goto out_wake_log_root;
2595 }
Yan Zheng7237f182009-01-21 12:54:03 -05002596
Chris Mason257c62e2009-10-13 13:21:08 -04002597 mutex_lock(&root->log_mutex);
2598 if (root->last_log_commit < log_transid)
2599 root->last_log_commit = log_transid;
2600 mutex_unlock(&root->log_mutex);
2601
Chris Mason12fcfd22009-03-24 10:24:20 -04002602out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002603 atomic_set(&log_root_tree->log_commit[index2], 0);
2604 smp_mb();
2605 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2606 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002607out:
Yan Zheng7237f182009-01-21 12:54:03 -05002608 atomic_set(&root->log_commit[index1], 0);
2609 smp_mb();
2610 if (waitqueue_active(&root->log_commit_wait[index1]))
2611 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002612 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002613}
2614
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002615static void free_log_tree(struct btrfs_trans_handle *trans,
2616 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002617{
2618 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002619 u64 start;
2620 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002621 struct walk_control wc = {
2622 .free = 1,
2623 .process_func = process_one_buffer
2624 };
2625
Josef Bacik681ae502013-10-07 15:11:00 -04002626 ret = walk_log_tree(trans, log, &wc);
2627 /* I don't think this can happen but just in case */
2628 if (ret)
2629 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002630
Chris Masond3977122009-01-05 21:25:51 -05002631 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002632 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002633 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2634 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002635 if (ret)
2636 break;
2637
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002638 clear_extent_bits(&log->dirty_log_pages, start, end,
2639 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002640 }
2641
Josef Bacik2ab28f32012-10-12 15:27:49 -04002642 /*
2643 * We may have short-circuited the log tree with the full commit logic
2644 * and left ordered extents on our list, so clear these out to keep us
2645 * from leaking inodes and memory.
2646 */
2647 btrfs_free_logged_extents(log, 0);
2648 btrfs_free_logged_extents(log, 1);
2649
Yan Zheng7237f182009-01-21 12:54:03 -05002650 free_extent_buffer(log->node);
2651 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002652}
2653
2654/*
2655 * free all the extents used by the tree log. This should be called
2656 * at commit time of the full transaction
2657 */
2658int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2659{
2660 if (root->log_root) {
2661 free_log_tree(trans, root->log_root);
2662 root->log_root = NULL;
2663 }
2664 return 0;
2665}
2666
2667int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2668 struct btrfs_fs_info *fs_info)
2669{
2670 if (fs_info->log_root_tree) {
2671 free_log_tree(trans, fs_info->log_root_tree);
2672 fs_info->log_root_tree = NULL;
2673 }
Chris Masone02119d2008-09-05 16:13:11 -04002674 return 0;
2675}
2676
2677/*
Chris Masone02119d2008-09-05 16:13:11 -04002678 * If both a file and directory are logged, and unlinks or renames are
2679 * mixed in, we have a few interesting corners:
2680 *
2681 * create file X in dir Y
2682 * link file X to X.link in dir Y
2683 * fsync file X
2684 * unlink file X but leave X.link
2685 * fsync dir Y
2686 *
2687 * After a crash we would expect only X.link to exist. But file X
2688 * didn't get fsync'd again so the log has back refs for X and X.link.
2689 *
2690 * We solve this by removing directory entries and inode backrefs from the
2691 * log when a file that was logged in the current transaction is
2692 * unlinked. Any later fsync will include the updated log entries, and
2693 * we'll be able to reconstruct the proper directory items from backrefs.
2694 *
2695 * This optimizations allows us to avoid relogging the entire inode
2696 * or the entire directory.
2697 */
2698int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2699 struct btrfs_root *root,
2700 const char *name, int name_len,
2701 struct inode *dir, u64 index)
2702{
2703 struct btrfs_root *log;
2704 struct btrfs_dir_item *di;
2705 struct btrfs_path *path;
2706 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002707 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002708 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002709 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002710
Chris Mason3a5f1d42008-09-11 15:53:37 -04002711 if (BTRFS_I(dir)->logged_trans < trans->transid)
2712 return 0;
2713
Chris Masone02119d2008-09-05 16:13:11 -04002714 ret = join_running_log_trans(root);
2715 if (ret)
2716 return 0;
2717
2718 mutex_lock(&BTRFS_I(dir)->log_mutex);
2719
2720 log = root->log_root;
2721 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002722 if (!path) {
2723 err = -ENOMEM;
2724 goto out_unlock;
2725 }
liubo2a29edc2011-01-26 06:22:08 +00002726
Li Zefan33345d012011-04-20 10:31:50 +08002727 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002728 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002729 if (IS_ERR(di)) {
2730 err = PTR_ERR(di);
2731 goto fail;
2732 }
2733 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002734 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2735 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002736 if (ret) {
2737 err = ret;
2738 goto fail;
2739 }
Chris Masone02119d2008-09-05 16:13:11 -04002740 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002741 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002742 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002743 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002744 if (IS_ERR(di)) {
2745 err = PTR_ERR(di);
2746 goto fail;
2747 }
2748 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002749 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2750 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002751 if (ret) {
2752 err = ret;
2753 goto fail;
2754 }
Chris Masone02119d2008-09-05 16:13:11 -04002755 }
2756
2757 /* update the directory size in the log to reflect the names
2758 * we have removed
2759 */
2760 if (bytes_del) {
2761 struct btrfs_key key;
2762
Li Zefan33345d012011-04-20 10:31:50 +08002763 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002764 key.offset = 0;
2765 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002766 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002767
2768 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002769 if (ret < 0) {
2770 err = ret;
2771 goto fail;
2772 }
Chris Masone02119d2008-09-05 16:13:11 -04002773 if (ret == 0) {
2774 struct btrfs_inode_item *item;
2775 u64 i_size;
2776
2777 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2778 struct btrfs_inode_item);
2779 i_size = btrfs_inode_size(path->nodes[0], item);
2780 if (i_size > bytes_del)
2781 i_size -= bytes_del;
2782 else
2783 i_size = 0;
2784 btrfs_set_inode_size(path->nodes[0], item, i_size);
2785 btrfs_mark_buffer_dirty(path->nodes[0]);
2786 } else
2787 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002788 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002789 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002790fail:
Chris Masone02119d2008-09-05 16:13:11 -04002791 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002792out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002793 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002794 if (ret == -ENOSPC) {
2795 root->fs_info->last_trans_log_full_commit = trans->transid;
2796 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002797 } else if (ret < 0)
2798 btrfs_abort_transaction(trans, root, ret);
2799
Chris Mason12fcfd22009-03-24 10:24:20 -04002800 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002801
Andi Kleen411fc6b2010-10-29 15:14:31 -04002802 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002803}
2804
2805/* see comments for btrfs_del_dir_entries_in_log */
2806int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2807 struct btrfs_root *root,
2808 const char *name, int name_len,
2809 struct inode *inode, u64 dirid)
2810{
2811 struct btrfs_root *log;
2812 u64 index;
2813 int ret;
2814
Chris Mason3a5f1d42008-09-11 15:53:37 -04002815 if (BTRFS_I(inode)->logged_trans < trans->transid)
2816 return 0;
2817
Chris Masone02119d2008-09-05 16:13:11 -04002818 ret = join_running_log_trans(root);
2819 if (ret)
2820 return 0;
2821 log = root->log_root;
2822 mutex_lock(&BTRFS_I(inode)->log_mutex);
2823
Li Zefan33345d012011-04-20 10:31:50 +08002824 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002825 dirid, &index);
2826 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002827 if (ret == -ENOSPC) {
2828 root->fs_info->last_trans_log_full_commit = trans->transid;
2829 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830 } else if (ret < 0 && ret != -ENOENT)
2831 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002832 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002833
Chris Masone02119d2008-09-05 16:13:11 -04002834 return ret;
2835}
2836
2837/*
2838 * creates a range item in the log for 'dirid'. first_offset and
2839 * last_offset tell us which parts of the key space the log should
2840 * be considered authoritative for.
2841 */
2842static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2843 struct btrfs_root *log,
2844 struct btrfs_path *path,
2845 int key_type, u64 dirid,
2846 u64 first_offset, u64 last_offset)
2847{
2848 int ret;
2849 struct btrfs_key key;
2850 struct btrfs_dir_log_item *item;
2851
2852 key.objectid = dirid;
2853 key.offset = first_offset;
2854 if (key_type == BTRFS_DIR_ITEM_KEY)
2855 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2856 else
2857 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2858 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 if (ret)
2860 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002861
2862 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2863 struct btrfs_dir_log_item);
2864 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2865 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002866 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002867 return 0;
2868}
2869
2870/*
2871 * log all the items included in the current transaction for a given
2872 * directory. This also creates the range items in the log tree required
2873 * to replay anything deleted before the fsync
2874 */
2875static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2876 struct btrfs_root *root, struct inode *inode,
2877 struct btrfs_path *path,
2878 struct btrfs_path *dst_path, int key_type,
2879 u64 min_offset, u64 *last_offset_ret)
2880{
2881 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002882 struct btrfs_root *log = root->log_root;
2883 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002884 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002885 int ret;
2886 int i;
2887 int nritems;
2888 u64 first_offset = min_offset;
2889 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002890 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002891
2892 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002893
Li Zefan33345d012011-04-20 10:31:50 +08002894 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002895 min_key.type = key_type;
2896 min_key.offset = min_offset;
2897
2898 path->keep_locks = 1;
2899
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002900 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002901
2902 /*
2903 * we didn't find anything from this transaction, see if there
2904 * is anything at all
2905 */
Li Zefan33345d012011-04-20 10:31:50 +08002906 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2907 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002908 min_key.type = key_type;
2909 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002910 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002911 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2912 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002913 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002914 return ret;
2915 }
Li Zefan33345d012011-04-20 10:31:50 +08002916 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002917
2918 /* if ret == 0 there are items for this type,
2919 * create a range to tell us the last key of this type.
2920 * otherwise, there are no items in this directory after
2921 * *min_offset, and we create a range to indicate that.
2922 */
2923 if (ret == 0) {
2924 struct btrfs_key tmp;
2925 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2926 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002927 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002928 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002929 }
2930 goto done;
2931 }
2932
2933 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002934 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002935 if (ret == 0) {
2936 struct btrfs_key tmp;
2937 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2938 if (key_type == tmp.type) {
2939 first_offset = tmp.offset;
2940 ret = overwrite_item(trans, log, dst_path,
2941 path->nodes[0], path->slots[0],
2942 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002943 if (ret) {
2944 err = ret;
2945 goto done;
2946 }
Chris Masone02119d2008-09-05 16:13:11 -04002947 }
2948 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002949 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002950
2951 /* find the first key from this transaction again */
2952 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2953 if (ret != 0) {
2954 WARN_ON(1);
2955 goto done;
2956 }
2957
2958 /*
2959 * we have a block from this transaction, log every item in it
2960 * from our directory
2961 */
Chris Masond3977122009-01-05 21:25:51 -05002962 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002963 struct btrfs_key tmp;
2964 src = path->nodes[0];
2965 nritems = btrfs_header_nritems(src);
2966 for (i = path->slots[0]; i < nritems; i++) {
2967 btrfs_item_key_to_cpu(src, &min_key, i);
2968
Li Zefan33345d012011-04-20 10:31:50 +08002969 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002970 goto done;
2971 ret = overwrite_item(trans, log, dst_path, src, i,
2972 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002973 if (ret) {
2974 err = ret;
2975 goto done;
2976 }
Chris Masone02119d2008-09-05 16:13:11 -04002977 }
2978 path->slots[0] = nritems;
2979
2980 /*
2981 * look ahead to the next item and see if it is also
2982 * from this directory and from this transaction
2983 */
2984 ret = btrfs_next_leaf(root, path);
2985 if (ret == 1) {
2986 last_offset = (u64)-1;
2987 goto done;
2988 }
2989 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002990 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002991 last_offset = (u64)-1;
2992 goto done;
2993 }
2994 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2995 ret = overwrite_item(trans, log, dst_path,
2996 path->nodes[0], path->slots[0],
2997 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002998 if (ret)
2999 err = ret;
3000 else
3001 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003002 goto done;
3003 }
3004 }
3005done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003006 btrfs_release_path(path);
3007 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003008
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003009 if (err == 0) {
3010 *last_offset_ret = last_offset;
3011 /*
3012 * insert the log range keys to indicate where the log
3013 * is valid
3014 */
3015 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003016 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003017 if (ret)
3018 err = ret;
3019 }
3020 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003021}
3022
3023/*
3024 * logging directories is very similar to logging inodes, We find all the items
3025 * from the current transaction and write them to the log.
3026 *
3027 * The recovery code scans the directory in the subvolume, and if it finds a
3028 * key in the range logged that is not present in the log tree, then it means
3029 * that dir entry was unlinked during the transaction.
3030 *
3031 * In order for that scan to work, we must include one key smaller than
3032 * the smallest logged by this transaction and one key larger than the largest
3033 * key logged by this transaction.
3034 */
3035static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3036 struct btrfs_root *root, struct inode *inode,
3037 struct btrfs_path *path,
3038 struct btrfs_path *dst_path)
3039{
3040 u64 min_key;
3041 u64 max_key;
3042 int ret;
3043 int key_type = BTRFS_DIR_ITEM_KEY;
3044
3045again:
3046 min_key = 0;
3047 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003048 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003049 ret = log_dir_items(trans, root, inode, path,
3050 dst_path, key_type, min_key,
3051 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003052 if (ret)
3053 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003054 if (max_key == (u64)-1)
3055 break;
3056 min_key = max_key + 1;
3057 }
3058
3059 if (key_type == BTRFS_DIR_ITEM_KEY) {
3060 key_type = BTRFS_DIR_INDEX_KEY;
3061 goto again;
3062 }
3063 return 0;
3064}
3065
3066/*
3067 * a helper function to drop items from the log before we relog an
3068 * inode. max_key_type indicates the highest item type to remove.
3069 * This cannot be run for file data extents because it does not
3070 * free the extents they point to.
3071 */
3072static int drop_objectid_items(struct btrfs_trans_handle *trans,
3073 struct btrfs_root *log,
3074 struct btrfs_path *path,
3075 u64 objectid, int max_key_type)
3076{
3077 int ret;
3078 struct btrfs_key key;
3079 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003080 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003081
3082 key.objectid = objectid;
3083 key.type = max_key_type;
3084 key.offset = (u64)-1;
3085
Chris Masond3977122009-01-05 21:25:51 -05003086 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003087 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003088 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003089 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003090 break;
3091
3092 if (path->slots[0] == 0)
3093 break;
3094
3095 path->slots[0]--;
3096 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3097 path->slots[0]);
3098
3099 if (found_key.objectid != objectid)
3100 break;
3101
Josef Bacik18ec90d2012-09-28 11:56:28 -04003102 found_key.offset = 0;
3103 found_key.type = 0;
3104 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3105 &start_slot);
3106
3107 ret = btrfs_del_items(trans, log, path, start_slot,
3108 path->slots[0] - start_slot + 1);
3109 /*
3110 * If start slot isn't 0 then we don't need to re-search, we've
3111 * found the last guy with the objectid in this tree.
3112 */
3113 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003114 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003115 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003116 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003117 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003118 if (ret > 0)
3119 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003120 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003121}
3122
Josef Bacik94edf4a2012-09-25 14:56:25 -04003123static void fill_inode_item(struct btrfs_trans_handle *trans,
3124 struct extent_buffer *leaf,
3125 struct btrfs_inode_item *item,
3126 struct inode *inode, int log_inode_only)
3127{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003128 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003129
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003130 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003131
3132 if (log_inode_only) {
3133 /* set the generation to zero so the recover code
3134 * can tell the difference between an logging
3135 * just to say 'this inode exists' and a logging
3136 * to say 'update this inode with these values'
3137 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003138 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3139 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003140 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003141 btrfs_set_token_inode_generation(leaf, item,
3142 BTRFS_I(inode)->generation,
3143 &token);
3144 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003145 }
3146
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003147 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3148 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3149 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3150 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3151
3152 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3153 inode->i_atime.tv_sec, &token);
3154 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3155 inode->i_atime.tv_nsec, &token);
3156
3157 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3158 inode->i_mtime.tv_sec, &token);
3159 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3160 inode->i_mtime.tv_nsec, &token);
3161
3162 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3163 inode->i_ctime.tv_sec, &token);
3164 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3165 inode->i_ctime.tv_nsec, &token);
3166
3167 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3168 &token);
3169
3170 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3171 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3172 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3173 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3174 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003175}
3176
Josef Bacika95249b2012-10-11 16:17:34 -04003177static int log_inode_item(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *log, struct btrfs_path *path,
3179 struct inode *inode)
3180{
3181 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003182 int ret;
3183
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003184 ret = btrfs_insert_empty_item(trans, log, path,
3185 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003186 sizeof(*inode_item));
3187 if (ret && ret != -EEXIST)
3188 return ret;
3189 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3190 struct btrfs_inode_item);
3191 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3192 btrfs_release_path(path);
3193 return 0;
3194}
3195
Chris Mason31ff1cd2008-09-11 16:17:57 -04003196static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003197 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003198 struct btrfs_path *dst_path,
3199 struct extent_buffer *src,
3200 int start_slot, int nr, int inode_only)
3201{
3202 unsigned long src_offset;
3203 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003204 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003205 struct btrfs_file_extent_item *extent;
3206 struct btrfs_inode_item *inode_item;
3207 int ret;
3208 struct btrfs_key *ins_keys;
3209 u32 *ins_sizes;
3210 char *ins_data;
3211 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003212 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003213 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003214
3215 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003216
3217 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3218 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003219 if (!ins_data)
3220 return -ENOMEM;
3221
Chris Mason31ff1cd2008-09-11 16:17:57 -04003222 ins_sizes = (u32 *)ins_data;
3223 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3224
3225 for (i = 0; i < nr; i++) {
3226 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3227 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3228 }
3229 ret = btrfs_insert_empty_items(trans, log, dst_path,
3230 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003231 if (ret) {
3232 kfree(ins_data);
3233 return ret;
3234 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003235
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003236 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003237 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3238 dst_path->slots[0]);
3239
3240 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3241
Josef Bacik94edf4a2012-09-25 14:56:25 -04003242 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003243 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3244 dst_path->slots[0],
3245 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003246 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3247 inode, inode_only == LOG_INODE_EXISTS);
3248 } else {
3249 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3250 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003251 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003252
Chris Mason31ff1cd2008-09-11 16:17:57 -04003253 /* take a reference on file data extents so that truncates
3254 * or deletes of this inode don't have to relog the inode
3255 * again
3256 */
Liu Bod2794402012-08-29 01:07:56 -06003257 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3258 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003259 int found_type;
3260 extent = btrfs_item_ptr(src, start_slot + i,
3261 struct btrfs_file_extent_item);
3262
liubo8e531cd2011-05-06 10:36:09 +08003263 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3264 continue;
3265
Chris Mason31ff1cd2008-09-11 16:17:57 -04003266 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003267 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003268 u64 ds, dl, cs, cl;
3269 ds = btrfs_file_extent_disk_bytenr(src,
3270 extent);
3271 /* ds == 0 is a hole */
3272 if (ds == 0)
3273 continue;
3274
3275 dl = btrfs_file_extent_disk_num_bytes(src,
3276 extent);
3277 cs = btrfs_file_extent_offset(src, extent);
3278 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003279 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003280 if (btrfs_file_extent_compression(src,
3281 extent)) {
3282 cs = 0;
3283 cl = dl;
3284 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003285
3286 ret = btrfs_lookup_csums_range(
3287 log->fs_info->csum_root,
3288 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003289 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003290 if (ret) {
3291 btrfs_release_path(dst_path);
3292 kfree(ins_data);
3293 return ret;
3294 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003295 }
3296 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003297 }
3298
3299 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003300 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003301 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003302
3303 /*
3304 * we have to do this after the loop above to avoid changing the
3305 * log tree while trying to change the log tree.
3306 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003307 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003308 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003309 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3310 struct btrfs_ordered_sum,
3311 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003312 if (!ret)
3313 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003314 list_del(&sums->list);
3315 kfree(sums);
3316 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003317 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003318}
3319
Josef Bacik5dc562c2012-08-17 13:14:17 -04003320static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3321{
3322 struct extent_map *em1, *em2;
3323
3324 em1 = list_entry(a, struct extent_map, list);
3325 em2 = list_entry(b, struct extent_map, list);
3326
3327 if (em1->start < em2->start)
3328 return -1;
3329 else if (em1->start > em2->start)
3330 return 1;
3331 return 0;
3332}
3333
Josef Bacik5dc562c2012-08-17 13:14:17 -04003334static int log_one_extent(struct btrfs_trans_handle *trans,
3335 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003336 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003337{
3338 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003339 struct btrfs_file_extent_item *fi;
3340 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003341 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003342 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003343 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003344 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003345 u64 mod_start = em->mod_start;
3346 u64 mod_len = em->mod_len;
3347 u64 csum_offset;
3348 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003349 u64 extent_offset = em->start - em->orig_start;
3350 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003351 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003352 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003353 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003354
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003355 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3356 em->start + em->len, NULL, 0);
3357 if (ret)
3358 return ret;
3359
Josef Bacik70c8a912012-10-11 16:54:30 -04003360 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003361 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003362 key.objectid = btrfs_ino(inode);
3363 key.type = BTRFS_EXTENT_DATA_KEY;
3364 key.offset = em->start;
Josef Bacik70c8a912012-10-11 16:54:30 -04003365
3366 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003367 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003368 return ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003369 leaf = path->nodes[0];
3370 fi = btrfs_item_ptr(leaf, path->slots[0],
3371 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003372
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003373 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3374 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003375 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3376 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003377 btrfs_set_token_file_extent_type(leaf, fi,
3378 BTRFS_FILE_EXTENT_PREALLOC,
3379 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003380 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003381 btrfs_set_token_file_extent_type(leaf, fi,
3382 BTRFS_FILE_EXTENT_REG,
3383 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003384 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003385 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003386 }
3387
Josef Bacik70c8a912012-10-11 16:54:30 -04003388 block_len = max(em->block_len, em->orig_block_len);
3389 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003390 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3391 em->block_start,
3392 &token);
3393 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3394 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003395 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003396 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3397 em->block_start -
3398 extent_offset, &token);
3399 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3400 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003401 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003402 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3403 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3404 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003405 }
3406
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003407 btrfs_set_token_file_extent_offset(leaf, fi,
3408 em->start - em->orig_start,
3409 &token);
3410 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003411 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003412 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3413 &token);
3414 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3415 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003416 btrfs_mark_buffer_dirty(leaf);
3417
Josef Bacik70c8a912012-10-11 16:54:30 -04003418 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003419 if (ret) {
3420 return ret;
3421 }
3422
3423 if (skip_csum)
3424 return 0;
3425
Liu Bo192000d2013-01-06 03:38:22 +00003426 if (em->compress_type) {
3427 csum_offset = 0;
3428 csum_len = block_len;
3429 }
3430
Josef Bacik2ab28f32012-10-12 15:27:49 -04003431 /*
3432 * First check and see if our csums are on our outstanding ordered
3433 * extents.
3434 */
3435again:
3436 spin_lock_irq(&log->log_extents_lock[index]);
3437 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3438 struct btrfs_ordered_sum *sum;
3439
3440 if (!mod_len)
3441 break;
3442
3443 if (ordered->inode != inode)
3444 continue;
3445
3446 if (ordered->file_offset + ordered->len <= mod_start ||
3447 mod_start + mod_len <= ordered->file_offset)
3448 continue;
3449
3450 /*
3451 * We are going to copy all the csums on this ordered extent, so
3452 * go ahead and adjust mod_start and mod_len in case this
3453 * ordered extent has already been logged.
3454 */
3455 if (ordered->file_offset > mod_start) {
3456 if (ordered->file_offset + ordered->len >=
3457 mod_start + mod_len)
3458 mod_len = ordered->file_offset - mod_start;
3459 /*
3460 * If we have this case
3461 *
3462 * |--------- logged extent ---------|
3463 * |----- ordered extent ----|
3464 *
3465 * Just don't mess with mod_start and mod_len, we'll
3466 * just end up logging more csums than we need and it
3467 * will be ok.
3468 */
3469 } else {
3470 if (ordered->file_offset + ordered->len <
3471 mod_start + mod_len) {
3472 mod_len = (mod_start + mod_len) -
3473 (ordered->file_offset + ordered->len);
3474 mod_start = ordered->file_offset +
3475 ordered->len;
3476 } else {
3477 mod_len = 0;
3478 }
3479 }
3480
3481 /*
3482 * To keep us from looping for the above case of an ordered
3483 * extent that falls inside of the logged extent.
3484 */
3485 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3486 &ordered->flags))
3487 continue;
3488 atomic_inc(&ordered->refs);
3489 spin_unlock_irq(&log->log_extents_lock[index]);
3490 /*
3491 * we've dropped the lock, we must either break or
3492 * start over after this.
3493 */
3494
3495 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3496
3497 list_for_each_entry(sum, &ordered->list, list) {
3498 ret = btrfs_csum_file_blocks(trans, log, sum);
3499 if (ret) {
3500 btrfs_put_ordered_extent(ordered);
3501 goto unlocked;
3502 }
3503 }
3504 btrfs_put_ordered_extent(ordered);
3505 goto again;
3506
3507 }
3508 spin_unlock_irq(&log->log_extents_lock[index]);
3509unlocked:
3510
3511 if (!mod_len || ret)
3512 return ret;
3513
3514 csum_offset = mod_start - em->start;
3515 csum_len = mod_len;
3516
Josef Bacik70c8a912012-10-11 16:54:30 -04003517 /* block start is already adjusted for the file extent offset. */
3518 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3519 em->block_start + csum_offset,
3520 em->block_start + csum_offset +
3521 csum_len - 1, &ordered_sums, 0);
3522 if (ret)
3523 return ret;
3524
3525 while (!list_empty(&ordered_sums)) {
3526 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3527 struct btrfs_ordered_sum,
3528 list);
3529 if (!ret)
3530 ret = btrfs_csum_file_blocks(trans, log, sums);
3531 list_del(&sums->list);
3532 kfree(sums);
3533 }
3534
3535 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003536}
3537
3538static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3539 struct btrfs_root *root,
3540 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003541 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003542{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003543 struct extent_map *em, *n;
3544 struct list_head extents;
3545 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3546 u64 test_gen;
3547 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003548 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003549
3550 INIT_LIST_HEAD(&extents);
3551
Josef Bacik5dc562c2012-08-17 13:14:17 -04003552 write_lock(&tree->lock);
3553 test_gen = root->fs_info->last_trans_committed;
3554
3555 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3556 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003557
3558 /*
3559 * Just an arbitrary number, this can be really CPU intensive
3560 * once we start getting a lot of extents, and really once we
3561 * have a bunch of extents we just want to commit since it will
3562 * be faster.
3563 */
3564 if (++num > 32768) {
3565 list_del_init(&tree->modified_extents);
3566 ret = -EFBIG;
3567 goto process;
3568 }
3569
Josef Bacik5dc562c2012-08-17 13:14:17 -04003570 if (em->generation <= test_gen)
3571 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003572 /* Need a ref to keep it from getting evicted from cache */
3573 atomic_inc(&em->refs);
3574 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003575 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003576 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003577 }
3578
3579 list_sort(NULL, &extents, extent_cmp);
3580
Josef Bacik2ab28f32012-10-12 15:27:49 -04003581process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003582 while (!list_empty(&extents)) {
3583 em = list_entry(extents.next, struct extent_map, list);
3584
3585 list_del_init(&em->list);
3586
3587 /*
3588 * If we had an error we just need to delete everybody from our
3589 * private list.
3590 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003591 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003592 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003593 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003594 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003595 }
3596
3597 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003598
Josef Bacik70c8a912012-10-11 16:54:30 -04003599 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003600 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003601 clear_em_logging(tree, em);
3602 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003603 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003604 WARN_ON(!list_empty(&extents));
3605 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003606
Josef Bacik5dc562c2012-08-17 13:14:17 -04003607 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003608 return ret;
3609}
3610
Chris Masone02119d2008-09-05 16:13:11 -04003611/* log a single inode in the tree log.
3612 * At least one parent directory for this inode must exist in the tree
3613 * or be logged already.
3614 *
3615 * Any items from this inode changed by the current transaction are copied
3616 * to the log tree. An extra reference is taken on any extents in this
3617 * file, allowing us to avoid a whole pile of corner cases around logging
3618 * blocks that have been removed from the tree.
3619 *
3620 * See LOG_INODE_ALL and related defines for a description of what inode_only
3621 * does.
3622 *
3623 * This handles both files and directories.
3624 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003625static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003626 struct btrfs_root *root, struct inode *inode,
3627 int inode_only)
3628{
3629 struct btrfs_path *path;
3630 struct btrfs_path *dst_path;
3631 struct btrfs_key min_key;
3632 struct btrfs_key max_key;
3633 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003634 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003635 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003636 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003637 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003638 int ins_start_slot = 0;
3639 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003640 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003641 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003642
Chris Masone02119d2008-09-05 16:13:11 -04003643 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003644 if (!path)
3645 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003646 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003647 if (!dst_path) {
3648 btrfs_free_path(path);
3649 return -ENOMEM;
3650 }
Chris Masone02119d2008-09-05 16:13:11 -04003651
Li Zefan33345d012011-04-20 10:31:50 +08003652 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003653 min_key.type = BTRFS_INODE_ITEM_KEY;
3654 min_key.offset = 0;
3655
Li Zefan33345d012011-04-20 10:31:50 +08003656 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003657
Chris Mason12fcfd22009-03-24 10:24:20 -04003658
Josef Bacik5dc562c2012-08-17 13:14:17 -04003659 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003660 if (S_ISDIR(inode->i_mode) ||
3661 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3662 &BTRFS_I(inode)->runtime_flags) &&
3663 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003664 max_key.type = BTRFS_XATTR_ITEM_KEY;
3665 else
3666 max_key.type = (u8)-1;
3667 max_key.offset = (u64)-1;
3668
Josef Bacik94edf4a2012-09-25 14:56:25 -04003669 /* Only run delayed items if we are a dir or a new file */
3670 if (S_ISDIR(inode->i_mode) ||
3671 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3672 ret = btrfs_commit_inode_delayed_items(trans, inode);
3673 if (ret) {
3674 btrfs_free_path(path);
3675 btrfs_free_path(dst_path);
3676 return ret;
3677 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003678 }
3679
Chris Masone02119d2008-09-05 16:13:11 -04003680 mutex_lock(&BTRFS_I(inode)->log_mutex);
3681
Josef Bacik2ab28f32012-10-12 15:27:49 -04003682 btrfs_get_logged_extents(log, inode);
3683
Chris Masone02119d2008-09-05 16:13:11 -04003684 /*
3685 * a brute force approach to making sure we get the most uptodate
3686 * copies of everything.
3687 */
3688 if (S_ISDIR(inode->i_mode)) {
3689 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3690
3691 if (inode_only == LOG_INODE_EXISTS)
3692 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003693 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003694 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003695 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3696 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003697 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3698 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003699 ret = btrfs_truncate_inode_items(trans, log,
3700 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003701 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3702 &BTRFS_I(inode)->runtime_flags)) {
3703 if (inode_only == LOG_INODE_ALL)
3704 fast_search = true;
3705 max_key.type = BTRFS_XATTR_ITEM_KEY;
3706 ret = drop_objectid_items(trans, log, path, ino,
3707 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003708 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003709 if (inode_only == LOG_INODE_ALL)
3710 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003711 ret = log_inode_item(trans, log, dst_path, inode);
3712 if (ret) {
3713 err = ret;
3714 goto out_unlock;
3715 }
3716 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003717 }
Josef Bacika95249b2012-10-11 16:17:34 -04003718
Chris Masone02119d2008-09-05 16:13:11 -04003719 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003720 if (ret) {
3721 err = ret;
3722 goto out_unlock;
3723 }
Chris Masone02119d2008-09-05 16:13:11 -04003724 path->keep_locks = 1;
3725
Chris Masond3977122009-01-05 21:25:51 -05003726 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003727 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003728 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003729 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003730 if (ret != 0)
3731 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003732again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003733 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003734 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003735 break;
3736 if (min_key.type > max_key.type)
3737 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003738
Chris Masone02119d2008-09-05 16:13:11 -04003739 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003740 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3741 ins_nr++;
3742 goto next_slot;
3743 } else if (!ins_nr) {
3744 ins_start_slot = path->slots[0];
3745 ins_nr = 1;
3746 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003747 }
3748
Liu Bod2794402012-08-29 01:07:56 -06003749 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003750 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003751 if (ret) {
3752 err = ret;
3753 goto out_unlock;
3754 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003755 ins_nr = 1;
3756 ins_start_slot = path->slots[0];
3757next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003758
Chris Mason3a5f1d42008-09-11 15:53:37 -04003759 nritems = btrfs_header_nritems(path->nodes[0]);
3760 path->slots[0]++;
3761 if (path->slots[0] < nritems) {
3762 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3763 path->slots[0]);
3764 goto again;
3765 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003766 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003767 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003768 ins_start_slot,
3769 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003770 if (ret) {
3771 err = ret;
3772 goto out_unlock;
3773 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003774 ins_nr = 0;
3775 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003776 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003777
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003778 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003779 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003780 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003781 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003782 min_key.offset = 0;
3783 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003784 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003785 }
Chris Masone02119d2008-09-05 16:13:11 -04003786 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003787 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003788 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003789 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003790 if (ret) {
3791 err = ret;
3792 goto out_unlock;
3793 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003794 ins_nr = 0;
3795 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003796
Josef Bacika95249b2012-10-11 16:17:34 -04003797log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003798 btrfs_release_path(path);
3799 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003800 if (fast_search) {
Josef Bacik70c8a912012-10-11 16:54:30 -04003801 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003802 if (ret) {
3803 err = ret;
3804 goto out_unlock;
3805 }
Liu Bo06d3d222012-08-27 10:52:19 -06003806 } else {
3807 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3808 struct extent_map *em, *n;
3809
Miao Xiebbe14262012-11-01 07:34:54 +00003810 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003811 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3812 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003813 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003814 }
3815
Chris Mason9623f9a2008-09-11 17:42:42 -04003816 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003817 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003818 if (ret) {
3819 err = ret;
3820 goto out_unlock;
3821 }
Chris Masone02119d2008-09-05 16:13:11 -04003822 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003823 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003824 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003825out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003826 if (err)
3827 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003828 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3829
3830 btrfs_free_path(path);
3831 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003832 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003833}
3834
Chris Mason12fcfd22009-03-24 10:24:20 -04003835/*
3836 * follow the dentry parent pointers up the chain and see if any
3837 * of the directories in it require a full commit before they can
3838 * be logged. Returns zero if nothing special needs to be done or 1 if
3839 * a full commit is required.
3840 */
3841static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3842 struct inode *inode,
3843 struct dentry *parent,
3844 struct super_block *sb,
3845 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003846{
Chris Mason12fcfd22009-03-24 10:24:20 -04003847 int ret = 0;
3848 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003849 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04003850 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04003851
Chris Masonaf4176b2009-03-24 10:24:31 -04003852 /*
3853 * for regular files, if its inode is already on disk, we don't
3854 * have to worry about the parents at all. This is because
3855 * we can use the last_unlink_trans field to record renames
3856 * and other fun in this file.
3857 */
3858 if (S_ISREG(inode->i_mode) &&
3859 BTRFS_I(inode)->generation <= last_committed &&
3860 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3861 goto out;
3862
Chris Mason12fcfd22009-03-24 10:24:20 -04003863 if (!S_ISDIR(inode->i_mode)) {
3864 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3865 goto out;
3866 inode = parent->d_inode;
3867 }
3868
3869 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04003870 /*
3871 * If we are logging a directory then we start with our inode,
3872 * not our parents inode, so we need to skipp setting the
3873 * logged_trans so that further down in the log code we don't
3874 * think this inode has already been logged.
3875 */
3876 if (inode != orig_inode)
3877 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04003878 smp_mb();
3879
3880 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3881 root = BTRFS_I(inode)->root;
3882
3883 /*
3884 * make sure any commits to the log are forced
3885 * to be full commits
3886 */
3887 root->fs_info->last_trans_log_full_commit =
3888 trans->transid;
3889 ret = 1;
3890 break;
3891 }
3892
3893 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3894 break;
3895
Yan, Zheng76dda932009-09-21 16:00:26 -04003896 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003897 break;
3898
Josef Bacik6a912212010-11-20 09:48:00 +00003899 parent = dget_parent(parent);
3900 dput(old_parent);
3901 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003902 inode = parent->d_inode;
3903
3904 }
Josef Bacik6a912212010-11-20 09:48:00 +00003905 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003906out:
Chris Masone02119d2008-09-05 16:13:11 -04003907 return ret;
3908}
3909
3910/*
3911 * helper function around btrfs_log_inode to make sure newly created
3912 * parent directories also end up in the log. A minimal inode and backref
3913 * only logging is done of any parent directories that are older than
3914 * the last committed transaction
3915 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003916static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3917 struct btrfs_root *root, struct inode *inode,
3918 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003919{
Chris Mason12fcfd22009-03-24 10:24:20 -04003920 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003921 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003922 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003923 int ret = 0;
3924 u64 last_committed = root->fs_info->last_trans_committed;
3925
3926 sb = inode->i_sb;
3927
Sage Weil3a5e1402009-04-02 16:49:40 -04003928 if (btrfs_test_opt(root, NOTREELOG)) {
3929 ret = 1;
3930 goto end_no_trans;
3931 }
3932
Chris Mason12fcfd22009-03-24 10:24:20 -04003933 if (root->fs_info->last_trans_log_full_commit >
3934 root->fs_info->last_trans_committed) {
3935 ret = 1;
3936 goto end_no_trans;
3937 }
3938
Yan, Zheng76dda932009-09-21 16:00:26 -04003939 if (root != BTRFS_I(inode)->root ||
3940 btrfs_root_refs(&root->root_item) == 0) {
3941 ret = 1;
3942 goto end_no_trans;
3943 }
3944
Chris Mason12fcfd22009-03-24 10:24:20 -04003945 ret = check_parent_dirs_for_sync(trans, inode, parent,
3946 sb, last_committed);
3947 if (ret)
3948 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003949
Josef Bacik22ee6982012-05-29 16:57:49 -04003950 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003951 ret = BTRFS_NO_LOG_SYNC;
3952 goto end_no_trans;
3953 }
3954
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003955 ret = start_log_trans(trans, root);
3956 if (ret)
3957 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003958
3959 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003960 if (ret)
3961 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003962
Chris Masonaf4176b2009-03-24 10:24:31 -04003963 /*
3964 * for regular files, if its inode is already on disk, we don't
3965 * have to worry about the parents at all. This is because
3966 * we can use the last_unlink_trans field to record renames
3967 * and other fun in this file.
3968 */
3969 if (S_ISREG(inode->i_mode) &&
3970 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003971 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3972 ret = 0;
3973 goto end_trans;
3974 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003975
3976 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003977 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003978 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003979 break;
3980
Chris Mason12fcfd22009-03-24 10:24:20 -04003981 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003982 if (root != BTRFS_I(inode)->root)
3983 break;
3984
Chris Mason12fcfd22009-03-24 10:24:20 -04003985 if (BTRFS_I(inode)->generation >
3986 root->fs_info->last_trans_committed) {
3987 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003988 if (ret)
3989 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003990 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003991 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003992 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003993
Josef Bacik6a912212010-11-20 09:48:00 +00003994 parent = dget_parent(parent);
3995 dput(old_parent);
3996 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003997 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003998 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003999end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004000 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004001 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004002 root->fs_info->last_trans_log_full_commit = trans->transid;
4003 ret = 1;
4004 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004005 btrfs_end_log_trans(root);
4006end_no_trans:
4007 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004008}
4009
4010/*
4011 * it is not safe to log dentry if the chunk root has added new
4012 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4013 * If this returns 1, you must commit the transaction to safely get your
4014 * data on disk.
4015 */
4016int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root, struct dentry *dentry)
4018{
Josef Bacik6a912212010-11-20 09:48:00 +00004019 struct dentry *parent = dget_parent(dentry);
4020 int ret;
4021
4022 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
4023 dput(parent);
4024
4025 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004026}
4027
4028/*
4029 * should be called during mount to recover any replay any log trees
4030 * from the FS
4031 */
4032int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4033{
4034 int ret;
4035 struct btrfs_path *path;
4036 struct btrfs_trans_handle *trans;
4037 struct btrfs_key key;
4038 struct btrfs_key found_key;
4039 struct btrfs_key tmp_key;
4040 struct btrfs_root *log;
4041 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4042 struct walk_control wc = {
4043 .process_func = process_one_buffer,
4044 .stage = 0,
4045 };
4046
Chris Masone02119d2008-09-05 16:13:11 -04004047 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004048 if (!path)
4049 return -ENOMEM;
4050
4051 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004052
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004053 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004054 if (IS_ERR(trans)) {
4055 ret = PTR_ERR(trans);
4056 goto error;
4057 }
Chris Masone02119d2008-09-05 16:13:11 -04004058
4059 wc.trans = trans;
4060 wc.pin = 1;
4061
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004062 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004063 if (ret) {
4064 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4065 "recovering log root tree.");
4066 goto error;
4067 }
Chris Masone02119d2008-09-05 16:13:11 -04004068
4069again:
4070 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4071 key.offset = (u64)-1;
4072 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4073
Chris Masond3977122009-01-05 21:25:51 -05004074 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004075 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004076
4077 if (ret < 0) {
4078 btrfs_error(fs_info, ret,
4079 "Couldn't find tree log root.");
4080 goto error;
4081 }
Chris Masone02119d2008-09-05 16:13:11 -04004082 if (ret > 0) {
4083 if (path->slots[0] == 0)
4084 break;
4085 path->slots[0]--;
4086 }
4087 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4088 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004089 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004090 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4091 break;
4092
Miao Xiecb517ea2013-05-15 07:48:19 +00004093 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (IS_ERR(log)) {
4095 ret = PTR_ERR(log);
4096 btrfs_error(fs_info, ret,
4097 "Couldn't read tree log root.");
4098 goto error;
4099 }
Chris Masone02119d2008-09-05 16:13:11 -04004100
4101 tmp_key.objectid = found_key.offset;
4102 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4103 tmp_key.offset = (u64)-1;
4104
4105 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 if (IS_ERR(wc.replay_dest)) {
4107 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004108 free_extent_buffer(log->node);
4109 free_extent_buffer(log->commit_root);
4110 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 btrfs_error(fs_info, ret, "Couldn't read target root "
4112 "for tree log recovery.");
4113 goto error;
4114 }
Chris Masone02119d2008-09-05 16:13:11 -04004115
Yan Zheng07d400a2009-01-06 11:42:00 -05004116 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004117 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004118 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004119
Josef Bacikb50c6e22013-04-25 15:55:30 -04004120 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004121 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4122 path);
Chris Masone02119d2008-09-05 16:13:11 -04004123 }
Chris Masone02119d2008-09-05 16:13:11 -04004124
4125 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004126 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004127 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004128 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004129 kfree(log);
4130
Josef Bacikb50c6e22013-04-25 15:55:30 -04004131 if (ret)
4132 goto error;
4133
Chris Masone02119d2008-09-05 16:13:11 -04004134 if (found_key.offset == 0)
4135 break;
4136 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004137 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004138
4139 /* step one is to pin it all, step two is to replay just inodes */
4140 if (wc.pin) {
4141 wc.pin = 0;
4142 wc.process_func = replay_one_buffer;
4143 wc.stage = LOG_WALK_REPLAY_INODES;
4144 goto again;
4145 }
4146 /* step three is to replay everything */
4147 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4148 wc.stage++;
4149 goto again;
4150 }
4151
4152 btrfs_free_path(path);
4153
Josef Bacikabefa552013-04-24 16:40:05 -04004154 /* step 4: commit the transaction, which also unpins the blocks */
4155 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4156 if (ret)
4157 return ret;
4158
Chris Masone02119d2008-09-05 16:13:11 -04004159 free_extent_buffer(log_root_tree->node);
4160 log_root_tree->log_root = NULL;
4161 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004162 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163
Josef Bacikabefa552013-04-24 16:40:05 -04004164 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004166 if (wc.trans)
4167 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004168 btrfs_free_path(path);
4169 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004170}
Chris Mason12fcfd22009-03-24 10:24:20 -04004171
4172/*
4173 * there are some corner cases where we want to force a full
4174 * commit instead of allowing a directory to be logged.
4175 *
4176 * They revolve around files there were unlinked from the directory, and
4177 * this function updates the parent directory so that a full commit is
4178 * properly done if it is fsync'd later after the unlinks are done.
4179 */
4180void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4181 struct inode *dir, struct inode *inode,
4182 int for_rename)
4183{
4184 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004185 * when we're logging a file, if it hasn't been renamed
4186 * or unlinked, and its inode is fully committed on disk,
4187 * we don't have to worry about walking up the directory chain
4188 * to log its parents.
4189 *
4190 * So, we use the last_unlink_trans field to put this transid
4191 * into the file. When the file is logged we check it and
4192 * don't log the parents if the file is fully on disk.
4193 */
4194 if (S_ISREG(inode->i_mode))
4195 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4196
4197 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004198 * if this directory was already logged any new
4199 * names for this file/dir will get recorded
4200 */
4201 smp_mb();
4202 if (BTRFS_I(dir)->logged_trans == trans->transid)
4203 return;
4204
4205 /*
4206 * if the inode we're about to unlink was logged,
4207 * the log will be properly updated for any new names
4208 */
4209 if (BTRFS_I(inode)->logged_trans == trans->transid)
4210 return;
4211
4212 /*
4213 * when renaming files across directories, if the directory
4214 * there we're unlinking from gets fsync'd later on, there's
4215 * no way to find the destination directory later and fsync it
4216 * properly. So, we have to be conservative and force commits
4217 * so the new name gets discovered.
4218 */
4219 if (for_rename)
4220 goto record;
4221
4222 /* we can safely do the unlink without any special recording */
4223 return;
4224
4225record:
4226 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4227}
4228
4229/*
4230 * Call this after adding a new name for a file and it will properly
4231 * update the log to reflect the new name.
4232 *
4233 * It will return zero if all goes well, and it will return 1 if a
4234 * full transaction commit is required.
4235 */
4236int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4237 struct inode *inode, struct inode *old_dir,
4238 struct dentry *parent)
4239{
4240 struct btrfs_root * root = BTRFS_I(inode)->root;
4241
4242 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004243 * this will force the logging code to walk the dentry chain
4244 * up for the file
4245 */
4246 if (S_ISREG(inode->i_mode))
4247 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4248
4249 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004250 * if this inode hasn't been logged and directory we're renaming it
4251 * from hasn't been logged, we don't need to log it
4252 */
4253 if (BTRFS_I(inode)->logged_trans <=
4254 root->fs_info->last_trans_committed &&
4255 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4256 root->fs_info->last_trans_committed))
4257 return 0;
4258
4259 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4260}
4261