1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2011, 2015, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
13 * Author: Mikhail Pershin <tappro@sun.com>
16 #define DEBUG_SUBSYSTEM S_OSD
18 #include <lprocfs_status.h>
20 #include "osd_internal.h"
22 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf)
24 struct brw_stats *bs = &osd->od_brw_stats;
25 int nr_pages = iobuf->dr_npages;
26 int rw = iobuf->dr_rw;
28 if (unlikely(nr_pages == 0))
31 lprocfs_oh_tally_log2_pcpu(&bs->bs_hist[BRW_R_PAGES + rw], nr_pages);
33 lprocfs_oh_tally_pcpu(&bs->bs_hist[BRW_R_DISCONT_PAGES+rw],
35 lprocfs_oh_tally_pcpu(&bs->bs_hist[BRW_R_DISCONT_BLOCKS+rw],
39 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
40 static int symlink_brw_stats;
41 module_param(symlink_brw_stats, int, 0644);
42 MODULE_PARM_DESC(symlink_brw_stats, "create /proc brw_stats symlink");
44 static void osd_symlink_brw_stats(struct osd_device *osd)
53 if (!symlink_brw_stats)
56 OBD_ALLOC(path, PATH_MAX);
60 p = dentry_path_raw(osd->od_dt_dev.dd_debugfs_entry, path, PATH_MAX);
64 root = osd->od_dt_dev.dd_debugfs_entry->d_sb->s_fs_info;
65 len_root = strlen(root);
67 if (len_root > (p - path) || len_root + len_path + 16 > PATH_MAX)
70 strscpy(path, root, len_root);
71 if (p > path + len_root) {
73 while ((*s++ = *p++) != '\0');
76 *(path + len_root + len_path) = '\0';
77 strcat(path, "/brw_stats");
78 lprocfs_add_symlink("brw_stats", osd->od_proc_entry,
79 "/sys/kernel/debug/%s", path);
82 OBD_FREE(path, PATH_MAX);
86 static int osd_stats_init(struct osd_device *osd)
88 char param[MAX_OBD_NAME * 4];
92 scnprintf(param, sizeof(param), "osd-ldiskfs.%s.stats", osd_name(osd));
93 osd->od_stats = ldebugfs_stats_alloc(LPROC_OSD_LAST, param,
94 osd->od_dt_dev.dd_debugfs_entry,
97 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
98 LPROCFS_TYPE_LATENCY, "get_page");
99 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
100 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_REQS,
101 "get_page_failures");
102 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
103 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
105 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
106 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
108 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
109 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
111 #if OSD_THANDLE_STATS
112 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
113 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_USECS,
115 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
116 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_USECS,
118 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
119 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_USECS,
122 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TOO_MANY_CREDITS,
123 LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_REQS,
128 ldebugfs_register_brw_stats(osd->od_dt_dev.dd_debugfs_entry,
131 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
132 osd_symlink_brw_stats(osd);
138 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
141 return sprintf(buf, "ldiskfs\n");
143 LUSTRE_RO_ATTR(fstype);
145 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
148 struct dt_device *dt = container_of(kobj, struct dt_device,
150 struct osd_device *osd = osd_dt_dev(dt);
153 if (unlikely(!osd->od_mnt))
156 return sprintf(buf, "%s\n", osd->od_mntdev);
158 LUSTRE_RO_ATTR(mntdev);
160 static ssize_t read_cache_enable_show(struct kobject *kobj,
161 struct attribute *attr,
164 struct dt_device *dt = container_of(kobj, struct dt_device,
166 struct osd_device *osd = osd_dt_dev(dt);
169 if (unlikely(!osd->od_mnt))
172 return sprintf(buf, "%u\n", osd->od_read_cache);
175 static ssize_t read_cache_enable_store(struct kobject *kobj,
176 struct attribute *attr,
177 const char *buffer, size_t count)
179 struct dt_device *dt = container_of(kobj, struct dt_device,
181 struct osd_device *osd = osd_dt_dev(dt);
186 if (unlikely(!osd->od_mnt))
189 rc = kstrtobool(buffer, &val);
193 osd->od_read_cache = !!val;
196 LUSTRE_RW_ATTR(read_cache_enable);
198 static ssize_t writethrough_cache_enable_show(struct kobject *kobj,
199 struct attribute *attr,
202 struct dt_device *dt = container_of(kobj, struct dt_device,
204 struct osd_device *osd = osd_dt_dev(dt);
207 if (unlikely(!osd->od_mnt))
210 return sprintf(buf, "%u\n", osd->od_writethrough_cache);
213 static ssize_t writethrough_cache_enable_store(struct kobject *kobj,
214 struct attribute *attr,
218 struct dt_device *dt = container_of(kobj, struct dt_device,
220 struct osd_device *osd = osd_dt_dev(dt);
225 if (unlikely(!osd->od_mnt))
228 rc = kstrtobool(buffer, &val);
232 osd->od_writethrough_cache = !!val;
235 LUSTRE_RW_ATTR(writethrough_cache_enable);
237 static ssize_t enable_projid_xattr_show(struct kobject *kobj,
238 struct attribute *attr,
241 struct dt_device *dt = container_of(kobj, struct dt_device,
243 struct osd_device *osd = osd_dt_dev(dt);
246 if (unlikely(!osd->od_mnt))
249 return snprintf(buf, PAGE_SIZE, "%u\n", osd->od_enable_projid_xattr);
252 static ssize_t enable_projid_xattr_store(struct kobject *kobj,
253 struct attribute *attr,
257 struct dt_device *dt = container_of(kobj, struct dt_device,
259 struct osd_device *osd = osd_dt_dev(dt);
264 if (unlikely(!osd->od_mnt))
267 rc = kstrtobool(buffer, &val);
271 osd->od_enable_projid_xattr = !!val;
274 LUSTRE_RW_ATTR(enable_projid_xattr);
276 static ssize_t fallocate_zero_blocks_show(struct kobject *kobj,
277 struct attribute *attr,
280 struct dt_device *dt = container_of(kobj, struct dt_device,
282 struct osd_device *osd = osd_dt_dev(dt);
285 if (unlikely(!osd->od_mnt))
288 return scnprintf(buf, PAGE_SIZE, "%d\n", osd->od_fallocate_zero_blocks);
292 * Set how fallocate() interacts with the backing filesystem:
293 * -1: fallocate is disabled and returns -EOPNOTSUPP
294 * 0: fallocate allocates unwritten extents (like ext4)
295 * 1: fallocate zeroes allocated extents on disk
297 static ssize_t fallocate_zero_blocks_store(struct kobject *kobj,
298 struct attribute *attr,
299 const char *buffer, size_t count)
301 struct dt_device *dt = container_of(kobj, struct dt_device,
303 struct osd_device *osd = osd_dt_dev(dt);
308 if (unlikely(!osd->od_mnt))
311 rc = kstrtol(buffer, 0, &val);
315 if (val < -1 || val > 1)
318 osd->od_fallocate_zero_blocks = val;
321 LUSTRE_RW_ATTR(fallocate_zero_blocks);
323 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
324 const char *buffer, size_t count)
326 struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
327 struct osd_device *osd = osd_dt_dev(dt);
331 if (unlikely(!osd->od_mnt))
334 flush_workqueue(LDISKFS_SB(osd_sb(osd_dt_dev(dt)))->s_misc_wq);
335 rc = dt_sync(NULL, dt);
337 return rc == 0 ? count : rc;
339 LUSTRE_WO_ATTR(force_sync);
341 static ssize_t nonrotational_show(struct kobject *kobj, struct attribute *attr,
344 struct dt_device *dt = container_of(kobj, struct dt_device,
346 struct osd_device *osd = osd_dt_dev(dt);
349 if (unlikely(!osd->od_mnt))
352 return sprintf(buf, "%u\n", osd->od_nonrotational);
355 static ssize_t nonrotational_store(struct kobject *kobj,
356 struct attribute *attr, const char *buffer,
359 struct dt_device *dt = container_of(kobj, struct dt_device,
361 struct osd_device *osd = osd_dt_dev(dt);
366 if (unlikely(!osd->od_mnt))
369 rc = kstrtobool(buffer, &val);
373 osd->od_nonrotational = val;
376 LUSTRE_RW_ATTR(nonrotational);
378 static ssize_t pdo_show(struct kobject *kobj, struct attribute *attr,
381 return sprintf(buf, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
384 static ssize_t pdo_store(struct kobject *kobj, struct attribute *attr,
385 const char *buffer, size_t count)
390 rc = kstrtobool(buffer, &pdo);
400 static ssize_t auto_scrub_show(struct kobject *kobj, struct attribute *attr,
403 struct dt_device *dt = container_of(kobj, struct dt_device,
405 struct osd_device *dev = osd_dt_dev(dt);
408 if (unlikely(!dev->od_mnt))
411 return scnprintf(buf, PAGE_SIZE, "%lld\n",
412 dev->od_scrub.os_scrub.os_auto_scrub_interval);
415 static ssize_t auto_scrub_store(struct kobject *kobj, struct attribute *attr,
416 const char *buffer, size_t count)
418 struct dt_device *dt = container_of(kobj, struct dt_device,
420 struct osd_device *dev = osd_dt_dev(dt);
425 if (unlikely(!dev->od_mnt))
428 rc = kstrtoll(buffer, 0, &val);
432 dev->od_scrub.os_scrub.os_auto_scrub_interval = val;
435 LUSTRE_RW_ATTR(auto_scrub);
437 static ssize_t full_scrub_ratio_show(struct kobject *kobj,
438 struct attribute *attr,
441 struct dt_device *dt = container_of(kobj, struct dt_device,
443 struct osd_device *dev = osd_dt_dev(dt);
446 if (unlikely(!dev->od_mnt))
449 return sprintf(buf, "%llu\n", dev->od_full_scrub_ratio);
452 static ssize_t full_scrub_ratio_store(struct kobject *kobj,
453 struct attribute *attr,
454 const char *buffer, size_t count)
456 struct dt_device *dt = container_of(kobj, struct dt_device,
458 struct osd_device *dev = osd_dt_dev(dt);
463 if (unlikely(!dev->od_mnt))
466 rc = kstrtoll(buffer, 0, &val);
473 dev->od_full_scrub_ratio = val;
476 LUSTRE_RW_ATTR(full_scrub_ratio);
478 static ssize_t full_scrub_threshold_rate_show(struct kobject *kobj,
479 struct attribute *attr,
482 struct dt_device *dt = container_of(kobj, struct dt_device,
484 struct osd_device *dev = osd_dt_dev(dt);
487 if (unlikely(!dev->od_mnt))
490 return sprintf(buf, "%llu (bad OI mappings/minute)\n",
491 dev->od_full_scrub_threshold_rate);
494 static ssize_t full_scrub_threshold_rate_store(struct kobject *kobj,
495 struct attribute *attr,
496 const char *buffer, size_t count)
498 struct dt_device *dt = container_of(kobj, struct dt_device,
500 struct osd_device *dev = osd_dt_dev(dt);
505 if (unlikely(!dev->od_mnt))
508 rc = kstrtoull(buffer, 0, &val);
512 dev->od_full_scrub_threshold_rate = val;
515 LUSTRE_RW_ATTR(full_scrub_threshold_rate);
517 static ssize_t extent_bytes_allocation_show(struct kobject *kobj,
518 struct attribute *attr, char *buf)
520 struct dt_device *dt = container_of(kobj, struct dt_device,
522 struct osd_device *dev = osd_dt_dev(dt);
524 unsigned int min = (unsigned int)(~0), cur;
526 for_each_online_cpu(i) {
527 cur = *per_cpu_ptr(dev->od_extent_bytes_percpu, i);
531 return snprintf(buf, PAGE_SIZE, "%u\n", min);
533 LUSTRE_RO_ATTR(extent_bytes_allocation);
535 static int ldiskfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
537 struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
539 LASSERT(dev != NULL);
540 if (unlikely(dev->od_mnt == NULL))
543 osd_scrub_dump(m, dev);
547 LDEBUGFS_SEQ_FOPS_RO(ldiskfs_osd_oi_scrub);
549 static ssize_t readcache_max_filesize_show(struct kobject *kobj,
550 struct attribute *attr,
553 struct dt_device *dt = container_of(kobj, struct dt_device,
555 struct osd_device *osd = osd_dt_dev(dt);
557 LASSERT(osd != NULL);
558 if (unlikely(osd->od_mnt == NULL))
561 return scnprintf(buf, PAGE_SIZE, "%llu\n",
562 osd->od_readcache_max_filesize);
565 static ssize_t readcache_max_filesize_store(struct kobject *kobj,
566 struct attribute *attr,
567 const char *buffer, size_t count)
569 struct dt_device *dt = container_of(kobj, struct dt_device,
571 struct osd_device *osd = osd_dt_dev(dt);
575 LASSERT(osd != NULL);
576 if (unlikely(osd->od_mnt == NULL))
579 rc = sysfs_memparse(buffer, count, &val, "B");
583 osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
584 OSD_MAX_CACHE_SIZE : val;
587 LUSTRE_RW_ATTR(readcache_max_filesize);
589 static ssize_t readcache_max_io_mb_show(struct kobject *kobj,
590 struct attribute *attr,
593 struct dt_device *dt = container_of(kobj, struct dt_device,
595 struct osd_device *osd = osd_dt_dev(dt);
597 LASSERT(osd != NULL);
598 if (unlikely(osd->od_mnt == NULL))
601 return scnprintf(buf, PAGE_SIZE, "%lu\n",
602 osd->od_readcache_max_iosize >> 20);
605 static ssize_t readcache_max_io_mb_store(struct kobject *kobj,
606 struct attribute *attr,
607 const char *buffer, size_t count)
609 struct dt_device *dt = container_of(kobj, struct dt_device,
611 struct osd_device *osd = osd_dt_dev(dt);
615 LASSERT(osd != NULL);
616 if (unlikely(osd->od_mnt == NULL))
619 rc = sysfs_memparse(buffer, count, &val, "MiB");
623 if (val > PTLRPC_MAX_BRW_SIZE)
625 osd->od_readcache_max_iosize = val;
628 LUSTRE_RW_ATTR(readcache_max_io_mb);
630 static ssize_t writethrough_max_io_mb_show(struct kobject *kobj,
631 struct attribute *attr,
634 struct dt_device *dt = container_of(kobj, struct dt_device,
636 struct osd_device *osd = osd_dt_dev(dt);
638 LASSERT(osd != NULL);
639 if (unlikely(osd->od_mnt == NULL))
642 return scnprintf(buf, PAGE_SIZE, "%lu\n",
643 osd->od_writethrough_max_iosize >> 20);
646 static ssize_t writethrough_max_io_mb_store(struct kobject *kobj,
647 struct attribute *attr,
648 const char *buffer, size_t count)
650 struct dt_device *dt = container_of(kobj, struct dt_device,
652 struct osd_device *osd = osd_dt_dev(dt);
656 LASSERT(osd != NULL);
657 if (unlikely(osd->od_mnt == NULL))
660 rc = sysfs_memparse(buffer, count, &val, "MiB");
664 if (val > PTLRPC_MAX_BRW_SIZE)
666 osd->od_writethrough_max_iosize = val;
669 LUSTRE_RW_ATTR(writethrough_max_io_mb);
671 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
672 static ssize_t index_in_idif_show(struct kobject *kobj, struct attribute *attr,
675 struct dt_device *dt = container_of(kobj, struct dt_device,
677 struct osd_device *dev = osd_dt_dev(dt);
680 if (unlikely(!dev->od_mnt))
683 return sprintf(buf, "%d\n", (int)(dev->od_index_in_idif));
686 static ssize_t index_in_idif_store(struct kobject *kobj,
687 struct attribute *attr,
688 const char *buffer, size_t count)
690 struct dt_device *dt = container_of(kobj, struct dt_device,
692 struct osd_device *dev = osd_dt_dev(dt);
693 struct lu_target *tgt;
699 if (unlikely(!dev->od_mnt))
702 rc = kstrtobool(buffer, &val);
706 if (dev->od_index_in_idif) {
710 LCONSOLE_WARN("%s: OST-index in IDIF has been enabled, "
711 "it cannot be reverted back.\n", osd_name(dev));
718 rc = lu_env_init(&env, LCT_DT_THREAD);
722 tgt = dev->od_dt_dev.dd_lu_dev.ld_site->ls_tgt;
723 tgt->lut_lsd.lsd_feature_rocompat |= OBD_ROCOMPAT_IDX_IN_IDIF;
724 rc = tgt_server_data_update(&env, tgt, 1);
729 LCONSOLE_INFO("%s: enable OST-index in IDIF successfully, "
730 "it cannot be reverted back.\n", osd_name(dev));
732 dev->od_index_in_idif = 1;
735 LUSTRE_RW_ATTR(index_in_idif);
737 int osd_register_proc_index_in_idif(struct osd_device *osd)
739 struct dt_device *dt = &osd->od_dt_dev;
741 return sysfs_create_file(&dt->dd_kobj, &lustre_attr_index_in_idif.attr);
745 static ssize_t index_backup_show(struct kobject *kobj, struct attribute *attr,
748 struct dt_device *dt = container_of(kobj, struct dt_device,
750 struct osd_device *dev = osd_dt_dev(dt);
753 if (unlikely(!dev->od_mnt))
756 return sprintf(buf, "%d\n", dev->od_index_backup_policy);
759 static ssize_t index_backup_store(struct kobject *kobj, struct attribute *attr,
760 const char *buffer, size_t count)
762 struct dt_device *dt = container_of(kobj, struct dt_device,
764 struct osd_device *dev = osd_dt_dev(dt);
769 if (unlikely(!dev->od_mnt))
772 rc = kstrtoint(buffer, 0, &val);
776 dev->od_index_backup_policy = val;
779 LUSTRE_RW_ATTR(index_backup);
781 #ifdef LDISKFS_GET_BLOCKS_VERY_DENSE
782 static ssize_t extents_dense_show(struct kobject *kobj, struct attribute *attr,
785 struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
786 struct osd_device *osd = osd_dt_dev(dt);
788 return snprintf(buf, PAGE_SIZE, "%d\n", osd->od_extents_dense);
791 static ssize_t extents_dense_store(struct kobject *kobj, struct attribute *attr,
792 const char *buffer, size_t count)
794 struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
795 struct osd_device *osd = osd_dt_dev(dt);
799 rc = kstrtobool(buffer, &extents_dense);
803 osd->od_extents_dense = extents_dense;
807 LUSTRE_RW_ATTR(extents_dense);
810 static struct ldebugfs_vars ldebugfs_osd_obd_vars[] = {
811 { .name = "oi_scrub",
812 .fops = &ldiskfs_osd_oi_scrub_fops },
816 static struct attribute *ldiskfs_attrs[] = {
817 &lustre_attr_read_cache_enable.attr,
818 &lustre_attr_writethrough_cache_enable.attr,
819 &lustre_attr_enable_projid_xattr.attr,
820 &lustre_attr_fstype.attr,
821 &lustre_attr_mntdev.attr,
822 &lustre_attr_fallocate_zero_blocks.attr,
823 &lustre_attr_force_sync.attr,
824 &lustre_attr_nonrotational.attr,
825 &lustre_attr_index_backup.attr,
826 &lustre_attr_auto_scrub.attr,
827 &lustre_attr_pdo.attr,
828 &lustre_attr_full_scrub_ratio.attr,
829 &lustre_attr_full_scrub_threshold_rate.attr,
830 &lustre_attr_extent_bytes_allocation.attr,
831 #ifdef LDISKFS_GET_BLOCKS_VERY_DENSE
832 &lustre_attr_extents_dense.attr,
834 &lustre_attr_readcache_max_filesize.attr,
835 &lustre_attr_readcache_max_io_mb.attr,
836 &lustre_attr_writethrough_max_io_mb.attr,
840 KOBJ_ATTRIBUTE_GROUPS(ldiskfs); /* creates ldiskfs_groups from ldiskfs_attrs */
842 int osd_procfs_init(struct osd_device *osd, const char *name)
844 struct obd_type *type;
849 /* at the moment there is no linkage between lu_type
850 * and obd_type, so we lookup obd_type this way
852 type = class_search_type(LUSTRE_OSD_LDISKFS_NAME);
857 CDEBUG(D_CONFIG, "%s: register osd-ldiskfs tunable parameters\n", name);
859 /* put reference taken by class_search_type */
860 kobject_put(&type->typ_kobj);
862 osd->od_dt_dev.dd_ktype.default_groups = KOBJ_ATTR_GROUPS(ldiskfs);
863 rc = dt_tunables_init(&osd->od_dt_dev, type, name,
864 ldebugfs_osd_obd_vars);
866 CERROR("%s: cannot setup sysfs / debugfs entry: %d\n",
871 if (osd->od_proc_entry)
874 /* Find the type procroot and add the proc entry for this device */
875 osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
876 NULL, &osd->od_dt_dev);
877 if (IS_ERR(osd->od_proc_entry)) {
878 rc = PTR_ERR(osd->od_proc_entry);
879 CERROR("Error %d setting up lprocfs for %s\n",
881 osd->od_proc_entry = NULL;
885 rc = osd_stats_init(osd);
890 osd_procfs_fini(osd);
894 void osd_procfs_fini(struct osd_device *osd)
896 lprocfs_fini_brw_stats(&osd->od_brw_stats);
899 lprocfs_stats_free(&osd->od_stats);
901 if (osd->od_proc_entry)
902 lprocfs_remove(&osd->od_proc_entry);
904 dt_tunables_fini(&osd->od_dt_dev);