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