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 #include "delayed-inode.h" 13 14 /* 15 * insert a name into a directory, doing overflow properly if there is a hash 16 * collision. data_size indicates how big the item inserted should be. On 17 * success a struct btrfs_dir_item pointer is returned, otherwise it is 18 * an ERR_PTR. 19 * 20 * The name is not copied into the dir item, you have to do that yourself. 21 */ 22 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle 23 *trans, 24 struct btrfs_root *root, 25 struct btrfs_path *path, 26 const struct btrfs_key *cpu_key, 27 u32 data_size, 28 const char *name, 29 int name_len) 30 { 31 int ret; 32 char *ptr; 33 struct extent_buffer *leaf; 34 35 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 36 if (ret == -EEXIST) { 37 struct btrfs_dir_item *di; 38 di = btrfs_match_dir_item_name(path, name, name_len); 39 if (di) 40 return ERR_PTR(-EEXIST); 41 btrfs_extend_item(trans, path, data_size); 42 } else if (ret < 0) 43 return ERR_PTR(ret); 44 WARN_ON(ret > 0); 45 leaf = path->nodes[0]; 46 ptr = btrfs_item_ptr(leaf, path->slots[0], char); 47 ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0])); 48 ptr += btrfs_item_size(leaf, path->slots[0]) - data_size; 49 return (struct btrfs_dir_item *)ptr; 50 } 51 52 /* 53 * xattrs work a lot like directories, this inserts an xattr item 54 * into the tree 55 */ 56 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, 57 struct btrfs_root *root, 58 struct btrfs_path *path, u64 objectid, 59 const char *name, u16 name_len, 60 const void *data, u16 data_len) 61 { 62 int ret = 0; 63 struct btrfs_dir_item *dir_item; 64 unsigned long name_ptr, data_ptr; 65 struct btrfs_key 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 82 leaf = path->nodes[0]; 83 memzero_extent_buffer(leaf, (unsigned long)dir_item + 84 offsetof(struct btrfs_dir_item, location), 85 sizeof(struct btrfs_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 */ 107 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, 108 const struct fscrypt_str *name, 109 struct btrfs_inode *dir, 110 const struct btrfs_key *location, u8 type, 111 u64 index, 112 struct btrfs_dir_index_prealloc *prealloc) 113 { 114 int ret = 0; 115 int ret2 = 0; 116 struct btrfs_root *root = dir->root; 117 BTRFS_PATH_AUTO_FREE(path); 118 struct btrfs_dir_item *dir_item; 119 struct extent_buffer *leaf; 120 unsigned long name_ptr; 121 struct btrfs_key key; 122 struct btrfs_disk_key disk_key; 123 u32 data_size; 124 const bool need_delayed_index = (root != root->fs_info->tree_root); 125 126 key.objectid = btrfs_ino(dir); 127 key.type = BTRFS_DIR_ITEM_KEY; 128 key.offset = btrfs_name_hash(name->name, name->len); 129 130 path = btrfs_alloc_path(); 131 if (!path) { 132 ret = -ENOMEM; 133 goto out_free_prealloc; 134 } 135 136 btrfs_cpu_key_to_disk(&disk_key, location); 137 138 /* Pre-allocate the delayed dir index before modifying the btree. */ 139 if (need_delayed_index && !prealloc) { 140 prealloc = btrfs_prealloc_delayed_dir_index(dir, name->name, name->len); 141 if (IS_ERR(prealloc)) 142 return PTR_ERR(prealloc); 143 } 144 145 data_size = sizeof(*dir_item) + name->len; 146 dir_item = insert_with_overflow(trans, root, path, &key, data_size, 147 name->name, name->len); 148 if (IS_ERR(dir_item)) { 149 ret = PTR_ERR(dir_item); 150 if (ret == -EEXIST) 151 goto second_insert; 152 goto out_free_prealloc; 153 } 154 155 if (IS_ENCRYPTED(&dir->vfs_inode)) 156 type |= BTRFS_FT_ENCRYPTED; 157 158 leaf = path->nodes[0]; 159 btrfs_set_dir_item_key(leaf, dir_item, &disk_key); 160 btrfs_set_dir_flags(leaf, dir_item, type); 161 btrfs_set_dir_data_len(leaf, dir_item, 0); 162 btrfs_set_dir_name_len(leaf, dir_item, name->len); 163 btrfs_set_dir_transid(leaf, dir_item, trans->transid); 164 name_ptr = (unsigned long)(dir_item + 1); 165 166 write_extent_buffer(leaf, name->name, name_ptr, name->len); 167 168 second_insert: 169 if (!need_delayed_index) { 170 ret = 0; 171 goto out_free_prealloc; 172 } 173 btrfs_release_path(path); 174 175 ret2 = btrfs_insert_delayed_dir_index_prealloc(trans, dir, prealloc, 176 &disk_key, type, index); 177 if (ret) 178 return ret; 179 return ret2; 180 181 out_free_prealloc: 182 btrfs_free_delayed_dir_index_prealloc(trans, prealloc); 183 return ret; 184 } 185 186 static struct btrfs_dir_item *btrfs_lookup_match_dir( 187 struct btrfs_trans_handle *trans, 188 struct btrfs_root *root, struct btrfs_path *path, 189 struct btrfs_key *key, const char *name, 190 int name_len, int mod) 191 { 192 const int ins_len = (mod < 0 ? -1 : 0); 193 const int cow = (mod != 0); 194 int ret; 195 196 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow); 197 if (ret < 0) 198 return ERR_PTR(ret); 199 if (ret > 0) 200 return ERR_PTR(-ENOENT); 201 202 return btrfs_match_dir_item_name(path, name, name_len); 203 } 204 205 /* 206 * Lookup for a directory item by name. 207 * 208 * @trans: The transaction handle to use. Can be NULL if @mod is 0. 209 * @root: The root of the target tree. 210 * @path: Path to use for the search. 211 * @dir: The inode number (objectid) of the directory. 212 * @name: The name associated to the directory entry we are looking for. 213 * @name_len: The length of the name. 214 * @mod: Used to indicate if the tree search is meant for a read only 215 * lookup, for a modification lookup or for a deletion lookup, so 216 * its value should be 0, 1 or -1, respectively. 217 * 218 * Returns: NULL if the dir item does not exists, an error pointer if an error 219 * happened, or a pointer to a dir item if a dir item exists for the given name. 220 */ 221 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, 222 struct btrfs_root *root, 223 struct btrfs_path *path, u64 dir, 224 const struct fscrypt_str *name, 225 int mod) 226 { 227 struct btrfs_key key; 228 struct btrfs_dir_item *di; 229 230 key.objectid = dir; 231 key.type = BTRFS_DIR_ITEM_KEY; 232 key.offset = btrfs_name_hash(name->name, name->len); 233 234 di = btrfs_lookup_match_dir(trans, root, path, &key, name->name, 235 name->len, mod); 236 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) 237 return NULL; 238 239 return di; 240 } 241 242 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir_ino, 243 const struct fscrypt_str *name) 244 { 245 int ret; 246 struct btrfs_key key; 247 struct btrfs_dir_item *di; 248 int data_size; 249 struct extent_buffer *leaf; 250 int slot; 251 BTRFS_PATH_AUTO_FREE(path); 252 253 path = btrfs_alloc_path(); 254 if (!path) 255 return -ENOMEM; 256 257 key.objectid = dir_ino; 258 key.type = BTRFS_DIR_ITEM_KEY; 259 key.offset = btrfs_name_hash(name->name, name->len); 260 261 di = btrfs_lookup_match_dir(NULL, root, path, &key, name->name, 262 name->len, 0); 263 if (IS_ERR(di)) { 264 ret = PTR_ERR(di); 265 /* Nothing found, we're safe */ 266 if (ret == -ENOENT) 267 return 0; 268 return ret; 269 } 270 271 /* we found an item, look for our name in the item */ 272 if (di) { 273 /* our exact name was found */ 274 return -EEXIST; 275 } 276 277 /* See if there is room in the item to insert this name. */ 278 data_size = sizeof(*di) + name->len; 279 leaf = path->nodes[0]; 280 slot = path->slots[0]; 281 if (data_size + btrfs_item_size(leaf, slot) + 282 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 283 return -EOVERFLOW; 284 } 285 286 /* Plenty of insertion room. */ 287 return 0; 288 } 289 290 /* 291 * Lookup for a directory index item by name and index number. 292 * 293 * @trans: The transaction handle to use. Can be NULL if @mod is 0. 294 * @root: The root of the target tree. 295 * @path: Path to use for the search. 296 * @dir: The inode number (objectid) of the directory. 297 * @index: The index number. 298 * @name: The name associated to the directory entry we are looking for. 299 * @name_len: The length of the name. 300 * @mod: Used to indicate if the tree search is meant for a read only 301 * lookup, for a modification lookup or for a deletion lookup, so 302 * its value should be 0, 1 or -1, respectively. 303 * 304 * Returns: NULL if the dir index item does not exists, an error pointer if an 305 * error happened, or a pointer to a dir item if the dir index item exists and 306 * matches the criteria (name and index number). 307 */ 308 struct btrfs_dir_item * 309 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, 310 struct btrfs_root *root, 311 struct btrfs_path *path, u64 dir, 312 u64 index, const struct fscrypt_str *name, int mod) 313 { 314 struct btrfs_dir_item *di; 315 struct btrfs_key key; 316 317 key.objectid = dir; 318 key.type = BTRFS_DIR_INDEX_KEY; 319 key.offset = index; 320 321 di = btrfs_lookup_match_dir(trans, root, path, &key, name->name, 322 name->len, mod); 323 if (di == ERR_PTR(-ENOENT)) 324 return NULL; 325 326 return di; 327 } 328 329 struct btrfs_dir_item * 330 btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path, 331 u64 dirid, const struct fscrypt_str *name) 332 { 333 struct btrfs_dir_item *di; 334 struct btrfs_key key; 335 int ret; 336 337 key.objectid = dirid; 338 key.type = BTRFS_DIR_INDEX_KEY; 339 key.offset = 0; 340 341 btrfs_for_each_slot(root, &key, &key, path, ret) { 342 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY) 343 break; 344 345 di = btrfs_match_dir_item_name(path, name->name, name->len); 346 if (di) 347 return di; 348 } 349 /* Adjust return code if the key was not found in the next leaf. */ 350 if (ret >= 0) 351 ret = -ENOENT; 352 353 return ERR_PTR(ret); 354 } 355 356 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, 357 struct btrfs_root *root, 358 struct btrfs_path *path, u64 dir, 359 const char *name, u16 name_len, 360 int mod) 361 { 362 struct btrfs_key key; 363 struct btrfs_dir_item *di; 364 365 key.objectid = dir; 366 key.type = BTRFS_XATTR_ITEM_KEY; 367 key.offset = btrfs_name_hash(name, name_len); 368 369 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod); 370 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) 371 return NULL; 372 373 return di; 374 } 375 376 /* 377 * helper function to look at the directory item pointed to by 'path' 378 * this walks through all the entries in a dir item and finds one 379 * for a specific name. 380 */ 381 struct btrfs_dir_item *btrfs_match_dir_item_name(const struct btrfs_path *path, 382 const char *name, int name_len) 383 { 384 struct btrfs_dir_item *dir_item; 385 unsigned long name_ptr; 386 u32 total_len; 387 u32 cur = 0; 388 u32 this_len; 389 struct extent_buffer *leaf; 390 391 leaf = path->nodes[0]; 392 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); 393 394 total_len = btrfs_item_size(leaf, path->slots[0]); 395 while (cur < total_len) { 396 this_len = sizeof(*dir_item) + 397 btrfs_dir_name_len(leaf, dir_item) + 398 btrfs_dir_data_len(leaf, dir_item); 399 name_ptr = (unsigned long)(dir_item + 1); 400 401 if (btrfs_dir_name_len(leaf, dir_item) == name_len && 402 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) 403 return dir_item; 404 405 cur += this_len; 406 dir_item = (struct btrfs_dir_item *)((char *)dir_item + 407 this_len); 408 } 409 return NULL; 410 } 411 412 /* 413 * given a pointer into a directory item, delete it. This 414 * handles items that have more than one entry in them. 415 */ 416 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, 417 struct btrfs_root *root, 418 struct btrfs_path *path, 419 const struct btrfs_dir_item *di) 420 { 421 422 struct extent_buffer *leaf; 423 u32 sub_item_len; 424 u32 item_len; 425 int ret = 0; 426 427 leaf = path->nodes[0]; 428 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) + 429 btrfs_dir_data_len(leaf, di); 430 item_len = btrfs_item_size(leaf, path->slots[0]); 431 if (sub_item_len == item_len) { 432 ret = btrfs_del_item(trans, root, path); 433 } else { 434 /* MARKER */ 435 unsigned long ptr = (unsigned long)di; 436 unsigned long start; 437 438 start = btrfs_item_ptr_offset(leaf, path->slots[0]); 439 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, 440 item_len - (ptr + sub_item_len - start)); 441 btrfs_truncate_item(trans, path, item_len - sub_item_len, 1); 442 } 443 return ret; 444 } 445