blob: 849b72985c7414f3f756c0ba05c02b9de9cfeed8 [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"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050030#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070031#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040032
33/* magic values for the inode_only field in btrfs_log_inode:
34 *
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
37 * during log replay
38 */
39#define LOG_INODE_ALL 0
40#define LOG_INODE_EXISTS 1
41
42/*
Chris Mason12fcfd22009-03-24 10:24:20 -040043 * directory trouble cases
44 *
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
49 *
50 * mkdir foo/some_dir
51 * normal commit
52 * rename foo/some_dir foo2/some_dir
53 * mkdir foo/some_dir
54 * fsync foo/some_dir/some_file
55 *
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
59 *
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
62 *
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
66 *
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
69 *
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
72 *
73 * mkdir f1/foo
74 * normal commit
75 * rm -rf f1/foo
76 * fsync(f1)
77 *
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
82 * ugly details.
83 */
84
85/*
Chris Masone02119d2008-09-05 16:13:11 -040086 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
90 *
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
93 */
94#define LOG_WALK_PIN_ONLY 0
95#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040096#define LOG_WALK_REPLAY_DIR_INDEX 2
97#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040098
Chris Mason12fcfd22009-03-24 10:24:20 -040099static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -0400100 struct btrfs_root *root, struct inode *inode,
101 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500102static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400105static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_root *log,
108 struct btrfs_path *path,
109 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400110
111/*
112 * tree logging is a special write ahead log used to make sure that
113 * fsyncs and O_SYNCs can happen without doing full tree commits.
114 *
115 * Full tree commits are expensive because they require commonly
116 * modified blocks to be recowed, creating many dirty pages in the
117 * extent tree an 4x-6x higher write load than ext3.
118 *
119 * Instead of doing a tree commit on every fsync, we use the
120 * key ranges and transaction ids to find items for a given file or directory
121 * that have changed in this transaction. Those items are copied into
122 * a special tree (one per subvolume root), that tree is written to disk
123 * and then the fsync is considered complete.
124 *
125 * After a crash, items are copied out of the log-tree back into the
126 * subvolume tree. Any file data extents found are recorded in the extent
127 * allocation tree, and the log-tree freed.
128 *
129 * The log tree is read three times, once to pin down all the extents it is
130 * using in ram and once, once to create all the inodes logged in the tree
131 * and once to do all the other items.
132 */
133
134/*
Chris Masone02119d2008-09-05 16:13:11 -0400135 * start a sub transaction and setup the log tree
136 * this increments the log tree writer count to make the people
137 * syncing the tree wait for us to finish
138 */
139static int start_log_trans(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root)
141{
142 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400143 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400147 if (!root->log_start_pid) {
148 root->log_start_pid = current->pid;
149 root->log_multiple_pids = false;
150 } else if (root->log_start_pid != current->pid) {
151 root->log_multiple_pids = true;
152 }
153
Miao Xie2ecb7922012-09-06 04:04:27 -0600154 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500155 atomic_inc(&root->log_writers);
156 mutex_unlock(&root->log_mutex);
157 return 0;
158 }
Josef Bacikff782e02009-10-08 15:30:04 -0400159 root->log_multiple_pids = false;
160 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400161 mutex_lock(&root->fs_info->tree_log_mutex);
162 if (!root->fs_info->log_root_tree) {
163 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400167 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400168 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400169 if (ret)
170 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400171 }
Chris Masone02119d2008-09-05 16:13:11 -0400172 mutex_unlock(&root->fs_info->tree_log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -0600173 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500174 atomic_inc(&root->log_writers);
175 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400176 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400177}
178
179/*
180 * returns 0 if there was a log transaction running and we were able
181 * to join, or returns -ENOENT if there were not transactions
182 * in progress
183 */
184static int join_running_log_trans(struct btrfs_root *root)
185{
186 int ret = -ENOENT;
187
188 smp_mb();
189 if (!root->log_root)
190 return -ENOENT;
191
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 if (root->log_root) {
194 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500195 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400196 }
Yan Zheng7237f182009-01-21 12:54:03 -0500197 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400198 return ret;
199}
200
201/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400202 * This either makes the current running log transaction wait
203 * until you call btrfs_end_log_trans() or it makes any future
204 * log transactions wait until you call btrfs_end_log_trans()
205 */
206int btrfs_pin_log_trans(struct btrfs_root *root)
207{
208 int ret = -ENOENT;
209
210 mutex_lock(&root->log_mutex);
211 atomic_inc(&root->log_writers);
212 mutex_unlock(&root->log_mutex);
213 return ret;
214}
215
216/*
Chris Masone02119d2008-09-05 16:13:11 -0400217 * indicate we're done making changes to the log tree
218 * and wake up anyone waiting to do a sync
219 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100220void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400221{
Yan Zheng7237f182009-01-21 12:54:03 -0500222 if (atomic_dec_and_test(&root->log_writers)) {
223 smp_mb();
224 if (waitqueue_active(&root->log_writer_wait))
225 wake_up(&root->log_writer_wait);
226 }
Chris Masone02119d2008-09-05 16:13:11 -0400227}
228
229
230/*
231 * the walk control struct is used to pass state down the chain when
232 * processing the log tree. The stage field tells us which part
233 * of the log tree processing we are currently doing. The others
234 * are state fields used for that specific part
235 */
236struct walk_control {
237 /* should we free the extent on disk when done? This is used
238 * at transaction commit time while freeing a log tree
239 */
240 int free;
241
242 /* should we write out the extent buffer? This is used
243 * while flushing the log tree to disk during a sync
244 */
245 int write;
246
247 /* should we wait for the extent buffer io to finish? Also used
248 * while flushing the log tree to disk for a sync
249 */
250 int wait;
251
252 /* pin only walk, we record which extents on disk belong to the
253 * log trees
254 */
255 int pin;
256
257 /* what stage of the replay code we're currently in */
258 int stage;
259
260 /* the root we are currently replaying */
261 struct btrfs_root *replay_dest;
262
263 /* the trans handle for the current replay */
264 struct btrfs_trans_handle *trans;
265
266 /* the function that gets used to process blocks we find in the
267 * tree. Note the extent_buffer might not be up to date when it is
268 * passed in, and it must be checked or read if you need the data
269 * inside it
270 */
271 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
272 struct walk_control *wc, u64 gen);
273};
274
275/*
276 * process_func used to pin down extents, write them or wait on them
277 */
278static int process_one_buffer(struct btrfs_root *log,
279 struct extent_buffer *eb,
280 struct walk_control *wc, u64 gen)
281{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400282 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400283
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400284 /*
285 * If this fs is mixed then we need to be able to process the leaves to
286 * pin down any logged extents, so we have to read the block.
287 */
288 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
289 ret = btrfs_read_buffer(eb, gen);
290 if (ret)
291 return ret;
292 }
293
Josef Bacikb50c6e22013-04-25 15:55:30 -0400294 if (wc->pin)
295 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
296 eb->start, eb->len);
297
298 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400299 if (wc->pin && btrfs_header_level(eb) == 0)
300 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400301 if (wc->write)
302 btrfs_write_tree_block(eb);
303 if (wc->wait)
304 btrfs_wait_tree_block_writeback(eb);
305 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400306 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400307}
308
309/*
310 * Item overwrite used by replay and tree logging. eb, slot and key all refer
311 * to the src data we are copying out.
312 *
313 * root is the tree we are copying into, and path is a scratch
314 * path for use in this function (it should be released on entry and
315 * will be released on exit).
316 *
317 * If the key is already in the destination tree the existing item is
318 * overwritten. If the existing item isn't big enough, it is extended.
319 * If it is too large, it is truncated.
320 *
321 * If the key isn't in the destination yet, a new item is inserted.
322 */
323static noinline int overwrite_item(struct btrfs_trans_handle *trans,
324 struct btrfs_root *root,
325 struct btrfs_path *path,
326 struct extent_buffer *eb, int slot,
327 struct btrfs_key *key)
328{
329 int ret;
330 u32 item_size;
331 u64 saved_i_size = 0;
332 int save_old_i_size = 0;
333 unsigned long src_ptr;
334 unsigned long dst_ptr;
335 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000336 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400337
338 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
339 overwrite_root = 1;
340
341 item_size = btrfs_item_size_nr(eb, slot);
342 src_ptr = btrfs_item_ptr_offset(eb, slot);
343
344 /* look for the key in the destination tree */
345 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000346 if (ret < 0)
347 return ret;
348
Chris Masone02119d2008-09-05 16:13:11 -0400349 if (ret == 0) {
350 char *src_copy;
351 char *dst_copy;
352 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
353 path->slots[0]);
354 if (dst_size != item_size)
355 goto insert;
356
357 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200358 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400359 return 0;
360 }
361 dst_copy = kmalloc(item_size, GFP_NOFS);
362 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000363 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200364 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000365 kfree(dst_copy);
366 kfree(src_copy);
367 return -ENOMEM;
368 }
Chris Masone02119d2008-09-05 16:13:11 -0400369
370 read_extent_buffer(eb, src_copy, src_ptr, item_size);
371
372 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
373 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
374 item_size);
375 ret = memcmp(dst_copy, src_copy, item_size);
376
377 kfree(dst_copy);
378 kfree(src_copy);
379 /*
380 * they have the same contents, just return, this saves
381 * us from cowing blocks in the destination tree and doing
382 * extra writes that may not have been done by a previous
383 * sync
384 */
385 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200386 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400387 return 0;
388 }
389
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000390 /*
391 * We need to load the old nbytes into the inode so when we
392 * replay the extents we've logged we get the right nbytes.
393 */
394 if (inode_item) {
395 struct btrfs_inode_item *item;
396 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400397 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000398
399 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
400 struct btrfs_inode_item);
401 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
402 item = btrfs_item_ptr(eb, slot,
403 struct btrfs_inode_item);
404 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400405
406 /*
407 * If this is a directory we need to reset the i_size to
408 * 0 so that we can set it up properly when replaying
409 * the rest of the items in this log.
410 */
411 mode = btrfs_inode_mode(eb, item);
412 if (S_ISDIR(mode))
413 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000414 }
415 } else if (inode_item) {
416 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400417 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000418
419 /*
420 * New inode, set nbytes to 0 so that the nbytes comes out
421 * properly when we replay the extents.
422 */
423 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
424 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400425
426 /*
427 * If this is a directory we need to reset the i_size to 0 so
428 * that we can set it up properly when replaying the rest of
429 * the items in this log.
430 */
431 mode = btrfs_inode_mode(eb, item);
432 if (S_ISDIR(mode))
433 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400434 }
435insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200436 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400437 /* try to insert the key into the destination tree */
438 ret = btrfs_insert_empty_item(trans, root, path,
439 key, item_size);
440
441 /* make sure any existing item is the correct size */
442 if (ret == -EEXIST) {
443 u32 found_size;
444 found_size = btrfs_item_size_nr(path->nodes[0],
445 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100446 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000447 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100448 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000449 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100450 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400451 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400452 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400453 }
454 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
455 path->slots[0]);
456
457 /* don't overwrite an existing inode if the generation number
458 * was logged as zero. This is done when the tree logging code
459 * is just logging an inode to make sure it exists after recovery.
460 *
461 * Also, don't overwrite i_size on directories during replay.
462 * log replay inserts and removes directory items based on the
463 * state of the tree found in the subvolume, and i_size is modified
464 * as it goes
465 */
466 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
467 struct btrfs_inode_item *src_item;
468 struct btrfs_inode_item *dst_item;
469
470 src_item = (struct btrfs_inode_item *)src_ptr;
471 dst_item = (struct btrfs_inode_item *)dst_ptr;
472
473 if (btrfs_inode_generation(eb, src_item) == 0)
474 goto no_copy;
475
476 if (overwrite_root &&
477 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
478 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
479 save_old_i_size = 1;
480 saved_i_size = btrfs_inode_size(path->nodes[0],
481 dst_item);
482 }
483 }
484
485 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
486 src_ptr, item_size);
487
488 if (save_old_i_size) {
489 struct btrfs_inode_item *dst_item;
490 dst_item = (struct btrfs_inode_item *)dst_ptr;
491 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
492 }
493
494 /* make sure the generation is filled in */
495 if (key->type == BTRFS_INODE_ITEM_KEY) {
496 struct btrfs_inode_item *dst_item;
497 dst_item = (struct btrfs_inode_item *)dst_ptr;
498 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
499 btrfs_set_inode_generation(path->nodes[0], dst_item,
500 trans->transid);
501 }
502 }
503no_copy:
504 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200505 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400506 return 0;
507}
508
509/*
510 * simple helper to read an inode off the disk from a given root
511 * This can only be called for subvolume roots and not for the log
512 */
513static noinline struct inode *read_one_inode(struct btrfs_root *root,
514 u64 objectid)
515{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400517 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400518
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400519 key.objectid = objectid;
520 key.type = BTRFS_INODE_ITEM_KEY;
521 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000522 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400523 if (IS_ERR(inode)) {
524 inode = NULL;
525 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400526 iput(inode);
527 inode = NULL;
528 }
529 return inode;
530}
531
532/* replays a single extent in 'eb' at 'slot' with 'key' into the
533 * subvolume 'root'. path is released on entry and should be released
534 * on exit.
535 *
536 * extents in the log tree have not been allocated out of the extent
537 * tree yet. So, this completes the allocation, taking a reference
538 * as required if the extent already exists or creating a new extent
539 * if it isn't in the extent allocation tree yet.
540 *
541 * The extent is inserted into the file, dropping any existing extents
542 * from the file that overlap the new one.
543 */
544static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root,
546 struct btrfs_path *path,
547 struct extent_buffer *eb, int slot,
548 struct btrfs_key *key)
549{
550 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400551 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400552 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000553 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400554 struct btrfs_file_extent_item *item;
555 struct inode *inode = NULL;
556 unsigned long size;
557 int ret = 0;
558
559 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
560 found_type = btrfs_file_extent_type(eb, item);
561
Yan Zhengd899e052008-10-30 14:25:28 -0400562 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000563 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
564 nbytes = btrfs_file_extent_num_bytes(eb, item);
565 extent_end = start + nbytes;
566
567 /*
568 * We don't add to the inodes nbytes if we are prealloc or a
569 * hole.
570 */
571 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
572 nbytes = 0;
573 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 size = btrfs_file_extent_inline_len(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000575 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000576 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400577 } else {
578 ret = 0;
579 goto out;
580 }
581
582 inode = read_one_inode(root, key->objectid);
583 if (!inode) {
584 ret = -EIO;
585 goto out;
586 }
587
588 /*
589 * first check to see if we already have this extent in the
590 * file. This must be done before the btrfs_drop_extents run
591 * so we don't try to drop this extent.
592 */
Li Zefan33345d012011-04-20 10:31:50 +0800593 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400594 start, 0);
595
Yan Zhengd899e052008-10-30 14:25:28 -0400596 if (ret == 0 &&
597 (found_type == BTRFS_FILE_EXTENT_REG ||
598 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400599 struct btrfs_file_extent_item cmp1;
600 struct btrfs_file_extent_item cmp2;
601 struct btrfs_file_extent_item *existing;
602 struct extent_buffer *leaf;
603
604 leaf = path->nodes[0];
605 existing = btrfs_item_ptr(leaf, path->slots[0],
606 struct btrfs_file_extent_item);
607
608 read_extent_buffer(eb, &cmp1, (unsigned long)item,
609 sizeof(cmp1));
610 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
611 sizeof(cmp2));
612
613 /*
614 * we already have a pointer to this exact extent,
615 * we don't have to do anything
616 */
617 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200618 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400619 goto out;
620 }
621 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200622 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400623
624 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400625 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400626 if (ret)
627 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400628
Yan Zheng07d400a2009-01-06 11:42:00 -0500629 if (found_type == BTRFS_FILE_EXTENT_REG ||
630 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400631 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500632 unsigned long dest_offset;
633 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400634
Yan Zheng07d400a2009-01-06 11:42:00 -0500635 ret = btrfs_insert_empty_item(trans, root, path, key,
636 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400637 if (ret)
638 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500639 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
640 path->slots[0]);
641 copy_extent_buffer(path->nodes[0], eb, dest_offset,
642 (unsigned long)item, sizeof(*item));
643
644 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
645 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
646 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400647 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500648
649 if (ins.objectid > 0) {
650 u64 csum_start;
651 u64 csum_end;
652 LIST_HEAD(ordered_sums);
653 /*
654 * is this extent already allocated in the extent
655 * allocation tree? If so, just add a reference
656 */
657 ret = btrfs_lookup_extent(root, ins.objectid,
658 ins.offset);
659 if (ret == 0) {
660 ret = btrfs_inc_extent_ref(trans, root,
661 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200663 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400664 if (ret)
665 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 } else {
667 /*
668 * insert the extent pointer in the extent
669 * allocation tree
670 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671 ret = btrfs_alloc_logged_file_extent(trans,
672 root, root->root_key.objectid,
673 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400674 if (ret)
675 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500676 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200677 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500678
679 if (btrfs_file_extent_compression(eb, item)) {
680 csum_start = ins.objectid;
681 csum_end = csum_start + ins.offset;
682 } else {
683 csum_start = ins.objectid +
684 btrfs_file_extent_offset(eb, item);
685 csum_end = csum_start +
686 btrfs_file_extent_num_bytes(eb, item);
687 }
688
689 ret = btrfs_lookup_csums_range(root->log_root,
690 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100691 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400692 if (ret)
693 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500694 while (!list_empty(&ordered_sums)) {
695 struct btrfs_ordered_sum *sums;
696 sums = list_entry(ordered_sums.next,
697 struct btrfs_ordered_sum,
698 list);
Josef Bacik36508602013-04-25 16:23:32 -0400699 if (!ret)
700 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500701 root->fs_info->csum_root,
702 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500703 list_del(&sums->list);
704 kfree(sums);
705 }
Josef Bacik36508602013-04-25 16:23:32 -0400706 if (ret)
707 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200709 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 }
711 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
712 /* inline extents are easy, we just overwrite them */
713 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400714 if (ret)
715 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500716 }
717
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000718 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600719 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400720out:
721 if (inode)
722 iput(inode);
723 return ret;
724}
725
726/*
727 * when cleaning up conflicts between the directory names in the
728 * subvolume, directory names in the log and directory names in the
729 * inode back references, we may have to unlink inodes from directories.
730 *
731 * This is a helper function to do the unlink of a specific directory
732 * item
733 */
734static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root,
736 struct btrfs_path *path,
737 struct inode *dir,
738 struct btrfs_dir_item *di)
739{
740 struct inode *inode;
741 char *name;
742 int name_len;
743 struct extent_buffer *leaf;
744 struct btrfs_key location;
745 int ret;
746
747 leaf = path->nodes[0];
748
749 btrfs_dir_item_key_to_cpu(leaf, di, &location);
750 name_len = btrfs_dir_name_len(leaf, di);
751 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000752 if (!name)
753 return -ENOMEM;
754
Chris Masone02119d2008-09-05 16:13:11 -0400755 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200756 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400757
758 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000759 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400760 ret = -EIO;
761 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000762 }
Chris Masone02119d2008-09-05 16:13:11 -0400763
Yan Zhengec051c02009-01-05 15:43:42 -0500764 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400765 if (ret)
766 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400767
Chris Masone02119d2008-09-05 16:13:11 -0400768 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400769 if (ret)
770 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100771 else
772 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400773out:
774 kfree(name);
775 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400776 return ret;
777}
778
779/*
780 * helper function to see if a given name and sequence number found
781 * in an inode back reference are already in a directory and correctly
782 * point to this inode
783 */
784static noinline int inode_in_dir(struct btrfs_root *root,
785 struct btrfs_path *path,
786 u64 dirid, u64 objectid, u64 index,
787 const char *name, int name_len)
788{
789 struct btrfs_dir_item *di;
790 struct btrfs_key location;
791 int match = 0;
792
793 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
794 index, name, name_len, 0);
795 if (di && !IS_ERR(di)) {
796 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
797 if (location.objectid != objectid)
798 goto out;
799 } else
800 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200801 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400802
803 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
804 if (di && !IS_ERR(di)) {
805 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
806 if (location.objectid != objectid)
807 goto out;
808 } else
809 goto out;
810 match = 1;
811out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200812 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400813 return match;
814}
815
816/*
817 * helper function to check a log tree for a named back reference in
818 * an inode. This is used to decide if a back reference that is
819 * found in the subvolume conflicts with what we find in the log.
820 *
821 * inode backreferences may have multiple refs in a single item,
822 * during replay we process one reference at a time, and we don't
823 * want to delete valid links to a file from the subvolume if that
824 * link is also in the log.
825 */
826static noinline int backref_in_log(struct btrfs_root *log,
827 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700828 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400829 char *name, int namelen)
830{
831 struct btrfs_path *path;
832 struct btrfs_inode_ref *ref;
833 unsigned long ptr;
834 unsigned long ptr_end;
835 unsigned long name_ptr;
836 int found_name_len;
837 int item_size;
838 int ret;
839 int match = 0;
840
841 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000842 if (!path)
843 return -ENOMEM;
844
Chris Masone02119d2008-09-05 16:13:11 -0400845 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
846 if (ret != 0)
847 goto out;
848
Chris Masone02119d2008-09-05 16:13:11 -0400849 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700850
851 if (key->type == BTRFS_INODE_EXTREF_KEY) {
852 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
853 name, namelen, NULL))
854 match = 1;
855
856 goto out;
857 }
858
859 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400860 ptr_end = ptr + item_size;
861 while (ptr < ptr_end) {
862 ref = (struct btrfs_inode_ref *)ptr;
863 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
864 if (found_name_len == namelen) {
865 name_ptr = (unsigned long)(ref + 1);
866 ret = memcmp_extent_buffer(path->nodes[0], name,
867 name_ptr, namelen);
868 if (ret == 0) {
869 match = 1;
870 goto out;
871 }
872 }
873 ptr = (unsigned long)(ref + 1) + found_name_len;
874 }
875out:
876 btrfs_free_path(path);
877 return match;
878}
879
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700880static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
881 struct btrfs_root *root,
882 struct btrfs_path *path,
883 struct btrfs_root *log_root,
884 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700885 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700886 u64 inode_objectid, u64 parent_objectid,
887 u64 ref_index, char *name, int namelen,
888 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700889{
890 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700891 char *victim_name;
892 int victim_name_len;
893 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700894 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700895 struct btrfs_key search_key;
896 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700897
Mark Fashehf1863732012-08-08 11:32:27 -0700898again:
899 /* Search old style refs */
900 search_key.objectid = inode_objectid;
901 search_key.type = BTRFS_INODE_REF_KEY;
902 search_key.offset = parent_objectid;
903 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700904 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700905 struct btrfs_inode_ref *victim_ref;
906 unsigned long ptr;
907 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700908
909 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700910
911 /* are we trying to overwrite a back ref for the root directory
912 * if so, just jump out, we're done
913 */
Mark Fashehf1863732012-08-08 11:32:27 -0700914 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700915 return 1;
916
917 /* check all the names in this back reference to see
918 * if they are in the log. if so, we allow them to stay
919 * otherwise they must be unlinked as a conflict
920 */
921 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
922 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
923 while (ptr < ptr_end) {
924 victim_ref = (struct btrfs_inode_ref *)ptr;
925 victim_name_len = btrfs_inode_ref_name_len(leaf,
926 victim_ref);
927 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400928 if (!victim_name)
929 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700930
931 read_extent_buffer(leaf, victim_name,
932 (unsigned long)(victim_ref + 1),
933 victim_name_len);
934
Mark Fashehf1863732012-08-08 11:32:27 -0700935 if (!backref_in_log(log_root, &search_key,
936 parent_objectid,
937 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700938 victim_name_len)) {
939 btrfs_inc_nlink(inode);
940 btrfs_release_path(path);
941
942 ret = btrfs_unlink_inode(trans, root, dir,
943 inode, victim_name,
944 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700945 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400946 if (ret)
947 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100948 ret = btrfs_run_delayed_items(trans, root);
949 if (ret)
950 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700951 *search_done = 1;
952 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700953 }
954 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700955
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700956 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
957 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700958
959 /*
960 * NOTE: we have searched root tree and checked the
961 * coresponding ref, it does not need to check again.
962 */
963 *search_done = 1;
964 }
965 btrfs_release_path(path);
966
Mark Fashehf1863732012-08-08 11:32:27 -0700967 /* Same search but for extended refs */
968 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
969 inode_objectid, parent_objectid, 0,
970 0);
971 if (!IS_ERR_OR_NULL(extref)) {
972 u32 item_size;
973 u32 cur_offset = 0;
974 unsigned long base;
975 struct inode *victim_parent;
976
977 leaf = path->nodes[0];
978
979 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
980 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
981
982 while (cur_offset < item_size) {
983 extref = (struct btrfs_inode_extref *)base + cur_offset;
984
985 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
986
987 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
988 goto next;
989
990 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400991 if (!victim_name)
992 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -0700993 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
994 victim_name_len);
995
996 search_key.objectid = inode_objectid;
997 search_key.type = BTRFS_INODE_EXTREF_KEY;
998 search_key.offset = btrfs_extref_hash(parent_objectid,
999 victim_name,
1000 victim_name_len);
1001 ret = 0;
1002 if (!backref_in_log(log_root, &search_key,
1003 parent_objectid, victim_name,
1004 victim_name_len)) {
1005 ret = -ENOENT;
1006 victim_parent = read_one_inode(root,
1007 parent_objectid);
1008 if (victim_parent) {
1009 btrfs_inc_nlink(inode);
1010 btrfs_release_path(path);
1011
1012 ret = btrfs_unlink_inode(trans, root,
1013 victim_parent,
1014 inode,
1015 victim_name,
1016 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001017 if (!ret)
1018 ret = btrfs_run_delayed_items(
1019 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001020 }
Mark Fashehf1863732012-08-08 11:32:27 -07001021 iput(victim_parent);
1022 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001023 if (ret)
1024 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001025 *search_done = 1;
1026 goto again;
1027 }
1028 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001029 if (ret)
1030 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001031next:
1032 cur_offset += victim_name_len + sizeof(*extref);
1033 }
1034 *search_done = 1;
1035 }
1036 btrfs_release_path(path);
1037
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001038 /* look for a conflicting sequence number */
1039 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001040 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001041 if (di && !IS_ERR(di)) {
1042 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001043 if (ret)
1044 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001045 }
1046 btrfs_release_path(path);
1047
1048 /* look for a conflicing name */
1049 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1050 name, namelen, 0);
1051 if (di && !IS_ERR(di)) {
1052 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001053 if (ret)
1054 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 }
1056 btrfs_release_path(path);
1057
1058 return 0;
1059}
Chris Masone02119d2008-09-05 16:13:11 -04001060
Mark Fashehf1863732012-08-08 11:32:27 -07001061static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1062 u32 *namelen, char **name, u64 *index,
1063 u64 *parent_objectid)
1064{
1065 struct btrfs_inode_extref *extref;
1066
1067 extref = (struct btrfs_inode_extref *)ref_ptr;
1068
1069 *namelen = btrfs_inode_extref_name_len(eb, extref);
1070 *name = kmalloc(*namelen, GFP_NOFS);
1071 if (*name == NULL)
1072 return -ENOMEM;
1073
1074 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1075 *namelen);
1076
1077 *index = btrfs_inode_extref_index(eb, extref);
1078 if (parent_objectid)
1079 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1080
1081 return 0;
1082}
1083
1084static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1085 u32 *namelen, char **name, u64 *index)
1086{
1087 struct btrfs_inode_ref *ref;
1088
1089 ref = (struct btrfs_inode_ref *)ref_ptr;
1090
1091 *namelen = btrfs_inode_ref_name_len(eb, ref);
1092 *name = kmalloc(*namelen, GFP_NOFS);
1093 if (*name == NULL)
1094 return -ENOMEM;
1095
1096 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1097
1098 *index = btrfs_inode_ref_index(eb, ref);
1099
1100 return 0;
1101}
1102
Chris Masone02119d2008-09-05 16:13:11 -04001103/*
1104 * replay one inode back reference item found in the log tree.
1105 * eb, slot and key refer to the buffer and key found in the log tree.
1106 * root is the destination we are replaying into, and path is for temp
1107 * use by this function. (it should be released on return).
1108 */
1109static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root,
1111 struct btrfs_root *log,
1112 struct btrfs_path *path,
1113 struct extent_buffer *eb, int slot,
1114 struct btrfs_key *key)
1115{
liubo34f3e4f2011-08-06 08:35:23 +00001116 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001117 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -04001118 unsigned long ref_ptr;
1119 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +00001120 char *name;
1121 int namelen;
1122 int ret;
liuboc622ae62011-03-26 08:01:12 -04001123 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001124 int log_ref_ver = 0;
1125 u64 parent_objectid;
1126 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001127 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001128 int ref_struct_size;
1129
1130 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1131 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1132
1133 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1134 struct btrfs_inode_extref *r;
1135
1136 ref_struct_size = sizeof(struct btrfs_inode_extref);
1137 log_ref_ver = 1;
1138 r = (struct btrfs_inode_extref *)ref_ptr;
1139 parent_objectid = btrfs_inode_extref_parent(eb, r);
1140 } else {
1141 ref_struct_size = sizeof(struct btrfs_inode_ref);
1142 parent_objectid = key->offset;
1143 }
1144 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001145
Chris Masone02119d2008-09-05 16:13:11 -04001146 /*
1147 * it is possible that we didn't log all the parent directories
1148 * for a given inode. If we don't find the dir, just don't
1149 * copy the back ref in. The link count fixup code will take
1150 * care of the rest
1151 */
Mark Fashehf1863732012-08-08 11:32:27 -07001152 dir = read_one_inode(root, parent_objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001153 if (!dir)
1154 return -ENOENT;
1155
Mark Fashehf1863732012-08-08 11:32:27 -07001156 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001157 if (!inode) {
1158 iput(dir);
1159 return -EIO;
1160 }
Chris Masone02119d2008-09-05 16:13:11 -04001161
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001162 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001163 if (log_ref_ver) {
1164 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1165 &ref_index, &parent_objectid);
1166 /*
1167 * parent object can change from one array
1168 * item to another.
1169 */
1170 if (!dir)
1171 dir = read_one_inode(root, parent_objectid);
1172 if (!dir)
1173 return -ENOENT;
1174 } else {
1175 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1176 &ref_index);
1177 }
1178 if (ret)
1179 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001180
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001181 /* if we already have a perfect match, we're done */
1182 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001183 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001184 /*
1185 * look for a conflicting back reference in the
1186 * metadata. if we find one we have to unlink that name
1187 * of the file before we add our new link. Later on, we
1188 * overwrite any existing back reference, and we don't
1189 * want to create dangling pointers in the directory.
1190 */
Chris Masone02119d2008-09-05 16:13:11 -04001191
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001192 if (!search_done) {
1193 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001194 dir, inode, eb,
1195 inode_objectid,
1196 parent_objectid,
1197 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001198 &search_done);
Josef Bacik36508602013-04-25 16:23:32 -04001199 if (ret == 1) {
1200 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001201 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001202 }
1203 if (ret)
1204 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001205 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001206
1207 /* insert our name */
1208 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001209 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001210 if (ret)
1211 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001212
1213 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001214 }
liuboc622ae62011-03-26 08:01:12 -04001215
Mark Fashehf1863732012-08-08 11:32:27 -07001216 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001217 kfree(name);
Mark Fashehf1863732012-08-08 11:32:27 -07001218 if (log_ref_ver) {
1219 iput(dir);
1220 dir = NULL;
1221 }
Chris Masone02119d2008-09-05 16:13:11 -04001222 }
Chris Masone02119d2008-09-05 16:13:11 -04001223
1224 /* finally write the back reference in the inode */
1225 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001226out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001227 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001228 iput(dir);
1229 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001230 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001231}
1232
Yan, Zhengc71bf092009-11-12 09:34:40 +00001233static int insert_orphan_item(struct btrfs_trans_handle *trans,
1234 struct btrfs_root *root, u64 offset)
1235{
1236 int ret;
1237 ret = btrfs_find_orphan_item(root, offset);
1238 if (ret > 0)
1239 ret = btrfs_insert_orphan_item(trans, root, offset);
1240 return ret;
1241}
1242
Mark Fashehf1863732012-08-08 11:32:27 -07001243static int count_inode_extrefs(struct btrfs_root *root,
1244 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001245{
Mark Fashehf1863732012-08-08 11:32:27 -07001246 int ret = 0;
1247 int name_len;
1248 unsigned int nlink = 0;
1249 u32 item_size;
1250 u32 cur_offset = 0;
1251 u64 inode_objectid = btrfs_ino(inode);
1252 u64 offset = 0;
1253 unsigned long ptr;
1254 struct btrfs_inode_extref *extref;
1255 struct extent_buffer *leaf;
1256
1257 while (1) {
1258 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1259 &extref, &offset);
1260 if (ret)
1261 break;
1262
1263 leaf = path->nodes[0];
1264 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1265 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1266
1267 while (cur_offset < item_size) {
1268 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1269 name_len = btrfs_inode_extref_name_len(leaf, extref);
1270
1271 nlink++;
1272
1273 cur_offset += name_len + sizeof(*extref);
1274 }
1275
1276 offset++;
1277 btrfs_release_path(path);
1278 }
1279 btrfs_release_path(path);
1280
1281 if (ret < 0)
1282 return ret;
1283 return nlink;
1284}
1285
1286static int count_inode_refs(struct btrfs_root *root,
1287 struct inode *inode, struct btrfs_path *path)
1288{
Chris Masone02119d2008-09-05 16:13:11 -04001289 int ret;
1290 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001291 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001292 unsigned long ptr;
1293 unsigned long ptr_end;
1294 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001295 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001296
Li Zefan33345d012011-04-20 10:31:50 +08001297 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001298 key.type = BTRFS_INODE_REF_KEY;
1299 key.offset = (u64)-1;
1300
Chris Masond3977122009-01-05 21:25:51 -05001301 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1303 if (ret < 0)
1304 break;
1305 if (ret > 0) {
1306 if (path->slots[0] == 0)
1307 break;
1308 path->slots[0]--;
1309 }
1310 btrfs_item_key_to_cpu(path->nodes[0], &key,
1311 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001313 key.type != BTRFS_INODE_REF_KEY)
1314 break;
1315 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1316 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1317 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001318 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001319 struct btrfs_inode_ref *ref;
1320
1321 ref = (struct btrfs_inode_ref *)ptr;
1322 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1323 ref);
1324 ptr = (unsigned long)(ref + 1) + name_len;
1325 nlink++;
1326 }
1327
1328 if (key.offset == 0)
1329 break;
1330 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001331 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001332 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001333 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001334
1335 return nlink;
1336}
1337
1338/*
1339 * There are a few corners where the link count of the file can't
1340 * be properly maintained during replay. So, instead of adding
1341 * lots of complexity to the log code, we just scan the backrefs
1342 * for any file that has been through replay.
1343 *
1344 * The scan will update the link count on the inode to reflect the
1345 * number of back refs found. If it goes down to zero, the iput
1346 * will free the inode.
1347 */
1348static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root,
1350 struct inode *inode)
1351{
1352 struct btrfs_path *path;
1353 int ret;
1354 u64 nlink = 0;
1355 u64 ino = btrfs_ino(inode);
1356
1357 path = btrfs_alloc_path();
1358 if (!path)
1359 return -ENOMEM;
1360
1361 ret = count_inode_refs(root, inode, path);
1362 if (ret < 0)
1363 goto out;
1364
1365 nlink = ret;
1366
1367 ret = count_inode_extrefs(root, inode, path);
1368 if (ret == -ENOENT)
1369 ret = 0;
1370
1371 if (ret < 0)
1372 goto out;
1373
1374 nlink += ret;
1375
1376 ret = 0;
1377
Chris Masone02119d2008-09-05 16:13:11 -04001378 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001379 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001380 btrfs_update_inode(trans, root, inode);
1381 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001382 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001383
Yan, Zhengc71bf092009-11-12 09:34:40 +00001384 if (inode->i_nlink == 0) {
1385 if (S_ISDIR(inode->i_mode)) {
1386 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001387 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001388 if (ret)
1389 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001390 }
Li Zefan33345d012011-04-20 10:31:50 +08001391 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001392 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001393
Mark Fashehf1863732012-08-08 11:32:27 -07001394out:
1395 btrfs_free_path(path);
1396 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001397}
1398
1399static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root,
1401 struct btrfs_path *path)
1402{
1403 int ret;
1404 struct btrfs_key key;
1405 struct inode *inode;
1406
1407 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1408 key.type = BTRFS_ORPHAN_ITEM_KEY;
1409 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001410 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001411 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1412 if (ret < 0)
1413 break;
1414
1415 if (ret == 1) {
1416 if (path->slots[0] == 0)
1417 break;
1418 path->slots[0]--;
1419 }
1420
1421 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1422 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1423 key.type != BTRFS_ORPHAN_ITEM_KEY)
1424 break;
1425
1426 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001427 if (ret)
1428 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001429
David Sterbab3b4aa72011-04-21 01:20:15 +02001430 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001431 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001432 if (!inode)
1433 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001434
1435 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001436 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001437 if (ret)
1438 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001439
Chris Mason12fcfd22009-03-24 10:24:20 -04001440 /*
1441 * fixup on a directory may create new entries,
1442 * make sure we always look for the highset possible
1443 * offset
1444 */
1445 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001446 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001447 ret = 0;
1448out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001449 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001450 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001451}
1452
1453
1454/*
1455 * record a given inode in the fixup dir so we can check its link
1456 * count when replay is done. The link count is incremented here
1457 * so the inode won't go away until we check it
1458 */
1459static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1460 struct btrfs_root *root,
1461 struct btrfs_path *path,
1462 u64 objectid)
1463{
1464 struct btrfs_key key;
1465 int ret = 0;
1466 struct inode *inode;
1467
1468 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001469 if (!inode)
1470 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001471
1472 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1473 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1474 key.offset = objectid;
1475
1476 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1477
David Sterbab3b4aa72011-04-21 01:20:15 +02001478 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001479 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001480 if (!inode->i_nlink)
1481 set_nlink(inode, 1);
1482 else
1483 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001484 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001485 } else if (ret == -EEXIST) {
1486 ret = 0;
1487 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001488 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001489 }
1490 iput(inode);
1491
1492 return ret;
1493}
1494
1495/*
1496 * when replaying the log for a directory, we only insert names
1497 * for inodes that actually exist. This means an fsync on a directory
1498 * does not implicitly fsync all the new files in it
1499 */
1500static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *root,
1502 struct btrfs_path *path,
1503 u64 dirid, u64 index,
1504 char *name, int name_len, u8 type,
1505 struct btrfs_key *location)
1506{
1507 struct inode *inode;
1508 struct inode *dir;
1509 int ret;
1510
1511 inode = read_one_inode(root, location->objectid);
1512 if (!inode)
1513 return -ENOENT;
1514
1515 dir = read_one_inode(root, dirid);
1516 if (!dir) {
1517 iput(inode);
1518 return -EIO;
1519 }
Josef Bacikd5554382013-09-11 14:17:00 -04001520
Chris Masone02119d2008-09-05 16:13:11 -04001521 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1522
1523 /* FIXME, put inode into FIXUP list */
1524
1525 iput(inode);
1526 iput(dir);
1527 return ret;
1528}
1529
1530/*
1531 * take a single entry in a log directory item and replay it into
1532 * the subvolume.
1533 *
1534 * if a conflicting item exists in the subdirectory already,
1535 * the inode it points to is unlinked and put into the link count
1536 * fix up tree.
1537 *
1538 * If a name from the log points to a file or directory that does
1539 * not exist in the FS, it is skipped. fsyncs on directories
1540 * do not force down inodes inside that directory, just changes to the
1541 * names or unlinks in a directory.
1542 */
1543static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
1545 struct btrfs_path *path,
1546 struct extent_buffer *eb,
1547 struct btrfs_dir_item *di,
1548 struct btrfs_key *key)
1549{
1550 char *name;
1551 int name_len;
1552 struct btrfs_dir_item *dst_di;
1553 struct btrfs_key found_key;
1554 struct btrfs_key log_key;
1555 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001556 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001557 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001558 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001559 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001560
1561 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001562 if (!dir)
1563 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001564
1565 name_len = btrfs_dir_name_len(eb, di);
1566 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001567 if (!name) {
1568 ret = -ENOMEM;
1569 goto out;
1570 }
liubo2a29edc2011-01-26 06:22:08 +00001571
Chris Masone02119d2008-09-05 16:13:11 -04001572 log_type = btrfs_dir_type(eb, di);
1573 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1574 name_len);
1575
1576 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001577 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1578 if (exists == 0)
1579 exists = 1;
1580 else
1581 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001582 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001583
Chris Masone02119d2008-09-05 16:13:11 -04001584 if (key->type == BTRFS_DIR_ITEM_KEY) {
1585 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1586 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001587 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001588 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1589 key->objectid,
1590 key->offset, name,
1591 name_len, 1);
1592 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001593 /* Corruption */
1594 ret = -EINVAL;
1595 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001596 }
David Sterbac7040052011-04-19 18:00:01 +02001597 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001598 /* we need a sequence number to insert, so we only
1599 * do inserts for the BTRFS_DIR_INDEX_KEY types
1600 */
1601 if (key->type != BTRFS_DIR_INDEX_KEY)
1602 goto out;
1603 goto insert;
1604 }
1605
1606 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1607 /* the existing item matches the logged item */
1608 if (found_key.objectid == log_key.objectid &&
1609 found_key.type == log_key.type &&
1610 found_key.offset == log_key.offset &&
1611 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1612 goto out;
1613 }
1614
1615 /*
1616 * don't drop the conflicting directory entry if the inode
1617 * for the new entry doesn't exist
1618 */
Chris Mason4bef0842008-09-08 11:18:08 -04001619 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001620 goto out;
1621
Chris Masone02119d2008-09-05 16:13:11 -04001622 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001623 if (ret)
1624 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001625
1626 if (key->type == BTRFS_DIR_INDEX_KEY)
1627 goto insert;
1628out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001629 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001630 if (!ret && update_size) {
1631 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1632 ret = btrfs_update_inode(trans, root, dir);
1633 }
Chris Masone02119d2008-09-05 16:13:11 -04001634 kfree(name);
1635 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001636 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001637
1638insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001639 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001640 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1641 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001642 if (ret && ret != -ENOENT)
1643 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001644 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001645 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001646 goto out;
1647}
1648
1649/*
1650 * find all the names in a directory item and reconcile them into
1651 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1652 * one name in a directory item, but the same code gets used for
1653 * both directory index types
1654 */
1655static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1656 struct btrfs_root *root,
1657 struct btrfs_path *path,
1658 struct extent_buffer *eb, int slot,
1659 struct btrfs_key *key)
1660{
1661 int ret;
1662 u32 item_size = btrfs_item_size_nr(eb, slot);
1663 struct btrfs_dir_item *di;
1664 int name_len;
1665 unsigned long ptr;
1666 unsigned long ptr_end;
1667
1668 ptr = btrfs_item_ptr_offset(eb, slot);
1669 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001670 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001671 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001672 if (verify_dir_item(root, eb, di))
1673 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001674 name_len = btrfs_dir_name_len(eb, di);
1675 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001676 if (ret)
1677 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001678 ptr = (unsigned long)(di + 1);
1679 ptr += name_len;
1680 }
1681 return 0;
1682}
1683
1684/*
1685 * directory replay has two parts. There are the standard directory
1686 * items in the log copied from the subvolume, and range items
1687 * created in the log while the subvolume was logged.
1688 *
1689 * The range items tell us which parts of the key space the log
1690 * is authoritative for. During replay, if a key in the subvolume
1691 * directory is in a logged range item, but not actually in the log
1692 * that means it was deleted from the directory before the fsync
1693 * and should be removed.
1694 */
1695static noinline int find_dir_range(struct btrfs_root *root,
1696 struct btrfs_path *path,
1697 u64 dirid, int key_type,
1698 u64 *start_ret, u64 *end_ret)
1699{
1700 struct btrfs_key key;
1701 u64 found_end;
1702 struct btrfs_dir_log_item *item;
1703 int ret;
1704 int nritems;
1705
1706 if (*start_ret == (u64)-1)
1707 return 1;
1708
1709 key.objectid = dirid;
1710 key.type = key_type;
1711 key.offset = *start_ret;
1712
1713 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1714 if (ret < 0)
1715 goto out;
1716 if (ret > 0) {
1717 if (path->slots[0] == 0)
1718 goto out;
1719 path->slots[0]--;
1720 }
1721 if (ret != 0)
1722 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1723
1724 if (key.type != key_type || key.objectid != dirid) {
1725 ret = 1;
1726 goto next;
1727 }
1728 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1729 struct btrfs_dir_log_item);
1730 found_end = btrfs_dir_log_end(path->nodes[0], item);
1731
1732 if (*start_ret >= key.offset && *start_ret <= found_end) {
1733 ret = 0;
1734 *start_ret = key.offset;
1735 *end_ret = found_end;
1736 goto out;
1737 }
1738 ret = 1;
1739next:
1740 /* check the next slot in the tree to see if it is a valid item */
1741 nritems = btrfs_header_nritems(path->nodes[0]);
1742 if (path->slots[0] >= nritems) {
1743 ret = btrfs_next_leaf(root, path);
1744 if (ret)
1745 goto out;
1746 } else {
1747 path->slots[0]++;
1748 }
1749
1750 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1751
1752 if (key.type != key_type || key.objectid != dirid) {
1753 ret = 1;
1754 goto out;
1755 }
1756 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1757 struct btrfs_dir_log_item);
1758 found_end = btrfs_dir_log_end(path->nodes[0], item);
1759 *start_ret = key.offset;
1760 *end_ret = found_end;
1761 ret = 0;
1762out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001763 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001764 return ret;
1765}
1766
1767/*
1768 * this looks for a given directory item in the log. If the directory
1769 * item is not in the log, the item is removed and the inode it points
1770 * to is unlinked
1771 */
1772static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1773 struct btrfs_root *root,
1774 struct btrfs_root *log,
1775 struct btrfs_path *path,
1776 struct btrfs_path *log_path,
1777 struct inode *dir,
1778 struct btrfs_key *dir_key)
1779{
1780 int ret;
1781 struct extent_buffer *eb;
1782 int slot;
1783 u32 item_size;
1784 struct btrfs_dir_item *di;
1785 struct btrfs_dir_item *log_di;
1786 int name_len;
1787 unsigned long ptr;
1788 unsigned long ptr_end;
1789 char *name;
1790 struct inode *inode;
1791 struct btrfs_key location;
1792
1793again:
1794 eb = path->nodes[0];
1795 slot = path->slots[0];
1796 item_size = btrfs_item_size_nr(eb, slot);
1797 ptr = btrfs_item_ptr_offset(eb, slot);
1798 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001799 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001800 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001801 if (verify_dir_item(root, eb, di)) {
1802 ret = -EIO;
1803 goto out;
1804 }
1805
Chris Masone02119d2008-09-05 16:13:11 -04001806 name_len = btrfs_dir_name_len(eb, di);
1807 name = kmalloc(name_len, GFP_NOFS);
1808 if (!name) {
1809 ret = -ENOMEM;
1810 goto out;
1811 }
1812 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1813 name_len);
1814 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001815 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001816 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1817 dir_key->objectid,
1818 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001819 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001820 log_di = btrfs_lookup_dir_index_item(trans, log,
1821 log_path,
1822 dir_key->objectid,
1823 dir_key->offset,
1824 name, name_len, 0);
1825 }
David Sterbac7040052011-04-19 18:00:01 +02001826 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001827 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001828 btrfs_release_path(path);
1829 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001830 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001831 if (!inode) {
1832 kfree(name);
1833 return -EIO;
1834 }
Chris Masone02119d2008-09-05 16:13:11 -04001835
1836 ret = link_to_fixup_dir(trans, root,
1837 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001838 if (ret) {
1839 kfree(name);
1840 iput(inode);
1841 goto out;
1842 }
1843
Chris Masone02119d2008-09-05 16:13:11 -04001844 btrfs_inc_nlink(inode);
1845 ret = btrfs_unlink_inode(trans, root, dir, inode,
1846 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001847 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001848 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001849 kfree(name);
1850 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001851 if (ret)
1852 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001853
1854 /* there might still be more names under this key
1855 * check and repeat if required
1856 */
1857 ret = btrfs_search_slot(NULL, root, dir_key, path,
1858 0, 0);
1859 if (ret == 0)
1860 goto again;
1861 ret = 0;
1862 goto out;
1863 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001864 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001865 kfree(name);
1866
1867 ptr = (unsigned long)(di + 1);
1868 ptr += name_len;
1869 }
1870 ret = 0;
1871out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001872 btrfs_release_path(path);
1873 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001874 return ret;
1875}
1876
1877/*
1878 * deletion replay happens before we copy any new directory items
1879 * out of the log or out of backreferences from inodes. It
1880 * scans the log to find ranges of keys that log is authoritative for,
1881 * and then scans the directory to find items in those ranges that are
1882 * not present in the log.
1883 *
1884 * Anything we don't find in the log is unlinked and removed from the
1885 * directory.
1886 */
1887static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1888 struct btrfs_root *root,
1889 struct btrfs_root *log,
1890 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001891 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001892{
1893 u64 range_start;
1894 u64 range_end;
1895 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1896 int ret = 0;
1897 struct btrfs_key dir_key;
1898 struct btrfs_key found_key;
1899 struct btrfs_path *log_path;
1900 struct inode *dir;
1901
1902 dir_key.objectid = dirid;
1903 dir_key.type = BTRFS_DIR_ITEM_KEY;
1904 log_path = btrfs_alloc_path();
1905 if (!log_path)
1906 return -ENOMEM;
1907
1908 dir = read_one_inode(root, dirid);
1909 /* it isn't an error if the inode isn't there, that can happen
1910 * because we replay the deletes before we copy in the inode item
1911 * from the log
1912 */
1913 if (!dir) {
1914 btrfs_free_path(log_path);
1915 return 0;
1916 }
1917again:
1918 range_start = 0;
1919 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001920 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001921 if (del_all)
1922 range_end = (u64)-1;
1923 else {
1924 ret = find_dir_range(log, path, dirid, key_type,
1925 &range_start, &range_end);
1926 if (ret != 0)
1927 break;
1928 }
Chris Masone02119d2008-09-05 16:13:11 -04001929
1930 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001931 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001932 int nritems;
1933 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1934 0, 0);
1935 if (ret < 0)
1936 goto out;
1937
1938 nritems = btrfs_header_nritems(path->nodes[0]);
1939 if (path->slots[0] >= nritems) {
1940 ret = btrfs_next_leaf(root, path);
1941 if (ret)
1942 break;
1943 }
1944 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1945 path->slots[0]);
1946 if (found_key.objectid != dirid ||
1947 found_key.type != dir_key.type)
1948 goto next_type;
1949
1950 if (found_key.offset > range_end)
1951 break;
1952
1953 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001954 log_path, dir,
1955 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001956 if (ret)
1957 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001958 if (found_key.offset == (u64)-1)
1959 break;
1960 dir_key.offset = found_key.offset + 1;
1961 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001962 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001963 if (range_end == (u64)-1)
1964 break;
1965 range_start = range_end + 1;
1966 }
1967
1968next_type:
1969 ret = 0;
1970 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1971 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1972 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001973 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001974 goto again;
1975 }
1976out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001977 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001978 btrfs_free_path(log_path);
1979 iput(dir);
1980 return ret;
1981}
1982
1983/*
1984 * the process_func used to replay items from the log tree. This
1985 * gets called in two different stages. The first stage just looks
1986 * for inodes and makes sure they are all copied into the subvolume.
1987 *
1988 * The second stage copies all the other item types from the log into
1989 * the subvolume. The two stage approach is slower, but gets rid of
1990 * lots of complexity around inodes referencing other inodes that exist
1991 * only in the log (references come from either directory items or inode
1992 * back refs).
1993 */
1994static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1995 struct walk_control *wc, u64 gen)
1996{
1997 int nritems;
1998 struct btrfs_path *path;
1999 struct btrfs_root *root = wc->replay_dest;
2000 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002001 int level;
2002 int i;
2003 int ret;
2004
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002005 ret = btrfs_read_buffer(eb, gen);
2006 if (ret)
2007 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002008
2009 level = btrfs_header_level(eb);
2010
2011 if (level != 0)
2012 return 0;
2013
2014 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002015 if (!path)
2016 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002017
2018 nritems = btrfs_header_nritems(eb);
2019 for (i = 0; i < nritems; i++) {
2020 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002021
2022 /* inode keys are done during the first stage */
2023 if (key.type == BTRFS_INODE_ITEM_KEY &&
2024 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002025 struct btrfs_inode_item *inode_item;
2026 u32 mode;
2027
2028 inode_item = btrfs_item_ptr(eb, i,
2029 struct btrfs_inode_item);
2030 mode = btrfs_inode_mode(eb, inode_item);
2031 if (S_ISDIR(mode)) {
2032 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002033 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002034 if (ret)
2035 break;
Chris Masone02119d2008-09-05 16:13:11 -04002036 }
2037 ret = overwrite_item(wc->trans, root, path,
2038 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002039 if (ret)
2040 break;
Chris Masone02119d2008-09-05 16:13:11 -04002041
Yan, Zhengc71bf092009-11-12 09:34:40 +00002042 /* for regular files, make sure corresponding
2043 * orhpan item exist. extents past the new EOF
2044 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002045 */
2046 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002047 ret = insert_orphan_item(wc->trans, root,
2048 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002049 if (ret)
2050 break;
Chris Masone02119d2008-09-05 16:13:11 -04002051 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002052
Chris Masone02119d2008-09-05 16:13:11 -04002053 ret = link_to_fixup_dir(wc->trans, root,
2054 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002055 if (ret)
2056 break;
Chris Masone02119d2008-09-05 16:13:11 -04002057 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002058
2059 if (key.type == BTRFS_DIR_INDEX_KEY &&
2060 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2061 ret = replay_one_dir_item(wc->trans, root, path,
2062 eb, i, &key);
2063 if (ret)
2064 break;
2065 }
2066
Chris Masone02119d2008-09-05 16:13:11 -04002067 if (wc->stage < LOG_WALK_REPLAY_ALL)
2068 continue;
2069
2070 /* these keys are simply copied */
2071 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2072 ret = overwrite_item(wc->trans, root, path,
2073 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002074 if (ret)
2075 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002076 } else if (key.type == BTRFS_INODE_REF_KEY ||
2077 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002078 ret = add_inode_ref(wc->trans, root, log, path,
2079 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002080 if (ret && ret != -ENOENT)
2081 break;
2082 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002083 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2084 ret = replay_one_extent(wc->trans, root, path,
2085 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002086 if (ret)
2087 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002088 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002089 ret = replay_one_dir_item(wc->trans, root, path,
2090 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002091 if (ret)
2092 break;
Chris Masone02119d2008-09-05 16:13:11 -04002093 }
2094 }
2095 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002096 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002097}
2098
Chris Masond3977122009-01-05 21:25:51 -05002099static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002100 struct btrfs_root *root,
2101 struct btrfs_path *path, int *level,
2102 struct walk_control *wc)
2103{
2104 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002105 u64 bytenr;
2106 u64 ptr_gen;
2107 struct extent_buffer *next;
2108 struct extent_buffer *cur;
2109 struct extent_buffer *parent;
2110 u32 blocksize;
2111 int ret = 0;
2112
2113 WARN_ON(*level < 0);
2114 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2115
Chris Masond3977122009-01-05 21:25:51 -05002116 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002117 WARN_ON(*level < 0);
2118 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2119 cur = path->nodes[*level];
2120
2121 if (btrfs_header_level(cur) != *level)
2122 WARN_ON(1);
2123
2124 if (path->slots[*level] >=
2125 btrfs_header_nritems(cur))
2126 break;
2127
2128 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2129 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2130 blocksize = btrfs_level_size(root, *level - 1);
2131
2132 parent = path->nodes[*level];
2133 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002134
2135 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002136 if (!next)
2137 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002138
Chris Masone02119d2008-09-05 16:13:11 -04002139 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002140 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002141 if (ret) {
2142 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002143 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002144 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002145
Chris Masone02119d2008-09-05 16:13:11 -04002146 path->slots[*level]++;
2147 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002148 ret = btrfs_read_buffer(next, ptr_gen);
2149 if (ret) {
2150 free_extent_buffer(next);
2151 return ret;
2152 }
Chris Masone02119d2008-09-05 16:13:11 -04002153
Josef Bacik681ae502013-10-07 15:11:00 -04002154 if (trans) {
2155 btrfs_tree_lock(next);
2156 btrfs_set_lock_blocking(next);
2157 clean_tree_block(trans, root, next);
2158 btrfs_wait_tree_block_writeback(next);
2159 btrfs_tree_unlock(next);
2160 }
Chris Masone02119d2008-09-05 16:13:11 -04002161
Chris Masone02119d2008-09-05 16:13:11 -04002162 WARN_ON(root_owner !=
2163 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002164 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002165 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002166 if (ret) {
2167 free_extent_buffer(next);
2168 return ret;
2169 }
Chris Masone02119d2008-09-05 16:13:11 -04002170 }
2171 free_extent_buffer(next);
2172 continue;
2173 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002174 ret = btrfs_read_buffer(next, ptr_gen);
2175 if (ret) {
2176 free_extent_buffer(next);
2177 return ret;
2178 }
Chris Masone02119d2008-09-05 16:13:11 -04002179
2180 WARN_ON(*level <= 0);
2181 if (path->nodes[*level-1])
2182 free_extent_buffer(path->nodes[*level-1]);
2183 path->nodes[*level-1] = next;
2184 *level = btrfs_header_level(next);
2185 path->slots[*level] = 0;
2186 cond_resched();
2187 }
2188 WARN_ON(*level < 0);
2189 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2190
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002191 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002192
2193 cond_resched();
2194 return 0;
2195}
2196
Chris Masond3977122009-01-05 21:25:51 -05002197static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002198 struct btrfs_root *root,
2199 struct btrfs_path *path, int *level,
2200 struct walk_control *wc)
2201{
2202 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002203 int i;
2204 int slot;
2205 int ret;
2206
Chris Masond3977122009-01-05 21:25:51 -05002207 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002208 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002209 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002210 path->slots[i]++;
2211 *level = i;
2212 WARN_ON(*level == 0);
2213 return 0;
2214 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002215 struct extent_buffer *parent;
2216 if (path->nodes[*level] == root->node)
2217 parent = path->nodes[*level];
2218 else
2219 parent = path->nodes[*level + 1];
2220
2221 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002222 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002223 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002224 if (ret)
2225 return ret;
2226
Chris Masone02119d2008-09-05 16:13:11 -04002227 if (wc->free) {
2228 struct extent_buffer *next;
2229
2230 next = path->nodes[*level];
2231
Josef Bacik681ae502013-10-07 15:11:00 -04002232 if (trans) {
2233 btrfs_tree_lock(next);
2234 btrfs_set_lock_blocking(next);
2235 clean_tree_block(trans, root, next);
2236 btrfs_wait_tree_block_writeback(next);
2237 btrfs_tree_unlock(next);
2238 }
Chris Masone02119d2008-09-05 16:13:11 -04002239
Chris Masone02119d2008-09-05 16:13:11 -04002240 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002241 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002242 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002243 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002244 if (ret)
2245 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002246 }
2247 free_extent_buffer(path->nodes[*level]);
2248 path->nodes[*level] = NULL;
2249 *level = i + 1;
2250 }
2251 }
2252 return 1;
2253}
2254
2255/*
2256 * drop the reference count on the tree rooted at 'snap'. This traverses
2257 * the tree freeing any blocks that have a ref count of zero after being
2258 * decremented.
2259 */
2260static int walk_log_tree(struct btrfs_trans_handle *trans,
2261 struct btrfs_root *log, struct walk_control *wc)
2262{
2263 int ret = 0;
2264 int wret;
2265 int level;
2266 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002267 int orig_level;
2268
2269 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002270 if (!path)
2271 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002272
2273 level = btrfs_header_level(log->node);
2274 orig_level = level;
2275 path->nodes[level] = log->node;
2276 extent_buffer_get(log->node);
2277 path->slots[level] = 0;
2278
Chris Masond3977122009-01-05 21:25:51 -05002279 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002280 wret = walk_down_log_tree(trans, log, path, &level, wc);
2281 if (wret > 0)
2282 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002284 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002285 goto out;
2286 }
Chris Masone02119d2008-09-05 16:13:11 -04002287
2288 wret = walk_up_log_tree(trans, log, path, &level, wc);
2289 if (wret > 0)
2290 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002292 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002293 goto out;
2294 }
Chris Masone02119d2008-09-05 16:13:11 -04002295 }
2296
2297 /* was the root node processed? if not, catch it here */
2298 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002300 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002301 if (ret)
2302 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002303 if (wc->free) {
2304 struct extent_buffer *next;
2305
2306 next = path->nodes[orig_level];
2307
Josef Bacik681ae502013-10-07 15:11:00 -04002308 if (trans) {
2309 btrfs_tree_lock(next);
2310 btrfs_set_lock_blocking(next);
2311 clean_tree_block(trans, log, next);
2312 btrfs_wait_tree_block_writeback(next);
2313 btrfs_tree_unlock(next);
2314 }
Chris Masone02119d2008-09-05 16:13:11 -04002315
Chris Masone02119d2008-09-05 16:13:11 -04002316 WARN_ON(log->root_key.objectid !=
2317 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002318 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002319 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002320 if (ret)
2321 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002322 }
2323 }
2324
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325out:
Chris Masone02119d2008-09-05 16:13:11 -04002326 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002327 return ret;
2328}
2329
Yan Zheng7237f182009-01-21 12:54:03 -05002330/*
2331 * helper function to update the item for a given subvolumes log root
2332 * in the tree of log roots
2333 */
2334static int update_log_root(struct btrfs_trans_handle *trans,
2335 struct btrfs_root *log)
2336{
2337 int ret;
2338
2339 if (log->log_transid == 1) {
2340 /* insert root item on the first sync */
2341 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2342 &log->root_key, &log->root_item);
2343 } else {
2344 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2345 &log->root_key, &log->root_item);
2346 }
2347 return ret;
2348}
2349
Chris Mason12fcfd22009-03-24 10:24:20 -04002350static int wait_log_commit(struct btrfs_trans_handle *trans,
2351 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002352{
2353 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002354 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002355
Yan Zheng7237f182009-01-21 12:54:03 -05002356 /*
2357 * we only allow two pending log transactions at a time,
2358 * so we know that if ours is more than 2 older than the
2359 * current transaction, we're done
2360 */
Chris Masone02119d2008-09-05 16:13:11 -04002361 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002362 prepare_to_wait(&root->log_commit_wait[index],
2363 &wait, TASK_UNINTERRUPTIBLE);
2364 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002365
2366 if (root->fs_info->last_trans_log_full_commit !=
2367 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002368 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002369 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002370
Yan Zheng7237f182009-01-21 12:54:03 -05002371 finish_wait(&root->log_commit_wait[index], &wait);
2372 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002373 } while (root->fs_info->last_trans_log_full_commit !=
2374 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002375 atomic_read(&root->log_commit[index]));
2376 return 0;
2377}
2378
Jeff Mahoney143bede2012-03-01 14:56:26 +01002379static void wait_for_writer(struct btrfs_trans_handle *trans,
2380 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002381{
2382 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002383 while (root->fs_info->last_trans_log_full_commit !=
2384 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002385 prepare_to_wait(&root->log_writer_wait,
2386 &wait, TASK_UNINTERRUPTIBLE);
2387 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002388 if (root->fs_info->last_trans_log_full_commit !=
2389 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002390 schedule();
2391 mutex_lock(&root->log_mutex);
2392 finish_wait(&root->log_writer_wait, &wait);
2393 }
Chris Masone02119d2008-09-05 16:13:11 -04002394}
2395
2396/*
2397 * btrfs_sync_log does sends a given tree log down to the disk and
2398 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002399 * you know that any inodes previously logged are safely on disk only
2400 * if it returns 0.
2401 *
2402 * Any other return value means you need to call btrfs_commit_transaction.
2403 * Some of the edge cases for fsyncing directories that have had unlinks
2404 * or renames done in the past mean that sometimes the only safe
2405 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2406 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002407 */
2408int btrfs_sync_log(struct btrfs_trans_handle *trans,
2409 struct btrfs_root *root)
2410{
Yan Zheng7237f182009-01-21 12:54:03 -05002411 int index1;
2412 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002413 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002414 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002415 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002416 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002417 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002418 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002419
Yan Zheng7237f182009-01-21 12:54:03 -05002420 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002421 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002422 index1 = root->log_transid % 2;
2423 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002424 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002425 mutex_unlock(&root->log_mutex);
2426 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002427 }
Yan Zheng7237f182009-01-21 12:54:03 -05002428 atomic_set(&root->log_commit[index1], 1);
2429
2430 /* wait for previous tree log sync to complete */
2431 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002432 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002433 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002434 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002435 /* when we're on an ssd, just kick the log commit out */
2436 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002437 mutex_unlock(&root->log_mutex);
2438 schedule_timeout_uninterruptible(1);
2439 mutex_lock(&root->log_mutex);
2440 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002441 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002442 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002443 break;
2444 }
Chris Masond0c803c2008-09-11 16:17:57 -04002445
Chris Mason12fcfd22009-03-24 10:24:20 -04002446 /* bail out if we need to do a full commit */
2447 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2448 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002449 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002450 mutex_unlock(&root->log_mutex);
2451 goto out;
2452 }
2453
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002454 if (log_transid % 2 == 0)
2455 mark = EXTENT_DIRTY;
2456 else
2457 mark = EXTENT_NEW;
2458
Chris Mason690587d2009-10-13 13:29:19 -04002459 /* we start IO on all the marked extents here, but we don't actually
2460 * wait for them until later.
2461 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002462 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002463 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002464 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002465 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002466 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002467 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002468 mutex_unlock(&root->log_mutex);
2469 goto out;
2470 }
Yan Zheng7237f182009-01-21 12:54:03 -05002471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002473
Yan Zheng7237f182009-01-21 12:54:03 -05002474 root->log_transid++;
2475 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002476 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002477 smp_mb();
2478 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002479 * IO has been started, blocks of the log tree have WRITTEN flag set
2480 * in their headers. new modifications of the log will be written to
2481 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002482 */
2483 mutex_unlock(&root->log_mutex);
2484
2485 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002486 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002487 atomic_inc(&log_root_tree->log_writers);
2488 mutex_unlock(&log_root_tree->log_mutex);
2489
2490 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002491
2492 mutex_lock(&log_root_tree->log_mutex);
2493 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2494 smp_mb();
2495 if (waitqueue_active(&log_root_tree->log_writer_wait))
2496 wake_up(&log_root_tree->log_writer_wait);
2497 }
2498
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002499 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002500 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002501 if (ret != -ENOSPC) {
2502 btrfs_abort_transaction(trans, root, ret);
2503 mutex_unlock(&log_root_tree->log_mutex);
2504 goto out;
2505 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002506 root->fs_info->last_trans_log_full_commit = trans->transid;
2507 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002508 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002509 mutex_unlock(&log_root_tree->log_mutex);
2510 ret = -EAGAIN;
2511 goto out;
2512 }
2513
Yan Zheng7237f182009-01-21 12:54:03 -05002514 index2 = log_root_tree->log_transid % 2;
2515 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002516 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002517 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002518 wait_log_commit(trans, log_root_tree,
2519 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002520 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002521 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002522 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002523 goto out;
2524 }
2525 atomic_set(&log_root_tree->log_commit[index2], 1);
2526
Chris Mason12fcfd22009-03-24 10:24:20 -04002527 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2528 wait_log_commit(trans, log_root_tree,
2529 log_root_tree->log_transid - 1);
2530 }
Yan Zheng7237f182009-01-21 12:54:03 -05002531
Chris Mason12fcfd22009-03-24 10:24:20 -04002532 wait_for_writer(trans, log_root_tree);
2533
2534 /*
2535 * now that we've moved on to the tree of log tree roots,
2536 * check the full commit flag again
2537 */
2538 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002539 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002540 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002541 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002542 mutex_unlock(&log_root_tree->log_mutex);
2543 ret = -EAGAIN;
2544 goto out_wake_log_root;
2545 }
Yan Zheng7237f182009-01-21 12:54:03 -05002546
Miao Xiec6adc9c2013-05-28 10:05:39 +00002547 ret = btrfs_write_marked_extents(log_root_tree,
2548 &log_root_tree->dirty_log_pages,
2549 EXTENT_DIRTY | EXTENT_NEW);
2550 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002551 if (ret) {
2552 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002553 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002554 mutex_unlock(&log_root_tree->log_mutex);
2555 goto out_wake_log_root;
2556 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002557 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002558 btrfs_wait_marked_extents(log_root_tree,
2559 &log_root_tree->dirty_log_pages,
2560 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002561 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002562
David Sterba6c417612011-04-13 15:41:04 +02002563 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002564 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002565 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002566 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002567
Yan Zheng7237f182009-01-21 12:54:03 -05002568 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002569 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002570
2571 mutex_unlock(&log_root_tree->log_mutex);
2572
2573 /*
2574 * nobody else is going to jump in and write the the ctree
2575 * super here because the log_commit atomic below is protecting
2576 * us. We must be called with a transaction handle pinning
2577 * the running transaction open, so a full commit can't hop
2578 * in and cause problems either.
2579 */
Arne Jansena2de7332011-03-08 14:14:00 +01002580 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002581 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002582 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002583 if (ret) {
2584 btrfs_abort_transaction(trans, root, ret);
2585 goto out_wake_log_root;
2586 }
Yan Zheng7237f182009-01-21 12:54:03 -05002587
Chris Mason257c62e2009-10-13 13:21:08 -04002588 mutex_lock(&root->log_mutex);
2589 if (root->last_log_commit < log_transid)
2590 root->last_log_commit = log_transid;
2591 mutex_unlock(&root->log_mutex);
2592
Chris Mason12fcfd22009-03-24 10:24:20 -04002593out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002594 atomic_set(&log_root_tree->log_commit[index2], 0);
2595 smp_mb();
2596 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2597 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002598out:
Yan Zheng7237f182009-01-21 12:54:03 -05002599 atomic_set(&root->log_commit[index1], 0);
2600 smp_mb();
2601 if (waitqueue_active(&root->log_commit_wait[index1]))
2602 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002603 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002604}
2605
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002606static void free_log_tree(struct btrfs_trans_handle *trans,
2607 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002608{
2609 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002610 u64 start;
2611 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002612 struct walk_control wc = {
2613 .free = 1,
2614 .process_func = process_one_buffer
2615 };
2616
Josef Bacik681ae502013-10-07 15:11:00 -04002617 ret = walk_log_tree(trans, log, &wc);
2618 /* I don't think this can happen but just in case */
2619 if (ret)
2620 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002621
Chris Masond3977122009-01-05 21:25:51 -05002622 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002623 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002624 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2625 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002626 if (ret)
2627 break;
2628
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002629 clear_extent_bits(&log->dirty_log_pages, start, end,
2630 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002631 }
2632
Josef Bacik2ab28f32012-10-12 15:27:49 -04002633 /*
2634 * We may have short-circuited the log tree with the full commit logic
2635 * and left ordered extents on our list, so clear these out to keep us
2636 * from leaking inodes and memory.
2637 */
2638 btrfs_free_logged_extents(log, 0);
2639 btrfs_free_logged_extents(log, 1);
2640
Yan Zheng7237f182009-01-21 12:54:03 -05002641 free_extent_buffer(log->node);
2642 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002643}
2644
2645/*
2646 * free all the extents used by the tree log. This should be called
2647 * at commit time of the full transaction
2648 */
2649int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2650{
2651 if (root->log_root) {
2652 free_log_tree(trans, root->log_root);
2653 root->log_root = NULL;
2654 }
2655 return 0;
2656}
2657
2658int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2659 struct btrfs_fs_info *fs_info)
2660{
2661 if (fs_info->log_root_tree) {
2662 free_log_tree(trans, fs_info->log_root_tree);
2663 fs_info->log_root_tree = NULL;
2664 }
Chris Masone02119d2008-09-05 16:13:11 -04002665 return 0;
2666}
2667
2668/*
Chris Masone02119d2008-09-05 16:13:11 -04002669 * If both a file and directory are logged, and unlinks or renames are
2670 * mixed in, we have a few interesting corners:
2671 *
2672 * create file X in dir Y
2673 * link file X to X.link in dir Y
2674 * fsync file X
2675 * unlink file X but leave X.link
2676 * fsync dir Y
2677 *
2678 * After a crash we would expect only X.link to exist. But file X
2679 * didn't get fsync'd again so the log has back refs for X and X.link.
2680 *
2681 * We solve this by removing directory entries and inode backrefs from the
2682 * log when a file that was logged in the current transaction is
2683 * unlinked. Any later fsync will include the updated log entries, and
2684 * we'll be able to reconstruct the proper directory items from backrefs.
2685 *
2686 * This optimizations allows us to avoid relogging the entire inode
2687 * or the entire directory.
2688 */
2689int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2690 struct btrfs_root *root,
2691 const char *name, int name_len,
2692 struct inode *dir, u64 index)
2693{
2694 struct btrfs_root *log;
2695 struct btrfs_dir_item *di;
2696 struct btrfs_path *path;
2697 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002698 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002699 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002700 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002701
Chris Mason3a5f1d42008-09-11 15:53:37 -04002702 if (BTRFS_I(dir)->logged_trans < trans->transid)
2703 return 0;
2704
Chris Masone02119d2008-09-05 16:13:11 -04002705 ret = join_running_log_trans(root);
2706 if (ret)
2707 return 0;
2708
2709 mutex_lock(&BTRFS_I(dir)->log_mutex);
2710
2711 log = root->log_root;
2712 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002713 if (!path) {
2714 err = -ENOMEM;
2715 goto out_unlock;
2716 }
liubo2a29edc2011-01-26 06:22:08 +00002717
Li Zefan33345d012011-04-20 10:31:50 +08002718 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002719 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002720 if (IS_ERR(di)) {
2721 err = PTR_ERR(di);
2722 goto fail;
2723 }
2724 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002725 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2726 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002727 if (ret) {
2728 err = ret;
2729 goto fail;
2730 }
Chris Masone02119d2008-09-05 16:13:11 -04002731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002732 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002733 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002734 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002735 if (IS_ERR(di)) {
2736 err = PTR_ERR(di);
2737 goto fail;
2738 }
2739 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002740 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2741 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002742 if (ret) {
2743 err = ret;
2744 goto fail;
2745 }
Chris Masone02119d2008-09-05 16:13:11 -04002746 }
2747
2748 /* update the directory size in the log to reflect the names
2749 * we have removed
2750 */
2751 if (bytes_del) {
2752 struct btrfs_key key;
2753
Li Zefan33345d012011-04-20 10:31:50 +08002754 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002755 key.offset = 0;
2756 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002757 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002758
2759 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002760 if (ret < 0) {
2761 err = ret;
2762 goto fail;
2763 }
Chris Masone02119d2008-09-05 16:13:11 -04002764 if (ret == 0) {
2765 struct btrfs_inode_item *item;
2766 u64 i_size;
2767
2768 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2769 struct btrfs_inode_item);
2770 i_size = btrfs_inode_size(path->nodes[0], item);
2771 if (i_size > bytes_del)
2772 i_size -= bytes_del;
2773 else
2774 i_size = 0;
2775 btrfs_set_inode_size(path->nodes[0], item, i_size);
2776 btrfs_mark_buffer_dirty(path->nodes[0]);
2777 } else
2778 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002779 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002780 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002781fail:
Chris Masone02119d2008-09-05 16:13:11 -04002782 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002783out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002784 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002785 if (ret == -ENOSPC) {
2786 root->fs_info->last_trans_log_full_commit = trans->transid;
2787 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002788 } else if (ret < 0)
2789 btrfs_abort_transaction(trans, root, ret);
2790
Chris Mason12fcfd22009-03-24 10:24:20 -04002791 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002792
Andi Kleen411fc6b2010-10-29 15:14:31 -04002793 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002794}
2795
2796/* see comments for btrfs_del_dir_entries_in_log */
2797int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2798 struct btrfs_root *root,
2799 const char *name, int name_len,
2800 struct inode *inode, u64 dirid)
2801{
2802 struct btrfs_root *log;
2803 u64 index;
2804 int ret;
2805
Chris Mason3a5f1d42008-09-11 15:53:37 -04002806 if (BTRFS_I(inode)->logged_trans < trans->transid)
2807 return 0;
2808
Chris Masone02119d2008-09-05 16:13:11 -04002809 ret = join_running_log_trans(root);
2810 if (ret)
2811 return 0;
2812 log = root->log_root;
2813 mutex_lock(&BTRFS_I(inode)->log_mutex);
2814
Li Zefan33345d012011-04-20 10:31:50 +08002815 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002816 dirid, &index);
2817 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002818 if (ret == -ENOSPC) {
2819 root->fs_info->last_trans_log_full_commit = trans->transid;
2820 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002821 } else if (ret < 0 && ret != -ENOENT)
2822 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002823 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002824
Chris Masone02119d2008-09-05 16:13:11 -04002825 return ret;
2826}
2827
2828/*
2829 * creates a range item in the log for 'dirid'. first_offset and
2830 * last_offset tell us which parts of the key space the log should
2831 * be considered authoritative for.
2832 */
2833static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2834 struct btrfs_root *log,
2835 struct btrfs_path *path,
2836 int key_type, u64 dirid,
2837 u64 first_offset, u64 last_offset)
2838{
2839 int ret;
2840 struct btrfs_key key;
2841 struct btrfs_dir_log_item *item;
2842
2843 key.objectid = dirid;
2844 key.offset = first_offset;
2845 if (key_type == BTRFS_DIR_ITEM_KEY)
2846 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2847 else
2848 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2849 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002850 if (ret)
2851 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002852
2853 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2854 struct btrfs_dir_log_item);
2855 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2856 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002857 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002858 return 0;
2859}
2860
2861/*
2862 * log all the items included in the current transaction for a given
2863 * directory. This also creates the range items in the log tree required
2864 * to replay anything deleted before the fsync
2865 */
2866static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2867 struct btrfs_root *root, struct inode *inode,
2868 struct btrfs_path *path,
2869 struct btrfs_path *dst_path, int key_type,
2870 u64 min_offset, u64 *last_offset_ret)
2871{
2872 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002873 struct btrfs_root *log = root->log_root;
2874 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002875 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002876 int ret;
2877 int i;
2878 int nritems;
2879 u64 first_offset = min_offset;
2880 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002881 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002882
2883 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002884
Li Zefan33345d012011-04-20 10:31:50 +08002885 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002886 min_key.type = key_type;
2887 min_key.offset = min_offset;
2888
2889 path->keep_locks = 1;
2890
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002891 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002892
2893 /*
2894 * we didn't find anything from this transaction, see if there
2895 * is anything at all
2896 */
Li Zefan33345d012011-04-20 10:31:50 +08002897 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2898 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002899 min_key.type = key_type;
2900 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002901 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002902 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2903 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002904 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002905 return ret;
2906 }
Li Zefan33345d012011-04-20 10:31:50 +08002907 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002908
2909 /* if ret == 0 there are items for this type,
2910 * create a range to tell us the last key of this type.
2911 * otherwise, there are no items in this directory after
2912 * *min_offset, and we create a range to indicate that.
2913 */
2914 if (ret == 0) {
2915 struct btrfs_key tmp;
2916 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2917 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002918 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002919 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002920 }
2921 goto done;
2922 }
2923
2924 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002925 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002926 if (ret == 0) {
2927 struct btrfs_key tmp;
2928 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2929 if (key_type == tmp.type) {
2930 first_offset = tmp.offset;
2931 ret = overwrite_item(trans, log, dst_path,
2932 path->nodes[0], path->slots[0],
2933 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002934 if (ret) {
2935 err = ret;
2936 goto done;
2937 }
Chris Masone02119d2008-09-05 16:13:11 -04002938 }
2939 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002940 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002941
2942 /* find the first key from this transaction again */
2943 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2944 if (ret != 0) {
2945 WARN_ON(1);
2946 goto done;
2947 }
2948
2949 /*
2950 * we have a block from this transaction, log every item in it
2951 * from our directory
2952 */
Chris Masond3977122009-01-05 21:25:51 -05002953 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002954 struct btrfs_key tmp;
2955 src = path->nodes[0];
2956 nritems = btrfs_header_nritems(src);
2957 for (i = path->slots[0]; i < nritems; i++) {
2958 btrfs_item_key_to_cpu(src, &min_key, i);
2959
Li Zefan33345d012011-04-20 10:31:50 +08002960 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002961 goto done;
2962 ret = overwrite_item(trans, log, dst_path, src, i,
2963 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002964 if (ret) {
2965 err = ret;
2966 goto done;
2967 }
Chris Masone02119d2008-09-05 16:13:11 -04002968 }
2969 path->slots[0] = nritems;
2970
2971 /*
2972 * look ahead to the next item and see if it is also
2973 * from this directory and from this transaction
2974 */
2975 ret = btrfs_next_leaf(root, path);
2976 if (ret == 1) {
2977 last_offset = (u64)-1;
2978 goto done;
2979 }
2980 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002981 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002982 last_offset = (u64)-1;
2983 goto done;
2984 }
2985 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2986 ret = overwrite_item(trans, log, dst_path,
2987 path->nodes[0], path->slots[0],
2988 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002989 if (ret)
2990 err = ret;
2991 else
2992 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002993 goto done;
2994 }
2995 }
2996done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002997 btrfs_release_path(path);
2998 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002999
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003000 if (err == 0) {
3001 *last_offset_ret = last_offset;
3002 /*
3003 * insert the log range keys to indicate where the log
3004 * is valid
3005 */
3006 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003007 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003008 if (ret)
3009 err = ret;
3010 }
3011 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003012}
3013
3014/*
3015 * logging directories is very similar to logging inodes, We find all the items
3016 * from the current transaction and write them to the log.
3017 *
3018 * The recovery code scans the directory in the subvolume, and if it finds a
3019 * key in the range logged that is not present in the log tree, then it means
3020 * that dir entry was unlinked during the transaction.
3021 *
3022 * In order for that scan to work, we must include one key smaller than
3023 * the smallest logged by this transaction and one key larger than the largest
3024 * key logged by this transaction.
3025 */
3026static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3027 struct btrfs_root *root, struct inode *inode,
3028 struct btrfs_path *path,
3029 struct btrfs_path *dst_path)
3030{
3031 u64 min_key;
3032 u64 max_key;
3033 int ret;
3034 int key_type = BTRFS_DIR_ITEM_KEY;
3035
3036again:
3037 min_key = 0;
3038 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003039 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003040 ret = log_dir_items(trans, root, inode, path,
3041 dst_path, key_type, min_key,
3042 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003043 if (ret)
3044 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003045 if (max_key == (u64)-1)
3046 break;
3047 min_key = max_key + 1;
3048 }
3049
3050 if (key_type == BTRFS_DIR_ITEM_KEY) {
3051 key_type = BTRFS_DIR_INDEX_KEY;
3052 goto again;
3053 }
3054 return 0;
3055}
3056
3057/*
3058 * a helper function to drop items from the log before we relog an
3059 * inode. max_key_type indicates the highest item type to remove.
3060 * This cannot be run for file data extents because it does not
3061 * free the extents they point to.
3062 */
3063static int drop_objectid_items(struct btrfs_trans_handle *trans,
3064 struct btrfs_root *log,
3065 struct btrfs_path *path,
3066 u64 objectid, int max_key_type)
3067{
3068 int ret;
3069 struct btrfs_key key;
3070 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003071 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003072
3073 key.objectid = objectid;
3074 key.type = max_key_type;
3075 key.offset = (u64)-1;
3076
Chris Masond3977122009-01-05 21:25:51 -05003077 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003078 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003079 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003080 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003081 break;
3082
3083 if (path->slots[0] == 0)
3084 break;
3085
3086 path->slots[0]--;
3087 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3088 path->slots[0]);
3089
3090 if (found_key.objectid != objectid)
3091 break;
3092
Josef Bacik18ec90d2012-09-28 11:56:28 -04003093 found_key.offset = 0;
3094 found_key.type = 0;
3095 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3096 &start_slot);
3097
3098 ret = btrfs_del_items(trans, log, path, start_slot,
3099 path->slots[0] - start_slot + 1);
3100 /*
3101 * If start slot isn't 0 then we don't need to re-search, we've
3102 * found the last guy with the objectid in this tree.
3103 */
3104 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003105 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003106 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003107 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003108 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003109 if (ret > 0)
3110 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003111 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003112}
3113
Josef Bacik94edf4a2012-09-25 14:56:25 -04003114static void fill_inode_item(struct btrfs_trans_handle *trans,
3115 struct extent_buffer *leaf,
3116 struct btrfs_inode_item *item,
3117 struct inode *inode, int log_inode_only)
3118{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003119 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003120
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003121 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003122
3123 if (log_inode_only) {
3124 /* set the generation to zero so the recover code
3125 * can tell the difference between an logging
3126 * just to say 'this inode exists' and a logging
3127 * to say 'update this inode with these values'
3128 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003129 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3130 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003131 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003132 btrfs_set_token_inode_generation(leaf, item,
3133 BTRFS_I(inode)->generation,
3134 &token);
3135 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003136 }
3137
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003138 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3139 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3140 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3141 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3142
3143 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3144 inode->i_atime.tv_sec, &token);
3145 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3146 inode->i_atime.tv_nsec, &token);
3147
3148 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3149 inode->i_mtime.tv_sec, &token);
3150 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3151 inode->i_mtime.tv_nsec, &token);
3152
3153 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3154 inode->i_ctime.tv_sec, &token);
3155 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3156 inode->i_ctime.tv_nsec, &token);
3157
3158 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3159 &token);
3160
3161 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3162 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3163 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3164 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3165 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003166}
3167
Josef Bacika95249b2012-10-11 16:17:34 -04003168static int log_inode_item(struct btrfs_trans_handle *trans,
3169 struct btrfs_root *log, struct btrfs_path *path,
3170 struct inode *inode)
3171{
3172 struct btrfs_inode_item *inode_item;
3173 struct btrfs_key key;
3174 int ret;
3175
3176 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3177 ret = btrfs_insert_empty_item(trans, log, path, &key,
3178 sizeof(*inode_item));
3179 if (ret && ret != -EEXIST)
3180 return ret;
3181 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3182 struct btrfs_inode_item);
3183 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3184 btrfs_release_path(path);
3185 return 0;
3186}
3187
Chris Mason31ff1cd2008-09-11 16:17:57 -04003188static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003189 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003190 struct btrfs_path *dst_path,
3191 struct extent_buffer *src,
3192 int start_slot, int nr, int inode_only)
3193{
3194 unsigned long src_offset;
3195 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003196 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003197 struct btrfs_file_extent_item *extent;
3198 struct btrfs_inode_item *inode_item;
3199 int ret;
3200 struct btrfs_key *ins_keys;
3201 u32 *ins_sizes;
3202 char *ins_data;
3203 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003204 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003205 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003206
3207 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003208
3209 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3210 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003211 if (!ins_data)
3212 return -ENOMEM;
3213
Chris Mason31ff1cd2008-09-11 16:17:57 -04003214 ins_sizes = (u32 *)ins_data;
3215 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3216
3217 for (i = 0; i < nr; i++) {
3218 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3219 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3220 }
3221 ret = btrfs_insert_empty_items(trans, log, dst_path,
3222 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003223 if (ret) {
3224 kfree(ins_data);
3225 return ret;
3226 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003228 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003229 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3230 dst_path->slots[0]);
3231
3232 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3233
Josef Bacik94edf4a2012-09-25 14:56:25 -04003234 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003235 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3236 dst_path->slots[0],
3237 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003238 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3239 inode, inode_only == LOG_INODE_EXISTS);
3240 } else {
3241 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3242 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003243 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003244
Chris Mason31ff1cd2008-09-11 16:17:57 -04003245 /* take a reference on file data extents so that truncates
3246 * or deletes of this inode don't have to relog the inode
3247 * again
3248 */
Liu Bod2794402012-08-29 01:07:56 -06003249 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3250 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003251 int found_type;
3252 extent = btrfs_item_ptr(src, start_slot + i,
3253 struct btrfs_file_extent_item);
3254
liubo8e531cd2011-05-06 10:36:09 +08003255 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3256 continue;
3257
Chris Mason31ff1cd2008-09-11 16:17:57 -04003258 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003259 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003260 u64 ds, dl, cs, cl;
3261 ds = btrfs_file_extent_disk_bytenr(src,
3262 extent);
3263 /* ds == 0 is a hole */
3264 if (ds == 0)
3265 continue;
3266
3267 dl = btrfs_file_extent_disk_num_bytes(src,
3268 extent);
3269 cs = btrfs_file_extent_offset(src, extent);
3270 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003271 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003272 if (btrfs_file_extent_compression(src,
3273 extent)) {
3274 cs = 0;
3275 cl = dl;
3276 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003277
3278 ret = btrfs_lookup_csums_range(
3279 log->fs_info->csum_root,
3280 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003281 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003282 if (ret) {
3283 btrfs_release_path(dst_path);
3284 kfree(ins_data);
3285 return ret;
3286 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003287 }
3288 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003289 }
3290
3291 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003292 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003293 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003294
3295 /*
3296 * we have to do this after the loop above to avoid changing the
3297 * log tree while trying to change the log tree.
3298 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003299 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003300 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003301 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3302 struct btrfs_ordered_sum,
3303 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003304 if (!ret)
3305 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003306 list_del(&sums->list);
3307 kfree(sums);
3308 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003309 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003310}
3311
Josef Bacik5dc562c2012-08-17 13:14:17 -04003312static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3313{
3314 struct extent_map *em1, *em2;
3315
3316 em1 = list_entry(a, struct extent_map, list);
3317 em2 = list_entry(b, struct extent_map, list);
3318
3319 if (em1->start < em2->start)
3320 return -1;
3321 else if (em1->start > em2->start)
3322 return 1;
3323 return 0;
3324}
3325
Josef Bacik5dc562c2012-08-17 13:14:17 -04003326static int log_one_extent(struct btrfs_trans_handle *trans,
3327 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003328 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003329{
3330 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003331 struct btrfs_file_extent_item *fi;
3332 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003333 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003334 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003335 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003336 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003337 u64 mod_start = em->mod_start;
3338 u64 mod_len = em->mod_len;
3339 u64 csum_offset;
3340 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003341 u64 extent_offset = em->start - em->orig_start;
3342 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003343 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003344 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003345 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003346
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003347 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3348 em->start + em->len, NULL, 0);
3349 if (ret)
3350 return ret;
3351
Josef Bacik70c8a912012-10-11 16:54:30 -04003352 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003353 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003354 key.objectid = btrfs_ino(inode);
3355 key.type = BTRFS_EXTENT_DATA_KEY;
3356 key.offset = em->start;
Josef Bacik70c8a912012-10-11 16:54:30 -04003357
3358 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003359 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003360 return ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003361 leaf = path->nodes[0];
3362 fi = btrfs_item_ptr(leaf, path->slots[0],
3363 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003364
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003365 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3366 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003367 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3368 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003369 btrfs_set_token_file_extent_type(leaf, fi,
3370 BTRFS_FILE_EXTENT_PREALLOC,
3371 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003372 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003373 btrfs_set_token_file_extent_type(leaf, fi,
3374 BTRFS_FILE_EXTENT_REG,
3375 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003376 if (em->block_start == 0)
3377 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003378 }
3379
Josef Bacik70c8a912012-10-11 16:54:30 -04003380 block_len = max(em->block_len, em->orig_block_len);
3381 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003382 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3383 em->block_start,
3384 &token);
3385 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3386 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003387 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003388 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3389 em->block_start -
3390 extent_offset, &token);
3391 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3392 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003393 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003394 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3395 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3396 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003397 }
3398
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003399 btrfs_set_token_file_extent_offset(leaf, fi,
3400 em->start - em->orig_start,
3401 &token);
3402 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003403 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003404 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3405 &token);
3406 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3407 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003408 btrfs_mark_buffer_dirty(leaf);
3409
Josef Bacik70c8a912012-10-11 16:54:30 -04003410 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003411 if (ret) {
3412 return ret;
3413 }
3414
3415 if (skip_csum)
3416 return 0;
3417
Liu Bo192000d2013-01-06 03:38:22 +00003418 if (em->compress_type) {
3419 csum_offset = 0;
3420 csum_len = block_len;
3421 }
3422
Josef Bacik2ab28f32012-10-12 15:27:49 -04003423 /*
3424 * First check and see if our csums are on our outstanding ordered
3425 * extents.
3426 */
3427again:
3428 spin_lock_irq(&log->log_extents_lock[index]);
3429 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3430 struct btrfs_ordered_sum *sum;
3431
3432 if (!mod_len)
3433 break;
3434
3435 if (ordered->inode != inode)
3436 continue;
3437
3438 if (ordered->file_offset + ordered->len <= mod_start ||
3439 mod_start + mod_len <= ordered->file_offset)
3440 continue;
3441
3442 /*
3443 * We are going to copy all the csums on this ordered extent, so
3444 * go ahead and adjust mod_start and mod_len in case this
3445 * ordered extent has already been logged.
3446 */
3447 if (ordered->file_offset > mod_start) {
3448 if (ordered->file_offset + ordered->len >=
3449 mod_start + mod_len)
3450 mod_len = ordered->file_offset - mod_start;
3451 /*
3452 * If we have this case
3453 *
3454 * |--------- logged extent ---------|
3455 * |----- ordered extent ----|
3456 *
3457 * Just don't mess with mod_start and mod_len, we'll
3458 * just end up logging more csums than we need and it
3459 * will be ok.
3460 */
3461 } else {
3462 if (ordered->file_offset + ordered->len <
3463 mod_start + mod_len) {
3464 mod_len = (mod_start + mod_len) -
3465 (ordered->file_offset + ordered->len);
3466 mod_start = ordered->file_offset +
3467 ordered->len;
3468 } else {
3469 mod_len = 0;
3470 }
3471 }
3472
3473 /*
3474 * To keep us from looping for the above case of an ordered
3475 * extent that falls inside of the logged extent.
3476 */
3477 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3478 &ordered->flags))
3479 continue;
3480 atomic_inc(&ordered->refs);
3481 spin_unlock_irq(&log->log_extents_lock[index]);
3482 /*
3483 * we've dropped the lock, we must either break or
3484 * start over after this.
3485 */
3486
3487 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3488
3489 list_for_each_entry(sum, &ordered->list, list) {
3490 ret = btrfs_csum_file_blocks(trans, log, sum);
3491 if (ret) {
3492 btrfs_put_ordered_extent(ordered);
3493 goto unlocked;
3494 }
3495 }
3496 btrfs_put_ordered_extent(ordered);
3497 goto again;
3498
3499 }
3500 spin_unlock_irq(&log->log_extents_lock[index]);
3501unlocked:
3502
3503 if (!mod_len || ret)
3504 return ret;
3505
3506 csum_offset = mod_start - em->start;
3507 csum_len = mod_len;
3508
Josef Bacik70c8a912012-10-11 16:54:30 -04003509 /* block start is already adjusted for the file extent offset. */
3510 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3511 em->block_start + csum_offset,
3512 em->block_start + csum_offset +
3513 csum_len - 1, &ordered_sums, 0);
3514 if (ret)
3515 return ret;
3516
3517 while (!list_empty(&ordered_sums)) {
3518 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3519 struct btrfs_ordered_sum,
3520 list);
3521 if (!ret)
3522 ret = btrfs_csum_file_blocks(trans, log, sums);
3523 list_del(&sums->list);
3524 kfree(sums);
3525 }
3526
3527 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003528}
3529
3530static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3531 struct btrfs_root *root,
3532 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003533 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003534{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003535 struct extent_map *em, *n;
3536 struct list_head extents;
3537 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3538 u64 test_gen;
3539 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003540 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003541
3542 INIT_LIST_HEAD(&extents);
3543
Josef Bacik5dc562c2012-08-17 13:14:17 -04003544 write_lock(&tree->lock);
3545 test_gen = root->fs_info->last_trans_committed;
3546
3547 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3548 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003549
3550 /*
3551 * Just an arbitrary number, this can be really CPU intensive
3552 * once we start getting a lot of extents, and really once we
3553 * have a bunch of extents we just want to commit since it will
3554 * be faster.
3555 */
3556 if (++num > 32768) {
3557 list_del_init(&tree->modified_extents);
3558 ret = -EFBIG;
3559 goto process;
3560 }
3561
Josef Bacik5dc562c2012-08-17 13:14:17 -04003562 if (em->generation <= test_gen)
3563 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003564 /* Need a ref to keep it from getting evicted from cache */
3565 atomic_inc(&em->refs);
3566 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003567 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003568 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003569 }
3570
3571 list_sort(NULL, &extents, extent_cmp);
3572
Josef Bacik2ab28f32012-10-12 15:27:49 -04003573process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003574 while (!list_empty(&extents)) {
3575 em = list_entry(extents.next, struct extent_map, list);
3576
3577 list_del_init(&em->list);
3578
3579 /*
3580 * If we had an error we just need to delete everybody from our
3581 * private list.
3582 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003583 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003584 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003585 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003586 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003587 }
3588
3589 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003590
Josef Bacik70c8a912012-10-11 16:54:30 -04003591 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003592 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003593 clear_em_logging(tree, em);
3594 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003595 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003596 WARN_ON(!list_empty(&extents));
3597 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003598
Josef Bacik5dc562c2012-08-17 13:14:17 -04003599 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003600 return ret;
3601}
3602
Chris Masone02119d2008-09-05 16:13:11 -04003603/* log a single inode in the tree log.
3604 * At least one parent directory for this inode must exist in the tree
3605 * or be logged already.
3606 *
3607 * Any items from this inode changed by the current transaction are copied
3608 * to the log tree. An extra reference is taken on any extents in this
3609 * file, allowing us to avoid a whole pile of corner cases around logging
3610 * blocks that have been removed from the tree.
3611 *
3612 * See LOG_INODE_ALL and related defines for a description of what inode_only
3613 * does.
3614 *
3615 * This handles both files and directories.
3616 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003617static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003618 struct btrfs_root *root, struct inode *inode,
3619 int inode_only)
3620{
3621 struct btrfs_path *path;
3622 struct btrfs_path *dst_path;
3623 struct btrfs_key min_key;
3624 struct btrfs_key max_key;
3625 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003626 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003627 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003628 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003629 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003630 int ins_start_slot = 0;
3631 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003632 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003633 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003634
Chris Masone02119d2008-09-05 16:13:11 -04003635 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003636 if (!path)
3637 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003638 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003639 if (!dst_path) {
3640 btrfs_free_path(path);
3641 return -ENOMEM;
3642 }
Chris Masone02119d2008-09-05 16:13:11 -04003643
Li Zefan33345d012011-04-20 10:31:50 +08003644 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003645 min_key.type = BTRFS_INODE_ITEM_KEY;
3646 min_key.offset = 0;
3647
Li Zefan33345d012011-04-20 10:31:50 +08003648 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003649
Chris Mason12fcfd22009-03-24 10:24:20 -04003650
Josef Bacik5dc562c2012-08-17 13:14:17 -04003651 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003652 if (S_ISDIR(inode->i_mode) ||
3653 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3654 &BTRFS_I(inode)->runtime_flags) &&
3655 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003656 max_key.type = BTRFS_XATTR_ITEM_KEY;
3657 else
3658 max_key.type = (u8)-1;
3659 max_key.offset = (u64)-1;
3660
Josef Bacik94edf4a2012-09-25 14:56:25 -04003661 /* Only run delayed items if we are a dir or a new file */
3662 if (S_ISDIR(inode->i_mode) ||
3663 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3664 ret = btrfs_commit_inode_delayed_items(trans, inode);
3665 if (ret) {
3666 btrfs_free_path(path);
3667 btrfs_free_path(dst_path);
3668 return ret;
3669 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003670 }
3671
Chris Masone02119d2008-09-05 16:13:11 -04003672 mutex_lock(&BTRFS_I(inode)->log_mutex);
3673
Josef Bacik2ab28f32012-10-12 15:27:49 -04003674 btrfs_get_logged_extents(log, inode);
3675
Chris Masone02119d2008-09-05 16:13:11 -04003676 /*
3677 * a brute force approach to making sure we get the most uptodate
3678 * copies of everything.
3679 */
3680 if (S_ISDIR(inode->i_mode)) {
3681 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3682
3683 if (inode_only == LOG_INODE_EXISTS)
3684 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003685 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003686 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003687 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3688 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003689 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3690 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003691 ret = btrfs_truncate_inode_items(trans, log,
3692 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003693 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3694 &BTRFS_I(inode)->runtime_flags)) {
3695 if (inode_only == LOG_INODE_ALL)
3696 fast_search = true;
3697 max_key.type = BTRFS_XATTR_ITEM_KEY;
3698 ret = drop_objectid_items(trans, log, path, ino,
3699 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003700 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003701 if (inode_only == LOG_INODE_ALL)
3702 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003703 ret = log_inode_item(trans, log, dst_path, inode);
3704 if (ret) {
3705 err = ret;
3706 goto out_unlock;
3707 }
3708 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003709 }
Josef Bacika95249b2012-10-11 16:17:34 -04003710
Chris Masone02119d2008-09-05 16:13:11 -04003711 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003712 if (ret) {
3713 err = ret;
3714 goto out_unlock;
3715 }
Chris Masone02119d2008-09-05 16:13:11 -04003716 path->keep_locks = 1;
3717
Chris Masond3977122009-01-05 21:25:51 -05003718 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003719 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003720 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003721 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003722 if (ret != 0)
3723 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003724again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003725 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003726 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003727 break;
3728 if (min_key.type > max_key.type)
3729 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003730
Chris Masone02119d2008-09-05 16:13:11 -04003731 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003732 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3733 ins_nr++;
3734 goto next_slot;
3735 } else if (!ins_nr) {
3736 ins_start_slot = path->slots[0];
3737 ins_nr = 1;
3738 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003739 }
3740
Liu Bod2794402012-08-29 01:07:56 -06003741 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003742 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003743 if (ret) {
3744 err = ret;
3745 goto out_unlock;
3746 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003747 ins_nr = 1;
3748 ins_start_slot = path->slots[0];
3749next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003750
Chris Mason3a5f1d42008-09-11 15:53:37 -04003751 nritems = btrfs_header_nritems(path->nodes[0]);
3752 path->slots[0]++;
3753 if (path->slots[0] < nritems) {
3754 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3755 path->slots[0]);
3756 goto again;
3757 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003758 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003759 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003760 ins_start_slot,
3761 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003762 if (ret) {
3763 err = ret;
3764 goto out_unlock;
3765 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003766 ins_nr = 0;
3767 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003768 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003769
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003770 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003771 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003772 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003773 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003774 min_key.offset = 0;
3775 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003776 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003777 }
Chris Masone02119d2008-09-05 16:13:11 -04003778 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003779 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003780 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003781 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003782 if (ret) {
3783 err = ret;
3784 goto out_unlock;
3785 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003786 ins_nr = 0;
3787 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003788
Josef Bacika95249b2012-10-11 16:17:34 -04003789log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003790 btrfs_release_path(path);
3791 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003792 if (fast_search) {
Josef Bacik70c8a912012-10-11 16:54:30 -04003793 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003794 if (ret) {
3795 err = ret;
3796 goto out_unlock;
3797 }
Liu Bo06d3d222012-08-27 10:52:19 -06003798 } else {
3799 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3800 struct extent_map *em, *n;
3801
Miao Xiebbe14262012-11-01 07:34:54 +00003802 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003803 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3804 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003805 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003806 }
3807
Chris Mason9623f9a2008-09-11 17:42:42 -04003808 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003809 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003810 if (ret) {
3811 err = ret;
3812 goto out_unlock;
3813 }
Chris Masone02119d2008-09-05 16:13:11 -04003814 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003815 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003816 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003817out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003818 if (err)
3819 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003820 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3821
3822 btrfs_free_path(path);
3823 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003824 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003825}
3826
Chris Mason12fcfd22009-03-24 10:24:20 -04003827/*
3828 * follow the dentry parent pointers up the chain and see if any
3829 * of the directories in it require a full commit before they can
3830 * be logged. Returns zero if nothing special needs to be done or 1 if
3831 * a full commit is required.
3832 */
3833static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3834 struct inode *inode,
3835 struct dentry *parent,
3836 struct super_block *sb,
3837 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003838{
Chris Mason12fcfd22009-03-24 10:24:20 -04003839 int ret = 0;
3840 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003841 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04003842 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04003843
Chris Masonaf4176b2009-03-24 10:24:31 -04003844 /*
3845 * for regular files, if its inode is already on disk, we don't
3846 * have to worry about the parents at all. This is because
3847 * we can use the last_unlink_trans field to record renames
3848 * and other fun in this file.
3849 */
3850 if (S_ISREG(inode->i_mode) &&
3851 BTRFS_I(inode)->generation <= last_committed &&
3852 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3853 goto out;
3854
Chris Mason12fcfd22009-03-24 10:24:20 -04003855 if (!S_ISDIR(inode->i_mode)) {
3856 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3857 goto out;
3858 inode = parent->d_inode;
3859 }
3860
3861 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04003862 /*
3863 * If we are logging a directory then we start with our inode,
3864 * not our parents inode, so we need to skipp setting the
3865 * logged_trans so that further down in the log code we don't
3866 * think this inode has already been logged.
3867 */
3868 if (inode != orig_inode)
3869 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04003870 smp_mb();
3871
3872 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3873 root = BTRFS_I(inode)->root;
3874
3875 /*
3876 * make sure any commits to the log are forced
3877 * to be full commits
3878 */
3879 root->fs_info->last_trans_log_full_commit =
3880 trans->transid;
3881 ret = 1;
3882 break;
3883 }
3884
3885 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3886 break;
3887
Yan, Zheng76dda932009-09-21 16:00:26 -04003888 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003889 break;
3890
Josef Bacik6a912212010-11-20 09:48:00 +00003891 parent = dget_parent(parent);
3892 dput(old_parent);
3893 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003894 inode = parent->d_inode;
3895
3896 }
Josef Bacik6a912212010-11-20 09:48:00 +00003897 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003898out:
Chris Masone02119d2008-09-05 16:13:11 -04003899 return ret;
3900}
3901
3902/*
3903 * helper function around btrfs_log_inode to make sure newly created
3904 * parent directories also end up in the log. A minimal inode and backref
3905 * only logging is done of any parent directories that are older than
3906 * the last committed transaction
3907 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003908static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3909 struct btrfs_root *root, struct inode *inode,
3910 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003911{
Chris Mason12fcfd22009-03-24 10:24:20 -04003912 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003913 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003914 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003915 int ret = 0;
3916 u64 last_committed = root->fs_info->last_trans_committed;
3917
3918 sb = inode->i_sb;
3919
Sage Weil3a5e1402009-04-02 16:49:40 -04003920 if (btrfs_test_opt(root, NOTREELOG)) {
3921 ret = 1;
3922 goto end_no_trans;
3923 }
3924
Chris Mason12fcfd22009-03-24 10:24:20 -04003925 if (root->fs_info->last_trans_log_full_commit >
3926 root->fs_info->last_trans_committed) {
3927 ret = 1;
3928 goto end_no_trans;
3929 }
3930
Yan, Zheng76dda932009-09-21 16:00:26 -04003931 if (root != BTRFS_I(inode)->root ||
3932 btrfs_root_refs(&root->root_item) == 0) {
3933 ret = 1;
3934 goto end_no_trans;
3935 }
3936
Chris Mason12fcfd22009-03-24 10:24:20 -04003937 ret = check_parent_dirs_for_sync(trans, inode, parent,
3938 sb, last_committed);
3939 if (ret)
3940 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003941
Josef Bacik22ee6982012-05-29 16:57:49 -04003942 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003943 ret = BTRFS_NO_LOG_SYNC;
3944 goto end_no_trans;
3945 }
3946
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003947 ret = start_log_trans(trans, root);
3948 if (ret)
3949 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003950
3951 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003952 if (ret)
3953 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003954
Chris Masonaf4176b2009-03-24 10:24:31 -04003955 /*
3956 * for regular files, if its inode is already on disk, we don't
3957 * have to worry about the parents at all. This is because
3958 * we can use the last_unlink_trans field to record renames
3959 * and other fun in this file.
3960 */
3961 if (S_ISREG(inode->i_mode) &&
3962 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003963 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3964 ret = 0;
3965 goto end_trans;
3966 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003967
3968 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003969 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003970 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003971 break;
3972
Chris Mason12fcfd22009-03-24 10:24:20 -04003973 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003974 if (root != BTRFS_I(inode)->root)
3975 break;
3976
Chris Mason12fcfd22009-03-24 10:24:20 -04003977 if (BTRFS_I(inode)->generation >
3978 root->fs_info->last_trans_committed) {
3979 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003980 if (ret)
3981 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003982 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003983 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003984 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003985
Josef Bacik6a912212010-11-20 09:48:00 +00003986 parent = dget_parent(parent);
3987 dput(old_parent);
3988 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003989 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003990 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003991end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003992 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003993 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003994 root->fs_info->last_trans_log_full_commit = trans->transid;
3995 ret = 1;
3996 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003997 btrfs_end_log_trans(root);
3998end_no_trans:
3999 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004000}
4001
4002/*
4003 * it is not safe to log dentry if the chunk root has added new
4004 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4005 * If this returns 1, you must commit the transaction to safely get your
4006 * data on disk.
4007 */
4008int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root, struct dentry *dentry)
4010{
Josef Bacik6a912212010-11-20 09:48:00 +00004011 struct dentry *parent = dget_parent(dentry);
4012 int ret;
4013
4014 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
4015 dput(parent);
4016
4017 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004018}
4019
4020/*
4021 * should be called during mount to recover any replay any log trees
4022 * from the FS
4023 */
4024int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4025{
4026 int ret;
4027 struct btrfs_path *path;
4028 struct btrfs_trans_handle *trans;
4029 struct btrfs_key key;
4030 struct btrfs_key found_key;
4031 struct btrfs_key tmp_key;
4032 struct btrfs_root *log;
4033 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4034 struct walk_control wc = {
4035 .process_func = process_one_buffer,
4036 .stage = 0,
4037 };
4038
Chris Masone02119d2008-09-05 16:13:11 -04004039 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004040 if (!path)
4041 return -ENOMEM;
4042
4043 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004044
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004045 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004046 if (IS_ERR(trans)) {
4047 ret = PTR_ERR(trans);
4048 goto error;
4049 }
Chris Masone02119d2008-09-05 16:13:11 -04004050
4051 wc.trans = trans;
4052 wc.pin = 1;
4053
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004054 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 if (ret) {
4056 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4057 "recovering log root tree.");
4058 goto error;
4059 }
Chris Masone02119d2008-09-05 16:13:11 -04004060
4061again:
4062 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4063 key.offset = (u64)-1;
4064 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4065
Chris Masond3977122009-01-05 21:25:51 -05004066 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004067 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004068
4069 if (ret < 0) {
4070 btrfs_error(fs_info, ret,
4071 "Couldn't find tree log root.");
4072 goto error;
4073 }
Chris Masone02119d2008-09-05 16:13:11 -04004074 if (ret > 0) {
4075 if (path->slots[0] == 0)
4076 break;
4077 path->slots[0]--;
4078 }
4079 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4080 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004081 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004082 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4083 break;
4084
Miao Xiecb517ea2013-05-15 07:48:19 +00004085 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (IS_ERR(log)) {
4087 ret = PTR_ERR(log);
4088 btrfs_error(fs_info, ret,
4089 "Couldn't read tree log root.");
4090 goto error;
4091 }
Chris Masone02119d2008-09-05 16:13:11 -04004092
4093 tmp_key.objectid = found_key.offset;
4094 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4095 tmp_key.offset = (u64)-1;
4096
4097 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (IS_ERR(wc.replay_dest)) {
4099 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004100 free_extent_buffer(log->node);
4101 free_extent_buffer(log->commit_root);
4102 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 btrfs_error(fs_info, ret, "Couldn't read target root "
4104 "for tree log recovery.");
4105 goto error;
4106 }
Chris Masone02119d2008-09-05 16:13:11 -04004107
Yan Zheng07d400a2009-01-06 11:42:00 -05004108 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004109 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004110 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004111
Josef Bacikb50c6e22013-04-25 15:55:30 -04004112 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004113 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4114 path);
Chris Masone02119d2008-09-05 16:13:11 -04004115 }
Chris Masone02119d2008-09-05 16:13:11 -04004116
4117 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004118 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004119 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004120 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004121 kfree(log);
4122
Josef Bacikb50c6e22013-04-25 15:55:30 -04004123 if (ret)
4124 goto error;
4125
Chris Masone02119d2008-09-05 16:13:11 -04004126 if (found_key.offset == 0)
4127 break;
4128 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004129 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004130
4131 /* step one is to pin it all, step two is to replay just inodes */
4132 if (wc.pin) {
4133 wc.pin = 0;
4134 wc.process_func = replay_one_buffer;
4135 wc.stage = LOG_WALK_REPLAY_INODES;
4136 goto again;
4137 }
4138 /* step three is to replay everything */
4139 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4140 wc.stage++;
4141 goto again;
4142 }
4143
4144 btrfs_free_path(path);
4145
Josef Bacikabefa552013-04-24 16:40:05 -04004146 /* step 4: commit the transaction, which also unpins the blocks */
4147 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4148 if (ret)
4149 return ret;
4150
Chris Masone02119d2008-09-05 16:13:11 -04004151 free_extent_buffer(log_root_tree->node);
4152 log_root_tree->log_root = NULL;
4153 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004154 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155
Josef Bacikabefa552013-04-24 16:40:05 -04004156 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004157error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004158 if (wc.trans)
4159 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 btrfs_free_path(path);
4161 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004162}
Chris Mason12fcfd22009-03-24 10:24:20 -04004163
4164/*
4165 * there are some corner cases where we want to force a full
4166 * commit instead of allowing a directory to be logged.
4167 *
4168 * They revolve around files there were unlinked from the directory, and
4169 * this function updates the parent directory so that a full commit is
4170 * properly done if it is fsync'd later after the unlinks are done.
4171 */
4172void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4173 struct inode *dir, struct inode *inode,
4174 int for_rename)
4175{
4176 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004177 * when we're logging a file, if it hasn't been renamed
4178 * or unlinked, and its inode is fully committed on disk,
4179 * we don't have to worry about walking up the directory chain
4180 * to log its parents.
4181 *
4182 * So, we use the last_unlink_trans field to put this transid
4183 * into the file. When the file is logged we check it and
4184 * don't log the parents if the file is fully on disk.
4185 */
4186 if (S_ISREG(inode->i_mode))
4187 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4188
4189 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004190 * if this directory was already logged any new
4191 * names for this file/dir will get recorded
4192 */
4193 smp_mb();
4194 if (BTRFS_I(dir)->logged_trans == trans->transid)
4195 return;
4196
4197 /*
4198 * if the inode we're about to unlink was logged,
4199 * the log will be properly updated for any new names
4200 */
4201 if (BTRFS_I(inode)->logged_trans == trans->transid)
4202 return;
4203
4204 /*
4205 * when renaming files across directories, if the directory
4206 * there we're unlinking from gets fsync'd later on, there's
4207 * no way to find the destination directory later and fsync it
4208 * properly. So, we have to be conservative and force commits
4209 * so the new name gets discovered.
4210 */
4211 if (for_rename)
4212 goto record;
4213
4214 /* we can safely do the unlink without any special recording */
4215 return;
4216
4217record:
4218 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4219}
4220
4221/*
4222 * Call this after adding a new name for a file and it will properly
4223 * update the log to reflect the new name.
4224 *
4225 * It will return zero if all goes well, and it will return 1 if a
4226 * full transaction commit is required.
4227 */
4228int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4229 struct inode *inode, struct inode *old_dir,
4230 struct dentry *parent)
4231{
4232 struct btrfs_root * root = BTRFS_I(inode)->root;
4233
4234 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004235 * this will force the logging code to walk the dentry chain
4236 * up for the file
4237 */
4238 if (S_ISREG(inode->i_mode))
4239 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4240
4241 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004242 * if this inode hasn't been logged and directory we're renaming it
4243 * from hasn't been logged, we don't need to log it
4244 */
4245 if (BTRFS_I(inode)->logged_trans <=
4246 root->fs_info->last_trans_committed &&
4247 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4248 root->fs_info->last_trans_committed))
4249 return 0;
4250
4251 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4252}
4253