xref: /openbmc/linux/fs/ceph/util.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
1*24604f7eSJeff Layton // SPDX-License-Identifier: GPL-2.0
2*24604f7eSJeff Layton /*
3*24604f7eSJeff Layton  * Some non-inline ceph helpers
4*24604f7eSJeff Layton  */
5*24604f7eSJeff Layton #include <linux/module.h>
6*24604f7eSJeff Layton #include <linux/ceph/types.h>
7*24604f7eSJeff Layton 
8*24604f7eSJeff Layton /*
9*24604f7eSJeff Layton  * return true if @layout appears to be valid
10*24604f7eSJeff Layton  */
ceph_file_layout_is_valid(const struct ceph_file_layout * layout)11*24604f7eSJeff Layton int ceph_file_layout_is_valid(const struct ceph_file_layout *layout)
12*24604f7eSJeff Layton {
13*24604f7eSJeff Layton 	__u32 su = layout->stripe_unit;
14*24604f7eSJeff Layton 	__u32 sc = layout->stripe_count;
15*24604f7eSJeff Layton 	__u32 os = layout->object_size;
16*24604f7eSJeff Layton 
17*24604f7eSJeff Layton 	/* stripe unit, object size must be non-zero, 64k increment */
18*24604f7eSJeff Layton 	if (!su || (su & (CEPH_MIN_STRIPE_UNIT-1)))
19*24604f7eSJeff Layton 		return 0;
20*24604f7eSJeff Layton 	if (!os || (os & (CEPH_MIN_STRIPE_UNIT-1)))
21*24604f7eSJeff Layton 		return 0;
22*24604f7eSJeff Layton 	/* object size must be a multiple of stripe unit */
23*24604f7eSJeff Layton 	if (os < su || os % su)
24*24604f7eSJeff Layton 		return 0;
25*24604f7eSJeff Layton 	/* stripe count must be non-zero */
26*24604f7eSJeff Layton 	if (!sc)
27*24604f7eSJeff Layton 		return 0;
28*24604f7eSJeff Layton 	return 1;
29*24604f7eSJeff Layton }
30*24604f7eSJeff Layton 
ceph_file_layout_from_legacy(struct ceph_file_layout * fl,struct ceph_file_layout_legacy * legacy)31*24604f7eSJeff Layton void ceph_file_layout_from_legacy(struct ceph_file_layout *fl,
32*24604f7eSJeff Layton 				  struct ceph_file_layout_legacy *legacy)
33*24604f7eSJeff Layton {
34*24604f7eSJeff Layton 	fl->stripe_unit = le32_to_cpu(legacy->fl_stripe_unit);
35*24604f7eSJeff Layton 	fl->stripe_count = le32_to_cpu(legacy->fl_stripe_count);
36*24604f7eSJeff Layton 	fl->object_size = le32_to_cpu(legacy->fl_object_size);
37*24604f7eSJeff Layton 	fl->pool_id = le32_to_cpu(legacy->fl_pg_pool);
38*24604f7eSJeff Layton 	if (fl->pool_id == 0 && fl->stripe_unit == 0 &&
39*24604f7eSJeff Layton 	    fl->stripe_count == 0 && fl->object_size == 0)
40*24604f7eSJeff Layton 		fl->pool_id = -1;
41*24604f7eSJeff Layton }
42*24604f7eSJeff Layton 
ceph_file_layout_to_legacy(struct ceph_file_layout * fl,struct ceph_file_layout_legacy * legacy)43*24604f7eSJeff Layton void ceph_file_layout_to_legacy(struct ceph_file_layout *fl,
44*24604f7eSJeff Layton 				struct ceph_file_layout_legacy *legacy)
45*24604f7eSJeff Layton {
46*24604f7eSJeff Layton 	legacy->fl_stripe_unit = cpu_to_le32(fl->stripe_unit);
47*24604f7eSJeff Layton 	legacy->fl_stripe_count = cpu_to_le32(fl->stripe_count);
48*24604f7eSJeff Layton 	legacy->fl_object_size = cpu_to_le32(fl->object_size);
49*24604f7eSJeff Layton 	if (fl->pool_id >= 0)
50*24604f7eSJeff Layton 		legacy->fl_pg_pool = cpu_to_le32(fl->pool_id);
51*24604f7eSJeff Layton 	else
52*24604f7eSJeff Layton 		legacy->fl_pg_pool = 0;
53*24604f7eSJeff Layton }
54*24604f7eSJeff Layton 
ceph_flags_to_mode(int flags)55*24604f7eSJeff Layton int ceph_flags_to_mode(int flags)
56*24604f7eSJeff Layton {
57*24604f7eSJeff Layton 	int mode;
58*24604f7eSJeff Layton 
59*24604f7eSJeff Layton #ifdef O_DIRECTORY  /* fixme */
60*24604f7eSJeff Layton 	if ((flags & O_DIRECTORY) == O_DIRECTORY)
61*24604f7eSJeff Layton 		return CEPH_FILE_MODE_PIN;
62*24604f7eSJeff Layton #endif
63*24604f7eSJeff Layton 
64*24604f7eSJeff Layton 	switch (flags & O_ACCMODE) {
65*24604f7eSJeff Layton 	case O_WRONLY:
66*24604f7eSJeff Layton 		mode = CEPH_FILE_MODE_WR;
67*24604f7eSJeff Layton 		break;
68*24604f7eSJeff Layton 	case O_RDONLY:
69*24604f7eSJeff Layton 		mode = CEPH_FILE_MODE_RD;
70*24604f7eSJeff Layton 		break;
71*24604f7eSJeff Layton 	case O_RDWR:
72*24604f7eSJeff Layton 	case O_ACCMODE: /* this is what the VFS does */
73*24604f7eSJeff Layton 		mode = CEPH_FILE_MODE_RDWR;
74*24604f7eSJeff Layton 		break;
75*24604f7eSJeff Layton 	}
76*24604f7eSJeff Layton #ifdef O_LAZY
77*24604f7eSJeff Layton 	if (flags & O_LAZY)
78*24604f7eSJeff Layton 		mode |= CEPH_FILE_MODE_LAZY;
79*24604f7eSJeff Layton #endif
80*24604f7eSJeff Layton 
81*24604f7eSJeff Layton 	return mode;
82*24604f7eSJeff Layton }
83*24604f7eSJeff Layton 
ceph_caps_for_mode(int mode)84*24604f7eSJeff Layton int ceph_caps_for_mode(int mode)
85*24604f7eSJeff Layton {
86*24604f7eSJeff Layton 	int caps = CEPH_CAP_PIN;
87*24604f7eSJeff Layton 
88*24604f7eSJeff Layton 	if (mode & CEPH_FILE_MODE_RD)
89*24604f7eSJeff Layton 		caps |= CEPH_CAP_FILE_SHARED |
90*24604f7eSJeff Layton 			CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE;
91*24604f7eSJeff Layton 	if (mode & CEPH_FILE_MODE_WR)
92*24604f7eSJeff Layton 		caps |= CEPH_CAP_FILE_EXCL |
93*24604f7eSJeff Layton 			CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER |
94*24604f7eSJeff Layton 			CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL |
95*24604f7eSJeff Layton 			CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL;
96*24604f7eSJeff Layton 	if (mode & CEPH_FILE_MODE_LAZY)
97*24604f7eSJeff Layton 		caps |= CEPH_CAP_FILE_LAZYIO;
98*24604f7eSJeff Layton 
99*24604f7eSJeff Layton 	return caps;
100*24604f7eSJeff Layton }
101