GNU Linux-libre 6.1.90-gnu
[releases.git] / fs / btrfs / dir-item.c
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 extent_buffer *leaf;
31
32         ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
33         if (ret == -EEXIST) {
34                 struct btrfs_dir_item *di;
35                 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
36                 if (di)
37                         return ERR_PTR(-EEXIST);
38                 btrfs_extend_item(path, data_size);
39         } else if (ret < 0)
40                 return ERR_PTR(ret);
41         WARN_ON(ret > 0);
42         leaf = path->nodes[0];
43         ptr = btrfs_item_ptr(leaf, path->slots[0], char);
44         ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
45         ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
46         return (struct btrfs_dir_item *)ptr;
47 }
48
49 /*
50  * xattrs work a lot like directories, this inserts an xattr item
51  * into the tree
52  */
53 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
54                             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)
58 {
59         int ret = 0;
60         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
67         if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
68                 return -ENOSPC;
69
70         key.objectid = objectid;
71         key.type = BTRFS_XATTR_ITEM_KEY;
72         key.offset = btrfs_name_hash(name, name_len);
73
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);
77         if (IS_ERR(dir_item))
78                 return PTR_ERR(dir_item);
79         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);
86         btrfs_set_dir_transid(leaf, dir_item, trans->transid);
87         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
95         return ret;
96 }
97
98 /*
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).
104  * Will return 0 or -ENOMEM
105  */
106 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
107                           const struct fscrypt_str *name, struct btrfs_inode *dir,
108                           struct btrfs_key *location, u8 type, u64 index)
109 {
110         int ret = 0;
111         int ret2 = 0;
112         struct btrfs_root *root = dir->root;
113         struct btrfs_path *path;
114         struct btrfs_dir_item *dir_item;
115         struct extent_buffer *leaf;
116         unsigned long name_ptr;
117         struct btrfs_key key;
118         struct btrfs_disk_key disk_key;
119         u32 data_size;
120
121         key.objectid = btrfs_ino(dir);
122         key.type = BTRFS_DIR_ITEM_KEY;
123         key.offset = btrfs_name_hash(name->name, name->len);
124
125         path = btrfs_alloc_path();
126         if (!path)
127                 return -ENOMEM;
128
129         btrfs_cpu_key_to_disk(&disk_key, location);
130
131         data_size = sizeof(*dir_item) + name->len;
132         dir_item = insert_with_overflow(trans, root, path, &key, data_size,
133                                         name->name, name->len);
134         if (IS_ERR(dir_item)) {
135                 ret = PTR_ERR(dir_item);
136                 if (ret == -EEXIST)
137                         goto second_insert;
138                 goto out_free;
139         }
140
141         leaf = path->nodes[0];
142         btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
143         btrfs_set_dir_type(leaf, dir_item, type);
144         btrfs_set_dir_data_len(leaf, dir_item, 0);
145         btrfs_set_dir_name_len(leaf, dir_item, name->len);
146         btrfs_set_dir_transid(leaf, dir_item, trans->transid);
147         name_ptr = (unsigned long)(dir_item + 1);
148
149         write_extent_buffer(leaf, name->name, name_ptr, name->len);
150         btrfs_mark_buffer_dirty(leaf);
151
152 second_insert:
153         /* FIXME, use some real flag for selecting the extra index */
154         if (root == root->fs_info->tree_root) {
155                 ret = 0;
156                 goto out_free;
157         }
158         btrfs_release_path(path);
159
160         ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
161                                               &disk_key, type, index);
162 out_free:
163         btrfs_free_path(path);
164         if (ret)
165                 return ret;
166         if (ret2)
167                 return ret2;
168         return 0;
169 }
170
171 static 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
190 /*
191  * 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.
205  */
206 struct 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 struct fscrypt_str *name,
210                                              int mod)
211 {
212         struct btrfs_key key;
213         struct btrfs_dir_item *di;
214
215         key.objectid = dir;
216         key.type = BTRFS_DIR_ITEM_KEY;
217         key.offset = btrfs_name_hash(name->name, name->len);
218
219         di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
220                                     name->len, mod);
221         if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
222                 return NULL;
223
224         return di;
225 }
226
227 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
228                                    const struct fscrypt_str *name)
229 {
230         int ret;
231         struct btrfs_key key;
232         struct btrfs_dir_item *di;
233         int data_size;
234         struct extent_buffer *leaf;
235         int slot;
236         struct btrfs_path *path;
237
238         path = btrfs_alloc_path();
239         if (!path)
240                 return -ENOMEM;
241
242         key.objectid = dir;
243         key.type = BTRFS_DIR_ITEM_KEY;
244         key.offset = btrfs_name_hash(name->name, name->len);
245
246         di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name,
247                                     name->len, 0);
248         if (IS_ERR(di)) {
249                 ret = PTR_ERR(di);
250                 /* Nothing found, we're safe */
251                 if (ret == -ENOENT) {
252                         ret = 0;
253                         goto out;
254                 }
255
256                 if (ret < 0)
257                         goto out;
258         }
259
260         /* we found an item, look for our name in the item */
261         if (di) {
262                 /* our exact name was found */
263                 ret = -EEXIST;
264                 goto out;
265         }
266
267         /* See if there is room in the item to insert this name. */
268         data_size = sizeof(*di) + name->len;
269         leaf = path->nodes[0];
270         slot = path->slots[0];
271         if (data_size + btrfs_item_size(leaf, slot) +
272             sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
273                 ret = -EOVERFLOW;
274         } else {
275                 /* plenty of insertion room */
276                 ret = 0;
277         }
278 out:
279         btrfs_free_path(path);
280         return ret;
281 }
282
283 /*
284  * Lookup for a directory index item by name and index number.
285  *
286  * @trans:      The transaction handle to use. Can be NULL if @mod is 0.
287  * @root:       The root of the target tree.
288  * @path:       Path to use for the search.
289  * @dir:        The inode number (objectid) of the directory.
290  * @index:      The index number.
291  * @name:       The name associated to the directory entry we are looking for.
292  * @name_len:   The length of the name.
293  * @mod:        Used to indicate if the tree search is meant for a read only
294  *              lookup, for a modification lookup or for a deletion lookup, so
295  *              its value should be 0, 1 or -1, respectively.
296  *
297  * Returns: NULL if the dir index item does not exists, an error pointer if an
298  * error happened, or a pointer to a dir item if the dir index item exists and
299  * matches the criteria (name and index number).
300  */
301 struct btrfs_dir_item *
302 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
303                             struct btrfs_root *root,
304                             struct btrfs_path *path, u64 dir,
305                             u64 index, const struct fscrypt_str *name, int mod)
306 {
307         struct btrfs_dir_item *di;
308         struct btrfs_key key;
309
310         key.objectid = dir;
311         key.type = BTRFS_DIR_INDEX_KEY;
312         key.offset = index;
313
314         di = btrfs_lookup_match_dir(trans, root, path, &key, name->name,
315                                     name->len, mod);
316         if (di == ERR_PTR(-ENOENT))
317                 return NULL;
318
319         return di;
320 }
321
322 struct btrfs_dir_item *
323 btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
324                             u64 dirid, const struct fscrypt_str *name)
325 {
326         struct btrfs_dir_item *di;
327         struct btrfs_key key;
328         int ret;
329
330         key.objectid = dirid;
331         key.type = BTRFS_DIR_INDEX_KEY;
332         key.offset = 0;
333
334         btrfs_for_each_slot(root, &key, &key, path, ret) {
335                 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
336                         break;
337
338                 di = btrfs_match_dir_item_name(root->fs_info, path,
339                                                name->name, name->len);
340                 if (di)
341                         return di;
342         }
343         /* Adjust return code if the key was not found in the next leaf. */
344         if (ret > 0)
345                 ret = 0;
346
347         return ERR_PTR(ret);
348 }
349
350 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
351                                           struct btrfs_root *root,
352                                           struct btrfs_path *path, u64 dir,
353                                           const char *name, u16 name_len,
354                                           int mod)
355 {
356         struct btrfs_key key;
357         struct btrfs_dir_item *di;
358
359         key.objectid = dir;
360         key.type = BTRFS_XATTR_ITEM_KEY;
361         key.offset = btrfs_name_hash(name, name_len);
362
363         di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
364         if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
365                 return NULL;
366
367         return di;
368 }
369
370 /*
371  * helper function to look at the directory item pointed to by 'path'
372  * this walks through all the entries in a dir item and finds one
373  * for a specific name.
374  */
375 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
376                                                  struct btrfs_path *path,
377                                                  const char *name, int name_len)
378 {
379         struct btrfs_dir_item *dir_item;
380         unsigned long name_ptr;
381         u32 total_len;
382         u32 cur = 0;
383         u32 this_len;
384         struct extent_buffer *leaf;
385
386         leaf = path->nodes[0];
387         dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
388
389         total_len = btrfs_item_size(leaf, path->slots[0]);
390         while (cur < total_len) {
391                 this_len = sizeof(*dir_item) +
392                         btrfs_dir_name_len(leaf, dir_item) +
393                         btrfs_dir_data_len(leaf, dir_item);
394                 name_ptr = (unsigned long)(dir_item + 1);
395
396                 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
397                     memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
398                         return dir_item;
399
400                 cur += this_len;
401                 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
402                                                      this_len);
403         }
404         return NULL;
405 }
406
407 /*
408  * given a pointer into a directory item, delete it.  This
409  * handles items that have more than one entry in them.
410  */
411 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
412                               struct btrfs_root *root,
413                               struct btrfs_path *path,
414                               struct btrfs_dir_item *di)
415 {
416
417         struct extent_buffer *leaf;
418         u32 sub_item_len;
419         u32 item_len;
420         int ret = 0;
421
422         leaf = path->nodes[0];
423         sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
424                 btrfs_dir_data_len(leaf, di);
425         item_len = btrfs_item_size(leaf, path->slots[0]);
426         if (sub_item_len == item_len) {
427                 ret = btrfs_del_item(trans, root, path);
428         } else {
429                 /* MARKER */
430                 unsigned long ptr = (unsigned long)di;
431                 unsigned long start;
432
433                 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
434                 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
435                         item_len - (ptr + sub_item_len - start));
436                 btrfs_truncate_item(path, item_len - sub_item_len, 1);
437         }
438         return ret;
439 }