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(int 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 get support.
52 * \param fd File to get lease on.
53 * \param data ll_ioc_lease data.
55 * For getting 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_get_ext(int fd, struct ll_ioc_lease *data)
65 rc = ioctl(fd, LL_IOC_SET_LEASE, data);
69 /* exclude ENOTTY in case this is an old kernel that only
70 * supports LL_IOC_SET_LEASE_OLD */
72 llapi_error(LLAPI_MSG_ERROR, rc,
73 "cannot get %s lease, ext %x",
74 lease_mode2str(data->lil_mode),
81 * Get a lease on an open file.
83 * \param fd File to get the lease on.
84 * \param mode Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
86 * \see llapi_lease_get_ext().
88 * \retval >= 0 on success.
89 * \retval -errno on error.
91 int llapi_lease_get(int fd, int mode)
93 struct ll_ioc_lease data = { 0 };
96 if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
100 rc = llapi_lease_get_ext(fd, &data);
102 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
111 * Check if a lease is still set on a file.
113 * \param fd File to check the lease on.
115 * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
116 * \retval 0 if no lease is present.
117 * \retval -errno on error.
119 int llapi_lease_check(int fd)
123 rc = ioctl(fd, LL_IOC_GET_LEASE);
126 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
134 * \param fd File to remove the lease from.
136 * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
137 * \retval 0 if no lease was present.
138 * \retval -errno on error.
140 int llapi_lease_put(int fd)
142 struct ll_ioc_lease data = { .lil_mode = LL_LEASE_UNLCK };
144 return llapi_lease_get_ext(fd, &data);