Whamcloud - gitweb
LU-4928 utils: LLAPI helpers for file leases 22/10022/8
authorHenri Doreau <henri.doreau@cea.fr>
Fri, 18 Apr 2014 19:47:37 +0000 (21:47 +0200)
committerOleg Drokin <oleg.drokin@intel.com>
Fri, 5 Sep 2014 00:25:12 +0000 (00:25 +0000)
Introduced llapi_lease_{get,check,put} functions to abstract the ioctl
implementation to manipulate file leases.

Signed-off-by: Henri Doreau <henri.doreau@cea.fr>
Change-Id: I956408defe550c1b432526e142825ea736d0e285
Reviewed-on: http://review.whamcloud.com/10022
Tested-by: Jenkins
Reviewed-by: Jinshan Xiong <jinshan.xiong@intel.com>
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Tested-by: Maloo <hpdd-maloo@intel.com>
lustre/include/lustre/lustreapi.h
lustre/utils/Makefile.am
lustre/utils/liblustreapi_lease.c [new file with mode: 0644]

index 5cccbf4..380dfc5 100644 (file)
@@ -375,6 +375,13 @@ extern int llapi_json_add_item(struct llapi_json_item_list **item_list,
 extern int llapi_json_write_list(struct llapi_json_item_list **item_list,
                                 FILE *fp);
 
+/* File lease */
+extern int llapi_lease_get(int fd, int mode);
+extern int llapi_lease_check(int fd);
+extern int llapi_lease_put(int fd);
+
+/** @} llapi */
+
 /* llapi_layout user interface */
 
 /** Opaque data type abstracting the layout of a Lustre file. */
index f56f1f7..5aefe21 100644 (file)
@@ -87,6 +87,7 @@ L_KERNELCOMM := $(top_builddir)/libcfs/libcfs/kernel_user_comm.c
 liblustreapitmp_a_SOURCES = liblustreapi.c liblustreapi_hsm.c \
                            liblustreapi_nodemap.c lustreapi_internal.h \
                            liblustreapi_json.c liblustreapi_layout.c \
+                           liblustreapi_lease.c \
                            $(L_IOCTL) $(L_KERNELCOMM) $(L_STRING)
 
 # build static and shared lib lustreapi
diff --git a/lustre/utils/liblustreapi_lease.c b/lustre/utils/liblustreapi_lease.c
new file mode 100644 (file)
index 0000000..d6063d4
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * LGPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * (C) Copyright 2014 Commissariat a l'energie atomique et aux energies
+ *     alternatives
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the GNU Lesser General Public License
+ * (LGPL) version 2.1 or (at your discretion) any later version.
+ * (LGPL) version 2.1 accompanies this distribution, and is available at
+ * http://www.gnu.org/licenses/lgpl-2.1.html
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * LGPL HEADER END
+ */
+/*
+ * lustre/utils/liblustreapi_lease.c
+ *
+ * lustreapi library for file leases
+ *
+ * Author: Henri Doreau <henri.doreau@cea.fr>
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <lustre/lustreapi.h>
+#include "lustreapi_internal.h"
+
+
+static inline const char *lease_mode2str(int mode)
+{
+       switch (mode) {
+       case LL_LEASE_WRLCK: return "WRITE";
+       case LL_LEASE_RDLCK: return "READ";
+       case LL_LEASE_UNLCK: return "UNLOCK";
+       }
+       return "???";
+}
+
+/**
+ * Get a lease on an open file.
+ *
+ * \param fd    File to get the lease on.
+ * \param mode  Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
+ *
+ * \retval 0 on success.
+ * \retval -errno on error.
+ */
+int llapi_lease_get(int fd, int mode)
+{
+       int rc;
+
+       if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
+               return -EINVAL;
+
+       rc = ioctl(fd, LL_IOC_SET_LEASE, mode);
+       if (rc < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "cannot get %s lease",
+                           lease_mode2str(mode));
+       }
+       return rc;
+}
+
+/**
+ * Check if a lease is still set on a file.
+ *
+ * \param fd    File to check the lease on.
+ *
+ * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
+ * \retval 0 if no lease is present.
+ * \retval -errno on error.
+ */
+int llapi_lease_check(int fd)
+{
+       int rc;
+
+       rc = ioctl(fd, LL_IOC_GET_LEASE);
+       if (rc < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
+       }
+       return rc;
+}
+
+/**
+ * Remove a lease.
+ *
+ * \param fd    File to remove the lease from.
+ *
+ * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
+ * \retval 0 if no lease was present.
+ * \retval -errno on error.
+ */
+int llapi_lease_put(int fd)
+{
+       int rc;
+
+       rc = ioctl(fd, LL_IOC_SET_LEASE, LL_LEASE_UNLCK);
+       if (rc < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "cannot put lease");
+       }
+       return rc;
+}