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