Whamcloud - gitweb
LU-8191 lustre: convert osp,osd,osc,ofd functions to static
[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         return count;
446 }
447 LUSTRE_RW_ATTR(create_count);
448
449 /**
450  * Show maximum number of objects to precreate
451  *
452  * \param[in] m         seq_file handle
453  * \param[in] data      unused for single entry
454  * \retval              0 on success
455  * \retval              negative number on error
456  */
457 static ssize_t max_create_count_show(struct kobject *kobj,
458                                      struct attribute *attr,
459                                      char *buf)
460 {
461         struct dt_device *dt = container_of(kobj, struct dt_device,
462                                             dd_kobj);
463         struct osp_device *osp = dt2osp_dev(dt);
464
465         if (!osp->opd_pre)
466                 return -EINVAL;
467
468         return sprintf(buf, "%d\n", osp->opd_pre_max_create_count);
469 }
470
471 /**
472  * Change maximum number of objects to precreate
473  *
474  * \param[in] file      proc file
475  * \param[in] buffer    string which represents maximum number
476  * \param[in] count     \a buffer length
477  * \param[in] off       unused for single entry
478  * \retval              \a count on success
479  * \retval              negative number on error
480  */
481 static ssize_t max_create_count_store(struct kobject *kobj,
482                                       struct attribute *attr,
483                                       const char *buffer, size_t count)
484 {
485         struct dt_device *dt = container_of(kobj, struct dt_device,
486                                             dd_kobj);
487         struct osp_device *osp = dt2osp_dev(dt);
488         unsigned int val;
489         int rc;
490
491         if (!osp->opd_pre)
492                 return -EINVAL;
493
494         rc = kstrtouint(buffer, 0, &val);
495         if (rc)
496                 return rc;
497
498         if (val && (val < OST_MIN_PRECREATE ||
499                     val > OST_MAX_PRECREATE))
500                 return -ERANGE;
501
502         if (osp->opd_pre_create_count > val)
503                 osp->opd_pre_create_count = val;
504
505         /* Can be 0 after setting max_create_count to 0 */
506         if (osp->opd_pre_create_count == 0 && val != 0)
507                 osp->opd_pre_create_count = OST_MIN_PRECREATE;
508
509         osp->opd_pre_max_create_count = val;
510
511         return count;
512 }
513 LUSTRE_RW_ATTR(max_create_count);
514
515 /**
516  * Show last id to assign in creation
517  *
518  * \param[in] m         seq_file handle
519  * \param[in] data      unused for single entry
520  * \retval              0 on success
521  * \retval              negative number on error
522  */
523 static ssize_t prealloc_next_id_show(struct kobject *kobj,
524                                      struct attribute *attr,
525                                      char *buf)
526 {
527         struct dt_device *dt = container_of(kobj, struct dt_device,
528                                             dd_kobj);
529         struct osp_device *osp = dt2osp_dev(dt);
530         struct lu_fid *fid;
531         u64 id;
532         __u64 seq_width;
533
534         if (!osp->opd_pre)
535                 return -EINVAL;
536
537         fid = &osp->opd_pre_used_fid;
538         seq_width = osp->opd_pre_seq_width;
539         if (fid_is_idif(fid)) {
540                 id = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
541                 if (unlikely(id >= min(IDIF_MAX_OID, seq_width)))
542                         id = 1;
543                 else
544                         id++;
545         } else {
546                 id = unlikely(fid_oid(fid) >= min(OBIF_MAX_OID, seq_width)) ?
547                         1 : fid_oid(fid) + 1;
548         }
549
550         return sprintf(buf, "%llu\n", id);
551 }
552 LUSTRE_RO_ATTR(prealloc_next_id);
553
554 /**
555  * Show last created id OST reported
556  *
557  * \param[in] m         seq_file handle
558  * \param[in] data      unused for single entry
559  * \retval              0 on success
560  * \retval              negative number on error
561  */
562
563 static ssize_t prealloc_last_id_show(struct kobject *kobj,
564                                      struct attribute *attr,
565                                      char *buf)
566 {
567         struct dt_device *dt = container_of(kobj, struct dt_device,
568                                             dd_kobj);
569         struct osp_device *osp = dt2osp_dev(dt);
570         struct lu_fid *fid;
571         u64 id;
572
573         if (!osp->opd_pre)
574                 return -EINVAL;
575
576         fid = &osp->opd_pre_last_created_fid;
577         id = fid_is_idif(fid) ?
578                          fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid)) :
579                          fid_oid(fid);
580
581         return sprintf(buf, "%llu\n", id);
582 }
583 LUSTRE_RO_ATTR(prealloc_last_id);
584
585 /**
586  * Show next FID sequence to precreate
587  *
588  * \param[in] m         seq_file handle
589  * \param[in] data      unused for single entry
590  * \retval              0 on success
591  * \retval              negative number on error
592  */
593 static ssize_t prealloc_next_seq_show(struct kobject *kobj,
594                                       struct attribute *attr,
595                                       char *buf)
596 {
597         struct dt_device *dt = container_of(kobj, struct dt_device,
598                                             dd_kobj);
599         struct osp_device *osp = dt2osp_dev(dt);
600         struct lu_fid *fid;
601
602         if (!osp->opd_pre)
603                 return -EINVAL;
604
605         fid = &osp->opd_pre_used_fid;
606         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
607                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
608 }
609 LUSTRE_RO_ATTR(prealloc_next_seq);
610
611 /**
612  * Show last created FID sequence OST reported
613  *
614  * \param[in] m         seq_file handle
615  * \param[in] data      unused for single entry
616  * \retval              0 on success
617  * \retval              negative number on error
618  */
619 static ssize_t prealloc_last_seq_show(struct kobject *kobj,
620                                       struct attribute *attr,
621                                       char *buf)
622 {
623         struct dt_device *dt = container_of(kobj, struct dt_device,
624                                             dd_kobj);
625         struct osp_device *osp = dt2osp_dev(dt);
626         struct lu_fid *fid;
627
628         if (!osp->opd_pre)
629                 return -EINVAL;
630
631         fid = &osp->opd_pre_last_created_fid;
632         return sprintf(buf, "%#llx\n", fid_is_idif(fid) ?
633                        fid_seq(fid) & (~0xffff) : fid_seq(fid));
634 }
635 LUSTRE_RO_ATTR(prealloc_last_seq);
636
637 /**
638  * Show the number of ids reserved by declare
639  *
640  * \param[in] m         seq_file handle
641  * \param[in] data      unused for single entry
642  * \retval              0 on success
643  * \retval              negative number on error
644  */
645 static ssize_t prealloc_reserved_show(struct kobject *kobj,
646                                       struct attribute *attr,
647                                       char *buf)
648 {
649         struct dt_device *dt = container_of(kobj, struct dt_device,
650                                             dd_kobj);
651         struct osp_device *osp = dt2osp_dev(dt);
652
653         if (!osp->opd_pre)
654                 return -EINVAL;
655
656         return sprintf(buf, "%llu\n", osp->opd_pre_reserved);
657 }
658 LUSTRE_RO_ATTR(prealloc_reserved);
659
660 /**
661  * Show interval (in seconds) to update statfs data
662  *
663  * \param[in] m         seq_file handle
664  * \param[in] data      unused for single entry
665  * \retval              0 on success
666  * \retval              negative number on error
667  */
668 static ssize_t maxage_show(struct kobject *kobj,
669                            struct attribute *attr,
670                            char *buf)
671 {
672         struct dt_device *dt = container_of(kobj, struct dt_device,
673                                             dd_kobj);
674         struct osp_device *osp = dt2osp_dev(dt);
675
676         return sprintf(buf, "%lld\n", osp->opd_statfs_maxage);
677 }
678
679 /**
680  * Change interval to update statfs data
681  *
682  * \param[in] file      proc file
683  * \param[in] buffer    string which represents statfs interval (in seconds)
684  * \param[in] count     \a buffer length
685  * \param[in] off       unused for single entry
686  * \retval              \a count on success
687  * \retval              negative number on error
688  */
689 static ssize_t maxage_store(struct kobject *kobj, struct attribute *attr,
690                             const char *buffer, size_t count)
691 {
692         struct dt_device *dt = container_of(kobj, struct dt_device,
693                                             dd_kobj);
694         struct osp_device *osp = dt2osp_dev(dt);
695         unsigned int val;
696         int rc;
697
698         rc = kstrtouint(buffer, 0, &val);
699         if (rc)
700                 return rc;
701
702         if (val == 0)
703                 return -ERANGE;
704
705         osp->opd_statfs_maxage = val;
706
707         return count;
708 }
709 LUSTRE_RW_ATTR(maxage);
710
711 /**
712  * Show current precreation status: output 0 means success, otherwise negative
713  * number is printed
714  *
715  * \param[in] m         seq_file handle
716  * \param[in] data      unused for single entry
717  * \retval              0 on success
718  * \retval              negative number on error
719  */
720 static ssize_t prealloc_status_show(struct kobject *kobj,
721                                     struct attribute *attr,
722                                     char *buf)
723 {
724         struct dt_device *dt = container_of(kobj, struct dt_device,
725                                             dd_kobj);
726         struct osp_device *osp = dt2osp_dev(dt);
727
728         if (!osp->opd_pre)
729                 return -EINVAL;
730
731         return sprintf(buf, "%d\n", osp->opd_pre_status);
732 }
733 LUSTRE_RO_ATTR(prealloc_status);
734
735 /**
736  * Show the number of RPCs in processing (including uncommitted by OST) plus
737  * changes to sync, i.e. this is the total number of changes OST needs to apply
738  * and commit.
739  *
740  * This counter is used to determine if OST has space returned. A zero value
741  * indicates that OST storage space consumed by destroyed objects has been freed
742  * on disk, the associated llog records have been cleared, and no synchronous
743  * RPC are being processed.
744  *
745  * \param[in] m         seq_file handle
746  * \param[in] data      unused for single entry
747  * \retval              0 on success
748  * \retval              negative number on error
749  */
750 static ssize_t destroys_in_flight_show(struct kobject *kobj,
751                                        struct attribute *attr,
752                                        char *buf)
753 {
754         struct dt_device *dt = container_of(kobj, struct dt_device,
755                                             dd_kobj);
756         struct osp_device *osp = dt2osp_dev(dt);
757
758         return sprintf(buf, "%u\n",
759                        atomic_read(&osp->opd_sync_rpcs_in_progress) +
760                        atomic_read(&osp->opd_sync_changes));
761 }
762 LUSTRE_RO_ATTR(destroys_in_flight);
763
764 /**
765  * Show changes synced from previous mount
766  *
767  * \param[in] m         seq_file handle
768  * \param[in] data      unused for single entry
769  * \retval              0 on success
770  * \retval              negative number on error
771  */
772 static ssize_t old_sync_processed_show(struct kobject *kobj,
773                                        struct attribute *attr,
774                                        char *buf)
775 {
776         struct dt_device *dt = container_of(kobj, struct dt_device,
777                                             dd_kobj);
778         struct osp_device *osp = dt2osp_dev(dt);
779
780         return sprintf(buf, "%d\n", osp->opd_sync_prev_done);
781 }
782 LUSTRE_RO_ATTR(old_sync_processed);
783
784 /**
785  * Show maximum number of RPCs in flight
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 lfsck_max_rpcs_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 lu_device *lu = dt2lu_dev(dt);
799         struct obd_device *obd = lu->ld_obd;
800         u32 max;
801
802         max = obd_get_max_rpcs_in_flight(&obd->u.cli);
803         return sprintf(buf, "%u\n", max);
804 }
805
806 /**
807  * Change maximum number of RPCs in flight
808  *
809  * \param[in] file      proc file
810  * \param[in] buffer    string which represents maximum number of RPCs in flight
811  * \param[in] count     \a buffer length
812  * \param[in] off       unused for single entry
813  * \retval              \a count on success
814  * \retval              negative number on error
815  */
816 static ssize_t lfsck_max_rpcs_in_flight_store(struct kobject *kobj,
817                                               struct attribute *attr,
818                                               const char *buffer,
819                                               size_t count)
820 {
821         struct dt_device *dt = container_of(kobj, struct dt_device,
822                                             dd_kobj);
823         struct lu_device *lu = dt2lu_dev(dt);
824         struct obd_device *obd = lu->ld_obd;
825         unsigned int val;
826         int rc;
827
828         rc = kstrtouint(buffer, 0, &val);
829         if (rc)
830                 return rc;
831
832         rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
833         return rc ? rc : count;
834 }
835 LUSTRE_RW_ATTR(lfsck_max_rpcs_in_flight);
836
837 ssize_t ping_show(struct kobject *kobj, struct attribute *attr,
838                   char *buffer)
839 {
840         struct dt_device *dt = container_of(kobj, struct dt_device,
841                                             dd_kobj);
842         struct lu_device *lu = dt2lu_dev(dt);
843         struct obd_device *obd = lu->ld_obd;
844         int rc;
845
846         rc = ptlrpc_obd_ping(obd);
847
848         return rc;
849 }
850 LUSTRE_RO_ATTR(ping);
851
852 static ssize_t osp_conn_uuid_show(struct kobject *kobj, struct attribute *attr,
853                                   char *buf)
854 {
855         struct dt_device *dt = container_of(kobj, struct dt_device,
856                                             dd_kobj);
857         struct lu_device *lu = dt2lu_dev(dt);
858         struct obd_device *obd = lu->ld_obd;
859         struct obd_import *imp;
860         struct ptlrpc_connection *conn;
861         ssize_t count;
862
863         with_imp_locked(obd, imp, count) {
864                 conn = imp->imp_connection;
865                 if (conn)
866                         count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
867                 else
868                         count = sprintf(buf, "%s\n", "<none>");
869         }
870
871         return count;
872 }
873
874 LUSTRE_ATTR(ost_conn_uuid, 0444, osp_conn_uuid_show, NULL);
875 LUSTRE_ATTR(mdt_conn_uuid, 0444, osp_conn_uuid_show, NULL);
876
877 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, connect_flags);
878 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, server_uuid);
879 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, timeouts);
880
881 LDEBUGFS_SEQ_FOPS_RW_TYPE(osp, import);
882 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, state);
883
884 static int osp_rpc_stats_seq_show(struct seq_file *seq, void *v)
885 {
886         struct obd_device *dev = seq->private;
887
888         return obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
889 }
890
891 static ssize_t osp_rpc_stats_seq_write(struct file *file,
892                                        const char __user *buf,
893                                        size_t len, loff_t *off)
894 {
895         struct seq_file *seq = file->private_data;
896         struct obd_device *dev = seq->private;
897         struct client_obd *cli = &dev->u.cli;
898
899         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
900         cli->cl_mod_rpcs_init = ktime_get_real();
901
902         return len;
903 }
904 LDEBUGFS_SEQ_FOPS(osp_rpc_stats);
905
906 /**
907  * Show high watermark (in megabytes). If available free space at OST is greater
908  * than high watermark and object allocation for OST is disabled, enable it.
909  */
910 static ssize_t reserved_mb_high_show(struct kobject *kobj,
911                                      struct attribute *attr,
912                                      char *buf)
913 {
914         struct dt_device *dt = container_of(kobj, struct dt_device,
915                                             dd_kobj);
916         struct osp_device *osp = dt2osp_dev(dt);
917
918         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_high);
919 }
920
921 /**
922  * Change high watermark
923  */
924 static ssize_t reserved_mb_high_store(struct kobject *kobj,
925                                       struct attribute *attr,
926                                       const char *buffer,
927                                       size_t count)
928 {
929         struct dt_device *dt = container_of(kobj, struct dt_device,
930                                             dd_kobj);
931         struct osp_device *osp = dt2osp_dev(dt);
932         u64 val;
933         int rc;
934
935         rc = sysfs_memparse(buffer, count, &val, "MiB");
936         if (rc < 0)
937                 return rc;
938         val >>= 20;
939         if (val < 1)
940                 return -ERANGE;
941
942         spin_lock(&osp->opd_pre_lock);
943         osp->opd_reserved_mb_high = val;
944         if (val <= osp->opd_reserved_mb_low)
945                 osp->opd_reserved_mb_low = val - 1;
946         spin_unlock(&osp->opd_pre_lock);
947
948         return count;
949 }
950 LUSTRE_RW_ATTR(reserved_mb_high);
951
952 /**
953  * Show low watermark (in megabytes). If available free space at OST is less
954  * than low watermark, object allocation for OST is disabled.
955  */
956 static ssize_t reserved_mb_low_show(struct kobject *kobj,
957                                     struct attribute *attr,
958                                     char *buf)
959 {
960         struct dt_device *dt = container_of(kobj, struct dt_device,
961                                             dd_kobj);
962         struct osp_device *osp = dt2osp_dev(dt);
963
964         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_low);
965 }
966
967 /**
968  * Change low watermark
969  */
970 static ssize_t reserved_mb_low_store(struct kobject *kobj,
971                                      struct attribute *attr,
972                                      const char *buffer,
973                                      size_t count)
974 {
975         struct dt_device *dt = container_of(kobj, struct dt_device,
976                                             dd_kobj);
977         struct osp_device *osp = dt2osp_dev(dt);
978         u64 val;
979         int rc;
980
981         rc = sysfs_memparse(buffer, count, &val, "MiB");
982         if (rc < 0)
983                 return rc;
984         val >>= 20;
985
986         spin_lock(&osp->opd_pre_lock);
987         osp->opd_reserved_mb_low = val;
988         if (val >= osp->opd_reserved_mb_high)
989                 osp->opd_reserved_mb_high = val + 1;
990         spin_unlock(&osp->opd_pre_lock);
991
992         return count;
993 }
994 LUSTRE_RW_ATTR(reserved_mb_low);
995
996 /**
997  * Show high watermark of inode.
998  */
999 static ssize_t reserved_ino_high_show(struct kobject *kobj,
1000                                       struct attribute *attr,
1001                                       char *buf)
1002 {
1003         struct dt_device *dt = container_of(kobj, struct dt_device,
1004                                             dd_kobj);
1005         struct osp_device *osp = dt2osp_dev(dt);
1006
1007         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_high);
1008 }
1009
1010 /**
1011  * Change high watermark of inode.
1012  */
1013 static ssize_t reserved_ino_high_store(struct kobject *kobj,
1014                                        struct attribute *attr,
1015                                        const char *buffer,
1016                                        size_t count)
1017 {
1018         struct dt_device *dt = container_of(kobj, struct dt_device,
1019                                             dd_kobj);
1020         struct osp_device *osp = dt2osp_dev(dt);
1021         unsigned int val;
1022         int rc;
1023
1024         rc = kstrtouint(buffer, 0, &val);
1025         if (rc < 0)
1026                 return rc;
1027         if (val < 1)
1028                 return -ERANGE;
1029
1030         spin_lock(&osp->opd_pre_lock);
1031         osp->opd_reserved_ino_high = val;
1032         if (val <= osp->opd_reserved_ino_low)
1033                 osp->opd_reserved_ino_low = val >> 1;
1034         spin_unlock(&osp->opd_pre_lock);
1035
1036         return count;
1037 }
1038 LUSTRE_RW_ATTR(reserved_ino_high);
1039
1040 /**
1041  * Show low watermark.
1042  */
1043 static ssize_t reserved_ino_low_show(struct kobject *kobj,
1044                                      struct attribute *attr,
1045                                      char *buf)
1046 {
1047         struct dt_device *dt = container_of(kobj, struct dt_device,
1048                                             dd_kobj);
1049         struct osp_device *osp = dt2osp_dev(dt);
1050
1051         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_low);
1052 }
1053
1054 /**
1055  * Change low watermark
1056  */
1057 static ssize_t reserved_ino_low_store(struct kobject *kobj,
1058                                       struct attribute *attr,
1059                                       const char *buffer,
1060                                       size_t count)
1061 {
1062         struct dt_device *dt = container_of(kobj, struct dt_device,
1063                                             dd_kobj);
1064         struct osp_device *osp = dt2osp_dev(dt);
1065         unsigned int val;
1066         int rc;
1067
1068         rc = kstrtouint(buffer, 0, &val);
1069         if (rc < 0)
1070                 return rc;
1071
1072         if (val & (1UL << 31))
1073                 return -EOVERFLOW;
1074
1075         spin_lock(&osp->opd_pre_lock);
1076         osp->opd_reserved_ino_low = val;
1077         if (val >= osp->opd_reserved_ino_high)
1078                 osp->opd_reserved_ino_high = val << 1;
1079         spin_unlock(&osp->opd_pre_lock);
1080
1081         return count;
1082 }
1083 LUSTRE_RW_ATTR(reserved_ino_low);
1084
1085 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
1086                                 const char *buffer, size_t count)
1087 {
1088         struct dt_device *dt = container_of(kobj, struct dt_device,
1089                                             dd_kobj);
1090         struct lu_env env;
1091         int rc;
1092
1093         rc = lu_env_init(&env, LCT_LOCAL);
1094         if (rc)
1095                 return rc;
1096
1097         rc = dt_sync(&env, dt);
1098         lu_env_fini(&env);
1099
1100         return rc == 0 ? count : rc;
1101 }
1102 LUSTRE_WO_ATTR(force_sync);
1103
1104 static struct ldebugfs_vars ldebugfs_osp_obd_vars[] = {
1105         { .name =       "connect_flags",
1106           .fops =       &osp_connect_flags_fops         },
1107         { .name =       "ost_server_uuid",
1108           .fops =       &osp_server_uuid_fops           },
1109         { .name =       "timeouts",
1110           .fops =       &osp_timeouts_fops              },
1111         { .name =       "import",
1112           .fops =       &osp_import_fops                },
1113         { .name =       "state",
1114           .fops =       &osp_state_fops                 },
1115         { NULL }
1116 };
1117
1118 static struct ldebugfs_vars ldebugfs_osp_md_vars[] = {
1119         { .name =       "connect_flags",
1120           .fops =       &osp_connect_flags_fops         },
1121         { .name =       "mdt_server_uuid",
1122           .fops =       &osp_server_uuid_fops           },
1123         { .name =       "timeouts",
1124           .fops =       &osp_timeouts_fops              },
1125         { .name =       "import",
1126           .fops =       &osp_import_fops                },
1127         { .name =       "state",
1128           .fops =       &osp_state_fops                 },
1129         { .name =       "rpc_stats",
1130           .fops =       &osp_rpc_stats_fops             },
1131         { NULL }
1132 };
1133
1134 static struct attribute *osp_obd_attrs[] = {
1135         /* First two for compatiability reasons */
1136         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1137         &lustre_attr_destroys_in_flight.attr,
1138         &lustre_attr_active.attr,
1139         &lustre_attr_max_rpcs_in_flight.attr,
1140         &lustre_attr_max_rpcs_in_progress.attr,
1141         &lustre_attr_maxage.attr,
1142         &lustre_attr_ost_conn_uuid.attr,
1143         &lustre_attr_ping.attr,
1144         &lustre_attr_prealloc_status.attr,
1145         &lustre_attr_prealloc_next_id.attr,
1146         &lustre_attr_prealloc_last_id.attr,
1147         &lustre_attr_prealloc_next_seq.attr,
1148         &lustre_attr_prealloc_last_seq.attr,
1149         &lustre_attr_prealloc_reserved.attr,
1150         &lustre_attr_sync_in_flight.attr,
1151         &lustre_attr_sync_in_progress.attr,
1152         &lustre_attr_sync_changes.attr,
1153         &lustre_attr_max_sync_changes.attr,
1154         &lustre_attr_force_sync.attr,
1155         &lustre_attr_old_sync_processed.attr,
1156         &lustre_attr_create_count.attr,
1157         &lustre_attr_max_create_count.attr,
1158         &lustre_attr_reserved_mb_high.attr,
1159         &lustre_attr_reserved_mb_low.attr,
1160         &lustre_attr_reserved_ino_high.attr,
1161         &lustre_attr_reserved_ino_low.attr,
1162         NULL,
1163 };
1164
1165 KOBJ_ATTRIBUTE_GROUPS(osp_obd); /* creates osp_obd_groups from osp_obd_attrs */
1166
1167 static struct attribute *osp_md_attrs[] = {
1168         /* First two for compatiability reasons */
1169         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1170         &lustre_attr_destroys_in_flight.attr,
1171         &lustre_attr_active.attr,
1172         &lustre_attr_max_rpcs_in_flight.attr,
1173         &lustre_attr_max_mod_rpcs_in_flight.attr,
1174         &lustre_attr_max_rpcs_in_progress.attr,
1175         &lustre_attr_maxage.attr,
1176         &lustre_attr_mdt_conn_uuid.attr,
1177         &lustre_attr_ping.attr,
1178         &lustre_attr_prealloc_status.attr,
1179         &lustre_attr_reserved_mb_high.attr,
1180         &lustre_attr_reserved_mb_low.attr,
1181         &lustre_attr_reserved_ino_high.attr,
1182         &lustre_attr_reserved_ino_low.attr,
1183         NULL,
1184 };
1185
1186 KOBJ_ATTRIBUTE_GROUPS(osp_md); /* creates osp_md_groups from osp_md_attrs */
1187
1188 void osp_tunables_fini(struct osp_device *osp)
1189 {
1190         struct obd_device *obd = osp->opd_obd;
1191         struct kobject *osc;
1192
1193         osc = kset_find_obj(lustre_kset, "osc");
1194         if (osc) {
1195                 sysfs_remove_link(osc, obd->obd_name);
1196                 kobject_put(osc);
1197         }
1198
1199         debugfs_remove_recursive(osp->opd_debugfs);
1200         osp->opd_debugfs = NULL;
1201
1202         ptlrpc_lprocfs_unregister_obd(obd);
1203
1204         debugfs_remove_recursive(obd->obd_debugfs_entry);
1205         obd->obd_debugfs_entry = NULL;
1206
1207         dt_tunables_fini(&osp->opd_dt_dev);
1208 }
1209
1210 /**
1211  * Initialize OSP sysfs / debugfs
1212  *
1213  * param[in] osp        OSP device
1214  */
1215 void osp_tunables_init(struct osp_device *osp)
1216 {
1217         struct obd_device *obd = osp->opd_obd;
1218         struct kobject *osc;
1219         int rc;
1220
1221         if (osp->opd_connect_mdt) {
1222                 osp->opd_dt_dev.dd_ktype.default_groups =
1223                         KOBJ_ATTR_GROUPS(osp_md);
1224                 obd->obd_debugfs_vars = ldebugfs_osp_md_vars;
1225         } else {
1226                 osp->opd_dt_dev.dd_ktype.default_groups =
1227                         KOBJ_ATTR_GROUPS(osp_obd);
1228                 obd->obd_debugfs_vars = ldebugfs_osp_obd_vars;
1229         }
1230
1231         rc = dt_tunables_init(&osp->opd_dt_dev, obd->obd_type, obd->obd_name,
1232                               NULL);
1233         if (rc) {
1234                 CERROR("%s: failed to setup DT tunables: %d\n",
1235                        obd->obd_name, rc);
1236                 return;
1237         }
1238
1239         /* Since we register the obd device with ptlrpc / sptlrpc we
1240          * have to register debugfs with obd_device
1241          */
1242         obd->obd_debugfs_entry = debugfs_create_dir(
1243                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1244         ldebugfs_add_vars(obd->obd_debugfs_entry, obd->obd_debugfs_vars, obd);
1245
1246         sptlrpc_lprocfs_cliobd_attach(obd);
1247         ptlrpc_lprocfs_register_obd(obd);
1248
1249         if (osp->opd_connect_mdt || !strstr(obd->obd_name, "osc"))
1250                 return;
1251
1252         /* If the real OSC is present which is the case for setups
1253          * with both server and clients on the same node then use
1254          * the OSC's proc root
1255          */
1256         osc = kset_find_obj(lustre_kset, "osc");
1257         if (osc) {
1258                 rc = sysfs_create_link(osc, &osp->opd_dt_dev.dd_kobj,
1259                                        obd->obd_name);
1260                 kobject_put(osc);
1261         }
1262
1263         osp->opd_debugfs = ldebugfs_add_symlink(obd->obd_name, "osc",
1264                                                 "../osp/%s", obd->obd_name);
1265         if (!osp->opd_debugfs)
1266                 CERROR("%s: failed to create OSC debugfs symlink\n",
1267                        obd->obd_name);
1268 }