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