Whamcloud - gitweb
LU-8066 obdclass: Remove lprocfs_obd_short_io_bytes_* declarations
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LOG
33
34 #include <obd_class.h>
35 #include <uapi/linux/lustre/lustre_ioctl.h>
36 #include <lustre_log.h>
37 #include "llog_internal.h"
38
39 static int str2logid(struct llog_logid *logid, char *str, int len)
40 {
41         unsigned long long id, seq;
42         char *start, *end;
43         u32 ogen;
44         int rc;
45
46         ENTRY;
47         start = str;
48         if (start[0] == '[') {
49                 struct lu_fid *fid = &logid->lgl_oi.oi_fid;
50                 struct lu_fid sfid;
51                 int num;
52
53                 fid_zero(fid);
54                 logid->lgl_ogen = 0;
55                 num = sscanf(start + 1, SFID, RFID(fid));
56                 CDEBUG(D_INFO, DFID":%x\n", PFID(fid), logid->lgl_ogen);
57                 logid_to_fid(logid, &sfid);
58                 RETURN(num == 3 && fid_is_sane(&sfid) ? 0 : -EINVAL);
59         }
60
61 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
62         /*
63          * logids used to be input in the form "#id#seq:ogen" before they
64          * were changed over to accept the FID [seq:oid:ver] format.
65          * This is accepted for compatibility reasons, though I doubt
66          * anyone is actually using this for anything.
67          */
68         if (start[0] != '#')
69                 RETURN(-EINVAL);
70
71         start++;
72         if (start - str >= len - 1)
73                 RETURN(-EINVAL);
74         end = strchr(start, '#');
75         if (end == NULL || end == start)
76                 RETURN(-EINVAL);
77
78         *end = '\0';
79         rc = kstrtoull(start, 0, &id);
80         if (rc)
81                 RETURN(rc);
82
83         start = ++end;
84         if (start - str >= len - 1)
85                 RETURN(-EINVAL);
86
87         end = strchr(start, '#');
88         if (!end || end == start)
89                 RETURN(-EINVAL);
90
91         *end = '\0';
92         rc = kstrtoull(start, 0, &seq);
93         if (rc)
94                 RETURN(rc);
95
96         ostid_set_seq(&logid->lgl_oi, seq);
97         if (ostid_set_id(&logid->lgl_oi, id))
98                 RETURN(-EINVAL);
99
100         start = ++end;
101         if (start - str >= len - 1)
102                 RETURN(-EINVAL);
103
104         rc = kstrtouint(start, 16, &ogen);
105         if (rc)
106                 RETURN(-EINVAL);
107         logid->lgl_ogen = ogen;
108
109         RETURN(0);
110 #else
111         RETURN(-EINVAL);
112 #endif
113 }
114
115 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
116                          struct llog_rec_hdr *rec, void *data)
117 {
118         struct obd_ioctl_data *ioc_data = data;
119         static int l, remains;
120         static long from, to;
121         static char *out;
122         int cur_index;
123         int rc = 0;
124
125         ENTRY;
126         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
127                 l = 0;
128                 remains = ioc_data->ioc_inllen4 +
129                           round_up(ioc_data->ioc_inllen1, 8) +
130                           round_up(ioc_data->ioc_inllen2, 8) +
131                           round_up(ioc_data->ioc_inllen3, 8);
132
133                 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
134                 if (rc)
135                         RETURN(rc);
136
137                 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
138                 if (rc)
139                         RETURN(rc);
140
141                 ioc_data->ioc_inllen1 = 0;
142                 out = ioc_data->ioc_bulk;
143         }
144
145         cur_index = rec->lrh_index;
146         if (cur_index < from)
147                 RETURN(0);
148         if (to > 0 && cur_index > to)
149                 RETURN(-LLOG_EEMPTY);
150
151         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
152                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
153                 struct llog_handle *loghandle;
154
155                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
156                         l = snprintf(out, remains,
157                                      "[index]: %05d  [type]: %02x  [len]: %04d failed\n",
158                                      cur_index, rec->lrh_type,
159                                      rec->lrh_len);
160                 }
161                 if (handle->lgh_ctxt == NULL)
162                         RETURN(-EOPNOTSUPP);
163                 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
164                 if (rc) {
165                         CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
166                                PFID(&lir->lid_id.lgl_oi.oi_fid),
167                                lir->lid_id.lgl_ogen);
168                         RETURN(rc);
169                 }
170                 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
171                 llog_handle_put(env, loghandle);
172         } else {
173                 bool ok;
174
175                 switch (rec->lrh_type) {
176                 case OST_SZ_REC:
177                 case MDS_UNLINK_REC:
178                 case MDS_UNLINK64_REC:
179                 case MDS_SETATTR64_REC:
180                 case OBD_CFG_REC:
181                 case LLOG_GEN_REC:
182                 case LLOG_HDR_MAGIC:
183                         ok = true;
184                         break;
185                 default:
186                         ok = false;
187                 }
188
189                 l = snprintf(out, remains, "[index]: %05d  [type]: "
190                              "%02x  [len]: %04d %s\n",
191                              cur_index, rec->lrh_type, rec->lrh_len,
192                              ok ? "ok" : "failed");
193                 out += l;
194                 remains -= l;
195                 if (remains <= 0) {
196                         CERROR("%s: no space to print log records\n",
197                                handle->lgh_ctxt->loc_obd->obd_name);
198                         RETURN(-LLOG_EEMPTY);
199                 }
200         }
201         RETURN(rc);
202 }
203
204 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
205                          struct llog_rec_hdr *rec, void *data)
206 {
207         struct obd_ioctl_data *ioc_data = data;
208         static int l, remains;
209         static long from, to;
210         static char *out;
211         int cur_index;
212         int rc;
213
214         ENTRY;
215         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
216                 l = 0;
217                 remains = ioc_data->ioc_inllen4 +
218                           round_up(ioc_data->ioc_inllen1, 8) +
219                           round_up(ioc_data->ioc_inllen2, 8) +
220                           round_up(ioc_data->ioc_inllen3, 8);
221
222                 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
223                 if (rc)
224                         RETURN(rc);
225
226                 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
227                 if (rc)
228                         RETURN(rc);
229
230                 out = ioc_data->ioc_bulk;
231                 ioc_data->ioc_inllen1 = 0;
232         }
233
234         cur_index = rec->lrh_index;
235         if (cur_index < from)
236                 RETURN(0);
237         if (to > 0 && cur_index > to)
238                 RETURN(-LLOG_EEMPTY);
239
240         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
241                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
242
243                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
244                         CERROR("invalid record in catalog\n");
245                         RETURN(-EINVAL);
246                 }
247
248                 l = snprintf(out, remains,
249                              "[index]: %05d  [logid]: "DFID":%x\n",
250                              cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
251                              lir->lid_id.lgl_ogen);
252         } else if (rec->lrh_type == OBD_CFG_REC) {
253                 int rc;
254
255                 rc = class_config_yaml_output(rec, out, remains);
256                 if (rc < 0)
257                         RETURN(rc);
258                 l = rc;
259         } else {
260                 l = snprintf(out, remains,
261                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
262                              cur_index, rec->lrh_type, rec->lrh_len);
263         }
264         out += l;
265         remains -= l;
266         if (remains <= 0) {
267                 CERROR("not enough space for print log records\n");
268                 RETURN(-LLOG_EEMPTY);
269         }
270
271         RETURN(0);
272 }
273 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
274                            struct llog_logid *logid)
275 {
276         struct llog_handle *log;
277         int rc;
278
279         ENTRY;
280
281         rc = llog_cat_id2handle(env, cat, &log, logid);
282         if (rc) {
283                 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
284                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
285                 RETURN(-ENOENT);
286         }
287
288         rc = llog_destroy(env, log);
289         if (rc) {
290                 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
291                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
292                 GOTO(out, rc);
293         }
294         llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
295 out:
296         llog_handle_put(env, log);
297         RETURN(rc);
298
299 }
300
301 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
302                           struct llog_rec_hdr *rec, void *data)
303 {
304         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
305         int rc;
306
307         ENTRY;
308         if (rec->lrh_type != LLOG_LOGID_MAGIC)
309                 RETURN(-EINVAL);
310         rc = llog_remove_log(env, handle, &lir->lid_id);
311
312         RETURN(rc);
313 }
314
315
316 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
317                struct obd_ioctl_data *data)
318 {
319         struct llog_logid logid;
320         int rc = 0;
321         struct llog_handle *handle = NULL;
322         char *logname, start;
323
324         ENTRY;
325
326         logname = data->ioc_inlbuf1;
327         start = logname[0];
328         if (start == '#' || start == '[') {
329                 rc = str2logid(&logid, logname, data->ioc_inllen1);
330                 if (rc)
331                         RETURN(rc);
332                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
333                                LLOG_OPEN_EXISTS);
334                 if (rc)
335                         RETURN(rc);
336         } else if (start == '$' || isalpha(start) || isdigit(start)) {
337                 if (start == '$')
338                         logname++;
339
340                 rc = llog_open(env, ctxt, &handle, NULL, logname,
341                                LLOG_OPEN_EXISTS);
342                 if (rc)
343                         RETURN(rc);
344         } else {
345                 rc = -EINVAL;
346                 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
347                       ctxt->loc_obd->obd_name, logname, rc);
348                 RETURN(rc);
349         }
350
351         rc = llog_init_handle(env, handle, 0, NULL);
352         if (rc)
353                 GOTO(out_close, rc = -ENOENT);
354
355         switch (cmd) {
356         case OBD_IOC_LLOG_INFO: {
357                 int l;
358                 int remains = data->ioc_inllen2 +
359                                    cfs_size_round(data->ioc_inllen1);
360                 char *out = data->ioc_bulk;
361
362                 l = snprintf(out, remains,
363                              "logid:            "DFID":%x\n"
364                              "flags:            %x (%s)\n"
365                              "records_count:    %d\n"
366                              "last_index:       %d\n",
367                              PFID(&handle->lgh_id.lgl_oi.oi_fid),
368                              handle->lgh_id.lgl_ogen,
369                              handle->lgh_hdr->llh_flags,
370                              handle->lgh_hdr->llh_flags &
371                                 LLOG_F_IS_CAT ? "cat" : "plain",
372                              handle->lgh_hdr->llh_count,
373                              handle->lgh_last_idx);
374                 out += l;
375                 remains -= l;
376                 if (remains <= 0) {
377                         CERROR("%s: not enough space for log header info\n",
378                                ctxt->loc_obd->obd_name);
379                         rc = -ENOSPC;
380                 }
381                 break;
382         }
383         case OBD_IOC_LLOG_CHECK:
384                 LASSERT(data->ioc_inllen1 > 0);
385                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
386                 if (rc == -LLOG_EEMPTY)
387                         rc = 0;
388                 else if (rc)
389                         GOTO(out_close, rc);
390                 break;
391         case OBD_IOC_LLOG_PRINT:
392                 LASSERT(data->ioc_inllen1 > 0);
393                 rc = llog_process(env, handle, llog_print_cb, data, NULL);
394                 if (rc == -LLOG_EEMPTY)
395                         rc = 0;
396                 else if (rc)
397                         GOTO(out_close, rc);
398                 break;
399         case OBD_IOC_LLOG_CANCEL: {
400                 struct llog_cookie cookie;
401                 struct llog_logid plain;
402                 u32 lgc_index;
403
404                 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
405                 if (rc)
406                         GOTO(out_close, rc);
407                 cookie.lgc_index = lgc_index;
408
409                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
410                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
411                         GOTO(out_close, rc);
412                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
413                         GOTO(out_close, rc = -EINVAL);
414                 }
415
416                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
417                         GOTO(out_close, rc = -ENOTTY);
418
419                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
420                 if (rc)
421                         GOTO(out_close, rc);
422                 cookie.lgc_lgl = plain;
423                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
424                 if (rc)
425                         GOTO(out_close, rc);
426                 break;
427         }
428         case OBD_IOC_LLOG_REMOVE: {
429                 struct llog_logid plain;
430
431                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
432                         rc = llog_destroy(env, handle);
433                         GOTO(out_close, rc);
434                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
435                         GOTO(out_close, rc = -EINVAL);
436                 }
437
438                 if (data->ioc_inlbuf2) {
439                         /* remove indicate log from the catalog */
440                         rc = str2logid(&plain, data->ioc_inlbuf2,
441                                        data->ioc_inllen2);
442                         if (rc)
443                                 GOTO(out_close, rc);
444                         rc = llog_remove_log(env, handle, &plain);
445                 } else {
446                         /* remove all the log of the catalog */
447                         rc = llog_process(env, handle, llog_delete_cb, NULL,
448                                           NULL);
449                         if (rc)
450                                 GOTO(out_close, rc);
451                 }
452                 break;
453         }
454         default:
455                 CERROR("%s: Unknown ioctl cmd %#x\n",
456                        ctxt->loc_obd->obd_name, cmd);
457                 GOTO(out_close, rc = -ENOTTY);
458         }
459
460 out_close:
461         if (handle->lgh_hdr &&
462             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
463                 llog_cat_close(env, handle);
464         else
465                 llog_close(env, handle);
466         RETURN(rc);
467 }
468 EXPORT_SYMBOL(llog_ioctl);
469
470 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
471                       int count, struct obd_ioctl_data *data,
472                       const struct lu_fid *fid)
473 {
474         int size, i;
475         struct llog_catid *idarray;
476         struct llog_logid *id;
477         char *out;
478         int l, remains, rc = 0;
479
480         ENTRY;
481
482         if (count == 0) { /* get total number of logs */
483                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
484                 if (rc < 0)
485                         RETURN(rc);
486                 count = rc;
487         }
488
489         size = sizeof(*idarray) * count;
490
491         OBD_ALLOC_LARGE(idarray, size);
492         if (!idarray)
493                 RETURN(-ENOMEM);
494
495         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
496         if (rc)
497                 GOTO(out, rc);
498
499         out = data->ioc_bulk;
500         remains = data->ioc_inllen1;
501         /* OBD_FAIL: fetch the catalog records from the specified one */
502         if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
503                 data->ioc_count = cfs_fail_val - 1;
504         for (i = data->ioc_count; i < count; i++) {
505                 id = &idarray[i].lci_logid;
506                 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
507                               PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
508                 out += l;
509                 remains -= l;
510                 if (remains <= 0) {
511                         if (remains < 0) {
512                                 /* the print is not complete */
513                                 remains += l;
514                                 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
515                                 data->ioc_count = i;
516                         } else {
517                                 data->ioc_count = i++;
518                         }
519                         goto out;
520                 }
521         }
522         data->ioc_count = 0;
523 out:
524         OBD_FREE_LARGE(idarray, size);
525         RETURN(rc);
526 }
527 EXPORT_SYMBOL(llog_catalog_list);