4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include "../include/obd_class.h"
45 #include "../include/obd_support.h"
46 #include "../include/lustre_fid.h"
47 #include <linux/list.h>
48 #include "../include/cl_object.h"
49 #include "cl_internal.h"
51 static void cl_lock_trace0(int level, const struct lu_env *env,
52 const char *prefix, const struct cl_lock *lock,
53 const char *func, const int line)
55 struct cl_object_header *h = cl_object_header(lock->cll_descr.cld_obj);
57 CDEBUG(level, "%s: %p (%p/%d) at %s():%d\n",
58 prefix, lock, env, h->coh_nesting, func, line);
60 #define cl_lock_trace(level, env, prefix, lock) \
61 cl_lock_trace0(level, env, prefix, lock, __func__, __LINE__)
64 * Adds lock slice to the compound lock.
66 * This is called by cl_object_operations::coo_lock_init() methods to add a
67 * per-layer state to the lock. New state is added at the end of
68 * cl_lock::cll_layers list, that is, it is at the bottom of the stack.
70 * \see cl_req_slice_add(), cl_page_slice_add(), cl_io_slice_add()
72 void cl_lock_slice_add(struct cl_lock *lock, struct cl_lock_slice *slice,
73 struct cl_object *obj,
74 const struct cl_lock_operations *ops)
76 slice->cls_lock = lock;
77 list_add_tail(&slice->cls_linkage, &lock->cll_layers);
81 EXPORT_SYMBOL(cl_lock_slice_add);
83 void cl_lock_fini(const struct lu_env *env, struct cl_lock *lock)
85 cl_lock_trace(D_DLMTRACE, env, "destroy lock", lock);
87 while (!list_empty(&lock->cll_layers)) {
88 struct cl_lock_slice *slice;
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);
95 POISON(lock, 0x5a, sizeof(*lock));
97 EXPORT_SYMBOL(cl_lock_fini);
99 int cl_lock_init(const struct lu_env *env, struct cl_lock *lock,
100 const struct cl_io *io)
102 struct cl_object *obj = lock->cll_descr.cld_obj;
103 struct cl_object *scan;
106 /* Make sure cl_lock::cll_descr is initialized. */
109 INIT_LIST_HEAD(&lock->cll_layers);
110 list_for_each_entry(scan, &obj->co_lu.lo_header->loh_layers,
112 result = scan->co_ops->coo_lock_init(env, scan, lock, io);
114 cl_lock_fini(env, lock);
121 EXPORT_SYMBOL(cl_lock_init);
124 * Returns a slice with a lock, corresponding to the given layer in the
129 const struct cl_lock_slice *cl_lock_at(const struct cl_lock *lock,
130 const struct lu_device_type *dtype)
132 const struct cl_lock_slice *slice;
134 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
135 if (slice->cls_obj->co_lu.lo_dev->ld_type == dtype)
140 EXPORT_SYMBOL(cl_lock_at);
142 void cl_lock_cancel(const struct lu_env *env, struct cl_lock *lock)
144 const struct cl_lock_slice *slice;
146 cl_lock_trace(D_DLMTRACE, env, "cancel lock", lock);
147 list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
148 if (slice->cls_ops->clo_cancel)
149 slice->cls_ops->clo_cancel(env, slice);
152 EXPORT_SYMBOL(cl_lock_cancel);
156 * \param anchor: if we need to wait for resources before getting the lock,
157 * use @anchor for the purpose.
158 * \retval 0 enqueue successfully
159 * \retval <0 error code
161 int cl_lock_enqueue(const struct lu_env *env, struct cl_io *io,
162 struct cl_lock *lock, struct cl_sync_io *anchor)
164 const struct cl_lock_slice *slice;
167 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
168 if (!slice->cls_ops->clo_enqueue)
171 rc = slice->cls_ops->clo_enqueue(env, slice, io, anchor);
177 EXPORT_SYMBOL(cl_lock_enqueue);
180 * Main high-level entry point of cl_lock interface that finds existing or
181 * enqueues new lock matching given description.
183 int cl_lock_request(const struct lu_env *env, struct cl_io *io,
184 struct cl_lock *lock)
186 struct cl_sync_io *anchor = NULL;
187 __u32 enq_flags = lock->cll_descr.cld_enq_flags;
190 rc = cl_lock_init(env, lock, io);
194 if ((enq_flags & CEF_ASYNC) && !(enq_flags & CEF_AGL)) {
195 anchor = &cl_env_info(env)->clt_anchor;
196 cl_sync_io_init(anchor, 1, cl_sync_io_end);
199 rc = cl_lock_enqueue(env, io, lock, anchor);
204 /* drop the reference count held at initialization time */
205 cl_sync_io_note(env, anchor, 0);
206 rc2 = cl_sync_io_wait(env, anchor, 0);
207 if (rc2 < 0 && rc == 0)
212 cl_lock_release(env, lock);
216 EXPORT_SYMBOL(cl_lock_request);
219 * Releases a hold and a reference on a lock, obtained by cl_lock_hold().
221 void cl_lock_release(const struct lu_env *env, struct cl_lock *lock)
223 cl_lock_trace(D_DLMTRACE, env, "release lock", lock);
224 cl_lock_cancel(env, lock);
225 cl_lock_fini(env, lock);
227 EXPORT_SYMBOL(cl_lock_release);
229 const char *cl_lock_mode_name(const enum cl_lock_mode mode)
231 static const char *names[] = {
236 if (0 <= mode && mode < ARRAY_SIZE(names))
241 EXPORT_SYMBOL(cl_lock_mode_name);
244 * Prints human readable representation of a lock description.
246 void cl_lock_descr_print(const struct lu_env *env, void *cookie,
247 lu_printer_t printer,
248 const struct cl_lock_descr *descr)
250 const struct lu_fid *fid;
252 fid = lu_object_fid(&descr->cld_obj->co_lu);
253 (*printer)(env, cookie, DDESCR"@"DFID, PDESCR(descr), PFID(fid));
255 EXPORT_SYMBOL(cl_lock_descr_print);
258 * Prints human readable representation of \a lock to the \a f.
260 void cl_lock_print(const struct lu_env *env, void *cookie,
261 lu_printer_t printer, const struct cl_lock *lock)
263 const struct cl_lock_slice *slice;
265 (*printer)(env, cookie, "lock@%p", lock);
266 cl_lock_descr_print(env, cookie, printer, &lock->cll_descr);
267 (*printer)(env, cookie, " {\n");
269 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
270 (*printer)(env, cookie, " %s@%p: ",
271 slice->cls_obj->co_lu.lo_dev->ld_type->ldt_name,
273 if (slice->cls_ops->clo_print)
274 slice->cls_ops->clo_print(env, cookie, printer, slice);
275 (*printer)(env, cookie, "\n");
277 (*printer)(env, cookie, "} lock@%p\n", lock);
279 EXPORT_SYMBOL(cl_lock_print);