Whamcloud - gitweb
LU-6864 osp: manage number of modify RPCs in flight
[fs/lustre-release.git] / lustre / osp / lproc_osp.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/osp/lproc_osp.c
32  *
33  * Lustre OST Proxy Device (OSP), procfs functions
34  *
35  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #include "osp_internal.h"
41
42 /**
43  * Show OSP active status
44  *
45  * \param[in] m         seq_file handle
46  * \param[in] data      unused for single entry
47  * \retval              0 on success
48  * \retval              negative number on error
49  */
50 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
51                            char *buf)
52 {
53         struct dt_device *dt = container_of(kobj, struct dt_device,
54                                             dd_kobj);
55         struct lu_device *lu = dt2lu_dev(dt);
56         struct obd_device *obd = lu->ld_obd;
57         struct obd_import *imp;
58         int rc;
59
60         with_imp_locked(obd, imp, rc)
61                 rc = sprintf(buf, "%d\n", !imp->imp_deactive);
62         return rc;
63 }
64
65 /**
66  * Activate/Deactivate OSP
67  *
68  * \param[in] file      proc file
69  * \param[in] buffer    string, which is "1" or "0" to activate/deactivate OSP
70  * \param[in] count     \a buffer length
71  * \param[in] off       unused for single entry
72  * \retval              \a count on success
73  * \retval              negative number on error
74  */
75 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
76                             const char *buffer, size_t count)
77 {
78         struct dt_device *dt = container_of(kobj, struct dt_device,
79                                             dd_kobj);
80         struct lu_device *lu = dt2lu_dev(dt);
81         struct obd_device *obd = lu->ld_obd;
82         struct obd_import *imp, *imp0;
83         bool val;
84         int rc;
85
86         rc = kstrtobool(buffer, &val);
87         if (rc)
88                 return rc;
89
90         with_imp_locked(obd, imp0, rc)
91                 imp = class_import_get(imp0);
92         if (rc)
93                 return rc;
94         /* opposite senses */
95         if (imp->imp_deactive == val)
96                 rc = ptlrpc_set_import_active(imp, val);
97         else
98                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
99                        (unsigned int)val);
100
101         class_import_put(imp);
102
103         return rc ?: count;
104 }
105 LUSTRE_RW_ATTR(active);
106
107 /**
108  * Show number of RPCs in flight
109  *
110  * \param[in] m         seq_file handle
111  * \param[in] data      unused for single entry
112  * \retval              0 on success
113  * \retval              negative number on error
114  */
115 static ssize_t sync_in_flight_show(struct kobject *kobj,
116                                    struct attribute *attr,
117                                    char *buf)
118 {
119         struct dt_device *dt = container_of(kobj, struct dt_device,
120                                             dd_kobj);
121         struct osp_device *osp = dt2osp_dev(dt);
122
123         return sprintf(buf, "%u\n", atomic_read(&osp->opd_sync_rpcs_in_flight));
124 }
125 LUSTRE_RO_ATTR(sync_in_flight);
126
127 /**
128  * Show number of RPCs in processing (including uncommitted by OST)
129  *
130  * \param[in] m         seq_file handle
131  * \param[in] data      unused for single entry
132  * \retval              0 on success
133  * \retval              negative number on error
134  */
135 static ssize_t sync_in_progress_show(struct kobject *kobj,
136                                      struct attribute *attr,
137                                      char *buf)
138 {
139         struct dt_device *dt = container_of(kobj, struct dt_device,
140                                             dd_kobj);
141         struct osp_device *osp = dt2osp_dev(dt);
142
143         return sprintf(buf, "%u\n", atomic_read(&osp->opd_sync_rpcs_in_progress));
144 }
145 LUSTRE_RO_ATTR(sync_in_progress);
146
147 /**
148  * Show number of changes to sync
149  *
150  * \param[in] m         seq_file handle
151  * \param[in] data      unused for single entry
152  * \retval              0 on success
153  * \retval              negative number on error
154  */
155 static ssize_t sync_changes_show(struct kobject *kobj,
156                                  struct attribute *attr,
157                                  char *buf)
158 {
159         struct dt_device *dt = container_of(kobj, struct dt_device,
160                                             dd_kobj);
161         struct osp_device *osp = dt2osp_dev(dt);
162
163         return sprintf(buf, "%u\n", atomic_read(&osp->opd_sync_changes));
164 }
165
166 /**
167  * Sync changes
168  *
169  * \param[in] file      proc file
170  * \param[in] buffer    unused because any input will do
171  * \param[in] count     \a buffer length
172  * \param[in] off       unused for single entry
173  * \retval              \a count on success
174  * \retval              negative number on error
175  */
176 static ssize_t sync_changes_store(struct kobject *kobj, struct attribute *attr,
177                                   const char *buffer, size_t count)
178 {
179         struct dt_device *dt = container_of(kobj, struct dt_device,
180                                             dd_kobj);
181         struct osp_device *osp = dt2osp_dev(dt);
182         struct lu_env env;
183         int rc;
184
185         rc = lu_env_init(&env, LCT_LOCAL);
186         if (rc != 0)
187                 return rc;
188
189         rc = dt_sync(&env, &osp->opd_dt_dev);
190         lu_env_fini(&env);
191
192         return rc == 0 ? count : rc;
193 }
194 LUSTRE_RW_ATTR(sync_changes);
195
196 static ssize_t max_sync_changes_show(struct kobject *kobj,
197                 struct attribute *attr, char *buf)
198 {
199         struct dt_device *dt = container_of(kobj, struct dt_device,
200                                             dd_kobj);
201         struct osp_device *osp = dt2osp_dev(dt);
202
203         return sprintf(buf, "%u\n", osp->opd_sync_max_changes);
204 }
205
206
207 static ssize_t max_sync_changes_store(struct kobject *kobj,
208                 struct attribute *attr, const char *buffer, size_t count)
209 {
210         struct dt_device *dt = container_of(kobj, struct dt_device,
211                                             dd_kobj);
212         struct osp_device *osp = dt2osp_dev(dt);
213         int val;
214         int rc;
215
216         rc = kstrtoint(buffer, 0, &val);
217         if (rc)
218                 return rc;
219         if (val <= 0)
220                 return -ERANGE;
221         osp->opd_sync_max_changes = val;
222         return count;
223 }
224
225 LUSTRE_RW_ATTR(max_sync_changes);
226
227 /**
228  * Show maximum number of RPCs in flight allowed
229  *
230  * \param[in] m         seq_file handle
231  * \param[in] data      unused for single entry
232  * \retval              0 on success
233  * \retval              negative number on error
234  */
235 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
236                                        struct attribute *attr,
237                                        char *buf)
238 {
239         struct dt_device *dt = container_of(kobj, struct dt_device,
240                                             dd_kobj);
241         struct osp_device *osp = dt2osp_dev(dt);
242
243         return sprintf(buf, "%u\n", osp->opd_sync_max_rpcs_in_flight);
244 }
245
246 /**
247  * Change maximum number of RPCs in flight allowed
248  *
249  * \param[in] file      proc file
250  * \param[in] buffer    string which represents maximum number
251  * \param[in] count     \a buffer length
252  * \param[in] off       unused for single entry
253  * \retval              \a count on success
254  * \retval              negative number on error
255  */
256 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
257                                         struct attribute *attr,
258                                         const char *buffer,
259                                         size_t count)
260 {
261         struct dt_device *dt = container_of(kobj, struct dt_device,
262                                             dd_kobj);
263         struct osp_device *osp = dt2osp_dev(dt);
264         unsigned int val;
265         int rc;
266
267         rc = kstrtouint(buffer, 0, &val);
268         if (rc)
269                 return rc;
270
271         if (val == 0)
272                 return -ERANGE;
273
274         osp->opd_sync_max_rpcs_in_flight = val;
275         return count;
276 }
277 LUSTRE_RW_ATTR(max_rpcs_in_flight);
278
279 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
280                                            struct attribute *attr,
281                                            char *buf)
282 {
283         struct dt_device *dt = container_of(kobj, struct dt_device,
284                                             dd_kobj);
285         struct lu_device *lu = dt2lu_dev(dt);
286         struct obd_device *obd = lu->ld_obd;
287         u16 max;
288
289         max = obd_get_max_mod_rpcs_in_flight(&obd->u.cli);
290         return scnprintf(buf, PAGE_SIZE, "%hu\n", max);
291 }
292
293 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
294                                             struct attribute *attr,
295                                             const char *buffer,
296                                             size_t count)
297 {
298         struct dt_device *dt = container_of(kobj, struct dt_device,
299                                             dd_kobj);
300         struct lu_device *lu = dt2lu_dev(dt);
301         struct obd_device *obd = lu->ld_obd;
302         u16 val;
303         int rc;
304
305         rc = kstrtou16(buffer, 10, &val);
306         if (rc)
307                 return rc;
308
309         rc = obd_set_max_mod_rpcs_in_flight(&obd->u.cli, val);
310         if (rc)
311                 count = rc;
312
313         return count;
314 }
315 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
316
317 /**
318  * Show maximum number of RPCs in processing allowed
319  *
320  * \param[in] m         seq_file handle
321  * \param[in] data      unused
322  * \retval              0 on success
323  * \retval              negative number on error
324  */
325 static ssize_t max_rpcs_in_progress_show(struct kobject *kobj,
326                                          struct attribute *attr,
327                                          char *buf)
328 {
329         struct dt_device *dt = container_of(kobj, struct dt_device,
330                                             dd_kobj);
331         struct osp_device *osp = dt2osp_dev(dt);
332
333         return sprintf(buf, "%u\n", osp->opd_sync_max_rpcs_in_progress);
334 }
335
336 /**
337  * Change maximum number of RPCs in processing allowed
338  *
339  * \param[in] file      proc file
340  * \param[in] buffer    string which represents maximum number
341  * \param[in] count     \a buffer length
342  * \param[in] off       unused for single entry
343  * \retval              \a count on success
344  * \retval              negative number on error
345  */
346 static ssize_t max_rpcs_in_progress_store(struct kobject *kobj,
347                                           struct attribute *attr,
348                                           const char *buffer,
349                                           size_t count)
350 {
351         struct dt_device *dt = container_of(kobj, struct dt_device,
352                                             dd_kobj);
353         struct osp_device *osp = dt2osp_dev(dt);
354         unsigned int val;
355         int rc;
356
357         rc = kstrtouint(buffer, 0, &val);
358         if (rc)
359                 return rc;
360
361         if (val == 0)
362                 return -ERANGE;
363
364         osp->opd_sync_max_rpcs_in_progress = val;
365
366         return count;
367 }
368 LUSTRE_RW_ATTR(max_rpcs_in_progress);
369
370 /**
371  * Show number of objects to precreate next time
372  *
373  * \param[in] m         seq_file handle
374  * \param[in] data      unused for single entry
375  * \retval              0 on success
376  * \retval              negative number on error
377  */
378 static ssize_t create_count_show(struct kobject *kobj,
379                                  struct attribute *attr,
380                                  char *buf)
381 {
382         struct dt_device *dt = container_of(kobj, struct dt_device,
383                                             dd_kobj);
384         struct osp_device *osp = dt2osp_dev(dt);
385
386         if (!osp->opd_pre)
387                 return -EINVAL;
388
389         return sprintf(buf, "%d\n", osp->opd_pre_create_count);
390 }
391
392 /**
393  * Change number of objects to precreate next time
394  *
395  * \param[in] file      proc file
396  * \param[in] buffer    string which represents number of objects to precreate
397  * \param[in] count     \a buffer length
398  * \param[in] off       unused for single entry
399  * \retval              \a count on success
400  * \retval              negative number on error
401  */
402 static ssize_t create_count_store(struct kobject *kobj, struct attribute *attr,
403                                   const char *buffer, size_t count)
404 {
405         struct dt_device *dt = container_of(kobj, struct dt_device,
406                                             dd_kobj);
407         struct osp_device *osp = dt2osp_dev(dt);
408         unsigned int val;
409         int rc;
410
411         if (!osp->opd_pre)
412                 return -EINVAL;
413
414         rc = kstrtouint(buffer, 0, &val);
415         if (rc)
416                 return rc;
417
418         /* The MDT ALWAYS needs to limit the precreate count to
419          * OST_MAX_PRECREATE, and the constant cannot be changed
420          * because it is a value shared between the OSP and OST
421          * that is the maximum possible number of objects that will
422          * ever be handled by MDT->OST recovery processing.
423          *
424          * The OSP enforces the pre_create_count to amaximum of
425          * one half of opd_pre_max_create_count.
426          *
427          * If the OST ever gets a request to delete more orphans,
428          * this implies that something has gone badly on the MDT
429          * and the OST will refuse to delete so much data from the
430          * filesystem as a safety measure.
431          */
432         if (val < OST_MIN_PRECREATE)
433                 return -ERANGE;
434         if (val > osp->opd_pre_max_create_count / 2)
435                 val = osp->opd_pre_max_create_count / 2;
436
437         /* set to largest value <= 32, 64, 128 or a multiple of 256 */
438         if (val > 256)
439                 osp->opd_pre_create_count = val & 0xffffff00;
440         else
441                 osp->opd_pre_create_count = rounddown_pow_of_two(val);
442
443         return count;
444 }
445 LUSTRE_RW_ATTR(create_count);
446
447 /**
448  * Show maximum number of objects to precreate
449  *
450  * \param[in] m         seq_file handle
451  * \param[in] data      unused for single entry
452  * \retval              0 on success
453  * \retval              negative number on error
454  */
455 static ssize_t max_create_count_show(struct kobject *kobj,
456                                      struct attribute *attr,
457                                      char *buf)
458 {
459         struct dt_device *dt = container_of(kobj, struct dt_device,
460                                             dd_kobj);
461         struct osp_device *osp = dt2osp_dev(dt);
462
463         if (!osp->opd_pre)
464                 return -EINVAL;
465
466         return sprintf(buf, "%d\n", osp->opd_pre_max_create_count);
467 }
468
469 /**
470  * Change maximum number of objects to precreate
471  *
472  * \param[in] file      proc file
473  * \param[in] buffer    string which represents maximum number
474  * \param[in] count     \a buffer length
475  * \param[in] off       unused for single entry
476  * \retval              \a count on success
477  * \retval              negative number on error
478  */
479 static ssize_t max_create_count_store(struct kobject *kobj,
480                                       struct attribute *attr,
481                                       const char *buffer, size_t count)
482 {
483         struct dt_device *dt = container_of(kobj, struct dt_device,
484                                             dd_kobj);
485         struct osp_device *osp = dt2osp_dev(dt);
486         unsigned int val;
487         int rc;
488
489         if (!osp->opd_pre)
490                 return -EINVAL;
491
492         rc = kstrtouint(buffer, 0, &val);
493         if (rc)
494                 return rc;
495
496         if (val && (val < OST_MIN_PRECREATE ||
497                     val > OST_MAX_PRECREATE))
498                 return -ERANGE;
499
500         if (osp->opd_pre_create_count > val)
501                 osp->opd_pre_create_count = val;
502
503         /* Can be 0 after setting max_create_count to 0 */
504         if (osp->opd_pre_create_count == 0 && val != 0)
505                 osp->opd_pre_create_count = OST_MIN_PRECREATE;
506
507         osp->opd_pre_max_create_count = val;
508
509         return count;
510 }
511 LUSTRE_RW_ATTR(max_create_count);
512
513 /**
514  * Show last id to assign in creation
515  *
516  * \param[in] m         seq_file handle
517  * \param[in] data      unused for single entry
518  * \retval              0 on success
519  * \retval              negative number on error
520  */
521 static ssize_t prealloc_next_id_show(struct kobject *kobj,
522                                      struct attribute *attr,
523                                      char *buf)
524 {
525         struct dt_device *dt = container_of(kobj, struct dt_device,
526                                             dd_kobj);
527         struct osp_device *osp = dt2osp_dev(dt);
528         struct lu_fid *fid;
529         u64 id;
530
531         if (!osp->opd_pre)
532                 return -EINVAL;
533
534         fid = &osp->opd_pre_used_fid;
535         if (fid_is_idif(fid)) {
536                 id = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
537                 id++;
538         } else {
539                 id = unlikely(fid_oid(fid) == LUSTRE_DATA_SEQ_MAX_WIDTH) ?
540                         1 : fid_oid(fid) + 1;
541         }
542
543         return sprintf(buf, "%llu\n", id);
544 }
545 LUSTRE_RO_ATTR(prealloc_next_id);
546
547 /**
548  * Show last created id OST reported
549  *
550  * \param[in] m         seq_file handle
551  * \param[in] data      unused for single entry
552  * \retval              0 on success
553  * \retval              negative number on error
554  */
555
556 static ssize_t prealloc_last_id_show(struct kobject *kobj,
557                                      struct attribute *attr,
558                                      char *buf)
559 {
560         struct dt_device *dt = container_of(kobj, struct dt_device,
561                                             dd_kobj);
562         struct osp_device *osp = dt2osp_dev(dt);
563         struct lu_fid *fid;
564         u64 id;
565
566         if (!osp->opd_pre)
567                 return -EINVAL;
568
569         fid = &osp->opd_pre_last_created_fid;
570         id = fid_is_idif(fid) ?
571                          fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid)) :
572                          fid_oid(fid);
573
574         return sprintf(buf, "%llu\n", id);
575 }
576 LUSTRE_RO_ATTR(prealloc_last_id);
577
578 /**
579  * Show next FID sequence to precreate
580  *
581  * \param[in] m         seq_file handle
582  * \param[in] data      unused for single entry
583  * \retval              0 on success
584  * \retval              negative number on error
585  */
586 static ssize_t prealloc_next_seq_show(struct kobject *kobj,
587                                       struct attribute *attr,
588                                       char *buf)
589 {
590         struct dt_device *dt = container_of(kobj, struct dt_device,
591                                             dd_kobj);
592         struct osp_device *osp = dt2osp_dev(dt);
593         struct lu_fid *fid;
594
595         if (!osp->opd_pre)
596                 return -EINVAL;
597
598         fid = &osp->opd_pre_used_fid;
599         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
600                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
601 }
602 LUSTRE_RO_ATTR(prealloc_next_seq);
603
604 /**
605  * Show last created FID sequence OST reported
606  *
607  * \param[in] m         seq_file handle
608  * \param[in] data      unused for single entry
609  * \retval              0 on success
610  * \retval              negative number on error
611  */
612 static ssize_t prealloc_last_seq_show(struct kobject *kobj,
613                                       struct attribute *attr,
614                                       char *buf)
615 {
616         struct dt_device *dt = container_of(kobj, struct dt_device,
617                                             dd_kobj);
618         struct osp_device *osp = dt2osp_dev(dt);
619         struct lu_fid *fid;
620
621         if (!osp->opd_pre)
622                 return -EINVAL;
623
624         fid = &osp->opd_pre_last_created_fid;
625         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
626                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
627 }
628 LUSTRE_RO_ATTR(prealloc_last_seq);
629
630 /**
631  * Show the number of ids reserved by declare
632  *
633  * \param[in] m         seq_file handle
634  * \param[in] data      unused for single entry
635  * \retval              0 on success
636  * \retval              negative number on error
637  */
638 static ssize_t prealloc_reserved_show(struct kobject *kobj,
639                                       struct attribute *attr,
640                                       char *buf)
641 {
642         struct dt_device *dt = container_of(kobj, struct dt_device,
643                                             dd_kobj);
644         struct osp_device *osp = dt2osp_dev(dt);
645
646         if (!osp->opd_pre)
647                 return -EINVAL;
648
649         return sprintf(buf, "%llu\n", osp->opd_pre_reserved);
650 }
651 LUSTRE_RO_ATTR(prealloc_reserved);
652
653 /**
654  * Show interval (in seconds) to update statfs data
655  *
656  * \param[in] m         seq_file handle
657  * \param[in] data      unused for single entry
658  * \retval              0 on success
659  * \retval              negative number on error
660  */
661 static ssize_t maxage_show(struct kobject *kobj,
662                            struct attribute *attr,
663                            char *buf)
664 {
665         struct dt_device *dt = container_of(kobj, struct dt_device,
666                                             dd_kobj);
667         struct osp_device *osp = dt2osp_dev(dt);
668
669         return sprintf(buf, "%lld\n", osp->opd_statfs_maxage);
670 }
671
672 /**
673  * Change interval to update statfs data
674  *
675  * \param[in] file      proc file
676  * \param[in] buffer    string which represents statfs interval (in seconds)
677  * \param[in] count     \a buffer length
678  * \param[in] off       unused for single entry
679  * \retval              \a count on success
680  * \retval              negative number on error
681  */
682 static ssize_t maxage_store(struct kobject *kobj, struct attribute *attr,
683                             const char *buffer, size_t count)
684 {
685         struct dt_device *dt = container_of(kobj, struct dt_device,
686                                             dd_kobj);
687         struct osp_device *osp = dt2osp_dev(dt);
688         unsigned int val;
689         int rc;
690
691         rc = kstrtouint(buffer, 0, &val);
692         if (rc)
693                 return rc;
694
695         if (val == 0)
696                 return -ERANGE;
697
698         osp->opd_statfs_maxage = val;
699
700         return count;
701 }
702 LUSTRE_RW_ATTR(maxage);
703
704 /**
705  * Show current precreation status: output 0 means success, otherwise negative
706  * number is printed
707  *
708  * \param[in] m         seq_file handle
709  * \param[in] data      unused for single entry
710  * \retval              0 on success
711  * \retval              negative number on error
712  */
713 static ssize_t prealloc_status_show(struct kobject *kobj,
714                                     struct attribute *attr,
715                                     char *buf)
716 {
717         struct dt_device *dt = container_of(kobj, struct dt_device,
718                                             dd_kobj);
719         struct osp_device *osp = dt2osp_dev(dt);
720
721         if (!osp->opd_pre)
722                 return -EINVAL;
723
724         return sprintf(buf, "%d\n", osp->opd_pre_status);
725 }
726 LUSTRE_RO_ATTR(prealloc_status);
727
728 /**
729  * Show the number of RPCs in processing (including uncommitted by OST) plus
730  * changes to sync, i.e. this is the total number of changes OST needs to apply
731  * and commit.
732  *
733  * This counter is used to determine if OST has space returned. A zero value
734  * indicates that OST storage space consumed by destroyed objects has been freed
735  * on disk, the associated llog records have been cleared, and no synchronous
736  * RPC are being processed.
737  *
738  * \param[in] m         seq_file handle
739  * \param[in] data      unused for single entry
740  * \retval              0 on success
741  * \retval              negative number on error
742  */
743 static ssize_t destroys_in_flight_show(struct kobject *kobj,
744                                        struct attribute *attr,
745                                        char *buf)
746 {
747         struct dt_device *dt = container_of(kobj, struct dt_device,
748                                             dd_kobj);
749         struct osp_device *osp = dt2osp_dev(dt);
750
751         return sprintf(buf, "%u\n",
752                        atomic_read(&osp->opd_sync_rpcs_in_progress) +
753                        atomic_read(&osp->opd_sync_changes));
754 }
755 LUSTRE_RO_ATTR(destroys_in_flight);
756
757 /**
758  * Show changes synced from previous mount
759  *
760  * \param[in] m         seq_file handle
761  * \param[in] data      unused for single entry
762  * \retval              0 on success
763  * \retval              negative number on error
764  */
765 static ssize_t old_sync_processed_show(struct kobject *kobj,
766                                        struct attribute *attr,
767                                        char *buf)
768 {
769         struct dt_device *dt = container_of(kobj, struct dt_device,
770                                             dd_kobj);
771         struct osp_device *osp = dt2osp_dev(dt);
772
773         return sprintf(buf, "%d\n", osp->opd_sync_prev_done);
774 }
775 LUSTRE_RO_ATTR(old_sync_processed);
776
777 /**
778  * Show maximum number of RPCs in flight
779  *
780  * \param[in] m         seq_file handle
781  * \param[in] data      unused for single entry
782  * \retval              0 on success
783  * \retval              negative number on error
784  */
785 static ssize_t lfsck_max_rpcs_in_flight_show(struct kobject *kobj,
786                                              struct attribute *attr,
787                                              char *buf)
788 {
789         struct dt_device *dt = container_of(kobj, struct dt_device,
790                                             dd_kobj);
791         struct lu_device *lu = dt2lu_dev(dt);
792         struct obd_device *obd = lu->ld_obd;
793         u32 max;
794
795         max = obd_get_max_rpcs_in_flight(&obd->u.cli);
796         return sprintf(buf, "%u\n", max);
797 }
798
799 /**
800  * Change maximum number of RPCs in flight
801  *
802  * \param[in] file      proc file
803  * \param[in] buffer    string which represents maximum number of RPCs in flight
804  * \param[in] count     \a buffer length
805  * \param[in] off       unused for single entry
806  * \retval              \a count on success
807  * \retval              negative number on error
808  */
809 static ssize_t lfsck_max_rpcs_in_flight_store(struct kobject *kobj,
810                                               struct attribute *attr,
811                                               const char *buffer,
812                                               size_t count)
813 {
814         struct dt_device *dt = container_of(kobj, struct dt_device,
815                                             dd_kobj);
816         struct lu_device *lu = dt2lu_dev(dt);
817         struct obd_device *obd = lu->ld_obd;
818         unsigned int val;
819         int rc;
820
821         rc = kstrtouint(buffer, 0, &val);
822         if (rc)
823                 return rc;
824
825         rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
826         return rc ? rc : count;
827 }
828 LUSTRE_RW_ATTR(lfsck_max_rpcs_in_flight);
829
830 ssize_t ping_show(struct kobject *kobj, struct attribute *attr,
831                   char *buffer)
832 {
833         struct dt_device *dt = container_of(kobj, struct dt_device,
834                                             dd_kobj);
835         struct lu_device *lu = dt2lu_dev(dt);
836         struct obd_device *obd = lu->ld_obd;
837         int rc;
838
839         rc = ptlrpc_obd_ping(obd);
840
841         return rc;
842 }
843 LUSTRE_RO_ATTR(ping);
844
845 ssize_t osp_conn_uuid_show(struct kobject *kobj, struct attribute *attr,
846                            char *buf)
847 {
848         struct dt_device *dt = container_of(kobj, struct dt_device,
849                                             dd_kobj);
850         struct lu_device *lu = dt2lu_dev(dt);
851         struct obd_device *obd = lu->ld_obd;
852         struct obd_import *imp;
853         struct ptlrpc_connection *conn;
854         ssize_t count;
855
856         with_imp_locked(obd, imp, count) {
857                 conn = imp->imp_connection;
858                 if (conn)
859                         count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
860                 else
861                         count = sprintf(buf, "%s\n", "<none>");
862         }
863
864         return count;
865 }
866
867 LUSTRE_ATTR(ost_conn_uuid, 0444, osp_conn_uuid_show, NULL);
868 LUSTRE_ATTR(mdt_conn_uuid, 0444, osp_conn_uuid_show, NULL);
869
870 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, connect_flags);
871 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, server_uuid);
872 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, timeouts);
873
874 LDEBUGFS_SEQ_FOPS_RW_TYPE(osp, import);
875 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, state);
876
877 /**
878  * Show high watermark (in megabytes). If available free space at OST is grater
879  * than high watermark and object allocation for OST is disabled, enable it.
880  *
881  * \param[in] m         seq_file handle
882  * \param[in] data      unused for single entry
883  * \retval              0 on success
884  * \retval              negative number on error
885  */
886 static int osp_reserved_mb_high_seq_show(struct seq_file *m, void *data)
887 {
888         struct obd_device       *dev = m->private;
889         struct osp_device       *osp = lu2osp_dev(dev->obd_lu_dev);
890
891         if (osp == NULL)
892                 return -EINVAL;
893
894         seq_printf(m, "%u\n", osp->opd_reserved_mb_high);
895         return 0;
896 }
897
898 /**
899  * Change high watermark
900  *
901  * \param[in] file      proc file
902  * \param[in] buffer    string which represents new value (in megabytes)
903  * \param[in] count     \a buffer length
904  * \param[in] off       unused for single entry
905  * \retval              \a count on success
906  * \retval              negative number on error
907  */
908 static ssize_t
909 osp_reserved_mb_high_seq_write(struct file *file, const char __user *buffer,
910                         size_t count, loff_t *off)
911 {
912         struct seq_file         *m = file->private_data;
913         struct obd_device       *dev = m->private;
914         struct osp_device       *osp = lu2osp_dev(dev->obd_lu_dev);
915         char kernbuf[22] = "";
916         u64 val;
917         int                     rc;
918
919         if (osp == NULL || osp->opd_pre == NULL)
920                 return -EINVAL;
921
922         if (count >= sizeof(kernbuf))
923                 return -EINVAL;
924
925         if (copy_from_user(kernbuf, buffer, count))
926                 return -EFAULT;
927         kernbuf[count] = 0;
928
929         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
930         if (rc < 0)
931                 return rc;
932         val >>= 20;
933         if (val < 1)
934                 return -ERANGE;
935
936         spin_lock(&osp->opd_pre_lock);
937         osp->opd_reserved_mb_high = val;
938         if (val <= osp->opd_reserved_mb_low)
939                 osp->opd_reserved_mb_low = val - 1;
940         spin_unlock(&osp->opd_pre_lock);
941
942         return count;
943 }
944 LDEBUGFS_SEQ_FOPS(osp_reserved_mb_high);
945
946 static int osp_rpc_stats_seq_show(struct seq_file *seq, void *v)
947 {
948         struct obd_device *dev = seq->private;
949
950         return obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
951 }
952
953 static ssize_t osp_rpc_stats_seq_write(struct file *file,
954                                        const char __user *buf,
955                                        size_t len, loff_t *off)
956 {
957         struct seq_file *seq = file->private_data;
958         struct obd_device *dev = seq->private;
959         struct client_obd *cli = &dev->u.cli;
960
961         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
962
963         return len;
964 }
965 LDEBUGFS_SEQ_FOPS(osp_rpc_stats);
966
967 /**
968  * Show low watermark (in megabytes). If available free space at OST is less
969  * than low watermark, object allocation for OST is disabled.
970  *
971  * \param[in] m         seq_file handle
972  * \param[in] data      unused for single entry
973  * \retval              0 on success
974  * \retval              negative number on error
975  */
976 static int osp_reserved_mb_low_seq_show(struct seq_file *m, void *data)
977 {
978         struct obd_device       *dev = m->private;
979         struct osp_device       *osp = lu2osp_dev(dev->obd_lu_dev);
980
981         if (osp == NULL)
982                 return -EINVAL;
983
984         seq_printf(m, "%u\n", osp->opd_reserved_mb_low);
985         return 0;
986 }
987
988 /**
989  * Change low watermark
990  *
991  * \param[in] file      proc file
992  * \param[in] buffer    string which represents new value (in megabytes)
993  * \param[in] count     \a buffer length
994  * \param[in] off       unused for single entry
995  * \retval              \a count on success
996  * \retval              negative number on error
997  */
998 static ssize_t
999 osp_reserved_mb_low_seq_write(struct file *file, const char __user *buffer,
1000                         size_t count, loff_t *off)
1001 {
1002         struct seq_file         *m = file->private_data;
1003         struct obd_device       *dev = m->private;
1004         struct osp_device       *osp = lu2osp_dev(dev->obd_lu_dev);
1005         char kernbuf[22] = "";
1006         u64 val;
1007         int                     rc;
1008
1009         if (osp == NULL || osp->opd_pre == NULL)
1010                 return -EINVAL;
1011
1012         if (count >= sizeof(kernbuf))
1013                 return -EINVAL;
1014
1015         if (copy_from_user(kernbuf, buffer, count))
1016                 return -EFAULT;
1017         kernbuf[count] = 0;
1018
1019         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
1020         if (rc < 0)
1021                 return rc;
1022         val >>= 20;
1023
1024         spin_lock(&osp->opd_pre_lock);
1025         osp->opd_reserved_mb_low = val;
1026         if (val >= osp->opd_reserved_mb_high)
1027                 osp->opd_reserved_mb_high = val + 1;
1028         spin_unlock(&osp->opd_pre_lock);
1029
1030         return count;
1031 }
1032 LDEBUGFS_SEQ_FOPS(osp_reserved_mb_low);
1033
1034 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
1035                                 const char *buffer, size_t count)
1036 {
1037         struct dt_device *dt = container_of(kobj, struct dt_device,
1038                                             dd_kobj);
1039         struct lu_env env;
1040         int rc;
1041
1042         rc = lu_env_init(&env, LCT_LOCAL);
1043         if (rc)
1044                 return rc;
1045
1046         rc = dt_sync(&env, dt);
1047         lu_env_fini(&env);
1048
1049         return rc == 0 ? count : rc;
1050 }
1051 LUSTRE_WO_ATTR(force_sync);
1052
1053 static struct ldebugfs_vars ldebugfs_osp_obd_vars[] = {
1054         { .name =       "connect_flags",
1055           .fops =       &osp_connect_flags_fops         },
1056         { .name =       "ost_server_uuid",
1057           .fops =       &osp_server_uuid_fops           },
1058         { .name =       "timeouts",
1059           .fops =       &osp_timeouts_fops              },
1060         { .name =       "import",
1061           .fops =       &osp_import_fops                },
1062         { .name =       "state",
1063           .fops =       &osp_state_fops                 },
1064         { .name =       "reserved_mb_high",
1065           .fops =       &osp_reserved_mb_high_fops      },
1066         { .name =       "reserved_mb_low",
1067           .fops =       &osp_reserved_mb_low_fops       },
1068         { NULL }
1069 };
1070
1071 static struct ldebugfs_vars ldebugfs_osp_md_vars[] = {
1072         { .name =       "connect_flags",
1073           .fops =       &osp_connect_flags_fops         },
1074         { .name =       "mdt_server_uuid",
1075           .fops =       &osp_server_uuid_fops           },
1076         { .name =       "timeouts",
1077           .fops =       &osp_timeouts_fops              },
1078         { .name =       "import",
1079           .fops =       &osp_import_fops                },
1080         { .name =       "state",
1081           .fops =       &osp_state_fops                 },
1082         { .name =       "rpc_stats",
1083           .fops =       &osp_rpc_stats_fops             },
1084         { NULL }
1085 };
1086
1087 static struct attribute *osp_obd_attrs[] = {
1088         /* First two for compatiability reasons */
1089         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1090         &lustre_attr_destroys_in_flight.attr,
1091         &lustre_attr_active.attr,
1092         &lustre_attr_max_rpcs_in_flight.attr,
1093         &lustre_attr_max_rpcs_in_progress.attr,
1094         &lustre_attr_maxage.attr,
1095         &lustre_attr_ost_conn_uuid.attr,
1096         &lustre_attr_ping.attr,
1097         &lustre_attr_prealloc_status.attr,
1098         &lustre_attr_prealloc_next_id.attr,
1099         &lustre_attr_prealloc_last_id.attr,
1100         &lustre_attr_prealloc_next_seq.attr,
1101         &lustre_attr_prealloc_last_seq.attr,
1102         &lustre_attr_prealloc_reserved.attr,
1103         &lustre_attr_sync_in_flight.attr,
1104         &lustre_attr_sync_in_progress.attr,
1105         &lustre_attr_sync_changes.attr,
1106         &lustre_attr_max_sync_changes.attr,
1107         &lustre_attr_force_sync.attr,
1108         &lustre_attr_old_sync_processed.attr,
1109         &lustre_attr_create_count.attr,
1110         &lustre_attr_max_create_count.attr,
1111         NULL,
1112 };
1113
1114 static struct attribute *osp_md_attrs[] = {
1115         /* First two for compatiability reasons */
1116         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1117         &lustre_attr_destroys_in_flight.attr,
1118         &lustre_attr_active.attr,
1119         &lustre_attr_max_rpcs_in_flight.attr,
1120         &lustre_attr_max_mod_rpcs_in_flight.attr,
1121         &lustre_attr_max_rpcs_in_progress.attr,
1122         &lustre_attr_maxage.attr,
1123         &lustre_attr_mdt_conn_uuid.attr,
1124         &lustre_attr_ping.attr,
1125         &lustre_attr_prealloc_status.attr,
1126         NULL,
1127 };
1128
1129 void osp_tunables_fini(struct osp_device *osp)
1130 {
1131         struct obd_device *obd = osp->opd_obd;
1132         struct kobject *osc;
1133
1134         osc = kset_find_obj(lustre_kset, "osc");
1135         if (osc) {
1136                 sysfs_remove_link(osc, obd->obd_name);
1137                 kobject_put(osc);
1138         }
1139
1140         debugfs_remove_recursive(osp->opd_debugfs);
1141         osp->opd_debugfs = NULL;
1142
1143         ptlrpc_lprocfs_unregister_obd(obd);
1144
1145         debugfs_remove_recursive(obd->obd_debugfs_entry);
1146         obd->obd_debugfs_entry = NULL;
1147
1148         dt_tunables_fini(&osp->opd_dt_dev);
1149 }
1150
1151 /**
1152  * Initialize OSP sysfs / debugfs
1153  *
1154  * param[in] osp        OSP device
1155  */
1156 void osp_tunables_init(struct osp_device *osp)
1157 {
1158         struct obd_device *obd = osp->opd_obd;
1159         struct kobject *osc;
1160         int rc;
1161
1162         if (osp->opd_connect_mdt) {
1163                 osp->opd_dt_dev.dd_ktype.default_attrs = osp_md_attrs;
1164                 obd->obd_debugfs_vars = ldebugfs_osp_md_vars;
1165         } else {
1166                 osp->opd_dt_dev.dd_ktype.default_attrs = osp_obd_attrs;
1167                 obd->obd_debugfs_vars = ldebugfs_osp_obd_vars;
1168         }
1169
1170         rc = dt_tunables_init(&osp->opd_dt_dev, obd->obd_type, obd->obd_name,
1171                               NULL);
1172         if (rc) {
1173                 CERROR("%s: failed to setup DT tunables: %d\n",
1174                        obd->obd_name, rc);
1175                 return;
1176         }
1177
1178         /* Since we register the obd device with ptlrpc / sptlrpc we
1179          * have to register debugfs with obd_device
1180          */
1181         obd->obd_debugfs_entry = debugfs_create_dir(
1182                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1183         ldebugfs_add_vars(obd->obd_debugfs_entry, obd->obd_debugfs_vars, obd);
1184
1185         sptlrpc_lprocfs_cliobd_attach(obd);
1186         ptlrpc_lprocfs_register_obd(obd);
1187
1188         if (osp->opd_connect_mdt || !strstr(obd->obd_name, "osc"))
1189                 return;
1190
1191         /* If the real OSC is present which is the case for setups
1192          * with both server and clients on the same node then use
1193          * the OSC's proc root
1194          */
1195         osc = kset_find_obj(lustre_kset, "osc");
1196         if (osc) {
1197                 rc = sysfs_create_link(osc, &osp->opd_dt_dev.dd_kobj,
1198                                        obd->obd_name);
1199                 kobject_put(osc);
1200         }
1201
1202         osp->opd_debugfs = ldebugfs_add_symlink(obd->obd_name, "osc",
1203                                                 "../osp/%s", obd->obd_name);
1204         if (!osp->opd_debugfs)
1205                 CERROR("%s: failed to create OSC debugfs symlink\n",
1206                        obd->obd_name);
1207 }