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