Whamcloud - gitweb
LU-8726 osd-ldiskfs: bypass read for benchmarking
[fs/lustre-release.git] / lustre / obdclass / llog_ioctl.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, 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
33 #define DEBUG_SUBSYSTEM S_LOG
34
35 #include <obd_class.h>
36 #include <lustre_ioctl.h>
37 #include <lustre_log.h>
38 #include "llog_internal.h"
39
40 static int str2logid(struct llog_logid *logid, char *str, int len)
41 {
42         char *start, *end, *endp;
43         __u64 id, seq;
44
45         ENTRY;
46         start = str;
47         if (*start != '#')
48                 RETURN(-EINVAL);
49
50         start++;
51         if (start - str >= len - 1)
52                 RETURN(-EINVAL);
53         end = strchr(start, '#');
54         if (end == NULL || end == start)
55                 RETURN(-EINVAL);
56
57         *end = '\0';
58         id = simple_strtoull(start, &endp, 0);
59         if (endp != end)
60                 RETURN(-EINVAL);
61
62         start = ++end;
63         if (start - str >= len - 1)
64                 RETURN(-EINVAL);
65         end = strchr(start, '#');
66         if (end == NULL || end == start)
67                 RETURN(-EINVAL);
68
69         *end = '\0';
70         seq = simple_strtoull(start, &endp, 0);
71         if (endp != end)
72                 RETURN(-EINVAL);
73
74         ostid_set_seq(&logid->lgl_oi, seq);
75         ostid_set_id(&logid->lgl_oi, id);
76
77         start = ++end;
78         if (start - str >= len - 1)
79                 RETURN(-EINVAL);
80         logid->lgl_ogen = simple_strtoul(start, &endp, 16);
81         if (*endp != '\0')
82                 RETURN(-EINVAL);
83
84         RETURN(0);
85 }
86
87 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
88                          struct llog_rec_hdr *rec, void *data)
89 {
90         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
91         static int l, remains;
92         static long from, to;
93         static char *out;
94         char *endp;
95         int cur_index, rc = 0;
96
97         ENTRY;
98
99         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
100                 l = 0;
101                 remains = ioc_data->ioc_inllen4 +
102                         cfs_size_round(ioc_data->ioc_inllen1) +
103                         cfs_size_round(ioc_data->ioc_inllen2) +
104                         cfs_size_round(ioc_data->ioc_inllen3);
105                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
106                 if (*endp != '\0')
107                         RETURN(-EINVAL);
108                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
109                 if (*endp != '\0')
110                         RETURN(-EINVAL);
111                 ioc_data->ioc_inllen1 = 0;
112                 out = ioc_data->ioc_bulk;
113         }
114
115         cur_index = rec->lrh_index;
116         if (cur_index < from)
117                 RETURN(0);
118         if (to > 0 && cur_index > to)
119                 RETURN(-LLOG_EEMPTY);
120
121         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
122                 struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
123                 struct llog_handle      *loghandle;
124
125                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
126                         l = snprintf(out, remains, "[index]: %05d  [type]: "
127                                      "%02x  [len]: %04d failed\n",
128                                      cur_index, rec->lrh_type,
129                                      rec->lrh_len);
130                 }
131                 if (handle->lgh_ctxt == NULL)
132                         RETURN(-EOPNOTSUPP);
133                 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
134                 if (rc) {
135                         CDEBUG(D_IOCTL, "cannot find log #"DOSTID"#%08x\n",
136                                POSTID(&lir->lid_id.lgl_oi),
137                                lir->lid_id.lgl_ogen);
138                         RETURN(rc);
139                 }
140                 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
141                 llog_handle_put(loghandle);
142         } else {
143                 bool ok;
144
145                 switch (rec->lrh_type) {
146                 case OST_SZ_REC:
147                 case MDS_UNLINK_REC:
148                 case MDS_UNLINK64_REC:
149                 case MDS_SETATTR64_REC:
150                 case OBD_CFG_REC:
151                 case LLOG_GEN_REC:
152                 case LLOG_HDR_MAGIC:
153                         ok = true;
154                         break;
155                 default:
156                         ok = false;
157                 }
158
159                 l = snprintf(out, remains, "[index]: %05d  [type]: "
160                              "%02x  [len]: %04d %s\n",
161                              cur_index, rec->lrh_type, rec->lrh_len,
162                              ok ? "ok" : "failed");
163                 out += l;
164                 remains -= l;
165                 if (remains <= 0) {
166                         CERROR("%s: no space to print log records\n",
167                                handle->lgh_ctxt->loc_obd->obd_name);
168                         RETURN(-LLOG_EEMPTY);
169                 }
170         }
171         RETURN(rc);
172 }
173
174 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
175                          struct llog_rec_hdr *rec, void *data)
176 {
177         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
178         static int l, remains;
179         static long from, to;
180         static char *out;
181         char *endp;
182         int cur_index;
183
184         ENTRY;
185         if (ioc_data != NULL && ioc_data->ioc_inllen1 > 0) {
186                 l = 0;
187                 remains = ioc_data->ioc_inllen4 +
188                         cfs_size_round(ioc_data->ioc_inllen1) +
189                         cfs_size_round(ioc_data->ioc_inllen2) +
190                         cfs_size_round(ioc_data->ioc_inllen3);
191                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
192                 if (*endp != '\0')
193                         RETURN(-EINVAL);
194                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
195                 if (*endp != '\0')
196                         RETURN(-EINVAL);
197                 out = ioc_data->ioc_bulk;
198                 ioc_data->ioc_inllen1 = 0;
199         }
200
201         cur_index = rec->lrh_index;
202         if (cur_index < from)
203                 RETURN(0);
204         if (to > 0 && cur_index > to)
205                 RETURN(-LLOG_EEMPTY);
206
207         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
208                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
209
210                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
211                         CERROR("invalid record in catalog\n");
212                         RETURN(-EINVAL);
213                 }
214
215                 l = snprintf(out, remains,
216                              "[index]: %05d  [logid]: #"DOSTID"#%08x\n",
217                              cur_index, POSTID(&lir->lid_id.lgl_oi),
218                              lir->lid_id.lgl_ogen);
219         } else if (rec->lrh_type == OBD_CFG_REC) {
220                 int rc;
221
222                 rc = class_config_yaml_output(rec, out, remains);
223                 if (rc < 0)
224                         RETURN(rc);
225                 l = rc;
226         } else {
227                 l = snprintf(out, remains,
228                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
229                              cur_index, rec->lrh_type, rec->lrh_len);
230         }
231         out += l;
232         remains -= l;
233         if (remains <= 0) {
234                 CERROR("not enough space for print log records\n");
235                 RETURN(-LLOG_EEMPTY);
236         }
237
238         RETURN(0);
239 }
240 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
241                            struct llog_logid *logid)
242 {
243         struct llog_handle      *log;
244         int                      rc;
245
246         ENTRY;
247
248         rc = llog_cat_id2handle(env, cat, &log, logid);
249         if (rc) {
250                 CDEBUG(D_IOCTL, "cannot find log #"DOSTID"#%08x\n",
251                        POSTID(&logid->lgl_oi), logid->lgl_ogen);
252                 RETURN(-ENOENT);
253         }
254
255         rc = llog_destroy(env, log);
256         if (rc) {
257                 CDEBUG(D_IOCTL, "cannot destroy log\n");
258                 GOTO(out, rc);
259         }
260         llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
261 out:
262         llog_handle_put(log);
263         RETURN(rc);
264
265 }
266
267 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
268                           struct llog_rec_hdr *rec, void *data)
269 {
270         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
271         int                      rc;
272
273         ENTRY;
274         if (rec->lrh_type != LLOG_LOGID_MAGIC)
275                 RETURN(-EINVAL);
276         rc = llog_remove_log(env, handle, &lir->lid_id);
277
278         RETURN(rc);
279 }
280
281
282 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
283                struct obd_ioctl_data *data)
284 {
285         struct llog_logid        logid;
286         int                      rc = 0;
287         struct llog_handle      *handle = NULL;
288
289         ENTRY;
290
291         if (*data->ioc_inlbuf1 == '#') {
292                 rc = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
293                 if (rc)
294                         RETURN(rc);
295                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
296                                LLOG_OPEN_EXISTS);
297                 if (rc)
298                         RETURN(rc);
299         } else if (*data->ioc_inlbuf1 == '$') {
300                 char *name = data->ioc_inlbuf1 + 1;
301
302                 rc = llog_open(env, ctxt, &handle, NULL, name,
303                                LLOG_OPEN_EXISTS);
304                 if (rc)
305                         RETURN(rc);
306         } else {
307                 RETURN(-EINVAL);
308         }
309
310         rc = llog_init_handle(env, handle, 0, NULL);
311         if (rc)
312                 GOTO(out_close, rc = -ENOENT);
313
314         switch (cmd) {
315         case OBD_IOC_LLOG_INFO: {
316                 int      l;
317                 int      remains = data->ioc_inllen2 +
318                                    cfs_size_round(data->ioc_inllen1);
319                 char    *out = data->ioc_bulk;
320
321                 l = snprintf(out, remains,
322                              "logid:            #"DOSTID"#%08x\n"
323                              "flags:            %x (%s)\n"
324                              "records count:    %d\n"
325                              "last index:       %d\n",
326                              POSTID(&handle->lgh_id.lgl_oi),
327                              handle->lgh_id.lgl_ogen,
328                              handle->lgh_hdr->llh_flags,
329                              handle->lgh_hdr->llh_flags &
330                              LLOG_F_IS_CAT ? "cat" : "plain",
331                              handle->lgh_hdr->llh_count,
332                              handle->lgh_last_idx);
333                 out += l;
334                 remains -= l;
335                 if (remains <= 0) {
336                         CERROR("%s: not enough space for log header info\n",
337                                ctxt->loc_obd->obd_name);
338                         rc = -ENOSPC;
339                 }
340                 break;
341         }
342         case OBD_IOC_LLOG_CHECK:
343                 LASSERT(data->ioc_inllen1 > 0);
344                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
345                 if (rc == -LLOG_EEMPTY)
346                         rc = 0;
347                 else if (rc)
348                         GOTO(out_close, rc);
349                 break;
350         case OBD_IOC_LLOG_PRINT:
351                 LASSERT(data->ioc_inllen1 > 0);
352                 rc = llog_process(env, handle, llog_print_cb, data, NULL);
353                 if (rc == -LLOG_EEMPTY)
354                         rc = 0;
355                 else if (rc)
356                         GOTO(out_close, rc);
357                 break;
358         case OBD_IOC_LLOG_CANCEL: {
359                 struct llog_cookie cookie;
360                 struct llog_logid plain;
361                 char *endp;
362
363                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
364                 if (*endp != '\0')
365                         GOTO(out_close, rc = -EINVAL);
366
367                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
368                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
369                         GOTO(out_close, rc);
370                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
371                         GOTO(out_close, rc = -EINVAL);
372                 }
373
374                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
375                         GOTO(out_close, rc = -ENOTTY);
376
377                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
378                 if (rc)
379                         GOTO(out_close, rc);
380                 cookie.lgc_lgl = plain;
381                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
382                 if (rc)
383                         GOTO(out_close, rc);
384                 break;
385         }
386         case OBD_IOC_LLOG_REMOVE: {
387                 struct llog_logid plain;
388
389                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
390                         rc = llog_destroy(env, handle);
391                         GOTO(out_close, rc);
392                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
393                         GOTO(out_close, rc = -EINVAL);
394                 }
395
396                 if (data->ioc_inlbuf2 > 0) {
397                         /* remove indicate log from the catalog */
398                         rc = str2logid(&plain, data->ioc_inlbuf2,
399                                        data->ioc_inllen2);
400                         if (rc)
401                                 GOTO(out_close, rc);
402                         rc = llog_remove_log(env, handle, &plain);
403                 } else {
404                         /* remove all the log of the catalog */
405                         rc = llog_process(env, handle, llog_delete_cb, NULL,
406                                           NULL);
407                         if (rc)
408                                 GOTO(out_close, rc);
409                 }
410                 break;
411         }
412         default:
413                 CERROR("%s: Unknown ioctl cmd %#x\n",
414                        ctxt->loc_obd->obd_name, cmd);
415                 GOTO(out_close, rc = -ENOTTY);
416         }
417
418 out_close:
419         if (handle->lgh_hdr &&
420             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
421                 llog_cat_close(env, handle);
422         else
423                 llog_close(env, handle);
424         RETURN(rc);
425 }
426 EXPORT_SYMBOL(llog_ioctl);
427
428 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
429                       int count, struct obd_ioctl_data *data,
430                       const struct lu_fid *fid)
431 {
432         int                      size, i;
433         struct llog_catid       *idarray;
434         struct llog_logid       *id;
435         char                    *out;
436         int                      l, remains, rc = 0;
437
438         ENTRY;
439
440         if (count == 0) { /* get total number of logs */
441                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
442                 if (rc < 0)
443                         RETURN(rc);
444                 count = rc;
445         }
446
447         size = sizeof(*idarray) * count;
448
449         OBD_ALLOC_LARGE(idarray, size);
450         if (!idarray)
451                 RETURN(-ENOMEM);
452
453         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
454         if (rc)
455                 GOTO(out, rc);
456
457         out = data->ioc_bulk;
458         remains = data->ioc_inllen1;
459         for (i = 0; i < count; i++) {
460                 id = &idarray[i].lci_logid;
461                 l = snprintf(out, remains,
462                              "catalog log: #"DOSTID"#%08x\n",
463                              POSTID(&id->lgl_oi),
464                              id->lgl_ogen);
465                 out += l;
466                 remains -= l;
467                 if (remains <= 0)
468                         break;
469         }
470 out:
471         OBD_FREE_LARGE(idarray, size);
472         RETURN(rc);
473 }
474 EXPORT_SYMBOL(llog_catalog_list);