blob: 62dd138018b57ce6040bda5eb59a748b294eff21 [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 Bo781feef2016-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,
Nikolay Borisova59108a2017-01-18 00:31:48 +0200100 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100101 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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400146 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800147 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500148
149 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800150
Yan Zheng7237f182009-01-21 12:54:03 -0500151 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400152 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800153 ret = -EAGAIN;
154 goto out;
155 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800156
Josef Bacikff782e02009-10-08 15:30:04 -0400157 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800158 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800159 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400160 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800161 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400162 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800163 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400164 mutex_lock(&fs_info->tree_log_mutex);
165 if (!fs_info->log_root_tree)
166 ret = btrfs_init_log_root_tree(trans, fs_info);
167 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800168 if (ret)
169 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400170
Chris Masone02119d2008-09-05 16:13:11 -0400171 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400172 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800173 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
175 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
176 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400177 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800178
Miao Xie2ecb7922012-09-06 04:04:27 -0600179 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500180 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800181 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800182 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800184 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800185 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800186
Miao Xiee87ac132014-02-20 18:08:53 +0800187out:
Yan Zheng7237f182009-01-21 12:54:03 -0500188 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400190}
191
192/*
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
195 * in progress
196 */
197static int join_running_log_trans(struct btrfs_root *root)
198{
199 int ret = -ENOENT;
200
201 smp_mb();
202 if (!root->log_root)
203 return -ENOENT;
204
Yan Zheng7237f182009-01-21 12:54:03 -0500205 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 if (root->log_root) {
207 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500208 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400209 }
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 return ret;
212}
213
214/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
218 */
219int btrfs_pin_log_trans(struct btrfs_root *root)
220{
221 int ret = -ENOENT;
222
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
226 return ret;
227}
228
229/*
Chris Masone02119d2008-09-05 16:13:11 -0400230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400234{
Yan Zheng7237f182009-01-21 12:54:03 -0500235 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100236 /*
237 * Implicit memory barrier after atomic_dec_and_test
238 */
Yan Zheng7237f182009-01-21 12:54:03 -0500239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
241 }
Chris Masone02119d2008-09-05 16:13:11 -0400242}
243
244
245/*
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
250 */
251struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
254 */
255 int free;
256
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
259 */
260 int write;
261
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
264 */
265 int wait;
266
267 /* pin only walk, we record which extents on disk belong to the
268 * log trees
269 */
270 int pin;
271
272 /* what stage of the replay code we're currently in */
273 int stage;
274
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
277
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
280
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
284 * inside it
285 */
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
288};
289
290/*
291 * process_func used to pin down extents, write them or wait on them
292 */
293static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400298 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400299
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400300 /*
301 * If this fs is mixed then we need to be able to process the leaves to
302 * pin down any logged extents, so we have to read the block.
303 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400305 ret = btrfs_read_buffer(eb, gen);
306 if (ret)
307 return ret;
308 }
309
Josef Bacikb50c6e22013-04-25 15:55:30 -0400310 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400311 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
312 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400313
314 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400315 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400316 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400317 if (wc->write)
318 btrfs_write_tree_block(eb);
319 if (wc->wait)
320 btrfs_wait_tree_block_writeback(eb);
321 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400322 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400323}
324
325/*
326 * Item overwrite used by replay and tree logging. eb, slot and key all refer
327 * to the src data we are copying out.
328 *
329 * root is the tree we are copying into, and path is a scratch
330 * path for use in this function (it should be released on entry and
331 * will be released on exit).
332 *
333 * If the key is already in the destination tree the existing item is
334 * overwritten. If the existing item isn't big enough, it is extended.
335 * If it is too large, it is truncated.
336 *
337 * If the key isn't in the destination yet, a new item is inserted.
338 */
339static noinline int overwrite_item(struct btrfs_trans_handle *trans,
340 struct btrfs_root *root,
341 struct btrfs_path *path,
342 struct extent_buffer *eb, int slot,
343 struct btrfs_key *key)
344{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400345 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400346 int ret;
347 u32 item_size;
348 u64 saved_i_size = 0;
349 int save_old_i_size = 0;
350 unsigned long src_ptr;
351 unsigned long dst_ptr;
352 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000353 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400354
355 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
356 overwrite_root = 1;
357
358 item_size = btrfs_item_size_nr(eb, slot);
359 src_ptr = btrfs_item_ptr_offset(eb, slot);
360
361 /* look for the key in the destination tree */
362 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000363 if (ret < 0)
364 return ret;
365
Chris Masone02119d2008-09-05 16:13:11 -0400366 if (ret == 0) {
367 char *src_copy;
368 char *dst_copy;
369 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
370 path->slots[0]);
371 if (dst_size != item_size)
372 goto insert;
373
374 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200375 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400376 return 0;
377 }
378 dst_copy = kmalloc(item_size, GFP_NOFS);
379 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000380 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200381 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000382 kfree(dst_copy);
383 kfree(src_copy);
384 return -ENOMEM;
385 }
Chris Masone02119d2008-09-05 16:13:11 -0400386
387 read_extent_buffer(eb, src_copy, src_ptr, item_size);
388
389 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
390 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
391 item_size);
392 ret = memcmp(dst_copy, src_copy, item_size);
393
394 kfree(dst_copy);
395 kfree(src_copy);
396 /*
397 * they have the same contents, just return, this saves
398 * us from cowing blocks in the destination tree and doing
399 * extra writes that may not have been done by a previous
400 * sync
401 */
402 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200403 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400404 return 0;
405 }
406
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000407 /*
408 * We need to load the old nbytes into the inode so when we
409 * replay the extents we've logged we get the right nbytes.
410 */
411 if (inode_item) {
412 struct btrfs_inode_item *item;
413 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400414 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000415
416 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
417 struct btrfs_inode_item);
418 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
419 item = btrfs_item_ptr(eb, slot,
420 struct btrfs_inode_item);
421 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400422
423 /*
424 * If this is a directory we need to reset the i_size to
425 * 0 so that we can set it up properly when replaying
426 * the rest of the items in this log.
427 */
428 mode = btrfs_inode_mode(eb, item);
429 if (S_ISDIR(mode))
430 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000431 }
432 } else if (inode_item) {
433 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400434 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000435
436 /*
437 * New inode, set nbytes to 0 so that the nbytes comes out
438 * properly when we replay the extents.
439 */
440 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
441 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400442
443 /*
444 * If this is a directory we need to reset the i_size to 0 so
445 * that we can set it up properly when replaying the rest of
446 * the items in this log.
447 */
448 mode = btrfs_inode_mode(eb, item);
449 if (S_ISDIR(mode))
450 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400451 }
452insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200453 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400454 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000455 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400456 ret = btrfs_insert_empty_item(trans, root, path,
457 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000458 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400459
460 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000461 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400462 u32 found_size;
463 found_size = btrfs_item_size_nr(path->nodes[0],
464 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400466 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400468 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100469 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400470 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400471 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400472 }
473 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
474 path->slots[0]);
475
476 /* don't overwrite an existing inode if the generation number
477 * was logged as zero. This is done when the tree logging code
478 * is just logging an inode to make sure it exists after recovery.
479 *
480 * Also, don't overwrite i_size on directories during replay.
481 * log replay inserts and removes directory items based on the
482 * state of the tree found in the subvolume, and i_size is modified
483 * as it goes
484 */
485 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
486 struct btrfs_inode_item *src_item;
487 struct btrfs_inode_item *dst_item;
488
489 src_item = (struct btrfs_inode_item *)src_ptr;
490 dst_item = (struct btrfs_inode_item *)dst_ptr;
491
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492 if (btrfs_inode_generation(eb, src_item) == 0) {
493 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000494 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000495
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000496 /*
497 * For regular files an ino_size == 0 is used only when
498 * logging that an inode exists, as part of a directory
499 * fsync, and the inode wasn't fsynced before. In this
500 * case don't set the size of the inode in the fs/subvol
501 * tree, otherwise we would be throwing valid data away.
502 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000503 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000504 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
505 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000506 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000507
508 btrfs_init_map_token(&token);
509 btrfs_set_token_inode_size(dst_eb, dst_item,
510 ino_size, &token);
511 }
Chris Masone02119d2008-09-05 16:13:11 -0400512 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000513 }
Chris Masone02119d2008-09-05 16:13:11 -0400514
515 if (overwrite_root &&
516 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
517 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
518 save_old_i_size = 1;
519 saved_i_size = btrfs_inode_size(path->nodes[0],
520 dst_item);
521 }
522 }
523
524 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
525 src_ptr, item_size);
526
527 if (save_old_i_size) {
528 struct btrfs_inode_item *dst_item;
529 dst_item = (struct btrfs_inode_item *)dst_ptr;
530 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
531 }
532
533 /* make sure the generation is filled in */
534 if (key->type == BTRFS_INODE_ITEM_KEY) {
535 struct btrfs_inode_item *dst_item;
536 dst_item = (struct btrfs_inode_item *)dst_ptr;
537 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
538 btrfs_set_inode_generation(path->nodes[0], dst_item,
539 trans->transid);
540 }
541 }
542no_copy:
543 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200544 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400545 return 0;
546}
547
548/*
549 * simple helper to read an inode off the disk from a given root
550 * This can only be called for subvolume roots and not for the log
551 */
552static noinline struct inode *read_one_inode(struct btrfs_root *root,
553 u64 objectid)
554{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400555 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400556 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400557
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 key.objectid = objectid;
559 key.type = BTRFS_INODE_ITEM_KEY;
560 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000561 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400562 if (IS_ERR(inode)) {
563 inode = NULL;
564 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400565 iput(inode);
566 inode = NULL;
567 }
568 return inode;
569}
570
571/* replays a single extent in 'eb' at 'slot' with 'key' into the
572 * subvolume 'root'. path is released on entry and should be released
573 * on exit.
574 *
575 * extents in the log tree have not been allocated out of the extent
576 * tree yet. So, this completes the allocation, taking a reference
577 * as required if the extent already exists or creating a new extent
578 * if it isn't in the extent allocation tree yet.
579 *
580 * The extent is inserted into the file, dropping any existing extents
581 * from the file that overlap the new one.
582 */
583static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
584 struct btrfs_root *root,
585 struct btrfs_path *path,
586 struct extent_buffer *eb, int slot,
587 struct btrfs_key *key)
588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400589 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400590 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400591 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400592 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000593 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400594 struct btrfs_file_extent_item *item;
595 struct inode *inode = NULL;
596 unsigned long size;
597 int ret = 0;
598
599 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
600 found_type = btrfs_file_extent_type(eb, item);
601
Yan Zhengd899e052008-10-30 14:25:28 -0400602 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000603 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
604 nbytes = btrfs_file_extent_num_bytes(eb, item);
605 extent_end = start + nbytes;
606
607 /*
608 * We don't add to the inodes nbytes if we are prealloc or a
609 * hole.
610 */
611 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
612 nbytes = 0;
613 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800614 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000615 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400616 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400617 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400618 } else {
619 ret = 0;
620 goto out;
621 }
622
623 inode = read_one_inode(root, key->objectid);
624 if (!inode) {
625 ret = -EIO;
626 goto out;
627 }
628
629 /*
630 * first check to see if we already have this extent in the
631 * file. This must be done before the btrfs_drop_extents run
632 * so we don't try to drop this extent.
633 */
David Sterbaf85b7372017-01-20 14:54:07 +0100634 ret = btrfs_lookup_file_extent(trans, root, path,
635 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400636
Yan Zhengd899e052008-10-30 14:25:28 -0400637 if (ret == 0 &&
638 (found_type == BTRFS_FILE_EXTENT_REG ||
639 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400640 struct btrfs_file_extent_item cmp1;
641 struct btrfs_file_extent_item cmp2;
642 struct btrfs_file_extent_item *existing;
643 struct extent_buffer *leaf;
644
645 leaf = path->nodes[0];
646 existing = btrfs_item_ptr(leaf, path->slots[0],
647 struct btrfs_file_extent_item);
648
649 read_extent_buffer(eb, &cmp1, (unsigned long)item,
650 sizeof(cmp1));
651 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
652 sizeof(cmp2));
653
654 /*
655 * we already have a pointer to this exact extent,
656 * we don't have to do anything
657 */
658 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200659 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400660 goto out;
661 }
662 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200663 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400664
665 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400666 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400667 if (ret)
668 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400669
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 if (found_type == BTRFS_FILE_EXTENT_REG ||
671 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500673 unsigned long dest_offset;
674 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400675
Yan Zheng07d400a2009-01-06 11:42:00 -0500676 ret = btrfs_insert_empty_item(trans, root, path, key,
677 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400678 if (ret)
679 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500680 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
681 path->slots[0]);
682 copy_extent_buffer(path->nodes[0], eb, dest_offset,
683 (unsigned long)item, sizeof(*item));
684
685 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
686 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
687 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500689
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800690 /*
691 * Manually record dirty extent, as here we did a shallow
692 * file extent item copy and skip normal backref update,
693 * but modifying extent tree all by ourselves.
694 * So need to manually record dirty extent for qgroup,
695 * as the owner of the file extent changed from log tree
696 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
697 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400698 ret = btrfs_qgroup_trace_extent(trans, fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800699 btrfs_file_extent_disk_bytenr(eb, item),
700 btrfs_file_extent_disk_num_bytes(eb, item),
701 GFP_NOFS);
702 if (ret < 0)
703 goto out;
704
Yan Zheng07d400a2009-01-06 11:42:00 -0500705 if (ins.objectid > 0) {
706 u64 csum_start;
707 u64 csum_end;
708 LIST_HEAD(ordered_sums);
709 /*
710 * is this extent already allocated in the extent
711 * allocation tree? If so, just add a reference
712 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400713 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500714 ins.offset);
715 if (ret == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400716 ret = btrfs_inc_extent_ref(trans, fs_info,
Yan Zheng07d400a2009-01-06 11:42:00 -0500717 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100719 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400720 if (ret)
721 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500722 } else {
723 /*
724 * insert the extent pointer in the extent
725 * allocation tree
726 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400727 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400728 fs_info,
729 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400730 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400731 if (ret)
732 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500733 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200734 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500735
736 if (btrfs_file_extent_compression(eb, item)) {
737 csum_start = ins.objectid;
738 csum_end = csum_start + ins.offset;
739 } else {
740 csum_start = ins.objectid +
741 btrfs_file_extent_offset(eb, item);
742 csum_end = csum_start +
743 btrfs_file_extent_num_bytes(eb, item);
744 }
745
746 ret = btrfs_lookup_csums_range(root->log_root,
747 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100748 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400749 if (ret)
750 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100751 /*
752 * Now delete all existing cums in the csum root that
753 * cover our range. We do this because we can have an
754 * extent that is completely referenced by one file
755 * extent item and partially referenced by another
756 * file extent item (like after using the clone or
757 * extent_same ioctls). In this case if we end up doing
758 * the replay of the one that partially references the
759 * extent first, and we do not do the csum deletion
760 * below, we can get 2 csum items in the csum tree that
761 * overlap each other. For example, imagine our log has
762 * the two following file extent items:
763 *
764 * key (257 EXTENT_DATA 409600)
765 * extent data disk byte 12845056 nr 102400
766 * extent data offset 20480 nr 20480 ram 102400
767 *
768 * key (257 EXTENT_DATA 819200)
769 * extent data disk byte 12845056 nr 102400
770 * extent data offset 0 nr 102400 ram 102400
771 *
772 * Where the second one fully references the 100K extent
773 * that starts at disk byte 12845056, and the log tree
774 * has a single csum item that covers the entire range
775 * of the extent:
776 *
777 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
778 *
779 * After the first file extent item is replayed, the
780 * csum tree gets the following csum item:
781 *
782 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
783 *
784 * Which covers the 20K sub-range starting at offset 20K
785 * of our extent. Now when we replay the second file
786 * extent item, if we do not delete existing csum items
787 * that cover any of its blocks, we end up getting two
788 * csum items in our csum tree that overlap each other:
789 *
790 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
791 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
792 *
793 * Which is a problem, because after this anyone trying
794 * to lookup up for the checksum of any block of our
795 * extent starting at an offset of 40K or higher, will
796 * end up looking at the second csum item only, which
797 * does not contain the checksum for any block starting
798 * at offset 40K or higher of our extent.
799 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500800 while (!list_empty(&ordered_sums)) {
801 struct btrfs_ordered_sum *sums;
802 sums = list_entry(ordered_sums.next,
803 struct btrfs_ordered_sum,
804 list);
Josef Bacik36508602013-04-25 16:23:32 -0400805 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400806 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400807 sums->bytenr,
808 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100809 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400810 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400811 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500812 list_del(&sums->list);
813 kfree(sums);
814 }
Josef Bacik36508602013-04-25 16:23:32 -0400815 if (ret)
816 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500817 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200818 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500819 }
820 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
821 /* inline extents are easy, we just overwrite them */
822 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400823 if (ret)
824 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500825 }
826
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000827 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600828 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400829out:
830 if (inode)
831 iput(inode);
832 return ret;
833}
834
835/*
836 * when cleaning up conflicts between the directory names in the
837 * subvolume, directory names in the log and directory names in the
838 * inode back references, we may have to unlink inodes from directories.
839 *
840 * This is a helper function to do the unlink of a specific directory
841 * item
842 */
843static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
844 struct btrfs_root *root,
845 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200846 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400847 struct btrfs_dir_item *di)
848{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400849 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400850 struct inode *inode;
851 char *name;
852 int name_len;
853 struct extent_buffer *leaf;
854 struct btrfs_key location;
855 int ret;
856
857 leaf = path->nodes[0];
858
859 btrfs_dir_item_key_to_cpu(leaf, di, &location);
860 name_len = btrfs_dir_name_len(leaf, di);
861 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000862 if (!name)
863 return -ENOMEM;
864
Chris Masone02119d2008-09-05 16:13:11 -0400865 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200866 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400867
868 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000869 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400870 ret = -EIO;
871 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000872 }
Chris Masone02119d2008-09-05 16:13:11 -0400873
Yan Zhengec051c02009-01-05 15:43:42 -0500874 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400875 if (ret)
876 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400877
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200878 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
879 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400880 if (ret)
881 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100882 else
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400883 ret = btrfs_run_delayed_items(trans, fs_info);
Josef Bacik36508602013-04-25 16:23:32 -0400884out:
885 kfree(name);
886 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400887 return ret;
888}
889
890/*
891 * helper function to see if a given name and sequence number found
892 * in an inode back reference are already in a directory and correctly
893 * point to this inode
894 */
895static noinline int inode_in_dir(struct btrfs_root *root,
896 struct btrfs_path *path,
897 u64 dirid, u64 objectid, u64 index,
898 const char *name, int name_len)
899{
900 struct btrfs_dir_item *di;
901 struct btrfs_key location;
902 int match = 0;
903
904 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
905 index, name, name_len, 0);
906 if (di && !IS_ERR(di)) {
907 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
908 if (location.objectid != objectid)
909 goto out;
910 } else
911 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200912 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400913
914 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
915 if (di && !IS_ERR(di)) {
916 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
917 if (location.objectid != objectid)
918 goto out;
919 } else
920 goto out;
921 match = 1;
922out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200923 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400924 return match;
925}
926
927/*
928 * helper function to check a log tree for a named back reference in
929 * an inode. This is used to decide if a back reference that is
930 * found in the subvolume conflicts with what we find in the log.
931 *
932 * inode backreferences may have multiple refs in a single item,
933 * during replay we process one reference at a time, and we don't
934 * want to delete valid links to a file from the subvolume if that
935 * link is also in the log.
936 */
937static noinline int backref_in_log(struct btrfs_root *log,
938 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700939 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000940 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400941{
942 struct btrfs_path *path;
943 struct btrfs_inode_ref *ref;
944 unsigned long ptr;
945 unsigned long ptr_end;
946 unsigned long name_ptr;
947 int found_name_len;
948 int item_size;
949 int ret;
950 int match = 0;
951
952 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000953 if (!path)
954 return -ENOMEM;
955
Chris Masone02119d2008-09-05 16:13:11 -0400956 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
957 if (ret != 0)
958 goto out;
959
Chris Masone02119d2008-09-05 16:13:11 -0400960 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700961
962 if (key->type == BTRFS_INODE_EXTREF_KEY) {
963 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
964 name, namelen, NULL))
965 match = 1;
966
967 goto out;
968 }
969
970 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400971 ptr_end = ptr + item_size;
972 while (ptr < ptr_end) {
973 ref = (struct btrfs_inode_ref *)ptr;
974 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
975 if (found_name_len == namelen) {
976 name_ptr = (unsigned long)(ref + 1);
977 ret = memcmp_extent_buffer(path->nodes[0], name,
978 name_ptr, namelen);
979 if (ret == 0) {
980 match = 1;
981 goto out;
982 }
983 }
984 ptr = (unsigned long)(ref + 1) + found_name_len;
985 }
986out:
987 btrfs_free_path(path);
988 return match;
989}
990
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700991static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
992 struct btrfs_root *root,
993 struct btrfs_path *path,
994 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200995 struct btrfs_inode *dir,
996 struct btrfs_inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700997 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700998 u64 inode_objectid, u64 parent_objectid,
999 u64 ref_index, char *name, int namelen,
1000 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001001{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001002 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001004 char *victim_name;
1005 int victim_name_len;
1006 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001008 struct btrfs_key search_key;
1009 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010
Mark Fashehf1863732012-08-08 11:32:27 -07001011again:
1012 /* Search old style refs */
1013 search_key.objectid = inode_objectid;
1014 search_key.type = BTRFS_INODE_REF_KEY;
1015 search_key.offset = parent_objectid;
1016 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001017 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001018 struct btrfs_inode_ref *victim_ref;
1019 unsigned long ptr;
1020 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001021
1022 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001023
1024 /* are we trying to overwrite a back ref for the root directory
1025 * if so, just jump out, we're done
1026 */
Mark Fashehf1863732012-08-08 11:32:27 -07001027 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001028 return 1;
1029
1030 /* check all the names in this back reference to see
1031 * if they are in the log. if so, we allow them to stay
1032 * otherwise they must be unlinked as a conflict
1033 */
1034 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1035 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1036 while (ptr < ptr_end) {
1037 victim_ref = (struct btrfs_inode_ref *)ptr;
1038 victim_name_len = btrfs_inode_ref_name_len(leaf,
1039 victim_ref);
1040 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001041 if (!victim_name)
1042 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001043
1044 read_extent_buffer(leaf, victim_name,
1045 (unsigned long)(victim_ref + 1),
1046 victim_name_len);
1047
Mark Fashehf1863732012-08-08 11:32:27 -07001048 if (!backref_in_log(log_root, &search_key,
1049 parent_objectid,
1050 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001051 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001052 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001053 btrfs_release_path(path);
1054
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001055 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001056 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001057 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001058 if (ret)
1059 return ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001060 ret = btrfs_run_delayed_items(trans, fs_info);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001061 if (ret)
1062 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001063 *search_done = 1;
1064 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001065 }
1066 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001067
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001068 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1069 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001070
1071 /*
1072 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001073 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001074 */
1075 *search_done = 1;
1076 }
1077 btrfs_release_path(path);
1078
Mark Fashehf1863732012-08-08 11:32:27 -07001079 /* Same search but for extended refs */
1080 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1081 inode_objectid, parent_objectid, 0,
1082 0);
1083 if (!IS_ERR_OR_NULL(extref)) {
1084 u32 item_size;
1085 u32 cur_offset = 0;
1086 unsigned long base;
1087 struct inode *victim_parent;
1088
1089 leaf = path->nodes[0];
1090
1091 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1092 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1093
1094 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001095 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001096
1097 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1098
1099 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1100 goto next;
1101
1102 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001103 if (!victim_name)
1104 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001105 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1106 victim_name_len);
1107
1108 search_key.objectid = inode_objectid;
1109 search_key.type = BTRFS_INODE_EXTREF_KEY;
1110 search_key.offset = btrfs_extref_hash(parent_objectid,
1111 victim_name,
1112 victim_name_len);
1113 ret = 0;
1114 if (!backref_in_log(log_root, &search_key,
1115 parent_objectid, victim_name,
1116 victim_name_len)) {
1117 ret = -ENOENT;
1118 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001119 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001120 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001121 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001122 btrfs_release_path(path);
1123
1124 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001125 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001126 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001127 victim_name,
1128 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001129 if (!ret)
1130 ret = btrfs_run_delayed_items(
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001131 trans,
1132 fs_info);
Mark Fashehf1863732012-08-08 11:32:27 -07001133 }
Mark Fashehf1863732012-08-08 11:32:27 -07001134 iput(victim_parent);
1135 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001136 if (ret)
1137 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001138 *search_done = 1;
1139 goto again;
1140 }
1141 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001142 if (ret)
1143 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001144next:
1145 cur_offset += victim_name_len + sizeof(*extref);
1146 }
1147 *search_done = 1;
1148 }
1149 btrfs_release_path(path);
1150
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001151 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001152 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001153 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001154 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001155 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001156 if (ret)
1157 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001158 }
1159 btrfs_release_path(path);
1160
1161 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001162 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001163 name, namelen, 0);
1164 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001165 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001166 if (ret)
1167 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001168 }
1169 btrfs_release_path(path);
1170
1171 return 0;
1172}
Chris Masone02119d2008-09-05 16:13:11 -04001173
Mark Fashehf1863732012-08-08 11:32:27 -07001174static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1175 u32 *namelen, char **name, u64 *index,
1176 u64 *parent_objectid)
1177{
1178 struct btrfs_inode_extref *extref;
1179
1180 extref = (struct btrfs_inode_extref *)ref_ptr;
1181
1182 *namelen = btrfs_inode_extref_name_len(eb, extref);
1183 *name = kmalloc(*namelen, GFP_NOFS);
1184 if (*name == NULL)
1185 return -ENOMEM;
1186
1187 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1188 *namelen);
1189
1190 *index = btrfs_inode_extref_index(eb, extref);
1191 if (parent_objectid)
1192 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1193
1194 return 0;
1195}
1196
1197static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1198 u32 *namelen, char **name, u64 *index)
1199{
1200 struct btrfs_inode_ref *ref;
1201
1202 ref = (struct btrfs_inode_ref *)ref_ptr;
1203
1204 *namelen = btrfs_inode_ref_name_len(eb, ref);
1205 *name = kmalloc(*namelen, GFP_NOFS);
1206 if (*name == NULL)
1207 return -ENOMEM;
1208
1209 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1210
1211 *index = btrfs_inode_ref_index(eb, ref);
1212
1213 return 0;
1214}
1215
Chris Masone02119d2008-09-05 16:13:11 -04001216/*
1217 * replay one inode back reference item found in the log tree.
1218 * eb, slot and key refer to the buffer and key found in the log tree.
1219 * root is the destination we are replaying into, and path is for temp
1220 * use by this function. (it should be released on return).
1221 */
1222static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1223 struct btrfs_root *root,
1224 struct btrfs_root *log,
1225 struct btrfs_path *path,
1226 struct extent_buffer *eb, int slot,
1227 struct btrfs_key *key)
1228{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001229 struct inode *dir = NULL;
1230 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001231 unsigned long ref_ptr;
1232 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001233 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001234 int namelen;
1235 int ret;
liuboc622ae62011-03-26 08:01:12 -04001236 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001237 int log_ref_ver = 0;
1238 u64 parent_objectid;
1239 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001240 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001241 int ref_struct_size;
1242
1243 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1244 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1245
1246 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1247 struct btrfs_inode_extref *r;
1248
1249 ref_struct_size = sizeof(struct btrfs_inode_extref);
1250 log_ref_ver = 1;
1251 r = (struct btrfs_inode_extref *)ref_ptr;
1252 parent_objectid = btrfs_inode_extref_parent(eb, r);
1253 } else {
1254 ref_struct_size = sizeof(struct btrfs_inode_ref);
1255 parent_objectid = key->offset;
1256 }
1257 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001258
Chris Masone02119d2008-09-05 16:13:11 -04001259 /*
1260 * it is possible that we didn't log all the parent directories
1261 * for a given inode. If we don't find the dir, just don't
1262 * copy the back ref in. The link count fixup code will take
1263 * care of the rest
1264 */
Mark Fashehf1863732012-08-08 11:32:27 -07001265 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001266 if (!dir) {
1267 ret = -ENOENT;
1268 goto out;
1269 }
Chris Masone02119d2008-09-05 16:13:11 -04001270
Mark Fashehf1863732012-08-08 11:32:27 -07001271 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001272 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001273 ret = -EIO;
1274 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001275 }
Chris Masone02119d2008-09-05 16:13:11 -04001276
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001277 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001278 if (log_ref_ver) {
1279 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1280 &ref_index, &parent_objectid);
1281 /*
1282 * parent object can change from one array
1283 * item to another.
1284 */
1285 if (!dir)
1286 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001287 if (!dir) {
1288 ret = -ENOENT;
1289 goto out;
1290 }
Mark Fashehf1863732012-08-08 11:32:27 -07001291 } else {
1292 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1293 &ref_index);
1294 }
1295 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001296 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001297
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001298 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001299 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1300 btrfs_ino(BTRFS_I(inode)), ref_index,
1301 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001302 /*
1303 * look for a conflicting back reference in the
1304 * metadata. if we find one we have to unlink that name
1305 * of the file before we add our new link. Later on, we
1306 * overwrite any existing back reference, and we don't
1307 * want to create dangling pointers in the directory.
1308 */
Chris Masone02119d2008-09-05 16:13:11 -04001309
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001310 if (!search_done) {
1311 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001312 BTRFS_I(dir),
1313 BTRFS_I(inode), eb,
Mark Fashehf1863732012-08-08 11:32:27 -07001314 inode_objectid,
1315 parent_objectid,
1316 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001317 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001318 if (ret) {
1319 if (ret == 1)
1320 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001321 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001322 }
Chris Masone02119d2008-09-05 16:13:11 -04001323 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001324
1325 /* insert our name */
1326 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001327 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001328 if (ret)
1329 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001330
1331 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001332 }
liuboc622ae62011-03-26 08:01:12 -04001333
Mark Fashehf1863732012-08-08 11:32:27 -07001334 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001335 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001336 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001337 if (log_ref_ver) {
1338 iput(dir);
1339 dir = NULL;
1340 }
Chris Masone02119d2008-09-05 16:13:11 -04001341 }
Chris Masone02119d2008-09-05 16:13:11 -04001342
1343 /* finally write the back reference in the inode */
1344 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001345out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001346 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001347 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001348 iput(dir);
1349 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001350 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001351}
1352
Yan, Zhengc71bf092009-11-12 09:34:40 +00001353static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001354 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001355{
1356 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001357
David Sterba9c4f61f2015-01-02 19:12:57 +01001358 ret = btrfs_insert_orphan_item(trans, root, ino);
1359 if (ret == -EEXIST)
1360 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001361
Yan, Zhengc71bf092009-11-12 09:34:40 +00001362 return ret;
1363}
1364
Mark Fashehf1863732012-08-08 11:32:27 -07001365static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001366 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001367{
Mark Fashehf1863732012-08-08 11:32:27 -07001368 int ret = 0;
1369 int name_len;
1370 unsigned int nlink = 0;
1371 u32 item_size;
1372 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001373 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001374 u64 offset = 0;
1375 unsigned long ptr;
1376 struct btrfs_inode_extref *extref;
1377 struct extent_buffer *leaf;
1378
1379 while (1) {
1380 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1381 &extref, &offset);
1382 if (ret)
1383 break;
1384
1385 leaf = path->nodes[0];
1386 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1387 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001388 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001389
1390 while (cur_offset < item_size) {
1391 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1392 name_len = btrfs_inode_extref_name_len(leaf, extref);
1393
1394 nlink++;
1395
1396 cur_offset += name_len + sizeof(*extref);
1397 }
1398
1399 offset++;
1400 btrfs_release_path(path);
1401 }
1402 btrfs_release_path(path);
1403
Filipe Manana2c2c4522015-01-13 16:40:04 +00001404 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001405 return ret;
1406 return nlink;
1407}
1408
1409static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001410 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001411{
Chris Masone02119d2008-09-05 16:13:11 -04001412 int ret;
1413 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001414 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001415 unsigned long ptr;
1416 unsigned long ptr_end;
1417 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001418 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001419
Li Zefan33345d012011-04-20 10:31:50 +08001420 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001421 key.type = BTRFS_INODE_REF_KEY;
1422 key.offset = (u64)-1;
1423
Chris Masond3977122009-01-05 21:25:51 -05001424 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1426 if (ret < 0)
1427 break;
1428 if (ret > 0) {
1429 if (path->slots[0] == 0)
1430 break;
1431 path->slots[0]--;
1432 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001433process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001434 btrfs_item_key_to_cpu(path->nodes[0], &key,
1435 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001436 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001437 key.type != BTRFS_INODE_REF_KEY)
1438 break;
1439 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1440 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1441 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001442 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001443 struct btrfs_inode_ref *ref;
1444
1445 ref = (struct btrfs_inode_ref *)ptr;
1446 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1447 ref);
1448 ptr = (unsigned long)(ref + 1) + name_len;
1449 nlink++;
1450 }
1451
1452 if (key.offset == 0)
1453 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001454 if (path->slots[0] > 0) {
1455 path->slots[0]--;
1456 goto process_slot;
1457 }
Chris Masone02119d2008-09-05 16:13:11 -04001458 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001460 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001461 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001462
1463 return nlink;
1464}
1465
1466/*
1467 * There are a few corners where the link count of the file can't
1468 * be properly maintained during replay. So, instead of adding
1469 * lots of complexity to the log code, we just scan the backrefs
1470 * for any file that has been through replay.
1471 *
1472 * The scan will update the link count on the inode to reflect the
1473 * number of back refs found. If it goes down to zero, the iput
1474 * will free the inode.
1475 */
1476static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1477 struct btrfs_root *root,
1478 struct inode *inode)
1479{
1480 struct btrfs_path *path;
1481 int ret;
1482 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001483 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001484
1485 path = btrfs_alloc_path();
1486 if (!path)
1487 return -ENOMEM;
1488
Nikolay Borisovf329e312017-01-18 00:31:50 +02001489 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001490 if (ret < 0)
1491 goto out;
1492
1493 nlink = ret;
1494
Nikolay Borisov36283652017-01-18 00:31:49 +02001495 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001496 if (ret < 0)
1497 goto out;
1498
1499 nlink += ret;
1500
1501 ret = 0;
1502
Chris Masone02119d2008-09-05 16:13:11 -04001503 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001504 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001505 btrfs_update_inode(trans, root, inode);
1506 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001507 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001508
Yan, Zhengc71bf092009-11-12 09:34:40 +00001509 if (inode->i_nlink == 0) {
1510 if (S_ISDIR(inode->i_mode)) {
1511 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001512 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001513 if (ret)
1514 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001515 }
Li Zefan33345d012011-04-20 10:31:50 +08001516 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001517 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001518
Mark Fashehf1863732012-08-08 11:32:27 -07001519out:
1520 btrfs_free_path(path);
1521 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001522}
1523
1524static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1525 struct btrfs_root *root,
1526 struct btrfs_path *path)
1527{
1528 int ret;
1529 struct btrfs_key key;
1530 struct inode *inode;
1531
1532 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1533 key.type = BTRFS_ORPHAN_ITEM_KEY;
1534 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001535 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001536 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1537 if (ret < 0)
1538 break;
1539
1540 if (ret == 1) {
1541 if (path->slots[0] == 0)
1542 break;
1543 path->slots[0]--;
1544 }
1545
1546 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1547 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1548 key.type != BTRFS_ORPHAN_ITEM_KEY)
1549 break;
1550
1551 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001552 if (ret)
1553 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001554
David Sterbab3b4aa72011-04-21 01:20:15 +02001555 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001556 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001557 if (!inode)
1558 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001559
1560 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001561 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001562 if (ret)
1563 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001564
Chris Mason12fcfd22009-03-24 10:24:20 -04001565 /*
1566 * fixup on a directory may create new entries,
1567 * make sure we always look for the highset possible
1568 * offset
1569 */
1570 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001571 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001572 ret = 0;
1573out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001574 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001575 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001576}
1577
1578
1579/*
1580 * record a given inode in the fixup dir so we can check its link
1581 * count when replay is done. The link count is incremented here
1582 * so the inode won't go away until we check it
1583 */
1584static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *root,
1586 struct btrfs_path *path,
1587 u64 objectid)
1588{
1589 struct btrfs_key key;
1590 int ret = 0;
1591 struct inode *inode;
1592
1593 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001594 if (!inode)
1595 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001596
1597 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001598 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001599 key.offset = objectid;
1600
1601 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1602
David Sterbab3b4aa72011-04-21 01:20:15 +02001603 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001604 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001605 if (!inode->i_nlink)
1606 set_nlink(inode, 1);
1607 else
Zach Brown8b558c52013-10-16 12:10:34 -07001608 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001609 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001610 } else if (ret == -EEXIST) {
1611 ret = 0;
1612 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001613 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001614 }
1615 iput(inode);
1616
1617 return ret;
1618}
1619
1620/*
1621 * when replaying the log for a directory, we only insert names
1622 * for inodes that actually exist. This means an fsync on a directory
1623 * does not implicitly fsync all the new files in it
1624 */
1625static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1626 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001627 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001628 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001629 struct btrfs_key *location)
1630{
1631 struct inode *inode;
1632 struct inode *dir;
1633 int ret;
1634
1635 inode = read_one_inode(root, location->objectid);
1636 if (!inode)
1637 return -ENOENT;
1638
1639 dir = read_one_inode(root, dirid);
1640 if (!dir) {
1641 iput(inode);
1642 return -EIO;
1643 }
Josef Bacikd5554382013-09-11 14:17:00 -04001644
Chris Masone02119d2008-09-05 16:13:11 -04001645 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1646
1647 /* FIXME, put inode into FIXUP list */
1648
1649 iput(inode);
1650 iput(dir);
1651 return ret;
1652}
1653
1654/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001655 * Return true if an inode reference exists in the log for the given name,
1656 * inode and parent inode.
1657 */
1658static bool name_in_log_ref(struct btrfs_root *log_root,
1659 const char *name, const int name_len,
1660 const u64 dirid, const u64 ino)
1661{
1662 struct btrfs_key search_key;
1663
1664 search_key.objectid = ino;
1665 search_key.type = BTRFS_INODE_REF_KEY;
1666 search_key.offset = dirid;
1667 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1668 return true;
1669
1670 search_key.type = BTRFS_INODE_EXTREF_KEY;
1671 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1672 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1673 return true;
1674
1675 return false;
1676}
1677
1678/*
Chris Masone02119d2008-09-05 16:13:11 -04001679 * take a single entry in a log directory item and replay it into
1680 * the subvolume.
1681 *
1682 * if a conflicting item exists in the subdirectory already,
1683 * the inode it points to is unlinked and put into the link count
1684 * fix up tree.
1685 *
1686 * If a name from the log points to a file or directory that does
1687 * not exist in the FS, it is skipped. fsyncs on directories
1688 * do not force down inodes inside that directory, just changes to the
1689 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001690 *
1691 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1692 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001693 */
1694static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
1696 struct btrfs_path *path,
1697 struct extent_buffer *eb,
1698 struct btrfs_dir_item *di,
1699 struct btrfs_key *key)
1700{
1701 char *name;
1702 int name_len;
1703 struct btrfs_dir_item *dst_di;
1704 struct btrfs_key found_key;
1705 struct btrfs_key log_key;
1706 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001707 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001708 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001709 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001710 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001711 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001712
1713 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001714 if (!dir)
1715 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001716
1717 name_len = btrfs_dir_name_len(eb, di);
1718 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001719 if (!name) {
1720 ret = -ENOMEM;
1721 goto out;
1722 }
liubo2a29edc2011-01-26 06:22:08 +00001723
Chris Masone02119d2008-09-05 16:13:11 -04001724 log_type = btrfs_dir_type(eb, di);
1725 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1726 name_len);
1727
1728 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001729 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1730 if (exists == 0)
1731 exists = 1;
1732 else
1733 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001734 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001735
Chris Masone02119d2008-09-05 16:13:11 -04001736 if (key->type == BTRFS_DIR_ITEM_KEY) {
1737 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1738 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001739 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001740 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1741 key->objectid,
1742 key->offset, name,
1743 name_len, 1);
1744 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001745 /* Corruption */
1746 ret = -EINVAL;
1747 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001748 }
David Sterbac7040052011-04-19 18:00:01 +02001749 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001750 /* we need a sequence number to insert, so we only
1751 * do inserts for the BTRFS_DIR_INDEX_KEY types
1752 */
1753 if (key->type != BTRFS_DIR_INDEX_KEY)
1754 goto out;
1755 goto insert;
1756 }
1757
1758 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1759 /* the existing item matches the logged item */
1760 if (found_key.objectid == log_key.objectid &&
1761 found_key.type == log_key.type &&
1762 found_key.offset == log_key.offset &&
1763 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001764 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001765 goto out;
1766 }
1767
1768 /*
1769 * don't drop the conflicting directory entry if the inode
1770 * for the new entry doesn't exist
1771 */
Chris Mason4bef0842008-09-08 11:18:08 -04001772 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001773 goto out;
1774
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001775 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001776 if (ret)
1777 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001778
1779 if (key->type == BTRFS_DIR_INDEX_KEY)
1780 goto insert;
1781out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001782 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001783 if (!ret && update_size) {
1784 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1785 ret = btrfs_update_inode(trans, root, dir);
1786 }
Chris Masone02119d2008-09-05 16:13:11 -04001787 kfree(name);
1788 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001789 if (!ret && name_added)
1790 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001791 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001792
1793insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001794 if (name_in_log_ref(root->log_root, name, name_len,
1795 key->objectid, log_key.objectid)) {
1796 /* The dentry will be added later. */
1797 ret = 0;
1798 update_size = false;
1799 goto out;
1800 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001801 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001802 ret = insert_one_name(trans, root, key->objectid, key->offset,
1803 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001804 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001805 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001806 if (!ret)
1807 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001808 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001809 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001810 goto out;
1811}
1812
1813/*
1814 * find all the names in a directory item and reconcile them into
1815 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1816 * one name in a directory item, but the same code gets used for
1817 * both directory index types
1818 */
1819static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1820 struct btrfs_root *root,
1821 struct btrfs_path *path,
1822 struct extent_buffer *eb, int slot,
1823 struct btrfs_key *key)
1824{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001825 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001826 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001827 u32 item_size = btrfs_item_size_nr(eb, slot);
1828 struct btrfs_dir_item *di;
1829 int name_len;
1830 unsigned long ptr;
1831 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001832 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001833
1834 ptr = btrfs_item_ptr_offset(eb, slot);
1835 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001836 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001837 di = (struct btrfs_dir_item *)ptr;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001838 if (verify_dir_item(fs_info, eb, di))
Josef Bacik22a94d42011-03-16 16:47:17 -04001839 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001840 name_len = btrfs_dir_name_len(eb, di);
1841 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001842 if (ret < 0)
1843 break;
Chris Masone02119d2008-09-05 16:13:11 -04001844 ptr = (unsigned long)(di + 1);
1845 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001846
1847 /*
1848 * If this entry refers to a non-directory (directories can not
1849 * have a link count > 1) and it was added in the transaction
1850 * that was not committed, make sure we fixup the link count of
1851 * the inode it the entry points to. Otherwise something like
1852 * the following would result in a directory pointing to an
1853 * inode with a wrong link that does not account for this dir
1854 * entry:
1855 *
1856 * mkdir testdir
1857 * touch testdir/foo
1858 * touch testdir/bar
1859 * sync
1860 *
1861 * ln testdir/bar testdir/bar_link
1862 * ln testdir/foo testdir/foo_link
1863 * xfs_io -c "fsync" testdir/bar
1864 *
1865 * <power failure>
1866 *
1867 * mount fs, log replay happens
1868 *
1869 * File foo would remain with a link count of 1 when it has two
1870 * entries pointing to it in the directory testdir. This would
1871 * make it impossible to ever delete the parent directory has
1872 * it would result in stale dentries that can never be deleted.
1873 */
1874 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1875 struct btrfs_key di_key;
1876
1877 if (!fixup_path) {
1878 fixup_path = btrfs_alloc_path();
1879 if (!fixup_path) {
1880 ret = -ENOMEM;
1881 break;
1882 }
1883 }
1884
1885 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1886 ret = link_to_fixup_dir(trans, root, fixup_path,
1887 di_key.objectid);
1888 if (ret)
1889 break;
1890 }
1891 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001892 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001893 btrfs_free_path(fixup_path);
1894 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001895}
1896
1897/*
1898 * directory replay has two parts. There are the standard directory
1899 * items in the log copied from the subvolume, and range items
1900 * created in the log while the subvolume was logged.
1901 *
1902 * The range items tell us which parts of the key space the log
1903 * is authoritative for. During replay, if a key in the subvolume
1904 * directory is in a logged range item, but not actually in the log
1905 * that means it was deleted from the directory before the fsync
1906 * and should be removed.
1907 */
1908static noinline int find_dir_range(struct btrfs_root *root,
1909 struct btrfs_path *path,
1910 u64 dirid, int key_type,
1911 u64 *start_ret, u64 *end_ret)
1912{
1913 struct btrfs_key key;
1914 u64 found_end;
1915 struct btrfs_dir_log_item *item;
1916 int ret;
1917 int nritems;
1918
1919 if (*start_ret == (u64)-1)
1920 return 1;
1921
1922 key.objectid = dirid;
1923 key.type = key_type;
1924 key.offset = *start_ret;
1925
1926 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1927 if (ret < 0)
1928 goto out;
1929 if (ret > 0) {
1930 if (path->slots[0] == 0)
1931 goto out;
1932 path->slots[0]--;
1933 }
1934 if (ret != 0)
1935 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1936
1937 if (key.type != key_type || key.objectid != dirid) {
1938 ret = 1;
1939 goto next;
1940 }
1941 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1942 struct btrfs_dir_log_item);
1943 found_end = btrfs_dir_log_end(path->nodes[0], item);
1944
1945 if (*start_ret >= key.offset && *start_ret <= found_end) {
1946 ret = 0;
1947 *start_ret = key.offset;
1948 *end_ret = found_end;
1949 goto out;
1950 }
1951 ret = 1;
1952next:
1953 /* check the next slot in the tree to see if it is a valid item */
1954 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08001955 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04001956 if (path->slots[0] >= nritems) {
1957 ret = btrfs_next_leaf(root, path);
1958 if (ret)
1959 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001960 }
1961
1962 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1963
1964 if (key.type != key_type || key.objectid != dirid) {
1965 ret = 1;
1966 goto out;
1967 }
1968 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1969 struct btrfs_dir_log_item);
1970 found_end = btrfs_dir_log_end(path->nodes[0], item);
1971 *start_ret = key.offset;
1972 *end_ret = found_end;
1973 ret = 0;
1974out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001975 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001976 return ret;
1977}
1978
1979/*
1980 * this looks for a given directory item in the log. If the directory
1981 * item is not in the log, the item is removed and the inode it points
1982 * to is unlinked
1983 */
1984static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1985 struct btrfs_root *root,
1986 struct btrfs_root *log,
1987 struct btrfs_path *path,
1988 struct btrfs_path *log_path,
1989 struct inode *dir,
1990 struct btrfs_key *dir_key)
1991{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001992 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04001993 int ret;
1994 struct extent_buffer *eb;
1995 int slot;
1996 u32 item_size;
1997 struct btrfs_dir_item *di;
1998 struct btrfs_dir_item *log_di;
1999 int name_len;
2000 unsigned long ptr;
2001 unsigned long ptr_end;
2002 char *name;
2003 struct inode *inode;
2004 struct btrfs_key location;
2005
2006again:
2007 eb = path->nodes[0];
2008 slot = path->slots[0];
2009 item_size = btrfs_item_size_nr(eb, slot);
2010 ptr = btrfs_item_ptr_offset(eb, slot);
2011 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002012 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002013 di = (struct btrfs_dir_item *)ptr;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002014 if (verify_dir_item(fs_info, eb, di)) {
Josef Bacik22a94d42011-03-16 16:47:17 -04002015 ret = -EIO;
2016 goto out;
2017 }
2018
Chris Masone02119d2008-09-05 16:13:11 -04002019 name_len = btrfs_dir_name_len(eb, di);
2020 name = kmalloc(name_len, GFP_NOFS);
2021 if (!name) {
2022 ret = -ENOMEM;
2023 goto out;
2024 }
2025 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2026 name_len);
2027 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002028 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2030 dir_key->objectid,
2031 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002032 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002033 log_di = btrfs_lookup_dir_index_item(trans, log,
2034 log_path,
2035 dir_key->objectid,
2036 dir_key->offset,
2037 name, name_len, 0);
2038 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002039 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002040 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002041 btrfs_release_path(path);
2042 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002043 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002044 if (!inode) {
2045 kfree(name);
2046 return -EIO;
2047 }
Chris Masone02119d2008-09-05 16:13:11 -04002048
2049 ret = link_to_fixup_dir(trans, root,
2050 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002051 if (ret) {
2052 kfree(name);
2053 iput(inode);
2054 goto out;
2055 }
2056
Zach Brown8b558c52013-10-16 12:10:34 -07002057 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002058 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2059 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002060 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002061 ret = btrfs_run_delayed_items(trans, fs_info);
Chris Masone02119d2008-09-05 16:13:11 -04002062 kfree(name);
2063 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002064 if (ret)
2065 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002066
2067 /* there might still be more names under this key
2068 * check and repeat if required
2069 */
2070 ret = btrfs_search_slot(NULL, root, dir_key, path,
2071 0, 0);
2072 if (ret == 0)
2073 goto again;
2074 ret = 0;
2075 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002076 } else if (IS_ERR(log_di)) {
2077 kfree(name);
2078 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002079 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002080 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002081 kfree(name);
2082
2083 ptr = (unsigned long)(di + 1);
2084 ptr += name_len;
2085 }
2086 ret = 0;
2087out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002088 btrfs_release_path(path);
2089 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002090 return ret;
2091}
2092
Filipe Manana4f764e52015-02-23 19:53:35 +00002093static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2094 struct btrfs_root *root,
2095 struct btrfs_root *log,
2096 struct btrfs_path *path,
2097 const u64 ino)
2098{
2099 struct btrfs_key search_key;
2100 struct btrfs_path *log_path;
2101 int i;
2102 int nritems;
2103 int ret;
2104
2105 log_path = btrfs_alloc_path();
2106 if (!log_path)
2107 return -ENOMEM;
2108
2109 search_key.objectid = ino;
2110 search_key.type = BTRFS_XATTR_ITEM_KEY;
2111 search_key.offset = 0;
2112again:
2113 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2114 if (ret < 0)
2115 goto out;
2116process_leaf:
2117 nritems = btrfs_header_nritems(path->nodes[0]);
2118 for (i = path->slots[0]; i < nritems; i++) {
2119 struct btrfs_key key;
2120 struct btrfs_dir_item *di;
2121 struct btrfs_dir_item *log_di;
2122 u32 total_size;
2123 u32 cur;
2124
2125 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2126 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2127 ret = 0;
2128 goto out;
2129 }
2130
2131 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2132 total_size = btrfs_item_size_nr(path->nodes[0], i);
2133 cur = 0;
2134 while (cur < total_size) {
2135 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2136 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2137 u32 this_len = sizeof(*di) + name_len + data_len;
2138 char *name;
2139
2140 name = kmalloc(name_len, GFP_NOFS);
2141 if (!name) {
2142 ret = -ENOMEM;
2143 goto out;
2144 }
2145 read_extent_buffer(path->nodes[0], name,
2146 (unsigned long)(di + 1), name_len);
2147
2148 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2149 name, name_len, 0);
2150 btrfs_release_path(log_path);
2151 if (!log_di) {
2152 /* Doesn't exist in log tree, so delete it. */
2153 btrfs_release_path(path);
2154 di = btrfs_lookup_xattr(trans, root, path, ino,
2155 name, name_len, -1);
2156 kfree(name);
2157 if (IS_ERR(di)) {
2158 ret = PTR_ERR(di);
2159 goto out;
2160 }
2161 ASSERT(di);
2162 ret = btrfs_delete_one_dir_name(trans, root,
2163 path, di);
2164 if (ret)
2165 goto out;
2166 btrfs_release_path(path);
2167 search_key = key;
2168 goto again;
2169 }
2170 kfree(name);
2171 if (IS_ERR(log_di)) {
2172 ret = PTR_ERR(log_di);
2173 goto out;
2174 }
2175 cur += this_len;
2176 di = (struct btrfs_dir_item *)((char *)di + this_len);
2177 }
2178 }
2179 ret = btrfs_next_leaf(root, path);
2180 if (ret > 0)
2181 ret = 0;
2182 else if (ret == 0)
2183 goto process_leaf;
2184out:
2185 btrfs_free_path(log_path);
2186 btrfs_release_path(path);
2187 return ret;
2188}
2189
2190
Chris Masone02119d2008-09-05 16:13:11 -04002191/*
2192 * deletion replay happens before we copy any new directory items
2193 * out of the log or out of backreferences from inodes. It
2194 * scans the log to find ranges of keys that log is authoritative for,
2195 * and then scans the directory to find items in those ranges that are
2196 * not present in the log.
2197 *
2198 * Anything we don't find in the log is unlinked and removed from the
2199 * directory.
2200 */
2201static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root,
2203 struct btrfs_root *log,
2204 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002205 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002206{
2207 u64 range_start;
2208 u64 range_end;
2209 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2210 int ret = 0;
2211 struct btrfs_key dir_key;
2212 struct btrfs_key found_key;
2213 struct btrfs_path *log_path;
2214 struct inode *dir;
2215
2216 dir_key.objectid = dirid;
2217 dir_key.type = BTRFS_DIR_ITEM_KEY;
2218 log_path = btrfs_alloc_path();
2219 if (!log_path)
2220 return -ENOMEM;
2221
2222 dir = read_one_inode(root, dirid);
2223 /* it isn't an error if the inode isn't there, that can happen
2224 * because we replay the deletes before we copy in the inode item
2225 * from the log
2226 */
2227 if (!dir) {
2228 btrfs_free_path(log_path);
2229 return 0;
2230 }
2231again:
2232 range_start = 0;
2233 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002234 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002235 if (del_all)
2236 range_end = (u64)-1;
2237 else {
2238 ret = find_dir_range(log, path, dirid, key_type,
2239 &range_start, &range_end);
2240 if (ret != 0)
2241 break;
2242 }
Chris Masone02119d2008-09-05 16:13:11 -04002243
2244 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002245 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002246 int nritems;
2247 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2248 0, 0);
2249 if (ret < 0)
2250 goto out;
2251
2252 nritems = btrfs_header_nritems(path->nodes[0]);
2253 if (path->slots[0] >= nritems) {
2254 ret = btrfs_next_leaf(root, path);
2255 if (ret)
2256 break;
2257 }
2258 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2259 path->slots[0]);
2260 if (found_key.objectid != dirid ||
2261 found_key.type != dir_key.type)
2262 goto next_type;
2263
2264 if (found_key.offset > range_end)
2265 break;
2266
2267 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002268 log_path, dir,
2269 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002270 if (ret)
2271 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002272 if (found_key.offset == (u64)-1)
2273 break;
2274 dir_key.offset = found_key.offset + 1;
2275 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002276 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002277 if (range_end == (u64)-1)
2278 break;
2279 range_start = range_end + 1;
2280 }
2281
2282next_type:
2283 ret = 0;
2284 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2285 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2286 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002287 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002288 goto again;
2289 }
2290out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002291 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002292 btrfs_free_path(log_path);
2293 iput(dir);
2294 return ret;
2295}
2296
2297/*
2298 * the process_func used to replay items from the log tree. This
2299 * gets called in two different stages. The first stage just looks
2300 * for inodes and makes sure they are all copied into the subvolume.
2301 *
2302 * The second stage copies all the other item types from the log into
2303 * the subvolume. The two stage approach is slower, but gets rid of
2304 * lots of complexity around inodes referencing other inodes that exist
2305 * only in the log (references come from either directory items or inode
2306 * back refs).
2307 */
2308static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2309 struct walk_control *wc, u64 gen)
2310{
2311 int nritems;
2312 struct btrfs_path *path;
2313 struct btrfs_root *root = wc->replay_dest;
2314 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002315 int level;
2316 int i;
2317 int ret;
2318
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002319 ret = btrfs_read_buffer(eb, gen);
2320 if (ret)
2321 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002322
2323 level = btrfs_header_level(eb);
2324
2325 if (level != 0)
2326 return 0;
2327
2328 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002329 if (!path)
2330 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002331
2332 nritems = btrfs_header_nritems(eb);
2333 for (i = 0; i < nritems; i++) {
2334 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002335
2336 /* inode keys are done during the first stage */
2337 if (key.type == BTRFS_INODE_ITEM_KEY &&
2338 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002339 struct btrfs_inode_item *inode_item;
2340 u32 mode;
2341
2342 inode_item = btrfs_item_ptr(eb, i,
2343 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002344 ret = replay_xattr_deletes(wc->trans, root, log,
2345 path, key.objectid);
2346 if (ret)
2347 break;
Chris Masone02119d2008-09-05 16:13:11 -04002348 mode = btrfs_inode_mode(eb, inode_item);
2349 if (S_ISDIR(mode)) {
2350 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002351 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002352 if (ret)
2353 break;
Chris Masone02119d2008-09-05 16:13:11 -04002354 }
2355 ret = overwrite_item(wc->trans, root, path,
2356 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002357 if (ret)
2358 break;
Chris Masone02119d2008-09-05 16:13:11 -04002359
Yan, Zhengc71bf092009-11-12 09:34:40 +00002360 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002361 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002362 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002363 */
2364 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002365 ret = insert_orphan_item(wc->trans, root,
2366 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002367 if (ret)
2368 break;
Chris Masone02119d2008-09-05 16:13:11 -04002369 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002370
Chris Masone02119d2008-09-05 16:13:11 -04002371 ret = link_to_fixup_dir(wc->trans, root,
2372 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002373 if (ret)
2374 break;
Chris Masone02119d2008-09-05 16:13:11 -04002375 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002376
2377 if (key.type == BTRFS_DIR_INDEX_KEY &&
2378 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2379 ret = replay_one_dir_item(wc->trans, root, path,
2380 eb, i, &key);
2381 if (ret)
2382 break;
2383 }
2384
Chris Masone02119d2008-09-05 16:13:11 -04002385 if (wc->stage < LOG_WALK_REPLAY_ALL)
2386 continue;
2387
2388 /* these keys are simply copied */
2389 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2390 ret = overwrite_item(wc->trans, root, path,
2391 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002392 if (ret)
2393 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002394 } else if (key.type == BTRFS_INODE_REF_KEY ||
2395 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002396 ret = add_inode_ref(wc->trans, root, log, path,
2397 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002398 if (ret && ret != -ENOENT)
2399 break;
2400 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002401 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2402 ret = replay_one_extent(wc->trans, root, path,
2403 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002404 if (ret)
2405 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002406 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002407 ret = replay_one_dir_item(wc->trans, root, path,
2408 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002409 if (ret)
2410 break;
Chris Masone02119d2008-09-05 16:13:11 -04002411 }
2412 }
2413 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002414 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002415}
2416
Chris Masond3977122009-01-05 21:25:51 -05002417static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002418 struct btrfs_root *root,
2419 struct btrfs_path *path, int *level,
2420 struct walk_control *wc)
2421{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002422 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002423 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002424 u64 bytenr;
2425 u64 ptr_gen;
2426 struct extent_buffer *next;
2427 struct extent_buffer *cur;
2428 struct extent_buffer *parent;
2429 u32 blocksize;
2430 int ret = 0;
2431
2432 WARN_ON(*level < 0);
2433 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2434
Chris Masond3977122009-01-05 21:25:51 -05002435 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002436 WARN_ON(*level < 0);
2437 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2438 cur = path->nodes[*level];
2439
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302440 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002441
2442 if (path->slots[*level] >=
2443 btrfs_header_nritems(cur))
2444 break;
2445
2446 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2447 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002448 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002449
2450 parent = path->nodes[*level];
2451 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002452
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002453 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002454 if (IS_ERR(next))
2455 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002456
Chris Masone02119d2008-09-05 16:13:11 -04002457 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002458 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002459 if (ret) {
2460 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002461 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002462 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002463
Chris Masone02119d2008-09-05 16:13:11 -04002464 path->slots[*level]++;
2465 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002466 ret = btrfs_read_buffer(next, ptr_gen);
2467 if (ret) {
2468 free_extent_buffer(next);
2469 return ret;
2470 }
Chris Masone02119d2008-09-05 16:13:11 -04002471
Josef Bacik681ae502013-10-07 15:11:00 -04002472 if (trans) {
2473 btrfs_tree_lock(next);
2474 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002475 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002476 btrfs_wait_tree_block_writeback(next);
2477 btrfs_tree_unlock(next);
2478 }
Chris Masone02119d2008-09-05 16:13:11 -04002479
Chris Masone02119d2008-09-05 16:13:11 -04002480 WARN_ON(root_owner !=
2481 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002482 ret = btrfs_free_and_pin_reserved_extent(
2483 fs_info, bytenr,
2484 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002485 if (ret) {
2486 free_extent_buffer(next);
2487 return ret;
2488 }
Chris Masone02119d2008-09-05 16:13:11 -04002489 }
2490 free_extent_buffer(next);
2491 continue;
2492 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002493 ret = btrfs_read_buffer(next, ptr_gen);
2494 if (ret) {
2495 free_extent_buffer(next);
2496 return ret;
2497 }
Chris Masone02119d2008-09-05 16:13:11 -04002498
2499 WARN_ON(*level <= 0);
2500 if (path->nodes[*level-1])
2501 free_extent_buffer(path->nodes[*level-1]);
2502 path->nodes[*level-1] = next;
2503 *level = btrfs_header_level(next);
2504 path->slots[*level] = 0;
2505 cond_resched();
2506 }
2507 WARN_ON(*level < 0);
2508 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2509
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002510 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002511
2512 cond_resched();
2513 return 0;
2514}
2515
Chris Masond3977122009-01-05 21:25:51 -05002516static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002517 struct btrfs_root *root,
2518 struct btrfs_path *path, int *level,
2519 struct walk_control *wc)
2520{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002521 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002522 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002523 int i;
2524 int slot;
2525 int ret;
2526
Chris Masond3977122009-01-05 21:25:51 -05002527 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002528 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002529 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002530 path->slots[i]++;
2531 *level = i;
2532 WARN_ON(*level == 0);
2533 return 0;
2534 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 struct extent_buffer *parent;
2536 if (path->nodes[*level] == root->node)
2537 parent = path->nodes[*level];
2538 else
2539 parent = path->nodes[*level + 1];
2540
2541 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002542 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002543 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002544 if (ret)
2545 return ret;
2546
Chris Masone02119d2008-09-05 16:13:11 -04002547 if (wc->free) {
2548 struct extent_buffer *next;
2549
2550 next = path->nodes[*level];
2551
Josef Bacik681ae502013-10-07 15:11:00 -04002552 if (trans) {
2553 btrfs_tree_lock(next);
2554 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002555 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002556 btrfs_wait_tree_block_writeback(next);
2557 btrfs_tree_unlock(next);
2558 }
Chris Masone02119d2008-09-05 16:13:11 -04002559
Chris Masone02119d2008-09-05 16:13:11 -04002560 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002561 ret = btrfs_free_and_pin_reserved_extent(
2562 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002563 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002564 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002565 if (ret)
2566 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002567 }
2568 free_extent_buffer(path->nodes[*level]);
2569 path->nodes[*level] = NULL;
2570 *level = i + 1;
2571 }
2572 }
2573 return 1;
2574}
2575
2576/*
2577 * drop the reference count on the tree rooted at 'snap'. This traverses
2578 * the tree freeing any blocks that have a ref count of zero after being
2579 * decremented.
2580 */
2581static int walk_log_tree(struct btrfs_trans_handle *trans,
2582 struct btrfs_root *log, struct walk_control *wc)
2583{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002584 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002585 int ret = 0;
2586 int wret;
2587 int level;
2588 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002589 int orig_level;
2590
2591 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002592 if (!path)
2593 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002594
2595 level = btrfs_header_level(log->node);
2596 orig_level = level;
2597 path->nodes[level] = log->node;
2598 extent_buffer_get(log->node);
2599 path->slots[level] = 0;
2600
Chris Masond3977122009-01-05 21:25:51 -05002601 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002602 wret = walk_down_log_tree(trans, log, path, &level, wc);
2603 if (wret > 0)
2604 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002605 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002606 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 goto out;
2608 }
Chris Masone02119d2008-09-05 16:13:11 -04002609
2610 wret = walk_up_log_tree(trans, log, path, &level, wc);
2611 if (wret > 0)
2612 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002614 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 goto out;
2616 }
Chris Masone02119d2008-09-05 16:13:11 -04002617 }
2618
2619 /* was the root node processed? if not, catch it here */
2620 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002621 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002622 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 if (ret)
2624 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002625 if (wc->free) {
2626 struct extent_buffer *next;
2627
2628 next = path->nodes[orig_level];
2629
Josef Bacik681ae502013-10-07 15:11:00 -04002630 if (trans) {
2631 btrfs_tree_lock(next);
2632 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002633 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002634 btrfs_wait_tree_block_writeback(next);
2635 btrfs_tree_unlock(next);
2636 }
Chris Masone02119d2008-09-05 16:13:11 -04002637
Chris Masone02119d2008-09-05 16:13:11 -04002638 WARN_ON(log->root_key.objectid !=
2639 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002640 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2641 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002642 if (ret)
2643 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002644 }
2645 }
2646
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002647out:
Chris Masone02119d2008-09-05 16:13:11 -04002648 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002649 return ret;
2650}
2651
Yan Zheng7237f182009-01-21 12:54:03 -05002652/*
2653 * helper function to update the item for a given subvolumes log root
2654 * in the tree of log roots
2655 */
2656static int update_log_root(struct btrfs_trans_handle *trans,
2657 struct btrfs_root *log)
2658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002660 int ret;
2661
2662 if (log->log_transid == 1) {
2663 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002664 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002665 &log->root_key, &log->root_item);
2666 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002668 &log->root_key, &log->root_item);
2669 }
2670 return ret;
2671}
2672
Zhaolei60d53eb2015-08-17 18:44:46 +08002673static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002674{
2675 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002676 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002677
Yan Zheng7237f182009-01-21 12:54:03 -05002678 /*
2679 * we only allow two pending log transactions at a time,
2680 * so we know that if ours is more than 2 older than the
2681 * current transaction, we're done
2682 */
Chris Masone02119d2008-09-05 16:13:11 -04002683 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002684 prepare_to_wait(&root->log_commit_wait[index],
2685 &wait, TASK_UNINTERRUPTIBLE);
2686 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002687
Miao Xied1433de2014-02-20 18:08:59 +08002688 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002689 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002690 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002691
Yan Zheng7237f182009-01-21 12:54:03 -05002692 finish_wait(&root->log_commit_wait[index], &wait);
2693 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002694 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002695 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002696}
2697
Zhaolei60d53eb2015-08-17 18:44:46 +08002698static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002699{
2700 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002701
2702 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002703 prepare_to_wait(&root->log_writer_wait,
2704 &wait, TASK_UNINTERRUPTIBLE);
2705 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002706 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002707 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002708 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002709 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002710 }
Chris Masone02119d2008-09-05 16:13:11 -04002711}
2712
Miao Xie8b050d32014-02-20 18:08:58 +08002713static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2714 struct btrfs_log_ctx *ctx)
2715{
2716 if (!ctx)
2717 return;
2718
2719 mutex_lock(&root->log_mutex);
2720 list_del_init(&ctx->list);
2721 mutex_unlock(&root->log_mutex);
2722}
2723
2724/*
2725 * Invoked in log mutex context, or be sure there is no other task which
2726 * can access the list.
2727 */
2728static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2729 int index, int error)
2730{
2731 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002732 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002733
Chris Mason570dd452016-10-27 10:42:20 -07002734 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2735 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002736 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002737 }
Miao Xie8b050d32014-02-20 18:08:58 +08002738
2739 INIT_LIST_HEAD(&root->log_ctxs[index]);
2740}
2741
Chris Masone02119d2008-09-05 16:13:11 -04002742/*
2743 * btrfs_sync_log does sends a given tree log down to the disk and
2744 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002745 * you know that any inodes previously logged are safely on disk only
2746 * if it returns 0.
2747 *
2748 * Any other return value means you need to call btrfs_commit_transaction.
2749 * Some of the edge cases for fsyncing directories that have had unlinks
2750 * or renames done in the past mean that sometimes the only safe
2751 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2752 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002753 */
2754int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002755 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002756{
Yan Zheng7237f182009-01-21 12:54:03 -05002757 int index1;
2758 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002759 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002760 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002761 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002762 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002763 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002764 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002765 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002766 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002767
Yan Zheng7237f182009-01-21 12:54:03 -05002768 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002769 log_transid = ctx->log_transid;
2770 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002771 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002772 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002773 }
Miao Xied1433de2014-02-20 18:08:59 +08002774
2775 index1 = log_transid % 2;
2776 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002777 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002778 mutex_unlock(&root->log_mutex);
2779 return ctx->log_ret;
2780 }
2781 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002782 atomic_set(&root->log_commit[index1], 1);
2783
2784 /* wait for previous tree log sync to complete */
2785 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002786 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002787
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002788 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002789 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002790 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002792 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002793 mutex_unlock(&root->log_mutex);
2794 schedule_timeout_uninterruptible(1);
2795 mutex_lock(&root->log_mutex);
2796 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002797 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002798 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002799 break;
2800 }
Chris Masond0c803c2008-09-11 16:17:57 -04002801
Chris Mason12fcfd22009-03-24 10:24:20 -04002802 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002804 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002805 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002806 mutex_unlock(&root->log_mutex);
2807 goto out;
2808 }
2809
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002810 if (log_transid % 2 == 0)
2811 mark = EXTENT_DIRTY;
2812 else
2813 mark = EXTENT_NEW;
2814
Chris Mason690587d2009-10-13 13:29:19 -04002815 /* we start IO on all the marked extents here, but we don't actually
2816 * wait for them until later.
2817 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002818 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002819 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002821 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002822 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002823 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002825 mutex_unlock(&root->log_mutex);
2826 goto out;
2827 }
Yan Zheng7237f182009-01-21 12:54:03 -05002828
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002829 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002830
Yan Zheng7237f182009-01-21 12:54:03 -05002831 root->log_transid++;
2832 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002833 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002834 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002835 * IO has been started, blocks of the log tree have WRITTEN flag set
2836 * in their headers. new modifications of the log will be written to
2837 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002838 */
2839 mutex_unlock(&root->log_mutex);
2840
Filipe Manana28a23592016-08-23 21:13:51 +01002841 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002842
Yan Zheng7237f182009-01-21 12:54:03 -05002843 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002844 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002845 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002846
2847 index2 = log_root_tree->log_transid % 2;
2848 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2849 root_log_ctx.log_transid = log_root_tree->log_transid;
2850
Yan Zheng7237f182009-01-21 12:54:03 -05002851 mutex_unlock(&log_root_tree->log_mutex);
2852
2853 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002854
2855 mutex_lock(&log_root_tree->log_mutex);
2856 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002857 /*
2858 * Implicit memory barrier after atomic_dec_and_test
2859 */
Yan Zheng7237f182009-01-21 12:54:03 -05002860 if (waitqueue_active(&log_root_tree->log_writer_wait))
2861 wake_up(&log_root_tree->log_writer_wait);
2862 }
2863
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002864 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002865 if (!list_empty(&root_log_ctx.list))
2866 list_del_init(&root_log_ctx.list);
2867
Miao Xiec6adc9c2013-05-28 10:05:39 +00002868 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002869 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08002870
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002871 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002872 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002873 mutex_unlock(&log_root_tree->log_mutex);
2874 goto out;
2875 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002876 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002877 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002878 mutex_unlock(&log_root_tree->log_mutex);
2879 ret = -EAGAIN;
2880 goto out;
2881 }
2882
Miao Xied1433de2014-02-20 18:08:59 +08002883 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002884 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002885 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002886 mutex_unlock(&log_root_tree->log_mutex);
2887 ret = root_log_ctx.log_ret;
2888 goto out;
2889 }
Miao Xie8b050d32014-02-20 18:08:58 +08002890
Miao Xied1433de2014-02-20 18:08:59 +08002891 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002892 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002893 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002894 ret = btrfs_wait_tree_log_extents(log, mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002895 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002896 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002897 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002898 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002899 if (!ret)
2900 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002901 goto out;
2902 }
Miao Xied1433de2014-02-20 18:08:59 +08002903 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002904 atomic_set(&log_root_tree->log_commit[index2], 1);
2905
Chris Mason12fcfd22009-03-24 10:24:20 -04002906 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002907 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002908 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002909 }
Yan Zheng7237f182009-01-21 12:54:03 -05002910
Zhaolei60d53eb2015-08-17 18:44:46 +08002911 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002912
2913 /*
2914 * now that we've moved on to the tree of log tree roots,
2915 * check the full commit flag again
2916 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002918 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002919 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002920 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002921 mutex_unlock(&log_root_tree->log_mutex);
2922 ret = -EAGAIN;
2923 goto out_wake_log_root;
2924 }
Yan Zheng7237f182009-01-21 12:54:03 -05002925
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002926 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00002927 &log_root_tree->dirty_log_pages,
2928 EXTENT_DIRTY | EXTENT_NEW);
2929 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002931 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002932 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002933 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 mutex_unlock(&log_root_tree->log_mutex);
2935 goto out_wake_log_root;
2936 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002937 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002938 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04002939 ret = btrfs_wait_tree_log_extents(log_root_tree,
2940 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002941 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002942 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002943 btrfs_free_logged_extents(log, log_transid);
2944 mutex_unlock(&log_root_tree->log_mutex);
2945 goto out_wake_log_root;
2946 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002947 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002948
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002949 btrfs_set_super_log_root(fs_info->super_for_commit,
2950 log_root_tree->node->start);
2951 btrfs_set_super_log_root_level(fs_info->super_for_commit,
2952 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002953
Yan Zheng7237f182009-01-21 12:54:03 -05002954 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002955 mutex_unlock(&log_root_tree->log_mutex);
2956
2957 /*
2958 * nobody else is going to jump in and write the the ctree
2959 * super here because the log_commit atomic below is protecting
2960 * us. We must be called with a transaction handle pinning
2961 * the running transaction open, so a full commit can't hop
2962 * in and cause problems either.
2963 */
David Sterbaeece6a92017-02-10 19:04:32 +01002964 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002965 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002966 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002967 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002968 goto out_wake_log_root;
2969 }
Yan Zheng7237f182009-01-21 12:54:03 -05002970
Chris Mason257c62e2009-10-13 13:21:08 -04002971 mutex_lock(&root->log_mutex);
2972 if (root->last_log_commit < log_transid)
2973 root->last_log_commit = log_transid;
2974 mutex_unlock(&root->log_mutex);
2975
Chris Mason12fcfd22009-03-24 10:24:20 -04002976out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002977 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002978 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2979
Miao Xied1433de2014-02-20 18:08:59 +08002980 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002981 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002982 mutex_unlock(&log_root_tree->log_mutex);
2983
David Sterba33a9eca2015-10-10 18:35:10 +02002984 /*
2985 * The barrier before waitqueue_active is implied by mutex_unlock
2986 */
Yan Zheng7237f182009-01-21 12:54:03 -05002987 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2988 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002989out:
Miao Xied1433de2014-02-20 18:08:59 +08002990 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002991 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002992 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002993 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002994 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002995
David Sterba33a9eca2015-10-10 18:35:10 +02002996 /*
2997 * The barrier before waitqueue_active is implied by mutex_unlock
2998 */
Yan Zheng7237f182009-01-21 12:54:03 -05002999 if (waitqueue_active(&root->log_commit_wait[index1]))
3000 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003001 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003002}
3003
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003004static void free_log_tree(struct btrfs_trans_handle *trans,
3005 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003006{
3007 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003008 u64 start;
3009 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003010 struct walk_control wc = {
3011 .free = 1,
3012 .process_func = process_one_buffer
3013 };
3014
Josef Bacik681ae502013-10-07 15:11:00 -04003015 ret = walk_log_tree(trans, log, &wc);
3016 /* I don't think this can happen but just in case */
3017 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003018 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003019
Chris Masond3977122009-01-05 21:25:51 -05003020 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003021 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04003022 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3023 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003024 if (ret)
3025 break;
3026
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003027 clear_extent_bits(&log->dirty_log_pages, start, end,
David Sterba91166212016-04-26 23:54:39 +02003028 EXTENT_DIRTY | EXTENT_NEW);
Chris Masond0c803c2008-09-11 16:17:57 -04003029 }
3030
Josef Bacik2ab28f32012-10-12 15:27:49 -04003031 /*
3032 * We may have short-circuited the log tree with the full commit logic
3033 * and left ordered extents on our list, so clear these out to keep us
3034 * from leaking inodes and memory.
3035 */
3036 btrfs_free_logged_extents(log, 0);
3037 btrfs_free_logged_extents(log, 1);
3038
Yan Zheng7237f182009-01-21 12:54:03 -05003039 free_extent_buffer(log->node);
3040 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003041}
3042
3043/*
3044 * free all the extents used by the tree log. This should be called
3045 * at commit time of the full transaction
3046 */
3047int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3048{
3049 if (root->log_root) {
3050 free_log_tree(trans, root->log_root);
3051 root->log_root = NULL;
3052 }
3053 return 0;
3054}
3055
3056int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3057 struct btrfs_fs_info *fs_info)
3058{
3059 if (fs_info->log_root_tree) {
3060 free_log_tree(trans, fs_info->log_root_tree);
3061 fs_info->log_root_tree = NULL;
3062 }
Chris Masone02119d2008-09-05 16:13:11 -04003063 return 0;
3064}
3065
3066/*
Chris Masone02119d2008-09-05 16:13:11 -04003067 * If both a file and directory are logged, and unlinks or renames are
3068 * mixed in, we have a few interesting corners:
3069 *
3070 * create file X in dir Y
3071 * link file X to X.link in dir Y
3072 * fsync file X
3073 * unlink file X but leave X.link
3074 * fsync dir Y
3075 *
3076 * After a crash we would expect only X.link to exist. But file X
3077 * didn't get fsync'd again so the log has back refs for X and X.link.
3078 *
3079 * We solve this by removing directory entries and inode backrefs from the
3080 * log when a file that was logged in the current transaction is
3081 * unlinked. Any later fsync will include the updated log entries, and
3082 * we'll be able to reconstruct the proper directory items from backrefs.
3083 *
3084 * This optimizations allows us to avoid relogging the entire inode
3085 * or the entire directory.
3086 */
3087int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3088 struct btrfs_root *root,
3089 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003090 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003091{
3092 struct btrfs_root *log;
3093 struct btrfs_dir_item *di;
3094 struct btrfs_path *path;
3095 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003096 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003097 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003098 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003099
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003100 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003101 return 0;
3102
Chris Masone02119d2008-09-05 16:13:11 -04003103 ret = join_running_log_trans(root);
3104 if (ret)
3105 return 0;
3106
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003107 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003108
3109 log = root->log_root;
3110 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003111 if (!path) {
3112 err = -ENOMEM;
3113 goto out_unlock;
3114 }
liubo2a29edc2011-01-26 06:22:08 +00003115
Li Zefan33345d012011-04-20 10:31:50 +08003116 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003117 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003118 if (IS_ERR(di)) {
3119 err = PTR_ERR(di);
3120 goto fail;
3121 }
3122 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003123 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3124 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003125 if (ret) {
3126 err = ret;
3127 goto fail;
3128 }
Chris Masone02119d2008-09-05 16:13:11 -04003129 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003130 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003131 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003132 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003133 if (IS_ERR(di)) {
3134 err = PTR_ERR(di);
3135 goto fail;
3136 }
3137 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003138 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3139 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003140 if (ret) {
3141 err = ret;
3142 goto fail;
3143 }
Chris Masone02119d2008-09-05 16:13:11 -04003144 }
3145
3146 /* update the directory size in the log to reflect the names
3147 * we have removed
3148 */
3149 if (bytes_del) {
3150 struct btrfs_key key;
3151
Li Zefan33345d012011-04-20 10:31:50 +08003152 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003153 key.offset = 0;
3154 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003155 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003156
3157 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003158 if (ret < 0) {
3159 err = ret;
3160 goto fail;
3161 }
Chris Masone02119d2008-09-05 16:13:11 -04003162 if (ret == 0) {
3163 struct btrfs_inode_item *item;
3164 u64 i_size;
3165
3166 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3167 struct btrfs_inode_item);
3168 i_size = btrfs_inode_size(path->nodes[0], item);
3169 if (i_size > bytes_del)
3170 i_size -= bytes_del;
3171 else
3172 i_size = 0;
3173 btrfs_set_inode_size(path->nodes[0], item, i_size);
3174 btrfs_mark_buffer_dirty(path->nodes[0]);
3175 } else
3176 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003177 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003178 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003179fail:
Chris Masone02119d2008-09-05 16:13:11 -04003180 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003181out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003182 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003184 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003185 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003186 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003187 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003188
Chris Mason12fcfd22009-03-24 10:24:20 -04003189 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003190
Andi Kleen411fc6b2010-10-29 15:14:31 -04003191 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003192}
3193
3194/* see comments for btrfs_del_dir_entries_in_log */
3195int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3196 struct btrfs_root *root,
3197 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003198 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003200 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003201 struct btrfs_root *log;
3202 u64 index;
3203 int ret;
3204
Nikolay Borisova491abb2017-01-18 00:31:33 +02003205 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003206 return 0;
3207
Chris Masone02119d2008-09-05 16:13:11 -04003208 ret = join_running_log_trans(root);
3209 if (ret)
3210 return 0;
3211 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003212 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003213
Nikolay Borisova491abb2017-01-18 00:31:33 +02003214 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003215 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003216 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003217 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003218 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003219 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003220 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003221 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003222 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003223
Chris Masone02119d2008-09-05 16:13:11 -04003224 return ret;
3225}
3226
3227/*
3228 * creates a range item in the log for 'dirid'. first_offset and
3229 * last_offset tell us which parts of the key space the log should
3230 * be considered authoritative for.
3231 */
3232static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3233 struct btrfs_root *log,
3234 struct btrfs_path *path,
3235 int key_type, u64 dirid,
3236 u64 first_offset, u64 last_offset)
3237{
3238 int ret;
3239 struct btrfs_key key;
3240 struct btrfs_dir_log_item *item;
3241
3242 key.objectid = dirid;
3243 key.offset = first_offset;
3244 if (key_type == BTRFS_DIR_ITEM_KEY)
3245 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3246 else
3247 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3248 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003249 if (ret)
3250 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003251
3252 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3253 struct btrfs_dir_log_item);
3254 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3255 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003256 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003257 return 0;
3258}
3259
3260/*
3261 * log all the items included in the current transaction for a given
3262 * directory. This also creates the range items in the log tree required
3263 * to replay anything deleted before the fsync
3264 */
3265static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003266 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003267 struct btrfs_path *path,
3268 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003269 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003270 u64 min_offset, u64 *last_offset_ret)
3271{
3272 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003273 struct btrfs_root *log = root->log_root;
3274 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003275 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003276 int ret;
3277 int i;
3278 int nritems;
3279 u64 first_offset = min_offset;
3280 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003281 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003282
3283 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003284
Li Zefan33345d012011-04-20 10:31:50 +08003285 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003286 min_key.type = key_type;
3287 min_key.offset = min_offset;
3288
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003289 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003290
3291 /*
3292 * we didn't find anything from this transaction, see if there
3293 * is anything at all
3294 */
Li Zefan33345d012011-04-20 10:31:50 +08003295 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3296 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003297 min_key.type = key_type;
3298 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003299 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003300 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3301 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003302 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003303 return ret;
3304 }
Li Zefan33345d012011-04-20 10:31:50 +08003305 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003306
3307 /* if ret == 0 there are items for this type,
3308 * create a range to tell us the last key of this type.
3309 * otherwise, there are no items in this directory after
3310 * *min_offset, and we create a range to indicate that.
3311 */
3312 if (ret == 0) {
3313 struct btrfs_key tmp;
3314 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3315 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003316 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003317 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003318 }
3319 goto done;
3320 }
3321
3322 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003323 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003324 if (ret == 0) {
3325 struct btrfs_key tmp;
3326 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3327 if (key_type == tmp.type) {
3328 first_offset = tmp.offset;
3329 ret = overwrite_item(trans, log, dst_path,
3330 path->nodes[0], path->slots[0],
3331 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003332 if (ret) {
3333 err = ret;
3334 goto done;
3335 }
Chris Masone02119d2008-09-05 16:13:11 -04003336 }
3337 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003338 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003339
3340 /* find the first key from this transaction again */
3341 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303342 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003343 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003344
3345 /*
3346 * we have a block from this transaction, log every item in it
3347 * from our directory
3348 */
Chris Masond3977122009-01-05 21:25:51 -05003349 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003350 struct btrfs_key tmp;
3351 src = path->nodes[0];
3352 nritems = btrfs_header_nritems(src);
3353 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003354 struct btrfs_dir_item *di;
3355
Chris Masone02119d2008-09-05 16:13:11 -04003356 btrfs_item_key_to_cpu(src, &min_key, i);
3357
Li Zefan33345d012011-04-20 10:31:50 +08003358 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003359 goto done;
3360 ret = overwrite_item(trans, log, dst_path, src, i,
3361 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003362 if (ret) {
3363 err = ret;
3364 goto done;
3365 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003366
3367 /*
3368 * We must make sure that when we log a directory entry,
3369 * the corresponding inode, after log replay, has a
3370 * matching link count. For example:
3371 *
3372 * touch foo
3373 * mkdir mydir
3374 * sync
3375 * ln foo mydir/bar
3376 * xfs_io -c "fsync" mydir
3377 * <crash>
3378 * <mount fs and log replay>
3379 *
3380 * Would result in a fsync log that when replayed, our
3381 * file inode would have a link count of 1, but we get
3382 * two directory entries pointing to the same inode.
3383 * After removing one of the names, it would not be
3384 * possible to remove the other name, which resulted
3385 * always in stale file handle errors, and would not
3386 * be possible to rmdir the parent directory, since
3387 * its i_size could never decrement to the value
3388 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3389 */
3390 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3391 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3392 if (ctx &&
3393 (btrfs_dir_transid(src, di) == trans->transid ||
3394 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3395 tmp.type != BTRFS_ROOT_ITEM_KEY)
3396 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003397 }
3398 path->slots[0] = nritems;
3399
3400 /*
3401 * look ahead to the next item and see if it is also
3402 * from this directory and from this transaction
3403 */
3404 ret = btrfs_next_leaf(root, path);
3405 if (ret == 1) {
3406 last_offset = (u64)-1;
3407 goto done;
3408 }
3409 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003410 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003411 last_offset = (u64)-1;
3412 goto done;
3413 }
3414 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3415 ret = overwrite_item(trans, log, dst_path,
3416 path->nodes[0], path->slots[0],
3417 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003418 if (ret)
3419 err = ret;
3420 else
3421 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003422 goto done;
3423 }
3424 }
3425done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003426 btrfs_release_path(path);
3427 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003428
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003429 if (err == 0) {
3430 *last_offset_ret = last_offset;
3431 /*
3432 * insert the log range keys to indicate where the log
3433 * is valid
3434 */
3435 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003436 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003437 if (ret)
3438 err = ret;
3439 }
3440 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003441}
3442
3443/*
3444 * logging directories is very similar to logging inodes, We find all the items
3445 * from the current transaction and write them to the log.
3446 *
3447 * The recovery code scans the directory in the subvolume, and if it finds a
3448 * key in the range logged that is not present in the log tree, then it means
3449 * that dir entry was unlinked during the transaction.
3450 *
3451 * In order for that scan to work, we must include one key smaller than
3452 * the smallest logged by this transaction and one key larger than the largest
3453 * key logged by this transaction.
3454 */
3455static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003456 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003457 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003458 struct btrfs_path *dst_path,
3459 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003460{
3461 u64 min_key;
3462 u64 max_key;
3463 int ret;
3464 int key_type = BTRFS_DIR_ITEM_KEY;
3465
3466again:
3467 min_key = 0;
3468 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003469 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003470 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3471 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003472 if (ret)
3473 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003474 if (max_key == (u64)-1)
3475 break;
3476 min_key = max_key + 1;
3477 }
3478
3479 if (key_type == BTRFS_DIR_ITEM_KEY) {
3480 key_type = BTRFS_DIR_INDEX_KEY;
3481 goto again;
3482 }
3483 return 0;
3484}
3485
3486/*
3487 * a helper function to drop items from the log before we relog an
3488 * inode. max_key_type indicates the highest item type to remove.
3489 * This cannot be run for file data extents because it does not
3490 * free the extents they point to.
3491 */
3492static int drop_objectid_items(struct btrfs_trans_handle *trans,
3493 struct btrfs_root *log,
3494 struct btrfs_path *path,
3495 u64 objectid, int max_key_type)
3496{
3497 int ret;
3498 struct btrfs_key key;
3499 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003500 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003501
3502 key.objectid = objectid;
3503 key.type = max_key_type;
3504 key.offset = (u64)-1;
3505
Chris Masond3977122009-01-05 21:25:51 -05003506 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003507 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003508 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003509 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003510 break;
3511
3512 if (path->slots[0] == 0)
3513 break;
3514
3515 path->slots[0]--;
3516 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3517 path->slots[0]);
3518
3519 if (found_key.objectid != objectid)
3520 break;
3521
Josef Bacik18ec90d2012-09-28 11:56:28 -04003522 found_key.offset = 0;
3523 found_key.type = 0;
3524 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3525 &start_slot);
3526
3527 ret = btrfs_del_items(trans, log, path, start_slot,
3528 path->slots[0] - start_slot + 1);
3529 /*
3530 * If start slot isn't 0 then we don't need to re-search, we've
3531 * found the last guy with the objectid in this tree.
3532 */
3533 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003534 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003535 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003536 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003537 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003538 if (ret > 0)
3539 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003540 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003541}
3542
Josef Bacik94edf4a2012-09-25 14:56:25 -04003543static void fill_inode_item(struct btrfs_trans_handle *trans,
3544 struct extent_buffer *leaf,
3545 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003546 struct inode *inode, int log_inode_only,
3547 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003548{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003549 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003550
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003551 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003552
3553 if (log_inode_only) {
3554 /* set the generation to zero so the recover code
3555 * can tell the difference between an logging
3556 * just to say 'this inode exists' and a logging
3557 * to say 'update this inode with these values'
3558 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003559 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003560 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003561 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003562 btrfs_set_token_inode_generation(leaf, item,
3563 BTRFS_I(inode)->generation,
3564 &token);
3565 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003566 }
3567
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003568 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3569 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3570 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3571 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3572
David Sterbaa937b972014-12-12 17:39:12 +01003573 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003574 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003575 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003576 inode->i_atime.tv_nsec, &token);
3577
David Sterbaa937b972014-12-12 17:39:12 +01003578 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003579 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003580 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003581 inode->i_mtime.tv_nsec, &token);
3582
David Sterbaa937b972014-12-12 17:39:12 +01003583 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003584 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003585 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003586 inode->i_ctime.tv_nsec, &token);
3587
3588 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3589 &token);
3590
3591 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3592 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3593 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3594 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3595 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003596}
3597
Josef Bacika95249b2012-10-11 16:17:34 -04003598static int log_inode_item(struct btrfs_trans_handle *trans,
3599 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003600 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003601{
3602 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003603 int ret;
3604
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003605 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003606 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003607 if (ret && ret != -EEXIST)
3608 return ret;
3609 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3610 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003611 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3612 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003613 btrfs_release_path(path);
3614 return 0;
3615}
3616
Chris Mason31ff1cd2008-09-11 16:17:57 -04003617static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003618 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003619 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003620 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003621 int start_slot, int nr, int inode_only,
3622 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003623{
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003624 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003625 unsigned long src_offset;
3626 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003627 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003628 struct btrfs_file_extent_item *extent;
3629 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003630 struct extent_buffer *src = src_path->nodes[0];
3631 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003632 int ret;
3633 struct btrfs_key *ins_keys;
3634 u32 *ins_sizes;
3635 char *ins_data;
3636 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003637 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003638 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003639 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003640 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003641 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003642
3643 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003644
3645 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3646 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003647 if (!ins_data)
3648 return -ENOMEM;
3649
Josef Bacik16e75492013-10-22 12:18:51 -04003650 first_key.objectid = (u64)-1;
3651
Chris Mason31ff1cd2008-09-11 16:17:57 -04003652 ins_sizes = (u32 *)ins_data;
3653 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3654
3655 for (i = 0; i < nr; i++) {
3656 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3657 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3658 }
3659 ret = btrfs_insert_empty_items(trans, log, dst_path,
3660 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003661 if (ret) {
3662 kfree(ins_data);
3663 return ret;
3664 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003666 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003667 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3668 dst_path->slots[0]);
3669
3670 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3671
Josef Bacik16e75492013-10-22 12:18:51 -04003672 if ((i == (nr - 1)))
3673 last_key = ins_keys[i];
3674
Josef Bacik94edf4a2012-09-25 14:56:25 -04003675 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003676 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3677 dst_path->slots[0],
3678 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003679 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003680 &inode->vfs_inode,
3681 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003682 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003683 } else {
3684 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3685 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003686 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003687
Josef Bacik16e75492013-10-22 12:18:51 -04003688 /*
3689 * We set need_find_last_extent here in case we know we were
3690 * processing other items and then walk into the first extent in
3691 * the inode. If we don't hit an extent then nothing changes,
3692 * we'll do the last search the next time around.
3693 */
3694 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3695 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003696 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003697 first_key = ins_keys[i];
3698 } else {
3699 need_find_last_extent = false;
3700 }
3701
Chris Mason31ff1cd2008-09-11 16:17:57 -04003702 /* take a reference on file data extents so that truncates
3703 * or deletes of this inode don't have to relog the inode
3704 * again
3705 */
David Sterba962a2982014-06-04 18:41:45 +02003706 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003707 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003708 int found_type;
3709 extent = btrfs_item_ptr(src, start_slot + i,
3710 struct btrfs_file_extent_item);
3711
liubo8e531cd2011-05-06 10:36:09 +08003712 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3713 continue;
3714
Chris Mason31ff1cd2008-09-11 16:17:57 -04003715 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003716 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003717 u64 ds, dl, cs, cl;
3718 ds = btrfs_file_extent_disk_bytenr(src,
3719 extent);
3720 /* ds == 0 is a hole */
3721 if (ds == 0)
3722 continue;
3723
3724 dl = btrfs_file_extent_disk_num_bytes(src,
3725 extent);
3726 cs = btrfs_file_extent_offset(src, extent);
3727 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003728 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003729 if (btrfs_file_extent_compression(src,
3730 extent)) {
3731 cs = 0;
3732 cl = dl;
3733 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003734
3735 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003736 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003737 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003738 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003739 if (ret) {
3740 btrfs_release_path(dst_path);
3741 kfree(ins_data);
3742 return ret;
3743 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003744 }
3745 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003746 }
3747
3748 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003749 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003750 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003751
3752 /*
3753 * we have to do this after the loop above to avoid changing the
3754 * log tree while trying to change the log tree.
3755 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003756 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003757 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003758 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3759 struct btrfs_ordered_sum,
3760 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003761 if (!ret)
3762 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003763 list_del(&sums->list);
3764 kfree(sums);
3765 }
Josef Bacik16e75492013-10-22 12:18:51 -04003766
3767 if (!has_extents)
3768 return ret;
3769
Filipe Manana74121f7c2014-08-07 12:00:44 +01003770 if (need_find_last_extent && *last_extent == first_key.offset) {
3771 /*
3772 * We don't have any leafs between our current one and the one
3773 * we processed before that can have file extent items for our
3774 * inode (and have a generation number smaller than our current
3775 * transaction id).
3776 */
3777 need_find_last_extent = false;
3778 }
3779
Josef Bacik16e75492013-10-22 12:18:51 -04003780 /*
3781 * Because we use btrfs_search_forward we could skip leaves that were
3782 * not modified and then assume *last_extent is valid when it really
3783 * isn't. So back up to the previous leaf and read the end of the last
3784 * extent before we go and fill in holes.
3785 */
3786 if (need_find_last_extent) {
3787 u64 len;
3788
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003789 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003790 if (ret < 0)
3791 return ret;
3792 if (ret)
3793 goto fill_holes;
3794 if (src_path->slots[0])
3795 src_path->slots[0]--;
3796 src = src_path->nodes[0];
3797 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003798 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003799 key.type != BTRFS_EXTENT_DATA_KEY)
3800 goto fill_holes;
3801 extent = btrfs_item_ptr(src, src_path->slots[0],
3802 struct btrfs_file_extent_item);
3803 if (btrfs_file_extent_type(src, extent) ==
3804 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003805 len = btrfs_file_extent_inline_len(src,
3806 src_path->slots[0],
3807 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003808 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003809 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003810 } else {
3811 len = btrfs_file_extent_num_bytes(src, extent);
3812 *last_extent = key.offset + len;
3813 }
3814 }
3815fill_holes:
3816 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3817 * things could have happened
3818 *
3819 * 1) A merge could have happened, so we could currently be on a leaf
3820 * that holds what we were copying in the first place.
3821 * 2) A split could have happened, and now not all of the items we want
3822 * are on the same leaf.
3823 *
3824 * So we need to adjust how we search for holes, we need to drop the
3825 * path and re-search for the first extent key we found, and then walk
3826 * forward until we hit the last one we copied.
3827 */
3828 if (need_find_last_extent) {
3829 /* btrfs_prev_leaf could return 1 without releasing the path */
3830 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01003831 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3832 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003833 if (ret < 0)
3834 return ret;
3835 ASSERT(ret == 0);
3836 src = src_path->nodes[0];
3837 i = src_path->slots[0];
3838 } else {
3839 i = start_slot;
3840 }
3841
3842 /*
3843 * Ok so here we need to go through and fill in any holes we may have
3844 * to make sure that holes are punched for those areas in case they had
3845 * extents previously.
3846 */
3847 while (!done) {
3848 u64 offset, len;
3849 u64 extent_end;
3850
3851 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003852 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003853 if (ret < 0)
3854 return ret;
3855 ASSERT(ret == 0);
3856 src = src_path->nodes[0];
3857 i = 0;
3858 }
3859
3860 btrfs_item_key_to_cpu(src, &key, i);
3861 if (!btrfs_comp_cpu_keys(&key, &last_key))
3862 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003863 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003864 key.type != BTRFS_EXTENT_DATA_KEY) {
3865 i++;
3866 continue;
3867 }
3868 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3869 if (btrfs_file_extent_type(src, extent) ==
3870 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003871 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04003872 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003873 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003874 } else {
3875 len = btrfs_file_extent_num_bytes(src, extent);
3876 extent_end = key.offset + len;
3877 }
3878 i++;
3879
3880 if (*last_extent == key.offset) {
3881 *last_extent = extent_end;
3882 continue;
3883 }
3884 offset = *last_extent;
3885 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003886 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01003887 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003888 if (ret)
3889 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003890 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003891 }
3892 /*
3893 * Need to let the callers know we dropped the path so they should
3894 * re-search.
3895 */
3896 if (!ret && need_find_last_extent)
3897 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003898 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003899}
3900
Josef Bacik5dc562c2012-08-17 13:14:17 -04003901static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3902{
3903 struct extent_map *em1, *em2;
3904
3905 em1 = list_entry(a, struct extent_map, list);
3906 em2 = list_entry(b, struct extent_map, list);
3907
3908 if (em1->start < em2->start)
3909 return -1;
3910 else if (em1->start > em2->start)
3911 return 1;
3912 return 0;
3913}
3914
Filipe Manana8407f552014-09-05 15:14:39 +01003915static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3916 struct inode *inode,
3917 struct btrfs_root *root,
3918 const struct extent_map *em,
3919 const struct list_head *logged_list,
3920 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003921{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003922 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003923 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003924 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003925 u64 mod_start = em->mod_start;
3926 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003927 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003928 u64 csum_offset;
3929 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003930 LIST_HEAD(ordered_sums);
3931 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003932
Filipe Manana8407f552014-09-05 15:14:39 +01003933 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003934
Filipe Manana8407f552014-09-05 15:14:39 +01003935 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3936 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003937 return 0;
3938
Josef Bacik2ab28f32012-10-12 15:27:49 -04003939 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003940 * Wait far any ordered extent that covers our extent map. If it
3941 * finishes without an error, first check and see if our csums are on
3942 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003943 */
Miao Xie827463c2014-01-14 20:31:51 +08003944 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003945 struct btrfs_ordered_sum *sum;
3946
3947 if (!mod_len)
3948 break;
3949
Josef Bacik2ab28f32012-10-12 15:27:49 -04003950 if (ordered->file_offset + ordered->len <= mod_start ||
3951 mod_start + mod_len <= ordered->file_offset)
3952 continue;
3953
Filipe Manana8407f552014-09-05 15:14:39 +01003954 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3955 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3956 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3957 const u64 start = ordered->file_offset;
3958 const u64 end = ordered->file_offset + ordered->len - 1;
3959
3960 WARN_ON(ordered->inode != inode);
3961 filemap_fdatawrite_range(inode->i_mapping, start, end);
3962 }
3963
3964 wait_event(ordered->wait,
3965 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3966 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3967
3968 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003969 /*
3970 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3971 * i_mapping flags, so that the next fsync won't get
3972 * an outdated io error too.
3973 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003974 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003975 *ordered_io_error = true;
3976 break;
3977 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003978 /*
3979 * We are going to copy all the csums on this ordered extent, so
3980 * go ahead and adjust mod_start and mod_len in case this
3981 * ordered extent has already been logged.
3982 */
3983 if (ordered->file_offset > mod_start) {
3984 if (ordered->file_offset + ordered->len >=
3985 mod_start + mod_len)
3986 mod_len = ordered->file_offset - mod_start;
3987 /*
3988 * If we have this case
3989 *
3990 * |--------- logged extent ---------|
3991 * |----- ordered extent ----|
3992 *
3993 * Just don't mess with mod_start and mod_len, we'll
3994 * just end up logging more csums than we need and it
3995 * will be ok.
3996 */
3997 } else {
3998 if (ordered->file_offset + ordered->len <
3999 mod_start + mod_len) {
4000 mod_len = (mod_start + mod_len) -
4001 (ordered->file_offset + ordered->len);
4002 mod_start = ordered->file_offset +
4003 ordered->len;
4004 } else {
4005 mod_len = 0;
4006 }
4007 }
4008
Filipe Manana8407f552014-09-05 15:14:39 +01004009 if (skip_csum)
4010 continue;
4011
Josef Bacik2ab28f32012-10-12 15:27:49 -04004012 /*
4013 * To keep us from looping for the above case of an ordered
4014 * extent that falls inside of the logged extent.
4015 */
4016 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4017 &ordered->flags))
4018 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004019
Josef Bacik2ab28f32012-10-12 15:27:49 -04004020 list_for_each_entry(sum, &ordered->list, list) {
4021 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004022 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004023 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004024 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004025 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004026
Filipe Manana8407f552014-09-05 15:14:39 +01004027 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004028 return ret;
4029
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004030 if (em->compress_type) {
4031 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004032 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004033 } else {
4034 csum_offset = mod_start - em->start;
4035 csum_len = mod_len;
4036 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004037
Josef Bacik70c8a912012-10-11 16:54:30 -04004038 /* block start is already adjusted for the file extent offset. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004039 ret = btrfs_lookup_csums_range(fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004040 em->block_start + csum_offset,
4041 em->block_start + csum_offset +
4042 csum_len - 1, &ordered_sums, 0);
4043 if (ret)
4044 return ret;
4045
4046 while (!list_empty(&ordered_sums)) {
4047 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4048 struct btrfs_ordered_sum,
4049 list);
4050 if (!ret)
4051 ret = btrfs_csum_file_blocks(trans, log, sums);
4052 list_del(&sums->list);
4053 kfree(sums);
4054 }
4055
4056 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004057}
4058
Filipe Manana8407f552014-09-05 15:14:39 +01004059static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004060 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004061 const struct extent_map *em,
4062 struct btrfs_path *path,
4063 const struct list_head *logged_list,
4064 struct btrfs_log_ctx *ctx)
4065{
4066 struct btrfs_root *log = root->log_root;
4067 struct btrfs_file_extent_item *fi;
4068 struct extent_buffer *leaf;
4069 struct btrfs_map_token token;
4070 struct btrfs_key key;
4071 u64 extent_offset = em->start - em->orig_start;
4072 u64 block_len;
4073 int ret;
4074 int extent_inserted = 0;
4075 bool ordered_io_err = false;
4076
David Sterbaf85b7372017-01-20 14:54:07 +01004077 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4078 logged_list, &ordered_io_err);
Filipe Manana8407f552014-09-05 15:14:39 +01004079 if (ret)
4080 return ret;
4081
4082 if (ordered_io_err) {
4083 ctx->io_err = -EIO;
4084 return 0;
4085 }
4086
4087 btrfs_init_map_token(&token);
4088
Nikolay Borisov9d122622017-01-18 00:31:40 +02004089 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004090 em->start + em->len, NULL, 0, 1,
4091 sizeof(*fi), &extent_inserted);
4092 if (ret)
4093 return ret;
4094
4095 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004096 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004097 key.type = BTRFS_EXTENT_DATA_KEY;
4098 key.offset = em->start;
4099
4100 ret = btrfs_insert_empty_item(trans, log, path, &key,
4101 sizeof(*fi));
4102 if (ret)
4103 return ret;
4104 }
4105 leaf = path->nodes[0];
4106 fi = btrfs_item_ptr(leaf, path->slots[0],
4107 struct btrfs_file_extent_item);
4108
Josef Bacik50d9aa92014-11-21 14:52:38 -05004109 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004110 &token);
4111 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4112 btrfs_set_token_file_extent_type(leaf, fi,
4113 BTRFS_FILE_EXTENT_PREALLOC,
4114 &token);
4115 else
4116 btrfs_set_token_file_extent_type(leaf, fi,
4117 BTRFS_FILE_EXTENT_REG,
4118 &token);
4119
4120 block_len = max(em->block_len, em->orig_block_len);
4121 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4122 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4123 em->block_start,
4124 &token);
4125 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4126 &token);
4127 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4128 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4129 em->block_start -
4130 extent_offset, &token);
4131 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4132 &token);
4133 } else {
4134 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4135 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4136 &token);
4137 }
4138
4139 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4140 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4141 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4142 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4143 &token);
4144 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4145 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4146 btrfs_mark_buffer_dirty(leaf);
4147
4148 btrfs_release_path(path);
4149
4150 return ret;
4151}
4152
Josef Bacik5dc562c2012-08-17 13:14:17 -04004153static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4154 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004155 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004156 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004157 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004158 struct btrfs_log_ctx *ctx,
4159 const u64 start,
4160 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004161{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004162 struct extent_map *em, *n;
4163 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004164 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004165 u64 test_gen;
4166 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004167 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004168
4169 INIT_LIST_HEAD(&extents);
4170
Nikolay Borisov9d122622017-01-18 00:31:40 +02004171 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004172 write_lock(&tree->lock);
4173 test_gen = root->fs_info->last_trans_committed;
4174
4175 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4176 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004177
4178 /*
4179 * Just an arbitrary number, this can be really CPU intensive
4180 * once we start getting a lot of extents, and really once we
4181 * have a bunch of extents we just want to commit since it will
4182 * be faster.
4183 */
4184 if (++num > 32768) {
4185 list_del_init(&tree->modified_extents);
4186 ret = -EFBIG;
4187 goto process;
4188 }
4189
Josef Bacik5dc562c2012-08-17 13:14:17 -04004190 if (em->generation <= test_gen)
4191 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004192 /* Need a ref to keep it from getting evicted from cache */
4193 atomic_inc(&em->refs);
4194 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004195 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004196 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004197 }
4198
4199 list_sort(NULL, &extents, extent_cmp);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004200 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004201 /*
4202 * Some ordered extents started by fsync might have completed
4203 * before we could collect them into the list logged_list, which
4204 * means they're gone, not in our logged_list nor in the inode's
4205 * ordered tree. We want the application/user space to know an
4206 * error happened while attempting to persist file data so that
4207 * it can take proper action. If such error happened, we leave
4208 * without writing to the log tree and the fsync must report the
4209 * file data write error and not commit the current transaction.
4210 */
Nikolay Borisov9d122622017-01-18 00:31:40 +02004211 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004212 if (ret)
4213 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004214process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004215 while (!list_empty(&extents)) {
4216 em = list_entry(extents.next, struct extent_map, list);
4217
4218 list_del_init(&em->list);
4219
4220 /*
4221 * If we had an error we just need to delete everybody from our
4222 * private list.
4223 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004224 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004225 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004226 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004227 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004228 }
4229
4230 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004231
Filipe Manana8407f552014-09-05 15:14:39 +01004232 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4233 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004234 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004235 clear_em_logging(tree, em);
4236 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004237 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004238 WARN_ON(!list_empty(&extents));
4239 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004240 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004241
Josef Bacik5dc562c2012-08-17 13:14:17 -04004242 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004243 return ret;
4244}
4245
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004246static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004247 struct btrfs_path *path, u64 *size_ret)
4248{
4249 struct btrfs_key key;
4250 int ret;
4251
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004252 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004253 key.type = BTRFS_INODE_ITEM_KEY;
4254 key.offset = 0;
4255
4256 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4257 if (ret < 0) {
4258 return ret;
4259 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004260 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004261 } else {
4262 struct btrfs_inode_item *item;
4263
4264 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4265 struct btrfs_inode_item);
4266 *size_ret = btrfs_inode_size(path->nodes[0], item);
4267 }
4268
4269 btrfs_release_path(path);
4270 return 0;
4271}
4272
Filipe Manana36283bf2015-06-20 00:44:51 +01004273/*
4274 * At the moment we always log all xattrs. This is to figure out at log replay
4275 * time which xattrs must have their deletion replayed. If a xattr is missing
4276 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4277 * because if a xattr is deleted, the inode is fsynced and a power failure
4278 * happens, causing the log to be replayed the next time the fs is mounted,
4279 * we want the xattr to not exist anymore (same behaviour as other filesystems
4280 * with a journal, ext3/4, xfs, f2fs, etc).
4281 */
4282static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4283 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004284 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004285 struct btrfs_path *path,
4286 struct btrfs_path *dst_path)
4287{
4288 int ret;
4289 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004290 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004291 int ins_nr = 0;
4292 int start_slot = 0;
4293
4294 key.objectid = ino;
4295 key.type = BTRFS_XATTR_ITEM_KEY;
4296 key.offset = 0;
4297
4298 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4299 if (ret < 0)
4300 return ret;
4301
4302 while (true) {
4303 int slot = path->slots[0];
4304 struct extent_buffer *leaf = path->nodes[0];
4305 int nritems = btrfs_header_nritems(leaf);
4306
4307 if (slot >= nritems) {
4308 if (ins_nr > 0) {
4309 u64 last_extent = 0;
4310
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004311 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004312 &last_extent, start_slot,
4313 ins_nr, 1, 0);
4314 /* can't be 1, extent items aren't processed */
4315 ASSERT(ret <= 0);
4316 if (ret < 0)
4317 return ret;
4318 ins_nr = 0;
4319 }
4320 ret = btrfs_next_leaf(root, path);
4321 if (ret < 0)
4322 return ret;
4323 else if (ret > 0)
4324 break;
4325 continue;
4326 }
4327
4328 btrfs_item_key_to_cpu(leaf, &key, slot);
4329 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4330 break;
4331
4332 if (ins_nr == 0)
4333 start_slot = slot;
4334 ins_nr++;
4335 path->slots[0]++;
4336 cond_resched();
4337 }
4338 if (ins_nr > 0) {
4339 u64 last_extent = 0;
4340
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004341 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004342 &last_extent, start_slot,
4343 ins_nr, 1, 0);
4344 /* can't be 1, extent items aren't processed */
4345 ASSERT(ret <= 0);
4346 if (ret < 0)
4347 return ret;
4348 }
4349
4350 return 0;
4351}
4352
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004353/*
4354 * If the no holes feature is enabled we need to make sure any hole between the
4355 * last extent and the i_size of our inode is explicitly marked in the log. This
4356 * is to make sure that doing something like:
4357 *
4358 * 1) create file with 128Kb of data
4359 * 2) truncate file to 64Kb
4360 * 3) truncate file to 256Kb
4361 * 4) fsync file
4362 * 5) <crash/power failure>
4363 * 6) mount fs and trigger log replay
4364 *
4365 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4366 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4367 * file correspond to a hole. The presence of explicit holes in a log tree is
4368 * what guarantees that log replay will remove/adjust file extent items in the
4369 * fs/subvol tree.
4370 *
4371 * Here we do not need to care about holes between extents, that is already done
4372 * by copy_items(). We also only need to do this in the full sync path, where we
4373 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4374 * lookup the list of modified extent maps and if any represents a hole, we
4375 * insert a corresponding extent representing a hole in the log tree.
4376 */
4377static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4378 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004379 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004380 struct btrfs_path *path)
4381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004382 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004383 int ret;
4384 struct btrfs_key key;
4385 u64 hole_start;
4386 u64 hole_size;
4387 struct extent_buffer *leaf;
4388 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004389 const u64 ino = btrfs_ino(inode);
4390 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004391
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004392 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004393 return 0;
4394
4395 key.objectid = ino;
4396 key.type = BTRFS_EXTENT_DATA_KEY;
4397 key.offset = (u64)-1;
4398
4399 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4400 ASSERT(ret != 0);
4401 if (ret < 0)
4402 return ret;
4403
4404 ASSERT(path->slots[0] > 0);
4405 path->slots[0]--;
4406 leaf = path->nodes[0];
4407 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4408
4409 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4410 /* inode does not have any extents */
4411 hole_start = 0;
4412 hole_size = i_size;
4413 } else {
4414 struct btrfs_file_extent_item *extent;
4415 u64 len;
4416
4417 /*
4418 * If there's an extent beyond i_size, an explicit hole was
4419 * already inserted by copy_items().
4420 */
4421 if (key.offset >= i_size)
4422 return 0;
4423
4424 extent = btrfs_item_ptr(leaf, path->slots[0],
4425 struct btrfs_file_extent_item);
4426
4427 if (btrfs_file_extent_type(leaf, extent) ==
4428 BTRFS_FILE_EXTENT_INLINE) {
4429 len = btrfs_file_extent_inline_len(leaf,
4430 path->slots[0],
4431 extent);
4432 ASSERT(len == i_size);
4433 return 0;
4434 }
4435
4436 len = btrfs_file_extent_num_bytes(leaf, extent);
4437 /* Last extent goes beyond i_size, no need to log a hole. */
4438 if (key.offset + len > i_size)
4439 return 0;
4440 hole_start = key.offset + len;
4441 hole_size = i_size - hole_start;
4442 }
4443 btrfs_release_path(path);
4444
4445 /* Last extent ends at i_size. */
4446 if (hole_size == 0)
4447 return 0;
4448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004449 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004450 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4451 hole_size, 0, hole_size, 0, 0, 0);
4452 return ret;
4453}
4454
Filipe Manana56f23fd2016-03-30 23:37:21 +01004455/*
4456 * When we are logging a new inode X, check if it doesn't have a reference that
4457 * matches the reference from some other inode Y created in a past transaction
4458 * and that was renamed in the current transaction. If we don't do this, then at
4459 * log replay time we can lose inode Y (and all its files if it's a directory):
4460 *
4461 * mkdir /mnt/x
4462 * echo "hello world" > /mnt/x/foobar
4463 * sync
4464 * mv /mnt/x /mnt/y
4465 * mkdir /mnt/x # or touch /mnt/x
4466 * xfs_io -c fsync /mnt/x
4467 * <power fail>
4468 * mount fs, trigger log replay
4469 *
4470 * After the log replay procedure, we would lose the first directory and all its
4471 * files (file foobar).
4472 * For the case where inode Y is not a directory we simply end up losing it:
4473 *
4474 * echo "123" > /mnt/foo
4475 * sync
4476 * mv /mnt/foo /mnt/bar
4477 * echo "abc" > /mnt/foo
4478 * xfs_io -c fsync /mnt/foo
4479 * <power fail>
4480 *
4481 * We also need this for cases where a snapshot entry is replaced by some other
4482 * entry (file or directory) otherwise we end up with an unreplayable log due to
4483 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4484 * if it were a regular entry:
4485 *
4486 * mkdir /mnt/x
4487 * btrfs subvolume snapshot /mnt /mnt/x/snap
4488 * btrfs subvolume delete /mnt/x/snap
4489 * rmdir /mnt/x
4490 * mkdir /mnt/x
4491 * fsync /mnt/x or fsync some new file inside it
4492 * <power fail>
4493 *
4494 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4495 * the same transaction.
4496 */
4497static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4498 const int slot,
4499 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004500 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004501 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004502{
4503 int ret;
4504 struct btrfs_path *search_path;
4505 char *name = NULL;
4506 u32 name_len = 0;
4507 u32 item_size = btrfs_item_size_nr(eb, slot);
4508 u32 cur_offset = 0;
4509 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4510
4511 search_path = btrfs_alloc_path();
4512 if (!search_path)
4513 return -ENOMEM;
4514 search_path->search_commit_root = 1;
4515 search_path->skip_locking = 1;
4516
4517 while (cur_offset < item_size) {
4518 u64 parent;
4519 u32 this_name_len;
4520 u32 this_len;
4521 unsigned long name_ptr;
4522 struct btrfs_dir_item *di;
4523
4524 if (key->type == BTRFS_INODE_REF_KEY) {
4525 struct btrfs_inode_ref *iref;
4526
4527 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4528 parent = key->offset;
4529 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4530 name_ptr = (unsigned long)(iref + 1);
4531 this_len = sizeof(*iref) + this_name_len;
4532 } else {
4533 struct btrfs_inode_extref *extref;
4534
4535 extref = (struct btrfs_inode_extref *)(ptr +
4536 cur_offset);
4537 parent = btrfs_inode_extref_parent(eb, extref);
4538 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4539 name_ptr = (unsigned long)&extref->name;
4540 this_len = sizeof(*extref) + this_name_len;
4541 }
4542
4543 if (this_name_len > name_len) {
4544 char *new_name;
4545
4546 new_name = krealloc(name, this_name_len, GFP_NOFS);
4547 if (!new_name) {
4548 ret = -ENOMEM;
4549 goto out;
4550 }
4551 name_len = this_name_len;
4552 name = new_name;
4553 }
4554
4555 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004556 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4557 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004558 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004559 struct btrfs_key di_key;
4560
4561 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4562 di, &di_key);
4563 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4564 ret = 1;
4565 *other_ino = di_key.objectid;
4566 } else {
4567 ret = -EAGAIN;
4568 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004569 goto out;
4570 } else if (IS_ERR(di)) {
4571 ret = PTR_ERR(di);
4572 goto out;
4573 }
4574 btrfs_release_path(search_path);
4575
4576 cur_offset += this_len;
4577 }
4578 ret = 0;
4579out:
4580 btrfs_free_path(search_path);
4581 kfree(name);
4582 return ret;
4583}
4584
Chris Masone02119d2008-09-05 16:13:11 -04004585/* log a single inode in the tree log.
4586 * At least one parent directory for this inode must exist in the tree
4587 * or be logged already.
4588 *
4589 * Any items from this inode changed by the current transaction are copied
4590 * to the log tree. An extra reference is taken on any extents in this
4591 * file, allowing us to avoid a whole pile of corner cases around logging
4592 * blocks that have been removed from the tree.
4593 *
4594 * See LOG_INODE_ALL and related defines for a description of what inode_only
4595 * does.
4596 *
4597 * This handles both files and directories.
4598 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004599static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004600 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004601 int inode_only,
4602 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004603 const loff_t end,
4604 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004605{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004606 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004607 struct btrfs_path *path;
4608 struct btrfs_path *dst_path;
4609 struct btrfs_key min_key;
4610 struct btrfs_key max_key;
4611 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004612 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004613 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004614 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004615 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004616 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004617 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004618 int ins_start_slot = 0;
4619 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004620 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004621 u64 ino = btrfs_ino(inode);
4622 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004623 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004624 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004625
Chris Masone02119d2008-09-05 16:13:11 -04004626 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004627 if (!path)
4628 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004629 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004630 if (!dst_path) {
4631 btrfs_free_path(path);
4632 return -ENOMEM;
4633 }
Chris Masone02119d2008-09-05 16:13:11 -04004634
Li Zefan33345d012011-04-20 10:31:50 +08004635 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004636 min_key.type = BTRFS_INODE_ITEM_KEY;
4637 min_key.offset = 0;
4638
Li Zefan33345d012011-04-20 10:31:50 +08004639 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004640
Chris Mason12fcfd22009-03-24 10:24:20 -04004641
Josef Bacik5dc562c2012-08-17 13:14:17 -04004642 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004643 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004644 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004645 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004646 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004647 max_key.type = BTRFS_XATTR_ITEM_KEY;
4648 else
4649 max_key.type = (u8)-1;
4650 max_key.offset = (u64)-1;
4651
Filipe Manana2c2c4522015-01-13 16:40:04 +00004652 /*
4653 * Only run delayed items if we are a dir or a new file.
4654 * Otherwise commit the delayed inode only, which is needed in
4655 * order for the log replay code to mark inodes for link count
4656 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4657 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004658 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4659 inode->generation > fs_info->last_trans_committed)
4660 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004661 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004662 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004663
4664 if (ret) {
4665 btrfs_free_path(path);
4666 btrfs_free_path(dst_path);
4667 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004668 }
4669
Liu Bo781feef2016-11-30 16:20:25 -08004670 if (inode_only == LOG_OTHER_INODE) {
4671 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004672 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004673 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004674 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004675 }
Chris Masone02119d2008-09-05 16:13:11 -04004676
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004677 /*
Chris Masone02119d2008-09-05 16:13:11 -04004678 * a brute force approach to making sure we get the most uptodate
4679 * copies of everything.
4680 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004681 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004682 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4683
Filipe Manana4f764e52015-02-23 19:53:35 +00004684 if (inode_only == LOG_INODE_EXISTS)
4685 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004686 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004687 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004688 if (inode_only == LOG_INODE_EXISTS) {
4689 /*
4690 * Make sure the new inode item we write to the log has
4691 * the same isize as the current one (if it exists).
4692 * This is necessary to prevent data loss after log
4693 * replay, and also to prevent doing a wrong expanding
4694 * truncate - for e.g. create file, write 4K into offset
4695 * 0, fsync, write 4K into offset 4096, add hard link,
4696 * fsync some other file (to sync log), power fail - if
4697 * we use the inode's current i_size, after log replay
4698 * we get a 8Kb file, with the last 4Kb extent as a hole
4699 * (zeroes), as if an expanding truncate happened,
4700 * instead of getting a file of 4Kb only.
4701 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004702 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004703 if (err)
4704 goto out_unlock;
4705 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004706 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004707 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004708 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004709 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004710 ret = drop_objectid_items(trans, log, path, ino,
4711 max_key.type);
4712 } else {
4713 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004714 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004715 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004716 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004717 while(1) {
4718 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004719 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004720 if (ret != -EAGAIN)
4721 break;
4722 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004723 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004724 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004725 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004726 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004727 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004728 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004729 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004730 ret = drop_objectid_items(trans, log, path, ino,
4731 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004732 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004733 if (inode_only == LOG_INODE_ALL)
4734 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004735 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004736 }
Josef Bacika95249b2012-10-11 16:17:34 -04004737
Chris Masone02119d2008-09-05 16:13:11 -04004738 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004739 if (ret) {
4740 err = ret;
4741 goto out_unlock;
4742 }
Chris Masone02119d2008-09-05 16:13:11 -04004743
Chris Masond3977122009-01-05 21:25:51 -05004744 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004745 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004746 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004747 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004748 if (ret < 0) {
4749 err = ret;
4750 goto out_unlock;
4751 }
Chris Masone02119d2008-09-05 16:13:11 -04004752 if (ret != 0)
4753 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004754again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004755 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004756 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004757 break;
4758 if (min_key.type > max_key.type)
4759 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004760
Filipe Mananae4545de2015-06-17 12:49:23 +01004761 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4762 need_log_inode_item = false;
4763
Filipe Manana56f23fd2016-03-30 23:37:21 +01004764 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4765 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004766 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004767 u64 other_ino = 0;
4768
Filipe Manana56f23fd2016-03-30 23:37:21 +01004769 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004770 path->slots[0], &min_key, inode,
4771 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004772 if (ret < 0) {
4773 err = ret;
4774 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004775 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004776 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004777 struct btrfs_key inode_key;
4778 struct inode *other_inode;
4779
4780 if (ins_nr > 0) {
4781 ins_nr++;
4782 } else {
4783 ins_nr = 1;
4784 ins_start_slot = path->slots[0];
4785 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02004786 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01004787 &last_extent, ins_start_slot,
4788 ins_nr, inode_only,
4789 logged_isize);
4790 if (ret < 0) {
4791 err = ret;
4792 goto out_unlock;
4793 }
4794 ins_nr = 0;
4795 btrfs_release_path(path);
4796 inode_key.objectid = other_ino;
4797 inode_key.type = BTRFS_INODE_ITEM_KEY;
4798 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004799 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01004800 &inode_key, root,
4801 NULL);
4802 /*
4803 * If the other inode that had a conflicting dir
4804 * entry was deleted in the current transaction,
4805 * we don't need to do more work nor fallback to
4806 * a transaction commit.
4807 */
4808 if (IS_ERR(other_inode) &&
4809 PTR_ERR(other_inode) == -ENOENT) {
4810 goto next_key;
4811 } else if (IS_ERR(other_inode)) {
4812 err = PTR_ERR(other_inode);
4813 goto out_unlock;
4814 }
4815 /*
4816 * We are safe logging the other inode without
4817 * acquiring its i_mutex as long as we log with
4818 * the LOG_INODE_EXISTS mode. We're safe against
4819 * concurrent renames of the other inode as well
4820 * because during a rename we pin the log and
4821 * update the log with the new name before we
4822 * unpin it.
4823 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004824 err = btrfs_log_inode(trans, root,
4825 BTRFS_I(other_inode),
4826 LOG_OTHER_INODE, 0, LLONG_MAX,
4827 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01004828 iput(other_inode);
4829 if (err)
4830 goto out_unlock;
4831 else
4832 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004833 }
4834 }
4835
Filipe Manana36283bf2015-06-20 00:44:51 +01004836 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4837 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4838 if (ins_nr == 0)
4839 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004840 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004841 &last_extent, ins_start_slot,
4842 ins_nr, inode_only, logged_isize);
4843 if (ret < 0) {
4844 err = ret;
4845 goto out_unlock;
4846 }
4847 ins_nr = 0;
4848 if (ret) {
4849 btrfs_release_path(path);
4850 continue;
4851 }
4852 goto next_slot;
4853 }
4854
Chris Masone02119d2008-09-05 16:13:11 -04004855 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004856 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4857 ins_nr++;
4858 goto next_slot;
4859 } else if (!ins_nr) {
4860 ins_start_slot = path->slots[0];
4861 ins_nr = 1;
4862 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004863 }
4864
Nikolay Borisova59108a2017-01-18 00:31:48 +02004865 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004866 ins_start_slot, ins_nr, inode_only,
4867 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004868 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004869 err = ret;
4870 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004871 }
4872 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004873 ins_nr = 0;
4874 btrfs_release_path(path);
4875 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004876 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004877 ins_nr = 1;
4878 ins_start_slot = path->slots[0];
4879next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004880
Chris Mason3a5f1d42008-09-11 15:53:37 -04004881 nritems = btrfs_header_nritems(path->nodes[0]);
4882 path->slots[0]++;
4883 if (path->slots[0] < nritems) {
4884 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4885 path->slots[0]);
4886 goto again;
4887 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004888 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004889 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04004890 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004891 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004892 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004893 err = ret;
4894 goto out_unlock;
4895 }
Josef Bacik16e75492013-10-22 12:18:51 -04004896 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004897 ins_nr = 0;
4898 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004899 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004900next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004901 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004902 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004903 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004904 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004905 min_key.offset = 0;
4906 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004907 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004908 }
Chris Masone02119d2008-09-05 16:13:11 -04004909 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004910 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004911 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004912 ins_start_slot, ins_nr, inode_only,
4913 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004914 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004915 err = ret;
4916 goto out_unlock;
4917 }
Josef Bacik16e75492013-10-22 12:18:51 -04004918 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004919 ins_nr = 0;
4920 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004921
Filipe Manana36283bf2015-06-20 00:44:51 +01004922 btrfs_release_path(path);
4923 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02004924 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01004925 if (err)
4926 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004927 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4928 btrfs_release_path(path);
4929 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02004930 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004931 if (err)
4932 goto out_unlock;
4933 }
Josef Bacika95249b2012-10-11 16:17:34 -04004934log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004935 btrfs_release_path(path);
4936 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004937 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004938 err = log_inode_item(trans, log, dst_path, inode);
Filipe Mananae4545de2015-06-17 12:49:23 +01004939 if (err)
4940 goto out_unlock;
4941 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004943 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004944 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004945 if (ret) {
4946 err = ret;
4947 goto out_unlock;
4948 }
Josef Bacikd006a042013-11-12 20:54:09 -05004949 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004950 struct extent_map *em, *n;
4951
Filipe Manana49dae1b2014-09-06 22:34:39 +01004952 write_lock(&em_tree->lock);
4953 /*
4954 * We can't just remove every em if we're called for a ranged
4955 * fsync - that is, one that doesn't cover the whole possible
4956 * file range (0 to LLONG_MAX). This is because we can have
4957 * em's that fall outside the range we're logging and therefore
4958 * their ordered operations haven't completed yet
4959 * (btrfs_finish_ordered_io() not invoked yet). This means we
4960 * didn't get their respective file extent item in the fs/subvol
4961 * tree yet, and need to let the next fast fsync (one which
4962 * consults the list of modified extent maps) find the em so
4963 * that it logs a matching file extent item and waits for the
4964 * respective ordered operation to complete (if it's still
4965 * running).
4966 *
4967 * Removing every em outside the range we're logging would make
4968 * the next fast fsync not log their matching file extent items,
4969 * therefore making us lose data after a log replay.
4970 */
4971 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4972 list) {
4973 const u64 mod_end = em->mod_start + em->mod_len - 1;
4974
4975 if (em->mod_start >= start && mod_end <= end)
4976 list_del_init(&em->list);
4977 }
4978 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004979 }
4980
Nikolay Borisova59108a2017-01-18 00:31:48 +02004981 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
4982 ret = log_directory_changes(trans, root, inode, path, dst_path,
4983 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004984 if (ret) {
4985 err = ret;
4986 goto out_unlock;
4987 }
Chris Masone02119d2008-09-05 16:13:11 -04004988 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004989
Nikolay Borisova59108a2017-01-18 00:31:48 +02004990 spin_lock(&inode->lock);
4991 inode->logged_trans = trans->transid;
4992 inode->last_log_commit = inode->last_sub_trans;
4993 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004994out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004995 if (unlikely(err))
4996 btrfs_put_logged_extents(&logged_list);
4997 else
4998 btrfs_submit_logged_extents(&logged_list, log);
Nikolay Borisova59108a2017-01-18 00:31:48 +02004999 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005000
5001 btrfs_free_path(path);
5002 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005003 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005004}
5005
Chris Mason12fcfd22009-03-24 10:24:20 -04005006/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005007 * Check if we must fallback to a transaction commit when logging an inode.
5008 * This must be called after logging the inode and is used only in the context
5009 * when fsyncing an inode requires the need to log some other inode - in which
5010 * case we can't lock the i_mutex of each other inode we need to log as that
5011 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5012 * log inodes up or down in the hierarchy) or rename operations for example. So
5013 * we take the log_mutex of the inode after we have logged it and then check for
5014 * its last_unlink_trans value - this is safe because any task setting
5015 * last_unlink_trans must take the log_mutex and it must do this before it does
5016 * the actual unlink operation, so if we do this check before a concurrent task
5017 * sets last_unlink_trans it means we've logged a consistent version/state of
5018 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005019 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005020 * we logged the inode or it might have also done the unlink).
5021 */
5022static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005023 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005024{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005025 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005026 bool ret = false;
5027
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005028 mutex_lock(&inode->log_mutex);
5029 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005030 /*
5031 * Make sure any commits to the log are forced to be full
5032 * commits.
5033 */
5034 btrfs_set_log_full_commit(fs_info, trans);
5035 ret = true;
5036 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005037 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005038
5039 return ret;
5040}
5041
5042/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005043 * follow the dentry parent pointers up the chain and see if any
5044 * of the directories in it require a full commit before they can
5045 * be logged. Returns zero if nothing special needs to be done or 1 if
5046 * a full commit is required.
5047 */
5048static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5049 struct inode *inode,
5050 struct dentry *parent,
5051 struct super_block *sb,
5052 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005053{
Chris Mason12fcfd22009-03-24 10:24:20 -04005054 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005055 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005056 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005057
Chris Masonaf4176b2009-03-24 10:24:31 -04005058 /*
5059 * for regular files, if its inode is already on disk, we don't
5060 * have to worry about the parents at all. This is because
5061 * we can use the last_unlink_trans field to record renames
5062 * and other fun in this file.
5063 */
5064 if (S_ISREG(inode->i_mode) &&
5065 BTRFS_I(inode)->generation <= last_committed &&
5066 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5067 goto out;
5068
Chris Mason12fcfd22009-03-24 10:24:20 -04005069 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005070 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005071 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005072 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005073 }
5074
5075 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005076 /*
5077 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005078 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005079 * logged_trans so that further down in the log code we don't
5080 * think this inode has already been logged.
5081 */
5082 if (inode != orig_inode)
5083 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005084 smp_mb();
5085
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005086 if (btrfs_must_commit_transaction(trans, BTRFS_I(inode))) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005087 ret = 1;
5088 break;
5089 }
5090
Al Virofc640052016-04-10 01:33:30 -04005091 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005092 break;
5093
Filipe Manana44f714d2016-06-06 16:11:13 +01005094 if (IS_ROOT(parent)) {
5095 inode = d_inode(parent);
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005096 if (btrfs_must_commit_transaction(trans, BTRFS_I(inode)))
Filipe Manana44f714d2016-06-06 16:11:13 +01005097 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005098 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005099 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005100
Josef Bacik6a912212010-11-20 09:48:00 +00005101 parent = dget_parent(parent);
5102 dput(old_parent);
5103 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005104 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005105
5106 }
Josef Bacik6a912212010-11-20 09:48:00 +00005107 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005108out:
Chris Masone02119d2008-09-05 16:13:11 -04005109 return ret;
5110}
5111
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005112struct btrfs_dir_list {
5113 u64 ino;
5114 struct list_head list;
5115};
5116
5117/*
5118 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5119 * details about the why it is needed.
5120 * This is a recursive operation - if an existing dentry corresponds to a
5121 * directory, that directory's new entries are logged too (same behaviour as
5122 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5123 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5124 * complains about the following circular lock dependency / possible deadlock:
5125 *
5126 * CPU0 CPU1
5127 * ---- ----
5128 * lock(&type->i_mutex_dir_key#3/2);
5129 * lock(sb_internal#2);
5130 * lock(&type->i_mutex_dir_key#3/2);
5131 * lock(&sb->s_type->i_mutex_key#14);
5132 *
5133 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5134 * sb_start_intwrite() in btrfs_start_transaction().
5135 * Not locking i_mutex of the inodes is still safe because:
5136 *
5137 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5138 * that while logging the inode new references (names) are added or removed
5139 * from the inode, leaving the logged inode item with a link count that does
5140 * not match the number of logged inode reference items. This is fine because
5141 * at log replay time we compute the real number of links and correct the
5142 * link count in the inode item (see replay_one_buffer() and
5143 * link_to_fixup_dir());
5144 *
5145 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5146 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5147 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5148 * has a size that doesn't match the sum of the lengths of all the logged
5149 * names. This does not result in a problem because if a dir_item key is
5150 * logged but its matching dir_index key is not logged, at log replay time we
5151 * don't use it to replay the respective name (see replay_one_name()). On the
5152 * other hand if only the dir_index key ends up being logged, the respective
5153 * name is added to the fs/subvol tree with both the dir_item and dir_index
5154 * keys created (see replay_one_name()).
5155 * The directory's inode item with a wrong i_size is not a problem as well,
5156 * since we don't use it at log replay time to set the i_size in the inode
5157 * item of the fs/subvol tree (see overwrite_item()).
5158 */
5159static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5160 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005161 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005162 struct btrfs_log_ctx *ctx)
5163{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005164 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005165 struct btrfs_root *log = root->log_root;
5166 struct btrfs_path *path;
5167 LIST_HEAD(dir_list);
5168 struct btrfs_dir_list *dir_elem;
5169 int ret = 0;
5170
5171 path = btrfs_alloc_path();
5172 if (!path)
5173 return -ENOMEM;
5174
5175 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5176 if (!dir_elem) {
5177 btrfs_free_path(path);
5178 return -ENOMEM;
5179 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005180 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005181 list_add_tail(&dir_elem->list, &dir_list);
5182
5183 while (!list_empty(&dir_list)) {
5184 struct extent_buffer *leaf;
5185 struct btrfs_key min_key;
5186 int nritems;
5187 int i;
5188
5189 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5190 list);
5191 if (ret)
5192 goto next_dir_inode;
5193
5194 min_key.objectid = dir_elem->ino;
5195 min_key.type = BTRFS_DIR_ITEM_KEY;
5196 min_key.offset = 0;
5197again:
5198 btrfs_release_path(path);
5199 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5200 if (ret < 0) {
5201 goto next_dir_inode;
5202 } else if (ret > 0) {
5203 ret = 0;
5204 goto next_dir_inode;
5205 }
5206
5207process_leaf:
5208 leaf = path->nodes[0];
5209 nritems = btrfs_header_nritems(leaf);
5210 for (i = path->slots[0]; i < nritems; i++) {
5211 struct btrfs_dir_item *di;
5212 struct btrfs_key di_key;
5213 struct inode *di_inode;
5214 struct btrfs_dir_list *new_dir_elem;
5215 int log_mode = LOG_INODE_EXISTS;
5216 int type;
5217
5218 btrfs_item_key_to_cpu(leaf, &min_key, i);
5219 if (min_key.objectid != dir_elem->ino ||
5220 min_key.type != BTRFS_DIR_ITEM_KEY)
5221 goto next_dir_inode;
5222
5223 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5224 type = btrfs_dir_type(leaf, di);
5225 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5226 type != BTRFS_FT_DIR)
5227 continue;
5228 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5229 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5230 continue;
5231
Robbie Koec125cf2016-10-28 10:48:26 +08005232 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005233 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005234 if (IS_ERR(di_inode)) {
5235 ret = PTR_ERR(di_inode);
5236 goto next_dir_inode;
5237 }
5238
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005239 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005240 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005241 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005242 }
5243
5244 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005245 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005246 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005247 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005248 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005249 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005250 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005251 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005252 iput(di_inode);
5253 if (ret)
5254 goto next_dir_inode;
5255 if (ctx->log_new_dentries) {
5256 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5257 GFP_NOFS);
5258 if (!new_dir_elem) {
5259 ret = -ENOMEM;
5260 goto next_dir_inode;
5261 }
5262 new_dir_elem->ino = di_key.objectid;
5263 list_add_tail(&new_dir_elem->list, &dir_list);
5264 }
5265 break;
5266 }
5267 if (i == nritems) {
5268 ret = btrfs_next_leaf(log, path);
5269 if (ret < 0) {
5270 goto next_dir_inode;
5271 } else if (ret > 0) {
5272 ret = 0;
5273 goto next_dir_inode;
5274 }
5275 goto process_leaf;
5276 }
5277 if (min_key.offset < (u64)-1) {
5278 min_key.offset++;
5279 goto again;
5280 }
5281next_dir_inode:
5282 list_del(&dir_elem->list);
5283 kfree(dir_elem);
5284 }
5285
5286 btrfs_free_path(path);
5287 return ret;
5288}
5289
Filipe Manana18aa0922015-08-05 16:49:08 +01005290static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5291 struct inode *inode,
5292 struct btrfs_log_ctx *ctx)
5293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana18aa0922015-08-05 16:49:08 +01005295 int ret;
5296 struct btrfs_path *path;
5297 struct btrfs_key key;
5298 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005299 const u64 ino = btrfs_ino(BTRFS_I(inode));
Filipe Manana18aa0922015-08-05 16:49:08 +01005300
5301 path = btrfs_alloc_path();
5302 if (!path)
5303 return -ENOMEM;
5304 path->skip_locking = 1;
5305 path->search_commit_root = 1;
5306
5307 key.objectid = ino;
5308 key.type = BTRFS_INODE_REF_KEY;
5309 key.offset = 0;
5310 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5311 if (ret < 0)
5312 goto out;
5313
5314 while (true) {
5315 struct extent_buffer *leaf = path->nodes[0];
5316 int slot = path->slots[0];
5317 u32 cur_offset = 0;
5318 u32 item_size;
5319 unsigned long ptr;
5320
5321 if (slot >= btrfs_header_nritems(leaf)) {
5322 ret = btrfs_next_leaf(root, path);
5323 if (ret < 0)
5324 goto out;
5325 else if (ret > 0)
5326 break;
5327 continue;
5328 }
5329
5330 btrfs_item_key_to_cpu(leaf, &key, slot);
5331 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5332 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5333 break;
5334
5335 item_size = btrfs_item_size_nr(leaf, slot);
5336 ptr = btrfs_item_ptr_offset(leaf, slot);
5337 while (cur_offset < item_size) {
5338 struct btrfs_key inode_key;
5339 struct inode *dir_inode;
5340
5341 inode_key.type = BTRFS_INODE_ITEM_KEY;
5342 inode_key.offset = 0;
5343
5344 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5345 struct btrfs_inode_extref *extref;
5346
5347 extref = (struct btrfs_inode_extref *)
5348 (ptr + cur_offset);
5349 inode_key.objectid = btrfs_inode_extref_parent(
5350 leaf, extref);
5351 cur_offset += sizeof(*extref);
5352 cur_offset += btrfs_inode_extref_name_len(leaf,
5353 extref);
5354 } else {
5355 inode_key.objectid = key.offset;
5356 cur_offset = item_size;
5357 }
5358
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005359 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005360 root, NULL);
5361 /* If parent inode was deleted, skip it. */
5362 if (IS_ERR(dir_inode))
5363 continue;
5364
Filipe Manana657ed1a2016-04-06 17:11:56 +01005365 if (ctx)
5366 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005367 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005368 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005369 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005370 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005371 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005372 if (!ret && ctx && ctx->log_new_dentries)
5373 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005374 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005375 iput(dir_inode);
5376 if (ret)
5377 goto out;
5378 }
5379 path->slots[0]++;
5380 }
5381 ret = 0;
5382out:
5383 btrfs_free_path(path);
5384 return ret;
5385}
5386
Chris Masone02119d2008-09-05 16:13:11 -04005387/*
5388 * helper function around btrfs_log_inode to make sure newly created
5389 * parent directories also end up in the log. A minimal inode and backref
5390 * only logging is done of any parent directories that are older than
5391 * the last committed transaction
5392 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005393static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5394 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005395 struct dentry *parent,
5396 const loff_t start,
5397 const loff_t end,
5398 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005399 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005400{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005401 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason12fcfd22009-03-24 10:24:20 -04005402 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005403 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005404 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005405 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005407 bool log_dentries = false;
5408 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005409
5410 sb = inode->i_sb;
5411
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005412 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005413 ret = 1;
5414 goto end_no_trans;
5415 }
5416
Miao Xie995946d2014-04-02 19:51:06 +08005417 /*
5418 * The prev transaction commit doesn't complete, we need do
5419 * full commit by ourselves.
5420 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005421 if (fs_info->last_trans_log_full_commit >
5422 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005423 ret = 1;
5424 goto end_no_trans;
5425 }
5426
Yan, Zheng76dda932009-09-21 16:00:26 -04005427 if (root != BTRFS_I(inode)->root ||
5428 btrfs_root_refs(&root->root_item) == 0) {
5429 ret = 1;
5430 goto end_no_trans;
5431 }
5432
Chris Mason12fcfd22009-03-24 10:24:20 -04005433 ret = check_parent_dirs_for_sync(trans, inode, parent,
5434 sb, last_committed);
5435 if (ret)
5436 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005437
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005438 if (btrfs_inode_in_log(BTRFS_I(inode), trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005439 ret = BTRFS_NO_LOG_SYNC;
5440 goto end_no_trans;
5441 }
5442
Miao Xie8b050d32014-02-20 18:08:58 +08005443 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005444 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005445 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005446
Nikolay Borisova59108a2017-01-18 00:31:48 +02005447 ret = btrfs_log_inode(trans, root, BTRFS_I(inode), inode_only,
5448 start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005449 if (ret)
5450 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005451
Chris Masonaf4176b2009-03-24 10:24:31 -04005452 /*
5453 * for regular files, if its inode is already on disk, we don't
5454 * have to worry about the parents at all. This is because
5455 * we can use the last_unlink_trans field to record renames
5456 * and other fun in this file.
5457 */
5458 if (S_ISREG(inode->i_mode) &&
5459 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005460 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5461 ret = 0;
5462 goto end_trans;
5463 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005464
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005465 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5466 log_dentries = true;
5467
Filipe Manana18aa0922015-08-05 16:49:08 +01005468 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005469 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005470 * inodes are fully logged. This is to prevent leaving dangling
5471 * directory index entries in directories that were our parents but are
5472 * not anymore. Not doing this results in old parent directory being
5473 * impossible to delete after log replay (rmdir will always fail with
5474 * error -ENOTEMPTY).
5475 *
5476 * Example 1:
5477 *
5478 * mkdir testdir
5479 * touch testdir/foo
5480 * ln testdir/foo testdir/bar
5481 * sync
5482 * unlink testdir/bar
5483 * xfs_io -c fsync testdir/foo
5484 * <power failure>
5485 * mount fs, triggers log replay
5486 *
5487 * If we don't log the parent directory (testdir), after log replay the
5488 * directory still has an entry pointing to the file inode using the bar
5489 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5490 * the file inode has a link count of 1.
5491 *
5492 * Example 2:
5493 *
5494 * mkdir testdir
5495 * touch foo
5496 * ln foo testdir/foo2
5497 * ln foo testdir/foo3
5498 * sync
5499 * unlink testdir/foo3
5500 * xfs_io -c fsync foo
5501 * <power failure>
5502 * mount fs, triggers log replay
5503 *
5504 * Similar as the first example, after log replay the parent directory
5505 * testdir still has an entry pointing to the inode file with name foo3
5506 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5507 * and has a link count of 2.
5508 */
5509 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5510 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5511 if (ret)
5512 goto end_trans;
5513 }
5514
Chris Masond3977122009-01-05 21:25:51 -05005515 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005516 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005517 break;
5518
David Howells2b0143b2015-03-17 22:25:59 +00005519 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005520 if (root != BTRFS_I(inode)->root)
5521 break;
5522
Filipe Manana18aa0922015-08-05 16:49:08 +01005523 if (BTRFS_I(inode)->generation > last_committed) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005524 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005525 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005526 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005527 if (ret)
5528 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005529 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005531 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005532
Josef Bacik6a912212010-11-20 09:48:00 +00005533 parent = dget_parent(parent);
5534 dput(old_parent);
5535 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005536 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005537 if (log_dentries)
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005538 ret = log_new_dir_dentries(trans, root, BTRFS_I(orig_inode), ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005539 else
5540 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005541end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005542 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005543 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005544 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005545 ret = 1;
5546 }
Miao Xie8b050d32014-02-20 18:08:58 +08005547
5548 if (ret)
5549 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005550 btrfs_end_log_trans(root);
5551end_no_trans:
5552 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005553}
5554
5555/*
5556 * it is not safe to log dentry if the chunk root has added new
5557 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5558 * If this returns 1, you must commit the transaction to safely get your
5559 * data on disk.
5560 */
5561int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005562 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005563 const loff_t start,
5564 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005565 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005566{
Josef Bacik6a912212010-11-20 09:48:00 +00005567 struct dentry *parent = dget_parent(dentry);
5568 int ret;
5569
David Howells2b0143b2015-03-17 22:25:59 +00005570 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005571 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005572 dput(parent);
5573
5574 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005575}
5576
5577/*
5578 * should be called during mount to recover any replay any log trees
5579 * from the FS
5580 */
5581int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5582{
5583 int ret;
5584 struct btrfs_path *path;
5585 struct btrfs_trans_handle *trans;
5586 struct btrfs_key key;
5587 struct btrfs_key found_key;
5588 struct btrfs_key tmp_key;
5589 struct btrfs_root *log;
5590 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5591 struct walk_control wc = {
5592 .process_func = process_one_buffer,
5593 .stage = 0,
5594 };
5595
Chris Masone02119d2008-09-05 16:13:11 -04005596 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005597 if (!path)
5598 return -ENOMEM;
5599
Josef Bacikafcdd122016-09-02 15:40:02 -04005600 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005601
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005602 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005603 if (IS_ERR(trans)) {
5604 ret = PTR_ERR(trans);
5605 goto error;
5606 }
Chris Masone02119d2008-09-05 16:13:11 -04005607
5608 wc.trans = trans;
5609 wc.pin = 1;
5610
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005611 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005612 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005613 btrfs_handle_fs_error(fs_info, ret,
5614 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005615 goto error;
5616 }
Chris Masone02119d2008-09-05 16:13:11 -04005617
5618again:
5619 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5620 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005621 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005622
Chris Masond3977122009-01-05 21:25:51 -05005623 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005624 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005625
5626 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005627 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005628 "Couldn't find tree log root.");
5629 goto error;
5630 }
Chris Masone02119d2008-09-05 16:13:11 -04005631 if (ret > 0) {
5632 if (path->slots[0] == 0)
5633 break;
5634 path->slots[0]--;
5635 }
5636 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5637 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005638 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005639 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5640 break;
5641
Miao Xiecb517ea2013-05-15 07:48:19 +00005642 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005643 if (IS_ERR(log)) {
5644 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005645 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005646 "Couldn't read tree log root.");
5647 goto error;
5648 }
Chris Masone02119d2008-09-05 16:13:11 -04005649
5650 tmp_key.objectid = found_key.offset;
5651 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5652 tmp_key.offset = (u64)-1;
5653
5654 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005655 if (IS_ERR(wc.replay_dest)) {
5656 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005657 free_extent_buffer(log->node);
5658 free_extent_buffer(log->commit_root);
5659 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005660 btrfs_handle_fs_error(fs_info, ret,
5661 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005662 goto error;
5663 }
Chris Masone02119d2008-09-05 16:13:11 -04005664
Yan Zheng07d400a2009-01-06 11:42:00 -05005665 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005666 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005667 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005668
Josef Bacikb50c6e22013-04-25 15:55:30 -04005669 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005670 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5671 path);
Chris Masone02119d2008-09-05 16:13:11 -04005672 }
Chris Masone02119d2008-09-05 16:13:11 -04005673
5674 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005675 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005676 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005677 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005678 kfree(log);
5679
Josef Bacikb50c6e22013-04-25 15:55:30 -04005680 if (ret)
5681 goto error;
5682
Chris Masone02119d2008-09-05 16:13:11 -04005683 if (found_key.offset == 0)
5684 break;
5685 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005686 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005687
5688 /* step one is to pin it all, step two is to replay just inodes */
5689 if (wc.pin) {
5690 wc.pin = 0;
5691 wc.process_func = replay_one_buffer;
5692 wc.stage = LOG_WALK_REPLAY_INODES;
5693 goto again;
5694 }
5695 /* step three is to replay everything */
5696 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5697 wc.stage++;
5698 goto again;
5699 }
5700
5701 btrfs_free_path(path);
5702
Josef Bacikabefa552013-04-24 16:40:05 -04005703 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005704 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005705 if (ret)
5706 return ret;
5707
Chris Masone02119d2008-09-05 16:13:11 -04005708 free_extent_buffer(log_root_tree->node);
5709 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005710 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005711 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005712
Josef Bacikabefa552013-04-24 16:40:05 -04005713 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005714error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005715 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005716 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005717 btrfs_free_path(path);
5718 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005719}
Chris Mason12fcfd22009-03-24 10:24:20 -04005720
5721/*
5722 * there are some corner cases where we want to force a full
5723 * commit instead of allowing a directory to be logged.
5724 *
5725 * They revolve around files there were unlinked from the directory, and
5726 * this function updates the parent directory so that a full commit is
5727 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005728 *
5729 * Must be called before the unlink operations (updates to the subvolume tree,
5730 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005731 */
5732void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005733 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005734 int for_rename)
5735{
5736 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005737 * when we're logging a file, if it hasn't been renamed
5738 * or unlinked, and its inode is fully committed on disk,
5739 * we don't have to worry about walking up the directory chain
5740 * to log its parents.
5741 *
5742 * So, we use the last_unlink_trans field to put this transid
5743 * into the file. When the file is logged we check it and
5744 * don't log the parents if the file is fully on disk.
5745 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005746 mutex_lock(&inode->log_mutex);
5747 inode->last_unlink_trans = trans->transid;
5748 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005749
5750 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005751 * if this directory was already logged any new
5752 * names for this file/dir will get recorded
5753 */
5754 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005755 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005756 return;
5757
5758 /*
5759 * if the inode we're about to unlink was logged,
5760 * the log will be properly updated for any new names
5761 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005762 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005763 return;
5764
5765 /*
5766 * when renaming files across directories, if the directory
5767 * there we're unlinking from gets fsync'd later on, there's
5768 * no way to find the destination directory later and fsync it
5769 * properly. So, we have to be conservative and force commits
5770 * so the new name gets discovered.
5771 */
5772 if (for_rename)
5773 goto record;
5774
5775 /* we can safely do the unlink without any special recording */
5776 return;
5777
5778record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005779 mutex_lock(&dir->log_mutex);
5780 dir->last_unlink_trans = trans->transid;
5781 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005782}
5783
5784/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005785 * Make sure that if someone attempts to fsync the parent directory of a deleted
5786 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5787 * that after replaying the log tree of the parent directory's root we will not
5788 * see the snapshot anymore and at log replay time we will not see any log tree
5789 * corresponding to the deleted snapshot's root, which could lead to replaying
5790 * it after replaying the log tree of the parent directory (which would replay
5791 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005792 *
5793 * Must be called before the actual snapshot destroy operation (updates to the
5794 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005795 */
5796void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02005797 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005798{
Nikolay Borisov43663552017-01-18 00:31:29 +02005799 mutex_lock(&dir->log_mutex);
5800 dir->last_unlink_trans = trans->transid;
5801 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005802}
5803
5804/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005805 * Call this after adding a new name for a file and it will properly
5806 * update the log to reflect the new name.
5807 *
5808 * It will return zero if all goes well, and it will return 1 if a
5809 * full transaction commit is required.
5810 */
5811int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005812 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Chris Mason12fcfd22009-03-24 10:24:20 -04005813 struct dentry *parent)
5814{
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
David Sterbaf85b7372017-01-20 14:54:07 +01005816 struct btrfs_root *root = inode->root;
Chris Mason12fcfd22009-03-24 10:24:20 -04005817
5818 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005819 * this will force the logging code to walk the dentry chain
5820 * up for the file
5821 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005822 if (S_ISREG(inode->vfs_inode.i_mode))
5823 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04005824
5825 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005826 * if this inode hasn't been logged and directory we're renaming it
5827 * from hasn't been logged, we don't need to log it
5828 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005829 if (inode->logged_trans <= fs_info->last_trans_committed &&
5830 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Chris Mason12fcfd22009-03-24 10:24:20 -04005831 return 0;
5832
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02005833 return btrfs_log_inode_parent(trans, root, &inode->vfs_inode, parent, 0,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005834 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005835}
5836