4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * (C) Copyright 2014 Commissariat a l'energie atomique et aux energies
9 * All rights reserved. This program and the accompanying materials
10 * are made available under the terms of the GNU Lesser General Public License
11 * (LGPL) version 2.1 or (at your discretion) any later version.
12 * (LGPL) version 2.1 accompanies this distribution, and is available at
13 * http://www.gnu.org/licenses/lgpl-2.1.html
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
23 * lustre/utils/liblustreapi_lease.c
25 * lustreapi library for file leases
27 * Author: Henri Doreau <henri.doreau@cea.fr>
32 #include <sys/ioctl.h>
34 #include <lustre/lustreapi.h>
35 #include "lustreapi_internal.h"
37 static inline const char *lease_mode2str(int mode)
40 case LL_LEASE_WRLCK: return "WRITE";
41 case LL_LEASE_RDLCK: return "READ";
42 case LL_LEASE_UNLCK: return "UNLOCK";
48 * Extend lease get support.
50 * \param fd File to get lease on.
51 * \param data ll_ioc_lease data.
53 * \retval 0 on success.
54 * \retval -errno on error.
56 int llapi_lease_get_ext(int fd, struct ll_ioc_lease *data)
60 rc = ioctl(fd, LL_IOC_SET_LEASE, data);
64 /* exclude ENOTTY in case this is an old kernel that only
65 * supports LL_IOC_SET_LEASE_OLD */
67 llapi_error(LLAPI_MSG_ERROR, rc,
68 "cannot get %s lease, ext %x",
69 lease_mode2str(data->lil_mode),
76 * Get a lease on an open file.
78 * \param fd File to get the lease on.
79 * \param mode Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
81 * \retval 0 on success.
82 * \retval -errno on error.
84 int llapi_lease_get(int fd, int mode)
86 struct ll_ioc_lease data = { 0 };
89 if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
93 rc = llapi_lease_get_ext(fd, &data);
95 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
104 * Check if a lease is still set on a file.
106 * \param fd File to check the lease on.
108 * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
109 * \retval 0 if no lease is present.
110 * \retval -errno on error.
112 int llapi_lease_check(int fd)
116 rc = ioctl(fd, LL_IOC_GET_LEASE);
119 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
127 * \param fd File to remove the lease from.
129 * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
130 * \retval 0 if no lease was present.
131 * \retval -errno on error.
133 int llapi_lease_put(int fd)
135 struct ll_ioc_lease data = { .lil_mode = LL_LEASE_UNLCK };
137 return llapi_lease_get_ext(fd, &data);