blob: 1a79087c45751a25526c09c37bcabc741550c7f6 [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>
Josef Bacik5dc562c2012-08-17 13:14:17 -040021#include <linux/list_sort.h>
Chris Masone02119d2008-09-05 16:13:11 -040022#include "ctree.h"
23#include "transaction.h"
24#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070027#include "backref.h"
Chris Masone02119d2008-09-05 16:13:11 -040028#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050029#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070030#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
40
41/*
Chris Mason12fcfd22009-03-24 10:24:20 -040042 * directory trouble cases
43 *
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
48 *
49 * mkdir foo/some_dir
50 * normal commit
51 * rename foo/some_dir foo2/some_dir
52 * mkdir foo/some_dir
53 * fsync foo/some_dir/some_file
54 *
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
58 *
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
61 *
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
65 *
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
68 *
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
71 *
72 * mkdir f1/foo
73 * normal commit
74 * rm -rf f1/foo
75 * fsync(f1)
76 *
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
81 * ugly details.
82 */
83
84/*
Chris Masone02119d2008-09-05 16:13:11 -040085 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
89 *
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
92 */
93#define LOG_WALK_PIN_ONLY 0
94#define LOG_WALK_REPLAY_INODES 1
95#define LOG_WALK_REPLAY_ALL 2
96
Chris Mason12fcfd22009-03-24 10:24:20 -040097static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040098 struct btrfs_root *root, struct inode *inode,
99 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500100static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400103static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root,
105 struct btrfs_root *log,
106 struct btrfs_path *path,
107 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400108
109/*
110 * tree logging is a special write ahead log used to make sure that
111 * fsyncs and O_SYNCs can happen without doing full tree commits.
112 *
113 * Full tree commits are expensive because they require commonly
114 * modified blocks to be recowed, creating many dirty pages in the
115 * extent tree an 4x-6x higher write load than ext3.
116 *
117 * Instead of doing a tree commit on every fsync, we use the
118 * key ranges and transaction ids to find items for a given file or directory
119 * that have changed in this transaction. Those items are copied into
120 * a special tree (one per subvolume root), that tree is written to disk
121 * and then the fsync is considered complete.
122 *
123 * After a crash, items are copied out of the log-tree back into the
124 * subvolume tree. Any file data extents found are recorded in the extent
125 * allocation tree, and the log-tree freed.
126 *
127 * The log tree is read three times, once to pin down all the extents it is
128 * using in ram and once, once to create all the inodes logged in the tree
129 * and once to do all the other items.
130 */
131
132/*
Chris Masone02119d2008-09-05 16:13:11 -0400133 * start a sub transaction and setup the log tree
134 * this increments the log tree writer count to make the people
135 * syncing the tree wait for us to finish
136 */
137static int start_log_trans(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root)
139{
140 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400141 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500142
143 mutex_lock(&root->log_mutex);
144 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400145 if (!root->log_start_pid) {
146 root->log_start_pid = current->pid;
147 root->log_multiple_pids = false;
148 } else if (root->log_start_pid != current->pid) {
149 root->log_multiple_pids = true;
150 }
151
Miao Xie2ecb7922012-09-06 04:04:27 -0600152 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500153 atomic_inc(&root->log_writers);
154 mutex_unlock(&root->log_mutex);
155 return 0;
156 }
Josef Bacikff782e02009-10-08 15:30:04 -0400157 root->log_multiple_pids = false;
158 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400159 mutex_lock(&root->fs_info->tree_log_mutex);
160 if (!root->fs_info->log_root_tree) {
161 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (ret)
163 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400164 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400165 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400166 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400167 if (ret)
168 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400169 }
Chris Masone02119d2008-09-05 16:13:11 -0400170 mutex_unlock(&root->fs_info->tree_log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -0600171 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500172 atomic_inc(&root->log_writers);
173 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400174 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400175}
176
177/*
178 * returns 0 if there was a log transaction running and we were able
179 * to join, or returns -ENOENT if there were not transactions
180 * in progress
181 */
182static int join_running_log_trans(struct btrfs_root *root)
183{
184 int ret = -ENOENT;
185
186 smp_mb();
187 if (!root->log_root)
188 return -ENOENT;
189
Yan Zheng7237f182009-01-21 12:54:03 -0500190 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400191 if (root->log_root) {
192 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500193 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400194 }
Yan Zheng7237f182009-01-21 12:54:03 -0500195 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400196 return ret;
197}
198
199/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400200 * This either makes the current running log transaction wait
201 * until you call btrfs_end_log_trans() or it makes any future
202 * log transactions wait until you call btrfs_end_log_trans()
203 */
204int btrfs_pin_log_trans(struct btrfs_root *root)
205{
206 int ret = -ENOENT;
207
208 mutex_lock(&root->log_mutex);
209 atomic_inc(&root->log_writers);
210 mutex_unlock(&root->log_mutex);
211 return ret;
212}
213
214/*
Chris Masone02119d2008-09-05 16:13:11 -0400215 * indicate we're done making changes to the log tree
216 * and wake up anyone waiting to do a sync
217 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100218void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400219{
Yan Zheng7237f182009-01-21 12:54:03 -0500220 if (atomic_dec_and_test(&root->log_writers)) {
221 smp_mb();
222 if (waitqueue_active(&root->log_writer_wait))
223 wake_up(&root->log_writer_wait);
224 }
Chris Masone02119d2008-09-05 16:13:11 -0400225}
226
227
228/*
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
233 */
234struct walk_control {
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
237 */
238 int free;
239
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
242 */
243 int write;
244
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
247 */
248 int wait;
249
250 /* pin only walk, we record which extents on disk belong to the
251 * log trees
252 */
253 int pin;
254
255 /* what stage of the replay code we're currently in */
256 int stage;
257
258 /* the root we are currently replaying */
259 struct btrfs_root *replay_dest;
260
261 /* the trans handle for the current replay */
262 struct btrfs_trans_handle *trans;
263
264 /* the function that gets used to process blocks we find in the
265 * tree. Note the extent_buffer might not be up to date when it is
266 * passed in, and it must be checked or read if you need the data
267 * inside it
268 */
269 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
270 struct walk_control *wc, u64 gen);
271};
272
273/*
274 * process_func used to pin down extents, write them or wait on them
275 */
276static int process_one_buffer(struct btrfs_root *log,
277 struct extent_buffer *eb,
278 struct walk_control *wc, u64 gen)
279{
Josef Bacik04018de2009-04-03 10:14:18 -0400280 if (wc->pin)
Liu Bodcfac412012-12-27 09:01:20 +0000281 btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
Chris Masone688b7252011-10-31 20:52:39 -0400282 eb->start, eb->len);
Chris Masone02119d2008-09-05 16:13:11 -0400283
Chris Masonb9fab912012-05-06 07:23:47 -0400284 if (btrfs_buffer_uptodate(eb, gen, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -0400285 if (wc->write)
286 btrfs_write_tree_block(eb);
287 if (wc->wait)
288 btrfs_wait_tree_block_writeback(eb);
289 }
290 return 0;
291}
292
293/*
294 * Item overwrite used by replay and tree logging. eb, slot and key all refer
295 * to the src data we are copying out.
296 *
297 * root is the tree we are copying into, and path is a scratch
298 * path for use in this function (it should be released on entry and
299 * will be released on exit).
300 *
301 * If the key is already in the destination tree the existing item is
302 * overwritten. If the existing item isn't big enough, it is extended.
303 * If it is too large, it is truncated.
304 *
305 * If the key isn't in the destination yet, a new item is inserted.
306 */
307static noinline int overwrite_item(struct btrfs_trans_handle *trans,
308 struct btrfs_root *root,
309 struct btrfs_path *path,
310 struct extent_buffer *eb, int slot,
311 struct btrfs_key *key)
312{
313 int ret;
314 u32 item_size;
315 u64 saved_i_size = 0;
316 int save_old_i_size = 0;
317 unsigned long src_ptr;
318 unsigned long dst_ptr;
319 int overwrite_root = 0;
320
321 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
322 overwrite_root = 1;
323
324 item_size = btrfs_item_size_nr(eb, slot);
325 src_ptr = btrfs_item_ptr_offset(eb, slot);
326
327 /* look for the key in the destination tree */
328 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
329 if (ret == 0) {
330 char *src_copy;
331 char *dst_copy;
332 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
333 path->slots[0]);
334 if (dst_size != item_size)
335 goto insert;
336
337 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200338 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400339 return 0;
340 }
341 dst_copy = kmalloc(item_size, GFP_NOFS);
342 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000343 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200344 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000345 kfree(dst_copy);
346 kfree(src_copy);
347 return -ENOMEM;
348 }
Chris Masone02119d2008-09-05 16:13:11 -0400349
350 read_extent_buffer(eb, src_copy, src_ptr, item_size);
351
352 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
353 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
354 item_size);
355 ret = memcmp(dst_copy, src_copy, item_size);
356
357 kfree(dst_copy);
358 kfree(src_copy);
359 /*
360 * they have the same contents, just return, this saves
361 * us from cowing blocks in the destination tree and doing
362 * extra writes that may not have been done by a previous
363 * sync
364 */
365 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200366 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400367 return 0;
368 }
369
370 }
371insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200372 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400373 /* try to insert the key into the destination tree */
374 ret = btrfs_insert_empty_item(trans, root, path,
375 key, item_size);
376
377 /* make sure any existing item is the correct size */
378 if (ret == -EEXIST) {
379 u32 found_size;
380 found_size = btrfs_item_size_nr(path->nodes[0],
381 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100382 if (found_size > item_size)
Chris Masone02119d2008-09-05 16:13:11 -0400383 btrfs_truncate_item(trans, root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100384 else if (found_size < item_size)
385 btrfs_extend_item(trans, root, path,
386 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400387 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400388 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400389 }
390 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
391 path->slots[0]);
392
393 /* don't overwrite an existing inode if the generation number
394 * was logged as zero. This is done when the tree logging code
395 * is just logging an inode to make sure it exists after recovery.
396 *
397 * Also, don't overwrite i_size on directories during replay.
398 * log replay inserts and removes directory items based on the
399 * state of the tree found in the subvolume, and i_size is modified
400 * as it goes
401 */
402 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
403 struct btrfs_inode_item *src_item;
404 struct btrfs_inode_item *dst_item;
405
406 src_item = (struct btrfs_inode_item *)src_ptr;
407 dst_item = (struct btrfs_inode_item *)dst_ptr;
408
409 if (btrfs_inode_generation(eb, src_item) == 0)
410 goto no_copy;
411
412 if (overwrite_root &&
413 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
414 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
415 save_old_i_size = 1;
416 saved_i_size = btrfs_inode_size(path->nodes[0],
417 dst_item);
418 }
419 }
420
421 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
422 src_ptr, item_size);
423
424 if (save_old_i_size) {
425 struct btrfs_inode_item *dst_item;
426 dst_item = (struct btrfs_inode_item *)dst_ptr;
427 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
428 }
429
430 /* make sure the generation is filled in */
431 if (key->type == BTRFS_INODE_ITEM_KEY) {
432 struct btrfs_inode_item *dst_item;
433 dst_item = (struct btrfs_inode_item *)dst_ptr;
434 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
435 btrfs_set_inode_generation(path->nodes[0], dst_item,
436 trans->transid);
437 }
438 }
439no_copy:
440 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200441 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400442 return 0;
443}
444
445/*
446 * simple helper to read an inode off the disk from a given root
447 * This can only be called for subvolume roots and not for the log
448 */
449static noinline struct inode *read_one_inode(struct btrfs_root *root,
450 u64 objectid)
451{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400453 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400454
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 key.objectid = objectid;
456 key.type = BTRFS_INODE_ITEM_KEY;
457 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000458 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400459 if (IS_ERR(inode)) {
460 inode = NULL;
461 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400462 iput(inode);
463 inode = NULL;
464 }
465 return inode;
466}
467
468/* replays a single extent in 'eb' at 'slot' with 'key' into the
469 * subvolume 'root'. path is released on entry and should be released
470 * on exit.
471 *
472 * extents in the log tree have not been allocated out of the extent
473 * tree yet. So, this completes the allocation, taking a reference
474 * as required if the extent already exists or creating a new extent
475 * if it isn't in the extent allocation tree yet.
476 *
477 * The extent is inserted into the file, dropping any existing extents
478 * from the file that overlap the new one.
479 */
480static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct btrfs_path *path,
483 struct extent_buffer *eb, int slot,
484 struct btrfs_key *key)
485{
486 int found_type;
487 u64 mask = root->sectorsize - 1;
488 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400489 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500490 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400491 struct btrfs_file_extent_item *item;
492 struct inode *inode = NULL;
493 unsigned long size;
494 int ret = 0;
495
496 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
497 found_type = btrfs_file_extent_type(eb, item);
498
Yan Zhengd899e052008-10-30 14:25:28 -0400499 if (found_type == BTRFS_FILE_EXTENT_REG ||
500 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400501 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
502 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400504 extent_end = (start + size + mask) & ~mask;
505 } else {
506 ret = 0;
507 goto out;
508 }
509
510 inode = read_one_inode(root, key->objectid);
511 if (!inode) {
512 ret = -EIO;
513 goto out;
514 }
515
516 /*
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
520 */
Li Zefan33345d012011-04-20 10:31:50 +0800521 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400522 start, 0);
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524 if (ret == 0 &&
525 (found_type == BTRFS_FILE_EXTENT_REG ||
526 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400527 struct btrfs_file_extent_item cmp1;
528 struct btrfs_file_extent_item cmp2;
529 struct btrfs_file_extent_item *existing;
530 struct extent_buffer *leaf;
531
532 leaf = path->nodes[0];
533 existing = btrfs_item_ptr(leaf, path->slots[0],
534 struct btrfs_file_extent_item);
535
536 read_extent_buffer(eb, &cmp1, (unsigned long)item,
537 sizeof(cmp1));
538 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
539 sizeof(cmp2));
540
541 /*
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
544 */
545 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400547 goto out;
548 }
549 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200550 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400551
Yan Zheng07d400a2009-01-06 11:42:00 -0500552 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400553 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400554 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400555 BUG_ON(ret);
556
Yan Zheng07d400a2009-01-06 11:42:00 -0500557 if (found_type == BTRFS_FILE_EXTENT_REG ||
558 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400559 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500560 unsigned long dest_offset;
561 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400562
Yan Zheng07d400a2009-01-06 11:42:00 -0500563 ret = btrfs_insert_empty_item(trans, root, path, key,
564 sizeof(*item));
565 BUG_ON(ret);
566 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
567 path->slots[0]);
568 copy_extent_buffer(path->nodes[0], eb, dest_offset,
569 (unsigned long)item, sizeof(*item));
570
571 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
572 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
573 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400574 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500575
576 if (ins.objectid > 0) {
577 u64 csum_start;
578 u64 csum_end;
579 LIST_HEAD(ordered_sums);
580 /*
581 * is this extent already allocated in the extent
582 * allocation tree? If so, just add a reference
583 */
584 ret = btrfs_lookup_extent(root, ins.objectid,
585 ins.offset);
586 if (ret == 0) {
587 ret = btrfs_inc_extent_ref(trans, root,
588 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400589 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200590 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000591 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500592 } else {
593 /*
594 * insert the extent pointer in the extent
595 * allocation tree
596 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400597 ret = btrfs_alloc_logged_file_extent(trans,
598 root, root->root_key.objectid,
599 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500600 BUG_ON(ret);
601 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200602 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500603
604 if (btrfs_file_extent_compression(eb, item)) {
605 csum_start = ins.objectid;
606 csum_end = csum_start + ins.offset;
607 } else {
608 csum_start = ins.objectid +
609 btrfs_file_extent_offset(eb, item);
610 csum_end = csum_start +
611 btrfs_file_extent_num_bytes(eb, item);
612 }
613
614 ret = btrfs_lookup_csums_range(root->log_root,
615 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100616 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500617 BUG_ON(ret);
618 while (!list_empty(&ordered_sums)) {
619 struct btrfs_ordered_sum *sums;
620 sums = list_entry(ordered_sums.next,
621 struct btrfs_ordered_sum,
622 list);
623 ret = btrfs_csum_file_blocks(trans,
624 root->fs_info->csum_root,
625 sums);
626 BUG_ON(ret);
627 list_del(&sums->list);
628 kfree(sums);
629 }
630 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200631 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500632 }
633 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
634 /* inline extents are easy, we just overwrite them */
635 ret = overwrite_item(trans, root, path, eb, slot, key);
636 BUG_ON(ret);
637 }
638
639 inode_set_bytes(inode, saved_nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600640 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400641out:
642 if (inode)
643 iput(inode);
644 return ret;
645}
646
647/*
648 * when cleaning up conflicts between the directory names in the
649 * subvolume, directory names in the log and directory names in the
650 * inode back references, we may have to unlink inodes from directories.
651 *
652 * This is a helper function to do the unlink of a specific directory
653 * item
654 */
655static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct btrfs_path *path,
658 struct inode *dir,
659 struct btrfs_dir_item *di)
660{
661 struct inode *inode;
662 char *name;
663 int name_len;
664 struct extent_buffer *leaf;
665 struct btrfs_key location;
666 int ret;
667
668 leaf = path->nodes[0];
669
670 btrfs_dir_item_key_to_cpu(leaf, di, &location);
671 name_len = btrfs_dir_name_len(leaf, di);
672 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000673 if (!name)
674 return -ENOMEM;
675
Chris Masone02119d2008-09-05 16:13:11 -0400676 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200677 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400678
679 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000680 if (!inode) {
681 kfree(name);
682 return -EIO;
683 }
Chris Masone02119d2008-09-05 16:13:11 -0400684
Yan Zhengec051c02009-01-05 15:43:42 -0500685 ret = link_to_fixup_dir(trans, root, path, location.objectid);
686 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400687
Chris Masone02119d2008-09-05 16:13:11 -0400688 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500689 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400690 kfree(name);
691
692 iput(inode);
Chris Masonb6305562012-07-02 15:29:53 -0400693
694 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400695 return ret;
696}
697
698/*
699 * helper function to see if a given name and sequence number found
700 * in an inode back reference are already in a directory and correctly
701 * point to this inode
702 */
703static noinline int inode_in_dir(struct btrfs_root *root,
704 struct btrfs_path *path,
705 u64 dirid, u64 objectid, u64 index,
706 const char *name, int name_len)
707{
708 struct btrfs_dir_item *di;
709 struct btrfs_key location;
710 int match = 0;
711
712 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
713 index, name, name_len, 0);
714 if (di && !IS_ERR(di)) {
715 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
716 if (location.objectid != objectid)
717 goto out;
718 } else
719 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200720 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400721
722 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
723 if (di && !IS_ERR(di)) {
724 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
725 if (location.objectid != objectid)
726 goto out;
727 } else
728 goto out;
729 match = 1;
730out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200731 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400732 return match;
733}
734
735/*
736 * helper function to check a log tree for a named back reference in
737 * an inode. This is used to decide if a back reference that is
738 * found in the subvolume conflicts with what we find in the log.
739 *
740 * inode backreferences may have multiple refs in a single item,
741 * during replay we process one reference at a time, and we don't
742 * want to delete valid links to a file from the subvolume if that
743 * link is also in the log.
744 */
745static noinline int backref_in_log(struct btrfs_root *log,
746 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700747 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400748 char *name, int namelen)
749{
750 struct btrfs_path *path;
751 struct btrfs_inode_ref *ref;
752 unsigned long ptr;
753 unsigned long ptr_end;
754 unsigned long name_ptr;
755 int found_name_len;
756 int item_size;
757 int ret;
758 int match = 0;
759
760 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000761 if (!path)
762 return -ENOMEM;
763
Chris Masone02119d2008-09-05 16:13:11 -0400764 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
765 if (ret != 0)
766 goto out;
767
Chris Masone02119d2008-09-05 16:13:11 -0400768 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700769
770 if (key->type == BTRFS_INODE_EXTREF_KEY) {
771 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
772 name, namelen, NULL))
773 match = 1;
774
775 goto out;
776 }
777
778 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400779 ptr_end = ptr + item_size;
780 while (ptr < ptr_end) {
781 ref = (struct btrfs_inode_ref *)ptr;
782 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
783 if (found_name_len == namelen) {
784 name_ptr = (unsigned long)(ref + 1);
785 ret = memcmp_extent_buffer(path->nodes[0], name,
786 name_ptr, namelen);
787 if (ret == 0) {
788 match = 1;
789 goto out;
790 }
791 }
792 ptr = (unsigned long)(ref + 1) + found_name_len;
793 }
794out:
795 btrfs_free_path(path);
796 return match;
797}
798
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700799static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
800 struct btrfs_root *root,
801 struct btrfs_path *path,
802 struct btrfs_root *log_root,
803 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700804 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700805 u64 inode_objectid, u64 parent_objectid,
806 u64 ref_index, char *name, int namelen,
807 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700808{
809 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700810 char *victim_name;
811 int victim_name_len;
812 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700813 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700814 struct btrfs_key search_key;
815 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700816
Mark Fashehf1863732012-08-08 11:32:27 -0700817again:
818 /* Search old style refs */
819 search_key.objectid = inode_objectid;
820 search_key.type = BTRFS_INODE_REF_KEY;
821 search_key.offset = parent_objectid;
822 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700823 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700824 struct btrfs_inode_ref *victim_ref;
825 unsigned long ptr;
826 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700827
828 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700829
830 /* are we trying to overwrite a back ref for the root directory
831 * if so, just jump out, we're done
832 */
Mark Fashehf1863732012-08-08 11:32:27 -0700833 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700834 return 1;
835
836 /* check all the names in this back reference to see
837 * if they are in the log. if so, we allow them to stay
838 * otherwise they must be unlinked as a conflict
839 */
840 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
841 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
842 while (ptr < ptr_end) {
843 victim_ref = (struct btrfs_inode_ref *)ptr;
844 victim_name_len = btrfs_inode_ref_name_len(leaf,
845 victim_ref);
846 victim_name = kmalloc(victim_name_len, GFP_NOFS);
847 BUG_ON(!victim_name);
848
849 read_extent_buffer(leaf, victim_name,
850 (unsigned long)(victim_ref + 1),
851 victim_name_len);
852
Mark Fashehf1863732012-08-08 11:32:27 -0700853 if (!backref_in_log(log_root, &search_key,
854 parent_objectid,
855 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700856 victim_name_len)) {
857 btrfs_inc_nlink(inode);
858 btrfs_release_path(path);
859
860 ret = btrfs_unlink_inode(trans, root, dir,
861 inode, victim_name,
862 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700863 BUG_ON(ret);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700864 btrfs_run_delayed_items(trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -0700865 kfree(victim_name);
866 *search_done = 1;
867 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700868 }
869 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700870
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700871 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
872 }
873 BUG_ON(ret);
874
875 /*
876 * NOTE: we have searched root tree and checked the
877 * coresponding ref, it does not need to check again.
878 */
879 *search_done = 1;
880 }
881 btrfs_release_path(path);
882
Mark Fashehf1863732012-08-08 11:32:27 -0700883 /* Same search but for extended refs */
884 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
885 inode_objectid, parent_objectid, 0,
886 0);
887 if (!IS_ERR_OR_NULL(extref)) {
888 u32 item_size;
889 u32 cur_offset = 0;
890 unsigned long base;
891 struct inode *victim_parent;
892
893 leaf = path->nodes[0];
894
895 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
896 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
897
898 while (cur_offset < item_size) {
899 extref = (struct btrfs_inode_extref *)base + cur_offset;
900
901 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
902
903 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
904 goto next;
905
906 victim_name = kmalloc(victim_name_len, GFP_NOFS);
907 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
908 victim_name_len);
909
910 search_key.objectid = inode_objectid;
911 search_key.type = BTRFS_INODE_EXTREF_KEY;
912 search_key.offset = btrfs_extref_hash(parent_objectid,
913 victim_name,
914 victim_name_len);
915 ret = 0;
916 if (!backref_in_log(log_root, &search_key,
917 parent_objectid, victim_name,
918 victim_name_len)) {
919 ret = -ENOENT;
920 victim_parent = read_one_inode(root,
921 parent_objectid);
922 if (victim_parent) {
923 btrfs_inc_nlink(inode);
924 btrfs_release_path(path);
925
926 ret = btrfs_unlink_inode(trans, root,
927 victim_parent,
928 inode,
929 victim_name,
930 victim_name_len);
931 btrfs_run_delayed_items(trans, root);
932 }
933 BUG_ON(ret);
934 iput(victim_parent);
935 kfree(victim_name);
936 *search_done = 1;
937 goto again;
938 }
939 kfree(victim_name);
940 BUG_ON(ret);
941next:
942 cur_offset += victim_name_len + sizeof(*extref);
943 }
944 *search_done = 1;
945 }
946 btrfs_release_path(path);
947
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700948 /* look for a conflicting sequence number */
949 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -0700950 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700951 if (di && !IS_ERR(di)) {
952 ret = drop_one_dir_item(trans, root, path, dir, di);
953 BUG_ON(ret);
954 }
955 btrfs_release_path(path);
956
957 /* look for a conflicing name */
958 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
959 name, namelen, 0);
960 if (di && !IS_ERR(di)) {
961 ret = drop_one_dir_item(trans, root, path, dir, di);
962 BUG_ON(ret);
963 }
964 btrfs_release_path(path);
965
966 return 0;
967}
Chris Masone02119d2008-09-05 16:13:11 -0400968
Mark Fashehf1863732012-08-08 11:32:27 -0700969static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
970 u32 *namelen, char **name, u64 *index,
971 u64 *parent_objectid)
972{
973 struct btrfs_inode_extref *extref;
974
975 extref = (struct btrfs_inode_extref *)ref_ptr;
976
977 *namelen = btrfs_inode_extref_name_len(eb, extref);
978 *name = kmalloc(*namelen, GFP_NOFS);
979 if (*name == NULL)
980 return -ENOMEM;
981
982 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
983 *namelen);
984
985 *index = btrfs_inode_extref_index(eb, extref);
986 if (parent_objectid)
987 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
988
989 return 0;
990}
991
992static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
993 u32 *namelen, char **name, u64 *index)
994{
995 struct btrfs_inode_ref *ref;
996
997 ref = (struct btrfs_inode_ref *)ref_ptr;
998
999 *namelen = btrfs_inode_ref_name_len(eb, ref);
1000 *name = kmalloc(*namelen, GFP_NOFS);
1001 if (*name == NULL)
1002 return -ENOMEM;
1003
1004 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1005
1006 *index = btrfs_inode_ref_index(eb, ref);
1007
1008 return 0;
1009}
1010
Chris Masone02119d2008-09-05 16:13:11 -04001011/*
1012 * replay one inode back reference item found in the log tree.
1013 * eb, slot and key refer to the buffer and key found in the log tree.
1014 * root is the destination we are replaying into, and path is for temp
1015 * use by this function. (it should be released on return).
1016 */
1017static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1018 struct btrfs_root *root,
1019 struct btrfs_root *log,
1020 struct btrfs_path *path,
1021 struct extent_buffer *eb, int slot,
1022 struct btrfs_key *key)
1023{
liubo34f3e4f2011-08-06 08:35:23 +00001024 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001025 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -04001026 unsigned long ref_ptr;
1027 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +00001028 char *name;
1029 int namelen;
1030 int ret;
liuboc622ae62011-03-26 08:01:12 -04001031 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001032 int log_ref_ver = 0;
1033 u64 parent_objectid;
1034 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001035 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001036 int ref_struct_size;
1037
1038 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1039 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1040
1041 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1042 struct btrfs_inode_extref *r;
1043
1044 ref_struct_size = sizeof(struct btrfs_inode_extref);
1045 log_ref_ver = 1;
1046 r = (struct btrfs_inode_extref *)ref_ptr;
1047 parent_objectid = btrfs_inode_extref_parent(eb, r);
1048 } else {
1049 ref_struct_size = sizeof(struct btrfs_inode_ref);
1050 parent_objectid = key->offset;
1051 }
1052 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001053
Chris Masone02119d2008-09-05 16:13:11 -04001054 /*
1055 * it is possible that we didn't log all the parent directories
1056 * for a given inode. If we don't find the dir, just don't
1057 * copy the back ref in. The link count fixup code will take
1058 * care of the rest
1059 */
Mark Fashehf1863732012-08-08 11:32:27 -07001060 dir = read_one_inode(root, parent_objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001061 if (!dir)
1062 return -ENOENT;
1063
Mark Fashehf1863732012-08-08 11:32:27 -07001064 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001065 if (!inode) {
1066 iput(dir);
1067 return -EIO;
1068 }
Chris Masone02119d2008-09-05 16:13:11 -04001069
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001070 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001071 if (log_ref_ver) {
1072 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1073 &ref_index, &parent_objectid);
1074 /*
1075 * parent object can change from one array
1076 * item to another.
1077 */
1078 if (!dir)
1079 dir = read_one_inode(root, parent_objectid);
1080 if (!dir)
1081 return -ENOENT;
1082 } else {
1083 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1084 &ref_index);
1085 }
1086 if (ret)
1087 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001088
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001089 /* if we already have a perfect match, we're done */
1090 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001091 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001092 /*
1093 * look for a conflicting back reference in the
1094 * metadata. if we find one we have to unlink that name
1095 * of the file before we add our new link. Later on, we
1096 * overwrite any existing back reference, and we don't
1097 * want to create dangling pointers in the directory.
1098 */
Chris Masone02119d2008-09-05 16:13:11 -04001099
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001100 if (!search_done) {
1101 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001102 dir, inode, eb,
1103 inode_objectid,
1104 parent_objectid,
1105 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001106 &search_done);
1107 if (ret == 1)
1108 goto out;
1109 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001110 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001111
1112 /* insert our name */
1113 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001114 0, ref_index);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001115 BUG_ON(ret);
1116
1117 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001118 }
liuboc622ae62011-03-26 08:01:12 -04001119
Mark Fashehf1863732012-08-08 11:32:27 -07001120 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001121 kfree(name);
Mark Fashehf1863732012-08-08 11:32:27 -07001122 if (log_ref_ver) {
1123 iput(dir);
1124 dir = NULL;
1125 }
Chris Masone02119d2008-09-05 16:13:11 -04001126 }
Chris Masone02119d2008-09-05 16:13:11 -04001127
1128 /* finally write the back reference in the inode */
1129 ret = overwrite_item(trans, root, path, eb, slot, key);
1130 BUG_ON(ret);
1131
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001132out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001133 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001134 iput(dir);
1135 iput(inode);
1136 return 0;
1137}
1138
Yan, Zhengc71bf092009-11-12 09:34:40 +00001139static int insert_orphan_item(struct btrfs_trans_handle *trans,
1140 struct btrfs_root *root, u64 offset)
1141{
1142 int ret;
1143 ret = btrfs_find_orphan_item(root, offset);
1144 if (ret > 0)
1145 ret = btrfs_insert_orphan_item(trans, root, offset);
1146 return ret;
1147}
1148
Mark Fashehf1863732012-08-08 11:32:27 -07001149static int count_inode_extrefs(struct btrfs_root *root,
1150 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001151{
Mark Fashehf1863732012-08-08 11:32:27 -07001152 int ret = 0;
1153 int name_len;
1154 unsigned int nlink = 0;
1155 u32 item_size;
1156 u32 cur_offset = 0;
1157 u64 inode_objectid = btrfs_ino(inode);
1158 u64 offset = 0;
1159 unsigned long ptr;
1160 struct btrfs_inode_extref *extref;
1161 struct extent_buffer *leaf;
1162
1163 while (1) {
1164 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1165 &extref, &offset);
1166 if (ret)
1167 break;
1168
1169 leaf = path->nodes[0];
1170 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1171 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1172
1173 while (cur_offset < item_size) {
1174 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1175 name_len = btrfs_inode_extref_name_len(leaf, extref);
1176
1177 nlink++;
1178
1179 cur_offset += name_len + sizeof(*extref);
1180 }
1181
1182 offset++;
1183 btrfs_release_path(path);
1184 }
1185 btrfs_release_path(path);
1186
1187 if (ret < 0)
1188 return ret;
1189 return nlink;
1190}
1191
1192static int count_inode_refs(struct btrfs_root *root,
1193 struct inode *inode, struct btrfs_path *path)
1194{
Chris Masone02119d2008-09-05 16:13:11 -04001195 int ret;
1196 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001197 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001198 unsigned long ptr;
1199 unsigned long ptr_end;
1200 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001201 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001202
Li Zefan33345d012011-04-20 10:31:50 +08001203 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001204 key.type = BTRFS_INODE_REF_KEY;
1205 key.offset = (u64)-1;
1206
Chris Masond3977122009-01-05 21:25:51 -05001207 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001208 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1209 if (ret < 0)
1210 break;
1211 if (ret > 0) {
1212 if (path->slots[0] == 0)
1213 break;
1214 path->slots[0]--;
1215 }
1216 btrfs_item_key_to_cpu(path->nodes[0], &key,
1217 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001218 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001219 key.type != BTRFS_INODE_REF_KEY)
1220 break;
1221 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1222 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1223 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001224 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001225 struct btrfs_inode_ref *ref;
1226
1227 ref = (struct btrfs_inode_ref *)ptr;
1228 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1229 ref);
1230 ptr = (unsigned long)(ref + 1) + name_len;
1231 nlink++;
1232 }
1233
1234 if (key.offset == 0)
1235 break;
1236 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001237 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001238 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001239 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001240
1241 return nlink;
1242}
1243
1244/*
1245 * There are a few corners where the link count of the file can't
1246 * be properly maintained during replay. So, instead of adding
1247 * lots of complexity to the log code, we just scan the backrefs
1248 * for any file that has been through replay.
1249 *
1250 * The scan will update the link count on the inode to reflect the
1251 * number of back refs found. If it goes down to zero, the iput
1252 * will free the inode.
1253 */
1254static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1255 struct btrfs_root *root,
1256 struct inode *inode)
1257{
1258 struct btrfs_path *path;
1259 int ret;
1260 u64 nlink = 0;
1261 u64 ino = btrfs_ino(inode);
1262
1263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
1266
1267 ret = count_inode_refs(root, inode, path);
1268 if (ret < 0)
1269 goto out;
1270
1271 nlink = ret;
1272
1273 ret = count_inode_extrefs(root, inode, path);
1274 if (ret == -ENOENT)
1275 ret = 0;
1276
1277 if (ret < 0)
1278 goto out;
1279
1280 nlink += ret;
1281
1282 ret = 0;
1283
Chris Masone02119d2008-09-05 16:13:11 -04001284 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001285 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001286 btrfs_update_inode(trans, root, inode);
1287 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001288 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001289
Yan, Zhengc71bf092009-11-12 09:34:40 +00001290 if (inode->i_nlink == 0) {
1291 if (S_ISDIR(inode->i_mode)) {
1292 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001293 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001294 BUG_ON(ret);
1295 }
Li Zefan33345d012011-04-20 10:31:50 +08001296 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001297 BUG_ON(ret);
1298 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001299
Mark Fashehf1863732012-08-08 11:32:27 -07001300out:
1301 btrfs_free_path(path);
1302 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001303}
1304
1305static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1306 struct btrfs_root *root,
1307 struct btrfs_path *path)
1308{
1309 int ret;
1310 struct btrfs_key key;
1311 struct inode *inode;
1312
1313 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1314 key.type = BTRFS_ORPHAN_ITEM_KEY;
1315 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001316 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001317 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1318 if (ret < 0)
1319 break;
1320
1321 if (ret == 1) {
1322 if (path->slots[0] == 0)
1323 break;
1324 path->slots[0]--;
1325 }
1326
1327 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1328 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1329 key.type != BTRFS_ORPHAN_ITEM_KEY)
1330 break;
1331
1332 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001333 if (ret)
1334 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001335
David Sterbab3b4aa72011-04-21 01:20:15 +02001336 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001337 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001338 if (!inode)
1339 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001340
1341 ret = fixup_inode_link_count(trans, root, inode);
1342 BUG_ON(ret);
1343
1344 iput(inode);
1345
Chris Mason12fcfd22009-03-24 10:24:20 -04001346 /*
1347 * fixup on a directory may create new entries,
1348 * make sure we always look for the highset possible
1349 * offset
1350 */
1351 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001352 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001353 ret = 0;
1354out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001355 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001356 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001357}
1358
1359
1360/*
1361 * record a given inode in the fixup dir so we can check its link
1362 * count when replay is done. The link count is incremented here
1363 * so the inode won't go away until we check it
1364 */
1365static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1366 struct btrfs_root *root,
1367 struct btrfs_path *path,
1368 u64 objectid)
1369{
1370 struct btrfs_key key;
1371 int ret = 0;
1372 struct inode *inode;
1373
1374 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001375 if (!inode)
1376 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001377
1378 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1379 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1380 key.offset = objectid;
1381
1382 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1383
David Sterbab3b4aa72011-04-21 01:20:15 +02001384 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001385 if (ret == 0) {
1386 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001387 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001388 } else if (ret == -EEXIST) {
1389 ret = 0;
1390 } else {
1391 BUG();
1392 }
1393 iput(inode);
1394
1395 return ret;
1396}
1397
1398/*
1399 * when replaying the log for a directory, we only insert names
1400 * for inodes that actually exist. This means an fsync on a directory
1401 * does not implicitly fsync all the new files in it
1402 */
1403static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1404 struct btrfs_root *root,
1405 struct btrfs_path *path,
1406 u64 dirid, u64 index,
1407 char *name, int name_len, u8 type,
1408 struct btrfs_key *location)
1409{
1410 struct inode *inode;
1411 struct inode *dir;
1412 int ret;
1413
1414 inode = read_one_inode(root, location->objectid);
1415 if (!inode)
1416 return -ENOENT;
1417
1418 dir = read_one_inode(root, dirid);
1419 if (!dir) {
1420 iput(inode);
1421 return -EIO;
1422 }
1423 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1424
1425 /* FIXME, put inode into FIXUP list */
1426
1427 iput(inode);
1428 iput(dir);
1429 return ret;
1430}
1431
1432/*
1433 * take a single entry in a log directory item and replay it into
1434 * the subvolume.
1435 *
1436 * if a conflicting item exists in the subdirectory already,
1437 * the inode it points to is unlinked and put into the link count
1438 * fix up tree.
1439 *
1440 * If a name from the log points to a file or directory that does
1441 * not exist in the FS, it is skipped. fsyncs on directories
1442 * do not force down inodes inside that directory, just changes to the
1443 * names or unlinks in a directory.
1444 */
1445static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root,
1447 struct btrfs_path *path,
1448 struct extent_buffer *eb,
1449 struct btrfs_dir_item *di,
1450 struct btrfs_key *key)
1451{
1452 char *name;
1453 int name_len;
1454 struct btrfs_dir_item *dst_di;
1455 struct btrfs_key found_key;
1456 struct btrfs_key log_key;
1457 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001458 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001459 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001460 int ret;
1461
1462 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001463 if (!dir)
1464 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001465
1466 name_len = btrfs_dir_name_len(eb, di);
1467 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001468 if (!name)
1469 return -ENOMEM;
1470
Chris Masone02119d2008-09-05 16:13:11 -04001471 log_type = btrfs_dir_type(eb, di);
1472 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1473 name_len);
1474
1475 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001476 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1477 if (exists == 0)
1478 exists = 1;
1479 else
1480 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001481 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001482
Chris Masone02119d2008-09-05 16:13:11 -04001483 if (key->type == BTRFS_DIR_ITEM_KEY) {
1484 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1485 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001486 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001487 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1488 key->objectid,
1489 key->offset, name,
1490 name_len, 1);
1491 } else {
1492 BUG();
1493 }
David Sterbac7040052011-04-19 18:00:01 +02001494 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001495 /* we need a sequence number to insert, so we only
1496 * do inserts for the BTRFS_DIR_INDEX_KEY types
1497 */
1498 if (key->type != BTRFS_DIR_INDEX_KEY)
1499 goto out;
1500 goto insert;
1501 }
1502
1503 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1504 /* the existing item matches the logged item */
1505 if (found_key.objectid == log_key.objectid &&
1506 found_key.type == log_key.type &&
1507 found_key.offset == log_key.offset &&
1508 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1509 goto out;
1510 }
1511
1512 /*
1513 * don't drop the conflicting directory entry if the inode
1514 * for the new entry doesn't exist
1515 */
Chris Mason4bef0842008-09-08 11:18:08 -04001516 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001517 goto out;
1518
Chris Masone02119d2008-09-05 16:13:11 -04001519 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1520 BUG_ON(ret);
1521
1522 if (key->type == BTRFS_DIR_INDEX_KEY)
1523 goto insert;
1524out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001525 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001526 kfree(name);
1527 iput(dir);
1528 return 0;
1529
1530insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001531 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001532 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1533 name, name_len, log_type, &log_key);
1534
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001535 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001536 goto out;
1537}
1538
1539/*
1540 * find all the names in a directory item and reconcile them into
1541 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1542 * one name in a directory item, but the same code gets used for
1543 * both directory index types
1544 */
1545static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1546 struct btrfs_root *root,
1547 struct btrfs_path *path,
1548 struct extent_buffer *eb, int slot,
1549 struct btrfs_key *key)
1550{
1551 int ret;
1552 u32 item_size = btrfs_item_size_nr(eb, slot);
1553 struct btrfs_dir_item *di;
1554 int name_len;
1555 unsigned long ptr;
1556 unsigned long ptr_end;
1557
1558 ptr = btrfs_item_ptr_offset(eb, slot);
1559 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001560 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001561 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001562 if (verify_dir_item(root, eb, di))
1563 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001564 name_len = btrfs_dir_name_len(eb, di);
1565 ret = replay_one_name(trans, root, path, eb, di, key);
1566 BUG_ON(ret);
1567 ptr = (unsigned long)(di + 1);
1568 ptr += name_len;
1569 }
1570 return 0;
1571}
1572
1573/*
1574 * directory replay has two parts. There are the standard directory
1575 * items in the log copied from the subvolume, and range items
1576 * created in the log while the subvolume was logged.
1577 *
1578 * The range items tell us which parts of the key space the log
1579 * is authoritative for. During replay, if a key in the subvolume
1580 * directory is in a logged range item, but not actually in the log
1581 * that means it was deleted from the directory before the fsync
1582 * and should be removed.
1583 */
1584static noinline int find_dir_range(struct btrfs_root *root,
1585 struct btrfs_path *path,
1586 u64 dirid, int key_type,
1587 u64 *start_ret, u64 *end_ret)
1588{
1589 struct btrfs_key key;
1590 u64 found_end;
1591 struct btrfs_dir_log_item *item;
1592 int ret;
1593 int nritems;
1594
1595 if (*start_ret == (u64)-1)
1596 return 1;
1597
1598 key.objectid = dirid;
1599 key.type = key_type;
1600 key.offset = *start_ret;
1601
1602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1603 if (ret < 0)
1604 goto out;
1605 if (ret > 0) {
1606 if (path->slots[0] == 0)
1607 goto out;
1608 path->slots[0]--;
1609 }
1610 if (ret != 0)
1611 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1612
1613 if (key.type != key_type || key.objectid != dirid) {
1614 ret = 1;
1615 goto next;
1616 }
1617 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1618 struct btrfs_dir_log_item);
1619 found_end = btrfs_dir_log_end(path->nodes[0], item);
1620
1621 if (*start_ret >= key.offset && *start_ret <= found_end) {
1622 ret = 0;
1623 *start_ret = key.offset;
1624 *end_ret = found_end;
1625 goto out;
1626 }
1627 ret = 1;
1628next:
1629 /* check the next slot in the tree to see if it is a valid item */
1630 nritems = btrfs_header_nritems(path->nodes[0]);
1631 if (path->slots[0] >= nritems) {
1632 ret = btrfs_next_leaf(root, path);
1633 if (ret)
1634 goto out;
1635 } else {
1636 path->slots[0]++;
1637 }
1638
1639 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1640
1641 if (key.type != key_type || key.objectid != dirid) {
1642 ret = 1;
1643 goto out;
1644 }
1645 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1646 struct btrfs_dir_log_item);
1647 found_end = btrfs_dir_log_end(path->nodes[0], item);
1648 *start_ret = key.offset;
1649 *end_ret = found_end;
1650 ret = 0;
1651out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001652 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001653 return ret;
1654}
1655
1656/*
1657 * this looks for a given directory item in the log. If the directory
1658 * item is not in the log, the item is removed and the inode it points
1659 * to is unlinked
1660 */
1661static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1662 struct btrfs_root *root,
1663 struct btrfs_root *log,
1664 struct btrfs_path *path,
1665 struct btrfs_path *log_path,
1666 struct inode *dir,
1667 struct btrfs_key *dir_key)
1668{
1669 int ret;
1670 struct extent_buffer *eb;
1671 int slot;
1672 u32 item_size;
1673 struct btrfs_dir_item *di;
1674 struct btrfs_dir_item *log_di;
1675 int name_len;
1676 unsigned long ptr;
1677 unsigned long ptr_end;
1678 char *name;
1679 struct inode *inode;
1680 struct btrfs_key location;
1681
1682again:
1683 eb = path->nodes[0];
1684 slot = path->slots[0];
1685 item_size = btrfs_item_size_nr(eb, slot);
1686 ptr = btrfs_item_ptr_offset(eb, slot);
1687 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001688 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001689 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001690 if (verify_dir_item(root, eb, di)) {
1691 ret = -EIO;
1692 goto out;
1693 }
1694
Chris Masone02119d2008-09-05 16:13:11 -04001695 name_len = btrfs_dir_name_len(eb, di);
1696 name = kmalloc(name_len, GFP_NOFS);
1697 if (!name) {
1698 ret = -ENOMEM;
1699 goto out;
1700 }
1701 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1702 name_len);
1703 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001704 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001705 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1706 dir_key->objectid,
1707 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001708 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001709 log_di = btrfs_lookup_dir_index_item(trans, log,
1710 log_path,
1711 dir_key->objectid,
1712 dir_key->offset,
1713 name, name_len, 0);
1714 }
David Sterbac7040052011-04-19 18:00:01 +02001715 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001716 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001717 btrfs_release_path(path);
1718 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001719 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001720 if (!inode) {
1721 kfree(name);
1722 return -EIO;
1723 }
Chris Masone02119d2008-09-05 16:13:11 -04001724
1725 ret = link_to_fixup_dir(trans, root,
1726 path, location.objectid);
1727 BUG_ON(ret);
1728 btrfs_inc_nlink(inode);
1729 ret = btrfs_unlink_inode(trans, root, dir, inode,
1730 name, name_len);
1731 BUG_ON(ret);
Chris Masonb6305562012-07-02 15:29:53 -04001732
1733 btrfs_run_delayed_items(trans, root);
1734
Chris Masone02119d2008-09-05 16:13:11 -04001735 kfree(name);
1736 iput(inode);
1737
1738 /* there might still be more names under this key
1739 * check and repeat if required
1740 */
1741 ret = btrfs_search_slot(NULL, root, dir_key, path,
1742 0, 0);
1743 if (ret == 0)
1744 goto again;
1745 ret = 0;
1746 goto out;
1747 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001748 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001749 kfree(name);
1750
1751 ptr = (unsigned long)(di + 1);
1752 ptr += name_len;
1753 }
1754 ret = 0;
1755out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001756 btrfs_release_path(path);
1757 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001758 return ret;
1759}
1760
1761/*
1762 * deletion replay happens before we copy any new directory items
1763 * out of the log or out of backreferences from inodes. It
1764 * scans the log to find ranges of keys that log is authoritative for,
1765 * and then scans the directory to find items in those ranges that are
1766 * not present in the log.
1767 *
1768 * Anything we don't find in the log is unlinked and removed from the
1769 * directory.
1770 */
1771static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1772 struct btrfs_root *root,
1773 struct btrfs_root *log,
1774 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001775 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001776{
1777 u64 range_start;
1778 u64 range_end;
1779 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1780 int ret = 0;
1781 struct btrfs_key dir_key;
1782 struct btrfs_key found_key;
1783 struct btrfs_path *log_path;
1784 struct inode *dir;
1785
1786 dir_key.objectid = dirid;
1787 dir_key.type = BTRFS_DIR_ITEM_KEY;
1788 log_path = btrfs_alloc_path();
1789 if (!log_path)
1790 return -ENOMEM;
1791
1792 dir = read_one_inode(root, dirid);
1793 /* it isn't an error if the inode isn't there, that can happen
1794 * because we replay the deletes before we copy in the inode item
1795 * from the log
1796 */
1797 if (!dir) {
1798 btrfs_free_path(log_path);
1799 return 0;
1800 }
1801again:
1802 range_start = 0;
1803 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001804 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001805 if (del_all)
1806 range_end = (u64)-1;
1807 else {
1808 ret = find_dir_range(log, path, dirid, key_type,
1809 &range_start, &range_end);
1810 if (ret != 0)
1811 break;
1812 }
Chris Masone02119d2008-09-05 16:13:11 -04001813
1814 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001815 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001816 int nritems;
1817 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1818 0, 0);
1819 if (ret < 0)
1820 goto out;
1821
1822 nritems = btrfs_header_nritems(path->nodes[0]);
1823 if (path->slots[0] >= nritems) {
1824 ret = btrfs_next_leaf(root, path);
1825 if (ret)
1826 break;
1827 }
1828 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1829 path->slots[0]);
1830 if (found_key.objectid != dirid ||
1831 found_key.type != dir_key.type)
1832 goto next_type;
1833
1834 if (found_key.offset > range_end)
1835 break;
1836
1837 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001838 log_path, dir,
1839 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001840 BUG_ON(ret);
1841 if (found_key.offset == (u64)-1)
1842 break;
1843 dir_key.offset = found_key.offset + 1;
1844 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001845 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001846 if (range_end == (u64)-1)
1847 break;
1848 range_start = range_end + 1;
1849 }
1850
1851next_type:
1852 ret = 0;
1853 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1854 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1855 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001856 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001857 goto again;
1858 }
1859out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001860 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001861 btrfs_free_path(log_path);
1862 iput(dir);
1863 return ret;
1864}
1865
1866/*
1867 * the process_func used to replay items from the log tree. This
1868 * gets called in two different stages. The first stage just looks
1869 * for inodes and makes sure they are all copied into the subvolume.
1870 *
1871 * The second stage copies all the other item types from the log into
1872 * the subvolume. The two stage approach is slower, but gets rid of
1873 * lots of complexity around inodes referencing other inodes that exist
1874 * only in the log (references come from either directory items or inode
1875 * back refs).
1876 */
1877static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1878 struct walk_control *wc, u64 gen)
1879{
1880 int nritems;
1881 struct btrfs_path *path;
1882 struct btrfs_root *root = wc->replay_dest;
1883 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001884 int level;
1885 int i;
1886 int ret;
1887
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001888 ret = btrfs_read_buffer(eb, gen);
1889 if (ret)
1890 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001891
1892 level = btrfs_header_level(eb);
1893
1894 if (level != 0)
1895 return 0;
1896
1897 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001898 if (!path)
1899 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001900
1901 nritems = btrfs_header_nritems(eb);
1902 for (i = 0; i < nritems; i++) {
1903 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001904
1905 /* inode keys are done during the first stage */
1906 if (key.type == BTRFS_INODE_ITEM_KEY &&
1907 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001908 struct btrfs_inode_item *inode_item;
1909 u32 mode;
1910
1911 inode_item = btrfs_item_ptr(eb, i,
1912 struct btrfs_inode_item);
1913 mode = btrfs_inode_mode(eb, inode_item);
1914 if (S_ISDIR(mode)) {
1915 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001916 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001917 BUG_ON(ret);
1918 }
1919 ret = overwrite_item(wc->trans, root, path,
1920 eb, i, &key);
1921 BUG_ON(ret);
1922
Yan, Zhengc71bf092009-11-12 09:34:40 +00001923 /* for regular files, make sure corresponding
1924 * orhpan item exist. extents past the new EOF
1925 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001926 */
1927 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001928 ret = insert_orphan_item(wc->trans, root,
1929 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001930 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001931 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001932
Chris Masone02119d2008-09-05 16:13:11 -04001933 ret = link_to_fixup_dir(wc->trans, root,
1934 path, key.objectid);
1935 BUG_ON(ret);
1936 }
1937 if (wc->stage < LOG_WALK_REPLAY_ALL)
1938 continue;
1939
1940 /* these keys are simply copied */
1941 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1942 ret = overwrite_item(wc->trans, root, path,
1943 eb, i, &key);
1944 BUG_ON(ret);
1945 } else if (key.type == BTRFS_INODE_REF_KEY) {
1946 ret = add_inode_ref(wc->trans, root, log, path,
1947 eb, i, &key);
1948 BUG_ON(ret && ret != -ENOENT);
Mark Fashehf1863732012-08-08 11:32:27 -07001949 } else if (key.type == BTRFS_INODE_EXTREF_KEY) {
1950 ret = add_inode_ref(wc->trans, root, log, path,
1951 eb, i, &key);
1952 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001953 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1954 ret = replay_one_extent(wc->trans, root, path,
1955 eb, i, &key);
1956 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001957 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1958 key.type == BTRFS_DIR_INDEX_KEY) {
1959 ret = replay_one_dir_item(wc->trans, root, path,
1960 eb, i, &key);
1961 BUG_ON(ret);
1962 }
1963 }
1964 btrfs_free_path(path);
1965 return 0;
1966}
1967
Chris Masond3977122009-01-05 21:25:51 -05001968static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001969 struct btrfs_root *root,
1970 struct btrfs_path *path, int *level,
1971 struct walk_control *wc)
1972{
1973 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001974 u64 bytenr;
1975 u64 ptr_gen;
1976 struct extent_buffer *next;
1977 struct extent_buffer *cur;
1978 struct extent_buffer *parent;
1979 u32 blocksize;
1980 int ret = 0;
1981
1982 WARN_ON(*level < 0);
1983 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1984
Chris Masond3977122009-01-05 21:25:51 -05001985 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001986 WARN_ON(*level < 0);
1987 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1988 cur = path->nodes[*level];
1989
1990 if (btrfs_header_level(cur) != *level)
1991 WARN_ON(1);
1992
1993 if (path->slots[*level] >=
1994 btrfs_header_nritems(cur))
1995 break;
1996
1997 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1998 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1999 blocksize = btrfs_level_size(root, *level - 1);
2000
2001 parent = path->nodes[*level];
2002 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002003
2004 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002005 if (!next)
2006 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002007
Chris Masone02119d2008-09-05 16:13:11 -04002008 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002009 ret = wc->process_func(root, next, wc, ptr_gen);
2010 if (ret)
2011 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002012
Chris Masone02119d2008-09-05 16:13:11 -04002013 path->slots[*level]++;
2014 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002015 ret = btrfs_read_buffer(next, ptr_gen);
2016 if (ret) {
2017 free_extent_buffer(next);
2018 return ret;
2019 }
Chris Masone02119d2008-09-05 16:13:11 -04002020
2021 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002022 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002023 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002024 btrfs_wait_tree_block_writeback(next);
2025 btrfs_tree_unlock(next);
2026
Chris Masone02119d2008-09-05 16:13:11 -04002027 WARN_ON(root_owner !=
2028 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002029 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002030 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002031 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002032 }
2033 free_extent_buffer(next);
2034 continue;
2035 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002036 ret = btrfs_read_buffer(next, ptr_gen);
2037 if (ret) {
2038 free_extent_buffer(next);
2039 return ret;
2040 }
Chris Masone02119d2008-09-05 16:13:11 -04002041
2042 WARN_ON(*level <= 0);
2043 if (path->nodes[*level-1])
2044 free_extent_buffer(path->nodes[*level-1]);
2045 path->nodes[*level-1] = next;
2046 *level = btrfs_header_level(next);
2047 path->slots[*level] = 0;
2048 cond_resched();
2049 }
2050 WARN_ON(*level < 0);
2051 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2052
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002053 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002054
2055 cond_resched();
2056 return 0;
2057}
2058
Chris Masond3977122009-01-05 21:25:51 -05002059static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002060 struct btrfs_root *root,
2061 struct btrfs_path *path, int *level,
2062 struct walk_control *wc)
2063{
2064 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002065 int i;
2066 int slot;
2067 int ret;
2068
Chris Masond3977122009-01-05 21:25:51 -05002069 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002070 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002071 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002072 path->slots[i]++;
2073 *level = i;
2074 WARN_ON(*level == 0);
2075 return 0;
2076 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002077 struct extent_buffer *parent;
2078 if (path->nodes[*level] == root->node)
2079 parent = path->nodes[*level];
2080 else
2081 parent = path->nodes[*level + 1];
2082
2083 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002084 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002085 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002086 if (ret)
2087 return ret;
2088
Chris Masone02119d2008-09-05 16:13:11 -04002089 if (wc->free) {
2090 struct extent_buffer *next;
2091
2092 next = path->nodes[*level];
2093
2094 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002095 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002096 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002097 btrfs_wait_tree_block_writeback(next);
2098 btrfs_tree_unlock(next);
2099
Chris Masone02119d2008-09-05 16:13:11 -04002100 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002101 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002102 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002103 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04002104 BUG_ON(ret);
2105 }
2106 free_extent_buffer(path->nodes[*level]);
2107 path->nodes[*level] = NULL;
2108 *level = i + 1;
2109 }
2110 }
2111 return 1;
2112}
2113
2114/*
2115 * drop the reference count on the tree rooted at 'snap'. This traverses
2116 * the tree freeing any blocks that have a ref count of zero after being
2117 * decremented.
2118 */
2119static int walk_log_tree(struct btrfs_trans_handle *trans,
2120 struct btrfs_root *log, struct walk_control *wc)
2121{
2122 int ret = 0;
2123 int wret;
2124 int level;
2125 struct btrfs_path *path;
2126 int i;
2127 int orig_level;
2128
2129 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002130 if (!path)
2131 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002132
2133 level = btrfs_header_level(log->node);
2134 orig_level = level;
2135 path->nodes[level] = log->node;
2136 extent_buffer_get(log->node);
2137 path->slots[level] = 0;
2138
Chris Masond3977122009-01-05 21:25:51 -05002139 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002140 wret = walk_down_log_tree(trans, log, path, &level, wc);
2141 if (wret > 0)
2142 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002143 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002144 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002145 goto out;
2146 }
Chris Masone02119d2008-09-05 16:13:11 -04002147
2148 wret = walk_up_log_tree(trans, log, path, &level, wc);
2149 if (wret > 0)
2150 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002151 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002152 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002153 goto out;
2154 }
Chris Masone02119d2008-09-05 16:13:11 -04002155 }
2156
2157 /* was the root node processed? if not, catch it here */
2158 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002159 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002160 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002161 if (ret)
2162 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002163 if (wc->free) {
2164 struct extent_buffer *next;
2165
2166 next = path->nodes[orig_level];
2167
2168 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002169 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002170 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04002171 btrfs_wait_tree_block_writeback(next);
2172 btrfs_tree_unlock(next);
2173
Chris Masone02119d2008-09-05 16:13:11 -04002174 WARN_ON(log->root_key.objectid !=
2175 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002176 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002177 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002178 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002179 }
2180 }
2181
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182out:
Chris Masone02119d2008-09-05 16:13:11 -04002183 for (i = 0; i <= orig_level; i++) {
2184 if (path->nodes[i]) {
2185 free_extent_buffer(path->nodes[i]);
2186 path->nodes[i] = NULL;
2187 }
2188 }
2189 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002190 return ret;
2191}
2192
Yan Zheng7237f182009-01-21 12:54:03 -05002193/*
2194 * helper function to update the item for a given subvolumes log root
2195 * in the tree of log roots
2196 */
2197static int update_log_root(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *log)
2199{
2200 int ret;
2201
2202 if (log->log_transid == 1) {
2203 /* insert root item on the first sync */
2204 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2205 &log->root_key, &log->root_item);
2206 } else {
2207 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2208 &log->root_key, &log->root_item);
2209 }
2210 return ret;
2211}
2212
Chris Mason12fcfd22009-03-24 10:24:20 -04002213static int wait_log_commit(struct btrfs_trans_handle *trans,
2214 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002215{
2216 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002217 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002218
Yan Zheng7237f182009-01-21 12:54:03 -05002219 /*
2220 * we only allow two pending log transactions at a time,
2221 * so we know that if ours is more than 2 older than the
2222 * current transaction, we're done
2223 */
Chris Masone02119d2008-09-05 16:13:11 -04002224 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002225 prepare_to_wait(&root->log_commit_wait[index],
2226 &wait, TASK_UNINTERRUPTIBLE);
2227 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002228
2229 if (root->fs_info->last_trans_log_full_commit !=
2230 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002231 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002232 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002233
Yan Zheng7237f182009-01-21 12:54:03 -05002234 finish_wait(&root->log_commit_wait[index], &wait);
2235 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002236 } while (root->fs_info->last_trans_log_full_commit !=
2237 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002238 atomic_read(&root->log_commit[index]));
2239 return 0;
2240}
2241
Jeff Mahoney143bede2012-03-01 14:56:26 +01002242static void wait_for_writer(struct btrfs_trans_handle *trans,
2243 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002244{
2245 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002246 while (root->fs_info->last_trans_log_full_commit !=
2247 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002248 prepare_to_wait(&root->log_writer_wait,
2249 &wait, TASK_UNINTERRUPTIBLE);
2250 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002251 if (root->fs_info->last_trans_log_full_commit !=
2252 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002253 schedule();
2254 mutex_lock(&root->log_mutex);
2255 finish_wait(&root->log_writer_wait, &wait);
2256 }
Chris Masone02119d2008-09-05 16:13:11 -04002257}
2258
2259/*
2260 * btrfs_sync_log does sends a given tree log down to the disk and
2261 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002262 * you know that any inodes previously logged are safely on disk only
2263 * if it returns 0.
2264 *
2265 * Any other return value means you need to call btrfs_commit_transaction.
2266 * Some of the edge cases for fsyncing directories that have had unlinks
2267 * or renames done in the past mean that sometimes the only safe
2268 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2269 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002270 */
2271int btrfs_sync_log(struct btrfs_trans_handle *trans,
2272 struct btrfs_root *root)
2273{
Yan Zheng7237f182009-01-21 12:54:03 -05002274 int index1;
2275 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002276 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002277 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002278 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002279 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002280 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002281
Yan Zheng7237f182009-01-21 12:54:03 -05002282 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002283 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002284 index1 = root->log_transid % 2;
2285 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002286 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002287 mutex_unlock(&root->log_mutex);
2288 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002289 }
Yan Zheng7237f182009-01-21 12:54:03 -05002290 atomic_set(&root->log_commit[index1], 1);
2291
2292 /* wait for previous tree log sync to complete */
2293 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002294 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002295 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002296 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002297 /* when we're on an ssd, just kick the log commit out */
2298 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002299 mutex_unlock(&root->log_mutex);
2300 schedule_timeout_uninterruptible(1);
2301 mutex_lock(&root->log_mutex);
2302 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002303 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002304 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002305 break;
2306 }
Chris Masond0c803c2008-09-11 16:17:57 -04002307
Chris Mason12fcfd22009-03-24 10:24:20 -04002308 /* bail out if we need to do a full commit */
2309 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2310 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002311 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002312 mutex_unlock(&root->log_mutex);
2313 goto out;
2314 }
2315
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002316 if (log_transid % 2 == 0)
2317 mark = EXTENT_DIRTY;
2318 else
2319 mark = EXTENT_NEW;
2320
Chris Mason690587d2009-10-13 13:29:19 -04002321 /* we start IO on all the marked extents here, but we don't actually
2322 * wait for them until later.
2323 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002324 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325 if (ret) {
2326 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002327 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002328 mutex_unlock(&root->log_mutex);
2329 goto out;
2330 }
Yan Zheng7237f182009-01-21 12:54:03 -05002331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002333
Yan Zheng7237f182009-01-21 12:54:03 -05002334 root->log_transid++;
2335 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002336 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002337 smp_mb();
2338 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002339 * IO has been started, blocks of the log tree have WRITTEN flag set
2340 * in their headers. new modifications of the log will be written to
2341 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002342 */
2343 mutex_unlock(&root->log_mutex);
2344
2345 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002346 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002347 atomic_inc(&log_root_tree->log_writers);
2348 mutex_unlock(&log_root_tree->log_mutex);
2349
2350 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002351
2352 mutex_lock(&log_root_tree->log_mutex);
2353 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2354 smp_mb();
2355 if (waitqueue_active(&log_root_tree->log_writer_wait))
2356 wake_up(&log_root_tree->log_writer_wait);
2357 }
2358
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002359 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002360 if (ret != -ENOSPC) {
2361 btrfs_abort_transaction(trans, root, ret);
2362 mutex_unlock(&log_root_tree->log_mutex);
2363 goto out;
2364 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002365 root->fs_info->last_trans_log_full_commit = trans->transid;
2366 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002367 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002368 mutex_unlock(&log_root_tree->log_mutex);
2369 ret = -EAGAIN;
2370 goto out;
2371 }
2372
Yan Zheng7237f182009-01-21 12:54:03 -05002373 index2 = log_root_tree->log_transid % 2;
2374 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002375 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002376 wait_log_commit(trans, log_root_tree,
2377 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002378 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002379 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002380 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002381 goto out;
2382 }
2383 atomic_set(&log_root_tree->log_commit[index2], 1);
2384
Chris Mason12fcfd22009-03-24 10:24:20 -04002385 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2386 wait_log_commit(trans, log_root_tree,
2387 log_root_tree->log_transid - 1);
2388 }
Yan Zheng7237f182009-01-21 12:54:03 -05002389
Chris Mason12fcfd22009-03-24 10:24:20 -04002390 wait_for_writer(trans, log_root_tree);
2391
2392 /*
2393 * now that we've moved on to the tree of log tree roots,
2394 * check the full commit flag again
2395 */
2396 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002397 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002398 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002399 mutex_unlock(&log_root_tree->log_mutex);
2400 ret = -EAGAIN;
2401 goto out_wake_log_root;
2402 }
Yan Zheng7237f182009-01-21 12:54:03 -05002403
2404 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002405 &log_root_tree->dirty_log_pages,
2406 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002407 if (ret) {
2408 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002409 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002410 mutex_unlock(&log_root_tree->log_mutex);
2411 goto out_wake_log_root;
2412 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002413 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002414 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002415
David Sterba6c417612011-04-13 15:41:04 +02002416 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002417 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002418 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002419 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002420
Yan Zheng7237f182009-01-21 12:54:03 -05002421 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002422 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002423
2424 mutex_unlock(&log_root_tree->log_mutex);
2425
2426 /*
2427 * nobody else is going to jump in and write the the ctree
2428 * super here because the log_commit atomic below is protecting
2429 * us. We must be called with a transaction handle pinning
2430 * the running transaction open, so a full commit can't hop
2431 * in and cause problems either.
2432 */
Arne Jansena2de7332011-03-08 14:14:00 +01002433 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002434 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002435 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002436 if (ret) {
2437 btrfs_abort_transaction(trans, root, ret);
2438 goto out_wake_log_root;
2439 }
Yan Zheng7237f182009-01-21 12:54:03 -05002440
Chris Mason257c62e2009-10-13 13:21:08 -04002441 mutex_lock(&root->log_mutex);
2442 if (root->last_log_commit < log_transid)
2443 root->last_log_commit = log_transid;
2444 mutex_unlock(&root->log_mutex);
2445
Chris Mason12fcfd22009-03-24 10:24:20 -04002446out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002447 atomic_set(&log_root_tree->log_commit[index2], 0);
2448 smp_mb();
2449 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2450 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002451out:
Yan Zheng7237f182009-01-21 12:54:03 -05002452 atomic_set(&root->log_commit[index1], 0);
2453 smp_mb();
2454 if (waitqueue_active(&root->log_commit_wait[index1]))
2455 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002456 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002457}
2458
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002459static void free_log_tree(struct btrfs_trans_handle *trans,
2460 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002461{
2462 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002463 u64 start;
2464 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002465 struct walk_control wc = {
2466 .free = 1,
2467 .process_func = process_one_buffer
2468 };
2469
Chris Masone02119d2008-09-05 16:13:11 -04002470 ret = walk_log_tree(trans, log, &wc);
2471 BUG_ON(ret);
2472
Chris Masond3977122009-01-05 21:25:51 -05002473 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002474 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002475 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2476 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002477 if (ret)
2478 break;
2479
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002480 clear_extent_bits(&log->dirty_log_pages, start, end,
2481 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002482 }
2483
Josef Bacik2ab28f32012-10-12 15:27:49 -04002484 /*
2485 * We may have short-circuited the log tree with the full commit logic
2486 * and left ordered extents on our list, so clear these out to keep us
2487 * from leaking inodes and memory.
2488 */
2489 btrfs_free_logged_extents(log, 0);
2490 btrfs_free_logged_extents(log, 1);
2491
Yan Zheng7237f182009-01-21 12:54:03 -05002492 free_extent_buffer(log->node);
2493 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002494}
2495
2496/*
2497 * free all the extents used by the tree log. This should be called
2498 * at commit time of the full transaction
2499 */
2500int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2501{
2502 if (root->log_root) {
2503 free_log_tree(trans, root->log_root);
2504 root->log_root = NULL;
2505 }
2506 return 0;
2507}
2508
2509int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2510 struct btrfs_fs_info *fs_info)
2511{
2512 if (fs_info->log_root_tree) {
2513 free_log_tree(trans, fs_info->log_root_tree);
2514 fs_info->log_root_tree = NULL;
2515 }
Chris Masone02119d2008-09-05 16:13:11 -04002516 return 0;
2517}
2518
2519/*
Chris Masone02119d2008-09-05 16:13:11 -04002520 * If both a file and directory are logged, and unlinks or renames are
2521 * mixed in, we have a few interesting corners:
2522 *
2523 * create file X in dir Y
2524 * link file X to X.link in dir Y
2525 * fsync file X
2526 * unlink file X but leave X.link
2527 * fsync dir Y
2528 *
2529 * After a crash we would expect only X.link to exist. But file X
2530 * didn't get fsync'd again so the log has back refs for X and X.link.
2531 *
2532 * We solve this by removing directory entries and inode backrefs from the
2533 * log when a file that was logged in the current transaction is
2534 * unlinked. Any later fsync will include the updated log entries, and
2535 * we'll be able to reconstruct the proper directory items from backrefs.
2536 *
2537 * This optimizations allows us to avoid relogging the entire inode
2538 * or the entire directory.
2539 */
2540int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2541 struct btrfs_root *root,
2542 const char *name, int name_len,
2543 struct inode *dir, u64 index)
2544{
2545 struct btrfs_root *log;
2546 struct btrfs_dir_item *di;
2547 struct btrfs_path *path;
2548 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002549 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002550 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002551 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002552
Chris Mason3a5f1d42008-09-11 15:53:37 -04002553 if (BTRFS_I(dir)->logged_trans < trans->transid)
2554 return 0;
2555
Chris Masone02119d2008-09-05 16:13:11 -04002556 ret = join_running_log_trans(root);
2557 if (ret)
2558 return 0;
2559
2560 mutex_lock(&BTRFS_I(dir)->log_mutex);
2561
2562 log = root->log_root;
2563 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002564 if (!path) {
2565 err = -ENOMEM;
2566 goto out_unlock;
2567 }
liubo2a29edc2011-01-26 06:22:08 +00002568
Li Zefan33345d012011-04-20 10:31:50 +08002569 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002570 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002571 if (IS_ERR(di)) {
2572 err = PTR_ERR(di);
2573 goto fail;
2574 }
2575 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002576 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2577 bytes_del += name_len;
2578 BUG_ON(ret);
2579 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002580 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002581 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002582 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002583 if (IS_ERR(di)) {
2584 err = PTR_ERR(di);
2585 goto fail;
2586 }
2587 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002588 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2589 bytes_del += name_len;
2590 BUG_ON(ret);
2591 }
2592
2593 /* update the directory size in the log to reflect the names
2594 * we have removed
2595 */
2596 if (bytes_del) {
2597 struct btrfs_key key;
2598
Li Zefan33345d012011-04-20 10:31:50 +08002599 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002600 key.offset = 0;
2601 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002602 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002603
2604 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002605 if (ret < 0) {
2606 err = ret;
2607 goto fail;
2608 }
Chris Masone02119d2008-09-05 16:13:11 -04002609 if (ret == 0) {
2610 struct btrfs_inode_item *item;
2611 u64 i_size;
2612
2613 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2614 struct btrfs_inode_item);
2615 i_size = btrfs_inode_size(path->nodes[0], item);
2616 if (i_size > bytes_del)
2617 i_size -= bytes_del;
2618 else
2619 i_size = 0;
2620 btrfs_set_inode_size(path->nodes[0], item, i_size);
2621 btrfs_mark_buffer_dirty(path->nodes[0]);
2622 } else
2623 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002624 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002625 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002626fail:
Chris Masone02119d2008-09-05 16:13:11 -04002627 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002628out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002629 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002630 if (ret == -ENOSPC) {
2631 root->fs_info->last_trans_log_full_commit = trans->transid;
2632 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002633 } else if (ret < 0)
2634 btrfs_abort_transaction(trans, root, ret);
2635
Chris Mason12fcfd22009-03-24 10:24:20 -04002636 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002637
Andi Kleen411fc6b2010-10-29 15:14:31 -04002638 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002639}
2640
2641/* see comments for btrfs_del_dir_entries_in_log */
2642int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root,
2644 const char *name, int name_len,
2645 struct inode *inode, u64 dirid)
2646{
2647 struct btrfs_root *log;
2648 u64 index;
2649 int ret;
2650
Chris Mason3a5f1d42008-09-11 15:53:37 -04002651 if (BTRFS_I(inode)->logged_trans < trans->transid)
2652 return 0;
2653
Chris Masone02119d2008-09-05 16:13:11 -04002654 ret = join_running_log_trans(root);
2655 if (ret)
2656 return 0;
2657 log = root->log_root;
2658 mutex_lock(&BTRFS_I(inode)->log_mutex);
2659
Li Zefan33345d012011-04-20 10:31:50 +08002660 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002661 dirid, &index);
2662 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002663 if (ret == -ENOSPC) {
2664 root->fs_info->last_trans_log_full_commit = trans->transid;
2665 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002666 } else if (ret < 0 && ret != -ENOENT)
2667 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002668 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002669
Chris Masone02119d2008-09-05 16:13:11 -04002670 return ret;
2671}
2672
2673/*
2674 * creates a range item in the log for 'dirid'. first_offset and
2675 * last_offset tell us which parts of the key space the log should
2676 * be considered authoritative for.
2677 */
2678static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2679 struct btrfs_root *log,
2680 struct btrfs_path *path,
2681 int key_type, u64 dirid,
2682 u64 first_offset, u64 last_offset)
2683{
2684 int ret;
2685 struct btrfs_key key;
2686 struct btrfs_dir_log_item *item;
2687
2688 key.objectid = dirid;
2689 key.offset = first_offset;
2690 if (key_type == BTRFS_DIR_ITEM_KEY)
2691 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2692 else
2693 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2694 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002695 if (ret)
2696 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002697
2698 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2699 struct btrfs_dir_log_item);
2700 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2701 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002702 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002703 return 0;
2704}
2705
2706/*
2707 * log all the items included in the current transaction for a given
2708 * directory. This also creates the range items in the log tree required
2709 * to replay anything deleted before the fsync
2710 */
2711static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2712 struct btrfs_root *root, struct inode *inode,
2713 struct btrfs_path *path,
2714 struct btrfs_path *dst_path, int key_type,
2715 u64 min_offset, u64 *last_offset_ret)
2716{
2717 struct btrfs_key min_key;
2718 struct btrfs_key max_key;
2719 struct btrfs_root *log = root->log_root;
2720 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002721 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002722 int ret;
2723 int i;
2724 int nritems;
2725 u64 first_offset = min_offset;
2726 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002727 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002728
2729 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002730 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002731 max_key.offset = (u64)-1;
2732 max_key.type = key_type;
2733
Li Zefan33345d012011-04-20 10:31:50 +08002734 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002735 min_key.type = key_type;
2736 min_key.offset = min_offset;
2737
2738 path->keep_locks = 1;
2739
2740 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00002741 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002742
2743 /*
2744 * we didn't find anything from this transaction, see if there
2745 * is anything at all
2746 */
Li Zefan33345d012011-04-20 10:31:50 +08002747 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2748 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002749 min_key.type = key_type;
2750 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002751 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002752 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2753 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002754 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002755 return ret;
2756 }
Li Zefan33345d012011-04-20 10:31:50 +08002757 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002758
2759 /* if ret == 0 there are items for this type,
2760 * create a range to tell us the last key of this type.
2761 * otherwise, there are no items in this directory after
2762 * *min_offset, and we create a range to indicate that.
2763 */
2764 if (ret == 0) {
2765 struct btrfs_key tmp;
2766 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2767 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002768 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002769 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002770 }
2771 goto done;
2772 }
2773
2774 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002775 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002776 if (ret == 0) {
2777 struct btrfs_key tmp;
2778 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2779 if (key_type == tmp.type) {
2780 first_offset = tmp.offset;
2781 ret = overwrite_item(trans, log, dst_path,
2782 path->nodes[0], path->slots[0],
2783 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002784 if (ret) {
2785 err = ret;
2786 goto done;
2787 }
Chris Masone02119d2008-09-05 16:13:11 -04002788 }
2789 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002790 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002791
2792 /* find the first key from this transaction again */
2793 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2794 if (ret != 0) {
2795 WARN_ON(1);
2796 goto done;
2797 }
2798
2799 /*
2800 * we have a block from this transaction, log every item in it
2801 * from our directory
2802 */
Chris Masond3977122009-01-05 21:25:51 -05002803 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002804 struct btrfs_key tmp;
2805 src = path->nodes[0];
2806 nritems = btrfs_header_nritems(src);
2807 for (i = path->slots[0]; i < nritems; i++) {
2808 btrfs_item_key_to_cpu(src, &min_key, i);
2809
Li Zefan33345d012011-04-20 10:31:50 +08002810 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002811 goto done;
2812 ret = overwrite_item(trans, log, dst_path, src, i,
2813 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002814 if (ret) {
2815 err = ret;
2816 goto done;
2817 }
Chris Masone02119d2008-09-05 16:13:11 -04002818 }
2819 path->slots[0] = nritems;
2820
2821 /*
2822 * look ahead to the next item and see if it is also
2823 * from this directory and from this transaction
2824 */
2825 ret = btrfs_next_leaf(root, path);
2826 if (ret == 1) {
2827 last_offset = (u64)-1;
2828 goto done;
2829 }
2830 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002831 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002832 last_offset = (u64)-1;
2833 goto done;
2834 }
2835 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2836 ret = overwrite_item(trans, log, dst_path,
2837 path->nodes[0], path->slots[0],
2838 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002839 if (ret)
2840 err = ret;
2841 else
2842 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002843 goto done;
2844 }
2845 }
2846done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002847 btrfs_release_path(path);
2848 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002849
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002850 if (err == 0) {
2851 *last_offset_ret = last_offset;
2852 /*
2853 * insert the log range keys to indicate where the log
2854 * is valid
2855 */
2856 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002857 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002858 if (ret)
2859 err = ret;
2860 }
2861 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002862}
2863
2864/*
2865 * logging directories is very similar to logging inodes, We find all the items
2866 * from the current transaction and write them to the log.
2867 *
2868 * The recovery code scans the directory in the subvolume, and if it finds a
2869 * key in the range logged that is not present in the log tree, then it means
2870 * that dir entry was unlinked during the transaction.
2871 *
2872 * In order for that scan to work, we must include one key smaller than
2873 * the smallest logged by this transaction and one key larger than the largest
2874 * key logged by this transaction.
2875 */
2876static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2877 struct btrfs_root *root, struct inode *inode,
2878 struct btrfs_path *path,
2879 struct btrfs_path *dst_path)
2880{
2881 u64 min_key;
2882 u64 max_key;
2883 int ret;
2884 int key_type = BTRFS_DIR_ITEM_KEY;
2885
2886again:
2887 min_key = 0;
2888 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002889 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002890 ret = log_dir_items(trans, root, inode, path,
2891 dst_path, key_type, min_key,
2892 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002893 if (ret)
2894 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002895 if (max_key == (u64)-1)
2896 break;
2897 min_key = max_key + 1;
2898 }
2899
2900 if (key_type == BTRFS_DIR_ITEM_KEY) {
2901 key_type = BTRFS_DIR_INDEX_KEY;
2902 goto again;
2903 }
2904 return 0;
2905}
2906
2907/*
2908 * a helper function to drop items from the log before we relog an
2909 * inode. max_key_type indicates the highest item type to remove.
2910 * This cannot be run for file data extents because it does not
2911 * free the extents they point to.
2912 */
2913static int drop_objectid_items(struct btrfs_trans_handle *trans,
2914 struct btrfs_root *log,
2915 struct btrfs_path *path,
2916 u64 objectid, int max_key_type)
2917{
2918 int ret;
2919 struct btrfs_key key;
2920 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04002921 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002922
2923 key.objectid = objectid;
2924 key.type = max_key_type;
2925 key.offset = (u64)-1;
2926
Chris Masond3977122009-01-05 21:25:51 -05002927 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002928 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002929 BUG_ON(ret == 0);
2930 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002931 break;
2932
2933 if (path->slots[0] == 0)
2934 break;
2935
2936 path->slots[0]--;
2937 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2938 path->slots[0]);
2939
2940 if (found_key.objectid != objectid)
2941 break;
2942
Josef Bacik18ec90d2012-09-28 11:56:28 -04002943 found_key.offset = 0;
2944 found_key.type = 0;
2945 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
2946 &start_slot);
2947
2948 ret = btrfs_del_items(trans, log, path, start_slot,
2949 path->slots[0] - start_slot + 1);
2950 /*
2951 * If start slot isn't 0 then we don't need to re-search, we've
2952 * found the last guy with the objectid in this tree.
2953 */
2954 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002955 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002956 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002957 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002958 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04002959 if (ret > 0)
2960 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002961 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002962}
2963
Josef Bacik94edf4a2012-09-25 14:56:25 -04002964static void fill_inode_item(struct btrfs_trans_handle *trans,
2965 struct extent_buffer *leaf,
2966 struct btrfs_inode_item *item,
2967 struct inode *inode, int log_inode_only)
2968{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002969 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04002970
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002971 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002972
2973 if (log_inode_only) {
2974 /* set the generation to zero so the recover code
2975 * can tell the difference between an logging
2976 * just to say 'this inode exists' and a logging
2977 * to say 'update this inode with these values'
2978 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002979 btrfs_set_token_inode_generation(leaf, item, 0, &token);
2980 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002981 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002982 btrfs_set_token_inode_generation(leaf, item,
2983 BTRFS_I(inode)->generation,
2984 &token);
2985 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002986 }
2987
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002988 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2989 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2990 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2991 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
2992
2993 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2994 inode->i_atime.tv_sec, &token);
2995 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2996 inode->i_atime.tv_nsec, &token);
2997
2998 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2999 inode->i_mtime.tv_sec, &token);
3000 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3001 inode->i_mtime.tv_nsec, &token);
3002
3003 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3004 inode->i_ctime.tv_sec, &token);
3005 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3006 inode->i_ctime.tv_nsec, &token);
3007
3008 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3009 &token);
3010
3011 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3012 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3013 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3014 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3015 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003016}
3017
Josef Bacika95249b2012-10-11 16:17:34 -04003018static int log_inode_item(struct btrfs_trans_handle *trans,
3019 struct btrfs_root *log, struct btrfs_path *path,
3020 struct inode *inode)
3021{
3022 struct btrfs_inode_item *inode_item;
3023 struct btrfs_key key;
3024 int ret;
3025
3026 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3027 ret = btrfs_insert_empty_item(trans, log, path, &key,
3028 sizeof(*inode_item));
3029 if (ret && ret != -EEXIST)
3030 return ret;
3031 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3032 struct btrfs_inode_item);
3033 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3034 btrfs_release_path(path);
3035 return 0;
3036}
3037
Chris Mason31ff1cd2008-09-11 16:17:57 -04003038static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003039 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003040 struct btrfs_path *dst_path,
3041 struct extent_buffer *src,
3042 int start_slot, int nr, int inode_only)
3043{
3044 unsigned long src_offset;
3045 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003046 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003047 struct btrfs_file_extent_item *extent;
3048 struct btrfs_inode_item *inode_item;
3049 int ret;
3050 struct btrfs_key *ins_keys;
3051 u32 *ins_sizes;
3052 char *ins_data;
3053 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003054 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003055 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003056
3057 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003058
3059 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3060 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003061 if (!ins_data)
3062 return -ENOMEM;
3063
Chris Mason31ff1cd2008-09-11 16:17:57 -04003064 ins_sizes = (u32 *)ins_data;
3065 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3066
3067 for (i = 0; i < nr; i++) {
3068 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3069 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3070 }
3071 ret = btrfs_insert_empty_items(trans, log, dst_path,
3072 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003073 if (ret) {
3074 kfree(ins_data);
3075 return ret;
3076 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003078 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003079 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3080 dst_path->slots[0]);
3081
3082 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3083
Josef Bacik94edf4a2012-09-25 14:56:25 -04003084 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003085 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3086 dst_path->slots[0],
3087 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003088 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3089 inode, inode_only == LOG_INODE_EXISTS);
3090 } else {
3091 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3092 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003093 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003094
Chris Mason31ff1cd2008-09-11 16:17:57 -04003095 /* take a reference on file data extents so that truncates
3096 * or deletes of this inode don't have to relog the inode
3097 * again
3098 */
Liu Bod2794402012-08-29 01:07:56 -06003099 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3100 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003101 int found_type;
3102 extent = btrfs_item_ptr(src, start_slot + i,
3103 struct btrfs_file_extent_item);
3104
liubo8e531cd2011-05-06 10:36:09 +08003105 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3106 continue;
3107
Chris Mason31ff1cd2008-09-11 16:17:57 -04003108 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003109 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110 u64 ds, dl, cs, cl;
3111 ds = btrfs_file_extent_disk_bytenr(src,
3112 extent);
3113 /* ds == 0 is a hole */
3114 if (ds == 0)
3115 continue;
3116
3117 dl = btrfs_file_extent_disk_num_bytes(src,
3118 extent);
3119 cs = btrfs_file_extent_offset(src, extent);
3120 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003121 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003122 if (btrfs_file_extent_compression(src,
3123 extent)) {
3124 cs = 0;
3125 cl = dl;
3126 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003127
3128 ret = btrfs_lookup_csums_range(
3129 log->fs_info->csum_root,
3130 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003131 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003133 }
3134 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003135 }
3136
3137 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003138 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003139 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003140
3141 /*
3142 * we have to do this after the loop above to avoid changing the
3143 * log tree while trying to change the log tree.
3144 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003145 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003146 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003147 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3148 struct btrfs_ordered_sum,
3149 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003150 if (!ret)
3151 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003152 list_del(&sums->list);
3153 kfree(sums);
3154 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003155 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003156}
3157
Josef Bacik5dc562c2012-08-17 13:14:17 -04003158static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3159{
3160 struct extent_map *em1, *em2;
3161
3162 em1 = list_entry(a, struct extent_map, list);
3163 em2 = list_entry(b, struct extent_map, list);
3164
3165 if (em1->start < em2->start)
3166 return -1;
3167 else if (em1->start > em2->start)
3168 return 1;
3169 return 0;
3170}
3171
Josef Bacik70c8a912012-10-11 16:54:30 -04003172static int drop_adjacent_extents(struct btrfs_trans_handle *trans,
3173 struct btrfs_root *root, struct inode *inode,
3174 struct extent_map *em,
3175 struct btrfs_path *path)
3176{
3177 struct btrfs_file_extent_item *fi;
3178 struct extent_buffer *leaf;
3179 struct btrfs_key key, new_key;
3180 struct btrfs_map_token token;
3181 u64 extent_end;
3182 u64 extent_offset = 0;
3183 int extent_type;
3184 int del_slot = 0;
3185 int del_nr = 0;
3186 int ret = 0;
3187
3188 while (1) {
3189 btrfs_init_map_token(&token);
3190 leaf = path->nodes[0];
3191 path->slots[0]++;
3192 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3193 if (del_nr) {
3194 ret = btrfs_del_items(trans, root, path,
3195 del_slot, del_nr);
3196 if (ret)
3197 return ret;
3198 del_nr = 0;
3199 }
3200
3201 ret = btrfs_next_leaf_write(trans, root, path, 1);
3202 if (ret < 0)
3203 return ret;
3204 if (ret > 0)
3205 return 0;
3206 leaf = path->nodes[0];
3207 }
3208
3209 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3210 if (key.objectid != btrfs_ino(inode) ||
3211 key.type != BTRFS_EXTENT_DATA_KEY ||
3212 key.offset >= em->start + em->len)
3213 break;
3214
3215 fi = btrfs_item_ptr(leaf, path->slots[0],
3216 struct btrfs_file_extent_item);
3217 extent_type = btrfs_token_file_extent_type(leaf, fi, &token);
3218 if (extent_type == BTRFS_FILE_EXTENT_REG ||
3219 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
3220 extent_offset = btrfs_token_file_extent_offset(leaf,
3221 fi, &token);
3222 extent_end = key.offset +
3223 btrfs_token_file_extent_num_bytes(leaf, fi,
3224 &token);
3225 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3226 extent_end = key.offset +
3227 btrfs_file_extent_inline_len(leaf, fi);
3228 } else {
3229 BUG();
3230 }
3231
3232 if (extent_end <= em->len + em->start) {
3233 if (!del_nr) {
3234 del_slot = path->slots[0];
3235 }
3236 del_nr++;
3237 continue;
3238 }
3239
3240 /*
3241 * Ok so we'll ignore previous items if we log a new extent,
3242 * which can lead to overlapping extents, so if we have an
3243 * existing extent we want to adjust we _have_ to check the next
3244 * guy to make sure we even need this extent anymore, this keeps
3245 * us from panicing in set_item_key_safe.
3246 */
3247 if (path->slots[0] < btrfs_header_nritems(leaf) - 1) {
3248 struct btrfs_key tmp_key;
3249
3250 btrfs_item_key_to_cpu(leaf, &tmp_key,
3251 path->slots[0] + 1);
3252 if (tmp_key.objectid == btrfs_ino(inode) &&
3253 tmp_key.type == BTRFS_EXTENT_DATA_KEY &&
3254 tmp_key.offset <= em->start + em->len) {
3255 if (!del_nr)
3256 del_slot = path->slots[0];
3257 del_nr++;
3258 continue;
3259 }
3260 }
3261
3262 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
3263 memcpy(&new_key, &key, sizeof(new_key));
3264 new_key.offset = em->start + em->len;
3265 btrfs_set_item_key_safe(trans, root, path, &new_key);
3266 extent_offset += em->start + em->len - key.offset;
3267 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset,
3268 &token);
3269 btrfs_set_token_file_extent_num_bytes(leaf, fi, extent_end -
3270 (em->start + em->len),
3271 &token);
3272 btrfs_mark_buffer_dirty(leaf);
3273 }
3274
3275 if (del_nr)
3276 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
3277
3278 return ret;
3279}
Josef Bacik5dc562c2012-08-17 13:14:17 -04003280
3281static int log_one_extent(struct btrfs_trans_handle *trans,
3282 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003283 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003284{
3285 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003286 struct btrfs_file_extent_item *fi;
3287 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003288 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003289 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003290 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003291 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003292 u64 mod_start = em->mod_start;
3293 u64 mod_len = em->mod_len;
3294 u64 csum_offset;
3295 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003296 u64 extent_offset = em->start - em->orig_start;
3297 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003298 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003299 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003300 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003301
Josef Bacik70c8a912012-10-11 16:54:30 -04003302 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003303 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003304 key.objectid = btrfs_ino(inode);
3305 key.type = BTRFS_EXTENT_DATA_KEY;
3306 key.offset = em->start;
3307 path->really_keep_locks = 1;
3308
3309 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
3310 if (ret && ret != -EEXIST) {
3311 path->really_keep_locks = 0;
3312 return ret;
3313 }
3314 leaf = path->nodes[0];
3315 fi = btrfs_item_ptr(leaf, path->slots[0],
3316 struct btrfs_file_extent_item);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003317 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3318 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003319 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3320 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003321 btrfs_set_token_file_extent_type(leaf, fi,
3322 BTRFS_FILE_EXTENT_PREALLOC,
3323 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003324 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003325 btrfs_set_token_file_extent_type(leaf, fi,
3326 BTRFS_FILE_EXTENT_REG,
3327 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003328 if (em->block_start == 0)
3329 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003330 }
3331
Josef Bacik70c8a912012-10-11 16:54:30 -04003332 block_len = max(em->block_len, em->orig_block_len);
3333 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003334 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3335 em->block_start,
3336 &token);
3337 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3338 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003339 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003340 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3341 em->block_start -
3342 extent_offset, &token);
3343 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3344 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003345 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003346 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3347 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3348 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003349 }
3350
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003351 btrfs_set_token_file_extent_offset(leaf, fi,
3352 em->start - em->orig_start,
3353 &token);
3354 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
3355 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->len, &token);
3356 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3357 &token);
3358 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3359 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003360 btrfs_mark_buffer_dirty(leaf);
3361
3362 /*
3363 * Have to check the extent to the right of us to make sure it doesn't
3364 * fall in our current range. We're ok if the previous extent is in our
3365 * range since the recovery stuff will run us in key order and thus just
3366 * drop the part we overwrote.
3367 */
3368 ret = drop_adjacent_extents(trans, log, inode, em, path);
3369 btrfs_release_path(path);
3370 path->really_keep_locks = 0;
3371 if (ret) {
3372 return ret;
3373 }
3374
3375 if (skip_csum)
3376 return 0;
3377
Liu Bo192000d2013-01-06 03:38:22 +00003378 if (em->compress_type) {
3379 csum_offset = 0;
3380 csum_len = block_len;
3381 }
3382
Josef Bacik2ab28f32012-10-12 15:27:49 -04003383 /*
3384 * First check and see if our csums are on our outstanding ordered
3385 * extents.
3386 */
3387again:
3388 spin_lock_irq(&log->log_extents_lock[index]);
3389 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3390 struct btrfs_ordered_sum *sum;
3391
3392 if (!mod_len)
3393 break;
3394
3395 if (ordered->inode != inode)
3396 continue;
3397
3398 if (ordered->file_offset + ordered->len <= mod_start ||
3399 mod_start + mod_len <= ordered->file_offset)
3400 continue;
3401
3402 /*
3403 * We are going to copy all the csums on this ordered extent, so
3404 * go ahead and adjust mod_start and mod_len in case this
3405 * ordered extent has already been logged.
3406 */
3407 if (ordered->file_offset > mod_start) {
3408 if (ordered->file_offset + ordered->len >=
3409 mod_start + mod_len)
3410 mod_len = ordered->file_offset - mod_start;
3411 /*
3412 * If we have this case
3413 *
3414 * |--------- logged extent ---------|
3415 * |----- ordered extent ----|
3416 *
3417 * Just don't mess with mod_start and mod_len, we'll
3418 * just end up logging more csums than we need and it
3419 * will be ok.
3420 */
3421 } else {
3422 if (ordered->file_offset + ordered->len <
3423 mod_start + mod_len) {
3424 mod_len = (mod_start + mod_len) -
3425 (ordered->file_offset + ordered->len);
3426 mod_start = ordered->file_offset +
3427 ordered->len;
3428 } else {
3429 mod_len = 0;
3430 }
3431 }
3432
3433 /*
3434 * To keep us from looping for the above case of an ordered
3435 * extent that falls inside of the logged extent.
3436 */
3437 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3438 &ordered->flags))
3439 continue;
3440 atomic_inc(&ordered->refs);
3441 spin_unlock_irq(&log->log_extents_lock[index]);
3442 /*
3443 * we've dropped the lock, we must either break or
3444 * start over after this.
3445 */
3446
3447 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3448
3449 list_for_each_entry(sum, &ordered->list, list) {
3450 ret = btrfs_csum_file_blocks(trans, log, sum);
3451 if (ret) {
3452 btrfs_put_ordered_extent(ordered);
3453 goto unlocked;
3454 }
3455 }
3456 btrfs_put_ordered_extent(ordered);
3457 goto again;
3458
3459 }
3460 spin_unlock_irq(&log->log_extents_lock[index]);
3461unlocked:
3462
3463 if (!mod_len || ret)
3464 return ret;
3465
3466 csum_offset = mod_start - em->start;
3467 csum_len = mod_len;
3468
Josef Bacik70c8a912012-10-11 16:54:30 -04003469 /* block start is already adjusted for the file extent offset. */
3470 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3471 em->block_start + csum_offset,
3472 em->block_start + csum_offset +
3473 csum_len - 1, &ordered_sums, 0);
3474 if (ret)
3475 return ret;
3476
3477 while (!list_empty(&ordered_sums)) {
3478 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3479 struct btrfs_ordered_sum,
3480 list);
3481 if (!ret)
3482 ret = btrfs_csum_file_blocks(trans, log, sums);
3483 list_del(&sums->list);
3484 kfree(sums);
3485 }
3486
3487 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003488}
3489
3490static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3491 struct btrfs_root *root,
3492 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003493 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003494{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003495 struct extent_map *em, *n;
3496 struct list_head extents;
3497 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3498 u64 test_gen;
3499 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003500 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003501
3502 INIT_LIST_HEAD(&extents);
3503
Josef Bacik5dc562c2012-08-17 13:14:17 -04003504 write_lock(&tree->lock);
3505 test_gen = root->fs_info->last_trans_committed;
3506
3507 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3508 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003509
3510 /*
3511 * Just an arbitrary number, this can be really CPU intensive
3512 * once we start getting a lot of extents, and really once we
3513 * have a bunch of extents we just want to commit since it will
3514 * be faster.
3515 */
3516 if (++num > 32768) {
3517 list_del_init(&tree->modified_extents);
3518 ret = -EFBIG;
3519 goto process;
3520 }
3521
Josef Bacik5dc562c2012-08-17 13:14:17 -04003522 if (em->generation <= test_gen)
3523 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003524 /* Need a ref to keep it from getting evicted from cache */
3525 atomic_inc(&em->refs);
3526 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003527 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003528 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003529 }
3530
3531 list_sort(NULL, &extents, extent_cmp);
3532
Josef Bacik2ab28f32012-10-12 15:27:49 -04003533process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003534 while (!list_empty(&extents)) {
3535 em = list_entry(extents.next, struct extent_map, list);
3536
3537 list_del_init(&em->list);
3538
3539 /*
3540 * If we had an error we just need to delete everybody from our
3541 * private list.
3542 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003543 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003544 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003545 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003546 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003547 }
3548
3549 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003550
Josef Bacik70c8a912012-10-11 16:54:30 -04003551 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003552 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003553 clear_em_logging(tree, em);
3554 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003555 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003556 WARN_ON(!list_empty(&extents));
3557 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003558
Josef Bacik5dc562c2012-08-17 13:14:17 -04003559 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003560 return ret;
3561}
3562
Chris Masone02119d2008-09-05 16:13:11 -04003563/* log a single inode in the tree log.
3564 * At least one parent directory for this inode must exist in the tree
3565 * or be logged already.
3566 *
3567 * Any items from this inode changed by the current transaction are copied
3568 * to the log tree. An extra reference is taken on any extents in this
3569 * file, allowing us to avoid a whole pile of corner cases around logging
3570 * blocks that have been removed from the tree.
3571 *
3572 * See LOG_INODE_ALL and related defines for a description of what inode_only
3573 * does.
3574 *
3575 * This handles both files and directories.
3576 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003577static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003578 struct btrfs_root *root, struct inode *inode,
3579 int inode_only)
3580{
3581 struct btrfs_path *path;
3582 struct btrfs_path *dst_path;
3583 struct btrfs_key min_key;
3584 struct btrfs_key max_key;
3585 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003586 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003587 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003588 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003589 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003590 int ins_start_slot = 0;
3591 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003592 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003593 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003594
3595 log = root->log_root;
3596
3597 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003598 if (!path)
3599 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003600 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003601 if (!dst_path) {
3602 btrfs_free_path(path);
3603 return -ENOMEM;
3604 }
Chris Masone02119d2008-09-05 16:13:11 -04003605
Li Zefan33345d012011-04-20 10:31:50 +08003606 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003607 min_key.type = BTRFS_INODE_ITEM_KEY;
3608 min_key.offset = 0;
3609
Li Zefan33345d012011-04-20 10:31:50 +08003610 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003611
Chris Mason12fcfd22009-03-24 10:24:20 -04003612
Josef Bacik5dc562c2012-08-17 13:14:17 -04003613 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003614 if (S_ISDIR(inode->i_mode) ||
3615 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3616 &BTRFS_I(inode)->runtime_flags) &&
3617 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003618 max_key.type = BTRFS_XATTR_ITEM_KEY;
3619 else
3620 max_key.type = (u8)-1;
3621 max_key.offset = (u64)-1;
3622
Josef Bacik94edf4a2012-09-25 14:56:25 -04003623 /* Only run delayed items if we are a dir or a new file */
3624 if (S_ISDIR(inode->i_mode) ||
3625 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3626 ret = btrfs_commit_inode_delayed_items(trans, inode);
3627 if (ret) {
3628 btrfs_free_path(path);
3629 btrfs_free_path(dst_path);
3630 return ret;
3631 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003632 }
3633
Chris Masone02119d2008-09-05 16:13:11 -04003634 mutex_lock(&BTRFS_I(inode)->log_mutex);
3635
Josef Bacik2ab28f32012-10-12 15:27:49 -04003636 btrfs_get_logged_extents(log, inode);
3637
Chris Masone02119d2008-09-05 16:13:11 -04003638 /*
3639 * a brute force approach to making sure we get the most uptodate
3640 * copies of everything.
3641 */
3642 if (S_ISDIR(inode->i_mode)) {
3643 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3644
3645 if (inode_only == LOG_INODE_EXISTS)
3646 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003647 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003648 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003649 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3650 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003651 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3652 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003653 ret = btrfs_truncate_inode_items(trans, log,
3654 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003655 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3656 &BTRFS_I(inode)->runtime_flags)) {
3657 if (inode_only == LOG_INODE_ALL)
3658 fast_search = true;
3659 max_key.type = BTRFS_XATTR_ITEM_KEY;
3660 ret = drop_objectid_items(trans, log, path, ino,
3661 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003662 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003663 if (inode_only == LOG_INODE_ALL)
3664 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003665 ret = log_inode_item(trans, log, dst_path, inode);
3666 if (ret) {
3667 err = ret;
3668 goto out_unlock;
3669 }
3670 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003671 }
Josef Bacika95249b2012-10-11 16:17:34 -04003672
Chris Masone02119d2008-09-05 16:13:11 -04003673 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003674 if (ret) {
3675 err = ret;
3676 goto out_unlock;
3677 }
Chris Masone02119d2008-09-05 16:13:11 -04003678 path->keep_locks = 1;
3679
Chris Masond3977122009-01-05 21:25:51 -05003680 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003681 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003682 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003683 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003684 if (ret != 0)
3685 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003686again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003687 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003688 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003689 break;
3690 if (min_key.type > max_key.type)
3691 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003692
Chris Masone02119d2008-09-05 16:13:11 -04003693 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003694 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3695 ins_nr++;
3696 goto next_slot;
3697 } else if (!ins_nr) {
3698 ins_start_slot = path->slots[0];
3699 ins_nr = 1;
3700 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003701 }
3702
Liu Bod2794402012-08-29 01:07:56 -06003703 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003704 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003705 if (ret) {
3706 err = ret;
3707 goto out_unlock;
3708 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003709 ins_nr = 1;
3710 ins_start_slot = path->slots[0];
3711next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003712
Chris Mason3a5f1d42008-09-11 15:53:37 -04003713 nritems = btrfs_header_nritems(path->nodes[0]);
3714 path->slots[0]++;
3715 if (path->slots[0] < nritems) {
3716 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3717 path->slots[0]);
3718 goto again;
3719 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003720 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003721 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003722 ins_start_slot,
3723 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003724 if (ret) {
3725 err = ret;
3726 goto out_unlock;
3727 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003728 ins_nr = 0;
3729 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003730 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003731
Chris Masone02119d2008-09-05 16:13:11 -04003732 if (min_key.offset < (u64)-1)
3733 min_key.offset++;
3734 else if (min_key.type < (u8)-1)
3735 min_key.type++;
3736 else if (min_key.objectid < (u64)-1)
3737 min_key.objectid++;
3738 else
3739 break;
3740 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003741 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003742 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003743 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003744 if (ret) {
3745 err = ret;
3746 goto out_unlock;
3747 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003748 ins_nr = 0;
3749 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003750
Josef Bacika95249b2012-10-11 16:17:34 -04003751log_extents:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003752 if (fast_search) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003753 btrfs_release_path(dst_path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003754 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003755 if (ret) {
3756 err = ret;
3757 goto out_unlock;
3758 }
Liu Bo06d3d222012-08-27 10:52:19 -06003759 } else {
3760 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3761 struct extent_map *em, *n;
3762
Miao Xiebbe14262012-11-01 07:34:54 +00003763 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003764 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3765 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003766 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003767 }
3768
Chris Mason9623f9a2008-09-11 17:42:42 -04003769 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003770 btrfs_release_path(path);
3771 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003772 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003773 if (ret) {
3774 err = ret;
3775 goto out_unlock;
3776 }
Chris Masone02119d2008-09-05 16:13:11 -04003777 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003778 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003779 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003780out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003781 if (err)
3782 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003783 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3784
3785 btrfs_free_path(path);
3786 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003787 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003788}
3789
Chris Mason12fcfd22009-03-24 10:24:20 -04003790/*
3791 * follow the dentry parent pointers up the chain and see if any
3792 * of the directories in it require a full commit before they can
3793 * be logged. Returns zero if nothing special needs to be done or 1 if
3794 * a full commit is required.
3795 */
3796static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3797 struct inode *inode,
3798 struct dentry *parent,
3799 struct super_block *sb,
3800 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003801{
Chris Mason12fcfd22009-03-24 10:24:20 -04003802 int ret = 0;
3803 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003804 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003805
Chris Masonaf4176b2009-03-24 10:24:31 -04003806 /*
3807 * for regular files, if its inode is already on disk, we don't
3808 * have to worry about the parents at all. This is because
3809 * we can use the last_unlink_trans field to record renames
3810 * and other fun in this file.
3811 */
3812 if (S_ISREG(inode->i_mode) &&
3813 BTRFS_I(inode)->generation <= last_committed &&
3814 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3815 goto out;
3816
Chris Mason12fcfd22009-03-24 10:24:20 -04003817 if (!S_ISDIR(inode->i_mode)) {
3818 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3819 goto out;
3820 inode = parent->d_inode;
3821 }
3822
3823 while (1) {
3824 BTRFS_I(inode)->logged_trans = trans->transid;
3825 smp_mb();
3826
3827 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3828 root = BTRFS_I(inode)->root;
3829
3830 /*
3831 * make sure any commits to the log are forced
3832 * to be full commits
3833 */
3834 root->fs_info->last_trans_log_full_commit =
3835 trans->transid;
3836 ret = 1;
3837 break;
3838 }
3839
3840 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3841 break;
3842
Yan, Zheng76dda932009-09-21 16:00:26 -04003843 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003844 break;
3845
Josef Bacik6a912212010-11-20 09:48:00 +00003846 parent = dget_parent(parent);
3847 dput(old_parent);
3848 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003849 inode = parent->d_inode;
3850
3851 }
Josef Bacik6a912212010-11-20 09:48:00 +00003852 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003853out:
Chris Masone02119d2008-09-05 16:13:11 -04003854 return ret;
3855}
3856
3857/*
3858 * helper function around btrfs_log_inode to make sure newly created
3859 * parent directories also end up in the log. A minimal inode and backref
3860 * only logging is done of any parent directories that are older than
3861 * the last committed transaction
3862 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003863int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3864 struct btrfs_root *root, struct inode *inode,
3865 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003866{
Chris Mason12fcfd22009-03-24 10:24:20 -04003867 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003868 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003869 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003870 int ret = 0;
3871 u64 last_committed = root->fs_info->last_trans_committed;
3872
3873 sb = inode->i_sb;
3874
Sage Weil3a5e1402009-04-02 16:49:40 -04003875 if (btrfs_test_opt(root, NOTREELOG)) {
3876 ret = 1;
3877 goto end_no_trans;
3878 }
3879
Chris Mason12fcfd22009-03-24 10:24:20 -04003880 if (root->fs_info->last_trans_log_full_commit >
3881 root->fs_info->last_trans_committed) {
3882 ret = 1;
3883 goto end_no_trans;
3884 }
3885
Yan, Zheng76dda932009-09-21 16:00:26 -04003886 if (root != BTRFS_I(inode)->root ||
3887 btrfs_root_refs(&root->root_item) == 0) {
3888 ret = 1;
3889 goto end_no_trans;
3890 }
3891
Chris Mason12fcfd22009-03-24 10:24:20 -04003892 ret = check_parent_dirs_for_sync(trans, inode, parent,
3893 sb, last_committed);
3894 if (ret)
3895 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003896
Josef Bacik22ee6982012-05-29 16:57:49 -04003897 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003898 ret = BTRFS_NO_LOG_SYNC;
3899 goto end_no_trans;
3900 }
3901
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003902 ret = start_log_trans(trans, root);
3903 if (ret)
3904 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003905
3906 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003907 if (ret)
3908 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003909
Chris Masonaf4176b2009-03-24 10:24:31 -04003910 /*
3911 * for regular files, if its inode is already on disk, we don't
3912 * have to worry about the parents at all. This is because
3913 * we can use the last_unlink_trans field to record renames
3914 * and other fun in this file.
3915 */
3916 if (S_ISREG(inode->i_mode) &&
3917 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003918 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3919 ret = 0;
3920 goto end_trans;
3921 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003922
3923 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003924 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003925 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003926 break;
3927
Chris Mason12fcfd22009-03-24 10:24:20 -04003928 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003929 if (root != BTRFS_I(inode)->root)
3930 break;
3931
Chris Mason12fcfd22009-03-24 10:24:20 -04003932 if (BTRFS_I(inode)->generation >
3933 root->fs_info->last_trans_committed) {
3934 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003935 if (ret)
3936 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003937 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003938 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003939 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003940
Josef Bacik6a912212010-11-20 09:48:00 +00003941 parent = dget_parent(parent);
3942 dput(old_parent);
3943 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003944 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003945 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003946end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003947 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003948 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003949 root->fs_info->last_trans_log_full_commit = trans->transid;
3950 ret = 1;
3951 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003952 btrfs_end_log_trans(root);
3953end_no_trans:
3954 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003955}
3956
3957/*
3958 * it is not safe to log dentry if the chunk root has added new
3959 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3960 * If this returns 1, you must commit the transaction to safely get your
3961 * data on disk.
3962 */
3963int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3964 struct btrfs_root *root, struct dentry *dentry)
3965{
Josef Bacik6a912212010-11-20 09:48:00 +00003966 struct dentry *parent = dget_parent(dentry);
3967 int ret;
3968
3969 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3970 dput(parent);
3971
3972 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003973}
3974
3975/*
3976 * should be called during mount to recover any replay any log trees
3977 * from the FS
3978 */
3979int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3980{
3981 int ret;
3982 struct btrfs_path *path;
3983 struct btrfs_trans_handle *trans;
3984 struct btrfs_key key;
3985 struct btrfs_key found_key;
3986 struct btrfs_key tmp_key;
3987 struct btrfs_root *log;
3988 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3989 struct walk_control wc = {
3990 .process_func = process_one_buffer,
3991 .stage = 0,
3992 };
3993
Chris Masone02119d2008-09-05 16:13:11 -04003994 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003995 if (!path)
3996 return -ENOMEM;
3997
3998 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003999
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004000 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004001 if (IS_ERR(trans)) {
4002 ret = PTR_ERR(trans);
4003 goto error;
4004 }
Chris Masone02119d2008-09-05 16:13:11 -04004005
4006 wc.trans = trans;
4007 wc.pin = 1;
4008
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004009 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004010 if (ret) {
4011 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4012 "recovering log root tree.");
4013 goto error;
4014 }
Chris Masone02119d2008-09-05 16:13:11 -04004015
4016again:
4017 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4018 key.offset = (u64)-1;
4019 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4020
Chris Masond3977122009-01-05 21:25:51 -05004021 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004022 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004023
4024 if (ret < 0) {
4025 btrfs_error(fs_info, ret,
4026 "Couldn't find tree log root.");
4027 goto error;
4028 }
Chris Masone02119d2008-09-05 16:13:11 -04004029 if (ret > 0) {
4030 if (path->slots[0] == 0)
4031 break;
4032 path->slots[0]--;
4033 }
4034 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4035 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004036 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004037 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4038 break;
4039
4040 log = btrfs_read_fs_root_no_radix(log_root_tree,
4041 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004042 if (IS_ERR(log)) {
4043 ret = PTR_ERR(log);
4044 btrfs_error(fs_info, ret,
4045 "Couldn't read tree log root.");
4046 goto error;
4047 }
Chris Masone02119d2008-09-05 16:13:11 -04004048
4049 tmp_key.objectid = found_key.offset;
4050 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4051 tmp_key.offset = (u64)-1;
4052
4053 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004054 if (IS_ERR(wc.replay_dest)) {
4055 ret = PTR_ERR(wc.replay_dest);
4056 btrfs_error(fs_info, ret, "Couldn't read target root "
4057 "for tree log recovery.");
4058 goto error;
4059 }
Chris Masone02119d2008-09-05 16:13:11 -04004060
Yan Zheng07d400a2009-01-06 11:42:00 -05004061 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004062 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004063 ret = walk_log_tree(trans, log, &wc);
4064 BUG_ON(ret);
4065
4066 if (wc.stage == LOG_WALK_REPLAY_ALL) {
4067 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4068 path);
4069 BUG_ON(ret);
4070 }
Chris Masone02119d2008-09-05 16:13:11 -04004071
4072 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004073 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004074 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004075 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004076 kfree(log);
4077
4078 if (found_key.offset == 0)
4079 break;
4080 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004081 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004082
4083 /* step one is to pin it all, step two is to replay just inodes */
4084 if (wc.pin) {
4085 wc.pin = 0;
4086 wc.process_func = replay_one_buffer;
4087 wc.stage = LOG_WALK_REPLAY_INODES;
4088 goto again;
4089 }
4090 /* step three is to replay everything */
4091 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4092 wc.stage++;
4093 goto again;
4094 }
4095
4096 btrfs_free_path(path);
4097
4098 free_extent_buffer(log_root_tree->node);
4099 log_root_tree->log_root = NULL;
4100 fs_info->log_root_recovering = 0;
4101
4102 /* step 4: commit the transaction, which also unpins the blocks */
4103 btrfs_commit_transaction(trans, fs_info->tree_root);
4104
4105 kfree(log_root_tree);
4106 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107
4108error:
4109 btrfs_free_path(path);
4110 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004111}
Chris Mason12fcfd22009-03-24 10:24:20 -04004112
4113/*
4114 * there are some corner cases where we want to force a full
4115 * commit instead of allowing a directory to be logged.
4116 *
4117 * They revolve around files there were unlinked from the directory, and
4118 * this function updates the parent directory so that a full commit is
4119 * properly done if it is fsync'd later after the unlinks are done.
4120 */
4121void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4122 struct inode *dir, struct inode *inode,
4123 int for_rename)
4124{
4125 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004126 * when we're logging a file, if it hasn't been renamed
4127 * or unlinked, and its inode is fully committed on disk,
4128 * we don't have to worry about walking up the directory chain
4129 * to log its parents.
4130 *
4131 * So, we use the last_unlink_trans field to put this transid
4132 * into the file. When the file is logged we check it and
4133 * don't log the parents if the file is fully on disk.
4134 */
4135 if (S_ISREG(inode->i_mode))
4136 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4137
4138 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004139 * if this directory was already logged any new
4140 * names for this file/dir will get recorded
4141 */
4142 smp_mb();
4143 if (BTRFS_I(dir)->logged_trans == trans->transid)
4144 return;
4145
4146 /*
4147 * if the inode we're about to unlink was logged,
4148 * the log will be properly updated for any new names
4149 */
4150 if (BTRFS_I(inode)->logged_trans == trans->transid)
4151 return;
4152
4153 /*
4154 * when renaming files across directories, if the directory
4155 * there we're unlinking from gets fsync'd later on, there's
4156 * no way to find the destination directory later and fsync it
4157 * properly. So, we have to be conservative and force commits
4158 * so the new name gets discovered.
4159 */
4160 if (for_rename)
4161 goto record;
4162
4163 /* we can safely do the unlink without any special recording */
4164 return;
4165
4166record:
4167 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4168}
4169
4170/*
4171 * Call this after adding a new name for a file and it will properly
4172 * update the log to reflect the new name.
4173 *
4174 * It will return zero if all goes well, and it will return 1 if a
4175 * full transaction commit is required.
4176 */
4177int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4178 struct inode *inode, struct inode *old_dir,
4179 struct dentry *parent)
4180{
4181 struct btrfs_root * root = BTRFS_I(inode)->root;
4182
4183 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004184 * this will force the logging code to walk the dentry chain
4185 * up for the file
4186 */
4187 if (S_ISREG(inode->i_mode))
4188 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4189
4190 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004191 * if this inode hasn't been logged and directory we're renaming it
4192 * from hasn't been logged, we don't need to log it
4193 */
4194 if (BTRFS_I(inode)->logged_trans <=
4195 root->fs_info->last_trans_committed &&
4196 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4197 root->fs_info->last_trans_committed))
4198 return 0;
4199
4200 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4201}
4202