Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26
27 /* for O_DIRECTORY */
28 #define _GNU_SOURCE
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stddef.h>
34 #include <sys/ioctl.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <dirent.h>
39 #include <stdarg.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <linux/types.h>
43 #include <linux/unistd.h>
44
45 #include <liblustre.h>
46 #include <linux/obd.h>
47 #include <linux/lustre_lib.h>
48 #include <lustre/lustre_user.h>
49 #include <linux/obd_lov.h>
50
51 #include <portals/ptlctl.h>
52
53 static void err_msg(char *fmt, ...)
54 {
55         va_list args;
56         int tmp_errno = errno;
57
58         va_start(args, fmt);
59         vfprintf(stderr, fmt, args);
60         va_end(args);
61         fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
62 }
63
64 int llapi_file_create(char *name, long stripe_size, int stripe_offset,
65                       int stripe_count, int stripe_pattern)
66 {
67         struct lov_user_md lum = { 0 };
68         int fd, rc = 0;
69
70         /*  Initialize IOCTL striping pattern structure  */
71         lum.lmm_magic = LOV_USER_MAGIC;
72         lum.lmm_pattern = stripe_pattern;
73         lum.lmm_stripe_size = stripe_size;
74         lum.lmm_stripe_count = stripe_count;
75         lum.lmm_stripe_offset = stripe_offset;
76
77         fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
78         if (errno == EISDIR)
79                 fd = open(name, O_DIRECTORY | O_RDONLY);
80
81         if (fd < 0) {
82                 err_msg("unable to open '%s'",name);
83                 rc = -errno;
84                 return rc;
85         }
86
87         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
88                 char *errmsg = "stripe already set";
89                 if (errno != EEXIST && errno != EALREADY)
90                         errmsg = strerror(errno);
91
92                 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
93                         name, fd, errmsg);
94                 rc = -errno;
95         }
96         if (close(fd) < 0) {
97                 err_msg("error on close for '%s' (%d)", name, fd);
98                 if (rc == 0)
99                         rc = -errno;
100         }
101         return rc;
102 }
103
104 int op_create_dir(char *name, int stripe_count)
105 {
106         struct  ll_user_mkdir_stripe lums = { 0 };
107         int fd, rc = 0;
108         char *dot = ".";
109         char *dirname;
110
111         dirname = rindex(name, '/');
112         if (!dirname) {
113                 dirname = "name";
114                 name = dot;
115         } else {
116                 *dirname = 0;
117                 dirname++;
118         }
119         lums.lums_nstripes = stripe_count;
120         lums.lums_namelen = strlen(dirname);
121         lums.lums_name = dirname;
122         /* XXX: Probably users might want to specify permissions as well? */
123         lums.lums_mode = 0755;
124                 
125         fd = open(name, O_RDONLY|O_DIRECTORY);
126         if (name != dot)
127                 *(dirname-1) = '/';
128         if (fd < 0) {
129                 err_msg("unable to open '%s'",name);
130                 rc = -errno;
131                 return rc;
132         }
133         
134         if (ioctl(fd, LL_IOC_MDC_MKDIRSTRIPE, &lums)) {
135                 char *errmsg = strerror(errno);
136
137                 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
138                         name, fd, errmsg);
139                 rc = -errno;
140         }
141         if (close(fd) < 0) {
142                 err_msg("error on close for '%s' (%d)", name, fd);
143                 if (rc == 0)
144                         rc = -errno;
145         }
146         return rc;
147 }
148
149 /* short term backwards compat only */
150 int op_create_file(char *name, long stripe_size, int stripe_offset,
151                    int stripe_count)
152 {
153         return llapi_file_create(name, stripe_size, stripe_offset,
154                                  stripe_count, 0);
155 }
156
157 struct find_param {
158         int     recursive;
159         int     verbose;
160         int     quiet;
161         struct  obd_uuid        *obduuid;
162         int     lumlen;
163         struct  lov_user_md     *lum;
164         int     got_uuids;
165         int     obdindex;
166 };
167
168 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
169 #define MAX_LOV_UUID_COUNT      1000
170 #define OBD_NOT_FOUND           (-1)
171
172 static int prepare_find(struct find_param *param)
173 {
174         param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
175         if ((param->lum = malloc(param->lumlen)) == NULL) {
176                 err_msg("unable to allocate %d bytes of memory for ioctl",
177                         param->lumlen);
178                 return ENOMEM;
179         }
180
181         param->got_uuids = 0;
182         param->obdindex = OBD_NOT_FOUND;
183
184         return 0;
185 }
186
187 static void cleanup_find(struct find_param *param)
188 {
189         if (param->obduuid)
190                 free(param->obduuid);
191         if (param->lum)
192                 free(param->lum);
193 }
194
195 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
196 {
197         struct obd_ioctl_data data = { 0, };
198         struct lov_desc desc = { 0, };
199         char *buf = NULL;
200         int max_ost_count, rc;
201
202         max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
203                          size_round(sizeof(desc))) / sizeof(*uuidp);
204         if (max_ost_count > *ost_count)
205                 max_ost_count = *ost_count;
206
207         data.ioc_inllen1 = sizeof(desc);
208         data.ioc_inlbuf1 = (char *)&desc;
209         data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
210         data.ioc_inlbuf2 = (char *)uuidp;
211
212         desc.ld_tgt_count = max_ost_count;
213
214         if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
215                 fprintf(stderr, "internal buffer error packing\n");
216                 rc = EINVAL;
217                 goto out;
218         }
219
220         rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
221         if (rc) {
222                 err_msg("error getting LOV config");
223                 rc = errno;
224                 goto out;
225         }
226
227         if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
228                 fprintf(stderr, "invalid reply from ioctl");
229                 rc = EINVAL;
230                 goto out;
231         }
232
233         *ost_count = desc.ld_tgt_count;
234 out:
235         free(buf);
236
237         return 0;
238 }
239
240 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
241 {
242         struct obd_uuid uuids[1024], *uuidp;
243         int obdcount = 1024;
244         int rc, i;
245
246         param->got_uuids = 1;
247
248         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
249         if (rc != 0)
250                 return (param->obduuid ? rc : 0);
251
252         if (obdcount == 0)
253                 return 0;
254
255         if (param->obduuid) {
256                 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
257                         if (strncmp(param->obduuid->uuid, uuidp->uuid,
258                                     sizeof(*uuidp)) == 0) {
259                                 param->obdindex = i;
260                                 break;
261                         }
262                 }
263                 if (param->obdindex == OBD_NOT_FOUND) {
264                         printf("unknown obduuid: %s\n", param->obduuid->uuid);
265                         return EINVAL;
266                 }
267         } else if (!param->quiet) {
268                 printf("OBDS:\n");
269                 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++)
270                         printf("%4d: %s\n", i, uuidp->uuid);
271         }
272
273         return 0;
274 }
275
276 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
277                           int obdindex, int quiet, int header, int body)
278 {
279         int i, obdstripe = 0;
280
281         if (obdindex != OBD_NOT_FOUND) {
282                 for (i = 0; i < lum->lmm_stripe_count; i++) {
283                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
284                                 printf("%s/%s\n", dname, fname);
285                                 obdstripe = 1;
286                                 break;
287                         }
288                 }
289         } else if (!quiet) {
290                 printf("%s/%s\n", dname, fname);
291                 obdstripe = 1;
292         }
293
294         /* if it's a directory */
295         if (*fname == '\0') {
296                 if (header && (obdstripe == 1)) {
297                         printf("count: %d, size: %d, offset: %d\n\n",
298                                lum->lmm_stripe_count, lum->lmm_stripe_size,
299                                (short int)lum->lmm_stripe_offset);
300                 }
301                 return;
302         }
303
304         if (header && (obdstripe == 1)) {
305                 printf("lmm_magic:          0x%08X\n",  lum->lmm_magic);
306                 printf("lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
307                 printf("lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
308                 printf("lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
309                 printf("lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
310                 printf("lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
311         }
312
313         if (body) {
314                 if ((!quiet) && (obdstripe == 1))
315                         printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
316
317                 for (i = 0; i < lum->lmm_stripe_count; i++) {
318                         int idx = lum->lmm_objects[i].l_ost_idx;
319                         long long oid = lum->lmm_objects[i].l_object_id;
320                         long long gr = lum->lmm_objects[i].l_object_gr;
321                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
322                                 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
323                                        idx, oid, oid, gr,
324                                        obdindex == idx ? " *" : "");
325                 }
326                 printf("\n");
327         }
328 }
329
330 void llapi_lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
331 {
332         switch(*(__u32 *)param->lum) { /* lum->lmm_magic */
333         case LOV_USER_MAGIC_V1:
334                 lov_dump_user_lmm_v1(param->lum, dname, fname, param->obdindex,
335                                      param->quiet, param->verbose,
336                                      (param->verbose || !param->obduuid));
337                 break;
338         default:
339                 printf("unknown lmm_magic:  0x%08X\n", *(__u32 *)param->lum);
340                 return;
341         }
342 }
343
344 int llapi_file_get_stripe(char *path, struct lov_user_md *lum)
345 {
346         char *dname, *fname;
347         int fd, rc = 0;
348
349         fname = strrchr(path, '/');
350
351         /* It should be a file (or other non-directory) */
352         if (fname == NULL) {
353                 dname = (char *)malloc(2);
354                 if (dname == NULL)
355                         return ENOMEM;
356                 strcpy(dname, ".");
357                 fname = path;
358         } else {
359                 dname = (char *)malloc(fname - path + 1);
360                 if (dname == NULL)
361                         return ENOMEM;
362                 strncpy(dname, path, fname - path);
363                 dname[fname - path + 1] = '\0';
364                 fname++;
365         }
366
367         if ((fd = open(dname, O_RDONLY)) == -1) {
368                 free(dname);
369                 return errno;
370         }
371
372         strncpy((char *)lum, fname, sizeof(*lum));
373         if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
374                 close(fd);
375                 free(dname);
376                 return errno;
377         }
378
379         if (close(fd) == -1)
380                 rc = errno;
381
382         free(dname);
383
384         return rc;
385 }
386
387 /* short term backwards compat only */
388 int op_get_file_stripe(char *path, struct lov_user_md *lum)
389 {
390         return llapi_file_get_stripe(path, lum);
391 }
392
393 static int process_file(DIR *dir, char *dname, char *fname,
394                         struct find_param *param)
395 {
396         int rc;
397
398         strncpy((char *)param->lum, fname, param->lumlen);
399
400         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->lum);
401         if (rc) {
402                 if (errno == ENODATA) {
403                         if (!param->obduuid && !param->quiet)
404                                 fprintf(stderr,
405                                         "%s/%s has no stripe info\n",
406                                         dname, fname);
407                         rc = 0;
408                 } else if (errno == EISDIR) {
409                         fprintf(stderr, "process_file on directory %s/%s!\n",
410                                 dname, fname);
411                         /* add fname to directory list; */
412                         rc = errno;
413                 } else {
414                         err_msg("IOC_MDC_GETSTRIPE ioctl failed");
415                         rc = errno;
416                 }
417                 return rc;
418         }
419
420         llapi_lov_dump_user_lmm(param, dname, fname);
421
422         return 0;
423 }
424
425 /* some 64bit libcs implement readdir64() by calling sys_getdents().  the
426  * kernel's sys_getdents() doesn't return d_type.  */
427 unsigned char handle_dt_unknown(char *parent, char *entry)
428 {
429         char path[PATH_MAX + 1];
430         int fd, ret;
431
432         ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
433         if (ret >= PATH_MAX)
434                 return DT_UNKNOWN;
435
436         fd = open(path, O_DIRECTORY|O_RDONLY);
437         if (fd < 0) {
438                 if (errno == ENOTDIR)
439                         return DT_REG; /* kind of a lie */
440                 return DT_UNKNOWN;
441         }
442         close(fd);
443         return DT_DIR;
444 }
445
446 static int process_dir(DIR *dir, char *dname, struct find_param *param)
447 {
448         struct dirent64 *dirp;
449         DIR *subdir;
450         char path[1024];
451         int rc;
452
453         if (!param->got_uuids) {
454                 rc = setup_obd_uuids(dir, dname, param);
455                 if (rc)
456                         return rc;
457         }
458
459         /* retrieve dir's stripe info */
460         strncpy((char *)param->lum, dname, param->lumlen);
461         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->lum);
462         if (rc) {
463                 if (errno == ENODATA) {
464                         if (!param->obduuid && param->verbose)
465                                 printf("%s/%s has no stripe info\n", dname, "");
466                         rc = 0;
467                 } else {
468                         err_msg("IOC_MDC_GETSTRIPE ioctl failed");
469                         return errno;
470                 }
471         } else {
472                llapi_lov_dump_user_lmm(param, dname, "");
473         }
474
475         /* Handle the contents of the directory */
476         while ((dirp = readdir64(dir)) != NULL) {
477                 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
478                         continue;
479
480                 if (dirp->d_type == DT_UNKNOWN)
481                         dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
482
483                 switch (dirp->d_type) {
484                 case DT_UNKNOWN:
485                         err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
486                                 dirp->d_type);
487                         /* If we cared we could stat the file to determine
488                          * type and continue on here, but we don't since we
489                          * know d_type should be valid for lustre and this
490                          * tool only makes sense for lustre filesystems. */
491                         return EINVAL;
492                         break;
493                 case DT_DIR:
494                         if (!param->recursive)
495                                 break;
496                         strcpy(path, dname);
497                         strcat(path, "/");
498                         strcat(path, dirp->d_name);
499                         subdir = opendir(path);
500                         if (subdir == NULL) {
501                                 err_msg("\"%.40s\" opendir failed", path);
502                                 return errno;
503                         }
504                         rc = process_dir(subdir, path, param);
505                         closedir(subdir);
506                         if (rc)
507                                 return rc;
508                         break;
509                 case DT_REG:
510                         rc = process_file(dir, dname, dirp->d_name, param);
511                         if (rc)
512                                 return rc;
513                         break;
514                 default:
515                         break;
516                 }
517         }
518
519         return 0;
520 }
521
522 static int process_path(char *path, struct find_param *param)
523 {
524         char *fname, *dname;
525         DIR *dir;
526         int rc = 0;
527
528         fname = strrchr(path, '/');
529         if (fname != NULL && fname[1] == '\0') {
530                 /* Trailing '/', it must be a dir */
531                 *fname = '\0';
532                 dir = opendir(path);
533                 if (dir == NULL) {
534                         err_msg("\"%.40s\" opendir failed", path);
535                         rc = errno;
536                 } else {
537                         rc = process_dir(dir, path, param);
538                         closedir(dir);
539                 }
540         } else if ((dir = opendir(path)) != NULL) {
541                 /* No trailing '/', but it is still a dir */
542                 rc = process_dir(dir, path, param);
543                 closedir(dir);
544         } else {
545                 /* It must be a file (or other non-directory) */
546                 if (fname == NULL) {
547                         dname = ".";
548                         fname = path;
549                 } else {
550                         *fname = '\0';
551                         fname++;
552                         dname = path;
553                 }
554                 dir = opendir(dname);
555                 if (dir == NULL) {
556                         err_msg("\"%.40s\" opendir failed", dname);
557                         rc = errno;
558                 } else {
559                         if (!param->got_uuids)
560                                 rc = setup_obd_uuids(dir, dname, param);
561                         if (rc == 0)
562                                 rc = process_file(dir, dname, fname, param);
563                         closedir(dir);
564                 }
565         }
566
567         return rc;
568 }
569
570 int llapi_find(char *path, struct obd_uuid *obduuid, int recursive,
571                int verbose, int quiet)
572 {
573         struct find_param param;
574         int ret = 0;
575
576         memset(&param, 0, sizeof(param));
577         param.recursive = recursive;
578         param.verbose = verbose;
579         param.quiet = quiet;
580         if (obduuid) {
581                 param.obduuid = malloc(sizeof(*obduuid));
582                 if (param.obduuid == NULL) {
583                         ret = ENOMEM;
584                         goto out;
585                 }
586                 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
587         }
588
589         ret = prepare_find(&param);
590         if (ret)
591                 goto out;
592
593         process_path(path, &param);
594 out:
595         cleanup_find(&param);
596         return ret;
597 }
598
599 #define MAX_STRING_SIZE 128
600 #define DEVICES_LIST "/proc/fs/lustre/devices"
601
602 int llapi_target_check(int type_num, char **obd_type, char *dir)
603 {
604         char buf[MAX_STRING_SIZE];
605         FILE *fp = fopen(DEVICES_LIST, "r");
606         int rc = 0;
607         int i;
608
609         if (fp == NULL) {
610                 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
611                         strerror(rc =  errno));
612                 return rc;
613         }
614
615         while (fgets(buf, sizeof(buf), fp) != NULL) {
616                 char *obd_type_name = NULL;
617                 char *obd_name = NULL;
618                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
619                 char *bufl = rawbuf;
620                 char *bufp = buf;
621                 int max = sizeof(rawbuf);
622                 struct obd_ioctl_data datal;
623                 struct obd_statfs osfs_buffer;
624
625                 while(bufp[0] == ' ')
626                         ++bufp;
627
628                 for(i = 0; i < 3; i++) {
629                         obd_type_name = strsep(&bufp, " ");
630                 }
631                 obd_name = strsep(&bufp, " ");
632
633                 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
634
635                 memset(bufl, 0, sizeof(rawbuf));
636                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
637                 datal.ioc_plen1 = sizeof(osfs_buffer);
638
639                 for (i = 0; i < type_num; i++)
640                         if (strcmp(obd_type_name, obd_type[i]) == 0) {
641                                 datal.ioc_inlbuf1 = obd_name;
642                                 datal.ioc_inllen1 = strlen(obd_name) + 1;
643
644                                 obd_ioctl_pack(&datal,&bufl,max);
645
646                                 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,
647                                            bufl);
648
649                                 if (rc) {
650                                         fprintf(stderr, "error: check %s: %s\n",
651                                                 obd_name, strerror(rc = errno));
652                                 } else {
653                                         printf("%s active.\n",obd_name);
654                                 }
655                         }
656
657         }
658         fclose(fp);
659         return rc;
660 }
661
662 #undef MAX_STRING_SIZE
663
664 int llapi_catinfo(char *dir, char *keyword, char *node_name)
665 {
666         char raw[OBD_MAX_IOCTL_BUFFER];
667         char out[LLOG_CHUNK_SIZE];
668         char *buf = raw;
669         struct obd_ioctl_data data;
670         char key[30];
671         DIR *root;
672         int rc;
673
674         sprintf(key, "%s", keyword);
675         memset(raw, 0, sizeof(buf));
676         memset(out, 0, sizeof(out));
677         data.ioc_inlbuf1 = key;
678         data.ioc_inllen1 = strlen(key) + 1;
679         if (node_name) {
680                 data.ioc_inlbuf2 = node_name;
681                 data.ioc_inllen2 = strlen(node_name) + 1;
682         }
683         data.ioc_pbuf1 = out;
684         data.ioc_plen1 = sizeof(out);
685         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
686         if (rc)
687                 return rc;
688
689         root = opendir(dir);
690         if (root == NULL) {
691                 err_msg("open %s failed", dir);
692                 return errno;
693         }
694
695         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
696         if (rc)
697                 err_msg("ioctl OBD_IOC_CATINFO failed");
698         else
699                 fprintf(stdout, "%s", data.ioc_pbuf1);
700
701         closedir(root);
702         return rc;
703 }
704
705 int llapi_is_lustre_mnttype(char *type)
706 {
707         return (strcmp(type,"lustre") == 0 || strcmp(type,"lustre_lite") == 0);
708 }