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