data.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/zalloc.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <asm/bug.h>
  13. #include <dirent.h>
  14. #include "data.h"
  15. #include "util.h" // rm_rf_perf_data()
  16. #include "debug.h"
  17. #include "header.h"
  18. #include <internal/lib.h>
  19. static void close_dir(struct perf_data_file *files, int nr)
  20. {
  21. while (--nr >= 0) {
  22. close(files[nr].fd);
  23. zfree(&files[nr].path);
  24. }
  25. free(files);
  26. }
  27. void perf_data__close_dir(struct perf_data *data)
  28. {
  29. close_dir(data->dir.files, data->dir.nr);
  30. }
  31. int perf_data__create_dir(struct perf_data *data, int nr)
  32. {
  33. struct perf_data_file *files = NULL;
  34. int i, ret;
  35. if (WARN_ON(!data->is_dir))
  36. return -EINVAL;
  37. files = zalloc(nr * sizeof(*files));
  38. if (!files)
  39. return -ENOMEM;
  40. data->dir.version = PERF_DIR_VERSION;
  41. data->dir.files = files;
  42. data->dir.nr = nr;
  43. for (i = 0; i < nr; i++) {
  44. struct perf_data_file *file = &files[i];
  45. ret = asprintf(&file->path, "%s/data.%d", data->path, i);
  46. if (ret < 0)
  47. goto out_err;
  48. ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
  49. if (ret < 0)
  50. goto out_err;
  51. file->fd = ret;
  52. }
  53. return 0;
  54. out_err:
  55. close_dir(files, i);
  56. return ret;
  57. }
  58. int perf_data__open_dir(struct perf_data *data)
  59. {
  60. struct perf_data_file *files = NULL;
  61. struct dirent *dent;
  62. int ret = -1;
  63. DIR *dir;
  64. int nr = 0;
  65. if (WARN_ON(!data->is_dir))
  66. return -EINVAL;
  67. /* The version is provided by DIR_FORMAT feature. */
  68. if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
  69. return -1;
  70. dir = opendir(data->path);
  71. if (!dir)
  72. return -EINVAL;
  73. while ((dent = readdir(dir)) != NULL) {
  74. struct perf_data_file *file;
  75. char path[PATH_MAX];
  76. struct stat st;
  77. snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
  78. if (stat(path, &st))
  79. continue;
  80. if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data", 4))
  81. continue;
  82. ret = -ENOMEM;
  83. file = realloc(files, (nr + 1) * sizeof(*files));
  84. if (!file)
  85. goto out_err;
  86. files = file;
  87. file = &files[nr++];
  88. file->path = strdup(path);
  89. if (!file->path)
  90. goto out_err;
  91. ret = open(file->path, O_RDONLY);
  92. if (ret < 0)
  93. goto out_err;
  94. file->fd = ret;
  95. file->size = st.st_size;
  96. }
  97. if (!files)
  98. return -EINVAL;
  99. data->dir.files = files;
  100. data->dir.nr = nr;
  101. return 0;
  102. out_err:
  103. close_dir(files, nr);
  104. return ret;
  105. }
  106. int perf_data__update_dir(struct perf_data *data)
  107. {
  108. int i;
  109. if (WARN_ON(!data->is_dir))
  110. return -EINVAL;
  111. for (i = 0; i < data->dir.nr; i++) {
  112. struct perf_data_file *file = &data->dir.files[i];
  113. struct stat st;
  114. if (fstat(file->fd, &st))
  115. return -1;
  116. file->size = st.st_size;
  117. }
  118. return 0;
  119. }
  120. static bool check_pipe(struct perf_data *data)
  121. {
  122. struct stat st;
  123. bool is_pipe = false;
  124. int fd = perf_data__is_read(data) ?
  125. STDIN_FILENO : STDOUT_FILENO;
  126. if (!data->path) {
  127. if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
  128. is_pipe = true;
  129. } else {
  130. if (!strcmp(data->path, "-"))
  131. is_pipe = true;
  132. }
  133. if (is_pipe)
  134. data->file.fd = fd;
  135. return data->is_pipe = is_pipe;
  136. }
  137. static int check_backup(struct perf_data *data)
  138. {
  139. struct stat st;
  140. if (perf_data__is_read(data))
  141. return 0;
  142. if (!stat(data->path, &st) && st.st_size) {
  143. char oldname[PATH_MAX];
  144. int ret;
  145. snprintf(oldname, sizeof(oldname), "%s.old",
  146. data->path);
  147. ret = rm_rf_perf_data(oldname);
  148. if (ret) {
  149. pr_err("Can't remove old data: %s (%s)\n",
  150. ret == -2 ?
  151. "Unknown file found" : strerror(errno),
  152. oldname);
  153. return -1;
  154. }
  155. if (rename(data->path, oldname)) {
  156. pr_err("Can't move data: %s (%s to %s)\n",
  157. strerror(errno),
  158. data->path, oldname);
  159. return -1;
  160. }
  161. }
  162. return 0;
  163. }
  164. static bool is_dir(struct perf_data *data)
  165. {
  166. struct stat st;
  167. if (stat(data->path, &st))
  168. return false;
  169. return (st.st_mode & S_IFMT) == S_IFDIR;
  170. }
  171. static int open_file_read(struct perf_data *data)
  172. {
  173. struct stat st;
  174. int fd;
  175. char sbuf[STRERR_BUFSIZE];
  176. fd = open(data->file.path, O_RDONLY);
  177. if (fd < 0) {
  178. int err = errno;
  179. pr_err("failed to open %s: %s", data->file.path,
  180. str_error_r(err, sbuf, sizeof(sbuf)));
  181. if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
  182. pr_err(" (try 'perf record' first)");
  183. pr_err("\n");
  184. return -err;
  185. }
  186. if (fstat(fd, &st) < 0)
  187. goto out_close;
  188. if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
  189. pr_err("File %s not owned by current user or root (use -f to override)\n",
  190. data->file.path);
  191. goto out_close;
  192. }
  193. if (!st.st_size) {
  194. pr_info("zero-sized data (%s), nothing to do!\n",
  195. data->file.path);
  196. goto out_close;
  197. }
  198. data->file.size = st.st_size;
  199. return fd;
  200. out_close:
  201. close(fd);
  202. return -1;
  203. }
  204. static int open_file_write(struct perf_data *data)
  205. {
  206. int fd;
  207. char sbuf[STRERR_BUFSIZE];
  208. fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
  209. S_IRUSR|S_IWUSR);
  210. if (fd < 0)
  211. pr_err("failed to open %s : %s\n", data->file.path,
  212. str_error_r(errno, sbuf, sizeof(sbuf)));
  213. return fd;
  214. }
  215. static int open_file(struct perf_data *data)
  216. {
  217. int fd;
  218. fd = perf_data__is_read(data) ?
  219. open_file_read(data) : open_file_write(data);
  220. if (fd < 0) {
  221. zfree(&data->file.path);
  222. return -1;
  223. }
  224. data->file.fd = fd;
  225. return 0;
  226. }
  227. static int open_file_dup(struct perf_data *data)
  228. {
  229. data->file.path = strdup(data->path);
  230. if (!data->file.path)
  231. return -ENOMEM;
  232. return open_file(data);
  233. }
  234. static int open_dir(struct perf_data *data)
  235. {
  236. int ret;
  237. /*
  238. * So far we open only the header, so we can read the data version and
  239. * layout.
  240. */
  241. if (asprintf(&data->file.path, "%s/header", data->path) < 0)
  242. return -1;
  243. if (perf_data__is_write(data) &&
  244. mkdir(data->path, S_IRWXU) < 0)
  245. return -1;
  246. ret = open_file(data);
  247. /* Cleanup whatever we managed to create so far. */
  248. if (ret && perf_data__is_write(data))
  249. rm_rf_perf_data(data->path);
  250. return ret;
  251. }
  252. int perf_data__open(struct perf_data *data)
  253. {
  254. if (check_pipe(data))
  255. return 0;
  256. if (!data->path)
  257. data->path = "perf.data";
  258. if (check_backup(data))
  259. return -1;
  260. if (perf_data__is_read(data))
  261. data->is_dir = is_dir(data);
  262. return perf_data__is_dir(data) ?
  263. open_dir(data) : open_file_dup(data);
  264. }
  265. void perf_data__close(struct perf_data *data)
  266. {
  267. if (perf_data__is_dir(data))
  268. perf_data__close_dir(data);
  269. zfree(&data->file.path);
  270. close(data->file.fd);
  271. }
  272. ssize_t perf_data_file__write(struct perf_data_file *file,
  273. void *buf, size_t size)
  274. {
  275. return writen(file->fd, buf, size);
  276. }
  277. ssize_t perf_data__write(struct perf_data *data,
  278. void *buf, size_t size)
  279. {
  280. return perf_data_file__write(&data->file, buf, size);
  281. }
  282. int perf_data__switch(struct perf_data *data,
  283. const char *postfix,
  284. size_t pos, bool at_exit,
  285. char **new_filepath)
  286. {
  287. int ret;
  288. if (check_pipe(data))
  289. return -EINVAL;
  290. if (perf_data__is_read(data))
  291. return -EINVAL;
  292. if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
  293. return -ENOMEM;
  294. /*
  295. * Only fire a warning, don't return error, continue fill
  296. * original file.
  297. */
  298. if (rename(data->path, *new_filepath))
  299. pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
  300. if (!at_exit) {
  301. close(data->file.fd);
  302. ret = perf_data__open(data);
  303. if (ret < 0)
  304. goto out;
  305. if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
  306. ret = -errno;
  307. pr_debug("Failed to lseek to %zu: %s",
  308. pos, strerror(errno));
  309. goto out;
  310. }
  311. }
  312. ret = data->file.fd;
  313. out:
  314. return ret;
  315. }
  316. unsigned long perf_data__size(struct perf_data *data)
  317. {
  318. u64 size = data->file.size;
  319. int i;
  320. if (!data->is_dir)
  321. return size;
  322. for (i = 0; i < data->dir.nr; i++) {
  323. struct perf_data_file *file = &data->dir.files[i];
  324. size += file->size;
  325. }
  326. return size;
  327. }