Whamcloud - gitweb
LU-8066 obd: embed typ_kobj in obd_type
[fs/lustre-release.git] / lustre / mdd / mdd_lproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_lproc.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lprocfs_status.h>
45 #include <libcfs/libcfs_string.h>
46 #include "mdd_internal.h"
47
48 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
49                          char *buf)
50 {
51         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
52                                               mdd_kobj);
53         struct obd_device *obd = mdd2obd_dev(mdd);
54
55         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
56 }
57 LUSTRE_RO_ATTR(uuid);
58
59 static ssize_t atime_diff_show(struct kobject *kobj, struct attribute *attr,
60                                char *buf)
61 {
62         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
63                                               mdd_kobj);
64
65         return sprintf(buf, "%lld\n", mdd->mdd_atime_diff);
66 }
67
68 static ssize_t atime_diff_store(struct kobject *kobj,
69                                 struct attribute *attr,
70                                 const char *buffer, size_t count)
71 {
72         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
73                                               mdd_kobj);
74         time64_t diff = 0;
75         int rc;
76
77         rc = kstrtoll(buffer, 10, &diff);
78         if (rc)
79                 return rc;
80
81         mdd->mdd_atime_diff = diff;
82         return count;
83 }
84 LUSTRE_RW_ATTR(atime_diff);
85
86 /**** changelogs ****/
87 static int mdd_changelog_mask_seq_show(struct seq_file *m, void *data)
88 {
89         struct mdd_device *mdd = m->private;
90         int i = 0;
91
92         while (i < CL_LAST) {
93                 if (mdd->mdd_cl.mc_mask & (1 << i))
94                         seq_printf(m, "%s ", changelog_type2str(i));
95                 i++;
96         }
97         seq_putc(m, '\n');
98         return 0;
99 }
100
101 static ssize_t
102 mdd_changelog_mask_seq_write(struct file *file, const char __user *buffer,
103                              size_t count, loff_t *off)
104 {
105         struct seq_file *m = file->private_data;
106         struct mdd_device *mdd = m->private;
107         char *kernbuf;
108         int rc;
109         ENTRY;
110
111         if (count >= PAGE_SIZE)
112                 RETURN(-EINVAL);
113         OBD_ALLOC(kernbuf, PAGE_SIZE);
114         if (kernbuf == NULL)
115                 RETURN(-ENOMEM);
116         if (copy_from_user(kernbuf, buffer, count))
117                 GOTO(out, rc = -EFAULT);
118         kernbuf[count] = 0;
119
120         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
121                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
122         if (rc == 0)
123                 rc = count;
124 out:
125         OBD_FREE(kernbuf, PAGE_SIZE);
126         return rc;
127 }
128 LDEBUGFS_SEQ_FOPS(mdd_changelog_mask);
129
130 static int lprocfs_changelog_users_cb(const struct lu_env *env,
131                                       struct llog_handle *llh,
132                                       struct llog_rec_hdr *hdr, void *data)
133 {
134         struct llog_changelog_user_rec *rec;
135         struct seq_file *m = data;
136
137         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
138
139         rec = (struct llog_changelog_user_rec *)hdr;
140
141         seq_printf(m, CHANGELOG_USER_PREFIX"%-3d %llu (%u)\n",
142                    rec->cur_id, rec->cur_endrec, (__u32)get_seconds() -
143                                                  rec->cur_time);
144         return 0;
145 }
146
147 static int mdd_changelog_users_seq_show(struct seq_file *m, void *data)
148 {
149         struct lu_env            env;
150         struct mdd_device       *mdd = m->private;
151         struct llog_ctxt        *ctxt;
152         __u64                    cur;
153         int                      rc;
154
155         ctxt = llog_get_context(mdd2obd_dev(mdd),
156                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
157         if (ctxt == NULL)
158                 return -ENXIO;
159         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
160
161         rc = lu_env_init(&env, LCT_LOCAL);
162         if (rc) {
163                 llog_ctxt_put(ctxt);
164                 return rc;
165         }
166
167         spin_lock(&mdd->mdd_cl.mc_lock);
168         cur = mdd->mdd_cl.mc_index;
169         spin_unlock(&mdd->mdd_cl.mc_lock);
170
171         seq_printf(m, "current index: %llu\n", cur);
172         seq_printf(m, "%-5s %s %s\n", "ID", "index", "(idle seconds)");
173
174         llog_cat_process(&env, ctxt->loc_handle, lprocfs_changelog_users_cb,
175                          m, 0, 0);
176
177         lu_env_fini(&env);
178         llog_ctxt_put(ctxt);
179         return 0;
180 }
181 LDEBUGFS_SEQ_FOPS_RO(mdd_changelog_users);
182
183 static int mdd_changelog_size_ctxt(const struct lu_env *env,
184                                    struct mdd_device *mdd,
185                                    int index, __u64 *val)
186 {
187         struct llog_ctxt        *ctxt;
188
189         ctxt = llog_get_context(mdd2obd_dev(mdd),
190                                 index);
191         if (ctxt == NULL)
192                 return -ENXIO;
193
194         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
195                 CERROR("%s: ChangeLog has wrong flags: rc = %d\n",
196                        ctxt->loc_obd->obd_name, -EINVAL);
197                 llog_ctxt_put(ctxt);
198                 return -EINVAL;
199         }
200
201         *val += llog_cat_size(env, ctxt->loc_handle);
202
203         llog_ctxt_put(ctxt);
204
205         return 0;
206 }
207
208 static ssize_t changelog_size_show(struct kobject *kobj,
209                                    struct attribute *attr,
210                                    char *buf)
211 {
212         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
213                                               mdd_kobj);
214         struct lu_env env;
215         u64 tmp = 0;
216         int rc;
217
218         rc = lu_env_init(&env, LCT_LOCAL);
219         if (rc)
220                 return rc;
221
222         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_ORIG_CTXT, &tmp);
223         if (rc) {
224                 lu_env_fini(&env);
225                 return rc;
226         }
227
228         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_USER_ORIG_CTXT,
229                                      &tmp);
230
231         rc = sprintf(buf, "%llu\n", tmp);
232         lu_env_fini(&env);
233         return rc;
234 }
235 LUSTRE_RO_ATTR(changelog_size);
236
237 static ssize_t changelog_gc_show(struct kobject *kobj,
238                                  struct attribute *attr,
239                                  char *buf)
240 {
241         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
242                                               mdd_kobj);
243
244         return sprintf(buf, "%u\n", mdd->mdd_changelog_gc);
245 }
246
247 static ssize_t changelog_gc_store(struct kobject *kobj,
248                                   struct attribute *attr,
249                                   const char *buffer, size_t count)
250 {
251         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
252                                               mdd_kobj);
253         bool val;
254         int rc;
255
256         rc = kstrtobool(buffer, &val);
257         if (rc)
258                 return rc;
259
260         mdd->mdd_changelog_gc = val;
261
262         return count;
263 }
264 LUSTRE_RW_ATTR(changelog_gc);
265
266 static ssize_t changelog_max_idle_time_show(struct kobject *kobj,
267                                             struct attribute *attr,
268                                             char *buf)
269 {
270         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
271                                               mdd_kobj);
272
273         return sprintf(buf, "%lld\n", mdd->mdd_changelog_max_idle_time);
274 }
275
276 static ssize_t changelog_max_idle_time_store(struct kobject *kobj,
277                                              struct attribute *attr,
278                                              const char *buffer, size_t count)
279 {
280         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
281                                               mdd_kobj);
282         time64_t val;
283         int rc;
284
285         rc = kstrtoll(buffer, 10, &val);
286         if (rc)
287                 return rc;
288
289         /* as it sounds reasonable, do not allow a user to be idle since
290          * more than about 68 years, this will allow to use 32bits
291          * timestamps for comparison
292          */
293         if (val < 1 || val > INT_MAX)
294                 return -ERANGE;
295
296         mdd->mdd_changelog_max_idle_time = val;
297
298         return count;
299 }
300 LUSTRE_RW_ATTR(changelog_max_idle_time);
301
302 static ssize_t changelog_max_idle_indexes_show(struct kobject *kobj,
303                                                struct attribute *attr,
304                                                char *buf)
305 {
306         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
307                                               mdd_kobj);
308
309         return sprintf(buf, "%lu\n", mdd->mdd_changelog_max_idle_indexes);
310 }
311
312 static ssize_t changelog_max_idle_indexes_store(struct kobject *kobj,
313                                                 struct attribute *attr,
314                                                 const char *buffer,
315                                                 size_t count)
316 {
317         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
318                                               mdd_kobj);
319         unsigned long val;
320         int rc;
321
322         LASSERT(mdd != NULL);
323         rc = kstrtoul(buffer, 0, &val);
324         if (rc)
325                 return rc;
326
327         /* XXX may need to limit/check with reasonable elapsed/idle indexes */
328         /* XXX may better allow to specify a % of full ChangeLogs */
329
330         mdd->mdd_changelog_max_idle_indexes = val;
331
332         return count;
333 }
334 LUSTRE_RW_ATTR(changelog_max_idle_indexes);
335
336 static ssize_t changelog_min_gc_interval_show(struct kobject *kobj,
337                                               struct attribute *attr,
338                                               char *buf)
339 {
340         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
341                                               mdd_kobj);
342
343         return sprintf(buf, "%lld\n", mdd->mdd_changelog_min_gc_interval);
344 }
345
346 static ssize_t changelog_min_gc_interval_store(struct kobject *kobj,
347                                                struct attribute *attr,
348                                                const char *buffer,
349                                                size_t count)
350 {
351         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
352                                               mdd_kobj);
353         time64_t val;
354         int rc;
355
356         rc = kstrtoll(buffer, 10, &val);
357         if (rc)
358                 return rc;
359
360         /* XXX may need to limit with reasonable elapsed/interval times */
361         if (val < 1)
362                 return -ERANGE;
363
364         mdd->mdd_changelog_min_gc_interval = val;
365
366         return count;
367 }
368 LUSTRE_RW_ATTR(changelog_min_gc_interval);
369
370 static ssize_t changelog_min_free_cat_entries_show(struct kobject *kobj,
371                                                    struct attribute *attr,
372                                                    char *buf)
373 {
374         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
375                                               mdd_kobj);
376
377         return sprintf(buf, "%u\n", mdd->mdd_changelog_min_free_cat_entries);
378 }
379
380 static ssize_t changelog_min_free_cat_entries_store(struct kobject *kobj,
381                                                     struct attribute *attr,
382                                                     const char *buffer,
383                                                     size_t count)
384 {
385         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
386                                               mdd_kobj);
387         unsigned int val;
388         int rc;
389
390         rc = kstrtouint(buffer, 10, &val);
391         if (rc)
392                 return rc;
393
394         /* XXX may need to limit with more reasonable number of free entries */
395         if (val < 1)
396                 return -ERANGE;
397
398         mdd->mdd_changelog_min_free_cat_entries = val;
399
400         return count;
401 }
402 LUSTRE_RW_ATTR(changelog_min_free_cat_entries);
403
404 static ssize_t changelog_deniednext_show(struct kobject *kobj,
405                                          struct attribute *attr,
406                                          char *buf)
407 {
408         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
409                                               mdd_kobj);
410
411         return sprintf(buf, "%u\n", mdd->mdd_cl.mc_deniednext);
412 }
413
414 static ssize_t changelog_deniednext_store(struct kobject *kobj,
415                                           struct attribute *attr,
416                                           const char *buffer,
417                                           size_t count)
418 {
419         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
420                                               mdd_kobj);
421         unsigned int time = 0;
422         int rc;
423
424         rc = kstrtouint(buffer, 0, &time);
425         if (rc)
426                 return rc;
427
428         mdd->mdd_cl.mc_deniednext = time;
429         return count;
430 }
431 LUSTRE_RW_ATTR(changelog_deniednext);
432
433 static ssize_t sync_perm_show(struct kobject *kobj, struct attribute *attr,
434                               char *buf)
435 {
436         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
437                                               mdd_kobj);
438
439         return sprintf(buf, "%d\n", mdd->mdd_sync_permission);
440 }
441
442 static ssize_t sync_perm_store(struct kobject *kobj, struct attribute *attr,
443                                const char *buffer, size_t count)
444 {
445         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
446                                               mdd_kobj);
447         bool val;
448         int rc;
449
450         rc = kstrtobool(buffer, &val);
451         if (rc)
452                 return rc;
453
454         mdd->mdd_sync_permission = val;
455
456         return count;
457 }
458 LUSTRE_RW_ATTR(sync_perm);
459
460 static ssize_t lfsck_speed_limit_show(struct kobject *kobj,
461                                       struct attribute *attr, char *buf)
462 {
463         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
464                                               mdd_kobj);
465
466         return lfsck_get_speed(buf, mdd->mdd_bottom);
467 }
468
469 static ssize_t lfsck_speed_limit_store(struct kobject *kobj,
470                                        struct attribute *attr,
471                                        const char *buffer, size_t count)
472 {
473         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
474                                               mdd_kobj);
475         unsigned int val;
476         int rc;
477
478         rc = kstrtouint(buffer, 10, &val);
479         if (rc != 0)
480                 return rc;
481
482         rc = lfsck_set_speed(mdd->mdd_bottom, val);
483         return rc != 0 ? rc : count;
484 }
485 LUSTRE_RW_ATTR(lfsck_speed_limit);
486
487 static ssize_t lfsck_async_windows_show(struct kobject *kobj,
488                                         struct attribute *attr, char *buf)
489 {
490         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
491                                               mdd_kobj);
492
493         return lfsck_get_windows(buf, mdd->mdd_bottom);
494 }
495
496 static ssize_t lfsck_async_windows_store(struct kobject *kobj,
497                                          struct attribute *attr,
498                                          const char *buffer, size_t count)
499 {
500         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
501                                               mdd_kobj);
502         unsigned int val;
503         int rc;
504
505         rc = kstrtouint(buffer, 10, &val);
506         if (rc)
507                 return rc;
508
509         rc = lfsck_set_windows(mdd->mdd_bottom, val);
510
511         return rc != 0 ? rc : count;
512 }
513 LUSTRE_RW_ATTR(lfsck_async_windows);
514
515 static int mdd_lfsck_namespace_seq_show(struct seq_file *m, void *data)
516 {
517         struct mdd_device *mdd = m->private;
518
519         LASSERT(mdd != NULL);
520
521         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_NAMESPACE);
522 }
523 LDEBUGFS_SEQ_FOPS_RO(mdd_lfsck_namespace);
524
525 static int mdd_lfsck_layout_seq_show(struct seq_file *m, void *data)
526 {
527         struct mdd_device *mdd = m->private;
528
529         LASSERT(mdd != NULL);
530
531         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_LAYOUT);
532 }
533 LDEBUGFS_SEQ_FOPS_RO(mdd_lfsck_layout);
534
535 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
536         { .name =       "changelog_mask",
537           .fops =       &mdd_changelog_mask_fops        },
538         { .name =       "changelog_users",
539           .fops =       &mdd_changelog_users_fops       },
540         { .name =       "lfsck_namespace",
541           .fops =       &mdd_lfsck_namespace_fops       },
542         { .name =       "lfsck_layout",
543           .fops =       &mdd_lfsck_layout_fops          },
544         { NULL }
545 };
546
547 static struct attribute *mdd_attrs[] = {
548         &lustre_attr_uuid.attr,
549         &lustre_attr_atime_diff.attr,
550         &lustre_attr_changelog_size.attr,
551         &lustre_attr_changelog_gc.attr,
552         &lustre_attr_changelog_max_idle_time.attr,
553         &lustre_attr_changelog_max_idle_indexes.attr,
554         &lustre_attr_changelog_min_gc_interval.attr,
555         &lustre_attr_changelog_min_free_cat_entries.attr,
556         &lustre_attr_changelog_deniednext.attr,
557         &lustre_attr_lfsck_async_windows.attr,
558         &lustre_attr_lfsck_speed_limit.attr,
559         &lustre_attr_sync_perm.attr,
560         NULL,
561 };
562
563 static void mdd_sysfs_release(struct kobject *kobj)
564 {
565         struct mdd_device *mdd = container_of(kobj, struct mdd_device,
566                                               mdd_kobj);
567
568         complete(&mdd->mdd_kobj_unregister);
569 }
570
571 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
572 {
573         struct obd_device *obd = mdd2obd_dev(mdd);
574         struct obd_type *type;
575         int rc;
576
577         ENTRY;
578         /* at the moment there is no linkage between lu_type
579          * and obd_type, so we lookup obd_type this way
580          */
581         type = class_search_type(LUSTRE_MDD_NAME);
582
583         LASSERT(name != NULL);
584         LASSERT(type != NULL);
585         LASSERT(obd  != NULL);
586
587         mdd->mdd_ktype.default_attrs = mdd_attrs;
588         mdd->mdd_ktype.release = mdd_sysfs_release;
589         mdd->mdd_ktype.sysfs_ops = &lustre_sysfs_ops;
590
591         init_completion(&mdd->mdd_kobj_unregister);
592         rc = kobject_init_and_add(&mdd->mdd_kobj, &mdd->mdd_ktype,
593                                   &type->typ_kobj, "%s", name);
594         if (rc)
595                 return rc;
596
597         /* Find the type procroot and add the proc entry for this device */
598         obd->obd_vars = lprocfs_mdd_obd_vars;
599         obd->obd_debugfs_entry = ldebugfs_register(name,
600                                                    type->typ_debugfs_entry,
601                                                    obd->obd_vars, mdd);
602         if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
603                 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
604                                             : -ENOMEM;
605                 CERROR("Error %d setting up debugfs for %s\n",
606                        rc, name);
607                 obd->obd_debugfs_entry = NULL;
608
609                 kobject_put(&mdd->mdd_kobj);
610         }
611
612         RETURN(rc);
613 }
614
615 void mdd_procfs_fini(struct mdd_device *mdd)
616 {
617         struct obd_device *obd = mdd2obd_dev(mdd);
618
619         kobject_put(&mdd->mdd_kobj);
620         wait_for_completion(&mdd->mdd_kobj_unregister);
621
622         if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
623                 ldebugfs_remove(&obd->obd_debugfs_entry);
624 }