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