Whamcloud - gitweb
LU-11912 tests: fix racing in force_new_seq_all
[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 static ssize_t prealloc_force_new_seq_show(struct kobject *kobj,
736                                            struct attribute *attr,
737                                            char *buf)
738 {
739         struct dt_device *dt = container_of(kobj, struct dt_device,
740                                             dd_kobj);
741         struct osp_device *osp = dt2osp_dev(dt);
742
743         if (!osp->opd_pre)
744                 return -EINVAL;
745
746         return scnprintf(buf, PAGE_SIZE, "%d\n", osp->opd_pre_force_new_seq);
747 }
748
749 static ssize_t prealloc_force_new_seq_store(struct kobject *kobj,
750                                             struct attribute *attr,
751                                             const char *buffer,
752                                             size_t count)
753 {
754         struct dt_device *dt = container_of(kobj, struct dt_device,
755                                             dd_kobj);
756         struct osp_device *osp = dt2osp_dev(dt);
757         bool val;
758         int rc;
759
760         if (!osp->opd_pre)
761                 return -EINVAL;
762
763         rc = kstrtobool(buffer, &val);
764         if (rc)
765                 return rc;
766
767         osp->opd_pre_force_new_seq = val;
768
769         return count;
770 }
771 LUSTRE_RW_ATTR(prealloc_force_new_seq);
772
773 /**
774  * Show the number of RPCs in processing (including uncommitted by OST) plus
775  * changes to sync, i.e. this is the total number of changes OST needs to apply
776  * and commit.
777  *
778  * This counter is used to determine if OST has space returned. A zero value
779  * indicates that OST storage space consumed by destroyed objects has been freed
780  * on disk, the associated llog records have been cleared, and no synchronous
781  * RPC are being processed.
782  *
783  * \param[in] m         seq_file handle
784  * \param[in] data      unused for single entry
785  * \retval              0 on success
786  * \retval              negative number on error
787  */
788 static ssize_t destroys_in_flight_show(struct kobject *kobj,
789                                        struct attribute *attr,
790                                        char *buf)
791 {
792         struct dt_device *dt = container_of(kobj, struct dt_device,
793                                             dd_kobj);
794         struct osp_device *osp = dt2osp_dev(dt);
795
796         return sprintf(buf, "%u\n",
797                        atomic_read(&osp->opd_sync_rpcs_in_progress) +
798                        atomic_read(&osp->opd_sync_changes));
799 }
800 LUSTRE_RO_ATTR(destroys_in_flight);
801
802 /**
803  * Show changes synced from previous mount
804  *
805  * \param[in] m         seq_file handle
806  * \param[in] data      unused for single entry
807  * \retval              0 on success
808  * \retval              negative number on error
809  */
810 static ssize_t old_sync_processed_show(struct kobject *kobj,
811                                        struct attribute *attr,
812                                        char *buf)
813 {
814         struct dt_device *dt = container_of(kobj, struct dt_device,
815                                             dd_kobj);
816         struct osp_device *osp = dt2osp_dev(dt);
817
818         return sprintf(buf, "%d\n", osp->opd_sync_prev_done);
819 }
820 LUSTRE_RO_ATTR(old_sync_processed);
821
822 /**
823  * Show maximum number of RPCs in flight
824  *
825  * \param[in] m         seq_file handle
826  * \param[in] data      unused for single entry
827  * \retval              0 on success
828  * \retval              negative number on error
829  */
830 static ssize_t lfsck_max_rpcs_in_flight_show(struct kobject *kobj,
831                                              struct attribute *attr,
832                                              char *buf)
833 {
834         struct dt_device *dt = container_of(kobj, struct dt_device,
835                                             dd_kobj);
836         struct lu_device *lu = dt2lu_dev(dt);
837         struct obd_device *obd = lu->ld_obd;
838         u32 max;
839
840         max = obd_get_max_rpcs_in_flight(&obd->u.cli);
841         return sprintf(buf, "%u\n", max);
842 }
843
844 /**
845  * Change maximum number of RPCs in flight
846  *
847  * \param[in] file      proc file
848  * \param[in] buffer    string which represents maximum number of RPCs in flight
849  * \param[in] count     \a buffer length
850  * \param[in] off       unused for single entry
851  * \retval              \a count on success
852  * \retval              negative number on error
853  */
854 static ssize_t lfsck_max_rpcs_in_flight_store(struct kobject *kobj,
855                                               struct attribute *attr,
856                                               const char *buffer,
857                                               size_t count)
858 {
859         struct dt_device *dt = container_of(kobj, struct dt_device,
860                                             dd_kobj);
861         struct lu_device *lu = dt2lu_dev(dt);
862         struct obd_device *obd = lu->ld_obd;
863         unsigned int val;
864         int rc;
865
866         rc = kstrtouint(buffer, 0, &val);
867         if (rc)
868                 return rc;
869
870         rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
871         return rc ? rc : count;
872 }
873 LUSTRE_RW_ATTR(lfsck_max_rpcs_in_flight);
874
875 ssize_t ping_show(struct kobject *kobj, struct attribute *attr,
876                   char *buffer)
877 {
878         struct dt_device *dt = container_of(kobj, struct dt_device,
879                                             dd_kobj);
880         struct lu_device *lu = dt2lu_dev(dt);
881         struct obd_device *obd = lu->ld_obd;
882         int rc;
883
884         rc = ptlrpc_obd_ping(obd);
885
886         return rc;
887 }
888 LUSTRE_RO_ATTR(ping);
889
890 static ssize_t osp_conn_uuid_show(struct kobject *kobj, struct attribute *attr,
891                                   char *buf)
892 {
893         struct dt_device *dt = container_of(kobj, struct dt_device,
894                                             dd_kobj);
895         struct lu_device *lu = dt2lu_dev(dt);
896         struct obd_device *obd = lu->ld_obd;
897         struct obd_import *imp;
898         struct ptlrpc_connection *conn;
899         ssize_t count;
900
901         with_imp_locked(obd, imp, count) {
902                 conn = imp->imp_connection;
903                 if (conn)
904                         count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
905                 else
906                         count = sprintf(buf, "%s\n", "<none>");
907         }
908
909         return count;
910 }
911
912 LUSTRE_ATTR(ost_conn_uuid, 0444, osp_conn_uuid_show, NULL);
913 LUSTRE_ATTR(mdt_conn_uuid, 0444, osp_conn_uuid_show, NULL);
914
915 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, connect_flags);
916 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, server_uuid);
917 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, timeouts);
918
919 LDEBUGFS_SEQ_FOPS_RW_TYPE(osp, import);
920 LDEBUGFS_SEQ_FOPS_RO_TYPE(osp, state);
921
922 static int osp_rpc_stats_seq_show(struct seq_file *seq, void *v)
923 {
924         struct obd_device *dev = seq->private;
925
926         return obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
927 }
928
929 static ssize_t osp_rpc_stats_seq_write(struct file *file,
930                                        const char __user *buf,
931                                        size_t len, loff_t *off)
932 {
933         struct seq_file *seq = file->private_data;
934         struct obd_device *dev = seq->private;
935         struct client_obd *cli = &dev->u.cli;
936
937         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
938         cli->cl_mod_rpcs_init = ktime_get_real();
939
940         return len;
941 }
942 LDEBUGFS_SEQ_FOPS(osp_rpc_stats);
943
944 /**
945  * Show high watermark (in megabytes). If available free space at OST is greater
946  * than high watermark and object allocation for OST is disabled, enable it.
947  */
948 static ssize_t reserved_mb_high_show(struct kobject *kobj,
949                                      struct attribute *attr,
950                                      char *buf)
951 {
952         struct dt_device *dt = container_of(kobj, struct dt_device,
953                                             dd_kobj);
954         struct osp_device *osp = dt2osp_dev(dt);
955
956         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_high);
957 }
958
959 /**
960  * Change high watermark
961  */
962 static ssize_t reserved_mb_high_store(struct kobject *kobj,
963                                       struct attribute *attr,
964                                       const char *buffer,
965                                       size_t count)
966 {
967         struct dt_device *dt = container_of(kobj, struct dt_device,
968                                             dd_kobj);
969         struct osp_device *osp = dt2osp_dev(dt);
970         u64 val;
971         int rc;
972
973         rc = sysfs_memparse(buffer, count, &val, "MiB");
974         if (rc < 0)
975                 return rc;
976         val >>= 20;
977         if (val < 1)
978                 return -ERANGE;
979
980         spin_lock(&osp->opd_pre_lock);
981         osp->opd_reserved_mb_high = val;
982         if (val <= osp->opd_reserved_mb_low)
983                 osp->opd_reserved_mb_low = val - 1;
984         spin_unlock(&osp->opd_pre_lock);
985
986         return count;
987 }
988 LUSTRE_RW_ATTR(reserved_mb_high);
989
990 /**
991  * Show low watermark (in megabytes). If available free space at OST is less
992  * than low watermark, object allocation for OST is disabled.
993  */
994 static ssize_t reserved_mb_low_show(struct kobject *kobj,
995                                     struct attribute *attr,
996                                     char *buf)
997 {
998         struct dt_device *dt = container_of(kobj, struct dt_device,
999                                             dd_kobj);
1000         struct osp_device *osp = dt2osp_dev(dt);
1001
1002         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_mb_low);
1003 }
1004
1005 /**
1006  * Change low watermark
1007  */
1008 static ssize_t reserved_mb_low_store(struct kobject *kobj,
1009                                      struct attribute *attr,
1010                                      const char *buffer,
1011                                      size_t count)
1012 {
1013         struct dt_device *dt = container_of(kobj, struct dt_device,
1014                                             dd_kobj);
1015         struct osp_device *osp = dt2osp_dev(dt);
1016         u64 val;
1017         int rc;
1018
1019         rc = sysfs_memparse(buffer, count, &val, "MiB");
1020         if (rc < 0)
1021                 return rc;
1022         val >>= 20;
1023
1024         spin_lock(&osp->opd_pre_lock);
1025         osp->opd_reserved_mb_low = val;
1026         if (val >= osp->opd_reserved_mb_high)
1027                 osp->opd_reserved_mb_high = val + 1;
1028         spin_unlock(&osp->opd_pre_lock);
1029
1030         return count;
1031 }
1032 LUSTRE_RW_ATTR(reserved_mb_low);
1033
1034 /**
1035  * Show high watermark of inode.
1036  */
1037 static ssize_t reserved_ino_high_show(struct kobject *kobj,
1038                                       struct attribute *attr,
1039                                       char *buf)
1040 {
1041         struct dt_device *dt = container_of(kobj, struct dt_device,
1042                                             dd_kobj);
1043         struct osp_device *osp = dt2osp_dev(dt);
1044
1045         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_high);
1046 }
1047
1048 /**
1049  * Change high watermark of inode.
1050  */
1051 static ssize_t reserved_ino_high_store(struct kobject *kobj,
1052                                        struct attribute *attr,
1053                                        const char *buffer,
1054                                        size_t count)
1055 {
1056         struct dt_device *dt = container_of(kobj, struct dt_device,
1057                                             dd_kobj);
1058         struct osp_device *osp = dt2osp_dev(dt);
1059         unsigned int val;
1060         int rc;
1061
1062         rc = kstrtouint(buffer, 0, &val);
1063         if (rc < 0)
1064                 return rc;
1065         if (val < 1)
1066                 return -ERANGE;
1067
1068         spin_lock(&osp->opd_pre_lock);
1069         osp->opd_reserved_ino_high = val;
1070         if (val <= osp->opd_reserved_ino_low)
1071                 osp->opd_reserved_ino_low = val >> 1;
1072         spin_unlock(&osp->opd_pre_lock);
1073
1074         return count;
1075 }
1076 LUSTRE_RW_ATTR(reserved_ino_high);
1077
1078 /**
1079  * Show low watermark.
1080  */
1081 static ssize_t reserved_ino_low_show(struct kobject *kobj,
1082                                      struct attribute *attr,
1083                                      char *buf)
1084 {
1085         struct dt_device *dt = container_of(kobj, struct dt_device,
1086                                             dd_kobj);
1087         struct osp_device *osp = dt2osp_dev(dt);
1088
1089         return snprintf(buf, PAGE_SIZE, "%u\n", osp->opd_reserved_ino_low);
1090 }
1091
1092 /**
1093  * Change low watermark
1094  */
1095 static ssize_t reserved_ino_low_store(struct kobject *kobj,
1096                                       struct attribute *attr,
1097                                       const char *buffer,
1098                                       size_t count)
1099 {
1100         struct dt_device *dt = container_of(kobj, struct dt_device,
1101                                             dd_kobj);
1102         struct osp_device *osp = dt2osp_dev(dt);
1103         unsigned int val;
1104         int rc;
1105
1106         rc = kstrtouint(buffer, 0, &val);
1107         if (rc < 0)
1108                 return rc;
1109
1110         if (val & (1UL << 31))
1111                 return -EOVERFLOW;
1112
1113         spin_lock(&osp->opd_pre_lock);
1114         osp->opd_reserved_ino_low = val;
1115         if (val >= osp->opd_reserved_ino_high)
1116                 osp->opd_reserved_ino_high = val << 1;
1117         spin_unlock(&osp->opd_pre_lock);
1118
1119         return count;
1120 }
1121 LUSTRE_RW_ATTR(reserved_ino_low);
1122
1123 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
1124                                 const char *buffer, size_t count)
1125 {
1126         struct dt_device *dt = container_of(kobj, struct dt_device,
1127                                             dd_kobj);
1128         struct lu_env env;
1129         int rc;
1130
1131         rc = lu_env_init(&env, LCT_LOCAL);
1132         if (rc)
1133                 return rc;
1134
1135         rc = dt_sync(&env, dt);
1136         lu_env_fini(&env);
1137
1138         return rc == 0 ? count : rc;
1139 }
1140 LUSTRE_WO_ATTR(force_sync);
1141
1142 static struct ldebugfs_vars ldebugfs_osp_obd_vars[] = {
1143         { .name =       "connect_flags",
1144           .fops =       &osp_connect_flags_fops         },
1145         { .name =       "ost_server_uuid",
1146           .fops =       &osp_server_uuid_fops           },
1147         { .name =       "timeouts",
1148           .fops =       &osp_timeouts_fops              },
1149         { .name =       "import",
1150           .fops =       &osp_import_fops                },
1151         { .name =       "state",
1152           .fops =       &osp_state_fops                 },
1153         { NULL }
1154 };
1155
1156 static struct ldebugfs_vars ldebugfs_osp_md_vars[] = {
1157         { .name =       "connect_flags",
1158           .fops =       &osp_connect_flags_fops         },
1159         { .name =       "mdt_server_uuid",
1160           .fops =       &osp_server_uuid_fops           },
1161         { .name =       "timeouts",
1162           .fops =       &osp_timeouts_fops              },
1163         { .name =       "import",
1164           .fops =       &osp_import_fops                },
1165         { .name =       "state",
1166           .fops =       &osp_state_fops                 },
1167         { .name =       "rpc_stats",
1168           .fops =       &osp_rpc_stats_fops             },
1169         { NULL }
1170 };
1171
1172 static struct attribute *osp_obd_attrs[] = {
1173         /* First two for compatiability reasons */
1174         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1175         &lustre_attr_destroys_in_flight.attr,
1176         &lustre_attr_active.attr,
1177         &lustre_attr_max_rpcs_in_flight.attr,
1178         &lustre_attr_max_rpcs_in_progress.attr,
1179         &lustre_attr_maxage.attr,
1180         &lustre_attr_ost_conn_uuid.attr,
1181         &lustre_attr_ping.attr,
1182         &lustre_attr_prealloc_status.attr,
1183         &lustre_attr_prealloc_next_id.attr,
1184         &lustre_attr_prealloc_last_id.attr,
1185         &lustre_attr_prealloc_next_seq.attr,
1186         &lustre_attr_prealloc_last_seq.attr,
1187         &lustre_attr_prealloc_reserved.attr,
1188         &lustre_attr_prealloc_force_new_seq.attr,
1189         &lustre_attr_sync_in_flight.attr,
1190         &lustre_attr_sync_in_progress.attr,
1191         &lustre_attr_sync_changes.attr,
1192         &lustre_attr_max_sync_changes.attr,
1193         &lustre_attr_force_sync.attr,
1194         &lustre_attr_old_sync_processed.attr,
1195         &lustre_attr_create_count.attr,
1196         &lustre_attr_max_create_count.attr,
1197         &lustre_attr_reserved_mb_high.attr,
1198         &lustre_attr_reserved_mb_low.attr,
1199         &lustre_attr_reserved_ino_high.attr,
1200         &lustre_attr_reserved_ino_low.attr,
1201         NULL,
1202 };
1203
1204 KOBJ_ATTRIBUTE_GROUPS(osp_obd); /* creates osp_obd_groups from osp_obd_attrs */
1205
1206 static struct attribute *osp_md_attrs[] = {
1207         /* First two for compatiability reasons */
1208         &lustre_attr_lfsck_max_rpcs_in_flight.attr,
1209         &lustre_attr_destroys_in_flight.attr,
1210         &lustre_attr_active.attr,
1211         &lustre_attr_max_rpcs_in_flight.attr,
1212         &lustre_attr_max_mod_rpcs_in_flight.attr,
1213         &lustre_attr_max_rpcs_in_progress.attr,
1214         &lustre_attr_maxage.attr,
1215         &lustre_attr_mdt_conn_uuid.attr,
1216         &lustre_attr_ping.attr,
1217         &lustre_attr_prealloc_status.attr,
1218         &lustre_attr_reserved_mb_high.attr,
1219         &lustre_attr_reserved_mb_low.attr,
1220         &lustre_attr_reserved_ino_high.attr,
1221         &lustre_attr_reserved_ino_low.attr,
1222         NULL,
1223 };
1224
1225 KOBJ_ATTRIBUTE_GROUPS(osp_md); /* creates osp_md_groups from osp_md_attrs */
1226
1227 void osp_tunables_fini(struct osp_device *osp)
1228 {
1229         struct obd_device *obd = osp->opd_obd;
1230         struct kobject *osc;
1231
1232         osc = kset_find_obj(lustre_kset, "osc");
1233         if (osc) {
1234                 sysfs_remove_link(osc, obd->obd_name);
1235                 kobject_put(osc);
1236         }
1237
1238         debugfs_remove_recursive(osp->opd_debugfs);
1239         osp->opd_debugfs = NULL;
1240
1241         ptlrpc_lprocfs_unregister_obd(obd);
1242
1243         debugfs_remove_recursive(obd->obd_debugfs_entry);
1244         obd->obd_debugfs_entry = NULL;
1245
1246         dt_tunables_fini(&osp->opd_dt_dev);
1247 }
1248
1249 /**
1250  * Initialize OSP sysfs / debugfs
1251  *
1252  * param[in] osp        OSP device
1253  */
1254 void osp_tunables_init(struct osp_device *osp)
1255 {
1256         struct obd_device *obd = osp->opd_obd;
1257         struct kobject *osc;
1258         int rc;
1259
1260         if (osp->opd_connect_mdt) {
1261                 osp->opd_dt_dev.dd_ktype.default_groups =
1262                         KOBJ_ATTR_GROUPS(osp_md);
1263                 obd->obd_debugfs_vars = ldebugfs_osp_md_vars;
1264         } else {
1265                 osp->opd_dt_dev.dd_ktype.default_groups =
1266                         KOBJ_ATTR_GROUPS(osp_obd);
1267                 obd->obd_debugfs_vars = ldebugfs_osp_obd_vars;
1268         }
1269
1270         rc = dt_tunables_init(&osp->opd_dt_dev, obd->obd_type, obd->obd_name,
1271                               NULL);
1272         if (rc) {
1273                 CERROR("%s: failed to setup DT tunables: %d\n",
1274                        obd->obd_name, rc);
1275                 return;
1276         }
1277
1278         /* Since we register the obd device with ptlrpc / sptlrpc we
1279          * have to register debugfs with obd_device
1280          */
1281         obd->obd_debugfs_entry = debugfs_create_dir(
1282                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1283         ldebugfs_add_vars(obd->obd_debugfs_entry, obd->obd_debugfs_vars, obd);
1284
1285         sptlrpc_lprocfs_cliobd_attach(obd);
1286         ptlrpc_lprocfs_register_obd(obd);
1287
1288         if (osp->opd_connect_mdt || !strstr(obd->obd_name, "osc"))
1289                 return;
1290
1291         /* If the real OSC is present which is the case for setups
1292          * with both server and clients on the same node then use
1293          * the OSC's proc root
1294          */
1295         osc = kset_find_obj(lustre_kset, "osc");
1296         if (osc) {
1297                 rc = sysfs_create_link(osc, &osp->opd_dt_dev.dd_kobj,
1298                                        obd->obd_name);
1299                 kobject_put(osc);
1300         }
1301
1302         osp->opd_debugfs = ldebugfs_add_symlink(obd->obd_name, "osc",
1303                                                 "../osp/%s", obd->obd_name);
1304         if (!osp->opd_debugfs)
1305                 CERROR("%s: failed to create OSC debugfs symlink\n",
1306                        obd->obd_name);
1307 }