Whamcloud - gitweb
LU-1818 quota: en/disable quota enforcement via conf_param
[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         cfs_down_write(&cat->lgh_lock);
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         llog_cat_set_first_idx(cat, index);
260         rc = llog_cancel_rec(env, cat, index);
261 out:
262         llog_free_handle(log);
263         cfs_up_write(&cat->lgh_lock);
264         RETURN(rc);
265
266 }
267
268 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
269                           struct llog_rec_hdr *rec, void *data)
270 {
271         struct  llog_logid_rec *lir = (struct llog_logid_rec*)rec;
272         int     rc;
273
274         ENTRY;
275         if (rec->lrh_type != LLOG_LOGID_MAGIC)
276               RETURN (-EINVAL);
277         rc = llog_remove_log(env, handle, &lir->lid_id);
278
279         RETURN(rc);
280 }
281
282
283 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
284 {
285         struct llog_logid logid;
286         int err = 0;
287         struct llog_handle *handle = NULL;
288
289         ENTRY;
290         if (*data->ioc_inlbuf1 == '#') {
291                 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
292                 if (err)
293                         GOTO(out, err);
294                 err = llog_create(NULL, ctxt, &handle, &logid, NULL);
295                 if (err)
296                         GOTO(out, err);
297         } else if (*data->ioc_inlbuf1 == '$') {
298                 char *name = data->ioc_inlbuf1 + 1;
299                 err = llog_create(NULL, ctxt, &handle, NULL, name);
300                 if (err)
301                         GOTO(out, err);
302         } else {
303                 GOTO(out, err = -EINVAL);
304         }
305
306         err = llog_init_handle(NULL, handle, 0, NULL);
307         if (err)
308                 GOTO(out_close, err = -ENOENT);
309
310         switch (cmd) {
311         case OBD_IOC_LLOG_INFO: {
312                 int l;
313                 int remains = data->ioc_inllen2 +
314                               cfs_size_round(data->ioc_inllen1);
315                 char *out = data->ioc_bulk;
316
317                 l = snprintf(out, remains,
318                              "logid:            #"LPX64"#"LPX64"#%08x\n"
319                              "flags:            %x (%s)\n"
320                              "records count:    %d\n"
321                              "last index:       %d\n",
322                              handle->lgh_id.lgl_oid, handle->lgh_id.lgl_oseq,
323                              handle->lgh_id.lgl_ogen,
324                              handle->lgh_hdr->llh_flags,
325                              handle->lgh_hdr->llh_flags &
326                              LLOG_F_IS_CAT ? "cat" : "plain",
327                              handle->lgh_hdr->llh_count,
328                              handle->lgh_last_idx);
329                 out += l;
330                 remains -= l;
331                 if (remains <= 0)
332                         CERROR("not enough space for log header info\n");
333
334                 GOTO(out_close, err);
335         }
336         case OBD_IOC_LLOG_CHECK: {
337                 LASSERT(data->ioc_inllen1);
338                 err = llog_process(NULL, handle, llog_check_cb, data, NULL);
339                 if (err == -LLOG_EEMPTY)
340                         err = 0;
341                 GOTO(out_close, err);
342         }
343
344         case OBD_IOC_LLOG_PRINT: {
345                 LASSERT(data->ioc_inllen1);
346                 err = llog_process(NULL, handle, class_config_dump_handler,
347                                    data, NULL);
348                 if (err == -LLOG_EEMPTY)
349                         err = 0;
350                 else
351                         err = llog_process(NULL, handle, llog_print_cb, data,
352                                            NULL);
353
354                 GOTO(out_close, err);
355         }
356         case OBD_IOC_LLOG_CANCEL: {
357                 struct llog_cookie cookie;
358                 struct llog_logid plain;
359                 char *endp;
360
361                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
362                 if (*endp != '\0')
363                         GOTO(out_close, err = -EINVAL);
364
365                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
366                         cfs_down_write(&handle->lgh_lock);
367                         err = llog_cancel_rec(NULL, handle, cookie.lgc_index);
368                         cfs_up_write(&handle->lgh_lock);
369                         GOTO(out_close, err);
370                 }
371
372                 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
373                 if (err)
374                         GOTO(out_close, err);
375                 cookie.lgc_lgl = plain;
376
377                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
378                         GOTO(out_close, err = -EINVAL);
379
380                 err = llog_cat_cancel_records(NULL, handle, 1, &cookie);
381                 GOTO(out_close, err);
382         }
383         case OBD_IOC_LLOG_REMOVE: {
384                 struct llog_logid plain;
385
386                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
387                         err = llog_destroy(NULL, handle);
388                         if (!err)
389                                 llog_free_handle(handle);
390                         GOTO(out, err);
391                 }
392
393                 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
394                         GOTO(out_close, err = -EINVAL);
395
396                 if (data->ioc_inlbuf2) {
397                         /*remove indicate log from the catalog*/
398                         err = str2logid(&plain, data->ioc_inlbuf2,
399                                         data->ioc_inllen2);
400                         if (err)
401                                 GOTO(out_close, err);
402                         err = llog_remove_log(NULL, handle, &plain);
403                 } else {
404                         /* remove all the log of the catalog */
405                         llog_process(NULL, handle, llog_delete_cb, NULL, NULL);
406                 }
407                 GOTO(out_close, err);
408         }
409         }
410
411 out_close:
412         if (handle->lgh_hdr &&
413             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
414                 llog_cat_put(NULL, handle);
415         else
416                 llog_close(NULL, handle);
417 out:
418         RETURN(err);
419 }
420 EXPORT_SYMBOL(llog_ioctl);
421
422 int llog_catalog_list(struct obd_device *obd, int count,
423                       struct obd_ioctl_data *data)
424 {
425         int size, i;
426         struct llog_catid *idarray;
427         struct llog_logid *id;
428         char name[32] = CATLIST;
429         char *out;
430         int l, remains, rc = 0;
431
432         ENTRY;
433         size = sizeof(*idarray) * count;
434
435         OBD_ALLOC_LARGE(idarray, size);
436         if (!idarray)
437                 RETURN(-ENOMEM);
438
439         cfs_mutex_lock(&obd->obd_olg.olg_cat_processing);
440         rc = llog_get_cat_list(obd, name, 0, count, idarray);
441         if (rc)
442                 GOTO(out, rc);
443
444         out = data->ioc_bulk;
445         remains = data->ioc_inllen1;
446         for (i = 0; i < count; i++) {
447                 id = &idarray[i].lci_logid;
448                 l = snprintf(out, remains,
449                              "catalog log: #"LPX64"#"LPX64"#%08x\n",
450                              id->lgl_oid, id->lgl_oseq, id->lgl_ogen);
451                 out += l;
452                 remains -= l;
453                 if (remains <= 0) {
454                         CWARN("not enough memory for catlog list\n");
455                         break;
456                 }
457         }
458 out:
459         /* release semaphore */
460         cfs_mutex_unlock(&obd->obd_olg.olg_cat_processing);
461
462         OBD_FREE_LARGE(idarray, size);
463         RETURN(rc);
464
465 }
466 EXPORT_SYMBOL(llog_catalog_list);