blob: f547d80b5bf160d6d1103cddcbe1f3d8dd5cf10d [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Miao Xie995946d2014-04-02 19:51:06 +080023#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040024#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070027#include "backref.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "hash.h"
Anand Jainebb87652016-03-10 17:26:59 +080029#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080030#include "qgroup.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
Liu Bo67312122016-11-30 16:20:25 -080040#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040041
42/*
Chris Mason12fcfd22009-03-24 10:24:20 -040043 * directory trouble cases
44 *
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
49 *
50 * mkdir foo/some_dir
51 * normal commit
52 * rename foo/some_dir foo2/some_dir
53 * mkdir foo/some_dir
54 * fsync foo/some_dir/some_file
55 *
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
59 *
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
62 *
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
66 *
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
69 *
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
72 *
73 * mkdir f1/foo
74 * normal commit
75 * rm -rf f1/foo
76 * fsync(f1)
77 *
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
82 * ugly details.
83 */
84
85/*
Chris Masone02119d2008-09-05 16:13:11 -040086 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
90 *
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
93 */
94#define LOG_WALK_PIN_ONLY 0
95#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040096#define LOG_WALK_REPLAY_DIR_INDEX 2
97#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040098
Chris Mason12fcfd22009-03-24 10:24:20 -040099static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100100 struct btrfs_root *root, struct inode *inode,
101 int inode_only,
102 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100103 const loff_t end,
104 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500105static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400108static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
109 struct btrfs_root *root,
110 struct btrfs_root *log,
111 struct btrfs_path *path,
112 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400113
114/*
115 * tree logging is a special write ahead log used to make sure that
116 * fsyncs and O_SYNCs can happen without doing full tree commits.
117 *
118 * Full tree commits are expensive because they require commonly
119 * modified blocks to be recowed, creating many dirty pages in the
120 * extent tree an 4x-6x higher write load than ext3.
121 *
122 * Instead of doing a tree commit on every fsync, we use the
123 * key ranges and transaction ids to find items for a given file or directory
124 * that have changed in this transaction. Those items are copied into
125 * a special tree (one per subvolume root), that tree is written to disk
126 * and then the fsync is considered complete.
127 *
128 * After a crash, items are copied out of the log-tree back into the
129 * subvolume tree. Any file data extents found are recorded in the extent
130 * allocation tree, and the log-tree freed.
131 *
132 * The log tree is read three times, once to pin down all the extents it is
133 * using in ram and once, once to create all the inodes logged in the tree
134 * and once to do all the other items.
135 */
136
137/*
Chris Masone02119d2008-09-05 16:13:11 -0400138 * start a sub transaction and setup the log tree
139 * this increments the log tree writer count to make the people
140 * syncing the tree wait for us to finish
141 */
142static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800143 struct btrfs_root *root,
144 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400145{
Zhaolei34eb2a52015-08-17 18:44:45 +0800146 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500147
148 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800149
Yan Zheng7237f182009-01-21 12:54:03 -0500150 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800151 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800152 ret = -EAGAIN;
153 goto out;
154 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800155
Josef Bacikff782e02009-10-08 15:30:04 -0400156 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800157 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800158 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400159 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800160 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400161 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800162 } else {
163 mutex_lock(&root->fs_info->tree_log_mutex);
164 if (!root->fs_info->log_root_tree)
165 ret = btrfs_init_log_root_tree(trans, root->fs_info);
166 mutex_unlock(&root->fs_info->tree_log_mutex);
167 if (ret)
168 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400169
Chris Masone02119d2008-09-05 16:13:11 -0400170 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400171 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800172 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800173
174 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
175 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400176 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800177
Miao Xie2ecb7922012-09-06 04:04:27 -0600178 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500179 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800180 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800181 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800182 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800183 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800184 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800185
Miao Xiee87ac132014-02-20 18:08:53 +0800186out:
Yan Zheng7237f182009-01-21 12:54:03 -0500187 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800188 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400189}
190
191/*
192 * returns 0 if there was a log transaction running and we were able
193 * to join, or returns -ENOENT if there were not transactions
194 * in progress
195 */
196static int join_running_log_trans(struct btrfs_root *root)
197{
198 int ret = -ENOENT;
199
200 smp_mb();
201 if (!root->log_root)
202 return -ENOENT;
203
Yan Zheng7237f182009-01-21 12:54:03 -0500204 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400205 if (root->log_root) {
206 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500207 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400208 }
Yan Zheng7237f182009-01-21 12:54:03 -0500209 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400210 return ret;
211}
212
213/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400214 * This either makes the current running log transaction wait
215 * until you call btrfs_end_log_trans() or it makes any future
216 * log transactions wait until you call btrfs_end_log_trans()
217 */
218int btrfs_pin_log_trans(struct btrfs_root *root)
219{
220 int ret = -ENOENT;
221
222 mutex_lock(&root->log_mutex);
223 atomic_inc(&root->log_writers);
224 mutex_unlock(&root->log_mutex);
225 return ret;
226}
227
228/*
Chris Masone02119d2008-09-05 16:13:11 -0400229 * indicate we're done making changes to the log tree
230 * and wake up anyone waiting to do a sync
231 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100232void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400233{
Yan Zheng7237f182009-01-21 12:54:03 -0500234 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100235 /*
236 * Implicit memory barrier after atomic_dec_and_test
237 */
Yan Zheng7237f182009-01-21 12:54:03 -0500238 if (waitqueue_active(&root->log_writer_wait))
239 wake_up(&root->log_writer_wait);
240 }
Chris Masone02119d2008-09-05 16:13:11 -0400241}
242
243
244/*
245 * the walk control struct is used to pass state down the chain when
246 * processing the log tree. The stage field tells us which part
247 * of the log tree processing we are currently doing. The others
248 * are state fields used for that specific part
249 */
250struct walk_control {
251 /* should we free the extent on disk when done? This is used
252 * at transaction commit time while freeing a log tree
253 */
254 int free;
255
256 /* should we write out the extent buffer? This is used
257 * while flushing the log tree to disk during a sync
258 */
259 int write;
260
261 /* should we wait for the extent buffer io to finish? Also used
262 * while flushing the log tree to disk for a sync
263 */
264 int wait;
265
266 /* pin only walk, we record which extents on disk belong to the
267 * log trees
268 */
269 int pin;
270
271 /* what stage of the replay code we're currently in */
272 int stage;
273
274 /* the root we are currently replaying */
275 struct btrfs_root *replay_dest;
276
277 /* the trans handle for the current replay */
278 struct btrfs_trans_handle *trans;
279
280 /* the function that gets used to process blocks we find in the
281 * tree. Note the extent_buffer might not be up to date when it is
282 * passed in, and it must be checked or read if you need the data
283 * inside it
284 */
285 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
286 struct walk_control *wc, u64 gen);
287};
288
289/*
290 * process_func used to pin down extents, write them or wait on them
291 */
292static int process_one_buffer(struct btrfs_root *log,
293 struct extent_buffer *eb,
294 struct walk_control *wc, u64 gen)
295{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400296 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400297
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400298 /*
299 * If this fs is mixed then we need to be able to process the leaves to
300 * pin down any logged extents, so we have to read the block.
301 */
302 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
303 ret = btrfs_read_buffer(eb, gen);
304 if (ret)
305 return ret;
306 }
307
Josef Bacikb50c6e22013-04-25 15:55:30 -0400308 if (wc->pin)
309 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
310 eb->start, eb->len);
311
312 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400313 if (wc->pin && btrfs_header_level(eb) == 0)
314 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400315 if (wc->write)
316 btrfs_write_tree_block(eb);
317 if (wc->wait)
318 btrfs_wait_tree_block_writeback(eb);
319 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400320 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400321}
322
323/*
324 * Item overwrite used by replay and tree logging. eb, slot and key all refer
325 * to the src data we are copying out.
326 *
327 * root is the tree we are copying into, and path is a scratch
328 * path for use in this function (it should be released on entry and
329 * will be released on exit).
330 *
331 * If the key is already in the destination tree the existing item is
332 * overwritten. If the existing item isn't big enough, it is extended.
333 * If it is too large, it is truncated.
334 *
335 * If the key isn't in the destination yet, a new item is inserted.
336 */
337static noinline int overwrite_item(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root,
339 struct btrfs_path *path,
340 struct extent_buffer *eb, int slot,
341 struct btrfs_key *key)
342{
343 int ret;
344 u32 item_size;
345 u64 saved_i_size = 0;
346 int save_old_i_size = 0;
347 unsigned long src_ptr;
348 unsigned long dst_ptr;
349 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000350 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400351
352 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
353 overwrite_root = 1;
354
355 item_size = btrfs_item_size_nr(eb, slot);
356 src_ptr = btrfs_item_ptr_offset(eb, slot);
357
358 /* look for the key in the destination tree */
359 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000360 if (ret < 0)
361 return ret;
362
Chris Masone02119d2008-09-05 16:13:11 -0400363 if (ret == 0) {
364 char *src_copy;
365 char *dst_copy;
366 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
367 path->slots[0]);
368 if (dst_size != item_size)
369 goto insert;
370
371 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200372 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400373 return 0;
374 }
375 dst_copy = kmalloc(item_size, GFP_NOFS);
376 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000377 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200378 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000379 kfree(dst_copy);
380 kfree(src_copy);
381 return -ENOMEM;
382 }
Chris Masone02119d2008-09-05 16:13:11 -0400383
384 read_extent_buffer(eb, src_copy, src_ptr, item_size);
385
386 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
387 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
388 item_size);
389 ret = memcmp(dst_copy, src_copy, item_size);
390
391 kfree(dst_copy);
392 kfree(src_copy);
393 /*
394 * they have the same contents, just return, this saves
395 * us from cowing blocks in the destination tree and doing
396 * extra writes that may not have been done by a previous
397 * sync
398 */
399 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200400 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400401 return 0;
402 }
403
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000404 /*
405 * We need to load the old nbytes into the inode so when we
406 * replay the extents we've logged we get the right nbytes.
407 */
408 if (inode_item) {
409 struct btrfs_inode_item *item;
410 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400411 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000412
413 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
414 struct btrfs_inode_item);
415 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
416 item = btrfs_item_ptr(eb, slot,
417 struct btrfs_inode_item);
418 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400419
420 /*
421 * If this is a directory we need to reset the i_size to
422 * 0 so that we can set it up properly when replaying
423 * the rest of the items in this log.
424 */
425 mode = btrfs_inode_mode(eb, item);
426 if (S_ISDIR(mode))
427 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000428 }
429 } else if (inode_item) {
430 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400431 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000432
433 /*
434 * New inode, set nbytes to 0 so that the nbytes comes out
435 * properly when we replay the extents.
436 */
437 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
438 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400439
440 /*
441 * If this is a directory we need to reset the i_size to 0 so
442 * that we can set it up properly when replaying the rest of
443 * the items in this log.
444 */
445 mode = btrfs_inode_mode(eb, item);
446 if (S_ISDIR(mode))
447 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400448 }
449insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200450 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400451 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000452 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400453 ret = btrfs_insert_empty_item(trans, root, path,
454 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000455 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400456
457 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000458 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400459 u32 found_size;
460 found_size = btrfs_item_size_nr(path->nodes[0],
461 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100462 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000463 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100464 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000465 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100466 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400467 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400468 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400469 }
470 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
471 path->slots[0]);
472
473 /* don't overwrite an existing inode if the generation number
474 * was logged as zero. This is done when the tree logging code
475 * is just logging an inode to make sure it exists after recovery.
476 *
477 * Also, don't overwrite i_size on directories during replay.
478 * log replay inserts and removes directory items based on the
479 * state of the tree found in the subvolume, and i_size is modified
480 * as it goes
481 */
482 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
483 struct btrfs_inode_item *src_item;
484 struct btrfs_inode_item *dst_item;
485
486 src_item = (struct btrfs_inode_item *)src_ptr;
487 dst_item = (struct btrfs_inode_item *)dst_ptr;
488
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000489 if (btrfs_inode_generation(eb, src_item) == 0) {
490 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000491 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000493 /*
494 * For regular files an ino_size == 0 is used only when
495 * logging that an inode exists, as part of a directory
496 * fsync, and the inode wasn't fsynced before. In this
497 * case don't set the size of the inode in the fs/subvol
498 * tree, otherwise we would be throwing valid data away.
499 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000500 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000501 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
502 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000503 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000504
505 btrfs_init_map_token(&token);
506 btrfs_set_token_inode_size(dst_eb, dst_item,
507 ino_size, &token);
508 }
Chris Masone02119d2008-09-05 16:13:11 -0400509 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000510 }
Chris Masone02119d2008-09-05 16:13:11 -0400511
512 if (overwrite_root &&
513 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
514 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
515 save_old_i_size = 1;
516 saved_i_size = btrfs_inode_size(path->nodes[0],
517 dst_item);
518 }
519 }
520
521 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
522 src_ptr, item_size);
523
524 if (save_old_i_size) {
525 struct btrfs_inode_item *dst_item;
526 dst_item = (struct btrfs_inode_item *)dst_ptr;
527 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
528 }
529
530 /* make sure the generation is filled in */
531 if (key->type == BTRFS_INODE_ITEM_KEY) {
532 struct btrfs_inode_item *dst_item;
533 dst_item = (struct btrfs_inode_item *)dst_ptr;
534 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
535 btrfs_set_inode_generation(path->nodes[0], dst_item,
536 trans->transid);
537 }
538 }
539no_copy:
540 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200541 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400542 return 0;
543}
544
545/*
546 * simple helper to read an inode off the disk from a given root
547 * This can only be called for subvolume roots and not for the log
548 */
549static noinline struct inode *read_one_inode(struct btrfs_root *root,
550 u64 objectid)
551{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400552 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400553 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400554
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400555 key.objectid = objectid;
556 key.type = BTRFS_INODE_ITEM_KEY;
557 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000558 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400559 if (IS_ERR(inode)) {
560 inode = NULL;
561 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400562 iput(inode);
563 inode = NULL;
564 }
565 return inode;
566}
567
568/* replays a single extent in 'eb' at 'slot' with 'key' into the
569 * subvolume 'root'. path is released on entry and should be released
570 * on exit.
571 *
572 * extents in the log tree have not been allocated out of the extent
573 * tree yet. So, this completes the allocation, taking a reference
574 * as required if the extent already exists or creating a new extent
575 * if it isn't in the extent allocation tree yet.
576 *
577 * The extent is inserted into the file, dropping any existing extents
578 * from the file that overlap the new one.
579 */
580static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
581 struct btrfs_root *root,
582 struct btrfs_path *path,
583 struct extent_buffer *eb, int slot,
584 struct btrfs_key *key)
585{
586 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400587 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400588 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000589 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400590 struct btrfs_file_extent_item *item;
591 struct inode *inode = NULL;
592 unsigned long size;
593 int ret = 0;
594
595 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
596 found_type = btrfs_file_extent_type(eb, item);
597
Yan Zhengd899e052008-10-30 14:25:28 -0400598 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000599 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
600 nbytes = btrfs_file_extent_num_bytes(eb, item);
601 extent_end = start + nbytes;
602
603 /*
604 * We don't add to the inodes nbytes if we are prealloc or a
605 * hole.
606 */
607 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
608 nbytes = 0;
609 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800610 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000611 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000612 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400613 } else {
614 ret = 0;
615 goto out;
616 }
617
618 inode = read_one_inode(root, key->objectid);
619 if (!inode) {
620 ret = -EIO;
621 goto out;
622 }
623
624 /*
625 * first check to see if we already have this extent in the
626 * file. This must be done before the btrfs_drop_extents run
627 * so we don't try to drop this extent.
628 */
Li Zefan33345d012011-04-20 10:31:50 +0800629 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400630 start, 0);
631
Yan Zhengd899e052008-10-30 14:25:28 -0400632 if (ret == 0 &&
633 (found_type == BTRFS_FILE_EXTENT_REG ||
634 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400635 struct btrfs_file_extent_item cmp1;
636 struct btrfs_file_extent_item cmp2;
637 struct btrfs_file_extent_item *existing;
638 struct extent_buffer *leaf;
639
640 leaf = path->nodes[0];
641 existing = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_file_extent_item);
643
644 read_extent_buffer(eb, &cmp1, (unsigned long)item,
645 sizeof(cmp1));
646 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
647 sizeof(cmp2));
648
649 /*
650 * we already have a pointer to this exact extent,
651 * we don't have to do anything
652 */
653 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200654 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400655 goto out;
656 }
657 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200658 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400659
660 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400661 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400662 if (ret)
663 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400664
Yan Zheng07d400a2009-01-06 11:42:00 -0500665 if (found_type == BTRFS_FILE_EXTENT_REG ||
666 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500668 unsigned long dest_offset;
669 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400670
Yan Zheng07d400a2009-01-06 11:42:00 -0500671 ret = btrfs_insert_empty_item(trans, root, path, key,
672 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400673 if (ret)
674 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500675 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
676 path->slots[0]);
677 copy_extent_buffer(path->nodes[0], eb, dest_offset,
678 (unsigned long)item, sizeof(*item));
679
680 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
681 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
682 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500684
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800685 /*
686 * Manually record dirty extent, as here we did a shallow
687 * file extent item copy and skip normal backref update,
688 * but modifying extent tree all by ourselves.
689 * So need to manually record dirty extent for qgroup,
690 * as the owner of the file extent changed from log tree
691 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
692 */
693 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
694 btrfs_file_extent_disk_bytenr(eb, item),
695 btrfs_file_extent_disk_num_bytes(eb, item),
696 GFP_NOFS);
697 if (ret < 0)
698 goto out;
699
Yan Zheng07d400a2009-01-06 11:42:00 -0500700 if (ins.objectid > 0) {
701 u64 csum_start;
702 u64 csum_end;
703 LIST_HEAD(ordered_sums);
704 /*
705 * is this extent already allocated in the extent
706 * allocation tree? If so, just add a reference
707 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000708 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500709 ins.offset);
710 if (ret == 0) {
711 ret = btrfs_inc_extent_ref(trans, root,
712 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100714 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400715 if (ret)
716 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500717 } else {
718 /*
719 * insert the extent pointer in the extent
720 * allocation tree
721 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 ret = btrfs_alloc_logged_file_extent(trans,
723 root, root->root_key.objectid,
724 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400725 if (ret)
726 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500727 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200728 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500729
730 if (btrfs_file_extent_compression(eb, item)) {
731 csum_start = ins.objectid;
732 csum_end = csum_start + ins.offset;
733 } else {
734 csum_start = ins.objectid +
735 btrfs_file_extent_offset(eb, item);
736 csum_end = csum_start +
737 btrfs_file_extent_num_bytes(eb, item);
738 }
739
740 ret = btrfs_lookup_csums_range(root->log_root,
741 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100742 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400743 if (ret)
744 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100745 /*
746 * Now delete all existing cums in the csum root that
747 * cover our range. We do this because we can have an
748 * extent that is completely referenced by one file
749 * extent item and partially referenced by another
750 * file extent item (like after using the clone or
751 * extent_same ioctls). In this case if we end up doing
752 * the replay of the one that partially references the
753 * extent first, and we do not do the csum deletion
754 * below, we can get 2 csum items in the csum tree that
755 * overlap each other. For example, imagine our log has
756 * the two following file extent items:
757 *
758 * key (257 EXTENT_DATA 409600)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 20480 nr 20480 ram 102400
761 *
762 * key (257 EXTENT_DATA 819200)
763 * extent data disk byte 12845056 nr 102400
764 * extent data offset 0 nr 102400 ram 102400
765 *
766 * Where the second one fully references the 100K extent
767 * that starts at disk byte 12845056, and the log tree
768 * has a single csum item that covers the entire range
769 * of the extent:
770 *
771 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
772 *
773 * After the first file extent item is replayed, the
774 * csum tree gets the following csum item:
775 *
776 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
777 *
778 * Which covers the 20K sub-range starting at offset 20K
779 * of our extent. Now when we replay the second file
780 * extent item, if we do not delete existing csum items
781 * that cover any of its blocks, we end up getting two
782 * csum items in our csum tree that overlap each other:
783 *
784 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
785 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
786 *
787 * Which is a problem, because after this anyone trying
788 * to lookup up for the checksum of any block of our
789 * extent starting at an offset of 40K or higher, will
790 * end up looking at the second csum item only, which
791 * does not contain the checksum for any block starting
792 * at offset 40K or higher of our extent.
793 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500794 while (!list_empty(&ordered_sums)) {
795 struct btrfs_ordered_sum *sums;
796 sums = list_entry(ordered_sums.next,
797 struct btrfs_ordered_sum,
798 list);
Josef Bacik36508602013-04-25 16:23:32 -0400799 if (!ret)
Filipe Mananab84b8392015-08-19 11:09:40 +0100800 ret = btrfs_del_csums(trans,
801 root->fs_info->csum_root,
802 sums->bytenr,
803 sums->len);
804 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400805 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500806 root->fs_info->csum_root,
807 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500808 list_del(&sums->list);
809 kfree(sums);
810 }
Josef Bacik36508602013-04-25 16:23:32 -0400811 if (ret)
812 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500813 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200814 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500815 }
816 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
817 /* inline extents are easy, we just overwrite them */
818 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400819 if (ret)
820 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500821 }
822
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000823 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600824 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400825out:
826 if (inode)
827 iput(inode);
828 return ret;
829}
830
831/*
832 * when cleaning up conflicts between the directory names in the
833 * subvolume, directory names in the log and directory names in the
834 * inode back references, we may have to unlink inodes from directories.
835 *
836 * This is a helper function to do the unlink of a specific directory
837 * item
838 */
839static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
840 struct btrfs_root *root,
841 struct btrfs_path *path,
842 struct inode *dir,
843 struct btrfs_dir_item *di)
844{
845 struct inode *inode;
846 char *name;
847 int name_len;
848 struct extent_buffer *leaf;
849 struct btrfs_key location;
850 int ret;
851
852 leaf = path->nodes[0];
853
854 btrfs_dir_item_key_to_cpu(leaf, di, &location);
855 name_len = btrfs_dir_name_len(leaf, di);
856 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000857 if (!name)
858 return -ENOMEM;
859
Chris Masone02119d2008-09-05 16:13:11 -0400860 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200861 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400862
863 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000864 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400865 ret = -EIO;
866 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000867 }
Chris Masone02119d2008-09-05 16:13:11 -0400868
Yan Zhengec051c02009-01-05 15:43:42 -0500869 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400870 if (ret)
871 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400872
Chris Masone02119d2008-09-05 16:13:11 -0400873 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400874 if (ret)
875 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100876 else
877 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400878out:
879 kfree(name);
880 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400881 return ret;
882}
883
884/*
885 * helper function to see if a given name and sequence number found
886 * in an inode back reference are already in a directory and correctly
887 * point to this inode
888 */
889static noinline int inode_in_dir(struct btrfs_root *root,
890 struct btrfs_path *path,
891 u64 dirid, u64 objectid, u64 index,
892 const char *name, int name_len)
893{
894 struct btrfs_dir_item *di;
895 struct btrfs_key location;
896 int match = 0;
897
898 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
899 index, name, name_len, 0);
900 if (di && !IS_ERR(di)) {
901 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
902 if (location.objectid != objectid)
903 goto out;
904 } else
905 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200906 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400907
908 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
909 if (di && !IS_ERR(di)) {
910 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
911 if (location.objectid != objectid)
912 goto out;
913 } else
914 goto out;
915 match = 1;
916out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400918 return match;
919}
920
921/*
922 * helper function to check a log tree for a named back reference in
923 * an inode. This is used to decide if a back reference that is
924 * found in the subvolume conflicts with what we find in the log.
925 *
926 * inode backreferences may have multiple refs in a single item,
927 * during replay we process one reference at a time, and we don't
928 * want to delete valid links to a file from the subvolume if that
929 * link is also in the log.
930 */
931static noinline int backref_in_log(struct btrfs_root *log,
932 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700933 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000934 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400935{
936 struct btrfs_path *path;
937 struct btrfs_inode_ref *ref;
938 unsigned long ptr;
939 unsigned long ptr_end;
940 unsigned long name_ptr;
941 int found_name_len;
942 int item_size;
943 int ret;
944 int match = 0;
945
946 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000947 if (!path)
948 return -ENOMEM;
949
Chris Masone02119d2008-09-05 16:13:11 -0400950 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
951 if (ret != 0)
952 goto out;
953
Chris Masone02119d2008-09-05 16:13:11 -0400954 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700955
956 if (key->type == BTRFS_INODE_EXTREF_KEY) {
957 if (btrfs_find_name_in_ext_backref(path, 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]);
Chris Masone02119d2008-09-05 16:13:11 -0400965 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
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700985static 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 inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700990 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700991 u64 inode_objectid, u64 parent_objectid,
992 u64 ref_index, char *name, int namelen,
993 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700994{
995 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700996 char *victim_name;
997 int victim_name_len;
998 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700999 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001000 struct btrfs_key search_key;
1001 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001002
Mark Fashehf1863732012-08-08 11:32:27 -07001003again:
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);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001009 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 struct btrfs_inode_ref *victim_ref;
1011 unsigned long ptr;
1012 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001013
1014 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001015
1016 /* are we trying to overwrite a back ref for the root directory
1017 * if so, just jump out, we're done
1018 */
Mark Fashehf1863732012-08-08 11:32:27 -07001019 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001020 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);
Josef Bacik36508602013-04-25 16:23:32 -04001033 if (!victim_name)
1034 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001035
1036 read_extent_buffer(leaf, victim_name,
1037 (unsigned long)(victim_ref + 1),
1038 victim_name_len);
1039
Mark Fashehf1863732012-08-08 11:32:27 -07001040 if (!backref_in_log(log_root, &search_key,
1041 parent_objectid,
1042 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001043 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -07001044 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001045 btrfs_release_path(path);
1046
1047 ret = btrfs_unlink_inode(trans, root, dir,
1048 inode, victim_name,
1049 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001050 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001051 if (ret)
1052 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001053 ret = btrfs_run_delayed_items(trans, root);
1054 if (ret)
1055 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001056 *search_done = 1;
1057 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 }
1059 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001060
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001061 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001063
1064 /*
1065 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001066 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001067 */
1068 *search_done = 1;
1069 }
1070 btrfs_release_path(path);
1071
Mark Fashehf1863732012-08-08 11:32:27 -07001072 /* Same search but for extended refs */
1073 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1074 inode_objectid, parent_objectid, 0,
1075 0);
1076 if (!IS_ERR_OR_NULL(extref)) {
1077 u32 item_size;
1078 u32 cur_offset = 0;
1079 unsigned long base;
1080 struct inode *victim_parent;
1081
1082 leaf = path->nodes[0];
1083
1084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1085 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1086
1087 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001088 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001089
1090 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1091
1092 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1093 goto next;
1094
1095 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001096 if (!victim_name)
1097 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001098 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1099 victim_name_len);
1100
1101 search_key.objectid = inode_objectid;
1102 search_key.type = BTRFS_INODE_EXTREF_KEY;
1103 search_key.offset = btrfs_extref_hash(parent_objectid,
1104 victim_name,
1105 victim_name_len);
1106 ret = 0;
1107 if (!backref_in_log(log_root, &search_key,
1108 parent_objectid, victim_name,
1109 victim_name_len)) {
1110 ret = -ENOENT;
1111 victim_parent = read_one_inode(root,
1112 parent_objectid);
1113 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001114 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001115 btrfs_release_path(path);
1116
1117 ret = btrfs_unlink_inode(trans, root,
1118 victim_parent,
1119 inode,
1120 victim_name,
1121 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001122 if (!ret)
1123 ret = btrfs_run_delayed_items(
1124 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001125 }
Mark Fashehf1863732012-08-08 11:32:27 -07001126 iput(victim_parent);
1127 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001128 if (ret)
1129 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001130 *search_done = 1;
1131 goto again;
1132 }
1133 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001134 if (ret)
1135 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001136next:
1137 cur_offset += victim_name_len + sizeof(*extref);
1138 }
1139 *search_done = 1;
1140 }
1141 btrfs_release_path(path);
1142
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001143 /* look for a conflicting sequence number */
1144 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001145 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001146 if (di && !IS_ERR(di)) {
1147 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001148 if (ret)
1149 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 }
1151 btrfs_release_path(path);
1152
1153 /* look for a conflicing name */
1154 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1155 name, namelen, 0);
1156 if (di && !IS_ERR(di)) {
1157 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001158 if (ret)
1159 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001160 }
1161 btrfs_release_path(path);
1162
1163 return 0;
1164}
Chris Masone02119d2008-09-05 16:13:11 -04001165
Mark Fashehf1863732012-08-08 11:32:27 -07001166static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1167 u32 *namelen, char **name, u64 *index,
1168 u64 *parent_objectid)
1169{
1170 struct btrfs_inode_extref *extref;
1171
1172 extref = (struct btrfs_inode_extref *)ref_ptr;
1173
1174 *namelen = btrfs_inode_extref_name_len(eb, extref);
1175 *name = kmalloc(*namelen, GFP_NOFS);
1176 if (*name == NULL)
1177 return -ENOMEM;
1178
1179 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1180 *namelen);
1181
1182 *index = btrfs_inode_extref_index(eb, extref);
1183 if (parent_objectid)
1184 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1185
1186 return 0;
1187}
1188
1189static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1190 u32 *namelen, char **name, u64 *index)
1191{
1192 struct btrfs_inode_ref *ref;
1193
1194 ref = (struct btrfs_inode_ref *)ref_ptr;
1195
1196 *namelen = btrfs_inode_ref_name_len(eb, ref);
1197 *name = kmalloc(*namelen, GFP_NOFS);
1198 if (*name == NULL)
1199 return -ENOMEM;
1200
1201 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1202
1203 *index = btrfs_inode_ref_index(eb, ref);
1204
1205 return 0;
1206}
1207
Chris Masone02119d2008-09-05 16:13:11 -04001208/*
1209 * replay one inode back reference item found in the log tree.
1210 * eb, slot and key refer to the buffer and key found in the log tree.
1211 * root is the destination we are replaying into, and path is for temp
1212 * use by this function. (it should be released on return).
1213 */
1214static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_root *log,
1217 struct btrfs_path *path,
1218 struct extent_buffer *eb, int slot,
1219 struct btrfs_key *key)
1220{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001221 struct inode *dir = NULL;
1222 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001223 unsigned long ref_ptr;
1224 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001225 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001226 int namelen;
1227 int ret;
liuboc622ae62011-03-26 08:01:12 -04001228 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001229 int log_ref_ver = 0;
1230 u64 parent_objectid;
1231 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001232 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001233 int ref_struct_size;
1234
1235 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1236 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1237
1238 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1239 struct btrfs_inode_extref *r;
1240
1241 ref_struct_size = sizeof(struct btrfs_inode_extref);
1242 log_ref_ver = 1;
1243 r = (struct btrfs_inode_extref *)ref_ptr;
1244 parent_objectid = btrfs_inode_extref_parent(eb, r);
1245 } else {
1246 ref_struct_size = sizeof(struct btrfs_inode_ref);
1247 parent_objectid = key->offset;
1248 }
1249 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001250
Chris Masone02119d2008-09-05 16:13:11 -04001251 /*
1252 * it is possible that we didn't log all the parent directories
1253 * for a given inode. If we don't find the dir, just don't
1254 * copy the back ref in. The link count fixup code will take
1255 * care of the rest
1256 */
Mark Fashehf1863732012-08-08 11:32:27 -07001257 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001258 if (!dir) {
1259 ret = -ENOENT;
1260 goto out;
1261 }
Chris Masone02119d2008-09-05 16:13:11 -04001262
Mark Fashehf1863732012-08-08 11:32:27 -07001263 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001264 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001265 ret = -EIO;
1266 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001267 }
Chris Masone02119d2008-09-05 16:13:11 -04001268
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001269 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001270 if (log_ref_ver) {
1271 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1272 &ref_index, &parent_objectid);
1273 /*
1274 * parent object can change from one array
1275 * item to another.
1276 */
1277 if (!dir)
1278 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001279 if (!dir) {
1280 ret = -ENOENT;
1281 goto out;
1282 }
Mark Fashehf1863732012-08-08 11:32:27 -07001283 } else {
1284 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1285 &ref_index);
1286 }
1287 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001288 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001289
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001290 /* if we already have a perfect match, we're done */
1291 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001292 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001293 /*
1294 * look for a conflicting back reference in the
1295 * metadata. if we find one we have to unlink that name
1296 * of the file before we add our new link. Later on, we
1297 * overwrite any existing back reference, and we don't
1298 * want to create dangling pointers in the directory.
1299 */
Chris Masone02119d2008-09-05 16:13:11 -04001300
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001301 if (!search_done) {
1302 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001303 dir, inode, eb,
1304 inode_objectid,
1305 parent_objectid,
1306 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001307 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001308 if (ret) {
1309 if (ret == 1)
1310 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001311 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001312 }
Chris Masone02119d2008-09-05 16:13:11 -04001313 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001314
1315 /* insert our name */
1316 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001317 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001318 if (ret)
1319 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001320
1321 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001322 }
liuboc622ae62011-03-26 08:01:12 -04001323
Mark Fashehf1863732012-08-08 11:32:27 -07001324 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001325 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001326 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001327 if (log_ref_ver) {
1328 iput(dir);
1329 dir = NULL;
1330 }
Chris Masone02119d2008-09-05 16:13:11 -04001331 }
Chris Masone02119d2008-09-05 16:13:11 -04001332
1333 /* finally write the back reference in the inode */
1334 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001335out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001336 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001337 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001338 iput(dir);
1339 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001340 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001341}
1342
Yan, Zhengc71bf092009-11-12 09:34:40 +00001343static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001344 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001345{
1346 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001347
David Sterba9c4f61f2015-01-02 19:12:57 +01001348 ret = btrfs_insert_orphan_item(trans, root, ino);
1349 if (ret == -EEXIST)
1350 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001351
Yan, Zhengc71bf092009-11-12 09:34:40 +00001352 return ret;
1353}
1354
Mark Fashehf1863732012-08-08 11:32:27 -07001355static int count_inode_extrefs(struct btrfs_root *root,
1356 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001357{
Mark Fashehf1863732012-08-08 11:32:27 -07001358 int ret = 0;
1359 int name_len;
1360 unsigned int nlink = 0;
1361 u32 item_size;
1362 u32 cur_offset = 0;
1363 u64 inode_objectid = btrfs_ino(inode);
1364 u64 offset = 0;
1365 unsigned long ptr;
1366 struct btrfs_inode_extref *extref;
1367 struct extent_buffer *leaf;
1368
1369 while (1) {
1370 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1371 &extref, &offset);
1372 if (ret)
1373 break;
1374
1375 leaf = path->nodes[0];
1376 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1377 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001378 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001379
1380 while (cur_offset < item_size) {
1381 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1382 name_len = btrfs_inode_extref_name_len(leaf, extref);
1383
1384 nlink++;
1385
1386 cur_offset += name_len + sizeof(*extref);
1387 }
1388
1389 offset++;
1390 btrfs_release_path(path);
1391 }
1392 btrfs_release_path(path);
1393
Filipe Manana2c2c4522015-01-13 16:40:04 +00001394 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001395 return ret;
1396 return nlink;
1397}
1398
1399static int count_inode_refs(struct btrfs_root *root,
1400 struct inode *inode, struct btrfs_path *path)
1401{
Chris Masone02119d2008-09-05 16:13:11 -04001402 int ret;
1403 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001404 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001405 unsigned long ptr;
1406 unsigned long ptr_end;
1407 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001408 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001409
Li Zefan33345d012011-04-20 10:31:50 +08001410 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001411 key.type = BTRFS_INODE_REF_KEY;
1412 key.offset = (u64)-1;
1413
Chris Masond3977122009-01-05 21:25:51 -05001414 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001415 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1416 if (ret < 0)
1417 break;
1418 if (ret > 0) {
1419 if (path->slots[0] == 0)
1420 break;
1421 path->slots[0]--;
1422 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001423process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001424 btrfs_item_key_to_cpu(path->nodes[0], &key,
1425 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001426 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001427 key.type != BTRFS_INODE_REF_KEY)
1428 break;
1429 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1430 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1431 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001432 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001433 struct btrfs_inode_ref *ref;
1434
1435 ref = (struct btrfs_inode_ref *)ptr;
1436 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1437 ref);
1438 ptr = (unsigned long)(ref + 1) + name_len;
1439 nlink++;
1440 }
1441
1442 if (key.offset == 0)
1443 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001444 if (path->slots[0] > 0) {
1445 path->slots[0]--;
1446 goto process_slot;
1447 }
Chris Masone02119d2008-09-05 16:13:11 -04001448 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001449 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001452
1453 return nlink;
1454}
1455
1456/*
1457 * There are a few corners where the link count of the file can't
1458 * be properly maintained during replay. So, instead of adding
1459 * lots of complexity to the log code, we just scan the backrefs
1460 * for any file that has been through replay.
1461 *
1462 * The scan will update the link count on the inode to reflect the
1463 * number of back refs found. If it goes down to zero, the iput
1464 * will free the inode.
1465 */
1466static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1467 struct btrfs_root *root,
1468 struct inode *inode)
1469{
1470 struct btrfs_path *path;
1471 int ret;
1472 u64 nlink = 0;
1473 u64 ino = btrfs_ino(inode);
1474
1475 path = btrfs_alloc_path();
1476 if (!path)
1477 return -ENOMEM;
1478
1479 ret = count_inode_refs(root, inode, path);
1480 if (ret < 0)
1481 goto out;
1482
1483 nlink = ret;
1484
1485 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001486 if (ret < 0)
1487 goto out;
1488
1489 nlink += ret;
1490
1491 ret = 0;
1492
Chris Masone02119d2008-09-05 16:13:11 -04001493 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001494 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001495 btrfs_update_inode(trans, root, inode);
1496 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001497 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001498
Yan, Zhengc71bf092009-11-12 09:34:40 +00001499 if (inode->i_nlink == 0) {
1500 if (S_ISDIR(inode->i_mode)) {
1501 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001502 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001503 if (ret)
1504 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001505 }
Li Zefan33345d012011-04-20 10:31:50 +08001506 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001507 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001508
Mark Fashehf1863732012-08-08 11:32:27 -07001509out:
1510 btrfs_free_path(path);
1511 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001512}
1513
1514static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1515 struct btrfs_root *root,
1516 struct btrfs_path *path)
1517{
1518 int ret;
1519 struct btrfs_key key;
1520 struct inode *inode;
1521
1522 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1523 key.type = BTRFS_ORPHAN_ITEM_KEY;
1524 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001525 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 break;
1529
1530 if (ret == 1) {
1531 if (path->slots[0] == 0)
1532 break;
1533 path->slots[0]--;
1534 }
1535
1536 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1537 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1538 key.type != BTRFS_ORPHAN_ITEM_KEY)
1539 break;
1540
1541 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001542 if (ret)
1543 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001544
David Sterbab3b4aa72011-04-21 01:20:15 +02001545 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001546 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001547 if (!inode)
1548 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001549
1550 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001551 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001552 if (ret)
1553 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001554
Chris Mason12fcfd22009-03-24 10:24:20 -04001555 /*
1556 * fixup on a directory may create new entries,
1557 * make sure we always look for the highset possible
1558 * offset
1559 */
1560 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001561 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001562 ret = 0;
1563out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001564 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001565 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001566}
1567
1568
1569/*
1570 * record a given inode in the fixup dir so we can check its link
1571 * count when replay is done. The link count is incremented here
1572 * so the inode won't go away until we check it
1573 */
1574static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1575 struct btrfs_root *root,
1576 struct btrfs_path *path,
1577 u64 objectid)
1578{
1579 struct btrfs_key key;
1580 int ret = 0;
1581 struct inode *inode;
1582
1583 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001584 if (!inode)
1585 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001586
1587 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001588 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001589 key.offset = objectid;
1590
1591 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1592
David Sterbab3b4aa72011-04-21 01:20:15 +02001593 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001594 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001595 if (!inode->i_nlink)
1596 set_nlink(inode, 1);
1597 else
Zach Brown8b558c52013-10-16 12:10:34 -07001598 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001599 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001600 } else if (ret == -EEXIST) {
1601 ret = 0;
1602 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001603 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001604 }
1605 iput(inode);
1606
1607 return ret;
1608}
1609
1610/*
1611 * when replaying the log for a directory, we only insert names
1612 * for inodes that actually exist. This means an fsync on a directory
1613 * does not implicitly fsync all the new files in it
1614 */
1615static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001617 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001618 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001619 struct btrfs_key *location)
1620{
1621 struct inode *inode;
1622 struct inode *dir;
1623 int ret;
1624
1625 inode = read_one_inode(root, location->objectid);
1626 if (!inode)
1627 return -ENOENT;
1628
1629 dir = read_one_inode(root, dirid);
1630 if (!dir) {
1631 iput(inode);
1632 return -EIO;
1633 }
Josef Bacikd5554382013-09-11 14:17:00 -04001634
Chris Masone02119d2008-09-05 16:13:11 -04001635 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1636
1637 /* FIXME, put inode into FIXUP list */
1638
1639 iput(inode);
1640 iput(dir);
1641 return ret;
1642}
1643
1644/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001645 * Return true if an inode reference exists in the log for the given name,
1646 * inode and parent inode.
1647 */
1648static bool name_in_log_ref(struct btrfs_root *log_root,
1649 const char *name, const int name_len,
1650 const u64 dirid, const u64 ino)
1651{
1652 struct btrfs_key search_key;
1653
1654 search_key.objectid = ino;
1655 search_key.type = BTRFS_INODE_REF_KEY;
1656 search_key.offset = dirid;
1657 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1658 return true;
1659
1660 search_key.type = BTRFS_INODE_EXTREF_KEY;
1661 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1662 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1663 return true;
1664
1665 return false;
1666}
1667
1668/*
Chris Masone02119d2008-09-05 16:13:11 -04001669 * take a single entry in a log directory item and replay it into
1670 * the subvolume.
1671 *
1672 * if a conflicting item exists in the subdirectory already,
1673 * the inode it points to is unlinked and put into the link count
1674 * fix up tree.
1675 *
1676 * If a name from the log points to a file or directory that does
1677 * not exist in the FS, it is skipped. fsyncs on directories
1678 * do not force down inodes inside that directory, just changes to the
1679 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001680 *
1681 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1682 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001683 */
1684static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1685 struct btrfs_root *root,
1686 struct btrfs_path *path,
1687 struct extent_buffer *eb,
1688 struct btrfs_dir_item *di,
1689 struct btrfs_key *key)
1690{
1691 char *name;
1692 int name_len;
1693 struct btrfs_dir_item *dst_di;
1694 struct btrfs_key found_key;
1695 struct btrfs_key log_key;
1696 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001697 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001698 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001699 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001700 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001701 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001702
1703 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001704 if (!dir)
1705 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001706
1707 name_len = btrfs_dir_name_len(eb, di);
1708 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001709 if (!name) {
1710 ret = -ENOMEM;
1711 goto out;
1712 }
liubo2a29edc2011-01-26 06:22:08 +00001713
Chris Masone02119d2008-09-05 16:13:11 -04001714 log_type = btrfs_dir_type(eb, di);
1715 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1716 name_len);
1717
1718 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001719 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1720 if (exists == 0)
1721 exists = 1;
1722 else
1723 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001724 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001725
Chris Masone02119d2008-09-05 16:13:11 -04001726 if (key->type == BTRFS_DIR_ITEM_KEY) {
1727 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1728 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001729 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001730 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1731 key->objectid,
1732 key->offset, name,
1733 name_len, 1);
1734 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001735 /* Corruption */
1736 ret = -EINVAL;
1737 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001738 }
David Sterbac7040052011-04-19 18:00:01 +02001739 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001740 /* we need a sequence number to insert, so we only
1741 * do inserts for the BTRFS_DIR_INDEX_KEY types
1742 */
1743 if (key->type != BTRFS_DIR_INDEX_KEY)
1744 goto out;
1745 goto insert;
1746 }
1747
1748 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1749 /* the existing item matches the logged item */
1750 if (found_key.objectid == log_key.objectid &&
1751 found_key.type == log_key.type &&
1752 found_key.offset == log_key.offset &&
1753 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001754 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001755 goto out;
1756 }
1757
1758 /*
1759 * don't drop the conflicting directory entry if the inode
1760 * for the new entry doesn't exist
1761 */
Chris Mason4bef0842008-09-08 11:18:08 -04001762 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001763 goto out;
1764
Chris Masone02119d2008-09-05 16:13:11 -04001765 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001766 if (ret)
1767 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001768
1769 if (key->type == BTRFS_DIR_INDEX_KEY)
1770 goto insert;
1771out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001773 if (!ret && update_size) {
1774 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1775 ret = btrfs_update_inode(trans, root, dir);
1776 }
Chris Masone02119d2008-09-05 16:13:11 -04001777 kfree(name);
1778 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001779 if (!ret && name_added)
1780 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001781 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001782
1783insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001784 if (name_in_log_ref(root->log_root, name, name_len,
1785 key->objectid, log_key.objectid)) {
1786 /* The dentry will be added later. */
1787 ret = 0;
1788 update_size = false;
1789 goto out;
1790 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001791 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001792 ret = insert_one_name(trans, root, key->objectid, key->offset,
1793 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001794 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001795 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001796 if (!ret)
1797 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001798 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001799 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001800 goto out;
1801}
1802
1803/*
1804 * find all the names in a directory item and reconcile them into
1805 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1806 * one name in a directory item, but the same code gets used for
1807 * both directory index types
1808 */
1809static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1810 struct btrfs_root *root,
1811 struct btrfs_path *path,
1812 struct extent_buffer *eb, int slot,
1813 struct btrfs_key *key)
1814{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001815 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001816 u32 item_size = btrfs_item_size_nr(eb, slot);
1817 struct btrfs_dir_item *di;
1818 int name_len;
1819 unsigned long ptr;
1820 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001821 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001822
1823 ptr = btrfs_item_ptr_offset(eb, slot);
1824 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001825 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001826 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001827 if (verify_dir_item(root, eb, di))
1828 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001829 name_len = btrfs_dir_name_len(eb, di);
1830 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001831 if (ret < 0)
1832 break;
Chris Masone02119d2008-09-05 16:13:11 -04001833 ptr = (unsigned long)(di + 1);
1834 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001835
1836 /*
1837 * If this entry refers to a non-directory (directories can not
1838 * have a link count > 1) and it was added in the transaction
1839 * that was not committed, make sure we fixup the link count of
1840 * the inode it the entry points to. Otherwise something like
1841 * the following would result in a directory pointing to an
1842 * inode with a wrong link that does not account for this dir
1843 * entry:
1844 *
1845 * mkdir testdir
1846 * touch testdir/foo
1847 * touch testdir/bar
1848 * sync
1849 *
1850 * ln testdir/bar testdir/bar_link
1851 * ln testdir/foo testdir/foo_link
1852 * xfs_io -c "fsync" testdir/bar
1853 *
1854 * <power failure>
1855 *
1856 * mount fs, log replay happens
1857 *
1858 * File foo would remain with a link count of 1 when it has two
1859 * entries pointing to it in the directory testdir. This would
1860 * make it impossible to ever delete the parent directory has
1861 * it would result in stale dentries that can never be deleted.
1862 */
1863 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1864 struct btrfs_key di_key;
1865
1866 if (!fixup_path) {
1867 fixup_path = btrfs_alloc_path();
1868 if (!fixup_path) {
1869 ret = -ENOMEM;
1870 break;
1871 }
1872 }
1873
1874 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1875 ret = link_to_fixup_dir(trans, root, fixup_path,
1876 di_key.objectid);
1877 if (ret)
1878 break;
1879 }
1880 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001881 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001882 btrfs_free_path(fixup_path);
1883 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001884}
1885
1886/*
1887 * directory replay has two parts. There are the standard directory
1888 * items in the log copied from the subvolume, and range items
1889 * created in the log while the subvolume was logged.
1890 *
1891 * The range items tell us which parts of the key space the log
1892 * is authoritative for. During replay, if a key in the subvolume
1893 * directory is in a logged range item, but not actually in the log
1894 * that means it was deleted from the directory before the fsync
1895 * and should be removed.
1896 */
1897static noinline int find_dir_range(struct btrfs_root *root,
1898 struct btrfs_path *path,
1899 u64 dirid, int key_type,
1900 u64 *start_ret, u64 *end_ret)
1901{
1902 struct btrfs_key key;
1903 u64 found_end;
1904 struct btrfs_dir_log_item *item;
1905 int ret;
1906 int nritems;
1907
1908 if (*start_ret == (u64)-1)
1909 return 1;
1910
1911 key.objectid = dirid;
1912 key.type = key_type;
1913 key.offset = *start_ret;
1914
1915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1916 if (ret < 0)
1917 goto out;
1918 if (ret > 0) {
1919 if (path->slots[0] == 0)
1920 goto out;
1921 path->slots[0]--;
1922 }
1923 if (ret != 0)
1924 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1925
1926 if (key.type != key_type || key.objectid != dirid) {
1927 ret = 1;
1928 goto next;
1929 }
1930 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1931 struct btrfs_dir_log_item);
1932 found_end = btrfs_dir_log_end(path->nodes[0], item);
1933
1934 if (*start_ret >= key.offset && *start_ret <= found_end) {
1935 ret = 0;
1936 *start_ret = key.offset;
1937 *end_ret = found_end;
1938 goto out;
1939 }
1940 ret = 1;
1941next:
1942 /* check the next slot in the tree to see if it is a valid item */
1943 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko26dc5242016-10-07 17:30:47 +08001944 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04001945 if (path->slots[0] >= nritems) {
1946 ret = btrfs_next_leaf(root, path);
1947 if (ret)
1948 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001949 }
1950
1951 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1952
1953 if (key.type != key_type || key.objectid != dirid) {
1954 ret = 1;
1955 goto out;
1956 }
1957 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1958 struct btrfs_dir_log_item);
1959 found_end = btrfs_dir_log_end(path->nodes[0], item);
1960 *start_ret = key.offset;
1961 *end_ret = found_end;
1962 ret = 0;
1963out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001964 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001965 return ret;
1966}
1967
1968/*
1969 * this looks for a given directory item in the log. If the directory
1970 * item is not in the log, the item is removed and the inode it points
1971 * to is unlinked
1972 */
1973static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root,
1975 struct btrfs_root *log,
1976 struct btrfs_path *path,
1977 struct btrfs_path *log_path,
1978 struct inode *dir,
1979 struct btrfs_key *dir_key)
1980{
1981 int ret;
1982 struct extent_buffer *eb;
1983 int slot;
1984 u32 item_size;
1985 struct btrfs_dir_item *di;
1986 struct btrfs_dir_item *log_di;
1987 int name_len;
1988 unsigned long ptr;
1989 unsigned long ptr_end;
1990 char *name;
1991 struct inode *inode;
1992 struct btrfs_key location;
1993
1994again:
1995 eb = path->nodes[0];
1996 slot = path->slots[0];
1997 item_size = btrfs_item_size_nr(eb, slot);
1998 ptr = btrfs_item_ptr_offset(eb, slot);
1999 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002000 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002001 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04002002 if (verify_dir_item(root, eb, di)) {
2003 ret = -EIO;
2004 goto out;
2005 }
2006
Chris Masone02119d2008-09-05 16:13:11 -04002007 name_len = btrfs_dir_name_len(eb, di);
2008 name = kmalloc(name_len, GFP_NOFS);
2009 if (!name) {
2010 ret = -ENOMEM;
2011 goto out;
2012 }
2013 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2014 name_len);
2015 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002016 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002017 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2018 dir_key->objectid,
2019 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002020 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002021 log_di = btrfs_lookup_dir_index_item(trans, log,
2022 log_path,
2023 dir_key->objectid,
2024 dir_key->offset,
2025 name, name_len, 0);
2026 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002027 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002028 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002029 btrfs_release_path(path);
2030 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002031 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002032 if (!inode) {
2033 kfree(name);
2034 return -EIO;
2035 }
Chris Masone02119d2008-09-05 16:13:11 -04002036
2037 ret = link_to_fixup_dir(trans, root,
2038 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002039 if (ret) {
2040 kfree(name);
2041 iput(inode);
2042 goto out;
2043 }
2044
Zach Brown8b558c52013-10-16 12:10:34 -07002045 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002046 ret = btrfs_unlink_inode(trans, root, dir, inode,
2047 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002048 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01002049 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002050 kfree(name);
2051 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002052 if (ret)
2053 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002054
2055 /* there might still be more names under this key
2056 * check and repeat if required
2057 */
2058 ret = btrfs_search_slot(NULL, root, dir_key, path,
2059 0, 0);
2060 if (ret == 0)
2061 goto again;
2062 ret = 0;
2063 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002064 } else if (IS_ERR(log_di)) {
2065 kfree(name);
2066 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002067 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002068 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002069 kfree(name);
2070
2071 ptr = (unsigned long)(di + 1);
2072 ptr += name_len;
2073 }
2074 ret = 0;
2075out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002076 btrfs_release_path(path);
2077 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002078 return ret;
2079}
2080
Filipe Manana4f764e52015-02-23 19:53:35 +00002081static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2082 struct btrfs_root *root,
2083 struct btrfs_root *log,
2084 struct btrfs_path *path,
2085 const u64 ino)
2086{
2087 struct btrfs_key search_key;
2088 struct btrfs_path *log_path;
2089 int i;
2090 int nritems;
2091 int ret;
2092
2093 log_path = btrfs_alloc_path();
2094 if (!log_path)
2095 return -ENOMEM;
2096
2097 search_key.objectid = ino;
2098 search_key.type = BTRFS_XATTR_ITEM_KEY;
2099 search_key.offset = 0;
2100again:
2101 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2102 if (ret < 0)
2103 goto out;
2104process_leaf:
2105 nritems = btrfs_header_nritems(path->nodes[0]);
2106 for (i = path->slots[0]; i < nritems; i++) {
2107 struct btrfs_key key;
2108 struct btrfs_dir_item *di;
2109 struct btrfs_dir_item *log_di;
2110 u32 total_size;
2111 u32 cur;
2112
2113 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2114 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2115 ret = 0;
2116 goto out;
2117 }
2118
2119 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2120 total_size = btrfs_item_size_nr(path->nodes[0], i);
2121 cur = 0;
2122 while (cur < total_size) {
2123 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2124 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2125 u32 this_len = sizeof(*di) + name_len + data_len;
2126 char *name;
2127
2128 name = kmalloc(name_len, GFP_NOFS);
2129 if (!name) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
2133 read_extent_buffer(path->nodes[0], name,
2134 (unsigned long)(di + 1), name_len);
2135
2136 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2137 name, name_len, 0);
2138 btrfs_release_path(log_path);
2139 if (!log_di) {
2140 /* Doesn't exist in log tree, so delete it. */
2141 btrfs_release_path(path);
2142 di = btrfs_lookup_xattr(trans, root, path, ino,
2143 name, name_len, -1);
2144 kfree(name);
2145 if (IS_ERR(di)) {
2146 ret = PTR_ERR(di);
2147 goto out;
2148 }
2149 ASSERT(di);
2150 ret = btrfs_delete_one_dir_name(trans, root,
2151 path, di);
2152 if (ret)
2153 goto out;
2154 btrfs_release_path(path);
2155 search_key = key;
2156 goto again;
2157 }
2158 kfree(name);
2159 if (IS_ERR(log_di)) {
2160 ret = PTR_ERR(log_di);
2161 goto out;
2162 }
2163 cur += this_len;
2164 di = (struct btrfs_dir_item *)((char *)di + this_len);
2165 }
2166 }
2167 ret = btrfs_next_leaf(root, path);
2168 if (ret > 0)
2169 ret = 0;
2170 else if (ret == 0)
2171 goto process_leaf;
2172out:
2173 btrfs_free_path(log_path);
2174 btrfs_release_path(path);
2175 return ret;
2176}
2177
2178
Chris Masone02119d2008-09-05 16:13:11 -04002179/*
2180 * deletion replay happens before we copy any new directory items
2181 * out of the log or out of backreferences from inodes. It
2182 * scans the log to find ranges of keys that log is authoritative for,
2183 * and then scans the directory to find items in those ranges that are
2184 * not present in the log.
2185 *
2186 * Anything we don't find in the log is unlinked and removed from the
2187 * directory.
2188 */
2189static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct btrfs_root *log,
2192 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002193 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002194{
2195 u64 range_start;
2196 u64 range_end;
2197 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2198 int ret = 0;
2199 struct btrfs_key dir_key;
2200 struct btrfs_key found_key;
2201 struct btrfs_path *log_path;
2202 struct inode *dir;
2203
2204 dir_key.objectid = dirid;
2205 dir_key.type = BTRFS_DIR_ITEM_KEY;
2206 log_path = btrfs_alloc_path();
2207 if (!log_path)
2208 return -ENOMEM;
2209
2210 dir = read_one_inode(root, dirid);
2211 /* it isn't an error if the inode isn't there, that can happen
2212 * because we replay the deletes before we copy in the inode item
2213 * from the log
2214 */
2215 if (!dir) {
2216 btrfs_free_path(log_path);
2217 return 0;
2218 }
2219again:
2220 range_start = 0;
2221 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002222 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002223 if (del_all)
2224 range_end = (u64)-1;
2225 else {
2226 ret = find_dir_range(log, path, dirid, key_type,
2227 &range_start, &range_end);
2228 if (ret != 0)
2229 break;
2230 }
Chris Masone02119d2008-09-05 16:13:11 -04002231
2232 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002233 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002234 int nritems;
2235 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2236 0, 0);
2237 if (ret < 0)
2238 goto out;
2239
2240 nritems = btrfs_header_nritems(path->nodes[0]);
2241 if (path->slots[0] >= nritems) {
2242 ret = btrfs_next_leaf(root, path);
2243 if (ret)
2244 break;
2245 }
2246 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2247 path->slots[0]);
2248 if (found_key.objectid != dirid ||
2249 found_key.type != dir_key.type)
2250 goto next_type;
2251
2252 if (found_key.offset > range_end)
2253 break;
2254
2255 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002256 log_path, dir,
2257 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002258 if (ret)
2259 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002260 if (found_key.offset == (u64)-1)
2261 break;
2262 dir_key.offset = found_key.offset + 1;
2263 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002264 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002265 if (range_end == (u64)-1)
2266 break;
2267 range_start = range_end + 1;
2268 }
2269
2270next_type:
2271 ret = 0;
2272 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2273 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2274 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002275 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002276 goto again;
2277 }
2278out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002279 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002280 btrfs_free_path(log_path);
2281 iput(dir);
2282 return ret;
2283}
2284
2285/*
2286 * the process_func used to replay items from the log tree. This
2287 * gets called in two different stages. The first stage just looks
2288 * for inodes and makes sure they are all copied into the subvolume.
2289 *
2290 * The second stage copies all the other item types from the log into
2291 * the subvolume. The two stage approach is slower, but gets rid of
2292 * lots of complexity around inodes referencing other inodes that exist
2293 * only in the log (references come from either directory items or inode
2294 * back refs).
2295 */
2296static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2297 struct walk_control *wc, u64 gen)
2298{
2299 int nritems;
2300 struct btrfs_path *path;
2301 struct btrfs_root *root = wc->replay_dest;
2302 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002303 int level;
2304 int i;
2305 int ret;
2306
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002307 ret = btrfs_read_buffer(eb, gen);
2308 if (ret)
2309 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002310
2311 level = btrfs_header_level(eb);
2312
2313 if (level != 0)
2314 return 0;
2315
2316 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002317 if (!path)
2318 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002319
2320 nritems = btrfs_header_nritems(eb);
2321 for (i = 0; i < nritems; i++) {
2322 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002323
2324 /* inode keys are done during the first stage */
2325 if (key.type == BTRFS_INODE_ITEM_KEY &&
2326 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002327 struct btrfs_inode_item *inode_item;
2328 u32 mode;
2329
2330 inode_item = btrfs_item_ptr(eb, i,
2331 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002332 ret = replay_xattr_deletes(wc->trans, root, log,
2333 path, key.objectid);
2334 if (ret)
2335 break;
Chris Masone02119d2008-09-05 16:13:11 -04002336 mode = btrfs_inode_mode(eb, inode_item);
2337 if (S_ISDIR(mode)) {
2338 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002339 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002340 if (ret)
2341 break;
Chris Masone02119d2008-09-05 16:13:11 -04002342 }
2343 ret = overwrite_item(wc->trans, root, path,
2344 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002345 if (ret)
2346 break;
Chris Masone02119d2008-09-05 16:13:11 -04002347
Yan, Zhengc71bf092009-11-12 09:34:40 +00002348 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002349 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002350 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002351 */
2352 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002353 ret = insert_orphan_item(wc->trans, root,
2354 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002355 if (ret)
2356 break;
Chris Masone02119d2008-09-05 16:13:11 -04002357 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002358
Chris Masone02119d2008-09-05 16:13:11 -04002359 ret = link_to_fixup_dir(wc->trans, root,
2360 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002361 if (ret)
2362 break;
Chris Masone02119d2008-09-05 16:13:11 -04002363 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002364
2365 if (key.type == BTRFS_DIR_INDEX_KEY &&
2366 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2367 ret = replay_one_dir_item(wc->trans, root, path,
2368 eb, i, &key);
2369 if (ret)
2370 break;
2371 }
2372
Chris Masone02119d2008-09-05 16:13:11 -04002373 if (wc->stage < LOG_WALK_REPLAY_ALL)
2374 continue;
2375
2376 /* these keys are simply copied */
2377 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2378 ret = overwrite_item(wc->trans, root, path,
2379 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002380 if (ret)
2381 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002382 } else if (key.type == BTRFS_INODE_REF_KEY ||
2383 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002384 ret = add_inode_ref(wc->trans, root, log, path,
2385 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002386 if (ret && ret != -ENOENT)
2387 break;
2388 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002389 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2390 ret = replay_one_extent(wc->trans, root, path,
2391 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002392 if (ret)
2393 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002394 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002395 ret = replay_one_dir_item(wc->trans, root, path,
2396 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002397 if (ret)
2398 break;
Chris Masone02119d2008-09-05 16:13:11 -04002399 }
2400 }
2401 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002402 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002403}
2404
Chris Masond3977122009-01-05 21:25:51 -05002405static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002406 struct btrfs_root *root,
2407 struct btrfs_path *path, int *level,
2408 struct walk_control *wc)
2409{
2410 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002411 u64 bytenr;
2412 u64 ptr_gen;
2413 struct extent_buffer *next;
2414 struct extent_buffer *cur;
2415 struct extent_buffer *parent;
2416 u32 blocksize;
2417 int ret = 0;
2418
2419 WARN_ON(*level < 0);
2420 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2421
Chris Masond3977122009-01-05 21:25:51 -05002422 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002423 WARN_ON(*level < 0);
2424 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2425 cur = path->nodes[*level];
2426
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302427 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002428
2429 if (path->slots[*level] >=
2430 btrfs_header_nritems(cur))
2431 break;
2432
2433 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2434 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002435 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002436
2437 parent = path->nodes[*level];
2438 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002439
David Sterbaa83fffb2014-06-15 02:39:54 +02002440 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002441 if (IS_ERR(next))
2442 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002443
Chris Masone02119d2008-09-05 16:13:11 -04002444 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002445 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002446 if (ret) {
2447 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002448 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002449 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002450
Chris Masone02119d2008-09-05 16:13:11 -04002451 path->slots[*level]++;
2452 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002453 ret = btrfs_read_buffer(next, ptr_gen);
2454 if (ret) {
2455 free_extent_buffer(next);
2456 return ret;
2457 }
Chris Masone02119d2008-09-05 16:13:11 -04002458
Josef Bacik681ae502013-10-07 15:11:00 -04002459 if (trans) {
2460 btrfs_tree_lock(next);
2461 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002462 clean_tree_block(trans, root->fs_info,
2463 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002464 btrfs_wait_tree_block_writeback(next);
2465 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002466 } else {
2467 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2468 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002469 }
Chris Masone02119d2008-09-05 16:13:11 -04002470
Chris Masone02119d2008-09-05 16:13:11 -04002471 WARN_ON(root_owner !=
2472 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002473 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002474 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002475 if (ret) {
2476 free_extent_buffer(next);
2477 return ret;
2478 }
Chris Masone02119d2008-09-05 16:13:11 -04002479 }
2480 free_extent_buffer(next);
2481 continue;
2482 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002483 ret = btrfs_read_buffer(next, ptr_gen);
2484 if (ret) {
2485 free_extent_buffer(next);
2486 return ret;
2487 }
Chris Masone02119d2008-09-05 16:13:11 -04002488
2489 WARN_ON(*level <= 0);
2490 if (path->nodes[*level-1])
2491 free_extent_buffer(path->nodes[*level-1]);
2492 path->nodes[*level-1] = next;
2493 *level = btrfs_header_level(next);
2494 path->slots[*level] = 0;
2495 cond_resched();
2496 }
2497 WARN_ON(*level < 0);
2498 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2499
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002500 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002501
2502 cond_resched();
2503 return 0;
2504}
2505
Chris Masond3977122009-01-05 21:25:51 -05002506static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002507 struct btrfs_root *root,
2508 struct btrfs_path *path, int *level,
2509 struct walk_control *wc)
2510{
2511 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002512 int i;
2513 int slot;
2514 int ret;
2515
Chris Masond3977122009-01-05 21:25:51 -05002516 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002517 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002518 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002519 path->slots[i]++;
2520 *level = i;
2521 WARN_ON(*level == 0);
2522 return 0;
2523 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002524 struct extent_buffer *parent;
2525 if (path->nodes[*level] == root->node)
2526 parent = path->nodes[*level];
2527 else
2528 parent = path->nodes[*level + 1];
2529
2530 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002531 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002532 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002533 if (ret)
2534 return ret;
2535
Chris Masone02119d2008-09-05 16:13:11 -04002536 if (wc->free) {
2537 struct extent_buffer *next;
2538
2539 next = path->nodes[*level];
2540
Josef Bacik681ae502013-10-07 15:11:00 -04002541 if (trans) {
2542 btrfs_tree_lock(next);
2543 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002544 clean_tree_block(trans, root->fs_info,
2545 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002546 btrfs_wait_tree_block_writeback(next);
2547 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002548 } else {
2549 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2550 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002551 }
Chris Masone02119d2008-09-05 16:13:11 -04002552
Chris Masone02119d2008-09-05 16:13:11 -04002553 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002554 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002555 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002556 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002557 if (ret)
2558 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002559 }
2560 free_extent_buffer(path->nodes[*level]);
2561 path->nodes[*level] = NULL;
2562 *level = i + 1;
2563 }
2564 }
2565 return 1;
2566}
2567
2568/*
2569 * drop the reference count on the tree rooted at 'snap'. This traverses
2570 * the tree freeing any blocks that have a ref count of zero after being
2571 * decremented.
2572 */
2573static int walk_log_tree(struct btrfs_trans_handle *trans,
2574 struct btrfs_root *log, struct walk_control *wc)
2575{
2576 int ret = 0;
2577 int wret;
2578 int level;
2579 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002580 int orig_level;
2581
2582 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002583 if (!path)
2584 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002585
2586 level = btrfs_header_level(log->node);
2587 orig_level = level;
2588 path->nodes[level] = log->node;
2589 extent_buffer_get(log->node);
2590 path->slots[level] = 0;
2591
Chris Masond3977122009-01-05 21:25:51 -05002592 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002593 wret = walk_down_log_tree(trans, log, path, &level, wc);
2594 if (wret > 0)
2595 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002596 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002597 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002598 goto out;
2599 }
Chris Masone02119d2008-09-05 16:13:11 -04002600
2601 wret = walk_up_log_tree(trans, log, path, &level, wc);
2602 if (wret > 0)
2603 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002604 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002605 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002606 goto out;
2607 }
Chris Masone02119d2008-09-05 16:13:11 -04002608 }
2609
2610 /* was the root node processed? if not, catch it here */
2611 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002612 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002613 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002614 if (ret)
2615 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002616 if (wc->free) {
2617 struct extent_buffer *next;
2618
2619 next = path->nodes[orig_level];
2620
Josef Bacik681ae502013-10-07 15:11:00 -04002621 if (trans) {
2622 btrfs_tree_lock(next);
2623 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002624 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002625 btrfs_wait_tree_block_writeback(next);
2626 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002627 } else {
2628 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2629 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002630 }
Chris Masone02119d2008-09-05 16:13:11 -04002631
Chris Masone02119d2008-09-05 16:13:11 -04002632 WARN_ON(log->root_key.objectid !=
2633 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002634 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002635 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002636 if (ret)
2637 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002638 }
2639 }
2640
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002641out:
Chris Masone02119d2008-09-05 16:13:11 -04002642 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002643 return ret;
2644}
2645
Yan Zheng7237f182009-01-21 12:54:03 -05002646/*
2647 * helper function to update the item for a given subvolumes log root
2648 * in the tree of log roots
2649 */
2650static int update_log_root(struct btrfs_trans_handle *trans,
2651 struct btrfs_root *log)
2652{
2653 int ret;
2654
2655 if (log->log_transid == 1) {
2656 /* insert root item on the first sync */
2657 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2658 &log->root_key, &log->root_item);
2659 } else {
2660 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2661 &log->root_key, &log->root_item);
2662 }
2663 return ret;
2664}
2665
Zhaolei60d53eb2015-08-17 18:44:46 +08002666static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002667{
2668 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002669 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002670
Yan Zheng7237f182009-01-21 12:54:03 -05002671 /*
2672 * we only allow two pending log transactions at a time,
2673 * so we know that if ours is more than 2 older than the
2674 * current transaction, we're done
2675 */
Chris Masone02119d2008-09-05 16:13:11 -04002676 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002677 prepare_to_wait(&root->log_commit_wait[index],
2678 &wait, TASK_UNINTERRUPTIBLE);
2679 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002680
Miao Xied1433de2014-02-20 18:08:59 +08002681 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002682 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002683 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002684
Yan Zheng7237f182009-01-21 12:54:03 -05002685 finish_wait(&root->log_commit_wait[index], &wait);
2686 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002687 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002688 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002689}
2690
Zhaolei60d53eb2015-08-17 18:44:46 +08002691static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002692{
2693 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002694
2695 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002696 prepare_to_wait(&root->log_writer_wait,
2697 &wait, TASK_UNINTERRUPTIBLE);
2698 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002699 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002700 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002701 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002702 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002703 }
Chris Masone02119d2008-09-05 16:13:11 -04002704}
2705
Miao Xie8b050d32014-02-20 18:08:58 +08002706static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2707 struct btrfs_log_ctx *ctx)
2708{
2709 if (!ctx)
2710 return;
2711
2712 mutex_lock(&root->log_mutex);
2713 list_del_init(&ctx->list);
2714 mutex_unlock(&root->log_mutex);
2715}
2716
2717/*
2718 * Invoked in log mutex context, or be sure there is no other task which
2719 * can access the list.
2720 */
2721static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2722 int index, int error)
2723{
2724 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002725 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002726
Chris Mason570dd452016-10-27 10:42:20 -07002727 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2728 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002729 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002730 }
Miao Xie8b050d32014-02-20 18:08:58 +08002731
2732 INIT_LIST_HEAD(&root->log_ctxs[index]);
2733}
2734
Chris Masone02119d2008-09-05 16:13:11 -04002735/*
2736 * btrfs_sync_log does sends a given tree log down to the disk and
2737 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002738 * you know that any inodes previously logged are safely on disk only
2739 * if it returns 0.
2740 *
2741 * Any other return value means you need to call btrfs_commit_transaction.
2742 * Some of the edge cases for fsyncing directories that have had unlinks
2743 * or renames done in the past mean that sometimes the only safe
2744 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2745 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002746 */
2747int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002748 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002749{
Yan Zheng7237f182009-01-21 12:54:03 -05002750 int index1;
2751 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002752 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002753 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002754 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002755 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002756 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002757 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002758 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002759
Yan Zheng7237f182009-01-21 12:54:03 -05002760 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002761 log_transid = ctx->log_transid;
2762 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002763 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002764 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002765 }
Miao Xied1433de2014-02-20 18:08:59 +08002766
2767 index1 = log_transid % 2;
2768 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002769 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002770 mutex_unlock(&root->log_mutex);
2771 return ctx->log_ret;
2772 }
2773 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002774 atomic_set(&root->log_commit[index1], 1);
2775
2776 /* wait for previous tree log sync to complete */
2777 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002778 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002779
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002780 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002781 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002782 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002783 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002784 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002785 mutex_unlock(&root->log_mutex);
2786 schedule_timeout_uninterruptible(1);
2787 mutex_lock(&root->log_mutex);
2788 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002789 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002790 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002791 break;
2792 }
Chris Masond0c803c2008-09-11 16:17:57 -04002793
Chris Mason12fcfd22009-03-24 10:24:20 -04002794 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002795 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002796 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002797 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002798 mutex_unlock(&root->log_mutex);
2799 goto out;
2800 }
2801
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002802 if (log_transid % 2 == 0)
2803 mark = EXTENT_DIRTY;
2804 else
2805 mark = EXTENT_NEW;
2806
Chris Mason690587d2009-10-13 13:29:19 -04002807 /* we start IO on all the marked extents here, but we don't actually
2808 * wait for them until later.
2809 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002810 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002811 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002812 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002813 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002814 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002815 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002816 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 mutex_unlock(&root->log_mutex);
2818 goto out;
2819 }
Yan Zheng7237f182009-01-21 12:54:03 -05002820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002821 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002822
Yan Zheng7237f182009-01-21 12:54:03 -05002823 root->log_transid++;
2824 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002825 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002826 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002827 * IO has been started, blocks of the log tree have WRITTEN flag set
2828 * in their headers. new modifications of the log will be written to
2829 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002830 */
2831 mutex_unlock(&root->log_mutex);
2832
Filipe Manana28a23592016-08-23 21:13:51 +01002833 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002834
Yan Zheng7237f182009-01-21 12:54:03 -05002835 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002836 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002837 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002838
2839 index2 = log_root_tree->log_transid % 2;
2840 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2841 root_log_ctx.log_transid = log_root_tree->log_transid;
2842
Yan Zheng7237f182009-01-21 12:54:03 -05002843 mutex_unlock(&log_root_tree->log_mutex);
2844
2845 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002846
2847 mutex_lock(&log_root_tree->log_mutex);
2848 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002849 /*
2850 * Implicit memory barrier after atomic_dec_and_test
2851 */
Yan Zheng7237f182009-01-21 12:54:03 -05002852 if (waitqueue_active(&log_root_tree->log_writer_wait))
2853 wake_up(&log_root_tree->log_writer_wait);
2854 }
2855
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002856 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002857 if (!list_empty(&root_log_ctx.list))
2858 list_del_init(&root_log_ctx.list);
2859
Miao Xiec6adc9c2013-05-28 10:05:39 +00002860 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002861 btrfs_set_log_full_commit(root->fs_info, trans);
2862
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002865 mutex_unlock(&log_root_tree->log_mutex);
2866 goto out;
2867 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002868 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002869 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002870 mutex_unlock(&log_root_tree->log_mutex);
2871 ret = -EAGAIN;
2872 goto out;
2873 }
2874
Miao Xied1433de2014-02-20 18:08:59 +08002875 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002876 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002877 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002878 mutex_unlock(&log_root_tree->log_mutex);
2879 ret = root_log_ctx.log_ret;
2880 goto out;
2881 }
Miao Xie8b050d32014-02-20 18:08:58 +08002882
Miao Xied1433de2014-02-20 18:08:59 +08002883 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002884 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002885 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002886 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2887 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002888 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002889 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002890 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002891 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002892 if (!ret)
2893 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002894 goto out;
2895 }
Miao Xied1433de2014-02-20 18:08:59 +08002896 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002897 atomic_set(&log_root_tree->log_commit[index2], 1);
2898
Chris Mason12fcfd22009-03-24 10:24:20 -04002899 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002900 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002901 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002902 }
Yan Zheng7237f182009-01-21 12:54:03 -05002903
Zhaolei60d53eb2015-08-17 18:44:46 +08002904 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002905
2906 /*
2907 * now that we've moved on to the tree of log tree roots,
2908 * check the full commit flag again
2909 */
Miao Xie995946d2014-04-02 19:51:06 +08002910 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002911 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002912 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002913 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002914 mutex_unlock(&log_root_tree->log_mutex);
2915 ret = -EAGAIN;
2916 goto out_wake_log_root;
2917 }
Yan Zheng7237f182009-01-21 12:54:03 -05002918
Miao Xiec6adc9c2013-05-28 10:05:39 +00002919 ret = btrfs_write_marked_extents(log_root_tree,
2920 &log_root_tree->dirty_log_pages,
2921 EXTENT_DIRTY | EXTENT_NEW);
2922 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002923 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002924 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002925 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002926 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927 mutex_unlock(&log_root_tree->log_mutex);
2928 goto out_wake_log_root;
2929 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002930 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2931 if (!ret)
2932 ret = btrfs_wait_marked_extents(log_root_tree,
2933 &log_root_tree->dirty_log_pages,
2934 EXTENT_NEW | EXTENT_DIRTY);
2935 if (ret) {
2936 btrfs_set_log_full_commit(root->fs_info, trans);
2937 btrfs_free_logged_extents(log, log_transid);
2938 mutex_unlock(&log_root_tree->log_mutex);
2939 goto out_wake_log_root;
2940 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002941 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002942
David Sterba6c417612011-04-13 15:41:04 +02002943 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002944 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002945 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002946 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002947
Yan Zheng7237f182009-01-21 12:54:03 -05002948 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002949 mutex_unlock(&log_root_tree->log_mutex);
2950
2951 /*
2952 * nobody else is going to jump in and write the the ctree
2953 * super here because the log_commit atomic below is protecting
2954 * us. We must be called with a transaction handle pinning
2955 * the running transaction open, so a full commit can't hop
2956 * in and cause problems either.
2957 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002958 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002959 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002960 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002961 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002962 goto out_wake_log_root;
2963 }
Yan Zheng7237f182009-01-21 12:54:03 -05002964
Chris Mason257c62e2009-10-13 13:21:08 -04002965 mutex_lock(&root->log_mutex);
2966 if (root->last_log_commit < log_transid)
2967 root->last_log_commit = log_transid;
2968 mutex_unlock(&root->log_mutex);
2969
Chris Mason12fcfd22009-03-24 10:24:20 -04002970out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002971 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002972 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2973
Miao Xied1433de2014-02-20 18:08:59 +08002974 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002975 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002976 mutex_unlock(&log_root_tree->log_mutex);
2977
David Sterba33a9eca2015-10-10 18:35:10 +02002978 /*
2979 * The barrier before waitqueue_active is implied by mutex_unlock
2980 */
Yan Zheng7237f182009-01-21 12:54:03 -05002981 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2982 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002983out:
Miao Xied1433de2014-02-20 18:08:59 +08002984 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002985 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002986 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002987 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002988 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002989
David Sterba33a9eca2015-10-10 18:35:10 +02002990 /*
2991 * The barrier before waitqueue_active is implied by mutex_unlock
2992 */
Yan Zheng7237f182009-01-21 12:54:03 -05002993 if (waitqueue_active(&root->log_commit_wait[index1]))
2994 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002995 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002996}
2997
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002998static void free_log_tree(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003000{
3001 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003002 u64 start;
3003 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003004 struct walk_control wc = {
3005 .free = 1,
3006 .process_func = process_one_buffer
3007 };
3008
Josef Bacik681ae502013-10-07 15:11:00 -04003009 ret = walk_log_tree(trans, log, &wc);
3010 /* I don't think this can happen but just in case */
3011 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003012 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003013
Chris Masond3977122009-01-05 21:25:51 -05003014 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003015 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04003016 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3017 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003018 if (ret)
3019 break;
3020
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003021 clear_extent_bits(&log->dirty_log_pages, start, end,
David Sterba91166212016-04-26 23:54:39 +02003022 EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04003023 }
3024
Josef Bacik2ab28f32012-10-12 15:27:49 -04003025 /*
3026 * We may have short-circuited the log tree with the full commit logic
3027 * and left ordered extents on our list, so clear these out to keep us
3028 * from leaking inodes and memory.
3029 */
3030 btrfs_free_logged_extents(log, 0);
3031 btrfs_free_logged_extents(log, 1);
3032
Yan Zheng7237f182009-01-21 12:54:03 -05003033 free_extent_buffer(log->node);
3034 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003035}
3036
3037/*
3038 * free all the extents used by the tree log. This should be called
3039 * at commit time of the full transaction
3040 */
3041int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3042{
3043 if (root->log_root) {
3044 free_log_tree(trans, root->log_root);
3045 root->log_root = NULL;
3046 }
3047 return 0;
3048}
3049
3050int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3051 struct btrfs_fs_info *fs_info)
3052{
3053 if (fs_info->log_root_tree) {
3054 free_log_tree(trans, fs_info->log_root_tree);
3055 fs_info->log_root_tree = NULL;
3056 }
Chris Masone02119d2008-09-05 16:13:11 -04003057 return 0;
3058}
3059
3060/*
Chris Masone02119d2008-09-05 16:13:11 -04003061 * If both a file and directory are logged, and unlinks or renames are
3062 * mixed in, we have a few interesting corners:
3063 *
3064 * create file X in dir Y
3065 * link file X to X.link in dir Y
3066 * fsync file X
3067 * unlink file X but leave X.link
3068 * fsync dir Y
3069 *
3070 * After a crash we would expect only X.link to exist. But file X
3071 * didn't get fsync'd again so the log has back refs for X and X.link.
3072 *
3073 * We solve this by removing directory entries and inode backrefs from the
3074 * log when a file that was logged in the current transaction is
3075 * unlinked. Any later fsync will include the updated log entries, and
3076 * we'll be able to reconstruct the proper directory items from backrefs.
3077 *
3078 * This optimizations allows us to avoid relogging the entire inode
3079 * or the entire directory.
3080 */
3081int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3082 struct btrfs_root *root,
3083 const char *name, int name_len,
3084 struct inode *dir, u64 index)
3085{
3086 struct btrfs_root *log;
3087 struct btrfs_dir_item *di;
3088 struct btrfs_path *path;
3089 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003090 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003091 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003092 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003093
Chris Mason3a5f1d42008-09-11 15:53:37 -04003094 if (BTRFS_I(dir)->logged_trans < trans->transid)
3095 return 0;
3096
Chris Masone02119d2008-09-05 16:13:11 -04003097 ret = join_running_log_trans(root);
3098 if (ret)
3099 return 0;
3100
3101 mutex_lock(&BTRFS_I(dir)->log_mutex);
3102
3103 log = root->log_root;
3104 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003105 if (!path) {
3106 err = -ENOMEM;
3107 goto out_unlock;
3108 }
liubo2a29edc2011-01-26 06:22:08 +00003109
Li Zefan33345d012011-04-20 10:31:50 +08003110 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003111 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003112 if (IS_ERR(di)) {
3113 err = PTR_ERR(di);
3114 goto fail;
3115 }
3116 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003117 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3118 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003119 if (ret) {
3120 err = ret;
3121 goto fail;
3122 }
Chris Masone02119d2008-09-05 16:13:11 -04003123 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003124 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003125 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003126 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003127 if (IS_ERR(di)) {
3128 err = PTR_ERR(di);
3129 goto fail;
3130 }
3131 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003132 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3133 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003134 if (ret) {
3135 err = ret;
3136 goto fail;
3137 }
Chris Masone02119d2008-09-05 16:13:11 -04003138 }
3139
3140 /* update the directory size in the log to reflect the names
3141 * we have removed
3142 */
3143 if (bytes_del) {
3144 struct btrfs_key key;
3145
Li Zefan33345d012011-04-20 10:31:50 +08003146 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003147 key.offset = 0;
3148 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003149 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003150
3151 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003152 if (ret < 0) {
3153 err = ret;
3154 goto fail;
3155 }
Chris Masone02119d2008-09-05 16:13:11 -04003156 if (ret == 0) {
3157 struct btrfs_inode_item *item;
3158 u64 i_size;
3159
3160 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3161 struct btrfs_inode_item);
3162 i_size = btrfs_inode_size(path->nodes[0], item);
3163 if (i_size > bytes_del)
3164 i_size -= bytes_del;
3165 else
3166 i_size = 0;
3167 btrfs_set_inode_size(path->nodes[0], item, i_size);
3168 btrfs_mark_buffer_dirty(path->nodes[0]);
3169 } else
3170 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003171 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003172 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003173fail:
Chris Masone02119d2008-09-05 16:13:11 -04003174 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003175out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003176 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003177 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003178 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003179 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003180 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003181 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003182
Chris Mason12fcfd22009-03-24 10:24:20 -04003183 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003184
Andi Kleen411fc6b2010-10-29 15:14:31 -04003185 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003186}
3187
3188/* see comments for btrfs_del_dir_entries_in_log */
3189int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3190 struct btrfs_root *root,
3191 const char *name, int name_len,
3192 struct inode *inode, u64 dirid)
3193{
3194 struct btrfs_root *log;
3195 u64 index;
3196 int ret;
3197
Chris Mason3a5f1d42008-09-11 15:53:37 -04003198 if (BTRFS_I(inode)->logged_trans < trans->transid)
3199 return 0;
3200
Chris Masone02119d2008-09-05 16:13:11 -04003201 ret = join_running_log_trans(root);
3202 if (ret)
3203 return 0;
3204 log = root->log_root;
3205 mutex_lock(&BTRFS_I(inode)->log_mutex);
3206
Li Zefan33345d012011-04-20 10:31:50 +08003207 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003208 dirid, &index);
3209 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003210 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003211 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003212 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003213 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003214 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003215 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003216
Chris Masone02119d2008-09-05 16:13:11 -04003217 return ret;
3218}
3219
3220/*
3221 * creates a range item in the log for 'dirid'. first_offset and
3222 * last_offset tell us which parts of the key space the log should
3223 * be considered authoritative for.
3224 */
3225static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3226 struct btrfs_root *log,
3227 struct btrfs_path *path,
3228 int key_type, u64 dirid,
3229 u64 first_offset, u64 last_offset)
3230{
3231 int ret;
3232 struct btrfs_key key;
3233 struct btrfs_dir_log_item *item;
3234
3235 key.objectid = dirid;
3236 key.offset = first_offset;
3237 if (key_type == BTRFS_DIR_ITEM_KEY)
3238 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3239 else
3240 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3241 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003242 if (ret)
3243 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003244
3245 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3246 struct btrfs_dir_log_item);
3247 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3248 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003249 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003250 return 0;
3251}
3252
3253/*
3254 * log all the items included in the current transaction for a given
3255 * directory. This also creates the range items in the log tree required
3256 * to replay anything deleted before the fsync
3257 */
3258static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3259 struct btrfs_root *root, struct inode *inode,
3260 struct btrfs_path *path,
3261 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003262 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003263 u64 min_offset, u64 *last_offset_ret)
3264{
3265 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003266 struct btrfs_root *log = root->log_root;
3267 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003268 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003269 int ret;
3270 int i;
3271 int nritems;
3272 u64 first_offset = min_offset;
3273 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003274 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003275
3276 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003277
Li Zefan33345d012011-04-20 10:31:50 +08003278 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003279 min_key.type = key_type;
3280 min_key.offset = min_offset;
3281
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003282 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003283
3284 /*
3285 * we didn't find anything from this transaction, see if there
3286 * is anything at all
3287 */
Li Zefan33345d012011-04-20 10:31:50 +08003288 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3289 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003290 min_key.type = key_type;
3291 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003292 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003293 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3294 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003295 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003296 return ret;
3297 }
Li Zefan33345d012011-04-20 10:31:50 +08003298 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003299
3300 /* if ret == 0 there are items for this type,
3301 * create a range to tell us the last key of this type.
3302 * otherwise, there are no items in this directory after
3303 * *min_offset, and we create a range to indicate that.
3304 */
3305 if (ret == 0) {
3306 struct btrfs_key tmp;
3307 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3308 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003309 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003310 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003311 }
3312 goto done;
3313 }
3314
3315 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003316 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003317 if (ret == 0) {
3318 struct btrfs_key tmp;
3319 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3320 if (key_type == tmp.type) {
3321 first_offset = tmp.offset;
3322 ret = overwrite_item(trans, log, dst_path,
3323 path->nodes[0], path->slots[0],
3324 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003325 if (ret) {
3326 err = ret;
3327 goto done;
3328 }
Chris Masone02119d2008-09-05 16:13:11 -04003329 }
3330 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003331 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003332
3333 /* find the first key from this transaction again */
3334 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303335 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003336 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003337
3338 /*
3339 * we have a block from this transaction, log every item in it
3340 * from our directory
3341 */
Chris Masond3977122009-01-05 21:25:51 -05003342 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003343 struct btrfs_key tmp;
3344 src = path->nodes[0];
3345 nritems = btrfs_header_nritems(src);
3346 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003347 struct btrfs_dir_item *di;
3348
Chris Masone02119d2008-09-05 16:13:11 -04003349 btrfs_item_key_to_cpu(src, &min_key, i);
3350
Li Zefan33345d012011-04-20 10:31:50 +08003351 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003352 goto done;
3353 ret = overwrite_item(trans, log, dst_path, src, i,
3354 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003355 if (ret) {
3356 err = ret;
3357 goto done;
3358 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003359
3360 /*
3361 * We must make sure that when we log a directory entry,
3362 * the corresponding inode, after log replay, has a
3363 * matching link count. For example:
3364 *
3365 * touch foo
3366 * mkdir mydir
3367 * sync
3368 * ln foo mydir/bar
3369 * xfs_io -c "fsync" mydir
3370 * <crash>
3371 * <mount fs and log replay>
3372 *
3373 * Would result in a fsync log that when replayed, our
3374 * file inode would have a link count of 1, but we get
3375 * two directory entries pointing to the same inode.
3376 * After removing one of the names, it would not be
3377 * possible to remove the other name, which resulted
3378 * always in stale file handle errors, and would not
3379 * be possible to rmdir the parent directory, since
3380 * its i_size could never decrement to the value
3381 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3382 */
3383 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3384 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3385 if (ctx &&
3386 (btrfs_dir_transid(src, di) == trans->transid ||
3387 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3388 tmp.type != BTRFS_ROOT_ITEM_KEY)
3389 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003390 }
3391 path->slots[0] = nritems;
3392
3393 /*
3394 * look ahead to the next item and see if it is also
3395 * from this directory and from this transaction
3396 */
3397 ret = btrfs_next_leaf(root, path);
3398 if (ret == 1) {
3399 last_offset = (u64)-1;
3400 goto done;
3401 }
3402 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003403 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003404 last_offset = (u64)-1;
3405 goto done;
3406 }
3407 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3408 ret = overwrite_item(trans, log, dst_path,
3409 path->nodes[0], path->slots[0],
3410 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003411 if (ret)
3412 err = ret;
3413 else
3414 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003415 goto done;
3416 }
3417 }
3418done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003419 btrfs_release_path(path);
3420 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003421
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003422 if (err == 0) {
3423 *last_offset_ret = last_offset;
3424 /*
3425 * insert the log range keys to indicate where the log
3426 * is valid
3427 */
3428 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003429 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003430 if (ret)
3431 err = ret;
3432 }
3433 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003434}
3435
3436/*
3437 * logging directories is very similar to logging inodes, We find all the items
3438 * from the current transaction and write them to the log.
3439 *
3440 * The recovery code scans the directory in the subvolume, and if it finds a
3441 * key in the range logged that is not present in the log tree, then it means
3442 * that dir entry was unlinked during the transaction.
3443 *
3444 * In order for that scan to work, we must include one key smaller than
3445 * the smallest logged by this transaction and one key larger than the largest
3446 * key logged by this transaction.
3447 */
3448static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3449 struct btrfs_root *root, struct inode *inode,
3450 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003451 struct btrfs_path *dst_path,
3452 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003453{
3454 u64 min_key;
3455 u64 max_key;
3456 int ret;
3457 int key_type = BTRFS_DIR_ITEM_KEY;
3458
3459again:
3460 min_key = 0;
3461 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003462 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003463 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003464 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003465 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003466 if (ret)
3467 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003468 if (max_key == (u64)-1)
3469 break;
3470 min_key = max_key + 1;
3471 }
3472
3473 if (key_type == BTRFS_DIR_ITEM_KEY) {
3474 key_type = BTRFS_DIR_INDEX_KEY;
3475 goto again;
3476 }
3477 return 0;
3478}
3479
3480/*
3481 * a helper function to drop items from the log before we relog an
3482 * inode. max_key_type indicates the highest item type to remove.
3483 * This cannot be run for file data extents because it does not
3484 * free the extents they point to.
3485 */
3486static int drop_objectid_items(struct btrfs_trans_handle *trans,
3487 struct btrfs_root *log,
3488 struct btrfs_path *path,
3489 u64 objectid, int max_key_type)
3490{
3491 int ret;
3492 struct btrfs_key key;
3493 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003494 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003495
3496 key.objectid = objectid;
3497 key.type = max_key_type;
3498 key.offset = (u64)-1;
3499
Chris Masond3977122009-01-05 21:25:51 -05003500 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003501 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003502 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003503 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003504 break;
3505
3506 if (path->slots[0] == 0)
3507 break;
3508
3509 path->slots[0]--;
3510 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3511 path->slots[0]);
3512
3513 if (found_key.objectid != objectid)
3514 break;
3515
Josef Bacik18ec90d2012-09-28 11:56:28 -04003516 found_key.offset = 0;
3517 found_key.type = 0;
3518 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3519 &start_slot);
3520
3521 ret = btrfs_del_items(trans, log, path, start_slot,
3522 path->slots[0] - start_slot + 1);
3523 /*
3524 * If start slot isn't 0 then we don't need to re-search, we've
3525 * found the last guy with the objectid in this tree.
3526 */
3527 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003528 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003529 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003530 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003531 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003532 if (ret > 0)
3533 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003534 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003535}
3536
Josef Bacik94edf4a2012-09-25 14:56:25 -04003537static void fill_inode_item(struct btrfs_trans_handle *trans,
3538 struct extent_buffer *leaf,
3539 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003540 struct inode *inode, int log_inode_only,
3541 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003542{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003543 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003544
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003545 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003546
3547 if (log_inode_only) {
3548 /* set the generation to zero so the recover code
3549 * can tell the difference between an logging
3550 * just to say 'this inode exists' and a logging
3551 * to say 'update this inode with these values'
3552 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003553 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003554 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003555 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003556 btrfs_set_token_inode_generation(leaf, item,
3557 BTRFS_I(inode)->generation,
3558 &token);
3559 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003560 }
3561
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003562 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3563 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3564 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3565 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3566
David Sterbaa937b972014-12-12 17:39:12 +01003567 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003568 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003569 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003570 inode->i_atime.tv_nsec, &token);
3571
David Sterbaa937b972014-12-12 17:39:12 +01003572 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003573 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003574 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003575 inode->i_mtime.tv_nsec, &token);
3576
David Sterbaa937b972014-12-12 17:39:12 +01003577 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003578 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003579 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003580 inode->i_ctime.tv_nsec, &token);
3581
3582 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3583 &token);
3584
3585 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3586 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3587 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3588 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3589 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003590}
3591
Josef Bacika95249b2012-10-11 16:17:34 -04003592static int log_inode_item(struct btrfs_trans_handle *trans,
3593 struct btrfs_root *log, struct btrfs_path *path,
3594 struct inode *inode)
3595{
3596 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003597 int ret;
3598
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003599 ret = btrfs_insert_empty_item(trans, log, path,
3600 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003601 sizeof(*inode_item));
3602 if (ret && ret != -EEXIST)
3603 return ret;
3604 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3605 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003606 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003607 btrfs_release_path(path);
3608 return 0;
3609}
3610
Chris Mason31ff1cd2008-09-11 16:17:57 -04003611static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003612 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003613 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003614 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003615 int start_slot, int nr, int inode_only,
3616 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003617{
3618 unsigned long src_offset;
3619 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003620 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003621 struct btrfs_file_extent_item *extent;
3622 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003623 struct extent_buffer *src = src_path->nodes[0];
3624 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003625 int ret;
3626 struct btrfs_key *ins_keys;
3627 u32 *ins_sizes;
3628 char *ins_data;
3629 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003630 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003631 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003632 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003633 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003634 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003635
3636 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003637
3638 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3639 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003640 if (!ins_data)
3641 return -ENOMEM;
3642
Josef Bacik16e75492013-10-22 12:18:51 -04003643 first_key.objectid = (u64)-1;
3644
Chris Mason31ff1cd2008-09-11 16:17:57 -04003645 ins_sizes = (u32 *)ins_data;
3646 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3647
3648 for (i = 0; i < nr; i++) {
3649 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3650 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3651 }
3652 ret = btrfs_insert_empty_items(trans, log, dst_path,
3653 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003654 if (ret) {
3655 kfree(ins_data);
3656 return ret;
3657 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003659 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003660 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3661 dst_path->slots[0]);
3662
3663 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3664
Josef Bacik16e75492013-10-22 12:18:51 -04003665 if ((i == (nr - 1)))
3666 last_key = ins_keys[i];
3667
Josef Bacik94edf4a2012-09-25 14:56:25 -04003668 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003669 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3670 dst_path->slots[0],
3671 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003672 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003673 inode, inode_only == LOG_INODE_EXISTS,
3674 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003675 } else {
3676 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3677 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003678 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003679
Josef Bacik16e75492013-10-22 12:18:51 -04003680 /*
3681 * We set need_find_last_extent here in case we know we were
3682 * processing other items and then walk into the first extent in
3683 * the inode. If we don't hit an extent then nothing changes,
3684 * we'll do the last search the next time around.
3685 */
3686 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3687 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003688 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003689 first_key = ins_keys[i];
3690 } else {
3691 need_find_last_extent = false;
3692 }
3693
Chris Mason31ff1cd2008-09-11 16:17:57 -04003694 /* take a reference on file data extents so that truncates
3695 * or deletes of this inode don't have to relog the inode
3696 * again
3697 */
David Sterba962a2982014-06-04 18:41:45 +02003698 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003699 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003700 int found_type;
3701 extent = btrfs_item_ptr(src, start_slot + i,
3702 struct btrfs_file_extent_item);
3703
liubo8e531cd2011-05-06 10:36:09 +08003704 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3705 continue;
3706
Chris Mason31ff1cd2008-09-11 16:17:57 -04003707 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003708 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003709 u64 ds, dl, cs, cl;
3710 ds = btrfs_file_extent_disk_bytenr(src,
3711 extent);
3712 /* ds == 0 is a hole */
3713 if (ds == 0)
3714 continue;
3715
3716 dl = btrfs_file_extent_disk_num_bytes(src,
3717 extent);
3718 cs = btrfs_file_extent_offset(src, extent);
3719 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003720 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003721 if (btrfs_file_extent_compression(src,
3722 extent)) {
3723 cs = 0;
3724 cl = dl;
3725 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003726
3727 ret = btrfs_lookup_csums_range(
3728 log->fs_info->csum_root,
3729 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003730 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003731 if (ret) {
3732 btrfs_release_path(dst_path);
3733 kfree(ins_data);
3734 return ret;
3735 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003736 }
3737 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003738 }
3739
3740 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003741 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003742 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003743
3744 /*
3745 * we have to do this after the loop above to avoid changing the
3746 * log tree while trying to change the log tree.
3747 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003748 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003749 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003750 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3751 struct btrfs_ordered_sum,
3752 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003753 if (!ret)
3754 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003755 list_del(&sums->list);
3756 kfree(sums);
3757 }
Josef Bacik16e75492013-10-22 12:18:51 -04003758
3759 if (!has_extents)
3760 return ret;
3761
Filipe Manana74121f72014-08-07 12:00:44 +01003762 if (need_find_last_extent && *last_extent == first_key.offset) {
3763 /*
3764 * We don't have any leafs between our current one and the one
3765 * we processed before that can have file extent items for our
3766 * inode (and have a generation number smaller than our current
3767 * transaction id).
3768 */
3769 need_find_last_extent = false;
3770 }
3771
Josef Bacik16e75492013-10-22 12:18:51 -04003772 /*
3773 * Because we use btrfs_search_forward we could skip leaves that were
3774 * not modified and then assume *last_extent is valid when it really
3775 * isn't. So back up to the previous leaf and read the end of the last
3776 * extent before we go and fill in holes.
3777 */
3778 if (need_find_last_extent) {
3779 u64 len;
3780
3781 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3782 if (ret < 0)
3783 return ret;
3784 if (ret)
3785 goto fill_holes;
3786 if (src_path->slots[0])
3787 src_path->slots[0]--;
3788 src = src_path->nodes[0];
3789 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3790 if (key.objectid != btrfs_ino(inode) ||
3791 key.type != BTRFS_EXTENT_DATA_KEY)
3792 goto fill_holes;
3793 extent = btrfs_item_ptr(src, src_path->slots[0],
3794 struct btrfs_file_extent_item);
3795 if (btrfs_file_extent_type(src, extent) ==
3796 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003797 len = btrfs_file_extent_inline_len(src,
3798 src_path->slots[0],
3799 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003800 *last_extent = ALIGN(key.offset + len,
3801 log->sectorsize);
3802 } else {
3803 len = btrfs_file_extent_num_bytes(src, extent);
3804 *last_extent = key.offset + len;
3805 }
3806 }
3807fill_holes:
3808 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3809 * things could have happened
3810 *
3811 * 1) A merge could have happened, so we could currently be on a leaf
3812 * that holds what we were copying in the first place.
3813 * 2) A split could have happened, and now not all of the items we want
3814 * are on the same leaf.
3815 *
3816 * So we need to adjust how we search for holes, we need to drop the
3817 * path and re-search for the first extent key we found, and then walk
3818 * forward until we hit the last one we copied.
3819 */
3820 if (need_find_last_extent) {
3821 /* btrfs_prev_leaf could return 1 without releasing the path */
3822 btrfs_release_path(src_path);
3823 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3824 src_path, 0, 0);
3825 if (ret < 0)
3826 return ret;
3827 ASSERT(ret == 0);
3828 src = src_path->nodes[0];
3829 i = src_path->slots[0];
3830 } else {
3831 i = start_slot;
3832 }
3833
3834 /*
3835 * Ok so here we need to go through and fill in any holes we may have
3836 * to make sure that holes are punched for those areas in case they had
3837 * extents previously.
3838 */
3839 while (!done) {
3840 u64 offset, len;
3841 u64 extent_end;
3842
3843 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3844 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3845 if (ret < 0)
3846 return ret;
3847 ASSERT(ret == 0);
3848 src = src_path->nodes[0];
3849 i = 0;
3850 }
3851
3852 btrfs_item_key_to_cpu(src, &key, i);
3853 if (!btrfs_comp_cpu_keys(&key, &last_key))
3854 done = true;
3855 if (key.objectid != btrfs_ino(inode) ||
3856 key.type != BTRFS_EXTENT_DATA_KEY) {
3857 i++;
3858 continue;
3859 }
3860 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3861 if (btrfs_file_extent_type(src, extent) ==
3862 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003863 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003864 extent_end = ALIGN(key.offset + len, log->sectorsize);
3865 } else {
3866 len = btrfs_file_extent_num_bytes(src, extent);
3867 extent_end = key.offset + len;
3868 }
3869 i++;
3870
3871 if (*last_extent == key.offset) {
3872 *last_extent = extent_end;
3873 continue;
3874 }
3875 offset = *last_extent;
3876 len = key.offset - *last_extent;
3877 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3878 offset, 0, 0, len, 0, len, 0,
3879 0, 0);
3880 if (ret)
3881 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003882 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003883 }
3884 /*
3885 * Need to let the callers know we dropped the path so they should
3886 * re-search.
3887 */
3888 if (!ret && need_find_last_extent)
3889 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003890 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003891}
3892
Josef Bacik5dc562c2012-08-17 13:14:17 -04003893static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3894{
3895 struct extent_map *em1, *em2;
3896
3897 em1 = list_entry(a, struct extent_map, list);
3898 em2 = list_entry(b, struct extent_map, list);
3899
3900 if (em1->start < em2->start)
3901 return -1;
3902 else if (em1->start > em2->start)
3903 return 1;
3904 return 0;
3905}
3906
Filipe Manana8407f552014-09-05 15:14:39 +01003907static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3908 struct inode *inode,
3909 struct btrfs_root *root,
3910 const struct extent_map *em,
3911 const struct list_head *logged_list,
3912 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003913{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003914 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003915 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003916 u64 mod_start = em->mod_start;
3917 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003918 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003919 u64 csum_offset;
3920 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003921 LIST_HEAD(ordered_sums);
3922 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003923
Filipe Manana8407f552014-09-05 15:14:39 +01003924 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003925
Filipe Manana8407f552014-09-05 15:14:39 +01003926 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3927 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003928 return 0;
3929
Josef Bacik2ab28f32012-10-12 15:27:49 -04003930 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003931 * Wait far any ordered extent that covers our extent map. If it
3932 * finishes without an error, first check and see if our csums are on
3933 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003934 */
Miao Xie827463c2014-01-14 20:31:51 +08003935 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003936 struct btrfs_ordered_sum *sum;
3937
3938 if (!mod_len)
3939 break;
3940
Josef Bacik2ab28f32012-10-12 15:27:49 -04003941 if (ordered->file_offset + ordered->len <= mod_start ||
3942 mod_start + mod_len <= ordered->file_offset)
3943 continue;
3944
Filipe Manana8407f552014-09-05 15:14:39 +01003945 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3946 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3947 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3948 const u64 start = ordered->file_offset;
3949 const u64 end = ordered->file_offset + ordered->len - 1;
3950
3951 WARN_ON(ordered->inode != inode);
3952 filemap_fdatawrite_range(inode->i_mapping, start, end);
3953 }
3954
3955 wait_event(ordered->wait,
3956 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3957 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3958
3959 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003960 /*
3961 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3962 * i_mapping flags, so that the next fsync won't get
3963 * an outdated io error too.
3964 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003965 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003966 *ordered_io_error = true;
3967 break;
3968 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003969 /*
3970 * We are going to copy all the csums on this ordered extent, so
3971 * go ahead and adjust mod_start and mod_len in case this
3972 * ordered extent has already been logged.
3973 */
3974 if (ordered->file_offset > mod_start) {
3975 if (ordered->file_offset + ordered->len >=
3976 mod_start + mod_len)
3977 mod_len = ordered->file_offset - mod_start;
3978 /*
3979 * If we have this case
3980 *
3981 * |--------- logged extent ---------|
3982 * |----- ordered extent ----|
3983 *
3984 * Just don't mess with mod_start and mod_len, we'll
3985 * just end up logging more csums than we need and it
3986 * will be ok.
3987 */
3988 } else {
3989 if (ordered->file_offset + ordered->len <
3990 mod_start + mod_len) {
3991 mod_len = (mod_start + mod_len) -
3992 (ordered->file_offset + ordered->len);
3993 mod_start = ordered->file_offset +
3994 ordered->len;
3995 } else {
3996 mod_len = 0;
3997 }
3998 }
3999
Filipe Manana8407f552014-09-05 15:14:39 +01004000 if (skip_csum)
4001 continue;
4002
Josef Bacik2ab28f32012-10-12 15:27:49 -04004003 /*
4004 * To keep us from looping for the above case of an ordered
4005 * extent that falls inside of the logged extent.
4006 */
4007 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4008 &ordered->flags))
4009 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004010
Josef Bacik2ab28f32012-10-12 15:27:49 -04004011 list_for_each_entry(sum, &ordered->list, list) {
4012 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004013 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004014 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004015 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004016 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004017
Filipe Manana8407f552014-09-05 15:14:39 +01004018 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004019 return ret;
4020
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004021 if (em->compress_type) {
4022 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004023 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004024 } else {
4025 csum_offset = mod_start - em->start;
4026 csum_len = mod_len;
4027 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004028
Josef Bacik70c8a912012-10-11 16:54:30 -04004029 /* block start is already adjusted for the file extent offset. */
4030 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4031 em->block_start + csum_offset,
4032 em->block_start + csum_offset +
4033 csum_len - 1, &ordered_sums, 0);
4034 if (ret)
4035 return ret;
4036
4037 while (!list_empty(&ordered_sums)) {
4038 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4039 struct btrfs_ordered_sum,
4040 list);
4041 if (!ret)
4042 ret = btrfs_csum_file_blocks(trans, log, sums);
4043 list_del(&sums->list);
4044 kfree(sums);
4045 }
4046
4047 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004048}
4049
Filipe Manana8407f552014-09-05 15:14:39 +01004050static int log_one_extent(struct btrfs_trans_handle *trans,
4051 struct inode *inode, struct btrfs_root *root,
4052 const struct extent_map *em,
4053 struct btrfs_path *path,
4054 const struct list_head *logged_list,
4055 struct btrfs_log_ctx *ctx)
4056{
4057 struct btrfs_root *log = root->log_root;
4058 struct btrfs_file_extent_item *fi;
4059 struct extent_buffer *leaf;
4060 struct btrfs_map_token token;
4061 struct btrfs_key key;
4062 u64 extent_offset = em->start - em->orig_start;
4063 u64 block_len;
4064 int ret;
4065 int extent_inserted = 0;
4066 bool ordered_io_err = false;
4067
4068 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4069 &ordered_io_err);
4070 if (ret)
4071 return ret;
4072
4073 if (ordered_io_err) {
4074 ctx->io_err = -EIO;
4075 return 0;
4076 }
4077
4078 btrfs_init_map_token(&token);
4079
4080 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4081 em->start + em->len, NULL, 0, 1,
4082 sizeof(*fi), &extent_inserted);
4083 if (ret)
4084 return ret;
4085
4086 if (!extent_inserted) {
4087 key.objectid = btrfs_ino(inode);
4088 key.type = BTRFS_EXTENT_DATA_KEY;
4089 key.offset = em->start;
4090
4091 ret = btrfs_insert_empty_item(trans, log, path, &key,
4092 sizeof(*fi));
4093 if (ret)
4094 return ret;
4095 }
4096 leaf = path->nodes[0];
4097 fi = btrfs_item_ptr(leaf, path->slots[0],
4098 struct btrfs_file_extent_item);
4099
Josef Bacik50d9aa92014-11-21 14:52:38 -05004100 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004101 &token);
4102 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4103 btrfs_set_token_file_extent_type(leaf, fi,
4104 BTRFS_FILE_EXTENT_PREALLOC,
4105 &token);
4106 else
4107 btrfs_set_token_file_extent_type(leaf, fi,
4108 BTRFS_FILE_EXTENT_REG,
4109 &token);
4110
4111 block_len = max(em->block_len, em->orig_block_len);
4112 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4113 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4114 em->block_start,
4115 &token);
4116 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4117 &token);
4118 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4119 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4120 em->block_start -
4121 extent_offset, &token);
4122 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4123 &token);
4124 } else {
4125 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4126 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4127 &token);
4128 }
4129
4130 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4131 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4132 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4133 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4134 &token);
4135 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4136 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4137 btrfs_mark_buffer_dirty(leaf);
4138
4139 btrfs_release_path(path);
4140
4141 return ret;
4142}
4143
Josef Bacik5dc562c2012-08-17 13:14:17 -04004144static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *root,
4146 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004147 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004148 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004149 struct btrfs_log_ctx *ctx,
4150 const u64 start,
4151 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004152{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004153 struct extent_map *em, *n;
4154 struct list_head extents;
4155 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4156 u64 test_gen;
4157 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004158 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004159
4160 INIT_LIST_HEAD(&extents);
4161
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004162 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004163 write_lock(&tree->lock);
4164 test_gen = root->fs_info->last_trans_committed;
4165
4166 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4167 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004168
4169 /*
4170 * Just an arbitrary number, this can be really CPU intensive
4171 * once we start getting a lot of extents, and really once we
4172 * have a bunch of extents we just want to commit since it will
4173 * be faster.
4174 */
4175 if (++num > 32768) {
4176 list_del_init(&tree->modified_extents);
4177 ret = -EFBIG;
4178 goto process;
4179 }
4180
Josef Bacik5dc562c2012-08-17 13:14:17 -04004181 if (em->generation <= test_gen)
4182 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004183 /* Need a ref to keep it from getting evicted from cache */
4184 atomic_inc(&em->refs);
4185 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004186 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004187 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004188 }
4189
4190 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004191 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004192 /*
4193 * Some ordered extents started by fsync might have completed
4194 * before we could collect them into the list logged_list, which
4195 * means they're gone, not in our logged_list nor in the inode's
4196 * ordered tree. We want the application/user space to know an
4197 * error happened while attempting to persist file data so that
4198 * it can take proper action. If such error happened, we leave
4199 * without writing to the log tree and the fsync must report the
4200 * file data write error and not commit the current transaction.
4201 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004202 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004203 if (ret)
4204 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004205process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004206 while (!list_empty(&extents)) {
4207 em = list_entry(extents.next, struct extent_map, list);
4208
4209 list_del_init(&em->list);
4210
4211 /*
4212 * If we had an error we just need to delete everybody from our
4213 * private list.
4214 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004215 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004216 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004217 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004218 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004219 }
4220
4221 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004222
Filipe Manana8407f552014-09-05 15:14:39 +01004223 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4224 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004225 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004226 clear_em_logging(tree, em);
4227 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004228 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004229 WARN_ON(!list_empty(&extents));
4230 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004231 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004232
Josef Bacik5dc562c2012-08-17 13:14:17 -04004233 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004234 return ret;
4235}
4236
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004237static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4238 struct btrfs_path *path, u64 *size_ret)
4239{
4240 struct btrfs_key key;
4241 int ret;
4242
4243 key.objectid = btrfs_ino(inode);
4244 key.type = BTRFS_INODE_ITEM_KEY;
4245 key.offset = 0;
4246
4247 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4248 if (ret < 0) {
4249 return ret;
4250 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004251 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004252 } else {
4253 struct btrfs_inode_item *item;
4254
4255 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4256 struct btrfs_inode_item);
4257 *size_ret = btrfs_inode_size(path->nodes[0], item);
4258 }
4259
4260 btrfs_release_path(path);
4261 return 0;
4262}
4263
Filipe Manana36283bf2015-06-20 00:44:51 +01004264/*
4265 * At the moment we always log all xattrs. This is to figure out at log replay
4266 * time which xattrs must have their deletion replayed. If a xattr is missing
4267 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4268 * because if a xattr is deleted, the inode is fsynced and a power failure
4269 * happens, causing the log to be replayed the next time the fs is mounted,
4270 * we want the xattr to not exist anymore (same behaviour as other filesystems
4271 * with a journal, ext3/4, xfs, f2fs, etc).
4272 */
4273static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4274 struct btrfs_root *root,
4275 struct inode *inode,
4276 struct btrfs_path *path,
4277 struct btrfs_path *dst_path)
4278{
4279 int ret;
4280 struct btrfs_key key;
4281 const u64 ino = btrfs_ino(inode);
4282 int ins_nr = 0;
4283 int start_slot = 0;
4284
4285 key.objectid = ino;
4286 key.type = BTRFS_XATTR_ITEM_KEY;
4287 key.offset = 0;
4288
4289 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4290 if (ret < 0)
4291 return ret;
4292
4293 while (true) {
4294 int slot = path->slots[0];
4295 struct extent_buffer *leaf = path->nodes[0];
4296 int nritems = btrfs_header_nritems(leaf);
4297
4298 if (slot >= nritems) {
4299 if (ins_nr > 0) {
4300 u64 last_extent = 0;
4301
4302 ret = copy_items(trans, inode, dst_path, path,
4303 &last_extent, start_slot,
4304 ins_nr, 1, 0);
4305 /* can't be 1, extent items aren't processed */
4306 ASSERT(ret <= 0);
4307 if (ret < 0)
4308 return ret;
4309 ins_nr = 0;
4310 }
4311 ret = btrfs_next_leaf(root, path);
4312 if (ret < 0)
4313 return ret;
4314 else if (ret > 0)
4315 break;
4316 continue;
4317 }
4318
4319 btrfs_item_key_to_cpu(leaf, &key, slot);
4320 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4321 break;
4322
4323 if (ins_nr == 0)
4324 start_slot = slot;
4325 ins_nr++;
4326 path->slots[0]++;
4327 cond_resched();
4328 }
4329 if (ins_nr > 0) {
4330 u64 last_extent = 0;
4331
4332 ret = copy_items(trans, inode, dst_path, path,
4333 &last_extent, start_slot,
4334 ins_nr, 1, 0);
4335 /* can't be 1, extent items aren't processed */
4336 ASSERT(ret <= 0);
4337 if (ret < 0)
4338 return ret;
4339 }
4340
4341 return 0;
4342}
4343
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004344/*
4345 * If the no holes feature is enabled we need to make sure any hole between the
4346 * last extent and the i_size of our inode is explicitly marked in the log. This
4347 * is to make sure that doing something like:
4348 *
4349 * 1) create file with 128Kb of data
4350 * 2) truncate file to 64Kb
4351 * 3) truncate file to 256Kb
4352 * 4) fsync file
4353 * 5) <crash/power failure>
4354 * 6) mount fs and trigger log replay
4355 *
4356 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4357 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4358 * file correspond to a hole. The presence of explicit holes in a log tree is
4359 * what guarantees that log replay will remove/adjust file extent items in the
4360 * fs/subvol tree.
4361 *
4362 * Here we do not need to care about holes between extents, that is already done
4363 * by copy_items(). We also only need to do this in the full sync path, where we
4364 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4365 * lookup the list of modified extent maps and if any represents a hole, we
4366 * insert a corresponding extent representing a hole in the log tree.
4367 */
4368static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4369 struct btrfs_root *root,
4370 struct inode *inode,
4371 struct btrfs_path *path)
4372{
4373 int ret;
4374 struct btrfs_key key;
4375 u64 hole_start;
4376 u64 hole_size;
4377 struct extent_buffer *leaf;
4378 struct btrfs_root *log = root->log_root;
4379 const u64 ino = btrfs_ino(inode);
4380 const u64 i_size = i_size_read(inode);
4381
4382 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4383 return 0;
4384
4385 key.objectid = ino;
4386 key.type = BTRFS_EXTENT_DATA_KEY;
4387 key.offset = (u64)-1;
4388
4389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4390 ASSERT(ret != 0);
4391 if (ret < 0)
4392 return ret;
4393
4394 ASSERT(path->slots[0] > 0);
4395 path->slots[0]--;
4396 leaf = path->nodes[0];
4397 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4398
4399 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4400 /* inode does not have any extents */
4401 hole_start = 0;
4402 hole_size = i_size;
4403 } else {
4404 struct btrfs_file_extent_item *extent;
4405 u64 len;
4406
4407 /*
4408 * If there's an extent beyond i_size, an explicit hole was
4409 * already inserted by copy_items().
4410 */
4411 if (key.offset >= i_size)
4412 return 0;
4413
4414 extent = btrfs_item_ptr(leaf, path->slots[0],
4415 struct btrfs_file_extent_item);
4416
4417 if (btrfs_file_extent_type(leaf, extent) ==
4418 BTRFS_FILE_EXTENT_INLINE) {
4419 len = btrfs_file_extent_inline_len(leaf,
4420 path->slots[0],
4421 extent);
4422 ASSERT(len == i_size);
4423 return 0;
4424 }
4425
4426 len = btrfs_file_extent_num_bytes(leaf, extent);
4427 /* Last extent goes beyond i_size, no need to log a hole. */
4428 if (key.offset + len > i_size)
4429 return 0;
4430 hole_start = key.offset + len;
4431 hole_size = i_size - hole_start;
4432 }
4433 btrfs_release_path(path);
4434
4435 /* Last extent ends at i_size. */
4436 if (hole_size == 0)
4437 return 0;
4438
4439 hole_size = ALIGN(hole_size, root->sectorsize);
4440 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4441 hole_size, 0, hole_size, 0, 0, 0);
4442 return ret;
4443}
4444
Filipe Manana56f23fd2016-03-30 23:37:21 +01004445/*
4446 * When we are logging a new inode X, check if it doesn't have a reference that
4447 * matches the reference from some other inode Y created in a past transaction
4448 * and that was renamed in the current transaction. If we don't do this, then at
4449 * log replay time we can lose inode Y (and all its files if it's a directory):
4450 *
4451 * mkdir /mnt/x
4452 * echo "hello world" > /mnt/x/foobar
4453 * sync
4454 * mv /mnt/x /mnt/y
4455 * mkdir /mnt/x # or touch /mnt/x
4456 * xfs_io -c fsync /mnt/x
4457 * <power fail>
4458 * mount fs, trigger log replay
4459 *
4460 * After the log replay procedure, we would lose the first directory and all its
4461 * files (file foobar).
4462 * For the case where inode Y is not a directory we simply end up losing it:
4463 *
4464 * echo "123" > /mnt/foo
4465 * sync
4466 * mv /mnt/foo /mnt/bar
4467 * echo "abc" > /mnt/foo
4468 * xfs_io -c fsync /mnt/foo
4469 * <power fail>
4470 *
4471 * We also need this for cases where a snapshot entry is replaced by some other
4472 * entry (file or directory) otherwise we end up with an unreplayable log due to
4473 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4474 * if it were a regular entry:
4475 *
4476 * mkdir /mnt/x
4477 * btrfs subvolume snapshot /mnt /mnt/x/snap
4478 * btrfs subvolume delete /mnt/x/snap
4479 * rmdir /mnt/x
4480 * mkdir /mnt/x
4481 * fsync /mnt/x or fsync some new file inside it
4482 * <power fail>
4483 *
4484 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4485 * the same transaction.
4486 */
4487static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4488 const int slot,
4489 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004490 struct inode *inode,
4491 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004492{
4493 int ret;
4494 struct btrfs_path *search_path;
4495 char *name = NULL;
4496 u32 name_len = 0;
4497 u32 item_size = btrfs_item_size_nr(eb, slot);
4498 u32 cur_offset = 0;
4499 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4500
4501 search_path = btrfs_alloc_path();
4502 if (!search_path)
4503 return -ENOMEM;
4504 search_path->search_commit_root = 1;
4505 search_path->skip_locking = 1;
4506
4507 while (cur_offset < item_size) {
4508 u64 parent;
4509 u32 this_name_len;
4510 u32 this_len;
4511 unsigned long name_ptr;
4512 struct btrfs_dir_item *di;
4513
4514 if (key->type == BTRFS_INODE_REF_KEY) {
4515 struct btrfs_inode_ref *iref;
4516
4517 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4518 parent = key->offset;
4519 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4520 name_ptr = (unsigned long)(iref + 1);
4521 this_len = sizeof(*iref) + this_name_len;
4522 } else {
4523 struct btrfs_inode_extref *extref;
4524
4525 extref = (struct btrfs_inode_extref *)(ptr +
4526 cur_offset);
4527 parent = btrfs_inode_extref_parent(eb, extref);
4528 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4529 name_ptr = (unsigned long)&extref->name;
4530 this_len = sizeof(*extref) + this_name_len;
4531 }
4532
4533 if (this_name_len > name_len) {
4534 char *new_name;
4535
4536 new_name = krealloc(name, this_name_len, GFP_NOFS);
4537 if (!new_name) {
4538 ret = -ENOMEM;
4539 goto out;
4540 }
4541 name_len = this_name_len;
4542 name = new_name;
4543 }
4544
4545 read_extent_buffer(eb, name, name_ptr, this_name_len);
4546 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4547 search_path, parent,
4548 name, this_name_len, 0);
4549 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004550 struct btrfs_key di_key;
4551
4552 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4553 di, &di_key);
4554 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4555 ret = 1;
4556 *other_ino = di_key.objectid;
4557 } else {
4558 ret = -EAGAIN;
4559 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004560 goto out;
4561 } else if (IS_ERR(di)) {
4562 ret = PTR_ERR(di);
4563 goto out;
4564 }
4565 btrfs_release_path(search_path);
4566
4567 cur_offset += this_len;
4568 }
4569 ret = 0;
4570out:
4571 btrfs_free_path(search_path);
4572 kfree(name);
4573 return ret;
4574}
4575
Chris Masone02119d2008-09-05 16:13:11 -04004576/* log a single inode in the tree log.
4577 * At least one parent directory for this inode must exist in the tree
4578 * or be logged already.
4579 *
4580 * Any items from this inode changed by the current transaction are copied
4581 * to the log tree. An extra reference is taken on any extents in this
4582 * file, allowing us to avoid a whole pile of corner cases around logging
4583 * blocks that have been removed from the tree.
4584 *
4585 * See LOG_INODE_ALL and related defines for a description of what inode_only
4586 * does.
4587 *
4588 * This handles both files and directories.
4589 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004590static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004591 struct btrfs_root *root, struct inode *inode,
4592 int inode_only,
4593 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004594 const loff_t end,
4595 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004596{
4597 struct btrfs_path *path;
4598 struct btrfs_path *dst_path;
4599 struct btrfs_key min_key;
4600 struct btrfs_key max_key;
4601 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004602 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004603 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004604 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004605 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004606 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004607 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004608 int ins_start_slot = 0;
4609 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004610 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004611 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004612 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004613 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004614 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004615
Chris Masone02119d2008-09-05 16:13:11 -04004616 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004617 if (!path)
4618 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004619 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004620 if (!dst_path) {
4621 btrfs_free_path(path);
4622 return -ENOMEM;
4623 }
Chris Masone02119d2008-09-05 16:13:11 -04004624
Li Zefan33345d012011-04-20 10:31:50 +08004625 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004626 min_key.type = BTRFS_INODE_ITEM_KEY;
4627 min_key.offset = 0;
4628
Li Zefan33345d012011-04-20 10:31:50 +08004629 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004630
Chris Mason12fcfd22009-03-24 10:24:20 -04004631
Josef Bacik5dc562c2012-08-17 13:14:17 -04004632 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004633 if (S_ISDIR(inode->i_mode) ||
4634 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4635 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004636 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004637 max_key.type = BTRFS_XATTR_ITEM_KEY;
4638 else
4639 max_key.type = (u8)-1;
4640 max_key.offset = (u64)-1;
4641
Filipe Manana2c2c4522015-01-13 16:40:04 +00004642 /*
4643 * Only run delayed items if we are a dir or a new file.
4644 * Otherwise commit the delayed inode only, which is needed in
4645 * order for the log replay code to mark inodes for link count
4646 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4647 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004648 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004649 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004650 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004651 else
4652 ret = btrfs_commit_inode_delayed_inode(inode);
4653
4654 if (ret) {
4655 btrfs_free_path(path);
4656 btrfs_free_path(dst_path);
4657 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004658 }
4659
Liu Bo67312122016-11-30 16:20:25 -08004660 if (inode_only == LOG_OTHER_INODE) {
4661 inode_only = LOG_INODE_EXISTS;
4662 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4663 SINGLE_DEPTH_NESTING);
4664 } else {
4665 mutex_lock(&BTRFS_I(inode)->log_mutex);
4666 }
Chris Masone02119d2008-09-05 16:13:11 -04004667
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004668 /*
Chris Masone02119d2008-09-05 16:13:11 -04004669 * a brute force approach to making sure we get the most uptodate
4670 * copies of everything.
4671 */
4672 if (S_ISDIR(inode->i_mode)) {
4673 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4674
Filipe Manana4f764e52015-02-23 19:53:35 +00004675 if (inode_only == LOG_INODE_EXISTS)
4676 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004677 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004678 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004679 if (inode_only == LOG_INODE_EXISTS) {
4680 /*
4681 * Make sure the new inode item we write to the log has
4682 * the same isize as the current one (if it exists).
4683 * This is necessary to prevent data loss after log
4684 * replay, and also to prevent doing a wrong expanding
4685 * truncate - for e.g. create file, write 4K into offset
4686 * 0, fsync, write 4K into offset 4096, add hard link,
4687 * fsync some other file (to sync log), power fail - if
4688 * we use the inode's current i_size, after log replay
4689 * we get a 8Kb file, with the last 4Kb extent as a hole
4690 * (zeroes), as if an expanding truncate happened,
4691 * instead of getting a file of 4Kb only.
4692 */
4693 err = logged_inode_size(log, inode, path,
4694 &logged_isize);
4695 if (err)
4696 goto out_unlock;
4697 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004698 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4699 &BTRFS_I(inode)->runtime_flags)) {
4700 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004701 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004702 ret = drop_objectid_items(trans, log, path, ino,
4703 max_key.type);
4704 } else {
4705 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4706 &BTRFS_I(inode)->runtime_flags);
4707 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4708 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004709 while(1) {
4710 ret = btrfs_truncate_inode_items(trans,
4711 log, inode, 0, 0);
4712 if (ret != -EAGAIN)
4713 break;
4714 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004715 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004716 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4717 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004718 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004719 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004720 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004721 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004722 ret = drop_objectid_items(trans, log, path, ino,
4723 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004724 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004725 if (inode_only == LOG_INODE_ALL)
4726 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004727 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004728 }
Josef Bacika95249b2012-10-11 16:17:34 -04004729
Chris Masone02119d2008-09-05 16:13:11 -04004730 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004731 if (ret) {
4732 err = ret;
4733 goto out_unlock;
4734 }
Chris Masone02119d2008-09-05 16:13:11 -04004735
Chris Masond3977122009-01-05 21:25:51 -05004736 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004737 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004738 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004739 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004740 if (ret < 0) {
4741 err = ret;
4742 goto out_unlock;
4743 }
Chris Masone02119d2008-09-05 16:13:11 -04004744 if (ret != 0)
4745 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004746again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004747 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004748 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004749 break;
4750 if (min_key.type > max_key.type)
4751 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004752
Filipe Mananae4545de2015-06-17 12:49:23 +01004753 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4754 need_log_inode_item = false;
4755
Filipe Manana56f23fd2016-03-30 23:37:21 +01004756 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4757 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4758 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004759 u64 other_ino = 0;
4760
Filipe Manana56f23fd2016-03-30 23:37:21 +01004761 ret = btrfs_check_ref_name_override(path->nodes[0],
4762 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004763 &min_key, inode,
4764 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004765 if (ret < 0) {
4766 err = ret;
4767 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004768 } else if (ret > 0 && ctx &&
4769 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004770 struct btrfs_key inode_key;
4771 struct inode *other_inode;
4772
4773 if (ins_nr > 0) {
4774 ins_nr++;
4775 } else {
4776 ins_nr = 1;
4777 ins_start_slot = path->slots[0];
4778 }
4779 ret = copy_items(trans, inode, dst_path, path,
4780 &last_extent, ins_start_slot,
4781 ins_nr, inode_only,
4782 logged_isize);
4783 if (ret < 0) {
4784 err = ret;
4785 goto out_unlock;
4786 }
4787 ins_nr = 0;
4788 btrfs_release_path(path);
4789 inode_key.objectid = other_ino;
4790 inode_key.type = BTRFS_INODE_ITEM_KEY;
4791 inode_key.offset = 0;
4792 other_inode = btrfs_iget(root->fs_info->sb,
4793 &inode_key, root,
4794 NULL);
4795 /*
4796 * If the other inode that had a conflicting dir
4797 * entry was deleted in the current transaction,
4798 * we don't need to do more work nor fallback to
4799 * a transaction commit.
4800 */
4801 if (IS_ERR(other_inode) &&
4802 PTR_ERR(other_inode) == -ENOENT) {
4803 goto next_key;
4804 } else if (IS_ERR(other_inode)) {
4805 err = PTR_ERR(other_inode);
4806 goto out_unlock;
4807 }
4808 /*
4809 * We are safe logging the other inode without
4810 * acquiring its i_mutex as long as we log with
4811 * the LOG_INODE_EXISTS mode. We're safe against
4812 * concurrent renames of the other inode as well
4813 * because during a rename we pin the log and
4814 * update the log with the new name before we
4815 * unpin it.
4816 */
4817 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004818 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004819 0, LLONG_MAX, ctx);
4820 iput(other_inode);
4821 if (err)
4822 goto out_unlock;
4823 else
4824 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004825 }
4826 }
4827
Filipe Manana36283bf2015-06-20 00:44:51 +01004828 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4829 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4830 if (ins_nr == 0)
4831 goto next_slot;
4832 ret = copy_items(trans, inode, dst_path, path,
4833 &last_extent, ins_start_slot,
4834 ins_nr, inode_only, logged_isize);
4835 if (ret < 0) {
4836 err = ret;
4837 goto out_unlock;
4838 }
4839 ins_nr = 0;
4840 if (ret) {
4841 btrfs_release_path(path);
4842 continue;
4843 }
4844 goto next_slot;
4845 }
4846
Chris Masone02119d2008-09-05 16:13:11 -04004847 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004848 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4849 ins_nr++;
4850 goto next_slot;
4851 } else if (!ins_nr) {
4852 ins_start_slot = path->slots[0];
4853 ins_nr = 1;
4854 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004855 }
4856
Josef Bacik16e75492013-10-22 12:18:51 -04004857 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004858 ins_start_slot, ins_nr, inode_only,
4859 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004860 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004861 err = ret;
4862 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004863 }
4864 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004865 ins_nr = 0;
4866 btrfs_release_path(path);
4867 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004868 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004869 ins_nr = 1;
4870 ins_start_slot = path->slots[0];
4871next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004872
Chris Mason3a5f1d42008-09-11 15:53:37 -04004873 nritems = btrfs_header_nritems(path->nodes[0]);
4874 path->slots[0]++;
4875 if (path->slots[0] < nritems) {
4876 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4877 path->slots[0]);
4878 goto again;
4879 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004880 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004881 ret = copy_items(trans, inode, dst_path, path,
4882 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004883 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004884 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004885 err = ret;
4886 goto out_unlock;
4887 }
Josef Bacik16e75492013-10-22 12:18:51 -04004888 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004889 ins_nr = 0;
4890 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004891 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004892next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004893 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004894 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004895 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004896 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004897 min_key.offset = 0;
4898 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004899 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004900 }
Chris Masone02119d2008-09-05 16:13:11 -04004901 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004902 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004903 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004904 ins_start_slot, ins_nr, inode_only,
4905 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004906 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004907 err = ret;
4908 goto out_unlock;
4909 }
Josef Bacik16e75492013-10-22 12:18:51 -04004910 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004911 ins_nr = 0;
4912 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004913
Filipe Manana36283bf2015-06-20 00:44:51 +01004914 btrfs_release_path(path);
4915 btrfs_release_path(dst_path);
4916 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4917 if (err)
4918 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004919 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4920 btrfs_release_path(path);
4921 btrfs_release_path(dst_path);
4922 err = btrfs_log_trailing_hole(trans, root, inode, path);
4923 if (err)
4924 goto out_unlock;
4925 }
Josef Bacika95249b2012-10-11 16:17:34 -04004926log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004927 btrfs_release_path(path);
4928 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004929 if (need_log_inode_item) {
4930 err = log_inode_item(trans, log, dst_path, inode);
4931 if (err)
4932 goto out_unlock;
4933 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004934 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004935 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004936 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004937 if (ret) {
4938 err = ret;
4939 goto out_unlock;
4940 }
Josef Bacikd006a042013-11-12 20:54:09 -05004941 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004942 struct extent_map *em, *n;
4943
Filipe Manana49dae1b2014-09-06 22:34:39 +01004944 write_lock(&em_tree->lock);
4945 /*
4946 * We can't just remove every em if we're called for a ranged
4947 * fsync - that is, one that doesn't cover the whole possible
4948 * file range (0 to LLONG_MAX). This is because we can have
4949 * em's that fall outside the range we're logging and therefore
4950 * their ordered operations haven't completed yet
4951 * (btrfs_finish_ordered_io() not invoked yet). This means we
4952 * didn't get their respective file extent item in the fs/subvol
4953 * tree yet, and need to let the next fast fsync (one which
4954 * consults the list of modified extent maps) find the em so
4955 * that it logs a matching file extent item and waits for the
4956 * respective ordered operation to complete (if it's still
4957 * running).
4958 *
4959 * Removing every em outside the range we're logging would make
4960 * the next fast fsync not log their matching file extent items,
4961 * therefore making us lose data after a log replay.
4962 */
4963 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4964 list) {
4965 const u64 mod_end = em->mod_start + em->mod_len - 1;
4966
4967 if (em->mod_start >= start && mod_end <= end)
4968 list_del_init(&em->list);
4969 }
4970 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004971 }
4972
Chris Mason9623f9a2008-09-11 17:42:42 -04004973 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004974 ret = log_directory_changes(trans, root, inode, path, dst_path,
4975 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004976 if (ret) {
4977 err = ret;
4978 goto out_unlock;
4979 }
Chris Masone02119d2008-09-05 16:13:11 -04004980 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004981
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004982 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004983 BTRFS_I(inode)->logged_trans = trans->transid;
4984 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004985 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004986out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004987 if (unlikely(err))
4988 btrfs_put_logged_extents(&logged_list);
4989 else
4990 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004991 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4992
4993 btrfs_free_path(path);
4994 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004995 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004996}
4997
Chris Mason12fcfd22009-03-24 10:24:20 -04004998/*
Filipe Manana2be63d52016-02-12 11:34:23 +00004999 * Check if we must fallback to a transaction commit when logging an inode.
5000 * This must be called after logging the inode and is used only in the context
5001 * when fsyncing an inode requires the need to log some other inode - in which
5002 * case we can't lock the i_mutex of each other inode we need to log as that
5003 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5004 * log inodes up or down in the hierarchy) or rename operations for example. So
5005 * we take the log_mutex of the inode after we have logged it and then check for
5006 * its last_unlink_trans value - this is safe because any task setting
5007 * last_unlink_trans must take the log_mutex and it must do this before it does
5008 * the actual unlink operation, so if we do this check before a concurrent task
5009 * sets last_unlink_trans it means we've logged a consistent version/state of
5010 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005011 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005012 * we logged the inode or it might have also done the unlink).
5013 */
5014static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5015 struct inode *inode)
5016{
5017 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5018 bool ret = false;
5019
5020 mutex_lock(&BTRFS_I(inode)->log_mutex);
5021 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5022 /*
5023 * Make sure any commits to the log are forced to be full
5024 * commits.
5025 */
5026 btrfs_set_log_full_commit(fs_info, trans);
5027 ret = true;
5028 }
5029 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5030
5031 return ret;
5032}
5033
5034/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005035 * follow the dentry parent pointers up the chain and see if any
5036 * of the directories in it require a full commit before they can
5037 * be logged. Returns zero if nothing special needs to be done or 1 if
5038 * a full commit is required.
5039 */
5040static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5041 struct inode *inode,
5042 struct dentry *parent,
5043 struct super_block *sb,
5044 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005045{
Chris Mason12fcfd22009-03-24 10:24:20 -04005046 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005047 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005048 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005049
Chris Masonaf4176b2009-03-24 10:24:31 -04005050 /*
5051 * for regular files, if its inode is already on disk, we don't
5052 * have to worry about the parents at all. This is because
5053 * we can use the last_unlink_trans field to record renames
5054 * and other fun in this file.
5055 */
5056 if (S_ISREG(inode->i_mode) &&
5057 BTRFS_I(inode)->generation <= last_committed &&
5058 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5059 goto out;
5060
Chris Mason12fcfd22009-03-24 10:24:20 -04005061 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005062 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005063 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005064 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005065 }
5066
5067 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005068 /*
5069 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005070 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005071 * logged_trans so that further down in the log code we don't
5072 * think this inode has already been logged.
5073 */
5074 if (inode != orig_inode)
5075 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005076 smp_mb();
5077
Filipe Manana2be63d52016-02-12 11:34:23 +00005078 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005079 ret = 1;
5080 break;
5081 }
5082
Al Virofc640052016-04-10 01:33:30 -04005083 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005084 break;
5085
Filipe Manana44f714d2016-06-06 16:11:13 +01005086 if (IS_ROOT(parent)) {
5087 inode = d_inode(parent);
5088 if (btrfs_must_commit_transaction(trans, inode))
5089 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005090 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005091 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005092
Josef Bacik6a912212010-11-20 09:48:00 +00005093 parent = dget_parent(parent);
5094 dput(old_parent);
5095 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005096 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005097
5098 }
Josef Bacik6a912212010-11-20 09:48:00 +00005099 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005100out:
Chris Masone02119d2008-09-05 16:13:11 -04005101 return ret;
5102}
5103
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005104struct btrfs_dir_list {
5105 u64 ino;
5106 struct list_head list;
5107};
5108
5109/*
5110 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5111 * details about the why it is needed.
5112 * This is a recursive operation - if an existing dentry corresponds to a
5113 * directory, that directory's new entries are logged too (same behaviour as
5114 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5115 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5116 * complains about the following circular lock dependency / possible deadlock:
5117 *
5118 * CPU0 CPU1
5119 * ---- ----
5120 * lock(&type->i_mutex_dir_key#3/2);
5121 * lock(sb_internal#2);
5122 * lock(&type->i_mutex_dir_key#3/2);
5123 * lock(&sb->s_type->i_mutex_key#14);
5124 *
5125 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5126 * sb_start_intwrite() in btrfs_start_transaction().
5127 * Not locking i_mutex of the inodes is still safe because:
5128 *
5129 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5130 * that while logging the inode new references (names) are added or removed
5131 * from the inode, leaving the logged inode item with a link count that does
5132 * not match the number of logged inode reference items. This is fine because
5133 * at log replay time we compute the real number of links and correct the
5134 * link count in the inode item (see replay_one_buffer() and
5135 * link_to_fixup_dir());
5136 *
5137 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5138 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5139 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5140 * has a size that doesn't match the sum of the lengths of all the logged
5141 * names. This does not result in a problem because if a dir_item key is
5142 * logged but its matching dir_index key is not logged, at log replay time we
5143 * don't use it to replay the respective name (see replay_one_name()). On the
5144 * other hand if only the dir_index key ends up being logged, the respective
5145 * name is added to the fs/subvol tree with both the dir_item and dir_index
5146 * keys created (see replay_one_name()).
5147 * The directory's inode item with a wrong i_size is not a problem as well,
5148 * since we don't use it at log replay time to set the i_size in the inode
5149 * item of the fs/subvol tree (see overwrite_item()).
5150 */
5151static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5152 struct btrfs_root *root,
5153 struct inode *start_inode,
5154 struct btrfs_log_ctx *ctx)
5155{
5156 struct btrfs_root *log = root->log_root;
5157 struct btrfs_path *path;
5158 LIST_HEAD(dir_list);
5159 struct btrfs_dir_list *dir_elem;
5160 int ret = 0;
5161
5162 path = btrfs_alloc_path();
5163 if (!path)
5164 return -ENOMEM;
5165
5166 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5167 if (!dir_elem) {
5168 btrfs_free_path(path);
5169 return -ENOMEM;
5170 }
5171 dir_elem->ino = btrfs_ino(start_inode);
5172 list_add_tail(&dir_elem->list, &dir_list);
5173
5174 while (!list_empty(&dir_list)) {
5175 struct extent_buffer *leaf;
5176 struct btrfs_key min_key;
5177 int nritems;
5178 int i;
5179
5180 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5181 list);
5182 if (ret)
5183 goto next_dir_inode;
5184
5185 min_key.objectid = dir_elem->ino;
5186 min_key.type = BTRFS_DIR_ITEM_KEY;
5187 min_key.offset = 0;
5188again:
5189 btrfs_release_path(path);
5190 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5191 if (ret < 0) {
5192 goto next_dir_inode;
5193 } else if (ret > 0) {
5194 ret = 0;
5195 goto next_dir_inode;
5196 }
5197
5198process_leaf:
5199 leaf = path->nodes[0];
5200 nritems = btrfs_header_nritems(leaf);
5201 for (i = path->slots[0]; i < nritems; i++) {
5202 struct btrfs_dir_item *di;
5203 struct btrfs_key di_key;
5204 struct inode *di_inode;
5205 struct btrfs_dir_list *new_dir_elem;
5206 int log_mode = LOG_INODE_EXISTS;
5207 int type;
5208
5209 btrfs_item_key_to_cpu(leaf, &min_key, i);
5210 if (min_key.objectid != dir_elem->ino ||
5211 min_key.type != BTRFS_DIR_ITEM_KEY)
5212 goto next_dir_inode;
5213
5214 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5215 type = btrfs_dir_type(leaf, di);
5216 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5217 type != BTRFS_FT_DIR)
5218 continue;
5219 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5220 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5221 continue;
5222
Robbie Ko664b0532016-10-28 10:48:26 +08005223 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005224 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5225 root, NULL);
5226 if (IS_ERR(di_inode)) {
5227 ret = PTR_ERR(di_inode);
5228 goto next_dir_inode;
5229 }
5230
5231 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5232 iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005233 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005234 }
5235
5236 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005237 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005238 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005239 ret = btrfs_log_inode(trans, root, di_inode,
5240 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005241 if (!ret &&
5242 btrfs_must_commit_transaction(trans, di_inode))
5243 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005244 iput(di_inode);
5245 if (ret)
5246 goto next_dir_inode;
5247 if (ctx->log_new_dentries) {
5248 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5249 GFP_NOFS);
5250 if (!new_dir_elem) {
5251 ret = -ENOMEM;
5252 goto next_dir_inode;
5253 }
5254 new_dir_elem->ino = di_key.objectid;
5255 list_add_tail(&new_dir_elem->list, &dir_list);
5256 }
5257 break;
5258 }
5259 if (i == nritems) {
5260 ret = btrfs_next_leaf(log, path);
5261 if (ret < 0) {
5262 goto next_dir_inode;
5263 } else if (ret > 0) {
5264 ret = 0;
5265 goto next_dir_inode;
5266 }
5267 goto process_leaf;
5268 }
5269 if (min_key.offset < (u64)-1) {
5270 min_key.offset++;
5271 goto again;
5272 }
5273next_dir_inode:
5274 list_del(&dir_elem->list);
5275 kfree(dir_elem);
5276 }
5277
5278 btrfs_free_path(path);
5279 return ret;
5280}
5281
Filipe Manana18aa0922015-08-05 16:49:08 +01005282static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5283 struct inode *inode,
5284 struct btrfs_log_ctx *ctx)
5285{
5286 int ret;
5287 struct btrfs_path *path;
5288 struct btrfs_key key;
5289 struct btrfs_root *root = BTRFS_I(inode)->root;
5290 const u64 ino = btrfs_ino(inode);
5291
5292 path = btrfs_alloc_path();
5293 if (!path)
5294 return -ENOMEM;
5295 path->skip_locking = 1;
5296 path->search_commit_root = 1;
5297
5298 key.objectid = ino;
5299 key.type = BTRFS_INODE_REF_KEY;
5300 key.offset = 0;
5301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5302 if (ret < 0)
5303 goto out;
5304
5305 while (true) {
5306 struct extent_buffer *leaf = path->nodes[0];
5307 int slot = path->slots[0];
5308 u32 cur_offset = 0;
5309 u32 item_size;
5310 unsigned long ptr;
5311
5312 if (slot >= btrfs_header_nritems(leaf)) {
5313 ret = btrfs_next_leaf(root, path);
5314 if (ret < 0)
5315 goto out;
5316 else if (ret > 0)
5317 break;
5318 continue;
5319 }
5320
5321 btrfs_item_key_to_cpu(leaf, &key, slot);
5322 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5323 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5324 break;
5325
5326 item_size = btrfs_item_size_nr(leaf, slot);
5327 ptr = btrfs_item_ptr_offset(leaf, slot);
5328 while (cur_offset < item_size) {
5329 struct btrfs_key inode_key;
5330 struct inode *dir_inode;
5331
5332 inode_key.type = BTRFS_INODE_ITEM_KEY;
5333 inode_key.offset = 0;
5334
5335 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5336 struct btrfs_inode_extref *extref;
5337
5338 extref = (struct btrfs_inode_extref *)
5339 (ptr + cur_offset);
5340 inode_key.objectid = btrfs_inode_extref_parent(
5341 leaf, extref);
5342 cur_offset += sizeof(*extref);
5343 cur_offset += btrfs_inode_extref_name_len(leaf,
5344 extref);
5345 } else {
5346 inode_key.objectid = key.offset;
5347 cur_offset = item_size;
5348 }
5349
5350 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5351 root, NULL);
5352 /* If parent inode was deleted, skip it. */
5353 if (IS_ERR(dir_inode))
5354 continue;
5355
Filipe Manana657ed1a2016-04-06 17:11:56 +01005356 if (ctx)
5357 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005358 ret = btrfs_log_inode(trans, root, dir_inode,
5359 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005360 if (!ret &&
5361 btrfs_must_commit_transaction(trans, dir_inode))
5362 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005363 if (!ret && ctx && ctx->log_new_dentries)
5364 ret = log_new_dir_dentries(trans, root,
5365 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005366 iput(dir_inode);
5367 if (ret)
5368 goto out;
5369 }
5370 path->slots[0]++;
5371 }
5372 ret = 0;
5373out:
5374 btrfs_free_path(path);
5375 return ret;
5376}
5377
Chris Masone02119d2008-09-05 16:13:11 -04005378/*
5379 * helper function around btrfs_log_inode to make sure newly created
5380 * parent directories also end up in the log. A minimal inode and backref
5381 * only logging is done of any parent directories that are older than
5382 * the last committed transaction
5383 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005384static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5385 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005386 struct dentry *parent,
5387 const loff_t start,
5388 const loff_t end,
5389 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005390 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005391{
Chris Mason12fcfd22009-03-24 10:24:20 -04005392 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005393 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005394 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005395 int ret = 0;
5396 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005397 bool log_dentries = false;
5398 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005399
5400 sb = inode->i_sb;
5401
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005402 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005403 ret = 1;
5404 goto end_no_trans;
5405 }
5406
Miao Xie995946d2014-04-02 19:51:06 +08005407 /*
5408 * The prev transaction commit doesn't complete, we need do
5409 * full commit by ourselves.
5410 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005411 if (root->fs_info->last_trans_log_full_commit >
5412 root->fs_info->last_trans_committed) {
5413 ret = 1;
5414 goto end_no_trans;
5415 }
5416
Yan, Zheng76dda932009-09-21 16:00:26 -04005417 if (root != BTRFS_I(inode)->root ||
5418 btrfs_root_refs(&root->root_item) == 0) {
5419 ret = 1;
5420 goto end_no_trans;
5421 }
5422
Chris Mason12fcfd22009-03-24 10:24:20 -04005423 ret = check_parent_dirs_for_sync(trans, inode, parent,
5424 sb, last_committed);
5425 if (ret)
5426 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005427
Josef Bacik22ee6982012-05-29 16:57:49 -04005428 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005429 ret = BTRFS_NO_LOG_SYNC;
5430 goto end_no_trans;
5431 }
5432
Miao Xie8b050d32014-02-20 18:08:58 +08005433 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005434 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005435 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005436
Filipe Manana8407f552014-09-05 15:14:39 +01005437 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005438 if (ret)
5439 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005440
Chris Masonaf4176b2009-03-24 10:24:31 -04005441 /*
5442 * for regular files, if its inode is already on disk, we don't
5443 * have to worry about the parents at all. This is because
5444 * we can use the last_unlink_trans field to record renames
5445 * and other fun in this file.
5446 */
5447 if (S_ISREG(inode->i_mode) &&
5448 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005449 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5450 ret = 0;
5451 goto end_trans;
5452 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005453
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005454 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5455 log_dentries = true;
5456
Filipe Manana18aa0922015-08-05 16:49:08 +01005457 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005458 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005459 * inodes are fully logged. This is to prevent leaving dangling
5460 * directory index entries in directories that were our parents but are
5461 * not anymore. Not doing this results in old parent directory being
5462 * impossible to delete after log replay (rmdir will always fail with
5463 * error -ENOTEMPTY).
5464 *
5465 * Example 1:
5466 *
5467 * mkdir testdir
5468 * touch testdir/foo
5469 * ln testdir/foo testdir/bar
5470 * sync
5471 * unlink testdir/bar
5472 * xfs_io -c fsync testdir/foo
5473 * <power failure>
5474 * mount fs, triggers log replay
5475 *
5476 * If we don't log the parent directory (testdir), after log replay the
5477 * directory still has an entry pointing to the file inode using the bar
5478 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5479 * the file inode has a link count of 1.
5480 *
5481 * Example 2:
5482 *
5483 * mkdir testdir
5484 * touch foo
5485 * ln foo testdir/foo2
5486 * ln foo testdir/foo3
5487 * sync
5488 * unlink testdir/foo3
5489 * xfs_io -c fsync foo
5490 * <power failure>
5491 * mount fs, triggers log replay
5492 *
5493 * Similar as the first example, after log replay the parent directory
5494 * testdir still has an entry pointing to the inode file with name foo3
5495 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5496 * and has a link count of 2.
5497 */
5498 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5499 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5500 if (ret)
5501 goto end_trans;
5502 }
5503
Chris Masond3977122009-01-05 21:25:51 -05005504 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005505 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005506 break;
5507
David Howells2b0143b2015-03-17 22:25:59 +00005508 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005509 if (root != BTRFS_I(inode)->root)
5510 break;
5511
Filipe Manana18aa0922015-08-05 16:49:08 +01005512 if (BTRFS_I(inode)->generation > last_committed) {
5513 ret = btrfs_log_inode(trans, root, inode,
5514 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005515 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005516 if (ret)
5517 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005518 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005519 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005520 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005521
Josef Bacik6a912212010-11-20 09:48:00 +00005522 parent = dget_parent(parent);
5523 dput(old_parent);
5524 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005525 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005526 if (log_dentries)
5527 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5528 else
5529 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005530end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005531 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005532 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005533 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005534 ret = 1;
5535 }
Miao Xie8b050d32014-02-20 18:08:58 +08005536
5537 if (ret)
5538 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005539 btrfs_end_log_trans(root);
5540end_no_trans:
5541 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005542}
5543
5544/*
5545 * it is not safe to log dentry if the chunk root has added new
5546 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5547 * If this returns 1, you must commit the transaction to safely get your
5548 * data on disk.
5549 */
5550int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005551 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005552 const loff_t start,
5553 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005554 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005555{
Josef Bacik6a912212010-11-20 09:48:00 +00005556 struct dentry *parent = dget_parent(dentry);
5557 int ret;
5558
David Howells2b0143b2015-03-17 22:25:59 +00005559 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005560 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005561 dput(parent);
5562
5563 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005564}
5565
5566/*
5567 * should be called during mount to recover any replay any log trees
5568 * from the FS
5569 */
5570int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5571{
5572 int ret;
5573 struct btrfs_path *path;
5574 struct btrfs_trans_handle *trans;
5575 struct btrfs_key key;
5576 struct btrfs_key found_key;
5577 struct btrfs_key tmp_key;
5578 struct btrfs_root *log;
5579 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5580 struct walk_control wc = {
5581 .process_func = process_one_buffer,
5582 .stage = 0,
5583 };
5584
Chris Masone02119d2008-09-05 16:13:11 -04005585 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005586 if (!path)
5587 return -ENOMEM;
5588
Josef Bacikafcdd122016-09-02 15:40:02 -04005589 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005590
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005591 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005592 if (IS_ERR(trans)) {
5593 ret = PTR_ERR(trans);
5594 goto error;
5595 }
Chris Masone02119d2008-09-05 16:13:11 -04005596
5597 wc.trans = trans;
5598 wc.pin = 1;
5599
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005600 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005601 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005602 btrfs_handle_fs_error(fs_info, ret,
5603 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005604 goto error;
5605 }
Chris Masone02119d2008-09-05 16:13:11 -04005606
5607again:
5608 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5609 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005610 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005611
Chris Masond3977122009-01-05 21:25:51 -05005612 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005613 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005614
5615 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005616 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005617 "Couldn't find tree log root.");
5618 goto error;
5619 }
Chris Masone02119d2008-09-05 16:13:11 -04005620 if (ret > 0) {
5621 if (path->slots[0] == 0)
5622 break;
5623 path->slots[0]--;
5624 }
5625 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5626 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005627 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005628 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5629 break;
5630
Miao Xiecb517ea2013-05-15 07:48:19 +00005631 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005632 if (IS_ERR(log)) {
5633 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005634 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635 "Couldn't read tree log root.");
5636 goto error;
5637 }
Chris Masone02119d2008-09-05 16:13:11 -04005638
5639 tmp_key.objectid = found_key.offset;
5640 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5641 tmp_key.offset = (u64)-1;
5642
5643 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005644 if (IS_ERR(wc.replay_dest)) {
5645 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005646 free_extent_buffer(log->node);
5647 free_extent_buffer(log->commit_root);
5648 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005649 btrfs_handle_fs_error(fs_info, ret,
5650 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005651 goto error;
5652 }
Chris Masone02119d2008-09-05 16:13:11 -04005653
Yan Zheng07d400a2009-01-06 11:42:00 -05005654 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005655 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005656 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005657
Josef Bacikb50c6e22013-04-25 15:55:30 -04005658 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005659 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5660 path);
Chris Masone02119d2008-09-05 16:13:11 -04005661 }
Chris Masone02119d2008-09-05 16:13:11 -04005662
5663 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005664 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005665 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005666 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005667 kfree(log);
5668
Josef Bacikb50c6e22013-04-25 15:55:30 -04005669 if (ret)
5670 goto error;
5671
Chris Masone02119d2008-09-05 16:13:11 -04005672 if (found_key.offset == 0)
5673 break;
5674 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005675 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005676
5677 /* step one is to pin it all, step two is to replay just inodes */
5678 if (wc.pin) {
5679 wc.pin = 0;
5680 wc.process_func = replay_one_buffer;
5681 wc.stage = LOG_WALK_REPLAY_INODES;
5682 goto again;
5683 }
5684 /* step three is to replay everything */
5685 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5686 wc.stage++;
5687 goto again;
5688 }
5689
5690 btrfs_free_path(path);
5691
Josef Bacikabefa552013-04-24 16:40:05 -04005692 /* step 4: commit the transaction, which also unpins the blocks */
5693 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5694 if (ret)
5695 return ret;
5696
Chris Masone02119d2008-09-05 16:13:11 -04005697 free_extent_buffer(log_root_tree->node);
5698 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005699 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005700 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005701
Josef Bacikabefa552013-04-24 16:40:05 -04005702 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005703error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005704 if (wc.trans)
5705 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005706 btrfs_free_path(path);
5707 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005708}
Chris Mason12fcfd22009-03-24 10:24:20 -04005709
5710/*
5711 * there are some corner cases where we want to force a full
5712 * commit instead of allowing a directory to be logged.
5713 *
5714 * They revolve around files there were unlinked from the directory, and
5715 * this function updates the parent directory so that a full commit is
5716 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005717 *
5718 * Must be called before the unlink operations (updates to the subvolume tree,
5719 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005720 */
5721void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5722 struct inode *dir, struct inode *inode,
5723 int for_rename)
5724{
5725 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005726 * when we're logging a file, if it hasn't been renamed
5727 * or unlinked, and its inode is fully committed on disk,
5728 * we don't have to worry about walking up the directory chain
5729 * to log its parents.
5730 *
5731 * So, we use the last_unlink_trans field to put this transid
5732 * into the file. When the file is logged we check it and
5733 * don't log the parents if the file is fully on disk.
5734 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005735 mutex_lock(&BTRFS_I(inode)->log_mutex);
5736 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5737 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005738
5739 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005740 * if this directory was already logged any new
5741 * names for this file/dir will get recorded
5742 */
5743 smp_mb();
5744 if (BTRFS_I(dir)->logged_trans == trans->transid)
5745 return;
5746
5747 /*
5748 * if the inode we're about to unlink was logged,
5749 * the log will be properly updated for any new names
5750 */
5751 if (BTRFS_I(inode)->logged_trans == trans->transid)
5752 return;
5753
5754 /*
5755 * when renaming files across directories, if the directory
5756 * there we're unlinking from gets fsync'd later on, there's
5757 * no way to find the destination directory later and fsync it
5758 * properly. So, we have to be conservative and force commits
5759 * so the new name gets discovered.
5760 */
5761 if (for_rename)
5762 goto record;
5763
5764 /* we can safely do the unlink without any special recording */
5765 return;
5766
5767record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005768 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005769 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005770 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005771}
5772
5773/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005774 * Make sure that if someone attempts to fsync the parent directory of a deleted
5775 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5776 * that after replaying the log tree of the parent directory's root we will not
5777 * see the snapshot anymore and at log replay time we will not see any log tree
5778 * corresponding to the deleted snapshot's root, which could lead to replaying
5779 * it after replaying the log tree of the parent directory (which would replay
5780 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005781 *
5782 * Must be called before the actual snapshot destroy operation (updates to the
5783 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005784 */
5785void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5786 struct inode *dir)
5787{
Filipe Manana2be63d52016-02-12 11:34:23 +00005788 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005789 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005790 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005791}
5792
5793/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005794 * Call this after adding a new name for a file and it will properly
5795 * update the log to reflect the new name.
5796 *
5797 * It will return zero if all goes well, and it will return 1 if a
5798 * full transaction commit is required.
5799 */
5800int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5801 struct inode *inode, struct inode *old_dir,
5802 struct dentry *parent)
5803{
5804 struct btrfs_root * root = BTRFS_I(inode)->root;
5805
5806 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005807 * this will force the logging code to walk the dentry chain
5808 * up for the file
5809 */
5810 if (S_ISREG(inode->i_mode))
5811 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5812
5813 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005814 * if this inode hasn't been logged and directory we're renaming it
5815 * from hasn't been logged, we don't need to log it
5816 */
5817 if (BTRFS_I(inode)->logged_trans <=
5818 root->fs_info->last_trans_committed &&
5819 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5820 root->fs_info->last_trans_committed))
5821 return 0;
5822
Filipe Manana49dae1b2014-09-06 22:34:39 +01005823 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5824 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005825}
5826