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