2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
21 #include "transaction.h"
24 #include "check/mode-common.h"
27 * Search in csum tree to find how many bytes of range [@start, @start + @len)
28 * has the corresponding csum item.
32 * @found: return value of found csum bytes
35 int count_csum_range(struct btrfs_fs_info *fs_info, u64 start,
39 struct btrfs_path path;
40 struct extent_buffer *leaf;
45 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
47 btrfs_init_path(&path);
49 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
51 key.type = BTRFS_EXTENT_CSUM_KEY;
53 ret = btrfs_search_slot(NULL, fs_info->csum_root,
57 if (ret > 0 && path.slots[0] > 0) {
59 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
60 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
61 key.type == BTRFS_EXTENT_CSUM_KEY)
67 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
68 ret = btrfs_next_leaf(fs_info->csum_root, &path);
76 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
77 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
78 key.type != BTRFS_EXTENT_CSUM_KEY)
81 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
82 if (key.offset >= start + len)
85 if (key.offset > start)
88 size = btrfs_item_size_nr(leaf, path.slots[0]);
89 csum_end = key.offset + (size / csum_size) *
91 if (csum_end > start) {
92 size = min(csum_end - start, len);
101 btrfs_release_path(&path);
108 * Wrapper to insert one inode item into given @root
109 * Timestamp will be set to current time.
111 * @root: the root to insert inode item into
114 * @nbytes: nbytes (real used size, without hole)
115 * @nlink: number of links
116 * @mode: file mode, including S_IF* bits
118 int insert_inode_item(struct btrfs_trans_handle *trans,
119 struct btrfs_root *root, u64 ino, u64 size,
120 u64 nbytes, u64 nlink, u32 mode)
122 struct btrfs_inode_item ii;
123 time_t now = time(NULL);
126 btrfs_set_stack_inode_size(&ii, size);
127 btrfs_set_stack_inode_nbytes(&ii, nbytes);
128 btrfs_set_stack_inode_nlink(&ii, nlink);
129 btrfs_set_stack_inode_mode(&ii, mode);
130 btrfs_set_stack_inode_generation(&ii, trans->transid);
131 btrfs_set_stack_timespec_nsec(&ii.atime, 0);
132 btrfs_set_stack_timespec_sec(&ii.ctime, now);
133 btrfs_set_stack_timespec_nsec(&ii.ctime, 0);
134 btrfs_set_stack_timespec_sec(&ii.mtime, now);
135 btrfs_set_stack_timespec_nsec(&ii.mtime, 0);
136 btrfs_set_stack_timespec_sec(&ii.otime, 0);
137 btrfs_set_stack_timespec_nsec(&ii.otime, 0);
139 ret = btrfs_insert_inode(trans, root, ino, &ii);
142 warning("root %llu inode %llu recreating inode item, this may "
143 "be incomplete, please check permissions and content after "
144 "the fsck completes.\n", (unsigned long long)root->objectid,
145 (unsigned long long)ino);
150 static int get_highest_inode(struct btrfs_trans_handle *trans,
151 struct btrfs_root *root, struct btrfs_path *path,
154 struct btrfs_key key, found_key;
157 btrfs_init_path(path);
158 key.objectid = BTRFS_LAST_FREE_OBJECTID;
160 key.type = BTRFS_INODE_ITEM_KEY;
161 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
163 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
165 *highest_ino = found_key.objectid;
168 if (*highest_ino >= BTRFS_LAST_FREE_OBJECTID)
170 btrfs_release_path(path);
175 * Link inode to dir 'lost+found'. Increase @ref_count.
177 * Returns 0 means success.
178 * Returns <0 means failure.
180 int link_inode_to_lostfound(struct btrfs_trans_handle *trans,
181 struct btrfs_root *root,
182 struct btrfs_path *path,
183 u64 ino, char *namebuf, u32 name_len,
184 u8 filetype, u64 *ref_count)
186 char *dir_name = "lost+found";
191 btrfs_release_path(path);
192 ret = get_highest_inode(trans, root, path, &lost_found_ino);
197 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
198 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
201 error("failed to create '%s' dir: %s", dir_name, strerror(-ret));
204 ret = btrfs_add_link(trans, root, ino, lost_found_ino,
205 namebuf, name_len, filetype, NULL, 1, 0);
207 * Add ".INO" suffix several times to handle case where
208 * "FILENAME.INO" is already taken by another file.
210 while (ret == -EEXIST) {
212 * Conflicting file name, add ".INO" as suffix * +1 for '.'
214 if (name_len + count_digits(ino) + 1 > BTRFS_NAME_LEN) {
218 snprintf(namebuf + name_len, BTRFS_NAME_LEN - name_len,
220 name_len += count_digits(ino) + 1;
221 ret = btrfs_add_link(trans, root, ino, lost_found_ino, namebuf,
222 name_len, filetype, NULL, 1, 0);
225 error("failed to link the inode %llu to %s dir: %s",
226 ino, dir_name, strerror(-ret));
231 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
232 name_len, namebuf, dir_name);
234 btrfs_release_path(path);
236 error("failed to move file '%.*s' to '%s' dir", name_len,
242 * Extra (optional) check for dev_item size to report possbile problem on a new
245 void check_dev_size_alignment(u64 devid, u64 total_bytes, u32 sectorsize)
247 if (!IS_ALIGNED(total_bytes, sectorsize)) {
249 "unaligned total_bytes detected for devid %llu, have %llu should be aligned to %u",
250 devid, total_bytes, sectorsize);
252 "this is OK for older kernel, but may cause kernel warning for newer kernels");
253 warning("this can be fixed by 'btrfs rescue fix-device-size'");
257 void reada_walk_down(struct btrfs_root *root, struct extent_buffer *node,
260 struct btrfs_fs_info *fs_info = root->fs_info;
267 level = btrfs_header_level(node);
271 nritems = btrfs_header_nritems(node);
272 for (i = slot; i < nritems; i++) {
273 bytenr = btrfs_node_blockptr(node, i);
274 ptr_gen = btrfs_node_ptr_generation(node, i);
275 readahead_tree_block(fs_info, bytenr, ptr_gen);
280 * Check the child node/leaf by the following condition:
281 * 1. the first item key of the node/leaf should be the same with the one
283 * 2. block in parent node should match the child node/leaf.
284 * 3. generation of parent node and child's header should be consistent.
286 * Or the child node/leaf pointed by the key in parent is not valid.
288 * We hope to check leaf owner too, but since subvol may share leaves,
289 * which makes leaf owner check not so strong, key check should be
290 * sufficient enough for that case.
292 int check_child_node(struct extent_buffer *parent, int slot,
293 struct extent_buffer *child)
295 struct btrfs_key parent_key;
296 struct btrfs_key child_key;
299 btrfs_node_key_to_cpu(parent, &parent_key, slot);
300 if (btrfs_header_level(child) == 0)
301 btrfs_item_key_to_cpu(child, &child_key, 0);
303 btrfs_node_key_to_cpu(child, &child_key, 0);
305 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
308 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
309 parent_key.objectid, parent_key.type, parent_key.offset,
310 child_key.objectid, child_key.type, child_key.offset);
312 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
314 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
315 btrfs_node_blockptr(parent, slot),
316 btrfs_header_bytenr(child));
318 if (btrfs_node_ptr_generation(parent, slot) !=
319 btrfs_header_generation(child)) {
321 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
322 btrfs_header_generation(child),
323 btrfs_node_ptr_generation(parent, slot));
328 void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
330 struct btrfs_block_group_cache *cache;
335 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
336 &start, &end, EXTENT_DIRTY);
339 clear_extent_dirty(&fs_info->free_space_cache, start, end);
344 cache = btrfs_lookup_first_block_group(fs_info, start);
349 start = cache->key.objectid + cache->key.offset;