Whamcloud - gitweb
LU-9771 flr: lfs mirror resync command
[fs/lustre-release.git] / lustre / utils / liblustreapi_lease.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * (C) Copyright 2014 Commissariat a l'energie atomique et aux energies
7  *     alternatives
8  *
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
14  *
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.
19  *
20  * LGPL HEADER END
21  */
22 /*
23  * lustre/utils/liblustreapi_lease.c
24  *
25  * lustreapi library for file leases
26  *
27  * Author: Henri Doreau <henri.doreau@cea.fr>
28  */
29
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <errno.h>
34 #include <lustre/lustreapi.h>
35 #include "lustreapi_internal.h"
36
37 static inline const char *lease_mode2str(int mode)
38 {
39         switch (mode) {
40         case LL_LEASE_WRLCK: return "WRITE";
41         case LL_LEASE_RDLCK: return "READ";
42         case LL_LEASE_UNLCK: return "UNLOCK";
43         }
44         return "???";
45 }
46
47 /**
48  * Extend lease get support.
49  *
50  * \param fd    File to get lease on.
51  * \param data  ll_ioc_lease data.
52  *
53  * For getting lease lock, it will return zero for success. For unlock, it will
54  * return the lock type it owned for succuess.
55  *
56  * \retval >= 0 on success.
57  * \retval -errno on error.
58  */
59 int llapi_lease_get_ext(int fd, struct ll_ioc_lease *data)
60 {
61         int rc;
62
63         rc = ioctl(fd, LL_IOC_SET_LEASE, data);
64         if (rc < 0) {
65                 rc = -errno;
66
67                 /* exclude ENOTTY in case this is an old kernel that only
68                  * supports LL_IOC_SET_LEASE_OLD */
69                 if (rc != -ENOTTY)
70                         llapi_error(LLAPI_MSG_ERROR, rc,
71                                     "cannot get %s lease, ext %x",
72                                     lease_mode2str(data->lil_mode),
73                                     data->lil_flags);
74         }
75         return rc;
76 }
77
78 /**
79  * Get a lease on an open file.
80  *
81  * \param fd    File to get the lease on.
82  * \param mode  Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
83  *
84  * \see llapi_lease_get_ext().
85  *
86  * \retval >= 0 on success.
87  * \retval -errno on error.
88  */
89 int llapi_lease_get(int fd, int mode)
90 {
91         struct ll_ioc_lease data = { 0 };
92         int rc;
93
94         if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
95                 return -EINVAL;
96
97         data.lil_mode = mode;
98         rc = llapi_lease_get_ext(fd, &data);
99         if (rc == -ENOTTY) {
100                 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
101                 if (rc < 0)
102                         rc = -errno;
103         }
104
105         return rc;
106 }
107
108 /**
109  * Check if a lease is still set on a file.
110  *
111  * \param fd    File to check the lease on.
112  *
113  * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
114  * \retval 0 if no lease is present.
115  * \retval -errno on error.
116  */
117 int llapi_lease_check(int fd)
118 {
119         int rc;
120
121         rc = ioctl(fd, LL_IOC_GET_LEASE);
122         if (rc < 0) {
123                 rc = -errno;
124                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
125         }
126         return rc;
127 }
128
129 /**
130  * Remove a lease.
131  *
132  * \param fd    File to remove the lease from.
133  *
134  * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
135  * \retval 0 if no lease was present.
136  * \retval -errno on error.
137  */
138 int llapi_lease_put(int fd)
139 {
140         struct ll_ioc_lease data = { .lil_mode = LL_LEASE_UNLCK };
141
142         return llapi_lease_get_ext(fd, &data);
143 }