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