2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
22 import java.io.IOException;
23 import java.io.FileNotFoundException;
24 import java.net.InetAddress;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantReadWriteLock;
27 import java.lang.String;
29 import com.ceph.crush.Bucket;
31 public class CephMount {
34 * Set via JNI callback in native_ceph_create
38 private long instance_ptr;
43 * Must be synchronized with JNI if changed.
45 public static final int O_RDONLY = 1;
46 public static final int O_RDWR = 2;
47 public static final int O_APPEND = 4;
48 public static final int O_CREAT = 8;
49 public static final int O_TRUNC = 16;
50 public static final int O_EXCL = 32;
51 public static final int O_WRONLY = 64;
52 public static final int O_DIRECTORY = 128;
55 * Whence flags for seek().
57 * Must be synchronized with JNI if changed.
59 public static final int SEEK_SET = 1;
60 public static final int SEEK_CUR = 2;
61 public static final int SEEK_END = 3;
64 * Attribute flags for setattr().
66 * Must be synchronized with JNI if changed.
68 public static final int SETATTR_MODE = 1;
69 public static final int SETATTR_UID = 2;
70 public static final int SETATTR_GID = 4;
71 public static final int SETATTR_MTIME = 8;
72 public static final int SETATTR_ATIME = 16;
75 * Flags for setxattr();
77 * Must be synchronized with JNI if changed.
79 public static final int XATTR_CREATE = 1;
80 public static final int XATTR_REPLACE = 2;
81 public static final int XATTR_NONE = 3;
86 * Must be synchronized with JNI if changed.
88 public static final int LOCK_SH = 1;
89 public static final int LOCK_EX = 2;
90 public static final int LOCK_NB = 4;
91 public static final int LOCK_UN = 8;
94 * This is run by the class loader and will report early any problems
95 * finding or linking in the shared JNI library.
101 static synchronized void loadLibrary() {
102 CephNativeLoader.getInstance().loadLibrary();
106 * Package-private: called from CephNativeLoader
108 static native void native_initialize();
111 * RW lock used for fine grained synchronization to native
113 private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
114 private final Lock rlock = rwlock.readLock();
115 private final Lock wlock = rwlock.writeLock();
118 * Controls clean-up synchronization between the constructor and finalize().
119 * If native_ceph_create fails, then we want a call to finalize() to not
120 * attempt to clean-up native context, because there is none.
122 private boolean initialized = false;
125 * Try to clean-up. First, unmount() will catch users who forget to do the
126 * unmount manually. Second, release() will destroy the entire context. It
127 * is safe to call release after a failure in unmount.
129 protected void finalize() throws Throwable {
133 } catch (Exception e) {}
135 native_ceph_release(instance_ptr);
136 } catch (Exception e) {}
142 * Create a new CephMount with specific client id.
144 * @param id client id.
146 public CephMount(String id) {
147 native_ceph_create(this, id);
151 private static native int native_ceph_create(CephMount mount, String id);
154 * Create a new CephMount with default client id.
161 * Activate the mount with a given root path.
163 * @param root The path to use as the root (pass null for "/").
165 public void mount(String root) {
168 native_ceph_mount(instance_ptr, root);
174 private static native int native_ceph_mount(long mountp, String root);
177 * Deactivate the mount.
179 * The mount can be reactivated using mount(). Configuration parameters
180 * previously set are not reset.
182 public void unmount() {
185 native_ceph_unmount(instance_ptr);
191 private static native int native_ceph_unmount(long mountp);
194 * Private access to low-level ceph_release.
196 private static native int native_ceph_release(long mountp);
199 * Load configuration from a file.
201 * @param path The path to the configuration file.
203 public void conf_read_file(String path) throws FileNotFoundException {
206 native_ceph_conf_read_file(instance_ptr, path);
212 private static native int native_ceph_conf_read_file(long mountp, String path);
215 * Set the value of a configuration option.
217 * @param option The configuration option to modify.
218 * @param value The new value of the option.
220 public void conf_set(String option, String value) {
223 native_ceph_conf_set(instance_ptr, option, value);
229 private static native int native_ceph_conf_set(long mountp, String option, String value);
232 * Get the value of a configuration option.
234 * @param option The name of the configuration option.
235 * @return The value of the option or null if option not found
237 public String conf_get(String option) {
240 return native_ceph_conf_get(instance_ptr, option);
246 private static native String native_ceph_conf_get(long mountp, String option);
249 * Get file system status.
251 * @param path Path to file in file system.
252 * @param statvfs CephStatVFS structure to hold status.
254 public void statfs(String path, CephStatVFS statvfs) throws FileNotFoundException {
257 native_ceph_statfs(instance_ptr, path, statvfs);
263 private static native int native_ceph_statfs(long mountp, String path, CephStatVFS statvfs);
266 * Get the current working directory.
268 * @return The current working directory in Ceph.
270 public String getcwd() {
273 return native_ceph_getcwd(instance_ptr);
279 private static native String native_ceph_getcwd(long mountp);
282 * Set the current working directory.
284 * @param path The directory set as the cwd.
286 public void chdir(String path) throws FileNotFoundException {
289 native_ceph_chdir(instance_ptr, path);
295 private static native int native_ceph_chdir(long mountp, String cwd);
298 * List the contents of a directory.
300 * @param dir The directory.
301 * @return List of files and directories excluding "." and "..".
303 public String[] listdir(String dir) throws FileNotFoundException {
306 return native_ceph_listdir(instance_ptr, dir);
312 private static native String[] native_ceph_listdir(long mountp, String path);
315 * Create a hard link to an existing file.
317 * @param oldpath The target path of the link.
318 * @param newpath The name of the link.
320 public void link(String oldpath, String newpath) throws FileNotFoundException {
323 native_ceph_link(instance_ptr, oldpath, newpath);
329 private static native int native_ceph_link(long mountp, String existing, String newname);
332 * Unlink/delete a name from the file system.
334 * @param path The name to unlink/delete.
336 public void unlink(String path) throws FileNotFoundException {
339 native_ceph_unlink(instance_ptr, path);
345 private static native int native_ceph_unlink(long mountp, String path);
348 * Rename a file or directory.
350 * @param from The current path.
351 * @param to The new path.
353 public void rename(String from, String to) throws FileNotFoundException {
356 native_ceph_rename(instance_ptr, from, to);
362 private static native int native_ceph_rename(long mountp, String from, String to);
365 * Create a directory.
367 * @param path The directory to create.
368 * @param mode The mode of the new directory.
370 public void mkdir(String path, int mode) {
373 native_ceph_mkdir(instance_ptr, path, mode);
379 private static native int native_ceph_mkdir(long mountp, String path, int mode);
382 * Create a directory and all parents.
384 * @param path The directory to create.
385 * @param mode The mode of the new directory.
387 public void mkdirs(String path, int mode) throws IOException {
390 native_ceph_mkdirs(instance_ptr, path, mode);
396 private static native int native_ceph_mkdirs(long mountp, String path, int mode);
399 * Delete a directory.
401 * @param path The directory to delete.
403 public void rmdir(String path) throws FileNotFoundException {
406 native_ceph_rmdir(instance_ptr, path);
412 private static native int native_ceph_rmdir(long mountp, String path);
415 * Read the value of a symbolic link.
417 public String readlink(String path) throws FileNotFoundException {
420 return native_ceph_readlink(instance_ptr, path);
426 private static native String native_ceph_readlink(long mountp, String path);
429 * Create a symbolic link.
431 * @param oldpath Target of the symbolic link.
432 * @param newpath Name of the link.
434 public void symlink(String oldpath, String newpath) {
437 native_ceph_symlink(instance_ptr, oldpath, newpath);
443 private static native int native_ceph_symlink(long mountp, String existing, String newname);
448 * @param path Path of file to stat.
449 * @param stat CephStat structure to hold file status.
451 public void stat(String path, CephStat stat) throws FileNotFoundException, CephNotDirectoryException {
454 native_ceph_stat(instance_ptr, path, stat);
460 private static native int native_ceph_stat(long mountp, String path, CephStat stat);
463 * Get file status, without following symlinks.
465 * @param path Path of file to stat.
466 * @param stat CephStat structure to hold file status.
468 public void lstat(String path, CephStat stat) throws FileNotFoundException, CephNotDirectoryException {
471 native_ceph_lstat(instance_ptr, path, stat);
477 private static native int native_ceph_lstat(long mountp, String path, CephStat stat);
480 * Set file attributes.
482 * @param path Path to file.
483 * @param stat CephStat structure holding attributes.
484 * @param mask Mask specifying which attributes to set.
486 public void setattr(String path, CephStat stat, int mask) throws FileNotFoundException {
489 native_ceph_setattr(instance_ptr, path, stat, mask);
495 private static native int native_ceph_setattr(long mountp, String relpath, CephStat stat, int mask);
500 * @param path Path to file.
501 * @param mode New mode bits.
503 public void chmod(String path, int mode) throws FileNotFoundException {
506 native_ceph_chmod(instance_ptr, path, mode);
512 private static native int native_ceph_chmod(long mountp, String path, int mode);
515 * Change file mode of an open file.
517 * @param fd The open file descriptor to change the mode bits on.
518 * @param mode New mode bits.
520 public void fchmod(int fd, int mode) {
523 native_ceph_fchmod(instance_ptr, fd, mode);
529 private static native int native_ceph_fchmod(long mountp, int fd, int mode);
532 * Truncate a file to a specified length.
534 * @param path Path of the file.
535 * @param size New file length.
537 public void truncate(String path, long size) throws FileNotFoundException {
540 native_ceph_truncate(instance_ptr, path, size);
546 private static native int native_ceph_truncate(long mountp, String path, long size);
551 * @param path Path of file to open or create.
552 * @param flags Open flags.
553 * @param mode Permission mode.
554 * @return File descriptor.
556 public int open(String path, int flags, int mode) throws FileNotFoundException {
559 return native_ceph_open(instance_ptr, path, flags, mode);
565 private static native int native_ceph_open(long mountp, String path, int flags, int mode);
568 * Open a file with a specific file layout.
570 * @param path Path of file to open or create.
571 * @param flags Open flags.
572 * @param mode Permission mode.
573 * @param stripe_unit File layout stripe unit size.
574 * @param stripe_count File layout stripe count.
575 * @param object_size Size of each object.
576 * @param data_pool The target data pool.
577 * @return File descriptor.
579 public int open(String path, int flags, int mode, int stripe_unit, int stripe_count,
580 int object_size, String data_pool) throws FileNotFoundException {
583 return native_ceph_open_layout(instance_ptr, path, flags, mode, stripe_unit,
584 stripe_count, object_size, data_pool);
590 private static native int native_ceph_open_layout(long mountp, String path,
591 int flags, int mode, int stripe_unit, int stripe_count, int object_size, String data_pool);
594 * Close an open file.
596 * @param fd The file descriptor.
598 public void close(int fd) {
601 native_ceph_close(instance_ptr, fd);
607 private static native int native_ceph_close(long mountp, int fd);
610 * Seek to a position in a file.
612 * @param fd File descriptor.
613 * @param offset New offset.
614 * @param whence Whence value.
615 * @return The new offset.
617 public long lseek(int fd, long offset, int whence) {
620 return native_ceph_lseek(instance_ptr, fd, offset, whence);
626 private static native long native_ceph_lseek(long mountp, int fd, long offset, int whence);
631 * @param fd The file descriptor.
632 * @param buf Buffer to for data read.
633 * @param size Amount of data to read into the buffer.
634 * @param offset Offset to read from (-1 for current position).
635 * @return The number of bytes read.
637 public long read(int fd, byte[] buf, long size, long offset) {
640 return native_ceph_read(instance_ptr, fd, buf, size, offset);
646 private static native long native_ceph_read(long mountp, int fd, byte[] buf, long size, long offset);
649 * Write to a file at a specific offset.
651 * @param fd The file descriptor.
652 * @param buf Buffer to write.
653 * @param size Amount of data to write.
654 * @param offset Offset to write from (-1 for current position).
655 * @return The number of bytes written.
657 public long write(int fd, byte[] buf, long size, long offset) {
660 return native_ceph_write(instance_ptr, fd, buf, size, offset);
666 private static native long native_ceph_write(long mountp, int fd, byte[] buf, long size, long offset);
671 * @param fd File descriptor of the file to truncate.
672 * @param size New file size.
674 public void ftruncate(int fd, long size) {
677 native_ceph_ftruncate(instance_ptr, fd, size);
683 private static native int native_ceph_ftruncate(long mountp, int fd, long size);
686 * Synchronize a file with the file system.
688 * @param fd File descriptor to synchronize.
689 * @param dataonly Synchronize only data.
691 public void fsync(int fd, boolean dataonly) {
694 native_ceph_fsync(instance_ptr, fd, dataonly);
700 private static native int native_ceph_fsync(long mountp, int fd, boolean dataonly);
703 * Apply or remove an advisory lock.
705 * @param fd File descriptor to lock or unlock.
706 * @param operation the advisory lock operation to be performed on the file
707 * descriptor among LOCK_SH (shared lock), LOCK_EX (exclusive lock),
708 * or LOCK_UN (remove lock). The LOCK_NB value can be ORed to perform a
709 * non-blocking operation.
710 * @param owner the user-supplied owner identifier (an arbitrary integer)
712 public void flock(int fd, int operation, long owner) throws IOException {
715 native_ceph_flock(instance_ptr, fd, operation, owner);
721 private static native int native_ceph_flock(long mountp, int fd, int operation, long owner);
726 * @param fd The file descriptor.
727 * @param stat The object in which to store the status.
729 public void fstat(int fd, CephStat stat) {
732 native_ceph_fstat(instance_ptr, fd, stat);
738 private static native int native_ceph_fstat(long mountp, int fd, CephStat stat);
741 * Synchronize the client with the file system.
743 public void sync_fs() {
746 native_ceph_sync_fs(instance_ptr);
752 private static native int native_ceph_sync_fs(long mountp);
755 * Get an extended attribute value.
757 * If the buffer is large enough to hold the entire attribute value, or
758 * buf is null, the size of the value is returned.
760 * @param path File path.
761 * @param name Name of the attribute.
762 * @param buf Buffer to store attribute value.
763 * @return The length of the attribute value. See description for more
766 public long getxattr(String path, String name, byte[] buf) throws FileNotFoundException {
769 return native_ceph_getxattr(instance_ptr, path, name, buf);
775 private static native long native_ceph_getxattr(long mountp, String path, String name, byte[] buf);
778 * Get an extended attribute value of a symbolic link.
780 * If the buffer is large enough to hold the entire attribute value, or
781 * buf is null, the size of the value is returned.
783 * @param path File path.
784 * @param name Name of attribute.
785 * @param buf Buffer to store attribute value.
786 * @return The length of the attribute value. See description for more
789 public long lgetxattr(String path, String name, byte[] buf) throws FileNotFoundException {
792 return native_ceph_lgetxattr(instance_ptr, path, name, buf);
798 private static native long native_ceph_lgetxattr(long mountp, String path, String name, byte[] buf);
801 * List extended attributes.
803 * @param path File path.
804 * @return List of attribute names.
806 public String[] listxattr(String path) throws FileNotFoundException {
809 return native_ceph_listxattr(instance_ptr, path);
815 private static native String[] native_ceph_listxattr(long mountp, String path);
818 * List extended attributes of a symbolic link.
820 * @param path File path.
821 * @return List of attribute names.
823 public String[] llistxattr(String path) throws FileNotFoundException {
826 return native_ceph_llistxattr(instance_ptr, path);
832 private static native String[] native_ceph_llistxattr(long mountp, String path);
835 * Remove an extended attribute.
837 * @param path File path.
838 * @param name Name of attribute.
840 public void removexattr(String path, String name) throws FileNotFoundException {
843 native_ceph_removexattr(instance_ptr, path, name);
849 private static native int native_ceph_removexattr(long mountp, String path, String name);
852 * Remove an extended attribute from a symbolic link.
854 * @param path File path.
855 * @param name Name of attribute.
857 public void lremovexattr(String path, String name) throws FileNotFoundException {
860 native_ceph_lremovexattr(instance_ptr, path, name);
866 private static native int native_ceph_lremovexattr(long mountp, String path, String name);
869 * Set the value of an extended attribute.
871 * @param path The file path.
872 * @param name The attribute name.
873 * @param buf The attribute value.
874 * @param size The size of the attribute value.
875 * @param flags Flag controlling behavior (XATTR_CREATE/REPLACE/NONE).
877 public void setxattr(String path, String name, byte[] buf, long size, int flags) throws FileNotFoundException {
880 native_ceph_setxattr(instance_ptr, path, name, buf, size, flags);
886 private static native int native_ceph_setxattr(long mountp, String path, String name, byte[] buf, long size, int flags);
889 * Set the value of an extended attribute on a symbolic link.
891 * @param path The file path.
892 * @param name The attribute name.
893 * @param buf The attribute value.
894 * @param size The size of the attribute value.
895 * @param flags Flag controlling behavior (XATTR_CREATE/REPLACE/NONE).
897 public void lsetxattr(String path, String name, byte[] buf, long size, int flags) throws FileNotFoundException {
900 native_ceph_lsetxattr(instance_ptr, path, name, buf, size, flags);
906 private static native int native_ceph_lsetxattr(long mountp, String path, String name, byte[] buf, long size, int flags);
909 * Get the stripe unit of a file.
911 * @param fd The file descriptor.
912 * @return The stripe unit.
914 public int get_file_stripe_unit(int fd) {
917 return native_ceph_get_file_stripe_unit(instance_ptr, fd);
923 private static native int native_ceph_get_file_stripe_unit(long mountp, int fd);
926 * Get the name of the pool a file is stored in.
928 * @param fd An open file descriptor.
929 * @return The pool name.
931 public String get_file_pool_name(int fd) {
934 return native_ceph_get_file_pool_name(instance_ptr, fd);
940 private static native String native_ceph_get_file_pool_name(long mountp, int fd);
943 * Get the default data pool of cephfs.
945 * @return The pool name.
947 public String get_default_data_pool_name() {
950 return native_ceph_get_default_data_pool_name(instance_ptr);
956 private static native String native_ceph_get_default_data_pool_name(long mountp);
959 * Get the replication of a file.
961 * @param fd The file descriptor.
962 * @return The file replication.
964 public int get_file_replication(int fd) {
967 return native_ceph_get_file_replication(instance_ptr, fd);
973 private static native int native_ceph_get_file_replication(long mountp, int fd);
976 * Favor reading from local replicas when possible.
978 * @param state Enable or disable localized reads.
980 public void localize_reads(boolean state) {
983 native_ceph_localize_reads(instance_ptr, state);
989 private static native int native_ceph_localize_reads(long mountp, boolean on);
992 * Get file layout stripe unit granularity.
994 * @return Stripe unit granularity.
996 public int get_stripe_unit_granularity() {
999 return native_ceph_get_stripe_unit_granularity(instance_ptr);
1005 private static native int native_ceph_get_stripe_unit_granularity(long mountp);
1008 * Get the pool id for the named pool.
1010 * @param name The pool name.
1011 * @return The pool id.
1013 public int get_pool_id(String name) throws CephPoolException {
1016 return native_ceph_get_pool_id(instance_ptr, name);
1017 } catch (FileNotFoundException e) {
1018 throw new CephPoolException("pool name " + name + " not found");
1024 private static native int native_ceph_get_pool_id(long mountp, String name) throws FileNotFoundException;
1027 * Get the pool replication factor.
1029 * @param pool_id The pool id.
1030 * @return Number of replicas stored in the pool.
1032 public int get_pool_replication(int pool_id) throws CephPoolException {
1035 return native_ceph_get_pool_replication(instance_ptr, pool_id);
1036 } catch (FileNotFoundException e) {
1037 throw new CephPoolException("pool id " + pool_id + " not found");
1043 private static native int native_ceph_get_pool_replication(long mountp, int pool_id) throws FileNotFoundException;
1046 * Get file extent containing a given offset.
1048 * @param fd The file descriptor.
1049 * @param offset Offset in file.
1050 * @return A CephFileExtent object.
1052 public CephFileExtent get_file_extent(int fd, long offset) {
1055 return native_ceph_get_file_extent_osds(instance_ptr, fd, offset);
1061 private static native CephFileExtent native_ceph_get_file_extent_osds(long mountp, int fd, long offset);
1064 * Get the fully qualified CRUSH location of an OSD.
1066 * Returns (type, name) string pairs for each device in the CRUSH bucket
1067 * hierarchy starting from the given OSD to the root.
1069 * @param osd The OSD device id.
1070 * @return List of pairs.
1072 public Bucket[] get_osd_crush_location(int osd) {
1075 String[] parts = native_ceph_get_osd_crush_location(instance_ptr, osd);
1076 Bucket[] path = new Bucket[parts.length / 2];
1077 for (int i = 0; i < path.length; i++)
1078 path[i] = new Bucket(parts[i*2], parts[i*2+1]);
1085 private static native String[] native_ceph_get_osd_crush_location(long mountp, int osd);
1088 * Get the network address of an OSD.
1090 * @param osd The OSD device id.
1091 * @return The network address.
1093 public InetAddress get_osd_address(int osd) {
1096 return native_ceph_get_osd_addr(instance_ptr, osd);
1102 private static native InetAddress native_ceph_get_osd_addr(long mountp, int osd);