blob: ff60d8978ae264d95709d83342e1c309113521f0 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Chris Masone02119d2008-09-05 16:13:11 -040023#include "ctree.h"
24#include "transaction.h"
25#include "disk-io.h"
26#include "locking.h"
27#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "backref.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050030#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070031#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040032
33/* magic values for the inode_only field in btrfs_log_inode:
34 *
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
37 * during log replay
38 */
39#define LOG_INODE_ALL 0
40#define LOG_INODE_EXISTS 1
41
42/*
Chris Mason12fcfd22009-03-24 10:24:20 -040043 * directory trouble cases
44 *
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
49 *
50 * mkdir foo/some_dir
51 * normal commit
52 * rename foo/some_dir foo2/some_dir
53 * mkdir foo/some_dir
54 * fsync foo/some_dir/some_file
55 *
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
59 *
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
62 *
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
66 *
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
69 *
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
72 *
73 * mkdir f1/foo
74 * normal commit
75 * rm -rf f1/foo
76 * fsync(f1)
77 *
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
82 * ugly details.
83 */
84
85/*
Chris Masone02119d2008-09-05 16:13:11 -040086 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
90 *
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
93 */
94#define LOG_WALK_PIN_ONLY 0
95#define LOG_WALK_REPLAY_INODES 1
96#define LOG_WALK_REPLAY_ALL 2
97
Chris Mason12fcfd22009-03-24 10:24:20 -040098static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040099 struct btrfs_root *root, struct inode *inode,
100 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400104static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_root *log,
107 struct btrfs_path *path,
108 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root)
140{
141 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400142 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500143
144 mutex_lock(&root->log_mutex);
145 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
147 root->log_start_pid = current->pid;
148 root->log_multiple_pids = false;
149 } else if (root->log_start_pid != current->pid) {
150 root->log_multiple_pids = true;
151 }
152
Miao Xie2ecb7922012-09-06 04:04:27 -0600153 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500154 atomic_inc(&root->log_writers);
155 mutex_unlock(&root->log_mutex);
156 return 0;
157 }
Josef Bacikff782e02009-10-08 15:30:04 -0400158 root->log_multiple_pids = false;
159 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400160 mutex_lock(&root->fs_info->tree_log_mutex);
161 if (!root->fs_info->log_root_tree) {
162 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400163 if (ret)
164 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400165 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400166 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400167 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400168 if (ret)
169 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400170 }
Chris Masone02119d2008-09-05 16:13:11 -0400171 mutex_unlock(&root->fs_info->tree_log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -0600172 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500173 atomic_inc(&root->log_writers);
174 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400175 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400176}
177
178/*
179 * returns 0 if there was a log transaction running and we were able
180 * to join, or returns -ENOENT if there were not transactions
181 * in progress
182 */
183static int join_running_log_trans(struct btrfs_root *root)
184{
185 int ret = -ENOENT;
186
187 smp_mb();
188 if (!root->log_root)
189 return -ENOENT;
190
Yan Zheng7237f182009-01-21 12:54:03 -0500191 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400192 if (root->log_root) {
193 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500194 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400195 }
Yan Zheng7237f182009-01-21 12:54:03 -0500196 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400197 return ret;
198}
199
200/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400201 * This either makes the current running log transaction wait
202 * until you call btrfs_end_log_trans() or it makes any future
203 * log transactions wait until you call btrfs_end_log_trans()
204 */
205int btrfs_pin_log_trans(struct btrfs_root *root)
206{
207 int ret = -ENOENT;
208
209 mutex_lock(&root->log_mutex);
210 atomic_inc(&root->log_writers);
211 mutex_unlock(&root->log_mutex);
212 return ret;
213}
214
215/*
Chris Masone02119d2008-09-05 16:13:11 -0400216 * indicate we're done making changes to the log tree
217 * and wake up anyone waiting to do a sync
218 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100219void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400220{
Yan Zheng7237f182009-01-21 12:54:03 -0500221 if (atomic_dec_and_test(&root->log_writers)) {
222 smp_mb();
223 if (waitqueue_active(&root->log_writer_wait))
224 wake_up(&root->log_writer_wait);
225 }
Chris Masone02119d2008-09-05 16:13:11 -0400226}
227
228
229/*
230 * the walk control struct is used to pass state down the chain when
231 * processing the log tree. The stage field tells us which part
232 * of the log tree processing we are currently doing. The others
233 * are state fields used for that specific part
234 */
235struct walk_control {
236 /* should we free the extent on disk when done? This is used
237 * at transaction commit time while freeing a log tree
238 */
239 int free;
240
241 /* should we write out the extent buffer? This is used
242 * while flushing the log tree to disk during a sync
243 */
244 int write;
245
246 /* should we wait for the extent buffer io to finish? Also used
247 * while flushing the log tree to disk for a sync
248 */
249 int wait;
250
251 /* pin only walk, we record which extents on disk belong to the
252 * log trees
253 */
254 int pin;
255
256 /* what stage of the replay code we're currently in */
257 int stage;
258
259 /* the root we are currently replaying */
260 struct btrfs_root *replay_dest;
261
262 /* the trans handle for the current replay */
263 struct btrfs_trans_handle *trans;
264
265 /* the function that gets used to process blocks we find in the
266 * tree. Note the extent_buffer might not be up to date when it is
267 * passed in, and it must be checked or read if you need the data
268 * inside it
269 */
270 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
271 struct walk_control *wc, u64 gen);
272};
273
274/*
275 * process_func used to pin down extents, write them or wait on them
276 */
277static int process_one_buffer(struct btrfs_root *log,
278 struct extent_buffer *eb,
279 struct walk_control *wc, u64 gen)
280{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400281 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400282
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400283 /*
284 * If this fs is mixed then we need to be able to process the leaves to
285 * pin down any logged extents, so we have to read the block.
286 */
287 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
288 ret = btrfs_read_buffer(eb, gen);
289 if (ret)
290 return ret;
291 }
292
Josef Bacikb50c6e22013-04-25 15:55:30 -0400293 if (wc->pin)
294 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
295 eb->start, eb->len);
296
297 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400298 if (wc->pin && btrfs_header_level(eb) == 0)
299 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400300 if (wc->write)
301 btrfs_write_tree_block(eb);
302 if (wc->wait)
303 btrfs_wait_tree_block_writeback(eb);
304 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400305 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400306}
307
308/*
309 * Item overwrite used by replay and tree logging. eb, slot and key all refer
310 * to the src data we are copying out.
311 *
312 * root is the tree we are copying into, and path is a scratch
313 * path for use in this function (it should be released on entry and
314 * will be released on exit).
315 *
316 * If the key is already in the destination tree the existing item is
317 * overwritten. If the existing item isn't big enough, it is extended.
318 * If it is too large, it is truncated.
319 *
320 * If the key isn't in the destination yet, a new item is inserted.
321 */
322static noinline int overwrite_item(struct btrfs_trans_handle *trans,
323 struct btrfs_root *root,
324 struct btrfs_path *path,
325 struct extent_buffer *eb, int slot,
326 struct btrfs_key *key)
327{
328 int ret;
329 u32 item_size;
330 u64 saved_i_size = 0;
331 int save_old_i_size = 0;
332 unsigned long src_ptr;
333 unsigned long dst_ptr;
334 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000335 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400336
337 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
338 overwrite_root = 1;
339
340 item_size = btrfs_item_size_nr(eb, slot);
341 src_ptr = btrfs_item_ptr_offset(eb, slot);
342
343 /* look for the key in the destination tree */
344 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000345 if (ret < 0)
346 return ret;
347
Chris Masone02119d2008-09-05 16:13:11 -0400348 if (ret == 0) {
349 char *src_copy;
350 char *dst_copy;
351 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
352 path->slots[0]);
353 if (dst_size != item_size)
354 goto insert;
355
356 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200357 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400358 return 0;
359 }
360 dst_copy = kmalloc(item_size, GFP_NOFS);
361 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000362 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200363 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000364 kfree(dst_copy);
365 kfree(src_copy);
366 return -ENOMEM;
367 }
Chris Masone02119d2008-09-05 16:13:11 -0400368
369 read_extent_buffer(eb, src_copy, src_ptr, item_size);
370
371 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
372 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
373 item_size);
374 ret = memcmp(dst_copy, src_copy, item_size);
375
376 kfree(dst_copy);
377 kfree(src_copy);
378 /*
379 * they have the same contents, just return, this saves
380 * us from cowing blocks in the destination tree and doing
381 * extra writes that may not have been done by a previous
382 * sync
383 */
384 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200385 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400386 return 0;
387 }
388
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000389 /*
390 * We need to load the old nbytes into the inode so when we
391 * replay the extents we've logged we get the right nbytes.
392 */
393 if (inode_item) {
394 struct btrfs_inode_item *item;
395 u64 nbytes;
396
397 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
398 struct btrfs_inode_item);
399 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
400 item = btrfs_item_ptr(eb, slot,
401 struct btrfs_inode_item);
402 btrfs_set_inode_nbytes(eb, item, nbytes);
403 }
404 } else if (inode_item) {
405 struct btrfs_inode_item *item;
406
407 /*
408 * New inode, set nbytes to 0 so that the nbytes comes out
409 * properly when we replay the extents.
410 */
411 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
412 btrfs_set_inode_nbytes(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400413 }
414insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200415 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400416 /* try to insert the key into the destination tree */
417 ret = btrfs_insert_empty_item(trans, root, path,
418 key, item_size);
419
420 /* make sure any existing item is the correct size */
421 if (ret == -EEXIST) {
422 u32 found_size;
423 found_size = btrfs_item_size_nr(path->nodes[0],
424 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100425 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000426 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100427 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000428 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100429 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400430 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400431 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400432 }
433 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
434 path->slots[0]);
435
436 /* don't overwrite an existing inode if the generation number
437 * was logged as zero. This is done when the tree logging code
438 * is just logging an inode to make sure it exists after recovery.
439 *
440 * Also, don't overwrite i_size on directories during replay.
441 * log replay inserts and removes directory items based on the
442 * state of the tree found in the subvolume, and i_size is modified
443 * as it goes
444 */
445 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
446 struct btrfs_inode_item *src_item;
447 struct btrfs_inode_item *dst_item;
448
449 src_item = (struct btrfs_inode_item *)src_ptr;
450 dst_item = (struct btrfs_inode_item *)dst_ptr;
451
452 if (btrfs_inode_generation(eb, src_item) == 0)
453 goto no_copy;
454
455 if (overwrite_root &&
456 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
457 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
458 save_old_i_size = 1;
459 saved_i_size = btrfs_inode_size(path->nodes[0],
460 dst_item);
461 }
462 }
463
464 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
465 src_ptr, item_size);
466
467 if (save_old_i_size) {
468 struct btrfs_inode_item *dst_item;
469 dst_item = (struct btrfs_inode_item *)dst_ptr;
470 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
471 }
472
473 /* make sure the generation is filled in */
474 if (key->type == BTRFS_INODE_ITEM_KEY) {
475 struct btrfs_inode_item *dst_item;
476 dst_item = (struct btrfs_inode_item *)dst_ptr;
477 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
478 btrfs_set_inode_generation(path->nodes[0], dst_item,
479 trans->transid);
480 }
481 }
482no_copy:
483 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200484 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400485 return 0;
486}
487
488/*
489 * simple helper to read an inode off the disk from a given root
490 * This can only be called for subvolume roots and not for the log
491 */
492static noinline struct inode *read_one_inode(struct btrfs_root *root,
493 u64 objectid)
494{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400495 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400496 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400497
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400498 key.objectid = objectid;
499 key.type = BTRFS_INODE_ITEM_KEY;
500 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000501 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400502 if (IS_ERR(inode)) {
503 inode = NULL;
504 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400505 iput(inode);
506 inode = NULL;
507 }
508 return inode;
509}
510
511/* replays a single extent in 'eb' at 'slot' with 'key' into the
512 * subvolume 'root'. path is released on entry and should be released
513 * on exit.
514 *
515 * extents in the log tree have not been allocated out of the extent
516 * tree yet. So, this completes the allocation, taking a reference
517 * as required if the extent already exists or creating a new extent
518 * if it isn't in the extent allocation tree yet.
519 *
520 * The extent is inserted into the file, dropping any existing extents
521 * from the file that overlap the new one.
522 */
523static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct btrfs_path *path,
526 struct extent_buffer *eb, int slot,
527 struct btrfs_key *key)
528{
529 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400530 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400531 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000532 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400533 struct btrfs_file_extent_item *item;
534 struct inode *inode = NULL;
535 unsigned long size;
536 int ret = 0;
537
538 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
539 found_type = btrfs_file_extent_type(eb, item);
540
Yan Zhengd899e052008-10-30 14:25:28 -0400541 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000542 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
543 nbytes = btrfs_file_extent_num_bytes(eb, item);
544 extent_end = start + nbytes;
545
546 /*
547 * We don't add to the inodes nbytes if we are prealloc or a
548 * hole.
549 */
550 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
551 nbytes = 0;
552 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400553 size = btrfs_file_extent_inline_len(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000554 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000555 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400556 } else {
557 ret = 0;
558 goto out;
559 }
560
561 inode = read_one_inode(root, key->objectid);
562 if (!inode) {
563 ret = -EIO;
564 goto out;
565 }
566
567 /*
568 * first check to see if we already have this extent in the
569 * file. This must be done before the btrfs_drop_extents run
570 * so we don't try to drop this extent.
571 */
Li Zefan33345d012011-04-20 10:31:50 +0800572 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400573 start, 0);
574
Yan Zhengd899e052008-10-30 14:25:28 -0400575 if (ret == 0 &&
576 (found_type == BTRFS_FILE_EXTENT_REG ||
577 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400578 struct btrfs_file_extent_item cmp1;
579 struct btrfs_file_extent_item cmp2;
580 struct btrfs_file_extent_item *existing;
581 struct extent_buffer *leaf;
582
583 leaf = path->nodes[0];
584 existing = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_file_extent_item);
586
587 read_extent_buffer(eb, &cmp1, (unsigned long)item,
588 sizeof(cmp1));
589 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
590 sizeof(cmp2));
591
592 /*
593 * we already have a pointer to this exact extent,
594 * we don't have to do anything
595 */
596 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200597 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400598 goto out;
599 }
600 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200601 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400602
603 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400604 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400605 if (ret)
606 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400607
Yan Zheng07d400a2009-01-06 11:42:00 -0500608 if (found_type == BTRFS_FILE_EXTENT_REG ||
609 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500611 unsigned long dest_offset;
612 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400613
Yan Zheng07d400a2009-01-06 11:42:00 -0500614 ret = btrfs_insert_empty_item(trans, root, path, key,
615 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400616 if (ret)
617 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500618 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
619 path->slots[0]);
620 copy_extent_buffer(path->nodes[0], eb, dest_offset,
621 (unsigned long)item, sizeof(*item));
622
623 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
624 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
625 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400626 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500627
628 if (ins.objectid > 0) {
629 u64 csum_start;
630 u64 csum_end;
631 LIST_HEAD(ordered_sums);
632 /*
633 * is this extent already allocated in the extent
634 * allocation tree? If so, just add a reference
635 */
636 ret = btrfs_lookup_extent(root, ins.objectid,
637 ins.offset);
638 if (ret == 0) {
639 ret = btrfs_inc_extent_ref(trans, root,
640 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400641 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200642 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400643 if (ret)
644 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500645 } else {
646 /*
647 * insert the extent pointer in the extent
648 * allocation tree
649 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400650 ret = btrfs_alloc_logged_file_extent(trans,
651 root, root->root_key.objectid,
652 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400653 if (ret)
654 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500655 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200656 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500657
658 if (btrfs_file_extent_compression(eb, item)) {
659 csum_start = ins.objectid;
660 csum_end = csum_start + ins.offset;
661 } else {
662 csum_start = ins.objectid +
663 btrfs_file_extent_offset(eb, item);
664 csum_end = csum_start +
665 btrfs_file_extent_num_bytes(eb, item);
666 }
667
668 ret = btrfs_lookup_csums_range(root->log_root,
669 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100670 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400671 if (ret)
672 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500673 while (!list_empty(&ordered_sums)) {
674 struct btrfs_ordered_sum *sums;
675 sums = list_entry(ordered_sums.next,
676 struct btrfs_ordered_sum,
677 list);
Josef Bacik36508602013-04-25 16:23:32 -0400678 if (!ret)
679 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500680 root->fs_info->csum_root,
681 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500682 list_del(&sums->list);
683 kfree(sums);
684 }
Josef Bacik36508602013-04-25 16:23:32 -0400685 if (ret)
686 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500687 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200688 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500689 }
690 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
691 /* inline extents are easy, we just overwrite them */
692 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400693 if (ret)
694 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500695 }
696
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000697 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600698 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400699out:
700 if (inode)
701 iput(inode);
702 return ret;
703}
704
705/*
706 * when cleaning up conflicts between the directory names in the
707 * subvolume, directory names in the log and directory names in the
708 * inode back references, we may have to unlink inodes from directories.
709 *
710 * This is a helper function to do the unlink of a specific directory
711 * item
712 */
713static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
714 struct btrfs_root *root,
715 struct btrfs_path *path,
716 struct inode *dir,
717 struct btrfs_dir_item *di)
718{
719 struct inode *inode;
720 char *name;
721 int name_len;
722 struct extent_buffer *leaf;
723 struct btrfs_key location;
724 int ret;
725
726 leaf = path->nodes[0];
727
728 btrfs_dir_item_key_to_cpu(leaf, di, &location);
729 name_len = btrfs_dir_name_len(leaf, di);
730 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000731 if (!name)
732 return -ENOMEM;
733
Chris Masone02119d2008-09-05 16:13:11 -0400734 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200735 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400736
737 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000738 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400739 ret = -EIO;
740 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000741 }
Chris Masone02119d2008-09-05 16:13:11 -0400742
Yan Zhengec051c02009-01-05 15:43:42 -0500743 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400744 if (ret)
745 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400746
Chris Masone02119d2008-09-05 16:13:11 -0400747 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400748 if (ret)
749 goto out;
Chris Masonb6305562012-07-02 15:29:53 -0400750 btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400751out:
752 kfree(name);
753 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400754 return ret;
755}
756
757/*
758 * helper function to see if a given name and sequence number found
759 * in an inode back reference are already in a directory and correctly
760 * point to this inode
761 */
762static noinline int inode_in_dir(struct btrfs_root *root,
763 struct btrfs_path *path,
764 u64 dirid, u64 objectid, u64 index,
765 const char *name, int name_len)
766{
767 struct btrfs_dir_item *di;
768 struct btrfs_key location;
769 int match = 0;
770
771 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
772 index, name, name_len, 0);
773 if (di && !IS_ERR(di)) {
774 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
775 if (location.objectid != objectid)
776 goto out;
777 } else
778 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200779 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400780
781 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
782 if (di && !IS_ERR(di)) {
783 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
784 if (location.objectid != objectid)
785 goto out;
786 } else
787 goto out;
788 match = 1;
789out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200790 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400791 return match;
792}
793
794/*
795 * helper function to check a log tree for a named back reference in
796 * an inode. This is used to decide if a back reference that is
797 * found in the subvolume conflicts with what we find in the log.
798 *
799 * inode backreferences may have multiple refs in a single item,
800 * during replay we process one reference at a time, and we don't
801 * want to delete valid links to a file from the subvolume if that
802 * link is also in the log.
803 */
804static noinline int backref_in_log(struct btrfs_root *log,
805 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700806 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400807 char *name, int namelen)
808{
809 struct btrfs_path *path;
810 struct btrfs_inode_ref *ref;
811 unsigned long ptr;
812 unsigned long ptr_end;
813 unsigned long name_ptr;
814 int found_name_len;
815 int item_size;
816 int ret;
817 int match = 0;
818
819 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000820 if (!path)
821 return -ENOMEM;
822
Chris Masone02119d2008-09-05 16:13:11 -0400823 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
824 if (ret != 0)
825 goto out;
826
Chris Masone02119d2008-09-05 16:13:11 -0400827 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700828
829 if (key->type == BTRFS_INODE_EXTREF_KEY) {
830 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
831 name, namelen, NULL))
832 match = 1;
833
834 goto out;
835 }
836
837 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400838 ptr_end = ptr + item_size;
839 while (ptr < ptr_end) {
840 ref = (struct btrfs_inode_ref *)ptr;
841 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
842 if (found_name_len == namelen) {
843 name_ptr = (unsigned long)(ref + 1);
844 ret = memcmp_extent_buffer(path->nodes[0], name,
845 name_ptr, namelen);
846 if (ret == 0) {
847 match = 1;
848 goto out;
849 }
850 }
851 ptr = (unsigned long)(ref + 1) + found_name_len;
852 }
853out:
854 btrfs_free_path(path);
855 return match;
856}
857
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700858static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
859 struct btrfs_root *root,
860 struct btrfs_path *path,
861 struct btrfs_root *log_root,
862 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700863 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700864 u64 inode_objectid, u64 parent_objectid,
865 u64 ref_index, char *name, int namelen,
866 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700867{
868 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700869 char *victim_name;
870 int victim_name_len;
871 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700872 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700873 struct btrfs_key search_key;
874 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700875
Mark Fashehf1863732012-08-08 11:32:27 -0700876again:
877 /* Search old style refs */
878 search_key.objectid = inode_objectid;
879 search_key.type = BTRFS_INODE_REF_KEY;
880 search_key.offset = parent_objectid;
881 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700882 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700883 struct btrfs_inode_ref *victim_ref;
884 unsigned long ptr;
885 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700886
887 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700888
889 /* are we trying to overwrite a back ref for the root directory
890 * if so, just jump out, we're done
891 */
Mark Fashehf1863732012-08-08 11:32:27 -0700892 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700893 return 1;
894
895 /* check all the names in this back reference to see
896 * if they are in the log. if so, we allow them to stay
897 * otherwise they must be unlinked as a conflict
898 */
899 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
900 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
901 while (ptr < ptr_end) {
902 victim_ref = (struct btrfs_inode_ref *)ptr;
903 victim_name_len = btrfs_inode_ref_name_len(leaf,
904 victim_ref);
905 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400906 if (!victim_name)
907 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700908
909 read_extent_buffer(leaf, victim_name,
910 (unsigned long)(victim_ref + 1),
911 victim_name_len);
912
Mark Fashehf1863732012-08-08 11:32:27 -0700913 if (!backref_in_log(log_root, &search_key,
914 parent_objectid,
915 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700916 victim_name_len)) {
917 btrfs_inc_nlink(inode);
918 btrfs_release_path(path);
919
920 ret = btrfs_unlink_inode(trans, root, dir,
921 inode, victim_name,
922 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700923 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400924 if (ret)
925 return ret;
926 btrfs_run_delayed_items(trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -0700927 *search_done = 1;
928 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700929 }
930 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700931
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700932 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
933 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700934
935 /*
936 * NOTE: we have searched root tree and checked the
937 * coresponding ref, it does not need to check again.
938 */
939 *search_done = 1;
940 }
941 btrfs_release_path(path);
942
Mark Fashehf1863732012-08-08 11:32:27 -0700943 /* Same search but for extended refs */
944 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
945 inode_objectid, parent_objectid, 0,
946 0);
947 if (!IS_ERR_OR_NULL(extref)) {
948 u32 item_size;
949 u32 cur_offset = 0;
950 unsigned long base;
951 struct inode *victim_parent;
952
953 leaf = path->nodes[0];
954
955 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
956 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
957
958 while (cur_offset < item_size) {
959 extref = (struct btrfs_inode_extref *)base + cur_offset;
960
961 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
962
963 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
964 goto next;
965
966 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400967 if (!victim_name)
968 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -0700969 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
970 victim_name_len);
971
972 search_key.objectid = inode_objectid;
973 search_key.type = BTRFS_INODE_EXTREF_KEY;
974 search_key.offset = btrfs_extref_hash(parent_objectid,
975 victim_name,
976 victim_name_len);
977 ret = 0;
978 if (!backref_in_log(log_root, &search_key,
979 parent_objectid, victim_name,
980 victim_name_len)) {
981 ret = -ENOENT;
982 victim_parent = read_one_inode(root,
983 parent_objectid);
984 if (victim_parent) {
985 btrfs_inc_nlink(inode);
986 btrfs_release_path(path);
987
988 ret = btrfs_unlink_inode(trans, root,
989 victim_parent,
990 inode,
991 victim_name,
992 victim_name_len);
993 btrfs_run_delayed_items(trans, root);
994 }
Mark Fashehf1863732012-08-08 11:32:27 -0700995 iput(victim_parent);
996 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400997 if (ret)
998 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700999 *search_done = 1;
1000 goto again;
1001 }
1002 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001003 if (ret)
1004 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001005next:
1006 cur_offset += victim_name_len + sizeof(*extref);
1007 }
1008 *search_done = 1;
1009 }
1010 btrfs_release_path(path);
1011
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001012 /* look for a conflicting sequence number */
1013 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001014 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001015 if (di && !IS_ERR(di)) {
1016 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001017 if (ret)
1018 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001019 }
1020 btrfs_release_path(path);
1021
1022 /* look for a conflicing name */
1023 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1024 name, namelen, 0);
1025 if (di && !IS_ERR(di)) {
1026 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001027 if (ret)
1028 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001029 }
1030 btrfs_release_path(path);
1031
1032 return 0;
1033}
Chris Masone02119d2008-09-05 16:13:11 -04001034
Mark Fashehf1863732012-08-08 11:32:27 -07001035static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1036 u32 *namelen, char **name, u64 *index,
1037 u64 *parent_objectid)
1038{
1039 struct btrfs_inode_extref *extref;
1040
1041 extref = (struct btrfs_inode_extref *)ref_ptr;
1042
1043 *namelen = btrfs_inode_extref_name_len(eb, extref);
1044 *name = kmalloc(*namelen, GFP_NOFS);
1045 if (*name == NULL)
1046 return -ENOMEM;
1047
1048 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1049 *namelen);
1050
1051 *index = btrfs_inode_extref_index(eb, extref);
1052 if (parent_objectid)
1053 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1054
1055 return 0;
1056}
1057
1058static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1059 u32 *namelen, char **name, u64 *index)
1060{
1061 struct btrfs_inode_ref *ref;
1062
1063 ref = (struct btrfs_inode_ref *)ref_ptr;
1064
1065 *namelen = btrfs_inode_ref_name_len(eb, ref);
1066 *name = kmalloc(*namelen, GFP_NOFS);
1067 if (*name == NULL)
1068 return -ENOMEM;
1069
1070 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1071
1072 *index = btrfs_inode_ref_index(eb, ref);
1073
1074 return 0;
1075}
1076
Chris Masone02119d2008-09-05 16:13:11 -04001077/*
1078 * replay one inode back reference item found in the log tree.
1079 * eb, slot and key refer to the buffer and key found in the log tree.
1080 * root is the destination we are replaying into, and path is for temp
1081 * use by this function. (it should be released on return).
1082 */
1083static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1084 struct btrfs_root *root,
1085 struct btrfs_root *log,
1086 struct btrfs_path *path,
1087 struct extent_buffer *eb, int slot,
1088 struct btrfs_key *key)
1089{
liubo34f3e4f2011-08-06 08:35:23 +00001090 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001091 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -04001092 unsigned long ref_ptr;
1093 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +00001094 char *name;
1095 int namelen;
1096 int ret;
liuboc622ae62011-03-26 08:01:12 -04001097 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001098 int log_ref_ver = 0;
1099 u64 parent_objectid;
1100 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001101 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001102 int ref_struct_size;
1103
1104 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1105 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1106
1107 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1108 struct btrfs_inode_extref *r;
1109
1110 ref_struct_size = sizeof(struct btrfs_inode_extref);
1111 log_ref_ver = 1;
1112 r = (struct btrfs_inode_extref *)ref_ptr;
1113 parent_objectid = btrfs_inode_extref_parent(eb, r);
1114 } else {
1115 ref_struct_size = sizeof(struct btrfs_inode_ref);
1116 parent_objectid = key->offset;
1117 }
1118 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001119
Chris Masone02119d2008-09-05 16:13:11 -04001120 /*
1121 * it is possible that we didn't log all the parent directories
1122 * for a given inode. If we don't find the dir, just don't
1123 * copy the back ref in. The link count fixup code will take
1124 * care of the rest
1125 */
Mark Fashehf1863732012-08-08 11:32:27 -07001126 dir = read_one_inode(root, parent_objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001127 if (!dir)
1128 return -ENOENT;
1129
Mark Fashehf1863732012-08-08 11:32:27 -07001130 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001131 if (!inode) {
1132 iput(dir);
1133 return -EIO;
1134 }
Chris Masone02119d2008-09-05 16:13:11 -04001135
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001136 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001137 if (log_ref_ver) {
1138 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1139 &ref_index, &parent_objectid);
1140 /*
1141 * parent object can change from one array
1142 * item to another.
1143 */
1144 if (!dir)
1145 dir = read_one_inode(root, parent_objectid);
1146 if (!dir)
1147 return -ENOENT;
1148 } else {
1149 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1150 &ref_index);
1151 }
1152 if (ret)
1153 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001154
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001155 /* if we already have a perfect match, we're done */
1156 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001157 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001158 /*
1159 * look for a conflicting back reference in the
1160 * metadata. if we find one we have to unlink that name
1161 * of the file before we add our new link. Later on, we
1162 * overwrite any existing back reference, and we don't
1163 * want to create dangling pointers in the directory.
1164 */
Chris Masone02119d2008-09-05 16:13:11 -04001165
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001166 if (!search_done) {
1167 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001168 dir, inode, eb,
1169 inode_objectid,
1170 parent_objectid,
1171 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001172 &search_done);
Josef Bacik36508602013-04-25 16:23:32 -04001173 if (ret == 1) {
1174 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001175 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001176 }
1177 if (ret)
1178 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001179 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001180
1181 /* insert our name */
1182 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001183 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001184 if (ret)
1185 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001186
1187 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001188 }
liuboc622ae62011-03-26 08:01:12 -04001189
Mark Fashehf1863732012-08-08 11:32:27 -07001190 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001191 kfree(name);
Mark Fashehf1863732012-08-08 11:32:27 -07001192 if (log_ref_ver) {
1193 iput(dir);
1194 dir = NULL;
1195 }
Chris Masone02119d2008-09-05 16:13:11 -04001196 }
Chris Masone02119d2008-09-05 16:13:11 -04001197
1198 /* finally write the back reference in the inode */
1199 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001200out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001201 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001202 iput(dir);
1203 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001204 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001205}
1206
Yan, Zhengc71bf092009-11-12 09:34:40 +00001207static int insert_orphan_item(struct btrfs_trans_handle *trans,
1208 struct btrfs_root *root, u64 offset)
1209{
1210 int ret;
1211 ret = btrfs_find_orphan_item(root, offset);
1212 if (ret > 0)
1213 ret = btrfs_insert_orphan_item(trans, root, offset);
1214 return ret;
1215}
1216
Mark Fashehf1863732012-08-08 11:32:27 -07001217static int count_inode_extrefs(struct btrfs_root *root,
1218 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001219{
Mark Fashehf1863732012-08-08 11:32:27 -07001220 int ret = 0;
1221 int name_len;
1222 unsigned int nlink = 0;
1223 u32 item_size;
1224 u32 cur_offset = 0;
1225 u64 inode_objectid = btrfs_ino(inode);
1226 u64 offset = 0;
1227 unsigned long ptr;
1228 struct btrfs_inode_extref *extref;
1229 struct extent_buffer *leaf;
1230
1231 while (1) {
1232 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1233 &extref, &offset);
1234 if (ret)
1235 break;
1236
1237 leaf = path->nodes[0];
1238 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1239 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1240
1241 while (cur_offset < item_size) {
1242 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1243 name_len = btrfs_inode_extref_name_len(leaf, extref);
1244
1245 nlink++;
1246
1247 cur_offset += name_len + sizeof(*extref);
1248 }
1249
1250 offset++;
1251 btrfs_release_path(path);
1252 }
1253 btrfs_release_path(path);
1254
1255 if (ret < 0)
1256 return ret;
1257 return nlink;
1258}
1259
1260static int count_inode_refs(struct btrfs_root *root,
1261 struct inode *inode, struct btrfs_path *path)
1262{
Chris Masone02119d2008-09-05 16:13:11 -04001263 int ret;
1264 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001265 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001266 unsigned long ptr;
1267 unsigned long ptr_end;
1268 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001269 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001270
Li Zefan33345d012011-04-20 10:31:50 +08001271 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001272 key.type = BTRFS_INODE_REF_KEY;
1273 key.offset = (u64)-1;
1274
Chris Masond3977122009-01-05 21:25:51 -05001275 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001276 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1277 if (ret < 0)
1278 break;
1279 if (ret > 0) {
1280 if (path->slots[0] == 0)
1281 break;
1282 path->slots[0]--;
1283 }
1284 btrfs_item_key_to_cpu(path->nodes[0], &key,
1285 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001286 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001287 key.type != BTRFS_INODE_REF_KEY)
1288 break;
1289 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1290 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1291 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001292 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001293 struct btrfs_inode_ref *ref;
1294
1295 ref = (struct btrfs_inode_ref *)ptr;
1296 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1297 ref);
1298 ptr = (unsigned long)(ref + 1) + name_len;
1299 nlink++;
1300 }
1301
1302 if (key.offset == 0)
1303 break;
1304 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001305 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001306 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001307 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001308
1309 return nlink;
1310}
1311
1312/*
1313 * There are a few corners where the link count of the file can't
1314 * be properly maintained during replay. So, instead of adding
1315 * lots of complexity to the log code, we just scan the backrefs
1316 * for any file that has been through replay.
1317 *
1318 * The scan will update the link count on the inode to reflect the
1319 * number of back refs found. If it goes down to zero, the iput
1320 * will free the inode.
1321 */
1322static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1323 struct btrfs_root *root,
1324 struct inode *inode)
1325{
1326 struct btrfs_path *path;
1327 int ret;
1328 u64 nlink = 0;
1329 u64 ino = btrfs_ino(inode);
1330
1331 path = btrfs_alloc_path();
1332 if (!path)
1333 return -ENOMEM;
1334
1335 ret = count_inode_refs(root, inode, path);
1336 if (ret < 0)
1337 goto out;
1338
1339 nlink = ret;
1340
1341 ret = count_inode_extrefs(root, inode, path);
1342 if (ret == -ENOENT)
1343 ret = 0;
1344
1345 if (ret < 0)
1346 goto out;
1347
1348 nlink += ret;
1349
1350 ret = 0;
1351
Chris Masone02119d2008-09-05 16:13:11 -04001352 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001353 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001354 btrfs_update_inode(trans, root, inode);
1355 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001356 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001357
Yan, Zhengc71bf092009-11-12 09:34:40 +00001358 if (inode->i_nlink == 0) {
1359 if (S_ISDIR(inode->i_mode)) {
1360 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001361 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001362 if (ret)
1363 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001364 }
Li Zefan33345d012011-04-20 10:31:50 +08001365 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001366 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001367
Mark Fashehf1863732012-08-08 11:32:27 -07001368out:
1369 btrfs_free_path(path);
1370 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001371}
1372
1373static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root,
1375 struct btrfs_path *path)
1376{
1377 int ret;
1378 struct btrfs_key key;
1379 struct inode *inode;
1380
1381 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1382 key.type = BTRFS_ORPHAN_ITEM_KEY;
1383 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001384 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001385 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1386 if (ret < 0)
1387 break;
1388
1389 if (ret == 1) {
1390 if (path->slots[0] == 0)
1391 break;
1392 path->slots[0]--;
1393 }
1394
1395 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1396 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1397 key.type != BTRFS_ORPHAN_ITEM_KEY)
1398 break;
1399
1400 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001401 if (ret)
1402 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001403
David Sterbab3b4aa72011-04-21 01:20:15 +02001404 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001405 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001406 if (!inode)
1407 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001408
1409 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001410 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001411 if (ret)
1412 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001413
Chris Mason12fcfd22009-03-24 10:24:20 -04001414 /*
1415 * fixup on a directory may create new entries,
1416 * make sure we always look for the highset possible
1417 * offset
1418 */
1419 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001420 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001421 ret = 0;
1422out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001423 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001424 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001425}
1426
1427
1428/*
1429 * record a given inode in the fixup dir so we can check its link
1430 * count when replay is done. The link count is incremented here
1431 * so the inode won't go away until we check it
1432 */
1433static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 struct btrfs_path *path,
1436 u64 objectid)
1437{
1438 struct btrfs_key key;
1439 int ret = 0;
1440 struct inode *inode;
1441
1442 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001443 if (!inode)
1444 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001445
1446 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1447 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1448 key.offset = objectid;
1449
1450 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1451
David Sterbab3b4aa72011-04-21 01:20:15 +02001452 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001453 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001454 if (!inode->i_nlink)
1455 set_nlink(inode, 1);
1456 else
1457 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001458 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001459 } else if (ret == -EEXIST) {
1460 ret = 0;
1461 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001462 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001463 }
1464 iput(inode);
1465
1466 return ret;
1467}
1468
1469/*
1470 * when replaying the log for a directory, we only insert names
1471 * for inodes that actually exist. This means an fsync on a directory
1472 * does not implicitly fsync all the new files in it
1473 */
1474static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1475 struct btrfs_root *root,
1476 struct btrfs_path *path,
1477 u64 dirid, u64 index,
1478 char *name, int name_len, u8 type,
1479 struct btrfs_key *location)
1480{
1481 struct inode *inode;
1482 struct inode *dir;
1483 int ret;
1484
1485 inode = read_one_inode(root, location->objectid);
1486 if (!inode)
1487 return -ENOENT;
1488
1489 dir = read_one_inode(root, dirid);
1490 if (!dir) {
1491 iput(inode);
1492 return -EIO;
1493 }
1494 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1495
1496 /* FIXME, put inode into FIXUP list */
1497
1498 iput(inode);
1499 iput(dir);
1500 return ret;
1501}
1502
1503/*
1504 * take a single entry in a log directory item and replay it into
1505 * the subvolume.
1506 *
1507 * if a conflicting item exists in the subdirectory already,
1508 * the inode it points to is unlinked and put into the link count
1509 * fix up tree.
1510 *
1511 * If a name from the log points to a file or directory that does
1512 * not exist in the FS, it is skipped. fsyncs on directories
1513 * do not force down inodes inside that directory, just changes to the
1514 * names or unlinks in a directory.
1515 */
1516static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1517 struct btrfs_root *root,
1518 struct btrfs_path *path,
1519 struct extent_buffer *eb,
1520 struct btrfs_dir_item *di,
1521 struct btrfs_key *key)
1522{
1523 char *name;
1524 int name_len;
1525 struct btrfs_dir_item *dst_di;
1526 struct btrfs_key found_key;
1527 struct btrfs_key log_key;
1528 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001529 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001530 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001531 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001532
1533 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001534 if (!dir)
1535 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001536
1537 name_len = btrfs_dir_name_len(eb, di);
1538 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001539 if (!name)
1540 return -ENOMEM;
1541
Chris Masone02119d2008-09-05 16:13:11 -04001542 log_type = btrfs_dir_type(eb, di);
1543 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1544 name_len);
1545
1546 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001547 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1548 if (exists == 0)
1549 exists = 1;
1550 else
1551 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001552 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001553
Chris Masone02119d2008-09-05 16:13:11 -04001554 if (key->type == BTRFS_DIR_ITEM_KEY) {
1555 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1556 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001557 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001558 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1559 key->objectid,
1560 key->offset, name,
1561 name_len, 1);
1562 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001563 /* Corruption */
1564 ret = -EINVAL;
1565 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001566 }
David Sterbac7040052011-04-19 18:00:01 +02001567 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001568 /* we need a sequence number to insert, so we only
1569 * do inserts for the BTRFS_DIR_INDEX_KEY types
1570 */
1571 if (key->type != BTRFS_DIR_INDEX_KEY)
1572 goto out;
1573 goto insert;
1574 }
1575
1576 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1577 /* the existing item matches the logged item */
1578 if (found_key.objectid == log_key.objectid &&
1579 found_key.type == log_key.type &&
1580 found_key.offset == log_key.offset &&
1581 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1582 goto out;
1583 }
1584
1585 /*
1586 * don't drop the conflicting directory entry if the inode
1587 * for the new entry doesn't exist
1588 */
Chris Mason4bef0842008-09-08 11:18:08 -04001589 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001590 goto out;
1591
Chris Masone02119d2008-09-05 16:13:11 -04001592 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001593 if (ret)
1594 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001595
1596 if (key->type == BTRFS_DIR_INDEX_KEY)
1597 goto insert;
1598out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001599 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001600 kfree(name);
1601 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001602 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001603
1604insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001605 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001606 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1607 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001608 if (ret && ret != -ENOENT)
1609 goto out;
1610 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001611 goto out;
1612}
1613
1614/*
1615 * find all the names in a directory item and reconcile them into
1616 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1617 * one name in a directory item, but the same code gets used for
1618 * both directory index types
1619 */
1620static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1621 struct btrfs_root *root,
1622 struct btrfs_path *path,
1623 struct extent_buffer *eb, int slot,
1624 struct btrfs_key *key)
1625{
1626 int ret;
1627 u32 item_size = btrfs_item_size_nr(eb, slot);
1628 struct btrfs_dir_item *di;
1629 int name_len;
1630 unsigned long ptr;
1631 unsigned long ptr_end;
1632
1633 ptr = btrfs_item_ptr_offset(eb, slot);
1634 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001635 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001636 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001637 if (verify_dir_item(root, eb, di))
1638 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001639 name_len = btrfs_dir_name_len(eb, di);
1640 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001641 if (ret)
1642 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001643 ptr = (unsigned long)(di + 1);
1644 ptr += name_len;
1645 }
1646 return 0;
1647}
1648
1649/*
1650 * directory replay has two parts. There are the standard directory
1651 * items in the log copied from the subvolume, and range items
1652 * created in the log while the subvolume was logged.
1653 *
1654 * The range items tell us which parts of the key space the log
1655 * is authoritative for. During replay, if a key in the subvolume
1656 * directory is in a logged range item, but not actually in the log
1657 * that means it was deleted from the directory before the fsync
1658 * and should be removed.
1659 */
1660static noinline int find_dir_range(struct btrfs_root *root,
1661 struct btrfs_path *path,
1662 u64 dirid, int key_type,
1663 u64 *start_ret, u64 *end_ret)
1664{
1665 struct btrfs_key key;
1666 u64 found_end;
1667 struct btrfs_dir_log_item *item;
1668 int ret;
1669 int nritems;
1670
1671 if (*start_ret == (u64)-1)
1672 return 1;
1673
1674 key.objectid = dirid;
1675 key.type = key_type;
1676 key.offset = *start_ret;
1677
1678 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1679 if (ret < 0)
1680 goto out;
1681 if (ret > 0) {
1682 if (path->slots[0] == 0)
1683 goto out;
1684 path->slots[0]--;
1685 }
1686 if (ret != 0)
1687 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1688
1689 if (key.type != key_type || key.objectid != dirid) {
1690 ret = 1;
1691 goto next;
1692 }
1693 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1694 struct btrfs_dir_log_item);
1695 found_end = btrfs_dir_log_end(path->nodes[0], item);
1696
1697 if (*start_ret >= key.offset && *start_ret <= found_end) {
1698 ret = 0;
1699 *start_ret = key.offset;
1700 *end_ret = found_end;
1701 goto out;
1702 }
1703 ret = 1;
1704next:
1705 /* check the next slot in the tree to see if it is a valid item */
1706 nritems = btrfs_header_nritems(path->nodes[0]);
1707 if (path->slots[0] >= nritems) {
1708 ret = btrfs_next_leaf(root, path);
1709 if (ret)
1710 goto out;
1711 } else {
1712 path->slots[0]++;
1713 }
1714
1715 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1716
1717 if (key.type != key_type || key.objectid != dirid) {
1718 ret = 1;
1719 goto out;
1720 }
1721 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1722 struct btrfs_dir_log_item);
1723 found_end = btrfs_dir_log_end(path->nodes[0], item);
1724 *start_ret = key.offset;
1725 *end_ret = found_end;
1726 ret = 0;
1727out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001728 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001729 return ret;
1730}
1731
1732/*
1733 * this looks for a given directory item in the log. If the directory
1734 * item is not in the log, the item is removed and the inode it points
1735 * to is unlinked
1736 */
1737static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1738 struct btrfs_root *root,
1739 struct btrfs_root *log,
1740 struct btrfs_path *path,
1741 struct btrfs_path *log_path,
1742 struct inode *dir,
1743 struct btrfs_key *dir_key)
1744{
1745 int ret;
1746 struct extent_buffer *eb;
1747 int slot;
1748 u32 item_size;
1749 struct btrfs_dir_item *di;
1750 struct btrfs_dir_item *log_di;
1751 int name_len;
1752 unsigned long ptr;
1753 unsigned long ptr_end;
1754 char *name;
1755 struct inode *inode;
1756 struct btrfs_key location;
1757
1758again:
1759 eb = path->nodes[0];
1760 slot = path->slots[0];
1761 item_size = btrfs_item_size_nr(eb, slot);
1762 ptr = btrfs_item_ptr_offset(eb, slot);
1763 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001764 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001765 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001766 if (verify_dir_item(root, eb, di)) {
1767 ret = -EIO;
1768 goto out;
1769 }
1770
Chris Masone02119d2008-09-05 16:13:11 -04001771 name_len = btrfs_dir_name_len(eb, di);
1772 name = kmalloc(name_len, GFP_NOFS);
1773 if (!name) {
1774 ret = -ENOMEM;
1775 goto out;
1776 }
1777 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1778 name_len);
1779 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001780 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001781 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1782 dir_key->objectid,
1783 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001784 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001785 log_di = btrfs_lookup_dir_index_item(trans, log,
1786 log_path,
1787 dir_key->objectid,
1788 dir_key->offset,
1789 name, name_len, 0);
1790 }
David Sterbac7040052011-04-19 18:00:01 +02001791 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001792 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001793 btrfs_release_path(path);
1794 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001795 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001796 if (!inode) {
1797 kfree(name);
1798 return -EIO;
1799 }
Chris Masone02119d2008-09-05 16:13:11 -04001800
1801 ret = link_to_fixup_dir(trans, root,
1802 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001803 if (ret) {
1804 kfree(name);
1805 iput(inode);
1806 goto out;
1807 }
1808
Chris Masone02119d2008-09-05 16:13:11 -04001809 btrfs_inc_nlink(inode);
1810 ret = btrfs_unlink_inode(trans, root, dir, inode,
1811 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001812 if (!ret)
1813 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001814 kfree(name);
1815 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001816 if (ret)
1817 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001818
1819 /* there might still be more names under this key
1820 * check and repeat if required
1821 */
1822 ret = btrfs_search_slot(NULL, root, dir_key, path,
1823 0, 0);
1824 if (ret == 0)
1825 goto again;
1826 ret = 0;
1827 goto out;
1828 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001829 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001830 kfree(name);
1831
1832 ptr = (unsigned long)(di + 1);
1833 ptr += name_len;
1834 }
1835 ret = 0;
1836out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001837 btrfs_release_path(path);
1838 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001839 return ret;
1840}
1841
1842/*
1843 * deletion replay happens before we copy any new directory items
1844 * out of the log or out of backreferences from inodes. It
1845 * scans the log to find ranges of keys that log is authoritative for,
1846 * and then scans the directory to find items in those ranges that are
1847 * not present in the log.
1848 *
1849 * Anything we don't find in the log is unlinked and removed from the
1850 * directory.
1851 */
1852static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1853 struct btrfs_root *root,
1854 struct btrfs_root *log,
1855 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001856 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001857{
1858 u64 range_start;
1859 u64 range_end;
1860 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1861 int ret = 0;
1862 struct btrfs_key dir_key;
1863 struct btrfs_key found_key;
1864 struct btrfs_path *log_path;
1865 struct inode *dir;
1866
1867 dir_key.objectid = dirid;
1868 dir_key.type = BTRFS_DIR_ITEM_KEY;
1869 log_path = btrfs_alloc_path();
1870 if (!log_path)
1871 return -ENOMEM;
1872
1873 dir = read_one_inode(root, dirid);
1874 /* it isn't an error if the inode isn't there, that can happen
1875 * because we replay the deletes before we copy in the inode item
1876 * from the log
1877 */
1878 if (!dir) {
1879 btrfs_free_path(log_path);
1880 return 0;
1881 }
1882again:
1883 range_start = 0;
1884 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001885 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001886 if (del_all)
1887 range_end = (u64)-1;
1888 else {
1889 ret = find_dir_range(log, path, dirid, key_type,
1890 &range_start, &range_end);
1891 if (ret != 0)
1892 break;
1893 }
Chris Masone02119d2008-09-05 16:13:11 -04001894
1895 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001896 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001897 int nritems;
1898 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1899 0, 0);
1900 if (ret < 0)
1901 goto out;
1902
1903 nritems = btrfs_header_nritems(path->nodes[0]);
1904 if (path->slots[0] >= nritems) {
1905 ret = btrfs_next_leaf(root, path);
1906 if (ret)
1907 break;
1908 }
1909 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1910 path->slots[0]);
1911 if (found_key.objectid != dirid ||
1912 found_key.type != dir_key.type)
1913 goto next_type;
1914
1915 if (found_key.offset > range_end)
1916 break;
1917
1918 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001919 log_path, dir,
1920 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001921 if (ret)
1922 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001923 if (found_key.offset == (u64)-1)
1924 break;
1925 dir_key.offset = found_key.offset + 1;
1926 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001927 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001928 if (range_end == (u64)-1)
1929 break;
1930 range_start = range_end + 1;
1931 }
1932
1933next_type:
1934 ret = 0;
1935 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1936 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1937 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001938 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001939 goto again;
1940 }
1941out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001942 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001943 btrfs_free_path(log_path);
1944 iput(dir);
1945 return ret;
1946}
1947
1948/*
1949 * the process_func used to replay items from the log tree. This
1950 * gets called in two different stages. The first stage just looks
1951 * for inodes and makes sure they are all copied into the subvolume.
1952 *
1953 * The second stage copies all the other item types from the log into
1954 * the subvolume. The two stage approach is slower, but gets rid of
1955 * lots of complexity around inodes referencing other inodes that exist
1956 * only in the log (references come from either directory items or inode
1957 * back refs).
1958 */
1959static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1960 struct walk_control *wc, u64 gen)
1961{
1962 int nritems;
1963 struct btrfs_path *path;
1964 struct btrfs_root *root = wc->replay_dest;
1965 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001966 int level;
1967 int i;
1968 int ret;
1969
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001970 ret = btrfs_read_buffer(eb, gen);
1971 if (ret)
1972 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001973
1974 level = btrfs_header_level(eb);
1975
1976 if (level != 0)
1977 return 0;
1978
1979 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001980 if (!path)
1981 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001982
1983 nritems = btrfs_header_nritems(eb);
1984 for (i = 0; i < nritems; i++) {
1985 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001986
1987 /* inode keys are done during the first stage */
1988 if (key.type == BTRFS_INODE_ITEM_KEY &&
1989 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001990 struct btrfs_inode_item *inode_item;
1991 u32 mode;
1992
1993 inode_item = btrfs_item_ptr(eb, i,
1994 struct btrfs_inode_item);
1995 mode = btrfs_inode_mode(eb, inode_item);
1996 if (S_ISDIR(mode)) {
1997 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001998 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001999 if (ret)
2000 break;
Chris Masone02119d2008-09-05 16:13:11 -04002001 }
2002 ret = overwrite_item(wc->trans, root, path,
2003 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002004 if (ret)
2005 break;
Chris Masone02119d2008-09-05 16:13:11 -04002006
Yan, Zhengc71bf092009-11-12 09:34:40 +00002007 /* for regular files, make sure corresponding
2008 * orhpan item exist. extents past the new EOF
2009 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002010 */
2011 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002012 ret = insert_orphan_item(wc->trans, root,
2013 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002014 if (ret)
2015 break;
Chris Masone02119d2008-09-05 16:13:11 -04002016 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002017
Chris Masone02119d2008-09-05 16:13:11 -04002018 ret = link_to_fixup_dir(wc->trans, root,
2019 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002020 if (ret)
2021 break;
Chris Masone02119d2008-09-05 16:13:11 -04002022 }
2023 if (wc->stage < LOG_WALK_REPLAY_ALL)
2024 continue;
2025
2026 /* these keys are simply copied */
2027 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2028 ret = overwrite_item(wc->trans, root, path,
2029 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002030 if (ret)
2031 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002032 } else if (key.type == BTRFS_INODE_REF_KEY ||
2033 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002034 ret = add_inode_ref(wc->trans, root, log, path,
2035 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002036 if (ret && ret != -ENOENT)
2037 break;
2038 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002039 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2040 ret = replay_one_extent(wc->trans, root, path,
2041 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002042 if (ret)
2043 break;
Chris Masone02119d2008-09-05 16:13:11 -04002044 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
2045 key.type == BTRFS_DIR_INDEX_KEY) {
2046 ret = replay_one_dir_item(wc->trans, root, path,
2047 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002048 if (ret)
2049 break;
Chris Masone02119d2008-09-05 16:13:11 -04002050 }
2051 }
2052 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002053 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002054}
2055
Chris Masond3977122009-01-05 21:25:51 -05002056static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002057 struct btrfs_root *root,
2058 struct btrfs_path *path, int *level,
2059 struct walk_control *wc)
2060{
2061 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002062 u64 bytenr;
2063 u64 ptr_gen;
2064 struct extent_buffer *next;
2065 struct extent_buffer *cur;
2066 struct extent_buffer *parent;
2067 u32 blocksize;
2068 int ret = 0;
2069
2070 WARN_ON(*level < 0);
2071 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2072
Chris Masond3977122009-01-05 21:25:51 -05002073 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002074 WARN_ON(*level < 0);
2075 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2076 cur = path->nodes[*level];
2077
2078 if (btrfs_header_level(cur) != *level)
2079 WARN_ON(1);
2080
2081 if (path->slots[*level] >=
2082 btrfs_header_nritems(cur))
2083 break;
2084
2085 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2086 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2087 blocksize = btrfs_level_size(root, *level - 1);
2088
2089 parent = path->nodes[*level];
2090 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002091
2092 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002093 if (!next)
2094 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002095
Chris Masone02119d2008-09-05 16:13:11 -04002096 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002097 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002098 if (ret) {
2099 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002100 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002101 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002102
Chris Masone02119d2008-09-05 16:13:11 -04002103 path->slots[*level]++;
2104 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002105 ret = btrfs_read_buffer(next, ptr_gen);
2106 if (ret) {
2107 free_extent_buffer(next);
2108 return ret;
2109 }
Chris Masone02119d2008-09-05 16:13:11 -04002110
2111 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002112 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002113 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002114 btrfs_wait_tree_block_writeback(next);
2115 btrfs_tree_unlock(next);
2116
Chris Masone02119d2008-09-05 16:13:11 -04002117 WARN_ON(root_owner !=
2118 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002119 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002120 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002121 if (ret) {
2122 free_extent_buffer(next);
2123 return ret;
2124 }
Chris Masone02119d2008-09-05 16:13:11 -04002125 }
2126 free_extent_buffer(next);
2127 continue;
2128 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002129 ret = btrfs_read_buffer(next, ptr_gen);
2130 if (ret) {
2131 free_extent_buffer(next);
2132 return ret;
2133 }
Chris Masone02119d2008-09-05 16:13:11 -04002134
2135 WARN_ON(*level <= 0);
2136 if (path->nodes[*level-1])
2137 free_extent_buffer(path->nodes[*level-1]);
2138 path->nodes[*level-1] = next;
2139 *level = btrfs_header_level(next);
2140 path->slots[*level] = 0;
2141 cond_resched();
2142 }
2143 WARN_ON(*level < 0);
2144 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2145
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002146 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002147
2148 cond_resched();
2149 return 0;
2150}
2151
Chris Masond3977122009-01-05 21:25:51 -05002152static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002153 struct btrfs_root *root,
2154 struct btrfs_path *path, int *level,
2155 struct walk_control *wc)
2156{
2157 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002158 int i;
2159 int slot;
2160 int ret;
2161
Chris Masond3977122009-01-05 21:25:51 -05002162 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002163 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002164 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002165 path->slots[i]++;
2166 *level = i;
2167 WARN_ON(*level == 0);
2168 return 0;
2169 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 struct extent_buffer *parent;
2171 if (path->nodes[*level] == root->node)
2172 parent = path->nodes[*level];
2173 else
2174 parent = path->nodes[*level + 1];
2175
2176 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002177 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002178 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002179 if (ret)
2180 return ret;
2181
Chris Masone02119d2008-09-05 16:13:11 -04002182 if (wc->free) {
2183 struct extent_buffer *next;
2184
2185 next = path->nodes[*level];
2186
2187 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002188 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002189 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04002190 btrfs_wait_tree_block_writeback(next);
2191 btrfs_tree_unlock(next);
2192
Chris Masone02119d2008-09-05 16:13:11 -04002193 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002194 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002195 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002196 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002197 if (ret)
2198 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002199 }
2200 free_extent_buffer(path->nodes[*level]);
2201 path->nodes[*level] = NULL;
2202 *level = i + 1;
2203 }
2204 }
2205 return 1;
2206}
2207
2208/*
2209 * drop the reference count on the tree rooted at 'snap'. This traverses
2210 * the tree freeing any blocks that have a ref count of zero after being
2211 * decremented.
2212 */
2213static int walk_log_tree(struct btrfs_trans_handle *trans,
2214 struct btrfs_root *log, struct walk_control *wc)
2215{
2216 int ret = 0;
2217 int wret;
2218 int level;
2219 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002220 int orig_level;
2221
2222 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002223 if (!path)
2224 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002225
2226 level = btrfs_header_level(log->node);
2227 orig_level = level;
2228 path->nodes[level] = log->node;
2229 extent_buffer_get(log->node);
2230 path->slots[level] = 0;
2231
Chris Masond3977122009-01-05 21:25:51 -05002232 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002233 wret = walk_down_log_tree(trans, log, path, &level, wc);
2234 if (wret > 0)
2235 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002237 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238 goto out;
2239 }
Chris Masone02119d2008-09-05 16:13:11 -04002240
2241 wret = walk_up_log_tree(trans, log, path, &level, wc);
2242 if (wret > 0)
2243 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002245 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002246 goto out;
2247 }
Chris Masone02119d2008-09-05 16:13:11 -04002248 }
2249
2250 /* was the root node processed? if not, catch it here */
2251 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002252 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002253 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254 if (ret)
2255 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002256 if (wc->free) {
2257 struct extent_buffer *next;
2258
2259 next = path->nodes[orig_level];
2260
2261 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002262 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04002263 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04002264 btrfs_wait_tree_block_writeback(next);
2265 btrfs_tree_unlock(next);
2266
Chris Masone02119d2008-09-05 16:13:11 -04002267 WARN_ON(log->root_key.objectid !=
2268 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002269 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002270 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002271 if (ret)
2272 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002273 }
2274 }
2275
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002276out:
Chris Masone02119d2008-09-05 16:13:11 -04002277 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002278 return ret;
2279}
2280
Yan Zheng7237f182009-01-21 12:54:03 -05002281/*
2282 * helper function to update the item for a given subvolumes log root
2283 * in the tree of log roots
2284 */
2285static int update_log_root(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *log)
2287{
2288 int ret;
2289
2290 if (log->log_transid == 1) {
2291 /* insert root item on the first sync */
2292 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2293 &log->root_key, &log->root_item);
2294 } else {
2295 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2296 &log->root_key, &log->root_item);
2297 }
2298 return ret;
2299}
2300
Chris Mason12fcfd22009-03-24 10:24:20 -04002301static int wait_log_commit(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002303{
2304 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002305 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002306
Yan Zheng7237f182009-01-21 12:54:03 -05002307 /*
2308 * we only allow two pending log transactions at a time,
2309 * so we know that if ours is more than 2 older than the
2310 * current transaction, we're done
2311 */
Chris Masone02119d2008-09-05 16:13:11 -04002312 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002313 prepare_to_wait(&root->log_commit_wait[index],
2314 &wait, TASK_UNINTERRUPTIBLE);
2315 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002316
2317 if (root->fs_info->last_trans_log_full_commit !=
2318 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002319 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002320 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002321
Yan Zheng7237f182009-01-21 12:54:03 -05002322 finish_wait(&root->log_commit_wait[index], &wait);
2323 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002324 } while (root->fs_info->last_trans_log_full_commit !=
2325 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002326 atomic_read(&root->log_commit[index]));
2327 return 0;
2328}
2329
Jeff Mahoney143bede2012-03-01 14:56:26 +01002330static void wait_for_writer(struct btrfs_trans_handle *trans,
2331 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002332{
2333 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002334 while (root->fs_info->last_trans_log_full_commit !=
2335 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002336 prepare_to_wait(&root->log_writer_wait,
2337 &wait, TASK_UNINTERRUPTIBLE);
2338 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002339 if (root->fs_info->last_trans_log_full_commit !=
2340 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002341 schedule();
2342 mutex_lock(&root->log_mutex);
2343 finish_wait(&root->log_writer_wait, &wait);
2344 }
Chris Masone02119d2008-09-05 16:13:11 -04002345}
2346
2347/*
2348 * btrfs_sync_log does sends a given tree log down to the disk and
2349 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002350 * you know that any inodes previously logged are safely on disk only
2351 * if it returns 0.
2352 *
2353 * Any other return value means you need to call btrfs_commit_transaction.
2354 * Some of the edge cases for fsyncing directories that have had unlinks
2355 * or renames done in the past mean that sometimes the only safe
2356 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2357 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002358 */
2359int btrfs_sync_log(struct btrfs_trans_handle *trans,
2360 struct btrfs_root *root)
2361{
Yan Zheng7237f182009-01-21 12:54:03 -05002362 int index1;
2363 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002364 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002365 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002366 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002367 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002368 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002369 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002370
Yan Zheng7237f182009-01-21 12:54:03 -05002371 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002372 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002373 index1 = root->log_transid % 2;
2374 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002375 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002376 mutex_unlock(&root->log_mutex);
2377 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002378 }
Yan Zheng7237f182009-01-21 12:54:03 -05002379 atomic_set(&root->log_commit[index1], 1);
2380
2381 /* wait for previous tree log sync to complete */
2382 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002383 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002384 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002385 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002386 /* when we're on an ssd, just kick the log commit out */
2387 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002388 mutex_unlock(&root->log_mutex);
2389 schedule_timeout_uninterruptible(1);
2390 mutex_lock(&root->log_mutex);
2391 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002392 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002393 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002394 break;
2395 }
Chris Masond0c803c2008-09-11 16:17:57 -04002396
Chris Mason12fcfd22009-03-24 10:24:20 -04002397 /* bail out if we need to do a full commit */
2398 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2399 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002400 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002401 mutex_unlock(&root->log_mutex);
2402 goto out;
2403 }
2404
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002405 if (log_transid % 2 == 0)
2406 mark = EXTENT_DIRTY;
2407 else
2408 mark = EXTENT_NEW;
2409
Chris Mason690587d2009-10-13 13:29:19 -04002410 /* we start IO on all the marked extents here, but we don't actually
2411 * wait for them until later.
2412 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002413 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002414 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002415 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002416 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002417 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002418 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002419 mutex_unlock(&root->log_mutex);
2420 goto out;
2421 }
Yan Zheng7237f182009-01-21 12:54:03 -05002422
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002424
Yan Zheng7237f182009-01-21 12:54:03 -05002425 root->log_transid++;
2426 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002427 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002428 smp_mb();
2429 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002430 * IO has been started, blocks of the log tree have WRITTEN flag set
2431 * in their headers. new modifications of the log will be written to
2432 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002433 */
2434 mutex_unlock(&root->log_mutex);
2435
2436 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002437 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002438 atomic_inc(&log_root_tree->log_writers);
2439 mutex_unlock(&log_root_tree->log_mutex);
2440
2441 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002442
2443 mutex_lock(&log_root_tree->log_mutex);
2444 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2445 smp_mb();
2446 if (waitqueue_active(&log_root_tree->log_writer_wait))
2447 wake_up(&log_root_tree->log_writer_wait);
2448 }
2449
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002450 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002451 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002452 if (ret != -ENOSPC) {
2453 btrfs_abort_transaction(trans, root, ret);
2454 mutex_unlock(&log_root_tree->log_mutex);
2455 goto out;
2456 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002457 root->fs_info->last_trans_log_full_commit = trans->transid;
2458 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002459 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002460 mutex_unlock(&log_root_tree->log_mutex);
2461 ret = -EAGAIN;
2462 goto out;
2463 }
2464
Yan Zheng7237f182009-01-21 12:54:03 -05002465 index2 = log_root_tree->log_transid % 2;
2466 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002467 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002468 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002469 wait_log_commit(trans, log_root_tree,
2470 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002471 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002472 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002473 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002474 goto out;
2475 }
2476 atomic_set(&log_root_tree->log_commit[index2], 1);
2477
Chris Mason12fcfd22009-03-24 10:24:20 -04002478 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2479 wait_log_commit(trans, log_root_tree,
2480 log_root_tree->log_transid - 1);
2481 }
Yan Zheng7237f182009-01-21 12:54:03 -05002482
Chris Mason12fcfd22009-03-24 10:24:20 -04002483 wait_for_writer(trans, log_root_tree);
2484
2485 /*
2486 * now that we've moved on to the tree of log tree roots,
2487 * check the full commit flag again
2488 */
2489 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002490 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002491 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002492 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002493 mutex_unlock(&log_root_tree->log_mutex);
2494 ret = -EAGAIN;
2495 goto out_wake_log_root;
2496 }
Yan Zheng7237f182009-01-21 12:54:03 -05002497
Miao Xiec6adc9c2013-05-28 10:05:39 +00002498 ret = btrfs_write_marked_extents(log_root_tree,
2499 &log_root_tree->dirty_log_pages,
2500 EXTENT_DIRTY | EXTENT_NEW);
2501 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502 if (ret) {
2503 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002504 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505 mutex_unlock(&log_root_tree->log_mutex);
2506 goto out_wake_log_root;
2507 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002508 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002509 btrfs_wait_marked_extents(log_root_tree,
2510 &log_root_tree->dirty_log_pages,
2511 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002512 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002513
David Sterba6c417612011-04-13 15:41:04 +02002514 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002515 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002516 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002517 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002518
Yan Zheng7237f182009-01-21 12:54:03 -05002519 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002520 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002521
2522 mutex_unlock(&log_root_tree->log_mutex);
2523
2524 /*
2525 * nobody else is going to jump in and write the the ctree
2526 * super here because the log_commit atomic below is protecting
2527 * us. We must be called with a transaction handle pinning
2528 * the running transaction open, so a full commit can't hop
2529 * in and cause problems either.
2530 */
Arne Jansena2de7332011-03-08 14:14:00 +01002531 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002532 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002533 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002534 if (ret) {
2535 btrfs_abort_transaction(trans, root, ret);
2536 goto out_wake_log_root;
2537 }
Yan Zheng7237f182009-01-21 12:54:03 -05002538
Chris Mason257c62e2009-10-13 13:21:08 -04002539 mutex_lock(&root->log_mutex);
2540 if (root->last_log_commit < log_transid)
2541 root->last_log_commit = log_transid;
2542 mutex_unlock(&root->log_mutex);
2543
Chris Mason12fcfd22009-03-24 10:24:20 -04002544out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002545 atomic_set(&log_root_tree->log_commit[index2], 0);
2546 smp_mb();
2547 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2548 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002549out:
Yan Zheng7237f182009-01-21 12:54:03 -05002550 atomic_set(&root->log_commit[index1], 0);
2551 smp_mb();
2552 if (waitqueue_active(&root->log_commit_wait[index1]))
2553 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002554 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002555}
2556
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002557static void free_log_tree(struct btrfs_trans_handle *trans,
2558 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002559{
2560 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002561 u64 start;
2562 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002563 struct walk_control wc = {
2564 .free = 1,
2565 .process_func = process_one_buffer
2566 };
2567
Liu Bo33217192013-02-27 13:28:24 +00002568 if (trans) {
2569 ret = walk_log_tree(trans, log, &wc);
Josef Bacik36508602013-04-25 16:23:32 -04002570
2571 /* I don't think this can happen but just in case */
2572 if (ret)
2573 btrfs_abort_transaction(trans, log, ret);
Liu Bo33217192013-02-27 13:28:24 +00002574 }
Chris Masone02119d2008-09-05 16:13:11 -04002575
Chris Masond3977122009-01-05 21:25:51 -05002576 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002577 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002578 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2579 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002580 if (ret)
2581 break;
2582
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002583 clear_extent_bits(&log->dirty_log_pages, start, end,
2584 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002585 }
2586
Josef Bacik2ab28f32012-10-12 15:27:49 -04002587 /*
2588 * We may have short-circuited the log tree with the full commit logic
2589 * and left ordered extents on our list, so clear these out to keep us
2590 * from leaking inodes and memory.
2591 */
2592 btrfs_free_logged_extents(log, 0);
2593 btrfs_free_logged_extents(log, 1);
2594
Yan Zheng7237f182009-01-21 12:54:03 -05002595 free_extent_buffer(log->node);
2596 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002597}
2598
2599/*
2600 * free all the extents used by the tree log. This should be called
2601 * at commit time of the full transaction
2602 */
2603int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2604{
2605 if (root->log_root) {
2606 free_log_tree(trans, root->log_root);
2607 root->log_root = NULL;
2608 }
2609 return 0;
2610}
2611
2612int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2613 struct btrfs_fs_info *fs_info)
2614{
2615 if (fs_info->log_root_tree) {
2616 free_log_tree(trans, fs_info->log_root_tree);
2617 fs_info->log_root_tree = NULL;
2618 }
Chris Masone02119d2008-09-05 16:13:11 -04002619 return 0;
2620}
2621
2622/*
Chris Masone02119d2008-09-05 16:13:11 -04002623 * If both a file and directory are logged, and unlinks or renames are
2624 * mixed in, we have a few interesting corners:
2625 *
2626 * create file X in dir Y
2627 * link file X to X.link in dir Y
2628 * fsync file X
2629 * unlink file X but leave X.link
2630 * fsync dir Y
2631 *
2632 * After a crash we would expect only X.link to exist. But file X
2633 * didn't get fsync'd again so the log has back refs for X and X.link.
2634 *
2635 * We solve this by removing directory entries and inode backrefs from the
2636 * log when a file that was logged in the current transaction is
2637 * unlinked. Any later fsync will include the updated log entries, and
2638 * we'll be able to reconstruct the proper directory items from backrefs.
2639 *
2640 * This optimizations allows us to avoid relogging the entire inode
2641 * or the entire directory.
2642 */
2643int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2644 struct btrfs_root *root,
2645 const char *name, int name_len,
2646 struct inode *dir, u64 index)
2647{
2648 struct btrfs_root *log;
2649 struct btrfs_dir_item *di;
2650 struct btrfs_path *path;
2651 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002652 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002653 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002654 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002655
Chris Mason3a5f1d42008-09-11 15:53:37 -04002656 if (BTRFS_I(dir)->logged_trans < trans->transid)
2657 return 0;
2658
Chris Masone02119d2008-09-05 16:13:11 -04002659 ret = join_running_log_trans(root);
2660 if (ret)
2661 return 0;
2662
2663 mutex_lock(&BTRFS_I(dir)->log_mutex);
2664
2665 log = root->log_root;
2666 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002667 if (!path) {
2668 err = -ENOMEM;
2669 goto out_unlock;
2670 }
liubo2a29edc2011-01-26 06:22:08 +00002671
Li Zefan33345d012011-04-20 10:31:50 +08002672 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002673 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002674 if (IS_ERR(di)) {
2675 err = PTR_ERR(di);
2676 goto fail;
2677 }
2678 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002679 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2680 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002681 if (ret) {
2682 err = ret;
2683 goto fail;
2684 }
Chris Masone02119d2008-09-05 16:13:11 -04002685 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002686 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002687 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002688 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002689 if (IS_ERR(di)) {
2690 err = PTR_ERR(di);
2691 goto fail;
2692 }
2693 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002694 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2695 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002696 if (ret) {
2697 err = ret;
2698 goto fail;
2699 }
Chris Masone02119d2008-09-05 16:13:11 -04002700 }
2701
2702 /* update the directory size in the log to reflect the names
2703 * we have removed
2704 */
2705 if (bytes_del) {
2706 struct btrfs_key key;
2707
Li Zefan33345d012011-04-20 10:31:50 +08002708 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002709 key.offset = 0;
2710 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002711 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002712
2713 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002714 if (ret < 0) {
2715 err = ret;
2716 goto fail;
2717 }
Chris Masone02119d2008-09-05 16:13:11 -04002718 if (ret == 0) {
2719 struct btrfs_inode_item *item;
2720 u64 i_size;
2721
2722 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2723 struct btrfs_inode_item);
2724 i_size = btrfs_inode_size(path->nodes[0], item);
2725 if (i_size > bytes_del)
2726 i_size -= bytes_del;
2727 else
2728 i_size = 0;
2729 btrfs_set_inode_size(path->nodes[0], item, i_size);
2730 btrfs_mark_buffer_dirty(path->nodes[0]);
2731 } else
2732 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002733 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002734 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002735fail:
Chris Masone02119d2008-09-05 16:13:11 -04002736 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002737out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002738 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002739 if (ret == -ENOSPC) {
2740 root->fs_info->last_trans_log_full_commit = trans->transid;
2741 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002742 } else if (ret < 0)
2743 btrfs_abort_transaction(trans, root, ret);
2744
Chris Mason12fcfd22009-03-24 10:24:20 -04002745 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002746
Andi Kleen411fc6b2010-10-29 15:14:31 -04002747 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002748}
2749
2750/* see comments for btrfs_del_dir_entries_in_log */
2751int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *root,
2753 const char *name, int name_len,
2754 struct inode *inode, u64 dirid)
2755{
2756 struct btrfs_root *log;
2757 u64 index;
2758 int ret;
2759
Chris Mason3a5f1d42008-09-11 15:53:37 -04002760 if (BTRFS_I(inode)->logged_trans < trans->transid)
2761 return 0;
2762
Chris Masone02119d2008-09-05 16:13:11 -04002763 ret = join_running_log_trans(root);
2764 if (ret)
2765 return 0;
2766 log = root->log_root;
2767 mutex_lock(&BTRFS_I(inode)->log_mutex);
2768
Li Zefan33345d012011-04-20 10:31:50 +08002769 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002770 dirid, &index);
2771 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002772 if (ret == -ENOSPC) {
2773 root->fs_info->last_trans_log_full_commit = trans->transid;
2774 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002775 } else if (ret < 0 && ret != -ENOENT)
2776 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002777 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002778
Chris Masone02119d2008-09-05 16:13:11 -04002779 return ret;
2780}
2781
2782/*
2783 * creates a range item in the log for 'dirid'. first_offset and
2784 * last_offset tell us which parts of the key space the log should
2785 * be considered authoritative for.
2786 */
2787static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2788 struct btrfs_root *log,
2789 struct btrfs_path *path,
2790 int key_type, u64 dirid,
2791 u64 first_offset, u64 last_offset)
2792{
2793 int ret;
2794 struct btrfs_key key;
2795 struct btrfs_dir_log_item *item;
2796
2797 key.objectid = dirid;
2798 key.offset = first_offset;
2799 if (key_type == BTRFS_DIR_ITEM_KEY)
2800 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2801 else
2802 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2803 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002804 if (ret)
2805 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002806
2807 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2808 struct btrfs_dir_log_item);
2809 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2810 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002811 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002812 return 0;
2813}
2814
2815/*
2816 * log all the items included in the current transaction for a given
2817 * directory. This also creates the range items in the log tree required
2818 * to replay anything deleted before the fsync
2819 */
2820static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2821 struct btrfs_root *root, struct inode *inode,
2822 struct btrfs_path *path,
2823 struct btrfs_path *dst_path, int key_type,
2824 u64 min_offset, u64 *last_offset_ret)
2825{
2826 struct btrfs_key min_key;
2827 struct btrfs_key max_key;
2828 struct btrfs_root *log = root->log_root;
2829 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002830 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002831 int ret;
2832 int i;
2833 int nritems;
2834 u64 first_offset = min_offset;
2835 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002836 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002837
2838 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002839 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002840 max_key.offset = (u64)-1;
2841 max_key.type = key_type;
2842
Li Zefan33345d012011-04-20 10:31:50 +08002843 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002844 min_key.type = key_type;
2845 min_key.offset = min_offset;
2846
2847 path->keep_locks = 1;
2848
2849 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00002850 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002851
2852 /*
2853 * we didn't find anything from this transaction, see if there
2854 * is anything at all
2855 */
Li Zefan33345d012011-04-20 10:31:50 +08002856 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2857 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002858 min_key.type = key_type;
2859 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002860 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002861 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2862 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002863 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002864 return ret;
2865 }
Li Zefan33345d012011-04-20 10:31:50 +08002866 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002867
2868 /* if ret == 0 there are items for this type,
2869 * create a range to tell us the last key of this type.
2870 * otherwise, there are no items in this directory after
2871 * *min_offset, and we create a range to indicate that.
2872 */
2873 if (ret == 0) {
2874 struct btrfs_key tmp;
2875 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2876 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002877 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002878 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002879 }
2880 goto done;
2881 }
2882
2883 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002884 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002885 if (ret == 0) {
2886 struct btrfs_key tmp;
2887 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2888 if (key_type == tmp.type) {
2889 first_offset = tmp.offset;
2890 ret = overwrite_item(trans, log, dst_path,
2891 path->nodes[0], path->slots[0],
2892 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002893 if (ret) {
2894 err = ret;
2895 goto done;
2896 }
Chris Masone02119d2008-09-05 16:13:11 -04002897 }
2898 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002899 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002900
2901 /* find the first key from this transaction again */
2902 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2903 if (ret != 0) {
2904 WARN_ON(1);
2905 goto done;
2906 }
2907
2908 /*
2909 * we have a block from this transaction, log every item in it
2910 * from our directory
2911 */
Chris Masond3977122009-01-05 21:25:51 -05002912 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002913 struct btrfs_key tmp;
2914 src = path->nodes[0];
2915 nritems = btrfs_header_nritems(src);
2916 for (i = path->slots[0]; i < nritems; i++) {
2917 btrfs_item_key_to_cpu(src, &min_key, i);
2918
Li Zefan33345d012011-04-20 10:31:50 +08002919 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002920 goto done;
2921 ret = overwrite_item(trans, log, dst_path, src, i,
2922 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002923 if (ret) {
2924 err = ret;
2925 goto done;
2926 }
Chris Masone02119d2008-09-05 16:13:11 -04002927 }
2928 path->slots[0] = nritems;
2929
2930 /*
2931 * look ahead to the next item and see if it is also
2932 * from this directory and from this transaction
2933 */
2934 ret = btrfs_next_leaf(root, path);
2935 if (ret == 1) {
2936 last_offset = (u64)-1;
2937 goto done;
2938 }
2939 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002940 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002941 last_offset = (u64)-1;
2942 goto done;
2943 }
2944 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2945 ret = overwrite_item(trans, log, dst_path,
2946 path->nodes[0], path->slots[0],
2947 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002948 if (ret)
2949 err = ret;
2950 else
2951 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002952 goto done;
2953 }
2954 }
2955done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002956 btrfs_release_path(path);
2957 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002958
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002959 if (err == 0) {
2960 *last_offset_ret = last_offset;
2961 /*
2962 * insert the log range keys to indicate where the log
2963 * is valid
2964 */
2965 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002966 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002967 if (ret)
2968 err = ret;
2969 }
2970 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002971}
2972
2973/*
2974 * logging directories is very similar to logging inodes, We find all the items
2975 * from the current transaction and write them to the log.
2976 *
2977 * The recovery code scans the directory in the subvolume, and if it finds a
2978 * key in the range logged that is not present in the log tree, then it means
2979 * that dir entry was unlinked during the transaction.
2980 *
2981 * In order for that scan to work, we must include one key smaller than
2982 * the smallest logged by this transaction and one key larger than the largest
2983 * key logged by this transaction.
2984 */
2985static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2986 struct btrfs_root *root, struct inode *inode,
2987 struct btrfs_path *path,
2988 struct btrfs_path *dst_path)
2989{
2990 u64 min_key;
2991 u64 max_key;
2992 int ret;
2993 int key_type = BTRFS_DIR_ITEM_KEY;
2994
2995again:
2996 min_key = 0;
2997 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002998 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002999 ret = log_dir_items(trans, root, inode, path,
3000 dst_path, key_type, min_key,
3001 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003002 if (ret)
3003 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003004 if (max_key == (u64)-1)
3005 break;
3006 min_key = max_key + 1;
3007 }
3008
3009 if (key_type == BTRFS_DIR_ITEM_KEY) {
3010 key_type = BTRFS_DIR_INDEX_KEY;
3011 goto again;
3012 }
3013 return 0;
3014}
3015
3016/*
3017 * a helper function to drop items from the log before we relog an
3018 * inode. max_key_type indicates the highest item type to remove.
3019 * This cannot be run for file data extents because it does not
3020 * free the extents they point to.
3021 */
3022static int drop_objectid_items(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *log,
3024 struct btrfs_path *path,
3025 u64 objectid, int max_key_type)
3026{
3027 int ret;
3028 struct btrfs_key key;
3029 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003030 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003031
3032 key.objectid = objectid;
3033 key.type = max_key_type;
3034 key.offset = (u64)-1;
3035
Chris Masond3977122009-01-05 21:25:51 -05003036 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003037 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003038 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003039 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003040 break;
3041
3042 if (path->slots[0] == 0)
3043 break;
3044
3045 path->slots[0]--;
3046 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3047 path->slots[0]);
3048
3049 if (found_key.objectid != objectid)
3050 break;
3051
Josef Bacik18ec90d2012-09-28 11:56:28 -04003052 found_key.offset = 0;
3053 found_key.type = 0;
3054 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3055 &start_slot);
3056
3057 ret = btrfs_del_items(trans, log, path, start_slot,
3058 path->slots[0] - start_slot + 1);
3059 /*
3060 * If start slot isn't 0 then we don't need to re-search, we've
3061 * found the last guy with the objectid in this tree.
3062 */
3063 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003064 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003065 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003066 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003067 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003068 if (ret > 0)
3069 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003070 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003071}
3072
Josef Bacik94edf4a2012-09-25 14:56:25 -04003073static void fill_inode_item(struct btrfs_trans_handle *trans,
3074 struct extent_buffer *leaf,
3075 struct btrfs_inode_item *item,
3076 struct inode *inode, int log_inode_only)
3077{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003078 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003079
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003080 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003081
3082 if (log_inode_only) {
3083 /* set the generation to zero so the recover code
3084 * can tell the difference between an logging
3085 * just to say 'this inode exists' and a logging
3086 * to say 'update this inode with these values'
3087 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003088 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3089 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003090 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003091 btrfs_set_token_inode_generation(leaf, item,
3092 BTRFS_I(inode)->generation,
3093 &token);
3094 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003095 }
3096
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003097 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3098 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3099 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3100 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3101
3102 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3103 inode->i_atime.tv_sec, &token);
3104 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3105 inode->i_atime.tv_nsec, &token);
3106
3107 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3108 inode->i_mtime.tv_sec, &token);
3109 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3110 inode->i_mtime.tv_nsec, &token);
3111
3112 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3113 inode->i_ctime.tv_sec, &token);
3114 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3115 inode->i_ctime.tv_nsec, &token);
3116
3117 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3118 &token);
3119
3120 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3121 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3122 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3123 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3124 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003125}
3126
Josef Bacika95249b2012-10-11 16:17:34 -04003127static int log_inode_item(struct btrfs_trans_handle *trans,
3128 struct btrfs_root *log, struct btrfs_path *path,
3129 struct inode *inode)
3130{
3131 struct btrfs_inode_item *inode_item;
3132 struct btrfs_key key;
3133 int ret;
3134
3135 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3136 ret = btrfs_insert_empty_item(trans, log, path, &key,
3137 sizeof(*inode_item));
3138 if (ret && ret != -EEXIST)
3139 return ret;
3140 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3141 struct btrfs_inode_item);
3142 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3143 btrfs_release_path(path);
3144 return 0;
3145}
3146
Chris Mason31ff1cd2008-09-11 16:17:57 -04003147static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003148 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003149 struct btrfs_path *dst_path,
3150 struct extent_buffer *src,
3151 int start_slot, int nr, int inode_only)
3152{
3153 unsigned long src_offset;
3154 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003155 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003156 struct btrfs_file_extent_item *extent;
3157 struct btrfs_inode_item *inode_item;
3158 int ret;
3159 struct btrfs_key *ins_keys;
3160 u32 *ins_sizes;
3161 char *ins_data;
3162 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003163 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003164 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003165
3166 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003167
3168 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3169 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003170 if (!ins_data)
3171 return -ENOMEM;
3172
Chris Mason31ff1cd2008-09-11 16:17:57 -04003173 ins_sizes = (u32 *)ins_data;
3174 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3175
3176 for (i = 0; i < nr; i++) {
3177 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3178 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3179 }
3180 ret = btrfs_insert_empty_items(trans, log, dst_path,
3181 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003182 if (ret) {
3183 kfree(ins_data);
3184 return ret;
3185 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003186
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003187 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003188 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3189 dst_path->slots[0]);
3190
3191 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3192
Josef Bacik94edf4a2012-09-25 14:56:25 -04003193 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003194 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3195 dst_path->slots[0],
3196 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003197 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3198 inode, inode_only == LOG_INODE_EXISTS);
3199 } else {
3200 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3201 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003202 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003203
Chris Mason31ff1cd2008-09-11 16:17:57 -04003204 /* take a reference on file data extents so that truncates
3205 * or deletes of this inode don't have to relog the inode
3206 * again
3207 */
Liu Bod2794402012-08-29 01:07:56 -06003208 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3209 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003210 int found_type;
3211 extent = btrfs_item_ptr(src, start_slot + i,
3212 struct btrfs_file_extent_item);
3213
liubo8e531cd2011-05-06 10:36:09 +08003214 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3215 continue;
3216
Chris Mason31ff1cd2008-09-11 16:17:57 -04003217 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003218 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003219 u64 ds, dl, cs, cl;
3220 ds = btrfs_file_extent_disk_bytenr(src,
3221 extent);
3222 /* ds == 0 is a hole */
3223 if (ds == 0)
3224 continue;
3225
3226 dl = btrfs_file_extent_disk_num_bytes(src,
3227 extent);
3228 cs = btrfs_file_extent_offset(src, extent);
3229 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003230 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003231 if (btrfs_file_extent_compression(src,
3232 extent)) {
3233 cs = 0;
3234 cl = dl;
3235 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003236
3237 ret = btrfs_lookup_csums_range(
3238 log->fs_info->csum_root,
3239 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003240 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003241 if (ret) {
3242 btrfs_release_path(dst_path);
3243 kfree(ins_data);
3244 return ret;
3245 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003246 }
3247 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003248 }
3249
3250 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003251 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003252 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003253
3254 /*
3255 * we have to do this after the loop above to avoid changing the
3256 * log tree while trying to change the log tree.
3257 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003258 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003259 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003260 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3261 struct btrfs_ordered_sum,
3262 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003263 if (!ret)
3264 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003265 list_del(&sums->list);
3266 kfree(sums);
3267 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003268 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003269}
3270
Josef Bacik5dc562c2012-08-17 13:14:17 -04003271static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3272{
3273 struct extent_map *em1, *em2;
3274
3275 em1 = list_entry(a, struct extent_map, list);
3276 em2 = list_entry(b, struct extent_map, list);
3277
3278 if (em1->start < em2->start)
3279 return -1;
3280 else if (em1->start > em2->start)
3281 return 1;
3282 return 0;
3283}
3284
Josef Bacik5dc562c2012-08-17 13:14:17 -04003285static int log_one_extent(struct btrfs_trans_handle *trans,
3286 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003287 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003288{
3289 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003290 struct btrfs_file_extent_item *fi;
3291 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003292 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003293 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003294 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003295 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003296 u64 mod_start = em->mod_start;
3297 u64 mod_len = em->mod_len;
3298 u64 csum_offset;
3299 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003300 u64 extent_offset = em->start - em->orig_start;
3301 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003302 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003303 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003304 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003305
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003306 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3307 em->start + em->len, NULL, 0);
3308 if (ret)
3309 return ret;
3310
Josef Bacik70c8a912012-10-11 16:54:30 -04003311 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003312 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003313 key.objectid = btrfs_ino(inode);
3314 key.type = BTRFS_EXTENT_DATA_KEY;
3315 key.offset = em->start;
Josef Bacik70c8a912012-10-11 16:54:30 -04003316
3317 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003318 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003319 return ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003320 leaf = path->nodes[0];
3321 fi = btrfs_item_ptr(leaf, path->slots[0],
3322 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003323
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003324 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3325 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003326 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3327 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003328 btrfs_set_token_file_extent_type(leaf, fi,
3329 BTRFS_FILE_EXTENT_PREALLOC,
3330 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003331 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003332 btrfs_set_token_file_extent_type(leaf, fi,
3333 BTRFS_FILE_EXTENT_REG,
3334 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003335 if (em->block_start == 0)
3336 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003337 }
3338
Josef Bacik70c8a912012-10-11 16:54:30 -04003339 block_len = max(em->block_len, em->orig_block_len);
3340 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003341 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3342 em->block_start,
3343 &token);
3344 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3345 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003346 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003347 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3348 em->block_start -
3349 extent_offset, &token);
3350 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3351 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003352 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003353 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3354 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3355 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003356 }
3357
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003358 btrfs_set_token_file_extent_offset(leaf, fi,
3359 em->start - em->orig_start,
3360 &token);
3361 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003362 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003363 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3364 &token);
3365 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3366 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003367 btrfs_mark_buffer_dirty(leaf);
3368
Josef Bacik70c8a912012-10-11 16:54:30 -04003369 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003370 if (ret) {
3371 return ret;
3372 }
3373
3374 if (skip_csum)
3375 return 0;
3376
Liu Bo192000d2013-01-06 03:38:22 +00003377 if (em->compress_type) {
3378 csum_offset = 0;
3379 csum_len = block_len;
3380 }
3381
Josef Bacik2ab28f32012-10-12 15:27:49 -04003382 /*
3383 * First check and see if our csums are on our outstanding ordered
3384 * extents.
3385 */
3386again:
3387 spin_lock_irq(&log->log_extents_lock[index]);
3388 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3389 struct btrfs_ordered_sum *sum;
3390
3391 if (!mod_len)
3392 break;
3393
3394 if (ordered->inode != inode)
3395 continue;
3396
3397 if (ordered->file_offset + ordered->len <= mod_start ||
3398 mod_start + mod_len <= ordered->file_offset)
3399 continue;
3400
3401 /*
3402 * We are going to copy all the csums on this ordered extent, so
3403 * go ahead and adjust mod_start and mod_len in case this
3404 * ordered extent has already been logged.
3405 */
3406 if (ordered->file_offset > mod_start) {
3407 if (ordered->file_offset + ordered->len >=
3408 mod_start + mod_len)
3409 mod_len = ordered->file_offset - mod_start;
3410 /*
3411 * If we have this case
3412 *
3413 * |--------- logged extent ---------|
3414 * |----- ordered extent ----|
3415 *
3416 * Just don't mess with mod_start and mod_len, we'll
3417 * just end up logging more csums than we need and it
3418 * will be ok.
3419 */
3420 } else {
3421 if (ordered->file_offset + ordered->len <
3422 mod_start + mod_len) {
3423 mod_len = (mod_start + mod_len) -
3424 (ordered->file_offset + ordered->len);
3425 mod_start = ordered->file_offset +
3426 ordered->len;
3427 } else {
3428 mod_len = 0;
3429 }
3430 }
3431
3432 /*
3433 * To keep us from looping for the above case of an ordered
3434 * extent that falls inside of the logged extent.
3435 */
3436 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3437 &ordered->flags))
3438 continue;
3439 atomic_inc(&ordered->refs);
3440 spin_unlock_irq(&log->log_extents_lock[index]);
3441 /*
3442 * we've dropped the lock, we must either break or
3443 * start over after this.
3444 */
3445
3446 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3447
3448 list_for_each_entry(sum, &ordered->list, list) {
3449 ret = btrfs_csum_file_blocks(trans, log, sum);
3450 if (ret) {
3451 btrfs_put_ordered_extent(ordered);
3452 goto unlocked;
3453 }
3454 }
3455 btrfs_put_ordered_extent(ordered);
3456 goto again;
3457
3458 }
3459 spin_unlock_irq(&log->log_extents_lock[index]);
3460unlocked:
3461
3462 if (!mod_len || ret)
3463 return ret;
3464
3465 csum_offset = mod_start - em->start;
3466 csum_len = mod_len;
3467
Josef Bacik70c8a912012-10-11 16:54:30 -04003468 /* block start is already adjusted for the file extent offset. */
3469 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3470 em->block_start + csum_offset,
3471 em->block_start + csum_offset +
3472 csum_len - 1, &ordered_sums, 0);
3473 if (ret)
3474 return ret;
3475
3476 while (!list_empty(&ordered_sums)) {
3477 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3478 struct btrfs_ordered_sum,
3479 list);
3480 if (!ret)
3481 ret = btrfs_csum_file_blocks(trans, log, sums);
3482 list_del(&sums->list);
3483 kfree(sums);
3484 }
3485
3486 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003487}
3488
3489static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3490 struct btrfs_root *root,
3491 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003492 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003493{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003494 struct extent_map *em, *n;
3495 struct list_head extents;
3496 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3497 u64 test_gen;
3498 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003499 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003500
3501 INIT_LIST_HEAD(&extents);
3502
Josef Bacik5dc562c2012-08-17 13:14:17 -04003503 write_lock(&tree->lock);
3504 test_gen = root->fs_info->last_trans_committed;
3505
3506 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3507 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003508
3509 /*
3510 * Just an arbitrary number, this can be really CPU intensive
3511 * once we start getting a lot of extents, and really once we
3512 * have a bunch of extents we just want to commit since it will
3513 * be faster.
3514 */
3515 if (++num > 32768) {
3516 list_del_init(&tree->modified_extents);
3517 ret = -EFBIG;
3518 goto process;
3519 }
3520
Josef Bacik5dc562c2012-08-17 13:14:17 -04003521 if (em->generation <= test_gen)
3522 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003523 /* Need a ref to keep it from getting evicted from cache */
3524 atomic_inc(&em->refs);
3525 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003526 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003527 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003528 }
3529
3530 list_sort(NULL, &extents, extent_cmp);
3531
Josef Bacik2ab28f32012-10-12 15:27:49 -04003532process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003533 while (!list_empty(&extents)) {
3534 em = list_entry(extents.next, struct extent_map, list);
3535
3536 list_del_init(&em->list);
3537
3538 /*
3539 * If we had an error we just need to delete everybody from our
3540 * private list.
3541 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003542 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003543 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003544 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003545 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003546 }
3547
3548 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003549
Josef Bacik70c8a912012-10-11 16:54:30 -04003550 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003551 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003552 clear_em_logging(tree, em);
3553 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003554 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003555 WARN_ON(!list_empty(&extents));
3556 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003557
Josef Bacik5dc562c2012-08-17 13:14:17 -04003558 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003559 return ret;
3560}
3561
Chris Masone02119d2008-09-05 16:13:11 -04003562/* log a single inode in the tree log.
3563 * At least one parent directory for this inode must exist in the tree
3564 * or be logged already.
3565 *
3566 * Any items from this inode changed by the current transaction are copied
3567 * to the log tree. An extra reference is taken on any extents in this
3568 * file, allowing us to avoid a whole pile of corner cases around logging
3569 * blocks that have been removed from the tree.
3570 *
3571 * See LOG_INODE_ALL and related defines for a description of what inode_only
3572 * does.
3573 *
3574 * This handles both files and directories.
3575 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003576static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003577 struct btrfs_root *root, struct inode *inode,
3578 int inode_only)
3579{
3580 struct btrfs_path *path;
3581 struct btrfs_path *dst_path;
3582 struct btrfs_key min_key;
3583 struct btrfs_key max_key;
3584 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003585 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003586 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003587 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003588 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003589 int ins_start_slot = 0;
3590 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003591 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003592 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003593
Chris Masone02119d2008-09-05 16:13:11 -04003594 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003595 if (!path)
3596 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003597 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003598 if (!dst_path) {
3599 btrfs_free_path(path);
3600 return -ENOMEM;
3601 }
Chris Masone02119d2008-09-05 16:13:11 -04003602
Li Zefan33345d012011-04-20 10:31:50 +08003603 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003604 min_key.type = BTRFS_INODE_ITEM_KEY;
3605 min_key.offset = 0;
3606
Li Zefan33345d012011-04-20 10:31:50 +08003607 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003608
Chris Mason12fcfd22009-03-24 10:24:20 -04003609
Josef Bacik5dc562c2012-08-17 13:14:17 -04003610 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003611 if (S_ISDIR(inode->i_mode) ||
3612 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3613 &BTRFS_I(inode)->runtime_flags) &&
3614 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003615 max_key.type = BTRFS_XATTR_ITEM_KEY;
3616 else
3617 max_key.type = (u8)-1;
3618 max_key.offset = (u64)-1;
3619
Josef Bacik94edf4a2012-09-25 14:56:25 -04003620 /* Only run delayed items if we are a dir or a new file */
3621 if (S_ISDIR(inode->i_mode) ||
3622 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3623 ret = btrfs_commit_inode_delayed_items(trans, inode);
3624 if (ret) {
3625 btrfs_free_path(path);
3626 btrfs_free_path(dst_path);
3627 return ret;
3628 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003629 }
3630
Chris Masone02119d2008-09-05 16:13:11 -04003631 mutex_lock(&BTRFS_I(inode)->log_mutex);
3632
Josef Bacik2ab28f32012-10-12 15:27:49 -04003633 btrfs_get_logged_extents(log, inode);
3634
Chris Masone02119d2008-09-05 16:13:11 -04003635 /*
3636 * a brute force approach to making sure we get the most uptodate
3637 * copies of everything.
3638 */
3639 if (S_ISDIR(inode->i_mode)) {
3640 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3641
3642 if (inode_only == LOG_INODE_EXISTS)
3643 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003644 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003645 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003646 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3647 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003648 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3649 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003650 ret = btrfs_truncate_inode_items(trans, log,
3651 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003652 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3653 &BTRFS_I(inode)->runtime_flags)) {
3654 if (inode_only == LOG_INODE_ALL)
3655 fast_search = true;
3656 max_key.type = BTRFS_XATTR_ITEM_KEY;
3657 ret = drop_objectid_items(trans, log, path, ino,
3658 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003659 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003660 if (inode_only == LOG_INODE_ALL)
3661 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003662 ret = log_inode_item(trans, log, dst_path, inode);
3663 if (ret) {
3664 err = ret;
3665 goto out_unlock;
3666 }
3667 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003668 }
Josef Bacika95249b2012-10-11 16:17:34 -04003669
Chris Masone02119d2008-09-05 16:13:11 -04003670 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003671 if (ret) {
3672 err = ret;
3673 goto out_unlock;
3674 }
Chris Masone02119d2008-09-05 16:13:11 -04003675 path->keep_locks = 1;
3676
Chris Masond3977122009-01-05 21:25:51 -05003677 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003678 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003679 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003680 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003681 if (ret != 0)
3682 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003683again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003684 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003685 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003686 break;
3687 if (min_key.type > max_key.type)
3688 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003689
Chris Masone02119d2008-09-05 16:13:11 -04003690 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003691 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3692 ins_nr++;
3693 goto next_slot;
3694 } else if (!ins_nr) {
3695 ins_start_slot = path->slots[0];
3696 ins_nr = 1;
3697 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003698 }
3699
Liu Bod2794402012-08-29 01:07:56 -06003700 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003701 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003702 if (ret) {
3703 err = ret;
3704 goto out_unlock;
3705 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003706 ins_nr = 1;
3707 ins_start_slot = path->slots[0];
3708next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003709
Chris Mason3a5f1d42008-09-11 15:53:37 -04003710 nritems = btrfs_header_nritems(path->nodes[0]);
3711 path->slots[0]++;
3712 if (path->slots[0] < nritems) {
3713 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3714 path->slots[0]);
3715 goto again;
3716 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003717 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003718 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003719 ins_start_slot,
3720 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003721 if (ret) {
3722 err = ret;
3723 goto out_unlock;
3724 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003725 ins_nr = 0;
3726 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003727 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003728
Chris Masone02119d2008-09-05 16:13:11 -04003729 if (min_key.offset < (u64)-1)
3730 min_key.offset++;
3731 else if (min_key.type < (u8)-1)
3732 min_key.type++;
3733 else if (min_key.objectid < (u64)-1)
3734 min_key.objectid++;
3735 else
3736 break;
3737 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003738 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003739 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003740 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003741 if (ret) {
3742 err = ret;
3743 goto out_unlock;
3744 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003745 ins_nr = 0;
3746 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003747
Josef Bacika95249b2012-10-11 16:17:34 -04003748log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003749 btrfs_release_path(path);
3750 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003751 if (fast_search) {
Josef Bacik70c8a912012-10-11 16:54:30 -04003752 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003753 if (ret) {
3754 err = ret;
3755 goto out_unlock;
3756 }
Liu Bo06d3d222012-08-27 10:52:19 -06003757 } else {
3758 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3759 struct extent_map *em, *n;
3760
Miao Xiebbe14262012-11-01 07:34:54 +00003761 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003762 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3763 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003764 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003765 }
3766
Chris Mason9623f9a2008-09-11 17:42:42 -04003767 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003768 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003769 if (ret) {
3770 err = ret;
3771 goto out_unlock;
3772 }
Chris Masone02119d2008-09-05 16:13:11 -04003773 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003774 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003775 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003776out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003777 if (err)
3778 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003779 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3780
3781 btrfs_free_path(path);
3782 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003783 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003784}
3785
Chris Mason12fcfd22009-03-24 10:24:20 -04003786/*
3787 * follow the dentry parent pointers up the chain and see if any
3788 * of the directories in it require a full commit before they can
3789 * be logged. Returns zero if nothing special needs to be done or 1 if
3790 * a full commit is required.
3791 */
3792static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3793 struct inode *inode,
3794 struct dentry *parent,
3795 struct super_block *sb,
3796 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003797{
Chris Mason12fcfd22009-03-24 10:24:20 -04003798 int ret = 0;
3799 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003800 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003801
Chris Masonaf4176b2009-03-24 10:24:31 -04003802 /*
3803 * for regular files, if its inode is already on disk, we don't
3804 * have to worry about the parents at all. This is because
3805 * we can use the last_unlink_trans field to record renames
3806 * and other fun in this file.
3807 */
3808 if (S_ISREG(inode->i_mode) &&
3809 BTRFS_I(inode)->generation <= last_committed &&
3810 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3811 goto out;
3812
Chris Mason12fcfd22009-03-24 10:24:20 -04003813 if (!S_ISDIR(inode->i_mode)) {
3814 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3815 goto out;
3816 inode = parent->d_inode;
3817 }
3818
3819 while (1) {
3820 BTRFS_I(inode)->logged_trans = trans->transid;
3821 smp_mb();
3822
3823 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3824 root = BTRFS_I(inode)->root;
3825
3826 /*
3827 * make sure any commits to the log are forced
3828 * to be full commits
3829 */
3830 root->fs_info->last_trans_log_full_commit =
3831 trans->transid;
3832 ret = 1;
3833 break;
3834 }
3835
3836 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3837 break;
3838
Yan, Zheng76dda932009-09-21 16:00:26 -04003839 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003840 break;
3841
Josef Bacik6a912212010-11-20 09:48:00 +00003842 parent = dget_parent(parent);
3843 dput(old_parent);
3844 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003845 inode = parent->d_inode;
3846
3847 }
Josef Bacik6a912212010-11-20 09:48:00 +00003848 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003849out:
Chris Masone02119d2008-09-05 16:13:11 -04003850 return ret;
3851}
3852
3853/*
3854 * helper function around btrfs_log_inode to make sure newly created
3855 * parent directories also end up in the log. A minimal inode and backref
3856 * only logging is done of any parent directories that are older than
3857 * the last committed transaction
3858 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003859static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root, struct inode *inode,
3861 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003862{
Chris Mason12fcfd22009-03-24 10:24:20 -04003863 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003864 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003865 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003866 int ret = 0;
3867 u64 last_committed = root->fs_info->last_trans_committed;
3868
3869 sb = inode->i_sb;
3870
Sage Weil3a5e1402009-04-02 16:49:40 -04003871 if (btrfs_test_opt(root, NOTREELOG)) {
3872 ret = 1;
3873 goto end_no_trans;
3874 }
3875
Chris Mason12fcfd22009-03-24 10:24:20 -04003876 if (root->fs_info->last_trans_log_full_commit >
3877 root->fs_info->last_trans_committed) {
3878 ret = 1;
3879 goto end_no_trans;
3880 }
3881
Yan, Zheng76dda932009-09-21 16:00:26 -04003882 if (root != BTRFS_I(inode)->root ||
3883 btrfs_root_refs(&root->root_item) == 0) {
3884 ret = 1;
3885 goto end_no_trans;
3886 }
3887
Chris Mason12fcfd22009-03-24 10:24:20 -04003888 ret = check_parent_dirs_for_sync(trans, inode, parent,
3889 sb, last_committed);
3890 if (ret)
3891 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003892
Josef Bacik22ee6982012-05-29 16:57:49 -04003893 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003894 ret = BTRFS_NO_LOG_SYNC;
3895 goto end_no_trans;
3896 }
3897
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003898 ret = start_log_trans(trans, root);
3899 if (ret)
3900 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003901
3902 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003903 if (ret)
3904 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003905
Chris Masonaf4176b2009-03-24 10:24:31 -04003906 /*
3907 * for regular files, if its inode is already on disk, we don't
3908 * have to worry about the parents at all. This is because
3909 * we can use the last_unlink_trans field to record renames
3910 * and other fun in this file.
3911 */
3912 if (S_ISREG(inode->i_mode) &&
3913 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003914 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3915 ret = 0;
3916 goto end_trans;
3917 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003918
3919 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003920 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003921 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003922 break;
3923
Chris Mason12fcfd22009-03-24 10:24:20 -04003924 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003925 if (root != BTRFS_I(inode)->root)
3926 break;
3927
Chris Mason12fcfd22009-03-24 10:24:20 -04003928 if (BTRFS_I(inode)->generation >
3929 root->fs_info->last_trans_committed) {
3930 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003931 if (ret)
3932 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003933 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003934 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003935 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003936
Josef Bacik6a912212010-11-20 09:48:00 +00003937 parent = dget_parent(parent);
3938 dput(old_parent);
3939 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003940 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003941 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003942end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003943 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003944 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003945 root->fs_info->last_trans_log_full_commit = trans->transid;
3946 ret = 1;
3947 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003948 btrfs_end_log_trans(root);
3949end_no_trans:
3950 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003951}
3952
3953/*
3954 * it is not safe to log dentry if the chunk root has added new
3955 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3956 * If this returns 1, you must commit the transaction to safely get your
3957 * data on disk.
3958 */
3959int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3960 struct btrfs_root *root, struct dentry *dentry)
3961{
Josef Bacik6a912212010-11-20 09:48:00 +00003962 struct dentry *parent = dget_parent(dentry);
3963 int ret;
3964
3965 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3966 dput(parent);
3967
3968 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003969}
3970
3971/*
3972 * should be called during mount to recover any replay any log trees
3973 * from the FS
3974 */
3975int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3976{
3977 int ret;
3978 struct btrfs_path *path;
3979 struct btrfs_trans_handle *trans;
3980 struct btrfs_key key;
3981 struct btrfs_key found_key;
3982 struct btrfs_key tmp_key;
3983 struct btrfs_root *log;
3984 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3985 struct walk_control wc = {
3986 .process_func = process_one_buffer,
3987 .stage = 0,
3988 };
3989
Chris Masone02119d2008-09-05 16:13:11 -04003990 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003991 if (!path)
3992 return -ENOMEM;
3993
3994 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003995
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003996 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 if (IS_ERR(trans)) {
3998 ret = PTR_ERR(trans);
3999 goto error;
4000 }
Chris Masone02119d2008-09-05 16:13:11 -04004001
4002 wc.trans = trans;
4003 wc.pin = 1;
4004
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004005 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004006 if (ret) {
4007 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4008 "recovering log root tree.");
4009 goto error;
4010 }
Chris Masone02119d2008-09-05 16:13:11 -04004011
4012again:
4013 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4014 key.offset = (u64)-1;
4015 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4016
Chris Masond3977122009-01-05 21:25:51 -05004017 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004018 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004019
4020 if (ret < 0) {
4021 btrfs_error(fs_info, ret,
4022 "Couldn't find tree log root.");
4023 goto error;
4024 }
Chris Masone02119d2008-09-05 16:13:11 -04004025 if (ret > 0) {
4026 if (path->slots[0] == 0)
4027 break;
4028 path->slots[0]--;
4029 }
4030 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4031 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004032 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4034 break;
4035
Miao Xiecb517ea2013-05-15 07:48:19 +00004036 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004037 if (IS_ERR(log)) {
4038 ret = PTR_ERR(log);
4039 btrfs_error(fs_info, ret,
4040 "Couldn't read tree log root.");
4041 goto error;
4042 }
Chris Masone02119d2008-09-05 16:13:11 -04004043
4044 tmp_key.objectid = found_key.offset;
4045 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4046 tmp_key.offset = (u64)-1;
4047
4048 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004049 if (IS_ERR(wc.replay_dest)) {
4050 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004051 free_extent_buffer(log->node);
4052 free_extent_buffer(log->commit_root);
4053 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004054 btrfs_error(fs_info, ret, "Couldn't read target root "
4055 "for tree log recovery.");
4056 goto error;
4057 }
Chris Masone02119d2008-09-05 16:13:11 -04004058
Yan Zheng07d400a2009-01-06 11:42:00 -05004059 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004060 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004061 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004062
Josef Bacikb50c6e22013-04-25 15:55:30 -04004063 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004064 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4065 path);
Chris Masone02119d2008-09-05 16:13:11 -04004066 }
Chris Masone02119d2008-09-05 16:13:11 -04004067
4068 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004069 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004070 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004071 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004072 kfree(log);
4073
Josef Bacikb50c6e22013-04-25 15:55:30 -04004074 if (ret)
4075 goto error;
4076
Chris Masone02119d2008-09-05 16:13:11 -04004077 if (found_key.offset == 0)
4078 break;
4079 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004080 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004081
4082 /* step one is to pin it all, step two is to replay just inodes */
4083 if (wc.pin) {
4084 wc.pin = 0;
4085 wc.process_func = replay_one_buffer;
4086 wc.stage = LOG_WALK_REPLAY_INODES;
4087 goto again;
4088 }
4089 /* step three is to replay everything */
4090 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4091 wc.stage++;
4092 goto again;
4093 }
4094
4095 btrfs_free_path(path);
4096
Josef Bacikabefa552013-04-24 16:40:05 -04004097 /* step 4: commit the transaction, which also unpins the blocks */
4098 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4099 if (ret)
4100 return ret;
4101
Chris Masone02119d2008-09-05 16:13:11 -04004102 free_extent_buffer(log_root_tree->node);
4103 log_root_tree->log_root = NULL;
4104 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004105 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106
Josef Bacikabefa552013-04-24 16:40:05 -04004107 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004109 if (wc.trans)
4110 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 btrfs_free_path(path);
4112 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004113}
Chris Mason12fcfd22009-03-24 10:24:20 -04004114
4115/*
4116 * there are some corner cases where we want to force a full
4117 * commit instead of allowing a directory to be logged.
4118 *
4119 * They revolve around files there were unlinked from the directory, and
4120 * this function updates the parent directory so that a full commit is
4121 * properly done if it is fsync'd later after the unlinks are done.
4122 */
4123void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4124 struct inode *dir, struct inode *inode,
4125 int for_rename)
4126{
4127 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004128 * when we're logging a file, if it hasn't been renamed
4129 * or unlinked, and its inode is fully committed on disk,
4130 * we don't have to worry about walking up the directory chain
4131 * to log its parents.
4132 *
4133 * So, we use the last_unlink_trans field to put this transid
4134 * into the file. When the file is logged we check it and
4135 * don't log the parents if the file is fully on disk.
4136 */
4137 if (S_ISREG(inode->i_mode))
4138 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4139
4140 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004141 * if this directory was already logged any new
4142 * names for this file/dir will get recorded
4143 */
4144 smp_mb();
4145 if (BTRFS_I(dir)->logged_trans == trans->transid)
4146 return;
4147
4148 /*
4149 * if the inode we're about to unlink was logged,
4150 * the log will be properly updated for any new names
4151 */
4152 if (BTRFS_I(inode)->logged_trans == trans->transid)
4153 return;
4154
4155 /*
4156 * when renaming files across directories, if the directory
4157 * there we're unlinking from gets fsync'd later on, there's
4158 * no way to find the destination directory later and fsync it
4159 * properly. So, we have to be conservative and force commits
4160 * so the new name gets discovered.
4161 */
4162 if (for_rename)
4163 goto record;
4164
4165 /* we can safely do the unlink without any special recording */
4166 return;
4167
4168record:
4169 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4170}
4171
4172/*
4173 * Call this after adding a new name for a file and it will properly
4174 * update the log to reflect the new name.
4175 *
4176 * It will return zero if all goes well, and it will return 1 if a
4177 * full transaction commit is required.
4178 */
4179int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4180 struct inode *inode, struct inode *old_dir,
4181 struct dentry *parent)
4182{
4183 struct btrfs_root * root = BTRFS_I(inode)->root;
4184
4185 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004186 * this will force the logging code to walk the dentry chain
4187 * up for the file
4188 */
4189 if (S_ISREG(inode->i_mode))
4190 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4191
4192 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004193 * if this inode hasn't been logged and directory we're renaming it
4194 * from hasn't been logged, we don't need to log it
4195 */
4196 if (BTRFS_I(inode)->logged_trans <=
4197 root->fs_info->last_trans_committed &&
4198 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4199 root->fs_info->last_trans_committed))
4200 return 0;
4201
4202 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4203}
4204