blob: 90b8d300c1eea8079b34e56f48743a634d8a5072 [file] [log] [blame]
Thomas Gleixner50acfb22019-05-29 07:18:00 -07001// SPDX-License-Identifier: GPL-2.0-only
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Gang Hea860f6e2016-03-22 14:24:24 -07003 * filecheck.c
4 *
5 * Code which implements online file check.
6 *
7 * Copyright (C) 2016 SuSE. All rights reserved.
Gang Hea860f6e2016-03-22 14:24:24 -07008 */
9
10#include <linux/list.h>
11#include <linux/spinlock.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/kmod.h>
15#include <linux/fs.h>
16#include <linux/kobject.h>
17#include <linux/sysfs.h>
18#include <linux/sysctl.h>
19#include <cluster/masklog.h>
20
21#include "ocfs2.h"
22#include "ocfs2_fs.h"
23#include "stackglue.h"
24#include "inode.h"
25
26#include "filecheck.h"
27
28
29/* File check error strings,
30 * must correspond with error number in header file.
31 */
32static const char * const ocfs2_filecheck_errs[] = {
33 "SUCCESS",
34 "FAILED",
35 "INPROGRESS",
36 "READONLY",
37 "INJBD",
38 "INVALIDINO",
39 "BLOCKECC",
40 "BLOCKNO",
41 "VALIDFLAG",
42 "GENERATION",
43 "UNSUPPORTED"
44};
45
Gang Hea860f6e2016-03-22 14:24:24 -070046struct ocfs2_filecheck_entry {
47 struct list_head fe_list;
48 unsigned long fe_ino;
49 unsigned int fe_type;
50 unsigned int fe_done:1;
51 unsigned int fe_status:31;
52};
53
54struct ocfs2_filecheck_args {
55 unsigned int fa_type;
56 union {
57 unsigned long fa_ino;
58 unsigned int fa_len;
59 };
60};
61
62static const char *
63ocfs2_filecheck_error(int errno)
64{
65 if (!errno)
66 return ocfs2_filecheck_errs[errno];
67
68 BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
69 errno > OCFS2_FILECHECK_ERR_END);
70 return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
71}
72
Gang He5f483c42018-04-05 16:19:29 -070073static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj,
74 struct kobj_attribute *attr,
75 char *buf);
76static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj,
77 struct kobj_attribute *attr,
78 const char *buf, size_t count);
79static struct kobj_attribute ocfs2_filecheck_attr_chk =
Gang Hea860f6e2016-03-22 14:24:24 -070080 __ATTR(check, S_IRUSR | S_IWUSR,
Gang He5f483c42018-04-05 16:19:29 -070081 ocfs2_filecheck_attr_show,
82 ocfs2_filecheck_attr_store);
83static struct kobj_attribute ocfs2_filecheck_attr_fix =
Gang Hea860f6e2016-03-22 14:24:24 -070084 __ATTR(fix, S_IRUSR | S_IWUSR,
Gang He5f483c42018-04-05 16:19:29 -070085 ocfs2_filecheck_attr_show,
86 ocfs2_filecheck_attr_store);
87static struct kobj_attribute ocfs2_filecheck_attr_set =
Gang Hea860f6e2016-03-22 14:24:24 -070088 __ATTR(set, S_IRUSR | S_IWUSR,
Gang He5f483c42018-04-05 16:19:29 -070089 ocfs2_filecheck_attr_show,
90 ocfs2_filecheck_attr_store);
91static struct attribute *ocfs2_filecheck_attrs[] = {
92 &ocfs2_filecheck_attr_chk.attr,
93 &ocfs2_filecheck_attr_fix.attr,
94 &ocfs2_filecheck_attr_set.attr,
95 NULL
96};
97
98static void ocfs2_filecheck_release(struct kobject *kobj)
99{
100 struct ocfs2_filecheck_sysfs_entry *entry = container_of(kobj,
101 struct ocfs2_filecheck_sysfs_entry, fs_kobj);
102
103 complete(&entry->fs_kobj_unregister);
104}
105
106static ssize_t
107ocfs2_filecheck_show(struct kobject *kobj, struct attribute *attr, char *buf)
108{
109 ssize_t ret = -EIO;
110 struct kobj_attribute *kattr = container_of(attr,
111 struct kobj_attribute, attr);
112
113 kobject_get(kobj);
114 if (kattr->show)
115 ret = kattr->show(kobj, kattr, buf);
116 kobject_put(kobj);
117 return ret;
118}
119
120static ssize_t
121ocfs2_filecheck_store(struct kobject *kobj, struct attribute *attr,
122 const char *buf, size_t count)
123{
124 ssize_t ret = -EIO;
125 struct kobj_attribute *kattr = container_of(attr,
126 struct kobj_attribute, attr);
127
128 kobject_get(kobj);
129 if (kattr->store)
130 ret = kattr->store(kobj, kattr, buf, count);
131 kobject_put(kobj);
132 return ret;
133}
134
135static const struct sysfs_ops ocfs2_filecheck_ops = {
136 .show = ocfs2_filecheck_show,
137 .store = ocfs2_filecheck_store,
138};
139
140static struct kobj_type ocfs2_ktype_filecheck = {
141 .default_attrs = ocfs2_filecheck_attrs,
142 .sysfs_ops = &ocfs2_filecheck_ops,
143 .release = ocfs2_filecheck_release,
144};
Gang Hea860f6e2016-03-22 14:24:24 -0700145
Gang Hea860f6e2016-03-22 14:24:24 -0700146static void
147ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
148{
149 struct ocfs2_filecheck_entry *p;
150
Gang Hea860f6e2016-03-22 14:24:24 -0700151 spin_lock(&entry->fs_fcheck->fc_lock);
152 while (!list_empty(&entry->fs_fcheck->fc_head)) {
153 p = list_first_entry(&entry->fs_fcheck->fc_head,
154 struct ocfs2_filecheck_entry, fe_list);
155 list_del(&p->fe_list);
156 BUG_ON(!p->fe_done); /* To free a undone file check entry */
157 kfree(p);
158 }
159 spin_unlock(&entry->fs_fcheck->fc_lock);
160
Gang Hea860f6e2016-03-22 14:24:24 -0700161 kfree(entry->fs_fcheck);
Gang He5f483c42018-04-05 16:19:29 -0700162 entry->fs_fcheck = NULL;
Gang Hea860f6e2016-03-22 14:24:24 -0700163}
164
Gang He5f483c42018-04-05 16:19:29 -0700165int ocfs2_filecheck_create_sysfs(struct ocfs2_super *osb)
Gang Hea860f6e2016-03-22 14:24:24 -0700166{
Gang He5f483c42018-04-05 16:19:29 -0700167 int ret;
168 struct ocfs2_filecheck *fcheck;
169 struct ocfs2_filecheck_sysfs_entry *entry = &osb->osb_fc_ent;
Gang Hea860f6e2016-03-22 14:24:24 -0700170
171 fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
Gang He5f483c42018-04-05 16:19:29 -0700172 if (!fcheck)
173 return -ENOMEM;
174
175 INIT_LIST_HEAD(&fcheck->fc_head);
176 spin_lock_init(&fcheck->fc_lock);
177 fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
178 fcheck->fc_size = 0;
179 fcheck->fc_done = 0;
180
181 entry->fs_kobj.kset = osb->osb_dev_kset;
182 init_completion(&entry->fs_kobj_unregister);
183 ret = kobject_init_and_add(&entry->fs_kobj, &ocfs2_ktype_filecheck,
184 NULL, "filecheck");
185 if (ret) {
Tobin C. Hardingb9fba672019-05-31 22:30:29 -0700186 kobject_put(&entry->fs_kobj);
Gang He5f483c42018-04-05 16:19:29 -0700187 kfree(fcheck);
188 return ret;
Gang Hea860f6e2016-03-22 14:24:24 -0700189 }
190
Gang He5f483c42018-04-05 16:19:29 -0700191 entry->fs_fcheck = fcheck;
Gang Hea860f6e2016-03-22 14:24:24 -0700192 return 0;
Gang Hea860f6e2016-03-22 14:24:24 -0700193}
194
Gang He5f483c42018-04-05 16:19:29 -0700195void ocfs2_filecheck_remove_sysfs(struct ocfs2_super *osb)
Gang Hea860f6e2016-03-22 14:24:24 -0700196{
Gang He5f483c42018-04-05 16:19:29 -0700197 if (!osb->osb_fc_ent.fs_fcheck)
198 return;
199
200 kobject_del(&osb->osb_fc_ent.fs_kobj);
201 kobject_put(&osb->osb_fc_ent.fs_kobj);
202 wait_for_completion(&osb->osb_fc_ent.fs_kobj_unregister);
203 ocfs2_filecheck_sysfs_free(&osb->osb_fc_ent);
Gang Hea860f6e2016-03-22 14:24:24 -0700204}
205
206static int
207ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
208 unsigned int count);
209static int
210ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
211 unsigned int len)
212{
213 int ret;
214
215 if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
216 return -EINVAL;
217
218 spin_lock(&ent->fs_fcheck->fc_lock);
219 if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
Gang He8fc2cb4b2018-04-05 16:19:25 -0700220 mlog(ML_NOTICE,
Gang Hea860f6e2016-03-22 14:24:24 -0700221 "Cannot set online file check maximum entry number "
222 "to %u due to too many pending entries(%u)\n",
223 len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
224 ret = -EBUSY;
225 } else {
226 if (len < ent->fs_fcheck->fc_size)
227 BUG_ON(!ocfs2_filecheck_erase_entries(ent,
228 ent->fs_fcheck->fc_size - len));
229
230 ent->fs_fcheck->fc_max = len;
231 ret = 0;
232 }
233 spin_unlock(&ent->fs_fcheck->fc_lock);
234
235 return ret;
236}
237
238#define OCFS2_FILECHECK_ARGS_LEN 24
239static int
240ocfs2_filecheck_args_get_long(const char *buf, size_t count,
241 unsigned long *val)
242{
243 char buffer[OCFS2_FILECHECK_ARGS_LEN];
244
245 memcpy(buffer, buf, count);
246 buffer[count] = '\0';
247
248 if (kstrtoul(buffer, 0, val))
249 return 1;
250
251 return 0;
252}
253
254static int
255ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
256{
257 if (!strncmp(name, "fix", 4))
258 *type = OCFS2_FILECHECK_TYPE_FIX;
259 else if (!strncmp(name, "check", 6))
260 *type = OCFS2_FILECHECK_TYPE_CHK;
261 else if (!strncmp(name, "set", 4))
262 *type = OCFS2_FILECHECK_TYPE_SET;
263 else
264 return 1;
265
266 return 0;
267}
268
269static int
270ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
271 struct ocfs2_filecheck_args *args)
272{
273 unsigned long val = 0;
274 unsigned int type;
275
276 /* too short/long args length */
277 if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
278 return 1;
279
280 if (ocfs2_filecheck_type_parse(name, &type))
281 return 1;
282 if (ocfs2_filecheck_args_get_long(buf, count, &val))
283 return 1;
284
285 if (val <= 0)
286 return 1;
287
288 args->fa_type = type;
289 if (type == OCFS2_FILECHECK_TYPE_SET)
290 args->fa_len = (unsigned int)val;
291 else
292 args->fa_ino = val;
293
294 return 0;
295}
296
Gang He5f483c42018-04-05 16:19:29 -0700297static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj,
Gang Hea860f6e2016-03-22 14:24:24 -0700298 struct kobj_attribute *attr,
299 char *buf)
300{
301
302 ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
303 unsigned int type;
304 struct ocfs2_filecheck_entry *p;
Gang He5f483c42018-04-05 16:19:29 -0700305 struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj,
306 struct ocfs2_filecheck_sysfs_entry, fs_kobj);
Gang Hea860f6e2016-03-22 14:24:24 -0700307
308 if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
309 return -EINVAL;
310
Gang Hea860f6e2016-03-22 14:24:24 -0700311 if (type == OCFS2_FILECHECK_TYPE_SET) {
312 spin_lock(&ent->fs_fcheck->fc_lock);
313 total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
314 spin_unlock(&ent->fs_fcheck->fc_lock);
315 goto exit;
316 }
317
318 ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
319 total += ret;
320 remain -= ret;
321 spin_lock(&ent->fs_fcheck->fc_lock);
322 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
323 if (p->fe_type != type)
324 continue;
325
326 ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
327 p->fe_ino, p->fe_done,
328 ocfs2_filecheck_error(p->fe_status));
329 if (ret < 0) {
330 total = ret;
331 break;
332 }
333 if (ret == remain) {
334 /* snprintf() didn't fit */
335 total = -E2BIG;
336 break;
337 }
338 total += ret;
339 remain -= ret;
340 }
341 spin_unlock(&ent->fs_fcheck->fc_lock);
342
343exit:
Gang Hea860f6e2016-03-22 14:24:24 -0700344 return total;
345}
346
Gang He5f483c42018-04-05 16:19:29 -0700347static inline int
Gang He39ec3772018-04-05 16:19:32 -0700348ocfs2_filecheck_is_dup_entry(struct ocfs2_filecheck_sysfs_entry *ent,
349 unsigned long ino)
350{
351 struct ocfs2_filecheck_entry *p;
352
353 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
354 if (!p->fe_done) {
355 if (p->fe_ino == ino)
356 return 1;
357 }
358 }
359
360 return 0;
361}
362
363static inline int
Gang Hea860f6e2016-03-22 14:24:24 -0700364ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
365{
366 struct ocfs2_filecheck_entry *p;
367
368 list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
369 if (p->fe_done) {
370 list_del(&p->fe_list);
371 kfree(p);
372 ent->fs_fcheck->fc_size--;
373 ent->fs_fcheck->fc_done--;
374 return 1;
375 }
376 }
377
378 return 0;
379}
380
381static int
382ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
383 unsigned int count)
384{
385 unsigned int i = 0;
386 unsigned int ret = 0;
387
388 while (i++ < count) {
389 if (ocfs2_filecheck_erase_entry(ent))
390 ret++;
391 else
392 break;
393 }
394
395 return (ret == count ? 1 : 0);
396}
397
398static void
399ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
400 struct ocfs2_filecheck_entry *entry)
401{
Gang Hea860f6e2016-03-22 14:24:24 -0700402 spin_lock(&ent->fs_fcheck->fc_lock);
Gang He8fc2cb4b2018-04-05 16:19:25 -0700403 entry->fe_done = 1;
Gang Hea860f6e2016-03-22 14:24:24 -0700404 ent->fs_fcheck->fc_done++;
405 spin_unlock(&ent->fs_fcheck->fc_lock);
406}
407
408static unsigned int
Gang He5f483c42018-04-05 16:19:29 -0700409ocfs2_filecheck_handle(struct ocfs2_super *osb,
Gang Hea860f6e2016-03-22 14:24:24 -0700410 unsigned long ino, unsigned int flags)
411{
412 unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
413 struct inode *inode = NULL;
414 int rc;
415
Gang He5f483c42018-04-05 16:19:29 -0700416 inode = ocfs2_iget(osb, ino, flags, 0);
Gang Hea860f6e2016-03-22 14:24:24 -0700417 if (IS_ERR(inode)) {
418 rc = (int)(-(long)inode);
419 if (rc >= OCFS2_FILECHECK_ERR_START &&
420 rc < OCFS2_FILECHECK_ERR_END)
421 ret = rc;
422 else
423 ret = OCFS2_FILECHECK_ERR_FAILED;
424 } else
425 iput(inode);
426
427 return ret;
428}
429
430static void
431ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
432 struct ocfs2_filecheck_entry *entry)
433{
Gang He5f483c42018-04-05 16:19:29 -0700434 struct ocfs2_super *osb = container_of(ent, struct ocfs2_super,
435 osb_fc_ent);
436
Gang Hea860f6e2016-03-22 14:24:24 -0700437 if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
Gang He5f483c42018-04-05 16:19:29 -0700438 entry->fe_status = ocfs2_filecheck_handle(osb,
Gang Hea860f6e2016-03-22 14:24:24 -0700439 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
440 else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
Gang He5f483c42018-04-05 16:19:29 -0700441 entry->fe_status = ocfs2_filecheck_handle(osb,
Gang Hea860f6e2016-03-22 14:24:24 -0700442 entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
443 else
444 entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
445
446 ocfs2_filecheck_done_entry(ent, entry);
447}
448
Gang He5f483c42018-04-05 16:19:29 -0700449static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj,
Gang Hea860f6e2016-03-22 14:24:24 -0700450 struct kobj_attribute *attr,
451 const char *buf, size_t count)
452{
Gang He5f483c42018-04-05 16:19:29 -0700453 ssize_t ret = 0;
Gang Hea860f6e2016-03-22 14:24:24 -0700454 struct ocfs2_filecheck_args args;
455 struct ocfs2_filecheck_entry *entry;
Gang He5f483c42018-04-05 16:19:29 -0700456 struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj,
457 struct ocfs2_filecheck_sysfs_entry, fs_kobj);
Gang Hea860f6e2016-03-22 14:24:24 -0700458
459 if (count == 0)
460 return count;
461
Gang He5f483c42018-04-05 16:19:29 -0700462 if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args))
Gang Hea860f6e2016-03-22 14:24:24 -0700463 return -EINVAL;
Gang Hea860f6e2016-03-22 14:24:24 -0700464
465 if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
466 ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
467 goto exit;
468 }
469
470 entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
471 if (!entry) {
472 ret = -ENOMEM;
473 goto exit;
474 }
475
476 spin_lock(&ent->fs_fcheck->fc_lock);
Gang He39ec3772018-04-05 16:19:32 -0700477 if (ocfs2_filecheck_is_dup_entry(ent, args.fa_ino)) {
478 ret = -EEXIST;
479 kfree(entry);
480 } else if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
Gang He5f483c42018-04-05 16:19:29 -0700481 (ent->fs_fcheck->fc_done == 0)) {
Gang He8fc2cb4b2018-04-05 16:19:25 -0700482 mlog(ML_NOTICE,
Gang Hea860f6e2016-03-22 14:24:24 -0700483 "Cannot do more file check "
484 "since file check queue(%u) is full now\n",
485 ent->fs_fcheck->fc_max);
Gang He8fc2cb4b2018-04-05 16:19:25 -0700486 ret = -EAGAIN;
Gang Hea860f6e2016-03-22 14:24:24 -0700487 kfree(entry);
488 } else {
489 if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
490 (ent->fs_fcheck->fc_done > 0)) {
491 /* Delete the oldest entry which was done,
492 * make sure the entry size in list does
493 * not exceed maximum value
494 */
495 BUG_ON(!ocfs2_filecheck_erase_entry(ent));
496 }
497
498 entry->fe_ino = args.fa_ino;
499 entry->fe_type = args.fa_type;
500 entry->fe_done = 0;
501 entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
502 list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
503 ent->fs_fcheck->fc_size++;
504 }
505 spin_unlock(&ent->fs_fcheck->fc_lock);
506
507 if (!ret)
508 ocfs2_filecheck_handle_entry(ent, entry);
509
510exit:
Gang Hea860f6e2016-03-22 14:24:24 -0700511 return (!ret ? count : ret);
512}