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 * Copyright (c) 2017, Intel Corporation.
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the GNU Lesser General Public License
13 * (LGPL) version 2.1 or (at your discretion) any later version.
14 * (LGPL) version 2.1 accompanies this distribution, and is available at
15 * http://www.gnu.org/licenses/lgpl-2.1.html
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
25 * lustre/utils/liblustreapi_lease.c
27 * lustreapi library for file leases
29 * Author: Henri Doreau <henri.doreau@cea.fr>
34 #include <sys/ioctl.h>
36 #include <lustre/lustreapi.h>
37 #include "lustreapi_internal.h"
39 static inline const char *lease_mode2str(enum ll_lease_mode mode)
42 case LL_LEASE_WRLCK: return "WRITE";
43 case LL_LEASE_RDLCK: return "READ";
44 case LL_LEASE_UNLCK: return "UNLOCK";
50 * Extend lease set support.
52 * \param fd File to set lease on.
53 * \param data ll_ioc_lease data.
55 * For setting lease lock, it will return zero for success. For unlock, it will
56 * return the lock type it owned for succuess.
58 * \retval >= 0 on success.
59 * \retval -errno on error.
61 int llapi_lease_set(int fd, const struct ll_ioc_lease *data)
65 rc = ioctl(fd, LL_IOC_SET_LEASE, data);
69 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get %s lease, ext %x",
70 lease_mode2str(data->lil_mode), data->lil_flags);
76 * Acquire 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 * \see llapi_lease_release().
83 * \retval >= 0 on success.
84 * \retval -errno on error.
86 int llapi_lease_acquire(int fd, enum ll_lease_mode mode)
88 struct ll_ioc_lease data = { .lil_mode = mode };
91 if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
94 rc = llapi_lease_set(fd, &data);
96 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
107 * \param fd File to remove the lease from.
109 * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
110 * \retval 0 if no lease was present.
111 * \retval -errno on error.
113 int llapi_lease_release(int fd)
115 struct ll_ioc_lease data = { .lil_mode = LL_LEASE_UNLCK };
117 return llapi_lease_set(fd, &data);
121 * Release a lease with intent operation. This API will release the lease
122 * and execute the intent operation atomically.
124 * \param fd File to remove the lease from.
126 * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
127 * \retval 0 if no lease was present.
128 * \retval -EBUSY lease broken, intent operation not executed.
129 * \retval -errno on error.
131 int llapi_lease_release_intent(int fd, struct ll_ioc_lease *data)
133 if (data->lil_mode != LL_LEASE_UNLCK)
136 return llapi_lease_set(fd, data);
140 * Check if a lease is still set on a file.
142 * \param fd File to check the lease on.
144 * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
145 * \retval 0 if no lease is present.
146 * \retval -errno on error.
148 int llapi_lease_check(int fd)
152 rc = ioctl(fd, LL_IOC_GET_LEASE);
155 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
161 * XXX: This is an obsoleted API - do not use it any more.
163 int llapi_lease_get(int fd, int mode)
167 if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
170 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
178 * XXX: This is an obsoleted API - do not use it any more.
180 int llapi_lease_put(int fd)
184 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, LL_LEASE_UNLCK);