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