Whamcloud - gitweb
b=2325
[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  *   You should have received a copy of the GNU General Public License
5  *   along with Lustre; if not, write to the Free Software
6  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7  */
8
9 #define DEBUG_SUBSYSTEM S_LOG
10
11 #ifndef EXPORT_SYMTAB
12 #define EXPORT_SYMTAB
13 #endif
14
15 #include <linux/fs.h>
16 #include <linux/obd_class.h>
17 #include <linux/lustre_log.h>
18 #include <portals/list.h>
19 #include "llog_internal.h"
20
21 static int str2logid(struct llog_logid *logid, char *str, int len)
22 {
23         char *start, *end, *endp;
24
25         start = str;
26         if (*start != '#')
27                 RETURN(-EINVAL);
28
29         start++;
30         if (start - str >= len - 1)
31                 RETURN(-EINVAL);
32         end = strchr(start, '#');
33         if (end == NULL || end == start)
34                 RETURN(-EINVAL);
35
36         *end = '\0';
37         logid->lgl_oid = simple_strtoull(start, &endp, 0);
38         if (endp != end)
39                 RETURN(-EINVAL);
40
41         start = ++end;
42         if (start - str >= len - 1)
43                 RETURN(-EINVAL);
44         end = strchr(start, '#');
45         if (end == NULL || end == start)
46                 RETURN(-EINVAL);
47
48         *end = '\0';
49         logid->lgl_ogr = simple_strtoull(start, &endp, 0);
50         if (endp != end)
51                 RETURN(-EINVAL);
52
53         start = ++end;
54         if (start - str >= len - 1)
55                 RETURN(-EINVAL);
56         logid->lgl_ogen = simple_strtoul(start, &endp, 16);
57         if (*endp != '\0')
58                 RETURN(-EINVAL);
59
60         RETURN(0);
61 }
62
63 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
64                          void *data)
65 {
66         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
67         static int l, remains, from, to;
68         static char *out;
69         char *endp;
70         int cur_index, rc = 0;
71
72         cur_index = rec->lrh_index;
73
74         if (ioc_data && (ioc_data->ioc_inllen1)) {
75                 l = 0;
76                 remains = ioc_data->ioc_inllen4 +
77                         size_round(ioc_data->ioc_inllen1) +
78                         size_round(ioc_data->ioc_inllen2) +
79                         size_round(ioc_data->ioc_inllen3);
80                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
81                 if (*endp != '\0')
82                         RETURN(-EINVAL);
83                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
84                 if (*endp != '\0')
85                         RETURN(-EINVAL);
86                 ioc_data->ioc_inllen1 = 0;
87                 out = ioc_data->ioc_bulk;
88                 if (cur_index < from)
89                         RETURN(0);
90                 if (to > 0 && cur_index > to)
91                         RETURN(-LLOG_EEMPTY);
92         }
93         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
94                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
95                 struct llog_handle *log_handle;
96
97                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
98                         l = snprintf(out, remains, "[index]: %05d  [type]: "
99                                      "%02x  [len]: %04d failed\n",
100                                      cur_index, rec->lrh_type,
101                                      rec->lrh_len);
102                 }
103                 if (handle->lgh_ctxt == NULL)
104                         RETURN(-EOPNOTSUPP);
105                 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
106                 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
107                 llog_close(log_handle);
108         } else {
109                 switch (rec->lrh_type) {
110                 case OST_SZ_REC:
111                 case OST_RAID1_REC:
112                 case MDS_UNLINK_REC:
113                 case OBD_CFG_REC:
114                 case PTL_CFG_REC:
115                 case LLOG_HDR_MAGIC: {
116                          l = snprintf(out, remains, "[index]: %05d  [type]: "
117                                       "%02x  [len]: %04d ok\n",
118                                       cur_index, rec->lrh_type,
119                                       rec->lrh_len);
120                          out += l;
121                          remains -= l;
122                          if (remains <= 0) {
123                                 CERROR("no space to print log records\n");
124                                 RETURN(-LLOG_EEMPTY);
125                          }
126                          RETURN(0);
127                 }
128                 default: {
129                          l = snprintf(out, remains, "[index]: %05d  [type]: "
130                                       "%02x  [len]: %04d failed\n",
131                                       cur_index, rec->lrh_type,
132                                       rec->lrh_len);
133                          out += l;
134                          remains -= l;
135                          if (remains <= 0) {
136                                 CERROR("no space to print log records\n");
137                                 RETURN(-LLOG_EEMPTY);
138                          }
139                          RETURN(0);
140                 }
141                 }
142         }
143         RETURN(rc);
144 }
145
146 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
147                          void *data)
148 {
149         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
150         static int l, remains, from, to;
151         static char *out;
152         char *endp;
153         int cur_index;
154
155         if (ioc_data->ioc_inllen1) {
156                 l = 0;
157                 remains = ioc_data->ioc_inllen4 +
158                         size_round(ioc_data->ioc_inllen1) +
159                         size_round(ioc_data->ioc_inllen2) +
160                         size_round(ioc_data->ioc_inllen3);
161                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
162                 if (*endp != '\0')
163                         RETURN(-EINVAL);
164                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
165                 if (*endp != '\0')
166                         RETURN(-EINVAL);
167                 out = ioc_data->ioc_bulk;
168                 ioc_data->ioc_inllen1 = 0;
169         }
170
171         cur_index = rec->lrh_index;
172         if (cur_index < from)
173                 RETURN(0);
174         if (to > 0 && cur_index > to)
175                 RETURN(-LLOG_EEMPTY);
176
177         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
178                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
179                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
180                         CERROR("invalid record in catalog\n");
181                         RETURN(-EINVAL);
182                 }
183
184                 l = snprintf(out, remains,
185                              "[index]: %05d  [logid]: #"LPX64"#"LPX64"#%08x\n",
186                              cur_index, lir->lid_id.lgl_oid,
187                              lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
188         } else {
189                 l = snprintf(out, remains,
190                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
191                              cur_index, rec->lrh_type,
192                              rec->lrh_len);
193         }
194         out += l;
195         remains -= l;
196         if (remains <= 0) {
197                 CERROR("not enough space for print log records\n");
198                 RETURN(-LLOG_EEMPTY);
199         }
200
201         RETURN(0);
202 }
203 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
204 {
205         struct llog_handle *log;
206         int rc, index = 0;
207
208         down_write(&cat->lgh_lock);
209         rc = llog_cat_id2handle(cat, &log, logid);
210         if (rc) {
211                 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
212                        logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
213                 GOTO(out, rc = -ENOENT);
214         }
215
216         index = log->u.phd.phd_cookie.lgc_index;
217         LASSERT(index);
218         rc = llog_destroy(log);
219         if (rc) {
220                 CDEBUG(D_IOCTL, "cannot destroy log\n");
221                 GOTO(out, rc);
222         }
223         llog_cat_set_first_idx(cat, index);
224         rc = llog_cancel_rec(cat, index);
225 out:
226         llog_free_handle(log);
227         up_write(&cat->lgh_lock);
228         RETURN(rc);
229
230 }
231
232 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
233                           void *data)
234 {
235         struct  llog_logid_rec *lir = (struct llog_logid_rec*)rec;
236         int     rc;
237
238         if (rec->lrh_type != LLOG_LOGID_MAGIC)
239               return (-EINVAL);
240         rc = llog_remove_log(handle, &lir->lid_id);
241
242         RETURN(rc);
243 }
244
245
246 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
247 {
248         struct llog_logid logid;
249         int err = 0;
250         struct llog_handle *handle = NULL;
251
252         if (*data->ioc_inlbuf1 == '#') {
253                 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
254                 if (err)
255                         GOTO(out, err);
256                 err = llog_create(ctxt, &handle, &logid, NULL);
257                 if (err)
258                         GOTO(out, err);
259         } else if (*data->ioc_inlbuf1 == '$') {
260                 char *name = data->ioc_inlbuf1 + 1;
261                 err = llog_create(ctxt, &handle, NULL, name);
262                 if (err)
263                         GOTO(out, err);
264         } else {
265                 GOTO(out, err = -EINVAL);
266         }
267
268         err = llog_init_handle(handle, 0, NULL);
269         if (err)
270                 GOTO(out_close, err = -ENOENT);
271
272         switch (cmd) {
273         case OBD_IOC_LLOG_INFO: {
274                 int l;
275                 int remains = data->ioc_inllen2 +
276                         size_round(data->ioc_inllen1);
277                 char *out = data->ioc_bulk;
278
279                 l = snprintf(out, remains,
280                              "logid:            #"LPX64"#"LPX64"#%08x\n"
281                              "flags:            %x (%s)\n"
282                              "records count:    %d\n"
283                              "last index:       %d\n",
284                              handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
285                              handle->lgh_id.lgl_ogen,
286                              handle->lgh_hdr->llh_flags,
287                              handle->lgh_hdr->llh_flags &
288                              LLOG_F_IS_CAT ? "cat" : "plain",
289                              handle->lgh_hdr->llh_count,
290                              handle->lgh_last_idx);
291                 out += l;
292                 remains -= l;
293                 if (remains <= 0)
294                         CERROR("not enough space for log header info\n");
295
296                 GOTO(out_close, err);
297         }
298         case OBD_IOC_LLOG_CHECK: {
299                 LASSERT(data->ioc_inllen1);
300                 err = llog_process(handle, llog_check_cb, data, NULL);
301                 if (err == -LLOG_EEMPTY)
302                         err = 0;
303                 GOTO(out_close, err);
304         }
305
306         case OBD_IOC_LLOG_PRINT: {
307                 LASSERT(data->ioc_inllen1);
308                 err = llog_process(handle, llog_print_cb, data, NULL);
309                 if (err == -LLOG_EEMPTY)
310                         err = 0;
311
312                 GOTO(out_close, err);
313         }
314         case OBD_IOC_LLOG_CANCEL: {
315                 struct llog_cookie cookie;
316                 struct llog_logid plain;
317                 char *endp;
318
319                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
320                 if (*endp != '\0')
321                         GOTO(out_close, err = -EINVAL);
322
323                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
324                         down_write(&handle->lgh_lock);
325                         err = llog_cancel_rec(handle, cookie.lgc_index);
326                         up_write(&handle->lgh_lock);
327                         GOTO(out_close, err);
328                 }
329
330                 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
331                 if (err)
332                         GOTO(out_close, err);
333                 cookie.lgc_lgl = plain;
334
335                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
336                         GOTO(out_close, err = -EINVAL);
337
338                 err = llog_cat_cancel_records(handle, 1, &cookie);
339                 GOTO(out_close, err);
340         }
341         case OBD_IOC_LLOG_REMOVE: {
342                 struct llog_logid plain;
343
344                 if (handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_PLAIN)) {
345                         err = llog_destroy(handle);
346                         if (!err)
347                                 llog_free_handle(handle);
348                         GOTO(out, err);
349                 }
350
351                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
352                         GOTO(out_close, err = -EINVAL);
353
354                 if (data->ioc_inlbuf2) {
355                         /*remove indicate log from the catalog*/
356                         err = str2logid(&plain, data->ioc_inlbuf2,
357                                         data->ioc_inllen2);
358                         if (err)
359                                 GOTO(out_close, err);
360                         err = llog_remove_log(handle, &plain);
361                 } else {
362                         /*remove all the log of the catalog*/
363                         llog_process(handle, llog_delete_cb, NULL, NULL);
364                 }
365                 GOTO(out_close, err);
366         }
367         }
368
369 out_close:
370         if (handle->lgh_hdr &&
371             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
372                 llog_cat_put(handle);
373         else
374                 llog_close(handle);
375 out:
376         RETURN(err);
377 }
378 EXPORT_SYMBOL(llog_ioctl);
379
380 int llog_catlog_list(struct obd_device *obd, int count,
381                      struct obd_ioctl_data *data)
382 {
383         int size, i;
384         struct llog_catid *idarray;
385         struct llog_logid *id;
386         char name[32] = CATLIST;
387         char *out;
388         int l, remains, rc = 0;
389
390         size = sizeof(*idarray) * count;
391
392         OBD_ALLOC(idarray, size);
393         if (!idarray)
394                 RETURN(-ENOMEM);
395         memset(idarray, 0, size);
396
397         rc = llog_get_cat_list(obd, obd, name, count, idarray);
398         if (rc) {
399                 OBD_FREE(idarray, size);
400                 RETURN(rc);
401         }
402
403         out = data->ioc_bulk;
404         remains = data->ioc_inllen1;
405         for (i = 0; i < count; i++) {
406                 id = &idarray[i].lci_logid;
407                 l = snprintf(out, remains,
408                              "catalog log: #"LPX64"#"LPX64"#%08x\n",
409                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
410                 out += l;
411                 remains -= l;
412                 if (remains <= 0) {
413                         CWARN("not enough memory for catlog list\n");
414                         break;
415                 }
416         }
417         OBD_FREE(idarray, size);
418         RETURN(0);
419
420 }
421 EXPORT_SYMBOL(llog_catlog_list);