hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/tools/testing/selftests/filesystems/incfs/utils.c
....@@ -25,6 +25,45 @@
2525 #define __S_IFREG S_IFREG
2626 #endif
2727
28
+unsigned int rnd(unsigned int max, unsigned int *seed)
29
+{
30
+ return rand_r(seed) * ((uint64_t)max + 1) / RAND_MAX;
31
+}
32
+
33
+int remove_dir(const char *dir)
34
+{
35
+ int err = rmdir(dir);
36
+
37
+ if (err && errno == ENOTEMPTY) {
38
+ err = delete_dir_tree(dir);
39
+ if (err)
40
+ return err;
41
+ return 0;
42
+ }
43
+
44
+ if (err && errno != ENOENT)
45
+ return -errno;
46
+
47
+ return 0;
48
+}
49
+
50
+int drop_caches(void)
51
+{
52
+ int drop_caches =
53
+ open("/proc/sys/vm/drop_caches", O_WRONLY | O_CLOEXEC);
54
+ int i;
55
+
56
+ if (drop_caches == -1)
57
+ return -errno;
58
+ i = write(drop_caches, "3", 1);
59
+ close(drop_caches);
60
+
61
+ if (i != 1)
62
+ return -errno;
63
+
64
+ return 0;
65
+}
66
+
2867 int mount_fs(const char *mount_dir, const char *backing_dir,
2968 int read_timeout_ms)
3069 {
....@@ -77,6 +116,9 @@
77116 size_t size = sizeof(struct signature_blob) + strlen(add_data) + 1;
78117 struct signature_blob *sb = malloc(size);
79118
119
+ if (!sb)
120
+ return 0;
121
+
80122 *sb = (struct signature_blob){
81123 .version = INCFS_SIGNATURE_VERSION,
82124 .hash_section_size = sizeof(struct hash_section),
....@@ -87,7 +129,7 @@
87129 .salt_size = 0,
88130 .hash_size = SHA256_DIGEST_SIZE,
89131 },
90
- .signing_section_size = sizeof(uint32_t) + strlen(add_data) + 1,
132
+ .signing_section_size = strlen(add_data) + 1,
91133 };
92134
93135 memcpy(sb->hash_section.hash, root_hash, SHA256_DIGEST_SIZE);
....@@ -195,14 +237,28 @@
195237
196238 int open_log_file(const char *mount_dir)
197239 {
198
- char cmd_file[255];
199
- int cmd_fd;
240
+ char file[255];
241
+ int fd;
200242
201
- snprintf(cmd_file, ARRAY_SIZE(cmd_file), "%s/.log", mount_dir);
202
- cmd_fd = open(cmd_file, O_RDWR | O_CLOEXEC);
203
- if (cmd_fd < 0)
243
+ snprintf(file, ARRAY_SIZE(file), "%s/.log", mount_dir);
244
+ fd = open(file, O_RDWR | O_CLOEXEC);
245
+ if (fd < 0)
204246 perror("Can't open log file");
205
- return cmd_fd;
247
+ return fd;
248
+}
249
+
250
+int open_blocks_written_file(const char *mount_dir)
251
+{
252
+ char file[255];
253
+ int fd;
254
+
255
+ snprintf(file, ARRAY_SIZE(file),
256
+ "%s/%s", mount_dir, INCFS_BLOCKS_WRITTEN_FILENAME);
257
+ fd = open(file, O_RDONLY | O_CLOEXEC);
258
+
259
+ if (fd < 0)
260
+ perror("Can't open blocks_written file");
261
+ return fd;
206262 }
207263
208264 int wait_for_pending_reads(int fd, int timeout_ms,
....@@ -233,7 +289,35 @@
233289 return read_res / sizeof(*prs);
234290 }
235291
236
-char *concat_file_name(const char *dir, char *file)
292
+int wait_for_pending_reads2(int fd, int timeout_ms,
293
+ struct incfs_pending_read_info2 *prs, int prs_count)
294
+{
295
+ ssize_t read_res = 0;
296
+
297
+ if (timeout_ms > 0) {
298
+ int poll_res = 0;
299
+ struct pollfd pollfd = {
300
+ .fd = fd,
301
+ .events = POLLIN
302
+ };
303
+
304
+ poll_res = poll(&pollfd, 1, timeout_ms);
305
+ if (poll_res < 0)
306
+ return -errno;
307
+ if (poll_res == 0)
308
+ return 0;
309
+ if (!(pollfd.revents | POLLIN))
310
+ return 0;
311
+ }
312
+
313
+ read_res = read(fd, prs, prs_count * sizeof(*prs));
314
+ if (read_res < 0)
315
+ return -errno;
316
+
317
+ return read_res / sizeof(*prs);
318
+}
319
+
320
+char *concat_file_name(const char *dir, const char *file)
237321 {
238322 char full_name[FILENAME_MAX] = "";
239323