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