Whamcloud - gitweb
LU-9325 llog: replace simple_strtol with kstrtol
[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                 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                 RETURN(num == 3 && fid_is_sane(fid) ? 0 : -EINVAL);
58         }
59
60 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
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         if (start[0] != '#')
66                 RETURN(-EINVAL);
67
68         start++;
69         if (start - str >= len - 1)
70                 RETURN(-EINVAL);
71         end = strchr(start, '#');
72         if (end == NULL || end == start)
73                 RETURN(-EINVAL);
74
75         *end = '\0';
76         rc = kstrtoull(start, 0, &id);
77         if (rc)
78                 RETURN(rc);
79
80         start = ++end;
81         if (start - str >= len - 1)
82                 RETURN(-EINVAL);
83
84         end = strchr(start, '#');
85         if (!end || end == start)
86                 RETURN(-EINVAL);
87
88         *end = '\0';
89         rc = kstrtoull(start, 0, &seq);
90         if (rc)
91                 RETURN(rc);
92
93         ostid_set_seq(&logid->lgl_oi, seq);
94         if (ostid_set_id(&logid->lgl_oi, id))
95                 RETURN(-EINVAL);
96
97         start = ++end;
98         if (start - str >= len - 1)
99                 RETURN(-EINVAL);
100
101         rc = kstrtouint(start, 16, &ogen);
102         if (rc)
103                 RETURN(-EINVAL);
104         logid->lgl_ogen = ogen;
105
106         RETURN(0);
107 #else
108         RETURN(-EINVAL);
109 #endif
110 }
111
112 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
113                          struct llog_rec_hdr *rec, void *data)
114 {
115         struct obd_ioctl_data *ioc_data = data;
116         static int l, remains;
117         static long from, to;
118         static char *out;
119         int cur_index;
120         int rc = 0;
121
122         ENTRY;
123         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
124                 l = 0;
125                 remains = ioc_data->ioc_inllen4 +
126                           round_up(ioc_data->ioc_inllen1, 8) +
127                           round_up(ioc_data->ioc_inllen2, 8) +
128                           round_up(ioc_data->ioc_inllen3, 8);
129
130                 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
131                 if (rc)
132                         RETURN(rc);
133
134                 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
135                 if (rc)
136                         RETURN(rc);
137
138                 ioc_data->ioc_inllen1 = 0;
139                 out = ioc_data->ioc_bulk;
140         }
141
142         cur_index = rec->lrh_index;
143         if (cur_index < from)
144                 RETURN(0);
145         if (to > 0 && cur_index > to)
146                 RETURN(-LLOG_EEMPTY);
147
148         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
149                 struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
150                 struct llog_handle      *loghandle;
151
152                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
153                         l = snprintf(out, remains, "[index]: %05d  [type]: "
154                                      "%02x  [len]: %04d failed\n",
155                                      cur_index, rec->lrh_type,
156                                      rec->lrh_len);
157                 }
158                 if (handle->lgh_ctxt == NULL)
159                         RETURN(-EOPNOTSUPP);
160                 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
161                 if (rc) {
162                         CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
163                                PFID(&lir->lid_id.lgl_oi.oi_fid),
164                                lir->lid_id.lgl_ogen);
165                         RETURN(rc);
166                 }
167                 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
168                 llog_handle_put(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 int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
202                          struct llog_rec_hdr *rec, void *data)
203 {
204         struct obd_ioctl_data *ioc_data = data;
205         static int l, remains;
206         static long from, to;
207         static char *out;
208         int cur_index;
209         int rc;
210
211         ENTRY;
212         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
213                 l = 0;
214                 remains = ioc_data->ioc_inllen4 +
215                           round_up(ioc_data->ioc_inllen1, 8) +
216                           round_up(ioc_data->ioc_inllen2, 8) +
217                           round_up(ioc_data->ioc_inllen3, 8);
218
219                 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
220                 if (rc)
221                         RETURN(rc);
222
223                 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
224                 if (rc)
225                         RETURN(rc);
226
227                 out = ioc_data->ioc_bulk;
228                 ioc_data->ioc_inllen1 = 0;
229         }
230
231         cur_index = rec->lrh_index;
232         if (cur_index < from)
233                 RETURN(0);
234         if (to > 0 && cur_index > to)
235                 RETURN(-LLOG_EEMPTY);
236
237         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
238                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
239
240                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
241                         CERROR("invalid record in catalog\n");
242                         RETURN(-EINVAL);
243                 }
244
245                 l = snprintf(out, remains,
246                              "[index]: %05d  [logid]: "DFID":%x\n",
247                              cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
248                              lir->lid_id.lgl_ogen);
249         } else if (rec->lrh_type == OBD_CFG_REC) {
250                 int rc;
251
252                 rc = class_config_yaml_output(rec, out, remains);
253                 if (rc < 0)
254                         RETURN(rc);
255                 l = rc;
256         } else {
257                 l = snprintf(out, remains,
258                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
259                              cur_index, rec->lrh_type, rec->lrh_len);
260         }
261         out += l;
262         remains -= l;
263         if (remains <= 0) {
264                 CERROR("not enough space for print log records\n");
265                 RETURN(-LLOG_EEMPTY);
266         }
267
268         RETURN(0);
269 }
270 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
271                            struct llog_logid *logid)
272 {
273         struct llog_handle      *log;
274         int                      rc;
275
276         ENTRY;
277
278         rc = llog_cat_id2handle(env, cat, &log, logid);
279         if (rc) {
280                 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
281                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
282                 RETURN(-ENOENT);
283         }
284
285         rc = llog_destroy(env, log);
286         if (rc) {
287                 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
288                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
289                 GOTO(out, rc);
290         }
291         llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
292 out:
293         llog_handle_put(log);
294         RETURN(rc);
295
296 }
297
298 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
299                           struct llog_rec_hdr *rec, void *data)
300 {
301         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
302         int                      rc;
303
304         ENTRY;
305         if (rec->lrh_type != LLOG_LOGID_MAGIC)
306                 RETURN(-EINVAL);
307         rc = llog_remove_log(env, handle, &lir->lid_id);
308
309         RETURN(rc);
310 }
311
312
313 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
314                struct obd_ioctl_data *data)
315 {
316         struct llog_logid        logid;
317         int                      rc = 0;
318         struct llog_handle      *handle = NULL;
319         char *logname;
320
321         ENTRY;
322
323         logname = data->ioc_inlbuf1;
324         if (logname[0] == '#' || logname[0] == '[') {
325                 rc = str2logid(&logid, logname, data->ioc_inllen1);
326                 if (rc)
327                         RETURN(rc);
328                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
329                                LLOG_OPEN_EXISTS);
330                 if (rc)
331                         RETURN(rc);
332         } else if (logname[0] == '$' || isalpha(logname[0])) {
333                 if (logname[0] == '$')
334                         logname++;
335
336                 rc = llog_open(env, ctxt, &handle, NULL, logname,
337                                LLOG_OPEN_EXISTS);
338                 if (rc)
339                         RETURN(rc);
340         } else {
341                 RETURN(-EINVAL);
342         }
343
344         rc = llog_init_handle(env, handle, 0, NULL);
345         if (rc)
346                 GOTO(out_close, rc = -ENOENT);
347
348         switch (cmd) {
349         case OBD_IOC_LLOG_INFO: {
350                 int      l;
351                 int      remains = data->ioc_inllen2 +
352                                    cfs_size_round(data->ioc_inllen1);
353                 char    *out = data->ioc_bulk;
354
355                 l = snprintf(out, remains,
356                              "logid:            "DFID":%x\n"
357                              "flags:            %x (%s)\n"
358                              "records_count:    %d\n"
359                              "last_index:       %d\n",
360                              PFID(&handle->lgh_id.lgl_oi.oi_fid),
361                              handle->lgh_id.lgl_ogen,
362                              handle->lgh_hdr->llh_flags,
363                              handle->lgh_hdr->llh_flags &
364                                 LLOG_F_IS_CAT ? "cat" : "plain",
365                              handle->lgh_hdr->llh_count,
366                              handle->lgh_last_idx);
367                 out += l;
368                 remains -= l;
369                 if (remains <= 0) {
370                         CERROR("%s: not enough space for log header info\n",
371                                ctxt->loc_obd->obd_name);
372                         rc = -ENOSPC;
373                 }
374                 break;
375         }
376         case OBD_IOC_LLOG_CHECK:
377                 LASSERT(data->ioc_inllen1 > 0);
378                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
379                 if (rc == -LLOG_EEMPTY)
380                         rc = 0;
381                 else if (rc)
382                         GOTO(out_close, rc);
383                 break;
384         case OBD_IOC_LLOG_PRINT:
385                 LASSERT(data->ioc_inllen1 > 0);
386                 rc = llog_process(env, handle, llog_print_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_CANCEL: {
393                 struct llog_cookie cookie;
394                 struct llog_logid plain;
395                 u32 lgc_index;
396
397                 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
398                 if (rc)
399                         GOTO(out_close, rc);
400                 cookie.lgc_index = lgc_index;
401
402                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
403                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
404                         GOTO(out_close, rc);
405                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
406                         GOTO(out_close, rc = -EINVAL);
407                 }
408
409                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
410                         GOTO(out_close, rc = -ENOTTY);
411
412                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
413                 if (rc)
414                         GOTO(out_close, rc);
415                 cookie.lgc_lgl = plain;
416                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
417                 if (rc)
418                         GOTO(out_close, rc);
419                 break;
420         }
421         case OBD_IOC_LLOG_REMOVE: {
422                 struct llog_logid plain;
423
424                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
425                         rc = llog_destroy(env, handle);
426                         GOTO(out_close, rc);
427                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
428                         GOTO(out_close, rc = -EINVAL);
429                 }
430
431                 if (data->ioc_inlbuf2 > 0) {
432                         /* remove indicate log from the catalog */
433                         rc = str2logid(&plain, data->ioc_inlbuf2,
434                                        data->ioc_inllen2);
435                         if (rc)
436                                 GOTO(out_close, rc);
437                         rc = llog_remove_log(env, handle, &plain);
438                 } else {
439                         /* remove all the log of the catalog */
440                         rc = llog_process(env, handle, llog_delete_cb, NULL,
441                                           NULL);
442                         if (rc)
443                                 GOTO(out_close, rc);
444                 }
445                 break;
446         }
447         default:
448                 CERROR("%s: Unknown ioctl cmd %#x\n",
449                        ctxt->loc_obd->obd_name, cmd);
450                 GOTO(out_close, rc = -ENOTTY);
451         }
452
453 out_close:
454         if (handle->lgh_hdr &&
455             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
456                 llog_cat_close(env, handle);
457         else
458                 llog_close(env, handle);
459         RETURN(rc);
460 }
461 EXPORT_SYMBOL(llog_ioctl);
462
463 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
464                       int count, struct obd_ioctl_data *data,
465                       const struct lu_fid *fid)
466 {
467         int                      size, i;
468         struct llog_catid       *idarray;
469         struct llog_logid       *id;
470         char                    *out;
471         int                      l, remains, rc = 0;
472
473         ENTRY;
474
475         if (count == 0) { /* get total number of logs */
476                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
477                 if (rc < 0)
478                         RETURN(rc);
479                 count = rc;
480         }
481
482         size = sizeof(*idarray) * count;
483
484         OBD_ALLOC_LARGE(idarray, size);
485         if (!idarray)
486                 RETURN(-ENOMEM);
487
488         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
489         if (rc)
490                 GOTO(out, rc);
491
492         out = data->ioc_bulk;
493         remains = data->ioc_inllen1;
494         for (i = 0; i < count; i++) {
495                 id = &idarray[i].lci_logid;
496                 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
497                              PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
498                 out += l;
499                 remains -= l;
500                 if (remains <= 0)
501                         break;
502         }
503 out:
504         OBD_FREE_LARGE(idarray, size);
505         RETURN(rc);
506 }
507 EXPORT_SYMBOL(llog_catalog_list);