1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "gtest/gtest.h"
16 #include "include/cephfs/libcephfs.h"
17 #include "include/stat.h"
21 #include <sys/types.h>
24 #include <sys/xattr.h>
27 #include <sys/resource.h>
37 TEST(LibCephFS
, OpenEmptyComponent
) {
39 pid_t mypid
= getpid();
40 struct ceph_mount_info
*cmount
;
41 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
42 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
43 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
44 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
47 sprintf(c_dir
, "/open_test_%d", mypid
);
48 struct ceph_dir_result
*dirp
;
50 ASSERT_EQ(0, ceph_mkdirs(cmount
, c_dir
, 0777));
52 ASSERT_EQ(0, ceph_opendir(cmount
, c_dir
, &dirp
));
55 sprintf(c_path
, "/open_test_%d//created_file_%d", mypid
, mypid
);
56 int fd
= ceph_open(cmount
, c_path
, O_RDONLY
|O_CREAT
, 0666);
59 ASSERT_EQ(0, ceph_close(cmount
, fd
));
60 ASSERT_EQ(0, ceph_closedir(cmount
, dirp
));
61 ceph_shutdown(cmount
);
63 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
64 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
65 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
67 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
69 fd
= ceph_open(cmount
, c_path
, O_RDONLY
, 0666);
72 ASSERT_EQ(0, ceph_close(cmount
, fd
));
73 ceph_shutdown(cmount
);
76 TEST(LibCephFS
, OpenReadWrite
) {
77 struct ceph_mount_info
*cmount
;
78 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
79 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
80 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
81 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
84 sprintf(c_path
, "test_open_rdwr_%d", getpid());
85 int fd
= ceph_open(cmount
, c_path
, O_WRONLY
|O_CREAT
, 0666);
88 const char *out_buf
= "hello world";
89 size_t size
= strlen(out_buf
);
91 ASSERT_EQ(ceph_write(cmount
, fd
, out_buf
, size
, 0), (int)size
);
92 ASSERT_EQ(ceph_read(cmount
, fd
, in_buf
, sizeof(in_buf
), 0), -EBADF
);
93 ASSERT_EQ(0, ceph_close(cmount
, fd
));
95 fd
= ceph_open(cmount
, c_path
, O_RDONLY
, 0);
97 ASSERT_EQ(ceph_write(cmount
, fd
, out_buf
, size
, 0), -EBADF
);
98 ASSERT_EQ(ceph_read(cmount
, fd
, in_buf
, sizeof(in_buf
), 0), (int)size
);
99 ASSERT_EQ(0, ceph_close(cmount
, fd
));
101 fd
= ceph_open(cmount
, c_path
, O_RDWR
, 0);
103 ASSERT_EQ(ceph_write(cmount
, fd
, out_buf
, size
, 0), (int)size
);
104 ASSERT_EQ(ceph_read(cmount
, fd
, in_buf
, sizeof(in_buf
), 0), (int)size
);
105 ASSERT_EQ(0, ceph_close(cmount
, fd
));
107 ceph_shutdown(cmount
);
110 TEST(LibCephFS
, MountNonExist
) {
112 struct ceph_mount_info
*cmount
;
114 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
115 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
116 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
117 ASSERT_NE(0, ceph_mount(cmount
, "/non-exist"));
118 ceph_shutdown(cmount
);
121 TEST(LibCephFS
, MountDouble
) {
123 struct ceph_mount_info
*cmount
;
125 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
126 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
127 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
128 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
129 ASSERT_EQ(-EISCONN
, ceph_mount(cmount
, "/"));
130 ceph_shutdown(cmount
);
133 TEST(LibCephFS
, MountRemount
) {
135 struct ceph_mount_info
*cmount
;
137 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
138 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
139 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
141 CephContext
*cct
= ceph_get_mount_context(cmount
);
142 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
143 ASSERT_EQ(0, ceph_unmount(cmount
));
145 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
146 ASSERT_EQ(cct
, ceph_get_mount_context(cmount
));
148 ceph_shutdown(cmount
);
151 TEST(LibCephFS
, UnmountUnmounted
) {
153 struct ceph_mount_info
*cmount
;
155 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
156 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
157 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
158 ASSERT_EQ(-ENOTCONN
, ceph_unmount(cmount
));
159 ceph_shutdown(cmount
);
162 TEST(LibCephFS
, ReleaseUnmounted
) {
164 struct ceph_mount_info
*cmount
;
166 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
167 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
168 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
169 ASSERT_EQ(0, ceph_release(cmount
));
172 TEST(LibCephFS
, ReleaseMounted
) {
174 struct ceph_mount_info
*cmount
;
176 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
177 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
178 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
179 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
180 ASSERT_EQ(-EISCONN
, ceph_release(cmount
));
181 ASSERT_EQ(0, ceph_unmount(cmount
));
182 ASSERT_EQ(0, ceph_release(cmount
));
185 TEST(LibCephFS
, UnmountRelease
) {
187 struct ceph_mount_info
*cmount
;
189 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
190 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
191 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
192 ASSERT_EQ(0, ceph_mount(cmount
, "/"));
193 ASSERT_EQ(0, ceph_unmount(cmount
));
194 ASSERT_EQ(0, ceph_release(cmount
));
197 TEST(LibCephFS
, Mount
) {
198 struct ceph_mount_info
*cmount
;
199 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
200 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
201 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
202 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
203 ceph_shutdown(cmount
);
205 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
206 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
207 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
208 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
209 ceph_shutdown(cmount
);
212 TEST(LibCephFS
, OpenLayout
) {
213 struct ceph_mount_info
*cmount
;
214 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
215 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
216 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
217 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
220 char test_layout_file
[256];
221 sprintf(test_layout_file
, "test_layout_%d_b", getpid());
222 int fd
= ceph_open_layout(cmount
, test_layout_file
, O_CREAT
|O_WRONLY
, 0666, (1<<20), 7, (1<<20), NULL
);
225 ASSERT_LT(0, ceph_get_file_pool_name(cmount
, fd
, poolname
, sizeof(poolname
)));
226 ASSERT_LT(0, ceph_get_file_pool_name(cmount
, fd
, poolname
, 0));
228 /* on already-written file (ENOTEMPTY) */
229 ceph_write(cmount
, fd
, "hello world", 11, 0);
230 ceph_close(cmount
, fd
);
234 sprintf(xattrk
, "ceph.file.layout.stripe_unit");
235 sprintf(xattrv
, "65536");
236 ASSERT_EQ(-ENOTEMPTY
, ceph_setxattr(cmount
, test_layout_file
, xattrk
, (void *)xattrv
, 5, 0));
239 sprintf(test_layout_file
, "test_layout_%d_c", getpid());
240 fd
= ceph_open_layout(cmount
, test_layout_file
, O_CREAT
, 0666, (1<<20), 1, 19, NULL
);
241 ASSERT_EQ(fd
, -EINVAL
);
244 sprintf(test_layout_file
, "test_layout_%d_d", getpid());
245 fd
= ceph_open_layout(cmount
, test_layout_file
, O_CREAT
, 0666, (1<<20), 7, (1<<20), poolname
);
247 ceph_close(cmount
, fd
);
249 /* with metadata pool (invalid) */
250 sprintf(test_layout_file
, "test_layout_%d_e", getpid());
251 fd
= ceph_open_layout(cmount
, test_layout_file
, O_CREAT
, 0666, (1<<20), 7, (1<<20), "metadata");
252 ASSERT_EQ(fd
, -EINVAL
);
254 /* with metadata pool (does not exist) */
255 sprintf(test_layout_file
, "test_layout_%d_f", getpid());
256 fd
= ceph_open_layout(cmount
, test_layout_file
, O_CREAT
, 0666, (1<<20), 7, (1<<20), "asdfjasdfjasdf");
257 ASSERT_EQ(fd
, -EINVAL
);
259 ceph_shutdown(cmount
);
262 TEST(LibCephFS
, DirLs
) {
264 pid_t mypid
= getpid();
266 struct ceph_mount_info
*cmount
;
267 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
268 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
269 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
270 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
272 struct ceph_dir_result
*ls_dir
= NULL
;
274 sprintf(foostr
, "dir_ls%d", mypid
);
275 ASSERT_EQ(ceph_opendir(cmount
, foostr
, &ls_dir
), -ENOENT
);
277 ASSERT_EQ(ceph_mkdir(cmount
, foostr
, 0777), 0);
278 struct ceph_statx stx
;
279 ASSERT_EQ(ceph_statx(cmount
, foostr
, &stx
, 0, 0), 0);
280 ASSERT_NE(S_ISDIR(stx
.stx_mode
), 0);
283 sprintf(barstr
, "dir_ls2%d", mypid
);
284 ASSERT_EQ(ceph_statx(cmount
, barstr
, &stx
, 0, AT_SYMLINK_NOFOLLOW
), -ENOENT
);
286 // insert files into directory and test open
288 int i
= 0, r
= rand() % 4096;
289 if (getenv("LIBCEPHFS_RAND")) {
290 r
= atoi(getenv("LIBCEPHFS_RAND"));
292 printf("rand: %d\n", r
);
295 sprintf(bazstr
, "dir_ls%d/dirf%d", mypid
, i
);
296 int fd
= ceph_open(cmount
, bazstr
, O_CREAT
|O_RDONLY
, 0666);
298 ASSERT_EQ(ceph_close(cmount
, fd
), 0);
300 // set file sizes for readdirplus
301 ceph_truncate(cmount
, bazstr
, i
);
304 ASSERT_EQ(ceph_opendir(cmount
, foostr
, &ls_dir
), 0);
306 // not guaranteed to get . and .. first, but its a safe assumption in this case
307 struct dirent
*result
= ceph_readdir(cmount
, ls_dir
);
308 ASSERT_TRUE(result
!= NULL
);
309 ASSERT_STREQ(result
->d_name
, ".");
310 result
= ceph_readdir(cmount
, ls_dir
);
311 ASSERT_TRUE(result
!= NULL
);
312 ASSERT_STREQ(result
->d_name
, "..");
314 std::vector
<std::string
> entries
;
315 std::map
<std::string
, int64_t> offset_map
;
316 int64_t offset
= ceph_telldir(cmount
, ls_dir
);
317 for(i
= 0; i
< r
; ++i
) {
318 result
= ceph_readdir(cmount
, ls_dir
);
319 ASSERT_TRUE(result
!= NULL
);
320 entries
.push_back(result
->d_name
);
321 offset_map
[result
->d_name
] = offset
;
322 offset
= ceph_telldir(cmount
, ls_dir
);
325 ASSERT_TRUE(ceph_readdir(cmount
, ls_dir
) == NULL
);
326 offset
= ceph_telldir(cmount
, ls_dir
);
328 ASSERT_EQ(offset_map
.size(), entries
.size());
329 for(i
= 0; i
< r
; ++i
) {
330 sprintf(bazstr
, "dirf%d", i
);
331 ASSERT_TRUE(offset_map
.count(bazstr
) == 1);
335 ceph_seekdir(cmount
, ls_dir
, offset
);
336 ASSERT_TRUE(ceph_readdir(cmount
, ls_dir
) == NULL
);
338 for (auto p
= offset_map
.begin(); p
!= offset_map
.end(); ++p
) {
339 ceph_seekdir(cmount
, ls_dir
, p
->second
);
340 result
= ceph_readdir(cmount
, ls_dir
);
341 ASSERT_TRUE(result
!= NULL
);
342 std::string
d_name(result
->d_name
);
343 ASSERT_EQ(p
->first
, d_name
);
347 ceph_rewinddir(cmount
, ls_dir
);
349 result
= ceph_readdir(cmount
, ls_dir
);
350 ASSERT_TRUE(result
!= NULL
);
351 ASSERT_STREQ(result
->d_name
, ".");
352 result
= ceph_readdir(cmount
, ls_dir
);
353 ASSERT_TRUE(result
!= NULL
);
354 ASSERT_STREQ(result
->d_name
, "..");
356 ceph_rewinddir(cmount
, ls_dir
);
358 int t
= ceph_telldir(cmount
, ls_dir
);
361 ASSERT_TRUE(ceph_readdir(cmount
, ls_dir
) != NULL
);
363 // test seekdir - move back to the beginning
364 ceph_seekdir(cmount
, ls_dir
, t
);
367 struct dirent
*getdents_entries
;
368 size_t getdents_entries_len
= (r
+ 2) * sizeof(*getdents_entries
);
369 getdents_entries
= (struct dirent
*)malloc(getdents_entries_len
);
372 std::vector
<std::string
> found
;
374 int len
= ceph_getdents(cmount
, ls_dir
, (char *)getdents_entries
, getdents_entries_len
);
378 ASSERT_TRUE((len
% sizeof(*getdents_entries
)) == 0);
379 int n
= len
/ sizeof(*getdents_entries
);
382 ASSERT_STREQ(getdents_entries
[0].d_name
, ".");
383 ASSERT_STREQ(getdents_entries
[1].d_name
, "..");
389 for(; j
< n
; ++i
, ++j
) {
390 const char *name
= getdents_entries
[j
].d_name
;
391 found
.push_back(name
);
394 ASSERT_EQ(found
, entries
);
395 free(getdents_entries
);
398 ceph_rewinddir(cmount
, ls_dir
);
400 result
= ceph_readdir(cmount
, ls_dir
);
401 ASSERT_TRUE(result
!= NULL
);
402 ASSERT_STREQ(result
->d_name
, ".");
403 result
= ceph_readdir(cmount
, ls_dir
);
404 ASSERT_TRUE(result
!= NULL
);
405 ASSERT_STREQ(result
->d_name
, "..");
410 int len
= ceph_readdir_r(cmount
, ls_dir
, &rdent
);
414 found
.push_back(rdent
.d_name
);
416 ASSERT_EQ(found
, entries
);
419 ceph_rewinddir(cmount
, ls_dir
);
421 result
= ceph_readdir(cmount
, ls_dir
);
422 ASSERT_TRUE(result
!= NULL
);
423 ASSERT_STREQ(result
->d_name
, ".");
424 result
= ceph_readdir(cmount
, ls_dir
);
425 ASSERT_TRUE(result
!= NULL
);
426 ASSERT_STREQ(result
->d_name
, "..");
431 struct ceph_statx stx
;
432 int len
= ceph_readdirplus_r(cmount
, ls_dir
, &rdent
, &stx
,
433 CEPH_STATX_SIZE
, AT_NO_ATTR_SYNC
, NULL
);
437 const char *name
= rdent
.d_name
;
438 found
.push_back(name
);
440 sscanf(name
, "dirf%d", &size
);
441 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_SIZE
);
442 ASSERT_EQ(stx
.stx_size
, (size_t)size
);
443 ASSERT_EQ(stx
.stx_ino
, rdent
.d_ino
);
444 //ASSERT_EQ(st.st_mode, (mode_t)0666);
446 ASSERT_EQ(found
, entries
);
448 ASSERT_EQ(ceph_closedir(cmount
, ls_dir
), 0);
450 ceph_shutdown(cmount
);
453 TEST(LibCephFS
, ManyNestedDirs
) {
454 struct ceph_mount_info
*cmount
;
455 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
456 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
457 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
458 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
460 const char *many_path
= "a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a";
461 ASSERT_EQ(ceph_mkdirs(cmount
, many_path
, 0755), 0);
466 ASSERT_EQ(ceph_chdir(cmount
, "a"), 0);
468 struct ceph_dir_result
*dirp
;
469 ASSERT_EQ(ceph_opendir(cmount
, "a", &dirp
), 0);
470 struct dirent
*dent
= ceph_readdir(cmount
, dirp
);
471 ASSERT_TRUE(dent
!= NULL
);
472 ASSERT_STREQ(dent
->d_name
, ".");
473 dent
= ceph_readdir(cmount
, dirp
);
474 ASSERT_TRUE(dent
!= NULL
);
475 ASSERT_STREQ(dent
->d_name
, "..");
476 dent
= ceph_readdir(cmount
, dirp
);
477 ASSERT_TRUE(dent
!= NULL
);
478 ASSERT_STREQ(dent
->d_name
, "a");
479 ASSERT_EQ(ceph_closedir(cmount
, dirp
), 0);
482 ASSERT_STREQ(ceph_getcwd(cmount
), "/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a");
484 ASSERT_EQ(ceph_chdir(cmount
, "a/a/a"), 0);
486 for(i
= 0; i
< 39; ++i
) {
487 ASSERT_EQ(ceph_chdir(cmount
, ".."), 0);
488 ASSERT_EQ(ceph_rmdir(cmount
, "a"), 0);
491 ASSERT_EQ(ceph_chdir(cmount
, "/"), 0);
493 ASSERT_EQ(ceph_rmdir(cmount
, "a/a/a"), 0);
495 ceph_shutdown(cmount
);
498 TEST(LibCephFS
, Xattrs
) {
499 struct ceph_mount_info
*cmount
;
500 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
501 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
502 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
503 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
505 char test_xattr_file
[256];
506 sprintf(test_xattr_file
, "test_xattr_%d", getpid());
507 int fd
= ceph_open(cmount
, test_xattr_file
, O_CREAT
, 0666);
513 for(; i
< 'a'+26; ++i
) {
514 sprintf(xattrk
, "user.test_xattr_%c", i
);
515 int len
= sprintf(xattrv
, "testxattr%c", i
);
516 ASSERT_EQ(ceph_setxattr(cmount
, test_xattr_file
, xattrk
, (void *) xattrv
, len
, XATTR_CREATE
), 0);
519 char xattrlist
[128*26];
520 int len
= ceph_listxattr(cmount
, test_xattr_file
, xattrlist
, sizeof(xattrlist
));
525 // skip/ignore the dir layout
526 if (strcmp(p
, "ceph.dir.layout") == 0 ||
527 strcmp(p
, "ceph.file.layout") == 0) {
528 len
-= strlen(p
) + 1;
533 sprintf(xattrk
, "user.test_xattr_%c", i
);
534 ASSERT_STREQ(p
, xattrk
);
537 std::cout
<< "getting attr " << p
<< std::endl
;
538 int alen
= ceph_getxattr(cmount
, test_xattr_file
, p
, (void *) gxattrv
, 128);
540 sprintf(xattrv
, "testxattr%c", i
);
541 ASSERT_TRUE(!strncmp(xattrv
, gxattrv
, alen
));
551 for(i
= 'a'; i
< 'a'+26; ++i
) {
552 sprintf(xattrk
, "user.test_xattr_%c", i
);
553 ASSERT_EQ(ceph_removexattr(cmount
, test_xattr_file
, xattrk
), 0);
556 ceph_close(cmount
, fd
);
557 ceph_shutdown(cmount
);
561 TEST(LibCephFS
, Xattrs_ll
) {
562 struct ceph_mount_info
*cmount
;
563 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
564 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
565 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
566 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
568 char test_xattr_file
[256];
569 sprintf(test_xattr_file
, "test_xattr_%d", getpid());
570 int fd
= ceph_open(cmount
, test_xattr_file
, O_CREAT
, 0666);
572 ceph_close(cmount
, fd
);
575 Inode
*existent_file_handle
= NULL
;
577 int res
= ceph_ll_lookup_root(cmount
, &root
);
580 UserPerm
*perms
= ceph_mount_perms(cmount
);
581 struct ceph_statx stx
;
583 res
= ceph_ll_lookup(cmount
, root
, test_xattr_file
, &existent_file_handle
,
587 const char *valid_name
= "user.attrname";
588 const char *value
= "attrvalue";
589 char value_buf
[256] = { 0 };
591 res
= ceph_ll_setxattr(cmount
, existent_file_handle
, valid_name
, value
, strlen(value
), 0, perms
);
594 res
= ceph_ll_getxattr(cmount
, existent_file_handle
, valid_name
, value_buf
, 256, perms
);
595 ASSERT_EQ(res
, (int)strlen(value
));
597 value_buf
[res
] = '\0';
598 ASSERT_STREQ(value_buf
, value
);
600 ceph_shutdown(cmount
);
603 TEST(LibCephFS
, LstatSlashdot
) {
604 struct ceph_mount_info
*cmount
;
605 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
606 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
607 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
608 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
610 struct ceph_statx stx
;
611 ASSERT_EQ(ceph_statx(cmount
, "/.", &stx
, 0, AT_SYMLINK_NOFOLLOW
), 0);
612 ASSERT_EQ(ceph_statx(cmount
, ".", &stx
, 0, AT_SYMLINK_NOFOLLOW
), 0);
614 ceph_shutdown(cmount
);
617 TEST(LibCephFS
, DoubleChmod
) {
619 struct ceph_mount_info
*cmount
;
620 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
621 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
622 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
623 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
626 sprintf(test_file
, "test_perms_%d", getpid());
628 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
632 const char *bytes
= "foobarbaz";
633 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), (int)strlen(bytes
));
635 ceph_close(cmount
, fd
);
637 // set perms to read but can't write
638 ASSERT_EQ(ceph_chmod(cmount
, test_file
, 0400), 0);
640 fd
= ceph_open(cmount
, test_file
, O_RDWR
, 0);
641 ASSERT_EQ(fd
, -EACCES
);
643 fd
= ceph_open(cmount
, test_file
, O_RDONLY
, 0);
647 int ret
= ceph_read(cmount
, fd
, buf
, 100, 0);
648 ASSERT_EQ(ret
, (int)strlen(bytes
));
650 ASSERT_STREQ(buf
, bytes
);
652 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), -EBADF
);
654 ceph_close(cmount
, fd
);
656 // reset back to writeable
657 ASSERT_EQ(ceph_chmod(cmount
, test_file
, 0600), 0);
659 // ensure perms are correct
660 struct ceph_statx stx
;
661 ASSERT_EQ(ceph_statx(cmount
, test_file
, &stx
, CEPH_STATX_MODE
, AT_SYMLINK_NOFOLLOW
), 0);
662 ASSERT_EQ(stx
.stx_mode
, 0100600U);
664 fd
= ceph_open(cmount
, test_file
, O_RDWR
, 0);
667 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), (int)strlen(bytes
));
668 ceph_close(cmount
, fd
);
670 ceph_shutdown(cmount
);
673 TEST(LibCephFS
, Fchmod
) {
674 struct ceph_mount_info
*cmount
;
675 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
676 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
677 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
678 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
681 sprintf(test_file
, "test_perms_%d", getpid());
683 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
687 const char *bytes
= "foobarbaz";
688 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), (int)strlen(bytes
));
690 // set perms to read but can't write
691 ASSERT_EQ(ceph_fchmod(cmount
, fd
, 0400), 0);
694 int ret
= ceph_read(cmount
, fd
, buf
, 100, 0);
695 ASSERT_EQ(ret
, (int)strlen(bytes
));
697 ASSERT_STREQ(buf
, bytes
);
699 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), (int)strlen(bytes
));
701 ceph_close(cmount
, fd
);
703 ASSERT_EQ(ceph_open(cmount
, test_file
, O_RDWR
, 0), -EACCES
);
705 // reset back to writeable
706 ASSERT_EQ(ceph_chmod(cmount
, test_file
, 0600), 0);
708 fd
= ceph_open(cmount
, test_file
, O_RDWR
, 0);
711 ASSERT_EQ(ceph_write(cmount
, fd
, bytes
, strlen(bytes
), 0), (int)strlen(bytes
));
712 ceph_close(cmount
, fd
);
714 ceph_shutdown(cmount
);
717 TEST(LibCephFS
, Fchown
) {
718 struct ceph_mount_info
*cmount
;
719 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
720 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
721 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
722 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
725 sprintf(test_file
, "test_fchown_%d", getpid());
727 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
730 // set perms to readable and writeable only by owner
731 ASSERT_EQ(ceph_fchmod(cmount
, fd
, 0600), 0);
733 // change ownership to nobody -- we assume nobody exists and id is always 65534
734 ASSERT_EQ(ceph_conf_set(cmount
, "client_permissions", "0"), 0);
735 ASSERT_EQ(ceph_fchown(cmount
, fd
, 65534, 65534), 0);
736 ASSERT_EQ(ceph_conf_set(cmount
, "client_permissions", "1"), 0);
738 ceph_close(cmount
, fd
);
740 fd
= ceph_open(cmount
, test_file
, O_RDWR
, 0);
741 ASSERT_EQ(fd
, -EACCES
);
743 ceph_shutdown(cmount
);
746 #if defined(__linux__) && defined(O_PATH)
747 TEST(LibCephFS
, FlagO_PATH
) {
748 struct ceph_mount_info
*cmount
;
750 ASSERT_EQ(0, ceph_create(&cmount
, NULL
));
751 ASSERT_EQ(0, ceph_conf_read_file(cmount
, NULL
));
752 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
753 ASSERT_EQ(0, ceph_mount(cmount
, NULL
));
755 char test_file
[PATH_MAX
];
756 sprintf(test_file
, "test_oflag_%d", getpid());
758 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
|O_PATH
, 0666);
759 ASSERT_EQ(-ENOENT
, fd
);
761 fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
763 ASSERT_EQ(0, ceph_close(cmount
, fd
));
765 // ok, the file has been created. perform real checks now
766 fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
|O_PATH
, 0666);
770 ASSERT_EQ(-EBADF
, ceph_read(cmount
, fd
, buf
, sizeof(buf
), 0));
771 ASSERT_EQ(-EBADF
, ceph_write(cmount
, fd
, buf
, sizeof(buf
), 0));
773 // set perms to readable and writeable only by owner
774 ASSERT_EQ(-EBADF
, ceph_fchmod(cmount
, fd
, 0600));
776 // change ownership to nobody -- we assume nobody exists and id is always 65534
777 ASSERT_EQ(-EBADF
, ceph_fchown(cmount
, fd
, 65534, 65534));
780 ASSERT_EQ(-EBADF
, ceph_fsync(cmount
, fd
, false));
782 struct ceph_statx stx
;
783 ASSERT_EQ(0, ceph_fstatx(cmount
, fd
, &stx
, 0, 0));
785 ASSERT_EQ(0, ceph_close(cmount
, fd
));
786 ceph_shutdown(cmount
);
790 TEST(LibCephFS
, Symlinks
) {
791 struct ceph_mount_info
*cmount
;
792 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
793 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
794 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
795 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
798 sprintf(test_file
, "test_symlinks_%d", getpid());
800 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
803 ceph_close(cmount
, fd
);
805 char test_symlink
[256];
806 sprintf(test_symlink
, "test_symlinks_sym_%d", getpid());
808 ASSERT_EQ(ceph_symlink(cmount
, test_file
, test_symlink
), 0);
810 // test the O_NOFOLLOW case
811 fd
= ceph_open(cmount
, test_symlink
, O_NOFOLLOW
, 0);
812 ASSERT_EQ(fd
, -ELOOP
);
814 // stat the original file
815 struct ceph_statx stx_orig
;
816 ASSERT_EQ(ceph_statx(cmount
, test_file
, &stx_orig
, CEPH_STATX_ALL_STATS
, 0), 0);
818 struct ceph_statx stx_symlink_orig
;
819 ASSERT_EQ(ceph_statx(cmount
, test_symlink
, &stx_symlink_orig
, CEPH_STATX_ALL_STATS
, 0), 0);
820 // ensure the statx bufs are equal
821 ASSERT_EQ(memcmp(&stx_orig
, &stx_symlink_orig
, sizeof(stx_orig
)), 0);
823 sprintf(test_file
, "/test_symlinks_abs_%d", getpid());
825 fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
828 ceph_close(cmount
, fd
);
830 sprintf(test_symlink
, "/test_symlinks_abs_sym_%d", getpid());
832 ASSERT_EQ(ceph_symlink(cmount
, test_file
, test_symlink
), 0);
834 // stat the original file
835 ASSERT_EQ(ceph_statx(cmount
, test_file
, &stx_orig
, CEPH_STATX_ALL_STATS
, 0), 0);
837 ASSERT_EQ(ceph_statx(cmount
, test_symlink
, &stx_symlink_orig
, CEPH_STATX_ALL_STATS
, 0), 0);
838 // ensure the statx bufs are equal
839 ASSERT_TRUE(!memcmp(&stx_orig
, &stx_symlink_orig
, sizeof(stx_orig
)));
842 ASSERT_EQ(ceph_statx(cmount
, test_symlink
, &stx_orig
, CEPH_STATX_ALL_STATS
, AT_SYMLINK_NOFOLLOW
), 0);
843 ASSERT_TRUE(S_ISLNK(stx_orig
.stx_mode
));
845 ceph_shutdown(cmount
);
848 TEST(LibCephFS
, DirSyms
) {
849 struct ceph_mount_info
*cmount
;
850 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
851 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
852 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
853 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
856 sprintf(test_dir1
, "dir1_symlinks_%d", getpid());
858 ASSERT_EQ(ceph_mkdir(cmount
, test_dir1
, 0700), 0);
860 char test_symdir
[256];
861 sprintf(test_symdir
, "symdir_symlinks_%d", getpid());
863 ASSERT_EQ(ceph_symlink(cmount
, test_dir1
, test_symdir
), 0);
866 sprintf(test_file
, "/symdir_symlinks_%d/test_symdir_file", getpid());
867 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0600);
869 ceph_close(cmount
, fd
);
871 struct ceph_statx stx
;
872 ASSERT_EQ(ceph_statx(cmount
, test_file
, &stx
, 0, AT_SYMLINK_NOFOLLOW
), 0);
874 // ensure that its a file not a directory we get back
875 ASSERT_TRUE(S_ISREG(stx
.stx_mode
));
877 ceph_shutdown(cmount
);
880 TEST(LibCephFS
, LoopSyms
) {
881 struct ceph_mount_info
*cmount
;
882 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
883 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
884 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
885 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
888 sprintf(test_dir1
, "dir1_loopsym_%d", getpid());
890 ASSERT_EQ(ceph_mkdir(cmount
, test_dir1
, 0700), 0);
893 sprintf(test_dir2
, "/dir1_loopsym_%d/loop_dir", getpid());
895 ASSERT_EQ(ceph_mkdir(cmount
, test_dir2
, 0700), 0);
897 // symlink it itself: /path/to/mysym -> /path/to/mysym
898 char test_symdir
[256];
899 sprintf(test_symdir
, "/dir1_loopsym_%d/loop_dir/symdir", getpid());
901 ASSERT_EQ(ceph_symlink(cmount
, test_symdir
, test_symdir
), 0);
904 sprintf(test_file
, "/dir1_loopsym_%d/loop_dir/symdir/test_loopsym_file", getpid());
905 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0600);
906 ASSERT_EQ(fd
, -ELOOP
);
908 // loop: /a -> /b, /b -> /c, /c -> /a
909 char a
[256], b
[256], c
[256];
910 sprintf(a
, "/%s/a", test_dir1
);
911 sprintf(b
, "/%s/b", test_dir1
);
912 sprintf(c
, "/%s/c", test_dir1
);
913 ASSERT_EQ(ceph_symlink(cmount
, a
, b
), 0);
914 ASSERT_EQ(ceph_symlink(cmount
, b
, c
), 0);
915 ASSERT_EQ(ceph_symlink(cmount
, c
, a
), 0);
916 ASSERT_EQ(ceph_open(cmount
, a
, O_RDWR
, 0), -ELOOP
);
918 ceph_shutdown(cmount
);
921 TEST(LibCephFS
, HardlinkNoOriginal
) {
923 int mypid
= getpid();
925 struct ceph_mount_info
*cmount
;
926 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
927 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
928 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
929 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
932 sprintf(dir
, "/test_rmdirfail%d", mypid
);
933 ASSERT_EQ(ceph_mkdir(cmount
, dir
, 0777), 0);
935 ASSERT_EQ(ceph_chdir(cmount
, dir
), 0);
937 int fd
= ceph_open(cmount
, "f1", O_CREAT
, 0644);
940 ceph_close(cmount
, fd
);
943 ASSERT_EQ(ceph_link(cmount
, "f1", "hardl1"), 0);
945 // remove file link points to
946 ASSERT_EQ(ceph_unlink(cmount
, "f1"), 0);
948 ceph_shutdown(cmount
);
951 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
952 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
953 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
954 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
955 ASSERT_EQ(ceph_chdir(cmount
, dir
), 0);
956 ASSERT_EQ(ceph_unlink(cmount
, "hardl1"), 0);
957 ASSERT_EQ(ceph_rmdir(cmount
, dir
), 0);
959 ceph_shutdown(cmount
);
962 TEST(LibCephFS
, BadArgument
) {
963 struct ceph_mount_info
*cmount
;
964 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
965 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
966 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
967 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
969 int fd
= ceph_open(cmount
, "test_file", O_CREAT
|O_RDWR
, 0666);
972 ASSERT_EQ(ceph_write(cmount
, fd
, buf
, sizeof(buf
), 0), (int)sizeof(buf
));
973 ASSERT_EQ(ceph_read(cmount
, fd
, buf
, 0, 5), 0);
974 ceph_close(cmount
, fd
);
975 ASSERT_EQ(ceph_unlink(cmount
, "test_file"), 0);
977 ceph_shutdown(cmount
);
980 TEST(LibCephFS
, BadFileDesc
) {
981 struct ceph_mount_info
*cmount
;
982 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
983 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
984 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
985 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
987 ASSERT_EQ(ceph_fchmod(cmount
, -1, 0655), -EBADF
);
988 ASSERT_EQ(ceph_close(cmount
, -1), -EBADF
);
989 ASSERT_EQ(ceph_lseek(cmount
, -1, 0, SEEK_SET
), -EBADF
);
992 ASSERT_EQ(ceph_read(cmount
, -1, buf
, 0, 0), -EBADF
);
993 ASSERT_EQ(ceph_write(cmount
, -1, buf
, 0, 0), -EBADF
);
995 ASSERT_EQ(ceph_ftruncate(cmount
, -1, 0), -EBADF
);
996 ASSERT_EQ(ceph_fsync(cmount
, -1, 0), -EBADF
);
998 struct ceph_statx stx
;
999 ASSERT_EQ(ceph_fstatx(cmount
, -1, &stx
, 0, 0), -EBADF
);
1001 struct sockaddr_storage addr
;
1002 ASSERT_EQ(ceph_get_file_stripe_address(cmount
, -1, 0, &addr
, 1), -EBADF
);
1004 ASSERT_EQ(ceph_get_file_stripe_unit(cmount
, -1), -EBADF
);
1005 ASSERT_EQ(ceph_get_file_pool(cmount
, -1), -EBADF
);
1007 ASSERT_EQ(ceph_get_file_pool_name(cmount
, -1, poolname
, sizeof(poolname
)), -EBADF
);
1008 ASSERT_EQ(ceph_get_file_replication(cmount
, -1), -EBADF
);
1009 ASSERT_EQ(ceph_get_file_object_size(cmount
, -1), -EBADF
);
1010 int stripe_unit
, stripe_count
, object_size
, pg_pool
;
1011 ASSERT_EQ(ceph_get_file_layout(cmount
, -1, &stripe_unit
, &stripe_count
, &object_size
, &pg_pool
), -EBADF
);
1012 ASSERT_EQ(ceph_get_file_stripe_count(cmount
, -1), -EBADF
);
1014 ceph_shutdown(cmount
);
1017 TEST(LibCephFS
, ReadEmptyFile
) {
1018 struct ceph_mount_info
*cmount
;
1019 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1020 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1021 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1022 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1024 // test the read_sync path in the client for zero files
1025 ASSERT_EQ(ceph_conf_set(cmount
, "client_debug_force_sync_read", "true"), 0);
1027 int mypid
= getpid();
1030 sprintf(testf
, "test_reademptyfile%d", mypid
);
1031 int fd
= ceph_open(cmount
, testf
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0644);
1034 ceph_close(cmount
, fd
);
1036 fd
= ceph_open(cmount
, testf
, O_RDONLY
, 0);
1040 ASSERT_EQ(ceph_read(cmount
, fd
, buf
, 4096, 0), 0);
1042 ceph_close(cmount
, fd
);
1043 ceph_shutdown(cmount
);
1046 TEST(LibCephFS
, PreadvPwritev
) {
1047 struct ceph_mount_info
*cmount
;
1048 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1049 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1050 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1051 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1053 int mypid
= getpid();
1056 sprintf(testf
, "test_preadvpwritevfile%d", mypid
);
1057 int fd
= ceph_open(cmount
, testf
, O_CREAT
|O_RDWR
, 0666);
1060 char out0
[] = "hello ";
1061 char out1
[] = "world\n";
1062 struct iovec iov_out
[2] = {
1063 {out0
, sizeof(out0
)},
1064 {out1
, sizeof(out1
)},
1066 char in0
[sizeof(out0
)];
1067 char in1
[sizeof(out1
)];
1068 struct iovec iov_in
[2] = {
1072 ssize_t nwritten
= iov_out
[0].iov_len
+ iov_out
[1].iov_len
;
1073 ssize_t nread
= iov_in
[0].iov_len
+ iov_in
[1].iov_len
;
1075 ASSERT_EQ(ceph_pwritev(cmount
, fd
, iov_out
, 2, 0), nwritten
);
1076 ASSERT_EQ(ceph_preadv(cmount
, fd
, iov_in
, 2, 0), nread
);
1077 ASSERT_EQ(0, strncmp((const char*)iov_in
[0].iov_base
, (const char*)iov_out
[0].iov_base
, iov_out
[0].iov_len
));
1078 ASSERT_EQ(0, strncmp((const char*)iov_in
[1].iov_base
, (const char*)iov_out
[1].iov_base
, iov_out
[1].iov_len
));
1080 ceph_close(cmount
, fd
);
1081 ceph_shutdown(cmount
);
1084 TEST(LibCephFS
, StripeUnitGran
) {
1085 struct ceph_mount_info
*cmount
;
1086 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1087 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1088 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1089 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1090 ASSERT_GT(ceph_get_stripe_unit_granularity(cmount
), 0);
1091 ceph_shutdown(cmount
);
1094 TEST(LibCephFS
, Rename
) {
1095 struct ceph_mount_info
*cmount
;
1096 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1097 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1098 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1099 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1101 int mypid
= getpid();
1105 /* make a source file */
1106 sprintf(path_src
, "test_rename_src%d", mypid
);
1107 int fd
= ceph_open(cmount
, path_src
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0777);
1109 ASSERT_EQ(0, ceph_close(cmount
, fd
));
1111 /* rename to a new dest path */
1112 sprintf(path_dst
, "test_rename_dst%d", mypid
);
1113 ASSERT_EQ(0, ceph_rename(cmount
, path_src
, path_dst
));
1115 /* test that dest path exists */
1116 struct ceph_statx stx
;
1117 ASSERT_EQ(0, ceph_statx(cmount
, path_dst
, &stx
, 0, 0));
1119 /* test that src path doesn't exist */
1120 ASSERT_EQ(-ENOENT
, ceph_statx(cmount
, path_src
, &stx
, 0, AT_SYMLINK_NOFOLLOW
));
1122 /* rename with non-existent source path */
1123 ASSERT_EQ(-ENOENT
, ceph_rename(cmount
, path_src
, path_dst
));
1125 ASSERT_EQ(0, ceph_unlink(cmount
, path_dst
));
1126 ceph_shutdown(cmount
);
1129 TEST(LibCephFS
, UseUnmounted
) {
1130 struct ceph_mount_info
*cmount
;
1131 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1132 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1133 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1135 struct statvfs stvfs
;
1136 EXPECT_EQ(-ENOTCONN
, ceph_statfs(cmount
, "/", &stvfs
));
1137 EXPECT_EQ(-ENOTCONN
, ceph_get_local_osd(cmount
));
1138 EXPECT_EQ(-ENOTCONN
, ceph_chdir(cmount
, "/"));
1140 struct ceph_dir_result
*dirp
;
1141 EXPECT_EQ(-ENOTCONN
, ceph_opendir(cmount
, "/", &dirp
));
1142 EXPECT_EQ(-ENOTCONN
, ceph_closedir(cmount
, dirp
));
1144 ceph_readdir(cmount
, dirp
);
1145 EXPECT_EQ(ENOTCONN
, errno
);
1147 struct dirent rdent
;
1148 EXPECT_EQ(-ENOTCONN
, ceph_readdir_r(cmount
, dirp
, &rdent
));
1150 struct ceph_statx stx
;
1151 EXPECT_EQ(-ENOTCONN
, ceph_readdirplus_r(cmount
, dirp
, &rdent
, &stx
, 0, 0, NULL
));
1152 EXPECT_EQ(-ENOTCONN
, ceph_getdents(cmount
, dirp
, NULL
, 0));
1153 EXPECT_EQ(-ENOTCONN
, ceph_getdnames(cmount
, dirp
, NULL
, 0));
1154 EXPECT_EQ(-ENOTCONN
, ceph_telldir(cmount
, dirp
));
1155 EXPECT_EQ(-ENOTCONN
, ceph_link(cmount
, "/", "/link"));
1156 EXPECT_EQ(-ENOTCONN
, ceph_unlink(cmount
, "/path"));
1157 EXPECT_EQ(-ENOTCONN
, ceph_rename(cmount
, "/path", "/path"));
1158 EXPECT_EQ(-ENOTCONN
, ceph_mkdir(cmount
, "/", 0655));
1159 EXPECT_EQ(-ENOTCONN
, ceph_mkdirs(cmount
, "/", 0655));
1160 EXPECT_EQ(-ENOTCONN
, ceph_rmdir(cmount
, "/path"));
1161 EXPECT_EQ(-ENOTCONN
, ceph_readlink(cmount
, "/path", NULL
, 0));
1162 EXPECT_EQ(-ENOTCONN
, ceph_symlink(cmount
, "/path", "/path"));
1163 EXPECT_EQ(-ENOTCONN
, ceph_statx(cmount
, "/path", &stx
, 0, 0));
1164 EXPECT_EQ(-ENOTCONN
, ceph_setattrx(cmount
, "/path", &stx
, 0, 0));
1165 EXPECT_EQ(-ENOTCONN
, ceph_getxattr(cmount
, "/path", "name", NULL
, 0));
1166 EXPECT_EQ(-ENOTCONN
, ceph_lgetxattr(cmount
, "/path", "name", NULL
, 0));
1167 EXPECT_EQ(-ENOTCONN
, ceph_listxattr(cmount
, "/path", NULL
, 0));
1168 EXPECT_EQ(-ENOTCONN
, ceph_llistxattr(cmount
, "/path", NULL
, 0));
1169 EXPECT_EQ(-ENOTCONN
, ceph_removexattr(cmount
, "/path", "name"));
1170 EXPECT_EQ(-ENOTCONN
, ceph_lremovexattr(cmount
, "/path", "name"));
1171 EXPECT_EQ(-ENOTCONN
, ceph_setxattr(cmount
, "/path", "name", NULL
, 0, 0));
1172 EXPECT_EQ(-ENOTCONN
, ceph_lsetxattr(cmount
, "/path", "name", NULL
, 0, 0));
1173 EXPECT_EQ(-ENOTCONN
, ceph_fsetattrx(cmount
, 0, &stx
, 0));
1174 EXPECT_EQ(-ENOTCONN
, ceph_chmod(cmount
, "/path", 0));
1175 EXPECT_EQ(-ENOTCONN
, ceph_fchmod(cmount
, 0, 0));
1176 EXPECT_EQ(-ENOTCONN
, ceph_chown(cmount
, "/path", 0, 0));
1177 EXPECT_EQ(-ENOTCONN
, ceph_lchown(cmount
, "/path", 0, 0));
1178 EXPECT_EQ(-ENOTCONN
, ceph_fchown(cmount
, 0, 0, 0));
1181 EXPECT_EQ(-ENOTCONN
, ceph_utime(cmount
, "/path", &utb
));
1182 EXPECT_EQ(-ENOTCONN
, ceph_truncate(cmount
, "/path", 0));
1183 EXPECT_EQ(-ENOTCONN
, ceph_mknod(cmount
, "/path", 0, 0));
1184 EXPECT_EQ(-ENOTCONN
, ceph_open(cmount
, "/path", 0, 0));
1185 EXPECT_EQ(-ENOTCONN
, ceph_open_layout(cmount
, "/path", 0, 0, 0, 0, 0, "pool"));
1186 EXPECT_EQ(-ENOTCONN
, ceph_close(cmount
, 0));
1187 EXPECT_EQ(-ENOTCONN
, ceph_lseek(cmount
, 0, 0, SEEK_SET
));
1188 EXPECT_EQ(-ENOTCONN
, ceph_read(cmount
, 0, NULL
, 0, 0));
1189 EXPECT_EQ(-ENOTCONN
, ceph_write(cmount
, 0, NULL
, 0, 0));
1190 EXPECT_EQ(-ENOTCONN
, ceph_ftruncate(cmount
, 0, 0));
1191 EXPECT_EQ(-ENOTCONN
, ceph_fsync(cmount
, 0, 0));
1192 EXPECT_EQ(-ENOTCONN
, ceph_fstatx(cmount
, 0, &stx
, 0, 0));
1193 EXPECT_EQ(-ENOTCONN
, ceph_sync_fs(cmount
));
1194 EXPECT_EQ(-ENOTCONN
, ceph_get_file_stripe_unit(cmount
, 0));
1195 EXPECT_EQ(-ENOTCONN
, ceph_get_file_stripe_count(cmount
, 0));
1196 EXPECT_EQ(-ENOTCONN
, ceph_get_file_layout(cmount
, 0, NULL
, NULL
,NULL
,NULL
));
1197 EXPECT_EQ(-ENOTCONN
, ceph_get_file_object_size(cmount
, 0));
1198 EXPECT_EQ(-ENOTCONN
, ceph_get_file_pool(cmount
, 0));
1199 EXPECT_EQ(-ENOTCONN
, ceph_get_file_pool_name(cmount
, 0, NULL
, 0));
1200 EXPECT_EQ(-ENOTCONN
, ceph_get_file_replication(cmount
, 0));
1201 EXPECT_EQ(-ENOTCONN
, ceph_get_path_replication(cmount
, "/path"));
1202 EXPECT_EQ(-ENOTCONN
, ceph_get_path_layout(cmount
, "/path", NULL
, NULL
, NULL
, NULL
));
1203 EXPECT_EQ(-ENOTCONN
, ceph_get_path_object_size(cmount
, "/path"));
1204 EXPECT_EQ(-ENOTCONN
, ceph_get_path_stripe_count(cmount
, "/path"));
1205 EXPECT_EQ(-ENOTCONN
, ceph_get_path_stripe_unit(cmount
, "/path"));
1206 EXPECT_EQ(-ENOTCONN
, ceph_get_path_pool(cmount
, "/path"));
1207 EXPECT_EQ(-ENOTCONN
, ceph_get_path_pool_name(cmount
, "/path", NULL
, 0));
1208 EXPECT_EQ(-ENOTCONN
, ceph_get_pool_name(cmount
, 0, NULL
, 0));
1209 EXPECT_EQ(-ENOTCONN
, ceph_get_file_stripe_address(cmount
, 0, 0, NULL
, 0));
1210 EXPECT_EQ(-ENOTCONN
, ceph_localize_reads(cmount
, 0));
1211 EXPECT_EQ(-ENOTCONN
, ceph_debug_get_fd_caps(cmount
, 0));
1212 EXPECT_EQ(-ENOTCONN
, ceph_debug_get_file_caps(cmount
, "/path"));
1213 EXPECT_EQ(-ENOTCONN
, ceph_get_stripe_unit_granularity(cmount
));
1214 EXPECT_EQ(-ENOTCONN
, ceph_get_pool_id(cmount
, "data"));
1215 EXPECT_EQ(-ENOTCONN
, ceph_get_pool_replication(cmount
, 1));
1217 ceph_release(cmount
);
1220 TEST(LibCephFS
, GetPoolId
) {
1221 struct ceph_mount_info
*cmount
;
1222 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1223 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1224 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1225 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1228 memset(name
, 0, sizeof(name
));
1229 ASSERT_LE(0, ceph_get_path_pool_name(cmount
, "/", name
, sizeof(name
)));
1230 ASSERT_GE(ceph_get_pool_id(cmount
, name
), 0);
1231 ASSERT_EQ(ceph_get_pool_id(cmount
, "weflkjwelfjwlkejf"), -ENOENT
);
1233 ceph_shutdown(cmount
);
1236 TEST(LibCephFS
, GetPoolReplication
) {
1237 struct ceph_mount_info
*cmount
;
1238 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1239 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1240 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1241 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1243 /* negative pools */
1244 ASSERT_EQ(ceph_get_pool_replication(cmount
, -10), -ENOENT
);
1248 int stripe_unit
, stripe_count
, object_size
;
1249 ASSERT_EQ(0, ceph_get_path_layout(cmount
, "/", &stripe_unit
, &stripe_count
,
1250 &object_size
, &pool_id
));
1251 ASSERT_GE(pool_id
, 0);
1252 ASSERT_GT(ceph_get_pool_replication(cmount
, pool_id
), 0);
1254 ceph_shutdown(cmount
);
1257 TEST(LibCephFS
, GetExtentOsds
) {
1258 struct ceph_mount_info
*cmount
;
1259 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1261 EXPECT_EQ(-ENOTCONN
, ceph_get_file_extent_osds(cmount
, 0, 0, NULL
, NULL
, 0));
1263 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1264 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1265 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1267 int stripe_unit
= (1<<18);
1270 char test_file
[256];
1271 sprintf(test_file
, "test_extent_osds_%d", getpid());
1272 int fd
= ceph_open_layout(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666,
1273 stripe_unit
, 2, stripe_unit
*2, NULL
);
1276 /* get back how many osds > 0 */
1277 int ret
= ceph_get_file_extent_osds(cmount
, fd
, 0, NULL
, NULL
, 0);
1283 /* full stripe extent */
1284 EXPECT_EQ(ret
, ceph_get_file_extent_osds(cmount
, fd
, 0, &len
, osds
, ret
));
1285 EXPECT_EQ(len
, (int64_t)stripe_unit
);
1287 /* half stripe extent */
1288 EXPECT_EQ(ret
, ceph_get_file_extent_osds(cmount
, fd
, stripe_unit
/2, &len
, osds
, ret
));
1289 EXPECT_EQ(len
, (int64_t)stripe_unit
/2);
1291 /* 1.5 stripe unit offset -1 byte */
1292 EXPECT_EQ(ret
, ceph_get_file_extent_osds(cmount
, fd
, 3*stripe_unit
/2-1, &len
, osds
, ret
));
1293 EXPECT_EQ(len
, (int64_t)stripe_unit
/2+1);
1295 /* 1.5 stripe unit offset +1 byte */
1296 EXPECT_EQ(ret
, ceph_get_file_extent_osds(cmount
, fd
, 3*stripe_unit
/2+1, &len
, osds
, ret
));
1297 EXPECT_EQ(len
, (int64_t)stripe_unit
/2-1);
1299 /* only when more than 1 osd */
1301 EXPECT_EQ(-ERANGE
, ceph_get_file_extent_osds(cmount
, fd
, 0, NULL
, osds
, 1));
1304 ceph_close(cmount
, fd
);
1306 ceph_shutdown(cmount
);
1309 TEST(LibCephFS
, GetOsdCrushLocation
) {
1310 struct ceph_mount_info
*cmount
;
1311 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1313 EXPECT_EQ(-ENOTCONN
, ceph_get_osd_crush_location(cmount
, 0, NULL
, 0));
1315 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1316 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1317 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1319 ASSERT_EQ(ceph_get_osd_crush_location(cmount
, 0, NULL
, 1), -EINVAL
);
1322 ASSERT_EQ(ceph_get_osd_crush_location(cmount
, 9999999, path
, 0), -ENOENT
);
1323 ASSERT_EQ(ceph_get_osd_crush_location(cmount
, -1, path
, 0), -EINVAL
);
1325 char test_file
[256];
1326 sprintf(test_file
, "test_osds_loc_%d", getpid());
1327 int fd
= ceph_open(cmount
, test_file
, O_CREAT
|O_RDWR
, 0666);
1330 /* get back how many osds > 0 */
1331 int ret
= ceph_get_file_extent_osds(cmount
, fd
, 0, NULL
, NULL
, 0);
1334 /* full stripe extent */
1336 EXPECT_EQ(ret
, ceph_get_file_extent_osds(cmount
, fd
, 0, NULL
, osds
, ret
));
1338 ASSERT_GT(ceph_get_osd_crush_location(cmount
, 0, path
, 0), 0);
1339 ASSERT_EQ(ceph_get_osd_crush_location(cmount
, 0, path
, 1), -ERANGE
);
1341 for (int i
= 0; i
< ret
; i
++) {
1342 int len
= ceph_get_osd_crush_location(cmount
, osds
[i
], path
, sizeof(path
));
1346 std::string
type(path
+ pos
);
1347 ASSERT_GT((int)type
.size(), 0);
1348 pos
+= type
.size() + 1;
1350 std::string
name(path
+ pos
);
1351 ASSERT_GT((int)name
.size(), 0);
1352 pos
+= name
.size() + 1;
1356 ceph_close(cmount
, fd
);
1357 ceph_shutdown(cmount
);
1360 TEST(LibCephFS
, GetOsdAddr
) {
1361 struct ceph_mount_info
*cmount
;
1362 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1364 EXPECT_EQ(-ENOTCONN
, ceph_get_osd_addr(cmount
, 0, NULL
));
1366 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1367 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1368 ASSERT_EQ(ceph_mount(cmount
, NULL
), 0);
1370 ASSERT_EQ(-EINVAL
, ceph_get_osd_addr(cmount
, 0, NULL
));
1372 struct sockaddr_storage addr
;
1373 ASSERT_EQ(-ENOENT
, ceph_get_osd_addr(cmount
, -1, &addr
));
1374 ASSERT_EQ(-ENOENT
, ceph_get_osd_addr(cmount
, 9999999, &addr
));
1376 ASSERT_EQ(0, ceph_get_osd_addr(cmount
, 0, &addr
));
1378 ceph_shutdown(cmount
);
1381 TEST(LibCephFS
, OpenNoClose
) {
1382 struct ceph_mount_info
*cmount
;
1383 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1384 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1385 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1386 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1388 pid_t mypid
= getpid();
1390 sprintf(str_buf
, "open_no_close_dir%d", mypid
);
1391 ASSERT_EQ(0, ceph_mkdirs(cmount
, str_buf
, 0777));
1393 struct ceph_dir_result
*ls_dir
= NULL
;
1394 ASSERT_EQ(ceph_opendir(cmount
, str_buf
, &ls_dir
), 0);
1396 sprintf(str_buf
, "open_no_close_file%d", mypid
);
1397 int fd
= ceph_open(cmount
, str_buf
, O_RDONLY
|O_CREAT
, 0666);
1400 // shutdown should force close opened file/dir
1401 ceph_shutdown(cmount
);
1404 TEST(LibCephFS
, Nlink
) {
1405 struct ceph_mount_info
*cmount
;
1406 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1407 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1408 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1409 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1411 Inode
*root
, *dir
, *file
;
1413 ASSERT_EQ(ceph_ll_lookup_root(cmount
, &root
), 0);
1415 char dirname
[32], filename
[32], linkname
[32];
1416 sprintf(dirname
, "nlinkdir%x", getpid());
1417 sprintf(filename
, "nlinkorig%x", getpid());
1418 sprintf(linkname
, "nlinklink%x", getpid());
1420 struct ceph_statx stx
;
1422 UserPerm
*perms
= ceph_mount_perms(cmount
);
1424 ASSERT_EQ(ceph_ll_mkdir(cmount
, root
, dirname
, 0755, &dir
, &stx
, 0, 0, perms
), 0);
1425 ASSERT_EQ(ceph_ll_create(cmount
, dir
, filename
, 0666, O_RDWR
|O_CREAT
|O_EXCL
,
1426 &file
, &fh
, &stx
, CEPH_STATX_NLINK
, 0, perms
), 0);
1427 ASSERT_EQ(ceph_ll_close(cmount
, fh
), 0);
1428 ASSERT_EQ(stx
.stx_nlink
, (nlink_t
)1);
1430 ASSERT_EQ(ceph_ll_link(cmount
, file
, dir
, linkname
, perms
), 0);
1431 ASSERT_EQ(ceph_ll_getattr(cmount
, file
, &stx
, CEPH_STATX_NLINK
, 0, perms
), 0);
1432 ASSERT_EQ(stx
.stx_nlink
, (nlink_t
)2);
1434 ASSERT_EQ(ceph_ll_unlink(cmount
, dir
, linkname
, perms
), 0);
1435 ASSERT_EQ(ceph_ll_lookup(cmount
, dir
, filename
, &file
, &stx
,
1436 CEPH_STATX_NLINK
, 0, perms
), 0);
1437 ASSERT_EQ(stx
.stx_nlink
, (nlink_t
)1);
1439 ceph_shutdown(cmount
);
1442 TEST(LibCephFS
, SlashDotDot
) {
1443 struct ceph_mount_info
*cmount
;
1444 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1445 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1446 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1447 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1449 struct ceph_statx stx
;
1450 ASSERT_EQ(ceph_statx(cmount
, "/.", &stx
, CEPH_STATX_INO
, 0), 0);
1452 ino_t ino
= stx
.stx_ino
;
1453 ASSERT_EQ(ceph_statx(cmount
, "/..", &stx
, CEPH_STATX_INO
, 0), 0);
1455 /* At root, "." and ".." should be the same inode */
1456 ASSERT_EQ(ino
, stx
.stx_ino
);
1458 /* Test accessing the parent of an unlinked directory */
1459 char dir1
[32], dir2
[32];
1460 sprintf(dir1
, "/sldotdot%x", getpid());
1461 sprintf(dir2
, "%s/sub%x", dir1
, getpid());
1463 ASSERT_EQ(ceph_mkdir(cmount
, dir1
, 0755), 0);
1464 ASSERT_EQ(ceph_mkdir(cmount
, dir2
, 0755), 0);
1466 ASSERT_EQ(ceph_chdir(cmount
, dir2
), 0);
1468 /* Test behavior when unlinking cwd */
1469 struct ceph_dir_result
*rdir
;
1470 ASSERT_EQ(ceph_opendir(cmount
, ".", &rdir
), 0);
1471 ASSERT_EQ(ceph_rmdir(cmount
, dir2
), 0);
1474 struct dirent
*result
= ceph_readdir(cmount
, rdir
);
1475 ino
= result
->d_ino
;
1477 /* get ".." entry */
1478 result
= ceph_readdir(cmount
, rdir
);
1479 ASSERT_EQ(ino
, result
->d_ino
);
1480 ceph_closedir(cmount
, rdir
);
1482 /* Make sure it works same way when mounting subtree */
1483 ASSERT_EQ(ceph_unmount(cmount
), 0);
1484 ASSERT_EQ(ceph_mount(cmount
, dir1
), 0);
1485 ASSERT_EQ(ceph_statx(cmount
, "/..", &stx
, CEPH_STATX_INO
, 0), 0);
1487 /* Test readdir behavior */
1488 ASSERT_EQ(ceph_opendir(cmount
, "/", &rdir
), 0);
1489 result
= ceph_readdir(cmount
, rdir
);
1490 ASSERT_TRUE(result
!= NULL
);
1491 ASSERT_STREQ(result
->d_name
, ".");
1492 ino
= result
->d_ino
;
1493 result
= ceph_readdir(cmount
, rdir
);
1494 ASSERT_TRUE(result
!= NULL
);
1495 ASSERT_STREQ(result
->d_name
, "..");
1496 ASSERT_EQ(ino
, result
->d_ino
);
1498 ceph_shutdown(cmount
);
1502 timespec_eq(timespec
const& lhs
, timespec
const& rhs
)
1504 return lhs
.tv_sec
== rhs
.tv_sec
&& lhs
.tv_nsec
== rhs
.tv_nsec
;
1507 TEST(LibCephFS
, Btime
) {
1508 struct ceph_mount_info
*cmount
;
1509 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1510 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1511 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1512 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1515 sprintf(filename
, "/getattrx%x", getpid());
1517 ceph_unlink(cmount
, filename
);
1518 int fd
= ceph_open(cmount
, filename
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1521 /* make sure fstatx works */
1522 struct ceph_statx stx
;
1524 ASSERT_EQ(ceph_fstatx(cmount
, fd
, &stx
, CEPH_STATX_CTIME
|CEPH_STATX_BTIME
, 0), 0);
1525 ASSERT_TRUE(stx
.stx_mask
& (CEPH_STATX_CTIME
|CEPH_STATX_BTIME
));
1526 ASSERT_TRUE(timespec_eq(stx
.stx_ctime
, stx
.stx_btime
));
1527 ceph_close(cmount
, fd
);
1529 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_CTIME
|CEPH_STATX_BTIME
, 0), 0);
1530 ASSERT_TRUE(timespec_eq(stx
.stx_ctime
, stx
.stx_btime
));
1531 ASSERT_TRUE(stx
.stx_mask
& (CEPH_STATX_CTIME
|CEPH_STATX_BTIME
));
1533 struct timespec old_btime
= stx
.stx_btime
;
1535 /* Now sleep, do a chmod and verify that the ctime changed, but btime didn't */
1537 ASSERT_EQ(ceph_chmod(cmount
, filename
, 0644), 0);
1538 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_CTIME
|CEPH_STATX_BTIME
, 0), 0);
1539 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_BTIME
);
1540 ASSERT_TRUE(timespec_eq(stx
.stx_btime
, old_btime
));
1541 ASSERT_FALSE(timespec_eq(stx
.stx_ctime
, stx
.stx_btime
));
1543 ceph_shutdown(cmount
);
1546 TEST(LibCephFS
, SetBtime
) {
1547 struct ceph_mount_info
*cmount
;
1548 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1549 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1550 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1551 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1554 sprintf(filename
, "/setbtime%x", getpid());
1556 ceph_unlink(cmount
, filename
);
1557 int fd
= ceph_open(cmount
, filename
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1559 ceph_close(cmount
, fd
);
1561 struct ceph_statx stx
;
1562 struct timespec old_btime
= { 1, 2 };
1564 stx
.stx_btime
= old_btime
;
1566 ASSERT_EQ(ceph_setattrx(cmount
, filename
, &stx
, CEPH_SETATTR_BTIME
, 0), 0);
1568 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_BTIME
, 0), 0);
1569 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_BTIME
);
1570 ASSERT_TRUE(timespec_eq(stx
.stx_btime
, old_btime
));
1572 ceph_shutdown(cmount
);
1575 TEST(LibCephFS
, LazyStatx
) {
1576 struct ceph_mount_info
*cmount1
, *cmount2
;
1577 ASSERT_EQ(ceph_create(&cmount1
, NULL
), 0);
1578 ASSERT_EQ(ceph_create(&cmount2
, NULL
), 0);
1579 ASSERT_EQ(ceph_conf_read_file(cmount1
, NULL
), 0);
1580 ASSERT_EQ(ceph_conf_read_file(cmount2
, NULL
), 0);
1581 ASSERT_EQ(0, ceph_conf_parse_env(cmount1
, NULL
));
1582 ASSERT_EQ(0, ceph_conf_parse_env(cmount2
, NULL
));
1583 ASSERT_EQ(ceph_mount(cmount1
, "/"), 0);
1584 ASSERT_EQ(ceph_mount(cmount2
, "/"), 0);
1587 sprintf(filename
, "lazystatx%x", getpid());
1589 Inode
*root1
, *file1
, *root2
, *file2
;
1590 struct ceph_statx stx
;
1592 UserPerm
*perms1
= ceph_mount_perms(cmount1
);
1593 UserPerm
*perms2
= ceph_mount_perms(cmount2
);
1595 ASSERT_EQ(ceph_ll_lookup_root(cmount1
, &root1
), 0);
1596 ceph_ll_unlink(cmount1
, root1
, filename
, perms1
);
1597 ASSERT_EQ(ceph_ll_create(cmount1
, root1
, filename
, 0666, O_RDWR
|O_CREAT
|O_EXCL
,
1598 &file1
, &fh
, &stx
, 0, 0, perms1
), 0);
1599 ASSERT_EQ(ceph_ll_close(cmount1
, fh
), 0);
1601 ASSERT_EQ(ceph_ll_lookup_root(cmount2
, &root2
), 0);
1603 ASSERT_EQ(ceph_ll_lookup(cmount2
, root2
, filename
, &file2
, &stx
, CEPH_STATX_CTIME
, 0, perms2
), 0);
1605 struct timespec old_ctime
= stx
.stx_ctime
;
1608 * Now sleep, do a chmod on the first client and the see whether we get a
1609 * different ctime with a statx that uses AT_NO_ATTR_SYNC
1612 stx
.stx_mode
= 0644;
1613 ASSERT_EQ(ceph_ll_setattr(cmount1
, file1
, &stx
, CEPH_SETATTR_MODE
, perms1
), 0);
1615 ASSERT_EQ(ceph_ll_getattr(cmount2
, file2
, &stx
, CEPH_STATX_CTIME
, AT_NO_ATTR_SYNC
, perms2
), 0);
1616 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_CTIME
);
1617 ASSERT_TRUE(stx
.stx_ctime
.tv_sec
== old_ctime
.tv_sec
&&
1618 stx
.stx_ctime
.tv_nsec
== old_ctime
.tv_nsec
);
1620 ceph_shutdown(cmount1
);
1621 ceph_shutdown(cmount2
);
1624 TEST(LibCephFS
, ChangeAttr
) {
1625 struct ceph_mount_info
*cmount
;
1626 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1627 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1628 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1629 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1632 sprintf(filename
, "/changeattr%x", getpid());
1634 ceph_unlink(cmount
, filename
);
1635 int fd
= ceph_open(cmount
, filename
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1638 struct ceph_statx stx
;
1639 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1640 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1642 uint64_t old_change_attr
= stx
.stx_version
;
1644 /* do chmod, and check whether change_attr changed */
1645 ASSERT_EQ(ceph_chmod(cmount
, filename
, 0644), 0);
1646 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1647 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1648 ASSERT_NE(stx
.stx_version
, old_change_attr
);
1649 old_change_attr
= stx
.stx_version
;
1651 /* now do a write and see if it changed again */
1652 ASSERT_EQ(3, ceph_write(cmount
, fd
, "foo", 3, 0));
1653 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1654 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1655 ASSERT_NE(stx
.stx_version
, old_change_attr
);
1656 old_change_attr
= stx
.stx_version
;
1658 /* Now truncate and check again */
1659 ASSERT_EQ(0, ceph_ftruncate(cmount
, fd
, 0));
1660 ASSERT_EQ(ceph_statx(cmount
, filename
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1661 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1662 ASSERT_NE(stx
.stx_version
, old_change_attr
);
1664 ceph_close(cmount
, fd
);
1665 ceph_shutdown(cmount
);
1668 TEST(LibCephFS
, DirChangeAttr
) {
1669 struct ceph_mount_info
*cmount
;
1670 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1671 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1672 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1673 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1675 char dirname
[32], filename
[32];
1676 sprintf(dirname
, "/dirchange%x", getpid());
1677 sprintf(filename
, "%s/foo", dirname
);
1679 ASSERT_EQ(ceph_mkdir(cmount
, dirname
, 0755), 0);
1681 struct ceph_statx stx
;
1682 ASSERT_EQ(ceph_statx(cmount
, dirname
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1683 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1685 uint64_t old_change_attr
= stx
.stx_version
;
1687 int fd
= ceph_open(cmount
, filename
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1689 ceph_close(cmount
, fd
);
1691 ASSERT_EQ(ceph_statx(cmount
, dirname
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1692 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1693 ASSERT_NE(stx
.stx_version
, old_change_attr
);
1695 old_change_attr
= stx
.stx_version
;
1697 ASSERT_EQ(ceph_unlink(cmount
, filename
), 0);
1698 ASSERT_EQ(ceph_statx(cmount
, dirname
, &stx
, CEPH_STATX_VERSION
, 0), 0);
1699 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_VERSION
);
1700 ASSERT_NE(stx
.stx_version
, old_change_attr
);
1702 ceph_shutdown(cmount
);
1705 TEST(LibCephFS
, SetSize
) {
1706 struct ceph_mount_info
*cmount
;
1707 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1708 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1709 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1710 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1713 sprintf(filename
, "/setsize%x", getpid());
1715 ceph_unlink(cmount
, filename
);
1716 int fd
= ceph_open(cmount
, filename
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1719 struct ceph_statx stx
;
1720 uint64_t size
= 8388608;
1721 stx
.stx_size
= size
;
1722 ASSERT_EQ(ceph_fsetattrx(cmount
, fd
, &stx
, CEPH_SETATTR_SIZE
), 0);
1723 ASSERT_EQ(ceph_fstatx(cmount
, fd
, &stx
, CEPH_STATX_SIZE
, 0), 0);
1724 ASSERT_EQ(stx
.stx_size
, size
);
1726 ceph_close(cmount
, fd
);
1727 ceph_shutdown(cmount
);
1730 TEST(LibCephFS
, ClearSetuid
) {
1731 struct ceph_mount_info
*cmount
;
1732 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1733 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1734 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1735 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1738 ASSERT_EQ(ceph_ll_lookup_root(cmount
, &root
), 0);
1741 sprintf(filename
, "clearsetuid%x", getpid());
1745 struct ceph_statx stx
;
1746 const mode_t after_mode
= S_IRWXU
;
1747 const mode_t before_mode
= S_IRWXU
| S_ISUID
| S_ISGID
;
1748 const unsigned want
= CEPH_STATX_UID
|CEPH_STATX_GID
|CEPH_STATX_MODE
;
1749 UserPerm
*usercred
= ceph_mount_perms(cmount
);
1751 ceph_ll_unlink(cmount
, root
, filename
, usercred
);
1752 ASSERT_EQ(ceph_ll_create(cmount
, root
, filename
, before_mode
,
1753 O_RDWR
|O_CREAT
|O_EXCL
, &in
, &fh
, &stx
, want
, 0,
1756 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, before_mode
);
1759 ASSERT_EQ(ceph_ll_write(cmount
, fh
, 0, 3, "foo"), 3);
1760 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, usercred
), 0);
1761 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_MODE
);
1762 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, after_mode
);
1765 stx
.stx_mode
= before_mode
;
1766 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, usercred
), 0);
1767 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, usercred
), 0);
1768 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_MODE
);
1769 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, before_mode
);
1773 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_SETATTR_SIZE
, usercred
), 0);
1774 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, usercred
), 0);
1775 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_MODE
);
1776 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, after_mode
);
1779 stx
.stx_mode
= before_mode
;
1780 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, usercred
), 0);
1781 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, usercred
), 0);
1782 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_MODE
);
1783 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, before_mode
);
1785 // chown -- for this we need to be "root"
1786 UserPerm
*rootcred
= ceph_userperm_new(0, 0, 0, NULL
);
1787 ASSERT_TRUE(rootcred
);
1790 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_SETATTR_UID
|CEPH_SETATTR_GID
, rootcred
), 0);
1791 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, usercred
), 0);
1792 ASSERT_TRUE(stx
.stx_mask
& CEPH_STATX_MODE
);
1793 ASSERT_EQ(stx
.stx_mode
& (mode_t
)ALLPERMS
, after_mode
);
1795 /* test chown with supplementary groups, and chown with/without exe bit */
1798 gid_t gids
[] = {65533,65532};
1799 UserPerm
*altcred
= ceph_userperm_new(u
, g
, sizeof gids
/ sizeof gids
[0], gids
);
1802 mode_t m
= S_ISGID
|S_ISUID
|S_IRUSR
|S_IWUSR
;
1804 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_STATX_MODE
|CEPH_SETATTR_UID
|CEPH_SETATTR_GID
, rootcred
), 0);
1805 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, altcred
), 0);
1806 ASSERT_EQ(stx
.stx_mode
&(mode_t
)ALLPERMS
, m
);
1807 /* not dropped without exe bit */
1808 stx
.stx_gid
= gids
[0];
1809 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_SETATTR_GID
, altcred
), 0);
1810 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, altcred
), 0);
1811 ASSERT_EQ(stx
.stx_mode
&(mode_t
)ALLPERMS
, m
);
1812 /* now check dropped with exe bit */
1813 m
= S_ISGID
|S_ISUID
|S_IRWXU
;
1815 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, altcred
), 0);
1816 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, altcred
), 0);
1817 ASSERT_EQ(stx
.stx_mode
&(mode_t
)ALLPERMS
, m
);
1818 stx
.stx_gid
= gids
[1];
1819 ASSERT_EQ(ceph_ll_setattr(cmount
, in
, &stx
, CEPH_SETATTR_GID
, altcred
), 0);
1820 ASSERT_EQ(ceph_ll_getattr(cmount
, in
, &stx
, CEPH_STATX_MODE
, 0, altcred
), 0);
1821 ASSERT_EQ(stx
.stx_mode
&(mode_t
)ALLPERMS
, m
&(S_IRWXU
|S_IRWXG
|S_IRWXO
));
1822 ceph_userperm_destroy(altcred
);
1824 ASSERT_EQ(ceph_ll_close(cmount
, fh
), 0);
1825 ceph_shutdown(cmount
);
1828 TEST(LibCephFS
, OperationsOnRoot
)
1830 struct ceph_mount_info
*cmount
;
1831 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1832 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1833 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1834 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1837 sprintf(dirname
, "/somedir%x", getpid());
1839 ASSERT_EQ(ceph_mkdir(cmount
, dirname
, 0755), 0);
1841 ASSERT_EQ(ceph_rmdir(cmount
, "/"), -EBUSY
);
1843 ASSERT_EQ(ceph_link(cmount
, "/", "/"), -EEXIST
);
1844 ASSERT_EQ(ceph_link(cmount
, dirname
, "/"), -EEXIST
);
1845 ASSERT_EQ(ceph_link(cmount
, "nonExisitingDir", "/"), -ENOENT
);
1847 ASSERT_EQ(ceph_unlink(cmount
, "/"), -EISDIR
);
1849 ASSERT_EQ(ceph_rename(cmount
, "/", "/"), -EBUSY
);
1850 ASSERT_EQ(ceph_rename(cmount
, dirname
, "/"), -EBUSY
);
1851 ASSERT_EQ(ceph_rename(cmount
, "nonExistingDir", "/"), -EBUSY
);
1852 ASSERT_EQ(ceph_rename(cmount
, "/", dirname
), -EBUSY
);
1853 ASSERT_EQ(ceph_rename(cmount
, "/", "nonExistingDir"), -EBUSY
);
1855 ASSERT_EQ(ceph_mkdir(cmount
, "/", 0777), -EEXIST
);
1857 ASSERT_EQ(ceph_mknod(cmount
, "/", 0, 0), -EEXIST
);
1859 ASSERT_EQ(ceph_symlink(cmount
, "/", "/"), -EEXIST
);
1860 ASSERT_EQ(ceph_symlink(cmount
, dirname
, "/"), -EEXIST
);
1861 ASSERT_EQ(ceph_symlink(cmount
, "nonExistingDir", "/"), -EEXIST
);
1863 ceph_shutdown(cmount
);
1866 static void shutdown_racer_func()
1868 const int niter
= 32;
1869 struct ceph_mount_info
*cmount
;
1872 for (i
= 0; i
< niter
; ++i
) {
1873 ASSERT_EQ(ceph_create(&cmount
, NULL
), 0);
1874 ASSERT_EQ(ceph_conf_read_file(cmount
, NULL
), 0);
1875 ASSERT_EQ(0, ceph_conf_parse_env(cmount
, NULL
));
1876 ASSERT_EQ(ceph_mount(cmount
, "/"), 0);
1877 ceph_shutdown(cmount
);
1881 // See tracker #20988
1882 TEST(LibCephFS
, ShutdownRace
)
1884 const int nthreads
= 128;
1885 std::thread threads
[nthreads
];
1887 // Need a bunch of fd's for this test
1888 struct rlimit rold
, rnew
;
1889 ASSERT_EQ(getrlimit(RLIMIT_NOFILE
, &rold
), 0);
1891 rnew
.rlim_cur
= rnew
.rlim_max
;
1892 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rnew
), 0);
1894 for (int i
= 0; i
< nthreads
; ++i
)
1895 threads
[i
] = std::thread(shutdown_racer_func
);
1897 for (int i
= 0; i
< nthreads
; ++i
)
1899 ASSERT_EQ(setrlimit(RLIMIT_NOFILE
, &rold
), 0);