Whamcloud - gitweb
LU-10198 llog: keep llog handle alive until last reference
[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(env, 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(env, 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, start;
322
323         ENTRY;
324
325         logname = data->ioc_inlbuf1;
326         start = logname[0];
327         if (start == '#' || start == '[') {
328                 rc = str2logid(&logid, logname, data->ioc_inllen1);
329                 if (rc)
330                         RETURN(rc);
331                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
332                                LLOG_OPEN_EXISTS);
333                 if (rc)
334                         RETURN(rc);
335         } else if (start == '$' || isalpha(start) || isdigit(start)) {
336                 if (start == '$')
337                         logname++;
338
339                 rc = llog_open(env, ctxt, &handle, NULL, logname,
340                                LLOG_OPEN_EXISTS);
341                 if (rc)
342                         RETURN(rc);
343         } else {
344                 rc = -EINVAL;
345                 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
346                       ctxt->loc_obd->obd_name, logname, rc);
347                 RETURN(rc);
348         }
349
350         rc = llog_init_handle(env, handle, 0, NULL);
351         if (rc)
352                 GOTO(out_close, rc = -ENOENT);
353
354         switch (cmd) {
355         case OBD_IOC_LLOG_INFO: {
356                 int l;
357                 int remains = data->ioc_inllen2 +
358                                    cfs_size_round(data->ioc_inllen1);
359                 char *out = data->ioc_bulk;
360
361                 l = snprintf(out, remains,
362                              "logid:            "DFID":%x\n"
363                              "flags:            %x (%s)\n"
364                              "records_count:    %d\n"
365                              "last_index:       %d\n",
366                              PFID(&handle->lgh_id.lgl_oi.oi_fid),
367                              handle->lgh_id.lgl_ogen,
368                              handle->lgh_hdr->llh_flags,
369                              handle->lgh_hdr->llh_flags &
370                                 LLOG_F_IS_CAT ? "cat" : "plain",
371                              handle->lgh_hdr->llh_count,
372                              handle->lgh_last_idx);
373                 out += l;
374                 remains -= l;
375                 if (remains <= 0) {
376                         CERROR("%s: not enough space for log header info\n",
377                                ctxt->loc_obd->obd_name);
378                         rc = -ENOSPC;
379                 }
380                 break;
381         }
382         case OBD_IOC_LLOG_CHECK:
383                 LASSERT(data->ioc_inllen1 > 0);
384                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
385                 if (rc == -LLOG_EEMPTY)
386                         rc = 0;
387                 else if (rc)
388                         GOTO(out_close, rc);
389                 break;
390         case OBD_IOC_LLOG_PRINT:
391                 LASSERT(data->ioc_inllen1 > 0);
392                 rc = llog_process(env, handle, llog_print_cb, data, NULL);
393                 if (rc == -LLOG_EEMPTY)
394                         rc = 0;
395                 else if (rc)
396                         GOTO(out_close, rc);
397                 break;
398         case OBD_IOC_LLOG_CANCEL: {
399                 struct llog_cookie cookie;
400                 struct llog_logid plain;
401                 u32 lgc_index;
402
403                 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
404                 if (rc)
405                         GOTO(out_close, rc);
406                 cookie.lgc_index = lgc_index;
407
408                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
409                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
410                         GOTO(out_close, rc);
411                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
412                         GOTO(out_close, rc = -EINVAL);
413                 }
414
415                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
416                         GOTO(out_close, rc = -ENOTTY);
417
418                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
419                 if (rc)
420                         GOTO(out_close, rc);
421                 cookie.lgc_lgl = plain;
422                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
423                 if (rc)
424                         GOTO(out_close, rc);
425                 break;
426         }
427         case OBD_IOC_LLOG_REMOVE: {
428                 struct llog_logid plain;
429
430                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
431                         rc = llog_destroy(env, handle);
432                         GOTO(out_close, rc);
433                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
434                         GOTO(out_close, rc = -EINVAL);
435                 }
436
437                 if (data->ioc_inlbuf2) {
438                         /* remove indicate log from the catalog */
439                         rc = str2logid(&plain, data->ioc_inlbuf2,
440                                        data->ioc_inllen2);
441                         if (rc)
442                                 GOTO(out_close, rc);
443                         rc = llog_remove_log(env, handle, &plain);
444                 } else {
445                         /* remove all the log of the catalog */
446                         rc = llog_process(env, handle, llog_delete_cb, NULL,
447                                           NULL);
448                         if (rc)
449                                 GOTO(out_close, rc);
450                 }
451                 break;
452         }
453         default:
454                 CERROR("%s: Unknown ioctl cmd %#x\n",
455                        ctxt->loc_obd->obd_name, cmd);
456                 GOTO(out_close, rc = -ENOTTY);
457         }
458
459 out_close:
460         if (handle->lgh_hdr &&
461             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
462                 llog_cat_close(env, handle);
463         else
464                 llog_close(env, handle);
465         RETURN(rc);
466 }
467 EXPORT_SYMBOL(llog_ioctl);
468
469 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
470                       int count, struct obd_ioctl_data *data,
471                       const struct lu_fid *fid)
472 {
473         int size, i;
474         struct llog_catid *idarray;
475         struct llog_logid *id;
476         char *out;
477         int l, remains, rc = 0;
478
479         ENTRY;
480
481         if (count == 0) { /* get total number of logs */
482                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
483                 if (rc < 0)
484                         RETURN(rc);
485                 count = rc;
486         }
487
488         size = sizeof(*idarray) * count;
489
490         OBD_ALLOC_LARGE(idarray, size);
491         if (!idarray)
492                 RETURN(-ENOMEM);
493
494         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
495         if (rc)
496                 GOTO(out, rc);
497
498         out = data->ioc_bulk;
499         remains = data->ioc_inllen1;
500         for (i = 0; i < count; i++) {
501                 id = &idarray[i].lci_logid;
502                 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
503                              PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
504                 out += l;
505                 remains -= l;
506                 if (remains <= 0)
507                         break;
508         }
509 out:
510         OBD_FREE_LARGE(idarray, size);
511         RETURN(rc);
512 }
513 EXPORT_SYMBOL(llog_catalog_list);