blob: 1d1ba083ca6ecc1497bd1c141a431a2d5fa26cd9 [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"
Chris Masone02119d2008-09-05 16:13:11 -040029
30/* magic values for the inode_only field in btrfs_log_inode:
31 *
32 * LOG_INODE_ALL means to log everything
33 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 * during log replay
35 */
36#define LOG_INODE_ALL 0
37#define LOG_INODE_EXISTS 1
38
39/*
Chris Mason12fcfd22009-03-24 10:24:20 -040040 * directory trouble cases
41 *
42 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
43 * log, we must force a full commit before doing an fsync of the directory
44 * where the unlink was done.
45 * ---> record transid of last unlink/rename per directory
46 *
47 * mkdir foo/some_dir
48 * normal commit
49 * rename foo/some_dir foo2/some_dir
50 * mkdir foo/some_dir
51 * fsync foo/some_dir/some_file
52 *
53 * The fsync above will unlink the original some_dir without recording
54 * it in its new location (foo2). After a crash, some_dir will be gone
55 * unless the fsync of some_file forces a full commit
56 *
57 * 2) we must log any new names for any file or dir that is in the fsync
58 * log. ---> check inode while renaming/linking.
59 *
60 * 2a) we must log any new names for any file or dir during rename
61 * when the directory they are being removed from was logged.
62 * ---> check inode and old parent dir during rename
63 *
64 * 2a is actually the more important variant. With the extra logging
65 * a crash might unlink the old name without recreating the new one
66 *
67 * 3) after a crash, we must go through any directories with a link count
68 * of zero and redo the rm -rf
69 *
70 * mkdir f1/foo
71 * normal commit
72 * rm -rf f1/foo
73 * fsync(f1)
74 *
75 * The directory f1 was fully removed from the FS, but fsync was never
76 * called on f1, only its parent dir. After a crash the rm -rf must
77 * be replayed. This must be able to recurse down the entire
78 * directory tree. The inode link count fixup code takes care of the
79 * ugly details.
80 */
81
82/*
Chris Masone02119d2008-09-05 16:13:11 -040083 * stages for the tree walking. The first
84 * stage (0) is to only pin down the blocks we find
85 * the second stage (1) is to make sure that all the inodes
86 * we find in the log are created in the subvolume.
87 *
88 * The last stage is to deal with directories and links and extents
89 * and all the other fun semantics
90 */
91#define LOG_WALK_PIN_ONLY 0
92#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040093#define LOG_WALK_REPLAY_DIR_INDEX 2
94#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040095
Chris Mason12fcfd22009-03-24 10:24:20 -040096static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +010097 struct btrfs_root *root, struct inode *inode,
98 int inode_only,
99 const loff_t start,
100 const loff_t end);
Yan Zhengec051c02009-01-05 15:43:42 -0500101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400104static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_root *log,
107 struct btrfs_path *path,
108 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800139 struct btrfs_root *root,
140 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400141{
Miao Xie8b050d32014-02-20 18:08:58 +0800142 int index;
Chris Masone02119d2008-09-05 16:13:11 -0400143 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800147 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800148 ret = -EAGAIN;
149 goto out;
150 }
Josef Bacikff782e02009-10-08 15:30:04 -0400151 if (!root->log_start_pid) {
152 root->log_start_pid = current->pid;
Miao Xie27cdeb72014-04-02 19:51:05 +0800153 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400154 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800155 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400156 }
157
Miao Xie2ecb7922012-09-06 04:04:27 -0600158 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500159 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800160 if (ctx) {
161 index = root->log_transid % 2;
162 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800163 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800164 }
Yan Zheng7237f182009-01-21 12:54:03 -0500165 mutex_unlock(&root->log_mutex);
166 return 0;
167 }
Miao Xiee87ac132014-02-20 18:08:53 +0800168
169 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400170 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800171 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400172 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800173 mutex_unlock(&root->fs_info->tree_log_mutex);
174 if (ret)
175 goto out;
176
177 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400178 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400179 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800180 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400181 }
Miao Xie27cdeb72014-04-02 19:51:05 +0800182 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Miao Xiee87ac132014-02-20 18:08:53 +0800183 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600184 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500185 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800186 if (ctx) {
187 index = root->log_transid % 2;
188 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800189 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800190 }
Miao Xiee87ac132014-02-20 18:08:53 +0800191out:
Yan Zheng7237f182009-01-21 12:54:03 -0500192 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800193 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400194}
195
196/*
197 * returns 0 if there was a log transaction running and we were able
198 * to join, or returns -ENOENT if there were not transactions
199 * in progress
200 */
201static int join_running_log_trans(struct btrfs_root *root)
202{
203 int ret = -ENOENT;
204
205 smp_mb();
206 if (!root->log_root)
207 return -ENOENT;
208
Yan Zheng7237f182009-01-21 12:54:03 -0500209 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400210 if (root->log_root) {
211 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500212 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400213 }
Yan Zheng7237f182009-01-21 12:54:03 -0500214 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400215 return ret;
216}
217
218/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400219 * This either makes the current running log transaction wait
220 * until you call btrfs_end_log_trans() or it makes any future
221 * log transactions wait until you call btrfs_end_log_trans()
222 */
223int btrfs_pin_log_trans(struct btrfs_root *root)
224{
225 int ret = -ENOENT;
226
227 mutex_lock(&root->log_mutex);
228 atomic_inc(&root->log_writers);
229 mutex_unlock(&root->log_mutex);
230 return ret;
231}
232
233/*
Chris Masone02119d2008-09-05 16:13:11 -0400234 * indicate we're done making changes to the log tree
235 * and wake up anyone waiting to do a sync
236 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100237void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400238{
Yan Zheng7237f182009-01-21 12:54:03 -0500239 if (atomic_dec_and_test(&root->log_writers)) {
240 smp_mb();
241 if (waitqueue_active(&root->log_writer_wait))
242 wake_up(&root->log_writer_wait);
243 }
Chris Masone02119d2008-09-05 16:13:11 -0400244}
245
246
247/*
248 * the walk control struct is used to pass state down the chain when
249 * processing the log tree. The stage field tells us which part
250 * of the log tree processing we are currently doing. The others
251 * are state fields used for that specific part
252 */
253struct walk_control {
254 /* should we free the extent on disk when done? This is used
255 * at transaction commit time while freeing a log tree
256 */
257 int free;
258
259 /* should we write out the extent buffer? This is used
260 * while flushing the log tree to disk during a sync
261 */
262 int write;
263
264 /* should we wait for the extent buffer io to finish? Also used
265 * while flushing the log tree to disk for a sync
266 */
267 int wait;
268
269 /* pin only walk, we record which extents on disk belong to the
270 * log trees
271 */
272 int pin;
273
274 /* what stage of the replay code we're currently in */
275 int stage;
276
277 /* the root we are currently replaying */
278 struct btrfs_root *replay_dest;
279
280 /* the trans handle for the current replay */
281 struct btrfs_trans_handle *trans;
282
283 /* the function that gets used to process blocks we find in the
284 * tree. Note the extent_buffer might not be up to date when it is
285 * passed in, and it must be checked or read if you need the data
286 * inside it
287 */
288 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
289 struct walk_control *wc, u64 gen);
290};
291
292/*
293 * process_func used to pin down extents, write them or wait on them
294 */
295static int process_one_buffer(struct btrfs_root *log,
296 struct extent_buffer *eb,
297 struct walk_control *wc, u64 gen)
298{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400299 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400300
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400301 /*
302 * If this fs is mixed then we need to be able to process the leaves to
303 * pin down any logged extents, so we have to read the block.
304 */
305 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
306 ret = btrfs_read_buffer(eb, gen);
307 if (ret)
308 return ret;
309 }
310
Josef Bacikb50c6e22013-04-25 15:55:30 -0400311 if (wc->pin)
312 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
313 eb->start, eb->len);
314
315 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400316 if (wc->pin && btrfs_header_level(eb) == 0)
317 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400318 if (wc->write)
319 btrfs_write_tree_block(eb);
320 if (wc->wait)
321 btrfs_wait_tree_block_writeback(eb);
322 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400323 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400324}
325
326/*
327 * Item overwrite used by replay and tree logging. eb, slot and key all refer
328 * to the src data we are copying out.
329 *
330 * root is the tree we are copying into, and path is a scratch
331 * path for use in this function (it should be released on entry and
332 * will be released on exit).
333 *
334 * If the key is already in the destination tree the existing item is
335 * overwritten. If the existing item isn't big enough, it is extended.
336 * If it is too large, it is truncated.
337 *
338 * If the key isn't in the destination yet, a new item is inserted.
339 */
340static noinline int overwrite_item(struct btrfs_trans_handle *trans,
341 struct btrfs_root *root,
342 struct btrfs_path *path,
343 struct extent_buffer *eb, int slot,
344 struct btrfs_key *key)
345{
346 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 */
455 ret = btrfs_insert_empty_item(trans, root, path,
456 key, item_size);
457
458 /* make sure any existing item is the correct size */
459 if (ret == -EEXIST) {
460 u32 found_size;
461 found_size = btrfs_item_size_nr(path->nodes[0],
462 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100463 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000464 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000466 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400468 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400469 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400470 }
471 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
472 path->slots[0]);
473
474 /* don't overwrite an existing inode if the generation number
475 * was logged as zero. This is done when the tree logging code
476 * is just logging an inode to make sure it exists after recovery.
477 *
478 * Also, don't overwrite i_size on directories during replay.
479 * log replay inserts and removes directory items based on the
480 * state of the tree found in the subvolume, and i_size is modified
481 * as it goes
482 */
483 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
484 struct btrfs_inode_item *src_item;
485 struct btrfs_inode_item *dst_item;
486
487 src_item = (struct btrfs_inode_item *)src_ptr;
488 dst_item = (struct btrfs_inode_item *)dst_ptr;
489
490 if (btrfs_inode_generation(eb, src_item) == 0)
491 goto no_copy;
492
493 if (overwrite_root &&
494 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
495 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
496 save_old_i_size = 1;
497 saved_i_size = btrfs_inode_size(path->nodes[0],
498 dst_item);
499 }
500 }
501
502 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
503 src_ptr, item_size);
504
505 if (save_old_i_size) {
506 struct btrfs_inode_item *dst_item;
507 dst_item = (struct btrfs_inode_item *)dst_ptr;
508 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
509 }
510
511 /* make sure the generation is filled in */
512 if (key->type == BTRFS_INODE_ITEM_KEY) {
513 struct btrfs_inode_item *dst_item;
514 dst_item = (struct btrfs_inode_item *)dst_ptr;
515 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
516 btrfs_set_inode_generation(path->nodes[0], dst_item,
517 trans->transid);
518 }
519 }
520no_copy:
521 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200522 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400523 return 0;
524}
525
526/*
527 * simple helper to read an inode off the disk from a given root
528 * This can only be called for subvolume roots and not for the log
529 */
530static noinline struct inode *read_one_inode(struct btrfs_root *root,
531 u64 objectid)
532{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400533 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400534 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400535
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400536 key.objectid = objectid;
537 key.type = BTRFS_INODE_ITEM_KEY;
538 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000539 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400540 if (IS_ERR(inode)) {
541 inode = NULL;
542 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400543 iput(inode);
544 inode = NULL;
545 }
546 return inode;
547}
548
549/* replays a single extent in 'eb' at 'slot' with 'key' into the
550 * subvolume 'root'. path is released on entry and should be released
551 * on exit.
552 *
553 * extents in the log tree have not been allocated out of the extent
554 * tree yet. So, this completes the allocation, taking a reference
555 * as required if the extent already exists or creating a new extent
556 * if it isn't in the extent allocation tree yet.
557 *
558 * The extent is inserted into the file, dropping any existing extents
559 * from the file that overlap the new one.
560 */
561static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
562 struct btrfs_root *root,
563 struct btrfs_path *path,
564 struct extent_buffer *eb, int slot,
565 struct btrfs_key *key)
566{
567 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400568 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400569 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000570 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400571 struct btrfs_file_extent_item *item;
572 struct inode *inode = NULL;
573 unsigned long size;
574 int ret = 0;
575
576 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
577 found_type = btrfs_file_extent_type(eb, item);
578
Yan Zhengd899e052008-10-30 14:25:28 -0400579 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000580 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
581 nbytes = btrfs_file_extent_num_bytes(eb, item);
582 extent_end = start + nbytes;
583
584 /*
585 * We don't add to the inodes nbytes if we are prealloc or a
586 * hole.
587 */
588 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
589 nbytes = 0;
590 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800591 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000592 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000593 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400594 } else {
595 ret = 0;
596 goto out;
597 }
598
599 inode = read_one_inode(root, key->objectid);
600 if (!inode) {
601 ret = -EIO;
602 goto out;
603 }
604
605 /*
606 * first check to see if we already have this extent in the
607 * file. This must be done before the btrfs_drop_extents run
608 * so we don't try to drop this extent.
609 */
Li Zefan33345d012011-04-20 10:31:50 +0800610 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400611 start, 0);
612
Yan Zhengd899e052008-10-30 14:25:28 -0400613 if (ret == 0 &&
614 (found_type == BTRFS_FILE_EXTENT_REG ||
615 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400616 struct btrfs_file_extent_item cmp1;
617 struct btrfs_file_extent_item cmp2;
618 struct btrfs_file_extent_item *existing;
619 struct extent_buffer *leaf;
620
621 leaf = path->nodes[0];
622 existing = btrfs_item_ptr(leaf, path->slots[0],
623 struct btrfs_file_extent_item);
624
625 read_extent_buffer(eb, &cmp1, (unsigned long)item,
626 sizeof(cmp1));
627 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
628 sizeof(cmp2));
629
630 /*
631 * we already have a pointer to this exact extent,
632 * we don't have to do anything
633 */
634 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200635 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400636 goto out;
637 }
638 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200639 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400640
641 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400642 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400643 if (ret)
644 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400645
Yan Zheng07d400a2009-01-06 11:42:00 -0500646 if (found_type == BTRFS_FILE_EXTENT_REG ||
647 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400648 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500649 unsigned long dest_offset;
650 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400651
Yan Zheng07d400a2009-01-06 11:42:00 -0500652 ret = btrfs_insert_empty_item(trans, root, path, key,
653 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400654 if (ret)
655 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500656 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
657 path->slots[0]);
658 copy_extent_buffer(path->nodes[0], eb, dest_offset,
659 (unsigned long)item, sizeof(*item));
660
661 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
662 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
663 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500665
666 if (ins.objectid > 0) {
667 u64 csum_start;
668 u64 csum_end;
669 LIST_HEAD(ordered_sums);
670 /*
671 * is this extent already allocated in the extent
672 * allocation tree? If so, just add a reference
673 */
674 ret = btrfs_lookup_extent(root, ins.objectid,
675 ins.offset);
676 if (ret == 0) {
677 ret = btrfs_inc_extent_ref(trans, root,
678 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200680 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400681 if (ret)
682 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500683 } else {
684 /*
685 * insert the extent pointer in the extent
686 * allocation tree
687 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 ret = btrfs_alloc_logged_file_extent(trans,
689 root, root->root_key.objectid,
690 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400691 if (ret)
692 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500693 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200694 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500695
696 if (btrfs_file_extent_compression(eb, item)) {
697 csum_start = ins.objectid;
698 csum_end = csum_start + ins.offset;
699 } else {
700 csum_start = ins.objectid +
701 btrfs_file_extent_offset(eb, item);
702 csum_end = csum_start +
703 btrfs_file_extent_num_bytes(eb, item);
704 }
705
706 ret = btrfs_lookup_csums_range(root->log_root,
707 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100708 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400709 if (ret)
710 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500711 while (!list_empty(&ordered_sums)) {
712 struct btrfs_ordered_sum *sums;
713 sums = list_entry(ordered_sums.next,
714 struct btrfs_ordered_sum,
715 list);
Josef Bacik36508602013-04-25 16:23:32 -0400716 if (!ret)
717 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500718 root->fs_info->csum_root,
719 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500720 list_del(&sums->list);
721 kfree(sums);
722 }
Josef Bacik36508602013-04-25 16:23:32 -0400723 if (ret)
724 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500725 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200726 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500727 }
728 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
729 /* inline extents are easy, we just overwrite them */
730 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400731 if (ret)
732 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500733 }
734
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000735 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600736 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400737out:
738 if (inode)
739 iput(inode);
740 return ret;
741}
742
743/*
744 * when cleaning up conflicts between the directory names in the
745 * subvolume, directory names in the log and directory names in the
746 * inode back references, we may have to unlink inodes from directories.
747 *
748 * This is a helper function to do the unlink of a specific directory
749 * item
750 */
751static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root,
753 struct btrfs_path *path,
754 struct inode *dir,
755 struct btrfs_dir_item *di)
756{
757 struct inode *inode;
758 char *name;
759 int name_len;
760 struct extent_buffer *leaf;
761 struct btrfs_key location;
762 int ret;
763
764 leaf = path->nodes[0];
765
766 btrfs_dir_item_key_to_cpu(leaf, di, &location);
767 name_len = btrfs_dir_name_len(leaf, di);
768 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000769 if (!name)
770 return -ENOMEM;
771
Chris Masone02119d2008-09-05 16:13:11 -0400772 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200773 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400774
775 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000776 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400777 ret = -EIO;
778 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000779 }
Chris Masone02119d2008-09-05 16:13:11 -0400780
Yan Zhengec051c02009-01-05 15:43:42 -0500781 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400782 if (ret)
783 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400784
Chris Masone02119d2008-09-05 16:13:11 -0400785 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400786 if (ret)
787 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100788 else
789 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400790out:
791 kfree(name);
792 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400793 return ret;
794}
795
796/*
797 * helper function to see if a given name and sequence number found
798 * in an inode back reference are already in a directory and correctly
799 * point to this inode
800 */
801static noinline int inode_in_dir(struct btrfs_root *root,
802 struct btrfs_path *path,
803 u64 dirid, u64 objectid, u64 index,
804 const char *name, int name_len)
805{
806 struct btrfs_dir_item *di;
807 struct btrfs_key location;
808 int match = 0;
809
810 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
811 index, name, name_len, 0);
812 if (di && !IS_ERR(di)) {
813 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
814 if (location.objectid != objectid)
815 goto out;
816 } else
817 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200818 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400819
820 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
821 if (di && !IS_ERR(di)) {
822 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
823 if (location.objectid != objectid)
824 goto out;
825 } else
826 goto out;
827 match = 1;
828out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200829 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400830 return match;
831}
832
833/*
834 * helper function to check a log tree for a named back reference in
835 * an inode. This is used to decide if a back reference that is
836 * found in the subvolume conflicts with what we find in the log.
837 *
838 * inode backreferences may have multiple refs in a single item,
839 * during replay we process one reference at a time, and we don't
840 * want to delete valid links to a file from the subvolume if that
841 * link is also in the log.
842 */
843static noinline int backref_in_log(struct btrfs_root *log,
844 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700845 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400846 char *name, int namelen)
847{
848 struct btrfs_path *path;
849 struct btrfs_inode_ref *ref;
850 unsigned long ptr;
851 unsigned long ptr_end;
852 unsigned long name_ptr;
853 int found_name_len;
854 int item_size;
855 int ret;
856 int match = 0;
857
858 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000859 if (!path)
860 return -ENOMEM;
861
Chris Masone02119d2008-09-05 16:13:11 -0400862 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
863 if (ret != 0)
864 goto out;
865
Chris Masone02119d2008-09-05 16:13:11 -0400866 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700867
868 if (key->type == BTRFS_INODE_EXTREF_KEY) {
869 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
870 name, namelen, NULL))
871 match = 1;
872
873 goto out;
874 }
875
876 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400877 ptr_end = ptr + item_size;
878 while (ptr < ptr_end) {
879 ref = (struct btrfs_inode_ref *)ptr;
880 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
881 if (found_name_len == namelen) {
882 name_ptr = (unsigned long)(ref + 1);
883 ret = memcmp_extent_buffer(path->nodes[0], name,
884 name_ptr, namelen);
885 if (ret == 0) {
886 match = 1;
887 goto out;
888 }
889 }
890 ptr = (unsigned long)(ref + 1) + found_name_len;
891 }
892out:
893 btrfs_free_path(path);
894 return match;
895}
896
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700897static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
898 struct btrfs_root *root,
899 struct btrfs_path *path,
900 struct btrfs_root *log_root,
901 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700902 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700903 u64 inode_objectid, u64 parent_objectid,
904 u64 ref_index, char *name, int namelen,
905 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700906{
907 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700908 char *victim_name;
909 int victim_name_len;
910 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700911 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700912 struct btrfs_key search_key;
913 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700914
Mark Fashehf1863732012-08-08 11:32:27 -0700915again:
916 /* Search old style refs */
917 search_key.objectid = inode_objectid;
918 search_key.type = BTRFS_INODE_REF_KEY;
919 search_key.offset = parent_objectid;
920 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700921 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700922 struct btrfs_inode_ref *victim_ref;
923 unsigned long ptr;
924 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700925
926 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700927
928 /* are we trying to overwrite a back ref for the root directory
929 * if so, just jump out, we're done
930 */
Mark Fashehf1863732012-08-08 11:32:27 -0700931 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700932 return 1;
933
934 /* check all the names in this back reference to see
935 * if they are in the log. if so, we allow them to stay
936 * otherwise they must be unlinked as a conflict
937 */
938 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
939 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
940 while (ptr < ptr_end) {
941 victim_ref = (struct btrfs_inode_ref *)ptr;
942 victim_name_len = btrfs_inode_ref_name_len(leaf,
943 victim_ref);
944 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400945 if (!victim_name)
946 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700947
948 read_extent_buffer(leaf, victim_name,
949 (unsigned long)(victim_ref + 1),
950 victim_name_len);
951
Mark Fashehf1863732012-08-08 11:32:27 -0700952 if (!backref_in_log(log_root, &search_key,
953 parent_objectid,
954 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700955 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700956 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700957 btrfs_release_path(path);
958
959 ret = btrfs_unlink_inode(trans, root, dir,
960 inode, victim_name,
961 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700962 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400963 if (ret)
964 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100965 ret = btrfs_run_delayed_items(trans, root);
966 if (ret)
967 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700968 *search_done = 1;
969 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700970 }
971 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700972
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700973 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
974 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700975
976 /*
977 * NOTE: we have searched root tree and checked the
978 * coresponding ref, it does not need to check again.
979 */
980 *search_done = 1;
981 }
982 btrfs_release_path(path);
983
Mark Fashehf1863732012-08-08 11:32:27 -0700984 /* Same search but for extended refs */
985 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
986 inode_objectid, parent_objectid, 0,
987 0);
988 if (!IS_ERR_OR_NULL(extref)) {
989 u32 item_size;
990 u32 cur_offset = 0;
991 unsigned long base;
992 struct inode *victim_parent;
993
994 leaf = path->nodes[0];
995
996 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
997 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
998
999 while (cur_offset < item_size) {
1000 extref = (struct btrfs_inode_extref *)base + cur_offset;
1001
1002 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1003
1004 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1005 goto next;
1006
1007 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001008 if (!victim_name)
1009 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001010 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1011 victim_name_len);
1012
1013 search_key.objectid = inode_objectid;
1014 search_key.type = BTRFS_INODE_EXTREF_KEY;
1015 search_key.offset = btrfs_extref_hash(parent_objectid,
1016 victim_name,
1017 victim_name_len);
1018 ret = 0;
1019 if (!backref_in_log(log_root, &search_key,
1020 parent_objectid, victim_name,
1021 victim_name_len)) {
1022 ret = -ENOENT;
1023 victim_parent = read_one_inode(root,
1024 parent_objectid);
1025 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001026 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001027 btrfs_release_path(path);
1028
1029 ret = btrfs_unlink_inode(trans, root,
1030 victim_parent,
1031 inode,
1032 victim_name,
1033 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001034 if (!ret)
1035 ret = btrfs_run_delayed_items(
1036 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001037 }
Mark Fashehf1863732012-08-08 11:32:27 -07001038 iput(victim_parent);
1039 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001040 if (ret)
1041 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001042 *search_done = 1;
1043 goto again;
1044 }
1045 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001046 if (ret)
1047 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001048next:
1049 cur_offset += victim_name_len + sizeof(*extref);
1050 }
1051 *search_done = 1;
1052 }
1053 btrfs_release_path(path);
1054
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055 /* look for a conflicting sequence number */
1056 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001057 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 if (di && !IS_ERR(di)) {
1059 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001060 if (ret)
1061 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001062 }
1063 btrfs_release_path(path);
1064
1065 /* look for a conflicing name */
1066 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1067 name, namelen, 0);
1068 if (di && !IS_ERR(di)) {
1069 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001070 if (ret)
1071 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001072 }
1073 btrfs_release_path(path);
1074
1075 return 0;
1076}
Chris Masone02119d2008-09-05 16:13:11 -04001077
Mark Fashehf1863732012-08-08 11:32:27 -07001078static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1079 u32 *namelen, char **name, u64 *index,
1080 u64 *parent_objectid)
1081{
1082 struct btrfs_inode_extref *extref;
1083
1084 extref = (struct btrfs_inode_extref *)ref_ptr;
1085
1086 *namelen = btrfs_inode_extref_name_len(eb, extref);
1087 *name = kmalloc(*namelen, GFP_NOFS);
1088 if (*name == NULL)
1089 return -ENOMEM;
1090
1091 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1092 *namelen);
1093
1094 *index = btrfs_inode_extref_index(eb, extref);
1095 if (parent_objectid)
1096 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1097
1098 return 0;
1099}
1100
1101static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1102 u32 *namelen, char **name, u64 *index)
1103{
1104 struct btrfs_inode_ref *ref;
1105
1106 ref = (struct btrfs_inode_ref *)ref_ptr;
1107
1108 *namelen = btrfs_inode_ref_name_len(eb, ref);
1109 *name = kmalloc(*namelen, GFP_NOFS);
1110 if (*name == NULL)
1111 return -ENOMEM;
1112
1113 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1114
1115 *index = btrfs_inode_ref_index(eb, ref);
1116
1117 return 0;
1118}
1119
Chris Masone02119d2008-09-05 16:13:11 -04001120/*
1121 * replay one inode back reference item found in the log tree.
1122 * eb, slot and key refer to the buffer and key found in the log tree.
1123 * root is the destination we are replaying into, and path is for temp
1124 * use by this function. (it should be released on return).
1125 */
1126static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1127 struct btrfs_root *root,
1128 struct btrfs_root *log,
1129 struct btrfs_path *path,
1130 struct extent_buffer *eb, int slot,
1131 struct btrfs_key *key)
1132{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001133 struct inode *dir = NULL;
1134 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001135 unsigned long ref_ptr;
1136 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001137 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001138 int namelen;
1139 int ret;
liuboc622ae62011-03-26 08:01:12 -04001140 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001141 int log_ref_ver = 0;
1142 u64 parent_objectid;
1143 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001144 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001145 int ref_struct_size;
1146
1147 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1148 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1149
1150 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1151 struct btrfs_inode_extref *r;
1152
1153 ref_struct_size = sizeof(struct btrfs_inode_extref);
1154 log_ref_ver = 1;
1155 r = (struct btrfs_inode_extref *)ref_ptr;
1156 parent_objectid = btrfs_inode_extref_parent(eb, r);
1157 } else {
1158 ref_struct_size = sizeof(struct btrfs_inode_ref);
1159 parent_objectid = key->offset;
1160 }
1161 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001162
Chris Masone02119d2008-09-05 16:13:11 -04001163 /*
1164 * it is possible that we didn't log all the parent directories
1165 * for a given inode. If we don't find the dir, just don't
1166 * copy the back ref in. The link count fixup code will take
1167 * care of the rest
1168 */
Mark Fashehf1863732012-08-08 11:32:27 -07001169 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001170 if (!dir) {
1171 ret = -ENOENT;
1172 goto out;
1173 }
Chris Masone02119d2008-09-05 16:13:11 -04001174
Mark Fashehf1863732012-08-08 11:32:27 -07001175 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001176 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001177 ret = -EIO;
1178 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001179 }
Chris Masone02119d2008-09-05 16:13:11 -04001180
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001181 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001182 if (log_ref_ver) {
1183 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1184 &ref_index, &parent_objectid);
1185 /*
1186 * parent object can change from one array
1187 * item to another.
1188 */
1189 if (!dir)
1190 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001191 if (!dir) {
1192 ret = -ENOENT;
1193 goto out;
1194 }
Mark Fashehf1863732012-08-08 11:32:27 -07001195 } else {
1196 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1197 &ref_index);
1198 }
1199 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001200 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001201
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001202 /* if we already have a perfect match, we're done */
1203 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001204 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001205 /*
1206 * look for a conflicting back reference in the
1207 * metadata. if we find one we have to unlink that name
1208 * of the file before we add our new link. Later on, we
1209 * overwrite any existing back reference, and we don't
1210 * want to create dangling pointers in the directory.
1211 */
Chris Masone02119d2008-09-05 16:13:11 -04001212
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001213 if (!search_done) {
1214 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001215 dir, inode, eb,
1216 inode_objectid,
1217 parent_objectid,
1218 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001219 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001220 if (ret) {
1221 if (ret == 1)
1222 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001223 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001224 }
Chris Masone02119d2008-09-05 16:13:11 -04001225 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001226
1227 /* insert our name */
1228 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001229 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001230 if (ret)
1231 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001232
1233 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001234 }
liuboc622ae62011-03-26 08:01:12 -04001235
Mark Fashehf1863732012-08-08 11:32:27 -07001236 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001237 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001238 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001239 if (log_ref_ver) {
1240 iput(dir);
1241 dir = NULL;
1242 }
Chris Masone02119d2008-09-05 16:13:11 -04001243 }
Chris Masone02119d2008-09-05 16:13:11 -04001244
1245 /* finally write the back reference in the inode */
1246 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001247out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001248 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001249 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001250 iput(dir);
1251 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001252 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001253}
1254
Yan, Zhengc71bf092009-11-12 09:34:40 +00001255static int insert_orphan_item(struct btrfs_trans_handle *trans,
1256 struct btrfs_root *root, u64 offset)
1257{
1258 int ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08001259 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1260 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001261 if (ret > 0)
1262 ret = btrfs_insert_orphan_item(trans, root, offset);
1263 return ret;
1264}
1265
Mark Fashehf1863732012-08-08 11:32:27 -07001266static int count_inode_extrefs(struct btrfs_root *root,
1267 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001268{
Mark Fashehf1863732012-08-08 11:32:27 -07001269 int ret = 0;
1270 int name_len;
1271 unsigned int nlink = 0;
1272 u32 item_size;
1273 u32 cur_offset = 0;
1274 u64 inode_objectid = btrfs_ino(inode);
1275 u64 offset = 0;
1276 unsigned long ptr;
1277 struct btrfs_inode_extref *extref;
1278 struct extent_buffer *leaf;
1279
1280 while (1) {
1281 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1282 &extref, &offset);
1283 if (ret)
1284 break;
1285
1286 leaf = path->nodes[0];
1287 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1288 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1289
1290 while (cur_offset < item_size) {
1291 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1292 name_len = btrfs_inode_extref_name_len(leaf, extref);
1293
1294 nlink++;
1295
1296 cur_offset += name_len + sizeof(*extref);
1297 }
1298
1299 offset++;
1300 btrfs_release_path(path);
1301 }
1302 btrfs_release_path(path);
1303
1304 if (ret < 0)
1305 return ret;
1306 return nlink;
1307}
1308
1309static int count_inode_refs(struct btrfs_root *root,
1310 struct inode *inode, struct btrfs_path *path)
1311{
Chris Masone02119d2008-09-05 16:13:11 -04001312 int ret;
1313 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001314 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001315 unsigned long ptr;
1316 unsigned long ptr_end;
1317 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001318 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001319
Li Zefan33345d012011-04-20 10:31:50 +08001320 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001321 key.type = BTRFS_INODE_REF_KEY;
1322 key.offset = (u64)-1;
1323
Chris Masond3977122009-01-05 21:25:51 -05001324 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1326 if (ret < 0)
1327 break;
1328 if (ret > 0) {
1329 if (path->slots[0] == 0)
1330 break;
1331 path->slots[0]--;
1332 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001333process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001334 btrfs_item_key_to_cpu(path->nodes[0], &key,
1335 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001336 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001337 key.type != BTRFS_INODE_REF_KEY)
1338 break;
1339 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1340 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1341 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001342 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001343 struct btrfs_inode_ref *ref;
1344
1345 ref = (struct btrfs_inode_ref *)ptr;
1346 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1347 ref);
1348 ptr = (unsigned long)(ref + 1) + name_len;
1349 nlink++;
1350 }
1351
1352 if (key.offset == 0)
1353 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001354 if (path->slots[0] > 0) {
1355 path->slots[0]--;
1356 goto process_slot;
1357 }
Chris Masone02119d2008-09-05 16:13:11 -04001358 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001359 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001360 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001361 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001362
1363 return nlink;
1364}
1365
1366/*
1367 * There are a few corners where the link count of the file can't
1368 * be properly maintained during replay. So, instead of adding
1369 * lots of complexity to the log code, we just scan the backrefs
1370 * for any file that has been through replay.
1371 *
1372 * The scan will update the link count on the inode to reflect the
1373 * number of back refs found. If it goes down to zero, the iput
1374 * will free the inode.
1375 */
1376static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1377 struct btrfs_root *root,
1378 struct inode *inode)
1379{
1380 struct btrfs_path *path;
1381 int ret;
1382 u64 nlink = 0;
1383 u64 ino = btrfs_ino(inode);
1384
1385 path = btrfs_alloc_path();
1386 if (!path)
1387 return -ENOMEM;
1388
1389 ret = count_inode_refs(root, inode, path);
1390 if (ret < 0)
1391 goto out;
1392
1393 nlink = ret;
1394
1395 ret = count_inode_extrefs(root, inode, path);
1396 if (ret == -ENOENT)
1397 ret = 0;
1398
1399 if (ret < 0)
1400 goto out;
1401
1402 nlink += ret;
1403
1404 ret = 0;
1405
Chris Masone02119d2008-09-05 16:13:11 -04001406 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001407 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001408 btrfs_update_inode(trans, root, inode);
1409 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001410 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001411
Yan, Zhengc71bf092009-11-12 09:34:40 +00001412 if (inode->i_nlink == 0) {
1413 if (S_ISDIR(inode->i_mode)) {
1414 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001415 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001416 if (ret)
1417 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001418 }
Li Zefan33345d012011-04-20 10:31:50 +08001419 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001420 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001421
Mark Fashehf1863732012-08-08 11:32:27 -07001422out:
1423 btrfs_free_path(path);
1424 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001425}
1426
1427static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root,
1429 struct btrfs_path *path)
1430{
1431 int ret;
1432 struct btrfs_key key;
1433 struct inode *inode;
1434
1435 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1436 key.type = BTRFS_ORPHAN_ITEM_KEY;
1437 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001438 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001439 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1440 if (ret < 0)
1441 break;
1442
1443 if (ret == 1) {
1444 if (path->slots[0] == 0)
1445 break;
1446 path->slots[0]--;
1447 }
1448
1449 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1450 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1451 key.type != BTRFS_ORPHAN_ITEM_KEY)
1452 break;
1453
1454 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001455 if (ret)
1456 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001457
David Sterbab3b4aa72011-04-21 01:20:15 +02001458 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001459 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001460 if (!inode)
1461 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001462
1463 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001464 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001465 if (ret)
1466 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001467
Chris Mason12fcfd22009-03-24 10:24:20 -04001468 /*
1469 * fixup on a directory may create new entries,
1470 * make sure we always look for the highset possible
1471 * offset
1472 */
1473 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001474 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001475 ret = 0;
1476out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001477 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001478 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001479}
1480
1481
1482/*
1483 * record a given inode in the fixup dir so we can check its link
1484 * count when replay is done. The link count is incremented here
1485 * so the inode won't go away until we check it
1486 */
1487static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1488 struct btrfs_root *root,
1489 struct btrfs_path *path,
1490 u64 objectid)
1491{
1492 struct btrfs_key key;
1493 int ret = 0;
1494 struct inode *inode;
1495
1496 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001497 if (!inode)
1498 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001499
1500 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1501 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1502 key.offset = objectid;
1503
1504 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1505
David Sterbab3b4aa72011-04-21 01:20:15 +02001506 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001507 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001508 if (!inode->i_nlink)
1509 set_nlink(inode, 1);
1510 else
Zach Brown8b558c52013-10-16 12:10:34 -07001511 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001512 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001513 } else if (ret == -EEXIST) {
1514 ret = 0;
1515 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001516 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001517 }
1518 iput(inode);
1519
1520 return ret;
1521}
1522
1523/*
1524 * when replaying the log for a directory, we only insert names
1525 * for inodes that actually exist. This means an fsync on a directory
1526 * does not implicitly fsync all the new files in it
1527 */
1528static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1529 struct btrfs_root *root,
1530 struct btrfs_path *path,
1531 u64 dirid, u64 index,
1532 char *name, int name_len, u8 type,
1533 struct btrfs_key *location)
1534{
1535 struct inode *inode;
1536 struct inode *dir;
1537 int ret;
1538
1539 inode = read_one_inode(root, location->objectid);
1540 if (!inode)
1541 return -ENOENT;
1542
1543 dir = read_one_inode(root, dirid);
1544 if (!dir) {
1545 iput(inode);
1546 return -EIO;
1547 }
Josef Bacikd5554382013-09-11 14:17:00 -04001548
Chris Masone02119d2008-09-05 16:13:11 -04001549 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1550
1551 /* FIXME, put inode into FIXUP list */
1552
1553 iput(inode);
1554 iput(dir);
1555 return ret;
1556}
1557
1558/*
1559 * take a single entry in a log directory item and replay it into
1560 * the subvolume.
1561 *
1562 * if a conflicting item exists in the subdirectory already,
1563 * the inode it points to is unlinked and put into the link count
1564 * fix up tree.
1565 *
1566 * If a name from the log points to a file or directory that does
1567 * not exist in the FS, it is skipped. fsyncs on directories
1568 * do not force down inodes inside that directory, just changes to the
1569 * names or unlinks in a directory.
1570 */
1571static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1572 struct btrfs_root *root,
1573 struct btrfs_path *path,
1574 struct extent_buffer *eb,
1575 struct btrfs_dir_item *di,
1576 struct btrfs_key *key)
1577{
1578 char *name;
1579 int name_len;
1580 struct btrfs_dir_item *dst_di;
1581 struct btrfs_key found_key;
1582 struct btrfs_key log_key;
1583 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001584 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001585 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001586 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001587 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001588
1589 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001590 if (!dir)
1591 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001592
1593 name_len = btrfs_dir_name_len(eb, di);
1594 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001595 if (!name) {
1596 ret = -ENOMEM;
1597 goto out;
1598 }
liubo2a29edc2011-01-26 06:22:08 +00001599
Chris Masone02119d2008-09-05 16:13:11 -04001600 log_type = btrfs_dir_type(eb, di);
1601 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1602 name_len);
1603
1604 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001605 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1606 if (exists == 0)
1607 exists = 1;
1608 else
1609 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001610 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001611
Chris Masone02119d2008-09-05 16:13:11 -04001612 if (key->type == BTRFS_DIR_ITEM_KEY) {
1613 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1614 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001615 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001616 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1617 key->objectid,
1618 key->offset, name,
1619 name_len, 1);
1620 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001621 /* Corruption */
1622 ret = -EINVAL;
1623 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001624 }
David Sterbac7040052011-04-19 18:00:01 +02001625 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001626 /* we need a sequence number to insert, so we only
1627 * do inserts for the BTRFS_DIR_INDEX_KEY types
1628 */
1629 if (key->type != BTRFS_DIR_INDEX_KEY)
1630 goto out;
1631 goto insert;
1632 }
1633
1634 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1635 /* the existing item matches the logged item */
1636 if (found_key.objectid == log_key.objectid &&
1637 found_key.type == log_key.type &&
1638 found_key.offset == log_key.offset &&
1639 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1640 goto out;
1641 }
1642
1643 /*
1644 * don't drop the conflicting directory entry if the inode
1645 * for the new entry doesn't exist
1646 */
Chris Mason4bef0842008-09-08 11:18:08 -04001647 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001648 goto out;
1649
Chris Masone02119d2008-09-05 16:13:11 -04001650 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001651 if (ret)
1652 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001653
1654 if (key->type == BTRFS_DIR_INDEX_KEY)
1655 goto insert;
1656out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001657 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001658 if (!ret && update_size) {
1659 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1660 ret = btrfs_update_inode(trans, root, dir);
1661 }
Chris Masone02119d2008-09-05 16:13:11 -04001662 kfree(name);
1663 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001664 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001665
1666insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001667 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001668 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1669 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001670 if (ret && ret != -ENOENT)
1671 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001672 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001673 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001674 goto out;
1675}
1676
1677/*
1678 * find all the names in a directory item and reconcile them into
1679 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1680 * one name in a directory item, but the same code gets used for
1681 * both directory index types
1682 */
1683static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
1685 struct btrfs_path *path,
1686 struct extent_buffer *eb, int slot,
1687 struct btrfs_key *key)
1688{
1689 int ret;
1690 u32 item_size = btrfs_item_size_nr(eb, slot);
1691 struct btrfs_dir_item *di;
1692 int name_len;
1693 unsigned long ptr;
1694 unsigned long ptr_end;
1695
1696 ptr = btrfs_item_ptr_offset(eb, slot);
1697 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001698 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001699 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001700 if (verify_dir_item(root, eb, di))
1701 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001702 name_len = btrfs_dir_name_len(eb, di);
1703 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001704 if (ret)
1705 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001706 ptr = (unsigned long)(di + 1);
1707 ptr += name_len;
1708 }
1709 return 0;
1710}
1711
1712/*
1713 * directory replay has two parts. There are the standard directory
1714 * items in the log copied from the subvolume, and range items
1715 * created in the log while the subvolume was logged.
1716 *
1717 * The range items tell us which parts of the key space the log
1718 * is authoritative for. During replay, if a key in the subvolume
1719 * directory is in a logged range item, but not actually in the log
1720 * that means it was deleted from the directory before the fsync
1721 * and should be removed.
1722 */
1723static noinline int find_dir_range(struct btrfs_root *root,
1724 struct btrfs_path *path,
1725 u64 dirid, int key_type,
1726 u64 *start_ret, u64 *end_ret)
1727{
1728 struct btrfs_key key;
1729 u64 found_end;
1730 struct btrfs_dir_log_item *item;
1731 int ret;
1732 int nritems;
1733
1734 if (*start_ret == (u64)-1)
1735 return 1;
1736
1737 key.objectid = dirid;
1738 key.type = key_type;
1739 key.offset = *start_ret;
1740
1741 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1742 if (ret < 0)
1743 goto out;
1744 if (ret > 0) {
1745 if (path->slots[0] == 0)
1746 goto out;
1747 path->slots[0]--;
1748 }
1749 if (ret != 0)
1750 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1751
1752 if (key.type != key_type || key.objectid != dirid) {
1753 ret = 1;
1754 goto next;
1755 }
1756 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1757 struct btrfs_dir_log_item);
1758 found_end = btrfs_dir_log_end(path->nodes[0], item);
1759
1760 if (*start_ret >= key.offset && *start_ret <= found_end) {
1761 ret = 0;
1762 *start_ret = key.offset;
1763 *end_ret = found_end;
1764 goto out;
1765 }
1766 ret = 1;
1767next:
1768 /* check the next slot in the tree to see if it is a valid item */
1769 nritems = btrfs_header_nritems(path->nodes[0]);
1770 if (path->slots[0] >= nritems) {
1771 ret = btrfs_next_leaf(root, path);
1772 if (ret)
1773 goto out;
1774 } else {
1775 path->slots[0]++;
1776 }
1777
1778 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1779
1780 if (key.type != key_type || key.objectid != dirid) {
1781 ret = 1;
1782 goto out;
1783 }
1784 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1785 struct btrfs_dir_log_item);
1786 found_end = btrfs_dir_log_end(path->nodes[0], item);
1787 *start_ret = key.offset;
1788 *end_ret = found_end;
1789 ret = 0;
1790out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001791 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001792 return ret;
1793}
1794
1795/*
1796 * this looks for a given directory item in the log. If the directory
1797 * item is not in the log, the item is removed and the inode it points
1798 * to is unlinked
1799 */
1800static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1801 struct btrfs_root *root,
1802 struct btrfs_root *log,
1803 struct btrfs_path *path,
1804 struct btrfs_path *log_path,
1805 struct inode *dir,
1806 struct btrfs_key *dir_key)
1807{
1808 int ret;
1809 struct extent_buffer *eb;
1810 int slot;
1811 u32 item_size;
1812 struct btrfs_dir_item *di;
1813 struct btrfs_dir_item *log_di;
1814 int name_len;
1815 unsigned long ptr;
1816 unsigned long ptr_end;
1817 char *name;
1818 struct inode *inode;
1819 struct btrfs_key location;
1820
1821again:
1822 eb = path->nodes[0];
1823 slot = path->slots[0];
1824 item_size = btrfs_item_size_nr(eb, slot);
1825 ptr = btrfs_item_ptr_offset(eb, slot);
1826 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001827 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001828 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001829 if (verify_dir_item(root, eb, di)) {
1830 ret = -EIO;
1831 goto out;
1832 }
1833
Chris Masone02119d2008-09-05 16:13:11 -04001834 name_len = btrfs_dir_name_len(eb, di);
1835 name = kmalloc(name_len, GFP_NOFS);
1836 if (!name) {
1837 ret = -ENOMEM;
1838 goto out;
1839 }
1840 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1841 name_len);
1842 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001843 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001844 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1845 dir_key->objectid,
1846 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001847 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001848 log_di = btrfs_lookup_dir_index_item(trans, log,
1849 log_path,
1850 dir_key->objectid,
1851 dir_key->offset,
1852 name, name_len, 0);
1853 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001854 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001855 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001856 btrfs_release_path(path);
1857 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001858 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001859 if (!inode) {
1860 kfree(name);
1861 return -EIO;
1862 }
Chris Masone02119d2008-09-05 16:13:11 -04001863
1864 ret = link_to_fixup_dir(trans, root,
1865 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001866 if (ret) {
1867 kfree(name);
1868 iput(inode);
1869 goto out;
1870 }
1871
Zach Brown8b558c52013-10-16 12:10:34 -07001872 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001873 ret = btrfs_unlink_inode(trans, root, dir, inode,
1874 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001875 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001876 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001877 kfree(name);
1878 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001879 if (ret)
1880 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001881
1882 /* there might still be more names under this key
1883 * check and repeat if required
1884 */
1885 ret = btrfs_search_slot(NULL, root, dir_key, path,
1886 0, 0);
1887 if (ret == 0)
1888 goto again;
1889 ret = 0;
1890 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001891 } else if (IS_ERR(log_di)) {
1892 kfree(name);
1893 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001894 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001895 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001896 kfree(name);
1897
1898 ptr = (unsigned long)(di + 1);
1899 ptr += name_len;
1900 }
1901 ret = 0;
1902out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001903 btrfs_release_path(path);
1904 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001905 return ret;
1906}
1907
1908/*
1909 * deletion replay happens before we copy any new directory items
1910 * out of the log or out of backreferences from inodes. It
1911 * scans the log to find ranges of keys that log is authoritative for,
1912 * and then scans the directory to find items in those ranges that are
1913 * not present in the log.
1914 *
1915 * Anything we don't find in the log is unlinked and removed from the
1916 * directory.
1917 */
1918static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1919 struct btrfs_root *root,
1920 struct btrfs_root *log,
1921 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001922 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001923{
1924 u64 range_start;
1925 u64 range_end;
1926 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1927 int ret = 0;
1928 struct btrfs_key dir_key;
1929 struct btrfs_key found_key;
1930 struct btrfs_path *log_path;
1931 struct inode *dir;
1932
1933 dir_key.objectid = dirid;
1934 dir_key.type = BTRFS_DIR_ITEM_KEY;
1935 log_path = btrfs_alloc_path();
1936 if (!log_path)
1937 return -ENOMEM;
1938
1939 dir = read_one_inode(root, dirid);
1940 /* it isn't an error if the inode isn't there, that can happen
1941 * because we replay the deletes before we copy in the inode item
1942 * from the log
1943 */
1944 if (!dir) {
1945 btrfs_free_path(log_path);
1946 return 0;
1947 }
1948again:
1949 range_start = 0;
1950 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001951 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001952 if (del_all)
1953 range_end = (u64)-1;
1954 else {
1955 ret = find_dir_range(log, path, dirid, key_type,
1956 &range_start, &range_end);
1957 if (ret != 0)
1958 break;
1959 }
Chris Masone02119d2008-09-05 16:13:11 -04001960
1961 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001962 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001963 int nritems;
1964 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1965 0, 0);
1966 if (ret < 0)
1967 goto out;
1968
1969 nritems = btrfs_header_nritems(path->nodes[0]);
1970 if (path->slots[0] >= nritems) {
1971 ret = btrfs_next_leaf(root, path);
1972 if (ret)
1973 break;
1974 }
1975 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1976 path->slots[0]);
1977 if (found_key.objectid != dirid ||
1978 found_key.type != dir_key.type)
1979 goto next_type;
1980
1981 if (found_key.offset > range_end)
1982 break;
1983
1984 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001985 log_path, dir,
1986 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001987 if (ret)
1988 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001989 if (found_key.offset == (u64)-1)
1990 break;
1991 dir_key.offset = found_key.offset + 1;
1992 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001993 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001994 if (range_end == (u64)-1)
1995 break;
1996 range_start = range_end + 1;
1997 }
1998
1999next_type:
2000 ret = 0;
2001 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2002 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2003 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002004 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002005 goto again;
2006 }
2007out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002008 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002009 btrfs_free_path(log_path);
2010 iput(dir);
2011 return ret;
2012}
2013
2014/*
2015 * the process_func used to replay items from the log tree. This
2016 * gets called in two different stages. The first stage just looks
2017 * for inodes and makes sure they are all copied into the subvolume.
2018 *
2019 * The second stage copies all the other item types from the log into
2020 * the subvolume. The two stage approach is slower, but gets rid of
2021 * lots of complexity around inodes referencing other inodes that exist
2022 * only in the log (references come from either directory items or inode
2023 * back refs).
2024 */
2025static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2026 struct walk_control *wc, u64 gen)
2027{
2028 int nritems;
2029 struct btrfs_path *path;
2030 struct btrfs_root *root = wc->replay_dest;
2031 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002032 int level;
2033 int i;
2034 int ret;
2035
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002036 ret = btrfs_read_buffer(eb, gen);
2037 if (ret)
2038 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002039
2040 level = btrfs_header_level(eb);
2041
2042 if (level != 0)
2043 return 0;
2044
2045 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002046 if (!path)
2047 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002048
2049 nritems = btrfs_header_nritems(eb);
2050 for (i = 0; i < nritems; i++) {
2051 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002052
2053 /* inode keys are done during the first stage */
2054 if (key.type == BTRFS_INODE_ITEM_KEY &&
2055 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002056 struct btrfs_inode_item *inode_item;
2057 u32 mode;
2058
2059 inode_item = btrfs_item_ptr(eb, i,
2060 struct btrfs_inode_item);
2061 mode = btrfs_inode_mode(eb, inode_item);
2062 if (S_ISDIR(mode)) {
2063 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002064 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002065 if (ret)
2066 break;
Chris Masone02119d2008-09-05 16:13:11 -04002067 }
2068 ret = overwrite_item(wc->trans, root, path,
2069 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002070 if (ret)
2071 break;
Chris Masone02119d2008-09-05 16:13:11 -04002072
Yan, Zhengc71bf092009-11-12 09:34:40 +00002073 /* for regular files, make sure corresponding
2074 * orhpan item exist. extents past the new EOF
2075 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002076 */
2077 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002078 ret = insert_orphan_item(wc->trans, root,
2079 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002080 if (ret)
2081 break;
Chris Masone02119d2008-09-05 16:13:11 -04002082 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002083
Chris Masone02119d2008-09-05 16:13:11 -04002084 ret = link_to_fixup_dir(wc->trans, root,
2085 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002086 if (ret)
2087 break;
Chris Masone02119d2008-09-05 16:13:11 -04002088 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002089
2090 if (key.type == BTRFS_DIR_INDEX_KEY &&
2091 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2092 ret = replay_one_dir_item(wc->trans, root, path,
2093 eb, i, &key);
2094 if (ret)
2095 break;
2096 }
2097
Chris Masone02119d2008-09-05 16:13:11 -04002098 if (wc->stage < LOG_WALK_REPLAY_ALL)
2099 continue;
2100
2101 /* these keys are simply copied */
2102 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2103 ret = overwrite_item(wc->trans, root, path,
2104 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002105 if (ret)
2106 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002107 } else if (key.type == BTRFS_INODE_REF_KEY ||
2108 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002109 ret = add_inode_ref(wc->trans, root, log, path,
2110 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002111 if (ret && ret != -ENOENT)
2112 break;
2113 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002114 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2115 ret = replay_one_extent(wc->trans, root, path,
2116 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002117 if (ret)
2118 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002119 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002120 ret = replay_one_dir_item(wc->trans, root, path,
2121 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002122 if (ret)
2123 break;
Chris Masone02119d2008-09-05 16:13:11 -04002124 }
2125 }
2126 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002127 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002128}
2129
Chris Masond3977122009-01-05 21:25:51 -05002130static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002131 struct btrfs_root *root,
2132 struct btrfs_path *path, int *level,
2133 struct walk_control *wc)
2134{
2135 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002136 u64 bytenr;
2137 u64 ptr_gen;
2138 struct extent_buffer *next;
2139 struct extent_buffer *cur;
2140 struct extent_buffer *parent;
2141 u32 blocksize;
2142 int ret = 0;
2143
2144 WARN_ON(*level < 0);
2145 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2146
Chris Masond3977122009-01-05 21:25:51 -05002147 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002148 WARN_ON(*level < 0);
2149 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2150 cur = path->nodes[*level];
2151
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302152 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002153
2154 if (path->slots[*level] >=
2155 btrfs_header_nritems(cur))
2156 break;
2157
2158 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2159 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2160 blocksize = btrfs_level_size(root, *level - 1);
2161
2162 parent = path->nodes[*level];
2163 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002164
2165 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002166 if (!next)
2167 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002168
Chris Masone02119d2008-09-05 16:13:11 -04002169 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002170 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002171 if (ret) {
2172 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002173 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002174 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002175
Chris Masone02119d2008-09-05 16:13:11 -04002176 path->slots[*level]++;
2177 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002178 ret = btrfs_read_buffer(next, ptr_gen);
2179 if (ret) {
2180 free_extent_buffer(next);
2181 return ret;
2182 }
Chris Masone02119d2008-09-05 16:13:11 -04002183
Josef Bacik681ae502013-10-07 15:11:00 -04002184 if (trans) {
2185 btrfs_tree_lock(next);
2186 btrfs_set_lock_blocking(next);
2187 clean_tree_block(trans, root, next);
2188 btrfs_wait_tree_block_writeback(next);
2189 btrfs_tree_unlock(next);
2190 }
Chris Masone02119d2008-09-05 16:13:11 -04002191
Chris Masone02119d2008-09-05 16:13:11 -04002192 WARN_ON(root_owner !=
2193 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002194 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002195 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002196 if (ret) {
2197 free_extent_buffer(next);
2198 return ret;
2199 }
Chris Masone02119d2008-09-05 16:13:11 -04002200 }
2201 free_extent_buffer(next);
2202 continue;
2203 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002204 ret = btrfs_read_buffer(next, ptr_gen);
2205 if (ret) {
2206 free_extent_buffer(next);
2207 return ret;
2208 }
Chris Masone02119d2008-09-05 16:13:11 -04002209
2210 WARN_ON(*level <= 0);
2211 if (path->nodes[*level-1])
2212 free_extent_buffer(path->nodes[*level-1]);
2213 path->nodes[*level-1] = next;
2214 *level = btrfs_header_level(next);
2215 path->slots[*level] = 0;
2216 cond_resched();
2217 }
2218 WARN_ON(*level < 0);
2219 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2220
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002221 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002222
2223 cond_resched();
2224 return 0;
2225}
2226
Chris Masond3977122009-01-05 21:25:51 -05002227static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002228 struct btrfs_root *root,
2229 struct btrfs_path *path, int *level,
2230 struct walk_control *wc)
2231{
2232 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002233 int i;
2234 int slot;
2235 int ret;
2236
Chris Masond3977122009-01-05 21:25:51 -05002237 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002238 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002239 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002240 path->slots[i]++;
2241 *level = i;
2242 WARN_ON(*level == 0);
2243 return 0;
2244 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002245 struct extent_buffer *parent;
2246 if (path->nodes[*level] == root->node)
2247 parent = path->nodes[*level];
2248 else
2249 parent = path->nodes[*level + 1];
2250
2251 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002252 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002253 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002254 if (ret)
2255 return ret;
2256
Chris Masone02119d2008-09-05 16:13:11 -04002257 if (wc->free) {
2258 struct extent_buffer *next;
2259
2260 next = path->nodes[*level];
2261
Josef Bacik681ae502013-10-07 15:11:00 -04002262 if (trans) {
2263 btrfs_tree_lock(next);
2264 btrfs_set_lock_blocking(next);
2265 clean_tree_block(trans, root, next);
2266 btrfs_wait_tree_block_writeback(next);
2267 btrfs_tree_unlock(next);
2268 }
Chris Masone02119d2008-09-05 16:13:11 -04002269
Chris Masone02119d2008-09-05 16:13:11 -04002270 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002271 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002272 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002273 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002274 if (ret)
2275 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002276 }
2277 free_extent_buffer(path->nodes[*level]);
2278 path->nodes[*level] = NULL;
2279 *level = i + 1;
2280 }
2281 }
2282 return 1;
2283}
2284
2285/*
2286 * drop the reference count on the tree rooted at 'snap'. This traverses
2287 * the tree freeing any blocks that have a ref count of zero after being
2288 * decremented.
2289 */
2290static int walk_log_tree(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *log, struct walk_control *wc)
2292{
2293 int ret = 0;
2294 int wret;
2295 int level;
2296 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002297 int orig_level;
2298
2299 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002300 if (!path)
2301 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002302
2303 level = btrfs_header_level(log->node);
2304 orig_level = level;
2305 path->nodes[level] = log->node;
2306 extent_buffer_get(log->node);
2307 path->slots[level] = 0;
2308
Chris Masond3977122009-01-05 21:25:51 -05002309 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002310 wret = walk_down_log_tree(trans, log, path, &level, wc);
2311 if (wret > 0)
2312 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002313 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002314 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 goto out;
2316 }
Chris Masone02119d2008-09-05 16:13:11 -04002317
2318 wret = walk_up_log_tree(trans, log, path, &level, wc);
2319 if (wret > 0)
2320 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002321 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002322 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002323 goto out;
2324 }
Chris Masone02119d2008-09-05 16:13:11 -04002325 }
2326
2327 /* was the root node processed? if not, catch it here */
2328 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002329 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002330 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002331 if (ret)
2332 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002333 if (wc->free) {
2334 struct extent_buffer *next;
2335
2336 next = path->nodes[orig_level];
2337
Josef Bacik681ae502013-10-07 15:11:00 -04002338 if (trans) {
2339 btrfs_tree_lock(next);
2340 btrfs_set_lock_blocking(next);
2341 clean_tree_block(trans, log, next);
2342 btrfs_wait_tree_block_writeback(next);
2343 btrfs_tree_unlock(next);
2344 }
Chris Masone02119d2008-09-05 16:13:11 -04002345
Chris Masone02119d2008-09-05 16:13:11 -04002346 WARN_ON(log->root_key.objectid !=
2347 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002348 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002349 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002350 if (ret)
2351 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002352 }
2353 }
2354
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002355out:
Chris Masone02119d2008-09-05 16:13:11 -04002356 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002357 return ret;
2358}
2359
Yan Zheng7237f182009-01-21 12:54:03 -05002360/*
2361 * helper function to update the item for a given subvolumes log root
2362 * in the tree of log roots
2363 */
2364static int update_log_root(struct btrfs_trans_handle *trans,
2365 struct btrfs_root *log)
2366{
2367 int ret;
2368
2369 if (log->log_transid == 1) {
2370 /* insert root item on the first sync */
2371 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2372 &log->root_key, &log->root_item);
2373 } else {
2374 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2375 &log->root_key, &log->root_item);
2376 }
2377 return ret;
2378}
2379
Miao Xie8b050d32014-02-20 18:08:58 +08002380static void wait_log_commit(struct btrfs_trans_handle *trans,
2381 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002382{
2383 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002384 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002385
Yan Zheng7237f182009-01-21 12:54:03 -05002386 /*
2387 * we only allow two pending log transactions at a time,
2388 * so we know that if ours is more than 2 older than the
2389 * current transaction, we're done
2390 */
Chris Masone02119d2008-09-05 16:13:11 -04002391 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002392 prepare_to_wait(&root->log_commit_wait[index],
2393 &wait, TASK_UNINTERRUPTIBLE);
2394 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002395
Miao Xied1433de2014-02-20 18:08:59 +08002396 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002397 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002398 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002399
Yan Zheng7237f182009-01-21 12:54:03 -05002400 finish_wait(&root->log_commit_wait[index], &wait);
2401 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002402 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002403 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002404}
2405
Jeff Mahoney143bede2012-03-01 14:56:26 +01002406static void wait_for_writer(struct btrfs_trans_handle *trans,
2407 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002408{
2409 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002410
2411 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002412 prepare_to_wait(&root->log_writer_wait,
2413 &wait, TASK_UNINTERRUPTIBLE);
2414 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002415 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002416 schedule();
2417 mutex_lock(&root->log_mutex);
2418 finish_wait(&root->log_writer_wait, &wait);
2419 }
Chris Masone02119d2008-09-05 16:13:11 -04002420}
2421
Miao Xie8b050d32014-02-20 18:08:58 +08002422static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2423 struct btrfs_log_ctx *ctx)
2424{
2425 if (!ctx)
2426 return;
2427
2428 mutex_lock(&root->log_mutex);
2429 list_del_init(&ctx->list);
2430 mutex_unlock(&root->log_mutex);
2431}
2432
2433/*
2434 * Invoked in log mutex context, or be sure there is no other task which
2435 * can access the list.
2436 */
2437static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2438 int index, int error)
2439{
2440 struct btrfs_log_ctx *ctx;
2441
2442 if (!error) {
2443 INIT_LIST_HEAD(&root->log_ctxs[index]);
2444 return;
2445 }
2446
2447 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2448 ctx->log_ret = error;
2449
2450 INIT_LIST_HEAD(&root->log_ctxs[index]);
2451}
2452
Chris Masone02119d2008-09-05 16:13:11 -04002453/*
2454 * btrfs_sync_log does sends a given tree log down to the disk and
2455 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002456 * you know that any inodes previously logged are safely on disk only
2457 * if it returns 0.
2458 *
2459 * Any other return value means you need to call btrfs_commit_transaction.
2460 * Some of the edge cases for fsyncing directories that have had unlinks
2461 * or renames done in the past mean that sometimes the only safe
2462 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2463 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002464 */
2465int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002466 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002467{
Yan Zheng7237f182009-01-21 12:54:03 -05002468 int index1;
2469 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002470 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002471 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002472 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002473 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002474 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002475 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002476 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002477
Yan Zheng7237f182009-01-21 12:54:03 -05002478 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002479 log_transid = ctx->log_transid;
2480 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002481 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002482 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002483 }
Miao Xied1433de2014-02-20 18:08:59 +08002484
2485 index1 = log_transid % 2;
2486 if (atomic_read(&root->log_commit[index1])) {
2487 wait_log_commit(trans, root, log_transid);
2488 mutex_unlock(&root->log_mutex);
2489 return ctx->log_ret;
2490 }
2491 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002492 atomic_set(&root->log_commit[index1], 1);
2493
2494 /* wait for previous tree log sync to complete */
2495 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Miao Xied1433de2014-02-20 18:08:59 +08002496 wait_log_commit(trans, root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002497
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002498 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002499 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002500 /* when we're on an ssd, just kick the log commit out */
Miao Xie27cdeb72014-04-02 19:51:05 +08002501 if (!btrfs_test_opt(root, SSD) &&
2502 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002503 mutex_unlock(&root->log_mutex);
2504 schedule_timeout_uninterruptible(1);
2505 mutex_lock(&root->log_mutex);
2506 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002507 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002508 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002509 break;
2510 }
Chris Masond0c803c2008-09-11 16:17:57 -04002511
Chris Mason12fcfd22009-03-24 10:24:20 -04002512 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002513 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002514 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002515 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002516 mutex_unlock(&root->log_mutex);
2517 goto out;
2518 }
2519
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002520 if (log_transid % 2 == 0)
2521 mark = EXTENT_DIRTY;
2522 else
2523 mark = EXTENT_NEW;
2524
Chris Mason690587d2009-10-13 13:29:19 -04002525 /* we start IO on all the marked extents here, but we don't actually
2526 * wait for them until later.
2527 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002528 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002529 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002530 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002531 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002532 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002533 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002534 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002535 mutex_unlock(&root->log_mutex);
2536 goto out;
2537 }
Yan Zheng7237f182009-01-21 12:54:03 -05002538
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002539 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002540
Yan Zheng7237f182009-01-21 12:54:03 -05002541 root->log_transid++;
2542 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002543 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002544 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002545 * IO has been started, blocks of the log tree have WRITTEN flag set
2546 * in their headers. new modifications of the log will be written to
2547 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002548 */
2549 mutex_unlock(&root->log_mutex);
2550
Miao Xied1433de2014-02-20 18:08:59 +08002551 btrfs_init_log_ctx(&root_log_ctx);
2552
Yan Zheng7237f182009-01-21 12:54:03 -05002553 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002554 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002555 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002556
2557 index2 = log_root_tree->log_transid % 2;
2558 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2559 root_log_ctx.log_transid = log_root_tree->log_transid;
2560
Yan Zheng7237f182009-01-21 12:54:03 -05002561 mutex_unlock(&log_root_tree->log_mutex);
2562
2563 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002564
2565 mutex_lock(&log_root_tree->log_mutex);
2566 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2567 smp_mb();
2568 if (waitqueue_active(&log_root_tree->log_writer_wait))
2569 wake_up(&log_root_tree->log_writer_wait);
2570 }
2571
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002572 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002573 if (!list_empty(&root_log_ctx.list))
2574 list_del_init(&root_log_ctx.list);
2575
Miao Xiec6adc9c2013-05-28 10:05:39 +00002576 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002577 btrfs_set_log_full_commit(root->fs_info, trans);
2578
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002579 if (ret != -ENOSPC) {
2580 btrfs_abort_transaction(trans, root, ret);
2581 mutex_unlock(&log_root_tree->log_mutex);
2582 goto out;
2583 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002584 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002585 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002586 mutex_unlock(&log_root_tree->log_mutex);
2587 ret = -EAGAIN;
2588 goto out;
2589 }
2590
Miao Xied1433de2014-02-20 18:08:59 +08002591 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2592 mutex_unlock(&log_root_tree->log_mutex);
2593 ret = root_log_ctx.log_ret;
2594 goto out;
2595 }
Miao Xie8b050d32014-02-20 18:08:58 +08002596
Miao Xied1433de2014-02-20 18:08:59 +08002597 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002598 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002599 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002600 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xie8b050d32014-02-20 18:08:58 +08002601 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002602 root_log_ctx.log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002603 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002604 mutex_unlock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002605 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002606 goto out;
2607 }
Miao Xied1433de2014-02-20 18:08:59 +08002608 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002609 atomic_set(&log_root_tree->log_commit[index2], 1);
2610
Chris Mason12fcfd22009-03-24 10:24:20 -04002611 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2612 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002613 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002614 }
Yan Zheng7237f182009-01-21 12:54:03 -05002615
Chris Mason12fcfd22009-03-24 10:24:20 -04002616 wait_for_writer(trans, log_root_tree);
2617
2618 /*
2619 * now that we've moved on to the tree of log tree roots,
2620 * check the full commit flag again
2621 */
Miao Xie995946d2014-04-02 19:51:06 +08002622 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002623 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002624 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002625 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002626 mutex_unlock(&log_root_tree->log_mutex);
2627 ret = -EAGAIN;
2628 goto out_wake_log_root;
2629 }
Yan Zheng7237f182009-01-21 12:54:03 -05002630
Miao Xiec6adc9c2013-05-28 10:05:39 +00002631 ret = btrfs_write_marked_extents(log_root_tree,
2632 &log_root_tree->dirty_log_pages,
2633 EXTENT_DIRTY | EXTENT_NEW);
2634 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002635 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002636 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002637 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002638 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002639 mutex_unlock(&log_root_tree->log_mutex);
2640 goto out_wake_log_root;
2641 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002642 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002643 btrfs_wait_marked_extents(log_root_tree,
2644 &log_root_tree->dirty_log_pages,
2645 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002646 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002647
David Sterba6c417612011-04-13 15:41:04 +02002648 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002649 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002650 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002651 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002652
Yan Zheng7237f182009-01-21 12:54:03 -05002653 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002654 mutex_unlock(&log_root_tree->log_mutex);
2655
2656 /*
2657 * nobody else is going to jump in and write the the ctree
2658 * super here because the log_commit atomic below is protecting
2659 * us. We must be called with a transaction handle pinning
2660 * the running transaction open, so a full commit can't hop
2661 * in and cause problems either.
2662 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002663 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002664 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002665 btrfs_set_log_full_commit(root->fs_info, trans);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002666 btrfs_abort_transaction(trans, root, ret);
2667 goto out_wake_log_root;
2668 }
Yan Zheng7237f182009-01-21 12:54:03 -05002669
Chris Mason257c62e2009-10-13 13:21:08 -04002670 mutex_lock(&root->log_mutex);
2671 if (root->last_log_commit < log_transid)
2672 root->last_log_commit = log_transid;
2673 mutex_unlock(&root->log_mutex);
2674
Chris Mason12fcfd22009-03-24 10:24:20 -04002675out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002676 /*
2677 * We needn't get log_mutex here because we are sure all
2678 * the other tasks are blocked.
2679 */
2680 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2681
Miao Xied1433de2014-02-20 18:08:59 +08002682 mutex_lock(&log_root_tree->log_mutex);
2683 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002684 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002685 mutex_unlock(&log_root_tree->log_mutex);
2686
Yan Zheng7237f182009-01-21 12:54:03 -05002687 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2688 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002689out:
Miao Xie8b050d32014-02-20 18:08:58 +08002690 /* See above. */
2691 btrfs_remove_all_log_ctxs(root, index1, ret);
2692
Miao Xied1433de2014-02-20 18:08:59 +08002693 mutex_lock(&root->log_mutex);
2694 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002695 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002696 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002697
Yan Zheng7237f182009-01-21 12:54:03 -05002698 if (waitqueue_active(&root->log_commit_wait[index1]))
2699 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002700 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002701}
2702
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002703static void free_log_tree(struct btrfs_trans_handle *trans,
2704 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002705{
2706 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002707 u64 start;
2708 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002709 struct walk_control wc = {
2710 .free = 1,
2711 .process_func = process_one_buffer
2712 };
2713
Josef Bacik681ae502013-10-07 15:11:00 -04002714 ret = walk_log_tree(trans, log, &wc);
2715 /* I don't think this can happen but just in case */
2716 if (ret)
2717 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002718
Chris Masond3977122009-01-05 21:25:51 -05002719 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002720 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002721 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2722 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002723 if (ret)
2724 break;
2725
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002726 clear_extent_bits(&log->dirty_log_pages, start, end,
2727 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002728 }
2729
Josef Bacik2ab28f32012-10-12 15:27:49 -04002730 /*
2731 * We may have short-circuited the log tree with the full commit logic
2732 * and left ordered extents on our list, so clear these out to keep us
2733 * from leaking inodes and memory.
2734 */
2735 btrfs_free_logged_extents(log, 0);
2736 btrfs_free_logged_extents(log, 1);
2737
Yan Zheng7237f182009-01-21 12:54:03 -05002738 free_extent_buffer(log->node);
2739 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002740}
2741
2742/*
2743 * free all the extents used by the tree log. This should be called
2744 * at commit time of the full transaction
2745 */
2746int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2747{
2748 if (root->log_root) {
2749 free_log_tree(trans, root->log_root);
2750 root->log_root = NULL;
2751 }
2752 return 0;
2753}
2754
2755int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2756 struct btrfs_fs_info *fs_info)
2757{
2758 if (fs_info->log_root_tree) {
2759 free_log_tree(trans, fs_info->log_root_tree);
2760 fs_info->log_root_tree = NULL;
2761 }
Chris Masone02119d2008-09-05 16:13:11 -04002762 return 0;
2763}
2764
2765/*
Chris Masone02119d2008-09-05 16:13:11 -04002766 * If both a file and directory are logged, and unlinks or renames are
2767 * mixed in, we have a few interesting corners:
2768 *
2769 * create file X in dir Y
2770 * link file X to X.link in dir Y
2771 * fsync file X
2772 * unlink file X but leave X.link
2773 * fsync dir Y
2774 *
2775 * After a crash we would expect only X.link to exist. But file X
2776 * didn't get fsync'd again so the log has back refs for X and X.link.
2777 *
2778 * We solve this by removing directory entries and inode backrefs from the
2779 * log when a file that was logged in the current transaction is
2780 * unlinked. Any later fsync will include the updated log entries, and
2781 * we'll be able to reconstruct the proper directory items from backrefs.
2782 *
2783 * This optimizations allows us to avoid relogging the entire inode
2784 * or the entire directory.
2785 */
2786int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2787 struct btrfs_root *root,
2788 const char *name, int name_len,
2789 struct inode *dir, u64 index)
2790{
2791 struct btrfs_root *log;
2792 struct btrfs_dir_item *di;
2793 struct btrfs_path *path;
2794 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002795 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002796 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002797 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002798
Chris Mason3a5f1d42008-09-11 15:53:37 -04002799 if (BTRFS_I(dir)->logged_trans < trans->transid)
2800 return 0;
2801
Chris Masone02119d2008-09-05 16:13:11 -04002802 ret = join_running_log_trans(root);
2803 if (ret)
2804 return 0;
2805
2806 mutex_lock(&BTRFS_I(dir)->log_mutex);
2807
2808 log = root->log_root;
2809 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002810 if (!path) {
2811 err = -ENOMEM;
2812 goto out_unlock;
2813 }
liubo2a29edc2011-01-26 06:22:08 +00002814
Li Zefan33345d012011-04-20 10:31:50 +08002815 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002816 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002817 if (IS_ERR(di)) {
2818 err = PTR_ERR(di);
2819 goto fail;
2820 }
2821 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002822 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2823 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002824 if (ret) {
2825 err = ret;
2826 goto fail;
2827 }
Chris Masone02119d2008-09-05 16:13:11 -04002828 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002829 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002830 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002831 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002832 if (IS_ERR(di)) {
2833 err = PTR_ERR(di);
2834 goto fail;
2835 }
2836 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002837 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2838 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002839 if (ret) {
2840 err = ret;
2841 goto fail;
2842 }
Chris Masone02119d2008-09-05 16:13:11 -04002843 }
2844
2845 /* update the directory size in the log to reflect the names
2846 * we have removed
2847 */
2848 if (bytes_del) {
2849 struct btrfs_key key;
2850
Li Zefan33345d012011-04-20 10:31:50 +08002851 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002852 key.offset = 0;
2853 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002854 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002855
2856 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002857 if (ret < 0) {
2858 err = ret;
2859 goto fail;
2860 }
Chris Masone02119d2008-09-05 16:13:11 -04002861 if (ret == 0) {
2862 struct btrfs_inode_item *item;
2863 u64 i_size;
2864
2865 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2866 struct btrfs_inode_item);
2867 i_size = btrfs_inode_size(path->nodes[0], item);
2868 if (i_size > bytes_del)
2869 i_size -= bytes_del;
2870 else
2871 i_size = 0;
2872 btrfs_set_inode_size(path->nodes[0], item, i_size);
2873 btrfs_mark_buffer_dirty(path->nodes[0]);
2874 } else
2875 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002876 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002877 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002878fail:
Chris Masone02119d2008-09-05 16:13:11 -04002879 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002880out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002881 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002882 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002883 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002884 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002885 } else if (ret < 0)
2886 btrfs_abort_transaction(trans, root, ret);
2887
Chris Mason12fcfd22009-03-24 10:24:20 -04002888 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002889
Andi Kleen411fc6b2010-10-29 15:14:31 -04002890 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002891}
2892
2893/* see comments for btrfs_del_dir_entries_in_log */
2894int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2895 struct btrfs_root *root,
2896 const char *name, int name_len,
2897 struct inode *inode, u64 dirid)
2898{
2899 struct btrfs_root *log;
2900 u64 index;
2901 int ret;
2902
Chris Mason3a5f1d42008-09-11 15:53:37 -04002903 if (BTRFS_I(inode)->logged_trans < trans->transid)
2904 return 0;
2905
Chris Masone02119d2008-09-05 16:13:11 -04002906 ret = join_running_log_trans(root);
2907 if (ret)
2908 return 0;
2909 log = root->log_root;
2910 mutex_lock(&BTRFS_I(inode)->log_mutex);
2911
Li Zefan33345d012011-04-20 10:31:50 +08002912 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002913 dirid, &index);
2914 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002915 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002916 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002917 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918 } else if (ret < 0 && ret != -ENOENT)
2919 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002920 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002921
Chris Masone02119d2008-09-05 16:13:11 -04002922 return ret;
2923}
2924
2925/*
2926 * creates a range item in the log for 'dirid'. first_offset and
2927 * last_offset tell us which parts of the key space the log should
2928 * be considered authoritative for.
2929 */
2930static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2931 struct btrfs_root *log,
2932 struct btrfs_path *path,
2933 int key_type, u64 dirid,
2934 u64 first_offset, u64 last_offset)
2935{
2936 int ret;
2937 struct btrfs_key key;
2938 struct btrfs_dir_log_item *item;
2939
2940 key.objectid = dirid;
2941 key.offset = first_offset;
2942 if (key_type == BTRFS_DIR_ITEM_KEY)
2943 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2944 else
2945 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2946 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002947 if (ret)
2948 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002949
2950 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2951 struct btrfs_dir_log_item);
2952 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2953 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002954 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002955 return 0;
2956}
2957
2958/*
2959 * log all the items included in the current transaction for a given
2960 * directory. This also creates the range items in the log tree required
2961 * to replay anything deleted before the fsync
2962 */
2963static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2964 struct btrfs_root *root, struct inode *inode,
2965 struct btrfs_path *path,
2966 struct btrfs_path *dst_path, int key_type,
2967 u64 min_offset, u64 *last_offset_ret)
2968{
2969 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002970 struct btrfs_root *log = root->log_root;
2971 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002972 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002973 int ret;
2974 int i;
2975 int nritems;
2976 u64 first_offset = min_offset;
2977 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002978 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002979
2980 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002981
Li Zefan33345d012011-04-20 10:31:50 +08002982 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002983 min_key.type = key_type;
2984 min_key.offset = min_offset;
2985
2986 path->keep_locks = 1;
2987
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002988 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002989
2990 /*
2991 * we didn't find anything from this transaction, see if there
2992 * is anything at all
2993 */
Li Zefan33345d012011-04-20 10:31:50 +08002994 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2995 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002996 min_key.type = key_type;
2997 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002998 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002999 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3000 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003001 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003002 return ret;
3003 }
Li Zefan33345d012011-04-20 10:31:50 +08003004 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003005
3006 /* if ret == 0 there are items for this type,
3007 * create a range to tell us the last key of this type.
3008 * otherwise, there are no items in this directory after
3009 * *min_offset, and we create a range to indicate that.
3010 */
3011 if (ret == 0) {
3012 struct btrfs_key tmp;
3013 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3014 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003015 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003016 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003017 }
3018 goto done;
3019 }
3020
3021 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003022 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003023 if (ret == 0) {
3024 struct btrfs_key tmp;
3025 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3026 if (key_type == tmp.type) {
3027 first_offset = tmp.offset;
3028 ret = overwrite_item(trans, log, dst_path,
3029 path->nodes[0], path->slots[0],
3030 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003031 if (ret) {
3032 err = ret;
3033 goto done;
3034 }
Chris Masone02119d2008-09-05 16:13:11 -04003035 }
3036 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003037 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003038
3039 /* find the first key from this transaction again */
3040 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303041 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003042 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003043
3044 /*
3045 * we have a block from this transaction, log every item in it
3046 * from our directory
3047 */
Chris Masond3977122009-01-05 21:25:51 -05003048 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003049 struct btrfs_key tmp;
3050 src = path->nodes[0];
3051 nritems = btrfs_header_nritems(src);
3052 for (i = path->slots[0]; i < nritems; i++) {
3053 btrfs_item_key_to_cpu(src, &min_key, i);
3054
Li Zefan33345d012011-04-20 10:31:50 +08003055 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003056 goto done;
3057 ret = overwrite_item(trans, log, dst_path, src, i,
3058 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003059 if (ret) {
3060 err = ret;
3061 goto done;
3062 }
Chris Masone02119d2008-09-05 16:13:11 -04003063 }
3064 path->slots[0] = nritems;
3065
3066 /*
3067 * look ahead to the next item and see if it is also
3068 * from this directory and from this transaction
3069 */
3070 ret = btrfs_next_leaf(root, path);
3071 if (ret == 1) {
3072 last_offset = (u64)-1;
3073 goto done;
3074 }
3075 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003076 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003077 last_offset = (u64)-1;
3078 goto done;
3079 }
3080 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3081 ret = overwrite_item(trans, log, dst_path,
3082 path->nodes[0], path->slots[0],
3083 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003084 if (ret)
3085 err = ret;
3086 else
3087 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003088 goto done;
3089 }
3090 }
3091done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003092 btrfs_release_path(path);
3093 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003094
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003095 if (err == 0) {
3096 *last_offset_ret = last_offset;
3097 /*
3098 * insert the log range keys to indicate where the log
3099 * is valid
3100 */
3101 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003102 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003103 if (ret)
3104 err = ret;
3105 }
3106 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003107}
3108
3109/*
3110 * logging directories is very similar to logging inodes, We find all the items
3111 * from the current transaction and write them to the log.
3112 *
3113 * The recovery code scans the directory in the subvolume, and if it finds a
3114 * key in the range logged that is not present in the log tree, then it means
3115 * that dir entry was unlinked during the transaction.
3116 *
3117 * In order for that scan to work, we must include one key smaller than
3118 * the smallest logged by this transaction and one key larger than the largest
3119 * key logged by this transaction.
3120 */
3121static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3122 struct btrfs_root *root, struct inode *inode,
3123 struct btrfs_path *path,
3124 struct btrfs_path *dst_path)
3125{
3126 u64 min_key;
3127 u64 max_key;
3128 int ret;
3129 int key_type = BTRFS_DIR_ITEM_KEY;
3130
3131again:
3132 min_key = 0;
3133 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003134 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003135 ret = log_dir_items(trans, root, inode, path,
3136 dst_path, key_type, min_key,
3137 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003138 if (ret)
3139 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003140 if (max_key == (u64)-1)
3141 break;
3142 min_key = max_key + 1;
3143 }
3144
3145 if (key_type == BTRFS_DIR_ITEM_KEY) {
3146 key_type = BTRFS_DIR_INDEX_KEY;
3147 goto again;
3148 }
3149 return 0;
3150}
3151
3152/*
3153 * a helper function to drop items from the log before we relog an
3154 * inode. max_key_type indicates the highest item type to remove.
3155 * This cannot be run for file data extents because it does not
3156 * free the extents they point to.
3157 */
3158static int drop_objectid_items(struct btrfs_trans_handle *trans,
3159 struct btrfs_root *log,
3160 struct btrfs_path *path,
3161 u64 objectid, int max_key_type)
3162{
3163 int ret;
3164 struct btrfs_key key;
3165 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003166 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003167
3168 key.objectid = objectid;
3169 key.type = max_key_type;
3170 key.offset = (u64)-1;
3171
Chris Masond3977122009-01-05 21:25:51 -05003172 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003173 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003174 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003175 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003176 break;
3177
3178 if (path->slots[0] == 0)
3179 break;
3180
3181 path->slots[0]--;
3182 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3183 path->slots[0]);
3184
3185 if (found_key.objectid != objectid)
3186 break;
3187
Josef Bacik18ec90d2012-09-28 11:56:28 -04003188 found_key.offset = 0;
3189 found_key.type = 0;
3190 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3191 &start_slot);
3192
3193 ret = btrfs_del_items(trans, log, path, start_slot,
3194 path->slots[0] - start_slot + 1);
3195 /*
3196 * If start slot isn't 0 then we don't need to re-search, we've
3197 * found the last guy with the objectid in this tree.
3198 */
3199 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003200 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003201 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003202 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003203 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003204 if (ret > 0)
3205 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003206 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003207}
3208
Josef Bacik94edf4a2012-09-25 14:56:25 -04003209static void fill_inode_item(struct btrfs_trans_handle *trans,
3210 struct extent_buffer *leaf,
3211 struct btrfs_inode_item *item,
3212 struct inode *inode, int log_inode_only)
3213{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003214 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003215
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003216 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003217
3218 if (log_inode_only) {
3219 /* set the generation to zero so the recover code
3220 * can tell the difference between an logging
3221 * just to say 'this inode exists' and a logging
3222 * to say 'update this inode with these values'
3223 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003224 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3225 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003226 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003227 btrfs_set_token_inode_generation(leaf, item,
3228 BTRFS_I(inode)->generation,
3229 &token);
3230 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003231 }
3232
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003233 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3234 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3235 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3236 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3237
3238 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3239 inode->i_atime.tv_sec, &token);
3240 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3241 inode->i_atime.tv_nsec, &token);
3242
3243 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3244 inode->i_mtime.tv_sec, &token);
3245 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3246 inode->i_mtime.tv_nsec, &token);
3247
3248 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3249 inode->i_ctime.tv_sec, &token);
3250 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3251 inode->i_ctime.tv_nsec, &token);
3252
3253 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3254 &token);
3255
3256 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3257 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3258 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3259 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3260 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003261}
3262
Josef Bacika95249b2012-10-11 16:17:34 -04003263static int log_inode_item(struct btrfs_trans_handle *trans,
3264 struct btrfs_root *log, struct btrfs_path *path,
3265 struct inode *inode)
3266{
3267 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003268 int ret;
3269
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003270 ret = btrfs_insert_empty_item(trans, log, path,
3271 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003272 sizeof(*inode_item));
3273 if (ret && ret != -EEXIST)
3274 return ret;
3275 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3276 struct btrfs_inode_item);
3277 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3278 btrfs_release_path(path);
3279 return 0;
3280}
3281
Chris Mason31ff1cd2008-09-11 16:17:57 -04003282static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003283 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003284 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003285 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003286 int start_slot, int nr, int inode_only)
3287{
3288 unsigned long src_offset;
3289 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003290 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003291 struct btrfs_file_extent_item *extent;
3292 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003293 struct extent_buffer *src = src_path->nodes[0];
3294 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003295 int ret;
3296 struct btrfs_key *ins_keys;
3297 u32 *ins_sizes;
3298 char *ins_data;
3299 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003300 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003301 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003302 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003303 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003304 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003305
3306 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003307
3308 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3309 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003310 if (!ins_data)
3311 return -ENOMEM;
3312
Josef Bacik16e75492013-10-22 12:18:51 -04003313 first_key.objectid = (u64)-1;
3314
Chris Mason31ff1cd2008-09-11 16:17:57 -04003315 ins_sizes = (u32 *)ins_data;
3316 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3317
3318 for (i = 0; i < nr; i++) {
3319 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3320 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3321 }
3322 ret = btrfs_insert_empty_items(trans, log, dst_path,
3323 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003324 if (ret) {
3325 kfree(ins_data);
3326 return ret;
3327 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003330 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3331 dst_path->slots[0]);
3332
3333 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3334
Josef Bacik16e75492013-10-22 12:18:51 -04003335 if ((i == (nr - 1)))
3336 last_key = ins_keys[i];
3337
Josef Bacik94edf4a2012-09-25 14:56:25 -04003338 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003339 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3340 dst_path->slots[0],
3341 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003342 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3343 inode, inode_only == LOG_INODE_EXISTS);
3344 } else {
3345 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3346 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003347 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003348
Josef Bacik16e75492013-10-22 12:18:51 -04003349 /*
3350 * We set need_find_last_extent here in case we know we were
3351 * processing other items and then walk into the first extent in
3352 * the inode. If we don't hit an extent then nothing changes,
3353 * we'll do the last search the next time around.
3354 */
3355 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3356 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003357 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003358 first_key = ins_keys[i];
3359 } else {
3360 need_find_last_extent = false;
3361 }
3362
Chris Mason31ff1cd2008-09-11 16:17:57 -04003363 /* take a reference on file data extents so that truncates
3364 * or deletes of this inode don't have to relog the inode
3365 * again
3366 */
Liu Bod2794402012-08-29 01:07:56 -06003367 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3368 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003369 int found_type;
3370 extent = btrfs_item_ptr(src, start_slot + i,
3371 struct btrfs_file_extent_item);
3372
liubo8e531cd2011-05-06 10:36:09 +08003373 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3374 continue;
3375
Chris Mason31ff1cd2008-09-11 16:17:57 -04003376 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003377 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003378 u64 ds, dl, cs, cl;
3379 ds = btrfs_file_extent_disk_bytenr(src,
3380 extent);
3381 /* ds == 0 is a hole */
3382 if (ds == 0)
3383 continue;
3384
3385 dl = btrfs_file_extent_disk_num_bytes(src,
3386 extent);
3387 cs = btrfs_file_extent_offset(src, extent);
3388 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003389 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003390 if (btrfs_file_extent_compression(src,
3391 extent)) {
3392 cs = 0;
3393 cl = dl;
3394 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003395
3396 ret = btrfs_lookup_csums_range(
3397 log->fs_info->csum_root,
3398 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003399 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003400 if (ret) {
3401 btrfs_release_path(dst_path);
3402 kfree(ins_data);
3403 return ret;
3404 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003405 }
3406 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003407 }
3408
3409 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003410 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003411 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003412
3413 /*
3414 * we have to do this after the loop above to avoid changing the
3415 * log tree while trying to change the log tree.
3416 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003417 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003418 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003419 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3420 struct btrfs_ordered_sum,
3421 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003422 if (!ret)
3423 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003424 list_del(&sums->list);
3425 kfree(sums);
3426 }
Josef Bacik16e75492013-10-22 12:18:51 -04003427
3428 if (!has_extents)
3429 return ret;
3430
Filipe Manana74121f7c2014-08-07 12:00:44 +01003431 if (need_find_last_extent && *last_extent == first_key.offset) {
3432 /*
3433 * We don't have any leafs between our current one and the one
3434 * we processed before that can have file extent items for our
3435 * inode (and have a generation number smaller than our current
3436 * transaction id).
3437 */
3438 need_find_last_extent = false;
3439 }
3440
Josef Bacik16e75492013-10-22 12:18:51 -04003441 /*
3442 * Because we use btrfs_search_forward we could skip leaves that were
3443 * not modified and then assume *last_extent is valid when it really
3444 * isn't. So back up to the previous leaf and read the end of the last
3445 * extent before we go and fill in holes.
3446 */
3447 if (need_find_last_extent) {
3448 u64 len;
3449
3450 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3451 if (ret < 0)
3452 return ret;
3453 if (ret)
3454 goto fill_holes;
3455 if (src_path->slots[0])
3456 src_path->slots[0]--;
3457 src = src_path->nodes[0];
3458 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3459 if (key.objectid != btrfs_ino(inode) ||
3460 key.type != BTRFS_EXTENT_DATA_KEY)
3461 goto fill_holes;
3462 extent = btrfs_item_ptr(src, src_path->slots[0],
3463 struct btrfs_file_extent_item);
3464 if (btrfs_file_extent_type(src, extent) ==
3465 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003466 len = btrfs_file_extent_inline_len(src,
3467 src_path->slots[0],
3468 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003469 *last_extent = ALIGN(key.offset + len,
3470 log->sectorsize);
3471 } else {
3472 len = btrfs_file_extent_num_bytes(src, extent);
3473 *last_extent = key.offset + len;
3474 }
3475 }
3476fill_holes:
3477 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3478 * things could have happened
3479 *
3480 * 1) A merge could have happened, so we could currently be on a leaf
3481 * that holds what we were copying in the first place.
3482 * 2) A split could have happened, and now not all of the items we want
3483 * are on the same leaf.
3484 *
3485 * So we need to adjust how we search for holes, we need to drop the
3486 * path and re-search for the first extent key we found, and then walk
3487 * forward until we hit the last one we copied.
3488 */
3489 if (need_find_last_extent) {
3490 /* btrfs_prev_leaf could return 1 without releasing the path */
3491 btrfs_release_path(src_path);
3492 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3493 src_path, 0, 0);
3494 if (ret < 0)
3495 return ret;
3496 ASSERT(ret == 0);
3497 src = src_path->nodes[0];
3498 i = src_path->slots[0];
3499 } else {
3500 i = start_slot;
3501 }
3502
3503 /*
3504 * Ok so here we need to go through and fill in any holes we may have
3505 * to make sure that holes are punched for those areas in case they had
3506 * extents previously.
3507 */
3508 while (!done) {
3509 u64 offset, len;
3510 u64 extent_end;
3511
3512 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3513 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3514 if (ret < 0)
3515 return ret;
3516 ASSERT(ret == 0);
3517 src = src_path->nodes[0];
3518 i = 0;
3519 }
3520
3521 btrfs_item_key_to_cpu(src, &key, i);
3522 if (!btrfs_comp_cpu_keys(&key, &last_key))
3523 done = true;
3524 if (key.objectid != btrfs_ino(inode) ||
3525 key.type != BTRFS_EXTENT_DATA_KEY) {
3526 i++;
3527 continue;
3528 }
3529 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3530 if (btrfs_file_extent_type(src, extent) ==
3531 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003532 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003533 extent_end = ALIGN(key.offset + len, log->sectorsize);
3534 } else {
3535 len = btrfs_file_extent_num_bytes(src, extent);
3536 extent_end = key.offset + len;
3537 }
3538 i++;
3539
3540 if (*last_extent == key.offset) {
3541 *last_extent = extent_end;
3542 continue;
3543 }
3544 offset = *last_extent;
3545 len = key.offset - *last_extent;
3546 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3547 offset, 0, 0, len, 0, len, 0,
3548 0, 0);
3549 if (ret)
3550 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003551 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003552 }
3553 /*
3554 * Need to let the callers know we dropped the path so they should
3555 * re-search.
3556 */
3557 if (!ret && need_find_last_extent)
3558 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003559 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003560}
3561
Josef Bacik5dc562c2012-08-17 13:14:17 -04003562static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3563{
3564 struct extent_map *em1, *em2;
3565
3566 em1 = list_entry(a, struct extent_map, list);
3567 em2 = list_entry(b, struct extent_map, list);
3568
3569 if (em1->start < em2->start)
3570 return -1;
3571 else if (em1->start > em2->start)
3572 return 1;
3573 return 0;
3574}
3575
Josef Bacik5dc562c2012-08-17 13:14:17 -04003576static int log_one_extent(struct btrfs_trans_handle *trans,
3577 struct inode *inode, struct btrfs_root *root,
Miao Xie827463c2014-01-14 20:31:51 +08003578 struct extent_map *em, struct btrfs_path *path,
3579 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003580{
3581 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003582 struct btrfs_file_extent_item *fi;
3583 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003584 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003585 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003586 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003587 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003588 u64 mod_start = em->mod_start;
3589 u64 mod_len = em->mod_len;
3590 u64 csum_offset;
3591 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003592 u64 extent_offset = em->start - em->orig_start;
3593 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003594 int ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003595 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003596 int extent_inserted = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003597
Josef Bacik70c8a912012-10-11 16:54:30 -04003598 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003599 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003600
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003601 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3602 em->start + em->len, NULL, 0, 1,
3603 sizeof(*fi), &extent_inserted);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003604 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003605 return ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003606
3607 if (!extent_inserted) {
3608 key.objectid = btrfs_ino(inode);
3609 key.type = BTRFS_EXTENT_DATA_KEY;
3610 key.offset = em->start;
3611
3612 ret = btrfs_insert_empty_item(trans, log, path, &key,
3613 sizeof(*fi));
3614 if (ret)
3615 return ret;
3616 }
Josef Bacik70c8a912012-10-11 16:54:30 -04003617 leaf = path->nodes[0];
3618 fi = btrfs_item_ptr(leaf, path->slots[0],
3619 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003620
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003621 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3622 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3624 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003625 btrfs_set_token_file_extent_type(leaf, fi,
3626 BTRFS_FILE_EXTENT_PREALLOC,
3627 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003628 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003629 btrfs_set_token_file_extent_type(leaf, fi,
3630 BTRFS_FILE_EXTENT_REG,
3631 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003632 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003633 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003634 }
3635
Josef Bacik70c8a912012-10-11 16:54:30 -04003636 block_len = max(em->block_len, em->orig_block_len);
3637 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003638 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3639 em->block_start,
3640 &token);
3641 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3642 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003643 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003644 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3645 em->block_start -
3646 extent_offset, &token);
3647 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3648 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003649 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003650 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3651 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3652 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003653 }
3654
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003655 btrfs_set_token_file_extent_offset(leaf, fi,
3656 em->start - em->orig_start,
3657 &token);
3658 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003659 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003660 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3661 &token);
3662 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3663 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003664 btrfs_mark_buffer_dirty(leaf);
3665
Josef Bacik70c8a912012-10-11 16:54:30 -04003666 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003667 if (ret) {
3668 return ret;
3669 }
3670
3671 if (skip_csum)
3672 return 0;
3673
Josef Bacik2ab28f32012-10-12 15:27:49 -04003674 /*
3675 * First check and see if our csums are on our outstanding ordered
3676 * extents.
3677 */
Miao Xie827463c2014-01-14 20:31:51 +08003678 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003679 struct btrfs_ordered_sum *sum;
3680
3681 if (!mod_len)
3682 break;
3683
Josef Bacik2ab28f32012-10-12 15:27:49 -04003684 if (ordered->file_offset + ordered->len <= mod_start ||
3685 mod_start + mod_len <= ordered->file_offset)
3686 continue;
3687
3688 /*
3689 * We are going to copy all the csums on this ordered extent, so
3690 * go ahead and adjust mod_start and mod_len in case this
3691 * ordered extent has already been logged.
3692 */
3693 if (ordered->file_offset > mod_start) {
3694 if (ordered->file_offset + ordered->len >=
3695 mod_start + mod_len)
3696 mod_len = ordered->file_offset - mod_start;
3697 /*
3698 * If we have this case
3699 *
3700 * |--------- logged extent ---------|
3701 * |----- ordered extent ----|
3702 *
3703 * Just don't mess with mod_start and mod_len, we'll
3704 * just end up logging more csums than we need and it
3705 * will be ok.
3706 */
3707 } else {
3708 if (ordered->file_offset + ordered->len <
3709 mod_start + mod_len) {
3710 mod_len = (mod_start + mod_len) -
3711 (ordered->file_offset + ordered->len);
3712 mod_start = ordered->file_offset +
3713 ordered->len;
3714 } else {
3715 mod_len = 0;
3716 }
3717 }
3718
3719 /*
3720 * To keep us from looping for the above case of an ordered
3721 * extent that falls inside of the logged extent.
3722 */
3723 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3724 &ordered->flags))
3725 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003726
Miao Xie23c671a2014-01-14 20:31:52 +08003727 if (ordered->csum_bytes_left) {
3728 btrfs_start_ordered_extent(inode, ordered, 0);
3729 wait_event(ordered->wait,
3730 ordered->csum_bytes_left == 0);
3731 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003732
3733 list_for_each_entry(sum, &ordered->list, list) {
3734 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003735 if (ret)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003736 goto unlocked;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003737 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003738
3739 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003740unlocked:
3741
3742 if (!mod_len || ret)
3743 return ret;
3744
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003745 if (em->compress_type) {
3746 csum_offset = 0;
3747 csum_len = block_len;
3748 } else {
3749 csum_offset = mod_start - em->start;
3750 csum_len = mod_len;
3751 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003752
Josef Bacik70c8a912012-10-11 16:54:30 -04003753 /* block start is already adjusted for the file extent offset. */
3754 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3755 em->block_start + csum_offset,
3756 em->block_start + csum_offset +
3757 csum_len - 1, &ordered_sums, 0);
3758 if (ret)
3759 return ret;
3760
3761 while (!list_empty(&ordered_sums)) {
3762 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3763 struct btrfs_ordered_sum,
3764 list);
3765 if (!ret)
3766 ret = btrfs_csum_file_blocks(trans, log, sums);
3767 list_del(&sums->list);
3768 kfree(sums);
3769 }
3770
3771 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003772}
3773
3774static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3775 struct btrfs_root *root,
3776 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003777 struct btrfs_path *path,
3778 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003779{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003780 struct extent_map *em, *n;
3781 struct list_head extents;
3782 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3783 u64 test_gen;
3784 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003785 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003786
3787 INIT_LIST_HEAD(&extents);
3788
Josef Bacik5dc562c2012-08-17 13:14:17 -04003789 write_lock(&tree->lock);
3790 test_gen = root->fs_info->last_trans_committed;
3791
3792 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3793 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003794
3795 /*
3796 * Just an arbitrary number, this can be really CPU intensive
3797 * once we start getting a lot of extents, and really once we
3798 * have a bunch of extents we just want to commit since it will
3799 * be faster.
3800 */
3801 if (++num > 32768) {
3802 list_del_init(&tree->modified_extents);
3803 ret = -EFBIG;
3804 goto process;
3805 }
3806
Josef Bacik5dc562c2012-08-17 13:14:17 -04003807 if (em->generation <= test_gen)
3808 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003809 /* Need a ref to keep it from getting evicted from cache */
3810 atomic_inc(&em->refs);
3811 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003812 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003813 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003814 }
3815
3816 list_sort(NULL, &extents, extent_cmp);
3817
Josef Bacik2ab28f32012-10-12 15:27:49 -04003818process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003819 while (!list_empty(&extents)) {
3820 em = list_entry(extents.next, struct extent_map, list);
3821
3822 list_del_init(&em->list);
3823
3824 /*
3825 * If we had an error we just need to delete everybody from our
3826 * private list.
3827 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003828 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003829 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003830 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003831 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003832 }
3833
3834 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003835
Miao Xie827463c2014-01-14 20:31:51 +08003836 ret = log_one_extent(trans, inode, root, em, path, logged_list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003837 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003838 clear_em_logging(tree, em);
3839 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003840 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003841 WARN_ON(!list_empty(&extents));
3842 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003843
Josef Bacik5dc562c2012-08-17 13:14:17 -04003844 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003845 return ret;
3846}
3847
Chris Masone02119d2008-09-05 16:13:11 -04003848/* log a single inode in the tree log.
3849 * At least one parent directory for this inode must exist in the tree
3850 * or be logged already.
3851 *
3852 * Any items from this inode changed by the current transaction are copied
3853 * to the log tree. An extra reference is taken on any extents in this
3854 * file, allowing us to avoid a whole pile of corner cases around logging
3855 * blocks that have been removed from the tree.
3856 *
3857 * See LOG_INODE_ALL and related defines for a description of what inode_only
3858 * does.
3859 *
3860 * This handles both files and directories.
3861 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003862static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01003863 struct btrfs_root *root, struct inode *inode,
3864 int inode_only,
3865 const loff_t start,
3866 const loff_t end)
Chris Masone02119d2008-09-05 16:13:11 -04003867{
3868 struct btrfs_path *path;
3869 struct btrfs_path *dst_path;
3870 struct btrfs_key min_key;
3871 struct btrfs_key max_key;
3872 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003873 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003874 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003875 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003876 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003877 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003878 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003879 int ins_start_slot = 0;
3880 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003881 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003882 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01003883 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masone02119d2008-09-05 16:13:11 -04003884
Chris Masone02119d2008-09-05 16:13:11 -04003885 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003886 if (!path)
3887 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003888 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003889 if (!dst_path) {
3890 btrfs_free_path(path);
3891 return -ENOMEM;
3892 }
Chris Masone02119d2008-09-05 16:13:11 -04003893
Li Zefan33345d012011-04-20 10:31:50 +08003894 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003895 min_key.type = BTRFS_INODE_ITEM_KEY;
3896 min_key.offset = 0;
3897
Li Zefan33345d012011-04-20 10:31:50 +08003898 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003899
Chris Mason12fcfd22009-03-24 10:24:20 -04003900
Josef Bacik5dc562c2012-08-17 13:14:17 -04003901 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003902 if (S_ISDIR(inode->i_mode) ||
3903 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3904 &BTRFS_I(inode)->runtime_flags) &&
3905 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003906 max_key.type = BTRFS_XATTR_ITEM_KEY;
3907 else
3908 max_key.type = (u8)-1;
3909 max_key.offset = (u64)-1;
3910
Josef Bacik94edf4a2012-09-25 14:56:25 -04003911 /* Only run delayed items if we are a dir or a new file */
3912 if (S_ISDIR(inode->i_mode) ||
3913 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3914 ret = btrfs_commit_inode_delayed_items(trans, inode);
3915 if (ret) {
3916 btrfs_free_path(path);
3917 btrfs_free_path(dst_path);
3918 return ret;
3919 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003920 }
3921
Chris Masone02119d2008-09-05 16:13:11 -04003922 mutex_lock(&BTRFS_I(inode)->log_mutex);
3923
Miao Xie827463c2014-01-14 20:31:51 +08003924 btrfs_get_logged_extents(inode, &logged_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003925
Chris Masone02119d2008-09-05 16:13:11 -04003926 /*
3927 * a brute force approach to making sure we get the most uptodate
3928 * copies of everything.
3929 */
3930 if (S_ISDIR(inode->i_mode)) {
3931 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3932
3933 if (inode_only == LOG_INODE_EXISTS)
3934 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003935 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003936 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003937 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3938 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003939 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3940 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003941 ret = btrfs_truncate_inode_items(trans, log,
3942 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003943 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05003944 &BTRFS_I(inode)->runtime_flags) ||
3945 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04003946 if (inode_only == LOG_INODE_ALL)
3947 fast_search = true;
3948 max_key.type = BTRFS_XATTR_ITEM_KEY;
3949 ret = drop_objectid_items(trans, log, path, ino,
3950 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003951 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003952 if (inode_only == LOG_INODE_ALL)
3953 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003954 ret = log_inode_item(trans, log, dst_path, inode);
3955 if (ret) {
3956 err = ret;
3957 goto out_unlock;
3958 }
3959 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003960 }
Josef Bacika95249b2012-10-11 16:17:34 -04003961
Chris Masone02119d2008-09-05 16:13:11 -04003962 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003963 if (ret) {
3964 err = ret;
3965 goto out_unlock;
3966 }
Chris Masone02119d2008-09-05 16:13:11 -04003967 path->keep_locks = 1;
3968
Chris Masond3977122009-01-05 21:25:51 -05003969 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003970 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003971 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003972 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003973 if (ret != 0)
3974 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003975again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003976 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003977 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003978 break;
3979 if (min_key.type > max_key.type)
3980 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003981
Chris Masone02119d2008-09-05 16:13:11 -04003982 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003983 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3984 ins_nr++;
3985 goto next_slot;
3986 } else if (!ins_nr) {
3987 ins_start_slot = path->slots[0];
3988 ins_nr = 1;
3989 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003990 }
3991
Josef Bacik16e75492013-10-22 12:18:51 -04003992 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3993 ins_start_slot, ins_nr, inode_only);
3994 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003995 err = ret;
3996 goto out_unlock;
Josef Bacik16e75492013-10-22 12:18:51 -04003997 } if (ret) {
3998 ins_nr = 0;
3999 btrfs_release_path(path);
4000 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004001 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004002 ins_nr = 1;
4003 ins_start_slot = path->slots[0];
4004next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004005
Chris Mason3a5f1d42008-09-11 15:53:37 -04004006 nritems = btrfs_header_nritems(path->nodes[0]);
4007 path->slots[0]++;
4008 if (path->slots[0] < nritems) {
4009 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4010 path->slots[0]);
4011 goto again;
4012 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004013 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004014 ret = copy_items(trans, inode, dst_path, path,
4015 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04004016 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04004017 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004018 err = ret;
4019 goto out_unlock;
4020 }
Josef Bacik16e75492013-10-22 12:18:51 -04004021 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004022 ins_nr = 0;
4023 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004024 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004025
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004026 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004027 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004028 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004029 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004030 min_key.offset = 0;
4031 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004032 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004033 }
Chris Masone02119d2008-09-05 16:13:11 -04004034 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004035 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004036 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4037 ins_start_slot, ins_nr, inode_only);
4038 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004039 err = ret;
4040 goto out_unlock;
4041 }
Josef Bacik16e75492013-10-22 12:18:51 -04004042 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004043 ins_nr = 0;
4044 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004045
Josef Bacika95249b2012-10-11 16:17:34 -04004046log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004047 btrfs_release_path(path);
4048 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004049 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004050 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4051 &logged_list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004052 if (ret) {
4053 err = ret;
4054 goto out_unlock;
4055 }
Josef Bacikd006a042013-11-12 20:54:09 -05004056 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004057 struct extent_map *em, *n;
4058
Filipe Manana49dae1b2014-09-06 22:34:39 +01004059 write_lock(&em_tree->lock);
4060 /*
4061 * We can't just remove every em if we're called for a ranged
4062 * fsync - that is, one that doesn't cover the whole possible
4063 * file range (0 to LLONG_MAX). This is because we can have
4064 * em's that fall outside the range we're logging and therefore
4065 * their ordered operations haven't completed yet
4066 * (btrfs_finish_ordered_io() not invoked yet). This means we
4067 * didn't get their respective file extent item in the fs/subvol
4068 * tree yet, and need to let the next fast fsync (one which
4069 * consults the list of modified extent maps) find the em so
4070 * that it logs a matching file extent item and waits for the
4071 * respective ordered operation to complete (if it's still
4072 * running).
4073 *
4074 * Removing every em outside the range we're logging would make
4075 * the next fast fsync not log their matching file extent items,
4076 * therefore making us lose data after a log replay.
4077 */
4078 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4079 list) {
4080 const u64 mod_end = em->mod_start + em->mod_len - 1;
4081
4082 if (em->mod_start >= start && mod_end <= end)
4083 list_del_init(&em->list);
4084 }
4085 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004086 }
4087
Chris Mason9623f9a2008-09-11 17:42:42 -04004088 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004089 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004090 if (ret) {
4091 err = ret;
4092 goto out_unlock;
4093 }
Chris Masone02119d2008-09-05 16:13:11 -04004094 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004095
Filipe Manana125c4cf92014-09-11 21:22:14 +01004096 BTRFS_I(inode)->logged_trans = trans->transid;
4097 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004098out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004099 if (unlikely(err))
4100 btrfs_put_logged_extents(&logged_list);
4101 else
4102 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004103 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4104
4105 btrfs_free_path(path);
4106 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004107 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004108}
4109
Chris Mason12fcfd22009-03-24 10:24:20 -04004110/*
4111 * follow the dentry parent pointers up the chain and see if any
4112 * of the directories in it require a full commit before they can
4113 * be logged. Returns zero if nothing special needs to be done or 1 if
4114 * a full commit is required.
4115 */
4116static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4117 struct inode *inode,
4118 struct dentry *parent,
4119 struct super_block *sb,
4120 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004121{
Chris Mason12fcfd22009-03-24 10:24:20 -04004122 int ret = 0;
4123 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004124 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004125 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004126
Chris Masonaf4176b2009-03-24 10:24:31 -04004127 /*
4128 * for regular files, if its inode is already on disk, we don't
4129 * have to worry about the parents at all. This is because
4130 * we can use the last_unlink_trans field to record renames
4131 * and other fun in this file.
4132 */
4133 if (S_ISREG(inode->i_mode) &&
4134 BTRFS_I(inode)->generation <= last_committed &&
4135 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4136 goto out;
4137
Chris Mason12fcfd22009-03-24 10:24:20 -04004138 if (!S_ISDIR(inode->i_mode)) {
4139 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4140 goto out;
4141 inode = parent->d_inode;
4142 }
4143
4144 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004145 /*
4146 * If we are logging a directory then we start with our inode,
4147 * not our parents inode, so we need to skipp setting the
4148 * logged_trans so that further down in the log code we don't
4149 * think this inode has already been logged.
4150 */
4151 if (inode != orig_inode)
4152 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004153 smp_mb();
4154
4155 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4156 root = BTRFS_I(inode)->root;
4157
4158 /*
4159 * make sure any commits to the log are forced
4160 * to be full commits
4161 */
Miao Xie995946d2014-04-02 19:51:06 +08004162 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04004163 ret = 1;
4164 break;
4165 }
4166
4167 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4168 break;
4169
Yan, Zheng76dda932009-09-21 16:00:26 -04004170 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004171 break;
4172
Josef Bacik6a912212010-11-20 09:48:00 +00004173 parent = dget_parent(parent);
4174 dput(old_parent);
4175 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004176 inode = parent->d_inode;
4177
4178 }
Josef Bacik6a912212010-11-20 09:48:00 +00004179 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004180out:
Chris Masone02119d2008-09-05 16:13:11 -04004181 return ret;
4182}
4183
4184/*
4185 * helper function around btrfs_log_inode to make sure newly created
4186 * parent directories also end up in the log. A minimal inode and backref
4187 * only logging is done of any parent directories that are older than
4188 * the last committed transaction
4189 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004190static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4191 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004192 struct dentry *parent,
4193 const loff_t start,
4194 const loff_t end,
4195 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08004196 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004197{
Chris Mason12fcfd22009-03-24 10:24:20 -04004198 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004199 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004200 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004201 int ret = 0;
4202 u64 last_committed = root->fs_info->last_trans_committed;
4203
4204 sb = inode->i_sb;
4205
Sage Weil3a5e1402009-04-02 16:49:40 -04004206 if (btrfs_test_opt(root, NOTREELOG)) {
4207 ret = 1;
4208 goto end_no_trans;
4209 }
4210
Miao Xie995946d2014-04-02 19:51:06 +08004211 /*
4212 * The prev transaction commit doesn't complete, we need do
4213 * full commit by ourselves.
4214 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004215 if (root->fs_info->last_trans_log_full_commit >
4216 root->fs_info->last_trans_committed) {
4217 ret = 1;
4218 goto end_no_trans;
4219 }
4220
Yan, Zheng76dda932009-09-21 16:00:26 -04004221 if (root != BTRFS_I(inode)->root ||
4222 btrfs_root_refs(&root->root_item) == 0) {
4223 ret = 1;
4224 goto end_no_trans;
4225 }
4226
Chris Mason12fcfd22009-03-24 10:24:20 -04004227 ret = check_parent_dirs_for_sync(trans, inode, parent,
4228 sb, last_committed);
4229 if (ret)
4230 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004231
Josef Bacik22ee6985d2012-05-29 16:57:49 -04004232 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004233 ret = BTRFS_NO_LOG_SYNC;
4234 goto end_no_trans;
4235 }
4236
Miao Xie8b050d32014-02-20 18:08:58 +08004237 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004238 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004239 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004240
Filipe Manana49dae1b2014-09-06 22:34:39 +01004241 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004242 if (ret)
4243 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004244
Chris Masonaf4176b2009-03-24 10:24:31 -04004245 /*
4246 * for regular files, if its inode is already on disk, we don't
4247 * have to worry about the parents at all. This is because
4248 * we can use the last_unlink_trans field to record renames
4249 * and other fun in this file.
4250 */
4251 if (S_ISREG(inode->i_mode) &&
4252 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004253 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4254 ret = 0;
4255 goto end_trans;
4256 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004257
4258 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05004259 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004260 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004261 break;
4262
Chris Mason12fcfd22009-03-24 10:24:20 -04004263 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004264 if (root != BTRFS_I(inode)->root)
4265 break;
4266
Chris Mason12fcfd22009-03-24 10:24:20 -04004267 if (BTRFS_I(inode)->generation >
4268 root->fs_info->last_trans_committed) {
Filipe Manana49dae1b2014-09-06 22:34:39 +01004269 ret = btrfs_log_inode(trans, root, inode, inode_only,
4270 0, LLONG_MAX);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004271 if (ret)
4272 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004273 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004274 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004275 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004276
Josef Bacik6a912212010-11-20 09:48:00 +00004277 parent = dget_parent(parent);
4278 dput(old_parent);
4279 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004280 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004281 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004282end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004283 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004284 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08004285 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004286 ret = 1;
4287 }
Miao Xie8b050d32014-02-20 18:08:58 +08004288
4289 if (ret)
4290 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04004291 btrfs_end_log_trans(root);
4292end_no_trans:
4293 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004294}
4295
4296/*
4297 * it is not safe to log dentry if the chunk root has added new
4298 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4299 * If this returns 1, you must commit the transaction to safely get your
4300 * data on disk.
4301 */
4302int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08004303 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004304 const loff_t start,
4305 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08004306 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004307{
Josef Bacik6a912212010-11-20 09:48:00 +00004308 struct dentry *parent = dget_parent(dentry);
4309 int ret;
4310
Miao Xie8b050d32014-02-20 18:08:58 +08004311 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004312 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00004313 dput(parent);
4314
4315 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004316}
4317
4318/*
4319 * should be called during mount to recover any replay any log trees
4320 * from the FS
4321 */
4322int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4323{
4324 int ret;
4325 struct btrfs_path *path;
4326 struct btrfs_trans_handle *trans;
4327 struct btrfs_key key;
4328 struct btrfs_key found_key;
4329 struct btrfs_key tmp_key;
4330 struct btrfs_root *log;
4331 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4332 struct walk_control wc = {
4333 .process_func = process_one_buffer,
4334 .stage = 0,
4335 };
4336
Chris Masone02119d2008-09-05 16:13:11 -04004337 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004338 if (!path)
4339 return -ENOMEM;
4340
4341 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004342
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004343 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004344 if (IS_ERR(trans)) {
4345 ret = PTR_ERR(trans);
4346 goto error;
4347 }
Chris Masone02119d2008-09-05 16:13:11 -04004348
4349 wc.trans = trans;
4350 wc.pin = 1;
4351
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004352 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004353 if (ret) {
4354 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4355 "recovering log root tree.");
4356 goto error;
4357 }
Chris Masone02119d2008-09-05 16:13:11 -04004358
4359again:
4360 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4361 key.offset = (u64)-1;
4362 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4363
Chris Masond3977122009-01-05 21:25:51 -05004364 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004365 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004366
4367 if (ret < 0) {
4368 btrfs_error(fs_info, ret,
4369 "Couldn't find tree log root.");
4370 goto error;
4371 }
Chris Masone02119d2008-09-05 16:13:11 -04004372 if (ret > 0) {
4373 if (path->slots[0] == 0)
4374 break;
4375 path->slots[0]--;
4376 }
4377 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4378 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004379 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004380 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4381 break;
4382
Miao Xiecb517ea2013-05-15 07:48:19 +00004383 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004384 if (IS_ERR(log)) {
4385 ret = PTR_ERR(log);
4386 btrfs_error(fs_info, ret,
4387 "Couldn't read tree log root.");
4388 goto error;
4389 }
Chris Masone02119d2008-09-05 16:13:11 -04004390
4391 tmp_key.objectid = found_key.offset;
4392 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4393 tmp_key.offset = (u64)-1;
4394
4395 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004396 if (IS_ERR(wc.replay_dest)) {
4397 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004398 free_extent_buffer(log->node);
4399 free_extent_buffer(log->commit_root);
4400 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004401 btrfs_error(fs_info, ret, "Couldn't read target root "
4402 "for tree log recovery.");
4403 goto error;
4404 }
Chris Masone02119d2008-09-05 16:13:11 -04004405
Yan Zheng07d400a2009-01-06 11:42:00 -05004406 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004408 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004409
Josef Bacikb50c6e22013-04-25 15:55:30 -04004410 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004411 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4412 path);
Chris Masone02119d2008-09-05 16:13:11 -04004413 }
Chris Masone02119d2008-09-05 16:13:11 -04004414
4415 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004416 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004417 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004418 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004419 kfree(log);
4420
Josef Bacikb50c6e22013-04-25 15:55:30 -04004421 if (ret)
4422 goto error;
4423
Chris Masone02119d2008-09-05 16:13:11 -04004424 if (found_key.offset == 0)
4425 break;
4426 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004427 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004428
4429 /* step one is to pin it all, step two is to replay just inodes */
4430 if (wc.pin) {
4431 wc.pin = 0;
4432 wc.process_func = replay_one_buffer;
4433 wc.stage = LOG_WALK_REPLAY_INODES;
4434 goto again;
4435 }
4436 /* step three is to replay everything */
4437 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4438 wc.stage++;
4439 goto again;
4440 }
4441
4442 btrfs_free_path(path);
4443
Josef Bacikabefa552013-04-24 16:40:05 -04004444 /* step 4: commit the transaction, which also unpins the blocks */
4445 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4446 if (ret)
4447 return ret;
4448
Chris Masone02119d2008-09-05 16:13:11 -04004449 free_extent_buffer(log_root_tree->node);
4450 log_root_tree->log_root = NULL;
4451 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004452 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004453
Josef Bacikabefa552013-04-24 16:40:05 -04004454 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004455error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004456 if (wc.trans)
4457 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004458 btrfs_free_path(path);
4459 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004460}
Chris Mason12fcfd22009-03-24 10:24:20 -04004461
4462/*
4463 * there are some corner cases where we want to force a full
4464 * commit instead of allowing a directory to be logged.
4465 *
4466 * They revolve around files there were unlinked from the directory, and
4467 * this function updates the parent directory so that a full commit is
4468 * properly done if it is fsync'd later after the unlinks are done.
4469 */
4470void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4471 struct inode *dir, struct inode *inode,
4472 int for_rename)
4473{
4474 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004475 * when we're logging a file, if it hasn't been renamed
4476 * or unlinked, and its inode is fully committed on disk,
4477 * we don't have to worry about walking up the directory chain
4478 * to log its parents.
4479 *
4480 * So, we use the last_unlink_trans field to put this transid
4481 * into the file. When the file is logged we check it and
4482 * don't log the parents if the file is fully on disk.
4483 */
4484 if (S_ISREG(inode->i_mode))
4485 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4486
4487 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004488 * if this directory was already logged any new
4489 * names for this file/dir will get recorded
4490 */
4491 smp_mb();
4492 if (BTRFS_I(dir)->logged_trans == trans->transid)
4493 return;
4494
4495 /*
4496 * if the inode we're about to unlink was logged,
4497 * the log will be properly updated for any new names
4498 */
4499 if (BTRFS_I(inode)->logged_trans == trans->transid)
4500 return;
4501
4502 /*
4503 * when renaming files across directories, if the directory
4504 * there we're unlinking from gets fsync'd later on, there's
4505 * no way to find the destination directory later and fsync it
4506 * properly. So, we have to be conservative and force commits
4507 * so the new name gets discovered.
4508 */
4509 if (for_rename)
4510 goto record;
4511
4512 /* we can safely do the unlink without any special recording */
4513 return;
4514
4515record:
4516 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4517}
4518
4519/*
4520 * Call this after adding a new name for a file and it will properly
4521 * update the log to reflect the new name.
4522 *
4523 * It will return zero if all goes well, and it will return 1 if a
4524 * full transaction commit is required.
4525 */
4526int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4527 struct inode *inode, struct inode *old_dir,
4528 struct dentry *parent)
4529{
4530 struct btrfs_root * root = BTRFS_I(inode)->root;
4531
4532 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004533 * this will force the logging code to walk the dentry chain
4534 * up for the file
4535 */
4536 if (S_ISREG(inode->i_mode))
4537 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4538
4539 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004540 * if this inode hasn't been logged and directory we're renaming it
4541 * from hasn't been logged, we don't need to log it
4542 */
4543 if (BTRFS_I(inode)->logged_trans <=
4544 root->fs_info->last_trans_committed &&
4545 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4546 root->fs_info->last_trans_committed))
4547 return 0;
4548
Filipe Manana49dae1b2014-09-06 22:34:39 +01004549 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
4550 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04004551}
4552