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 | ||
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 |
21 | static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle |
22 | *trans, | |
23 | struct btrfs_root *root, | |
24 | struct btrfs_path *path, | |
2917f741 | 25 | const struct btrfs_key *cpu_key, |
e06afa83 CM |
26 | u32 data_size, |
27 | const char *name, | |
28 | int name_len) | |
7fcde0e3 | 29 | { |
7fcde0e3 | 30 | int ret; |
7e38180e | 31 | char *ptr; |
5f39d397 | 32 | struct extent_buffer *leaf; |
7fcde0e3 CM |
33 | |
34 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); | |
7e38180e | 35 | if (ret == -EEXIST) { |
e06afa83 | 36 | struct btrfs_dir_item *di; |
8c7cd2b6 | 37 | di = btrfs_match_dir_item_name(path, name, name_len); |
e06afa83 CM |
38 | if (di) |
39 | return ERR_PTR(-EEXIST); | |
50564b65 | 40 | btrfs_extend_item(trans, path, data_size); |
143bede5 | 41 | } else if (ret < 0) |
54aa1f4d | 42 | return ERR_PTR(ret); |
7e38180e | 43 | WARN_ON(ret > 0); |
5f39d397 | 44 | leaf = path->nodes[0]; |
7e38180e | 45 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
3212fa14 JB |
46 | ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0])); |
47 | ptr += btrfs_item_size(leaf, path->slots[0]) - data_size; | |
7e38180e | 48 | return (struct btrfs_dir_item *)ptr; |
7fcde0e3 CM |
49 | } |
50 | ||
d352ac68 CM |
51 | /* |
52 | * xattrs work a lot like directories, this inserts an xattr item | |
53 | * into the tree | |
54 | */ | |
5103e947 | 55 | int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, |
f34f57a3 YZ |
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) | |
5103e947 JB |
60 | { |
61 | int ret = 0; | |
5103e947 JB |
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; | |
67 | u32 data_size; | |
68 | ||
b9d04c60 DS |
69 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) |
70 | return -ENOSPC; | |
f34f57a3 YZ |
71 | |
72 | key.objectid = objectid; | |
962a298f | 73 | key.type = BTRFS_XATTR_ITEM_KEY; |
df68b8a7 | 74 | key.offset = btrfs_name_hash(name, name_len); |
5103e947 JB |
75 | |
76 | data_size = sizeof(*dir_item) + name_len + data_len; | |
77 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, | |
78 | name, name_len); | |
fa09200b JB |
79 | if (IS_ERR(dir_item)) |
80 | return PTR_ERR(dir_item); | |
5103e947 JB |
81 | memset(&location, 0, sizeof(location)); |
82 | ||
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); | |
94a48aef | 86 | btrfs_set_dir_flags(leaf, dir_item, BTRFS_FT_XATTR); |
5103e947 | 87 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
e02119d5 | 88 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
5103e947 JB |
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); | |
92 | ||
93 | write_extent_buffer(leaf, name, name_ptr, name_len); | |
94 | write_extent_buffer(leaf, data, data_ptr, data_len); | |
5103e947 | 95 | |
5103e947 JB |
96 | return ret; |
97 | } | |
98 | ||
d352ac68 CM |
99 | /* |
100 | * insert a directory item in the tree, doing all the magic for | |
101 | * both indexes. 'dir' indicates which objectid to insert it into, | |
102 | * 'location' is the key to stuff into the directory item, 'type' is the | |
103 | * type of the inode we're pointing to, and 'index' is the sequence number | |
104 | * to use for the second index (if one is created). | |
79787eaa | 105 | * Will return 0 or -ENOMEM |
d352ac68 | 106 | */ |
e43eec81 | 107 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, |
6db75318 | 108 | const struct fscrypt_str *name, struct btrfs_inode *dir, |
2917f741 | 109 | const struct btrfs_key *location, u8 type, u64 index) |
62e2749e CM |
110 | { |
111 | int ret = 0; | |
e06afa83 | 112 | int ret2 = 0; |
684572df | 113 | struct btrfs_root *root = dir->root; |
5caf2a00 | 114 | struct btrfs_path *path; |
62e2749e | 115 | struct btrfs_dir_item *dir_item; |
5f39d397 CM |
116 | struct extent_buffer *leaf; |
117 | unsigned long name_ptr; | |
62e2749e | 118 | struct btrfs_key key; |
5f39d397 | 119 | struct btrfs_disk_key disk_key; |
62e2749e CM |
120 | u32 data_size; |
121 | ||
8e7611cf | 122 | key.objectid = btrfs_ino(dir); |
962a298f | 123 | key.type = BTRFS_DIR_ITEM_KEY; |
e43eec81 | 124 | key.offset = btrfs_name_hash(name->name, name->len); |
b9473439 | 125 | |
5caf2a00 | 126 | path = btrfs_alloc_path(); |
16cdcec7 MX |
127 | if (!path) |
128 | return -ENOMEM; | |
b9473439 | 129 | |
16cdcec7 MX |
130 | btrfs_cpu_key_to_disk(&disk_key, location); |
131 | ||
e43eec81 | 132 | data_size = sizeof(*dir_item) + name->len; |
e06afa83 | 133 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
e43eec81 | 134 | name->name, name->len); |
7e38180e CM |
135 | if (IS_ERR(dir_item)) { |
136 | ret = PTR_ERR(dir_item); | |
e06afa83 CM |
137 | if (ret == -EEXIST) |
138 | goto second_insert; | |
c2db1073 | 139 | goto out_free; |
7e38180e | 140 | } |
62e2749e | 141 | |
94a48aef OS |
142 | if (IS_ENCRYPTED(&dir->vfs_inode)) |
143 | type |= BTRFS_FT_ENCRYPTED; | |
144 | ||
5f39d397 | 145 | leaf = path->nodes[0]; |
5f39d397 | 146 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
94a48aef | 147 | btrfs_set_dir_flags(leaf, dir_item, type); |
5103e947 | 148 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
e43eec81 | 149 | btrfs_set_dir_name_len(leaf, dir_item, name->len); |
e02119d5 | 150 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
5f39d397 | 151 | name_ptr = (unsigned long)(dir_item + 1); |
c5739bba | 152 | |
e43eec81 | 153 | write_extent_buffer(leaf, name->name, name_ptr, name->len); |
7e38180e | 154 | |
e06afa83 | 155 | second_insert: |
7e38180e CM |
156 | /* FIXME, use some real flag for selecting the extra index */ |
157 | if (root == root->fs_info->tree_root) { | |
158 | ret = 0; | |
c2db1073 | 159 | goto out_free; |
7e38180e | 160 | } |
b3b4aa74 | 161 | btrfs_release_path(path); |
7e38180e | 162 | |
e43eec81 | 163 | ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir, |
4465c8b4 | 164 | &disk_key, type, index); |
c2db1073 | 165 | out_free: |
5caf2a00 | 166 | btrfs_free_path(path); |
e06afa83 CM |
167 | if (ret) |
168 | return ret; | |
169 | if (ret2) | |
170 | return ret2; | |
171 | return 0; | |
62e2749e CM |
172 | } |
173 | ||
a7d1c5dc MPS |
174 | static struct btrfs_dir_item *btrfs_lookup_match_dir( |
175 | struct btrfs_trans_handle *trans, | |
176 | struct btrfs_root *root, struct btrfs_path *path, | |
177 | struct btrfs_key *key, const char *name, | |
178 | int name_len, int mod) | |
179 | { | |
180 | const int ins_len = (mod < 0 ? -1 : 0); | |
181 | const int cow = (mod != 0); | |
182 | int ret; | |
183 | ||
184 | ret = btrfs_search_slot(trans, root, key, path, ins_len, cow); | |
185 | if (ret < 0) | |
186 | return ERR_PTR(ret); | |
187 | if (ret > 0) | |
188 | return ERR_PTR(-ENOENT); | |
189 | ||
8c7cd2b6 | 190 | return btrfs_match_dir_item_name(path, name, name_len); |
a7d1c5dc MPS |
191 | } |
192 | ||
d352ac68 | 193 | /* |
8dcbc261 FM |
194 | * Lookup for a directory item by name. |
195 | * | |
196 | * @trans: The transaction handle to use. Can be NULL if @mod is 0. | |
197 | * @root: The root of the target tree. | |
198 | * @path: Path to use for the search. | |
199 | * @dir: The inode number (objectid) of the directory. | |
200 | * @name: The name associated to the directory entry we are looking for. | |
201 | * @name_len: The length of the name. | |
202 | * @mod: Used to indicate if the tree search is meant for a read only | |
203 | * lookup, for a modification lookup or for a deletion lookup, so | |
204 | * its value should be 0, 1 or -1, respectively. | |
205 | * | |
206 | * Returns: NULL if the dir item does not exists, an error pointer if an error | |
207 | * happened, or a pointer to a dir item if a dir item exists for the given name. | |
d352ac68 | 208 | */ |
7e38180e CM |
209 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
210 | struct btrfs_root *root, | |
211 | struct btrfs_path *path, u64 dir, | |
6db75318 | 212 | const struct fscrypt_str *name, |
7e38180e | 213 | int mod) |
62e2749e | 214 | { |
62e2749e | 215 | struct btrfs_key key; |
a7d1c5dc | 216 | struct btrfs_dir_item *di; |
62e2749e CM |
217 | |
218 | key.objectid = dir; | |
962a298f | 219 | key.type = BTRFS_DIR_ITEM_KEY; |
e43eec81 | 220 | key.offset = btrfs_name_hash(name->name, name->len); |
5f39d397 | 221 | |
e43eec81 STD |
222 | di = btrfs_lookup_match_dir(trans, root, path, &key, name->name, |
223 | name->len, mod); | |
a7d1c5dc | 224 | if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) |
7e38180e CM |
225 | return NULL; |
226 | ||
a7d1c5dc | 227 | return di; |
62e2749e CM |
228 | } |
229 | ||
9c52057c | 230 | int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir, |
6db75318 | 231 | const struct fscrypt_str *name) |
9c52057c CM |
232 | { |
233 | int ret; | |
234 | struct btrfs_key key; | |
235 | struct btrfs_dir_item *di; | |
236 | int data_size; | |
237 | struct extent_buffer *leaf; | |
238 | int slot; | |
50833146 | 239 | BTRFS_PATH_AUTO_FREE(path); |
9c52057c | 240 | |
9c52057c CM |
241 | path = btrfs_alloc_path(); |
242 | if (!path) | |
243 | return -ENOMEM; | |
244 | ||
245 | key.objectid = dir; | |
962a298f | 246 | key.type = BTRFS_DIR_ITEM_KEY; |
e43eec81 | 247 | key.offset = btrfs_name_hash(name->name, name->len); |
9c52057c | 248 | |
e43eec81 STD |
249 | di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name, |
250 | name->len, 0); | |
a7d1c5dc MPS |
251 | if (IS_ERR(di)) { |
252 | ret = PTR_ERR(di); | |
253 | /* Nothing found, we're safe */ | |
50833146 DS |
254 | if (ret == -ENOENT) |
255 | return 0; | |
9c52057c | 256 | |
a7d1c5dc | 257 | if (ret < 0) |
50833146 | 258 | return ret; |
9c52057c CM |
259 | } |
260 | ||
261 | /* we found an item, look for our name in the item */ | |
9c52057c CM |
262 | if (di) { |
263 | /* our exact name was found */ | |
50833146 | 264 | return -EEXIST; |
9c52057c CM |
265 | } |
266 | ||
e43eec81 STD |
267 | /* See if there is room in the item to insert this name. */ |
268 | data_size = sizeof(*di) + name->len; | |
9c52057c CM |
269 | leaf = path->nodes[0]; |
270 | slot = path->slots[0]; | |
3212fa14 | 271 | if (data_size + btrfs_item_size(leaf, slot) + |
da17066c | 272 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { |
50833146 | 273 | return -EOVERFLOW; |
9c52057c | 274 | } |
50833146 DS |
275 | |
276 | /* Plenty of insertion room. */ | |
277 | return 0; | |
9c52057c CM |
278 | } |
279 | ||
d352ac68 | 280 | /* |
8dcbc261 | 281 | * Lookup for a directory index item by name and index number. |
d352ac68 | 282 | * |
8dcbc261 FM |
283 | * @trans: The transaction handle to use. Can be NULL if @mod is 0. |
284 | * @root: The root of the target tree. | |
285 | * @path: Path to use for the search. | |
286 | * @dir: The inode number (objectid) of the directory. | |
287 | * @index: The index number. | |
288 | * @name: The name associated to the directory entry we are looking for. | |
289 | * @name_len: The length of the name. | |
290 | * @mod: Used to indicate if the tree search is meant for a read only | |
291 | * lookup, for a modification lookup or for a deletion lookup, so | |
292 | * its value should be 0, 1 or -1, respectively. | |
293 | * | |
294 | * Returns: NULL if the dir index item does not exists, an error pointer if an | |
295 | * error happened, or a pointer to a dir item if the dir index item exists and | |
296 | * matches the criteria (name and index number). | |
d352ac68 | 297 | */ |
7e38180e CM |
298 | struct btrfs_dir_item * |
299 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, | |
300 | struct btrfs_root *root, | |
301 | struct btrfs_path *path, u64 dir, | |
6db75318 | 302 | u64 index, const struct fscrypt_str *name, int mod) |
7e38180e | 303 | { |
8dcbc261 | 304 | struct btrfs_dir_item *di; |
7e38180e | 305 | struct btrfs_key key; |
7e38180e CM |
306 | |
307 | key.objectid = dir; | |
962a298f | 308 | key.type = BTRFS_DIR_INDEX_KEY; |
8dcbc261 | 309 | key.offset = index; |
7e38180e | 310 | |
e43eec81 STD |
311 | di = btrfs_lookup_match_dir(trans, root, path, &key, name->name, |
312 | name->len, mod); | |
8dcbc261 FM |
313 | if (di == ERR_PTR(-ENOENT)) |
314 | return NULL; | |
315 | ||
316 | return di; | |
7e38180e CM |
317 | } |
318 | ||
4df27c4d | 319 | struct btrfs_dir_item * |
e43eec81 | 320 | btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path, |
6db75318 | 321 | u64 dirid, const struct fscrypt_str *name) |
4df27c4d | 322 | { |
4df27c4d YZ |
323 | struct btrfs_dir_item *di; |
324 | struct btrfs_key key; | |
4df27c4d YZ |
325 | int ret; |
326 | ||
327 | key.objectid = dirid; | |
328 | key.type = BTRFS_DIR_INDEX_KEY; | |
329 | key.offset = 0; | |
330 | ||
9dcbe16f | 331 | btrfs_for_each_slot(root, &key, &key, path, ret) { |
4df27c4d YZ |
332 | if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY) |
333 | break; | |
334 | ||
8c7cd2b6 | 335 | di = btrfs_match_dir_item_name(path, name->name, name->len); |
4df27c4d YZ |
336 | if (di) |
337 | return di; | |
4df27c4d | 338 | } |
9dcbe16f | 339 | /* Adjust return code if the key was not found in the next leaf. */ |
75f49c3d YH |
340 | if (ret >= 0) |
341 | ret = -ENOENT; | |
9dcbe16f GN |
342 | |
343 | return ERR_PTR(ret); | |
4df27c4d YZ |
344 | } |
345 | ||
5103e947 JB |
346 | struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, |
347 | struct btrfs_root *root, | |
348 | struct btrfs_path *path, u64 dir, | |
349 | const char *name, u16 name_len, | |
350 | int mod) | |
351 | { | |
5103e947 | 352 | struct btrfs_key key; |
a7d1c5dc | 353 | struct btrfs_dir_item *di; |
5103e947 JB |
354 | |
355 | key.objectid = dir; | |
962a298f | 356 | key.type = BTRFS_XATTR_ITEM_KEY; |
df68b8a7 | 357 | key.offset = btrfs_name_hash(name, name_len); |
a7d1c5dc MPS |
358 | |
359 | di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod); | |
360 | if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) | |
5103e947 JB |
361 | return NULL; |
362 | ||
a7d1c5dc | 363 | return di; |
5103e947 JB |
364 | } |
365 | ||
d352ac68 CM |
366 | /* |
367 | * helper function to look at the directory item pointed to by 'path' | |
368 | * this walks through all the entries in a dir item and finds one | |
369 | * for a specific name. | |
370 | */ | |
8c7cd2b6 | 371 | struct btrfs_dir_item *btrfs_match_dir_item_name(const struct btrfs_path *path, |
5f5bc6b1 | 372 | const char *name, int name_len) |
62e2749e | 373 | { |
62e2749e | 374 | struct btrfs_dir_item *dir_item; |
5f39d397 | 375 | unsigned long name_ptr; |
7e38180e CM |
376 | u32 total_len; |
377 | u32 cur = 0; | |
378 | u32 this_len; | |
5f39d397 | 379 | struct extent_buffer *leaf; |
a8a2ee0c | 380 | |
5f39d397 | 381 | leaf = path->nodes[0]; |
7e38180e | 382 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
22a94d44 | 383 | |
3212fa14 | 384 | total_len = btrfs_item_size(leaf, path->slots[0]); |
d397712b | 385 | while (cur < total_len) { |
5f39d397 | 386 | this_len = sizeof(*dir_item) + |
5103e947 JB |
387 | btrfs_dir_name_len(leaf, dir_item) + |
388 | btrfs_dir_data_len(leaf, dir_item); | |
5f39d397 | 389 | name_ptr = (unsigned long)(dir_item + 1); |
7e38180e | 390 | |
5f39d397 CM |
391 | if (btrfs_dir_name_len(leaf, dir_item) == name_len && |
392 | memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) | |
7e38180e CM |
393 | return dir_item; |
394 | ||
395 | cur += this_len; | |
396 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + | |
397 | this_len); | |
398 | } | |
399 | return NULL; | |
62e2749e | 400 | } |
7e38180e | 401 | |
d352ac68 CM |
402 | /* |
403 | * given a pointer into a directory item, delete it. This | |
404 | * handles items that have more than one entry in them. | |
405 | */ | |
7e38180e CM |
406 | int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, |
407 | struct btrfs_root *root, | |
408 | struct btrfs_path *path, | |
2917f741 | 409 | const struct btrfs_dir_item *di) |
7e38180e CM |
410 | { |
411 | ||
5f39d397 | 412 | struct extent_buffer *leaf; |
7e38180e CM |
413 | u32 sub_item_len; |
414 | u32 item_len; | |
54aa1f4d | 415 | int ret = 0; |
7e38180e | 416 | |
5f39d397 | 417 | leaf = path->nodes[0]; |
5103e947 JB |
418 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) + |
419 | btrfs_dir_data_len(leaf, di); | |
3212fa14 | 420 | item_len = btrfs_item_size(leaf, path->slots[0]); |
5f39d397 | 421 | if (sub_item_len == item_len) { |
7e38180e | 422 | ret = btrfs_del_item(trans, root, path); |
7e38180e | 423 | } else { |
5f39d397 CM |
424 | /* MARKER */ |
425 | unsigned long ptr = (unsigned long)di; | |
426 | unsigned long start; | |
427 | ||
428 | start = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
429 | memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, | |
7e38180e | 430 | item_len - (ptr + sub_item_len - start)); |
50564b65 | 431 | btrfs_truncate_item(trans, path, item_len - sub_item_len, 1); |
7e38180e | 432 | } |
411fc6bc | 433 | return ret; |
7e38180e | 434 | } |