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