Whamcloud - gitweb
LU-9934 build: address issues raised by gcc7
[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  * Copyright (c) 2017, Intel Corporation.
10  *
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
16  *
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.
21  *
22  * LGPL HEADER END
23  */
24 /*
25  * lustre/utils/liblustreapi_lease.c
26  *
27  * lustreapi library for file leases
28  *
29  * Author: Henri Doreau <henri.doreau@cea.fr>
30  */
31
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #include <errno.h>
36 #include <lustre/lustreapi.h>
37 #include "lustreapi_internal.h"
38
39 static inline const char *lease_mode2str(int mode)
40 {
41         switch (mode) {
42         case LL_LEASE_WRLCK: return "WRITE";
43         case LL_LEASE_RDLCK: return "READ";
44         case LL_LEASE_UNLCK: return "UNLOCK";
45         }
46         return "???";
47 }
48
49 /**
50  * Extend lease get support.
51  *
52  * \param fd    File to get lease on.
53  * \param data  ll_ioc_lease data.
54  *
55  * For getting lease lock, it will return zero for success. For unlock, it will
56  * return the lock type it owned for succuess.
57  *
58  * \retval >= 0 on success.
59  * \retval -errno on error.
60  */
61 int llapi_lease_get_ext(int fd, struct ll_ioc_lease *data)
62 {
63         int rc;
64
65         rc = ioctl(fd, LL_IOC_SET_LEASE, data);
66         if (rc < 0) {
67                 rc = -errno;
68
69                 /* exclude ENOTTY in case this is an old kernel that only
70                  * supports LL_IOC_SET_LEASE_OLD */
71                 if (rc != -ENOTTY)
72                         llapi_error(LLAPI_MSG_ERROR, rc,
73                                     "cannot get %s lease, ext %x",
74                                     lease_mode2str(data->lil_mode),
75                                     data->lil_flags);
76         }
77         return rc;
78 }
79
80 /**
81  * Get a lease on an open file.
82  *
83  * \param fd    File to get the lease on.
84  * \param mode  Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
85  *
86  * \see llapi_lease_get_ext().
87  *
88  * \retval >= 0 on success.
89  * \retval -errno on error.
90  */
91 int llapi_lease_get(int fd, int mode)
92 {
93         struct ll_ioc_lease data = { 0 };
94         int rc;
95
96         if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
97                 return -EINVAL;
98
99         data.lil_mode = mode;
100         rc = llapi_lease_get_ext(fd, &data);
101         if (rc == -ENOTTY) {
102                 rc = ioctl(fd, LL_IOC_SET_LEASE_OLD, mode);
103                 if (rc < 0)
104                         rc = -errno;
105         }
106
107         return rc;
108 }
109
110 /**
111  * Check if a lease is still set on a file.
112  *
113  * \param fd    File to check the lease on.
114  *
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.
118  */
119 int llapi_lease_check(int fd)
120 {
121         int rc;
122
123         rc = ioctl(fd, LL_IOC_GET_LEASE);
124         if (rc < 0) {
125                 rc = -errno;
126                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
127         }
128         return rc;
129 }
130
131 /**
132  * Remove a lease.
133  *
134  * \param fd    File to remove the lease from.
135  *
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.
139  */
140 int llapi_lease_put(int fd)
141 {
142         struct ll_ioc_lease data = { .lil_mode = LL_LEASE_UNLCK };
143
144         return llapi_lease_get_ext(fd, &data);
145 }