blob: 3ef7f386e265333c7909e463176040d19bbfa6d1 [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>
Chris Masone02119d2008-09-05 16:13:11 -040021#include "ctree.h"
22#include "transaction.h"
23#include "disk-io.h"
24#include "locking.h"
25#include "print-tree.h"
26#include "compat.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050027#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040028
29/* magic values for the inode_only field in btrfs_log_inode:
30 *
31 * LOG_INODE_ALL means to log everything
32 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 * during log replay
34 */
35#define LOG_INODE_ALL 0
36#define LOG_INODE_EXISTS 1
37
38/*
Chris Mason12fcfd22009-03-24 10:24:20 -040039 * directory trouble cases
40 *
41 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
42 * log, we must force a full commit before doing an fsync of the directory
43 * where the unlink was done.
44 * ---> record transid of last unlink/rename per directory
45 *
46 * mkdir foo/some_dir
47 * normal commit
48 * rename foo/some_dir foo2/some_dir
49 * mkdir foo/some_dir
50 * fsync foo/some_dir/some_file
51 *
52 * The fsync above will unlink the original some_dir without recording
53 * it in its new location (foo2). After a crash, some_dir will be gone
54 * unless the fsync of some_file forces a full commit
55 *
56 * 2) we must log any new names for any file or dir that is in the fsync
57 * log. ---> check inode while renaming/linking.
58 *
59 * 2a) we must log any new names for any file or dir during rename
60 * when the directory they are being removed from was logged.
61 * ---> check inode and old parent dir during rename
62 *
63 * 2a is actually the more important variant. With the extra logging
64 * a crash might unlink the old name without recreating the new one
65 *
66 * 3) after a crash, we must go through any directories with a link count
67 * of zero and redo the rm -rf
68 *
69 * mkdir f1/foo
70 * normal commit
71 * rm -rf f1/foo
72 * fsync(f1)
73 *
74 * The directory f1 was fully removed from the FS, but fsync was never
75 * called on f1, only its parent dir. After a crash the rm -rf must
76 * be replayed. This must be able to recurse down the entire
77 * directory tree. The inode link count fixup code takes care of the
78 * ugly details.
79 */
80
81/*
Chris Masone02119d2008-09-05 16:13:11 -040082 * stages for the tree walking. The first
83 * stage (0) is to only pin down the blocks we find
84 * the second stage (1) is to make sure that all the inodes
85 * we find in the log are created in the subvolume.
86 *
87 * The last stage is to deal with directories and links and extents
88 * and all the other fun semantics
89 */
90#define LOG_WALK_PIN_ONLY 0
91#define LOG_WALK_REPLAY_INODES 1
92#define LOG_WALK_REPLAY_ALL 2
93
Chris Mason12fcfd22009-03-24 10:24:20 -040094static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040095 struct btrfs_root *root, struct inode *inode,
96 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -050097static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_root *log,
103 struct btrfs_path *path,
104 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400105
106/*
107 * tree logging is a special write ahead log used to make sure that
108 * fsyncs and O_SYNCs can happen without doing full tree commits.
109 *
110 * Full tree commits are expensive because they require commonly
111 * modified blocks to be recowed, creating many dirty pages in the
112 * extent tree an 4x-6x higher write load than ext3.
113 *
114 * Instead of doing a tree commit on every fsync, we use the
115 * key ranges and transaction ids to find items for a given file or directory
116 * that have changed in this transaction. Those items are copied into
117 * a special tree (one per subvolume root), that tree is written to disk
118 * and then the fsync is considered complete.
119 *
120 * After a crash, items are copied out of the log-tree back into the
121 * subvolume tree. Any file data extents found are recorded in the extent
122 * allocation tree, and the log-tree freed.
123 *
124 * The log tree is read three times, once to pin down all the extents it is
125 * using in ram and once, once to create all the inodes logged in the tree
126 * and once to do all the other items.
127 */
128
129/*
Chris Masone02119d2008-09-05 16:13:11 -0400130 * start a sub transaction and setup the log tree
131 * this increments the log tree writer count to make the people
132 * syncing the tree wait for us to finish
133 */
134static int start_log_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root)
136{
137 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400138 int err = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500139
140 mutex_lock(&root->log_mutex);
141 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400142 if (!root->log_start_pid) {
143 root->log_start_pid = current->pid;
144 root->log_multiple_pids = false;
145 } else if (root->log_start_pid != current->pid) {
146 root->log_multiple_pids = true;
147 }
148
Yan Zheng7237f182009-01-21 12:54:03 -0500149 root->log_batch++;
150 atomic_inc(&root->log_writers);
151 mutex_unlock(&root->log_mutex);
152 return 0;
153 }
Josef Bacikff782e02009-10-08 15:30:04 -0400154 root->log_multiple_pids = false;
155 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400156 mutex_lock(&root->fs_info->tree_log_mutex);
157 if (!root->fs_info->log_root_tree) {
158 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400159 if (ret)
160 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400161 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400162 if (err == 0 && !root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400163 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400164 if (ret)
165 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Chris Masone02119d2008-09-05 16:13:11 -0400167 mutex_unlock(&root->fs_info->tree_log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -0500168 root->log_batch++;
169 atomic_inc(&root->log_writers);
170 mutex_unlock(&root->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400171 return err;
Chris Masone02119d2008-09-05 16:13:11 -0400172}
173
174/*
175 * returns 0 if there was a log transaction running and we were able
176 * to join, or returns -ENOENT if there were not transactions
177 * in progress
178 */
179static int join_running_log_trans(struct btrfs_root *root)
180{
181 int ret = -ENOENT;
182
183 smp_mb();
184 if (!root->log_root)
185 return -ENOENT;
186
Yan Zheng7237f182009-01-21 12:54:03 -0500187 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400188 if (root->log_root) {
189 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500190 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400191 }
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400193 return ret;
194}
195
196/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400197 * This either makes the current running log transaction wait
198 * until you call btrfs_end_log_trans() or it makes any future
199 * log transactions wait until you call btrfs_end_log_trans()
200 */
201int btrfs_pin_log_trans(struct btrfs_root *root)
202{
203 int ret = -ENOENT;
204
205 mutex_lock(&root->log_mutex);
206 atomic_inc(&root->log_writers);
207 mutex_unlock(&root->log_mutex);
208 return ret;
209}
210
211/*
Chris Masone02119d2008-09-05 16:13:11 -0400212 * indicate we're done making changes to the log tree
213 * and wake up anyone waiting to do a sync
214 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100215void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400216{
Yan Zheng7237f182009-01-21 12:54:03 -0500217 if (atomic_dec_and_test(&root->log_writers)) {
218 smp_mb();
219 if (waitqueue_active(&root->log_writer_wait))
220 wake_up(&root->log_writer_wait);
221 }
Chris Masone02119d2008-09-05 16:13:11 -0400222}
223
224
225/*
226 * the walk control struct is used to pass state down the chain when
227 * processing the log tree. The stage field tells us which part
228 * of the log tree processing we are currently doing. The others
229 * are state fields used for that specific part
230 */
231struct walk_control {
232 /* should we free the extent on disk when done? This is used
233 * at transaction commit time while freeing a log tree
234 */
235 int free;
236
237 /* should we write out the extent buffer? This is used
238 * while flushing the log tree to disk during a sync
239 */
240 int write;
241
242 /* should we wait for the extent buffer io to finish? Also used
243 * while flushing the log tree to disk for a sync
244 */
245 int wait;
246
247 /* pin only walk, we record which extents on disk belong to the
248 * log trees
249 */
250 int pin;
251
252 /* what stage of the replay code we're currently in */
253 int stage;
254
255 /* the root we are currently replaying */
256 struct btrfs_root *replay_dest;
257
258 /* the trans handle for the current replay */
259 struct btrfs_trans_handle *trans;
260
261 /* the function that gets used to process blocks we find in the
262 * tree. Note the extent_buffer might not be up to date when it is
263 * passed in, and it must be checked or read if you need the data
264 * inside it
265 */
266 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
267 struct walk_control *wc, u64 gen);
268};
269
270/*
271 * process_func used to pin down extents, write them or wait on them
272 */
273static int process_one_buffer(struct btrfs_root *log,
274 struct extent_buffer *eb,
275 struct walk_control *wc, u64 gen)
276{
Josef Bacik04018de2009-04-03 10:14:18 -0400277 if (wc->pin)
Chris Masone688b722011-10-31 20:52:39 -0400278 btrfs_pin_extent_for_log_replay(wc->trans,
279 log->fs_info->extent_root,
280 eb->start, eb->len);
Chris Masone02119d2008-09-05 16:13:11 -0400281
Chris Masonb9fab912012-05-06 07:23:47 -0400282 if (btrfs_buffer_uptodate(eb, gen, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -0400283 if (wc->write)
284 btrfs_write_tree_block(eb);
285 if (wc->wait)
286 btrfs_wait_tree_block_writeback(eb);
287 }
288 return 0;
289}
290
291/*
292 * Item overwrite used by replay and tree logging. eb, slot and key all refer
293 * to the src data we are copying out.
294 *
295 * root is the tree we are copying into, and path is a scratch
296 * path for use in this function (it should be released on entry and
297 * will be released on exit).
298 *
299 * If the key is already in the destination tree the existing item is
300 * overwritten. If the existing item isn't big enough, it is extended.
301 * If it is too large, it is truncated.
302 *
303 * If the key isn't in the destination yet, a new item is inserted.
304 */
305static noinline int overwrite_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path,
308 struct extent_buffer *eb, int slot,
309 struct btrfs_key *key)
310{
311 int ret;
312 u32 item_size;
313 u64 saved_i_size = 0;
314 int save_old_i_size = 0;
315 unsigned long src_ptr;
316 unsigned long dst_ptr;
317 int overwrite_root = 0;
Josef Bacik8fdeb712013-04-05 20:50:09 +0000318 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400319
320 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
321 overwrite_root = 1;
322
323 item_size = btrfs_item_size_nr(eb, slot);
324 src_ptr = btrfs_item_ptr_offset(eb, slot);
325
326 /* look for the key in the destination tree */
327 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik8fdeb712013-04-05 20:50:09 +0000328 if (ret < 0)
329 return ret;
330
Chris Masone02119d2008-09-05 16:13:11 -0400331 if (ret == 0) {
332 char *src_copy;
333 char *dst_copy;
334 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
335 path->slots[0]);
336 if (dst_size != item_size)
337 goto insert;
338
339 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200340 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400341 return 0;
342 }
343 dst_copy = kmalloc(item_size, GFP_NOFS);
344 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000345 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200346 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000347 kfree(dst_copy);
348 kfree(src_copy);
349 return -ENOMEM;
350 }
Chris Masone02119d2008-09-05 16:13:11 -0400351
352 read_extent_buffer(eb, src_copy, src_ptr, item_size);
353
354 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
355 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
356 item_size);
357 ret = memcmp(dst_copy, src_copy, item_size);
358
359 kfree(dst_copy);
360 kfree(src_copy);
361 /*
362 * they have the same contents, just return, this saves
363 * us from cowing blocks in the destination tree and doing
364 * extra writes that may not have been done by a previous
365 * sync
366 */
367 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200368 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400369 return 0;
370 }
371
Josef Bacik8fdeb712013-04-05 20:50:09 +0000372 /*
373 * We need to load the old nbytes into the inode so when we
374 * replay the extents we've logged we get the right nbytes.
375 */
376 if (inode_item) {
377 struct btrfs_inode_item *item;
378 u64 nbytes;
379
380 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
381 struct btrfs_inode_item);
382 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
383 item = btrfs_item_ptr(eb, slot,
384 struct btrfs_inode_item);
385 btrfs_set_inode_nbytes(eb, item, nbytes);
386 }
387 } else if (inode_item) {
388 struct btrfs_inode_item *item;
389
390 /*
391 * New inode, set nbytes to 0 so that the nbytes comes out
392 * properly when we replay the extents.
393 */
394 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
395 btrfs_set_inode_nbytes(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400396 }
397insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200398 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400399 /* try to insert the key into the destination tree */
400 ret = btrfs_insert_empty_item(trans, root, path,
401 key, item_size);
402
403 /* make sure any existing item is the correct size */
404 if (ret == -EEXIST) {
405 u32 found_size;
406 found_size = btrfs_item_size_nr(path->nodes[0],
407 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100408 if (found_size > item_size)
Chris Masone02119d2008-09-05 16:13:11 -0400409 btrfs_truncate_item(trans, root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100410 else if (found_size < item_size)
411 btrfs_extend_item(trans, root, path,
412 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400413 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400414 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400415 }
416 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
417 path->slots[0]);
418
419 /* don't overwrite an existing inode if the generation number
420 * was logged as zero. This is done when the tree logging code
421 * is just logging an inode to make sure it exists after recovery.
422 *
423 * Also, don't overwrite i_size on directories during replay.
424 * log replay inserts and removes directory items based on the
425 * state of the tree found in the subvolume, and i_size is modified
426 * as it goes
427 */
428 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
429 struct btrfs_inode_item *src_item;
430 struct btrfs_inode_item *dst_item;
431
432 src_item = (struct btrfs_inode_item *)src_ptr;
433 dst_item = (struct btrfs_inode_item *)dst_ptr;
434
435 if (btrfs_inode_generation(eb, src_item) == 0)
436 goto no_copy;
437
438 if (overwrite_root &&
439 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
440 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
441 save_old_i_size = 1;
442 saved_i_size = btrfs_inode_size(path->nodes[0],
443 dst_item);
444 }
445 }
446
447 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
448 src_ptr, item_size);
449
450 if (save_old_i_size) {
451 struct btrfs_inode_item *dst_item;
452 dst_item = (struct btrfs_inode_item *)dst_ptr;
453 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
454 }
455
456 /* make sure the generation is filled in */
457 if (key->type == BTRFS_INODE_ITEM_KEY) {
458 struct btrfs_inode_item *dst_item;
459 dst_item = (struct btrfs_inode_item *)dst_ptr;
460 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
461 btrfs_set_inode_generation(path->nodes[0], dst_item,
462 trans->transid);
463 }
464 }
465no_copy:
466 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200467 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400468 return 0;
469}
470
471/*
472 * simple helper to read an inode off the disk from a given root
473 * This can only be called for subvolume roots and not for the log
474 */
475static noinline struct inode *read_one_inode(struct btrfs_root *root,
476 u64 objectid)
477{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400479 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400480
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400481 key.objectid = objectid;
482 key.type = BTRFS_INODE_ITEM_KEY;
483 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000484 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400485 if (IS_ERR(inode)) {
486 inode = NULL;
487 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400488 iput(inode);
489 inode = NULL;
490 }
491 return inode;
492}
493
494/* replays a single extent in 'eb' at 'slot' with 'key' into the
495 * subvolume 'root'. path is released on entry and should be released
496 * on exit.
497 *
498 * extents in the log tree have not been allocated out of the extent
499 * tree yet. So, this completes the allocation, taking a reference
500 * as required if the extent already exists or creating a new extent
501 * if it isn't in the extent allocation tree yet.
502 *
503 * The extent is inserted into the file, dropping any existing extents
504 * from the file that overlap the new one.
505 */
506static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 struct extent_buffer *eb, int slot,
510 struct btrfs_key *key)
511{
512 int found_type;
513 u64 mask = root->sectorsize - 1;
514 u64 extent_end;
515 u64 alloc_hint;
516 u64 start = key->offset;
Josef Bacik8fdeb712013-04-05 20:50:09 +0000517 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400518 struct btrfs_file_extent_item *item;
519 struct inode *inode = NULL;
520 unsigned long size;
521 int ret = 0;
522
523 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
524 found_type = btrfs_file_extent_type(eb, item);
525
Yan Zhengd899e052008-10-30 14:25:28 -0400526 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik8fdeb712013-04-05 20:50:09 +0000527 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
528 nbytes = btrfs_file_extent_num_bytes(eb, item);
529 extent_end = start + nbytes;
530
531 /*
532 * We don't add to the inodes nbytes if we are prealloc or a
533 * hole.
534 */
535 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
536 nbytes = 0;
537 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400538 size = btrfs_file_extent_inline_len(eb, item);
Josef Bacik8fdeb712013-04-05 20:50:09 +0000539 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Chris Masone02119d2008-09-05 16:13:11 -0400540 extent_end = (start + size + mask) & ~mask;
541 } else {
542 ret = 0;
543 goto out;
544 }
545
546 inode = read_one_inode(root, key->objectid);
547 if (!inode) {
548 ret = -EIO;
549 goto out;
550 }
551
552 /*
553 * first check to see if we already have this extent in the
554 * file. This must be done before the btrfs_drop_extents run
555 * so we don't try to drop this extent.
556 */
Li Zefan33345d012011-04-20 10:31:50 +0800557 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400558 start, 0);
559
Yan Zhengd899e052008-10-30 14:25:28 -0400560 if (ret == 0 &&
561 (found_type == BTRFS_FILE_EXTENT_REG ||
562 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400563 struct btrfs_file_extent_item cmp1;
564 struct btrfs_file_extent_item cmp2;
565 struct btrfs_file_extent_item *existing;
566 struct extent_buffer *leaf;
567
568 leaf = path->nodes[0];
569 existing = btrfs_item_ptr(leaf, path->slots[0],
570 struct btrfs_file_extent_item);
571
572 read_extent_buffer(eb, &cmp1, (unsigned long)item,
573 sizeof(cmp1));
574 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
575 sizeof(cmp2));
576
577 /*
578 * we already have a pointer to this exact extent,
579 * we don't have to do anything
580 */
581 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200582 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400583 goto out;
584 }
585 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200586 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400587
588 /* drop any overlapping extents */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000589 ret = btrfs_drop_extents(trans, inode, start, extent_end,
590 &alloc_hint, 1);
Chris Masone02119d2008-09-05 16:13:11 -0400591 BUG_ON(ret);
592
Yan Zheng07d400a2009-01-06 11:42:00 -0500593 if (found_type == BTRFS_FILE_EXTENT_REG ||
594 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400595 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500596 unsigned long dest_offset;
597 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400598
Yan Zheng07d400a2009-01-06 11:42:00 -0500599 ret = btrfs_insert_empty_item(trans, root, path, key,
600 sizeof(*item));
601 BUG_ON(ret);
602 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
603 path->slots[0]);
604 copy_extent_buffer(path->nodes[0], eb, dest_offset,
605 (unsigned long)item, sizeof(*item));
606
607 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
608 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
609 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500611
612 if (ins.objectid > 0) {
613 u64 csum_start;
614 u64 csum_end;
615 LIST_HEAD(ordered_sums);
616 /*
617 * is this extent already allocated in the extent
618 * allocation tree? If so, just add a reference
619 */
620 ret = btrfs_lookup_extent(root, ins.objectid,
621 ins.offset);
622 if (ret == 0) {
623 ret = btrfs_inc_extent_ref(trans, root,
624 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400625 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200626 key->objectid, offset, 0);
Tsutomu Itoh37daa4f2011-04-28 09:18:21 +0000627 BUG_ON(ret);
Yan Zheng07d400a2009-01-06 11:42:00 -0500628 } else {
629 /*
630 * insert the extent pointer in the extent
631 * allocation tree
632 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400633 ret = btrfs_alloc_logged_file_extent(trans,
634 root, root->root_key.objectid,
635 key->objectid, offset, &ins);
Yan Zheng07d400a2009-01-06 11:42:00 -0500636 BUG_ON(ret);
637 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200638 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500639
640 if (btrfs_file_extent_compression(eb, item)) {
641 csum_start = ins.objectid;
642 csum_end = csum_start + ins.offset;
643 } else {
644 csum_start = ins.objectid +
645 btrfs_file_extent_offset(eb, item);
646 csum_end = csum_start +
647 btrfs_file_extent_num_bytes(eb, item);
648 }
649
650 ret = btrfs_lookup_csums_range(root->log_root,
651 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100652 &ordered_sums, 0);
Yan Zheng07d400a2009-01-06 11:42:00 -0500653 BUG_ON(ret);
654 while (!list_empty(&ordered_sums)) {
655 struct btrfs_ordered_sum *sums;
656 sums = list_entry(ordered_sums.next,
657 struct btrfs_ordered_sum,
658 list);
659 ret = btrfs_csum_file_blocks(trans,
660 root->fs_info->csum_root,
661 sums);
662 BUG_ON(ret);
663 list_del(&sums->list);
664 kfree(sums);
665 }
666 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200667 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500668 }
669 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
670 /* inline extents are easy, we just overwrite them */
671 ret = overwrite_item(trans, root, path, eb, slot, key);
672 BUG_ON(ret);
673 }
674
Josef Bacik8fdeb712013-04-05 20:50:09 +0000675 inode_add_bytes(inode, nbytes);
Chris Masone02119d2008-09-05 16:13:11 -0400676 btrfs_update_inode(trans, root, inode);
677out:
678 if (inode)
679 iput(inode);
680 return ret;
681}
682
683/*
684 * when cleaning up conflicts between the directory names in the
685 * subvolume, directory names in the log and directory names in the
686 * inode back references, we may have to unlink inodes from directories.
687 *
688 * This is a helper function to do the unlink of a specific directory
689 * item
690 */
691static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
692 struct btrfs_root *root,
693 struct btrfs_path *path,
694 struct inode *dir,
695 struct btrfs_dir_item *di)
696{
697 struct inode *inode;
698 char *name;
699 int name_len;
700 struct extent_buffer *leaf;
701 struct btrfs_key location;
702 int ret;
703
704 leaf = path->nodes[0];
705
706 btrfs_dir_item_key_to_cpu(leaf, di, &location);
707 name_len = btrfs_dir_name_len(leaf, di);
708 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000709 if (!name)
710 return -ENOMEM;
711
Chris Masone02119d2008-09-05 16:13:11 -0400712 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200713 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400714
715 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000716 if (!inode) {
717 kfree(name);
718 return -EIO;
719 }
Chris Masone02119d2008-09-05 16:13:11 -0400720
Yan Zhengec051c02009-01-05 15:43:42 -0500721 ret = link_to_fixup_dir(trans, root, path, location.objectid);
722 BUG_ON(ret);
Chris Mason12fcfd22009-03-24 10:24:20 -0400723
Chris Masone02119d2008-09-05 16:13:11 -0400724 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Yan Zhengec051c02009-01-05 15:43:42 -0500725 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -0400726 kfree(name);
727
728 iput(inode);
Chris Masondbc90432012-07-02 15:29:53 -0400729
730 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400731 return ret;
732}
733
734/*
735 * helper function to see if a given name and sequence number found
736 * in an inode back reference are already in a directory and correctly
737 * point to this inode
738 */
739static noinline int inode_in_dir(struct btrfs_root *root,
740 struct btrfs_path *path,
741 u64 dirid, u64 objectid, u64 index,
742 const char *name, int name_len)
743{
744 struct btrfs_dir_item *di;
745 struct btrfs_key location;
746 int match = 0;
747
748 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
749 index, name, name_len, 0);
750 if (di && !IS_ERR(di)) {
751 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
752 if (location.objectid != objectid)
753 goto out;
754 } else
755 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200756 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400757
758 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
759 if (di && !IS_ERR(di)) {
760 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
761 if (location.objectid != objectid)
762 goto out;
763 } else
764 goto out;
765 match = 1;
766out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200767 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400768 return match;
769}
770
771/*
772 * helper function to check a log tree for a named back reference in
773 * an inode. This is used to decide if a back reference that is
774 * found in the subvolume conflicts with what we find in the log.
775 *
776 * inode backreferences may have multiple refs in a single item,
777 * during replay we process one reference at a time, and we don't
778 * want to delete valid links to a file from the subvolume if that
779 * link is also in the log.
780 */
781static noinline int backref_in_log(struct btrfs_root *log,
782 struct btrfs_key *key,
783 char *name, int namelen)
784{
785 struct btrfs_path *path;
786 struct btrfs_inode_ref *ref;
787 unsigned long ptr;
788 unsigned long ptr_end;
789 unsigned long name_ptr;
790 int found_name_len;
791 int item_size;
792 int ret;
793 int match = 0;
794
795 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000796 if (!path)
797 return -ENOMEM;
798
Chris Masone02119d2008-09-05 16:13:11 -0400799 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
800 if (ret != 0)
801 goto out;
802
803 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
804 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
805 ptr_end = ptr + item_size;
806 while (ptr < ptr_end) {
807 ref = (struct btrfs_inode_ref *)ptr;
808 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
809 if (found_name_len == namelen) {
810 name_ptr = (unsigned long)(ref + 1);
811 ret = memcmp_extent_buffer(path->nodes[0], name,
812 name_ptr, namelen);
813 if (ret == 0) {
814 match = 1;
815 goto out;
816 }
817 }
818 ptr = (unsigned long)(ref + 1) + found_name_len;
819 }
820out:
821 btrfs_free_path(path);
822 return match;
823}
824
825
826/*
827 * replay one inode back reference item found in the log tree.
828 * eb, slot and key refer to the buffer and key found in the log tree.
829 * root is the destination we are replaying into, and path is for temp
830 * use by this function. (it should be released on return).
831 */
832static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct btrfs_root *log,
835 struct btrfs_path *path,
836 struct extent_buffer *eb, int slot,
837 struct btrfs_key *key)
838{
Chris Masone02119d2008-09-05 16:13:11 -0400839 struct btrfs_inode_ref *ref;
liubo34f3e4f2011-08-06 08:35:23 +0000840 struct btrfs_dir_item *di;
841 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -0400842 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400843 unsigned long ref_ptr;
844 unsigned long ref_end;
liubo34f3e4f2011-08-06 08:35:23 +0000845 char *name;
846 int namelen;
847 int ret;
liuboc622ae62011-03-26 08:01:12 -0400848 int search_done = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400849
Chris Masone02119d2008-09-05 16:13:11 -0400850 /*
851 * it is possible that we didn't log all the parent directories
852 * for a given inode. If we don't find the dir, just don't
853 * copy the back ref in. The link count fixup code will take
854 * care of the rest
855 */
856 dir = read_one_inode(root, key->offset);
857 if (!dir)
858 return -ENOENT;
859
860 inode = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000861 if (!inode) {
862 iput(dir);
863 return -EIO;
864 }
Chris Masone02119d2008-09-05 16:13:11 -0400865
866 ref_ptr = btrfs_item_ptr_offset(eb, slot);
867 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
868
869again:
870 ref = (struct btrfs_inode_ref *)ref_ptr;
871
872 namelen = btrfs_inode_ref_name_len(eb, ref);
873 name = kmalloc(namelen, GFP_NOFS);
874 BUG_ON(!name);
875
876 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
877
878 /* if we already have a perfect match, we're done */
Li Zefan33345d012011-04-20 10:31:50 +0800879 if (inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400880 btrfs_inode_ref_index(eb, ref),
881 name, namelen)) {
882 goto out;
883 }
884
885 /*
886 * look for a conflicting back reference in the metadata.
887 * if we find one we have to unlink that name of the file
888 * before we add our new link. Later on, we overwrite any
889 * existing back reference, and we don't want to create
890 * dangling pointers in the directory.
891 */
liuboc622ae62011-03-26 08:01:12 -0400892
893 if (search_done)
894 goto insert;
895
Chris Masone02119d2008-09-05 16:13:11 -0400896 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
897 if (ret == 0) {
898 char *victim_name;
899 int victim_name_len;
900 struct btrfs_inode_ref *victim_ref;
901 unsigned long ptr;
902 unsigned long ptr_end;
903 struct extent_buffer *leaf = path->nodes[0];
904
905 /* are we trying to overwrite a back ref for the root directory
906 * if so, just jump out, we're done
907 */
908 if (key->objectid == key->offset)
909 goto out_nowrite;
910
911 /* check all the names in this back reference to see
912 * if they are in the log. if so, we allow them to stay
913 * otherwise they must be unlinked as a conflict
914 */
915 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
916 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500917 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -0400918 victim_ref = (struct btrfs_inode_ref *)ptr;
919 victim_name_len = btrfs_inode_ref_name_len(leaf,
920 victim_ref);
921 victim_name = kmalloc(victim_name_len, GFP_NOFS);
922 BUG_ON(!victim_name);
923
924 read_extent_buffer(leaf, victim_name,
925 (unsigned long)(victim_ref + 1),
926 victim_name_len);
927
928 if (!backref_in_log(log, key, victim_name,
929 victim_name_len)) {
930 btrfs_inc_nlink(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +0200931 btrfs_release_path(path);
Chris Mason12fcfd22009-03-24 10:24:20 -0400932
Chris Masone02119d2008-09-05 16:13:11 -0400933 ret = btrfs_unlink_inode(trans, root, dir,
934 inode, victim_name,
935 victim_name_len);
Chris Masondbc90432012-07-02 15:29:53 -0400936 btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400937 }
938 kfree(victim_name);
939 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
940 }
941 BUG_ON(ret);
liuboc622ae62011-03-26 08:01:12 -0400942
943 /*
944 * NOTE: we have searched root tree and checked the
945 * coresponding ref, it does not need to check again.
946 */
947 search_done = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400948 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200949 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400950
liubo34f3e4f2011-08-06 08:35:23 +0000951 /* look for a conflicting sequence number */
952 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
953 btrfs_inode_ref_index(eb, ref),
954 name, namelen, 0);
955 if (di && !IS_ERR(di)) {
956 ret = drop_one_dir_item(trans, root, path, dir, di);
957 BUG_ON(ret);
958 }
959 btrfs_release_path(path);
960
961 /* look for a conflicing name */
962 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
963 name, namelen, 0);
964 if (di && !IS_ERR(di)) {
965 ret = drop_one_dir_item(trans, root, path, dir, di);
966 BUG_ON(ret);
967 }
968 btrfs_release_path(path);
969
liuboc622ae62011-03-26 08:01:12 -0400970insert:
Chris Masone02119d2008-09-05 16:13:11 -0400971 /* insert our name */
972 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
973 btrfs_inode_ref_index(eb, ref));
974 BUG_ON(ret);
975
976 btrfs_update_inode(trans, root, inode);
977
978out:
979 ref_ptr = (unsigned long)(ref + 1) + namelen;
980 kfree(name);
981 if (ref_ptr < ref_end)
982 goto again;
983
984 /* finally write the back reference in the inode */
985 ret = overwrite_item(trans, root, path, eb, slot, key);
986 BUG_ON(ret);
987
988out_nowrite:
David Sterbab3b4aa72011-04-21 01:20:15 +0200989 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400990 iput(dir);
991 iput(inode);
992 return 0;
993}
994
Yan, Zhengc71bf092009-11-12 09:34:40 +0000995static int insert_orphan_item(struct btrfs_trans_handle *trans,
996 struct btrfs_root *root, u64 offset)
997{
998 int ret;
999 ret = btrfs_find_orphan_item(root, offset);
1000 if (ret > 0)
1001 ret = btrfs_insert_orphan_item(trans, root, offset);
1002 return ret;
1003}
1004
1005
Chris Masone02119d2008-09-05 16:13:11 -04001006/*
Chris Masone02119d2008-09-05 16:13:11 -04001007 * There are a few corners where the link count of the file can't
1008 * be properly maintained during replay. So, instead of adding
1009 * lots of complexity to the log code, we just scan the backrefs
1010 * for any file that has been through replay.
1011 *
1012 * The scan will update the link count on the inode to reflect the
1013 * number of back refs found. If it goes down to zero, the iput
1014 * will free the inode.
1015 */
1016static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root,
1018 struct inode *inode)
1019{
1020 struct btrfs_path *path;
1021 int ret;
1022 struct btrfs_key key;
1023 u64 nlink = 0;
1024 unsigned long ptr;
1025 unsigned long ptr_end;
1026 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001027 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001028
Li Zefan33345d012011-04-20 10:31:50 +08001029 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001030 key.type = BTRFS_INODE_REF_KEY;
1031 key.offset = (u64)-1;
1032
1033 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +00001034 if (!path)
1035 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001036
Chris Masond3977122009-01-05 21:25:51 -05001037 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001038 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1039 if (ret < 0)
1040 break;
1041 if (ret > 0) {
1042 if (path->slots[0] == 0)
1043 break;
1044 path->slots[0]--;
1045 }
1046 btrfs_item_key_to_cpu(path->nodes[0], &key,
1047 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001048 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001049 key.type != BTRFS_INODE_REF_KEY)
1050 break;
1051 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1052 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1053 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001054 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001055 struct btrfs_inode_ref *ref;
1056
1057 ref = (struct btrfs_inode_ref *)ptr;
1058 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1059 ref);
1060 ptr = (unsigned long)(ref + 1) + name_len;
1061 nlink++;
1062 }
1063
1064 if (key.offset == 0)
1065 break;
1066 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001067 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001069 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001070 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001071 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001072 btrfs_update_inode(trans, root, inode);
1073 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001074 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001075
Yan, Zhengc71bf092009-11-12 09:34:40 +00001076 if (inode->i_nlink == 0) {
1077 if (S_ISDIR(inode->i_mode)) {
1078 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001079 ino, 1);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001080 BUG_ON(ret);
1081 }
Li Zefan33345d012011-04-20 10:31:50 +08001082 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001083 BUG_ON(ret);
1084 }
1085 btrfs_free_path(path);
1086
Chris Masone02119d2008-09-05 16:13:11 -04001087 return 0;
1088}
1089
1090static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1091 struct btrfs_root *root,
1092 struct btrfs_path *path)
1093{
1094 int ret;
1095 struct btrfs_key key;
1096 struct inode *inode;
1097
1098 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1099 key.type = BTRFS_ORPHAN_ITEM_KEY;
1100 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001101 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103 if (ret < 0)
1104 break;
1105
1106 if (ret == 1) {
1107 if (path->slots[0] == 0)
1108 break;
1109 path->slots[0]--;
1110 }
1111
1112 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1113 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1114 key.type != BTRFS_ORPHAN_ITEM_KEY)
1115 break;
1116
1117 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001118 if (ret)
1119 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001120
David Sterbab3b4aa72011-04-21 01:20:15 +02001121 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001122 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001123 if (!inode)
1124 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001125
1126 ret = fixup_inode_link_count(trans, root, inode);
1127 BUG_ON(ret);
1128
1129 iput(inode);
1130
Chris Mason12fcfd22009-03-24 10:24:20 -04001131 /*
1132 * fixup on a directory may create new entries,
1133 * make sure we always look for the highset possible
1134 * offset
1135 */
1136 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001137 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001138 ret = 0;
1139out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001140 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001141 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001142}
1143
1144
1145/*
1146 * record a given inode in the fixup dir so we can check its link
1147 * count when replay is done. The link count is incremented here
1148 * so the inode won't go away until we check it
1149 */
1150static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1151 struct btrfs_root *root,
1152 struct btrfs_path *path,
1153 u64 objectid)
1154{
1155 struct btrfs_key key;
1156 int ret = 0;
1157 struct inode *inode;
1158
1159 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001160 if (!inode)
1161 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001162
1163 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1164 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1165 key.offset = objectid;
1166
1167 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1168
David Sterbab3b4aa72011-04-21 01:20:15 +02001169 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001170 if (ret == 0) {
1171 btrfs_inc_nlink(inode);
1172 btrfs_update_inode(trans, root, inode);
1173 } else if (ret == -EEXIST) {
1174 ret = 0;
1175 } else {
1176 BUG();
1177 }
1178 iput(inode);
1179
1180 return ret;
1181}
1182
1183/*
1184 * when replaying the log for a directory, we only insert names
1185 * for inodes that actually exist. This means an fsync on a directory
1186 * does not implicitly fsync all the new files in it
1187 */
1188static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 struct btrfs_path *path,
1191 u64 dirid, u64 index,
1192 char *name, int name_len, u8 type,
1193 struct btrfs_key *location)
1194{
1195 struct inode *inode;
1196 struct inode *dir;
1197 int ret;
1198
1199 inode = read_one_inode(root, location->objectid);
1200 if (!inode)
1201 return -ENOENT;
1202
1203 dir = read_one_inode(root, dirid);
1204 if (!dir) {
1205 iput(inode);
1206 return -EIO;
1207 }
1208 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1209
1210 /* FIXME, put inode into FIXUP list */
1211
1212 iput(inode);
1213 iput(dir);
1214 return ret;
1215}
1216
1217/*
1218 * take a single entry in a log directory item and replay it into
1219 * the subvolume.
1220 *
1221 * if a conflicting item exists in the subdirectory already,
1222 * the inode it points to is unlinked and put into the link count
1223 * fix up tree.
1224 *
1225 * If a name from the log points to a file or directory that does
1226 * not exist in the FS, it is skipped. fsyncs on directories
1227 * do not force down inodes inside that directory, just changes to the
1228 * names or unlinks in a directory.
1229 */
1230static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1231 struct btrfs_root *root,
1232 struct btrfs_path *path,
1233 struct extent_buffer *eb,
1234 struct btrfs_dir_item *di,
1235 struct btrfs_key *key)
1236{
1237 char *name;
1238 int name_len;
1239 struct btrfs_dir_item *dst_di;
1240 struct btrfs_key found_key;
1241 struct btrfs_key log_key;
1242 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001243 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001244 int exists;
Chris Masone02119d2008-09-05 16:13:11 -04001245 int ret;
1246
1247 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001248 if (!dir)
1249 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001250
1251 name_len = btrfs_dir_name_len(eb, di);
1252 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00001253 if (!name)
1254 return -ENOMEM;
1255
Chris Masone02119d2008-09-05 16:13:11 -04001256 log_type = btrfs_dir_type(eb, di);
1257 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1258 name_len);
1259
1260 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001261 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1262 if (exists == 0)
1263 exists = 1;
1264 else
1265 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001266 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001267
Chris Masone02119d2008-09-05 16:13:11 -04001268 if (key->type == BTRFS_DIR_ITEM_KEY) {
1269 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1270 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001271 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001272 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1273 key->objectid,
1274 key->offset, name,
1275 name_len, 1);
1276 } else {
1277 BUG();
1278 }
David Sterbac7040052011-04-19 18:00:01 +02001279 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001280 /* we need a sequence number to insert, so we only
1281 * do inserts for the BTRFS_DIR_INDEX_KEY types
1282 */
1283 if (key->type != BTRFS_DIR_INDEX_KEY)
1284 goto out;
1285 goto insert;
1286 }
1287
1288 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1289 /* the existing item matches the logged item */
1290 if (found_key.objectid == log_key.objectid &&
1291 found_key.type == log_key.type &&
1292 found_key.offset == log_key.offset &&
1293 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1294 goto out;
1295 }
1296
1297 /*
1298 * don't drop the conflicting directory entry if the inode
1299 * for the new entry doesn't exist
1300 */
Chris Mason4bef0842008-09-08 11:18:08 -04001301 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001302 goto out;
1303
Chris Masone02119d2008-09-05 16:13:11 -04001304 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1305 BUG_ON(ret);
1306
1307 if (key->type == BTRFS_DIR_INDEX_KEY)
1308 goto insert;
1309out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001310 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001311 kfree(name);
1312 iput(dir);
1313 return 0;
1314
1315insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001316 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001317 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1318 name, name_len, log_type, &log_key);
1319
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001320 BUG_ON(ret && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04001321 goto out;
1322}
1323
1324/*
1325 * find all the names in a directory item and reconcile them into
1326 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1327 * one name in a directory item, but the same code gets used for
1328 * both directory index types
1329 */
1330static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 struct extent_buffer *eb, int slot,
1334 struct btrfs_key *key)
1335{
1336 int ret;
1337 u32 item_size = btrfs_item_size_nr(eb, slot);
1338 struct btrfs_dir_item *di;
1339 int name_len;
1340 unsigned long ptr;
1341 unsigned long ptr_end;
1342
1343 ptr = btrfs_item_ptr_offset(eb, slot);
1344 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001345 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001346 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001347 if (verify_dir_item(root, eb, di))
1348 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001349 name_len = btrfs_dir_name_len(eb, di);
1350 ret = replay_one_name(trans, root, path, eb, di, key);
1351 BUG_ON(ret);
1352 ptr = (unsigned long)(di + 1);
1353 ptr += name_len;
1354 }
1355 return 0;
1356}
1357
1358/*
1359 * directory replay has two parts. There are the standard directory
1360 * items in the log copied from the subvolume, and range items
1361 * created in the log while the subvolume was logged.
1362 *
1363 * The range items tell us which parts of the key space the log
1364 * is authoritative for. During replay, if a key in the subvolume
1365 * directory is in a logged range item, but not actually in the log
1366 * that means it was deleted from the directory before the fsync
1367 * and should be removed.
1368 */
1369static noinline int find_dir_range(struct btrfs_root *root,
1370 struct btrfs_path *path,
1371 u64 dirid, int key_type,
1372 u64 *start_ret, u64 *end_ret)
1373{
1374 struct btrfs_key key;
1375 u64 found_end;
1376 struct btrfs_dir_log_item *item;
1377 int ret;
1378 int nritems;
1379
1380 if (*start_ret == (u64)-1)
1381 return 1;
1382
1383 key.objectid = dirid;
1384 key.type = key_type;
1385 key.offset = *start_ret;
1386
1387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1388 if (ret < 0)
1389 goto out;
1390 if (ret > 0) {
1391 if (path->slots[0] == 0)
1392 goto out;
1393 path->slots[0]--;
1394 }
1395 if (ret != 0)
1396 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1397
1398 if (key.type != key_type || key.objectid != dirid) {
1399 ret = 1;
1400 goto next;
1401 }
1402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1403 struct btrfs_dir_log_item);
1404 found_end = btrfs_dir_log_end(path->nodes[0], item);
1405
1406 if (*start_ret >= key.offset && *start_ret <= found_end) {
1407 ret = 0;
1408 *start_ret = key.offset;
1409 *end_ret = found_end;
1410 goto out;
1411 }
1412 ret = 1;
1413next:
1414 /* check the next slot in the tree to see if it is a valid item */
1415 nritems = btrfs_header_nritems(path->nodes[0]);
1416 if (path->slots[0] >= nritems) {
1417 ret = btrfs_next_leaf(root, path);
1418 if (ret)
1419 goto out;
1420 } else {
1421 path->slots[0]++;
1422 }
1423
1424 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1425
1426 if (key.type != key_type || key.objectid != dirid) {
1427 ret = 1;
1428 goto out;
1429 }
1430 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1431 struct btrfs_dir_log_item);
1432 found_end = btrfs_dir_log_end(path->nodes[0], item);
1433 *start_ret = key.offset;
1434 *end_ret = found_end;
1435 ret = 0;
1436out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001437 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001438 return ret;
1439}
1440
1441/*
1442 * this looks for a given directory item in the log. If the directory
1443 * item is not in the log, the item is removed and the inode it points
1444 * to is unlinked
1445 */
1446static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1447 struct btrfs_root *root,
1448 struct btrfs_root *log,
1449 struct btrfs_path *path,
1450 struct btrfs_path *log_path,
1451 struct inode *dir,
1452 struct btrfs_key *dir_key)
1453{
1454 int ret;
1455 struct extent_buffer *eb;
1456 int slot;
1457 u32 item_size;
1458 struct btrfs_dir_item *di;
1459 struct btrfs_dir_item *log_di;
1460 int name_len;
1461 unsigned long ptr;
1462 unsigned long ptr_end;
1463 char *name;
1464 struct inode *inode;
1465 struct btrfs_key location;
1466
1467again:
1468 eb = path->nodes[0];
1469 slot = path->slots[0];
1470 item_size = btrfs_item_size_nr(eb, slot);
1471 ptr = btrfs_item_ptr_offset(eb, slot);
1472 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001473 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001474 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001475 if (verify_dir_item(root, eb, di)) {
1476 ret = -EIO;
1477 goto out;
1478 }
1479
Chris Masone02119d2008-09-05 16:13:11 -04001480 name_len = btrfs_dir_name_len(eb, di);
1481 name = kmalloc(name_len, GFP_NOFS);
1482 if (!name) {
1483 ret = -ENOMEM;
1484 goto out;
1485 }
1486 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1487 name_len);
1488 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001489 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001490 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1491 dir_key->objectid,
1492 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001493 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001494 log_di = btrfs_lookup_dir_index_item(trans, log,
1495 log_path,
1496 dir_key->objectid,
1497 dir_key->offset,
1498 name, name_len, 0);
1499 }
David Sterbac7040052011-04-19 18:00:01 +02001500 if (IS_ERR_OR_NULL(log_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001501 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001502 btrfs_release_path(path);
1503 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001504 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001505 if (!inode) {
1506 kfree(name);
1507 return -EIO;
1508 }
Chris Masone02119d2008-09-05 16:13:11 -04001509
1510 ret = link_to_fixup_dir(trans, root,
1511 path, location.objectid);
1512 BUG_ON(ret);
1513 btrfs_inc_nlink(inode);
1514 ret = btrfs_unlink_inode(trans, root, dir, inode,
1515 name, name_len);
1516 BUG_ON(ret);
Chris Masondbc90432012-07-02 15:29:53 -04001517
1518 btrfs_run_delayed_items(trans, root);
1519
Chris Masone02119d2008-09-05 16:13:11 -04001520 kfree(name);
1521 iput(inode);
1522
1523 /* there might still be more names under this key
1524 * check and repeat if required
1525 */
1526 ret = btrfs_search_slot(NULL, root, dir_key, path,
1527 0, 0);
1528 if (ret == 0)
1529 goto again;
1530 ret = 0;
1531 goto out;
1532 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001533 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001534 kfree(name);
1535
1536 ptr = (unsigned long)(di + 1);
1537 ptr += name_len;
1538 }
1539 ret = 0;
1540out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001541 btrfs_release_path(path);
1542 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001543 return ret;
1544}
1545
1546/*
1547 * deletion replay happens before we copy any new directory items
1548 * out of the log or out of backreferences from inodes. It
1549 * scans the log to find ranges of keys that log is authoritative for,
1550 * and then scans the directory to find items in those ranges that are
1551 * not present in the log.
1552 *
1553 * Anything we don't find in the log is unlinked and removed from the
1554 * directory.
1555 */
1556static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1557 struct btrfs_root *root,
1558 struct btrfs_root *log,
1559 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001560 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001561{
1562 u64 range_start;
1563 u64 range_end;
1564 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1565 int ret = 0;
1566 struct btrfs_key dir_key;
1567 struct btrfs_key found_key;
1568 struct btrfs_path *log_path;
1569 struct inode *dir;
1570
1571 dir_key.objectid = dirid;
1572 dir_key.type = BTRFS_DIR_ITEM_KEY;
1573 log_path = btrfs_alloc_path();
1574 if (!log_path)
1575 return -ENOMEM;
1576
1577 dir = read_one_inode(root, dirid);
1578 /* it isn't an error if the inode isn't there, that can happen
1579 * because we replay the deletes before we copy in the inode item
1580 * from the log
1581 */
1582 if (!dir) {
1583 btrfs_free_path(log_path);
1584 return 0;
1585 }
1586again:
1587 range_start = 0;
1588 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001589 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001590 if (del_all)
1591 range_end = (u64)-1;
1592 else {
1593 ret = find_dir_range(log, path, dirid, key_type,
1594 &range_start, &range_end);
1595 if (ret != 0)
1596 break;
1597 }
Chris Masone02119d2008-09-05 16:13:11 -04001598
1599 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001600 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001601 int nritems;
1602 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1603 0, 0);
1604 if (ret < 0)
1605 goto out;
1606
1607 nritems = btrfs_header_nritems(path->nodes[0]);
1608 if (path->slots[0] >= nritems) {
1609 ret = btrfs_next_leaf(root, path);
1610 if (ret)
1611 break;
1612 }
1613 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1614 path->slots[0]);
1615 if (found_key.objectid != dirid ||
1616 found_key.type != dir_key.type)
1617 goto next_type;
1618
1619 if (found_key.offset > range_end)
1620 break;
1621
1622 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001623 log_path, dir,
1624 &found_key);
Chris Masone02119d2008-09-05 16:13:11 -04001625 BUG_ON(ret);
1626 if (found_key.offset == (u64)-1)
1627 break;
1628 dir_key.offset = found_key.offset + 1;
1629 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001630 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001631 if (range_end == (u64)-1)
1632 break;
1633 range_start = range_end + 1;
1634 }
1635
1636next_type:
1637 ret = 0;
1638 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1639 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1640 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001641 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001642 goto again;
1643 }
1644out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001645 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001646 btrfs_free_path(log_path);
1647 iput(dir);
1648 return ret;
1649}
1650
1651/*
1652 * the process_func used to replay items from the log tree. This
1653 * gets called in two different stages. The first stage just looks
1654 * for inodes and makes sure they are all copied into the subvolume.
1655 *
1656 * The second stage copies all the other item types from the log into
1657 * the subvolume. The two stage approach is slower, but gets rid of
1658 * lots of complexity around inodes referencing other inodes that exist
1659 * only in the log (references come from either directory items or inode
1660 * back refs).
1661 */
1662static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1663 struct walk_control *wc, u64 gen)
1664{
1665 int nritems;
1666 struct btrfs_path *path;
1667 struct btrfs_root *root = wc->replay_dest;
1668 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04001669 int level;
1670 int i;
1671 int ret;
1672
1673 btrfs_read_buffer(eb, gen);
1674
1675 level = btrfs_header_level(eb);
1676
1677 if (level != 0)
1678 return 0;
1679
1680 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001681 if (!path)
1682 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001683
1684 nritems = btrfs_header_nritems(eb);
1685 for (i = 0; i < nritems; i++) {
1686 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04001687
1688 /* inode keys are done during the first stage */
1689 if (key.type == BTRFS_INODE_ITEM_KEY &&
1690 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04001691 struct btrfs_inode_item *inode_item;
1692 u32 mode;
1693
1694 inode_item = btrfs_item_ptr(eb, i,
1695 struct btrfs_inode_item);
1696 mode = btrfs_inode_mode(eb, inode_item);
1697 if (S_ISDIR(mode)) {
1698 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04001699 root, log, path, key.objectid, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001700 BUG_ON(ret);
1701 }
1702 ret = overwrite_item(wc->trans, root, path,
1703 eb, i, &key);
1704 BUG_ON(ret);
1705
Yan, Zhengc71bf092009-11-12 09:34:40 +00001706 /* for regular files, make sure corresponding
1707 * orhpan item exist. extents past the new EOF
1708 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04001709 */
1710 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00001711 ret = insert_orphan_item(wc->trans, root,
1712 key.objectid);
Chris Masone02119d2008-09-05 16:13:11 -04001713 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001714 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00001715
Chris Masone02119d2008-09-05 16:13:11 -04001716 ret = link_to_fixup_dir(wc->trans, root,
1717 path, key.objectid);
1718 BUG_ON(ret);
1719 }
1720 if (wc->stage < LOG_WALK_REPLAY_ALL)
1721 continue;
1722
1723 /* these keys are simply copied */
1724 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1725 ret = overwrite_item(wc->trans, root, path,
1726 eb, i, &key);
1727 BUG_ON(ret);
1728 } else if (key.type == BTRFS_INODE_REF_KEY) {
1729 ret = add_inode_ref(wc->trans, root, log, path,
1730 eb, i, &key);
1731 BUG_ON(ret && ret != -ENOENT);
1732 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1733 ret = replay_one_extent(wc->trans, root, path,
1734 eb, i, &key);
1735 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001736 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1737 key.type == BTRFS_DIR_INDEX_KEY) {
1738 ret = replay_one_dir_item(wc->trans, root, path,
1739 eb, i, &key);
1740 BUG_ON(ret);
1741 }
1742 }
1743 btrfs_free_path(path);
1744 return 0;
1745}
1746
Chris Masond3977122009-01-05 21:25:51 -05001747static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001748 struct btrfs_root *root,
1749 struct btrfs_path *path, int *level,
1750 struct walk_control *wc)
1751{
1752 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001753 u64 bytenr;
1754 u64 ptr_gen;
1755 struct extent_buffer *next;
1756 struct extent_buffer *cur;
1757 struct extent_buffer *parent;
1758 u32 blocksize;
1759 int ret = 0;
1760
1761 WARN_ON(*level < 0);
1762 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1763
Chris Masond3977122009-01-05 21:25:51 -05001764 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001765 WARN_ON(*level < 0);
1766 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1767 cur = path->nodes[*level];
1768
1769 if (btrfs_header_level(cur) != *level)
1770 WARN_ON(1);
1771
1772 if (path->slots[*level] >=
1773 btrfs_header_nritems(cur))
1774 break;
1775
1776 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1777 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1778 blocksize = btrfs_level_size(root, *level - 1);
1779
1780 parent = path->nodes[*level];
1781 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04001782
1783 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00001784 if (!next)
1785 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001786
Chris Masone02119d2008-09-05 16:13:11 -04001787 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001788 ret = wc->process_func(root, next, wc, ptr_gen);
1789 if (ret)
1790 return ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001791
Chris Masone02119d2008-09-05 16:13:11 -04001792 path->slots[*level]++;
1793 if (wc->free) {
1794 btrfs_read_buffer(next, ptr_gen);
1795
1796 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001797 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001798 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001799 btrfs_wait_tree_block_writeback(next);
1800 btrfs_tree_unlock(next);
1801
Chris Masone02119d2008-09-05 16:13:11 -04001802 WARN_ON(root_owner !=
1803 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b722011-10-31 20:52:39 -04001804 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04001805 bytenr, blocksize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001806 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001807 }
1808 free_extent_buffer(next);
1809 continue;
1810 }
1811 btrfs_read_buffer(next, ptr_gen);
1812
1813 WARN_ON(*level <= 0);
1814 if (path->nodes[*level-1])
1815 free_extent_buffer(path->nodes[*level-1]);
1816 path->nodes[*level-1] = next;
1817 *level = btrfs_header_level(next);
1818 path->slots[*level] = 0;
1819 cond_resched();
1820 }
1821 WARN_ON(*level < 0);
1822 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1823
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001824 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04001825
1826 cond_resched();
1827 return 0;
1828}
1829
Chris Masond3977122009-01-05 21:25:51 -05001830static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04001831 struct btrfs_root *root,
1832 struct btrfs_path *path, int *level,
1833 struct walk_control *wc)
1834{
1835 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04001836 int i;
1837 int slot;
1838 int ret;
1839
Chris Masond3977122009-01-05 21:25:51 -05001840 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04001841 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04001842 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04001843 path->slots[i]++;
1844 *level = i;
1845 WARN_ON(*level == 0);
1846 return 0;
1847 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001848 struct extent_buffer *parent;
1849 if (path->nodes[*level] == root->node)
1850 parent = path->nodes[*level];
1851 else
1852 parent = path->nodes[*level + 1];
1853
1854 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001855 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001856 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07001857 if (ret)
1858 return ret;
1859
Chris Masone02119d2008-09-05 16:13:11 -04001860 if (wc->free) {
1861 struct extent_buffer *next;
1862
1863 next = path->nodes[*level];
1864
1865 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001866 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001867 clean_tree_block(trans, root, next);
Chris Masone02119d2008-09-05 16:13:11 -04001868 btrfs_wait_tree_block_writeback(next);
1869 btrfs_tree_unlock(next);
1870
Chris Masone02119d2008-09-05 16:13:11 -04001871 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b722011-10-31 20:52:39 -04001872 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04001873 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04001874 path->nodes[*level]->len);
Chris Masone02119d2008-09-05 16:13:11 -04001875 BUG_ON(ret);
1876 }
1877 free_extent_buffer(path->nodes[*level]);
1878 path->nodes[*level] = NULL;
1879 *level = i + 1;
1880 }
1881 }
1882 return 1;
1883}
1884
1885/*
1886 * drop the reference count on the tree rooted at 'snap'. This traverses
1887 * the tree freeing any blocks that have a ref count of zero after being
1888 * decremented.
1889 */
1890static int walk_log_tree(struct btrfs_trans_handle *trans,
1891 struct btrfs_root *log, struct walk_control *wc)
1892{
1893 int ret = 0;
1894 int wret;
1895 int level;
1896 struct btrfs_path *path;
1897 int i;
1898 int orig_level;
1899
1900 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001901 if (!path)
1902 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04001903
1904 level = btrfs_header_level(log->node);
1905 orig_level = level;
1906 path->nodes[level] = log->node;
1907 extent_buffer_get(log->node);
1908 path->slots[level] = 0;
1909
Chris Masond3977122009-01-05 21:25:51 -05001910 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001911 wret = walk_down_log_tree(trans, log, path, &level, wc);
1912 if (wret > 0)
1913 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001914 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001915 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 goto out;
1917 }
Chris Masone02119d2008-09-05 16:13:11 -04001918
1919 wret = walk_up_log_tree(trans, log, path, &level, wc);
1920 if (wret > 0)
1921 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001923 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 goto out;
1925 }
Chris Masone02119d2008-09-05 16:13:11 -04001926 }
1927
1928 /* was the root node processed? if not, catch it here */
1929 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04001931 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 if (ret)
1933 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001934 if (wc->free) {
1935 struct extent_buffer *next;
1936
1937 next = path->nodes[orig_level];
1938
1939 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001940 btrfs_set_lock_blocking(next);
Chris Masonbd681512011-07-16 15:23:14 -04001941 clean_tree_block(trans, log, next);
Chris Masone02119d2008-09-05 16:13:11 -04001942 btrfs_wait_tree_block_writeback(next);
1943 btrfs_tree_unlock(next);
1944
Chris Masone02119d2008-09-05 16:13:11 -04001945 WARN_ON(log->root_key.objectid !=
1946 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b722011-10-31 20:52:39 -04001947 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04001948 next->len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001949 BUG_ON(ret); /* -ENOMEM or logic errors */
Chris Masone02119d2008-09-05 16:13:11 -04001950 }
1951 }
1952
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953out:
Chris Masone02119d2008-09-05 16:13:11 -04001954 for (i = 0; i <= orig_level; i++) {
1955 if (path->nodes[i]) {
1956 free_extent_buffer(path->nodes[i]);
1957 path->nodes[i] = NULL;
1958 }
1959 }
1960 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001961 return ret;
1962}
1963
Yan Zheng7237f182009-01-21 12:54:03 -05001964/*
1965 * helper function to update the item for a given subvolumes log root
1966 * in the tree of log roots
1967 */
1968static int update_log_root(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *log)
1970{
1971 int ret;
1972
1973 if (log->log_transid == 1) {
1974 /* insert root item on the first sync */
1975 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1976 &log->root_key, &log->root_item);
1977 } else {
1978 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1979 &log->root_key, &log->root_item);
1980 }
1981 return ret;
1982}
1983
Chris Mason12fcfd22009-03-24 10:24:20 -04001984static int wait_log_commit(struct btrfs_trans_handle *trans,
1985 struct btrfs_root *root, unsigned long transid)
Chris Masone02119d2008-09-05 16:13:11 -04001986{
1987 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05001988 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04001989
Yan Zheng7237f182009-01-21 12:54:03 -05001990 /*
1991 * we only allow two pending log transactions at a time,
1992 * so we know that if ours is more than 2 older than the
1993 * current transaction, we're done
1994 */
Chris Masone02119d2008-09-05 16:13:11 -04001995 do {
Yan Zheng7237f182009-01-21 12:54:03 -05001996 prepare_to_wait(&root->log_commit_wait[index],
1997 &wait, TASK_UNINTERRUPTIBLE);
1998 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04001999
2000 if (root->fs_info->last_trans_log_full_commit !=
2001 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002002 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002003 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002004
Yan Zheng7237f182009-01-21 12:54:03 -05002005 finish_wait(&root->log_commit_wait[index], &wait);
2006 mutex_lock(&root->log_mutex);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002007 } while (root->fs_info->last_trans_log_full_commit !=
2008 trans->transid && root->log_transid < transid + 2 &&
Yan Zheng7237f182009-01-21 12:54:03 -05002009 atomic_read(&root->log_commit[index]));
2010 return 0;
2011}
2012
Jeff Mahoney143bede2012-03-01 14:56:26 +01002013static void wait_for_writer(struct btrfs_trans_handle *trans,
2014 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002015{
2016 DEFINE_WAIT(wait);
Jan Kara6dd70ce2012-01-26 15:01:11 -05002017 while (root->fs_info->last_trans_log_full_commit !=
2018 trans->transid && atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002019 prepare_to_wait(&root->log_writer_wait,
2020 &wait, TASK_UNINTERRUPTIBLE);
2021 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002022 if (root->fs_info->last_trans_log_full_commit !=
2023 trans->transid && atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002024 schedule();
2025 mutex_lock(&root->log_mutex);
2026 finish_wait(&root->log_writer_wait, &wait);
2027 }
Chris Masone02119d2008-09-05 16:13:11 -04002028}
2029
2030/*
2031 * btrfs_sync_log does sends a given tree log down to the disk and
2032 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002033 * you know that any inodes previously logged are safely on disk only
2034 * if it returns 0.
2035 *
2036 * Any other return value means you need to call btrfs_commit_transaction.
2037 * Some of the edge cases for fsyncing directories that have had unlinks
2038 * or renames done in the past mean that sometimes the only safe
2039 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2040 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002041 */
2042int btrfs_sync_log(struct btrfs_trans_handle *trans,
2043 struct btrfs_root *root)
2044{
Yan Zheng7237f182009-01-21 12:54:03 -05002045 int index1;
2046 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002047 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002048 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002049 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002050 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002051 unsigned long log_transid = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002052
Yan Zheng7237f182009-01-21 12:54:03 -05002053 mutex_lock(&root->log_mutex);
2054 index1 = root->log_transid % 2;
2055 if (atomic_read(&root->log_commit[index1])) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002056 wait_log_commit(trans, root, root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002057 mutex_unlock(&root->log_mutex);
2058 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04002059 }
Yan Zheng7237f182009-01-21 12:54:03 -05002060 atomic_set(&root->log_commit[index1], 1);
2061
2062 /* wait for previous tree log sync to complete */
2063 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Chris Mason12fcfd22009-03-24 10:24:20 -04002064 wait_log_commit(trans, root, root->log_transid - 1);
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002065 while (1) {
Yan Zheng7237f182009-01-21 12:54:03 -05002066 unsigned long batch = root->log_batch;
Chris Masoncd354ad2011-10-20 15:45:37 -04002067 /* when we're on an ssd, just kick the log commit out */
2068 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002069 mutex_unlock(&root->log_mutex);
2070 schedule_timeout_uninterruptible(1);
2071 mutex_lock(&root->log_mutex);
2072 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002073 wait_for_writer(trans, root);
Yan Zheng7237f182009-01-21 12:54:03 -05002074 if (batch == root->log_batch)
Chris Masone02119d2008-09-05 16:13:11 -04002075 break;
2076 }
Chris Masond0c803c2008-09-11 16:17:57 -04002077
Chris Mason12fcfd22009-03-24 10:24:20 -04002078 /* bail out if we need to do a full commit */
2079 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2080 ret = -EAGAIN;
2081 mutex_unlock(&root->log_mutex);
2082 goto out;
2083 }
2084
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002085 log_transid = root->log_transid;
2086 if (log_transid % 2 == 0)
2087 mark = EXTENT_DIRTY;
2088 else
2089 mark = EXTENT_NEW;
2090
Chris Mason690587d2009-10-13 13:29:19 -04002091 /* we start IO on all the marked extents here, but we don't actually
2092 * wait for them until later.
2093 */
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002094 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002095 if (ret) {
2096 btrfs_abort_transaction(trans, root, ret);
2097 mutex_unlock(&root->log_mutex);
2098 goto out;
2099 }
Yan Zheng7237f182009-01-21 12:54:03 -05002100
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002102
2103 root->log_batch = 0;
2104 root->log_transid++;
2105 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002106 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002107 smp_mb();
2108 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002109 * IO has been started, blocks of the log tree have WRITTEN flag set
2110 * in their headers. new modifications of the log will be written to
2111 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002112 */
2113 mutex_unlock(&root->log_mutex);
2114
2115 mutex_lock(&log_root_tree->log_mutex);
2116 log_root_tree->log_batch++;
2117 atomic_inc(&log_root_tree->log_writers);
2118 mutex_unlock(&log_root_tree->log_mutex);
2119
2120 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002121
2122 mutex_lock(&log_root_tree->log_mutex);
2123 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2124 smp_mb();
2125 if (waitqueue_active(&log_root_tree->log_writer_wait))
2126 wake_up(&log_root_tree->log_writer_wait);
2127 }
2128
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002129 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002130 if (ret != -ENOSPC) {
2131 btrfs_abort_transaction(trans, root, ret);
2132 mutex_unlock(&log_root_tree->log_mutex);
2133 goto out;
2134 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002135 root->fs_info->last_trans_log_full_commit = trans->transid;
2136 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2137 mutex_unlock(&log_root_tree->log_mutex);
2138 ret = -EAGAIN;
2139 goto out;
2140 }
2141
Yan Zheng7237f182009-01-21 12:54:03 -05002142 index2 = log_root_tree->log_transid % 2;
2143 if (atomic_read(&log_root_tree->log_commit[index2])) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002144 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002145 wait_log_commit(trans, log_root_tree,
2146 log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002147 mutex_unlock(&log_root_tree->log_mutex);
Chris Masonb31eabd2011-01-31 16:48:24 -05002148 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002149 goto out;
2150 }
2151 atomic_set(&log_root_tree->log_commit[index2], 1);
2152
Chris Mason12fcfd22009-03-24 10:24:20 -04002153 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2154 wait_log_commit(trans, log_root_tree,
2155 log_root_tree->log_transid - 1);
2156 }
Yan Zheng7237f182009-01-21 12:54:03 -05002157
Chris Mason12fcfd22009-03-24 10:24:20 -04002158 wait_for_writer(trans, log_root_tree);
2159
2160 /*
2161 * now that we've moved on to the tree of log tree roots,
2162 * check the full commit flag again
2163 */
2164 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002165 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04002166 mutex_unlock(&log_root_tree->log_mutex);
2167 ret = -EAGAIN;
2168 goto out_wake_log_root;
2169 }
Yan Zheng7237f182009-01-21 12:54:03 -05002170
2171 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002172 &log_root_tree->dirty_log_pages,
2173 EXTENT_DIRTY | EXTENT_NEW);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002174 if (ret) {
2175 btrfs_abort_transaction(trans, root, ret);
2176 mutex_unlock(&log_root_tree->log_mutex);
2177 goto out_wake_log_root;
2178 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002179 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Chris Masone02119d2008-09-05 16:13:11 -04002180
David Sterba6c417612011-04-13 15:41:04 +02002181 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002182 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002183 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002184 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002185
Yan Zheng7237f182009-01-21 12:54:03 -05002186 log_root_tree->log_batch = 0;
2187 log_root_tree->log_transid++;
Chris Masone02119d2008-09-05 16:13:11 -04002188 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002189
2190 mutex_unlock(&log_root_tree->log_mutex);
2191
2192 /*
2193 * nobody else is going to jump in and write the the ctree
2194 * super here because the log_commit atomic below is protecting
2195 * us. We must be called with a transaction handle pinning
2196 * the running transaction open, so a full commit can't hop
2197 * in and cause problems either.
2198 */
Arne Jansena2de7332011-03-08 14:14:00 +01002199 btrfs_scrub_pause_super(root);
Chris Mason47226072009-10-13 12:55:09 -04002200 write_ctree_super(trans, root->fs_info->tree_root, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002201 btrfs_scrub_continue_super(root);
Chris Mason12fcfd22009-03-24 10:24:20 -04002202 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002203
Chris Mason257c62e2009-10-13 13:21:08 -04002204 mutex_lock(&root->log_mutex);
2205 if (root->last_log_commit < log_transid)
2206 root->last_log_commit = log_transid;
2207 mutex_unlock(&root->log_mutex);
2208
Chris Mason12fcfd22009-03-24 10:24:20 -04002209out_wake_log_root:
Yan Zheng7237f182009-01-21 12:54:03 -05002210 atomic_set(&log_root_tree->log_commit[index2], 0);
2211 smp_mb();
2212 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2213 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002214out:
Yan Zheng7237f182009-01-21 12:54:03 -05002215 atomic_set(&root->log_commit[index1], 0);
2216 smp_mb();
2217 if (waitqueue_active(&root->log_commit_wait[index1]))
2218 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002219 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002220}
2221
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002222static void free_log_tree(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002224{
2225 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002226 u64 start;
2227 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002228 struct walk_control wc = {
2229 .free = 1,
2230 .process_func = process_one_buffer
2231 };
2232
Chris Masone02119d2008-09-05 16:13:11 -04002233 ret = walk_log_tree(trans, log, &wc);
2234 BUG_ON(ret);
2235
Chris Masond3977122009-01-05 21:25:51 -05002236 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002237 ret = find_first_extent_bit(&log->dirty_log_pages,
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002238 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04002239 if (ret)
2240 break;
2241
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002242 clear_extent_bits(&log->dirty_log_pages, start, end,
2243 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002244 }
2245
Yan Zheng7237f182009-01-21 12:54:03 -05002246 free_extent_buffer(log->node);
2247 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002248}
2249
2250/*
2251 * free all the extents used by the tree log. This should be called
2252 * at commit time of the full transaction
2253 */
2254int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2255{
2256 if (root->log_root) {
2257 free_log_tree(trans, root->log_root);
2258 root->log_root = NULL;
2259 }
2260 return 0;
2261}
2262
2263int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2264 struct btrfs_fs_info *fs_info)
2265{
2266 if (fs_info->log_root_tree) {
2267 free_log_tree(trans, fs_info->log_root_tree);
2268 fs_info->log_root_tree = NULL;
2269 }
Chris Masone02119d2008-09-05 16:13:11 -04002270 return 0;
2271}
2272
2273/*
Chris Masone02119d2008-09-05 16:13:11 -04002274 * If both a file and directory are logged, and unlinks or renames are
2275 * mixed in, we have a few interesting corners:
2276 *
2277 * create file X in dir Y
2278 * link file X to X.link in dir Y
2279 * fsync file X
2280 * unlink file X but leave X.link
2281 * fsync dir Y
2282 *
2283 * After a crash we would expect only X.link to exist. But file X
2284 * didn't get fsync'd again so the log has back refs for X and X.link.
2285 *
2286 * We solve this by removing directory entries and inode backrefs from the
2287 * log when a file that was logged in the current transaction is
2288 * unlinked. Any later fsync will include the updated log entries, and
2289 * we'll be able to reconstruct the proper directory items from backrefs.
2290 *
2291 * This optimizations allows us to avoid relogging the entire inode
2292 * or the entire directory.
2293 */
2294int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2295 struct btrfs_root *root,
2296 const char *name, int name_len,
2297 struct inode *dir, u64 index)
2298{
2299 struct btrfs_root *log;
2300 struct btrfs_dir_item *di;
2301 struct btrfs_path *path;
2302 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002303 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002304 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002305 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002306
Chris Mason3a5f1d42008-09-11 15:53:37 -04002307 if (BTRFS_I(dir)->logged_trans < trans->transid)
2308 return 0;
2309
Chris Masone02119d2008-09-05 16:13:11 -04002310 ret = join_running_log_trans(root);
2311 if (ret)
2312 return 0;
2313
2314 mutex_lock(&BTRFS_I(dir)->log_mutex);
2315
2316 log = root->log_root;
2317 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002318 if (!path) {
2319 err = -ENOMEM;
2320 goto out_unlock;
2321 }
liubo2a29edc2011-01-26 06:22:08 +00002322
Li Zefan33345d012011-04-20 10:31:50 +08002323 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002324 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002325 if (IS_ERR(di)) {
2326 err = PTR_ERR(di);
2327 goto fail;
2328 }
2329 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002330 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2331 bytes_del += name_len;
2332 BUG_ON(ret);
2333 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002334 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002335 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002336 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002337 if (IS_ERR(di)) {
2338 err = PTR_ERR(di);
2339 goto fail;
2340 }
2341 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002342 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2343 bytes_del += name_len;
2344 BUG_ON(ret);
2345 }
2346
2347 /* update the directory size in the log to reflect the names
2348 * we have removed
2349 */
2350 if (bytes_del) {
2351 struct btrfs_key key;
2352
Li Zefan33345d012011-04-20 10:31:50 +08002353 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002354 key.offset = 0;
2355 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002356 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002357
2358 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002359 if (ret < 0) {
2360 err = ret;
2361 goto fail;
2362 }
Chris Masone02119d2008-09-05 16:13:11 -04002363 if (ret == 0) {
2364 struct btrfs_inode_item *item;
2365 u64 i_size;
2366
2367 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2368 struct btrfs_inode_item);
2369 i_size = btrfs_inode_size(path->nodes[0], item);
2370 if (i_size > bytes_del)
2371 i_size -= bytes_del;
2372 else
2373 i_size = 0;
2374 btrfs_set_inode_size(path->nodes[0], item, i_size);
2375 btrfs_mark_buffer_dirty(path->nodes[0]);
2376 } else
2377 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002378 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002379 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002380fail:
Chris Masone02119d2008-09-05 16:13:11 -04002381 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002382out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002383 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002384 if (ret == -ENOSPC) {
2385 root->fs_info->last_trans_log_full_commit = trans->transid;
2386 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002387 } else if (ret < 0)
2388 btrfs_abort_transaction(trans, root, ret);
2389
Chris Mason12fcfd22009-03-24 10:24:20 -04002390 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002391
Andi Kleen411fc6b2010-10-29 15:14:31 -04002392 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002393}
2394
2395/* see comments for btrfs_del_dir_entries_in_log */
2396int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2397 struct btrfs_root *root,
2398 const char *name, int name_len,
2399 struct inode *inode, u64 dirid)
2400{
2401 struct btrfs_root *log;
2402 u64 index;
2403 int ret;
2404
Chris Mason3a5f1d42008-09-11 15:53:37 -04002405 if (BTRFS_I(inode)->logged_trans < trans->transid)
2406 return 0;
2407
Chris Masone02119d2008-09-05 16:13:11 -04002408 ret = join_running_log_trans(root);
2409 if (ret)
2410 return 0;
2411 log = root->log_root;
2412 mutex_lock(&BTRFS_I(inode)->log_mutex);
2413
Li Zefan33345d012011-04-20 10:31:50 +08002414 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002415 dirid, &index);
2416 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002417 if (ret == -ENOSPC) {
2418 root->fs_info->last_trans_log_full_commit = trans->transid;
2419 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002420 } else if (ret < 0 && ret != -ENOENT)
2421 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002422 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002423
Chris Masone02119d2008-09-05 16:13:11 -04002424 return ret;
2425}
2426
2427/*
2428 * creates a range item in the log for 'dirid'. first_offset and
2429 * last_offset tell us which parts of the key space the log should
2430 * be considered authoritative for.
2431 */
2432static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2433 struct btrfs_root *log,
2434 struct btrfs_path *path,
2435 int key_type, u64 dirid,
2436 u64 first_offset, u64 last_offset)
2437{
2438 int ret;
2439 struct btrfs_key key;
2440 struct btrfs_dir_log_item *item;
2441
2442 key.objectid = dirid;
2443 key.offset = first_offset;
2444 if (key_type == BTRFS_DIR_ITEM_KEY)
2445 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2446 else
2447 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2448 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002449 if (ret)
2450 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002451
2452 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2453 struct btrfs_dir_log_item);
2454 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2455 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002456 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002457 return 0;
2458}
2459
2460/*
2461 * log all the items included in the current transaction for a given
2462 * directory. This also creates the range items in the log tree required
2463 * to replay anything deleted before the fsync
2464 */
2465static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root, struct inode *inode,
2467 struct btrfs_path *path,
2468 struct btrfs_path *dst_path, int key_type,
2469 u64 min_offset, u64 *last_offset_ret)
2470{
2471 struct btrfs_key min_key;
2472 struct btrfs_key max_key;
2473 struct btrfs_root *log = root->log_root;
2474 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002475 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002476 int ret;
2477 int i;
2478 int nritems;
2479 u64 first_offset = min_offset;
2480 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002481 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002482
2483 log = root->log_root;
Li Zefan33345d012011-04-20 10:31:50 +08002484 max_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002485 max_key.offset = (u64)-1;
2486 max_key.type = key_type;
2487
Li Zefan33345d012011-04-20 10:31:50 +08002488 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002489 min_key.type = key_type;
2490 min_key.offset = min_offset;
2491
2492 path->keep_locks = 1;
2493
2494 ret = btrfs_search_forward(root, &min_key, &max_key,
2495 path, 0, trans->transid);
2496
2497 /*
2498 * we didn't find anything from this transaction, see if there
2499 * is anything at all
2500 */
Li Zefan33345d012011-04-20 10:31:50 +08002501 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2502 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002503 min_key.type = key_type;
2504 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002505 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002506 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2507 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002508 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002509 return ret;
2510 }
Li Zefan33345d012011-04-20 10:31:50 +08002511 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002512
2513 /* if ret == 0 there are items for this type,
2514 * create a range to tell us the last key of this type.
2515 * otherwise, there are no items in this directory after
2516 * *min_offset, and we create a range to indicate that.
2517 */
2518 if (ret == 0) {
2519 struct btrfs_key tmp;
2520 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2521 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05002522 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04002523 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04002524 }
2525 goto done;
2526 }
2527
2528 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08002529 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002530 if (ret == 0) {
2531 struct btrfs_key tmp;
2532 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2533 if (key_type == tmp.type) {
2534 first_offset = tmp.offset;
2535 ret = overwrite_item(trans, log, dst_path,
2536 path->nodes[0], path->slots[0],
2537 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002538 if (ret) {
2539 err = ret;
2540 goto done;
2541 }
Chris Masone02119d2008-09-05 16:13:11 -04002542 }
2543 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002544 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002545
2546 /* find the first key from this transaction again */
2547 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2548 if (ret != 0) {
2549 WARN_ON(1);
2550 goto done;
2551 }
2552
2553 /*
2554 * we have a block from this transaction, log every item in it
2555 * from our directory
2556 */
Chris Masond3977122009-01-05 21:25:51 -05002557 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002558 struct btrfs_key tmp;
2559 src = path->nodes[0];
2560 nritems = btrfs_header_nritems(src);
2561 for (i = path->slots[0]; i < nritems; i++) {
2562 btrfs_item_key_to_cpu(src, &min_key, i);
2563
Li Zefan33345d012011-04-20 10:31:50 +08002564 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04002565 goto done;
2566 ret = overwrite_item(trans, log, dst_path, src, i,
2567 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002568 if (ret) {
2569 err = ret;
2570 goto done;
2571 }
Chris Masone02119d2008-09-05 16:13:11 -04002572 }
2573 path->slots[0] = nritems;
2574
2575 /*
2576 * look ahead to the next item and see if it is also
2577 * from this directory and from this transaction
2578 */
2579 ret = btrfs_next_leaf(root, path);
2580 if (ret == 1) {
2581 last_offset = (u64)-1;
2582 goto done;
2583 }
2584 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08002585 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04002586 last_offset = (u64)-1;
2587 goto done;
2588 }
2589 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2590 ret = overwrite_item(trans, log, dst_path,
2591 path->nodes[0], path->slots[0],
2592 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002593 if (ret)
2594 err = ret;
2595 else
2596 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04002597 goto done;
2598 }
2599 }
2600done:
David Sterbab3b4aa72011-04-21 01:20:15 +02002601 btrfs_release_path(path);
2602 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002603
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002604 if (err == 0) {
2605 *last_offset_ret = last_offset;
2606 /*
2607 * insert the log range keys to indicate where the log
2608 * is valid
2609 */
2610 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08002611 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002612 if (ret)
2613 err = ret;
2614 }
2615 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002616}
2617
2618/*
2619 * logging directories is very similar to logging inodes, We find all the items
2620 * from the current transaction and write them to the log.
2621 *
2622 * The recovery code scans the directory in the subvolume, and if it finds a
2623 * key in the range logged that is not present in the log tree, then it means
2624 * that dir entry was unlinked during the transaction.
2625 *
2626 * In order for that scan to work, we must include one key smaller than
2627 * the smallest logged by this transaction and one key larger than the largest
2628 * key logged by this transaction.
2629 */
2630static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2631 struct btrfs_root *root, struct inode *inode,
2632 struct btrfs_path *path,
2633 struct btrfs_path *dst_path)
2634{
2635 u64 min_key;
2636 u64 max_key;
2637 int ret;
2638 int key_type = BTRFS_DIR_ITEM_KEY;
2639
2640again:
2641 min_key = 0;
2642 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05002643 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002644 ret = log_dir_items(trans, root, inode, path,
2645 dst_path, key_type, min_key,
2646 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002647 if (ret)
2648 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002649 if (max_key == (u64)-1)
2650 break;
2651 min_key = max_key + 1;
2652 }
2653
2654 if (key_type == BTRFS_DIR_ITEM_KEY) {
2655 key_type = BTRFS_DIR_INDEX_KEY;
2656 goto again;
2657 }
2658 return 0;
2659}
2660
2661/*
2662 * a helper function to drop items from the log before we relog an
2663 * inode. max_key_type indicates the highest item type to remove.
2664 * This cannot be run for file data extents because it does not
2665 * free the extents they point to.
2666 */
2667static int drop_objectid_items(struct btrfs_trans_handle *trans,
2668 struct btrfs_root *log,
2669 struct btrfs_path *path,
2670 u64 objectid, int max_key_type)
2671{
2672 int ret;
2673 struct btrfs_key key;
2674 struct btrfs_key found_key;
2675
2676 key.objectid = objectid;
2677 key.type = max_key_type;
2678 key.offset = (u64)-1;
2679
Chris Masond3977122009-01-05 21:25:51 -05002680 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002681 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002682 BUG_ON(ret == 0);
2683 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04002684 break;
2685
2686 if (path->slots[0] == 0)
2687 break;
2688
2689 path->slots[0]--;
2690 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2691 path->slots[0]);
2692
2693 if (found_key.objectid != objectid)
2694 break;
2695
2696 ret = btrfs_del_item(trans, log, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002697 if (ret)
2698 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02002699 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002700 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002701 btrfs_release_path(path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002702 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002703}
2704
Chris Mason31ff1cd2008-09-11 16:17:57 -04002705static noinline int copy_items(struct btrfs_trans_handle *trans,
2706 struct btrfs_root *log,
2707 struct btrfs_path *dst_path,
2708 struct extent_buffer *src,
2709 int start_slot, int nr, int inode_only)
2710{
2711 unsigned long src_offset;
2712 unsigned long dst_offset;
2713 struct btrfs_file_extent_item *extent;
2714 struct btrfs_inode_item *inode_item;
2715 int ret;
2716 struct btrfs_key *ins_keys;
2717 u32 *ins_sizes;
2718 char *ins_data;
2719 int i;
Chris Masond20f7042008-12-08 16:58:54 -05002720 struct list_head ordered_sums;
2721
2722 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002723
2724 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2725 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00002726 if (!ins_data)
2727 return -ENOMEM;
2728
Chris Mason31ff1cd2008-09-11 16:17:57 -04002729 ins_sizes = (u32 *)ins_data;
2730 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2731
2732 for (i = 0; i < nr; i++) {
2733 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2734 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2735 }
2736 ret = btrfs_insert_empty_items(trans, log, dst_path,
2737 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002738 if (ret) {
2739 kfree(ins_data);
2740 return ret;
2741 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002742
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002743 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002744 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2745 dst_path->slots[0]);
2746
2747 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2748
2749 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2750 src_offset, ins_sizes[i]);
2751
2752 if (inode_only == LOG_INODE_EXISTS &&
2753 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2754 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2755 dst_path->slots[0],
2756 struct btrfs_inode_item);
2757 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2758
2759 /* set the generation to zero so the recover code
2760 * can tell the difference between an logging
2761 * just to say 'this inode exists' and a logging
2762 * to say 'update this inode with these values'
2763 */
2764 btrfs_set_inode_generation(dst_path->nodes[0],
2765 inode_item, 0);
2766 }
2767 /* take a reference on file data extents so that truncates
2768 * or deletes of this inode don't have to relog the inode
2769 * again
2770 */
2771 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2772 int found_type;
2773 extent = btrfs_item_ptr(src, start_slot + i,
2774 struct btrfs_file_extent_item);
2775
liubo8e531cd2011-05-06 10:36:09 +08002776 if (btrfs_file_extent_generation(src, extent) < trans->transid)
2777 continue;
2778
Chris Mason31ff1cd2008-09-11 16:17:57 -04002779 found_type = btrfs_file_extent_type(src, extent);
Yan Zhengd899e052008-10-30 14:25:28 -04002780 if (found_type == BTRFS_FILE_EXTENT_REG ||
2781 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782 u64 ds, dl, cs, cl;
2783 ds = btrfs_file_extent_disk_bytenr(src,
2784 extent);
2785 /* ds == 0 is a hole */
2786 if (ds == 0)
2787 continue;
2788
2789 dl = btrfs_file_extent_disk_num_bytes(src,
2790 extent);
2791 cs = btrfs_file_extent_offset(src, extent);
2792 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07002793 extent);
Chris Mason580afd72008-12-08 19:15:39 -05002794 if (btrfs_file_extent_compression(src,
2795 extent)) {
2796 cs = 0;
2797 cl = dl;
2798 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002799
2800 ret = btrfs_lookup_csums_range(
2801 log->fs_info->csum_root,
2802 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01002803 &ordered_sums, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002804 BUG_ON(ret);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002805 }
2806 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002807 }
2808
2809 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002810 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04002811 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05002812
2813 /*
2814 * we have to do this after the loop above to avoid changing the
2815 * log tree while trying to change the log tree.
2816 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002817 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002818 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05002819 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2820 struct btrfs_ordered_sum,
2821 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002822 if (!ret)
2823 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05002824 list_del(&sums->list);
2825 kfree(sums);
2826 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002827 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002828}
2829
Chris Masone02119d2008-09-05 16:13:11 -04002830/* log a single inode in the tree log.
2831 * At least one parent directory for this inode must exist in the tree
2832 * or be logged already.
2833 *
2834 * Any items from this inode changed by the current transaction are copied
2835 * to the log tree. An extra reference is taken on any extents in this
2836 * file, allowing us to avoid a whole pile of corner cases around logging
2837 * blocks that have been removed from the tree.
2838 *
2839 * See LOG_INODE_ALL and related defines for a description of what inode_only
2840 * does.
2841 *
2842 * This handles both files and directories.
2843 */
Chris Mason12fcfd22009-03-24 10:24:20 -04002844static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002845 struct btrfs_root *root, struct inode *inode,
2846 int inode_only)
2847{
2848 struct btrfs_path *path;
2849 struct btrfs_path *dst_path;
2850 struct btrfs_key min_key;
2851 struct btrfs_key max_key;
2852 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002853 struct extent_buffer *src = NULL;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002854 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002855 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002856 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002857 int ins_start_slot = 0;
2858 int ins_nr;
Li Zefan33345d012011-04-20 10:31:50 +08002859 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002860
2861 log = root->log_root;
2862
2863 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002864 if (!path)
2865 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002866 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002867 if (!dst_path) {
2868 btrfs_free_path(path);
2869 return -ENOMEM;
2870 }
Chris Masone02119d2008-09-05 16:13:11 -04002871
Li Zefan33345d012011-04-20 10:31:50 +08002872 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002873 min_key.type = BTRFS_INODE_ITEM_KEY;
2874 min_key.offset = 0;
2875
Li Zefan33345d012011-04-20 10:31:50 +08002876 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04002877
2878 /* today the code can only do partial logging of directories */
2879 if (!S_ISDIR(inode->i_mode))
2880 inode_only = LOG_INODE_ALL;
2881
Chris Masone02119d2008-09-05 16:13:11 -04002882 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2883 max_key.type = BTRFS_XATTR_ITEM_KEY;
2884 else
2885 max_key.type = (u8)-1;
2886 max_key.offset = (u64)-1;
2887
Miao Xie16cdcec2011-04-22 18:12:22 +08002888 ret = btrfs_commit_inode_delayed_items(trans, inode);
2889 if (ret) {
2890 btrfs_free_path(path);
2891 btrfs_free_path(dst_path);
2892 return ret;
2893 }
2894
Chris Masone02119d2008-09-05 16:13:11 -04002895 mutex_lock(&BTRFS_I(inode)->log_mutex);
2896
2897 /*
2898 * a brute force approach to making sure we get the most uptodate
2899 * copies of everything.
2900 */
2901 if (S_ISDIR(inode->i_mode)) {
2902 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2903
2904 if (inode_only == LOG_INODE_EXISTS)
2905 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08002906 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002907 } else {
2908 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2909 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002910 if (ret) {
2911 err = ret;
2912 goto out_unlock;
2913 }
Chris Masone02119d2008-09-05 16:13:11 -04002914 path->keep_locks = 1;
2915
Chris Masond3977122009-01-05 21:25:51 -05002916 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04002917 ins_nr = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002918 ret = btrfs_search_forward(root, &min_key, &max_key,
2919 path, 0, trans->transid);
2920 if (ret != 0)
2921 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04002922again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04002923 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08002924 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04002925 break;
2926 if (min_key.type > max_key.type)
2927 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04002928
Chris Masone02119d2008-09-05 16:13:11 -04002929 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04002930 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2931 ins_nr++;
2932 goto next_slot;
2933 } else if (!ins_nr) {
2934 ins_start_slot = path->slots[0];
2935 ins_nr = 1;
2936 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04002937 }
2938
Chris Mason31ff1cd2008-09-11 16:17:57 -04002939 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2940 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002941 if (ret) {
2942 err = ret;
2943 goto out_unlock;
2944 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002945 ins_nr = 1;
2946 ins_start_slot = path->slots[0];
2947next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04002948
Chris Mason3a5f1d42008-09-11 15:53:37 -04002949 nritems = btrfs_header_nritems(path->nodes[0]);
2950 path->slots[0]++;
2951 if (path->slots[0] < nritems) {
2952 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2953 path->slots[0]);
2954 goto again;
2955 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002956 if (ins_nr) {
2957 ret = copy_items(trans, log, dst_path, src,
2958 ins_start_slot,
2959 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002960 if (ret) {
2961 err = ret;
2962 goto out_unlock;
2963 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002964 ins_nr = 0;
2965 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002966 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04002967
Chris Masone02119d2008-09-05 16:13:11 -04002968 if (min_key.offset < (u64)-1)
2969 min_key.offset++;
2970 else if (min_key.type < (u8)-1)
2971 min_key.type++;
2972 else if (min_key.objectid < (u64)-1)
2973 min_key.objectid++;
2974 else
2975 break;
2976 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002977 if (ins_nr) {
2978 ret = copy_items(trans, log, dst_path, src,
2979 ins_start_slot,
2980 ins_nr, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002981 if (ret) {
2982 err = ret;
2983 goto out_unlock;
2984 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04002985 ins_nr = 0;
2986 }
2987 WARN_ON(ins_nr);
Chris Mason9623f9a2008-09-11 17:42:42 -04002988 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002989 btrfs_release_path(path);
2990 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04002991 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002992 if (ret) {
2993 err = ret;
2994 goto out_unlock;
2995 }
Chris Masone02119d2008-09-05 16:13:11 -04002996 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04002997 BTRFS_I(inode)->logged_trans = trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002998out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002999 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3000
3001 btrfs_free_path(path);
3002 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003003 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003004}
3005
Chris Mason12fcfd22009-03-24 10:24:20 -04003006/*
3007 * follow the dentry parent pointers up the chain and see if any
3008 * of the directories in it require a full commit before they can
3009 * be logged. Returns zero if nothing special needs to be done or 1 if
3010 * a full commit is required.
3011 */
3012static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3013 struct inode *inode,
3014 struct dentry *parent,
3015 struct super_block *sb,
3016 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04003017{
Chris Mason12fcfd22009-03-24 10:24:20 -04003018 int ret = 0;
3019 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00003020 struct dentry *old_parent = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003021
Chris Masonaf4176b2009-03-24 10:24:31 -04003022 /*
3023 * for regular files, if its inode is already on disk, we don't
3024 * have to worry about the parents at all. This is because
3025 * we can use the last_unlink_trans field to record renames
3026 * and other fun in this file.
3027 */
3028 if (S_ISREG(inode->i_mode) &&
3029 BTRFS_I(inode)->generation <= last_committed &&
3030 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3031 goto out;
3032
Chris Mason12fcfd22009-03-24 10:24:20 -04003033 if (!S_ISDIR(inode->i_mode)) {
3034 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3035 goto out;
3036 inode = parent->d_inode;
3037 }
3038
3039 while (1) {
3040 BTRFS_I(inode)->logged_trans = trans->transid;
3041 smp_mb();
3042
3043 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3044 root = BTRFS_I(inode)->root;
3045
3046 /*
3047 * make sure any commits to the log are forced
3048 * to be full commits
3049 */
3050 root->fs_info->last_trans_log_full_commit =
3051 trans->transid;
3052 ret = 1;
3053 break;
3054 }
3055
3056 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3057 break;
3058
Yan, Zheng76dda932009-09-21 16:00:26 -04003059 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04003060 break;
3061
Josef Bacik6a912212010-11-20 09:48:00 +00003062 parent = dget_parent(parent);
3063 dput(old_parent);
3064 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04003065 inode = parent->d_inode;
3066
3067 }
Josef Bacik6a912212010-11-20 09:48:00 +00003068 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04003069out:
Chris Masone02119d2008-09-05 16:13:11 -04003070 return ret;
3071}
3072
Chris Mason257c62e2009-10-13 13:21:08 -04003073static int inode_in_log(struct btrfs_trans_handle *trans,
3074 struct inode *inode)
3075{
3076 struct btrfs_root *root = BTRFS_I(inode)->root;
3077 int ret = 0;
3078
3079 mutex_lock(&root->log_mutex);
3080 if (BTRFS_I(inode)->logged_trans == trans->transid &&
3081 BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
3082 ret = 1;
3083 mutex_unlock(&root->log_mutex);
3084 return ret;
3085}
3086
3087
Chris Masone02119d2008-09-05 16:13:11 -04003088/*
3089 * helper function around btrfs_log_inode to make sure newly created
3090 * parent directories also end up in the log. A minimal inode and backref
3091 * only logging is done of any parent directories that are older than
3092 * the last committed transaction
3093 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003094int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3095 struct btrfs_root *root, struct inode *inode,
3096 struct dentry *parent, int exists_only)
Chris Masone02119d2008-09-05 16:13:11 -04003097{
Chris Mason12fcfd22009-03-24 10:24:20 -04003098 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04003099 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00003100 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04003101 int ret = 0;
3102 u64 last_committed = root->fs_info->last_trans_committed;
3103
3104 sb = inode->i_sb;
3105
Sage Weil3a5e1402009-04-02 16:49:40 -04003106 if (btrfs_test_opt(root, NOTREELOG)) {
3107 ret = 1;
3108 goto end_no_trans;
3109 }
3110
Chris Mason12fcfd22009-03-24 10:24:20 -04003111 if (root->fs_info->last_trans_log_full_commit >
3112 root->fs_info->last_trans_committed) {
3113 ret = 1;
3114 goto end_no_trans;
3115 }
3116
Yan, Zheng76dda932009-09-21 16:00:26 -04003117 if (root != BTRFS_I(inode)->root ||
3118 btrfs_root_refs(&root->root_item) == 0) {
3119 ret = 1;
3120 goto end_no_trans;
3121 }
3122
Chris Mason12fcfd22009-03-24 10:24:20 -04003123 ret = check_parent_dirs_for_sync(trans, inode, parent,
3124 sb, last_committed);
3125 if (ret)
3126 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04003127
Chris Mason257c62e2009-10-13 13:21:08 -04003128 if (inode_in_log(trans, inode)) {
3129 ret = BTRFS_NO_LOG_SYNC;
3130 goto end_no_trans;
3131 }
3132
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003133 ret = start_log_trans(trans, root);
3134 if (ret)
3135 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003136
3137 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003138 if (ret)
3139 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003140
Chris Masonaf4176b2009-03-24 10:24:31 -04003141 /*
3142 * for regular files, if its inode is already on disk, we don't
3143 * have to worry about the parents at all. This is because
3144 * we can use the last_unlink_trans field to record renames
3145 * and other fun in this file.
3146 */
3147 if (S_ISREG(inode->i_mode) &&
3148 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003149 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3150 ret = 0;
3151 goto end_trans;
3152 }
Chris Masonaf4176b2009-03-24 10:24:31 -04003153
3154 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05003155 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003156 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04003157 break;
3158
Chris Mason12fcfd22009-03-24 10:24:20 -04003159 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04003160 if (root != BTRFS_I(inode)->root)
3161 break;
3162
Chris Mason12fcfd22009-03-24 10:24:20 -04003163 if (BTRFS_I(inode)->generation >
3164 root->fs_info->last_trans_committed) {
3165 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003166 if (ret)
3167 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04003168 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003169 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04003170 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04003171
Josef Bacik6a912212010-11-20 09:48:00 +00003172 parent = dget_parent(parent);
3173 dput(old_parent);
3174 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04003175 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003176 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003177end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00003178 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003179 if (ret < 0) {
3180 BUG_ON(ret != -ENOSPC);
3181 root->fs_info->last_trans_log_full_commit = trans->transid;
3182 ret = 1;
3183 }
Chris Mason12fcfd22009-03-24 10:24:20 -04003184 btrfs_end_log_trans(root);
3185end_no_trans:
3186 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003187}
3188
3189/*
3190 * it is not safe to log dentry if the chunk root has added new
3191 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3192 * If this returns 1, you must commit the transaction to safely get your
3193 * data on disk.
3194 */
3195int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3196 struct btrfs_root *root, struct dentry *dentry)
3197{
Josef Bacik6a912212010-11-20 09:48:00 +00003198 struct dentry *parent = dget_parent(dentry);
3199 int ret;
3200
3201 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3202 dput(parent);
3203
3204 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003205}
3206
3207/*
3208 * should be called during mount to recover any replay any log trees
3209 * from the FS
3210 */
3211int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3212{
3213 int ret;
3214 struct btrfs_path *path;
3215 struct btrfs_trans_handle *trans;
3216 struct btrfs_key key;
3217 struct btrfs_key found_key;
3218 struct btrfs_key tmp_key;
3219 struct btrfs_root *log;
3220 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3221 struct walk_control wc = {
3222 .process_func = process_one_buffer,
3223 .stage = 0,
3224 };
3225
Chris Masone02119d2008-09-05 16:13:11 -04003226 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003227 if (!path)
3228 return -ENOMEM;
3229
3230 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003231
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003232 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003233 if (IS_ERR(trans)) {
3234 ret = PTR_ERR(trans);
3235 goto error;
3236 }
Chris Masone02119d2008-09-05 16:13:11 -04003237
3238 wc.trans = trans;
3239 wc.pin = 1;
3240
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003241 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003242 if (ret) {
3243 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3244 "recovering log root tree.");
3245 goto error;
3246 }
Chris Masone02119d2008-09-05 16:13:11 -04003247
3248again:
3249 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3250 key.offset = (u64)-1;
3251 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3252
Chris Masond3977122009-01-05 21:25:51 -05003253 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003254 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003255
3256 if (ret < 0) {
3257 btrfs_error(fs_info, ret,
3258 "Couldn't find tree log root.");
3259 goto error;
3260 }
Chris Masone02119d2008-09-05 16:13:11 -04003261 if (ret > 0) {
3262 if (path->slots[0] == 0)
3263 break;
3264 path->slots[0]--;
3265 }
3266 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3267 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003268 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003269 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3270 break;
3271
3272 log = btrfs_read_fs_root_no_radix(log_root_tree,
3273 &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003274 if (IS_ERR(log)) {
3275 ret = PTR_ERR(log);
3276 btrfs_error(fs_info, ret,
3277 "Couldn't read tree log root.");
3278 goto error;
3279 }
Chris Masone02119d2008-09-05 16:13:11 -04003280
3281 tmp_key.objectid = found_key.offset;
3282 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3283 tmp_key.offset = (u64)-1;
3284
3285 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003286 if (IS_ERR(wc.replay_dest)) {
3287 ret = PTR_ERR(wc.replay_dest);
3288 btrfs_error(fs_info, ret, "Couldn't read target root "
3289 "for tree log recovery.");
3290 goto error;
3291 }
Chris Masone02119d2008-09-05 16:13:11 -04003292
Yan Zheng07d400a2009-01-06 11:42:00 -05003293 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003294 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04003295 ret = walk_log_tree(trans, log, &wc);
3296 BUG_ON(ret);
3297
3298 if (wc.stage == LOG_WALK_REPLAY_ALL) {
3299 ret = fixup_inode_link_counts(trans, wc.replay_dest,
3300 path);
3301 BUG_ON(ret);
3302 }
Chris Masone02119d2008-09-05 16:13:11 -04003303
3304 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05003305 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04003306 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04003307 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04003308 kfree(log);
3309
3310 if (found_key.offset == 0)
3311 break;
3312 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003313 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003314
3315 /* step one is to pin it all, step two is to replay just inodes */
3316 if (wc.pin) {
3317 wc.pin = 0;
3318 wc.process_func = replay_one_buffer;
3319 wc.stage = LOG_WALK_REPLAY_INODES;
3320 goto again;
3321 }
3322 /* step three is to replay everything */
3323 if (wc.stage < LOG_WALK_REPLAY_ALL) {
3324 wc.stage++;
3325 goto again;
3326 }
3327
3328 btrfs_free_path(path);
3329
3330 free_extent_buffer(log_root_tree->node);
3331 log_root_tree->log_root = NULL;
3332 fs_info->log_root_recovering = 0;
3333
3334 /* step 4: commit the transaction, which also unpins the blocks */
3335 btrfs_commit_transaction(trans, fs_info->tree_root);
3336
3337 kfree(log_root_tree);
3338 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003339
3340error:
3341 btrfs_free_path(path);
3342 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003343}
Chris Mason12fcfd22009-03-24 10:24:20 -04003344
3345/*
3346 * there are some corner cases where we want to force a full
3347 * commit instead of allowing a directory to be logged.
3348 *
3349 * They revolve around files there were unlinked from the directory, and
3350 * this function updates the parent directory so that a full commit is
3351 * properly done if it is fsync'd later after the unlinks are done.
3352 */
3353void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
3354 struct inode *dir, struct inode *inode,
3355 int for_rename)
3356{
3357 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003358 * when we're logging a file, if it hasn't been renamed
3359 * or unlinked, and its inode is fully committed on disk,
3360 * we don't have to worry about walking up the directory chain
3361 * to log its parents.
3362 *
3363 * So, we use the last_unlink_trans field to put this transid
3364 * into the file. When the file is logged we check it and
3365 * don't log the parents if the file is fully on disk.
3366 */
3367 if (S_ISREG(inode->i_mode))
3368 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3369
3370 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003371 * if this directory was already logged any new
3372 * names for this file/dir will get recorded
3373 */
3374 smp_mb();
3375 if (BTRFS_I(dir)->logged_trans == trans->transid)
3376 return;
3377
3378 /*
3379 * if the inode we're about to unlink was logged,
3380 * the log will be properly updated for any new names
3381 */
3382 if (BTRFS_I(inode)->logged_trans == trans->transid)
3383 return;
3384
3385 /*
3386 * when renaming files across directories, if the directory
3387 * there we're unlinking from gets fsync'd later on, there's
3388 * no way to find the destination directory later and fsync it
3389 * properly. So, we have to be conservative and force commits
3390 * so the new name gets discovered.
3391 */
3392 if (for_rename)
3393 goto record;
3394
3395 /* we can safely do the unlink without any special recording */
3396 return;
3397
3398record:
3399 BTRFS_I(dir)->last_unlink_trans = trans->transid;
3400}
3401
3402/*
3403 * Call this after adding a new name for a file and it will properly
3404 * update the log to reflect the new name.
3405 *
3406 * It will return zero if all goes well, and it will return 1 if a
3407 * full transaction commit is required.
3408 */
3409int btrfs_log_new_name(struct btrfs_trans_handle *trans,
3410 struct inode *inode, struct inode *old_dir,
3411 struct dentry *parent)
3412{
3413 struct btrfs_root * root = BTRFS_I(inode)->root;
3414
3415 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04003416 * this will force the logging code to walk the dentry chain
3417 * up for the file
3418 */
3419 if (S_ISREG(inode->i_mode))
3420 BTRFS_I(inode)->last_unlink_trans = trans->transid;
3421
3422 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04003423 * if this inode hasn't been logged and directory we're renaming it
3424 * from hasn't been logged, we don't need to log it
3425 */
3426 if (BTRFS_I(inode)->logged_trans <=
3427 root->fs_info->last_trans_committed &&
3428 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
3429 root->fs_info->last_trans_committed))
3430 return 0;
3431
3432 return btrfs_log_inode_parent(trans, root, inode, parent, 1);
3433}
3434