~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/bcachefs/nocow_locking.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #include "bcachefs.h"
  4 #include "bkey_methods.h"
  5 #include "nocow_locking.h"
  6 #include "util.h"
  7 
  8 #include <linux/closure.h>
  9 
 10 bool bch2_bucket_nocow_is_locked(struct bucket_nocow_lock_table *t, struct bpos bucket)
 11 {
 12         u64 dev_bucket = bucket_to_u64(bucket);
 13         struct nocow_lock_bucket *l = bucket_nocow_lock(t, dev_bucket);
 14         unsigned i;
 15 
 16         for (i = 0; i < ARRAY_SIZE(l->b); i++)
 17                 if (l->b[i] == dev_bucket && atomic_read(&l->l[i]))
 18                         return true;
 19         return false;
 20 }
 21 
 22 #define sign(v)         (v < 0 ? -1 : v > 0 ? 1 : 0)
 23 
 24 void bch2_bucket_nocow_unlock(struct bucket_nocow_lock_table *t, struct bpos bucket, int flags)
 25 {
 26         u64 dev_bucket = bucket_to_u64(bucket);
 27         struct nocow_lock_bucket *l = bucket_nocow_lock(t, dev_bucket);
 28         int lock_val = flags ? 1 : -1;
 29         unsigned i;
 30 
 31         for (i = 0; i < ARRAY_SIZE(l->b); i++)
 32                 if (l->b[i] == dev_bucket) {
 33                         int v = atomic_sub_return(lock_val, &l->l[i]);
 34 
 35                         BUG_ON(v && sign(v) != lock_val);
 36                         if (!v)
 37                                 closure_wake_up(&l->wait);
 38                         return;
 39                 }
 40 
 41         BUG();
 42 }
 43 
 44 bool __bch2_bucket_nocow_trylock(struct nocow_lock_bucket *l,
 45                                  u64 dev_bucket, int flags)
 46 {
 47         int v, lock_val = flags ? 1 : -1;
 48         unsigned i;
 49 
 50         spin_lock(&l->lock);
 51 
 52         for (i = 0; i < ARRAY_SIZE(l->b); i++)
 53                 if (l->b[i] == dev_bucket)
 54                         goto got_entry;
 55 
 56         for (i = 0; i < ARRAY_SIZE(l->b); i++)
 57                 if (!atomic_read(&l->l[i])) {
 58                         l->b[i] = dev_bucket;
 59                         goto take_lock;
 60                 }
 61 fail:
 62         spin_unlock(&l->lock);
 63         return false;
 64 got_entry:
 65         v = atomic_read(&l->l[i]);
 66         if (lock_val > 0 ? v < 0 : v > 0)
 67                 goto fail;
 68 take_lock:
 69         v = atomic_read(&l->l[i]);
 70         /* Overflow? */
 71         if (v && sign(v + lock_val) != sign(v))
 72                 goto fail;
 73 
 74         atomic_add(lock_val, &l->l[i]);
 75         spin_unlock(&l->lock);
 76         return true;
 77 }
 78 
 79 void __bch2_bucket_nocow_lock(struct bucket_nocow_lock_table *t,
 80                               struct nocow_lock_bucket *l,
 81                               u64 dev_bucket, int flags)
 82 {
 83         if (!__bch2_bucket_nocow_trylock(l, dev_bucket, flags)) {
 84                 struct bch_fs *c = container_of(t, struct bch_fs, nocow_locks);
 85                 u64 start_time = local_clock();
 86 
 87                 __closure_wait_event(&l->wait, __bch2_bucket_nocow_trylock(l, dev_bucket, flags));
 88                 bch2_time_stats_update(&c->times[BCH_TIME_nocow_lock_contended], start_time);
 89         }
 90 }
 91 
 92 void bch2_nocow_locks_to_text(struct printbuf *out, struct bucket_nocow_lock_table *t)
 93 
 94 {
 95         unsigned i, nr_zero = 0;
 96         struct nocow_lock_bucket *l;
 97 
 98         for (l = t->l; l < t->l + ARRAY_SIZE(t->l); l++) {
 99                 unsigned v = 0;
100 
101                 for (i = 0; i < ARRAY_SIZE(l->l); i++)
102                         v |= atomic_read(&l->l[i]);
103 
104                 if (!v) {
105                         nr_zero++;
106                         continue;
107                 }
108 
109                 if (nr_zero)
110                         prt_printf(out, "(%u empty entries)\n", nr_zero);
111                 nr_zero = 0;
112 
113                 for (i = 0; i < ARRAY_SIZE(l->l); i++) {
114                         int v = atomic_read(&l->l[i]);
115                         if (v) {
116                                 bch2_bpos_to_text(out, u64_to_bucket(l->b[i]));
117                                 prt_printf(out, ": %s %u ", v < 0 ? "copy" : "update", abs(v));
118                         }
119                 }
120                 prt_newline(out);
121         }
122 
123         if (nr_zero)
124                 prt_printf(out, "(%u empty entries)\n", nr_zero);
125 }
126 
127 void bch2_fs_nocow_locking_exit(struct bch_fs *c)
128 {
129         struct bucket_nocow_lock_table *t = &c->nocow_locks;
130 
131         for (struct nocow_lock_bucket *l = t->l; l < t->l + ARRAY_SIZE(t->l); l++)
132                 for (unsigned j = 0; j < ARRAY_SIZE(l->l); j++)
133                         BUG_ON(atomic_read(&l->l[j]));
134 }
135 
136 int bch2_fs_nocow_locking_init(struct bch_fs *c)
137 {
138         struct bucket_nocow_lock_table *t = &c->nocow_locks;
139 
140         for (struct nocow_lock_bucket *l = t->l; l < t->l + ARRAY_SIZE(t->l); l++)
141                 spin_lock_init(&l->lock);
142 
143         return 0;
144 }
145 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php