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_var_event(&entry->fs_count,
139 !atomic_read(&entry->fs_count));
142 spin_lock(&entry->fs_fcheck->fc_lock);
143 while (!list_empty(&entry->fs_fcheck->fc_head)) {
144 p = list_first_entry(&entry->fs_fcheck->fc_head,
145 struct ocfs2_filecheck_entry, fe_list);
146 list_del(&p->fe_list);
147 BUG_ON(!p->fe_done); /* To free a undone file check entry */
150 spin_unlock(&entry->fs_fcheck->fc_lock);
152 kset_unregister(entry->fs_fcheckkset);
153 kset_unregister(entry->fs_devicekset);
154 kfree(entry->fs_fcheck);
159 ocfs2_filecheck_sysfs_add(struct ocfs2_filecheck_sysfs_entry *entry)
161 spin_lock(&ocfs2_filecheck_sysfs_lock);
162 list_add_tail(&entry->fs_list, &ocfs2_filecheck_sysfs_list);
163 spin_unlock(&ocfs2_filecheck_sysfs_lock);
166 static int ocfs2_filecheck_sysfs_del(const char *devname)
168 struct ocfs2_filecheck_sysfs_entry *p;
170 spin_lock(&ocfs2_filecheck_sysfs_lock);
171 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
172 if (!strcmp(p->fs_sb->s_id, devname)) {
173 list_del(&p->fs_list);
174 spin_unlock(&ocfs2_filecheck_sysfs_lock);
175 ocfs2_filecheck_sysfs_free(p);
179 spin_unlock(&ocfs2_filecheck_sysfs_lock);
184 ocfs2_filecheck_sysfs_put(struct ocfs2_filecheck_sysfs_entry *entry)
186 if (atomic_dec_and_test(&entry->fs_count))
187 wake_up_var(&entry->fs_count);
190 static struct ocfs2_filecheck_sysfs_entry *
191 ocfs2_filecheck_sysfs_get(const char *devname)
193 struct ocfs2_filecheck_sysfs_entry *p = NULL;
195 spin_lock(&ocfs2_filecheck_sysfs_lock);
196 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
197 if (!strcmp(p->fs_sb->s_id, devname)) {
198 atomic_inc(&p->fs_count);
199 spin_unlock(&ocfs2_filecheck_sysfs_lock);
203 spin_unlock(&ocfs2_filecheck_sysfs_lock);
207 int ocfs2_filecheck_create_sysfs(struct super_block *sb)
210 struct kset *device_kset = NULL;
211 struct kset *fcheck_kset = NULL;
212 struct ocfs2_filecheck *fcheck = NULL;
213 struct ocfs2_filecheck_sysfs_entry *entry = NULL;
214 struct attribute **attrs = NULL;
215 struct attribute_group attrgp;
220 attrs = kmalloc(sizeof(struct attribute *) * 4, GFP_NOFS);
225 attrs[0] = &ocfs2_attr_filecheck_chk.attr;
226 attrs[1] = &ocfs2_attr_filecheck_fix.attr;
227 attrs[2] = &ocfs2_attr_filecheck_set.attr;
229 memset(&attrgp, 0, sizeof(attrgp));
230 attrgp.attrs = attrs;
233 fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
238 INIT_LIST_HEAD(&fcheck->fc_head);
239 spin_lock_init(&fcheck->fc_lock);
240 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
245 if (strlen(sb->s_id) <= 0) {
247 "Cannot get device basename when create filecheck sysfs\n");
252 device_kset = kset_create_and_add(sb->s_id, NULL, &ocfs2_kset->kobj);
258 fcheck_kset = kset_create_and_add("filecheck", NULL,
265 ret = sysfs_create_group(&fcheck_kset->kobj, &attrgp);
269 entry = kmalloc(sizeof(struct ocfs2_filecheck_sysfs_entry), GFP_NOFS);
274 atomic_set(&entry->fs_count, 1);
276 entry->fs_devicekset = device_kset;
277 entry->fs_fcheckkset = fcheck_kset;
278 entry->fs_fcheck = fcheck;
279 ocfs2_filecheck_sysfs_add(entry);
289 kset_unregister(fcheck_kset);
290 kset_unregister(device_kset);
294 int ocfs2_filecheck_remove_sysfs(struct super_block *sb)
296 return ocfs2_filecheck_sysfs_del(sb->s_id);
300 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
303 ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
308 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
311 spin_lock(&ent->fs_fcheck->fc_lock);
312 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
314 "Cannot set online file check maximum entry number "
315 "to %u due to too many pending entries(%u)\n",
316 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
319 if (len < ent->fs_fcheck->fc_size)
320 BUG_ON(!ocfs2_filecheck_erase_entries(ent,
321 ent->fs_fcheck->fc_size - len));
323 ent->fs_fcheck->fc_max = len;
326 spin_unlock(&ent->fs_fcheck->fc_lock);
331 #define OCFS2_FILECHECK_ARGS_LEN 24
333 ocfs2_filecheck_args_get_long(const char *buf, size_t count,
336 char buffer[OCFS2_FILECHECK_ARGS_LEN];
338 memcpy(buffer, buf, count);
339 buffer[count] = '\0';
341 if (kstrtoul(buffer, 0, val))
348 ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
350 if (!strncmp(name, "fix", 4))
351 *type = OCFS2_FILECHECK_TYPE_FIX;
352 else if (!strncmp(name, "check", 6))
353 *type = OCFS2_FILECHECK_TYPE_CHK;
354 else if (!strncmp(name, "set", 4))
355 *type = OCFS2_FILECHECK_TYPE_SET;
363 ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
364 struct ocfs2_filecheck_args *args)
366 unsigned long val = 0;
369 /* too short/long args length */
370 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
373 if (ocfs2_filecheck_type_parse(name, &type))
375 if (ocfs2_filecheck_args_get_long(buf, count, &val))
381 args->fa_type = type;
382 if (type == OCFS2_FILECHECK_TYPE_SET)
383 args->fa_len = (unsigned int)val;
390 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
391 struct kobj_attribute *attr,
395 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
397 struct ocfs2_filecheck_entry *p;
398 struct ocfs2_filecheck_sysfs_entry *ent;
400 if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
403 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
406 "Cannot get the corresponding entry via device basename %s\n",
411 if (type == OCFS2_FILECHECK_TYPE_SET) {
412 spin_lock(&ent->fs_fcheck->fc_lock);
413 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
414 spin_unlock(&ent->fs_fcheck->fc_lock);
418 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
421 spin_lock(&ent->fs_fcheck->fc_lock);
422 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
423 if (p->fe_type != type)
426 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
427 p->fe_ino, p->fe_done,
428 ocfs2_filecheck_error(p->fe_status));
434 /* snprintf() didn't fit */
441 spin_unlock(&ent->fs_fcheck->fc_lock);
444 ocfs2_filecheck_sysfs_put(ent);
449 ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
451 struct ocfs2_filecheck_entry *p;
453 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
455 list_del(&p->fe_list);
457 ent->fs_fcheck->fc_size--;
458 ent->fs_fcheck->fc_done--;
467 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
471 unsigned int ret = 0;
473 while (i++ < count) {
474 if (ocfs2_filecheck_erase_entry(ent))
480 return (ret == count ? 1 : 0);
484 ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
485 struct ocfs2_filecheck_entry *entry)
488 spin_lock(&ent->fs_fcheck->fc_lock);
489 ent->fs_fcheck->fc_done++;
490 spin_unlock(&ent->fs_fcheck->fc_lock);
494 ocfs2_filecheck_handle(struct super_block *sb,
495 unsigned long ino, unsigned int flags)
497 unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
498 struct inode *inode = NULL;
501 inode = ocfs2_iget(OCFS2_SB(sb), ino, flags, 0);
503 rc = (int)(-(long)inode);
504 if (rc >= OCFS2_FILECHECK_ERR_START &&
505 rc < OCFS2_FILECHECK_ERR_END)
508 ret = OCFS2_FILECHECK_ERR_FAILED;
516 ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
517 struct ocfs2_filecheck_entry *entry)
519 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
520 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
521 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
522 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
523 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
524 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
526 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
528 ocfs2_filecheck_done_entry(ent, entry);
531 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
532 struct kobj_attribute *attr,
533 const char *buf, size_t count)
535 struct ocfs2_filecheck_args args;
536 struct ocfs2_filecheck_entry *entry;
537 struct ocfs2_filecheck_sysfs_entry *ent;
543 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) {
544 mlog(ML_ERROR, "Invalid arguments for online file check\n");
548 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
551 "Cannot get the corresponding entry via device basename %s\n",
556 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
557 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
561 entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
567 spin_lock(&ent->fs_fcheck->fc_lock);
568 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
569 (ent->fs_fcheck->fc_done == 0)) {
571 "Cannot do more file check "
572 "since file check queue(%u) is full now\n",
573 ent->fs_fcheck->fc_max);
577 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
578 (ent->fs_fcheck->fc_done > 0)) {
579 /* Delete the oldest entry which was done,
580 * make sure the entry size in list does
581 * not exceed maximum value
583 BUG_ON(!ocfs2_filecheck_erase_entry(ent));
586 entry->fe_ino = args.fa_ino;
587 entry->fe_type = args.fa_type;
589 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
590 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
591 ent->fs_fcheck->fc_size++;
593 spin_unlock(&ent->fs_fcheck->fc_lock);
596 ocfs2_filecheck_handle_entry(ent, entry);
599 ocfs2_filecheck_sysfs_put(ent);
600 return (!ret ? count : ret);