Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / fid / lproc_fid.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) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/fid/lproc_fid.c
32  *
33  * Lustre Sequence Manager
34  *
35  * Author: Yury Umanets <umka@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_FID
39
40 #include <libcfs/libcfs.h>
41 #include <linux/module.h>
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <lprocfs_status.h>
47 #include "fid_internal.h"
48
49 /* Format: [0x64BIT_INT - 0x64BIT_INT] + 32 bytes just in case */
50 #define MAX_FID_RANGE_STRLEN (32 + 2 * 2 * sizeof(__u64))
51 /**
52  * Reduce the SEQ range allocated to a node to a strict subset of the range
53  * currently-allocated SEQ range.  If the specified range is "clear", then
54  * drop all allocated sequences and request a new one from the master.
55  *
56  * Note: this function should only be used for testing, it is not necessarily
57  * safe for production use.
58  */
59 static int
60 ldebugfs_fid_write_common(const char __user *buffer, size_t count,
61                           struct lu_seq_range *range)
62 {
63         char kernbuf[MAX_FID_RANGE_STRLEN];
64         struct lu_seq_range tmp = {
65                 .lsr_start = 0,
66         };
67         int rc;
68
69         ENTRY;
70         LASSERT(range);
71
72         if (count >= sizeof(kernbuf))
73                 RETURN(-EINVAL);
74
75         if (copy_from_user(kernbuf, buffer, count))
76                 RETURN(-EFAULT);
77
78         kernbuf[count] = 0;
79
80         if (count == 5 && strcmp(kernbuf, "clear") == 0) {
81                 memset(range, 0, sizeof(*range));
82                 RETURN(count);
83         }
84
85         /* of the form "[0x0000000240000400 - 0x000000028000400]" */
86         rc = sscanf(kernbuf, "[%llx - %llx]\n",
87                     (unsigned long long *)&tmp.lsr_start,
88                     (unsigned long long *)&tmp.lsr_end);
89         if (rc != 2)
90                 RETURN(-EINVAL);
91         if (!lu_seq_range_is_sane(&tmp) || lu_seq_range_is_zero(&tmp) ||
92             tmp.lsr_start < range->lsr_start || tmp.lsr_end > range->lsr_end)
93                 RETURN(-EINVAL);
94         *range = tmp;
95         RETURN(0);
96 }
97
98 #ifdef HAVE_SERVER_SUPPORT
99 /*
100  * Server side debugfs stuff.
101  */
102 static ssize_t
103 ldebugfs_server_fid_space_seq_write(struct file *file,
104                                     const char __user *buffer,
105                                     size_t count, loff_t *off)
106 {
107         struct seq_file *m = file->private_data;
108         struct lu_server_seq *seq = m->private;
109         int rc;
110
111         ENTRY;
112
113         mutex_lock(&seq->lss_mutex);
114         rc = ldebugfs_fid_write_common(buffer, count, &seq->lss_space);
115         if (rc == 0) {
116                 CDEBUG(D_INFO, "%s: Space: " DRANGE "\n",
117                        seq->lss_name, PRANGE(&seq->lss_space));
118         }
119         mutex_unlock(&seq->lss_mutex);
120
121         RETURN(count);
122 }
123
124 static int
125 ldebugfs_server_fid_space_seq_show(struct seq_file *m, void *unused)
126 {
127         struct lu_server_seq *seq = (struct lu_server_seq *)m->private;
128         ENTRY;
129
130         mutex_lock(&seq->lss_mutex);
131         seq_printf(m, "[%#llx - %#llx]:%x:%s\n", PRANGE(&seq->lss_space));
132         mutex_unlock(&seq->lss_mutex);
133
134         RETURN(0);
135 }
136
137 static int
138 ldebugfs_server_fid_server_seq_show(struct seq_file *m, void *unused)
139 {
140         struct lu_server_seq *seq = (struct lu_server_seq *)m->private;
141         struct client_obd *cli;
142         ENTRY;
143
144         if (seq->lss_cli) {
145                 if (seq->lss_cli->lcs_exp != NULL) {
146                         cli = &seq->lss_cli->lcs_exp->exp_obd->u.cli;
147                         seq_printf(m, "%s\n", cli->cl_target_uuid.uuid);
148                 } else {
149                         seq_printf(m, "%s\n", seq->lss_cli->lcs_srv->lss_name);
150                 }
151         } else {
152                 seq_puts(m, "<none>\n");
153         }
154
155         RETURN(0);
156 }
157
158 static ssize_t ldebugfs_server_fid_width_seq_write(struct file *file,
159                                                    const char __user *buffer,
160                                                    size_t count, loff_t *off)
161 {
162         struct seq_file *m = file->private_data;
163         struct lu_server_seq *seq = m->private;
164         int rc;
165
166         ENTRY;
167         mutex_lock(&seq->lss_mutex);
168
169         rc = kstrtoull_from_user(buffer, count, 0, &seq->lss_width);
170         if (rc) {
171                 CERROR("%s: invalid FID sequence width: rc = %d\n",
172                        seq->lss_name, rc);
173                 GOTO(out_unlock, count = rc);
174         }
175
176         CDEBUG(D_INFO, "%s: Width: %llu\n",
177                seq->lss_name, seq->lss_width);
178 out_unlock:
179         mutex_unlock(&seq->lss_mutex);
180
181         RETURN(count);
182 }
183
184 static int
185 ldebugfs_server_fid_width_seq_show(struct seq_file *m, void *unused)
186 {
187         struct lu_server_seq *seq = (struct lu_server_seq *)m->private;
188
189         ENTRY;
190         mutex_lock(&seq->lss_mutex);
191         seq_printf(m, "%llu\n", seq->lss_width);
192         mutex_unlock(&seq->lss_mutex);
193
194         RETURN(0);
195 }
196
197 LDEBUGFS_SEQ_FOPS(ldebugfs_server_fid_space);
198 LDEBUGFS_SEQ_FOPS(ldebugfs_server_fid_width);
199 LDEBUGFS_SEQ_FOPS_RO(ldebugfs_server_fid_server);
200
201 struct ldebugfs_vars seq_server_debugfs_list[] = {
202         { .name =       "space",
203           .fops =       &ldebugfs_server_fid_space_fops },
204         { .name =       "width",
205           .fops =       &ldebugfs_server_fid_width_fops },
206         { .name =       "server",
207           .fops =       &ldebugfs_server_fid_server_fops},
208         { NULL }
209 };
210
211 struct fld_seq_param {
212         struct lu_env           fsp_env;
213         struct dt_it            *fsp_it;
214         struct lu_server_fld    *fsp_fld;
215         struct lu_server_seq    *fsp_seq;
216         unsigned int            fsp_stop:1;
217 };
218
219 /*
220  * XXX: below is a copy of the functions in lustre/fld/lproc_fld.c.
221  * we want to avoid this duplication either by exporting the
222  * functions or merging fid and fld into a single module.
223  */
224 static void *fldb_seq_start(struct seq_file *p, loff_t *pos)
225 {
226         struct fld_seq_param    *param = p->private;
227         struct lu_server_fld    *fld;
228         struct dt_object        *obj;
229         const struct dt_it_ops  *iops;
230         struct dt_key           *key;
231         int                     rc;
232
233         if (param == NULL || param->fsp_stop)
234                 return NULL;
235
236         fld = param->fsp_fld;
237         obj = fld->lsf_obj;
238         LASSERT(obj != NULL);
239         iops = &obj->do_index_ops->dio_it;
240
241         rc = iops->load(&param->fsp_env, param->fsp_it, *pos);
242         if (rc <= 0)
243                 return NULL;
244
245         key = iops->key(&param->fsp_env, param->fsp_it);
246         if (IS_ERR(key))
247                 return NULL;
248
249         *pos = be64_to_cpu(*(__u64 *)key);
250
251         return param;
252 }
253
254 static void fldb_seq_stop(struct seq_file *p, void *v)
255 {
256         struct fld_seq_param    *param = p->private;
257         const struct dt_it_ops  *iops;
258         struct lu_server_fld    *fld;
259         struct dt_object        *obj;
260
261         if (param == NULL)
262                 return;
263
264         fld = param->fsp_fld;
265         obj = fld->lsf_obj;
266         LASSERT(obj != NULL);
267         iops = &obj->do_index_ops->dio_it;
268
269         iops->put(&param->fsp_env, param->fsp_it);
270 }
271
272 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
273 {
274         struct fld_seq_param    *param = p->private;
275         struct lu_server_fld    *fld;
276         struct dt_object        *obj;
277         const struct dt_it_ops  *iops;
278         int                     rc;
279
280         ++*pos;
281         if (param == NULL || param->fsp_stop)
282                 return NULL;
283
284         fld = param->fsp_fld;
285         obj = fld->lsf_obj;
286         LASSERT(obj != NULL);
287         iops = &obj->do_index_ops->dio_it;
288
289         rc = iops->next(&param->fsp_env, param->fsp_it);
290         if (rc > 0) {
291                 param->fsp_stop = 1;
292                 return NULL;
293         }
294
295         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
296         return param;
297 }
298
299 static int fldb_seq_show(struct seq_file *p, void *v)
300 {
301         struct fld_seq_param    *param = p->private;
302         struct lu_server_fld    *fld;
303         struct dt_object        *obj;
304         const struct dt_it_ops  *iops;
305         struct lu_seq_range      fld_rec;
306         int                     rc;
307
308         if (param == NULL || param->fsp_stop)
309                 return 0;
310
311         fld = param->fsp_fld;
312         obj = fld->lsf_obj;
313         LASSERT(obj != NULL);
314         iops = &obj->do_index_ops->dio_it;
315
316         rc = iops->rec(&param->fsp_env, param->fsp_it,
317                        (struct dt_rec *)&fld_rec, 0);
318         if (rc != 0) {
319                 CERROR("%s: read record error: rc = %d\n",
320                        fld->lsf_name, rc);
321         } else if (fld_rec.lsr_start != 0) {
322                 range_be_to_cpu(&fld_rec, &fld_rec);
323                 seq_printf(p, DRANGE"\n", PRANGE(&fld_rec));
324         }
325
326         return rc;
327 }
328
329 static const struct seq_operations fldb_sops = {
330         .start = fldb_seq_start,
331         .stop = fldb_seq_stop,
332         .next = fldb_seq_next,
333         .show = fldb_seq_show,
334 };
335
336 static int fldb_seq_open(struct inode *inode, struct file *file)
337 {
338         struct seq_file         *seq;
339         struct lu_server_seq *ss = inode->i_private;
340         struct lu_server_fld    *fld;
341         struct dt_object        *obj;
342         const struct dt_it_ops  *iops;
343         struct fld_seq_param    *param = NULL;
344         int                     env_init = 0;
345         int                     rc;
346
347         fld = ss->lss_site->ss_server_fld;
348         LASSERT(fld != NULL);
349
350         rc = seq_open(file, &fldb_sops);
351         if (rc)
352                 return rc;
353
354         obj = fld->lsf_obj;
355         if (obj == NULL) {
356                 seq = file->private_data;
357                 seq->private = NULL;
358                 return 0;
359         }
360
361         OBD_ALLOC_PTR(param);
362         if (param == NULL)
363                 GOTO(out, rc = -ENOMEM);
364
365         rc = lu_env_init(&param->fsp_env, LCT_MD_THREAD);
366         if (rc != 0)
367                 GOTO(out, rc);
368
369         env_init = 1;
370         iops = &obj->do_index_ops->dio_it;
371         param->fsp_it = iops->init(&param->fsp_env, obj, 0);
372         if (IS_ERR(param->fsp_it))
373                 GOTO(out, rc = PTR_ERR(param->fsp_it));
374
375         param->fsp_fld = fld;
376         param->fsp_seq = ss;
377         param->fsp_stop = 0;
378
379         seq = file->private_data;
380         seq->private = param;
381 out:
382         if (rc != 0) {
383                 if (env_init == 1)
384                         lu_env_fini(&param->fsp_env);
385                 if (param != NULL)
386                         OBD_FREE_PTR(param);
387         }
388         return rc;
389 }
390
391 static int fldb_seq_release(struct inode *inode, struct file *file)
392 {
393         struct seq_file         *seq = file->private_data;
394         struct fld_seq_param    *param;
395         struct lu_server_fld    *fld;
396         struct dt_object        *obj;
397         const struct dt_it_ops  *iops;
398
399         param = seq->private;
400         if (param == NULL) {
401                 seq_release(inode, file);
402                 return 0;
403         }
404
405         fld = param->fsp_fld;
406         obj = fld->lsf_obj;
407         LASSERT(obj != NULL);
408         iops = &obj->do_index_ops->dio_it;
409
410         LASSERT(iops != NULL);
411         LASSERT(param->fsp_it != NULL);
412         iops->fini(&param->fsp_env, param->fsp_it);
413         lu_env_fini(&param->fsp_env);
414         OBD_FREE_PTR(param);
415         seq_release(inode, file);
416
417         return 0;
418 }
419
420 static ssize_t fldb_seq_write(struct file *file, const char __user *buf,
421                               size_t len, loff_t *off)
422 {
423         struct seq_file         *seq = file->private_data;
424         struct fld_seq_param    *param;
425         struct lu_seq_range      range;
426         int                      rc = 0;
427         char                     _buffer[MAX_FID_RANGE_STRLEN];
428         char                    *buffer = _buffer;
429         char *tmp;
430         ENTRY;
431
432         param = seq->private;
433         if (param == NULL)
434                 RETURN(-EINVAL);
435
436         if (len >= sizeof(_buffer))
437                 RETURN(-EINVAL);
438
439         if (copy_from_user(buffer, buf, len))
440                 GOTO(out, rc = -EFAULT);
441         buffer[len] = 0;
442
443         /*
444          * format - [0x0000000200000007-0x0000000200000008):0:mdt
445          */
446         if (*buffer != '[')
447                 GOTO(out, rc = -EINVAL);
448         buffer++;
449
450         tmp = strchr(buffer, '-');
451         if (!tmp)
452                 GOTO(out, rc = -EINVAL);
453         *tmp++ = '\0';
454         rc = kstrtoull(buffer, 0, &range.lsr_start);
455         if (rc)
456                 GOTO(out, rc);
457         buffer = tmp;
458
459         tmp = strchr(buffer, ')');
460         if (!tmp)
461                 GOTO(out, rc = -EINVAL);
462         *tmp++ = '\0';
463         rc = kstrtoull(buffer, 0, &range.lsr_end);
464         if (rc)
465                 GOTO(out, rc);
466         buffer = tmp;
467
468         if (*buffer != ':')
469                 GOTO(out, rc = -EINVAL);
470         buffer++;
471
472         tmp = strchr(buffer, ':');
473         if (!tmp)
474                 GOTO(out, rc = -EINVAL);
475         *tmp++ = '\0';
476         rc = kstrtouint(buffer, 0, &range.lsr_index);
477         if (rc)
478                 GOTO(out, rc);
479         buffer = tmp;
480
481         if (strncmp(buffer, "mdt", 3) == 0)
482                 range.lsr_flags = LU_SEQ_RANGE_MDT;
483         else if (strncmp(buffer, "ost", 3) == 0)
484                 range.lsr_flags = LU_SEQ_RANGE_OST;
485         else
486                 GOTO(out, rc = -EINVAL);
487
488         rc = seq_server_alloc_spec(param->fsp_seq->lss_site->ss_control_seq,
489                                    &range, &param->fsp_env);
490
491 out:
492         RETURN(rc < 0 ? rc : len);
493 }
494
495 const struct file_operations seq_fld_debugfs_seq_fops = {
496         .owner   = THIS_MODULE,
497         .open    = fldb_seq_open,
498         .read    = seq_read,
499         .write   = fldb_seq_write,
500         .release = fldb_seq_release,
501 };
502
503 #endif /* HAVE_SERVER_SUPPORT */
504
505 /* Client side debugfs stuff */
506 static ssize_t
507 ldebugfs_client_fid_space_seq_write(struct file *file,
508                                     const char __user *buffer,
509                                     size_t count, loff_t *off)
510 {
511         struct seq_file *m = file->private_data;
512         struct lu_client_seq *seq = m->private;
513         int rc;
514
515         ENTRY;
516
517         mutex_lock(&seq->lcs_mutex);
518         rc = ldebugfs_fid_write_common(buffer, count, &seq->lcs_space);
519         if (rc == 0) {
520                 CDEBUG(D_INFO, "%s: Space: " DRANGE "\n",
521                        seq->lcs_name, PRANGE(&seq->lcs_space));
522         }
523
524         mutex_unlock(&seq->lcs_mutex);
525
526         RETURN(count);
527 }
528
529 static int ldebugfs_client_fid_space_seq_show(struct seq_file *m, void *unused)
530 {
531         struct lu_client_seq *seq = (struct lu_client_seq *)m->private;
532
533         ENTRY;
534         mutex_lock(&seq->lcs_mutex);
535         seq_printf(m, "[%#llx - %#llx]:%x:%s\n", PRANGE(&seq->lcs_space));
536         mutex_unlock(&seq->lcs_mutex);
537
538         RETURN(0);
539 }
540
541 static ssize_t ldebugfs_client_fid_width_seq_write(struct file *file,
542                                                    const char __user *buffer,
543                                                    size_t count, loff_t *off)
544 {
545         struct seq_file *m = file->private_data;
546         struct lu_client_seq *seq = m->private;
547         u64 val;
548         u64 max;
549         int rc;
550
551         ENTRY;
552         rc = kstrtoull_from_user(buffer, count, 0, &val);
553         if (rc)
554                 return rc;
555
556         mutex_lock(&seq->lcs_mutex);
557         if (seq->lcs_type == LUSTRE_SEQ_DATA)
558                 max = LUSTRE_DATA_SEQ_MAX_WIDTH;
559         else
560                 max = LUSTRE_METADATA_SEQ_MAX_WIDTH;
561
562         if (val <= max) {
563                 seq->lcs_width = val;
564
565                 CDEBUG(D_INFO, "%s: Sequence size: %llu\n", seq->lcs_name,
566                        seq->lcs_width);
567         } else {
568                 count = -ERANGE;
569         }
570
571         mutex_unlock(&seq->lcs_mutex);
572         RETURN(count);
573 }
574
575 static int
576 ldebugfs_client_fid_width_seq_show(struct seq_file *m, void *unused)
577 {
578         struct lu_client_seq *seq = (struct lu_client_seq *)m->private;
579
580         ENTRY;
581         mutex_lock(&seq->lcs_mutex);
582         seq_printf(m, "%llu\n", seq->lcs_width);
583         mutex_unlock(&seq->lcs_mutex);
584
585         RETURN(0);
586 }
587
588 static int
589 ldebugfs_client_fid_fid_seq_show(struct seq_file *m, void *unused)
590 {
591         struct lu_client_seq *seq = (struct lu_client_seq *)m->private;
592
593         ENTRY;
594         mutex_lock(&seq->lcs_mutex);
595         seq_printf(m, DFID"\n", PFID(&seq->lcs_fid));
596         mutex_unlock(&seq->lcs_mutex);
597
598         RETURN(0);
599 }
600
601 static int
602 ldebugfs_client_fid_server_seq_show(struct seq_file *m, void *unused)
603 {
604         struct lu_client_seq *seq = (struct lu_client_seq *)m->private;
605         struct client_obd *cli;
606         ENTRY;
607
608         if (seq->lcs_exp) {
609                 cli = &seq->lcs_exp->exp_obd->u.cli;
610                 seq_printf(m, "%s\n", cli->cl_target_uuid.uuid);
611 #ifdef HAVE_SERVER_SUPPORT
612         } else {
613                 seq_printf(m, "%s\n", seq->lcs_srv->lss_name);
614 #endif /* HAVE_SERVER_SUPPORT */
615         }
616
617         RETURN(0);
618 }
619
620 LDEBUGFS_SEQ_FOPS(ldebugfs_client_fid_space);
621 LDEBUGFS_SEQ_FOPS(ldebugfs_client_fid_width);
622 LDEBUGFS_SEQ_FOPS_RO(ldebugfs_client_fid_server);
623 LDEBUGFS_SEQ_FOPS_RO(ldebugfs_client_fid_fid);
624
625 struct ldebugfs_vars seq_client_debugfs_list[] = {
626         { .name =       "space",
627           .fops =       &ldebugfs_client_fid_space_fops },
628         { .name =       "width",
629           .fops =       &ldebugfs_client_fid_width_fops },
630         { .name =       "server",
631           .fops =       &ldebugfs_client_fid_server_fops},
632         { .name =       "fid",
633           .fops =       &ldebugfs_client_fid_fid_fops   },
634         { NULL }
635 };