Merge tag 'net-6.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-block.git] / fs / btrfs / dir-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
9b569ea0 6#include "messages.h"
62e2749e
CM
7#include "ctree.h"
8#include "disk-io.h"
e089f05c 9#include "transaction.h"
07e81dc9 10#include "accessors.h"
f2b39277 11#include "dir-item.h"
62e2749e 12
d352ac68
CM
13/*
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
17 * an ERR_PTR.
18 *
19 * The name is not copied into the dir item, you have to do that yourself.
20 */
35b7e476
CM
21static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
22 *trans,
23 struct btrfs_root *root,
24 struct btrfs_path *path,
25 struct btrfs_key *cpu_key,
e06afa83
CM
26 u32 data_size,
27 const char *name,
28 int name_len)
7fcde0e3 29{
2ff7e61e 30 struct btrfs_fs_info *fs_info = root->fs_info;
7fcde0e3 31 int ret;
7e38180e 32 char *ptr;
5f39d397 33 struct extent_buffer *leaf;
7fcde0e3
CM
34
35 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
7e38180e 36 if (ret == -EEXIST) {
e06afa83 37 struct btrfs_dir_item *di;
2ff7e61e 38 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
e06afa83
CM
39 if (di)
40 return ERR_PTR(-EEXIST);
50564b65 41 btrfs_extend_item(trans, path, data_size);
143bede5 42 } else if (ret < 0)
54aa1f4d 43 return ERR_PTR(ret);
7e38180e 44 WARN_ON(ret > 0);
5f39d397 45 leaf = path->nodes[0];
7e38180e 46 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
3212fa14
JB
47 ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
48 ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
7e38180e 49 return (struct btrfs_dir_item *)ptr;
7fcde0e3
CM
50}
51
d352ac68
CM
52/*
53 * xattrs work a lot like directories, this inserts an xattr item
54 * into the tree
55 */
5103e947 56int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
f34f57a3
YZ
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)
5103e947
JB
61{
62 int ret = 0;
5103e947
JB
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;
68 u32 data_size;
69
b9d04c60
DS
70 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
71 return -ENOSPC;
f34f57a3
YZ
72
73 key.objectid = objectid;
962a298f 74 key.type = BTRFS_XATTR_ITEM_KEY;
df68b8a7 75 key.offset = btrfs_name_hash(name, name_len);
5103e947
JB
76
77 data_size = sizeof(*dir_item) + name_len + data_len;
78 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
79 name, name_len);
fa09200b
JB
80 if (IS_ERR(dir_item))
81 return PTR_ERR(dir_item);
5103e947
JB
82 memset(&location, 0, sizeof(location));
83
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);
94a48aef 87 btrfs_set_dir_flags(leaf, dir_item, BTRFS_FT_XATTR);
5103e947 88 btrfs_set_dir_name_len(leaf, dir_item, name_len);
e02119d5 89 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
5103e947
JB
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);
93
94 write_extent_buffer(leaf, name, name_ptr, name_len);
95 write_extent_buffer(leaf, data, data_ptr, data_len);
50564b65 96 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
5103e947 97
5103e947
JB
98 return ret;
99}
100
d352ac68
CM
101/*
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).
79787eaa 107 * Will return 0 or -ENOMEM
d352ac68 108 */
e43eec81 109int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
6db75318 110 const struct fscrypt_str *name, struct btrfs_inode *dir,
684572df 111 struct btrfs_key *location, u8 type, u64 index)
62e2749e
CM
112{
113 int ret = 0;
e06afa83 114 int ret2 = 0;
684572df 115 struct btrfs_root *root = dir->root;
5caf2a00 116 struct btrfs_path *path;
62e2749e 117 struct btrfs_dir_item *dir_item;
5f39d397
CM
118 struct extent_buffer *leaf;
119 unsigned long name_ptr;
62e2749e 120 struct btrfs_key key;
5f39d397 121 struct btrfs_disk_key disk_key;
62e2749e
CM
122 u32 data_size;
123
8e7611cf 124 key.objectid = btrfs_ino(dir);
962a298f 125 key.type = BTRFS_DIR_ITEM_KEY;
e43eec81 126 key.offset = btrfs_name_hash(name->name, name->len);
b9473439 127
5caf2a00 128 path = btrfs_alloc_path();
16cdcec7
MX
129 if (!path)
130 return -ENOMEM;
b9473439 131
16cdcec7
MX
132 btrfs_cpu_key_to_disk(&disk_key, location);
133
e43eec81 134 data_size = sizeof(*dir_item) + name->len;
e06afa83 135 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
e43eec81 136 name->name, name->len);
7e38180e
CM
137 if (IS_ERR(dir_item)) {
138 ret = PTR_ERR(dir_item);
e06afa83
CM
139 if (ret == -EEXIST)
140 goto second_insert;
c2db1073 141 goto out_free;
7e38180e 142 }
62e2749e 143
94a48aef
OS
144 if (IS_ENCRYPTED(&dir->vfs_inode))
145 type |= BTRFS_FT_ENCRYPTED;
146
5f39d397 147 leaf = path->nodes[0];
5f39d397 148 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
94a48aef 149 btrfs_set_dir_flags(leaf, dir_item, type);
5103e947 150 btrfs_set_dir_data_len(leaf, dir_item, 0);
e43eec81 151 btrfs_set_dir_name_len(leaf, dir_item, name->len);
e02119d5 152 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
5f39d397 153 name_ptr = (unsigned long)(dir_item + 1);
c5739bba 154
e43eec81 155 write_extent_buffer(leaf, name->name, name_ptr, name->len);
50564b65 156 btrfs_mark_buffer_dirty(trans, leaf);
7e38180e 157
e06afa83 158second_insert:
7e38180e
CM
159 /* FIXME, use some real flag for selecting the extra index */
160 if (root == root->fs_info->tree_root) {
161 ret = 0;
c2db1073 162 goto out_free;
7e38180e 163 }
b3b4aa74 164 btrfs_release_path(path);
7e38180e 165
e43eec81 166 ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
4465c8b4 167 &disk_key, type, index);
c2db1073 168out_free:
5caf2a00 169 btrfs_free_path(path);
e06afa83
CM
170 if (ret)
171 return ret;
172 if (ret2)
173 return ret2;
174 return 0;
62e2749e
CM
175}
176
a7d1c5dc
MPS
177static 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)
182{
183 const int ins_len = (mod < 0 ? -1 : 0);
184 const int cow = (mod != 0);
185 int ret;
186
187 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
188 if (ret < 0)
189 return ERR_PTR(ret);
190 if (ret > 0)
191 return ERR_PTR(-ENOENT);
192
193 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
194}
195
d352ac68 196/*
8dcbc261
FM
197 * Lookup for a directory item by name.
198 *
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.
208 *
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.
d352ac68 211 */
7e38180e
CM
212struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
213 struct btrfs_root *root,
214 struct btrfs_path *path, u64 dir,
6db75318 215 const struct fscrypt_str *name,
7e38180e 216 int mod)
62e2749e 217{
62e2749e 218 struct btrfs_key key;
a7d1c5dc 219 struct btrfs_dir_item *di;
62e2749e
CM
220
221 key.objectid = dir;
962a298f 222 key.type = BTRFS_DIR_ITEM_KEY;
e43eec81 223 key.offset = btrfs_name_hash(name->name, name->len);
5f39d397 224
e43eec81
STD
225 di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
226 name->len, mod);
a7d1c5dc 227 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
7e38180e
CM
228 return NULL;
229
a7d1c5dc 230 return di;
62e2749e
CM
231}
232
9c52057c 233int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
6db75318 234 const struct fscrypt_str *name)
9c52057c
CM
235{
236 int ret;
237 struct btrfs_key key;
238 struct btrfs_dir_item *di;
239 int data_size;
240 struct extent_buffer *leaf;
241 int slot;
242 struct btrfs_path *path;
243
9c52057c
CM
244 path = btrfs_alloc_path();
245 if (!path)
246 return -ENOMEM;
247
248 key.objectid = dir;
962a298f 249 key.type = BTRFS_DIR_ITEM_KEY;
e43eec81 250 key.offset = btrfs_name_hash(name->name, name->len);
9c52057c 251
e43eec81
STD
252 di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name,
253 name->len, 0);
a7d1c5dc
MPS
254 if (IS_ERR(di)) {
255 ret = PTR_ERR(di);
256 /* Nothing found, we're safe */
257 if (ret == -ENOENT) {
258 ret = 0;
259 goto out;
260 }
9c52057c 261
a7d1c5dc
MPS
262 if (ret < 0)
263 goto out;
9c52057c
CM
264 }
265
266 /* we found an item, look for our name in the item */
9c52057c
CM
267 if (di) {
268 /* our exact name was found */
269 ret = -EEXIST;
270 goto out;
271 }
272
e43eec81
STD
273 /* See if there is room in the item to insert this name. */
274 data_size = sizeof(*di) + name->len;
9c52057c
CM
275 leaf = path->nodes[0];
276 slot = path->slots[0];
3212fa14 277 if (data_size + btrfs_item_size(leaf, slot) +
da17066c 278 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
9c52057c
CM
279 ret = -EOVERFLOW;
280 } else {
281 /* plenty of insertion room */
282 ret = 0;
283 }
284out:
285 btrfs_free_path(path);
286 return ret;
287}
288
d352ac68 289/*
8dcbc261 290 * Lookup for a directory index item by name and index number.
d352ac68 291 *
8dcbc261
FM
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.
302 *
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).
d352ac68 306 */
7e38180e
CM
307struct btrfs_dir_item *
308btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
309 struct btrfs_root *root,
310 struct btrfs_path *path, u64 dir,
6db75318 311 u64 index, const struct fscrypt_str *name, int mod)
7e38180e 312{
8dcbc261 313 struct btrfs_dir_item *di;
7e38180e 314 struct btrfs_key key;
7e38180e
CM
315
316 key.objectid = dir;
962a298f 317 key.type = BTRFS_DIR_INDEX_KEY;
8dcbc261 318 key.offset = index;
7e38180e 319
e43eec81
STD
320 di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
321 name->len, mod);
8dcbc261
FM
322 if (di == ERR_PTR(-ENOENT))
323 return NULL;
324
325 return di;
7e38180e
CM
326}
327
4df27c4d 328struct btrfs_dir_item *
e43eec81 329btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
6db75318 330 u64 dirid, const struct fscrypt_str *name)
4df27c4d 331{
4df27c4d
YZ
332 struct btrfs_dir_item *di;
333 struct btrfs_key key;
4df27c4d
YZ
334 int ret;
335
336 key.objectid = dirid;
337 key.type = BTRFS_DIR_INDEX_KEY;
338 key.offset = 0;
339
9dcbe16f 340 btrfs_for_each_slot(root, &key, &key, path, ret) {
4df27c4d
YZ
341 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
342 break;
343
2ff7e61e 344 di = btrfs_match_dir_item_name(root->fs_info, path,
e43eec81 345 name->name, name->len);
4df27c4d
YZ
346 if (di)
347 return di;
4df27c4d 348 }
9dcbe16f
GN
349 /* Adjust return code if the key was not found in the next leaf. */
350 if (ret > 0)
351 ret = 0;
352
353 return ERR_PTR(ret);
4df27c4d
YZ
354}
355
5103e947
JB
356struct 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,
360 int mod)
361{
5103e947 362 struct btrfs_key key;
a7d1c5dc 363 struct btrfs_dir_item *di;
5103e947
JB
364
365 key.objectid = dir;
962a298f 366 key.type = BTRFS_XATTR_ITEM_KEY;
df68b8a7 367 key.offset = btrfs_name_hash(name, name_len);
a7d1c5dc
MPS
368
369 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
370 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
5103e947
JB
371 return NULL;
372
a7d1c5dc 373 return di;
5103e947
JB
374}
375
d352ac68
CM
376/*
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.
380 */
2ff7e61e 381struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
5f5bc6b1
FM
382 struct btrfs_path *path,
383 const char *name, int name_len)
62e2749e 384{
62e2749e 385 struct btrfs_dir_item *dir_item;
5f39d397 386 unsigned long name_ptr;
7e38180e
CM
387 u32 total_len;
388 u32 cur = 0;
389 u32 this_len;
5f39d397 390 struct extent_buffer *leaf;
a8a2ee0c 391
5f39d397 392 leaf = path->nodes[0];
7e38180e 393 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
22a94d44 394
3212fa14 395 total_len = btrfs_item_size(leaf, path->slots[0]);
d397712b 396 while (cur < total_len) {
5f39d397 397 this_len = sizeof(*dir_item) +
5103e947
JB
398 btrfs_dir_name_len(leaf, dir_item) +
399 btrfs_dir_data_len(leaf, dir_item);
5f39d397 400 name_ptr = (unsigned long)(dir_item + 1);
7e38180e 401
5f39d397
CM
402 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
403 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
7e38180e
CM
404 return dir_item;
405
406 cur += this_len;
407 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
408 this_len);
409 }
410 return NULL;
62e2749e 411}
7e38180e 412
d352ac68
CM
413/*
414 * given a pointer into a directory item, delete it. This
415 * handles items that have more than one entry in them.
416 */
7e38180e
CM
417int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
418 struct btrfs_root *root,
419 struct btrfs_path *path,
420 struct btrfs_dir_item *di)
421{
422
5f39d397 423 struct extent_buffer *leaf;
7e38180e
CM
424 u32 sub_item_len;
425 u32 item_len;
54aa1f4d 426 int ret = 0;
7e38180e 427
5f39d397 428 leaf = path->nodes[0];
5103e947
JB
429 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
430 btrfs_dir_data_len(leaf, di);
3212fa14 431 item_len = btrfs_item_size(leaf, path->slots[0]);
5f39d397 432 if (sub_item_len == item_len) {
7e38180e 433 ret = btrfs_del_item(trans, root, path);
7e38180e 434 } else {
5f39d397
CM
435 /* MARKER */
436 unsigned long ptr = (unsigned long)di;
437 unsigned long start;
438
439 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
440 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
7e38180e 441 item_len - (ptr + sub_item_len - start));
50564b65 442 btrfs_truncate_item(trans, path, item_len - sub_item_len, 1);
7e38180e 443 }
411fc6bc 444 return ret;
7e38180e 445}