^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * insert a name into a directory, doing overflow properly if there is a hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * collision. data_size indicates how big the item inserted should be. On
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * success a struct btrfs_dir_item pointer is returned, otherwise it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * an ERR_PTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * The name is not copied into the dir item, you have to do that yourself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct btrfs_key *cpu_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u32 data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return ERR_PTR(-EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) btrfs_extend_item(path, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } else if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) WARN_ON(ret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) item = btrfs_item_nr(path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ptr = btrfs_item_ptr(leaf, path->slots[0], char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) BUG_ON(data_size > btrfs_item_size(leaf, item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ptr += btrfs_item_size(leaf, item) - data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return (struct btrfs_dir_item *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * xattrs work a lot like directories, this inserts an xattr item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * into the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct btrfs_path *path, u64 objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) const char *name, u16 name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) const void *data, u16 data_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct btrfs_dir_item *dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned long name_ptr, data_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct btrfs_key key, location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u32 data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) key.objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) key.offset = btrfs_name_hash(name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) data_size = sizeof(*dir_item) + name_len + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dir_item = insert_with_overflow(trans, root, path, &key, data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (IS_ERR(dir_item))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return PTR_ERR(dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) memset(&location, 0, sizeof(location));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) btrfs_cpu_key_to_disk(&disk_key, &location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) btrfs_set_dir_name_len(leaf, dir_item, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) btrfs_set_dir_transid(leaf, dir_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) btrfs_set_dir_data_len(leaf, dir_item, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) name_ptr = (unsigned long)(dir_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) data_ptr = (unsigned long)((char *)name_ptr + name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) write_extent_buffer(leaf, name, name_ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) write_extent_buffer(leaf, data, data_ptr, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * insert a directory item in the tree, doing all the magic for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * both indexes. 'dir' indicates which objectid to insert it into,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * 'location' is the key to stuff into the directory item, 'type' is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * type of the inode we're pointing to, and 'index' is the sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * to use for the second index (if one is created).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Will return 0 or -ENOMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int name_len, struct btrfs_inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct btrfs_key *location, u8 type, u64 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int ret2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct btrfs_root *root = dir->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct btrfs_dir_item *dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long name_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u32 data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) key.objectid = btrfs_ino(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) key.type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) key.offset = btrfs_name_hash(name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) btrfs_cpu_key_to_disk(&disk_key, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) data_size = sizeof(*dir_item) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dir_item = insert_with_overflow(trans, root, path, &key, data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (IS_ERR(dir_item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret = PTR_ERR(dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (ret == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) goto second_insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) btrfs_set_dir_type(leaf, dir_item, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) btrfs_set_dir_data_len(leaf, dir_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) btrfs_set_dir_name_len(leaf, dir_item, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) btrfs_set_dir_transid(leaf, dir_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) name_ptr = (unsigned long)(dir_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) write_extent_buffer(leaf, name, name_ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) second_insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* FIXME, use some real flag for selecting the extra index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (root == root->fs_info->tree_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &disk_key, type, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (ret2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * lookup a directory item based on name. 'dir' is the objectid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * we're searching in, and 'mod' tells us if you plan on deleting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * item (use mod < 0) or changing the options (use mod > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct btrfs_path *path, u64 dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) const char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int ins_len = mod < 0 ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int cow = mod != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) key.objectid = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) key.type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) key.offset = btrfs_name_hash(name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) key.objectid = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) key.type = BTRFS_DIR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) key.offset = btrfs_name_hash(name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* return back any errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* nothing found, we're safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* we found an item, look for our name in the item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) di = btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* our exact name was found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * see if there is room in the item to insert this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) data_size = sizeof(*di) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (data_size + btrfs_item_size_nr(leaf, slot) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* plenty of insertion room */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * lookup a directory item based on index. 'dir' is the objectid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * we're searching in, and 'mod' tells us if you plan on deleting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * item (use mod < 0) or changing the options (use mod > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * The name is used to make sure the index really points to the name you were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct btrfs_dir_item *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct btrfs_path *path, u64 dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) u64 objectid, const char *name, int name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int ins_len = mod < 0 ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int cow = mod != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) key.objectid = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) key.type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) key.offset = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct btrfs_dir_item *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) btrfs_search_dir_index_item(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct btrfs_path *path, u64 dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) u32 nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) key.type = BTRFS_DIR_INDEX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (path->slots[0] >= nritems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) di = btrfs_match_dir_item_name(root->fs_info, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct btrfs_path *path, u64 dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) const char *name, u16 name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int ins_len = mod < 0 ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int cow = mod != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) key.objectid = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) key.offset = btrfs_name_hash(name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * helper function to look at the directory item pointed to by 'path'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * this walks through all the entries in a dir item and finds one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * for a specific name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) const char *name, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct btrfs_dir_item *dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) unsigned long name_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) u32 total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) u32 cur = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) u32 this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) total_len = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) while (cur < total_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) this_len = sizeof(*dir_item) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) btrfs_dir_name_len(leaf, dir_item) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) btrfs_dir_data_len(leaf, dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) name_ptr = (unsigned long)(dir_item + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) cur += this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) dir_item = (struct btrfs_dir_item *)((char *)dir_item +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) this_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * given a pointer into a directory item, delete it. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * handles items that have more than one entry in them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct btrfs_dir_item *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) u32 sub_item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) u32 item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) item_len = btrfs_item_size_nr(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (sub_item_len == item_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ret = btrfs_del_item(trans, root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* MARKER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) unsigned long ptr = (unsigned long)di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) start = btrfs_item_ptr_offset(leaf, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) item_len - (ptr + sub_item_len - start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) btrfs_truncate_item(path, item_len - sub_item_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }