Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[fs/lustre-release.git] / lustre / utils / lfind.c
index b88f548..847dd4f 100644 (file)
@@ -1,5 +1,3 @@
-#define _XOPEN_SOURCE 500
-
 #include <stdio.h>
 #include <getopt.h>
 #include <stdlib.h>
@@ -21,7 +19,7 @@
 
 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
 #define MAX_LOV_UUID_COUNT     1000
-#define OBD_NOT_FOUND          ((__u32)-1)
+#define OBD_NOT_FOUND          (-1)
 
 char *         cmd;
 struct option  longOpts[] = {
@@ -38,13 +36,11 @@ char *              usageMsg = "[ --obd <obd uuid> | --query ] <dir|file> ...";
 
 int            max_ost_count = MAX_LOV_UUID_COUNT;
 struct obd_uuid *      obduuid;
-__u32          obdcount;
-__u32          obdindex;
 char *         buf;
 int            buflen;
+struct obd_uuid *      uuids;
 struct obd_ioctl_data data;
 struct lov_desc desc;
-struct obd_uuid *      uuids;
 int            uuidslen;
 int            cfglen;
 struct lov_mds_md *lmm;
@@ -53,14 +49,7 @@ int          lmmlen;
 void   init();
 void   usage(FILE *stream);
 void   errMsg(char *fmt, ...);
-void   processPath(char *path);
-int    processFile(
-               const char *path,
-               const struct stat *sp,
-               int flag,
-               struct FTW *ftwp
-       );
-__u32  getobdindex(const char *path);
+void   processPath(const char *path);
 
 int
 main (int argc, char **argv) {
@@ -180,49 +169,100 @@ errMsg(char *fmt, ...)
 }
 
 void
-processPath(char *path)
-{
-       obdindex = OBD_NOT_FOUND;
-       nftw((const char *)path, processFile, 128, FTW_PHYS|FTW_MOUNT);
-}
-
-int
-processFile(const char *path, const struct stat *sp, int flag, struct FTW *ftwp)
+processPath(const char *path)
 {
        int fd;
-       int count;
        int rc;
        int i;
+       int obdindex;
+       int obdcount;
+       struct obd_uuid *uuidp;
 
-       if (flag != FTW_F)
-               return 0;
-
-       if (getobdindex(path) == OBD_NOT_FOUND && obdcount == 0) {
-               /* terminate nftw walking this tree */
-               return(1);
+       if (query || verbose && !obduuid) {
+               printf("%s\n", path);
        }
 
        if ((fd = open(path, O_RDONLY | O_LOV_DELAY_CREATE)) < 0) {
                errMsg("open \"%.20s\" failed.", path);
                perror("open");
+               return;
+       }
+
+       memset(&data, 0, sizeof(data));
+        data.ioc_inllen1 = sizeof(desc);
+        data.ioc_inlbuf1 = (char *)&desc;
+        data.ioc_inllen2 = uuidslen;
+        data.ioc_inlbuf2 = (char *)uuids;
+
+        memset(&desc, 0, sizeof(desc));
+        desc.ld_tgt_count = max_ost_count;
+
+        if (obd_ioctl_pack(&data, &buf, buflen)) {
+                errMsg("internal buffering error.");
+               exit(1);
+        }
+
+        rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
+        if (rc) {
+               if (errno == ENOTTY) {
+                       if (!obduuid) {
+                               printf("Not a regular file or not Lustre file.\n\n");
+                       }
+                       return;
+               }
+               errMsg("OBD_IOC_LOV_GET_CONFIG ioctl failed: %d.", errno);
+               perror("ioctl");
                exit(1);
+        }
+
+       if (obd_ioctl_unpack(&data, buf, buflen)) {
+               errMsg("Invalid reply from ioctl.");
+                exit(1);
+       }
+
+        obdcount = desc.ld_tgt_count;
+       if (obdcount == 0)
+               return;
+
+       obdindex = OBD_NOT_FOUND;
+
+       if (obduuid) {
+               for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
+                       if (strncmp((const char *)obduuid, (const char *)uuidp,
+                                   sizeof(*uuidp)) == 0) {
+                               obdindex = i;
+                       }
+               }
+
+               if (obdindex == OBD_NOT_FOUND)
+                       return;
+       } else  if (query || verbose) {
+               printf("OBDS:\n");
+               for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++)
+                       printf("%4d: %s\n", i, (char *)uuidp);
        }
 
        memset((void *)buf, 0, buflen);
        lmm->lmm_magic = LOV_MAGIC;
         lmm->lmm_ost_count = max_ost_count;
 
-       if ((rc = ioctl(fd, LL_IOC_LOV_GETSTRIPE, (void *)lmm)) < 0) {
-               errMsg("LL_IOC_LOV_GETSTRIPE ioctl failed.");
-               perror("ioctl");
-               return 0;
+       rc = ioctl(fd, LL_IOC_LOV_GETSTRIPE, (void *)lmm);
+       if (rc) {
+               if (errno == ENODATA) {
+                       if(!obduuid) {
+                               printf("Has no stripe information.\n\n");
+                       }
+               }
+               else {
+                       errMsg("LL_IOC_LOV_GETSTRIPE ioctl failed. %d", errno);
+                       perror("ioctl");
+               }
+               return;
        }
 
        close(fd);
 
-       if (query || verbose ||
-           (obdindex != OBD_NOT_FOUND &&
-            lmm->lmm_objects[obdindex].l_object_id))
+       if (obduuid && lmm->lmm_objects[obdindex].l_object_id)
                printf("%s\n", path);
 
        if (verbose) {
@@ -235,14 +275,12 @@ processFile(const char *path, const struct stat *sp, int flag, struct FTW *ftwp)
                printf("lmm_stripe_pattern: %d\n", lmm->lmm_magic & 0xf);
        }
 
-       count = lmm->lmm_ost_count;
-
        if (query || verbose) {
                long long oid;
                int ost = lmm->lmm_stripe_offset;
                int header = 1;
 
-               for (i = 0; i < count; i++, ost++) {
+               for (i = 0; i < lmm->lmm_ost_count; i++, ost++) {
                        ost %= lmm->lmm_ost_count;
                        if ((oid = lmm->lmm_objects[ost].l_object_id)) {
                                if (header) {
@@ -253,79 +291,6 @@ processFile(const char *path, const struct stat *sp, int flag, struct FTW *ftwp)
                                       ost, oid, obdindex == ost ? " *" : "");
                        }
                }
-
-               if (query)
-                       return(0);
+               printf("\n");
        }
-
-       return(0);
-}
-
-__u32
-getobdindex(const char *path)
-{
-       struct obd_uuid *uuidp;
-       int fd;
-       int rc;
-       int i;
-
-       if ((fd = open(path, O_RDONLY)) < 0) {
-               errMsg("open \"%.20s\" failed.", path);
-               perror("open");
-               exit(1);
-       }
-
-       memset(&data, 0, sizeof data);
-        data.ioc_inllen1 = sizeof(desc);
-        data.ioc_inlbuf1 = (char *)&desc;
-        data.ioc_inllen2 = uuidslen;
-        data.ioc_inlbuf2 = (char *)uuids;
-
-        memset(&desc, 0, sizeof(desc));
-        desc.ld_tgt_count = max_ost_count;
-
-        if (obd_ioctl_pack(&data, &buf, buflen)) {
-                errMsg("internal buffering error.");
-               exit(1);
-        }
-
-        rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
-        if (rc) {
-               errMsg("OBD_IOC_LOV_GET_CONFIG ioctl failed: %d.", errno);
-               perror("ioctl");
-                exit(1);
-        }
-
-       if (obd_ioctl_unpack(&data, buf, buflen)) {
-               errMsg("Invalid reply from ioctl.");
-                exit(1);
-       }
-
-       close(fd);
-
-        obdcount = desc.ld_tgt_count;
-
-       if (query || verbose) {
-               printf("OBDS:\n");
-               for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++)
-                       printf("%4d: %s\n", i, (char *)uuidp);
-
-               return(0);
-       }
-
-        for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
-               rc = strncmp((const char *)obduuid, (const char *)uuidp,
-                               sizeof(*uuidp));
-               if (rc == 0) {
-                       obdindex = i;
-                       break;
-               }
-       }
-
-       if (obdindex == OBD_NOT_FOUND) {
-               errMsg("obd UUID '%s' not found.", obduuid);
-               return(OBD_NOT_FOUND);
-       }
-
-       return(0);
 }