1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include "ctree.h"
7 #include "disk-io.h"
8 #include "transaction.h"
9
10 /*
11 * insert a name into a directory, doing overflow properly if there is a hash
12 * collision. data_size indicates how big the item inserted should be. On
13 * success a struct btrfs_dir_item pointer is returned, otherwise it is
14 * an ERR_PTR.
15 *
16 * The name is not copied into the dir item, you have to do that yourself.
17 */
insert_with_overflow(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_key * cpu_key,u32 data_size,const char * name,int name_len)18 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
19 *trans,
20 struct btrfs_root *root,
21 struct btrfs_path *path,
22 struct btrfs_key *cpu_key,
23 u32 data_size,
24 const char *name,
25 int name_len)
26 {
27 struct btrfs_fs_info *fs_info = root->fs_info;
28 int ret;
29 char *ptr;
30 struct btrfs_item *item;
31 struct extent_buffer *leaf;
32
33 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
34 if (ret == -EEXIST) {
35 struct btrfs_dir_item *di;
36 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
37 if (di)
38 return ERR_PTR(-EEXIST);
39 btrfs_extend_item(path, data_size);
40 } else if (ret < 0)
41 return ERR_PTR(ret);
42 WARN_ON(ret > 0);
43 leaf = path->nodes[0];
44 item = btrfs_item_nr(path->slots[0]);
45 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
46 BUG_ON(data_size > btrfs_item_size(leaf, item));
47 ptr += btrfs_item_size(leaf, item) - data_size;
48 return (struct btrfs_dir_item *)ptr;
49 }
50
51 /*
52 * xattrs work a lot like directories, this inserts an xattr item
53 * into the tree
54 */
btrfs_insert_xattr_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 objectid,const char * name,u16 name_len,const void * data,u16 data_len)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)
60 {
61 int ret = 0;
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
69 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
70 return -ENOSPC;
71
72 key.objectid = objectid;
73 key.type = BTRFS_XATTR_ITEM_KEY;
74 key.offset = btrfs_name_hash(name, name_len);
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);
79 if (IS_ERR(dir_item))
80 return PTR_ERR(dir_item);
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);
86 btrfs_set_dir_type(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);
92
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(path->nodes[0]);
96
97 return ret;
98 }
99
100 /*
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
107 */
btrfs_insert_dir_item(struct btrfs_trans_handle * trans,const char * name,int name_len,struct btrfs_inode * dir,struct btrfs_key * location,u8 type,u64 index)108 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
109 int name_len, struct btrfs_inode *dir,
110 struct btrfs_key *location, u8 type, u64 index)
111 {
112 int ret = 0;
113 int ret2 = 0;
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;
121 u32 data_size;
122
123 key.objectid = btrfs_ino(dir);
124 key.type = BTRFS_DIR_ITEM_KEY;
125 key.offset = btrfs_name_hash(name, name_len);
126
127 path = btrfs_alloc_path();
128 if (!path)
129 return -ENOMEM;
130 path->leave_spinning = 1;
131
132 btrfs_cpu_key_to_disk(&disk_key, location);
133
134 data_size = sizeof(*dir_item) + name_len;
135 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
136 name, name_len);
137 if (IS_ERR(dir_item)) {
138 ret = PTR_ERR(dir_item);
139 if (ret == -EEXIST)
140 goto second_insert;
141 goto out_free;
142 }
143
144 leaf = path->nodes[0];
145 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
146 btrfs_set_dir_type(leaf, dir_item, type);
147 btrfs_set_dir_data_len(leaf, dir_item, 0);
148 btrfs_set_dir_name_len(leaf, dir_item, name_len);
149 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
150 name_ptr = (unsigned long)(dir_item + 1);
151
152 write_extent_buffer(leaf, name, name_ptr, name_len);
153 btrfs_mark_buffer_dirty(leaf);
154
155 second_insert:
156 /* FIXME, use some real flag for selecting the extra index */
157 if (root == root->fs_info->tree_root) {
158 ret = 0;
159 goto out_free;
160 }
161 btrfs_release_path(path);
162
163 ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
164 &disk_key, type, index);
165 out_free:
166 btrfs_free_path(path);
167 if (ret)
168 return ret;
169 if (ret2)
170 return ret2;
171 return 0;
172 }
173
btrfs_lookup_match_dir(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_key * key,const char * name,int name_len,int mod)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
190 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
191 }
192
193 /*
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.
208 */
btrfs_lookup_dir_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 dir,const char * name,int name_len,int mod)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,
212 const char *name, int name_len,
213 int mod)
214 {
215 struct btrfs_key key;
216 struct btrfs_dir_item *di;
217
218 key.objectid = dir;
219 key.type = BTRFS_DIR_ITEM_KEY;
220 key.offset = btrfs_name_hash(name, name_len);
221
222 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
223 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
224 return NULL;
225
226 return di;
227 }
228
btrfs_check_dir_item_collision(struct btrfs_root * root,u64 dir,const char * name,int name_len)229 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
230 const char *name, int name_len)
231 {
232 int ret;
233 struct btrfs_key key;
234 struct btrfs_dir_item *di;
235 int data_size;
236 struct extent_buffer *leaf;
237 int slot;
238 struct btrfs_path *path;
239
240 path = btrfs_alloc_path();
241 if (!path)
242 return -ENOMEM;
243
244 key.objectid = dir;
245 key.type = BTRFS_DIR_ITEM_KEY;
246 key.offset = btrfs_name_hash(name, name_len);
247
248 di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
249 if (IS_ERR(di)) {
250 ret = PTR_ERR(di);
251 /* Nothing found, we're safe */
252 if (ret == -ENOENT) {
253 ret = 0;
254 goto out;
255 }
256
257 if (ret < 0)
258 goto out;
259 }
260
261 /* we found an item, look for our name in the item */
262 if (di) {
263 /* our exact name was found */
264 ret = -EEXIST;
265 goto out;
266 }
267
268 /*
269 * see if there is room in the item to insert this
270 * name
271 */
272 data_size = sizeof(*di) + name_len;
273 leaf = path->nodes[0];
274 slot = path->slots[0];
275 if (data_size + btrfs_item_size_nr(leaf, slot) +
276 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
277 ret = -EOVERFLOW;
278 } else {
279 /* plenty of insertion room */
280 ret = 0;
281 }
282 out:
283 btrfs_free_path(path);
284 return ret;
285 }
286
287 /*
288 * Lookup for a directory index item by name and index number.
289 *
290 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
291 * @root: The root of the target tree.
292 * @path: Path to use for the search.
293 * @dir: The inode number (objectid) of the directory.
294 * @index: The index number.
295 * @name: The name associated to the directory entry we are looking for.
296 * @name_len: The length of the name.
297 * @mod: Used to indicate if the tree search is meant for a read only
298 * lookup, for a modification lookup or for a deletion lookup, so
299 * its value should be 0, 1 or -1, respectively.
300 *
301 * Returns: NULL if the dir index item does not exists, an error pointer if an
302 * error happened, or a pointer to a dir item if the dir index item exists and
303 * matches the criteria (name and index number).
304 */
305 struct btrfs_dir_item *
btrfs_lookup_dir_index_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 dir,u64 index,const char * name,int name_len,int mod)306 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
307 struct btrfs_root *root,
308 struct btrfs_path *path, u64 dir,
309 u64 index, const char *name, int name_len,
310 int mod)
311 {
312 struct btrfs_dir_item *di;
313 struct btrfs_key key;
314
315 key.objectid = dir;
316 key.type = BTRFS_DIR_INDEX_KEY;
317 key.offset = index;
318
319 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
320 if (di == ERR_PTR(-ENOENT))
321 return NULL;
322
323 return di;
324 }
325
326 struct btrfs_dir_item *
btrfs_search_dir_index_item(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,const char * name,int name_len)327 btrfs_search_dir_index_item(struct btrfs_root *root,
328 struct btrfs_path *path, u64 dirid,
329 const char *name, int name_len)
330 {
331 struct extent_buffer *leaf;
332 struct btrfs_dir_item *di;
333 struct btrfs_key key;
334 u32 nritems;
335 int ret;
336
337 key.objectid = dirid;
338 key.type = BTRFS_DIR_INDEX_KEY;
339 key.offset = 0;
340
341 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
342 if (ret < 0)
343 return ERR_PTR(ret);
344
345 leaf = path->nodes[0];
346 nritems = btrfs_header_nritems(leaf);
347
348 while (1) {
349 if (path->slots[0] >= nritems) {
350 ret = btrfs_next_leaf(root, path);
351 if (ret < 0)
352 return ERR_PTR(ret);
353 if (ret > 0)
354 break;
355 leaf = path->nodes[0];
356 nritems = btrfs_header_nritems(leaf);
357 continue;
358 }
359
360 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
361 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
362 break;
363
364 di = btrfs_match_dir_item_name(root->fs_info, path,
365 name, name_len);
366 if (di)
367 return di;
368
369 path->slots[0]++;
370 }
371 return NULL;
372 }
373
btrfs_lookup_xattr(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,u64 dir,const char * name,u16 name_len,int mod)374 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
375 struct btrfs_root *root,
376 struct btrfs_path *path, u64 dir,
377 const char *name, u16 name_len,
378 int mod)
379 {
380 struct btrfs_key key;
381 struct btrfs_dir_item *di;
382
383 key.objectid = dir;
384 key.type = BTRFS_XATTR_ITEM_KEY;
385 key.offset = btrfs_name_hash(name, name_len);
386
387 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
388 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
389 return NULL;
390
391 return di;
392 }
393
394 /*
395 * helper function to look at the directory item pointed to by 'path'
396 * this walks through all the entries in a dir item and finds one
397 * for a specific name.
398 */
btrfs_match_dir_item_name(struct btrfs_fs_info * fs_info,struct btrfs_path * path,const char * name,int name_len)399 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
400 struct btrfs_path *path,
401 const char *name, int name_len)
402 {
403 struct btrfs_dir_item *dir_item;
404 unsigned long name_ptr;
405 u32 total_len;
406 u32 cur = 0;
407 u32 this_len;
408 struct extent_buffer *leaf;
409
410 leaf = path->nodes[0];
411 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
412
413 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
414 while (cur < total_len) {
415 this_len = sizeof(*dir_item) +
416 btrfs_dir_name_len(leaf, dir_item) +
417 btrfs_dir_data_len(leaf, dir_item);
418 name_ptr = (unsigned long)(dir_item + 1);
419
420 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
421 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
422 return dir_item;
423
424 cur += this_len;
425 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
426 this_len);
427 }
428 return NULL;
429 }
430
431 /*
432 * given a pointer into a directory item, delete it. This
433 * handles items that have more than one entry in them.
434 */
btrfs_delete_one_dir_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct btrfs_dir_item * di)435 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
436 struct btrfs_root *root,
437 struct btrfs_path *path,
438 struct btrfs_dir_item *di)
439 {
440
441 struct extent_buffer *leaf;
442 u32 sub_item_len;
443 u32 item_len;
444 int ret = 0;
445
446 leaf = path->nodes[0];
447 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
448 btrfs_dir_data_len(leaf, di);
449 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
450 if (sub_item_len == item_len) {
451 ret = btrfs_del_item(trans, root, path);
452 } else {
453 /* MARKER */
454 unsigned long ptr = (unsigned long)di;
455 unsigned long start;
456
457 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
458 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
459 item_len - (ptr + sub_item_len - start));
460 btrfs_truncate_item(path, item_len - sub_item_len, 1);
461 }
462 return ret;
463 }
464