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