Whamcloud - gitweb
LU-15706 llog: deal with "SKIP" pool llog records correctly
[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 struct llog_print_data {
205         struct obd_ioctl_data *lprd_data;
206         unsigned int           lprd_cfg_flags;
207         bool                   lprd_raw;
208 };
209
210 #define MARKER_DIFF     10
211 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
212                          struct llog_rec_hdr *rec, void *data)
213 {
214         struct llog_print_data *lprd = data;
215         struct obd_ioctl_data *ioc_data = lprd->lprd_data;
216         static int l, remains;
217         static long from, to;
218         static char *out;
219         int cur_index;
220         int rc;
221
222         ENTRY;
223         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
224                 l = 0;
225                 remains = ioc_data->ioc_inllen4 +
226                           round_up(ioc_data->ioc_inllen1, 8) +
227                           round_up(ioc_data->ioc_inllen2, 8) +
228                           round_up(ioc_data->ioc_inllen3, 8);
229
230                 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
231                 if (rc)
232                         RETURN(rc);
233
234                 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
235                 if (rc)
236                         RETURN(rc);
237
238                 out = ioc_data->ioc_bulk;
239                 ioc_data->ioc_inllen1 = 0;
240         }
241
242         cur_index = rec->lrh_index;
243         if (from > MARKER_DIFF && cur_index >= from - MARKER_DIFF &&
244             cur_index < from) {
245                 /* LU-15706: try to remember the marker cfg_flag that the "from"
246                  * is using, in case that the "from" record doesn't know its
247                  * "SKIP" or not flag.
248                  */
249                 llog_get_marker_cfg_flags(rec, &lprd->lprd_cfg_flags);
250         }
251         if (cur_index < from)
252                 RETURN(0);
253         if (to > 0 && cur_index > to)
254                 RETURN(-LLOG_EEMPTY);
255
256         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
257                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
258
259                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
260                         CERROR("invalid record in catalog\n");
261                         RETURN(-EINVAL);
262                 }
263
264                 l = snprintf(out, remains,
265                              "[index]: %05d  [logid]: "DFID":%x\n",
266                              cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
267                              lir->lid_id.lgl_ogen);
268         } else if (rec->lrh_type == OBD_CFG_REC) {
269                 int rc;
270
271                 rc = class_config_yaml_output(rec, out, remains,
272                                               &lprd->lprd_cfg_flags,
273                                               lprd->lprd_raw);
274                 if (rc < 0)
275                         RETURN(rc);
276                 l = rc;
277         } else {
278                 l = snprintf(out, remains,
279                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
280                              cur_index, rec->lrh_type, rec->lrh_len);
281         }
282         out += l;
283         remains -= l;
284         if (remains <= 0) {
285                 CERROR("not enough space for print log records\n");
286                 RETURN(-LLOG_EEMPTY);
287         }
288
289         RETURN(0);
290 }
291 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
292                            struct llog_logid *logid)
293 {
294         struct llog_handle *log;
295         int rc;
296
297         ENTRY;
298
299         rc = llog_cat_id2handle(env, cat, &log, logid);
300         if (rc) {
301                 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
302                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
303                 RETURN(-ENOENT);
304         }
305
306         rc = llog_destroy(env, log);
307         if (rc) {
308                 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
309                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
310                 GOTO(out, rc);
311         }
312         llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
313 out:
314         llog_handle_put(env, log);
315         RETURN(rc);
316
317 }
318
319 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
320                           struct llog_rec_hdr *rec, void *data)
321 {
322         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
323         int rc;
324
325         ENTRY;
326         if (rec->lrh_type != LLOG_LOGID_MAGIC)
327                 RETURN(-EINVAL);
328         rc = llog_remove_log(env, handle, &lir->lid_id);
329
330         RETURN(rc);
331 }
332
333
334 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
335                struct obd_ioctl_data *data)
336 {
337         struct llog_logid logid;
338         int rc = 0;
339         struct llog_handle *handle = NULL;
340         char *logname, start;
341
342         ENTRY;
343
344         logname = data->ioc_inlbuf1;
345         start = logname[0];
346         if (start == '#' || start == '[') {
347                 rc = str2logid(&logid, logname, data->ioc_inllen1);
348                 if (rc)
349                         RETURN(rc);
350                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
351                                LLOG_OPEN_EXISTS);
352                 if (rc)
353                         RETURN(rc);
354         } else if (start == '$' || isalpha(start) || isdigit(start)) {
355                 if (start == '$')
356                         logname++;
357
358                 rc = llog_open(env, ctxt, &handle, NULL, logname,
359                                LLOG_OPEN_EXISTS);
360                 if (rc)
361                         RETURN(rc);
362         } else {
363                 rc = -EINVAL;
364                 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
365                       ctxt->loc_obd->obd_name, logname, rc);
366                 RETURN(rc);
367         }
368
369         rc = llog_init_handle(env, handle, 0, NULL);
370         if (rc)
371                 GOTO(out_close, rc = -ENOENT);
372
373         switch (cmd) {
374         case OBD_IOC_LLOG_INFO: {
375                 int l;
376                 int remains = data->ioc_inllen2 +
377                                    cfs_size_round(data->ioc_inllen1);
378                 char *out = data->ioc_bulk;
379
380                 l = snprintf(out, remains,
381                              "logid:            "DFID":%x\n"
382                              "flags:            %x (%s)\n"
383                              "records_count:    %d\n"
384                              "last_index:       %d\n",
385                              PFID(&handle->lgh_id.lgl_oi.oi_fid),
386                              handle->lgh_id.lgl_ogen,
387                              handle->lgh_hdr->llh_flags,
388                              handle->lgh_hdr->llh_flags &
389                                 LLOG_F_IS_CAT ? "cat" : "plain",
390                              handle->lgh_hdr->llh_count,
391                              handle->lgh_last_idx);
392                 out += l;
393                 remains -= l;
394                 if (remains <= 0) {
395                         CERROR("%s: not enough space for log header info\n",
396                                ctxt->loc_obd->obd_name);
397                         rc = -ENOSPC;
398                 }
399                 break;
400         }
401         case OBD_IOC_LLOG_CHECK:
402                 LASSERT(data->ioc_inllen1 > 0);
403                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
404                 if (rc == -LLOG_EEMPTY)
405                         rc = 0;
406                 else if (rc)
407                         GOTO(out_close, rc);
408                 break;
409         case OBD_IOC_LLOG_PRINT: {
410                 struct llog_print_data lprd = {
411                         .lprd_data = data,
412                         .lprd_raw = data->ioc_u32_1,
413                 };
414
415                 LASSERT(data->ioc_inllen1 > 0);
416                 rc = llog_process(env, handle, llog_print_cb, &lprd, NULL);
417                 if (rc == -LLOG_EEMPTY)
418                         rc = 0;
419                 else if (rc)
420                         GOTO(out_close, rc);
421                 break;
422         }
423         case OBD_IOC_LLOG_CANCEL: {
424                 struct llog_cookie cookie;
425                 struct llog_logid plain;
426                 u32 lgc_index;
427
428                 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
429                 if (rc)
430                         GOTO(out_close, rc);
431                 cookie.lgc_index = lgc_index;
432
433                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
434                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
435                         GOTO(out_close, rc);
436                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
437                         GOTO(out_close, rc = -EINVAL);
438                 }
439
440                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
441                         GOTO(out_close, rc = -ENOTTY);
442
443                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
444                 if (rc)
445                         GOTO(out_close, rc);
446                 cookie.lgc_lgl = plain;
447                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
448                 if (rc)
449                         GOTO(out_close, rc);
450                 break;
451         }
452         case OBD_IOC_LLOG_REMOVE: {
453                 struct llog_logid plain;
454
455                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
456                         rc = llog_destroy(env, handle);
457                         GOTO(out_close, rc);
458                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
459                         GOTO(out_close, rc = -EINVAL);
460                 }
461
462                 if (data->ioc_inlbuf2) {
463                         /* remove indicate log from the catalog */
464                         rc = str2logid(&plain, data->ioc_inlbuf2,
465                                        data->ioc_inllen2);
466                         if (rc)
467                                 GOTO(out_close, rc);
468                         rc = llog_remove_log(env, handle, &plain);
469                 } else {
470                         /* remove all the log of the catalog */
471                         rc = llog_process(env, handle, llog_delete_cb, NULL,
472                                           NULL);
473                         if (rc)
474                                 GOTO(out_close, rc);
475                 }
476                 break;
477         }
478         default:
479                 CERROR("%s: Unknown ioctl cmd %#x\n",
480                        ctxt->loc_obd->obd_name, cmd);
481                 GOTO(out_close, rc = -ENOTTY);
482         }
483
484 out_close:
485         if (handle->lgh_hdr &&
486             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
487                 llog_cat_close(env, handle);
488         else
489                 llog_close(env, handle);
490         RETURN(rc);
491 }
492 EXPORT_SYMBOL(llog_ioctl);
493
494 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
495                       int count, struct obd_ioctl_data *data,
496                       const struct lu_fid *fid)
497 {
498         int size, i;
499         struct llog_catid *idarray;
500         struct llog_logid *id;
501         char *out;
502         int l, remains, rc = 0;
503
504         ENTRY;
505
506         if (count == 0) { /* get total number of logs */
507                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
508                 if (rc < 0)
509                         RETURN(rc);
510                 count = rc;
511         }
512
513         size = sizeof(*idarray) * count;
514
515         OBD_ALLOC_LARGE(idarray, size);
516         if (!idarray)
517                 RETURN(-ENOMEM);
518
519         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
520         if (rc)
521                 GOTO(out, rc);
522
523         out = data->ioc_bulk;
524         remains = data->ioc_inllen1;
525         /* OBD_FAIL: fetch the catalog records from the specified one */
526         if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
527                 data->ioc_count = cfs_fail_val - 1;
528         for (i = data->ioc_count; i < count; i++) {
529                 id = &idarray[i].lci_logid;
530                 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
531                               PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
532                 out += l;
533                 remains -= l;
534                 if (remains <= 0) {
535                         if (remains < 0) {
536                                 /* the print is not complete */
537                                 remains += l;
538                                 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
539                                 data->ioc_count = i;
540                         } else {
541                                 data->ioc_count = i++;
542                         }
543                         goto out;
544                 }
545         }
546         data->ioc_count = 0;
547 out:
548         OBD_FREE_LARGE(idarray, size);
549         RETURN(rc);
550 }
551 EXPORT_SYMBOL(llog_catalog_list);