blob: 3b532bab075521cc9b1e5c8577d0eeef00b3301e [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason62e27492007-03-15 12:56:47 -04006#include "ctree.h"
7#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04008#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -04009
Chris Masond352ac62008-09-29 15:18:18 -040010/*
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 */
Chris Mason35b7e472007-05-02 15:53:43 -040018static 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,
Chris Masone06afa82007-05-23 15:44:28 -040023 u32 data_size,
24 const char *name,
25 int name_len)
Chris Mason7fcde0e2007-04-05 12:13:21 -040026{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040027 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7fcde0e2007-04-05 12:13:21 -040028 int ret;
Chris Mason7e381802007-04-19 15:36:27 -040029 char *ptr;
Chris Mason5f39d392007-10-15 16:14:19 -040030 struct extent_buffer *leaf;
Chris Mason7fcde0e2007-04-05 12:13:21 -040031
32 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason7e381802007-04-19 15:36:27 -040033 if (ret == -EEXIST) {
Chris Masone06afa82007-05-23 15:44:28 -040034 struct btrfs_dir_item *di;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040035 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
Chris Masone06afa82007-05-23 15:44:28 -040036 if (di)
37 return ERR_PTR(-EEXIST);
David Sterbac71dd882019-03-20 14:51:10 +010038 btrfs_extend_item(path, data_size);
Jeff Mahoney143bede2012-03-01 14:56:26 +010039 } else if (ret < 0)
Chris Mason54aa1f42007-06-22 14:16:25 -040040 return ERR_PTR(ret);
Chris Mason7e381802007-04-19 15:36:27 -040041 WARN_ON(ret > 0);
Chris Mason5f39d392007-10-15 16:14:19 -040042 leaf = path->nodes[0];
Chris Mason7e381802007-04-19 15:36:27 -040043 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
Josef Bacik3212fa12021-10-21 14:58:35 -040044 ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
45 ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
Chris Mason7e381802007-04-19 15:36:27 -040046 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040047}
48
Chris Masond352ac62008-09-29 15:18:18 -040049/*
50 * xattrs work a lot like directories, this inserts an xattr item
51 * into the tree
52 */
Josef Bacik5103e942007-11-16 11:45:54 -050053int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
Yan, Zhengf34f57a2009-11-12 09:35:27 +000054 struct btrfs_root *root,
55 struct btrfs_path *path, u64 objectid,
56 const char *name, u16 name_len,
57 const void *data, u16 data_len)
Josef Bacik5103e942007-11-16 11:45:54 -050058{
59 int ret = 0;
Josef Bacik5103e942007-11-16 11:45:54 -050060 struct btrfs_dir_item *dir_item;
61 unsigned long name_ptr, data_ptr;
62 struct btrfs_key key, location;
63 struct btrfs_disk_key disk_key;
64 struct extent_buffer *leaf;
65 u32 data_size;
66
David Sterbab9d04c62017-02-17 19:42:43 +010067 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
68 return -ENOSPC;
Yan, Zhengf34f57a2009-11-12 09:35:27 +000069
70 key.objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +020071 key.type = BTRFS_XATTR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -050072 key.offset = btrfs_name_hash(name, name_len);
Josef Bacik5103e942007-11-16 11:45:54 -050073
74 data_size = sizeof(*dir_item) + name_len + data_len;
75 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
76 name, name_len);
Josef Bacikfa092002011-05-27 12:06:11 -040077 if (IS_ERR(dir_item))
78 return PTR_ERR(dir_item);
Josef Bacik5103e942007-11-16 11:45:54 -050079 memset(&location, 0, sizeof(location));
80
81 leaf = path->nodes[0];
82 btrfs_cpu_key_to_disk(&disk_key, &location);
83 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
84 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
85 btrfs_set_dir_name_len(leaf, dir_item, name_len);
Chris Masone02119d2008-09-05 16:13:11 -040086 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
Josef Bacik5103e942007-11-16 11:45:54 -050087 btrfs_set_dir_data_len(leaf, dir_item, data_len);
88 name_ptr = (unsigned long)(dir_item + 1);
89 data_ptr = (unsigned long)((char *)name_ptr + name_len);
90
91 write_extent_buffer(leaf, name, name_ptr, name_len);
92 write_extent_buffer(leaf, data, data_ptr, data_len);
93 btrfs_mark_buffer_dirty(path->nodes[0]);
94
Josef Bacik5103e942007-11-16 11:45:54 -050095 return ret;
96}
97
Chris Masond352ac62008-09-29 15:18:18 -040098/*
99 * insert a directory item in the tree, doing all the magic for
100 * both indexes. 'dir' indicates which objectid to insert it into,
101 * 'location' is the key to stuff into the directory item, 'type' is the
102 * type of the inode we're pointing to, and 'index' is the sequence number
103 * to use for the second index (if one is created).
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100104 * Will return 0 or -ENOMEM
Chris Masond352ac62008-09-29 15:18:18 -0400105 */
Lu Fengqi684572d2018-08-04 21:10:57 +0800106int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
107 int name_len, struct btrfs_inode *dir,
108 struct btrfs_key *location, u8 type, u64 index)
Chris Mason62e27492007-03-15 12:56:47 -0400109{
110 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -0400111 int ret2 = 0;
Lu Fengqi684572d2018-08-04 21:10:57 +0800112 struct btrfs_root *root = dir->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400113 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -0400114 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400115 struct extent_buffer *leaf;
116 unsigned long name_ptr;
Chris Mason62e27492007-03-15 12:56:47 -0400117 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400118 struct btrfs_disk_key disk_key;
Chris Mason62e27492007-03-15 12:56:47 -0400119 u32 data_size;
120
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200121 key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +0200122 key.type = BTRFS_DIR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -0500123 key.offset = btrfs_name_hash(name, name_len);
Chris Masonb9473432009-03-13 11:00:37 -0400124
Chris Mason5caf2a02007-04-02 11:20:42 -0400125 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +0800126 if (!path)
127 return -ENOMEM;
Chris Masonb9473432009-03-13 11:00:37 -0400128
Miao Xie16cdcec2011-04-22 18:12:22 +0800129 btrfs_cpu_key_to_disk(&disk_key, location);
130
Chris Mason62e27492007-03-15 12:56:47 -0400131 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -0400132 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
133 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400134 if (IS_ERR(dir_item)) {
135 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -0400136 if (ret == -EEXIST)
137 goto second_insert;
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000138 goto out_free;
Chris Mason7e381802007-04-19 15:36:27 -0400139 }
Chris Mason62e27492007-03-15 12:56:47 -0400140
Chris Mason5f39d392007-10-15 16:14:19 -0400141 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400142 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
143 btrfs_set_dir_type(leaf, dir_item, type);
Josef Bacik5103e942007-11-16 11:45:54 -0500144 btrfs_set_dir_data_len(leaf, dir_item, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400145 btrfs_set_dir_name_len(leaf, dir_item, name_len);
Chris Masone02119d2008-09-05 16:13:11 -0400146 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -0400147 name_ptr = (unsigned long)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400148
Chris Mason5f39d392007-10-15 16:14:19 -0400149 write_extent_buffer(leaf, name, name_ptr, name_len);
150 btrfs_mark_buffer_dirty(leaf);
Chris Mason7e381802007-04-19 15:36:27 -0400151
Chris Masone06afa82007-05-23 15:44:28 -0400152second_insert:
Chris Mason7e381802007-04-19 15:36:27 -0400153 /* FIXME, use some real flag for selecting the extra index */
154 if (root == root->fs_info->tree_root) {
155 ret = 0;
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000156 goto out_free;
Chris Mason7e381802007-04-19 15:36:27 -0400157 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200158 btrfs_release_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400159
Lu Fengqi4465c8b2018-08-01 11:32:25 +0800160 ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
161 &disk_key, type, index);
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000162out_free:
Chris Mason5caf2a02007-04-02 11:20:42 -0400163 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400164 if (ret)
165 return ret;
166 if (ret2)
167 return ret2;
168 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400169}
170
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300171static struct btrfs_dir_item *btrfs_lookup_match_dir(
172 struct btrfs_trans_handle *trans,
173 struct btrfs_root *root, struct btrfs_path *path,
174 struct btrfs_key *key, const char *name,
175 int name_len, int mod)
176{
177 const int ins_len = (mod < 0 ? -1 : 0);
178 const int cow = (mod != 0);
179 int ret;
180
181 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
182 if (ret < 0)
183 return ERR_PTR(ret);
184 if (ret > 0)
185 return ERR_PTR(-ENOENT);
186
187 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
188}
189
Chris Masond352ac62008-09-29 15:18:18 -0400190/*
Filipe Manana8dcbc262021-10-01 13:52:33 +0100191 * Lookup for a directory item by name.
192 *
193 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
194 * @root: The root of the target tree.
195 * @path: Path to use for the search.
196 * @dir: The inode number (objectid) of the directory.
197 * @name: The name associated to the directory entry we are looking for.
198 * @name_len: The length of the name.
199 * @mod: Used to indicate if the tree search is meant for a read only
200 * lookup, for a modification lookup or for a deletion lookup, so
201 * its value should be 0, 1 or -1, respectively.
202 *
203 * Returns: NULL if the dir item does not exists, an error pointer if an error
204 * happened, or a pointer to a dir item if a dir item exists for the given name.
Chris Masond352ac62008-09-29 15:18:18 -0400205 */
Chris Mason7e381802007-04-19 15:36:27 -0400206struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root,
208 struct btrfs_path *path, u64 dir,
209 const char *name, int name_len,
210 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400211{
Chris Mason62e27492007-03-15 12:56:47 -0400212 struct btrfs_key key;
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300213 struct btrfs_dir_item *di;
Chris Mason62e27492007-03-15 12:56:47 -0400214
215 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200216 key.type = BTRFS_DIR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -0500217 key.offset = btrfs_name_hash(name, name_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400218
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300219 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
220 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
Chris Mason7e381802007-04-19 15:36:27 -0400221 return NULL;
222
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300223 return di;
Chris Mason62e27492007-03-15 12:56:47 -0400224}
225
Chris Mason9c520572012-12-17 14:26:57 -0500226int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
227 const char *name, int name_len)
228{
229 int ret;
230 struct btrfs_key key;
231 struct btrfs_dir_item *di;
232 int data_size;
233 struct extent_buffer *leaf;
234 int slot;
235 struct btrfs_path *path;
236
Chris Mason9c520572012-12-17 14:26:57 -0500237 path = btrfs_alloc_path();
238 if (!path)
239 return -ENOMEM;
240
241 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200242 key.type = BTRFS_DIR_ITEM_KEY;
Chris Mason9c520572012-12-17 14:26:57 -0500243 key.offset = btrfs_name_hash(name, name_len);
244
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300245 di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
246 if (IS_ERR(di)) {
247 ret = PTR_ERR(di);
248 /* Nothing found, we're safe */
249 if (ret == -ENOENT) {
250 ret = 0;
251 goto out;
252 }
Chris Mason9c520572012-12-17 14:26:57 -0500253
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300254 if (ret < 0)
255 goto out;
Chris Mason9c520572012-12-17 14:26:57 -0500256 }
257
258 /* we found an item, look for our name in the item */
Chris Mason9c520572012-12-17 14:26:57 -0500259 if (di) {
260 /* our exact name was found */
261 ret = -EEXIST;
262 goto out;
263 }
264
265 /*
266 * see if there is room in the item to insert this
267 * name
268 */
Filipe David Borba Manana878f2d22013-11-27 16:06:16 +0000269 data_size = sizeof(*di) + name_len;
Chris Mason9c520572012-12-17 14:26:57 -0500270 leaf = path->nodes[0];
271 slot = path->slots[0];
Josef Bacik3212fa12021-10-21 14:58:35 -0400272 if (data_size + btrfs_item_size(leaf, slot) +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400273 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
Chris Mason9c520572012-12-17 14:26:57 -0500274 ret = -EOVERFLOW;
275 } else {
276 /* plenty of insertion room */
277 ret = 0;
278 }
279out:
280 btrfs_free_path(path);
281 return ret;
282}
283
Chris Masond352ac62008-09-29 15:18:18 -0400284/*
Filipe Manana8dcbc262021-10-01 13:52:33 +0100285 * Lookup for a directory index item by name and index number.
Chris Masond352ac62008-09-29 15:18:18 -0400286 *
Filipe Manana8dcbc262021-10-01 13:52:33 +0100287 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
288 * @root: The root of the target tree.
289 * @path: Path to use for the search.
290 * @dir: The inode number (objectid) of the directory.
291 * @index: The index number.
292 * @name: The name associated to the directory entry we are looking for.
293 * @name_len: The length of the name.
294 * @mod: Used to indicate if the tree search is meant for a read only
295 * lookup, for a modification lookup or for a deletion lookup, so
296 * its value should be 0, 1 or -1, respectively.
297 *
298 * Returns: NULL if the dir index item does not exists, an error pointer if an
299 * error happened, or a pointer to a dir item if the dir index item exists and
300 * matches the criteria (name and index number).
Chris Masond352ac62008-09-29 15:18:18 -0400301 */
Chris Mason7e381802007-04-19 15:36:27 -0400302struct btrfs_dir_item *
303btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
304 struct btrfs_root *root,
305 struct btrfs_path *path, u64 dir,
Filipe Manana8dcbc262021-10-01 13:52:33 +0100306 u64 index, const char *name, int name_len,
Chris Mason7e381802007-04-19 15:36:27 -0400307 int mod)
308{
Filipe Manana8dcbc262021-10-01 13:52:33 +0100309 struct btrfs_dir_item *di;
Chris Mason7e381802007-04-19 15:36:27 -0400310 struct btrfs_key key;
Chris Mason7e381802007-04-19 15:36:27 -0400311
312 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200313 key.type = BTRFS_DIR_INDEX_KEY;
Filipe Manana8dcbc262021-10-01 13:52:33 +0100314 key.offset = index;
Chris Mason7e381802007-04-19 15:36:27 -0400315
Filipe Manana8dcbc262021-10-01 13:52:33 +0100316 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
317 if (di == ERR_PTR(-ENOENT))
318 return NULL;
319
320 return di;
Chris Mason7e381802007-04-19 15:36:27 -0400321}
322
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400323struct btrfs_dir_item *
324btrfs_search_dir_index_item(struct btrfs_root *root,
325 struct btrfs_path *path, u64 dirid,
326 const char *name, int name_len)
327{
328 struct extent_buffer *leaf;
329 struct btrfs_dir_item *di;
330 struct btrfs_key key;
331 u32 nritems;
332 int ret;
333
334 key.objectid = dirid;
335 key.type = BTRFS_DIR_INDEX_KEY;
336 key.offset = 0;
337
338 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
339 if (ret < 0)
340 return ERR_PTR(ret);
341
342 leaf = path->nodes[0];
343 nritems = btrfs_header_nritems(leaf);
344
345 while (1) {
346 if (path->slots[0] >= nritems) {
347 ret = btrfs_next_leaf(root, path);
348 if (ret < 0)
349 return ERR_PTR(ret);
350 if (ret > 0)
351 break;
352 leaf = path->nodes[0];
353 nritems = btrfs_header_nritems(leaf);
354 continue;
355 }
356
357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
358 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
359 break;
360
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400361 di = btrfs_match_dir_item_name(root->fs_info, path,
362 name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400363 if (di)
364 return di;
365
366 path->slots[0]++;
367 }
368 return NULL;
369}
370
Josef Bacik5103e942007-11-16 11:45:54 -0500371struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
372 struct btrfs_root *root,
373 struct btrfs_path *path, u64 dir,
374 const char *name, u16 name_len,
375 int mod)
376{
Josef Bacik5103e942007-11-16 11:45:54 -0500377 struct btrfs_key key;
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300378 struct btrfs_dir_item *di;
Josef Bacik5103e942007-11-16 11:45:54 -0500379
380 key.objectid = dir;
David Sterba962a2982014-06-04 18:41:45 +0200381 key.type = BTRFS_XATTR_ITEM_KEY;
David Millerdf68b8a2008-02-15 10:40:52 -0500382 key.offset = btrfs_name_hash(name, name_len);
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300383
384 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
385 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
Josef Bacik5103e942007-11-16 11:45:54 -0500386 return NULL;
387
Marcos Paulo de Souzaa7d1c5d2021-07-26 16:19:09 -0300388 return di;
Josef Bacik5103e942007-11-16 11:45:54 -0500389}
390
Chris Masond352ac62008-09-29 15:18:18 -0400391/*
392 * helper function to look at the directory item pointed to by 'path'
393 * this walks through all the entries in a dir item and finds one
394 * for a specific name.
395 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400396struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
Filipe Manana5f5bc6b2014-11-09 08:38:39 +0000397 struct btrfs_path *path,
398 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400399{
Chris Mason62e27492007-03-15 12:56:47 -0400400 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400401 unsigned long name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400402 u32 total_len;
403 u32 cur = 0;
404 u32 this_len;
Chris Mason5f39d392007-10-15 16:14:19 -0400405 struct extent_buffer *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400406
Chris Mason5f39d392007-10-15 16:14:19 -0400407 leaf = path->nodes[0];
Chris Mason7e381802007-04-19 15:36:27 -0400408 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
Josef Bacik22a94d42011-03-16 16:47:17 -0400409
Josef Bacik3212fa12021-10-21 14:58:35 -0400410 total_len = btrfs_item_size(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500411 while (cur < total_len) {
Chris Mason5f39d392007-10-15 16:14:19 -0400412 this_len = sizeof(*dir_item) +
Josef Bacik5103e942007-11-16 11:45:54 -0500413 btrfs_dir_name_len(leaf, dir_item) +
414 btrfs_dir_data_len(leaf, dir_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400415 name_ptr = (unsigned long)(dir_item + 1);
Chris Mason7e381802007-04-19 15:36:27 -0400416
Chris Mason5f39d392007-10-15 16:14:19 -0400417 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
418 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
Chris Mason7e381802007-04-19 15:36:27 -0400419 return dir_item;
420
421 cur += this_len;
422 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
423 this_len);
424 }
425 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400426}
Chris Mason7e381802007-04-19 15:36:27 -0400427
Chris Masond352ac62008-09-29 15:18:18 -0400428/*
429 * given a pointer into a directory item, delete it. This
430 * handles items that have more than one entry in them.
431 */
Chris Mason7e381802007-04-19 15:36:27 -0400432int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
433 struct btrfs_root *root,
434 struct btrfs_path *path,
435 struct btrfs_dir_item *di)
436{
437
Chris Mason5f39d392007-10-15 16:14:19 -0400438 struct extent_buffer *leaf;
Chris Mason7e381802007-04-19 15:36:27 -0400439 u32 sub_item_len;
440 u32 item_len;
Chris Mason54aa1f42007-06-22 14:16:25 -0400441 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400442
Chris Mason5f39d392007-10-15 16:14:19 -0400443 leaf = path->nodes[0];
Josef Bacik5103e942007-11-16 11:45:54 -0500444 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
445 btrfs_dir_data_len(leaf, di);
Josef Bacik3212fa12021-10-21 14:58:35 -0400446 item_len = btrfs_item_size(leaf, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -0400447 if (sub_item_len == item_len) {
Chris Mason7e381802007-04-19 15:36:27 -0400448 ret = btrfs_del_item(trans, root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400449 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400450 /* MARKER */
451 unsigned long ptr = (unsigned long)di;
452 unsigned long start;
453
454 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
455 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
Chris Mason7e381802007-04-19 15:36:27 -0400456 item_len - (ptr + sub_item_len - start));
David Sterba78ac4f92019-03-20 14:49:12 +0100457 btrfs_truncate_item(path, item_len - sub_item_len, 1);
Chris Mason7e381802007-04-19 15:36:27 -0400458 }
Andi Kleen411fc6b2010-10-29 15:14:31 -0400459 return ret;
Chris Mason7e381802007-04-19 15:36:27 -0400460}