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