Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Fujitsu. All rights reserved. |
| 3 | * Written by Miao Xie <miaox@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public |
| 7 | * License v2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public |
| 15 | * License along with this program; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 021110-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/slab.h> |
| 21 | #include "delayed-inode.h" |
| 22 | #include "disk-io.h" |
| 23 | #include "transaction.h" |
| 24 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 25 | #define BTRFS_DELAYED_WRITEBACK 512 |
| 26 | #define BTRFS_DELAYED_BACKGROUND 128 |
| 27 | #define BTRFS_DELAYED_BATCH 16 |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 28 | |
| 29 | static struct kmem_cache *delayed_node_cache; |
| 30 | |
| 31 | int __init btrfs_delayed_inode_init(void) |
| 32 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 33 | delayed_node_cache = kmem_cache_create("btrfs_delayed_node", |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 34 | sizeof(struct btrfs_delayed_node), |
| 35 | 0, |
| 36 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 37 | NULL); |
| 38 | if (!delayed_node_cache) |
| 39 | return -ENOMEM; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | void btrfs_delayed_inode_exit(void) |
| 44 | { |
| 45 | if (delayed_node_cache) |
| 46 | kmem_cache_destroy(delayed_node_cache); |
| 47 | } |
| 48 | |
| 49 | static inline void btrfs_init_delayed_node( |
| 50 | struct btrfs_delayed_node *delayed_node, |
| 51 | struct btrfs_root *root, u64 inode_id) |
| 52 | { |
| 53 | delayed_node->root = root; |
| 54 | delayed_node->inode_id = inode_id; |
| 55 | atomic_set(&delayed_node->refs, 0); |
| 56 | delayed_node->count = 0; |
| 57 | delayed_node->in_list = 0; |
| 58 | delayed_node->inode_dirty = 0; |
| 59 | delayed_node->ins_root = RB_ROOT; |
| 60 | delayed_node->del_root = RB_ROOT; |
| 61 | mutex_init(&delayed_node->mutex); |
| 62 | delayed_node->index_cnt = 0; |
| 63 | INIT_LIST_HEAD(&delayed_node->n_list); |
| 64 | INIT_LIST_HEAD(&delayed_node->p_list); |
| 65 | delayed_node->bytes_reserved = 0; |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 66 | memset(&delayed_node->inode_item, 0, sizeof(delayed_node->inode_item)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline int btrfs_is_continuous_delayed_item( |
| 70 | struct btrfs_delayed_item *item1, |
| 71 | struct btrfs_delayed_item *item2) |
| 72 | { |
| 73 | if (item1->key.type == BTRFS_DIR_INDEX_KEY && |
| 74 | item1->key.objectid == item2->key.objectid && |
| 75 | item1->key.type == item2->key.type && |
| 76 | item1->key.offset + 1 == item2->key.offset) |
| 77 | return 1; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static inline struct btrfs_delayed_root *btrfs_get_delayed_root( |
| 82 | struct btrfs_root *root) |
| 83 | { |
| 84 | return root->fs_info->delayed_root; |
| 85 | } |
| 86 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 87 | static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode) |
| 88 | { |
| 89 | struct btrfs_inode *btrfs_inode = BTRFS_I(inode); |
| 90 | struct btrfs_root *root = btrfs_inode->root; |
| 91 | u64 ino = btrfs_ino(inode); |
| 92 | struct btrfs_delayed_node *node; |
| 93 | |
| 94 | node = ACCESS_ONCE(btrfs_inode->delayed_node); |
| 95 | if (node) { |
| 96 | atomic_inc(&node->refs); |
| 97 | return node; |
| 98 | } |
| 99 | |
| 100 | spin_lock(&root->inode_lock); |
| 101 | node = radix_tree_lookup(&root->delayed_nodes_tree, ino); |
| 102 | if (node) { |
| 103 | if (btrfs_inode->delayed_node) { |
| 104 | atomic_inc(&node->refs); /* can be accessed */ |
| 105 | BUG_ON(btrfs_inode->delayed_node != node); |
| 106 | spin_unlock(&root->inode_lock); |
| 107 | return node; |
| 108 | } |
| 109 | btrfs_inode->delayed_node = node; |
| 110 | atomic_inc(&node->refs); /* can be accessed */ |
| 111 | atomic_inc(&node->refs); /* cached in the inode */ |
| 112 | spin_unlock(&root->inode_lock); |
| 113 | return node; |
| 114 | } |
| 115 | spin_unlock(&root->inode_lock); |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 120 | /* Will return either the node or PTR_ERR(-ENOMEM) */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 121 | static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( |
| 122 | struct inode *inode) |
| 123 | { |
| 124 | struct btrfs_delayed_node *node; |
| 125 | struct btrfs_inode *btrfs_inode = BTRFS_I(inode); |
| 126 | struct btrfs_root *root = btrfs_inode->root; |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 127 | u64 ino = btrfs_ino(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 128 | int ret; |
| 129 | |
| 130 | again: |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 131 | node = btrfs_get_delayed_node(inode); |
| 132 | if (node) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 133 | return node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 134 | |
| 135 | node = kmem_cache_alloc(delayed_node_cache, GFP_NOFS); |
| 136 | if (!node) |
| 137 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 138 | btrfs_init_delayed_node(node, root, ino); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 139 | |
| 140 | atomic_inc(&node->refs); /* cached in the btrfs inode */ |
| 141 | atomic_inc(&node->refs); /* can be accessed */ |
| 142 | |
| 143 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 144 | if (ret) { |
| 145 | kmem_cache_free(delayed_node_cache, node); |
| 146 | return ERR_PTR(ret); |
| 147 | } |
| 148 | |
| 149 | spin_lock(&root->inode_lock); |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 150 | ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 151 | if (ret == -EEXIST) { |
| 152 | kmem_cache_free(delayed_node_cache, node); |
| 153 | spin_unlock(&root->inode_lock); |
| 154 | radix_tree_preload_end(); |
| 155 | goto again; |
| 156 | } |
| 157 | btrfs_inode->delayed_node = node; |
| 158 | spin_unlock(&root->inode_lock); |
| 159 | radix_tree_preload_end(); |
| 160 | |
| 161 | return node; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Call it when holding delayed_node->mutex |
| 166 | * |
| 167 | * If mod = 1, add this node into the prepared list. |
| 168 | */ |
| 169 | static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root, |
| 170 | struct btrfs_delayed_node *node, |
| 171 | int mod) |
| 172 | { |
| 173 | spin_lock(&root->lock); |
| 174 | if (node->in_list) { |
| 175 | if (!list_empty(&node->p_list)) |
| 176 | list_move_tail(&node->p_list, &root->prepare_list); |
| 177 | else if (mod) |
| 178 | list_add_tail(&node->p_list, &root->prepare_list); |
| 179 | } else { |
| 180 | list_add_tail(&node->n_list, &root->node_list); |
| 181 | list_add_tail(&node->p_list, &root->prepare_list); |
| 182 | atomic_inc(&node->refs); /* inserted into list */ |
| 183 | root->nodes++; |
| 184 | node->in_list = 1; |
| 185 | } |
| 186 | spin_unlock(&root->lock); |
| 187 | } |
| 188 | |
| 189 | /* Call it when holding delayed_node->mutex */ |
| 190 | static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root, |
| 191 | struct btrfs_delayed_node *node) |
| 192 | { |
| 193 | spin_lock(&root->lock); |
| 194 | if (node->in_list) { |
| 195 | root->nodes--; |
| 196 | atomic_dec(&node->refs); /* not in the list */ |
| 197 | list_del_init(&node->n_list); |
| 198 | if (!list_empty(&node->p_list)) |
| 199 | list_del_init(&node->p_list); |
| 200 | node->in_list = 0; |
| 201 | } |
| 202 | spin_unlock(&root->lock); |
| 203 | } |
| 204 | |
| 205 | struct btrfs_delayed_node *btrfs_first_delayed_node( |
| 206 | struct btrfs_delayed_root *delayed_root) |
| 207 | { |
| 208 | struct list_head *p; |
| 209 | struct btrfs_delayed_node *node = NULL; |
| 210 | |
| 211 | spin_lock(&delayed_root->lock); |
| 212 | if (list_empty(&delayed_root->node_list)) |
| 213 | goto out; |
| 214 | |
| 215 | p = delayed_root->node_list.next; |
| 216 | node = list_entry(p, struct btrfs_delayed_node, n_list); |
| 217 | atomic_inc(&node->refs); |
| 218 | out: |
| 219 | spin_unlock(&delayed_root->lock); |
| 220 | |
| 221 | return node; |
| 222 | } |
| 223 | |
| 224 | struct btrfs_delayed_node *btrfs_next_delayed_node( |
| 225 | struct btrfs_delayed_node *node) |
| 226 | { |
| 227 | struct btrfs_delayed_root *delayed_root; |
| 228 | struct list_head *p; |
| 229 | struct btrfs_delayed_node *next = NULL; |
| 230 | |
| 231 | delayed_root = node->root->fs_info->delayed_root; |
| 232 | spin_lock(&delayed_root->lock); |
| 233 | if (!node->in_list) { /* not in the list */ |
| 234 | if (list_empty(&delayed_root->node_list)) |
| 235 | goto out; |
| 236 | p = delayed_root->node_list.next; |
| 237 | } else if (list_is_last(&node->n_list, &delayed_root->node_list)) |
| 238 | goto out; |
| 239 | else |
| 240 | p = node->n_list.next; |
| 241 | |
| 242 | next = list_entry(p, struct btrfs_delayed_node, n_list); |
| 243 | atomic_inc(&next->refs); |
| 244 | out: |
| 245 | spin_unlock(&delayed_root->lock); |
| 246 | |
| 247 | return next; |
| 248 | } |
| 249 | |
| 250 | static void __btrfs_release_delayed_node( |
| 251 | struct btrfs_delayed_node *delayed_node, |
| 252 | int mod) |
| 253 | { |
| 254 | struct btrfs_delayed_root *delayed_root; |
| 255 | |
| 256 | if (!delayed_node) |
| 257 | return; |
| 258 | |
| 259 | delayed_root = delayed_node->root->fs_info->delayed_root; |
| 260 | |
| 261 | mutex_lock(&delayed_node->mutex); |
| 262 | if (delayed_node->count) |
| 263 | btrfs_queue_delayed_node(delayed_root, delayed_node, mod); |
| 264 | else |
| 265 | btrfs_dequeue_delayed_node(delayed_root, delayed_node); |
| 266 | mutex_unlock(&delayed_node->mutex); |
| 267 | |
| 268 | if (atomic_dec_and_test(&delayed_node->refs)) { |
| 269 | struct btrfs_root *root = delayed_node->root; |
| 270 | spin_lock(&root->inode_lock); |
| 271 | if (atomic_read(&delayed_node->refs) == 0) { |
| 272 | radix_tree_delete(&root->delayed_nodes_tree, |
| 273 | delayed_node->inode_id); |
| 274 | kmem_cache_free(delayed_node_cache, delayed_node); |
| 275 | } |
| 276 | spin_unlock(&root->inode_lock); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node) |
| 281 | { |
| 282 | __btrfs_release_delayed_node(node, 0); |
| 283 | } |
| 284 | |
| 285 | struct btrfs_delayed_node *btrfs_first_prepared_delayed_node( |
| 286 | struct btrfs_delayed_root *delayed_root) |
| 287 | { |
| 288 | struct list_head *p; |
| 289 | struct btrfs_delayed_node *node = NULL; |
| 290 | |
| 291 | spin_lock(&delayed_root->lock); |
| 292 | if (list_empty(&delayed_root->prepare_list)) |
| 293 | goto out; |
| 294 | |
| 295 | p = delayed_root->prepare_list.next; |
| 296 | list_del_init(p); |
| 297 | node = list_entry(p, struct btrfs_delayed_node, p_list); |
| 298 | atomic_inc(&node->refs); |
| 299 | out: |
| 300 | spin_unlock(&delayed_root->lock); |
| 301 | |
| 302 | return node; |
| 303 | } |
| 304 | |
| 305 | static inline void btrfs_release_prepared_delayed_node( |
| 306 | struct btrfs_delayed_node *node) |
| 307 | { |
| 308 | __btrfs_release_delayed_node(node, 1); |
| 309 | } |
| 310 | |
| 311 | struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len) |
| 312 | { |
| 313 | struct btrfs_delayed_item *item; |
| 314 | item = kmalloc(sizeof(*item) + data_len, GFP_NOFS); |
| 315 | if (item) { |
| 316 | item->data_len = data_len; |
| 317 | item->ins_or_del = 0; |
| 318 | item->bytes_reserved = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 319 | item->delayed_node = NULL; |
| 320 | atomic_set(&item->refs, 1); |
| 321 | } |
| 322 | return item; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * __btrfs_lookup_delayed_item - look up the delayed item by key |
| 327 | * @delayed_node: pointer to the delayed node |
| 328 | * @key: the key to look up |
| 329 | * @prev: used to store the prev item if the right item isn't found |
| 330 | * @next: used to store the next item if the right item isn't found |
| 331 | * |
| 332 | * Note: if we don't find the right item, we will return the prev item and |
| 333 | * the next item. |
| 334 | */ |
| 335 | static struct btrfs_delayed_item *__btrfs_lookup_delayed_item( |
| 336 | struct rb_root *root, |
| 337 | struct btrfs_key *key, |
| 338 | struct btrfs_delayed_item **prev, |
| 339 | struct btrfs_delayed_item **next) |
| 340 | { |
| 341 | struct rb_node *node, *prev_node = NULL; |
| 342 | struct btrfs_delayed_item *delayed_item = NULL; |
| 343 | int ret = 0; |
| 344 | |
| 345 | node = root->rb_node; |
| 346 | |
| 347 | while (node) { |
| 348 | delayed_item = rb_entry(node, struct btrfs_delayed_item, |
| 349 | rb_node); |
| 350 | prev_node = node; |
| 351 | ret = btrfs_comp_cpu_keys(&delayed_item->key, key); |
| 352 | if (ret < 0) |
| 353 | node = node->rb_right; |
| 354 | else if (ret > 0) |
| 355 | node = node->rb_left; |
| 356 | else |
| 357 | return delayed_item; |
| 358 | } |
| 359 | |
| 360 | if (prev) { |
| 361 | if (!prev_node) |
| 362 | *prev = NULL; |
| 363 | else if (ret < 0) |
| 364 | *prev = delayed_item; |
| 365 | else if ((node = rb_prev(prev_node)) != NULL) { |
| 366 | *prev = rb_entry(node, struct btrfs_delayed_item, |
| 367 | rb_node); |
| 368 | } else |
| 369 | *prev = NULL; |
| 370 | } |
| 371 | |
| 372 | if (next) { |
| 373 | if (!prev_node) |
| 374 | *next = NULL; |
| 375 | else if (ret > 0) |
| 376 | *next = delayed_item; |
| 377 | else if ((node = rb_next(prev_node)) != NULL) { |
| 378 | *next = rb_entry(node, struct btrfs_delayed_item, |
| 379 | rb_node); |
| 380 | } else |
| 381 | *next = NULL; |
| 382 | } |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item( |
| 387 | struct btrfs_delayed_node *delayed_node, |
| 388 | struct btrfs_key *key) |
| 389 | { |
| 390 | struct btrfs_delayed_item *item; |
| 391 | |
| 392 | item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key, |
| 393 | NULL, NULL); |
| 394 | return item; |
| 395 | } |
| 396 | |
| 397 | struct btrfs_delayed_item *__btrfs_lookup_delayed_deletion_item( |
| 398 | struct btrfs_delayed_node *delayed_node, |
| 399 | struct btrfs_key *key) |
| 400 | { |
| 401 | struct btrfs_delayed_item *item; |
| 402 | |
| 403 | item = __btrfs_lookup_delayed_item(&delayed_node->del_root, key, |
| 404 | NULL, NULL); |
| 405 | return item; |
| 406 | } |
| 407 | |
| 408 | struct btrfs_delayed_item *__btrfs_search_delayed_insertion_item( |
| 409 | struct btrfs_delayed_node *delayed_node, |
| 410 | struct btrfs_key *key) |
| 411 | { |
| 412 | struct btrfs_delayed_item *item, *next; |
| 413 | |
| 414 | item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key, |
| 415 | NULL, &next); |
| 416 | if (!item) |
| 417 | item = next; |
| 418 | |
| 419 | return item; |
| 420 | } |
| 421 | |
| 422 | struct btrfs_delayed_item *__btrfs_search_delayed_deletion_item( |
| 423 | struct btrfs_delayed_node *delayed_node, |
| 424 | struct btrfs_key *key) |
| 425 | { |
| 426 | struct btrfs_delayed_item *item, *next; |
| 427 | |
| 428 | item = __btrfs_lookup_delayed_item(&delayed_node->del_root, key, |
| 429 | NULL, &next); |
| 430 | if (!item) |
| 431 | item = next; |
| 432 | |
| 433 | return item; |
| 434 | } |
| 435 | |
| 436 | static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node, |
| 437 | struct btrfs_delayed_item *ins, |
| 438 | int action) |
| 439 | { |
| 440 | struct rb_node **p, *node; |
| 441 | struct rb_node *parent_node = NULL; |
| 442 | struct rb_root *root; |
| 443 | struct btrfs_delayed_item *item; |
| 444 | int cmp; |
| 445 | |
| 446 | if (action == BTRFS_DELAYED_INSERTION_ITEM) |
| 447 | root = &delayed_node->ins_root; |
| 448 | else if (action == BTRFS_DELAYED_DELETION_ITEM) |
| 449 | root = &delayed_node->del_root; |
| 450 | else |
| 451 | BUG(); |
| 452 | p = &root->rb_node; |
| 453 | node = &ins->rb_node; |
| 454 | |
| 455 | while (*p) { |
| 456 | parent_node = *p; |
| 457 | item = rb_entry(parent_node, struct btrfs_delayed_item, |
| 458 | rb_node); |
| 459 | |
| 460 | cmp = btrfs_comp_cpu_keys(&item->key, &ins->key); |
| 461 | if (cmp < 0) |
| 462 | p = &(*p)->rb_right; |
| 463 | else if (cmp > 0) |
| 464 | p = &(*p)->rb_left; |
| 465 | else |
| 466 | return -EEXIST; |
| 467 | } |
| 468 | |
| 469 | rb_link_node(node, parent_node, p); |
| 470 | rb_insert_color(node, root); |
| 471 | ins->delayed_node = delayed_node; |
| 472 | ins->ins_or_del = action; |
| 473 | |
| 474 | if (ins->key.type == BTRFS_DIR_INDEX_KEY && |
| 475 | action == BTRFS_DELAYED_INSERTION_ITEM && |
| 476 | ins->key.offset >= delayed_node->index_cnt) |
| 477 | delayed_node->index_cnt = ins->key.offset + 1; |
| 478 | |
| 479 | delayed_node->count++; |
| 480 | atomic_inc(&delayed_node->root->fs_info->delayed_root->items); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node, |
| 485 | struct btrfs_delayed_item *item) |
| 486 | { |
| 487 | return __btrfs_add_delayed_item(node, item, |
| 488 | BTRFS_DELAYED_INSERTION_ITEM); |
| 489 | } |
| 490 | |
| 491 | static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node, |
| 492 | struct btrfs_delayed_item *item) |
| 493 | { |
| 494 | return __btrfs_add_delayed_item(node, item, |
| 495 | BTRFS_DELAYED_DELETION_ITEM); |
| 496 | } |
| 497 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 498 | static void finish_one_item(struct btrfs_delayed_root *delayed_root) |
| 499 | { |
| 500 | int seq = atomic_inc_return(&delayed_root->items_seq); |
| 501 | if ((atomic_dec_return(&delayed_root->items) < |
| 502 | BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) && |
| 503 | waitqueue_active(&delayed_root->wait)) |
| 504 | wake_up(&delayed_root->wait); |
| 505 | } |
| 506 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 507 | static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item) |
| 508 | { |
| 509 | struct rb_root *root; |
| 510 | struct btrfs_delayed_root *delayed_root; |
| 511 | |
| 512 | delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root; |
| 513 | |
| 514 | BUG_ON(!delayed_root); |
| 515 | BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM && |
| 516 | delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM); |
| 517 | |
| 518 | if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM) |
| 519 | root = &delayed_item->delayed_node->ins_root; |
| 520 | else |
| 521 | root = &delayed_item->delayed_node->del_root; |
| 522 | |
| 523 | rb_erase(&delayed_item->rb_node, root); |
| 524 | delayed_item->delayed_node->count--; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 525 | |
| 526 | finish_one_item(delayed_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void btrfs_release_delayed_item(struct btrfs_delayed_item *item) |
| 530 | { |
| 531 | if (item) { |
| 532 | __btrfs_remove_delayed_item(item); |
| 533 | if (atomic_dec_and_test(&item->refs)) |
| 534 | kfree(item); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item( |
| 539 | struct btrfs_delayed_node *delayed_node) |
| 540 | { |
| 541 | struct rb_node *p; |
| 542 | struct btrfs_delayed_item *item = NULL; |
| 543 | |
| 544 | p = rb_first(&delayed_node->ins_root); |
| 545 | if (p) |
| 546 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 547 | |
| 548 | return item; |
| 549 | } |
| 550 | |
| 551 | struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item( |
| 552 | struct btrfs_delayed_node *delayed_node) |
| 553 | { |
| 554 | struct rb_node *p; |
| 555 | struct btrfs_delayed_item *item = NULL; |
| 556 | |
| 557 | p = rb_first(&delayed_node->del_root); |
| 558 | if (p) |
| 559 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 560 | |
| 561 | return item; |
| 562 | } |
| 563 | |
| 564 | struct btrfs_delayed_item *__btrfs_next_delayed_item( |
| 565 | struct btrfs_delayed_item *item) |
| 566 | { |
| 567 | struct rb_node *p; |
| 568 | struct btrfs_delayed_item *next = NULL; |
| 569 | |
| 570 | p = rb_next(&item->rb_node); |
| 571 | if (p) |
| 572 | next = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 573 | |
| 574 | return next; |
| 575 | } |
| 576 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 577 | static inline struct btrfs_root *btrfs_get_fs_root(struct btrfs_root *root, |
| 578 | u64 root_id) |
| 579 | { |
| 580 | struct btrfs_key root_key; |
| 581 | |
| 582 | if (root->objectid == root_id) |
| 583 | return root; |
| 584 | |
| 585 | root_key.objectid = root_id; |
| 586 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 587 | root_key.offset = (u64)-1; |
| 588 | return btrfs_read_fs_root_no_name(root->fs_info, &root_key); |
| 589 | } |
| 590 | |
| 591 | static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, |
| 592 | struct btrfs_root *root, |
| 593 | struct btrfs_delayed_item *item) |
| 594 | { |
| 595 | struct btrfs_block_rsv *src_rsv; |
| 596 | struct btrfs_block_rsv *dst_rsv; |
| 597 | u64 num_bytes; |
| 598 | int ret; |
| 599 | |
| 600 | if (!trans->bytes_reserved) |
| 601 | return 0; |
| 602 | |
| 603 | src_rsv = trans->block_rsv; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 604 | dst_rsv = &root->fs_info->delayed_block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 605 | |
| 606 | num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 607 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 608 | if (!ret) { |
| 609 | trace_btrfs_space_reservation(root->fs_info, "delayed_item", |
| 610 | item->key.objectid, |
| 611 | num_bytes, 1); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 612 | item->bytes_reserved = num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 613 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 614 | |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, |
| 619 | struct btrfs_delayed_item *item) |
| 620 | { |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 621 | struct btrfs_block_rsv *rsv; |
| 622 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 623 | if (!item->bytes_reserved) |
| 624 | return; |
| 625 | |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 626 | rsv = &root->fs_info->delayed_block_rsv; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 627 | trace_btrfs_space_reservation(root->fs_info, "delayed_item", |
| 628 | item->key.objectid, item->bytes_reserved, |
| 629 | 0); |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 630 | btrfs_block_rsv_release(root, rsv, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 631 | item->bytes_reserved); |
| 632 | } |
| 633 | |
| 634 | static int btrfs_delayed_inode_reserve_metadata( |
| 635 | struct btrfs_trans_handle *trans, |
| 636 | struct btrfs_root *root, |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 637 | struct inode *inode, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 638 | struct btrfs_delayed_node *node) |
| 639 | { |
| 640 | struct btrfs_block_rsv *src_rsv; |
| 641 | struct btrfs_block_rsv *dst_rsv; |
| 642 | u64 num_bytes; |
| 643 | int ret; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 644 | bool release = false; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 645 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 646 | src_rsv = trans->block_rsv; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 647 | dst_rsv = &root->fs_info->delayed_block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 648 | |
| 649 | num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 650 | |
| 651 | /* |
| 652 | * btrfs_dirty_inode will update the inode under btrfs_join_transaction |
| 653 | * which doesn't reserve space for speed. This is a problem since we |
| 654 | * still need to reserve space for this update, so try to reserve the |
| 655 | * space. |
| 656 | * |
| 657 | * Now if src_rsv == delalloc_block_rsv we'll let it just steal since |
| 658 | * we're accounted for. |
| 659 | */ |
Chris Mason | e755d9a | 2011-12-15 13:36:29 -0500 | [diff] [blame] | 660 | if (!src_rsv || (!trans->bytes_reserved && |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 661 | src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 662 | ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes, |
| 663 | BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 664 | /* |
| 665 | * Since we're under a transaction reserve_metadata_bytes could |
| 666 | * try to commit the transaction which will make it return |
| 667 | * EAGAIN to make us stop the transaction we have, so return |
| 668 | * ENOSPC instead so that btrfs_dirty_inode knows what to do. |
| 669 | */ |
| 670 | if (ret == -EAGAIN) |
| 671 | ret = -ENOSPC; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 672 | if (!ret) { |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 673 | node->bytes_reserved = num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 674 | trace_btrfs_space_reservation(root->fs_info, |
| 675 | "delayed_inode", |
| 676 | btrfs_ino(inode), |
| 677 | num_bytes, 1); |
| 678 | } |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 679 | return ret; |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 680 | } else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 681 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 682 | if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 683 | &BTRFS_I(inode)->runtime_flags)) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 684 | spin_unlock(&BTRFS_I(inode)->lock); |
| 685 | release = true; |
| 686 | goto migrate; |
| 687 | } |
| 688 | spin_unlock(&BTRFS_I(inode)->lock); |
| 689 | |
| 690 | /* Ok we didn't have space pre-reserved. This shouldn't happen |
| 691 | * too often but it can happen if we do delalloc to an existing |
| 692 | * inode which gets dirtied because of the time update, and then |
| 693 | * isn't touched again until after the transaction commits and |
| 694 | * then we try to write out the data. First try to be nice and |
| 695 | * reserve something strictly for us. If not be a pain and try |
| 696 | * to steal from the delalloc block rsv. |
| 697 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 698 | ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes, |
| 699 | BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 700 | if (!ret) |
| 701 | goto out; |
| 702 | |
| 703 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes); |
| 704 | if (!ret) |
| 705 | goto out; |
| 706 | |
| 707 | /* |
| 708 | * Ok this is a problem, let's just steal from the global rsv |
| 709 | * since this really shouldn't happen that often. |
| 710 | */ |
| 711 | WARN_ON(1); |
| 712 | ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv, |
| 713 | dst_rsv, num_bytes); |
| 714 | goto out; |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 715 | } |
| 716 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 717 | migrate: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 718 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes); |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 719 | |
| 720 | out: |
| 721 | /* |
| 722 | * Migrate only takes a reservation, it doesn't touch the size of the |
| 723 | * block_rsv. This is to simplify people who don't normally have things |
| 724 | * migrated from their block rsv. If they go to release their |
| 725 | * reservation, that will decrease the size as well, so if migrate |
| 726 | * reduced size we'd end up with a negative size. But for the |
| 727 | * delalloc_meta_reserved stuff we will only know to drop 1 reservation, |
| 728 | * but we could in fact do this reserve/migrate dance several times |
| 729 | * between the time we did the original reservation and we'd clean it |
| 730 | * up. So to take care of this, release the space for the meta |
| 731 | * reservation here. I think it may be time for a documentation page on |
| 732 | * how block rsvs. work. |
| 733 | */ |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 734 | if (!ret) { |
| 735 | trace_btrfs_space_reservation(root->fs_info, "delayed_inode", |
| 736 | btrfs_ino(inode), num_bytes, 1); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 737 | node->bytes_reserved = num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 738 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 739 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 740 | if (release) { |
| 741 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 742 | btrfs_ino(inode), num_bytes, 0); |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 743 | btrfs_block_rsv_release(root, src_rsv, num_bytes); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 744 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 745 | |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root, |
| 750 | struct btrfs_delayed_node *node) |
| 751 | { |
| 752 | struct btrfs_block_rsv *rsv; |
| 753 | |
| 754 | if (!node->bytes_reserved) |
| 755 | return; |
| 756 | |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 757 | rsv = &root->fs_info->delayed_block_rsv; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 758 | trace_btrfs_space_reservation(root->fs_info, "delayed_inode", |
| 759 | node->inode_id, node->bytes_reserved, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 760 | btrfs_block_rsv_release(root, rsv, |
| 761 | node->bytes_reserved); |
| 762 | node->bytes_reserved = 0; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * This helper will insert some continuous items into the same leaf according |
| 767 | * to the free space of the leaf. |
| 768 | */ |
| 769 | static int btrfs_batch_insert_items(struct btrfs_trans_handle *trans, |
| 770 | struct btrfs_root *root, |
| 771 | struct btrfs_path *path, |
| 772 | struct btrfs_delayed_item *item) |
| 773 | { |
| 774 | struct btrfs_delayed_item *curr, *next; |
| 775 | int free_space; |
| 776 | int total_data_size = 0, total_size = 0; |
| 777 | struct extent_buffer *leaf; |
| 778 | char *data_ptr; |
| 779 | struct btrfs_key *keys; |
| 780 | u32 *data_size; |
| 781 | struct list_head head; |
| 782 | int slot; |
| 783 | int nitems; |
| 784 | int i; |
| 785 | int ret = 0; |
| 786 | |
| 787 | BUG_ON(!path->nodes[0]); |
| 788 | |
| 789 | leaf = path->nodes[0]; |
| 790 | free_space = btrfs_leaf_free_space(root, leaf); |
| 791 | INIT_LIST_HEAD(&head); |
| 792 | |
| 793 | next = item; |
Chris Mason | 17aca1c | 2011-06-03 01:13:45 -0400 | [diff] [blame] | 794 | nitems = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 795 | |
| 796 | /* |
| 797 | * count the number of the continuous items that we can insert in batch |
| 798 | */ |
| 799 | while (total_size + next->data_len + sizeof(struct btrfs_item) <= |
| 800 | free_space) { |
| 801 | total_data_size += next->data_len; |
| 802 | total_size += next->data_len + sizeof(struct btrfs_item); |
| 803 | list_add_tail(&next->tree_list, &head); |
| 804 | nitems++; |
| 805 | |
| 806 | curr = next; |
| 807 | next = __btrfs_next_delayed_item(curr); |
| 808 | if (!next) |
| 809 | break; |
| 810 | |
| 811 | if (!btrfs_is_continuous_delayed_item(curr, next)) |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | if (!nitems) { |
| 816 | ret = 0; |
| 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * we need allocate some memory space, but it might cause the task |
| 822 | * to sleep, so we set all locked nodes in the path to blocking locks |
| 823 | * first. |
| 824 | */ |
| 825 | btrfs_set_path_blocking(path); |
| 826 | |
| 827 | keys = kmalloc(sizeof(struct btrfs_key) * nitems, GFP_NOFS); |
| 828 | if (!keys) { |
| 829 | ret = -ENOMEM; |
| 830 | goto out; |
| 831 | } |
| 832 | |
| 833 | data_size = kmalloc(sizeof(u32) * nitems, GFP_NOFS); |
| 834 | if (!data_size) { |
| 835 | ret = -ENOMEM; |
| 836 | goto error; |
| 837 | } |
| 838 | |
| 839 | /* get keys of all the delayed items */ |
| 840 | i = 0; |
| 841 | list_for_each_entry(next, &head, tree_list) { |
| 842 | keys[i] = next->key; |
| 843 | data_size[i] = next->data_len; |
| 844 | i++; |
| 845 | } |
| 846 | |
| 847 | /* reset all the locked nodes in the patch to spinning locks. */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 848 | btrfs_clear_path_blocking(path, NULL, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 849 | |
| 850 | /* insert the keys of the items */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 851 | setup_items_for_insert(trans, root, path, keys, data_size, |
| 852 | total_data_size, total_size, nitems); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 853 | |
| 854 | /* insert the dir index items */ |
| 855 | slot = path->slots[0]; |
| 856 | list_for_each_entry_safe(curr, next, &head, tree_list) { |
| 857 | data_ptr = btrfs_item_ptr(leaf, slot, char); |
| 858 | write_extent_buffer(leaf, &curr->data, |
| 859 | (unsigned long)data_ptr, |
| 860 | curr->data_len); |
| 861 | slot++; |
| 862 | |
| 863 | btrfs_delayed_item_release_metadata(root, curr); |
| 864 | |
| 865 | list_del(&curr->tree_list); |
| 866 | btrfs_release_delayed_item(curr); |
| 867 | } |
| 868 | |
| 869 | error: |
| 870 | kfree(data_size); |
| 871 | kfree(keys); |
| 872 | out: |
| 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * This helper can just do simple insertion that needn't extend item for new |
| 878 | * data, such as directory name index insertion, inode insertion. |
| 879 | */ |
| 880 | static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans, |
| 881 | struct btrfs_root *root, |
| 882 | struct btrfs_path *path, |
| 883 | struct btrfs_delayed_item *delayed_item) |
| 884 | { |
| 885 | struct extent_buffer *leaf; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 886 | char *ptr; |
| 887 | int ret; |
| 888 | |
| 889 | ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key, |
| 890 | delayed_item->data_len); |
| 891 | if (ret < 0 && ret != -EEXIST) |
| 892 | return ret; |
| 893 | |
| 894 | leaf = path->nodes[0]; |
| 895 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 896 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 897 | |
| 898 | write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr, |
| 899 | delayed_item->data_len); |
| 900 | btrfs_mark_buffer_dirty(leaf); |
| 901 | |
| 902 | btrfs_delayed_item_release_metadata(root, delayed_item); |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * we insert an item first, then if there are some continuous items, we try |
| 908 | * to insert those items into the same leaf. |
| 909 | */ |
| 910 | static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans, |
| 911 | struct btrfs_path *path, |
| 912 | struct btrfs_root *root, |
| 913 | struct btrfs_delayed_node *node) |
| 914 | { |
| 915 | struct btrfs_delayed_item *curr, *prev; |
| 916 | int ret = 0; |
| 917 | |
| 918 | do_again: |
| 919 | mutex_lock(&node->mutex); |
| 920 | curr = __btrfs_first_delayed_insertion_item(node); |
| 921 | if (!curr) |
| 922 | goto insert_end; |
| 923 | |
| 924 | ret = btrfs_insert_delayed_item(trans, root, path, curr); |
| 925 | if (ret < 0) { |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 926 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 927 | goto insert_end; |
| 928 | } |
| 929 | |
| 930 | prev = curr; |
| 931 | curr = __btrfs_next_delayed_item(prev); |
| 932 | if (curr && btrfs_is_continuous_delayed_item(prev, curr)) { |
| 933 | /* insert the continuous items into the same leaf */ |
| 934 | path->slots[0]++; |
| 935 | btrfs_batch_insert_items(trans, root, path, curr); |
| 936 | } |
| 937 | btrfs_release_delayed_item(prev); |
| 938 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 939 | |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 940 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 941 | mutex_unlock(&node->mutex); |
| 942 | goto do_again; |
| 943 | |
| 944 | insert_end: |
| 945 | mutex_unlock(&node->mutex); |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans, |
| 950 | struct btrfs_root *root, |
| 951 | struct btrfs_path *path, |
| 952 | struct btrfs_delayed_item *item) |
| 953 | { |
| 954 | struct btrfs_delayed_item *curr, *next; |
| 955 | struct extent_buffer *leaf; |
| 956 | struct btrfs_key key; |
| 957 | struct list_head head; |
| 958 | int nitems, i, last_item; |
| 959 | int ret = 0; |
| 960 | |
| 961 | BUG_ON(!path->nodes[0]); |
| 962 | |
| 963 | leaf = path->nodes[0]; |
| 964 | |
| 965 | i = path->slots[0]; |
| 966 | last_item = btrfs_header_nritems(leaf) - 1; |
| 967 | if (i > last_item) |
| 968 | return -ENOENT; /* FIXME: Is errno suitable? */ |
| 969 | |
| 970 | next = item; |
| 971 | INIT_LIST_HEAD(&head); |
| 972 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 973 | nitems = 0; |
| 974 | /* |
| 975 | * count the number of the dir index items that we can delete in batch |
| 976 | */ |
| 977 | while (btrfs_comp_cpu_keys(&next->key, &key) == 0) { |
| 978 | list_add_tail(&next->tree_list, &head); |
| 979 | nitems++; |
| 980 | |
| 981 | curr = next; |
| 982 | next = __btrfs_next_delayed_item(curr); |
| 983 | if (!next) |
| 984 | break; |
| 985 | |
| 986 | if (!btrfs_is_continuous_delayed_item(curr, next)) |
| 987 | break; |
| 988 | |
| 989 | i++; |
| 990 | if (i > last_item) |
| 991 | break; |
| 992 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 993 | } |
| 994 | |
| 995 | if (!nitems) |
| 996 | return 0; |
| 997 | |
| 998 | ret = btrfs_del_items(trans, root, path, path->slots[0], nitems); |
| 999 | if (ret) |
| 1000 | goto out; |
| 1001 | |
| 1002 | list_for_each_entry_safe(curr, next, &head, tree_list) { |
| 1003 | btrfs_delayed_item_release_metadata(root, curr); |
| 1004 | list_del(&curr->tree_list); |
| 1005 | btrfs_release_delayed_item(curr); |
| 1006 | } |
| 1007 | |
| 1008 | out: |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans, |
| 1013 | struct btrfs_path *path, |
| 1014 | struct btrfs_root *root, |
| 1015 | struct btrfs_delayed_node *node) |
| 1016 | { |
| 1017 | struct btrfs_delayed_item *curr, *prev; |
| 1018 | int ret = 0; |
| 1019 | |
| 1020 | do_again: |
| 1021 | mutex_lock(&node->mutex); |
| 1022 | curr = __btrfs_first_delayed_deletion_item(node); |
| 1023 | if (!curr) |
| 1024 | goto delete_fail; |
| 1025 | |
| 1026 | ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1); |
| 1027 | if (ret < 0) |
| 1028 | goto delete_fail; |
| 1029 | else if (ret > 0) { |
| 1030 | /* |
| 1031 | * can't find the item which the node points to, so this node |
| 1032 | * is invalid, just drop it. |
| 1033 | */ |
| 1034 | prev = curr; |
| 1035 | curr = __btrfs_next_delayed_item(prev); |
| 1036 | btrfs_release_delayed_item(prev); |
| 1037 | ret = 0; |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1038 | btrfs_release_path(path); |
Fengguang Wu | 6209526 | 2012-08-04 01:45:02 -0600 | [diff] [blame] | 1039 | if (curr) { |
| 1040 | mutex_unlock(&node->mutex); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1041 | goto do_again; |
Fengguang Wu | 6209526 | 2012-08-04 01:45:02 -0600 | [diff] [blame] | 1042 | } else |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1043 | goto delete_fail; |
| 1044 | } |
| 1045 | |
| 1046 | btrfs_batch_delete_items(trans, root, path, curr); |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1047 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1048 | mutex_unlock(&node->mutex); |
| 1049 | goto do_again; |
| 1050 | |
| 1051 | delete_fail: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1052 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1053 | mutex_unlock(&node->mutex); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node) |
| 1058 | { |
| 1059 | struct btrfs_delayed_root *delayed_root; |
| 1060 | |
| 1061 | if (delayed_node && delayed_node->inode_dirty) { |
| 1062 | BUG_ON(!delayed_node->root); |
| 1063 | delayed_node->inode_dirty = 0; |
| 1064 | delayed_node->count--; |
| 1065 | |
| 1066 | delayed_root = delayed_node->root->fs_info->delayed_root; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1067 | finish_one_item(delayed_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1071 | static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
| 1072 | struct btrfs_root *root, |
| 1073 | struct btrfs_path *path, |
| 1074 | struct btrfs_delayed_node *node) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1075 | { |
| 1076 | struct btrfs_key key; |
| 1077 | struct btrfs_inode_item *inode_item; |
| 1078 | struct extent_buffer *leaf; |
| 1079 | int ret; |
| 1080 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1081 | key.objectid = node->inode_id; |
| 1082 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1083 | key.offset = 0; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1084 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1085 | ret = btrfs_lookup_inode(trans, root, path, &key, 1); |
| 1086 | if (ret > 0) { |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1087 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1088 | return -ENOENT; |
| 1089 | } else if (ret < 0) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | btrfs_unlock_up_safe(path, 1); |
| 1094 | leaf = path->nodes[0]; |
| 1095 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1096 | struct btrfs_inode_item); |
| 1097 | write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item, |
| 1098 | sizeof(struct btrfs_inode_item)); |
| 1099 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1100 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1101 | |
| 1102 | btrfs_delayed_inode_release_metadata(root, node); |
| 1103 | btrfs_release_delayed_inode(node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1108 | static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
| 1109 | struct btrfs_root *root, |
| 1110 | struct btrfs_path *path, |
| 1111 | struct btrfs_delayed_node *node) |
| 1112 | { |
| 1113 | int ret; |
| 1114 | |
| 1115 | mutex_lock(&node->mutex); |
| 1116 | if (!node->inode_dirty) { |
| 1117 | mutex_unlock(&node->mutex); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | ret = __btrfs_update_delayed_inode(trans, root, path, node); |
| 1122 | mutex_unlock(&node->mutex); |
| 1123 | return ret; |
| 1124 | } |
| 1125 | |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1126 | static inline int |
| 1127 | __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
| 1128 | struct btrfs_path *path, |
| 1129 | struct btrfs_delayed_node *node) |
| 1130 | { |
| 1131 | int ret; |
| 1132 | |
| 1133 | ret = btrfs_insert_delayed_items(trans, path, node->root, node); |
| 1134 | if (ret) |
| 1135 | return ret; |
| 1136 | |
| 1137 | ret = btrfs_delete_delayed_items(trans, path, node->root, node); |
| 1138 | if (ret) |
| 1139 | return ret; |
| 1140 | |
| 1141 | ret = btrfs_update_delayed_inode(trans, node->root, path, node); |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1145 | /* |
| 1146 | * Called when committing the transaction. |
| 1147 | * Returns 0 on success. |
| 1148 | * Returns < 0 on error and returns with an aborted transaction with any |
| 1149 | * outstanding delayed items cleaned up. |
| 1150 | */ |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1151 | static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, |
| 1152 | struct btrfs_root *root, int nr) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1153 | { |
| 1154 | struct btrfs_delayed_root *delayed_root; |
| 1155 | struct btrfs_delayed_node *curr_node, *prev_node; |
| 1156 | struct btrfs_path *path; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1157 | struct btrfs_block_rsv *block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1158 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1159 | bool count = (nr > 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1160 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1161 | if (trans->aborted) |
| 1162 | return -EIO; |
| 1163 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1164 | path = btrfs_alloc_path(); |
| 1165 | if (!path) |
| 1166 | return -ENOMEM; |
| 1167 | path->leave_spinning = 1; |
| 1168 | |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1169 | block_rsv = trans->block_rsv; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 1170 | trans->block_rsv = &root->fs_info->delayed_block_rsv; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1171 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1172 | delayed_root = btrfs_get_delayed_root(root); |
| 1173 | |
| 1174 | curr_node = btrfs_first_delayed_node(delayed_root); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1175 | while (curr_node && (!count || (count && nr--))) { |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1176 | ret = __btrfs_commit_inode_delayed_items(trans, path, |
| 1177 | curr_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1178 | if (ret) { |
| 1179 | btrfs_release_delayed_node(curr_node); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1180 | curr_node = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1181 | btrfs_abort_transaction(trans, root, ret); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | prev_node = curr_node; |
| 1186 | curr_node = btrfs_next_delayed_node(curr_node); |
| 1187 | btrfs_release_delayed_node(prev_node); |
| 1188 | } |
| 1189 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1190 | if (curr_node) |
| 1191 | btrfs_release_delayed_node(curr_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1192 | btrfs_free_path(path); |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1193 | trans->block_rsv = block_rsv; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1194 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1195 | return ret; |
| 1196 | } |
| 1197 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1198 | int btrfs_run_delayed_items(struct btrfs_trans_handle *trans, |
| 1199 | struct btrfs_root *root) |
| 1200 | { |
| 1201 | return __btrfs_run_delayed_items(trans, root, -1); |
| 1202 | } |
| 1203 | |
| 1204 | int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, |
| 1205 | struct btrfs_root *root, int nr) |
| 1206 | { |
| 1207 | return __btrfs_run_delayed_items(trans, root, nr); |
| 1208 | } |
| 1209 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1210 | int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
| 1211 | struct inode *inode) |
| 1212 | { |
| 1213 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1214 | struct btrfs_path *path; |
| 1215 | struct btrfs_block_rsv *block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1216 | int ret; |
| 1217 | |
| 1218 | if (!delayed_node) |
| 1219 | return 0; |
| 1220 | |
| 1221 | mutex_lock(&delayed_node->mutex); |
| 1222 | if (!delayed_node->count) { |
| 1223 | mutex_unlock(&delayed_node->mutex); |
| 1224 | btrfs_release_delayed_node(delayed_node); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | mutex_unlock(&delayed_node->mutex); |
| 1228 | |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1229 | path = btrfs_alloc_path(); |
| 1230 | if (!path) |
| 1231 | return -ENOMEM; |
| 1232 | path->leave_spinning = 1; |
| 1233 | |
| 1234 | block_rsv = trans->block_rsv; |
| 1235 | trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; |
| 1236 | |
| 1237 | ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node); |
| 1238 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1239 | btrfs_release_delayed_node(delayed_node); |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1240 | btrfs_free_path(path); |
| 1241 | trans->block_rsv = block_rsv; |
| 1242 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1246 | int btrfs_commit_inode_delayed_inode(struct inode *inode) |
| 1247 | { |
| 1248 | struct btrfs_trans_handle *trans; |
| 1249 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
| 1250 | struct btrfs_path *path; |
| 1251 | struct btrfs_block_rsv *block_rsv; |
| 1252 | int ret; |
| 1253 | |
| 1254 | if (!delayed_node) |
| 1255 | return 0; |
| 1256 | |
| 1257 | mutex_lock(&delayed_node->mutex); |
| 1258 | if (!delayed_node->inode_dirty) { |
| 1259 | mutex_unlock(&delayed_node->mutex); |
| 1260 | btrfs_release_delayed_node(delayed_node); |
| 1261 | return 0; |
| 1262 | } |
| 1263 | mutex_unlock(&delayed_node->mutex); |
| 1264 | |
| 1265 | trans = btrfs_join_transaction(delayed_node->root); |
| 1266 | if (IS_ERR(trans)) { |
| 1267 | ret = PTR_ERR(trans); |
| 1268 | goto out; |
| 1269 | } |
| 1270 | |
| 1271 | path = btrfs_alloc_path(); |
| 1272 | if (!path) { |
| 1273 | ret = -ENOMEM; |
| 1274 | goto trans_out; |
| 1275 | } |
| 1276 | path->leave_spinning = 1; |
| 1277 | |
| 1278 | block_rsv = trans->block_rsv; |
| 1279 | trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; |
| 1280 | |
| 1281 | mutex_lock(&delayed_node->mutex); |
| 1282 | if (delayed_node->inode_dirty) |
| 1283 | ret = __btrfs_update_delayed_inode(trans, delayed_node->root, |
| 1284 | path, delayed_node); |
| 1285 | else |
| 1286 | ret = 0; |
| 1287 | mutex_unlock(&delayed_node->mutex); |
| 1288 | |
| 1289 | btrfs_free_path(path); |
| 1290 | trans->block_rsv = block_rsv; |
| 1291 | trans_out: |
| 1292 | btrfs_end_transaction(trans, delayed_node->root); |
| 1293 | btrfs_btree_balance_dirty(delayed_node->root); |
| 1294 | out: |
| 1295 | btrfs_release_delayed_node(delayed_node); |
| 1296 | |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1300 | void btrfs_remove_delayed_node(struct inode *inode) |
| 1301 | { |
| 1302 | struct btrfs_delayed_node *delayed_node; |
| 1303 | |
| 1304 | delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node); |
| 1305 | if (!delayed_node) |
| 1306 | return; |
| 1307 | |
| 1308 | BTRFS_I(inode)->delayed_node = NULL; |
| 1309 | btrfs_release_delayed_node(delayed_node); |
| 1310 | } |
| 1311 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1312 | struct btrfs_async_delayed_work { |
| 1313 | struct btrfs_delayed_root *delayed_root; |
| 1314 | int nr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1315 | struct btrfs_work work; |
| 1316 | }; |
| 1317 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1318 | static void btrfs_async_run_delayed_root(struct btrfs_work *work) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1319 | { |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1320 | struct btrfs_async_delayed_work *async_work; |
| 1321 | struct btrfs_delayed_root *delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1322 | struct btrfs_trans_handle *trans; |
| 1323 | struct btrfs_path *path; |
| 1324 | struct btrfs_delayed_node *delayed_node = NULL; |
| 1325 | struct btrfs_root *root; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1326 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1327 | int total_done = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1328 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1329 | async_work = container_of(work, struct btrfs_async_delayed_work, work); |
| 1330 | delayed_root = async_work->delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1331 | |
| 1332 | path = btrfs_alloc_path(); |
| 1333 | if (!path) |
| 1334 | goto out; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1335 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1336 | again: |
| 1337 | if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2) |
| 1338 | goto free_path; |
| 1339 | |
| 1340 | delayed_node = btrfs_first_prepared_delayed_node(delayed_root); |
| 1341 | if (!delayed_node) |
| 1342 | goto free_path; |
| 1343 | |
| 1344 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1345 | root = delayed_node->root; |
| 1346 | |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1347 | trans = btrfs_join_transaction(root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1348 | if (IS_ERR(trans)) |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1349 | goto release_path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1350 | |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1351 | block_rsv = trans->block_rsv; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 1352 | trans->block_rsv = &root->fs_info->delayed_block_rsv; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1353 | |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1354 | __btrfs_commit_inode_delayed_items(trans, path, delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1355 | /* |
| 1356 | * Maybe new delayed items have been inserted, so we need requeue |
| 1357 | * the work. Besides that, we must dequeue the empty delayed nodes |
| 1358 | * to avoid the race between delayed items balance and the worker. |
| 1359 | * The race like this: |
| 1360 | * Task1 Worker thread |
| 1361 | * count == 0, needn't requeue |
| 1362 | * also needn't insert the |
| 1363 | * delayed node into prepare |
| 1364 | * list again. |
| 1365 | * add lots of delayed items |
| 1366 | * queue the delayed node |
| 1367 | * already in the list, |
| 1368 | * and not in the prepare |
| 1369 | * list, it means the delayed |
| 1370 | * node is being dealt with |
| 1371 | * by the worker. |
| 1372 | * do delayed items balance |
| 1373 | * the delayed node is being |
| 1374 | * dealt with by the worker |
| 1375 | * now, just wait. |
| 1376 | * the worker goto idle. |
| 1377 | * Task1 will sleep until the transaction is commited. |
| 1378 | */ |
| 1379 | mutex_lock(&delayed_node->mutex); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1380 | btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1381 | mutex_unlock(&delayed_node->mutex); |
| 1382 | |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1383 | trans->block_rsv = block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1384 | btrfs_end_transaction_dmeta(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 1385 | btrfs_btree_balance_dirty_nodelay(root); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1386 | |
| 1387 | release_path: |
| 1388 | btrfs_release_path(path); |
| 1389 | total_done++; |
| 1390 | |
| 1391 | btrfs_release_prepared_delayed_node(delayed_node); |
| 1392 | if (async_work->nr == 0 || total_done < async_work->nr) |
| 1393 | goto again; |
| 1394 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1395 | free_path: |
| 1396 | btrfs_free_path(path); |
| 1397 | out: |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1398 | wake_up(&delayed_root->wait); |
| 1399 | kfree(async_work); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1402 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1403 | static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, |
| 1404 | struct btrfs_root *root, int nr) |
| 1405 | { |
| 1406 | struct btrfs_async_delayed_work *async_work; |
| 1407 | |
| 1408 | if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1409 | return 0; |
| 1410 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1411 | async_work = kmalloc(sizeof(*async_work), GFP_NOFS); |
| 1412 | if (!async_work) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1413 | return -ENOMEM; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1414 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1415 | async_work->delayed_root = delayed_root; |
| 1416 | async_work->work.func = btrfs_async_run_delayed_root; |
| 1417 | async_work->work.flags = 0; |
| 1418 | async_work->nr = nr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1419 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1420 | btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
Chris Mason | e999376 | 2011-06-17 16:14:09 -0400 | [diff] [blame] | 1424 | void btrfs_assert_delayed_root_empty(struct btrfs_root *root) |
| 1425 | { |
| 1426 | struct btrfs_delayed_root *delayed_root; |
| 1427 | delayed_root = btrfs_get_delayed_root(root); |
| 1428 | WARN_ON(btrfs_first_delayed_node(delayed_root)); |
| 1429 | } |
| 1430 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1431 | static int refs_newer(struct btrfs_delayed_root *delayed_root, |
| 1432 | int seq, int count) |
| 1433 | { |
| 1434 | int val = atomic_read(&delayed_root->items_seq); |
| 1435 | |
| 1436 | if (val < seq || val >= seq + count) |
| 1437 | return 1; |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1441 | void btrfs_balance_delayed_items(struct btrfs_root *root) |
| 1442 | { |
| 1443 | struct btrfs_delayed_root *delayed_root; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1444 | int seq; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1445 | |
| 1446 | delayed_root = btrfs_get_delayed_root(root); |
| 1447 | |
| 1448 | if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) |
| 1449 | return; |
| 1450 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1451 | seq = atomic_read(&delayed_root->items_seq); |
| 1452 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1453 | if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) { |
| 1454 | int ret; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1455 | DEFINE_WAIT(__wait); |
| 1456 | |
| 1457 | ret = btrfs_wq_run_delayed_node(delayed_root, root, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1458 | if (ret) |
| 1459 | return; |
| 1460 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1461 | while (1) { |
| 1462 | prepare_to_wait(&delayed_root->wait, &__wait, |
| 1463 | TASK_INTERRUPTIBLE); |
| 1464 | |
| 1465 | if (refs_newer(delayed_root, seq, |
| 1466 | BTRFS_DELAYED_BATCH) || |
| 1467 | atomic_read(&delayed_root->items) < |
| 1468 | BTRFS_DELAYED_BACKGROUND) { |
| 1469 | break; |
| 1470 | } |
| 1471 | if (!signal_pending(current)) |
| 1472 | schedule(); |
| 1473 | else |
| 1474 | break; |
| 1475 | } |
| 1476 | finish_wait(&delayed_root->wait, &__wait); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1479 | btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1482 | /* Will return 0 or -ENOMEM */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1483 | int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, |
| 1484 | struct btrfs_root *root, const char *name, |
| 1485 | int name_len, struct inode *dir, |
| 1486 | struct btrfs_disk_key *disk_key, u8 type, |
| 1487 | u64 index) |
| 1488 | { |
| 1489 | struct btrfs_delayed_node *delayed_node; |
| 1490 | struct btrfs_delayed_item *delayed_item; |
| 1491 | struct btrfs_dir_item *dir_item; |
| 1492 | int ret; |
| 1493 | |
| 1494 | delayed_node = btrfs_get_or_create_delayed_node(dir); |
| 1495 | if (IS_ERR(delayed_node)) |
| 1496 | return PTR_ERR(delayed_node); |
| 1497 | |
| 1498 | delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len); |
| 1499 | if (!delayed_item) { |
| 1500 | ret = -ENOMEM; |
| 1501 | goto release_node; |
| 1502 | } |
| 1503 | |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 1504 | delayed_item->key.objectid = btrfs_ino(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1505 | btrfs_set_key_type(&delayed_item->key, BTRFS_DIR_INDEX_KEY); |
| 1506 | delayed_item->key.offset = index; |
| 1507 | |
| 1508 | dir_item = (struct btrfs_dir_item *)delayed_item->data; |
| 1509 | dir_item->location = *disk_key; |
| 1510 | dir_item->transid = cpu_to_le64(trans->transid); |
| 1511 | dir_item->data_len = 0; |
| 1512 | dir_item->name_len = cpu_to_le16(name_len); |
| 1513 | dir_item->type = type; |
| 1514 | memcpy((char *)(dir_item + 1), name, name_len); |
| 1515 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 1516 | ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item); |
| 1517 | /* |
| 1518 | * we have reserved enough space when we start a new transaction, |
| 1519 | * so reserving metadata failure is impossible |
| 1520 | */ |
| 1521 | BUG_ON(ret); |
| 1522 | |
| 1523 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1524 | mutex_lock(&delayed_node->mutex); |
| 1525 | ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item); |
| 1526 | if (unlikely(ret)) { |
| 1527 | printk(KERN_ERR "err add delayed dir index item(name: %s) into " |
| 1528 | "the insertion tree of the delayed node" |
| 1529 | "(root id: %llu, inode id: %llu, errno: %d)\n", |
| 1530 | name, |
| 1531 | (unsigned long long)delayed_node->root->objectid, |
| 1532 | (unsigned long long)delayed_node->inode_id, |
| 1533 | ret); |
| 1534 | BUG(); |
| 1535 | } |
| 1536 | mutex_unlock(&delayed_node->mutex); |
| 1537 | |
| 1538 | release_node: |
| 1539 | btrfs_release_delayed_node(delayed_node); |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root, |
| 1544 | struct btrfs_delayed_node *node, |
| 1545 | struct btrfs_key *key) |
| 1546 | { |
| 1547 | struct btrfs_delayed_item *item; |
| 1548 | |
| 1549 | mutex_lock(&node->mutex); |
| 1550 | item = __btrfs_lookup_delayed_insertion_item(node, key); |
| 1551 | if (!item) { |
| 1552 | mutex_unlock(&node->mutex); |
| 1553 | return 1; |
| 1554 | } |
| 1555 | |
| 1556 | btrfs_delayed_item_release_metadata(root, item); |
| 1557 | btrfs_release_delayed_item(item); |
| 1558 | mutex_unlock(&node->mutex); |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, |
| 1563 | struct btrfs_root *root, struct inode *dir, |
| 1564 | u64 index) |
| 1565 | { |
| 1566 | struct btrfs_delayed_node *node; |
| 1567 | struct btrfs_delayed_item *item; |
| 1568 | struct btrfs_key item_key; |
| 1569 | int ret; |
| 1570 | |
| 1571 | node = btrfs_get_or_create_delayed_node(dir); |
| 1572 | if (IS_ERR(node)) |
| 1573 | return PTR_ERR(node); |
| 1574 | |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 1575 | item_key.objectid = btrfs_ino(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1576 | btrfs_set_key_type(&item_key, BTRFS_DIR_INDEX_KEY); |
| 1577 | item_key.offset = index; |
| 1578 | |
| 1579 | ret = btrfs_delete_delayed_insertion_item(root, node, &item_key); |
| 1580 | if (!ret) |
| 1581 | goto end; |
| 1582 | |
| 1583 | item = btrfs_alloc_delayed_item(0); |
| 1584 | if (!item) { |
| 1585 | ret = -ENOMEM; |
| 1586 | goto end; |
| 1587 | } |
| 1588 | |
| 1589 | item->key = item_key; |
| 1590 | |
| 1591 | ret = btrfs_delayed_item_reserve_metadata(trans, root, item); |
| 1592 | /* |
| 1593 | * we have reserved enough space when we start a new transaction, |
| 1594 | * so reserving metadata failure is impossible. |
| 1595 | */ |
| 1596 | BUG_ON(ret); |
| 1597 | |
| 1598 | mutex_lock(&node->mutex); |
| 1599 | ret = __btrfs_add_delayed_deletion_item(node, item); |
| 1600 | if (unlikely(ret)) { |
| 1601 | printk(KERN_ERR "err add delayed dir index item(index: %llu) " |
| 1602 | "into the deletion tree of the delayed node" |
| 1603 | "(root id: %llu, inode id: %llu, errno: %d)\n", |
| 1604 | (unsigned long long)index, |
| 1605 | (unsigned long long)node->root->objectid, |
| 1606 | (unsigned long long)node->inode_id, |
| 1607 | ret); |
| 1608 | BUG(); |
| 1609 | } |
| 1610 | mutex_unlock(&node->mutex); |
| 1611 | end: |
| 1612 | btrfs_release_delayed_node(node); |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
| 1616 | int btrfs_inode_delayed_dir_index_count(struct inode *inode) |
| 1617 | { |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1618 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1619 | |
| 1620 | if (!delayed_node) |
| 1621 | return -ENOENT; |
| 1622 | |
| 1623 | /* |
| 1624 | * Since we have held i_mutex of this directory, it is impossible that |
| 1625 | * a new directory index is added into the delayed node and index_cnt |
| 1626 | * is updated now. So we needn't lock the delayed node. |
| 1627 | */ |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1628 | if (!delayed_node->index_cnt) { |
| 1629 | btrfs_release_delayed_node(delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1630 | return -EINVAL; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1631 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1632 | |
| 1633 | BTRFS_I(inode)->index_cnt = delayed_node->index_cnt; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1634 | btrfs_release_delayed_node(delayed_node); |
| 1635 | return 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list, |
| 1639 | struct list_head *del_list) |
| 1640 | { |
| 1641 | struct btrfs_delayed_node *delayed_node; |
| 1642 | struct btrfs_delayed_item *item; |
| 1643 | |
| 1644 | delayed_node = btrfs_get_delayed_node(inode); |
| 1645 | if (!delayed_node) |
| 1646 | return; |
| 1647 | |
| 1648 | mutex_lock(&delayed_node->mutex); |
| 1649 | item = __btrfs_first_delayed_insertion_item(delayed_node); |
| 1650 | while (item) { |
| 1651 | atomic_inc(&item->refs); |
| 1652 | list_add_tail(&item->readdir_list, ins_list); |
| 1653 | item = __btrfs_next_delayed_item(item); |
| 1654 | } |
| 1655 | |
| 1656 | item = __btrfs_first_delayed_deletion_item(delayed_node); |
| 1657 | while (item) { |
| 1658 | atomic_inc(&item->refs); |
| 1659 | list_add_tail(&item->readdir_list, del_list); |
| 1660 | item = __btrfs_next_delayed_item(item); |
| 1661 | } |
| 1662 | mutex_unlock(&delayed_node->mutex); |
| 1663 | /* |
| 1664 | * This delayed node is still cached in the btrfs inode, so refs |
| 1665 | * must be > 1 now, and we needn't check it is going to be freed |
| 1666 | * or not. |
| 1667 | * |
| 1668 | * Besides that, this function is used to read dir, we do not |
| 1669 | * insert/delete delayed items in this period. So we also needn't |
| 1670 | * requeue or dequeue this delayed node. |
| 1671 | */ |
| 1672 | atomic_dec(&delayed_node->refs); |
| 1673 | } |
| 1674 | |
| 1675 | void btrfs_put_delayed_items(struct list_head *ins_list, |
| 1676 | struct list_head *del_list) |
| 1677 | { |
| 1678 | struct btrfs_delayed_item *curr, *next; |
| 1679 | |
| 1680 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { |
| 1681 | list_del(&curr->readdir_list); |
| 1682 | if (atomic_dec_and_test(&curr->refs)) |
| 1683 | kfree(curr); |
| 1684 | } |
| 1685 | |
| 1686 | list_for_each_entry_safe(curr, next, del_list, readdir_list) { |
| 1687 | list_del(&curr->readdir_list); |
| 1688 | if (atomic_dec_and_test(&curr->refs)) |
| 1689 | kfree(curr); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | int btrfs_should_delete_dir_index(struct list_head *del_list, |
| 1694 | u64 index) |
| 1695 | { |
| 1696 | struct btrfs_delayed_item *curr, *next; |
| 1697 | int ret; |
| 1698 | |
| 1699 | if (list_empty(del_list)) |
| 1700 | return 0; |
| 1701 | |
| 1702 | list_for_each_entry_safe(curr, next, del_list, readdir_list) { |
| 1703 | if (curr->key.offset > index) |
| 1704 | break; |
| 1705 | |
| 1706 | list_del(&curr->readdir_list); |
| 1707 | ret = (curr->key.offset == index); |
| 1708 | |
| 1709 | if (atomic_dec_and_test(&curr->refs)) |
| 1710 | kfree(curr); |
| 1711 | |
| 1712 | if (ret) |
| 1713 | return 1; |
| 1714 | else |
| 1715 | continue; |
| 1716 | } |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree |
| 1722 | * |
| 1723 | */ |
| 1724 | int btrfs_readdir_delayed_dir_index(struct file *filp, void *dirent, |
| 1725 | filldir_t filldir, |
| 1726 | struct list_head *ins_list) |
| 1727 | { |
| 1728 | struct btrfs_dir_item *di; |
| 1729 | struct btrfs_delayed_item *curr, *next; |
| 1730 | struct btrfs_key location; |
| 1731 | char *name; |
| 1732 | int name_len; |
| 1733 | int over = 0; |
| 1734 | unsigned char d_type; |
| 1735 | |
| 1736 | if (list_empty(ins_list)) |
| 1737 | return 0; |
| 1738 | |
| 1739 | /* |
| 1740 | * Changing the data of the delayed item is impossible. So |
| 1741 | * we needn't lock them. And we have held i_mutex of the |
| 1742 | * directory, nobody can delete any directory indexes now. |
| 1743 | */ |
| 1744 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { |
| 1745 | list_del(&curr->readdir_list); |
| 1746 | |
| 1747 | if (curr->key.offset < filp->f_pos) { |
| 1748 | if (atomic_dec_and_test(&curr->refs)) |
| 1749 | kfree(curr); |
| 1750 | continue; |
| 1751 | } |
| 1752 | |
| 1753 | filp->f_pos = curr->key.offset; |
| 1754 | |
| 1755 | di = (struct btrfs_dir_item *)curr->data; |
| 1756 | name = (char *)(di + 1); |
| 1757 | name_len = le16_to_cpu(di->name_len); |
| 1758 | |
| 1759 | d_type = btrfs_filetype_table[di->type]; |
| 1760 | btrfs_disk_key_to_cpu(&location, &di->location); |
| 1761 | |
| 1762 | over = filldir(dirent, name, name_len, curr->key.offset, |
| 1763 | location.objectid, d_type); |
| 1764 | |
| 1765 | if (atomic_dec_and_test(&curr->refs)) |
| 1766 | kfree(curr); |
| 1767 | |
| 1768 | if (over) |
| 1769 | return 1; |
| 1770 | } |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item, |
| 1775 | generation, 64); |
| 1776 | BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item, |
| 1777 | sequence, 64); |
| 1778 | BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item, |
| 1779 | transid, 64); |
| 1780 | BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64); |
| 1781 | BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item, |
| 1782 | nbytes, 64); |
| 1783 | BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item, |
| 1784 | block_group, 64); |
| 1785 | BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32); |
| 1786 | BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32); |
| 1787 | BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32); |
| 1788 | BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32); |
| 1789 | BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64); |
| 1790 | BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64); |
| 1791 | |
| 1792 | BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64); |
| 1793 | BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32); |
| 1794 | |
| 1795 | static void fill_stack_inode_item(struct btrfs_trans_handle *trans, |
| 1796 | struct btrfs_inode_item *inode_item, |
| 1797 | struct inode *inode) |
| 1798 | { |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 1799 | btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode)); |
| 1800 | btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1801 | btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size); |
| 1802 | btrfs_set_stack_inode_mode(inode_item, inode->i_mode); |
| 1803 | btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink); |
| 1804 | btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode)); |
| 1805 | btrfs_set_stack_inode_generation(inode_item, |
| 1806 | BTRFS_I(inode)->generation); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 1807 | btrfs_set_stack_inode_sequence(inode_item, inode->i_version); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1808 | btrfs_set_stack_inode_transid(inode_item, trans->transid); |
| 1809 | btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev); |
| 1810 | btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1811 | btrfs_set_stack_inode_block_group(inode_item, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1812 | |
| 1813 | btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item), |
| 1814 | inode->i_atime.tv_sec); |
| 1815 | btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item), |
| 1816 | inode->i_atime.tv_nsec); |
| 1817 | |
| 1818 | btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item), |
| 1819 | inode->i_mtime.tv_sec); |
| 1820 | btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item), |
| 1821 | inode->i_mtime.tv_nsec); |
| 1822 | |
| 1823 | btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item), |
| 1824 | inode->i_ctime.tv_sec); |
| 1825 | btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item), |
| 1826 | inode->i_ctime.tv_nsec); |
| 1827 | } |
| 1828 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1829 | int btrfs_fill_inode(struct inode *inode, u32 *rdev) |
| 1830 | { |
| 1831 | struct btrfs_delayed_node *delayed_node; |
| 1832 | struct btrfs_inode_item *inode_item; |
| 1833 | struct btrfs_timespec *tspec; |
| 1834 | |
| 1835 | delayed_node = btrfs_get_delayed_node(inode); |
| 1836 | if (!delayed_node) |
| 1837 | return -ENOENT; |
| 1838 | |
| 1839 | mutex_lock(&delayed_node->mutex); |
| 1840 | if (!delayed_node->inode_dirty) { |
| 1841 | mutex_unlock(&delayed_node->mutex); |
| 1842 | btrfs_release_delayed_node(delayed_node); |
| 1843 | return -ENOENT; |
| 1844 | } |
| 1845 | |
| 1846 | inode_item = &delayed_node->inode_item; |
| 1847 | |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 1848 | i_uid_write(inode, btrfs_stack_inode_uid(inode_item)); |
| 1849 | i_gid_write(inode, btrfs_stack_inode_gid(inode_item)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1850 | btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item)); |
| 1851 | inode->i_mode = btrfs_stack_inode_mode(inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1852 | set_nlink(inode, btrfs_stack_inode_nlink(inode_item)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1853 | inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item)); |
| 1854 | BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 1855 | inode->i_version = btrfs_stack_inode_sequence(inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1856 | inode->i_rdev = 0; |
| 1857 | *rdev = btrfs_stack_inode_rdev(inode_item); |
| 1858 | BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item); |
| 1859 | |
| 1860 | tspec = btrfs_inode_atime(inode_item); |
| 1861 | inode->i_atime.tv_sec = btrfs_stack_timespec_sec(tspec); |
| 1862 | inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec); |
| 1863 | |
| 1864 | tspec = btrfs_inode_mtime(inode_item); |
| 1865 | inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec); |
| 1866 | inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec); |
| 1867 | |
| 1868 | tspec = btrfs_inode_ctime(inode_item); |
| 1869 | inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec); |
| 1870 | inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec); |
| 1871 | |
| 1872 | inode->i_generation = BTRFS_I(inode)->generation; |
| 1873 | BTRFS_I(inode)->index_cnt = (u64)-1; |
| 1874 | |
| 1875 | mutex_unlock(&delayed_node->mutex); |
| 1876 | btrfs_release_delayed_node(delayed_node); |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1880 | int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans, |
| 1881 | struct btrfs_root *root, struct inode *inode) |
| 1882 | { |
| 1883 | struct btrfs_delayed_node *delayed_node; |
David Sterba | aa0467d | 2011-06-03 16:29:08 +0200 | [diff] [blame] | 1884 | int ret = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1885 | |
| 1886 | delayed_node = btrfs_get_or_create_delayed_node(inode); |
| 1887 | if (IS_ERR(delayed_node)) |
| 1888 | return PTR_ERR(delayed_node); |
| 1889 | |
| 1890 | mutex_lock(&delayed_node->mutex); |
| 1891 | if (delayed_node->inode_dirty) { |
| 1892 | fill_stack_inode_item(trans, &delayed_node->inode_item, inode); |
| 1893 | goto release_node; |
| 1894 | } |
| 1895 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 1896 | ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode, |
| 1897 | delayed_node); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 1898 | if (ret) |
| 1899 | goto release_node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1900 | |
| 1901 | fill_stack_inode_item(trans, &delayed_node->inode_item, inode); |
| 1902 | delayed_node->inode_dirty = 1; |
| 1903 | delayed_node->count++; |
| 1904 | atomic_inc(&root->fs_info->delayed_root->items); |
| 1905 | release_node: |
| 1906 | mutex_unlock(&delayed_node->mutex); |
| 1907 | btrfs_release_delayed_node(delayed_node); |
| 1908 | return ret; |
| 1909 | } |
| 1910 | |
| 1911 | static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node) |
| 1912 | { |
| 1913 | struct btrfs_root *root = delayed_node->root; |
| 1914 | struct btrfs_delayed_item *curr_item, *prev_item; |
| 1915 | |
| 1916 | mutex_lock(&delayed_node->mutex); |
| 1917 | curr_item = __btrfs_first_delayed_insertion_item(delayed_node); |
| 1918 | while (curr_item) { |
| 1919 | btrfs_delayed_item_release_metadata(root, curr_item); |
| 1920 | prev_item = curr_item; |
| 1921 | curr_item = __btrfs_next_delayed_item(prev_item); |
| 1922 | btrfs_release_delayed_item(prev_item); |
| 1923 | } |
| 1924 | |
| 1925 | curr_item = __btrfs_first_delayed_deletion_item(delayed_node); |
| 1926 | while (curr_item) { |
| 1927 | btrfs_delayed_item_release_metadata(root, curr_item); |
| 1928 | prev_item = curr_item; |
| 1929 | curr_item = __btrfs_next_delayed_item(prev_item); |
| 1930 | btrfs_release_delayed_item(prev_item); |
| 1931 | } |
| 1932 | |
| 1933 | if (delayed_node->inode_dirty) { |
| 1934 | btrfs_delayed_inode_release_metadata(root, delayed_node); |
| 1935 | btrfs_release_delayed_inode(delayed_node); |
| 1936 | } |
| 1937 | mutex_unlock(&delayed_node->mutex); |
| 1938 | } |
| 1939 | |
| 1940 | void btrfs_kill_delayed_inode_items(struct inode *inode) |
| 1941 | { |
| 1942 | struct btrfs_delayed_node *delayed_node; |
| 1943 | |
| 1944 | delayed_node = btrfs_get_delayed_node(inode); |
| 1945 | if (!delayed_node) |
| 1946 | return; |
| 1947 | |
| 1948 | __btrfs_kill_delayed_node(delayed_node); |
| 1949 | btrfs_release_delayed_node(delayed_node); |
| 1950 | } |
| 1951 | |
| 1952 | void btrfs_kill_all_delayed_nodes(struct btrfs_root *root) |
| 1953 | { |
| 1954 | u64 inode_id = 0; |
| 1955 | struct btrfs_delayed_node *delayed_nodes[8]; |
| 1956 | int i, n; |
| 1957 | |
| 1958 | while (1) { |
| 1959 | spin_lock(&root->inode_lock); |
| 1960 | n = radix_tree_gang_lookup(&root->delayed_nodes_tree, |
| 1961 | (void **)delayed_nodes, inode_id, |
| 1962 | ARRAY_SIZE(delayed_nodes)); |
| 1963 | if (!n) { |
| 1964 | spin_unlock(&root->inode_lock); |
| 1965 | break; |
| 1966 | } |
| 1967 | |
| 1968 | inode_id = delayed_nodes[n - 1]->inode_id + 1; |
| 1969 | |
| 1970 | for (i = 0; i < n; i++) |
| 1971 | atomic_inc(&delayed_nodes[i]->refs); |
| 1972 | spin_unlock(&root->inode_lock); |
| 1973 | |
| 1974 | for (i = 0; i < n; i++) { |
| 1975 | __btrfs_kill_delayed_node(delayed_nodes[i]); |
| 1976 | btrfs_release_delayed_node(delayed_nodes[i]); |
| 1977 | } |
| 1978 | } |
| 1979 | } |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 1980 | |
| 1981 | void btrfs_destroy_delayed_inodes(struct btrfs_root *root) |
| 1982 | { |
| 1983 | struct btrfs_delayed_root *delayed_root; |
| 1984 | struct btrfs_delayed_node *curr_node, *prev_node; |
| 1985 | |
| 1986 | delayed_root = btrfs_get_delayed_root(root); |
| 1987 | |
| 1988 | curr_node = btrfs_first_delayed_node(delayed_root); |
| 1989 | while (curr_node) { |
| 1990 | __btrfs_kill_delayed_node(curr_node); |
| 1991 | |
| 1992 | prev_node = curr_node; |
| 1993 | curr_node = btrfs_next_delayed_node(curr_node); |
| 1994 | btrfs_release_delayed_node(prev_node); |
| 1995 | } |
| 1996 | } |
| 1997 | |