Whamcloud - gitweb
LU-12923 lustre: Replace CLASSERT() with BUILD_BUG_ON()
[fs/lustre-release.git] / lustre / obdclass / cl_lock.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Client Extent Lock.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #include <linux/list.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <cl_object.h>
46 #include "cl_internal.h"
47
48 static void cl_lock_trace0(int level, const struct lu_env *env,
49                            const char *prefix, const struct cl_lock *lock,
50                            const char *func, const int line)
51 {
52         struct cl_object_header *h = cl_object_header(lock->cll_descr.cld_obj);
53         CDEBUG(level, "%s: %p (%p/%d) at %s():%d\n",
54                prefix, lock, env, h->coh_nesting, func, line);
55 }
56 #define cl_lock_trace(level, env, prefix, lock)                         \
57         cl_lock_trace0(level, env, prefix, lock, __FUNCTION__, __LINE__)
58
59 /**
60  * Adds lock slice to the compound lock.
61  *
62  * This is called by cl_object_operations::coo_lock_init() methods to add a
63  * per-layer state to the lock. New state is added at the end of
64  * cl_lock::cll_layers list, that is, it is at the bottom of the stack.
65  *
66  * \see cl_req_slice_add(), cl_page_slice_add(), cl_io_slice_add()
67  */
68 void cl_lock_slice_add(struct cl_lock *lock, struct cl_lock_slice *slice,
69                        struct cl_object *obj,
70                        const struct cl_lock_operations *ops)
71 {
72         ENTRY;
73         slice->cls_lock = lock;
74         list_add_tail(&slice->cls_linkage, &lock->cll_layers);
75         slice->cls_obj = obj;
76         slice->cls_ops = ops;
77         EXIT;
78 }
79 EXPORT_SYMBOL(cl_lock_slice_add);
80
81 void cl_lock_fini(const struct lu_env *env, struct cl_lock *lock)
82 {
83         ENTRY;
84
85         cl_lock_trace(D_DLMTRACE, env, "destroy lock", lock);
86
87         while (!list_empty(&lock->cll_layers)) {
88                 struct cl_lock_slice *slice;
89
90                 slice = list_entry(lock->cll_layers.next,
91                                 struct cl_lock_slice, cls_linkage);
92                 list_del_init(lock->cll_layers.next);
93                 slice->cls_ops->clo_fini(env, slice);
94         }
95         POISON(lock, 0x5a, sizeof(*lock));
96         EXIT;
97 }
98 EXPORT_SYMBOL(cl_lock_fini);
99
100 int cl_lock_init(const struct lu_env *env, struct cl_lock *lock,
101                  const struct cl_io *io)
102 {
103         struct cl_object *obj = lock->cll_descr.cld_obj;
104         struct cl_object *scan;
105         int result = 0;
106         ENTRY;
107
108         /* Make sure cl_lock::cll_descr is initialized. */
109         LASSERT(obj != NULL);
110
111         INIT_LIST_HEAD(&lock->cll_layers);
112         list_for_each_entry(scan, &obj->co_lu.lo_header->loh_layers,
113                             co_lu.lo_linkage) {
114                 if (scan->co_ops->coo_lock_init != NULL)
115                         result = scan->co_ops->coo_lock_init(env, scan, lock,
116                                                              io);
117
118                 if (result != 0) {
119                         cl_lock_fini(env, lock);
120                         break;
121                 }
122         }
123         RETURN(result);
124 }
125 EXPORT_SYMBOL(cl_lock_init);
126
127 /**
128  * Returns a slice with a lock, corresponding to the given layer in the
129  * device stack.
130  *
131  * \see cl_page_at()
132  */
133 const struct cl_lock_slice *cl_lock_at(const struct cl_lock *lock,
134                                        const struct lu_device_type *dtype)
135 {
136         const struct cl_lock_slice *slice;
137
138         ENTRY;
139
140         list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
141                 if (slice->cls_obj->co_lu.lo_dev->ld_type == dtype)
142                         RETURN(slice);
143         }
144         RETURN(NULL);
145 }
146 EXPORT_SYMBOL(cl_lock_at);
147
148 void cl_lock_cancel(const struct lu_env *env, struct cl_lock *lock)
149 {
150         const struct cl_lock_slice *slice;
151         ENTRY;
152
153         cl_lock_trace(D_DLMTRACE, env, "cancel lock", lock);
154         list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
155                 if (slice->cls_ops->clo_cancel != NULL)
156                         slice->cls_ops->clo_cancel(env, slice);
157         }
158
159         EXIT;
160 }
161 EXPORT_SYMBOL(cl_lock_cancel);
162
163 /**
164  * Enqueue a lock.
165  * \param anchor: if we need to wait for resources before getting the lock,
166  *                use @anchor for the purpose.
167  * \retval 0  enqueue successfully
168  * \retval <0 error code
169  */
170 int cl_lock_enqueue(const struct lu_env *env, struct cl_io *io,
171                     struct cl_lock *lock, struct cl_sync_io *anchor)
172 {
173         const struct cl_lock_slice *slice;
174         int rc = 0;
175
176         ENTRY;
177
178         list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
179                 if (slice->cls_ops->clo_enqueue == NULL)
180                         continue;
181
182                 rc = slice->cls_ops->clo_enqueue(env, slice, io, anchor);
183                 if (rc != 0)
184                         break;
185         }
186         RETURN(rc);
187 }
188 EXPORT_SYMBOL(cl_lock_enqueue);
189
190 /**
191  * Main high-level entry point of cl_lock interface that finds existing or
192  * enqueues new lock matching given description.
193  */
194 int cl_lock_request(const struct lu_env *env, struct cl_io *io,
195                     struct cl_lock *lock)
196 {
197         struct cl_sync_io       *anchor = NULL;
198         __u32                   enq_flags = lock->cll_descr.cld_enq_flags;
199         int                     rc;
200         ENTRY;
201
202         rc = cl_lock_init(env, lock, io);
203         if (rc < 0)
204                 RETURN(rc);
205
206         if ((enq_flags & CEF_GLIMPSE) && !(enq_flags & CEF_SPECULATIVE)) {
207                 anchor = &cl_env_info(env)->clt_anchor;
208                 cl_sync_io_init(anchor, 1);
209         }
210
211         rc = cl_lock_enqueue(env, io, lock, anchor);
212
213         if (anchor != NULL) {
214                 int rc2;
215
216                 /* drop the reference count held at initialization time */
217                 cl_sync_io_note(env, anchor, 0);
218                 rc2 = cl_sync_io_wait(env, anchor, 0);
219                 if (rc2 < 0 && rc == 0)
220                         rc = rc2;
221         }
222
223         if (rc < 0)
224                 cl_lock_release(env, lock);
225         RETURN(rc);
226 }
227 EXPORT_SYMBOL(cl_lock_request);
228
229 /**
230  * Releases a hold and a reference on a lock, obtained by cl_lock_hold().
231  */
232 void cl_lock_release(const struct lu_env *env, struct cl_lock *lock)
233 {
234         ENTRY;
235
236         cl_lock_trace(D_DLMTRACE, env, "release lock", lock);
237         cl_lock_cancel(env, lock);
238         cl_lock_fini(env, lock);
239         EXIT;
240 }
241 EXPORT_SYMBOL(cl_lock_release);
242
243 const char *cl_lock_mode_name(const enum cl_lock_mode mode)
244 {
245         static const char * const names[] = {
246                 [CLM_READ]    = "R",
247                 [CLM_WRITE]   = "W",
248                 [CLM_GROUP]   = "G"
249         };
250         BUILD_BUG_ON(CLM_MAX != ARRAY_SIZE(names));
251         return names[mode];
252 }
253 EXPORT_SYMBOL(cl_lock_mode_name);
254
255 /**
256  * Prints human readable representation of a lock description.
257  */
258 void cl_lock_descr_print(const struct lu_env *env, void *cookie,
259                          lu_printer_t printer,
260                          const struct cl_lock_descr *descr)
261 {
262         const struct lu_fid  *fid;
263
264         fid = lu_object_fid(&descr->cld_obj->co_lu);
265         (*printer)(env, cookie, DDESCR"@"DFID, PDESCR(descr), PFID(fid));
266 }
267 EXPORT_SYMBOL(cl_lock_descr_print);
268
269 /**
270  * Prints human readable representation of \a lock to the \a f.
271  */
272 void cl_lock_print(const struct lu_env *env, void *cookie,
273                    lu_printer_t printer, const struct cl_lock *lock)
274 {
275         const struct cl_lock_slice *slice;
276
277         (*printer)(env, cookie, "lock@%p", lock);
278         cl_lock_descr_print(env, cookie, printer, &lock->cll_descr);
279         (*printer)(env, cookie, " {\n");
280
281         list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
282                 (*printer)(env, cookie, "    %s@%p: ",
283                            slice->cls_obj->co_lu.lo_dev->ld_type->ldt_name,
284                            slice);
285                 if (slice->cls_ops->clo_print != NULL)
286                         slice->cls_ops->clo_print(env, cookie, printer, slice);
287                 (*printer)(env, cookie, "\n");
288         }
289         (*printer)(env, cookie, "} lock@%p\n", lock);
290 }
291 EXPORT_SYMBOL(cl_lock_print);