blob: f50137a98fb1ac524f9c4bbdbd16011e99515f5e [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 Zheng7237f1832009-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 Zheng7237f1832009-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 Zheng7237f1832009-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 Zheng7237f1832009-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 Zheng7237f1832009-01-21 12:54:03 -0500193 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400194 }
Yan Zheng7237f1832009-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 Zheng7237f1832009-01-21 12:54:03 -0500220 if (atomic_dec_and_test(&root->log_writers)) {
221 smp_mb();
222 if (waitqueue_active(&root->log_writer_wait))
223 wake_up(&root->log_writer_wait);
224 }
Chris Masone02119d2008-09-05 16:13:11 -0400225}
226
227
228/*
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
233 */
234struct walk_control {
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
237 */
238 int free;
239
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
242 */
243 int write;
244
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
247 */
248 int wait;
249
250 /* pin only walk, we record which extents on disk belong to the
251 * log trees
252 */
253 int pin;
254
255 /* what stage of the replay code we're currently in */
256 int stage;
257
258 /* the root we are currently replaying */
259 struct btrfs_root *replay_dest;
260
261 /* the trans handle for the current replay */
262 struct btrfs_trans_handle *trans;
263
264 /* the function that gets used to process blocks we find in the
265 * tree. Note the extent_buffer might not be up to date when it is
266 * passed in, and it must be checked or read if you need the data
267 * inside it
268 */
269 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
270 struct walk_control *wc, u64 gen);
271};
272
273/*
274 * process_func used to pin down extents, write them or wait on them
275 */
276static int process_one_buffer(struct btrfs_root *log,
277 struct extent_buffer *eb,
278 struct walk_control *wc, u64 gen)
279{
Josef Bacik04018de2009-04-03 10:14:18 -0400280 if (wc->pin)
Liu Bodcfac412012-12-27 09:01:20 +0000281 btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
Chris Masone688b7252011-10-31 20:52:39 -0400282 eb->start, eb->len);
Chris Masone02119d2008-09-05 16:13:11 -0400283
Chris Masonb9fab912012-05-06 07:23:47 -0400284 if (btrfs_buffer_uptodate(eb, gen, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -0400285 if (wc->write)
286 btrfs_write_tree_block(eb);
287 if (wc->wait)
288 btrfs_wait_tree_block_writeback(eb);
289 }
290 return 0;
291}
292
293/*
294 * Item overwrite used by replay and tree logging. eb, slot and key all refer
295 * to the src data we are copying out.
296 *
297 * root is the tree we are copying into, and path is a scratch
298 * path for use in this function (it should be released on entry and
299 * will be released on exit).
300 *
301 * If the key is already in the destination tree the existing item is
302 * overwritten. If the existing item isn't big enough, it is extended.
303 * If it is too large, it is truncated.
304 *
305 * If the key isn't in the destination yet, a new item is inserted.
306 */
307static noinline int overwrite_item(struct btrfs_trans_handle *trans,
308 struct btrfs_root *root,
309 struct btrfs_path *path,
310 struct extent_buffer *eb, int slot,
311 struct btrfs_key *key)
312{
313 int ret;
314 u32 item_size;
315 u64 saved_i_size = 0;
316 int save_old_i_size = 0;
317 unsigned long src_ptr;
318 unsigned long dst_ptr;
319 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000320 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400321
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);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000330 if (ret < 0)
331 return ret;
332
Chris Masone02119d2008-09-05 16:13:11 -0400333 if (ret == 0) {
334 char *src_copy;
335 char *dst_copy;
336 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
337 path->slots[0]);
338 if (dst_size != item_size)
339 goto insert;
340
341 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200342 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400343 return 0;
344 }
345 dst_copy = kmalloc(item_size, GFP_NOFS);
346 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000347 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200348 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000349 kfree(dst_copy);
350 kfree(src_copy);
351 return -ENOMEM;
352 }
Chris Masone02119d2008-09-05 16:13:11 -0400353
354 read_extent_buffer(eb, src_copy, src_ptr, item_size);
355
356 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
357 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
358 item_size);
359 ret = memcmp(dst_copy, src_copy, item_size);
360
361 kfree(dst_copy);
362 kfree(src_copy);
363 /*
364 * they have the same contents, just return, this saves
365 * us from cowing blocks in the destination tree and doing
366 * extra writes that may not have been done by a previous
367 * sync
368 */
369 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200370 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400371 return 0;
372 }
373
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000374 /*
375 * We need to load the old nbytes into the inode so when we
376 * replay the extents we've logged we get the right nbytes.
377 */
378 if (inode_item) {
379 struct btrfs_inode_item *item;
380 u64 nbytes;
381
382 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
383 struct btrfs_inode_item);
384 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
385 item = btrfs_item_ptr(eb, slot,
386 struct btrfs_inode_item);
387 btrfs_set_inode_nbytes(eb, item, nbytes);
388 }
389 } else if (inode_item) {
390 struct btrfs_inode_item *item;
391
392 /*
393 * New inode, set nbytes to 0 so that the nbytes comes out
394 * properly when we replay the extents.
395 */
396 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
397 btrfs_set_inode_nbytes(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400398 }
399insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200400 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400401 /* try to insert the key into the destination tree */
402 ret = btrfs_insert_empty_item(trans, root, path,
403 key, item_size);
404
405 /* make sure any existing item is the correct size */
406 if (ret == -EEXIST) {
407 u32 found_size;
408 found_size = btrfs_item_size_nr(path->nodes[0],
409 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100410 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000411 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100412 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000413 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100414 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400415 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400416 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400417 }
418 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
419 path->slots[0]);
420
421 /* don't overwrite an existing inode if the generation number
422 * was logged as zero. This is done when the tree logging code
423 * is just logging an inode to make sure it exists after recovery.
424 *
425 * Also, don't overwrite i_size on directories during replay.
426 * log replay inserts and removes directory items based on the
427 * state of the tree found in the subvolume, and i_size is modified
428 * as it goes
429 */
430 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
431 struct btrfs_inode_item *src_item;
432 struct btrfs_inode_item *dst_item;
433
434 src_item = (struct btrfs_inode_item *)src_ptr;
435 dst_item = (struct btrfs_inode_item *)dst_ptr;
436
437 if (btrfs_inode_generation(eb, src_item) == 0)
438 goto no_copy;
439
440 if (overwrite_root &&
441 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
442 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
443 save_old_i_size = 1;
444 saved_i_size = btrfs_inode_size(path->nodes[0],
445 dst_item);
446 }
447 }
448
449 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
450 src_ptr, item_size);
451
452 if (save_old_i_size) {
453 struct btrfs_inode_item *dst_item;
454 dst_item = (struct btrfs_inode_item *)dst_ptr;
455 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
456 }
457
458 /* make sure the generation is filled in */
459 if (key->type == BTRFS_INODE_ITEM_KEY) {
460 struct btrfs_inode_item *dst_item;
461 dst_item = (struct btrfs_inode_item *)dst_ptr;
462 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
463 btrfs_set_inode_generation(path->nodes[0], dst_item,
464 trans->transid);
465 }
466 }
467no_copy:
468 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200469 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400470 return 0;
471}
472
473/*
474 * simple helper to read an inode off the disk from a given root
475 * This can only be called for subvolume roots and not for the log
476 */
477static noinline struct inode *read_one_inode(struct btrfs_root *root,
478 u64 objectid)
479{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400480 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400481 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400482
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483 key.objectid = objectid;
484 key.type = BTRFS_INODE_ITEM_KEY;
485 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000486 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400487 if (IS_ERR(inode)) {
488 inode = NULL;
489 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400490 iput(inode);
491 inode = NULL;
492 }
493 return inode;
494}
495
496/* replays a single extent in 'eb' at 'slot' with 'key' into the
497 * subvolume 'root'. path is released on entry and should be released
498 * on exit.
499 *
500 * extents in the log tree have not been allocated out of the extent
501 * tree yet. So, this completes the allocation, taking a reference
502 * as required if the extent already exists or creating a new extent
503 * if it isn't in the extent allocation tree yet.
504 *
505 * The extent is inserted into the file, dropping any existing extents
506 * from the file that overlap the new one.
507 */
508static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
509 struct btrfs_root *root,
510 struct btrfs_path *path,
511 struct extent_buffer *eb, int slot,
512 struct btrfs_key *key)
513{
514 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400515 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400516 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000517 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400518 struct btrfs_file_extent_item *item;
519 struct inode *inode = NULL;
520 unsigned long size;
521 int ret = 0;
522
523 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
524 found_type = btrfs_file_extent_type(eb, item);
525
Yan Zhengd899e052008-10-30 14:25:28 -0400526 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000527 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
528 nbytes = btrfs_file_extent_num_bytes(eb, item);
529 extent_end = start + nbytes;
530
531 /*
532 * We don't add to the inodes nbytes if we are prealloc or a
533 * hole.
534 */
535 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
536 nbytes = 0;
537 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400538 size = btrfs_file_extent_inline_len(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000539 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000540 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400541 } else {
542 ret = 0;
543 goto out;
544 }
545
546 inode = read_one_inode(root, key->objectid);
547 if (!inode) {
548 ret = -EIO;
549 goto out;
550 }
551
552 /*
553 * first check to see if we already have this extent in the
554 * file. This must be done before the btrfs_drop_extents run
555 * so we don't try to drop this extent.
556 */
Li Zefan33345d012011-04-20 10:31:50 +0800557 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400558 start, 0);
559
Yan Zhengd899e052008-10-30 14:25:28 -0400560 if (ret == 0 &&
561 (found_type == BTRFS_FILE_EXTENT_REG ||
562 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400563 struct btrfs_file_extent_item cmp1;
564 struct btrfs_file_extent_item cmp2;
565 struct btrfs_file_extent_item *existing;
566 struct extent_buffer *leaf;
567
568 leaf = path->nodes[0];
569 existing = btrfs_item_ptr(leaf, path->slots[0],
570 struct btrfs_file_extent_item);
571
572 read_extent_buffer(eb, &cmp1, (unsigned long)item,
573 sizeof(cmp1));
574 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
575 sizeof(cmp2));
576
577 /*
578 * we already have a pointer to this exact extent,
579 * we don't have to do anything
580 */
581 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200582 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400583 goto out;
584 }
585 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200586 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400587
588 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400589 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400590 BUG_ON(ret);
591
Yan Zheng07d400a2009-01-06 11:42:00 -0500592 if (found_type == BTRFS_FILE_EXTENT_REG ||
593 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400594 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500595 unsigned long dest_offset;
596 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400597
Yan Zheng07d400a2009-01-06 11:42:00 -0500598 ret = btrfs_insert_empty_item(trans, root, path, key,
599 sizeof(*item));
600 BUG_ON(ret);
601 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
602 path->slots[0]);
603 copy_extent_buffer(path->nodes[0], eb, dest_offset,
604 (unsigned long)item, sizeof(*item));
605
606 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
607 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
608 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400609 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500610
611 if (ins.objectid > 0) {
612 u64 csum_start;
613 u64 csum_end;
614 LIST_HEAD(ordered_sums);
615 /*
616 * is this extent already allocated in the extent
617 * allocation tree? If so, just add a reference
618 */
619 ret = btrfs_lookup_extent(root, ins.objectid,
620 ins.offset);
621 if (ret == 0) {
622 ret = btrfs_inc_extent_ref(trans, root,
623 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200625 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000626 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500627 } else {
628 /*
629 * insert the extent pointer in the extent
630 * allocation tree
631 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 ret = btrfs_alloc_logged_file_extent(trans,
633 root, root->root_key.objectid,
634 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500635 BUG_ON(ret);
636 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200637 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500638
639 if (btrfs_file_extent_compression(eb, item)) {
640 csum_start = ins.objectid;
641 csum_end = csum_start + ins.offset;
642 } else {
643 csum_start = ins.objectid +
644 btrfs_file_extent_offset(eb, item);
645 csum_end = csum_start +
646 btrfs_file_extent_num_bytes(eb, item);
647 }
648
649 ret = btrfs_lookup_csums_range(root->log_root,
650 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100651 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500652 BUG_ON(ret);
653 while (!list_empty(&ordered_sums)) {
654 struct btrfs_ordered_sum *sums;
655 sums = list_entry(ordered_sums.next,
656 struct btrfs_ordered_sum,
657 list);
658 ret = btrfs_csum_file_blocks(trans,
659 root->fs_info->csum_root,
660 sums);
661 BUG_ON(ret);
662 list_del(&sums->list);
663 kfree(sums);
664 }
665 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200666 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500667 }
668 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
669 /* inline extents are easy, we just overwrite them */
670 ret = overwrite_item(trans, root, path, eb, slot, key);
671 BUG_ON(ret);
672 }
673
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000674 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600675 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400676out:
677 if (inode)
678 iput(inode);
679 return ret;
680}
681
682/*
683 * when cleaning up conflicts between the directory names in the
684 * subvolume, directory names in the log and directory names in the
685 * inode back references, we may have to unlink inodes from directories.
686 *
687 * This is a helper function to do the unlink of a specific directory
688 * item
689 */
690static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
691 struct btrfs_root *root,
692 struct btrfs_path *path,
693 struct inode *dir,
694 struct btrfs_dir_item *di)
695{
696 struct inode *inode;
697 char *name;
698 int name_len;
699 struct extent_buffer *leaf;
700 struct btrfs_key location;
701 int ret;
702
703 leaf = path->nodes[0];
704
705 btrfs_dir_item_key_to_cpu(leaf, di, &location);
706 name_len = btrfs_dir_name_len(leaf, di);
707 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000708 if (!name)
709 return -ENOMEM;
710
Chris Masone02119d2008-09-05 16:13:11 -0400711 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200712 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400713
714 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000715 if (!inode) {
716 kfree(name);
717 return -EIO;
718 }
Chris Masone02119d2008-09-05 16:13:11 -0400719
Yan Zhengec051c02009-01-05 15:43:42 -0500720 ret = link_to_fixup_dir(trans, root, path, location.objectid);
721 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400722
Chris Masone02119d2008-09-05 16:13:11 -0400723 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500724 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400725 kfree(name);
726
727 iput(inode);
Chris Masonb6305562012-07-02 15:29:53 -0400728
729 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400730 return ret;
731}
732
733/*
734 * helper function to see if a given name and sequence number found
735 * in an inode back reference are already in a directory and correctly
736 * point to this inode
737 */
738static noinline int inode_in_dir(struct btrfs_root *root,
739 struct btrfs_path *path,
740 u64 dirid, u64 objectid, u64 index,
741 const char *name, int name_len)
742{
743 struct btrfs_dir_item *di;
744 struct btrfs_key location;
745 int match = 0;
746
747 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
748 index, name, name_len, 0);
749 if (di && !IS_ERR(di)) {
750 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
751 if (location.objectid != objectid)
752 goto out;
753 } else
754 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200755 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400756
757 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
758 if (di && !IS_ERR(di)) {
759 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
760 if (location.objectid != objectid)
761 goto out;
762 } else
763 goto out;
764 match = 1;
765out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200766 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400767 return match;
768}
769
770/*
771 * helper function to check a log tree for a named back reference in
772 * an inode. This is used to decide if a back reference that is
773 * found in the subvolume conflicts with what we find in the log.
774 *
775 * inode backreferences may have multiple refs in a single item,
776 * during replay we process one reference at a time, and we don't
777 * want to delete valid links to a file from the subvolume if that
778 * link is also in the log.
779 */
780static noinline int backref_in_log(struct btrfs_root *log,
781 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700782 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400783 char *name, int namelen)
784{
785 struct btrfs_path *path;
786 struct btrfs_inode_ref *ref;
787 unsigned long ptr;
788 unsigned long ptr_end;
789 unsigned long name_ptr;
790 int found_name_len;
791 int item_size;
792 int ret;
793 int match = 0;
794
795 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000796 if (!path)
797 return -ENOMEM;
798
Chris Masone02119d2008-09-05 16:13:11 -0400799 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
800 if (ret != 0)
801 goto out;
802
Chris Masone02119d2008-09-05 16:13:11 -0400803 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700804
805 if (key->type == BTRFS_INODE_EXTREF_KEY) {
806 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
807 name, namelen, NULL))
808 match = 1;
809
810 goto out;
811 }
812
813 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400814 ptr_end = ptr + item_size;
815 while (ptr < ptr_end) {
816 ref = (struct btrfs_inode_ref *)ptr;
817 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
818 if (found_name_len == namelen) {
819 name_ptr = (unsigned long)(ref + 1);
820 ret = memcmp_extent_buffer(path->nodes[0], name,
821 name_ptr, namelen);
822 if (ret == 0) {
823 match = 1;
824 goto out;
825 }
826 }
827 ptr = (unsigned long)(ref + 1) + found_name_len;
828 }
829out:
830 btrfs_free_path(path);
831 return match;
832}
833
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700834static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct btrfs_path *path,
837 struct btrfs_root *log_root,
838 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700839 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700840 u64 inode_objectid, u64 parent_objectid,
841 u64 ref_index, char *name, int namelen,
842 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700843{
844 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700845 char *victim_name;
846 int victim_name_len;
847 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700848 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700849 struct btrfs_key search_key;
850 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700851
Mark Fashehf1863732012-08-08 11:32:27 -0700852again:
853 /* Search old style refs */
854 search_key.objectid = inode_objectid;
855 search_key.type = BTRFS_INODE_REF_KEY;
856 search_key.offset = parent_objectid;
857 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700858 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700859 struct btrfs_inode_ref *victim_ref;
860 unsigned long ptr;
861 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700862
863 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700864
865 /* are we trying to overwrite a back ref for the root directory
866 * if so, just jump out, we're done
867 */
Mark Fashehf1863732012-08-08 11:32:27 -0700868 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700869 return 1;
870
871 /* check all the names in this back reference to see
872 * if they are in the log. if so, we allow them to stay
873 * otherwise they must be unlinked as a conflict
874 */
875 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
876 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
877 while (ptr < ptr_end) {
878 victim_ref = (struct btrfs_inode_ref *)ptr;
879 victim_name_len = btrfs_inode_ref_name_len(leaf,
880 victim_ref);
881 victim_name = kmalloc(victim_name_len, GFP_NOFS);
882 BUG_ON(!victim_name);
883
884 read_extent_buffer(leaf, victim_name,
885 (unsigned long)(victim_ref + 1),
886 victim_name_len);
887
Mark Fashehf1863732012-08-08 11:32:27 -0700888 if (!backref_in_log(log_root, &search_key,
889 parent_objectid,
890 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700891 victim_name_len)) {
892 btrfs_inc_nlink(inode);
893 btrfs_release_path(path);
894
895 ret = btrfs_unlink_inode(trans, root, dir,
896 inode, victim_name,
897 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700898 BUG_ON(ret);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700899 btrfs_run_delayed_items(trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -0700900 kfree(victim_name);
901 *search_done = 1;
902 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700903 }
904 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700905
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700906 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
907 }
908 BUG_ON(ret);
909
910 /*
911 * NOTE: we have searched root tree and checked the
912 * coresponding ref, it does not need to check again.
913 */
914 *search_done = 1;
915 }
916 btrfs_release_path(path);
917
Mark Fashehf1863732012-08-08 11:32:27 -0700918 /* Same search but for extended refs */
919 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
920 inode_objectid, parent_objectid, 0,
921 0);
922 if (!IS_ERR_OR_NULL(extref)) {
923 u32 item_size;
924 u32 cur_offset = 0;
925 unsigned long base;
926 struct inode *victim_parent;
927
928 leaf = path->nodes[0];
929
930 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
931 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
932
933 while (cur_offset < item_size) {
934 extref = (struct btrfs_inode_extref *)base + cur_offset;
935
936 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
937
938 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
939 goto next;
940
941 victim_name = kmalloc(victim_name_len, GFP_NOFS);
942 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
943 victim_name_len);
944
945 search_key.objectid = inode_objectid;
946 search_key.type = BTRFS_INODE_EXTREF_KEY;
947 search_key.offset = btrfs_extref_hash(parent_objectid,
948 victim_name,
949 victim_name_len);
950 ret = 0;
951 if (!backref_in_log(log_root, &search_key,
952 parent_objectid, victim_name,
953 victim_name_len)) {
954 ret = -ENOENT;
955 victim_parent = read_one_inode(root,
956 parent_objectid);
957 if (victim_parent) {
958 btrfs_inc_nlink(inode);
959 btrfs_release_path(path);
960
961 ret = btrfs_unlink_inode(trans, root,
962 victim_parent,
963 inode,
964 victim_name,
965 victim_name_len);
966 btrfs_run_delayed_items(trans, root);
967 }
968 BUG_ON(ret);
969 iput(victim_parent);
970 kfree(victim_name);
971 *search_done = 1;
972 goto again;
973 }
974 kfree(victim_name);
975 BUG_ON(ret);
976next:
977 cur_offset += victim_name_len + sizeof(*extref);
978 }
979 *search_done = 1;
980 }
981 btrfs_release_path(path);
982
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700983 /* look for a conflicting sequence number */
984 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -0700985 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700986 if (di && !IS_ERR(di)) {
987 ret = drop_one_dir_item(trans, root, path, dir, di);
988 BUG_ON(ret);
989 }
990 btrfs_release_path(path);
991
992 /* look for a conflicing name */
993 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
994 name, namelen, 0);
995 if (di && !IS_ERR(di)) {
996 ret = drop_one_dir_item(trans, root, path, dir, di);
997 BUG_ON(ret);
998 }
999 btrfs_release_path(path);
1000
1001 return 0;
1002}
Chris Masone02119d2008-09-05 16:13:11 -04001003
Mark Fashehf1863732012-08-08 11:32:27 -07001004static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1005 u32 *namelen, char **name, u64 *index,
1006 u64 *parent_objectid)
1007{
1008 struct btrfs_inode_extref *extref;
1009
1010 extref = (struct btrfs_inode_extref *)ref_ptr;
1011
1012 *namelen = btrfs_inode_extref_name_len(eb, extref);
1013 *name = kmalloc(*namelen, GFP_NOFS);
1014 if (*name == NULL)
1015 return -ENOMEM;
1016
1017 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1018 *namelen);
1019
1020 *index = btrfs_inode_extref_index(eb, extref);
1021 if (parent_objectid)
1022 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1023
1024 return 0;
1025}
1026
1027static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1028 u32 *namelen, char **name, u64 *index)
1029{
1030 struct btrfs_inode_ref *ref;
1031
1032 ref = (struct btrfs_inode_ref *)ref_ptr;
1033
1034 *namelen = btrfs_inode_ref_name_len(eb, ref);
1035 *name = kmalloc(*namelen, GFP_NOFS);
1036 if (*name == NULL)
1037 return -ENOMEM;
1038
1039 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1040
1041 *index = btrfs_inode_ref_index(eb, ref);
1042
1043 return 0;
1044}
1045
Chris Masone02119d2008-09-05 16:13:11 -04001046/*
1047 * replay one inode back reference item found in the log tree.
1048 * eb, slot and key refer to the buffer and key found in the log tree.
1049 * root is the destination we are replaying into, and path is for temp
1050 * use by this function. (it should be released on return).
1051 */
1052static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1053 struct btrfs_root *root,
1054 struct btrfs_root *log,
1055 struct btrfs_path *path,
1056 struct extent_buffer *eb, int slot,
1057 struct btrfs_key *key)
1058{
liubo34f3e4f2011-08-06 08:35:23 +00001059 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001060 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -04001061 unsigned long ref_ptr;
1062 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +00001063 char *name;
1064 int namelen;
1065 int ret;
liuboc622ae62011-03-26 08:01:12 -04001066 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001067 int log_ref_ver = 0;
1068 u64 parent_objectid;
1069 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001070 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001071 int ref_struct_size;
1072
1073 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1074 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1075
1076 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1077 struct btrfs_inode_extref *r;
1078
1079 ref_struct_size = sizeof(struct btrfs_inode_extref);
1080 log_ref_ver = 1;
1081 r = (struct btrfs_inode_extref *)ref_ptr;
1082 parent_objectid = btrfs_inode_extref_parent(eb, r);
1083 } else {
1084 ref_struct_size = sizeof(struct btrfs_inode_ref);
1085 parent_objectid = key->offset;
1086 }
1087 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001088
Chris Masone02119d2008-09-05 16:13:11 -04001089 /*
1090 * it is possible that we didn't log all the parent directories
1091 * for a given inode. If we don't find the dir, just don't
1092 * copy the back ref in. The link count fixup code will take
1093 * care of the rest
1094 */
Mark Fashehf1863732012-08-08 11:32:27 -07001095 dir = read_one_inode(root, parent_objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001096 if (!dir)
1097 return -ENOENT;
1098
Mark Fashehf1863732012-08-08 11:32:27 -07001099 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001100 if (!inode) {
1101 iput(dir);
1102 return -EIO;
1103 }
Chris Masone02119d2008-09-05 16:13:11 -04001104
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001105 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001106 if (log_ref_ver) {
1107 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1108 &ref_index, &parent_objectid);
1109 /*
1110 * parent object can change from one array
1111 * item to another.
1112 */
1113 if (!dir)
1114 dir = read_one_inode(root, parent_objectid);
1115 if (!dir)
1116 return -ENOENT;
1117 } else {
1118 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1119 &ref_index);
1120 }
1121 if (ret)
1122 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001123
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001124 /* if we already have a perfect match, we're done */
1125 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001126 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001127 /*
1128 * look for a conflicting back reference in the
1129 * metadata. if we find one we have to unlink that name
1130 * of the file before we add our new link. Later on, we
1131 * overwrite any existing back reference, and we don't
1132 * want to create dangling pointers in the directory.
1133 */
Chris Masone02119d2008-09-05 16:13:11 -04001134
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001135 if (!search_done) {
1136 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001137 dir, inode, eb,
1138 inode_objectid,
1139 parent_objectid,
1140 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001141 &search_done);
1142 if (ret == 1)
1143 goto out;
1144 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001145 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001146
1147 /* insert our name */
1148 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001149 0, ref_index);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 BUG_ON(ret);
1151
1152 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001153 }
liuboc622ae62011-03-26 08:01:12 -04001154
Mark Fashehf1863732012-08-08 11:32:27 -07001155 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001156 kfree(name);
Mark Fashehf1863732012-08-08 11:32:27 -07001157 if (log_ref_ver) {
1158 iput(dir);
1159 dir = NULL;
1160 }
Chris Masone02119d2008-09-05 16:13:11 -04001161 }
Chris Masone02119d2008-09-05 16:13:11 -04001162
1163 /* finally write the back reference in the inode */
1164 ret = overwrite_item(trans, root, path, eb, slot, key);
1165 BUG_ON(ret);
1166
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001167out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001168 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001169 iput(dir);
1170 iput(inode);
1171 return 0;
1172}
1173
Yan, Zhengc71bf092009-11-12 09:34:40 +00001174static int insert_orphan_item(struct btrfs_trans_handle *trans,
1175 struct btrfs_root *root, u64 offset)
1176{
1177 int ret;
1178 ret = btrfs_find_orphan_item(root, offset);
1179 if (ret > 0)
1180 ret = btrfs_insert_orphan_item(trans, root, offset);
1181 return ret;
1182}
1183
Mark Fashehf1863732012-08-08 11:32:27 -07001184static int count_inode_extrefs(struct btrfs_root *root,
1185 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001186{
Mark Fashehf1863732012-08-08 11:32:27 -07001187 int ret = 0;
1188 int name_len;
1189 unsigned int nlink = 0;
1190 u32 item_size;
1191 u32 cur_offset = 0;
1192 u64 inode_objectid = btrfs_ino(inode);
1193 u64 offset = 0;
1194 unsigned long ptr;
1195 struct btrfs_inode_extref *extref;
1196 struct extent_buffer *leaf;
1197
1198 while (1) {
1199 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1200 &extref, &offset);
1201 if (ret)
1202 break;
1203
1204 leaf = path->nodes[0];
1205 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1206 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1207
1208 while (cur_offset < item_size) {
1209 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1210 name_len = btrfs_inode_extref_name_len(leaf, extref);
1211
1212 nlink++;
1213
1214 cur_offset += name_len + sizeof(*extref);
1215 }
1216
1217 offset++;
1218 btrfs_release_path(path);
1219 }
1220 btrfs_release_path(path);
1221
1222 if (ret < 0)
1223 return ret;
1224 return nlink;
1225}
1226
1227static int count_inode_refs(struct btrfs_root *root,
1228 struct inode *inode, struct btrfs_path *path)
1229{
Chris Masone02119d2008-09-05 16:13:11 -04001230 int ret;
1231 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001232 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001233 unsigned long ptr;
1234 unsigned long ptr_end;
1235 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001236 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001237
Li Zefan33345d012011-04-20 10:31:50 +08001238 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001239 key.type = BTRFS_INODE_REF_KEY;
1240 key.offset = (u64)-1;
1241
Chris Masond3977122009-01-05 21:25:51 -05001242 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001243 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1244 if (ret < 0)
1245 break;
1246 if (ret > 0) {
1247 if (path->slots[0] == 0)
1248 break;
1249 path->slots[0]--;
1250 }
1251 btrfs_item_key_to_cpu(path->nodes[0], &key,
1252 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001253 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001254 key.type != BTRFS_INODE_REF_KEY)
1255 break;
1256 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1257 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1258 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001259 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001260 struct btrfs_inode_ref *ref;
1261
1262 ref = (struct btrfs_inode_ref *)ptr;
1263 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1264 ref);
1265 ptr = (unsigned long)(ref + 1) + name_len;
1266 nlink++;
1267 }
1268
1269 if (key.offset == 0)
1270 break;
1271 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001272 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001273 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001274 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001275
1276 return nlink;
1277}
1278
1279/*
1280 * There are a few corners where the link count of the file can't
1281 * be properly maintained during replay. So, instead of adding
1282 * lots of complexity to the log code, we just scan the backrefs
1283 * for any file that has been through replay.
1284 *
1285 * The scan will update the link count on the inode to reflect the
1286 * number of back refs found. If it goes down to zero, the iput
1287 * will free the inode.
1288 */
1289static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1290 struct btrfs_root *root,
1291 struct inode *inode)
1292{
1293 struct btrfs_path *path;
1294 int ret;
1295 u64 nlink = 0;
1296 u64 ino = btrfs_ino(inode);
1297
1298 path = btrfs_alloc_path();
1299 if (!path)
1300 return -ENOMEM;
1301
1302 ret = count_inode_refs(root, inode, path);
1303 if (ret < 0)
1304 goto out;
1305
1306 nlink = ret;
1307
1308 ret = count_inode_extrefs(root, inode, path);
1309 if (ret == -ENOENT)
1310 ret = 0;
1311
1312 if (ret < 0)
1313 goto out;
1314
1315 nlink += ret;
1316
1317 ret = 0;
1318
Chris Masone02119d2008-09-05 16:13:11 -04001319 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001320 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001321 btrfs_update_inode(trans, root, inode);
1322 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001323 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001324
Yan, Zhengc71bf092009-11-12 09:34:40 +00001325 if (inode->i_nlink == 0) {
1326 if (S_ISDIR(inode->i_mode)) {
1327 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001328 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001329 BUG_ON(ret);
1330 }
Li Zefan33345d012011-04-20 10:31:50 +08001331 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001332 BUG_ON(ret);
1333 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001334
Mark Fashehf1863732012-08-08 11:32:27 -07001335out:
1336 btrfs_free_path(path);
1337 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001338}
1339
1340static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root,
1342 struct btrfs_path *path)
1343{
1344 int ret;
1345 struct btrfs_key key;
1346 struct inode *inode;
1347
1348 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1349 key.type = BTRFS_ORPHAN_ITEM_KEY;
1350 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001351 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001352 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1353 if (ret < 0)
1354 break;
1355
1356 if (ret == 1) {
1357 if (path->slots[0] == 0)
1358 break;
1359 path->slots[0]--;
1360 }
1361
1362 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1363 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1364 key.type != BTRFS_ORPHAN_ITEM_KEY)
1365 break;
1366
1367 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001368 if (ret)
1369 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001370
David Sterbab3b4aa72011-04-21 01:20:15 +02001371 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001372 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001373 if (!inode)
1374 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001375
1376 ret = fixup_inode_link_count(trans, root, inode);
1377 BUG_ON(ret);
1378
1379 iput(inode);
1380
Chris Mason12fcfd22009-03-24 10:24:20 -04001381 /*
1382 * fixup on a directory may create new entries,
1383 * make sure we always look for the highset possible
1384 * offset
1385 */
1386 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001387 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001388 ret = 0;
1389out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001390 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001391 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001392}
1393
1394
1395/*
1396 * record a given inode in the fixup dir so we can check its link
1397 * count when replay is done. The link count is incremented here
1398 * so the inode won't go away until we check it
1399 */
1400static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1401 struct btrfs_root *root,
1402 struct btrfs_path *path,
1403 u64 objectid)
1404{
1405 struct btrfs_key key;
1406 int ret = 0;
1407 struct inode *inode;
1408
1409 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001410 if (!inode)
1411 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001412
1413 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1414 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1415 key.offset = objectid;
1416
1417 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1418
David Sterbab3b4aa72011-04-21 01:20:15 +02001419 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001420 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001421 if (!inode->i_nlink)
1422 set_nlink(inode, 1);
1423 else
1424 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001425 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001426 } else if (ret == -EEXIST) {
1427 ret = 0;
1428 } else {
1429 BUG();
1430 }
1431 iput(inode);
1432
1433 return ret;
1434}
1435
1436/*
1437 * when replaying the log for a directory, we only insert names
1438 * for inodes that actually exist. This means an fsync on a directory
1439 * does not implicitly fsync all the new files in it
1440 */
1441static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1442 struct btrfs_root *root,
1443 struct btrfs_path *path,
1444 u64 dirid, u64 index,
1445 char *name, int name_len, u8 type,
1446 struct btrfs_key *location)
1447{
1448 struct inode *inode;
1449 struct inode *dir;
1450 int ret;
1451
1452 inode = read_one_inode(root, location->objectid);
1453 if (!inode)
1454 return -ENOENT;
1455
1456 dir = read_one_inode(root, dirid);
1457 if (!dir) {
1458 iput(inode);
1459 return -EIO;
1460 }
1461 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1462
1463 /* FIXME, put inode into FIXUP list */
1464
1465 iput(inode);
1466 iput(dir);
1467 return ret;
1468}
1469
1470/*
1471 * take a single entry in a log directory item and replay it into
1472 * the subvolume.
1473 *
1474 * if a conflicting item exists in the subdirectory already,
1475 * the inode it points to is unlinked and put into the link count
1476 * fix up tree.
1477 *
1478 * If a name from the log points to a file or directory that does
1479 * not exist in the FS, it is skipped. fsyncs on directories
1480 * do not force down inodes inside that directory, just changes to the
1481 * names or unlinks in a directory.
1482 */
1483static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1484 struct btrfs_root *root,
1485 struct btrfs_path *path,
1486 struct extent_buffer *eb,
1487 struct btrfs_dir_item *di,
1488 struct btrfs_key *key)
1489{
1490 char *name;
1491 int name_len;
1492 struct btrfs_dir_item *dst_di;
1493 struct btrfs_key found_key;
1494 struct btrfs_key log_key;
1495 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001496 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001497 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001498 int ret;
1499
1500 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001501 if (!dir)
1502 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001503
1504 name_len = btrfs_dir_name_len(eb, di);
1505 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001506 if (!name)
1507 return -ENOMEM;
1508
Chris Masone02119d2008-09-05 16:13:11 -04001509 log_type = btrfs_dir_type(eb, di);
1510 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1511 name_len);
1512
1513 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001514 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1515 if (exists == 0)
1516 exists = 1;
1517 else
1518 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001519 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001520
Chris Masone02119d2008-09-05 16:13:11 -04001521 if (key->type == BTRFS_DIR_ITEM_KEY) {
1522 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1523 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001524 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001525 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1526 key->objectid,
1527 key->offset, name,
1528 name_len, 1);
1529 } else {
1530 BUG();
1531 }
David Sterbac7040052011-04-19 18:00:01 +02001532 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001533 /* we need a sequence number to insert, so we only
1534 * do inserts for the BTRFS_DIR_INDEX_KEY types
1535 */
1536 if (key->type != BTRFS_DIR_INDEX_KEY)
1537 goto out;
1538 goto insert;
1539 }
1540
1541 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1542 /* the existing item matches the logged item */
1543 if (found_key.objectid == log_key.objectid &&
1544 found_key.type == log_key.type &&
1545 found_key.offset == log_key.offset &&
1546 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1547 goto out;
1548 }
1549
1550 /*
1551 * don't drop the conflicting directory entry if the inode
1552 * for the new entry doesn't exist
1553 */
Chris Mason4bef0842008-09-08 11:18:08 -04001554 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001555 goto out;
1556
Chris Masone02119d2008-09-05 16:13:11 -04001557 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1558 BUG_ON(ret);
1559
1560 if (key->type == BTRFS_DIR_INDEX_KEY)
1561 goto insert;
1562out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001563 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001564 kfree(name);
1565 iput(dir);
1566 return 0;
1567
1568insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001569 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001570 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1571 name, name_len, log_type, &log_key);
1572
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001573 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001574 goto out;
1575}
1576
1577/*
1578 * find all the names in a directory item and reconcile them into
1579 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1580 * one name in a directory item, but the same code gets used for
1581 * both directory index types
1582 */
1583static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1584 struct btrfs_root *root,
1585 struct btrfs_path *path,
1586 struct extent_buffer *eb, int slot,
1587 struct btrfs_key *key)
1588{
1589 int ret;
1590 u32 item_size = btrfs_item_size_nr(eb, slot);
1591 struct btrfs_dir_item *di;
1592 int name_len;
1593 unsigned long ptr;
1594 unsigned long ptr_end;
1595
1596 ptr = btrfs_item_ptr_offset(eb, slot);
1597 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001598 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001599 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001600 if (verify_dir_item(root, eb, di))
1601 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001602 name_len = btrfs_dir_name_len(eb, di);
1603 ret = replay_one_name(trans, root, path, eb, di, key);
1604 BUG_ON(ret);
1605 ptr = (unsigned long)(di + 1);
1606 ptr += name_len;
1607 }
1608 return 0;
1609}
1610
1611/*
1612 * directory replay has two parts. There are the standard directory
1613 * items in the log copied from the subvolume, and range items
1614 * created in the log while the subvolume was logged.
1615 *
1616 * The range items tell us which parts of the key space the log
1617 * is authoritative for. During replay, if a key in the subvolume
1618 * directory is in a logged range item, but not actually in the log
1619 * that means it was deleted from the directory before the fsync
1620 * and should be removed.
1621 */
1622static noinline int find_dir_range(struct btrfs_root *root,
1623 struct btrfs_path *path,
1624 u64 dirid, int key_type,
1625 u64 *start_ret, u64 *end_ret)
1626{
1627 struct btrfs_key key;
1628 u64 found_end;
1629 struct btrfs_dir_log_item *item;
1630 int ret;
1631 int nritems;
1632
1633 if (*start_ret == (u64)-1)
1634 return 1;
1635
1636 key.objectid = dirid;
1637 key.type = key_type;
1638 key.offset = *start_ret;
1639
1640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1641 if (ret < 0)
1642 goto out;
1643 if (ret > 0) {
1644 if (path->slots[0] == 0)
1645 goto out;
1646 path->slots[0]--;
1647 }
1648 if (ret != 0)
1649 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1650
1651 if (key.type != key_type || key.objectid != dirid) {
1652 ret = 1;
1653 goto next;
1654 }
1655 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1656 struct btrfs_dir_log_item);
1657 found_end = btrfs_dir_log_end(path->nodes[0], item);
1658
1659 if (*start_ret >= key.offset && *start_ret <= found_end) {
1660 ret = 0;
1661 *start_ret = key.offset;
1662 *end_ret = found_end;
1663 goto out;
1664 }
1665 ret = 1;
1666next:
1667 /* check the next slot in the tree to see if it is a valid item */
1668 nritems = btrfs_header_nritems(path->nodes[0]);
1669 if (path->slots[0] >= nritems) {
1670 ret = btrfs_next_leaf(root, path);
1671 if (ret)
1672 goto out;
1673 } else {
1674 path->slots[0]++;
1675 }
1676
1677 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1678
1679 if (key.type != key_type || key.objectid != dirid) {
1680 ret = 1;
1681 goto out;
1682 }
1683 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1684 struct btrfs_dir_log_item);
1685 found_end = btrfs_dir_log_end(path->nodes[0], item);
1686 *start_ret = key.offset;
1687 *end_ret = found_end;
1688 ret = 0;
1689out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001690 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001691 return ret;
1692}
1693
1694/*
1695 * this looks for a given directory item in the log. If the directory
1696 * item is not in the log, the item is removed and the inode it points
1697 * to is unlinked
1698 */
1699static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_root *log,
1702 struct btrfs_path *path,
1703 struct btrfs_path *log_path,
1704 struct inode *dir,
1705 struct btrfs_key *dir_key)
1706{
1707 int ret;
1708 struct extent_buffer *eb;
1709 int slot;
1710 u32 item_size;
1711 struct btrfs_dir_item *di;
1712 struct btrfs_dir_item *log_di;
1713 int name_len;
1714 unsigned long ptr;
1715 unsigned long ptr_end;
1716 char *name;
1717 struct inode *inode;
1718 struct btrfs_key location;
1719
1720again:
1721 eb = path->nodes[0];
1722 slot = path->slots[0];
1723 item_size = btrfs_item_size_nr(eb, slot);
1724 ptr = btrfs_item_ptr_offset(eb, slot);
1725 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001726 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001727 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001728 if (verify_dir_item(root, eb, di)) {
1729 ret = -EIO;
1730 goto out;
1731 }
1732
Chris Masone02119d2008-09-05 16:13:11 -04001733 name_len = btrfs_dir_name_len(eb, di);
1734 name = kmalloc(name_len, GFP_NOFS);
1735 if (!name) {
1736 ret = -ENOMEM;
1737 goto out;
1738 }
1739 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1740 name_len);
1741 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001742 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001743 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1744 dir_key->objectid,
1745 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001746 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001747 log_di = btrfs_lookup_dir_index_item(trans, log,
1748 log_path,
1749 dir_key->objectid,
1750 dir_key->offset,
1751 name, name_len, 0);
1752 }
David Sterbac7040052011-04-19 18:00:01 +02001753 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001754 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001755 btrfs_release_path(path);
1756 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001757 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001758 if (!inode) {
1759 kfree(name);
1760 return -EIO;
1761 }
Chris Masone02119d2008-09-05 16:13:11 -04001762
1763 ret = link_to_fixup_dir(trans, root,
1764 path, location.objectid);
1765 BUG_ON(ret);
1766 btrfs_inc_nlink(inode);
1767 ret = btrfs_unlink_inode(trans, root, dir, inode,
1768 name, name_len);
1769 BUG_ON(ret);
Chris Masonb6305562012-07-02 15:29:53 -04001770
1771 btrfs_run_delayed_items(trans, root);
1772
Chris Masone02119d2008-09-05 16:13:11 -04001773 kfree(name);
1774 iput(inode);
1775
1776 /* there might still be more names under this key
1777 * check and repeat if required
1778 */
1779 ret = btrfs_search_slot(NULL, root, dir_key, path,
1780 0, 0);
1781 if (ret == 0)
1782 goto again;
1783 ret = 0;
1784 goto out;
1785 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001786 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001787 kfree(name);
1788
1789 ptr = (unsigned long)(di + 1);
1790 ptr += name_len;
1791 }
1792 ret = 0;
1793out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001794 btrfs_release_path(path);
1795 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001796 return ret;
1797}
1798
1799/*
1800 * deletion replay happens before we copy any new directory items
1801 * out of the log or out of backreferences from inodes. It
1802 * scans the log to find ranges of keys that log is authoritative for,
1803 * and then scans the directory to find items in those ranges that are
1804 * not present in the log.
1805 *
1806 * Anything we don't find in the log is unlinked and removed from the
1807 * directory.
1808 */
1809static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1810 struct btrfs_root *root,
1811 struct btrfs_root *log,
1812 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001813 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001814{
1815 u64 range_start;
1816 u64 range_end;
1817 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1818 int ret = 0;
1819 struct btrfs_key dir_key;
1820 struct btrfs_key found_key;
1821 struct btrfs_path *log_path;
1822 struct inode *dir;
1823
1824 dir_key.objectid = dirid;
1825 dir_key.type = BTRFS_DIR_ITEM_KEY;
1826 log_path = btrfs_alloc_path();
1827 if (!log_path)
1828 return -ENOMEM;
1829
1830 dir = read_one_inode(root, dirid);
1831 /* it isn't an error if the inode isn't there, that can happen
1832 * because we replay the deletes before we copy in the inode item
1833 * from the log
1834 */
1835 if (!dir) {
1836 btrfs_free_path(log_path);
1837 return 0;
1838 }
1839again:
1840 range_start = 0;
1841 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001842 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001843 if (del_all)
1844 range_end = (u64)-1;
1845 else {
1846 ret = find_dir_range(log, path, dirid, key_type,
1847 &range_start, &range_end);
1848 if (ret != 0)
1849 break;
1850 }
Chris Masone02119d2008-09-05 16:13:11 -04001851
1852 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001853 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001854 int nritems;
1855 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1856 0, 0);
1857 if (ret < 0)
1858 goto out;
1859
1860 nritems = btrfs_header_nritems(path->nodes[0]);
1861 if (path->slots[0] >= nritems) {
1862 ret = btrfs_next_leaf(root, path);
1863 if (ret)
1864 break;
1865 }
1866 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1867 path->slots[0]);
1868 if (found_key.objectid != dirid ||
1869 found_key.type != dir_key.type)
1870 goto next_type;
1871
1872 if (found_key.offset > range_end)
1873 break;
1874
1875 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001876 log_path, dir,
1877 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001878 BUG_ON(ret);
1879 if (found_key.offset == (u64)-1)
1880 break;
1881 dir_key.offset = found_key.offset + 1;
1882 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001883 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001884 if (range_end == (u64)-1)
1885 break;
1886 range_start = range_end + 1;
1887 }
1888
1889next_type:
1890 ret = 0;
1891 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1892 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1893 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001894 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001895 goto again;
1896 }
1897out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001898 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001899 btrfs_free_path(log_path);
1900 iput(dir);
1901 return ret;
1902}
1903
1904/*
1905 * the process_func used to replay items from the log tree. This
1906 * gets called in two different stages. The first stage just looks
1907 * for inodes and makes sure they are all copied into the subvolume.
1908 *
1909 * The second stage copies all the other item types from the log into
1910 * the subvolume. The two stage approach is slower, but gets rid of
1911 * lots of complexity around inodes referencing other inodes that exist
1912 * only in the log (references come from either directory items or inode
1913 * back refs).
1914 */
1915static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1916 struct walk_control *wc, u64 gen)
1917{
1918 int nritems;
1919 struct btrfs_path *path;
1920 struct btrfs_root *root = wc->replay_dest;
1921 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001922 int level;
1923 int i;
1924 int ret;
1925
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001926 ret = btrfs_read_buffer(eb, gen);
1927 if (ret)
1928 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001929
1930 level = btrfs_header_level(eb);
1931
1932 if (level != 0)
1933 return 0;
1934
1935 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001936 if (!path)
1937 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001938
1939 nritems = btrfs_header_nritems(eb);
1940 for (i = 0; i < nritems; i++) {
1941 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001942
1943 /* inode keys are done during the first stage */
1944 if (key.type == BTRFS_INODE_ITEM_KEY &&
1945 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001946 struct btrfs_inode_item *inode_item;
1947 u32 mode;
1948
1949 inode_item = btrfs_item_ptr(eb, i,
1950 struct btrfs_inode_item);
1951 mode = btrfs_inode_mode(eb, inode_item);
1952 if (S_ISDIR(mode)) {
1953 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001954 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001955 BUG_ON(ret);
1956 }
1957 ret = overwrite_item(wc->trans, root, path,
1958 eb, i, &key);
1959 BUG_ON(ret);
1960
Yan, Zhengc71bf092009-11-12 09:34:40 +00001961 /* for regular files, make sure corresponding
1962 * orhpan item exist. extents past the new EOF
1963 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001964 */
1965 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001966 ret = insert_orphan_item(wc->trans, root,
1967 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001968 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001969 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001970
Chris Masone02119d2008-09-05 16:13:11 -04001971 ret = link_to_fixup_dir(wc->trans, root,
1972 path, key.objectid);
1973 BUG_ON(ret);
1974 }
1975 if (wc->stage < LOG_WALK_REPLAY_ALL)
1976 continue;
1977
1978 /* these keys are simply copied */
1979 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1980 ret = overwrite_item(wc->trans, root, path,
1981 eb, i, &key);
1982 BUG_ON(ret);
1983 } else if (key.type == BTRFS_INODE_REF_KEY) {
1984 ret = add_inode_ref(wc->trans, root, log, path,
1985 eb, i, &key);
1986 BUG_ON(ret && ret != -ENOENT);
Mark Fashehf1863732012-08-08 11:32:27 -07001987 } else if (key.type == BTRFS_INODE_EXTREF_KEY) {
1988 ret = add_inode_ref(wc->trans, root, log, path,
1989 eb, i, &key);
1990 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001991 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1992 ret = replay_one_extent(wc->trans, root, path,
1993 eb, i, &key);
1994 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001995 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1996 key.type == BTRFS_DIR_INDEX_KEY) {
1997 ret = replay_one_dir_item(wc->trans, root, path,
1998 eb, i, &key);
1999 BUG_ON(ret);
2000 }
2001 }
2002 btrfs_free_path(path);
2003 return 0;
2004}
2005
Chris Masond3977122009-01-05 21:25:51 -05002006static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002007 struct btrfs_root *root,
2008 struct btrfs_path *path, int *level,
2009 struct walk_control *wc)
2010{
2011 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002012 u64 bytenr;
2013 u64 ptr_gen;
2014 struct extent_buffer *next;
2015 struct extent_buffer *cur;
2016 struct extent_buffer *parent;
2017 u32 blocksize;
2018 int ret = 0;
2019
2020 WARN_ON(*level < 0);
2021 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2022
Chris Masond3977122009-01-05 21:25:51 -05002023 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002024 WARN_ON(*level < 0);
2025 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2026 cur = path->nodes[*level];
2027
2028 if (btrfs_header_level(cur) != *level)
2029 WARN_ON(1);
2030
2031 if (path->slots[*level] >=
2032 btrfs_header_nritems(cur))
2033 break;
2034
2035 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2036 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2037 blocksize = btrfs_level_size(root, *level - 1);
2038
2039 parent = path->nodes[*level];
2040 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002041
2042 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002043 if (!next)
2044 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002045
Chris Masone02119d2008-09-05 16:13:11 -04002046 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002047 ret = wc->process_func(root, next, wc, ptr_gen);
2048 if (ret)
2049 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002050
Chris Masone02119d2008-09-05 16:13:11 -04002051 path->slots[*level]++;
2052 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002053 ret = btrfs_read_buffer(next, ptr_gen);
2054 if (ret) {
2055 free_extent_buffer(next);
2056 return ret;
2057 }
Chris Masone02119d2008-09-05 16:13:11 -04002058
2059 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002060 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002061 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002062 btrfs_wait_tree_block_writeback(next);
2063 btrfs_tree_unlock(next);
2064
Chris Masone02119d2008-09-05 16:13:11 -04002065 WARN_ON(root_owner !=
2066 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002067 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002068 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002069 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002070 }
2071 free_extent_buffer(next);
2072 continue;
2073 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002074 ret = btrfs_read_buffer(next, ptr_gen);
2075 if (ret) {
2076 free_extent_buffer(next);
2077 return ret;
2078 }
Chris Masone02119d2008-09-05 16:13:11 -04002079
2080 WARN_ON(*level <= 0);
2081 if (path->nodes[*level-1])
2082 free_extent_buffer(path->nodes[*level-1]);
2083 path->nodes[*level-1] = next;
2084 *level = btrfs_header_level(next);
2085 path->slots[*level] = 0;
2086 cond_resched();
2087 }
2088 WARN_ON(*level < 0);
2089 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2090
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002091 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002092
2093 cond_resched();
2094 return 0;
2095}
2096
Chris Masond3977122009-01-05 21:25:51 -05002097static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002098 struct btrfs_root *root,
2099 struct btrfs_path *path, int *level,
2100 struct walk_control *wc)
2101{
2102 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002103 int i;
2104 int slot;
2105 int ret;
2106
Chris Masond3977122009-01-05 21:25:51 -05002107 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002108 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002109 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002110 path->slots[i]++;
2111 *level = i;
2112 WARN_ON(*level == 0);
2113 return 0;
2114 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002115 struct extent_buffer *parent;
2116 if (path->nodes[*level] == root->node)
2117 parent = path->nodes[*level];
2118 else
2119 parent = path->nodes[*level + 1];
2120
2121 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002122 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002123 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002124 if (ret)
2125 return ret;
2126
Chris Masone02119d2008-09-05 16:13:11 -04002127 if (wc->free) {
2128 struct extent_buffer *next;
2129
2130 next = path->nodes[*level];
2131
2132 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002133 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002134 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002135 btrfs_wait_tree_block_writeback(next);
2136 btrfs_tree_unlock(next);
2137
Chris Masone02119d2008-09-05 16:13:11 -04002138 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002139 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002140 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002141 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04002142 BUG_ON(ret);
2143 }
2144 free_extent_buffer(path->nodes[*level]);
2145 path->nodes[*level] = NULL;
2146 *level = i + 1;
2147 }
2148 }
2149 return 1;
2150}
2151
2152/*
2153 * drop the reference count on the tree rooted at 'snap'. This traverses
2154 * the tree freeing any blocks that have a ref count of zero after being
2155 * decremented.
2156 */
2157static int walk_log_tree(struct btrfs_trans_handle *trans,
2158 struct btrfs_root *log, struct walk_control *wc)
2159{
2160 int ret = 0;
2161 int wret;
2162 int level;
2163 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002164 int orig_level;
2165
2166 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002167 if (!path)
2168 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002169
2170 level = btrfs_header_level(log->node);
2171 orig_level = level;
2172 path->nodes[level] = log->node;
2173 extent_buffer_get(log->node);
2174 path->slots[level] = 0;
2175
Chris Masond3977122009-01-05 21:25:51 -05002176 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002177 wret = walk_down_log_tree(trans, log, path, &level, wc);
2178 if (wret > 0)
2179 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002180 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002181 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182 goto out;
2183 }
Chris Masone02119d2008-09-05 16:13:11 -04002184
2185 wret = walk_up_log_tree(trans, log, path, &level, wc);
2186 if (wret > 0)
2187 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002188 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002189 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002190 goto out;
2191 }
Chris Masone02119d2008-09-05 16:13:11 -04002192 }
2193
2194 /* was the root node processed? if not, catch it here */
2195 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002196 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002197 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002198 if (ret)
2199 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002200 if (wc->free) {
2201 struct extent_buffer *next;
2202
2203 next = path->nodes[orig_level];
2204
2205 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002206 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002207 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04002208 btrfs_wait_tree_block_writeback(next);
2209 btrfs_tree_unlock(next);
2210
Chris Masone02119d2008-09-05 16:13:11 -04002211 WARN_ON(log->root_key.objectid !=
2212 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002213 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002214 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002215 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04002216 }
2217 }
2218
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002219out:
Chris Masone02119d2008-09-05 16:13:11 -04002220 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002221 return ret;
2222}
2223
Yan Zheng7237f1832009-01-21 12:54:03 -05002224/*
2225 * helper function to update the item for a given subvolumes log root
2226 * in the tree of log roots
2227 */
2228static int update_log_root(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *log)
2230{
2231 int ret;
2232
2233 if (log->log_transid == 1) {
2234 /* insert root item on the first sync */
2235 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2236 &log->root_key, &log->root_item);
2237 } else {
2238 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2239 &log->root_key, &log->root_item);
2240 }
2241 return ret;
2242}
2243
Chris Mason12fcfd22009-03-24 10:24:20 -04002244static int wait_log_commit(struct btrfs_trans_handle *trans,
2245 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002246{
2247 DEFINE_WAIT(wait);
Yan Zheng7237f1832009-01-21 12:54:03 -05002248 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002249
Yan Zheng7237f1832009-01-21 12:54:03 -05002250 /*
2251 * we only allow two pending log transactions at a time,
2252 * so we know that if ours is more than 2 older than the
2253 * current transaction, we're done
2254 */
Chris Masone02119d2008-09-05 16:13:11 -04002255 do {
Yan Zheng7237f1832009-01-21 12:54:03 -05002256 prepare_to_wait(&root->log_commit_wait[index],
2257 &wait, TASK_UNINTERRUPTIBLE);
2258 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002259
2260 if (root->fs_info->last_trans_log_full_commit !=
2261 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002262 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002263 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002264
Yan Zheng7237f1832009-01-21 12:54:03 -05002265 finish_wait(&root->log_commit_wait[index], &wait);
2266 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002267 } while (root->fs_info->last_trans_log_full_commit !=
2268 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002269 atomic_read(&root->log_commit[index]));
2270 return 0;
2271}
2272
Jeff Mahoney143bede2012-03-01 14:56:26 +01002273static void wait_for_writer(struct btrfs_trans_handle *trans,
2274 struct btrfs_root *root)
Yan Zheng7237f1832009-01-21 12:54:03 -05002275{
2276 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002277 while (root->fs_info->last_trans_log_full_commit !=
2278 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f1832009-01-21 12:54:03 -05002279 prepare_to_wait(&root->log_writer_wait,
2280 &wait, TASK_UNINTERRUPTIBLE);
2281 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002282 if (root->fs_info->last_trans_log_full_commit !=
2283 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f1832009-01-21 12:54:03 -05002284 schedule();
2285 mutex_lock(&root->log_mutex);
2286 finish_wait(&root->log_writer_wait, &wait);
2287 }
Chris Masone02119d2008-09-05 16:13:11 -04002288}
2289
2290/*
2291 * btrfs_sync_log does sends a given tree log down to the disk and
2292 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002293 * you know that any inodes previously logged are safely on disk only
2294 * if it returns 0.
2295 *
2296 * Any other return value means you need to call btrfs_commit_transaction.
2297 * Some of the edge cases for fsyncing directories that have had unlinks
2298 * or renames done in the past mean that sometimes the only safe
2299 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2300 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002301 */
2302int btrfs_sync_log(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root)
2304{
Yan Zheng7237f1832009-01-21 12:54:03 -05002305 int index1;
2306 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002307 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002308 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002309 struct btrfs_root *log = root->log_root;
Yan Zheng7237f1832009-01-21 12:54:03 -05002310 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002311 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002312
Yan Zheng7237f1832009-01-21 12:54:03 -05002313 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002314 log_transid = root->log_transid;
Yan Zheng7237f1832009-01-21 12:54:03 -05002315 index1 = root->log_transid % 2;
2316 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002317 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002318 mutex_unlock(&root->log_mutex);
2319 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002320 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002321 atomic_set(&root->log_commit[index1], 1);
2322
2323 /* wait for previous tree log sync to complete */
2324 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002325 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002326 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002327 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002328 /* when we're on an ssd, just kick the log commit out */
2329 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002330 mutex_unlock(&root->log_mutex);
2331 schedule_timeout_uninterruptible(1);
2332 mutex_lock(&root->log_mutex);
2333 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002334 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002335 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002336 break;
2337 }
Chris Masond0c803c2008-09-11 16:17:57 -04002338
Chris Mason12fcfd22009-03-24 10:24:20 -04002339 /* bail out if we need to do a full commit */
2340 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2341 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002342 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002343 mutex_unlock(&root->log_mutex);
2344 goto out;
2345 }
2346
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002347 if (log_transid % 2 == 0)
2348 mark = EXTENT_DIRTY;
2349 else
2350 mark = EXTENT_NEW;
2351
Chris Mason690587d2009-10-13 13:29:19 -04002352 /* we start IO on all the marked extents here, but we don't actually
2353 * wait for them until later.
2354 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002355 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002356 if (ret) {
2357 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002358 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002359 mutex_unlock(&root->log_mutex);
2360 goto out;
2361 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002362
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002363 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f1832009-01-21 12:54:03 -05002364
Yan Zheng7237f1832009-01-21 12:54:03 -05002365 root->log_transid++;
2366 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002367 root->log_start_pid = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -05002368 smp_mb();
2369 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002370 * IO has been started, blocks of the log tree have WRITTEN flag set
2371 * in their headers. new modifications of the log will be written to
2372 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f1832009-01-21 12:54:03 -05002373 */
2374 mutex_unlock(&root->log_mutex);
2375
2376 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002377 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -05002378 atomic_inc(&log_root_tree->log_writers);
2379 mutex_unlock(&log_root_tree->log_mutex);
2380
2381 ret = update_log_root(trans, log);
Yan Zheng7237f1832009-01-21 12:54:03 -05002382
2383 mutex_lock(&log_root_tree->log_mutex);
2384 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2385 smp_mb();
2386 if (waitqueue_active(&log_root_tree->log_writer_wait))
2387 wake_up(&log_root_tree->log_writer_wait);
2388 }
2389
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002390 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002391 if (ret != -ENOSPC) {
2392 btrfs_abort_transaction(trans, root, ret);
2393 mutex_unlock(&log_root_tree->log_mutex);
2394 goto out;
2395 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002396 root->fs_info->last_trans_log_full_commit = trans->transid;
2397 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002398 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002399 mutex_unlock(&log_root_tree->log_mutex);
2400 ret = -EAGAIN;
2401 goto out;
2402 }
2403
Yan Zheng7237f1832009-01-21 12:54:03 -05002404 index2 = log_root_tree->log_transid % 2;
2405 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002406 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002407 wait_log_commit(trans, log_root_tree,
2408 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002409 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002410 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002411 ret = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -05002412 goto out;
2413 }
2414 atomic_set(&log_root_tree->log_commit[index2], 1);
2415
Chris Mason12fcfd22009-03-24 10:24:20 -04002416 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2417 wait_log_commit(trans, log_root_tree,
2418 log_root_tree->log_transid - 1);
2419 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002420
Chris Mason12fcfd22009-03-24 10:24:20 -04002421 wait_for_writer(trans, log_root_tree);
2422
2423 /*
2424 * now that we've moved on to the tree of log tree roots,
2425 * check the full commit flag again
2426 */
2427 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002428 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002429 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002430 mutex_unlock(&log_root_tree->log_mutex);
2431 ret = -EAGAIN;
2432 goto out_wake_log_root;
2433 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002434
2435 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002436 &log_root_tree->dirty_log_pages,
2437 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002438 if (ret) {
2439 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002440 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002441 mutex_unlock(&log_root_tree->log_mutex);
2442 goto out_wake_log_root;
2443 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002444 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002445 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002446
David Sterba6c417612011-04-13 15:41:04 +02002447 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002448 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002449 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002450 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002451
Yan Zheng7237f1832009-01-21 12:54:03 -05002452 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002453 smp_mb();
Yan Zheng7237f1832009-01-21 12:54:03 -05002454
2455 mutex_unlock(&log_root_tree->log_mutex);
2456
2457 /*
2458 * nobody else is going to jump in and write the the ctree
2459 * super here because the log_commit atomic below is protecting
2460 * us. We must be called with a transaction handle pinning
2461 * the running transaction open, so a full commit can't hop
2462 * in and cause problems either.
2463 */
Arne Jansena2de7332011-03-08 14:14:00 +01002464 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002465 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002466 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002467 if (ret) {
2468 btrfs_abort_transaction(trans, root, ret);
2469 goto out_wake_log_root;
2470 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002471
Chris Mason257c62e2009-10-13 13:21:08 -04002472 mutex_lock(&root->log_mutex);
2473 if (root->last_log_commit < log_transid)
2474 root->last_log_commit = log_transid;
2475 mutex_unlock(&root->log_mutex);
2476
Chris Mason12fcfd22009-03-24 10:24:20 -04002477out_wake_log_root:
Yan Zheng7237f1832009-01-21 12:54:03 -05002478 atomic_set(&log_root_tree->log_commit[index2], 0);
2479 smp_mb();
2480 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2481 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002482out:
Yan Zheng7237f1832009-01-21 12:54:03 -05002483 atomic_set(&root->log_commit[index1], 0);
2484 smp_mb();
2485 if (waitqueue_active(&root->log_commit_wait[index1]))
2486 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002487 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002488}
2489
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002490static void free_log_tree(struct btrfs_trans_handle *trans,
2491 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002492{
2493 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002494 u64 start;
2495 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002496 struct walk_control wc = {
2497 .free = 1,
2498 .process_func = process_one_buffer
2499 };
2500
Liu Bo33217192013-02-27 13:28:24 +00002501 if (trans) {
2502 ret = walk_log_tree(trans, log, &wc);
2503 BUG_ON(ret);
2504 }
Chris Masone02119d2008-09-05 16:13:11 -04002505
Chris Masond3977122009-01-05 21:25:51 -05002506 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002507 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002508 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2509 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002510 if (ret)
2511 break;
2512
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002513 clear_extent_bits(&log->dirty_log_pages, start, end,
2514 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002515 }
2516
Josef Bacik2ab28f32012-10-12 15:27:49 -04002517 /*
2518 * We may have short-circuited the log tree with the full commit logic
2519 * and left ordered extents on our list, so clear these out to keep us
2520 * from leaking inodes and memory.
2521 */
2522 btrfs_free_logged_extents(log, 0);
2523 btrfs_free_logged_extents(log, 1);
2524
Yan Zheng7237f1832009-01-21 12:54:03 -05002525 free_extent_buffer(log->node);
2526 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002527}
2528
2529/*
2530 * free all the extents used by the tree log. This should be called
2531 * at commit time of the full transaction
2532 */
2533int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2534{
2535 if (root->log_root) {
2536 free_log_tree(trans, root->log_root);
2537 root->log_root = NULL;
2538 }
2539 return 0;
2540}
2541
2542int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2543 struct btrfs_fs_info *fs_info)
2544{
2545 if (fs_info->log_root_tree) {
2546 free_log_tree(trans, fs_info->log_root_tree);
2547 fs_info->log_root_tree = NULL;
2548 }
Chris Masone02119d2008-09-05 16:13:11 -04002549 return 0;
2550}
2551
2552/*
Chris Masone02119d2008-09-05 16:13:11 -04002553 * If both a file and directory are logged, and unlinks or renames are
2554 * mixed in, we have a few interesting corners:
2555 *
2556 * create file X in dir Y
2557 * link file X to X.link in dir Y
2558 * fsync file X
2559 * unlink file X but leave X.link
2560 * fsync dir Y
2561 *
2562 * After a crash we would expect only X.link to exist. But file X
2563 * didn't get fsync'd again so the log has back refs for X and X.link.
2564 *
2565 * We solve this by removing directory entries and inode backrefs from the
2566 * log when a file that was logged in the current transaction is
2567 * unlinked. Any later fsync will include the updated log entries, and
2568 * we'll be able to reconstruct the proper directory items from backrefs.
2569 *
2570 * This optimizations allows us to avoid relogging the entire inode
2571 * or the entire directory.
2572 */
2573int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2574 struct btrfs_root *root,
2575 const char *name, int name_len,
2576 struct inode *dir, u64 index)
2577{
2578 struct btrfs_root *log;
2579 struct btrfs_dir_item *di;
2580 struct btrfs_path *path;
2581 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002582 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002583 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002584 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002585
Chris Mason3a5f1d42008-09-11 15:53:37 -04002586 if (BTRFS_I(dir)->logged_trans < trans->transid)
2587 return 0;
2588
Chris Masone02119d2008-09-05 16:13:11 -04002589 ret = join_running_log_trans(root);
2590 if (ret)
2591 return 0;
2592
2593 mutex_lock(&BTRFS_I(dir)->log_mutex);
2594
2595 log = root->log_root;
2596 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002597 if (!path) {
2598 err = -ENOMEM;
2599 goto out_unlock;
2600 }
liubo2a29edc2011-01-26 06:22:08 +00002601
Li Zefan33345d012011-04-20 10:31:50 +08002602 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002603 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002604 if (IS_ERR(di)) {
2605 err = PTR_ERR(di);
2606 goto fail;
2607 }
2608 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002609 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2610 bytes_del += name_len;
2611 BUG_ON(ret);
2612 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002613 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002614 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002615 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002616 if (IS_ERR(di)) {
2617 err = PTR_ERR(di);
2618 goto fail;
2619 }
2620 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002621 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2622 bytes_del += name_len;
2623 BUG_ON(ret);
2624 }
2625
2626 /* update the directory size in the log to reflect the names
2627 * we have removed
2628 */
2629 if (bytes_del) {
2630 struct btrfs_key key;
2631
Li Zefan33345d012011-04-20 10:31:50 +08002632 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002633 key.offset = 0;
2634 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002635 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002636
2637 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002638 if (ret < 0) {
2639 err = ret;
2640 goto fail;
2641 }
Chris Masone02119d2008-09-05 16:13:11 -04002642 if (ret == 0) {
2643 struct btrfs_inode_item *item;
2644 u64 i_size;
2645
2646 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2647 struct btrfs_inode_item);
2648 i_size = btrfs_inode_size(path->nodes[0], item);
2649 if (i_size > bytes_del)
2650 i_size -= bytes_del;
2651 else
2652 i_size = 0;
2653 btrfs_set_inode_size(path->nodes[0], item, i_size);
2654 btrfs_mark_buffer_dirty(path->nodes[0]);
2655 } else
2656 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002657 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002658 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002659fail:
Chris Masone02119d2008-09-05 16:13:11 -04002660 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002661out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002662 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002663 if (ret == -ENOSPC) {
2664 root->fs_info->last_trans_log_full_commit = trans->transid;
2665 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002666 } else if (ret < 0)
2667 btrfs_abort_transaction(trans, root, ret);
2668
Chris Mason12fcfd22009-03-24 10:24:20 -04002669 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002670
Andi Kleen411fc6b2010-10-29 15:14:31 -04002671 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002672}
2673
2674/* see comments for btrfs_del_dir_entries_in_log */
2675int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2676 struct btrfs_root *root,
2677 const char *name, int name_len,
2678 struct inode *inode, u64 dirid)
2679{
2680 struct btrfs_root *log;
2681 u64 index;
2682 int ret;
2683
Chris Mason3a5f1d42008-09-11 15:53:37 -04002684 if (BTRFS_I(inode)->logged_trans < trans->transid)
2685 return 0;
2686
Chris Masone02119d2008-09-05 16:13:11 -04002687 ret = join_running_log_trans(root);
2688 if (ret)
2689 return 0;
2690 log = root->log_root;
2691 mutex_lock(&BTRFS_I(inode)->log_mutex);
2692
Li Zefan33345d012011-04-20 10:31:50 +08002693 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002694 dirid, &index);
2695 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002696 if (ret == -ENOSPC) {
2697 root->fs_info->last_trans_log_full_commit = trans->transid;
2698 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002699 } else if (ret < 0 && ret != -ENOENT)
2700 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002701 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002702
Chris Masone02119d2008-09-05 16:13:11 -04002703 return ret;
2704}
2705
2706/*
2707 * creates a range item in the log for 'dirid'. first_offset and
2708 * last_offset tell us which parts of the key space the log should
2709 * be considered authoritative for.
2710 */
2711static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2712 struct btrfs_root *log,
2713 struct btrfs_path *path,
2714 int key_type, u64 dirid,
2715 u64 first_offset, u64 last_offset)
2716{
2717 int ret;
2718 struct btrfs_key key;
2719 struct btrfs_dir_log_item *item;
2720
2721 key.objectid = dirid;
2722 key.offset = first_offset;
2723 if (key_type == BTRFS_DIR_ITEM_KEY)
2724 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2725 else
2726 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2727 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002728 if (ret)
2729 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002730
2731 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2732 struct btrfs_dir_log_item);
2733 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2734 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002735 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002736 return 0;
2737}
2738
2739/*
2740 * log all the items included in the current transaction for a given
2741 * directory. This also creates the range items in the log tree required
2742 * to replay anything deleted before the fsync
2743 */
2744static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2745 struct btrfs_root *root, struct inode *inode,
2746 struct btrfs_path *path,
2747 struct btrfs_path *dst_path, int key_type,
2748 u64 min_offset, u64 *last_offset_ret)
2749{
2750 struct btrfs_key min_key;
2751 struct btrfs_key max_key;
2752 struct btrfs_root *log = root->log_root;
2753 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002754 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002755 int ret;
2756 int i;
2757 int nritems;
2758 u64 first_offset = min_offset;
2759 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002760 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002761
2762 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002763 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002764 max_key.offset = (u64)-1;
2765 max_key.type = key_type;
2766
Li Zefan33345d012011-04-20 10:31:50 +08002767 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002768 min_key.type = key_type;
2769 min_key.offset = min_offset;
2770
2771 path->keep_locks = 1;
2772
2773 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00002774 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002775
2776 /*
2777 * we didn't find anything from this transaction, see if there
2778 * is anything at all
2779 */
Li Zefan33345d012011-04-20 10:31:50 +08002780 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2781 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002782 min_key.type = key_type;
2783 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002784 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002785 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2786 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002787 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002788 return ret;
2789 }
Li Zefan33345d012011-04-20 10:31:50 +08002790 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002791
2792 /* if ret == 0 there are items for this type,
2793 * create a range to tell us the last key of this type.
2794 * otherwise, there are no items in this directory after
2795 * *min_offset, and we create a range to indicate that.
2796 */
2797 if (ret == 0) {
2798 struct btrfs_key tmp;
2799 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2800 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002801 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002802 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002803 }
2804 goto done;
2805 }
2806
2807 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002808 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002809 if (ret == 0) {
2810 struct btrfs_key tmp;
2811 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2812 if (key_type == tmp.type) {
2813 first_offset = tmp.offset;
2814 ret = overwrite_item(trans, log, dst_path,
2815 path->nodes[0], path->slots[0],
2816 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002817 if (ret) {
2818 err = ret;
2819 goto done;
2820 }
Chris Masone02119d2008-09-05 16:13:11 -04002821 }
2822 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002823 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002824
2825 /* find the first key from this transaction again */
2826 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2827 if (ret != 0) {
2828 WARN_ON(1);
2829 goto done;
2830 }
2831
2832 /*
2833 * we have a block from this transaction, log every item in it
2834 * from our directory
2835 */
Chris Masond3977122009-01-05 21:25:51 -05002836 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002837 struct btrfs_key tmp;
2838 src = path->nodes[0];
2839 nritems = btrfs_header_nritems(src);
2840 for (i = path->slots[0]; i < nritems; i++) {
2841 btrfs_item_key_to_cpu(src, &min_key, i);
2842
Li Zefan33345d012011-04-20 10:31:50 +08002843 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002844 goto done;
2845 ret = overwrite_item(trans, log, dst_path, src, i,
2846 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002847 if (ret) {
2848 err = ret;
2849 goto done;
2850 }
Chris Masone02119d2008-09-05 16:13:11 -04002851 }
2852 path->slots[0] = nritems;
2853
2854 /*
2855 * look ahead to the next item and see if it is also
2856 * from this directory and from this transaction
2857 */
2858 ret = btrfs_next_leaf(root, path);
2859 if (ret == 1) {
2860 last_offset = (u64)-1;
2861 goto done;
2862 }
2863 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002864 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002865 last_offset = (u64)-1;
2866 goto done;
2867 }
2868 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2869 ret = overwrite_item(trans, log, dst_path,
2870 path->nodes[0], path->slots[0],
2871 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002872 if (ret)
2873 err = ret;
2874 else
2875 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002876 goto done;
2877 }
2878 }
2879done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002880 btrfs_release_path(path);
2881 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002882
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002883 if (err == 0) {
2884 *last_offset_ret = last_offset;
2885 /*
2886 * insert the log range keys to indicate where the log
2887 * is valid
2888 */
2889 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002890 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002891 if (ret)
2892 err = ret;
2893 }
2894 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002895}
2896
2897/*
2898 * logging directories is very similar to logging inodes, We find all the items
2899 * from the current transaction and write them to the log.
2900 *
2901 * The recovery code scans the directory in the subvolume, and if it finds a
2902 * key in the range logged that is not present in the log tree, then it means
2903 * that dir entry was unlinked during the transaction.
2904 *
2905 * In order for that scan to work, we must include one key smaller than
2906 * the smallest logged by this transaction and one key larger than the largest
2907 * key logged by this transaction.
2908 */
2909static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2910 struct btrfs_root *root, struct inode *inode,
2911 struct btrfs_path *path,
2912 struct btrfs_path *dst_path)
2913{
2914 u64 min_key;
2915 u64 max_key;
2916 int ret;
2917 int key_type = BTRFS_DIR_ITEM_KEY;
2918
2919again:
2920 min_key = 0;
2921 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002922 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002923 ret = log_dir_items(trans, root, inode, path,
2924 dst_path, key_type, min_key,
2925 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002926 if (ret)
2927 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002928 if (max_key == (u64)-1)
2929 break;
2930 min_key = max_key + 1;
2931 }
2932
2933 if (key_type == BTRFS_DIR_ITEM_KEY) {
2934 key_type = BTRFS_DIR_INDEX_KEY;
2935 goto again;
2936 }
2937 return 0;
2938}
2939
2940/*
2941 * a helper function to drop items from the log before we relog an
2942 * inode. max_key_type indicates the highest item type to remove.
2943 * This cannot be run for file data extents because it does not
2944 * free the extents they point to.
2945 */
2946static int drop_objectid_items(struct btrfs_trans_handle *trans,
2947 struct btrfs_root *log,
2948 struct btrfs_path *path,
2949 u64 objectid, int max_key_type)
2950{
2951 int ret;
2952 struct btrfs_key key;
2953 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04002954 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002955
2956 key.objectid = objectid;
2957 key.type = max_key_type;
2958 key.offset = (u64)-1;
2959
Chris Masond3977122009-01-05 21:25:51 -05002960 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002961 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002962 BUG_ON(ret == 0);
2963 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002964 break;
2965
2966 if (path->slots[0] == 0)
2967 break;
2968
2969 path->slots[0]--;
2970 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2971 path->slots[0]);
2972
2973 if (found_key.objectid != objectid)
2974 break;
2975
Josef Bacik18ec90d2012-09-28 11:56:28 -04002976 found_key.offset = 0;
2977 found_key.type = 0;
2978 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
2979 &start_slot);
2980
2981 ret = btrfs_del_items(trans, log, path, start_slot,
2982 path->slots[0] - start_slot + 1);
2983 /*
2984 * If start slot isn't 0 then we don't need to re-search, we've
2985 * found the last guy with the objectid in this tree.
2986 */
2987 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002988 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002989 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002990 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002991 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04002992 if (ret > 0)
2993 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002994 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002995}
2996
Josef Bacik94edf4a2012-09-25 14:56:25 -04002997static void fill_inode_item(struct btrfs_trans_handle *trans,
2998 struct extent_buffer *leaf,
2999 struct btrfs_inode_item *item,
3000 struct inode *inode, int log_inode_only)
3001{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003002 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003003
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003004 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003005
3006 if (log_inode_only) {
3007 /* set the generation to zero so the recover code
3008 * can tell the difference between an logging
3009 * just to say 'this inode exists' and a logging
3010 * to say 'update this inode with these values'
3011 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003012 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3013 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003014 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003015 btrfs_set_token_inode_generation(leaf, item,
3016 BTRFS_I(inode)->generation,
3017 &token);
3018 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003019 }
3020
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003021 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3022 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3023 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3024 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3025
3026 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3027 inode->i_atime.tv_sec, &token);
3028 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3029 inode->i_atime.tv_nsec, &token);
3030
3031 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3032 inode->i_mtime.tv_sec, &token);
3033 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3034 inode->i_mtime.tv_nsec, &token);
3035
3036 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3037 inode->i_ctime.tv_sec, &token);
3038 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3039 inode->i_ctime.tv_nsec, &token);
3040
3041 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3042 &token);
3043
3044 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3045 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3046 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3047 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3048 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003049}
3050
Josef Bacika95249b2012-10-11 16:17:34 -04003051static int log_inode_item(struct btrfs_trans_handle *trans,
3052 struct btrfs_root *log, struct btrfs_path *path,
3053 struct inode *inode)
3054{
3055 struct btrfs_inode_item *inode_item;
3056 struct btrfs_key key;
3057 int ret;
3058
3059 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3060 ret = btrfs_insert_empty_item(trans, log, path, &key,
3061 sizeof(*inode_item));
3062 if (ret && ret != -EEXIST)
3063 return ret;
3064 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3065 struct btrfs_inode_item);
3066 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3067 btrfs_release_path(path);
3068 return 0;
3069}
3070
Chris Mason31ff1cd2008-09-11 16:17:57 -04003071static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003072 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003073 struct btrfs_path *dst_path,
3074 struct extent_buffer *src,
3075 int start_slot, int nr, int inode_only)
3076{
3077 unsigned long src_offset;
3078 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003079 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003080 struct btrfs_file_extent_item *extent;
3081 struct btrfs_inode_item *inode_item;
3082 int ret;
3083 struct btrfs_key *ins_keys;
3084 u32 *ins_sizes;
3085 char *ins_data;
3086 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003087 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003088 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003089
3090 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003091
3092 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3093 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003094 if (!ins_data)
3095 return -ENOMEM;
3096
Chris Mason31ff1cd2008-09-11 16:17:57 -04003097 ins_sizes = (u32 *)ins_data;
3098 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3099
3100 for (i = 0; i < nr; i++) {
3101 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3102 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3103 }
3104 ret = btrfs_insert_empty_items(trans, log, dst_path,
3105 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003106 if (ret) {
3107 kfree(ins_data);
3108 return ret;
3109 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003112 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3113 dst_path->slots[0]);
3114
3115 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3116
Josef Bacik94edf4a2012-09-25 14:56:25 -04003117 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003118 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3119 dst_path->slots[0],
3120 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003121 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3122 inode, inode_only == LOG_INODE_EXISTS);
3123 } else {
3124 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3125 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003126 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003127
Chris Mason31ff1cd2008-09-11 16:17:57 -04003128 /* take a reference on file data extents so that truncates
3129 * or deletes of this inode don't have to relog the inode
3130 * again
3131 */
Liu Bod2794402012-08-29 01:07:56 -06003132 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3133 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003134 int found_type;
3135 extent = btrfs_item_ptr(src, start_slot + i,
3136 struct btrfs_file_extent_item);
3137
liubo8e531cd2011-05-06 10:36:09 +08003138 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3139 continue;
3140
Chris Mason31ff1cd2008-09-11 16:17:57 -04003141 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003142 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003143 u64 ds, dl, cs, cl;
3144 ds = btrfs_file_extent_disk_bytenr(src,
3145 extent);
3146 /* ds == 0 is a hole */
3147 if (ds == 0)
3148 continue;
3149
3150 dl = btrfs_file_extent_disk_num_bytes(src,
3151 extent);
3152 cs = btrfs_file_extent_offset(src, extent);
3153 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003154 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003155 if (btrfs_file_extent_compression(src,
3156 extent)) {
3157 cs = 0;
3158 cl = dl;
3159 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003160
3161 ret = btrfs_lookup_csums_range(
3162 log->fs_info->csum_root,
3163 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003164 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003166 }
3167 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003168 }
3169
3170 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003171 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003172 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003173
3174 /*
3175 * we have to do this after the loop above to avoid changing the
3176 * log tree while trying to change the log tree.
3177 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003178 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003179 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003180 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3181 struct btrfs_ordered_sum,
3182 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183 if (!ret)
3184 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003185 list_del(&sums->list);
3186 kfree(sums);
3187 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003188 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003189}
3190
Josef Bacik5dc562c2012-08-17 13:14:17 -04003191static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3192{
3193 struct extent_map *em1, *em2;
3194
3195 em1 = list_entry(a, struct extent_map, list);
3196 em2 = list_entry(b, struct extent_map, list);
3197
3198 if (em1->start < em2->start)
3199 return -1;
3200 else if (em1->start > em2->start)
3201 return 1;
3202 return 0;
3203}
3204
Josef Bacik5dc562c2012-08-17 13:14:17 -04003205static int log_one_extent(struct btrfs_trans_handle *trans,
3206 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003207 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003208{
3209 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003210 struct btrfs_file_extent_item *fi;
3211 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003212 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003213 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003214 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003215 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003216 u64 mod_start = em->mod_start;
3217 u64 mod_len = em->mod_len;
3218 u64 csum_offset;
3219 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003220 u64 extent_offset = em->start - em->orig_start;
3221 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003222 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003223 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003224 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003225
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003226 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3227 em->start + em->len, NULL, 0);
3228 if (ret)
3229 return ret;
3230
Josef Bacik70c8a912012-10-11 16:54:30 -04003231 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003232 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003233 key.objectid = btrfs_ino(inode);
3234 key.type = BTRFS_EXTENT_DATA_KEY;
3235 key.offset = em->start;
Josef Bacik70c8a912012-10-11 16:54:30 -04003236
3237 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003238 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003239 return ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003240 leaf = path->nodes[0];
3241 fi = btrfs_item_ptr(leaf, path->slots[0],
3242 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003243
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003244 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3245 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003246 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3247 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003248 btrfs_set_token_file_extent_type(leaf, fi,
3249 BTRFS_FILE_EXTENT_PREALLOC,
3250 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003251 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003252 btrfs_set_token_file_extent_type(leaf, fi,
3253 BTRFS_FILE_EXTENT_REG,
3254 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003255 if (em->block_start == 0)
3256 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003257 }
3258
Josef Bacik70c8a912012-10-11 16:54:30 -04003259 block_len = max(em->block_len, em->orig_block_len);
3260 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003261 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3262 em->block_start,
3263 &token);
3264 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3265 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003266 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003267 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3268 em->block_start -
3269 extent_offset, &token);
3270 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3271 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003272 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003273 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3274 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3275 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003276 }
3277
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003278 btrfs_set_token_file_extent_offset(leaf, fi,
3279 em->start - em->orig_start,
3280 &token);
3281 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003282 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003283 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3284 &token);
3285 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3286 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003287 btrfs_mark_buffer_dirty(leaf);
3288
Josef Bacik70c8a912012-10-11 16:54:30 -04003289 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003290 if (ret) {
3291 return ret;
3292 }
3293
3294 if (skip_csum)
3295 return 0;
3296
Liu Bo192000d2013-01-06 03:38:22 +00003297 if (em->compress_type) {
3298 csum_offset = 0;
3299 csum_len = block_len;
3300 }
3301
Josef Bacik2ab28f32012-10-12 15:27:49 -04003302 /*
3303 * First check and see if our csums are on our outstanding ordered
3304 * extents.
3305 */
3306again:
3307 spin_lock_irq(&log->log_extents_lock[index]);
3308 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3309 struct btrfs_ordered_sum *sum;
3310
3311 if (!mod_len)
3312 break;
3313
3314 if (ordered->inode != inode)
3315 continue;
3316
3317 if (ordered->file_offset + ordered->len <= mod_start ||
3318 mod_start + mod_len <= ordered->file_offset)
3319 continue;
3320
3321 /*
3322 * We are going to copy all the csums on this ordered extent, so
3323 * go ahead and adjust mod_start and mod_len in case this
3324 * ordered extent has already been logged.
3325 */
3326 if (ordered->file_offset > mod_start) {
3327 if (ordered->file_offset + ordered->len >=
3328 mod_start + mod_len)
3329 mod_len = ordered->file_offset - mod_start;
3330 /*
3331 * If we have this case
3332 *
3333 * |--------- logged extent ---------|
3334 * |----- ordered extent ----|
3335 *
3336 * Just don't mess with mod_start and mod_len, we'll
3337 * just end up logging more csums than we need and it
3338 * will be ok.
3339 */
3340 } else {
3341 if (ordered->file_offset + ordered->len <
3342 mod_start + mod_len) {
3343 mod_len = (mod_start + mod_len) -
3344 (ordered->file_offset + ordered->len);
3345 mod_start = ordered->file_offset +
3346 ordered->len;
3347 } else {
3348 mod_len = 0;
3349 }
3350 }
3351
3352 /*
3353 * To keep us from looping for the above case of an ordered
3354 * extent that falls inside of the logged extent.
3355 */
3356 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3357 &ordered->flags))
3358 continue;
3359 atomic_inc(&ordered->refs);
3360 spin_unlock_irq(&log->log_extents_lock[index]);
3361 /*
3362 * we've dropped the lock, we must either break or
3363 * start over after this.
3364 */
3365
3366 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3367
3368 list_for_each_entry(sum, &ordered->list, list) {
3369 ret = btrfs_csum_file_blocks(trans, log, sum);
3370 if (ret) {
3371 btrfs_put_ordered_extent(ordered);
3372 goto unlocked;
3373 }
3374 }
3375 btrfs_put_ordered_extent(ordered);
3376 goto again;
3377
3378 }
3379 spin_unlock_irq(&log->log_extents_lock[index]);
3380unlocked:
3381
3382 if (!mod_len || ret)
3383 return ret;
3384
3385 csum_offset = mod_start - em->start;
3386 csum_len = mod_len;
3387
Josef Bacik70c8a912012-10-11 16:54:30 -04003388 /* block start is already adjusted for the file extent offset. */
3389 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3390 em->block_start + csum_offset,
3391 em->block_start + csum_offset +
3392 csum_len - 1, &ordered_sums, 0);
3393 if (ret)
3394 return ret;
3395
3396 while (!list_empty(&ordered_sums)) {
3397 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3398 struct btrfs_ordered_sum,
3399 list);
3400 if (!ret)
3401 ret = btrfs_csum_file_blocks(trans, log, sums);
3402 list_del(&sums->list);
3403 kfree(sums);
3404 }
3405
3406 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003407}
3408
3409static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3410 struct btrfs_root *root,
3411 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003412 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003413{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003414 struct extent_map *em, *n;
3415 struct list_head extents;
3416 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3417 u64 test_gen;
3418 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003419 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003420
3421 INIT_LIST_HEAD(&extents);
3422
Josef Bacik5dc562c2012-08-17 13:14:17 -04003423 write_lock(&tree->lock);
3424 test_gen = root->fs_info->last_trans_committed;
3425
3426 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3427 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003428
3429 /*
3430 * Just an arbitrary number, this can be really CPU intensive
3431 * once we start getting a lot of extents, and really once we
3432 * have a bunch of extents we just want to commit since it will
3433 * be faster.
3434 */
3435 if (++num > 32768) {
3436 list_del_init(&tree->modified_extents);
3437 ret = -EFBIG;
3438 goto process;
3439 }
3440
Josef Bacik5dc562c2012-08-17 13:14:17 -04003441 if (em->generation <= test_gen)
3442 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003443 /* Need a ref to keep it from getting evicted from cache */
3444 atomic_inc(&em->refs);
3445 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003446 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003447 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003448 }
3449
3450 list_sort(NULL, &extents, extent_cmp);
3451
Josef Bacik2ab28f32012-10-12 15:27:49 -04003452process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003453 while (!list_empty(&extents)) {
3454 em = list_entry(extents.next, struct extent_map, list);
3455
3456 list_del_init(&em->list);
3457
3458 /*
3459 * If we had an error we just need to delete everybody from our
3460 * private list.
3461 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003462 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003463 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003464 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003465 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003466 }
3467
3468 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003469
Josef Bacik70c8a912012-10-11 16:54:30 -04003470 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003471 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003472 clear_em_logging(tree, em);
3473 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003474 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003475 WARN_ON(!list_empty(&extents));
3476 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003477
Josef Bacik5dc562c2012-08-17 13:14:17 -04003478 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003479 return ret;
3480}
3481
Chris Masone02119d2008-09-05 16:13:11 -04003482/* log a single inode in the tree log.
3483 * At least one parent directory for this inode must exist in the tree
3484 * or be logged already.
3485 *
3486 * Any items from this inode changed by the current transaction are copied
3487 * to the log tree. An extra reference is taken on any extents in this
3488 * file, allowing us to avoid a whole pile of corner cases around logging
3489 * blocks that have been removed from the tree.
3490 *
3491 * See LOG_INODE_ALL and related defines for a description of what inode_only
3492 * does.
3493 *
3494 * This handles both files and directories.
3495 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003496static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003497 struct btrfs_root *root, struct inode *inode,
3498 int inode_only)
3499{
3500 struct btrfs_path *path;
3501 struct btrfs_path *dst_path;
3502 struct btrfs_key min_key;
3503 struct btrfs_key max_key;
3504 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003505 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003506 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003507 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003508 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003509 int ins_start_slot = 0;
3510 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003511 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003512 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003513
Chris Masone02119d2008-09-05 16:13:11 -04003514 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003515 if (!path)
3516 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003517 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003518 if (!dst_path) {
3519 btrfs_free_path(path);
3520 return -ENOMEM;
3521 }
Chris Masone02119d2008-09-05 16:13:11 -04003522
Li Zefan33345d012011-04-20 10:31:50 +08003523 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003524 min_key.type = BTRFS_INODE_ITEM_KEY;
3525 min_key.offset = 0;
3526
Li Zefan33345d012011-04-20 10:31:50 +08003527 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003528
Chris Mason12fcfd22009-03-24 10:24:20 -04003529
Josef Bacik5dc562c2012-08-17 13:14:17 -04003530 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003531 if (S_ISDIR(inode->i_mode) ||
3532 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3533 &BTRFS_I(inode)->runtime_flags) &&
3534 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003535 max_key.type = BTRFS_XATTR_ITEM_KEY;
3536 else
3537 max_key.type = (u8)-1;
3538 max_key.offset = (u64)-1;
3539
Josef Bacik94edf4a2012-09-25 14:56:25 -04003540 /* Only run delayed items if we are a dir or a new file */
3541 if (S_ISDIR(inode->i_mode) ||
3542 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3543 ret = btrfs_commit_inode_delayed_items(trans, inode);
3544 if (ret) {
3545 btrfs_free_path(path);
3546 btrfs_free_path(dst_path);
3547 return ret;
3548 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003549 }
3550
Chris Masone02119d2008-09-05 16:13:11 -04003551 mutex_lock(&BTRFS_I(inode)->log_mutex);
3552
Josef Bacik2ab28f32012-10-12 15:27:49 -04003553 btrfs_get_logged_extents(log, inode);
3554
Chris Masone02119d2008-09-05 16:13:11 -04003555 /*
3556 * a brute force approach to making sure we get the most uptodate
3557 * copies of everything.
3558 */
3559 if (S_ISDIR(inode->i_mode)) {
3560 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3561
3562 if (inode_only == LOG_INODE_EXISTS)
3563 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003564 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003565 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003566 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3567 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003568 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3569 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003570 ret = btrfs_truncate_inode_items(trans, log,
3571 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003572 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3573 &BTRFS_I(inode)->runtime_flags)) {
3574 if (inode_only == LOG_INODE_ALL)
3575 fast_search = true;
3576 max_key.type = BTRFS_XATTR_ITEM_KEY;
3577 ret = drop_objectid_items(trans, log, path, ino,
3578 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003579 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003580 if (inode_only == LOG_INODE_ALL)
3581 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003582 ret = log_inode_item(trans, log, dst_path, inode);
3583 if (ret) {
3584 err = ret;
3585 goto out_unlock;
3586 }
3587 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003588 }
Josef Bacika95249b2012-10-11 16:17:34 -04003589
Chris Masone02119d2008-09-05 16:13:11 -04003590 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003591 if (ret) {
3592 err = ret;
3593 goto out_unlock;
3594 }
Chris Masone02119d2008-09-05 16:13:11 -04003595 path->keep_locks = 1;
3596
Chris Masond3977122009-01-05 21:25:51 -05003597 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003598 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003599 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003600 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003601 if (ret != 0)
3602 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003603again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003604 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003605 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003606 break;
3607 if (min_key.type > max_key.type)
3608 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003609
Chris Masone02119d2008-09-05 16:13:11 -04003610 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003611 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3612 ins_nr++;
3613 goto next_slot;
3614 } else if (!ins_nr) {
3615 ins_start_slot = path->slots[0];
3616 ins_nr = 1;
3617 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003618 }
3619
Liu Bod2794402012-08-29 01:07:56 -06003620 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003621 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003622 if (ret) {
3623 err = ret;
3624 goto out_unlock;
3625 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003626 ins_nr = 1;
3627 ins_start_slot = path->slots[0];
3628next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003629
Chris Mason3a5f1d42008-09-11 15:53:37 -04003630 nritems = btrfs_header_nritems(path->nodes[0]);
3631 path->slots[0]++;
3632 if (path->slots[0] < nritems) {
3633 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3634 path->slots[0]);
3635 goto again;
3636 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003637 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003638 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003639 ins_start_slot,
3640 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003641 if (ret) {
3642 err = ret;
3643 goto out_unlock;
3644 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003645 ins_nr = 0;
3646 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003647 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003648
Chris Masone02119d2008-09-05 16:13:11 -04003649 if (min_key.offset < (u64)-1)
3650 min_key.offset++;
3651 else if (min_key.type < (u8)-1)
3652 min_key.type++;
3653 else if (min_key.objectid < (u64)-1)
3654 min_key.objectid++;
3655 else
3656 break;
3657 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003658 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003659 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003660 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003661 if (ret) {
3662 err = ret;
3663 goto out_unlock;
3664 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003665 ins_nr = 0;
3666 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003667
Josef Bacika95249b2012-10-11 16:17:34 -04003668log_extents:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003669 if (fast_search) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003670 btrfs_release_path(dst_path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003671 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003672 if (ret) {
3673 err = ret;
3674 goto out_unlock;
3675 }
Liu Bo06d3d222012-08-27 10:52:19 -06003676 } else {
3677 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3678 struct extent_map *em, *n;
3679
Miao Xiebbe14262012-11-01 07:34:54 +00003680 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003681 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3682 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003683 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003684 }
3685
Chris Mason9623f9a2008-09-11 17:42:42 -04003686 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003687 btrfs_release_path(path);
3688 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003689 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003690 if (ret) {
3691 err = ret;
3692 goto out_unlock;
3693 }
Chris Masone02119d2008-09-05 16:13:11 -04003694 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003695 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003696 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003697out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003698 if (err)
3699 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003700 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3701
3702 btrfs_free_path(path);
3703 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003704 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003705}
3706
Chris Mason12fcfd22009-03-24 10:24:20 -04003707/*
3708 * follow the dentry parent pointers up the chain and see if any
3709 * of the directories in it require a full commit before they can
3710 * be logged. Returns zero if nothing special needs to be done or 1 if
3711 * a full commit is required.
3712 */
3713static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3714 struct inode *inode,
3715 struct dentry *parent,
3716 struct super_block *sb,
3717 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003718{
Chris Mason12fcfd22009-03-24 10:24:20 -04003719 int ret = 0;
3720 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003721 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003722
Chris Masonaf4176b2009-03-24 10:24:31 -04003723 /*
3724 * for regular files, if its inode is already on disk, we don't
3725 * have to worry about the parents at all. This is because
3726 * we can use the last_unlink_trans field to record renames
3727 * and other fun in this file.
3728 */
3729 if (S_ISREG(inode->i_mode) &&
3730 BTRFS_I(inode)->generation <= last_committed &&
3731 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3732 goto out;
3733
Chris Mason12fcfd22009-03-24 10:24:20 -04003734 if (!S_ISDIR(inode->i_mode)) {
3735 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3736 goto out;
3737 inode = parent->d_inode;
3738 }
3739
3740 while (1) {
3741 BTRFS_I(inode)->logged_trans = trans->transid;
3742 smp_mb();
3743
3744 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3745 root = BTRFS_I(inode)->root;
3746
3747 /*
3748 * make sure any commits to the log are forced
3749 * to be full commits
3750 */
3751 root->fs_info->last_trans_log_full_commit =
3752 trans->transid;
3753 ret = 1;
3754 break;
3755 }
3756
3757 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3758 break;
3759
Yan, Zheng76dda932009-09-21 16:00:26 -04003760 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003761 break;
3762
Josef Bacik6a912212010-11-20 09:48:00 +00003763 parent = dget_parent(parent);
3764 dput(old_parent);
3765 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003766 inode = parent->d_inode;
3767
3768 }
Josef Bacik6a912212010-11-20 09:48:00 +00003769 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003770out:
Chris Masone02119d2008-09-05 16:13:11 -04003771 return ret;
3772}
3773
3774/*
3775 * helper function around btrfs_log_inode to make sure newly created
3776 * parent directories also end up in the log. A minimal inode and backref
3777 * only logging is done of any parent directories that are older than
3778 * the last committed transaction
3779 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003780int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root, struct inode *inode,
3782 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003783{
Chris Mason12fcfd22009-03-24 10:24:20 -04003784 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003785 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003786 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003787 int ret = 0;
3788 u64 last_committed = root->fs_info->last_trans_committed;
3789
3790 sb = inode->i_sb;
3791
Sage Weil3a5e1402009-04-02 16:49:40 -04003792 if (btrfs_test_opt(root, NOTREELOG)) {
3793 ret = 1;
3794 goto end_no_trans;
3795 }
3796
Chris Mason12fcfd22009-03-24 10:24:20 -04003797 if (root->fs_info->last_trans_log_full_commit >
3798 root->fs_info->last_trans_committed) {
3799 ret = 1;
3800 goto end_no_trans;
3801 }
3802
Yan, Zheng76dda932009-09-21 16:00:26 -04003803 if (root != BTRFS_I(inode)->root ||
3804 btrfs_root_refs(&root->root_item) == 0) {
3805 ret = 1;
3806 goto end_no_trans;
3807 }
3808
Chris Mason12fcfd22009-03-24 10:24:20 -04003809 ret = check_parent_dirs_for_sync(trans, inode, parent,
3810 sb, last_committed);
3811 if (ret)
3812 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003813
Josef Bacik22ee6982012-05-29 16:57:49 -04003814 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003815 ret = BTRFS_NO_LOG_SYNC;
3816 goto end_no_trans;
3817 }
3818
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003819 ret = start_log_trans(trans, root);
3820 if (ret)
3821 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003822
3823 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003824 if (ret)
3825 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003826
Chris Masonaf4176b2009-03-24 10:24:31 -04003827 /*
3828 * for regular files, if its inode is already on disk, we don't
3829 * have to worry about the parents at all. This is because
3830 * we can use the last_unlink_trans field to record renames
3831 * and other fun in this file.
3832 */
3833 if (S_ISREG(inode->i_mode) &&
3834 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003835 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3836 ret = 0;
3837 goto end_trans;
3838 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003839
3840 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003841 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003842 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003843 break;
3844
Chris Mason12fcfd22009-03-24 10:24:20 -04003845 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003846 if (root != BTRFS_I(inode)->root)
3847 break;
3848
Chris Mason12fcfd22009-03-24 10:24:20 -04003849 if (BTRFS_I(inode)->generation >
3850 root->fs_info->last_trans_committed) {
3851 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003852 if (ret)
3853 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003854 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003855 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003856 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003857
Josef Bacik6a912212010-11-20 09:48:00 +00003858 parent = dget_parent(parent);
3859 dput(old_parent);
3860 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003861 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003862 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003863end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003864 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003865 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003866 root->fs_info->last_trans_log_full_commit = trans->transid;
3867 ret = 1;
3868 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003869 btrfs_end_log_trans(root);
3870end_no_trans:
3871 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003872}
3873
3874/*
3875 * it is not safe to log dentry if the chunk root has added new
3876 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3877 * If this returns 1, you must commit the transaction to safely get your
3878 * data on disk.
3879 */
3880int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *root, struct dentry *dentry)
3882{
Josef Bacik6a912212010-11-20 09:48:00 +00003883 struct dentry *parent = dget_parent(dentry);
3884 int ret;
3885
3886 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3887 dput(parent);
3888
3889 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003890}
3891
3892/*
3893 * should be called during mount to recover any replay any log trees
3894 * from the FS
3895 */
3896int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3897{
3898 int ret;
3899 struct btrfs_path *path;
3900 struct btrfs_trans_handle *trans;
3901 struct btrfs_key key;
3902 struct btrfs_key found_key;
3903 struct btrfs_key tmp_key;
3904 struct btrfs_root *log;
3905 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3906 struct walk_control wc = {
3907 .process_func = process_one_buffer,
3908 .stage = 0,
3909 };
3910
Chris Masone02119d2008-09-05 16:13:11 -04003911 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003912 if (!path)
3913 return -ENOMEM;
3914
3915 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003916
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003917 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003918 if (IS_ERR(trans)) {
3919 ret = PTR_ERR(trans);
3920 goto error;
3921 }
Chris Masone02119d2008-09-05 16:13:11 -04003922
3923 wc.trans = trans;
3924 wc.pin = 1;
3925
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003926 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003927 if (ret) {
3928 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3929 "recovering log root tree.");
3930 goto error;
3931 }
Chris Masone02119d2008-09-05 16:13:11 -04003932
3933again:
3934 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3935 key.offset = (u64)-1;
3936 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3937
Chris Masond3977122009-01-05 21:25:51 -05003938 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003939 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940
3941 if (ret < 0) {
3942 btrfs_error(fs_info, ret,
3943 "Couldn't find tree log root.");
3944 goto error;
3945 }
Chris Masone02119d2008-09-05 16:13:11 -04003946 if (ret > 0) {
3947 if (path->slots[0] == 0)
3948 break;
3949 path->slots[0]--;
3950 }
3951 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3952 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003953 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003954 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3955 break;
3956
3957 log = btrfs_read_fs_root_no_radix(log_root_tree,
3958 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003959 if (IS_ERR(log)) {
3960 ret = PTR_ERR(log);
3961 btrfs_error(fs_info, ret,
3962 "Couldn't read tree log root.");
3963 goto error;
3964 }
Chris Masone02119d2008-09-05 16:13:11 -04003965
3966 tmp_key.objectid = found_key.offset;
3967 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3968 tmp_key.offset = (u64)-1;
3969
3970 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 if (IS_ERR(wc.replay_dest)) {
3972 ret = PTR_ERR(wc.replay_dest);
3973 btrfs_error(fs_info, ret, "Couldn't read target root "
3974 "for tree log recovery.");
3975 goto error;
3976 }
Chris Masone02119d2008-09-05 16:13:11 -04003977
Yan Zheng07d400a2009-01-06 11:42:00 -05003978 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003979 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003980 ret = walk_log_tree(trans, log, &wc);
3981 BUG_ON(ret);
3982
3983 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3984 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3985 path);
3986 BUG_ON(ret);
3987 }
Chris Masone02119d2008-09-05 16:13:11 -04003988
3989 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003990 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003991 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003992 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003993 kfree(log);
3994
3995 if (found_key.offset == 0)
3996 break;
3997 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003998 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003999
4000 /* step one is to pin it all, step two is to replay just inodes */
4001 if (wc.pin) {
4002 wc.pin = 0;
4003 wc.process_func = replay_one_buffer;
4004 wc.stage = LOG_WALK_REPLAY_INODES;
4005 goto again;
4006 }
4007 /* step three is to replay everything */
4008 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4009 wc.stage++;
4010 goto again;
4011 }
4012
4013 btrfs_free_path(path);
4014
Josef Bacikabefa552013-04-24 16:40:05 -04004015 /* step 4: commit the transaction, which also unpins the blocks */
4016 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4017 if (ret)
4018 return ret;
4019
Chris Masone02119d2008-09-05 16:13:11 -04004020 free_extent_buffer(log_root_tree->node);
4021 log_root_tree->log_root = NULL;
4022 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004023 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004024
Josef Bacikabefa552013-04-24 16:40:05 -04004025 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004026error:
4027 btrfs_free_path(path);
4028 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004029}
Chris Mason12fcfd22009-03-24 10:24:20 -04004030
4031/*
4032 * there are some corner cases where we want to force a full
4033 * commit instead of allowing a directory to be logged.
4034 *
4035 * They revolve around files there were unlinked from the directory, and
4036 * this function updates the parent directory so that a full commit is
4037 * properly done if it is fsync'd later after the unlinks are done.
4038 */
4039void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4040 struct inode *dir, struct inode *inode,
4041 int for_rename)
4042{
4043 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004044 * when we're logging a file, if it hasn't been renamed
4045 * or unlinked, and its inode is fully committed on disk,
4046 * we don't have to worry about walking up the directory chain
4047 * to log its parents.
4048 *
4049 * So, we use the last_unlink_trans field to put this transid
4050 * into the file. When the file is logged we check it and
4051 * don't log the parents if the file is fully on disk.
4052 */
4053 if (S_ISREG(inode->i_mode))
4054 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4055
4056 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004057 * if this directory was already logged any new
4058 * names for this file/dir will get recorded
4059 */
4060 smp_mb();
4061 if (BTRFS_I(dir)->logged_trans == trans->transid)
4062 return;
4063
4064 /*
4065 * if the inode we're about to unlink was logged,
4066 * the log will be properly updated for any new names
4067 */
4068 if (BTRFS_I(inode)->logged_trans == trans->transid)
4069 return;
4070
4071 /*
4072 * when renaming files across directories, if the directory
4073 * there we're unlinking from gets fsync'd later on, there's
4074 * no way to find the destination directory later and fsync it
4075 * properly. So, we have to be conservative and force commits
4076 * so the new name gets discovered.
4077 */
4078 if (for_rename)
4079 goto record;
4080
4081 /* we can safely do the unlink without any special recording */
4082 return;
4083
4084record:
4085 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4086}
4087
4088/*
4089 * Call this after adding a new name for a file and it will properly
4090 * update the log to reflect the new name.
4091 *
4092 * It will return zero if all goes well, and it will return 1 if a
4093 * full transaction commit is required.
4094 */
4095int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4096 struct inode *inode, struct inode *old_dir,
4097 struct dentry *parent)
4098{
4099 struct btrfs_root * root = BTRFS_I(inode)->root;
4100
4101 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004102 * this will force the logging code to walk the dentry chain
4103 * up for the file
4104 */
4105 if (S_ISREG(inode->i_mode))
4106 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4107
4108 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004109 * if this inode hasn't been logged and directory we're renaming it
4110 * from hasn't been logged, we don't need to log it
4111 */
4112 if (BTRFS_I(inode)->logged_trans <=
4113 root->fs_info->last_trans_committed &&
4114 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4115 root->fs_info->last_trans_committed))
4116 return 0;
4117
4118 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4119}
4120