Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[fs/lustre-release.git] / lustre / lmv / lproc_lmv.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) 2004, 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
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <linux/seq_file.h>
35 #include <linux/statfs.h>
36 #include <lprocfs_status.h>
37 #include <obd_class.h>
38
39 #include "lmv_internal.h"
40
41 static ssize_t numobd_show(struct kobject *kobj, struct attribute *attr,
42                            char *buf)
43 {
44         struct obd_device *obd = container_of(kobj, struct obd_device,
45                                               obd_kset.kobj);
46
47         return scnprintf(buf, PAGE_SIZE, "%u\n", obd->u.lmv.lmv_mdt_count);
48 }
49 LUSTRE_RO_ATTR(numobd);
50
51 static ssize_t activeobd_show(struct kobject *kobj, struct attribute *attr,
52                               char *buf)
53 {
54         struct obd_device *obd = container_of(kobj, struct obd_device,
55                                               obd_kset.kobj);
56
57         return scnprintf(buf, PAGE_SIZE, "%u\n",
58                 obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count);
59 }
60 LUSTRE_RO_ATTR(activeobd);
61
62 static ssize_t desc_uuid_show(struct kobject *kobj, struct attribute *attr,
63                               char *buf)
64 {
65         struct obd_device *obd = container_of(kobj, struct obd_device,
66                                               obd_kset.kobj);
67
68         return scnprintf(buf, PAGE_SIZE, "%s\n",
69                         obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_uuid.uuid);
70 }
71 LUSTRE_RO_ATTR(desc_uuid);
72
73 static ssize_t qos_maxage_show(struct kobject *kobj,
74                                struct attribute *attr,
75                                char *buf)
76 {
77         struct obd_device *obd = container_of(kobj, struct obd_device,
78                                               obd_kset.kobj);
79
80         return scnprintf(buf, PAGE_SIZE, "%u\n",
81                         obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage);
82 }
83
84 static ssize_t qos_maxage_store(struct kobject *kobj,
85                                 struct attribute *attr,
86                                 const char *buffer,
87                                 size_t count)
88 {
89         struct obd_device *obd = container_of(kobj, struct obd_device,
90                                               obd_kset.kobj);
91         unsigned int val;
92         int rc;
93
94         rc = kstrtouint(buffer, 0, &val);
95         if (rc)
96                 return rc;
97
98         obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage = val;
99
100         return count;
101 }
102 LUSTRE_RW_ATTR(qos_maxage);
103
104 static ssize_t qos_prio_free_show(struct kobject *kobj,
105                                   struct attribute *attr,
106                                   char *buf)
107 {
108         struct obd_device *obd = container_of(kobj, struct obd_device,
109                                               obd_kset.kobj);
110
111         return scnprintf(buf, PAGE_SIZE, "%u%%\n",
112                         (obd->u.lmv.lmv_qos.lq_prio_free * 100 + 255) >> 8);
113 }
114
115 static ssize_t qos_prio_free_store(struct kobject *kobj,
116                                    struct attribute *attr,
117                                    const char *buffer,
118                                    size_t count)
119 {
120         struct obd_device *obd = container_of(kobj, struct obd_device,
121                                               obd_kset.kobj);
122         struct lmv_obd *lmv = &obd->u.lmv;
123         char buf[6], *tmp;
124         unsigned int val;
125         int rc;
126
127         /* "100%\n\0" should be largest string */
128         if (count >= sizeof(buf))
129                 return -ERANGE;
130
131         strncpy(buf, buffer, sizeof(buf));
132         buf[sizeof(buf) - 1] = '\0';
133         tmp = strchr(buf, '%');
134         if (tmp)
135                 *tmp = '\0';
136
137         rc = kstrtouint(buf, 0, &val);
138         if (rc)
139                 return rc;
140
141         if (val > 100)
142                 return -EINVAL;
143
144         lmv->lmv_qos.lq_prio_free = (val << 8) / 100;
145         set_bit(LQ_DIRTY, &lmv->lmv_qos.lq_flags);
146         set_bit(LQ_RESET, &lmv->lmv_qos.lq_flags);
147
148         return count;
149 }
150 LUSTRE_RW_ATTR(qos_prio_free);
151
152 static ssize_t qos_threshold_rr_show(struct kobject *kobj,
153                                      struct attribute *attr,
154                                      char *buf)
155 {
156         struct obd_device *obd = container_of(kobj, struct obd_device,
157                                               obd_kset.kobj);
158
159         return scnprintf(buf, PAGE_SIZE, "%u%%\n",
160                         (obd->u.lmv.lmv_qos.lq_threshold_rr * 100 +
161                         (QOS_THRESHOLD_MAX - 1)) / QOS_THRESHOLD_MAX);
162 }
163
164 static ssize_t qos_threshold_rr_store(struct kobject *kobj,
165                                       struct attribute *attr,
166                                       const char *buffer,
167                                       size_t count)
168 {
169         struct obd_device *obd = container_of(kobj, struct obd_device,
170                                               obd_kset.kobj);
171         struct lmv_obd *lmv = &obd->u.lmv;
172         char buf[6], *tmp;
173         unsigned int val;
174         int rc;
175
176         /* "100%\n\0" should be largest string */
177         if (count >= sizeof(buf))
178                 return -ERANGE;
179
180         strncpy(buf, buffer, sizeof(buf));
181         buf[sizeof(buf) - 1] = '\0';
182         tmp = strchr(buf, '%');
183         if (tmp)
184                 *tmp = '\0';
185
186         rc = kstrtouint(buf, 0, &val);
187         if (rc)
188                 return rc;
189
190         if (val > 100)
191                 return -EINVAL;
192
193         lmv->lmv_qos.lq_threshold_rr = (val * QOS_THRESHOLD_MAX) / 100;
194         set_bit(LQ_DIRTY, &lmv->lmv_qos.lq_flags);
195
196         return count;
197 }
198 LUSTRE_RW_ATTR(qos_threshold_rr);
199
200 #ifdef CONFIG_PROC_FS
201 /* directories with exclude prefixes will be created on the same MDT as its
202  * parent directory, the prefixes are set with the rule as shell environment
203  * PATH: ':' is used as separator for prefixes. And for convenience, '+/-' is
204  * used to add/remove prefixes.
205  */
206 static int qos_exclude_prefixes_seq_show(struct seq_file *m, void *v)
207 {
208         struct obd_device *obd = m->private;
209         struct lmv_obd *lmv = &obd->u.lmv;
210         struct qos_exclude_prefix *prefix;
211
212 restart:
213         spin_lock(&lmv->lmv_lock);
214         list_for_each_entry(prefix, &lmv->lmv_qos_exclude_list, qep_list) {
215                 seq_printf(m, "%s\n", prefix->qep_name);
216                 if (seq_has_overflowed(m)) {
217                         spin_unlock(&lmv->lmv_lock);
218                         kvfree(m->buf);
219                         m->count = 0;
220                         m->buf = kvmalloc(m->size <<= 1, GFP_KERNEL_ACCOUNT);
221                         if (!m->buf)
222                                 return -ENOMEM;
223                         goto restart;
224                 }
225         }
226         spin_unlock(&lmv->lmv_lock);
227
228         return 0;
229 }
230
231 static ssize_t qos_exclude_prefixes_seq_write(struct file *file,
232                                               const char __user *buffer,
233                                               size_t count, loff_t *off)
234 {
235         struct obd_device *obd;
236         struct lmv_obd *lmv;
237         char *buf;
238         char op = 0;
239         char *p;
240         char *name;
241         char namebuf[NAME_MAX + 1];
242         struct qos_exclude_prefix *prefix;
243         struct qos_exclude_prefix *tmp;
244         int len;
245         bool pruned = false;
246         int rc;
247
248         /* one extra char to ensure buf ends with '\0' */
249         OBD_ALLOC(buf, count + 1);
250         if (!buf)
251                 return -ENOMEM;
252         if (copy_from_user(buf, buffer, count)) {
253                 OBD_FREE(buf, count + 1);
254                 return -EFAULT;
255         }
256
257         obd = ((struct seq_file *)file->private_data)->private;
258         lmv = &obd->u.lmv;
259         p = buf;
260         while (p) {
261                 while (*p == ':')
262                         p++;
263                 if (*p == '\0')
264                         break;
265                 if (*p == '+' || *p == '-')
266                         op = *p++;
267
268                 name = p;
269                 p = strchr(name, ':');
270                 if (p)
271                         len = p - name;
272                 else
273                         len = strlen(name);
274                 if (!len)
275                         break;
276                 if (len > NAME_MAX) {
277                         CERROR("%s: %s length exceeds NAME_MAX\n",
278                                obd->obd_name, name);
279                         OBD_FREE(buf, count + 1);
280                         return -ERANGE;
281                 }
282
283                 switch (op) {
284                 default:
285                         if (!pruned) {
286                                 spin_lock(&lmv->lmv_lock);
287                                 list_for_each_entry_safe(prefix, tmp,
288                                                 &lmv->lmv_qos_exclude_list,
289                                                 qep_list) {
290                                         list_del(&prefix->qep_list);
291                                         rhashtable_remove_fast(
292                                                 &lmv->lmv_qos_exclude_hash,
293                                                 &prefix->qep_hash,
294                                                 qos_exclude_hash_params);
295                                         kfree(prefix);
296                                 }
297                                 spin_unlock(&lmv->lmv_lock);
298                                 pruned = true;
299                         }
300                         fallthrough;
301                 case '+':
302                         prefix = kmalloc(sizeof(*prefix), __GFP_ZERO);
303                         if (!prefix) {
304                                 OBD_FREE(buf, count + 1);
305                                 return -ENOMEM;
306                         }
307                         strncpy(prefix->qep_name, name, len);
308                         rc = rhashtable_lookup_insert_fast(
309                                                 &lmv->lmv_qos_exclude_hash,
310                                                 &prefix->qep_hash,
311                                                 qos_exclude_hash_params);
312                         if (!rc) {
313                                 spin_lock(&lmv->lmv_lock);
314                                 list_add_tail(&prefix->qep_list,
315                                               &lmv->lmv_qos_exclude_list);
316                                 spin_unlock(&lmv->lmv_lock);
317                         } else {
318                                 kfree(prefix);
319                         }
320                         break;
321                 case '-':
322                         strncpy(namebuf, name, len);
323                         namebuf[len] = '\0';
324                         prefix = rhashtable_lookup(&lmv->lmv_qos_exclude_hash,
325                                                    namebuf,
326                                                    qos_exclude_hash_params);
327                         if (prefix) {
328                                 spin_lock(&lmv->lmv_lock);
329                                 list_del(&prefix->qep_list);
330                                 spin_unlock(&lmv->lmv_lock);
331                                 rhashtable_remove_fast(
332                                                 &lmv->lmv_qos_exclude_hash,
333                                                 &prefix->qep_hash,
334                                                 qos_exclude_hash_params);
335                                 kfree(prefix);
336                         }
337                         break;
338                 }
339         }
340
341         OBD_FREE(buf, count + 1);
342         return count;
343 }
344 LPROC_SEQ_FOPS(qos_exclude_prefixes);
345
346 static void *lmv_tgt_seq_start(struct seq_file *p, loff_t *pos)
347 {
348         struct obd_device *obd = p->private;
349         struct lmv_obd *lmv = &obd->u.lmv;
350         struct lu_tgt_desc *tgt;
351
352         while (*pos < lmv->lmv_mdt_descs.ltd_tgts_size) {
353                 tgt = lmv_tgt(lmv, (__u32)*pos);
354                 if (tgt)
355                         return tgt;
356
357                 ++*pos;
358         }
359
360         return NULL;
361 }
362
363 static void lmv_tgt_seq_stop(struct seq_file *p, void *v)
364 {
365 }
366
367 static void *lmv_tgt_seq_next(struct seq_file *p, void *v, loff_t *pos)
368 {
369         struct obd_device *obd = p->private;
370         struct lmv_obd *lmv = &obd->u.lmv;
371         struct lu_tgt_desc *tgt;
372
373         ++*pos;
374         while (*pos < lmv->lmv_mdt_descs.ltd_tgts_size) {
375                 tgt = lmv_tgt(lmv, (__u32)*pos);
376                 if (tgt)
377                         return tgt;
378
379                 ++*pos;
380         }
381
382         return NULL;
383 }
384
385 static int lmv_tgt_seq_show(struct seq_file *p, void *v)
386 {
387         struct lmv_tgt_desc     *tgt = v;
388
389         if (!tgt)
390                 return 0;
391
392         seq_printf(p, "%u: %s %sACTIVE\n",
393                    tgt->ltd_index, tgt->ltd_uuid.uuid,
394                    tgt->ltd_active ? "" : "IN");
395         return 0;
396 }
397
398 static const struct seq_operations lmv_tgt_sops = {
399         .start                 = lmv_tgt_seq_start,
400         .stop                  = lmv_tgt_seq_stop,
401         .next                  = lmv_tgt_seq_next,
402         .show                  = lmv_tgt_seq_show,
403 };
404
405 static int lmv_target_seq_open(struct inode *inode, struct file *file)
406 {
407         struct seq_file         *seq;
408         int                     rc;
409
410         rc = seq_open(file, &lmv_tgt_sops);
411         if (rc)
412                 return rc;
413
414         seq = file->private_data;
415         seq->private = pde_data(inode);
416         return 0;
417 }
418
419 static const struct proc_ops lmv_proc_target_fops = {
420         PROC_OWNER(THIS_MODULE)
421         .proc_open      = lmv_target_seq_open,
422         .proc_read      = seq_read,
423         .proc_lseek     = seq_lseek,
424         .proc_release   = seq_release,
425 };
426
427 struct lprocfs_vars lprocfs_lmv_obd_vars[] = {
428         { .name =       "qos_exclude_prefixes",
429           .fops =       &qos_exclude_prefixes_fops },
430         { .name =       "target_obd",
431           .fops =       &lmv_proc_target_fops },
432         { NULL }
433 };
434 #endif /* CONFIG_PROC_FS */
435
436 static struct attribute *lmv_attrs[] = {
437         &lustre_attr_activeobd.attr,
438         &lustre_attr_desc_uuid.attr,
439         &lustre_attr_numobd.attr,
440         &lustre_attr_qos_maxage.attr,
441         &lustre_attr_qos_prio_free.attr,
442         &lustre_attr_qos_threshold_rr.attr,
443         NULL,
444 };
445
446 KOBJ_ATTRIBUTE_GROUPS(lmv); /* creates lmv_groups */
447
448 int lmv_tunables_init(struct obd_device *obd)
449 {
450         int rc;
451
452         obd->obd_ktype.default_groups = KOBJ_ATTR_GROUPS(lmv);
453 #ifdef CONFIG_PROC_FS
454         obd->obd_vars = lprocfs_lmv_obd_vars;
455 #endif
456         rc = lprocfs_obd_setup(obd, true);
457         if (rc)
458                 goto out_failed;
459 #ifdef CONFIG_PROC_FS
460         rc = lprocfs_alloc_md_stats(obd, 0);
461         if (rc) {
462                 lprocfs_obd_cleanup(obd);
463                 goto out_failed;
464         }
465 #endif /* CONFIG_PROC_FS */
466 out_failed:
467         return rc;
468 }