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