summaryrefslogtreecommitdiffstats
path: root/src/common/ceph_fs.cc
blob: 380b401df301a5755a7bf02452448f6ef1fe96f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * ceph_fs.cc - Some Ceph functions that are shared between kernel space and
 * user space.
 *
 */

/*
 * Some non-inline ceph helpers
 */
#include "include/types.h"

int ceph_flags_to_mode(int flags)
{
	/* because CEPH_FILE_MODE_PIN is zero, so mode = -1 is error */
	int mode = -1;

	if ((flags & CEPH_O_DIRECTORY) == CEPH_O_DIRECTORY)
		return CEPH_FILE_MODE_PIN;

	switch (flags & O_ACCMODE) {
	case CEPH_O_WRONLY:
		mode = CEPH_FILE_MODE_WR;
		break;
	case CEPH_O_RDONLY:
		mode = CEPH_FILE_MODE_RD;
		break;
	case CEPH_O_RDWR:
	case O_ACCMODE: /* this is what the VFS does */
		mode = CEPH_FILE_MODE_RDWR;
		break;
	}

	if (flags & CEPH_O_LAZY)
		mode |= CEPH_FILE_MODE_LAZY;

	return mode;
}

int ceph_caps_for_mode(int mode)
{
	int caps = CEPH_CAP_PIN;

	if (mode & CEPH_FILE_MODE_RD)
		caps |= CEPH_CAP_FILE_SHARED |
			CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE;
	if (mode & CEPH_FILE_MODE_WR)
		caps |= CEPH_CAP_FILE_EXCL |
			CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER |
			CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL |
			CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL;
	if (mode & CEPH_FILE_MODE_LAZY)
		caps |= CEPH_CAP_FILE_LAZYIO;

	return caps;
}

int ceph_flags_sys2wire(int flags)
{
       int wire_flags = 0;

       switch (flags & O_ACCMODE) {
       case O_RDONLY:
               wire_flags |= CEPH_O_RDONLY;
               break;
       case O_WRONLY:
               wire_flags |= CEPH_O_WRONLY;
               break;
       case O_RDWR:
               wire_flags |= CEPH_O_RDWR;
               break;
       }
       flags &= ~O_ACCMODE;

#define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }

       ceph_sys2wire(O_CREAT);
       ceph_sys2wire(O_EXCL);
       ceph_sys2wire(O_TRUNC);

       #ifndef _WIN32
       ceph_sys2wire(O_DIRECTORY);
       ceph_sys2wire(O_NOFOLLOW);
       // In some cases, FILE_FLAG_BACKUP_SEMANTICS may be used instead
       // of O_DIRECTORY. We may need some workarounds in order to handle
       // the fact that those flags are not available on Windows.
       #endif

#undef ceph_sys2wire

       return wire_flags;
}