Whamcloud - gitweb
LU-7057 utils: use stronger flags when opening volatile files
[fs/lustre-release.git] / lustre / utils / lfs.c
index c58113d..c221e14 100644 (file)
@@ -623,9 +623,13 @@ static int lfs_migrate(char *name, __u64 migration_flags,
 {
        int                      fd = -1;
        int                      fdv = -1;
-       char                     volatile_file[PATH_MAX +
-                                               LUSTRE_VOLATILE_HDR_LEN + 4];
        char                     parent[PATH_MAX];
+       int                      mdt_index;
+       int                      random_value;
+       char                     volatile_file[sizeof(parent) +
+                                              LUSTRE_VOLATILE_HDR_LEN +
+                                              2 * sizeof(mdt_index) +
+                                              2 * sizeof(random_value) + 4];
        char                    *ptr;
        int                      rc;
        struct lov_user_md      *lum = NULL;
@@ -699,18 +703,29 @@ static int lfs_migrate(char *name, __u64 migration_flags,
                        *ptr = '\0';
        }
 
-       rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
-                     LUSTRE_VOLATILE_HDR);
-       if (rc >= sizeof(volatile_file)) {
-               rc = -E2BIG;
+       rc = llapi_file_fget_mdtidx(fd, &mdt_index);
+       if (rc < 0) {
+               fprintf(stderr, "%s: %s: cannot get MDT index: %s\n",
+                       progname, name, strerror(-rc));
                goto error;
        }
 
-       /* create, open a volatile file, use caching (ie no directio) */
-       /* exclusive create is not needed because volatile files cannot
-        * conflict on name by construction */
-       fdv = llapi_file_open_param(volatile_file, O_CREAT | O_WRONLY, 0644,
-                                   param);
+       do {
+               random_value = random();
+               rc = snprintf(volatile_file, sizeof(volatile_file),
+                             "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
+                             mdt_index, random_value);
+               if (rc >= sizeof(volatile_file)) {
+                       rc = -E2BIG;
+                       goto error;
+               }
+
+               /* create, open a volatile file, use caching (ie no directio) */
+               fdv = llapi_file_open_param(volatile_file,
+                               O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW,
+                                           S_IRUSR | S_IWUSR, param);
+       } while (fdv == -EEXIST);
+
        if (fdv < 0) {
                rc = fdv;
                fprintf(stderr, "%s: %s: cannot create volatile file in"