{
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;
*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"
{
char file_path[PATH_MAX];
char filename[PATH_MAX];
+ int saved_errno = errno;
int fd;
int rnumber;
int rc;
- rnumber = random();
- if (idx == -1)
- snprintf(filename, sizeof(filename),
- LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
- else
- snprintf(filename, sizeof(filename),
- LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
+ do {
+ rnumber = random();
+ if (idx == -1)
+ snprintf(filename, sizeof(filename),
+ LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
+ else
+ snprintf(filename, sizeof(filename),
+ LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
+
+ rc = snprintf(file_path, sizeof(file_path),
+ "%s/%s", directory, filename);
+ if (rc >= sizeof(file_path))
+ return -E2BIG;
- rc = snprintf(file_path, sizeof(file_path),
- "%s/%s", directory, filename);
- if (rc >= sizeof(file_path))
- return -E2BIG;
+ fd = open(file_path,
+ O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
+ S_IRUSR | S_IWUSR);
+ } while (fd < 0 && errno == EEXIST);
- fd = open(file_path,
- O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
- S_IRUSR | S_IWUSR);
if (fd < 0) {
llapi_error(LLAPI_MSG_ERROR, errno,
"Cannot create volatile file '%s' in '%s'",
directory);
return -errno;
}
- /* unlink file in case this wasn't a Lustre filesystem, and the
- * magic volatile filename wasn't handled as intended. The effect
- * is the same. */
- unlink(file_path);
+
+ /* Unlink file in case this wasn't a Lustre filesystem and the
+ * magic volatile filename wasn't handled as intended. The
+ * effect is the same. If volatile open was supported then we
+ * expect unlink() to return -ENOENT. */
+ (void)unlink(file_path);
+
+ /* Since we are returning successfully we restore errno (and
+ * mask out possible EEXIST from open() and ENOENT from
+ * unlink(). */
+ errno = saved_errno;
return fd;
}