1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_lock for LOVSUB layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_LOV
43 #include "lov_cl_internal.h"
49 /*****************************************************************************
51 * Lovsub lock operations.
55 static void lovsub_lock_fini(const struct lu_env *env,
56 struct cl_lock_slice *slice)
58 struct lovsub_lock *lsl;
61 lsl = cl2lovsub_lock(slice);
62 LASSERT(cfs_list_empty(&lsl->lss_parents));
63 OBD_SLAB_FREE_PTR(lsl, lovsub_lock_kmem);
67 static void lovsub_parent_lock(const struct lu_env *env, struct lov_lock *lov)
69 struct cl_lock *parent;
72 parent = lov->lls_cl.cls_lock;
74 lu_ref_add(&parent->cll_reference, "lovsub-parent", cfs_current());
75 cl_lock_mutex_get(env, parent);
79 static void lovsub_parent_unlock(const struct lu_env *env, struct lov_lock *lov)
81 struct cl_lock *parent;
84 parent = lov->lls_cl.cls_lock;
85 cl_lock_mutex_put(env, lov->lls_cl.cls_lock);
86 lu_ref_del(&parent->cll_reference, "lovsub-parent", cfs_current());
87 cl_lock_put(env, parent);
92 * Implements cl_lock_operations::clo_state() method for lovsub layer, which
93 * method is called whenever sub-lock state changes. Propagates state change
96 static void lovsub_lock_state(const struct lu_env *env,
97 const struct cl_lock_slice *slice,
98 enum cl_lock_state state)
100 struct lovsub_lock *sub = cl2lovsub_lock(slice);
101 struct lov_lock_link *scan;
103 LASSERT(cl_lock_is_mutexed(slice->cls_lock));
106 cfs_list_for_each_entry(scan, &sub->lss_parents, lll_list) {
107 struct lov_lock *lov = scan->lll_super;
108 struct cl_lock *parent = lov->lls_cl.cls_lock;
110 if (sub->lss_active != parent) {
111 lovsub_parent_lock(env, lov);
112 cl_lock_signal(env, parent);
113 lovsub_parent_unlock(env, lov);
120 * Implementation of cl_lock_operation::clo_weigh() estimating lock weight by
121 * asking parent lock.
123 static unsigned long lovsub_lock_weigh(const struct lu_env *env,
124 const struct cl_lock_slice *slice)
126 struct lovsub_lock *lock = cl2lovsub_lock(slice);
127 struct lov_lock *lov;
128 unsigned long dumbbell;
132 LASSERT(cl_lock_is_mutexed(slice->cls_lock));
134 if (!cfs_list_empty(&lock->lss_parents)) {
136 * It is not clear whether all parents have to be asked and
137 * their estimations summed, or it is enough to ask one. For
138 * the current usages, one is always enough.
140 lov = container_of(lock->lss_parents.next,
141 struct lov_lock_link, lll_list)->lll_super;
143 lovsub_parent_lock(env, lov);
144 dumbbell = cl_lock_weigh(env, lov->lls_cl.cls_lock);
145 lovsub_parent_unlock(env, lov);
153 * Maps start/end offsets within a stripe, to offsets within a file.
155 static void lovsub_lock_descr_map(const struct cl_lock_descr *in,
156 struct lov_object *obj,
157 int stripe, struct cl_lock_descr *out)
159 struct lov_stripe_md *lsm = lov_r0(obj)->lo_lsm;
160 pgoff_t size; /* stripe size in pages */
161 pgoff_t skip; /* how many pages in every stripe are occupied by
167 start = in->cld_start;
170 if (lsm->lsm_stripe_count > 1) {
171 size = cl_index(lov2cl(obj), lsm->lsm_stripe_size);
172 skip = (lsm->lsm_stripe_count - 1) * size;
174 /* XXX overflow check here? */
175 start += start/size * skip + stripe * size;
177 if (end != CL_PAGE_EOF) {
178 end += end/size * skip + stripe * size;
180 * And check for overflow...
182 if (end < in->cld_end)
186 out->cld_start = start;
192 * Adjusts parent lock extent when a sub-lock is attached to a parent. This is
193 * called in two ways:
195 * - as part of receive call-back, when server returns granted extent to
198 * - when top-lock finds existing sub-lock in the cache.
200 * Note, that lock mode is not propagated to the parent: i.e., if CLM_READ
201 * top-lock matches CLM_WRITE sub-lock, top-lock is still CLM_READ.
203 int lov_sublock_modify(const struct lu_env *env, struct lov_lock *lov,
204 struct lovsub_lock *sublock,
205 const struct cl_lock_descr *d, int idx)
207 struct cl_lock *parent;
208 struct cl_lock *child;
209 struct lovsub_object *subobj;
210 struct cl_lock_descr *pd;
211 struct cl_lock_descr *parent_descr;
214 parent = lov->lls_cl.cls_lock;
215 parent_descr = &parent->cll_descr;
216 LASSERT(cl_lock_mode_match(d->cld_mode, parent_descr->cld_mode));
218 child = sublock->lss_cl.cls_lock;
219 subobj = cl2lovsub(sublock->lss_cl.cls_obj);
220 pd = &lov_env_info(env)->lti_ldescr;
222 pd->cld_obj = parent_descr->cld_obj;
223 pd->cld_mode = parent_descr->cld_mode;
224 pd->cld_gid = parent_descr->cld_gid;
225 lovsub_lock_descr_map(d, subobj->lso_super, subobj->lso_index, pd);
226 lov->lls_sub[idx].sub_got = *d;
228 * Notify top-lock about modification, if lock description changes
231 if (!cl_lock_ext_match(parent_descr, pd))
232 result = cl_lock_modify(env, parent, pd);
238 static int lovsub_lock_modify(const struct lu_env *env,
239 const struct cl_lock_slice *s,
240 const struct cl_lock_descr *d)
242 struct lovsub_lock *lock = cl2lovsub_lock(s);
243 struct lov_lock_link *scan;
244 struct lov_lock *lov;
249 LASSERT(cl_lock_mode_match(d->cld_mode,
250 s->cls_lock->cll_descr.cld_mode));
251 cfs_list_for_each_entry(scan, &lock->lss_parents, lll_list) {
254 lov = scan->lll_super;
255 lovsub_parent_lock(env, lov);
256 rc = lov_sublock_modify(env, lov, lock, d, scan->lll_idx);
257 lovsub_parent_unlock(env, lov);
258 result = result ?: rc;
263 static int lovsub_lock_closure(const struct lu_env *env,
264 const struct cl_lock_slice *slice,
265 struct cl_lock_closure *closure)
267 struct lovsub_lock *sub;
268 struct cl_lock *parent;
269 struct lov_lock_link *scan;
272 LASSERT(cl_lock_is_mutexed(slice->cls_lock));
275 sub = cl2lovsub_lock(slice);
278 cfs_list_for_each_entry(scan, &sub->lss_parents, lll_list) {
279 parent = scan->lll_super->lls_cl.cls_lock;
280 result = cl_lock_closure_build(env, parent, closure);
288 * A helper function for lovsub_lock_delete() that deals with a given parent
291 static int lovsub_lock_delete_one(const struct lu_env *env,
292 struct cl_lock *child, struct lov_lock *lov)
294 struct cl_lock *parent;
298 parent = lov->lls_cl.cls_lock;
299 if (parent->cll_error)
303 switch (parent->cll_state) {
308 cl_lock_signal(env, parent);
312 * Here lies a problem: a sub-lock is canceled while top-lock
313 * is being unlocked. Top-lock cannot be moved into CLS_NEW
314 * state, because unlocking has to succeed eventually by
315 * placing lock into CLS_CACHED (or failing it), see
316 * cl_unuse_try(). Nor can top-lock be left in CLS_CACHED
317 * state, because lov maintains an invariant that all
318 * sub-locks exist in CLS_CACHED (this allows cached top-lock
319 * to be reused immediately). Nor can we wait for top-lock
320 * state to change, because this can be synchronous to the
323 * We know for sure that lov_lock_unuse() will be called at
324 * least one more time to finish un-using, so leave a mark on
325 * the top-lock, that will be seen by the next call to
328 if (cl_lock_is_intransit(parent))
329 lov->lls_cancel_race = 1;
333 * if a sub-lock is canceled move its top-lock into CLS_NEW
334 * state to preserve an invariant that a top-lock in
335 * CLS_CACHED is immediately ready for re-use (i.e., has all
336 * sub-locks), and so that next attempt to re-use the top-lock
337 * enqueues missing sub-lock.
339 cl_lock_state_set(env, parent, CLS_NEW);
341 * if last sub-lock is canceled, destroy the top-lock (which
342 * is now `empty') proactively.
344 if (lov->lls_nr_filled == 0) {
345 /* ... but unfortunately, this cannot be done easily,
346 * as cancellation of a top-lock might acquire mutices
347 * of its other sub-locks, violating lock ordering,
348 * see cl_lock_{cancel,delete}() preconditions.
350 * To work around this, the mutex of this sub-lock is
351 * released, top-lock is destroyed, and sub-lock mutex
352 * acquired again. The list of parents has to be
353 * re-scanned from the beginning after this.
355 * Only do this if no mutices other than on @child and
356 * @parent are held by the current thread.
358 * TODO: The lock modal here is too complex, because
359 * the lock may be canceled and deleted by voluntarily:
361 * -> osc_lock_enqueue_wait
362 * -> osc_lock_cancel_wait
364 * -> lovsub_lock_delete
365 * -> cl_lock_cancel/delete
368 * The better choice is to spawn a kernel thread for
371 if (cl_lock_nr_mutexed(env) == 2) {
372 cl_lock_mutex_put(env, child);
373 cl_lock_cancel(env, parent);
374 cl_lock_delete(env, parent);
380 CL_LOCK_DEBUG(D_ERROR, env, parent, "Delete CLS_HELD lock\n");
382 CERROR("Impossible state: %i\n", parent->cll_state);
391 * An implementation of cl_lock_operations::clo_delete() method. This is
392 * invoked in "bottom-to-top" delete, when lock destruction starts from the
393 * sub-lock (e.g, as a result of ldlm lock LRU policy).
395 static void lovsub_lock_delete(const struct lu_env *env,
396 const struct cl_lock_slice *slice)
398 struct cl_lock *child = slice->cls_lock;
399 struct lovsub_lock *sub = cl2lovsub_lock(slice);
402 LASSERT(cl_lock_is_mutexed(child));
406 * Destruction of a sub-lock might take multiple iterations, because
407 * when the last sub-lock of a given top-lock is deleted, top-lock is
408 * canceled proactively, and this requires to release sub-lock
409 * mutex. Once sub-lock mutex has been released, list of its parents
410 * has to be re-scanned from the beginning.
413 struct lov_lock *lov;
414 struct lov_lock_link *scan;
415 struct lov_lock_link *temp;
416 struct lov_lock_sub *subdata;
419 cfs_list_for_each_entry_safe(scan, temp,
420 &sub->lss_parents, lll_list) {
421 lov = scan->lll_super;
422 subdata = &lov->lls_sub[scan->lll_idx];
423 lovsub_parent_lock(env, lov);
424 subdata->sub_got = subdata->sub_descr;
425 lov_lock_unlink(env, scan, sub);
426 restart = lovsub_lock_delete_one(env, child, lov);
427 lovsub_parent_unlock(env, lov);
430 cl_lock_mutex_get(env, child);
438 static int lovsub_lock_print(const struct lu_env *env, void *cookie,
439 lu_printer_t p, const struct cl_lock_slice *slice)
441 struct lovsub_lock *sub = cl2lovsub_lock(slice);
442 struct lov_lock *lov;
443 struct lov_lock_link *scan;
445 cfs_list_for_each_entry(scan, &sub->lss_parents, lll_list) {
446 lov = scan->lll_super;
447 (*p)(env, cookie, "[%d %p ", scan->lll_idx, lov);
449 cl_lock_descr_print(env, cookie, p,
450 &lov->lls_cl.cls_lock->cll_descr);
451 (*p)(env, cookie, "] ");
456 static const struct cl_lock_operations lovsub_lock_ops = {
457 .clo_fini = lovsub_lock_fini,
458 .clo_state = lovsub_lock_state,
459 .clo_delete = lovsub_lock_delete,
460 .clo_modify = lovsub_lock_modify,
461 .clo_closure = lovsub_lock_closure,
462 .clo_weigh = lovsub_lock_weigh,
463 .clo_print = lovsub_lock_print
466 int lovsub_lock_init(const struct lu_env *env, struct cl_object *obj,
467 struct cl_lock *lock, const struct cl_io *io)
469 struct lovsub_lock *lsk;
473 OBD_SLAB_ALLOC_PTR_GFP(lsk, lovsub_lock_kmem, CFS_ALLOC_IO);
475 CFS_INIT_LIST_HEAD(&lsk->lss_parents);
476 cl_lock_slice_add(lock, &lsk->lss_cl, obj, &lovsub_lock_ops);