Linux Audio

Check our new training course

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