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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
5dc562c5 | 21 | #include <linux/list_sort.h> |
e02119d5 CM |
22 | #include "ctree.h" |
23 | #include "transaction.h" | |
24 | #include "disk-io.h" | |
25 | #include "locking.h" | |
26 | #include "print-tree.h" | |
27 | #include "compat.h" | |
b2950863 | 28 | #include "tree-log.h" |
e02119d5 CM |
29 | |
30 | /* magic values for the inode_only field in btrfs_log_inode: | |
31 | * | |
32 | * LOG_INODE_ALL means to log everything | |
33 | * LOG_INODE_EXISTS means to log just enough to recreate the inode | |
34 | * during log replay | |
35 | */ | |
36 | #define LOG_INODE_ALL 0 | |
37 | #define LOG_INODE_EXISTS 1 | |
38 | ||
12fcfd22 CM |
39 | /* |
40 | * directory trouble cases | |
41 | * | |
42 | * 1) on rename or unlink, if the inode being unlinked isn't in the fsync | |
43 | * log, we must force a full commit before doing an fsync of the directory | |
44 | * where the unlink was done. | |
45 | * ---> record transid of last unlink/rename per directory | |
46 | * | |
47 | * mkdir foo/some_dir | |
48 | * normal commit | |
49 | * rename foo/some_dir foo2/some_dir | |
50 | * mkdir foo/some_dir | |
51 | * fsync foo/some_dir/some_file | |
52 | * | |
53 | * The fsync above will unlink the original some_dir without recording | |
54 | * it in its new location (foo2). After a crash, some_dir will be gone | |
55 | * unless the fsync of some_file forces a full commit | |
56 | * | |
57 | * 2) we must log any new names for any file or dir that is in the fsync | |
58 | * log. ---> check inode while renaming/linking. | |
59 | * | |
60 | * 2a) we must log any new names for any file or dir during rename | |
61 | * when the directory they are being removed from was logged. | |
62 | * ---> check inode and old parent dir during rename | |
63 | * | |
64 | * 2a is actually the more important variant. With the extra logging | |
65 | * a crash might unlink the old name without recreating the new one | |
66 | * | |
67 | * 3) after a crash, we must go through any directories with a link count | |
68 | * of zero and redo the rm -rf | |
69 | * | |
70 | * mkdir f1/foo | |
71 | * normal commit | |
72 | * rm -rf f1/foo | |
73 | * fsync(f1) | |
74 | * | |
75 | * The directory f1 was fully removed from the FS, but fsync was never | |
76 | * called on f1, only its parent dir. After a crash the rm -rf must | |
77 | * be replayed. This must be able to recurse down the entire | |
78 | * directory tree. The inode link count fixup code takes care of the | |
79 | * ugly details. | |
80 | */ | |
81 | ||
e02119d5 CM |
82 | /* |
83 | * stages for the tree walking. The first | |
84 | * stage (0) is to only pin down the blocks we find | |
85 | * the second stage (1) is to make sure that all the inodes | |
86 | * we find in the log are created in the subvolume. | |
87 | * | |
88 | * The last stage is to deal with directories and links and extents | |
89 | * and all the other fun semantics | |
90 | */ | |
91 | #define LOG_WALK_PIN_ONLY 0 | |
92 | #define LOG_WALK_REPLAY_INODES 1 | |
93 | #define LOG_WALK_REPLAY_ALL 2 | |
94 | ||
12fcfd22 | 95 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
e02119d5 CM |
96 | struct btrfs_root *root, struct inode *inode, |
97 | int inode_only); | |
ec051c0f YZ |
98 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
99 | struct btrfs_root *root, | |
100 | struct btrfs_path *path, u64 objectid); | |
12fcfd22 CM |
101 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
102 | struct btrfs_root *root, | |
103 | struct btrfs_root *log, | |
104 | struct btrfs_path *path, | |
105 | u64 dirid, int del_all); | |
e02119d5 CM |
106 | |
107 | /* | |
108 | * tree logging is a special write ahead log used to make sure that | |
109 | * fsyncs and O_SYNCs can happen without doing full tree commits. | |
110 | * | |
111 | * Full tree commits are expensive because they require commonly | |
112 | * modified blocks to be recowed, creating many dirty pages in the | |
113 | * extent tree an 4x-6x higher write load than ext3. | |
114 | * | |
115 | * Instead of doing a tree commit on every fsync, we use the | |
116 | * key ranges and transaction ids to find items for a given file or directory | |
117 | * that have changed in this transaction. Those items are copied into | |
118 | * a special tree (one per subvolume root), that tree is written to disk | |
119 | * and then the fsync is considered complete. | |
120 | * | |
121 | * After a crash, items are copied out of the log-tree back into the | |
122 | * subvolume tree. Any file data extents found are recorded in the extent | |
123 | * allocation tree, and the log-tree freed. | |
124 | * | |
125 | * The log tree is read three times, once to pin down all the extents it is | |
126 | * using in ram and once, once to create all the inodes logged in the tree | |
127 | * and once to do all the other items. | |
128 | */ | |
129 | ||
e02119d5 CM |
130 | /* |
131 | * start a sub transaction and setup the log tree | |
132 | * this increments the log tree writer count to make the people | |
133 | * syncing the tree wait for us to finish | |
134 | */ | |
135 | static int start_log_trans(struct btrfs_trans_handle *trans, | |
136 | struct btrfs_root *root) | |
137 | { | |
138 | int ret; | |
4a500fd1 | 139 | int err = 0; |
7237f183 YZ |
140 | |
141 | mutex_lock(&root->log_mutex); | |
142 | if (root->log_root) { | |
ff782e0a JB |
143 | if (!root->log_start_pid) { |
144 | root->log_start_pid = current->pid; | |
145 | root->log_multiple_pids = false; | |
146 | } else if (root->log_start_pid != current->pid) { | |
147 | root->log_multiple_pids = true; | |
148 | } | |
149 | ||
2ecb7923 | 150 | atomic_inc(&root->log_batch); |
7237f183 YZ |
151 | atomic_inc(&root->log_writers); |
152 | mutex_unlock(&root->log_mutex); | |
153 | return 0; | |
154 | } | |
ff782e0a JB |
155 | root->log_multiple_pids = false; |
156 | root->log_start_pid = current->pid; | |
e02119d5 CM |
157 | mutex_lock(&root->fs_info->tree_log_mutex); |
158 | if (!root->fs_info->log_root_tree) { | |
159 | ret = btrfs_init_log_root_tree(trans, root->fs_info); | |
4a500fd1 YZ |
160 | if (ret) |
161 | err = ret; | |
e02119d5 | 162 | } |
4a500fd1 | 163 | if (err == 0 && !root->log_root) { |
e02119d5 | 164 | ret = btrfs_add_log_tree(trans, root); |
4a500fd1 YZ |
165 | if (ret) |
166 | err = ret; | |
e02119d5 | 167 | } |
e02119d5 | 168 | mutex_unlock(&root->fs_info->tree_log_mutex); |
2ecb7923 | 169 | atomic_inc(&root->log_batch); |
7237f183 YZ |
170 | atomic_inc(&root->log_writers); |
171 | mutex_unlock(&root->log_mutex); | |
4a500fd1 | 172 | return err; |
e02119d5 CM |
173 | } |
174 | ||
175 | /* | |
176 | * returns 0 if there was a log transaction running and we were able | |
177 | * to join, or returns -ENOENT if there were not transactions | |
178 | * in progress | |
179 | */ | |
180 | static int join_running_log_trans(struct btrfs_root *root) | |
181 | { | |
182 | int ret = -ENOENT; | |
183 | ||
184 | smp_mb(); | |
185 | if (!root->log_root) | |
186 | return -ENOENT; | |
187 | ||
7237f183 | 188 | mutex_lock(&root->log_mutex); |
e02119d5 CM |
189 | if (root->log_root) { |
190 | ret = 0; | |
7237f183 | 191 | atomic_inc(&root->log_writers); |
e02119d5 | 192 | } |
7237f183 | 193 | mutex_unlock(&root->log_mutex); |
e02119d5 CM |
194 | return ret; |
195 | } | |
196 | ||
12fcfd22 CM |
197 | /* |
198 | * This either makes the current running log transaction wait | |
199 | * until you call btrfs_end_log_trans() or it makes any future | |
200 | * log transactions wait until you call btrfs_end_log_trans() | |
201 | */ | |
202 | int btrfs_pin_log_trans(struct btrfs_root *root) | |
203 | { | |
204 | int ret = -ENOENT; | |
205 | ||
206 | mutex_lock(&root->log_mutex); | |
207 | atomic_inc(&root->log_writers); | |
208 | mutex_unlock(&root->log_mutex); | |
209 | return ret; | |
210 | } | |
211 | ||
e02119d5 CM |
212 | /* |
213 | * indicate we're done making changes to the log tree | |
214 | * and wake up anyone waiting to do a sync | |
215 | */ | |
143bede5 | 216 | void btrfs_end_log_trans(struct btrfs_root *root) |
e02119d5 | 217 | { |
7237f183 YZ |
218 | if (atomic_dec_and_test(&root->log_writers)) { |
219 | smp_mb(); | |
220 | if (waitqueue_active(&root->log_writer_wait)) | |
221 | wake_up(&root->log_writer_wait); | |
222 | } | |
e02119d5 CM |
223 | } |
224 | ||
225 | ||
226 | /* | |
227 | * the walk control struct is used to pass state down the chain when | |
228 | * processing the log tree. The stage field tells us which part | |
229 | * of the log tree processing we are currently doing. The others | |
230 | * are state fields used for that specific part | |
231 | */ | |
232 | struct walk_control { | |
233 | /* should we free the extent on disk when done? This is used | |
234 | * at transaction commit time while freeing a log tree | |
235 | */ | |
236 | int free; | |
237 | ||
238 | /* should we write out the extent buffer? This is used | |
239 | * while flushing the log tree to disk during a sync | |
240 | */ | |
241 | int write; | |
242 | ||
243 | /* should we wait for the extent buffer io to finish? Also used | |
244 | * while flushing the log tree to disk for a sync | |
245 | */ | |
246 | int wait; | |
247 | ||
248 | /* pin only walk, we record which extents on disk belong to the | |
249 | * log trees | |
250 | */ | |
251 | int pin; | |
252 | ||
253 | /* what stage of the replay code we're currently in */ | |
254 | int stage; | |
255 | ||
256 | /* the root we are currently replaying */ | |
257 | struct btrfs_root *replay_dest; | |
258 | ||
259 | /* the trans handle for the current replay */ | |
260 | struct btrfs_trans_handle *trans; | |
261 | ||
262 | /* the function that gets used to process blocks we find in the | |
263 | * tree. Note the extent_buffer might not be up to date when it is | |
264 | * passed in, and it must be checked or read if you need the data | |
265 | * inside it | |
266 | */ | |
267 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | |
268 | struct walk_control *wc, u64 gen); | |
269 | }; | |
270 | ||
271 | /* | |
272 | * process_func used to pin down extents, write them or wait on them | |
273 | */ | |
274 | static int process_one_buffer(struct btrfs_root *log, | |
275 | struct extent_buffer *eb, | |
276 | struct walk_control *wc, u64 gen) | |
277 | { | |
04018de5 | 278 | if (wc->pin) |
e688b725 CM |
279 | btrfs_pin_extent_for_log_replay(wc->trans, |
280 | log->fs_info->extent_root, | |
281 | eb->start, eb->len); | |
e02119d5 | 282 | |
b9fab919 | 283 | if (btrfs_buffer_uptodate(eb, gen, 0)) { |
e02119d5 CM |
284 | if (wc->write) |
285 | btrfs_write_tree_block(eb); | |
286 | if (wc->wait) | |
287 | btrfs_wait_tree_block_writeback(eb); | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Item overwrite used by replay and tree logging. eb, slot and key all refer | |
294 | * to the src data we are copying out. | |
295 | * | |
296 | * root is the tree we are copying into, and path is a scratch | |
297 | * path for use in this function (it should be released on entry and | |
298 | * will be released on exit). | |
299 | * | |
300 | * If the key is already in the destination tree the existing item is | |
301 | * overwritten. If the existing item isn't big enough, it is extended. | |
302 | * If it is too large, it is truncated. | |
303 | * | |
304 | * If the key isn't in the destination yet, a new item is inserted. | |
305 | */ | |
306 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, | |
307 | struct btrfs_root *root, | |
308 | struct btrfs_path *path, | |
309 | struct extent_buffer *eb, int slot, | |
310 | struct btrfs_key *key) | |
311 | { | |
312 | int ret; | |
313 | u32 item_size; | |
314 | u64 saved_i_size = 0; | |
315 | int save_old_i_size = 0; | |
316 | unsigned long src_ptr; | |
317 | unsigned long dst_ptr; | |
318 | int overwrite_root = 0; | |
319 | ||
320 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
321 | overwrite_root = 1; | |
322 | ||
323 | item_size = btrfs_item_size_nr(eb, slot); | |
324 | src_ptr = btrfs_item_ptr_offset(eb, slot); | |
325 | ||
326 | /* look for the key in the destination tree */ | |
327 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
328 | if (ret == 0) { | |
329 | char *src_copy; | |
330 | char *dst_copy; | |
331 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], | |
332 | path->slots[0]); | |
333 | if (dst_size != item_size) | |
334 | goto insert; | |
335 | ||
336 | if (item_size == 0) { | |
b3b4aa74 | 337 | btrfs_release_path(path); |
e02119d5 CM |
338 | return 0; |
339 | } | |
340 | dst_copy = kmalloc(item_size, GFP_NOFS); | |
341 | src_copy = kmalloc(item_size, GFP_NOFS); | |
2a29edc6 | 342 | if (!dst_copy || !src_copy) { |
b3b4aa74 | 343 | btrfs_release_path(path); |
2a29edc6 | 344 | kfree(dst_copy); |
345 | kfree(src_copy); | |
346 | return -ENOMEM; | |
347 | } | |
e02119d5 CM |
348 | |
349 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | |
350 | ||
351 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
352 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | |
353 | item_size); | |
354 | ret = memcmp(dst_copy, src_copy, item_size); | |
355 | ||
356 | kfree(dst_copy); | |
357 | kfree(src_copy); | |
358 | /* | |
359 | * they have the same contents, just return, this saves | |
360 | * us from cowing blocks in the destination tree and doing | |
361 | * extra writes that may not have been done by a previous | |
362 | * sync | |
363 | */ | |
364 | if (ret == 0) { | |
b3b4aa74 | 365 | btrfs_release_path(path); |
e02119d5 CM |
366 | return 0; |
367 | } | |
368 | ||
369 | } | |
370 | insert: | |
b3b4aa74 | 371 | btrfs_release_path(path); |
e02119d5 CM |
372 | /* try to insert the key into the destination tree */ |
373 | ret = btrfs_insert_empty_item(trans, root, path, | |
374 | key, item_size); | |
375 | ||
376 | /* make sure any existing item is the correct size */ | |
377 | if (ret == -EEXIST) { | |
378 | u32 found_size; | |
379 | found_size = btrfs_item_size_nr(path->nodes[0], | |
380 | path->slots[0]); | |
143bede5 | 381 | if (found_size > item_size) |
e02119d5 | 382 | btrfs_truncate_item(trans, root, path, item_size, 1); |
143bede5 JM |
383 | else if (found_size < item_size) |
384 | btrfs_extend_item(trans, root, path, | |
385 | item_size - found_size); | |
e02119d5 | 386 | } else if (ret) { |
4a500fd1 | 387 | return ret; |
e02119d5 CM |
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 | } | |
438 | no_copy: | |
439 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 440 | btrfs_release_path(path); |
e02119d5 CM |
441 | return 0; |
442 | } | |
443 | ||
444 | /* | |
445 | * simple helper to read an inode off the disk from a given root | |
446 | * This can only be called for subvolume roots and not for the log | |
447 | */ | |
448 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | |
449 | u64 objectid) | |
450 | { | |
5d4f98a2 | 451 | struct btrfs_key key; |
e02119d5 | 452 | struct inode *inode; |
e02119d5 | 453 | |
5d4f98a2 YZ |
454 | key.objectid = objectid; |
455 | key.type = BTRFS_INODE_ITEM_KEY; | |
456 | key.offset = 0; | |
73f73415 | 457 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); |
5d4f98a2 YZ |
458 | if (IS_ERR(inode)) { |
459 | inode = NULL; | |
460 | } else if (is_bad_inode(inode)) { | |
e02119d5 CM |
461 | iput(inode); |
462 | inode = NULL; | |
463 | } | |
464 | return inode; | |
465 | } | |
466 | ||
467 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | |
468 | * subvolume 'root'. path is released on entry and should be released | |
469 | * on exit. | |
470 | * | |
471 | * extents in the log tree have not been allocated out of the extent | |
472 | * tree yet. So, this completes the allocation, taking a reference | |
473 | * as required if the extent already exists or creating a new extent | |
474 | * if it isn't in the extent allocation tree yet. | |
475 | * | |
476 | * The extent is inserted into the file, dropping any existing extents | |
477 | * from the file that overlap the new one. | |
478 | */ | |
479 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |
480 | struct btrfs_root *root, | |
481 | struct btrfs_path *path, | |
482 | struct extent_buffer *eb, int slot, | |
483 | struct btrfs_key *key) | |
484 | { | |
485 | int found_type; | |
486 | u64 mask = root->sectorsize - 1; | |
487 | u64 extent_end; | |
e02119d5 | 488 | u64 start = key->offset; |
07d400a6 | 489 | u64 saved_nbytes; |
e02119d5 CM |
490 | struct btrfs_file_extent_item *item; |
491 | struct inode *inode = NULL; | |
492 | unsigned long size; | |
493 | int ret = 0; | |
494 | ||
495 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
496 | found_type = btrfs_file_extent_type(eb, item); | |
497 | ||
d899e052 YZ |
498 | if (found_type == BTRFS_FILE_EXTENT_REG || |
499 | found_type == BTRFS_FILE_EXTENT_PREALLOC) | |
e02119d5 CM |
500 | extent_end = start + btrfs_file_extent_num_bytes(eb, item); |
501 | else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
c8b97818 | 502 | size = btrfs_file_extent_inline_len(eb, item); |
e02119d5 CM |
503 | extent_end = (start + size + mask) & ~mask; |
504 | } else { | |
505 | ret = 0; | |
506 | goto out; | |
507 | } | |
508 | ||
509 | inode = read_one_inode(root, key->objectid); | |
510 | if (!inode) { | |
511 | ret = -EIO; | |
512 | goto out; | |
513 | } | |
514 | ||
515 | /* | |
516 | * first check to see if we already have this extent in the | |
517 | * file. This must be done before the btrfs_drop_extents run | |
518 | * so we don't try to drop this extent. | |
519 | */ | |
33345d01 | 520 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
e02119d5 CM |
521 | start, 0); |
522 | ||
d899e052 YZ |
523 | if (ret == 0 && |
524 | (found_type == BTRFS_FILE_EXTENT_REG || | |
525 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | |
e02119d5 CM |
526 | struct btrfs_file_extent_item cmp1; |
527 | struct btrfs_file_extent_item cmp2; | |
528 | struct btrfs_file_extent_item *existing; | |
529 | struct extent_buffer *leaf; | |
530 | ||
531 | leaf = path->nodes[0]; | |
532 | existing = btrfs_item_ptr(leaf, path->slots[0], | |
533 | struct btrfs_file_extent_item); | |
534 | ||
535 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | |
536 | sizeof(cmp1)); | |
537 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | |
538 | sizeof(cmp2)); | |
539 | ||
540 | /* | |
541 | * we already have a pointer to this exact extent, | |
542 | * we don't have to do anything | |
543 | */ | |
544 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | |
b3b4aa74 | 545 | btrfs_release_path(path); |
e02119d5 CM |
546 | goto out; |
547 | } | |
548 | } | |
b3b4aa74 | 549 | btrfs_release_path(path); |
e02119d5 | 550 | |
07d400a6 | 551 | saved_nbytes = inode_get_bytes(inode); |
e02119d5 | 552 | /* drop any overlapping extents */ |
2671485d | 553 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
e02119d5 CM |
554 | BUG_ON(ret); |
555 | ||
07d400a6 YZ |
556 | if (found_type == BTRFS_FILE_EXTENT_REG || |
557 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
5d4f98a2 | 558 | u64 offset; |
07d400a6 YZ |
559 | unsigned long dest_offset; |
560 | struct btrfs_key ins; | |
561 | ||
562 | ret = btrfs_insert_empty_item(trans, root, path, key, | |
563 | sizeof(*item)); | |
564 | BUG_ON(ret); | |
565 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], | |
566 | path->slots[0]); | |
567 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | |
568 | (unsigned long)item, sizeof(*item)); | |
569 | ||
570 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
571 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
572 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 | 573 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
07d400a6 YZ |
574 | |
575 | if (ins.objectid > 0) { | |
576 | u64 csum_start; | |
577 | u64 csum_end; | |
578 | LIST_HEAD(ordered_sums); | |
579 | /* | |
580 | * is this extent already allocated in the extent | |
581 | * allocation tree? If so, just add a reference | |
582 | */ | |
583 | ret = btrfs_lookup_extent(root, ins.objectid, | |
584 | ins.offset); | |
585 | if (ret == 0) { | |
586 | ret = btrfs_inc_extent_ref(trans, root, | |
587 | ins.objectid, ins.offset, | |
5d4f98a2 | 588 | 0, root->root_key.objectid, |
66d7e7f0 | 589 | key->objectid, offset, 0); |
37daa4f9 | 590 | BUG_ON(ret); |
07d400a6 YZ |
591 | } else { |
592 | /* | |
593 | * insert the extent pointer in the extent | |
594 | * allocation tree | |
595 | */ | |
5d4f98a2 YZ |
596 | ret = btrfs_alloc_logged_file_extent(trans, |
597 | root, root->root_key.objectid, | |
598 | key->objectid, offset, &ins); | |
07d400a6 YZ |
599 | BUG_ON(ret); |
600 | } | |
b3b4aa74 | 601 | btrfs_release_path(path); |
07d400a6 YZ |
602 | |
603 | if (btrfs_file_extent_compression(eb, item)) { | |
604 | csum_start = ins.objectid; | |
605 | csum_end = csum_start + ins.offset; | |
606 | } else { | |
607 | csum_start = ins.objectid + | |
608 | btrfs_file_extent_offset(eb, item); | |
609 | csum_end = csum_start + | |
610 | btrfs_file_extent_num_bytes(eb, item); | |
611 | } | |
612 | ||
613 | ret = btrfs_lookup_csums_range(root->log_root, | |
614 | csum_start, csum_end - 1, | |
a2de733c | 615 | &ordered_sums, 0); |
07d400a6 YZ |
616 | BUG_ON(ret); |
617 | while (!list_empty(&ordered_sums)) { | |
618 | struct btrfs_ordered_sum *sums; | |
619 | sums = list_entry(ordered_sums.next, | |
620 | struct btrfs_ordered_sum, | |
621 | list); | |
622 | ret = btrfs_csum_file_blocks(trans, | |
623 | root->fs_info->csum_root, | |
624 | sums); | |
625 | BUG_ON(ret); | |
626 | list_del(&sums->list); | |
627 | kfree(sums); | |
628 | } | |
629 | } else { | |
b3b4aa74 | 630 | btrfs_release_path(path); |
07d400a6 YZ |
631 | } |
632 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
633 | /* inline extents are easy, we just overwrite them */ | |
634 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
635 | BUG_ON(ret); | |
636 | } | |
e02119d5 | 637 | |
07d400a6 | 638 | inode_set_bytes(inode, saved_nbytes); |
b9959295 | 639 | ret = btrfs_update_inode(trans, root, inode); |
e02119d5 CM |
640 | out: |
641 | if (inode) | |
642 | iput(inode); | |
643 | return ret; | |
644 | } | |
645 | ||
646 | /* | |
647 | * when cleaning up conflicts between the directory names in the | |
648 | * subvolume, directory names in the log and directory names in the | |
649 | * inode back references, we may have to unlink inodes from directories. | |
650 | * | |
651 | * This is a helper function to do the unlink of a specific directory | |
652 | * item | |
653 | */ | |
654 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | |
655 | struct btrfs_root *root, | |
656 | struct btrfs_path *path, | |
657 | struct inode *dir, | |
658 | struct btrfs_dir_item *di) | |
659 | { | |
660 | struct inode *inode; | |
661 | char *name; | |
662 | int name_len; | |
663 | struct extent_buffer *leaf; | |
664 | struct btrfs_key location; | |
665 | int ret; | |
666 | ||
667 | leaf = path->nodes[0]; | |
668 | ||
669 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
670 | name_len = btrfs_dir_name_len(leaf, di); | |
671 | name = kmalloc(name_len, GFP_NOFS); | |
2a29edc6 | 672 | if (!name) |
673 | return -ENOMEM; | |
674 | ||
e02119d5 | 675 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
b3b4aa74 | 676 | btrfs_release_path(path); |
e02119d5 CM |
677 | |
678 | inode = read_one_inode(root, location.objectid); | |
c00e9493 TI |
679 | if (!inode) { |
680 | kfree(name); | |
681 | return -EIO; | |
682 | } | |
e02119d5 | 683 | |
ec051c0f YZ |
684 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
685 | BUG_ON(ret); | |
12fcfd22 | 686 | |
e02119d5 | 687 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
ec051c0f | 688 | BUG_ON(ret); |
e02119d5 CM |
689 | kfree(name); |
690 | ||
691 | iput(inode); | |
b6305567 CM |
692 | |
693 | btrfs_run_delayed_items(trans, root); | |
e02119d5 CM |
694 | return ret; |
695 | } | |
696 | ||
697 | /* | |
698 | * helper function to see if a given name and sequence number found | |
699 | * in an inode back reference are already in a directory and correctly | |
700 | * point to this inode | |
701 | */ | |
702 | static noinline int inode_in_dir(struct btrfs_root *root, | |
703 | struct btrfs_path *path, | |
704 | u64 dirid, u64 objectid, u64 index, | |
705 | const char *name, int name_len) | |
706 | { | |
707 | struct btrfs_dir_item *di; | |
708 | struct btrfs_key location; | |
709 | int match = 0; | |
710 | ||
711 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | |
712 | index, name, name_len, 0); | |
713 | if (di && !IS_ERR(di)) { | |
714 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
715 | if (location.objectid != objectid) | |
716 | goto out; | |
717 | } else | |
718 | goto out; | |
b3b4aa74 | 719 | btrfs_release_path(path); |
e02119d5 CM |
720 | |
721 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); | |
722 | if (di && !IS_ERR(di)) { | |
723 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
724 | if (location.objectid != objectid) | |
725 | goto out; | |
726 | } else | |
727 | goto out; | |
728 | match = 1; | |
729 | out: | |
b3b4aa74 | 730 | btrfs_release_path(path); |
e02119d5 CM |
731 | return match; |
732 | } | |
733 | ||
734 | /* | |
735 | * helper function to check a log tree for a named back reference in | |
736 | * an inode. This is used to decide if a back reference that is | |
737 | * found in the subvolume conflicts with what we find in the log. | |
738 | * | |
739 | * inode backreferences may have multiple refs in a single item, | |
740 | * during replay we process one reference at a time, and we don't | |
741 | * want to delete valid links to a file from the subvolume if that | |
742 | * link is also in the log. | |
743 | */ | |
744 | static noinline int backref_in_log(struct btrfs_root *log, | |
745 | struct btrfs_key *key, | |
746 | char *name, int namelen) | |
747 | { | |
748 | struct btrfs_path *path; | |
749 | struct btrfs_inode_ref *ref; | |
750 | unsigned long ptr; | |
751 | unsigned long ptr_end; | |
752 | unsigned long name_ptr; | |
753 | int found_name_len; | |
754 | int item_size; | |
755 | int ret; | |
756 | int match = 0; | |
757 | ||
758 | path = btrfs_alloc_path(); | |
2a29edc6 | 759 | if (!path) |
760 | return -ENOMEM; | |
761 | ||
e02119d5 CM |
762 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
763 | if (ret != 0) | |
764 | goto out; | |
765 | ||
766 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | |
767 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
768 | ptr_end = ptr + item_size; | |
769 | while (ptr < ptr_end) { | |
770 | ref = (struct btrfs_inode_ref *)ptr; | |
771 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); | |
772 | if (found_name_len == namelen) { | |
773 | name_ptr = (unsigned long)(ref + 1); | |
774 | ret = memcmp_extent_buffer(path->nodes[0], name, | |
775 | name_ptr, namelen); | |
776 | if (ret == 0) { | |
777 | match = 1; | |
778 | goto out; | |
779 | } | |
780 | } | |
781 | ptr = (unsigned long)(ref + 1) + found_name_len; | |
782 | } | |
783 | out: | |
784 | btrfs_free_path(path); | |
785 | return match; | |
786 | } | |
787 | ||
5a1d7843 | 788 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
e02119d5 | 789 | struct btrfs_root *root, |
e02119d5 | 790 | struct btrfs_path *path, |
5a1d7843 JS |
791 | struct btrfs_root *log_root, |
792 | struct inode *dir, struct inode *inode, | |
793 | struct btrfs_key *key, | |
794 | struct extent_buffer *eb, | |
795 | struct btrfs_inode_ref *ref, | |
796 | char *name, int namelen, int *search_done) | |
e02119d5 | 797 | { |
34f3e4f2 | 798 | int ret; |
5a1d7843 | 799 | struct btrfs_dir_item *di; |
c622ae60 | 800 | |
e02119d5 CM |
801 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
802 | if (ret == 0) { | |
803 | char *victim_name; | |
804 | int victim_name_len; | |
805 | struct btrfs_inode_ref *victim_ref; | |
806 | unsigned long ptr; | |
807 | unsigned long ptr_end; | |
808 | struct extent_buffer *leaf = path->nodes[0]; | |
809 | ||
810 | /* are we trying to overwrite a back ref for the root directory | |
811 | * if so, just jump out, we're done | |
812 | */ | |
813 | if (key->objectid == key->offset) | |
5a1d7843 | 814 | return 1; |
e02119d5 CM |
815 | |
816 | /* check all the names in this back reference to see | |
817 | * if they are in the log. if so, we allow them to stay | |
818 | * otherwise they must be unlinked as a conflict | |
819 | */ | |
820 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
821 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); | |
d397712b | 822 | while (ptr < ptr_end) { |
e02119d5 CM |
823 | victim_ref = (struct btrfs_inode_ref *)ptr; |
824 | victim_name_len = btrfs_inode_ref_name_len(leaf, | |
825 | victim_ref); | |
826 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | |
827 | BUG_ON(!victim_name); | |
828 | ||
829 | read_extent_buffer(leaf, victim_name, | |
830 | (unsigned long)(victim_ref + 1), | |
831 | victim_name_len); | |
832 | ||
5a1d7843 | 833 | if (!backref_in_log(log_root, key, victim_name, |
e02119d5 CM |
834 | victim_name_len)) { |
835 | btrfs_inc_nlink(inode); | |
b3b4aa74 | 836 | btrfs_release_path(path); |
12fcfd22 | 837 | |
e02119d5 CM |
838 | ret = btrfs_unlink_inode(trans, root, dir, |
839 | inode, victim_name, | |
840 | victim_name_len); | |
b6305567 | 841 | btrfs_run_delayed_items(trans, root); |
e02119d5 CM |
842 | } |
843 | kfree(victim_name); | |
844 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; | |
845 | } | |
846 | BUG_ON(ret); | |
e02119d5 | 847 | |
c622ae60 | 848 | /* |
849 | * NOTE: we have searched root tree and checked the | |
850 | * coresponding ref, it does not need to check again. | |
851 | */ | |
5a1d7843 | 852 | *search_done = 1; |
e02119d5 | 853 | } |
b3b4aa74 | 854 | btrfs_release_path(path); |
e02119d5 | 855 | |
34f3e4f2 | 856 | /* look for a conflicting sequence number */ |
857 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), | |
858 | btrfs_inode_ref_index(eb, ref), | |
859 | name, namelen, 0); | |
860 | if (di && !IS_ERR(di)) { | |
861 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
862 | BUG_ON(ret); | |
863 | } | |
864 | btrfs_release_path(path); | |
865 | ||
866 | /* look for a conflicing name */ | |
867 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), | |
868 | name, namelen, 0); | |
869 | if (di && !IS_ERR(di)) { | |
870 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
871 | BUG_ON(ret); | |
872 | } | |
873 | btrfs_release_path(path); | |
874 | ||
5a1d7843 JS |
875 | return 0; |
876 | } | |
e02119d5 | 877 | |
5a1d7843 JS |
878 | /* |
879 | * replay one inode back reference item found in the log tree. | |
880 | * eb, slot and key refer to the buffer and key found in the log tree. | |
881 | * root is the destination we are replaying into, and path is for temp | |
882 | * use by this function. (it should be released on return). | |
883 | */ | |
884 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |
885 | struct btrfs_root *root, | |
886 | struct btrfs_root *log, | |
887 | struct btrfs_path *path, | |
888 | struct extent_buffer *eb, int slot, | |
889 | struct btrfs_key *key) | |
890 | { | |
891 | struct btrfs_inode_ref *ref; | |
892 | struct inode *dir; | |
893 | struct inode *inode; | |
894 | unsigned long ref_ptr; | |
895 | unsigned long ref_end; | |
896 | char *name; | |
897 | int namelen; | |
898 | int ret; | |
899 | int search_done = 0; | |
e02119d5 | 900 | |
5a1d7843 JS |
901 | /* |
902 | * it is possible that we didn't log all the parent directories | |
903 | * for a given inode. If we don't find the dir, just don't | |
904 | * copy the back ref in. The link count fixup code will take | |
905 | * care of the rest | |
906 | */ | |
907 | dir = read_one_inode(root, key->offset); | |
908 | if (!dir) | |
909 | return -ENOENT; | |
910 | ||
911 | inode = read_one_inode(root, key->objectid); | |
912 | if (!inode) { | |
913 | iput(dir); | |
914 | return -EIO; | |
915 | } | |
916 | ||
917 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | |
918 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); | |
919 | ||
920 | while (ref_ptr < ref_end) { | |
921 | ref = (struct btrfs_inode_ref *)ref_ptr; | |
922 | ||
923 | namelen = btrfs_inode_ref_name_len(eb, ref); | |
924 | name = kmalloc(namelen, GFP_NOFS); | |
925 | BUG_ON(!name); | |
926 | ||
927 | read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen); | |
928 | ||
929 | /* if we already have a perfect match, we're done */ | |
930 | if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode), | |
931 | btrfs_inode_ref_index(eb, ref), | |
932 | name, namelen)) { | |
933 | /* | |
934 | * look for a conflicting back reference in the | |
935 | * metadata. if we find one we have to unlink that name | |
936 | * of the file before we add our new link. Later on, we | |
937 | * overwrite any existing back reference, and we don't | |
938 | * want to create dangling pointers in the directory. | |
939 | */ | |
940 | ||
941 | if (!search_done) { | |
942 | ret = __add_inode_ref(trans, root, path, log, | |
943 | dir, inode, key, eb, ref, | |
944 | name, namelen, | |
945 | &search_done); | |
946 | if (ret == 1) | |
947 | goto out; | |
948 | BUG_ON(ret); | |
949 | } | |
950 | ||
951 | /* insert our name */ | |
952 | ret = btrfs_add_link(trans, dir, inode, name, namelen, | |
953 | 0, btrfs_inode_ref_index(eb, ref)); | |
954 | BUG_ON(ret); | |
955 | ||
956 | btrfs_update_inode(trans, root, inode); | |
957 | } | |
958 | ||
959 | ref_ptr = (unsigned long)(ref + 1) + namelen; | |
960 | kfree(name); | |
961 | } | |
e02119d5 CM |
962 | |
963 | /* finally write the back reference in the inode */ | |
964 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
965 | BUG_ON(ret); | |
966 | ||
5a1d7843 | 967 | out: |
b3b4aa74 | 968 | btrfs_release_path(path); |
e02119d5 CM |
969 | iput(dir); |
970 | iput(inode); | |
971 | return 0; | |
972 | } | |
973 | ||
c71bf099 YZ |
974 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
975 | struct btrfs_root *root, u64 offset) | |
976 | { | |
977 | int ret; | |
978 | ret = btrfs_find_orphan_item(root, offset); | |
979 | if (ret > 0) | |
980 | ret = btrfs_insert_orphan_item(trans, root, offset); | |
981 | return ret; | |
982 | } | |
983 | ||
984 | ||
e02119d5 CM |
985 | /* |
986 | * There are a few corners where the link count of the file can't | |
987 | * be properly maintained during replay. So, instead of adding | |
988 | * lots of complexity to the log code, we just scan the backrefs | |
989 | * for any file that has been through replay. | |
990 | * | |
991 | * The scan will update the link count on the inode to reflect the | |
992 | * number of back refs found. If it goes down to zero, the iput | |
993 | * will free the inode. | |
994 | */ | |
995 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | |
996 | struct btrfs_root *root, | |
997 | struct inode *inode) | |
998 | { | |
999 | struct btrfs_path *path; | |
1000 | int ret; | |
1001 | struct btrfs_key key; | |
1002 | u64 nlink = 0; | |
1003 | unsigned long ptr; | |
1004 | unsigned long ptr_end; | |
1005 | int name_len; | |
33345d01 | 1006 | u64 ino = btrfs_ino(inode); |
e02119d5 | 1007 | |
33345d01 | 1008 | key.objectid = ino; |
e02119d5 CM |
1009 | key.type = BTRFS_INODE_REF_KEY; |
1010 | key.offset = (u64)-1; | |
1011 | ||
1012 | path = btrfs_alloc_path(); | |
2a29edc6 | 1013 | if (!path) |
1014 | return -ENOMEM; | |
e02119d5 | 1015 | |
d397712b | 1016 | while (1) { |
e02119d5 CM |
1017 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1018 | if (ret < 0) | |
1019 | break; | |
1020 | if (ret > 0) { | |
1021 | if (path->slots[0] == 0) | |
1022 | break; | |
1023 | path->slots[0]--; | |
1024 | } | |
1025 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
1026 | path->slots[0]); | |
33345d01 | 1027 | if (key.objectid != ino || |
e02119d5 CM |
1028 | key.type != BTRFS_INODE_REF_KEY) |
1029 | break; | |
1030 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
1031 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], | |
1032 | path->slots[0]); | |
d397712b | 1033 | while (ptr < ptr_end) { |
e02119d5 CM |
1034 | struct btrfs_inode_ref *ref; |
1035 | ||
1036 | ref = (struct btrfs_inode_ref *)ptr; | |
1037 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | |
1038 | ref); | |
1039 | ptr = (unsigned long)(ref + 1) + name_len; | |
1040 | nlink++; | |
1041 | } | |
1042 | ||
1043 | if (key.offset == 0) | |
1044 | break; | |
1045 | key.offset--; | |
b3b4aa74 | 1046 | btrfs_release_path(path); |
e02119d5 | 1047 | } |
b3b4aa74 | 1048 | btrfs_release_path(path); |
e02119d5 | 1049 | if (nlink != inode->i_nlink) { |
bfe86848 | 1050 | set_nlink(inode, nlink); |
e02119d5 CM |
1051 | btrfs_update_inode(trans, root, inode); |
1052 | } | |
8d5bf1cb | 1053 | BTRFS_I(inode)->index_cnt = (u64)-1; |
e02119d5 | 1054 | |
c71bf099 YZ |
1055 | if (inode->i_nlink == 0) { |
1056 | if (S_ISDIR(inode->i_mode)) { | |
1057 | ret = replay_dir_deletes(trans, root, NULL, path, | |
33345d01 | 1058 | ino, 1); |
c71bf099 YZ |
1059 | BUG_ON(ret); |
1060 | } | |
33345d01 | 1061 | ret = insert_orphan_item(trans, root, ino); |
12fcfd22 CM |
1062 | BUG_ON(ret); |
1063 | } | |
1064 | btrfs_free_path(path); | |
1065 | ||
e02119d5 CM |
1066 | return 0; |
1067 | } | |
1068 | ||
1069 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | |
1070 | struct btrfs_root *root, | |
1071 | struct btrfs_path *path) | |
1072 | { | |
1073 | int ret; | |
1074 | struct btrfs_key key; | |
1075 | struct inode *inode; | |
1076 | ||
1077 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1078 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
1079 | key.offset = (u64)-1; | |
d397712b | 1080 | while (1) { |
e02119d5 CM |
1081 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1082 | if (ret < 0) | |
1083 | break; | |
1084 | ||
1085 | if (ret == 1) { | |
1086 | if (path->slots[0] == 0) | |
1087 | break; | |
1088 | path->slots[0]--; | |
1089 | } | |
1090 | ||
1091 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1092 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | |
1093 | key.type != BTRFS_ORPHAN_ITEM_KEY) | |
1094 | break; | |
1095 | ||
1096 | ret = btrfs_del_item(trans, root, path); | |
65a246c5 TI |
1097 | if (ret) |
1098 | goto out; | |
e02119d5 | 1099 | |
b3b4aa74 | 1100 | btrfs_release_path(path); |
e02119d5 | 1101 | inode = read_one_inode(root, key.offset); |
c00e9493 TI |
1102 | if (!inode) |
1103 | return -EIO; | |
e02119d5 CM |
1104 | |
1105 | ret = fixup_inode_link_count(trans, root, inode); | |
1106 | BUG_ON(ret); | |
1107 | ||
1108 | iput(inode); | |
1109 | ||
12fcfd22 CM |
1110 | /* |
1111 | * fixup on a directory may create new entries, | |
1112 | * make sure we always look for the highset possible | |
1113 | * offset | |
1114 | */ | |
1115 | key.offset = (u64)-1; | |
e02119d5 | 1116 | } |
65a246c5 TI |
1117 | ret = 0; |
1118 | out: | |
b3b4aa74 | 1119 | btrfs_release_path(path); |
65a246c5 | 1120 | return ret; |
e02119d5 CM |
1121 | } |
1122 | ||
1123 | ||
1124 | /* | |
1125 | * record a given inode in the fixup dir so we can check its link | |
1126 | * count when replay is done. The link count is incremented here | |
1127 | * so the inode won't go away until we check it | |
1128 | */ | |
1129 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | |
1130 | struct btrfs_root *root, | |
1131 | struct btrfs_path *path, | |
1132 | u64 objectid) | |
1133 | { | |
1134 | struct btrfs_key key; | |
1135 | int ret = 0; | |
1136 | struct inode *inode; | |
1137 | ||
1138 | inode = read_one_inode(root, objectid); | |
c00e9493 TI |
1139 | if (!inode) |
1140 | return -EIO; | |
e02119d5 CM |
1141 | |
1142 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1143 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | |
1144 | key.offset = objectid; | |
1145 | ||
1146 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
1147 | ||
b3b4aa74 | 1148 | btrfs_release_path(path); |
e02119d5 CM |
1149 | if (ret == 0) { |
1150 | btrfs_inc_nlink(inode); | |
b9959295 | 1151 | ret = btrfs_update_inode(trans, root, inode); |
e02119d5 CM |
1152 | } else if (ret == -EEXIST) { |
1153 | ret = 0; | |
1154 | } else { | |
1155 | BUG(); | |
1156 | } | |
1157 | iput(inode); | |
1158 | ||
1159 | return ret; | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * when replaying the log for a directory, we only insert names | |
1164 | * for inodes that actually exist. This means an fsync on a directory | |
1165 | * does not implicitly fsync all the new files in it | |
1166 | */ | |
1167 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | |
1168 | struct btrfs_root *root, | |
1169 | struct btrfs_path *path, | |
1170 | u64 dirid, u64 index, | |
1171 | char *name, int name_len, u8 type, | |
1172 | struct btrfs_key *location) | |
1173 | { | |
1174 | struct inode *inode; | |
1175 | struct inode *dir; | |
1176 | int ret; | |
1177 | ||
1178 | inode = read_one_inode(root, location->objectid); | |
1179 | if (!inode) | |
1180 | return -ENOENT; | |
1181 | ||
1182 | dir = read_one_inode(root, dirid); | |
1183 | if (!dir) { | |
1184 | iput(inode); | |
1185 | return -EIO; | |
1186 | } | |
1187 | ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index); | |
1188 | ||
1189 | /* FIXME, put inode into FIXUP list */ | |
1190 | ||
1191 | iput(inode); | |
1192 | iput(dir); | |
1193 | return ret; | |
1194 | } | |
1195 | ||
1196 | /* | |
1197 | * take a single entry in a log directory item and replay it into | |
1198 | * the subvolume. | |
1199 | * | |
1200 | * if a conflicting item exists in the subdirectory already, | |
1201 | * the inode it points to is unlinked and put into the link count | |
1202 | * fix up tree. | |
1203 | * | |
1204 | * If a name from the log points to a file or directory that does | |
1205 | * not exist in the FS, it is skipped. fsyncs on directories | |
1206 | * do not force down inodes inside that directory, just changes to the | |
1207 | * names or unlinks in a directory. | |
1208 | */ | |
1209 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |
1210 | struct btrfs_root *root, | |
1211 | struct btrfs_path *path, | |
1212 | struct extent_buffer *eb, | |
1213 | struct btrfs_dir_item *di, | |
1214 | struct btrfs_key *key) | |
1215 | { | |
1216 | char *name; | |
1217 | int name_len; | |
1218 | struct btrfs_dir_item *dst_di; | |
1219 | struct btrfs_key found_key; | |
1220 | struct btrfs_key log_key; | |
1221 | struct inode *dir; | |
e02119d5 | 1222 | u8 log_type; |
4bef0848 | 1223 | int exists; |
e02119d5 CM |
1224 | int ret; |
1225 | ||
1226 | dir = read_one_inode(root, key->objectid); | |
c00e9493 TI |
1227 | if (!dir) |
1228 | return -EIO; | |
e02119d5 CM |
1229 | |
1230 | name_len = btrfs_dir_name_len(eb, di); | |
1231 | name = kmalloc(name_len, GFP_NOFS); | |
2a29edc6 | 1232 | if (!name) |
1233 | return -ENOMEM; | |
1234 | ||
e02119d5 CM |
1235 | log_type = btrfs_dir_type(eb, di); |
1236 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1237 | name_len); | |
1238 | ||
1239 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); | |
4bef0848 CM |
1240 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
1241 | if (exists == 0) | |
1242 | exists = 1; | |
1243 | else | |
1244 | exists = 0; | |
b3b4aa74 | 1245 | btrfs_release_path(path); |
4bef0848 | 1246 | |
e02119d5 CM |
1247 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
1248 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, | |
1249 | name, name_len, 1); | |
d397712b | 1250 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
1251 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
1252 | key->objectid, | |
1253 | key->offset, name, | |
1254 | name_len, 1); | |
1255 | } else { | |
1256 | BUG(); | |
1257 | } | |
c704005d | 1258 | if (IS_ERR_OR_NULL(dst_di)) { |
e02119d5 CM |
1259 | /* we need a sequence number to insert, so we only |
1260 | * do inserts for the BTRFS_DIR_INDEX_KEY types | |
1261 | */ | |
1262 | if (key->type != BTRFS_DIR_INDEX_KEY) | |
1263 | goto out; | |
1264 | goto insert; | |
1265 | } | |
1266 | ||
1267 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | |
1268 | /* the existing item matches the logged item */ | |
1269 | if (found_key.objectid == log_key.objectid && | |
1270 | found_key.type == log_key.type && | |
1271 | found_key.offset == log_key.offset && | |
1272 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { | |
1273 | goto out; | |
1274 | } | |
1275 | ||
1276 | /* | |
1277 | * don't drop the conflicting directory entry if the inode | |
1278 | * for the new entry doesn't exist | |
1279 | */ | |
4bef0848 | 1280 | if (!exists) |
e02119d5 CM |
1281 | goto out; |
1282 | ||
e02119d5 CM |
1283 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); |
1284 | BUG_ON(ret); | |
1285 | ||
1286 | if (key->type == BTRFS_DIR_INDEX_KEY) | |
1287 | goto insert; | |
1288 | out: | |
b3b4aa74 | 1289 | btrfs_release_path(path); |
e02119d5 CM |
1290 | kfree(name); |
1291 | iput(dir); | |
1292 | return 0; | |
1293 | ||
1294 | insert: | |
b3b4aa74 | 1295 | btrfs_release_path(path); |
e02119d5 CM |
1296 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, |
1297 | name, name_len, log_type, &log_key); | |
1298 | ||
c293498b | 1299 | BUG_ON(ret && ret != -ENOENT); |
e02119d5 CM |
1300 | goto out; |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * find all the names in a directory item and reconcile them into | |
1305 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than | |
1306 | * one name in a directory item, but the same code gets used for | |
1307 | * both directory index types | |
1308 | */ | |
1309 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | |
1310 | struct btrfs_root *root, | |
1311 | struct btrfs_path *path, | |
1312 | struct extent_buffer *eb, int slot, | |
1313 | struct btrfs_key *key) | |
1314 | { | |
1315 | int ret; | |
1316 | u32 item_size = btrfs_item_size_nr(eb, slot); | |
1317 | struct btrfs_dir_item *di; | |
1318 | int name_len; | |
1319 | unsigned long ptr; | |
1320 | unsigned long ptr_end; | |
1321 | ||
1322 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1323 | ptr_end = ptr + item_size; | |
d397712b | 1324 | while (ptr < ptr_end) { |
e02119d5 | 1325 | di = (struct btrfs_dir_item *)ptr; |
22a94d44 JB |
1326 | if (verify_dir_item(root, eb, di)) |
1327 | return -EIO; | |
e02119d5 CM |
1328 | name_len = btrfs_dir_name_len(eb, di); |
1329 | ret = replay_one_name(trans, root, path, eb, di, key); | |
1330 | BUG_ON(ret); | |
1331 | ptr = (unsigned long)(di + 1); | |
1332 | ptr += name_len; | |
1333 | } | |
1334 | return 0; | |
1335 | } | |
1336 | ||
1337 | /* | |
1338 | * directory replay has two parts. There are the standard directory | |
1339 | * items in the log copied from the subvolume, and range items | |
1340 | * created in the log while the subvolume was logged. | |
1341 | * | |
1342 | * The range items tell us which parts of the key space the log | |
1343 | * is authoritative for. During replay, if a key in the subvolume | |
1344 | * directory is in a logged range item, but not actually in the log | |
1345 | * that means it was deleted from the directory before the fsync | |
1346 | * and should be removed. | |
1347 | */ | |
1348 | static noinline int find_dir_range(struct btrfs_root *root, | |
1349 | struct btrfs_path *path, | |
1350 | u64 dirid, int key_type, | |
1351 | u64 *start_ret, u64 *end_ret) | |
1352 | { | |
1353 | struct btrfs_key key; | |
1354 | u64 found_end; | |
1355 | struct btrfs_dir_log_item *item; | |
1356 | int ret; | |
1357 | int nritems; | |
1358 | ||
1359 | if (*start_ret == (u64)-1) | |
1360 | return 1; | |
1361 | ||
1362 | key.objectid = dirid; | |
1363 | key.type = key_type; | |
1364 | key.offset = *start_ret; | |
1365 | ||
1366 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1367 | if (ret < 0) | |
1368 | goto out; | |
1369 | if (ret > 0) { | |
1370 | if (path->slots[0] == 0) | |
1371 | goto out; | |
1372 | path->slots[0]--; | |
1373 | } | |
1374 | if (ret != 0) | |
1375 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1376 | ||
1377 | if (key.type != key_type || key.objectid != dirid) { | |
1378 | ret = 1; | |
1379 | goto next; | |
1380 | } | |
1381 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1382 | struct btrfs_dir_log_item); | |
1383 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1384 | ||
1385 | if (*start_ret >= key.offset && *start_ret <= found_end) { | |
1386 | ret = 0; | |
1387 | *start_ret = key.offset; | |
1388 | *end_ret = found_end; | |
1389 | goto out; | |
1390 | } | |
1391 | ret = 1; | |
1392 | next: | |
1393 | /* check the next slot in the tree to see if it is a valid item */ | |
1394 | nritems = btrfs_header_nritems(path->nodes[0]); | |
1395 | if (path->slots[0] >= nritems) { | |
1396 | ret = btrfs_next_leaf(root, path); | |
1397 | if (ret) | |
1398 | goto out; | |
1399 | } else { | |
1400 | path->slots[0]++; | |
1401 | } | |
1402 | ||
1403 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1404 | ||
1405 | if (key.type != key_type || key.objectid != dirid) { | |
1406 | ret = 1; | |
1407 | goto out; | |
1408 | } | |
1409 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1410 | struct btrfs_dir_log_item); | |
1411 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1412 | *start_ret = key.offset; | |
1413 | *end_ret = found_end; | |
1414 | ret = 0; | |
1415 | out: | |
b3b4aa74 | 1416 | btrfs_release_path(path); |
e02119d5 CM |
1417 | return ret; |
1418 | } | |
1419 | ||
1420 | /* | |
1421 | * this looks for a given directory item in the log. If the directory | |
1422 | * item is not in the log, the item is removed and the inode it points | |
1423 | * to is unlinked | |
1424 | */ | |
1425 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | |
1426 | struct btrfs_root *root, | |
1427 | struct btrfs_root *log, | |
1428 | struct btrfs_path *path, | |
1429 | struct btrfs_path *log_path, | |
1430 | struct inode *dir, | |
1431 | struct btrfs_key *dir_key) | |
1432 | { | |
1433 | int ret; | |
1434 | struct extent_buffer *eb; | |
1435 | int slot; | |
1436 | u32 item_size; | |
1437 | struct btrfs_dir_item *di; | |
1438 | struct btrfs_dir_item *log_di; | |
1439 | int name_len; | |
1440 | unsigned long ptr; | |
1441 | unsigned long ptr_end; | |
1442 | char *name; | |
1443 | struct inode *inode; | |
1444 | struct btrfs_key location; | |
1445 | ||
1446 | again: | |
1447 | eb = path->nodes[0]; | |
1448 | slot = path->slots[0]; | |
1449 | item_size = btrfs_item_size_nr(eb, slot); | |
1450 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1451 | ptr_end = ptr + item_size; | |
d397712b | 1452 | while (ptr < ptr_end) { |
e02119d5 | 1453 | di = (struct btrfs_dir_item *)ptr; |
22a94d44 JB |
1454 | if (verify_dir_item(root, eb, di)) { |
1455 | ret = -EIO; | |
1456 | goto out; | |
1457 | } | |
1458 | ||
e02119d5 CM |
1459 | name_len = btrfs_dir_name_len(eb, di); |
1460 | name = kmalloc(name_len, GFP_NOFS); | |
1461 | if (!name) { | |
1462 | ret = -ENOMEM; | |
1463 | goto out; | |
1464 | } | |
1465 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1466 | name_len); | |
1467 | log_di = NULL; | |
12fcfd22 | 1468 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
e02119d5 CM |
1469 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
1470 | dir_key->objectid, | |
1471 | name, name_len, 0); | |
12fcfd22 | 1472 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
1473 | log_di = btrfs_lookup_dir_index_item(trans, log, |
1474 | log_path, | |
1475 | dir_key->objectid, | |
1476 | dir_key->offset, | |
1477 | name, name_len, 0); | |
1478 | } | |
c704005d | 1479 | if (IS_ERR_OR_NULL(log_di)) { |
e02119d5 | 1480 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
b3b4aa74 DS |
1481 | btrfs_release_path(path); |
1482 | btrfs_release_path(log_path); | |
e02119d5 | 1483 | inode = read_one_inode(root, location.objectid); |
c00e9493 TI |
1484 | if (!inode) { |
1485 | kfree(name); | |
1486 | return -EIO; | |
1487 | } | |
e02119d5 CM |
1488 | |
1489 | ret = link_to_fixup_dir(trans, root, | |
1490 | path, location.objectid); | |
1491 | BUG_ON(ret); | |
1492 | btrfs_inc_nlink(inode); | |
1493 | ret = btrfs_unlink_inode(trans, root, dir, inode, | |
1494 | name, name_len); | |
1495 | BUG_ON(ret); | |
b6305567 CM |
1496 | |
1497 | btrfs_run_delayed_items(trans, root); | |
1498 | ||
e02119d5 CM |
1499 | kfree(name); |
1500 | iput(inode); | |
1501 | ||
1502 | /* there might still be more names under this key | |
1503 | * check and repeat if required | |
1504 | */ | |
1505 | ret = btrfs_search_slot(NULL, root, dir_key, path, | |
1506 | 0, 0); | |
1507 | if (ret == 0) | |
1508 | goto again; | |
1509 | ret = 0; | |
1510 | goto out; | |
1511 | } | |
b3b4aa74 | 1512 | btrfs_release_path(log_path); |
e02119d5 CM |
1513 | kfree(name); |
1514 | ||
1515 | ptr = (unsigned long)(di + 1); | |
1516 | ptr += name_len; | |
1517 | } | |
1518 | ret = 0; | |
1519 | out: | |
b3b4aa74 DS |
1520 | btrfs_release_path(path); |
1521 | btrfs_release_path(log_path); | |
e02119d5 CM |
1522 | return ret; |
1523 | } | |
1524 | ||
1525 | /* | |
1526 | * deletion replay happens before we copy any new directory items | |
1527 | * out of the log or out of backreferences from inodes. It | |
1528 | * scans the log to find ranges of keys that log is authoritative for, | |
1529 | * and then scans the directory to find items in those ranges that are | |
1530 | * not present in the log. | |
1531 | * | |
1532 | * Anything we don't find in the log is unlinked and removed from the | |
1533 | * directory. | |
1534 | */ | |
1535 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | |
1536 | struct btrfs_root *root, | |
1537 | struct btrfs_root *log, | |
1538 | struct btrfs_path *path, | |
12fcfd22 | 1539 | u64 dirid, int del_all) |
e02119d5 CM |
1540 | { |
1541 | u64 range_start; | |
1542 | u64 range_end; | |
1543 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; | |
1544 | int ret = 0; | |
1545 | struct btrfs_key dir_key; | |
1546 | struct btrfs_key found_key; | |
1547 | struct btrfs_path *log_path; | |
1548 | struct inode *dir; | |
1549 | ||
1550 | dir_key.objectid = dirid; | |
1551 | dir_key.type = BTRFS_DIR_ITEM_KEY; | |
1552 | log_path = btrfs_alloc_path(); | |
1553 | if (!log_path) | |
1554 | return -ENOMEM; | |
1555 | ||
1556 | dir = read_one_inode(root, dirid); | |
1557 | /* it isn't an error if the inode isn't there, that can happen | |
1558 | * because we replay the deletes before we copy in the inode item | |
1559 | * from the log | |
1560 | */ | |
1561 | if (!dir) { | |
1562 | btrfs_free_path(log_path); | |
1563 | return 0; | |
1564 | } | |
1565 | again: | |
1566 | range_start = 0; | |
1567 | range_end = 0; | |
d397712b | 1568 | while (1) { |
12fcfd22 CM |
1569 | if (del_all) |
1570 | range_end = (u64)-1; | |
1571 | else { | |
1572 | ret = find_dir_range(log, path, dirid, key_type, | |
1573 | &range_start, &range_end); | |
1574 | if (ret != 0) | |
1575 | break; | |
1576 | } | |
e02119d5 CM |
1577 | |
1578 | dir_key.offset = range_start; | |
d397712b | 1579 | while (1) { |
e02119d5 CM |
1580 | int nritems; |
1581 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | |
1582 | 0, 0); | |
1583 | if (ret < 0) | |
1584 | goto out; | |
1585 | ||
1586 | nritems = btrfs_header_nritems(path->nodes[0]); | |
1587 | if (path->slots[0] >= nritems) { | |
1588 | ret = btrfs_next_leaf(root, path); | |
1589 | if (ret) | |
1590 | break; | |
1591 | } | |
1592 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1593 | path->slots[0]); | |
1594 | if (found_key.objectid != dirid || | |
1595 | found_key.type != dir_key.type) | |
1596 | goto next_type; | |
1597 | ||
1598 | if (found_key.offset > range_end) | |
1599 | break; | |
1600 | ||
1601 | ret = check_item_in_log(trans, root, log, path, | |
12fcfd22 CM |
1602 | log_path, dir, |
1603 | &found_key); | |
e02119d5 CM |
1604 | BUG_ON(ret); |
1605 | if (found_key.offset == (u64)-1) | |
1606 | break; | |
1607 | dir_key.offset = found_key.offset + 1; | |
1608 | } | |
b3b4aa74 | 1609 | btrfs_release_path(path); |
e02119d5 CM |
1610 | if (range_end == (u64)-1) |
1611 | break; | |
1612 | range_start = range_end + 1; | |
1613 | } | |
1614 | ||
1615 | next_type: | |
1616 | ret = 0; | |
1617 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { | |
1618 | key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
1619 | dir_key.type = BTRFS_DIR_INDEX_KEY; | |
b3b4aa74 | 1620 | btrfs_release_path(path); |
e02119d5 CM |
1621 | goto again; |
1622 | } | |
1623 | out: | |
b3b4aa74 | 1624 | btrfs_release_path(path); |
e02119d5 CM |
1625 | btrfs_free_path(log_path); |
1626 | iput(dir); | |
1627 | return ret; | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * the process_func used to replay items from the log tree. This | |
1632 | * gets called in two different stages. The first stage just looks | |
1633 | * for inodes and makes sure they are all copied into the subvolume. | |
1634 | * | |
1635 | * The second stage copies all the other item types from the log into | |
1636 | * the subvolume. The two stage approach is slower, but gets rid of | |
1637 | * lots of complexity around inodes referencing other inodes that exist | |
1638 | * only in the log (references come from either directory items or inode | |
1639 | * back refs). | |
1640 | */ | |
1641 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |
1642 | struct walk_control *wc, u64 gen) | |
1643 | { | |
1644 | int nritems; | |
1645 | struct btrfs_path *path; | |
1646 | struct btrfs_root *root = wc->replay_dest; | |
1647 | struct btrfs_key key; | |
e02119d5 CM |
1648 | int level; |
1649 | int i; | |
1650 | int ret; | |
1651 | ||
018642a1 TI |
1652 | ret = btrfs_read_buffer(eb, gen); |
1653 | if (ret) | |
1654 | return ret; | |
e02119d5 CM |
1655 | |
1656 | level = btrfs_header_level(eb); | |
1657 | ||
1658 | if (level != 0) | |
1659 | return 0; | |
1660 | ||
1661 | path = btrfs_alloc_path(); | |
1e5063d0 MF |
1662 | if (!path) |
1663 | return -ENOMEM; | |
e02119d5 CM |
1664 | |
1665 | nritems = btrfs_header_nritems(eb); | |
1666 | for (i = 0; i < nritems; i++) { | |
1667 | btrfs_item_key_to_cpu(eb, &key, i); | |
e02119d5 CM |
1668 | |
1669 | /* inode keys are done during the first stage */ | |
1670 | if (key.type == BTRFS_INODE_ITEM_KEY && | |
1671 | wc->stage == LOG_WALK_REPLAY_INODES) { | |
e02119d5 CM |
1672 | struct btrfs_inode_item *inode_item; |
1673 | u32 mode; | |
1674 | ||
1675 | inode_item = btrfs_item_ptr(eb, i, | |
1676 | struct btrfs_inode_item); | |
1677 | mode = btrfs_inode_mode(eb, inode_item); | |
1678 | if (S_ISDIR(mode)) { | |
1679 | ret = replay_dir_deletes(wc->trans, | |
12fcfd22 | 1680 | root, log, path, key.objectid, 0); |
e02119d5 CM |
1681 | BUG_ON(ret); |
1682 | } | |
1683 | ret = overwrite_item(wc->trans, root, path, | |
1684 | eb, i, &key); | |
1685 | BUG_ON(ret); | |
1686 | ||
c71bf099 YZ |
1687 | /* for regular files, make sure corresponding |
1688 | * orhpan item exist. extents past the new EOF | |
1689 | * will be truncated later by orphan cleanup. | |
e02119d5 CM |
1690 | */ |
1691 | if (S_ISREG(mode)) { | |
c71bf099 YZ |
1692 | ret = insert_orphan_item(wc->trans, root, |
1693 | key.objectid); | |
e02119d5 | 1694 | BUG_ON(ret); |
e02119d5 | 1695 | } |
c71bf099 | 1696 | |
e02119d5 CM |
1697 | ret = link_to_fixup_dir(wc->trans, root, |
1698 | path, key.objectid); | |
1699 | BUG_ON(ret); | |
1700 | } | |
1701 | if (wc->stage < LOG_WALK_REPLAY_ALL) | |
1702 | continue; | |
1703 | ||
1704 | /* these keys are simply copied */ | |
1705 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | |
1706 | ret = overwrite_item(wc->trans, root, path, | |
1707 | eb, i, &key); | |
1708 | BUG_ON(ret); | |
1709 | } else if (key.type == BTRFS_INODE_REF_KEY) { | |
1710 | ret = add_inode_ref(wc->trans, root, log, path, | |
1711 | eb, i, &key); | |
1712 | BUG_ON(ret && ret != -ENOENT); | |
1713 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { | |
1714 | ret = replay_one_extent(wc->trans, root, path, | |
1715 | eb, i, &key); | |
1716 | BUG_ON(ret); | |
e02119d5 CM |
1717 | } else if (key.type == BTRFS_DIR_ITEM_KEY || |
1718 | key.type == BTRFS_DIR_INDEX_KEY) { | |
1719 | ret = replay_one_dir_item(wc->trans, root, path, | |
1720 | eb, i, &key); | |
1721 | BUG_ON(ret); | |
1722 | } | |
1723 | } | |
1724 | btrfs_free_path(path); | |
1725 | return 0; | |
1726 | } | |
1727 | ||
d397712b | 1728 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
1729 | struct btrfs_root *root, |
1730 | struct btrfs_path *path, int *level, | |
1731 | struct walk_control *wc) | |
1732 | { | |
1733 | u64 root_owner; | |
e02119d5 CM |
1734 | u64 bytenr; |
1735 | u64 ptr_gen; | |
1736 | struct extent_buffer *next; | |
1737 | struct extent_buffer *cur; | |
1738 | struct extent_buffer *parent; | |
1739 | u32 blocksize; | |
1740 | int ret = 0; | |
1741 | ||
1742 | WARN_ON(*level < 0); | |
1743 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1744 | ||
d397712b | 1745 | while (*level > 0) { |
e02119d5 CM |
1746 | WARN_ON(*level < 0); |
1747 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1748 | cur = path->nodes[*level]; | |
1749 | ||
1750 | if (btrfs_header_level(cur) != *level) | |
1751 | WARN_ON(1); | |
1752 | ||
1753 | if (path->slots[*level] >= | |
1754 | btrfs_header_nritems(cur)) | |
1755 | break; | |
1756 | ||
1757 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
1758 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
1759 | blocksize = btrfs_level_size(root, *level - 1); | |
1760 | ||
1761 | parent = path->nodes[*level]; | |
1762 | root_owner = btrfs_header_owner(parent); | |
e02119d5 CM |
1763 | |
1764 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
2a29edc6 | 1765 | if (!next) |
1766 | return -ENOMEM; | |
e02119d5 | 1767 | |
e02119d5 | 1768 | if (*level == 1) { |
1e5063d0 MF |
1769 | ret = wc->process_func(root, next, wc, ptr_gen); |
1770 | if (ret) | |
1771 | return ret; | |
4a500fd1 | 1772 | |
e02119d5 CM |
1773 | path->slots[*level]++; |
1774 | if (wc->free) { | |
018642a1 TI |
1775 | ret = btrfs_read_buffer(next, ptr_gen); |
1776 | if (ret) { | |
1777 | free_extent_buffer(next); | |
1778 | return ret; | |
1779 | } | |
e02119d5 CM |
1780 | |
1781 | btrfs_tree_lock(next); | |
b4ce94de | 1782 | btrfs_set_lock_blocking(next); |
bd681513 | 1783 | clean_tree_block(trans, root, next); |
e02119d5 CM |
1784 | btrfs_wait_tree_block_writeback(next); |
1785 | btrfs_tree_unlock(next); | |
1786 | ||
e02119d5 CM |
1787 | WARN_ON(root_owner != |
1788 | BTRFS_TREE_LOG_OBJECTID); | |
e688b725 | 1789 | ret = btrfs_free_and_pin_reserved_extent(root, |
d00aff00 | 1790 | bytenr, blocksize); |
79787eaa | 1791 | BUG_ON(ret); /* -ENOMEM or logic errors */ |
e02119d5 CM |
1792 | } |
1793 | free_extent_buffer(next); | |
1794 | continue; | |
1795 | } | |
018642a1 TI |
1796 | ret = btrfs_read_buffer(next, ptr_gen); |
1797 | if (ret) { | |
1798 | free_extent_buffer(next); | |
1799 | return ret; | |
1800 | } | |
e02119d5 CM |
1801 | |
1802 | WARN_ON(*level <= 0); | |
1803 | if (path->nodes[*level-1]) | |
1804 | free_extent_buffer(path->nodes[*level-1]); | |
1805 | path->nodes[*level-1] = next; | |
1806 | *level = btrfs_header_level(next); | |
1807 | path->slots[*level] = 0; | |
1808 | cond_resched(); | |
1809 | } | |
1810 | WARN_ON(*level < 0); | |
1811 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
1812 | ||
4a500fd1 | 1813 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
e02119d5 CM |
1814 | |
1815 | cond_resched(); | |
1816 | return 0; | |
1817 | } | |
1818 | ||
d397712b | 1819 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
1820 | struct btrfs_root *root, |
1821 | struct btrfs_path *path, int *level, | |
1822 | struct walk_control *wc) | |
1823 | { | |
1824 | u64 root_owner; | |
e02119d5 CM |
1825 | int i; |
1826 | int slot; | |
1827 | int ret; | |
1828 | ||
d397712b | 1829 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
e02119d5 | 1830 | slot = path->slots[i]; |
4a500fd1 | 1831 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
e02119d5 CM |
1832 | path->slots[i]++; |
1833 | *level = i; | |
1834 | WARN_ON(*level == 0); | |
1835 | return 0; | |
1836 | } else { | |
31840ae1 ZY |
1837 | struct extent_buffer *parent; |
1838 | if (path->nodes[*level] == root->node) | |
1839 | parent = path->nodes[*level]; | |
1840 | else | |
1841 | parent = path->nodes[*level + 1]; | |
1842 | ||
1843 | root_owner = btrfs_header_owner(parent); | |
1e5063d0 | 1844 | ret = wc->process_func(root, path->nodes[*level], wc, |
e02119d5 | 1845 | btrfs_header_generation(path->nodes[*level])); |
1e5063d0 MF |
1846 | if (ret) |
1847 | return ret; | |
1848 | ||
e02119d5 CM |
1849 | if (wc->free) { |
1850 | struct extent_buffer *next; | |
1851 | ||
1852 | next = path->nodes[*level]; | |
1853 | ||
1854 | btrfs_tree_lock(next); | |
b4ce94de | 1855 | btrfs_set_lock_blocking(next); |
bd681513 | 1856 | clean_tree_block(trans, root, next); |
e02119d5 CM |
1857 | btrfs_wait_tree_block_writeback(next); |
1858 | btrfs_tree_unlock(next); | |
1859 | ||
e02119d5 | 1860 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
e688b725 | 1861 | ret = btrfs_free_and_pin_reserved_extent(root, |
e02119d5 | 1862 | path->nodes[*level]->start, |
d00aff00 | 1863 | path->nodes[*level]->len); |
e02119d5 CM |
1864 | BUG_ON(ret); |
1865 | } | |
1866 | free_extent_buffer(path->nodes[*level]); | |
1867 | path->nodes[*level] = NULL; | |
1868 | *level = i + 1; | |
1869 | } | |
1870 | } | |
1871 | return 1; | |
1872 | } | |
1873 | ||
1874 | /* | |
1875 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
1876 | * the tree freeing any blocks that have a ref count of zero after being | |
1877 | * decremented. | |
1878 | */ | |
1879 | static int walk_log_tree(struct btrfs_trans_handle *trans, | |
1880 | struct btrfs_root *log, struct walk_control *wc) | |
1881 | { | |
1882 | int ret = 0; | |
1883 | int wret; | |
1884 | int level; | |
1885 | struct btrfs_path *path; | |
1886 | int i; | |
1887 | int orig_level; | |
1888 | ||
1889 | path = btrfs_alloc_path(); | |
db5b493a TI |
1890 | if (!path) |
1891 | return -ENOMEM; | |
e02119d5 CM |
1892 | |
1893 | level = btrfs_header_level(log->node); | |
1894 | orig_level = level; | |
1895 | path->nodes[level] = log->node; | |
1896 | extent_buffer_get(log->node); | |
1897 | path->slots[level] = 0; | |
1898 | ||
d397712b | 1899 | while (1) { |
e02119d5 CM |
1900 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
1901 | if (wret > 0) | |
1902 | break; | |
79787eaa | 1903 | if (wret < 0) { |
e02119d5 | 1904 | ret = wret; |
79787eaa JM |
1905 | goto out; |
1906 | } | |
e02119d5 CM |
1907 | |
1908 | wret = walk_up_log_tree(trans, log, path, &level, wc); | |
1909 | if (wret > 0) | |
1910 | break; | |
79787eaa | 1911 | if (wret < 0) { |
e02119d5 | 1912 | ret = wret; |
79787eaa JM |
1913 | goto out; |
1914 | } | |
e02119d5 CM |
1915 | } |
1916 | ||
1917 | /* was the root node processed? if not, catch it here */ | |
1918 | if (path->nodes[orig_level]) { | |
79787eaa | 1919 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
e02119d5 | 1920 | btrfs_header_generation(path->nodes[orig_level])); |
79787eaa JM |
1921 | if (ret) |
1922 | goto out; | |
e02119d5 CM |
1923 | if (wc->free) { |
1924 | struct extent_buffer *next; | |
1925 | ||
1926 | next = path->nodes[orig_level]; | |
1927 | ||
1928 | btrfs_tree_lock(next); | |
b4ce94de | 1929 | btrfs_set_lock_blocking(next); |
bd681513 | 1930 | clean_tree_block(trans, log, next); |
e02119d5 CM |
1931 | btrfs_wait_tree_block_writeback(next); |
1932 | btrfs_tree_unlock(next); | |
1933 | ||
e02119d5 CM |
1934 | WARN_ON(log->root_key.objectid != |
1935 | BTRFS_TREE_LOG_OBJECTID); | |
e688b725 | 1936 | ret = btrfs_free_and_pin_reserved_extent(log, next->start, |
d00aff00 | 1937 | next->len); |
79787eaa | 1938 | BUG_ON(ret); /* -ENOMEM or logic errors */ |
e02119d5 CM |
1939 | } |
1940 | } | |
1941 | ||
79787eaa | 1942 | out: |
e02119d5 CM |
1943 | for (i = 0; i <= orig_level; i++) { |
1944 | if (path->nodes[i]) { | |
1945 | free_extent_buffer(path->nodes[i]); | |
1946 | path->nodes[i] = NULL; | |
1947 | } | |
1948 | } | |
1949 | btrfs_free_path(path); | |
e02119d5 CM |
1950 | return ret; |
1951 | } | |
1952 | ||
7237f183 YZ |
1953 | /* |
1954 | * helper function to update the item for a given subvolumes log root | |
1955 | * in the tree of log roots | |
1956 | */ | |
1957 | static int update_log_root(struct btrfs_trans_handle *trans, | |
1958 | struct btrfs_root *log) | |
1959 | { | |
1960 | int ret; | |
1961 | ||
1962 | if (log->log_transid == 1) { | |
1963 | /* insert root item on the first sync */ | |
1964 | ret = btrfs_insert_root(trans, log->fs_info->log_root_tree, | |
1965 | &log->root_key, &log->root_item); | |
1966 | } else { | |
1967 | ret = btrfs_update_root(trans, log->fs_info->log_root_tree, | |
1968 | &log->root_key, &log->root_item); | |
1969 | } | |
1970 | return ret; | |
1971 | } | |
1972 | ||
12fcfd22 CM |
1973 | static int wait_log_commit(struct btrfs_trans_handle *trans, |
1974 | struct btrfs_root *root, unsigned long transid) | |
e02119d5 CM |
1975 | { |
1976 | DEFINE_WAIT(wait); | |
7237f183 | 1977 | int index = transid % 2; |
e02119d5 | 1978 | |
7237f183 YZ |
1979 | /* |
1980 | * we only allow two pending log transactions at a time, | |
1981 | * so we know that if ours is more than 2 older than the | |
1982 | * current transaction, we're done | |
1983 | */ | |
e02119d5 | 1984 | do { |
7237f183 YZ |
1985 | prepare_to_wait(&root->log_commit_wait[index], |
1986 | &wait, TASK_UNINTERRUPTIBLE); | |
1987 | mutex_unlock(&root->log_mutex); | |
12fcfd22 CM |
1988 | |
1989 | if (root->fs_info->last_trans_log_full_commit != | |
1990 | trans->transid && root->log_transid < transid + 2 && | |
7237f183 YZ |
1991 | atomic_read(&root->log_commit[index])) |
1992 | schedule(); | |
12fcfd22 | 1993 | |
7237f183 YZ |
1994 | finish_wait(&root->log_commit_wait[index], &wait); |
1995 | mutex_lock(&root->log_mutex); | |
6dd70ce4 JK |
1996 | } while (root->fs_info->last_trans_log_full_commit != |
1997 | trans->transid && root->log_transid < transid + 2 && | |
7237f183 YZ |
1998 | atomic_read(&root->log_commit[index])); |
1999 | return 0; | |
2000 | } | |
2001 | ||
143bede5 JM |
2002 | static void wait_for_writer(struct btrfs_trans_handle *trans, |
2003 | struct btrfs_root *root) | |
7237f183 YZ |
2004 | { |
2005 | DEFINE_WAIT(wait); | |
6dd70ce4 JK |
2006 | while (root->fs_info->last_trans_log_full_commit != |
2007 | trans->transid && atomic_read(&root->log_writers)) { | |
7237f183 YZ |
2008 | prepare_to_wait(&root->log_writer_wait, |
2009 | &wait, TASK_UNINTERRUPTIBLE); | |
2010 | mutex_unlock(&root->log_mutex); | |
12fcfd22 CM |
2011 | if (root->fs_info->last_trans_log_full_commit != |
2012 | trans->transid && atomic_read(&root->log_writers)) | |
e02119d5 | 2013 | schedule(); |
7237f183 YZ |
2014 | mutex_lock(&root->log_mutex); |
2015 | finish_wait(&root->log_writer_wait, &wait); | |
2016 | } | |
e02119d5 CM |
2017 | } |
2018 | ||
2019 | /* | |
2020 | * btrfs_sync_log does sends a given tree log down to the disk and | |
2021 | * updates the super blocks to record it. When this call is done, | |
12fcfd22 CM |
2022 | * you know that any inodes previously logged are safely on disk only |
2023 | * if it returns 0. | |
2024 | * | |
2025 | * Any other return value means you need to call btrfs_commit_transaction. | |
2026 | * Some of the edge cases for fsyncing directories that have had unlinks | |
2027 | * or renames done in the past mean that sometimes the only safe | |
2028 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, | |
2029 | * that has happened. | |
e02119d5 CM |
2030 | */ |
2031 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | |
2032 | struct btrfs_root *root) | |
2033 | { | |
7237f183 YZ |
2034 | int index1; |
2035 | int index2; | |
8cef4e16 | 2036 | int mark; |
e02119d5 | 2037 | int ret; |
e02119d5 | 2038 | struct btrfs_root *log = root->log_root; |
7237f183 | 2039 | struct btrfs_root *log_root_tree = root->fs_info->log_root_tree; |
8cef4e16 | 2040 | unsigned long log_transid = 0; |
e02119d5 | 2041 | |
7237f183 YZ |
2042 | mutex_lock(&root->log_mutex); |
2043 | index1 = root->log_transid % 2; | |
2044 | if (atomic_read(&root->log_commit[index1])) { | |
12fcfd22 | 2045 | wait_log_commit(trans, root, root->log_transid); |
7237f183 YZ |
2046 | mutex_unlock(&root->log_mutex); |
2047 | return 0; | |
e02119d5 | 2048 | } |
7237f183 YZ |
2049 | atomic_set(&root->log_commit[index1], 1); |
2050 | ||
2051 | /* wait for previous tree log sync to complete */ | |
2052 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) | |
12fcfd22 | 2053 | wait_log_commit(trans, root, root->log_transid - 1); |
86df7eb9 | 2054 | while (1) { |
2ecb7923 | 2055 | int batch = atomic_read(&root->log_batch); |
cd354ad6 CM |
2056 | /* when we're on an ssd, just kick the log commit out */ |
2057 | if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) { | |
86df7eb9 YZ |
2058 | mutex_unlock(&root->log_mutex); |
2059 | schedule_timeout_uninterruptible(1); | |
2060 | mutex_lock(&root->log_mutex); | |
2061 | } | |
12fcfd22 | 2062 | wait_for_writer(trans, root); |
2ecb7923 | 2063 | if (batch == atomic_read(&root->log_batch)) |
e02119d5 CM |
2064 | break; |
2065 | } | |
e02119d5 | 2066 | |
12fcfd22 CM |
2067 | /* bail out if we need to do a full commit */ |
2068 | if (root->fs_info->last_trans_log_full_commit == trans->transid) { | |
2069 | ret = -EAGAIN; | |
2070 | mutex_unlock(&root->log_mutex); | |
2071 | goto out; | |
2072 | } | |
2073 | ||
8cef4e16 YZ |
2074 | log_transid = root->log_transid; |
2075 | if (log_transid % 2 == 0) | |
2076 | mark = EXTENT_DIRTY; | |
2077 | else | |
2078 | mark = EXTENT_NEW; | |
2079 | ||
690587d1 CM |
2080 | /* we start IO on all the marked extents here, but we don't actually |
2081 | * wait for them until later. | |
2082 | */ | |
8cef4e16 | 2083 | ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark); |
79787eaa JM |
2084 | if (ret) { |
2085 | btrfs_abort_transaction(trans, root, ret); | |
2086 | mutex_unlock(&root->log_mutex); | |
2087 | goto out; | |
2088 | } | |
7237f183 | 2089 | |
5d4f98a2 | 2090 | btrfs_set_root_node(&log->root_item, log->node); |
7237f183 | 2091 | |
7237f183 YZ |
2092 | root->log_transid++; |
2093 | log->log_transid = root->log_transid; | |
ff782e0a | 2094 | root->log_start_pid = 0; |
7237f183 YZ |
2095 | smp_mb(); |
2096 | /* | |
8cef4e16 YZ |
2097 | * IO has been started, blocks of the log tree have WRITTEN flag set |
2098 | * in their headers. new modifications of the log will be written to | |
2099 | * new positions. so it's safe to allow log writers to go in. | |
7237f183 YZ |
2100 | */ |
2101 | mutex_unlock(&root->log_mutex); | |
2102 | ||
2103 | mutex_lock(&log_root_tree->log_mutex); | |
2ecb7923 | 2104 | atomic_inc(&log_root_tree->log_batch); |
7237f183 YZ |
2105 | atomic_inc(&log_root_tree->log_writers); |
2106 | mutex_unlock(&log_root_tree->log_mutex); | |
2107 | ||
2108 | ret = update_log_root(trans, log); | |
7237f183 YZ |
2109 | |
2110 | mutex_lock(&log_root_tree->log_mutex); | |
2111 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { | |
2112 | smp_mb(); | |
2113 | if (waitqueue_active(&log_root_tree->log_writer_wait)) | |
2114 | wake_up(&log_root_tree->log_writer_wait); | |
2115 | } | |
2116 | ||
4a500fd1 | 2117 | if (ret) { |
79787eaa JM |
2118 | if (ret != -ENOSPC) { |
2119 | btrfs_abort_transaction(trans, root, ret); | |
2120 | mutex_unlock(&log_root_tree->log_mutex); | |
2121 | goto out; | |
2122 | } | |
4a500fd1 YZ |
2123 | root->fs_info->last_trans_log_full_commit = trans->transid; |
2124 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); | |
2125 | mutex_unlock(&log_root_tree->log_mutex); | |
2126 | ret = -EAGAIN; | |
2127 | goto out; | |
2128 | } | |
2129 | ||
7237f183 YZ |
2130 | index2 = log_root_tree->log_transid % 2; |
2131 | if (atomic_read(&log_root_tree->log_commit[index2])) { | |
8cef4e16 | 2132 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
12fcfd22 CM |
2133 | wait_log_commit(trans, log_root_tree, |
2134 | log_root_tree->log_transid); | |
7237f183 | 2135 | mutex_unlock(&log_root_tree->log_mutex); |
b31eabd8 | 2136 | ret = 0; |
7237f183 YZ |
2137 | goto out; |
2138 | } | |
2139 | atomic_set(&log_root_tree->log_commit[index2], 1); | |
2140 | ||
12fcfd22 CM |
2141 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
2142 | wait_log_commit(trans, log_root_tree, | |
2143 | log_root_tree->log_transid - 1); | |
2144 | } | |
2145 | ||
2146 | wait_for_writer(trans, log_root_tree); | |
7237f183 | 2147 | |
12fcfd22 CM |
2148 | /* |
2149 | * now that we've moved on to the tree of log tree roots, | |
2150 | * check the full commit flag again | |
2151 | */ | |
2152 | if (root->fs_info->last_trans_log_full_commit == trans->transid) { | |
8cef4e16 | 2153 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
12fcfd22 CM |
2154 | mutex_unlock(&log_root_tree->log_mutex); |
2155 | ret = -EAGAIN; | |
2156 | goto out_wake_log_root; | |
2157 | } | |
7237f183 YZ |
2158 | |
2159 | ret = btrfs_write_and_wait_marked_extents(log_root_tree, | |
8cef4e16 YZ |
2160 | &log_root_tree->dirty_log_pages, |
2161 | EXTENT_DIRTY | EXTENT_NEW); | |
79787eaa JM |
2162 | if (ret) { |
2163 | btrfs_abort_transaction(trans, root, ret); | |
2164 | mutex_unlock(&log_root_tree->log_mutex); | |
2165 | goto out_wake_log_root; | |
2166 | } | |
8cef4e16 | 2167 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
e02119d5 | 2168 | |
6c41761f | 2169 | btrfs_set_super_log_root(root->fs_info->super_for_commit, |
7237f183 | 2170 | log_root_tree->node->start); |
6c41761f | 2171 | btrfs_set_super_log_root_level(root->fs_info->super_for_commit, |
7237f183 | 2172 | btrfs_header_level(log_root_tree->node)); |
e02119d5 | 2173 | |
7237f183 | 2174 | log_root_tree->log_transid++; |
e02119d5 | 2175 | smp_mb(); |
7237f183 YZ |
2176 | |
2177 | mutex_unlock(&log_root_tree->log_mutex); | |
2178 | ||
2179 | /* | |
2180 | * nobody else is going to jump in and write the the ctree | |
2181 | * super here because the log_commit atomic below is protecting | |
2182 | * us. We must be called with a transaction handle pinning | |
2183 | * the running transaction open, so a full commit can't hop | |
2184 | * in and cause problems either. | |
2185 | */ | |
a2de733c | 2186 | btrfs_scrub_pause_super(root); |
4722607d | 2187 | write_ctree_super(trans, root->fs_info->tree_root, 1); |
a2de733c | 2188 | btrfs_scrub_continue_super(root); |
12fcfd22 | 2189 | ret = 0; |
7237f183 | 2190 | |
257c62e1 CM |
2191 | mutex_lock(&root->log_mutex); |
2192 | if (root->last_log_commit < log_transid) | |
2193 | root->last_log_commit = log_transid; | |
2194 | mutex_unlock(&root->log_mutex); | |
2195 | ||
12fcfd22 | 2196 | out_wake_log_root: |
7237f183 YZ |
2197 | atomic_set(&log_root_tree->log_commit[index2], 0); |
2198 | smp_mb(); | |
2199 | if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) | |
2200 | wake_up(&log_root_tree->log_commit_wait[index2]); | |
e02119d5 | 2201 | out: |
7237f183 YZ |
2202 | atomic_set(&root->log_commit[index1], 0); |
2203 | smp_mb(); | |
2204 | if (waitqueue_active(&root->log_commit_wait[index1])) | |
2205 | wake_up(&root->log_commit_wait[index1]); | |
b31eabd8 | 2206 | return ret; |
e02119d5 CM |
2207 | } |
2208 | ||
4a500fd1 YZ |
2209 | static void free_log_tree(struct btrfs_trans_handle *trans, |
2210 | struct btrfs_root *log) | |
e02119d5 CM |
2211 | { |
2212 | int ret; | |
d0c803c4 CM |
2213 | u64 start; |
2214 | u64 end; | |
e02119d5 CM |
2215 | struct walk_control wc = { |
2216 | .free = 1, | |
2217 | .process_func = process_one_buffer | |
2218 | }; | |
2219 | ||
e02119d5 CM |
2220 | ret = walk_log_tree(trans, log, &wc); |
2221 | BUG_ON(ret); | |
2222 | ||
d397712b | 2223 | while (1) { |
d0c803c4 | 2224 | ret = find_first_extent_bit(&log->dirty_log_pages, |
8cef4e16 | 2225 | 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW); |
d0c803c4 CM |
2226 | if (ret) |
2227 | break; | |
2228 | ||
8cef4e16 YZ |
2229 | clear_extent_bits(&log->dirty_log_pages, start, end, |
2230 | EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS); | |
d0c803c4 CM |
2231 | } |
2232 | ||
7237f183 YZ |
2233 | free_extent_buffer(log->node); |
2234 | kfree(log); | |
4a500fd1 YZ |
2235 | } |
2236 | ||
2237 | /* | |
2238 | * free all the extents used by the tree log. This should be called | |
2239 | * at commit time of the full transaction | |
2240 | */ | |
2241 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | |
2242 | { | |
2243 | if (root->log_root) { | |
2244 | free_log_tree(trans, root->log_root); | |
2245 | root->log_root = NULL; | |
2246 | } | |
2247 | return 0; | |
2248 | } | |
2249 | ||
2250 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | |
2251 | struct btrfs_fs_info *fs_info) | |
2252 | { | |
2253 | if (fs_info->log_root_tree) { | |
2254 | free_log_tree(trans, fs_info->log_root_tree); | |
2255 | fs_info->log_root_tree = NULL; | |
2256 | } | |
e02119d5 CM |
2257 | return 0; |
2258 | } | |
2259 | ||
e02119d5 CM |
2260 | /* |
2261 | * If both a file and directory are logged, and unlinks or renames are | |
2262 | * mixed in, we have a few interesting corners: | |
2263 | * | |
2264 | * create file X in dir Y | |
2265 | * link file X to X.link in dir Y | |
2266 | * fsync file X | |
2267 | * unlink file X but leave X.link | |
2268 | * fsync dir Y | |
2269 | * | |
2270 | * After a crash we would expect only X.link to exist. But file X | |
2271 | * didn't get fsync'd again so the log has back refs for X and X.link. | |
2272 | * | |
2273 | * We solve this by removing directory entries and inode backrefs from the | |
2274 | * log when a file that was logged in the current transaction is | |
2275 | * unlinked. Any later fsync will include the updated log entries, and | |
2276 | * we'll be able to reconstruct the proper directory items from backrefs. | |
2277 | * | |
2278 | * This optimizations allows us to avoid relogging the entire inode | |
2279 | * or the entire directory. | |
2280 | */ | |
2281 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | |
2282 | struct btrfs_root *root, | |
2283 | const char *name, int name_len, | |
2284 | struct inode *dir, u64 index) | |
2285 | { | |
2286 | struct btrfs_root *log; | |
2287 | struct btrfs_dir_item *di; | |
2288 | struct btrfs_path *path; | |
2289 | int ret; | |
4a500fd1 | 2290 | int err = 0; |
e02119d5 | 2291 | int bytes_del = 0; |
33345d01 | 2292 | u64 dir_ino = btrfs_ino(dir); |
e02119d5 | 2293 | |
3a5f1d45 CM |
2294 | if (BTRFS_I(dir)->logged_trans < trans->transid) |
2295 | return 0; | |
2296 | ||
e02119d5 CM |
2297 | ret = join_running_log_trans(root); |
2298 | if (ret) | |
2299 | return 0; | |
2300 | ||
2301 | mutex_lock(&BTRFS_I(dir)->log_mutex); | |
2302 | ||
2303 | log = root->log_root; | |
2304 | path = btrfs_alloc_path(); | |
a62f44a5 TI |
2305 | if (!path) { |
2306 | err = -ENOMEM; | |
2307 | goto out_unlock; | |
2308 | } | |
2a29edc6 | 2309 | |
33345d01 | 2310 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
e02119d5 | 2311 | name, name_len, -1); |
4a500fd1 YZ |
2312 | if (IS_ERR(di)) { |
2313 | err = PTR_ERR(di); | |
2314 | goto fail; | |
2315 | } | |
2316 | if (di) { | |
e02119d5 CM |
2317 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2318 | bytes_del += name_len; | |
2319 | BUG_ON(ret); | |
2320 | } | |
b3b4aa74 | 2321 | btrfs_release_path(path); |
33345d01 | 2322 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
e02119d5 | 2323 | index, name, name_len, -1); |
4a500fd1 YZ |
2324 | if (IS_ERR(di)) { |
2325 | err = PTR_ERR(di); | |
2326 | goto fail; | |
2327 | } | |
2328 | if (di) { | |
e02119d5 CM |
2329 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2330 | bytes_del += name_len; | |
2331 | BUG_ON(ret); | |
2332 | } | |
2333 | ||
2334 | /* update the directory size in the log to reflect the names | |
2335 | * we have removed | |
2336 | */ | |
2337 | if (bytes_del) { | |
2338 | struct btrfs_key key; | |
2339 | ||
33345d01 | 2340 | key.objectid = dir_ino; |
e02119d5 CM |
2341 | key.offset = 0; |
2342 | key.type = BTRFS_INODE_ITEM_KEY; | |
b3b4aa74 | 2343 | btrfs_release_path(path); |
e02119d5 CM |
2344 | |
2345 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); | |
4a500fd1 YZ |
2346 | if (ret < 0) { |
2347 | err = ret; | |
2348 | goto fail; | |
2349 | } | |
e02119d5 CM |
2350 | if (ret == 0) { |
2351 | struct btrfs_inode_item *item; | |
2352 | u64 i_size; | |
2353 | ||
2354 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2355 | struct btrfs_inode_item); | |
2356 | i_size = btrfs_inode_size(path->nodes[0], item); | |
2357 | if (i_size > bytes_del) | |
2358 | i_size -= bytes_del; | |
2359 | else | |
2360 | i_size = 0; | |
2361 | btrfs_set_inode_size(path->nodes[0], item, i_size); | |
2362 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2363 | } else | |
2364 | ret = 0; | |
b3b4aa74 | 2365 | btrfs_release_path(path); |
e02119d5 | 2366 | } |
4a500fd1 | 2367 | fail: |
e02119d5 | 2368 | btrfs_free_path(path); |
a62f44a5 | 2369 | out_unlock: |
e02119d5 | 2370 | mutex_unlock(&BTRFS_I(dir)->log_mutex); |
4a500fd1 YZ |
2371 | if (ret == -ENOSPC) { |
2372 | root->fs_info->last_trans_log_full_commit = trans->transid; | |
2373 | ret = 0; | |
79787eaa JM |
2374 | } else if (ret < 0) |
2375 | btrfs_abort_transaction(trans, root, ret); | |
2376 | ||
12fcfd22 | 2377 | btrfs_end_log_trans(root); |
e02119d5 | 2378 | |
411fc6bc | 2379 | return err; |
e02119d5 CM |
2380 | } |
2381 | ||
2382 | /* see comments for btrfs_del_dir_entries_in_log */ | |
2383 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | |
2384 | struct btrfs_root *root, | |
2385 | const char *name, int name_len, | |
2386 | struct inode *inode, u64 dirid) | |
2387 | { | |
2388 | struct btrfs_root *log; | |
2389 | u64 index; | |
2390 | int ret; | |
2391 | ||
3a5f1d45 CM |
2392 | if (BTRFS_I(inode)->logged_trans < trans->transid) |
2393 | return 0; | |
2394 | ||
e02119d5 CM |
2395 | ret = join_running_log_trans(root); |
2396 | if (ret) | |
2397 | return 0; | |
2398 | log = root->log_root; | |
2399 | mutex_lock(&BTRFS_I(inode)->log_mutex); | |
2400 | ||
33345d01 | 2401 | ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode), |
e02119d5 CM |
2402 | dirid, &index); |
2403 | mutex_unlock(&BTRFS_I(inode)->log_mutex); | |
4a500fd1 YZ |
2404 | if (ret == -ENOSPC) { |
2405 | root->fs_info->last_trans_log_full_commit = trans->transid; | |
2406 | ret = 0; | |
79787eaa JM |
2407 | } else if (ret < 0 && ret != -ENOENT) |
2408 | btrfs_abort_transaction(trans, root, ret); | |
12fcfd22 | 2409 | btrfs_end_log_trans(root); |
e02119d5 | 2410 | |
e02119d5 CM |
2411 | return ret; |
2412 | } | |
2413 | ||
2414 | /* | |
2415 | * creates a range item in the log for 'dirid'. first_offset and | |
2416 | * last_offset tell us which parts of the key space the log should | |
2417 | * be considered authoritative for. | |
2418 | */ | |
2419 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | |
2420 | struct btrfs_root *log, | |
2421 | struct btrfs_path *path, | |
2422 | int key_type, u64 dirid, | |
2423 | u64 first_offset, u64 last_offset) | |
2424 | { | |
2425 | int ret; | |
2426 | struct btrfs_key key; | |
2427 | struct btrfs_dir_log_item *item; | |
2428 | ||
2429 | key.objectid = dirid; | |
2430 | key.offset = first_offset; | |
2431 | if (key_type == BTRFS_DIR_ITEM_KEY) | |
2432 | key.type = BTRFS_DIR_LOG_ITEM_KEY; | |
2433 | else | |
2434 | key.type = BTRFS_DIR_LOG_INDEX_KEY; | |
2435 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); | |
4a500fd1 YZ |
2436 | if (ret) |
2437 | return ret; | |
e02119d5 CM |
2438 | |
2439 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2440 | struct btrfs_dir_log_item); | |
2441 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); | |
2442 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 2443 | btrfs_release_path(path); |
e02119d5 CM |
2444 | return 0; |
2445 | } | |
2446 | ||
2447 | /* | |
2448 | * log all the items included in the current transaction for a given | |
2449 | * directory. This also creates the range items in the log tree required | |
2450 | * to replay anything deleted before the fsync | |
2451 | */ | |
2452 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | |
2453 | struct btrfs_root *root, struct inode *inode, | |
2454 | struct btrfs_path *path, | |
2455 | struct btrfs_path *dst_path, int key_type, | |
2456 | u64 min_offset, u64 *last_offset_ret) | |
2457 | { | |
2458 | struct btrfs_key min_key; | |
2459 | struct btrfs_key max_key; | |
2460 | struct btrfs_root *log = root->log_root; | |
2461 | struct extent_buffer *src; | |
4a500fd1 | 2462 | int err = 0; |
e02119d5 CM |
2463 | int ret; |
2464 | int i; | |
2465 | int nritems; | |
2466 | u64 first_offset = min_offset; | |
2467 | u64 last_offset = (u64)-1; | |
33345d01 | 2468 | u64 ino = btrfs_ino(inode); |
e02119d5 CM |
2469 | |
2470 | log = root->log_root; | |
33345d01 | 2471 | max_key.objectid = ino; |
e02119d5 CM |
2472 | max_key.offset = (u64)-1; |
2473 | max_key.type = key_type; | |
2474 | ||
33345d01 | 2475 | min_key.objectid = ino; |
e02119d5 CM |
2476 | min_key.type = key_type; |
2477 | min_key.offset = min_offset; | |
2478 | ||
2479 | path->keep_locks = 1; | |
2480 | ||
2481 | ret = btrfs_search_forward(root, &min_key, &max_key, | |
2482 | path, 0, trans->transid); | |
2483 | ||
2484 | /* | |
2485 | * we didn't find anything from this transaction, see if there | |
2486 | * is anything at all | |
2487 | */ | |
33345d01 LZ |
2488 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
2489 | min_key.objectid = ino; | |
e02119d5 CM |
2490 | min_key.type = key_type; |
2491 | min_key.offset = (u64)-1; | |
b3b4aa74 | 2492 | btrfs_release_path(path); |
e02119d5 CM |
2493 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
2494 | if (ret < 0) { | |
b3b4aa74 | 2495 | btrfs_release_path(path); |
e02119d5 CM |
2496 | return ret; |
2497 | } | |
33345d01 | 2498 | ret = btrfs_previous_item(root, path, ino, key_type); |
e02119d5 CM |
2499 | |
2500 | /* if ret == 0 there are items for this type, | |
2501 | * create a range to tell us the last key of this type. | |
2502 | * otherwise, there are no items in this directory after | |
2503 | * *min_offset, and we create a range to indicate that. | |
2504 | */ | |
2505 | if (ret == 0) { | |
2506 | struct btrfs_key tmp; | |
2507 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, | |
2508 | path->slots[0]); | |
d397712b | 2509 | if (key_type == tmp.type) |
e02119d5 | 2510 | first_offset = max(min_offset, tmp.offset) + 1; |
e02119d5 CM |
2511 | } |
2512 | goto done; | |
2513 | } | |
2514 | ||
2515 | /* go backward to find any previous key */ | |
33345d01 | 2516 | ret = btrfs_previous_item(root, path, ino, key_type); |
e02119d5 CM |
2517 | if (ret == 0) { |
2518 | struct btrfs_key tmp; | |
2519 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
2520 | if (key_type == tmp.type) { | |
2521 | first_offset = tmp.offset; | |
2522 | ret = overwrite_item(trans, log, dst_path, | |
2523 | path->nodes[0], path->slots[0], | |
2524 | &tmp); | |
4a500fd1 YZ |
2525 | if (ret) { |
2526 | err = ret; | |
2527 | goto done; | |
2528 | } | |
e02119d5 CM |
2529 | } |
2530 | } | |
b3b4aa74 | 2531 | btrfs_release_path(path); |
e02119d5 CM |
2532 | |
2533 | /* find the first key from this transaction again */ | |
2534 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | |
2535 | if (ret != 0) { | |
2536 | WARN_ON(1); | |
2537 | goto done; | |
2538 | } | |
2539 | ||
2540 | /* | |
2541 | * we have a block from this transaction, log every item in it | |
2542 | * from our directory | |
2543 | */ | |
d397712b | 2544 | while (1) { |
e02119d5 CM |
2545 | struct btrfs_key tmp; |
2546 | src = path->nodes[0]; | |
2547 | nritems = btrfs_header_nritems(src); | |
2548 | for (i = path->slots[0]; i < nritems; i++) { | |
2549 | btrfs_item_key_to_cpu(src, &min_key, i); | |
2550 | ||
33345d01 | 2551 | if (min_key.objectid != ino || min_key.type != key_type) |
e02119d5 CM |
2552 | goto done; |
2553 | ret = overwrite_item(trans, log, dst_path, src, i, | |
2554 | &min_key); | |
4a500fd1 YZ |
2555 | if (ret) { |
2556 | err = ret; | |
2557 | goto done; | |
2558 | } | |
e02119d5 CM |
2559 | } |
2560 | path->slots[0] = nritems; | |
2561 | ||
2562 | /* | |
2563 | * look ahead to the next item and see if it is also | |
2564 | * from this directory and from this transaction | |
2565 | */ | |
2566 | ret = btrfs_next_leaf(root, path); | |
2567 | if (ret == 1) { | |
2568 | last_offset = (u64)-1; | |
2569 | goto done; | |
2570 | } | |
2571 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
33345d01 | 2572 | if (tmp.objectid != ino || tmp.type != key_type) { |
e02119d5 CM |
2573 | last_offset = (u64)-1; |
2574 | goto done; | |
2575 | } | |
2576 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | |
2577 | ret = overwrite_item(trans, log, dst_path, | |
2578 | path->nodes[0], path->slots[0], | |
2579 | &tmp); | |
4a500fd1 YZ |
2580 | if (ret) |
2581 | err = ret; | |
2582 | else | |
2583 | last_offset = tmp.offset; | |
e02119d5 CM |
2584 | goto done; |
2585 | } | |
2586 | } | |
2587 | done: | |
b3b4aa74 DS |
2588 | btrfs_release_path(path); |
2589 | btrfs_release_path(dst_path); | |
e02119d5 | 2590 | |
4a500fd1 YZ |
2591 | if (err == 0) { |
2592 | *last_offset_ret = last_offset; | |
2593 | /* | |
2594 | * insert the log range keys to indicate where the log | |
2595 | * is valid | |
2596 | */ | |
2597 | ret = insert_dir_log_key(trans, log, path, key_type, | |
33345d01 | 2598 | ino, first_offset, last_offset); |
4a500fd1 YZ |
2599 | if (ret) |
2600 | err = ret; | |
2601 | } | |
2602 | return err; | |
e02119d5 CM |
2603 | } |
2604 | ||
2605 | /* | |
2606 | * logging directories is very similar to logging inodes, We find all the items | |
2607 | * from the current transaction and write them to the log. | |
2608 | * | |
2609 | * The recovery code scans the directory in the subvolume, and if it finds a | |
2610 | * key in the range logged that is not present in the log tree, then it means | |
2611 | * that dir entry was unlinked during the transaction. | |
2612 | * | |
2613 | * In order for that scan to work, we must include one key smaller than | |
2614 | * the smallest logged by this transaction and one key larger than the largest | |
2615 | * key logged by this transaction. | |
2616 | */ | |
2617 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | |
2618 | struct btrfs_root *root, struct inode *inode, | |
2619 | struct btrfs_path *path, | |
2620 | struct btrfs_path *dst_path) | |
2621 | { | |
2622 | u64 min_key; | |
2623 | u64 max_key; | |
2624 | int ret; | |
2625 | int key_type = BTRFS_DIR_ITEM_KEY; | |
2626 | ||
2627 | again: | |
2628 | min_key = 0; | |
2629 | max_key = 0; | |
d397712b | 2630 | while (1) { |
e02119d5 CM |
2631 | ret = log_dir_items(trans, root, inode, path, |
2632 | dst_path, key_type, min_key, | |
2633 | &max_key); | |
4a500fd1 YZ |
2634 | if (ret) |
2635 | return ret; | |
e02119d5 CM |
2636 | if (max_key == (u64)-1) |
2637 | break; | |
2638 | min_key = max_key + 1; | |
2639 | } | |
2640 | ||
2641 | if (key_type == BTRFS_DIR_ITEM_KEY) { | |
2642 | key_type = BTRFS_DIR_INDEX_KEY; | |
2643 | goto again; | |
2644 | } | |
2645 | return 0; | |
2646 | } | |
2647 | ||
2648 | /* | |
2649 | * a helper function to drop items from the log before we relog an | |
2650 | * inode. max_key_type indicates the highest item type to remove. | |
2651 | * This cannot be run for file data extents because it does not | |
2652 | * free the extents they point to. | |
2653 | */ | |
2654 | static int drop_objectid_items(struct btrfs_trans_handle *trans, | |
2655 | struct btrfs_root *log, | |
2656 | struct btrfs_path *path, | |
2657 | u64 objectid, int max_key_type) | |
2658 | { | |
2659 | int ret; | |
2660 | struct btrfs_key key; | |
2661 | struct btrfs_key found_key; | |
2662 | ||
2663 | key.objectid = objectid; | |
2664 | key.type = max_key_type; | |
2665 | key.offset = (u64)-1; | |
2666 | ||
d397712b | 2667 | while (1) { |
e02119d5 | 2668 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
4a500fd1 YZ |
2669 | BUG_ON(ret == 0); |
2670 | if (ret < 0) | |
e02119d5 CM |
2671 | break; |
2672 | ||
2673 | if (path->slots[0] == 0) | |
2674 | break; | |
2675 | ||
2676 | path->slots[0]--; | |
2677 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2678 | path->slots[0]); | |
2679 | ||
2680 | if (found_key.objectid != objectid) | |
2681 | break; | |
2682 | ||
2683 | ret = btrfs_del_item(trans, log, path); | |
65a246c5 TI |
2684 | if (ret) |
2685 | break; | |
b3b4aa74 | 2686 | btrfs_release_path(path); |
e02119d5 | 2687 | } |
b3b4aa74 | 2688 | btrfs_release_path(path); |
5bdbeb21 JB |
2689 | if (ret > 0) |
2690 | ret = 0; | |
4a500fd1 | 2691 | return ret; |
e02119d5 CM |
2692 | } |
2693 | ||
31ff1cd2 | 2694 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
d2794405 | 2695 | struct inode *inode, |
31ff1cd2 CM |
2696 | struct btrfs_path *dst_path, |
2697 | struct extent_buffer *src, | |
2698 | int start_slot, int nr, int inode_only) | |
2699 | { | |
2700 | unsigned long src_offset; | |
2701 | unsigned long dst_offset; | |
d2794405 | 2702 | struct btrfs_root *log = BTRFS_I(inode)->root->log_root; |
31ff1cd2 CM |
2703 | struct btrfs_file_extent_item *extent; |
2704 | struct btrfs_inode_item *inode_item; | |
2705 | int ret; | |
2706 | struct btrfs_key *ins_keys; | |
2707 | u32 *ins_sizes; | |
2708 | char *ins_data; | |
2709 | int i; | |
d20f7043 | 2710 | struct list_head ordered_sums; |
d2794405 | 2711 | int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
d20f7043 CM |
2712 | |
2713 | INIT_LIST_HEAD(&ordered_sums); | |
31ff1cd2 CM |
2714 | |
2715 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + | |
2716 | nr * sizeof(u32), GFP_NOFS); | |
2a29edc6 | 2717 | if (!ins_data) |
2718 | return -ENOMEM; | |
2719 | ||
31ff1cd2 CM |
2720 | ins_sizes = (u32 *)ins_data; |
2721 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | |
2722 | ||
2723 | for (i = 0; i < nr; i++) { | |
2724 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); | |
2725 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); | |
2726 | } | |
2727 | ret = btrfs_insert_empty_items(trans, log, dst_path, | |
2728 | ins_keys, ins_sizes, nr); | |
4a500fd1 YZ |
2729 | if (ret) { |
2730 | kfree(ins_data); | |
2731 | return ret; | |
2732 | } | |
31ff1cd2 | 2733 | |
5d4f98a2 | 2734 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
31ff1cd2 CM |
2735 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
2736 | dst_path->slots[0]); | |
2737 | ||
2738 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); | |
2739 | ||
2740 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | |
2741 | src_offset, ins_sizes[i]); | |
2742 | ||
2743 | if (inode_only == LOG_INODE_EXISTS && | |
2744 | ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { | |
2745 | inode_item = btrfs_item_ptr(dst_path->nodes[0], | |
2746 | dst_path->slots[0], | |
2747 | struct btrfs_inode_item); | |
2748 | btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0); | |
2749 | ||
2750 | /* set the generation to zero so the recover code | |
2751 | * can tell the difference between an logging | |
2752 | * just to say 'this inode exists' and a logging | |
2753 | * to say 'update this inode with these values' | |
2754 | */ | |
2755 | btrfs_set_inode_generation(dst_path->nodes[0], | |
2756 | inode_item, 0); | |
2757 | } | |
2758 | /* take a reference on file data extents so that truncates | |
2759 | * or deletes of this inode don't have to relog the inode | |
2760 | * again | |
2761 | */ | |
d2794405 LB |
2762 | if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY && |
2763 | !skip_csum) { | |
31ff1cd2 CM |
2764 | int found_type; |
2765 | extent = btrfs_item_ptr(src, start_slot + i, | |
2766 | struct btrfs_file_extent_item); | |
2767 | ||
8e531cdf | 2768 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
2769 | continue; | |
2770 | ||
31ff1cd2 | 2771 | found_type = btrfs_file_extent_type(src, extent); |
d899e052 YZ |
2772 | if (found_type == BTRFS_FILE_EXTENT_REG || |
2773 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
5d4f98a2 YZ |
2774 | u64 ds, dl, cs, cl; |
2775 | ds = btrfs_file_extent_disk_bytenr(src, | |
2776 | extent); | |
2777 | /* ds == 0 is a hole */ | |
2778 | if (ds == 0) | |
2779 | continue; | |
2780 | ||
2781 | dl = btrfs_file_extent_disk_num_bytes(src, | |
2782 | extent); | |
2783 | cs = btrfs_file_extent_offset(src, extent); | |
2784 | cl = btrfs_file_extent_num_bytes(src, | |
a419aef8 | 2785 | extent); |
580afd76 CM |
2786 | if (btrfs_file_extent_compression(src, |
2787 | extent)) { | |
2788 | cs = 0; | |
2789 | cl = dl; | |
2790 | } | |
5d4f98a2 YZ |
2791 | |
2792 | ret = btrfs_lookup_csums_range( | |
2793 | log->fs_info->csum_root, | |
2794 | ds + cs, ds + cs + cl - 1, | |
a2de733c | 2795 | &ordered_sums, 0); |
5d4f98a2 | 2796 | BUG_ON(ret); |
31ff1cd2 CM |
2797 | } |
2798 | } | |
31ff1cd2 CM |
2799 | } |
2800 | ||
2801 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | |
b3b4aa74 | 2802 | btrfs_release_path(dst_path); |
31ff1cd2 | 2803 | kfree(ins_data); |
d20f7043 CM |
2804 | |
2805 | /* | |
2806 | * we have to do this after the loop above to avoid changing the | |
2807 | * log tree while trying to change the log tree. | |
2808 | */ | |
4a500fd1 | 2809 | ret = 0; |
d397712b | 2810 | while (!list_empty(&ordered_sums)) { |
d20f7043 CM |
2811 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
2812 | struct btrfs_ordered_sum, | |
2813 | list); | |
4a500fd1 YZ |
2814 | if (!ret) |
2815 | ret = btrfs_csum_file_blocks(trans, log, sums); | |
d20f7043 CM |
2816 | list_del(&sums->list); |
2817 | kfree(sums); | |
2818 | } | |
4a500fd1 | 2819 | return ret; |
31ff1cd2 CM |
2820 | } |
2821 | ||
5dc562c5 JB |
2822 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
2823 | { | |
2824 | struct extent_map *em1, *em2; | |
2825 | ||
2826 | em1 = list_entry(a, struct extent_map, list); | |
2827 | em2 = list_entry(b, struct extent_map, list); | |
2828 | ||
2829 | if (em1->start < em2->start) | |
2830 | return -1; | |
2831 | else if (em1->start > em2->start) | |
2832 | return 1; | |
2833 | return 0; | |
2834 | } | |
2835 | ||
2836 | struct log_args { | |
2837 | struct extent_buffer *src; | |
2838 | u64 next_offset; | |
2839 | int start_slot; | |
2840 | int nr; | |
2841 | }; | |
2842 | ||
2843 | static int log_one_extent(struct btrfs_trans_handle *trans, | |
2844 | struct inode *inode, struct btrfs_root *root, | |
2845 | struct extent_map *em, struct btrfs_path *path, | |
2846 | struct btrfs_path *dst_path, struct log_args *args) | |
2847 | { | |
2848 | struct btrfs_root *log = root->log_root; | |
2849 | struct btrfs_file_extent_item *fi; | |
2850 | struct btrfs_key key; | |
4e2f84e6 | 2851 | u64 start = em->mod_start; |
0aa4a17d | 2852 | u64 search_start = start; |
4e2f84e6 | 2853 | u64 len = em->mod_len; |
5dc562c5 JB |
2854 | u64 num_bytes; |
2855 | int nritems; | |
2856 | int ret; | |
2857 | ||
2858 | if (BTRFS_I(inode)->logged_trans == trans->transid) { | |
5dc562c5 | 2859 | ret = __btrfs_drop_extents(trans, log, inode, dst_path, start, |
2aaa6655 | 2860 | start + len, NULL, 0); |
5dc562c5 JB |
2861 | if (ret) |
2862 | return ret; | |
2863 | } | |
2864 | ||
2865 | while (len) { | |
2866 | if (args->nr) | |
2867 | goto next_slot; | |
0aa4a17d | 2868 | again: |
5dc562c5 JB |
2869 | key.objectid = btrfs_ino(inode); |
2870 | key.type = BTRFS_EXTENT_DATA_KEY; | |
0aa4a17d | 2871 | key.offset = search_start; |
5dc562c5 JB |
2872 | |
2873 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2874 | if (ret < 0) | |
2875 | return ret; | |
0aa4a17d | 2876 | |
5dc562c5 JB |
2877 | if (ret) { |
2878 | /* | |
0aa4a17d JB |
2879 | * A rare case were we can have an em for a section of a |
2880 | * larger extent so we need to make sure that this em | |
2881 | * falls within the extent we've found. If not we just | |
2882 | * bail and go back to ye-olde way of doing things but | |
2883 | * it happens often enough in testing that we need to do | |
2884 | * this dance to make sure. | |
5dc562c5 | 2885 | */ |
0aa4a17d JB |
2886 | do { |
2887 | if (path->slots[0] == 0) { | |
2888 | btrfs_release_path(path); | |
2889 | if (search_start == 0) | |
2890 | return -ENOENT; | |
2891 | search_start--; | |
2892 | goto again; | |
2893 | } | |
2894 | ||
2895 | path->slots[0]--; | |
2896 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
2897 | path->slots[0]); | |
2898 | if (key.objectid != btrfs_ino(inode) || | |
2899 | key.type != BTRFS_EXTENT_DATA_KEY) { | |
2900 | btrfs_release_path(path); | |
2901 | return -ENOENT; | |
2902 | } | |
2903 | } while (key.offset > start); | |
2904 | ||
2905 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2906 | struct btrfs_file_extent_item); | |
2907 | num_bytes = btrfs_file_extent_num_bytes(path->nodes[0], | |
2908 | fi); | |
2909 | if (key.offset + num_bytes <= start) { | |
2910 | btrfs_release_path(path); | |
2911 | return -ENOENT; | |
2912 | } | |
5dc562c5 JB |
2913 | } |
2914 | args->src = path->nodes[0]; | |
2915 | next_slot: | |
0aa4a17d | 2916 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
5dc562c5 JB |
2917 | fi = btrfs_item_ptr(args->src, path->slots[0], |
2918 | struct btrfs_file_extent_item); | |
2919 | if (args->nr && | |
2920 | args->start_slot + args->nr == path->slots[0]) { | |
2921 | args->nr++; | |
2922 | } else if (args->nr) { | |
d2794405 | 2923 | ret = copy_items(trans, inode, dst_path, args->src, |
5dc562c5 JB |
2924 | args->start_slot, args->nr, |
2925 | LOG_INODE_ALL); | |
2926 | if (ret) | |
2927 | return ret; | |
2928 | args->nr = 1; | |
2929 | args->start_slot = path->slots[0]; | |
2930 | } else if (!args->nr) { | |
2931 | args->nr = 1; | |
2932 | args->start_slot = path->slots[0]; | |
2933 | } | |
2934 | nritems = btrfs_header_nritems(path->nodes[0]); | |
2935 | path->slots[0]++; | |
2936 | num_bytes = btrfs_file_extent_num_bytes(args->src, fi); | |
2937 | if (len < num_bytes) { | |
2938 | /* I _think_ this is ok, envision we write to a | |
2939 | * preallocated space that is adjacent to a previously | |
2940 | * written preallocated space that gets merged when we | |
2941 | * mark this preallocated space written. If we do not | |
2942 | * have the adjacent extent in cache then when we copy | |
2943 | * this extent it could end up being larger than our EM | |
2944 | * thinks it is, which is a-ok, so just set len to 0. | |
2945 | */ | |
2946 | len = 0; | |
2947 | } else { | |
2948 | len -= num_bytes; | |
2949 | } | |
0aa4a17d | 2950 | start = key.offset + num_bytes; |
5dc562c5 | 2951 | args->next_offset = start; |
0aa4a17d | 2952 | search_start = start; |
5dc562c5 JB |
2953 | |
2954 | if (path->slots[0] < nritems) { | |
2955 | if (len) | |
2956 | goto next_slot; | |
2957 | break; | |
2958 | } | |
2959 | ||
2960 | if (args->nr) { | |
d2794405 | 2961 | ret = copy_items(trans, inode, dst_path, args->src, |
5dc562c5 JB |
2962 | args->start_slot, args->nr, |
2963 | LOG_INODE_ALL); | |
2964 | if (ret) | |
2965 | return ret; | |
2966 | args->nr = 0; | |
2967 | btrfs_release_path(path); | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | return 0; | |
2972 | } | |
2973 | ||
2974 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, | |
2975 | struct btrfs_root *root, | |
2976 | struct inode *inode, | |
2977 | struct btrfs_path *path, | |
2978 | struct btrfs_path *dst_path) | |
2979 | { | |
2980 | struct log_args args; | |
5dc562c5 JB |
2981 | struct extent_map *em, *n; |
2982 | struct list_head extents; | |
2983 | struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree; | |
2984 | u64 test_gen; | |
2985 | int ret = 0; | |
2986 | ||
2987 | INIT_LIST_HEAD(&extents); | |
2988 | ||
2989 | memset(&args, 0, sizeof(args)); | |
2990 | ||
2991 | write_lock(&tree->lock); | |
2992 | test_gen = root->fs_info->last_trans_committed; | |
2993 | ||
2994 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { | |
2995 | list_del_init(&em->list); | |
2996 | if (em->generation <= test_gen) | |
2997 | continue; | |
ff44c6e3 JB |
2998 | /* Need a ref to keep it from getting evicted from cache */ |
2999 | atomic_inc(&em->refs); | |
3000 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
5dc562c5 JB |
3001 | list_add_tail(&em->list, &extents); |
3002 | } | |
3003 | ||
3004 | list_sort(NULL, &extents, extent_cmp); | |
3005 | ||
3006 | while (!list_empty(&extents)) { | |
3007 | em = list_entry(extents.next, struct extent_map, list); | |
3008 | ||
3009 | list_del_init(&em->list); | |
ff44c6e3 | 3010 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
5dc562c5 JB |
3011 | |
3012 | /* | |
3013 | * If we had an error we just need to delete everybody from our | |
3014 | * private list. | |
3015 | */ | |
ff44c6e3 JB |
3016 | if (ret) { |
3017 | free_extent_map(em); | |
5dc562c5 | 3018 | continue; |
ff44c6e3 JB |
3019 | } |
3020 | ||
3021 | write_unlock(&tree->lock); | |
5dc562c5 JB |
3022 | |
3023 | /* | |
3024 | * If the previous EM and the last extent we left off on aren't | |
3025 | * sequential then we need to copy the items we have and redo | |
3026 | * our search | |
3027 | */ | |
4e2f84e6 | 3028 | if (args.nr && em->mod_start != args.next_offset) { |
d2794405 | 3029 | ret = copy_items(trans, inode, dst_path, args.src, |
5dc562c5 JB |
3030 | args.start_slot, args.nr, |
3031 | LOG_INODE_ALL); | |
ff44c6e3 JB |
3032 | if (ret) { |
3033 | free_extent_map(em); | |
3034 | write_lock(&tree->lock); | |
5dc562c5 | 3035 | continue; |
ff44c6e3 | 3036 | } |
5dc562c5 JB |
3037 | btrfs_release_path(path); |
3038 | args.nr = 0; | |
3039 | } | |
3040 | ||
3041 | ret = log_one_extent(trans, inode, root, em, path, dst_path, &args); | |
ff44c6e3 JB |
3042 | free_extent_map(em); |
3043 | write_lock(&tree->lock); | |
5dc562c5 | 3044 | } |
ff44c6e3 JB |
3045 | WARN_ON(!list_empty(&extents)); |
3046 | write_unlock(&tree->lock); | |
5dc562c5 JB |
3047 | |
3048 | if (!ret && args.nr) | |
d2794405 | 3049 | ret = copy_items(trans, inode, dst_path, args.src, |
5dc562c5 JB |
3050 | args.start_slot, args.nr, LOG_INODE_ALL); |
3051 | btrfs_release_path(path); | |
5dc562c5 JB |
3052 | return ret; |
3053 | } | |
3054 | ||
e02119d5 CM |
3055 | /* log a single inode in the tree log. |
3056 | * At least one parent directory for this inode must exist in the tree | |
3057 | * or be logged already. | |
3058 | * | |
3059 | * Any items from this inode changed by the current transaction are copied | |
3060 | * to the log tree. An extra reference is taken on any extents in this | |
3061 | * file, allowing us to avoid a whole pile of corner cases around logging | |
3062 | * blocks that have been removed from the tree. | |
3063 | * | |
3064 | * See LOG_INODE_ALL and related defines for a description of what inode_only | |
3065 | * does. | |
3066 | * | |
3067 | * This handles both files and directories. | |
3068 | */ | |
12fcfd22 | 3069 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
e02119d5 CM |
3070 | struct btrfs_root *root, struct inode *inode, |
3071 | int inode_only) | |
3072 | { | |
3073 | struct btrfs_path *path; | |
3074 | struct btrfs_path *dst_path; | |
3075 | struct btrfs_key min_key; | |
3076 | struct btrfs_key max_key; | |
3077 | struct btrfs_root *log = root->log_root; | |
31ff1cd2 | 3078 | struct extent_buffer *src = NULL; |
4a500fd1 | 3079 | int err = 0; |
e02119d5 | 3080 | int ret; |
3a5f1d45 | 3081 | int nritems; |
31ff1cd2 CM |
3082 | int ins_start_slot = 0; |
3083 | int ins_nr; | |
5dc562c5 | 3084 | bool fast_search = false; |
33345d01 | 3085 | u64 ino = btrfs_ino(inode); |
e02119d5 CM |
3086 | |
3087 | log = root->log_root; | |
3088 | ||
3089 | path = btrfs_alloc_path(); | |
5df67083 TI |
3090 | if (!path) |
3091 | return -ENOMEM; | |
e02119d5 | 3092 | dst_path = btrfs_alloc_path(); |
5df67083 TI |
3093 | if (!dst_path) { |
3094 | btrfs_free_path(path); | |
3095 | return -ENOMEM; | |
3096 | } | |
e02119d5 | 3097 | |
33345d01 | 3098 | min_key.objectid = ino; |
e02119d5 CM |
3099 | min_key.type = BTRFS_INODE_ITEM_KEY; |
3100 | min_key.offset = 0; | |
3101 | ||
33345d01 | 3102 | max_key.objectid = ino; |
12fcfd22 | 3103 | |
12fcfd22 | 3104 | |
5dc562c5 | 3105 | /* today the code can only do partial logging of directories */ |
e02119d5 CM |
3106 | if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode)) |
3107 | max_key.type = BTRFS_XATTR_ITEM_KEY; | |
3108 | else | |
3109 | max_key.type = (u8)-1; | |
3110 | max_key.offset = (u64)-1; | |
3111 | ||
16cdcec7 MX |
3112 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
3113 | if (ret) { | |
3114 | btrfs_free_path(path); | |
3115 | btrfs_free_path(dst_path); | |
3116 | return ret; | |
3117 | } | |
3118 | ||
e02119d5 CM |
3119 | mutex_lock(&BTRFS_I(inode)->log_mutex); |
3120 | ||
3121 | /* | |
3122 | * a brute force approach to making sure we get the most uptodate | |
3123 | * copies of everything. | |
3124 | */ | |
3125 | if (S_ISDIR(inode->i_mode)) { | |
3126 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
3127 | ||
3128 | if (inode_only == LOG_INODE_EXISTS) | |
3129 | max_key_type = BTRFS_XATTR_ITEM_KEY; | |
33345d01 | 3130 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
e02119d5 | 3131 | } else { |
5dc562c5 JB |
3132 | if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
3133 | &BTRFS_I(inode)->runtime_flags)) { | |
3134 | ret = btrfs_truncate_inode_items(trans, log, | |
3135 | inode, 0, 0); | |
3136 | } else { | |
3137 | fast_search = true; | |
3138 | max_key.type = BTRFS_XATTR_ITEM_KEY; | |
3139 | ret = drop_objectid_items(trans, log, path, ino, | |
3140 | BTRFS_XATTR_ITEM_KEY); | |
3141 | } | |
e02119d5 | 3142 | } |
4a500fd1 YZ |
3143 | if (ret) { |
3144 | err = ret; | |
3145 | goto out_unlock; | |
3146 | } | |
e02119d5 CM |
3147 | path->keep_locks = 1; |
3148 | ||
d397712b | 3149 | while (1) { |
31ff1cd2 | 3150 | ins_nr = 0; |
e02119d5 CM |
3151 | ret = btrfs_search_forward(root, &min_key, &max_key, |
3152 | path, 0, trans->transid); | |
3153 | if (ret != 0) | |
3154 | break; | |
3a5f1d45 | 3155 | again: |
31ff1cd2 | 3156 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
33345d01 | 3157 | if (min_key.objectid != ino) |
e02119d5 CM |
3158 | break; |
3159 | if (min_key.type > max_key.type) | |
3160 | break; | |
31ff1cd2 | 3161 | |
e02119d5 | 3162 | src = path->nodes[0]; |
31ff1cd2 CM |
3163 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
3164 | ins_nr++; | |
3165 | goto next_slot; | |
3166 | } else if (!ins_nr) { | |
3167 | ins_start_slot = path->slots[0]; | |
3168 | ins_nr = 1; | |
3169 | goto next_slot; | |
e02119d5 CM |
3170 | } |
3171 | ||
d2794405 | 3172 | ret = copy_items(trans, inode, dst_path, src, ins_start_slot, |
31ff1cd2 | 3173 | ins_nr, inode_only); |
4a500fd1 YZ |
3174 | if (ret) { |
3175 | err = ret; | |
3176 | goto out_unlock; | |
3177 | } | |
31ff1cd2 CM |
3178 | ins_nr = 1; |
3179 | ins_start_slot = path->slots[0]; | |
3180 | next_slot: | |
e02119d5 | 3181 | |
3a5f1d45 CM |
3182 | nritems = btrfs_header_nritems(path->nodes[0]); |
3183 | path->slots[0]++; | |
3184 | if (path->slots[0] < nritems) { | |
3185 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, | |
3186 | path->slots[0]); | |
3187 | goto again; | |
3188 | } | |
31ff1cd2 | 3189 | if (ins_nr) { |
d2794405 | 3190 | ret = copy_items(trans, inode, dst_path, src, |
31ff1cd2 CM |
3191 | ins_start_slot, |
3192 | ins_nr, inode_only); | |
4a500fd1 YZ |
3193 | if (ret) { |
3194 | err = ret; | |
3195 | goto out_unlock; | |
3196 | } | |
31ff1cd2 CM |
3197 | ins_nr = 0; |
3198 | } | |
b3b4aa74 | 3199 | btrfs_release_path(path); |
3a5f1d45 | 3200 | |
e02119d5 CM |
3201 | if (min_key.offset < (u64)-1) |
3202 | min_key.offset++; | |
3203 | else if (min_key.type < (u8)-1) | |
3204 | min_key.type++; | |
3205 | else if (min_key.objectid < (u64)-1) | |
3206 | min_key.objectid++; | |
3207 | else | |
3208 | break; | |
3209 | } | |
31ff1cd2 | 3210 | if (ins_nr) { |
d2794405 | 3211 | ret = copy_items(trans, inode, dst_path, src, ins_start_slot, |
31ff1cd2 | 3212 | ins_nr, inode_only); |
4a500fd1 YZ |
3213 | if (ret) { |
3214 | err = ret; | |
3215 | goto out_unlock; | |
3216 | } | |
31ff1cd2 CM |
3217 | ins_nr = 0; |
3218 | } | |
5dc562c5 JB |
3219 | |
3220 | if (fast_search) { | |
3221 | btrfs_release_path(path); | |
3222 | btrfs_release_path(dst_path); | |
3223 | ret = btrfs_log_changed_extents(trans, root, inode, path, | |
3224 | dst_path); | |
3225 | if (ret) { | |
3226 | err = ret; | |
3227 | goto out_unlock; | |
3228 | } | |
06d3d22b LB |
3229 | } else { |
3230 | struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree; | |
3231 | struct extent_map *em, *n; | |
3232 | ||
3233 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) | |
3234 | list_del_init(&em->list); | |
5dc562c5 JB |
3235 | } |
3236 | ||
9623f9a3 | 3237 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { |
b3b4aa74 DS |
3238 | btrfs_release_path(path); |
3239 | btrfs_release_path(dst_path); | |
e02119d5 | 3240 | ret = log_directory_changes(trans, root, inode, path, dst_path); |
4a500fd1 YZ |
3241 | if (ret) { |
3242 | err = ret; | |
3243 | goto out_unlock; | |
3244 | } | |
e02119d5 | 3245 | } |
3a5f1d45 | 3246 | BTRFS_I(inode)->logged_trans = trans->transid; |
46d8bc34 | 3247 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans; |
4a500fd1 | 3248 | out_unlock: |
e02119d5 CM |
3249 | mutex_unlock(&BTRFS_I(inode)->log_mutex); |
3250 | ||
3251 | btrfs_free_path(path); | |
3252 | btrfs_free_path(dst_path); | |
4a500fd1 | 3253 | return err; |
e02119d5 CM |
3254 | } |
3255 | ||
12fcfd22 CM |
3256 | /* |
3257 | * follow the dentry parent pointers up the chain and see if any | |
3258 | * of the directories in it require a full commit before they can | |
3259 | * be logged. Returns zero if nothing special needs to be done or 1 if | |
3260 | * a full commit is required. | |
3261 | */ | |
3262 | static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans, | |
3263 | struct inode *inode, | |
3264 | struct dentry *parent, | |
3265 | struct super_block *sb, | |
3266 | u64 last_committed) | |
e02119d5 | 3267 | { |
12fcfd22 CM |
3268 | int ret = 0; |
3269 | struct btrfs_root *root; | |
6a912213 | 3270 | struct dentry *old_parent = NULL; |
e02119d5 | 3271 | |
af4176b4 CM |
3272 | /* |
3273 | * for regular files, if its inode is already on disk, we don't | |
3274 | * have to worry about the parents at all. This is because | |
3275 | * we can use the last_unlink_trans field to record renames | |
3276 | * and other fun in this file. | |
3277 | */ | |
3278 | if (S_ISREG(inode->i_mode) && | |
3279 | BTRFS_I(inode)->generation <= last_committed && | |
3280 | BTRFS_I(inode)->last_unlink_trans <= last_committed) | |
3281 | goto out; | |
3282 | ||
12fcfd22 CM |
3283 | if (!S_ISDIR(inode->i_mode)) { |
3284 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
3285 | goto out; | |
3286 | inode = parent->d_inode; | |
3287 | } | |
3288 | ||
3289 | while (1) { | |
3290 | BTRFS_I(inode)->logged_trans = trans->transid; | |
3291 | smp_mb(); | |
3292 | ||
3293 | if (BTRFS_I(inode)->last_unlink_trans > last_committed) { | |
3294 | root = BTRFS_I(inode)->root; | |
3295 | ||
3296 | /* | |
3297 | * make sure any commits to the log are forced | |
3298 | * to be full commits | |
3299 | */ | |
3300 | root->fs_info->last_trans_log_full_commit = | |
3301 | trans->transid; | |
3302 | ret = 1; | |
3303 | break; | |
3304 | } | |
3305 | ||
3306 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
3307 | break; | |
3308 | ||
76dda93c | 3309 | if (IS_ROOT(parent)) |
12fcfd22 CM |
3310 | break; |
3311 | ||
6a912213 JB |
3312 | parent = dget_parent(parent); |
3313 | dput(old_parent); | |
3314 | old_parent = parent; | |
12fcfd22 CM |
3315 | inode = parent->d_inode; |
3316 | ||
3317 | } | |
6a912213 | 3318 | dput(old_parent); |
12fcfd22 | 3319 | out: |
e02119d5 CM |
3320 | return ret; |
3321 | } | |
3322 | ||
3323 | /* | |
3324 | * helper function around btrfs_log_inode to make sure newly created | |
3325 | * parent directories also end up in the log. A minimal inode and backref | |
3326 | * only logging is done of any parent directories that are older than | |
3327 | * the last committed transaction | |
3328 | */ | |
12fcfd22 CM |
3329 | int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
3330 | struct btrfs_root *root, struct inode *inode, | |
3331 | struct dentry *parent, int exists_only) | |
e02119d5 | 3332 | { |
12fcfd22 | 3333 | int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; |
e02119d5 | 3334 | struct super_block *sb; |
6a912213 | 3335 | struct dentry *old_parent = NULL; |
12fcfd22 CM |
3336 | int ret = 0; |
3337 | u64 last_committed = root->fs_info->last_trans_committed; | |
3338 | ||
3339 | sb = inode->i_sb; | |
3340 | ||
3a5e1404 SW |
3341 | if (btrfs_test_opt(root, NOTREELOG)) { |
3342 | ret = 1; | |
3343 | goto end_no_trans; | |
3344 | } | |
3345 | ||
12fcfd22 CM |
3346 | if (root->fs_info->last_trans_log_full_commit > |
3347 | root->fs_info->last_trans_committed) { | |
3348 | ret = 1; | |
3349 | goto end_no_trans; | |
3350 | } | |
3351 | ||
76dda93c YZ |
3352 | if (root != BTRFS_I(inode)->root || |
3353 | btrfs_root_refs(&root->root_item) == 0) { | |
3354 | ret = 1; | |
3355 | goto end_no_trans; | |
3356 | } | |
3357 | ||
12fcfd22 CM |
3358 | ret = check_parent_dirs_for_sync(trans, inode, parent, |
3359 | sb, last_committed); | |
3360 | if (ret) | |
3361 | goto end_no_trans; | |
e02119d5 | 3362 | |
22ee6985 | 3363 | if (btrfs_inode_in_log(inode, trans->transid)) { |
257c62e1 CM |
3364 | ret = BTRFS_NO_LOG_SYNC; |
3365 | goto end_no_trans; | |
3366 | } | |
3367 | ||
4a500fd1 YZ |
3368 | ret = start_log_trans(trans, root); |
3369 | if (ret) | |
3370 | goto end_trans; | |
e02119d5 | 3371 | |
12fcfd22 | 3372 | ret = btrfs_log_inode(trans, root, inode, inode_only); |
4a500fd1 YZ |
3373 | if (ret) |
3374 | goto end_trans; | |
12fcfd22 | 3375 | |
af4176b4 CM |
3376 | /* |
3377 | * for regular files, if its inode is already on disk, we don't | |
3378 | * have to worry about the parents at all. This is because | |
3379 | * we can use the last_unlink_trans field to record renames | |
3380 | * and other fun in this file. | |
3381 | */ | |
3382 | if (S_ISREG(inode->i_mode) && | |
3383 | BTRFS_I(inode)->generation <= last_committed && | |
4a500fd1 YZ |
3384 | BTRFS_I(inode)->last_unlink_trans <= last_committed) { |
3385 | ret = 0; | |
3386 | goto end_trans; | |
3387 | } | |
af4176b4 CM |
3388 | |
3389 | inode_only = LOG_INODE_EXISTS; | |
12fcfd22 CM |
3390 | while (1) { |
3391 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
e02119d5 CM |
3392 | break; |
3393 | ||
12fcfd22 | 3394 | inode = parent->d_inode; |
76dda93c YZ |
3395 | if (root != BTRFS_I(inode)->root) |
3396 | break; | |
3397 | ||
12fcfd22 CM |
3398 | if (BTRFS_I(inode)->generation > |
3399 | root->fs_info->last_trans_committed) { | |
3400 | ret = btrfs_log_inode(trans, root, inode, inode_only); | |
4a500fd1 YZ |
3401 | if (ret) |
3402 | goto end_trans; | |
12fcfd22 | 3403 | } |
76dda93c | 3404 | if (IS_ROOT(parent)) |
e02119d5 | 3405 | break; |
12fcfd22 | 3406 | |
6a912213 JB |
3407 | parent = dget_parent(parent); |
3408 | dput(old_parent); | |
3409 | old_parent = parent; | |
e02119d5 | 3410 | } |
12fcfd22 | 3411 | ret = 0; |
4a500fd1 | 3412 | end_trans: |
6a912213 | 3413 | dput(old_parent); |
4a500fd1 | 3414 | if (ret < 0) { |
0fa83cdb | 3415 | WARN_ON(ret != -ENOSPC); |
4a500fd1 YZ |
3416 | root->fs_info->last_trans_log_full_commit = trans->transid; |
3417 | ret = 1; | |
3418 | } | |
12fcfd22 CM |
3419 | btrfs_end_log_trans(root); |
3420 | end_no_trans: | |
3421 | return ret; | |
e02119d5 CM |
3422 | } |
3423 | ||
3424 | /* | |
3425 | * it is not safe to log dentry if the chunk root has added new | |
3426 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | |
3427 | * If this returns 1, you must commit the transaction to safely get your | |
3428 | * data on disk. | |
3429 | */ | |
3430 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | |
3431 | struct btrfs_root *root, struct dentry *dentry) | |
3432 | { | |
6a912213 JB |
3433 | struct dentry *parent = dget_parent(dentry); |
3434 | int ret; | |
3435 | ||
3436 | ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0); | |
3437 | dput(parent); | |
3438 | ||
3439 | return ret; | |
e02119d5 CM |
3440 | } |
3441 | ||
3442 | /* | |
3443 | * should be called during mount to recover any replay any log trees | |
3444 | * from the FS | |
3445 | */ | |
3446 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | |
3447 | { | |
3448 | int ret; | |
3449 | struct btrfs_path *path; | |
3450 | struct btrfs_trans_handle *trans; | |
3451 | struct btrfs_key key; | |
3452 | struct btrfs_key found_key; | |
3453 | struct btrfs_key tmp_key; | |
3454 | struct btrfs_root *log; | |
3455 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | |
3456 | struct walk_control wc = { | |
3457 | .process_func = process_one_buffer, | |
3458 | .stage = 0, | |
3459 | }; | |
3460 | ||
e02119d5 | 3461 | path = btrfs_alloc_path(); |
db5b493a TI |
3462 | if (!path) |
3463 | return -ENOMEM; | |
3464 | ||
3465 | fs_info->log_root_recovering = 1; | |
e02119d5 | 3466 | |
4a500fd1 | 3467 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
79787eaa JM |
3468 | if (IS_ERR(trans)) { |
3469 | ret = PTR_ERR(trans); | |
3470 | goto error; | |
3471 | } | |
e02119d5 CM |
3472 | |
3473 | wc.trans = trans; | |
3474 | wc.pin = 1; | |
3475 | ||
db5b493a | 3476 | ret = walk_log_tree(trans, log_root_tree, &wc); |
79787eaa JM |
3477 | if (ret) { |
3478 | btrfs_error(fs_info, ret, "Failed to pin buffers while " | |
3479 | "recovering log root tree."); | |
3480 | goto error; | |
3481 | } | |
e02119d5 CM |
3482 | |
3483 | again: | |
3484 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
3485 | key.offset = (u64)-1; | |
3486 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
3487 | ||
d397712b | 3488 | while (1) { |
e02119d5 | 3489 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
79787eaa JM |
3490 | |
3491 | if (ret < 0) { | |
3492 | btrfs_error(fs_info, ret, | |
3493 | "Couldn't find tree log root."); | |
3494 | goto error; | |
3495 | } | |
e02119d5 CM |
3496 | if (ret > 0) { |
3497 | if (path->slots[0] == 0) | |
3498 | break; | |
3499 | path->slots[0]--; | |
3500 | } | |
3501 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
3502 | path->slots[0]); | |
b3b4aa74 | 3503 | btrfs_release_path(path); |
e02119d5 CM |
3504 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
3505 | break; | |
3506 | ||
3507 | log = btrfs_read_fs_root_no_radix(log_root_tree, | |
3508 | &found_key); | |
79787eaa JM |
3509 | if (IS_ERR(log)) { |
3510 | ret = PTR_ERR(log); | |
3511 | btrfs_error(fs_info, ret, | |
3512 | "Couldn't read tree log root."); | |
3513 | goto error; | |
3514 | } | |
e02119d5 CM |
3515 | |
3516 | tmp_key.objectid = found_key.offset; | |
3517 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; | |
3518 | tmp_key.offset = (u64)-1; | |
3519 | ||
3520 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); | |
79787eaa JM |
3521 | if (IS_ERR(wc.replay_dest)) { |
3522 | ret = PTR_ERR(wc.replay_dest); | |
3523 | btrfs_error(fs_info, ret, "Couldn't read target root " | |
3524 | "for tree log recovery."); | |
3525 | goto error; | |
3526 | } | |
e02119d5 | 3527 | |
07d400a6 | 3528 | wc.replay_dest->log_root = log; |
5d4f98a2 | 3529 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
e02119d5 CM |
3530 | ret = walk_log_tree(trans, log, &wc); |
3531 | BUG_ON(ret); | |
3532 | ||
3533 | if (wc.stage == LOG_WALK_REPLAY_ALL) { | |
3534 | ret = fixup_inode_link_counts(trans, wc.replay_dest, | |
3535 | path); | |
3536 | BUG_ON(ret); | |
3537 | } | |
3538 | ||
3539 | key.offset = found_key.offset - 1; | |
07d400a6 | 3540 | wc.replay_dest->log_root = NULL; |
e02119d5 | 3541 | free_extent_buffer(log->node); |
b263c2c8 | 3542 | free_extent_buffer(log->commit_root); |
e02119d5 CM |
3543 | kfree(log); |
3544 | ||
3545 | if (found_key.offset == 0) | |
3546 | break; | |
3547 | } | |
b3b4aa74 | 3548 | btrfs_release_path(path); |
e02119d5 CM |
3549 | |
3550 | /* step one is to pin it all, step two is to replay just inodes */ | |
3551 | if (wc.pin) { | |
3552 | wc.pin = 0; | |
3553 | wc.process_func = replay_one_buffer; | |
3554 | wc.stage = LOG_WALK_REPLAY_INODES; | |
3555 | goto again; | |
3556 | } | |
3557 | /* step three is to replay everything */ | |
3558 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | |
3559 | wc.stage++; | |
3560 | goto again; | |
3561 | } | |
3562 | ||
3563 | btrfs_free_path(path); | |
3564 | ||
3565 | free_extent_buffer(log_root_tree->node); | |
3566 | log_root_tree->log_root = NULL; | |
3567 | fs_info->log_root_recovering = 0; | |
3568 | ||
3569 | /* step 4: commit the transaction, which also unpins the blocks */ | |
3570 | btrfs_commit_transaction(trans, fs_info->tree_root); | |
3571 | ||
3572 | kfree(log_root_tree); | |
3573 | return 0; | |
79787eaa JM |
3574 | |
3575 | error: | |
3576 | btrfs_free_path(path); | |
3577 | return ret; | |
e02119d5 | 3578 | } |
12fcfd22 CM |
3579 | |
3580 | /* | |
3581 | * there are some corner cases where we want to force a full | |
3582 | * commit instead of allowing a directory to be logged. | |
3583 | * | |
3584 | * They revolve around files there were unlinked from the directory, and | |
3585 | * this function updates the parent directory so that a full commit is | |
3586 | * properly done if it is fsync'd later after the unlinks are done. | |
3587 | */ | |
3588 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, | |
3589 | struct inode *dir, struct inode *inode, | |
3590 | int for_rename) | |
3591 | { | |
af4176b4 CM |
3592 | /* |
3593 | * when we're logging a file, if it hasn't been renamed | |
3594 | * or unlinked, and its inode is fully committed on disk, | |
3595 | * we don't have to worry about walking up the directory chain | |
3596 | * to log its parents. | |
3597 | * | |
3598 | * So, we use the last_unlink_trans field to put this transid | |
3599 | * into the file. When the file is logged we check it and | |
3600 | * don't log the parents if the file is fully on disk. | |
3601 | */ | |
3602 | if (S_ISREG(inode->i_mode)) | |
3603 | BTRFS_I(inode)->last_unlink_trans = trans->transid; | |
3604 | ||
12fcfd22 CM |
3605 | /* |
3606 | * if this directory was already logged any new | |
3607 | * names for this file/dir will get recorded | |
3608 | */ | |
3609 | smp_mb(); | |
3610 | if (BTRFS_I(dir)->logged_trans == trans->transid) | |
3611 | return; | |
3612 | ||
3613 | /* | |
3614 | * if the inode we're about to unlink was logged, | |
3615 | * the log will be properly updated for any new names | |
3616 | */ | |
3617 | if (BTRFS_I(inode)->logged_trans == trans->transid) | |
3618 | return; | |
3619 | ||
3620 | /* | |
3621 | * when renaming files across directories, if the directory | |
3622 | * there we're unlinking from gets fsync'd later on, there's | |
3623 | * no way to find the destination directory later and fsync it | |
3624 | * properly. So, we have to be conservative and force commits | |
3625 | * so the new name gets discovered. | |
3626 | */ | |
3627 | if (for_rename) | |
3628 | goto record; | |
3629 | ||
3630 | /* we can safely do the unlink without any special recording */ | |
3631 | return; | |
3632 | ||
3633 | record: | |
3634 | BTRFS_I(dir)->last_unlink_trans = trans->transid; | |
3635 | } | |
3636 | ||
3637 | /* | |
3638 | * Call this after adding a new name for a file and it will properly | |
3639 | * update the log to reflect the new name. | |
3640 | * | |
3641 | * It will return zero if all goes well, and it will return 1 if a | |
3642 | * full transaction commit is required. | |
3643 | */ | |
3644 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, | |
3645 | struct inode *inode, struct inode *old_dir, | |
3646 | struct dentry *parent) | |
3647 | { | |
3648 | struct btrfs_root * root = BTRFS_I(inode)->root; | |
3649 | ||
af4176b4 CM |
3650 | /* |
3651 | * this will force the logging code to walk the dentry chain | |
3652 | * up for the file | |
3653 | */ | |
3654 | if (S_ISREG(inode->i_mode)) | |
3655 | BTRFS_I(inode)->last_unlink_trans = trans->transid; | |
3656 | ||
12fcfd22 CM |
3657 | /* |
3658 | * if this inode hasn't been logged and directory we're renaming it | |
3659 | * from hasn't been logged, we don't need to log it | |
3660 | */ | |
3661 | if (BTRFS_I(inode)->logged_trans <= | |
3662 | root->fs_info->last_trans_committed && | |
3663 | (!old_dir || BTRFS_I(old_dir)->logged_trans <= | |
3664 | root->fs_info->last_trans_committed)) | |
3665 | return 0; | |
3666 | ||
3667 | return btrfs_log_inode_parent(trans, root, inode, parent, 1); | |
3668 | } | |
3669 |