blob: d45c04b3069074ab438ad094bb44efca980e06a1 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Chris Masone02119d2008-09-05 16:13:11 -040023#include "ctree.h"
24#include "transaction.h"
25#include "disk-io.h"
26#include "locking.h"
27#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "backref.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050030#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070031#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040032
33/* magic values for the inode_only field in btrfs_log_inode:
34 *
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
37 * during log replay
38 */
39#define LOG_INODE_ALL 0
40#define LOG_INODE_EXISTS 1
41
42/*
Chris Mason12fcfd22009-03-24 10:24:20 -040043 * directory trouble cases
44 *
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
49 *
50 * mkdir foo/some_dir
51 * normal commit
52 * rename foo/some_dir foo2/some_dir
53 * mkdir foo/some_dir
54 * fsync foo/some_dir/some_file
55 *
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
59 *
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
62 *
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
66 *
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
69 *
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
72 *
73 * mkdir f1/foo
74 * normal commit
75 * rm -rf f1/foo
76 * fsync(f1)
77 *
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
82 * ugly details.
83 */
84
85/*
Chris Masone02119d2008-09-05 16:13:11 -040086 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
90 *
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
93 */
94#define LOG_WALK_PIN_ONLY 0
95#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040096#define LOG_WALK_REPLAY_DIR_INDEX 2
97#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040098
Chris Mason12fcfd22009-03-24 10:24:20 -040099static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -0400100 struct btrfs_root *root, struct inode *inode,
101 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500102static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400105static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_root *log,
108 struct btrfs_path *path,
109 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400110
111/*
112 * tree logging is a special write ahead log used to make sure that
113 * fsyncs and O_SYNCs can happen without doing full tree commits.
114 *
115 * Full tree commits are expensive because they require commonly
116 * modified blocks to be recowed, creating many dirty pages in the
117 * extent tree an 4x-6x higher write load than ext3.
118 *
119 * Instead of doing a tree commit on every fsync, we use the
120 * key ranges and transaction ids to find items for a given file or directory
121 * that have changed in this transaction. Those items are copied into
122 * a special tree (one per subvolume root), that tree is written to disk
123 * and then the fsync is considered complete.
124 *
125 * After a crash, items are copied out of the log-tree back into the
126 * subvolume tree. Any file data extents found are recorded in the extent
127 * allocation tree, and the log-tree freed.
128 *
129 * The log tree is read three times, once to pin down all the extents it is
130 * using in ram and once, once to create all the inodes logged in the tree
131 * and once to do all the other items.
132 */
133
134/*
Chris Masone02119d2008-09-05 16:13:11 -0400135 * start a sub transaction and setup the log tree
136 * this increments the log tree writer count to make the people
137 * syncing the tree wait for us to finish
138 */
139static int start_log_trans(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root)
141{
142 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400143 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400147 if (!root->log_start_pid) {
148 root->log_start_pid = current->pid;
149 root->log_multiple_pids = false;
150 } else if (root->log_start_pid != current->pid) {
151 root->log_multiple_pids = true;
152 }
153
Miao Xie2ecb7922012-09-06 04:04:27 -0600154 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500155 atomic_inc(&root->log_writers);
156 mutex_unlock(&root->log_mutex);
157 return 0;
158 }
Josef Bacikff782e02009-10-08 15:30:04 -0400159 root->log_multiple_pids = false;
160 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400161 mutex_lock(&root->fs_info->tree_log_mutex);
162 if (!root->fs_info->log_root_tree) {
163 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400167 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400168 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400169 if (ret)
170 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400171 }
Chris Masone02119d2008-09-05 16:13:11 -0400172 mutex_unlock(&root->fs_info->tree_log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -0600173 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500174 atomic_inc(&root->log_writers);
175 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400176 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400177}
178
179/*
180 * returns 0 if there was a log transaction running and we were able
181 * to join, or returns -ENOENT if there were not transactions
182 * in progress
183 */
184static int join_running_log_trans(struct btrfs_root *root)
185{
186 int ret = -ENOENT;
187
188 smp_mb();
189 if (!root->log_root)
190 return -ENOENT;
191
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 if (root->log_root) {
194 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500195 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400196 }
Yan Zheng7237f182009-01-21 12:54:03 -0500197 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400198 return ret;
199}
200
201/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400202 * This either makes the current running log transaction wait
203 * until you call btrfs_end_log_trans() or it makes any future
204 * log transactions wait until you call btrfs_end_log_trans()
205 */
206int btrfs_pin_log_trans(struct btrfs_root *root)
207{
208 int ret = -ENOENT;
209
210 mutex_lock(&root->log_mutex);
211 atomic_inc(&root->log_writers);
212 mutex_unlock(&root->log_mutex);
213 return ret;
214}
215
216/*
Chris Masone02119d2008-09-05 16:13:11 -0400217 * indicate we're done making changes to the log tree
218 * and wake up anyone waiting to do a sync
219 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100220void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400221{
Yan Zheng7237f182009-01-21 12:54:03 -0500222 if (atomic_dec_and_test(&root->log_writers)) {
223 smp_mb();
224 if (waitqueue_active(&root->log_writer_wait))
225 wake_up(&root->log_writer_wait);
226 }
Chris Masone02119d2008-09-05 16:13:11 -0400227}
228
229
230/*
231 * the walk control struct is used to pass state down the chain when
232 * processing the log tree. The stage field tells us which part
233 * of the log tree processing we are currently doing. The others
234 * are state fields used for that specific part
235 */
236struct walk_control {
237 /* should we free the extent on disk when done? This is used
238 * at transaction commit time while freeing a log tree
239 */
240 int free;
241
242 /* should we write out the extent buffer? This is used
243 * while flushing the log tree to disk during a sync
244 */
245 int write;
246
247 /* should we wait for the extent buffer io to finish? Also used
248 * while flushing the log tree to disk for a sync
249 */
250 int wait;
251
252 /* pin only walk, we record which extents on disk belong to the
253 * log trees
254 */
255 int pin;
256
257 /* what stage of the replay code we're currently in */
258 int stage;
259
260 /* the root we are currently replaying */
261 struct btrfs_root *replay_dest;
262
263 /* the trans handle for the current replay */
264 struct btrfs_trans_handle *trans;
265
266 /* the function that gets used to process blocks we find in the
267 * tree. Note the extent_buffer might not be up to date when it is
268 * passed in, and it must be checked or read if you need the data
269 * inside it
270 */
271 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
272 struct walk_control *wc, u64 gen);
273};
274
275/*
276 * process_func used to pin down extents, write them or wait on them
277 */
278static int process_one_buffer(struct btrfs_root *log,
279 struct extent_buffer *eb,
280 struct walk_control *wc, u64 gen)
281{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400282 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400283
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400284 /*
285 * If this fs is mixed then we need to be able to process the leaves to
286 * pin down any logged extents, so we have to read the block.
287 */
288 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
289 ret = btrfs_read_buffer(eb, gen);
290 if (ret)
291 return ret;
292 }
293
Josef Bacikb50c6e22013-04-25 15:55:30 -0400294 if (wc->pin)
295 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
296 eb->start, eb->len);
297
298 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400299 if (wc->pin && btrfs_header_level(eb) == 0)
300 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400301 if (wc->write)
302 btrfs_write_tree_block(eb);
303 if (wc->wait)
304 btrfs_wait_tree_block_writeback(eb);
305 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400306 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400307}
308
309/*
310 * Item overwrite used by replay and tree logging. eb, slot and key all refer
311 * to the src data we are copying out.
312 *
313 * root is the tree we are copying into, and path is a scratch
314 * path for use in this function (it should be released on entry and
315 * will be released on exit).
316 *
317 * If the key is already in the destination tree the existing item is
318 * overwritten. If the existing item isn't big enough, it is extended.
319 * If it is too large, it is truncated.
320 *
321 * If the key isn't in the destination yet, a new item is inserted.
322 */
323static noinline int overwrite_item(struct btrfs_trans_handle *trans,
324 struct btrfs_root *root,
325 struct btrfs_path *path,
326 struct extent_buffer *eb, int slot,
327 struct btrfs_key *key)
328{
329 int ret;
330 u32 item_size;
331 u64 saved_i_size = 0;
332 int save_old_i_size = 0;
333 unsigned long src_ptr;
334 unsigned long dst_ptr;
335 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000336 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400337
338 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
339 overwrite_root = 1;
340
341 item_size = btrfs_item_size_nr(eb, slot);
342 src_ptr = btrfs_item_ptr_offset(eb, slot);
343
344 /* look for the key in the destination tree */
345 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000346 if (ret < 0)
347 return ret;
348
Chris Masone02119d2008-09-05 16:13:11 -0400349 if (ret == 0) {
350 char *src_copy;
351 char *dst_copy;
352 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
353 path->slots[0]);
354 if (dst_size != item_size)
355 goto insert;
356
357 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200358 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400359 return 0;
360 }
361 dst_copy = kmalloc(item_size, GFP_NOFS);
362 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000363 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200364 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000365 kfree(dst_copy);
366 kfree(src_copy);
367 return -ENOMEM;
368 }
Chris Masone02119d2008-09-05 16:13:11 -0400369
370 read_extent_buffer(eb, src_copy, src_ptr, item_size);
371
372 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
373 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
374 item_size);
375 ret = memcmp(dst_copy, src_copy, item_size);
376
377 kfree(dst_copy);
378 kfree(src_copy);
379 /*
380 * they have the same contents, just return, this saves
381 * us from cowing blocks in the destination tree and doing
382 * extra writes that may not have been done by a previous
383 * sync
384 */
385 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200386 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400387 return 0;
388 }
389
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000390 /*
391 * We need to load the old nbytes into the inode so when we
392 * replay the extents we've logged we get the right nbytes.
393 */
394 if (inode_item) {
395 struct btrfs_inode_item *item;
396 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400397 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000398
399 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
400 struct btrfs_inode_item);
401 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
402 item = btrfs_item_ptr(eb, slot,
403 struct btrfs_inode_item);
404 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400405
406 /*
407 * If this is a directory we need to reset the i_size to
408 * 0 so that we can set it up properly when replaying
409 * the rest of the items in this log.
410 */
411 mode = btrfs_inode_mode(eb, item);
412 if (S_ISDIR(mode))
413 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000414 }
415 } else if (inode_item) {
416 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400417 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000418
419 /*
420 * New inode, set nbytes to 0 so that the nbytes comes out
421 * properly when we replay the extents.
422 */
423 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
424 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400425
426 /*
427 * If this is a directory we need to reset the i_size to 0 so
428 * that we can set it up properly when replaying the rest of
429 * the items in this log.
430 */
431 mode = btrfs_inode_mode(eb, item);
432 if (S_ISDIR(mode))
433 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400434 }
435insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200436 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400437 /* try to insert the key into the destination tree */
438 ret = btrfs_insert_empty_item(trans, root, path,
439 key, item_size);
440
441 /* make sure any existing item is the correct size */
442 if (ret == -EEXIST) {
443 u32 found_size;
444 found_size = btrfs_item_size_nr(path->nodes[0],
445 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100446 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000447 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100448 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000449 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100450 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400451 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400452 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400453 }
454 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
455 path->slots[0]);
456
457 /* don't overwrite an existing inode if the generation number
458 * was logged as zero. This is done when the tree logging code
459 * is just logging an inode to make sure it exists after recovery.
460 *
461 * Also, don't overwrite i_size on directories during replay.
462 * log replay inserts and removes directory items based on the
463 * state of the tree found in the subvolume, and i_size is modified
464 * as it goes
465 */
466 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
467 struct btrfs_inode_item *src_item;
468 struct btrfs_inode_item *dst_item;
469
470 src_item = (struct btrfs_inode_item *)src_ptr;
471 dst_item = (struct btrfs_inode_item *)dst_ptr;
472
473 if (btrfs_inode_generation(eb, src_item) == 0)
474 goto no_copy;
475
476 if (overwrite_root &&
477 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
478 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
479 save_old_i_size = 1;
480 saved_i_size = btrfs_inode_size(path->nodes[0],
481 dst_item);
482 }
483 }
484
485 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
486 src_ptr, item_size);
487
488 if (save_old_i_size) {
489 struct btrfs_inode_item *dst_item;
490 dst_item = (struct btrfs_inode_item *)dst_ptr;
491 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
492 }
493
494 /* make sure the generation is filled in */
495 if (key->type == BTRFS_INODE_ITEM_KEY) {
496 struct btrfs_inode_item *dst_item;
497 dst_item = (struct btrfs_inode_item *)dst_ptr;
498 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
499 btrfs_set_inode_generation(path->nodes[0], dst_item,
500 trans->transid);
501 }
502 }
503no_copy:
504 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200505 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400506 return 0;
507}
508
509/*
510 * simple helper to read an inode off the disk from a given root
511 * This can only be called for subvolume roots and not for the log
512 */
513static noinline struct inode *read_one_inode(struct btrfs_root *root,
514 u64 objectid)
515{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400517 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400518
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400519 key.objectid = objectid;
520 key.type = BTRFS_INODE_ITEM_KEY;
521 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000522 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400523 if (IS_ERR(inode)) {
524 inode = NULL;
525 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400526 iput(inode);
527 inode = NULL;
528 }
529 return inode;
530}
531
532/* replays a single extent in 'eb' at 'slot' with 'key' into the
533 * subvolume 'root'. path is released on entry and should be released
534 * on exit.
535 *
536 * extents in the log tree have not been allocated out of the extent
537 * tree yet. So, this completes the allocation, taking a reference
538 * as required if the extent already exists or creating a new extent
539 * if it isn't in the extent allocation tree yet.
540 *
541 * The extent is inserted into the file, dropping any existing extents
542 * from the file that overlap the new one.
543 */
544static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root,
546 struct btrfs_path *path,
547 struct extent_buffer *eb, int slot,
548 struct btrfs_key *key)
549{
550 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400551 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400552 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000553 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400554 struct btrfs_file_extent_item *item;
555 struct inode *inode = NULL;
556 unsigned long size;
557 int ret = 0;
558
559 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
560 found_type = btrfs_file_extent_type(eb, item);
561
Yan Zhengd899e052008-10-30 14:25:28 -0400562 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000563 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
564 nbytes = btrfs_file_extent_num_bytes(eb, item);
565 extent_end = start + nbytes;
566
567 /*
568 * We don't add to the inodes nbytes if we are prealloc or a
569 * hole.
570 */
571 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
572 nbytes = 0;
573 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 size = btrfs_file_extent_inline_len(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000575 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000576 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400577 } else {
578 ret = 0;
579 goto out;
580 }
581
582 inode = read_one_inode(root, key->objectid);
583 if (!inode) {
584 ret = -EIO;
585 goto out;
586 }
587
588 /*
589 * first check to see if we already have this extent in the
590 * file. This must be done before the btrfs_drop_extents run
591 * so we don't try to drop this extent.
592 */
Li Zefan33345d012011-04-20 10:31:50 +0800593 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400594 start, 0);
595
Yan Zhengd899e052008-10-30 14:25:28 -0400596 if (ret == 0 &&
597 (found_type == BTRFS_FILE_EXTENT_REG ||
598 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400599 struct btrfs_file_extent_item cmp1;
600 struct btrfs_file_extent_item cmp2;
601 struct btrfs_file_extent_item *existing;
602 struct extent_buffer *leaf;
603
604 leaf = path->nodes[0];
605 existing = btrfs_item_ptr(leaf, path->slots[0],
606 struct btrfs_file_extent_item);
607
608 read_extent_buffer(eb, &cmp1, (unsigned long)item,
609 sizeof(cmp1));
610 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
611 sizeof(cmp2));
612
613 /*
614 * we already have a pointer to this exact extent,
615 * we don't have to do anything
616 */
617 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200618 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400619 goto out;
620 }
621 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200622 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400623
624 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400625 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400626 if (ret)
627 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400628
Yan Zheng07d400a2009-01-06 11:42:00 -0500629 if (found_type == BTRFS_FILE_EXTENT_REG ||
630 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400631 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500632 unsigned long dest_offset;
633 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400634
Yan Zheng07d400a2009-01-06 11:42:00 -0500635 ret = btrfs_insert_empty_item(trans, root, path, key,
636 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400637 if (ret)
638 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500639 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
640 path->slots[0]);
641 copy_extent_buffer(path->nodes[0], eb, dest_offset,
642 (unsigned long)item, sizeof(*item));
643
644 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
645 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
646 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400647 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500648
649 if (ins.objectid > 0) {
650 u64 csum_start;
651 u64 csum_end;
652 LIST_HEAD(ordered_sums);
653 /*
654 * is this extent already allocated in the extent
655 * allocation tree? If so, just add a reference
656 */
657 ret = btrfs_lookup_extent(root, ins.objectid,
658 ins.offset);
659 if (ret == 0) {
660 ret = btrfs_inc_extent_ref(trans, root,
661 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200663 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400664 if (ret)
665 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 } else {
667 /*
668 * insert the extent pointer in the extent
669 * allocation tree
670 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671 ret = btrfs_alloc_logged_file_extent(trans,
672 root, root->root_key.objectid,
673 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400674 if (ret)
675 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500676 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200677 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500678
679 if (btrfs_file_extent_compression(eb, item)) {
680 csum_start = ins.objectid;
681 csum_end = csum_start + ins.offset;
682 } else {
683 csum_start = ins.objectid +
684 btrfs_file_extent_offset(eb, item);
685 csum_end = csum_start +
686 btrfs_file_extent_num_bytes(eb, item);
687 }
688
689 ret = btrfs_lookup_csums_range(root->log_root,
690 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100691 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400692 if (ret)
693 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500694 while (!list_empty(&ordered_sums)) {
695 struct btrfs_ordered_sum *sums;
696 sums = list_entry(ordered_sums.next,
697 struct btrfs_ordered_sum,
698 list);
Josef Bacik36508602013-04-25 16:23:32 -0400699 if (!ret)
700 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500701 root->fs_info->csum_root,
702 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500703 list_del(&sums->list);
704 kfree(sums);
705 }
Josef Bacik36508602013-04-25 16:23:32 -0400706 if (ret)
707 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200709 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 }
711 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
712 /* inline extents are easy, we just overwrite them */
713 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400714 if (ret)
715 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500716 }
717
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000718 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600719 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400720out:
721 if (inode)
722 iput(inode);
723 return ret;
724}
725
726/*
727 * when cleaning up conflicts between the directory names in the
728 * subvolume, directory names in the log and directory names in the
729 * inode back references, we may have to unlink inodes from directories.
730 *
731 * This is a helper function to do the unlink of a specific directory
732 * item
733 */
734static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root,
736 struct btrfs_path *path,
737 struct inode *dir,
738 struct btrfs_dir_item *di)
739{
740 struct inode *inode;
741 char *name;
742 int name_len;
743 struct extent_buffer *leaf;
744 struct btrfs_key location;
745 int ret;
746
747 leaf = path->nodes[0];
748
749 btrfs_dir_item_key_to_cpu(leaf, di, &location);
750 name_len = btrfs_dir_name_len(leaf, di);
751 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000752 if (!name)
753 return -ENOMEM;
754
Chris Masone02119d2008-09-05 16:13:11 -0400755 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200756 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400757
758 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000759 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400760 ret = -EIO;
761 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000762 }
Chris Masone02119d2008-09-05 16:13:11 -0400763
Yan Zhengec051c02009-01-05 15:43:42 -0500764 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400765 if (ret)
766 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400767
Chris Masone02119d2008-09-05 16:13:11 -0400768 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400769 if (ret)
770 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100771 else
772 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400773out:
774 kfree(name);
775 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400776 return ret;
777}
778
779/*
780 * helper function to see if a given name and sequence number found
781 * in an inode back reference are already in a directory and correctly
782 * point to this inode
783 */
784static noinline int inode_in_dir(struct btrfs_root *root,
785 struct btrfs_path *path,
786 u64 dirid, u64 objectid, u64 index,
787 const char *name, int name_len)
788{
789 struct btrfs_dir_item *di;
790 struct btrfs_key location;
791 int match = 0;
792
793 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
794 index, name, name_len, 0);
795 if (di && !IS_ERR(di)) {
796 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
797 if (location.objectid != objectid)
798 goto out;
799 } else
800 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200801 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400802
803 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
804 if (di && !IS_ERR(di)) {
805 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
806 if (location.objectid != objectid)
807 goto out;
808 } else
809 goto out;
810 match = 1;
811out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200812 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400813 return match;
814}
815
816/*
817 * helper function to check a log tree for a named back reference in
818 * an inode. This is used to decide if a back reference that is
819 * found in the subvolume conflicts with what we find in the log.
820 *
821 * inode backreferences may have multiple refs in a single item,
822 * during replay we process one reference at a time, and we don't
823 * want to delete valid links to a file from the subvolume if that
824 * link is also in the log.
825 */
826static noinline int backref_in_log(struct btrfs_root *log,
827 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700828 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400829 char *name, int namelen)
830{
831 struct btrfs_path *path;
832 struct btrfs_inode_ref *ref;
833 unsigned long ptr;
834 unsigned long ptr_end;
835 unsigned long name_ptr;
836 int found_name_len;
837 int item_size;
838 int ret;
839 int match = 0;
840
841 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000842 if (!path)
843 return -ENOMEM;
844
Chris Masone02119d2008-09-05 16:13:11 -0400845 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
846 if (ret != 0)
847 goto out;
848
Chris Masone02119d2008-09-05 16:13:11 -0400849 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700850
851 if (key->type == BTRFS_INODE_EXTREF_KEY) {
852 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
853 name, namelen, NULL))
854 match = 1;
855
856 goto out;
857 }
858
859 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400860 ptr_end = ptr + item_size;
861 while (ptr < ptr_end) {
862 ref = (struct btrfs_inode_ref *)ptr;
863 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
864 if (found_name_len == namelen) {
865 name_ptr = (unsigned long)(ref + 1);
866 ret = memcmp_extent_buffer(path->nodes[0], name,
867 name_ptr, namelen);
868 if (ret == 0) {
869 match = 1;
870 goto out;
871 }
872 }
873 ptr = (unsigned long)(ref + 1) + found_name_len;
874 }
875out:
876 btrfs_free_path(path);
877 return match;
878}
879
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700880static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
881 struct btrfs_root *root,
882 struct btrfs_path *path,
883 struct btrfs_root *log_root,
884 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700885 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700886 u64 inode_objectid, u64 parent_objectid,
887 u64 ref_index, char *name, int namelen,
888 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700889{
890 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700891 char *victim_name;
892 int victim_name_len;
893 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700894 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700895 struct btrfs_key search_key;
896 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700897
Mark Fashehf1863732012-08-08 11:32:27 -0700898again:
899 /* Search old style refs */
900 search_key.objectid = inode_objectid;
901 search_key.type = BTRFS_INODE_REF_KEY;
902 search_key.offset = parent_objectid;
903 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700904 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700905 struct btrfs_inode_ref *victim_ref;
906 unsigned long ptr;
907 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700908
909 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700910
911 /* are we trying to overwrite a back ref for the root directory
912 * if so, just jump out, we're done
913 */
Mark Fashehf1863732012-08-08 11:32:27 -0700914 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700915 return 1;
916
917 /* check all the names in this back reference to see
918 * if they are in the log. if so, we allow them to stay
919 * otherwise they must be unlinked as a conflict
920 */
921 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
922 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
923 while (ptr < ptr_end) {
924 victim_ref = (struct btrfs_inode_ref *)ptr;
925 victim_name_len = btrfs_inode_ref_name_len(leaf,
926 victim_ref);
927 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400928 if (!victim_name)
929 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700930
931 read_extent_buffer(leaf, victim_name,
932 (unsigned long)(victim_ref + 1),
933 victim_name_len);
934
Mark Fashehf1863732012-08-08 11:32:27 -0700935 if (!backref_in_log(log_root, &search_key,
936 parent_objectid,
937 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700938 victim_name_len)) {
939 btrfs_inc_nlink(inode);
940 btrfs_release_path(path);
941
942 ret = btrfs_unlink_inode(trans, root, dir,
943 inode, victim_name,
944 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700945 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400946 if (ret)
947 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100948 ret = btrfs_run_delayed_items(trans, root);
949 if (ret)
950 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700951 *search_done = 1;
952 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700953 }
954 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700955
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700956 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
957 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700958
959 /*
960 * NOTE: we have searched root tree and checked the
961 * coresponding ref, it does not need to check again.
962 */
963 *search_done = 1;
964 }
965 btrfs_release_path(path);
966
Mark Fashehf1863732012-08-08 11:32:27 -0700967 /* Same search but for extended refs */
968 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
969 inode_objectid, parent_objectid, 0,
970 0);
971 if (!IS_ERR_OR_NULL(extref)) {
972 u32 item_size;
973 u32 cur_offset = 0;
974 unsigned long base;
975 struct inode *victim_parent;
976
977 leaf = path->nodes[0];
978
979 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
980 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
981
982 while (cur_offset < item_size) {
983 extref = (struct btrfs_inode_extref *)base + cur_offset;
984
985 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
986
987 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
988 goto next;
989
990 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400991 if (!victim_name)
992 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -0700993 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
994 victim_name_len);
995
996 search_key.objectid = inode_objectid;
997 search_key.type = BTRFS_INODE_EXTREF_KEY;
998 search_key.offset = btrfs_extref_hash(parent_objectid,
999 victim_name,
1000 victim_name_len);
1001 ret = 0;
1002 if (!backref_in_log(log_root, &search_key,
1003 parent_objectid, victim_name,
1004 victim_name_len)) {
1005 ret = -ENOENT;
1006 victim_parent = read_one_inode(root,
1007 parent_objectid);
1008 if (victim_parent) {
1009 btrfs_inc_nlink(inode);
1010 btrfs_release_path(path);
1011
1012 ret = btrfs_unlink_inode(trans, root,
1013 victim_parent,
1014 inode,
1015 victim_name,
1016 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001017 if (!ret)
1018 ret = btrfs_run_delayed_items(
1019 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001020 }
Mark Fashehf1863732012-08-08 11:32:27 -07001021 iput(victim_parent);
1022 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001023 if (ret)
1024 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001025 *search_done = 1;
1026 goto again;
1027 }
1028 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001029 if (ret)
1030 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001031next:
1032 cur_offset += victim_name_len + sizeof(*extref);
1033 }
1034 *search_done = 1;
1035 }
1036 btrfs_release_path(path);
1037
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001038 /* look for a conflicting sequence number */
1039 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001040 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001041 if (di && !IS_ERR(di)) {
1042 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001043 if (ret)
1044 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001045 }
1046 btrfs_release_path(path);
1047
1048 /* look for a conflicing name */
1049 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1050 name, namelen, 0);
1051 if (di && !IS_ERR(di)) {
1052 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001053 if (ret)
1054 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 }
1056 btrfs_release_path(path);
1057
1058 return 0;
1059}
Chris Masone02119d2008-09-05 16:13:11 -04001060
Mark Fashehf1863732012-08-08 11:32:27 -07001061static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1062 u32 *namelen, char **name, u64 *index,
1063 u64 *parent_objectid)
1064{
1065 struct btrfs_inode_extref *extref;
1066
1067 extref = (struct btrfs_inode_extref *)ref_ptr;
1068
1069 *namelen = btrfs_inode_extref_name_len(eb, extref);
1070 *name = kmalloc(*namelen, GFP_NOFS);
1071 if (*name == NULL)
1072 return -ENOMEM;
1073
1074 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1075 *namelen);
1076
1077 *index = btrfs_inode_extref_index(eb, extref);
1078 if (parent_objectid)
1079 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1080
1081 return 0;
1082}
1083
1084static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1085 u32 *namelen, char **name, u64 *index)
1086{
1087 struct btrfs_inode_ref *ref;
1088
1089 ref = (struct btrfs_inode_ref *)ref_ptr;
1090
1091 *namelen = btrfs_inode_ref_name_len(eb, ref);
1092 *name = kmalloc(*namelen, GFP_NOFS);
1093 if (*name == NULL)
1094 return -ENOMEM;
1095
1096 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1097
1098 *index = btrfs_inode_ref_index(eb, ref);
1099
1100 return 0;
1101}
1102
Chris Masone02119d2008-09-05 16:13:11 -04001103/*
1104 * replay one inode back reference item found in the log tree.
1105 * eb, slot and key refer to the buffer and key found in the log tree.
1106 * root is the destination we are replaying into, and path is for temp
1107 * use by this function. (it should be released on return).
1108 */
1109static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root,
1111 struct btrfs_root *log,
1112 struct btrfs_path *path,
1113 struct extent_buffer *eb, int slot,
1114 struct btrfs_key *key)
1115{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001116 struct inode *dir = NULL;
1117 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001118 unsigned long ref_ptr;
1119 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001120 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001121 int namelen;
1122 int ret;
liuboc622ae62011-03-26 08:01:12 -04001123 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001124 int log_ref_ver = 0;
1125 u64 parent_objectid;
1126 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001127 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001128 int ref_struct_size;
1129
1130 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1131 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1132
1133 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1134 struct btrfs_inode_extref *r;
1135
1136 ref_struct_size = sizeof(struct btrfs_inode_extref);
1137 log_ref_ver = 1;
1138 r = (struct btrfs_inode_extref *)ref_ptr;
1139 parent_objectid = btrfs_inode_extref_parent(eb, r);
1140 } else {
1141 ref_struct_size = sizeof(struct btrfs_inode_ref);
1142 parent_objectid = key->offset;
1143 }
1144 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001145
Chris Masone02119d2008-09-05 16:13:11 -04001146 /*
1147 * it is possible that we didn't log all the parent directories
1148 * for a given inode. If we don't find the dir, just don't
1149 * copy the back ref in. The link count fixup code will take
1150 * care of the rest
1151 */
Mark Fashehf1863732012-08-08 11:32:27 -07001152 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001153 if (!dir) {
1154 ret = -ENOENT;
1155 goto out;
1156 }
Chris Masone02119d2008-09-05 16:13:11 -04001157
Mark Fashehf1863732012-08-08 11:32:27 -07001158 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001159 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001160 ret = -EIO;
1161 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001162 }
Chris Masone02119d2008-09-05 16:13:11 -04001163
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001164 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001165 if (log_ref_ver) {
1166 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1167 &ref_index, &parent_objectid);
1168 /*
1169 * parent object can change from one array
1170 * item to another.
1171 */
1172 if (!dir)
1173 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001174 if (!dir) {
1175 ret = -ENOENT;
1176 goto out;
1177 }
Mark Fashehf1863732012-08-08 11:32:27 -07001178 } else {
1179 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1180 &ref_index);
1181 }
1182 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001183 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001184
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001185 /* if we already have a perfect match, we're done */
1186 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001187 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001188 /*
1189 * look for a conflicting back reference in the
1190 * metadata. if we find one we have to unlink that name
1191 * of the file before we add our new link. Later on, we
1192 * overwrite any existing back reference, and we don't
1193 * want to create dangling pointers in the directory.
1194 */
Chris Masone02119d2008-09-05 16:13:11 -04001195
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001196 if (!search_done) {
1197 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001198 dir, inode, eb,
1199 inode_objectid,
1200 parent_objectid,
1201 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001202 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001203 if (ret) {
1204 if (ret == 1)
1205 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001206 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001207 }
Chris Masone02119d2008-09-05 16:13:11 -04001208 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001209
1210 /* insert our name */
1211 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001212 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001213 if (ret)
1214 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001215
1216 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001217 }
liuboc622ae62011-03-26 08:01:12 -04001218
Mark Fashehf1863732012-08-08 11:32:27 -07001219 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001220 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001221 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001222 if (log_ref_ver) {
1223 iput(dir);
1224 dir = NULL;
1225 }
Chris Masone02119d2008-09-05 16:13:11 -04001226 }
Chris Masone02119d2008-09-05 16:13:11 -04001227
1228 /* finally write the back reference in the inode */
1229 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001230out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001231 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001232 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001233 iput(dir);
1234 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001235 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001236}
1237
Yan, Zhengc71bf092009-11-12 09:34:40 +00001238static int insert_orphan_item(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root, u64 offset)
1240{
1241 int ret;
1242 ret = btrfs_find_orphan_item(root, offset);
1243 if (ret > 0)
1244 ret = btrfs_insert_orphan_item(trans, root, offset);
1245 return ret;
1246}
1247
Mark Fashehf1863732012-08-08 11:32:27 -07001248static int count_inode_extrefs(struct btrfs_root *root,
1249 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001250{
Mark Fashehf1863732012-08-08 11:32:27 -07001251 int ret = 0;
1252 int name_len;
1253 unsigned int nlink = 0;
1254 u32 item_size;
1255 u32 cur_offset = 0;
1256 u64 inode_objectid = btrfs_ino(inode);
1257 u64 offset = 0;
1258 unsigned long ptr;
1259 struct btrfs_inode_extref *extref;
1260 struct extent_buffer *leaf;
1261
1262 while (1) {
1263 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1264 &extref, &offset);
1265 if (ret)
1266 break;
1267
1268 leaf = path->nodes[0];
1269 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1270 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1271
1272 while (cur_offset < item_size) {
1273 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1274 name_len = btrfs_inode_extref_name_len(leaf, extref);
1275
1276 nlink++;
1277
1278 cur_offset += name_len + sizeof(*extref);
1279 }
1280
1281 offset++;
1282 btrfs_release_path(path);
1283 }
1284 btrfs_release_path(path);
1285
1286 if (ret < 0)
1287 return ret;
1288 return nlink;
1289}
1290
1291static int count_inode_refs(struct btrfs_root *root,
1292 struct inode *inode, struct btrfs_path *path)
1293{
Chris Masone02119d2008-09-05 16:13:11 -04001294 int ret;
1295 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001296 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001297 unsigned long ptr;
1298 unsigned long ptr_end;
1299 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001300 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001301
Li Zefan33345d012011-04-20 10:31:50 +08001302 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001303 key.type = BTRFS_INODE_REF_KEY;
1304 key.offset = (u64)-1;
1305
Chris Masond3977122009-01-05 21:25:51 -05001306 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1308 if (ret < 0)
1309 break;
1310 if (ret > 0) {
1311 if (path->slots[0] == 0)
1312 break;
1313 path->slots[0]--;
1314 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001315process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001316 btrfs_item_key_to_cpu(path->nodes[0], &key,
1317 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001318 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001319 key.type != BTRFS_INODE_REF_KEY)
1320 break;
1321 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1322 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1323 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001324 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001325 struct btrfs_inode_ref *ref;
1326
1327 ref = (struct btrfs_inode_ref *)ptr;
1328 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1329 ref);
1330 ptr = (unsigned long)(ref + 1) + name_len;
1331 nlink++;
1332 }
1333
1334 if (key.offset == 0)
1335 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001336 if (path->slots[0] > 0) {
1337 path->slots[0]--;
1338 goto process_slot;
1339 }
Chris Masone02119d2008-09-05 16:13:11 -04001340 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001341 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001342 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001344
1345 return nlink;
1346}
1347
1348/*
1349 * There are a few corners where the link count of the file can't
1350 * be properly maintained during replay. So, instead of adding
1351 * lots of complexity to the log code, we just scan the backrefs
1352 * for any file that has been through replay.
1353 *
1354 * The scan will update the link count on the inode to reflect the
1355 * number of back refs found. If it goes down to zero, the iput
1356 * will free the inode.
1357 */
1358static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root,
1360 struct inode *inode)
1361{
1362 struct btrfs_path *path;
1363 int ret;
1364 u64 nlink = 0;
1365 u64 ino = btrfs_ino(inode);
1366
1367 path = btrfs_alloc_path();
1368 if (!path)
1369 return -ENOMEM;
1370
1371 ret = count_inode_refs(root, inode, path);
1372 if (ret < 0)
1373 goto out;
1374
1375 nlink = ret;
1376
1377 ret = count_inode_extrefs(root, inode, path);
1378 if (ret == -ENOENT)
1379 ret = 0;
1380
1381 if (ret < 0)
1382 goto out;
1383
1384 nlink += ret;
1385
1386 ret = 0;
1387
Chris Masone02119d2008-09-05 16:13:11 -04001388 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001389 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001390 btrfs_update_inode(trans, root, inode);
1391 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001392 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001393
Yan, Zhengc71bf092009-11-12 09:34:40 +00001394 if (inode->i_nlink == 0) {
1395 if (S_ISDIR(inode->i_mode)) {
1396 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001397 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001398 if (ret)
1399 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001400 }
Li Zefan33345d012011-04-20 10:31:50 +08001401 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001402 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001403
Mark Fashehf1863732012-08-08 11:32:27 -07001404out:
1405 btrfs_free_path(path);
1406 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001407}
1408
1409static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1410 struct btrfs_root *root,
1411 struct btrfs_path *path)
1412{
1413 int ret;
1414 struct btrfs_key key;
1415 struct inode *inode;
1416
1417 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1418 key.type = BTRFS_ORPHAN_ITEM_KEY;
1419 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001420 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001421 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1422 if (ret < 0)
1423 break;
1424
1425 if (ret == 1) {
1426 if (path->slots[0] == 0)
1427 break;
1428 path->slots[0]--;
1429 }
1430
1431 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1432 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1433 key.type != BTRFS_ORPHAN_ITEM_KEY)
1434 break;
1435
1436 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001437 if (ret)
1438 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001439
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001441 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001442 if (!inode)
1443 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001444
1445 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001446 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001447 if (ret)
1448 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001449
Chris Mason12fcfd22009-03-24 10:24:20 -04001450 /*
1451 * fixup on a directory may create new entries,
1452 * make sure we always look for the highset possible
1453 * offset
1454 */
1455 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001456 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001457 ret = 0;
1458out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001460 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001461}
1462
1463
1464/*
1465 * record a given inode in the fixup dir so we can check its link
1466 * count when replay is done. The link count is incremented here
1467 * so the inode won't go away until we check it
1468 */
1469static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1470 struct btrfs_root *root,
1471 struct btrfs_path *path,
1472 u64 objectid)
1473{
1474 struct btrfs_key key;
1475 int ret = 0;
1476 struct inode *inode;
1477
1478 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001479 if (!inode)
1480 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001481
1482 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1483 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1484 key.offset = objectid;
1485
1486 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1487
David Sterbab3b4aa72011-04-21 01:20:15 +02001488 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001489 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001490 if (!inode->i_nlink)
1491 set_nlink(inode, 1);
1492 else
1493 btrfs_inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001494 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001495 } else if (ret == -EEXIST) {
1496 ret = 0;
1497 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001498 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001499 }
1500 iput(inode);
1501
1502 return ret;
1503}
1504
1505/*
1506 * when replaying the log for a directory, we only insert names
1507 * for inodes that actually exist. This means an fsync on a directory
1508 * does not implicitly fsync all the new files in it
1509 */
1510static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1511 struct btrfs_root *root,
1512 struct btrfs_path *path,
1513 u64 dirid, u64 index,
1514 char *name, int name_len, u8 type,
1515 struct btrfs_key *location)
1516{
1517 struct inode *inode;
1518 struct inode *dir;
1519 int ret;
1520
1521 inode = read_one_inode(root, location->objectid);
1522 if (!inode)
1523 return -ENOENT;
1524
1525 dir = read_one_inode(root, dirid);
1526 if (!dir) {
1527 iput(inode);
1528 return -EIO;
1529 }
Josef Bacikd5554382013-09-11 14:17:00 -04001530
Chris Masone02119d2008-09-05 16:13:11 -04001531 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1532
1533 /* FIXME, put inode into FIXUP list */
1534
1535 iput(inode);
1536 iput(dir);
1537 return ret;
1538}
1539
1540/*
1541 * take a single entry in a log directory item and replay it into
1542 * the subvolume.
1543 *
1544 * if a conflicting item exists in the subdirectory already,
1545 * the inode it points to is unlinked and put into the link count
1546 * fix up tree.
1547 *
1548 * If a name from the log points to a file or directory that does
1549 * not exist in the FS, it is skipped. fsyncs on directories
1550 * do not force down inodes inside that directory, just changes to the
1551 * names or unlinks in a directory.
1552 */
1553static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1554 struct btrfs_root *root,
1555 struct btrfs_path *path,
1556 struct extent_buffer *eb,
1557 struct btrfs_dir_item *di,
1558 struct btrfs_key *key)
1559{
1560 char *name;
1561 int name_len;
1562 struct btrfs_dir_item *dst_di;
1563 struct btrfs_key found_key;
1564 struct btrfs_key log_key;
1565 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001566 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001567 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001568 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001569 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001570
1571 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001572 if (!dir)
1573 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001574
1575 name_len = btrfs_dir_name_len(eb, di);
1576 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001577 if (!name) {
1578 ret = -ENOMEM;
1579 goto out;
1580 }
liubo2a29edc2011-01-26 06:22:08 +00001581
Chris Masone02119d2008-09-05 16:13:11 -04001582 log_type = btrfs_dir_type(eb, di);
1583 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1584 name_len);
1585
1586 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001587 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1588 if (exists == 0)
1589 exists = 1;
1590 else
1591 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001592 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001593
Chris Masone02119d2008-09-05 16:13:11 -04001594 if (key->type == BTRFS_DIR_ITEM_KEY) {
1595 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1596 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001597 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001598 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1599 key->objectid,
1600 key->offset, name,
1601 name_len, 1);
1602 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001603 /* Corruption */
1604 ret = -EINVAL;
1605 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001606 }
David Sterbac7040052011-04-19 18:00:01 +02001607 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001608 /* we need a sequence number to insert, so we only
1609 * do inserts for the BTRFS_DIR_INDEX_KEY types
1610 */
1611 if (key->type != BTRFS_DIR_INDEX_KEY)
1612 goto out;
1613 goto insert;
1614 }
1615
1616 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1617 /* the existing item matches the logged item */
1618 if (found_key.objectid == log_key.objectid &&
1619 found_key.type == log_key.type &&
1620 found_key.offset == log_key.offset &&
1621 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1622 goto out;
1623 }
1624
1625 /*
1626 * don't drop the conflicting directory entry if the inode
1627 * for the new entry doesn't exist
1628 */
Chris Mason4bef0842008-09-08 11:18:08 -04001629 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001630 goto out;
1631
Chris Masone02119d2008-09-05 16:13:11 -04001632 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001633 if (ret)
1634 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001635
1636 if (key->type == BTRFS_DIR_INDEX_KEY)
1637 goto insert;
1638out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001639 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001640 if (!ret && update_size) {
1641 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1642 ret = btrfs_update_inode(trans, root, dir);
1643 }
Chris Masone02119d2008-09-05 16:13:11 -04001644 kfree(name);
1645 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001646 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001647
1648insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001650 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1651 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001652 if (ret && ret != -ENOENT)
1653 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001654 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001655 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001656 goto out;
1657}
1658
1659/*
1660 * find all the names in a directory item and reconcile them into
1661 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1662 * one name in a directory item, but the same code gets used for
1663 * both directory index types
1664 */
1665static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1666 struct btrfs_root *root,
1667 struct btrfs_path *path,
1668 struct extent_buffer *eb, int slot,
1669 struct btrfs_key *key)
1670{
1671 int ret;
1672 u32 item_size = btrfs_item_size_nr(eb, slot);
1673 struct btrfs_dir_item *di;
1674 int name_len;
1675 unsigned long ptr;
1676 unsigned long ptr_end;
1677
1678 ptr = btrfs_item_ptr_offset(eb, slot);
1679 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001680 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001681 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001682 if (verify_dir_item(root, eb, di))
1683 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001684 name_len = btrfs_dir_name_len(eb, di);
1685 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001686 if (ret)
1687 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001688 ptr = (unsigned long)(di + 1);
1689 ptr += name_len;
1690 }
1691 return 0;
1692}
1693
1694/*
1695 * directory replay has two parts. There are the standard directory
1696 * items in the log copied from the subvolume, and range items
1697 * created in the log while the subvolume was logged.
1698 *
1699 * The range items tell us which parts of the key space the log
1700 * is authoritative for. During replay, if a key in the subvolume
1701 * directory is in a logged range item, but not actually in the log
1702 * that means it was deleted from the directory before the fsync
1703 * and should be removed.
1704 */
1705static noinline int find_dir_range(struct btrfs_root *root,
1706 struct btrfs_path *path,
1707 u64 dirid, int key_type,
1708 u64 *start_ret, u64 *end_ret)
1709{
1710 struct btrfs_key key;
1711 u64 found_end;
1712 struct btrfs_dir_log_item *item;
1713 int ret;
1714 int nritems;
1715
1716 if (*start_ret == (u64)-1)
1717 return 1;
1718
1719 key.objectid = dirid;
1720 key.type = key_type;
1721 key.offset = *start_ret;
1722
1723 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1724 if (ret < 0)
1725 goto out;
1726 if (ret > 0) {
1727 if (path->slots[0] == 0)
1728 goto out;
1729 path->slots[0]--;
1730 }
1731 if (ret != 0)
1732 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1733
1734 if (key.type != key_type || key.objectid != dirid) {
1735 ret = 1;
1736 goto next;
1737 }
1738 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1739 struct btrfs_dir_log_item);
1740 found_end = btrfs_dir_log_end(path->nodes[0], item);
1741
1742 if (*start_ret >= key.offset && *start_ret <= found_end) {
1743 ret = 0;
1744 *start_ret = key.offset;
1745 *end_ret = found_end;
1746 goto out;
1747 }
1748 ret = 1;
1749next:
1750 /* check the next slot in the tree to see if it is a valid item */
1751 nritems = btrfs_header_nritems(path->nodes[0]);
1752 if (path->slots[0] >= nritems) {
1753 ret = btrfs_next_leaf(root, path);
1754 if (ret)
1755 goto out;
1756 } else {
1757 path->slots[0]++;
1758 }
1759
1760 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1761
1762 if (key.type != key_type || key.objectid != dirid) {
1763 ret = 1;
1764 goto out;
1765 }
1766 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1767 struct btrfs_dir_log_item);
1768 found_end = btrfs_dir_log_end(path->nodes[0], item);
1769 *start_ret = key.offset;
1770 *end_ret = found_end;
1771 ret = 0;
1772out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001773 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001774 return ret;
1775}
1776
1777/*
1778 * this looks for a given directory item in the log. If the directory
1779 * item is not in the log, the item is removed and the inode it points
1780 * to is unlinked
1781 */
1782static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1783 struct btrfs_root *root,
1784 struct btrfs_root *log,
1785 struct btrfs_path *path,
1786 struct btrfs_path *log_path,
1787 struct inode *dir,
1788 struct btrfs_key *dir_key)
1789{
1790 int ret;
1791 struct extent_buffer *eb;
1792 int slot;
1793 u32 item_size;
1794 struct btrfs_dir_item *di;
1795 struct btrfs_dir_item *log_di;
1796 int name_len;
1797 unsigned long ptr;
1798 unsigned long ptr_end;
1799 char *name;
1800 struct inode *inode;
1801 struct btrfs_key location;
1802
1803again:
1804 eb = path->nodes[0];
1805 slot = path->slots[0];
1806 item_size = btrfs_item_size_nr(eb, slot);
1807 ptr = btrfs_item_ptr_offset(eb, slot);
1808 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001809 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001810 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001811 if (verify_dir_item(root, eb, di)) {
1812 ret = -EIO;
1813 goto out;
1814 }
1815
Chris Masone02119d2008-09-05 16:13:11 -04001816 name_len = btrfs_dir_name_len(eb, di);
1817 name = kmalloc(name_len, GFP_NOFS);
1818 if (!name) {
1819 ret = -ENOMEM;
1820 goto out;
1821 }
1822 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1823 name_len);
1824 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001825 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001826 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1827 dir_key->objectid,
1828 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001829 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001830 log_di = btrfs_lookup_dir_index_item(trans, log,
1831 log_path,
1832 dir_key->objectid,
1833 dir_key->offset,
1834 name, name_len, 0);
1835 }
David Sterbac7040052011-04-19 18:00:01 +02001836 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001837 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001838 btrfs_release_path(path);
1839 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001840 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001841 if (!inode) {
1842 kfree(name);
1843 return -EIO;
1844 }
Chris Masone02119d2008-09-05 16:13:11 -04001845
1846 ret = link_to_fixup_dir(trans, root,
1847 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001848 if (ret) {
1849 kfree(name);
1850 iput(inode);
1851 goto out;
1852 }
1853
Chris Masone02119d2008-09-05 16:13:11 -04001854 btrfs_inc_nlink(inode);
1855 ret = btrfs_unlink_inode(trans, root, dir, inode,
1856 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001857 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001858 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001859 kfree(name);
1860 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001861 if (ret)
1862 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001863
1864 /* there might still be more names under this key
1865 * check and repeat if required
1866 */
1867 ret = btrfs_search_slot(NULL, root, dir_key, path,
1868 0, 0);
1869 if (ret == 0)
1870 goto again;
1871 ret = 0;
1872 goto out;
1873 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001874 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001875 kfree(name);
1876
1877 ptr = (unsigned long)(di + 1);
1878 ptr += name_len;
1879 }
1880 ret = 0;
1881out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001882 btrfs_release_path(path);
1883 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001884 return ret;
1885}
1886
1887/*
1888 * deletion replay happens before we copy any new directory items
1889 * out of the log or out of backreferences from inodes. It
1890 * scans the log to find ranges of keys that log is authoritative for,
1891 * and then scans the directory to find items in those ranges that are
1892 * not present in the log.
1893 *
1894 * Anything we don't find in the log is unlinked and removed from the
1895 * directory.
1896 */
1897static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root,
1899 struct btrfs_root *log,
1900 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001901 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001902{
1903 u64 range_start;
1904 u64 range_end;
1905 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1906 int ret = 0;
1907 struct btrfs_key dir_key;
1908 struct btrfs_key found_key;
1909 struct btrfs_path *log_path;
1910 struct inode *dir;
1911
1912 dir_key.objectid = dirid;
1913 dir_key.type = BTRFS_DIR_ITEM_KEY;
1914 log_path = btrfs_alloc_path();
1915 if (!log_path)
1916 return -ENOMEM;
1917
1918 dir = read_one_inode(root, dirid);
1919 /* it isn't an error if the inode isn't there, that can happen
1920 * because we replay the deletes before we copy in the inode item
1921 * from the log
1922 */
1923 if (!dir) {
1924 btrfs_free_path(log_path);
1925 return 0;
1926 }
1927again:
1928 range_start = 0;
1929 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001930 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001931 if (del_all)
1932 range_end = (u64)-1;
1933 else {
1934 ret = find_dir_range(log, path, dirid, key_type,
1935 &range_start, &range_end);
1936 if (ret != 0)
1937 break;
1938 }
Chris Masone02119d2008-09-05 16:13:11 -04001939
1940 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001941 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001942 int nritems;
1943 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1944 0, 0);
1945 if (ret < 0)
1946 goto out;
1947
1948 nritems = btrfs_header_nritems(path->nodes[0]);
1949 if (path->slots[0] >= nritems) {
1950 ret = btrfs_next_leaf(root, path);
1951 if (ret)
1952 break;
1953 }
1954 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1955 path->slots[0]);
1956 if (found_key.objectid != dirid ||
1957 found_key.type != dir_key.type)
1958 goto next_type;
1959
1960 if (found_key.offset > range_end)
1961 break;
1962
1963 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001964 log_path, dir,
1965 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001966 if (ret)
1967 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001968 if (found_key.offset == (u64)-1)
1969 break;
1970 dir_key.offset = found_key.offset + 1;
1971 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001972 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001973 if (range_end == (u64)-1)
1974 break;
1975 range_start = range_end + 1;
1976 }
1977
1978next_type:
1979 ret = 0;
1980 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1981 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1982 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001983 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001984 goto again;
1985 }
1986out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001987 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001988 btrfs_free_path(log_path);
1989 iput(dir);
1990 return ret;
1991}
1992
1993/*
1994 * the process_func used to replay items from the log tree. This
1995 * gets called in two different stages. The first stage just looks
1996 * for inodes and makes sure they are all copied into the subvolume.
1997 *
1998 * The second stage copies all the other item types from the log into
1999 * the subvolume. The two stage approach is slower, but gets rid of
2000 * lots of complexity around inodes referencing other inodes that exist
2001 * only in the log (references come from either directory items or inode
2002 * back refs).
2003 */
2004static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2005 struct walk_control *wc, u64 gen)
2006{
2007 int nritems;
2008 struct btrfs_path *path;
2009 struct btrfs_root *root = wc->replay_dest;
2010 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002011 int level;
2012 int i;
2013 int ret;
2014
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002015 ret = btrfs_read_buffer(eb, gen);
2016 if (ret)
2017 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002018
2019 level = btrfs_header_level(eb);
2020
2021 if (level != 0)
2022 return 0;
2023
2024 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002025 if (!path)
2026 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002027
2028 nritems = btrfs_header_nritems(eb);
2029 for (i = 0; i < nritems; i++) {
2030 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002031
2032 /* inode keys are done during the first stage */
2033 if (key.type == BTRFS_INODE_ITEM_KEY &&
2034 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002035 struct btrfs_inode_item *inode_item;
2036 u32 mode;
2037
2038 inode_item = btrfs_item_ptr(eb, i,
2039 struct btrfs_inode_item);
2040 mode = btrfs_inode_mode(eb, inode_item);
2041 if (S_ISDIR(mode)) {
2042 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002043 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002044 if (ret)
2045 break;
Chris Masone02119d2008-09-05 16:13:11 -04002046 }
2047 ret = overwrite_item(wc->trans, root, path,
2048 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002049 if (ret)
2050 break;
Chris Masone02119d2008-09-05 16:13:11 -04002051
Yan, Zhengc71bf092009-11-12 09:34:40 +00002052 /* for regular files, make sure corresponding
2053 * orhpan item exist. extents past the new EOF
2054 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002055 */
2056 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002057 ret = insert_orphan_item(wc->trans, root,
2058 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002059 if (ret)
2060 break;
Chris Masone02119d2008-09-05 16:13:11 -04002061 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002062
Chris Masone02119d2008-09-05 16:13:11 -04002063 ret = link_to_fixup_dir(wc->trans, root,
2064 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002065 if (ret)
2066 break;
Chris Masone02119d2008-09-05 16:13:11 -04002067 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002068
2069 if (key.type == BTRFS_DIR_INDEX_KEY &&
2070 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2071 ret = replay_one_dir_item(wc->trans, root, path,
2072 eb, i, &key);
2073 if (ret)
2074 break;
2075 }
2076
Chris Masone02119d2008-09-05 16:13:11 -04002077 if (wc->stage < LOG_WALK_REPLAY_ALL)
2078 continue;
2079
2080 /* these keys are simply copied */
2081 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2082 ret = overwrite_item(wc->trans, root, path,
2083 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002084 if (ret)
2085 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002086 } else if (key.type == BTRFS_INODE_REF_KEY ||
2087 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002088 ret = add_inode_ref(wc->trans, root, log, path,
2089 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002090 if (ret && ret != -ENOENT)
2091 break;
2092 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002093 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2094 ret = replay_one_extent(wc->trans, root, path,
2095 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002096 if (ret)
2097 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002098 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002099 ret = replay_one_dir_item(wc->trans, root, path,
2100 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002101 if (ret)
2102 break;
Chris Masone02119d2008-09-05 16:13:11 -04002103 }
2104 }
2105 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002106 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002107}
2108
Chris Masond3977122009-01-05 21:25:51 -05002109static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002110 struct btrfs_root *root,
2111 struct btrfs_path *path, int *level,
2112 struct walk_control *wc)
2113{
2114 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002115 u64 bytenr;
2116 u64 ptr_gen;
2117 struct extent_buffer *next;
2118 struct extent_buffer *cur;
2119 struct extent_buffer *parent;
2120 u32 blocksize;
2121 int ret = 0;
2122
2123 WARN_ON(*level < 0);
2124 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2125
Chris Masond3977122009-01-05 21:25:51 -05002126 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002127 WARN_ON(*level < 0);
2128 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2129 cur = path->nodes[*level];
2130
2131 if (btrfs_header_level(cur) != *level)
2132 WARN_ON(1);
2133
2134 if (path->slots[*level] >=
2135 btrfs_header_nritems(cur))
2136 break;
2137
2138 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2139 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2140 blocksize = btrfs_level_size(root, *level - 1);
2141
2142 parent = path->nodes[*level];
2143 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002144
2145 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002146 if (!next)
2147 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002148
Chris Masone02119d2008-09-05 16:13:11 -04002149 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002150 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002151 if (ret) {
2152 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002153 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002154 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002155
Chris Masone02119d2008-09-05 16:13:11 -04002156 path->slots[*level]++;
2157 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002158 ret = btrfs_read_buffer(next, ptr_gen);
2159 if (ret) {
2160 free_extent_buffer(next);
2161 return ret;
2162 }
Chris Masone02119d2008-09-05 16:13:11 -04002163
Josef Bacik681ae502013-10-07 15:11:00 -04002164 if (trans) {
2165 btrfs_tree_lock(next);
2166 btrfs_set_lock_blocking(next);
2167 clean_tree_block(trans, root, next);
2168 btrfs_wait_tree_block_writeback(next);
2169 btrfs_tree_unlock(next);
2170 }
Chris Masone02119d2008-09-05 16:13:11 -04002171
Chris Masone02119d2008-09-05 16:13:11 -04002172 WARN_ON(root_owner !=
2173 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002174 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002175 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002176 if (ret) {
2177 free_extent_buffer(next);
2178 return ret;
2179 }
Chris Masone02119d2008-09-05 16:13:11 -04002180 }
2181 free_extent_buffer(next);
2182 continue;
2183 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002184 ret = btrfs_read_buffer(next, ptr_gen);
2185 if (ret) {
2186 free_extent_buffer(next);
2187 return ret;
2188 }
Chris Masone02119d2008-09-05 16:13:11 -04002189
2190 WARN_ON(*level <= 0);
2191 if (path->nodes[*level-1])
2192 free_extent_buffer(path->nodes[*level-1]);
2193 path->nodes[*level-1] = next;
2194 *level = btrfs_header_level(next);
2195 path->slots[*level] = 0;
2196 cond_resched();
2197 }
2198 WARN_ON(*level < 0);
2199 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2200
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002201 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002202
2203 cond_resched();
2204 return 0;
2205}
2206
Chris Masond3977122009-01-05 21:25:51 -05002207static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002208 struct btrfs_root *root,
2209 struct btrfs_path *path, int *level,
2210 struct walk_control *wc)
2211{
2212 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002213 int i;
2214 int slot;
2215 int ret;
2216
Chris Masond3977122009-01-05 21:25:51 -05002217 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002218 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002219 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002220 path->slots[i]++;
2221 *level = i;
2222 WARN_ON(*level == 0);
2223 return 0;
2224 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002225 struct extent_buffer *parent;
2226 if (path->nodes[*level] == root->node)
2227 parent = path->nodes[*level];
2228 else
2229 parent = path->nodes[*level + 1];
2230
2231 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002232 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002233 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002234 if (ret)
2235 return ret;
2236
Chris Masone02119d2008-09-05 16:13:11 -04002237 if (wc->free) {
2238 struct extent_buffer *next;
2239
2240 next = path->nodes[*level];
2241
Josef Bacik681ae502013-10-07 15:11:00 -04002242 if (trans) {
2243 btrfs_tree_lock(next);
2244 btrfs_set_lock_blocking(next);
2245 clean_tree_block(trans, root, next);
2246 btrfs_wait_tree_block_writeback(next);
2247 btrfs_tree_unlock(next);
2248 }
Chris Masone02119d2008-09-05 16:13:11 -04002249
Chris Masone02119d2008-09-05 16:13:11 -04002250 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002251 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002252 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002253 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002254 if (ret)
2255 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002256 }
2257 free_extent_buffer(path->nodes[*level]);
2258 path->nodes[*level] = NULL;
2259 *level = i + 1;
2260 }
2261 }
2262 return 1;
2263}
2264
2265/*
2266 * drop the reference count on the tree rooted at 'snap'. This traverses
2267 * the tree freeing any blocks that have a ref count of zero after being
2268 * decremented.
2269 */
2270static int walk_log_tree(struct btrfs_trans_handle *trans,
2271 struct btrfs_root *log, struct walk_control *wc)
2272{
2273 int ret = 0;
2274 int wret;
2275 int level;
2276 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002277 int orig_level;
2278
2279 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002280 if (!path)
2281 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002282
2283 level = btrfs_header_level(log->node);
2284 orig_level = level;
2285 path->nodes[level] = log->node;
2286 extent_buffer_get(log->node);
2287 path->slots[level] = 0;
2288
Chris Masond3977122009-01-05 21:25:51 -05002289 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002290 wret = walk_down_log_tree(trans, log, path, &level, wc);
2291 if (wret > 0)
2292 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002293 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002294 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295 goto out;
2296 }
Chris Masone02119d2008-09-05 16:13:11 -04002297
2298 wret = walk_up_log_tree(trans, log, path, &level, wc);
2299 if (wret > 0)
2300 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002301 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002302 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002303 goto out;
2304 }
Chris Masone02119d2008-09-05 16:13:11 -04002305 }
2306
2307 /* was the root node processed? if not, catch it here */
2308 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002309 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002310 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002311 if (ret)
2312 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002313 if (wc->free) {
2314 struct extent_buffer *next;
2315
2316 next = path->nodes[orig_level];
2317
Josef Bacik681ae502013-10-07 15:11:00 -04002318 if (trans) {
2319 btrfs_tree_lock(next);
2320 btrfs_set_lock_blocking(next);
2321 clean_tree_block(trans, log, next);
2322 btrfs_wait_tree_block_writeback(next);
2323 btrfs_tree_unlock(next);
2324 }
Chris Masone02119d2008-09-05 16:13:11 -04002325
Chris Masone02119d2008-09-05 16:13:11 -04002326 WARN_ON(log->root_key.objectid !=
2327 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002328 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002329 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002330 if (ret)
2331 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002332 }
2333 }
2334
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002335out:
Chris Masone02119d2008-09-05 16:13:11 -04002336 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002337 return ret;
2338}
2339
Yan Zheng7237f182009-01-21 12:54:03 -05002340/*
2341 * helper function to update the item for a given subvolumes log root
2342 * in the tree of log roots
2343 */
2344static int update_log_root(struct btrfs_trans_handle *trans,
2345 struct btrfs_root *log)
2346{
2347 int ret;
2348
2349 if (log->log_transid == 1) {
2350 /* insert root item on the first sync */
2351 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2352 &log->root_key, &log->root_item);
2353 } else {
2354 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2355 &log->root_key, &log->root_item);
2356 }
2357 return ret;
2358}
2359
Chris Mason12fcfd22009-03-24 10:24:20 -04002360static int wait_log_commit(struct btrfs_trans_handle *trans,
2361 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04002362{
2363 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002364 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002365
Yan Zheng7237f182009-01-21 12:54:03 -05002366 /*
2367 * we only allow two pending log transactions at a time,
2368 * so we know that if ours is more than 2 older than the
2369 * current transaction, we're done
2370 */
Chris Masone02119d2008-09-05 16:13:11 -04002371 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002372 prepare_to_wait(&root->log_commit_wait[index],
2373 &wait, TASK_UNINTERRUPTIBLE);
2374 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002375
2376 if (root->fs_info->last_trans_log_full_commit !=
2377 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002378 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002379 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002380
Yan Zheng7237f182009-01-21 12:54:03 -05002381 finish_wait(&root->log_commit_wait[index], &wait);
2382 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002383 } while (root->fs_info->last_trans_log_full_commit !=
2384 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002385 atomic_read(&root->log_commit[index]));
2386 return 0;
2387}
2388
Jeff Mahoney143bede2012-03-01 14:56:26 +01002389static void wait_for_writer(struct btrfs_trans_handle *trans,
2390 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002391{
2392 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002393 while (root->fs_info->last_trans_log_full_commit !=
2394 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002395 prepare_to_wait(&root->log_writer_wait,
2396 &wait, TASK_UNINTERRUPTIBLE);
2397 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002398 if (root->fs_info->last_trans_log_full_commit !=
2399 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002400 schedule();
2401 mutex_lock(&root->log_mutex);
2402 finish_wait(&root->log_writer_wait, &wait);
2403 }
Chris Masone02119d2008-09-05 16:13:11 -04002404}
2405
2406/*
2407 * btrfs_sync_log does sends a given tree log down to the disk and
2408 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002409 * you know that any inodes previously logged are safely on disk only
2410 * if it returns 0.
2411 *
2412 * Any other return value means you need to call btrfs_commit_transaction.
2413 * Some of the edge cases for fsyncing directories that have had unlinks
2414 * or renames done in the past mean that sometimes the only safe
2415 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2416 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002417 */
2418int btrfs_sync_log(struct btrfs_trans_handle *trans,
2419 struct btrfs_root *root)
2420{
Yan Zheng7237f182009-01-21 12:54:03 -05002421 int index1;
2422 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002423 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002424 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002425 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002426 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002427 unsigned long log_transid = 0;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002428 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002429
Yan Zheng7237f182009-01-21 12:54:03 -05002430 mutex_lock(&root->log_mutex);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002431 log_transid = root->log_transid;
Yan Zheng7237f182009-01-21 12:54:03 -05002432 index1 = root->log_transid % 2;
2433 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002434 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002435 mutex_unlock(&root->log_mutex);
2436 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002437 }
Yan Zheng7237f182009-01-21 12:54:03 -05002438 atomic_set(&root->log_commit[index1], 1);
2439
2440 /* wait for previous tree log sync to complete */
2441 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002442 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002443 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002444 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002445 /* when we're on an ssd, just kick the log commit out */
2446 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002447 mutex_unlock(&root->log_mutex);
2448 schedule_timeout_uninterruptible(1);
2449 mutex_lock(&root->log_mutex);
2450 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002451 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002452 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002453 break;
2454 }
Chris Masond0c803c2008-09-11 16:17:57 -04002455
Chris Mason12fcfd22009-03-24 10:24:20 -04002456 /* bail out if we need to do a full commit */
2457 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2458 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002459 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002460 mutex_unlock(&root->log_mutex);
2461 goto out;
2462 }
2463
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002464 if (log_transid % 2 == 0)
2465 mark = EXTENT_DIRTY;
2466 else
2467 mark = EXTENT_NEW;
2468
Chris Mason690587d2009-10-13 13:29:19 -04002469 /* we start IO on all the marked extents here, but we don't actually
2470 * wait for them until later.
2471 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002472 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002473 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002475 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002476 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002477 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002478 mutex_unlock(&root->log_mutex);
2479 goto out;
2480 }
Yan Zheng7237f182009-01-21 12:54:03 -05002481
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002483
Yan Zheng7237f182009-01-21 12:54:03 -05002484 root->log_transid++;
2485 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002486 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002487 smp_mb();
2488 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002489 * IO has been started, blocks of the log tree have WRITTEN flag set
2490 * in their headers. new modifications of the log will be written to
2491 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002492 */
2493 mutex_unlock(&root->log_mutex);
2494
2495 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002496 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002497 atomic_inc(&log_root_tree->log_writers);
2498 mutex_unlock(&log_root_tree->log_mutex);
2499
2500 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002501
2502 mutex_lock(&log_root_tree->log_mutex);
2503 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2504 smp_mb();
2505 if (waitqueue_active(&log_root_tree->log_writer_wait))
2506 wake_up(&log_root_tree->log_writer_wait);
2507 }
2508
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002509 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002510 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002511 if (ret != -ENOSPC) {
2512 btrfs_abort_transaction(trans, root, ret);
2513 mutex_unlock(&log_root_tree->log_mutex);
2514 goto out;
2515 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002516 root->fs_info->last_trans_log_full_commit = trans->transid;
2517 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002518 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002519 mutex_unlock(&log_root_tree->log_mutex);
2520 ret = -EAGAIN;
2521 goto out;
2522 }
2523
Yan Zheng7237f182009-01-21 12:54:03 -05002524 index2 = log_root_tree->log_transid % 2;
2525 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002526 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002527 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002528 wait_log_commit(trans, log_root_tree,
2529 log_root_tree->log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002530 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002531 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002532 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002533 goto out;
2534 }
2535 atomic_set(&log_root_tree->log_commit[index2], 1);
2536
Chris Mason12fcfd22009-03-24 10:24:20 -04002537 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2538 wait_log_commit(trans, log_root_tree,
2539 log_root_tree->log_transid - 1);
2540 }
Yan Zheng7237f182009-01-21 12:54:03 -05002541
Chris Mason12fcfd22009-03-24 10:24:20 -04002542 wait_for_writer(trans, log_root_tree);
2543
2544 /*
2545 * now that we've moved on to the tree of log tree roots,
2546 * check the full commit flag again
2547 */
2548 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002549 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002550 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002551 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002552 mutex_unlock(&log_root_tree->log_mutex);
2553 ret = -EAGAIN;
2554 goto out_wake_log_root;
2555 }
Yan Zheng7237f182009-01-21 12:54:03 -05002556
Miao Xiec6adc9c2013-05-28 10:05:39 +00002557 ret = btrfs_write_marked_extents(log_root_tree,
2558 &log_root_tree->dirty_log_pages,
2559 EXTENT_DIRTY | EXTENT_NEW);
2560 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002561 if (ret) {
2562 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002563 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564 mutex_unlock(&log_root_tree->log_mutex);
2565 goto out_wake_log_root;
2566 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002567 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002568 btrfs_wait_marked_extents(log_root_tree,
2569 &log_root_tree->dirty_log_pages,
2570 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002571 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002572
David Sterba6c417612011-04-13 15:41:04 +02002573 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002574 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002575 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002576 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002577
Yan Zheng7237f182009-01-21 12:54:03 -05002578 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002579 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002580
2581 mutex_unlock(&log_root_tree->log_mutex);
2582
2583 /*
2584 * nobody else is going to jump in and write the the ctree
2585 * super here because the log_commit atomic below is protecting
2586 * us. We must be called with a transaction handle pinning
2587 * the running transaction open, so a full commit can't hop
2588 * in and cause problems either.
2589 */
Arne Jansena2de7332011-03-08 14:14:00 +01002590 btrfs_scrub_pause_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002591 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002592 btrfs_scrub_continue_super(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002593 if (ret) {
2594 btrfs_abort_transaction(trans, root, ret);
2595 goto out_wake_log_root;
2596 }
Yan Zheng7237f182009-01-21 12:54:03 -05002597
Chris Mason257c62e2009-10-13 13:21:08 -04002598 mutex_lock(&root->log_mutex);
2599 if (root->last_log_commit < log_transid)
2600 root->last_log_commit = log_transid;
2601 mutex_unlock(&root->log_mutex);
2602
Chris Mason12fcfd22009-03-24 10:24:20 -04002603out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002604 atomic_set(&log_root_tree->log_commit[index2], 0);
2605 smp_mb();
2606 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2607 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002608out:
Yan Zheng7237f182009-01-21 12:54:03 -05002609 atomic_set(&root->log_commit[index1], 0);
2610 smp_mb();
2611 if (waitqueue_active(&root->log_commit_wait[index1]))
2612 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002613 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002614}
2615
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002616static void free_log_tree(struct btrfs_trans_handle *trans,
2617 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002618{
2619 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002620 u64 start;
2621 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002622 struct walk_control wc = {
2623 .free = 1,
2624 .process_func = process_one_buffer
2625 };
2626
Josef Bacik681ae502013-10-07 15:11:00 -04002627 ret = walk_log_tree(trans, log, &wc);
2628 /* I don't think this can happen but just in case */
2629 if (ret)
2630 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002631
Chris Masond3977122009-01-05 21:25:51 -05002632 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002633 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002634 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2635 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002636 if (ret)
2637 break;
2638
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002639 clear_extent_bits(&log->dirty_log_pages, start, end,
2640 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002641 }
2642
Josef Bacik2ab28f32012-10-12 15:27:49 -04002643 /*
2644 * We may have short-circuited the log tree with the full commit logic
2645 * and left ordered extents on our list, so clear these out to keep us
2646 * from leaking inodes and memory.
2647 */
2648 btrfs_free_logged_extents(log, 0);
2649 btrfs_free_logged_extents(log, 1);
2650
Yan Zheng7237f182009-01-21 12:54:03 -05002651 free_extent_buffer(log->node);
2652 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002653}
2654
2655/*
2656 * free all the extents used by the tree log. This should be called
2657 * at commit time of the full transaction
2658 */
2659int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2660{
2661 if (root->log_root) {
2662 free_log_tree(trans, root->log_root);
2663 root->log_root = NULL;
2664 }
2665 return 0;
2666}
2667
2668int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2669 struct btrfs_fs_info *fs_info)
2670{
2671 if (fs_info->log_root_tree) {
2672 free_log_tree(trans, fs_info->log_root_tree);
2673 fs_info->log_root_tree = NULL;
2674 }
Chris Masone02119d2008-09-05 16:13:11 -04002675 return 0;
2676}
2677
2678/*
Chris Masone02119d2008-09-05 16:13:11 -04002679 * If both a file and directory are logged, and unlinks or renames are
2680 * mixed in, we have a few interesting corners:
2681 *
2682 * create file X in dir Y
2683 * link file X to X.link in dir Y
2684 * fsync file X
2685 * unlink file X but leave X.link
2686 * fsync dir Y
2687 *
2688 * After a crash we would expect only X.link to exist. But file X
2689 * didn't get fsync'd again so the log has back refs for X and X.link.
2690 *
2691 * We solve this by removing directory entries and inode backrefs from the
2692 * log when a file that was logged in the current transaction is
2693 * unlinked. Any later fsync will include the updated log entries, and
2694 * we'll be able to reconstruct the proper directory items from backrefs.
2695 *
2696 * This optimizations allows us to avoid relogging the entire inode
2697 * or the entire directory.
2698 */
2699int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2700 struct btrfs_root *root,
2701 const char *name, int name_len,
2702 struct inode *dir, u64 index)
2703{
2704 struct btrfs_root *log;
2705 struct btrfs_dir_item *di;
2706 struct btrfs_path *path;
2707 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002708 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002709 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002710 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002711
Chris Mason3a5f1d42008-09-11 15:53:37 -04002712 if (BTRFS_I(dir)->logged_trans < trans->transid)
2713 return 0;
2714
Chris Masone02119d2008-09-05 16:13:11 -04002715 ret = join_running_log_trans(root);
2716 if (ret)
2717 return 0;
2718
2719 mutex_lock(&BTRFS_I(dir)->log_mutex);
2720
2721 log = root->log_root;
2722 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002723 if (!path) {
2724 err = -ENOMEM;
2725 goto out_unlock;
2726 }
liubo2a29edc2011-01-26 06:22:08 +00002727
Li Zefan33345d012011-04-20 10:31:50 +08002728 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002729 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002730 if (IS_ERR(di)) {
2731 err = PTR_ERR(di);
2732 goto fail;
2733 }
2734 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002735 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2736 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002737 if (ret) {
2738 err = ret;
2739 goto fail;
2740 }
Chris Masone02119d2008-09-05 16:13:11 -04002741 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002742 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002743 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002744 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002745 if (IS_ERR(di)) {
2746 err = PTR_ERR(di);
2747 goto fail;
2748 }
2749 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002750 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2751 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002752 if (ret) {
2753 err = ret;
2754 goto fail;
2755 }
Chris Masone02119d2008-09-05 16:13:11 -04002756 }
2757
2758 /* update the directory size in the log to reflect the names
2759 * we have removed
2760 */
2761 if (bytes_del) {
2762 struct btrfs_key key;
2763
Li Zefan33345d012011-04-20 10:31:50 +08002764 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002765 key.offset = 0;
2766 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002767 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002768
2769 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002770 if (ret < 0) {
2771 err = ret;
2772 goto fail;
2773 }
Chris Masone02119d2008-09-05 16:13:11 -04002774 if (ret == 0) {
2775 struct btrfs_inode_item *item;
2776 u64 i_size;
2777
2778 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2779 struct btrfs_inode_item);
2780 i_size = btrfs_inode_size(path->nodes[0], item);
2781 if (i_size > bytes_del)
2782 i_size -= bytes_del;
2783 else
2784 i_size = 0;
2785 btrfs_set_inode_size(path->nodes[0], item, i_size);
2786 btrfs_mark_buffer_dirty(path->nodes[0]);
2787 } else
2788 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002789 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002790 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002791fail:
Chris Masone02119d2008-09-05 16:13:11 -04002792 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002793out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002794 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002795 if (ret == -ENOSPC) {
2796 root->fs_info->last_trans_log_full_commit = trans->transid;
2797 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002798 } else if (ret < 0)
2799 btrfs_abort_transaction(trans, root, ret);
2800
Chris Mason12fcfd22009-03-24 10:24:20 -04002801 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002802
Andi Kleen411fc6b2010-10-29 15:14:31 -04002803 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002804}
2805
2806/* see comments for btrfs_del_dir_entries_in_log */
2807int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2808 struct btrfs_root *root,
2809 const char *name, int name_len,
2810 struct inode *inode, u64 dirid)
2811{
2812 struct btrfs_root *log;
2813 u64 index;
2814 int ret;
2815
Chris Mason3a5f1d42008-09-11 15:53:37 -04002816 if (BTRFS_I(inode)->logged_trans < trans->transid)
2817 return 0;
2818
Chris Masone02119d2008-09-05 16:13:11 -04002819 ret = join_running_log_trans(root);
2820 if (ret)
2821 return 0;
2822 log = root->log_root;
2823 mutex_lock(&BTRFS_I(inode)->log_mutex);
2824
Li Zefan33345d012011-04-20 10:31:50 +08002825 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002826 dirid, &index);
2827 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002828 if (ret == -ENOSPC) {
2829 root->fs_info->last_trans_log_full_commit = trans->transid;
2830 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002831 } else if (ret < 0 && ret != -ENOENT)
2832 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002833 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002834
Chris Masone02119d2008-09-05 16:13:11 -04002835 return ret;
2836}
2837
2838/*
2839 * creates a range item in the log for 'dirid'. first_offset and
2840 * last_offset tell us which parts of the key space the log should
2841 * be considered authoritative for.
2842 */
2843static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *log,
2845 struct btrfs_path *path,
2846 int key_type, u64 dirid,
2847 u64 first_offset, u64 last_offset)
2848{
2849 int ret;
2850 struct btrfs_key key;
2851 struct btrfs_dir_log_item *item;
2852
2853 key.objectid = dirid;
2854 key.offset = first_offset;
2855 if (key_type == BTRFS_DIR_ITEM_KEY)
2856 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2857 else
2858 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2859 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002860 if (ret)
2861 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002862
2863 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2864 struct btrfs_dir_log_item);
2865 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2866 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002867 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002868 return 0;
2869}
2870
2871/*
2872 * log all the items included in the current transaction for a given
2873 * directory. This also creates the range items in the log tree required
2874 * to replay anything deleted before the fsync
2875 */
2876static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2877 struct btrfs_root *root, struct inode *inode,
2878 struct btrfs_path *path,
2879 struct btrfs_path *dst_path, int key_type,
2880 u64 min_offset, u64 *last_offset_ret)
2881{
2882 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002883 struct btrfs_root *log = root->log_root;
2884 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002885 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002886 int ret;
2887 int i;
2888 int nritems;
2889 u64 first_offset = min_offset;
2890 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002891 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002892
2893 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002894
Li Zefan33345d012011-04-20 10:31:50 +08002895 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002896 min_key.type = key_type;
2897 min_key.offset = min_offset;
2898
2899 path->keep_locks = 1;
2900
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002901 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002902
2903 /*
2904 * we didn't find anything from this transaction, see if there
2905 * is anything at all
2906 */
Li Zefan33345d012011-04-20 10:31:50 +08002907 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2908 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002909 min_key.type = key_type;
2910 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002911 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002912 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2913 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002914 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002915 return ret;
2916 }
Li Zefan33345d012011-04-20 10:31:50 +08002917 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002918
2919 /* if ret == 0 there are items for this type,
2920 * create a range to tell us the last key of this type.
2921 * otherwise, there are no items in this directory after
2922 * *min_offset, and we create a range to indicate that.
2923 */
2924 if (ret == 0) {
2925 struct btrfs_key tmp;
2926 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2927 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002928 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002929 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002930 }
2931 goto done;
2932 }
2933
2934 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002935 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002936 if (ret == 0) {
2937 struct btrfs_key tmp;
2938 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2939 if (key_type == tmp.type) {
2940 first_offset = tmp.offset;
2941 ret = overwrite_item(trans, log, dst_path,
2942 path->nodes[0], path->slots[0],
2943 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002944 if (ret) {
2945 err = ret;
2946 goto done;
2947 }
Chris Masone02119d2008-09-05 16:13:11 -04002948 }
2949 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002950 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002951
2952 /* find the first key from this transaction again */
2953 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2954 if (ret != 0) {
2955 WARN_ON(1);
2956 goto done;
2957 }
2958
2959 /*
2960 * we have a block from this transaction, log every item in it
2961 * from our directory
2962 */
Chris Masond3977122009-01-05 21:25:51 -05002963 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002964 struct btrfs_key tmp;
2965 src = path->nodes[0];
2966 nritems = btrfs_header_nritems(src);
2967 for (i = path->slots[0]; i < nritems; i++) {
2968 btrfs_item_key_to_cpu(src, &min_key, i);
2969
Li Zefan33345d012011-04-20 10:31:50 +08002970 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002971 goto done;
2972 ret = overwrite_item(trans, log, dst_path, src, i,
2973 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002974 if (ret) {
2975 err = ret;
2976 goto done;
2977 }
Chris Masone02119d2008-09-05 16:13:11 -04002978 }
2979 path->slots[0] = nritems;
2980
2981 /*
2982 * look ahead to the next item and see if it is also
2983 * from this directory and from this transaction
2984 */
2985 ret = btrfs_next_leaf(root, path);
2986 if (ret == 1) {
2987 last_offset = (u64)-1;
2988 goto done;
2989 }
2990 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002991 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002992 last_offset = (u64)-1;
2993 goto done;
2994 }
2995 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2996 ret = overwrite_item(trans, log, dst_path,
2997 path->nodes[0], path->slots[0],
2998 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002999 if (ret)
3000 err = ret;
3001 else
3002 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003003 goto done;
3004 }
3005 }
3006done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003007 btrfs_release_path(path);
3008 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003009
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003010 if (err == 0) {
3011 *last_offset_ret = last_offset;
3012 /*
3013 * insert the log range keys to indicate where the log
3014 * is valid
3015 */
3016 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003017 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003018 if (ret)
3019 err = ret;
3020 }
3021 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003022}
3023
3024/*
3025 * logging directories is very similar to logging inodes, We find all the items
3026 * from the current transaction and write them to the log.
3027 *
3028 * The recovery code scans the directory in the subvolume, and if it finds a
3029 * key in the range logged that is not present in the log tree, then it means
3030 * that dir entry was unlinked during the transaction.
3031 *
3032 * In order for that scan to work, we must include one key smaller than
3033 * the smallest logged by this transaction and one key larger than the largest
3034 * key logged by this transaction.
3035 */
3036static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3037 struct btrfs_root *root, struct inode *inode,
3038 struct btrfs_path *path,
3039 struct btrfs_path *dst_path)
3040{
3041 u64 min_key;
3042 u64 max_key;
3043 int ret;
3044 int key_type = BTRFS_DIR_ITEM_KEY;
3045
3046again:
3047 min_key = 0;
3048 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003049 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003050 ret = log_dir_items(trans, root, inode, path,
3051 dst_path, key_type, min_key,
3052 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003053 if (ret)
3054 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003055 if (max_key == (u64)-1)
3056 break;
3057 min_key = max_key + 1;
3058 }
3059
3060 if (key_type == BTRFS_DIR_ITEM_KEY) {
3061 key_type = BTRFS_DIR_INDEX_KEY;
3062 goto again;
3063 }
3064 return 0;
3065}
3066
3067/*
3068 * a helper function to drop items from the log before we relog an
3069 * inode. max_key_type indicates the highest item type to remove.
3070 * This cannot be run for file data extents because it does not
3071 * free the extents they point to.
3072 */
3073static int drop_objectid_items(struct btrfs_trans_handle *trans,
3074 struct btrfs_root *log,
3075 struct btrfs_path *path,
3076 u64 objectid, int max_key_type)
3077{
3078 int ret;
3079 struct btrfs_key key;
3080 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003081 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003082
3083 key.objectid = objectid;
3084 key.type = max_key_type;
3085 key.offset = (u64)-1;
3086
Chris Masond3977122009-01-05 21:25:51 -05003087 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003088 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003089 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003090 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003091 break;
3092
3093 if (path->slots[0] == 0)
3094 break;
3095
3096 path->slots[0]--;
3097 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3098 path->slots[0]);
3099
3100 if (found_key.objectid != objectid)
3101 break;
3102
Josef Bacik18ec90d2012-09-28 11:56:28 -04003103 found_key.offset = 0;
3104 found_key.type = 0;
3105 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3106 &start_slot);
3107
3108 ret = btrfs_del_items(trans, log, path, start_slot,
3109 path->slots[0] - start_slot + 1);
3110 /*
3111 * If start slot isn't 0 then we don't need to re-search, we've
3112 * found the last guy with the objectid in this tree.
3113 */
3114 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003115 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003117 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003118 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003119 if (ret > 0)
3120 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003121 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003122}
3123
Josef Bacik94edf4a2012-09-25 14:56:25 -04003124static void fill_inode_item(struct btrfs_trans_handle *trans,
3125 struct extent_buffer *leaf,
3126 struct btrfs_inode_item *item,
3127 struct inode *inode, int log_inode_only)
3128{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003129 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003130
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003131 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003132
3133 if (log_inode_only) {
3134 /* set the generation to zero so the recover code
3135 * can tell the difference between an logging
3136 * just to say 'this inode exists' and a logging
3137 * to say 'update this inode with these values'
3138 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003139 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3140 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003141 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003142 btrfs_set_token_inode_generation(leaf, item,
3143 BTRFS_I(inode)->generation,
3144 &token);
3145 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003146 }
3147
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003148 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3149 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3150 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3151 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3152
3153 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3154 inode->i_atime.tv_sec, &token);
3155 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3156 inode->i_atime.tv_nsec, &token);
3157
3158 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3159 inode->i_mtime.tv_sec, &token);
3160 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3161 inode->i_mtime.tv_nsec, &token);
3162
3163 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3164 inode->i_ctime.tv_sec, &token);
3165 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3166 inode->i_ctime.tv_nsec, &token);
3167
3168 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3169 &token);
3170
3171 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3172 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3173 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3174 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3175 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003176}
3177
Josef Bacika95249b2012-10-11 16:17:34 -04003178static int log_inode_item(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *log, struct btrfs_path *path,
3180 struct inode *inode)
3181{
3182 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003183 int ret;
3184
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003185 ret = btrfs_insert_empty_item(trans, log, path,
3186 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003187 sizeof(*inode_item));
3188 if (ret && ret != -EEXIST)
3189 return ret;
3190 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3191 struct btrfs_inode_item);
3192 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3193 btrfs_release_path(path);
3194 return 0;
3195}
3196
Chris Mason31ff1cd2008-09-11 16:17:57 -04003197static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003198 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003199 struct btrfs_path *dst_path,
3200 struct extent_buffer *src,
3201 int start_slot, int nr, int inode_only)
3202{
3203 unsigned long src_offset;
3204 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003205 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003206 struct btrfs_file_extent_item *extent;
3207 struct btrfs_inode_item *inode_item;
3208 int ret;
3209 struct btrfs_key *ins_keys;
3210 u32 *ins_sizes;
3211 char *ins_data;
3212 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003213 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003214 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masond20f7042008-12-08 16:58:54 -05003215
3216 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003217
3218 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3219 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003220 if (!ins_data)
3221 return -ENOMEM;
3222
Chris Mason31ff1cd2008-09-11 16:17:57 -04003223 ins_sizes = (u32 *)ins_data;
3224 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3225
3226 for (i = 0; i < nr; i++) {
3227 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3228 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3229 }
3230 ret = btrfs_insert_empty_items(trans, log, dst_path,
3231 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003232 if (ret) {
3233 kfree(ins_data);
3234 return ret;
3235 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003238 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3239 dst_path->slots[0]);
3240
3241 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3242
Josef Bacik94edf4a2012-09-25 14:56:25 -04003243 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003244 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3245 dst_path->slots[0],
3246 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003247 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3248 inode, inode_only == LOG_INODE_EXISTS);
3249 } else {
3250 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3251 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003252 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003253
Chris Mason31ff1cd2008-09-11 16:17:57 -04003254 /* take a reference on file data extents so that truncates
3255 * or deletes of this inode don't have to relog the inode
3256 * again
3257 */
Liu Bod2794402012-08-29 01:07:56 -06003258 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3259 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003260 int found_type;
3261 extent = btrfs_item_ptr(src, start_slot + i,
3262 struct btrfs_file_extent_item);
3263
liubo8e531cd2011-05-06 10:36:09 +08003264 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3265 continue;
3266
Chris Mason31ff1cd2008-09-11 16:17:57 -04003267 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003268 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003269 u64 ds, dl, cs, cl;
3270 ds = btrfs_file_extent_disk_bytenr(src,
3271 extent);
3272 /* ds == 0 is a hole */
3273 if (ds == 0)
3274 continue;
3275
3276 dl = btrfs_file_extent_disk_num_bytes(src,
3277 extent);
3278 cs = btrfs_file_extent_offset(src, extent);
3279 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003280 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003281 if (btrfs_file_extent_compression(src,
3282 extent)) {
3283 cs = 0;
3284 cl = dl;
3285 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286
3287 ret = btrfs_lookup_csums_range(
3288 log->fs_info->csum_root,
3289 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003290 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003291 if (ret) {
3292 btrfs_release_path(dst_path);
3293 kfree(ins_data);
3294 return ret;
3295 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003296 }
3297 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003298 }
3299
3300 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003301 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003302 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003303
3304 /*
3305 * we have to do this after the loop above to avoid changing the
3306 * log tree while trying to change the log tree.
3307 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003308 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003309 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003310 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3311 struct btrfs_ordered_sum,
3312 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003313 if (!ret)
3314 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003315 list_del(&sums->list);
3316 kfree(sums);
3317 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003318 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003319}
3320
Josef Bacik5dc562c2012-08-17 13:14:17 -04003321static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3322{
3323 struct extent_map *em1, *em2;
3324
3325 em1 = list_entry(a, struct extent_map, list);
3326 em2 = list_entry(b, struct extent_map, list);
3327
3328 if (em1->start < em2->start)
3329 return -1;
3330 else if (em1->start > em2->start)
3331 return 1;
3332 return 0;
3333}
3334
Josef Bacik5dc562c2012-08-17 13:14:17 -04003335static int log_one_extent(struct btrfs_trans_handle *trans,
3336 struct inode *inode, struct btrfs_root *root,
Josef Bacik70c8a912012-10-11 16:54:30 -04003337 struct extent_map *em, struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003338{
3339 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003340 struct btrfs_file_extent_item *fi;
3341 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003342 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003343 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003344 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003345 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003346 u64 mod_start = em->mod_start;
3347 u64 mod_len = em->mod_len;
3348 u64 csum_offset;
3349 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003350 u64 extent_offset = em->start - em->orig_start;
3351 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003352 int ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003353 int index = log->log_transid % 2;
Josef Bacik70c8a912012-10-11 16:54:30 -04003354 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003355
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003356 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3357 em->start + em->len, NULL, 0);
3358 if (ret)
3359 return ret;
3360
Josef Bacik70c8a912012-10-11 16:54:30 -04003361 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003362 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003363 key.objectid = btrfs_ino(inode);
3364 key.type = BTRFS_EXTENT_DATA_KEY;
3365 key.offset = em->start;
Josef Bacik70c8a912012-10-11 16:54:30 -04003366
3367 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003368 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003369 return ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003370 leaf = path->nodes[0];
3371 fi = btrfs_item_ptr(leaf, path->slots[0],
3372 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003373
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003374 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3375 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003376 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3377 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003378 btrfs_set_token_file_extent_type(leaf, fi,
3379 BTRFS_FILE_EXTENT_PREALLOC,
3380 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003381 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003382 btrfs_set_token_file_extent_type(leaf, fi,
3383 BTRFS_FILE_EXTENT_REG,
3384 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003385 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003386 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003387 }
3388
Josef Bacik70c8a912012-10-11 16:54:30 -04003389 block_len = max(em->block_len, em->orig_block_len);
3390 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003391 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3392 em->block_start,
3393 &token);
3394 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3395 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003396 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003397 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3398 em->block_start -
3399 extent_offset, &token);
3400 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3401 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003402 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003403 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3404 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3405 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003406 }
3407
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003408 btrfs_set_token_file_extent_offset(leaf, fi,
3409 em->start - em->orig_start,
3410 &token);
3411 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003412 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003413 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3414 &token);
3415 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3416 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003417 btrfs_mark_buffer_dirty(leaf);
3418
Josef Bacik70c8a912012-10-11 16:54:30 -04003419 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003420 if (ret) {
3421 return ret;
3422 }
3423
3424 if (skip_csum)
3425 return 0;
3426
Liu Bo192000d2013-01-06 03:38:22 +00003427 if (em->compress_type) {
3428 csum_offset = 0;
3429 csum_len = block_len;
3430 }
3431
Josef Bacik2ab28f32012-10-12 15:27:49 -04003432 /*
3433 * First check and see if our csums are on our outstanding ordered
3434 * extents.
3435 */
3436again:
3437 spin_lock_irq(&log->log_extents_lock[index]);
3438 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3439 struct btrfs_ordered_sum *sum;
3440
3441 if (!mod_len)
3442 break;
3443
3444 if (ordered->inode != inode)
3445 continue;
3446
3447 if (ordered->file_offset + ordered->len <= mod_start ||
3448 mod_start + mod_len <= ordered->file_offset)
3449 continue;
3450
3451 /*
3452 * We are going to copy all the csums on this ordered extent, so
3453 * go ahead and adjust mod_start and mod_len in case this
3454 * ordered extent has already been logged.
3455 */
3456 if (ordered->file_offset > mod_start) {
3457 if (ordered->file_offset + ordered->len >=
3458 mod_start + mod_len)
3459 mod_len = ordered->file_offset - mod_start;
3460 /*
3461 * If we have this case
3462 *
3463 * |--------- logged extent ---------|
3464 * |----- ordered extent ----|
3465 *
3466 * Just don't mess with mod_start and mod_len, we'll
3467 * just end up logging more csums than we need and it
3468 * will be ok.
3469 */
3470 } else {
3471 if (ordered->file_offset + ordered->len <
3472 mod_start + mod_len) {
3473 mod_len = (mod_start + mod_len) -
3474 (ordered->file_offset + ordered->len);
3475 mod_start = ordered->file_offset +
3476 ordered->len;
3477 } else {
3478 mod_len = 0;
3479 }
3480 }
3481
3482 /*
3483 * To keep us from looping for the above case of an ordered
3484 * extent that falls inside of the logged extent.
3485 */
3486 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3487 &ordered->flags))
3488 continue;
3489 atomic_inc(&ordered->refs);
3490 spin_unlock_irq(&log->log_extents_lock[index]);
3491 /*
3492 * we've dropped the lock, we must either break or
3493 * start over after this.
3494 */
3495
3496 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3497
3498 list_for_each_entry(sum, &ordered->list, list) {
3499 ret = btrfs_csum_file_blocks(trans, log, sum);
3500 if (ret) {
3501 btrfs_put_ordered_extent(ordered);
3502 goto unlocked;
3503 }
3504 }
3505 btrfs_put_ordered_extent(ordered);
3506 goto again;
3507
3508 }
3509 spin_unlock_irq(&log->log_extents_lock[index]);
3510unlocked:
3511
3512 if (!mod_len || ret)
3513 return ret;
3514
3515 csum_offset = mod_start - em->start;
3516 csum_len = mod_len;
3517
Josef Bacik70c8a912012-10-11 16:54:30 -04003518 /* block start is already adjusted for the file extent offset. */
3519 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3520 em->block_start + csum_offset,
3521 em->block_start + csum_offset +
3522 csum_len - 1, &ordered_sums, 0);
3523 if (ret)
3524 return ret;
3525
3526 while (!list_empty(&ordered_sums)) {
3527 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3528 struct btrfs_ordered_sum,
3529 list);
3530 if (!ret)
3531 ret = btrfs_csum_file_blocks(trans, log, sums);
3532 list_del(&sums->list);
3533 kfree(sums);
3534 }
3535
3536 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003537}
3538
3539static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3540 struct btrfs_root *root,
3541 struct inode *inode,
Josef Bacik70c8a912012-10-11 16:54:30 -04003542 struct btrfs_path *path)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003543{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003544 struct extent_map *em, *n;
3545 struct list_head extents;
3546 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3547 u64 test_gen;
3548 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003549 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003550
3551 INIT_LIST_HEAD(&extents);
3552
Josef Bacik5dc562c2012-08-17 13:14:17 -04003553 write_lock(&tree->lock);
3554 test_gen = root->fs_info->last_trans_committed;
3555
3556 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3557 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003558
3559 /*
3560 * Just an arbitrary number, this can be really CPU intensive
3561 * once we start getting a lot of extents, and really once we
3562 * have a bunch of extents we just want to commit since it will
3563 * be faster.
3564 */
3565 if (++num > 32768) {
3566 list_del_init(&tree->modified_extents);
3567 ret = -EFBIG;
3568 goto process;
3569 }
3570
Josef Bacik5dc562c2012-08-17 13:14:17 -04003571 if (em->generation <= test_gen)
3572 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003573 /* Need a ref to keep it from getting evicted from cache */
3574 atomic_inc(&em->refs);
3575 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003576 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003577 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003578 }
3579
3580 list_sort(NULL, &extents, extent_cmp);
3581
Josef Bacik2ab28f32012-10-12 15:27:49 -04003582process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003583 while (!list_empty(&extents)) {
3584 em = list_entry(extents.next, struct extent_map, list);
3585
3586 list_del_init(&em->list);
3587
3588 /*
3589 * If we had an error we just need to delete everybody from our
3590 * private list.
3591 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003592 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003593 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003594 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003595 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003596 }
3597
3598 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003599
Josef Bacik70c8a912012-10-11 16:54:30 -04003600 ret = log_one_extent(trans, inode, root, em, path);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003601 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003602 clear_em_logging(tree, em);
3603 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003604 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003605 WARN_ON(!list_empty(&extents));
3606 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003607
Josef Bacik5dc562c2012-08-17 13:14:17 -04003608 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003609 return ret;
3610}
3611
Chris Masone02119d2008-09-05 16:13:11 -04003612/* log a single inode in the tree log.
3613 * At least one parent directory for this inode must exist in the tree
3614 * or be logged already.
3615 *
3616 * Any items from this inode changed by the current transaction are copied
3617 * to the log tree. An extra reference is taken on any extents in this
3618 * file, allowing us to avoid a whole pile of corner cases around logging
3619 * blocks that have been removed from the tree.
3620 *
3621 * See LOG_INODE_ALL and related defines for a description of what inode_only
3622 * does.
3623 *
3624 * This handles both files and directories.
3625 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003626static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003627 struct btrfs_root *root, struct inode *inode,
3628 int inode_only)
3629{
3630 struct btrfs_path *path;
3631 struct btrfs_path *dst_path;
3632 struct btrfs_key min_key;
3633 struct btrfs_key max_key;
3634 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003635 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003636 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003637 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003638 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003639 int ins_start_slot = 0;
3640 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003641 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003642 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003643
Chris Masone02119d2008-09-05 16:13:11 -04003644 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003645 if (!path)
3646 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003647 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003648 if (!dst_path) {
3649 btrfs_free_path(path);
3650 return -ENOMEM;
3651 }
Chris Masone02119d2008-09-05 16:13:11 -04003652
Li Zefan33345d012011-04-20 10:31:50 +08003653 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003654 min_key.type = BTRFS_INODE_ITEM_KEY;
3655 min_key.offset = 0;
3656
Li Zefan33345d012011-04-20 10:31:50 +08003657 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003658
Chris Mason12fcfd22009-03-24 10:24:20 -04003659
Josef Bacik5dc562c2012-08-17 13:14:17 -04003660 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003661 if (S_ISDIR(inode->i_mode) ||
3662 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3663 &BTRFS_I(inode)->runtime_flags) &&
3664 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003665 max_key.type = BTRFS_XATTR_ITEM_KEY;
3666 else
3667 max_key.type = (u8)-1;
3668 max_key.offset = (u64)-1;
3669
Josef Bacik94edf4a2012-09-25 14:56:25 -04003670 /* Only run delayed items if we are a dir or a new file */
3671 if (S_ISDIR(inode->i_mode) ||
3672 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3673 ret = btrfs_commit_inode_delayed_items(trans, inode);
3674 if (ret) {
3675 btrfs_free_path(path);
3676 btrfs_free_path(dst_path);
3677 return ret;
3678 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003679 }
3680
Chris Masone02119d2008-09-05 16:13:11 -04003681 mutex_lock(&BTRFS_I(inode)->log_mutex);
3682
Josef Bacik2ab28f32012-10-12 15:27:49 -04003683 btrfs_get_logged_extents(log, inode);
3684
Chris Masone02119d2008-09-05 16:13:11 -04003685 /*
3686 * a brute force approach to making sure we get the most uptodate
3687 * copies of everything.
3688 */
3689 if (S_ISDIR(inode->i_mode)) {
3690 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3691
3692 if (inode_only == LOG_INODE_EXISTS)
3693 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003694 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003695 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003696 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3697 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003698 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3699 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003700 ret = btrfs_truncate_inode_items(trans, log,
3701 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003702 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3703 &BTRFS_I(inode)->runtime_flags)) {
3704 if (inode_only == LOG_INODE_ALL)
3705 fast_search = true;
3706 max_key.type = BTRFS_XATTR_ITEM_KEY;
3707 ret = drop_objectid_items(trans, log, path, ino,
3708 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003709 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003710 if (inode_only == LOG_INODE_ALL)
3711 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003712 ret = log_inode_item(trans, log, dst_path, inode);
3713 if (ret) {
3714 err = ret;
3715 goto out_unlock;
3716 }
3717 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003718 }
Josef Bacika95249b2012-10-11 16:17:34 -04003719
Chris Masone02119d2008-09-05 16:13:11 -04003720 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003721 if (ret) {
3722 err = ret;
3723 goto out_unlock;
3724 }
Chris Masone02119d2008-09-05 16:13:11 -04003725 path->keep_locks = 1;
3726
Chris Masond3977122009-01-05 21:25:51 -05003727 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003728 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003729 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003730 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003731 if (ret != 0)
3732 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003733again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003734 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003735 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003736 break;
3737 if (min_key.type > max_key.type)
3738 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003739
Chris Masone02119d2008-09-05 16:13:11 -04003740 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003741 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3742 ins_nr++;
3743 goto next_slot;
3744 } else if (!ins_nr) {
3745 ins_start_slot = path->slots[0];
3746 ins_nr = 1;
3747 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003748 }
3749
Liu Bod2794402012-08-29 01:07:56 -06003750 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003751 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003752 if (ret) {
3753 err = ret;
3754 goto out_unlock;
3755 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003756 ins_nr = 1;
3757 ins_start_slot = path->slots[0];
3758next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003759
Chris Mason3a5f1d42008-09-11 15:53:37 -04003760 nritems = btrfs_header_nritems(path->nodes[0]);
3761 path->slots[0]++;
3762 if (path->slots[0] < nritems) {
3763 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3764 path->slots[0]);
3765 goto again;
3766 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003767 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003768 ret = copy_items(trans, inode, dst_path, src,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003769 ins_start_slot,
3770 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003771 if (ret) {
3772 err = ret;
3773 goto out_unlock;
3774 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003775 ins_nr = 0;
3776 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003777 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04003778
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003779 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04003780 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003781 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04003782 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003783 min_key.offset = 0;
3784 } else {
Chris Masone02119d2008-09-05 16:13:11 -04003785 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01003786 }
Chris Masone02119d2008-09-05 16:13:11 -04003787 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003788 if (ins_nr) {
Liu Bod2794402012-08-29 01:07:56 -06003789 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003790 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003791 if (ret) {
3792 err = ret;
3793 goto out_unlock;
3794 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003795 ins_nr = 0;
3796 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003797
Josef Bacika95249b2012-10-11 16:17:34 -04003798log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04003799 btrfs_release_path(path);
3800 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003801 if (fast_search) {
Josef Bacik70c8a912012-10-11 16:54:30 -04003802 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003803 if (ret) {
3804 err = ret;
3805 goto out_unlock;
3806 }
Liu Bo06d3d222012-08-27 10:52:19 -06003807 } else {
3808 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3809 struct extent_map *em, *n;
3810
Miao Xiebbe14262012-11-01 07:34:54 +00003811 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06003812 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3813 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00003814 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003815 }
3816
Chris Mason9623f9a2008-09-11 17:42:42 -04003817 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04003818 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003819 if (ret) {
3820 err = ret;
3821 goto out_unlock;
3822 }
Chris Masone02119d2008-09-05 16:13:11 -04003823 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04003824 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06003825 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003826out_unlock:
Josef Bacik2ab28f32012-10-12 15:27:49 -04003827 if (err)
3828 btrfs_free_logged_extents(log, log->log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003829 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3830
3831 btrfs_free_path(path);
3832 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003833 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003834}
3835
Chris Mason12fcfd22009-03-24 10:24:20 -04003836/*
3837 * follow the dentry parent pointers up the chain and see if any
3838 * of the directories in it require a full commit before they can
3839 * be logged. Returns zero if nothing special needs to be done or 1 if
3840 * a full commit is required.
3841 */
3842static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3843 struct inode *inode,
3844 struct dentry *parent,
3845 struct super_block *sb,
3846 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003847{
Chris Mason12fcfd22009-03-24 10:24:20 -04003848 int ret = 0;
3849 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003850 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04003851 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04003852
Chris Masonaf4176b2009-03-24 10:24:31 -04003853 /*
3854 * for regular files, if its inode is already on disk, we don't
3855 * have to worry about the parents at all. This is because
3856 * we can use the last_unlink_trans field to record renames
3857 * and other fun in this file.
3858 */
3859 if (S_ISREG(inode->i_mode) &&
3860 BTRFS_I(inode)->generation <= last_committed &&
3861 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3862 goto out;
3863
Chris Mason12fcfd22009-03-24 10:24:20 -04003864 if (!S_ISDIR(inode->i_mode)) {
3865 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3866 goto out;
3867 inode = parent->d_inode;
3868 }
3869
3870 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04003871 /*
3872 * If we are logging a directory then we start with our inode,
3873 * not our parents inode, so we need to skipp setting the
3874 * logged_trans so that further down in the log code we don't
3875 * think this inode has already been logged.
3876 */
3877 if (inode != orig_inode)
3878 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04003879 smp_mb();
3880
3881 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3882 root = BTRFS_I(inode)->root;
3883
3884 /*
3885 * make sure any commits to the log are forced
3886 * to be full commits
3887 */
3888 root->fs_info->last_trans_log_full_commit =
3889 trans->transid;
3890 ret = 1;
3891 break;
3892 }
3893
3894 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3895 break;
3896
Yan, Zheng76dda932009-09-21 16:00:26 -04003897 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003898 break;
3899
Josef Bacik6a912212010-11-20 09:48:00 +00003900 parent = dget_parent(parent);
3901 dput(old_parent);
3902 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003903 inode = parent->d_inode;
3904
3905 }
Josef Bacik6a912212010-11-20 09:48:00 +00003906 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003907out:
Chris Masone02119d2008-09-05 16:13:11 -04003908 return ret;
3909}
3910
3911/*
3912 * helper function around btrfs_log_inode to make sure newly created
3913 * parent directories also end up in the log. A minimal inode and backref
3914 * only logging is done of any parent directories that are older than
3915 * the last committed transaction
3916 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003917static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3918 struct btrfs_root *root, struct inode *inode,
3919 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003920{
Chris Mason12fcfd22009-03-24 10:24:20 -04003921 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003922 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003923 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003924 int ret = 0;
3925 u64 last_committed = root->fs_info->last_trans_committed;
3926
3927 sb = inode->i_sb;
3928
Sage Weil3a5e1402009-04-02 16:49:40 -04003929 if (btrfs_test_opt(root, NOTREELOG)) {
3930 ret = 1;
3931 goto end_no_trans;
3932 }
3933
Chris Mason12fcfd22009-03-24 10:24:20 -04003934 if (root->fs_info->last_trans_log_full_commit >
3935 root->fs_info->last_trans_committed) {
3936 ret = 1;
3937 goto end_no_trans;
3938 }
3939
Yan, Zheng76dda932009-09-21 16:00:26 -04003940 if (root != BTRFS_I(inode)->root ||
3941 btrfs_root_refs(&root->root_item) == 0) {
3942 ret = 1;
3943 goto end_no_trans;
3944 }
3945
Chris Mason12fcfd22009-03-24 10:24:20 -04003946 ret = check_parent_dirs_for_sync(trans, inode, parent,
3947 sb, last_committed);
3948 if (ret)
3949 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003950
Josef Bacik22ee6982012-05-29 16:57:49 -04003951 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04003952 ret = BTRFS_NO_LOG_SYNC;
3953 goto end_no_trans;
3954 }
3955
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003956 ret = start_log_trans(trans, root);
3957 if (ret)
3958 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003959
3960 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003961 if (ret)
3962 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003963
Chris Masonaf4176b2009-03-24 10:24:31 -04003964 /*
3965 * for regular files, if its inode is already on disk, we don't
3966 * have to worry about the parents at all. This is because
3967 * we can use the last_unlink_trans field to record renames
3968 * and other fun in this file.
3969 */
3970 if (S_ISREG(inode->i_mode) &&
3971 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003972 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3973 ret = 0;
3974 goto end_trans;
3975 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003976
3977 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003978 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003979 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003980 break;
3981
Chris Mason12fcfd22009-03-24 10:24:20 -04003982 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003983 if (root != BTRFS_I(inode)->root)
3984 break;
3985
Chris Mason12fcfd22009-03-24 10:24:20 -04003986 if (BTRFS_I(inode)->generation >
3987 root->fs_info->last_trans_committed) {
3988 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003989 if (ret)
3990 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003991 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003992 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003993 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003994
Josef Bacik6a912212010-11-20 09:48:00 +00003995 parent = dget_parent(parent);
3996 dput(old_parent);
3997 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003998 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003999 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004000end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004001 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004002 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004003 root->fs_info->last_trans_log_full_commit = trans->transid;
4004 ret = 1;
4005 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004006 btrfs_end_log_trans(root);
4007end_no_trans:
4008 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004009}
4010
4011/*
4012 * it is not safe to log dentry if the chunk root has added new
4013 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4014 * If this returns 1, you must commit the transaction to safely get your
4015 * data on disk.
4016 */
4017int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4018 struct btrfs_root *root, struct dentry *dentry)
4019{
Josef Bacik6a912212010-11-20 09:48:00 +00004020 struct dentry *parent = dget_parent(dentry);
4021 int ret;
4022
4023 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
4024 dput(parent);
4025
4026 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004027}
4028
4029/*
4030 * should be called during mount to recover any replay any log trees
4031 * from the FS
4032 */
4033int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4034{
4035 int ret;
4036 struct btrfs_path *path;
4037 struct btrfs_trans_handle *trans;
4038 struct btrfs_key key;
4039 struct btrfs_key found_key;
4040 struct btrfs_key tmp_key;
4041 struct btrfs_root *log;
4042 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4043 struct walk_control wc = {
4044 .process_func = process_one_buffer,
4045 .stage = 0,
4046 };
4047
Chris Masone02119d2008-09-05 16:13:11 -04004048 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004049 if (!path)
4050 return -ENOMEM;
4051
4052 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004053
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004054 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 if (IS_ERR(trans)) {
4056 ret = PTR_ERR(trans);
4057 goto error;
4058 }
Chris Masone02119d2008-09-05 16:13:11 -04004059
4060 wc.trans = trans;
4061 wc.pin = 1;
4062
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004063 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004064 if (ret) {
4065 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4066 "recovering log root tree.");
4067 goto error;
4068 }
Chris Masone02119d2008-09-05 16:13:11 -04004069
4070again:
4071 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4072 key.offset = (u64)-1;
4073 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4074
Chris Masond3977122009-01-05 21:25:51 -05004075 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004076 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077
4078 if (ret < 0) {
4079 btrfs_error(fs_info, ret,
4080 "Couldn't find tree log root.");
4081 goto error;
4082 }
Chris Masone02119d2008-09-05 16:13:11 -04004083 if (ret > 0) {
4084 if (path->slots[0] == 0)
4085 break;
4086 path->slots[0]--;
4087 }
4088 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4089 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004090 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004091 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4092 break;
4093
Miao Xiecb517ea2013-05-15 07:48:19 +00004094 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (IS_ERR(log)) {
4096 ret = PTR_ERR(log);
4097 btrfs_error(fs_info, ret,
4098 "Couldn't read tree log root.");
4099 goto error;
4100 }
Chris Masone02119d2008-09-05 16:13:11 -04004101
4102 tmp_key.objectid = found_key.offset;
4103 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4104 tmp_key.offset = (u64)-1;
4105
4106 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (IS_ERR(wc.replay_dest)) {
4108 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004109 free_extent_buffer(log->node);
4110 free_extent_buffer(log->commit_root);
4111 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 btrfs_error(fs_info, ret, "Couldn't read target root "
4113 "for tree log recovery.");
4114 goto error;
4115 }
Chris Masone02119d2008-09-05 16:13:11 -04004116
Yan Zheng07d400a2009-01-06 11:42:00 -05004117 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004118 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004119 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004120
Josef Bacikb50c6e22013-04-25 15:55:30 -04004121 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004122 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4123 path);
Chris Masone02119d2008-09-05 16:13:11 -04004124 }
Chris Masone02119d2008-09-05 16:13:11 -04004125
4126 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004127 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004128 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004129 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004130 kfree(log);
4131
Josef Bacikb50c6e22013-04-25 15:55:30 -04004132 if (ret)
4133 goto error;
4134
Chris Masone02119d2008-09-05 16:13:11 -04004135 if (found_key.offset == 0)
4136 break;
4137 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004138 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004139
4140 /* step one is to pin it all, step two is to replay just inodes */
4141 if (wc.pin) {
4142 wc.pin = 0;
4143 wc.process_func = replay_one_buffer;
4144 wc.stage = LOG_WALK_REPLAY_INODES;
4145 goto again;
4146 }
4147 /* step three is to replay everything */
4148 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4149 wc.stage++;
4150 goto again;
4151 }
4152
4153 btrfs_free_path(path);
4154
Josef Bacikabefa552013-04-24 16:40:05 -04004155 /* step 4: commit the transaction, which also unpins the blocks */
4156 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4157 if (ret)
4158 return ret;
4159
Chris Masone02119d2008-09-05 16:13:11 -04004160 free_extent_buffer(log_root_tree->node);
4161 log_root_tree->log_root = NULL;
4162 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004163 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164
Josef Bacikabefa552013-04-24 16:40:05 -04004165 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004167 if (wc.trans)
4168 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004169 btrfs_free_path(path);
4170 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004171}
Chris Mason12fcfd22009-03-24 10:24:20 -04004172
4173/*
4174 * there are some corner cases where we want to force a full
4175 * commit instead of allowing a directory to be logged.
4176 *
4177 * They revolve around files there were unlinked from the directory, and
4178 * this function updates the parent directory so that a full commit is
4179 * properly done if it is fsync'd later after the unlinks are done.
4180 */
4181void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4182 struct inode *dir, struct inode *inode,
4183 int for_rename)
4184{
4185 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004186 * when we're logging a file, if it hasn't been renamed
4187 * or unlinked, and its inode is fully committed on disk,
4188 * we don't have to worry about walking up the directory chain
4189 * to log its parents.
4190 *
4191 * So, we use the last_unlink_trans field to put this transid
4192 * into the file. When the file is logged we check it and
4193 * don't log the parents if the file is fully on disk.
4194 */
4195 if (S_ISREG(inode->i_mode))
4196 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4197
4198 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004199 * if this directory was already logged any new
4200 * names for this file/dir will get recorded
4201 */
4202 smp_mb();
4203 if (BTRFS_I(dir)->logged_trans == trans->transid)
4204 return;
4205
4206 /*
4207 * if the inode we're about to unlink was logged,
4208 * the log will be properly updated for any new names
4209 */
4210 if (BTRFS_I(inode)->logged_trans == trans->transid)
4211 return;
4212
4213 /*
4214 * when renaming files across directories, if the directory
4215 * there we're unlinking from gets fsync'd later on, there's
4216 * no way to find the destination directory later and fsync it
4217 * properly. So, we have to be conservative and force commits
4218 * so the new name gets discovered.
4219 */
4220 if (for_rename)
4221 goto record;
4222
4223 /* we can safely do the unlink without any special recording */
4224 return;
4225
4226record:
4227 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4228}
4229
4230/*
4231 * Call this after adding a new name for a file and it will properly
4232 * update the log to reflect the new name.
4233 *
4234 * It will return zero if all goes well, and it will return 1 if a
4235 * full transaction commit is required.
4236 */
4237int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4238 struct inode *inode, struct inode *old_dir,
4239 struct dentry *parent)
4240{
4241 struct btrfs_root * root = BTRFS_I(inode)->root;
4242
4243 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004244 * this will force the logging code to walk the dentry chain
4245 * up for the file
4246 */
4247 if (S_ISREG(inode->i_mode))
4248 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4249
4250 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004251 * if this inode hasn't been logged and directory we're renaming it
4252 * from hasn't been logged, we don't need to log it
4253 */
4254 if (BTRFS_I(inode)->logged_trans <=
4255 root->fs_info->last_trans_committed &&
4256 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4257 root->fs_info->last_trans_committed))
4258 return 0;
4259
4260 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
4261}
4262