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