Whamcloud - gitweb
LU-7853 lod: fixes bitfield in lod qos code
[fs/lustre-release.git] / lustre / lod / lproc_lod.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  2008 Sun Microsystems, Inc. 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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <lprocfs_status.h>
35 #include <obd_class.h>
36 #include <linux/seq_file.h>
37 #include "lod_internal.h"
38 #include <uapi/linux/lustre/lustre_param.h>
39
40 /*
41  * Notice, all the functions below (except for lod_procfs_init() and
42  * lod_procfs_fini()) are not supposed to be used directly. They are
43  * called by Linux kernel's procfs.
44  */
45
46 #ifdef CONFIG_PROC_FS
47
48 static ssize_t dom_stripesize_show(struct kobject *kobj,
49                                    struct attribute *attr,
50                                    char *buf)
51 {
52         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
53         struct lod_device *lod = dt2lod_dev(dt);
54
55         return snprintf(buf, PAGE_SIZE, "%u\n",
56                         lod->lod_dom_stripesize_max_kb << 10);
57 }
58
59 static inline int dom_stripesize_max_kb_update(struct lod_device *lod,
60                                                __u64 val)
61 {
62         /* 1GB is the limit */
63         if (val > (1ULL << 20))
64                 return -ERANGE;
65
66         if (val > 0) {
67                 if (val < LOD_DOM_MIN_SIZE_KB) {
68                         LCONSOLE_INFO("Increasing provided stripe size to a minimum value %u\n",
69                                       LOD_DOM_MIN_SIZE_KB);
70                         val = LOD_DOM_MIN_SIZE_KB;
71                 } else if (val & (LOD_DOM_MIN_SIZE_KB - 1)) {
72                         val &= ~(LOD_DOM_MIN_SIZE_KB - 1);
73                         LCONSOLE_WARN("Changing provided stripe size to %llu (a multiple of minimum %u)\n",
74                                       val, LOD_DOM_MIN_SIZE_KB);
75                 }
76         }
77         spin_lock(&lod->lod_lsfs_lock);
78         lod->lod_dom_stripesize_max_kb = val;
79         lod_dom_stripesize_recalc(lod);
80         spin_unlock(&lod->lod_lsfs_lock);
81         return 0;
82 }
83
84 static ssize_t dom_stripesize_store(struct kobject *kobj,
85                                     struct attribute *attr,
86                                     const char *buffer, size_t count)
87 {
88         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
89         struct lod_device *lod = dt2lod_dev(dt);
90         u64 val;
91         int rc;
92
93         rc = sysfs_memparse(buffer, count, &val, "B");
94         if (rc < 0)
95                 return rc;
96
97         rc = dom_stripesize_max_kb_update(lod, val >> 10);
98         if (rc)
99                 return rc;
100         return count;
101 }
102
103 /* Old attribute name is still supported */
104 LUSTRE_RW_ATTR(dom_stripesize);
105
106 /**
107  * Show DoM maximum allowed stripe size.
108  */
109 static ssize_t dom_stripesize_max_kb_show(struct kobject *kobj,
110                                           struct attribute *attr,
111                                           char *buf)
112 {
113         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
114         struct lod_device *lod = dt2lod_dev(dt);
115
116         return snprintf(buf, PAGE_SIZE, "%u\n",
117                         lod->lod_dom_stripesize_max_kb);
118 }
119
120 /**
121  * Set DoM maximum allowed stripe size.
122  */
123 static ssize_t dom_stripesize_max_kb_store(struct kobject *kobj,
124                                            struct attribute *attr,
125                                            const char *buffer, size_t count)
126 {
127         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
128         struct lod_device *lod = dt2lod_dev(dt);
129         u64 val;
130         int rc;
131
132         rc = sysfs_memparse(buffer, count, &val, "KiB");
133         if (rc < 0)
134                 return rc;
135
136         rc = dom_stripesize_max_kb_update(lod, val >> 10);
137         if (rc)
138                 return rc;
139         return count;
140 }
141 LUSTRE_RW_ATTR(dom_stripesize_max_kb);
142
143 /**
144  * Show DoM default stripe size.
145  */
146 static ssize_t dom_stripesize_cur_kb_show(struct kobject *kobj,
147                                           struct attribute *attr,
148                                           char *buf)
149 {
150         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
151         struct lod_device *lod = dt2lod_dev(dt);
152
153         return snprintf(buf, PAGE_SIZE, "%u\n", lod->lod_dom_stripesize_cur_kb);
154 }
155
156 LUSTRE_RO_ATTR(dom_stripesize_cur_kb);
157
158 /**
159  * Show DoM threshold.
160  */
161 static ssize_t dom_threshold_free_mb_show(struct kobject *kobj,
162                                           struct attribute *attr, char *buf)
163 {
164         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
165         struct lod_device *lod = dt2lod_dev(dt);
166
167         return snprintf(buf, PAGE_SIZE, "%llu\n",
168                         lod->lod_dom_threshold_free_mb);
169 }
170
171 /**
172  * Set DoM default stripe size.
173  */
174 static ssize_t dom_threshold_free_mb_store(struct kobject *kobj,
175                                            struct attribute *attr,
176                                            const char *buffer, size_t count)
177 {
178         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
179         struct lod_device *lod = dt2lod_dev(dt);
180         u64 val;
181         int rc;
182         char *pct;
183
184         pct = strnchr(buffer, count, '%');
185         if (pct) {
186                 rc = string_to_size(&val, buffer, pct - buffer);
187                 if (rc < 0)
188                         return rc;
189                 val = mult_frac(lod->lod_lsfs_total_mb,
190                                 min_t(unsigned int, val, 100), 100);
191         } else {
192                 rc = sysfs_memparse(buffer, count, &val, "MiB");
193                 if (rc < 0)
194                         return rc;
195                 val >>= 20;
196         }
197
198         spin_lock(&lod->lod_lsfs_lock);
199         lod->lod_dom_threshold_free_mb = val;
200         lod_dom_stripesize_recalc(lod);
201         spin_unlock(&lod->lod_lsfs_lock);
202
203         return count;
204 }
205
206 LUSTRE_RW_ATTR(dom_threshold_free_mb);
207
208 static ssize_t stripesize_show(struct kobject *kobj, struct attribute *attr,
209                                char *buf)
210 {
211         struct dt_device *dt = container_of(kobj, struct dt_device,
212                                             dd_kobj);
213         struct lod_device *lod = dt2lod_dev(dt);
214
215         return snprintf(buf, PAGE_SIZE, "%llu\n",
216                         lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_size);
217 }
218
219 static ssize_t stripesize_store(struct kobject *kobj, struct attribute *attr,
220                                 const char *buffer, size_t count)
221 {
222         struct dt_device *dt = container_of(kobj, struct dt_device,
223                                             dd_kobj);
224         struct lod_device *lod = dt2lod_dev(dt);
225         u64 val;
226         int rc;
227
228         rc = sysfs_memparse(buffer, count, &val, "B");
229         if (rc < 0)
230                 return rc;
231
232         lod_fix_desc_stripe_size(&val);
233         lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_size = val;
234
235         return count;
236 }
237
238 LUSTRE_RW_ATTR(stripesize);
239
240 /**
241  * Show default stripe offset.
242  */
243 static ssize_t stripeoffset_show(struct kobject *kobj, struct attribute *attr,
244                                  char *buf)
245 {
246         struct dt_device *dt = container_of(kobj, struct dt_device,
247                                             dd_kobj);
248         struct lod_device *lod = dt2lod_dev(dt);
249
250         return snprintf(buf, PAGE_SIZE, "%lld\n",
251                 lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_offset);
252 }
253
254 /**
255  * Set default stripe offset.
256  *
257  * Usually contains -1 allowing Lustre to balance objects among OST
258  * otherwise may cause severe OST imbalance.
259  */
260 static ssize_t stripeoffset_store(struct kobject *kobj,
261                                     struct attribute *attr,
262                                     const char *buffer, size_t count)
263 {
264         struct dt_device *dt = container_of(kobj, struct dt_device,
265                                             dd_kobj);
266         struct lod_device *lod = dt2lod_dev(dt);
267         long val;
268         int rc;
269
270         rc = kstrtol(buffer, 0, &val);
271         if (rc)
272                 return rc;
273
274         if (val < -1 || val > LOV_MAX_STRIPE_COUNT)
275                 return -ERANGE;
276
277         lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_offset = val;
278
279         return count;
280 }
281
282 LUSTRE_RW_ATTR(stripeoffset);
283
284 /**
285  * Show default striping pattern (LOV_PATTERN_*).
286  */
287 static ssize_t __stripetype_show(struct kobject *kobj, struct attribute *attr,
288                                  char *buf, bool is_mdt)
289 {
290         struct dt_device *dt = container_of(kobj, struct dt_device,
291                                             dd_kobj);
292         struct lod_device *lod = dt2lod_dev(dt);
293         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
294                                             &lod->lod_ost_descs;
295
296         return snprintf(buf, PAGE_SIZE, "%u\n", ltd->ltd_lov_desc.ld_pattern);
297 }
298
299 static ssize_t mdt_stripetype_show(struct kobject *kobj, struct attribute *attr,
300                                    char *buf)
301 {
302         return __stripetype_show(kobj, attr, buf, true);
303 }
304
305 static ssize_t stripetype_show(struct kobject *kobj, struct attribute *attr,
306                                char *buf)
307 {
308         return __stripetype_show(kobj, attr, buf, false);
309 }
310
311 /**
312  * Set default striping pattern (a number, not a human-readable string).
313  */
314 static ssize_t __stripetype_store(struct kobject *kobj, struct attribute *attr,
315                                   const char *buffer, size_t count, bool is_mdt)
316 {
317         struct dt_device *dt = container_of(kobj, struct dt_device,
318                                             dd_kobj);
319         struct lod_device *lod = dt2lod_dev(dt);
320         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
321                                             &lod->lod_ost_descs;
322         u32 pattern;
323         int rc;
324
325         rc = kstrtouint(buffer, 0, &pattern);
326         if (rc)
327                 return rc;
328
329         if (is_mdt)
330                 lod_fix_lmv_desc_pattern(&pattern);
331         else
332                 lod_fix_desc_pattern(&pattern);
333
334         ltd->ltd_lov_desc.ld_pattern = pattern;
335
336         return count;
337 }
338
339 static ssize_t mdt_stripetype_store(struct kobject *kobj,
340                                     struct attribute *attr, const char *buffer,
341                                     size_t count)
342 {
343         return __stripetype_store(kobj, attr, buffer, count, true);
344 }
345
346 static ssize_t stripetype_store(struct kobject *kobj,
347                                     struct attribute *attr, const char *buffer,
348                                     size_t count)
349 {
350         return __stripetype_store(kobj, attr, buffer, count, false);
351 }
352
353 LUSTRE_RW_ATTR(mdt_stripetype);
354 LUSTRE_RW_ATTR(stripetype);
355
356 /**
357  * Show default number of stripes.
358  */
359 static ssize_t __stripecount_show(struct kobject *kobj, struct attribute *attr,
360                                   char *buf, bool is_mdt)
361 {
362         struct dt_device *dt = container_of(kobj, struct dt_device,
363                                             dd_kobj);
364         struct lod_device *lod = dt2lod_dev(dt);
365         struct lov_desc *desc = is_mdt ? &lod->lod_mdt_descs.ltd_lov_desc :
366                                          &lod->lod_ost_descs.ltd_lov_desc;
367
368         return snprintf(buf, PAGE_SIZE, "%d\n",
369                       (s16)(desc->ld_default_stripe_count + 1) - 1);
370 }
371
372 static ssize_t mdt_stripecount_show(struct kobject *kobj,
373                                     struct attribute *attr, char *buf)
374 {
375         return __stripecount_show(kobj, attr, buf, true);
376 }
377
378 static ssize_t stripecount_show(struct kobject *kobj, struct attribute *attr,
379                                 char *buf)
380 {
381         return __stripecount_show(kobj, attr, buf, false);
382 }
383
384 /**
385  * Set default number of stripes.
386  */
387 static ssize_t __stripecount_store(struct kobject *kobj, struct attribute *attr,
388                                    const char *buffer, size_t count,
389                                    bool is_mdt)
390 {
391         struct dt_device *dt = container_of(kobj, struct dt_device,
392                                             dd_kobj);
393         struct lod_device *lod = dt2lod_dev(dt);
394         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
395                                             &lod->lod_ost_descs;
396         int stripe_count;
397         int rc;
398
399         rc = kstrtoint(buffer, 0, &stripe_count);
400         if (rc)
401                 return rc;
402
403         if (stripe_count < -1)
404                 return -ERANGE;
405
406         lod_fix_desc_stripe_count(&stripe_count);
407         ltd->ltd_lov_desc.ld_default_stripe_count = stripe_count;
408
409         return count;
410 }
411
412 static ssize_t mdt_stripecount_store(struct kobject *kobj,
413                                      struct attribute *attr,
414                                      const char *buffer, size_t count)
415 {
416         return __stripecount_store(kobj, attr, buffer, count, true);
417 }
418
419 static ssize_t stripecount_store(struct kobject *kobj,
420                                  struct attribute *attr,
421                                  const char *buffer, size_t count)
422 {
423         return __stripecount_store(kobj, attr, buffer, count, false);
424 }
425
426 LUSTRE_RW_ATTR(mdt_stripecount);
427 LUSTRE_RW_ATTR(stripecount);
428
429 /**
430  * Show number of targets.
431  */
432 static ssize_t __numobd_show(struct kobject *kobj, struct attribute *attr,
433                              char *buf, bool is_mdt)
434 {
435         struct dt_device *dt = container_of(kobj, struct dt_device,
436                                             dd_kobj);
437         struct lod_device *lod = dt2lod_dev(dt);
438         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
439                                             &lod->lod_ost_descs;
440
441         return snprintf(buf, PAGE_SIZE, "%u\n", ltd->ltd_lov_desc.ld_tgt_count);
442 }
443
444 static ssize_t mdt_numobd_show(struct kobject *kobj, struct attribute *attr,
445                                char *buf)
446 {
447         return __numobd_show(kobj, attr, buf, true);
448 }
449
450 static ssize_t numobd_show(struct kobject *kobj, struct attribute *attr,
451                            char *buf)
452 {
453         return __numobd_show(kobj, attr, buf, false);
454 }
455
456 LUSTRE_RO_ATTR(mdt_numobd);
457 LUSTRE_RO_ATTR(numobd);
458
459 /**
460  * Show number of active targets.
461  */
462 static ssize_t __activeobd_show(struct kobject *kobj, struct attribute *attr,
463                                 char *buf, bool is_mdt)
464 {
465         struct dt_device *dt = container_of(kobj, struct dt_device,
466                                             dd_kobj);
467         struct lod_device *lod = dt2lod_dev(dt);
468         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
469                                             &lod->lod_ost_descs;
470
471         return snprintf(buf, PAGE_SIZE, "%u\n",
472                         ltd->ltd_lov_desc.ld_active_tgt_count);
473 }
474
475 static ssize_t mdt_activeobd_show(struct kobject *kobj, struct attribute *attr,
476                                   char *buf)
477 {
478         return __activeobd_show(kobj, attr, buf, true);
479 }
480
481 static ssize_t activeobd_show(struct kobject *kobj, struct attribute *attr,
482                               char *buf)
483 {
484         return __activeobd_show(kobj, attr, buf, false);
485 }
486
487 LUSTRE_RO_ATTR(mdt_activeobd);
488 LUSTRE_RO_ATTR(activeobd);
489
490 /**
491  * Show UUID of LOD device.
492  */
493 static ssize_t desc_uuid_show(struct kobject *kobj, struct attribute *attr,
494                               char *buf)
495 {
496         struct dt_device *dt = container_of(kobj, struct dt_device,
497                                             dd_kobj);
498         struct lod_device *lod = dt2lod_dev(dt);
499
500         return snprintf(buf, PAGE_SIZE, "%s\n",
501                        lod->lod_ost_descs.ltd_lov_desc.ld_uuid.uuid);
502 }
503 LUSTRE_RO_ATTR(desc_uuid);
504
505 /**
506  * Show QoS priority parameter.
507  *
508  * The printed value is a percentage value (0-100%) indicating the priority
509  * of free space compared to performance. 0% means select OSTs equally
510  * regardless of their free space, 100% means select OSTs only by their free
511  * space even if it results in very imbalanced load on the OSTs.
512  */
513 static ssize_t __qos_prio_free_show(struct kobject *kobj,
514                                     struct attribute *attr, char *buf,
515                                     bool is_mdt)
516 {
517         struct dt_device *dt = container_of(kobj, struct dt_device,
518                                             dd_kobj);
519         struct lod_device *lod = dt2lod_dev(dt);
520         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
521                                             &lod->lod_ost_descs;
522
523         return snprintf(buf, PAGE_SIZE, "%d%%\n",
524                        (ltd->ltd_qos.lq_prio_free * 100 + 255) >> 8);
525 }
526
527 static ssize_t mdt_qos_prio_free_show(struct kobject *kobj,
528                                       struct attribute *attr, char *buf)
529 {
530         return __qos_prio_free_show(kobj, attr, buf, true);
531 }
532
533 static ssize_t qos_prio_free_show(struct kobject *kobj,
534                                   struct attribute *attr, char *buf)
535 {
536         return __qos_prio_free_show(kobj, attr, buf, false);
537 }
538
539 /**
540  * Set QoS free space priority parameter.
541  *
542  * Set the relative priority of free OST space compared to OST load when OSTs
543  * are space imbalanced.  See qos_priofree_show() for description of
544  * this parameter.  See qos_threshold_rr_store() and lq_threshold_rr to
545  * determine what constitutes "space imbalanced" OSTs.
546  */
547 static ssize_t __qos_prio_free_store(struct kobject *kobj,
548                                      struct attribute *attr,
549                                      const char *buffer, size_t count,
550                                      bool is_mdt)
551 {
552         struct dt_device *dt = container_of(kobj, struct dt_device,
553                                             dd_kobj);
554         struct lod_device *lod = dt2lod_dev(dt);
555         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
556                                             &lod->lod_ost_descs;
557         unsigned int val;
558         int rc;
559
560         rc = kstrtouint(buffer, 0, &val);
561         if (rc)
562                 return rc;
563
564         if (val > 100)
565                 return -EINVAL;
566         ltd->ltd_qos.lq_prio_free = (val << 8) / 100;
567         set_bit(LQ_DIRTY, &ltd->ltd_qos.lq_flags);
568         set_bit(LQ_RESET, &ltd->ltd_qos.lq_flags);
569
570         return count;
571 }
572
573 static ssize_t mdt_qos_prio_free_store(struct kobject *kobj,
574                                        struct attribute *attr,
575                                        const char *buffer, size_t count)
576 {
577         return __qos_prio_free_store(kobj, attr, buffer, count, true);
578 }
579
580 static ssize_t qos_prio_free_store(struct kobject *kobj, struct attribute *attr,
581                                    const char *buffer, size_t count)
582 {
583         return __qos_prio_free_store(kobj, attr, buffer, count, false);
584 }
585
586 LUSTRE_RW_ATTR(mdt_qos_prio_free);
587 LUSTRE_RW_ATTR(qos_prio_free);
588
589 /**
590  * Show threshold for "same space on all OSTs" rule.
591  */
592 static ssize_t __qos_threshold_rr_show(struct kobject *kobj,
593                                        struct attribute *attr, char *buf,
594                                        bool is_mdt)
595 {
596         struct dt_device *dt = container_of(kobj, struct dt_device,
597                                             dd_kobj);
598         struct lod_device *lod = dt2lod_dev(dt);
599         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
600                                             &lod->lod_ost_descs;
601
602         return snprintf(buf, PAGE_SIZE, "%d%%\n",
603                        (ltd->ltd_qos.lq_threshold_rr * 100 + 255) >> 8);
604 }
605
606 static ssize_t mdt_qos_threshold_rr_show(struct kobject *kobj,
607                                          struct attribute *attr, char *buf)
608 {
609         return __qos_threshold_rr_show(kobj, attr, buf, true);
610 }
611
612 static ssize_t qos_threshold_rr_show(struct kobject *kobj,
613                                      struct attribute *attr, char *buf)
614 {
615         return __qos_threshold_rr_show(kobj, attr, buf, false);
616 }
617
618 /**
619  * Set threshold for "same space on all OSTs" rule.
620  *
621  * This sets the maximum percentage difference of free space between the most
622  * full and most empty OST in the currently available OSTs. If this percentage
623  * is exceeded, use the QoS allocator to select OSTs based on their available
624  * space so that more full OSTs are chosen less often, otherwise use the
625  * round-robin allocator for efficiency and performance.
626  */
627 static ssize_t __qos_threshold_rr_store(struct kobject *kobj,
628                                         struct attribute *attr,
629                                         const char *buffer, size_t count,
630                                         bool is_mdt)
631 {
632         struct dt_device *dt = container_of(kobj, struct dt_device,
633                                             dd_kobj);
634         struct lod_device *lod = dt2lod_dev(dt);
635         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
636                                             &lod->lod_ost_descs;
637         char buf[6], *tmp;
638         unsigned int val;
639         int rc;
640
641         /* "100%\n\0" should be largest string */
642         if (count >= sizeof(buf))
643                 return -ERANGE;
644
645         strncpy(buf, buffer, sizeof(buf));
646         buf[sizeof(buf) - 1] = '\0';
647         tmp = strchr(buf, '%');
648         if (tmp)
649                 *tmp = '\0';
650
651         rc = kstrtouint(buf, 0, &val);
652         if (rc)
653                 return rc;
654
655         if (val > 100)
656                 return -EINVAL;
657         ltd->ltd_qos.lq_threshold_rr = (val << 8) / 100;
658         set_bit(LQ_DIRTY, &ltd->ltd_qos.lq_flags);
659
660         return count;
661 }
662
663 static ssize_t mdt_qos_threshold_rr_store(struct kobject *kobj,
664                                           struct attribute *attr,
665                                           const char *buffer, size_t count)
666 {
667         return __qos_threshold_rr_store(kobj, attr, buffer, count, true);
668 }
669
670 static ssize_t qos_threshold_rr_store(struct kobject *kobj,
671                                       struct attribute *attr,
672                                       const char *buffer, size_t count)
673 {
674         return __qos_threshold_rr_store(kobj, attr, buffer, count, false);
675 }
676
677 LUSTRE_RW_ATTR(mdt_qos_threshold_rr);
678 LUSTRE_RW_ATTR(qos_threshold_rr);
679
680 /**
681  * Show expiration period used to refresh cached statfs data, which
682  * is used to implement QoS/RR striping allocation algorithm.
683  */
684 static ssize_t __qos_maxage_show(struct kobject *kobj, struct attribute *attr,
685                                  char *buf, bool is_mdt)
686 {
687         struct dt_device *dt = container_of(kobj, struct dt_device,
688                                             dd_kobj);
689         struct lod_device *lod = dt2lod_dev(dt);
690         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
691                                             &lod->lod_ost_descs;
692
693         return snprintf(buf, PAGE_SIZE, "%u Sec\n",
694                        ltd->ltd_lov_desc.ld_qos_maxage);
695 }
696
697 static ssize_t mdt_qos_maxage_show(struct kobject *kobj, struct attribute *attr,
698                                    char *buf)
699 {
700         return __qos_maxage_show(kobj, attr, buf, true);
701 }
702
703 static ssize_t qos_maxage_show(struct kobject *kobj, struct attribute *attr,
704                                char *buf)
705 {
706         return __qos_maxage_show(kobj, attr, buf, true);
707 }
708
709 /**
710  * Set expiration period used to refresh cached statfs data.
711  */
712 static ssize_t __qos_maxage_store(struct kobject *kobj, struct attribute *attr,
713                                   const char *buffer, size_t count, bool is_mdt)
714 {
715         struct dt_device *dt = container_of(kobj, struct dt_device,
716                                             dd_kobj);
717         struct lod_device *lod = dt2lod_dev(dt);
718         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
719                                             &lod->lod_ost_descs;
720         struct lustre_cfg_bufs bufs;
721         struct lu_device *next;
722         struct lustre_cfg *lcfg;
723         char str[32];
724         struct lu_tgt_desc *tgt;
725         int rc;
726         u32 val;
727
728         rc = kstrtouint(buffer, 0, &val);
729         if (rc)
730                 return rc;
731
732         if (val <= 0)
733                 return -EINVAL;
734
735         ltd->ltd_lov_desc.ld_qos_maxage = val;
736
737         /*
738          * propogate the value down to OSPs
739          */
740         lustre_cfg_bufs_reset(&bufs, NULL);
741         snprintf(str, 32, "%smaxage=%u", PARAM_OSP, val);
742         lustre_cfg_bufs_set_string(&bufs, 1, str);
743         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
744         if (lcfg == NULL)
745                 return -ENOMEM;
746         lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
747
748         lod_getref(ltd);
749         ltd_foreach_tgt(ltd, tgt) {
750                 next = &tgt->ltd_tgt->dd_lu_dev;
751                 rc = next->ld_ops->ldo_process_config(NULL, next, lcfg);
752                 if (rc)
753                         CERROR("can't set maxage on #%d: %d\n",
754                                tgt->ltd_index, rc);
755         }
756         lod_putref(lod, ltd);
757         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
758
759         return count;
760 }
761
762 static ssize_t mdt_qos_maxage_store(struct kobject *kobj,
763                                     struct attribute *attr,
764                                     const char *buffer, size_t count)
765 {
766         return __qos_maxage_store(kobj, attr, buffer, count, true);
767 }
768
769 static ssize_t qos_maxage_store(struct kobject *kobj, struct attribute *attr,
770                                 const char *buffer, size_t count)
771 {
772         return __qos_maxage_store(kobj, attr, buffer, count, false);
773 }
774
775 LUSTRE_RW_ATTR(mdt_qos_maxage);
776 LUSTRE_RW_ATTR(qos_maxage);
777
778 static void *lod_tgts_seq_start(struct seq_file *p, loff_t *pos, bool is_mdt)
779 {
780         struct obd_device *obd = p->private;
781         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
782         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
783                                             &lod->lod_ost_descs;
784
785         LASSERT(obd != NULL);
786
787         lod_getref(ltd); /* released in lod_tgts_seq_stop */
788         if (*pos >= ltd->ltd_tgts_size)
789                 return NULL;
790
791         *pos = find_next_bit(ltd->ltd_tgt_bitmap,
792                              ltd->ltd_tgts_size, *pos);
793         if (*pos < ltd->ltd_tgts_size)
794                 return LTD_TGT(ltd, *pos);
795         else
796                 return NULL;
797 }
798
799 static void *lod_mdts_seq_start(struct seq_file *p, loff_t *pos)
800 {
801         return lod_tgts_seq_start(p, pos, true);
802 }
803
804 static void *lod_osts_seq_start(struct seq_file *p, loff_t *pos)
805 {
806         return lod_tgts_seq_start(p, pos, false);
807 }
808
809 static void lod_tgts_seq_stop(struct seq_file *p, void *v, bool is_mdt)
810 {
811         struct obd_device *obd = p->private;
812         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
813         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
814                                             &lod->lod_ost_descs;
815
816         LASSERT(obd != NULL);
817         lod_putref(lod, ltd);
818 }
819
820 static void lod_mdts_seq_stop(struct seq_file *p, void *v)
821 {
822         lod_tgts_seq_stop(p, v, true);
823 }
824
825 static void lod_osts_seq_stop(struct seq_file *p, void *v)
826 {
827         lod_tgts_seq_stop(p, v, false);
828 }
829
830 static void *lod_tgts_seq_next(struct seq_file *p, void *v, loff_t *pos,
831                                bool is_mdt)
832 {
833         struct obd_device *obd = p->private;
834         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
835         struct lu_tgt_descs *ltd = is_mdt ? &lod->lod_mdt_descs :
836                                             &lod->lod_ost_descs;
837
838         (*pos)++;
839         if (*pos > ltd->ltd_tgts_size - 1)
840                 return NULL;
841
842         *pos = find_next_bit(ltd->ltd_tgt_bitmap,
843                              ltd->ltd_tgts_size, *pos);
844         if (*pos < ltd->ltd_tgts_size)
845                 return LTD_TGT(ltd, *pos);
846         else
847                 return NULL;
848 }
849
850 static void *lod_mdts_seq_next(struct seq_file *p, void *v, loff_t *pos)
851 {
852         return lod_tgts_seq_next(p, v, pos, true);
853 }
854
855 static void *lod_osts_seq_next(struct seq_file *p, void *v, loff_t *pos)
856 {
857         return lod_tgts_seq_next(p, v, pos, false);
858 }
859
860 /**
861  * Show active/inactive status for OST found by lod_osts_seq_next().
862  *
863  * \param[in] m         seq file
864  * \param[in] v         unused for single entry
865  *
866  * \retval 0            on success
867  * \retval negative     error code if failed
868  */
869 static int lod_tgts_seq_show(struct seq_file *p, void *v)
870 {
871         struct obd_device *obd = p->private;
872         struct lu_tgt_desc *tgt = v;
873         struct dt_device *next;
874         int rc, active;
875
876         LASSERT(obd->obd_lu_dev);
877
878         next = tgt->ltd_tgt;
879         if (!next)
880                 return -EINVAL;
881
882         /* XXX: should be non-NULL env, but it's very expensive */
883         active = 1;
884         rc = dt_statfs(NULL, next, &tgt->ltd_statfs);
885         if (rc == -ENOTCONN) {
886                 active = 0;
887                 rc = 0;
888         } else if (rc)
889                 return rc;
890
891         seq_printf(p, "%d: %s %sACTIVE\n", tgt->ltd_index,
892                    obd_uuid2str(&tgt->ltd_uuid),
893                    active ? "" : "IN");
894         return 0;
895 }
896
897 static const struct seq_operations lod_mdts_sops = {
898         .start  = lod_mdts_seq_start,
899         .stop   = lod_mdts_seq_stop,
900         .next   = lod_mdts_seq_next,
901         .show   = lod_tgts_seq_show,
902 };
903
904 static const struct seq_operations lod_osts_sops = {
905         .start  = lod_osts_seq_start,
906         .stop   = lod_osts_seq_stop,
907         .next   = lod_osts_seq_next,
908         .show   = lod_tgts_seq_show,
909 };
910
911 static int lod_mdts_seq_open(struct inode *inode, struct file *file)
912 {
913         struct seq_file *seq;
914         int rc;
915
916         rc = seq_open(file, &lod_mdts_sops);
917         if (rc)
918                 return rc;
919
920         seq = file->private_data;
921         seq->private = PDE_DATA(inode);
922         return 0;
923 }
924
925 static int lod_osts_seq_open(struct inode *inode, struct file *file)
926 {
927         struct seq_file *seq;
928         int rc;
929
930         rc = seq_open(file, &lod_osts_sops);
931         if (rc)
932                 return rc;
933
934         seq = file->private_data;
935         seq->private = PDE_DATA(inode);
936         return 0;
937 }
938
939 /**
940  * Show whether special failout mode for testing is enabled or not.
941  */
942 static ssize_t lmv_failout_show(struct kobject *kobj, struct attribute *attr,
943                                 char *buf)
944 {
945         struct dt_device *dt = container_of(kobj, struct dt_device,
946                                             dd_kobj);
947         struct lod_device *lod = dt2lod_dev(dt);
948
949         return snprintf(buf, PAGE_SIZE, "%d\n", lod->lod_lmv_failout ? 1 : 0);
950 }
951
952 /**
953  * Enable/disable a special failout mode for testing.
954  *
955  * This determines whether the LMV will try to continue processing a striped
956  * directory even if it has a (partly) corrupted entry in the master directory,
957  * or if it will abort upon finding a corrupted slave directory entry.
958  */
959 static ssize_t lmv_failout_store(struct kobject *kobj, struct attribute *attr,
960                                  const char *buffer, size_t count)
961 {
962         struct dt_device *dt = container_of(kobj, struct dt_device,
963                                             dd_kobj);
964         struct lod_device *lod = dt2lod_dev(dt);
965         bool val = 0;
966         int rc;
967
968         rc = kstrtobool(buffer, &val);
969         if (rc)
970                 return rc;
971
972         lod->lod_lmv_failout = val;
973
974         return count;
975 }
976 LUSTRE_RW_ATTR(lmv_failout);
977
978 char *mdt_hash_name[] = { "none",
979                           LMV_HASH_NAME_ALL_CHARS,
980                           LMV_HASH_NAME_FNV_1A_64,
981                           LMV_HASH_NAME_CRUSH,
982 };
983
984 static ssize_t mdt_hash_show(struct kobject *kobj, struct attribute *attr,
985                              char *buf)
986 {
987         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
988         struct lod_device *lod = dt2lod_dev(dt);
989
990         return snprintf(buf, PAGE_SIZE, "%s\n",
991                 mdt_hash_name[lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern]);
992 }
993
994 static ssize_t mdt_hash_store(struct kobject *kobj, struct attribute *attr,
995                               const char *buffer, size_t count)
996 {
997         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
998         struct lod_device *lod = dt2lod_dev(dt);
999         char *hash;
1000         int len;
1001         int i;
1002
1003         hash = kstrndup(buffer, count, GFP_KERNEL);
1004         if (!hash)
1005                 return -ENOMEM;
1006
1007         len = strcspn(hash, "\n ");
1008         hash[len] = '\0';
1009         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++) {
1010                 if (!strcmp(hash, mdt_hash_name[i])) {
1011                         lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern = i;
1012                         kfree(hash);
1013                         return count;
1014                 }
1015         }
1016         kfree(hash);
1017
1018         return -EINVAL;
1019 }
1020 LUSTRE_RW_ATTR(mdt_hash);
1021
1022 static struct lprocfs_vars lprocfs_lod_obd_vars[] = {
1023         { NULL }
1024 };
1025
1026 static const struct file_operations lod_proc_mdt_fops = {
1027         .owner   = THIS_MODULE,
1028         .open    = lod_mdts_seq_open,
1029         .read    = seq_read,
1030         .llseek  = seq_lseek,
1031         .release = lprocfs_seq_release,
1032 };
1033
1034 static const struct file_operations lod_proc_target_fops = {
1035         .owner   = THIS_MODULE,
1036         .open    = lod_osts_seq_open,
1037         .read    = seq_read,
1038         .llseek  = seq_lseek,
1039         .release = lprocfs_seq_release,
1040 };
1041
1042 static struct attribute *lod_attrs[] = {
1043         &lustre_attr_dom_stripesize.attr,
1044         &lustre_attr_dom_stripesize_max_kb.attr,
1045         &lustre_attr_dom_stripesize_cur_kb.attr,
1046         &lustre_attr_dom_threshold_free_mb.attr,
1047         &lustre_attr_stripesize.attr,
1048         &lustre_attr_stripeoffset.attr,
1049         &lustre_attr_stripecount.attr,
1050         &lustre_attr_stripetype.attr,
1051         &lustre_attr_activeobd.attr,
1052         &lustre_attr_desc_uuid.attr,
1053         &lustre_attr_lmv_failout.attr,
1054         &lustre_attr_numobd.attr,
1055         &lustre_attr_qos_maxage.attr,
1056         &lustre_attr_qos_prio_free.attr,
1057         &lustre_attr_qos_threshold_rr.attr,
1058         &lustre_attr_mdt_stripecount.attr,
1059         &lustre_attr_mdt_stripetype.attr,
1060         &lustre_attr_mdt_activeobd.attr,
1061         &lustre_attr_mdt_numobd.attr,
1062         &lustre_attr_mdt_qos_maxage.attr,
1063         &lustre_attr_mdt_qos_prio_free.attr,
1064         &lustre_attr_mdt_qos_threshold_rr.attr,
1065         &lustre_attr_mdt_hash.attr,
1066         NULL,
1067 };
1068
1069 /**
1070  * Initialize procfs entries for LOD.
1071  *
1072  * \param[in] lod       LOD device
1073  *
1074  * \retval 0            on success
1075  * \retval negative     error code if failed
1076  */
1077 int lod_procfs_init(struct lod_device *lod)
1078 {
1079         struct ldebugfs_vars ldebugfs_obd_vars[] = { { NULL } };
1080         struct obd_device *obd = lod2obd(lod);
1081         struct obd_type *type;
1082         struct kobject *lov;
1083         int rc;
1084
1085         lod->lod_dt_dev.dd_ktype.default_attrs = lod_attrs;
1086         rc = dt_tunables_init(&lod->lod_dt_dev, obd->obd_type, obd->obd_name,
1087                               ldebugfs_obd_vars);
1088         if (rc) {
1089                 CERROR("%s: failed to setup DT tunables: %d\n",
1090                        obd->obd_name, rc);
1091                 RETURN(rc);
1092         }
1093
1094         obd->obd_vars = lprocfs_lod_obd_vars;
1095         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1096                                                obd->obd_type->typ_procroot,
1097                                                obd->obd_vars, obd);
1098         if (IS_ERR(obd->obd_proc_entry)) {
1099                 rc = PTR_ERR(obd->obd_proc_entry);
1100                 CERROR("%s: error %d setting up lprocfs\n",
1101                        obd->obd_name, rc);
1102                 GOTO(out, rc);
1103         }
1104
1105         rc = lprocfs_seq_create(obd->obd_proc_entry, "mdt_obd",
1106                                 0444, &lod_proc_mdt_fops, obd);
1107         if (rc) {
1108                 CWARN("%s: Error adding the target_obd file %d\n",
1109                       obd->obd_name, rc);
1110                 GOTO(out, rc);
1111         }
1112
1113         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1114                                 0444, &lod_proc_target_fops, obd);
1115         if (rc) {
1116                 CWARN("%s: Error adding the target_obd file %d\n",
1117                       obd->obd_name, rc);
1118                 GOTO(out, rc);
1119         }
1120
1121         lod->lod_pool_proc_entry = lprocfs_register("pools",
1122                                                     obd->obd_proc_entry,
1123                                                     NULL, NULL);
1124         if (IS_ERR(lod->lod_pool_proc_entry)) {
1125                 rc = PTR_ERR(lod->lod_pool_proc_entry);
1126                 lod->lod_pool_proc_entry = NULL;
1127                 CWARN("%s: Failed to create pool proc file: %d\n",
1128                       obd->obd_name, rc);
1129                 GOTO(out, rc);
1130         }
1131
1132         lov = kset_find_obj(lustre_kset, "lov");
1133         if (!lov) {
1134                 CERROR("%s: lov subsystem not found\n", obd->obd_name);
1135                 GOTO(out, rc = -ENODEV);
1136         }
1137
1138         rc = sysfs_create_link(lov, &lod->lod_dt_dev.dd_kobj,
1139                                obd->obd_name);
1140         if (rc)
1141                 CERROR("%s: failed to create LOV sysfs symlink\n",
1142                        obd->obd_name);
1143         kobject_put(lov);
1144
1145         lod->lod_debugfs = ldebugfs_add_symlink(obd->obd_name, "lov",
1146                                                 "../lod/%s", obd->obd_name);
1147         if (!lod->lod_debugfs)
1148                 CERROR("%s: failed to create LOV debugfs symlink\n",
1149                        obd->obd_name);
1150
1151         type = container_of(lov, struct obd_type, typ_kobj);
1152         if (!type->typ_procroot)
1153                 RETURN(0);
1154
1155         /* for compatibility we link old procfs's LOV entries to lod ones */
1156         lod->lod_symlink = lprocfs_add_symlink(obd->obd_name,
1157                                                type->typ_procroot,
1158                                                "../lod/%s", obd->obd_name);
1159         if (lod->lod_symlink == NULL)
1160                 CERROR("cannot create LOV symlink for /proc/fs/lustre/lod/%s\n",
1161                        obd->obd_name);
1162         RETURN(0);
1163
1164 out:
1165         dt_tunables_fini(&lod->lod_dt_dev);
1166
1167         return rc;
1168 }
1169
1170 /**
1171  * Cleanup procfs entries registred for LOD.
1172  *
1173  * \param[in] lod       LOD device
1174  */
1175 void lod_procfs_fini(struct lod_device *lod)
1176 {
1177         struct obd_device *obd = lod2obd(lod);
1178         struct kobject *lov;
1179
1180         if (lod->lod_symlink != NULL) {
1181                 lprocfs_remove(&lod->lod_symlink);
1182                 lod->lod_symlink = NULL;
1183         }
1184
1185         lov = kset_find_obj(lustre_kset, "lov");
1186         if (lov) {
1187                 sysfs_remove_link(lov, obd->obd_name);
1188                 kobject_put(lov);
1189         }
1190
1191         debugfs_remove_recursive(lod->lod_debugfs);
1192
1193         if (obd->obd_proc_entry) {
1194                 lprocfs_remove(&obd->obd_proc_entry);
1195                 obd->obd_proc_entry = NULL;
1196         }
1197
1198         dt_tunables_fini(&lod->lod_dt_dev);
1199 }
1200
1201 #endif /* CONFIG_PROC_FS */