1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2022-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_bit.h"
12 #include "xfs_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_btree.h"
15 #include "xfs_alloc_btree.h"
16 #include "xfs_rmap_btree.h"
17 #include "xfs_alloc.h"
18 #include "xfs_ialloc.h"
19 #include "xfs_rmap.h"
20 #include "xfs_ag.h"
21 #include "xfs_ag_resv.h"
22 #include "xfs_health.h"
23 #include "xfs_error.h"
24 #include "xfs_bmap.h"
25 #include "xfs_defer.h"
26 #include "xfs_log_format.h"
27 #include "xfs_trans.h"
28 #include "xfs_trace.h"
29 #include "xfs_inode.h"
30 #include "xfs_icache.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_rtgroup.h"
33 #include "xfs_rtbitmap.h"
34 #include "xfs_metafile.h"
35 #include "xfs_metadir.h"
36 #include "xfs_rtrmap_btree.h"
37 #include "xfs_rtrefcount_btree.h"
38
39 /* Find the first usable fsblock in this rtgroup. */
40 static inline uint32_t
xfs_rtgroup_min_block(struct xfs_mount * mp,xfs_rgnumber_t rgno)41 xfs_rtgroup_min_block(
42 struct xfs_mount *mp,
43 xfs_rgnumber_t rgno)
44 {
45 if (xfs_has_rtsb(mp) && rgno == 0)
46 return mp->m_sb.sb_rextsize;
47
48 return 0;
49 }
50
51 /* Precompute this group's geometry */
52 void
xfs_rtgroup_calc_geometry(struct xfs_mount * mp,struct xfs_rtgroup * rtg,xfs_rgnumber_t rgno,xfs_rgnumber_t rgcount,xfs_rtbxlen_t rextents)53 xfs_rtgroup_calc_geometry(
54 struct xfs_mount *mp,
55 struct xfs_rtgroup *rtg,
56 xfs_rgnumber_t rgno,
57 xfs_rgnumber_t rgcount,
58 xfs_rtbxlen_t rextents)
59 {
60 rtg->rtg_extents = __xfs_rtgroup_extents(mp, rgno, rgcount, rextents);
61 rtg_group(rtg)->xg_block_count = rtg->rtg_extents * mp->m_sb.sb_rextsize;
62 rtg_group(rtg)->xg_min_gbno = xfs_rtgroup_min_block(mp, rgno);
63 }
64
65 int
xfs_rtgroup_alloc(struct xfs_mount * mp,xfs_rgnumber_t rgno,xfs_rgnumber_t rgcount,xfs_rtbxlen_t rextents)66 xfs_rtgroup_alloc(
67 struct xfs_mount *mp,
68 xfs_rgnumber_t rgno,
69 xfs_rgnumber_t rgcount,
70 xfs_rtbxlen_t rextents)
71 {
72 struct xfs_rtgroup *rtg;
73 int error;
74
75 rtg = kzalloc(sizeof(struct xfs_rtgroup), GFP_KERNEL);
76 if (!rtg)
77 return -ENOMEM;
78
79 xfs_rtgroup_calc_geometry(mp, rtg, rgno, rgcount, rextents);
80
81 error = xfs_group_insert(mp, rtg_group(rtg), rgno, XG_TYPE_RTG);
82 if (error)
83 goto out_free_rtg;
84 return 0;
85
86 out_free_rtg:
87 kfree(rtg);
88 return error;
89 }
90
91 void
xfs_rtgroup_free(struct xfs_mount * mp,xfs_rgnumber_t rgno)92 xfs_rtgroup_free(
93 struct xfs_mount *mp,
94 xfs_rgnumber_t rgno)
95 {
96 xfs_group_free(mp, rgno, XG_TYPE_RTG, NULL);
97 }
98
99 /* Free a range of incore rtgroup objects. */
100 void
xfs_free_rtgroups(struct xfs_mount * mp,xfs_rgnumber_t first_rgno,xfs_rgnumber_t end_rgno)101 xfs_free_rtgroups(
102 struct xfs_mount *mp,
103 xfs_rgnumber_t first_rgno,
104 xfs_rgnumber_t end_rgno)
105 {
106 xfs_rgnumber_t rgno;
107
108 for (rgno = first_rgno; rgno < end_rgno; rgno++)
109 xfs_rtgroup_free(mp, rgno);
110 }
111
112 /* Initialize some range of incore rtgroup objects. */
113 int
xfs_initialize_rtgroups(struct xfs_mount * mp,xfs_rgnumber_t first_rgno,xfs_rgnumber_t end_rgno,xfs_rtbxlen_t rextents)114 xfs_initialize_rtgroups(
115 struct xfs_mount *mp,
116 xfs_rgnumber_t first_rgno,
117 xfs_rgnumber_t end_rgno,
118 xfs_rtbxlen_t rextents)
119 {
120 xfs_rgnumber_t index;
121 int error;
122
123 if (first_rgno >= end_rgno)
124 return 0;
125
126 for (index = first_rgno; index < end_rgno; index++) {
127 error = xfs_rtgroup_alloc(mp, index, end_rgno, rextents);
128 if (error)
129 goto out_unwind_new_rtgs;
130 }
131
132 return 0;
133
134 out_unwind_new_rtgs:
135 xfs_free_rtgroups(mp, first_rgno, index);
136 return error;
137 }
138
139 /* Compute the number of rt extents in this realtime group. */
140 xfs_rtxnum_t
__xfs_rtgroup_extents(struct xfs_mount * mp,xfs_rgnumber_t rgno,xfs_rgnumber_t rgcount,xfs_rtbxlen_t rextents)141 __xfs_rtgroup_extents(
142 struct xfs_mount *mp,
143 xfs_rgnumber_t rgno,
144 xfs_rgnumber_t rgcount,
145 xfs_rtbxlen_t rextents)
146 {
147 ASSERT(rgno < rgcount);
148 if (rgno == rgcount - 1)
149 return rextents - ((xfs_rtxnum_t)rgno * mp->m_sb.sb_rgextents);
150
151 ASSERT(xfs_has_rtgroups(mp));
152 return mp->m_sb.sb_rgextents;
153 }
154
155 xfs_rtxnum_t
xfs_rtgroup_extents(struct xfs_mount * mp,xfs_rgnumber_t rgno)156 xfs_rtgroup_extents(
157 struct xfs_mount *mp,
158 xfs_rgnumber_t rgno)
159 {
160 return __xfs_rtgroup_extents(mp, rgno, mp->m_sb.sb_rgcount,
161 mp->m_sb.sb_rextents);
162 }
163
164 /*
165 * Update the rt extent count of the previous tail rtgroup if it changed during
166 * recovery (i.e. recovery of a growfs).
167 */
168 int
xfs_update_last_rtgroup_size(struct xfs_mount * mp,xfs_rgnumber_t prev_rgcount)169 xfs_update_last_rtgroup_size(
170 struct xfs_mount *mp,
171 xfs_rgnumber_t prev_rgcount)
172 {
173 struct xfs_rtgroup *rtg;
174
175 ASSERT(prev_rgcount > 0);
176
177 rtg = xfs_rtgroup_grab(mp, prev_rgcount - 1);
178 if (!rtg)
179 return -EFSCORRUPTED;
180 rtg->rtg_extents = __xfs_rtgroup_extents(mp, prev_rgcount - 1,
181 mp->m_sb.sb_rgcount, mp->m_sb.sb_rextents);
182 rtg_group(rtg)->xg_block_count = rtg->rtg_extents * mp->m_sb.sb_rextsize;
183 xfs_rtgroup_rele(rtg);
184 return 0;
185 }
186
187 /* Lock metadata inodes associated with this rt group. */
188 void
xfs_rtgroup_lock(struct xfs_rtgroup * rtg,unsigned int rtglock_flags)189 xfs_rtgroup_lock(
190 struct xfs_rtgroup *rtg,
191 unsigned int rtglock_flags)
192 {
193 ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
194 ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) ||
195 !(rtglock_flags & XFS_RTGLOCK_BITMAP));
196
197 if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
198 /*
199 * Lock both realtime free space metadata inodes for a freespace
200 * update.
201 */
202 xfs_ilock(rtg_bitmap(rtg), XFS_ILOCK_EXCL);
203 xfs_ilock(rtg_summary(rtg), XFS_ILOCK_EXCL);
204 } else if (rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) {
205 xfs_ilock(rtg_bitmap(rtg), XFS_ILOCK_SHARED);
206 }
207
208 if ((rtglock_flags & XFS_RTGLOCK_RMAP) && rtg_rmap(rtg))
209 xfs_ilock(rtg_rmap(rtg), XFS_ILOCK_EXCL);
210
211 if ((rtglock_flags & XFS_RTGLOCK_REFCOUNT) && rtg_refcount(rtg))
212 xfs_ilock(rtg_refcount(rtg), XFS_ILOCK_EXCL);
213 }
214
215 /* Unlock metadata inodes associated with this rt group. */
216 void
xfs_rtgroup_unlock(struct xfs_rtgroup * rtg,unsigned int rtglock_flags)217 xfs_rtgroup_unlock(
218 struct xfs_rtgroup *rtg,
219 unsigned int rtglock_flags)
220 {
221 ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
222 ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) ||
223 !(rtglock_flags & XFS_RTGLOCK_BITMAP));
224
225 if ((rtglock_flags & XFS_RTGLOCK_REFCOUNT) && rtg_refcount(rtg))
226 xfs_iunlock(rtg_refcount(rtg), XFS_ILOCK_EXCL);
227
228 if ((rtglock_flags & XFS_RTGLOCK_RMAP) && rtg_rmap(rtg))
229 xfs_iunlock(rtg_rmap(rtg), XFS_ILOCK_EXCL);
230
231 if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
232 xfs_iunlock(rtg_summary(rtg), XFS_ILOCK_EXCL);
233 xfs_iunlock(rtg_bitmap(rtg), XFS_ILOCK_EXCL);
234 } else if (rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) {
235 xfs_iunlock(rtg_bitmap(rtg), XFS_ILOCK_SHARED);
236 }
237 }
238
239 /*
240 * Join realtime group metadata inodes to the transaction. The ILOCKs will be
241 * released on transaction commit.
242 */
243 void
xfs_rtgroup_trans_join(struct xfs_trans * tp,struct xfs_rtgroup * rtg,unsigned int rtglock_flags)244 xfs_rtgroup_trans_join(
245 struct xfs_trans *tp,
246 struct xfs_rtgroup *rtg,
247 unsigned int rtglock_flags)
248 {
249 ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
250 ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED));
251
252 if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
253 xfs_trans_ijoin(tp, rtg_bitmap(rtg), XFS_ILOCK_EXCL);
254 xfs_trans_ijoin(tp, rtg_summary(rtg), XFS_ILOCK_EXCL);
255 }
256
257 if ((rtglock_flags & XFS_RTGLOCK_RMAP) && rtg_rmap(rtg))
258 xfs_trans_ijoin(tp, rtg_rmap(rtg), XFS_ILOCK_EXCL);
259
260 if ((rtglock_flags & XFS_RTGLOCK_REFCOUNT) && rtg_refcount(rtg))
261 xfs_trans_ijoin(tp, rtg_refcount(rtg), XFS_ILOCK_EXCL);
262 }
263
264 /* Retrieve rt group geometry. */
265 int
xfs_rtgroup_get_geometry(struct xfs_rtgroup * rtg,struct xfs_rtgroup_geometry * rgeo)266 xfs_rtgroup_get_geometry(
267 struct xfs_rtgroup *rtg,
268 struct xfs_rtgroup_geometry *rgeo)
269 {
270 /* Fill out form. */
271 memset(rgeo, 0, sizeof(*rgeo));
272 rgeo->rg_number = rtg_rgno(rtg);
273 rgeo->rg_length = rtg_group(rtg)->xg_block_count;
274 xfs_rtgroup_geom_health(rtg, rgeo);
275 return 0;
276 }
277
278 #ifdef CONFIG_PROVE_LOCKING
279 static struct lock_class_key xfs_rtginode_lock_class;
280
281 static int
xfs_rtginode_ilock_cmp_fn(const struct lockdep_map * m1,const struct lockdep_map * m2)282 xfs_rtginode_ilock_cmp_fn(
283 const struct lockdep_map *m1,
284 const struct lockdep_map *m2)
285 {
286 const struct xfs_inode *ip1 =
287 container_of(m1, struct xfs_inode, i_lock.dep_map);
288 const struct xfs_inode *ip2 =
289 container_of(m2, struct xfs_inode, i_lock.dep_map);
290
291 if (ip1->i_projid < ip2->i_projid)
292 return -1;
293 if (ip1->i_projid > ip2->i_projid)
294 return 1;
295 return 0;
296 }
297
298 static inline void
xfs_rtginode_ilock_print_fn(const struct lockdep_map * m)299 xfs_rtginode_ilock_print_fn(
300 const struct lockdep_map *m)
301 {
302 const struct xfs_inode *ip =
303 container_of(m, struct xfs_inode, i_lock.dep_map);
304
305 printk(KERN_CONT " rgno=%u metatype=%s", ip->i_projid,
306 xfs_metafile_type_str(ip->i_metatype));
307 }
308
309 /*
310 * Most of the time each of the RTG inode locks are only taken one at a time.
311 * But when committing deferred ops, more than one of a kind can be taken.
312 * However, deferred rt ops will be committed in rgno order so there is no
313 * potential for deadlocks. The code here is needed to tell lockdep about this
314 * order.
315 */
316 static inline void
xfs_rtginode_lockdep_setup(struct xfs_inode * ip,xfs_rgnumber_t rgno,enum xfs_rtg_inodes type)317 xfs_rtginode_lockdep_setup(
318 struct xfs_inode *ip,
319 xfs_rgnumber_t rgno,
320 enum xfs_rtg_inodes type)
321 {
322 lockdep_set_class_and_subclass(&ip->i_lock, &xfs_rtginode_lock_class,
323 type);
324 lock_set_cmp_fn(&ip->i_lock, xfs_rtginode_ilock_cmp_fn,
325 xfs_rtginode_ilock_print_fn);
326 }
327 #else
328 #define xfs_rtginode_lockdep_setup(ip, rgno, type) do { } while (0)
329 #endif /* CONFIG_PROVE_LOCKING */
330
331 struct xfs_rtginode_ops {
332 const char *name; /* short name */
333
334 enum xfs_metafile_type metafile_type;
335
336 unsigned int sick; /* rtgroup sickness flag */
337
338 unsigned int fmt_mask; /* all valid data fork formats */
339
340 /* Does the fs have this feature? */
341 bool (*enabled)(const struct xfs_mount *mp);
342
343 /* Create this rtgroup metadata inode and initialize it. */
344 int (*create)(struct xfs_rtgroup *rtg,
345 struct xfs_inode *ip,
346 struct xfs_trans *tp,
347 bool init);
348 };
349
350 static const struct xfs_rtginode_ops xfs_rtginode_ops[XFS_RTGI_MAX] = {
351 [XFS_RTGI_BITMAP] = {
352 .name = "bitmap",
353 .metafile_type = XFS_METAFILE_RTBITMAP,
354 .sick = XFS_SICK_RG_BITMAP,
355 .fmt_mask = (1U << XFS_DINODE_FMT_EXTENTS) |
356 (1U << XFS_DINODE_FMT_BTREE),
357 .create = xfs_rtbitmap_create,
358 },
359 [XFS_RTGI_SUMMARY] = {
360 .name = "summary",
361 .metafile_type = XFS_METAFILE_RTSUMMARY,
362 .sick = XFS_SICK_RG_SUMMARY,
363 .fmt_mask = (1U << XFS_DINODE_FMT_EXTENTS) |
364 (1U << XFS_DINODE_FMT_BTREE),
365 .create = xfs_rtsummary_create,
366 },
367 [XFS_RTGI_RMAP] = {
368 .name = "rmap",
369 .metafile_type = XFS_METAFILE_RTRMAP,
370 .sick = XFS_SICK_RG_RMAPBT,
371 .fmt_mask = 1U << XFS_DINODE_FMT_META_BTREE,
372 /*
373 * growfs must create the rtrmap inodes before adding a
374 * realtime volume to the filesystem, so we cannot use the
375 * rtrmapbt predicate here.
376 */
377 .enabled = xfs_has_rmapbt,
378 .create = xfs_rtrmapbt_create,
379 },
380 [XFS_RTGI_REFCOUNT] = {
381 .name = "refcount",
382 .metafile_type = XFS_METAFILE_RTREFCOUNT,
383 .sick = XFS_SICK_RG_REFCNTBT,
384 .fmt_mask = 1U << XFS_DINODE_FMT_META_BTREE,
385 /* same comment about growfs and rmap inodes applies here */
386 .enabled = xfs_has_reflink,
387 .create = xfs_rtrefcountbt_create,
388 },
389 };
390
391 /* Return the shortname of this rtgroup inode. */
392 const char *
xfs_rtginode_name(enum xfs_rtg_inodes type)393 xfs_rtginode_name(
394 enum xfs_rtg_inodes type)
395 {
396 return xfs_rtginode_ops[type].name;
397 }
398
399 /* Return the metafile type of this rtgroup inode. */
400 enum xfs_metafile_type
xfs_rtginode_metafile_type(enum xfs_rtg_inodes type)401 xfs_rtginode_metafile_type(
402 enum xfs_rtg_inodes type)
403 {
404 return xfs_rtginode_ops[type].metafile_type;
405 }
406
407 /* Should this rtgroup inode be present? */
408 bool
xfs_rtginode_enabled(struct xfs_rtgroup * rtg,enum xfs_rtg_inodes type)409 xfs_rtginode_enabled(
410 struct xfs_rtgroup *rtg,
411 enum xfs_rtg_inodes type)
412 {
413 const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
414
415 if (!ops->enabled)
416 return true;
417 return ops->enabled(rtg_mount(rtg));
418 }
419
420 /* Mark an rtgroup inode sick */
421 void
xfs_rtginode_mark_sick(struct xfs_rtgroup * rtg,enum xfs_rtg_inodes type)422 xfs_rtginode_mark_sick(
423 struct xfs_rtgroup *rtg,
424 enum xfs_rtg_inodes type)
425 {
426 const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
427
428 xfs_group_mark_sick(rtg_group(rtg), ops->sick);
429 }
430
431 /* Load and existing rtgroup inode into the rtgroup structure. */
432 int
xfs_rtginode_load(struct xfs_rtgroup * rtg,enum xfs_rtg_inodes type,struct xfs_trans * tp)433 xfs_rtginode_load(
434 struct xfs_rtgroup *rtg,
435 enum xfs_rtg_inodes type,
436 struct xfs_trans *tp)
437 {
438 struct xfs_mount *mp = tp->t_mountp;
439 struct xfs_inode *ip;
440 const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
441 int error;
442
443 if (!xfs_rtginode_enabled(rtg, type))
444 return 0;
445
446 if (!xfs_has_rtgroups(mp)) {
447 xfs_ino_t ino;
448
449 switch (type) {
450 case XFS_RTGI_BITMAP:
451 ino = mp->m_sb.sb_rbmino;
452 break;
453 case XFS_RTGI_SUMMARY:
454 ino = mp->m_sb.sb_rsumino;
455 break;
456 default:
457 /* None of the other types exist on !rtgroups */
458 return 0;
459 }
460
461 error = xfs_trans_metafile_iget(tp, ino, ops->metafile_type,
462 &ip);
463 } else {
464 const char *path;
465
466 if (!mp->m_rtdirip) {
467 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
468 return -EFSCORRUPTED;
469 }
470
471 path = xfs_rtginode_path(rtg_rgno(rtg), type);
472 if (!path)
473 return -ENOMEM;
474 error = xfs_metadir_load(tp, mp->m_rtdirip, path,
475 ops->metafile_type, &ip);
476 kfree(path);
477 }
478
479 if (error) {
480 if (xfs_metadata_is_sick(error))
481 xfs_rtginode_mark_sick(rtg, type);
482 return error;
483 }
484
485 if (XFS_IS_CORRUPT(mp, !((1U << ip->i_df.if_format) & ops->fmt_mask))) {
486 xfs_irele(ip);
487 xfs_rtginode_mark_sick(rtg, type);
488 return -EFSCORRUPTED;
489 }
490
491 if (XFS_IS_CORRUPT(mp, ip->i_projid != rtg_rgno(rtg))) {
492 xfs_irele(ip);
493 xfs_rtginode_mark_sick(rtg, type);
494 return -EFSCORRUPTED;
495 }
496
497 xfs_rtginode_lockdep_setup(ip, rtg_rgno(rtg), type);
498 rtg->rtg_inodes[type] = ip;
499 return 0;
500 }
501
502 /* Release an rtgroup metadata inode. */
503 void
xfs_rtginode_irele(struct xfs_inode ** ipp)504 xfs_rtginode_irele(
505 struct xfs_inode **ipp)
506 {
507 if (*ipp)
508 xfs_irele(*ipp);
509 *ipp = NULL;
510 }
511
512 /* Add a metadata inode for a realtime rmap btree. */
513 int
xfs_rtginode_create(struct xfs_rtgroup * rtg,enum xfs_rtg_inodes type,bool init)514 xfs_rtginode_create(
515 struct xfs_rtgroup *rtg,
516 enum xfs_rtg_inodes type,
517 bool init)
518 {
519 const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
520 struct xfs_mount *mp = rtg_mount(rtg);
521 struct xfs_metadir_update upd = {
522 .dp = mp->m_rtdirip,
523 .metafile_type = ops->metafile_type,
524 };
525 int error;
526
527 if (!xfs_rtginode_enabled(rtg, type))
528 return 0;
529
530 if (!mp->m_rtdirip) {
531 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
532 return -EFSCORRUPTED;
533 }
534
535 upd.path = xfs_rtginode_path(rtg_rgno(rtg), type);
536 if (!upd.path)
537 return -ENOMEM;
538
539 error = xfs_metadir_start_create(&upd);
540 if (error)
541 goto out_path;
542
543 error = xfs_metadir_create(&upd, S_IFREG);
544 if (error)
545 goto out_cancel;
546
547 xfs_rtginode_lockdep_setup(upd.ip, rtg_rgno(rtg), type);
548
549 upd.ip->i_projid = rtg_rgno(rtg);
550 error = ops->create(rtg, upd.ip, upd.tp, init);
551 if (error)
552 goto out_cancel;
553
554 error = xfs_metadir_commit(&upd);
555 if (error)
556 goto out_path;
557
558 kfree(upd.path);
559 xfs_finish_inode_setup(upd.ip);
560 rtg->rtg_inodes[type] = upd.ip;
561 return 0;
562
563 out_cancel:
564 xfs_metadir_cancel(&upd, error);
565 /* Have to finish setting up the inode to ensure it's deleted. */
566 if (upd.ip) {
567 xfs_finish_inode_setup(upd.ip);
568 xfs_irele(upd.ip);
569 }
570 out_path:
571 kfree(upd.path);
572 return error;
573 }
574
575 /* Create the parent directory for all rtgroup inodes and load it. */
576 int
xfs_rtginode_mkdir_parent(struct xfs_mount * mp)577 xfs_rtginode_mkdir_parent(
578 struct xfs_mount *mp)
579 {
580 if (!mp->m_metadirip) {
581 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
582 return -EFSCORRUPTED;
583 }
584
585 return xfs_metadir_mkdir(mp->m_metadirip, "rtgroups", &mp->m_rtdirip);
586 }
587
588 /* Load the parent directory of all rtgroup inodes. */
589 int
xfs_rtginode_load_parent(struct xfs_trans * tp)590 xfs_rtginode_load_parent(
591 struct xfs_trans *tp)
592 {
593 struct xfs_mount *mp = tp->t_mountp;
594
595 if (!mp->m_metadirip) {
596 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
597 return -EFSCORRUPTED;
598 }
599
600 return xfs_metadir_load(tp, mp->m_metadirip, "rtgroups",
601 XFS_METAFILE_DIR, &mp->m_rtdirip);
602 }
603
604 /* Check superblock fields for a read or a write. */
605 static xfs_failaddr_t
xfs_rtsb_verify_common(struct xfs_buf * bp)606 xfs_rtsb_verify_common(
607 struct xfs_buf *bp)
608 {
609 struct xfs_rtsb *rsb = bp->b_addr;
610
611 if (!xfs_verify_magic(bp, rsb->rsb_magicnum))
612 return __this_address;
613 if (rsb->rsb_pad)
614 return __this_address;
615
616 /* Everything to the end of the fs block must be zero */
617 if (memchr_inv(rsb + 1, 0, BBTOB(bp->b_length) - sizeof(*rsb)))
618 return __this_address;
619
620 return NULL;
621 }
622
623 /* Check superblock fields for a read or revalidation. */
624 static inline xfs_failaddr_t
xfs_rtsb_verify_all(struct xfs_buf * bp)625 xfs_rtsb_verify_all(
626 struct xfs_buf *bp)
627 {
628 struct xfs_rtsb *rsb = bp->b_addr;
629 struct xfs_mount *mp = bp->b_mount;
630 xfs_failaddr_t fa;
631
632 fa = xfs_rtsb_verify_common(bp);
633 if (fa)
634 return fa;
635
636 if (memcmp(&rsb->rsb_fname, &mp->m_sb.sb_fname, XFSLABEL_MAX))
637 return __this_address;
638 if (!uuid_equal(&rsb->rsb_uuid, &mp->m_sb.sb_uuid))
639 return __this_address;
640 if (!uuid_equal(&rsb->rsb_meta_uuid, &mp->m_sb.sb_meta_uuid))
641 return __this_address;
642
643 return NULL;
644 }
645
646 static void
xfs_rtsb_read_verify(struct xfs_buf * bp)647 xfs_rtsb_read_verify(
648 struct xfs_buf *bp)
649 {
650 xfs_failaddr_t fa;
651
652 if (!xfs_buf_verify_cksum(bp, XFS_RTSB_CRC_OFF)) {
653 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
654 return;
655 }
656
657 fa = xfs_rtsb_verify_all(bp);
658 if (fa)
659 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
660 }
661
662 static void
xfs_rtsb_write_verify(struct xfs_buf * bp)663 xfs_rtsb_write_verify(
664 struct xfs_buf *bp)
665 {
666 xfs_failaddr_t fa;
667
668 fa = xfs_rtsb_verify_common(bp);
669 if (fa) {
670 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
671 return;
672 }
673
674 xfs_buf_update_cksum(bp, XFS_RTSB_CRC_OFF);
675 }
676
677 const struct xfs_buf_ops xfs_rtsb_buf_ops = {
678 .name = "xfs_rtsb",
679 .magic = { 0, cpu_to_be32(XFS_RTSB_MAGIC) },
680 .verify_read = xfs_rtsb_read_verify,
681 .verify_write = xfs_rtsb_write_verify,
682 .verify_struct = xfs_rtsb_verify_all,
683 };
684
685 /* Update a realtime superblock from the primary fs super */
686 void
xfs_update_rtsb(struct xfs_buf * rtsb_bp,const struct xfs_buf * sb_bp)687 xfs_update_rtsb(
688 struct xfs_buf *rtsb_bp,
689 const struct xfs_buf *sb_bp)
690 {
691 const struct xfs_dsb *dsb = sb_bp->b_addr;
692 struct xfs_rtsb *rsb = rtsb_bp->b_addr;
693 const uuid_t *meta_uuid;
694
695 rsb->rsb_magicnum = cpu_to_be32(XFS_RTSB_MAGIC);
696
697 rsb->rsb_pad = 0;
698 memcpy(&rsb->rsb_fname, &dsb->sb_fname, XFSLABEL_MAX);
699
700 memcpy(&rsb->rsb_uuid, &dsb->sb_uuid, sizeof(rsb->rsb_uuid));
701
702 /*
703 * The metadata uuid is the fs uuid if the metauuid feature is not
704 * enabled.
705 */
706 if (dsb->sb_features_incompat &
707 cpu_to_be32(XFS_SB_FEAT_INCOMPAT_META_UUID))
708 meta_uuid = &dsb->sb_meta_uuid;
709 else
710 meta_uuid = &dsb->sb_uuid;
711 memcpy(&rsb->rsb_meta_uuid, meta_uuid, sizeof(rsb->rsb_meta_uuid));
712 }
713
714 /*
715 * Update the realtime superblock from a filesystem superblock and log it to
716 * the given transaction.
717 */
718 struct xfs_buf *
xfs_log_rtsb(struct xfs_trans * tp,const struct xfs_buf * sb_bp)719 xfs_log_rtsb(
720 struct xfs_trans *tp,
721 const struct xfs_buf *sb_bp)
722 {
723 struct xfs_buf *rtsb_bp;
724
725 if (!xfs_has_rtsb(tp->t_mountp))
726 return NULL;
727
728 rtsb_bp = xfs_trans_getrtsb(tp);
729 if (!rtsb_bp) {
730 /*
731 * It's possible for the rtgroups feature to be enabled but
732 * there is no incore rt superblock buffer if the rt geometry
733 * was specified at mkfs time but the rt section has not yet
734 * been attached. In this case, rblocks must be zero.
735 */
736 ASSERT(tp->t_mountp->m_sb.sb_rblocks == 0);
737 return NULL;
738 }
739
740 xfs_update_rtsb(rtsb_bp, sb_bp);
741 xfs_trans_ordered_buf(tp, rtsb_bp);
742 return rtsb_bp;
743 }
744