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