Whamcloud - gitweb
New tag 2.15.63
[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,
198                                      char *buf)
199 {
200         struct dt_device *dt = container_of(kobj, struct dt_device,
201                                             dd_kobj);
202         struct osp_device *osp = dt2osp_dev(dt);
203
204         return sprintf(buf, "%u\n", osp->opd_sync_max_changes);
205 }
206
207
208 static ssize_t max_sync_changes_store(struct kobject *kobj,
209                                       struct attribute *attr,
210                                       const char *buffer, size_t count)
211 {
212         struct dt_device *dt = container_of(kobj, struct dt_device,
213                                             dd_kobj);
214         struct osp_device *osp = dt2osp_dev(dt);
215         int val;
216         int rc;
217
218         rc = kstrtoint(buffer, 0, &val);
219         if (rc)
220                 return rc;
221         if (val <= 0)
222                 return -ERANGE;
223         osp->opd_sync_max_changes = val;
224         return count;
225 }
226
227 LUSTRE_RW_ATTR(max_sync_changes);
228
229 /**
230  * Show maximum number of RPCs in flight allowed
231  *
232  * \param[in] m         seq_file handle
233  * \param[in] data      unused for single entry
234  * \retval              0 on success
235  * \retval              negative number on error
236  */
237 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
238                                        struct attribute *attr,
239                                        char *buf)
240 {
241         struct dt_device *dt = container_of(kobj, struct dt_device,
242                                             dd_kobj);
243         struct osp_device *osp = dt2osp_dev(dt);
244
245         return sprintf(buf, "%u\n", osp->opd_sync_max_rpcs_in_flight);
246 }
247
248 /**
249  * Change maximum number of RPCs in flight allowed
250  *
251  * \param[in] file      proc file
252  * \param[in] buffer    string which represents maximum number
253  * \param[in] count     \a buffer length
254  * \param[in] off       unused for single entry
255  * \retval              \a count on success
256  * \retval              negative number on error
257  */
258 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
259                                         struct attribute *attr,
260                                         const char *buffer,
261                                         size_t count)
262 {
263         struct dt_device *dt = container_of(kobj, struct dt_device,
264                                             dd_kobj);
265         struct osp_device *osp = dt2osp_dev(dt);
266         unsigned int val;
267         int rc;
268
269         rc = kstrtouint(buffer, 0, &val);
270         if (rc)
271                 return rc;
272
273         if (val == 0)
274                 return -ERANGE;
275
276         osp->opd_sync_max_rpcs_in_flight = val;
277         return count;
278 }
279 LUSTRE_RW_ATTR(max_rpcs_in_flight);
280
281 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
282                                            struct attribute *attr,
283                                            char *buf)
284 {
285         struct dt_device *dt = container_of(kobj, struct dt_device,
286                                             dd_kobj);
287         struct lu_device *lu = dt2lu_dev(dt);
288         struct obd_device *obd = lu->ld_obd;
289         u16 max;
290
291         max = obd_get_max_mod_rpcs_in_flight(&obd->u.cli);
292         return scnprintf(buf, PAGE_SIZE, "%hu\n", max);
293 }
294
295 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
296                                             struct attribute *attr,
297                                             const char *buffer,
298                                             size_t count)
299 {
300         struct dt_device *dt = container_of(kobj, struct dt_device,
301                                             dd_kobj);
302         struct lu_device *lu = dt2lu_dev(dt);
303         struct obd_device *obd = lu->ld_obd;
304         u16 val;
305         int rc;
306
307         rc = kstrtou16(buffer, 10, &val);
308         if (rc)
309                 return rc;
310
311         rc = obd_set_max_mod_rpcs_in_flight(&obd->u.cli, val);
312         if (rc)
313                 count = rc;
314
315         return count;
316 }
317 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
318
319 /**
320  * Show maximum number of RPCs in processing allowed
321  *
322  * \param[in] m         seq_file handle
323  * \param[in] data      unused
324  * \retval              0 on success
325  * \retval              negative number on error
326  */
327 static ssize_t max_rpcs_in_progress_show(struct kobject *kobj,
328                                          struct attribute *attr,
329                                          char *buf)
330 {
331         struct dt_device *dt = container_of(kobj, struct dt_device,
332                                             dd_kobj);
333         struct osp_device *osp = dt2osp_dev(dt);
334
335         return sprintf(buf, "%u\n", osp->opd_sync_max_rpcs_in_progress);
336 }
337
338 /**
339  * Change maximum number of RPCs in processing allowed
340  *
341  * \param[in] file      proc file
342  * \param[in] buffer    string which represents maximum number
343  * \param[in] count     \a buffer length
344  * \param[in] off       unused for single entry
345  * \retval              \a count on success
346  * \retval              negative number on error
347  */
348 static ssize_t max_rpcs_in_progress_store(struct kobject *kobj,
349                                           struct attribute *attr,
350                                           const char *buffer,
351                                           size_t count)
352 {
353         struct dt_device *dt = container_of(kobj, struct dt_device,
354                                             dd_kobj);
355         struct osp_device *osp = dt2osp_dev(dt);
356         unsigned int val;
357         int rc;
358
359         rc = kstrtouint(buffer, 0, &val);
360         if (rc)
361                 return rc;
362
363         if (val == 0)
364                 return -ERANGE;
365
366         osp->opd_sync_max_rpcs_in_progress = val;
367
368         return count;
369 }
370 LUSTRE_RW_ATTR(max_rpcs_in_progress);
371
372 /**
373  * Show number of objects to precreate next time
374  *
375  * \param[in] m         seq_file handle
376  * \param[in] data      unused for single entry
377  * \retval              0 on success
378  * \retval              negative number on error
379  */
380 static ssize_t create_count_show(struct kobject *kobj,
381                                  struct attribute *attr,
382                                  char *buf)
383 {
384         struct dt_device *dt = container_of(kobj, struct dt_device,
385                                             dd_kobj);
386         struct osp_device *osp = dt2osp_dev(dt);
387
388         if (!osp->opd_pre)
389                 return -EINVAL;
390
391         return sprintf(buf, "%d\n", osp->opd_pre_create_count);
392 }
393
394 /**
395  * Change number of objects to precreate next time
396  *
397  * \param[in] file      proc file
398  * \param[in] buffer    string which represents number of objects to precreate
399  * \param[in] count     \a buffer length
400  * \param[in] off       unused for single entry
401  * \retval              \a count on success
402  * \retval              negative number on error
403  */
404 static ssize_t create_count_store(struct kobject *kobj, struct attribute *attr,
405                                   const char *buffer, size_t count)
406 {
407         struct dt_device *dt = container_of(kobj, struct dt_device,
408                                             dd_kobj);
409         struct osp_device *osp = dt2osp_dev(dt);
410         unsigned int val;
411         int rc;
412
413         if (!osp->opd_pre)
414                 return -EINVAL;
415
416         rc = kstrtouint(buffer, 0, &val);
417         if (rc)
418                 return rc;
419
420         /* The MDT ALWAYS needs to limit the precreate count to
421          * OST_MAX_PRECREATE, and the constant cannot be changed
422          * because it is a value shared between the OSP and OST
423          * that is the maximum possible number of objects that will
424          * ever be handled by MDT->OST recovery processing.
425          *
426          * The OSP enforces the pre_create_count to amaximum of
427          * one half of opd_pre_max_create_count.
428          *
429          * If the OST ever gets a request to delete more orphans,
430          * this implies that something has gone badly on the MDT
431          * and the OST will refuse to delete so much data from the
432          * filesystem as a safety measure.
433          */
434         if (val < OST_MIN_PRECREATE)
435                 return -ERANGE;
436         if (val > osp->opd_pre_max_create_count / 2)
437                 val = osp->opd_pre_max_create_count / 2;
438
439         /* set to largest value <= 32, 64, 128 or a multiple of 256 */
440         if (val > 256)
441                 osp->opd_pre_create_count = val & 0xffffff00;
442         else
443                 osp->opd_pre_create_count = rounddown_pow_of_two(val);
444
445         if (osp->opd_pre_create_count + osp->opd_pre_reserved >
446             osp_objs_precreated(osp))
447                 wake_up(&osp->opd_pre_waitq);
448
449         return count;
450 }
451 LUSTRE_RW_ATTR(create_count);
452
453 /**
454  * Show maximum number of objects to precreate
455  *
456  * \param[in] m         seq_file handle
457  * \param[in] data      unused for single entry
458  * \retval              0 on success
459  * \retval              negative number on error
460  */
461 static ssize_t max_create_count_show(struct kobject *kobj,
462                                      struct attribute *attr,
463                                      char *buf)
464 {
465         struct dt_device *dt = container_of(kobj, struct dt_device,
466                                             dd_kobj);
467         struct osp_device *osp = dt2osp_dev(dt);
468
469         if (!osp->opd_pre)
470                 return -EINVAL;
471
472         return sprintf(buf, "%d\n", osp->opd_pre_max_create_count);
473 }
474
475 /**
476  * Change maximum number of objects to precreate
477  *
478  * \param[in] file      proc file
479  * \param[in] buffer    string which represents maximum number
480  * \param[in] count     \a buffer length
481  * \param[in] off       unused for single entry
482  * \retval              \a count on success
483  * \retval              negative number on error
484  */
485 static ssize_t max_create_count_store(struct kobject *kobj,
486                                       struct attribute *attr,
487                                       const char *buffer, size_t count)
488 {
489         struct dt_device *dt = container_of(kobj, struct dt_device,
490                                             dd_kobj);
491         struct osp_device *osp = dt2osp_dev(dt);
492         unsigned int val;
493         int rc;
494
495         if (!osp->opd_pre)
496                 return -EINVAL;
497
498         rc = kstrtouint(buffer, 0, &val);
499         if (rc)
500                 return rc;
501
502         if (val && (val < OST_MIN_PRECREATE ||
503                     val > OST_MAX_PRECREATE))
504                 return -ERANGE;
505
506         if (osp->opd_pre_create_count > val)
507                 osp->opd_pre_create_count = val;
508
509         /* Can be 0 after setting max_create_count to 0 */
510         if (osp->opd_pre_create_count == 0 && val != 0)
511                 osp->opd_pre_create_count = OST_MIN_PRECREATE;
512
513         osp->opd_pre_max_create_count = val;
514
515         return count;
516 }
517 LUSTRE_RW_ATTR(max_create_count);
518
519 /**
520  * Show last id to assign in creation
521  *
522  * \param[in] m         seq_file handle
523  * \param[in] data      unused for single entry
524  * \retval              0 on success
525  * \retval              negative number on error
526  */
527 static ssize_t prealloc_next_id_show(struct kobject *kobj,
528                                      struct attribute *attr,
529                                      char *buf)
530 {
531         struct dt_device *dt = container_of(kobj, struct dt_device,
532                                             dd_kobj);
533         struct osp_device *osp = dt2osp_dev(dt);
534         struct lu_fid *fid;
535         u64 id;
536         __u64 seq_width;
537
538         if (!osp->opd_pre)
539                 return -EINVAL;
540
541         fid = &osp->opd_pre_used_fid;
542         seq_width = osp->opd_pre_seq_width;
543         if (fid_is_idif(fid)) {
544                 id = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
545                 if (unlikely(id >= min(IDIF_MAX_OID, seq_width)))
546                         id = 1;
547                 else
548                         id++;
549         } else {
550                 id = unlikely(fid_oid(fid) >= min(OBIF_MAX_OID, seq_width)) ?
551                         1 : fid_oid(fid) + 1;
552         }
553
554         return sprintf(buf, "%llu\n", id);
555 }
556 LUSTRE_RO_ATTR(prealloc_next_id);
557
558 /**
559  * Show last created id OST reported
560  *
561  * \param[in] m         seq_file handle
562  * \param[in] data      unused for single entry
563  * \retval              0 on success
564  * \retval              negative number on error
565  */
566
567 static ssize_t prealloc_last_id_show(struct kobject *kobj,
568                                      struct attribute *attr,
569                                      char *buf)
570 {
571         struct dt_device *dt = container_of(kobj, struct dt_device,
572                                             dd_kobj);
573         struct osp_device *osp = dt2osp_dev(dt);
574         struct lu_fid *fid;
575         u64 id;
576
577         if (!osp->opd_pre)
578                 return -EINVAL;
579
580         fid = &osp->opd_pre_last_created_fid;
581         id = fid_is_idif(fid) ?
582                          fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid)) :
583                          fid_oid(fid);
584
585         return sprintf(buf, "%llu\n", id);
586 }
587 LUSTRE_RO_ATTR(prealloc_last_id);
588
589 /**
590  * Show next FID sequence to precreate
591  *
592  * \param[in] m         seq_file handle
593  * \param[in] data      unused for single entry
594  * \retval              0 on success
595  * \retval              negative number on error
596  */
597 static ssize_t prealloc_next_seq_show(struct kobject *kobj,
598                                       struct attribute *attr,
599                                       char *buf)
600 {
601         struct dt_device *dt = container_of(kobj, struct dt_device,
602                                             dd_kobj);
603         struct osp_device *osp = dt2osp_dev(dt);
604         struct lu_fid *fid;
605
606         if (!osp->opd_pre)
607                 return -EINVAL;
608
609         fid = &osp->opd_pre_used_fid;
610         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
611                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
612 }
613 LUSTRE_RO_ATTR(prealloc_next_seq);
614
615 /**
616  * Show last created FID sequence OST reported
617  *
618  * \param[in] m         seq_file handle
619  * \param[in] data      unused for single entry
620  * \retval              0 on success
621  * \retval              negative number on error
622  */
623 static ssize_t prealloc_last_seq_show(struct kobject *kobj,
624                                       struct attribute *attr,
625                                       char *buf)
626 {
627         struct dt_device *dt = container_of(kobj, struct dt_device,
628                                             dd_kobj);
629         struct osp_device *osp = dt2osp_dev(dt);
630         struct lu_fid *fid;
631
632         if (!osp->opd_pre)
633                 return -EINVAL;
634
635         fid = &osp->opd_pre_last_created_fid;
636         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
637                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
638 }
639 LUSTRE_RO_ATTR(prealloc_last_seq);
640
641 /**
642  * Show the number of ids reserved by declare
643  *
644  * \param[in] m         seq_file handle
645  * \param[in] data      unused for single entry
646  * \retval              0 on success
647  * \retval              negative number on error
648  */
649 static ssize_t prealloc_reserved_show(struct kobject *kobj,
650                                       struct attribute *attr,
651                                       char *buf)
652 {
653         struct dt_device *dt = container_of(kobj, struct dt_device,
654                                             dd_kobj);
655         struct osp_device *osp = dt2osp_dev(dt);
656
657         if (!osp->opd_pre)
658                 return -EINVAL;
659
660         return sprintf(buf, "%llu\n", osp->opd_pre_reserved);
661 }
662 LUSTRE_RO_ATTR(prealloc_reserved);
663
664 /**
665  * Show interval (in seconds) to update statfs data
666  *
667  * \param[in] m         seq_file handle
668  * \param[in] data      unused for single entry
669  * \retval              0 on success
670  * \retval              negative number on error
671  */
672 static ssize_t maxage_show(struct kobject *kobj,
673                            struct attribute *attr,
674                            char *buf)
675 {
676         struct dt_device *dt = container_of(kobj, struct dt_device,
677                                             dd_kobj);
678         struct osp_device *osp = dt2osp_dev(dt);
679
680         return sprintf(buf, "%lld\n", osp->opd_statfs_maxage);
681 }
682
683 /**
684  * Change interval to update statfs data
685  *
686  * \param[in] file      proc file
687  * \param[in] buffer    string which represents statfs interval (in seconds)
688  * \param[in] count     \a buffer length
689  * \param[in] off       unused for single entry
690  * \retval              \a count on success
691  * \retval              negative number on error
692  */
693 static ssize_t maxage_store(struct kobject *kobj, struct attribute *attr,
694                             const char *buffer, size_t count)
695 {
696         struct dt_device *dt = container_of(kobj, struct dt_device,
697                                             dd_kobj);
698         struct osp_device *osp = dt2osp_dev(dt);
699         unsigned int val;
700         int rc;
701
702         rc = kstrtouint(buffer, 0, &val);
703         if (rc)
704                 return rc;
705
706         if (val == 0)
707                 return -ERANGE;
708
709         osp->opd_statfs_maxage = val;
710
711         return count;
712 }
713 LUSTRE_RW_ATTR(maxage);
714
715 /**
716  * Show current precreation status: output 0 means success, otherwise negative
717  * number is printed
718  *
719  * \param[in] m         seq_file handle
720  * \param[in] data      unused for single entry
721  * \retval              0 on success
722  * \retval              negative number on error
723  */
724 static ssize_t prealloc_status_show(struct kobject *kobj,
725                                     struct attribute *attr,
726                                     char *buf)
727 {
728         struct dt_device *dt = container_of(kobj, struct dt_device,
729                                             dd_kobj);
730         struct osp_device *osp = dt2osp_dev(dt);
731
732         if (!osp->opd_pre)
733                 return -EINVAL;
734
735         return sprintf(buf, "%d\n", osp->opd_pre_status);
736 }
737 LUSTRE_RO_ATTR(prealloc_status);
738
739 static ssize_t prealloc_force_new_seq_show(struct kobject *kobj,
740                                            struct attribute *attr,
741                                            char *buf)
742 {
743         struct dt_device *dt = container_of(kobj, struct dt_device,
744                                             dd_kobj);
745         struct osp_device *osp = dt2osp_dev(dt);
746
747         if (!osp->opd_pre)
748                 return -EINVAL;
749
750         return scnprintf(buf, PAGE_SIZE, "%d\n", osp->opd_pre_force_new_seq);
751 }
752
753 static ssize_t prealloc_force_new_seq_store(struct kobject *kobj,
754                                             struct attribute *attr,
755                                             const char *buffer,
756                                             size_t count)
757 {
758         struct dt_device *dt = container_of(kobj, struct dt_device,
759                                             dd_kobj);
760         struct osp_device *osp = dt2osp_dev(dt);
761         bool val;
762         int rc;
763
764         if (!osp->opd_pre)
765                 return -EINVAL;
766
767         rc = kstrtobool(buffer, &val);
768         if (rc)
769                 return rc;
770
771         osp->opd_pre_force_new_seq = val;
772
773         return count;
774 }
775 LUSTRE_RW_ATTR(prealloc_force_new_seq);
776
777 /**
778  * Show the number of RPCs in processing (including uncommitted by OST) plus
779  * changes to sync, i.e. this is the total number of changes OST needs to apply
780  * and commit.
781  *
782  * This counter is used to determine if OST has space returned. A zero value
783  * indicates that OST storage space consumed by destroyed objects has been freed
784  * on disk, the associated llog records have been cleared, and no synchronous
785  * RPC are being processed.
786  *
787  * \param[in] m         seq_file handle
788  * \param[in] data      unused for single entry
789  * \retval              0 on success
790  * \retval              negative number on error
791  */
792 static ssize_t destroys_in_flight_show(struct kobject *kobj,
793                                        struct attribute *attr,
794                                        char *buf)
795 {
796         struct dt_device *dt = container_of(kobj, struct dt_device,
797                                             dd_kobj);
798         struct osp_device *osp = dt2osp_dev(dt);
799
800         return sprintf(buf, "%u\n",
801                        atomic_read(&osp->opd_sync_rpcs_in_progress) +
802                        atomic_read(&osp->opd_sync_changes));
803 }
804 LUSTRE_RO_ATTR(destroys_in_flight);
805
806 /**
807  * Show changes synced from previous mount
808  *
809  * \param[in] m         seq_file handle
810  * \param[in] data      unused for single entry
811  * \retval              0 on success
812  * \retval              negative number on error
813  */
814 static ssize_t old_sync_processed_show(struct kobject *kobj,
815                                        struct attribute *attr,
816                                        char *buf)
817 {
818         struct dt_device *dt = container_of(kobj, struct dt_device,
819                                             dd_kobj);
820         struct osp_device *osp = dt2osp_dev(dt);
821
822         return sprintf(buf, "%d\n", osp->opd_sync_prev_done);
823 }
824 LUSTRE_RO_ATTR(old_sync_processed);
825
826 /**
827  * Show maximum number of RPCs in flight
828  *
829  * \param[in] m         seq_file handle
830  * \param[in] data      unused for single entry
831  * \retval              0 on success
832  * \retval              negative number on error
833  */
834 static ssize_t lfsck_max_rpcs_in_flight_show(struct kobject *kobj,
835                                              struct attribute *attr,
836                                              char *buf)
837 {
838         struct dt_device *dt = container_of(kobj, struct dt_device,
839                                             dd_kobj);
840         struct lu_device *lu = dt2lu_dev(dt);
841         struct obd_device *obd = lu->ld_obd;
842         u32 max;
843
844         max = obd_get_max_rpcs_in_flight(&obd->u.cli);
845         return sprintf(buf, "%u\n", max);
846 }
847
848 /**
849  * Change maximum number of RPCs in flight
850  *
851  * \param[in] file      proc file
852  * \param[in] buffer    string which represents maximum number of RPCs in flight
853  * \param[in] count     \a buffer length
854  * \param[in] off       unused for single entry
855  * \retval              \a count on success
856  * \retval              negative number on error
857  */
858 static ssize_t lfsck_max_rpcs_in_flight_store(struct kobject *kobj,
859                                               struct attribute *attr,
860                                               const char *buffer,
861                                               size_t count)
862 {
863         struct dt_device *dt = container_of(kobj, struct dt_device,
864                                             dd_kobj);
865         struct lu_device *lu = dt2lu_dev(dt);
866         struct obd_device *obd = lu->ld_obd;
867         unsigned int val;
868         int rc;
869
870         rc = kstrtouint(buffer, 0, &val);
871         if (rc)
872                 return rc;
873
874         rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
875         return rc ? rc : count;
876 }
877 LUSTRE_RW_ATTR(lfsck_max_rpcs_in_flight);
878
879 ssize_t ping_show(struct kobject *kobj, struct attribute *attr,
880                   char *buffer)
881 {
882         struct dt_device *dt = container_of(kobj, struct dt_device,
883                                             dd_kobj);
884         struct lu_device *lu = dt2lu_dev(dt);
885         struct obd_device *obd = lu->ld_obd;
886         int rc;
887
888         rc = ptlrpc_obd_ping(obd);
889
890         return rc;
891 }
892 LUSTRE_RO_ATTR(ping);
893
894 static ssize_t osp_conn_uuid_show(struct kobject *kobj, struct attribute *attr,
895                                   char *buf)
896 {
897         struct dt_device *dt = container_of(kobj, struct dt_device,
898                                             dd_kobj);
899         struct lu_device *lu = dt2lu_dev(dt);
900         struct obd_device *obd = lu->ld_obd;
901         struct obd_import *imp;
902         struct ptlrpc_connection *conn;
903         ssize_t count;
904
905         with_imp_locked(obd, imp, count) {
906                 conn = imp->imp_connection;
907                 if (conn)
908                         count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
909                 else
910                         count = sprintf(buf, "%s\n", "<none>");
911         }
912
913         return count;
914 }
915
916 LUSTRE_ATTR(ost_conn_uuid, 0444, osp_conn_uuid_show, NULL);
917 LUSTRE_ATTR(mdt_conn_uuid, 0444, osp_conn_uuid_show, NULL);
918
919 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, connect_flags);
920 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, server_uuid);
921 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, timeouts);
922
923 LDEBUGFS_SEQ_FOPS_RW_TYPE(osp, import);
924 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, state);
925
926 static int osp_rpc_stats_seq_show(struct seq_file *seq, void *v)
927 {
928         struct obd_device *dev = seq->private;
929
930         return obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
931 }
932
933 static ssize_t osp_rpc_stats_seq_write(struct file *file,
934                                        const char __user *buf,
935                                        size_t len, loff_t *off)
936 {
937         struct seq_file *seq = file->private_data;
938         struct obd_device *dev = seq->private;
939         struct client_obd *cli = &dev->u.cli;
940
941         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
942         cli->cl_mod_rpcs_init = ktime_get_real();
943
944         return len;
945 }
946 LDEBUGFS_SEQ_FOPS(osp_rpc_stats);
947
948 /**
949  * Show high watermark (in megabytes). If available free space at OST is greater
950  * than high watermark and object allocation for OST is disabled, enable it.
951  */
952 static ssize_t reserved_mb_high_show(struct kobject *kobj,
953                                      struct attribute *attr,
954                                      char *buf)
955 {
956         struct dt_device *dt = container_of(kobj, struct dt_device,
957                                             dd_kobj);
958         struct osp_device *osp = dt2osp_dev(dt);
959
960         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_high);
961 }
962
963 /**
964  * Change high watermark
965  */
966 static ssize_t reserved_mb_high_store(struct kobject *kobj,
967                                       struct attribute *attr,
968                                       const char *buffer,
969                                       size_t count)
970 {
971         struct dt_device *dt = container_of(kobj, struct dt_device,
972                                             dd_kobj);
973         struct osp_device *osp = dt2osp_dev(dt);
974         u64 val;
975         int rc;
976
977         rc = sysfs_memparse(buffer, count, &val, "MiB");
978         if (rc < 0)
979                 return rc;
980         val >>= 20;
981         if (val < 1)
982                 return -ERANGE;
983
984         spin_lock(&osp->opd_pre_lock);
985         osp->opd_reserved_mb_high = val;
986         if (val <= osp->opd_reserved_mb_low)
987                 osp->opd_reserved_mb_low = val - 1;
988         spin_unlock(&osp->opd_pre_lock);
989
990         return count;
991 }
992 LUSTRE_RW_ATTR(reserved_mb_high);
993
994 /**
995  * Show low watermark (in megabytes). If available free space at OST is less
996  * than low watermark, object allocation for OST is disabled.
997  */
998 static ssize_t reserved_mb_low_show(struct kobject *kobj,
999                                     struct attribute *attr,
1000                                     char *buf)
1001 {
1002         struct dt_device *dt = container_of(kobj, struct dt_device,
1003                                             dd_kobj);
1004         struct osp_device *osp = dt2osp_dev(dt);
1005
1006         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_low);
1007 }
1008
1009 /**
1010  * Change low watermark
1011  */
1012 static ssize_t reserved_mb_low_store(struct kobject *kobj,
1013                                      struct attribute *attr,
1014                                      const char *buffer,
1015                                      size_t count)
1016 {
1017         struct dt_device *dt = container_of(kobj, struct dt_device,
1018                                             dd_kobj);
1019         struct osp_device *osp = dt2osp_dev(dt);
1020         u64 val;
1021         int rc;
1022
1023         rc = sysfs_memparse(buffer, count, &val, "MiB");
1024         if (rc < 0)
1025                 return rc;
1026         val >>= 20;
1027
1028         spin_lock(&osp->opd_pre_lock);
1029         osp->opd_reserved_mb_low = val;
1030         if (val >= osp->opd_reserved_mb_high)
1031                 osp->opd_reserved_mb_high = val + 1;
1032         spin_unlock(&osp->opd_pre_lock);
1033
1034         return count;
1035 }
1036 LUSTRE_RW_ATTR(reserved_mb_low);
1037
1038 /**
1039  * Show high watermark of inode.
1040  */
1041 static ssize_t reserved_ino_high_show(struct kobject *kobj,
1042                                       struct attribute *attr,
1043                                       char *buf)
1044 {
1045         struct dt_device *dt = container_of(kobj, struct dt_device,
1046                                             dd_kobj);
1047         struct osp_device *osp = dt2osp_dev(dt);
1048
1049         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_high);
1050 }
1051
1052 /**
1053  * Change high watermark of inode.
1054  */
1055 static ssize_t reserved_ino_high_store(struct kobject *kobj,
1056                                        struct attribute *attr,
1057                                        const char *buffer,
1058                                        size_t count)
1059 {
1060         struct dt_device *dt = container_of(kobj, struct dt_device,
1061                                             dd_kobj);
1062         struct osp_device *osp = dt2osp_dev(dt);
1063         unsigned int val;
1064         int rc;
1065
1066         rc = kstrtouint(buffer, 0, &val);
1067         if (rc < 0)
1068                 return rc;
1069         if (val < 1)
1070                 return -ERANGE;
1071
1072         spin_lock(&osp->opd_pre_lock);
1073         osp->opd_reserved_ino_high = val;
1074         if (val <= osp->opd_reserved_ino_low)
1075                 osp->opd_reserved_ino_low = val >> 1;
1076         spin_unlock(&osp->opd_pre_lock);
1077
1078         return count;
1079 }
1080 LUSTRE_RW_ATTR(reserved_ino_high);
1081
1082 /**
1083  * Show low watermark.
1084  */
1085 static ssize_t reserved_ino_low_show(struct kobject *kobj,
1086                                      struct attribute *attr,
1087                                      char *buf)
1088 {
1089         struct dt_device *dt = container_of(kobj, struct dt_device,
1090                                             dd_kobj);
1091         struct osp_device *osp = dt2osp_dev(dt);
1092
1093         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_low);
1094 }
1095
1096 /**
1097  * Change low watermark
1098  */
1099 static ssize_t reserved_ino_low_store(struct kobject *kobj,
1100                                       struct attribute *attr,
1101                                       const char *buffer,
1102                                       size_t count)
1103 {
1104         struct dt_device *dt = container_of(kobj, struct dt_device,
1105                                             dd_kobj);
1106         struct osp_device *osp = dt2osp_dev(dt);
1107         unsigned int val;
1108         int rc;
1109
1110         rc = kstrtouint(buffer, 0, &val);
1111         if (rc < 0)
1112                 return rc;
1113
1114         if (val & (1UL << 31))
1115                 return -EOVERFLOW;
1116
1117         spin_lock(&osp->opd_pre_lock);
1118         osp->opd_reserved_ino_low = val;
1119         if (val >= osp->opd_reserved_ino_high)
1120                 osp->opd_reserved_ino_high = val << 1;
1121         spin_unlock(&osp->opd_pre_lock);
1122
1123         return count;
1124 }
1125 LUSTRE_RW_ATTR(reserved_ino_low);
1126
1127 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
1128                                 const char *buffer, size_t count)
1129 {
1130         struct dt_device *dt = container_of(kobj, struct dt_device,
1131                                             dd_kobj);
1132         struct lu_env env;
1133         int rc;
1134
1135         rc = lu_env_init(&env, LCT_LOCAL);
1136         if (rc)
1137                 return rc;
1138
1139         rc = dt_sync(&env, dt);
1140         lu_env_fini(&env);
1141
1142         return rc == 0 ? count : rc;
1143 }
1144 LUSTRE_WO_ATTR(force_sync);
1145
1146 static struct ldebugfs_vars ldebugfs_osp_obd_vars[] = {
1147         { .name =       "connect_flags",
1148           .fops =       &osp_connect_flags_fops         },
1149         { .name =       "ost_server_uuid",
1150           .fops =       &osp_server_uuid_fops           },
1151         { .name =       "timeouts",
1152           .fops =       &osp_timeouts_fops              },
1153         { .name =       "import",
1154           .fops =       &osp_import_fops                },
1155         { .name =       "state",
1156           .fops =       &osp_state_fops                 },
1157         { NULL }
1158 };
1159
1160 static struct ldebugfs_vars ldebugfs_osp_md_vars[] = {
1161         { .name =       "connect_flags",
1162           .fops =       &osp_connect_flags_fops         },
1163         { .name =       "mdt_server_uuid",
1164           .fops =       &osp_server_uuid_fops           },
1165         { .name =       "timeouts",
1166           .fops =       &osp_timeouts_fops              },
1167         { .name =       "import",
1168           .fops =       &osp_import_fops                },
1169         { .name =       "state",
1170           .fops =       &osp_state_fops                 },
1171         { .name =       "rpc_stats",
1172           .fops =       &osp_rpc_stats_fops             },
1173         { NULL }
1174 };
1175
1176 static struct attribute *osp_obd_attrs[] = {
1177         /* First two for compatiability reasons */
1178         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1179         &lustre_attr_destroys_in_flight.attr,
1180         &lustre_attr_active.attr,
1181         &lustre_attr_max_rpcs_in_flight.attr,
1182         &lustre_attr_max_rpcs_in_progress.attr,
1183         &lustre_attr_maxage.attr,
1184         &lustre_attr_ost_conn_uuid.attr,
1185         &lustre_attr_ping.attr,
1186         &lustre_attr_prealloc_status.attr,
1187         &lustre_attr_prealloc_next_id.attr,
1188         &lustre_attr_prealloc_last_id.attr,
1189         &lustre_attr_prealloc_next_seq.attr,
1190         &lustre_attr_prealloc_last_seq.attr,
1191         &lustre_attr_prealloc_reserved.attr,
1192         &lustre_attr_prealloc_force_new_seq.attr,
1193         &lustre_attr_sync_in_flight.attr,
1194         &lustre_attr_sync_in_progress.attr,
1195         &lustre_attr_sync_changes.attr,
1196         &lustre_attr_max_sync_changes.attr,
1197         &lustre_attr_force_sync.attr,
1198         &lustre_attr_old_sync_processed.attr,
1199         &lustre_attr_create_count.attr,
1200         &lustre_attr_max_create_count.attr,
1201         &lustre_attr_reserved_mb_high.attr,
1202         &lustre_attr_reserved_mb_low.attr,
1203         &lustre_attr_reserved_ino_high.attr,
1204         &lustre_attr_reserved_ino_low.attr,
1205         NULL,
1206 };
1207
1208 KOBJ_ATTRIBUTE_GROUPS(osp_obd); /* creates osp_obd_groups from osp_obd_attrs */
1209
1210 static struct attribute *osp_md_attrs[] = {
1211         /* First two for compatiability reasons */
1212         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1213         &lustre_attr_destroys_in_flight.attr,
1214         &lustre_attr_active.attr,
1215         &lustre_attr_max_rpcs_in_flight.attr,
1216         &lustre_attr_max_mod_rpcs_in_flight.attr,
1217         &lustre_attr_max_rpcs_in_progress.attr,
1218         &lustre_attr_maxage.attr,
1219         &lustre_attr_mdt_conn_uuid.attr,
1220         &lustre_attr_ping.attr,
1221         &lustre_attr_prealloc_status.attr,
1222         &lustre_attr_reserved_mb_high.attr,
1223         &lustre_attr_reserved_mb_low.attr,
1224         &lustre_attr_reserved_ino_high.attr,
1225         &lustre_attr_reserved_ino_low.attr,
1226         NULL,
1227 };
1228
1229 KOBJ_ATTRIBUTE_GROUPS(osp_md); /* creates osp_md_groups from osp_md_attrs */
1230
1231 void osp_tunables_fini(struct osp_device *osp)
1232 {
1233         struct obd_device *obd = osp->opd_obd;
1234         struct kobject *osc;
1235
1236         osc = kset_find_obj(lustre_kset, "osc");
1237         if (osc) {
1238                 sysfs_remove_link(osc, obd->obd_name);
1239                 kobject_put(osc);
1240         }
1241
1242         debugfs_remove_recursive(osp->opd_debugfs);
1243         osp->opd_debugfs = NULL;
1244
1245         ptlrpc_lprocfs_unregister_obd(obd);
1246
1247         debugfs_remove_recursive(obd->obd_debugfs_entry);
1248         obd->obd_debugfs_entry = NULL;
1249
1250         dt_tunables_fini(&osp->opd_dt_dev);
1251 }
1252
1253 /**
1254  * Initialize OSP sysfs / debugfs
1255  *
1256  * param[in] osp        OSP device
1257  */
1258 void osp_tunables_init(struct osp_device *osp)
1259 {
1260         struct obd_device *obd = osp->opd_obd;
1261         struct kobject *osc;
1262         int rc;
1263
1264         if (osp->opd_connect_mdt) {
1265                 osp->opd_dt_dev.dd_ktype.default_groups =
1266                         KOBJ_ATTR_GROUPS(osp_md);
1267                 obd->obd_debugfs_vars = ldebugfs_osp_md_vars;
1268         } else {
1269                 osp->opd_dt_dev.dd_ktype.default_groups =
1270                         KOBJ_ATTR_GROUPS(osp_obd);
1271                 obd->obd_debugfs_vars = ldebugfs_osp_obd_vars;
1272         }
1273
1274         rc = dt_tunables_init(&osp->opd_dt_dev, obd->obd_type, obd->obd_name,
1275                               NULL);
1276         if (rc) {
1277                 CERROR("%s: failed to setup DT tunables: %d\n",
1278                        obd->obd_name, rc);
1279                 return;
1280         }
1281
1282         /* Since we register the obd device with ptlrpc / sptlrpc we
1283          * have to register debugfs with obd_device
1284          */
1285         obd->obd_debugfs_entry = debugfs_create_dir(
1286                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1287         ldebugfs_add_vars(obd->obd_debugfs_entry, obd->obd_debugfs_vars, obd);
1288
1289         sptlrpc_lprocfs_cliobd_attach(obd);
1290         ptlrpc_lprocfs_register_obd(obd);
1291
1292         if (osp->opd_connect_mdt || !strstr(obd->obd_name, "osc"))
1293                 return;
1294
1295         /* If the real OSC is present which is the case for setups
1296          * with both server and clients on the same node then use
1297          * the OSC's proc root
1298          */
1299         osc = kset_find_obj(lustre_kset, "osc");
1300         if (osc) {
1301                 rc = sysfs_create_link(osc, &osp->opd_dt_dev.dd_kobj,
1302                                        obd->obd_name);
1303                 kobject_put(osc);
1304         }
1305
1306         osp->opd_debugfs = ldebugfs_add_symlink(obd->obd_name, "osc",
1307                                                 "../osp/%s", obd->obd_name);
1308         if (!osp->opd_debugfs)
1309                 CERROR("%s: failed to create OSC debugfs symlink\n",
1310                        obd->obd_name);
1311 }