Whamcloud - gitweb
9bdea74eaf27d36e24d7c933636a80fb73215f1d
[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                 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
123                 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
124                 llog_close(log_handle);
125         } else {
126                 switch (rec->lrh_type) {
127                 case OST_SZ_REC:
128                 case OST_RAID1_REC:
129                 case MDS_UNLINK_REC:
130                 case MDS_SETATTR_REC:
131                 case OBD_CFG_REC:
132                 case LLOG_HDR_MAGIC: {
133                          l = snprintf(out, remains, "[index]: %05d  [type]: "
134                                       "%02x  [len]: %04d ok\n",
135                                       cur_index, rec->lrh_type,
136                                       rec->lrh_len);
137                          out += l;
138                          remains -= l;
139                          if (remains <= 0) {
140                                 CERROR("no space to print log records\n");
141                                 RETURN(-LLOG_EEMPTY);
142                          }
143                          RETURN(0);
144                 }
145                 default: {
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                          out += l;
151                          remains -= l;
152                          if (remains <= 0) {
153                                 CERROR("no space to print log records\n");
154                                 RETURN(-LLOG_EEMPTY);
155                          }
156                          RETURN(0);
157                 }
158                 }
159         }
160         RETURN(rc);
161 }
162
163 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
164                          void *data)
165 {
166         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
167         static int l, remains, from, to;
168         static char *out;
169         char *endp;
170         int cur_index;
171
172         ENTRY;
173         if (ioc_data->ioc_inllen1) {
174                 l = 0;
175                 remains = ioc_data->ioc_inllen4 +
176                         size_round(ioc_data->ioc_inllen1) +
177                         size_round(ioc_data->ioc_inllen2) +
178                         size_round(ioc_data->ioc_inllen3);
179                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
180                 if (*endp != '\0')
181                         RETURN(-EINVAL);
182                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
183                 if (*endp != '\0')
184                         RETURN(-EINVAL);
185                 out = ioc_data->ioc_bulk;
186                 ioc_data->ioc_inllen1 = 0;
187         }
188
189         cur_index = rec->lrh_index;
190         if (cur_index < from)
191                 RETURN(0);
192         if (to > 0 && cur_index > to)
193                 RETURN(-LLOG_EEMPTY);
194
195         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
196                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
197                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
198                         CERROR("invalid record in catalog\n");
199                         RETURN(-EINVAL);
200                 }
201
202                 l = snprintf(out, remains,
203                              "[index]: %05d  [logid]: #"LPX64"#"LPX64"#%08x\n",
204                              cur_index, lir->lid_id.lgl_oid,
205                              lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
206         } else {
207                 l = snprintf(out, remains,
208                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
209                              cur_index, rec->lrh_type,
210                              rec->lrh_len);
211         }
212         out += l;
213         remains -= l;
214         if (remains <= 0) {
215                 CERROR("not enough space for print log records\n");
216                 RETURN(-LLOG_EEMPTY);
217         }
218
219         RETURN(0);
220 }
221 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
222 {
223         struct llog_handle *log;
224         int rc, index = 0;
225
226         ENTRY;
227         down_write(&cat->lgh_lock);
228         rc = llog_cat_id2handle(cat, &log, logid);
229         if (rc) {
230                 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
231                        logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
232                 GOTO(out, rc = -ENOENT);
233         }
234
235         index = log->u.phd.phd_cookie.lgc_index;
236         LASSERT(index);
237         rc = llog_destroy(log);
238         if (rc) {
239                 CDEBUG(D_IOCTL, "cannot destroy log\n");
240                 GOTO(out, rc);
241         }
242         llog_cat_set_first_idx(cat, index);
243         rc = llog_cancel_rec(cat, index);
244 out:
245         llog_free_handle(log);
246         up_write(&cat->lgh_lock);
247         RETURN(rc);
248
249 }
250
251 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
252                           void *data)
253 {
254         struct  llog_logid_rec *lir = (struct llog_logid_rec*)rec;
255         int     rc;
256
257         ENTRY;
258         if (rec->lrh_type != LLOG_LOGID_MAGIC)
259               RETURN (-EINVAL);
260         rc = llog_remove_log(handle, &lir->lid_id);
261
262         RETURN(rc);
263 }
264
265
266 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
267 {
268         struct llog_logid logid;
269         int err = 0;
270         struct llog_handle *handle = NULL;
271
272         ENTRY;
273         if (*data->ioc_inlbuf1 == '#') {
274                 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
275                 if (err)
276                         GOTO(out, err);
277                 err = llog_create(ctxt, &handle, &logid, NULL);
278                 if (err)
279                         GOTO(out, err);
280         } else if (*data->ioc_inlbuf1 == '$') {
281                 char *name = data->ioc_inlbuf1 + 1;
282                 err = llog_create(ctxt, &handle, NULL, name);
283                 if (err)
284                         GOTO(out, err);
285         } else {
286                 GOTO(out, err = -EINVAL);
287         }
288
289         err = llog_init_handle(handle, 0, NULL);
290         if (err)
291                 GOTO(out_close, err = -ENOENT);
292
293         switch (cmd) {
294         case OBD_IOC_LLOG_INFO: {
295                 int l;
296                 int remains = data->ioc_inllen2 +
297                         size_round(data->ioc_inllen1);
298                 char *out = data->ioc_bulk;
299
300                 l = snprintf(out, remains,
301                              "logid:            #"LPX64"#"LPX64"#%08x\n"
302                              "flags:            %x (%s)\n"
303                              "records count:    %d\n"
304                              "last index:       %d\n",
305                              handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
306                              handle->lgh_id.lgl_ogen,
307                              handle->lgh_hdr->llh_flags,
308                              handle->lgh_hdr->llh_flags &
309                              LLOG_F_IS_CAT ? "cat" : "plain",
310                              handle->lgh_hdr->llh_count,
311                              handle->lgh_last_idx);
312                 out += l;
313                 remains -= l;
314                 if (remains <= 0)
315                         CERROR("not enough space for log header info\n");
316
317                 GOTO(out_close, err);
318         }
319         case OBD_IOC_LLOG_CHECK: {
320                 LASSERT(data->ioc_inllen1);
321                 err = llog_process(handle, llog_check_cb, data, NULL);
322                 if (err == -LLOG_EEMPTY)
323                         err = 0;
324                 GOTO(out_close, err);
325         }
326
327         case OBD_IOC_LLOG_PRINT: {
328                 LASSERT(data->ioc_inllen1);
329                 err = llog_process(handle, class_config_dump_handler,data,NULL);
330                 if (err == -LLOG_EEMPTY)
331                         err = 0;
332                 else
333                         err = llog_process(handle, llog_print_cb, data, NULL);
334
335                 GOTO(out_close, err);
336         }
337         case OBD_IOC_LLOG_CANCEL: {
338                 struct llog_cookie cookie;
339                 struct llog_logid plain;
340                 char *endp;
341
342                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
343                 if (*endp != '\0')
344                         GOTO(out_close, err = -EINVAL);
345
346                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
347                         down_write(&handle->lgh_lock);
348                         err = llog_cancel_rec(handle, cookie.lgc_index);
349                         up_write(&handle->lgh_lock);
350                         GOTO(out_close, err);
351                 }
352
353                 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
354                 if (err)
355                         GOTO(out_close, err);
356                 cookie.lgc_lgl = plain;
357
358                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
359                         GOTO(out_close, err = -EINVAL);
360
361                 err = llog_cat_cancel_records(handle, 1, &cookie);
362                 GOTO(out_close, err);
363         }
364         case OBD_IOC_LLOG_REMOVE: {
365                 struct llog_logid plain;
366
367                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
368                         err = llog_destroy(handle);
369                         if (!err)
370                                 llog_free_handle(handle);
371                         GOTO(out, err);
372                 }
373
374                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
375                         GOTO(out_close, err = -EINVAL);
376
377                 if (data->ioc_inlbuf2) {
378                         /*remove indicate log from the catalog*/
379                         err = str2logid(&plain, data->ioc_inlbuf2,
380                                         data->ioc_inllen2);
381                         if (err)
382                                 GOTO(out_close, err);
383                         err = llog_remove_log(handle, &plain);
384                 } else {
385                         /*remove all the log of the catalog*/
386                         llog_process(handle, llog_delete_cb, NULL, NULL);
387                 }
388                 GOTO(out_close, err);
389         }
390         }
391
392 out_close:
393         if (handle->lgh_hdr &&
394             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
395                 llog_cat_put(handle);
396         else
397                 llog_close(handle);
398 out:
399         RETURN(err);
400 }
401 EXPORT_SYMBOL(llog_ioctl);
402
403 int llog_catalog_list(struct obd_device *obd, int count,
404                       struct obd_ioctl_data *data)
405 {
406         int size, i;
407         struct llog_catid *idarray;
408         struct llog_logid *id;
409         char name[32] = CATLIST;
410         char *out;
411         int l, remains, rc = 0;
412
413         ENTRY;
414         size = sizeof(*idarray) * count;
415
416         OBD_ALLOC(idarray, size);
417         if (!idarray)
418                 RETURN(-ENOMEM);
419         memset(idarray, 0, size);
420
421         rc = llog_get_cat_list(obd, obd, name, count, idarray);
422         if (rc) {
423                 OBD_FREE(idarray, size);
424                 RETURN(rc);
425         }
426
427         out = data->ioc_bulk;
428         remains = data->ioc_inllen1;
429         for (i = 0; i < count; i++) {
430                 id = &idarray[i].lci_logid;
431                 l = snprintf(out, remains,
432                              "catalog log: #"LPX64"#"LPX64"#%08x\n",
433                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
434                 out += l;
435                 remains -= l;
436                 if (remains <= 0) {
437                         CWARN("not enough memory for catlog list\n");
438                         break;
439                 }
440         }
441         OBD_FREE(idarray, size);
442         RETURN(0);
443
444 }
445 EXPORT_SYMBOL(llog_catalog_list);