Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 CM |
4 | */ |
5 | ||
1e1d2701 | 6 | #include "ctree.h" |
26c2c454 | 7 | #include "inode-item.h" |
1e1d2701 | 8 | #include "disk-io.h" |
e089f05c | 9 | #include "transaction.h" |
727011e0 | 10 | #include "print-tree.h" |
1e1d2701 | 11 | |
9bb8407f NB |
12 | struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf, |
13 | int slot, const char *name, | |
14 | int name_len) | |
3954401f | 15 | { |
3954401f CM |
16 | struct btrfs_inode_ref *ref; |
17 | unsigned long ptr; | |
18 | unsigned long name_ptr; | |
19 | u32 item_size; | |
20 | u32 cur_offset = 0; | |
21 | int len; | |
22 | ||
3212fa14 | 23 | item_size = btrfs_item_size(leaf, slot); |
1f250e92 | 24 | ptr = btrfs_item_ptr_offset(leaf, slot); |
3954401f CM |
25 | while (cur_offset < item_size) { |
26 | ref = (struct btrfs_inode_ref *)(ptr + cur_offset); | |
27 | len = btrfs_inode_ref_name_len(leaf, ref); | |
28 | name_ptr = (unsigned long)(ref + 1); | |
29 | cur_offset += len + sizeof(*ref); | |
30 | if (len != name_len) | |
31 | continue; | |
9bb8407f NB |
32 | if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
33 | return ref; | |
3954401f | 34 | } |
9bb8407f | 35 | return NULL; |
3954401f CM |
36 | } |
37 | ||
6ff49c6a NB |
38 | struct btrfs_inode_extref *btrfs_find_name_in_ext_backref( |
39 | struct extent_buffer *leaf, int slot, u64 ref_objectid, | |
40 | const char *name, int name_len) | |
f186373f | 41 | { |
f186373f MF |
42 | struct btrfs_inode_extref *extref; |
43 | unsigned long ptr; | |
44 | unsigned long name_ptr; | |
45 | u32 item_size; | |
46 | u32 cur_offset = 0; | |
47 | int ref_name_len; | |
48 | ||
3212fa14 | 49 | item_size = btrfs_item_size(leaf, slot); |
1f250e92 | 50 | ptr = btrfs_item_ptr_offset(leaf, slot); |
f186373f MF |
51 | |
52 | /* | |
53 | * Search all extended backrefs in this item. We're only | |
54 | * looking through any collisions so most of the time this is | |
55 | * just going to compare against one buffer. If all is well, | |
56 | * we'll return success and the inode ref object. | |
57 | */ | |
58 | while (cur_offset < item_size) { | |
59 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); | |
60 | name_ptr = (unsigned long)(&extref->name); | |
61 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); | |
62 | ||
63 | if (ref_name_len == name_len && | |
64 | btrfs_inode_extref_parent(leaf, extref) == ref_objectid && | |
6ff49c6a NB |
65 | (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)) |
66 | return extref; | |
f186373f MF |
67 | |
68 | cur_offset += ref_name_len + sizeof(*extref); | |
69 | } | |
6ff49c6a | 70 | return NULL; |
f186373f MF |
71 | } |
72 | ||
f186373f MF |
73 | /* Returns NULL if no extref found */ |
74 | struct btrfs_inode_extref * | |
75 | btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans, | |
76 | struct btrfs_root *root, | |
77 | struct btrfs_path *path, | |
78 | const char *name, int name_len, | |
79 | u64 inode_objectid, u64 ref_objectid, int ins_len, | |
80 | int cow) | |
81 | { | |
82 | int ret; | |
83 | struct btrfs_key key; | |
f186373f MF |
84 | |
85 | key.objectid = inode_objectid; | |
86 | key.type = BTRFS_INODE_EXTREF_KEY; | |
87 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); | |
88 | ||
89 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); | |
90 | if (ret < 0) | |
91 | return ERR_PTR(ret); | |
92 | if (ret > 0) | |
93 | return NULL; | |
6ff49c6a NB |
94 | return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], |
95 | ref_objectid, name, name_len); | |
96 | ||
f186373f MF |
97 | } |
98 | ||
48a3b636 ES |
99 | static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, |
100 | struct btrfs_root *root, | |
101 | const char *name, int name_len, | |
102 | u64 inode_objectid, u64 ref_objectid, | |
103 | u64 *index) | |
f186373f MF |
104 | { |
105 | struct btrfs_path *path; | |
106 | struct btrfs_key key; | |
107 | struct btrfs_inode_extref *extref; | |
108 | struct extent_buffer *leaf; | |
109 | int ret; | |
110 | int del_len = name_len + sizeof(*extref); | |
111 | unsigned long ptr; | |
112 | unsigned long item_start; | |
113 | u32 item_size; | |
114 | ||
115 | key.objectid = inode_objectid; | |
962a298f | 116 | key.type = BTRFS_INODE_EXTREF_KEY; |
f186373f MF |
117 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); |
118 | ||
119 | path = btrfs_alloc_path(); | |
120 | if (!path) | |
121 | return -ENOMEM; | |
122 | ||
f186373f MF |
123 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
124 | if (ret > 0) | |
125 | ret = -ENOENT; | |
126 | if (ret < 0) | |
127 | goto out; | |
128 | ||
129 | /* | |
130 | * Sanity check - did we find the right item for this name? | |
131 | * This should always succeed so error here will make the FS | |
132 | * readonly. | |
133 | */ | |
6ff49c6a NB |
134 | extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], |
135 | ref_objectid, name, name_len); | |
136 | if (!extref) { | |
34d97007 | 137 | btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL); |
f186373f MF |
138 | ret = -EROFS; |
139 | goto out; | |
140 | } | |
141 | ||
142 | leaf = path->nodes[0]; | |
3212fa14 | 143 | item_size = btrfs_item_size(leaf, path->slots[0]); |
f186373f MF |
144 | if (index) |
145 | *index = btrfs_inode_extref_index(leaf, extref); | |
146 | ||
147 | if (del_len == item_size) { | |
148 | /* | |
149 | * Common case only one ref in the item, remove the | |
150 | * whole item. | |
151 | */ | |
152 | ret = btrfs_del_item(trans, root, path); | |
153 | goto out; | |
154 | } | |
155 | ||
156 | ptr = (unsigned long)extref; | |
157 | item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
158 | ||
159 | memmove_extent_buffer(leaf, ptr, ptr + del_len, | |
160 | item_size - (ptr + del_len - item_start)); | |
161 | ||
78ac4f9e | 162 | btrfs_truncate_item(path, item_size - del_len, 1); |
f186373f MF |
163 | |
164 | out: | |
165 | btrfs_free_path(path); | |
166 | ||
167 | return ret; | |
168 | } | |
169 | ||
170 | int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, | |
171 | struct btrfs_root *root, | |
172 | const char *name, int name_len, | |
173 | u64 inode_objectid, u64 ref_objectid, u64 *index) | |
3954401f CM |
174 | { |
175 | struct btrfs_path *path; | |
176 | struct btrfs_key key; | |
177 | struct btrfs_inode_ref *ref; | |
178 | struct extent_buffer *leaf; | |
179 | unsigned long ptr; | |
180 | unsigned long item_start; | |
181 | u32 item_size; | |
182 | u32 sub_item_len; | |
183 | int ret; | |
f186373f | 184 | int search_ext_refs = 0; |
3954401f CM |
185 | int del_len = name_len + sizeof(*ref); |
186 | ||
187 | key.objectid = inode_objectid; | |
188 | key.offset = ref_objectid; | |
962a298f | 189 | key.type = BTRFS_INODE_REF_KEY; |
3954401f CM |
190 | |
191 | path = btrfs_alloc_path(); | |
192 | if (!path) | |
193 | return -ENOMEM; | |
194 | ||
195 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
196 | if (ret > 0) { | |
197 | ret = -ENOENT; | |
f186373f | 198 | search_ext_refs = 1; |
3954401f CM |
199 | goto out; |
200 | } else if (ret < 0) { | |
201 | goto out; | |
202 | } | |
9bb8407f NB |
203 | |
204 | ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name, | |
205 | name_len); | |
206 | if (!ref) { | |
3954401f | 207 | ret = -ENOENT; |
f186373f | 208 | search_ext_refs = 1; |
3954401f CM |
209 | goto out; |
210 | } | |
211 | leaf = path->nodes[0]; | |
3212fa14 | 212 | item_size = btrfs_item_size(leaf, path->slots[0]); |
aec7477b JB |
213 | |
214 | if (index) | |
215 | *index = btrfs_inode_ref_index(leaf, ref); | |
216 | ||
3954401f CM |
217 | if (del_len == item_size) { |
218 | ret = btrfs_del_item(trans, root, path); | |
219 | goto out; | |
220 | } | |
221 | ptr = (unsigned long)ref; | |
222 | sub_item_len = name_len + sizeof(*ref); | |
223 | item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
224 | memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, | |
225 | item_size - (ptr + sub_item_len - item_start)); | |
78ac4f9e | 226 | btrfs_truncate_item(path, item_size - sub_item_len, 1); |
f186373f MF |
227 | out: |
228 | btrfs_free_path(path); | |
229 | ||
230 | if (search_ext_refs) { | |
231 | /* | |
232 | * No refs were found, or we could not find the | |
233 | * name in our ref array. Find and remove the extended | |
234 | * inode ref then. | |
235 | */ | |
236 | return btrfs_del_inode_extref(trans, root, name, name_len, | |
237 | inode_objectid, ref_objectid, index); | |
238 | } | |
239 | ||
240 | return ret; | |
241 | } | |
242 | ||
243 | /* | |
244 | * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree. | |
245 | * | |
246 | * The caller must have checked against BTRFS_LINK_MAX already. | |
247 | */ | |
248 | static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, | |
249 | struct btrfs_root *root, | |
250 | const char *name, int name_len, | |
251 | u64 inode_objectid, u64 ref_objectid, u64 index) | |
252 | { | |
253 | struct btrfs_inode_extref *extref; | |
254 | int ret; | |
255 | int ins_len = name_len + sizeof(*extref); | |
256 | unsigned long ptr; | |
257 | struct btrfs_path *path; | |
258 | struct btrfs_key key; | |
259 | struct extent_buffer *leaf; | |
f186373f MF |
260 | |
261 | key.objectid = inode_objectid; | |
262 | key.type = BTRFS_INODE_EXTREF_KEY; | |
263 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); | |
264 | ||
265 | path = btrfs_alloc_path(); | |
266 | if (!path) | |
267 | return -ENOMEM; | |
268 | ||
f186373f MF |
269 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
270 | ins_len); | |
271 | if (ret == -EEXIST) { | |
1f250e92 FM |
272 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
273 | path->slots[0], | |
274 | ref_objectid, | |
6ff49c6a | 275 | name, name_len)) |
f186373f MF |
276 | goto out; |
277 | ||
c71dd880 | 278 | btrfs_extend_item(path, ins_len); |
f186373f MF |
279 | ret = 0; |
280 | } | |
281 | if (ret < 0) | |
282 | goto out; | |
283 | ||
284 | leaf = path->nodes[0]; | |
f186373f | 285 | ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char); |
3212fa14 | 286 | ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len; |
f186373f MF |
287 | extref = (struct btrfs_inode_extref *)ptr; |
288 | ||
289 | btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len); | |
290 | btrfs_set_inode_extref_index(path->nodes[0], extref, index); | |
291 | btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid); | |
292 | ||
293 | ptr = (unsigned long)&extref->name; | |
294 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
295 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
296 | ||
3954401f CM |
297 | out: |
298 | btrfs_free_path(path); | |
299 | return ret; | |
300 | } | |
301 | ||
79787eaa | 302 | /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */ |
3954401f CM |
303 | int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, |
304 | struct btrfs_root *root, | |
305 | const char *name, int name_len, | |
aec7477b | 306 | u64 inode_objectid, u64 ref_objectid, u64 index) |
3954401f | 307 | { |
0b246afa | 308 | struct btrfs_fs_info *fs_info = root->fs_info; |
3954401f CM |
309 | struct btrfs_path *path; |
310 | struct btrfs_key key; | |
311 | struct btrfs_inode_ref *ref; | |
312 | unsigned long ptr; | |
313 | int ret; | |
314 | int ins_len = name_len + sizeof(*ref); | |
315 | ||
316 | key.objectid = inode_objectid; | |
317 | key.offset = ref_objectid; | |
962a298f | 318 | key.type = BTRFS_INODE_REF_KEY; |
3954401f CM |
319 | |
320 | path = btrfs_alloc_path(); | |
321 | if (!path) | |
322 | return -ENOMEM; | |
323 | ||
df8d116f | 324 | path->skip_release_on_error = 1; |
3954401f CM |
325 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
326 | ins_len); | |
327 | if (ret == -EEXIST) { | |
328 | u32 old_size; | |
9bb8407f NB |
329 | ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], |
330 | name, name_len); | |
331 | if (ref) | |
3954401f CM |
332 | goto out; |
333 | ||
3212fa14 | 334 | old_size = btrfs_item_size(path->nodes[0], path->slots[0]); |
c71dd880 | 335 | btrfs_extend_item(path, ins_len); |
3954401f CM |
336 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
337 | struct btrfs_inode_ref); | |
338 | ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size); | |
339 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
aec7477b | 340 | btrfs_set_inode_ref_index(path->nodes[0], ref, index); |
3954401f CM |
341 | ptr = (unsigned long)(ref + 1); |
342 | ret = 0; | |
343 | } else if (ret < 0) { | |
df8d116f | 344 | if (ret == -EOVERFLOW) { |
1f250e92 FM |
345 | if (btrfs_find_name_in_backref(path->nodes[0], |
346 | path->slots[0], | |
9bb8407f | 347 | name, name_len)) |
df8d116f FM |
348 | ret = -EEXIST; |
349 | else | |
350 | ret = -EMLINK; | |
351 | } | |
3954401f CM |
352 | goto out; |
353 | } else { | |
354 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
355 | struct btrfs_inode_ref); | |
356 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
aec7477b | 357 | btrfs_set_inode_ref_index(path->nodes[0], ref, index); |
3954401f CM |
358 | ptr = (unsigned long)(ref + 1); |
359 | } | |
360 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
361 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
362 | ||
363 | out: | |
364 | btrfs_free_path(path); | |
f186373f MF |
365 | |
366 | if (ret == -EMLINK) { | |
0b246afa | 367 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
f186373f MF |
368 | /* We ran out of space in the ref array. Need to |
369 | * add an extended ref. */ | |
370 | if (btrfs_super_incompat_flags(disk_super) | |
371 | & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF) | |
372 | ret = btrfs_insert_inode_extref(trans, root, name, | |
373 | name_len, | |
374 | inode_objectid, | |
375 | ref_objectid, index); | |
376 | } | |
377 | ||
3954401f CM |
378 | return ret; |
379 | } | |
380 | ||
5f39d397 CM |
381 | int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, |
382 | struct btrfs_root *root, | |
383 | struct btrfs_path *path, u64 objectid) | |
1e1d2701 | 384 | { |
1e1d2701 CM |
385 | struct btrfs_key key; |
386 | int ret; | |
387 | key.objectid = objectid; | |
962a298f | 388 | key.type = BTRFS_INODE_ITEM_KEY; |
1e1d2701 CM |
389 | key.offset = 0; |
390 | ||
5f39d397 CM |
391 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
392 | sizeof(struct btrfs_inode_item)); | |
1e1d2701 CM |
393 | return ret; |
394 | } | |
395 | ||
e089f05c | 396 | int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
d6e4a428 CM |
397 | *root, struct btrfs_path *path, |
398 | struct btrfs_key *location, int mod) | |
1e1d2701 | 399 | { |
1e1d2701 CM |
400 | int ins_len = mod < 0 ? -1 : 0; |
401 | int cow = mod != 0; | |
d6e4a428 CM |
402 | int ret; |
403 | int slot; | |
5f39d397 | 404 | struct extent_buffer *leaf; |
d6e4a428 | 405 | struct btrfs_key found_key; |
1e1d2701 | 406 | |
d6e4a428 | 407 | ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); |
962a298f | 408 | if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY && |
d6e4a428 CM |
409 | location->offset == (u64)-1 && path->slots[0] != 0) { |
410 | slot = path->slots[0] - 1; | |
5f39d397 CM |
411 | leaf = path->nodes[0]; |
412 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
d6e4a428 | 413 | if (found_key.objectid == location->objectid && |
962a298f | 414 | found_key.type == location->type) { |
d6e4a428 CM |
415 | path->slots[0]--; |
416 | return 0; | |
417 | } | |
418 | } | |
419 | return ret; | |
1e1d2701 | 420 | } |
54f03ab1 JB |
421 | |
422 | /* | |
423 | * Remove inode items from a given root. | |
424 | * | |
425 | * @trans: A transaction handle. | |
426 | * @root: The root from which to remove items. | |
427 | * @inode: The inode whose items we want to remove. | |
d9ac19c3 JB |
428 | * @control: The btrfs_truncate_control to control how and what we |
429 | * are truncating. | |
54f03ab1 JB |
430 | * |
431 | * Remove all keys associated with the inode from the given root that have a key | |
432 | * with a type greater than or equals to @min_type. When @min_type has a value of | |
433 | * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value | |
434 | * greater than or equals to @new_size. If a file extent item that starts before | |
435 | * @new_size and ends after it is found, its length is adjusted. | |
436 | * | |
437 | * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is | |
438 | * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block. | |
439 | */ | |
440 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |
441 | struct btrfs_root *root, | |
442 | struct btrfs_inode *inode, | |
d9ac19c3 | 443 | struct btrfs_truncate_control *control) |
54f03ab1 JB |
444 | { |
445 | struct btrfs_fs_info *fs_info = root->fs_info; | |
446 | struct btrfs_path *path; | |
447 | struct extent_buffer *leaf; | |
448 | struct btrfs_file_extent_item *fi; | |
449 | struct btrfs_key key; | |
450 | struct btrfs_key found_key; | |
d9ac19c3 | 451 | u64 new_size = control->new_size; |
54f03ab1 JB |
452 | u64 extent_num_bytes = 0; |
453 | u64 extent_offset = 0; | |
454 | u64 item_end = 0; | |
54f03ab1 | 455 | u32 found_type = (u8)-1; |
54f03ab1 JB |
456 | int del_item; |
457 | int pending_del_nr = 0; | |
458 | int pending_del_slot = 0; | |
459 | int extent_type = -1; | |
460 | int ret; | |
54f03ab1 JB |
461 | u64 bytes_deleted = 0; |
462 | bool be_nice = false; | |
463 | bool should_throttle = false; | |
54f03ab1 | 464 | |
d9ac19c3 | 465 | BUG_ON(new_size > 0 && control->min_type != BTRFS_EXTENT_DATA_KEY); |
54f03ab1 | 466 | |
c2ddb612 | 467 | control->last_size = new_size; |
462b728e | 468 | control->sub_bytes = 0; |
c2ddb612 | 469 | |
54f03ab1 | 470 | /* |
275312a0 JB |
471 | * For shareable roots we want to back off from time to time, this turns |
472 | * out to be subvolume roots, reloc roots, and data reloc roots. | |
54f03ab1 | 473 | */ |
275312a0 | 474 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
54f03ab1 JB |
475 | be_nice = true; |
476 | ||
477 | path = btrfs_alloc_path(); | |
478 | if (!path) | |
479 | return -ENOMEM; | |
480 | path->reada = READA_BACK; | |
481 | ||
487e81d2 | 482 | key.objectid = control->ino; |
54f03ab1 JB |
483 | key.offset = (u64)-1; |
484 | key.type = (u8)-1; | |
485 | ||
486 | search_again: | |
487 | /* | |
488 | * With a 16K leaf size and 128MiB extents, you can actually queue up a | |
489 | * huge file in a single leaf. Most of the time that bytes_deleted is | |
490 | * > 0, it will be huge by the time we get here | |
491 | */ | |
492 | if (be_nice && bytes_deleted > SZ_32M && | |
493 | btrfs_should_end_transaction(trans)) { | |
494 | ret = -EAGAIN; | |
495 | goto out; | |
496 | } | |
497 | ||
498 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
499 | if (ret < 0) | |
500 | goto out; | |
501 | ||
502 | if (ret > 0) { | |
503 | ret = 0; | |
504 | /* There are no items in the tree for us to truncate, we're done */ | |
505 | if (path->slots[0] == 0) | |
506 | goto out; | |
507 | path->slots[0]--; | |
508 | } | |
509 | ||
510 | while (1) { | |
7097a941 | 511 | u64 clear_start = 0, clear_len = 0, extent_start = 0; |
54f03ab1 JB |
512 | |
513 | fi = NULL; | |
514 | leaf = path->nodes[0]; | |
515 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
516 | found_type = found_key.type; | |
517 | ||
487e81d2 | 518 | if (found_key.objectid != control->ino) |
54f03ab1 JB |
519 | break; |
520 | ||
d9ac19c3 | 521 | if (found_type < control->min_type) |
54f03ab1 JB |
522 | break; |
523 | ||
524 | item_end = found_key.offset; | |
525 | if (found_type == BTRFS_EXTENT_DATA_KEY) { | |
526 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
527 | struct btrfs_file_extent_item); | |
528 | extent_type = btrfs_file_extent_type(leaf, fi); | |
529 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
530 | item_end += | |
531 | btrfs_file_extent_num_bytes(leaf, fi); | |
532 | ||
533 | trace_btrfs_truncate_show_fi_regular( | |
534 | inode, leaf, fi, found_key.offset); | |
535 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
536 | item_end += btrfs_file_extent_ram_bytes(leaf, fi); | |
537 | ||
538 | trace_btrfs_truncate_show_fi_inline( | |
539 | inode, leaf, fi, path->slots[0], | |
540 | found_key.offset); | |
541 | } | |
542 | item_end--; | |
543 | } | |
d9ac19c3 | 544 | if (found_type > control->min_type) { |
54f03ab1 JB |
545 | del_item = 1; |
546 | } else { | |
547 | if (item_end < new_size) | |
548 | break; | |
549 | if (found_key.offset >= new_size) | |
550 | del_item = 1; | |
551 | else | |
552 | del_item = 0; | |
553 | } | |
7097a941 | 554 | |
54f03ab1 JB |
555 | /* FIXME, shrink the extent if the ref count is only 1 */ |
556 | if (found_type != BTRFS_EXTENT_DATA_KEY) | |
557 | goto delete; | |
558 | ||
d9ac19c3 | 559 | control->extents_found++; |
54f03ab1 JB |
560 | |
561 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
562 | u64 num_dec; | |
563 | ||
564 | clear_start = found_key.offset; | |
565 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); | |
566 | if (!del_item) { | |
567 | u64 orig_num_bytes = | |
568 | btrfs_file_extent_num_bytes(leaf, fi); | |
569 | extent_num_bytes = ALIGN(new_size - | |
570 | found_key.offset, | |
571 | fs_info->sectorsize); | |
572 | clear_start = ALIGN(new_size, fs_info->sectorsize); | |
573 | ||
574 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
575 | extent_num_bytes); | |
576 | num_dec = (orig_num_bytes - extent_num_bytes); | |
462b728e JB |
577 | if (extent_start != 0) |
578 | control->sub_bytes += num_dec; | |
54f03ab1 JB |
579 | btrfs_mark_buffer_dirty(leaf); |
580 | } else { | |
581 | extent_num_bytes = | |
582 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
583 | extent_offset = found_key.offset - | |
584 | btrfs_file_extent_offset(leaf, fi); | |
585 | ||
586 | /* FIXME blocksize != 4096 */ | |
587 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); | |
462b728e JB |
588 | if (extent_start != 0) |
589 | control->sub_bytes += num_dec; | |
54f03ab1 JB |
590 | } |
591 | clear_len = num_dec; | |
592 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
593 | /* | |
594 | * We can't truncate inline items that have had | |
595 | * special encodings | |
596 | */ | |
597 | if (!del_item && | |
598 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
599 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && | |
600 | btrfs_file_extent_compression(leaf, fi) == 0) { | |
601 | u32 size = (u32)(new_size - found_key.offset); | |
602 | ||
603 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
604 | size = btrfs_file_extent_calc_inline_size(size); | |
605 | btrfs_truncate_item(path, size, 1); | |
606 | } else if (!del_item) { | |
607 | /* | |
608 | * We have to bail so the last_size is set to | |
609 | * just before this extent. | |
610 | */ | |
611 | ret = BTRFS_NEED_TRUNCATE_BLOCK; | |
612 | break; | |
613 | } else { | |
614 | /* | |
615 | * Inline extents are special, we just treat | |
616 | * them as a full sector worth in the file | |
617 | * extent tree just for simplicity sake. | |
618 | */ | |
619 | clear_len = fs_info->sectorsize; | |
620 | } | |
621 | ||
462b728e | 622 | control->sub_bytes += item_end + 1 - new_size; |
54f03ab1 JB |
623 | } |
624 | delete: | |
625 | /* | |
655807b8 JB |
626 | * We only want to clear the file extent range if we're |
627 | * modifying the actual inode's mapping, which is just the | |
628 | * normal truncate path. | |
54f03ab1 | 629 | */ |
655807b8 | 630 | if (control->clear_extent_range) { |
54f03ab1 JB |
631 | ret = btrfs_inode_clear_file_extent_range(inode, |
632 | clear_start, clear_len); | |
633 | if (ret) { | |
634 | btrfs_abort_transaction(trans, ret); | |
635 | break; | |
636 | } | |
637 | } | |
638 | ||
639 | if (del_item) | |
c2ddb612 | 640 | control->last_size = found_key.offset; |
54f03ab1 | 641 | else |
c2ddb612 | 642 | control->last_size = new_size; |
54f03ab1 JB |
643 | if (del_item) { |
644 | if (!pending_del_nr) { | |
645 | /* No pending yet, add ourselves */ | |
646 | pending_del_slot = path->slots[0]; | |
647 | pending_del_nr = 1; | |
648 | } else if (pending_del_nr && | |
649 | path->slots[0] + 1 == pending_del_slot) { | |
650 | /* Hop on the pending chunk */ | |
651 | pending_del_nr++; | |
652 | pending_del_slot = path->slots[0]; | |
653 | } else { | |
654 | BUG(); | |
655 | } | |
656 | } else { | |
657 | break; | |
658 | } | |
659 | should_throttle = false; | |
660 | ||
5caa490e | 661 | if (del_item && extent_start != 0 && !control->skip_ref_updates) { |
54f03ab1 JB |
662 | struct btrfs_ref ref = { 0 }; |
663 | ||
664 | bytes_deleted += extent_num_bytes; | |
665 | ||
666 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, | |
667 | extent_start, extent_num_bytes, 0); | |
668 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
487e81d2 | 669 | control->ino, extent_offset, |
54f03ab1 JB |
670 | root->root_key.objectid, false); |
671 | ret = btrfs_free_extent(trans, &ref); | |
672 | if (ret) { | |
673 | btrfs_abort_transaction(trans, ret); | |
674 | break; | |
675 | } | |
676 | if (be_nice) { | |
677 | if (btrfs_should_throttle_delayed_refs(trans)) | |
678 | should_throttle = true; | |
679 | } | |
680 | } | |
681 | ||
682 | if (found_type == BTRFS_INODE_ITEM_KEY) | |
683 | break; | |
684 | ||
685 | if (path->slots[0] == 0 || | |
686 | path->slots[0] != pending_del_slot || | |
687 | should_throttle) { | |
688 | if (pending_del_nr) { | |
689 | ret = btrfs_del_items(trans, root, path, | |
690 | pending_del_slot, | |
691 | pending_del_nr); | |
692 | if (ret) { | |
693 | btrfs_abort_transaction(trans, ret); | |
694 | break; | |
695 | } | |
696 | pending_del_nr = 0; | |
697 | } | |
698 | btrfs_release_path(path); | |
699 | ||
700 | /* | |
701 | * We can generate a lot of delayed refs, so we need to | |
702 | * throttle every once and a while and make sure we're | |
703 | * adding enough space to keep up with the work we are | |
704 | * generating. Since we hold a transaction here we | |
705 | * can't flush, and we don't want to FLUSH_LIMIT because | |
706 | * we could have generated too many delayed refs to | |
707 | * actually allocate, so just bail if we're short and | |
708 | * let the normal reservation dance happen higher up. | |
709 | */ | |
710 | if (should_throttle) { | |
711 | ret = btrfs_delayed_refs_rsv_refill(fs_info, | |
712 | BTRFS_RESERVE_NO_FLUSH); | |
713 | if (ret) { | |
714 | ret = -EAGAIN; | |
715 | break; | |
716 | } | |
717 | } | |
718 | goto search_again; | |
719 | } else { | |
720 | path->slots[0]--; | |
721 | } | |
722 | } | |
723 | out: | |
724 | if (ret >= 0 && pending_del_nr) { | |
725 | int err; | |
726 | ||
727 | err = btrfs_del_items(trans, root, path, pending_del_slot, | |
728 | pending_del_nr); | |
729 | if (err) { | |
730 | btrfs_abort_transaction(trans, err); | |
731 | ret = err; | |
732 | } | |
733 | } | |
c2ddb612 JB |
734 | |
735 | ASSERT(control->last_size >= new_size); | |
736 | if (!ret && control->last_size > new_size) | |
737 | control->last_size = new_size; | |
54f03ab1 JB |
738 | |
739 | btrfs_free_path(path); | |
740 | return ret; | |
741 | } |