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