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