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