Whamcloud - gitweb
d1c3fa5def18c5a8bf73c25d9ea951446efce773
[fs/lustre-release.git] / lustre / obdclass / llog_ioctl.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOG
38
39 #ifndef EXPORT_SYMTAB
40 #define EXPORT_SYMTAB
41 #endif
42
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <libcfs/list.h>
46 #include "llog_internal.h"
47
48 static int str2logid(struct llog_logid *logid, char *str, int len)
49 {
50         char *start, *end, *endp;
51
52         ENTRY;
53         start = str;
54         if (*start != '#')
55                 RETURN(-EINVAL);
56
57         start++;
58         if (start - str >= len - 1)
59                 RETURN(-EINVAL);
60         end = strchr(start, '#');
61         if (end == NULL || end == start)
62                 RETURN(-EINVAL);
63
64         *end = '\0';
65         logid->lgl_oid = simple_strtoull(start, &endp, 0);
66         if (endp != end)
67                 RETURN(-EINVAL);
68
69         start = ++end;
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         logid->lgl_ogr = simple_strtoull(start, &endp, 0);
78         if (endp != end)
79                 RETURN(-EINVAL);
80
81         start = ++end;
82         if (start - str >= len - 1)
83                 RETURN(-EINVAL);
84         logid->lgl_ogen = simple_strtoul(start, &endp, 16);
85         if (*endp != '\0')
86                 RETURN(-EINVAL);
87
88         RETURN(0);
89 }
90
91 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
92                          void *data)
93 {
94         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
95         static int l, remains, from, to;
96         static char *out;
97         char *endp;
98         int cur_index, rc = 0;
99
100         ENTRY;
101         cur_index = rec->lrh_index;
102
103         if (ioc_data && (ioc_data->ioc_inllen1)) {
104                 l = 0;
105                 remains = ioc_data->ioc_inllen4 +
106                         size_round(ioc_data->ioc_inllen1) +
107                         size_round(ioc_data->ioc_inllen2) +
108                         size_round(ioc_data->ioc_inllen3);
109                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
110                 if (*endp != '\0')
111                         RETURN(-EINVAL);
112                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
113                 if (*endp != '\0')
114                         RETURN(-EINVAL);
115                 ioc_data->ioc_inllen1 = 0;
116                 out = ioc_data->ioc_bulk;
117                 if (cur_index < from)
118                         RETURN(0);
119                 if (to > 0 && cur_index > to)
120                         RETURN(-LLOG_EEMPTY);
121         }
122         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
123                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
124                 struct llog_handle *log_handle;
125
126                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
127                         l = snprintf(out, remains, "[index]: %05d  [type]: "
128                                      "%02x  [len]: %04d failed\n",
129                                      cur_index, rec->lrh_type,
130                                      rec->lrh_len);
131                 }
132                 if (handle->lgh_ctxt == NULL)
133                         RETURN(-EOPNOTSUPP);
134                 rc = llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
135                 if (rc) {
136                         CDEBUG(D_IOCTL,
137                                "cannot find log #"LPX64"#"LPX64"#%08x\n",
138                                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogr,
139                                lir->lid_id.lgl_ogen);
140                         RETURN(rc);
141                 }
142                 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
143                 llog_close(log_handle);
144         } else {
145                 switch (rec->lrh_type) {
146                 case OST_SZ_REC:
147                 case OST_RAID1_REC:
148                 case MDS_UNLINK_REC:
149                 case MDS_SETATTR_REC:
150                 case OBD_CFG_REC:
151                 case LLOG_HDR_MAGIC: {
152                          l = snprintf(out, remains, "[index]: %05d  [type]: "
153                                       "%02x  [len]: %04d ok\n",
154                                       cur_index, rec->lrh_type,
155                                       rec->lrh_len);
156                          out += l;
157                          remains -= l;
158                          if (remains <= 0) {
159                                 CERROR("no space to print log records\n");
160                                 RETURN(-LLOG_EEMPTY);
161                          }
162                          RETURN(0);
163                 }
164                 default: {
165                          l = snprintf(out, remains, "[index]: %05d  [type]: "
166                                       "%02x  [len]: %04d failed\n",
167                                       cur_index, rec->lrh_type,
168                                       rec->lrh_len);
169                          out += l;
170                          remains -= l;
171                          if (remains <= 0) {
172                                 CERROR("no space to print log records\n");
173                                 RETURN(-LLOG_EEMPTY);
174                          }
175                          RETURN(0);
176                 }
177                 }
178         }
179         RETURN(rc);
180 }
181
182 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
183                          void *data)
184 {
185         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
186         static int l, remains, from, to;
187         static char *out;
188         char *endp;
189         int cur_index;
190
191         ENTRY;
192         if (ioc_data->ioc_inllen1) {
193                 l = 0;
194                 remains = ioc_data->ioc_inllen4 +
195                         size_round(ioc_data->ioc_inllen1) +
196                         size_round(ioc_data->ioc_inllen2) +
197                         size_round(ioc_data->ioc_inllen3);
198                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
199                 if (*endp != '\0')
200                         RETURN(-EINVAL);
201                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
202                 if (*endp != '\0')
203                         RETURN(-EINVAL);
204                 out = ioc_data->ioc_bulk;
205                 ioc_data->ioc_inllen1 = 0;
206         }
207
208         cur_index = rec->lrh_index;
209         if (cur_index < from)
210                 RETURN(0);
211         if (to > 0 && cur_index > to)
212                 RETURN(-LLOG_EEMPTY);
213
214         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
215                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
216                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
217                         CERROR("invalid record in catalog\n");
218                         RETURN(-EINVAL);
219                 }
220
221                 l = snprintf(out, remains,
222                              "[index]: %05d  [logid]: #"LPX64"#"LPX64"#%08x\n",
223                              cur_index, lir->lid_id.lgl_oid,
224                              lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
225         } else {
226                 l = snprintf(out, remains,
227                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
228                              cur_index, rec->lrh_type,
229                              rec->lrh_len);
230         }
231         out += l;
232         remains -= l;
233         if (remains <= 0) {
234                 CERROR("not enough space for print log records\n");
235                 RETURN(-LLOG_EEMPTY);
236         }
237
238         RETURN(0);
239 }
240 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
241 {
242         struct llog_handle *log;
243         int rc, index = 0;
244
245         ENTRY;
246         down_write(&cat->lgh_lock);
247         rc = llog_cat_id2handle(cat, &log, logid);
248         if (rc) {
249                 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
250                        logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
251                 GOTO(out, rc = -ENOENT);
252         }
253
254         index = log->u.phd.phd_cookie.lgc_index;
255         LASSERT(index);
256         rc = llog_destroy(log);
257         if (rc) {
258                 CDEBUG(D_IOCTL, "cannot destroy log\n");
259                 GOTO(out, rc);
260         }
261         llog_cat_set_first_idx(cat, index);
262         rc = llog_cancel_rec(cat, index);
263 out:
264         llog_free_handle(log);
265         up_write(&cat->lgh_lock);
266         RETURN(rc);
267
268 }
269
270 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
271                           void *data)
272 {
273         struct  llog_logid_rec *lir = (struct llog_logid_rec*)rec;
274         int     rc;
275
276         ENTRY;
277         if (rec->lrh_type != LLOG_LOGID_MAGIC)
278               RETURN (-EINVAL);
279         rc = llog_remove_log(handle, &lir->lid_id);
280
281         RETURN(rc);
282 }
283
284
285 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
286 {
287         struct llog_logid logid;
288         int err = 0;
289         struct llog_handle *handle = NULL;
290
291         ENTRY;
292         if (*data->ioc_inlbuf1 == '#') {
293                 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
294                 if (err)
295                         GOTO(out, err);
296                 err = llog_create(ctxt, &handle, &logid, NULL);
297                 if (err)
298                         GOTO(out, err);
299         } else if (*data->ioc_inlbuf1 == '$') {
300                 char *name = data->ioc_inlbuf1 + 1;
301                 err = llog_create(ctxt, &handle, NULL, name);
302                 if (err)
303                         GOTO(out, err);
304         } else {
305                 GOTO(out, err = -EINVAL);
306         }
307
308         err = llog_init_handle(handle, 0, NULL);
309         if (err)
310                 GOTO(out_close, err = -ENOENT);
311
312         switch (cmd) {
313         case OBD_IOC_LLOG_INFO: {
314                 int l;
315                 int remains = data->ioc_inllen2 +
316                         size_round(data->ioc_inllen1);
317                 char *out = data->ioc_bulk;
318
319                 l = snprintf(out, remains,
320                              "logid:            #"LPX64"#"LPX64"#%08x\n"
321                              "flags:            %x (%s)\n"
322                              "records count:    %d\n"
323                              "last index:       %d\n",
324                              handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
325                              handle->lgh_id.lgl_ogen,
326                              handle->lgh_hdr->llh_flags,
327                              handle->lgh_hdr->llh_flags &
328                              LLOG_F_IS_CAT ? "cat" : "plain",
329                              handle->lgh_hdr->llh_count,
330                              handle->lgh_last_idx);
331                 out += l;
332                 remains -= l;
333                 if (remains <= 0)
334                         CERROR("not enough space for log header info\n");
335
336                 GOTO(out_close, err);
337         }
338         case OBD_IOC_LLOG_CHECK: {
339                 LASSERT(data->ioc_inllen1);
340                 err = llog_process(handle, llog_check_cb, data, NULL);
341                 if (err == -LLOG_EEMPTY)
342                         err = 0;
343                 GOTO(out_close, err);
344         }
345
346         case OBD_IOC_LLOG_PRINT: {
347                 LASSERT(data->ioc_inllen1);
348                 err = llog_process(handle, class_config_dump_handler,data,NULL);
349                 if (err == -LLOG_EEMPTY)
350                         err = 0;
351                 else
352                         err = llog_process(handle, llog_print_cb, data, NULL);
353
354                 GOTO(out_close, err);
355         }
356         case OBD_IOC_LLOG_CANCEL: {
357                 struct llog_cookie cookie;
358                 struct llog_logid plain;
359                 char *endp;
360
361                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
362                 if (*endp != '\0')
363                         GOTO(out_close, err = -EINVAL);
364
365                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
366                         down_write(&handle->lgh_lock);
367                         err = llog_cancel_rec(handle, cookie.lgc_index);
368                         up_write(&handle->lgh_lock);
369                         GOTO(out_close, err);
370                 }
371
372                 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
373                 if (err)
374                         GOTO(out_close, err);
375                 cookie.lgc_lgl = plain;
376
377                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
378                         GOTO(out_close, err = -EINVAL);
379
380                 err = llog_cat_cancel_records(handle, 1, &cookie);
381                 GOTO(out_close, err);
382         }
383         case OBD_IOC_LLOG_REMOVE: {
384                 struct llog_logid plain;
385
386                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
387                         err = llog_destroy(handle);
388                         if (!err)
389                                 llog_free_handle(handle);
390                         GOTO(out, err);
391                 }
392
393                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
394                         GOTO(out_close, err = -EINVAL);
395
396                 if (data->ioc_inlbuf2) {
397                         /*remove indicate log from the catalog*/
398                         err = str2logid(&plain, data->ioc_inlbuf2,
399                                         data->ioc_inllen2);
400                         if (err)
401                                 GOTO(out_close, err);
402                         err = llog_remove_log(handle, &plain);
403                 } else {
404                         /*remove all the log of the catalog*/
405                         llog_process(handle, llog_delete_cb, NULL, NULL);
406                 }
407                 GOTO(out_close, err);
408         }
409         }
410
411 out_close:
412         if (handle->lgh_hdr &&
413             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
414                 llog_cat_put(handle);
415         else
416                 llog_close(handle);
417 out:
418         RETURN(err);
419 }
420 EXPORT_SYMBOL(llog_ioctl);
421
422 int llog_catalog_list(struct obd_device *obd, int count,
423                       struct obd_ioctl_data *data)
424 {
425         int size, i;
426         struct llog_catid *idarray;
427         struct llog_logid *id;
428         char name[32] = CATLIST;
429         char *out;
430         int l, remains, rc = 0;
431
432         ENTRY;
433         size = sizeof(*idarray) * count;
434
435         OBD_ALLOC(idarray, size);
436         if (!idarray)
437                 RETURN(-ENOMEM);
438         memset(idarray, 0, size);
439
440         rc = llog_get_cat_list(obd, obd, name, count, idarray);
441         if (rc) {
442                 OBD_FREE(idarray, size);
443                 RETURN(rc);
444         }
445
446         out = data->ioc_bulk;
447         remains = data->ioc_inllen1;
448         for (i = 0; i < count; i++) {
449                 id = &idarray[i].lci_logid;
450                 l = snprintf(out, remains,
451                              "catalog log: #"LPX64"#"LPX64"#%08x\n",
452                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
453                 out += l;
454                 remains -= l;
455                 if (remains <= 0) {
456                         CWARN("not enough memory for catlog list\n");
457                         break;
458                 }
459         }
460         OBD_FREE(idarray, size);
461         RETURN(0);
462
463 }
464 EXPORT_SYMBOL(llog_catalog_list);