David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 6 | #include "ctree.h" |
| 7 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 8 | #include "transaction.h" |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 9 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 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 | */ |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 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, |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 23 | u32 data_size, |
| 24 | const char *name, |
| 25 | int name_len) |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 26 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 27 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 28 | int ret; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 29 | char *ptr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 30 | struct extent_buffer *leaf; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 31 | |
| 32 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 33 | if (ret == -EEXIST) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 34 | struct btrfs_dir_item *di; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 35 | di = btrfs_match_dir_item_name(fs_info, path, name, name_len); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 36 | if (di) |
| 37 | return ERR_PTR(-EEXIST); |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 38 | btrfs_extend_item(path, data_size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 39 | } else if (ret < 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 40 | return ERR_PTR(ret); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 41 | WARN_ON(ret > 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 42 | leaf = path->nodes[0]; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 43 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 44 | ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0])); |
| 45 | ptr += btrfs_item_size(leaf, path->slots[0]) - data_size; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 46 | return (struct btrfs_dir_item *)ptr; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 49 | /* |
| 50 | * xattrs work a lot like directories, this inserts an xattr item |
| 51 | * into the tree |
| 52 | */ |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 53 | int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 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) |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 58 | { |
| 59 | int ret = 0; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 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 | |
David Sterba | b9d04c6 | 2017-02-17 19:42:43 +0100 | [diff] [blame] | 67 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) |
| 68 | return -ENOSPC; |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 69 | |
| 70 | key.objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 71 | key.type = BTRFS_XATTR_ITEM_KEY; |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 72 | key.offset = btrfs_name_hash(name, name_len); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 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); |
Josef Bacik | fa09200 | 2011-05-27 12:06:11 -0400 | [diff] [blame] | 77 | if (IS_ERR(dir_item)) |
| 78 | return PTR_ERR(dir_item); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 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); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 86 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 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 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 95 | return ret; |
| 96 | } |
| 97 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 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). |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 104 | * Will return 0 or -ENOMEM |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 105 | */ |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 106 | int 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 Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 109 | { |
| 110 | int ret = 0; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 111 | int ret2 = 0; |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 112 | struct btrfs_root *root = dir->root; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 113 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 114 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 115 | struct extent_buffer *leaf; |
| 116 | unsigned long name_ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 117 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 118 | struct btrfs_disk_key disk_key; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 119 | u32 data_size; |
| 120 | |
Nikolay Borisov | 8e7611c | 2017-02-20 13:50:31 +0200 | [diff] [blame] | 121 | key.objectid = btrfs_ino(dir); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 122 | key.type = BTRFS_DIR_ITEM_KEY; |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 123 | key.offset = btrfs_name_hash(name, name_len); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 124 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 125 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 126 | if (!path) |
| 127 | return -ENOMEM; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 128 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 129 | btrfs_cpu_key_to_disk(&disk_key, location); |
| 130 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 131 | data_size = sizeof(*dir_item) + name_len; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 132 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 133 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 134 | if (IS_ERR(dir_item)) { |
| 135 | ret = PTR_ERR(dir_item); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 136 | if (ret == -EEXIST) |
| 137 | goto second_insert; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 138 | goto out_free; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 139 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 140 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 141 | leaf = path->nodes[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 142 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 143 | btrfs_set_dir_type(leaf, dir_item, type); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 144 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 145 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 146 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 147 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 148 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 149 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 150 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 151 | |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 152 | second_insert: |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 153 | /* FIXME, use some real flag for selecting the extra index */ |
| 154 | if (root == root->fs_info->tree_root) { |
| 155 | ret = 0; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 156 | goto out_free; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 157 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 158 | btrfs_release_path(path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 159 | |
Lu Fengqi | 4465c8b | 2018-08-01 11:32:25 +0800 | [diff] [blame] | 160 | ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir, |
| 161 | &disk_key, type, index); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 162 | out_free: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 163 | btrfs_free_path(path); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 164 | if (ret) |
| 165 | return ret; |
| 166 | if (ret2) |
| 167 | return ret2; |
| 168 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 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 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 190 | /* |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 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. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 205 | */ |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 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 char *name, int name_len, |
| 210 | int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 211 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 212 | struct btrfs_key key; |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 213 | struct btrfs_dir_item *di; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 214 | |
| 215 | key.objectid = dir; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 216 | key.type = BTRFS_DIR_ITEM_KEY; |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 217 | key.offset = btrfs_name_hash(name, name_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 218 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 219 | di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod); |
| 220 | if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 221 | return NULL; |
| 222 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 223 | return di; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 226 | int 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 Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 237 | path = btrfs_alloc_path(); |
| 238 | if (!path) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | key.objectid = dir; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 242 | key.type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 243 | key.offset = btrfs_name_hash(name, name_len); |
| 244 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 245 | 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 Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 253 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 254 | if (ret < 0) |
| 255 | goto out; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* we found an item, look for our name in the item */ |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 259 | 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 Manana | 878f2d2 | 2013-11-27 16:06:16 +0000 | [diff] [blame] | 269 | data_size = sizeof(*di) + name_len; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 270 | leaf = path->nodes[0]; |
| 271 | slot = path->slots[0]; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 272 | if (data_size + btrfs_item_size(leaf, slot) + |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 273 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 274 | ret = -EOVERFLOW; |
| 275 | } else { |
| 276 | /* plenty of insertion room */ |
| 277 | ret = 0; |
| 278 | } |
| 279 | out: |
| 280 | btrfs_free_path(path); |
| 281 | return ret; |
| 282 | } |
| 283 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 284 | /* |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 285 | * Lookup for a directory index item by name and index number. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 286 | * |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 287 | * @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 Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 301 | */ |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 302 | struct btrfs_dir_item * |
| 303 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 304 | struct btrfs_root *root, |
| 305 | struct btrfs_path *path, u64 dir, |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 306 | u64 index, const char *name, int name_len, |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 307 | int mod) |
| 308 | { |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 309 | struct btrfs_dir_item *di; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 310 | struct btrfs_key key; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 311 | |
| 312 | key.objectid = dir; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 313 | key.type = BTRFS_DIR_INDEX_KEY; |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 314 | key.offset = index; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 315 | |
Filipe Manana | 8dcbc26 | 2021-10-01 13:52:33 +0100 | [diff] [blame] | 316 | 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 Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 323 | struct btrfs_dir_item * |
| 324 | btrfs_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 Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 361 | di = btrfs_match_dir_item_name(root->fs_info, path, |
| 362 | name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 363 | if (di) |
| 364 | return di; |
| 365 | |
| 366 | path->slots[0]++; |
| 367 | } |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 371 | struct 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 Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 377 | struct btrfs_key key; |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 378 | struct btrfs_dir_item *di; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 379 | |
| 380 | key.objectid = dir; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 381 | key.type = BTRFS_XATTR_ITEM_KEY; |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 382 | key.offset = btrfs_name_hash(name, name_len); |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 383 | |
| 384 | di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod); |
| 385 | if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 386 | return NULL; |
| 387 | |
Marcos Paulo de Souza | a7d1c5d | 2021-07-26 16:19:09 -0300 | [diff] [blame] | 388 | return di; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 391 | /* |
| 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 Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 396 | struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info, |
Filipe Manana | 5f5bc6b | 2014-11-09 08:38:39 +0000 | [diff] [blame] | 397 | struct btrfs_path *path, |
| 398 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 399 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 400 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 401 | unsigned long name_ptr; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 402 | u32 total_len; |
| 403 | u32 cur = 0; |
| 404 | u32 this_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 405 | struct extent_buffer *leaf; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 406 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 407 | leaf = path->nodes[0]; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 408 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 409 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 410 | total_len = btrfs_item_size(leaf, path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 411 | while (cur < total_len) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 412 | this_len = sizeof(*dir_item) + |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 413 | btrfs_dir_name_len(leaf, dir_item) + |
| 414 | btrfs_dir_data_len(leaf, dir_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 415 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 416 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 417 | if (btrfs_dir_name_len(leaf, dir_item) == name_len && |
| 418 | memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 419 | 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 Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 426 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 427 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 428 | /* |
| 429 | * given a pointer into a directory item, delete it. This |
| 430 | * handles items that have more than one entry in them. |
| 431 | */ |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 432 | int 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 Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 438 | struct extent_buffer *leaf; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 439 | u32 sub_item_len; |
| 440 | u32 item_len; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 441 | int ret = 0; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 442 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 443 | leaf = path->nodes[0]; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 444 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) + |
| 445 | btrfs_dir_data_len(leaf, di); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 446 | item_len = btrfs_item_size(leaf, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 447 | if (sub_item_len == item_len) { |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 448 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 449 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 450 | /* 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 Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 456 | item_len - (ptr + sub_item_len - start)); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 457 | btrfs_truncate_item(path, item_len - sub_item_len, 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 458 | } |
Andi Kleen | 411fc6b | 2010-10-29 15:14:31 -0400 | [diff] [blame] | 459 | return ret; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 460 | } |