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