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