]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/libcephfs/reclaim.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / libcephfs / reclaim.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Tests for Ceph delegation handling
5 *
6 * (c) 2017, Jeff Layton <jlayton@redhat.com>
7 */
8
9 #include "gtest/gtest.h"
10 #include "include/cephfs/libcephfs.h"
11 #include "include/stat.h"
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <dirent.h>
18 #include <sys/xattr.h>
19 #include <sys/uio.h>
20 #include <libgen.h>
21 #include <stdlib.h>
22
23 #ifdef __linux__
24 #include <limits.h>
25 #endif
26
27
28 #include <map>
29 #include <vector>
30 #include <thread>
31 #include <atomic>
32
33 #define CEPHFS_RECLAIM_TIMEOUT 60
34
35 static int dying_client(int argc, char **argv)
36 {
37 struct ceph_mount_info *cmount;
38
39 /* Caller must pass in the uuid */
40 if (argc < 2)
41 return 1;
42
43 if (ceph_create(&cmount, nullptr) != 0)
44 return 1;
45
46 if (ceph_conf_read_file(cmount, nullptr) != 0)
47 return 1;
48
49 if (ceph_conf_parse_env(cmount, nullptr) != 0)
50 return 1;
51
52 if (ceph_init(cmount) != 0)
53 return 1;
54
55 ceph_set_session_timeout(cmount, CEPHFS_RECLAIM_TIMEOUT);
56
57 if (ceph_start_reclaim(cmount, argv[1], CEPH_RECLAIM_RESET) != -ENOENT)
58 return 1;
59
60 ceph_set_uuid(cmount, argv[1]);
61
62 if (ceph_mount(cmount, "/") != 0)
63 return 1;
64
65 Inode *root, *file;
66 if (ceph_ll_lookup_root(cmount, &root) != 0)
67 return 1;
68
69 Fh *fh;
70 struct ceph_statx stx;
71 UserPerm *perms = ceph_mount_perms(cmount);
72
73 if (ceph_ll_create(cmount, root, argv[1], 0666, O_RDWR|O_CREAT|O_EXCL,
74 &file, &fh, &stx, 0, 0, perms) != 0)
75 return 1;
76
77 return 0;
78 }
79
80 TEST(LibCephFS, ReclaimReset) {
81 pid_t pid;
82 char uuid[256];
83 const char *exe = "/proc/self/exe";
84
85 sprintf(uuid, "simplereclaim:%x", getpid());
86
87 pid = fork();
88 ASSERT_GE(pid, 0);
89 if (pid == 0) {
90 errno = 0;
91 execl(exe, exe, uuid, nullptr);
92 /* It won't be zero of course, which is sort of the point... */
93 ASSERT_EQ(errno, 0);
94 }
95
96 /* parent - wait for child to exit */
97 int ret;
98 pid_t wp = wait(&ret);
99 ASSERT_GE(wp, 0);
100 ASSERT_EQ(WIFEXITED(ret), true);
101 ASSERT_EQ(WEXITSTATUS(ret), 0);
102
103 struct ceph_mount_info *cmount;
104 ASSERT_EQ(ceph_create(&cmount, nullptr), 0);
105 ASSERT_EQ(ceph_conf_read_file(cmount, nullptr), 0);
106 ASSERT_EQ(0, ceph_conf_parse_env(cmount, nullptr));
107 ASSERT_EQ(ceph_init(cmount), 0);
108 ceph_set_session_timeout(cmount, CEPHFS_RECLAIM_TIMEOUT);
109 ASSERT_EQ(ceph_start_reclaim(cmount, uuid, CEPH_RECLAIM_RESET), 0);
110 ceph_set_uuid(cmount, uuid);
111 ASSERT_EQ(ceph_mount(cmount, "/"), 0);
112
113 Inode *root, *file;
114 ASSERT_EQ(ceph_ll_lookup_root(cmount, &root), 0);
115 UserPerm *perms = ceph_mount_perms(cmount);
116 struct ceph_statx stx;
117 ASSERT_EQ(ceph_ll_lookup(cmount, root, uuid, &file, &stx, 0, 0, perms), 0);
118 Fh *fh;
119 ASSERT_EQ(ceph_ll_open(cmount, file, O_WRONLY, &fh, perms), 0);
120
121 ceph_unmount(cmount);
122 ceph_release(cmount);
123 }
124
125 static int update_root_mode()
126 {
127 struct ceph_mount_info *admin;
128 int r = ceph_create(&admin, nullptr);
129 if (r < 0)
130 return r;
131 ceph_conf_read_file(admin, nullptr);
132 ceph_conf_parse_env(admin, nullptr);
133 ceph_conf_set(admin, "client_permissions", "false");
134 r = ceph_mount(admin, "/");
135 if (r < 0)
136 goto out;
137 r = ceph_chmod(admin, "/", 01777);
138 out:
139 ceph_shutdown(admin);
140 return r;
141 }
142
143 int main(int argc, char **argv)
144 {
145 int r = update_root_mode();
146 if (r < 0)
147 exit(1);
148
149 ::testing::InitGoogleTest(&argc, argv);
150
151 if (argc > 1)
152 return dying_client(argc, argv);
153
154 srand(getpid());
155
156 return RUN_ALL_TESTS();
157 }