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 11 /* 12 * insert a name into a directory, doing overflow properly if there is a hash 13 * collision. data_size indicates how big the item inserted should be. On 14 * success a struct btrfs_dir_item pointer is returned, otherwise it is 15 * an ERR_PTR. 16 * 17 * The name is not copied into the dir item, you have to do that yourself. 18 */ 19 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle 20 *trans, 21 struct btrfs_root *root, 22 struct btrfs_path *path, 23 struct btrfs_key *cpu_key, 24 u32 data_size, 25 const char *name, 26 int name_len) 27 { 28 struct btrfs_fs_info *fs_info = root->fs_info; 29 int ret; 30 char *ptr; 31 struct extent_buffer *leaf; 32 33 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 34 if (ret == -EEXIST) { 35 struct btrfs_dir_item *di; 36 di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 37 if (di) 38 return ERR_PTR(-EEXIST); 39 btrfs_extend_item(path, data_size); 40 } else if (ret < 0) 41 return ERR_PTR(ret); 42 WARN_ON(ret > 0); 43 leaf = path->nodes[0]; 44 ptr = btrfs_item_ptr(leaf, path->slots[0], char); 45 ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0])); 46 ptr += btrfs_item_size(leaf, path->slots[0]) - data_size; 47 return (struct btrfs_dir_item *)ptr; 48 } 49 50 /* 51 * xattrs work a lot like directories, this inserts an xattr item 52 * into the tree 53 */ 54 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, 55 struct btrfs_root *root, 56 struct btrfs_path *path, u64 objectid, 57 const char *name, u16 name_len, 58 const void *data, u16 data_len) 59 { 60 int ret = 0; 61 struct btrfs_dir_item *dir_item; 62 unsigned long name_ptr, data_ptr; 63 struct btrfs_key key, location; 64 struct btrfs_disk_key disk_key; 65 struct extent_buffer *leaf; 66 u32 data_size; 67 68 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) 69 return -ENOSPC; 70 71 key.objectid = objectid; 72 key.type = BTRFS_XATTR_ITEM_KEY; 73 key.offset = btrfs_name_hash(name, name_len); 74 75 data_size = sizeof(*dir_item) + name_len + data_len; 76 dir_item = insert_with_overflow(trans, root, path, &key, data_size, 77 name, name_len); 78 if (IS_ERR(dir_item)) 79 return PTR_ERR(dir_item); 80 memset(&location, 0, sizeof(location)); 81 82 leaf = path->nodes[0]; 83 btrfs_cpu_key_to_disk(&disk_key, &location); 84 btrfs_set_dir_item_key(leaf, dir_item, &disk_key); 85 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR); 86 btrfs_set_dir_name_len(leaf, dir_item, name_len); 87 btrfs_set_dir_transid(leaf, dir_item, trans->transid); 88 btrfs_set_dir_data_len(leaf, dir_item, data_len); 89 name_ptr = (unsigned long)(dir_item + 1); 90 data_ptr = (unsigned long)((char *)name_ptr + name_len); 91 92 write_extent_buffer(leaf, name, name_ptr, name_len); 93 write_extent_buffer(leaf, data, data_ptr, data_len); 94 btrfs_mark_buffer_dirty(path->nodes[0]); 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, const char *name, 108 int name_len, struct btrfs_inode *dir, 109 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_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_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 leaf = path->nodes[0]; 143 btrfs_set_dir_item_key(leaf, dir_item, &disk_key); 144 btrfs_set_dir_type(leaf, dir_item, type); 145 btrfs_set_dir_data_len(leaf, dir_item, 0); 146 btrfs_set_dir_name_len(leaf, dir_item, name_len); 147 btrfs_set_dir_transid(leaf, dir_item, trans->transid); 148 name_ptr = (unsigned long)(dir_item + 1); 149 150 write_extent_buffer(leaf, name, name_ptr, name_len); 151 btrfs_mark_buffer_dirty(leaf); 152 153 second_insert: 154 /* FIXME, use some real flag for selecting the extra index */ 155 if (root == root->fs_info->tree_root) { 156 ret = 0; 157 goto out_free; 158 } 159 btrfs_release_path(path); 160 161 ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir, 162 &disk_key, type, index); 163 out_free: 164 btrfs_free_path(path); 165 if (ret) 166 return ret; 167 if (ret2) 168 return ret2; 169 return 0; 170 } 171 172 static struct btrfs_dir_item *btrfs_lookup_match_dir( 173 struct btrfs_trans_handle *trans, 174 struct btrfs_root *root, struct btrfs_path *path, 175 struct btrfs_key *key, const char *name, 176 int name_len, int mod) 177 { 178 const int ins_len = (mod < 0 ? -1 : 0); 179 const int cow = (mod != 0); 180 int ret; 181 182 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow); 183 if (ret < 0) 184 return ERR_PTR(ret); 185 if (ret > 0) 186 return ERR_PTR(-ENOENT); 187 188 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len); 189 } 190 191 /* 192 * Lookup for a directory item by name. 193 * 194 * @trans: The transaction handle to use. Can be NULL if @mod is 0. 195 * @root: The root of the target tree. 196 * @path: Path to use for the search. 197 * @dir: The inode number (objectid) of the directory. 198 * @name: The name associated to the directory entry we are looking for. 199 * @name_len: The length of the name. 200 * @mod: Used to indicate if the tree search is meant for a read only 201 * lookup, for a modification lookup or for a deletion lookup, so 202 * its value should be 0, 1 or -1, respectively. 203 * 204 * Returns: NULL if the dir item does not exists, an error pointer if an error 205 * happened, or a pointer to a dir item if a dir item exists for the given name. 206 */ 207 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, 208 struct btrfs_root *root, 209 struct btrfs_path *path, u64 dir, 210 const char *name, int name_len, 211 int mod) 212 { 213 struct btrfs_key key; 214 struct btrfs_dir_item *di; 215 216 key.objectid = dir; 217 key.type = BTRFS_DIR_ITEM_KEY; 218 key.offset = btrfs_name_hash(name, name_len); 219 220 di = btrfs_lookup_match_dir(trans, root, path, &key, name, 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 char *name, int name_len) 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_len); 245 246 di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0); 247 if (IS_ERR(di)) { 248 ret = PTR_ERR(di); 249 /* Nothing found, we're safe */ 250 if (ret == -ENOENT) { 251 ret = 0; 252 goto out; 253 } 254 255 if (ret < 0) 256 goto out; 257 } 258 259 /* we found an item, look for our name in the item */ 260 if (di) { 261 /* our exact name was found */ 262 ret = -EEXIST; 263 goto out; 264 } 265 266 /* 267 * see if there is room in the item to insert this 268 * name 269 */ 270 data_size = sizeof(*di) + name_len; 271 leaf = path->nodes[0]; 272 slot = path->slots[0]; 273 if (data_size + btrfs_item_size(leaf, slot) + 274 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 275 ret = -EOVERFLOW; 276 } else { 277 /* plenty of insertion room */ 278 ret = 0; 279 } 280 out: 281 btrfs_free_path(path); 282 return ret; 283 } 284 285 /* 286 * Lookup for a directory index item by name and index number. 287 * 288 * @trans: The transaction handle to use. Can be NULL if @mod is 0. 289 * @root: The root of the target tree. 290 * @path: Path to use for the search. 291 * @dir: The inode number (objectid) of the directory. 292 * @index: The index number. 293 * @name: The name associated to the directory entry we are looking for. 294 * @name_len: The length of the name. 295 * @mod: Used to indicate if the tree search is meant for a read only 296 * lookup, for a modification lookup or for a deletion lookup, so 297 * its value should be 0, 1 or -1, respectively. 298 * 299 * Returns: NULL if the dir index item does not exists, an error pointer if an 300 * error happened, or a pointer to a dir item if the dir index item exists and 301 * matches the criteria (name and index number). 302 */ 303 struct btrfs_dir_item * 304 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, 305 struct btrfs_root *root, 306 struct btrfs_path *path, u64 dir, 307 u64 index, const char *name, int name_len, 308 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_len, mod); 318 if (di == ERR_PTR(-ENOENT)) 319 return NULL; 320 321 return di; 322 } 323 324 struct btrfs_dir_item * 325 btrfs_search_dir_index_item(struct btrfs_root *root, 326 struct btrfs_path *path, u64 dirid, 327 const char *name, int name_len) 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(root->fs_info, path, 342 name, name_len); 343 if (di) 344 return di; 345 } 346 /* Adjust return code if the key was not found in the next leaf. */ 347 if (ret > 0) 348 ret = 0; 349 350 return ERR_PTR(ret); 351 } 352 353 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, 354 struct btrfs_root *root, 355 struct btrfs_path *path, u64 dir, 356 const char *name, u16 name_len, 357 int mod) 358 { 359 struct btrfs_key key; 360 struct btrfs_dir_item *di; 361 362 key.objectid = dir; 363 key.type = BTRFS_XATTR_ITEM_KEY; 364 key.offset = btrfs_name_hash(name, name_len); 365 366 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod); 367 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT) 368 return NULL; 369 370 return di; 371 } 372 373 /* 374 * helper function to look at the directory item pointed to by 'path' 375 * this walks through all the entries in a dir item and finds one 376 * for a specific name. 377 */ 378 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info, 379 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 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(path, item_len - sub_item_len, 1); 440 } 441 return ret; 442 } 443