Whamcloud - gitweb
LU-13922 osd-ldiskfs: no need to add OI cache in readdir
[fs/lustre-release.git] / lustre / ofd / lproc_ofd.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) 2009, 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/ofd/lproc_ofd.c
33  *
34  * This file provides functions of procfs interface for OBD Filter Device (OFD).
35  *
36  * Author: Andreas Dilger <andreas.dilger@intel.com>
37  * Author: Mikhail Pershin <mike.pershin@intel.com>
38  * Author: Johann Lombardi <johann.lombardi@intel.com>
39  * Author: Fan Yong <fan.yong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_CLASS
43
44 #include <obd.h>
45 #include <lprocfs_status.h>
46 #include <linux/seq_file.h>
47 #include <lustre_lfsck.h>
48 #include <uapi/linux/lustre/lustre_access_log.h>
49
50 #include "ofd_internal.h"
51
52 #ifdef CONFIG_PROC_FS
53
54 /**
55  * Show number of FID allocation sequences.
56  *
57  * \param[in] m         seq_file handle
58  * \param[in] data      unused for single entry
59  *
60  * \retval              0 on success
61  * \retval              negative value on error
62  */
63 static ssize_t seqs_allocated_show(struct kobject *kobj, struct attribute *attr,
64                                    char *buf)
65 {
66         struct obd_device *obd = container_of(kobj, struct obd_device,
67                                               obd_kset.kobj);
68         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
69
70         return sprintf(buf, "%u\n", ofd->ofd_seq_count);
71 }
72 LUSTRE_RO_ATTR(seqs_allocated);
73
74 /**
75  * Show total number of grants for precreate.
76  *
77  * \param[in] m         seq_file handle
78  * \param[in] data      unused for single entry
79  *
80  * \retval              0 on success
81  * \retval              negative value on error
82  */
83 static ssize_t grant_precreate_show(struct kobject *kobj,
84                                     struct attribute *attr,
85                                     char *buf)
86 {
87         struct obd_device *obd = container_of(kobj, struct obd_device,
88                                               obd_kset.kobj);
89
90         return sprintf(buf, "%ld\n",
91                        obd->obd_self_export->exp_target_data.ted_grant);
92 }
93 LUSTRE_RO_ATTR(grant_precreate);
94
95 /**
96  * Show number of precreates allowed in a single transaction.
97  *
98  * \param[in] m         seq_file handle
99  * \param[in] data      unused for single entry
100  *
101  * \retval              0 on success
102  * \retval              negative value on error
103  */
104 static ssize_t precreate_batch_show(struct kobject *kobj,
105                                     struct attribute *attr,
106                                     char *buf)
107 {
108         struct obd_device *obd = container_of(kobj, struct obd_device,
109                                               obd_kset.kobj);
110         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
111
112         return sprintf(buf, "%d\n", ofd->ofd_precreate_batch);
113 }
114
115 /**
116  * Change number of precreates allowed in a single transaction.
117  *
118  * \param[in] file      proc file
119  * \param[in] buffer    string which represents maximum number
120  * \param[in] count     \a buffer length
121  * \param[in] off       unused for single entry
122  *
123  * \retval              \a count on success
124  * \retval              negative number on error
125  */
126 static ssize_t precreate_batch_store(struct kobject *kobj,
127                                      struct attribute *attr,
128                                      const char *buffer, size_t count)
129 {
130         struct obd_device *obd = container_of(kobj, struct obd_device,
131                                               obd_kset.kobj);
132         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
133         unsigned int val;
134         int rc;
135
136         rc = kstrtouint(buffer, 0, &val);
137         if (rc)
138                 return rc;
139
140         if (val < 1 || val > 65536)
141                 return -EINVAL;
142
143         spin_lock(&ofd->ofd_batch_lock);
144         ofd->ofd_precreate_batch = val;
145         spin_unlock(&ofd->ofd_batch_lock);
146         return count;
147 }
148 LUSTRE_RW_ATTR(precreate_batch);
149
150 /**
151  * Show number of seconds to delay atime
152  *
153  * \param[in] m         seq_file handle
154  * \param[in] data      unused for single entry
155  *
156  * \retval              0 on success
157  * \retval              negative value on error
158  */
159 static ssize_t atime_diff_show(struct kobject *kobj, struct attribute *attr,
160                                char *buf)
161 {
162         struct obd_device *obd = container_of(kobj, struct obd_device,
163                                               obd_kset.kobj);
164         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
165
166         return snprintf(buf, PAGE_SIZE, "%lld\n", ofd->ofd_atime_diff);
167 }
168
169 /**
170  * Change number of seconds to delay atime
171  *
172  * \param[in] file      proc file
173  * \param[in] buffer    string which represents maximum number
174  * \param[in] count     \a buffer length
175  * \param[in] off       unused for single entry
176  *
177  * \retval              \a count on success
178  * \retval              negative number on error
179  */
180 static ssize_t atime_diff_store(struct kobject *kobj, struct attribute *attr,
181                                 const char *buffer, size_t count)
182 {
183         struct obd_device *obd = container_of(kobj, struct obd_device,
184                                               obd_kset.kobj);
185         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
186         unsigned int val;
187         int rc;
188
189         rc = kstrtouint(buffer, 0, &val);
190         if (rc)
191                 return rc;
192
193         if (val > 86400)
194                 return -EINVAL;
195
196         ofd->ofd_atime_diff = val;
197         return count;
198 }
199 LUSTRE_RW_ATTR(atime_diff);
200
201 /**
202  * Show the last used ID for each FID sequence used by OFD.
203  *
204  * \param[in] m         seq_file handle
205  * \param[in] data      unused for single entry
206  *
207  * \retval              0 on success
208  * \retval              negative value on error
209  */
210 static int ofd_last_id_seq_show(struct seq_file *m, void *data)
211 {
212         struct obd_device       *obd = m->private;
213         struct ofd_device       *ofd;
214         struct ofd_seq          *oseq = NULL;
215
216         if (obd == NULL)
217                 return 0;
218
219         ofd = ofd_dev(obd->obd_lu_dev);
220
221         read_lock(&ofd->ofd_seq_list_lock);
222         list_for_each_entry(oseq, &ofd->ofd_seq_list, os_list) {
223                 __u64 seq;
224
225                 seq = ostid_seq(&oseq->os_oi) == 0 ?
226                       fid_idif_seq(ostid_id(&oseq->os_oi),
227                                 ofd->ofd_lut.lut_lsd.lsd_osd_index) & ~0xFFFF :
228                                 ostid_seq(&oseq->os_oi);
229                 seq_printf(m, DOSTID"\n", seq, ostid_id(&oseq->os_oi));
230         }
231         read_unlock(&ofd->ofd_seq_list_lock);
232         return 0;
233 }
234
235 LPROC_SEQ_FOPS_RO(ofd_last_id);
236
237 /**
238  * Show if the OFD is in degraded mode.
239  *
240  * Degraded means OFD has a failed drive or is undergoing RAID rebuild.
241  * The MDS will try to avoid using this OST for new object allocations
242  * to reduce the impact to global IO performance when clients writing to
243  * this OST are slowed down.  It also reduces the contention on the OST
244  * RAID device, allowing it to rebuild more quickly.
245  *
246  * \retval              count of bytes written
247  */
248 static ssize_t degraded_show(struct kobject *kobj, struct attribute *attr,
249                              char *buf)
250 {
251         struct obd_device *obd = container_of(kobj, struct obd_device,
252                                               obd_kset.kobj);
253         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
254
255         return sprintf(buf, "%u\n", ofd->ofd_raid_degraded);
256 }
257
258 /**
259  * Set OFD to degraded mode.
260  *
261  * This is used to interface to userspace administrative tools for
262  * the underlying RAID storage, so that they can mark an OST
263  * as having degraded performance.
264  *
265  * \param[in] count     \a buffer length
266  * \param[in] off       unused for single entry
267  *
268  * \retval              \a count on success
269  * \retval              negative number on error
270  */
271 static ssize_t degraded_store(struct kobject *kobj, struct attribute *attr,
272                               const char *buffer, size_t count)
273 {
274         struct obd_device *obd = container_of(kobj, struct obd_device,
275                                               obd_kset.kobj);
276         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
277         bool val;
278         int rc;
279
280         rc = kstrtobool(buffer, &val);
281         if (rc)
282                 return rc;
283
284         spin_lock(&ofd->ofd_flags_lock);
285         ofd->ofd_raid_degraded = val;
286         spin_unlock(&ofd->ofd_flags_lock);
287         return count;
288 }
289 LUSTRE_RW_ATTR(degraded);
290
291 /**
292  * Show if the OFD is in no precreate mode.
293  *
294  * This means OFD has been adminstratively disabled at the OST to prevent
295  * the MDS from creating any new files on the OST, though existing files
296  * can still be read, written, and unlinked.
297  *
298  * \retval              number of bytes written
299  */
300 static ssize_t no_precreate_show(struct kobject *kobj, struct attribute *attr,
301                                  char *buf)
302 {
303         struct obd_device *obd = container_of(kobj, struct obd_device,
304                                               obd_kset.kobj);
305         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
306
307         return snprintf(buf, PAGE_SIZE, "%u\n", ofd->ofd_no_precreate);
308 }
309
310 /**
311  * Set OFD to no precreate mode.
312  *
313  * This is used to interface to userspace administrative tools to
314  * disable new object creation on the OST.
315  *
316  * \param[in] count     \a buffer length
317  *
318  * \retval              \a count on success
319  * \retval              negative number on error
320  */
321 static ssize_t no_precreate_store(struct kobject *kobj, struct attribute *attr,
322                                   const char *buffer, size_t count)
323 {
324         struct obd_device *obd = container_of(kobj, struct obd_device,
325                                               obd_kset.kobj);
326         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
327         bool val;
328         int rc;
329
330         rc = kstrtobool(buffer, &val);
331         if (rc)
332                 return rc;
333
334         spin_lock(&ofd->ofd_flags_lock);
335         ofd->ofd_no_precreate = val;
336         spin_unlock(&ofd->ofd_flags_lock);
337
338         return count;
339 }
340 LUSTRE_RW_ATTR(no_precreate);
341
342 /**
343  * Show OFD filesystem type.
344  *
345  * \param[in] m         seq_file handle
346  * \param[in] data      unused for single entry
347  *
348  * \retval              0 on success
349  * \retval              negative value on error
350  */
351 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
352                            char *buf)
353 {
354         struct obd_device *obd = container_of(kobj, struct obd_device,
355                                               obd_kset.kobj);
356         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
357         struct lu_device  *d;
358
359         LASSERT(ofd->ofd_osd);
360         d = &ofd->ofd_osd->dd_lu_dev;
361         LASSERT(d->ld_type);
362         return sprintf(buf, "%s\n", d->ld_type->ldt_name);
363 }
364 LUSTRE_RO_ATTR(fstype);
365
366 /**
367  * Show journal handling mode: synchronous or asynchronous.
368  *
369  * When running in asynchronous mode the journal transactions are not
370  * committed to disk before the RPC is replied back to the client.
371  * This will typically improve client performance when only a small number
372  * of clients are writing, since the client(s) can have more write RPCs
373  * in flight. However, it also means that the client has to handle recovery
374  * on bulk RPCs, and will have to keep more dirty pages in cache before they
375  * are committed on the OST.
376  *
377  * \param[in] m         seq_file handle
378  * \param[in] data      unused for single entry
379  *
380  * \retval              0 on success
381  * \retval              negative value on error
382  */
383 static ssize_t sync_journal_show(struct kobject *kobj, struct attribute *attr,
384                                 char *buf)
385 {
386         struct obd_device *obd = container_of(kobj, struct obd_device,
387                                               obd_kset.kobj);
388         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
389
390         return sprintf(buf, "%u\n", ofd->ofd_sync_journal);
391 }
392
393 /**
394  * Set journal mode to synchronous or asynchronous.
395  *
396  * \param[in] file      proc file
397  * \param[in] buffer    string which represents mode
398  *                      1: synchronous mode
399  *                      0: asynchronous mode
400  * \param[in] count     \a buffer length
401  * \param[in] off       unused for single entry
402  *
403  * \retval              \a count on success
404  * \retval              negative number on error
405  */
406 static ssize_t sync_journal_store(struct kobject *kobj, struct attribute *attr,
407                                  const char *buffer, size_t count)
408 {
409         struct obd_device *obd = container_of(kobj, struct obd_device,
410                                               obd_kset.kobj);
411         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
412         bool val;
413         int rc;
414
415         rc = kstrtobool(buffer, &val);
416         if (rc)
417                 return rc;
418
419         spin_lock(&ofd->ofd_flags_lock);
420         ofd->ofd_sync_journal = val;
421         ofd_slc_set(ofd);
422         spin_unlock(&ofd->ofd_flags_lock);
423
424         return count;
425 }
426 LUSTRE_RW_ATTR(sync_journal);
427
428 static int ofd_brw_size_seq_show(struct seq_file *m, void *data)
429 {
430         struct obd_device       *obd = m->private;
431         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
432
433         seq_printf(m, "%u\n", ofd->ofd_brw_size / ONE_MB_BRW_SIZE);
434         return 0;
435 }
436
437 static ssize_t
438 ofd_brw_size_seq_write(struct file *file, const char __user *buffer,
439                        size_t count, loff_t *off)
440 {
441         struct seq_file *m = file->private_data;
442         struct obd_device *obd = m->private;
443         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
444         char kernbuf[22] = "";
445         u64 val;
446         int rc;
447
448         if (count >= sizeof(kernbuf))
449                 return -EINVAL;
450
451         if (copy_from_user(kernbuf, buffer, count))
452                 return -EFAULT;
453         kernbuf[count] = 0;
454
455         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
456         if (rc < 0)
457                 return rc;
458
459         if (val == 0)
460                 return -EINVAL;
461
462         if (val > DT_MAX_BRW_SIZE ||
463             val < (1 << ofd->ofd_lut.lut_tgd.tgd_blockbits))
464                 return -ERANGE;
465
466         spin_lock(&ofd->ofd_flags_lock);
467         ofd->ofd_brw_size = val;
468         spin_unlock(&ofd->ofd_flags_lock);
469
470         return count;
471 }
472 LPROC_SEQ_FOPS(ofd_brw_size);
473
474 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
475 static bool sync_on_lock_cancel_warned;
476 static ssize_t sync_on_lock_cancel_show(struct kobject *kobj,
477                                         struct attribute *attr, char *buf)
478 {
479         if (!sync_on_lock_cancel_warned) {
480                 sync_on_lock_cancel_warned = true;
481                 pr_info("ofd: 'obdfilter.*.sync_on_lock_cancel' is deprecated, use 'obdfilter.*.sync_lock_cancel' instead\n");
482         }
483         return sync_lock_cancel_show(kobj, attr, buf);
484 }
485
486 static ssize_t sync_on_lock_cancel_store(struct kobject *kobj,
487                                          struct attribute *attr,
488                                          const char *buffer, size_t count)
489 {
490         if (!sync_on_lock_cancel_warned) {
491                 sync_on_lock_cancel_warned = true;
492                 pr_info("ofd: 'obdfilter.*.sync_on_lock_cancel' is deprecated, use 'obdfilter.*.sync_lock_cancel' instead\n");
493         }
494         return sync_lock_cancel_store(kobj, attr, buffer, count);
495 }
496 LUSTRE_RW_ATTR(sync_on_lock_cancel);
497 #endif
498
499 /**
500  * Show the limit of soft sync RPCs.
501  *
502  * This value defines how many IO RPCs with OBD_BRW_SOFT_SYNC flag
503  * are allowed before sync update will be triggered.
504  *
505  * \param[in] m         seq_file handle
506  * \param[in] data      unused for single entry
507  *
508  * \retval              0 on success
509  * \retval              negative value on error
510  */
511 static ssize_t soft_sync_limit_show(struct kobject *kobj,
512                                     struct attribute *attr, char *buf)
513 {
514         struct obd_device *obd = container_of(kobj, struct obd_device,
515                                               obd_kset.kobj);
516         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
517
518         return sprintf(buf, "%u\n", ofd->ofd_soft_sync_limit);
519 }
520
521 /**
522  * Change the limit of soft sync RPCs.
523  *
524  * Define how many IO RPCs with OBD_BRW_SOFT_SYNC flag
525  * allowed before sync update will be done.
526  *
527  * This limit is global across all exports.
528  *
529  * \param[in] file      proc file
530  * \param[in] buffer    string which represents limit
531  * \param[in] count     \a buffer length
532  * \param[in] off       unused for single entry
533  *
534  * \retval              \a count on success
535  * \retval              negative number on error
536  */
537 static ssize_t soft_sync_limit_store(struct kobject *kobj,
538                                      struct attribute *attr,
539                                      const char *buffer, size_t count)
540 {
541         struct obd_device *obd = container_of(kobj, struct obd_device,
542                                               obd_kset.kobj);
543         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
544         unsigned int val;
545         int rc;
546
547         rc = kstrtouint(buffer, 0, &val);
548         if (rc < 0)
549                 return rc;
550
551         ofd->ofd_soft_sync_limit = val;
552         return 0;
553 }
554 LUSTRE_RW_ATTR(soft_sync_limit);
555
556 /**
557  * Show the LFSCK speed limit.
558  *
559  * The maximum number of items scanned per second.
560  *
561  * \param[in] m         seq_file handle
562  * \param[in] data      unused for single entry
563  *
564  * \retval              0 on success
565  * \retval              negative value on error
566  */
567 static ssize_t lfsck_speed_limit_show(struct kobject *kobj,
568                                       struct attribute *attr, char *buf)
569 {
570         struct obd_device *obd = container_of(kobj, struct obd_device,
571                                               obd_kset.kobj);
572         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
573
574         return lfsck_get_speed(buf, ofd->ofd_osd);
575 }
576
577 /**
578  * Change the LFSCK speed limit.
579  *
580  * Limit number of items that may be scanned per second.
581  *
582  * \param[in] file      proc file
583  * \param[in] buffer    string which represents limit
584  * \param[in] count     \a buffer length
585  * \param[in] off       unused for single entry
586  *
587  * \retval              \a count on success
588  * \retval              negative number on error
589  */
590 static ssize_t lfsck_speed_limit_store(struct kobject *kobj,
591                                        struct attribute *attr,
592                                        const char *buffer, size_t count)
593 {
594         struct obd_device *obd = container_of(kobj, struct obd_device,
595                                               obd_kset.kobj);
596         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
597         unsigned int val;
598         int rc;
599
600         rc = kstrtouint(buffer, 0, &val);
601         if (rc != 0)
602                 return rc;
603
604         rc = lfsck_set_speed(ofd->ofd_osd, val);
605
606         return rc != 0 ? rc : count;
607 }
608 LUSTRE_RW_ATTR(lfsck_speed_limit);
609
610 /**
611  * Show LFSCK layout verification stats from the most recent LFSCK run.
612  *
613  * \param[in] m         seq_file handle
614  * \param[in] data      unused for single entry
615  *
616  * \retval              0 on success
617  * \retval              negative value on error
618  */
619 static int ofd_lfsck_layout_seq_show(struct seq_file *m, void *data)
620 {
621         struct obd_device *obd = m->private;
622         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
623
624         return lfsck_dump(m, ofd->ofd_osd, LFSCK_TYPE_LAYOUT);
625 }
626
627 LPROC_SEQ_FOPS_RO(ofd_lfsck_layout);
628
629 /**
630  * Show if LFSCK performed parent FID verification.
631  *
632  * \param[in] m         seq_file handle
633  * \param[in] data      unused for single entry
634  *
635  * \retval              0 on success
636  * \retval              negative value on error
637  */
638 static int ofd_lfsck_verify_pfid_seq_show(struct seq_file *m, void *data)
639 {
640         struct obd_device *obd = m->private;
641         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
642
643         seq_printf(m, "switch: %s\ndetected: %llu\nrepaired: %llu\n",
644                    ofd->ofd_lfsck_verify_pfid ? "on" : "off",
645                    ofd->ofd_inconsistency_self_detected,
646                    ofd->ofd_inconsistency_self_repaired);
647         return 0;
648 }
649
650 /**
651  * Set the LFSCK behavior to verify parent FID correctness.
652  *
653  * If flag ofd_lfsck_verify_pfid is set then LFSCK does parent FID
654  * verification during read/write operations.
655  *
656  * \param[in] file      proc file
657  * \param[in] buffer    string which represents behavior
658  *                      1: verify parent FID
659  *                      0: don't verify parent FID
660  * \param[in] count     \a buffer length
661  * \param[in] off       unused for single entry
662  *
663  * \retval              \a count on success
664  * \retval              negative number on error
665  */
666 static ssize_t
667 ofd_lfsck_verify_pfid_seq_write(struct file *file, const char __user *buffer,
668                                 size_t count, loff_t *off)
669 {
670         struct seq_file *m = file->private_data;
671         struct obd_device *obd = m->private;
672         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
673         bool val;
674         int rc;
675
676         rc = kstrtobool_from_user(buffer, count, &val);
677         if (rc)
678                 return rc;
679
680         ofd->ofd_lfsck_verify_pfid = val;
681         if (!ofd->ofd_lfsck_verify_pfid) {
682                 ofd->ofd_inconsistency_self_detected = 0;
683                 ofd->ofd_inconsistency_self_repaired = 0;
684         }
685
686         return count;
687 }
688
689 LPROC_SEQ_FOPS(ofd_lfsck_verify_pfid);
690
691 static ssize_t access_log_mask_show(struct kobject *kobj,
692                         struct attribute *attr, char *buf)
693 {
694         struct obd_device *obd = container_of(kobj, struct obd_device,
695                                               obd_kset.kobj);
696         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
697
698         return snprintf(buf, PAGE_SIZE, "%s%s%s\n",
699                 (ofd->ofd_access_log_mask == 0) ? "0" : "",
700                 (ofd->ofd_access_log_mask & OFD_ACCESS_READ) ? "r" : "",
701                 (ofd->ofd_access_log_mask & OFD_ACCESS_WRITE) ? "w" : "");
702 }
703
704 static ssize_t access_log_mask_store(struct kobject *kobj,
705                                      struct attribute *attr,
706                                      const char *buffer, size_t count)
707 {
708         struct obd_device *obd = container_of(kobj, struct obd_device,
709                                               obd_kset.kobj);
710         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
711         unsigned int mask = 0;
712         size_t i;
713
714         for (i = 0; i < count; i++) {
715                 switch (tolower(buffer[i])) {
716                 case '0':
717                         break;
718                 case 'r':
719                         mask |= OFD_ACCESS_READ;
720                         break;
721                 case 'w':
722                         mask |= OFD_ACCESS_WRITE;
723                         break;
724                 default:
725                         return -EINVAL;
726                 }
727         }
728
729         ofd->ofd_access_log_mask = mask;
730
731         return count;
732 }
733 LUSTRE_RW_ATTR(access_log_mask);
734
735 static ssize_t access_log_size_show(struct kobject *kobj,
736                                     struct attribute *attr, char *buf)
737 {
738         struct obd_device *obd = container_of(kobj, struct obd_device,
739                                               obd_kset.kobj);
740         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
741
742         return snprintf(buf, PAGE_SIZE, "%u\n", ofd->ofd_access_log_size);
743 }
744
745 static ssize_t access_log_size_store(struct kobject *kobj,
746                                      struct attribute *attr,
747                                      const char *buffer, size_t count)
748 {
749         struct obd_device *obd = container_of(kobj, struct obd_device,
750                                               obd_kset.kobj);
751         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
752         struct ofd_access_log *oal;
753         unsigned int size;
754         ssize_t rc;
755
756         rc = kstrtouint(buffer, 0, &size);
757         if (rc < 0)
758                 return rc;
759
760         if (!ofd_access_log_size_is_valid(size))
761                 return -EINVAL;
762
763         /* The size of the ofd_access_log cannot be changed after it
764          * has been created.
765          */
766         if (ofd->ofd_access_log_size == size)
767                 return count;
768
769         oal = ofd_access_log_create(obd->obd_name, size);
770         if (IS_ERR(oal))
771                 return PTR_ERR(oal);
772
773         spin_lock(&ofd->ofd_flags_lock);
774         if (ofd->ofd_access_log != NULL) {
775                 rc = -EBUSY;
776         } else {
777                 ofd->ofd_access_log = oal;
778                 ofd->ofd_access_log_size = size;
779                 oal = NULL;
780                 rc = count;
781         }
782         spin_unlock(&ofd->ofd_flags_lock);
783
784         ofd_access_log_delete(oal);
785
786         return rc;
787 }
788 LUSTRE_RW_ATTR(access_log_size);
789
790 static int ofd_site_stats_seq_show(struct seq_file *m, void *data)
791 {
792         struct obd_device *obd = m->private;
793
794         return lu_site_stats_seq_print(obd->obd_lu_dev->ld_site, m);
795 }
796
797 LPROC_SEQ_FOPS_RO(ofd_site_stats);
798
799 /**
800  * Show if the OFD enforces T10PI checksum.
801  *
802  * \param[in] m         seq_file handle
803  * \param[in] data      unused for single entry
804  *
805  * \retval              0 on success
806  * \retval              negative value on error
807  */
808 static ssize_t checksum_t10pi_enforce_show(struct kobject *kobj,
809                                            struct attribute *attr,
810                                            char *buf)
811 {
812         struct obd_device *obd = container_of(kobj, struct obd_device,
813                                               obd_kset.kobj);
814         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
815
816         return sprintf(buf, "%u\n", ofd->ofd_checksum_t10pi_enforce);
817 }
818
819 /**
820  * Force specific T10PI checksum modes to be enabled
821  *
822  * If T10PI *is* supported in hardware, allow only the supported T10PI type
823  * to be used. If T10PI is *not* supported by the OSD, setting the enforce
824  * parameter forces all T10PI types to be enabled (even if slower) for
825  * testing.
826  *
827  * The final determination of which algorithm to be used depends whether
828  * the client supports T10PI or not, and is handled at client connect time.
829  *
830  * \param[in] file      proc file
831  * \param[in] buffer    string which represents mode
832  *                      1: set T10PI checksums enforced
833  *                      0: unset T10PI checksums enforced
834  * \param[in] count     \a buffer length
835  * \param[in] off       unused for single entry
836  *
837  * \retval              \a count on success
838  * \retval              negative number on error
839  */
840 static ssize_t checksum_t10pi_enforce_store(struct kobject *kobj,
841                                             struct attribute *attr,
842                                             const char *buffer, size_t count)
843 {
844         struct obd_device *obd = container_of(kobj, struct obd_device,
845                                               obd_kset.kobj);
846         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
847         bool enforce;
848         int rc;
849
850         rc = kstrtobool(buffer, &enforce);
851         if (rc)
852                 return rc;
853
854         spin_lock(&ofd->ofd_flags_lock);
855         ofd->ofd_checksum_t10pi_enforce = enforce;
856         spin_unlock(&ofd->ofd_flags_lock);
857         return count;
858 }
859 LUSTRE_RW_ATTR(checksum_t10pi_enforce);
860
861 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
862 static bool max_file_warned;
863 static bool rd_cache_warned;
864 static bool wr_cache_warned;
865
866 static ssize_t read_cache_enable_show(struct kobject *kobj,
867                                       struct attribute *attr,
868                                       char *buf)
869 {
870         struct obd_device *obd = container_of(kobj, struct obd_device,
871                                               obd_kset.kobj);
872         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
873
874         if (!rd_cache_warned) {
875                 rd_cache_warned = true;
876                 pr_info("ofd: 'obdfilter.*.read_cache_enabled' is deprecated, use 'osd-*.read_cache_enabled' instead\n");
877         }
878
879         if (!ofd->ofd_read_cache_enable)
880                 return -EOPNOTSUPP;
881
882         return lustre_attr_show(&ofd->ofd_osd->dd_kobj,
883                                 ofd->ofd_read_cache_enable, buf);
884 }
885
886 static ssize_t read_cache_enable_store(struct kobject *kobj,
887                                        struct attribute *attr,
888                                        const char *buffer, size_t count)
889 {
890         struct obd_device *obd = container_of(kobj, struct obd_device,
891                                               obd_kset.kobj);
892         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
893
894         if (!rd_cache_warned) {
895                 rd_cache_warned = true;
896                 pr_info("ofd: 'obdfilter.*.read_cache_enabled' is deprecated, use 'osd-*.read_cache_enabled' instead\n");
897         }
898
899         if (!ofd->ofd_read_cache_enable)
900                 return -EOPNOTSUPP;
901
902         return lustre_attr_store(&ofd->ofd_osd->dd_kobj,
903                                  ofd->ofd_read_cache_enable, buffer, count);
904 }
905 LUSTRE_RW_ATTR(read_cache_enable);
906
907 static ssize_t readcache_max_filesize_show(struct kobject *kobj,
908                                            struct attribute *attr,
909                                            char *buf)
910 {
911         struct obd_device *obd = container_of(kobj, struct obd_device,
912                                               obd_kset.kobj);
913         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
914
915         if (!max_file_warned) {
916                 max_file_warned = true;
917                 pr_info("ofd: 'obdfilter.*.readcache_max_filesize' is deprecated, use 'osd-*.readcache_max_filesize' instead\n");
918         }
919
920         if (!ofd->ofd_read_cache_max_filesize)
921                 return -EOPNOTSUPP;
922
923         return lustre_attr_show(&ofd->ofd_osd->dd_kobj,
924                                 ofd->ofd_read_cache_max_filesize, buf);
925 }
926
927 static ssize_t readcache_max_filesize_store(struct kobject *kobj,
928                                             struct attribute *attr,
929                                             const char *buffer, size_t count)
930 {
931         struct obd_device *obd = container_of(kobj, struct obd_device,
932                                               obd_kset.kobj);
933         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
934
935         if (!max_file_warned) {
936                 max_file_warned = true;
937                 pr_info("ofd: 'obdfilter.*.readcache_max_filesize' is deprecated, use 'osd-*.readcache_max_filesize' instead\n");
938         }
939
940         if (!ofd->ofd_read_cache_max_filesize)
941                 return -EOPNOTSUPP;
942
943         return lustre_attr_store(&ofd->ofd_osd->dd_kobj,
944                                  ofd->ofd_read_cache_max_filesize,
945                                  buffer, count);
946 }
947 LUSTRE_RW_ATTR(readcache_max_filesize);
948
949 static ssize_t writethrough_cache_enable_show(struct kobject *kobj,
950                                               struct attribute *attr,
951                                               char *buf)
952 {
953         struct obd_device *obd = container_of(kobj, struct obd_device,
954                                               obd_kset.kobj);
955         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
956
957         if (!wr_cache_warned) {
958                 wr_cache_warned = true;
959                 pr_info("ofd: 'obdfilter.*.writethrough_cache_enabled' is deprecated, use 'osd-*.writethrough_cache_enabled' instead\n");
960         }
961
962         if (!ofd->ofd_write_cache_enable)
963                 return -EOPNOTSUPP;
964
965         return lustre_attr_show(&ofd->ofd_osd->dd_kobj,
966                                 ofd->ofd_write_cache_enable, buf);
967 }
968
969 static ssize_t writethrough_cache_enable_store(struct kobject *kobj,
970                                                struct attribute *attr,
971                                                const char *buffer, size_t count)
972 {
973         struct obd_device *obd = container_of(kobj, struct obd_device,
974                                               obd_kset.kobj);
975         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
976
977         if (!ofd->ofd_write_cache_enable)
978                 return -EOPNOTSUPP;
979
980         return lustre_attr_store(&ofd->ofd_osd->dd_kobj,
981                                  ofd->ofd_write_cache_enable,
982                                  buffer, count);
983 }
984 LUSTRE_RW_ATTR(writethrough_cache_enable);
985 #endif
986
987 LPROC_SEQ_FOPS_RO_TYPE(ofd, recovery_status);
988 LUSTRE_RW_ATTR(recovery_time_hard);
989 LUSTRE_RW_ATTR(recovery_time_soft);
990 LUSTRE_RW_ATTR(ir_factor);
991
992 LPROC_SEQ_FOPS_WR_ONLY(ofd, evict_client);
993 LPROC_SEQ_FOPS_RW_TYPE(ofd, checksum_dump);
994 LUSTRE_RW_ATTR(job_cleanup_interval);
995
996 LUSTRE_RO_ATTR(tot_dirty);
997 LUSTRE_RO_ATTR(tot_granted);
998 LUSTRE_RO_ATTR(tot_pending);
999 LUSTRE_RW_ATTR(grant_compat_disable);
1000 LUSTRE_RO_ATTR(instance);
1001
1002 LUSTRE_RO_ATTR(num_exports);
1003
1004 struct lprocfs_vars lprocfs_ofd_obd_vars[] = {
1005         { .name =       "last_id",
1006           .fops =       &ofd_last_id_fops               },
1007         { .name =       "recovery_status",
1008           .fops =       &ofd_recovery_status_fops       },
1009         { .name =       "evict_client",
1010           .fops =       &ofd_evict_client_fops          },
1011         { .name =       "brw_size",
1012           .fops =       &ofd_brw_size_fops              },
1013         { .name =       "checksum_dump",
1014           .fops =       &ofd_checksum_dump_fops         },
1015         { .name =       "lfsck_layout",
1016           .fops =       &ofd_lfsck_layout_fops          },
1017         { .name =       "lfsck_verify_pfid",
1018           .fops =       &ofd_lfsck_verify_pfid_fops     },
1019         { .name =       "site_stats",
1020           .fops =       &ofd_site_stats_fops            },
1021         { NULL }
1022 };
1023
1024 /**
1025  * Initialize OFD statistics counters
1026  *
1027  * param[in] stats      statistics counters
1028  */
1029 void ofd_stats_counter_init(struct lprocfs_stats *stats, unsigned int offset)
1030 {
1031         LASSERT(stats && stats->ls_num >= LPROC_OFD_STATS_LAST);
1032
1033         lprocfs_counter_init(stats, LPROC_OFD_STATS_READ_BYTES,
1034                              LPROCFS_TYPE_BYTES_FULL, "read_bytes", "bytes");
1035         lprocfs_counter_init(stats, LPROC_OFD_STATS_WRITE_BYTES,
1036                              LPROCFS_TYPE_BYTES_FULL, "write_bytes", "bytes");
1037         lprocfs_counter_init(stats, LPROC_OFD_STATS_READ,
1038                              LPROCFS_TYPE_LATENCY, "read", "usecs");
1039         lprocfs_counter_init(stats, LPROC_OFD_STATS_WRITE,
1040                              LPROCFS_TYPE_LATENCY, "write", "usecs");
1041         lprocfs_counter_init(stats, LPROC_OFD_STATS_GETATTR,
1042                              LPROCFS_TYPE_LATENCY, "getattr", "usecs");
1043         lprocfs_counter_init(stats, LPROC_OFD_STATS_SETATTR,
1044                              LPROCFS_TYPE_LATENCY, "setattr", "usecs");
1045         lprocfs_counter_init(stats, LPROC_OFD_STATS_PUNCH,
1046                              LPROCFS_TYPE_LATENCY, "punch", "usecs");
1047         lprocfs_counter_init(stats, LPROC_OFD_STATS_SYNC,
1048                              LPROCFS_TYPE_LATENCY, "sync", "usecs");
1049         lprocfs_counter_init(stats, LPROC_OFD_STATS_DESTROY,
1050                              LPROCFS_TYPE_LATENCY, "destroy", "usecs");
1051         lprocfs_counter_init(stats, LPROC_OFD_STATS_CREATE,
1052                              LPROCFS_TYPE_LATENCY, "create", "usecs");
1053         lprocfs_counter_init(stats, LPROC_OFD_STATS_STATFS,
1054                              LPROCFS_TYPE_LATENCY, "statfs", "usecs");
1055         lprocfs_counter_init(stats, LPROC_OFD_STATS_GET_INFO,
1056                              LPROCFS_TYPE_LATENCY, "get_info", "usecs");
1057         lprocfs_counter_init(stats, LPROC_OFD_STATS_SET_INFO,
1058                              LPROCFS_TYPE_LATENCY, "set_info", "usecs");
1059         lprocfs_counter_init(stats, LPROC_OFD_STATS_QUOTACTL,
1060                              LPROCFS_TYPE_LATENCY, "quotactl", "usecs");
1061         lprocfs_counter_init(stats, LPROC_OFD_STATS_PREALLOC,
1062                              LPROCFS_TYPE_LATENCY, "prealloc", "usecs");
1063 }
1064
1065 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
1066
1067 static struct attribute *ofd_attrs[] = {
1068         &lustre_attr_tot_dirty.attr,
1069         &lustre_attr_tot_granted.attr,
1070         &lustre_attr_tot_pending.attr,
1071         &lustre_attr_grant_compat_disable.attr,
1072         &lustre_attr_instance.attr,
1073         &lustre_attr_recovery_time_hard.attr,
1074         &lustre_attr_recovery_time_soft.attr,
1075         &lustre_attr_ir_factor.attr,
1076         &lustre_attr_num_exports.attr,
1077         &lustre_attr_seqs_allocated.attr,
1078         &lustre_attr_grant_precreate.attr,
1079         &lustre_attr_precreate_batch.attr,
1080         &lustre_attr_atime_diff.attr,
1081         &lustre_attr_degraded.attr,
1082         &lustre_attr_fstype.attr,
1083         &lustre_attr_no_precreate.attr,
1084         &lustre_attr_sync_journal.attr,
1085 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
1086         &lustre_attr_sync_on_lock_cancel.attr,
1087 #endif
1088         &lustre_attr_soft_sync_limit.attr,
1089         &lustre_attr_lfsck_speed_limit.attr,
1090         &lustre_attr_access_log_mask.attr,
1091         &lustre_attr_access_log_size.attr,
1092         &lustre_attr_job_cleanup_interval.attr,
1093         &lustre_attr_checksum_t10pi_enforce.attr,
1094 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
1095         &lustre_attr_read_cache_enable.attr,
1096         &lustre_attr_readcache_max_filesize.attr,
1097         &lustre_attr_writethrough_cache_enable.attr,
1098 #endif
1099         NULL,
1100 };
1101
1102 /**
1103  * Initialize all needed procfs entries for OFD device.
1104  *
1105  * \param[in] ofd       OFD device
1106  *
1107  * \retval              0 if successful
1108  * \retval              negative value on error
1109  */
1110 int ofd_tunables_init(struct ofd_device *ofd)
1111 {
1112         struct obd_device *obd = ofd_obd(ofd);
1113         struct proc_dir_entry *entry;
1114         int rc = 0;
1115
1116         ENTRY;
1117         /* lprocfs must be setup before the ofd so state can be safely added
1118          * to /proc incrementally as the ofd is setup
1119          */
1120         obd->obd_ktype.default_attrs = ofd_attrs;
1121         obd->obd_vars = lprocfs_ofd_obd_vars;
1122         rc = lprocfs_obd_setup(obd, false);
1123         if (rc) {
1124                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
1125                        obd->obd_name, rc);
1126                 RETURN(rc);
1127         }
1128
1129         rc = tgt_tunables_init(&ofd->ofd_lut);
1130         if (rc) {
1131                 CERROR("%s: tgt_tunables_init failed: rc = %d\n",
1132                        obd->obd_name, rc);
1133                 GOTO(obd_cleanup, rc);
1134         }
1135
1136         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
1137         if (rc) {
1138                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
1139                        obd->obd_name, rc);
1140                 GOTO(tgt_cleanup, rc);
1141         }
1142
1143         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
1144         if (IS_ERR(entry)) {
1145                 rc = PTR_ERR(entry);
1146                 CERROR("%s: error %d setting up lprocfs for %s\n",
1147                        obd->obd_name, rc, "exports");
1148                 GOTO(obd_free_stats, rc);
1149         }
1150         obd->obd_proc_exports_entry = entry;
1151
1152         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
1153                                    obd, &lprocfs_nid_stats_clear_fops);
1154         if (IS_ERR(entry)) {
1155                 rc = PTR_ERR(entry);
1156                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
1157                        obd->obd_name, rc);
1158                 GOTO(obd_free_stats, rc);
1159         }
1160
1161         ofd_stats_counter_init(obd->obd_stats, 0);
1162
1163         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
1164                                     ofd_stats_counter_init);
1165         if (rc)
1166                 GOTO(obd_free_stats, rc);
1167
1168         RETURN(0);
1169
1170 obd_free_stats:
1171         lprocfs_free_obd_stats(obd);
1172 tgt_cleanup:
1173         tgt_tunables_fini(&ofd->ofd_lut);
1174 obd_cleanup:
1175         lprocfs_obd_cleanup(obd);
1176
1177         return rc;
1178 }
1179 #endif /* CONFIG_PROC_FS */