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