Whamcloud - gitweb
b=2979
[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 op_create_file(char *name, long stripe_size, int stripe_offset,
65                    int stripe_count)
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_stripe_size = stripe_size;
73         lum.lmm_stripe_offset = stripe_offset;
74         lum.lmm_stripe_count = stripe_count;
75
76         fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
77         if (errno == EISDIR) 
78                 fd = open(name, O_DIRECTORY | O_RDONLY);
79
80         if (fd < 0) {
81                 err_msg("unable to open '%s'",name);
82                 rc = -errno;
83                 return rc;
84         }
85
86         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
87                 char *errmsg = "stripe already set";
88                 if (errno != EEXIST && errno != EALREADY)
89                         errmsg = strerror(errno);
90
91                 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
92                         name, fd, errmsg);
93                 rc = -errno;
94         }
95         if (close(fd) < 0) {
96                 err_msg("error on close for '%s' (%d)", name, fd);
97                 if (rc == 0)
98                         rc = -errno;
99         }
100         return rc;
101 }
102
103 struct find_param {
104         int     recursive;
105         int     verbose;
106         int     quiet;
107         struct  obd_uuid        *obduuid;
108         struct  obd_ioctl_data  data;
109         struct  lov_desc        desc;
110         int     uuidslen;
111         char    *buf;
112         int     buflen;
113         struct  obd_uuid        *uuids;
114         struct  lov_user_md     *lum;
115         int     got_uuids;
116         int     obdindex;
117         int     max_ost_count;
118 };
119
120 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
121 #define MAX_LOV_UUID_COUNT      1000
122 #define OBD_NOT_FOUND           (-1)
123
124 static int prepare_find(struct find_param *param)
125 {
126         int datalen, desclen;
127         int cfglen, lumlen;
128         int max_ost_count = MAX_LOV_UUID_COUNT;
129
130         datalen = size_round(sizeof(param->data));
131         desclen = size_round(sizeof(param->desc));
132         param->uuidslen = size_round(max_ost_count * sizeof(*param->uuids));
133         cfglen = datalen + desclen + param->uuidslen;
134         lumlen = lov_mds_md_size(max_ost_count);
135         if (cfglen > lumlen)
136                 param->buflen = cfglen;
137         else
138                 param->buflen = lumlen;
139
140         /* XXX max ioctl buffer size currently hardcoded to 8192 */
141         if (param->buflen > 8192) {
142                 int nuuids, remaining;
143
144                 param->buflen = 8192;
145                 nuuids = (param->buflen - datalen - desclen) /
146                         sizeof(*param->uuids);
147                 param->uuidslen = size_round(nuuids * sizeof(*param->uuids));
148                 remaining = nuuids * sizeof(*param->uuids);
149                 if (param->uuidslen > remaining)
150                         nuuids--;
151                 max_ost_count = nuuids;
152                 while ((lumlen=lov_mds_md_size(max_ost_count)) > param->buflen)
153                         --max_ost_count;
154
155                 cfglen = datalen + desclen + param->uuidslen;
156         }
157
158         if ((param->buf = malloc(param->buflen)) == NULL) {
159                 err_msg("unable to allocate %d bytes of memory for ioctl's",
160                         param->buflen);
161                 return ENOMEM;
162         }
163
164         param->lum = (struct lov_user_md *)param->buf;
165         param->uuids = (struct obd_uuid *)param->buf;
166         param->got_uuids = 0;
167         param->obdindex = OBD_NOT_FOUND;
168         param->max_ost_count = max_ost_count;
169
170         return 0;
171 }
172
173 static void cleanup_find(struct find_param *param)
174 {
175         if (param->obduuid)
176                 free(param->obduuid);
177         if (param->buf)
178                 free(param->buf);
179 }
180
181 static int get_obd_uuids(DIR *dir, char *dname, struct find_param *param)
182 {
183         int obdcount;
184         struct obd_uuid *uuidp;
185         int rc, i;
186
187         param->got_uuids = 1;
188         memset(&param->data, 0, sizeof(param->data));
189         param->data.ioc_inllen1 = sizeof(struct lov_desc);
190         param->data.ioc_inlbuf1 = (char *)&param->desc;
191         param->data.ioc_inllen2 = param->uuidslen;
192         param->data.ioc_inlbuf2 = (char *)param->uuids;
193
194         memset(&param->desc, 0, sizeof(struct lov_desc));
195         param->desc.ld_tgt_count = param->max_ost_count;
196
197         if (obd_ioctl_pack(&param->data, &param->buf, param->buflen)) {
198                 fprintf(stderr, "internal buffer error from %s\n", dname);
199                 return (param->obduuid ? EINVAL : 0);
200         }
201
202         rc = ioctl(dirfd(dir), OBD_IOC_LOV_GET_CONFIG, param->buf);
203         if (rc) {
204                 err_msg("error getting LOV config from %s", dname);
205                 return (param->obduuid ? errno : 0);
206         }
207
208         if (obd_ioctl_unpack(&param->data, param->buf, param->buflen)) {
209                 err_msg("invalid reply from ioctl from %s", dname);
210                 return (param->obduuid ? EINVAL : 0);
211         }
212
213         obdcount = param->desc.ld_tgt_count;
214         if (obdcount == 0)
215                 return 0;
216
217         if (param->obduuid) {
218                 for (i = 0, uuidp = param->uuids; i < obdcount; i++, uuidp++) {
219                         if (strncmp(param->obduuid->uuid, uuidp->uuid,
220                                     sizeof(*uuidp)) == 0) {
221                                 param->obdindex = i;
222                                 break;
223                         }
224                 }
225                 if (param->obdindex == OBD_NOT_FOUND) {
226                         printf("unknown obduuid: %s\n", param->obduuid->uuid);
227                         return EINVAL;
228                 }
229         } else if (!param->quiet) {
230                 printf("OBDS:\n");
231                 for (i = 0, uuidp = param->uuids; i < obdcount; i++, uuidp++)
232                         printf("%4d: %s\n", i, uuidp->uuid);
233         }
234
235         return 0;
236 }
237
238 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
239                           int obdindex, int quiet, int header, int body)
240 {
241         int i, obdstripe = 0;
242
243         if (obdindex != OBD_NOT_FOUND) {
244                 for (i = 0; i < lum->lmm_stripe_count; i++) {
245                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
246                                 printf("%s/%s\n", dname, fname);
247                                 obdstripe = 1;
248                                 break;
249                         }
250                 }
251         } else if (!quiet) {
252                 printf("%s/%s\n", dname, fname);
253                 obdstripe = 1;
254         }
255
256         /* if it's a directory */
257         if (*fname == '\0') {
258                 if (header && (obdstripe == 1)) {
259                         printf("count: %d, size: %d, offset: %d\n\n",
260                                lum->lmm_stripe_count, lum->lmm_stripe_size,
261                                (short int)lum->lmm_stripe_offset);
262                 }                
263                 return;
264         }        
265
266         if (header && (obdstripe == 1)) {
267                 printf("lmm_magic:          0x%08X\n",  lum->lmm_magic);
268                 printf("lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
269                 printf("lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
270                 printf("lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
271                 printf("lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
272                 printf("lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
273         }
274
275         if (body) {
276                 long long oid;
277
278                 if ((!quiet) && (obdstripe == 1))
279                         printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
280
281                 for (i = 0; i < lum->lmm_stripe_count; i++) {
282                         int idx = lum->lmm_objects[i].l_ost_idx;
283                         oid = lum->lmm_objects[i].l_object_id;
284                         if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
285                                 printf("\t%6u\t%14llu\t%#13llx\t%14lld%s\n",
286                                        idx, oid, oid, 
287                                        (long long)lum->lmm_objects[i].l_object_gr,
288                                        obdindex == idx ? " *" : "");
289                 }
290                 printf("\n");
291         }
292 }
293
294 void lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
295 {
296         switch(*(__u32 *)param->lum) { /* lum->lmm_magic */
297         case LOV_USER_MAGIC_V1:
298                 lov_dump_user_lmm_v1(param->lum, dname, fname, param->obdindex,
299                                      param->quiet, param->verbose,
300                                      (param->verbose || !param->obduuid));
301                 break;
302         default:
303                 printf("unknown lmm_magic:  0x%08X\n", *(__u32 *)param->lum);
304                 return;
305         }
306 }
307
308 int get_file_stripe(char *path, struct lov_user_md *lum)
309 {
310         char *dname, *fname;
311         int fd, rc = 0;
312
313         fname = strrchr(path, '/');
314
315         /* It should be a file (or other non-directory) */
316         if (fname == NULL) {
317                 dname = (char *)malloc(2);
318                 if (dname == NULL)
319                         return ENOMEM;
320                 strcpy(dname, ".");
321                 fname = path;
322         } else {
323                 dname = (char *)malloc(fname - path + 1);
324                 if (dname == NULL)
325                         return ENOMEM;
326                 strncpy(dname, path, fname - path);
327                 dname[fname - path + 1] = '\0';
328                 fname++;
329         }
330
331         if ((fd = open(dname, O_RDONLY)) == -1) {
332                 free(dname);
333                 return errno;
334         }
335
336         strncpy((char *)lum, fname, sizeof(*lum));
337         if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
338                 close(fd);
339                 free(dname);
340                 return errno;
341         }
342
343         if (close(fd) == -1)
344                 rc = errno;
345
346         free(dname);
347
348         return rc;
349 }
350
351 static int process_file(DIR *dir, char *dname, char *fname,
352                          struct find_param *param)
353 {
354         int rc;
355
356         strncpy((char *)param->lum, fname, param->buflen);
357
358         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->lum);
359         if (rc) {
360                 if (errno == ENODATA) {
361                         if (!param->obduuid && !param->quiet)
362                                 fprintf(stderr,
363                                         "%s/%s has no stripe info\n",
364                                         dname, fname);
365                         rc = 0;
366                 } else if (errno == EISDIR) {
367                         fprintf(stderr, "process_file on directory %s/%s!\n",
368                                 dname, fname);
369                         /* add fname to directory list; */
370                         rc = errno;
371                 } else {
372                         err_msg("IOC_MDC_GETSTRIPE ioctl failed");
373                         rc = errno;
374                 }
375                 return rc;
376         }
377
378         lov_dump_user_lmm(param, dname, fname);
379
380         return 0;
381 }
382
383 /* some 64bit libcs implement readdir64() by calling sys_getdents().  the
384  * kernel's sys_getdents() doesn't return d_type.  */
385 unsigned char handle_dt_unknown(char *parent, char *entry)
386 {
387         char path[PATH_MAX + 1];
388         int fd, ret;
389
390         ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
391         if (ret >= PATH_MAX)
392                 return DT_UNKNOWN;
393
394         fd = open(path, O_DIRECTORY|O_RDONLY);
395         if (fd < 0) {
396                 if (errno == ENOTDIR)
397                         return DT_REG; /* kind of a lie */
398                 return DT_UNKNOWN;
399         }
400         close(fd);
401         return DT_DIR;
402 }
403
404 static int process_dir(DIR *dir, char *dname, struct find_param *param)
405 {
406         struct dirent64 *dirp;
407         DIR *subdir;
408         char path[1024];
409         int rc;
410
411         if (!param->got_uuids) {
412                 rc = get_obd_uuids(dir, dname, param);
413                 if (rc)
414                         return rc;
415         }
416
417         /* retrieve dir's stripe info */
418         strncpy((char *)param->lum, dname, param->buflen);
419         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->lum);
420         if (rc) {
421                 if (errno == ENODATA) {
422                         if (!param->obduuid && !param->quiet)
423                                 printf("%s/%s has no stripe info\n", 
424                                        dname, "");
425                         rc = 0;
426                 } else {
427                         err_msg("IOC_MDC_GETSTRIPE ioctl failed");
428                         return errno;
429                 }
430         } else {
431                lov_dump_user_lmm(param, dname, "");
432         }
433
434         /* Handle the contents of the directory */
435         while ((dirp = readdir64(dir)) != NULL) {
436                 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
437                         continue;
438
439                 if (dirp->d_type == DT_UNKNOWN)
440                         dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
441
442                 switch (dirp->d_type) {
443                 case DT_UNKNOWN:
444                         err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
445                                 dirp->d_type);
446                         /* If we cared we could stat the file to determine
447                          * type and continue on here, but we don't since we
448                          * know d_type should be valid for lustre and this
449                          * tool only makes sense for lustre filesystems. */
450                         return EINVAL;
451                         break;
452                 case DT_DIR:
453                         if (!param->recursive)
454                                 break;
455                         strcpy(path, dname);
456                         strcat(path, "/");
457                         strcat(path, dirp->d_name);
458                         subdir = opendir(path);
459                         if (subdir == NULL) {
460                                 err_msg("\"%.40s\" opendir failed", path);
461                                 return errno;
462                         }
463                         rc = process_dir(subdir, path, param);
464                         closedir(subdir);
465                         if (rc)
466                                 return rc;
467                         break;
468                 case DT_REG:
469                         rc = process_file(dir, dname, dirp->d_name, param);
470                         if (rc)
471                                 return rc;
472                         break;
473                 default:
474                         break;
475                 }
476         }
477
478         return 0;
479 }
480
481 static int process_path(char *path, struct find_param *param)
482 {
483         char *fname, *dname;
484         DIR *dir;
485         int rc = 0;
486
487         fname = strrchr(path, '/');
488         if (fname != NULL && fname[1] == '\0') {
489                 /* Trailing '/', it must be a dir */
490                 *fname = '\0';
491                 dir = opendir(path);
492                 if (dir == NULL) {
493                         err_msg("\"%.40s\" opendir failed", path);
494                         rc = errno;
495                 } else {
496                         rc = process_dir(dir, path, param);
497                         closedir(dir);
498                 }
499         } else if ((dir = opendir(path)) != NULL) {
500                 /* No trailing '/', but it is still a dir */
501                 rc = process_dir(dir, path, param);
502                 closedir(dir);
503         } else {
504                 /* It must be a file (or other non-directory) */
505                 if (fname == NULL) {
506                         dname = ".";
507                         fname = path;
508                 } else {
509                         *fname = '\0';
510                         fname++;
511                         dname = path;
512                 }
513                 dir = opendir(dname);
514                 if (dir == NULL) {
515                         err_msg("\"%.40s\" opendir failed", dname);
516                         rc = errno;
517                 } else {
518                         if (!param->got_uuids)
519                                 rc = get_obd_uuids(dir, dname, param);
520                         if (rc == 0)
521                                 rc = process_file(dir, dname, fname, param);
522                         closedir(dir);
523                 }
524         }
525
526         return rc;
527 }
528
529
530 int op_find(char *path, struct obd_uuid *obduuid, int recursive,
531             int verbose, int quiet)
532 {
533         struct find_param param;
534         int ret = 0;
535
536         memset(&param, 0, sizeof(param));
537         param.recursive = recursive;
538         param.verbose = verbose;
539         param.quiet = quiet;
540         if (obduuid) {
541                 param.obduuid = malloc(sizeof(*obduuid));
542                 if (param.obduuid == NULL) {
543                         ret = ENOMEM;
544                         goto out;
545                 }
546                 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
547         }
548
549         ret = prepare_find(&param);
550         if (ret)
551                 goto out;
552
553         process_path(path, &param);
554 out:
555         cleanup_find(&param);
556         return ret;
557 }
558
559 #define MAX_STRING_SIZE 128
560 #define DEVICES_LIST "/proc/fs/lustre/devices"
561
562 int op_check(int type_num, char **obd_type, char *dir)
563 {
564         int rc = 0;
565         int i;
566
567         char buf[MAX_STRING_SIZE];
568         FILE *fp = fopen(DEVICES_LIST, "r");
569                                                                                                                                                
570         if (fp == NULL) {
571                 fprintf(stderr, "error: %s could not open file "
572                         DEVICES_LIST " .\n", strerror(rc =  errno));
573                 return rc;
574         }
575
576         while (fgets(buf, sizeof(buf), fp) != NULL) {
577                 char *obd_type_name = NULL;
578                 char *obd_name = NULL;
579
580                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
581                 char *bufl = rawbuf;
582                 char *bufp = buf;
583                 int max = sizeof(rawbuf);
584                 struct obd_ioctl_data datal;
585                 struct obd_statfs osfs_buffer;
586                                                                                 
587                 while(bufp[0] == ' ') bufp += 1;
588                 for(i = 0; i < 3; i++) {
589                         obd_type_name = strsep(&bufp, " ");
590                 }
591                 obd_name = strsep(&bufp, " ");
592
593                 memset (&osfs_buffer, 0, sizeof (osfs_buffer));
594
595                 memset(bufl, 0, sizeof(rawbuf));
596                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
597                 datal.ioc_plen1 = sizeof (osfs_buffer);
598
599                 for (i=0;i<type_num;i++) 
600                         if (strcmp(obd_type_name, obd_type[i]) == 0) {
601                                 datal.ioc_inlbuf1 = obd_name;
602                                 datal.ioc_inllen1 = strlen(obd_name) + 1; 
603
604                                 obd_ioctl_pack(&datal,&bufl,max);
605
606                                 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,bufl);
607
608                                 if (rc) {
609                                         fprintf(stderr, "error: check %s: %s\n", 
610                                                 obd_name, strerror(rc = errno));
611                                 } else {
612                                         printf("%s active.\n",obd_name);
613                                 }
614                         }
615
616         }                                                                                                  
617         fclose(fp);
618         return rc;
619 }
620
621 #undef MAX_STRING_SIZE
622
623 int op_catinfo(char *dir, char *keyword, char *node_name)
624 {
625         char raw[OBD_MAX_IOCTL_BUFFER];
626         char out[LLOG_CHUNK_SIZE];
627         char *buf = raw;
628         struct obd_ioctl_data data;
629         char key[30];
630         DIR *root;
631         int rc;
632         
633         sprintf(key, "%s", keyword);
634         memset(raw, 0, sizeof(buf));
635         memset(out, 0, sizeof(out));
636         data.ioc_inlbuf1 = key;
637         data.ioc_inllen1 = strlen(key) + 1;
638         if (node_name) {
639                 data.ioc_inlbuf2 = node_name;
640                 data.ioc_inllen2 = strlen(node_name) + 1;
641         }
642         data.ioc_pbuf1 = out;
643         data.ioc_plen1 = sizeof(out);
644         rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
645         if (rc) 
646                 return rc;
647         
648         root = opendir(dir);
649         if (root == NULL) {
650                 err_msg("open %s failed", dir);
651                 return errno;
652         }
653
654         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf); 
655         if (rc)
656                 err_msg("ioctl OBD_IOC_CATINFO failed");
657         else
658                 fprintf(stdout, "%s", data.ioc_pbuf1);
659                 
660         closedir(root);
661         return rc;
662 }
663