xref: /openbmc/linux/fs/btrfs/dir-item.c (revision 0ad53fe3)
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  */
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  */
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  */
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 
131 	btrfs_cpu_key_to_disk(&disk_key, location);
132 
133 	data_size = sizeof(*dir_item) + name_len;
134 	dir_item = insert_with_overflow(trans, root, path, &key, data_size,
135 					name, name_len);
136 	if (IS_ERR(dir_item)) {
137 		ret = PTR_ERR(dir_item);
138 		if (ret == -EEXIST)
139 			goto second_insert;
140 		goto out_free;
141 	}
142 
143 	leaf = path->nodes[0];
144 	btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
145 	btrfs_set_dir_type(leaf, dir_item, type);
146 	btrfs_set_dir_data_len(leaf, dir_item, 0);
147 	btrfs_set_dir_name_len(leaf, dir_item, name_len);
148 	btrfs_set_dir_transid(leaf, dir_item, trans->transid);
149 	name_ptr = (unsigned long)(dir_item + 1);
150 
151 	write_extent_buffer(leaf, name, name_ptr, name_len);
152 	btrfs_mark_buffer_dirty(leaf);
153 
154 second_insert:
155 	/* FIXME, use some real flag for selecting the extra index */
156 	if (root == root->fs_info->tree_root) {
157 		ret = 0;
158 		goto out_free;
159 	}
160 	btrfs_release_path(path);
161 
162 	ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
163 					      &disk_key, type, index);
164 out_free:
165 	btrfs_free_path(path);
166 	if (ret)
167 		return ret;
168 	if (ret2)
169 		return ret2;
170 	return 0;
171 }
172 
173 static struct btrfs_dir_item *btrfs_lookup_match_dir(
174 			struct btrfs_trans_handle *trans,
175 			struct btrfs_root *root, struct btrfs_path *path,
176 			struct btrfs_key *key, const char *name,
177 			int name_len, int mod)
178 {
179 	const int ins_len = (mod < 0 ? -1 : 0);
180 	const int cow = (mod != 0);
181 	int ret;
182 
183 	ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
184 	if (ret < 0)
185 		return ERR_PTR(ret);
186 	if (ret > 0)
187 		return ERR_PTR(-ENOENT);
188 
189 	return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
190 }
191 
192 /*
193  * lookup a directory item based on name.  'dir' is the objectid
194  * we're searching in, and 'mod' tells us if you plan on deleting the
195  * item (use mod < 0) or changing the options (use mod > 0)
196  */
197 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
198 					     struct btrfs_root *root,
199 					     struct btrfs_path *path, u64 dir,
200 					     const char *name, int name_len,
201 					     int mod)
202 {
203 	struct btrfs_key key;
204 	struct btrfs_dir_item *di;
205 
206 	key.objectid = dir;
207 	key.type = BTRFS_DIR_ITEM_KEY;
208 	key.offset = btrfs_name_hash(name, name_len);
209 
210 	di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
211 	if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
212 		return NULL;
213 
214 	return di;
215 }
216 
217 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
218 				   const char *name, int name_len)
219 {
220 	int ret;
221 	struct btrfs_key key;
222 	struct btrfs_dir_item *di;
223 	int data_size;
224 	struct extent_buffer *leaf;
225 	int slot;
226 	struct btrfs_path *path;
227 
228 	path = btrfs_alloc_path();
229 	if (!path)
230 		return -ENOMEM;
231 
232 	key.objectid = dir;
233 	key.type = BTRFS_DIR_ITEM_KEY;
234 	key.offset = btrfs_name_hash(name, name_len);
235 
236 	di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
237 	if (IS_ERR(di)) {
238 		ret = PTR_ERR(di);
239 		/* Nothing found, we're safe */
240 		if (ret == -ENOENT) {
241 			ret = 0;
242 			goto out;
243 		}
244 
245 		if (ret < 0)
246 			goto out;
247 	}
248 
249 	/* we found an item, look for our name in the item */
250 	if (di) {
251 		/* our exact name was found */
252 		ret = -EEXIST;
253 		goto out;
254 	}
255 
256 	/*
257 	 * see if there is room in the item to insert this
258 	 * name
259 	 */
260 	data_size = sizeof(*di) + name_len;
261 	leaf = path->nodes[0];
262 	slot = path->slots[0];
263 	if (data_size + btrfs_item_size_nr(leaf, slot) +
264 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
265 		ret = -EOVERFLOW;
266 	} else {
267 		/* plenty of insertion room */
268 		ret = 0;
269 	}
270 out:
271 	btrfs_free_path(path);
272 	return ret;
273 }
274 
275 /*
276  * lookup a directory item based on index.  'dir' is the objectid
277  * we're searching in, and 'mod' tells us if you plan on deleting the
278  * item (use mod < 0) or changing the options (use mod > 0)
279  *
280  * The name is used to make sure the index really points to the name you were
281  * looking for.
282  */
283 struct btrfs_dir_item *
284 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
285 			    struct btrfs_root *root,
286 			    struct btrfs_path *path, u64 dir,
287 			    u64 objectid, const char *name, int name_len,
288 			    int mod)
289 {
290 	struct btrfs_key key;
291 
292 	key.objectid = dir;
293 	key.type = BTRFS_DIR_INDEX_KEY;
294 	key.offset = objectid;
295 
296 	return btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
297 }
298 
299 struct btrfs_dir_item *
300 btrfs_search_dir_index_item(struct btrfs_root *root,
301 			    struct btrfs_path *path, u64 dirid,
302 			    const char *name, int name_len)
303 {
304 	struct extent_buffer *leaf;
305 	struct btrfs_dir_item *di;
306 	struct btrfs_key key;
307 	u32 nritems;
308 	int ret;
309 
310 	key.objectid = dirid;
311 	key.type = BTRFS_DIR_INDEX_KEY;
312 	key.offset = 0;
313 
314 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
315 	if (ret < 0)
316 		return ERR_PTR(ret);
317 
318 	leaf = path->nodes[0];
319 	nritems = btrfs_header_nritems(leaf);
320 
321 	while (1) {
322 		if (path->slots[0] >= nritems) {
323 			ret = btrfs_next_leaf(root, path);
324 			if (ret < 0)
325 				return ERR_PTR(ret);
326 			if (ret > 0)
327 				break;
328 			leaf = path->nodes[0];
329 			nritems = btrfs_header_nritems(leaf);
330 			continue;
331 		}
332 
333 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
334 		if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
335 			break;
336 
337 		di = btrfs_match_dir_item_name(root->fs_info, path,
338 					       name, name_len);
339 		if (di)
340 			return di;
341 
342 		path->slots[0]++;
343 	}
344 	return NULL;
345 }
346 
347 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
348 					  struct btrfs_root *root,
349 					  struct btrfs_path *path, u64 dir,
350 					  const char *name, u16 name_len,
351 					  int mod)
352 {
353 	struct btrfs_key key;
354 	struct btrfs_dir_item *di;
355 
356 	key.objectid = dir;
357 	key.type = BTRFS_XATTR_ITEM_KEY;
358 	key.offset = btrfs_name_hash(name, name_len);
359 
360 	di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
361 	if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
362 		return NULL;
363 
364 	return di;
365 }
366 
367 /*
368  * helper function to look at the directory item pointed to by 'path'
369  * this walks through all the entries in a dir item and finds one
370  * for a specific name.
371  */
372 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
373 						 struct btrfs_path *path,
374 						 const char *name, int name_len)
375 {
376 	struct btrfs_dir_item *dir_item;
377 	unsigned long name_ptr;
378 	u32 total_len;
379 	u32 cur = 0;
380 	u32 this_len;
381 	struct extent_buffer *leaf;
382 
383 	leaf = path->nodes[0];
384 	dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
385 
386 	total_len = btrfs_item_size_nr(leaf, path->slots[0]);
387 	while (cur < total_len) {
388 		this_len = sizeof(*dir_item) +
389 			btrfs_dir_name_len(leaf, dir_item) +
390 			btrfs_dir_data_len(leaf, dir_item);
391 		name_ptr = (unsigned long)(dir_item + 1);
392 
393 		if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
394 		    memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
395 			return dir_item;
396 
397 		cur += this_len;
398 		dir_item = (struct btrfs_dir_item *)((char *)dir_item +
399 						     this_len);
400 	}
401 	return NULL;
402 }
403 
404 /*
405  * given a pointer into a directory item, delete it.  This
406  * handles items that have more than one entry in them.
407  */
408 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
409 			      struct btrfs_root *root,
410 			      struct btrfs_path *path,
411 			      struct btrfs_dir_item *di)
412 {
413 
414 	struct extent_buffer *leaf;
415 	u32 sub_item_len;
416 	u32 item_len;
417 	int ret = 0;
418 
419 	leaf = path->nodes[0];
420 	sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
421 		btrfs_dir_data_len(leaf, di);
422 	item_len = btrfs_item_size_nr(leaf, path->slots[0]);
423 	if (sub_item_len == item_len) {
424 		ret = btrfs_del_item(trans, root, path);
425 	} else {
426 		/* MARKER */
427 		unsigned long ptr = (unsigned long)di;
428 		unsigned long start;
429 
430 		start = btrfs_item_ptr_offset(leaf, path->slots[0]);
431 		memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
432 			item_len - (ptr + sub_item_len - start));
433 		btrfs_truncate_item(path, item_len - sub_item_len, 1);
434 	}
435 	return ret;
436 }
437