1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2023 Oracle. All Rights Reserved. 4 * Author: Darrick J. Wong <djwong@kernel.org> 5 */ 6 #include "xfs_platform.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_mount.h" 12 #include "xfs_sysfs.h" 13 #include "xfs_btree.h" 14 #include "xfs_super.h" 15 #include "scrub/scrub.h" 16 #include "scrub/stats.h" 17 #include "scrub/trace.h" 18 19 struct xchk_scrub_stats { 20 /* all 32-bit counters here */ 21 22 /* checking stats */ 23 uint32_t invocations; 24 uint32_t clean; 25 uint32_t corrupt; 26 uint32_t preen; 27 uint32_t xfail; 28 uint32_t xcorrupt; 29 uint32_t incomplete; 30 uint32_t warning; 31 uint32_t retries; 32 uint32_t runtime_errors; 33 34 /* repair stats */ 35 uint32_t repair_invocations; 36 uint32_t repair_success; 37 38 /* all 64-bit items here */ 39 40 /* runtimes */ 41 uint64_t checktime_us; 42 uint64_t repairtime_us; 43 44 /* non-counter state must go at the end for clearall */ 45 spinlock_t css_lock; 46 }; 47 48 struct xchk_stats { 49 struct dentry *cs_debugfs; 50 struct xchk_scrub_stats cs_stats[XFS_SCRUB_TYPE_NR]; 51 }; 52 53 54 static struct xchk_stats global_stats; 55 56 static const char *name_map[XFS_SCRUB_TYPE_NR] = { 57 [XFS_SCRUB_TYPE_SB] = "sb", 58 [XFS_SCRUB_TYPE_AGF] = "agf", 59 [XFS_SCRUB_TYPE_AGFL] = "agfl", 60 [XFS_SCRUB_TYPE_AGI] = "agi", 61 [XFS_SCRUB_TYPE_BNOBT] = "bnobt", 62 [XFS_SCRUB_TYPE_CNTBT] = "cntbt", 63 [XFS_SCRUB_TYPE_INOBT] = "inobt", 64 [XFS_SCRUB_TYPE_FINOBT] = "finobt", 65 [XFS_SCRUB_TYPE_RMAPBT] = "rmapbt", 66 [XFS_SCRUB_TYPE_REFCNTBT] = "refcountbt", 67 [XFS_SCRUB_TYPE_INODE] = "inode", 68 [XFS_SCRUB_TYPE_BMBTD] = "bmapbtd", 69 [XFS_SCRUB_TYPE_BMBTA] = "bmapbta", 70 [XFS_SCRUB_TYPE_BMBTC] = "bmapbtc", 71 [XFS_SCRUB_TYPE_DIR] = "directory", 72 [XFS_SCRUB_TYPE_XATTR] = "xattr", 73 [XFS_SCRUB_TYPE_SYMLINK] = "symlink", 74 [XFS_SCRUB_TYPE_PARENT] = "parent", 75 [XFS_SCRUB_TYPE_RTBITMAP] = "rtbitmap", 76 [XFS_SCRUB_TYPE_RTSUM] = "rtsummary", 77 [XFS_SCRUB_TYPE_UQUOTA] = "usrquota", 78 [XFS_SCRUB_TYPE_GQUOTA] = "grpquota", 79 [XFS_SCRUB_TYPE_PQUOTA] = "prjquota", 80 [XFS_SCRUB_TYPE_FSCOUNTERS] = "fscounters", 81 [XFS_SCRUB_TYPE_QUOTACHECK] = "quotacheck", 82 [XFS_SCRUB_TYPE_NLINKS] = "nlinks", 83 [XFS_SCRUB_TYPE_DIRTREE] = "dirtree", 84 [XFS_SCRUB_TYPE_METAPATH] = "metapath", 85 [XFS_SCRUB_TYPE_RGSUPER] = "rgsuper", 86 [XFS_SCRUB_TYPE_RTRMAPBT] = "rtrmapbt", 87 [XFS_SCRUB_TYPE_RTREFCBT] = "rtrefcountbt", 88 [XFS_SCRUB_TYPE_HEALTHY] = "healthy", 89 }; 90 91 /* Format the scrub stats into a text buffer, similar to pcp style. */ 92 STATIC ssize_t 93 xchk_stats_format( 94 struct xchk_stats *cs, 95 char *buf, 96 size_t remaining) 97 { 98 struct xchk_scrub_stats *css = &cs->cs_stats[0]; 99 unsigned int i; 100 ssize_t copied = 0; 101 int ret = 0; 102 103 for (i = 0; i < XFS_SCRUB_TYPE_NR; i++, css++) { 104 struct xchk_scrub_stats fss; 105 106 if (!name_map[i]) 107 continue; 108 109 spin_lock(&css->css_lock); 110 memcpy(&fss, css, offsetof(struct xchk_scrub_stats, css_lock)); 111 spin_unlock(&css->css_lock); 112 113 ret = scnprintf(buf, remaining, 114 "%s %u %u %u %u %u %u %u %u %u %llu %u %u %llu %u\n", 115 name_map[i], 116 (unsigned int)fss.invocations, 117 (unsigned int)fss.clean, 118 (unsigned int)fss.corrupt, 119 (unsigned int)fss.preen, 120 (unsigned int)fss.xfail, 121 (unsigned int)fss.xcorrupt, 122 (unsigned int)fss.incomplete, 123 (unsigned int)fss.warning, 124 (unsigned int)fss.retries, 125 (unsigned long long)fss.checktime_us, 126 (unsigned int)fss.repair_invocations, 127 (unsigned int)fss.repair_success, 128 (unsigned long long)fss.repairtime_us, 129 (unsigned int)fss.runtime_errors); 130 if (ret <= 0) 131 break; 132 133 remaining -= ret; 134 copied += ret; 135 buf += ret; 136 } 137 138 return copied > 0 ? copied : ret; 139 } 140 141 /* Estimate the worst case buffer size required to hold the whole report. */ 142 STATIC size_t 143 xchk_stats_estimate_bufsize( 144 struct xchk_stats *cs) 145 { 146 unsigned int i; 147 size_t field_width; 148 size_t ret = 0; 149 150 /* 4294967296 plus one space for each u32 field */ 151 field_width = 11 * (offsetof(struct xchk_scrub_stats, checktime_us) / 152 sizeof(uint32_t)); 153 154 /* 18446744073709551615 plus one space for each u64 field */ 155 field_width += 21 * ((offsetof(struct xchk_scrub_stats, css_lock) - 156 offsetof(struct xchk_scrub_stats, checktime_us)) / 157 sizeof(uint64_t)); 158 159 for (i = 0; i < XFS_SCRUB_TYPE_NR; i++) { 160 if (!name_map[i]) 161 continue; 162 163 /* name plus one space */ 164 ret += 1 + strlen(name_map[i]); 165 166 /* all fields, plus newline */ 167 ret += field_width + 1; 168 } 169 170 /* null byte for scnprintf */ 171 return ret + 1; 172 } 173 174 /* Clear all counters. */ 175 STATIC void 176 xchk_stats_clearall( 177 struct xchk_stats *cs) 178 { 179 struct xchk_scrub_stats *css = &cs->cs_stats[0]; 180 unsigned int i; 181 182 for (i = 0; i < XFS_SCRUB_TYPE_NR; i++, css++) { 183 spin_lock(&css->css_lock); 184 memset(css, 0, offsetof(struct xchk_scrub_stats, css_lock)); 185 spin_unlock(&css->css_lock); 186 } 187 } 188 189 #define XFS_SCRUB_OFLAG_UNCLEAN (XFS_SCRUB_OFLAG_CORRUPT | \ 190 XFS_SCRUB_OFLAG_PREEN | \ 191 XFS_SCRUB_OFLAG_XFAIL | \ 192 XFS_SCRUB_OFLAG_XCORRUPT | \ 193 XFS_SCRUB_OFLAG_INCOMPLETE | \ 194 XFS_SCRUB_OFLAG_WARNING) 195 196 STATIC void 197 xchk_stats_merge_one( 198 struct xchk_stats *cs, 199 const struct xfs_scrub_metadata *sm, 200 int error, 201 const struct xchk_stats_run *run) 202 { 203 struct xchk_scrub_stats *css; 204 unsigned int sm_flags = sm->sm_flags; 205 206 if (sm->sm_type >= XFS_SCRUB_TYPE_NR) { 207 ASSERT(sm->sm_type < XFS_SCRUB_TYPE_NR); 208 return; 209 } 210 211 /* caller applies this same transformation after we return */ 212 if (error == -EFSCORRUPTED || error == -EFSBADCRC) { 213 sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 214 error = 0; 215 } 216 217 css = &cs->cs_stats[sm->sm_type]; 218 spin_lock(&css->css_lock); 219 css->invocations++; 220 if (error) 221 css->runtime_errors++; 222 else if (!(sm_flags & XFS_SCRUB_OFLAG_UNCLEAN)) 223 css->clean++; 224 if (sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 225 css->corrupt++; 226 if (sm_flags & XFS_SCRUB_OFLAG_PREEN) 227 css->preen++; 228 if (sm_flags & XFS_SCRUB_OFLAG_XFAIL) 229 css->xfail++; 230 if (sm_flags & XFS_SCRUB_OFLAG_XCORRUPT) 231 css->xcorrupt++; 232 if (sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE) 233 css->incomplete++; 234 if (sm_flags & XFS_SCRUB_OFLAG_WARNING) 235 css->warning++; 236 css->retries += run->retries; 237 css->checktime_us += howmany_64(run->scrub_ns, NSEC_PER_USEC); 238 239 if (run->repair_attempted) 240 css->repair_invocations++; 241 if (run->repair_succeeded) 242 css->repair_success++; 243 css->repairtime_us += howmany_64(run->repair_ns, NSEC_PER_USEC); 244 spin_unlock(&css->css_lock); 245 } 246 247 /* Merge these scrub-run stats into the global and mount stat data. */ 248 void 249 xchk_stats_merge( 250 struct xfs_mount *mp, 251 const struct xfs_scrub_metadata *sm, 252 int error, 253 const struct xchk_stats_run *run) 254 { 255 if (error == -ENOENT) 256 return; 257 258 xchk_stats_merge_one(&global_stats, sm, error, run); 259 xchk_stats_merge_one(mp->m_scrub_stats, sm, error, run); 260 } 261 262 /* debugfs boilerplate */ 263 264 static ssize_t 265 xchk_scrub_stats_read( 266 struct file *file, 267 char __user *ubuf, 268 size_t count, 269 loff_t *ppos) 270 { 271 struct xchk_stats *cs = file->private_data; 272 char *buf; 273 size_t bufsize; 274 ssize_t avail, ret; 275 276 /* 277 * This generates stringly snapshot of all the scrub counters, so we 278 * do not want userspace to receive garbled text from multiple calls. 279 * If the file position is greater than 0, return a short read. 280 */ 281 if (*ppos > 0) 282 return 0; 283 284 bufsize = xchk_stats_estimate_bufsize(cs); 285 286 buf = kvmalloc(bufsize, XCHK_GFP_FLAGS); 287 if (!buf) 288 return -ENOMEM; 289 290 avail = xchk_stats_format(cs, buf, bufsize); 291 if (avail < 0) { 292 ret = avail; 293 goto out; 294 } 295 296 ret = simple_read_from_buffer(ubuf, count, ppos, buf, avail); 297 out: 298 kvfree(buf); 299 return ret; 300 } 301 302 static const struct file_operations scrub_stats_fops = { 303 .owner = THIS_MODULE, 304 .open = simple_open, 305 .read = xchk_scrub_stats_read, 306 }; 307 308 static ssize_t 309 xchk_clear_scrub_stats_write( 310 struct file *file, 311 const char __user *ubuf, 312 size_t count, 313 loff_t *ppos) 314 { 315 struct xchk_stats *cs = file->private_data; 316 unsigned int val; 317 int ret; 318 319 ret = kstrtouint_from_user(ubuf, count, 0, &val); 320 if (ret) 321 return ret; 322 323 if (val != 1) 324 return -EINVAL; 325 326 xchk_stats_clearall(cs); 327 return count; 328 } 329 330 static const struct file_operations clear_scrub_stats_fops = { 331 .owner = THIS_MODULE, 332 .open = simple_open, 333 .write = xchk_clear_scrub_stats_write, 334 }; 335 336 /* Initialize the stats object. */ 337 STATIC int 338 xchk_stats_init( 339 struct xchk_stats *cs, 340 struct xfs_mount *mp) 341 { 342 struct xchk_scrub_stats *css = &cs->cs_stats[0]; 343 unsigned int i; 344 345 for (i = 0; i < XFS_SCRUB_TYPE_NR; i++, css++) 346 spin_lock_init(&css->css_lock); 347 348 return 0; 349 } 350 351 /* Connect the stats object to debugfs. */ 352 void 353 xchk_stats_register( 354 struct xchk_stats *cs, 355 struct dentry *parent) 356 { 357 if (!parent) 358 return; 359 360 cs->cs_debugfs = xfs_debugfs_mkdir("scrub", parent); 361 if (!cs->cs_debugfs) 362 return; 363 364 debugfs_create_file("stats", 0444, cs->cs_debugfs, cs, 365 &scrub_stats_fops); 366 debugfs_create_file("clear_stats", 0200, cs->cs_debugfs, cs, 367 &clear_scrub_stats_fops); 368 } 369 370 /* Free all resources related to the stats object. */ 371 STATIC int 372 xchk_stats_teardown( 373 struct xchk_stats *cs) 374 { 375 return 0; 376 } 377 378 /* Disconnect the stats object from debugfs. */ 379 void 380 xchk_stats_unregister( 381 struct xchk_stats *cs) 382 { 383 debugfs_remove(cs->cs_debugfs); 384 } 385 386 /* Initialize global stats and register them */ 387 int __init 388 xchk_global_stats_setup( 389 struct dentry *parent) 390 { 391 int error; 392 393 error = xchk_stats_init(&global_stats, NULL); 394 if (error) 395 return error; 396 397 xchk_stats_register(&global_stats, parent); 398 return 0; 399 } 400 401 /* Unregister global stats and tear them down */ 402 void 403 xchk_global_stats_teardown(void) 404 { 405 xchk_stats_unregister(&global_stats); 406 xchk_stats_teardown(&global_stats); 407 } 408 409 /* Allocate per-mount stats */ 410 int 411 xchk_mount_stats_alloc( 412 struct xfs_mount *mp) 413 { 414 struct xchk_stats *cs; 415 int error; 416 417 cs = kvzalloc_obj(struct xchk_stats); 418 if (!cs) 419 return -ENOMEM; 420 421 error = xchk_stats_init(cs, mp); 422 if (error) 423 goto out_free; 424 425 mp->m_scrub_stats = cs; 426 return 0; 427 out_free: 428 kvfree(cs); 429 return error; 430 } 431 432 /* Free per-mount stats */ 433 void 434 xchk_mount_stats_free( 435 struct xfs_mount *mp) 436 { 437 xchk_stats_teardown(mp->m_scrub_stats); 438 kvfree(mp->m_scrub_stats); 439 mp->m_scrub_stats = NULL; 440 } 441