Whamcloud - gitweb
LU-7931 tests: setup/cleanup after every test script
[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
38 static inline const char *lease_mode2str(int mode)
39 {
40         switch (mode) {
41         case LL_LEASE_WRLCK: return "WRITE";
42         case LL_LEASE_RDLCK: return "READ";
43         case LL_LEASE_UNLCK: return "UNLOCK";
44         }
45         return "???";
46 }
47
48 /**
49  * Get a lease on an open file.
50  *
51  * \param fd    File to get the lease on.
52  * \param mode  Lease mode, either LL_LEASE_RDLCK or LL_LEASE_WRLCK.
53  *
54  * \retval 0 on success.
55  * \retval -errno on error.
56  */
57 int llapi_lease_get(int fd, int mode)
58 {
59         int rc;
60
61         if (mode != LL_LEASE_RDLCK && mode != LL_LEASE_WRLCK)
62                 return -EINVAL;
63
64         rc = ioctl(fd, LL_IOC_SET_LEASE, mode);
65         if (rc < 0) {
66                 rc = -errno;
67                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get %s lease",
68                             lease_mode2str(mode));
69         }
70         return rc;
71 }
72
73 /**
74  * Check if a lease is still set on a file.
75  *
76  * \param fd    File to check the lease on.
77  *
78  * \retval lease type if present (LL_LEASE_READ or LL_LEASE_WRITE).
79  * \retval 0 if no lease is present.
80  * \retval -errno on error.
81  */
82 int llapi_lease_check(int fd)
83 {
84         int rc;
85
86         rc = ioctl(fd, LL_IOC_GET_LEASE);
87         if (rc < 0) {
88                 rc = -errno;
89                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot check lease");
90         }
91         return rc;
92 }
93
94 /**
95  * Remove a lease.
96  *
97  * \param fd    File to remove the lease from.
98  *
99  * \retval type of the lease that was removed (LL_LEASE_READ or LL_LEASE_WRITE).
100  * \retval 0 if no lease was present.
101  * \retval -errno on error.
102  */
103 int llapi_lease_put(int fd)
104 {
105         int rc;
106
107         rc = ioctl(fd, LL_IOC_SET_LEASE, LL_LEASE_UNLCK);
108         if (rc < 0) {
109                 rc = -errno;
110                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put lease");
111         }
112         return rc;
113 }