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