xref: /linux/fs/btrfs/dir-item.c (revision 0eb4aaa230d725fa9b1cd758c0f17abca5597af6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include "messages.h"
7 #include "ctree.h"
8 #include "disk-io.h"
9 #include "transaction.h"
10 #include "accessors.h"
11 #include "dir-item.h"
12 
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  */
insert_with_overflow(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * cpu_key,u32 data_size,const char * name,int name_len)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,
25 						   const struct btrfs_key *cpu_key,
26 						   u32 data_size,
27 						   const char *name,
28 						   int name_len)
29 {
30 	int ret;
31 	char *ptr;
32 	struct extent_buffer *leaf;
33 
34 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
35 	if (ret == -EEXIST) {
36 		struct btrfs_dir_item *di;
37 		di = btrfs_match_dir_item_name(path, name, name_len);
38 		if (di)
39 			return ERR_PTR(-EEXIST);
40 		btrfs_extend_item(trans, path, data_size);
41 	} else if (ret < 0)
42 		return ERR_PTR(ret);
43 	WARN_ON(ret > 0);
44 	leaf = path->nodes[0];
45 	ptr = btrfs_item_ptr(leaf, path->slots[0], char);
46 	ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
47 	ptr += btrfs_item_size(leaf, path->slots[0]) - 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_flags(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 
96 	return ret;
97 }
98 
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).
105  * Will return 0 or -ENOMEM
106  */
btrfs_insert_dir_item(struct btrfs_trans_handle * trans,const struct fscrypt_str * name,struct btrfs_inode * dir,const struct btrfs_key * location,u8 type,u64 index)107 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
108 			  const struct fscrypt_str *name, struct btrfs_inode *dir,
109 			  const struct btrfs_key *location, u8 type, u64 index)
110 {
111 	int ret = 0;
112 	int ret2 = 0;
113 	struct btrfs_root *root = dir->root;
114 	struct btrfs_path *path;
115 	struct btrfs_dir_item *dir_item;
116 	struct extent_buffer *leaf;
117 	unsigned long name_ptr;
118 	struct btrfs_key key;
119 	struct btrfs_disk_key disk_key;
120 	u32 data_size;
121 
122 	key.objectid = btrfs_ino(dir);
123 	key.type = BTRFS_DIR_ITEM_KEY;
124 	key.offset = btrfs_name_hash(name->name, name->len);
125 
126 	path = btrfs_alloc_path();
127 	if (!path)
128 		return -ENOMEM;
129 
130 	btrfs_cpu_key_to_disk(&disk_key, location);
131 
132 	data_size = sizeof(*dir_item) + name->len;
133 	dir_item = insert_with_overflow(trans, root, path, &key, data_size,
134 					name->name, name->len);
135 	if (IS_ERR(dir_item)) {
136 		ret = PTR_ERR(dir_item);
137 		if (ret == -EEXIST)
138 			goto second_insert;
139 		goto out_free;
140 	}
141 
142 	if (IS_ENCRYPTED(&dir->vfs_inode))
143 		type |= BTRFS_FT_ENCRYPTED;
144 
145 	leaf = path->nodes[0];
146 	btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
147 	btrfs_set_dir_flags(leaf, dir_item, type);
148 	btrfs_set_dir_data_len(leaf, dir_item, 0);
149 	btrfs_set_dir_name_len(leaf, dir_item, name->len);
150 	btrfs_set_dir_transid(leaf, dir_item, trans->transid);
151 	name_ptr = (unsigned long)(dir_item + 1);
152 
153 	write_extent_buffer(leaf, name->name, name_ptr, name->len);
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, 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(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 struct fscrypt_str * name,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 struct fscrypt_str *name,
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, name->len);
221 
222 	di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
223 				    name->len, mod);
224 	if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
225 		return NULL;
226 
227 	return di;
228 }
229 
btrfs_check_dir_item_collision(struct btrfs_root * root,u64 dir,const struct fscrypt_str * name)230 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
231 				   const struct fscrypt_str *name)
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;
239 	struct btrfs_path *path;
240 
241 	path = btrfs_alloc_path();
242 	if (!path)
243 		return -ENOMEM;
244 
245 	key.objectid = dir;
246 	key.type = BTRFS_DIR_ITEM_KEY;
247 	key.offset = btrfs_name_hash(name->name, name->len);
248 
249 	di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name,
250 				    name->len, 0);
251 	if (IS_ERR(di)) {
252 		ret = PTR_ERR(di);
253 		/* Nothing found, we're safe */
254 		if (ret == -ENOENT) {
255 			ret = 0;
256 			goto out;
257 		}
258 
259 		if (ret < 0)
260 			goto out;
261 	}
262 
263 	/* we found an item, look for our name in the item */
264 	if (di) {
265 		/* our exact name was found */
266 		ret = -EEXIST;
267 		goto out;
268 	}
269 
270 	/* See if there is room in the item to insert this name. */
271 	data_size = sizeof(*di) + name->len;
272 	leaf = path->nodes[0];
273 	slot = path->slots[0];
274 	if (data_size + btrfs_item_size(leaf, slot) +
275 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
276 		ret = -EOVERFLOW;
277 	} else {
278 		/* plenty of insertion room */
279 		ret = 0;
280 	}
281 out:
282 	btrfs_free_path(path);
283 	return ret;
284 }
285 
286 /*
287  * Lookup for a directory index item by name and index number.
288  *
289  * @trans:	The transaction handle to use. Can be NULL if @mod is 0.
290  * @root:	The root of the target tree.
291  * @path:	Path to use for the search.
292  * @dir:	The inode number (objectid) of the directory.
293  * @index:	The index number.
294  * @name:	The name associated to the directory entry we are looking for.
295  * @name_len:	The length of the name.
296  * @mod:	Used to indicate if the tree search is meant for a read only
297  *		lookup, for a modification lookup or for a deletion lookup, so
298  *		its value should be 0, 1 or -1, respectively.
299  *
300  * Returns: NULL if the dir index item does not exists, an error pointer if an
301  * error happened, or a pointer to a dir item if the dir index item exists and
302  * matches the criteria (name and index number).
303  */
304 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 struct fscrypt_str * name,int mod)305 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
306 			    struct btrfs_root *root,
307 			    struct btrfs_path *path, u64 dir,
308 			    u64 index, const struct fscrypt_str *name, int mod)
309 {
310 	struct btrfs_dir_item *di;
311 	struct btrfs_key key;
312 
313 	key.objectid = dir;
314 	key.type = BTRFS_DIR_INDEX_KEY;
315 	key.offset = index;
316 
317 	di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
318 				    name->len, mod);
319 	if (di == ERR_PTR(-ENOENT))
320 		return NULL;
321 
322 	return di;
323 }
324 
325 struct btrfs_dir_item *
btrfs_search_dir_index_item(struct btrfs_root * root,struct btrfs_path * path,u64 dirid,const struct fscrypt_str * name)326 btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
327 			    u64 dirid, const struct fscrypt_str *name)
328 {
329 	struct btrfs_dir_item *di;
330 	struct btrfs_key key;
331 	int ret;
332 
333 	key.objectid = dirid;
334 	key.type = BTRFS_DIR_INDEX_KEY;
335 	key.offset = 0;
336 
337 	btrfs_for_each_slot(root, &key, &key, path, ret) {
338 		if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
339 			break;
340 
341 		di = btrfs_match_dir_item_name(path, name->name, name->len);
342 		if (di)
343 			return di;
344 	}
345 	/* Adjust return code if the key was not found in the next leaf. */
346 	if (ret >= 0)
347 		ret = -ENOENT;
348 
349 	return ERR_PTR(ret);
350 }
351 
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)352 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
353 					  struct btrfs_root *root,
354 					  struct btrfs_path *path, u64 dir,
355 					  const char *name, u16 name_len,
356 					  int mod)
357 {
358 	struct btrfs_key key;
359 	struct btrfs_dir_item *di;
360 
361 	key.objectid = dir;
362 	key.type = BTRFS_XATTR_ITEM_KEY;
363 	key.offset = btrfs_name_hash(name, name_len);
364 
365 	di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
366 	if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
367 		return NULL;
368 
369 	return di;
370 }
371 
372 /*
373  * helper function to look at the directory item pointed to by 'path'
374  * this walks through all the entries in a dir item and finds one
375  * for a specific name.
376  */
btrfs_match_dir_item_name(const struct btrfs_path * path,const char * name,int name_len)377 struct btrfs_dir_item *btrfs_match_dir_item_name(const struct btrfs_path *path,
378 						 const char *name, int name_len)
379 {
380 	struct btrfs_dir_item *dir_item;
381 	unsigned long name_ptr;
382 	u32 total_len;
383 	u32 cur = 0;
384 	u32 this_len;
385 	struct extent_buffer *leaf;
386 
387 	leaf = path->nodes[0];
388 	dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
389 
390 	total_len = btrfs_item_size(leaf, path->slots[0]);
391 	while (cur < total_len) {
392 		this_len = sizeof(*dir_item) +
393 			btrfs_dir_name_len(leaf, dir_item) +
394 			btrfs_dir_data_len(leaf, dir_item);
395 		name_ptr = (unsigned long)(dir_item + 1);
396 
397 		if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
398 		    memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
399 			return dir_item;
400 
401 		cur += this_len;
402 		dir_item = (struct btrfs_dir_item *)((char *)dir_item +
403 						     this_len);
404 	}
405 	return NULL;
406 }
407 
408 /*
409  * given a pointer into a directory item, delete it.  This
410  * handles items that have more than one entry in them.
411  */
btrfs_delete_one_dir_name(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_dir_item * di)412 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
413 			      struct btrfs_root *root,
414 			      struct btrfs_path *path,
415 			      const struct btrfs_dir_item *di)
416 {
417 
418 	struct extent_buffer *leaf;
419 	u32 sub_item_len;
420 	u32 item_len;
421 	int ret = 0;
422 
423 	leaf = path->nodes[0];
424 	sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
425 		btrfs_dir_data_len(leaf, di);
426 	item_len = btrfs_item_size(leaf, path->slots[0]);
427 	if (sub_item_len == item_len) {
428 		ret = btrfs_del_item(trans, root, path);
429 	} else {
430 		/* MARKER */
431 		unsigned long ptr = (unsigned long)di;
432 		unsigned long start;
433 
434 		start = btrfs_item_ptr_offset(leaf, path->slots[0]);
435 		memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
436 			item_len - (ptr + sub_item_len - start));
437 		btrfs_truncate_item(trans, path, item_len - sub_item_len, 1);
438 	}
439 	return ret;
440 }
441