Whamcloud - gitweb
LU-13128 osc: glimpse and lock cancel race
[fs/lustre-release.git] / lustre / mdc / lproc_mdc.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 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 <linux/vfs.h>
35 #include <obd_class.h>
36 #include <lprocfs_status.h>
37 #include <lustre_osc.h>
38 #include <cl_object.h>
39 #include "mdc_internal.h"
40
41 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
42                            char *buf)
43 {
44         struct obd_device *dev = container_of(kobj, struct obd_device,
45                                               obd_kset.kobj);
46         ssize_t len;
47
48         LPROCFS_CLIMP_CHECK(dev);
49         len = sprintf(buf, "%d\n", !dev->u.cli.cl_import->imp_deactive);
50         LPROCFS_CLIMP_EXIT(dev);
51         return len;
52 }
53
54 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
55                             const char *buffer, size_t count)
56 {
57         struct obd_device *dev = container_of(kobj, struct obd_device,
58                                               obd_kset.kobj);
59         bool val;
60         int rc;
61
62         rc = kstrtobool(buffer, &val);
63         if (rc)
64                 return rc;
65
66         /* opposite senses */
67         if (dev->u.cli.cl_import->imp_deactive == val)
68                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
69         else
70                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
71                        val);
72
73         return count;
74 }
75 LUSTRE_RW_ATTR(active);
76
77 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
78                                        struct attribute *attr,
79                                        char *buf)
80 {
81         struct obd_device *dev = container_of(kobj, struct obd_device,
82                                               obd_kset.kobj);
83         ssize_t len;
84         u32 max;
85
86         max = obd_get_max_rpcs_in_flight(&dev->u.cli);
87         len = sprintf(buf, "%u\n", max);
88
89         return len;
90 }
91
92 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
93                                         struct attribute *attr,
94                                         const char *buffer,
95                                         size_t count)
96 {
97         struct obd_device *dev = container_of(kobj, struct obd_device,
98                                               obd_kset.kobj);
99         unsigned int val;
100         int rc;
101
102         rc = kstrtouint(buffer, 10, &val);
103         if (rc)
104                 return rc;
105
106         rc = obd_set_max_rpcs_in_flight(&dev->u.cli, val);
107         if (rc)
108                 count = rc;
109
110         return count;
111 }
112 LUSTRE_RW_ATTR(max_rpcs_in_flight);
113
114 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
115                                            struct attribute *attr,
116                                            char *buf)
117 {
118         struct obd_device *dev = container_of(kobj, struct obd_device,
119                                               obd_kset.kobj);
120         u16 max;
121
122         max = obd_get_max_mod_rpcs_in_flight(&dev->u.cli);
123         return sprintf(buf, "%hu\n", max);
124 }
125
126 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
127                                             struct attribute *attr,
128                                             const char *buffer,
129                                             size_t count)
130 {
131         struct obd_device *dev = container_of(kobj, struct obd_device,
132                                               obd_kset.kobj);
133         u16 val;
134         int rc;
135
136         rc = kstrtou16(buffer, 10, &val);
137         if (rc)
138                 return rc;
139
140         rc = obd_set_max_mod_rpcs_in_flight(&dev->u.cli, val);
141         if (rc)
142                 count = rc;
143
144         return count;
145 }
146 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
147
148 static int mdc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
149 {
150         struct obd_device *dev = m->private;
151         struct client_obd *cli = &dev->u.cli;
152         unsigned long val;
153
154         spin_lock(&cli->cl_loi_list_lock);
155         val = PAGES_TO_MiB(cli->cl_dirty_max_pages);
156         spin_unlock(&cli->cl_loi_list_lock);
157
158         seq_printf(m, "%lu\n", val);
159         return 0;
160 }
161
162 static ssize_t mdc_max_dirty_mb_seq_write(struct file *file,
163                                           const char __user *buffer,
164                                           size_t count, loff_t *off)
165 {
166         struct seq_file *sfl = file->private_data;
167         struct obd_device *dev = sfl->private;
168         struct client_obd *cli = &dev->u.cli;
169         char kernbuf[22] = "";
170         u64 pages_number;
171         int rc;
172
173         if (count >= sizeof(kernbuf))
174                 return -EINVAL;
175
176         if (copy_from_user(kernbuf, buffer, count))
177                 return -EFAULT;
178         kernbuf[count] = 0;
179
180         rc = sysfs_memparse(kernbuf, count, &pages_number, "MiB");
181         if (rc < 0)
182                 return rc;
183
184         /* MB -> pages */
185         pages_number = round_up(pages_number, 1024 * 1024) >> PAGE_SHIFT;
186         if (pages_number <= 0 ||
187             pages_number >= MiB_TO_PAGES(OSC_MAX_DIRTY_MB_MAX) ||
188             pages_number > cfs_totalram_pages() / 4) /* 1/4 of RAM */
189                 return -ERANGE;
190
191         spin_lock(&cli->cl_loi_list_lock);
192         cli->cl_dirty_max_pages = pages_number;
193         osc_wake_cache_waiters(cli);
194         spin_unlock(&cli->cl_loi_list_lock);
195
196         return count;
197 }
198 LPROC_SEQ_FOPS(mdc_max_dirty_mb);
199
200 static ssize_t contention_seconds_show(struct kobject *kobj,
201                                        struct attribute *attr,
202                                        char *buf)
203 {
204         struct obd_device *obd = container_of(kobj, struct obd_device,
205                                               obd_kset.kobj);
206         struct osc_device *od = obd2osc_dev(obd);
207
208         return sprintf(buf, "%lld\n", od->od_contention_time);
209 }
210
211 static ssize_t contention_seconds_store(struct kobject *kobj,
212                                         struct attribute *attr,
213                                         const char *buffer,
214                                         size_t count)
215 {
216         struct obd_device *obd = container_of(kobj, struct obd_device,
217                                               obd_kset.kobj);
218         struct osc_device *od = obd2osc_dev(obd);
219         time64_t val;
220         int rc;
221
222         rc = kstrtoll(buffer, 0, &val);
223         if (rc)
224                 return rc;
225
226         od->od_contention_time = val;
227
228         return count;
229 }
230 LUSTRE_RW_ATTR(contention_seconds);
231
232 LUSTRE_ATTR(mds_conn_uuid, 0444, conn_uuid_show, NULL);
233 LUSTRE_RO_ATTR(conn_uuid);
234
235 LUSTRE_RW_ATTR(ping);
236
237 static int mdc_cached_mb_seq_show(struct seq_file *m, void *v)
238 {
239         struct obd_device *dev = m->private;
240         struct client_obd *cli = &dev->u.cli;
241         int shift = 20 - PAGE_SHIFT;
242
243         seq_printf(m, "used_mb: %ld\n"
244                    "busy_cnt: %ld\n"
245                    "reclaim: %llu\n",
246                    (atomic_long_read(&cli->cl_lru_in_list) +
247                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
248                     atomic_long_read(&cli->cl_lru_busy),
249                    cli->cl_lru_reclaim);
250
251         return 0;
252 }
253
254 /* shrink the number of caching pages to a specific number */
255 static ssize_t
256 mdc_cached_mb_seq_write(struct file *file, const char __user *buffer,
257                         size_t count, loff_t *off)
258 {
259         struct seq_file *sfl = file->private_data;
260         struct obd_device *dev = sfl->private;
261         struct client_obd *cli = &dev->u.cli;
262         u64 pages_number;
263         const char *tmp;
264         long rc;
265         char kernbuf[128];
266
267         if (count >= sizeof(kernbuf))
268                 return -EINVAL;
269
270         if (copy_from_user(kernbuf, buffer, count))
271                 return -EFAULT;
272         kernbuf[count] = 0;
273
274         tmp = lprocfs_find_named_value(kernbuf, "used_mb:", &count);
275         rc = sysfs_memparse(tmp, count, &pages_number, "MiB");
276         if (rc < 0)
277                 return rc;
278
279         pages_number >>= PAGE_SHIFT;
280
281         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
282         if (rc > 0) {
283                 struct lu_env *env;
284                 __u16 refcheck;
285
286                 env = cl_env_get(&refcheck);
287                 if (!IS_ERR(env)) {
288                         (void)osc_lru_shrink(env, cli, rc, true);
289                         cl_env_put(env, &refcheck);
290                 }
291         }
292
293         return count;
294 }
295 LPROC_SEQ_FOPS(mdc_cached_mb);
296
297 static int mdc_unstable_stats_seq_show(struct seq_file *m, void *v)
298 {
299         struct obd_device *dev = m->private;
300         struct client_obd *cli = &dev->u.cli;
301         long pages;
302         int mb;
303
304         pages = atomic_long_read(&cli->cl_unstable_count);
305         mb    = (pages * PAGE_SIZE) >> 20;
306
307         seq_printf(m, "unstable_pages: %20ld\n"
308                    "unstable_mb:              %10d\n", pages, mb);
309         return 0;
310 }
311 LPROC_SEQ_FOPS_RO(mdc_unstable_stats);
312
313 static ssize_t mdc_rpc_stats_seq_write(struct file *file,
314                                        const char __user *buf,
315                                        size_t len, loff_t *off)
316 {
317         struct seq_file *seq = file->private_data;
318         struct obd_device *dev = seq->private;
319         struct client_obd *cli = &dev->u.cli;
320
321         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
322
323         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
324         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
325         lprocfs_oh_clear(&cli->cl_read_page_hist);
326         lprocfs_oh_clear(&cli->cl_write_page_hist);
327         lprocfs_oh_clear(&cli->cl_read_offset_hist);
328         lprocfs_oh_clear(&cli->cl_write_offset_hist);
329
330         return len;
331 }
332
333 static int mdc_rpc_stats_seq_show(struct seq_file *seq, void *v)
334 {
335         struct obd_device *dev = seq->private;
336         struct client_obd *cli = &dev->u.cli;
337         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
338         int i;
339
340         obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
341
342         spin_lock(&cli->cl_loi_list_lock);
343
344         seq_printf(seq, "\nread RPCs in flight:  %d\n",
345                    cli->cl_r_in_flight);
346         seq_printf(seq, "write RPCs in flight: %d\n",
347                    cli->cl_w_in_flight);
348         seq_printf(seq, "pending write pages:  %d\n",
349                    atomic_read(&cli->cl_pending_w_pages));
350         seq_printf(seq, "pending read pages:   %d\n",
351                    atomic_read(&cli->cl_pending_r_pages));
352
353         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
354         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
355         seq_printf(seq, "       rpcs   %% cum %%\n");
356
357         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
358         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
359
360         read_cum = 0;
361         write_cum = 0;
362         for (i = 0; i < OBD_HIST_MAX; i++) {
363                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
364                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
365
366                 read_cum += r;
367                 write_cum += w;
368                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
369                            1 << i, r, pct(r, read_tot),
370                            pct(read_cum, read_tot), w,
371                            pct(w, write_tot),
372                            pct(write_cum, write_tot));
373                 if (read_cum == read_tot && write_cum == write_tot)
374                         break;
375         }
376
377         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
378         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
379         seq_printf(seq, "       rpcs   %% cum %%\n");
380
381         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
382         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
383
384         read_cum = 0;
385         write_cum = 0;
386         for (i = 0; i < OBD_HIST_MAX; i++) {
387                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
388                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
389
390                 read_cum += r;
391                 write_cum += w;
392                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
393                            i, r, pct(r, read_tot), pct(read_cum, read_tot), w,
394                            pct(w, write_tot), pct(write_cum, write_tot));
395                 if (read_cum == read_tot && write_cum == write_tot)
396                         break;
397         }
398
399         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
400         seq_printf(seq, "offset                rpcs   %% cum %% |");
401         seq_printf(seq, "       rpcs   %% cum %%\n");
402
403         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
404         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
405
406         read_cum = 0;
407         write_cum = 0;
408         for (i = 0; i < OBD_HIST_MAX; i++) {
409                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
410                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
411
412                 read_cum += r;
413                 write_cum += w;
414                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
415                            (i == 0) ? 0 : 1 << (i - 1),
416                            r, pct(r, read_tot), pct(read_cum, read_tot),
417                            w, pct(w, write_tot), pct(write_cum, write_tot));
418                 if (read_cum == read_tot && write_cum == write_tot)
419                         break;
420         }
421         spin_unlock(&cli->cl_loi_list_lock);
422
423         return 0;
424 }
425 LPROC_SEQ_FOPS(mdc_rpc_stats);
426
427 static int mdc_stats_seq_show(struct seq_file *seq, void *v)
428 {
429         struct timespec64 now;
430         struct obd_device *dev = seq->private;
431         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
432
433         ktime_get_real_ts64(&now);
434
435         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
436                    (s64)now.tv_sec, now.tv_nsec);
437         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
438                    stats->os_lockless_writes);
439         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
440                    stats->os_lockless_reads);
441         seq_printf(seq, "lockless_truncate\t\t%llu\n",
442                    stats->os_lockless_truncates);
443         return 0;
444 }
445
446 static ssize_t mdc_stats_seq_write(struct file *file,
447                                    const char __user *buf,
448                                    size_t len, loff_t *off)
449 {
450         struct seq_file *seq = file->private_data;
451         struct obd_device *dev = seq->private;
452         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
453
454         memset(stats, 0, sizeof(*stats));
455         return len;
456 }
457 LPROC_SEQ_FOPS(mdc_stats);
458
459 static int mdc_dom_min_repsize_seq_show(struct seq_file *m, void *v)
460 {
461         struct obd_device *dev = m->private;
462
463         seq_printf(m, "%u\n", dev->u.cli.cl_dom_min_inline_repsize);
464
465         return 0;
466 }
467
468 static ssize_t mdc_dom_min_repsize_seq_write(struct file *file,
469                                              const char __user *buffer,
470                                              size_t count, loff_t *off)
471 {
472         struct obd_device *dev;
473         unsigned int val;
474         int rc;
475
476         dev =  ((struct seq_file *)file->private_data)->private;
477         rc = kstrtouint_from_user(buffer, count, 0, &val);
478         if (rc)
479                 return rc;
480
481         if (val > MDC_DOM_MAX_INLINE_REPSIZE)
482                 return -ERANGE;
483
484         dev->u.cli.cl_dom_min_inline_repsize = val;
485         return count;
486 }
487 LPROC_SEQ_FOPS(mdc_dom_min_repsize);
488
489 LPROC_SEQ_FOPS_RO_TYPE(mdc, connect_flags);
490 LPROC_SEQ_FOPS_RO_TYPE(mdc, server_uuid);
491 LPROC_SEQ_FOPS_RO_TYPE(mdc, timeouts);
492 LPROC_SEQ_FOPS_RO_TYPE(mdc, state);
493 LPROC_SEQ_FOPS_RW_TYPE(mdc, obd_max_pages_per_rpc);
494 LPROC_SEQ_FOPS_RW_TYPE(mdc, import);
495 LPROC_SEQ_FOPS_RW_TYPE(mdc, pinger_recov);
496
497 struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
498         { .name =       "connect_flags",
499           .fops =       &mdc_connect_flags_fops },
500         { .name =       "mds_server_uuid",
501           .fops =       &mdc_server_uuid_fops   },
502         { .name =       "max_pages_per_rpc",
503           .fops =       &mdc_obd_max_pages_per_rpc_fops },
504         { .name =       "max_dirty_mb",
505           .fops =       &mdc_max_dirty_mb_fops          },
506         { .name =       "mdc_cached_mb",
507           .fops =       &mdc_cached_mb_fops             },
508         { .name =       "timeouts",
509           .fops =       &mdc_timeouts_fops              },
510         { .name =       "import",
511           .fops =       &mdc_import_fops                },
512         { .name =       "state",
513           .fops =       &mdc_state_fops                 },
514         { .name =       "pinger_recov",
515           .fops =       &mdc_pinger_recov_fops          },
516         { .name =       "rpc_stats",
517           .fops =       &mdc_rpc_stats_fops             },
518         { .name =       "unstable_stats",
519           .fops =       &mdc_unstable_stats_fops        },
520         { .name =       "mdc_stats",
521           .fops =       &mdc_stats_fops                 },
522         { .name =       "mdc_dom_min_repsize",
523           .fops =       &mdc_dom_min_repsize_fops       },
524         { NULL }
525 };
526
527 static struct attribute *mdc_attrs[] = {
528         &lustre_attr_active.attr,
529         &lustre_attr_max_rpcs_in_flight.attr,
530         &lustre_attr_max_mod_rpcs_in_flight.attr,
531         &lustre_attr_contention_seconds.attr,
532         &lustre_attr_mds_conn_uuid.attr,
533         &lustre_attr_conn_uuid.attr,
534         &lustre_attr_ping.attr,
535         NULL,
536 };
537
538 int mdc_tunables_init(struct obd_device *obd)
539 {
540         int rc;
541
542         obd->obd_ktype.default_attrs = mdc_attrs;
543         obd->obd_vars = lprocfs_mdc_obd_vars;
544
545         rc = lprocfs_obd_setup(obd, false);
546         if (rc)
547                 goto out_failed;
548 #ifdef CONFIG_PROC_FS
549         rc = lprocfs_alloc_md_stats(obd, 0);
550         if (rc) {
551                 lprocfs_obd_cleanup(obd);
552                 goto out_failed;
553         }
554 #endif
555         rc = sptlrpc_lprocfs_cliobd_attach(obd);
556         if (rc) {
557 #ifdef CONFIG_PROC_FS
558                 lprocfs_free_md_stats(obd);
559 #endif
560                 lprocfs_obd_cleanup(obd);
561                 goto out_failed;
562         }
563         ptlrpc_lprocfs_register_obd(obd);
564
565 out_failed:
566         return rc;
567 }