+
+/*
+ * Create a volatile file and open it for write:
+ * - file is created as a standard file in the directory
+ * - file does not appears in directory and directory mtime does not change
+ * - file is removed at close
+ * - file modes are rw-------, if user wants another one it must use fchmod()
+ * \param directory Directory where the file is created
+ * \param idx MDT index on which the file is created
+ * \param flags Std open flags
+ *
+ * \retval 0 on success.
+ * \retval -errno on error.
+ */
+int llapi_create_volatile_idx(char *directory, int idx, int mode)
+{
+ char file_path[PATH_MAX];
+ char filename[PATH_MAX];
+ int fd;
+ int random;
+ int rc;
+
+ fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0) {
+ llapi_error(LLAPI_MSG_ERROR, errno,
+ "Cannot open /dev/urandom\n");
+ return -errno;
+ }
+ rc = read(fd, &random, sizeof(random));
+ close(fd);
+ if (rc < sizeof(random)) {
+ llapi_error(LLAPI_MSG_ERROR, errno,
+ "Cannot read %d bytes from /dev/urandom\n",
+ sizeof(random));
+ return -errno;
+ }
+ if (idx == -1)
+ sprintf(filename, LUSTRE_VOLATILE_HDR"::%.4X", random);
+ else
+ sprintf(filename, LUSTRE_VOLATILE_IDX"%.4X", 0, random);
+
+ sprintf(file_path, "%s/%s", directory, filename);
+
+ fd = open(file_path, O_RDWR|O_CREAT|mode, S_IRUSR|S_IWUSR);
+ if (fd < 0) {
+ llapi_error(LLAPI_MSG_ERROR, errno,
+ "Cannot create volatile file %s in %s\n",
+ filename + LUSTRE_VOLATILE_HDR_LEN,
+ directory);
+ return -errno;
+ }
+ return fd;
+}
+
+