124604f7eSJeff Layton // SPDX-License-Identifier: GPL-2.0 224604f7eSJeff Layton /* 324604f7eSJeff Layton * Some non-inline ceph helpers 424604f7eSJeff Layton */ 524604f7eSJeff Layton #include <linux/module.h> 624604f7eSJeff Layton #include <linux/ceph/types.h> 724604f7eSJeff Layton 824604f7eSJeff Layton /* 924604f7eSJeff Layton * return true if @layout appears to be valid 1024604f7eSJeff Layton */ ceph_file_layout_is_valid(const struct ceph_file_layout * layout)1124604f7eSJeff Laytonint ceph_file_layout_is_valid(const struct ceph_file_layout *layout) 1224604f7eSJeff Layton { 1324604f7eSJeff Layton __u32 su = layout->stripe_unit; 1424604f7eSJeff Layton __u32 sc = layout->stripe_count; 1524604f7eSJeff Layton __u32 os = layout->object_size; 1624604f7eSJeff Layton 1724604f7eSJeff Layton /* stripe unit, object size must be non-zero, 64k increment */ 1824604f7eSJeff Layton if (!su || (su & (CEPH_MIN_STRIPE_UNIT-1))) 1924604f7eSJeff Layton return 0; 2024604f7eSJeff Layton if (!os || (os & (CEPH_MIN_STRIPE_UNIT-1))) 2124604f7eSJeff Layton return 0; 2224604f7eSJeff Layton /* object size must be a multiple of stripe unit */ 2324604f7eSJeff Layton if (os < su || os % su) 2424604f7eSJeff Layton return 0; 2524604f7eSJeff Layton /* stripe count must be non-zero */ 2624604f7eSJeff Layton if (!sc) 2724604f7eSJeff Layton return 0; 2824604f7eSJeff Layton return 1; 2924604f7eSJeff Layton } 3024604f7eSJeff Layton ceph_file_layout_from_legacy(struct ceph_file_layout * fl,struct ceph_file_layout_legacy * legacy)3124604f7eSJeff Laytonvoid ceph_file_layout_from_legacy(struct ceph_file_layout *fl, 3224604f7eSJeff Layton struct ceph_file_layout_legacy *legacy) 3324604f7eSJeff Layton { 3424604f7eSJeff Layton fl->stripe_unit = le32_to_cpu(legacy->fl_stripe_unit); 3524604f7eSJeff Layton fl->stripe_count = le32_to_cpu(legacy->fl_stripe_count); 3624604f7eSJeff Layton fl->object_size = le32_to_cpu(legacy->fl_object_size); 3724604f7eSJeff Layton fl->pool_id = le32_to_cpu(legacy->fl_pg_pool); 3824604f7eSJeff Layton if (fl->pool_id == 0 && fl->stripe_unit == 0 && 3924604f7eSJeff Layton fl->stripe_count == 0 && fl->object_size == 0) 4024604f7eSJeff Layton fl->pool_id = -1; 4124604f7eSJeff Layton } 4224604f7eSJeff Layton ceph_file_layout_to_legacy(struct ceph_file_layout * fl,struct ceph_file_layout_legacy * legacy)4324604f7eSJeff Laytonvoid ceph_file_layout_to_legacy(struct ceph_file_layout *fl, 4424604f7eSJeff Layton struct ceph_file_layout_legacy *legacy) 4524604f7eSJeff Layton { 4624604f7eSJeff Layton legacy->fl_stripe_unit = cpu_to_le32(fl->stripe_unit); 4724604f7eSJeff Layton legacy->fl_stripe_count = cpu_to_le32(fl->stripe_count); 4824604f7eSJeff Layton legacy->fl_object_size = cpu_to_le32(fl->object_size); 4924604f7eSJeff Layton if (fl->pool_id >= 0) 5024604f7eSJeff Layton legacy->fl_pg_pool = cpu_to_le32(fl->pool_id); 5124604f7eSJeff Layton else 5224604f7eSJeff Layton legacy->fl_pg_pool = 0; 5324604f7eSJeff Layton } 5424604f7eSJeff Layton ceph_flags_to_mode(int flags)5524604f7eSJeff Laytonint ceph_flags_to_mode(int flags) 5624604f7eSJeff Layton { 5724604f7eSJeff Layton int mode; 5824604f7eSJeff Layton 5924604f7eSJeff Layton #ifdef O_DIRECTORY /* fixme */ 6024604f7eSJeff Layton if ((flags & O_DIRECTORY) == O_DIRECTORY) 6124604f7eSJeff Layton return CEPH_FILE_MODE_PIN; 6224604f7eSJeff Layton #endif 6324604f7eSJeff Layton 6424604f7eSJeff Layton switch (flags & O_ACCMODE) { 6524604f7eSJeff Layton case O_WRONLY: 6624604f7eSJeff Layton mode = CEPH_FILE_MODE_WR; 6724604f7eSJeff Layton break; 6824604f7eSJeff Layton case O_RDONLY: 6924604f7eSJeff Layton mode = CEPH_FILE_MODE_RD; 7024604f7eSJeff Layton break; 7124604f7eSJeff Layton case O_RDWR: 7224604f7eSJeff Layton case O_ACCMODE: /* this is what the VFS does */ 7324604f7eSJeff Layton mode = CEPH_FILE_MODE_RDWR; 7424604f7eSJeff Layton break; 7524604f7eSJeff Layton } 7624604f7eSJeff Layton #ifdef O_LAZY 7724604f7eSJeff Layton if (flags & O_LAZY) 7824604f7eSJeff Layton mode |= CEPH_FILE_MODE_LAZY; 7924604f7eSJeff Layton #endif 8024604f7eSJeff Layton 8124604f7eSJeff Layton return mode; 8224604f7eSJeff Layton } 8324604f7eSJeff Layton ceph_caps_for_mode(int mode)8424604f7eSJeff Laytonint ceph_caps_for_mode(int mode) 8524604f7eSJeff Layton { 8624604f7eSJeff Layton int caps = CEPH_CAP_PIN; 8724604f7eSJeff Layton 8824604f7eSJeff Layton if (mode & CEPH_FILE_MODE_RD) 8924604f7eSJeff Layton caps |= CEPH_CAP_FILE_SHARED | 9024604f7eSJeff Layton CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE; 9124604f7eSJeff Layton if (mode & CEPH_FILE_MODE_WR) 9224604f7eSJeff Layton caps |= CEPH_CAP_FILE_EXCL | 9324604f7eSJeff Layton CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | 9424604f7eSJeff Layton CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL | 9524604f7eSJeff Layton CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL; 9624604f7eSJeff Layton if (mode & CEPH_FILE_MODE_LAZY) 9724604f7eSJeff Layton caps |= CEPH_CAP_FILE_LAZYIO; 9824604f7eSJeff Layton 9924604f7eSJeff Layton return caps; 10024604f7eSJeff Layton } 101