]>
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> |
c6adc9cc | 21 | #include <linux/blkdev.h> |
5dc562c5 | 22 | #include <linux/list_sort.h> |
995946dd | 23 | #include "tree-log.h" |
e02119d5 CM |
24 | #include "disk-io.h" |
25 | #include "locking.h" | |
26 | #include "print-tree.h" | |
f186373f | 27 | #include "backref.h" |
f186373f | 28 | #include "hash.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 | |
dd8e7217 JB |
93 | #define LOG_WALK_REPLAY_DIR_INDEX 2 |
94 | #define LOG_WALK_REPLAY_ALL 3 | |
e02119d5 | 95 | |
12fcfd22 | 96 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
49dae1bc FM |
97 | struct btrfs_root *root, struct inode *inode, |
98 | int inode_only, | |
99 | const loff_t start, | |
8407f553 FM |
100 | const loff_t end, |
101 | struct btrfs_log_ctx *ctx); | |
ec051c0f YZ |
102 | static int link_to_fixup_dir(struct btrfs_trans_handle *trans, |
103 | struct btrfs_root *root, | |
104 | struct btrfs_path *path, u64 objectid); | |
12fcfd22 CM |
105 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, |
106 | struct btrfs_root *root, | |
107 | struct btrfs_root *log, | |
108 | struct btrfs_path *path, | |
109 | u64 dirid, int del_all); | |
e02119d5 CM |
110 | |
111 | /* | |
112 | * tree logging is a special write ahead log used to make sure that | |
113 | * fsyncs and O_SYNCs can happen without doing full tree commits. | |
114 | * | |
115 | * Full tree commits are expensive because they require commonly | |
116 | * modified blocks to be recowed, creating many dirty pages in the | |
117 | * extent tree an 4x-6x higher write load than ext3. | |
118 | * | |
119 | * Instead of doing a tree commit on every fsync, we use the | |
120 | * key ranges and transaction ids to find items for a given file or directory | |
121 | * that have changed in this transaction. Those items are copied into | |
122 | * a special tree (one per subvolume root), that tree is written to disk | |
123 | * and then the fsync is considered complete. | |
124 | * | |
125 | * After a crash, items are copied out of the log-tree back into the | |
126 | * subvolume tree. Any file data extents found are recorded in the extent | |
127 | * allocation tree, and the log-tree freed. | |
128 | * | |
129 | * The log tree is read three times, once to pin down all the extents it is | |
130 | * using in ram and once, once to create all the inodes logged in the tree | |
131 | * and once to do all the other items. | |
132 | */ | |
133 | ||
e02119d5 CM |
134 | /* |
135 | * start a sub transaction and setup the log tree | |
136 | * this increments the log tree writer count to make the people | |
137 | * syncing the tree wait for us to finish | |
138 | */ | |
139 | static int start_log_trans(struct btrfs_trans_handle *trans, | |
8b050d35 MX |
140 | struct btrfs_root *root, |
141 | struct btrfs_log_ctx *ctx) | |
e02119d5 | 142 | { |
8b050d35 | 143 | int index; |
e02119d5 | 144 | int ret; |
7237f183 YZ |
145 | |
146 | mutex_lock(&root->log_mutex); | |
147 | if (root->log_root) { | |
995946dd | 148 | if (btrfs_need_log_full_commit(root->fs_info, trans)) { |
50471a38 MX |
149 | ret = -EAGAIN; |
150 | goto out; | |
151 | } | |
ff782e0a JB |
152 | if (!root->log_start_pid) { |
153 | root->log_start_pid = current->pid; | |
27cdeb70 | 154 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
ff782e0a | 155 | } else if (root->log_start_pid != current->pid) { |
27cdeb70 | 156 | set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
ff782e0a JB |
157 | } |
158 | ||
2ecb7923 | 159 | atomic_inc(&root->log_batch); |
7237f183 | 160 | atomic_inc(&root->log_writers); |
8b050d35 MX |
161 | if (ctx) { |
162 | index = root->log_transid % 2; | |
163 | list_add_tail(&ctx->list, &root->log_ctxs[index]); | |
d1433deb | 164 | ctx->log_transid = root->log_transid; |
8b050d35 | 165 | } |
7237f183 YZ |
166 | mutex_unlock(&root->log_mutex); |
167 | return 0; | |
168 | } | |
e87ac136 MX |
169 | |
170 | ret = 0; | |
e02119d5 | 171 | mutex_lock(&root->fs_info->tree_log_mutex); |
e87ac136 | 172 | if (!root->fs_info->log_root_tree) |
e02119d5 | 173 | ret = btrfs_init_log_root_tree(trans, root->fs_info); |
e87ac136 MX |
174 | mutex_unlock(&root->fs_info->tree_log_mutex); |
175 | if (ret) | |
176 | goto out; | |
177 | ||
178 | if (!root->log_root) { | |
e02119d5 | 179 | ret = btrfs_add_log_tree(trans, root); |
4a500fd1 | 180 | if (ret) |
e87ac136 | 181 | goto out; |
e02119d5 | 182 | } |
27cdeb70 | 183 | clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); |
e87ac136 | 184 | root->log_start_pid = current->pid; |
2ecb7923 | 185 | atomic_inc(&root->log_batch); |
7237f183 | 186 | atomic_inc(&root->log_writers); |
8b050d35 MX |
187 | if (ctx) { |
188 | index = root->log_transid % 2; | |
189 | list_add_tail(&ctx->list, &root->log_ctxs[index]); | |
d1433deb | 190 | ctx->log_transid = root->log_transid; |
8b050d35 | 191 | } |
e87ac136 | 192 | out: |
7237f183 | 193 | mutex_unlock(&root->log_mutex); |
e87ac136 | 194 | return ret; |
e02119d5 CM |
195 | } |
196 | ||
197 | /* | |
198 | * returns 0 if there was a log transaction running and we were able | |
199 | * to join, or returns -ENOENT if there were not transactions | |
200 | * in progress | |
201 | */ | |
202 | static int join_running_log_trans(struct btrfs_root *root) | |
203 | { | |
204 | int ret = -ENOENT; | |
205 | ||
206 | smp_mb(); | |
207 | if (!root->log_root) | |
208 | return -ENOENT; | |
209 | ||
7237f183 | 210 | mutex_lock(&root->log_mutex); |
e02119d5 CM |
211 | if (root->log_root) { |
212 | ret = 0; | |
7237f183 | 213 | atomic_inc(&root->log_writers); |
e02119d5 | 214 | } |
7237f183 | 215 | mutex_unlock(&root->log_mutex); |
e02119d5 CM |
216 | return ret; |
217 | } | |
218 | ||
12fcfd22 CM |
219 | /* |
220 | * This either makes the current running log transaction wait | |
221 | * until you call btrfs_end_log_trans() or it makes any future | |
222 | * log transactions wait until you call btrfs_end_log_trans() | |
223 | */ | |
224 | int btrfs_pin_log_trans(struct btrfs_root *root) | |
225 | { | |
226 | int ret = -ENOENT; | |
227 | ||
228 | mutex_lock(&root->log_mutex); | |
229 | atomic_inc(&root->log_writers); | |
230 | mutex_unlock(&root->log_mutex); | |
231 | return ret; | |
232 | } | |
233 | ||
e02119d5 CM |
234 | /* |
235 | * indicate we're done making changes to the log tree | |
236 | * and wake up anyone waiting to do a sync | |
237 | */ | |
143bede5 | 238 | void btrfs_end_log_trans(struct btrfs_root *root) |
e02119d5 | 239 | { |
7237f183 YZ |
240 | if (atomic_dec_and_test(&root->log_writers)) { |
241 | smp_mb(); | |
242 | if (waitqueue_active(&root->log_writer_wait)) | |
243 | wake_up(&root->log_writer_wait); | |
244 | } | |
e02119d5 CM |
245 | } |
246 | ||
247 | ||
248 | /* | |
249 | * the walk control struct is used to pass state down the chain when | |
250 | * processing the log tree. The stage field tells us which part | |
251 | * of the log tree processing we are currently doing. The others | |
252 | * are state fields used for that specific part | |
253 | */ | |
254 | struct walk_control { | |
255 | /* should we free the extent on disk when done? This is used | |
256 | * at transaction commit time while freeing a log tree | |
257 | */ | |
258 | int free; | |
259 | ||
260 | /* should we write out the extent buffer? This is used | |
261 | * while flushing the log tree to disk during a sync | |
262 | */ | |
263 | int write; | |
264 | ||
265 | /* should we wait for the extent buffer io to finish? Also used | |
266 | * while flushing the log tree to disk for a sync | |
267 | */ | |
268 | int wait; | |
269 | ||
270 | /* pin only walk, we record which extents on disk belong to the | |
271 | * log trees | |
272 | */ | |
273 | int pin; | |
274 | ||
275 | /* what stage of the replay code we're currently in */ | |
276 | int stage; | |
277 | ||
278 | /* the root we are currently replaying */ | |
279 | struct btrfs_root *replay_dest; | |
280 | ||
281 | /* the trans handle for the current replay */ | |
282 | struct btrfs_trans_handle *trans; | |
283 | ||
284 | /* the function that gets used to process blocks we find in the | |
285 | * tree. Note the extent_buffer might not be up to date when it is | |
286 | * passed in, and it must be checked or read if you need the data | |
287 | * inside it | |
288 | */ | |
289 | int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb, | |
290 | struct walk_control *wc, u64 gen); | |
291 | }; | |
292 | ||
293 | /* | |
294 | * process_func used to pin down extents, write them or wait on them | |
295 | */ | |
296 | static int process_one_buffer(struct btrfs_root *log, | |
297 | struct extent_buffer *eb, | |
298 | struct walk_control *wc, u64 gen) | |
299 | { | |
b50c6e25 JB |
300 | int ret = 0; |
301 | ||
8c2a1a30 JB |
302 | /* |
303 | * If this fs is mixed then we need to be able to process the leaves to | |
304 | * pin down any logged extents, so we have to read the block. | |
305 | */ | |
306 | if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) { | |
307 | ret = btrfs_read_buffer(eb, gen); | |
308 | if (ret) | |
309 | return ret; | |
310 | } | |
311 | ||
04018de5 | 312 | if (wc->pin) |
b50c6e25 JB |
313 | ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root, |
314 | eb->start, eb->len); | |
e02119d5 | 315 | |
b50c6e25 | 316 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { |
8c2a1a30 JB |
317 | if (wc->pin && btrfs_header_level(eb) == 0) |
318 | ret = btrfs_exclude_logged_extents(log, eb); | |
e02119d5 CM |
319 | if (wc->write) |
320 | btrfs_write_tree_block(eb); | |
321 | if (wc->wait) | |
322 | btrfs_wait_tree_block_writeback(eb); | |
323 | } | |
b50c6e25 | 324 | return ret; |
e02119d5 CM |
325 | } |
326 | ||
327 | /* | |
328 | * Item overwrite used by replay and tree logging. eb, slot and key all refer | |
329 | * to the src data we are copying out. | |
330 | * | |
331 | * root is the tree we are copying into, and path is a scratch | |
332 | * path for use in this function (it should be released on entry and | |
333 | * will be released on exit). | |
334 | * | |
335 | * If the key is already in the destination tree the existing item is | |
336 | * overwritten. If the existing item isn't big enough, it is extended. | |
337 | * If it is too large, it is truncated. | |
338 | * | |
339 | * If the key isn't in the destination yet, a new item is inserted. | |
340 | */ | |
341 | static noinline int overwrite_item(struct btrfs_trans_handle *trans, | |
342 | struct btrfs_root *root, | |
343 | struct btrfs_path *path, | |
344 | struct extent_buffer *eb, int slot, | |
345 | struct btrfs_key *key) | |
346 | { | |
347 | int ret; | |
348 | u32 item_size; | |
349 | u64 saved_i_size = 0; | |
350 | int save_old_i_size = 0; | |
351 | unsigned long src_ptr; | |
352 | unsigned long dst_ptr; | |
353 | int overwrite_root = 0; | |
4bc4bee4 | 354 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; |
e02119d5 CM |
355 | |
356 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
357 | overwrite_root = 1; | |
358 | ||
359 | item_size = btrfs_item_size_nr(eb, slot); | |
360 | src_ptr = btrfs_item_ptr_offset(eb, slot); | |
361 | ||
362 | /* look for the key in the destination tree */ | |
363 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
4bc4bee4 JB |
364 | if (ret < 0) |
365 | return ret; | |
366 | ||
e02119d5 CM |
367 | if (ret == 0) { |
368 | char *src_copy; | |
369 | char *dst_copy; | |
370 | u32 dst_size = btrfs_item_size_nr(path->nodes[0], | |
371 | path->slots[0]); | |
372 | if (dst_size != item_size) | |
373 | goto insert; | |
374 | ||
375 | if (item_size == 0) { | |
b3b4aa74 | 376 | btrfs_release_path(path); |
e02119d5 CM |
377 | return 0; |
378 | } | |
379 | dst_copy = kmalloc(item_size, GFP_NOFS); | |
380 | src_copy = kmalloc(item_size, GFP_NOFS); | |
2a29edc6 | 381 | if (!dst_copy || !src_copy) { |
b3b4aa74 | 382 | btrfs_release_path(path); |
2a29edc6 | 383 | kfree(dst_copy); |
384 | kfree(src_copy); | |
385 | return -ENOMEM; | |
386 | } | |
e02119d5 CM |
387 | |
388 | read_extent_buffer(eb, src_copy, src_ptr, item_size); | |
389 | ||
390 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
391 | read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, | |
392 | item_size); | |
393 | ret = memcmp(dst_copy, src_copy, item_size); | |
394 | ||
395 | kfree(dst_copy); | |
396 | kfree(src_copy); | |
397 | /* | |
398 | * they have the same contents, just return, this saves | |
399 | * us from cowing blocks in the destination tree and doing | |
400 | * extra writes that may not have been done by a previous | |
401 | * sync | |
402 | */ | |
403 | if (ret == 0) { | |
b3b4aa74 | 404 | btrfs_release_path(path); |
e02119d5 CM |
405 | return 0; |
406 | } | |
407 | ||
4bc4bee4 JB |
408 | /* |
409 | * We need to load the old nbytes into the inode so when we | |
410 | * replay the extents we've logged we get the right nbytes. | |
411 | */ | |
412 | if (inode_item) { | |
413 | struct btrfs_inode_item *item; | |
414 | u64 nbytes; | |
d555438b | 415 | u32 mode; |
4bc4bee4 JB |
416 | |
417 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
418 | struct btrfs_inode_item); | |
419 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); | |
420 | item = btrfs_item_ptr(eb, slot, | |
421 | struct btrfs_inode_item); | |
422 | btrfs_set_inode_nbytes(eb, item, nbytes); | |
d555438b JB |
423 | |
424 | /* | |
425 | * If this is a directory we need to reset the i_size to | |
426 | * 0 so that we can set it up properly when replaying | |
427 | * the rest of the items in this log. | |
428 | */ | |
429 | mode = btrfs_inode_mode(eb, item); | |
430 | if (S_ISDIR(mode)) | |
431 | btrfs_set_inode_size(eb, item, 0); | |
4bc4bee4 JB |
432 | } |
433 | } else if (inode_item) { | |
434 | struct btrfs_inode_item *item; | |
d555438b | 435 | u32 mode; |
4bc4bee4 JB |
436 | |
437 | /* | |
438 | * New inode, set nbytes to 0 so that the nbytes comes out | |
439 | * properly when we replay the extents. | |
440 | */ | |
441 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
442 | btrfs_set_inode_nbytes(eb, item, 0); | |
d555438b JB |
443 | |
444 | /* | |
445 | * If this is a directory we need to reset the i_size to 0 so | |
446 | * that we can set it up properly when replaying the rest of | |
447 | * the items in this log. | |
448 | */ | |
449 | mode = btrfs_inode_mode(eb, item); | |
450 | if (S_ISDIR(mode)) | |
451 | btrfs_set_inode_size(eb, item, 0); | |
e02119d5 CM |
452 | } |
453 | insert: | |
b3b4aa74 | 454 | btrfs_release_path(path); |
e02119d5 | 455 | /* try to insert the key into the destination tree */ |
df8d116f | 456 | path->skip_release_on_error = 1; |
e02119d5 CM |
457 | ret = btrfs_insert_empty_item(trans, root, path, |
458 | key, item_size); | |
df8d116f | 459 | path->skip_release_on_error = 0; |
e02119d5 CM |
460 | |
461 | /* make sure any existing item is the correct size */ | |
df8d116f | 462 | if (ret == -EEXIST || ret == -EOVERFLOW) { |
e02119d5 CM |
463 | u32 found_size; |
464 | found_size = btrfs_item_size_nr(path->nodes[0], | |
465 | path->slots[0]); | |
143bede5 | 466 | if (found_size > item_size) |
afe5fea7 | 467 | btrfs_truncate_item(root, path, item_size, 1); |
143bede5 | 468 | else if (found_size < item_size) |
4b90c680 | 469 | btrfs_extend_item(root, path, |
143bede5 | 470 | item_size - found_size); |
e02119d5 | 471 | } else if (ret) { |
4a500fd1 | 472 | return ret; |
e02119d5 CM |
473 | } |
474 | dst_ptr = btrfs_item_ptr_offset(path->nodes[0], | |
475 | path->slots[0]); | |
476 | ||
477 | /* don't overwrite an existing inode if the generation number | |
478 | * was logged as zero. This is done when the tree logging code | |
479 | * is just logging an inode to make sure it exists after recovery. | |
480 | * | |
481 | * Also, don't overwrite i_size on directories during replay. | |
482 | * log replay inserts and removes directory items based on the | |
483 | * state of the tree found in the subvolume, and i_size is modified | |
484 | * as it goes | |
485 | */ | |
486 | if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) { | |
487 | struct btrfs_inode_item *src_item; | |
488 | struct btrfs_inode_item *dst_item; | |
489 | ||
490 | src_item = (struct btrfs_inode_item *)src_ptr; | |
491 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
492 | ||
1a4bcf47 FM |
493 | if (btrfs_inode_generation(eb, src_item) == 0) { |
494 | struct extent_buffer *dst_eb = path->nodes[0]; | |
495 | ||
496 | if (S_ISREG(btrfs_inode_mode(eb, src_item)) && | |
497 | S_ISREG(btrfs_inode_mode(dst_eb, dst_item))) { | |
498 | struct btrfs_map_token token; | |
499 | u64 ino_size = btrfs_inode_size(eb, src_item); | |
500 | ||
501 | btrfs_init_map_token(&token); | |
502 | btrfs_set_token_inode_size(dst_eb, dst_item, | |
503 | ino_size, &token); | |
504 | } | |
e02119d5 | 505 | goto no_copy; |
1a4bcf47 | 506 | } |
e02119d5 CM |
507 | |
508 | if (overwrite_root && | |
509 | S_ISDIR(btrfs_inode_mode(eb, src_item)) && | |
510 | S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { | |
511 | save_old_i_size = 1; | |
512 | saved_i_size = btrfs_inode_size(path->nodes[0], | |
513 | dst_item); | |
514 | } | |
515 | } | |
516 | ||
517 | copy_extent_buffer(path->nodes[0], eb, dst_ptr, | |
518 | src_ptr, item_size); | |
519 | ||
520 | if (save_old_i_size) { | |
521 | struct btrfs_inode_item *dst_item; | |
522 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
523 | btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); | |
524 | } | |
525 | ||
526 | /* make sure the generation is filled in */ | |
527 | if (key->type == BTRFS_INODE_ITEM_KEY) { | |
528 | struct btrfs_inode_item *dst_item; | |
529 | dst_item = (struct btrfs_inode_item *)dst_ptr; | |
530 | if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { | |
531 | btrfs_set_inode_generation(path->nodes[0], dst_item, | |
532 | trans->transid); | |
533 | } | |
534 | } | |
535 | no_copy: | |
536 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 537 | btrfs_release_path(path); |
e02119d5 CM |
538 | return 0; |
539 | } | |
540 | ||
541 | /* | |
542 | * simple helper to read an inode off the disk from a given root | |
543 | * This can only be called for subvolume roots and not for the log | |
544 | */ | |
545 | static noinline struct inode *read_one_inode(struct btrfs_root *root, | |
546 | u64 objectid) | |
547 | { | |
5d4f98a2 | 548 | struct btrfs_key key; |
e02119d5 | 549 | struct inode *inode; |
e02119d5 | 550 | |
5d4f98a2 YZ |
551 | key.objectid = objectid; |
552 | key.type = BTRFS_INODE_ITEM_KEY; | |
553 | key.offset = 0; | |
73f73415 | 554 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); |
5d4f98a2 YZ |
555 | if (IS_ERR(inode)) { |
556 | inode = NULL; | |
557 | } else if (is_bad_inode(inode)) { | |
e02119d5 CM |
558 | iput(inode); |
559 | inode = NULL; | |
560 | } | |
561 | return inode; | |
562 | } | |
563 | ||
564 | /* replays a single extent in 'eb' at 'slot' with 'key' into the | |
565 | * subvolume 'root'. path is released on entry and should be released | |
566 | * on exit. | |
567 | * | |
568 | * extents in the log tree have not been allocated out of the extent | |
569 | * tree yet. So, this completes the allocation, taking a reference | |
570 | * as required if the extent already exists or creating a new extent | |
571 | * if it isn't in the extent allocation tree yet. | |
572 | * | |
573 | * The extent is inserted into the file, dropping any existing extents | |
574 | * from the file that overlap the new one. | |
575 | */ | |
576 | static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |
577 | struct btrfs_root *root, | |
578 | struct btrfs_path *path, | |
579 | struct extent_buffer *eb, int slot, | |
580 | struct btrfs_key *key) | |
581 | { | |
582 | int found_type; | |
e02119d5 | 583 | u64 extent_end; |
e02119d5 | 584 | u64 start = key->offset; |
4bc4bee4 | 585 | u64 nbytes = 0; |
e02119d5 CM |
586 | struct btrfs_file_extent_item *item; |
587 | struct inode *inode = NULL; | |
588 | unsigned long size; | |
589 | int ret = 0; | |
590 | ||
591 | item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
592 | found_type = btrfs_file_extent_type(eb, item); | |
593 | ||
d899e052 | 594 | if (found_type == BTRFS_FILE_EXTENT_REG || |
4bc4bee4 JB |
595 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
596 | nbytes = btrfs_file_extent_num_bytes(eb, item); | |
597 | extent_end = start + nbytes; | |
598 | ||
599 | /* | |
600 | * We don't add to the inodes nbytes if we are prealloc or a | |
601 | * hole. | |
602 | */ | |
603 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | |
604 | nbytes = 0; | |
605 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
514ac8ad | 606 | size = btrfs_file_extent_inline_len(eb, slot, item); |
4bc4bee4 | 607 | nbytes = btrfs_file_extent_ram_bytes(eb, item); |
fda2832f | 608 | extent_end = ALIGN(start + size, root->sectorsize); |
e02119d5 CM |
609 | } else { |
610 | ret = 0; | |
611 | goto out; | |
612 | } | |
613 | ||
614 | inode = read_one_inode(root, key->objectid); | |
615 | if (!inode) { | |
616 | ret = -EIO; | |
617 | goto out; | |
618 | } | |
619 | ||
620 | /* | |
621 | * first check to see if we already have this extent in the | |
622 | * file. This must be done before the btrfs_drop_extents run | |
623 | * so we don't try to drop this extent. | |
624 | */ | |
33345d01 | 625 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
e02119d5 CM |
626 | start, 0); |
627 | ||
d899e052 YZ |
628 | if (ret == 0 && |
629 | (found_type == BTRFS_FILE_EXTENT_REG || | |
630 | found_type == BTRFS_FILE_EXTENT_PREALLOC)) { | |
e02119d5 CM |
631 | struct btrfs_file_extent_item cmp1; |
632 | struct btrfs_file_extent_item cmp2; | |
633 | struct btrfs_file_extent_item *existing; | |
634 | struct extent_buffer *leaf; | |
635 | ||
636 | leaf = path->nodes[0]; | |
637 | existing = btrfs_item_ptr(leaf, path->slots[0], | |
638 | struct btrfs_file_extent_item); | |
639 | ||
640 | read_extent_buffer(eb, &cmp1, (unsigned long)item, | |
641 | sizeof(cmp1)); | |
642 | read_extent_buffer(leaf, &cmp2, (unsigned long)existing, | |
643 | sizeof(cmp2)); | |
644 | ||
645 | /* | |
646 | * we already have a pointer to this exact extent, | |
647 | * we don't have to do anything | |
648 | */ | |
649 | if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { | |
b3b4aa74 | 650 | btrfs_release_path(path); |
e02119d5 CM |
651 | goto out; |
652 | } | |
653 | } | |
b3b4aa74 | 654 | btrfs_release_path(path); |
e02119d5 CM |
655 | |
656 | /* drop any overlapping extents */ | |
2671485d | 657 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
3650860b JB |
658 | if (ret) |
659 | goto out; | |
e02119d5 | 660 | |
07d400a6 YZ |
661 | if (found_type == BTRFS_FILE_EXTENT_REG || |
662 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
5d4f98a2 | 663 | u64 offset; |
07d400a6 YZ |
664 | unsigned long dest_offset; |
665 | struct btrfs_key ins; | |
666 | ||
667 | ret = btrfs_insert_empty_item(trans, root, path, key, | |
668 | sizeof(*item)); | |
3650860b JB |
669 | if (ret) |
670 | goto out; | |
07d400a6 YZ |
671 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
672 | path->slots[0]); | |
673 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | |
674 | (unsigned long)item, sizeof(*item)); | |
675 | ||
676 | ins.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
677 | ins.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
678 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 | 679 | offset = key->offset - btrfs_file_extent_offset(eb, item); |
07d400a6 YZ |
680 | |
681 | if (ins.objectid > 0) { | |
682 | u64 csum_start; | |
683 | u64 csum_end; | |
684 | LIST_HEAD(ordered_sums); | |
685 | /* | |
686 | * is this extent already allocated in the extent | |
687 | * allocation tree? If so, just add a reference | |
688 | */ | |
1a4ed8fd | 689 | ret = btrfs_lookup_data_extent(root, ins.objectid, |
07d400a6 YZ |
690 | ins.offset); |
691 | if (ret == 0) { | |
692 | ret = btrfs_inc_extent_ref(trans, root, | |
693 | ins.objectid, ins.offset, | |
5d4f98a2 | 694 | 0, root->root_key.objectid, |
66d7e7f0 | 695 | key->objectid, offset, 0); |
b50c6e25 JB |
696 | if (ret) |
697 | goto out; | |
07d400a6 YZ |
698 | } else { |
699 | /* | |
700 | * insert the extent pointer in the extent | |
701 | * allocation tree | |
702 | */ | |
5d4f98a2 YZ |
703 | ret = btrfs_alloc_logged_file_extent(trans, |
704 | root, root->root_key.objectid, | |
705 | key->objectid, offset, &ins); | |
b50c6e25 JB |
706 | if (ret) |
707 | goto out; | |
07d400a6 | 708 | } |
b3b4aa74 | 709 | btrfs_release_path(path); |
07d400a6 YZ |
710 | |
711 | if (btrfs_file_extent_compression(eb, item)) { | |
712 | csum_start = ins.objectid; | |
713 | csum_end = csum_start + ins.offset; | |
714 | } else { | |
715 | csum_start = ins.objectid + | |
716 | btrfs_file_extent_offset(eb, item); | |
717 | csum_end = csum_start + | |
718 | btrfs_file_extent_num_bytes(eb, item); | |
719 | } | |
720 | ||
721 | ret = btrfs_lookup_csums_range(root->log_root, | |
722 | csum_start, csum_end - 1, | |
a2de733c | 723 | &ordered_sums, 0); |
3650860b JB |
724 | if (ret) |
725 | goto out; | |
07d400a6 YZ |
726 | while (!list_empty(&ordered_sums)) { |
727 | struct btrfs_ordered_sum *sums; | |
728 | sums = list_entry(ordered_sums.next, | |
729 | struct btrfs_ordered_sum, | |
730 | list); | |
3650860b JB |
731 | if (!ret) |
732 | ret = btrfs_csum_file_blocks(trans, | |
07d400a6 YZ |
733 | root->fs_info->csum_root, |
734 | sums); | |
07d400a6 YZ |
735 | list_del(&sums->list); |
736 | kfree(sums); | |
737 | } | |
3650860b JB |
738 | if (ret) |
739 | goto out; | |
07d400a6 | 740 | } else { |
b3b4aa74 | 741 | btrfs_release_path(path); |
07d400a6 YZ |
742 | } |
743 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
744 | /* inline extents are easy, we just overwrite them */ | |
745 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
3650860b JB |
746 | if (ret) |
747 | goto out; | |
07d400a6 | 748 | } |
e02119d5 | 749 | |
4bc4bee4 | 750 | inode_add_bytes(inode, nbytes); |
b9959295 | 751 | ret = btrfs_update_inode(trans, root, inode); |
e02119d5 CM |
752 | out: |
753 | if (inode) | |
754 | iput(inode); | |
755 | return ret; | |
756 | } | |
757 | ||
758 | /* | |
759 | * when cleaning up conflicts between the directory names in the | |
760 | * subvolume, directory names in the log and directory names in the | |
761 | * inode back references, we may have to unlink inodes from directories. | |
762 | * | |
763 | * This is a helper function to do the unlink of a specific directory | |
764 | * item | |
765 | */ | |
766 | static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | |
767 | struct btrfs_root *root, | |
768 | struct btrfs_path *path, | |
769 | struct inode *dir, | |
770 | struct btrfs_dir_item *di) | |
771 | { | |
772 | struct inode *inode; | |
773 | char *name; | |
774 | int name_len; | |
775 | struct extent_buffer *leaf; | |
776 | struct btrfs_key location; | |
777 | int ret; | |
778 | ||
779 | leaf = path->nodes[0]; | |
780 | ||
781 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
782 | name_len = btrfs_dir_name_len(leaf, di); | |
783 | name = kmalloc(name_len, GFP_NOFS); | |
2a29edc6 | 784 | if (!name) |
785 | return -ENOMEM; | |
786 | ||
e02119d5 | 787 | read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len); |
b3b4aa74 | 788 | btrfs_release_path(path); |
e02119d5 CM |
789 | |
790 | inode = read_one_inode(root, location.objectid); | |
c00e9493 | 791 | if (!inode) { |
3650860b JB |
792 | ret = -EIO; |
793 | goto out; | |
c00e9493 | 794 | } |
e02119d5 | 795 | |
ec051c0f | 796 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
3650860b JB |
797 | if (ret) |
798 | goto out; | |
12fcfd22 | 799 | |
e02119d5 | 800 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
3650860b JB |
801 | if (ret) |
802 | goto out; | |
ada9af21 FDBM |
803 | else |
804 | ret = btrfs_run_delayed_items(trans, root); | |
3650860b | 805 | out: |
e02119d5 | 806 | kfree(name); |
e02119d5 CM |
807 | iput(inode); |
808 | return ret; | |
809 | } | |
810 | ||
811 | /* | |
812 | * helper function to see if a given name and sequence number found | |
813 | * in an inode back reference are already in a directory and correctly | |
814 | * point to this inode | |
815 | */ | |
816 | static noinline int inode_in_dir(struct btrfs_root *root, | |
817 | struct btrfs_path *path, | |
818 | u64 dirid, u64 objectid, u64 index, | |
819 | const char *name, int name_len) | |
820 | { | |
821 | struct btrfs_dir_item *di; | |
822 | struct btrfs_key location; | |
823 | int match = 0; | |
824 | ||
825 | di = btrfs_lookup_dir_index_item(NULL, root, path, dirid, | |
826 | index, name, name_len, 0); | |
827 | if (di && !IS_ERR(di)) { | |
828 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
829 | if (location.objectid != objectid) | |
830 | goto out; | |
831 | } else | |
832 | goto out; | |
b3b4aa74 | 833 | btrfs_release_path(path); |
e02119d5 CM |
834 | |
835 | di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0); | |
836 | if (di && !IS_ERR(di)) { | |
837 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
838 | if (location.objectid != objectid) | |
839 | goto out; | |
840 | } else | |
841 | goto out; | |
842 | match = 1; | |
843 | out: | |
b3b4aa74 | 844 | btrfs_release_path(path); |
e02119d5 CM |
845 | return match; |
846 | } | |
847 | ||
848 | /* | |
849 | * helper function to check a log tree for a named back reference in | |
850 | * an inode. This is used to decide if a back reference that is | |
851 | * found in the subvolume conflicts with what we find in the log. | |
852 | * | |
853 | * inode backreferences may have multiple refs in a single item, | |
854 | * during replay we process one reference at a time, and we don't | |
855 | * want to delete valid links to a file from the subvolume if that | |
856 | * link is also in the log. | |
857 | */ | |
858 | static noinline int backref_in_log(struct btrfs_root *log, | |
859 | struct btrfs_key *key, | |
f186373f | 860 | u64 ref_objectid, |
df8d116f | 861 | const char *name, int namelen) |
e02119d5 CM |
862 | { |
863 | struct btrfs_path *path; | |
864 | struct btrfs_inode_ref *ref; | |
865 | unsigned long ptr; | |
866 | unsigned long ptr_end; | |
867 | unsigned long name_ptr; | |
868 | int found_name_len; | |
869 | int item_size; | |
870 | int ret; | |
871 | int match = 0; | |
872 | ||
873 | path = btrfs_alloc_path(); | |
2a29edc6 | 874 | if (!path) |
875 | return -ENOMEM; | |
876 | ||
e02119d5 CM |
877 | ret = btrfs_search_slot(NULL, log, key, path, 0, 0); |
878 | if (ret != 0) | |
879 | goto out; | |
880 | ||
e02119d5 | 881 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); |
f186373f MF |
882 | |
883 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | |
884 | if (btrfs_find_name_in_ext_backref(path, ref_objectid, | |
885 | name, namelen, NULL)) | |
886 | match = 1; | |
887 | ||
888 | goto out; | |
889 | } | |
890 | ||
891 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | |
e02119d5 CM |
892 | ptr_end = ptr + item_size; |
893 | while (ptr < ptr_end) { | |
894 | ref = (struct btrfs_inode_ref *)ptr; | |
895 | found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref); | |
896 | if (found_name_len == namelen) { | |
897 | name_ptr = (unsigned long)(ref + 1); | |
898 | ret = memcmp_extent_buffer(path->nodes[0], name, | |
899 | name_ptr, namelen); | |
900 | if (ret == 0) { | |
901 | match = 1; | |
902 | goto out; | |
903 | } | |
904 | } | |
905 | ptr = (unsigned long)(ref + 1) + found_name_len; | |
906 | } | |
907 | out: | |
908 | btrfs_free_path(path); | |
909 | return match; | |
910 | } | |
911 | ||
5a1d7843 | 912 | static inline int __add_inode_ref(struct btrfs_trans_handle *trans, |
e02119d5 | 913 | struct btrfs_root *root, |
e02119d5 | 914 | struct btrfs_path *path, |
5a1d7843 JS |
915 | struct btrfs_root *log_root, |
916 | struct inode *dir, struct inode *inode, | |
5a1d7843 | 917 | struct extent_buffer *eb, |
f186373f MF |
918 | u64 inode_objectid, u64 parent_objectid, |
919 | u64 ref_index, char *name, int namelen, | |
920 | int *search_done) | |
e02119d5 | 921 | { |
34f3e4f2 | 922 | int ret; |
f186373f MF |
923 | char *victim_name; |
924 | int victim_name_len; | |
925 | struct extent_buffer *leaf; | |
5a1d7843 | 926 | struct btrfs_dir_item *di; |
f186373f MF |
927 | struct btrfs_key search_key; |
928 | struct btrfs_inode_extref *extref; | |
c622ae60 | 929 | |
f186373f MF |
930 | again: |
931 | /* Search old style refs */ | |
932 | search_key.objectid = inode_objectid; | |
933 | search_key.type = BTRFS_INODE_REF_KEY; | |
934 | search_key.offset = parent_objectid; | |
935 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
e02119d5 | 936 | if (ret == 0) { |
e02119d5 CM |
937 | struct btrfs_inode_ref *victim_ref; |
938 | unsigned long ptr; | |
939 | unsigned long ptr_end; | |
f186373f MF |
940 | |
941 | leaf = path->nodes[0]; | |
e02119d5 CM |
942 | |
943 | /* are we trying to overwrite a back ref for the root directory | |
944 | * if so, just jump out, we're done | |
945 | */ | |
f186373f | 946 | if (search_key.objectid == search_key.offset) |
5a1d7843 | 947 | return 1; |
e02119d5 CM |
948 | |
949 | /* check all the names in this back reference to see | |
950 | * if they are in the log. if so, we allow them to stay | |
951 | * otherwise they must be unlinked as a conflict | |
952 | */ | |
953 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
954 | ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]); | |
d397712b | 955 | while (ptr < ptr_end) { |
e02119d5 CM |
956 | victim_ref = (struct btrfs_inode_ref *)ptr; |
957 | victim_name_len = btrfs_inode_ref_name_len(leaf, | |
958 | victim_ref); | |
959 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | |
3650860b JB |
960 | if (!victim_name) |
961 | return -ENOMEM; | |
e02119d5 CM |
962 | |
963 | read_extent_buffer(leaf, victim_name, | |
964 | (unsigned long)(victim_ref + 1), | |
965 | victim_name_len); | |
966 | ||
f186373f MF |
967 | if (!backref_in_log(log_root, &search_key, |
968 | parent_objectid, | |
969 | victim_name, | |
e02119d5 | 970 | victim_name_len)) { |
8b558c5f | 971 | inc_nlink(inode); |
b3b4aa74 | 972 | btrfs_release_path(path); |
12fcfd22 | 973 | |
e02119d5 CM |
974 | ret = btrfs_unlink_inode(trans, root, dir, |
975 | inode, victim_name, | |
976 | victim_name_len); | |
f186373f | 977 | kfree(victim_name); |
3650860b JB |
978 | if (ret) |
979 | return ret; | |
ada9af21 FDBM |
980 | ret = btrfs_run_delayed_items(trans, root); |
981 | if (ret) | |
982 | return ret; | |
f186373f MF |
983 | *search_done = 1; |
984 | goto again; | |
e02119d5 CM |
985 | } |
986 | kfree(victim_name); | |
f186373f | 987 | |
e02119d5 CM |
988 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
989 | } | |
e02119d5 | 990 | |
c622ae60 | 991 | /* |
992 | * NOTE: we have searched root tree and checked the | |
993 | * coresponding ref, it does not need to check again. | |
994 | */ | |
5a1d7843 | 995 | *search_done = 1; |
e02119d5 | 996 | } |
b3b4aa74 | 997 | btrfs_release_path(path); |
e02119d5 | 998 | |
f186373f MF |
999 | /* Same search but for extended refs */ |
1000 | extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen, | |
1001 | inode_objectid, parent_objectid, 0, | |
1002 | 0); | |
1003 | if (!IS_ERR_OR_NULL(extref)) { | |
1004 | u32 item_size; | |
1005 | u32 cur_offset = 0; | |
1006 | unsigned long base; | |
1007 | struct inode *victim_parent; | |
1008 | ||
1009 | leaf = path->nodes[0]; | |
1010 | ||
1011 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1012 | base = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
1013 | ||
1014 | while (cur_offset < item_size) { | |
1015 | extref = (struct btrfs_inode_extref *)base + cur_offset; | |
1016 | ||
1017 | victim_name_len = btrfs_inode_extref_name_len(leaf, extref); | |
1018 | ||
1019 | if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid) | |
1020 | goto next; | |
1021 | ||
1022 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | |
3650860b JB |
1023 | if (!victim_name) |
1024 | return -ENOMEM; | |
f186373f MF |
1025 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
1026 | victim_name_len); | |
1027 | ||
1028 | search_key.objectid = inode_objectid; | |
1029 | search_key.type = BTRFS_INODE_EXTREF_KEY; | |
1030 | search_key.offset = btrfs_extref_hash(parent_objectid, | |
1031 | victim_name, | |
1032 | victim_name_len); | |
1033 | ret = 0; | |
1034 | if (!backref_in_log(log_root, &search_key, | |
1035 | parent_objectid, victim_name, | |
1036 | victim_name_len)) { | |
1037 | ret = -ENOENT; | |
1038 | victim_parent = read_one_inode(root, | |
1039 | parent_objectid); | |
1040 | if (victim_parent) { | |
8b558c5f | 1041 | inc_nlink(inode); |
f186373f MF |
1042 | btrfs_release_path(path); |
1043 | ||
1044 | ret = btrfs_unlink_inode(trans, root, | |
1045 | victim_parent, | |
1046 | inode, | |
1047 | victim_name, | |
1048 | victim_name_len); | |
ada9af21 FDBM |
1049 | if (!ret) |
1050 | ret = btrfs_run_delayed_items( | |
1051 | trans, root); | |
f186373f | 1052 | } |
f186373f MF |
1053 | iput(victim_parent); |
1054 | kfree(victim_name); | |
3650860b JB |
1055 | if (ret) |
1056 | return ret; | |
f186373f MF |
1057 | *search_done = 1; |
1058 | goto again; | |
1059 | } | |
1060 | kfree(victim_name); | |
3650860b JB |
1061 | if (ret) |
1062 | return ret; | |
f186373f MF |
1063 | next: |
1064 | cur_offset += victim_name_len + sizeof(*extref); | |
1065 | } | |
1066 | *search_done = 1; | |
1067 | } | |
1068 | btrfs_release_path(path); | |
1069 | ||
34f3e4f2 | 1070 | /* look for a conflicting sequence number */ |
1071 | di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir), | |
f186373f | 1072 | ref_index, name, namelen, 0); |
34f3e4f2 | 1073 | if (di && !IS_ERR(di)) { |
1074 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
3650860b JB |
1075 | if (ret) |
1076 | return ret; | |
34f3e4f2 | 1077 | } |
1078 | btrfs_release_path(path); | |
1079 | ||
1080 | /* look for a conflicing name */ | |
1081 | di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), | |
1082 | name, namelen, 0); | |
1083 | if (di && !IS_ERR(di)) { | |
1084 | ret = drop_one_dir_item(trans, root, path, dir, di); | |
3650860b JB |
1085 | if (ret) |
1086 | return ret; | |
34f3e4f2 | 1087 | } |
1088 | btrfs_release_path(path); | |
1089 | ||
5a1d7843 JS |
1090 | return 0; |
1091 | } | |
e02119d5 | 1092 | |
f186373f MF |
1093 | static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, |
1094 | u32 *namelen, char **name, u64 *index, | |
1095 | u64 *parent_objectid) | |
1096 | { | |
1097 | struct btrfs_inode_extref *extref; | |
1098 | ||
1099 | extref = (struct btrfs_inode_extref *)ref_ptr; | |
1100 | ||
1101 | *namelen = btrfs_inode_extref_name_len(eb, extref); | |
1102 | *name = kmalloc(*namelen, GFP_NOFS); | |
1103 | if (*name == NULL) | |
1104 | return -ENOMEM; | |
1105 | ||
1106 | read_extent_buffer(eb, *name, (unsigned long)&extref->name, | |
1107 | *namelen); | |
1108 | ||
1109 | *index = btrfs_inode_extref_index(eb, extref); | |
1110 | if (parent_objectid) | |
1111 | *parent_objectid = btrfs_inode_extref_parent(eb, extref); | |
1112 | ||
1113 | return 0; | |
1114 | } | |
1115 | ||
1116 | static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr, | |
1117 | u32 *namelen, char **name, u64 *index) | |
1118 | { | |
1119 | struct btrfs_inode_ref *ref; | |
1120 | ||
1121 | ref = (struct btrfs_inode_ref *)ref_ptr; | |
1122 | ||
1123 | *namelen = btrfs_inode_ref_name_len(eb, ref); | |
1124 | *name = kmalloc(*namelen, GFP_NOFS); | |
1125 | if (*name == NULL) | |
1126 | return -ENOMEM; | |
1127 | ||
1128 | read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen); | |
1129 | ||
1130 | *index = btrfs_inode_ref_index(eb, ref); | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
5a1d7843 JS |
1135 | /* |
1136 | * replay one inode back reference item found in the log tree. | |
1137 | * eb, slot and key refer to the buffer and key found in the log tree. | |
1138 | * root is the destination we are replaying into, and path is for temp | |
1139 | * use by this function. (it should be released on return). | |
1140 | */ | |
1141 | static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |
1142 | struct btrfs_root *root, | |
1143 | struct btrfs_root *log, | |
1144 | struct btrfs_path *path, | |
1145 | struct extent_buffer *eb, int slot, | |
1146 | struct btrfs_key *key) | |
1147 | { | |
03b2f08b GB |
1148 | struct inode *dir = NULL; |
1149 | struct inode *inode = NULL; | |
5a1d7843 JS |
1150 | unsigned long ref_ptr; |
1151 | unsigned long ref_end; | |
03b2f08b | 1152 | char *name = NULL; |
5a1d7843 JS |
1153 | int namelen; |
1154 | int ret; | |
1155 | int search_done = 0; | |
f186373f MF |
1156 | int log_ref_ver = 0; |
1157 | u64 parent_objectid; | |
1158 | u64 inode_objectid; | |
f46dbe3d | 1159 | u64 ref_index = 0; |
f186373f MF |
1160 | int ref_struct_size; |
1161 | ||
1162 | ref_ptr = btrfs_item_ptr_offset(eb, slot); | |
1163 | ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); | |
1164 | ||
1165 | if (key->type == BTRFS_INODE_EXTREF_KEY) { | |
1166 | struct btrfs_inode_extref *r; | |
1167 | ||
1168 | ref_struct_size = sizeof(struct btrfs_inode_extref); | |
1169 | log_ref_ver = 1; | |
1170 | r = (struct btrfs_inode_extref *)ref_ptr; | |
1171 | parent_objectid = btrfs_inode_extref_parent(eb, r); | |
1172 | } else { | |
1173 | ref_struct_size = sizeof(struct btrfs_inode_ref); | |
1174 | parent_objectid = key->offset; | |
1175 | } | |
1176 | inode_objectid = key->objectid; | |
e02119d5 | 1177 | |
5a1d7843 JS |
1178 | /* |
1179 | * it is possible that we didn't log all the parent directories | |
1180 | * for a given inode. If we don't find the dir, just don't | |
1181 | * copy the back ref in. The link count fixup code will take | |
1182 | * care of the rest | |
1183 | */ | |
f186373f | 1184 | dir = read_one_inode(root, parent_objectid); |
03b2f08b GB |
1185 | if (!dir) { |
1186 | ret = -ENOENT; | |
1187 | goto out; | |
1188 | } | |
5a1d7843 | 1189 | |
f186373f | 1190 | inode = read_one_inode(root, inode_objectid); |
5a1d7843 | 1191 | if (!inode) { |
03b2f08b GB |
1192 | ret = -EIO; |
1193 | goto out; | |
5a1d7843 JS |
1194 | } |
1195 | ||
5a1d7843 | 1196 | while (ref_ptr < ref_end) { |
f186373f MF |
1197 | if (log_ref_ver) { |
1198 | ret = extref_get_fields(eb, ref_ptr, &namelen, &name, | |
1199 | &ref_index, &parent_objectid); | |
1200 | /* | |
1201 | * parent object can change from one array | |
1202 | * item to another. | |
1203 | */ | |
1204 | if (!dir) | |
1205 | dir = read_one_inode(root, parent_objectid); | |
03b2f08b GB |
1206 | if (!dir) { |
1207 | ret = -ENOENT; | |
1208 | goto out; | |
1209 | } | |
f186373f MF |
1210 | } else { |
1211 | ret = ref_get_fields(eb, ref_ptr, &namelen, &name, | |
1212 | &ref_index); | |
1213 | } | |
1214 | if (ret) | |
03b2f08b | 1215 | goto out; |
5a1d7843 JS |
1216 | |
1217 | /* if we already have a perfect match, we're done */ | |
1218 | if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode), | |
f186373f | 1219 | ref_index, name, namelen)) { |
5a1d7843 JS |
1220 | /* |
1221 | * look for a conflicting back reference in the | |
1222 | * metadata. if we find one we have to unlink that name | |
1223 | * of the file before we add our new link. Later on, we | |
1224 | * overwrite any existing back reference, and we don't | |
1225 | * want to create dangling pointers in the directory. | |
1226 | */ | |
1227 | ||
1228 | if (!search_done) { | |
1229 | ret = __add_inode_ref(trans, root, path, log, | |
f186373f MF |
1230 | dir, inode, eb, |
1231 | inode_objectid, | |
1232 | parent_objectid, | |
1233 | ref_index, name, namelen, | |
5a1d7843 | 1234 | &search_done); |
03b2f08b GB |
1235 | if (ret) { |
1236 | if (ret == 1) | |
1237 | ret = 0; | |
3650860b JB |
1238 | goto out; |
1239 | } | |
5a1d7843 JS |
1240 | } |
1241 | ||
1242 | /* insert our name */ | |
1243 | ret = btrfs_add_link(trans, dir, inode, name, namelen, | |
f186373f | 1244 | 0, ref_index); |
3650860b JB |
1245 | if (ret) |
1246 | goto out; | |
5a1d7843 JS |
1247 | |
1248 | btrfs_update_inode(trans, root, inode); | |
1249 | } | |
1250 | ||
f186373f | 1251 | ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen; |
5a1d7843 | 1252 | kfree(name); |
03b2f08b | 1253 | name = NULL; |
f186373f MF |
1254 | if (log_ref_ver) { |
1255 | iput(dir); | |
1256 | dir = NULL; | |
1257 | } | |
5a1d7843 | 1258 | } |
e02119d5 CM |
1259 | |
1260 | /* finally write the back reference in the inode */ | |
1261 | ret = overwrite_item(trans, root, path, eb, slot, key); | |
5a1d7843 | 1262 | out: |
b3b4aa74 | 1263 | btrfs_release_path(path); |
03b2f08b | 1264 | kfree(name); |
e02119d5 CM |
1265 | iput(dir); |
1266 | iput(inode); | |
3650860b | 1267 | return ret; |
e02119d5 CM |
1268 | } |
1269 | ||
c71bf099 | 1270 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
9c4f61f0 | 1271 | struct btrfs_root *root, u64 ino) |
c71bf099 YZ |
1272 | { |
1273 | int ret; | |
381cf658 | 1274 | |
9c4f61f0 DS |
1275 | ret = btrfs_insert_orphan_item(trans, root, ino); |
1276 | if (ret == -EEXIST) | |
1277 | ret = 0; | |
381cf658 | 1278 | |
c71bf099 YZ |
1279 | return ret; |
1280 | } | |
1281 | ||
f186373f MF |
1282 | static int count_inode_extrefs(struct btrfs_root *root, |
1283 | struct inode *inode, struct btrfs_path *path) | |
1284 | { | |
1285 | int ret = 0; | |
1286 | int name_len; | |
1287 | unsigned int nlink = 0; | |
1288 | u32 item_size; | |
1289 | u32 cur_offset = 0; | |
1290 | u64 inode_objectid = btrfs_ino(inode); | |
1291 | u64 offset = 0; | |
1292 | unsigned long ptr; | |
1293 | struct btrfs_inode_extref *extref; | |
1294 | struct extent_buffer *leaf; | |
1295 | ||
1296 | while (1) { | |
1297 | ret = btrfs_find_one_extref(root, inode_objectid, offset, path, | |
1298 | &extref, &offset); | |
1299 | if (ret) | |
1300 | break; | |
c71bf099 | 1301 | |
f186373f MF |
1302 | leaf = path->nodes[0]; |
1303 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1304 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
2c2c452b | 1305 | cur_offset = 0; |
f186373f MF |
1306 | |
1307 | while (cur_offset < item_size) { | |
1308 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); | |
1309 | name_len = btrfs_inode_extref_name_len(leaf, extref); | |
1310 | ||
1311 | nlink++; | |
1312 | ||
1313 | cur_offset += name_len + sizeof(*extref); | |
1314 | } | |
1315 | ||
1316 | offset++; | |
1317 | btrfs_release_path(path); | |
1318 | } | |
1319 | btrfs_release_path(path); | |
1320 | ||
2c2c452b | 1321 | if (ret < 0 && ret != -ENOENT) |
f186373f MF |
1322 | return ret; |
1323 | return nlink; | |
1324 | } | |
1325 | ||
1326 | static int count_inode_refs(struct btrfs_root *root, | |
1327 | struct inode *inode, struct btrfs_path *path) | |
e02119d5 | 1328 | { |
e02119d5 CM |
1329 | int ret; |
1330 | struct btrfs_key key; | |
f186373f | 1331 | unsigned int nlink = 0; |
e02119d5 CM |
1332 | unsigned long ptr; |
1333 | unsigned long ptr_end; | |
1334 | int name_len; | |
33345d01 | 1335 | u64 ino = btrfs_ino(inode); |
e02119d5 | 1336 | |
33345d01 | 1337 | key.objectid = ino; |
e02119d5 CM |
1338 | key.type = BTRFS_INODE_REF_KEY; |
1339 | key.offset = (u64)-1; | |
1340 | ||
d397712b | 1341 | while (1) { |
e02119d5 CM |
1342 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1343 | if (ret < 0) | |
1344 | break; | |
1345 | if (ret > 0) { | |
1346 | if (path->slots[0] == 0) | |
1347 | break; | |
1348 | path->slots[0]--; | |
1349 | } | |
e93ae26f | 1350 | process_slot: |
e02119d5 CM |
1351 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
1352 | path->slots[0]); | |
33345d01 | 1353 | if (key.objectid != ino || |
e02119d5 CM |
1354 | key.type != BTRFS_INODE_REF_KEY) |
1355 | break; | |
1356 | ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); | |
1357 | ptr_end = ptr + btrfs_item_size_nr(path->nodes[0], | |
1358 | path->slots[0]); | |
d397712b | 1359 | while (ptr < ptr_end) { |
e02119d5 CM |
1360 | struct btrfs_inode_ref *ref; |
1361 | ||
1362 | ref = (struct btrfs_inode_ref *)ptr; | |
1363 | name_len = btrfs_inode_ref_name_len(path->nodes[0], | |
1364 | ref); | |
1365 | ptr = (unsigned long)(ref + 1) + name_len; | |
1366 | nlink++; | |
1367 | } | |
1368 | ||
1369 | if (key.offset == 0) | |
1370 | break; | |
e93ae26f FDBM |
1371 | if (path->slots[0] > 0) { |
1372 | path->slots[0]--; | |
1373 | goto process_slot; | |
1374 | } | |
e02119d5 | 1375 | key.offset--; |
b3b4aa74 | 1376 | btrfs_release_path(path); |
e02119d5 | 1377 | } |
b3b4aa74 | 1378 | btrfs_release_path(path); |
f186373f MF |
1379 | |
1380 | return nlink; | |
1381 | } | |
1382 | ||
1383 | /* | |
1384 | * There are a few corners where the link count of the file can't | |
1385 | * be properly maintained during replay. So, instead of adding | |
1386 | * lots of complexity to the log code, we just scan the backrefs | |
1387 | * for any file that has been through replay. | |
1388 | * | |
1389 | * The scan will update the link count on the inode to reflect the | |
1390 | * number of back refs found. If it goes down to zero, the iput | |
1391 | * will free the inode. | |
1392 | */ | |
1393 | static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | |
1394 | struct btrfs_root *root, | |
1395 | struct inode *inode) | |
1396 | { | |
1397 | struct btrfs_path *path; | |
1398 | int ret; | |
1399 | u64 nlink = 0; | |
1400 | u64 ino = btrfs_ino(inode); | |
1401 | ||
1402 | path = btrfs_alloc_path(); | |
1403 | if (!path) | |
1404 | return -ENOMEM; | |
1405 | ||
1406 | ret = count_inode_refs(root, inode, path); | |
1407 | if (ret < 0) | |
1408 | goto out; | |
1409 | ||
1410 | nlink = ret; | |
1411 | ||
1412 | ret = count_inode_extrefs(root, inode, path); | |
f186373f MF |
1413 | if (ret < 0) |
1414 | goto out; | |
1415 | ||
1416 | nlink += ret; | |
1417 | ||
1418 | ret = 0; | |
1419 | ||
e02119d5 | 1420 | if (nlink != inode->i_nlink) { |
bfe86848 | 1421 | set_nlink(inode, nlink); |
e02119d5 CM |
1422 | btrfs_update_inode(trans, root, inode); |
1423 | } | |
8d5bf1cb | 1424 | BTRFS_I(inode)->index_cnt = (u64)-1; |
e02119d5 | 1425 | |
c71bf099 YZ |
1426 | if (inode->i_nlink == 0) { |
1427 | if (S_ISDIR(inode->i_mode)) { | |
1428 | ret = replay_dir_deletes(trans, root, NULL, path, | |
33345d01 | 1429 | ino, 1); |
3650860b JB |
1430 | if (ret) |
1431 | goto out; | |
c71bf099 | 1432 | } |
33345d01 | 1433 | ret = insert_orphan_item(trans, root, ino); |
12fcfd22 | 1434 | } |
12fcfd22 | 1435 | |
f186373f MF |
1436 | out: |
1437 | btrfs_free_path(path); | |
1438 | return ret; | |
e02119d5 CM |
1439 | } |
1440 | ||
1441 | static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | |
1442 | struct btrfs_root *root, | |
1443 | struct btrfs_path *path) | |
1444 | { | |
1445 | int ret; | |
1446 | struct btrfs_key key; | |
1447 | struct inode *inode; | |
1448 | ||
1449 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
1450 | key.type = BTRFS_ORPHAN_ITEM_KEY; | |
1451 | key.offset = (u64)-1; | |
d397712b | 1452 | while (1) { |
e02119d5 CM |
1453 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1454 | if (ret < 0) | |
1455 | break; | |
1456 | ||
1457 | if (ret == 1) { | |
1458 | if (path->slots[0] == 0) | |
1459 | break; | |
1460 | path->slots[0]--; | |
1461 | } | |
1462 | ||
1463 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1464 | if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID || | |
1465 | key.type != BTRFS_ORPHAN_ITEM_KEY) | |
1466 | break; | |
1467 | ||
1468 | ret = btrfs_del_item(trans, root, path); | |
65a246c5 TI |
1469 | if (ret) |
1470 | goto out; | |
e02119d5 | 1471 | |
b3b4aa74 | 1472 | btrfs_release_path(path); |
e02119d5 | 1473 | inode = read_one_inode(root, key.offset); |
c00e9493 TI |
1474 | if (!inode) |
1475 | return -EIO; | |
e02119d5 CM |
1476 | |
1477 | ret = fixup_inode_link_count(trans, root, inode); | |
e02119d5 | 1478 | iput(inode); |
3650860b JB |
1479 | if (ret) |
1480 | goto out; | |
e02119d5 | 1481 | |
12fcfd22 CM |
1482 | /* |
1483 | * fixup on a directory may create new entries, | |
1484 | * make sure we always look for the highset possible | |
1485 | * offset | |
1486 | */ | |
1487 | key.offset = (u64)-1; | |
e02119d5 | 1488 | } |
65a246c5 TI |
1489 | ret = 0; |
1490 | out: | |
b3b4aa74 | 1491 | btrfs_release_path(path); |
65a246c5 | 1492 | return ret; |
e02119d5 CM |
1493 | } |
1494 | ||
1495 | ||
1496 | /* | |
1497 | * record a given inode in the fixup dir so we can check its link | |
1498 | * count when replay is done. The link count is incremented here | |
1499 | * so the inode won't go away until we check it | |
1500 | */ | |
1501 | static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | |
1502 | struct btrfs_root *root, | |
1503 | struct btrfs_path *path, | |
1504 | u64 objectid) | |
1505 | { | |
1506 | struct btrfs_key key; | |
1507 | int ret = 0; | |
1508 | struct inode *inode; | |
1509 | ||
1510 | inode = read_one_inode(root, objectid); | |
c00e9493 TI |
1511 | if (!inode) |
1512 | return -EIO; | |
e02119d5 CM |
1513 | |
1514 | key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; | |
962a298f | 1515 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
e02119d5 CM |
1516 | key.offset = objectid; |
1517 | ||
1518 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
1519 | ||
b3b4aa74 | 1520 | btrfs_release_path(path); |
e02119d5 | 1521 | if (ret == 0) { |
9bf7a489 JB |
1522 | if (!inode->i_nlink) |
1523 | set_nlink(inode, 1); | |
1524 | else | |
8b558c5f | 1525 | inc_nlink(inode); |
b9959295 | 1526 | ret = btrfs_update_inode(trans, root, inode); |
e02119d5 CM |
1527 | } else if (ret == -EEXIST) { |
1528 | ret = 0; | |
1529 | } else { | |
3650860b | 1530 | BUG(); /* Logic Error */ |
e02119d5 CM |
1531 | } |
1532 | iput(inode); | |
1533 | ||
1534 | return ret; | |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * when replaying the log for a directory, we only insert names | |
1539 | * for inodes that actually exist. This means an fsync on a directory | |
1540 | * does not implicitly fsync all the new files in it | |
1541 | */ | |
1542 | static noinline int insert_one_name(struct btrfs_trans_handle *trans, | |
1543 | struct btrfs_root *root, | |
1544 | struct btrfs_path *path, | |
1545 | u64 dirid, u64 index, | |
1546 | char *name, int name_len, u8 type, | |
1547 | struct btrfs_key *location) | |
1548 | { | |
1549 | struct inode *inode; | |
1550 | struct inode *dir; | |
1551 | int ret; | |
1552 | ||
1553 | inode = read_one_inode(root, location->objectid); | |
1554 | if (!inode) | |
1555 | return -ENOENT; | |
1556 | ||
1557 | dir = read_one_inode(root, dirid); | |
1558 | if (!dir) { | |
1559 | iput(inode); | |
1560 | return -EIO; | |
1561 | } | |
d555438b | 1562 | |
e02119d5 CM |
1563 | ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index); |
1564 | ||
1565 | /* FIXME, put inode into FIXUP list */ | |
1566 | ||
1567 | iput(inode); | |
1568 | iput(dir); | |
1569 | return ret; | |
1570 | } | |
1571 | ||
df8d116f FM |
1572 | /* |
1573 | * Return true if an inode reference exists in the log for the given name, | |
1574 | * inode and parent inode. | |
1575 | */ | |
1576 | static bool name_in_log_ref(struct btrfs_root *log_root, | |
1577 | const char *name, const int name_len, | |
1578 | const u64 dirid, const u64 ino) | |
1579 | { | |
1580 | struct btrfs_key search_key; | |
1581 | ||
1582 | search_key.objectid = ino; | |
1583 | search_key.type = BTRFS_INODE_REF_KEY; | |
1584 | search_key.offset = dirid; | |
1585 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) | |
1586 | return true; | |
1587 | ||
1588 | search_key.type = BTRFS_INODE_EXTREF_KEY; | |
1589 | search_key.offset = btrfs_extref_hash(dirid, name, name_len); | |
1590 | if (backref_in_log(log_root, &search_key, dirid, name, name_len)) | |
1591 | return true; | |
1592 | ||
1593 | return false; | |
1594 | } | |
1595 | ||
e02119d5 CM |
1596 | /* |
1597 | * take a single entry in a log directory item and replay it into | |
1598 | * the subvolume. | |
1599 | * | |
1600 | * if a conflicting item exists in the subdirectory already, | |
1601 | * the inode it points to is unlinked and put into the link count | |
1602 | * fix up tree. | |
1603 | * | |
1604 | * If a name from the log points to a file or directory that does | |
1605 | * not exist in the FS, it is skipped. fsyncs on directories | |
1606 | * do not force down inodes inside that directory, just changes to the | |
1607 | * names or unlinks in a directory. | |
1608 | */ | |
1609 | static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |
1610 | struct btrfs_root *root, | |
1611 | struct btrfs_path *path, | |
1612 | struct extent_buffer *eb, | |
1613 | struct btrfs_dir_item *di, | |
1614 | struct btrfs_key *key) | |
1615 | { | |
1616 | char *name; | |
1617 | int name_len; | |
1618 | struct btrfs_dir_item *dst_di; | |
1619 | struct btrfs_key found_key; | |
1620 | struct btrfs_key log_key; | |
1621 | struct inode *dir; | |
e02119d5 | 1622 | u8 log_type; |
4bef0848 | 1623 | int exists; |
3650860b | 1624 | int ret = 0; |
d555438b | 1625 | bool update_size = (key->type == BTRFS_DIR_INDEX_KEY); |
e02119d5 CM |
1626 | |
1627 | dir = read_one_inode(root, key->objectid); | |
c00e9493 TI |
1628 | if (!dir) |
1629 | return -EIO; | |
e02119d5 CM |
1630 | |
1631 | name_len = btrfs_dir_name_len(eb, di); | |
1632 | name = kmalloc(name_len, GFP_NOFS); | |
2bac325e FDBM |
1633 | if (!name) { |
1634 | ret = -ENOMEM; | |
1635 | goto out; | |
1636 | } | |
2a29edc6 | 1637 | |
e02119d5 CM |
1638 | log_type = btrfs_dir_type(eb, di); |
1639 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1640 | name_len); | |
1641 | ||
1642 | btrfs_dir_item_key_to_cpu(eb, di, &log_key); | |
4bef0848 CM |
1643 | exists = btrfs_lookup_inode(trans, root, path, &log_key, 0); |
1644 | if (exists == 0) | |
1645 | exists = 1; | |
1646 | else | |
1647 | exists = 0; | |
b3b4aa74 | 1648 | btrfs_release_path(path); |
4bef0848 | 1649 | |
e02119d5 CM |
1650 | if (key->type == BTRFS_DIR_ITEM_KEY) { |
1651 | dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid, | |
1652 | name, name_len, 1); | |
d397712b | 1653 | } else if (key->type == BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
1654 | dst_di = btrfs_lookup_dir_index_item(trans, root, path, |
1655 | key->objectid, | |
1656 | key->offset, name, | |
1657 | name_len, 1); | |
1658 | } else { | |
3650860b JB |
1659 | /* Corruption */ |
1660 | ret = -EINVAL; | |
1661 | goto out; | |
e02119d5 | 1662 | } |
c704005d | 1663 | if (IS_ERR_OR_NULL(dst_di)) { |
e02119d5 CM |
1664 | /* we need a sequence number to insert, so we only |
1665 | * do inserts for the BTRFS_DIR_INDEX_KEY types | |
1666 | */ | |
1667 | if (key->type != BTRFS_DIR_INDEX_KEY) | |
1668 | goto out; | |
1669 | goto insert; | |
1670 | } | |
1671 | ||
1672 | btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key); | |
1673 | /* the existing item matches the logged item */ | |
1674 | if (found_key.objectid == log_key.objectid && | |
1675 | found_key.type == log_key.type && | |
1676 | found_key.offset == log_key.offset && | |
1677 | btrfs_dir_type(path->nodes[0], dst_di) == log_type) { | |
a2cc11db | 1678 | update_size = false; |
e02119d5 CM |
1679 | goto out; |
1680 | } | |
1681 | ||
1682 | /* | |
1683 | * don't drop the conflicting directory entry if the inode | |
1684 | * for the new entry doesn't exist | |
1685 | */ | |
4bef0848 | 1686 | if (!exists) |
e02119d5 CM |
1687 | goto out; |
1688 | ||
e02119d5 | 1689 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); |
3650860b JB |
1690 | if (ret) |
1691 | goto out; | |
e02119d5 CM |
1692 | |
1693 | if (key->type == BTRFS_DIR_INDEX_KEY) | |
1694 | goto insert; | |
1695 | out: | |
b3b4aa74 | 1696 | btrfs_release_path(path); |
d555438b JB |
1697 | if (!ret && update_size) { |
1698 | btrfs_i_size_write(dir, dir->i_size + name_len * 2); | |
1699 | ret = btrfs_update_inode(trans, root, dir); | |
1700 | } | |
e02119d5 CM |
1701 | kfree(name); |
1702 | iput(dir); | |
3650860b | 1703 | return ret; |
e02119d5 CM |
1704 | |
1705 | insert: | |
df8d116f FM |
1706 | if (name_in_log_ref(root->log_root, name, name_len, |
1707 | key->objectid, log_key.objectid)) { | |
1708 | /* The dentry will be added later. */ | |
1709 | ret = 0; | |
1710 | update_size = false; | |
1711 | goto out; | |
1712 | } | |
b3b4aa74 | 1713 | btrfs_release_path(path); |
e02119d5 CM |
1714 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, |
1715 | name, name_len, log_type, &log_key); | |
df8d116f | 1716 | if (ret && ret != -ENOENT && ret != -EEXIST) |
3650860b | 1717 | goto out; |
d555438b | 1718 | update_size = false; |
3650860b | 1719 | ret = 0; |
e02119d5 CM |
1720 | goto out; |
1721 | } | |
1722 | ||
1723 | /* | |
1724 | * find all the names in a directory item and reconcile them into | |
1725 | * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than | |
1726 | * one name in a directory item, but the same code gets used for | |
1727 | * both directory index types | |
1728 | */ | |
1729 | static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | |
1730 | struct btrfs_root *root, | |
1731 | struct btrfs_path *path, | |
1732 | struct extent_buffer *eb, int slot, | |
1733 | struct btrfs_key *key) | |
1734 | { | |
1735 | int ret; | |
1736 | u32 item_size = btrfs_item_size_nr(eb, slot); | |
1737 | struct btrfs_dir_item *di; | |
1738 | int name_len; | |
1739 | unsigned long ptr; | |
1740 | unsigned long ptr_end; | |
1741 | ||
1742 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1743 | ptr_end = ptr + item_size; | |
d397712b | 1744 | while (ptr < ptr_end) { |
e02119d5 | 1745 | di = (struct btrfs_dir_item *)ptr; |
22a94d44 JB |
1746 | if (verify_dir_item(root, eb, di)) |
1747 | return -EIO; | |
e02119d5 CM |
1748 | name_len = btrfs_dir_name_len(eb, di); |
1749 | ret = replay_one_name(trans, root, path, eb, di, key); | |
3650860b JB |
1750 | if (ret) |
1751 | return ret; | |
e02119d5 CM |
1752 | ptr = (unsigned long)(di + 1); |
1753 | ptr += name_len; | |
1754 | } | |
1755 | return 0; | |
1756 | } | |
1757 | ||
1758 | /* | |
1759 | * directory replay has two parts. There are the standard directory | |
1760 | * items in the log copied from the subvolume, and range items | |
1761 | * created in the log while the subvolume was logged. | |
1762 | * | |
1763 | * The range items tell us which parts of the key space the log | |
1764 | * is authoritative for. During replay, if a key in the subvolume | |
1765 | * directory is in a logged range item, but not actually in the log | |
1766 | * that means it was deleted from the directory before the fsync | |
1767 | * and should be removed. | |
1768 | */ | |
1769 | static noinline int find_dir_range(struct btrfs_root *root, | |
1770 | struct btrfs_path *path, | |
1771 | u64 dirid, int key_type, | |
1772 | u64 *start_ret, u64 *end_ret) | |
1773 | { | |
1774 | struct btrfs_key key; | |
1775 | u64 found_end; | |
1776 | struct btrfs_dir_log_item *item; | |
1777 | int ret; | |
1778 | int nritems; | |
1779 | ||
1780 | if (*start_ret == (u64)-1) | |
1781 | return 1; | |
1782 | ||
1783 | key.objectid = dirid; | |
1784 | key.type = key_type; | |
1785 | key.offset = *start_ret; | |
1786 | ||
1787 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1788 | if (ret < 0) | |
1789 | goto out; | |
1790 | if (ret > 0) { | |
1791 | if (path->slots[0] == 0) | |
1792 | goto out; | |
1793 | path->slots[0]--; | |
1794 | } | |
1795 | if (ret != 0) | |
1796 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1797 | ||
1798 | if (key.type != key_type || key.objectid != dirid) { | |
1799 | ret = 1; | |
1800 | goto next; | |
1801 | } | |
1802 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1803 | struct btrfs_dir_log_item); | |
1804 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1805 | ||
1806 | if (*start_ret >= key.offset && *start_ret <= found_end) { | |
1807 | ret = 0; | |
1808 | *start_ret = key.offset; | |
1809 | *end_ret = found_end; | |
1810 | goto out; | |
1811 | } | |
1812 | ret = 1; | |
1813 | next: | |
1814 | /* check the next slot in the tree to see if it is a valid item */ | |
1815 | nritems = btrfs_header_nritems(path->nodes[0]); | |
1816 | if (path->slots[0] >= nritems) { | |
1817 | ret = btrfs_next_leaf(root, path); | |
1818 | if (ret) | |
1819 | goto out; | |
1820 | } else { | |
1821 | path->slots[0]++; | |
1822 | } | |
1823 | ||
1824 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1825 | ||
1826 | if (key.type != key_type || key.objectid != dirid) { | |
1827 | ret = 1; | |
1828 | goto out; | |
1829 | } | |
1830 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1831 | struct btrfs_dir_log_item); | |
1832 | found_end = btrfs_dir_log_end(path->nodes[0], item); | |
1833 | *start_ret = key.offset; | |
1834 | *end_ret = found_end; | |
1835 | ret = 0; | |
1836 | out: | |
b3b4aa74 | 1837 | btrfs_release_path(path); |
e02119d5 CM |
1838 | return ret; |
1839 | } | |
1840 | ||
1841 | /* | |
1842 | * this looks for a given directory item in the log. If the directory | |
1843 | * item is not in the log, the item is removed and the inode it points | |
1844 | * to is unlinked | |
1845 | */ | |
1846 | static noinline int check_item_in_log(struct btrfs_trans_handle *trans, | |
1847 | struct btrfs_root *root, | |
1848 | struct btrfs_root *log, | |
1849 | struct btrfs_path *path, | |
1850 | struct btrfs_path *log_path, | |
1851 | struct inode *dir, | |
1852 | struct btrfs_key *dir_key) | |
1853 | { | |
1854 | int ret; | |
1855 | struct extent_buffer *eb; | |
1856 | int slot; | |
1857 | u32 item_size; | |
1858 | struct btrfs_dir_item *di; | |
1859 | struct btrfs_dir_item *log_di; | |
1860 | int name_len; | |
1861 | unsigned long ptr; | |
1862 | unsigned long ptr_end; | |
1863 | char *name; | |
1864 | struct inode *inode; | |
1865 | struct btrfs_key location; | |
1866 | ||
1867 | again: | |
1868 | eb = path->nodes[0]; | |
1869 | slot = path->slots[0]; | |
1870 | item_size = btrfs_item_size_nr(eb, slot); | |
1871 | ptr = btrfs_item_ptr_offset(eb, slot); | |
1872 | ptr_end = ptr + item_size; | |
d397712b | 1873 | while (ptr < ptr_end) { |
e02119d5 | 1874 | di = (struct btrfs_dir_item *)ptr; |
22a94d44 JB |
1875 | if (verify_dir_item(root, eb, di)) { |
1876 | ret = -EIO; | |
1877 | goto out; | |
1878 | } | |
1879 | ||
e02119d5 CM |
1880 | name_len = btrfs_dir_name_len(eb, di); |
1881 | name = kmalloc(name_len, GFP_NOFS); | |
1882 | if (!name) { | |
1883 | ret = -ENOMEM; | |
1884 | goto out; | |
1885 | } | |
1886 | read_extent_buffer(eb, name, (unsigned long)(di + 1), | |
1887 | name_len); | |
1888 | log_di = NULL; | |
12fcfd22 | 1889 | if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) { |
e02119d5 CM |
1890 | log_di = btrfs_lookup_dir_item(trans, log, log_path, |
1891 | dir_key->objectid, | |
1892 | name, name_len, 0); | |
12fcfd22 | 1893 | } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) { |
e02119d5 CM |
1894 | log_di = btrfs_lookup_dir_index_item(trans, log, |
1895 | log_path, | |
1896 | dir_key->objectid, | |
1897 | dir_key->offset, | |
1898 | name, name_len, 0); | |
1899 | } | |
269d040f | 1900 | if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) { |
e02119d5 | 1901 | btrfs_dir_item_key_to_cpu(eb, di, &location); |
b3b4aa74 DS |
1902 | btrfs_release_path(path); |
1903 | btrfs_release_path(log_path); | |
e02119d5 | 1904 | inode = read_one_inode(root, location.objectid); |
c00e9493 TI |
1905 | if (!inode) { |
1906 | kfree(name); | |
1907 | return -EIO; | |
1908 | } | |
e02119d5 CM |
1909 | |
1910 | ret = link_to_fixup_dir(trans, root, | |
1911 | path, location.objectid); | |
3650860b JB |
1912 | if (ret) { |
1913 | kfree(name); | |
1914 | iput(inode); | |
1915 | goto out; | |
1916 | } | |
1917 | ||
8b558c5f | 1918 | inc_nlink(inode); |
e02119d5 CM |
1919 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
1920 | name, name_len); | |
3650860b | 1921 | if (!ret) |
ada9af21 | 1922 | ret = btrfs_run_delayed_items(trans, root); |
e02119d5 CM |
1923 | kfree(name); |
1924 | iput(inode); | |
3650860b JB |
1925 | if (ret) |
1926 | goto out; | |
e02119d5 CM |
1927 | |
1928 | /* there might still be more names under this key | |
1929 | * check and repeat if required | |
1930 | */ | |
1931 | ret = btrfs_search_slot(NULL, root, dir_key, path, | |
1932 | 0, 0); | |
1933 | if (ret == 0) | |
1934 | goto again; | |
1935 | ret = 0; | |
1936 | goto out; | |
269d040f FDBM |
1937 | } else if (IS_ERR(log_di)) { |
1938 | kfree(name); | |
1939 | return PTR_ERR(log_di); | |
e02119d5 | 1940 | } |
b3b4aa74 | 1941 | btrfs_release_path(log_path); |
e02119d5 CM |
1942 | kfree(name); |
1943 | ||
1944 | ptr = (unsigned long)(di + 1); | |
1945 | ptr += name_len; | |
1946 | } | |
1947 | ret = 0; | |
1948 | out: | |
b3b4aa74 DS |
1949 | btrfs_release_path(path); |
1950 | btrfs_release_path(log_path); | |
e02119d5 CM |
1951 | return ret; |
1952 | } | |
1953 | ||
1954 | /* | |
1955 | * deletion replay happens before we copy any new directory items | |
1956 | * out of the log or out of backreferences from inodes. It | |
1957 | * scans the log to find ranges of keys that log is authoritative for, | |
1958 | * and then scans the directory to find items in those ranges that are | |
1959 | * not present in the log. | |
1960 | * | |
1961 | * Anything we don't find in the log is unlinked and removed from the | |
1962 | * directory. | |
1963 | */ | |
1964 | static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans, | |
1965 | struct btrfs_root *root, | |
1966 | struct btrfs_root *log, | |
1967 | struct btrfs_path *path, | |
12fcfd22 | 1968 | u64 dirid, int del_all) |
e02119d5 CM |
1969 | { |
1970 | u64 range_start; | |
1971 | u64 range_end; | |
1972 | int key_type = BTRFS_DIR_LOG_ITEM_KEY; | |
1973 | int ret = 0; | |
1974 | struct btrfs_key dir_key; | |
1975 | struct btrfs_key found_key; | |
1976 | struct btrfs_path *log_path; | |
1977 | struct inode *dir; | |
1978 | ||
1979 | dir_key.objectid = dirid; | |
1980 | dir_key.type = BTRFS_DIR_ITEM_KEY; | |
1981 | log_path = btrfs_alloc_path(); | |
1982 | if (!log_path) | |
1983 | return -ENOMEM; | |
1984 | ||
1985 | dir = read_one_inode(root, dirid); | |
1986 | /* it isn't an error if the inode isn't there, that can happen | |
1987 | * because we replay the deletes before we copy in the inode item | |
1988 | * from the log | |
1989 | */ | |
1990 | if (!dir) { | |
1991 | btrfs_free_path(log_path); | |
1992 | return 0; | |
1993 | } | |
1994 | again: | |
1995 | range_start = 0; | |
1996 | range_end = 0; | |
d397712b | 1997 | while (1) { |
12fcfd22 CM |
1998 | if (del_all) |
1999 | range_end = (u64)-1; | |
2000 | else { | |
2001 | ret = find_dir_range(log, path, dirid, key_type, | |
2002 | &range_start, &range_end); | |
2003 | if (ret != 0) | |
2004 | break; | |
2005 | } | |
e02119d5 CM |
2006 | |
2007 | dir_key.offset = range_start; | |
d397712b | 2008 | while (1) { |
e02119d5 CM |
2009 | int nritems; |
2010 | ret = btrfs_search_slot(NULL, root, &dir_key, path, | |
2011 | 0, 0); | |
2012 | if (ret < 0) | |
2013 | goto out; | |
2014 | ||
2015 | nritems = btrfs_header_nritems(path->nodes[0]); | |
2016 | if (path->slots[0] >= nritems) { | |
2017 | ret = btrfs_next_leaf(root, path); | |
2018 | if (ret) | |
2019 | break; | |
2020 | } | |
2021 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2022 | path->slots[0]); | |
2023 | if (found_key.objectid != dirid || | |
2024 | found_key.type != dir_key.type) | |
2025 | goto next_type; | |
2026 | ||
2027 | if (found_key.offset > range_end) | |
2028 | break; | |
2029 | ||
2030 | ret = check_item_in_log(trans, root, log, path, | |
12fcfd22 CM |
2031 | log_path, dir, |
2032 | &found_key); | |
3650860b JB |
2033 | if (ret) |
2034 | goto out; | |
e02119d5 CM |
2035 | if (found_key.offset == (u64)-1) |
2036 | break; | |
2037 | dir_key.offset = found_key.offset + 1; | |
2038 | } | |
b3b4aa74 | 2039 | btrfs_release_path(path); |
e02119d5 CM |
2040 | if (range_end == (u64)-1) |
2041 | break; | |
2042 | range_start = range_end + 1; | |
2043 | } | |
2044 | ||
2045 | next_type: | |
2046 | ret = 0; | |
2047 | if (key_type == BTRFS_DIR_LOG_ITEM_KEY) { | |
2048 | key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
2049 | dir_key.type = BTRFS_DIR_INDEX_KEY; | |
b3b4aa74 | 2050 | btrfs_release_path(path); |
e02119d5 CM |
2051 | goto again; |
2052 | } | |
2053 | out: | |
b3b4aa74 | 2054 | btrfs_release_path(path); |
e02119d5 CM |
2055 | btrfs_free_path(log_path); |
2056 | iput(dir); | |
2057 | return ret; | |
2058 | } | |
2059 | ||
2060 | /* | |
2061 | * the process_func used to replay items from the log tree. This | |
2062 | * gets called in two different stages. The first stage just looks | |
2063 | * for inodes and makes sure they are all copied into the subvolume. | |
2064 | * | |
2065 | * The second stage copies all the other item types from the log into | |
2066 | * the subvolume. The two stage approach is slower, but gets rid of | |
2067 | * lots of complexity around inodes referencing other inodes that exist | |
2068 | * only in the log (references come from either directory items or inode | |
2069 | * back refs). | |
2070 | */ | |
2071 | static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |
2072 | struct walk_control *wc, u64 gen) | |
2073 | { | |
2074 | int nritems; | |
2075 | struct btrfs_path *path; | |
2076 | struct btrfs_root *root = wc->replay_dest; | |
2077 | struct btrfs_key key; | |
e02119d5 CM |
2078 | int level; |
2079 | int i; | |
2080 | int ret; | |
2081 | ||
018642a1 TI |
2082 | ret = btrfs_read_buffer(eb, gen); |
2083 | if (ret) | |
2084 | return ret; | |
e02119d5 CM |
2085 | |
2086 | level = btrfs_header_level(eb); | |
2087 | ||
2088 | if (level != 0) | |
2089 | return 0; | |
2090 | ||
2091 | path = btrfs_alloc_path(); | |
1e5063d0 MF |
2092 | if (!path) |
2093 | return -ENOMEM; | |
e02119d5 CM |
2094 | |
2095 | nritems = btrfs_header_nritems(eb); | |
2096 | for (i = 0; i < nritems; i++) { | |
2097 | btrfs_item_key_to_cpu(eb, &key, i); | |
e02119d5 CM |
2098 | |
2099 | /* inode keys are done during the first stage */ | |
2100 | if (key.type == BTRFS_INODE_ITEM_KEY && | |
2101 | wc->stage == LOG_WALK_REPLAY_INODES) { | |
e02119d5 CM |
2102 | struct btrfs_inode_item *inode_item; |
2103 | u32 mode; | |
2104 | ||
2105 | inode_item = btrfs_item_ptr(eb, i, | |
2106 | struct btrfs_inode_item); | |
2107 | mode = btrfs_inode_mode(eb, inode_item); | |
2108 | if (S_ISDIR(mode)) { | |
2109 | ret = replay_dir_deletes(wc->trans, | |
12fcfd22 | 2110 | root, log, path, key.objectid, 0); |
b50c6e25 JB |
2111 | if (ret) |
2112 | break; | |
e02119d5 CM |
2113 | } |
2114 | ret = overwrite_item(wc->trans, root, path, | |
2115 | eb, i, &key); | |
b50c6e25 JB |
2116 | if (ret) |
2117 | break; | |
e02119d5 | 2118 | |
c71bf099 YZ |
2119 | /* for regular files, make sure corresponding |
2120 | * orhpan item exist. extents past the new EOF | |
2121 | * will be truncated later by orphan cleanup. | |
e02119d5 CM |
2122 | */ |
2123 | if (S_ISREG(mode)) { | |
c71bf099 YZ |
2124 | ret = insert_orphan_item(wc->trans, root, |
2125 | key.objectid); | |
b50c6e25 JB |
2126 | if (ret) |
2127 | break; | |
e02119d5 | 2128 | } |
c71bf099 | 2129 | |
e02119d5 CM |
2130 | ret = link_to_fixup_dir(wc->trans, root, |
2131 | path, key.objectid); | |
b50c6e25 JB |
2132 | if (ret) |
2133 | break; | |
e02119d5 | 2134 | } |
dd8e7217 JB |
2135 | |
2136 | if (key.type == BTRFS_DIR_INDEX_KEY && | |
2137 | wc->stage == LOG_WALK_REPLAY_DIR_INDEX) { | |
2138 | ret = replay_one_dir_item(wc->trans, root, path, | |
2139 | eb, i, &key); | |
2140 | if (ret) | |
2141 | break; | |
2142 | } | |
2143 | ||
e02119d5 CM |
2144 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
2145 | continue; | |
2146 | ||
2147 | /* these keys are simply copied */ | |
2148 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | |
2149 | ret = overwrite_item(wc->trans, root, path, | |
2150 | eb, i, &key); | |
b50c6e25 JB |
2151 | if (ret) |
2152 | break; | |
2da1c669 LB |
2153 | } else if (key.type == BTRFS_INODE_REF_KEY || |
2154 | key.type == BTRFS_INODE_EXTREF_KEY) { | |
f186373f MF |
2155 | ret = add_inode_ref(wc->trans, root, log, path, |
2156 | eb, i, &key); | |
b50c6e25 JB |
2157 | if (ret && ret != -ENOENT) |
2158 | break; | |
2159 | ret = 0; | |
e02119d5 CM |
2160 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
2161 | ret = replay_one_extent(wc->trans, root, path, | |
2162 | eb, i, &key); | |
b50c6e25 JB |
2163 | if (ret) |
2164 | break; | |
dd8e7217 | 2165 | } else if (key.type == BTRFS_DIR_ITEM_KEY) { |
e02119d5 CM |
2166 | ret = replay_one_dir_item(wc->trans, root, path, |
2167 | eb, i, &key); | |
b50c6e25 JB |
2168 | if (ret) |
2169 | break; | |
e02119d5 CM |
2170 | } |
2171 | } | |
2172 | btrfs_free_path(path); | |
b50c6e25 | 2173 | return ret; |
e02119d5 CM |
2174 | } |
2175 | ||
d397712b | 2176 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2177 | struct btrfs_root *root, |
2178 | struct btrfs_path *path, int *level, | |
2179 | struct walk_control *wc) | |
2180 | { | |
2181 | u64 root_owner; | |
e02119d5 CM |
2182 | u64 bytenr; |
2183 | u64 ptr_gen; | |
2184 | struct extent_buffer *next; | |
2185 | struct extent_buffer *cur; | |
2186 | struct extent_buffer *parent; | |
2187 | u32 blocksize; | |
2188 | int ret = 0; | |
2189 | ||
2190 | WARN_ON(*level < 0); | |
2191 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
2192 | ||
d397712b | 2193 | while (*level > 0) { |
e02119d5 CM |
2194 | WARN_ON(*level < 0); |
2195 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
2196 | cur = path->nodes[*level]; | |
2197 | ||
fae7f21c | 2198 | WARN_ON(btrfs_header_level(cur) != *level); |
e02119d5 CM |
2199 | |
2200 | if (path->slots[*level] >= | |
2201 | btrfs_header_nritems(cur)) | |
2202 | break; | |
2203 | ||
2204 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
2205 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
707e8a07 | 2206 | blocksize = root->nodesize; |
e02119d5 CM |
2207 | |
2208 | parent = path->nodes[*level]; | |
2209 | root_owner = btrfs_header_owner(parent); | |
e02119d5 | 2210 | |
a83fffb7 | 2211 | next = btrfs_find_create_tree_block(root, bytenr); |
2a29edc6 | 2212 | if (!next) |
2213 | return -ENOMEM; | |
e02119d5 | 2214 | |
e02119d5 | 2215 | if (*level == 1) { |
1e5063d0 | 2216 | ret = wc->process_func(root, next, wc, ptr_gen); |
b50c6e25 JB |
2217 | if (ret) { |
2218 | free_extent_buffer(next); | |
1e5063d0 | 2219 | return ret; |
b50c6e25 | 2220 | } |
4a500fd1 | 2221 | |
e02119d5 CM |
2222 | path->slots[*level]++; |
2223 | if (wc->free) { | |
018642a1 TI |
2224 | ret = btrfs_read_buffer(next, ptr_gen); |
2225 | if (ret) { | |
2226 | free_extent_buffer(next); | |
2227 | return ret; | |
2228 | } | |
e02119d5 | 2229 | |
681ae509 JB |
2230 | if (trans) { |
2231 | btrfs_tree_lock(next); | |
2232 | btrfs_set_lock_blocking(next); | |
2233 | clean_tree_block(trans, root, next); | |
2234 | btrfs_wait_tree_block_writeback(next); | |
2235 | btrfs_tree_unlock(next); | |
2236 | } | |
e02119d5 | 2237 | |
e02119d5 CM |
2238 | WARN_ON(root_owner != |
2239 | BTRFS_TREE_LOG_OBJECTID); | |
e688b725 | 2240 | ret = btrfs_free_and_pin_reserved_extent(root, |
d00aff00 | 2241 | bytenr, blocksize); |
3650860b JB |
2242 | if (ret) { |
2243 | free_extent_buffer(next); | |
2244 | return ret; | |
2245 | } | |
e02119d5 CM |
2246 | } |
2247 | free_extent_buffer(next); | |
2248 | continue; | |
2249 | } | |
018642a1 TI |
2250 | ret = btrfs_read_buffer(next, ptr_gen); |
2251 | if (ret) { | |
2252 | free_extent_buffer(next); | |
2253 | return ret; | |
2254 | } | |
e02119d5 CM |
2255 | |
2256 | WARN_ON(*level <= 0); | |
2257 | if (path->nodes[*level-1]) | |
2258 | free_extent_buffer(path->nodes[*level-1]); | |
2259 | path->nodes[*level-1] = next; | |
2260 | *level = btrfs_header_level(next); | |
2261 | path->slots[*level] = 0; | |
2262 | cond_resched(); | |
2263 | } | |
2264 | WARN_ON(*level < 0); | |
2265 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
2266 | ||
4a500fd1 | 2267 | path->slots[*level] = btrfs_header_nritems(path->nodes[*level]); |
e02119d5 CM |
2268 | |
2269 | cond_resched(); | |
2270 | return 0; | |
2271 | } | |
2272 | ||
d397712b | 2273 | static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, |
e02119d5 CM |
2274 | struct btrfs_root *root, |
2275 | struct btrfs_path *path, int *level, | |
2276 | struct walk_control *wc) | |
2277 | { | |
2278 | u64 root_owner; | |
e02119d5 CM |
2279 | int i; |
2280 | int slot; | |
2281 | int ret; | |
2282 | ||
d397712b | 2283 | for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
e02119d5 | 2284 | slot = path->slots[i]; |
4a500fd1 | 2285 | if (slot + 1 < btrfs_header_nritems(path->nodes[i])) { |
e02119d5 CM |
2286 | path->slots[i]++; |
2287 | *level = i; | |
2288 | WARN_ON(*level == 0); | |
2289 | return 0; | |
2290 | } else { | |
31840ae1 ZY |
2291 | struct extent_buffer *parent; |
2292 | if (path->nodes[*level] == root->node) | |
2293 | parent = path->nodes[*level]; | |
2294 | else | |
2295 | parent = path->nodes[*level + 1]; | |
2296 | ||
2297 | root_owner = btrfs_header_owner(parent); | |
1e5063d0 | 2298 | ret = wc->process_func(root, path->nodes[*level], wc, |
e02119d5 | 2299 | btrfs_header_generation(path->nodes[*level])); |
1e5063d0 MF |
2300 | if (ret) |
2301 | return ret; | |
2302 | ||
e02119d5 CM |
2303 | if (wc->free) { |
2304 | struct extent_buffer *next; | |
2305 | ||
2306 | next = path->nodes[*level]; | |
2307 | ||
681ae509 JB |
2308 | if (trans) { |
2309 | btrfs_tree_lock(next); | |
2310 | btrfs_set_lock_blocking(next); | |
2311 | clean_tree_block(trans, root, next); | |
2312 | btrfs_wait_tree_block_writeback(next); | |
2313 | btrfs_tree_unlock(next); | |
2314 | } | |
e02119d5 | 2315 | |
e02119d5 | 2316 | WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); |
e688b725 | 2317 | ret = btrfs_free_and_pin_reserved_extent(root, |
e02119d5 | 2318 | path->nodes[*level]->start, |
d00aff00 | 2319 | path->nodes[*level]->len); |
3650860b JB |
2320 | if (ret) |
2321 | return ret; | |
e02119d5 CM |
2322 | } |
2323 | free_extent_buffer(path->nodes[*level]); | |
2324 | path->nodes[*level] = NULL; | |
2325 | *level = i + 1; | |
2326 | } | |
2327 | } | |
2328 | return 1; | |
2329 | } | |
2330 | ||
2331 | /* | |
2332 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
2333 | * the tree freeing any blocks that have a ref count of zero after being | |
2334 | * decremented. | |
2335 | */ | |
2336 | static int walk_log_tree(struct btrfs_trans_handle *trans, | |
2337 | struct btrfs_root *log, struct walk_control *wc) | |
2338 | { | |
2339 | int ret = 0; | |
2340 | int wret; | |
2341 | int level; | |
2342 | struct btrfs_path *path; | |
e02119d5 CM |
2343 | int orig_level; |
2344 | ||
2345 | path = btrfs_alloc_path(); | |
db5b493a TI |
2346 | if (!path) |
2347 | return -ENOMEM; | |
e02119d5 CM |
2348 | |
2349 | level = btrfs_header_level(log->node); | |
2350 | orig_level = level; | |
2351 | path->nodes[level] = log->node; | |
2352 | extent_buffer_get(log->node); | |
2353 | path->slots[level] = 0; | |
2354 | ||
d397712b | 2355 | while (1) { |
e02119d5 CM |
2356 | wret = walk_down_log_tree(trans, log, path, &level, wc); |
2357 | if (wret > 0) | |
2358 | break; | |
79787eaa | 2359 | if (wret < 0) { |
e02119d5 | 2360 | ret = wret; |
79787eaa JM |
2361 | goto out; |
2362 | } | |
e02119d5 CM |
2363 | |
2364 | wret = walk_up_log_tree(trans, log, path, &level, wc); | |
2365 | if (wret > 0) | |
2366 | break; | |
79787eaa | 2367 | if (wret < 0) { |
e02119d5 | 2368 | ret = wret; |
79787eaa JM |
2369 | goto out; |
2370 | } | |
e02119d5 CM |
2371 | } |
2372 | ||
2373 | /* was the root node processed? if not, catch it here */ | |
2374 | if (path->nodes[orig_level]) { | |
79787eaa | 2375 | ret = wc->process_func(log, path->nodes[orig_level], wc, |
e02119d5 | 2376 | btrfs_header_generation(path->nodes[orig_level])); |
79787eaa JM |
2377 | if (ret) |
2378 | goto out; | |
e02119d5 CM |
2379 | if (wc->free) { |
2380 | struct extent_buffer *next; | |
2381 | ||
2382 | next = path->nodes[orig_level]; | |
2383 | ||
681ae509 JB |
2384 | if (trans) { |
2385 | btrfs_tree_lock(next); | |
2386 | btrfs_set_lock_blocking(next); | |
2387 | clean_tree_block(trans, log, next); | |
2388 | btrfs_wait_tree_block_writeback(next); | |
2389 | btrfs_tree_unlock(next); | |
2390 | } | |
e02119d5 | 2391 | |
e02119d5 CM |
2392 | WARN_ON(log->root_key.objectid != |
2393 | BTRFS_TREE_LOG_OBJECTID); | |
e688b725 | 2394 | ret = btrfs_free_and_pin_reserved_extent(log, next->start, |
d00aff00 | 2395 | next->len); |
3650860b JB |
2396 | if (ret) |
2397 | goto out; | |
e02119d5 CM |
2398 | } |
2399 | } | |
2400 | ||
79787eaa | 2401 | out: |
e02119d5 | 2402 | btrfs_free_path(path); |
e02119d5 CM |
2403 | return ret; |
2404 | } | |
2405 | ||
7237f183 YZ |
2406 | /* |
2407 | * helper function to update the item for a given subvolumes log root | |
2408 | * in the tree of log roots | |
2409 | */ | |
2410 | static int update_log_root(struct btrfs_trans_handle *trans, | |
2411 | struct btrfs_root *log) | |
2412 | { | |
2413 | int ret; | |
2414 | ||
2415 | if (log->log_transid == 1) { | |
2416 | /* insert root item on the first sync */ | |
2417 | ret = btrfs_insert_root(trans, log->fs_info->log_root_tree, | |
2418 | &log->root_key, &log->root_item); | |
2419 | } else { | |
2420 | ret = btrfs_update_root(trans, log->fs_info->log_root_tree, | |
2421 | &log->root_key, &log->root_item); | |
2422 | } | |
2423 | return ret; | |
2424 | } | |
2425 | ||
8b050d35 MX |
2426 | static void wait_log_commit(struct btrfs_trans_handle *trans, |
2427 | struct btrfs_root *root, int transid) | |
e02119d5 CM |
2428 | { |
2429 | DEFINE_WAIT(wait); | |
7237f183 | 2430 | int index = transid % 2; |
e02119d5 | 2431 | |
7237f183 YZ |
2432 | /* |
2433 | * we only allow two pending log transactions at a time, | |
2434 | * so we know that if ours is more than 2 older than the | |
2435 | * current transaction, we're done | |
2436 | */ | |
e02119d5 | 2437 | do { |
7237f183 YZ |
2438 | prepare_to_wait(&root->log_commit_wait[index], |
2439 | &wait, TASK_UNINTERRUPTIBLE); | |
2440 | mutex_unlock(&root->log_mutex); | |
12fcfd22 | 2441 | |
d1433deb | 2442 | if (root->log_transid_committed < transid && |
7237f183 YZ |
2443 | atomic_read(&root->log_commit[index])) |
2444 | schedule(); | |
12fcfd22 | 2445 | |
7237f183 YZ |
2446 | finish_wait(&root->log_commit_wait[index], &wait); |
2447 | mutex_lock(&root->log_mutex); | |
d1433deb | 2448 | } while (root->log_transid_committed < transid && |
7237f183 | 2449 | atomic_read(&root->log_commit[index])); |
7237f183 YZ |
2450 | } |
2451 | ||
143bede5 JM |
2452 | static void wait_for_writer(struct btrfs_trans_handle *trans, |
2453 | struct btrfs_root *root) | |
7237f183 YZ |
2454 | { |
2455 | DEFINE_WAIT(wait); | |
8b050d35 MX |
2456 | |
2457 | while (atomic_read(&root->log_writers)) { | |
7237f183 YZ |
2458 | prepare_to_wait(&root->log_writer_wait, |
2459 | &wait, TASK_UNINTERRUPTIBLE); | |
2460 | mutex_unlock(&root->log_mutex); | |
8b050d35 | 2461 | if (atomic_read(&root->log_writers)) |
e02119d5 | 2462 | schedule(); |
7237f183 | 2463 | finish_wait(&root->log_writer_wait, &wait); |
575849ec | 2464 | mutex_lock(&root->log_mutex); |
7237f183 | 2465 | } |
e02119d5 CM |
2466 | } |
2467 | ||
8b050d35 MX |
2468 | static inline void btrfs_remove_log_ctx(struct btrfs_root *root, |
2469 | struct btrfs_log_ctx *ctx) | |
2470 | { | |
2471 | if (!ctx) | |
2472 | return; | |
2473 | ||
2474 | mutex_lock(&root->log_mutex); | |
2475 | list_del_init(&ctx->list); | |
2476 | mutex_unlock(&root->log_mutex); | |
2477 | } | |
2478 | ||
2479 | /* | |
2480 | * Invoked in log mutex context, or be sure there is no other task which | |
2481 | * can access the list. | |
2482 | */ | |
2483 | static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, | |
2484 | int index, int error) | |
2485 | { | |
2486 | struct btrfs_log_ctx *ctx; | |
2487 | ||
2488 | if (!error) { | |
2489 | INIT_LIST_HEAD(&root->log_ctxs[index]); | |
2490 | return; | |
2491 | } | |
2492 | ||
2493 | list_for_each_entry(ctx, &root->log_ctxs[index], list) | |
2494 | ctx->log_ret = error; | |
2495 | ||
2496 | INIT_LIST_HEAD(&root->log_ctxs[index]); | |
2497 | } | |
2498 | ||
e02119d5 CM |
2499 | /* |
2500 | * btrfs_sync_log does sends a given tree log down to the disk and | |
2501 | * updates the super blocks to record it. When this call is done, | |
12fcfd22 CM |
2502 | * you know that any inodes previously logged are safely on disk only |
2503 | * if it returns 0. | |
2504 | * | |
2505 | * Any other return value means you need to call btrfs_commit_transaction. | |
2506 | * Some of the edge cases for fsyncing directories that have had unlinks | |
2507 | * or renames done in the past mean that sometimes the only safe | |
2508 | * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN, | |
2509 | * that has happened. | |
e02119d5 CM |
2510 | */ |
2511 | int btrfs_sync_log(struct btrfs_trans_handle *trans, | |
8b050d35 | 2512 | struct btrfs_root *root, struct btrfs_log_ctx *ctx) |
e02119d5 | 2513 | { |
7237f183 YZ |
2514 | int index1; |
2515 | int index2; | |
8cef4e16 | 2516 | int mark; |
e02119d5 | 2517 | int ret; |
e02119d5 | 2518 | struct btrfs_root *log = root->log_root; |
7237f183 | 2519 | struct btrfs_root *log_root_tree = root->fs_info->log_root_tree; |
bb14a59b | 2520 | int log_transid = 0; |
8b050d35 | 2521 | struct btrfs_log_ctx root_log_ctx; |
c6adc9cc | 2522 | struct blk_plug plug; |
e02119d5 | 2523 | |
7237f183 | 2524 | mutex_lock(&root->log_mutex); |
d1433deb MX |
2525 | log_transid = ctx->log_transid; |
2526 | if (root->log_transid_committed >= log_transid) { | |
2527 | mutex_unlock(&root->log_mutex); | |
2528 | return ctx->log_ret; | |
2529 | } | |
2530 | ||
2531 | index1 = log_transid % 2; | |
7237f183 | 2532 | if (atomic_read(&root->log_commit[index1])) { |
d1433deb | 2533 | wait_log_commit(trans, root, log_transid); |
7237f183 | 2534 | mutex_unlock(&root->log_mutex); |
8b050d35 | 2535 | return ctx->log_ret; |
e02119d5 | 2536 | } |
d1433deb | 2537 | ASSERT(log_transid == root->log_transid); |
7237f183 YZ |
2538 | atomic_set(&root->log_commit[index1], 1); |
2539 | ||
2540 | /* wait for previous tree log sync to complete */ | |
2541 | if (atomic_read(&root->log_commit[(index1 + 1) % 2])) | |
d1433deb | 2542 | wait_log_commit(trans, root, log_transid - 1); |
48cab2e0 | 2543 | |
86df7eb9 | 2544 | while (1) { |
2ecb7923 | 2545 | int batch = atomic_read(&root->log_batch); |
cd354ad6 | 2546 | /* when we're on an ssd, just kick the log commit out */ |
27cdeb70 MX |
2547 | if (!btrfs_test_opt(root, SSD) && |
2548 | test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { | |
86df7eb9 YZ |
2549 | mutex_unlock(&root->log_mutex); |
2550 | schedule_timeout_uninterruptible(1); | |
2551 | mutex_lock(&root->log_mutex); | |
2552 | } | |
12fcfd22 | 2553 | wait_for_writer(trans, root); |
2ecb7923 | 2554 | if (batch == atomic_read(&root->log_batch)) |
e02119d5 CM |
2555 | break; |
2556 | } | |
e02119d5 | 2557 | |
12fcfd22 | 2558 | /* bail out if we need to do a full commit */ |
995946dd | 2559 | if (btrfs_need_log_full_commit(root->fs_info, trans)) { |
12fcfd22 | 2560 | ret = -EAGAIN; |
2ab28f32 | 2561 | btrfs_free_logged_extents(log, log_transid); |
12fcfd22 CM |
2562 | mutex_unlock(&root->log_mutex); |
2563 | goto out; | |
2564 | } | |
2565 | ||
8cef4e16 YZ |
2566 | if (log_transid % 2 == 0) |
2567 | mark = EXTENT_DIRTY; | |
2568 | else | |
2569 | mark = EXTENT_NEW; | |
2570 | ||
690587d1 CM |
2571 | /* we start IO on all the marked extents here, but we don't actually |
2572 | * wait for them until later. | |
2573 | */ | |
c6adc9cc | 2574 | blk_start_plug(&plug); |
8cef4e16 | 2575 | ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark); |
79787eaa | 2576 | if (ret) { |
c6adc9cc | 2577 | blk_finish_plug(&plug); |
79787eaa | 2578 | btrfs_abort_transaction(trans, root, ret); |
2ab28f32 | 2579 | btrfs_free_logged_extents(log, log_transid); |
995946dd | 2580 | btrfs_set_log_full_commit(root->fs_info, trans); |
79787eaa JM |
2581 | mutex_unlock(&root->log_mutex); |
2582 | goto out; | |
2583 | } | |
7237f183 | 2584 | |
5d4f98a2 | 2585 | btrfs_set_root_node(&log->root_item, log->node); |
7237f183 | 2586 | |
7237f183 YZ |
2587 | root->log_transid++; |
2588 | log->log_transid = root->log_transid; | |
ff782e0a | 2589 | root->log_start_pid = 0; |
7237f183 | 2590 | /* |
8cef4e16 YZ |
2591 | * IO has been started, blocks of the log tree have WRITTEN flag set |
2592 | * in their headers. new modifications of the log will be written to | |
2593 | * new positions. so it's safe to allow log writers to go in. | |
7237f183 YZ |
2594 | */ |
2595 | mutex_unlock(&root->log_mutex); | |
2596 | ||
d1433deb MX |
2597 | btrfs_init_log_ctx(&root_log_ctx); |
2598 | ||
7237f183 | 2599 | mutex_lock(&log_root_tree->log_mutex); |
2ecb7923 | 2600 | atomic_inc(&log_root_tree->log_batch); |
7237f183 | 2601 | atomic_inc(&log_root_tree->log_writers); |
d1433deb MX |
2602 | |
2603 | index2 = log_root_tree->log_transid % 2; | |
2604 | list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]); | |
2605 | root_log_ctx.log_transid = log_root_tree->log_transid; | |
2606 | ||
7237f183 YZ |
2607 | mutex_unlock(&log_root_tree->log_mutex); |
2608 | ||
2609 | ret = update_log_root(trans, log); | |
7237f183 YZ |
2610 | |
2611 | mutex_lock(&log_root_tree->log_mutex); | |
2612 | if (atomic_dec_and_test(&log_root_tree->log_writers)) { | |
2613 | smp_mb(); | |
2614 | if (waitqueue_active(&log_root_tree->log_writer_wait)) | |
2615 | wake_up(&log_root_tree->log_writer_wait); | |
2616 | } | |
2617 | ||
4a500fd1 | 2618 | if (ret) { |
d1433deb MX |
2619 | if (!list_empty(&root_log_ctx.list)) |
2620 | list_del_init(&root_log_ctx.list); | |
2621 | ||
c6adc9cc | 2622 | blk_finish_plug(&plug); |
995946dd MX |
2623 | btrfs_set_log_full_commit(root->fs_info, trans); |
2624 | ||
79787eaa JM |
2625 | if (ret != -ENOSPC) { |
2626 | btrfs_abort_transaction(trans, root, ret); | |
2627 | mutex_unlock(&log_root_tree->log_mutex); | |
2628 | goto out; | |
2629 | } | |
4a500fd1 | 2630 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
2ab28f32 | 2631 | btrfs_free_logged_extents(log, log_transid); |
4a500fd1 YZ |
2632 | mutex_unlock(&log_root_tree->log_mutex); |
2633 | ret = -EAGAIN; | |
2634 | goto out; | |
2635 | } | |
2636 | ||
d1433deb MX |
2637 | if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) { |
2638 | mutex_unlock(&log_root_tree->log_mutex); | |
2639 | ret = root_log_ctx.log_ret; | |
2640 | goto out; | |
2641 | } | |
8b050d35 | 2642 | |
d1433deb | 2643 | index2 = root_log_ctx.log_transid % 2; |
7237f183 | 2644 | if (atomic_read(&log_root_tree->log_commit[index2])) { |
c6adc9cc | 2645 | blk_finish_plug(&plug); |
5ab5e44a FM |
2646 | ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, |
2647 | mark); | |
50d9aa99 | 2648 | btrfs_wait_logged_extents(trans, log, log_transid); |
8b050d35 | 2649 | wait_log_commit(trans, log_root_tree, |
d1433deb | 2650 | root_log_ctx.log_transid); |
7237f183 | 2651 | mutex_unlock(&log_root_tree->log_mutex); |
5ab5e44a FM |
2652 | if (!ret) |
2653 | ret = root_log_ctx.log_ret; | |
7237f183 YZ |
2654 | goto out; |
2655 | } | |
d1433deb | 2656 | ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid); |
7237f183 YZ |
2657 | atomic_set(&log_root_tree->log_commit[index2], 1); |
2658 | ||
12fcfd22 CM |
2659 | if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) { |
2660 | wait_log_commit(trans, log_root_tree, | |
d1433deb | 2661 | root_log_ctx.log_transid - 1); |
12fcfd22 CM |
2662 | } |
2663 | ||
2664 | wait_for_writer(trans, log_root_tree); | |
7237f183 | 2665 | |
12fcfd22 CM |
2666 | /* |
2667 | * now that we've moved on to the tree of log tree roots, | |
2668 | * check the full commit flag again | |
2669 | */ | |
995946dd | 2670 | if (btrfs_need_log_full_commit(root->fs_info, trans)) { |
c6adc9cc | 2671 | blk_finish_plug(&plug); |
8cef4e16 | 2672 | btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
2ab28f32 | 2673 | btrfs_free_logged_extents(log, log_transid); |
12fcfd22 CM |
2674 | mutex_unlock(&log_root_tree->log_mutex); |
2675 | ret = -EAGAIN; | |
2676 | goto out_wake_log_root; | |
2677 | } | |
7237f183 | 2678 | |
c6adc9cc MX |
2679 | ret = btrfs_write_marked_extents(log_root_tree, |
2680 | &log_root_tree->dirty_log_pages, | |
2681 | EXTENT_DIRTY | EXTENT_NEW); | |
2682 | blk_finish_plug(&plug); | |
79787eaa | 2683 | if (ret) { |
995946dd | 2684 | btrfs_set_log_full_commit(root->fs_info, trans); |
79787eaa | 2685 | btrfs_abort_transaction(trans, root, ret); |
2ab28f32 | 2686 | btrfs_free_logged_extents(log, log_transid); |
79787eaa JM |
2687 | mutex_unlock(&log_root_tree->log_mutex); |
2688 | goto out_wake_log_root; | |
2689 | } | |
5ab5e44a FM |
2690 | ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); |
2691 | if (!ret) | |
2692 | ret = btrfs_wait_marked_extents(log_root_tree, | |
2693 | &log_root_tree->dirty_log_pages, | |
2694 | EXTENT_NEW | EXTENT_DIRTY); | |
2695 | if (ret) { | |
2696 | btrfs_set_log_full_commit(root->fs_info, trans); | |
2697 | btrfs_free_logged_extents(log, log_transid); | |
2698 | mutex_unlock(&log_root_tree->log_mutex); | |
2699 | goto out_wake_log_root; | |
2700 | } | |
50d9aa99 | 2701 | btrfs_wait_logged_extents(trans, log, log_transid); |
e02119d5 | 2702 | |
6c41761f | 2703 | btrfs_set_super_log_root(root->fs_info->super_for_commit, |
7237f183 | 2704 | log_root_tree->node->start); |
6c41761f | 2705 | btrfs_set_super_log_root_level(root->fs_info->super_for_commit, |
7237f183 | 2706 | btrfs_header_level(log_root_tree->node)); |
e02119d5 | 2707 | |
7237f183 | 2708 | log_root_tree->log_transid++; |
7237f183 YZ |
2709 | mutex_unlock(&log_root_tree->log_mutex); |
2710 | ||
2711 | /* | |
2712 | * nobody else is going to jump in and write the the ctree | |
2713 | * super here because the log_commit atomic below is protecting | |
2714 | * us. We must be called with a transaction handle pinning | |
2715 | * the running transaction open, so a full commit can't hop | |
2716 | * in and cause problems either. | |
2717 | */ | |
5af3e8cc | 2718 | ret = write_ctree_super(trans, root->fs_info->tree_root, 1); |
5af3e8cc | 2719 | if (ret) { |
995946dd | 2720 | btrfs_set_log_full_commit(root->fs_info, trans); |
5af3e8cc SB |
2721 | btrfs_abort_transaction(trans, root, ret); |
2722 | goto out_wake_log_root; | |
2723 | } | |
7237f183 | 2724 | |
257c62e1 CM |
2725 | mutex_lock(&root->log_mutex); |
2726 | if (root->last_log_commit < log_transid) | |
2727 | root->last_log_commit = log_transid; | |
2728 | mutex_unlock(&root->log_mutex); | |
2729 | ||
12fcfd22 | 2730 | out_wake_log_root: |
8b050d35 MX |
2731 | /* |
2732 | * We needn't get log_mutex here because we are sure all | |
2733 | * the other tasks are blocked. | |
2734 | */ | |
2735 | btrfs_remove_all_log_ctxs(log_root_tree, index2, ret); | |
2736 | ||
d1433deb MX |
2737 | mutex_lock(&log_root_tree->log_mutex); |
2738 | log_root_tree->log_transid_committed++; | |
7237f183 | 2739 | atomic_set(&log_root_tree->log_commit[index2], 0); |
d1433deb MX |
2740 | mutex_unlock(&log_root_tree->log_mutex); |
2741 | ||
7237f183 YZ |
2742 | if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) |
2743 | wake_up(&log_root_tree->log_commit_wait[index2]); | |
e02119d5 | 2744 | out: |
8b050d35 MX |
2745 | /* See above. */ |
2746 | btrfs_remove_all_log_ctxs(root, index1, ret); | |
2747 | ||
d1433deb MX |
2748 | mutex_lock(&root->log_mutex); |
2749 | root->log_transid_committed++; | |
7237f183 | 2750 | atomic_set(&root->log_commit[index1], 0); |
d1433deb | 2751 | mutex_unlock(&root->log_mutex); |
8b050d35 | 2752 | |
7237f183 YZ |
2753 | if (waitqueue_active(&root->log_commit_wait[index1])) |
2754 | wake_up(&root->log_commit_wait[index1]); | |
b31eabd8 | 2755 | return ret; |
e02119d5 CM |
2756 | } |
2757 | ||
4a500fd1 YZ |
2758 | static void free_log_tree(struct btrfs_trans_handle *trans, |
2759 | struct btrfs_root *log) | |
e02119d5 CM |
2760 | { |
2761 | int ret; | |
d0c803c4 CM |
2762 | u64 start; |
2763 | u64 end; | |
e02119d5 CM |
2764 | struct walk_control wc = { |
2765 | .free = 1, | |
2766 | .process_func = process_one_buffer | |
2767 | }; | |
2768 | ||
681ae509 JB |
2769 | ret = walk_log_tree(trans, log, &wc); |
2770 | /* I don't think this can happen but just in case */ | |
2771 | if (ret) | |
2772 | btrfs_abort_transaction(trans, log, ret); | |
e02119d5 | 2773 | |
d397712b | 2774 | while (1) { |
d0c803c4 | 2775 | ret = find_first_extent_bit(&log->dirty_log_pages, |
e6138876 JB |
2776 | 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW, |
2777 | NULL); | |
d0c803c4 CM |
2778 | if (ret) |
2779 | break; | |
2780 | ||
8cef4e16 YZ |
2781 | clear_extent_bits(&log->dirty_log_pages, start, end, |
2782 | EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS); | |
d0c803c4 CM |
2783 | } |
2784 | ||
2ab28f32 JB |
2785 | /* |
2786 | * We may have short-circuited the log tree with the full commit logic | |
2787 | * and left ordered extents on our list, so clear these out to keep us | |
2788 | * from leaking inodes and memory. | |
2789 | */ | |
2790 | btrfs_free_logged_extents(log, 0); | |
2791 | btrfs_free_logged_extents(log, 1); | |
2792 | ||
7237f183 YZ |
2793 | free_extent_buffer(log->node); |
2794 | kfree(log); | |
4a500fd1 YZ |
2795 | } |
2796 | ||
2797 | /* | |
2798 | * free all the extents used by the tree log. This should be called | |
2799 | * at commit time of the full transaction | |
2800 | */ | |
2801 | int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root) | |
2802 | { | |
2803 | if (root->log_root) { | |
2804 | free_log_tree(trans, root->log_root); | |
2805 | root->log_root = NULL; | |
2806 | } | |
2807 | return 0; | |
2808 | } | |
2809 | ||
2810 | int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, | |
2811 | struct btrfs_fs_info *fs_info) | |
2812 | { | |
2813 | if (fs_info->log_root_tree) { | |
2814 | free_log_tree(trans, fs_info->log_root_tree); | |
2815 | fs_info->log_root_tree = NULL; | |
2816 | } | |
e02119d5 CM |
2817 | return 0; |
2818 | } | |
2819 | ||
e02119d5 CM |
2820 | /* |
2821 | * If both a file and directory are logged, and unlinks or renames are | |
2822 | * mixed in, we have a few interesting corners: | |
2823 | * | |
2824 | * create file X in dir Y | |
2825 | * link file X to X.link in dir Y | |
2826 | * fsync file X | |
2827 | * unlink file X but leave X.link | |
2828 | * fsync dir Y | |
2829 | * | |
2830 | * After a crash we would expect only X.link to exist. But file X | |
2831 | * didn't get fsync'd again so the log has back refs for X and X.link. | |
2832 | * | |
2833 | * We solve this by removing directory entries and inode backrefs from the | |
2834 | * log when a file that was logged in the current transaction is | |
2835 | * unlinked. Any later fsync will include the updated log entries, and | |
2836 | * we'll be able to reconstruct the proper directory items from backrefs. | |
2837 | * | |
2838 | * This optimizations allows us to avoid relogging the entire inode | |
2839 | * or the entire directory. | |
2840 | */ | |
2841 | int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | |
2842 | struct btrfs_root *root, | |
2843 | const char *name, int name_len, | |
2844 | struct inode *dir, u64 index) | |
2845 | { | |
2846 | struct btrfs_root *log; | |
2847 | struct btrfs_dir_item *di; | |
2848 | struct btrfs_path *path; | |
2849 | int ret; | |
4a500fd1 | 2850 | int err = 0; |
e02119d5 | 2851 | int bytes_del = 0; |
33345d01 | 2852 | u64 dir_ino = btrfs_ino(dir); |
e02119d5 | 2853 | |
3a5f1d45 CM |
2854 | if (BTRFS_I(dir)->logged_trans < trans->transid) |
2855 | return 0; | |
2856 | ||
e02119d5 CM |
2857 | ret = join_running_log_trans(root); |
2858 | if (ret) | |
2859 | return 0; | |
2860 | ||
2861 | mutex_lock(&BTRFS_I(dir)->log_mutex); | |
2862 | ||
2863 | log = root->log_root; | |
2864 | path = btrfs_alloc_path(); | |
a62f44a5 TI |
2865 | if (!path) { |
2866 | err = -ENOMEM; | |
2867 | goto out_unlock; | |
2868 | } | |
2a29edc6 | 2869 | |
33345d01 | 2870 | di = btrfs_lookup_dir_item(trans, log, path, dir_ino, |
e02119d5 | 2871 | name, name_len, -1); |
4a500fd1 YZ |
2872 | if (IS_ERR(di)) { |
2873 | err = PTR_ERR(di); | |
2874 | goto fail; | |
2875 | } | |
2876 | if (di) { | |
e02119d5 CM |
2877 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2878 | bytes_del += name_len; | |
3650860b JB |
2879 | if (ret) { |
2880 | err = ret; | |
2881 | goto fail; | |
2882 | } | |
e02119d5 | 2883 | } |
b3b4aa74 | 2884 | btrfs_release_path(path); |
33345d01 | 2885 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
e02119d5 | 2886 | index, name, name_len, -1); |
4a500fd1 YZ |
2887 | if (IS_ERR(di)) { |
2888 | err = PTR_ERR(di); | |
2889 | goto fail; | |
2890 | } | |
2891 | if (di) { | |
e02119d5 CM |
2892 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2893 | bytes_del += name_len; | |
3650860b JB |
2894 | if (ret) { |
2895 | err = ret; | |
2896 | goto fail; | |
2897 | } | |
e02119d5 CM |
2898 | } |
2899 | ||
2900 | /* update the directory size in the log to reflect the names | |
2901 | * we have removed | |
2902 | */ | |
2903 | if (bytes_del) { | |
2904 | struct btrfs_key key; | |
2905 | ||
33345d01 | 2906 | key.objectid = dir_ino; |
e02119d5 CM |
2907 | key.offset = 0; |
2908 | key.type = BTRFS_INODE_ITEM_KEY; | |
b3b4aa74 | 2909 | btrfs_release_path(path); |
e02119d5 CM |
2910 | |
2911 | ret = btrfs_search_slot(trans, log, &key, path, 0, 1); | |
4a500fd1 YZ |
2912 | if (ret < 0) { |
2913 | err = ret; | |
2914 | goto fail; | |
2915 | } | |
e02119d5 CM |
2916 | if (ret == 0) { |
2917 | struct btrfs_inode_item *item; | |
2918 | u64 i_size; | |
2919 | ||
2920 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2921 | struct btrfs_inode_item); | |
2922 | i_size = btrfs_inode_size(path->nodes[0], item); | |
2923 | if (i_size > bytes_del) | |
2924 | i_size -= bytes_del; | |
2925 | else | |
2926 | i_size = 0; | |
2927 | btrfs_set_inode_size(path->nodes[0], item, i_size); | |
2928 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2929 | } else | |
2930 | ret = 0; | |
b3b4aa74 | 2931 | btrfs_release_path(path); |
e02119d5 | 2932 | } |
4a500fd1 | 2933 | fail: |
e02119d5 | 2934 | btrfs_free_path(path); |
a62f44a5 | 2935 | out_unlock: |
e02119d5 | 2936 | mutex_unlock(&BTRFS_I(dir)->log_mutex); |
4a500fd1 | 2937 | if (ret == -ENOSPC) { |
995946dd | 2938 | btrfs_set_log_full_commit(root->fs_info, trans); |
4a500fd1 | 2939 | ret = 0; |
79787eaa JM |
2940 | } else if (ret < 0) |
2941 | btrfs_abort_transaction(trans, root, ret); | |
2942 | ||
12fcfd22 | 2943 | btrfs_end_log_trans(root); |
e02119d5 | 2944 | |
411fc6bc | 2945 | return err; |
e02119d5 CM |
2946 | } |
2947 | ||
2948 | /* see comments for btrfs_del_dir_entries_in_log */ | |
2949 | int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans, | |
2950 | struct btrfs_root *root, | |
2951 | const char *name, int name_len, | |
2952 | struct inode *inode, u64 dirid) | |
2953 | { | |
2954 | struct btrfs_root *log; | |
2955 | u64 index; | |
2956 | int ret; | |
2957 | ||
3a5f1d45 CM |
2958 | if (BTRFS_I(inode)->logged_trans < trans->transid) |
2959 | return 0; | |
2960 | ||
e02119d5 CM |
2961 | ret = join_running_log_trans(root); |
2962 | if (ret) | |
2963 | return 0; | |
2964 | log = root->log_root; | |
2965 | mutex_lock(&BTRFS_I(inode)->log_mutex); | |
2966 | ||
33345d01 | 2967 | ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode), |
e02119d5 CM |
2968 | dirid, &index); |
2969 | mutex_unlock(&BTRFS_I(inode)->log_mutex); | |
4a500fd1 | 2970 | if (ret == -ENOSPC) { |
995946dd | 2971 | btrfs_set_log_full_commit(root->fs_info, trans); |
4a500fd1 | 2972 | ret = 0; |
79787eaa JM |
2973 | } else if (ret < 0 && ret != -ENOENT) |
2974 | btrfs_abort_transaction(trans, root, ret); | |
12fcfd22 | 2975 | btrfs_end_log_trans(root); |
e02119d5 | 2976 | |
e02119d5 CM |
2977 | return ret; |
2978 | } | |
2979 | ||
2980 | /* | |
2981 | * creates a range item in the log for 'dirid'. first_offset and | |
2982 | * last_offset tell us which parts of the key space the log should | |
2983 | * be considered authoritative for. | |
2984 | */ | |
2985 | static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, | |
2986 | struct btrfs_root *log, | |
2987 | struct btrfs_path *path, | |
2988 | int key_type, u64 dirid, | |
2989 | u64 first_offset, u64 last_offset) | |
2990 | { | |
2991 | int ret; | |
2992 | struct btrfs_key key; | |
2993 | struct btrfs_dir_log_item *item; | |
2994 | ||
2995 | key.objectid = dirid; | |
2996 | key.offset = first_offset; | |
2997 | if (key_type == BTRFS_DIR_ITEM_KEY) | |
2998 | key.type = BTRFS_DIR_LOG_ITEM_KEY; | |
2999 | else | |
3000 | key.type = BTRFS_DIR_LOG_INDEX_KEY; | |
3001 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item)); | |
4a500fd1 YZ |
3002 | if (ret) |
3003 | return ret; | |
e02119d5 CM |
3004 | |
3005 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3006 | struct btrfs_dir_log_item); | |
3007 | btrfs_set_dir_log_end(path->nodes[0], item, last_offset); | |
3008 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
b3b4aa74 | 3009 | btrfs_release_path(path); |
e02119d5 CM |
3010 | return 0; |
3011 | } | |
3012 | ||
3013 | /* | |
3014 | * log all the items included in the current transaction for a given | |
3015 | * directory. This also creates the range items in the log tree required | |
3016 | * to replay anything deleted before the fsync | |
3017 | */ | |
3018 | static noinline int log_dir_items(struct btrfs_trans_handle *trans, | |
3019 | struct btrfs_root *root, struct inode *inode, | |
3020 | struct btrfs_path *path, | |
3021 | struct btrfs_path *dst_path, int key_type, | |
3022 | u64 min_offset, u64 *last_offset_ret) | |
3023 | { | |
3024 | struct btrfs_key min_key; | |
e02119d5 CM |
3025 | struct btrfs_root *log = root->log_root; |
3026 | struct extent_buffer *src; | |
4a500fd1 | 3027 | int err = 0; |
e02119d5 CM |
3028 | int ret; |
3029 | int i; | |
3030 | int nritems; | |
3031 | u64 first_offset = min_offset; | |
3032 | u64 last_offset = (u64)-1; | |
33345d01 | 3033 | u64 ino = btrfs_ino(inode); |
e02119d5 CM |
3034 | |
3035 | log = root->log_root; | |
e02119d5 | 3036 | |
33345d01 | 3037 | min_key.objectid = ino; |
e02119d5 CM |
3038 | min_key.type = key_type; |
3039 | min_key.offset = min_offset; | |
3040 | ||
6174d3cb | 3041 | ret = btrfs_search_forward(root, &min_key, path, trans->transid); |
e02119d5 CM |
3042 | |
3043 | /* | |
3044 | * we didn't find anything from this transaction, see if there | |
3045 | * is anything at all | |
3046 | */ | |
33345d01 LZ |
3047 | if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) { |
3048 | min_key.objectid = ino; | |
e02119d5 CM |
3049 | min_key.type = key_type; |
3050 | min_key.offset = (u64)-1; | |
b3b4aa74 | 3051 | btrfs_release_path(path); |
e02119d5 CM |
3052 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); |
3053 | if (ret < 0) { | |
b3b4aa74 | 3054 | btrfs_release_path(path); |
e02119d5 CM |
3055 | return ret; |
3056 | } | |
33345d01 | 3057 | ret = btrfs_previous_item(root, path, ino, key_type); |
e02119d5 CM |
3058 | |
3059 | /* if ret == 0 there are items for this type, | |
3060 | * create a range to tell us the last key of this type. | |
3061 | * otherwise, there are no items in this directory after | |
3062 | * *min_offset, and we create a range to indicate that. | |
3063 | */ | |
3064 | if (ret == 0) { | |
3065 | struct btrfs_key tmp; | |
3066 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, | |
3067 | path->slots[0]); | |
d397712b | 3068 | if (key_type == tmp.type) |
e02119d5 | 3069 | first_offset = max(min_offset, tmp.offset) + 1; |
e02119d5 CM |
3070 | } |
3071 | goto done; | |
3072 | } | |
3073 | ||
3074 | /* go backward to find any previous key */ | |
33345d01 | 3075 | ret = btrfs_previous_item(root, path, ino, key_type); |
e02119d5 CM |
3076 | if (ret == 0) { |
3077 | struct btrfs_key tmp; | |
3078 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
3079 | if (key_type == tmp.type) { | |
3080 | first_offset = tmp.offset; | |
3081 | ret = overwrite_item(trans, log, dst_path, | |
3082 | path->nodes[0], path->slots[0], | |
3083 | &tmp); | |
4a500fd1 YZ |
3084 | if (ret) { |
3085 | err = ret; | |
3086 | goto done; | |
3087 | } | |
e02119d5 CM |
3088 | } |
3089 | } | |
b3b4aa74 | 3090 | btrfs_release_path(path); |
e02119d5 CM |
3091 | |
3092 | /* find the first key from this transaction again */ | |
3093 | ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0); | |
fae7f21c | 3094 | if (WARN_ON(ret != 0)) |
e02119d5 | 3095 | goto done; |
e02119d5 CM |
3096 | |
3097 | /* | |
3098 | * we have a block from this transaction, log every item in it | |
3099 | * from our directory | |
3100 | */ | |
d397712b | 3101 | while (1) { |
e02119d5 CM |
3102 | struct btrfs_key tmp; |
3103 | src = path->nodes[0]; | |
3104 | nritems = btrfs_header_nritems(src); | |
3105 | for (i = path->slots[0]; i < nritems; i++) { | |
3106 | btrfs_item_key_to_cpu(src, &min_key, i); | |
3107 | ||
33345d01 | 3108 | if (min_key.objectid != ino || min_key.type != key_type) |
e02119d5 CM |
3109 | goto done; |
3110 | ret = overwrite_item(trans, log, dst_path, src, i, | |
3111 | &min_key); | |
4a500fd1 YZ |
3112 | if (ret) { |
3113 | err = ret; | |
3114 | goto done; | |
3115 | } | |
e02119d5 CM |
3116 | } |
3117 | path->slots[0] = nritems; | |
3118 | ||
3119 | /* | |
3120 | * look ahead to the next item and see if it is also | |
3121 | * from this directory and from this transaction | |
3122 | */ | |
3123 | ret = btrfs_next_leaf(root, path); | |
3124 | if (ret == 1) { | |
3125 | last_offset = (u64)-1; | |
3126 | goto done; | |
3127 | } | |
3128 | btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]); | |
33345d01 | 3129 | if (tmp.objectid != ino || tmp.type != key_type) { |
e02119d5 CM |
3130 | last_offset = (u64)-1; |
3131 | goto done; | |
3132 | } | |
3133 | if (btrfs_header_generation(path->nodes[0]) != trans->transid) { | |
3134 | ret = overwrite_item(trans, log, dst_path, | |
3135 | path->nodes[0], path->slots[0], | |
3136 | &tmp); | |
4a500fd1 YZ |
3137 | if (ret) |
3138 | err = ret; | |
3139 | else | |
3140 | last_offset = tmp.offset; | |
e02119d5 CM |
3141 | goto done; |
3142 | } | |
3143 | } | |
3144 | done: | |
b3b4aa74 DS |
3145 | btrfs_release_path(path); |
3146 | btrfs_release_path(dst_path); | |
e02119d5 | 3147 | |
4a500fd1 YZ |
3148 | if (err == 0) { |
3149 | *last_offset_ret = last_offset; | |
3150 | /* | |
3151 | * insert the log range keys to indicate where the log | |
3152 | * is valid | |
3153 | */ | |
3154 | ret = insert_dir_log_key(trans, log, path, key_type, | |
33345d01 | 3155 | ino, first_offset, last_offset); |
4a500fd1 YZ |
3156 | if (ret) |
3157 | err = ret; | |
3158 | } | |
3159 | return err; | |
e02119d5 CM |
3160 | } |
3161 | ||
3162 | /* | |
3163 | * logging directories is very similar to logging inodes, We find all the items | |
3164 | * from the current transaction and write them to the log. | |
3165 | * | |
3166 | * The recovery code scans the directory in the subvolume, and if it finds a | |
3167 | * key in the range logged that is not present in the log tree, then it means | |
3168 | * that dir entry was unlinked during the transaction. | |
3169 | * | |
3170 | * In order for that scan to work, we must include one key smaller than | |
3171 | * the smallest logged by this transaction and one key larger than the largest | |
3172 | * key logged by this transaction. | |
3173 | */ | |
3174 | static noinline int log_directory_changes(struct btrfs_trans_handle *trans, | |
3175 | struct btrfs_root *root, struct inode *inode, | |
3176 | struct btrfs_path *path, | |
3177 | struct btrfs_path *dst_path) | |
3178 | { | |
3179 | u64 min_key; | |
3180 | u64 max_key; | |
3181 | int ret; | |
3182 | int key_type = BTRFS_DIR_ITEM_KEY; | |
3183 | ||
3184 | again: | |
3185 | min_key = 0; | |
3186 | max_key = 0; | |
d397712b | 3187 | while (1) { |
e02119d5 CM |
3188 | ret = log_dir_items(trans, root, inode, path, |
3189 | dst_path, key_type, min_key, | |
3190 | &max_key); | |
4a500fd1 YZ |
3191 | if (ret) |
3192 | return ret; | |
e02119d5 CM |
3193 | if (max_key == (u64)-1) |
3194 | break; | |
3195 | min_key = max_key + 1; | |
3196 | } | |
3197 | ||
3198 | if (key_type == BTRFS_DIR_ITEM_KEY) { | |
3199 | key_type = BTRFS_DIR_INDEX_KEY; | |
3200 | goto again; | |
3201 | } | |
3202 | return 0; | |
3203 | } | |
3204 | ||
3205 | /* | |
3206 | * a helper function to drop items from the log before we relog an | |
3207 | * inode. max_key_type indicates the highest item type to remove. | |
3208 | * This cannot be run for file data extents because it does not | |
3209 | * free the extents they point to. | |
3210 | */ | |
3211 | static int drop_objectid_items(struct btrfs_trans_handle *trans, | |
3212 | struct btrfs_root *log, | |
3213 | struct btrfs_path *path, | |
3214 | u64 objectid, int max_key_type) | |
3215 | { | |
3216 | int ret; | |
3217 | struct btrfs_key key; | |
3218 | struct btrfs_key found_key; | |
18ec90d6 | 3219 | int start_slot; |
e02119d5 CM |
3220 | |
3221 | key.objectid = objectid; | |
3222 | key.type = max_key_type; | |
3223 | key.offset = (u64)-1; | |
3224 | ||
d397712b | 3225 | while (1) { |
e02119d5 | 3226 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
3650860b | 3227 | BUG_ON(ret == 0); /* Logic error */ |
4a500fd1 | 3228 | if (ret < 0) |
e02119d5 CM |
3229 | break; |
3230 | ||
3231 | if (path->slots[0] == 0) | |
3232 | break; | |
3233 | ||
3234 | path->slots[0]--; | |
3235 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
3236 | path->slots[0]); | |
3237 | ||
3238 | if (found_key.objectid != objectid) | |
3239 | break; | |
3240 | ||
18ec90d6 JB |
3241 | found_key.offset = 0; |
3242 | found_key.type = 0; | |
3243 | ret = btrfs_bin_search(path->nodes[0], &found_key, 0, | |
3244 | &start_slot); | |
3245 | ||
3246 | ret = btrfs_del_items(trans, log, path, start_slot, | |
3247 | path->slots[0] - start_slot + 1); | |
3248 | /* | |
3249 | * If start slot isn't 0 then we don't need to re-search, we've | |
3250 | * found the last guy with the objectid in this tree. | |
3251 | */ | |
3252 | if (ret || start_slot != 0) | |
65a246c5 | 3253 | break; |
b3b4aa74 | 3254 | btrfs_release_path(path); |
e02119d5 | 3255 | } |
b3b4aa74 | 3256 | btrfs_release_path(path); |
5bdbeb21 JB |
3257 | if (ret > 0) |
3258 | ret = 0; | |
4a500fd1 | 3259 | return ret; |
e02119d5 CM |
3260 | } |
3261 | ||
94edf4ae JB |
3262 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3263 | struct extent_buffer *leaf, | |
3264 | struct btrfs_inode_item *item, | |
1a4bcf47 FM |
3265 | struct inode *inode, int log_inode_only, |
3266 | u64 logged_isize) | |
94edf4ae | 3267 | { |
0b1c6cca JB |
3268 | struct btrfs_map_token token; |
3269 | ||
3270 | btrfs_init_map_token(&token); | |
94edf4ae JB |
3271 | |
3272 | if (log_inode_only) { | |
3273 | /* set the generation to zero so the recover code | |
3274 | * can tell the difference between an logging | |
3275 | * just to say 'this inode exists' and a logging | |
3276 | * to say 'update this inode with these values' | |
3277 | */ | |
0b1c6cca | 3278 | btrfs_set_token_inode_generation(leaf, item, 0, &token); |
1a4bcf47 | 3279 | btrfs_set_token_inode_size(leaf, item, logged_isize, &token); |
94edf4ae | 3280 | } else { |
0b1c6cca JB |
3281 | btrfs_set_token_inode_generation(leaf, item, |
3282 | BTRFS_I(inode)->generation, | |
3283 | &token); | |
3284 | btrfs_set_token_inode_size(leaf, item, inode->i_size, &token); | |
3285 | } | |
3286 | ||
3287 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); | |
3288 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3289 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3290 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
3291 | ||
a937b979 | 3292 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
0b1c6cca | 3293 | inode->i_atime.tv_sec, &token); |
a937b979 | 3294 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
0b1c6cca JB |
3295 | inode->i_atime.tv_nsec, &token); |
3296 | ||
a937b979 | 3297 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
0b1c6cca | 3298 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3299 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
0b1c6cca JB |
3300 | inode->i_mtime.tv_nsec, &token); |
3301 | ||
a937b979 | 3302 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
0b1c6cca | 3303 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3304 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
0b1c6cca JB |
3305 | inode->i_ctime.tv_nsec, &token); |
3306 | ||
3307 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), | |
3308 | &token); | |
3309 | ||
3310 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3311 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3312 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3313 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3314 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
94edf4ae JB |
3315 | } |
3316 | ||
a95249b3 JB |
3317 | static int log_inode_item(struct btrfs_trans_handle *trans, |
3318 | struct btrfs_root *log, struct btrfs_path *path, | |
3319 | struct inode *inode) | |
3320 | { | |
3321 | struct btrfs_inode_item *inode_item; | |
a95249b3 JB |
3322 | int ret; |
3323 | ||
efd0c405 FDBM |
3324 | ret = btrfs_insert_empty_item(trans, log, path, |
3325 | &BTRFS_I(inode)->location, | |
a95249b3 JB |
3326 | sizeof(*inode_item)); |
3327 | if (ret && ret != -EEXIST) | |
3328 | return ret; | |
3329 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3330 | struct btrfs_inode_item); | |
1a4bcf47 | 3331 | fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0); |
a95249b3 JB |
3332 | btrfs_release_path(path); |
3333 | return 0; | |
3334 | } | |
3335 | ||
31ff1cd2 | 3336 | static noinline int copy_items(struct btrfs_trans_handle *trans, |
d2794405 | 3337 | struct inode *inode, |
31ff1cd2 | 3338 | struct btrfs_path *dst_path, |
16e7549f | 3339 | struct btrfs_path *src_path, u64 *last_extent, |
1a4bcf47 FM |
3340 | int start_slot, int nr, int inode_only, |
3341 | u64 logged_isize) | |
31ff1cd2 CM |
3342 | { |
3343 | unsigned long src_offset; | |
3344 | unsigned long dst_offset; | |
d2794405 | 3345 | struct btrfs_root *log = BTRFS_I(inode)->root->log_root; |
31ff1cd2 CM |
3346 | struct btrfs_file_extent_item *extent; |
3347 | struct btrfs_inode_item *inode_item; | |
16e7549f JB |
3348 | struct extent_buffer *src = src_path->nodes[0]; |
3349 | struct btrfs_key first_key, last_key, key; | |
31ff1cd2 CM |
3350 | int ret; |
3351 | struct btrfs_key *ins_keys; | |
3352 | u32 *ins_sizes; | |
3353 | char *ins_data; | |
3354 | int i; | |
d20f7043 | 3355 | struct list_head ordered_sums; |
d2794405 | 3356 | int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
16e7549f | 3357 | bool has_extents = false; |
74121f7c | 3358 | bool need_find_last_extent = true; |
16e7549f | 3359 | bool done = false; |
d20f7043 CM |
3360 | |
3361 | INIT_LIST_HEAD(&ordered_sums); | |
31ff1cd2 CM |
3362 | |
3363 | ins_data = kmalloc(nr * sizeof(struct btrfs_key) + | |
3364 | nr * sizeof(u32), GFP_NOFS); | |
2a29edc6 | 3365 | if (!ins_data) |
3366 | return -ENOMEM; | |
3367 | ||
16e7549f JB |
3368 | first_key.objectid = (u64)-1; |
3369 | ||
31ff1cd2 CM |
3370 | ins_sizes = (u32 *)ins_data; |
3371 | ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32)); | |
3372 | ||
3373 | for (i = 0; i < nr; i++) { | |
3374 | ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot); | |
3375 | btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot); | |
3376 | } | |
3377 | ret = btrfs_insert_empty_items(trans, log, dst_path, | |
3378 | ins_keys, ins_sizes, nr); | |
4a500fd1 YZ |
3379 | if (ret) { |
3380 | kfree(ins_data); | |
3381 | return ret; | |
3382 | } | |
31ff1cd2 | 3383 | |
5d4f98a2 | 3384 | for (i = 0; i < nr; i++, dst_path->slots[0]++) { |
31ff1cd2 CM |
3385 | dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], |
3386 | dst_path->slots[0]); | |
3387 | ||
3388 | src_offset = btrfs_item_ptr_offset(src, start_slot + i); | |
3389 | ||
16e7549f JB |
3390 | if ((i == (nr - 1))) |
3391 | last_key = ins_keys[i]; | |
3392 | ||
94edf4ae | 3393 | if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) { |
31ff1cd2 CM |
3394 | inode_item = btrfs_item_ptr(dst_path->nodes[0], |
3395 | dst_path->slots[0], | |
3396 | struct btrfs_inode_item); | |
94edf4ae | 3397 | fill_inode_item(trans, dst_path->nodes[0], inode_item, |
1a4bcf47 FM |
3398 | inode, inode_only == LOG_INODE_EXISTS, |
3399 | logged_isize); | |
94edf4ae JB |
3400 | } else { |
3401 | copy_extent_buffer(dst_path->nodes[0], src, dst_offset, | |
3402 | src_offset, ins_sizes[i]); | |
31ff1cd2 | 3403 | } |
94edf4ae | 3404 | |
16e7549f JB |
3405 | /* |
3406 | * We set need_find_last_extent here in case we know we were | |
3407 | * processing other items and then walk into the first extent in | |
3408 | * the inode. If we don't hit an extent then nothing changes, | |
3409 | * we'll do the last search the next time around. | |
3410 | */ | |
3411 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) { | |
3412 | has_extents = true; | |
74121f7c | 3413 | if (first_key.objectid == (u64)-1) |
16e7549f JB |
3414 | first_key = ins_keys[i]; |
3415 | } else { | |
3416 | need_find_last_extent = false; | |
3417 | } | |
3418 | ||
31ff1cd2 CM |
3419 | /* take a reference on file data extents so that truncates |
3420 | * or deletes of this inode don't have to relog the inode | |
3421 | * again | |
3422 | */ | |
962a298f | 3423 | if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY && |
d2794405 | 3424 | !skip_csum) { |
31ff1cd2 CM |
3425 | int found_type; |
3426 | extent = btrfs_item_ptr(src, start_slot + i, | |
3427 | struct btrfs_file_extent_item); | |
3428 | ||
8e531cdf | 3429 | if (btrfs_file_extent_generation(src, extent) < trans->transid) |
3430 | continue; | |
3431 | ||
31ff1cd2 | 3432 | found_type = btrfs_file_extent_type(src, extent); |
6f1fed77 | 3433 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
5d4f98a2 YZ |
3434 | u64 ds, dl, cs, cl; |
3435 | ds = btrfs_file_extent_disk_bytenr(src, | |
3436 | extent); | |
3437 | /* ds == 0 is a hole */ | |
3438 | if (ds == 0) | |
3439 | continue; | |
3440 | ||
3441 | dl = btrfs_file_extent_disk_num_bytes(src, | |
3442 | extent); | |
3443 | cs = btrfs_file_extent_offset(src, extent); | |
3444 | cl = btrfs_file_extent_num_bytes(src, | |
a419aef8 | 3445 | extent); |
580afd76 CM |
3446 | if (btrfs_file_extent_compression(src, |
3447 | extent)) { | |
3448 | cs = 0; | |
3449 | cl = dl; | |
3450 | } | |
5d4f98a2 YZ |
3451 | |
3452 | ret = btrfs_lookup_csums_range( | |
3453 | log->fs_info->csum_root, | |
3454 | ds + cs, ds + cs + cl - 1, | |
a2de733c | 3455 | &ordered_sums, 0); |
3650860b JB |
3456 | if (ret) { |
3457 | btrfs_release_path(dst_path); | |
3458 | kfree(ins_data); | |
3459 | return ret; | |
3460 | } | |
31ff1cd2 CM |
3461 | } |
3462 | } | |
31ff1cd2 CM |
3463 | } |
3464 | ||
3465 | btrfs_mark_buffer_dirty(dst_path->nodes[0]); | |
b3b4aa74 | 3466 | btrfs_release_path(dst_path); |
31ff1cd2 | 3467 | kfree(ins_data); |
d20f7043 CM |
3468 | |
3469 | /* | |
3470 | * we have to do this after the loop above to avoid changing the | |
3471 | * log tree while trying to change the log tree. | |
3472 | */ | |
4a500fd1 | 3473 | ret = 0; |
d397712b | 3474 | while (!list_empty(&ordered_sums)) { |
d20f7043 CM |
3475 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, |
3476 | struct btrfs_ordered_sum, | |
3477 | list); | |
4a500fd1 YZ |
3478 | if (!ret) |
3479 | ret = btrfs_csum_file_blocks(trans, log, sums); | |
d20f7043 CM |
3480 | list_del(&sums->list); |
3481 | kfree(sums); | |
3482 | } | |
16e7549f JB |
3483 | |
3484 | if (!has_extents) | |
3485 | return ret; | |
3486 | ||
74121f7c FM |
3487 | if (need_find_last_extent && *last_extent == first_key.offset) { |
3488 | /* | |
3489 | * We don't have any leafs between our current one and the one | |
3490 | * we processed before that can have file extent items for our | |
3491 | * inode (and have a generation number smaller than our current | |
3492 | * transaction id). | |
3493 | */ | |
3494 | need_find_last_extent = false; | |
3495 | } | |
3496 | ||
16e7549f JB |
3497 | /* |
3498 | * Because we use btrfs_search_forward we could skip leaves that were | |
3499 | * not modified and then assume *last_extent is valid when it really | |
3500 | * isn't. So back up to the previous leaf and read the end of the last | |
3501 | * extent before we go and fill in holes. | |
3502 | */ | |
3503 | if (need_find_last_extent) { | |
3504 | u64 len; | |
3505 | ||
3506 | ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path); | |
3507 | if (ret < 0) | |
3508 | return ret; | |
3509 | if (ret) | |
3510 | goto fill_holes; | |
3511 | if (src_path->slots[0]) | |
3512 | src_path->slots[0]--; | |
3513 | src = src_path->nodes[0]; | |
3514 | btrfs_item_key_to_cpu(src, &key, src_path->slots[0]); | |
3515 | if (key.objectid != btrfs_ino(inode) || | |
3516 | key.type != BTRFS_EXTENT_DATA_KEY) | |
3517 | goto fill_holes; | |
3518 | extent = btrfs_item_ptr(src, src_path->slots[0], | |
3519 | struct btrfs_file_extent_item); | |
3520 | if (btrfs_file_extent_type(src, extent) == | |
3521 | BTRFS_FILE_EXTENT_INLINE) { | |
514ac8ad CM |
3522 | len = btrfs_file_extent_inline_len(src, |
3523 | src_path->slots[0], | |
3524 | extent); | |
16e7549f JB |
3525 | *last_extent = ALIGN(key.offset + len, |
3526 | log->sectorsize); | |
3527 | } else { | |
3528 | len = btrfs_file_extent_num_bytes(src, extent); | |
3529 | *last_extent = key.offset + len; | |
3530 | } | |
3531 | } | |
3532 | fill_holes: | |
3533 | /* So we did prev_leaf, now we need to move to the next leaf, but a few | |
3534 | * things could have happened | |
3535 | * | |
3536 | * 1) A merge could have happened, so we could currently be on a leaf | |
3537 | * that holds what we were copying in the first place. | |
3538 | * 2) A split could have happened, and now not all of the items we want | |
3539 | * are on the same leaf. | |
3540 | * | |
3541 | * So we need to adjust how we search for holes, we need to drop the | |
3542 | * path and re-search for the first extent key we found, and then walk | |
3543 | * forward until we hit the last one we copied. | |
3544 | */ | |
3545 | if (need_find_last_extent) { | |
3546 | /* btrfs_prev_leaf could return 1 without releasing the path */ | |
3547 | btrfs_release_path(src_path); | |
3548 | ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key, | |
3549 | src_path, 0, 0); | |
3550 | if (ret < 0) | |
3551 | return ret; | |
3552 | ASSERT(ret == 0); | |
3553 | src = src_path->nodes[0]; | |
3554 | i = src_path->slots[0]; | |
3555 | } else { | |
3556 | i = start_slot; | |
3557 | } | |
3558 | ||
3559 | /* | |
3560 | * Ok so here we need to go through and fill in any holes we may have | |
3561 | * to make sure that holes are punched for those areas in case they had | |
3562 | * extents previously. | |
3563 | */ | |
3564 | while (!done) { | |
3565 | u64 offset, len; | |
3566 | u64 extent_end; | |
3567 | ||
3568 | if (i >= btrfs_header_nritems(src_path->nodes[0])) { | |
3569 | ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path); | |
3570 | if (ret < 0) | |
3571 | return ret; | |
3572 | ASSERT(ret == 0); | |
3573 | src = src_path->nodes[0]; | |
3574 | i = 0; | |
3575 | } | |
3576 | ||
3577 | btrfs_item_key_to_cpu(src, &key, i); | |
3578 | if (!btrfs_comp_cpu_keys(&key, &last_key)) | |
3579 | done = true; | |
3580 | if (key.objectid != btrfs_ino(inode) || | |
3581 | key.type != BTRFS_EXTENT_DATA_KEY) { | |
3582 | i++; | |
3583 | continue; | |
3584 | } | |
3585 | extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item); | |
3586 | if (btrfs_file_extent_type(src, extent) == | |
3587 | BTRFS_FILE_EXTENT_INLINE) { | |
514ac8ad | 3588 | len = btrfs_file_extent_inline_len(src, i, extent); |
16e7549f JB |
3589 | extent_end = ALIGN(key.offset + len, log->sectorsize); |
3590 | } else { | |
3591 | len = btrfs_file_extent_num_bytes(src, extent); | |
3592 | extent_end = key.offset + len; | |
3593 | } | |
3594 | i++; | |
3595 | ||
3596 | if (*last_extent == key.offset) { | |
3597 | *last_extent = extent_end; | |
3598 | continue; | |
3599 | } | |
3600 | offset = *last_extent; | |
3601 | len = key.offset - *last_extent; | |
3602 | ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode), | |
3603 | offset, 0, 0, len, 0, len, 0, | |
3604 | 0, 0); | |
3605 | if (ret) | |
3606 | break; | |
74121f7c | 3607 | *last_extent = extent_end; |
16e7549f JB |
3608 | } |
3609 | /* | |
3610 | * Need to let the callers know we dropped the path so they should | |
3611 | * re-search. | |
3612 | */ | |
3613 | if (!ret && need_find_last_extent) | |
3614 | ret = 1; | |
4a500fd1 | 3615 | return ret; |
31ff1cd2 CM |
3616 | } |
3617 | ||
5dc562c5 JB |
3618 | static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) |
3619 | { | |
3620 | struct extent_map *em1, *em2; | |
3621 | ||
3622 | em1 = list_entry(a, struct extent_map, list); | |
3623 | em2 = list_entry(b, struct extent_map, list); | |
3624 | ||
3625 | if (em1->start < em2->start) | |
3626 | return -1; | |
3627 | else if (em1->start > em2->start) | |
3628 | return 1; | |
3629 | return 0; | |
3630 | } | |
3631 | ||
8407f553 FM |
3632 | static int wait_ordered_extents(struct btrfs_trans_handle *trans, |
3633 | struct inode *inode, | |
3634 | struct btrfs_root *root, | |
3635 | const struct extent_map *em, | |
3636 | const struct list_head *logged_list, | |
3637 | bool *ordered_io_error) | |
5dc562c5 | 3638 | { |
2ab28f32 | 3639 | struct btrfs_ordered_extent *ordered; |
8407f553 | 3640 | struct btrfs_root *log = root->log_root; |
2ab28f32 JB |
3641 | u64 mod_start = em->mod_start; |
3642 | u64 mod_len = em->mod_len; | |
8407f553 | 3643 | const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
2ab28f32 JB |
3644 | u64 csum_offset; |
3645 | u64 csum_len; | |
8407f553 FM |
3646 | LIST_HEAD(ordered_sums); |
3647 | int ret = 0; | |
0aa4a17d | 3648 | |
8407f553 | 3649 | *ordered_io_error = false; |
0aa4a17d | 3650 | |
8407f553 FM |
3651 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
3652 | em->block_start == EXTENT_MAP_HOLE) | |
70c8a91c | 3653 | return 0; |
5dc562c5 | 3654 | |
2ab28f32 | 3655 | /* |
8407f553 FM |
3656 | * Wait far any ordered extent that covers our extent map. If it |
3657 | * finishes without an error, first check and see if our csums are on | |
3658 | * our outstanding ordered extents. | |
2ab28f32 | 3659 | */ |
827463c4 | 3660 | list_for_each_entry(ordered, logged_list, log_list) { |
2ab28f32 JB |
3661 | struct btrfs_ordered_sum *sum; |
3662 | ||
3663 | if (!mod_len) | |
3664 | break; | |
3665 | ||
2ab28f32 JB |
3666 | if (ordered->file_offset + ordered->len <= mod_start || |
3667 | mod_start + mod_len <= ordered->file_offset) | |
3668 | continue; | |
3669 | ||
8407f553 FM |
3670 | if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) && |
3671 | !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) && | |
3672 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) { | |
3673 | const u64 start = ordered->file_offset; | |
3674 | const u64 end = ordered->file_offset + ordered->len - 1; | |
3675 | ||
3676 | WARN_ON(ordered->inode != inode); | |
3677 | filemap_fdatawrite_range(inode->i_mapping, start, end); | |
3678 | } | |
3679 | ||
3680 | wait_event(ordered->wait, | |
3681 | (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) || | |
3682 | test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))); | |
3683 | ||
3684 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) { | |
b38ef71c FM |
3685 | /* |
3686 | * Clear the AS_EIO/AS_ENOSPC flags from the inode's | |
3687 | * i_mapping flags, so that the next fsync won't get | |
3688 | * an outdated io error too. | |
3689 | */ | |
3690 | btrfs_inode_check_errors(inode); | |
8407f553 FM |
3691 | *ordered_io_error = true; |
3692 | break; | |
3693 | } | |
2ab28f32 JB |
3694 | /* |
3695 | * We are going to copy all the csums on this ordered extent, so | |
3696 | * go ahead and adjust mod_start and mod_len in case this | |
3697 | * ordered extent has already been logged. | |
3698 | */ | |
3699 | if (ordered->file_offset > mod_start) { | |
3700 | if (ordered->file_offset + ordered->len >= | |
3701 | mod_start + mod_len) | |
3702 | mod_len = ordered->file_offset - mod_start; | |
3703 | /* | |
3704 | * If we have this case | |
3705 | * | |
3706 | * |--------- logged extent ---------| | |
3707 | * |----- ordered extent ----| | |
3708 | * | |
3709 | * Just don't mess with mod_start and mod_len, we'll | |
3710 | * just end up logging more csums than we need and it | |
3711 | * will be ok. | |
3712 | */ | |
3713 | } else { | |
3714 | if (ordered->file_offset + ordered->len < | |
3715 | mod_start + mod_len) { | |
3716 | mod_len = (mod_start + mod_len) - | |
3717 | (ordered->file_offset + ordered->len); | |
3718 | mod_start = ordered->file_offset + | |
3719 | ordered->len; | |
3720 | } else { | |
3721 | mod_len = 0; | |
3722 | } | |
3723 | } | |
3724 | ||
8407f553 FM |
3725 | if (skip_csum) |
3726 | continue; | |
3727 | ||
2ab28f32 JB |
3728 | /* |
3729 | * To keep us from looping for the above case of an ordered | |
3730 | * extent that falls inside of the logged extent. | |
3731 | */ | |
3732 | if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, | |
3733 | &ordered->flags)) | |
3734 | continue; | |
2ab28f32 | 3735 | |
23c671a5 MX |
3736 | if (ordered->csum_bytes_left) { |
3737 | btrfs_start_ordered_extent(inode, ordered, 0); | |
3738 | wait_event(ordered->wait, | |
3739 | ordered->csum_bytes_left == 0); | |
3740 | } | |
2ab28f32 JB |
3741 | |
3742 | list_for_each_entry(sum, &ordered->list, list) { | |
3743 | ret = btrfs_csum_file_blocks(trans, log, sum); | |
827463c4 | 3744 | if (ret) |
8407f553 | 3745 | break; |
2ab28f32 | 3746 | } |
2ab28f32 | 3747 | } |
2ab28f32 | 3748 | |
8407f553 | 3749 | if (*ordered_io_error || !mod_len || ret || skip_csum) |
2ab28f32 JB |
3750 | return ret; |
3751 | ||
488111aa FDBM |
3752 | if (em->compress_type) { |
3753 | csum_offset = 0; | |
8407f553 | 3754 | csum_len = max(em->block_len, em->orig_block_len); |
488111aa FDBM |
3755 | } else { |
3756 | csum_offset = mod_start - em->start; | |
3757 | csum_len = mod_len; | |
3758 | } | |
2ab28f32 | 3759 | |
70c8a91c JB |
3760 | /* block start is already adjusted for the file extent offset. */ |
3761 | ret = btrfs_lookup_csums_range(log->fs_info->csum_root, | |
3762 | em->block_start + csum_offset, | |
3763 | em->block_start + csum_offset + | |
3764 | csum_len - 1, &ordered_sums, 0); | |
3765 | if (ret) | |
3766 | return ret; | |
5dc562c5 | 3767 | |
70c8a91c JB |
3768 | while (!list_empty(&ordered_sums)) { |
3769 | struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, | |
3770 | struct btrfs_ordered_sum, | |
3771 | list); | |
3772 | if (!ret) | |
3773 | ret = btrfs_csum_file_blocks(trans, log, sums); | |
3774 | list_del(&sums->list); | |
3775 | kfree(sums); | |
5dc562c5 JB |
3776 | } |
3777 | ||
70c8a91c | 3778 | return ret; |
5dc562c5 JB |
3779 | } |
3780 | ||
8407f553 FM |
3781 | static int log_one_extent(struct btrfs_trans_handle *trans, |
3782 | struct inode *inode, struct btrfs_root *root, | |
3783 | const struct extent_map *em, | |
3784 | struct btrfs_path *path, | |
3785 | const struct list_head *logged_list, | |
3786 | struct btrfs_log_ctx *ctx) | |
3787 | { | |
3788 | struct btrfs_root *log = root->log_root; | |
3789 | struct btrfs_file_extent_item *fi; | |
3790 | struct extent_buffer *leaf; | |
3791 | struct btrfs_map_token token; | |
3792 | struct btrfs_key key; | |
3793 | u64 extent_offset = em->start - em->orig_start; | |
3794 | u64 block_len; | |
3795 | int ret; | |
3796 | int extent_inserted = 0; | |
3797 | bool ordered_io_err = false; | |
3798 | ||
3799 | ret = wait_ordered_extents(trans, inode, root, em, logged_list, | |
3800 | &ordered_io_err); | |
3801 | if (ret) | |
3802 | return ret; | |
3803 | ||
3804 | if (ordered_io_err) { | |
3805 | ctx->io_err = -EIO; | |
3806 | return 0; | |
3807 | } | |
3808 | ||
3809 | btrfs_init_map_token(&token); | |
3810 | ||
3811 | ret = __btrfs_drop_extents(trans, log, inode, path, em->start, | |
3812 | em->start + em->len, NULL, 0, 1, | |
3813 | sizeof(*fi), &extent_inserted); | |
3814 | if (ret) | |
3815 | return ret; | |
3816 | ||
3817 | if (!extent_inserted) { | |
3818 | key.objectid = btrfs_ino(inode); | |
3819 | key.type = BTRFS_EXTENT_DATA_KEY; | |
3820 | key.offset = em->start; | |
3821 | ||
3822 | ret = btrfs_insert_empty_item(trans, log, path, &key, | |
3823 | sizeof(*fi)); | |
3824 | if (ret) | |
3825 | return ret; | |
3826 | } | |
3827 | leaf = path->nodes[0]; | |
3828 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
3829 | struct btrfs_file_extent_item); | |
3830 | ||
50d9aa99 | 3831 | btrfs_set_token_file_extent_generation(leaf, fi, trans->transid, |
8407f553 FM |
3832 | &token); |
3833 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
3834 | btrfs_set_token_file_extent_type(leaf, fi, | |
3835 | BTRFS_FILE_EXTENT_PREALLOC, | |
3836 | &token); | |
3837 | else | |
3838 | btrfs_set_token_file_extent_type(leaf, fi, | |
3839 | BTRFS_FILE_EXTENT_REG, | |
3840 | &token); | |
3841 | ||
3842 | block_len = max(em->block_len, em->orig_block_len); | |
3843 | if (em->compress_type != BTRFS_COMPRESS_NONE) { | |
3844 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, | |
3845 | em->block_start, | |
3846 | &token); | |
3847 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, | |
3848 | &token); | |
3849 | } else if (em->block_start < EXTENT_MAP_LAST_BYTE) { | |
3850 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, | |
3851 | em->block_start - | |
3852 | extent_offset, &token); | |
3853 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len, | |
3854 | &token); | |
3855 | } else { | |
3856 | btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token); | |
3857 | btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0, | |
3858 | &token); | |
3859 | } | |
3860 | ||
3861 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token); | |
3862 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); | |
3863 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); | |
3864 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, | |
3865 | &token); | |
3866 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); | |
3867 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); | |
3868 | btrfs_mark_buffer_dirty(leaf); | |
3869 | ||
3870 | btrfs_release_path(path); | |
3871 | ||
3872 | return ret; | |
3873 | } | |
3874 | ||
5dc562c5 JB |
3875 | static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, |
3876 | struct btrfs_root *root, | |
3877 | struct inode *inode, | |
827463c4 | 3878 | struct btrfs_path *path, |
8407f553 FM |
3879 | struct list_head *logged_list, |
3880 | struct btrfs_log_ctx *ctx) | |
5dc562c5 | 3881 | { |
5dc562c5 JB |
3882 | struct extent_map *em, *n; |
3883 | struct list_head extents; | |
3884 | struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree; | |
3885 | u64 test_gen; | |
3886 | int ret = 0; | |
2ab28f32 | 3887 | int num = 0; |
5dc562c5 JB |
3888 | |
3889 | INIT_LIST_HEAD(&extents); | |
3890 | ||
5dc562c5 JB |
3891 | write_lock(&tree->lock); |
3892 | test_gen = root->fs_info->last_trans_committed; | |
3893 | ||
3894 | list_for_each_entry_safe(em, n, &tree->modified_extents, list) { | |
3895 | list_del_init(&em->list); | |
2ab28f32 JB |
3896 | |
3897 | /* | |
3898 | * Just an arbitrary number, this can be really CPU intensive | |
3899 | * once we start getting a lot of extents, and really once we | |
3900 | * have a bunch of extents we just want to commit since it will | |
3901 | * be faster. | |
3902 | */ | |
3903 | if (++num > 32768) { | |
3904 | list_del_init(&tree->modified_extents); | |
3905 | ret = -EFBIG; | |
3906 | goto process; | |
3907 | } | |
3908 | ||
5dc562c5 JB |
3909 | if (em->generation <= test_gen) |
3910 | continue; | |
ff44c6e3 JB |
3911 | /* Need a ref to keep it from getting evicted from cache */ |
3912 | atomic_inc(&em->refs); | |
3913 | set_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
5dc562c5 | 3914 | list_add_tail(&em->list, &extents); |
2ab28f32 | 3915 | num++; |
5dc562c5 JB |
3916 | } |
3917 | ||
3918 | list_sort(NULL, &extents, extent_cmp); | |
3919 | ||
2ab28f32 | 3920 | process: |
5dc562c5 JB |
3921 | while (!list_empty(&extents)) { |
3922 | em = list_entry(extents.next, struct extent_map, list); | |
3923 | ||
3924 | list_del_init(&em->list); | |
3925 | ||
3926 | /* | |
3927 | * If we had an error we just need to delete everybody from our | |
3928 | * private list. | |
3929 | */ | |
ff44c6e3 | 3930 | if (ret) { |
201a9038 | 3931 | clear_em_logging(tree, em); |
ff44c6e3 | 3932 | free_extent_map(em); |
5dc562c5 | 3933 | continue; |
ff44c6e3 JB |
3934 | } |
3935 | ||
3936 | write_unlock(&tree->lock); | |
5dc562c5 | 3937 | |
8407f553 FM |
3938 | ret = log_one_extent(trans, inode, root, em, path, logged_list, |
3939 | ctx); | |
ff44c6e3 | 3940 | write_lock(&tree->lock); |
201a9038 JB |
3941 | clear_em_logging(tree, em); |
3942 | free_extent_map(em); | |
5dc562c5 | 3943 | } |
ff44c6e3 JB |
3944 | WARN_ON(!list_empty(&extents)); |
3945 | write_unlock(&tree->lock); | |
5dc562c5 | 3946 | |
5dc562c5 | 3947 | btrfs_release_path(path); |
5dc562c5 JB |
3948 | return ret; |
3949 | } | |
3950 | ||
1a4bcf47 FM |
3951 | static int logged_inode_size(struct btrfs_root *log, struct inode *inode, |
3952 | struct btrfs_path *path, u64 *size_ret) | |
3953 | { | |
3954 | struct btrfs_key key; | |
3955 | int ret; | |
3956 | ||
3957 | key.objectid = btrfs_ino(inode); | |
3958 | key.type = BTRFS_INODE_ITEM_KEY; | |
3959 | key.offset = 0; | |
3960 | ||
3961 | ret = btrfs_search_slot(NULL, log, &key, path, 0, 0); | |
3962 | if (ret < 0) { | |
3963 | return ret; | |
3964 | } else if (ret > 0) { | |
3965 | *size_ret = i_size_read(inode); | |
3966 | } else { | |
3967 | struct btrfs_inode_item *item; | |
3968 | ||
3969 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3970 | struct btrfs_inode_item); | |
3971 | *size_ret = btrfs_inode_size(path->nodes[0], item); | |
3972 | } | |
3973 | ||
3974 | btrfs_release_path(path); | |
3975 | return 0; | |
3976 | } | |
3977 | ||
e02119d5 CM |
3978 | /* log a single inode in the tree log. |
3979 | * At least one parent directory for this inode must exist in the tree | |
3980 | * or be logged already. | |
3981 | * | |
3982 | * Any items from this inode changed by the current transaction are copied | |
3983 | * to the log tree. An extra reference is taken on any extents in this | |
3984 | * file, allowing us to avoid a whole pile of corner cases around logging | |
3985 | * blocks that have been removed from the tree. | |
3986 | * | |
3987 | * See LOG_INODE_ALL and related defines for a description of what inode_only | |
3988 | * does. | |
3989 | * | |
3990 | * This handles both files and directories. | |
3991 | */ | |
12fcfd22 | 3992 | static int btrfs_log_inode(struct btrfs_trans_handle *trans, |
49dae1bc FM |
3993 | struct btrfs_root *root, struct inode *inode, |
3994 | int inode_only, | |
3995 | const loff_t start, | |
8407f553 FM |
3996 | const loff_t end, |
3997 | struct btrfs_log_ctx *ctx) | |
e02119d5 CM |
3998 | { |
3999 | struct btrfs_path *path; | |
4000 | struct btrfs_path *dst_path; | |
4001 | struct btrfs_key min_key; | |
4002 | struct btrfs_key max_key; | |
4003 | struct btrfs_root *log = root->log_root; | |
31ff1cd2 | 4004 | struct extent_buffer *src = NULL; |
827463c4 | 4005 | LIST_HEAD(logged_list); |
16e7549f | 4006 | u64 last_extent = 0; |
4a500fd1 | 4007 | int err = 0; |
e02119d5 | 4008 | int ret; |
3a5f1d45 | 4009 | int nritems; |
31ff1cd2 CM |
4010 | int ins_start_slot = 0; |
4011 | int ins_nr; | |
5dc562c5 | 4012 | bool fast_search = false; |
33345d01 | 4013 | u64 ino = btrfs_ino(inode); |
49dae1bc | 4014 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
1a4bcf47 | 4015 | u64 logged_isize = 0; |
e02119d5 | 4016 | |
e02119d5 | 4017 | path = btrfs_alloc_path(); |
5df67083 TI |
4018 | if (!path) |
4019 | return -ENOMEM; | |
e02119d5 | 4020 | dst_path = btrfs_alloc_path(); |
5df67083 TI |
4021 | if (!dst_path) { |
4022 | btrfs_free_path(path); | |
4023 | return -ENOMEM; | |
4024 | } | |
e02119d5 | 4025 | |
33345d01 | 4026 | min_key.objectid = ino; |
e02119d5 CM |
4027 | min_key.type = BTRFS_INODE_ITEM_KEY; |
4028 | min_key.offset = 0; | |
4029 | ||
33345d01 | 4030 | max_key.objectid = ino; |
12fcfd22 | 4031 | |
12fcfd22 | 4032 | |
5dc562c5 | 4033 | /* today the code can only do partial logging of directories */ |
5269b67e MX |
4034 | if (S_ISDIR(inode->i_mode) || |
4035 | (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4036 | &BTRFS_I(inode)->runtime_flags) && | |
4037 | inode_only == LOG_INODE_EXISTS)) | |
e02119d5 CM |
4038 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
4039 | else | |
4040 | max_key.type = (u8)-1; | |
4041 | max_key.offset = (u64)-1; | |
4042 | ||
2c2c452b FM |
4043 | /* |
4044 | * Only run delayed items if we are a dir or a new file. | |
4045 | * Otherwise commit the delayed inode only, which is needed in | |
4046 | * order for the log replay code to mark inodes for link count | |
4047 | * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). | |
4048 | */ | |
94edf4ae | 4049 | if (S_ISDIR(inode->i_mode) || |
2c2c452b | 4050 | BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) |
94edf4ae | 4051 | ret = btrfs_commit_inode_delayed_items(trans, inode); |
2c2c452b FM |
4052 | else |
4053 | ret = btrfs_commit_inode_delayed_inode(inode); | |
4054 | ||
4055 | if (ret) { | |
4056 | btrfs_free_path(path); | |
4057 | btrfs_free_path(dst_path); | |
4058 | return ret; | |
16cdcec7 MX |
4059 | } |
4060 | ||
e02119d5 CM |
4061 | mutex_lock(&BTRFS_I(inode)->log_mutex); |
4062 | ||
0870295b | 4063 | btrfs_get_logged_extents(inode, &logged_list, start, end); |
2ab28f32 | 4064 | |
e02119d5 CM |
4065 | /* |
4066 | * a brute force approach to making sure we get the most uptodate | |
4067 | * copies of everything. | |
4068 | */ | |
4069 | if (S_ISDIR(inode->i_mode)) { | |
4070 | int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; | |
4071 | ||
a742994a FM |
4072 | if (inode_only == LOG_INODE_EXISTS) { |
4073 | max_key_type = BTRFS_INODE_EXTREF_KEY; | |
4074 | max_key.type = max_key_type; | |
4075 | } | |
33345d01 | 4076 | ret = drop_objectid_items(trans, log, path, ino, max_key_type); |
e02119d5 | 4077 | } else { |
1a4bcf47 FM |
4078 | if (inode_only == LOG_INODE_EXISTS) { |
4079 | /* | |
4080 | * Make sure the new inode item we write to the log has | |
4081 | * the same isize as the current one (if it exists). | |
4082 | * This is necessary to prevent data loss after log | |
4083 | * replay, and also to prevent doing a wrong expanding | |
4084 | * truncate - for e.g. create file, write 4K into offset | |
4085 | * 0, fsync, write 4K into offset 4096, add hard link, | |
4086 | * fsync some other file (to sync log), power fail - if | |
4087 | * we use the inode's current i_size, after log replay | |
4088 | * we get a 8Kb file, with the last 4Kb extent as a hole | |
4089 | * (zeroes), as if an expanding truncate happened, | |
4090 | * instead of getting a file of 4Kb only. | |
4091 | */ | |
4092 | err = logged_inode_size(log, inode, path, | |
4093 | &logged_isize); | |
4094 | if (err) | |
4095 | goto out_unlock; | |
4096 | } | |
a742994a FM |
4097 | if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
4098 | &BTRFS_I(inode)->runtime_flags)) { | |
4099 | if (inode_only == LOG_INODE_EXISTS) { | |
4100 | max_key.type = BTRFS_INODE_EXTREF_KEY; | |
4101 | ret = drop_objectid_items(trans, log, path, ino, | |
4102 | max_key.type); | |
4103 | } else { | |
4104 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4105 | &BTRFS_I(inode)->runtime_flags); | |
4106 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, | |
4107 | &BTRFS_I(inode)->runtime_flags); | |
4108 | ret = btrfs_truncate_inode_items(trans, log, | |
4109 | inode, 0, 0); | |
4110 | } | |
4111 | } else if (test_bit(BTRFS_INODE_COPY_EVERYTHING, | |
4112 | &BTRFS_I(inode)->runtime_flags) || | |
6cfab851 | 4113 | inode_only == LOG_INODE_EXISTS) { |
a742994a FM |
4114 | if (inode_only == LOG_INODE_ALL) { |
4115 | clear_bit(BTRFS_INODE_COPY_EVERYTHING, | |
4116 | &BTRFS_I(inode)->runtime_flags); | |
183f37fa | 4117 | fast_search = true; |
a742994a FM |
4118 | max_key.type = BTRFS_XATTR_ITEM_KEY; |
4119 | } else { | |
4120 | max_key.type = BTRFS_INODE_EXTREF_KEY; | |
4121 | } | |
5dc562c5 | 4122 | ret = drop_objectid_items(trans, log, path, ino, |
e9976151 | 4123 | max_key.type); |
a95249b3 JB |
4124 | } else { |
4125 | if (inode_only == LOG_INODE_ALL) | |
4126 | fast_search = true; | |
4127 | ret = log_inode_item(trans, log, dst_path, inode); | |
4128 | if (ret) { | |
4129 | err = ret; | |
4130 | goto out_unlock; | |
4131 | } | |
4132 | goto log_extents; | |
5dc562c5 | 4133 | } |
a95249b3 | 4134 | |
e02119d5 | 4135 | } |
4a500fd1 YZ |
4136 | if (ret) { |
4137 | err = ret; | |
4138 | goto out_unlock; | |
4139 | } | |
e02119d5 | 4140 | |
d397712b | 4141 | while (1) { |
31ff1cd2 | 4142 | ins_nr = 0; |
6174d3cb | 4143 | ret = btrfs_search_forward(root, &min_key, |
de78b51a | 4144 | path, trans->transid); |
e02119d5 CM |
4145 | if (ret != 0) |
4146 | break; | |
3a5f1d45 | 4147 | again: |
31ff1cd2 | 4148 | /* note, ins_nr might be > 0 here, cleanup outside the loop */ |
33345d01 | 4149 | if (min_key.objectid != ino) |
e02119d5 CM |
4150 | break; |
4151 | if (min_key.type > max_key.type) | |
4152 | break; | |
31ff1cd2 | 4153 | |
e02119d5 | 4154 | src = path->nodes[0]; |
31ff1cd2 CM |
4155 | if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) { |
4156 | ins_nr++; | |
4157 | goto next_slot; | |
4158 | } else if (!ins_nr) { | |
4159 | ins_start_slot = path->slots[0]; | |
4160 | ins_nr = 1; | |
4161 | goto next_slot; | |
e02119d5 CM |
4162 | } |
4163 | ||
16e7549f | 4164 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
1a4bcf47 FM |
4165 | ins_start_slot, ins_nr, inode_only, |
4166 | logged_isize); | |
16e7549f | 4167 | if (ret < 0) { |
4a500fd1 YZ |
4168 | err = ret; |
4169 | goto out_unlock; | |
a71db86e RV |
4170 | } |
4171 | if (ret) { | |
16e7549f JB |
4172 | ins_nr = 0; |
4173 | btrfs_release_path(path); | |
4174 | continue; | |
4a500fd1 | 4175 | } |
31ff1cd2 CM |
4176 | ins_nr = 1; |
4177 | ins_start_slot = path->slots[0]; | |
4178 | next_slot: | |
e02119d5 | 4179 | |
3a5f1d45 CM |
4180 | nritems = btrfs_header_nritems(path->nodes[0]); |
4181 | path->slots[0]++; | |
4182 | if (path->slots[0] < nritems) { | |
4183 | btrfs_item_key_to_cpu(path->nodes[0], &min_key, | |
4184 | path->slots[0]); | |
4185 | goto again; | |
4186 | } | |
31ff1cd2 | 4187 | if (ins_nr) { |
16e7549f JB |
4188 | ret = copy_items(trans, inode, dst_path, path, |
4189 | &last_extent, ins_start_slot, | |
1a4bcf47 | 4190 | ins_nr, inode_only, logged_isize); |
16e7549f | 4191 | if (ret < 0) { |
4a500fd1 YZ |
4192 | err = ret; |
4193 | goto out_unlock; | |
4194 | } | |
16e7549f | 4195 | ret = 0; |
31ff1cd2 CM |
4196 | ins_nr = 0; |
4197 | } | |
b3b4aa74 | 4198 | btrfs_release_path(path); |
3a5f1d45 | 4199 | |
3d41d702 | 4200 | if (min_key.offset < (u64)-1) { |
e02119d5 | 4201 | min_key.offset++; |
3d41d702 | 4202 | } else if (min_key.type < max_key.type) { |
e02119d5 | 4203 | min_key.type++; |
3d41d702 FDBM |
4204 | min_key.offset = 0; |
4205 | } else { | |
e02119d5 | 4206 | break; |
3d41d702 | 4207 | } |
e02119d5 | 4208 | } |
31ff1cd2 | 4209 | if (ins_nr) { |
16e7549f | 4210 | ret = copy_items(trans, inode, dst_path, path, &last_extent, |
1a4bcf47 FM |
4211 | ins_start_slot, ins_nr, inode_only, |
4212 | logged_isize); | |
16e7549f | 4213 | if (ret < 0) { |
4a500fd1 YZ |
4214 | err = ret; |
4215 | goto out_unlock; | |
4216 | } | |
16e7549f | 4217 | ret = 0; |
31ff1cd2 CM |
4218 | ins_nr = 0; |
4219 | } | |
5dc562c5 | 4220 | |
a95249b3 | 4221 | log_extents: |
f3b15ccd JB |
4222 | btrfs_release_path(path); |
4223 | btrfs_release_path(dst_path); | |
5dc562c5 | 4224 | if (fast_search) { |
b38ef71c FM |
4225 | /* |
4226 | * Some ordered extents started by fsync might have completed | |
4227 | * before we collected the ordered extents in logged_list, which | |
4228 | * means they're gone, not in our logged_list nor in the inode's | |
4229 | * ordered tree. We want the application/user space to know an | |
4230 | * error happened while attempting to persist file data so that | |
4231 | * it can take proper action. If such error happened, we leave | |
4232 | * without writing to the log tree and the fsync must report the | |
4233 | * file data write error and not commit the current transaction. | |
4234 | */ | |
4235 | err = btrfs_inode_check_errors(inode); | |
4236 | if (err) { | |
4237 | ctx->io_err = err; | |
4238 | goto out_unlock; | |
4239 | } | |
827463c4 | 4240 | ret = btrfs_log_changed_extents(trans, root, inode, dst_path, |
8407f553 | 4241 | &logged_list, ctx); |
5dc562c5 JB |
4242 | if (ret) { |
4243 | err = ret; | |
4244 | goto out_unlock; | |
4245 | } | |
d006a048 | 4246 | } else if (inode_only == LOG_INODE_ALL) { |
06d3d22b LB |
4247 | struct extent_map *em, *n; |
4248 | ||
49dae1bc FM |
4249 | write_lock(&em_tree->lock); |
4250 | /* | |
4251 | * We can't just remove every em if we're called for a ranged | |
4252 | * fsync - that is, one that doesn't cover the whole possible | |
4253 | * file range (0 to LLONG_MAX). This is because we can have | |
4254 | * em's that fall outside the range we're logging and therefore | |
4255 | * their ordered operations haven't completed yet | |
4256 | * (btrfs_finish_ordered_io() not invoked yet). This means we | |
4257 | * didn't get their respective file extent item in the fs/subvol | |
4258 | * tree yet, and need to let the next fast fsync (one which | |
4259 | * consults the list of modified extent maps) find the em so | |
4260 | * that it logs a matching file extent item and waits for the | |
4261 | * respective ordered operation to complete (if it's still | |
4262 | * running). | |
4263 | * | |
4264 | * Removing every em outside the range we're logging would make | |
4265 | * the next fast fsync not log their matching file extent items, | |
4266 | * therefore making us lose data after a log replay. | |
4267 | */ | |
4268 | list_for_each_entry_safe(em, n, &em_tree->modified_extents, | |
4269 | list) { | |
4270 | const u64 mod_end = em->mod_start + em->mod_len - 1; | |
4271 | ||
4272 | if (em->mod_start >= start && mod_end <= end) | |
4273 | list_del_init(&em->list); | |
4274 | } | |
4275 | write_unlock(&em_tree->lock); | |
5dc562c5 JB |
4276 | } |
4277 | ||
9623f9a3 | 4278 | if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { |
e02119d5 | 4279 | ret = log_directory_changes(trans, root, inode, path, dst_path); |
4a500fd1 YZ |
4280 | if (ret) { |
4281 | err = ret; | |
4282 | goto out_unlock; | |
4283 | } | |
e02119d5 | 4284 | } |
49dae1bc | 4285 | |
125c4cf9 FM |
4286 | BTRFS_I(inode)->logged_trans = trans->transid; |
4287 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans; | |
4a500fd1 | 4288 | out_unlock: |
827463c4 MX |
4289 | if (unlikely(err)) |
4290 | btrfs_put_logged_extents(&logged_list); | |
4291 | else | |
4292 | btrfs_submit_logged_extents(&logged_list, log); | |
e02119d5 CM |
4293 | mutex_unlock(&BTRFS_I(inode)->log_mutex); |
4294 | ||
4295 | btrfs_free_path(path); | |
4296 | btrfs_free_path(dst_path); | |
4a500fd1 | 4297 | return err; |
e02119d5 CM |
4298 | } |
4299 | ||
12fcfd22 CM |
4300 | /* |
4301 | * follow the dentry parent pointers up the chain and see if any | |
4302 | * of the directories in it require a full commit before they can | |
4303 | * be logged. Returns zero if nothing special needs to be done or 1 if | |
4304 | * a full commit is required. | |
4305 | */ | |
4306 | static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans, | |
4307 | struct inode *inode, | |
4308 | struct dentry *parent, | |
4309 | struct super_block *sb, | |
4310 | u64 last_committed) | |
e02119d5 | 4311 | { |
12fcfd22 CM |
4312 | int ret = 0; |
4313 | struct btrfs_root *root; | |
6a912213 | 4314 | struct dentry *old_parent = NULL; |
de2b530b | 4315 | struct inode *orig_inode = inode; |
e02119d5 | 4316 | |
af4176b4 CM |
4317 | /* |
4318 | * for regular files, if its inode is already on disk, we don't | |
4319 | * have to worry about the parents at all. This is because | |
4320 | * we can use the last_unlink_trans field to record renames | |
4321 | * and other fun in this file. | |
4322 | */ | |
4323 | if (S_ISREG(inode->i_mode) && | |
4324 | BTRFS_I(inode)->generation <= last_committed && | |
4325 | BTRFS_I(inode)->last_unlink_trans <= last_committed) | |
4326 | goto out; | |
4327 | ||
12fcfd22 CM |
4328 | if (!S_ISDIR(inode->i_mode)) { |
4329 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
4330 | goto out; | |
4331 | inode = parent->d_inode; | |
4332 | } | |
4333 | ||
4334 | while (1) { | |
de2b530b JB |
4335 | /* |
4336 | * If we are logging a directory then we start with our inode, | |
4337 | * not our parents inode, so we need to skipp setting the | |
4338 | * logged_trans so that further down in the log code we don't | |
4339 | * think this inode has already been logged. | |
4340 | */ | |
4341 | if (inode != orig_inode) | |
4342 | BTRFS_I(inode)->logged_trans = trans->transid; | |
12fcfd22 CM |
4343 | smp_mb(); |
4344 | ||
4345 | if (BTRFS_I(inode)->last_unlink_trans > last_committed) { | |
4346 | root = BTRFS_I(inode)->root; | |
4347 | ||
4348 | /* | |
4349 | * make sure any commits to the log are forced | |
4350 | * to be full commits | |
4351 | */ | |
995946dd | 4352 | btrfs_set_log_full_commit(root->fs_info, trans); |
12fcfd22 CM |
4353 | ret = 1; |
4354 | break; | |
4355 | } | |
4356 | ||
4357 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
4358 | break; | |
4359 | ||
76dda93c | 4360 | if (IS_ROOT(parent)) |
12fcfd22 CM |
4361 | break; |
4362 | ||
6a912213 JB |
4363 | parent = dget_parent(parent); |
4364 | dput(old_parent); | |
4365 | old_parent = parent; | |
12fcfd22 CM |
4366 | inode = parent->d_inode; |
4367 | ||
4368 | } | |
6a912213 | 4369 | dput(old_parent); |
12fcfd22 | 4370 | out: |
e02119d5 CM |
4371 | return ret; |
4372 | } | |
4373 | ||
4374 | /* | |
4375 | * helper function around btrfs_log_inode to make sure newly created | |
4376 | * parent directories also end up in the log. A minimal inode and backref | |
4377 | * only logging is done of any parent directories that are older than | |
4378 | * the last committed transaction | |
4379 | */ | |
48a3b636 ES |
4380 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
4381 | struct btrfs_root *root, struct inode *inode, | |
49dae1bc FM |
4382 | struct dentry *parent, |
4383 | const loff_t start, | |
4384 | const loff_t end, | |
4385 | int exists_only, | |
8b050d35 | 4386 | struct btrfs_log_ctx *ctx) |
e02119d5 | 4387 | { |
12fcfd22 | 4388 | int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; |
e02119d5 | 4389 | struct super_block *sb; |
6a912213 | 4390 | struct dentry *old_parent = NULL; |
12fcfd22 CM |
4391 | int ret = 0; |
4392 | u64 last_committed = root->fs_info->last_trans_committed; | |
d36808e0 FM |
4393 | const struct dentry * const first_parent = parent; |
4394 | const bool did_unlink = (BTRFS_I(inode)->last_unlink_trans > | |
4395 | last_committed); | |
12fcfd22 CM |
4396 | |
4397 | sb = inode->i_sb; | |
4398 | ||
3a5e1404 SW |
4399 | if (btrfs_test_opt(root, NOTREELOG)) { |
4400 | ret = 1; | |
4401 | goto end_no_trans; | |
4402 | } | |
4403 | ||
995946dd MX |
4404 | /* |
4405 | * The prev transaction commit doesn't complete, we need do | |
4406 | * full commit by ourselves. | |
4407 | */ | |
12fcfd22 CM |
4408 | if (root->fs_info->last_trans_log_full_commit > |
4409 | root->fs_info->last_trans_committed) { | |
4410 | ret = 1; | |
4411 | goto end_no_trans; | |
4412 | } | |
4413 | ||
76dda93c YZ |
4414 | if (root != BTRFS_I(inode)->root || |
4415 | btrfs_root_refs(&root->root_item) == 0) { | |
4416 | ret = 1; | |
4417 | goto end_no_trans; | |
4418 | } | |
4419 | ||
12fcfd22 CM |
4420 | ret = check_parent_dirs_for_sync(trans, inode, parent, |
4421 | sb, last_committed); | |
4422 | if (ret) | |
4423 | goto end_no_trans; | |
e02119d5 | 4424 | |
22ee6985 | 4425 | if (btrfs_inode_in_log(inode, trans->transid)) { |
257c62e1 CM |
4426 | ret = BTRFS_NO_LOG_SYNC; |
4427 | goto end_no_trans; | |
4428 | } | |
4429 | ||
8b050d35 | 4430 | ret = start_log_trans(trans, root, ctx); |
4a500fd1 | 4431 | if (ret) |
e87ac136 | 4432 | goto end_no_trans; |
e02119d5 | 4433 | |
8407f553 | 4434 | ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx); |
4a500fd1 YZ |
4435 | if (ret) |
4436 | goto end_trans; | |
12fcfd22 | 4437 | |
af4176b4 CM |
4438 | /* |
4439 | * for regular files, if its inode is already on disk, we don't | |
4440 | * have to worry about the parents at all. This is because | |
4441 | * we can use the last_unlink_trans field to record renames | |
4442 | * and other fun in this file. | |
4443 | */ | |
4444 | if (S_ISREG(inode->i_mode) && | |
4445 | BTRFS_I(inode)->generation <= last_committed && | |
4a500fd1 YZ |
4446 | BTRFS_I(inode)->last_unlink_trans <= last_committed) { |
4447 | ret = 0; | |
4448 | goto end_trans; | |
4449 | } | |
af4176b4 | 4450 | |
12fcfd22 CM |
4451 | while (1) { |
4452 | if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb) | |
e02119d5 CM |
4453 | break; |
4454 | ||
12fcfd22 | 4455 | inode = parent->d_inode; |
76dda93c YZ |
4456 | if (root != BTRFS_I(inode)->root) |
4457 | break; | |
4458 | ||
d36808e0 FM |
4459 | /* |
4460 | * On unlink we must make sure our immediate parent directory | |
4461 | * inode is fully logged. This is to prevent leaving dangling | |
4462 | * directory index entries and a wrong directory inode's i_size. | |
4463 | * Not doing so can result in a directory being impossible to | |
4464 | * delete after log replay (rmdir will always fail with error | |
4465 | * -ENOTEMPTY). | |
4466 | */ | |
4467 | if (did_unlink && parent == first_parent) | |
4468 | inode_only = LOG_INODE_ALL; | |
4469 | else | |
4470 | inode_only = LOG_INODE_EXISTS; | |
4471 | ||
12fcfd22 | 4472 | if (BTRFS_I(inode)->generation > |
d36808e0 FM |
4473 | root->fs_info->last_trans_committed || |
4474 | inode_only == LOG_INODE_ALL) { | |
49dae1bc | 4475 | ret = btrfs_log_inode(trans, root, inode, inode_only, |
8407f553 | 4476 | 0, LLONG_MAX, ctx); |
4a500fd1 YZ |
4477 | if (ret) |
4478 | goto end_trans; | |
12fcfd22 | 4479 | } |
76dda93c | 4480 | if (IS_ROOT(parent)) |
e02119d5 | 4481 | break; |
12fcfd22 | 4482 | |
6a912213 JB |
4483 | parent = dget_parent(parent); |
4484 | dput(old_parent); | |
4485 | old_parent = parent; | |
e02119d5 | 4486 | } |
12fcfd22 | 4487 | ret = 0; |
4a500fd1 | 4488 | end_trans: |
6a912213 | 4489 | dput(old_parent); |
4a500fd1 | 4490 | if (ret < 0) { |
995946dd | 4491 | btrfs_set_log_full_commit(root->fs_info, trans); |
4a500fd1 YZ |
4492 | ret = 1; |
4493 | } | |
8b050d35 MX |
4494 | |
4495 | if (ret) | |
4496 | btrfs_remove_log_ctx(root, ctx); | |
12fcfd22 CM |
4497 | btrfs_end_log_trans(root); |
4498 | end_no_trans: | |
4499 | return ret; | |
e02119d5 CM |
4500 | } |
4501 | ||
4502 | /* | |
4503 | * it is not safe to log dentry if the chunk root has added new | |
4504 | * chunks. This returns 0 if the dentry was logged, and 1 otherwise. | |
4505 | * If this returns 1, you must commit the transaction to safely get your | |
4506 | * data on disk. | |
4507 | */ | |
4508 | int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, | |
8b050d35 | 4509 | struct btrfs_root *root, struct dentry *dentry, |
49dae1bc FM |
4510 | const loff_t start, |
4511 | const loff_t end, | |
8b050d35 | 4512 | struct btrfs_log_ctx *ctx) |
e02119d5 | 4513 | { |
6a912213 JB |
4514 | struct dentry *parent = dget_parent(dentry); |
4515 | int ret; | |
4516 | ||
8b050d35 | 4517 | ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, |
49dae1bc | 4518 | start, end, 0, ctx); |
6a912213 JB |
4519 | dput(parent); |
4520 | ||
4521 | return ret; | |
e02119d5 CM |
4522 | } |
4523 | ||
4524 | /* | |
4525 | * should be called during mount to recover any replay any log trees | |
4526 | * from the FS | |
4527 | */ | |
4528 | int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) | |
4529 | { | |
4530 | int ret; | |
4531 | struct btrfs_path *path; | |
4532 | struct btrfs_trans_handle *trans; | |
4533 | struct btrfs_key key; | |
4534 | struct btrfs_key found_key; | |
4535 | struct btrfs_key tmp_key; | |
4536 | struct btrfs_root *log; | |
4537 | struct btrfs_fs_info *fs_info = log_root_tree->fs_info; | |
4538 | struct walk_control wc = { | |
4539 | .process_func = process_one_buffer, | |
4540 | .stage = 0, | |
4541 | }; | |
4542 | ||
e02119d5 | 4543 | path = btrfs_alloc_path(); |
db5b493a TI |
4544 | if (!path) |
4545 | return -ENOMEM; | |
4546 | ||
4547 | fs_info->log_root_recovering = 1; | |
e02119d5 | 4548 | |
4a500fd1 | 4549 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
79787eaa JM |
4550 | if (IS_ERR(trans)) { |
4551 | ret = PTR_ERR(trans); | |
4552 | goto error; | |
4553 | } | |
e02119d5 CM |
4554 | |
4555 | wc.trans = trans; | |
4556 | wc.pin = 1; | |
4557 | ||
db5b493a | 4558 | ret = walk_log_tree(trans, log_root_tree, &wc); |
79787eaa JM |
4559 | if (ret) { |
4560 | btrfs_error(fs_info, ret, "Failed to pin buffers while " | |
4561 | "recovering log root tree."); | |
4562 | goto error; | |
4563 | } | |
e02119d5 CM |
4564 | |
4565 | again: | |
4566 | key.objectid = BTRFS_TREE_LOG_OBJECTID; | |
4567 | key.offset = (u64)-1; | |
962a298f | 4568 | key.type = BTRFS_ROOT_ITEM_KEY; |
e02119d5 | 4569 | |
d397712b | 4570 | while (1) { |
e02119d5 | 4571 | ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); |
79787eaa JM |
4572 | |
4573 | if (ret < 0) { | |
4574 | btrfs_error(fs_info, ret, | |
4575 | "Couldn't find tree log root."); | |
4576 | goto error; | |
4577 | } | |
e02119d5 CM |
4578 | if (ret > 0) { |
4579 | if (path->slots[0] == 0) | |
4580 | break; | |
4581 | path->slots[0]--; | |
4582 | } | |
4583 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
4584 | path->slots[0]); | |
b3b4aa74 | 4585 | btrfs_release_path(path); |
e02119d5 CM |
4586 | if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
4587 | break; | |
4588 | ||
cb517eab | 4589 | log = btrfs_read_fs_root(log_root_tree, &found_key); |
79787eaa JM |
4590 | if (IS_ERR(log)) { |
4591 | ret = PTR_ERR(log); | |
4592 | btrfs_error(fs_info, ret, | |
4593 | "Couldn't read tree log root."); | |
4594 | goto error; | |
4595 | } | |
e02119d5 CM |
4596 | |
4597 | tmp_key.objectid = found_key.offset; | |
4598 | tmp_key.type = BTRFS_ROOT_ITEM_KEY; | |
4599 | tmp_key.offset = (u64)-1; | |
4600 | ||
4601 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); | |
79787eaa JM |
4602 | if (IS_ERR(wc.replay_dest)) { |
4603 | ret = PTR_ERR(wc.replay_dest); | |
b50c6e25 JB |
4604 | free_extent_buffer(log->node); |
4605 | free_extent_buffer(log->commit_root); | |
4606 | kfree(log); | |
79787eaa JM |
4607 | btrfs_error(fs_info, ret, "Couldn't read target root " |
4608 | "for tree log recovery."); | |
4609 | goto error; | |
4610 | } | |
e02119d5 | 4611 | |
07d400a6 | 4612 | wc.replay_dest->log_root = log; |
5d4f98a2 | 4613 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
e02119d5 | 4614 | ret = walk_log_tree(trans, log, &wc); |
e02119d5 | 4615 | |
b50c6e25 | 4616 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
e02119d5 CM |
4617 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
4618 | path); | |
e02119d5 CM |
4619 | } |
4620 | ||
4621 | key.offset = found_key.offset - 1; | |
07d400a6 | 4622 | wc.replay_dest->log_root = NULL; |
e02119d5 | 4623 | free_extent_buffer(log->node); |
b263c2c8 | 4624 | free_extent_buffer(log->commit_root); |
e02119d5 CM |
4625 | kfree(log); |
4626 | ||
b50c6e25 JB |
4627 | if (ret) |
4628 | goto error; | |
4629 | ||
e02119d5 CM |
4630 | if (found_key.offset == 0) |
4631 | break; | |
4632 | } | |
b3b4aa74 | 4633 | btrfs_release_path(path); |
e02119d5 CM |
4634 | |
4635 | /* step one is to pin it all, step two is to replay just inodes */ | |
4636 | if (wc.pin) { | |
4637 | wc.pin = 0; | |
4638 | wc.process_func = replay_one_buffer; | |
4639 | wc.stage = LOG_WALK_REPLAY_INODES; | |
4640 | goto again; | |
4641 | } | |
4642 | /* step three is to replay everything */ | |
4643 | if (wc.stage < LOG_WALK_REPLAY_ALL) { | |
4644 | wc.stage++; | |
4645 | goto again; | |
4646 | } | |
4647 | ||
4648 | btrfs_free_path(path); | |
4649 | ||
abefa55a JB |
4650 | /* step 4: commit the transaction, which also unpins the blocks */ |
4651 | ret = btrfs_commit_transaction(trans, fs_info->tree_root); | |
4652 | if (ret) | |
4653 | return ret; | |
4654 | ||
e02119d5 CM |
4655 | free_extent_buffer(log_root_tree->node); |
4656 | log_root_tree->log_root = NULL; | |
4657 | fs_info->log_root_recovering = 0; | |
e02119d5 | 4658 | kfree(log_root_tree); |
79787eaa | 4659 | |
abefa55a | 4660 | return 0; |
79787eaa | 4661 | error: |
b50c6e25 JB |
4662 | if (wc.trans) |
4663 | btrfs_end_transaction(wc.trans, fs_info->tree_root); | |
79787eaa JM |
4664 | btrfs_free_path(path); |
4665 | return ret; | |
e02119d5 | 4666 | } |
12fcfd22 CM |
4667 | |
4668 | /* | |
4669 | * there are some corner cases where we want to force a full | |
4670 | * commit instead of allowing a directory to be logged. | |
4671 | * | |
4672 | * They revolve around files there were unlinked from the directory, and | |
4673 | * this function updates the parent directory so that a full commit is | |
4674 | * properly done if it is fsync'd later after the unlinks are done. | |
4675 | */ | |
4676 | void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans, | |
4677 | struct inode *dir, struct inode *inode, | |
4678 | int for_rename) | |
4679 | { | |
af4176b4 CM |
4680 | /* |
4681 | * when we're logging a file, if it hasn't been renamed | |
4682 | * or unlinked, and its inode is fully committed on disk, | |
4683 | * we don't have to worry about walking up the directory chain | |
4684 | * to log its parents. | |
4685 | * | |
4686 | * So, we use the last_unlink_trans field to put this transid | |
4687 | * into the file. When the file is logged we check it and | |
4688 | * don't log the parents if the file is fully on disk. | |
4689 | */ | |
4690 | if (S_ISREG(inode->i_mode)) | |
4691 | BTRFS_I(inode)->last_unlink_trans = trans->transid; | |
4692 | ||
12fcfd22 CM |
4693 | /* |
4694 | * if this directory was already logged any new | |
4695 | * names for this file/dir will get recorded | |
4696 | */ | |
4697 | smp_mb(); | |
4698 | if (BTRFS_I(dir)->logged_trans == trans->transid) | |
4699 | return; | |
4700 | ||
4701 | /* | |
4702 | * if the inode we're about to unlink was logged, | |
4703 | * the log will be properly updated for any new names | |
4704 | */ | |
4705 | if (BTRFS_I(inode)->logged_trans == trans->transid) | |
4706 | return; | |
4707 | ||
4708 | /* | |
4709 | * when renaming files across directories, if the directory | |
4710 | * there we're unlinking from gets fsync'd later on, there's | |
4711 | * no way to find the destination directory later and fsync it | |
4712 | * properly. So, we have to be conservative and force commits | |
4713 | * so the new name gets discovered. | |
4714 | */ | |
4715 | if (for_rename) | |
4716 | goto record; | |
4717 | ||
4718 | /* we can safely do the unlink without any special recording */ | |
4719 | return; | |
4720 | ||
4721 | record: | |
4722 | BTRFS_I(dir)->last_unlink_trans = trans->transid; | |
4723 | } | |
4724 | ||
4725 | /* | |
4726 | * Call this after adding a new name for a file and it will properly | |
4727 | * update the log to reflect the new name. | |
4728 | * | |
4729 | * It will return zero if all goes well, and it will return 1 if a | |
4730 | * full transaction commit is required. | |
4731 | */ | |
4732 | int btrfs_log_new_name(struct btrfs_trans_handle *trans, | |
4733 | struct inode *inode, struct inode *old_dir, | |
4734 | struct dentry *parent) | |
4735 | { | |
4736 | struct btrfs_root * root = BTRFS_I(inode)->root; | |
4737 | ||
af4176b4 CM |
4738 | /* |
4739 | * this will force the logging code to walk the dentry chain | |
4740 | * up for the file | |
4741 | */ | |
4742 | if (S_ISREG(inode->i_mode)) | |
4743 | BTRFS_I(inode)->last_unlink_trans = trans->transid; | |
4744 | ||
12fcfd22 CM |
4745 | /* |
4746 | * if this inode hasn't been logged and directory we're renaming it | |
4747 | * from hasn't been logged, we don't need to log it | |
4748 | */ | |
4749 | if (BTRFS_I(inode)->logged_trans <= | |
4750 | root->fs_info->last_trans_committed && | |
4751 | (!old_dir || BTRFS_I(old_dir)->logged_trans <= | |
4752 | root->fs_info->last_trans_committed)) | |
4753 | return 0; | |
4754 | ||
49dae1bc FM |
4755 | return btrfs_log_inode_parent(trans, root, inode, parent, 0, |
4756 | LLONG_MAX, 1, NULL); | |
12fcfd22 CM |
4757 | } |
4758 |