blob: 7de720d22b74418d8679d3264591f030d0349b2b [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)
Chris Masone688b7252011-10-31 20:52:39 -0400281 btrfs_pin_extent_for_log_replay(wc->trans,
282 log->fs_info->extent_root,
283 eb->start, eb->len);
Chris Masone02119d2008-09-05 16:13:11 -0400284
Chris Masonb9fab912012-05-06 07:23:47 -0400285 if (btrfs_buffer_uptodate(eb, gen, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -0400286 if (wc->write)
287 btrfs_write_tree_block(eb);
288 if (wc->wait)
289 btrfs_wait_tree_block_writeback(eb);
290 }
291 return 0;
292}
293
294/*
295 * Item overwrite used by replay and tree logging. eb, slot and key all refer
296 * to the src data we are copying out.
297 *
298 * root is the tree we are copying into, and path is a scratch
299 * path for use in this function (it should be released on entry and
300 * will be released on exit).
301 *
302 * If the key is already in the destination tree the existing item is
303 * overwritten. If the existing item isn't big enough, it is extended.
304 * If it is too large, it is truncated.
305 *
306 * If the key isn't in the destination yet, a new item is inserted.
307 */
308static noinline int overwrite_item(struct btrfs_trans_handle *trans,
309 struct btrfs_root *root,
310 struct btrfs_path *path,
311 struct extent_buffer *eb, int slot,
312 struct btrfs_key *key)
313{
314 int ret;
315 u32 item_size;
316 u64 saved_i_size = 0;
317 int save_old_i_size = 0;
318 unsigned long src_ptr;
319 unsigned long dst_ptr;
320 int overwrite_root = 0;
321
322 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
323 overwrite_root = 1;
324
325 item_size = btrfs_item_size_nr(eb, slot);
326 src_ptr = btrfs_item_ptr_offset(eb, slot);
327
328 /* look for the key in the destination tree */
329 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
330 if (ret == 0) {
331 char *src_copy;
332 char *dst_copy;
333 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
334 path->slots[0]);
335 if (dst_size != item_size)
336 goto insert;
337
338 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200339 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400340 return 0;
341 }
342 dst_copy = kmalloc(item_size, GFP_NOFS);
343 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000344 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200345 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000346 kfree(dst_copy);
347 kfree(src_copy);
348 return -ENOMEM;
349 }
Chris Masone02119d2008-09-05 16:13:11 -0400350
351 read_extent_buffer(eb, src_copy, src_ptr, item_size);
352
353 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
354 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
355 item_size);
356 ret = memcmp(dst_copy, src_copy, item_size);
357
358 kfree(dst_copy);
359 kfree(src_copy);
360 /*
361 * they have the same contents, just return, this saves
362 * us from cowing blocks in the destination tree and doing
363 * extra writes that may not have been done by a previous
364 * sync
365 */
366 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200367 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400368 return 0;
369 }
370
371 }
372insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200373 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400374 /* try to insert the key into the destination tree */
375 ret = btrfs_insert_empty_item(trans, root, path,
376 key, item_size);
377
378 /* make sure any existing item is the correct size */
379 if (ret == -EEXIST) {
380 u32 found_size;
381 found_size = btrfs_item_size_nr(path->nodes[0],
382 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100383 if (found_size > item_size)
Chris Masone02119d2008-09-05 16:13:11 -0400384 btrfs_truncate_item(trans, root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100385 else if (found_size < item_size)
386 btrfs_extend_item(trans, root, path,
387 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400388 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400389 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400390 }
391 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
392 path->slots[0]);
393
394 /* don't overwrite an existing inode if the generation number
395 * was logged as zero. This is done when the tree logging code
396 * is just logging an inode to make sure it exists after recovery.
397 *
398 * Also, don't overwrite i_size on directories during replay.
399 * log replay inserts and removes directory items based on the
400 * state of the tree found in the subvolume, and i_size is modified
401 * as it goes
402 */
403 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
404 struct btrfs_inode_item *src_item;
405 struct btrfs_inode_item *dst_item;
406
407 src_item = (struct btrfs_inode_item *)src_ptr;
408 dst_item = (struct btrfs_inode_item *)dst_ptr;
409
410 if (btrfs_inode_generation(eb, src_item) == 0)
411 goto no_copy;
412
413 if (overwrite_root &&
414 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
415 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
416 save_old_i_size = 1;
417 saved_i_size = btrfs_inode_size(path->nodes[0],
418 dst_item);
419 }
420 }
421
422 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
423 src_ptr, item_size);
424
425 if (save_old_i_size) {
426 struct btrfs_inode_item *dst_item;
427 dst_item = (struct btrfs_inode_item *)dst_ptr;
428 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
429 }
430
431 /* make sure the generation is filled in */
432 if (key->type == BTRFS_INODE_ITEM_KEY) {
433 struct btrfs_inode_item *dst_item;
434 dst_item = (struct btrfs_inode_item *)dst_ptr;
435 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
436 btrfs_set_inode_generation(path->nodes[0], dst_item,
437 trans->transid);
438 }
439 }
440no_copy:
441 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200442 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400443 return 0;
444}
445
446/*
447 * simple helper to read an inode off the disk from a given root
448 * This can only be called for subvolume roots and not for the log
449 */
450static noinline struct inode *read_one_inode(struct btrfs_root *root,
451 u64 objectid)
452{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400454 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400455
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400456 key.objectid = objectid;
457 key.type = BTRFS_INODE_ITEM_KEY;
458 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000459 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 if (IS_ERR(inode)) {
461 inode = NULL;
462 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400463 iput(inode);
464 inode = NULL;
465 }
466 return inode;
467}
468
469/* replays a single extent in 'eb' at 'slot' with 'key' into the
470 * subvolume 'root'. path is released on entry and should be released
471 * on exit.
472 *
473 * extents in the log tree have not been allocated out of the extent
474 * tree yet. So, this completes the allocation, taking a reference
475 * as required if the extent already exists or creating a new extent
476 * if it isn't in the extent allocation tree yet.
477 *
478 * The extent is inserted into the file, dropping any existing extents
479 * from the file that overlap the new one.
480 */
481static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
482 struct btrfs_root *root,
483 struct btrfs_path *path,
484 struct extent_buffer *eb, int slot,
485 struct btrfs_key *key)
486{
487 int found_type;
488 u64 mask = root->sectorsize - 1;
489 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400490 u64 start = key->offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500491 u64 saved_nbytes;
Chris Masone02119d2008-09-05 16:13:11 -0400492 struct btrfs_file_extent_item *item;
493 struct inode *inode = NULL;
494 unsigned long size;
495 int ret = 0;
496
497 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
498 found_type = btrfs_file_extent_type(eb, item);
499
Yan Zhengd899e052008-10-30 14:25:28 -0400500 if (found_type == BTRFS_FILE_EXTENT_REG ||
501 found_type == BTRFS_FILE_EXTENT_PREALLOC)
Chris Masone02119d2008-09-05 16:13:11 -0400502 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
503 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400504 size = btrfs_file_extent_inline_len(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400505 extent_end = (start + size + mask) & ~mask;
506 } else {
507 ret = 0;
508 goto out;
509 }
510
511 inode = read_one_inode(root, key->objectid);
512 if (!inode) {
513 ret = -EIO;
514 goto out;
515 }
516
517 /*
518 * first check to see if we already have this extent in the
519 * file. This must be done before the btrfs_drop_extents run
520 * so we don't try to drop this extent.
521 */
Li Zefan33345d012011-04-20 10:31:50 +0800522 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400523 start, 0);
524
Yan Zhengd899e052008-10-30 14:25:28 -0400525 if (ret == 0 &&
526 (found_type == BTRFS_FILE_EXTENT_REG ||
527 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400528 struct btrfs_file_extent_item cmp1;
529 struct btrfs_file_extent_item cmp2;
530 struct btrfs_file_extent_item *existing;
531 struct extent_buffer *leaf;
532
533 leaf = path->nodes[0];
534 existing = btrfs_item_ptr(leaf, path->slots[0],
535 struct btrfs_file_extent_item);
536
537 read_extent_buffer(eb, &cmp1, (unsigned long)item,
538 sizeof(cmp1));
539 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
540 sizeof(cmp2));
541
542 /*
543 * we already have a pointer to this exact extent,
544 * we don't have to do anything
545 */
546 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200547 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400548 goto out;
549 }
550 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200551 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400552
Yan Zheng07d400a2009-01-06 11:42:00 -0500553 saved_nbytes = inode_get_bytes(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400554 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400555 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400556 BUG_ON(ret);
557
Yan Zheng07d400a2009-01-06 11:42:00 -0500558 if (found_type == BTRFS_FILE_EXTENT_REG ||
559 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500561 unsigned long dest_offset;
562 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400563
Yan Zheng07d400a2009-01-06 11:42:00 -0500564 ret = btrfs_insert_empty_item(trans, root, path, key,
565 sizeof(*item));
566 BUG_ON(ret);
567 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
568 path->slots[0]);
569 copy_extent_buffer(path->nodes[0], eb, dest_offset,
570 (unsigned long)item, sizeof(*item));
571
572 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
573 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
574 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500576
577 if (ins.objectid > 0) {
578 u64 csum_start;
579 u64 csum_end;
580 LIST_HEAD(ordered_sums);
581 /*
582 * is this extent already allocated in the extent
583 * allocation tree? If so, just add a reference
584 */
585 ret = btrfs_lookup_extent(root, ins.objectid,
586 ins.offset);
587 if (ret == 0) {
588 ret = btrfs_inc_extent_ref(trans, root,
589 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200591 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000592 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500593 } else {
594 /*
595 * insert the extent pointer in the extent
596 * allocation tree
597 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 ret = btrfs_alloc_logged_file_extent(trans,
599 root, root->root_key.objectid,
600 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500601 BUG_ON(ret);
602 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200603 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500604
605 if (btrfs_file_extent_compression(eb, item)) {
606 csum_start = ins.objectid;
607 csum_end = csum_start + ins.offset;
608 } else {
609 csum_start = ins.objectid +
610 btrfs_file_extent_offset(eb, item);
611 csum_end = csum_start +
612 btrfs_file_extent_num_bytes(eb, item);
613 }
614
615 ret = btrfs_lookup_csums_range(root->log_root,
616 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100617 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500618 BUG_ON(ret);
619 while (!list_empty(&ordered_sums)) {
620 struct btrfs_ordered_sum *sums;
621 sums = list_entry(ordered_sums.next,
622 struct btrfs_ordered_sum,
623 list);
624 ret = btrfs_csum_file_blocks(trans,
625 root->fs_info->csum_root,
626 sums);
627 BUG_ON(ret);
628 list_del(&sums->list);
629 kfree(sums);
630 }
631 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200632 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500633 }
634 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
635 /* inline extents are easy, we just overwrite them */
636 ret = overwrite_item(trans, root, path, eb, slot, key);
637 BUG_ON(ret);
638 }
639
640 inode_set_bytes(inode, saved_nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600641 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400642out:
643 if (inode)
644 iput(inode);
645 return ret;
646}
647
648/*
649 * when cleaning up conflicts between the directory names in the
650 * subvolume, directory names in the log and directory names in the
651 * inode back references, we may have to unlink inodes from directories.
652 *
653 * This is a helper function to do the unlink of a specific directory
654 * item
655 */
656static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
657 struct btrfs_root *root,
658 struct btrfs_path *path,
659 struct inode *dir,
660 struct btrfs_dir_item *di)
661{
662 struct inode *inode;
663 char *name;
664 int name_len;
665 struct extent_buffer *leaf;
666 struct btrfs_key location;
667 int ret;
668
669 leaf = path->nodes[0];
670
671 btrfs_dir_item_key_to_cpu(leaf, di, &location);
672 name_len = btrfs_dir_name_len(leaf, di);
673 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000674 if (!name)
675 return -ENOMEM;
676
Chris Masone02119d2008-09-05 16:13:11 -0400677 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200678 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400679
680 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000681 if (!inode) {
682 kfree(name);
683 return -EIO;
684 }
Chris Masone02119d2008-09-05 16:13:11 -0400685
Yan Zhengec051c02009-01-05 15:43:42 -0500686 ret = link_to_fixup_dir(trans, root, path, location.objectid);
687 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400688
Chris Masone02119d2008-09-05 16:13:11 -0400689 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500690 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400691 kfree(name);
692
693 iput(inode);
Chris Masonb6305562012-07-02 15:29:53 -0400694
695 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400696 return ret;
697}
698
699/*
700 * helper function to see if a given name and sequence number found
701 * in an inode back reference are already in a directory and correctly
702 * point to this inode
703 */
704static noinline int inode_in_dir(struct btrfs_root *root,
705 struct btrfs_path *path,
706 u64 dirid, u64 objectid, u64 index,
707 const char *name, int name_len)
708{
709 struct btrfs_dir_item *di;
710 struct btrfs_key location;
711 int match = 0;
712
713 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
714 index, name, name_len, 0);
715 if (di && !IS_ERR(di)) {
716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
717 if (location.objectid != objectid)
718 goto out;
719 } else
720 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200721 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400722
723 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
724 if (di && !IS_ERR(di)) {
725 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
726 if (location.objectid != objectid)
727 goto out;
728 } else
729 goto out;
730 match = 1;
731out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200732 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400733 return match;
734}
735
736/*
737 * helper function to check a log tree for a named back reference in
738 * an inode. This is used to decide if a back reference that is
739 * found in the subvolume conflicts with what we find in the log.
740 *
741 * inode backreferences may have multiple refs in a single item,
742 * during replay we process one reference at a time, and we don't
743 * want to delete valid links to a file from the subvolume if that
744 * link is also in the log.
745 */
746static noinline int backref_in_log(struct btrfs_root *log,
747 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700748 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400749 char *name, int namelen)
750{
751 struct btrfs_path *path;
752 struct btrfs_inode_ref *ref;
753 unsigned long ptr;
754 unsigned long ptr_end;
755 unsigned long name_ptr;
756 int found_name_len;
757 int item_size;
758 int ret;
759 int match = 0;
760
761 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000762 if (!path)
763 return -ENOMEM;
764
Chris Masone02119d2008-09-05 16:13:11 -0400765 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
766 if (ret != 0)
767 goto out;
768
Chris Masone02119d2008-09-05 16:13:11 -0400769 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700770
771 if (key->type == BTRFS_INODE_EXTREF_KEY) {
772 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
773 name, namelen, NULL))
774 match = 1;
775
776 goto out;
777 }
778
779 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400780 ptr_end = ptr + item_size;
781 while (ptr < ptr_end) {
782 ref = (struct btrfs_inode_ref *)ptr;
783 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
784 if (found_name_len == namelen) {
785 name_ptr = (unsigned long)(ref + 1);
786 ret = memcmp_extent_buffer(path->nodes[0], name,
787 name_ptr, namelen);
788 if (ret == 0) {
789 match = 1;
790 goto out;
791 }
792 }
793 ptr = (unsigned long)(ref + 1) + found_name_len;
794 }
795out:
796 btrfs_free_path(path);
797 return match;
798}
799
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700800static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
801 struct btrfs_root *root,
802 struct btrfs_path *path,
803 struct btrfs_root *log_root,
804 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700805 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700806 u64 inode_objectid, u64 parent_objectid,
807 u64 ref_index, char *name, int namelen,
808 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700809{
810 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700811 char *victim_name;
812 int victim_name_len;
813 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700814 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700815 struct btrfs_key search_key;
816 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700817
Mark Fashehf1863732012-08-08 11:32:27 -0700818again:
819 /* Search old style refs */
820 search_key.objectid = inode_objectid;
821 search_key.type = BTRFS_INODE_REF_KEY;
822 search_key.offset = parent_objectid;
823 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700824 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700825 struct btrfs_inode_ref *victim_ref;
826 unsigned long ptr;
827 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700828
829 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700830
831 /* are we trying to overwrite a back ref for the root directory
832 * if so, just jump out, we're done
833 */
Mark Fashehf1863732012-08-08 11:32:27 -0700834 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700835 return 1;
836
837 /* check all the names in this back reference to see
838 * if they are in the log. if so, we allow them to stay
839 * otherwise they must be unlinked as a conflict
840 */
841 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
842 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
843 while (ptr < ptr_end) {
844 victim_ref = (struct btrfs_inode_ref *)ptr;
845 victim_name_len = btrfs_inode_ref_name_len(leaf,
846 victim_ref);
847 victim_name = kmalloc(victim_name_len, GFP_NOFS);
848 BUG_ON(!victim_name);
849
850 read_extent_buffer(leaf, victim_name,
851 (unsigned long)(victim_ref + 1),
852 victim_name_len);
853
Mark Fashehf1863732012-08-08 11:32:27 -0700854 if (!backref_in_log(log_root, &search_key,
855 parent_objectid,
856 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700857 victim_name_len)) {
858 btrfs_inc_nlink(inode);
859 btrfs_release_path(path);
860
861 ret = btrfs_unlink_inode(trans, root, dir,
862 inode, victim_name,
863 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700864 BUG_ON(ret);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700865 btrfs_run_delayed_items(trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -0700866 kfree(victim_name);
867 *search_done = 1;
868 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700869 }
870 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700871
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700872 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
873 }
874 BUG_ON(ret);
875
876 /*
877 * NOTE: we have searched root tree and checked the
878 * coresponding ref, it does not need to check again.
879 */
880 *search_done = 1;
881 }
882 btrfs_release_path(path);
883
Mark Fashehf1863732012-08-08 11:32:27 -0700884 /* Same search but for extended refs */
885 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
886 inode_objectid, parent_objectid, 0,
887 0);
888 if (!IS_ERR_OR_NULL(extref)) {
889 u32 item_size;
890 u32 cur_offset = 0;
891 unsigned long base;
892 struct inode *victim_parent;
893
894 leaf = path->nodes[0];
895
896 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
897 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
898
899 while (cur_offset < item_size) {
900 extref = (struct btrfs_inode_extref *)base + cur_offset;
901
902 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
903
904 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
905 goto next;
906
907 victim_name = kmalloc(victim_name_len, GFP_NOFS);
908 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
909 victim_name_len);
910
911 search_key.objectid = inode_objectid;
912 search_key.type = BTRFS_INODE_EXTREF_KEY;
913 search_key.offset = btrfs_extref_hash(parent_objectid,
914 victim_name,
915 victim_name_len);
916 ret = 0;
917 if (!backref_in_log(log_root, &search_key,
918 parent_objectid, victim_name,
919 victim_name_len)) {
920 ret = -ENOENT;
921 victim_parent = read_one_inode(root,
922 parent_objectid);
923 if (victim_parent) {
924 btrfs_inc_nlink(inode);
925 btrfs_release_path(path);
926
927 ret = btrfs_unlink_inode(trans, root,
928 victim_parent,
929 inode,
930 victim_name,
931 victim_name_len);
932 btrfs_run_delayed_items(trans, root);
933 }
934 BUG_ON(ret);
935 iput(victim_parent);
936 kfree(victim_name);
937 *search_done = 1;
938 goto again;
939 }
940 kfree(victim_name);
941 BUG_ON(ret);
942next:
943 cur_offset += victim_name_len + sizeof(*extref);
944 }
945 *search_done = 1;
946 }
947 btrfs_release_path(path);
948
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700949 /* look for a conflicting sequence number */
950 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -0700951 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700952 if (di && !IS_ERR(di)) {
953 ret = drop_one_dir_item(trans, root, path, dir, di);
954 BUG_ON(ret);
955 }
956 btrfs_release_path(path);
957
958 /* look for a conflicing name */
959 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
960 name, namelen, 0);
961 if (di && !IS_ERR(di)) {
962 ret = drop_one_dir_item(trans, root, path, dir, di);
963 BUG_ON(ret);
964 }
965 btrfs_release_path(path);
966
967 return 0;
968}
Chris Masone02119d2008-09-05 16:13:11 -0400969
Mark Fashehf1863732012-08-08 11:32:27 -0700970static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
971 u32 *namelen, char **name, u64 *index,
972 u64 *parent_objectid)
973{
974 struct btrfs_inode_extref *extref;
975
976 extref = (struct btrfs_inode_extref *)ref_ptr;
977
978 *namelen = btrfs_inode_extref_name_len(eb, extref);
979 *name = kmalloc(*namelen, GFP_NOFS);
980 if (*name == NULL)
981 return -ENOMEM;
982
983 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
984 *namelen);
985
986 *index = btrfs_inode_extref_index(eb, extref);
987 if (parent_objectid)
988 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
989
990 return 0;
991}
992
993static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
994 u32 *namelen, char **name, u64 *index)
995{
996 struct btrfs_inode_ref *ref;
997
998 ref = (struct btrfs_inode_ref *)ref_ptr;
999
1000 *namelen = btrfs_inode_ref_name_len(eb, ref);
1001 *name = kmalloc(*namelen, GFP_NOFS);
1002 if (*name == NULL)
1003 return -ENOMEM;
1004
1005 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1006
1007 *index = btrfs_inode_ref_index(eb, ref);
1008
1009 return 0;
1010}
1011
Chris Masone02119d2008-09-05 16:13:11 -04001012/*
1013 * replay one inode back reference item found in the log tree.
1014 * eb, slot and key refer to the buffer and key found in the log tree.
1015 * root is the destination we are replaying into, and path is for temp
1016 * use by this function. (it should be released on return).
1017 */
1018static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1019 struct btrfs_root *root,
1020 struct btrfs_root *log,
1021 struct btrfs_path *path,
1022 struct extent_buffer *eb, int slot,
1023 struct btrfs_key *key)
1024{
liubo34f3e4f2011-08-06 08:35:23 +00001025 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001026 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -04001027 unsigned long ref_ptr;
1028 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +00001029 char *name;
1030 int namelen;
1031 int ret;
liuboc622ae62011-03-26 08:01:12 -04001032 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001033 int log_ref_ver = 0;
1034 u64 parent_objectid;
1035 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001036 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001037 int ref_struct_size;
1038
1039 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1040 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1041
1042 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1043 struct btrfs_inode_extref *r;
1044
1045 ref_struct_size = sizeof(struct btrfs_inode_extref);
1046 log_ref_ver = 1;
1047 r = (struct btrfs_inode_extref *)ref_ptr;
1048 parent_objectid = btrfs_inode_extref_parent(eb, r);
1049 } else {
1050 ref_struct_size = sizeof(struct btrfs_inode_ref);
1051 parent_objectid = key->offset;
1052 }
1053 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001054
Chris Masone02119d2008-09-05 16:13:11 -04001055 /*
1056 * it is possible that we didn't log all the parent directories
1057 * for a given inode. If we don't find the dir, just don't
1058 * copy the back ref in. The link count fixup code will take
1059 * care of the rest
1060 */
Mark Fashehf1863732012-08-08 11:32:27 -07001061 dir = read_one_inode(root, parent_objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001062 if (!dir)
1063 return -ENOENT;
1064
Mark Fashehf1863732012-08-08 11:32:27 -07001065 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001066 if (!inode) {
1067 iput(dir);
1068 return -EIO;
1069 }
Chris Masone02119d2008-09-05 16:13:11 -04001070
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001071 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001072 if (log_ref_ver) {
1073 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1074 &ref_index, &parent_objectid);
1075 /*
1076 * parent object can change from one array
1077 * item to another.
1078 */
1079 if (!dir)
1080 dir = read_one_inode(root, parent_objectid);
1081 if (!dir)
1082 return -ENOENT;
1083 } else {
1084 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1085 &ref_index);
1086 }
1087 if (ret)
1088 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001089
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001090 /* if we already have a perfect match, we're done */
1091 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001092 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001093 /*
1094 * look for a conflicting back reference in the
1095 * metadata. if we find one we have to unlink that name
1096 * of the file before we add our new link. Later on, we
1097 * overwrite any existing back reference, and we don't
1098 * want to create dangling pointers in the directory.
1099 */
Chris Masone02119d2008-09-05 16:13:11 -04001100
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001101 if (!search_done) {
1102 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001103 dir, inode, eb,
1104 inode_objectid,
1105 parent_objectid,
1106 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001107 &search_done);
1108 if (ret == 1)
1109 goto out;
1110 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001111 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001112
1113 /* insert our name */
1114 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001115 0, ref_index);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001116 BUG_ON(ret);
1117
1118 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001119 }
liuboc622ae62011-03-26 08:01:12 -04001120
Mark Fashehf1863732012-08-08 11:32:27 -07001121 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001122 kfree(name);
Mark Fashehf1863732012-08-08 11:32:27 -07001123 if (log_ref_ver) {
1124 iput(dir);
1125 dir = NULL;
1126 }
Chris Masone02119d2008-09-05 16:13:11 -04001127 }
Chris Masone02119d2008-09-05 16:13:11 -04001128
1129 /* finally write the back reference in the inode */
1130 ret = overwrite_item(trans, root, path, eb, slot, key);
1131 BUG_ON(ret);
1132
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001133out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001134 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001135 iput(dir);
1136 iput(inode);
1137 return 0;
1138}
1139
Yan, Zhengc71bf092009-11-12 09:34:40 +00001140static int insert_orphan_item(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root, u64 offset)
1142{
1143 int ret;
1144 ret = btrfs_find_orphan_item(root, offset);
1145 if (ret > 0)
1146 ret = btrfs_insert_orphan_item(trans, root, offset);
1147 return ret;
1148}
1149
Mark Fashehf1863732012-08-08 11:32:27 -07001150static int count_inode_extrefs(struct btrfs_root *root,
1151 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001152{
Mark Fashehf1863732012-08-08 11:32:27 -07001153 int ret = 0;
1154 int name_len;
1155 unsigned int nlink = 0;
1156 u32 item_size;
1157 u32 cur_offset = 0;
1158 u64 inode_objectid = btrfs_ino(inode);
1159 u64 offset = 0;
1160 unsigned long ptr;
1161 struct btrfs_inode_extref *extref;
1162 struct extent_buffer *leaf;
1163
1164 while (1) {
1165 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1166 &extref, &offset);
1167 if (ret)
1168 break;
1169
1170 leaf = path->nodes[0];
1171 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1172 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1173
1174 while (cur_offset < item_size) {
1175 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1176 name_len = btrfs_inode_extref_name_len(leaf, extref);
1177
1178 nlink++;
1179
1180 cur_offset += name_len + sizeof(*extref);
1181 }
1182
1183 offset++;
1184 btrfs_release_path(path);
1185 }
1186 btrfs_release_path(path);
1187
1188 if (ret < 0)
1189 return ret;
1190 return nlink;
1191}
1192
1193static int count_inode_refs(struct btrfs_root *root,
1194 struct inode *inode, struct btrfs_path *path)
1195{
Chris Masone02119d2008-09-05 16:13:11 -04001196 int ret;
1197 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001198 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001199 unsigned long ptr;
1200 unsigned long ptr_end;
1201 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001202 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001203
Li Zefan33345d012011-04-20 10:31:50 +08001204 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001205 key.type = BTRFS_INODE_REF_KEY;
1206 key.offset = (u64)-1;
1207
Chris Masond3977122009-01-05 21:25:51 -05001208 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001209 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1210 if (ret < 0)
1211 break;
1212 if (ret > 0) {
1213 if (path->slots[0] == 0)
1214 break;
1215 path->slots[0]--;
1216 }
1217 btrfs_item_key_to_cpu(path->nodes[0], &key,
1218 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001219 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001220 key.type != BTRFS_INODE_REF_KEY)
1221 break;
1222 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1223 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1224 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001225 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001226 struct btrfs_inode_ref *ref;
1227
1228 ref = (struct btrfs_inode_ref *)ptr;
1229 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1230 ref);
1231 ptr = (unsigned long)(ref + 1) + name_len;
1232 nlink++;
1233 }
1234
1235 if (key.offset == 0)
1236 break;
1237 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001238 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001239 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001240 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001241
1242 return nlink;
1243}
1244
1245/*
1246 * There are a few corners where the link count of the file can't
1247 * be properly maintained during replay. So, instead of adding
1248 * lots of complexity to the log code, we just scan the backrefs
1249 * for any file that has been through replay.
1250 *
1251 * The scan will update the link count on the inode to reflect the
1252 * number of back refs found. If it goes down to zero, the iput
1253 * will free the inode.
1254 */
1255static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1256 struct btrfs_root *root,
1257 struct inode *inode)
1258{
1259 struct btrfs_path *path;
1260 int ret;
1261 u64 nlink = 0;
1262 u64 ino = btrfs_ino(inode);
1263
1264 path = btrfs_alloc_path();
1265 if (!path)
1266 return -ENOMEM;
1267
1268 ret = count_inode_refs(root, inode, path);
1269 if (ret < 0)
1270 goto out;
1271
1272 nlink = ret;
1273
1274 ret = count_inode_extrefs(root, inode, path);
1275 if (ret == -ENOENT)
1276 ret = 0;
1277
1278 if (ret < 0)
1279 goto out;
1280
1281 nlink += ret;
1282
1283 ret = 0;
1284
Chris Masone02119d2008-09-05 16:13:11 -04001285 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001286 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001287 btrfs_update_inode(trans, root, inode);
1288 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001289 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001290
Yan, Zhengc71bf092009-11-12 09:34:40 +00001291 if (inode->i_nlink == 0) {
1292 if (S_ISDIR(inode->i_mode)) {
1293 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001294 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001295 BUG_ON(ret);
1296 }
Li Zefan33345d012011-04-20 10:31:50 +08001297 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001298 BUG_ON(ret);
1299 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001300
Mark Fashehf1863732012-08-08 11:32:27 -07001301out:
1302 btrfs_free_path(path);
1303 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001304}
1305
1306static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1307 struct btrfs_root *root,
1308 struct btrfs_path *path)
1309{
1310 int ret;
1311 struct btrfs_key key;
1312 struct inode *inode;
1313
1314 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1315 key.type = BTRFS_ORPHAN_ITEM_KEY;
1316 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001317 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001318 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1319 if (ret < 0)
1320 break;
1321
1322 if (ret == 1) {
1323 if (path->slots[0] == 0)
1324 break;
1325 path->slots[0]--;
1326 }
1327
1328 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1329 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1330 key.type != BTRFS_ORPHAN_ITEM_KEY)
1331 break;
1332
1333 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001334 if (ret)
1335 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001336
David Sterbab3b4aa72011-04-21 01:20:15 +02001337 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001338 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001339 if (!inode)
1340 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001341
1342 ret = fixup_inode_link_count(trans, root, inode);
1343 BUG_ON(ret);
1344
1345 iput(inode);
1346
Chris Mason12fcfd22009-03-24 10:24:20 -04001347 /*
1348 * fixup on a directory may create new entries,
1349 * make sure we always look for the highset possible
1350 * offset
1351 */
1352 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001353 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001354 ret = 0;
1355out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001356 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001357 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001358}
1359
1360
1361/*
1362 * record a given inode in the fixup dir so we can check its link
1363 * count when replay is done. The link count is incremented here
1364 * so the inode won't go away until we check it
1365 */
1366static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *root,
1368 struct btrfs_path *path,
1369 u64 objectid)
1370{
1371 struct btrfs_key key;
1372 int ret = 0;
1373 struct inode *inode;
1374
1375 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001376 if (!inode)
1377 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001378
1379 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1380 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1381 key.offset = objectid;
1382
1383 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1384
David Sterbab3b4aa72011-04-21 01:20:15 +02001385 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001386 if (ret == 0) {
1387 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001388 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001389 } else if (ret == -EEXIST) {
1390 ret = 0;
1391 } else {
1392 BUG();
1393 }
1394 iput(inode);
1395
1396 return ret;
1397}
1398
1399/*
1400 * when replaying the log for a directory, we only insert names
1401 * for inodes that actually exist. This means an fsync on a directory
1402 * does not implicitly fsync all the new files in it
1403 */
1404static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root,
1406 struct btrfs_path *path,
1407 u64 dirid, u64 index,
1408 char *name, int name_len, u8 type,
1409 struct btrfs_key *location)
1410{
1411 struct inode *inode;
1412 struct inode *dir;
1413 int ret;
1414
1415 inode = read_one_inode(root, location->objectid);
1416 if (!inode)
1417 return -ENOENT;
1418
1419 dir = read_one_inode(root, dirid);
1420 if (!dir) {
1421 iput(inode);
1422 return -EIO;
1423 }
1424 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1425
1426 /* FIXME, put inode into FIXUP list */
1427
1428 iput(inode);
1429 iput(dir);
1430 return ret;
1431}
1432
1433/*
1434 * take a single entry in a log directory item and replay it into
1435 * the subvolume.
1436 *
1437 * if a conflicting item exists in the subdirectory already,
1438 * the inode it points to is unlinked and put into the link count
1439 * fix up tree.
1440 *
1441 * If a name from the log points to a file or directory that does
1442 * not exist in the FS, it is skipped. fsyncs on directories
1443 * do not force down inodes inside that directory, just changes to the
1444 * names or unlinks in a directory.
1445 */
1446static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1447 struct btrfs_root *root,
1448 struct btrfs_path *path,
1449 struct extent_buffer *eb,
1450 struct btrfs_dir_item *di,
1451 struct btrfs_key *key)
1452{
1453 char *name;
1454 int name_len;
1455 struct btrfs_dir_item *dst_di;
1456 struct btrfs_key found_key;
1457 struct btrfs_key log_key;
1458 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001459 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001460 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001461 int ret;
1462
1463 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001464 if (!dir)
1465 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001466
1467 name_len = btrfs_dir_name_len(eb, di);
1468 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001469 if (!name)
1470 return -ENOMEM;
1471
Chris Masone02119d2008-09-05 16:13:11 -04001472 log_type = btrfs_dir_type(eb, di);
1473 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1474 name_len);
1475
1476 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001477 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1478 if (exists == 0)
1479 exists = 1;
1480 else
1481 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001482 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001483
Chris Masone02119d2008-09-05 16:13:11 -04001484 if (key->type == BTRFS_DIR_ITEM_KEY) {
1485 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1486 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001487 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001488 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1489 key->objectid,
1490 key->offset, name,
1491 name_len, 1);
1492 } else {
1493 BUG();
1494 }
David Sterbac7040052011-04-19 18:00:01 +02001495 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001496 /* we need a sequence number to insert, so we only
1497 * do inserts for the BTRFS_DIR_INDEX_KEY types
1498 */
1499 if (key->type != BTRFS_DIR_INDEX_KEY)
1500 goto out;
1501 goto insert;
1502 }
1503
1504 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1505 /* the existing item matches the logged item */
1506 if (found_key.objectid == log_key.objectid &&
1507 found_key.type == log_key.type &&
1508 found_key.offset == log_key.offset &&
1509 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1510 goto out;
1511 }
1512
1513 /*
1514 * don't drop the conflicting directory entry if the inode
1515 * for the new entry doesn't exist
1516 */
Chris Mason4bef0842008-09-08 11:18:08 -04001517 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001518 goto out;
1519
Chris Masone02119d2008-09-05 16:13:11 -04001520 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1521 BUG_ON(ret);
1522
1523 if (key->type == BTRFS_DIR_INDEX_KEY)
1524 goto insert;
1525out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001526 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001527 kfree(name);
1528 iput(dir);
1529 return 0;
1530
1531insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001533 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1534 name, name_len, log_type, &log_key);
1535
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001536 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001537 goto out;
1538}
1539
1540/*
1541 * find all the names in a directory item and reconcile them into
1542 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1543 * one name in a directory item, but the same code gets used for
1544 * both directory index types
1545 */
1546static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1547 struct btrfs_root *root,
1548 struct btrfs_path *path,
1549 struct extent_buffer *eb, int slot,
1550 struct btrfs_key *key)
1551{
1552 int ret;
1553 u32 item_size = btrfs_item_size_nr(eb, slot);
1554 struct btrfs_dir_item *di;
1555 int name_len;
1556 unsigned long ptr;
1557 unsigned long ptr_end;
1558
1559 ptr = btrfs_item_ptr_offset(eb, slot);
1560 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001561 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001562 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001563 if (verify_dir_item(root, eb, di))
1564 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001565 name_len = btrfs_dir_name_len(eb, di);
1566 ret = replay_one_name(trans, root, path, eb, di, key);
1567 BUG_ON(ret);
1568 ptr = (unsigned long)(di + 1);
1569 ptr += name_len;
1570 }
1571 return 0;
1572}
1573
1574/*
1575 * directory replay has two parts. There are the standard directory
1576 * items in the log copied from the subvolume, and range items
1577 * created in the log while the subvolume was logged.
1578 *
1579 * The range items tell us which parts of the key space the log
1580 * is authoritative for. During replay, if a key in the subvolume
1581 * directory is in a logged range item, but not actually in the log
1582 * that means it was deleted from the directory before the fsync
1583 * and should be removed.
1584 */
1585static noinline int find_dir_range(struct btrfs_root *root,
1586 struct btrfs_path *path,
1587 u64 dirid, int key_type,
1588 u64 *start_ret, u64 *end_ret)
1589{
1590 struct btrfs_key key;
1591 u64 found_end;
1592 struct btrfs_dir_log_item *item;
1593 int ret;
1594 int nritems;
1595
1596 if (*start_ret == (u64)-1)
1597 return 1;
1598
1599 key.objectid = dirid;
1600 key.type = key_type;
1601 key.offset = *start_ret;
1602
1603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1604 if (ret < 0)
1605 goto out;
1606 if (ret > 0) {
1607 if (path->slots[0] == 0)
1608 goto out;
1609 path->slots[0]--;
1610 }
1611 if (ret != 0)
1612 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1613
1614 if (key.type != key_type || key.objectid != dirid) {
1615 ret = 1;
1616 goto next;
1617 }
1618 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1619 struct btrfs_dir_log_item);
1620 found_end = btrfs_dir_log_end(path->nodes[0], item);
1621
1622 if (*start_ret >= key.offset && *start_ret <= found_end) {
1623 ret = 0;
1624 *start_ret = key.offset;
1625 *end_ret = found_end;
1626 goto out;
1627 }
1628 ret = 1;
1629next:
1630 /* check the next slot in the tree to see if it is a valid item */
1631 nritems = btrfs_header_nritems(path->nodes[0]);
1632 if (path->slots[0] >= nritems) {
1633 ret = btrfs_next_leaf(root, path);
1634 if (ret)
1635 goto out;
1636 } else {
1637 path->slots[0]++;
1638 }
1639
1640 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1641
1642 if (key.type != key_type || key.objectid != dirid) {
1643 ret = 1;
1644 goto out;
1645 }
1646 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1647 struct btrfs_dir_log_item);
1648 found_end = btrfs_dir_log_end(path->nodes[0], item);
1649 *start_ret = key.offset;
1650 *end_ret = found_end;
1651 ret = 0;
1652out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001653 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001654 return ret;
1655}
1656
1657/*
1658 * this looks for a given directory item in the log. If the directory
1659 * item is not in the log, the item is removed and the inode it points
1660 * to is unlinked
1661 */
1662static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
1664 struct btrfs_root *log,
1665 struct btrfs_path *path,
1666 struct btrfs_path *log_path,
1667 struct inode *dir,
1668 struct btrfs_key *dir_key)
1669{
1670 int ret;
1671 struct extent_buffer *eb;
1672 int slot;
1673 u32 item_size;
1674 struct btrfs_dir_item *di;
1675 struct btrfs_dir_item *log_di;
1676 int name_len;
1677 unsigned long ptr;
1678 unsigned long ptr_end;
1679 char *name;
1680 struct inode *inode;
1681 struct btrfs_key location;
1682
1683again:
1684 eb = path->nodes[0];
1685 slot = path->slots[0];
1686 item_size = btrfs_item_size_nr(eb, slot);
1687 ptr = btrfs_item_ptr_offset(eb, slot);
1688 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001689 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001690 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001691 if (verify_dir_item(root, eb, di)) {
1692 ret = -EIO;
1693 goto out;
1694 }
1695
Chris Masone02119d2008-09-05 16:13:11 -04001696 name_len = btrfs_dir_name_len(eb, di);
1697 name = kmalloc(name_len, GFP_NOFS);
1698 if (!name) {
1699 ret = -ENOMEM;
1700 goto out;
1701 }
1702 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1703 name_len);
1704 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001705 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001706 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1707 dir_key->objectid,
1708 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001709 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001710 log_di = btrfs_lookup_dir_index_item(trans, log,
1711 log_path,
1712 dir_key->objectid,
1713 dir_key->offset,
1714 name, name_len, 0);
1715 }
David Sterbac7040052011-04-19 18:00:01 +02001716 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001717 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001718 btrfs_release_path(path);
1719 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001720 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001721 if (!inode) {
1722 kfree(name);
1723 return -EIO;
1724 }
Chris Masone02119d2008-09-05 16:13:11 -04001725
1726 ret = link_to_fixup_dir(trans, root,
1727 path, location.objectid);
1728 BUG_ON(ret);
1729 btrfs_inc_nlink(inode);
1730 ret = btrfs_unlink_inode(trans, root, dir, inode,
1731 name, name_len);
1732 BUG_ON(ret);
Chris Masonb6305562012-07-02 15:29:53 -04001733
1734 btrfs_run_delayed_items(trans, root);
1735
Chris Masone02119d2008-09-05 16:13:11 -04001736 kfree(name);
1737 iput(inode);
1738
1739 /* there might still be more names under this key
1740 * check and repeat if required
1741 */
1742 ret = btrfs_search_slot(NULL, root, dir_key, path,
1743 0, 0);
1744 if (ret == 0)
1745 goto again;
1746 ret = 0;
1747 goto out;
1748 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001749 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001750 kfree(name);
1751
1752 ptr = (unsigned long)(di + 1);
1753 ptr += name_len;
1754 }
1755 ret = 0;
1756out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001757 btrfs_release_path(path);
1758 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001759 return ret;
1760}
1761
1762/*
1763 * deletion replay happens before we copy any new directory items
1764 * out of the log or out of backreferences from inodes. It
1765 * scans the log to find ranges of keys that log is authoritative for,
1766 * and then scans the directory to find items in those ranges that are
1767 * not present in the log.
1768 *
1769 * Anything we don't find in the log is unlinked and removed from the
1770 * directory.
1771 */
1772static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1773 struct btrfs_root *root,
1774 struct btrfs_root *log,
1775 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001776 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001777{
1778 u64 range_start;
1779 u64 range_end;
1780 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1781 int ret = 0;
1782 struct btrfs_key dir_key;
1783 struct btrfs_key found_key;
1784 struct btrfs_path *log_path;
1785 struct inode *dir;
1786
1787 dir_key.objectid = dirid;
1788 dir_key.type = BTRFS_DIR_ITEM_KEY;
1789 log_path = btrfs_alloc_path();
1790 if (!log_path)
1791 return -ENOMEM;
1792
1793 dir = read_one_inode(root, dirid);
1794 /* it isn't an error if the inode isn't there, that can happen
1795 * because we replay the deletes before we copy in the inode item
1796 * from the log
1797 */
1798 if (!dir) {
1799 btrfs_free_path(log_path);
1800 return 0;
1801 }
1802again:
1803 range_start = 0;
1804 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001805 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001806 if (del_all)
1807 range_end = (u64)-1;
1808 else {
1809 ret = find_dir_range(log, path, dirid, key_type,
1810 &range_start, &range_end);
1811 if (ret != 0)
1812 break;
1813 }
Chris Masone02119d2008-09-05 16:13:11 -04001814
1815 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001816 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001817 int nritems;
1818 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1819 0, 0);
1820 if (ret < 0)
1821 goto out;
1822
1823 nritems = btrfs_header_nritems(path->nodes[0]);
1824 if (path->slots[0] >= nritems) {
1825 ret = btrfs_next_leaf(root, path);
1826 if (ret)
1827 break;
1828 }
1829 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1830 path->slots[0]);
1831 if (found_key.objectid != dirid ||
1832 found_key.type != dir_key.type)
1833 goto next_type;
1834
1835 if (found_key.offset > range_end)
1836 break;
1837
1838 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001839 log_path, dir,
1840 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001841 BUG_ON(ret);
1842 if (found_key.offset == (u64)-1)
1843 break;
1844 dir_key.offset = found_key.offset + 1;
1845 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001846 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001847 if (range_end == (u64)-1)
1848 break;
1849 range_start = range_end + 1;
1850 }
1851
1852next_type:
1853 ret = 0;
1854 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1855 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1856 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001857 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001858 goto again;
1859 }
1860out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001861 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001862 btrfs_free_path(log_path);
1863 iput(dir);
1864 return ret;
1865}
1866
1867/*
1868 * the process_func used to replay items from the log tree. This
1869 * gets called in two different stages. The first stage just looks
1870 * for inodes and makes sure they are all copied into the subvolume.
1871 *
1872 * The second stage copies all the other item types from the log into
1873 * the subvolume. The two stage approach is slower, but gets rid of
1874 * lots of complexity around inodes referencing other inodes that exist
1875 * only in the log (references come from either directory items or inode
1876 * back refs).
1877 */
1878static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1879 struct walk_control *wc, u64 gen)
1880{
1881 int nritems;
1882 struct btrfs_path *path;
1883 struct btrfs_root *root = wc->replay_dest;
1884 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001885 int level;
1886 int i;
1887 int ret;
1888
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001889 ret = btrfs_read_buffer(eb, gen);
1890 if (ret)
1891 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001892
1893 level = btrfs_header_level(eb);
1894
1895 if (level != 0)
1896 return 0;
1897
1898 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001899 if (!path)
1900 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001901
1902 nritems = btrfs_header_nritems(eb);
1903 for (i = 0; i < nritems; i++) {
1904 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001905
1906 /* inode keys are done during the first stage */
1907 if (key.type == BTRFS_INODE_ITEM_KEY &&
1908 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001909 struct btrfs_inode_item *inode_item;
1910 u32 mode;
1911
1912 inode_item = btrfs_item_ptr(eb, i,
1913 struct btrfs_inode_item);
1914 mode = btrfs_inode_mode(eb, inode_item);
1915 if (S_ISDIR(mode)) {
1916 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001917 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001918 BUG_ON(ret);
1919 }
1920 ret = overwrite_item(wc->trans, root, path,
1921 eb, i, &key);
1922 BUG_ON(ret);
1923
Yan, Zhengc71bf092009-11-12 09:34:40 +00001924 /* for regular files, make sure corresponding
1925 * orhpan item exist. extents past the new EOF
1926 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001927 */
1928 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001929 ret = insert_orphan_item(wc->trans, root,
1930 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001931 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001932 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001933
Chris Masone02119d2008-09-05 16:13:11 -04001934 ret = link_to_fixup_dir(wc->trans, root,
1935 path, key.objectid);
1936 BUG_ON(ret);
1937 }
1938 if (wc->stage < LOG_WALK_REPLAY_ALL)
1939 continue;
1940
1941 /* these keys are simply copied */
1942 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1943 ret = overwrite_item(wc->trans, root, path,
1944 eb, i, &key);
1945 BUG_ON(ret);
1946 } else if (key.type == BTRFS_INODE_REF_KEY) {
1947 ret = add_inode_ref(wc->trans, root, log, path,
1948 eb, i, &key);
1949 BUG_ON(ret && ret != -ENOENT);
Mark Fashehf1863732012-08-08 11:32:27 -07001950 } else if (key.type == BTRFS_INODE_EXTREF_KEY) {
1951 ret = add_inode_ref(wc->trans, root, log, path,
1952 eb, i, &key);
1953 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001954 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1955 ret = replay_one_extent(wc->trans, root, path,
1956 eb, i, &key);
1957 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001958 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1959 key.type == BTRFS_DIR_INDEX_KEY) {
1960 ret = replay_one_dir_item(wc->trans, root, path,
1961 eb, i, &key);
1962 BUG_ON(ret);
1963 }
1964 }
1965 btrfs_free_path(path);
1966 return 0;
1967}
1968
Chris Masond3977122009-01-05 21:25:51 -05001969static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001970 struct btrfs_root *root,
1971 struct btrfs_path *path, int *level,
1972 struct walk_control *wc)
1973{
1974 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001975 u64 bytenr;
1976 u64 ptr_gen;
1977 struct extent_buffer *next;
1978 struct extent_buffer *cur;
1979 struct extent_buffer *parent;
1980 u32 blocksize;
1981 int ret = 0;
1982
1983 WARN_ON(*level < 0);
1984 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1985
Chris Masond3977122009-01-05 21:25:51 -05001986 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001987 WARN_ON(*level < 0);
1988 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1989 cur = path->nodes[*level];
1990
1991 if (btrfs_header_level(cur) != *level)
1992 WARN_ON(1);
1993
1994 if (path->slots[*level] >=
1995 btrfs_header_nritems(cur))
1996 break;
1997
1998 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1999 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2000 blocksize = btrfs_level_size(root, *level - 1);
2001
2002 parent = path->nodes[*level];
2003 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002004
2005 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002006 if (!next)
2007 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002008
Chris Masone02119d2008-09-05 16:13:11 -04002009 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002010 ret = wc->process_func(root, next, wc, ptr_gen);
2011 if (ret)
2012 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002013
Chris Masone02119d2008-09-05 16:13:11 -04002014 path->slots[*level]++;
2015 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002016 ret = btrfs_read_buffer(next, ptr_gen);
2017 if (ret) {
2018 free_extent_buffer(next);
2019 return ret;
2020 }
Chris Masone02119d2008-09-05 16:13:11 -04002021
2022 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002023 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002024 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002025 btrfs_wait_tree_block_writeback(next);
2026 btrfs_tree_unlock(next);
2027
Chris Masone02119d2008-09-05 16:13:11 -04002028 WARN_ON(root_owner !=
2029 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002030 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002031 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002032 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002033 }
2034 free_extent_buffer(next);
2035 continue;
2036 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002037 ret = btrfs_read_buffer(next, ptr_gen);
2038 if (ret) {
2039 free_extent_buffer(next);
2040 return ret;
2041 }
Chris Masone02119d2008-09-05 16:13:11 -04002042
2043 WARN_ON(*level <= 0);
2044 if (path->nodes[*level-1])
2045 free_extent_buffer(path->nodes[*level-1]);
2046 path->nodes[*level-1] = next;
2047 *level = btrfs_header_level(next);
2048 path->slots[*level] = 0;
2049 cond_resched();
2050 }
2051 WARN_ON(*level < 0);
2052 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2053
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002054 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002055
2056 cond_resched();
2057 return 0;
2058}
2059
Chris Masond3977122009-01-05 21:25:51 -05002060static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002061 struct btrfs_root *root,
2062 struct btrfs_path *path, int *level,
2063 struct walk_control *wc)
2064{
2065 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002066 int i;
2067 int slot;
2068 int ret;
2069
Chris Masond3977122009-01-05 21:25:51 -05002070 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002071 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002072 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002073 path->slots[i]++;
2074 *level = i;
2075 WARN_ON(*level == 0);
2076 return 0;
2077 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002078 struct extent_buffer *parent;
2079 if (path->nodes[*level] == root->node)
2080 parent = path->nodes[*level];
2081 else
2082 parent = path->nodes[*level + 1];
2083
2084 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002085 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002086 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002087 if (ret)
2088 return ret;
2089
Chris Masone02119d2008-09-05 16:13:11 -04002090 if (wc->free) {
2091 struct extent_buffer *next;
2092
2093 next = path->nodes[*level];
2094
2095 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002096 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002097 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002098 btrfs_wait_tree_block_writeback(next);
2099 btrfs_tree_unlock(next);
2100
Chris Masone02119d2008-09-05 16:13:11 -04002101 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002102 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002103 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002104 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04002105 BUG_ON(ret);
2106 }
2107 free_extent_buffer(path->nodes[*level]);
2108 path->nodes[*level] = NULL;
2109 *level = i + 1;
2110 }
2111 }
2112 return 1;
2113}
2114
2115/*
2116 * drop the reference count on the tree rooted at 'snap'. This traverses
2117 * the tree freeing any blocks that have a ref count of zero after being
2118 * decremented.
2119 */
2120static int walk_log_tree(struct btrfs_trans_handle *trans,
2121 struct btrfs_root *log, struct walk_control *wc)
2122{
2123 int ret = 0;
2124 int wret;
2125 int level;
2126 struct btrfs_path *path;
2127 int i;
2128 int orig_level;
2129
2130 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002131 if (!path)
2132 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002133
2134 level = btrfs_header_level(log->node);
2135 orig_level = level;
2136 path->nodes[level] = log->node;
2137 extent_buffer_get(log->node);
2138 path->slots[level] = 0;
2139
Chris Masond3977122009-01-05 21:25:51 -05002140 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002141 wret = walk_down_log_tree(trans, log, path, &level, wc);
2142 if (wret > 0)
2143 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002144 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002145 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002146 goto out;
2147 }
Chris Masone02119d2008-09-05 16:13:11 -04002148
2149 wret = walk_up_log_tree(trans, log, path, &level, wc);
2150 if (wret > 0)
2151 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002152 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002153 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002154 goto out;
2155 }
Chris Masone02119d2008-09-05 16:13:11 -04002156 }
2157
2158 /* was the root node processed? if not, catch it here */
2159 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002161 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002162 if (ret)
2163 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002164 if (wc->free) {
2165 struct extent_buffer *next;
2166
2167 next = path->nodes[orig_level];
2168
2169 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002170 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002171 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04002172 btrfs_wait_tree_block_writeback(next);
2173 btrfs_tree_unlock(next);
2174
Chris Masone02119d2008-09-05 16:13:11 -04002175 WARN_ON(log->root_key.objectid !=
2176 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002177 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002178 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002180 }
2181 }
2182
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002183out:
Chris Masone02119d2008-09-05 16:13:11 -04002184 for (i = 0; i <= orig_level; i++) {
2185 if (path->nodes[i]) {
2186 free_extent_buffer(path->nodes[i]);
2187 path->nodes[i] = NULL;
2188 }
2189 }
2190 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002191 return ret;
2192}
2193
Yan Zheng7237f182009-01-21 12:54:03 -05002194/*
2195 * helper function to update the item for a given subvolumes log root
2196 * in the tree of log roots
2197 */
2198static int update_log_root(struct btrfs_trans_handle *trans,
2199 struct btrfs_root *log)
2200{
2201 int ret;
2202
2203 if (log->log_transid == 1) {
2204 /* insert root item on the first sync */
2205 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2206 &log->root_key, &log->root_item);
2207 } else {
2208 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2209 &log->root_key, &log->root_item);
2210 }
2211 return ret;
2212}
2213
Chris Mason12fcfd22009-03-24 10:24:20 -04002214static int wait_log_commit(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002216{
2217 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002218 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002219
Yan Zheng7237f182009-01-21 12:54:03 -05002220 /*
2221 * we only allow two pending log transactions at a time,
2222 * so we know that if ours is more than 2 older than the
2223 * current transaction, we're done
2224 */
Chris Masone02119d2008-09-05 16:13:11 -04002225 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002226 prepare_to_wait(&root->log_commit_wait[index],
2227 &wait, TASK_UNINTERRUPTIBLE);
2228 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002229
2230 if (root->fs_info->last_trans_log_full_commit !=
2231 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002232 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002233 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002234
Yan Zheng7237f182009-01-21 12:54:03 -05002235 finish_wait(&root->log_commit_wait[index], &wait);
2236 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002237 } while (root->fs_info->last_trans_log_full_commit !=
2238 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002239 atomic_read(&root->log_commit[index]));
2240 return 0;
2241}
2242
Jeff Mahoney143bede2012-03-01 14:56:26 +01002243static void wait_for_writer(struct btrfs_trans_handle *trans,
2244 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002245{
2246 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002247 while (root->fs_info->last_trans_log_full_commit !=
2248 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002249 prepare_to_wait(&root->log_writer_wait,
2250 &wait, TASK_UNINTERRUPTIBLE);
2251 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002252 if (root->fs_info->last_trans_log_full_commit !=
2253 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002254 schedule();
2255 mutex_lock(&root->log_mutex);
2256 finish_wait(&root->log_writer_wait, &wait);
2257 }
Chris Masone02119d2008-09-05 16:13:11 -04002258}
2259
2260/*
2261 * btrfs_sync_log does sends a given tree log down to the disk and
2262 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002263 * you know that any inodes previously logged are safely on disk only
2264 * if it returns 0.
2265 *
2266 * Any other return value means you need to call btrfs_commit_transaction.
2267 * Some of the edge cases for fsyncing directories that have had unlinks
2268 * or renames done in the past mean that sometimes the only safe
2269 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2270 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002271 */
2272int btrfs_sync_log(struct btrfs_trans_handle *trans,
2273 struct btrfs_root *root)
2274{
Yan Zheng7237f182009-01-21 12:54:03 -05002275 int index1;
2276 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002277 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002278 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002279 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002280 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002281 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002282
Yan Zheng7237f182009-01-21 12:54:03 -05002283 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002284 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002285 index1 = root->log_transid % 2;
2286 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002287 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002288 mutex_unlock(&root->log_mutex);
2289 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002290 }
Yan Zheng7237f182009-01-21 12:54:03 -05002291 atomic_set(&root->log_commit[index1], 1);
2292
2293 /* wait for previous tree log sync to complete */
2294 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002295 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002296 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002297 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002298 /* when we're on an ssd, just kick the log commit out */
2299 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002300 mutex_unlock(&root->log_mutex);
2301 schedule_timeout_uninterruptible(1);
2302 mutex_lock(&root->log_mutex);
2303 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002304 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002305 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002306 break;
2307 }
Chris Masond0c803c2008-09-11 16:17:57 -04002308
Chris Mason12fcfd22009-03-24 10:24:20 -04002309 /* bail out if we need to do a full commit */
2310 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2311 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002312 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002313 mutex_unlock(&root->log_mutex);
2314 goto out;
2315 }
2316
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002317 if (log_transid % 2 == 0)
2318 mark = EXTENT_DIRTY;
2319 else
2320 mark = EXTENT_NEW;
2321
Chris Mason690587d2009-10-13 13:29:19 -04002322 /* we start IO on all the marked extents here, but we don't actually
2323 * wait for them until later.
2324 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002325 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002326 if (ret) {
2327 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002328 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002329 mutex_unlock(&root->log_mutex);
2330 goto out;
2331 }
Yan Zheng7237f182009-01-21 12:54:03 -05002332
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002334
Yan Zheng7237f182009-01-21 12:54:03 -05002335 root->log_transid++;
2336 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002337 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002338 smp_mb();
2339 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002340 * IO has been started, blocks of the log tree have WRITTEN flag set
2341 * in their headers. new modifications of the log will be written to
2342 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002343 */
2344 mutex_unlock(&root->log_mutex);
2345
2346 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002347 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002348 atomic_inc(&log_root_tree->log_writers);
2349 mutex_unlock(&log_root_tree->log_mutex);
2350
2351 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002352
2353 mutex_lock(&log_root_tree->log_mutex);
2354 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2355 smp_mb();
2356 if (waitqueue_active(&log_root_tree->log_writer_wait))
2357 wake_up(&log_root_tree->log_writer_wait);
2358 }
2359
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002360 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002361 if (ret != -ENOSPC) {
2362 btrfs_abort_transaction(trans, root, ret);
2363 mutex_unlock(&log_root_tree->log_mutex);
2364 goto out;
2365 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002366 root->fs_info->last_trans_log_full_commit = trans->transid;
2367 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002368 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002369 mutex_unlock(&log_root_tree->log_mutex);
2370 ret = -EAGAIN;
2371 goto out;
2372 }
2373
Yan Zheng7237f182009-01-21 12:54:03 -05002374 index2 = log_root_tree->log_transid % 2;
2375 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002376 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002377 wait_log_commit(trans, log_root_tree,
2378 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002379 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002380 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002381 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002382 goto out;
2383 }
2384 atomic_set(&log_root_tree->log_commit[index2], 1);
2385
Chris Mason12fcfd22009-03-24 10:24:20 -04002386 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2387 wait_log_commit(trans, log_root_tree,
2388 log_root_tree->log_transid - 1);
2389 }
Yan Zheng7237f182009-01-21 12:54:03 -05002390
Chris Mason12fcfd22009-03-24 10:24:20 -04002391 wait_for_writer(trans, log_root_tree);
2392
2393 /*
2394 * now that we've moved on to the tree of log tree roots,
2395 * check the full commit flag again
2396 */
2397 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002398 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002399 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002400 mutex_unlock(&log_root_tree->log_mutex);
2401 ret = -EAGAIN;
2402 goto out_wake_log_root;
2403 }
Yan Zheng7237f182009-01-21 12:54:03 -05002404
2405 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002406 &log_root_tree->dirty_log_pages,
2407 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002408 if (ret) {
2409 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002410 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002411 mutex_unlock(&log_root_tree->log_mutex);
2412 goto out_wake_log_root;
2413 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002414 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002415 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002416
David Sterba6c417612011-04-13 15:41:04 +02002417 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002418 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002419 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002420 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002421
Yan Zheng7237f182009-01-21 12:54:03 -05002422 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002423 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002424
2425 mutex_unlock(&log_root_tree->log_mutex);
2426
2427 /*
2428 * nobody else is going to jump in and write the the ctree
2429 * super here because the log_commit atomic below is protecting
2430 * us. We must be called with a transaction handle pinning
2431 * the running transaction open, so a full commit can't hop
2432 * in and cause problems either.
2433 */
Arne Jansena2de7332011-03-08 14:14:00 +01002434 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002435 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002436 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002437 if (ret) {
2438 btrfs_abort_transaction(trans, root, ret);
2439 goto out_wake_log_root;
2440 }
Yan Zheng7237f182009-01-21 12:54:03 -05002441
Chris Mason257c62e2009-10-13 13:21:08 -04002442 mutex_lock(&root->log_mutex);
2443 if (root->last_log_commit < log_transid)
2444 root->last_log_commit = log_transid;
2445 mutex_unlock(&root->log_mutex);
2446
Chris Mason12fcfd22009-03-24 10:24:20 -04002447out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002448 atomic_set(&log_root_tree->log_commit[index2], 0);
2449 smp_mb();
2450 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2451 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002452out:
Yan Zheng7237f182009-01-21 12:54:03 -05002453 atomic_set(&root->log_commit[index1], 0);
2454 smp_mb();
2455 if (waitqueue_active(&root->log_commit_wait[index1]))
2456 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002457 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002458}
2459
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002460static void free_log_tree(struct btrfs_trans_handle *trans,
2461 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002462{
2463 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002464 u64 start;
2465 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002466 struct walk_control wc = {
2467 .free = 1,
2468 .process_func = process_one_buffer
2469 };
2470
Chris Masone02119d2008-09-05 16:13:11 -04002471 ret = walk_log_tree(trans, log, &wc);
2472 BUG_ON(ret);
2473
Chris Masond3977122009-01-05 21:25:51 -05002474 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002475 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002476 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2477 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002478 if (ret)
2479 break;
2480
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002481 clear_extent_bits(&log->dirty_log_pages, start, end,
2482 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002483 }
2484
Josef Bacik2ab28f32012-10-12 15:27:49 -04002485 /*
2486 * We may have short-circuited the log tree with the full commit logic
2487 * and left ordered extents on our list, so clear these out to keep us
2488 * from leaking inodes and memory.
2489 */
2490 btrfs_free_logged_extents(log, 0);
2491 btrfs_free_logged_extents(log, 1);
2492
Yan Zheng7237f182009-01-21 12:54:03 -05002493 free_extent_buffer(log->node);
2494 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002495}
2496
2497/*
2498 * free all the extents used by the tree log. This should be called
2499 * at commit time of the full transaction
2500 */
2501int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2502{
2503 if (root->log_root) {
2504 free_log_tree(trans, root->log_root);
2505 root->log_root = NULL;
2506 }
2507 return 0;
2508}
2509
2510int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2511 struct btrfs_fs_info *fs_info)
2512{
2513 if (fs_info->log_root_tree) {
2514 free_log_tree(trans, fs_info->log_root_tree);
2515 fs_info->log_root_tree = NULL;
2516 }
Chris Masone02119d2008-09-05 16:13:11 -04002517 return 0;
2518}
2519
2520/*
Chris Masone02119d2008-09-05 16:13:11 -04002521 * If both a file and directory are logged, and unlinks or renames are
2522 * mixed in, we have a few interesting corners:
2523 *
2524 * create file X in dir Y
2525 * link file X to X.link in dir Y
2526 * fsync file X
2527 * unlink file X but leave X.link
2528 * fsync dir Y
2529 *
2530 * After a crash we would expect only X.link to exist. But file X
2531 * didn't get fsync'd again so the log has back refs for X and X.link.
2532 *
2533 * We solve this by removing directory entries and inode backrefs from the
2534 * log when a file that was logged in the current transaction is
2535 * unlinked. Any later fsync will include the updated log entries, and
2536 * we'll be able to reconstruct the proper directory items from backrefs.
2537 *
2538 * This optimizations allows us to avoid relogging the entire inode
2539 * or the entire directory.
2540 */
2541int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2542 struct btrfs_root *root,
2543 const char *name, int name_len,
2544 struct inode *dir, u64 index)
2545{
2546 struct btrfs_root *log;
2547 struct btrfs_dir_item *di;
2548 struct btrfs_path *path;
2549 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002550 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002551 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002552 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002553
Chris Mason3a5f1d42008-09-11 15:53:37 -04002554 if (BTRFS_I(dir)->logged_trans < trans->transid)
2555 return 0;
2556
Chris Masone02119d2008-09-05 16:13:11 -04002557 ret = join_running_log_trans(root);
2558 if (ret)
2559 return 0;
2560
2561 mutex_lock(&BTRFS_I(dir)->log_mutex);
2562
2563 log = root->log_root;
2564 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002565 if (!path) {
2566 err = -ENOMEM;
2567 goto out_unlock;
2568 }
liubo2a29edc2011-01-26 06:22:08 +00002569
Li Zefan33345d012011-04-20 10:31:50 +08002570 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002571 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002572 if (IS_ERR(di)) {
2573 err = PTR_ERR(di);
2574 goto fail;
2575 }
2576 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002577 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2578 bytes_del += name_len;
2579 BUG_ON(ret);
2580 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002581 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002582 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002583 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002584 if (IS_ERR(di)) {
2585 err = PTR_ERR(di);
2586 goto fail;
2587 }
2588 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002589 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2590 bytes_del += name_len;
2591 BUG_ON(ret);
2592 }
2593
2594 /* update the directory size in the log to reflect the names
2595 * we have removed
2596 */
2597 if (bytes_del) {
2598 struct btrfs_key key;
2599
Li Zefan33345d012011-04-20 10:31:50 +08002600 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002601 key.offset = 0;
2602 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002603 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002604
2605 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002606 if (ret < 0) {
2607 err = ret;
2608 goto fail;
2609 }
Chris Masone02119d2008-09-05 16:13:11 -04002610 if (ret == 0) {
2611 struct btrfs_inode_item *item;
2612 u64 i_size;
2613
2614 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2615 struct btrfs_inode_item);
2616 i_size = btrfs_inode_size(path->nodes[0], item);
2617 if (i_size > bytes_del)
2618 i_size -= bytes_del;
2619 else
2620 i_size = 0;
2621 btrfs_set_inode_size(path->nodes[0], item, i_size);
2622 btrfs_mark_buffer_dirty(path->nodes[0]);
2623 } else
2624 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002625 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002626 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002627fail:
Chris Masone02119d2008-09-05 16:13:11 -04002628 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002629out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002630 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002631 if (ret == -ENOSPC) {
2632 root->fs_info->last_trans_log_full_commit = trans->transid;
2633 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002634 } else if (ret < 0)
2635 btrfs_abort_transaction(trans, root, ret);
2636
Chris Mason12fcfd22009-03-24 10:24:20 -04002637 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002638
Andi Kleen411fc6b2010-10-29 15:14:31 -04002639 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002640}
2641
2642/* see comments for btrfs_del_dir_entries_in_log */
2643int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2644 struct btrfs_root *root,
2645 const char *name, int name_len,
2646 struct inode *inode, u64 dirid)
2647{
2648 struct btrfs_root *log;
2649 u64 index;
2650 int ret;
2651
Chris Mason3a5f1d42008-09-11 15:53:37 -04002652 if (BTRFS_I(inode)->logged_trans < trans->transid)
2653 return 0;
2654
Chris Masone02119d2008-09-05 16:13:11 -04002655 ret = join_running_log_trans(root);
2656 if (ret)
2657 return 0;
2658 log = root->log_root;
2659 mutex_lock(&BTRFS_I(inode)->log_mutex);
2660
Li Zefan33345d012011-04-20 10:31:50 +08002661 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002662 dirid, &index);
2663 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002664 if (ret == -ENOSPC) {
2665 root->fs_info->last_trans_log_full_commit = trans->transid;
2666 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002667 } else if (ret < 0 && ret != -ENOENT)
2668 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002669 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002670
Chris Masone02119d2008-09-05 16:13:11 -04002671 return ret;
2672}
2673
2674/*
2675 * creates a range item in the log for 'dirid'. first_offset and
2676 * last_offset tell us which parts of the key space the log should
2677 * be considered authoritative for.
2678 */
2679static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2680 struct btrfs_root *log,
2681 struct btrfs_path *path,
2682 int key_type, u64 dirid,
2683 u64 first_offset, u64 last_offset)
2684{
2685 int ret;
2686 struct btrfs_key key;
2687 struct btrfs_dir_log_item *item;
2688
2689 key.objectid = dirid;
2690 key.offset = first_offset;
2691 if (key_type == BTRFS_DIR_ITEM_KEY)
2692 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2693 else
2694 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2695 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002696 if (ret)
2697 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002698
2699 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2700 struct btrfs_dir_log_item);
2701 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2702 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002703 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002704 return 0;
2705}
2706
2707/*
2708 * log all the items included in the current transaction for a given
2709 * directory. This also creates the range items in the log tree required
2710 * to replay anything deleted before the fsync
2711 */
2712static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2713 struct btrfs_root *root, struct inode *inode,
2714 struct btrfs_path *path,
2715 struct btrfs_path *dst_path, int key_type,
2716 u64 min_offset, u64 *last_offset_ret)
2717{
2718 struct btrfs_key min_key;
2719 struct btrfs_key max_key;
2720 struct btrfs_root *log = root->log_root;
2721 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002722 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002723 int ret;
2724 int i;
2725 int nritems;
2726 u64 first_offset = min_offset;
2727 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002728 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002729
2730 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002731 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002732 max_key.offset = (u64)-1;
2733 max_key.type = key_type;
2734
Li Zefan33345d012011-04-20 10:31:50 +08002735 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002736 min_key.type = key_type;
2737 min_key.offset = min_offset;
2738
2739 path->keep_locks = 1;
2740
2741 ret = btrfs_search_forward(root, &min_key, &max_key,
2742 path, 0, trans->transid);
2743
2744 /*
2745 * we didn't find anything from this transaction, see if there
2746 * is anything at all
2747 */
Li Zefan33345d012011-04-20 10:31:50 +08002748 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2749 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002750 min_key.type = key_type;
2751 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002752 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002753 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2754 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002755 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002756 return ret;
2757 }
Li Zefan33345d012011-04-20 10:31:50 +08002758 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002759
2760 /* if ret == 0 there are items for this type,
2761 * create a range to tell us the last key of this type.
2762 * otherwise, there are no items in this directory after
2763 * *min_offset, and we create a range to indicate that.
2764 */
2765 if (ret == 0) {
2766 struct btrfs_key tmp;
2767 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2768 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002769 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002770 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002771 }
2772 goto done;
2773 }
2774
2775 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002776 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002777 if (ret == 0) {
2778 struct btrfs_key tmp;
2779 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2780 if (key_type == tmp.type) {
2781 first_offset = tmp.offset;
2782 ret = overwrite_item(trans, log, dst_path,
2783 path->nodes[0], path->slots[0],
2784 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002785 if (ret) {
2786 err = ret;
2787 goto done;
2788 }
Chris Masone02119d2008-09-05 16:13:11 -04002789 }
2790 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002791 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002792
2793 /* find the first key from this transaction again */
2794 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2795 if (ret != 0) {
2796 WARN_ON(1);
2797 goto done;
2798 }
2799
2800 /*
2801 * we have a block from this transaction, log every item in it
2802 * from our directory
2803 */
Chris Masond3977122009-01-05 21:25:51 -05002804 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002805 struct btrfs_key tmp;
2806 src = path->nodes[0];
2807 nritems = btrfs_header_nritems(src);
2808 for (i = path->slots[0]; i < nritems; i++) {
2809 btrfs_item_key_to_cpu(src, &min_key, i);
2810
Li Zefan33345d012011-04-20 10:31:50 +08002811 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002812 goto done;
2813 ret = overwrite_item(trans, log, dst_path, src, i,
2814 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002815 if (ret) {
2816 err = ret;
2817 goto done;
2818 }
Chris Masone02119d2008-09-05 16:13:11 -04002819 }
2820 path->slots[0] = nritems;
2821
2822 /*
2823 * look ahead to the next item and see if it is also
2824 * from this directory and from this transaction
2825 */
2826 ret = btrfs_next_leaf(root, path);
2827 if (ret == 1) {
2828 last_offset = (u64)-1;
2829 goto done;
2830 }
2831 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002832 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002833 last_offset = (u64)-1;
2834 goto done;
2835 }
2836 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2837 ret = overwrite_item(trans, log, dst_path,
2838 path->nodes[0], path->slots[0],
2839 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002840 if (ret)
2841 err = ret;
2842 else
2843 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002844 goto done;
2845 }
2846 }
2847done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002848 btrfs_release_path(path);
2849 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002850
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002851 if (err == 0) {
2852 *last_offset_ret = last_offset;
2853 /*
2854 * insert the log range keys to indicate where the log
2855 * is valid
2856 */
2857 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002858 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 if (ret)
2860 err = ret;
2861 }
2862 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002863}
2864
2865/*
2866 * logging directories is very similar to logging inodes, We find all the items
2867 * from the current transaction and write them to the log.
2868 *
2869 * The recovery code scans the directory in the subvolume, and if it finds a
2870 * key in the range logged that is not present in the log tree, then it means
2871 * that dir entry was unlinked during the transaction.
2872 *
2873 * In order for that scan to work, we must include one key smaller than
2874 * the smallest logged by this transaction and one key larger than the largest
2875 * key logged by this transaction.
2876 */
2877static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2878 struct btrfs_root *root, struct inode *inode,
2879 struct btrfs_path *path,
2880 struct btrfs_path *dst_path)
2881{
2882 u64 min_key;
2883 u64 max_key;
2884 int ret;
2885 int key_type = BTRFS_DIR_ITEM_KEY;
2886
2887again:
2888 min_key = 0;
2889 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002890 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002891 ret = log_dir_items(trans, root, inode, path,
2892 dst_path, key_type, min_key,
2893 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002894 if (ret)
2895 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002896 if (max_key == (u64)-1)
2897 break;
2898 min_key = max_key + 1;
2899 }
2900
2901 if (key_type == BTRFS_DIR_ITEM_KEY) {
2902 key_type = BTRFS_DIR_INDEX_KEY;
2903 goto again;
2904 }
2905 return 0;
2906}
2907
2908/*
2909 * a helper function to drop items from the log before we relog an
2910 * inode. max_key_type indicates the highest item type to remove.
2911 * This cannot be run for file data extents because it does not
2912 * free the extents they point to.
2913 */
2914static int drop_objectid_items(struct btrfs_trans_handle *trans,
2915 struct btrfs_root *log,
2916 struct btrfs_path *path,
2917 u64 objectid, int max_key_type)
2918{
2919 int ret;
2920 struct btrfs_key key;
2921 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04002922 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002923
2924 key.objectid = objectid;
2925 key.type = max_key_type;
2926 key.offset = (u64)-1;
2927
Chris Masond3977122009-01-05 21:25:51 -05002928 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002929 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002930 BUG_ON(ret == 0);
2931 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002932 break;
2933
2934 if (path->slots[0] == 0)
2935 break;
2936
2937 path->slots[0]--;
2938 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2939 path->slots[0]);
2940
2941 if (found_key.objectid != objectid)
2942 break;
2943
Josef Bacik18ec90d2012-09-28 11:56:28 -04002944 found_key.offset = 0;
2945 found_key.type = 0;
2946 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
2947 &start_slot);
2948
2949 ret = btrfs_del_items(trans, log, path, start_slot,
2950 path->slots[0] - start_slot + 1);
2951 /*
2952 * If start slot isn't 0 then we don't need to re-search, we've
2953 * found the last guy with the objectid in this tree.
2954 */
2955 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002956 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002957 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002958 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002959 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04002960 if (ret > 0)
2961 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002962 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002963}
2964
Josef Bacik94edf4a2012-09-25 14:56:25 -04002965static void fill_inode_item(struct btrfs_trans_handle *trans,
2966 struct extent_buffer *leaf,
2967 struct btrfs_inode_item *item,
2968 struct inode *inode, int log_inode_only)
2969{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002970 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04002971
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002972 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002973
2974 if (log_inode_only) {
2975 /* set the generation to zero so the recover code
2976 * can tell the difference between an logging
2977 * just to say 'this inode exists' and a logging
2978 * to say 'update this inode with these values'
2979 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002980 btrfs_set_token_inode_generation(leaf, item, 0, &token);
2981 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002982 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002983 btrfs_set_token_inode_generation(leaf, item,
2984 BTRFS_I(inode)->generation,
2985 &token);
2986 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04002987 }
2988
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04002989 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2990 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2991 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2992 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
2993
2994 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2995 inode->i_atime.tv_sec, &token);
2996 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2997 inode->i_atime.tv_nsec, &token);
2998
2999 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3000 inode->i_mtime.tv_sec, &token);
3001 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3002 inode->i_mtime.tv_nsec, &token);
3003
3004 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3005 inode->i_ctime.tv_sec, &token);
3006 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3007 inode->i_ctime.tv_nsec, &token);
3008
3009 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3010 &token);
3011
3012 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3013 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3014 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3015 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3016 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003017}
3018
Josef Bacika95249b2012-10-11 16:17:34 -04003019static int log_inode_item(struct btrfs_trans_handle *trans,
3020 struct btrfs_root *log, struct btrfs_path *path,
3021 struct inode *inode)
3022{
3023 struct btrfs_inode_item *inode_item;
3024 struct btrfs_key key;
3025 int ret;
3026
3027 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3028 ret = btrfs_insert_empty_item(trans, log, path, &key,
3029 sizeof(*inode_item));
3030 if (ret && ret != -EEXIST)
3031 return ret;
3032 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3033 struct btrfs_inode_item);
3034 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3035 btrfs_release_path(path);
3036 return 0;
3037}
3038
Chris Mason31ff1cd2008-09-11 16:17:57 -04003039static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003040 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003041 struct btrfs_path *dst_path,
3042 struct extent_buffer *src,
3043 int start_slot, int nr, int inode_only)
3044{
3045 unsigned long src_offset;
3046 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003047 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003048 struct btrfs_file_extent_item *extent;
3049 struct btrfs_inode_item *inode_item;
3050 int ret;
3051 struct btrfs_key *ins_keys;
3052 u32 *ins_sizes;
3053 char *ins_data;
3054 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003055 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003056 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003057
3058 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003059
3060 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3061 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003062 if (!ins_data)
3063 return -ENOMEM;
3064
Chris Mason31ff1cd2008-09-11 16:17:57 -04003065 ins_sizes = (u32 *)ins_data;
3066 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3067
3068 for (i = 0; i < nr; i++) {
3069 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3070 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3071 }
3072 ret = btrfs_insert_empty_items(trans, log, dst_path,
3073 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003074 if (ret) {
3075 kfree(ins_data);
3076 return ret;
3077 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003078
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003079 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003080 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3081 dst_path->slots[0]);
3082
3083 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3084
Josef Bacik94edf4a2012-09-25 14:56:25 -04003085 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003086 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3087 dst_path->slots[0],
3088 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003089 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3090 inode, inode_only == LOG_INODE_EXISTS);
3091 } else {
3092 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3093 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003094 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003095
Chris Mason31ff1cd2008-09-11 16:17:57 -04003096 /* take a reference on file data extents so that truncates
3097 * or deletes of this inode don't have to relog the inode
3098 * again
3099 */
Liu Bod2794402012-08-29 01:07:56 -06003100 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3101 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003102 int found_type;
3103 extent = btrfs_item_ptr(src, start_slot + i,
3104 struct btrfs_file_extent_item);
3105
liubo8e531cd2011-05-06 10:36:09 +08003106 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3107 continue;
3108
Chris Mason31ff1cd2008-09-11 16:17:57 -04003109 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003110 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 u64 ds, dl, cs, cl;
3112 ds = btrfs_file_extent_disk_bytenr(src,
3113 extent);
3114 /* ds == 0 is a hole */
3115 if (ds == 0)
3116 continue;
3117
3118 dl = btrfs_file_extent_disk_num_bytes(src,
3119 extent);
3120 cs = btrfs_file_extent_offset(src, extent);
3121 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003122 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003123 if (btrfs_file_extent_compression(src,
3124 extent)) {
3125 cs = 0;
3126 cl = dl;
3127 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128
3129 ret = btrfs_lookup_csums_range(
3130 log->fs_info->csum_root,
3131 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003132 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003134 }
3135 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003136 }
3137
3138 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003139 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003140 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003141
3142 /*
3143 * we have to do this after the loop above to avoid changing the
3144 * log tree while trying to change the log tree.
3145 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003146 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003147 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003148 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3149 struct btrfs_ordered_sum,
3150 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003151 if (!ret)
3152 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003153 list_del(&sums->list);
3154 kfree(sums);
3155 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003156 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003157}
3158
Josef Bacik5dc562c2012-08-17 13:14:17 -04003159static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3160{
3161 struct extent_map *em1, *em2;
3162
3163 em1 = list_entry(a, struct extent_map, list);
3164 em2 = list_entry(b, struct extent_map, list);
3165
3166 if (em1->start < em2->start)
3167 return -1;
3168 else if (em1->start > em2->start)
3169 return 1;
3170 return 0;
3171}
3172
Josef Bacik70c8a912012-10-11 16:54:30 -04003173static int drop_adjacent_extents(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root, struct inode *inode,
3175 struct extent_map *em,
3176 struct btrfs_path *path)
3177{
3178 struct btrfs_file_extent_item *fi;
3179 struct extent_buffer *leaf;
3180 struct btrfs_key key, new_key;
3181 struct btrfs_map_token token;
3182 u64 extent_end;
3183 u64 extent_offset = 0;
3184 int extent_type;
3185 int del_slot = 0;
3186 int del_nr = 0;
3187 int ret = 0;
3188
3189 while (1) {
3190 btrfs_init_map_token(&token);
3191 leaf = path->nodes[0];
3192 path->slots[0]++;
3193 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3194 if (del_nr) {
3195 ret = btrfs_del_items(trans, root, path,
3196 del_slot, del_nr);
3197 if (ret)
3198 return ret;
3199 del_nr = 0;
3200 }
3201
3202 ret = btrfs_next_leaf_write(trans, root, path, 1);
3203 if (ret < 0)
3204 return ret;
3205 if (ret > 0)
3206 return 0;
3207 leaf = path->nodes[0];
3208 }
3209
3210 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3211 if (key.objectid != btrfs_ino(inode) ||
3212 key.type != BTRFS_EXTENT_DATA_KEY ||
3213 key.offset >= em->start + em->len)
3214 break;
3215
3216 fi = btrfs_item_ptr(leaf, path->slots[0],
3217 struct btrfs_file_extent_item);
3218 extent_type = btrfs_token_file_extent_type(leaf, fi, &token);
3219 if (extent_type == BTRFS_FILE_EXTENT_REG ||
3220 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
3221 extent_offset = btrfs_token_file_extent_offset(leaf,
3222 fi, &token);
3223 extent_end = key.offset +
3224 btrfs_token_file_extent_num_bytes(leaf, fi,
3225 &token);
3226 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3227 extent_end = key.offset +
3228 btrfs_file_extent_inline_len(leaf, fi);
3229 } else {
3230 BUG();
3231 }
3232
3233 if (extent_end <= em->len + em->start) {
3234 if (!del_nr) {
3235 del_slot = path->slots[0];
3236 }
3237 del_nr++;
3238 continue;
3239 }
3240
3241 /*
3242 * Ok so we'll ignore previous items if we log a new extent,
3243 * which can lead to overlapping extents, so if we have an
3244 * existing extent we want to adjust we _have_ to check the next
3245 * guy to make sure we even need this extent anymore, this keeps
3246 * us from panicing in set_item_key_safe.
3247 */
3248 if (path->slots[0] < btrfs_header_nritems(leaf) - 1) {
3249 struct btrfs_key tmp_key;
3250
3251 btrfs_item_key_to_cpu(leaf, &tmp_key,
3252 path->slots[0] + 1);
3253 if (tmp_key.objectid == btrfs_ino(inode) &&
3254 tmp_key.type == BTRFS_EXTENT_DATA_KEY &&
3255 tmp_key.offset <= em->start + em->len) {
3256 if (!del_nr)
3257 del_slot = path->slots[0];
3258 del_nr++;
3259 continue;
3260 }
3261 }
3262
3263 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
3264 memcpy(&new_key, &key, sizeof(new_key));
3265 new_key.offset = em->start + em->len;
3266 btrfs_set_item_key_safe(trans, root, path, &new_key);
3267 extent_offset += em->start + em->len - key.offset;
3268 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset,
3269 &token);
3270 btrfs_set_token_file_extent_num_bytes(leaf, fi, extent_end -
3271 (em->start + em->len),
3272 &token);
3273 btrfs_mark_buffer_dirty(leaf);
3274 }
3275
3276 if (del_nr)
3277 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
3278
3279 return ret;
3280}
Josef Bacik5dc562c2012-08-17 13:14:17 -04003281
3282static int log_one_extent(struct btrfs_trans_handle *trans,
3283 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003284 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003285{
3286 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003287 struct btrfs_file_extent_item *fi;
3288 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003289 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003290 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003291 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003292 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003293 u64 mod_start = em->mod_start;
3294 u64 mod_len = em->mod_len;
3295 u64 csum_offset;
3296 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003297 u64 extent_offset = em->start - em->orig_start;
3298 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003299 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003300 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003301 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003302
Josef Bacik70c8a912012-10-11 16:54:30 -04003303 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003304 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003305 key.objectid = btrfs_ino(inode);
3306 key.type = BTRFS_EXTENT_DATA_KEY;
3307 key.offset = em->start;
3308 path->really_keep_locks = 1;
3309
3310 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
3311 if (ret && ret != -EEXIST) {
3312 path->really_keep_locks = 0;
3313 return ret;
3314 }
3315 leaf = path->nodes[0];
3316 fi = btrfs_item_ptr(leaf, path->slots[0],
3317 struct btrfs_file_extent_item);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003318 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3319 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003320 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3321 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003322 btrfs_set_token_file_extent_type(leaf, fi,
3323 BTRFS_FILE_EXTENT_PREALLOC,
3324 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003325 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003326 btrfs_set_token_file_extent_type(leaf, fi,
3327 BTRFS_FILE_EXTENT_REG,
3328 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003329 if (em->block_start == 0)
3330 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003331 }
3332
Josef Bacik70c8a912012-10-11 16:54:30 -04003333 block_len = max(em->block_len, em->orig_block_len);
3334 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003335 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3336 em->block_start,
3337 &token);
3338 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3339 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003340 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003341 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3342 em->block_start -
3343 extent_offset, &token);
3344 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3345 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003346 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003347 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3348 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3349 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003350 }
3351
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003352 btrfs_set_token_file_extent_offset(leaf, fi,
3353 em->start - em->orig_start,
3354 &token);
3355 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
3356 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->len, &token);
3357 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3358 &token);
3359 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3360 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003361 btrfs_mark_buffer_dirty(leaf);
3362
3363 /*
3364 * Have to check the extent to the right of us to make sure it doesn't
3365 * fall in our current range. We're ok if the previous extent is in our
3366 * range since the recovery stuff will run us in key order and thus just
3367 * drop the part we overwrote.
3368 */
3369 ret = drop_adjacent_extents(trans, log, inode, em, path);
3370 btrfs_release_path(path);
3371 path->really_keep_locks = 0;
3372 if (ret) {
3373 return ret;
3374 }
3375
3376 if (skip_csum)
3377 return 0;
3378
Liu Bo192000d2013-01-06 03:38:22 +00003379 if (em->compress_type) {
3380 csum_offset = 0;
3381 csum_len = block_len;
3382 }
3383
Josef Bacik2ab28f32012-10-12 15:27:49 -04003384 /*
3385 * First check and see if our csums are on our outstanding ordered
3386 * extents.
3387 */
3388again:
3389 spin_lock_irq(&log->log_extents_lock[index]);
3390 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3391 struct btrfs_ordered_sum *sum;
3392
3393 if (!mod_len)
3394 break;
3395
3396 if (ordered->inode != inode)
3397 continue;
3398
3399 if (ordered->file_offset + ordered->len <= mod_start ||
3400 mod_start + mod_len <= ordered->file_offset)
3401 continue;
3402
3403 /*
3404 * We are going to copy all the csums on this ordered extent, so
3405 * go ahead and adjust mod_start and mod_len in case this
3406 * ordered extent has already been logged.
3407 */
3408 if (ordered->file_offset > mod_start) {
3409 if (ordered->file_offset + ordered->len >=
3410 mod_start + mod_len)
3411 mod_len = ordered->file_offset - mod_start;
3412 /*
3413 * If we have this case
3414 *
3415 * |--------- logged extent ---------|
3416 * |----- ordered extent ----|
3417 *
3418 * Just don't mess with mod_start and mod_len, we'll
3419 * just end up logging more csums than we need and it
3420 * will be ok.
3421 */
3422 } else {
3423 if (ordered->file_offset + ordered->len <
3424 mod_start + mod_len) {
3425 mod_len = (mod_start + mod_len) -
3426 (ordered->file_offset + ordered->len);
3427 mod_start = ordered->file_offset +
3428 ordered->len;
3429 } else {
3430 mod_len = 0;
3431 }
3432 }
3433
3434 /*
3435 * To keep us from looping for the above case of an ordered
3436 * extent that falls inside of the logged extent.
3437 */
3438 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3439 &ordered->flags))
3440 continue;
3441 atomic_inc(&ordered->refs);
3442 spin_unlock_irq(&log->log_extents_lock[index]);
3443 /*
3444 * we've dropped the lock, we must either break or
3445 * start over after this.
3446 */
3447
3448 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3449
3450 list_for_each_entry(sum, &ordered->list, list) {
3451 ret = btrfs_csum_file_blocks(trans, log, sum);
3452 if (ret) {
3453 btrfs_put_ordered_extent(ordered);
3454 goto unlocked;
3455 }
3456 }
3457 btrfs_put_ordered_extent(ordered);
3458 goto again;
3459
3460 }
3461 spin_unlock_irq(&log->log_extents_lock[index]);
3462unlocked:
3463
3464 if (!mod_len || ret)
3465 return ret;
3466
3467 csum_offset = mod_start - em->start;
3468 csum_len = mod_len;
3469
Josef Bacik70c8a912012-10-11 16:54:30 -04003470 /* block start is already adjusted for the file extent offset. */
3471 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3472 em->block_start + csum_offset,
3473 em->block_start + csum_offset +
3474 csum_len - 1, &ordered_sums, 0);
3475 if (ret)
3476 return ret;
3477
3478 while (!list_empty(&ordered_sums)) {
3479 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3480 struct btrfs_ordered_sum,
3481 list);
3482 if (!ret)
3483 ret = btrfs_csum_file_blocks(trans, log, sums);
3484 list_del(&sums->list);
3485 kfree(sums);
3486 }
3487
3488 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003489}
3490
3491static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3492 struct btrfs_root *root,
3493 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003494 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003495{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003496 struct extent_map *em, *n;
3497 struct list_head extents;
3498 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3499 u64 test_gen;
3500 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003501 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003502
3503 INIT_LIST_HEAD(&extents);
3504
Josef Bacik5dc562c2012-08-17 13:14:17 -04003505 write_lock(&tree->lock);
3506 test_gen = root->fs_info->last_trans_committed;
3507
3508 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3509 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003510
3511 /*
3512 * Just an arbitrary number, this can be really CPU intensive
3513 * once we start getting a lot of extents, and really once we
3514 * have a bunch of extents we just want to commit since it will
3515 * be faster.
3516 */
3517 if (++num > 32768) {
3518 list_del_init(&tree->modified_extents);
3519 ret = -EFBIG;
3520 goto process;
3521 }
3522
Josef Bacik5dc562c2012-08-17 13:14:17 -04003523 if (em->generation <= test_gen)
3524 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003525 /* Need a ref to keep it from getting evicted from cache */
3526 atomic_inc(&em->refs);
3527 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003528 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003529 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003530 }
3531
3532 list_sort(NULL, &extents, extent_cmp);
3533
Josef Bacik2ab28f32012-10-12 15:27:49 -04003534process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003535 while (!list_empty(&extents)) {
3536 em = list_entry(extents.next, struct extent_map, list);
3537
3538 list_del_init(&em->list);
3539
3540 /*
3541 * If we had an error we just need to delete everybody from our
3542 * private list.
3543 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003544 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003545 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003546 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003547 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003548 }
3549
3550 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003551
Josef Bacik70c8a912012-10-11 16:54:30 -04003552 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003553 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003554 clear_em_logging(tree, em);
3555 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003556 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003557 WARN_ON(!list_empty(&extents));
3558 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003559
Josef Bacik5dc562c2012-08-17 13:14:17 -04003560 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003561 return ret;
3562}
3563
Chris Masone02119d2008-09-05 16:13:11 -04003564/* log a single inode in the tree log.
3565 * At least one parent directory for this inode must exist in the tree
3566 * or be logged already.
3567 *
3568 * Any items from this inode changed by the current transaction are copied
3569 * to the log tree. An extra reference is taken on any extents in this
3570 * file, allowing us to avoid a whole pile of corner cases around logging
3571 * blocks that have been removed from the tree.
3572 *
3573 * See LOG_INODE_ALL and related defines for a description of what inode_only
3574 * does.
3575 *
3576 * This handles both files and directories.
3577 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003578static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003579 struct btrfs_root *root, struct inode *inode,
3580 int inode_only)
3581{
3582 struct btrfs_path *path;
3583 struct btrfs_path *dst_path;
3584 struct btrfs_key min_key;
3585 struct btrfs_key max_key;
3586 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003587 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003588 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003589 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003590 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003591 int ins_start_slot = 0;
3592 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003593 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003594 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003595
3596 log = root->log_root;
3597
3598 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003599 if (!path)
3600 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003601 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003602 if (!dst_path) {
3603 btrfs_free_path(path);
3604 return -ENOMEM;
3605 }
Chris Masone02119d2008-09-05 16:13:11 -04003606
Li Zefan33345d012011-04-20 10:31:50 +08003607 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003608 min_key.type = BTRFS_INODE_ITEM_KEY;
3609 min_key.offset = 0;
3610
Li Zefan33345d012011-04-20 10:31:50 +08003611 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003612
Chris Mason12fcfd22009-03-24 10:24:20 -04003613
Josef Bacik5dc562c2012-08-17 13:14:17 -04003614 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003615 if (S_ISDIR(inode->i_mode) ||
3616 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3617 &BTRFS_I(inode)->runtime_flags) &&
3618 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003619 max_key.type = BTRFS_XATTR_ITEM_KEY;
3620 else
3621 max_key.type = (u8)-1;
3622 max_key.offset = (u64)-1;
3623
Josef Bacik94edf4a2012-09-25 14:56:25 -04003624 /* Only run delayed items if we are a dir or a new file */
3625 if (S_ISDIR(inode->i_mode) ||
3626 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3627 ret = btrfs_commit_inode_delayed_items(trans, inode);
3628 if (ret) {
3629 btrfs_free_path(path);
3630 btrfs_free_path(dst_path);
3631 return ret;
3632 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003633 }
3634
Chris Masone02119d2008-09-05 16:13:11 -04003635 mutex_lock(&BTRFS_I(inode)->log_mutex);
3636
Josef Bacik2ab28f32012-10-12 15:27:49 -04003637 btrfs_get_logged_extents(log, inode);
3638
Chris Masone02119d2008-09-05 16:13:11 -04003639 /*
3640 * a brute force approach to making sure we get the most uptodate
3641 * copies of everything.
3642 */
3643 if (S_ISDIR(inode->i_mode)) {
3644 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3645
3646 if (inode_only == LOG_INODE_EXISTS)
3647 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003648 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003649 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003650 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3651 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003652 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3653 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003654 ret = btrfs_truncate_inode_items(trans, log,
3655 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003656 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3657 &BTRFS_I(inode)->runtime_flags)) {
3658 if (inode_only == LOG_INODE_ALL)
3659 fast_search = true;
3660 max_key.type = BTRFS_XATTR_ITEM_KEY;
3661 ret = drop_objectid_items(trans, log, path, ino,
3662 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003663 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003664 if (inode_only == LOG_INODE_ALL)
3665 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003666 ret = log_inode_item(trans, log, dst_path, inode);
3667 if (ret) {
3668 err = ret;
3669 goto out_unlock;
3670 }
3671 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003672 }
Josef Bacika95249b2012-10-11 16:17:34 -04003673
Chris Masone02119d2008-09-05 16:13:11 -04003674 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003675 if (ret) {
3676 err = ret;
3677 goto out_unlock;
3678 }
Chris Masone02119d2008-09-05 16:13:11 -04003679 path->keep_locks = 1;
3680
Chris Masond3977122009-01-05 21:25:51 -05003681 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003682 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003683 ret = btrfs_search_forward(root, &min_key, &max_key,
3684 path, 0, trans->transid);
3685 if (ret != 0)
3686 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003687again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003688 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003689 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003690 break;
3691 if (min_key.type > max_key.type)
3692 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003693
Chris Masone02119d2008-09-05 16:13:11 -04003694 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003695 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3696 ins_nr++;
3697 goto next_slot;
3698 } else if (!ins_nr) {
3699 ins_start_slot = path->slots[0];
3700 ins_nr = 1;
3701 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003702 }
3703
Liu Bod2794402012-08-29 01:07:56 -06003704 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003705 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003706 if (ret) {
3707 err = ret;
3708 goto out_unlock;
3709 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003710 ins_nr = 1;
3711 ins_start_slot = path->slots[0];
3712next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003713
Chris Mason3a5f1d42008-09-11 15:53:37 -04003714 nritems = btrfs_header_nritems(path->nodes[0]);
3715 path->slots[0]++;
3716 if (path->slots[0] < nritems) {
3717 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3718 path->slots[0]);
3719 goto again;
3720 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003721 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003722 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003723 ins_start_slot,
3724 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003725 if (ret) {
3726 err = ret;
3727 goto out_unlock;
3728 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003729 ins_nr = 0;
3730 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003731 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003732
Chris Masone02119d2008-09-05 16:13:11 -04003733 if (min_key.offset < (u64)-1)
3734 min_key.offset++;
3735 else if (min_key.type < (u8)-1)
3736 min_key.type++;
3737 else if (min_key.objectid < (u64)-1)
3738 min_key.objectid++;
3739 else
3740 break;
3741 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003742 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003743 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003744 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003745 if (ret) {
3746 err = ret;
3747 goto out_unlock;
3748 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003749 ins_nr = 0;
3750 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003751
Josef Bacika95249b2012-10-11 16:17:34 -04003752log_extents:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003753 if (fast_search) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003754 btrfs_release_path(dst_path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003755 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003756 if (ret) {
3757 err = ret;
3758 goto out_unlock;
3759 }
Liu Bo06d3d222012-08-27 10:52:19 -06003760 } else {
3761 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3762 struct extent_map *em, *n;
3763
Miao Xiebbe14262012-11-01 07:34:54 +00003764 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003765 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3766 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003767 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003768 }
3769
Chris Mason9623f9a2008-09-11 17:42:42 -04003770 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003771 btrfs_release_path(path);
3772 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003773 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003774 if (ret) {
3775 err = ret;
3776 goto out_unlock;
3777 }
Chris Masone02119d2008-09-05 16:13:11 -04003778 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003779 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003780 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003781out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003782 if (err)
3783 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003784 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3785
3786 btrfs_free_path(path);
3787 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003788 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003789}
3790
Chris Mason12fcfd22009-03-24 10:24:20 -04003791/*
3792 * follow the dentry parent pointers up the chain and see if any
3793 * of the directories in it require a full commit before they can
3794 * be logged. Returns zero if nothing special needs to be done or 1 if
3795 * a full commit is required.
3796 */
3797static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3798 struct inode *inode,
3799 struct dentry *parent,
3800 struct super_block *sb,
3801 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003802{
Chris Mason12fcfd22009-03-24 10:24:20 -04003803 int ret = 0;
3804 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003805 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003806
Chris Masonaf4176b2009-03-24 10:24:31 -04003807 /*
3808 * for regular files, if its inode is already on disk, we don't
3809 * have to worry about the parents at all. This is because
3810 * we can use the last_unlink_trans field to record renames
3811 * and other fun in this file.
3812 */
3813 if (S_ISREG(inode->i_mode) &&
3814 BTRFS_I(inode)->generation <= last_committed &&
3815 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3816 goto out;
3817
Chris Mason12fcfd22009-03-24 10:24:20 -04003818 if (!S_ISDIR(inode->i_mode)) {
3819 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3820 goto out;
3821 inode = parent->d_inode;
3822 }
3823
3824 while (1) {
3825 BTRFS_I(inode)->logged_trans = trans->transid;
3826 smp_mb();
3827
3828 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3829 root = BTRFS_I(inode)->root;
3830
3831 /*
3832 * make sure any commits to the log are forced
3833 * to be full commits
3834 */
3835 root->fs_info->last_trans_log_full_commit =
3836 trans->transid;
3837 ret = 1;
3838 break;
3839 }
3840
3841 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3842 break;
3843
Yan, Zheng76dda932009-09-21 16:00:26 -04003844 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003845 break;
3846
Josef Bacik6a912212010-11-20 09:48:00 +00003847 parent = dget_parent(parent);
3848 dput(old_parent);
3849 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003850 inode = parent->d_inode;
3851
3852 }
Josef Bacik6a912212010-11-20 09:48:00 +00003853 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003854out:
Chris Masone02119d2008-09-05 16:13:11 -04003855 return ret;
3856}
3857
3858/*
3859 * helper function around btrfs_log_inode to make sure newly created
3860 * parent directories also end up in the log. A minimal inode and backref
3861 * only logging is done of any parent directories that are older than
3862 * the last committed transaction
3863 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003864int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3865 struct btrfs_root *root, struct inode *inode,
3866 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003867{
Chris Mason12fcfd22009-03-24 10:24:20 -04003868 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003869 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003870 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003871 int ret = 0;
3872 u64 last_committed = root->fs_info->last_trans_committed;
3873
3874 sb = inode->i_sb;
3875
Sage Weil3a5e1402009-04-02 16:49:40 -04003876 if (btrfs_test_opt(root, NOTREELOG)) {
3877 ret = 1;
3878 goto end_no_trans;
3879 }
3880
Chris Mason12fcfd22009-03-24 10:24:20 -04003881 if (root->fs_info->last_trans_log_full_commit >
3882 root->fs_info->last_trans_committed) {
3883 ret = 1;
3884 goto end_no_trans;
3885 }
3886
Yan, Zheng76dda932009-09-21 16:00:26 -04003887 if (root != BTRFS_I(inode)->root ||
3888 btrfs_root_refs(&root->root_item) == 0) {
3889 ret = 1;
3890 goto end_no_trans;
3891 }
3892
Chris Mason12fcfd22009-03-24 10:24:20 -04003893 ret = check_parent_dirs_for_sync(trans, inode, parent,
3894 sb, last_committed);
3895 if (ret)
3896 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003897
Josef Bacik22ee6982012-05-29 16:57:49 -04003898 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003899 ret = BTRFS_NO_LOG_SYNC;
3900 goto end_no_trans;
3901 }
3902
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003903 ret = start_log_trans(trans, root);
3904 if (ret)
3905 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003906
3907 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003908 if (ret)
3909 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003910
Chris Masonaf4176b2009-03-24 10:24:31 -04003911 /*
3912 * for regular files, if its inode is already on disk, we don't
3913 * have to worry about the parents at all. This is because
3914 * we can use the last_unlink_trans field to record renames
3915 * and other fun in this file.
3916 */
3917 if (S_ISREG(inode->i_mode) &&
3918 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003919 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3920 ret = 0;
3921 goto end_trans;
3922 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003923
3924 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003925 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003926 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003927 break;
3928
Chris Mason12fcfd22009-03-24 10:24:20 -04003929 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003930 if (root != BTRFS_I(inode)->root)
3931 break;
3932
Chris Mason12fcfd22009-03-24 10:24:20 -04003933 if (BTRFS_I(inode)->generation >
3934 root->fs_info->last_trans_committed) {
3935 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003936 if (ret)
3937 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003938 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003939 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003940 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003941
Josef Bacik6a912212010-11-20 09:48:00 +00003942 parent = dget_parent(parent);
3943 dput(old_parent);
3944 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003945 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003946 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003947end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003948 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003949 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003950 root->fs_info->last_trans_log_full_commit = trans->transid;
3951 ret = 1;
3952 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003953 btrfs_end_log_trans(root);
3954end_no_trans:
3955 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003956}
3957
3958/*
3959 * it is not safe to log dentry if the chunk root has added new
3960 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3961 * If this returns 1, you must commit the transaction to safely get your
3962 * data on disk.
3963 */
3964int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3965 struct btrfs_root *root, struct dentry *dentry)
3966{
Josef Bacik6a912212010-11-20 09:48:00 +00003967 struct dentry *parent = dget_parent(dentry);
3968 int ret;
3969
3970 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3971 dput(parent);
3972
3973 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003974}
3975
3976/*
3977 * should be called during mount to recover any replay any log trees
3978 * from the FS
3979 */
3980int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3981{
3982 int ret;
3983 struct btrfs_path *path;
3984 struct btrfs_trans_handle *trans;
3985 struct btrfs_key key;
3986 struct btrfs_key found_key;
3987 struct btrfs_key tmp_key;
3988 struct btrfs_root *log;
3989 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3990 struct walk_control wc = {
3991 .process_func = process_one_buffer,
3992 .stage = 0,
3993 };
3994
Chris Masone02119d2008-09-05 16:13:11 -04003995 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003996 if (!path)
3997 return -ENOMEM;
3998
3999 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004000
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004001 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004002 if (IS_ERR(trans)) {
4003 ret = PTR_ERR(trans);
4004 goto error;
4005 }
Chris Masone02119d2008-09-05 16:13:11 -04004006
4007 wc.trans = trans;
4008 wc.pin = 1;
4009
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004010 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004011 if (ret) {
4012 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4013 "recovering log root tree.");
4014 goto error;
4015 }
Chris Masone02119d2008-09-05 16:13:11 -04004016
4017again:
4018 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4019 key.offset = (u64)-1;
4020 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4021
Chris Masond3977122009-01-05 21:25:51 -05004022 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004023 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004024
4025 if (ret < 0) {
4026 btrfs_error(fs_info, ret,
4027 "Couldn't find tree log root.");
4028 goto error;
4029 }
Chris Masone02119d2008-09-05 16:13:11 -04004030 if (ret > 0) {
4031 if (path->slots[0] == 0)
4032 break;
4033 path->slots[0]--;
4034 }
4035 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4036 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004037 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004038 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4039 break;
4040
4041 log = btrfs_read_fs_root_no_radix(log_root_tree,
4042 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004043 if (IS_ERR(log)) {
4044 ret = PTR_ERR(log);
4045 btrfs_error(fs_info, ret,
4046 "Couldn't read tree log root.");
4047 goto error;
4048 }
Chris Masone02119d2008-09-05 16:13:11 -04004049
4050 tmp_key.objectid = found_key.offset;
4051 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4052 tmp_key.offset = (u64)-1;
4053
4054 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 if (IS_ERR(wc.replay_dest)) {
4056 ret = PTR_ERR(wc.replay_dest);
4057 btrfs_error(fs_info, ret, "Couldn't read target root "
4058 "for tree log recovery.");
4059 goto error;
4060 }
Chris Masone02119d2008-09-05 16:13:11 -04004061
Yan Zheng07d400a2009-01-06 11:42:00 -05004062 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004064 ret = walk_log_tree(trans, log, &wc);
4065 BUG_ON(ret);
4066
4067 if (wc.stage == LOG_WALK_REPLAY_ALL) {
4068 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4069 path);
4070 BUG_ON(ret);
4071 }
Chris Masone02119d2008-09-05 16:13:11 -04004072
4073 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004074 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004075 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004076 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004077 kfree(log);
4078
4079 if (found_key.offset == 0)
4080 break;
4081 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004082 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004083
4084 /* step one is to pin it all, step two is to replay just inodes */
4085 if (wc.pin) {
4086 wc.pin = 0;
4087 wc.process_func = replay_one_buffer;
4088 wc.stage = LOG_WALK_REPLAY_INODES;
4089 goto again;
4090 }
4091 /* step three is to replay everything */
4092 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4093 wc.stage++;
4094 goto again;
4095 }
4096
4097 btrfs_free_path(path);
4098
4099 free_extent_buffer(log_root_tree->node);
4100 log_root_tree->log_root = NULL;
4101 fs_info->log_root_recovering = 0;
4102
4103 /* step 4: commit the transaction, which also unpins the blocks */
4104 btrfs_commit_transaction(trans, fs_info->tree_root);
4105
4106 kfree(log_root_tree);
4107 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108
4109error:
4110 btrfs_free_path(path);
4111 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004112}
Chris Mason12fcfd22009-03-24 10:24:20 -04004113
4114/*
4115 * there are some corner cases where we want to force a full
4116 * commit instead of allowing a directory to be logged.
4117 *
4118 * They revolve around files there were unlinked from the directory, and
4119 * this function updates the parent directory so that a full commit is
4120 * properly done if it is fsync'd later after the unlinks are done.
4121 */
4122void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4123 struct inode *dir, struct inode *inode,
4124 int for_rename)
4125{
4126 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004127 * when we're logging a file, if it hasn't been renamed
4128 * or unlinked, and its inode is fully committed on disk,
4129 * we don't have to worry about walking up the directory chain
4130 * to log its parents.
4131 *
4132 * So, we use the last_unlink_trans field to put this transid
4133 * into the file. When the file is logged we check it and
4134 * don't log the parents if the file is fully on disk.
4135 */
4136 if (S_ISREG(inode->i_mode))
4137 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4138
4139 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004140 * if this directory was already logged any new
4141 * names for this file/dir will get recorded
4142 */
4143 smp_mb();
4144 if (BTRFS_I(dir)->logged_trans == trans->transid)
4145 return;
4146
4147 /*
4148 * if the inode we're about to unlink was logged,
4149 * the log will be properly updated for any new names
4150 */
4151 if (BTRFS_I(inode)->logged_trans == trans->transid)
4152 return;
4153
4154 /*
4155 * when renaming files across directories, if the directory
4156 * there we're unlinking from gets fsync'd later on, there's
4157 * no way to find the destination directory later and fsync it
4158 * properly. So, we have to be conservative and force commits
4159 * so the new name gets discovered.
4160 */
4161 if (for_rename)
4162 goto record;
4163
4164 /* we can safely do the unlink without any special recording */
4165 return;
4166
4167record:
4168 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4169}
4170
4171/*
4172 * Call this after adding a new name for a file and it will properly
4173 * update the log to reflect the new name.
4174 *
4175 * It will return zero if all goes well, and it will return 1 if a
4176 * full transaction commit is required.
4177 */
4178int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4179 struct inode *inode, struct inode *old_dir,
4180 struct dentry *parent)
4181{
4182 struct btrfs_root * root = BTRFS_I(inode)->root;
4183
4184 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004185 * this will force the logging code to walk the dentry chain
4186 * up for the file
4187 */
4188 if (S_ISREG(inode->i_mode))
4189 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4190
4191 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004192 * if this inode hasn't been logged and directory we're renaming it
4193 * from hasn't been logged, we don't need to log it
4194 */
4195 if (BTRFS_I(inode)->logged_trans <=
4196 root->fs_info->last_trans_committed &&
4197 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4198 root->fs_info->last_trans_committed))
4199 return 0;
4200
4201 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4202}
4203