Commit | Line | Data |
---|---|---|
e02119d5 CM |
1 | /* |
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> | |
20 | #include "ctree.h" | |
21 | #include "transaction.h" | |
22 | #include "disk-io.h" | |
23 | #include "locking.h" | |
24 | #include "print-tree.h" | |
25 | #include "compat.h" | |
b2950863 | 26 | #include "tree-log.h" |
e02119d5 CM |
27 | |
28 | /* magic values for the inode_only field in btrfs_log_inode: | |
29 | * | |
30 | * LOG_INODE_ALL means to log everything | |
31 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | |
32 | * during log replay | |
33 | */ | |
34 | #define LOG_INODE_ALL 0 | |
35 | #define LOG_INODE_EXISTS 1 | |
36 | ||
37 | /* | |
38 | * stages for the tree walking. The first | |
39 | * stage (0) is to only pin down the blocks we find | |
40 | * the second stage (1) is to make sure that all the inodes | |
41 | * we find in the log are created in the subvolume. | |
42 | * | |
43 | * The last stage is to deal with directories and links and extents | |
44 | * and all the other fun semantics | |
45 | */ | |
46 | #define LOG_WALK_PIN_ONLY 0 | |
47 | #define LOG_WALK_REPLAY_INODES 1 | |
48 | #define LOG_WALK_REPLAY_ALL 2 | |
49 | ||
50 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, | |
51 | struct btrfs_root *root, struct inode *inode, | |
52 | int inode_only); | |
53 | ||
54 | /* | |
55 | * tree logging is a special write ahead log used to make sure that | |
56 | * fsyncs and O_SYNCs can happen without doing full tree commits. | |
57 | * | |
58 | * Full tree commits are expensive because they require commonly | |
59 | * modified blocks to be recowed, creating many dirty pages in the | |
60 | * extent tree an 4x-6x higher write load than ext3. | |
61 | * | |
62 | * Instead of doing a tree commit on every fsync, we use the | |
63 | * key ranges and transaction ids to find items for a given file or directory | |
64 | * that have changed in this transaction. Those items are copied into | |
65 | * a special tree (one per subvolume root), that tree is written to disk | |
66 | * and then the fsync is considered complete. | |
67 | * | |
68 | * After a crash, items are copied out of the log-tree back into the | |
69 | * subvolume tree. Any file data extents found are recorded in the extent | |
70 | * allocation tree, and the log-tree freed. | |
71 | * | |
72 | * The log tree is read three times, once to pin down all the extents it is | |
73 | * using in ram and once, once to create all the inodes logged in the tree | |
74 | * and once to do all the other items. | |
75 | */ | |
76 | ||
77 | /* | |
78 | * btrfs_add_log_tree adds a new per-subvolume log tree into the | |
79 | * tree of log tree roots. This must be called with a tree log transaction | |
80 | * running (see start_log_trans). | |
81 | */ | |
b2950863 | 82 | static int btrfs_add_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
83 | struct btrfs_root *root) |
84 | { | |
85 | struct btrfs_key key; | |
86 | struct btrfs_root_item root_item; | |
87 | struct btrfs_inode_item *inode_item; | |
88 | struct extent_buffer *leaf; | |
89 | struct btrfs_root *new_root = root; | |
90 | int ret; | |
91 | u64 objectid = root->root_key.objectid; | |
92 | ||
31840ae1 | 93 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
e02119d5 | 94 | BTRFS_TREE_LOG_OBJECTID, |
31840ae1 | 95 | trans->transid, 0, 0, 0); |
e02119d5 CM |
96 | if (IS_ERR(leaf)) { |
97 | ret = PTR_ERR(leaf); | |
98 | return ret; | |
99 | } | |
100 | ||
101 | btrfs_set_header_nritems(leaf, 0); | |
102 | btrfs_set_header_level(leaf, 0); | |
103 | btrfs_set_header_bytenr(leaf, leaf->start); | |
104 | btrfs_set_header_generation(leaf, trans->transid); | |
105 | btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); | |
106 | ||
107 | write_extent_buffer(leaf, root->fs_info->fsid, | |
108 | (unsigned long)btrfs_header_fsid(leaf), | |
109 | BTRFS_FSID_SIZE); | |
110 | btrfs_mark_buffer_dirty(leaf); | |
111 | ||
112 | inode_item = &root_item.inode; | |
113 | memset(inode_item, 0, sizeof(*inode_item)); | |
114 | inode_item->generation = cpu_to_le64(1); | |
115 | inode_item->size = cpu_to_le64(3); | |
116 | inode_item->nlink = cpu_to_le32(1); | |
a76a3cd4 | 117 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
e02119d5 CM |
118 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
119 | ||
120 | btrfs_set_root_bytenr(&root_item, leaf->start); | |
84234f3a | 121 | btrfs_set_root_generation(&root_item, trans->transid); |
e02119d5 CM |
122 | btrfs_set_root_level(&root_item, 0); |
123 | btrfs_set_root_refs(&root_item, 0); | |
124 | btrfs_set_root_used(&root_item, 0); | |
125 | ||
126 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | |
127 | root_item.drop_level = 0; | |
128 | ||
129 | btrfs_tree_unlock(leaf); | |
130 | free_extent_buffer(leaf); | |
131 | leaf = NULL; | |
132 | ||
133 | btrfs_set_root_dirid(&root_item, 0); | |
134 | ||
135 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
136 | key.offset = objectid; | |
137 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
138 | ret = btrfs_insert_root(trans, root->fs_info->log_root_tree, &key, | |
139 | &root_item); | |
140 | if (ret) | |
141 | goto fail; | |
142 | ||
143 | new_root = btrfs_read_fs_root_no_radix(root->fs_info->log_root_tree, | |
144 | &key); | |
145 | BUG_ON(!new_root); | |
146 | ||
147 | WARN_ON(root->log_root); | |
148 | root->log_root = new_root; | |
149 | ||
150 | /* | |
151 | * log trees do not get reference counted because they go away | |
152 | * before a real commit is actually done. They do store pointers | |
153 | * to file data extents, and those reference counts still get | |
154 | * updated (along with back refs to the log tree). | |
155 | */ | |
156 | new_root->ref_cows = 0; | |
157 | new_root->last_trans = trans->transid; | |
158 | fail: | |
159 | return ret; | |
160 | } | |
161 | ||
162 | /* | |
163 | * start a sub transaction and setup the log tree | |
164 | * this increments the log tree writer count to make the people | |
165 | * syncing the tree wait for us to finish | |
166 | */ | |
167 | static int start_log_trans(struct btrfs_trans_handle *trans, | |
168 | struct btrfs_root *root) | |
169 | { | |
170 | int ret; | |
171 | mutex_lock(&root->fs_info->tree_log_mutex); | |
172 | if (!root->fs_info->log_root_tree) { | |
173 | ret = btrfs_init_log_root_tree(trans, root->fs_info); | |
174 | BUG_ON(ret); | |
175 | } | |
176 | if (!root->log_root) { | |
177 | ret = btrfs_add_log_tree(trans, root); | |
178 | BUG_ON(ret); | |
179 | } | |
180 | atomic_inc(&root->fs_info->tree_log_writers); | |
181 | root->fs_info->tree_log_batch++; | |
182 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
183 | return 0; | |
184 | } | |
185 | ||
186 | /* | |
187 | * returns 0 if there was a log transaction running and we were able | |
188 | * to join, or returns -ENOENT if there were not transactions | |
189 | * in progress | |
190 | */ | |
191 | static int join_running_log_trans(struct btrfs_root *root) | |
192 | { | |
193 | int ret = -ENOENT; | |
194 | ||
195 | smp_mb(); | |
196 | if (!root->log_root) | |
197 | return -ENOENT; | |
198 | ||
199 | mutex_lock(&root->fs_info->tree_log_mutex); | |
200 | if (root->log_root) { | |
201 | ret = 0; | |
202 | atomic_inc(&root->fs_info->tree_log_writers); | |
203 | root->fs_info->tree_log_batch++; | |
204 | } | |
205 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
206 | return ret; | |
207 | } | |
208 | ||
209 | /* | |
210 | * indicate we're done making changes to the log tree | |
211 | * and wake up anyone waiting to do a sync | |
212 | */ | |
213 | static int end_log_trans(struct btrfs_root *root) | |
214 | { | |
215 | atomic_dec(&root->fs_info->tree_log_writers); | |
216 | smp_mb(); | |
217 | if (waitqueue_active(&root->fs_info->tree_log_wait)) | |
218 | wake_up(&root->fs_info->tree_log_wait); | |
219 | return 0; | |
220 | } | |
221 | ||
222 | ||
223 | /* | |
224 | * the walk control struct is used to pass state down the chain when | |
225 | * processing the log tree. The stage field tells us which part | |
226 | * of the log tree processing we are currently doing. The others | |
227 | * are state fields used for that specific part | |
228 | */ | |
229 | struct walk_control { | |
230 | /* should we free the extent on disk when done? This is used | |
231 | * at transaction commit time while freeing a log tree | |
232 | */ | |
233 | int free; | |
234 | ||
235 | /* should we write out the extent buffer? This is used | |
236 | * while flushing the log tree to disk during a sync | |
237 | */ | |
238 | int write; | |
239 | ||
240 | /* should we wait for the extent buffer io to finish? Also used | |
241 | * while flushing the log tree to disk for a sync | |
242 | */ | |
243 | int wait; | |
244 | ||
245 | /* pin only walk, we record which extents on disk belong to the | |
246 | * log trees | |
247 | */ | |
248 | int pin; | |
249 | ||
250 | /* what stage of the replay code we're currently in */ | |
251 | int stage; | |
252 | ||
253 | /* the root we are currently replaying */ | |
254 | struct btrfs_root *replay_dest; | |
255 | ||
256 | /* the trans handle for the current replay */ | |
257 | struct btrfs_trans_handle *trans; | |
258 | ||
259 | /* the function that gets used to process blocks we find in the | |
260 | * tree. Note the extent_buffer might not be up to date when it is | |
261 | * passed in, and it must be checked or read if you need the data | |
262 | * inside it | |
263 | */ | |
264 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | |
265 | struct walk_control *wc, u64 gen); | |
266 | }; | |
267 | ||
268 | /* | |
269 | * process_func used to pin down extents, write them or wait on them | |
270 | */ | |
271 | static int process_one_buffer(struct btrfs_root *log, | |
272 | struct extent_buffer *eb, | |
273 | struct walk_control *wc, u64 gen) | |
274 | { | |
275 | if (wc->pin) { | |
25179201 | 276 | mutex_lock(&log->fs_info->pinned_mutex); |
e02119d5 CM |
277 | btrfs_update_pinned_extents(log->fs_info->extent_root, |
278 | eb->start, eb->len, 1); | |
25179201 | 279 | mutex_unlock(&log->fs_info->pinned_mutex); |
e02119d5 CM |
280 | } |
281 | ||
282 | if (btrfs_buffer_uptodate(eb, gen)) { | |
283 | 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 | */ | |
305 | static 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; | |
318 | ||
319 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
320 | overwrite_root = 1; | |
321 | ||
322 | item_size = btrfs_item_size_nr(eb, slot); | |
323 | src_ptr = btrfs_item_ptr_offset(eb, slot); | |
324 | ||
325 | /* look for the key in the destination tree */ | |
326 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
327 | if (ret == 0) { | |
328 | char *src_copy; | |
329 | char *dst_copy; | |
330 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], | |
331 | path->slots[0]); | |
332 | if (dst_size != item_size) | |
333 | goto insert; | |
334 | ||
335 | if (item_size == 0) { | |
336 | btrfs_release_path(root, path); | |
337 | return 0; | |
338 | } | |
339 | dst_copy = kmalloc(item_size, GFP_NOFS); | |
340 | src_copy = kmalloc(item_size, GFP_NOFS); | |
341 | ||
342 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | |
343 | ||
344 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
345 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | |
346 | item_size); | |
347 | ret = memcmp(dst_copy, src_copy, item_size); | |
348 | ||
349 | kfree(dst_copy); | |
350 | kfree(src_copy); | |
351 | /* | |
352 | * they have the same contents, just return, this saves | |
353 | * us from cowing blocks in the destination tree and doing | |
354 | * extra writes that may not have been done by a previous | |
355 | * sync | |
356 | */ | |
357 | if (ret == 0) { | |
358 | btrfs_release_path(root, path); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | } | |
363 | insert: | |
364 | btrfs_release_path(root, path); | |
365 | /* try to insert the key into the destination tree */ | |
366 | ret = btrfs_insert_empty_item(trans, root, path, | |
367 | key, item_size); | |
368 | ||
369 | /* make sure any existing item is the correct size */ | |
370 | if (ret == -EEXIST) { | |
371 | u32 found_size; | |
372 | found_size = btrfs_item_size_nr(path->nodes[0], | |
373 | path->slots[0]); | |
374 | if (found_size > item_size) { | |
375 | btrfs_truncate_item(trans, root, path, item_size, 1); | |
376 | } else if (found_size < item_size) { | |
377 | ret = btrfs_del_item(trans, root, | |
378 | path); | |
379 | BUG_ON(ret); | |
380 | ||
381 | btrfs_release_path(root, path); | |
382 | ret = btrfs_insert_empty_item(trans, | |
383 | root, path, key, item_size); | |
384 | BUG_ON(ret); | |
385 | } | |
386 | } else if (ret) { | |
387 | BUG(); | |
388 | } | |
389 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | |
390 | path->slots[0]); | |
391 | ||
392 | /* don't overwrite an existing inode if the generation number | |
393 | * was logged as zero. This is done when the tree logging code | |
394 | * is just logging an inode to make sure it exists after recovery. | |
395 | * | |
396 | * Also, don't overwrite i_size on directories during replay. | |
397 | * log replay inserts and removes directory items based on the | |
398 | * state of the tree found in the subvolume, and i_size is modified | |
399 | * as it goes | |
400 | */ | |
401 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | |
402 | struct btrfs_inode_item *src_item; | |
403 | struct btrfs_inode_item *dst_item; | |
404 | ||
405 | src_item = (struct btrfs_inode_item *)src_ptr; | |
406 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
407 | ||
408 | if (btrfs_inode_generation(eb, src_item) == 0) | |
409 | goto no_copy; | |
410 | ||
411 | if (overwrite_root && | |
412 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && | |
413 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { | |
414 | save_old_i_size = 1; | |
415 | saved_i_size = btrfs_inode_size(path->nodes[0], | |
416 | dst_item); | |
417 | } | |
418 | } | |
419 | ||
420 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | |
421 | src_ptr, item_size); | |
422 | ||
423 | if (save_old_i_size) { | |
424 | struct btrfs_inode_item *dst_item; | |
425 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
426 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | |
427 | } | |
428 | ||
429 | /* make sure the generation is filled in */ | |
430 | if (key->type == BTRFS_INODE_ITEM_KEY) { | |
431 | struct btrfs_inode_item *dst_item; | |
432 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
433 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | |
434 | btrfs_set_inode_generation(path->nodes[0], dst_item, | |
435 | trans->transid); | |
436 | } | |
437 | } | |
31840ae1 ZY |
438 | |
439 | if (overwrite_root && | |
440 | key->type == BTRFS_EXTENT_DATA_KEY) { | |
441 | int extent_type; | |
442 | struct btrfs_file_extent_item *fi; | |
443 | ||
444 | fi = (struct btrfs_file_extent_item *)dst_ptr; | |
445 | extent_type = btrfs_file_extent_type(path->nodes[0], fi); | |
d899e052 YZ |
446 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
447 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
31840ae1 ZY |
448 | struct btrfs_key ins; |
449 | ins.objectid = btrfs_file_extent_disk_bytenr( | |
450 | path->nodes[0], fi); | |
451 | ins.offset = btrfs_file_extent_disk_num_bytes( | |
452 | path->nodes[0], fi); | |
453 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
454 | ||
455 | /* | |
456 | * is this extent already allocated in the extent | |
457 | * allocation tree? If so, just add a reference | |
458 | */ | |
459 | ret = btrfs_lookup_extent(root, ins.objectid, | |
460 | ins.offset); | |
461 | if (ret == 0) { | |
462 | ret = btrfs_inc_extent_ref(trans, root, | |
463 | ins.objectid, ins.offset, | |
464 | path->nodes[0]->start, | |
465 | root->root_key.objectid, | |
3bb1a1bc | 466 | trans->transid, key->objectid); |
31840ae1 ZY |
467 | } else { |
468 | /* | |
469 | * insert the extent pointer in the extent | |
470 | * allocation tree | |
471 | */ | |
472 | ret = btrfs_alloc_logged_extent(trans, root, | |
473 | path->nodes[0]->start, | |
474 | root->root_key.objectid, | |
475 | trans->transid, key->objectid, | |
3bb1a1bc | 476 | &ins); |
31840ae1 ZY |
477 | BUG_ON(ret); |
478 | } | |
479 | } | |
480 | } | |
e02119d5 CM |
481 | no_copy: |
482 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
483 | btrfs_release_path(root, path); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | /* | |
488 | * simple helper to read an inode off the disk from a given root | |
489 | * This can only be called for subvolume roots and not for the log | |
490 | */ | |
491 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | |
492 | u64 objectid) | |
493 | { | |
494 | struct inode *inode; | |
495 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); | |
496 | if (inode->i_state & I_NEW) { | |
497 | BTRFS_I(inode)->root = root; | |
498 | BTRFS_I(inode)->location.objectid = objectid; | |
499 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
500 | BTRFS_I(inode)->location.offset = 0; | |
501 | btrfs_read_locked_inode(inode); | |
502 | unlock_new_inode(inode); | |
503 | ||
504 | } | |
505 | if (is_bad_inode(inode)) { | |
506 | iput(inode); | |
507 | inode = NULL; | |
508 | } | |
509 | return inode; | |
510 | } | |
511 | ||
512 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | |
513 | * subvolume 'root'. path is released on entry and should be released | |
514 | * on exit. | |
515 | * | |
516 | * extents in the log tree have not been allocated out of the extent | |
517 | * tree yet. So, this completes the allocation, taking a reference | |
518 | * as required if the extent already exists or creating a new extent | |
519 | * if it isn't in the extent allocation tree yet. | |
520 | * | |
521 | * The extent is inserted into the file, dropping any existing extents | |
522 | * from the file that overlap the new one. | |
523 | */ | |
524 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |
525 | struct btrfs_root *root, | |
526 | struct btrfs_path *path, | |
527 | struct extent_buffer *eb, int slot, | |
528 | struct btrfs_key *key) | |
529 | { | |
530 | int found_type; | |
531 | u64 mask = root->sectorsize - 1; | |
532 | u64 extent_end; | |
533 | u64 alloc_hint; | |
534 | u64 start = key->offset; | |
535 | struct btrfs_file_extent_item *item; | |
536 | struct inode *inode = NULL; | |
537 | unsigned long size; | |
538 | int ret = 0; | |
539 | ||
540 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
541 | found_type = btrfs_file_extent_type(eb, item); | |
542 | ||
d899e052 YZ |
543 | if (found_type == BTRFS_FILE_EXTENT_REG || |
544 | found_type == BTRFS_FILE_EXTENT_PREALLOC) | |
e02119d5 CM |
545 | extent_end = start + btrfs_file_extent_num_bytes(eb, item); |
546 | else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
c8b97818 | 547 | size = btrfs_file_extent_inline_len(eb, item); |
e02119d5 CM |
548 | extent_end = (start + size + mask) & ~mask; |
549 | } else { | |
550 | ret = 0; | |
551 | goto out; | |
552 | } | |
553 | ||
554 | inode = read_one_inode(root, key->objectid); | |
555 | if (!inode) { | |
556 | ret = -EIO; | |
557 | goto out; | |
558 | } | |
559 | ||
560 | /* | |
561 | * first check to see if we already have this extent in the | |
562 | * file. This must be done before the btrfs_drop_extents run | |
563 | * so we don't try to drop this extent. | |
564 | */ | |
565 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, | |
566 | start, 0); | |
567 | ||
d899e052 YZ |
568 | if (ret == 0 && |
569 | (found_type == BTRFS_FILE_EXTENT_REG || | |
570 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | |
e02119d5 CM |
571 | struct btrfs_file_extent_item cmp1; |
572 | struct btrfs_file_extent_item cmp2; | |
573 | struct btrfs_file_extent_item *existing; | |
574 | struct extent_buffer *leaf; | |
575 | ||
576 | leaf = path->nodes[0]; | |
577 | existing = btrfs_item_ptr(leaf, path->slots[0], | |
578 | struct btrfs_file_extent_item); | |
579 | ||
580 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | |
581 | sizeof(cmp1)); | |
582 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | |
583 | sizeof(cmp2)); | |
584 | ||
585 | /* | |
586 | * we already have a pointer to this exact extent, | |
587 | * we don't have to do anything | |
588 | */ | |
589 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | |
590 | btrfs_release_path(root, path); | |
591 | goto out; | |
592 | } | |
593 | } | |
594 | btrfs_release_path(root, path); | |
595 | ||
596 | /* drop any overlapping extents */ | |
597 | ret = btrfs_drop_extents(trans, root, inode, | |
598 | start, extent_end, start, &alloc_hint); | |
599 | BUG_ON(ret); | |
600 | ||
31840ae1 ZY |
601 | /* insert the extent */ |
602 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
e02119d5 | 603 | BUG_ON(ret); |
e02119d5 | 604 | |
a76a3cd4 YZ |
605 | /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */ |
606 | inode_add_bytes(inode, extent_end - start); | |
e02119d5 CM |
607 | btrfs_update_inode(trans, root, inode); |
608 | out: | |
609 | if (inode) | |
610 | iput(inode); | |
611 | return ret; | |
612 | } | |
613 | ||
614 | /* | |
615 | * when cleaning up conflicts between the directory names in the | |
616 | * subvolume, directory names in the log and directory names in the | |
617 | * inode back references, we may have to unlink inodes from directories. | |
618 | * | |
619 | * This is a helper function to do the unlink of a specific directory | |
620 | * item | |
621 | */ | |
622 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | |
623 | struct btrfs_root *root, | |
624 | struct btrfs_path *path, | |
625 | struct inode *dir, | |
626 | struct btrfs_dir_item *di) | |
627 | { | |
628 | struct inode *inode; | |
629 | char *name; | |
630 | int name_len; | |
631 | struct extent_buffer *leaf; | |
632 | struct btrfs_key location; | |
633 | int ret; | |
634 | ||
635 | leaf = path->nodes[0]; | |
636 | ||
637 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
638 | name_len = btrfs_dir_name_len(leaf, di); | |
639 | name = kmalloc(name_len, GFP_NOFS); | |
640 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); | |
641 | btrfs_release_path(root, path); | |
642 | ||
643 | inode = read_one_inode(root, location.objectid); | |
644 | BUG_ON(!inode); | |
645 | ||
646 | btrfs_inc_nlink(inode); | |
647 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
648 | kfree(name); | |
649 | ||
650 | iput(inode); | |
651 | return ret; | |
652 | } | |
653 | ||
654 | /* | |
655 | * helper function to see if a given name and sequence number found | |
656 | * in an inode back reference are already in a directory and correctly | |
657 | * point to this inode | |
658 | */ | |
659 | static noinline int inode_in_dir(struct btrfs_root *root, | |
660 | struct btrfs_path *path, | |
661 | u64 dirid, u64 objectid, u64 index, | |
662 | const char *name, int name_len) | |
663 | { | |
664 | struct btrfs_dir_item *di; | |
665 | struct btrfs_key location; | |
666 | int match = 0; | |
667 | ||
668 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | |
669 | index, name, name_len, 0); | |
670 | if (di && !IS_ERR(di)) { | |
671 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
672 | if (location.objectid != objectid) | |
673 | goto out; | |
674 | } else | |
675 | goto out; | |
676 | btrfs_release_path(root, path); | |
677 | ||
678 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); | |
679 | if (di && !IS_ERR(di)) { | |
680 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
681 | if (location.objectid != objectid) | |
682 | goto out; | |
683 | } else | |
684 | goto out; | |
685 | match = 1; | |
686 | out: | |
687 | btrfs_release_path(root, path); | |
688 | return match; | |
689 | } | |
690 | ||
691 | /* | |
692 | * helper function to check a log tree for a named back reference in | |
693 | * an inode. This is used to decide if a back reference that is | |
694 | * found in the subvolume conflicts with what we find in the log. | |
695 | * | |
696 | * inode backreferences may have multiple refs in a single item, | |
697 | * during replay we process one reference at a time, and we don't | |
698 | * want to delete valid links to a file from the subvolume if that | |
699 | * link is also in the log. | |
700 | */ | |
701 | static noinline int backref_in_log(struct btrfs_root *log, | |
702 | struct btrfs_key *key, | |
703 | char *name, int namelen) | |
704 | { | |
705 | struct btrfs_path *path; | |
706 | struct btrfs_inode_ref *ref; | |
707 | unsigned long ptr; | |
708 | unsigned long ptr_end; | |
709 | unsigned long name_ptr; | |
710 | int found_name_len; | |
711 | int item_size; | |
712 | int ret; | |
713 | int match = 0; | |
714 | ||
715 | path = btrfs_alloc_path(); | |
716 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); | |
717 | if (ret != 0) | |
718 | goto out; | |
719 | ||
720 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | |
721 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
722 | ptr_end = ptr + item_size; | |
723 | while (ptr < ptr_end) { | |
724 | ref = (struct btrfs_inode_ref *)ptr; | |
725 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); | |
726 | if (found_name_len == namelen) { | |
727 | name_ptr = (unsigned long)(ref + 1); | |
728 | ret = memcmp_extent_buffer(path->nodes[0], name, | |
729 | name_ptr, namelen); | |
730 | if (ret == 0) { | |
731 | match = 1; | |
732 | goto out; | |
733 | } | |
734 | } | |
735 | ptr = (unsigned long)(ref + 1) + found_name_len; | |
736 | } | |
737 | out: | |
738 | btrfs_free_path(path); | |
739 | return match; | |
740 | } | |
741 | ||
742 | ||
743 | /* | |
744 | * replay one inode back reference item found in the log tree. | |
745 | * eb, slot and key refer to the buffer and key found in the log tree. | |
746 | * root is the destination we are replaying into, and path is for temp | |
747 | * use by this function. (it should be released on return). | |
748 | */ | |
749 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |
750 | struct btrfs_root *root, | |
751 | struct btrfs_root *log, | |
752 | struct btrfs_path *path, | |
753 | struct extent_buffer *eb, int slot, | |
754 | struct btrfs_key *key) | |
755 | { | |
756 | struct inode *dir; | |
757 | int ret; | |
758 | struct btrfs_key location; | |
759 | struct btrfs_inode_ref *ref; | |
760 | struct btrfs_dir_item *di; | |
761 | struct inode *inode; | |
762 | char *name; | |
763 | int namelen; | |
764 | unsigned long ref_ptr; | |
765 | unsigned long ref_end; | |
766 | ||
767 | location.objectid = key->objectid; | |
768 | location.type = BTRFS_INODE_ITEM_KEY; | |
769 | location.offset = 0; | |
770 | ||
771 | /* | |
772 | * it is possible that we didn't log all the parent directories | |
773 | * for a given inode. If we don't find the dir, just don't | |
774 | * copy the back ref in. The link count fixup code will take | |
775 | * care of the rest | |
776 | */ | |
777 | dir = read_one_inode(root, key->offset); | |
778 | if (!dir) | |
779 | return -ENOENT; | |
780 | ||
781 | inode = read_one_inode(root, key->objectid); | |
782 | BUG_ON(!dir); | |
783 | ||
784 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | |
785 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); | |
786 | ||
787 | again: | |
788 | ref = (struct btrfs_inode_ref *)ref_ptr; | |
789 | ||
790 | namelen = btrfs_inode_ref_name_len(eb, ref); | |
791 | name = kmalloc(namelen, GFP_NOFS); | |
792 | BUG_ON(!name); | |
793 | ||
794 | read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen); | |
795 | ||
796 | /* if we already have a perfect match, we're done */ | |
797 | if (inode_in_dir(root, path, dir->i_ino, inode->i_ino, | |
798 | btrfs_inode_ref_index(eb, ref), | |
799 | name, namelen)) { | |
800 | goto out; | |
801 | } | |
802 | ||
803 | /* | |
804 | * look for a conflicting back reference in the metadata. | |
805 | * if we find one we have to unlink that name of the file | |
806 | * before we add our new link. Later on, we overwrite any | |
807 | * existing back reference, and we don't want to create | |
808 | * dangling pointers in the directory. | |
809 | */ | |
810 | conflict_again: | |
811 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
812 | if (ret == 0) { | |
813 | char *victim_name; | |
814 | int victim_name_len; | |
815 | struct btrfs_inode_ref *victim_ref; | |
816 | unsigned long ptr; | |
817 | unsigned long ptr_end; | |
818 | struct extent_buffer *leaf = path->nodes[0]; | |
819 | ||
820 | /* are we trying to overwrite a back ref for the root directory | |
821 | * if so, just jump out, we're done | |
822 | */ | |
823 | if (key->objectid == key->offset) | |
824 | goto out_nowrite; | |
825 | ||
826 | /* check all the names in this back reference to see | |
827 | * if they are in the log. if so, we allow them to stay | |
828 | * otherwise they must be unlinked as a conflict | |
829 | */ | |
830 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
831 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); | |
832 | while(ptr < ptr_end) { | |
833 | victim_ref = (struct btrfs_inode_ref *)ptr; | |
834 | victim_name_len = btrfs_inode_ref_name_len(leaf, | |
835 | victim_ref); | |
836 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | |
837 | BUG_ON(!victim_name); | |
838 | ||
839 | read_extent_buffer(leaf, victim_name, | |
840 | (unsigned long)(victim_ref + 1), | |
841 | victim_name_len); | |
842 | ||
843 | if (!backref_in_log(log, key, victim_name, | |
844 | victim_name_len)) { | |
845 | btrfs_inc_nlink(inode); | |
846 | btrfs_release_path(root, path); | |
847 | ret = btrfs_unlink_inode(trans, root, dir, | |
848 | inode, victim_name, | |
849 | victim_name_len); | |
850 | kfree(victim_name); | |
851 | btrfs_release_path(root, path); | |
852 | goto conflict_again; | |
853 | } | |
854 | kfree(victim_name); | |
855 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; | |
856 | } | |
857 | BUG_ON(ret); | |
858 | } | |
859 | btrfs_release_path(root, path); | |
860 | ||
861 | /* look for a conflicting sequence number */ | |
862 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | |
863 | btrfs_inode_ref_index(eb, ref), | |
864 | name, namelen, 0); | |
865 | if (di && !IS_ERR(di)) { | |
866 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
867 | BUG_ON(ret); | |
868 | } | |
869 | btrfs_release_path(root, path); | |
870 | ||
871 | ||
872 | /* look for a conflicting name */ | |
873 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, | |
874 | name, namelen, 0); | |
875 | if (di && !IS_ERR(di)) { | |
876 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
877 | BUG_ON(ret); | |
878 | } | |
879 | btrfs_release_path(root, path); | |
880 | ||
881 | /* insert our name */ | |
882 | ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, | |
883 | btrfs_inode_ref_index(eb, ref)); | |
884 | BUG_ON(ret); | |
885 | ||
886 | btrfs_update_inode(trans, root, inode); | |
887 | ||
888 | out: | |
889 | ref_ptr = (unsigned long)(ref + 1) + namelen; | |
890 | kfree(name); | |
891 | if (ref_ptr < ref_end) | |
892 | goto again; | |
893 | ||
894 | /* finally write the back reference in the inode */ | |
895 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
896 | BUG_ON(ret); | |
897 | ||
898 | out_nowrite: | |
899 | btrfs_release_path(root, path); | |
900 | iput(dir); | |
901 | iput(inode); | |
902 | return 0; | |
903 | } | |
904 | ||
905 | /* | |
906 | * replay one csum item from the log tree into the subvolume 'root' | |
907 | * eb, slot and key all refer to the log tree | |
908 | * path is for temp use by this function and should be released on return | |
909 | * | |
910 | * This copies the checksums out of the log tree and inserts them into | |
911 | * the subvolume. Any existing checksums for this range in the file | |
912 | * are overwritten, and new items are added where required. | |
913 | * | |
914 | * We keep this simple by reusing the btrfs_ordered_sum code from | |
915 | * the data=ordered mode. This basically means making a copy | |
916 | * of all the checksums in ram, which we have to do anyway for kmap | |
917 | * rules. | |
918 | * | |
919 | * The copy is then sent down to btrfs_csum_file_blocks, which | |
920 | * does all the hard work of finding existing items in the file | |
921 | * or adding new ones. | |
922 | */ | |
923 | static noinline int replay_one_csum(struct btrfs_trans_handle *trans, | |
924 | struct btrfs_root *root, | |
925 | struct btrfs_path *path, | |
926 | struct extent_buffer *eb, int slot, | |
927 | struct btrfs_key *key) | |
928 | { | |
929 | int ret; | |
930 | u32 item_size = btrfs_item_size_nr(eb, slot); | |
931 | u64 cur_offset; | |
607d432d JB |
932 | u16 csum_size = |
933 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
e02119d5 CM |
934 | unsigned long file_bytes; |
935 | struct btrfs_ordered_sum *sums; | |
936 | struct btrfs_sector_sum *sector_sum; | |
937 | struct inode *inode; | |
938 | unsigned long ptr; | |
939 | ||
607d432d | 940 | file_bytes = (item_size / csum_size) * root->sectorsize; |
e02119d5 CM |
941 | inode = read_one_inode(root, key->objectid); |
942 | if (!inode) { | |
943 | return -EIO; | |
944 | } | |
945 | ||
946 | sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS); | |
947 | if (!sums) { | |
948 | iput(inode); | |
949 | return -ENOMEM; | |
950 | } | |
951 | ||
952 | INIT_LIST_HEAD(&sums->list); | |
953 | sums->len = file_bytes; | |
954 | sums->file_offset = key->offset; | |
955 | ||
956 | /* | |
957 | * copy all the sums into the ordered sum struct | |
958 | */ | |
959 | sector_sum = sums->sums; | |
960 | cur_offset = key->offset; | |
961 | ptr = btrfs_item_ptr_offset(eb, slot); | |
962 | while(item_size > 0) { | |
963 | sector_sum->offset = cur_offset; | |
607d432d | 964 | read_extent_buffer(eb, §or_sum->sum, ptr, csum_size); |
e02119d5 | 965 | sector_sum++; |
607d432d JB |
966 | item_size -= csum_size; |
967 | ptr += csum_size; | |
e02119d5 CM |
968 | cur_offset += root->sectorsize; |
969 | } | |
970 | ||
971 | /* let btrfs_csum_file_blocks add them into the file */ | |
972 | ret = btrfs_csum_file_blocks(trans, root, inode, sums); | |
973 | BUG_ON(ret); | |
974 | kfree(sums); | |
975 | iput(inode); | |
976 | ||
977 | return 0; | |
978 | } | |
979 | /* | |
980 | * There are a few corners where the link count of the file can't | |
981 | * be properly maintained during replay. So, instead of adding | |
982 | * lots of complexity to the log code, we just scan the backrefs | |
983 | * for any file that has been through replay. | |
984 | * | |
985 | * The scan will update the link count on the inode to reflect the | |
986 | * number of back refs found. If it goes down to zero, the iput | |
987 | * will free the inode. | |
988 | */ | |
989 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | |
990 | struct btrfs_root *root, | |
991 | struct inode *inode) | |
992 | { | |
993 | struct btrfs_path *path; | |
994 | int ret; | |
995 | struct btrfs_key key; | |
996 | u64 nlink = 0; | |
997 | unsigned long ptr; | |
998 | unsigned long ptr_end; | |
999 | int name_len; | |
1000 | ||
1001 | key.objectid = inode->i_ino; | |
1002 | key.type = BTRFS_INODE_REF_KEY; | |
1003 | key.offset = (u64)-1; | |
1004 | ||
1005 | path = btrfs_alloc_path(); | |
1006 | ||
1007 | while(1) { | |
1008 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1009 | if (ret < 0) | |
1010 | break; | |
1011 | if (ret > 0) { | |
1012 | if (path->slots[0] == 0) | |
1013 | break; | |
1014 | path->slots[0]--; | |
1015 | } | |
1016 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
1017 | path->slots[0]); | |
1018 | if (key.objectid != inode->i_ino || | |
1019 | key.type != BTRFS_INODE_REF_KEY) | |
1020 | break; | |
1021 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
1022 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], | |
1023 | path->slots[0]); | |
1024 | while(ptr < ptr_end) { | |
1025 | struct btrfs_inode_ref *ref; | |
1026 | ||
1027 | ref = (struct btrfs_inode_ref *)ptr; | |
1028 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | |
1029 | ref); | |
1030 | ptr = (unsigned long)(ref + 1) + name_len; | |
1031 | nlink++; | |
1032 | } | |
1033 | ||
1034 | if (key.offset == 0) | |
1035 | break; | |
1036 | key.offset--; | |
1037 | btrfs_release_path(root, path); | |
1038 | } | |
1039 | btrfs_free_path(path); | |
1040 | if (nlink != inode->i_nlink) { | |
1041 | inode->i_nlink = nlink; | |
1042 | btrfs_update_inode(trans, root, inode); | |
1043 | } | |
8d5bf1cb | 1044 | BTRFS_I(inode)->index_cnt = (u64)-1; |
e02119d5 CM |
1045 | |
1046 | return 0; | |
1047 | } | |
1048 | ||
1049 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | |
1050 | struct btrfs_root *root, | |
1051 | struct btrfs_path *path) | |
1052 | { | |
1053 | int ret; | |
1054 | struct btrfs_key key; | |
1055 | struct inode *inode; | |
1056 | ||
1057 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1058 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
1059 | key.offset = (u64)-1; | |
1060 | while(1) { | |
1061 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1062 | if (ret < 0) | |
1063 | break; | |
1064 | ||
1065 | if (ret == 1) { | |
1066 | if (path->slots[0] == 0) | |
1067 | break; | |
1068 | path->slots[0]--; | |
1069 | } | |
1070 | ||
1071 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1072 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | |
1073 | key.type != BTRFS_ORPHAN_ITEM_KEY) | |
1074 | break; | |
1075 | ||
1076 | ret = btrfs_del_item(trans, root, path); | |
1077 | BUG_ON(ret); | |
1078 | ||
1079 | btrfs_release_path(root, path); | |
1080 | inode = read_one_inode(root, key.offset); | |
1081 | BUG_ON(!inode); | |
1082 | ||
1083 | ret = fixup_inode_link_count(trans, root, inode); | |
1084 | BUG_ON(ret); | |
1085 | ||
1086 | iput(inode); | |
1087 | ||
1088 | if (key.offset == 0) | |
1089 | break; | |
1090 | key.offset--; | |
1091 | } | |
1092 | btrfs_release_path(root, path); | |
1093 | return 0; | |
1094 | } | |
1095 | ||
1096 | ||
1097 | /* | |
1098 | * record a given inode in the fixup dir so we can check its link | |
1099 | * count when replay is done. The link count is incremented here | |
1100 | * so the inode won't go away until we check it | |
1101 | */ | |
1102 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | |
1103 | struct btrfs_root *root, | |
1104 | struct btrfs_path *path, | |
1105 | u64 objectid) | |
1106 | { | |
1107 | struct btrfs_key key; | |
1108 | int ret = 0; | |
1109 | struct inode *inode; | |
1110 | ||
1111 | inode = read_one_inode(root, objectid); | |
1112 | BUG_ON(!inode); | |
1113 | ||
1114 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1115 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | |
1116 | key.offset = objectid; | |
1117 | ||
1118 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
1119 | ||
1120 | btrfs_release_path(root, path); | |
1121 | if (ret == 0) { | |
1122 | btrfs_inc_nlink(inode); | |
1123 | btrfs_update_inode(trans, root, inode); | |
1124 | } else if (ret == -EEXIST) { | |
1125 | ret = 0; | |
1126 | } else { | |
1127 | BUG(); | |
1128 | } | |
1129 | iput(inode); | |
1130 | ||
1131 | return ret; | |
1132 | } | |
1133 | ||
1134 | /* | |
1135 | * when replaying the log for a directory, we only insert names | |
1136 | * for inodes that actually exist. This means an fsync on a directory | |
1137 | * does not implicitly fsync all the new files in it | |
1138 | */ | |
1139 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | |
1140 | struct btrfs_root *root, | |
1141 | struct btrfs_path *path, | |
1142 | u64 dirid, u64 index, | |
1143 | char *name, int name_len, u8 type, | |
1144 | struct btrfs_key *location) | |
1145 | { | |
1146 | struct inode *inode; | |
1147 | struct inode *dir; | |
1148 | int ret; | |
1149 | ||
1150 | inode = read_one_inode(root, location->objectid); | |
1151 | if (!inode) | |
1152 | return -ENOENT; | |
1153 | ||
1154 | dir = read_one_inode(root, dirid); | |
1155 | if (!dir) { | |
1156 | iput(inode); | |
1157 | return -EIO; | |
1158 | } | |
1159 | ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index); | |
1160 | ||
1161 | /* FIXME, put inode into FIXUP list */ | |
1162 | ||
1163 | iput(inode); | |
1164 | iput(dir); | |
1165 | return ret; | |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | * take a single entry in a log directory item and replay it into | |
1170 | * the subvolume. | |
1171 | * | |
1172 | * if a conflicting item exists in the subdirectory already, | |
1173 | * the inode it points to is unlinked and put into the link count | |
1174 | * fix up tree. | |
1175 | * | |
1176 | * If a name from the log points to a file or directory that does | |
1177 | * not exist in the FS, it is skipped. fsyncs on directories | |
1178 | * do not force down inodes inside that directory, just changes to the | |
1179 | * names or unlinks in a directory. | |
1180 | */ | |
1181 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |
1182 | struct btrfs_root *root, | |
1183 | struct btrfs_path *path, | |
1184 | struct extent_buffer *eb, | |
1185 | struct btrfs_dir_item *di, | |
1186 | struct btrfs_key *key) | |
1187 | { | |
1188 | char *name; | |
1189 | int name_len; | |
1190 | struct btrfs_dir_item *dst_di; | |
1191 | struct btrfs_key found_key; | |
1192 | struct btrfs_key log_key; | |
1193 | struct inode *dir; | |
e02119d5 | 1194 | u8 log_type; |
4bef0848 | 1195 | int exists; |
e02119d5 CM |
1196 | int ret; |
1197 | ||
1198 | dir = read_one_inode(root, key->objectid); | |
1199 | BUG_ON(!dir); | |
1200 | ||
1201 | name_len = btrfs_dir_name_len(eb, di); | |
1202 | name = kmalloc(name_len, GFP_NOFS); | |
1203 | log_type = btrfs_dir_type(eb, di); | |
1204 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1205 | name_len); | |
1206 | ||
1207 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); | |
4bef0848 CM |
1208 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
1209 | if (exists == 0) | |
1210 | exists = 1; | |
1211 | else | |
1212 | exists = 0; | |
1213 | btrfs_release_path(root, path); | |
1214 | ||
e02119d5 CM |
1215 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
1216 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, | |
1217 | name, name_len, 1); | |
1218 | } | |
1219 | else if (key->type == BTRFS_DIR_INDEX_KEY) { | |
1220 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, | |
1221 | key->objectid, | |
1222 | key->offset, name, | |
1223 | name_len, 1); | |
1224 | } else { | |
1225 | BUG(); | |
1226 | } | |
1227 | if (!dst_di || IS_ERR(dst_di)) { | |
1228 | /* we need a sequence number to insert, so we only | |
1229 | * do inserts for the BTRFS_DIR_INDEX_KEY types | |
1230 | */ | |
1231 | if (key->type != BTRFS_DIR_INDEX_KEY) | |
1232 | goto out; | |
1233 | goto insert; | |
1234 | } | |
1235 | ||
1236 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | |
1237 | /* the existing item matches the logged item */ | |
1238 | if (found_key.objectid == log_key.objectid && | |
1239 | found_key.type == log_key.type && | |
1240 | found_key.offset == log_key.offset && | |
1241 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { | |
1242 | goto out; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * don't drop the conflicting directory entry if the inode | |
1247 | * for the new entry doesn't exist | |
1248 | */ | |
4bef0848 | 1249 | if (!exists) |
e02119d5 CM |
1250 | goto out; |
1251 | ||
e02119d5 CM |
1252 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); |
1253 | BUG_ON(ret); | |
1254 | ||
1255 | if (key->type == BTRFS_DIR_INDEX_KEY) | |
1256 | goto insert; | |
1257 | out: | |
1258 | btrfs_release_path(root, path); | |
1259 | kfree(name); | |
1260 | iput(dir); | |
1261 | return 0; | |
1262 | ||
1263 | insert: | |
1264 | btrfs_release_path(root, path); | |
1265 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, | |
1266 | name, name_len, log_type, &log_key); | |
1267 | ||
1268 | if (ret && ret != -ENOENT) | |
1269 | BUG(); | |
1270 | goto out; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * find all the names in a directory item and reconcile them into | |
1275 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than | |
1276 | * one name in a directory item, but the same code gets used for | |
1277 | * both directory index types | |
1278 | */ | |
1279 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | |
1280 | struct btrfs_root *root, | |
1281 | struct btrfs_path *path, | |
1282 | struct extent_buffer *eb, int slot, | |
1283 | struct btrfs_key *key) | |
1284 | { | |
1285 | int ret; | |
1286 | u32 item_size = btrfs_item_size_nr(eb, slot); | |
1287 | struct btrfs_dir_item *di; | |
1288 | int name_len; | |
1289 | unsigned long ptr; | |
1290 | unsigned long ptr_end; | |
1291 | ||
1292 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1293 | ptr_end = ptr + item_size; | |
1294 | while(ptr < ptr_end) { | |
1295 | di = (struct btrfs_dir_item *)ptr; | |
1296 | name_len = btrfs_dir_name_len(eb, di); | |
1297 | ret = replay_one_name(trans, root, path, eb, di, key); | |
1298 | BUG_ON(ret); | |
1299 | ptr = (unsigned long)(di + 1); | |
1300 | ptr += name_len; | |
1301 | } | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * directory replay has two parts. There are the standard directory | |
1307 | * items in the log copied from the subvolume, and range items | |
1308 | * created in the log while the subvolume was logged. | |
1309 | * | |
1310 | * The range items tell us which parts of the key space the log | |
1311 | * is authoritative for. During replay, if a key in the subvolume | |
1312 | * directory is in a logged range item, but not actually in the log | |
1313 | * that means it was deleted from the directory before the fsync | |
1314 | * and should be removed. | |
1315 | */ | |
1316 | static noinline int find_dir_range(struct btrfs_root *root, | |
1317 | struct btrfs_path *path, | |
1318 | u64 dirid, int key_type, | |
1319 | u64 *start_ret, u64 *end_ret) | |
1320 | { | |
1321 | struct btrfs_key key; | |
1322 | u64 found_end; | |
1323 | struct btrfs_dir_log_item *item; | |
1324 | int ret; | |
1325 | int nritems; | |
1326 | ||
1327 | if (*start_ret == (u64)-1) | |
1328 | return 1; | |
1329 | ||
1330 | key.objectid = dirid; | |
1331 | key.type = key_type; | |
1332 | key.offset = *start_ret; | |
1333 | ||
1334 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1335 | if (ret < 0) | |
1336 | goto out; | |
1337 | if (ret > 0) { | |
1338 | if (path->slots[0] == 0) | |
1339 | goto out; | |
1340 | path->slots[0]--; | |
1341 | } | |
1342 | if (ret != 0) | |
1343 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1344 | ||
1345 | if (key.type != key_type || key.objectid != dirid) { | |
1346 | ret = 1; | |
1347 | goto next; | |
1348 | } | |
1349 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1350 | struct btrfs_dir_log_item); | |
1351 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1352 | ||
1353 | if (*start_ret >= key.offset && *start_ret <= found_end) { | |
1354 | ret = 0; | |
1355 | *start_ret = key.offset; | |
1356 | *end_ret = found_end; | |
1357 | goto out; | |
1358 | } | |
1359 | ret = 1; | |
1360 | next: | |
1361 | /* check the next slot in the tree to see if it is a valid item */ | |
1362 | nritems = btrfs_header_nritems(path->nodes[0]); | |
1363 | if (path->slots[0] >= nritems) { | |
1364 | ret = btrfs_next_leaf(root, path); | |
1365 | if (ret) | |
1366 | goto out; | |
1367 | } else { | |
1368 | path->slots[0]++; | |
1369 | } | |
1370 | ||
1371 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1372 | ||
1373 | if (key.type != key_type || key.objectid != dirid) { | |
1374 | ret = 1; | |
1375 | goto out; | |
1376 | } | |
1377 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1378 | struct btrfs_dir_log_item); | |
1379 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1380 | *start_ret = key.offset; | |
1381 | *end_ret = found_end; | |
1382 | ret = 0; | |
1383 | out: | |
1384 | btrfs_release_path(root, path); | |
1385 | return ret; | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * this looks for a given directory item in the log. If the directory | |
1390 | * item is not in the log, the item is removed and the inode it points | |
1391 | * to is unlinked | |
1392 | */ | |
1393 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | |
1394 | struct btrfs_root *root, | |
1395 | struct btrfs_root *log, | |
1396 | struct btrfs_path *path, | |
1397 | struct btrfs_path *log_path, | |
1398 | struct inode *dir, | |
1399 | struct btrfs_key *dir_key) | |
1400 | { | |
1401 | int ret; | |
1402 | struct extent_buffer *eb; | |
1403 | int slot; | |
1404 | u32 item_size; | |
1405 | struct btrfs_dir_item *di; | |
1406 | struct btrfs_dir_item *log_di; | |
1407 | int name_len; | |
1408 | unsigned long ptr; | |
1409 | unsigned long ptr_end; | |
1410 | char *name; | |
1411 | struct inode *inode; | |
1412 | struct btrfs_key location; | |
1413 | ||
1414 | again: | |
1415 | eb = path->nodes[0]; | |
1416 | slot = path->slots[0]; | |
1417 | item_size = btrfs_item_size_nr(eb, slot); | |
1418 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1419 | ptr_end = ptr + item_size; | |
1420 | while(ptr < ptr_end) { | |
1421 | di = (struct btrfs_dir_item *)ptr; | |
1422 | name_len = btrfs_dir_name_len(eb, di); | |
1423 | name = kmalloc(name_len, GFP_NOFS); | |
1424 | if (!name) { | |
1425 | ret = -ENOMEM; | |
1426 | goto out; | |
1427 | } | |
1428 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1429 | name_len); | |
1430 | log_di = NULL; | |
1431 | if (dir_key->type == BTRFS_DIR_ITEM_KEY) { | |
1432 | log_di = btrfs_lookup_dir_item(trans, log, log_path, | |
1433 | dir_key->objectid, | |
1434 | name, name_len, 0); | |
1435 | } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) { | |
1436 | log_di = btrfs_lookup_dir_index_item(trans, log, | |
1437 | log_path, | |
1438 | dir_key->objectid, | |
1439 | dir_key->offset, | |
1440 | name, name_len, 0); | |
1441 | } | |
1442 | if (!log_di || IS_ERR(log_di)) { | |
1443 | btrfs_dir_item_key_to_cpu(eb, di, &location); | |
1444 | btrfs_release_path(root, path); | |
1445 | btrfs_release_path(log, log_path); | |
1446 | inode = read_one_inode(root, location.objectid); | |
1447 | BUG_ON(!inode); | |
1448 | ||
1449 | ret = link_to_fixup_dir(trans, root, | |
1450 | path, location.objectid); | |
1451 | BUG_ON(ret); | |
1452 | btrfs_inc_nlink(inode); | |
1453 | ret = btrfs_unlink_inode(trans, root, dir, inode, | |
1454 | name, name_len); | |
1455 | BUG_ON(ret); | |
1456 | kfree(name); | |
1457 | iput(inode); | |
1458 | ||
1459 | /* there might still be more names under this key | |
1460 | * check and repeat if required | |
1461 | */ | |
1462 | ret = btrfs_search_slot(NULL, root, dir_key, path, | |
1463 | 0, 0); | |
1464 | if (ret == 0) | |
1465 | goto again; | |
1466 | ret = 0; | |
1467 | goto out; | |
1468 | } | |
1469 | btrfs_release_path(log, log_path); | |
1470 | kfree(name); | |
1471 | ||
1472 | ptr = (unsigned long)(di + 1); | |
1473 | ptr += name_len; | |
1474 | } | |
1475 | ret = 0; | |
1476 | out: | |
1477 | btrfs_release_path(root, path); | |
1478 | btrfs_release_path(log, log_path); | |
1479 | return ret; | |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * deletion replay happens before we copy any new directory items | |
1484 | * out of the log or out of backreferences from inodes. It | |
1485 | * scans the log to find ranges of keys that log is authoritative for, | |
1486 | * and then scans the directory to find items in those ranges that are | |
1487 | * not present in the log. | |
1488 | * | |
1489 | * Anything we don't find in the log is unlinked and removed from the | |
1490 | * directory. | |
1491 | */ | |
1492 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | |
1493 | struct btrfs_root *root, | |
1494 | struct btrfs_root *log, | |
1495 | struct btrfs_path *path, | |
1496 | u64 dirid) | |
1497 | { | |
1498 | u64 range_start; | |
1499 | u64 range_end; | |
1500 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; | |
1501 | int ret = 0; | |
1502 | struct btrfs_key dir_key; | |
1503 | struct btrfs_key found_key; | |
1504 | struct btrfs_path *log_path; | |
1505 | struct inode *dir; | |
1506 | ||
1507 | dir_key.objectid = dirid; | |
1508 | dir_key.type = BTRFS_DIR_ITEM_KEY; | |
1509 | log_path = btrfs_alloc_path(); | |
1510 | if (!log_path) | |
1511 | return -ENOMEM; | |
1512 | ||
1513 | dir = read_one_inode(root, dirid); | |
1514 | /* it isn't an error if the inode isn't there, that can happen | |
1515 | * because we replay the deletes before we copy in the inode item | |
1516 | * from the log | |
1517 | */ | |
1518 | if (!dir) { | |
1519 | btrfs_free_path(log_path); | |
1520 | return 0; | |
1521 | } | |
1522 | again: | |
1523 | range_start = 0; | |
1524 | range_end = 0; | |
1525 | while(1) { | |
1526 | ret = find_dir_range(log, path, dirid, key_type, | |
1527 | &range_start, &range_end); | |
1528 | if (ret != 0) | |
1529 | break; | |
1530 | ||
1531 | dir_key.offset = range_start; | |
1532 | while(1) { | |
1533 | int nritems; | |
1534 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | |
1535 | 0, 0); | |
1536 | if (ret < 0) | |
1537 | goto out; | |
1538 | ||
1539 | nritems = btrfs_header_nritems(path->nodes[0]); | |
1540 | if (path->slots[0] >= nritems) { | |
1541 | ret = btrfs_next_leaf(root, path); | |
1542 | if (ret) | |
1543 | break; | |
1544 | } | |
1545 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1546 | path->slots[0]); | |
1547 | if (found_key.objectid != dirid || | |
1548 | found_key.type != dir_key.type) | |
1549 | goto next_type; | |
1550 | ||
1551 | if (found_key.offset > range_end) | |
1552 | break; | |
1553 | ||
1554 | ret = check_item_in_log(trans, root, log, path, | |
1555 | log_path, dir, &found_key); | |
1556 | BUG_ON(ret); | |
1557 | if (found_key.offset == (u64)-1) | |
1558 | break; | |
1559 | dir_key.offset = found_key.offset + 1; | |
1560 | } | |
1561 | btrfs_release_path(root, path); | |
1562 | if (range_end == (u64)-1) | |
1563 | break; | |
1564 | range_start = range_end + 1; | |
1565 | } | |
1566 | ||
1567 | next_type: | |
1568 | ret = 0; | |
1569 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { | |
1570 | key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
1571 | dir_key.type = BTRFS_DIR_INDEX_KEY; | |
1572 | btrfs_release_path(root, path); | |
1573 | goto again; | |
1574 | } | |
1575 | out: | |
1576 | btrfs_release_path(root, path); | |
1577 | btrfs_free_path(log_path); | |
1578 | iput(dir); | |
1579 | return ret; | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * the process_func used to replay items from the log tree. This | |
1584 | * gets called in two different stages. The first stage just looks | |
1585 | * for inodes and makes sure they are all copied into the subvolume. | |
1586 | * | |
1587 | * The second stage copies all the other item types from the log into | |
1588 | * the subvolume. The two stage approach is slower, but gets rid of | |
1589 | * lots of complexity around inodes referencing other inodes that exist | |
1590 | * only in the log (references come from either directory items or inode | |
1591 | * back refs). | |
1592 | */ | |
1593 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |
1594 | struct walk_control *wc, u64 gen) | |
1595 | { | |
1596 | int nritems; | |
1597 | struct btrfs_path *path; | |
1598 | struct btrfs_root *root = wc->replay_dest; | |
1599 | struct btrfs_key key; | |
1600 | u32 item_size; | |
1601 | int level; | |
1602 | int i; | |
1603 | int ret; | |
1604 | ||
1605 | btrfs_read_buffer(eb, gen); | |
1606 | ||
1607 | level = btrfs_header_level(eb); | |
1608 | ||
1609 | if (level != 0) | |
1610 | return 0; | |
1611 | ||
1612 | path = btrfs_alloc_path(); | |
1613 | BUG_ON(!path); | |
1614 | ||
1615 | nritems = btrfs_header_nritems(eb); | |
1616 | for (i = 0; i < nritems; i++) { | |
1617 | btrfs_item_key_to_cpu(eb, &key, i); | |
1618 | item_size = btrfs_item_size_nr(eb, i); | |
1619 | ||
1620 | /* inode keys are done during the first stage */ | |
1621 | if (key.type == BTRFS_INODE_ITEM_KEY && | |
1622 | wc->stage == LOG_WALK_REPLAY_INODES) { | |
1623 | struct inode *inode; | |
1624 | struct btrfs_inode_item *inode_item; | |
1625 | u32 mode; | |
1626 | ||
1627 | inode_item = btrfs_item_ptr(eb, i, | |
1628 | struct btrfs_inode_item); | |
1629 | mode = btrfs_inode_mode(eb, inode_item); | |
1630 | if (S_ISDIR(mode)) { | |
1631 | ret = replay_dir_deletes(wc->trans, | |
1632 | root, log, path, key.objectid); | |
1633 | BUG_ON(ret); | |
1634 | } | |
1635 | ret = overwrite_item(wc->trans, root, path, | |
1636 | eb, i, &key); | |
1637 | BUG_ON(ret); | |
1638 | ||
1639 | /* for regular files, truncate away | |
1640 | * extents past the new EOF | |
1641 | */ | |
1642 | if (S_ISREG(mode)) { | |
1643 | inode = read_one_inode(root, | |
1644 | key.objectid); | |
1645 | BUG_ON(!inode); | |
1646 | ||
1647 | ret = btrfs_truncate_inode_items(wc->trans, | |
1648 | root, inode, inode->i_size, | |
1649 | BTRFS_EXTENT_DATA_KEY); | |
1650 | BUG_ON(ret); | |
1651 | iput(inode); | |
1652 | } | |
1653 | ret = link_to_fixup_dir(wc->trans, root, | |
1654 | path, key.objectid); | |
1655 | BUG_ON(ret); | |
1656 | } | |
1657 | if (wc->stage < LOG_WALK_REPLAY_ALL) | |
1658 | continue; | |
1659 | ||
1660 | /* these keys are simply copied */ | |
1661 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | |
1662 | ret = overwrite_item(wc->trans, root, path, | |
1663 | eb, i, &key); | |
1664 | BUG_ON(ret); | |
1665 | } else if (key.type == BTRFS_INODE_REF_KEY) { | |
1666 | ret = add_inode_ref(wc->trans, root, log, path, | |
1667 | eb, i, &key); | |
1668 | BUG_ON(ret && ret != -ENOENT); | |
1669 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { | |
1670 | ret = replay_one_extent(wc->trans, root, path, | |
1671 | eb, i, &key); | |
1672 | BUG_ON(ret); | |
1673 | } else if (key.type == BTRFS_CSUM_ITEM_KEY) { | |
1674 | ret = replay_one_csum(wc->trans, root, path, | |
1675 | eb, i, &key); | |
1676 | BUG_ON(ret); | |
1677 | } else if (key.type == BTRFS_DIR_ITEM_KEY || | |
1678 | key.type == BTRFS_DIR_INDEX_KEY) { | |
1679 | ret = replay_one_dir_item(wc->trans, root, path, | |
1680 | eb, i, &key); | |
1681 | BUG_ON(ret); | |
1682 | } | |
1683 | } | |
1684 | btrfs_free_path(path); | |
1685 | return 0; | |
1686 | } | |
1687 | ||
1688 | static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans, | |
1689 | struct btrfs_root *root, | |
1690 | struct btrfs_path *path, int *level, | |
1691 | struct walk_control *wc) | |
1692 | { | |
1693 | u64 root_owner; | |
1694 | u64 root_gen; | |
1695 | u64 bytenr; | |
1696 | u64 ptr_gen; | |
1697 | struct extent_buffer *next; | |
1698 | struct extent_buffer *cur; | |
1699 | struct extent_buffer *parent; | |
1700 | u32 blocksize; | |
1701 | int ret = 0; | |
1702 | ||
1703 | WARN_ON(*level < 0); | |
1704 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1705 | ||
1706 | while(*level > 0) { | |
1707 | WARN_ON(*level < 0); | |
1708 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1709 | cur = path->nodes[*level]; | |
1710 | ||
1711 | if (btrfs_header_level(cur) != *level) | |
1712 | WARN_ON(1); | |
1713 | ||
1714 | if (path->slots[*level] >= | |
1715 | btrfs_header_nritems(cur)) | |
1716 | break; | |
1717 | ||
1718 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
1719 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
1720 | blocksize = btrfs_level_size(root, *level - 1); | |
1721 | ||
1722 | parent = path->nodes[*level]; | |
1723 | root_owner = btrfs_header_owner(parent); | |
1724 | root_gen = btrfs_header_generation(parent); | |
1725 | ||
1726 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
1727 | ||
1728 | wc->process_func(root, next, wc, ptr_gen); | |
1729 | ||
1730 | if (*level == 1) { | |
1731 | path->slots[*level]++; | |
1732 | if (wc->free) { | |
1733 | btrfs_read_buffer(next, ptr_gen); | |
1734 | ||
1735 | btrfs_tree_lock(next); | |
1736 | clean_tree_block(trans, root, next); | |
1737 | btrfs_wait_tree_block_writeback(next); | |
1738 | btrfs_tree_unlock(next); | |
1739 | ||
1740 | ret = btrfs_drop_leaf_ref(trans, root, next); | |
1741 | BUG_ON(ret); | |
1742 | ||
1743 | WARN_ON(root_owner != | |
1744 | BTRFS_TREE_LOG_OBJECTID); | |
d00aff00 CM |
1745 | ret = btrfs_free_reserved_extent(root, |
1746 | bytenr, blocksize); | |
e02119d5 CM |
1747 | BUG_ON(ret); |
1748 | } | |
1749 | free_extent_buffer(next); | |
1750 | continue; | |
1751 | } | |
1752 | btrfs_read_buffer(next, ptr_gen); | |
1753 | ||
1754 | WARN_ON(*level <= 0); | |
1755 | if (path->nodes[*level-1]) | |
1756 | free_extent_buffer(path->nodes[*level-1]); | |
1757 | path->nodes[*level-1] = next; | |
1758 | *level = btrfs_header_level(next); | |
1759 | path->slots[*level] = 0; | |
1760 | cond_resched(); | |
1761 | } | |
1762 | WARN_ON(*level < 0); | |
1763 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1764 | ||
1765 | if (path->nodes[*level] == root->node) { | |
1766 | parent = path->nodes[*level]; | |
1767 | } else { | |
1768 | parent = path->nodes[*level + 1]; | |
1769 | } | |
1770 | bytenr = path->nodes[*level]->start; | |
1771 | ||
1772 | blocksize = btrfs_level_size(root, *level); | |
1773 | root_owner = btrfs_header_owner(parent); | |
1774 | root_gen = btrfs_header_generation(parent); | |
1775 | ||
1776 | wc->process_func(root, path->nodes[*level], wc, | |
1777 | btrfs_header_generation(path->nodes[*level])); | |
1778 | ||
1779 | if (wc->free) { | |
1780 | next = path->nodes[*level]; | |
1781 | btrfs_tree_lock(next); | |
1782 | clean_tree_block(trans, root, next); | |
1783 | btrfs_wait_tree_block_writeback(next); | |
1784 | btrfs_tree_unlock(next); | |
1785 | ||
1786 | if (*level == 0) { | |
1787 | ret = btrfs_drop_leaf_ref(trans, root, next); | |
1788 | BUG_ON(ret); | |
1789 | } | |
1790 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); | |
d00aff00 | 1791 | ret = btrfs_free_reserved_extent(root, bytenr, blocksize); |
e02119d5 CM |
1792 | BUG_ON(ret); |
1793 | } | |
1794 | free_extent_buffer(path->nodes[*level]); | |
1795 | path->nodes[*level] = NULL; | |
1796 | *level += 1; | |
1797 | ||
1798 | cond_resched(); | |
1799 | return 0; | |
1800 | } | |
1801 | ||
1802 | static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans, | |
1803 | struct btrfs_root *root, | |
1804 | struct btrfs_path *path, int *level, | |
1805 | struct walk_control *wc) | |
1806 | { | |
1807 | u64 root_owner; | |
1808 | u64 root_gen; | |
1809 | int i; | |
1810 | int slot; | |
1811 | int ret; | |
1812 | ||
1813 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { | |
1814 | slot = path->slots[i]; | |
1815 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { | |
1816 | struct extent_buffer *node; | |
1817 | node = path->nodes[i]; | |
1818 | path->slots[i]++; | |
1819 | *level = i; | |
1820 | WARN_ON(*level == 0); | |
1821 | return 0; | |
1822 | } else { | |
31840ae1 ZY |
1823 | struct extent_buffer *parent; |
1824 | if (path->nodes[*level] == root->node) | |
1825 | parent = path->nodes[*level]; | |
1826 | else | |
1827 | parent = path->nodes[*level + 1]; | |
1828 | ||
1829 | root_owner = btrfs_header_owner(parent); | |
1830 | root_gen = btrfs_header_generation(parent); | |
e02119d5 CM |
1831 | wc->process_func(root, path->nodes[*level], wc, |
1832 | btrfs_header_generation(path->nodes[*level])); | |
1833 | if (wc->free) { | |
1834 | struct extent_buffer *next; | |
1835 | ||
1836 | next = path->nodes[*level]; | |
1837 | ||
1838 | btrfs_tree_lock(next); | |
1839 | clean_tree_block(trans, root, next); | |
1840 | btrfs_wait_tree_block_writeback(next); | |
1841 | btrfs_tree_unlock(next); | |
1842 | ||
1843 | if (*level == 0) { | |
1844 | ret = btrfs_drop_leaf_ref(trans, root, | |
1845 | next); | |
1846 | BUG_ON(ret); | |
1847 | } | |
1848 | ||
1849 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); | |
d00aff00 | 1850 | ret = btrfs_free_reserved_extent(root, |
e02119d5 | 1851 | path->nodes[*level]->start, |
d00aff00 | 1852 | path->nodes[*level]->len); |
e02119d5 CM |
1853 | BUG_ON(ret); |
1854 | } | |
1855 | free_extent_buffer(path->nodes[*level]); | |
1856 | path->nodes[*level] = NULL; | |
1857 | *level = i + 1; | |
1858 | } | |
1859 | } | |
1860 | return 1; | |
1861 | } | |
1862 | ||
1863 | /* | |
1864 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
1865 | * the tree freeing any blocks that have a ref count of zero after being | |
1866 | * decremented. | |
1867 | */ | |
1868 | static int walk_log_tree(struct btrfs_trans_handle *trans, | |
1869 | struct btrfs_root *log, struct walk_control *wc) | |
1870 | { | |
1871 | int ret = 0; | |
1872 | int wret; | |
1873 | int level; | |
1874 | struct btrfs_path *path; | |
1875 | int i; | |
1876 | int orig_level; | |
1877 | ||
1878 | path = btrfs_alloc_path(); | |
1879 | BUG_ON(!path); | |
1880 | ||
1881 | level = btrfs_header_level(log->node); | |
1882 | orig_level = level; | |
1883 | path->nodes[level] = log->node; | |
1884 | extent_buffer_get(log->node); | |
1885 | path->slots[level] = 0; | |
1886 | ||
1887 | while(1) { | |
1888 | wret = walk_down_log_tree(trans, log, path, &level, wc); | |
1889 | if (wret > 0) | |
1890 | break; | |
1891 | if (wret < 0) | |
1892 | ret = wret; | |
1893 | ||
1894 | wret = walk_up_log_tree(trans, log, path, &level, wc); | |
1895 | if (wret > 0) | |
1896 | break; | |
1897 | if (wret < 0) | |
1898 | ret = wret; | |
1899 | } | |
1900 | ||
1901 | /* was the root node processed? if not, catch it here */ | |
1902 | if (path->nodes[orig_level]) { | |
1903 | wc->process_func(log, path->nodes[orig_level], wc, | |
1904 | btrfs_header_generation(path->nodes[orig_level])); | |
1905 | if (wc->free) { | |
1906 | struct extent_buffer *next; | |
1907 | ||
1908 | next = path->nodes[orig_level]; | |
1909 | ||
1910 | btrfs_tree_lock(next); | |
1911 | clean_tree_block(trans, log, next); | |
1912 | btrfs_wait_tree_block_writeback(next); | |
1913 | btrfs_tree_unlock(next); | |
1914 | ||
1915 | if (orig_level == 0) { | |
1916 | ret = btrfs_drop_leaf_ref(trans, log, | |
1917 | next); | |
1918 | BUG_ON(ret); | |
1919 | } | |
1920 | WARN_ON(log->root_key.objectid != | |
1921 | BTRFS_TREE_LOG_OBJECTID); | |
d00aff00 CM |
1922 | ret = btrfs_free_reserved_extent(log, next->start, |
1923 | next->len); | |
e02119d5 CM |
1924 | BUG_ON(ret); |
1925 | } | |
1926 | } | |
1927 | ||
1928 | for (i = 0; i <= orig_level; i++) { | |
1929 | if (path->nodes[i]) { | |
1930 | free_extent_buffer(path->nodes[i]); | |
1931 | path->nodes[i] = NULL; | |
1932 | } | |
1933 | } | |
1934 | btrfs_free_path(path); | |
1935 | if (wc->free) | |
1936 | free_extent_buffer(log->node); | |
1937 | return ret; | |
1938 | } | |
1939 | ||
b2950863 | 1940 | static int wait_log_commit(struct btrfs_root *log) |
e02119d5 CM |
1941 | { |
1942 | DEFINE_WAIT(wait); | |
1943 | u64 transid = log->fs_info->tree_log_transid; | |
1944 | ||
1945 | do { | |
1946 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, | |
1947 | TASK_UNINTERRUPTIBLE); | |
1948 | mutex_unlock(&log->fs_info->tree_log_mutex); | |
1949 | if (atomic_read(&log->fs_info->tree_log_commit)) | |
1950 | schedule(); | |
1951 | finish_wait(&log->fs_info->tree_log_wait, &wait); | |
1952 | mutex_lock(&log->fs_info->tree_log_mutex); | |
1953 | } while(transid == log->fs_info->tree_log_transid && | |
1954 | atomic_read(&log->fs_info->tree_log_commit)); | |
1955 | return 0; | |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * btrfs_sync_log does sends a given tree log down to the disk and | |
1960 | * updates the super blocks to record it. When this call is done, | |
1961 | * you know that any inodes previously logged are safely on disk | |
1962 | */ | |
1963 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | |
1964 | struct btrfs_root *root) | |
1965 | { | |
1966 | int ret; | |
1967 | unsigned long batch; | |
1968 | struct btrfs_root *log = root->log_root; | |
e02119d5 CM |
1969 | |
1970 | mutex_lock(&log->fs_info->tree_log_mutex); | |
1971 | if (atomic_read(&log->fs_info->tree_log_commit)) { | |
1972 | wait_log_commit(log); | |
1973 | goto out; | |
1974 | } | |
1975 | atomic_set(&log->fs_info->tree_log_commit, 1); | |
1976 | ||
1977 | while(1) { | |
49eb7e46 | 1978 | batch = log->fs_info->tree_log_batch; |
e02119d5 CM |
1979 | mutex_unlock(&log->fs_info->tree_log_mutex); |
1980 | schedule_timeout_uninterruptible(1); | |
1981 | mutex_lock(&log->fs_info->tree_log_mutex); | |
e02119d5 CM |
1982 | |
1983 | while(atomic_read(&log->fs_info->tree_log_writers)) { | |
1984 | DEFINE_WAIT(wait); | |
1985 | prepare_to_wait(&log->fs_info->tree_log_wait, &wait, | |
1986 | TASK_UNINTERRUPTIBLE); | |
e02119d5 CM |
1987 | mutex_unlock(&log->fs_info->tree_log_mutex); |
1988 | if (atomic_read(&log->fs_info->tree_log_writers)) | |
1989 | schedule(); | |
1990 | mutex_lock(&log->fs_info->tree_log_mutex); | |
1991 | finish_wait(&log->fs_info->tree_log_wait, &wait); | |
1992 | } | |
1993 | if (batch == log->fs_info->tree_log_batch) | |
1994 | break; | |
1995 | } | |
e02119d5 | 1996 | |
d0c803c4 | 1997 | ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages); |
e02119d5 | 1998 | BUG_ON(ret); |
d0c803c4 CM |
1999 | ret = btrfs_write_and_wait_marked_extents(root->fs_info->log_root_tree, |
2000 | &root->fs_info->log_root_tree->dirty_log_pages); | |
e02119d5 CM |
2001 | BUG_ON(ret); |
2002 | ||
2003 | btrfs_set_super_log_root(&root->fs_info->super_for_commit, | |
2004 | log->fs_info->log_root_tree->node->start); | |
2005 | btrfs_set_super_log_root_level(&root->fs_info->super_for_commit, | |
2006 | btrfs_header_level(log->fs_info->log_root_tree->node)); | |
2007 | ||
2008 | write_ctree_super(trans, log->fs_info->tree_root); | |
2009 | log->fs_info->tree_log_transid++; | |
2010 | log->fs_info->tree_log_batch = 0; | |
2011 | atomic_set(&log->fs_info->tree_log_commit, 0); | |
2012 | smp_mb(); | |
2013 | if (waitqueue_active(&log->fs_info->tree_log_wait)) | |
2014 | wake_up(&log->fs_info->tree_log_wait); | |
2015 | out: | |
2016 | mutex_unlock(&log->fs_info->tree_log_mutex); | |
2017 | return 0; | |
2018 | ||
2019 | } | |
2020 | ||
3a5f1d45 | 2021 | /* * free all the extents used by the tree log. This should be called |
e02119d5 CM |
2022 | * at commit time of the full transaction |
2023 | */ | |
2024 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | |
2025 | { | |
2026 | int ret; | |
2027 | struct btrfs_root *log; | |
2028 | struct key; | |
d0c803c4 CM |
2029 | u64 start; |
2030 | u64 end; | |
e02119d5 CM |
2031 | struct walk_control wc = { |
2032 | .free = 1, | |
2033 | .process_func = process_one_buffer | |
2034 | }; | |
2035 | ||
2036 | if (!root->log_root) | |
2037 | return 0; | |
2038 | ||
2039 | log = root->log_root; | |
2040 | ret = walk_log_tree(trans, log, &wc); | |
2041 | BUG_ON(ret); | |
2042 | ||
d0c803c4 CM |
2043 | while(1) { |
2044 | ret = find_first_extent_bit(&log->dirty_log_pages, | |
2045 | 0, &start, &end, EXTENT_DIRTY); | |
2046 | if (ret) | |
2047 | break; | |
2048 | ||
2049 | clear_extent_dirty(&log->dirty_log_pages, | |
2050 | start, end, GFP_NOFS); | |
2051 | } | |
2052 | ||
e02119d5 CM |
2053 | log = root->log_root; |
2054 | ret = btrfs_del_root(trans, root->fs_info->log_root_tree, | |
2055 | &log->root_key); | |
2056 | BUG_ON(ret); | |
2057 | root->log_root = NULL; | |
2058 | kfree(root->log_root); | |
2059 | return 0; | |
2060 | } | |
2061 | ||
2062 | /* | |
2063 | * helper function to update the item for a given subvolumes log root | |
2064 | * in the tree of log roots | |
2065 | */ | |
2066 | static int update_log_root(struct btrfs_trans_handle *trans, | |
2067 | struct btrfs_root *log) | |
2068 | { | |
2069 | u64 bytenr = btrfs_root_bytenr(&log->root_item); | |
2070 | int ret; | |
2071 | ||
2072 | if (log->node->start == bytenr) | |
2073 | return 0; | |
2074 | ||
2075 | btrfs_set_root_bytenr(&log->root_item, log->node->start); | |
84234f3a | 2076 | btrfs_set_root_generation(&log->root_item, trans->transid); |
e02119d5 CM |
2077 | btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node)); |
2078 | ret = btrfs_update_root(trans, log->fs_info->log_root_tree, | |
2079 | &log->root_key, &log->root_item); | |
2080 | BUG_ON(ret); | |
2081 | return ret; | |
2082 | } | |
2083 | ||
2084 | /* | |
2085 | * If both a file and directory are logged, and unlinks or renames are | |
2086 | * mixed in, we have a few interesting corners: | |
2087 | * | |
2088 | * create file X in dir Y | |
2089 | * link file X to X.link in dir Y | |
2090 | * fsync file X | |
2091 | * unlink file X but leave X.link | |
2092 | * fsync dir Y | |
2093 | * | |
2094 | * After a crash we would expect only X.link to exist. But file X | |
2095 | * didn't get fsync'd again so the log has back refs for X and X.link. | |
2096 | * | |
2097 | * We solve this by removing directory entries and inode backrefs from the | |
2098 | * log when a file that was logged in the current transaction is | |
2099 | * unlinked. Any later fsync will include the updated log entries, and | |
2100 | * we'll be able to reconstruct the proper directory items from backrefs. | |
2101 | * | |
2102 | * This optimizations allows us to avoid relogging the entire inode | |
2103 | * or the entire directory. | |
2104 | */ | |
2105 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | |
2106 | struct btrfs_root *root, | |
2107 | const char *name, int name_len, | |
2108 | struct inode *dir, u64 index) | |
2109 | { | |
2110 | struct btrfs_root *log; | |
2111 | struct btrfs_dir_item *di; | |
2112 | struct btrfs_path *path; | |
2113 | int ret; | |
2114 | int bytes_del = 0; | |
2115 | ||
3a5f1d45 CM |
2116 | if (BTRFS_I(dir)->logged_trans < trans->transid) |
2117 | return 0; | |
2118 | ||
e02119d5 CM |
2119 | ret = join_running_log_trans(root); |
2120 | if (ret) | |
2121 | return 0; | |
2122 | ||
2123 | mutex_lock(&BTRFS_I(dir)->log_mutex); | |
2124 | ||
2125 | log = root->log_root; | |
2126 | path = btrfs_alloc_path(); | |
2127 | di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino, | |
2128 | name, name_len, -1); | |
2129 | if (di && !IS_ERR(di)) { | |
2130 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | |
2131 | bytes_del += name_len; | |
2132 | BUG_ON(ret); | |
2133 | } | |
2134 | btrfs_release_path(log, path); | |
2135 | di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino, | |
2136 | index, name, name_len, -1); | |
2137 | if (di && !IS_ERR(di)) { | |
2138 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | |
2139 | bytes_del += name_len; | |
2140 | BUG_ON(ret); | |
2141 | } | |
2142 | ||
2143 | /* update the directory size in the log to reflect the names | |
2144 | * we have removed | |
2145 | */ | |
2146 | if (bytes_del) { | |
2147 | struct btrfs_key key; | |
2148 | ||
2149 | key.objectid = dir->i_ino; | |
2150 | key.offset = 0; | |
2151 | key.type = BTRFS_INODE_ITEM_KEY; | |
2152 | btrfs_release_path(log, path); | |
2153 | ||
2154 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); | |
2155 | if (ret == 0) { | |
2156 | struct btrfs_inode_item *item; | |
2157 | u64 i_size; | |
2158 | ||
2159 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2160 | struct btrfs_inode_item); | |
2161 | i_size = btrfs_inode_size(path->nodes[0], item); | |
2162 | if (i_size > bytes_del) | |
2163 | i_size -= bytes_del; | |
2164 | else | |
2165 | i_size = 0; | |
2166 | btrfs_set_inode_size(path->nodes[0], item, i_size); | |
2167 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2168 | } else | |
2169 | ret = 0; | |
2170 | btrfs_release_path(log, path); | |
2171 | } | |
2172 | ||
2173 | btrfs_free_path(path); | |
2174 | mutex_unlock(&BTRFS_I(dir)->log_mutex); | |
2175 | end_log_trans(root); | |
2176 | ||
2177 | return 0; | |
2178 | } | |
2179 | ||
2180 | /* see comments for btrfs_del_dir_entries_in_log */ | |
2181 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | |
2182 | struct btrfs_root *root, | |
2183 | const char *name, int name_len, | |
2184 | struct inode *inode, u64 dirid) | |
2185 | { | |
2186 | struct btrfs_root *log; | |
2187 | u64 index; | |
2188 | int ret; | |
2189 | ||
3a5f1d45 CM |
2190 | if (BTRFS_I(inode)->logged_trans < trans->transid) |
2191 | return 0; | |
2192 | ||
e02119d5 CM |
2193 | ret = join_running_log_trans(root); |
2194 | if (ret) | |
2195 | return 0; | |
2196 | log = root->log_root; | |
2197 | mutex_lock(&BTRFS_I(inode)->log_mutex); | |
2198 | ||
2199 | ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino, | |
2200 | dirid, &index); | |
2201 | mutex_unlock(&BTRFS_I(inode)->log_mutex); | |
2202 | end_log_trans(root); | |
2203 | ||
e02119d5 CM |
2204 | return ret; |
2205 | } | |
2206 | ||
2207 | /* | |
2208 | * creates a range item in the log for 'dirid'. first_offset and | |
2209 | * last_offset tell us which parts of the key space the log should | |
2210 | * be considered authoritative for. | |
2211 | */ | |
2212 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | |
2213 | struct btrfs_root *log, | |
2214 | struct btrfs_path *path, | |
2215 | int key_type, u64 dirid, | |
2216 | u64 first_offset, u64 last_offset) | |
2217 | { | |
2218 | int ret; | |
2219 | struct btrfs_key key; | |
2220 | struct btrfs_dir_log_item *item; | |
2221 | ||
2222 | key.objectid = dirid; | |
2223 | key.offset = first_offset; | |
2224 | if (key_type == BTRFS_DIR_ITEM_KEY) | |
2225 | key.type = BTRFS_DIR_LOG_ITEM_KEY; | |
2226 | else | |
2227 | key.type = BTRFS_DIR_LOG_INDEX_KEY; | |
2228 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); | |
2229 | BUG_ON(ret); | |
2230 | ||
2231 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2232 | struct btrfs_dir_log_item); | |
2233 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); | |
2234 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2235 | btrfs_release_path(log, path); | |
2236 | return 0; | |
2237 | } | |
2238 | ||
2239 | /* | |
2240 | * log all the items included in the current transaction for a given | |
2241 | * directory. This also creates the range items in the log tree required | |
2242 | * to replay anything deleted before the fsync | |
2243 | */ | |
2244 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | |
2245 | struct btrfs_root *root, struct inode *inode, | |
2246 | struct btrfs_path *path, | |
2247 | struct btrfs_path *dst_path, int key_type, | |
2248 | u64 min_offset, u64 *last_offset_ret) | |
2249 | { | |
2250 | struct btrfs_key min_key; | |
2251 | struct btrfs_key max_key; | |
2252 | struct btrfs_root *log = root->log_root; | |
2253 | struct extent_buffer *src; | |
2254 | int ret; | |
2255 | int i; | |
2256 | int nritems; | |
2257 | u64 first_offset = min_offset; | |
2258 | u64 last_offset = (u64)-1; | |
2259 | ||
2260 | log = root->log_root; | |
2261 | max_key.objectid = inode->i_ino; | |
2262 | max_key.offset = (u64)-1; | |
2263 | max_key.type = key_type; | |
2264 | ||
2265 | min_key.objectid = inode->i_ino; | |
2266 | min_key.type = key_type; | |
2267 | min_key.offset = min_offset; | |
2268 | ||
2269 | path->keep_locks = 1; | |
2270 | ||
2271 | ret = btrfs_search_forward(root, &min_key, &max_key, | |
2272 | path, 0, trans->transid); | |
2273 | ||
2274 | /* | |
2275 | * we didn't find anything from this transaction, see if there | |
2276 | * is anything at all | |
2277 | */ | |
2278 | if (ret != 0 || min_key.objectid != inode->i_ino || | |
2279 | min_key.type != key_type) { | |
2280 | min_key.objectid = inode->i_ino; | |
2281 | min_key.type = key_type; | |
2282 | min_key.offset = (u64)-1; | |
2283 | btrfs_release_path(root, path); | |
2284 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | |
2285 | if (ret < 0) { | |
2286 | btrfs_release_path(root, path); | |
2287 | return ret; | |
2288 | } | |
2289 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); | |
2290 | ||
2291 | /* if ret == 0 there are items for this type, | |
2292 | * create a range to tell us the last key of this type. | |
2293 | * otherwise, there are no items in this directory after | |
2294 | * *min_offset, and we create a range to indicate that. | |
2295 | */ | |
2296 | if (ret == 0) { | |
2297 | struct btrfs_key tmp; | |
2298 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, | |
2299 | path->slots[0]); | |
2300 | if (key_type == tmp.type) { | |
2301 | first_offset = max(min_offset, tmp.offset) + 1; | |
2302 | } | |
2303 | } | |
2304 | goto done; | |
2305 | } | |
2306 | ||
2307 | /* go backward to find any previous key */ | |
2308 | ret = btrfs_previous_item(root, path, inode->i_ino, key_type); | |
2309 | if (ret == 0) { | |
2310 | struct btrfs_key tmp; | |
2311 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
2312 | if (key_type == tmp.type) { | |
2313 | first_offset = tmp.offset; | |
2314 | ret = overwrite_item(trans, log, dst_path, | |
2315 | path->nodes[0], path->slots[0], | |
2316 | &tmp); | |
2317 | } | |
2318 | } | |
2319 | btrfs_release_path(root, path); | |
2320 | ||
2321 | /* find the first key from this transaction again */ | |
2322 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | |
2323 | if (ret != 0) { | |
2324 | WARN_ON(1); | |
2325 | goto done; | |
2326 | } | |
2327 | ||
2328 | /* | |
2329 | * we have a block from this transaction, log every item in it | |
2330 | * from our directory | |
2331 | */ | |
2332 | while(1) { | |
2333 | struct btrfs_key tmp; | |
2334 | src = path->nodes[0]; | |
2335 | nritems = btrfs_header_nritems(src); | |
2336 | for (i = path->slots[0]; i < nritems; i++) { | |
2337 | btrfs_item_key_to_cpu(src, &min_key, i); | |
2338 | ||
2339 | if (min_key.objectid != inode->i_ino || | |
2340 | min_key.type != key_type) | |
2341 | goto done; | |
2342 | ret = overwrite_item(trans, log, dst_path, src, i, | |
2343 | &min_key); | |
2344 | BUG_ON(ret); | |
2345 | } | |
2346 | path->slots[0] = nritems; | |
2347 | ||
2348 | /* | |
2349 | * look ahead to the next item and see if it is also | |
2350 | * from this directory and from this transaction | |
2351 | */ | |
2352 | ret = btrfs_next_leaf(root, path); | |
2353 | if (ret == 1) { | |
2354 | last_offset = (u64)-1; | |
2355 | goto done; | |
2356 | } | |
2357 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
2358 | if (tmp.objectid != inode->i_ino || tmp.type != key_type) { | |
2359 | last_offset = (u64)-1; | |
2360 | goto done; | |
2361 | } | |
2362 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | |
2363 | ret = overwrite_item(trans, log, dst_path, | |
2364 | path->nodes[0], path->slots[0], | |
2365 | &tmp); | |
2366 | ||
2367 | BUG_ON(ret); | |
2368 | last_offset = tmp.offset; | |
2369 | goto done; | |
2370 | } | |
2371 | } | |
2372 | done: | |
2373 | *last_offset_ret = last_offset; | |
2374 | btrfs_release_path(root, path); | |
2375 | btrfs_release_path(log, dst_path); | |
2376 | ||
2377 | /* insert the log range keys to indicate where the log is valid */ | |
2378 | ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino, | |
2379 | first_offset, last_offset); | |
2380 | BUG_ON(ret); | |
2381 | return 0; | |
2382 | } | |
2383 | ||
2384 | /* | |
2385 | * logging directories is very similar to logging inodes, We find all the items | |
2386 | * from the current transaction and write them to the log. | |
2387 | * | |
2388 | * The recovery code scans the directory in the subvolume, and if it finds a | |
2389 | * key in the range logged that is not present in the log tree, then it means | |
2390 | * that dir entry was unlinked during the transaction. | |
2391 | * | |
2392 | * In order for that scan to work, we must include one key smaller than | |
2393 | * the smallest logged by this transaction and one key larger than the largest | |
2394 | * key logged by this transaction. | |
2395 | */ | |
2396 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | |
2397 | struct btrfs_root *root, struct inode *inode, | |
2398 | struct btrfs_path *path, | |
2399 | struct btrfs_path *dst_path) | |
2400 | { | |
2401 | u64 min_key; | |
2402 | u64 max_key; | |
2403 | int ret; | |
2404 | int key_type = BTRFS_DIR_ITEM_KEY; | |
2405 | ||
2406 | again: | |
2407 | min_key = 0; | |
2408 | max_key = 0; | |
2409 | while(1) { | |
2410 | ret = log_dir_items(trans, root, inode, path, | |
2411 | dst_path, key_type, min_key, | |
2412 | &max_key); | |
2413 | BUG_ON(ret); | |
2414 | if (max_key == (u64)-1) | |
2415 | break; | |
2416 | min_key = max_key + 1; | |
2417 | } | |
2418 | ||
2419 | if (key_type == BTRFS_DIR_ITEM_KEY) { | |
2420 | key_type = BTRFS_DIR_INDEX_KEY; | |
2421 | goto again; | |
2422 | } | |
2423 | return 0; | |
2424 | } | |
2425 | ||
2426 | /* | |
2427 | * a helper function to drop items from the log before we relog an | |
2428 | * inode. max_key_type indicates the highest item type to remove. | |
2429 | * This cannot be run for file data extents because it does not | |
2430 | * free the extents they point to. | |
2431 | */ | |
2432 | static int drop_objectid_items(struct btrfs_trans_handle *trans, | |
2433 | struct btrfs_root *log, | |
2434 | struct btrfs_path *path, | |
2435 | u64 objectid, int max_key_type) | |
2436 | { | |
2437 | int ret; | |
2438 | struct btrfs_key key; | |
2439 | struct btrfs_key found_key; | |
2440 | ||
2441 | key.objectid = objectid; | |
2442 | key.type = max_key_type; | |
2443 | key.offset = (u64)-1; | |
2444 | ||
2445 | while(1) { | |
2446 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | |
2447 | ||
2448 | if (ret != 1) | |
2449 | break; | |
2450 | ||
2451 | if (path->slots[0] == 0) | |
2452 | break; | |
2453 | ||
2454 | path->slots[0]--; | |
2455 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2456 | path->slots[0]); | |
2457 | ||
2458 | if (found_key.objectid != objectid) | |
2459 | break; | |
2460 | ||
2461 | ret = btrfs_del_item(trans, log, path); | |
2462 | BUG_ON(ret); | |
2463 | btrfs_release_path(log, path); | |
2464 | } | |
2465 | btrfs_release_path(log, path); | |
2466 | return 0; | |
2467 | } | |
2468 | ||
31ff1cd2 CM |
2469 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
2470 | struct btrfs_root *log, | |
2471 | struct btrfs_path *dst_path, | |
2472 | struct extent_buffer *src, | |
2473 | int start_slot, int nr, int inode_only) | |
2474 | { | |
2475 | unsigned long src_offset; | |
2476 | unsigned long dst_offset; | |
2477 | struct btrfs_file_extent_item *extent; | |
2478 | struct btrfs_inode_item *inode_item; | |
2479 | int ret; | |
2480 | struct btrfs_key *ins_keys; | |
2481 | u32 *ins_sizes; | |
2482 | char *ins_data; | |
2483 | int i; | |
2484 | ||
2485 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + | |
2486 | nr * sizeof(u32), GFP_NOFS); | |
2487 | ins_sizes = (u32 *)ins_data; | |
2488 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | |
2489 | ||
2490 | for (i = 0; i < nr; i++) { | |
2491 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); | |
2492 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); | |
2493 | } | |
2494 | ret = btrfs_insert_empty_items(trans, log, dst_path, | |
2495 | ins_keys, ins_sizes, nr); | |
2496 | BUG_ON(ret); | |
2497 | ||
2498 | for (i = 0; i < nr; i++) { | |
2499 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], | |
2500 | dst_path->slots[0]); | |
2501 | ||
2502 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); | |
2503 | ||
2504 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | |
2505 | src_offset, ins_sizes[i]); | |
2506 | ||
2507 | if (inode_only == LOG_INODE_EXISTS && | |
2508 | ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { | |
2509 | inode_item = btrfs_item_ptr(dst_path->nodes[0], | |
2510 | dst_path->slots[0], | |
2511 | struct btrfs_inode_item); | |
2512 | btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0); | |
2513 | ||
2514 | /* set the generation to zero so the recover code | |
2515 | * can tell the difference between an logging | |
2516 | * just to say 'this inode exists' and a logging | |
2517 | * to say 'update this inode with these values' | |
2518 | */ | |
2519 | btrfs_set_inode_generation(dst_path->nodes[0], | |
2520 | inode_item, 0); | |
2521 | } | |
2522 | /* take a reference on file data extents so that truncates | |
2523 | * or deletes of this inode don't have to relog the inode | |
2524 | * again | |
2525 | */ | |
2526 | if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) { | |
2527 | int found_type; | |
2528 | extent = btrfs_item_ptr(src, start_slot + i, | |
2529 | struct btrfs_file_extent_item); | |
2530 | ||
2531 | found_type = btrfs_file_extent_type(src, extent); | |
d899e052 YZ |
2532 | if (found_type == BTRFS_FILE_EXTENT_REG || |
2533 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
31ff1cd2 CM |
2534 | u64 ds = btrfs_file_extent_disk_bytenr(src, |
2535 | extent); | |
2536 | u64 dl = btrfs_file_extent_disk_num_bytes(src, | |
2537 | extent); | |
2538 | /* ds == 0 is a hole */ | |
2539 | if (ds != 0) { | |
2540 | ret = btrfs_inc_extent_ref(trans, log, | |
2541 | ds, dl, | |
31840ae1 | 2542 | dst_path->nodes[0]->start, |
31ff1cd2 | 2543 | BTRFS_TREE_LOG_OBJECTID, |
31840ae1 | 2544 | trans->transid, |
3bb1a1bc | 2545 | ins_keys[i].objectid); |
31ff1cd2 CM |
2546 | BUG_ON(ret); |
2547 | } | |
2548 | } | |
2549 | } | |
2550 | dst_path->slots[0]++; | |
2551 | } | |
2552 | ||
2553 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | |
2554 | btrfs_release_path(log, dst_path); | |
2555 | kfree(ins_data); | |
2556 | return 0; | |
2557 | } | |
2558 | ||
e02119d5 CM |
2559 | /* log a single inode in the tree log. |
2560 | * At least one parent directory for this inode must exist in the tree | |
2561 | * or be logged already. | |
2562 | * | |
2563 | * Any items from this inode changed by the current transaction are copied | |
2564 | * to the log tree. An extra reference is taken on any extents in this | |
2565 | * file, allowing us to avoid a whole pile of corner cases around logging | |
2566 | * blocks that have been removed from the tree. | |
2567 | * | |
2568 | * See LOG_INODE_ALL and related defines for a description of what inode_only | |
2569 | * does. | |
2570 | * | |
2571 | * This handles both files and directories. | |
2572 | */ | |
2573 | static int __btrfs_log_inode(struct btrfs_trans_handle *trans, | |
2574 | struct btrfs_root *root, struct inode *inode, | |
2575 | int inode_only) | |
2576 | { | |
2577 | struct btrfs_path *path; | |
2578 | struct btrfs_path *dst_path; | |
2579 | struct btrfs_key min_key; | |
2580 | struct btrfs_key max_key; | |
2581 | struct btrfs_root *log = root->log_root; | |
31ff1cd2 | 2582 | struct extent_buffer *src = NULL; |
e02119d5 CM |
2583 | u32 size; |
2584 | int ret; | |
3a5f1d45 | 2585 | int nritems; |
31ff1cd2 CM |
2586 | int ins_start_slot = 0; |
2587 | int ins_nr; | |
e02119d5 CM |
2588 | |
2589 | log = root->log_root; | |
2590 | ||
2591 | path = btrfs_alloc_path(); | |
2592 | dst_path = btrfs_alloc_path(); | |
2593 | ||
2594 | min_key.objectid = inode->i_ino; | |
2595 | min_key.type = BTRFS_INODE_ITEM_KEY; | |
2596 | min_key.offset = 0; | |
2597 | ||
2598 | max_key.objectid = inode->i_ino; | |
2599 | if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode)) | |
2600 | max_key.type = BTRFS_XATTR_ITEM_KEY; | |
2601 | else | |
2602 | max_key.type = (u8)-1; | |
2603 | max_key.offset = (u64)-1; | |
2604 | ||
2605 | /* | |
2606 | * if this inode has already been logged and we're in inode_only | |
2607 | * mode, we don't want to delete the things that have already | |
2608 | * been written to the log. | |
2609 | * | |
2610 | * But, if the inode has been through an inode_only log, | |
2611 | * the logged_trans field is not set. This allows us to catch | |
2612 | * any new names for this inode in the backrefs by logging it | |
2613 | * again | |
2614 | */ | |
2615 | if (inode_only == LOG_INODE_EXISTS && | |
2616 | BTRFS_I(inode)->logged_trans == trans->transid) { | |
2617 | btrfs_free_path(path); | |
2618 | btrfs_free_path(dst_path); | |
2619 | goto out; | |
2620 | } | |
2621 | mutex_lock(&BTRFS_I(inode)->log_mutex); | |
2622 | ||
2623 | /* | |
2624 | * a brute force approach to making sure we get the most uptodate | |
2625 | * copies of everything. | |
2626 | */ | |
2627 | if (S_ISDIR(inode->i_mode)) { | |
2628 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
2629 | ||
2630 | if (inode_only == LOG_INODE_EXISTS) | |
2631 | max_key_type = BTRFS_XATTR_ITEM_KEY; | |
2632 | ret = drop_objectid_items(trans, log, path, | |
2633 | inode->i_ino, max_key_type); | |
2634 | } else { | |
2635 | ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0); | |
2636 | } | |
2637 | BUG_ON(ret); | |
2638 | path->keep_locks = 1; | |
2639 | ||
2640 | while(1) { | |
31ff1cd2 | 2641 | ins_nr = 0; |
e02119d5 CM |
2642 | ret = btrfs_search_forward(root, &min_key, &max_key, |
2643 | path, 0, trans->transid); | |
2644 | if (ret != 0) | |
2645 | break; | |
3a5f1d45 | 2646 | again: |
31ff1cd2 | 2647 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
e02119d5 CM |
2648 | if (min_key.objectid != inode->i_ino) |
2649 | break; | |
2650 | if (min_key.type > max_key.type) | |
2651 | break; | |
31ff1cd2 | 2652 | |
e02119d5 CM |
2653 | src = path->nodes[0]; |
2654 | size = btrfs_item_size_nr(src, path->slots[0]); | |
31ff1cd2 CM |
2655 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
2656 | ins_nr++; | |
2657 | goto next_slot; | |
2658 | } else if (!ins_nr) { | |
2659 | ins_start_slot = path->slots[0]; | |
2660 | ins_nr = 1; | |
2661 | goto next_slot; | |
e02119d5 CM |
2662 | } |
2663 | ||
31ff1cd2 CM |
2664 | ret = copy_items(trans, log, dst_path, src, ins_start_slot, |
2665 | ins_nr, inode_only); | |
2666 | BUG_ON(ret); | |
2667 | ins_nr = 1; | |
2668 | ins_start_slot = path->slots[0]; | |
2669 | next_slot: | |
e02119d5 | 2670 | |
3a5f1d45 CM |
2671 | nritems = btrfs_header_nritems(path->nodes[0]); |
2672 | path->slots[0]++; | |
2673 | if (path->slots[0] < nritems) { | |
2674 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, | |
2675 | path->slots[0]); | |
2676 | goto again; | |
2677 | } | |
31ff1cd2 CM |
2678 | if (ins_nr) { |
2679 | ret = copy_items(trans, log, dst_path, src, | |
2680 | ins_start_slot, | |
2681 | ins_nr, inode_only); | |
2682 | BUG_ON(ret); | |
2683 | ins_nr = 0; | |
2684 | } | |
3a5f1d45 CM |
2685 | btrfs_release_path(root, path); |
2686 | ||
e02119d5 CM |
2687 | if (min_key.offset < (u64)-1) |
2688 | min_key.offset++; | |
2689 | else if (min_key.type < (u8)-1) | |
2690 | min_key.type++; | |
2691 | else if (min_key.objectid < (u64)-1) | |
2692 | min_key.objectid++; | |
2693 | else | |
2694 | break; | |
2695 | } | |
31ff1cd2 CM |
2696 | if (ins_nr) { |
2697 | ret = copy_items(trans, log, dst_path, src, | |
2698 | ins_start_slot, | |
2699 | ins_nr, inode_only); | |
2700 | BUG_ON(ret); | |
2701 | ins_nr = 0; | |
2702 | } | |
2703 | WARN_ON(ins_nr); | |
9623f9a3 | 2704 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { |
e02119d5 CM |
2705 | btrfs_release_path(root, path); |
2706 | btrfs_release_path(log, dst_path); | |
49eb7e46 | 2707 | BTRFS_I(inode)->log_dirty_trans = 0; |
e02119d5 CM |
2708 | ret = log_directory_changes(trans, root, inode, path, dst_path); |
2709 | BUG_ON(ret); | |
2710 | } | |
3a5f1d45 | 2711 | BTRFS_I(inode)->logged_trans = trans->transid; |
e02119d5 CM |
2712 | mutex_unlock(&BTRFS_I(inode)->log_mutex); |
2713 | ||
2714 | btrfs_free_path(path); | |
2715 | btrfs_free_path(dst_path); | |
2716 | ||
2717 | mutex_lock(&root->fs_info->tree_log_mutex); | |
2718 | ret = update_log_root(trans, log); | |
2719 | BUG_ON(ret); | |
2720 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
2721 | out: | |
2722 | return 0; | |
2723 | } | |
2724 | ||
2725 | int btrfs_log_inode(struct btrfs_trans_handle *trans, | |
2726 | struct btrfs_root *root, struct inode *inode, | |
2727 | int inode_only) | |
2728 | { | |
2729 | int ret; | |
2730 | ||
2731 | start_log_trans(trans, root); | |
2732 | ret = __btrfs_log_inode(trans, root, inode, inode_only); | |
2733 | end_log_trans(root); | |
2734 | return ret; | |
2735 | } | |
2736 | ||
2737 | /* | |
2738 | * helper function around btrfs_log_inode to make sure newly created | |
2739 | * parent directories also end up in the log. A minimal inode and backref | |
2740 | * only logging is done of any parent directories that are older than | |
2741 | * the last committed transaction | |
2742 | */ | |
2743 | int btrfs_log_dentry(struct btrfs_trans_handle *trans, | |
2744 | struct btrfs_root *root, struct dentry *dentry) | |
2745 | { | |
2746 | int inode_only = LOG_INODE_ALL; | |
2747 | struct super_block *sb; | |
2748 | int ret; | |
2749 | ||
2750 | start_log_trans(trans, root); | |
2751 | sb = dentry->d_inode->i_sb; | |
2752 | while(1) { | |
2753 | ret = __btrfs_log_inode(trans, root, dentry->d_inode, | |
2754 | inode_only); | |
2755 | BUG_ON(ret); | |
2756 | inode_only = LOG_INODE_EXISTS; | |
2757 | ||
2758 | dentry = dentry->d_parent; | |
2759 | if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb) | |
2760 | break; | |
2761 | ||
2762 | if (BTRFS_I(dentry->d_inode)->generation <= | |
2763 | root->fs_info->last_trans_committed) | |
2764 | break; | |
2765 | } | |
2766 | end_log_trans(root); | |
2767 | return 0; | |
2768 | } | |
2769 | ||
2770 | /* | |
2771 | * it is not safe to log dentry if the chunk root has added new | |
2772 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | |
2773 | * If this returns 1, you must commit the transaction to safely get your | |
2774 | * data on disk. | |
2775 | */ | |
2776 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | |
2777 | struct btrfs_root *root, struct dentry *dentry) | |
2778 | { | |
2779 | u64 gen; | |
2780 | gen = root->fs_info->last_trans_new_blockgroup; | |
2781 | if (gen > root->fs_info->last_trans_committed) | |
2782 | return 1; | |
2783 | else | |
2784 | return btrfs_log_dentry(trans, root, dentry); | |
2785 | } | |
2786 | ||
2787 | /* | |
2788 | * should be called during mount to recover any replay any log trees | |
2789 | * from the FS | |
2790 | */ | |
2791 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | |
2792 | { | |
2793 | int ret; | |
2794 | struct btrfs_path *path; | |
2795 | struct btrfs_trans_handle *trans; | |
2796 | struct btrfs_key key; | |
2797 | struct btrfs_key found_key; | |
2798 | struct btrfs_key tmp_key; | |
2799 | struct btrfs_root *log; | |
2800 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | |
8d5bf1cb | 2801 | u64 highest_inode; |
e02119d5 CM |
2802 | struct walk_control wc = { |
2803 | .process_func = process_one_buffer, | |
2804 | .stage = 0, | |
2805 | }; | |
2806 | ||
2807 | fs_info->log_root_recovering = 1; | |
2808 | path = btrfs_alloc_path(); | |
2809 | BUG_ON(!path); | |
2810 | ||
2811 | trans = btrfs_start_transaction(fs_info->tree_root, 1); | |
2812 | ||
2813 | wc.trans = trans; | |
2814 | wc.pin = 1; | |
2815 | ||
2816 | walk_log_tree(trans, log_root_tree, &wc); | |
2817 | ||
2818 | again: | |
2819 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
2820 | key.offset = (u64)-1; | |
2821 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
2822 | ||
2823 | while(1) { | |
2824 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); | |
2825 | if (ret < 0) | |
2826 | break; | |
2827 | if (ret > 0) { | |
2828 | if (path->slots[0] == 0) | |
2829 | break; | |
2830 | path->slots[0]--; | |
2831 | } | |
2832 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2833 | path->slots[0]); | |
2834 | btrfs_release_path(log_root_tree, path); | |
2835 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
2836 | break; | |
2837 | ||
2838 | log = btrfs_read_fs_root_no_radix(log_root_tree, | |
2839 | &found_key); | |
2840 | BUG_ON(!log); | |
2841 | ||
2842 | ||
2843 | tmp_key.objectid = found_key.offset; | |
2844 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; | |
2845 | tmp_key.offset = (u64)-1; | |
2846 | ||
2847 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); | |
2848 | ||
2849 | BUG_ON(!wc.replay_dest); | |
2850 | ||
2851 | btrfs_record_root_in_trans(wc.replay_dest); | |
2852 | ret = walk_log_tree(trans, log, &wc); | |
2853 | BUG_ON(ret); | |
2854 | ||
2855 | if (wc.stage == LOG_WALK_REPLAY_ALL) { | |
2856 | ret = fixup_inode_link_counts(trans, wc.replay_dest, | |
2857 | path); | |
2858 | BUG_ON(ret); | |
2859 | } | |
8d5bf1cb CM |
2860 | ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode); |
2861 | if (ret == 0) { | |
2862 | wc.replay_dest->highest_inode = highest_inode; | |
2863 | wc.replay_dest->last_inode_alloc = highest_inode; | |
2864 | } | |
e02119d5 CM |
2865 | |
2866 | key.offset = found_key.offset - 1; | |
2867 | free_extent_buffer(log->node); | |
2868 | kfree(log); | |
2869 | ||
2870 | if (found_key.offset == 0) | |
2871 | break; | |
2872 | } | |
2873 | btrfs_release_path(log_root_tree, path); | |
2874 | ||
2875 | /* step one is to pin it all, step two is to replay just inodes */ | |
2876 | if (wc.pin) { | |
2877 | wc.pin = 0; | |
2878 | wc.process_func = replay_one_buffer; | |
2879 | wc.stage = LOG_WALK_REPLAY_INODES; | |
2880 | goto again; | |
2881 | } | |
2882 | /* step three is to replay everything */ | |
2883 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | |
2884 | wc.stage++; | |
2885 | goto again; | |
2886 | } | |
2887 | ||
2888 | btrfs_free_path(path); | |
2889 | ||
2890 | free_extent_buffer(log_root_tree->node); | |
2891 | log_root_tree->log_root = NULL; | |
2892 | fs_info->log_root_recovering = 0; | |
2893 | ||
2894 | /* step 4: commit the transaction, which also unpins the blocks */ | |
2895 | btrfs_commit_transaction(trans, fs_info->tree_root); | |
2896 | ||
2897 | kfree(log_root_tree); | |
2898 | return 0; | |
2899 | } |