Whamcloud - gitweb
6b96896b07b88881fffcec3de310f1088f124ab2
[fs/lustre-release.git] / lustre / lov / lovsub_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 LOVSUB 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 lov @{ */
46
47 /*****************************************************************************
48  *
49  * Lovsub lock operations.
50  *
51  */
52
53 static void lovsub_lock_fini(const struct lu_env *env,
54                              struct cl_lock_slice *slice)
55 {
56         struct lovsub_lock   *lsl;
57
58         ENTRY;
59         lsl = cl2lovsub_lock(slice);
60         LASSERT(list_empty(&lsl->lss_parents));
61         OBD_SLAB_FREE_PTR(lsl, lovsub_lock_kmem);
62         EXIT;
63 }
64
65 static void lovsub_parent_lock(const struct lu_env *env, struct lov_lock *lov)
66 {
67         struct cl_lock *parent;
68
69         ENTRY;
70         parent = lov->lls_cl.cls_lock;
71         cl_lock_get(parent);
72         lu_ref_add(&parent->cll_reference, "lovsub-parent", cfs_current());
73         cl_lock_mutex_get(env, parent);
74         EXIT;
75 }
76
77 static void lovsub_parent_unlock(const struct lu_env *env, struct lov_lock *lov)
78 {
79         struct cl_lock *parent;
80
81         ENTRY;
82         parent = lov->lls_cl.cls_lock;
83         cl_lock_mutex_put(env, lov->lls_cl.cls_lock);
84         lu_ref_del(&parent->cll_reference, "lovsub-parent", cfs_current());
85         cl_lock_put(env, parent);
86         EXIT;
87 }
88
89 static void lovsub_lock_state_one(const struct lu_env *env,
90                                   const struct lovsub_lock *lovsub,
91                                   struct lov_lock *lov)
92 {
93         struct cl_lock       *parent;
94         const struct cl_lock *child;
95
96         ENTRY;
97         parent = lov->lls_cl.cls_lock;
98         child  = lovsub->lss_cl.cls_lock;
99
100         if (lovsub->lss_active != parent) {
101                 lovsub_parent_lock(env, lov);
102                 if (child->cll_error != 0)
103                         cl_lock_error(env, parent, child->cll_error);
104                 else
105                         cl_lock_signal(env, parent);
106                 lovsub_parent_unlock(env, lov);
107         }
108         EXIT;
109 }
110
111 /**
112  * Implements cl_lock_operations::clo_state() method for lovsub layer, which
113  * method is called whenever sub-lock state changes. Propagates state change
114  * to the top-locks.
115  */
116 static void lovsub_lock_state(const struct lu_env *env,
117                               const struct cl_lock_slice *slice,
118                               enum cl_lock_state state)
119 {
120         struct lovsub_lock   *sub = cl2lovsub_lock(slice);
121         struct lov_lock_link *scan;
122         struct lov_lock_link *temp;
123
124         LASSERT(cl_lock_is_mutexed(slice->cls_lock));
125         ENTRY;
126
127         /*
128          * Use _safe() version, because
129          *
130          *     lovsub_lock_state_one()
131          *       ->cl_lock_error()
132          *         ->cl_lock_delete()
133          *           ->lov_lock_delete()
134          *
135          * can unlink parent from the parent list.
136          */
137         list_for_each_entry_safe(scan, temp, &sub->lss_parents, lll_list)
138                 lovsub_lock_state_one(env, sub, scan->lll_super);
139         EXIT;
140 }
141
142 /**
143  * Implementation of cl_lock_operation::clo_weigh() estimating lock weight by
144  * asking parent lock.
145  */
146 static unsigned long lovsub_lock_weigh(const struct lu_env *env,
147                                        const struct cl_lock_slice *slice)
148 {
149         struct lovsub_lock *lock = cl2lovsub_lock(slice);
150         struct lov_lock    *lov;
151         unsigned long       dumbbell;
152
153         ENTRY;
154
155         LASSERT(cl_lock_is_mutexed(slice->cls_lock));
156
157         if (!list_empty(&lock->lss_parents)) {
158                 /*
159                  * It is not clear whether all parents have to be asked and
160                  * their estimations summed, or it is enough to ask one. For
161                  * the current usages, one is always enough.
162                  */
163                 lov = container_of(lock->lss_parents.next,
164                                    struct lov_lock_link, lll_list)->lll_super;
165
166                 lovsub_parent_lock(env, lov);
167                 dumbbell = cl_lock_weigh(env, lov->lls_cl.cls_lock);
168                 lovsub_parent_unlock(env, lov);
169         } else
170                 dumbbell = 0;
171
172         RETURN(dumbbell);
173 }
174
175 /**
176  * Maps start/end offsets within a stripe, to offsets within a file.
177  */
178 static void lovsub_lock_descr_map(const struct cl_lock_descr *in,
179                                   struct lov_object *obj,
180                                   int stripe, struct cl_lock_descr *out)
181 {
182         struct lov_stripe_md *lsm = lov_r0(obj)->lo_lsm;
183         pgoff_t size; /* stripe size in pages */
184         pgoff_t skip; /* how many pages in every stripe are occupied by
185                        * "other" stripes */
186         pgoff_t start;
187         pgoff_t end;
188
189         ENTRY;
190         start = in->cld_start;
191         end   = in->cld_end;
192
193         /*
194          * XXX join file support.
195          */
196         if (lsm->lsm_stripe_count > 1) {
197                 size = cl_index(lov2cl(obj), lsm->lsm_stripe_size);
198                 skip = (lsm->lsm_stripe_count - 1) * size;
199
200                 /* XXX overflow check here? */
201                 start += start/size * skip + stripe * size;
202
203                 if (end != CL_PAGE_EOF) {
204                         end += end/size * skip + stripe * size;
205                         /*
206                          * And check for overflow...
207                          */
208                         if (end < in->cld_end)
209                                 end = CL_PAGE_EOF;
210                 }
211         }
212         out->cld_start = start;
213         out->cld_end   = end;
214         EXIT;
215 }
216
217 /**
218  * Adjusts parent lock extent when a sub-lock is attached to a parent. This is
219  * called in two ways:
220  *
221  *     - as part of receive call-back, when server returns granted extent to
222  *       the client, and
223  *
224  *     - when top-lock finds existing sub-lock in the cache.
225  *
226  * Note, that lock mode is not propagated to the parent: i.e., if CLM_READ
227  * top-lock matches CLM_WRITE sub-lock, top-lock is still CLM_READ.
228  */
229 int lov_sublock_modify(const struct lu_env *env, struct lov_lock *lov,
230                        struct lovsub_lock *sublock,
231                        const struct cl_lock_descr *d, int idx)
232 {
233         struct cl_lock       *parent;
234         struct cl_lock       *child;
235         struct lovsub_object *subobj;
236         struct cl_lock_descr *pd;
237         struct cl_lock_descr *parent_descr;
238         int                   result;
239
240         parent       = lov->lls_cl.cls_lock;
241         parent_descr = &parent->cll_descr;
242         LASSERT(cl_lock_mode_match(d->cld_mode, parent_descr->cld_mode));
243
244         child  = sublock->lss_cl.cls_lock;
245         subobj = cl2lovsub(sublock->lss_cl.cls_obj);
246         pd     = &lov_env_info(env)->lti_ldescr;
247
248         pd->cld_obj  = parent_descr->cld_obj;
249         pd->cld_mode = parent_descr->cld_mode;
250         lovsub_lock_descr_map(d, subobj->lso_super, subobj->lso_index, pd);
251         lov->lls_sub[idx].sub_got = *d;
252         /*
253          * Notify top-lock about modification, if lock description changes
254          * materially.
255          */
256         if (!cl_lock_ext_match(parent_descr, pd))
257                 result = cl_lock_modify(env, parent, pd);
258         else
259                 result = 0;
260         return result;
261 }
262
263 static int lovsub_lock_modify(const struct lu_env *env,
264                               const struct cl_lock_slice *s,
265                               const struct cl_lock_descr *d)
266 {
267         struct lovsub_lock   *lock   = cl2lovsub_lock(s);
268         struct lov_lock_link *scan;
269         struct lov_lock      *lov;
270         int result                   = 0;
271
272         ENTRY;
273
274         LASSERT(cl_lock_mode_match(d->cld_mode,
275                                    s->cls_lock->cll_descr.cld_mode));
276         list_for_each_entry(scan, &lock->lss_parents, lll_list) {
277                 int rc;
278
279                 lov = scan->lll_super;
280                 lovsub_parent_lock(env, lov);
281                 rc = lov_sublock_modify(env, lov, lock, d, scan->lll_idx);
282                 lovsub_parent_unlock(env, lov);
283                 result = result ?: rc;
284         }
285         RETURN(result);
286 }
287
288 static int lovsub_lock_closure(const struct lu_env *env,
289                                const struct cl_lock_slice *slice,
290                                struct cl_lock_closure *closure)
291 {
292         struct lovsub_lock   *sub;
293         struct cl_lock       *parent;
294         struct lov_lock_link *scan;
295         int                   result;
296
297         LASSERT(cl_lock_is_mutexed(slice->cls_lock));
298         ENTRY;
299
300         sub    = cl2lovsub_lock(slice);
301         result = 0;
302
303         list_for_each_entry(scan, &sub->lss_parents, lll_list) {
304                 parent = scan->lll_super->lls_cl.cls_lock;
305                 result = cl_lock_closure_build(env, parent, closure);
306                 if (result != 0)
307                         break;
308         }
309         RETURN(result);
310 }
311
312 /**
313  * A helper function for lovsub_lock_delete() that deals with a given parent
314  * top-lock.
315  */
316 static int lovsub_lock_delete_one(const struct lu_env *env,
317                                   struct cl_lock *child, struct lov_lock *lov)
318 {
319         struct cl_lock       *parent;
320         int             result;
321         ENTRY;
322
323         parent  = lov->lls_cl.cls_lock;
324         result = 0;
325
326         switch (parent->cll_state) {
327         case CLS_NEW:
328         case CLS_QUEUING:
329         case CLS_ENQUEUED:
330         case CLS_FREEING:
331                 cl_lock_signal(env, parent);
332                 break;
333         case CLS_UNLOCKING:
334                 /*
335                  * Here lies a problem: a sub-lock is canceled while top-lock
336                  * is being unlocked. Top-lock cannot be moved into CLS_NEW
337                  * state, because unlocking has to succeed eventually by
338                  * placing lock into CLS_CACHED (or failing it), see
339                  * cl_unuse_try(). Nor can top-lock be left in CLS_CACHED
340                  * state, because lov maintains an invariant that all
341                  * sub-locks exist in CLS_CACHED (this allows cached top-lock
342                  * to be reused immediately). Nor can we wait for top-lock
343                  * state to change, because this can be synchronous to the
344                  * current thread.
345                          *
346                  * We know for sure that lov_lock_unuse() will be called at
347                  * least one more time to finish un-using, so leave a mark on
348                  * the top-lock, that will be seen by the next call to
349                  * lov_lock_unuse().
350                  */
351                 lov->lls_unuse_race = 1;
352                 break;
353         case CLS_CACHED:
354                 /*
355                  * if a sub-lock is canceled move its top-lock into CLS_NEW
356                  * state to preserve an invariant that a top-lock in
357                  * CLS_CACHED is immediately ready for re-use (i.e., has all
358                  * sub-locks), and so that next attempt to re-use the top-lock
359                  * enqueues missing sub-lock.
360                  */
361                 cl_lock_state_set(env, parent, CLS_NEW);
362                 /*
363                  * if last sub-lock is canceled, destroy the top-lock (which
364                  * is now `empty') proactively.
365                  */
366                 if (lov->lls_nr_filled == 0) {
367                         /* ... but unfortunately, this cannot be done easily,
368                          * as cancellation of a top-lock might acquire mutices
369                          * of its other sub-locks, violating lock ordering,
370                          * see cl_lock_{cancel,delete}() preconditions.
371                          *
372                          * To work around this, the mutex of this sub-lock is
373                          * released, top-lock is destroyed, and sub-lock mutex
374                          * acquired again. The list of parents has to be
375                          * re-scanned from the beginning after this.
376                          *
377                          * Only do this if no mutices other than on @child and
378                          * @parent are held by the current thread.
379                          *
380                          * TODO: The lock modal here is too complex, because
381                          * the lock may be canceled and deleted by voluntarily:
382                          *    cl_lock_request
383                          *      -> osc_lock_enqueue_wait
384                          *        -> osc_lock_cancel_wait
385                          *          -> cl_lock_delete
386                          *            -> lovsub_lock_delete
387                          *              -> cl_lock_cancel/delete
388                          *                -> ...
389                          *
390                          * The better choice is to spawn a kernel thread for
391                          * this purpose. -jay
392                          */
393                         if (cl_lock_nr_mutexed(env) == 2) {
394                                 cl_lock_mutex_put(env, child);
395                                 cl_lock_cancel(env, parent);
396                                 cl_lock_delete(env, parent);
397                                 result = 1;
398                         }
399                 }
400                 break;
401         case CLS_HELD:
402         default:
403                 CERROR("Impossible state: %i\n", parent->cll_state);
404                 LBUG();
405         }
406
407         RETURN(result);
408 }
409
410 /**
411  * An implementation of cl_lock_operations::clo_delete() method. This is
412  * invoked in "bottom-to-top" delete, when lock destruction starts from the
413  * sub-lock (e.g, as a result of ldlm lock LRU policy).
414  */
415 static void lovsub_lock_delete(const struct lu_env *env,
416                                const struct cl_lock_slice *slice)
417 {
418         struct cl_lock     *child = slice->cls_lock;
419         struct lovsub_lock *sub   = cl2lovsub_lock(slice);
420         int restart;
421
422         LASSERT(cl_lock_is_mutexed(child));
423
424         ENTRY;
425         /*
426          * Destruction of a sub-lock might take multiple iterations, because
427          * when the last sub-lock of a given top-lock is deleted, top-lock is
428          * canceled proactively, and this requires to release sub-lock
429          * mutex. Once sub-lock mutex has been released, list of its parents
430          * has to be re-scanned from the beginning.
431          */
432         do {
433                 struct lov_lock      *lov;
434                 struct lov_lock_link *scan;
435                 struct lov_lock_link *temp;
436                 struct lov_lock_sub  *subdata;
437
438                 restart = 0;
439                 list_for_each_entry_safe(scan, temp,
440                                          &sub->lss_parents, lll_list) {
441                         lov     = scan->lll_super;
442                         subdata = &lov->lls_sub[scan->lll_idx];
443                         lovsub_parent_lock(env, lov);
444                         subdata->sub_got = subdata->sub_descr;
445                         lov_lock_unlink(env, scan, sub);
446                         restart = lovsub_lock_delete_one(env, child, lov);
447                         lovsub_parent_unlock(env, lov);
448
449                         if (restart) {
450                                 cl_lock_mutex_get(env, child);
451                                 break;
452                         }
453                }
454         } while (restart);
455         EXIT;
456 }
457
458 static int lovsub_lock_print(const struct lu_env *env, void *cookie,
459                              lu_printer_t p, const struct cl_lock_slice *slice)
460 {
461         struct lovsub_lock   *sub = cl2lovsub_lock(slice);
462         struct lov_lock      *lov;
463         struct lov_lock_link *scan;
464
465         list_for_each_entry(scan, &sub->lss_parents, lll_list) {
466                 lov = scan->lll_super;
467                 (*p)(env, cookie, "[%d %p ", scan->lll_idx, lov);
468                 if (lov != NULL)
469                         cl_lock_descr_print(env, cookie, p,
470                                             &lov->lls_cl.cls_lock->cll_descr);
471                 (*p)(env, cookie, "] ");
472         }
473         return 0;
474 }
475
476 static const struct cl_lock_operations lovsub_lock_ops = {
477         .clo_fini    = lovsub_lock_fini,
478         .clo_state   = lovsub_lock_state,
479         .clo_delete  = lovsub_lock_delete,
480         .clo_modify  = lovsub_lock_modify,
481         .clo_closure = lovsub_lock_closure,
482         .clo_weigh   = lovsub_lock_weigh,
483         .clo_print   = lovsub_lock_print
484 };
485
486 int lovsub_lock_init(const struct lu_env *env, struct cl_object *obj,
487                      struct cl_lock *lock, const struct cl_io *io)
488 {
489         struct lovsub_lock *lsk;
490         int result;
491
492         ENTRY;
493         OBD_SLAB_ALLOC_PTR_GFP(lsk, lovsub_lock_kmem, CFS_ALLOC_IO);
494         if (lsk != NULL) {
495                 CFS_INIT_LIST_HEAD(&lsk->lss_parents);
496                 cl_lock_slice_add(lock, &lsk->lss_cl, obj, &lovsub_lock_ops);
497                 result = 0;
498         } else
499                 result = -ENOMEM;
500         RETURN(result);
501 }
502
503 /** @} lov */