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