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