1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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>
9 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
34 #include <sys/ioctl.h>
41 #include <sys/types.h>
42 #ifdef HAVE_LINUX_TYPES_H
43 #include <linux/types.h>
47 #ifdef HAVE_LINUX_UNISTD_H
48 #include <linux/unistd.h>
52 #include <liblustre.h>
53 #include <linux/obd.h>
54 #include <linux/lustre_lib.h>
55 #include <linux/lustre_acl.h>
56 #include <lustre/lustre_user.h>
57 #include <linux/obd_lov.h>
58 #include <portals/ptlctl.h>
60 static void err_msg(char *fmt, ...)
63 int tmp_errno = errno;
66 vfprintf(stderr, fmt, args);
68 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
71 int llapi_file_create(char *name, long stripe_size, int stripe_offset,
72 int stripe_count, int stripe_pattern)
74 struct lov_user_md lum = { 0 };
78 /* Initialize IOCTL striping pattern structure */
79 lum.lmm_magic = LOV_USER_MAGIC;
80 lum.lmm_pattern = stripe_pattern;
81 lum.lmm_stripe_size = stripe_size;
82 lum.lmm_stripe_count = stripe_count;
83 lum.lmm_stripe_offset = stripe_offset;
85 fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
86 if (errno == EISDIR) {
87 fd = open(name, O_DIRECTORY | O_RDONLY);
92 err_msg("unable to open '%s'",name);
97 /* setting stripe pattern 0 -1 0 to a dir means to delete it */
99 if (stripe_size == 0 && stripe_count == 0 &&
101 lum.lmm_stripe_size = -1;
103 if (stripe_size == -1) {
104 err_msg("deleting file stripe info is not allowed\n");
110 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
111 char *errmsg = "stripe already set";
112 if (errno != EEXIST && errno != EALREADY)
113 errmsg = strerror(errno);
115 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
121 err_msg("error on close for '%s' (%d)", name, fd);
128 int op_create_dir(char *name, int stripe_count)
130 struct ll_user_mkdir_stripe lums = { 0 };
135 dirname = rindex(name, '/');
143 lums.lums_nstripes = stripe_count;
144 lums.lums_namelen = strlen(dirname);
145 lums.lums_name = dirname;
146 /* XXX: Probably users might want to specify permissions as well? */
147 lums.lums_mode = 0755;
149 fd = open(name, O_RDONLY|O_DIRECTORY);
153 err_msg("unable to open '%s'",name);
158 if (ioctl(fd, LL_IOC_MDC_MKDIRSTRIPE, &lums)) {
159 char *errmsg = strerror(errno);
161 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
166 err_msg("error on close for '%s' (%d)", name, fd);
173 /* short term backwards compat only */
174 int op_create_file(char *name, long stripe_size, int stripe_offset,
177 return llapi_file_create(name, stripe_size, stripe_offset,
186 struct obd_uuid *obduuid;
188 struct lov_user_md *data;
194 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
195 #define MAX_LOV_UUID_COUNT 1000
196 #define OBD_NOT_FOUND (-1)
198 static int prepare_find(struct find_param *param)
200 if (param->showfid) {
201 param->datalen = PATH_MAX + 1;
202 if ((param->data = malloc(param->datalen)) == NULL) {
203 err_msg("unable to allocate %d bytes of memory for ioctl",
208 param->datalen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
209 if ((param->data = malloc(param->datalen)) == NULL) {
210 err_msg("unable to allocate %d bytes of memory for ioctl",
215 param->got_uuids = 0;
216 param->obdindex = OBD_NOT_FOUND;
220 static void cleanup_find(struct find_param *param)
223 free(param->obduuid);
228 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp,
229 __u32 *obdgens, int *ost_count)
231 struct obd_ioctl_data data = { 0, };
232 struct lov_desc desc = { 0, };
234 int max_ost_count, rc;
236 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
237 size_round(sizeof(desc))) /
238 (sizeof(*uuidp) + sizeof(*obdgens));
239 if (max_ost_count > *ost_count)
240 max_ost_count = *ost_count;
242 data.ioc_inllen1 = sizeof(desc);
243 data.ioc_inlbuf1 = (char *)&desc;
244 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
245 data.ioc_inlbuf2 = (char *)uuidp;
246 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
247 data.ioc_inlbuf3 = (char *)obdgens;
249 desc.ld_tgt_count = max_ost_count;
251 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
252 fprintf(stderr, "internal buffer error packing\n");
257 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
259 err_msg("error getting LOV config");
264 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
265 fprintf(stderr, "invalid reply from ioctl");
270 *ost_count = desc.ld_tgt_count;
277 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
279 struct obd_uuid uuids[1024], *uuidp;
280 __u32 obdgens[1024], *genp;
284 param->got_uuids = 1;
286 rc = llapi_lov_get_uuids(dirfd(dir), uuids, obdgens, &obdcount);
288 return (param->obduuid ? rc : 0);
293 if (param->obduuid) {
294 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
295 if (strncmp((char *)param->obduuid->uuid, (char *)uuidp->uuid,
296 sizeof(*uuidp)) == 0) {
301 if (param->obdindex == OBD_NOT_FOUND) {
302 printf("unknown obduuid: %s\n", param->obduuid->uuid);
305 } else if (!param->quiet) {
307 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
310 for (i = 0; i < obdcount; i++, uuidp++, genp++) {
311 if (obd_uuid_empty(uuidp))
313 printf("\t%6u\t%14u\t\t %s\n", i, *genp, uuidp->uuid);
319 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
320 int obdindex, int quiet, int header, int body)
322 int i, obdstripe = 0;
324 if (obdindex != OBD_NOT_FOUND) {
325 for (i = 0; i < lum->lmm_stripe_count; i++) {
326 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
327 printf("%s/%s\n", dname, fname);
333 printf("%s/%s\n", dname, fname);
337 /* if it's a directory */
338 if (*fname == '\0') {
339 if (header && (obdstripe == 1)) {
340 printf("count: %d, size: %d, offset: %d\n\n",
341 lum->lmm_stripe_count, lum->lmm_stripe_size,
342 (short int)lum->lmm_stripe_offset);
347 if (header && (obdstripe == 1)) {
348 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
349 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
350 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
351 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
352 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
353 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
357 if ((!quiet) && (obdstripe == 1))
358 printf("\tobdidx\t\t obdgen\t\t objid\t\tobjid\t\t group\n");
360 for (i = 0; i < lum->lmm_stripe_count; i++) {
361 int idx = lum->lmm_objects[i].l_ost_idx;
362 long long oid = lum->lmm_objects[i].l_object_id;
363 int gen = lum->lmm_objects[i].l_ost_gen;
364 long long gr = lum->lmm_objects[i].l_object_gr;
365 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
366 printf("\t%6u\t%14u\t%14llu\t%#13llx\t%14lld%s\n",
367 idx, gen, oid, oid, gr,
368 obdindex == idx ? " *" : "");
374 void llapi_lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
376 switch(*(__u32 *)param->data) { /* lum->lmm_magic */
377 case LOV_USER_MAGIC_V1:
378 lov_dump_user_lmm_v1(param->data, dname, fname, param->obdindex,
379 param->quiet, param->verbose,
380 (param->verbose || !param->obduuid));
383 printf("unknown lmm_magic: 0x%08X\n", *(__u32 *)param->data);
388 void llapi_dump_fid(struct find_param *param, char *dname, char *fname)
392 char path[PATH_MAX + 1];
394 n = snprintf(path, PATH_MAX, "%s/%s",
402 printf("%s:%*s"DLID4"\n", path, n, "",
403 OLID4((struct lustre_id *)param->data));
407 int llapi_file_get_stripe(char *path, struct lov_user_md *lum)
412 fname = strrchr(path, '/');
414 /* It should be a file (or other non-directory) */
416 dname = (char *)malloc(2);
422 dname = (char *)malloc(fname - path + 1);
425 strncpy(dname, path, fname - path);
426 dname[fname - path] = '\0';
430 if ((fd = open(dname, O_RDONLY)) == -1) {
435 strncpy((char *)lum, fname, sizeof(*lum));
436 if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
450 int llapi_file_get_fid(char *path, void *data)
455 fname = strrchr(path, '/');
458 dname = (char *)malloc(2);
464 dname = (char *)malloc(fname - path + 1);
467 strncpy(dname, path, fname - path);
468 dname[fname - path] = '\0';
475 if ((fd = open(dname, O_RDONLY)) == -1) {
480 strncpy((char *)data, fname, strlen(fname));
481 if (ioctl(fd, IOC_MDC_SHOWFID, (void *)data) == -1) {
494 /* short term backwards compat only */
495 int op_get_file_stripe(char *path, struct lov_user_md *lum)
497 return llapi_file_get_stripe(path, lum);
500 static int process_file(DIR *dir, char *dname, char *fname,
501 struct find_param *param)
505 if (param->showfid) {
506 char path[PATH_MAX + 1];
508 snprintf(path, PATH_MAX, "%s/%s", dname, fname);
509 rc = llapi_file_get_fid(path, (void *)param->data);
511 err_msg("IOC_MDC_SHOWFID ioctl failed");
514 llapi_dump_fid(param, dname, fname);
516 strncpy((char *)param->data, fname, param->datalen);
517 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->data);
519 if (errno == ENODATA) {
520 if (!param->obduuid && !param->quiet)
522 "%s/%s has no stripe info\n",
525 } else if (errno == EISDIR) {
526 fprintf(stderr, "process_file on directory %s/%s!\n",
528 /* add fname to directory list; */
531 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
536 llapi_lov_dump_user_lmm(param, dname, fname);
541 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
542 * kernel's sys_getdents() doesn't return d_type. */
543 unsigned char handle_dt_unknown(char *parent, char *entry)
545 char path[PATH_MAX + 1];
548 ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
552 fd = open(path, O_DIRECTORY|O_RDONLY);
554 if (errno == ENOTDIR)
555 return DT_REG; /* kind of a lie */
562 static int process_dir(DIR *dir, char *dname, struct find_param *param)
564 char path[PATH_MAX + 1];
565 struct dirent64 *dirp;
569 /* retrieve dir's stripe info */
570 if (param->showfid) {
571 rc = llapi_file_get_fid(dname, (void *)param->data);
573 err_msg("IOC_MDC_SHOWFID ioctl failed");
576 llapi_dump_fid(param, dname, "");
578 if (!param->got_uuids) {
579 rc = setup_obd_uuids(dir, dname, param);
584 strncpy((char *)param->data, dname, param->datalen);
585 rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->data);
587 if (errno == ENODATA) {
588 if (!param->obduuid && param->verbose)
589 printf("%s/%s has no stripe info\n", dname, "");
592 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
596 llapi_lov_dump_user_lmm(param, dname, "");
600 /* Handle the contents of the directory */
601 while ((dirp = readdir64(dir)) != NULL) {
602 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
605 if (dirp->d_type == DT_UNKNOWN)
606 dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
608 switch (dirp->d_type) {
610 err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
612 /* If we cared we could stat the file to determine
613 * type and continue on here, but we don't since we
614 * know d_type should be valid for lustre and this
615 * tool only makes sense for lustre filesystems. */
618 if (!param->recursive)
622 strcat(path, dirp->d_name);
623 subdir = opendir(path);
624 if (subdir == NULL) {
625 err_msg("\"%.40s\" opendir failed", path);
628 rc = process_dir(subdir, path, param);
634 rc = process_file(dir, dname, dirp->d_name, param);
646 static int process_path(char *path, struct find_param *param)
652 fname = strrchr(path, '/');
653 if (fname != NULL && fname[1] == '\0') {
654 /* Trailing '/', it must be a dir */
658 err_msg("\"%.40s\" opendir failed", path);
661 rc = process_dir(dir, path, param);
664 } else if ((dir = opendir(path)) != NULL) {
665 /* No trailing '/', but it is still a dir */
666 rc = process_dir(dir, path, param);
669 /* It must be a file (or other non-directory) */
678 dir = opendir(dname);
680 err_msg("\"%.40s\" opendir failed", dname);
683 if (!param->showfid) {
684 if (!param->got_uuids)
685 rc = setup_obd_uuids(dir, dname, param);
688 rc = process_file(dir, dname, fname, param);
696 int llapi_find(char *path, struct obd_uuid *obduuid, int recursive,
697 int verbose, int quiet, int showfid)
699 struct find_param param;
702 memset(¶m, 0, sizeof(param));
703 param.recursive = recursive;
704 param.showfid = showfid;
705 param.verbose = verbose;
708 param.obduuid = malloc(sizeof(*obduuid));
709 if (param.obduuid == NULL) {
713 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
716 ret = prepare_find(¶m);
720 process_path(path, ¶m);
722 cleanup_find(¶m);
726 #define MAX_STRING_SIZE 128
727 #define DEVICES_LIST "/proc/fs/lustre/devices"
729 int llapi_target_check(int type_num, char **obd_type, char *dir)
731 char buf[MAX_STRING_SIZE];
732 FILE *fp = fopen(DEVICES_LIST, "r");
737 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
738 strerror(rc = errno));
742 while (fgets(buf, sizeof(buf), fp) != NULL) {
743 char *obd_type_name = NULL;
744 char *obd_name = NULL;
745 char rawbuf[OBD_MAX_IOCTL_BUFFER];
748 struct obd_ioctl_data datal = { 0, };
749 struct obd_statfs osfs_buffer;
751 while(bufp[0] == ' ')
754 for(i = 0; i < 3; i++) {
755 obd_type_name = strsep(&bufp, " ");
757 obd_name = strsep(&bufp, " ");
759 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
761 memset(bufl, 0, sizeof(rawbuf));
762 datal.ioc_pbuf1 = (char *)&osfs_buffer;
763 datal.ioc_plen1 = sizeof(osfs_buffer);
765 for (i = 0; i < type_num; i++)
766 if (strcmp(obd_type_name, obd_type[i]) == 0) {
767 datal.ioc_inlbuf1 = obd_name;
768 datal.ioc_inllen1 = strlen(obd_name) + 1;
770 rc = obd_ioctl_pack(&datal, &bufl, OBD_MAX_IOCTL_BUFFER);
772 fprintf(stderr, "internal buffer error packing\n");
776 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,
780 fprintf(stderr, "error: check %s: %s\n",
781 obd_name, strerror(rc = errno));
783 printf("%s active.\n",obd_name);
792 #undef MAX_STRING_SIZE
794 int llapi_catinfo(char *dir, char *keyword, char *node_name)
796 char raw[OBD_MAX_IOCTL_BUFFER];
797 char out[LLOG_CHUNK_SIZE];
799 struct obd_ioctl_data data;
804 sprintf(key, "%s", keyword);
805 memset(raw, 0, sizeof(buf));
806 memset(out, 0, sizeof(out));
807 data.ioc_inlbuf1 = key;
808 data.ioc_inllen1 = strlen(key) + 1;
810 data.ioc_inlbuf2 = node_name;
811 data.ioc_inllen2 = strlen(node_name) + 1;
813 data.ioc_pbuf1 = out;
814 data.ioc_plen1 = sizeof(out);
815 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
821 err_msg("open %s failed", dir);
825 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
827 err_msg("ioctl OBD_IOC_CATINFO failed");
829 fprintf(stdout, "%s", data.ioc_pbuf1);
835 int llapi_getfacl(char *dir, char *cmd)
837 struct ll_acl_ioctl_data data;
838 char out[LUSTRE_ACL_SIZE_MAX];
842 data.cmd_len = strlen(cmd) + 1;
844 data.res_len = sizeof(out);
847 fd = open(dir, O_RDONLY | O_DIRECTORY);
849 err_msg("can't open dir %s", dir);
853 rc = ioctl(fd, LL_IOC_GETFACL, &data);
855 err_msg("getfacl failed");
857 out[sizeof(out) - 1] = '\0';
867 int llapi_setfacl(char *dir, char *cmd)
869 struct ll_acl_ioctl_data data;
870 char out[LUSTRE_ACL_SIZE_MAX];
874 data.cmd_len = strlen(cmd) + 1;
876 data.res_len = sizeof(out);
879 fd = open(dir, O_RDONLY | O_DIRECTORY);
881 err_msg("can't open dir %s", dir);
885 rc = ioctl(fd, LL_IOC_SETFACL, &data);
887 err_msg("setfacl failed");
889 out[sizeof(out) - 1] = '\0';
899 int llapi_is_lustre_mnttype(char *type)
901 return (strcmp(type,"lustre") == 0 || strcmp(type,"lustre_lite") == 0);