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

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

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/bcachefs/io_write.c (Version linux-6.12-rc7) and /fs/bcachefs/io_write.c (Version linux-6.7.12)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * Copyright 2010, 2011 Kent Overstreet <kent.      3  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  4  * Copyright 2012 Google, Inc.                      4  * Copyright 2012 Google, Inc.
  5  */                                                 5  */
  6                                                     6 
  7 #include "bcachefs.h"                               7 #include "bcachefs.h"
  8 #include "alloc_foreground.h"                       8 #include "alloc_foreground.h"
  9 #include "bkey_buf.h"                               9 #include "bkey_buf.h"
 10 #include "bset.h"                                  10 #include "bset.h"
 11 #include "btree_update.h"                          11 #include "btree_update.h"
 12 #include "buckets.h"                               12 #include "buckets.h"
 13 #include "checksum.h"                              13 #include "checksum.h"
 14 #include "clock.h"                                 14 #include "clock.h"
 15 #include "compress.h"                              15 #include "compress.h"
 16 #include "debug.h"                                 16 #include "debug.h"
 17 #include "ec.h"                                    17 #include "ec.h"
 18 #include "error.h"                                 18 #include "error.h"
 19 #include "extent_update.h"                         19 #include "extent_update.h"
 20 #include "inode.h"                                 20 #include "inode.h"
 21 #include "io_write.h"                              21 #include "io_write.h"
 22 #include "journal.h"                               22 #include "journal.h"
 23 #include "keylist.h"                               23 #include "keylist.h"
 24 #include "move.h"                                  24 #include "move.h"
 25 #include "nocow_locking.h"                         25 #include "nocow_locking.h"
 26 #include "rebalance.h"                             26 #include "rebalance.h"
 27 #include "subvolume.h"                             27 #include "subvolume.h"
 28 #include "super.h"                                 28 #include "super.h"
 29 #include "super-io.h"                              29 #include "super-io.h"
 30 #include "trace.h"                                 30 #include "trace.h"
 31                                                    31 
 32 #include <linux/blkdev.h>                          32 #include <linux/blkdev.h>
 33 #include <linux/prefetch.h>                        33 #include <linux/prefetch.h>
 34 #include <linux/random.h>                          34 #include <linux/random.h>
 35 #include <linux/sched/mm.h>                        35 #include <linux/sched/mm.h>
 36                                                    36 
 37 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT            37 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
 38                                                    38 
 39 static inline void bch2_congested_acct(struct      39 static inline void bch2_congested_acct(struct bch_dev *ca, u64 io_latency,
 40                                        u64 now     40                                        u64 now, int rw)
 41 {                                                  41 {
 42         u64 latency_capable =                      42         u64 latency_capable =
 43                 ca->io_latency[rw].quantiles.e     43                 ca->io_latency[rw].quantiles.entries[QUANTILE_IDX(1)].m;
 44         /* ideally we'd be taking into account     44         /* ideally we'd be taking into account the device's variance here: */
 45         u64 latency_threshold = latency_capabl     45         u64 latency_threshold = latency_capable << (rw == READ ? 2 : 3);
 46         s64 latency_over = io_latency - latenc     46         s64 latency_over = io_latency - latency_threshold;
 47                                                    47 
 48         if (latency_threshold && latency_over      48         if (latency_threshold && latency_over > 0) {
 49                 /*                                 49                 /*
 50                  * bump up congested by approx     50                  * bump up congested by approximately latency_over * 4 /
 51                  * latency_threshold - we don'     51                  * latency_threshold - we don't need much accuracy here so don't
 52                  * bother with the divide:         52                  * bother with the divide:
 53                  */                                53                  */
 54                 if (atomic_read(&ca->congested     54                 if (atomic_read(&ca->congested) < CONGESTED_MAX)
 55                         atomic_add(latency_ove     55                         atomic_add(latency_over >>
 56                                    max_t(int,      56                                    max_t(int, ilog2(latency_threshold) - 2, 0),
 57                                    &ca->conges     57                                    &ca->congested);
 58                                                    58 
 59                 ca->congested_last = now;          59                 ca->congested_last = now;
 60         } else if (atomic_read(&ca->congested)     60         } else if (atomic_read(&ca->congested) > 0) {
 61                 atomic_dec(&ca->congested);        61                 atomic_dec(&ca->congested);
 62         }                                          62         }
 63 }                                                  63 }
 64                                                    64 
 65 void bch2_latency_acct(struct bch_dev *ca, u64     65 void bch2_latency_acct(struct bch_dev *ca, u64 submit_time, int rw)
 66 {                                                  66 {
 67         atomic64_t *latency = &ca->cur_latency     67         atomic64_t *latency = &ca->cur_latency[rw];
 68         u64 now = local_clock();                   68         u64 now = local_clock();
 69         u64 io_latency = time_after64(now, sub     69         u64 io_latency = time_after64(now, submit_time)
 70                 ? now - submit_time                70                 ? now - submit_time
 71                 : 0;                               71                 : 0;
 72         u64 old, new;                          !!  72         u64 old, new, v = atomic64_read(latency);
 73                                                    73 
 74         old = atomic64_read(latency);          << 
 75         do {                                       74         do {
                                                   >>  75                 old = v;
                                                   >>  76 
 76                 /*                                 77                 /*
 77                  * If the io latency was reaso     78                  * If the io latency was reasonably close to the current
 78                  * latency, skip doing the upd     79                  * latency, skip doing the update and atomic operation - most of
 79                  * the time:                       80                  * the time:
 80                  */                                81                  */
 81                 if (abs((int) (old - io_latenc     82                 if (abs((int) (old - io_latency)) < (old >> 1) &&
 82                     now & ~(~0U << 5))             83                     now & ~(~0U << 5))
 83                         break;                     84                         break;
 84                                                    85 
 85                 new = ewma_add(old, io_latency     86                 new = ewma_add(old, io_latency, 5);
 86         } while (!atomic64_try_cmpxchg(latency !!  87         } while ((v = atomic64_cmpxchg(latency, old, new)) != old);
 87                                                    88 
 88         bch2_congested_acct(ca, io_latency, no     89         bch2_congested_acct(ca, io_latency, now, rw);
 89                                                    90 
 90         __bch2_time_stats_update(&ca->io_laten !!  91         __bch2_time_stats_update(&ca->io_latency[rw], submit_time, now);
 91 }                                                  92 }
 92                                                    93 
 93 #endif                                             94 #endif
 94                                                    95 
 95 /* Allocate, free from mempool: */                 96 /* Allocate, free from mempool: */
 96                                                    97 
 97 void bch2_bio_free_pages_pool(struct bch_fs *c     98 void bch2_bio_free_pages_pool(struct bch_fs *c, struct bio *bio)
 98 {                                                  99 {
 99         struct bvec_iter_all iter;                100         struct bvec_iter_all iter;
100         struct bio_vec *bv;                       101         struct bio_vec *bv;
101                                                   102 
102         bio_for_each_segment_all(bv, bio, iter    103         bio_for_each_segment_all(bv, bio, iter)
103                 if (bv->bv_page != ZERO_PAGE(0    104                 if (bv->bv_page != ZERO_PAGE(0))
104                         mempool_free(bv->bv_pa    105                         mempool_free(bv->bv_page, &c->bio_bounce_pages);
105         bio->bi_vcnt = 0;                         106         bio->bi_vcnt = 0;
106 }                                                 107 }
107                                                   108 
108 static struct page *__bio_alloc_page_pool(stru    109 static struct page *__bio_alloc_page_pool(struct bch_fs *c, bool *using_mempool)
109 {                                                 110 {
110         struct page *page;                        111         struct page *page;
111                                                   112 
112         if (likely(!*using_mempool)) {            113         if (likely(!*using_mempool)) {
113                 page = alloc_page(GFP_NOFS);      114                 page = alloc_page(GFP_NOFS);
114                 if (unlikely(!page)) {            115                 if (unlikely(!page)) {
115                         mutex_lock(&c->bio_bou    116                         mutex_lock(&c->bio_bounce_pages_lock);
116                         *using_mempool = true;    117                         *using_mempool = true;
117                         goto pool_alloc;          118                         goto pool_alloc;
118                                                   119 
119                 }                                 120                 }
120         } else {                                  121         } else {
121 pool_alloc:                                       122 pool_alloc:
122                 page = mempool_alloc(&c->bio_b    123                 page = mempool_alloc(&c->bio_bounce_pages, GFP_NOFS);
123         }                                         124         }
124                                                   125 
125         return page;                              126         return page;
126 }                                                 127 }
127                                                   128 
128 void bch2_bio_alloc_pages_pool(struct bch_fs *    129 void bch2_bio_alloc_pages_pool(struct bch_fs *c, struct bio *bio,
129                                size_t size)       130                                size_t size)
130 {                                                 131 {
131         bool using_mempool = false;               132         bool using_mempool = false;
132                                                   133 
133         while (size) {                            134         while (size) {
134                 struct page *page = __bio_allo    135                 struct page *page = __bio_alloc_page_pool(c, &using_mempool);
135                 unsigned len = min_t(size_t, P    136                 unsigned len = min_t(size_t, PAGE_SIZE, size);
136                                                   137 
137                 BUG_ON(!bio_add_page(bio, page    138                 BUG_ON(!bio_add_page(bio, page, len, 0));
138                 size -= len;                      139                 size -= len;
139         }                                         140         }
140                                                   141 
141         if (using_mempool)                        142         if (using_mempool)
142                 mutex_unlock(&c->bio_bounce_pa    143                 mutex_unlock(&c->bio_bounce_pages_lock);
143 }                                                 144 }
144                                                   145 
145 /* Extent update path: */                         146 /* Extent update path: */
146                                                   147 
147 int bch2_sum_sector_overwrites(struct btree_tr    148 int bch2_sum_sector_overwrites(struct btree_trans *trans,
148                                struct btree_it    149                                struct btree_iter *extent_iter,
149                                struct bkey_i *    150                                struct bkey_i *new,
150                                bool *usage_inc    151                                bool *usage_increasing,
151                                s64 *i_sectors_    152                                s64 *i_sectors_delta,
152                                s64 *disk_secto    153                                s64 *disk_sectors_delta)
153 {                                                 154 {
154         struct bch_fs *c = trans->c;              155         struct bch_fs *c = trans->c;
155         struct btree_iter iter;                   156         struct btree_iter iter;
156         struct bkey_s_c old;                      157         struct bkey_s_c old;
157         unsigned new_replicas = bch2_bkey_repl    158         unsigned new_replicas = bch2_bkey_replicas(c, bkey_i_to_s_c(new));
158         bool new_compressed = bch2_bkey_sector    159         bool new_compressed = bch2_bkey_sectors_compressed(bkey_i_to_s_c(new));
159         int ret = 0;                              160         int ret = 0;
160                                                   161 
161         *usage_increasing       = false;          162         *usage_increasing       = false;
162         *i_sectors_delta        = 0;              163         *i_sectors_delta        = 0;
163         *disk_sectors_delta     = 0;              164         *disk_sectors_delta     = 0;
164                                                   165 
165         bch2_trans_copy_iter(&iter, extent_ite    166         bch2_trans_copy_iter(&iter, extent_iter);
166                                                   167 
167         for_each_btree_key_upto_continue_nores    168         for_each_btree_key_upto_continue_norestart(iter,
168                                 new->k.p, BTRE !! 169                                 new->k.p, BTREE_ITER_SLOTS, old, ret) {
169                 s64 sectors = min(new->k.p.off    170                 s64 sectors = min(new->k.p.offset, old.k->p.offset) -
170                         max(bkey_start_offset(    171                         max(bkey_start_offset(&new->k),
171                             bkey_start_offset(    172                             bkey_start_offset(old.k));
172                                                   173 
173                 *i_sectors_delta += sectors *     174                 *i_sectors_delta += sectors *
174                         (bkey_extent_is_alloca    175                         (bkey_extent_is_allocation(&new->k) -
175                          bkey_extent_is_alloca    176                          bkey_extent_is_allocation(old.k));
176                                                   177 
177                 *disk_sectors_delta += sectors    178                 *disk_sectors_delta += sectors * bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(new));
178                 *disk_sectors_delta -= new->k.    179                 *disk_sectors_delta -= new->k.p.snapshot == old.k->p.snapshot
179                         ? sectors * bch2_bkey_    180                         ? sectors * bch2_bkey_nr_ptrs_fully_allocated(old)
180                         : 0;                      181                         : 0;
181                                                   182 
182                 if (!*usage_increasing &&         183                 if (!*usage_increasing &&
183                     (new->k.p.snapshot != old.    184                     (new->k.p.snapshot != old.k->p.snapshot ||
184                      new_replicas > bch2_bkey_    185                      new_replicas > bch2_bkey_replicas(c, old) ||
185                      (!new_compressed && bch2_    186                      (!new_compressed && bch2_bkey_sectors_compressed(old))))
186                         *usage_increasing = tr    187                         *usage_increasing = true;
187                                                   188 
188                 if (bkey_ge(old.k->p, new->k.p    189                 if (bkey_ge(old.k->p, new->k.p))
189                         break;                    190                         break;
190         }                                         191         }
191                                                   192 
192         bch2_trans_iter_exit(trans, &iter);       193         bch2_trans_iter_exit(trans, &iter);
193         return ret;                               194         return ret;
194 }                                                 195 }
195                                                   196 
196 static inline int bch2_extent_update_i_size_se    197 static inline int bch2_extent_update_i_size_sectors(struct btree_trans *trans,
197                                                   198                                                     struct btree_iter *extent_iter,
198                                                   199                                                     u64 new_i_size,
199                                                   200                                                     s64 i_sectors_delta)
200 {                                                 201 {
                                                   >> 202         struct btree_iter iter;
                                                   >> 203         struct bkey_i *k;
                                                   >> 204         struct bkey_i_inode_v3 *inode;
201         /*                                        205         /*
202          * Crazy performance optimization:        206          * Crazy performance optimization:
203          * Every extent update needs to also u    207          * Every extent update needs to also update the inode: the inode trigger
204          * will set bi->journal_seq to the jou    208          * will set bi->journal_seq to the journal sequence number of this
205          * transaction - for fsync.               209          * transaction - for fsync.
206          *                                        210          *
207          * But if that's the only reason we're    211          * But if that's the only reason we're updating the inode (we're not
208          * updating bi_size or bi_sectors), th    212          * updating bi_size or bi_sectors), then we don't need the inode update
209          * to be journalled - if we crash, the    213          * to be journalled - if we crash, the bi_journal_seq update will be
210          * lost, but that's fine.                 214          * lost, but that's fine.
211          */                                       215          */
212         unsigned inode_update_flags = BTREE_UP !! 216         unsigned inode_update_flags = BTREE_UPDATE_NOJOURNAL;
                                                   >> 217         int ret;
213                                                   218 
214         struct btree_iter iter;                !! 219         k = bch2_bkey_get_mut_noupdate(trans, &iter, BTREE_ID_inodes,
215         struct bkey_s_c k = bch2_bkey_get_iter << 
216                               SPOS(0,             220                               SPOS(0,
217                                    extent_iter    221                                    extent_iter->pos.inode,
218                                    extent_iter    222                                    extent_iter->snapshot),
219                               BTREE_ITER_cache !! 223                               BTREE_ITER_CACHED);
220         int ret = bkey_err(k);                 !! 224         ret = PTR_ERR_OR_ZERO(k);
221         if (unlikely(ret))                        225         if (unlikely(ret))
222                 return ret;                       226                 return ret;
223                                                   227 
224         /*                                     !! 228         if (unlikely(k->k.type != KEY_TYPE_inode_v3)) {
225          * varint_decode_fast(), in the inode  !! 229                 k = bch2_inode_to_v3(trans, k);
226          * bytes past the end of the buffer:   !! 230                 ret = PTR_ERR_OR_ZERO(k);
227          */                                    << 
228         struct bkey_i *k_mut = bch2_trans_kmal << 
229         ret = PTR_ERR_OR_ZERO(k_mut);          << 
230         if (unlikely(ret))                     << 
231                 goto err;                      << 
232                                                << 
233         bkey_reassemble(k_mut, k);             << 
234                                                << 
235         if (unlikely(k_mut->k.type != KEY_TYPE << 
236                 k_mut = bch2_inode_to_v3(trans << 
237                 ret = PTR_ERR_OR_ZERO(k_mut);  << 
238                 if (unlikely(ret))                231                 if (unlikely(ret))
239                         goto err;                 232                         goto err;
240         }                                         233         }
241                                                   234 
242         struct bkey_i_inode_v3 *inode = bkey_i !! 235         inode = bkey_i_to_inode_v3(k);
243                                                   236 
244         if (!(le64_to_cpu(inode->v.bi_flags) &    237         if (!(le64_to_cpu(inode->v.bi_flags) & BCH_INODE_i_size_dirty) &&
245             new_i_size > le64_to_cpu(inode->v.    238             new_i_size > le64_to_cpu(inode->v.bi_size)) {
246                 inode->v.bi_size = cpu_to_le64    239                 inode->v.bi_size = cpu_to_le64(new_i_size);
247                 inode_update_flags = 0;           240                 inode_update_flags = 0;
248         }                                         241         }
249                                                   242 
250         if (i_sectors_delta) {                    243         if (i_sectors_delta) {
251                 le64_add_cpu(&inode->v.bi_sect    244                 le64_add_cpu(&inode->v.bi_sectors, i_sectors_delta);
252                 inode_update_flags = 0;           245                 inode_update_flags = 0;
253         }                                         246         }
254                                                   247 
255         if (inode->k.p.snapshot != iter.snapsh    248         if (inode->k.p.snapshot != iter.snapshot) {
256                 inode->k.p.snapshot = iter.sna    249                 inode->k.p.snapshot = iter.snapshot;
257                 inode_update_flags = 0;           250                 inode_update_flags = 0;
258         }                                         251         }
259                                                   252 
260         ret = bch2_trans_update(trans, &iter,     253         ret = bch2_trans_update(trans, &iter, &inode->k_i,
261                                 BTREE_UPDATE_i !! 254                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
262                                 inode_update_f    255                                 inode_update_flags);
263 err:                                              256 err:
264         bch2_trans_iter_exit(trans, &iter);       257         bch2_trans_iter_exit(trans, &iter);
265         return ret;                               258         return ret;
266 }                                                 259 }
267                                                   260 
268 int bch2_extent_update(struct btree_trans *tra    261 int bch2_extent_update(struct btree_trans *trans,
269                        subvol_inum inum,          262                        subvol_inum inum,
270                        struct btree_iter *iter    263                        struct btree_iter *iter,
271                        struct bkey_i *k,          264                        struct bkey_i *k,
272                        struct disk_reservation    265                        struct disk_reservation *disk_res,
273                        u64 new_i_size,            266                        u64 new_i_size,
274                        s64 *i_sectors_delta_to    267                        s64 *i_sectors_delta_total,
275                        bool check_enospc)         268                        bool check_enospc)
276 {                                                 269 {
277         struct bpos next_pos;                     270         struct bpos next_pos;
278         bool usage_increasing;                    271         bool usage_increasing;
279         s64 i_sectors_delta = 0, disk_sectors_    272         s64 i_sectors_delta = 0, disk_sectors_delta = 0;
280         int ret;                                  273         int ret;
281                                                   274 
282         /*                                        275         /*
283          * This traverses us the iterator with    276          * This traverses us the iterator without changing iter->path->pos to
284          * search_key() (which is pos + 1 for     277          * search_key() (which is pos + 1 for extents): we want there to be a
285          * path already traversed at iter->pos    278          * path already traversed at iter->pos because
286          * bch2_trans_extent_update() will use    279          * bch2_trans_extent_update() will use it to attempt extent merging
287          */                                       280          */
288         ret = __bch2_btree_iter_traverse(iter)    281         ret = __bch2_btree_iter_traverse(iter);
289         if (ret)                                  282         if (ret)
290                 return ret;                       283                 return ret;
291                                                   284 
292         ret = bch2_extent_trim_atomic(trans, i    285         ret = bch2_extent_trim_atomic(trans, iter, k);
293         if (ret)                                  286         if (ret)
294                 return ret;                       287                 return ret;
295                                                   288 
296         next_pos = k->k.p;                        289         next_pos = k->k.p;
297                                                   290 
298         ret = bch2_sum_sector_overwrites(trans    291         ret = bch2_sum_sector_overwrites(trans, iter, k,
299                         &usage_increasing,        292                         &usage_increasing,
300                         &i_sectors_delta,         293                         &i_sectors_delta,
301                         &disk_sectors_delta);     294                         &disk_sectors_delta);
302         if (ret)                                  295         if (ret)
303                 return ret;                       296                 return ret;
304                                                   297 
305         if (disk_res &&                           298         if (disk_res &&
306             disk_sectors_delta > (s64) disk_re    299             disk_sectors_delta > (s64) disk_res->sectors) {
307                 ret = bch2_disk_reservation_ad    300                 ret = bch2_disk_reservation_add(trans->c, disk_res,
308                                         disk_s    301                                         disk_sectors_delta - disk_res->sectors,
309                                         !check    302                                         !check_enospc || !usage_increasing
310                                         ? BCH_    303                                         ? BCH_DISK_RESERVATION_NOFAIL : 0);
311                 if (ret)                          304                 if (ret)
312                         return ret;               305                         return ret;
313         }                                         306         }
314                                                   307 
315         /*                                        308         /*
316          * Note:                                  309          * Note:
317          * We always have to do an inode updat    310          * We always have to do an inode update - even when i_size/i_sectors
318          * aren't changing - for fsync to work    311          * aren't changing - for fsync to work properly; fsync relies on
319          * inode->bi_journal_seq which is upda    312          * inode->bi_journal_seq which is updated by the trigger code:
320          */                                       313          */
321         ret =   bch2_extent_update_i_size_sect    314         ret =   bch2_extent_update_i_size_sectors(trans, iter,
322                                                   315                                                   min(k->k.p.offset << 9, new_i_size),
323                                                   316                                                   i_sectors_delta) ?:
324                 bch2_trans_update(trans, iter,    317                 bch2_trans_update(trans, iter, k, 0) ?:
325                 bch2_trans_commit(trans, disk_    318                 bch2_trans_commit(trans, disk_res, NULL,
326                                 BCH_TRANS_COMM !! 319                                 BTREE_INSERT_NOCHECK_RW|
327                                 BCH_TRANS_COMM !! 320                                 BTREE_INSERT_NOFAIL);
328         if (unlikely(ret))                        321         if (unlikely(ret))
329                 return ret;                       322                 return ret;
330                                                   323 
331         if (i_sectors_delta_total)                324         if (i_sectors_delta_total)
332                 *i_sectors_delta_total += i_se    325                 *i_sectors_delta_total += i_sectors_delta;
333         bch2_btree_iter_set_pos(iter, next_pos    326         bch2_btree_iter_set_pos(iter, next_pos);
334         return 0;                                 327         return 0;
335 }                                                 328 }
336                                                   329 
337 static int bch2_write_index_default(struct bch    330 static int bch2_write_index_default(struct bch_write_op *op)
338 {                                                 331 {
339         struct bch_fs *c = op->c;                 332         struct bch_fs *c = op->c;
340         struct bkey_buf sk;                       333         struct bkey_buf sk;
341         struct keylist *keys = &op->insert_key    334         struct keylist *keys = &op->insert_keys;
342         struct bkey_i *k = bch2_keylist_front(    335         struct bkey_i *k = bch2_keylist_front(keys);
343         struct btree_trans *trans = bch2_trans    336         struct btree_trans *trans = bch2_trans_get(c);
344         struct btree_iter iter;                   337         struct btree_iter iter;
345         subvol_inum inum = {                      338         subvol_inum inum = {
346                 .subvol = op->subvol,             339                 .subvol = op->subvol,
347                 .inum   = k->k.p.inode,           340                 .inum   = k->k.p.inode,
348         };                                        341         };
349         int ret;                                  342         int ret;
350                                                   343 
351         BUG_ON(!inum.subvol);                     344         BUG_ON(!inum.subvol);
352                                                   345 
353         bch2_bkey_buf_init(&sk);                  346         bch2_bkey_buf_init(&sk);
354                                                   347 
355         do {                                      348         do {
356                 bch2_trans_begin(trans);          349                 bch2_trans_begin(trans);
357                                                   350 
358                 k = bch2_keylist_front(keys);     351                 k = bch2_keylist_front(keys);
359                 bch2_bkey_buf_copy(&sk, c, k);    352                 bch2_bkey_buf_copy(&sk, c, k);
360                                                   353 
361                 ret = bch2_subvolume_get_snaps    354                 ret = bch2_subvolume_get_snapshot(trans, inum.subvol,
362                                                   355                                                   &sk.k->k.p.snapshot);
363                 if (bch2_err_matches(ret, BCH_    356                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
364                         continue;                 357                         continue;
365                 if (ret)                          358                 if (ret)
366                         break;                    359                         break;
367                                                   360 
368                 bch2_trans_iter_init(trans, &i    361                 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
369                                      bkey_star    362                                      bkey_start_pos(&sk.k->k),
370                                      BTREE_ITE !! 363                                      BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
371                                                   364 
372                 ret =   bch2_bkey_set_needs_re !! 365                 ret =   bch2_bkey_set_needs_rebalance(c, sk.k,
                                                   >> 366                                         op->opts.background_target,
                                                   >> 367                                         op->opts.background_compression) ?:
373                         bch2_extent_update(tra    368                         bch2_extent_update(trans, inum, &iter, sk.k,
374                                         &op->r    369                                         &op->res,
375                                         op->ne    370                                         op->new_i_size, &op->i_sectors_delta,
376                                         op->fl    371                                         op->flags & BCH_WRITE_CHECK_ENOSPC);
377                 bch2_trans_iter_exit(trans, &i    372                 bch2_trans_iter_exit(trans, &iter);
378                                                   373 
379                 if (bch2_err_matches(ret, BCH_    374                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
380                         continue;                 375                         continue;
381                 if (ret)                          376                 if (ret)
382                         break;                    377                         break;
383                                                   378 
384                 if (bkey_ge(iter.pos, k->k.p))    379                 if (bkey_ge(iter.pos, k->k.p))
385                         bch2_keylist_pop_front    380                         bch2_keylist_pop_front(&op->insert_keys);
386                 else                              381                 else
387                         bch2_cut_front(iter.po    382                         bch2_cut_front(iter.pos, k);
388         } while (!bch2_keylist_empty(keys));      383         } while (!bch2_keylist_empty(keys));
389                                                   384 
390         bch2_trans_put(trans);                    385         bch2_trans_put(trans);
391         bch2_bkey_buf_exit(&sk, c);               386         bch2_bkey_buf_exit(&sk, c);
392                                                   387 
393         return ret;                               388         return ret;
394 }                                                 389 }
395                                                   390 
396 /* Writes */                                      391 /* Writes */
397                                                   392 
398 void bch2_submit_wbio_replicas(struct bch_writ    393 void bch2_submit_wbio_replicas(struct bch_write_bio *wbio, struct bch_fs *c,
399                                enum bch_data_t    394                                enum bch_data_type type,
400                                const struct bk    395                                const struct bkey_i *k,
401                                bool nocow)        396                                bool nocow)
402 {                                                 397 {
403         struct bkey_ptrs_c ptrs = bch2_bkey_pt    398         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
                                                   >> 399         const struct bch_extent_ptr *ptr;
404         struct bch_write_bio *n;                  400         struct bch_write_bio *n;
                                                   >> 401         struct bch_dev *ca;
405                                                   402 
406         BUG_ON(c->opts.nochanges);                403         BUG_ON(c->opts.nochanges);
407                                                   404 
408         bkey_for_each_ptr(ptrs, ptr) {            405         bkey_for_each_ptr(ptrs, ptr) {
409                 struct bch_dev *ca = nocow     !! 406                 BUG_ON(ptr->dev >= BCH_SB_MEMBERS_MAX ||
410                         ? bch2_dev_have_ref(c, !! 407                        !c->devs[ptr->dev]);
411                         : bch2_dev_get_ioref(c !! 408 
                                                   >> 409                 ca = bch_dev_bkey_exists(c, ptr->dev);
412                                                   410 
413                 if (to_entry(ptr + 1) < ptrs.e    411                 if (to_entry(ptr + 1) < ptrs.end) {
414                         n = to_wbio(bio_alloc_ !! 412                         n = to_wbio(bio_alloc_clone(NULL, &wbio->bio,
                                                   >> 413                                                 GFP_NOFS, &ca->replica_set));
415                                                   414 
416                         n->bio.bi_end_io          415                         n->bio.bi_end_io        = wbio->bio.bi_end_io;
417                         n->bio.bi_private         416                         n->bio.bi_private       = wbio->bio.bi_private;
418                         n->parent                 417                         n->parent               = wbio;
419                         n->split                  418                         n->split                = true;
420                         n->bounce                 419                         n->bounce               = false;
421                         n->put_bio                420                         n->put_bio              = true;
422                         n->bio.bi_opf             421                         n->bio.bi_opf           = wbio->bio.bi_opf;
423                         bio_inc_remaining(&wbi    422                         bio_inc_remaining(&wbio->bio);
424                 } else {                          423                 } else {
425                         n = wbio;                 424                         n = wbio;
426                         n->split                  425                         n->split                = false;
427                 }                                 426                 }
428                                                   427 
429                 n->c                    = c;      428                 n->c                    = c;
430                 n->dev                  = ptr-    429                 n->dev                  = ptr->dev;
431                 n->have_ioref           = ca ! !! 430                 n->have_ioref           = nocow || bch2_dev_get_ioref(ca,
                                                   >> 431                                         type == BCH_DATA_btree ? READ : WRITE);
432                 n->nocow                = noco    432                 n->nocow                = nocow;
433                 n->submit_time          = loca    433                 n->submit_time          = local_clock();
434                 n->inode_offset         = bkey    434                 n->inode_offset         = bkey_start_offset(&k->k);
435                 if (nocow)                     << 
436                         n->nocow_bucket = PTR_ << 
437                 n->bio.bi_iter.bi_sector = ptr    435                 n->bio.bi_iter.bi_sector = ptr->offset;
438                                                   436 
439                 if (likely(n->have_ioref)) {      437                 if (likely(n->have_ioref)) {
440                         this_cpu_add(ca->io_do    438                         this_cpu_add(ca->io_done->sectors[WRITE][type],
441                                      bio_secto    439                                      bio_sectors(&n->bio));
442                                                   440 
443                         bio_set_dev(&n->bio, c    441                         bio_set_dev(&n->bio, ca->disk_sb.bdev);
444                                                   442 
445                         if (type != BCH_DATA_b    443                         if (type != BCH_DATA_btree && unlikely(c->opts.no_data_io)) {
446                                 bio_endio(&n->    444                                 bio_endio(&n->bio);
447                                 continue;         445                                 continue;
448                         }                         446                         }
449                                                   447 
450                         submit_bio(&n->bio);      448                         submit_bio(&n->bio);
451                 } else {                          449                 } else {
452                         n->bio.bi_status          450                         n->bio.bi_status        = BLK_STS_REMOVED;
453                         bio_endio(&n->bio);       451                         bio_endio(&n->bio);
454                 }                                 452                 }
455         }                                         453         }
456 }                                                 454 }
457                                                   455 
458 static void __bch2_write(struct bch_write_op *    456 static void __bch2_write(struct bch_write_op *);
459                                                   457 
460 static void bch2_write_done(struct closure *cl    458 static void bch2_write_done(struct closure *cl)
461 {                                                 459 {
462         struct bch_write_op *op = container_of    460         struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
463         struct bch_fs *c = op->c;                 461         struct bch_fs *c = op->c;
464                                                   462 
465         EBUG_ON(op->open_buckets.nr);             463         EBUG_ON(op->open_buckets.nr);
466                                                   464 
467         bch2_time_stats_update(&c->times[BCH_T    465         bch2_time_stats_update(&c->times[BCH_TIME_data_write], op->start_time);
468         bch2_disk_reservation_put(c, &op->res)    466         bch2_disk_reservation_put(c, &op->res);
469                                                   467 
470         if (!(op->flags & BCH_WRITE_MOVE))        468         if (!(op->flags & BCH_WRITE_MOVE))
471                 bch2_write_ref_put(c, BCH_WRIT    469                 bch2_write_ref_put(c, BCH_WRITE_REF_write);
472         bch2_keylist_free(&op->insert_keys, op    470         bch2_keylist_free(&op->insert_keys, op->inline_keys);
473                                                   471 
474         EBUG_ON(cl->parent);                      472         EBUG_ON(cl->parent);
475         closure_debug_destroy(cl);                473         closure_debug_destroy(cl);
476         if (op->end_io)                           474         if (op->end_io)
477                 op->end_io(op);                   475                 op->end_io(op);
478 }                                                 476 }
479                                                   477 
480 static noinline int bch2_write_drop_io_error_p    478 static noinline int bch2_write_drop_io_error_ptrs(struct bch_write_op *op)
481 {                                                 479 {
482         struct keylist *keys = &op->insert_key    480         struct keylist *keys = &op->insert_keys;
                                                   >> 481         struct bch_extent_ptr *ptr;
483         struct bkey_i *src, *dst = keys->keys,    482         struct bkey_i *src, *dst = keys->keys, *n;
484                                                   483 
485         for (src = keys->keys; src != keys->to    484         for (src = keys->keys; src != keys->top; src = n) {
486                 n = bkey_next(src);               485                 n = bkey_next(src);
487                                                   486 
488                 if (bkey_extent_is_direct_data    487                 if (bkey_extent_is_direct_data(&src->k)) {
489                         bch2_bkey_drop_ptrs(bk    488                         bch2_bkey_drop_ptrs(bkey_i_to_s(src), ptr,
490                                             te    489                                             test_bit(ptr->dev, op->failed.d));
491                                                   490 
492                         if (!bch2_bkey_nr_ptrs    491                         if (!bch2_bkey_nr_ptrs(bkey_i_to_s_c(src)))
493                                 return -EIO;      492                                 return -EIO;
494                 }                                 493                 }
495                                                   494 
496                 if (dst != src)                   495                 if (dst != src)
497                         memmove_u64s_down(dst,    496                         memmove_u64s_down(dst, src, src->k.u64s);
498                 dst = bkey_next(dst);             497                 dst = bkey_next(dst);
499         }                                         498         }
500                                                   499 
501         keys->top = dst;                          500         keys->top = dst;
502         return 0;                                 501         return 0;
503 }                                                 502 }
504                                                   503 
505 /**                                               504 /**
506  * __bch2_write_index - after a write, update     505  * __bch2_write_index - after a write, update index to point to new data
507  * @op:         bch_write_op to process           506  * @op:         bch_write_op to process
508  */                                               507  */
509 static void __bch2_write_index(struct bch_writ    508 static void __bch2_write_index(struct bch_write_op *op)
510 {                                                 509 {
511         struct bch_fs *c = op->c;                 510         struct bch_fs *c = op->c;
512         struct keylist *keys = &op->insert_key    511         struct keylist *keys = &op->insert_keys;
513         unsigned dev;                             512         unsigned dev;
514         int ret = 0;                              513         int ret = 0;
515                                                   514 
516         if (unlikely(op->flags & BCH_WRITE_IO_    515         if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
517                 ret = bch2_write_drop_io_error    516                 ret = bch2_write_drop_io_error_ptrs(op);
518                 if (ret)                          517                 if (ret)
519                         goto err;                 518                         goto err;
520         }                                         519         }
521                                                   520 
522         if (!bch2_keylist_empty(keys)) {          521         if (!bch2_keylist_empty(keys)) {
523                 u64 sectors_start = keylist_se    522                 u64 sectors_start = keylist_sectors(keys);
524                                                   523 
525                 ret = !(op->flags & BCH_WRITE_    524                 ret = !(op->flags & BCH_WRITE_MOVE)
526                         ? bch2_write_index_def    525                         ? bch2_write_index_default(op)
527                         : bch2_data_update_ind    526                         : bch2_data_update_index_update(op);
528                                                   527 
529                 BUG_ON(bch2_err_matches(ret, B    528                 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
530                 BUG_ON(keylist_sectors(keys) &    529                 BUG_ON(keylist_sectors(keys) && !ret);
531                                                   530 
532                 op->written += sectors_start -    531                 op->written += sectors_start - keylist_sectors(keys);
533                                                   532 
534                 if (ret && !bch2_err_matches(r    533                 if (ret && !bch2_err_matches(ret, EROFS)) {
535                         struct bkey_i *insert     534                         struct bkey_i *insert = bch2_keylist_front(&op->insert_keys);
536                                                   535 
537                         bch_err_inum_offset_ra    536                         bch_err_inum_offset_ratelimited(c,
538                                 insert->k.p.in    537                                 insert->k.p.inode, insert->k.p.offset << 9,
539                                 "%s write erro !! 538                                 "write error while doing btree update: %s",
540                                 op->flags & BC << 
541                                 bch2_err_str(r    539                                 bch2_err_str(ret));
542                 }                                 540                 }
543                                                   541 
544                 if (ret)                          542                 if (ret)
545                         goto err;                 543                         goto err;
546         }                                         544         }
547 out:                                              545 out:
548         /* If some a bucket wasn't written, we    546         /* If some a bucket wasn't written, we can't erasure code it: */
549         for_each_set_bit(dev, op->failed.d, BC    547         for_each_set_bit(dev, op->failed.d, BCH_SB_MEMBERS_MAX)
550                 bch2_open_bucket_write_error(c    548                 bch2_open_bucket_write_error(c, &op->open_buckets, dev);
551                                                   549 
552         bch2_open_buckets_put(c, &op->open_buc    550         bch2_open_buckets_put(c, &op->open_buckets);
553         return;                                   551         return;
554 err:                                              552 err:
555         keys->top = keys->keys;                   553         keys->top = keys->keys;
556         op->error = ret;                          554         op->error = ret;
557         op->flags |= BCH_WRITE_SUBMITTED;      !! 555         op->flags |= BCH_WRITE_DONE;
558         goto out;                                 556         goto out;
559 }                                                 557 }
560                                                   558 
561 static inline void __wp_update_state(struct wr    559 static inline void __wp_update_state(struct write_point *wp, enum write_point_state state)
562 {                                                 560 {
563         if (state != wp->state) {                 561         if (state != wp->state) {
564                 u64 now = ktime_get_ns();         562                 u64 now = ktime_get_ns();
565                                                   563 
566                 if (wp->last_state_change &&      564                 if (wp->last_state_change &&
567                     time_after64(now, wp->last    565                     time_after64(now, wp->last_state_change))
568                         wp->time[wp->state] +=    566                         wp->time[wp->state] += now - wp->last_state_change;
569                 wp->state = state;                567                 wp->state = state;
570                 wp->last_state_change = now;      568                 wp->last_state_change = now;
571         }                                         569         }
572 }                                                 570 }
573                                                   571 
574 static inline void wp_update_state(struct writ    572 static inline void wp_update_state(struct write_point *wp, bool running)
575 {                                                 573 {
576         enum write_point_state state;             574         enum write_point_state state;
577                                                   575 
578         state = running                  ? WRI    576         state = running                  ? WRITE_POINT_running :
579                 !list_empty(&wp->writes) ? WRI    577                 !list_empty(&wp->writes) ? WRITE_POINT_waiting_io
580                                          : WRI    578                                          : WRITE_POINT_stopped;
581                                                   579 
582         __wp_update_state(wp, state);             580         __wp_update_state(wp, state);
583 }                                                 581 }
584                                                   582 
585 static CLOSURE_CALLBACK(bch2_write_index)         583 static CLOSURE_CALLBACK(bch2_write_index)
586 {                                                 584 {
587         closure_type(op, struct bch_write_op,     585         closure_type(op, struct bch_write_op, cl);
588         struct write_point *wp = op->wp;          586         struct write_point *wp = op->wp;
589         struct workqueue_struct *wq = index_up    587         struct workqueue_struct *wq = index_update_wq(op);
590         unsigned long flags;                      588         unsigned long flags;
591                                                   589 
592         if ((op->flags & BCH_WRITE_SUBMITTED)  !! 590         if ((op->flags & BCH_WRITE_DONE) &&
593             (op->flags & BCH_WRITE_MOVE))         591             (op->flags & BCH_WRITE_MOVE))
594                 bch2_bio_free_pages_pool(op->c    592                 bch2_bio_free_pages_pool(op->c, &op->wbio.bio);
595                                                   593 
596         spin_lock_irqsave(&wp->writes_lock, fl    594         spin_lock_irqsave(&wp->writes_lock, flags);
597         if (wp->state == WRITE_POINT_waiting_i    595         if (wp->state == WRITE_POINT_waiting_io)
598                 __wp_update_state(wp, WRITE_PO    596                 __wp_update_state(wp, WRITE_POINT_waiting_work);
599         list_add_tail(&op->wp_list, &wp->write    597         list_add_tail(&op->wp_list, &wp->writes);
600         spin_unlock_irqrestore (&wp->writes_lo    598         spin_unlock_irqrestore (&wp->writes_lock, flags);
601                                                   599 
602         queue_work(wq, &wp->index_update_work)    600         queue_work(wq, &wp->index_update_work);
603 }                                                 601 }
604                                                   602 
605 static inline void bch2_write_queue(struct bch    603 static inline void bch2_write_queue(struct bch_write_op *op, struct write_point *wp)
606 {                                                 604 {
607         op->wp = wp;                              605         op->wp = wp;
608                                                   606 
609         if (wp->state == WRITE_POINT_stopped)     607         if (wp->state == WRITE_POINT_stopped) {
610                 spin_lock_irq(&wp->writes_lock    608                 spin_lock_irq(&wp->writes_lock);
611                 __wp_update_state(wp, WRITE_PO    609                 __wp_update_state(wp, WRITE_POINT_waiting_io);
612                 spin_unlock_irq(&wp->writes_lo    610                 spin_unlock_irq(&wp->writes_lock);
613         }                                         611         }
614 }                                                 612 }
615                                                   613 
616 void bch2_write_point_do_index_updates(struct     614 void bch2_write_point_do_index_updates(struct work_struct *work)
617 {                                                 615 {
618         struct write_point *wp =                  616         struct write_point *wp =
619                 container_of(work, struct writ    617                 container_of(work, struct write_point, index_update_work);
620         struct bch_write_op *op;                  618         struct bch_write_op *op;
621                                                   619 
622         while (1) {                               620         while (1) {
623                 spin_lock_irq(&wp->writes_lock    621                 spin_lock_irq(&wp->writes_lock);
624                 op = list_first_entry_or_null(    622                 op = list_first_entry_or_null(&wp->writes, struct bch_write_op, wp_list);
625                 if (op)                           623                 if (op)
626                         list_del(&op->wp_list)    624                         list_del(&op->wp_list);
627                 wp_update_state(wp, op != NULL    625                 wp_update_state(wp, op != NULL);
628                 spin_unlock_irq(&wp->writes_lo    626                 spin_unlock_irq(&wp->writes_lock);
629                                                   627 
630                 if (!op)                          628                 if (!op)
631                         break;                    629                         break;
632                                                   630 
633                 op->flags |= BCH_WRITE_IN_WORK    631                 op->flags |= BCH_WRITE_IN_WORKER;
634                                                   632 
635                 __bch2_write_index(op);           633                 __bch2_write_index(op);
636                                                   634 
637                 if (!(op->flags & BCH_WRITE_SU !! 635                 if (!(op->flags & BCH_WRITE_DONE))
638                         __bch2_write(op);         636                         __bch2_write(op);
639                 else                              637                 else
640                         bch2_write_done(&op->c    638                         bch2_write_done(&op->cl);
641         }                                         639         }
642 }                                                 640 }
643                                                   641 
644 static void bch2_write_endio(struct bio *bio)     642 static void bch2_write_endio(struct bio *bio)
645 {                                                 643 {
646         struct closure *cl              = bio-    644         struct closure *cl              = bio->bi_private;
647         struct bch_write_op *op         = cont    645         struct bch_write_op *op         = container_of(cl, struct bch_write_op, cl);
648         struct bch_write_bio *wbio      = to_w    646         struct bch_write_bio *wbio      = to_wbio(bio);
649         struct bch_write_bio *parent    = wbio    647         struct bch_write_bio *parent    = wbio->split ? wbio->parent : NULL;
650         struct bch_fs *c                = wbio    648         struct bch_fs *c                = wbio->c;
651         struct bch_dev *ca              = wbio !! 649         struct bch_dev *ca              = bch_dev_bkey_exists(c, wbio->dev);
652                 ? bch2_dev_have_ref(c, wbio->d << 
653                 : NULL;                        << 
654                                                   650 
655         if (bch2_dev_inum_io_err_on(bio->bi_st    651         if (bch2_dev_inum_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
656                                     op->pos.in    652                                     op->pos.inode,
657                                     wbio->inod    653                                     wbio->inode_offset << 9,
658                                     "data writ    654                                     "data write error: %s",
659                                     bch2_blk_s    655                                     bch2_blk_status_to_str(bio->bi_status))) {
660                 set_bit(wbio->dev, op->failed.    656                 set_bit(wbio->dev, op->failed.d);
661                 op->flags |= BCH_WRITE_IO_ERRO    657                 op->flags |= BCH_WRITE_IO_ERROR;
662         }                                         658         }
663                                                   659 
664         if (wbio->nocow) {                     !! 660         if (wbio->nocow)
665                 bch2_bucket_nocow_unlock(&c->n << 
666                                          POS(c << 
667                                          BUCKE << 
668                 set_bit(wbio->dev, op->devs_ne    661                 set_bit(wbio->dev, op->devs_need_flush->d);
669         }                                      << 
670                                                   662 
671         if (wbio->have_ioref) {                   663         if (wbio->have_ioref) {
672                 bch2_latency_acct(ca, wbio->su    664                 bch2_latency_acct(ca, wbio->submit_time, WRITE);
673                 percpu_ref_put(&ca->io_ref);      665                 percpu_ref_put(&ca->io_ref);
674         }                                         666         }
675                                                   667 
676         if (wbio->bounce)                         668         if (wbio->bounce)
677                 bch2_bio_free_pages_pool(c, bi    669                 bch2_bio_free_pages_pool(c, bio);
678                                                   670 
679         if (wbio->put_bio)                        671         if (wbio->put_bio)
680                 bio_put(bio);                     672                 bio_put(bio);
681                                                   673 
682         if (parent)                               674         if (parent)
683                 bio_endio(&parent->bio);          675                 bio_endio(&parent->bio);
684         else                                      676         else
685                 closure_put(cl);                  677                 closure_put(cl);
686 }                                                 678 }
687                                                   679 
688 static void init_append_extent(struct bch_writ    680 static void init_append_extent(struct bch_write_op *op,
689                                struct write_po    681                                struct write_point *wp,
690                                struct bversion    682                                struct bversion version,
691                                struct bch_exte    683                                struct bch_extent_crc_unpacked crc)
692 {                                                 684 {
693         struct bkey_i_extent *e;                  685         struct bkey_i_extent *e;
694                                                   686 
695         op->pos.offset += crc.uncompressed_siz    687         op->pos.offset += crc.uncompressed_size;
696                                                   688 
697         e = bkey_extent_init(op->insert_keys.t    689         e = bkey_extent_init(op->insert_keys.top);
698         e->k.p          = op->pos;                690         e->k.p          = op->pos;
699         e->k.size       = crc.uncompressed_siz    691         e->k.size       = crc.uncompressed_size;
700         e->k.bversion   = version;             !! 692         e->k.version    = version;
701                                                   693 
702         if (crc.csum_type ||                      694         if (crc.csum_type ||
703             crc.compression_type ||               695             crc.compression_type ||
704             crc.nonce)                            696             crc.nonce)
705                 bch2_extent_crc_append(&e->k_i    697                 bch2_extent_crc_append(&e->k_i, crc);
706                                                   698 
707         bch2_alloc_sectors_append_ptrs_inlined    699         bch2_alloc_sectors_append_ptrs_inlined(op->c, wp, &e->k_i, crc.compressed_size,
708                                        op->fla    700                                        op->flags & BCH_WRITE_CACHED);
709                                                   701 
710         bch2_keylist_push(&op->insert_keys);      702         bch2_keylist_push(&op->insert_keys);
711 }                                                 703 }
712                                                   704 
713 static struct bio *bch2_write_bio_alloc(struct    705 static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
714                                         struct    706                                         struct write_point *wp,
715                                         struct    707                                         struct bio *src,
716                                         bool *    708                                         bool *page_alloc_failed,
717                                         void *    709                                         void *buf)
718 {                                                 710 {
719         struct bch_write_bio *wbio;               711         struct bch_write_bio *wbio;
720         struct bio *bio;                          712         struct bio *bio;
721         unsigned output_available =               713         unsigned output_available =
722                 min(wp->sectors_free << 9, src    714                 min(wp->sectors_free << 9, src->bi_iter.bi_size);
723         unsigned pages = DIV_ROUND_UP(output_a    715         unsigned pages = DIV_ROUND_UP(output_available +
724                                       (buf        716                                       (buf
725                                        ? ((uns    717                                        ? ((unsigned long) buf & (PAGE_SIZE - 1))
726                                        : 0), P    718                                        : 0), PAGE_SIZE);
727                                                   719 
728         pages = min(pages, BIO_MAX_VECS);         720         pages = min(pages, BIO_MAX_VECS);
729                                                   721 
730         bio = bio_alloc_bioset(NULL, pages, 0,    722         bio = bio_alloc_bioset(NULL, pages, 0,
731                                GFP_NOFS, &c->b    723                                GFP_NOFS, &c->bio_write);
732         wbio                    = wbio_init(bi    724         wbio                    = wbio_init(bio);
733         wbio->put_bio           = true;           725         wbio->put_bio           = true;
734         /* copy WRITE_SYNC flag */                726         /* copy WRITE_SYNC flag */
735         wbio->bio.bi_opf        = src->bi_opf;    727         wbio->bio.bi_opf        = src->bi_opf;
736                                                   728 
737         if (buf) {                                729         if (buf) {
738                 bch2_bio_map(bio, buf, output_    730                 bch2_bio_map(bio, buf, output_available);
739                 return bio;                       731                 return bio;
740         }                                         732         }
741                                                   733 
742         wbio->bounce            = true;           734         wbio->bounce            = true;
743                                                   735 
744         /*                                        736         /*
745          * We can't use mempool for more than     737          * We can't use mempool for more than c->sb.encoded_extent_max
746          * worth of pages, but we'd like to al    738          * worth of pages, but we'd like to allocate more if we can:
747          */                                       739          */
748         bch2_bio_alloc_pages_pool(c, bio,         740         bch2_bio_alloc_pages_pool(c, bio,
749                                   min_t(unsign    741                                   min_t(unsigned, output_available,
750                                         c->opt    742                                         c->opts.encoded_extent_max));
751                                                   743 
752         if (bio->bi_iter.bi_size < output_avai    744         if (bio->bi_iter.bi_size < output_available)
753                 *page_alloc_failed =              745                 *page_alloc_failed =
754                         bch2_bio_alloc_pages(b    746                         bch2_bio_alloc_pages(bio,
755                                              o    747                                              output_available -
756                                              b    748                                              bio->bi_iter.bi_size,
757                                              G    749                                              GFP_NOFS) != 0;
758                                                   750 
759         return bio;                               751         return bio;
760 }                                                 752 }
761                                                   753 
762 static int bch2_write_rechecksum(struct bch_fs    754 static int bch2_write_rechecksum(struct bch_fs *c,
763                                  struct bch_wr    755                                  struct bch_write_op *op,
764                                  unsigned new_    756                                  unsigned new_csum_type)
765 {                                                 757 {
766         struct bio *bio = &op->wbio.bio;          758         struct bio *bio = &op->wbio.bio;
767         struct bch_extent_crc_unpacked new_crc    759         struct bch_extent_crc_unpacked new_crc;
768         int ret;                                  760         int ret;
769                                                   761 
770         /* bch2_rechecksum_bio() can't encrypt    762         /* bch2_rechecksum_bio() can't encrypt or decrypt data: */
771                                                   763 
772         if (bch2_csum_type_is_encryption(op->c    764         if (bch2_csum_type_is_encryption(op->crc.csum_type) !=
773             bch2_csum_type_is_encryption(new_c    765             bch2_csum_type_is_encryption(new_csum_type))
774                 new_csum_type = op->crc.csum_t    766                 new_csum_type = op->crc.csum_type;
775                                                   767 
776         ret = bch2_rechecksum_bio(c, bio, op->    768         ret = bch2_rechecksum_bio(c, bio, op->version, op->crc,
777                                   NULL, &new_c    769                                   NULL, &new_crc,
778                                   op->crc.offs    770                                   op->crc.offset, op->crc.live_size,
779                                   new_csum_typ    771                                   new_csum_type);
780         if (ret)                                  772         if (ret)
781                 return ret;                       773                 return ret;
782                                                   774 
783         bio_advance(bio, op->crc.offset << 9);    775         bio_advance(bio, op->crc.offset << 9);
784         bio->bi_iter.bi_size = op->crc.live_si    776         bio->bi_iter.bi_size = op->crc.live_size << 9;
785         op->crc = new_crc;                        777         op->crc = new_crc;
786         return 0;                                 778         return 0;
787 }                                                 779 }
788                                                   780 
789 static int bch2_write_decrypt(struct bch_write    781 static int bch2_write_decrypt(struct bch_write_op *op)
790 {                                                 782 {
791         struct bch_fs *c = op->c;                 783         struct bch_fs *c = op->c;
792         struct nonce nonce = extent_nonce(op->    784         struct nonce nonce = extent_nonce(op->version, op->crc);
793         struct bch_csum csum;                     785         struct bch_csum csum;
794         int ret;                                  786         int ret;
795                                                   787 
796         if (!bch2_csum_type_is_encryption(op->    788         if (!bch2_csum_type_is_encryption(op->crc.csum_type))
797                 return 0;                         789                 return 0;
798                                                   790 
799         /*                                        791         /*
800          * If we need to decrypt data in the w    792          * If we need to decrypt data in the write path, we'll no longer be able
801          * to verify the existing checksum (po    793          * to verify the existing checksum (poly1305 mac, in this case) after
802          * it's decrypted - this is the last p    794          * it's decrypted - this is the last point we'll be able to reverify the
803          * checksum:                              795          * checksum:
804          */                                       796          */
805         csum = bch2_checksum_bio(c, op->crc.cs    797         csum = bch2_checksum_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
806         if (bch2_crc_cmp(op->crc.csum, csum) &    798         if (bch2_crc_cmp(op->crc.csum, csum) && !c->opts.no_data_io)
807                 return -EIO;                      799                 return -EIO;
808                                                   800 
809         ret = bch2_encrypt_bio(c, op->crc.csum    801         ret = bch2_encrypt_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
810         op->crc.csum_type = 0;                    802         op->crc.csum_type = 0;
811         op->crc.csum = (struct bch_csum) { 0,     803         op->crc.csum = (struct bch_csum) { 0, 0 };
812         return ret;                               804         return ret;
813 }                                                 805 }
814                                                   806 
815 static enum prep_encoded_ret {                    807 static enum prep_encoded_ret {
816         PREP_ENCODED_OK,                          808         PREP_ENCODED_OK,
817         PREP_ENCODED_ERR,                         809         PREP_ENCODED_ERR,
818         PREP_ENCODED_CHECKSUM_ERR,                810         PREP_ENCODED_CHECKSUM_ERR,
819         PREP_ENCODED_DO_WRITE,                    811         PREP_ENCODED_DO_WRITE,
820 } bch2_write_prep_encoded_data(struct bch_writ    812 } bch2_write_prep_encoded_data(struct bch_write_op *op, struct write_point *wp)
821 {                                                 813 {
822         struct bch_fs *c = op->c;                 814         struct bch_fs *c = op->c;
823         struct bio *bio = &op->wbio.bio;          815         struct bio *bio = &op->wbio.bio;
824                                                   816 
825         if (!(op->flags & BCH_WRITE_DATA_ENCOD    817         if (!(op->flags & BCH_WRITE_DATA_ENCODED))
826                 return PREP_ENCODED_OK;           818                 return PREP_ENCODED_OK;
827                                                   819 
828         BUG_ON(bio_sectors(bio) != op->crc.com    820         BUG_ON(bio_sectors(bio) != op->crc.compressed_size);
829                                                   821 
830         /* Can we just write the entire extent    822         /* Can we just write the entire extent as is? */
831         if (op->crc.uncompressed_size == op->c    823         if (op->crc.uncompressed_size == op->crc.live_size &&
832             op->crc.uncompressed_size <= c->op    824             op->crc.uncompressed_size <= c->opts.encoded_extent_max >> 9 &&
833             op->crc.compressed_size <= wp->sec    825             op->crc.compressed_size <= wp->sectors_free &&
834             (op->crc.compression_type == bch2_    826             (op->crc.compression_type == bch2_compression_opt_to_type(op->compression_opt) ||
835              op->incompressible)) {               827              op->incompressible)) {
836                 if (!crc_is_compressed(op->crc    828                 if (!crc_is_compressed(op->crc) &&
837                     op->csum_type != op->crc.c    829                     op->csum_type != op->crc.csum_type &&
838                     bch2_write_rechecksum(c, o    830                     bch2_write_rechecksum(c, op, op->csum_type) &&
839                     !c->opts.no_data_io)          831                     !c->opts.no_data_io)
840                         return PREP_ENCODED_CH    832                         return PREP_ENCODED_CHECKSUM_ERR;
841                                                   833 
842                 return PREP_ENCODED_DO_WRITE;     834                 return PREP_ENCODED_DO_WRITE;
843         }                                         835         }
844                                                   836 
845         /*                                        837         /*
846          * If the data is compressed and we co    838          * If the data is compressed and we couldn't write the entire extent as
847          * is, we have to decompress it:          839          * is, we have to decompress it:
848          */                                       840          */
849         if (crc_is_compressed(op->crc)) {         841         if (crc_is_compressed(op->crc)) {
850                 struct bch_csum csum;             842                 struct bch_csum csum;
851                                                   843 
852                 if (bch2_write_decrypt(op))       844                 if (bch2_write_decrypt(op))
853                         return PREP_ENCODED_CH    845                         return PREP_ENCODED_CHECKSUM_ERR;
854                                                   846 
855                 /* Last point we can still ver    847                 /* Last point we can still verify checksum: */
856                 csum = bch2_checksum_bio(c, op    848                 csum = bch2_checksum_bio(c, op->crc.csum_type,
857                                          exten    849                                          extent_nonce(op->version, op->crc),
858                                          bio);    850                                          bio);
859                 if (bch2_crc_cmp(op->crc.csum,    851                 if (bch2_crc_cmp(op->crc.csum, csum) && !c->opts.no_data_io)
860                         return PREP_ENCODED_CH    852                         return PREP_ENCODED_CHECKSUM_ERR;
861                                                   853 
862                 if (bch2_bio_uncompress_inplac    854                 if (bch2_bio_uncompress_inplace(c, bio, &op->crc))
863                         return PREP_ENCODED_ER    855                         return PREP_ENCODED_ERR;
864         }                                         856         }
865                                                   857 
866         /*                                        858         /*
867          * No longer have compressed data afte    859          * No longer have compressed data after this point - data might be
868          * encrypted:                             860          * encrypted:
869          */                                       861          */
870                                                   862 
871         /*                                        863         /*
872          * If the data is checksummed and we'r    864          * If the data is checksummed and we're only writing a subset,
873          * rechecksum and adjust bio to point     865          * rechecksum and adjust bio to point to currently live data:
874          */                                       866          */
875         if ((op->crc.live_size != op->crc.unco    867         if ((op->crc.live_size != op->crc.uncompressed_size ||
876              op->crc.csum_type != op->csum_typ    868              op->crc.csum_type != op->csum_type) &&
877             bch2_write_rechecksum(c, op, op->c    869             bch2_write_rechecksum(c, op, op->csum_type) &&
878             !c->opts.no_data_io)                  870             !c->opts.no_data_io)
879                 return PREP_ENCODED_CHECKSUM_E    871                 return PREP_ENCODED_CHECKSUM_ERR;
880                                                   872 
881         /*                                        873         /*
882          * If we want to compress the data, it    874          * If we want to compress the data, it has to be decrypted:
883          */                                       875          */
884         if ((op->compression_opt ||               876         if ((op->compression_opt ||
885              bch2_csum_type_is_encryption(op->    877              bch2_csum_type_is_encryption(op->crc.csum_type) !=
886              bch2_csum_type_is_encryption(op->    878              bch2_csum_type_is_encryption(op->csum_type)) &&
887             bch2_write_decrypt(op))               879             bch2_write_decrypt(op))
888                 return PREP_ENCODED_CHECKSUM_E    880                 return PREP_ENCODED_CHECKSUM_ERR;
889                                                   881 
890         return PREP_ENCODED_OK;                   882         return PREP_ENCODED_OK;
891 }                                                 883 }
892                                                   884 
893 static int bch2_write_extent(struct bch_write_    885 static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
894                              struct bio **_dst    886                              struct bio **_dst)
895 {                                                 887 {
896         struct bch_fs *c = op->c;                 888         struct bch_fs *c = op->c;
897         struct bio *src = &op->wbio.bio, *dst     889         struct bio *src = &op->wbio.bio, *dst = src;
898         struct bvec_iter saved_iter;              890         struct bvec_iter saved_iter;
899         void *ec_buf;                             891         void *ec_buf;
900         unsigned total_output = 0, total_input    892         unsigned total_output = 0, total_input = 0;
901         bool bounce = false;                      893         bool bounce = false;
902         bool page_alloc_failed = false;           894         bool page_alloc_failed = false;
903         int ret, more = 0;                        895         int ret, more = 0;
904                                                   896 
905         BUG_ON(!bio_sectors(src));                897         BUG_ON(!bio_sectors(src));
906                                                   898 
907         ec_buf = bch2_writepoint_ec_buf(c, wp)    899         ec_buf = bch2_writepoint_ec_buf(c, wp);
908                                                   900 
909         switch (bch2_write_prep_encoded_data(o    901         switch (bch2_write_prep_encoded_data(op, wp)) {
910         case PREP_ENCODED_OK:                     902         case PREP_ENCODED_OK:
911                 break;                            903                 break;
912         case PREP_ENCODED_ERR:                    904         case PREP_ENCODED_ERR:
913                 ret = -EIO;                       905                 ret = -EIO;
914                 goto err;                         906                 goto err;
915         case PREP_ENCODED_CHECKSUM_ERR:           907         case PREP_ENCODED_CHECKSUM_ERR:
916                 goto csum_err;                    908                 goto csum_err;
917         case PREP_ENCODED_DO_WRITE:               909         case PREP_ENCODED_DO_WRITE:
918                 /* XXX look for bug here */       910                 /* XXX look for bug here */
919                 if (ec_buf) {                     911                 if (ec_buf) {
920                         dst = bch2_write_bio_a    912                         dst = bch2_write_bio_alloc(c, wp, src,
921                                                   913                                                    &page_alloc_failed,
922                                                   914                                                    ec_buf);
923                         bio_copy_data(dst, src    915                         bio_copy_data(dst, src);
924                         bounce = true;            916                         bounce = true;
925                 }                                 917                 }
926                 init_append_extent(op, wp, op-    918                 init_append_extent(op, wp, op->version, op->crc);
927                 goto do_write;                    919                 goto do_write;
928         }                                         920         }
929                                                   921 
930         if (ec_buf ||                             922         if (ec_buf ||
931             op->compression_opt ||                923             op->compression_opt ||
932             (op->csum_type &&                     924             (op->csum_type &&
933              !(op->flags & BCH_WRITE_PAGES_STA    925              !(op->flags & BCH_WRITE_PAGES_STABLE)) ||
934             (bch2_csum_type_is_encryption(op->    926             (bch2_csum_type_is_encryption(op->csum_type) &&
935              !(op->flags & BCH_WRITE_PAGES_OWN    927              !(op->flags & BCH_WRITE_PAGES_OWNED))) {
936                 dst = bch2_write_bio_alloc(c,     928                 dst = bch2_write_bio_alloc(c, wp, src,
937                                            &pa    929                                            &page_alloc_failed,
938                                            ec_    930                                            ec_buf);
939                 bounce = true;                    931                 bounce = true;
940         }                                         932         }
941                                                   933 
942         saved_iter = dst->bi_iter;                934         saved_iter = dst->bi_iter;
943                                                   935 
944         do {                                      936         do {
945                 struct bch_extent_crc_unpacked    937                 struct bch_extent_crc_unpacked crc = { 0 };
946                 struct bversion version = op->    938                 struct bversion version = op->version;
947                 size_t dst_len = 0, src_len =     939                 size_t dst_len = 0, src_len = 0;
948                                                   940 
949                 if (page_alloc_failed &&          941                 if (page_alloc_failed &&
950                     dst->bi_iter.bi_size  < (w    942                     dst->bi_iter.bi_size  < (wp->sectors_free << 9) &&
951                     dst->bi_iter.bi_size < c->    943                     dst->bi_iter.bi_size < c->opts.encoded_extent_max)
952                         break;                    944                         break;
953                                                   945 
954                 BUG_ON(op->compression_opt &&     946                 BUG_ON(op->compression_opt &&
955                        (op->flags & BCH_WRITE_    947                        (op->flags & BCH_WRITE_DATA_ENCODED) &&
956                        bch2_csum_type_is_encry    948                        bch2_csum_type_is_encryption(op->crc.csum_type));
957                 BUG_ON(op->compression_opt &&     949                 BUG_ON(op->compression_opt && !bounce);
958                                                   950 
959                 crc.compression_type = op->inc    951                 crc.compression_type = op->incompressible
960                         ? BCH_COMPRESSION_TYPE    952                         ? BCH_COMPRESSION_TYPE_incompressible
961                         : op->compression_opt     953                         : op->compression_opt
962                         ? bch2_bio_compress(c,    954                         ? bch2_bio_compress(c, dst, &dst_len, src, &src_len,
963                                             op    955                                             op->compression_opt)
964                         : 0;                      956                         : 0;
965                 if (!crc_is_compressed(crc)) {    957                 if (!crc_is_compressed(crc)) {
966                         dst_len = min(dst->bi_    958                         dst_len = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
967                         dst_len = min_t(unsign    959                         dst_len = min_t(unsigned, dst_len, wp->sectors_free << 9);
968                                                   960 
969                         if (op->csum_type)        961                         if (op->csum_type)
970                                 dst_len = min_    962                                 dst_len = min_t(unsigned, dst_len,
971                                                   963                                                 c->opts.encoded_extent_max);
972                                                   964 
973                         if (bounce) {             965                         if (bounce) {
974                                 swap(dst->bi_i    966                                 swap(dst->bi_iter.bi_size, dst_len);
975                                 bio_copy_data(    967                                 bio_copy_data(dst, src);
976                                 swap(dst->bi_i    968                                 swap(dst->bi_iter.bi_size, dst_len);
977                         }                         969                         }
978                                                   970 
979                         src_len = dst_len;        971                         src_len = dst_len;
980                 }                                 972                 }
981                                                   973 
982                 BUG_ON(!src_len || !dst_len);     974                 BUG_ON(!src_len || !dst_len);
983                                                   975 
984                 if (bch2_csum_type_is_encrypti    976                 if (bch2_csum_type_is_encryption(op->csum_type)) {
985                         if (bversion_zero(vers    977                         if (bversion_zero(version)) {
986                                 version.lo = a    978                                 version.lo = atomic64_inc_return(&c->key_version);
987                         } else {                  979                         } else {
988                                 crc.nonce = op    980                                 crc.nonce = op->nonce;
989                                 op->nonce += s    981                                 op->nonce += src_len >> 9;
990                         }                         982                         }
991                 }                                 983                 }
992                                                   984 
993                 if ((op->flags & BCH_WRITE_DAT    985                 if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
994                     !crc_is_compressed(crc) &&    986                     !crc_is_compressed(crc) &&
995                     bch2_csum_type_is_encrypti    987                     bch2_csum_type_is_encryption(op->crc.csum_type) ==
996                     bch2_csum_type_is_encrypti    988                     bch2_csum_type_is_encryption(op->csum_type)) {
997                         u8 compression_type =     989                         u8 compression_type = crc.compression_type;
998                         u16 nonce = crc.nonce;    990                         u16 nonce = crc.nonce;
999                         /*                        991                         /*
1000                          * Note: when we're u    992                          * Note: when we're using rechecksum(), we need to be
1001                          * checksumming @src     993                          * checksumming @src because it has all the data our
1002                          * existing checksum     994                          * existing checksum covers - if we bounced (because we
1003                          * were trying to com    995                          * were trying to compress), @dst will only have the
1004                          * part of the data t    996                          * part of the data the new checksum will cover.
1005                          *                       997                          *
1006                          * But normally we wa    998                          * But normally we want to be checksumming post bounce,
1007                          * because part of th    999                          * because part of the reason for bouncing is so the
1008                          * data can't be modi    1000                          * data can't be modified (by userspace) while it's in
1009                          * flight.               1001                          * flight.
1010                          */                      1002                          */
1011                         if (bch2_rechecksum_b    1003                         if (bch2_rechecksum_bio(c, src, version, op->crc,
1012                                         &crc,    1004                                         &crc, &op->crc,
1013                                         src_l    1005                                         src_len >> 9,
1014                                         bio_s    1006                                         bio_sectors(src) - (src_len >> 9),
1015                                         op->c    1007                                         op->csum_type))
1016                                 goto csum_err    1008                                 goto csum_err;
1017                         /*                       1009                         /*
1018                          * rchecksum_bio sets    1010                          * rchecksum_bio sets compression_type on crc from op->crc,
1019                          * this isn't always     1011                          * this isn't always correct as sometimes we're changing
1020                          * an extent from unc    1012                          * an extent from uncompressed to incompressible.
1021                          */                      1013                          */
1022                         crc.compression_type     1014                         crc.compression_type = compression_type;
1023                         crc.nonce = nonce;       1015                         crc.nonce = nonce;
1024                 } else {                         1016                 } else {
1025                         if ((op->flags & BCH_    1017                         if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
1026                             bch2_rechecksum_b    1018                             bch2_rechecksum_bio(c, src, version, op->crc,
1027                                         NULL,    1019                                         NULL, &op->crc,
1028                                         src_l    1020                                         src_len >> 9,
1029                                         bio_s    1021                                         bio_sectors(src) - (src_len >> 9),
1030                                         op->c    1022                                         op->crc.csum_type))
1031                                 goto csum_err    1023                                 goto csum_err;
1032                                                  1024 
1033                         crc.compressed_size      1025                         crc.compressed_size     = dst_len >> 9;
1034                         crc.uncompressed_size    1026                         crc.uncompressed_size   = src_len >> 9;
1035                         crc.live_size            1027                         crc.live_size           = src_len >> 9;
1036                                                  1028 
1037                         swap(dst->bi_iter.bi_    1029                         swap(dst->bi_iter.bi_size, dst_len);
1038                         ret = bch2_encrypt_bi    1030                         ret = bch2_encrypt_bio(c, op->csum_type,
1039                                                  1031                                                extent_nonce(version, crc), dst);
1040                         if (ret)                 1032                         if (ret)
1041                                 goto err;        1033                                 goto err;
1042                                                  1034 
1043                         crc.csum = bch2_check    1035                         crc.csum = bch2_checksum_bio(c, op->csum_type,
1044                                          exte    1036                                          extent_nonce(version, crc), dst);
1045                         crc.csum_type = op->c    1037                         crc.csum_type = op->csum_type;
1046                         swap(dst->bi_iter.bi_    1038                         swap(dst->bi_iter.bi_size, dst_len);
1047                 }                                1039                 }
1048                                                  1040 
1049                 init_append_extent(op, wp, ve    1041                 init_append_extent(op, wp, version, crc);
1050                                                  1042 
1051                 if (dst != src)                  1043                 if (dst != src)
1052                         bio_advance(dst, dst_    1044                         bio_advance(dst, dst_len);
1053                 bio_advance(src, src_len);       1045                 bio_advance(src, src_len);
1054                 total_output    += dst_len;      1046                 total_output    += dst_len;
1055                 total_input     += src_len;      1047                 total_input     += src_len;
1056         } while (dst->bi_iter.bi_size &&         1048         } while (dst->bi_iter.bi_size &&
1057                  src->bi_iter.bi_size &&         1049                  src->bi_iter.bi_size &&
1058                  wp->sectors_free &&             1050                  wp->sectors_free &&
1059                  !bch2_keylist_realloc(&op->i    1051                  !bch2_keylist_realloc(&op->insert_keys,
1060                                       op->inl    1052                                       op->inline_keys,
1061                                       ARRAY_S    1053                                       ARRAY_SIZE(op->inline_keys),
1062                                       BKEY_EX    1054                                       BKEY_EXTENT_U64s_MAX));
1063                                                  1055 
1064         more = src->bi_iter.bi_size != 0;        1056         more = src->bi_iter.bi_size != 0;
1065                                                  1057 
1066         dst->bi_iter = saved_iter;               1058         dst->bi_iter = saved_iter;
1067                                                  1059 
1068         if (dst == src && more) {                1060         if (dst == src && more) {
1069                 BUG_ON(total_output != total_    1061                 BUG_ON(total_output != total_input);
1070                                                  1062 
1071                 dst = bio_split(src, total_in    1063                 dst = bio_split(src, total_input >> 9,
1072                                 GFP_NOFS, &c-    1064                                 GFP_NOFS, &c->bio_write);
1073                 wbio_init(dst)->put_bio = tru    1065                 wbio_init(dst)->put_bio = true;
1074                 /* copy WRITE_SYNC flag */       1066                 /* copy WRITE_SYNC flag */
1075                 dst->bi_opf             = src    1067                 dst->bi_opf             = src->bi_opf;
1076         }                                        1068         }
1077                                                  1069 
1078         dst->bi_iter.bi_size = total_output;     1070         dst->bi_iter.bi_size = total_output;
1079 do_write:                                        1071 do_write:
1080         *_dst = dst;                             1072         *_dst = dst;
1081         return more;                             1073         return more;
1082 csum_err:                                        1074 csum_err:
1083         bch_err_inum_offset_ratelimited(c,    !! 1075         bch_err(c, "error verifying existing checksum while rewriting existing data (memory corruption?)");
1084                 op->pos.inode,                << 
1085                 op->pos.offset << 9,          << 
1086                 "%s write error: error verify << 
1087                 op->flags & BCH_WRITE_MOVE ?  << 
1088         ret = -EIO;                              1076         ret = -EIO;
1089 err:                                             1077 err:
1090         if (to_wbio(dst)->bounce)                1078         if (to_wbio(dst)->bounce)
1091                 bch2_bio_free_pages_pool(c, d    1079                 bch2_bio_free_pages_pool(c, dst);
1092         if (to_wbio(dst)->put_bio)               1080         if (to_wbio(dst)->put_bio)
1093                 bio_put(dst);                    1081                 bio_put(dst);
1094                                                  1082 
1095         return ret;                              1083         return ret;
1096 }                                                1084 }
1097                                                  1085 
1098 static bool bch2_extent_is_writeable(struct b    1086 static bool bch2_extent_is_writeable(struct bch_write_op *op,
1099                                      struct b    1087                                      struct bkey_s_c k)
1100 {                                                1088 {
1101         struct bch_fs *c = op->c;                1089         struct bch_fs *c = op->c;
1102         struct bkey_s_c_extent e;                1090         struct bkey_s_c_extent e;
1103         struct extent_ptr_decoded p;             1091         struct extent_ptr_decoded p;
1104         const union bch_extent_entry *entry;     1092         const union bch_extent_entry *entry;
1105         unsigned replicas = 0;                   1093         unsigned replicas = 0;
1106                                                  1094 
1107         if (k.k->type != KEY_TYPE_extent)        1095         if (k.k->type != KEY_TYPE_extent)
1108                 return false;                    1096                 return false;
1109                                                  1097 
1110         e = bkey_s_c_to_extent(k);               1098         e = bkey_s_c_to_extent(k);
1111                                               << 
1112         rcu_read_lock();                      << 
1113         extent_for_each_ptr_decode(e, p, entr    1099         extent_for_each_ptr_decode(e, p, entry) {
1114                 if (crc_is_encoded(p.crc) ||  !! 1100                 if (crc_is_encoded(p.crc) || p.has_ec)
1115                         rcu_read_unlock();    << 
1116                         return false;            1101                         return false;
1117                 }                             << 
1118                                                  1102 
1119                 replicas += bch2_extent_ptr_d    1103                 replicas += bch2_extent_ptr_durability(c, &p);
1120         }                                        1104         }
1121         rcu_read_unlock();                    << 
1122                                                  1105 
1123         return replicas >= op->opts.data_repl    1106         return replicas >= op->opts.data_replicas;
1124 }                                                1107 }
1125                                                  1108 
                                                   >> 1109 static inline void bch2_nocow_write_unlock(struct bch_write_op *op)
                                                   >> 1110 {
                                                   >> 1111         struct bch_fs *c = op->c;
                                                   >> 1112         const struct bch_extent_ptr *ptr;
                                                   >> 1113         struct bkey_i *k;
                                                   >> 1114 
                                                   >> 1115         for_each_keylist_key(&op->insert_keys, k) {
                                                   >> 1116                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
                                                   >> 1117 
                                                   >> 1118                 bkey_for_each_ptr(ptrs, ptr)
                                                   >> 1119                         bch2_bucket_nocow_unlock(&c->nocow_locks,
                                                   >> 1120                                                PTR_BUCKET_POS(c, ptr),
                                                   >> 1121                                                BUCKET_NOCOW_LOCK_UPDATE);
                                                   >> 1122         }
                                                   >> 1123 }
                                                   >> 1124 
1126 static int bch2_nocow_write_convert_one_unwri    1125 static int bch2_nocow_write_convert_one_unwritten(struct btree_trans *trans,
1127                                                  1126                                                   struct btree_iter *iter,
1128                                                  1127                                                   struct bkey_i *orig,
1129                                                  1128                                                   struct bkey_s_c k,
1130                                                  1129                                                   u64 new_i_size)
1131 {                                                1130 {
                                                   >> 1131         struct bkey_i *new;
                                                   >> 1132         struct bkey_ptrs ptrs;
                                                   >> 1133         struct bch_extent_ptr *ptr;
                                                   >> 1134         int ret;
                                                   >> 1135 
1132         if (!bch2_extents_match(bkey_i_to_s_c    1136         if (!bch2_extents_match(bkey_i_to_s_c(orig), k)) {
1133                 /* trace this */                 1137                 /* trace this */
1134                 return 0;                        1138                 return 0;
1135         }                                        1139         }
1136                                                  1140 
1137         struct bkey_i *new = bch2_bkey_make_m !! 1141         new = bch2_bkey_make_mut_noupdate(trans, k);
1138         int ret = PTR_ERR_OR_ZERO(new);       !! 1142         ret = PTR_ERR_OR_ZERO(new);
1139         if (ret)                                 1143         if (ret)
1140                 return ret;                      1144                 return ret;
1141                                                  1145 
1142         bch2_cut_front(bkey_start_pos(&orig->    1146         bch2_cut_front(bkey_start_pos(&orig->k), new);
1143         bch2_cut_back(orig->k.p, new);           1147         bch2_cut_back(orig->k.p, new);
1144                                                  1148 
1145         struct bkey_ptrs ptrs = bch2_bkey_ptr !! 1149         ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
1146         bkey_for_each_ptr(ptrs, ptr)             1150         bkey_for_each_ptr(ptrs, ptr)
1147                 ptr->unwritten = 0;              1151                 ptr->unwritten = 0;
1148                                                  1152 
1149         /*                                       1153         /*
1150          * Note that we're not calling bch2_s    1154          * Note that we're not calling bch2_subvol_get_snapshot() in this path -
1151          * that was done when we kicked off t    1155          * that was done when we kicked off the write, and here it's important
1152          * that we update the extent that we     1156          * that we update the extent that we wrote to - even if a snapshot has
1153          * since been created. The write is s    1157          * since been created. The write is still outstanding, so we're ok
1154          * w.r.t. snapshot atomicity:            1158          * w.r.t. snapshot atomicity:
1155          */                                      1159          */
1156         return  bch2_extent_update_i_size_sec    1160         return  bch2_extent_update_i_size_sectors(trans, iter,
1157                                         min(n    1161                                         min(new->k.p.offset << 9, new_i_size), 0) ?:
1158                 bch2_trans_update(trans, iter    1162                 bch2_trans_update(trans, iter, new,
1159                                   BTREE_UPDAT !! 1163                                   BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1160 }                                                1164 }
1161                                                  1165 
1162 static void bch2_nocow_write_convert_unwritte    1166 static void bch2_nocow_write_convert_unwritten(struct bch_write_op *op)
1163 {                                                1167 {
1164         struct bch_fs *c = op->c;                1168         struct bch_fs *c = op->c;
1165         struct btree_trans *trans = bch2_tran    1169         struct btree_trans *trans = bch2_trans_get(c);
                                                   >> 1170         struct btree_iter iter;
                                                   >> 1171         struct bkey_i *orig;
                                                   >> 1172         struct bkey_s_c k;
                                                   >> 1173         int ret;
1166                                                  1174 
1167         for_each_keylist_key(&op->insert_keys    1175         for_each_keylist_key(&op->insert_keys, orig) {
1168                 int ret = for_each_btree_key_ !! 1176                 ret = for_each_btree_key_upto_commit(trans, iter, BTREE_ID_extents,
1169                                      bkey_sta    1177                                      bkey_start_pos(&orig->k), orig->k.p,
1170                                      BTREE_IT !! 1178                                      BTREE_ITER_INTENT, k,
1171                                      NULL, NU !! 1179                                      NULL, NULL, BTREE_INSERT_NOFAIL, ({
1172                         bch2_nocow_write_conv    1180                         bch2_nocow_write_convert_one_unwritten(trans, &iter, orig, k, op->new_i_size);
1173                 }));                             1181                 }));
1174                                                  1182 
1175                 if (ret && !bch2_err_matches(    1183                 if (ret && !bch2_err_matches(ret, EROFS)) {
1176                         struct bkey_i *insert    1184                         struct bkey_i *insert = bch2_keylist_front(&op->insert_keys);
1177                                                  1185 
1178                         bch_err_inum_offset_r    1186                         bch_err_inum_offset_ratelimited(c,
1179                                 insert->k.p.i    1187                                 insert->k.p.inode, insert->k.p.offset << 9,
1180                                 "%s write err !! 1188                                 "write error while doing btree update: %s",
1181                                 op->flags & B << 
1182                                 bch2_err_str(    1189                                 bch2_err_str(ret));
1183                 }                                1190                 }
1184                                                  1191 
1185                 if (ret) {                       1192                 if (ret) {
1186                         op->error = ret;         1193                         op->error = ret;
1187                         break;                   1194                         break;
1188                 }                                1195                 }
1189         }                                        1196         }
1190                                                  1197 
1191         bch2_trans_put(trans);                   1198         bch2_trans_put(trans);
1192 }                                                1199 }
1193                                                  1200 
1194 static void __bch2_nocow_write_done(struct bc    1201 static void __bch2_nocow_write_done(struct bch_write_op *op)
1195 {                                                1202 {
                                                   >> 1203         bch2_nocow_write_unlock(op);
                                                   >> 1204 
1196         if (unlikely(op->flags & BCH_WRITE_IO    1205         if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
1197                 op->error = -EIO;                1206                 op->error = -EIO;
1198         } else if (unlikely(op->flags & BCH_W    1207         } else if (unlikely(op->flags & BCH_WRITE_CONVERT_UNWRITTEN))
1199                 bch2_nocow_write_convert_unwr    1208                 bch2_nocow_write_convert_unwritten(op);
1200 }                                                1209 }
1201                                                  1210 
1202 static CLOSURE_CALLBACK(bch2_nocow_write_done    1211 static CLOSURE_CALLBACK(bch2_nocow_write_done)
1203 {                                                1212 {
1204         closure_type(op, struct bch_write_op,    1213         closure_type(op, struct bch_write_op, cl);
1205                                                  1214 
1206         __bch2_nocow_write_done(op);             1215         __bch2_nocow_write_done(op);
1207         bch2_write_done(cl);                     1216         bch2_write_done(cl);
1208 }                                                1217 }
1209                                                  1218 
1210 struct bucket_to_lock {                          1219 struct bucket_to_lock {
1211         struct bpos             b;               1220         struct bpos             b;
1212         unsigned                gen;             1221         unsigned                gen;
1213         struct nocow_lock_bucket *l;             1222         struct nocow_lock_bucket *l;
1214 };                                               1223 };
1215                                                  1224 
1216 static void bch2_nocow_write(struct bch_write    1225 static void bch2_nocow_write(struct bch_write_op *op)
1217 {                                                1226 {
1218         struct bch_fs *c = op->c;                1227         struct bch_fs *c = op->c;
1219         struct btree_trans *trans;               1228         struct btree_trans *trans;
1220         struct btree_iter iter;                  1229         struct btree_iter iter;
1221         struct bkey_s_c k;                       1230         struct bkey_s_c k;
                                                   >> 1231         struct bkey_ptrs_c ptrs;
                                                   >> 1232         const struct bch_extent_ptr *ptr;
1222         DARRAY_PREALLOCATED(struct bucket_to_    1233         DARRAY_PREALLOCATED(struct bucket_to_lock, 3) buckets;
                                                   >> 1234         struct bucket_to_lock *i;
1223         u32 snapshot;                            1235         u32 snapshot;
1224         struct bucket_to_lock *stale_at;         1236         struct bucket_to_lock *stale_at;
1225         int stale, ret;                       !! 1237         int ret;
1226                                                  1238 
1227         if (op->flags & BCH_WRITE_MOVE)          1239         if (op->flags & BCH_WRITE_MOVE)
1228                 return;                          1240                 return;
1229                                                  1241 
1230         darray_init(&buckets);                   1242         darray_init(&buckets);
1231         trans = bch2_trans_get(c);               1243         trans = bch2_trans_get(c);
1232 retry:                                           1244 retry:
1233         bch2_trans_begin(trans);                 1245         bch2_trans_begin(trans);
1234                                                  1246 
1235         ret = bch2_subvolume_get_snapshot(tra    1247         ret = bch2_subvolume_get_snapshot(trans, op->subvol, &snapshot);
1236         if (unlikely(ret))                       1248         if (unlikely(ret))
1237                 goto err;                        1249                 goto err;
1238                                                  1250 
1239         bch2_trans_iter_init(trans, &iter, BT    1251         bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1240                              SPOS(op->pos.ino    1252                              SPOS(op->pos.inode, op->pos.offset, snapshot),
1241                              BTREE_ITER_slots !! 1253                              BTREE_ITER_SLOTS);
1242         while (1) {                              1254         while (1) {
1243                 struct bio *bio = &op->wbio.b    1255                 struct bio *bio = &op->wbio.bio;
1244                                                  1256 
1245                 buckets.nr = 0;                  1257                 buckets.nr = 0;
1246                                                  1258 
1247                 ret = bch2_trans_relock(trans << 
1248                 if (ret)                      << 
1249                         break;                << 
1250                                               << 
1251                 k = bch2_btree_iter_peek_slot    1259                 k = bch2_btree_iter_peek_slot(&iter);
1252                 ret = bkey_err(k);               1260                 ret = bkey_err(k);
1253                 if (ret)                         1261                 if (ret)
1254                         break;                   1262                         break;
1255                                                  1263 
1256                 /* fall back to normal cow wr    1264                 /* fall back to normal cow write path? */
1257                 if (unlikely(k.k->p.snapshot     1265                 if (unlikely(k.k->p.snapshot != snapshot ||
1258                              !bch2_extent_is_    1266                              !bch2_extent_is_writeable(op, k)))
1259                         break;                   1267                         break;
1260                                                  1268 
1261                 if (bch2_keylist_realloc(&op-    1269                 if (bch2_keylist_realloc(&op->insert_keys,
1262                                          op->    1270                                          op->inline_keys,
1263                                          ARRA    1271                                          ARRAY_SIZE(op->inline_keys),
1264                                          k.k-    1272                                          k.k->u64s))
1265                         break;                   1273                         break;
1266                                                  1274 
1267                 /* Get iorefs before dropping    1275                 /* Get iorefs before dropping btree locks: */
1268                 struct bkey_ptrs_c ptrs = bch !! 1276                 ptrs = bch2_bkey_ptrs_c(k);
1269                 bkey_for_each_ptr(ptrs, ptr)     1277                 bkey_for_each_ptr(ptrs, ptr) {
1270                         struct bch_dev *ca =  !! 1278                         struct bpos b = PTR_BUCKET_POS(c, ptr);
1271                         if (unlikely(!ca))    << 
1272                                 goto err_get_ << 
1273                                               << 
1274                         struct bpos b = PTR_B << 
1275                         struct nocow_lock_buc    1279                         struct nocow_lock_bucket *l =
1276                                 bucket_nocow_    1280                                 bucket_nocow_lock(&c->nocow_locks, bucket_to_u64(b));
1277                         prefetch(l);             1281                         prefetch(l);
1278                                                  1282 
                                                   >> 1283                         if (unlikely(!bch2_dev_get_ioref(bch_dev_bkey_exists(c, ptr->dev), WRITE)))
                                                   >> 1284                                 goto err_get_ioref;
                                                   >> 1285 
1279                         /* XXX allocating mem    1286                         /* XXX allocating memory with btree locks held - rare */
1280                         darray_push_gfp(&buck    1287                         darray_push_gfp(&buckets, ((struct bucket_to_lock) {
1281                                                  1288                                                    .b = b, .gen = ptr->gen, .l = l,
1282                                                  1289                                                    }), GFP_KERNEL|__GFP_NOFAIL);
1283                                                  1290 
1284                         if (ptr->unwritten)      1291                         if (ptr->unwritten)
1285                                 op->flags |=     1292                                 op->flags |= BCH_WRITE_CONVERT_UNWRITTEN;
1286                 }                                1293                 }
1287                                                  1294 
1288                 /* Unlock before taking nocow    1295                 /* Unlock before taking nocow locks, doing IO: */
1289                 bkey_reassemble(op->insert_ke    1296                 bkey_reassemble(op->insert_keys.top, k);
1290                 bch2_trans_unlock(trans);        1297                 bch2_trans_unlock(trans);
1291                                                  1298 
1292                 bch2_cut_front(op->pos, op->i    1299                 bch2_cut_front(op->pos, op->insert_keys.top);
1293                 if (op->flags & BCH_WRITE_CON    1300                 if (op->flags & BCH_WRITE_CONVERT_UNWRITTEN)
1294                         bch2_cut_back(POS(op-    1301                         bch2_cut_back(POS(op->pos.inode, op->pos.offset + bio_sectors(bio)), op->insert_keys.top);
1295                                                  1302 
1296                 darray_for_each(buckets, i) {    1303                 darray_for_each(buckets, i) {
1297                         struct bch_dev *ca =  !! 1304                         struct bch_dev *ca = bch_dev_bkey_exists(c, i->b.inode);
1298                                                  1305 
1299                         __bch2_bucket_nocow_l    1306                         __bch2_bucket_nocow_lock(&c->nocow_locks, i->l,
1300                                                  1307                                                  bucket_to_u64(i->b),
1301                                                  1308                                                  BUCKET_NOCOW_LOCK_UPDATE);
1302                                                  1309 
1303                         int gen = bucket_gen_ !! 1310                         rcu_read_lock();
1304                         stale = gen < 0 ? gen !! 1311                         bool stale = gen_after(*bucket_gen(ca, i->b.offset), i->gen);
                                                   >> 1312                         rcu_read_unlock();
                                                   >> 1313 
1305                         if (unlikely(stale))     1314                         if (unlikely(stale)) {
1306                                 stale_at = i;    1315                                 stale_at = i;
1307                                 goto err_buck    1316                                 goto err_bucket_stale;
1308                         }                        1317                         }
1309                 }                                1318                 }
1310                                                  1319 
1311                 bio = &op->wbio.bio;             1320                 bio = &op->wbio.bio;
1312                 if (k.k->p.offset < op->pos.o    1321                 if (k.k->p.offset < op->pos.offset + bio_sectors(bio)) {
1313                         bio = bio_split(bio,     1322                         bio = bio_split(bio, k.k->p.offset - op->pos.offset,
1314                                         GFP_K    1323                                         GFP_KERNEL, &c->bio_write);
1315                         wbio_init(bio)->put_b    1324                         wbio_init(bio)->put_bio = true;
1316                         bio->bi_opf = op->wbi    1325                         bio->bi_opf = op->wbio.bio.bi_opf;
1317                 } else {                         1326                 } else {
1318                         op->flags |= BCH_WRIT !! 1327                         op->flags |= BCH_WRITE_DONE;
1319                 }                                1328                 }
1320                                                  1329 
1321                 op->pos.offset += bio_sectors    1330                 op->pos.offset += bio_sectors(bio);
1322                 op->written += bio_sectors(bi    1331                 op->written += bio_sectors(bio);
1323                                                  1332 
1324                 bio->bi_end_io  = bch2_write_    1333                 bio->bi_end_io  = bch2_write_endio;
1325                 bio->bi_private = &op->cl;       1334                 bio->bi_private = &op->cl;
1326                 bio->bi_opf |= REQ_OP_WRITE;     1335                 bio->bi_opf |= REQ_OP_WRITE;
1327                 closure_get(&op->cl);            1336                 closure_get(&op->cl);
1328                 bch2_submit_wbio_replicas(to_    1337                 bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1329                                           op-    1338                                           op->insert_keys.top, true);
1330                                                  1339 
1331                 bch2_keylist_push(&op->insert    1340                 bch2_keylist_push(&op->insert_keys);
1332                 if (op->flags & BCH_WRITE_SUB !! 1341                 if (op->flags & BCH_WRITE_DONE)
1333                         break;                   1342                         break;
1334                 bch2_btree_iter_advance(&iter    1343                 bch2_btree_iter_advance(&iter);
1335         }                                        1344         }
1336 out:                                             1345 out:
1337         bch2_trans_iter_exit(trans, &iter);      1346         bch2_trans_iter_exit(trans, &iter);
1338 err:                                             1347 err:
1339         if (bch2_err_matches(ret, BCH_ERR_tra    1348         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1340                 goto retry;                      1349                 goto retry;
1341                                                  1350 
1342         if (ret) {                               1351         if (ret) {
1343                 bch_err_inum_offset_ratelimit    1352                 bch_err_inum_offset_ratelimited(c,
1344                         op->pos.inode, op->po    1353                         op->pos.inode, op->pos.offset << 9,
1345                         "%s: btree lookup err    1354                         "%s: btree lookup error %s", __func__, bch2_err_str(ret));
1346                 op->error = ret;                 1355                 op->error = ret;
1347                 op->flags |= BCH_WRITE_SUBMIT !! 1356                 op->flags |= BCH_WRITE_DONE;
1348         }                                        1357         }
1349                                                  1358 
1350         bch2_trans_put(trans);                   1359         bch2_trans_put(trans);
1351         darray_exit(&buckets);                   1360         darray_exit(&buckets);
1352                                                  1361 
1353         /* fallback to cow write path? */        1362         /* fallback to cow write path? */
1354         if (!(op->flags & BCH_WRITE_SUBMITTED !! 1363         if (!(op->flags & BCH_WRITE_DONE)) {
1355                 closure_sync(&op->cl);           1364                 closure_sync(&op->cl);
1356                 __bch2_nocow_write_done(op);     1365                 __bch2_nocow_write_done(op);
1357                 op->insert_keys.top = op->ins    1366                 op->insert_keys.top = op->insert_keys.keys;
1358         } else if (op->flags & BCH_WRITE_SYNC    1367         } else if (op->flags & BCH_WRITE_SYNC) {
1359                 closure_sync(&op->cl);           1368                 closure_sync(&op->cl);
1360                 bch2_nocow_write_done(&op->cl    1369                 bch2_nocow_write_done(&op->cl.work);
1361         } else {                                 1370         } else {
1362                 /*                               1371                 /*
1363                  * XXX                           1372                  * XXX
1364                  * needs to run out of proces    1373                  * needs to run out of process context because ei_quota_lock is
1365                  * a mutex                       1374                  * a mutex
1366                  */                              1375                  */
1367                 continue_at(&op->cl, bch2_noc    1376                 continue_at(&op->cl, bch2_nocow_write_done, index_update_wq(op));
1368         }                                        1377         }
1369         return;                                  1378         return;
1370 err_get_ioref:                                   1379 err_get_ioref:
1371         darray_for_each(buckets, i)              1380         darray_for_each(buckets, i)
1372                 percpu_ref_put(&bch2_dev_have !! 1381                 percpu_ref_put(&bch_dev_bkey_exists(c, i->b.inode)->io_ref);
1373                                                  1382 
1374         /* Fall back to COW path: */             1383         /* Fall back to COW path: */
1375         goto out;                                1384         goto out;
1376 err_bucket_stale:                                1385 err_bucket_stale:
1377         darray_for_each(buckets, i) {            1386         darray_for_each(buckets, i) {
1378                 bch2_bucket_nocow_unlock(&c->    1387                 bch2_bucket_nocow_unlock(&c->nocow_locks, i->b, BUCKET_NOCOW_LOCK_UPDATE);
1379                 if (i == stale_at)               1388                 if (i == stale_at)
1380                         break;                   1389                         break;
1381         }                                        1390         }
1382                                                  1391 
1383         struct printbuf buf = PRINTBUF;       !! 1392         /* We can retry this: */
1384         if (bch2_fs_inconsistent_on(stale < 0 !! 1393         ret = -BCH_ERR_transaction_restart;
1385                                     "pointer  << 
1386                                     stale_at- << 
1387                                     (bch2_bke << 
1388                 ret = -EIO;                   << 
1389         } else {                              << 
1390                 /* We can retry this: */      << 
1391                 ret = -BCH_ERR_transaction_re << 
1392         }                                     << 
1393         printbuf_exit(&buf);                  << 
1394                                               << 
1395         goto err_get_ioref;                      1394         goto err_get_ioref;
1396 }                                                1395 }
1397                                                  1396 
1398 static void __bch2_write(struct bch_write_op     1397 static void __bch2_write(struct bch_write_op *op)
1399 {                                                1398 {
1400         struct bch_fs *c = op->c;                1399         struct bch_fs *c = op->c;
1401         struct write_point *wp = NULL;           1400         struct write_point *wp = NULL;
1402         struct bio *bio = NULL;                  1401         struct bio *bio = NULL;
1403         unsigned nofs_flags;                     1402         unsigned nofs_flags;
1404         int ret;                                 1403         int ret;
1405                                                  1404 
1406         nofs_flags = memalloc_nofs_save();       1405         nofs_flags = memalloc_nofs_save();
1407                                                  1406 
1408         if (unlikely(op->opts.nocow && c->opt    1407         if (unlikely(op->opts.nocow && c->opts.nocow_enabled)) {
1409                 bch2_nocow_write(op);            1408                 bch2_nocow_write(op);
1410                 if (op->flags & BCH_WRITE_SUB !! 1409                 if (op->flags & BCH_WRITE_DONE)
1411                         goto out_nofs_restore    1410                         goto out_nofs_restore;
1412         }                                        1411         }
1413 again:                                           1412 again:
1414         memset(&op->failed, 0, sizeof(op->fai    1413         memset(&op->failed, 0, sizeof(op->failed));
1415                                                  1414 
1416         do {                                     1415         do {
1417                 struct bkey_i *key_to_write;     1416                 struct bkey_i *key_to_write;
1418                 unsigned key_to_write_offset     1417                 unsigned key_to_write_offset = op->insert_keys.top_p -
1419                         op->insert_keys.keys_    1418                         op->insert_keys.keys_p;
1420                                                  1419 
1421                 /* +1 for possible cache devi    1420                 /* +1 for possible cache device: */
1422                 if (op->open_buckets.nr + op-    1421                 if (op->open_buckets.nr + op->nr_replicas + 1 >
1423                     ARRAY_SIZE(op->open_bucke    1422                     ARRAY_SIZE(op->open_buckets.v))
1424                         break;                   1423                         break;
1425                                                  1424 
1426                 if (bch2_keylist_realloc(&op-    1425                 if (bch2_keylist_realloc(&op->insert_keys,
1427                                         op->i    1426                                         op->inline_keys,
1428                                         ARRAY    1427                                         ARRAY_SIZE(op->inline_keys),
1429                                         BKEY_    1428                                         BKEY_EXTENT_U64s_MAX))
1430                         break;                   1429                         break;
1431                                                  1430 
1432                 /*                               1431                 /*
1433                  * The copygc thread is now g    1432                  * The copygc thread is now global, which means it's no longer
1434                  * freeing up space on specif    1433                  * freeing up space on specific disks, which means that
1435                  * allocations for specific d    1434                  * allocations for specific disks may hang arbitrarily long:
1436                  */                              1435                  */
1437                 ret = bch2_trans_run(c, lockr !! 1436                 ret = bch2_trans_do(c, NULL, NULL, 0,
1438                         bch2_alloc_sectors_st    1437                         bch2_alloc_sectors_start_trans(trans,
1439                                 op->target,      1438                                 op->target,
1440                                 op->opts.eras    1439                                 op->opts.erasure_code && !(op->flags & BCH_WRITE_CACHED),
1441                                 op->write_poi    1440                                 op->write_point,
1442                                 &op->devs_hav    1441                                 &op->devs_have,
1443                                 op->nr_replic    1442                                 op->nr_replicas,
1444                                 op->nr_replic    1443                                 op->nr_replicas_required,
1445                                 op->watermark    1444                                 op->watermark,
1446                                 op->flags,       1445                                 op->flags,
1447                                 &op->cl, &wp) !! 1446                                 (op->flags & (BCH_WRITE_ALLOC_NOWAIT|
                                                   >> 1447                                               BCH_WRITE_ONLY_SPECIFIED_DEVS))
                                                   >> 1448                                 ? NULL : &op->cl, &wp));
1448                 if (unlikely(ret)) {             1449                 if (unlikely(ret)) {
1449                         if (bch2_err_matches(    1450                         if (bch2_err_matches(ret, BCH_ERR_operation_blocked))
1450                                 break;           1451                                 break;
1451                                                  1452 
1452                         goto err;                1453                         goto err;
1453                 }                                1454                 }
1454                                                  1455 
1455                 EBUG_ON(!wp);                    1456                 EBUG_ON(!wp);
1456                                                  1457 
1457                 bch2_open_bucket_get(c, wp, &    1458                 bch2_open_bucket_get(c, wp, &op->open_buckets);
1458                 ret = bch2_write_extent(op, w    1459                 ret = bch2_write_extent(op, wp, &bio);
1459                                                  1460 
1460                 bch2_alloc_sectors_done_inlin    1461                 bch2_alloc_sectors_done_inlined(c, wp);
1461 err:                                             1462 err:
1462                 if (ret <= 0) {                  1463                 if (ret <= 0) {
1463                         op->flags |= BCH_WRIT !! 1464                         op->flags |= BCH_WRITE_DONE;
1464                                                  1465 
1465                         if (ret < 0) {           1466                         if (ret < 0) {
1466                                 if (!(op->fla << 
1467                                         bch_e << 
1468                                               << 
1469                                               << 
1470                                               << 
1471                                               << 
1472                                               << 
1473                                 op->error = r    1467                                 op->error = ret;
1474                                 break;           1468                                 break;
1475                         }                        1469                         }
1476                 }                                1470                 }
1477                                                  1471 
1478                 bio->bi_end_io  = bch2_write_    1472                 bio->bi_end_io  = bch2_write_endio;
1479                 bio->bi_private = &op->cl;       1473                 bio->bi_private = &op->cl;
1480                 bio->bi_opf |= REQ_OP_WRITE;     1474                 bio->bi_opf |= REQ_OP_WRITE;
1481                                                  1475 
1482                 closure_get(bio->bi_private);    1476                 closure_get(bio->bi_private);
1483                                                  1477 
1484                 key_to_write = (void *) (op->    1478                 key_to_write = (void *) (op->insert_keys.keys_p +
1485                                          key_    1479                                          key_to_write_offset);
1486                                                  1480 
1487                 bch2_submit_wbio_replicas(to_    1481                 bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1488                                           key    1482                                           key_to_write, false);
1489         } while (ret);                           1483         } while (ret);
1490                                                  1484 
1491         /*                                       1485         /*
1492          * Sync or no?                           1486          * Sync or no?
1493          *                                       1487          *
1494          * If we're running asynchronously, w    1488          * If we're running asynchronously, wne may still want to block
1495          * synchronously here if we weren't a    1489          * synchronously here if we weren't able to submit all of the IO at
1496          * once, as that signals backpressure    1490          * once, as that signals backpressure to the caller.
1497          */                                      1491          */
1498         if ((op->flags & BCH_WRITE_SYNC) ||      1492         if ((op->flags & BCH_WRITE_SYNC) ||
1499             (!(op->flags & BCH_WRITE_SUBMITTE !! 1493             (!(op->flags & BCH_WRITE_DONE) &&
1500              !(op->flags & BCH_WRITE_IN_WORKE    1494              !(op->flags & BCH_WRITE_IN_WORKER))) {
1501                 bch2_wait_on_allocator(c, &op !! 1495                 closure_sync(&op->cl);
1502                                               << 
1503                 __bch2_write_index(op);          1496                 __bch2_write_index(op);
1504                                                  1497 
1505                 if (!(op->flags & BCH_WRITE_S !! 1498                 if (!(op->flags & BCH_WRITE_DONE))
1506                         goto again;              1499                         goto again;
1507                 bch2_write_done(&op->cl);        1500                 bch2_write_done(&op->cl);
1508         } else {                                 1501         } else {
1509                 bch2_write_queue(op, wp);        1502                 bch2_write_queue(op, wp);
1510                 continue_at(&op->cl, bch2_wri    1503                 continue_at(&op->cl, bch2_write_index, NULL);
1511         }                                        1504         }
1512 out_nofs_restore:                                1505 out_nofs_restore:
1513         memalloc_nofs_restore(nofs_flags);       1506         memalloc_nofs_restore(nofs_flags);
1514 }                                                1507 }
1515                                                  1508 
1516 static void bch2_write_data_inline(struct bch    1509 static void bch2_write_data_inline(struct bch_write_op *op, unsigned data_len)
1517 {                                                1510 {
1518         struct bio *bio = &op->wbio.bio;         1511         struct bio *bio = &op->wbio.bio;
1519         struct bvec_iter iter;                   1512         struct bvec_iter iter;
1520         struct bkey_i_inline_data *id;           1513         struct bkey_i_inline_data *id;
1521         unsigned sectors;                        1514         unsigned sectors;
1522         int ret;                                 1515         int ret;
1523                                                  1516 
1524         memset(&op->failed, 0, sizeof(op->fai << 
1525                                               << 
1526         op->flags |= BCH_WRITE_WROTE_DATA_INL    1517         op->flags |= BCH_WRITE_WROTE_DATA_INLINE;
1527         op->flags |= BCH_WRITE_SUBMITTED;     !! 1518         op->flags |= BCH_WRITE_DONE;
1528                                                  1519 
1529         bch2_check_set_feature(op->c, BCH_FEA    1520         bch2_check_set_feature(op->c, BCH_FEATURE_inline_data);
1530                                                  1521 
1531         ret = bch2_keylist_realloc(&op->inser    1522         ret = bch2_keylist_realloc(&op->insert_keys, op->inline_keys,
1532                                    ARRAY_SIZE    1523                                    ARRAY_SIZE(op->inline_keys),
1533                                    BKEY_U64s     1524                                    BKEY_U64s + DIV_ROUND_UP(data_len, 8));
1534         if (ret) {                               1525         if (ret) {
1535                 op->error = ret;                 1526                 op->error = ret;
1536                 goto err;                        1527                 goto err;
1537         }                                        1528         }
1538                                                  1529 
1539         sectors = bio_sectors(bio);              1530         sectors = bio_sectors(bio);
1540         op->pos.offset += sectors;               1531         op->pos.offset += sectors;
1541                                                  1532 
1542         id = bkey_inline_data_init(op->insert    1533         id = bkey_inline_data_init(op->insert_keys.top);
1543         id->k.p         = op->pos;               1534         id->k.p         = op->pos;
1544         id->k.bversion  = op->version;        !! 1535         id->k.version   = op->version;
1545         id->k.size      = sectors;               1536         id->k.size      = sectors;
1546                                                  1537 
1547         iter = bio->bi_iter;                     1538         iter = bio->bi_iter;
1548         iter.bi_size = data_len;                 1539         iter.bi_size = data_len;
1549         memcpy_from_bio(id->v.data, bio, iter    1540         memcpy_from_bio(id->v.data, bio, iter);
1550                                                  1541 
1551         while (data_len & 7)                     1542         while (data_len & 7)
1552                 id->v.data[data_len++] = '\0'    1543                 id->v.data[data_len++] = '\0';
1553         set_bkey_val_bytes(&id->k, data_len);    1544         set_bkey_val_bytes(&id->k, data_len);
1554         bch2_keylist_push(&op->insert_keys);     1545         bch2_keylist_push(&op->insert_keys);
1555                                                  1546 
1556         __bch2_write_index(op);                  1547         __bch2_write_index(op);
1557 err:                                             1548 err:
1558         bch2_write_done(&op->cl);                1549         bch2_write_done(&op->cl);
1559 }                                                1550 }
1560                                                  1551 
1561 /**                                              1552 /**
1562  * bch2_write() - handle a write to a cache d    1553  * bch2_write() - handle a write to a cache device or flash only volume
1563  * @cl:         &bch_write_op->cl                1554  * @cl:         &bch_write_op->cl
1564  *                                               1555  *
1565  * This is the starting point for any data to    1556  * This is the starting point for any data to end up in a cache device; it could
1566  * be from a normal write, or a writeback wri    1557  * be from a normal write, or a writeback write, or a write to a flash only
1567  * volume - it's also used by the moving garb    1558  * volume - it's also used by the moving garbage collector to compact data in
1568  * mostly empty buckets.                         1559  * mostly empty buckets.
1569  *                                               1560  *
1570  * It first writes the data to the cache, cre    1561  * It first writes the data to the cache, creating a list of keys to be inserted
1571  * (if the data won't fit in a single open bu    1562  * (if the data won't fit in a single open bucket, there will be multiple keys);
1572  * after the data is written it calls bch_jou    1563  * after the data is written it calls bch_journal, and after the keys have been
1573  * added to the next journal write they're in    1564  * added to the next journal write they're inserted into the btree.
1574  *                                               1565  *
1575  * If op->discard is true, instead of inserti    1566  * If op->discard is true, instead of inserting the data it invalidates the
1576  * region of the cache represented by op->bio    1567  * region of the cache represented by op->bio and op->inode.
1577  */                                              1568  */
1578 CLOSURE_CALLBACK(bch2_write)                     1569 CLOSURE_CALLBACK(bch2_write)
1579 {                                                1570 {
1580         closure_type(op, struct bch_write_op,    1571         closure_type(op, struct bch_write_op, cl);
1581         struct bio *bio = &op->wbio.bio;         1572         struct bio *bio = &op->wbio.bio;
1582         struct bch_fs *c = op->c;                1573         struct bch_fs *c = op->c;
1583         unsigned data_len;                       1574         unsigned data_len;
1584                                                  1575 
1585         EBUG_ON(op->cl.parent);                  1576         EBUG_ON(op->cl.parent);
1586         BUG_ON(!op->nr_replicas);                1577         BUG_ON(!op->nr_replicas);
1587         BUG_ON(!op->write_point.v);              1578         BUG_ON(!op->write_point.v);
1588         BUG_ON(bkey_eq(op->pos, POS_MAX));       1579         BUG_ON(bkey_eq(op->pos, POS_MAX));
1589                                                  1580 
1590         if (op->flags & BCH_WRITE_ONLY_SPECIF << 
1591                 op->flags |= BCH_WRITE_ALLOC_ << 
1592                                               << 
1593         op->nr_replicas_required = min_t(unsi << 
1594         op->start_time = local_clock();          1581         op->start_time = local_clock();
1595         bch2_keylist_init(&op->insert_keys, o    1582         bch2_keylist_init(&op->insert_keys, op->inline_keys);
1596         wbio_init(bio)->put_bio = false;         1583         wbio_init(bio)->put_bio = false;
1597                                                  1584 
1598         if (bio->bi_iter.bi_size & (c->opts.b    1585         if (bio->bi_iter.bi_size & (c->opts.block_size - 1)) {
1599                 bch_err_inum_offset_ratelimit    1586                 bch_err_inum_offset_ratelimited(c,
1600                         op->pos.inode,           1587                         op->pos.inode,
1601                         op->pos.offset << 9,     1588                         op->pos.offset << 9,
1602                         "%s write error: misa !! 1589                         "misaligned write");
1603                         op->flags & BCH_WRITE << 
1604                 op->error = -EIO;                1590                 op->error = -EIO;
1605                 goto err;                        1591                 goto err;
1606         }                                        1592         }
1607                                                  1593 
1608         if (c->opts.nochanges) {                 1594         if (c->opts.nochanges) {
1609                 op->error = -BCH_ERR_erofs_no    1595                 op->error = -BCH_ERR_erofs_no_writes;
1610                 goto err;                        1596                 goto err;
1611         }                                        1597         }
1612                                                  1598 
1613         if (!(op->flags & BCH_WRITE_MOVE) &&     1599         if (!(op->flags & BCH_WRITE_MOVE) &&
1614             !bch2_write_ref_tryget(c, BCH_WRI    1600             !bch2_write_ref_tryget(c, BCH_WRITE_REF_write)) {
1615                 op->error = -BCH_ERR_erofs_no    1601                 op->error = -BCH_ERR_erofs_no_writes;
1616                 goto err;                        1602                 goto err;
1617         }                                        1603         }
1618                                                  1604 
1619         this_cpu_add(c->counters[BCH_COUNTER_    1605         this_cpu_add(c->counters[BCH_COUNTER_io_write], bio_sectors(bio));
1620         bch2_increment_clock(c, bio_sectors(b    1606         bch2_increment_clock(c, bio_sectors(bio), WRITE);
1621                                                  1607 
1622         data_len = min_t(u64, bio->bi_iter.bi    1608         data_len = min_t(u64, bio->bi_iter.bi_size,
1623                          op->new_i_size - (op    1609                          op->new_i_size - (op->pos.offset << 9));
1624                                                  1610 
1625         if (c->opts.inline_data &&               1611         if (c->opts.inline_data &&
1626             data_len <= min(block_bytes(c) /     1612             data_len <= min(block_bytes(c) / 2, 1024U)) {
1627                 bch2_write_data_inline(op, da    1613                 bch2_write_data_inline(op, data_len);
1628                 return;                          1614                 return;
1629         }                                        1615         }
1630                                                  1616 
1631         __bch2_write(op);                        1617         __bch2_write(op);
1632         return;                                  1618         return;
1633 err:                                             1619 err:
1634         bch2_disk_reservation_put(c, &op->res    1620         bch2_disk_reservation_put(c, &op->res);
1635                                                  1621 
1636         closure_debug_destroy(&op->cl);          1622         closure_debug_destroy(&op->cl);
1637         if (op->end_io)                          1623         if (op->end_io)
1638                 op->end_io(op);                  1624                 op->end_io(op);
1639 }                                                1625 }
1640                                                  1626 
1641 static const char * const bch2_write_flags[]     1627 static const char * const bch2_write_flags[] = {
1642 #define x(f)    #f,                              1628 #define x(f)    #f,
1643         BCH_WRITE_FLAGS()                        1629         BCH_WRITE_FLAGS()
1644 #undef x                                         1630 #undef x
1645         NULL                                     1631         NULL
1646 };                                               1632 };
1647                                                  1633 
1648 void bch2_write_op_to_text(struct printbuf *o    1634 void bch2_write_op_to_text(struct printbuf *out, struct bch_write_op *op)
1649 {                                                1635 {
1650         prt_str(out, "pos: ");                   1636         prt_str(out, "pos: ");
1651         bch2_bpos_to_text(out, op->pos);         1637         bch2_bpos_to_text(out, op->pos);
1652         prt_newline(out);                        1638         prt_newline(out);
1653         printbuf_indent_add(out, 2);             1639         printbuf_indent_add(out, 2);
1654                                                  1640 
1655         prt_str(out, "started: ");               1641         prt_str(out, "started: ");
1656         bch2_pr_time_units(out, local_clock()    1642         bch2_pr_time_units(out, local_clock() - op->start_time);
1657         prt_newline(out);                        1643         prt_newline(out);
1658                                                  1644 
1659         prt_str(out, "flags: ");                 1645         prt_str(out, "flags: ");
1660         prt_bitflags(out, bch2_write_flags, o    1646         prt_bitflags(out, bch2_write_flags, op->flags);
1661         prt_newline(out);                        1647         prt_newline(out);
1662                                                  1648 
1663         prt_printf(out, "ref: %u\n", closure_ !! 1649         prt_printf(out, "ref: %u", closure_nr_remaining(&op->cl));
                                                   >> 1650         prt_newline(out);
1664                                                  1651 
1665         printbuf_indent_sub(out, 2);             1652         printbuf_indent_sub(out, 2);
1666 }                                                1653 }
1667                                                  1654 
1668 void bch2_fs_io_write_exit(struct bch_fs *c)     1655 void bch2_fs_io_write_exit(struct bch_fs *c)
1669 {                                                1656 {
1670         mempool_exit(&c->bio_bounce_pages);      1657         mempool_exit(&c->bio_bounce_pages);
1671         bioset_exit(&c->replica_set);         << 
1672         bioset_exit(&c->bio_write);              1658         bioset_exit(&c->bio_write);
1673 }                                                1659 }
1674                                                  1660 
1675 int bch2_fs_io_write_init(struct bch_fs *c)      1661 int bch2_fs_io_write_init(struct bch_fs *c)
1676 {                                                1662 {
1677         if (bioset_init(&c->bio_write,   1, o !! 1663         if (bioset_init(&c->bio_write, 1, offsetof(struct bch_write_bio, bio),
1678             bioset_init(&c->replica_set, 4, o !! 1664                         BIOSET_NEED_BVECS))
1679                 return -BCH_ERR_ENOMEM_bio_wr    1665                 return -BCH_ERR_ENOMEM_bio_write_init;
1680                                                  1666 
1681         if (mempool_init_page_pool(&c->bio_bo    1667         if (mempool_init_page_pool(&c->bio_bounce_pages,
1682                                    max_t(unsi    1668                                    max_t(unsigned,
1683                                          c->o    1669                                          c->opts.btree_node_size,
1684                                          c->o    1670                                          c->opts.encoded_extent_max) /
1685                                    PAGE_SIZE,    1671                                    PAGE_SIZE, 0))
1686                 return -BCH_ERR_ENOMEM_bio_bo    1672                 return -BCH_ERR_ENOMEM_bio_bounce_pages_init;
1687                                                  1673 
1688         return 0;                                1674         return 0;
1689 }                                                1675 }
1690                                                  1676 

~ [ 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