1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * filecheck.c 4 * 5 * Code which implements online file check. 6 * 7 * Copyright (C) 2016 SuSE. All rights reserved. 8 */ 9 10 #include <linux/list.h> 11 #include <linux/spinlock.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include <linux/kmod.h> 15 #include <linux/fs.h> 16 #include <linux/kobject.h> 17 #include <linux/sysfs.h> 18 #include <linux/sysctl.h> 19 #include <cluster/masklog.h> 20 21 #include "ocfs2.h" 22 #include "ocfs2_fs.h" 23 #include "stackglue.h" 24 #include "inode.h" 25 26 #include "filecheck.h" 27 28 29 /* File check error strings, 30 * must correspond with error number in header file. 31 */ 32 static const char * const ocfs2_filecheck_errs[] = { 33 "SUCCESS", 34 "FAILED", 35 "INPROGRESS", 36 "READONLY", 37 "INJBD", 38 "INVALIDINO", 39 "BLOCKECC", 40 "BLOCKNO", 41 "VALIDFLAG", 42 "GENERATION", 43 "UNSUPPORTED" 44 }; 45 46 struct ocfs2_filecheck_entry { 47 struct list_head fe_list; 48 unsigned long fe_ino; 49 unsigned int fe_type; 50 unsigned int fe_done:1; 51 unsigned int fe_status:31; 52 }; 53 54 struct ocfs2_filecheck_args { 55 unsigned int fa_type; 56 union { 57 unsigned long fa_ino; 58 unsigned int fa_len; 59 }; 60 }; 61 62 static const char * 63 ocfs2_filecheck_error(int errno) 64 { 65 if (!errno) 66 return ocfs2_filecheck_errs[errno]; 67 68 BUG_ON(errno < OCFS2_FILECHECK_ERR_START || 69 errno > OCFS2_FILECHECK_ERR_END); 70 return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1]; 71 } 72 73 static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj, 74 struct kobj_attribute *attr, 75 char *buf); 76 static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj, 77 struct kobj_attribute *attr, 78 const char *buf, size_t count); 79 static struct kobj_attribute ocfs2_filecheck_attr_chk = 80 __ATTR(check, S_IRUSR | S_IWUSR, 81 ocfs2_filecheck_attr_show, 82 ocfs2_filecheck_attr_store); 83 static struct kobj_attribute ocfs2_filecheck_attr_fix = 84 __ATTR(fix, S_IRUSR | S_IWUSR, 85 ocfs2_filecheck_attr_show, 86 ocfs2_filecheck_attr_store); 87 static struct kobj_attribute ocfs2_filecheck_attr_set = 88 __ATTR(set, S_IRUSR | S_IWUSR, 89 ocfs2_filecheck_attr_show, 90 ocfs2_filecheck_attr_store); 91 static struct attribute *ocfs2_filecheck_attrs[] = { 92 &ocfs2_filecheck_attr_chk.attr, 93 &ocfs2_filecheck_attr_fix.attr, 94 &ocfs2_filecheck_attr_set.attr, 95 NULL 96 }; 97 ATTRIBUTE_GROUPS(ocfs2_filecheck); 98 99 static void ocfs2_filecheck_release(struct kobject *kobj) 100 { 101 struct ocfs2_filecheck_sysfs_entry *entry = container_of(kobj, 102 struct ocfs2_filecheck_sysfs_entry, fs_kobj); 103 104 complete(&entry->fs_kobj_unregister); 105 } 106 107 static ssize_t 108 ocfs2_filecheck_show(struct kobject *kobj, struct attribute *attr, char *buf) 109 { 110 ssize_t ret = -EIO; 111 struct kobj_attribute *kattr = container_of(attr, 112 struct kobj_attribute, attr); 113 114 kobject_get(kobj); 115 if (kattr->show) 116 ret = kattr->show(kobj, kattr, buf); 117 kobject_put(kobj); 118 return ret; 119 } 120 121 static ssize_t 122 ocfs2_filecheck_store(struct kobject *kobj, struct attribute *attr, 123 const char *buf, size_t count) 124 { 125 ssize_t ret = -EIO; 126 struct kobj_attribute *kattr = container_of(attr, 127 struct kobj_attribute, attr); 128 129 kobject_get(kobj); 130 if (kattr->store) 131 ret = kattr->store(kobj, kattr, buf, count); 132 kobject_put(kobj); 133 return ret; 134 } 135 136 static const struct sysfs_ops ocfs2_filecheck_ops = { 137 .show = ocfs2_filecheck_show, 138 .store = ocfs2_filecheck_store, 139 }; 140 141 static struct kobj_type ocfs2_ktype_filecheck = { 142 .default_groups = ocfs2_filecheck_groups, 143 .sysfs_ops = &ocfs2_filecheck_ops, 144 .release = ocfs2_filecheck_release, 145 }; 146 147 static void 148 ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry) 149 { 150 struct ocfs2_filecheck_entry *p; 151 152 spin_lock(&entry->fs_fcheck->fc_lock); 153 while (!list_empty(&entry->fs_fcheck->fc_head)) { 154 p = list_first_entry(&entry->fs_fcheck->fc_head, 155 struct ocfs2_filecheck_entry, fe_list); 156 list_del(&p->fe_list); 157 BUG_ON(!p->fe_done); /* To free a undone file check entry */ 158 kfree(p); 159 } 160 spin_unlock(&entry->fs_fcheck->fc_lock); 161 162 kfree(entry->fs_fcheck); 163 entry->fs_fcheck = NULL; 164 } 165 166 int ocfs2_filecheck_create_sysfs(struct ocfs2_super *osb) 167 { 168 int ret; 169 struct ocfs2_filecheck *fcheck; 170 struct ocfs2_filecheck_sysfs_entry *entry = &osb->osb_fc_ent; 171 172 fcheck = kmalloc_obj(struct ocfs2_filecheck, GFP_NOFS); 173 if (!fcheck) 174 return -ENOMEM; 175 176 INIT_LIST_HEAD(&fcheck->fc_head); 177 spin_lock_init(&fcheck->fc_lock); 178 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE; 179 fcheck->fc_size = 0; 180 fcheck->fc_done = 0; 181 182 /* Publish the initialized owner before activating its sysfs callbacks. */ 183 entry->fs_fcheck = fcheck; 184 entry->fs_kobj.kset = osb->osb_dev_kset; 185 init_completion(&entry->fs_kobj_unregister); 186 ret = kobject_init_and_add(&entry->fs_kobj, &ocfs2_ktype_filecheck, 187 NULL, "filecheck"); 188 if (ret) { 189 /* Mount failure still calls ocfs2_filecheck_remove_sysfs(). */ 190 entry->fs_fcheck = NULL; 191 kobject_put(&entry->fs_kobj); 192 /* Keep ocfs2_super alive until the kobject is released. */ 193 wait_for_completion(&entry->fs_kobj_unregister); 194 kfree(fcheck); 195 return ret; 196 } 197 198 return 0; 199 } 200 201 void ocfs2_filecheck_remove_sysfs(struct ocfs2_super *osb) 202 { 203 if (!osb->osb_fc_ent.fs_fcheck) 204 return; 205 206 kobject_del(&osb->osb_fc_ent.fs_kobj); 207 kobject_put(&osb->osb_fc_ent.fs_kobj); 208 wait_for_completion(&osb->osb_fc_ent.fs_kobj_unregister); 209 ocfs2_filecheck_sysfs_free(&osb->osb_fc_ent); 210 } 211 212 static int 213 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent, 214 unsigned int count); 215 static int 216 ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent, 217 unsigned int len) 218 { 219 int ret; 220 221 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE)) 222 return -EINVAL; 223 224 spin_lock(&ent->fs_fcheck->fc_lock); 225 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) { 226 mlog(ML_NOTICE, 227 "Cannot set online file check maximum entry number " 228 "to %u due to too many pending entries(%u)\n", 229 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done); 230 ret = -EBUSY; 231 } else { 232 if (len < ent->fs_fcheck->fc_size) 233 BUG_ON(!ocfs2_filecheck_erase_entries(ent, 234 ent->fs_fcheck->fc_size - len)); 235 236 ent->fs_fcheck->fc_max = len; 237 ret = 0; 238 } 239 spin_unlock(&ent->fs_fcheck->fc_lock); 240 241 return ret; 242 } 243 244 #define OCFS2_FILECHECK_ARGS_LEN 24 245 static int 246 ocfs2_filecheck_args_get_long(const char *buf, size_t count, 247 unsigned long *val) 248 { 249 char buffer[OCFS2_FILECHECK_ARGS_LEN]; 250 251 memcpy(buffer, buf, count); 252 buffer[count] = '\0'; 253 254 if (kstrtoul(buffer, 0, val)) 255 return 1; 256 257 return 0; 258 } 259 260 static int 261 ocfs2_filecheck_type_parse(const char *name, unsigned int *type) 262 { 263 if (!strncmp(name, "fix", 4)) 264 *type = OCFS2_FILECHECK_TYPE_FIX; 265 else if (!strncmp(name, "check", 6)) 266 *type = OCFS2_FILECHECK_TYPE_CHK; 267 else if (!strncmp(name, "set", 4)) 268 *type = OCFS2_FILECHECK_TYPE_SET; 269 else 270 return 1; 271 272 return 0; 273 } 274 275 static int 276 ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count, 277 struct ocfs2_filecheck_args *args) 278 { 279 unsigned long val = 0; 280 unsigned int type; 281 282 /* too short/long args length */ 283 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN)) 284 return 1; 285 286 if (ocfs2_filecheck_type_parse(name, &type)) 287 return 1; 288 if (ocfs2_filecheck_args_get_long(buf, count, &val)) 289 return 1; 290 291 if (val <= 0) 292 return 1; 293 294 args->fa_type = type; 295 if (type == OCFS2_FILECHECK_TYPE_SET) 296 args->fa_len = (unsigned int)val; 297 else 298 args->fa_ino = val; 299 300 return 0; 301 } 302 303 static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj, 304 struct kobj_attribute *attr, 305 char *buf) 306 { 307 308 ssize_t ret = 0, total = 0, remain = PAGE_SIZE; 309 unsigned int type; 310 struct ocfs2_filecheck_entry *p; 311 struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj, 312 struct ocfs2_filecheck_sysfs_entry, fs_kobj); 313 314 if (ocfs2_filecheck_type_parse(attr->attr.name, &type)) 315 return -EINVAL; 316 317 if (type == OCFS2_FILECHECK_TYPE_SET) { 318 spin_lock(&ent->fs_fcheck->fc_lock); 319 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max); 320 spin_unlock(&ent->fs_fcheck->fc_lock); 321 goto exit; 322 } 323 324 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n"); 325 total += ret; 326 remain -= ret; 327 spin_lock(&ent->fs_fcheck->fc_lock); 328 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) { 329 if (p->fe_type != type) 330 continue; 331 332 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n", 333 p->fe_ino, p->fe_done, 334 ocfs2_filecheck_error(p->fe_status)); 335 if (ret >= remain) { 336 /* snprintf() didn't fit */ 337 total = -E2BIG; 338 break; 339 } 340 total += ret; 341 remain -= ret; 342 } 343 spin_unlock(&ent->fs_fcheck->fc_lock); 344 345 exit: 346 return total; 347 } 348 349 static inline int 350 ocfs2_filecheck_is_dup_entry(struct ocfs2_filecheck_sysfs_entry *ent, 351 unsigned long ino) 352 { 353 struct ocfs2_filecheck_entry *p; 354 355 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) { 356 if (!p->fe_done) { 357 if (p->fe_ino == ino) 358 return 1; 359 } 360 } 361 362 return 0; 363 } 364 365 static inline int 366 ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent) 367 { 368 struct ocfs2_filecheck_entry *p; 369 370 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) { 371 if (p->fe_done) { 372 list_del(&p->fe_list); 373 kfree(p); 374 ent->fs_fcheck->fc_size--; 375 ent->fs_fcheck->fc_done--; 376 return 1; 377 } 378 } 379 380 return 0; 381 } 382 383 static int 384 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent, 385 unsigned int count) 386 { 387 unsigned int i = 0; 388 unsigned int ret = 0; 389 390 while (i++ < count) { 391 if (ocfs2_filecheck_erase_entry(ent)) 392 ret++; 393 else 394 break; 395 } 396 397 return (ret == count ? 1 : 0); 398 } 399 400 static void 401 ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent, 402 struct ocfs2_filecheck_entry *entry) 403 { 404 spin_lock(&ent->fs_fcheck->fc_lock); 405 entry->fe_done = 1; 406 ent->fs_fcheck->fc_done++; 407 spin_unlock(&ent->fs_fcheck->fc_lock); 408 } 409 410 static unsigned int 411 ocfs2_filecheck_handle(struct ocfs2_super *osb, 412 unsigned long ino, unsigned int flags) 413 { 414 unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS; 415 struct inode *inode = NULL; 416 int rc; 417 418 inode = ocfs2_iget(osb, ino, flags, 0); 419 if (IS_ERR(inode)) { 420 rc = (int)(-(long)inode); 421 if (rc >= OCFS2_FILECHECK_ERR_START && 422 rc < OCFS2_FILECHECK_ERR_END) 423 ret = rc; 424 else 425 ret = OCFS2_FILECHECK_ERR_FAILED; 426 } else 427 iput(inode); 428 429 return ret; 430 } 431 432 static void 433 ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent, 434 struct ocfs2_filecheck_entry *entry) 435 { 436 struct ocfs2_super *osb = container_of(ent, struct ocfs2_super, 437 osb_fc_ent); 438 439 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK) 440 entry->fe_status = ocfs2_filecheck_handle(osb, 441 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK); 442 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX) 443 entry->fe_status = ocfs2_filecheck_handle(osb, 444 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX); 445 else 446 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED; 447 448 ocfs2_filecheck_done_entry(ent, entry); 449 } 450 451 static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj, 452 struct kobj_attribute *attr, 453 const char *buf, size_t count) 454 { 455 ssize_t ret = 0; 456 struct ocfs2_filecheck_args args; 457 struct ocfs2_filecheck_entry *entry; 458 struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj, 459 struct ocfs2_filecheck_sysfs_entry, fs_kobj); 460 461 if (count == 0) 462 return count; 463 464 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) 465 return -EINVAL; 466 467 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) { 468 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len); 469 goto exit; 470 } 471 472 entry = kmalloc_obj(struct ocfs2_filecheck_entry, GFP_NOFS); 473 if (!entry) { 474 ret = -ENOMEM; 475 goto exit; 476 } 477 478 spin_lock(&ent->fs_fcheck->fc_lock); 479 if (ocfs2_filecheck_is_dup_entry(ent, args.fa_ino)) { 480 ret = -EEXIST; 481 kfree(entry); 482 } else if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) && 483 (ent->fs_fcheck->fc_done == 0)) { 484 mlog(ML_NOTICE, 485 "Cannot do more file check " 486 "since file check queue(%u) is full now\n", 487 ent->fs_fcheck->fc_max); 488 ret = -EAGAIN; 489 kfree(entry); 490 } else { 491 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) && 492 (ent->fs_fcheck->fc_done > 0)) { 493 /* Delete the oldest entry which was done, 494 * make sure the entry size in list does 495 * not exceed maximum value 496 */ 497 BUG_ON(!ocfs2_filecheck_erase_entry(ent)); 498 } 499 500 entry->fe_ino = args.fa_ino; 501 entry->fe_type = args.fa_type; 502 entry->fe_done = 0; 503 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS; 504 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head); 505 ent->fs_fcheck->fc_size++; 506 } 507 spin_unlock(&ent->fs_fcheck->fc_lock); 508 509 if (!ret) 510 ocfs2_filecheck_handle_entry(ent, entry); 511 512 exit: 513 return ret ?: count; 514 } 515