1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2023-2025 Christoph Hellwig.
4 * Copyright (c) 2024-2025, Western Digital Corporation or its affiliates.
5 */
6 #include "xfs_platform.h"
7 #include "xfs_shared.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_error.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_iomap.h"
15 #include "xfs_trans.h"
16 #include "xfs_alloc.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bmap_btree.h"
19 #include "xfs_trans_space.h"
20 #include "xfs_refcount.h"
21 #include "xfs_rtbitmap.h"
22 #include "xfs_rtrmap_btree.h"
23 #include "xfs_zone_alloc.h"
24 #include "xfs_sysfs.h"
25 #include "xfs_zone_priv.h"
26 #include "xfs_zones.h"
27 #include "xfs_trace.h"
28 #include "xfs_mru_cache.h"
29
30 static void
xfs_open_zone_free_rcu(struct callback_head * cb)31 xfs_open_zone_free_rcu(
32 struct callback_head *cb)
33 {
34 struct xfs_open_zone *oz = container_of(cb, typeof(*oz), oz_rcu);
35
36 xfs_rtgroup_rele(oz->oz_rtg);
37 kfree(oz);
38 }
39
40 void
xfs_open_zone_put(struct xfs_open_zone * oz)41 xfs_open_zone_put(
42 struct xfs_open_zone *oz)
43 {
44 if (atomic_dec_and_test(&oz->oz_ref))
45 call_rcu(&oz->oz_rcu, xfs_open_zone_free_rcu);
46 }
47
48 static inline uint32_t
xfs_zone_bucket(struct xfs_mount * mp,uint32_t used_blocks)49 xfs_zone_bucket(
50 struct xfs_mount *mp,
51 uint32_t used_blocks)
52 {
53 return XFS_ZONE_USED_BUCKETS * used_blocks /
54 mp->m_groups[XG_TYPE_RTG].blocks;
55 }
56
57 static inline void
xfs_zone_add_to_bucket(struct xfs_zone_info * zi,xfs_rgnumber_t rgno,uint32_t to_bucket)58 xfs_zone_add_to_bucket(
59 struct xfs_zone_info *zi,
60 xfs_rgnumber_t rgno,
61 uint32_t to_bucket)
62 {
63 __set_bit(rgno, zi->zi_used_bucket_bitmap[to_bucket]);
64 zi->zi_used_bucket_entries[to_bucket]++;
65 }
66
67 static inline void
xfs_zone_remove_from_bucket(struct xfs_zone_info * zi,xfs_rgnumber_t rgno,uint32_t from_bucket)68 xfs_zone_remove_from_bucket(
69 struct xfs_zone_info *zi,
70 xfs_rgnumber_t rgno,
71 uint32_t from_bucket)
72 {
73 __clear_bit(rgno, zi->zi_used_bucket_bitmap[from_bucket]);
74 zi->zi_used_bucket_entries[from_bucket]--;
75 }
76
77 static void
xfs_zone_account_reclaimable(struct xfs_rtgroup * rtg,uint32_t freed)78 xfs_zone_account_reclaimable(
79 struct xfs_rtgroup *rtg,
80 uint32_t freed)
81 {
82 struct xfs_group *xg = rtg_group(rtg);
83 struct xfs_mount *mp = rtg_mount(rtg);
84 struct xfs_zone_info *zi = mp->m_zone_info;
85 uint32_t used = rtg_rmap(rtg)->i_used_blocks;
86 xfs_rgnumber_t rgno = rtg_rgno(rtg);
87 uint32_t from_bucket = xfs_zone_bucket(mp, used + freed);
88 uint32_t to_bucket = xfs_zone_bucket(mp, used);
89 bool was_full = (used + freed == rtg_blocks(rtg));
90
91 /*
92 * This can be called from log recovery, where the zone_info structure
93 * hasn't been allocated yet. Skip all work as xfs_mount_zones will
94 * add the zones to the right buckets before the file systems becomes
95 * active.
96 */
97 if (!zi)
98 return;
99
100 if (!used) {
101 /*
102 * The zone is now empty, remove it from the bottom bucket and
103 * trigger a reset.
104 */
105 trace_xfs_zone_emptied(rtg);
106
107 spin_lock(&zi->zi_used_buckets_lock);
108 if (!was_full)
109 xfs_zone_remove_from_bucket(zi, rgno, from_bucket);
110 spin_unlock(&zi->zi_used_buckets_lock);
111
112 spin_lock(&zi->zi_reset_list_lock);
113 xg->xg_next_reset = zi->zi_reset_list;
114 zi->zi_reset_list = xg;
115 spin_unlock(&zi->zi_reset_list_lock);
116
117 if (zi->zi_gc_thread)
118 wake_up_process(zi->zi_gc_thread);
119 } else if (was_full) {
120 /*
121 * The zone transitioned from full, mark it up as reclaimable
122 * and wake up GC which might be waiting for zones to reclaim.
123 */
124 spin_lock(&zi->zi_used_buckets_lock);
125 xfs_zone_add_to_bucket(zi, rgno, to_bucket);
126 spin_unlock(&zi->zi_used_buckets_lock);
127
128 if (zi->zi_gc_thread && xfs_zoned_need_gc(mp))
129 wake_up_process(zi->zi_gc_thread);
130 } else if (to_bucket != from_bucket) {
131 /*
132 * Move the zone to a new bucket if it dropped below the
133 * threshold.
134 */
135 spin_lock(&zi->zi_used_buckets_lock);
136 xfs_zone_add_to_bucket(zi, rgno, to_bucket);
137 xfs_zone_remove_from_bucket(zi, rgno, from_bucket);
138 spin_unlock(&zi->zi_used_buckets_lock);
139 }
140 }
141
142 /*
143 * Check if we have any zones that can be reclaimed by looking at the entry
144 * counters for the zone buckets.
145 */
146 bool
xfs_zoned_have_reclaimable(struct xfs_zone_info * zi)147 xfs_zoned_have_reclaimable(
148 struct xfs_zone_info *zi)
149 {
150 int i;
151
152 spin_lock(&zi->zi_used_buckets_lock);
153 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) {
154 if (zi->zi_used_bucket_entries[i]) {
155 spin_unlock(&zi->zi_used_buckets_lock);
156 return true;
157 }
158 }
159 spin_unlock(&zi->zi_used_buckets_lock);
160
161 return false;
162 }
163
164 static void
xfs_open_zone_mark_full(struct xfs_open_zone * oz)165 xfs_open_zone_mark_full(
166 struct xfs_open_zone *oz)
167 {
168 struct xfs_rtgroup *rtg = oz->oz_rtg;
169 struct xfs_mount *mp = rtg_mount(rtg);
170 struct xfs_zone_info *zi = mp->m_zone_info;
171 uint32_t used = rtg_rmap(rtg)->i_used_blocks;
172
173 trace_xfs_zone_full(rtg);
174
175 WRITE_ONCE(rtg->rtg_open_zone, NULL);
176
177 spin_lock(&zi->zi_open_zones_lock);
178 if (oz->oz_is_gc)
179 zi->zi_nr_open_gc_zones--;
180 else
181 zi->zi_nr_open_zones--;
182 list_del_init(&oz->oz_entry);
183 spin_unlock(&zi->zi_open_zones_lock);
184
185 if (oz->oz_is_gc)
186 wake_up_process(zi->zi_gc_thread);
187 else
188 wake_up_all(&zi->zi_zone_wait);
189
190 if (used < rtg_blocks(rtg))
191 xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used);
192 xfs_open_zone_put(oz);
193 }
194
195 static inline void
xfs_zone_inc_written(struct xfs_open_zone * oz,xfs_filblks_t len)196 xfs_zone_inc_written(
197 struct xfs_open_zone *oz,
198 xfs_filblks_t len)
199 {
200 xfs_assert_ilocked(rtg_rmap(oz->oz_rtg), XFS_ILOCK_EXCL);
201
202 oz->oz_written += len;
203 if (oz->oz_written == rtg_blocks(oz->oz_rtg))
204 xfs_open_zone_mark_full(oz);
205 }
206
207 /*
208 * Called for blocks that have been written to disk, but not actually linked to
209 * an inode, which can happen when garbage collection races with user data
210 * writes to a file.
211 */
212 static void
xfs_zone_skip_blocks(struct xfs_open_zone * oz,xfs_filblks_t len)213 xfs_zone_skip_blocks(
214 struct xfs_open_zone *oz,
215 xfs_filblks_t len)
216 {
217 struct xfs_rtgroup *rtg = oz->oz_rtg;
218
219 trace_xfs_zone_skip_blocks(oz, 0, len);
220
221 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
222 xfs_zone_inc_written(oz, len);
223 xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
224
225 xfs_add_frextents(rtg_mount(rtg), len);
226 }
227
228 static int
xfs_zoned_map_extent(struct xfs_trans * tp,struct xfs_inode * ip,struct xfs_bmbt_irec * new,struct xfs_open_zone * oz,xfs_fsblock_t old_startblock)229 xfs_zoned_map_extent(
230 struct xfs_trans *tp,
231 struct xfs_inode *ip,
232 struct xfs_bmbt_irec *new,
233 struct xfs_open_zone *oz,
234 xfs_fsblock_t old_startblock)
235 {
236 struct xfs_bmbt_irec data;
237 struct xfs_rtgroup *rtg = oz->oz_rtg;
238 struct xfs_inode *rmapip = rtg_rmap(rtg);
239 int nmaps = 1;
240 int error;
241
242 /* Grab the corresponding mapping in the data fork. */
243 error = xfs_bmapi_read(ip, new->br_startoff, new->br_blockcount, &data,
244 &nmaps, 0);
245 if (error)
246 return error;
247
248 /*
249 * Cap the update to the existing extent in the data fork because we can
250 * only overwrite one extent at a time.
251 */
252 ASSERT(new->br_blockcount >= data.br_blockcount);
253 new->br_blockcount = data.br_blockcount;
254
255 /*
256 * If a data write raced with this GC write, keep the existing data in
257 * the data fork, mark our newly written GC extent as reclaimable, then
258 * move on to the next extent.
259 *
260 * Note that this can also happen when racing with operations that do
261 * not actually invalidate the data, but just move it to a different
262 * inode (XFS_IOC_EXCHANGE_RANGE), or to a different offset inside the
263 * inode (FALLOC_FL_COLLAPSE_RANGE / FALLOC_FL_INSERT_RANGE). If the
264 * data was just moved around, GC fails to free the zone, but the zone
265 * becomes a GC candidate again as soon as all previous GC I/O has
266 * finished and these blocks will be moved out eventually.
267 */
268 if (old_startblock != NULLFSBLOCK &&
269 old_startblock != data.br_startblock)
270 goto skip;
271
272 trace_xfs_reflink_cow_remap_from(ip, new);
273 trace_xfs_reflink_cow_remap_to(ip, &data);
274
275 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
276 XFS_IEXT_REFLINK_END_COW_CNT);
277 if (error)
278 return error;
279
280 if (data.br_startblock != HOLESTARTBLOCK) {
281 ASSERT(data.br_startblock != DELAYSTARTBLOCK);
282 ASSERT(!isnullstartblock(data.br_startblock));
283
284 xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &data);
285 if (xfs_is_reflink_inode(ip)) {
286 xfs_refcount_decrease_extent(tp, true, &data);
287 } else {
288 error = xfs_free_extent_later(tp, data.br_startblock,
289 data.br_blockcount, NULL,
290 XFS_AG_RESV_NONE,
291 XFS_FREE_EXTENT_REALTIME);
292 if (error)
293 return error;
294 }
295 }
296
297 trace_xfs_zone_record_blocks(oz,
298 xfs_rtb_to_rgbno(tp->t_mountp, new->br_startblock),
299 new->br_blockcount);
300 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
301 xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
302 rmapip->i_used_blocks += new->br_blockcount;
303 ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg));
304 xfs_zone_inc_written(oz, new->br_blockcount);
305 xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
306
307 /* Map the new blocks into the data fork. */
308 xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, new);
309 return 0;
310
311 skip:
312 trace_xfs_reflink_cow_remap_skip(ip, new);
313 xfs_zone_skip_blocks(oz, new->br_blockcount);
314 return 0;
315 }
316
317 int
xfs_zoned_end_io(struct xfs_inode * ip,xfs_off_t offset,xfs_off_t count,xfs_daddr_t daddr,struct xfs_open_zone * oz,xfs_fsblock_t old_startblock)318 xfs_zoned_end_io(
319 struct xfs_inode *ip,
320 xfs_off_t offset,
321 xfs_off_t count,
322 xfs_daddr_t daddr,
323 struct xfs_open_zone *oz,
324 xfs_fsblock_t old_startblock)
325 {
326 struct xfs_mount *mp = ip->i_mount;
327 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
328 struct xfs_bmbt_irec new = {
329 .br_startoff = XFS_B_TO_FSBT(mp, offset),
330 .br_startblock = xfs_daddr_to_rtb(mp, daddr),
331 .br_state = XFS_EXT_NORM,
332 };
333 unsigned int resblks =
334 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
335 struct xfs_trans *tp;
336 int error;
337
338 if (xfs_is_shutdown(mp))
339 return -EIO;
340
341 while (new.br_startoff < end_fsb) {
342 new.br_blockcount = end_fsb - new.br_startoff;
343
344 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
345 XFS_TRANS_RESERVE | XFS_TRANS_RES_FDBLKS, &tp);
346 if (error)
347 return error;
348 xfs_ilock(ip, XFS_ILOCK_EXCL);
349 xfs_trans_ijoin(tp, ip, 0);
350
351 error = xfs_zoned_map_extent(tp, ip, &new, oz, old_startblock);
352 if (error)
353 xfs_trans_cancel(tp);
354 else
355 error = xfs_trans_commit(tp);
356 xfs_iunlock(ip, XFS_ILOCK_EXCL);
357 if (error)
358 return error;
359
360 new.br_startoff += new.br_blockcount;
361 new.br_startblock += new.br_blockcount;
362 if (old_startblock != NULLFSBLOCK)
363 old_startblock += new.br_blockcount;
364 }
365
366 return 0;
367 }
368
369 /*
370 * "Free" blocks allocated in a zone.
371 *
372 * Just decrement the used blocks counter and report the space as freed.
373 */
374 int
xfs_zone_free_blocks(struct xfs_trans * tp,struct xfs_rtgroup * rtg,xfs_fsblock_t fsbno,xfs_filblks_t len)375 xfs_zone_free_blocks(
376 struct xfs_trans *tp,
377 struct xfs_rtgroup *rtg,
378 xfs_fsblock_t fsbno,
379 xfs_filblks_t len)
380 {
381 struct xfs_mount *mp = tp->t_mountp;
382 struct xfs_inode *rmapip = rtg_rmap(rtg);
383
384 xfs_assert_ilocked(rmapip, XFS_ILOCK_EXCL);
385
386 if (len > rmapip->i_used_blocks) {
387 xfs_err(mp,
388 "trying to free more blocks (%lld) than used counter (%u).",
389 len, rmapip->i_used_blocks);
390 ASSERT(len <= rmapip->i_used_blocks);
391 xfs_rtginode_mark_sick(rtg, XFS_RTGI_RMAP);
392 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
393 return -EFSCORRUPTED;
394 }
395
396 trace_xfs_zone_free_blocks(rtg, xfs_rtb_to_rgbno(mp, fsbno), len);
397
398 rmapip->i_used_blocks -= len;
399 /*
400 * Don't add open zones to the reclaimable buckets. The I/O completion
401 * for writing the last block will take care of accounting for already
402 * unused blocks instead.
403 */
404 if (!READ_ONCE(rtg->rtg_open_zone))
405 xfs_zone_account_reclaimable(rtg, len);
406 xfs_add_frextents(mp, len);
407 xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
408 return 0;
409 }
410
411 static struct xfs_open_zone *
xfs_init_open_zone(struct xfs_rtgroup * rtg,xfs_rgblock_t write_pointer,enum rw_hint write_hint,bool is_gc)412 xfs_init_open_zone(
413 struct xfs_rtgroup *rtg,
414 xfs_rgblock_t write_pointer,
415 enum rw_hint write_hint,
416 bool is_gc)
417 {
418 struct xfs_open_zone *oz;
419
420 oz = kzalloc_obj(*oz, GFP_NOFS | __GFP_NOFAIL);
421 spin_lock_init(&oz->oz_alloc_lock);
422 atomic_set(&oz->oz_ref, 1);
423 oz->oz_rtg = rtg;
424 oz->oz_allocated = write_pointer;
425 oz->oz_written = write_pointer;
426 oz->oz_write_hint = write_hint;
427 oz->oz_is_gc = is_gc;
428
429 /*
430 * All dereferences of rtg->rtg_open_zone hold the ILOCK for the rmap
431 * inode, but we don't really want to take that here because we are
432 * under the zone_list_lock. Ensure the pointer is only set for a fully
433 * initialized open zone structure so that a racy lookup finding it is
434 * fine.
435 */
436 WRITE_ONCE(rtg->rtg_open_zone, oz);
437 return oz;
438 }
439
440 /*
441 * Find a completely free zone, open it, and return a reference.
442 */
443 struct xfs_open_zone *
xfs_open_zone(struct xfs_mount * mp,enum rw_hint write_hint,bool is_gc)444 xfs_open_zone(
445 struct xfs_mount *mp,
446 enum rw_hint write_hint,
447 bool is_gc)
448 {
449 struct xfs_zone_info *zi = mp->m_zone_info;
450 XA_STATE (xas, &mp->m_groups[XG_TYPE_RTG].xa, 0);
451 struct xfs_group *xg;
452
453 /*
454 * Pick the free zone with lowest index. Zones in the beginning of the
455 * address space typically provides higher bandwidth than those at the
456 * end of the address space on HDDs.
457 */
458 xas_lock(&xas);
459 xas_for_each_marked(&xas, xg, ULONG_MAX, XFS_RTG_FREE)
460 if (atomic_inc_not_zero(&xg->xg_active_ref))
461 goto found;
462 xas_unlock(&xas);
463 return NULL;
464
465 found:
466 xas_clear_mark(&xas, XFS_RTG_FREE);
467 atomic_dec(&zi->zi_nr_free_zones);
468 xas_unlock(&xas);
469
470 set_current_state(TASK_RUNNING);
471 return xfs_init_open_zone(to_rtg(xg), 0, write_hint, is_gc);
472 }
473
474 static struct xfs_open_zone *
xfs_try_open_zone(struct xfs_mount * mp,enum rw_hint write_hint)475 xfs_try_open_zone(
476 struct xfs_mount *mp,
477 enum rw_hint write_hint)
478 {
479 struct xfs_zone_info *zi = mp->m_zone_info;
480 struct xfs_open_zone *oz;
481
482 if (zi->zi_nr_open_zones >= mp->m_max_open_zones - XFS_OPEN_GC_ZONES)
483 return NULL;
484 if (atomic_read(&zi->zi_nr_free_zones) <
485 XFS_GC_ZONES - XFS_OPEN_GC_ZONES)
486 return NULL;
487
488 /*
489 * Increment the open zone count to reserve our slot before dropping
490 * zi_open_zones_lock.
491 */
492 zi->zi_nr_open_zones++;
493 spin_unlock(&zi->zi_open_zones_lock);
494 oz = xfs_open_zone(mp, write_hint, false);
495 spin_lock(&zi->zi_open_zones_lock);
496 if (!oz) {
497 zi->zi_nr_open_zones--;
498 return NULL;
499 }
500
501 atomic_inc(&oz->oz_ref);
502 list_add_tail(&oz->oz_entry, &zi->zi_open_zones);
503
504 /*
505 * If this was the last free zone, other waiters might be waiting
506 * on us to write to it as well.
507 */
508 wake_up_all(&zi->zi_zone_wait);
509
510 if (xfs_zoned_need_gc(mp))
511 wake_up_process(zi->zi_gc_thread);
512
513 trace_xfs_zone_opened(oz->oz_rtg);
514 return oz;
515 }
516
517 enum xfs_zone_alloc_score {
518 /* Any open zone will do it, we're desperate */
519 XFS_ZONE_ALLOC_ANY = 0,
520
521 /* It better fit somehow */
522 XFS_ZONE_ALLOC_OK = 1,
523
524 /* Only reuse a zone if it fits really well. */
525 XFS_ZONE_ALLOC_GOOD = 2,
526 };
527
528 /*
529 * Life time hint co-location matrix. Fields not set default to 0
530 * aka XFS_ZONE_ALLOC_ANY.
531 */
532 static const unsigned int
533 xfs_zoned_hint_score[WRITE_LIFE_HINT_NR][WRITE_LIFE_HINT_NR] = {
534 [WRITE_LIFE_NOT_SET] = {
535 [WRITE_LIFE_NOT_SET] = XFS_ZONE_ALLOC_OK,
536 },
537 [WRITE_LIFE_NONE] = {
538 [WRITE_LIFE_NONE] = XFS_ZONE_ALLOC_OK,
539 },
540 [WRITE_LIFE_SHORT] = {
541 [WRITE_LIFE_SHORT] = XFS_ZONE_ALLOC_GOOD,
542 },
543 [WRITE_LIFE_MEDIUM] = {
544 [WRITE_LIFE_MEDIUM] = XFS_ZONE_ALLOC_GOOD,
545 },
546 [WRITE_LIFE_LONG] = {
547 [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK,
548 [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK,
549 },
550 [WRITE_LIFE_EXTREME] = {
551 [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK,
552 [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK,
553 },
554 };
555
556 static bool
xfs_try_use_zone(struct xfs_zone_info * zi,enum rw_hint file_hint,struct xfs_open_zone * oz,unsigned int goodness)557 xfs_try_use_zone(
558 struct xfs_zone_info *zi,
559 enum rw_hint file_hint,
560 struct xfs_open_zone *oz,
561 unsigned int goodness)
562 {
563 if (oz->oz_is_gc)
564 return false;
565
566 if (oz->oz_allocated == rtg_blocks(oz->oz_rtg))
567 return false;
568
569 if (xfs_zoned_hint_score[oz->oz_write_hint][file_hint] < goodness)
570 return false;
571
572 if (!atomic_inc_not_zero(&oz->oz_ref))
573 return false;
574
575 /*
576 * If we have a hint set for the data, use that for the zone even if
577 * some data was written already without any hint set, but don't change
578 * the temperature after that as that would make little sense without
579 * tracking per-temperature class written block counts, which is
580 * probably overkill anyway.
581 */
582 if (file_hint != WRITE_LIFE_NOT_SET &&
583 oz->oz_write_hint == WRITE_LIFE_NOT_SET)
584 oz->oz_write_hint = file_hint;
585
586 /*
587 * If we couldn't match by inode or life time we just pick the first
588 * zone with enough space above. For that we want the least busy zone
589 * for some definition of "least" busy. For now this simple LRU
590 * algorithm that rotates every zone to the end of the list will do it,
591 * even if it isn't exactly cache friendly.
592 */
593 if (!list_is_last(&oz->oz_entry, &zi->zi_open_zones))
594 list_move_tail(&oz->oz_entry, &zi->zi_open_zones);
595 return true;
596 }
597
598 static struct xfs_open_zone *
xfs_select_open_zone_lru(struct xfs_zone_info * zi,enum rw_hint file_hint,unsigned int goodness)599 xfs_select_open_zone_lru(
600 struct xfs_zone_info *zi,
601 enum rw_hint file_hint,
602 unsigned int goodness)
603 {
604 struct xfs_open_zone *oz;
605
606 lockdep_assert_held(&zi->zi_open_zones_lock);
607
608 list_for_each_entry(oz, &zi->zi_open_zones, oz_entry)
609 if (xfs_try_use_zone(zi, file_hint, oz, goodness))
610 return oz;
611
612 cond_resched_lock(&zi->zi_open_zones_lock);
613 return NULL;
614 }
615
616 static struct xfs_open_zone *
xfs_select_open_zone_mru(struct xfs_zone_info * zi,enum rw_hint file_hint)617 xfs_select_open_zone_mru(
618 struct xfs_zone_info *zi,
619 enum rw_hint file_hint)
620 {
621 struct xfs_open_zone *oz;
622
623 lockdep_assert_held(&zi->zi_open_zones_lock);
624
625 list_for_each_entry_reverse(oz, &zi->zi_open_zones, oz_entry)
626 if (xfs_try_use_zone(zi, file_hint, oz, XFS_ZONE_ALLOC_OK))
627 return oz;
628
629 cond_resched_lock(&zi->zi_open_zones_lock);
630 return NULL;
631 }
632
xfs_inode_write_hint(struct xfs_inode * ip)633 static inline enum rw_hint xfs_inode_write_hint(struct xfs_inode *ip)
634 {
635 if (xfs_has_nolifetime(ip->i_mount))
636 return WRITE_LIFE_NOT_SET;
637 return VFS_I(ip)->i_write_hint;
638 }
639
640 /*
641 * Try to tightly pack small files that are written back after they were closed
642 * instead of trying to open new zones for them or spread them to the least
643 * recently used zone. This optimizes the data layout for workloads that untar
644 * or copy a lot of small files. Right now this does not separate multiple such
645 * streams.
646 */
xfs_zoned_pack_tight(struct xfs_inode * ip)647 static inline bool xfs_zoned_pack_tight(struct xfs_inode *ip)
648 {
649 struct xfs_mount *mp = ip->i_mount;
650 size_t zone_capacity =
651 XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_RTG].blocks);
652
653 /*
654 * Do not pack write files that are already using a full zone to avoid
655 * fragmentation.
656 */
657 if (i_size_read(VFS_I(ip)) >= zone_capacity)
658 return false;
659
660 return !inode_is_open_for_write(VFS_I(ip)) &&
661 !(ip->i_diflags & XFS_DIFLAG_APPEND);
662 }
663
664 static struct xfs_open_zone *
xfs_select_zone_nowait(struct xfs_mount * mp,enum rw_hint write_hint,bool pack_tight)665 xfs_select_zone_nowait(
666 struct xfs_mount *mp,
667 enum rw_hint write_hint,
668 bool pack_tight)
669 {
670 struct xfs_zone_info *zi = mp->m_zone_info;
671 struct xfs_open_zone *oz = NULL;
672
673 if (xfs_is_shutdown(mp))
674 return NULL;
675
676 /*
677 * Try to fill up open zones with matching temperature if available. It
678 * is better to try to co-locate data when this is favorable, so we can
679 * activate empty zones when it is statistically better to separate
680 * data.
681 */
682 spin_lock(&zi->zi_open_zones_lock);
683 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_GOOD);
684 if (oz)
685 goto out_unlock;
686
687 if (pack_tight) {
688 oz = xfs_select_open_zone_mru(zi, write_hint);
689 if (oz)
690 goto out_unlock;
691 }
692
693 /*
694 * See if we can open a new zone and use that so that data for different
695 * files is mixed as little as possible.
696 */
697 oz = xfs_try_open_zone(mp, write_hint);
698 if (oz)
699 goto out_unlock;
700
701 /*
702 * Try to find a zone that is an ok match to colocate data with.
703 */
704 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_OK);
705 if (oz)
706 goto out_unlock;
707
708 /*
709 * Pick the least recently used zone, regardless of hint match
710 */
711 oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_ANY);
712 out_unlock:
713 spin_unlock(&zi->zi_open_zones_lock);
714 return oz;
715 }
716
717 static struct xfs_open_zone *
xfs_select_zone(struct xfs_mount * mp,enum rw_hint write_hint,bool pack_tight)718 xfs_select_zone(
719 struct xfs_mount *mp,
720 enum rw_hint write_hint,
721 bool pack_tight)
722 {
723 struct xfs_zone_info *zi = mp->m_zone_info;
724 DEFINE_WAIT (wait);
725 struct xfs_open_zone *oz;
726
727 oz = xfs_select_zone_nowait(mp, write_hint, pack_tight);
728 if (oz)
729 return oz;
730
731 for (;;) {
732 prepare_to_wait(&zi->zi_zone_wait, &wait, TASK_UNINTERRUPTIBLE);
733 oz = xfs_select_zone_nowait(mp, write_hint, pack_tight);
734 if (oz || xfs_is_shutdown(mp))
735 break;
736 schedule();
737 }
738 finish_wait(&zi->zi_zone_wait, &wait);
739 return oz;
740 }
741
742 static unsigned int
xfs_zone_alloc_blocks(struct xfs_open_zone * oz,xfs_filblks_t count_fsb,sector_t * sector,bool * is_seq)743 xfs_zone_alloc_blocks(
744 struct xfs_open_zone *oz,
745 xfs_filblks_t count_fsb,
746 sector_t *sector,
747 bool *is_seq)
748 {
749 struct xfs_rtgroup *rtg = oz->oz_rtg;
750 struct xfs_mount *mp = rtg_mount(rtg);
751 xfs_rgblock_t allocated;
752
753 spin_lock(&oz->oz_alloc_lock);
754 count_fsb = min3(count_fsb, XFS_MAX_BMBT_EXTLEN,
755 (xfs_filblks_t)rtg_blocks(rtg) - oz->oz_allocated);
756 if (!count_fsb) {
757 spin_unlock(&oz->oz_alloc_lock);
758 return 0;
759 }
760 allocated = oz->oz_allocated;
761 oz->oz_allocated += count_fsb;
762 spin_unlock(&oz->oz_alloc_lock);
763
764 trace_xfs_zone_alloc_blocks(oz, allocated, count_fsb);
765
766 *sector = xfs_gbno_to_daddr(rtg_group(rtg), 0);
767 *is_seq = bdev_zone_is_seq(mp->m_rtdev_targp->bt_bdev, *sector);
768 if (!*is_seq)
769 *sector += XFS_FSB_TO_BB(mp, allocated);
770 return XFS_FSB_TO_B(mp, count_fsb);
771 }
772
773 void
xfs_mark_rtg_boundary(struct iomap_ioend * ioend)774 xfs_mark_rtg_boundary(
775 struct iomap_ioend *ioend)
776 {
777 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
778 sector_t sector = ioend->io_bio.bi_iter.bi_sector;
779
780 if (xfs_rtb_to_rgbno(mp, xfs_daddr_to_rtb(mp, sector)) == 0)
781 ioend->io_flags |= IOMAP_IOEND_BOUNDARY;
782 }
783
784 /*
785 * Check if we have a cached last open zone available for the inode and
786 * if yes return a reference to it.
787 */
788 static struct xfs_open_zone *
xfs_get_cached_zone(struct xfs_inode * ip)789 xfs_get_cached_zone(
790 struct xfs_inode *ip)
791 {
792 struct xfs_open_zone *oz;
793
794 rcu_read_lock();
795 oz = VFS_I(ip)->i_private;
796 if (!oz)
797 goto out_unlock;
798
799 /*
800 * GC only steals open zones at mount time, so no GC zones should end up
801 * in the cache.
802 */
803 ASSERT(!oz->oz_is_gc);
804
805 /*
806 * Drop the old cached open zone if it is full.
807 */
808 if (oz->oz_allocated == rtg_blocks(oz->oz_rtg)) {
809 spin_lock(&ip->i_flags_lock);
810 oz = VFS_I(ip)->i_private;
811 if (oz && oz->oz_allocated == rtg_blocks(oz->oz_rtg)) {
812 VFS_I(ip)->i_private = NULL;
813 spin_unlock(&ip->i_flags_lock);
814 xfs_open_zone_put(oz);
815 oz = NULL;
816 goto out_unlock;
817 }
818 spin_unlock(&ip->i_flags_lock);
819 }
820
821 if (!atomic_inc_not_zero(&oz->oz_ref))
822 oz = NULL;
823 out_unlock:
824 rcu_read_unlock();
825 return oz;
826 }
827
828 /*
829 * Stash our zone in the inode so that is is reused for future allocations.
830 *
831 * The open_zone structure will be pinned until either the inode is freed or
832 * until the cached open zone is replaced with a different one because the
833 * current one was full when we tried to use it. This means we keep any
834 * open zone around forever as long as any inode that used it for the last
835 * write is cached, which slightly increases the memory use of cached inodes
836 * that were every written to, but significantly simplifies the cached zone
837 * lookup. Because the open_zone is clearly marked as full when all data
838 * in the underlying RTG was written, the caching is always safe.
839 *
840 * Called with a reference on @oz held. And returns two references on the
841 * returned zone: one for the caller and one for pinning the zone in
842 * inode->i_private.
843 */
844 static struct xfs_open_zone *
xfs_set_cached_zone(struct xfs_inode * ip,struct xfs_open_zone * oz)845 xfs_set_cached_zone(
846 struct xfs_inode *ip,
847 struct xfs_open_zone *oz)
848 {
849 struct xfs_open_zone *old_oz;
850
851 /*
852 * If the open zone cached in the inode still has free space, use that
853 * instead of the new open zone just selected. This can happen when
854 * multiple threads race to perform zone selection for an inode.
855 * io_uring worker threads seem to be good way to trigger this.
856 *
857 * We need to grab an extra reference to this open zone as the caller
858 * owns a reference in addition to the i_private pointer.
859 */
860 spin_lock(&ip->i_flags_lock);
861 old_oz = VFS_I(ip)->i_private;
862 if (old_oz && old_oz->oz_allocated < rtg_blocks(old_oz->oz_rtg) &&
863 atomic_inc_not_zero(&old_oz->oz_ref)) {
864 spin_unlock(&ip->i_flags_lock);
865 xfs_open_zone_put(oz);
866 return old_oz;
867 }
868 VFS_I(ip)->i_private = oz;
869 atomic_inc(&oz->oz_ref);
870 spin_unlock(&ip->i_flags_lock);
871 if (old_oz)
872 xfs_open_zone_put(old_oz);
873 return oz;
874 }
875
876 static void
xfs_submit_zoned_bio(struct iomap_ioend * ioend,struct xfs_open_zone * oz,bool is_seq)877 xfs_submit_zoned_bio(
878 struct iomap_ioend *ioend,
879 struct xfs_open_zone *oz,
880 bool is_seq)
881 {
882 ioend->io_bio.bi_iter.bi_sector = ioend->io_sector;
883 ioend->io_private = oz;
884 atomic_inc(&oz->oz_ref); /* for xfs_zoned_end_io */
885
886 if (is_seq) {
887 ioend->io_bio.bi_opf &= ~REQ_OP_WRITE;
888 ioend->io_bio.bi_opf |= REQ_OP_ZONE_APPEND;
889 } else {
890 xfs_mark_rtg_boundary(ioend);
891 }
892
893 submit_bio(&ioend->io_bio);
894 }
895
896 void
xfs_zone_alloc_and_submit(struct iomap_ioend * ioend,struct xfs_open_zone ** oz)897 xfs_zone_alloc_and_submit(
898 struct iomap_ioend *ioend,
899 struct xfs_open_zone **oz)
900 {
901 struct xfs_inode *ip = XFS_I(ioend->io_inode);
902 struct xfs_mount *mp = ip->i_mount;
903 enum rw_hint write_hint = xfs_inode_write_hint(ip);
904 bool pack_tight = xfs_zoned_pack_tight(ip);
905 unsigned int alloc_len;
906 struct iomap_ioend *split;
907 bool is_seq;
908
909 if (xfs_is_shutdown(mp))
910 goto out_error;
911
912 /*
913 * If we don't have a locally cached zone in this write context, see if
914 * the inode is still associated with a zone and use that if so.
915 */
916 if (!*oz)
917 select_zone:
918 *oz = xfs_get_cached_zone(ip);
919 if (!*oz) {
920 *oz = xfs_select_zone(mp, write_hint, pack_tight);
921 if (!*oz)
922 goto out_error;
923 *oz = xfs_set_cached_zone(ip, *oz);
924 }
925
926 alloc_len = xfs_zone_alloc_blocks(*oz, XFS_B_TO_FSB(mp, ioend->io_size),
927 &ioend->io_sector, &is_seq);
928 if (!alloc_len) {
929 xfs_open_zone_put(*oz);
930 goto select_zone;
931 }
932
933 while ((split = iomap_split_ioend(ioend, alloc_len, is_seq))) {
934 if (IS_ERR(split))
935 goto out_split_error;
936 alloc_len -= split->io_bio.bi_iter.bi_size;
937 xfs_submit_zoned_bio(split, *oz, is_seq);
938 if (!alloc_len) {
939 xfs_open_zone_put(*oz);
940 goto select_zone;
941 }
942 }
943
944 xfs_submit_zoned_bio(ioend, *oz, is_seq);
945 return;
946
947 out_split_error:
948 ioend->io_bio.bi_status = errno_to_blk_status(PTR_ERR(split));
949 out_error:
950 bio_io_error(&ioend->io_bio);
951 }
952
953 /*
954 * Wake up all threads waiting for a zoned space allocation when the file system
955 * is shut down.
956 */
957 void
xfs_zoned_wake_all(struct xfs_mount * mp)958 xfs_zoned_wake_all(
959 struct xfs_mount *mp)
960 {
961 /*
962 * Don't wake up if there is no m_zone_info. This is complicated by the
963 * fact that unmount can't atomically clear m_zone_info and thus we need
964 * to check SB_ACTIVE for that, but mount temporarily enables SB_ACTIVE
965 * during log recovery so we can't entirely rely on that either.
966 */
967 if ((mp->m_super->s_flags & SB_ACTIVE) && mp->m_zone_info)
968 wake_up_all(&mp->m_zone_info->zi_zone_wait);
969 }
970
971 /*
972 * Check if @rgbno in @rgb is a potentially valid block. It might still be
973 * unused, but that information is only found in the rmap.
974 */
975 bool
xfs_zone_rgbno_is_valid(struct xfs_rtgroup * rtg,xfs_rgnumber_t rgbno)976 xfs_zone_rgbno_is_valid(
977 struct xfs_rtgroup *rtg,
978 xfs_rgnumber_t rgbno)
979 {
980 lockdep_assert_held(&rtg_rmap(rtg)->i_lock);
981
982 if (rtg->rtg_open_zone)
983 return rgbno < rtg->rtg_open_zone->oz_allocated;
984 return !xa_get_mark(&rtg_mount(rtg)->m_groups[XG_TYPE_RTG].xa,
985 rtg_rgno(rtg), XFS_RTG_FREE);
986 }
987
988 void
xfs_zone_mark_free(struct xfs_rtgroup * rtg)989 xfs_zone_mark_free(
990 struct xfs_rtgroup *rtg)
991 {
992 xfs_group_set_mark(rtg_group(rtg), XFS_RTG_FREE);
993 atomic_inc(&rtg_mount(rtg)->m_zone_info->zi_nr_free_zones);
994 }
995
996 static void
xfs_free_open_zones(struct xfs_zone_info * zi)997 xfs_free_open_zones(
998 struct xfs_zone_info *zi)
999 {
1000 struct xfs_open_zone *oz;
1001
1002 spin_lock(&zi->zi_open_zones_lock);
1003 while ((oz = list_first_entry_or_null(&zi->zi_open_zones,
1004 struct xfs_open_zone, oz_entry))) {
1005 list_del(&oz->oz_entry);
1006 xfs_open_zone_put(oz);
1007 }
1008 spin_unlock(&zi->zi_open_zones_lock);
1009
1010 /*
1011 * Wait for all open zones to be freed so that they drop the group
1012 * references:
1013 */
1014 rcu_barrier();
1015 }
1016
1017 struct xfs_init_zones {
1018 uint32_t zone_size;
1019 uint32_t zone_capacity;
1020 uint64_t available;
1021 uint64_t reclaimable;
1022 };
1023
1024 /*
1025 * For sequential write required zones, we restart writing at the hardware write
1026 * pointer returned by xfs_validate_blk_zone().
1027 *
1028 * For conventional zones or conventional devices we have to query the rmap to
1029 * find the highest recorded block and set the write pointer to the block after
1030 * that. In case of a power loss this misses blocks where the data I/O has
1031 * completed but not recorded in the rmap yet, and it also rewrites blocks if
1032 * the most recently written ones got deleted again before unmount, but this is
1033 * the best we can do without hardware support.
1034 */
1035 static int
xfs_query_write_pointer(struct xfs_init_zones * iz,struct xfs_rtgroup * rtg,xfs_rgblock_t * write_pointer)1036 xfs_query_write_pointer(
1037 struct xfs_init_zones *iz,
1038 struct xfs_rtgroup *rtg,
1039 xfs_rgblock_t *write_pointer)
1040 {
1041 struct xfs_mount *mp = rtg_mount(rtg);
1042 struct block_device *bdev = mp->m_rtdev_targp->bt_bdev;
1043 sector_t start = xfs_gbno_to_daddr(&rtg->rtg_group, 0);
1044 xfs_rgblock_t highest_rgbno;
1045 struct blk_zone zone = {};
1046 int error;
1047
1048 if (bdev_is_zoned(bdev)) {
1049 error = blkdev_get_zone_info(bdev, start, &zone);
1050 if (error)
1051 return error;
1052 if (zone.start != start) {
1053 xfs_warn(mp, "mismatched zone start: 0x%llx/0x%llx.",
1054 zone.start, start);
1055 return -EFSCORRUPTED;
1056 }
1057
1058 if (!xfs_validate_blk_zone(mp, &zone, rtg_rgno(rtg),
1059 iz->zone_size, iz->zone_capacity,
1060 write_pointer))
1061 return -EFSCORRUPTED;
1062
1063 /*
1064 * Use the hardware write pointer returned by
1065 * xfs_validate_blk_zone for sequential write required zones,
1066 * else fall through to the rmap-based estimation below.
1067 */
1068 if (zone.cond != BLK_ZONE_COND_NOT_WP)
1069 return 0;
1070 }
1071
1072 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
1073 highest_rgbno = xfs_rtrmap_highest_rgbno(rtg);
1074 xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
1075
1076 if (highest_rgbno == NULLRGBLOCK)
1077 *write_pointer = 0;
1078 else
1079 *write_pointer = highest_rgbno + 1;
1080 return 0;
1081 }
1082
1083 static int
xfs_init_zone(struct xfs_init_zones * iz,struct xfs_rtgroup * rtg,xfs_rgblock_t write_pointer)1084 xfs_init_zone(
1085 struct xfs_init_zones *iz,
1086 struct xfs_rtgroup *rtg,
1087 xfs_rgblock_t write_pointer)
1088 {
1089 struct xfs_mount *mp = rtg_mount(rtg);
1090 struct xfs_zone_info *zi = mp->m_zone_info;
1091 uint32_t used = rtg_rmap(rtg)->i_used_blocks;
1092 int error;
1093
1094 if (write_pointer > rtg->rtg_extents) {
1095 xfs_warn(mp, "zone %u has invalid write pointer (0x%x).",
1096 rtg_rgno(rtg), write_pointer);
1097 return -EFSCORRUPTED;
1098 }
1099
1100 if (used > rtg->rtg_extents) {
1101 xfs_warn(mp,
1102 "zone %u has used counter (0x%x) larger than zone capacity (0x%llx).",
1103 rtg_rgno(rtg), used, rtg->rtg_extents);
1104 return -EFSCORRUPTED;
1105 }
1106
1107 if (used > write_pointer) {
1108 xfs_warn(mp,
1109 "zone %u has used counter (0x%x) larger than write pointer (0x%x).",
1110 rtg_rgno(rtg), used, write_pointer);
1111 return -EFSCORRUPTED;
1112 }
1113
1114 if (write_pointer == 0 && used != 0) {
1115 xfs_warn(mp, "empty zone %u has non-zero used counter (0x%x).",
1116 rtg_rgno(rtg), used);
1117 return -EFSCORRUPTED;
1118 }
1119
1120 /*
1121 * If there are no used blocks, but the zone is not in empty state yet
1122 * we lost power before the zoned reset. In that case finish the work
1123 * here.
1124 */
1125 if (write_pointer == rtg_blocks(rtg) && used == 0) {
1126 error = xfs_zone_gc_reset_sync(rtg);
1127 if (error)
1128 return error;
1129 write_pointer = 0;
1130 }
1131
1132 if (write_pointer == 0) {
1133 /* zone is empty */
1134 xfs_zone_mark_free(rtg);
1135 iz->available += rtg_blocks(rtg);
1136 } else if (write_pointer < rtg_blocks(rtg)) {
1137 /* zone is open */
1138 struct xfs_open_zone *oz;
1139
1140 atomic_inc(&rtg_group(rtg)->xg_active_ref);
1141 oz = xfs_init_open_zone(rtg, write_pointer, WRITE_LIFE_NOT_SET,
1142 false);
1143 list_add_tail(&oz->oz_entry, &zi->zi_open_zones);
1144 zi->zi_nr_open_zones++;
1145
1146 iz->available += (rtg_blocks(rtg) - write_pointer);
1147 iz->reclaimable += write_pointer - used;
1148 } else if (used < rtg_blocks(rtg)) {
1149 /* zone fully written, but has freed blocks */
1150 xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used);
1151 iz->reclaimable += (rtg_blocks(rtg) - used);
1152 }
1153
1154 return 0;
1155 }
1156
1157 /*
1158 * Calculate the max open zone limit based on the of number of backing zones
1159 * available.
1160 */
1161 static inline uint32_t
xfs_max_open_zones(struct xfs_mount * mp)1162 xfs_max_open_zones(
1163 struct xfs_mount *mp)
1164 {
1165 unsigned int max_open, max_open_data_zones;
1166
1167 /*
1168 * We need two zones for every open data zone, one in reserve as we
1169 * don't reclaim open zones. One data zone and its spare is included
1170 * in XFS_MIN_ZONES to support at least one user data writer.
1171 */
1172 max_open_data_zones = (mp->m_sb.sb_rgcount - XFS_MIN_ZONES) / 2 + 1;
1173 max_open = max_open_data_zones + XFS_OPEN_GC_ZONES;
1174
1175 /*
1176 * Cap the max open limit to 1/4 of available space. Without this we'd
1177 * run out of easy reclaim targets too quickly and storage devices don't
1178 * handle huge numbers of concurrent write streams overly well.
1179 */
1180 max_open = min(max_open, mp->m_sb.sb_rgcount / 4);
1181
1182 return max(XFS_MIN_OPEN_ZONES, max_open);
1183 }
1184
1185 /*
1186 * Normally we use the open zone limit that the device reports. If there is
1187 * none let the user pick one from the command line.
1188 *
1189 * If the device doesn't report an open zone limit and there is no override,
1190 * allow to hold about a quarter of the zones open. In theory we could allow
1191 * all to be open, but at that point we run into GC deadlocks because we can't
1192 * reclaim open zones.
1193 *
1194 * When used on conventional SSDs a lower open limit is advisable as we'll
1195 * otherwise overwhelm the FTL just as much as a conventional block allocator.
1196 *
1197 * Note: To debug the open zone management code, force max_open to 1 here.
1198 */
1199 static int
xfs_calc_open_zones(struct xfs_mount * mp)1200 xfs_calc_open_zones(
1201 struct xfs_mount *mp)
1202 {
1203 struct block_device *bdev = mp->m_rtdev_targp->bt_bdev;
1204 unsigned int bdev_open_zones = bdev_max_open_zones(bdev);
1205
1206 if (!mp->m_max_open_zones) {
1207 if (bdev_open_zones)
1208 mp->m_max_open_zones = bdev_open_zones;
1209 else
1210 mp->m_max_open_zones = XFS_DEFAULT_MAX_OPEN_ZONES;
1211 }
1212
1213 if (mp->m_max_open_zones < XFS_MIN_OPEN_ZONES) {
1214 xfs_notice(mp, "need at least %u open zones.",
1215 XFS_MIN_OPEN_ZONES);
1216 return -EIO;
1217 }
1218
1219 if (bdev_open_zones && bdev_open_zones < mp->m_max_open_zones) {
1220 mp->m_max_open_zones = bdev_open_zones;
1221 xfs_info(mp, "limiting open zones to %u due to hardware limit.",
1222 bdev_open_zones);
1223 }
1224
1225 if (mp->m_max_open_zones > xfs_max_open_zones(mp)) {
1226 mp->m_max_open_zones = xfs_max_open_zones(mp);
1227 xfs_info(mp,
1228 "limiting open zones to %u due to total zone count (%u)",
1229 mp->m_max_open_zones, mp->m_sb.sb_rgcount);
1230 }
1231
1232 return 0;
1233 }
1234
1235 static unsigned long *
xfs_alloc_bucket_bitmap(struct xfs_mount * mp)1236 xfs_alloc_bucket_bitmap(
1237 struct xfs_mount *mp)
1238 {
1239 return kvmalloc_array(BITS_TO_LONGS(mp->m_sb.sb_rgcount),
1240 sizeof(unsigned long), GFP_KERNEL | __GFP_ZERO);
1241 }
1242
1243 static struct xfs_zone_info *
xfs_alloc_zone_info(struct xfs_mount * mp)1244 xfs_alloc_zone_info(
1245 struct xfs_mount *mp)
1246 {
1247 struct xfs_zone_info *zi;
1248 int i;
1249
1250 zi = kzalloc_obj(*zi);
1251 if (!zi)
1252 return NULL;
1253 INIT_LIST_HEAD(&zi->zi_open_zones);
1254 INIT_LIST_HEAD(&zi->zi_reclaim_reservations);
1255 spin_lock_init(&zi->zi_reset_list_lock);
1256 spin_lock_init(&zi->zi_open_zones_lock);
1257 spin_lock_init(&zi->zi_reservation_lock);
1258 init_waitqueue_head(&zi->zi_zone_wait);
1259 spin_lock_init(&zi->zi_used_buckets_lock);
1260 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) {
1261 zi->zi_used_bucket_bitmap[i] = xfs_alloc_bucket_bitmap(mp);
1262 if (!zi->zi_used_bucket_bitmap[i])
1263 goto out_free_bitmaps;
1264 }
1265 return zi;
1266
1267 out_free_bitmaps:
1268 while (--i >= 0)
1269 kvfree(zi->zi_used_bucket_bitmap[i]);
1270 kfree(zi);
1271 return NULL;
1272 }
1273
1274 static void
xfs_free_zone_info(struct xfs_zone_info * zi)1275 xfs_free_zone_info(
1276 struct xfs_zone_info *zi)
1277 {
1278 int i;
1279
1280 xfs_free_open_zones(zi);
1281 for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++)
1282 kvfree(zi->zi_used_bucket_bitmap[i]);
1283 kfree(zi);
1284 }
1285
1286 static int
xfs_report_zones(struct xfs_mount * mp,struct xfs_init_zones * iz)1287 xfs_report_zones(
1288 struct xfs_mount *mp,
1289 struct xfs_init_zones *iz)
1290 {
1291 struct xfs_rtgroup *rtg = NULL;
1292
1293 while ((rtg = xfs_rtgroup_next(mp, rtg))) {
1294 xfs_rgblock_t write_pointer;
1295 int error;
1296
1297 error = xfs_query_write_pointer(iz, rtg, &write_pointer);
1298 if (!error)
1299 error = xfs_init_zone(iz, rtg, write_pointer);
1300 if (error) {
1301 xfs_rtgroup_rele(rtg);
1302 return error;
1303 }
1304 }
1305
1306 return 0;
1307 }
1308
1309 static inline bool
xfs_zone_is_conv(struct xfs_rtgroup * rtg)1310 xfs_zone_is_conv(
1311 struct xfs_rtgroup *rtg)
1312 {
1313 return !bdev_zone_is_seq(rtg_mount(rtg)->m_rtdev_targp->bt_bdev,
1314 xfs_gbno_to_daddr(rtg_group(rtg), 0));
1315 }
1316
1317 static struct xfs_open_zone *
xfs_find_fullest_conventional_open_zone(struct xfs_mount * mp)1318 xfs_find_fullest_conventional_open_zone(
1319 struct xfs_mount *mp)
1320 {
1321 struct xfs_zone_info *zi = mp->m_zone_info;
1322 struct xfs_open_zone *found = NULL, *oz;
1323
1324 spin_lock(&zi->zi_open_zones_lock);
1325 list_for_each_entry(oz, &zi->zi_open_zones, oz_entry) {
1326 if (!xfs_zone_is_conv(oz->oz_rtg))
1327 continue;
1328 if (!found || oz->oz_allocated > found->oz_allocated)
1329 found = oz;
1330 }
1331 spin_unlock(&zi->zi_open_zones_lock);
1332
1333 return found;
1334 }
1335
1336 /*
1337 * Find the fullest conventional zones and remove them from the open zone pool
1338 * until we are at the open zone limit.
1339 *
1340 * We can end up with spurious "open" zones when the last blocks in a fully
1341 * written zone were invalidate as there is no write pointer for conventional
1342 * zones.
1343 *
1344 * If we are still over the limit when there is no conventional open zone left,
1345 * the user overrode the max open zones limit using the max_open_zones mount
1346 * option we should fail.
1347 */
1348 static int
xfs_finish_spurious_open_zones(struct xfs_mount * mp,struct xfs_init_zones * iz)1349 xfs_finish_spurious_open_zones(
1350 struct xfs_mount *mp,
1351 struct xfs_init_zones *iz)
1352 {
1353 struct xfs_zone_info *zi = mp->m_zone_info;
1354
1355 while (zi->zi_nr_open_zones > mp->m_max_open_zones) {
1356 struct xfs_open_zone *oz;
1357 xfs_filblks_t adjust;
1358
1359 oz = xfs_find_fullest_conventional_open_zone(mp);
1360 if (!oz) {
1361 xfs_err(mp,
1362 "too many open zones for max_open_zones limit (%u/%u)",
1363 zi->zi_nr_open_zones, mp->m_max_open_zones);
1364 return -EINVAL;
1365 }
1366
1367 xfs_rtgroup_lock(oz->oz_rtg, XFS_RTGLOCK_RMAP);
1368 adjust = rtg_blocks(oz->oz_rtg) - oz->oz_written;
1369 trace_xfs_zone_spurious_open(oz, oz->oz_written, adjust);
1370 oz->oz_written = rtg_blocks(oz->oz_rtg);
1371 xfs_open_zone_mark_full(oz);
1372 xfs_rtgroup_unlock(oz->oz_rtg, XFS_RTGLOCK_RMAP);
1373 iz->available -= adjust;
1374 iz->reclaimable += adjust;
1375 }
1376
1377 return 0;
1378 }
1379
1380 int
xfs_mount_zones(struct xfs_mount * mp)1381 xfs_mount_zones(
1382 struct xfs_mount *mp)
1383 {
1384 struct xfs_init_zones iz = {
1385 .zone_capacity = mp->m_groups[XG_TYPE_RTG].blocks,
1386 .zone_size = xfs_rtgroup_raw_size(mp),
1387 };
1388 int error;
1389
1390 if (!mp->m_rtdev_targp) {
1391 xfs_notice(mp, "RT device missing.");
1392 return -EINVAL;
1393 }
1394
1395 if (!xfs_has_rtgroups(mp) || !xfs_has_rmapbt(mp)) {
1396 xfs_notice(mp, "invalid flag combination.");
1397 return -EFSCORRUPTED;
1398 }
1399 if (mp->m_sb.sb_rextsize != 1) {
1400 xfs_notice(mp, "zoned file systems do not support rextsize.");
1401 return -EFSCORRUPTED;
1402 }
1403 if (mp->m_sb.sb_rgcount < XFS_MIN_ZONES) {
1404 xfs_notice(mp,
1405 "zoned file systems need to have at least %u zones.", XFS_MIN_ZONES);
1406 return -EFSCORRUPTED;
1407 }
1408
1409 error = xfs_calc_open_zones(mp);
1410 if (error)
1411 return error;
1412
1413 mp->m_zone_info = xfs_alloc_zone_info(mp);
1414 if (!mp->m_zone_info)
1415 return -ENOMEM;
1416
1417 error = xfs_report_zones(mp, &iz);
1418 if (error)
1419 goto out_free_zone_info;
1420
1421 error = xfs_finish_spurious_open_zones(mp, &iz);
1422 if (error)
1423 goto out_free_zone_info;
1424
1425 xfs_set_freecounter(mp, XC_FREE_RTAVAILABLE, iz.available);
1426 xfs_set_freecounter(mp, XC_FREE_RTEXTENTS,
1427 iz.available + iz.reclaimable);
1428
1429 /*
1430 * The writeback code switches between inodes regularly to provide
1431 * fairness. The default lower bound is 4MiB, but for zoned file
1432 * systems we want to increase that both to reduce seeks, but also more
1433 * importantly so that workloads that writes files in a multiple of the
1434 * zone size do not get fragmented and require garbage collection when
1435 * they shouldn't. Increase is to the zone size capped by the max
1436 * extent len.
1437 *
1438 * Note that because s_min_writeback_pages is a superblock field, this
1439 * value also get applied to non-zoned files on the data device if
1440 * there are any. On typical zoned setup all data is on the RT device
1441 * because using the more efficient sequential write required zones
1442 * is the reason for using the zone allocator, and either the RT device
1443 * and the (meta)data device are on the same block device, or the
1444 * (meta)data device is on a fast SSD while the data on the RT device
1445 * is on a SMR HDD. In any combination of the above cases enforcing
1446 * the higher min_writeback_pages for non-RT inodes is either a noop
1447 * or beneficial.
1448 */
1449 mp->m_super->s_min_writeback_pages =
1450 XFS_FSB_TO_B(mp, min(iz.zone_capacity, XFS_MAX_BMBT_EXTLEN)) >>
1451 PAGE_SHIFT;
1452
1453 /*
1454 * The user may configure GC to free up a percentage of unused blocks.
1455 * By default this is 0. GC will always trigger at the minimum level
1456 * for keeping max_open_zones available for data placement.
1457 */
1458 mp->m_zonegc_low_space = 0;
1459
1460 error = xfs_zone_gc_mount(mp);
1461 if (error)
1462 goto out_free_zone_info;
1463
1464 error = xfs_zoned_sysfs_init(mp);
1465 if (error)
1466 goto out_zone_gc_unmount;
1467
1468 xfs_info(mp, "%u zones of %u blocks (%u max open zones)",
1469 mp->m_sb.sb_rgcount, iz.zone_capacity, mp->m_max_open_zones);
1470 trace_xfs_zones_mount(mp);
1471 return 0;
1472
1473 out_zone_gc_unmount:
1474 xfs_zone_gc_unmount(mp);
1475 out_free_zone_info:
1476 xfs_free_zone_info(mp->m_zone_info);
1477 return error;
1478 }
1479
1480 void
xfs_unmount_zones(struct xfs_mount * mp)1481 xfs_unmount_zones(
1482 struct xfs_mount *mp)
1483 {
1484 xfs_zoned_sysfs_del(mp);
1485 xfs_zone_gc_unmount(mp);
1486 xfs_free_zone_info(mp->m_zone_info);
1487 }
1488