Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LOG
34
35 #include <obd_class.h>
36 #include <lustre_ioctl.h>
37 #include <lustre_log.h>
38 #include "llog_internal.h"
39
40 static int str2logid(struct llog_logid *logid, char *str, int len)
41 {
42         char *start, *end, *endp;
43         __u64 id, seq;
44
45         ENTRY;
46         start = str;
47         if (start[0] == '[') {
48                 struct lu_fid *fid = &logid->lgl_oi.oi_fid;
49                 int num;
50
51                 fid_zero(fid);
52                 logid->lgl_ogen = 0;
53                 num = sscanf(start + 1, SFID, RFID(fid));
54                 CDEBUG(D_INFO, DFID":%x\n", PFID(fid), logid->lgl_ogen);
55                 RETURN(num == 3 && fid_is_sane(fid) ? 0 : -EINVAL);
56         }
57
58 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
59         /* logids used to be input in the form "#id#seq:ogen" before they
60          * were changed over to accept the FID [seq:oid:ver] format.
61          * This is accepted for compatibility reasons, though I doubt
62          * anyone is actually using this for anything. */
63         if (start[0] != '#')
64                 RETURN(-EINVAL);
65
66         start++;
67         if (start - str >= len - 1)
68                 RETURN(-EINVAL);
69         end = strchr(start, '#');
70         if (end == NULL || end == start)
71                 RETURN(-EINVAL);
72
73         *end = '\0';
74         id = simple_strtoull(start, &endp, 0);
75         if (endp != end)
76                 RETURN(-EINVAL);
77
78         start = ++end;
79         if (start - str >= len - 1)
80                 RETURN(-EINVAL);
81         end = strchr(start, '#');
82         if (end == NULL || end == start)
83                 RETURN(-EINVAL);
84
85         *end = '\0';
86         seq = simple_strtoull(start, &endp, 0);
87         if (endp != end)
88                 RETURN(-EINVAL);
89
90         ostid_set_seq(&logid->lgl_oi, seq);
91         ostid_set_id(&logid->lgl_oi, id);
92
93         start = ++end;
94         if (start - str >= len - 1)
95                 RETURN(-EINVAL);
96         logid->lgl_ogen = simple_strtoul(start, &endp, 16);
97         if (*endp != '\0')
98                 RETURN(-EINVAL);
99
100         RETURN(0);
101 #else
102         RETURN(-EINVAL);
103 #endif
104 }
105
106 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
107                          struct llog_rec_hdr *rec, void *data)
108 {
109         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
110         static int l, remains;
111         static long from, to;
112         static char *out;
113         char *endp;
114         int cur_index, rc = 0;
115
116         ENTRY;
117
118         if (ioc_data && ioc_data->ioc_inllen1 > 0) {
119                 l = 0;
120                 remains = ioc_data->ioc_inllen4 +
121                         cfs_size_round(ioc_data->ioc_inllen1) +
122                         cfs_size_round(ioc_data->ioc_inllen2) +
123                         cfs_size_round(ioc_data->ioc_inllen3);
124                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
125                 if (*endp != '\0')
126                         RETURN(-EINVAL);
127                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
128                 if (*endp != '\0')
129                         RETURN(-EINVAL);
130                 ioc_data->ioc_inllen1 = 0;
131                 out = ioc_data->ioc_bulk;
132         }
133
134         cur_index = rec->lrh_index;
135         if (cur_index < from)
136                 RETURN(0);
137         if (to > 0 && cur_index > to)
138                 RETURN(-LLOG_EEMPTY);
139
140         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
141                 struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
142                 struct llog_handle      *loghandle;
143
144                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
145                         l = snprintf(out, remains, "[index]: %05d  [type]: "
146                                      "%02x  [len]: %04d failed\n",
147                                      cur_index, rec->lrh_type,
148                                      rec->lrh_len);
149                 }
150                 if (handle->lgh_ctxt == NULL)
151                         RETURN(-EOPNOTSUPP);
152                 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
153                 if (rc) {
154                         CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
155                                PFID(&lir->lid_id.lgl_oi.oi_fid),
156                                lir->lid_id.lgl_ogen);
157                         RETURN(rc);
158                 }
159                 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
160                 llog_handle_put(loghandle);
161         } else {
162                 bool ok;
163
164                 switch (rec->lrh_type) {
165                 case OST_SZ_REC:
166                 case MDS_UNLINK_REC:
167                 case MDS_UNLINK64_REC:
168                 case MDS_SETATTR64_REC:
169                 case OBD_CFG_REC:
170                 case LLOG_GEN_REC:
171                 case LLOG_HDR_MAGIC:
172                         ok = true;
173                         break;
174                 default:
175                         ok = false;
176                 }
177
178                 l = snprintf(out, remains, "[index]: %05d  [type]: "
179                              "%02x  [len]: %04d %s\n",
180                              cur_index, rec->lrh_type, rec->lrh_len,
181                              ok ? "ok" : "failed");
182                 out += l;
183                 remains -= l;
184                 if (remains <= 0) {
185                         CERROR("%s: no space to print log records\n",
186                                handle->lgh_ctxt->loc_obd->obd_name);
187                         RETURN(-LLOG_EEMPTY);
188                 }
189         }
190         RETURN(rc);
191 }
192
193 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
194                          struct llog_rec_hdr *rec, void *data)
195 {
196         struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
197         static int l, remains;
198         static long from, to;
199         static char *out;
200         char *endp;
201         int cur_index;
202
203         ENTRY;
204         if (ioc_data != NULL && ioc_data->ioc_inllen1 > 0) {
205                 l = 0;
206                 remains = ioc_data->ioc_inllen4 +
207                         cfs_size_round(ioc_data->ioc_inllen1) +
208                         cfs_size_round(ioc_data->ioc_inllen2) +
209                         cfs_size_round(ioc_data->ioc_inllen3);
210                 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
211                 if (*endp != '\0')
212                         RETURN(-EINVAL);
213                 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
214                 if (*endp != '\0')
215                         RETURN(-EINVAL);
216                 out = ioc_data->ioc_bulk;
217                 ioc_data->ioc_inllen1 = 0;
218         }
219
220         cur_index = rec->lrh_index;
221         if (cur_index < from)
222                 RETURN(0);
223         if (to > 0 && cur_index > to)
224                 RETURN(-LLOG_EEMPTY);
225
226         if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
227                 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
228
229                 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
230                         CERROR("invalid record in catalog\n");
231                         RETURN(-EINVAL);
232                 }
233
234                 l = snprintf(out, remains,
235                              "[index]: %05d  [logid]: "DFID":%x\n",
236                              cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
237                              lir->lid_id.lgl_ogen);
238         } else if (rec->lrh_type == OBD_CFG_REC) {
239                 int rc;
240
241                 rc = class_config_yaml_output(rec, out, remains);
242                 if (rc < 0)
243                         RETURN(rc);
244                 l = rc;
245         } else {
246                 l = snprintf(out, remains,
247                              "[index]: %05d  [type]: %02x  [len]: %04d\n",
248                              cur_index, rec->lrh_type, rec->lrh_len);
249         }
250         out += l;
251         remains -= l;
252         if (remains <= 0) {
253                 CERROR("not enough space for print log records\n");
254                 RETURN(-LLOG_EEMPTY);
255         }
256
257         RETURN(0);
258 }
259 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
260                            struct llog_logid *logid)
261 {
262         struct llog_handle      *log;
263         int                      rc;
264
265         ENTRY;
266
267         rc = llog_cat_id2handle(env, cat, &log, logid);
268         if (rc) {
269                 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
270                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
271                 RETURN(-ENOENT);
272         }
273
274         rc = llog_destroy(env, log);
275         if (rc) {
276                 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
277                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
278                 GOTO(out, rc);
279         }
280         llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
281 out:
282         llog_handle_put(log);
283         RETURN(rc);
284
285 }
286
287 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
288                           struct llog_rec_hdr *rec, void *data)
289 {
290         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
291         int                      rc;
292
293         ENTRY;
294         if (rec->lrh_type != LLOG_LOGID_MAGIC)
295                 RETURN(-EINVAL);
296         rc = llog_remove_log(env, handle, &lir->lid_id);
297
298         RETURN(rc);
299 }
300
301
302 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
303                struct obd_ioctl_data *data)
304 {
305         struct llog_logid        logid;
306         int                      rc = 0;
307         struct llog_handle      *handle = NULL;
308         char *logname;
309
310         ENTRY;
311
312         logname = data->ioc_inlbuf1;
313         if (logname[0] == '#' || logname[0] == '[') {
314                 rc = str2logid(&logid, logname, data->ioc_inllen1);
315                 if (rc)
316                         RETURN(rc);
317                 rc = llog_open(env, ctxt, &handle, &logid, NULL,
318                                LLOG_OPEN_EXISTS);
319                 if (rc)
320                         RETURN(rc);
321         } else if (logname[0] == '$' || isalpha(logname[0])) {
322                 if (logname[0] == '$')
323                         logname++;
324
325                 rc = llog_open(env, ctxt, &handle, NULL, logname,
326                                LLOG_OPEN_EXISTS);
327                 if (rc)
328                         RETURN(rc);
329         } else {
330                 RETURN(-EINVAL);
331         }
332
333         rc = llog_init_handle(env, handle, 0, NULL);
334         if (rc)
335                 GOTO(out_close, rc = -ENOENT);
336
337         switch (cmd) {
338         case OBD_IOC_LLOG_INFO: {
339                 int      l;
340                 int      remains = data->ioc_inllen2 +
341                                    cfs_size_round(data->ioc_inllen1);
342                 char    *out = data->ioc_bulk;
343
344                 l = snprintf(out, remains,
345                              "logid:            "DFID":%x\n"
346                              "flags:            %x (%s)\n"
347                              "records_count:    %d\n"
348                              "last_index:       %d\n",
349                              PFID(&handle->lgh_id.lgl_oi.oi_fid),
350                              handle->lgh_id.lgl_ogen,
351                              handle->lgh_hdr->llh_flags,
352                              handle->lgh_hdr->llh_flags &
353                                 LLOG_F_IS_CAT ? "cat" : "plain",
354                              handle->lgh_hdr->llh_count,
355                              handle->lgh_last_idx);
356                 out += l;
357                 remains -= l;
358                 if (remains <= 0) {
359                         CERROR("%s: not enough space for log header info\n",
360                                ctxt->loc_obd->obd_name);
361                         rc = -ENOSPC;
362                 }
363                 break;
364         }
365         case OBD_IOC_LLOG_CHECK:
366                 LASSERT(data->ioc_inllen1 > 0);
367                 rc = llog_process(env, handle, llog_check_cb, data, NULL);
368                 if (rc == -LLOG_EEMPTY)
369                         rc = 0;
370                 else if (rc)
371                         GOTO(out_close, rc);
372                 break;
373         case OBD_IOC_LLOG_PRINT:
374                 LASSERT(data->ioc_inllen1 > 0);
375                 rc = llog_process(env, handle, llog_print_cb, data, NULL);
376                 if (rc == -LLOG_EEMPTY)
377                         rc = 0;
378                 else if (rc)
379                         GOTO(out_close, rc);
380                 break;
381         case OBD_IOC_LLOG_CANCEL: {
382                 struct llog_cookie cookie;
383                 struct llog_logid plain;
384                 char *endp;
385
386                 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
387                 if (*endp != '\0')
388                         GOTO(out_close, rc = -EINVAL);
389
390                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
391                         rc = llog_cancel_rec(env, handle, cookie.lgc_index);
392                         GOTO(out_close, rc);
393                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
394                         GOTO(out_close, rc = -EINVAL);
395                 }
396
397                 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
398                         GOTO(out_close, rc = -ENOTTY);
399
400                 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
401                 if (rc)
402                         GOTO(out_close, rc);
403                 cookie.lgc_lgl = plain;
404                 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
405                 if (rc)
406                         GOTO(out_close, rc);
407                 break;
408         }
409         case OBD_IOC_LLOG_REMOVE: {
410                 struct llog_logid plain;
411
412                 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
413                         rc = llog_destroy(env, handle);
414                         GOTO(out_close, rc);
415                 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
416                         GOTO(out_close, rc = -EINVAL);
417                 }
418
419                 if (data->ioc_inlbuf2 > 0) {
420                         /* remove indicate log from the catalog */
421                         rc = str2logid(&plain, data->ioc_inlbuf2,
422                                        data->ioc_inllen2);
423                         if (rc)
424                                 GOTO(out_close, rc);
425                         rc = llog_remove_log(env, handle, &plain);
426                 } else {
427                         /* remove all the log of the catalog */
428                         rc = llog_process(env, handle, llog_delete_cb, NULL,
429                                           NULL);
430                         if (rc)
431                                 GOTO(out_close, rc);
432                 }
433                 break;
434         }
435         default:
436                 CERROR("%s: Unknown ioctl cmd %#x\n",
437                        ctxt->loc_obd->obd_name, cmd);
438                 GOTO(out_close, rc = -ENOTTY);
439         }
440
441 out_close:
442         if (handle->lgh_hdr &&
443             handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
444                 llog_cat_close(env, handle);
445         else
446                 llog_close(env, handle);
447         RETURN(rc);
448 }
449 EXPORT_SYMBOL(llog_ioctl);
450
451 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
452                       int count, struct obd_ioctl_data *data,
453                       const struct lu_fid *fid)
454 {
455         int                      size, i;
456         struct llog_catid       *idarray;
457         struct llog_logid       *id;
458         char                    *out;
459         int                      l, remains, rc = 0;
460
461         ENTRY;
462
463         if (count == 0) { /* get total number of logs */
464                 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
465                 if (rc < 0)
466                         RETURN(rc);
467                 count = rc;
468         }
469
470         size = sizeof(*idarray) * count;
471
472         OBD_ALLOC_LARGE(idarray, size);
473         if (!idarray)
474                 RETURN(-ENOMEM);
475
476         rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
477         if (rc)
478                 GOTO(out, rc);
479
480         out = data->ioc_bulk;
481         remains = data->ioc_inllen1;
482         for (i = 0; i < count; i++) {
483                 id = &idarray[i].lci_logid;
484                 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
485                              PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
486                 out += l;
487                 remains -= l;
488                 if (remains <= 0)
489                         break;
490         }
491 out:
492         OBD_FREE_LARGE(idarray, size);
493         RETURN(rc);
494 }
495 EXPORT_SYMBOL(llog_catalog_list);