From e5f0eff4514adbfca7a7de6a6e4c1d0448089698 Mon Sep 17 00:00:00 2001 From: Henri Doreau Date: Fri, 18 Apr 2014 21:47:37 +0200 Subject: [PATCH] LU-4928 utils: LLAPI helpers for file leases Introduced llapi_lease_{get,check,put} functions to abstract the ioctl implementation to manipulate file leases. Signed-off-by: Henri Doreau Change-Id: I956408defe550c1b432526e142825ea736d0e285 Reviewed-on: http://review.whamcloud.com/10022 Tested-by: Jenkins Reviewed-by: Jinshan Xiong Reviewed-by: Andreas Dilger Tested-by: Maloo --- lustre/include/lustre/lustreapi.h | 7 +++ lustre/utils/Makefile.am | 1 + lustre/utils/liblustreapi_lease.c | 113 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 lustre/utils/liblustreapi_lease.c diff --git a/lustre/include/lustre/lustreapi.h b/lustre/include/lustre/lustreapi.h index 5cccbf4..380dfc5 100644 --- a/lustre/include/lustre/lustreapi.h +++ b/lustre/include/lustre/lustreapi.h @@ -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. */ diff --git a/lustre/utils/Makefile.am b/lustre/utils/Makefile.am index f56f1f7..5aefe21 100644 --- a/lustre/utils/Makefile.am +++ b/lustre/utils/Makefile.am @@ -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 index 0000000..d6063d4 --- /dev/null +++ b/lustre/utils/liblustreapi_lease.c @@ -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 + */ + +#include +#include +#include +#include +#include +#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; +} -- 1.8.3.1