Whamcloud - gitweb
b=20997 Reset imp_vbr_fail flag upon eviction
[fs/lustre-release.git] / lustre / osc / osc_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 OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_OSC
42
43 #ifdef __KERNEL__
44 # include <libcfs/libcfs.h>
45 #else
46 # include <liblustre.h>
47 #endif
48 /* fid_build_reg_res_name() */
49 #include <lustre_fid.h>
50
51 #include "osc_cl_internal.h"
52
53 /** \addtogroup osc 
54  *  @{ 
55  */
56
57 /*****************************************************************************
58  *
59  * Type conversions.
60  *
61  */
62
63 static const struct cl_lock_operations osc_lock_ops;
64 static const struct cl_lock_operations osc_lock_lockless_ops;
65 static void osc_lock_to_lockless(const struct lu_env *env,
66                                  struct osc_lock *ols, int force);
67 static int osc_lock_has_pages(struct osc_lock *olck);
68
69 int osc_lock_is_lockless(const struct osc_lock *olck)
70 {
71         return (olck->ols_cl.cls_ops == &osc_lock_lockless_ops);
72 }
73
74 /**
75  * Returns a weak pointer to the ldlm lock identified by a handle. Returned
76  * pointer cannot be dereferenced, as lock is not protected from concurrent
77  * reclaim. This function is a helper for osc_lock_invariant().
78  */
79 static struct ldlm_lock *osc_handle_ptr(struct lustre_handle *handle)
80 {
81         struct ldlm_lock *lock;
82
83         lock = ldlm_handle2lock(handle);
84         if (lock != NULL)
85                 LDLM_LOCK_PUT(lock);
86         return lock;
87 }
88
89 /**
90  * Invariant that has to be true all of the time.
91  */
92 static int osc_lock_invariant(struct osc_lock *ols)
93 {
94         struct ldlm_lock *lock        = osc_handle_ptr(&ols->ols_handle);
95         struct ldlm_lock *olock       = ols->ols_lock;
96         int               handle_used = lustre_handle_is_used(&ols->ols_handle);
97
98         return
99                 ergo(osc_lock_is_lockless(ols),
100                      ols->ols_locklessable && ols->ols_lock == NULL)  ||
101                 (ergo(olock != NULL, handle_used) &&
102                  ergo(olock != NULL,
103                       olock->l_handle.h_cookie == ols->ols_handle.cookie) &&
104                  /*
105                   * Check that ->ols_handle and ->ols_lock are consistent, but
106                   * take into account that they are set at the different time.
107                   */
108                  ergo(handle_used,
109                       ergo(lock != NULL && olock != NULL, lock == olock) &&
110                       ergo(lock == NULL, olock == NULL)) &&
111                  ergo(ols->ols_state == OLS_CANCELLED,
112                       olock == NULL && !handle_used) &&
113                  /*
114                   * DLM lock is destroyed only after we have seen cancellation
115                   * ast.
116                   */
117                  ergo(olock != NULL && ols->ols_state < OLS_CANCELLED,
118                       !olock->l_destroyed) &&
119                  ergo(ols->ols_state == OLS_GRANTED,
120                       olock != NULL &&
121                       olock->l_req_mode == olock->l_granted_mode &&
122                       ols->ols_hold));
123 }
124
125 /*****************************************************************************
126  *
127  * Lock operations.
128  *
129  */
130
131 /**
132  * Breaks a link between osc_lock and dlm_lock.
133  */
134 static void osc_lock_detach(const struct lu_env *env, struct osc_lock *olck)
135 {
136         struct ldlm_lock *dlmlock;
137
138         cfs_spin_lock(&osc_ast_guard);
139         dlmlock = olck->ols_lock;
140         if (dlmlock == NULL) {
141                 cfs_spin_unlock(&osc_ast_guard);
142                 return;
143         }
144
145         olck->ols_lock = NULL;
146         /* wb(); --- for all who checks (ols->ols_lock != NULL) before
147          * call to osc_lock_detach() */
148         dlmlock->l_ast_data = NULL;
149         olck->ols_handle.cookie = 0ULL;
150         cfs_spin_unlock(&osc_ast_guard);
151
152         lock_res_and_lock(dlmlock);
153         if (dlmlock->l_granted_mode == dlmlock->l_req_mode) {
154                 struct cl_object *obj = olck->ols_cl.cls_obj;
155                 struct cl_attr *attr  = &osc_env_info(env)->oti_attr;
156                 __u64 old_kms = cl2osc(obj)->oo_oinfo->loi_kms;
157
158                 /* Update the kms. Need to loop all granted locks.
159                  * Not a problem for the client */
160                 attr->cat_kms = ldlm_extent_shift_kms(dlmlock, old_kms);
161                 unlock_res_and_lock(dlmlock);
162
163                 cl_object_attr_lock(obj);
164                 cl_object_attr_set(env, obj, attr, CAT_KMS);
165                 cl_object_attr_unlock(obj);
166         } else
167                 unlock_res_and_lock(dlmlock);
168
169         /* release a reference taken in osc_lock_upcall0(). */
170         LASSERT(olck->ols_has_ref);
171         lu_ref_del(&dlmlock->l_reference, "osc_lock", olck);
172         LDLM_LOCK_RELEASE(dlmlock);
173         olck->ols_has_ref = 0;
174 }
175
176 static int osc_lock_unhold(struct osc_lock *ols)
177 {
178         int result = 0;
179
180         if (ols->ols_hold) {
181                 ols->ols_hold = 0;
182                 result = osc_cancel_base(&ols->ols_handle,
183                                          ols->ols_einfo.ei_mode);
184         }
185         return result;
186 }
187
188 static int osc_lock_unuse(const struct lu_env *env,
189                           const struct cl_lock_slice *slice)
190 {
191         struct osc_lock *ols = cl2osc_lock(slice);
192
193         LASSERT(ols->ols_state == OLS_GRANTED ||
194                 ols->ols_state == OLS_UPCALL_RECEIVED);
195         LINVRNT(osc_lock_invariant(ols));
196
197         if (ols->ols_glimpse) {
198                 LASSERT(ols->ols_hold == 0);
199                 return 0;
200         }
201         LASSERT(ols->ols_hold);
202
203         /*
204          * Move lock into OLS_RELEASED state before calling osc_cancel_base()
205          * so that possible synchronous cancellation (that always happens
206          * e.g., for liblustre) sees that lock is released.
207          */
208         ols->ols_state = OLS_RELEASED;
209         return osc_lock_unhold(ols);
210 }
211
212 static void osc_lock_fini(const struct lu_env *env,
213                           struct cl_lock_slice *slice)
214 {
215         struct osc_lock  *ols = cl2osc_lock(slice);
216
217         LINVRNT(osc_lock_invariant(ols));
218         /*
219          * ->ols_hold can still be true at this point if, for example, a
220          * thread that requested a lock was killed (and released a reference
221          * to the lock), before reply from a server was received. In this case
222          * lock is destroyed immediately after upcall.
223          */
224         osc_lock_unhold(ols);
225         LASSERT(ols->ols_lock == NULL);
226
227         OBD_SLAB_FREE_PTR(ols, osc_lock_kmem);
228 }
229
230 void osc_lock_build_res(const struct lu_env *env, const struct osc_object *obj,
231                         struct ldlm_res_id *resname)
232 {
233         const struct lu_fid *fid = lu_object_fid(&obj->oo_cl.co_lu);
234         if (0) {
235                 /*
236                  * In the perfect world of the future, where ost servers talk
237                  * idif-fids...
238                  */
239                 fid_build_reg_res_name(fid, resname);
240         } else {
241                 /*
242                  * In reality, where ost server expects ->lsm_object_id and
243                  * ->lsm_object_gr in rename.
244                  */
245                 osc_build_res_name(obj->oo_oinfo->loi_id, obj->oo_oinfo->loi_gr,
246                                    resname);
247         }
248 }
249
250 static void osc_lock_build_policy(const struct lu_env *env,
251                                   const struct cl_lock *lock,
252                                   ldlm_policy_data_t *policy)
253 {
254         const struct cl_lock_descr *d = &lock->cll_descr;
255
256         osc_index2policy(policy, d->cld_obj, d->cld_start, d->cld_end);
257         policy->l_extent.gid = d->cld_gid;
258 }
259
260 static int osc_enq2ldlm_flags(__u32 enqflags)
261 {
262         int result = 0;
263
264         LASSERT((enqflags & ~CEF_MASK) == 0);
265
266         if (enqflags & CEF_NONBLOCK)
267                 result |= LDLM_FL_BLOCK_NOWAIT;
268         if (enqflags & CEF_ASYNC)
269                 result |= LDLM_FL_HAS_INTENT;
270         if (enqflags & CEF_DISCARD_DATA)
271                 result |= LDLM_AST_DISCARD_DATA;
272         return result;
273 }
274
275 /**
276  * Global spin-lock protecting consistency of ldlm_lock::l_ast_data
277  * pointers. Initialized in osc_init().
278  */
279 cfs_spinlock_t osc_ast_guard;
280
281 static struct osc_lock *osc_ast_data_get(struct ldlm_lock *dlm_lock)
282 {
283         struct osc_lock *olck;
284
285         lock_res_and_lock(dlm_lock);
286         cfs_spin_lock(&osc_ast_guard);
287         olck = dlm_lock->l_ast_data;
288         if (olck != NULL) {
289                 struct cl_lock *lock = olck->ols_cl.cls_lock;
290                 /*
291                  * If osc_lock holds a reference on ldlm lock, return it even
292                  * when cl_lock is in CLS_FREEING state. This way
293                  *
294                  *         osc_ast_data_get(dlmlock) == NULL
295                  *
296                  * guarantees that all osc references on dlmlock were
297                  * released. osc_dlm_blocking_ast0() relies on that.
298                  */
299                 if (lock->cll_state < CLS_FREEING || olck->ols_has_ref) {
300                         cl_lock_get_trust(lock);
301                         lu_ref_add_atomic(&lock->cll_reference,
302                                           "ast", cfs_current());
303                 } else
304                         olck = NULL;
305         }
306         cfs_spin_unlock(&osc_ast_guard);
307         unlock_res_and_lock(dlm_lock);
308         return olck;
309 }
310
311 static void osc_ast_data_put(const struct lu_env *env, struct osc_lock *olck)
312 {
313         struct cl_lock *lock;
314
315         lock = olck->ols_cl.cls_lock;
316         lu_ref_del(&lock->cll_reference, "ast", cfs_current());
317         cl_lock_put(env, lock);
318 }
319
320 /**
321  * Updates object attributes from a lock value block (lvb) received together
322  * with the DLM lock reply from the server. Copy of osc_update_enqueue()
323  * logic.
324  *
325  * This can be optimized to not update attributes when lock is a result of a
326  * local match.
327  *
328  * Called under lock and resource spin-locks.
329  */
330 static void osc_lock_lvb_update(const struct lu_env *env, struct osc_lock *olck,
331                                 int rc)
332 {
333         struct ost_lvb    *lvb;
334         struct cl_object  *obj;
335         struct lov_oinfo  *oinfo;
336         struct cl_attr    *attr;
337         unsigned           valid;
338
339         ENTRY;
340
341         if (!(olck->ols_flags & LDLM_FL_LVB_READY)) {
342                 EXIT;
343                 return;
344         }
345
346         lvb   = &olck->ols_lvb;
347         obj   = olck->ols_cl.cls_obj;
348         oinfo = cl2osc(obj)->oo_oinfo;
349         attr  = &osc_env_info(env)->oti_attr;
350         valid = CAT_BLOCKS | CAT_ATIME | CAT_CTIME | CAT_MTIME | CAT_SIZE;
351         cl_lvb2attr(attr, lvb);
352
353         cl_object_attr_lock(obj);
354         if (rc == 0) {
355                 struct ldlm_lock  *dlmlock;
356                 __u64 size;
357
358                 dlmlock = olck->ols_lock;
359                 LASSERT(dlmlock != NULL);
360
361                 /* re-grab LVB from a dlm lock under DLM spin-locks. */
362                 *lvb = *(struct ost_lvb *)dlmlock->l_lvb_data;
363                 size = lvb->lvb_size;
364                 /* Extend KMS up to the end of this lock and no further
365                  * A lock on [x,y] means a KMS of up to y + 1 bytes! */
366                 if (size > dlmlock->l_policy_data.l_extent.end)
367                         size = dlmlock->l_policy_data.l_extent.end + 1;
368                 if (size >= oinfo->loi_kms) {
369                         LDLM_DEBUG(dlmlock, "lock acquired, setting rss="LPU64
370                                    ", kms="LPU64, lvb->lvb_size, size);
371                         valid |= CAT_KMS;
372                         attr->cat_kms = size;
373                 } else {
374                         LDLM_DEBUG(dlmlock, "lock acquired, setting rss="
375                                    LPU64"; leaving kms="LPU64", end="LPU64,
376                                    lvb->lvb_size, oinfo->loi_kms,
377                                    dlmlock->l_policy_data.l_extent.end);
378                 }
379                 ldlm_lock_allow_match_locked(dlmlock);
380         } else if (rc == -ENAVAIL && olck->ols_glimpse) {
381                 CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
382                        " kms="LPU64"\n", lvb->lvb_size, oinfo->loi_kms);
383         } else
384                 valid = 0;
385
386         if (valid != 0)
387                 cl_object_attr_set(env, obj, attr, valid);
388
389         cl_object_attr_unlock(obj);
390
391         EXIT;
392 }
393
394 /**
395  * Called when a lock is granted, from an upcall (when server returned a
396  * granted lock), or from completion AST, when server returned a blocked lock.
397  *
398  * Called under lock and resource spin-locks, that are released temporarily
399  * here.
400  */
401 static void osc_lock_granted(const struct lu_env *env, struct osc_lock *olck,
402                              struct ldlm_lock *dlmlock, int rc)
403 {
404         struct ldlm_extent   *ext;
405         struct cl_lock       *lock;
406         struct cl_lock_descr *descr;
407
408         LASSERT(dlmlock->l_granted_mode == dlmlock->l_req_mode);
409
410         ENTRY;
411         if (olck->ols_state != OLS_GRANTED) {
412                 lock  = olck->ols_cl.cls_lock;
413                 ext   = &dlmlock->l_policy_data.l_extent;
414                 descr = &osc_env_info(env)->oti_descr;
415                 descr->cld_obj = lock->cll_descr.cld_obj;
416
417                 /* XXX check that ->l_granted_mode is valid. */
418                 descr->cld_mode  = osc_ldlm2cl_lock(dlmlock->l_granted_mode);
419                 descr->cld_start = cl_index(descr->cld_obj, ext->start);
420                 descr->cld_end   = cl_index(descr->cld_obj, ext->end);
421                 descr->cld_gid   = ext->gid;
422                 /*
423                  * tell upper layers the extent of the lock that was actually
424                  * granted
425                  */
426                 olck->ols_state = OLS_GRANTED;
427                 osc_lock_lvb_update(env, olck, rc);
428
429                 /* release DLM spin-locks to allow cl_lock_{modify,signal}()
430                  * to take a semaphore on a parent lock. This is safe, because
431                  * spin-locks are needed to protect consistency of
432                  * dlmlock->l_*_mode and LVB, and we have finished processing
433                  * them. */
434                 unlock_res_and_lock(dlmlock);
435                 cl_lock_modify(env, lock, descr);
436                 cl_lock_signal(env, lock);
437                 LINVRNT(osc_lock_invariant(olck));
438                 lock_res_and_lock(dlmlock);
439         }
440         EXIT;
441 }
442
443 static void osc_lock_upcall0(const struct lu_env *env, struct osc_lock *olck)
444
445 {
446         struct ldlm_lock *dlmlock;
447
448         ENTRY;
449
450         dlmlock = ldlm_handle2lock_long(&olck->ols_handle, 0);
451         LASSERT(dlmlock != NULL);
452
453         lock_res_and_lock(dlmlock);
454         cfs_spin_lock(&osc_ast_guard);
455         LASSERT(dlmlock->l_ast_data == olck);
456         LASSERT(olck->ols_lock == NULL);
457         olck->ols_lock = dlmlock;
458         cfs_spin_unlock(&osc_ast_guard);
459
460         /*
461          * Lock might be not yet granted. In this case, completion ast
462          * (osc_ldlm_completion_ast()) comes later and finishes lock
463          * granting.
464          */
465         if (dlmlock->l_granted_mode == dlmlock->l_req_mode)
466                 osc_lock_granted(env, olck, dlmlock, 0);
467         unlock_res_and_lock(dlmlock);
468
469         /*
470          * osc_enqueue_interpret() decrefs asynchronous locks, counter
471          * this.
472          */
473         ldlm_lock_addref(&olck->ols_handle, olck->ols_einfo.ei_mode);
474         olck->ols_hold = 1;
475
476         /* lock reference taken by ldlm_handle2lock_long() is owned by
477          * osc_lock and released in osc_lock_detach() */
478         lu_ref_add(&dlmlock->l_reference, "osc_lock", olck);
479         olck->ols_has_ref = 1;
480 }
481
482 /**
483  * Lock upcall function that is executed either when a reply to ENQUEUE rpc is
484  * received from a server, or after osc_enqueue_base() matched a local DLM
485  * lock.
486  */
487 static int osc_lock_upcall(void *cookie, int errcode)
488 {
489         struct osc_lock         *olck  = cookie;
490         struct cl_lock_slice    *slice = &olck->ols_cl;
491         struct cl_lock          *lock  = slice->cls_lock;
492         struct lu_env           *env;
493         struct cl_env_nest       nest;
494
495         ENTRY;
496         env = cl_env_nested_get(&nest);
497         if (!IS_ERR(env)) {
498                 int rc;
499
500                 cl_lock_mutex_get(env, lock);
501
502                 LASSERT(lock->cll_state >= CLS_QUEUING);
503                 if (olck->ols_state == OLS_ENQUEUED) {
504                         olck->ols_state = OLS_UPCALL_RECEIVED;
505                         rc = ldlm_error2errno(errcode);
506                 } else if (olck->ols_state == OLS_CANCELLED) {
507                         rc = -EIO;
508                 } else {
509                         CERROR("Impossible state: %i\n", olck->ols_state);
510                         LBUG();
511                 }
512                 if (rc) {
513                         struct ldlm_lock *dlmlock;
514
515                         dlmlock = ldlm_handle2lock(&olck->ols_handle);
516                         if (dlmlock != NULL) {
517                                 lock_res_and_lock(dlmlock);
518                                 cfs_spin_lock(&osc_ast_guard);
519                                 LASSERT(olck->ols_lock == NULL);
520                                 dlmlock->l_ast_data = NULL;
521                                 olck->ols_handle.cookie = 0ULL;
522                                 cfs_spin_unlock(&osc_ast_guard);
523                                 unlock_res_and_lock(dlmlock);
524                                 LDLM_LOCK_PUT(dlmlock);
525                         }
526                 } else {
527                         if (olck->ols_glimpse)
528                                 olck->ols_glimpse = 0;
529                         osc_lock_upcall0(env, olck);
530                 }
531
532                 /* Error handling, some errors are tolerable. */
533                 if (olck->ols_locklessable && rc == -EUSERS) {
534                         /* This is a tolerable error, turn this lock into
535                          * lockless lock.
536                          */
537                         osc_object_set_contended(cl2osc(slice->cls_obj));
538                         LASSERT(slice->cls_ops == &osc_lock_ops);
539
540                         /* Change this lock to ldlmlock-less lock. */
541                         osc_lock_to_lockless(env, olck, 1);
542                         olck->ols_state = OLS_GRANTED;
543                         rc = 0;
544                 } else if (olck->ols_glimpse && rc == -ENAVAIL) {
545                         osc_lock_lvb_update(env, olck, rc);
546                         cl_lock_delete(env, lock);
547                         /* Hide the error. */
548                         rc = 0;
549                 }
550
551                 if (rc == 0)
552                         /* on error, lock was signaled by cl_lock_error() */
553                         cl_lock_signal(env, lock);
554                 else
555                         cl_lock_error(env, lock, rc);
556
557                 cl_lock_mutex_put(env, lock);
558
559                 /* release cookie reference, acquired by osc_lock_enqueue() */
560                 lu_ref_del(&lock->cll_reference, "upcall", lock);
561                 cl_lock_put(env, lock);
562                 cl_env_nested_put(&nest, env);
563         } else
564                 /* should never happen, similar to osc_ldlm_blocking_ast(). */
565                 LBUG();
566         RETURN(errcode);
567 }
568
569 /**
570  * Core of osc_dlm_blocking_ast() logic.
571  */
572 static void osc_lock_blocking(const struct lu_env *env,
573                               struct ldlm_lock *dlmlock,
574                               struct osc_lock *olck, int blocking)
575 {
576         struct cl_lock *lock = olck->ols_cl.cls_lock;
577
578         LASSERT(olck->ols_lock == dlmlock);
579         CLASSERT(OLS_BLOCKED < OLS_CANCELLED);
580         LASSERT(!osc_lock_is_lockless(olck));
581
582         /*
583          * Lock might be still addref-ed here, if e.g., blocking ast
584          * is sent for a failed lock.
585          */
586         osc_lock_unhold(olck);
587
588         if (blocking && olck->ols_state < OLS_BLOCKED)
589                 /*
590                  * Move osc_lock into OLS_BLOCKED before canceling the lock,
591                  * because it recursively re-enters osc_lock_blocking(), with
592                  * the state set to OLS_CANCELLED.
593                  */
594                 olck->ols_state = OLS_BLOCKED;
595         /*
596          * cancel and destroy lock at least once no matter how blocking ast is
597          * entered (see comment above osc_ldlm_blocking_ast() for use
598          * cases). cl_lock_cancel() and cl_lock_delete() are idempotent.
599          */
600         cl_lock_cancel(env, lock);
601         cl_lock_delete(env, lock);
602 }
603
604 /**
605  * Helper for osc_dlm_blocking_ast() handling discrepancies between cl_lock
606  * and ldlm_lock caches.
607  */
608 static int osc_dlm_blocking_ast0(const struct lu_env *env,
609                                  struct ldlm_lock *dlmlock,
610                                  void *data, int flag)
611 {
612         struct osc_lock *olck;
613         struct cl_lock  *lock;
614         int result;
615         int cancel;
616
617         LASSERT(flag == LDLM_CB_BLOCKING || flag == LDLM_CB_CANCELING);
618
619         cancel = 0;
620         olck = osc_ast_data_get(dlmlock);
621         if (olck != NULL) {
622                 lock = olck->ols_cl.cls_lock;
623                 cl_lock_mutex_get(env, lock);
624                 LINVRNT(osc_lock_invariant(olck));
625                 if (olck->ols_ast_wait) {
626                         /* wake up osc_lock_use() */
627                         cl_lock_signal(env, lock);
628                         olck->ols_ast_wait = 0;
629                 }
630                 /*
631                  * Lock might have been canceled while this thread was
632                  * sleeping for lock mutex, but olck is pinned in memory.
633                  */
634                 if (olck == dlmlock->l_ast_data) {
635                         /*
636                          * NOTE: DLM sends blocking AST's for failed locks
637                          *       (that are still in pre-OLS_GRANTED state)
638                          *       too, and they have to be canceled otherwise
639                          *       DLM lock is never destroyed and stuck in
640                          *       the memory.
641                          *
642                          *       Alternatively, ldlm_cli_cancel() can be
643                          *       called here directly for osc_locks with
644                          *       ols_state < OLS_GRANTED to maintain an
645                          *       invariant that ->clo_cancel() is only called
646                          *       for locks that were granted.
647                          */
648                         LASSERT(data == olck);
649                         osc_lock_blocking(env, dlmlock,
650                                           olck, flag == LDLM_CB_BLOCKING);
651                 } else
652                         cancel = 1;
653                 cl_lock_mutex_put(env, lock);
654                 osc_ast_data_put(env, olck);
655         } else
656                 /*
657                  * DLM lock exists, but there is no cl_lock attached to it.
658                  * This is a `normal' race. cl_object and its cl_lock's can be
659                  * removed by memory pressure, together with all pages.
660                  */
661                 cancel = (flag == LDLM_CB_BLOCKING);
662
663         if (cancel) {
664                 struct lustre_handle *lockh;
665
666                 lockh = &osc_env_info(env)->oti_handle;
667                 ldlm_lock2handle(dlmlock, lockh);
668                 result = ldlm_cli_cancel(lockh);
669         } else
670                 result = 0;
671         return result;
672 }
673
674 /**
675  * Blocking ast invoked by ldlm when dlm lock is either blocking progress of
676  * some other lock, or is canceled. This function is installed as a
677  * ldlm_lock::l_blocking_ast() for client extent locks.
678  *
679  * Control flow is tricky, because ldlm uses the same call-back
680  * (ldlm_lock::l_blocking_ast()) for both blocking and cancellation ast's.
681  *
682  * \param dlmlock lock for which ast occurred.
683  *
684  * \param new description of a conflicting lock in case of blocking ast.
685  *
686  * \param data value of dlmlock->l_ast_data
687  *
688  * \param flag LDLM_CB_BLOCKING or LDLM_CB_CANCELING. Used to distinguish
689  *             cancellation and blocking ast's.
690  *
691  * Possible use cases:
692  *
693  *     - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING) to cancel
694  *       lock due to lock lru pressure, or explicit user request to purge
695  *       locks.
696  *
697  *     - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_BLOCKING) to notify
698  *       us that dlmlock conflicts with another lock that some client is
699  *       enqueing. Lock is canceled.
700  *
701  *           - cl_lock_cancel() is called. osc_lock_cancel() calls
702  *             ldlm_cli_cancel() that calls
703  *
704  *                  dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
705  *
706  *             recursively entering osc_ldlm_blocking_ast().
707  *
708  *     - client cancels lock voluntary (e.g., as a part of early cancellation):
709  *
710  *           cl_lock_cancel()->
711  *             osc_lock_cancel()->
712  *               ldlm_cli_cancel()->
713  *                 dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
714  *
715  */
716 static int osc_ldlm_blocking_ast(struct ldlm_lock *dlmlock,
717                                  struct ldlm_lock_desc *new, void *data,
718                                  int flag)
719 {
720         struct lu_env     *env;
721         struct cl_env_nest nest;
722         int                result;
723
724         /*
725          * This can be called in the context of outer IO, e.g.,
726          *
727          *     cl_enqueue()->...
728          *       ->osc_enqueue_base()->...
729          *         ->ldlm_prep_elc_req()->...
730          *           ->ldlm_cancel_callback()->...
731          *             ->osc_ldlm_blocking_ast()
732          *
733          * new environment has to be created to not corrupt outer context.
734          */
735         env = cl_env_nested_get(&nest);
736         if (!IS_ERR(env)) {
737                 result = osc_dlm_blocking_ast0(env, dlmlock, data, flag);
738                 cl_env_nested_put(&nest, env);
739         } else {
740                 result = PTR_ERR(env);
741                 /*
742                  * XXX This should never happen, as cl_lock is
743                  * stuck. Pre-allocated environment a la vvp_inode_fini_env
744                  * should be used.
745                  */
746                 LBUG();
747         }
748         if (result != 0) {
749                 if (result == -ENODATA)
750                         result = 0;
751                 else
752                         CERROR("BAST failed: %d\n", result);
753         }
754         return result;
755 }
756
757 static int osc_ldlm_completion_ast(struct ldlm_lock *dlmlock,
758                                    int flags, void *data)
759 {
760         struct cl_env_nest nest;
761         struct lu_env     *env;
762         struct osc_lock   *olck;
763         struct cl_lock    *lock;
764         int result;
765         int dlmrc;
766
767         /* first, do dlm part of the work */
768         dlmrc = ldlm_completion_ast_async(dlmlock, flags, data);
769         /* then, notify cl_lock */
770         env = cl_env_nested_get(&nest);
771         if (!IS_ERR(env)) {
772                 olck = osc_ast_data_get(dlmlock);
773                 if (olck != NULL) {
774                         lock = olck->ols_cl.cls_lock;
775                         cl_lock_mutex_get(env, lock);
776                         /*
777                          * ldlm_handle_cp_callback() copied LVB from request
778                          * to lock->l_lvb_data, store it in osc_lock.
779                          */
780                         LASSERT(dlmlock->l_lvb_data != NULL);
781                         lock_res_and_lock(dlmlock);
782                         olck->ols_lvb = *(struct ost_lvb *)dlmlock->l_lvb_data;
783                         if (olck->ols_lock == NULL) {
784                                 /*
785                                  * upcall (osc_lock_upcall()) hasn't yet been
786                                  * called. Do nothing now, upcall will bind
787                                  * olck to dlmlock and signal the waiters.
788                                  *
789                                  * This maintains an invariant that osc_lock
790                                  * and ldlm_lock are always bound when
791                                  * osc_lock is in OLS_GRANTED state.
792                                  */
793                         } else if (dlmlock->l_granted_mode ==
794                                    dlmlock->l_req_mode) {
795                                 osc_lock_granted(env, olck, dlmlock, dlmrc);
796                         }
797                         unlock_res_and_lock(dlmlock);
798
799                         if (dlmrc != 0) {
800                                 CL_LOCK_DEBUG(D_ERROR, env, lock,
801                                               "dlmlock returned %d\n", dlmrc);
802                                 cl_lock_error(env, lock, dlmrc);
803                         }
804                         cl_lock_mutex_put(env, lock);
805                         osc_ast_data_put(env, olck);
806                         result = 0;
807                 } else
808                         result = -ELDLM_NO_LOCK_DATA;
809                 cl_env_nested_put(&nest, env);
810         } else
811                 result = PTR_ERR(env);
812         return dlmrc ?: result;
813 }
814
815 static int osc_ldlm_glimpse_ast(struct ldlm_lock *dlmlock, void *data)
816 {
817         struct ptlrpc_request  *req  = data;
818         struct osc_lock        *olck;
819         struct cl_lock         *lock;
820         struct cl_object       *obj;
821         struct cl_env_nest      nest;
822         struct lu_env          *env;
823         struct ost_lvb         *lvb;
824         struct req_capsule     *cap;
825         int                     result;
826
827         LASSERT(lustre_msg_get_opc(req->rq_reqmsg) == LDLM_GL_CALLBACK);
828
829         env = cl_env_nested_get(&nest);
830         if (!IS_ERR(env)) {
831                 /*
832                  * osc_ast_data_get() has to go after environment is
833                  * allocated, because osc_ast_data() acquires a
834                  * reference to a lock, and it can only be released in
835                  * environment.
836                  */
837                 olck = osc_ast_data_get(dlmlock);
838                 if (olck != NULL) {
839                         lock = olck->ols_cl.cls_lock;
840                         cl_lock_mutex_get(env, lock);
841                         cap = &req->rq_pill;
842                         req_capsule_extend(cap, &RQF_LDLM_GL_CALLBACK);
843                         req_capsule_set_size(cap, &RMF_DLM_LVB, RCL_SERVER,
844                                              sizeof *lvb);
845                         result = req_capsule_server_pack(cap);
846                         if (result == 0) {
847                                 lvb = req_capsule_server_get(cap, &RMF_DLM_LVB);
848                                 obj = lock->cll_descr.cld_obj;
849                                 result = cl_object_glimpse(env, obj, lvb);
850                         }
851                         cl_lock_mutex_put(env, lock);
852                         osc_ast_data_put(env, olck);
853                 } else {
854                         /*
855                          * These errors are normal races, so we don't want to
856                          * fill the console with messages by calling
857                          * ptlrpc_error()
858                          */
859                         lustre_pack_reply(req, 1, NULL, NULL);
860                         result = -ELDLM_NO_LOCK_DATA;
861                 }
862                 cl_env_nested_put(&nest, env);
863         } else
864                 result = PTR_ERR(env);
865         req->rq_status = result;
866         return result;
867 }
868
869 static unsigned long osc_lock_weigh(const struct lu_env *env,
870                                     const struct cl_lock_slice *slice)
871 {
872         /*
873          * don't need to grab coh_page_guard since we don't care the exact #
874          * of pages..
875          */
876         return cl_object_header(slice->cls_obj)->coh_pages;
877 }
878
879 /**
880  * Get the weight of dlm lock for early cancellation.
881  *
882  * XXX: it should return the pages covered by this \a dlmlock.
883  */
884 static unsigned long osc_ldlm_weigh_ast(struct ldlm_lock *dlmlock)
885 {
886         struct cl_env_nest       nest;
887         struct lu_env           *env;
888         struct osc_lock         *lock;
889         struct cl_lock          *cll;
890         unsigned long            weight;
891         ENTRY;
892
893         cfs_might_sleep();
894         /*
895          * osc_ldlm_weigh_ast has a complex context since it might be called
896          * because of lock canceling, or from user's input. We have to make
897          * a new environment for it. Probably it is implementation safe to use
898          * the upper context because cl_lock_put don't modify environment
899          * variables. But in case of ..
900          */
901         env = cl_env_nested_get(&nest);
902         if (IS_ERR(env))
903                 /* Mostly because lack of memory, tend to eliminate this lock*/
904                 RETURN(0);
905
906         LASSERT(dlmlock->l_resource->lr_type == LDLM_EXTENT);
907         lock = osc_ast_data_get(dlmlock);
908         if (lock == NULL) {
909                 /* cl_lock was destroyed because of memory pressure.
910                  * It is much reasonable to assign this type of lock
911                  * a lower cost.
912                  */
913                 GOTO(out, weight = 0);
914         }
915
916         cll = lock->ols_cl.cls_lock;
917         cl_lock_mutex_get(env, cll);
918         weight = cl_lock_weigh(env, cll);
919         cl_lock_mutex_put(env, cll);
920         osc_ast_data_put(env, lock);
921         EXIT;
922
923 out:
924         cl_env_nested_put(&nest, env);
925         return weight;
926 }
927
928 static void osc_lock_build_einfo(const struct lu_env *env,
929                                  const struct cl_lock *clock,
930                                  struct osc_lock *lock,
931                                  struct ldlm_enqueue_info *einfo)
932 {
933         enum cl_lock_mode mode;
934
935         mode = clock->cll_descr.cld_mode;
936         if (mode == CLM_PHANTOM)
937                 /*
938                  * For now, enqueue all glimpse locks in read mode. In the
939                  * future, client might choose to enqueue LCK_PW lock for
940                  * glimpse on a file opened for write.
941                  */
942                 mode = CLM_READ;
943
944         einfo->ei_type   = LDLM_EXTENT;
945         einfo->ei_mode   = osc_cl_lock2ldlm(mode);
946         einfo->ei_cb_bl  = osc_ldlm_blocking_ast;
947         einfo->ei_cb_cp  = osc_ldlm_completion_ast;
948         einfo->ei_cb_gl  = osc_ldlm_glimpse_ast;
949         einfo->ei_cb_wg  = osc_ldlm_weigh_ast;
950         einfo->ei_cbdata = lock; /* value to be put into ->l_ast_data */
951 }
952
953 /**
954  * Determine if the lock should be converted into a lockless lock.
955  *
956  * Steps to check:
957  * - if the lock has an explicite requirment for a non-lockless lock;
958  * - if the io lock request type ci_lockreq;
959  * - send the enqueue rpc to ost to make the further decision;
960  * - special treat to truncate lockless lock
961  *
962  *  Additional policy can be implemented here, e.g., never do lockless-io
963  *  for large extents.
964  */
965 static void osc_lock_to_lockless(const struct lu_env *env,
966                                  struct osc_lock *ols, int force)
967 {
968         struct cl_lock_slice *slice = &ols->ols_cl;
969         struct cl_lock *lock        = slice->cls_lock;
970
971         LASSERT(ols->ols_state == OLS_NEW ||
972                 ols->ols_state == OLS_UPCALL_RECEIVED);
973
974         if (force) {
975                 ols->ols_locklessable = 1;
976                 LASSERT(cl_lock_is_mutexed(lock));
977                 slice->cls_ops = &osc_lock_lockless_ops;
978         } else {
979                 struct osc_io *oio     = osc_env_io(env);
980                 struct cl_io  *io      = oio->oi_cl.cis_io;
981                 struct cl_object *obj  = slice->cls_obj;
982                 struct osc_object *oob = cl2osc(obj);
983                 const struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
984                 struct obd_connect_data *ocd;
985
986                 LASSERT(io->ci_lockreq == CILR_MANDATORY ||
987                         io->ci_lockreq == CILR_MAYBE ||
988                         io->ci_lockreq == CILR_NEVER);
989
990                 ocd = &class_exp2cliimp(osc_export(oob))->imp_connect_data;
991                 ols->ols_locklessable = (io->ci_type != CIT_TRUNC) &&
992                                 (io->ci_lockreq == CILR_MAYBE) &&
993                                 (ocd->ocd_connect_flags & OBD_CONNECT_SRVLOCK);
994                 if (io->ci_lockreq == CILR_NEVER ||
995                         /* lockless IO */
996                     (ols->ols_locklessable && osc_object_is_contended(oob)) ||
997                         /* lockless truncate */
998                     (io->ci_type == CIT_TRUNC &&
999                      (ocd->ocd_connect_flags & OBD_CONNECT_TRUNCLOCK) &&
1000                       osd->od_lockless_truncate)) {
1001                         ols->ols_locklessable = 1;
1002                         slice->cls_ops = &osc_lock_lockless_ops;
1003                 }
1004         }
1005         LASSERT(ergo(ols->ols_glimpse, !osc_lock_is_lockless(ols)));
1006 }
1007
1008 static int osc_lock_compatible(const struct osc_lock *qing,
1009                                const struct osc_lock *qed)
1010 {
1011         enum cl_lock_mode qing_mode;
1012         enum cl_lock_mode qed_mode;
1013
1014         qing_mode = qing->ols_cl.cls_lock->cll_descr.cld_mode;
1015         if (qed->ols_glimpse &&
1016             (qed->ols_state >= OLS_UPCALL_RECEIVED || qing_mode == CLM_READ))
1017                 return 1;
1018
1019         qed_mode = qed->ols_cl.cls_lock->cll_descr.cld_mode;
1020         return ((qing_mode == CLM_READ) && (qed_mode == CLM_READ));
1021 }
1022
1023 /**
1024  * Cancel all conflicting locks and wait for them to be destroyed.
1025  *
1026  * This function is used for two purposes:
1027  *
1028  *     - early cancel all conflicting locks before starting IO, and
1029  *
1030  *     - guarantee that pages added to the page cache by lockless IO are never
1031  *       covered by locks other than lockless IO lock, and, hence, are not
1032  *       visible to other threads.
1033  */
1034 static int osc_lock_enqueue_wait(const struct lu_env *env,
1035                                  const struct osc_lock *olck)
1036 {
1037         struct cl_lock          *lock    = olck->ols_cl.cls_lock;
1038         struct cl_lock_descr    *descr   = &lock->cll_descr;
1039         struct cl_object_header *hdr     = cl_object_header(descr->cld_obj);
1040         struct cl_lock          *scan    = lock;
1041         struct cl_lock          *conflict= NULL;
1042         int lockless                     = osc_lock_is_lockless(olck);
1043         int rc                           = 0;
1044         ENTRY;
1045
1046         LASSERT(cl_lock_is_mutexed(lock));
1047         LASSERT(lock->cll_state == CLS_QUEUING);
1048
1049         /* make it enqueue anyway for glimpse lock, because we actually
1050          * don't need to cancel any conflicting locks. */
1051         if (olck->ols_glimpse)
1052                 return 0;
1053
1054         cfs_spin_lock(&hdr->coh_lock_guard);
1055         cfs_list_for_each_entry_continue(scan, &hdr->coh_locks, cll_linkage) {
1056                 struct cl_lock_descr *cld = &scan->cll_descr;
1057                 const struct osc_lock *scan_ols;
1058
1059                 if (scan->cll_state < CLS_QUEUING ||
1060                     scan->cll_state == CLS_FREEING ||
1061                     cld->cld_start > descr->cld_end ||
1062                     cld->cld_end < descr->cld_start)
1063                         continue;
1064
1065                 /* overlapped and living locks. */
1066
1067                 /* We're not supposed to give up group lock. */
1068                 if (scan->cll_descr.cld_mode == CLM_GROUP) {
1069                         LASSERT(descr->cld_mode != CLM_GROUP ||
1070                                 descr->cld_gid != scan->cll_descr.cld_gid);
1071                         continue;
1072                 }
1073
1074                 scan_ols = osc_lock_at(scan);
1075
1076                 /* We need to cancel the compatible locks if we're enqueuing
1077                  * a lockless lock, for example:
1078                  * imagine that client has PR lock on [0, 1000], and thread T0
1079                  * is doing lockless IO in [500, 1500] region. Concurrent
1080                  * thread T1 can see lockless data in [500, 1000], which is
1081                  * wrong, because these data are possibly stale. */
1082                 if (!lockless && osc_lock_compatible(olck, scan_ols))
1083                         continue;
1084
1085                 /* Now @scan is conflicting with @lock, this means current
1086                  * thread have to sleep for @scan being destroyed. */
1087                 if (scan_ols->ols_owner == osc_env_io(env)) {
1088                         CERROR("DEADLOCK POSSIBLE!\n");
1089                         CL_LOCK_DEBUG(D_ERROR, env, scan, "queued.\n");
1090                         CL_LOCK_DEBUG(D_ERROR, env, lock, "queuing.\n");
1091                         libcfs_debug_dumpstack(NULL);
1092                 }
1093                 cl_lock_get_trust(scan);
1094                 conflict = scan;
1095                 break;
1096         }
1097         cfs_spin_unlock(&hdr->coh_lock_guard);
1098
1099         if (conflict) {
1100                 CDEBUG(D_DLMTRACE, "lock %p is confliced with %p, will wait\n",
1101                        lock, conflict);
1102                 lu_ref_add(&conflict->cll_reference, "cancel-wait", lock);
1103                 LASSERT(lock->cll_conflict == NULL);
1104                 lock->cll_conflict = conflict;
1105                 rc = CLO_WAIT;
1106         }
1107         RETURN(rc);
1108 }
1109
1110 /**
1111  * Deadlock avoidance for osc_lock_enqueue(). Consider following scenario:
1112  *
1113  *     - Thread0: obtains PR:[0, 10]. Lock is busy.
1114  *
1115  *     - Thread1: enqueues PW:[5, 50]. Blocking ast is sent to
1116  *       PR:[0, 10], but cancellation of busy lock is postponed.
1117  *
1118  *     - Thread0: enqueue PR:[30, 40]. Lock is locally matched to
1119  *       PW:[5, 50], and thread0 waits for the lock completion never
1120  *       releasing PR:[0, 10]---deadlock.
1121  *
1122  * The second PR lock can be glimpse (it is to deal with that situation that
1123  * ll_glimpse_size() has second argument, preventing local match of
1124  * not-yet-granted locks, see bug 10295). Similar situation is possible in the
1125  * case of memory mapped user level buffer.
1126  *
1127  * To prevent this we can detect a situation when current "thread" or "io"
1128  * already holds a lock on this object and either add LDLM_FL_BLOCK_GRANTED to
1129  * the ols->ols_flags, or prevent local match with PW locks.
1130  */
1131 static int osc_deadlock_is_possible(const struct lu_env *env,
1132                                     struct cl_lock *lock)
1133 {
1134         struct cl_object        *obj;
1135         struct cl_object_header *head;
1136         struct cl_lock          *scan;
1137         struct osc_io           *oio;
1138
1139         int result;
1140
1141         ENTRY;
1142
1143         LASSERT(cl_lock_is_mutexed(lock));
1144
1145         oio  = osc_env_io(env);
1146         obj  = lock->cll_descr.cld_obj;
1147         head = cl_object_header(obj);
1148
1149         result = 0;
1150         cfs_spin_lock(&head->coh_lock_guard);
1151         cfs_list_for_each_entry(scan, &head->coh_locks, cll_linkage) {
1152                 if (scan != lock) {
1153                         struct osc_lock *oscan;
1154
1155                         oscan = osc_lock_at(scan);
1156                         LASSERT(oscan != NULL);
1157                         if (oscan->ols_owner == oio) {
1158                                 result = 1;
1159                                 break;
1160                         }
1161                 }
1162         }
1163         cfs_spin_unlock(&head->coh_lock_guard);
1164         RETURN(result);
1165 }
1166
1167 /**
1168  * Implementation of cl_lock_operations::clo_enqueue() method for osc
1169  * layer. This initiates ldlm enqueue:
1170  *
1171  *     - checks for possible dead-lock conditions (osc_deadlock_is_possible());
1172  *
1173  *     - cancels conflicting locks early (osc_lock_enqueue_wait());
1174  *
1175  *     - calls osc_enqueue_base() to do actual enqueue.
1176  *
1177  * osc_enqueue_base() is supplied with an upcall function that is executed
1178  * when lock is received either after a local cached ldlm lock is matched, or
1179  * when a reply from the server is received.
1180  *
1181  * This function does not wait for the network communication to complete.
1182  */
1183 static int osc_lock_enqueue(const struct lu_env *env,
1184                             const struct cl_lock_slice *slice,
1185                             struct cl_io *unused, __u32 enqflags)
1186 {
1187         struct osc_lock          *ols     = cl2osc_lock(slice);
1188         struct cl_lock           *lock    = ols->ols_cl.cls_lock;
1189         struct osc_object        *obj     = cl2osc(slice->cls_obj);
1190         struct osc_thread_info   *info    = osc_env_info(env);
1191         struct ldlm_res_id       *resname = &info->oti_resname;
1192         ldlm_policy_data_t       *policy  = &info->oti_policy;
1193         struct ldlm_enqueue_info *einfo   = &ols->ols_einfo;
1194         int result;
1195         ENTRY;
1196
1197         LASSERT(cl_lock_is_mutexed(lock));
1198         LASSERT(lock->cll_state == CLS_QUEUING);
1199         LASSERT(ols->ols_state == OLS_NEW);
1200
1201         osc_lock_build_res(env, obj, resname);
1202         osc_lock_build_policy(env, lock, policy);
1203         ols->ols_flags = osc_enq2ldlm_flags(enqflags);
1204         if (osc_deadlock_is_possible(env, lock))
1205                 ols->ols_flags |= LDLM_FL_BLOCK_GRANTED;
1206         if (ols->ols_flags & LDLM_FL_HAS_INTENT)
1207                 ols->ols_glimpse = 1;
1208         if (!(enqflags & CEF_MUST))
1209                 /* try to convert this lock to a lockless lock */
1210                 osc_lock_to_lockless(env, ols, (enqflags & CEF_NEVER));
1211
1212         result = osc_lock_enqueue_wait(env, ols);
1213         if (result == 0) {
1214                 if (!osc_lock_is_lockless(ols)) {
1215                         if (ols->ols_locklessable)
1216                                 ols->ols_flags |= LDLM_FL_DENY_ON_CONTENTION;
1217
1218                         /* a reference for lock, passed as an upcall cookie */
1219                         cl_lock_get(lock);
1220                         lu_ref_add(&lock->cll_reference, "upcall", lock);
1221                         ols->ols_state = OLS_ENQUEUED;
1222
1223                         /*
1224                          * XXX: this is possible blocking point as
1225                          * ldlm_lock_match(LDLM_FL_LVB_READY) waits for
1226                          * LDLM_CP_CALLBACK.
1227                          */
1228                         result = osc_enqueue_base(osc_export(obj), resname,
1229                                           &ols->ols_flags, policy,
1230                                           &ols->ols_lvb,
1231                                           obj->oo_oinfo->loi_kms_valid,
1232                                           osc_lock_upcall,
1233                                           ols, einfo, &ols->ols_handle,
1234                                           PTLRPCD_SET, 1);
1235                         if (result != 0) {
1236                                 lu_ref_del(&lock->cll_reference,
1237                                            "upcall", lock);
1238                                 cl_lock_put(env, lock);
1239                         }
1240                 } else {
1241                         ols->ols_state = OLS_GRANTED;
1242                 }
1243         }
1244         LASSERT(ergo(ols->ols_glimpse, !osc_lock_is_lockless(ols)));
1245         RETURN(result);
1246 }
1247
1248 static int osc_lock_wait(const struct lu_env *env,
1249                          const struct cl_lock_slice *slice)
1250 {
1251         struct osc_lock *olck = cl2osc_lock(slice);
1252         struct cl_lock  *lock = olck->ols_cl.cls_lock;
1253
1254         LINVRNT(osc_lock_invariant(olck));
1255         if (olck->ols_glimpse && olck->ols_state >= OLS_UPCALL_RECEIVED)
1256                 return 0;
1257
1258         LASSERT(equi(olck->ols_state >= OLS_UPCALL_RECEIVED &&
1259                      lock->cll_error == 0, olck->ols_lock != NULL));
1260
1261         return lock->cll_error ?: olck->ols_state >= OLS_GRANTED ? 0 : CLO_WAIT;
1262 }
1263
1264 /**
1265  * An implementation of cl_lock_operations::clo_use() method that pins cached
1266  * lock.
1267  */
1268 static int osc_lock_use(const struct lu_env *env,
1269                         const struct cl_lock_slice *slice)
1270 {
1271         struct osc_lock *olck = cl2osc_lock(slice);
1272         int rc;
1273
1274         LASSERT(!olck->ols_hold);
1275
1276         /*
1277          * Atomically check for LDLM_FL_CBPENDING and addref a lock if this
1278          * flag is not set. This protects us from a concurrent blocking ast.
1279          */
1280         rc = ldlm_lock_addref_try(&olck->ols_handle, olck->ols_einfo.ei_mode);
1281         if (rc == 0) {
1282                 olck->ols_hold = 1;
1283                 olck->ols_state = OLS_GRANTED;
1284         } else {
1285                 struct cl_lock *lock;
1286
1287                 /*
1288                  * Lock is being cancelled somewhere within
1289                  * ldlm_handle_bl_callback(): LDLM_FL_CBPENDING is already
1290                  * set, but osc_ldlm_blocking_ast() hasn't yet acquired
1291                  * cl_lock mutex.
1292                  */
1293                 lock = slice->cls_lock;
1294                 LASSERT(lock->cll_state == CLS_INTRANSIT);
1295                 LASSERT(lock->cll_users > 0);
1296                 /* set a flag for osc_dlm_blocking_ast0() to signal the
1297                  * lock.*/
1298                 olck->ols_ast_wait = 1;
1299                 rc = CLO_WAIT;
1300         }
1301         return rc;
1302 }
1303
1304 static int osc_lock_flush(struct osc_lock *ols, int discard)
1305 {
1306         struct cl_lock       *lock  = ols->ols_cl.cls_lock;
1307         struct cl_env_nest    nest;
1308         struct lu_env        *env;
1309         int result = 0;
1310
1311         env = cl_env_nested_get(&nest);
1312         if (!IS_ERR(env)) {
1313                 result = cl_lock_page_out(env, lock, discard);
1314                 cl_env_nested_put(&nest, env);
1315         } else
1316                 result = PTR_ERR(env);
1317         if (result == 0) {
1318                 ols->ols_flush = 1;
1319                 LINVRNT(!osc_lock_has_pages(ols));
1320         }
1321         return result;
1322 }
1323
1324 /**
1325  * Implements cl_lock_operations::clo_cancel() method for osc layer. This is
1326  * called (as part of cl_lock_cancel()) when lock is canceled either voluntary
1327  * (LRU pressure, early cancellation, umount, etc.) or due to the conflict
1328  * with some other lock some where in the cluster. This function does the
1329  * following:
1330  *
1331  *     - invalidates all pages protected by this lock (after sending dirty
1332  *       ones to the server, as necessary);
1333  *
1334  *     - decref's underlying ldlm lock;
1335  *
1336  *     - cancels ldlm lock (ldlm_cli_cancel()).
1337  */
1338 static void osc_lock_cancel(const struct lu_env *env,
1339                             const struct cl_lock_slice *slice)
1340 {
1341         struct cl_lock   *lock    = slice->cls_lock;
1342         struct osc_lock  *olck    = cl2osc_lock(slice);
1343         struct ldlm_lock *dlmlock = olck->ols_lock;
1344         int               result  = 0;
1345         int               discard;
1346
1347         LASSERT(cl_lock_is_mutexed(lock));
1348         LINVRNT(osc_lock_invariant(olck));
1349
1350         if (dlmlock != NULL) {
1351                 int do_cancel;
1352
1353                 discard = dlmlock->l_flags & LDLM_FL_DISCARD_DATA;
1354                 result = osc_lock_flush(olck, discard);
1355                 osc_lock_unhold(olck);
1356
1357                 lock_res_and_lock(dlmlock);
1358                 /* Now that we're the only user of dlm read/write reference,
1359                  * mostly the ->l_readers + ->l_writers should be zero.
1360                  * However, there is a corner case.
1361                  * See bug 18829 for details.*/
1362                 do_cancel = (dlmlock->l_readers == 0 &&
1363                              dlmlock->l_writers == 0);
1364                 dlmlock->l_flags |= LDLM_FL_CBPENDING;
1365                 unlock_res_and_lock(dlmlock);
1366                 if (do_cancel)
1367                         result = ldlm_cli_cancel(&olck->ols_handle);
1368                 if (result < 0)
1369                         CL_LOCK_DEBUG(D_ERROR, env, lock,
1370                                       "lock %p cancel failure with error(%d)\n",
1371                                       lock, result);
1372         }
1373         olck->ols_state = OLS_CANCELLED;
1374         osc_lock_detach(env, olck);
1375 }
1376
1377 void cl_lock_page_list_fixup(const struct lu_env *env,
1378                              struct cl_io *io, struct cl_lock *lock,
1379                              struct cl_page_list *queue);
1380
1381 #ifdef INVARIANT_CHECK
1382 /**
1383  * Returns true iff there are pages under \a olck not protected by other
1384  * locks.
1385  */
1386 static int osc_lock_has_pages(struct osc_lock *olck)
1387 {
1388         struct cl_lock       *lock;
1389         struct cl_lock_descr *descr;
1390         struct cl_object     *obj;
1391         struct osc_object    *oob;
1392         struct cl_page_list  *plist;
1393         struct cl_page       *page;
1394         struct cl_env_nest    nest;
1395         struct cl_io         *io;
1396         struct lu_env        *env;
1397         int                   result;
1398
1399         env = cl_env_nested_get(&nest);
1400         if (!IS_ERR(env)) {
1401                 obj   = olck->ols_cl.cls_obj;
1402                 oob   = cl2osc(obj);
1403                 io    = &oob->oo_debug_io;
1404                 lock  = olck->ols_cl.cls_lock;
1405                 descr = &lock->cll_descr;
1406                 plist = &osc_env_info(env)->oti_plist;
1407                 cl_page_list_init(plist);
1408
1409                 cfs_mutex_lock(&oob->oo_debug_mutex);
1410
1411                 io->ci_obj = cl_object_top(obj);
1412                 cl_io_init(env, io, CIT_MISC, io->ci_obj);
1413                 cl_page_gang_lookup(env, obj, io,
1414                                     descr->cld_start, descr->cld_end, plist, 0);
1415                 cl_lock_page_list_fixup(env, io, lock, plist);
1416                 if (plist->pl_nr > 0) {
1417                         CL_LOCK_DEBUG(D_ERROR, env, lock, "still has pages\n");
1418                         cl_page_list_for_each(page, plist)
1419                                 CL_PAGE_DEBUG(D_ERROR, env, page, "\n");
1420                 }
1421                 result = plist->pl_nr > 0;
1422                 cl_page_list_disown(env, io, plist);
1423                 cl_page_list_fini(env, plist);
1424                 cl_io_fini(env, io);
1425                 cfs_mutex_unlock(&oob->oo_debug_mutex);
1426                 cl_env_nested_put(&nest, env);
1427         } else
1428                 result = 0;
1429         return result;
1430 }
1431 #else
1432 static int osc_lock_has_pages(struct osc_lock *olck)
1433 {
1434         return 0;
1435 }
1436 #endif /* INVARIANT_CHECK */
1437
1438 static void osc_lock_delete(const struct lu_env *env,
1439                             const struct cl_lock_slice *slice)
1440 {
1441         struct osc_lock *olck;
1442
1443         olck = cl2osc_lock(slice);
1444         if (olck->ols_glimpse) {
1445                 LASSERT(!olck->ols_hold);
1446                 LASSERT(!olck->ols_lock);
1447                 return;
1448         }
1449
1450         LINVRNT(osc_lock_invariant(olck));
1451         LINVRNT(!osc_lock_has_pages(olck));
1452
1453         osc_lock_unhold(olck);
1454         osc_lock_detach(env, olck);
1455 }
1456
1457 /**
1458  * Implements cl_lock_operations::clo_state() method for osc layer.
1459  *
1460  * Maintains osc_lock::ols_owner field.
1461  *
1462  * This assumes that lock always enters CLS_HELD (from some other state) in
1463  * the same IO context as one that requested the lock. This should not be a
1464  * problem, because context is by definition shared by all activity pertaining
1465  * to the same high-level IO.
1466  */
1467 static void osc_lock_state(const struct lu_env *env,
1468                            const struct cl_lock_slice *slice,
1469                            enum cl_lock_state state)
1470 {
1471         struct osc_lock *lock = cl2osc_lock(slice);
1472         struct osc_io   *oio  = osc_env_io(env);
1473
1474         /*
1475          * XXX multiple io contexts can use the lock at the same time.
1476          */
1477         LINVRNT(osc_lock_invariant(lock));
1478         if (state == CLS_HELD && slice->cls_lock->cll_state != CLS_HELD) {
1479                 LASSERT(lock->ols_owner == NULL);
1480                 lock->ols_owner = oio;
1481         } else if (state != CLS_HELD)
1482                 lock->ols_owner = NULL;
1483 }
1484
1485 static int osc_lock_print(const struct lu_env *env, void *cookie,
1486                           lu_printer_t p, const struct cl_lock_slice *slice)
1487 {
1488         struct osc_lock *lock = cl2osc_lock(slice);
1489
1490         /*
1491          * XXX print ldlm lock and einfo properly.
1492          */
1493         (*p)(env, cookie, "%p %08x "LPX64" %d %p ",
1494              lock->ols_lock, lock->ols_flags, lock->ols_handle.cookie,
1495              lock->ols_state, lock->ols_owner);
1496         osc_lvb_print(env, cookie, p, &lock->ols_lvb);
1497         return 0;
1498 }
1499
1500 static int osc_lock_fits_into(const struct lu_env *env,
1501                               const struct cl_lock_slice *slice,
1502                               const struct cl_lock_descr *need,
1503                               const struct cl_io *io)
1504 {
1505         struct osc_lock *ols = cl2osc_lock(slice);
1506
1507         if (need->cld_enq_flags & CEF_NEVER)
1508                 return 0;
1509
1510         if (need->cld_mode == CLM_PHANTOM) {
1511                 /*
1512                  * Note: the QUEUED lock can't be matched here, otherwise
1513                  * it might cause the deadlocks.
1514                  * In read_process,
1515                  * P1: enqueued read lock, create sublock1
1516                  * P2: enqueued write lock, create sublock2(conflicted
1517                  *     with sublock1).
1518                  * P1: Grant read lock.
1519                  * P1: enqueued glimpse lock(with holding sublock1_read),
1520                  *     matched with sublock2, waiting sublock2 to be granted.
1521                  *     But sublock2 can not be granted, because P1
1522                  *     will not release sublock1. Bang!
1523                  */
1524                 if (ols->ols_state < OLS_GRANTED ||
1525                         ols->ols_state > OLS_RELEASED)
1526                         return 0;
1527         } else if (need->cld_enq_flags & CEF_MUST) {
1528                  /*
1529                  * If the lock hasn't ever enqueued, it can't be matched
1530                  * because enqueue process brings in many information
1531                  * which can be used to determine things such as lockless,
1532                  * CEF_MUST, etc.
1533                  */
1534                 if (ols->ols_state < OLS_GRANTED ||
1535                         ols->ols_state > OLS_RELEASED)
1536                         return 0;
1537                 if (ols->ols_state < OLS_UPCALL_RECEIVED &&
1538                         ols->ols_locklessable)
1539                         return 0;
1540         }
1541         return 1;
1542 }
1543
1544 static const struct cl_lock_operations osc_lock_ops = {
1545         .clo_fini    = osc_lock_fini,
1546         .clo_enqueue = osc_lock_enqueue,
1547         .clo_wait    = osc_lock_wait,
1548         .clo_unuse   = osc_lock_unuse,
1549         .clo_use     = osc_lock_use,
1550         .clo_delete  = osc_lock_delete,
1551         .clo_state   = osc_lock_state,
1552         .clo_cancel  = osc_lock_cancel,
1553         .clo_weigh   = osc_lock_weigh,
1554         .clo_print   = osc_lock_print,
1555         .clo_fits_into = osc_lock_fits_into,
1556 };
1557
1558 static int osc_lock_lockless_enqueue(const struct lu_env *env,
1559                                      const struct cl_lock_slice *slice,
1560                                      struct cl_io *unused, __u32 enqflags)
1561 {
1562         LBUG();
1563         return 0;
1564 }
1565
1566 static int osc_lock_lockless_unuse(const struct lu_env *env,
1567                                    const struct cl_lock_slice *slice)
1568 {
1569         struct osc_lock *ols = cl2osc_lock(slice);
1570         struct cl_lock *lock = slice->cls_lock;
1571
1572         LASSERT(ols->ols_state == OLS_GRANTED);
1573         LINVRNT(osc_lock_invariant(ols));
1574
1575         cl_lock_cancel(env, lock);
1576         cl_lock_delete(env, lock);
1577         return 0;
1578 }
1579
1580 static void osc_lock_lockless_cancel(const struct lu_env *env,
1581                                      const struct cl_lock_slice *slice)
1582 {
1583         struct osc_lock   *ols  = cl2osc_lock(slice);
1584         int result;
1585
1586         result = osc_lock_flush(ols, 0);
1587         if (result)
1588                 CERROR("Pages for lockless lock %p were not purged(%d)\n",
1589                        ols, result);
1590         ols->ols_state = OLS_CANCELLED;
1591 }
1592
1593 static int osc_lock_lockless_wait(const struct lu_env *env,
1594                                   const struct cl_lock_slice *slice)
1595 {
1596         struct osc_lock *olck = cl2osc_lock(slice);
1597         struct cl_lock  *lock = olck->ols_cl.cls_lock;
1598
1599         LINVRNT(osc_lock_invariant(olck));
1600         LASSERT(olck->ols_state >= OLS_UPCALL_RECEIVED);
1601
1602         return lock->cll_error;
1603 }
1604
1605 static void osc_lock_lockless_state(const struct lu_env *env,
1606                                     const struct cl_lock_slice *slice,
1607                                     enum cl_lock_state state)
1608 {
1609         struct osc_lock *lock = cl2osc_lock(slice);
1610         struct osc_io   *oio  = osc_env_io(env);
1611
1612         LINVRNT(osc_lock_invariant(lock));
1613         if (state == CLS_HELD) {
1614                 LASSERT(lock->ols_owner == NULL);
1615                 lock->ols_owner = oio;
1616
1617                 /* set the io to be lockless if this lock is for io's
1618                  * host object */
1619                 if (cl_object_same(oio->oi_cl.cis_obj, slice->cls_obj))
1620                         oio->oi_lockless = 1;
1621         } else
1622                 lock->ols_owner = NULL;
1623 }
1624
1625 static int osc_lock_lockless_fits_into(const struct lu_env *env,
1626                                        const struct cl_lock_slice *slice,
1627                                        const struct cl_lock_descr *need,
1628                                        const struct cl_io *io)
1629 {
1630         return 0;
1631 }
1632
1633 static const struct cl_lock_operations osc_lock_lockless_ops = {
1634         .clo_fini      = osc_lock_fini,
1635         .clo_enqueue   = osc_lock_lockless_enqueue,
1636         .clo_wait      = osc_lock_lockless_wait,
1637         .clo_unuse     = osc_lock_lockless_unuse,
1638         .clo_state     = osc_lock_lockless_state,
1639         .clo_fits_into = osc_lock_lockless_fits_into,
1640         .clo_cancel    = osc_lock_lockless_cancel,
1641         .clo_print     = osc_lock_print
1642 };
1643
1644 int osc_lock_init(const struct lu_env *env,
1645                   struct cl_object *obj, struct cl_lock *lock,
1646                   const struct cl_io *unused)
1647 {
1648         struct osc_lock *clk;
1649         int result;
1650
1651         OBD_SLAB_ALLOC_PTR_GFP(clk, osc_lock_kmem, CFS_ALLOC_IO);
1652         if (clk != NULL) {
1653                 osc_lock_build_einfo(env, lock, clk, &clk->ols_einfo);
1654                 clk->ols_state = OLS_NEW;
1655                 cl_lock_slice_add(lock, &clk->ols_cl, obj, &osc_lock_ops);
1656                 result = 0;
1657         } else
1658                 result = -ENOMEM;
1659         return result;
1660 }
1661
1662
1663 /** @} osc */