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