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