2 * Copyright (C) 2014 Fujitsu. All rights reserved.
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.
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.
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.
21 #include "transaction.h"
22 #include "kerncompat.h"
25 * Get the first file extent that covers (part of) the given range
26 * Unlike kernel using extent_map to handle hole even no-hole is enabled,
27 * progs don't have such infrastructure, so caller should do extra care
30 * return 0 for found, and path points to the file extent.
31 * return >0 for not found, and path points to the insert position.
32 * return <0 for error.
34 int btrfs_get_extent(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct btrfs_path *path,
37 u64 ino, u64 offset, u64 len, int ins_len)
40 struct btrfs_key found_key;
41 struct btrfs_file_extent_item *fi_item;
47 key.type = BTRFS_EXTENT_DATA_KEY;
50 ret = btrfs_search_slot(trans, root, &key, path, ins_len,
55 /* Check previous file extent */
56 ret = btrfs_previous_item(root, path, ino,
57 BTRFS_EXTENT_DATA_KEY);
63 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
64 if (found_key.objectid != ino ||
65 found_key.type != BTRFS_EXTENT_DATA_KEY)
68 fi_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
69 struct btrfs_file_extent_item);
70 end = found_key.offset +
71 btrfs_file_extent_ram_bytes(path->nodes[0], fi_item);
73 * existing file extent
86 ret = btrfs_next_item(root, path);
90 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
91 if (found_key.objectid != ino ||
92 found_key.type != BTRFS_EXTENT_DATA_KEY) {
96 if (found_key.offset < offset + len)
98 * existing file extent
106 * existing file extent
114 * To keep the search behavior consistent with search_slot(),
115 * we need to go back to the prev leaf's nritem slot if
116 * we are at the first slot of the leaf.
118 if (path->slots[0] == 0) {
119 ret = btrfs_prev_leaf(root, path);
123 path->slots[0] = btrfs_header_nritems(path->nodes[0]);
133 * Punch hole ranged [offset,len) for the file given by ino and root.
135 * Unlink kernel punch_hole, which will not zero/free existing extent,
136 * instead it will return -EEXIST if there is any extents in the hole
139 int btrfs_punch_hole(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root,
141 u64 ino, u64 offset, u64 len)
143 struct btrfs_path *path;
146 path = btrfs_alloc_path();
150 ret = btrfs_get_extent(NULL, root, path, ino, offset, len, 0);
158 ret = btrfs_insert_file_extent(trans, root, ino, offset, 0, 0, len);
160 btrfs_free_path(path);