Whamcloud - gitweb
b=20824
[fs/lustre-release.git] / lustre / lov / lov_lock.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_lock for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42
43 #include "lov_cl_internal.h"
44
45 /** \addtogroup lov
46  *  @{
47  */
48
49 static struct cl_lock_closure *lov_closure_get(const struct lu_env *env,
50                                                struct cl_lock *parent);
51
52 static int lov_lock_unuse(const struct lu_env *env,
53                           const struct cl_lock_slice *slice);
54 /*****************************************************************************
55  *
56  * Lov lock operations.
57  *
58  */
59
60 static struct lov_sublock_env *lov_sublock_env_get(const struct lu_env *env,
61                                                    struct cl_lock *parent,
62                                                    struct lov_lock_sub *lls)
63 {
64         struct lov_sublock_env *subenv;
65         struct lov_io          *lio    = lov_env_io(env);
66         struct cl_io           *io     = lio->lis_cl.cis_io;
67         struct lov_io_sub      *sub;
68
69         subenv = &lov_env_session(env)->ls_subenv;
70
71         /*
72          * FIXME: We tend to use the subio's env & io to call the sublock
73          * lock operations because osc lock sometimes stores some control
74          * variables in thread's IO infomation(Now only lockless information).
75          * However, if the lock's host(object) is different from the object
76          * for current IO, we have no way to get the subenv and subio because
77          * they are not initialized at all. As a temp fix, in this case,
78          * we still borrow the parent's env to call sublock operations.
79          */
80         if (!cl_object_same(io->ci_obj, parent->cll_descr.cld_obj)) {
81                 subenv->lse_env = env;
82                 subenv->lse_io  = io;
83                 subenv->lse_sub = NULL;
84         } else {
85                 sub = lov_sub_get(env, lio, lls->sub_stripe);
86                 if (!IS_ERR(sub)) {
87                         subenv->lse_env = sub->sub_env;
88                         subenv->lse_io  = sub->sub_io;
89                         subenv->lse_sub = sub;
90                 } else {
91                         subenv = (void*)sub;
92                 }
93         }
94         return subenv;
95 }
96
97 static void lov_sublock_env_put(struct lov_sublock_env *subenv)
98 {
99         if (subenv && subenv->lse_sub)
100                 lov_sub_put(subenv->lse_sub);
101 }
102
103 static void lov_sublock_adopt(const struct lu_env *env, struct lov_lock *lck,
104                               struct cl_lock *sublock, int idx,
105                               struct lov_lock_link *link)
106 {
107         struct lovsub_lock *lsl;
108         struct cl_lock     *parent = lck->lls_cl.cls_lock;
109         int                 rc;
110
111         LASSERT(cl_lock_is_mutexed(parent));
112         LASSERT(cl_lock_is_mutexed(sublock));
113         ENTRY;
114
115         lsl = cl2sub_lock(sublock);
116         /*
117          * check that sub-lock doesn't have lock link to this top-lock.
118          */
119         LASSERT(lov_lock_link_find(env, lck, lsl) == NULL);
120         LASSERT(idx < lck->lls_nr);
121
122         lck->lls_sub[idx].sub_lock = lsl;
123         lck->lls_nr_filled++;
124         LASSERT(lck->lls_nr_filled <= lck->lls_nr);
125         list_add_tail(&link->lll_list, &lsl->lss_parents);
126         link->lll_idx = idx;
127         link->lll_super = lck;
128         cl_lock_get(parent);
129         lu_ref_add(&parent->cll_reference, "lov-child", sublock);
130         lck->lls_sub[idx].sub_flags |= LSF_HELD;
131         cl_lock_user_add(env, sublock);
132
133         rc = lov_sublock_modify(env, lck, lsl, &sublock->cll_descr, idx);
134         LASSERT(rc == 0); /* there is no way this can fail, currently */
135         EXIT;
136 }
137
138 static struct cl_lock *lov_sublock_alloc(const struct lu_env *env,
139                                          const struct cl_io *io,
140                                          struct lov_lock *lck,
141                                          int idx, struct lov_lock_link **out)
142 {
143         struct cl_lock       *sublock;
144         struct cl_lock       *parent;
145         struct lov_lock_link *link;
146
147         LASSERT(idx < lck->lls_nr);
148         ENTRY;
149
150         OBD_SLAB_ALLOC_PTR_GFP(link, lov_lock_link_kmem, CFS_ALLOC_IO);
151         if (link != NULL) {
152                 struct lov_sublock_env *subenv;
153                 struct lov_lock_sub  *lls;
154                 struct cl_lock_descr *descr;
155
156                 parent = lck->lls_cl.cls_lock;
157                 lls    = &lck->lls_sub[idx];
158                 descr  = &lls->sub_descr;
159
160                 subenv = lov_sublock_env_get(env, parent, lls);
161                 if (!IS_ERR(subenv)) {
162                         /* CAVEAT: Don't try to add a field in lov_lock_sub
163                          * to remember the subio. This is because lock is able
164                          * to be cached, but this is not true for IO. This
165                          * further means a sublock might be referenced in
166                          * different io context. -jay */
167
168                         sublock = cl_lock_hold(subenv->lse_env, subenv->lse_io,
169                                                descr, "lov-parent", parent);
170                         lov_sublock_env_put(subenv);
171                 } else {
172                         /* error occurs. */
173                         sublock = (void*)subenv;
174                 }
175
176                 if (!IS_ERR(sublock))
177                         *out = link;
178                 else
179                         OBD_SLAB_FREE_PTR(link, lov_lock_link_kmem);
180         } else
181                 sublock = ERR_PTR(-ENOMEM);
182         RETURN(sublock);
183 }
184
185 static void lov_sublock_unlock(const struct lu_env *env,
186                                struct lovsub_lock *lsl,
187                                struct cl_lock_closure *closure,
188                                struct lov_sublock_env *subenv)
189 {
190         ENTRY;
191         lov_sublock_env_put(subenv);
192         lsl->lss_active = NULL;
193         cl_lock_disclosure(env, closure);
194         EXIT;
195 }
196
197 static int lov_sublock_lock(const struct lu_env *env,
198                             struct lov_lock *lck,
199                             struct lov_lock_sub *lls,
200                             struct cl_lock_closure *closure,
201                             struct lov_sublock_env **lsep)
202 {
203         struct lovsub_lock *sublock;
204         struct cl_lock     *child;
205         int                 result = 0;
206         ENTRY;
207
208         LASSERT(list_empty(&closure->clc_list));
209
210         sublock = lls->sub_lock;
211         child = sublock->lss_cl.cls_lock;
212         result = cl_lock_closure_build(env, child, closure);
213         if (result == 0) {
214                 struct cl_lock *parent = closure->clc_origin;
215
216                 LASSERT(cl_lock_is_mutexed(child));
217                 sublock->lss_active = parent;
218
219                 if (unlikely(child->cll_state == CLS_FREEING)) {
220                         struct lov_lock_link *link;
221                         /*
222                          * we could race with lock deletion which temporarily
223                          * put the lock in freeing state, bug 19080.
224                          */
225                         LASSERT(!(lls->sub_flags & LSF_HELD));
226
227                         link = lov_lock_link_find(env, lck, sublock);
228                         LASSERT(link != NULL);
229                         lov_lock_unlink(env, link, sublock);
230                         lov_sublock_unlock(env, sublock, closure, NULL);
231                         lck->lls_cancel_race = 1;
232                         result = CLO_REPEAT;
233                 } else if (lsep) {
234                         struct lov_sublock_env *subenv;
235                         subenv = lov_sublock_env_get(env, parent, lls);
236                         if (IS_ERR(subenv)) {
237                                 lov_sublock_unlock(env, sublock,
238                                                    closure, NULL);
239                                 result = PTR_ERR(subenv);
240                         } else {
241                                 *lsep = subenv;
242                         }
243                 }
244         }
245         RETURN(result);
246 }
247
248 /**
249  * Updates the result of a top-lock operation from a result of sub-lock
250  * sub-operations. Top-operations like lov_lock_{enqueue,use,unuse}() iterate
251  * over sub-locks and lov_subresult() is used to calculate return value of a
252  * top-operation. To this end, possible return values of sub-operations are
253  * ordered as
254  *
255  *     - 0                  success
256  *     - CLO_WAIT           wait for event
257  *     - CLO_REPEAT         repeat top-operation
258  *     - -ne                fundamental error
259  *
260  * Top-level return code can only go down through this list. CLO_REPEAT
261  * overwrites CLO_WAIT, because lock mutex was released and sleeping condition
262  * has to be rechecked by the upper layer.
263  */
264 static int lov_subresult(int result, int rc)
265 {
266         int result_rank;
267         int rc_rank;
268
269         LASSERT(result <= 0 || result == CLO_REPEAT || result == CLO_WAIT);
270         LASSERT(rc <= 0 || rc == CLO_REPEAT || rc == CLO_WAIT);
271         CLASSERT(CLO_WAIT < CLO_REPEAT);
272
273         ENTRY;
274
275         /* calculate ranks in the ordering above */
276         result_rank = result < 0 ? 1 + CLO_REPEAT : result;
277         rc_rank = rc < 0 ? 1 + CLO_REPEAT : rc;
278
279         if (result_rank < rc_rank)
280                 result = rc;
281         RETURN(result);
282 }
283
284 /**
285  * Creates sub-locks for a given lov_lock for the first time.
286  *
287  * Goes through all sub-objects of top-object, and creates sub-locks on every
288  * sub-object intersecting with top-lock extent. This is complicated by the
289  * fact that top-lock (that is being created) can be accessed concurrently
290  * through already created sub-locks (possibly shared with other top-locks).
291  */
292 static int lov_lock_sub_init(const struct lu_env *env,
293                              struct lov_lock *lck, const struct cl_io *io)
294 {
295         int result = 0;
296         int i;
297         int nr;
298         obd_off start;
299         obd_off end;
300         obd_off file_start;
301         obd_off file_end;
302
303         struct lov_object       *loo    = cl2lov(lck->lls_cl.cls_obj);
304         struct lov_layout_raid0 *r0     = lov_r0(loo);
305         struct cl_lock          *parent = lck->lls_cl.cls_lock;
306
307         ENTRY;
308
309         lck->lls_orig = parent->cll_descr;
310         file_start = cl_offset(lov2cl(loo), parent->cll_descr.cld_start);
311         file_end   = cl_offset(lov2cl(loo), parent->cll_descr.cld_end + 1) - 1;
312
313         for (i = 0, nr = 0; i < r0->lo_nr; i++) {
314                 /*
315                  * XXX for wide striping smarter algorithm is desirable,
316                  * breaking out of the loop, early.
317                  */
318                 if (lov_stripe_intersects(r0->lo_lsm, i,
319                                           file_start, file_end, &start, &end))
320                         nr++;
321         }
322         LASSERT(nr > 0);
323         OBD_ALLOC(lck->lls_sub, nr * sizeof lck->lls_sub[0]);
324         if (lck->lls_sub == NULL)
325                 RETURN(-ENOMEM);
326
327         lck->lls_nr = nr;
328         /*
329          * First, fill in sub-lock descriptions in
330          * lck->lls_sub[].sub_descr. They are used by lov_sublock_alloc()
331          * (called below in this function, and by lov_lock_enqueue()) to
332          * create sub-locks. At this moment, no other thread can access
333          * top-lock.
334          */
335         for (i = 0, nr = 0; i < r0->lo_nr; ++i) {
336                 if (lov_stripe_intersects(r0->lo_lsm, i,
337                                           file_start, file_end, &start, &end)) {
338                         struct cl_lock_descr *descr;
339
340                         descr = &lck->lls_sub[nr].sub_descr;
341
342                         LASSERT(descr->cld_obj == NULL);
343                         descr->cld_obj   = lovsub2cl(r0->lo_sub[i]);
344                         descr->cld_start = cl_index(descr->cld_obj, start);
345                         descr->cld_end   = cl_index(descr->cld_obj, end);
346                         descr->cld_mode  = parent->cll_descr.cld_mode;
347                         descr->cld_gid   = parent->cll_descr.cld_gid;
348                         /* XXX has no effect */
349                         lck->lls_sub[nr].sub_got = *descr;
350                         lck->lls_sub[nr].sub_stripe = i;
351                         nr++;
352                 }
353         }
354         LASSERT(nr == lck->lls_nr);
355         /*
356          * Then, create sub-locks. Once at least one sub-lock was created,
357          * top-lock can be reached by other threads.
358          */
359         for (i = 0; i < lck->lls_nr; ++i) {
360                 struct cl_lock       *sublock;
361                 struct lov_lock_link *link;
362
363                 if (lck->lls_sub[i].sub_lock == NULL) {
364                         sublock = lov_sublock_alloc(env, io, lck, i, &link);
365                         if (IS_ERR(sublock)) {
366                                 result = PTR_ERR(sublock);
367                                 break;
368                         }
369                         cl_lock_mutex_get(env, sublock);
370                         cl_lock_mutex_get(env, parent);
371                         /*
372                          * recheck under mutex that sub-lock wasn't created
373                          * concurrently, and that top-lock is still alive.
374                          */
375                         if (lck->lls_sub[i].sub_lock == NULL &&
376                             parent->cll_state < CLS_FREEING) {
377                                 lov_sublock_adopt(env, lck, sublock, i, link);
378                                 cl_lock_mutex_put(env, parent);
379                         } else {
380                                 OBD_SLAB_FREE_PTR(link, lov_lock_link_kmem);
381                                 cl_lock_mutex_put(env, parent);
382                                 cl_lock_unhold(env, sublock,
383                                                "lov-parent", parent);
384                         }
385                         cl_lock_mutex_put(env, sublock);
386                 }
387         }
388         /*
389          * Some sub-locks can be missing at this point. This is not a problem,
390          * because enqueue will create them anyway. Main duty of this function
391          * is to fill in sub-lock descriptions in a race free manner.
392          */
393         RETURN(result);
394 }
395
396 static int lov_sublock_release(const struct lu_env *env, struct lov_lock *lck,
397                                int i, int deluser, int rc)
398 {
399         struct cl_lock *parent = lck->lls_cl.cls_lock;
400
401         LASSERT(cl_lock_is_mutexed(parent));
402         ENTRY;
403
404         if (lck->lls_sub[i].sub_flags & LSF_HELD) {
405                 struct cl_lock    *sublock;
406                 int dying;
407
408                 LASSERT(lck->lls_sub[i].sub_lock != NULL);
409                 sublock = lck->lls_sub[i].sub_lock->lss_cl.cls_lock;
410                 LASSERT(cl_lock_is_mutexed(sublock));
411
412                 lck->lls_sub[i].sub_flags &= ~LSF_HELD;
413                 if (deluser)
414                         cl_lock_user_del(env, sublock);
415                 /*
416                  * If the last hold is released, and cancellation is pending
417                  * for a sub-lock, release parent mutex, to avoid keeping it
418                  * while sub-lock is being paged out.
419                  */
420                 dying = (sublock->cll_descr.cld_mode == CLM_PHANTOM ||
421                          sublock->cll_descr.cld_mode == CLM_GROUP ||
422                          (sublock->cll_flags & (CLF_CANCELPEND|CLF_DOOMED))) &&
423                         sublock->cll_holds == 1;
424                 if (dying)
425                         cl_lock_mutex_put(env, parent);
426                 cl_lock_unhold(env, sublock, "lov-parent", parent);
427                 if (dying) {
428                         cl_lock_mutex_get(env, parent);
429                         rc = lov_subresult(rc, CLO_REPEAT);
430                 }
431                 /*
432                  * From now on lck->lls_sub[i].sub_lock is a "weak" pointer,
433                  * not backed by a reference on a
434                  * sub-lock. lovsub_lock_delete() will clear
435                  * lck->lls_sub[i].sub_lock under semaphores, just before
436                  * sub-lock is destroyed.
437                  */
438         }
439         RETURN(rc);
440 }
441
442 static void lov_sublock_hold(const struct lu_env *env, struct lov_lock *lck,
443                              int i)
444 {
445         struct cl_lock *parent = lck->lls_cl.cls_lock;
446
447         LASSERT(cl_lock_is_mutexed(parent));
448         ENTRY;
449
450         if (!(lck->lls_sub[i].sub_flags & LSF_HELD)) {
451                 struct cl_lock *sublock;
452
453                 LASSERT(lck->lls_sub[i].sub_lock != NULL);
454                 sublock = lck->lls_sub[i].sub_lock->lss_cl.cls_lock;
455                 LASSERT(cl_lock_is_mutexed(sublock));
456                 LASSERT(sublock->cll_state != CLS_FREEING);
457
458                 lck->lls_sub[i].sub_flags |= LSF_HELD;
459
460                 cl_lock_get_trust(sublock);
461                 cl_lock_hold_add(env, sublock, "lov-parent", parent);
462                 cl_lock_user_add(env, sublock);
463                 cl_lock_put(env, sublock);
464         }
465         EXIT;
466 }
467
468 static void lov_lock_fini(const struct lu_env *env,
469                           struct cl_lock_slice *slice)
470 {
471         struct lov_lock *lck;
472         int i;
473
474         ENTRY;
475         lck = cl2lov_lock(slice);
476         LASSERT(lck->lls_nr_filled == 0);
477         if (lck->lls_sub != NULL) {
478                 for (i = 0; i < lck->lls_nr; ++i)
479                         /*
480                          * No sub-locks exists at this point, as sub-lock has
481                          * a reference on its parent.
482                          */
483                         LASSERT(lck->lls_sub[i].sub_lock == NULL);
484                 OBD_FREE(lck->lls_sub, lck->lls_nr * sizeof lck->lls_sub[0]);
485         }
486         OBD_SLAB_FREE_PTR(lck, lov_lock_kmem);
487         EXIT;
488 }
489
490 /**
491  * Tries to advance a state machine of a given sub-lock toward enqueuing of
492  * the top-lock.
493  *
494  * \retval 0 if state-transition can proceed
495  * \retval -ve otherwise.
496  */
497 static int lov_lock_enqueue_one(const struct lu_env *env, struct lov_lock *lck,
498                                 struct cl_lock *sublock,
499                                 struct cl_io *io, __u32 enqflags, int last)
500 {
501         int result;
502         ENTRY;
503
504         /* first, try to enqueue a sub-lock ... */
505         result = cl_enqueue_try(env, sublock, io, enqflags);
506         if (sublock->cll_state == CLS_ENQUEUED)
507                 /* if it is enqueued, try to `wait' on it---maybe it's already
508                  * granted */
509                 result = cl_wait_try(env, sublock);
510         /*
511          * If CEF_ASYNC flag is set, then all sub-locks can be enqueued in
512          * parallel, otherwise---enqueue has to wait until sub-lock is granted
513          * before proceeding to the next one.
514          */
515         if (result == CLO_WAIT && sublock->cll_state <= CLS_HELD &&
516             enqflags & CEF_ASYNC && !last)
517                 result = 0;
518         RETURN(result);
519 }
520
521 /**
522  * Helper function for lov_lock_enqueue() that creates missing sub-lock.
523  */
524 static int lov_sublock_fill(const struct lu_env *env, struct cl_lock *parent,
525                             struct cl_io *io, struct lov_lock *lck, int idx)
526 {
527         struct lov_lock_link *link;
528         struct cl_lock       *sublock;
529         int                   result;
530
531         LASSERT(parent->cll_depth == 1);
532         cl_lock_mutex_put(env, parent);
533         sublock = lov_sublock_alloc(env, io, lck, idx, &link);
534         if (!IS_ERR(sublock))
535                 cl_lock_mutex_get(env, sublock);
536         cl_lock_mutex_get(env, parent);
537
538         if (!IS_ERR(sublock)) {
539                 if (parent->cll_state == CLS_QUEUING &&
540                     lck->lls_sub[idx].sub_lock == NULL)
541                         lov_sublock_adopt(env, lck, sublock, idx, link);
542                 else {
543                         OBD_SLAB_FREE_PTR(link, lov_lock_link_kmem);
544                         /* other thread allocated sub-lock, or enqueue is no
545                          * longer going on */
546                         cl_lock_mutex_put(env, parent);
547                         cl_lock_unhold(env, sublock, "lov-parent", parent);
548                         cl_lock_mutex_get(env, parent);
549                 }
550                 cl_lock_mutex_put(env, sublock);
551                 result = CLO_REPEAT;
552         } else
553                 result = PTR_ERR(sublock);
554         return result;
555 }
556
557 /**
558  * Implementation of cl_lock_operations::clo_enqueue() for lov layer. This
559  * function is rather subtle, as it enqueues top-lock (i.e., advances top-lock
560  * state machine from CLS_QUEUING to CLS_ENQUEUED states) by juggling sub-lock
561  * state machines in the face of sub-locks sharing (by multiple top-locks),
562  * and concurrent sub-lock cancellations.
563  */
564 static int lov_lock_enqueue(const struct lu_env *env,
565                             const struct cl_lock_slice *slice,
566                             struct cl_io *io, __u32 enqflags)
567 {
568         struct cl_lock         *lock    = slice->cls_lock;
569         struct lov_lock        *lck     = cl2lov_lock(slice);
570         struct cl_lock_closure *closure = lov_closure_get(env, lock);
571         int i;
572         int result;
573         enum cl_lock_state minstate;
574
575         ENTRY;
576
577         for (result = 0, minstate = CLS_FREEING, i = 0; i < lck->lls_nr; ++i) {
578                 int rc;
579                 struct lovsub_lock     *sub;
580                 struct lov_lock_sub    *lls;
581                 struct cl_lock         *sublock;
582                 struct lov_sublock_env *subenv;
583
584                 if (lock->cll_state != CLS_QUEUING) {
585                         /*
586                          * Lock might have left QUEUING state if previous
587                          * iteration released its mutex. Stop enqueing in this
588                          * case and let the upper layer to decide what to do.
589                          */
590                         LASSERT(i > 0 && result != 0);
591                         break;
592                 }
593
594                 lls = &lck->lls_sub[i];
595                 sub = lls->sub_lock;
596                 /*
597                  * Sub-lock might have been canceled, while top-lock was
598                  * cached.
599                  */
600                 if (sub == NULL) {
601                         result = lov_sublock_fill(env, lock, io, lck, i);
602                         /* lov_sublock_fill() released @lock mutex,
603                          * restart. */
604                         break;
605                 }
606                 sublock = sub->lss_cl.cls_lock;
607                 rc = lov_sublock_lock(env, lck, lls, closure, &subenv);
608                 if (rc == 0) {
609                         lov_sublock_hold(env, lck, i);
610                         rc = lov_lock_enqueue_one(subenv->lse_env, lck, sublock,
611                                                   subenv->lse_io, enqflags,
612                                                   i == lck->lls_nr - 1);
613                         minstate = min(minstate, sublock->cll_state);
614                         /*
615                          * Don't hold a sub-lock in CLS_CACHED state, see
616                          * description for lov_lock::lls_sub.
617                          */
618                         if (sublock->cll_state > CLS_HELD)
619                                 rc = lov_sublock_release(env, lck, i, 1, rc);
620                         lov_sublock_unlock(env, sub, closure, subenv);
621                 }
622                 result = lov_subresult(result, rc);
623                 if (result != 0)
624                         break;
625         }
626         cl_lock_closure_fini(closure);
627         RETURN(result ?: minstate >= CLS_ENQUEUED ? 0 : CLO_WAIT);
628 }
629
630 static int lov_lock_unuse(const struct lu_env *env,
631                           const struct cl_lock_slice *slice)
632 {
633         struct lov_lock        *lck     = cl2lov_lock(slice);
634         struct cl_lock_closure *closure = lov_closure_get(env, slice->cls_lock);
635         int i;
636         int result;
637
638         ENTRY;
639
640         for (result = 0, i = 0; i < lck->lls_nr; ++i) {
641                 int rc;
642                 struct lovsub_lock     *sub;
643                 struct cl_lock         *sublock;
644                 struct lov_lock_sub    *lls;
645                 struct lov_sublock_env *subenv;
646
647                 /* top-lock state cannot change concurrently, because single
648                  * thread (one that released the last hold) carries unlocking
649                  * to the completion. */
650                 LASSERT(slice->cls_lock->cll_state == CLS_INTRANSIT);
651                 lls = &lck->lls_sub[i];
652                 sub = lls->sub_lock;
653                 if (sub == NULL)
654                         continue;
655
656                 sublock = sub->lss_cl.cls_lock;
657                 rc = lov_sublock_lock(env, lck, lls, closure, &subenv);
658                 if (rc == 0) {
659                         if (lls->sub_flags & LSF_HELD) {
660                                 LASSERT(sublock->cll_state == CLS_HELD);
661                                 rc = cl_unuse_try(subenv->lse_env, sublock);
662                                 if (rc != CLO_WAIT)
663                                         rc = lov_sublock_release(env, lck,
664                                                                  i, 0, rc);
665                         }
666                         lov_sublock_unlock(env, sub, closure, subenv);
667                 }
668                 result = lov_subresult(result, rc);
669                 if (result < 0)
670                         break;
671         }
672
673         if (result == 0 && lck->lls_cancel_race) {
674                 lck->lls_cancel_race = 0;
675                 result = -ESTALE;
676         }
677         cl_lock_closure_fini(closure);
678         RETURN(result);
679 }
680
681 static int lov_lock_wait(const struct lu_env *env,
682                          const struct cl_lock_slice *slice)
683 {
684         struct lov_lock        *lck     = cl2lov_lock(slice);
685         struct cl_lock_closure *closure = lov_closure_get(env, slice->cls_lock);
686         enum cl_lock_state      minstate;
687         int                     result;
688         int                     i;
689
690         ENTRY;
691
692         for (result = 0, minstate = CLS_FREEING, i = 0; i < lck->lls_nr; ++i) {
693                 int rc;
694                 struct lovsub_lock     *sub;
695                 struct cl_lock         *sublock;
696                 struct lov_lock_sub    *lls;
697                 struct lov_sublock_env *subenv;
698
699                 lls = &lck->lls_sub[i];
700                 sub = lls->sub_lock;
701                 LASSERT(sub != NULL);
702                 sublock = sub->lss_cl.cls_lock;
703                 rc = lov_sublock_lock(env, lck, lls, closure, &subenv);
704                 if (rc == 0) {
705                         LASSERT(sublock->cll_state >= CLS_ENQUEUED);
706                         if (sublock->cll_state < CLS_HELD)
707                                 rc = cl_wait_try(env, sublock);
708
709                         minstate = min(minstate, sublock->cll_state);
710                         lov_sublock_unlock(env, sub, closure, subenv);
711                 }
712                 result = lov_subresult(result, rc);
713                 if (result != 0)
714                         break;
715         }
716         cl_lock_closure_fini(closure);
717         RETURN(result ?: minstate >= CLS_HELD ? 0 : CLO_WAIT);
718 }
719
720 static int lov_lock_use(const struct lu_env *env,
721                         const struct cl_lock_slice *slice)
722 {
723         struct lov_lock        *lck     = cl2lov_lock(slice);
724         struct cl_lock_closure *closure = lov_closure_get(env, slice->cls_lock);
725         int                     result;
726         int                     i;
727
728         LASSERT(slice->cls_lock->cll_state == CLS_INTRANSIT);
729         ENTRY;
730
731         for (result = 0, i = 0; i < lck->lls_nr; ++i) {
732                 int rc;
733                 struct lovsub_lock     *sub;
734                 struct cl_lock         *sublock;
735                 struct lov_lock_sub    *lls;
736                 struct lov_sublock_env *subenv;
737
738                 LASSERT(slice->cls_lock->cll_state == CLS_INTRANSIT);
739
740                 lls = &lck->lls_sub[i];
741                 sub = lls->sub_lock;
742                 if (sub == NULL) {
743                         /*
744                          * Sub-lock might have been canceled, while top-lock was
745                          * cached.
746                          */
747                         result = -ESTALE;
748                         break;
749                 }
750
751                 sublock = sub->lss_cl.cls_lock;
752                 rc = lov_sublock_lock(env, lck, lls, closure, &subenv);
753                 if (rc == 0) {
754                         LASSERT(sublock->cll_state != CLS_FREEING);
755                         lov_sublock_hold(env, lck, i);
756                         if (sublock->cll_state == CLS_CACHED) {
757                                 rc = cl_use_try(subenv->lse_env, sublock, 0);
758                                 if (rc != 0)
759                                         rc = lov_sublock_release(env, lck,
760                                                                  i, 1, rc);
761                         }
762                         lov_sublock_unlock(env, sub, closure, subenv);
763                 }
764                 result = lov_subresult(result, rc);
765                 if (result != 0)
766                         break;
767         }
768
769         if (lck->lls_cancel_race) {
770                 /*
771                  * If there is unlocking happened at the same time, then
772                  * sublock_lock state should be FREEING, and lov_sublock_lock
773                  * should return CLO_REPEAT. In this case, it should return
774                  * ESTALE, and up layer should reset the lock state to be NEW.
775                  */
776                 lck->lls_cancel_race = 0;
777                 LASSERT(result != 0);
778                 result = -ESTALE;
779         }
780         cl_lock_closure_fini(closure);
781         RETURN(result);
782 }
783
784 #if 0
785 static int lock_lock_multi_match()
786 {
787         struct cl_lock          *lock    = slice->cls_lock;
788         struct cl_lock_descr    *subneed = &lov_env_info(env)->lti_ldescr;
789         struct lov_object       *loo     = cl2lov(lov->lls_cl.cls_obj);
790         struct lov_layout_raid0 *r0      = lov_r0(loo);
791         struct lov_lock_sub     *sub;
792         struct cl_object        *subobj;
793         obd_off  fstart;
794         obd_off  fend;
795         obd_off  start;
796         obd_off  end;
797         int i;
798
799         fstart = cl_offset(need->cld_obj, need->cld_start);
800         fend   = cl_offset(need->cld_obj, need->cld_end + 1) - 1;
801         subneed->cld_mode = need->cld_mode;
802         cl_lock_mutex_get(env, lock);
803         for (i = 0; i < lov->lls_nr; ++i) {
804                 sub = &lov->lls_sub[i];
805                 if (sub->sub_lock == NULL)
806                         continue;
807                 subobj = sub->sub_descr.cld_obj;
808                 if (!lov_stripe_intersects(r0->lo_lsm, sub->sub_stripe,
809                                            fstart, fend, &start, &end))
810                         continue;
811                 subneed->cld_start = cl_index(subobj, start);
812                 subneed->cld_end   = cl_index(subobj, end);
813                 subneed->cld_obj   = subobj;
814                 if (!cl_lock_ext_match(&sub->sub_got, subneed)) {
815                         result = 0;
816                         break;
817                 }
818         }
819         cl_lock_mutex_put(env, lock);
820 }
821 #endif
822
823 /**
824  * Check if the extent region \a descr is covered by \a child against the
825  * specific \a stripe.
826  */
827 static int lov_lock_stripe_is_matching(const struct lu_env *env,
828                                        struct lov_object *lov, int stripe,
829                                        const struct cl_lock_descr *child,
830                                        const struct cl_lock_descr *descr)
831 {
832         struct lov_stripe_md *lsm = lov_r0(lov)->lo_lsm;
833         obd_off start;
834         obd_off end;
835         int result;
836
837         if (lov_r0(lov)->lo_nr == 1)
838                 return cl_lock_ext_match(child, descr);
839
840         /*
841          * For a multi-stripes object:
842          * - make sure the descr only covers child's stripe, and
843          * - check if extent is matching.
844          */
845         start = cl_offset(&lov->lo_cl, descr->cld_start);
846         end   = cl_offset(&lov->lo_cl, descr->cld_end + 1) - 1;
847         result = end - start <= lsm->lsm_stripe_size &&
848                  stripe == lov_stripe_number(lsm, start) &&
849                  stripe == lov_stripe_number(lsm, end);
850         if (result) {
851                 struct cl_lock_descr *subd = &lov_env_info(env)->lti_ldescr;
852                 obd_off sub_start;
853                 obd_off sub_end;
854
855                 subd->cld_obj  = NULL;   /* don't need sub object at all */
856                 subd->cld_mode = descr->cld_mode;
857                 subd->cld_gid  = descr->cld_gid;
858                 result = lov_stripe_intersects(lsm, stripe, start, end,
859                                                &sub_start, &sub_end);
860                 LASSERT(result);
861                 subd->cld_start = cl_index(child->cld_obj, sub_start);
862                 subd->cld_end   = cl_index(child->cld_obj, sub_end);
863                 result = cl_lock_ext_match(child, subd);
864         }
865         return result;
866 }
867
868 /**
869  * An implementation of cl_lock_operations::clo_fits_into() method.
870  *
871  * Checks whether a lock (given by \a slice) is suitable for \a
872  * io. Multi-stripe locks can be used only for "quick" io, like truncate, or
873  * O_APPEND write.
874  *
875  * \see ccc_lock_fits_into().
876  */
877 static int lov_lock_fits_into(const struct lu_env *env,
878                               const struct cl_lock_slice *slice,
879                               const struct cl_lock_descr *need,
880                               const struct cl_io *io)
881 {
882         struct lov_lock   *lov = cl2lov_lock(slice);
883         struct lov_object *obj = cl2lov(slice->cls_obj);
884         int result;
885
886         LASSERT(cl_object_same(need->cld_obj, slice->cls_obj));
887         LASSERT(lov->lls_nr > 0);
888
889         ENTRY;
890
891         if (need->cld_mode == CLM_GROUP)
892                 /*
893                  * always allow to match group lock.
894                  */
895                 result = cl_lock_ext_match(&lov->lls_orig, need);
896         else if (lov->lls_nr == 1) {
897                 struct cl_lock_descr *got = &lov->lls_sub[0].sub_got;
898                 result = lov_lock_stripe_is_matching(env,
899                                                      cl2lov(slice->cls_obj),
900                                                      lov->lls_sub[0].sub_stripe,
901                                                      got, need);
902         } else if (io->ci_type != CIT_TRUNC && io->ci_type != CIT_MISC &&
903                    !cl_io_is_append(io) && need->cld_mode != CLM_PHANTOM)
904                 /*
905                  * Multi-stripe locks are only suitable for `quick' IO and for
906                  * glimpse.
907                  */
908                 result = 0;
909         else
910                 /*
911                  * Most general case: multi-stripe existing lock, and
912                  * (potentially) multi-stripe @need lock. Check that @need is
913                  * covered by @lov's sub-locks.
914                  *
915                  * For now, ignore lock expansions made by the server, and
916                  * match against original lock extent.
917                  */
918                 result = cl_lock_ext_match(&lov->lls_orig, need);
919         CDEBUG(D_DLMTRACE, DDESCR"/"DDESCR" %i %i/%i: %i\n",
920                PDESCR(&lov->lls_orig), PDESCR(&lov->lls_sub[0].sub_got),
921                lov->lls_sub[0].sub_stripe, lov->lls_nr, lov_r0(obj)->lo_nr,
922                result);
923         RETURN(result);
924 }
925
926 void lov_lock_unlink(const struct lu_env *env,
927                      struct lov_lock_link *link, struct lovsub_lock *sub)
928 {
929         struct lov_lock *lck    = link->lll_super;
930         struct cl_lock  *parent = lck->lls_cl.cls_lock;
931
932         LASSERT(cl_lock_is_mutexed(parent));
933         LASSERT(cl_lock_is_mutexed(sub->lss_cl.cls_lock));
934         ENTRY;
935
936         list_del_init(&link->lll_list);
937         LASSERT(lck->lls_sub[link->lll_idx].sub_lock == sub);
938         /* yank this sub-lock from parent's array */
939         lck->lls_sub[link->lll_idx].sub_lock = NULL;
940         LASSERT(lck->lls_nr_filled > 0);
941         lck->lls_nr_filled--;
942         lu_ref_del(&parent->cll_reference, "lov-child", sub->lss_cl.cls_lock);
943         cl_lock_put(env, parent);
944         OBD_SLAB_FREE_PTR(link, lov_lock_link_kmem);
945         EXIT;
946 }
947
948 struct lov_lock_link *lov_lock_link_find(const struct lu_env *env,
949                                          struct lov_lock *lck,
950                                          struct lovsub_lock *sub)
951 {
952         struct lov_lock_link *scan;
953
954         LASSERT(cl_lock_is_mutexed(sub->lss_cl.cls_lock));
955         ENTRY;
956
957         list_for_each_entry(scan, &sub->lss_parents, lll_list) {
958                 if (scan->lll_super == lck)
959                         RETURN(scan);
960         }
961         RETURN(NULL);
962 }
963
964 /**
965  * An implementation of cl_lock_operations::clo_delete() method. This is
966  * invoked for "top-to-bottom" delete, when lock destruction starts from the
967  * top-lock, e.g., as a result of inode destruction.
968  *
969  * Unlinks top-lock from all its sub-locks. Sub-locks are not deleted there:
970  * this is done separately elsewhere:
971  *
972  *     - for inode destruction, lov_object_delete() calls cl_object_kill() for
973  *       each sub-object, purging its locks;
974  *
975  *     - in other cases (e.g., a fatal error with a top-lock) sub-locks are
976  *       left in the cache.
977  */
978 static void lov_lock_delete(const struct lu_env *env,
979                             const struct cl_lock_slice *slice)
980 {
981         struct lov_lock        *lck     = cl2lov_lock(slice);
982         struct cl_lock_closure *closure = lov_closure_get(env, slice->cls_lock);
983         int i;
984
985         LASSERT(slice->cls_lock->cll_state == CLS_FREEING);
986         ENTRY;
987
988         for (i = 0; i < lck->lls_nr; ++i) {
989                 struct lov_lock_sub *lls;
990                 struct lovsub_lock  *lsl;
991                 struct cl_lock      *sublock;
992                 int rc;
993
994                 lls = &lck->lls_sub[i];
995                 lsl = lls->sub_lock;
996                 if (lsl == NULL)
997                         continue;
998
999                 sublock = lsl->lss_cl.cls_lock;
1000                 rc = lov_sublock_lock(env, lck, lls, closure, NULL);
1001                 if (rc == 0) {
1002                         if (lls->sub_flags & LSF_HELD)
1003                                 lov_sublock_release(env, lck, i, 1, 0);
1004                         if (sublock->cll_state < CLS_FREEING) {
1005                                 struct lov_lock_link *link;
1006
1007                                 link = lov_lock_link_find(env, lck, lsl);
1008                                 LASSERT(link != NULL);
1009                                 lov_lock_unlink(env, link, lsl);
1010                                 LASSERT(lck->lls_sub[i].sub_lock == NULL);
1011                         }
1012                         lov_sublock_unlock(env, lsl, closure, NULL);
1013                 } else if (rc == CLO_REPEAT) {
1014                         --i; /* repeat with this lock */
1015                 } else {
1016                         CL_LOCK_DEBUG(D_ERROR, env, sublock,
1017                                       "Cannot get sub-lock for delete: %i\n",
1018                                       rc);
1019                 }
1020         }
1021         cl_lock_closure_fini(closure);
1022         EXIT;
1023 }
1024
1025 static int lov_lock_print(const struct lu_env *env, void *cookie,
1026                           lu_printer_t p, const struct cl_lock_slice *slice)
1027 {
1028         struct lov_lock *lck = cl2lov_lock(slice);
1029         int              i;
1030
1031         (*p)(env, cookie, "%d\n", lck->lls_nr);
1032         for (i = 0; i < lck->lls_nr; ++i) {
1033                 struct lov_lock_sub *sub;
1034
1035                 sub = &lck->lls_sub[i];
1036                 (*p)(env, cookie, "    %d %x: ", i, sub->sub_flags);
1037                 if (sub->sub_lock != NULL)
1038                         cl_lock_print(env, cookie, p,
1039                                       sub->sub_lock->lss_cl.cls_lock);
1040                 else
1041                         (*p)(env, cookie, "---\n");
1042         }
1043         return 0;
1044 }
1045
1046 static const struct cl_lock_operations lov_lock_ops = {
1047         .clo_fini      = lov_lock_fini,
1048         .clo_enqueue   = lov_lock_enqueue,
1049         .clo_wait      = lov_lock_wait,
1050         .clo_use       = lov_lock_use,
1051         .clo_unuse     = lov_lock_unuse,
1052         .clo_fits_into = lov_lock_fits_into,
1053         .clo_delete    = lov_lock_delete,
1054         .clo_print     = lov_lock_print
1055 };
1056
1057 int lov_lock_init_raid0(const struct lu_env *env, struct cl_object *obj,
1058                         struct cl_lock *lock, const struct cl_io *io)
1059 {
1060         struct lov_lock *lck;
1061         int result;
1062
1063         ENTRY;
1064         OBD_SLAB_ALLOC_PTR_GFP(lck, lov_lock_kmem, CFS_ALLOC_IO);
1065         if (lck != NULL) {
1066                 cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_lock_ops);
1067                 result = lov_lock_sub_init(env, lck, io);
1068         } else
1069                 result = -ENOMEM;
1070         RETURN(result);
1071 }
1072
1073 static struct cl_lock_closure *lov_closure_get(const struct lu_env *env,
1074                                                struct cl_lock *parent)
1075 {
1076         struct cl_lock_closure *closure;
1077
1078         closure = &lov_env_info(env)->lti_closure;
1079         LASSERT(list_empty(&closure->clc_list));
1080         cl_lock_closure_init(env, closure, parent, 1);
1081         return closure;
1082 }
1083
1084
1085 /** @} lov */