Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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 <linux/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 (fd < 0) {
78                 err_msg("unable to open '%s'",name);
79                 rc = -errno;
80                 return rc;
81         }
82         if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
83                 char *errmsg = "stripe already set";
84                 if (errno != EEXIST && errno != EALREADY)
85                         errmsg = strerror(errno);
86
87                 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
88                         name, fd, errmsg);
89                 rc = -errno;
90         }
91         if (close(fd) < 0) {
92                 err_msg("error on close for '%s' (%d)", name, fd);
93                 if (rc == 0)
94                         rc = -errno;
95         }
96         return rc;
97 }
98
99
100 struct find_param {
101         int     recursive;
102         int     verbose;
103         int     quiet;
104         struct  obd_uuid        *obduuid;
105         struct  obd_ioctl_data  data;
106         struct  lov_desc        desc;
107         int     uuidslen;
108         char    *buf;
109         int     buflen;
110         struct  obd_uuid        *uuids;
111         struct  lov_user_md     *lum;
112         int     got_uuids;
113         int     obdindex;
114         int     max_ost_count;
115 };
116
117 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
118 #define MAX_LOV_UUID_COUNT      1000
119 #define OBD_NOT_FOUND           (-1)
120
121 static int prepare_find(struct find_param *param)
122 {
123         int datalen, desclen;
124         int cfglen, lumlen;
125         int max_ost_count = MAX_LOV_UUID_COUNT;
126
127         datalen = size_round(sizeof(param->data));
128         desclen = size_round(sizeof(param->desc));
129         param->uuidslen = size_round(max_ost_count * sizeof(*param->uuids));
130         cfglen = datalen + desclen + param->uuidslen;
131         lumlen = lov_mds_md_size(max_ost_count);
132         if (cfglen > lumlen)
133                 param->buflen = cfglen;
134         else
135                 param->buflen = lumlen;
136
137         /* XXX max ioctl buffer size currently hardcoded to 8192 */
138         if (param->buflen > 8192) {
139                 int nuuids, remaining;
140
141                 param->buflen = 8192;
142                 nuuids = (param->buflen - datalen - desclen) /
143                         sizeof(*param->uuids);
144                 param->uuidslen = size_round(nuuids * sizeof(*param->uuids));
145                 remaining = nuuids * sizeof(*param->uuids);
146                 if (param->uuidslen > remaining)
147                         nuuids--;
148                 max_ost_count = nuuids;
149                 while ((lumlen=lov_mds_md_size(max_ost_count)) > param->buflen)
150                         --max_ost_count;
151
152                 cfglen = datalen + desclen + param->uuidslen;
153         }
154
155         if ((param->buf = malloc(param->buflen)) == NULL) {
156                 err_msg("unable to allocate %d bytes of memory for ioctl's",
157                         param->buflen);
158                 return ENOMEM;
159         }
160
161         param->lum = (struct lov_user_md *)param->buf;
162         param->uuids = (struct obd_uuid *)param->buf;
163         param->got_uuids = 0;
164         param->obdindex = OBD_NOT_FOUND;
165         param->max_ost_count = max_ost_count;
166
167         return 0;
168 }
169
170 static void cleanup_find(struct find_param *param)
171 {
172         if (param->obduuid)
173                 free(param->obduuid);
174         if (param->buf)
175                 free(param->buf);
176 }
177
178 static int get_obd_uuids(DIR *dir, char *dname, struct find_param *param)
179 {
180         int obdcount;
181         struct obd_uuid *uuidp;
182         int rc, i;
183
184         param->got_uuids = 1;
185         memset(&param->data, 0, sizeof(param->data));
186         param->data.ioc_inllen1 = sizeof(struct lov_desc);
187         param->data.ioc_inlbuf1 = (char *)&param->desc;
188         param->data.ioc_inllen2 = param->uuidslen;
189         param->data.ioc_inlbuf2 = (char *)param->uuids;
190
191         memset(&param->desc, 0, sizeof(struct lov_desc));
192         param->desc.ld_tgt_count = param->max_ost_count;
193
194         if (obd_ioctl_pack(&param->data, &param->buf, param->buflen)) {
195                 fprintf(stderr, "internal buffer error from %s\n", dname);
196                 return (param->obduuid ? EINVAL : 0);
197         }
198
199         rc = ioctl(dirfd(dir), OBD_IOC_LOV_GET_CONFIG, param->buf);
200         if (rc) {
201                 err_msg("error getting LOV config from %s", dname);
202                 return (param->obduuid ? errno : 0);
203         }
204
205         if (obd_ioctl_unpack(&param->data, param->buf, param->buflen)) {
206                 err_msg("invalid reply from ioctl from %s", dname);
207                 return (param->obduuid ? EINVAL : 0);
208         }
209
210         obdcount = param->desc.ld_tgt_count;
211         if (obdcount == 0)
212                 return 0;
213
214         if (param->obduuid) {
215                 for (i = 0, uuidp = param->uuids; i < obdcount; i++, uuidp++) {
216                         if (strncmp(param->obduuid->uuid, uuidp->uuid,
217                                     sizeof(*uuidp)) == 0) {
218                                 param->obdindex = i;
219                                 break;
220                         }
221                 }
222         } else if (!param->quiet) {
223                 printf("OBDS:\n");
224                 for (i = 0, uuidp = param->uuids; i < obdcount; i++, uuidp++)
225                         printf("%4d: %s\n", i, uuidp->uuid);
226         }
227
228         return 0;
229 }
230
231 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
232                           int obdindex, int quiet, int header, int body)
233 {
234         int i;
235
236         if (obdindex != OBD_NOT_FOUND) {
237                 for (i = 0; i < lum->lmm_stripe_count; i++) {
238                         if (obdindex == lum->lmm_objects[i].l_ost_idx) {
239                                 printf("%s/%s\n", dname, fname);
240                                 break;
241                         }
242                 }
243         } else if (!quiet) {
244                 printf("%s/%s\n", dname, fname);
245         }
246
247         if (header) {
248                 printf("lmm_magic:          0x%80X\n",  lum->lmm_magic);
249                 printf("lmm_object_gr:      "LPX64"\n", lum->lmm_object_gr);
250                 printf("lmm_object_id:      "LPX64"\n", lum->lmm_object_id);
251                 printf("lmm_stripe_count:   %u\n", (int)lum->lmm_stripe_count);
252                 printf("lmm_stripe_size:    %u\n",      lum->lmm_stripe_size);
253                 printf("lmm_stripe_pattern: %x\n",      lum->lmm_pattern);
254         }
255
256         if (body) {
257                 long long oid;
258
259                 if (!quiet)
260                         printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
261
262                 for (i = 0; i < lum->lmm_stripe_count; i++) {
263                         int idx = lum->lmm_objects[i].l_ost_idx;
264                         oid = lum->lmm_objects[i].l_object_id;
265                         printf("\t%6u\t%14llu\t%#13llx\t%14lld%s\n", idx, oid,
266                                oid, (long long)lum->lmm_objects[i].l_object_gr,
267                                obdindex == idx ? " *" : "");
268                 }
269                 printf("\n");
270         }
271 }
272
273 void lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
274 {
275         switch(*(__u32 *)param->lum) { /* lum->lmm_magic */
276         case LOV_USER_MAGIC_V1:
277                 lov_dump_user_lmm_v1(param->lum, dname, fname, param->obdindex,
278                                      param->quiet, param->verbose,
279                                      (param->verbose || !param->obduuid));
280                 break;
281         default:
282                 printf("unknown lmm_magic:  0x%08X\n", *(__u32 *)param->lum);
283                 return;
284         }
285 }
286
287 static int process_file(DIR *dir, char *dname, char *fname,
288                          struct find_param *param)
289 {
290         int rc;
291
292         strncpy((char *)param->lum, fname, param->buflen);
293
294         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->lum);
295         if (rc) {
296                 if (errno == ENODATA) {
297                         if (!param->obduuid && !param->quiet)
298                                 fprintf(stderr,
299                                         "%s/%s has no stripe info\n",
300                                         dname, fname);
301                         rc = 0;
302                 } else if (errno == EISDIR) {
303                         fprintf(stderr, "process_file on directory %s/%s!\n",
304                                 dname, fname);
305                         /* add fname to directory list; */
306                         rc = errno;
307                 } else {
308                         err_msg("IOC_MDC_GETSTRIPE ioctl failed");
309                         rc = errno;
310                 }
311                 return rc;
312         }
313
314         lov_dump_user_lmm(param, dname, fname);
315
316         return 0;
317 }
318
319
320 static int process_dir(DIR *dir, char *dname, struct find_param *param)
321 {
322         struct dirent64 *dirp;
323         DIR *subdir;
324         char path[1024];
325         int rc;
326
327         if (!param->got_uuids) {
328                 rc = get_obd_uuids(dir, dname, param);
329                 if (rc)
330                         return rc;
331         }
332
333         /* Handle the contents of the directory */
334         while ((dirp = readdir64(dir)) != NULL) {
335                 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
336                         continue;
337
338                 switch (dirp->d_type) {
339                 case DT_UNKNOWN:
340                         err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
341                                 dirp->d_type);
342                         /* If we cared we could stat the file to determine
343                          * type and continue on here, but we don't since we
344                          * know d_type should be valid for lustre and this
345                          * tool only makes sense for lustre filesystems. */
346                         return EINVAL;
347                         break;
348                 case DT_DIR:
349                         if (!param->recursive)
350                                 break;
351                         strcpy(path, dname);
352                         strcat(path, "/");
353                         strcat(path, dirp->d_name);
354                         subdir = opendir(path);
355                         if (subdir == NULL) {
356                                 err_msg("\"%.40s\" opendir failed", path);
357                                 return errno;
358                         }
359                         rc = process_dir(subdir, path, param);
360                         closedir(subdir);
361                         if (rc)
362                                 return rc;
363                         break;
364                 case DT_REG:
365                         rc = process_file(dir, dname, dirp->d_name, param);
366                         if (rc)
367                                 return rc;
368                         break;
369                 default:
370                         break;
371                 }
372         }
373
374         return 0;
375 }
376
377 static int process_path(char *path, struct find_param *param)
378 {
379         char *fname, *dname;
380         DIR *dir;
381         int rc = 0;
382
383         fname = strrchr(path, '/');
384         if (fname != NULL && fname[1] == '\0') {
385                 /* Trailing '/', it must be a dir */
386                 *fname = '\0';
387                 dir = opendir(path);
388                 if (dir == NULL) {
389                         err_msg("\"%.40s\" opendir failed", path);
390                         rc = errno;
391                 } else {
392                         rc = process_dir(dir, path, param);
393                         closedir(dir);
394                 }
395         } else if ((dir = opendir(path)) != NULL) {
396                 /* No trailing '/', but it is still a dir */
397                 rc = process_dir(dir, path, param);
398                 closedir(dir);
399         } else {
400                 /* It must be a file (or other non-directory) */
401                 if (fname == NULL) {
402                         dname = ".";
403                         fname = path;
404                 } else {
405                         *fname = '\0';
406                         fname++;
407                         dname = path;
408                 }
409                 dir = opendir(dname);
410                 if (dir == NULL) {
411                         err_msg("\"%.40s\" opendir failed", dname);
412                         rc = errno;
413                 } else {
414                         if (!param->got_uuids)
415                                 rc = get_obd_uuids(dir, dname, param);
416                         if (rc == 0)
417                                 rc = process_file(dir, dname, fname, param);
418                         closedir(dir);
419                 }
420         }
421
422         return rc;
423 }
424
425
426 int op_find(char *path, struct obd_uuid *obduuid, int recursive,
427             int verbose, int quiet)
428 {
429         struct find_param param;
430         int ret = 0;
431
432         memset(&param, 0, sizeof(param));
433         param.recursive = recursive;
434         param.verbose = verbose;
435         param.quiet = quiet;
436         if (obduuid) {
437                 param.obduuid = malloc(sizeof(*obduuid));
438                 if (param.obduuid == NULL) {
439                         ret = ENOMEM;
440                         goto out;
441                 }
442                 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
443         }
444
445         ret = prepare_find(&param);
446         if (ret)
447                 goto out;
448
449         process_path(path, &param);
450 out:
451         cleanup_find(&param);
452         return ret;
453 }
454
455 #define MAX_STRING_SIZE 128
456
457 int op_check(int type_num, char **obd_type, char *dir)
458 {
459         int rc=0;
460         int i=0,j=0,k;
461         char buf[OBD_MAX_IOCTL_BUFFER];
462         char *buf2;
463         struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
464                                                                                                                      
465         memset(buf, 0, sizeof(buf));
466         data->ioc_version = OBD_IOCTL_VERSION;
467         data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
468         data->ioc_len = obd_ioctl_packlen(data);
469                                                                                                                              
470         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_LIST, data);
471                    
472         buf2 = data->ioc_bulk;
473
474         if (!data->ioc_inlbuf1) {
475                 err_msg("No buffer passed!\n");
476                 rc = errno;
477         }
478
479         do {
480                 char status[3];
481                 char obd_type_name[sizeof(struct obd_type)];
482                 char obd_name[MAX_STRING_SIZE];
483                 char obd_uuid[sizeof(struct obd_uuid)];
484                 int obd_type_refcnt;
485
486                 char rawbuf[OBD_MAX_IOCTL_BUFFER];
487                 char *bufl = rawbuf;
488                 int max = sizeof(rawbuf);
489                 struct obd_ioctl_data datal;
490                 struct obd_statfs osfs_buffer;
491                                                                                 
492                 memset (&osfs_buffer, 0, sizeof (osfs_buffer));
493
494                 memset(bufl, 0, sizeof(rawbuf));
495                 datal.ioc_pbuf1 = (char *)&osfs_buffer;
496                 datal.ioc_plen1 = sizeof (osfs_buffer);
497
498                 j = sscanf(buf2,"%d %s %s %s %s %d",&j,
499                              status,obd_type_name,
500                              obd_name, obd_uuid,
501                              &obd_type_refcnt);
502
503                 if (j != 6) break;
504
505                 for (k=0;k<type_num;k++) 
506                         if (strcmp(obd_type_name, obd_type[k]) == 0) {
507                                 datal.ioc_inlbuf1 = obd_name;
508                                 datal.ioc_inllen1 = strlen(obd_name) + 1; 
509
510                                 obd_ioctl_pack(&datal,&bufl,max);
511
512                                 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,bufl);
513
514                                 if (rc) {
515                                         fprintf(stderr, "error: check %s: %s\n", 
516                                                 obd_name, strerror(rc = errno));
517                                 } else {
518                                         printf("%s active.\n",obd_name);
519                                 }
520                         }
521
522                 if (j==6)
523                         for (i=0;buf2[i]!= '\n';i++);
524
525                 buf2 +=(i+1);
526
527         } while (j==6);                                                                                                     
528
529         return rc;
530 }
531
532 #undef MAX_STRING_SIZE
533