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