1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
9 #include "transaction.h"
10 #include "accessors.h"
14 * insert a name into a directory, doing overflow properly if there is a hash
15 * collision. data_size indicates how big the item inserted should be. On
16 * success a struct btrfs_dir_item pointer is returned, otherwise it is
19 * The name is not copied into the dir item, you have to do that yourself.
21 static struct btrfs_dir_item
*insert_with_overflow(struct btrfs_trans_handle
23 struct btrfs_root
*root
,
24 struct btrfs_path
*path
,
25 const struct btrfs_key
*cpu_key
,
32 struct extent_buffer
*leaf
;
34 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
36 struct btrfs_dir_item
*di
;
37 di
= btrfs_match_dir_item_name(path
, name
, name_len
);
39 return ERR_PTR(-EEXIST
);
40 btrfs_extend_item(trans
, path
, data_size
);
44 leaf
= path
->nodes
[0];
45 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
46 ASSERT(data_size
<= btrfs_item_size(leaf
, path
->slots
[0]));
47 ptr
+= btrfs_item_size(leaf
, path
->slots
[0]) - data_size
;
48 return (struct btrfs_dir_item
*)ptr
;
52 * xattrs work a lot like directories, this inserts an xattr item
55 int btrfs_insert_xattr_item(struct btrfs_trans_handle
*trans
,
56 struct btrfs_root
*root
,
57 struct btrfs_path
*path
, u64 objectid
,
58 const char *name
, u16 name_len
,
59 const void *data
, u16 data_len
)
62 struct btrfs_dir_item
*dir_item
;
63 unsigned long name_ptr
, data_ptr
;
64 struct btrfs_key key
, location
;
65 struct btrfs_disk_key disk_key
;
66 struct extent_buffer
*leaf
;
69 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(root
->fs_info
))
72 key
.objectid
= objectid
;
73 key
.type
= BTRFS_XATTR_ITEM_KEY
;
74 key
.offset
= btrfs_name_hash(name
, name_len
);
76 data_size
= sizeof(*dir_item
) + name_len
+ data_len
;
77 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
80 return PTR_ERR(dir_item
);
81 memset(&location
, 0, sizeof(location
));
83 leaf
= path
->nodes
[0];
84 btrfs_cpu_key_to_disk(&disk_key
, &location
);
85 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
86 btrfs_set_dir_flags(leaf
, dir_item
, BTRFS_FT_XATTR
);
87 btrfs_set_dir_name_len(leaf
, dir_item
, name_len
);
88 btrfs_set_dir_transid(leaf
, dir_item
, trans
->transid
);
89 btrfs_set_dir_data_len(leaf
, dir_item
, data_len
);
90 name_ptr
= (unsigned long)(dir_item
+ 1);
91 data_ptr
= (unsigned long)((char *)name_ptr
+ name_len
);
93 write_extent_buffer(leaf
, name
, name_ptr
, name_len
);
94 write_extent_buffer(leaf
, data
, data_ptr
, data_len
);
95 btrfs_mark_buffer_dirty(trans
, path
->nodes
[0]);
101 * insert a directory item in the tree, doing all the magic for
102 * both indexes. 'dir' indicates which objectid to insert it into,
103 * 'location' is the key to stuff into the directory item, 'type' is the
104 * type of the inode we're pointing to, and 'index' is the sequence number
105 * to use for the second index (if one is created).
106 * Will return 0 or -ENOMEM
108 int btrfs_insert_dir_item(struct btrfs_trans_handle
*trans
,
109 const struct fscrypt_str
*name
, struct btrfs_inode
*dir
,
110 const struct btrfs_key
*location
, u8 type
, u64 index
)
114 struct btrfs_root
*root
= dir
->root
;
115 struct btrfs_path
*path
;
116 struct btrfs_dir_item
*dir_item
;
117 struct extent_buffer
*leaf
;
118 unsigned long name_ptr
;
119 struct btrfs_key key
;
120 struct btrfs_disk_key disk_key
;
123 key
.objectid
= btrfs_ino(dir
);
124 key
.type
= BTRFS_DIR_ITEM_KEY
;
125 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
127 path
= btrfs_alloc_path();
131 btrfs_cpu_key_to_disk(&disk_key
, location
);
133 data_size
= sizeof(*dir_item
) + name
->len
;
134 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
135 name
->name
, name
->len
);
136 if (IS_ERR(dir_item
)) {
137 ret
= PTR_ERR(dir_item
);
143 if (IS_ENCRYPTED(&dir
->vfs_inode
))
144 type
|= BTRFS_FT_ENCRYPTED
;
146 leaf
= path
->nodes
[0];
147 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
148 btrfs_set_dir_flags(leaf
, dir_item
, type
);
149 btrfs_set_dir_data_len(leaf
, dir_item
, 0);
150 btrfs_set_dir_name_len(leaf
, dir_item
, name
->len
);
151 btrfs_set_dir_transid(leaf
, dir_item
, trans
->transid
);
152 name_ptr
= (unsigned long)(dir_item
+ 1);
154 write_extent_buffer(leaf
, name
->name
, name_ptr
, name
->len
);
155 btrfs_mark_buffer_dirty(trans
, leaf
);
158 /* FIXME, use some real flag for selecting the extra index */
159 if (root
== root
->fs_info
->tree_root
) {
163 btrfs_release_path(path
);
165 ret2
= btrfs_insert_delayed_dir_index(trans
, name
->name
, name
->len
, dir
,
166 &disk_key
, type
, index
);
168 btrfs_free_path(path
);
176 static struct btrfs_dir_item
*btrfs_lookup_match_dir(
177 struct btrfs_trans_handle
*trans
,
178 struct btrfs_root
*root
, struct btrfs_path
*path
,
179 struct btrfs_key
*key
, const char *name
,
180 int name_len
, int mod
)
182 const int ins_len
= (mod
< 0 ? -1 : 0);
183 const int cow
= (mod
!= 0);
186 ret
= btrfs_search_slot(trans
, root
, key
, path
, ins_len
, cow
);
190 return ERR_PTR(-ENOENT
);
192 return btrfs_match_dir_item_name(path
, name
, name_len
);
196 * Lookup for a directory item by name.
198 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
199 * @root: The root of the target tree.
200 * @path: Path to use for the search.
201 * @dir: The inode number (objectid) of the directory.
202 * @name: The name associated to the directory entry we are looking for.
203 * @name_len: The length of the name.
204 * @mod: Used to indicate if the tree search is meant for a read only
205 * lookup, for a modification lookup or for a deletion lookup, so
206 * its value should be 0, 1 or -1, respectively.
208 * Returns: NULL if the dir item does not exists, an error pointer if an error
209 * happened, or a pointer to a dir item if a dir item exists for the given name.
211 struct btrfs_dir_item
*btrfs_lookup_dir_item(struct btrfs_trans_handle
*trans
,
212 struct btrfs_root
*root
,
213 struct btrfs_path
*path
, u64 dir
,
214 const struct fscrypt_str
*name
,
217 struct btrfs_key key
;
218 struct btrfs_dir_item
*di
;
221 key
.type
= BTRFS_DIR_ITEM_KEY
;
222 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
224 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
->name
,
226 if (IS_ERR(di
) && PTR_ERR(di
) == -ENOENT
)
232 int btrfs_check_dir_item_collision(struct btrfs_root
*root
, u64 dir
,
233 const struct fscrypt_str
*name
)
236 struct btrfs_key key
;
237 struct btrfs_dir_item
*di
;
239 struct extent_buffer
*leaf
;
241 struct btrfs_path
*path
;
243 path
= btrfs_alloc_path();
248 key
.type
= BTRFS_DIR_ITEM_KEY
;
249 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
251 di
= btrfs_lookup_match_dir(NULL
, root
, path
, &key
, name
->name
,
255 /* Nothing found, we're safe */
256 if (ret
== -ENOENT
) {
265 /* we found an item, look for our name in the item */
267 /* our exact name was found */
272 /* See if there is room in the item to insert this name. */
273 data_size
= sizeof(*di
) + name
->len
;
274 leaf
= path
->nodes
[0];
275 slot
= path
->slots
[0];
276 if (data_size
+ btrfs_item_size(leaf
, slot
) +
277 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
280 /* plenty of insertion room */
284 btrfs_free_path(path
);
289 * Lookup for a directory index item by name and index number.
291 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
292 * @root: The root of the target tree.
293 * @path: Path to use for the search.
294 * @dir: The inode number (objectid) of the directory.
295 * @index: The index number.
296 * @name: The name associated to the directory entry we are looking for.
297 * @name_len: The length of the name.
298 * @mod: Used to indicate if the tree search is meant for a read only
299 * lookup, for a modification lookup or for a deletion lookup, so
300 * its value should be 0, 1 or -1, respectively.
302 * Returns: NULL if the dir index item does not exists, an error pointer if an
303 * error happened, or a pointer to a dir item if the dir index item exists and
304 * matches the criteria (name and index number).
306 struct btrfs_dir_item
*
307 btrfs_lookup_dir_index_item(struct btrfs_trans_handle
*trans
,
308 struct btrfs_root
*root
,
309 struct btrfs_path
*path
, u64 dir
,
310 u64 index
, const struct fscrypt_str
*name
, int mod
)
312 struct btrfs_dir_item
*di
;
313 struct btrfs_key key
;
316 key
.type
= BTRFS_DIR_INDEX_KEY
;
319 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
->name
,
321 if (di
== ERR_PTR(-ENOENT
))
327 struct btrfs_dir_item
*
328 btrfs_search_dir_index_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
329 u64 dirid
, const struct fscrypt_str
*name
)
331 struct btrfs_dir_item
*di
;
332 struct btrfs_key key
;
335 key
.objectid
= dirid
;
336 key
.type
= BTRFS_DIR_INDEX_KEY
;
339 btrfs_for_each_slot(root
, &key
, &key
, path
, ret
) {
340 if (key
.objectid
!= dirid
|| key
.type
!= BTRFS_DIR_INDEX_KEY
)
343 di
= btrfs_match_dir_item_name(path
, name
->name
, name
->len
);
347 /* Adjust return code if the key was not found in the next leaf. */
354 struct btrfs_dir_item
*btrfs_lookup_xattr(struct btrfs_trans_handle
*trans
,
355 struct btrfs_root
*root
,
356 struct btrfs_path
*path
, u64 dir
,
357 const char *name
, u16 name_len
,
360 struct btrfs_key key
;
361 struct btrfs_dir_item
*di
;
364 key
.type
= BTRFS_XATTR_ITEM_KEY
;
365 key
.offset
= btrfs_name_hash(name
, name_len
);
367 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
, name_len
, mod
);
368 if (IS_ERR(di
) && PTR_ERR(di
) == -ENOENT
)
375 * helper function to look at the directory item pointed to by 'path'
376 * this walks through all the entries in a dir item and finds one
377 * for a specific name.
379 struct btrfs_dir_item
*btrfs_match_dir_item_name(const struct btrfs_path
*path
,
380 const char *name
, int name_len
)
382 struct btrfs_dir_item
*dir_item
;
383 unsigned long name_ptr
;
387 struct extent_buffer
*leaf
;
389 leaf
= path
->nodes
[0];
390 dir_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dir_item
);
392 total_len
= btrfs_item_size(leaf
, path
->slots
[0]);
393 while (cur
< total_len
) {
394 this_len
= sizeof(*dir_item
) +
395 btrfs_dir_name_len(leaf
, dir_item
) +
396 btrfs_dir_data_len(leaf
, dir_item
);
397 name_ptr
= (unsigned long)(dir_item
+ 1);
399 if (btrfs_dir_name_len(leaf
, dir_item
) == name_len
&&
400 memcmp_extent_buffer(leaf
, name
, name_ptr
, name_len
) == 0)
404 dir_item
= (struct btrfs_dir_item
*)((char *)dir_item
+
411 * given a pointer into a directory item, delete it. This
412 * handles items that have more than one entry in them.
414 int btrfs_delete_one_dir_name(struct btrfs_trans_handle
*trans
,
415 struct btrfs_root
*root
,
416 struct btrfs_path
*path
,
417 const struct btrfs_dir_item
*di
)
420 struct extent_buffer
*leaf
;
425 leaf
= path
->nodes
[0];
426 sub_item_len
= sizeof(*di
) + btrfs_dir_name_len(leaf
, di
) +
427 btrfs_dir_data_len(leaf
, di
);
428 item_len
= btrfs_item_size(leaf
, path
->slots
[0]);
429 if (sub_item_len
== item_len
) {
430 ret
= btrfs_del_item(trans
, root
, path
);
433 unsigned long ptr
= (unsigned long)di
;
436 start
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
437 memmove_extent_buffer(leaf
, ptr
, ptr
+ sub_item_len
,
438 item_len
- (ptr
+ sub_item_len
- start
));
439 btrfs_truncate_item(trans
, path
, item_len
- sub_item_len
, 1);