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
,
30 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
33 struct extent_buffer
*leaf
;
35 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
37 struct btrfs_dir_item
*di
;
38 di
= btrfs_match_dir_item_name(fs_info
, path
, name
, name_len
);
40 return ERR_PTR(-EEXIST
);
41 btrfs_extend_item(trans
, path
, data_size
);
45 leaf
= path
->nodes
[0];
46 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
47 ASSERT(data_size
<= btrfs_item_size(leaf
, path
->slots
[0]));
48 ptr
+= btrfs_item_size(leaf
, path
->slots
[0]) - data_size
;
49 return (struct btrfs_dir_item
*)ptr
;
53 * xattrs work a lot like directories, this inserts an xattr item
56 int btrfs_insert_xattr_item(struct btrfs_trans_handle
*trans
,
57 struct btrfs_root
*root
,
58 struct btrfs_path
*path
, u64 objectid
,
59 const char *name
, u16 name_len
,
60 const void *data
, u16 data_len
)
63 struct btrfs_dir_item
*dir_item
;
64 unsigned long name_ptr
, data_ptr
;
65 struct btrfs_key key
, location
;
66 struct btrfs_disk_key disk_key
;
67 struct extent_buffer
*leaf
;
70 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(root
->fs_info
))
73 key
.objectid
= objectid
;
74 key
.type
= BTRFS_XATTR_ITEM_KEY
;
75 key
.offset
= btrfs_name_hash(name
, name_len
);
77 data_size
= sizeof(*dir_item
) + name_len
+ data_len
;
78 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
81 return PTR_ERR(dir_item
);
82 memset(&location
, 0, sizeof(location
));
84 leaf
= path
->nodes
[0];
85 btrfs_cpu_key_to_disk(&disk_key
, &location
);
86 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
87 btrfs_set_dir_flags(leaf
, dir_item
, BTRFS_FT_XATTR
);
88 btrfs_set_dir_name_len(leaf
, dir_item
, name_len
);
89 btrfs_set_dir_transid(leaf
, dir_item
, trans
->transid
);
90 btrfs_set_dir_data_len(leaf
, dir_item
, data_len
);
91 name_ptr
= (unsigned long)(dir_item
+ 1);
92 data_ptr
= (unsigned long)((char *)name_ptr
+ name_len
);
94 write_extent_buffer(leaf
, name
, name_ptr
, name_len
);
95 write_extent_buffer(leaf
, data
, data_ptr
, data_len
);
96 btrfs_mark_buffer_dirty(trans
, path
->nodes
[0]);
102 * insert a directory item in the tree, doing all the magic for
103 * both indexes. 'dir' indicates which objectid to insert it into,
104 * 'location' is the key to stuff into the directory item, 'type' is the
105 * type of the inode we're pointing to, and 'index' is the sequence number
106 * to use for the second index (if one is created).
107 * Will return 0 or -ENOMEM
109 int btrfs_insert_dir_item(struct btrfs_trans_handle
*trans
,
110 const struct fscrypt_str
*name
, struct btrfs_inode
*dir
,
111 const struct btrfs_key
*location
, u8 type
, u64 index
)
115 struct btrfs_root
*root
= dir
->root
;
116 struct btrfs_path
*path
;
117 struct btrfs_dir_item
*dir_item
;
118 struct extent_buffer
*leaf
;
119 unsigned long name_ptr
;
120 struct btrfs_key key
;
121 struct btrfs_disk_key disk_key
;
124 key
.objectid
= btrfs_ino(dir
);
125 key
.type
= BTRFS_DIR_ITEM_KEY
;
126 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
128 path
= btrfs_alloc_path();
132 btrfs_cpu_key_to_disk(&disk_key
, location
);
134 data_size
= sizeof(*dir_item
) + name
->len
;
135 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
136 name
->name
, name
->len
);
137 if (IS_ERR(dir_item
)) {
138 ret
= PTR_ERR(dir_item
);
144 if (IS_ENCRYPTED(&dir
->vfs_inode
))
145 type
|= BTRFS_FT_ENCRYPTED
;
147 leaf
= path
->nodes
[0];
148 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
149 btrfs_set_dir_flags(leaf
, dir_item
, type
);
150 btrfs_set_dir_data_len(leaf
, dir_item
, 0);
151 btrfs_set_dir_name_len(leaf
, dir_item
, name
->len
);
152 btrfs_set_dir_transid(leaf
, dir_item
, trans
->transid
);
153 name_ptr
= (unsigned long)(dir_item
+ 1);
155 write_extent_buffer(leaf
, name
->name
, name_ptr
, name
->len
);
156 btrfs_mark_buffer_dirty(trans
, leaf
);
159 /* FIXME, use some real flag for selecting the extra index */
160 if (root
== root
->fs_info
->tree_root
) {
164 btrfs_release_path(path
);
166 ret2
= btrfs_insert_delayed_dir_index(trans
, name
->name
, name
->len
, dir
,
167 &disk_key
, type
, index
);
169 btrfs_free_path(path
);
177 static struct btrfs_dir_item
*btrfs_lookup_match_dir(
178 struct btrfs_trans_handle
*trans
,
179 struct btrfs_root
*root
, struct btrfs_path
*path
,
180 struct btrfs_key
*key
, const char *name
,
181 int name_len
, int mod
)
183 const int ins_len
= (mod
< 0 ? -1 : 0);
184 const int cow
= (mod
!= 0);
187 ret
= btrfs_search_slot(trans
, root
, key
, path
, ins_len
, cow
);
191 return ERR_PTR(-ENOENT
);
193 return btrfs_match_dir_item_name(root
->fs_info
, path
, name
, name_len
);
197 * Lookup for a directory item by name.
199 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
200 * @root: The root of the target tree.
201 * @path: Path to use for the search.
202 * @dir: The inode number (objectid) of the directory.
203 * @name: The name associated to the directory entry we are looking for.
204 * @name_len: The length of the name.
205 * @mod: Used to indicate if the tree search is meant for a read only
206 * lookup, for a modification lookup or for a deletion lookup, so
207 * its value should be 0, 1 or -1, respectively.
209 * Returns: NULL if the dir item does not exists, an error pointer if an error
210 * happened, or a pointer to a dir item if a dir item exists for the given name.
212 struct btrfs_dir_item
*btrfs_lookup_dir_item(struct btrfs_trans_handle
*trans
,
213 struct btrfs_root
*root
,
214 struct btrfs_path
*path
, u64 dir
,
215 const struct fscrypt_str
*name
,
218 struct btrfs_key key
;
219 struct btrfs_dir_item
*di
;
222 key
.type
= BTRFS_DIR_ITEM_KEY
;
223 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
225 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
->name
,
227 if (IS_ERR(di
) && PTR_ERR(di
) == -ENOENT
)
233 int btrfs_check_dir_item_collision(struct btrfs_root
*root
, u64 dir
,
234 const struct fscrypt_str
*name
)
237 struct btrfs_key key
;
238 struct btrfs_dir_item
*di
;
240 struct extent_buffer
*leaf
;
242 struct btrfs_path
*path
;
244 path
= btrfs_alloc_path();
249 key
.type
= BTRFS_DIR_ITEM_KEY
;
250 key
.offset
= btrfs_name_hash(name
->name
, name
->len
);
252 di
= btrfs_lookup_match_dir(NULL
, root
, path
, &key
, name
->name
,
256 /* Nothing found, we're safe */
257 if (ret
== -ENOENT
) {
266 /* we found an item, look for our name in the item */
268 /* our exact name was found */
273 /* See if there is room in the item to insert this name. */
274 data_size
= sizeof(*di
) + name
->len
;
275 leaf
= path
->nodes
[0];
276 slot
= path
->slots
[0];
277 if (data_size
+ btrfs_item_size(leaf
, slot
) +
278 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
->fs_info
)) {
281 /* plenty of insertion room */
285 btrfs_free_path(path
);
290 * Lookup for a directory index item by name and index number.
292 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
293 * @root: The root of the target tree.
294 * @path: Path to use for the search.
295 * @dir: The inode number (objectid) of the directory.
296 * @index: The index number.
297 * @name: The name associated to the directory entry we are looking for.
298 * @name_len: The length of the name.
299 * @mod: Used to indicate if the tree search is meant for a read only
300 * lookup, for a modification lookup or for a deletion lookup, so
301 * its value should be 0, 1 or -1, respectively.
303 * Returns: NULL if the dir index item does not exists, an error pointer if an
304 * error happened, or a pointer to a dir item if the dir index item exists and
305 * matches the criteria (name and index number).
307 struct btrfs_dir_item
*
308 btrfs_lookup_dir_index_item(struct btrfs_trans_handle
*trans
,
309 struct btrfs_root
*root
,
310 struct btrfs_path
*path
, u64 dir
,
311 u64 index
, const struct fscrypt_str
*name
, int mod
)
313 struct btrfs_dir_item
*di
;
314 struct btrfs_key key
;
317 key
.type
= BTRFS_DIR_INDEX_KEY
;
320 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
->name
,
322 if (di
== ERR_PTR(-ENOENT
))
328 struct btrfs_dir_item
*
329 btrfs_search_dir_index_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
330 u64 dirid
, const struct fscrypt_str
*name
)
332 struct btrfs_dir_item
*di
;
333 struct btrfs_key key
;
336 key
.objectid
= dirid
;
337 key
.type
= BTRFS_DIR_INDEX_KEY
;
340 btrfs_for_each_slot(root
, &key
, &key
, path
, ret
) {
341 if (key
.objectid
!= dirid
|| key
.type
!= BTRFS_DIR_INDEX_KEY
)
344 di
= btrfs_match_dir_item_name(root
->fs_info
, path
,
345 name
->name
, name
->len
);
349 /* Adjust return code if the key was not found in the next leaf. */
356 struct btrfs_dir_item
*btrfs_lookup_xattr(struct btrfs_trans_handle
*trans
,
357 struct btrfs_root
*root
,
358 struct btrfs_path
*path
, u64 dir
,
359 const char *name
, u16 name_len
,
362 struct btrfs_key key
;
363 struct btrfs_dir_item
*di
;
366 key
.type
= BTRFS_XATTR_ITEM_KEY
;
367 key
.offset
= btrfs_name_hash(name
, name_len
);
369 di
= btrfs_lookup_match_dir(trans
, root
, path
, &key
, name
, name_len
, mod
);
370 if (IS_ERR(di
) && PTR_ERR(di
) == -ENOENT
)
377 * helper function to look at the directory item pointed to by 'path'
378 * this walks through all the entries in a dir item and finds one
379 * for a specific name.
381 struct btrfs_dir_item
*btrfs_match_dir_item_name(struct btrfs_fs_info
*fs_info
,
382 const struct btrfs_path
*path
,
383 const char *name
, int name_len
)
385 struct btrfs_dir_item
*dir_item
;
386 unsigned long name_ptr
;
390 struct extent_buffer
*leaf
;
392 leaf
= path
->nodes
[0];
393 dir_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dir_item
);
395 total_len
= btrfs_item_size(leaf
, path
->slots
[0]);
396 while (cur
< total_len
) {
397 this_len
= sizeof(*dir_item
) +
398 btrfs_dir_name_len(leaf
, dir_item
) +
399 btrfs_dir_data_len(leaf
, dir_item
);
400 name_ptr
= (unsigned long)(dir_item
+ 1);
402 if (btrfs_dir_name_len(leaf
, dir_item
) == name_len
&&
403 memcmp_extent_buffer(leaf
, name
, name_ptr
, name_len
) == 0)
407 dir_item
= (struct btrfs_dir_item
*)((char *)dir_item
+
414 * given a pointer into a directory item, delete it. This
415 * handles items that have more than one entry in them.
417 int btrfs_delete_one_dir_name(struct btrfs_trans_handle
*trans
,
418 struct btrfs_root
*root
,
419 struct btrfs_path
*path
,
420 const struct btrfs_dir_item
*di
)
423 struct extent_buffer
*leaf
;
428 leaf
= path
->nodes
[0];
429 sub_item_len
= sizeof(*di
) + btrfs_dir_name_len(leaf
, di
) +
430 btrfs_dir_data_len(leaf
, di
);
431 item_len
= btrfs_item_size(leaf
, path
->slots
[0]);
432 if (sub_item_len
== item_len
) {
433 ret
= btrfs_del_item(trans
, root
, path
);
436 unsigned long ptr
= (unsigned long)di
;
439 start
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
440 memmove_extent_buffer(leaf
, ptr
, ptr
+ sub_item_len
,
441 item_len
- (ptr
+ sub_item_len
- start
));
442 btrfs_truncate_item(trans
, path
, item_len
- sub_item_len
, 1);