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