1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements online file check.
8 * Copyright (C) 2016 SuSE. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/kmod.h>
26 #include <linux/kobject.h>
27 #include <linux/sysfs.h>
28 #include <linux/sysctl.h>
29 #include <cluster/masklog.h>
33 #include "stackglue.h"
36 #include "filecheck.h"
39 /* File check error strings,
40 * must correspond with error number in header file.
42 static const char * const ocfs2_filecheck_errs[] = {
56 static DEFINE_SPINLOCK(ocfs2_filecheck_sysfs_lock);
57 static LIST_HEAD(ocfs2_filecheck_sysfs_list);
59 struct ocfs2_filecheck {
60 struct list_head fc_head; /* File check entry list head */
62 unsigned int fc_max; /* Maximum number of entry in list */
63 unsigned int fc_size; /* Current entry count in list */
64 unsigned int fc_done; /* Finished entry count in list */
67 struct ocfs2_filecheck_sysfs_entry { /* sysfs entry per mounting */
68 struct list_head fs_list;
70 struct super_block *fs_sb;
71 struct kset *fs_devicekset;
72 struct kset *fs_fcheckkset;
73 struct ocfs2_filecheck *fs_fcheck;
76 #define OCFS2_FILECHECK_MAXSIZE 100
77 #define OCFS2_FILECHECK_MINSIZE 10
79 /* File check operation type */
81 OCFS2_FILECHECK_TYPE_CHK = 0, /* Check a file(inode) */
82 OCFS2_FILECHECK_TYPE_FIX, /* Fix a file(inode) */
83 OCFS2_FILECHECK_TYPE_SET = 100 /* Set entry list maximum size */
86 struct ocfs2_filecheck_entry {
87 struct list_head fe_list;
90 unsigned int fe_done:1;
91 unsigned int fe_status:31;
94 struct ocfs2_filecheck_args {
103 ocfs2_filecheck_error(int errno)
106 return ocfs2_filecheck_errs[errno];
108 BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
109 errno > OCFS2_FILECHECK_ERR_END);
110 return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
113 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
114 struct kobj_attribute *attr,
116 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
117 struct kobj_attribute *attr,
118 const char *buf, size_t count);
119 static struct kobj_attribute ocfs2_attr_filecheck_chk =
120 __ATTR(check, S_IRUSR | S_IWUSR,
121 ocfs2_filecheck_show,
122 ocfs2_filecheck_store);
123 static struct kobj_attribute ocfs2_attr_filecheck_fix =
124 __ATTR(fix, S_IRUSR | S_IWUSR,
125 ocfs2_filecheck_show,
126 ocfs2_filecheck_store);
127 static struct kobj_attribute ocfs2_attr_filecheck_set =
128 __ATTR(set, S_IRUSR | S_IWUSR,
129 ocfs2_filecheck_show,
130 ocfs2_filecheck_store);
133 ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
135 struct ocfs2_filecheck_entry *p;
137 if (!atomic_dec_and_test(&entry->fs_count))
138 wait_on_atomic_t(&entry->fs_count, atomic_t_wait,
139 TASK_UNINTERRUPTIBLE);
141 spin_lock(&entry->fs_fcheck->fc_lock);
142 while (!list_empty(&entry->fs_fcheck->fc_head)) {
143 p = list_first_entry(&entry->fs_fcheck->fc_head,
144 struct ocfs2_filecheck_entry, fe_list);
145 list_del(&p->fe_list);
146 BUG_ON(!p->fe_done); /* To free a undone file check entry */
149 spin_unlock(&entry->fs_fcheck->fc_lock);
151 kset_unregister(entry->fs_fcheckkset);
152 kset_unregister(entry->fs_devicekset);
153 kfree(entry->fs_fcheck);
158 ocfs2_filecheck_sysfs_add(struct ocfs2_filecheck_sysfs_entry *entry)
160 spin_lock(&ocfs2_filecheck_sysfs_lock);
161 list_add_tail(&entry->fs_list, &ocfs2_filecheck_sysfs_list);
162 spin_unlock(&ocfs2_filecheck_sysfs_lock);
165 static int ocfs2_filecheck_sysfs_del(const char *devname)
167 struct ocfs2_filecheck_sysfs_entry *p;
169 spin_lock(&ocfs2_filecheck_sysfs_lock);
170 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
171 if (!strcmp(p->fs_sb->s_id, devname)) {
172 list_del(&p->fs_list);
173 spin_unlock(&ocfs2_filecheck_sysfs_lock);
174 ocfs2_filecheck_sysfs_free(p);
178 spin_unlock(&ocfs2_filecheck_sysfs_lock);
183 ocfs2_filecheck_sysfs_put(struct ocfs2_filecheck_sysfs_entry *entry)
185 if (atomic_dec_and_test(&entry->fs_count))
186 wake_up_atomic_t(&entry->fs_count);
189 static struct ocfs2_filecheck_sysfs_entry *
190 ocfs2_filecheck_sysfs_get(const char *devname)
192 struct ocfs2_filecheck_sysfs_entry *p = NULL;
194 spin_lock(&ocfs2_filecheck_sysfs_lock);
195 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
196 if (!strcmp(p->fs_sb->s_id, devname)) {
197 atomic_inc(&p->fs_count);
198 spin_unlock(&ocfs2_filecheck_sysfs_lock);
202 spin_unlock(&ocfs2_filecheck_sysfs_lock);
206 int ocfs2_filecheck_create_sysfs(struct super_block *sb)
209 struct kset *device_kset = NULL;
210 struct kset *fcheck_kset = NULL;
211 struct ocfs2_filecheck *fcheck = NULL;
212 struct ocfs2_filecheck_sysfs_entry *entry = NULL;
213 struct attribute **attrs = NULL;
214 struct attribute_group attrgp;
219 attrs = kmalloc(sizeof(struct attribute *) * 4, GFP_NOFS);
224 attrs[0] = &ocfs2_attr_filecheck_chk.attr;
225 attrs[1] = &ocfs2_attr_filecheck_fix.attr;
226 attrs[2] = &ocfs2_attr_filecheck_set.attr;
228 memset(&attrgp, 0, sizeof(attrgp));
229 attrgp.attrs = attrs;
232 fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
237 INIT_LIST_HEAD(&fcheck->fc_head);
238 spin_lock_init(&fcheck->fc_lock);
239 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
244 if (strlen(sb->s_id) <= 0) {
246 "Cannot get device basename when create filecheck sysfs\n");
251 device_kset = kset_create_and_add(sb->s_id, NULL, &ocfs2_kset->kobj);
257 fcheck_kset = kset_create_and_add("filecheck", NULL,
264 ret = sysfs_create_group(&fcheck_kset->kobj, &attrgp);
268 entry = kmalloc(sizeof(struct ocfs2_filecheck_sysfs_entry), GFP_NOFS);
273 atomic_set(&entry->fs_count, 1);
275 entry->fs_devicekset = device_kset;
276 entry->fs_fcheckkset = fcheck_kset;
277 entry->fs_fcheck = fcheck;
278 ocfs2_filecheck_sysfs_add(entry);
288 kset_unregister(fcheck_kset);
289 kset_unregister(device_kset);
293 int ocfs2_filecheck_remove_sysfs(struct super_block *sb)
295 return ocfs2_filecheck_sysfs_del(sb->s_id);
299 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
302 ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
307 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
310 spin_lock(&ent->fs_fcheck->fc_lock);
311 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
313 "Cannot set online file check maximum entry number "
314 "to %u due to too many pending entries(%u)\n",
315 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
318 if (len < ent->fs_fcheck->fc_size)
319 BUG_ON(!ocfs2_filecheck_erase_entries(ent,
320 ent->fs_fcheck->fc_size - len));
322 ent->fs_fcheck->fc_max = len;
325 spin_unlock(&ent->fs_fcheck->fc_lock);
330 #define OCFS2_FILECHECK_ARGS_LEN 24
332 ocfs2_filecheck_args_get_long(const char *buf, size_t count,
335 char buffer[OCFS2_FILECHECK_ARGS_LEN];
337 memcpy(buffer, buf, count);
338 buffer[count] = '\0';
340 if (kstrtoul(buffer, 0, val))
347 ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
349 if (!strncmp(name, "fix", 4))
350 *type = OCFS2_FILECHECK_TYPE_FIX;
351 else if (!strncmp(name, "check", 6))
352 *type = OCFS2_FILECHECK_TYPE_CHK;
353 else if (!strncmp(name, "set", 4))
354 *type = OCFS2_FILECHECK_TYPE_SET;
362 ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
363 struct ocfs2_filecheck_args *args)
365 unsigned long val = 0;
368 /* too short/long args length */
369 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
372 if (ocfs2_filecheck_type_parse(name, &type))
374 if (ocfs2_filecheck_args_get_long(buf, count, &val))
380 args->fa_type = type;
381 if (type == OCFS2_FILECHECK_TYPE_SET)
382 args->fa_len = (unsigned int)val;
389 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
390 struct kobj_attribute *attr,
394 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
396 struct ocfs2_filecheck_entry *p;
397 struct ocfs2_filecheck_sysfs_entry *ent;
399 if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
402 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
405 "Cannot get the corresponding entry via device basename %s\n",
410 if (type == OCFS2_FILECHECK_TYPE_SET) {
411 spin_lock(&ent->fs_fcheck->fc_lock);
412 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
413 spin_unlock(&ent->fs_fcheck->fc_lock);
417 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
420 spin_lock(&ent->fs_fcheck->fc_lock);
421 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
422 if (p->fe_type != type)
425 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
426 p->fe_ino, p->fe_done,
427 ocfs2_filecheck_error(p->fe_status));
433 /* snprintf() didn't fit */
440 spin_unlock(&ent->fs_fcheck->fc_lock);
443 ocfs2_filecheck_sysfs_put(ent);
448 ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
450 struct ocfs2_filecheck_entry *p;
452 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
454 list_del(&p->fe_list);
456 ent->fs_fcheck->fc_size--;
457 ent->fs_fcheck->fc_done--;
466 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
470 unsigned int ret = 0;
472 while (i++ < count) {
473 if (ocfs2_filecheck_erase_entry(ent))
479 return (ret == count ? 1 : 0);
483 ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
484 struct ocfs2_filecheck_entry *entry)
487 spin_lock(&ent->fs_fcheck->fc_lock);
488 ent->fs_fcheck->fc_done++;
489 spin_unlock(&ent->fs_fcheck->fc_lock);
493 ocfs2_filecheck_handle(struct super_block *sb,
494 unsigned long ino, unsigned int flags)
496 unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
497 struct inode *inode = NULL;
500 inode = ocfs2_iget(OCFS2_SB(sb), ino, flags, 0);
502 rc = (int)(-(long)inode);
503 if (rc >= OCFS2_FILECHECK_ERR_START &&
504 rc < OCFS2_FILECHECK_ERR_END)
507 ret = OCFS2_FILECHECK_ERR_FAILED;
515 ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
516 struct ocfs2_filecheck_entry *entry)
518 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
519 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
520 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
521 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
522 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
523 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
525 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
527 ocfs2_filecheck_done_entry(ent, entry);
530 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
531 struct kobj_attribute *attr,
532 const char *buf, size_t count)
534 struct ocfs2_filecheck_args args;
535 struct ocfs2_filecheck_entry *entry;
536 struct ocfs2_filecheck_sysfs_entry *ent;
542 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) {
543 mlog(ML_ERROR, "Invalid arguments for online file check\n");
547 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
550 "Cannot get the corresponding entry via device basename %s\n",
555 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
556 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
560 entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
566 spin_lock(&ent->fs_fcheck->fc_lock);
567 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
568 (ent->fs_fcheck->fc_done == 0)) {
570 "Cannot do more file check "
571 "since file check queue(%u) is full now\n",
572 ent->fs_fcheck->fc_max);
576 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
577 (ent->fs_fcheck->fc_done > 0)) {
578 /* Delete the oldest entry which was done,
579 * make sure the entry size in list does
580 * not exceed maximum value
582 BUG_ON(!ocfs2_filecheck_erase_entry(ent));
585 entry->fe_ino = args.fa_ino;
586 entry->fe_type = args.fa_type;
588 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
589 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
590 ent->fs_fcheck->fc_size++;
592 spin_unlock(&ent->fs_fcheck->fc_lock);
595 ocfs2_filecheck_handle_entry(ent, entry);
598 ocfs2_filecheck_sysfs_put(ent);
599 return (!ret ? count : ret);