blob: 5fabda8f0a80f8ab766137f88715299ed0f32a41 [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Chris Mason925baed2008-06-25 16:01:30 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Mason925baed2008-06-25 16:01:30 -04004 */
5
David Sterba9888c342018-04-03 19:16:55 +02006#ifndef BTRFS_LOCKING_H
7#define BTRFS_LOCKING_H
Chris Mason925baed2008-06-25 16:01:30 -04008
Nikolay Borisov2992df72020-01-30 14:59:44 +02009#include <linux/atomic.h>
10#include <linux/wait.h>
11#include <linux/percpu_counter.h>
David Sterba31f6e762019-09-24 18:44:24 +020012#include "extent_io.h"
13
Chris Masonbd681512011-07-16 15:23:14 -040014#define BTRFS_WRITE_LOCK 1
15#define BTRFS_READ_LOCK 2
16#define BTRFS_WRITE_LOCK_BLOCKING 3
17#define BTRFS_READ_LOCK_BLOCKING 4
18
Josef Bacikfd7ba1c2020-08-20 11:46:02 -040019/*
20 * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
21 * the time of this patch is 8, which is how many we use. Keep this in mind if
22 * you decide you want to add another subclass.
23 */
24enum btrfs_lock_nesting {
25 BTRFS_NESTING_NORMAL,
26
27 /*
28 * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
29 * add this in here and add a static_assert to keep us from going over
30 * the limit. As of this writing we're limited to 8, and we're
31 * definitely using 8, hence this check to keep us from messing up in
32 * the future.
33 */
34 BTRFS_NESTING_MAX,
35};
36
37static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
38 "too many lock subclasses defined");
39
Nikolay Borisov2992df72020-01-30 14:59:44 +020040struct btrfs_path;
41
Josef Bacikfd7ba1c2020-08-20 11:46:02 -040042void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
Jeff Mahoney143bede2012-03-01 14:56:26 +010043void btrfs_tree_lock(struct extent_buffer *eb);
44void btrfs_tree_unlock(struct extent_buffer *eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -050045
Josef Bacikfd7ba1c2020-08-20 11:46:02 -040046void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest,
47 bool recurse);
Chris Masonbd681512011-07-16 15:23:14 -040048void btrfs_tree_read_lock(struct extent_buffer *eb);
49void btrfs_tree_read_unlock(struct extent_buffer *eb);
50void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb);
David Sterbab95be2d2018-04-04 01:43:05 +020051void btrfs_set_lock_blocking_read(struct extent_buffer *eb);
52void btrfs_set_lock_blocking_write(struct extent_buffer *eb);
Chris Masonbd681512011-07-16 15:23:14 -040053int btrfs_try_tree_read_lock(struct extent_buffer *eb);
54int btrfs_try_tree_write_lock(struct extent_buffer *eb);
Chris Masonf82c4582014-11-19 10:25:09 -080055int btrfs_tree_read_lock_atomic(struct extent_buffer *eb);
Josef Bacik51899412020-08-20 11:46:01 -040056struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
57struct extent_buffer *__btrfs_read_lock_root_node(struct btrfs_root *root,
58 bool recurse);
59
60static inline struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
61{
62 return __btrfs_read_lock_root_node(root, false);
63}
Chris Masonf82c4582014-11-19 10:25:09 -080064
David Sterba31f6e762019-09-24 18:44:24 +020065#ifdef CONFIG_BTRFS_DEBUG
66static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) {
67 BUG_ON(!eb->write_locks);
68}
69#else
70static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) { }
71#endif
Chris Masonbd681512011-07-16 15:23:14 -040072
David Sterbaed2b1d32019-09-24 19:17:17 +020073void btrfs_set_path_blocking(struct btrfs_path *p);
David Sterba1f95ec02019-09-24 19:17:17 +020074void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
David Sterbaed2b1d32019-09-24 19:17:17 +020075
Chris Masonbd681512011-07-16 15:23:14 -040076static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
77{
78 if (rw == BTRFS_WRITE_LOCK || rw == BTRFS_WRITE_LOCK_BLOCKING)
79 btrfs_tree_unlock(eb);
80 else if (rw == BTRFS_READ_LOCK_BLOCKING)
81 btrfs_tree_read_unlock_blocking(eb);
82 else if (rw == BTRFS_READ_LOCK)
83 btrfs_tree_read_unlock(eb);
84 else
85 BUG();
86}
87
Nikolay Borisov2992df72020-01-30 14:59:44 +020088struct btrfs_drew_lock {
89 atomic_t readers;
90 struct percpu_counter writers;
91 wait_queue_head_t pending_writers;
92 wait_queue_head_t pending_readers;
93};
94
95int btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
96void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock);
97void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
98bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
99void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
100void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
101void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
102
Chris Mason925baed2008-06-25 16:01:30 -0400103#endif