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 | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 6 | #include "ctree.h" |
Josef Bacik | 26c2c45 | 2021-12-03 17:18:03 -0500 | [diff] [blame^] | 7 | #include "inode-item.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 8 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 9 | #include "transaction.h" |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 10 | #include "print-tree.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 11 | |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 12 | struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf, |
| 13 | int slot, const char *name, |
| 14 | int name_len) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 15 | { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 16 | struct btrfs_inode_ref *ref; |
| 17 | unsigned long ptr; |
| 18 | unsigned long name_ptr; |
| 19 | u32 item_size; |
| 20 | u32 cur_offset = 0; |
| 21 | int len; |
| 22 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 23 | item_size = btrfs_item_size(leaf, slot); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 24 | ptr = btrfs_item_ptr_offset(leaf, slot); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 25 | while (cur_offset < item_size) { |
| 26 | ref = (struct btrfs_inode_ref *)(ptr + cur_offset); |
| 27 | len = btrfs_inode_ref_name_len(leaf, ref); |
| 28 | name_ptr = (unsigned long)(ref + 1); |
| 29 | cur_offset += len + sizeof(*ref); |
| 30 | if (len != name_len) |
| 31 | continue; |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 32 | if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
| 33 | return ref; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 34 | } |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 35 | return NULL; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 38 | struct btrfs_inode_extref *btrfs_find_name_in_ext_backref( |
| 39 | struct extent_buffer *leaf, int slot, u64 ref_objectid, |
| 40 | const char *name, int name_len) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 41 | { |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 42 | struct btrfs_inode_extref *extref; |
| 43 | unsigned long ptr; |
| 44 | unsigned long name_ptr; |
| 45 | u32 item_size; |
| 46 | u32 cur_offset = 0; |
| 47 | int ref_name_len; |
| 48 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 49 | item_size = btrfs_item_size(leaf, slot); |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 50 | ptr = btrfs_item_ptr_offset(leaf, slot); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * Search all extended backrefs in this item. We're only |
| 54 | * looking through any collisions so most of the time this is |
| 55 | * just going to compare against one buffer. If all is well, |
| 56 | * we'll return success and the inode ref object. |
| 57 | */ |
| 58 | while (cur_offset < item_size) { |
| 59 | extref = (struct btrfs_inode_extref *) (ptr + cur_offset); |
| 60 | name_ptr = (unsigned long)(&extref->name); |
| 61 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 62 | |
| 63 | if (ref_name_len == name_len && |
| 64 | btrfs_inode_extref_parent(leaf, extref) == ref_objectid && |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 65 | (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)) |
| 66 | return extref; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 67 | |
| 68 | cur_offset += ref_name_len + sizeof(*extref); |
| 69 | } |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 70 | return NULL; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 73 | /* Returns NULL if no extref found */ |
| 74 | struct btrfs_inode_extref * |
| 75 | btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans, |
| 76 | struct btrfs_root *root, |
| 77 | struct btrfs_path *path, |
| 78 | const char *name, int name_len, |
| 79 | u64 inode_objectid, u64 ref_objectid, int ins_len, |
| 80 | int cow) |
| 81 | { |
| 82 | int ret; |
| 83 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 84 | |
| 85 | key.objectid = inode_objectid; |
| 86 | key.type = BTRFS_INODE_EXTREF_KEY; |
| 87 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); |
| 88 | |
| 89 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 90 | if (ret < 0) |
| 91 | return ERR_PTR(ret); |
| 92 | if (ret > 0) |
| 93 | return NULL; |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 94 | return btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], |
| 95 | ref_objectid, name, name_len); |
| 96 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 99 | static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, |
| 100 | struct btrfs_root *root, |
| 101 | const char *name, int name_len, |
| 102 | u64 inode_objectid, u64 ref_objectid, |
| 103 | u64 *index) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 104 | { |
| 105 | struct btrfs_path *path; |
| 106 | struct btrfs_key key; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 107 | struct btrfs_inode_extref *extref; |
| 108 | struct extent_buffer *leaf; |
| 109 | int ret; |
| 110 | int del_len = name_len + sizeof(*extref); |
| 111 | unsigned long ptr; |
| 112 | unsigned long item_start; |
| 113 | u32 item_size; |
| 114 | |
| 115 | key.objectid = inode_objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 116 | key.type = BTRFS_INODE_EXTREF_KEY; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 117 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); |
| 118 | |
| 119 | path = btrfs_alloc_path(); |
| 120 | if (!path) |
| 121 | return -ENOMEM; |
| 122 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 123 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 124 | if (ret > 0) |
| 125 | ret = -ENOENT; |
| 126 | if (ret < 0) |
| 127 | goto out; |
| 128 | |
| 129 | /* |
| 130 | * Sanity check - did we find the right item for this name? |
| 131 | * This should always succeed so error here will make the FS |
| 132 | * readonly. |
| 133 | */ |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 134 | extref = btrfs_find_name_in_ext_backref(path->nodes[0], path->slots[0], |
| 135 | ref_objectid, name, name_len); |
| 136 | if (!extref) { |
Anand Jain | 34d9700 | 2016-03-16 16:43:06 +0800 | [diff] [blame] | 137 | btrfs_handle_fs_error(root->fs_info, -ENOENT, NULL); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 138 | ret = -EROFS; |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | leaf = path->nodes[0]; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 143 | item_size = btrfs_item_size(leaf, path->slots[0]); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 144 | if (index) |
| 145 | *index = btrfs_inode_extref_index(leaf, extref); |
| 146 | |
| 147 | if (del_len == item_size) { |
| 148 | /* |
| 149 | * Common case only one ref in the item, remove the |
| 150 | * whole item. |
| 151 | */ |
| 152 | ret = btrfs_del_item(trans, root, path); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | ptr = (unsigned long)extref; |
| 157 | item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 158 | |
| 159 | memmove_extent_buffer(leaf, ptr, ptr + del_len, |
| 160 | item_size - (ptr + del_len - item_start)); |
| 161 | |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 162 | btrfs_truncate_item(path, item_size - del_len, 1); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 163 | |
| 164 | out: |
| 165 | btrfs_free_path(path); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, |
| 171 | struct btrfs_root *root, |
| 172 | const char *name, int name_len, |
| 173 | u64 inode_objectid, u64 ref_objectid, u64 *index) |
| 174 | { |
| 175 | struct btrfs_path *path; |
| 176 | struct btrfs_key key; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 177 | struct btrfs_inode_ref *ref; |
| 178 | struct extent_buffer *leaf; |
| 179 | unsigned long ptr; |
| 180 | unsigned long item_start; |
| 181 | u32 item_size; |
| 182 | u32 sub_item_len; |
| 183 | int ret; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 184 | int search_ext_refs = 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 185 | int del_len = name_len + sizeof(*ref); |
| 186 | |
| 187 | key.objectid = inode_objectid; |
| 188 | key.offset = ref_objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 189 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 190 | |
| 191 | path = btrfs_alloc_path(); |
| 192 | if (!path) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 196 | if (ret > 0) { |
| 197 | ret = -ENOENT; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 198 | search_ext_refs = 1; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 199 | goto out; |
| 200 | } else if (ret < 0) { |
| 201 | goto out; |
| 202 | } |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 203 | |
| 204 | ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], name, |
| 205 | name_len); |
| 206 | if (!ref) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 207 | ret = -ENOENT; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 208 | search_ext_refs = 1; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 209 | goto out; |
| 210 | } |
| 211 | leaf = path->nodes[0]; |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 212 | item_size = btrfs_item_size(leaf, path->slots[0]); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 213 | |
| 214 | if (index) |
| 215 | *index = btrfs_inode_ref_index(leaf, ref); |
| 216 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 217 | if (del_len == item_size) { |
| 218 | ret = btrfs_del_item(trans, root, path); |
| 219 | goto out; |
| 220 | } |
| 221 | ptr = (unsigned long)ref; |
| 222 | sub_item_len = name_len + sizeof(*ref); |
| 223 | item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 224 | memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, |
| 225 | item_size - (ptr + sub_item_len - item_start)); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 226 | btrfs_truncate_item(path, item_size - sub_item_len, 1); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 227 | out: |
| 228 | btrfs_free_path(path); |
| 229 | |
| 230 | if (search_ext_refs) { |
| 231 | /* |
| 232 | * No refs were found, or we could not find the |
| 233 | * name in our ref array. Find and remove the extended |
| 234 | * inode ref then. |
| 235 | */ |
| 236 | return btrfs_del_inode_extref(trans, root, name, name_len, |
| 237 | inode_objectid, ref_objectid, index); |
| 238 | } |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree. |
| 245 | * |
| 246 | * The caller must have checked against BTRFS_LINK_MAX already. |
| 247 | */ |
| 248 | static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, |
| 249 | struct btrfs_root *root, |
| 250 | const char *name, int name_len, |
| 251 | u64 inode_objectid, u64 ref_objectid, u64 index) |
| 252 | { |
| 253 | struct btrfs_inode_extref *extref; |
| 254 | int ret; |
| 255 | int ins_len = name_len + sizeof(*extref); |
| 256 | unsigned long ptr; |
| 257 | struct btrfs_path *path; |
| 258 | struct btrfs_key key; |
| 259 | struct extent_buffer *leaf; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 260 | |
| 261 | key.objectid = inode_objectid; |
| 262 | key.type = BTRFS_INODE_EXTREF_KEY; |
| 263 | key.offset = btrfs_extref_hash(ref_objectid, name, name_len); |
| 264 | |
| 265 | path = btrfs_alloc_path(); |
| 266 | if (!path) |
| 267 | return -ENOMEM; |
| 268 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 269 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 270 | ins_len); |
| 271 | if (ret == -EEXIST) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 272 | if (btrfs_find_name_in_ext_backref(path->nodes[0], |
| 273 | path->slots[0], |
| 274 | ref_objectid, |
Nikolay Borisov | 6ff49c6 | 2019-08-27 14:46:29 +0300 | [diff] [blame] | 275 | name, name_len)) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 276 | goto out; |
| 277 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 278 | btrfs_extend_item(path, ins_len); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 279 | ret = 0; |
| 280 | } |
| 281 | if (ret < 0) |
| 282 | goto out; |
| 283 | |
| 284 | leaf = path->nodes[0]; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 285 | ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 286 | ptr += btrfs_item_size(leaf, path->slots[0]) - ins_len; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 287 | extref = (struct btrfs_inode_extref *)ptr; |
| 288 | |
| 289 | btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len); |
| 290 | btrfs_set_inode_extref_index(path->nodes[0], extref, index); |
| 291 | btrfs_set_inode_extref_parent(path->nodes[0], extref, ref_objectid); |
| 292 | |
| 293 | ptr = (unsigned long)&extref->name; |
| 294 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 295 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 296 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 297 | out: |
| 298 | btrfs_free_path(path); |
| 299 | return ret; |
| 300 | } |
| 301 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 302 | /* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */ |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 303 | int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, |
| 304 | struct btrfs_root *root, |
| 305 | const char *name, int name_len, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 306 | u64 inode_objectid, u64 ref_objectid, u64 index) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 307 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 308 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 309 | struct btrfs_path *path; |
| 310 | struct btrfs_key key; |
| 311 | struct btrfs_inode_ref *ref; |
| 312 | unsigned long ptr; |
| 313 | int ret; |
| 314 | int ins_len = name_len + sizeof(*ref); |
| 315 | |
| 316 | key.objectid = inode_objectid; |
| 317 | key.offset = ref_objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 318 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 319 | |
| 320 | path = btrfs_alloc_path(); |
| 321 | if (!path) |
| 322 | return -ENOMEM; |
| 323 | |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 324 | path->skip_release_on_error = 1; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 325 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 326 | ins_len); |
| 327 | if (ret == -EEXIST) { |
| 328 | u32 old_size; |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 329 | ref = btrfs_find_name_in_backref(path->nodes[0], path->slots[0], |
| 330 | name, name_len); |
| 331 | if (ref) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 332 | goto out; |
| 333 | |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 334 | old_size = btrfs_item_size(path->nodes[0], path->slots[0]); |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 335 | btrfs_extend_item(path, ins_len); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 336 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 337 | struct btrfs_inode_ref); |
| 338 | ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size); |
| 339 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 340 | btrfs_set_inode_ref_index(path->nodes[0], ref, index); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 341 | ptr = (unsigned long)(ref + 1); |
| 342 | ret = 0; |
| 343 | } else if (ret < 0) { |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 344 | if (ret == -EOVERFLOW) { |
Filipe Manana | 1f250e9 | 2018-02-28 15:56:10 +0000 | [diff] [blame] | 345 | if (btrfs_find_name_in_backref(path->nodes[0], |
| 346 | path->slots[0], |
Nikolay Borisov | 9bb8407 | 2019-08-27 14:46:28 +0300 | [diff] [blame] | 347 | name, name_len)) |
Filipe Manana | df8d116 | 2015-01-14 01:52:25 +0000 | [diff] [blame] | 348 | ret = -EEXIST; |
| 349 | else |
| 350 | ret = -EMLINK; |
| 351 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 352 | goto out; |
| 353 | } else { |
| 354 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 355 | struct btrfs_inode_ref); |
| 356 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 357 | btrfs_set_inode_ref_index(path->nodes[0], ref, index); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 358 | ptr = (unsigned long)(ref + 1); |
| 359 | } |
| 360 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 361 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 362 | |
| 363 | out: |
| 364 | btrfs_free_path(path); |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 365 | |
| 366 | if (ret == -EMLINK) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 367 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 368 | /* We ran out of space in the ref array. Need to |
| 369 | * add an extended ref. */ |
| 370 | if (btrfs_super_incompat_flags(disk_super) |
| 371 | & BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF) |
| 372 | ret = btrfs_insert_inode_extref(trans, root, name, |
| 373 | name_len, |
| 374 | inode_objectid, |
| 375 | ref_objectid, index); |
| 376 | } |
| 377 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 378 | return ret; |
| 379 | } |
| 380 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 381 | int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, |
| 382 | struct btrfs_root *root, |
| 383 | struct btrfs_path *path, u64 objectid) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 384 | { |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 385 | struct btrfs_key key; |
| 386 | int ret; |
| 387 | key.objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 388 | key.type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 389 | key.offset = 0; |
| 390 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 391 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 392 | sizeof(struct btrfs_inode_item)); |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 396 | int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 397 | *root, struct btrfs_path *path, |
| 398 | struct btrfs_key *location, int mod) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 399 | { |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 400 | int ins_len = mod < 0 ? -1 : 0; |
| 401 | int cow = mod != 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 402 | int ret; |
| 403 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 404 | struct extent_buffer *leaf; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 405 | struct btrfs_key found_key; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 406 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 407 | ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 408 | if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY && |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 409 | location->offset == (u64)-1 && path->slots[0] != 0) { |
| 410 | slot = path->slots[0] - 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 411 | leaf = path->nodes[0]; |
| 412 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 413 | if (found_key.objectid == location->objectid && |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 414 | found_key.type == location->type) { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 415 | path->slots[0]--; |
| 416 | return 0; |
| 417 | } |
| 418 | } |
| 419 | return ret; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 420 | } |