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) 2015 Novell. 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[] = {
55 static DEFINE_SPINLOCK(ocfs2_filecheck_sysfs_lock);
56 static LIST_HEAD(ocfs2_filecheck_sysfs_list);
58 struct ocfs2_filecheck {
59 struct list_head fc_head; /* File check entry list head */
61 unsigned int fc_max; /* Maximum number of entry in list */
62 unsigned int fc_size; /* Current entry count in list */
63 unsigned int fc_done; /* Finished entry count in list */
66 struct ocfs2_filecheck_sysfs_entry { /* sysfs entry per mounting */
67 struct list_head fs_list;
69 struct super_block *fs_sb;
70 struct kset *fs_devicekset;
71 struct kset *fs_fcheckkset;
72 struct ocfs2_filecheck *fs_fcheck;
75 #define OCFS2_FILECHECK_MAXSIZE 100
76 #define OCFS2_FILECHECK_MINSIZE 10
78 /* File check operation type */
80 OCFS2_FILECHECK_TYPE_CHK = 0, /* Check a file(inode) */
81 OCFS2_FILECHECK_TYPE_FIX, /* Fix a file(inode) */
82 OCFS2_FILECHECK_TYPE_SET = 100 /* Set entry list maximum size */
85 struct ocfs2_filecheck_entry {
86 struct list_head fe_list;
89 unsigned short fe_done:1;
90 unsigned short fe_status:15;
93 struct ocfs2_filecheck_args {
102 ocfs2_filecheck_error(int errno)
105 return ocfs2_filecheck_errs[errno];
107 BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
108 errno > OCFS2_FILECHECK_ERR_END);
109 return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
112 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
113 struct kobj_attribute *attr,
115 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
116 struct kobj_attribute *attr,
117 const char *buf, size_t count);
118 static struct kobj_attribute ocfs2_attr_filecheck_chk =
119 __ATTR(check, S_IRUSR | S_IWUSR,
120 ocfs2_filecheck_show,
121 ocfs2_filecheck_store);
122 static struct kobj_attribute ocfs2_attr_filecheck_fix =
123 __ATTR(fix, S_IRUSR | S_IWUSR,
124 ocfs2_filecheck_show,
125 ocfs2_filecheck_store);
126 static struct kobj_attribute ocfs2_attr_filecheck_set =
127 __ATTR(set, S_IRUSR | S_IWUSR,
128 ocfs2_filecheck_show,
129 ocfs2_filecheck_store);
131 static int ocfs2_filecheck_sysfs_wait(atomic_t *p)
138 ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
140 struct ocfs2_filecheck_entry *p;
142 if (!atomic_dec_and_test(&entry->fs_count))
143 wait_on_atomic_t(&entry->fs_count, ocfs2_filecheck_sysfs_wait,
144 TASK_UNINTERRUPTIBLE);
146 spin_lock(&entry->fs_fcheck->fc_lock);
147 while (!list_empty(&entry->fs_fcheck->fc_head)) {
148 p = list_first_entry(&entry->fs_fcheck->fc_head,
149 struct ocfs2_filecheck_entry, fe_list);
150 list_del(&p->fe_list);
151 BUG_ON(!p->fe_done); /* To free a undone file check entry */
154 spin_unlock(&entry->fs_fcheck->fc_lock);
156 kset_unregister(entry->fs_fcheckkset);
157 kset_unregister(entry->fs_devicekset);
158 kfree(entry->fs_fcheck);
163 ocfs2_filecheck_sysfs_add(struct ocfs2_filecheck_sysfs_entry *entry)
165 spin_lock(&ocfs2_filecheck_sysfs_lock);
166 list_add_tail(&entry->fs_list, &ocfs2_filecheck_sysfs_list);
167 spin_unlock(&ocfs2_filecheck_sysfs_lock);
170 static int ocfs2_filecheck_sysfs_del(const char *devname)
172 struct ocfs2_filecheck_sysfs_entry *p;
174 spin_lock(&ocfs2_filecheck_sysfs_lock);
175 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
176 if (!strcmp(p->fs_sb->s_id, devname)) {
177 list_del(&p->fs_list);
178 spin_unlock(&ocfs2_filecheck_sysfs_lock);
179 ocfs2_filecheck_sysfs_free(p);
183 spin_unlock(&ocfs2_filecheck_sysfs_lock);
188 ocfs2_filecheck_sysfs_put(struct ocfs2_filecheck_sysfs_entry *entry)
190 if (atomic_dec_and_test(&entry->fs_count))
191 wake_up_atomic_t(&entry->fs_count);
194 static struct ocfs2_filecheck_sysfs_entry *
195 ocfs2_filecheck_sysfs_get(const char *devname)
197 struct ocfs2_filecheck_sysfs_entry *p = NULL;
199 spin_lock(&ocfs2_filecheck_sysfs_lock);
200 list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
201 if (!strcmp(p->fs_sb->s_id, devname)) {
202 atomic_inc(&p->fs_count);
203 spin_unlock(&ocfs2_filecheck_sysfs_lock);
207 spin_unlock(&ocfs2_filecheck_sysfs_lock);
211 int ocfs2_filecheck_create_sysfs(struct super_block *sb)
214 struct kset *device_kset = NULL;
215 struct kset *fcheck_kset = NULL;
216 struct ocfs2_filecheck *fcheck = NULL;
217 struct ocfs2_filecheck_sysfs_entry *entry = NULL;
218 struct attribute **attrs = NULL;
219 struct attribute_group attrgp;
224 attrs = kmalloc(sizeof(struct attribute *) * 4, GFP_NOFS);
229 attrs[0] = &ocfs2_attr_filecheck_chk.attr;
230 attrs[1] = &ocfs2_attr_filecheck_fix.attr;
231 attrs[2] = &ocfs2_attr_filecheck_set.attr;
233 memset(&attrgp, 0, sizeof(attrgp));
234 attrgp.attrs = attrs;
237 fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
242 INIT_LIST_HEAD(&fcheck->fc_head);
243 spin_lock_init(&fcheck->fc_lock);
244 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
249 if (strlen(sb->s_id) <= 0) {
251 "Cannot get device basename when create filecheck sysfs\n");
256 device_kset = kset_create_and_add(sb->s_id, NULL, &ocfs2_kset->kobj);
262 fcheck_kset = kset_create_and_add("filecheck", NULL,
269 ret = sysfs_create_group(&fcheck_kset->kobj, &attrgp);
273 entry = kmalloc(sizeof(struct ocfs2_filecheck_sysfs_entry), GFP_NOFS);
278 atomic_set(&entry->fs_count, 1);
280 entry->fs_devicekset = device_kset;
281 entry->fs_fcheckkset = fcheck_kset;
282 entry->fs_fcheck = fcheck;
283 ocfs2_filecheck_sysfs_add(entry);
293 kset_unregister(fcheck_kset);
294 kset_unregister(device_kset);
298 int ocfs2_filecheck_remove_sysfs(struct super_block *sb)
300 return ocfs2_filecheck_sysfs_del(sb->s_id);
304 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
307 ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
312 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
315 spin_lock(&ent->fs_fcheck->fc_lock);
316 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
318 "Cannot set online file check maximum entry number "
319 "to %u due to too many pending entries(%u)\n",
320 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
323 if (len < ent->fs_fcheck->fc_size)
324 BUG_ON(!ocfs2_filecheck_erase_entries(ent,
325 ent->fs_fcheck->fc_size - len));
327 ent->fs_fcheck->fc_max = len;
330 spin_unlock(&ent->fs_fcheck->fc_lock);
335 #define OCFS2_FILECHECK_ARGS_LEN 24
337 ocfs2_filecheck_args_get_long(const char *buf, size_t count,
340 char buffer[OCFS2_FILECHECK_ARGS_LEN];
342 memcpy(buffer, buf, count);
343 buffer[count] = '\0';
345 if (kstrtoul(buffer, 0, val))
352 ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
354 if (!strncmp(name, "fix", 4))
355 *type = OCFS2_FILECHECK_TYPE_FIX;
356 else if (!strncmp(name, "check", 6))
357 *type = OCFS2_FILECHECK_TYPE_CHK;
358 else if (!strncmp(name, "set", 4))
359 *type = OCFS2_FILECHECK_TYPE_SET;
367 ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
368 struct ocfs2_filecheck_args *args)
370 unsigned long val = 0;
373 /* too short/long args length */
374 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
377 if (ocfs2_filecheck_type_parse(name, &type))
379 if (ocfs2_filecheck_args_get_long(buf, count, &val))
385 args->fa_type = type;
386 if (type == OCFS2_FILECHECK_TYPE_SET)
387 args->fa_len = (unsigned int)val;
394 static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
395 struct kobj_attribute *attr,
399 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
401 struct ocfs2_filecheck_entry *p;
402 struct ocfs2_filecheck_sysfs_entry *ent;
404 if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
407 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
410 "Cannot get the corresponding entry via device basename %s\n",
415 if (type == OCFS2_FILECHECK_TYPE_SET) {
416 spin_lock(&ent->fs_fcheck->fc_lock);
417 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
418 spin_unlock(&ent->fs_fcheck->fc_lock);
422 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
425 spin_lock(&ent->fs_fcheck->fc_lock);
426 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
427 if (p->fe_type != type)
430 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
431 p->fe_ino, p->fe_done,
432 ocfs2_filecheck_error(p->fe_status));
438 /* snprintf() didn't fit */
445 spin_unlock(&ent->fs_fcheck->fc_lock);
448 ocfs2_filecheck_sysfs_put(ent);
453 ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
455 struct ocfs2_filecheck_entry *p;
457 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
459 list_del(&p->fe_list);
461 ent->fs_fcheck->fc_size--;
462 ent->fs_fcheck->fc_done--;
471 ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
475 unsigned int ret = 0;
477 while (i++ < count) {
478 if (ocfs2_filecheck_erase_entry(ent))
484 return (ret == count ? 1 : 0);
488 ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
489 struct ocfs2_filecheck_entry *entry)
492 spin_lock(&ent->fs_fcheck->fc_lock);
493 ent->fs_fcheck->fc_done++;
494 spin_unlock(&ent->fs_fcheck->fc_lock);
497 static unsigned short
498 ocfs2_filecheck_handle(struct super_block *sb,
499 unsigned long ino, unsigned int flags)
501 unsigned short ret = OCFS2_FILECHECK_ERR_SUCCESS;
502 struct inode *inode = NULL;
505 inode = ocfs2_iget(OCFS2_SB(sb), ino, flags, 0);
507 rc = (int)(-(long)inode);
508 if (rc >= OCFS2_FILECHECK_ERR_START &&
509 rc < OCFS2_FILECHECK_ERR_END)
512 ret = OCFS2_FILECHECK_ERR_FAILED;
520 ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
521 struct ocfs2_filecheck_entry *entry)
523 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
524 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
525 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
526 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
527 entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
528 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
530 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
532 ocfs2_filecheck_done_entry(ent, entry);
535 static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 const char *buf, size_t count)
539 struct ocfs2_filecheck_args args;
540 struct ocfs2_filecheck_entry *entry;
541 struct ocfs2_filecheck_sysfs_entry *ent;
547 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) {
548 mlog(ML_ERROR, "Invalid arguments for online file check\n");
552 ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
555 "Cannot get the corresponding entry via device basename %s\n",
560 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
561 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
565 entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
571 spin_lock(&ent->fs_fcheck->fc_lock);
572 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
573 (ent->fs_fcheck->fc_done == 0)) {
575 "Cannot do more file check "
576 "since file check queue(%u) is full now\n",
577 ent->fs_fcheck->fc_max);
581 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
582 (ent->fs_fcheck->fc_done > 0)) {
583 /* Delete the oldest entry which was done,
584 * make sure the entry size in list does
585 * not exceed maximum value
587 BUG_ON(!ocfs2_filecheck_erase_entry(ent));
590 entry->fe_ino = args.fa_ino;
591 entry->fe_type = args.fa_type;
593 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
594 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
595 ent->fs_fcheck->fc_size++;
597 spin_unlock(&ent->fs_fcheck->fc_lock);
600 ocfs2_filecheck_handle_entry(ent, entry);
603 ocfs2_filecheck_sysfs_put(ent);
604 return (!ret ? count : ret);