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_entry {
60 struct list_head fe_list;
63 unsigned int fe_done:1;
64 unsigned int fe_status:31;
67 struct ocfs2_filecheck_args {
76 ocfs2_filecheck_error(int errno)
79 return ocfs2_filecheck_errs[errno];
81 BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
82 errno > OCFS2_FILECHECK_ERR_END);
83 return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
86 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
87 struct kobj_attribute *attr,
89 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
90 struct kobj_attribute *attr,
91 const char *buf, size_t count);
92 static struct kobj_attribute ocfs2_attr_filecheck_chk =
93 __ATTR(check, S_IRUSR | S_IWUSR,
95 ocfs2_filecheck_store);
96 static struct kobj_attribute ocfs2_attr_filecheck_fix =
97 __ATTR(fix, S_IRUSR | S_IWUSR,
99 ocfs2_filecheck_store);
100 static struct kobj_attribute ocfs2_attr_filecheck_set =
101 __ATTR(set, S_IRUSR | S_IWUSR,
102 ocfs2_filecheck_show,
103 ocfs2_filecheck_store);
106 ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
108 struct ocfs2_filecheck_entry *p;
110 if (!atomic_dec_and_test(&entry->fs_count)) {
111 wait_var_event(&entry->fs_count,
112 !atomic_read(&entry->fs_count));
115 spin_lock(&entry->fs_fcheck->fc_lock);
116 while (!list_empty(&entry->fs_fcheck->fc_head)) {
117 p = list_first_entry(&entry->fs_fcheck->fc_head,
118 struct ocfs2_filecheck_entry, fe_list);
119 list_del(&p->fe_list);
120 BUG_ON(!p->fe_done); /* To free a undone file check entry */
123 spin_unlock(&entry->fs_fcheck->fc_lock);
125 kset_unregister(entry->fs_fcheckkset);
126 kset_unregister(entry->fs_devicekset);
127 kfree(entry->fs_fcheck);
132 ocfs2_filecheck_sysfs_add(struct ocfs2_filecheck_sysfs_entry *entry)
134 spin_lock(&ocfs2_filecheck_sysfs_lock);
135 list_add_tail(&entry->fs_list, &ocfs2_filecheck_sysfs_list);
136 spin_unlock(&ocfs2_filecheck_sysfs_lock);
139 static int ocfs2_filecheck_sysfs_del(const char *devname)
141 struct ocfs2_filecheck_sysfs_entry *p;
143 spin_lock(&ocfs2_filecheck_sysfs_lock);
144 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
145 if (!strcmp(p->fs_sb->s_id, devname)) {
146 list_del(&p->fs_list);
147 spin_unlock(&ocfs2_filecheck_sysfs_lock);
148 ocfs2_filecheck_sysfs_free(p);
152 spin_unlock(&ocfs2_filecheck_sysfs_lock);
157 ocfs2_filecheck_sysfs_put(struct ocfs2_filecheck_sysfs_entry *entry)
159 if (atomic_dec_and_test(&entry->fs_count))
160 wake_up_var(&entry->fs_count);
163 static struct ocfs2_filecheck_sysfs_entry *
164 ocfs2_filecheck_sysfs_get(const char *devname)
166 struct ocfs2_filecheck_sysfs_entry *p = NULL;
168 spin_lock(&ocfs2_filecheck_sysfs_lock);
169 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
170 if (!strcmp(p->fs_sb->s_id, devname)) {
171 atomic_inc(&p->fs_count);
172 spin_unlock(&ocfs2_filecheck_sysfs_lock);
176 spin_unlock(&ocfs2_filecheck_sysfs_lock);
180 int ocfs2_filecheck_create_sysfs(struct super_block *sb)
183 struct kset *device_kset = NULL;
184 struct kset *fcheck_kset = NULL;
185 struct ocfs2_filecheck *fcheck = NULL;
186 struct ocfs2_filecheck_sysfs_entry *entry = NULL;
187 struct attribute **attrs = NULL;
188 struct attribute_group attrgp;
193 attrs = kmalloc(sizeof(struct attribute *) * 4, GFP_NOFS);
198 attrs[0] = &ocfs2_attr_filecheck_chk.attr;
199 attrs[1] = &ocfs2_attr_filecheck_fix.attr;
200 attrs[2] = &ocfs2_attr_filecheck_set.attr;
202 memset(&attrgp, 0, sizeof(attrgp));
203 attrgp.attrs = attrs;
206 fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
211 INIT_LIST_HEAD(&fcheck->fc_head);
212 spin_lock_init(&fcheck->fc_lock);
213 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
218 if (strlen(sb->s_id) <= 0) {
220 "Cannot get device basename when create filecheck sysfs\n");
225 device_kset = kset_create_and_add(sb->s_id, NULL, &ocfs2_kset->kobj);
231 fcheck_kset = kset_create_and_add("filecheck", NULL,
238 ret = sysfs_create_group(&fcheck_kset->kobj, &attrgp);
242 entry = kmalloc(sizeof(struct ocfs2_filecheck_sysfs_entry), GFP_NOFS);
247 atomic_set(&entry->fs_count, 1);
249 entry->fs_devicekset = device_kset;
250 entry->fs_fcheckkset = fcheck_kset;
251 entry->fs_fcheck = fcheck;
252 ocfs2_filecheck_sysfs_add(entry);
262 kset_unregister(fcheck_kset);
263 kset_unregister(device_kset);
267 int ocfs2_filecheck_remove_sysfs(struct super_block *sb)
269 return ocfs2_filecheck_sysfs_del(sb->s_id);
273 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
276 ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
281 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
284 spin_lock(&ent->fs_fcheck->fc_lock);
285 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
287 "Cannot set online file check maximum entry number "
288 "to %u due to too many pending entries(%u)\n",
289 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
292 if (len < ent->fs_fcheck->fc_size)
293 BUG_ON(!ocfs2_filecheck_erase_entries(ent,
294 ent->fs_fcheck->fc_size - len));
296 ent->fs_fcheck->fc_max = len;
299 spin_unlock(&ent->fs_fcheck->fc_lock);
304 #define OCFS2_FILECHECK_ARGS_LEN 24
306 ocfs2_filecheck_args_get_long(const char *buf, size_t count,
309 char buffer[OCFS2_FILECHECK_ARGS_LEN];
311 memcpy(buffer, buf, count);
312 buffer[count] = '\0';
314 if (kstrtoul(buffer, 0, val))
321 ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
323 if (!strncmp(name, "fix", 4))
324 *type = OCFS2_FILECHECK_TYPE_FIX;
325 else if (!strncmp(name, "check", 6))
326 *type = OCFS2_FILECHECK_TYPE_CHK;
327 else if (!strncmp(name, "set", 4))
328 *type = OCFS2_FILECHECK_TYPE_SET;
336 ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
337 struct ocfs2_filecheck_args *args)
339 unsigned long val = 0;
342 /* too short/long args length */
343 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
346 if (ocfs2_filecheck_type_parse(name, &type))
348 if (ocfs2_filecheck_args_get_long(buf, count, &val))
354 args->fa_type = type;
355 if (type == OCFS2_FILECHECK_TYPE_SET)
356 args->fa_len = (unsigned int)val;
363 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
364 struct kobj_attribute *attr,
368 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
370 struct ocfs2_filecheck_entry *p;
371 struct ocfs2_filecheck_sysfs_entry *ent;
373 if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
376 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
379 "Cannot get the corresponding entry via device basename %s\n",
384 if (type == OCFS2_FILECHECK_TYPE_SET) {
385 spin_lock(&ent->fs_fcheck->fc_lock);
386 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
387 spin_unlock(&ent->fs_fcheck->fc_lock);
391 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
394 spin_lock(&ent->fs_fcheck->fc_lock);
395 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
396 if (p->fe_type != type)
399 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
400 p->fe_ino, p->fe_done,
401 ocfs2_filecheck_error(p->fe_status));
407 /* snprintf() didn't fit */
414 spin_unlock(&ent->fs_fcheck->fc_lock);
417 ocfs2_filecheck_sysfs_put(ent);
422 ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
424 struct ocfs2_filecheck_entry *p;
426 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
428 list_del(&p->fe_list);
430 ent->fs_fcheck->fc_size--;
431 ent->fs_fcheck->fc_done--;
440 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
444 unsigned int ret = 0;
446 while (i++ < count) {
447 if (ocfs2_filecheck_erase_entry(ent))
453 return (ret == count ? 1 : 0);
457 ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
458 struct ocfs2_filecheck_entry *entry)
460 spin_lock(&ent->fs_fcheck->fc_lock);
462 ent->fs_fcheck->fc_done++;
463 spin_unlock(&ent->fs_fcheck->fc_lock);
467 ocfs2_filecheck_handle(struct super_block *sb,
468 unsigned long ino, unsigned int flags)
470 unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
471 struct inode *inode = NULL;
474 inode = ocfs2_iget(OCFS2_SB(sb), ino, flags, 0);
476 rc = (int)(-(long)inode);
477 if (rc >= OCFS2_FILECHECK_ERR_START &&
478 rc < OCFS2_FILECHECK_ERR_END)
481 ret = OCFS2_FILECHECK_ERR_FAILED;
489 ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
490 struct ocfs2_filecheck_entry *entry)
492 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
493 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
494 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
495 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
496 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
497 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
499 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
501 ocfs2_filecheck_done_entry(ent, entry);
504 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
505 struct kobj_attribute *attr,
506 const char *buf, size_t count)
508 struct ocfs2_filecheck_args args;
509 struct ocfs2_filecheck_entry *entry;
510 struct ocfs2_filecheck_sysfs_entry *ent;
516 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) {
517 mlog(ML_ERROR, "Invalid arguments for online file check\n");
521 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
524 "Cannot get the corresponding entry via device basename %s\n",
529 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
530 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
534 entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
540 spin_lock(&ent->fs_fcheck->fc_lock);
541 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
542 (ent->fs_fcheck->fc_done == 0)) {
544 "Cannot do more file check "
545 "since file check queue(%u) is full now\n",
546 ent->fs_fcheck->fc_max);
550 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
551 (ent->fs_fcheck->fc_done > 0)) {
552 /* Delete the oldest entry which was done,
553 * make sure the entry size in list does
554 * not exceed maximum value
556 BUG_ON(!ocfs2_filecheck_erase_entry(ent));
559 entry->fe_ino = args.fa_ino;
560 entry->fe_type = args.fa_type;
562 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
563 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
564 ent->fs_fcheck->fc_size++;
566 spin_unlock(&ent->fs_fcheck->fc_lock);
569 ocfs2_filecheck_handle_entry(ent, entry);
572 ocfs2_filecheck_sysfs_put(ent);
573 return (!ret ? count : ret);