Whamcloud - gitweb
3e4f1acf54588cea9de2a4b4e12e5646eea6bc40
[fs/lustre-release.git] / lustre / ldlm / ldlm_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  * lustre/ldlm/ldlm_lock.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LDLM
43
44 #ifdef __KERNEL__
45 # include <libcfs/libcfs.h>
46 # ifndef HAVE_VFS_INTENT_PATCHES
47 # include <linux/lustre_intent.h>
48 # endif
49 #else
50 # include <liblustre.h>
51 #endif
52
53 #include <obd_class.h>
54 #include "ldlm_internal.h"
55
56 /* lock types */
57 char *ldlm_lockname[] = {
58         [0] "--",
59         [LCK_EX] "EX",
60         [LCK_PW] "PW",
61         [LCK_PR] "PR",
62         [LCK_CW] "CW",
63         [LCK_CR] "CR",
64         [LCK_NL] "NL",
65         [LCK_GROUP] "GROUP",
66         [LCK_COS] "COS"
67 };
68
69 char *ldlm_typename[] = {
70         [LDLM_PLAIN] "PLN",
71         [LDLM_EXTENT] "EXT",
72         [LDLM_FLOCK] "FLK",
73         [LDLM_IBITS] "IBT",
74 };
75
76 char *ldlm_it2str(int it)
77 {
78         switch (it) {
79         case IT_OPEN:
80                 return "open";
81         case IT_CREAT:
82                 return "creat";
83         case (IT_OPEN | IT_CREAT):
84                 return "open|creat";
85         case IT_READDIR:
86                 return "readdir";
87         case IT_GETATTR:
88                 return "getattr";
89         case IT_LOOKUP:
90                 return "lookup";
91         case IT_UNLINK:
92                 return "unlink";
93         case IT_GETXATTR:
94                 return "getxattr";
95         default:
96                 CERROR("Unknown intent %d\n", it);
97                 return "UNKNOWN";
98         }
99 }
100
101 extern cfs_mem_cache_t *ldlm_lock_slab;
102
103 static ldlm_processing_policy ldlm_processing_policy_table[] = {
104         [LDLM_PLAIN] ldlm_process_plain_lock,
105         [LDLM_EXTENT] ldlm_process_extent_lock,
106 #ifdef __KERNEL__
107         [LDLM_FLOCK] ldlm_process_flock_lock,
108 #endif
109         [LDLM_IBITS] ldlm_process_inodebits_lock,
110 };
111
112 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
113 {
114         return ldlm_processing_policy_table[res->lr_type];
115 }
116
117 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
118 {
119         ns->ns_policy = arg;
120 }
121
122 /*
123  * REFCOUNTED LOCK OBJECTS
124  */
125
126
127 /*
128  * Lock refcounts, during creation:
129  *   - one special one for allocation, dec'd only once in destroy
130  *   - one for being a lock that's in-use
131  *   - one for the addref associated with a new lock
132  */
133 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
134 {
135         atomic_inc(&lock->l_refc);
136         return lock;
137 }
138
139 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
140 {
141         LASSERT(size == sizeof(*lock));
142         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
143 }
144
145 void ldlm_lock_put(struct ldlm_lock *lock)
146 {
147         ENTRY;
148
149         LASSERT(lock->l_resource != LP_POISON);
150         LASSERT(atomic_read(&lock->l_refc) > 0);
151         if (atomic_dec_and_test(&lock->l_refc)) {
152                 struct ldlm_resource *res;
153
154                 LDLM_DEBUG(lock,
155                            "final lock_put on destroyed lock, freeing it.");
156
157                 res = lock->l_resource;
158                 LASSERT(lock->l_destroyed);
159                 LASSERT(list_empty(&lock->l_res_link));
160                 LASSERT(list_empty(&lock->l_pending_chain));
161
162                 atomic_dec(&res->lr_namespace->ns_locks);
163                 lu_ref_del(&res->lr_reference, "lock", lock);
164                 ldlm_resource_putref(res);
165                 lock->l_resource = NULL;
166                 if (lock->l_export) {
167                         LASSERT(atomic_read(&lock->l_export->exp_locks_count) > 0);
168                         atomic_dec(&lock->l_export->exp_locks_count);
169                         class_export_put(lock->l_export);
170                         lock->l_export = NULL;
171                 }
172
173                 if (lock->l_lvb_data != NULL)
174                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
175
176                 ldlm_interval_free(ldlm_interval_detach(lock));
177                 lu_ref_fini(&lock->l_reference);
178                 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
179                                 ldlm_lock_free);
180         }
181
182         EXIT;
183 }
184
185 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
186 {
187         int rc = 0;
188         if (!list_empty(&lock->l_lru)) {
189                 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
190                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
191                 list_del_init(&lock->l_lru);
192                 LASSERT(ns->ns_nr_unused > 0);
193                 ns->ns_nr_unused--;
194                 rc = 1;
195         }
196         return rc;
197 }
198
199 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
200 {
201         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
202         int rc;
203         ENTRY;
204         spin_lock(&ns->ns_unused_lock);
205         rc = ldlm_lock_remove_from_lru_nolock(lock);
206         spin_unlock(&ns->ns_unused_lock);
207         EXIT;
208         return rc;
209 }
210
211 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
212 {
213         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
214         lock->l_last_used = cfs_time_current();
215         LASSERT(list_empty(&lock->l_lru));
216         LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
217         list_add_tail(&lock->l_lru, &ns->ns_unused_list);
218         LASSERT(ns->ns_nr_unused >= 0);
219         ns->ns_nr_unused++;
220 }
221
222 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
223 {
224         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
225         ENTRY;
226         spin_lock(&ns->ns_unused_lock);
227         ldlm_lock_add_to_lru_nolock(lock);
228         spin_unlock(&ns->ns_unused_lock);
229         EXIT;
230 }
231
232 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
233 {
234         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
235         ENTRY;
236         spin_lock(&ns->ns_unused_lock);
237         if (!list_empty(&lock->l_lru)) {
238                 ldlm_lock_remove_from_lru_nolock(lock);
239                 ldlm_lock_add_to_lru_nolock(lock);
240         }
241         spin_unlock(&ns->ns_unused_lock);
242         EXIT;
243 }
244
245 /* This used to have a 'strict' flag, which recovery would use to mark an
246  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
247  * shall explain why it's gone: with the new hash table scheme, once you call
248  * ldlm_lock_destroy, you can never drop your final references on this lock.
249  * Because it's not in the hash table anymore.  -phil */
250 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
251 {
252         ENTRY;
253
254         if (lock->l_readers || lock->l_writers) {
255                 LDLM_ERROR(lock, "lock still has references");
256                 ldlm_lock_dump(D_ERROR, lock, 0);
257                 LBUG();
258         }
259
260         if (!list_empty(&lock->l_res_link)) {
261                 LDLM_ERROR(lock, "lock still on resource");
262                 ldlm_lock_dump(D_ERROR, lock, 0);
263                 LBUG();
264         }
265
266         if (lock->l_destroyed) {
267                 LASSERT(list_empty(&lock->l_lru));
268                 EXIT;
269                 return 0;
270         }
271         lock->l_destroyed = 1;
272
273         if (lock->l_export && lock->l_export->exp_lock_hash &&
274             !hlist_unhashed(&lock->l_exp_hash))
275                 lustre_hash_del(lock->l_export->exp_lock_hash,
276                                 &lock->l_remote_handle, &lock->l_exp_hash);
277
278         ldlm_lock_remove_from_lru(lock);
279         class_handle_unhash(&lock->l_handle);
280
281 #if 0
282         /* Wake anyone waiting for this lock */
283         /* FIXME: I should probably add yet another flag, instead of using
284          * l_export to only call this on clients */
285         if (lock->l_export)
286                 class_export_put(lock->l_export);
287         lock->l_export = NULL;
288         if (lock->l_export && lock->l_completion_ast)
289                 lock->l_completion_ast(lock, 0);
290 #endif
291         EXIT;
292         return 1;
293 }
294
295 void ldlm_lock_destroy(struct ldlm_lock *lock)
296 {
297         int first;
298         ENTRY;
299         lock_res_and_lock(lock);
300         first = ldlm_lock_destroy_internal(lock);
301         unlock_res_and_lock(lock);
302
303         /* drop reference from hashtable only for first destroy */
304         if (first) {
305                 lu_ref_del(&lock->l_reference, "hash", lock);
306                 LDLM_LOCK_RELEASE(lock);
307         }
308         EXIT;
309 }
310
311 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
312 {
313         int first;
314         ENTRY;
315         first = ldlm_lock_destroy_internal(lock);
316         /* drop reference from hashtable only for first destroy */
317         if (first) {
318                 lu_ref_del(&lock->l_reference, "hash", lock);
319                 LDLM_LOCK_RELEASE(lock);
320         }
321         EXIT;
322 }
323
324 /* this is called by portals_handle2object with the handle lock taken */
325 static void lock_handle_addref(void *lock)
326 {
327         LDLM_LOCK_GET((struct ldlm_lock *)lock);
328 }
329
330 /*
331  * usage: pass in a resource on which you have done ldlm_resource_get
332  *        pass in a parent lock on which you have done a ldlm_lock_get
333  *        after return, ldlm_*_put the resource and parent
334  * returns: lock with refcount 2 - one for current caller and one for remote
335  */
336 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
337 {
338         struct ldlm_lock *lock;
339         ENTRY;
340
341         if (resource == NULL)
342                 LBUG();
343
344         OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
345         if (lock == NULL)
346                 RETURN(NULL);
347
348         spin_lock_init(&lock->l_lock);
349         lock->l_resource = ldlm_resource_getref(resource);
350         lu_ref_add(&resource->lr_reference, "lock", lock);
351
352         atomic_set(&lock->l_refc, 2);
353         CFS_INIT_LIST_HEAD(&lock->l_res_link);
354         CFS_INIT_LIST_HEAD(&lock->l_lru);
355         CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
356         CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
357         CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
358         CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
359         cfs_waitq_init(&lock->l_waitq);
360         lock->l_blocking_lock = NULL;
361         CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
362         CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
363         CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
364
365         atomic_inc(&resource->lr_namespace->ns_locks);
366         CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
367         class_handle_hash(&lock->l_handle, lock_handle_addref);
368
369         CFS_INIT_LIST_HEAD(&lock->l_extents_list);
370         spin_lock_init(&lock->l_extents_list_lock);
371         CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
372         lu_ref_init(&lock->l_reference);
373         lu_ref_add(&lock->l_reference, "hash", lock);
374         lock->l_callback_timeout = 0;
375
376         RETURN(lock);
377 }
378
379 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
380                               const struct ldlm_res_id *new_resid)
381 {
382         struct ldlm_resource *oldres = lock->l_resource;
383         struct ldlm_resource *newres;
384         int type;
385         ENTRY;
386
387         LASSERT(ns_is_client(ns));
388
389         lock_res_and_lock(lock);
390         if (memcmp(new_resid, &lock->l_resource->lr_name,
391                    sizeof(lock->l_resource->lr_name)) == 0) {
392                 /* Nothing to do */
393                 unlock_res_and_lock(lock);
394                 RETURN(0);
395         }
396
397         LASSERT(new_resid->name[0] != 0);
398
399         /* This function assumes that the lock isn't on any lists */
400         LASSERT(list_empty(&lock->l_res_link));
401
402         type = oldres->lr_type;
403         unlock_res_and_lock(lock);
404
405         newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
406         lu_ref_add(&newres->lr_reference, "lock", lock);
407         if (newres == NULL)
408                 RETURN(-ENOMEM);
409         /*
410          * To flip the lock from the old to the new resource, lock, oldres and
411          * newres have to be locked. Resource spin-locks are nested within
412          * lock->l_lock, and are taken in the memory address order to avoid
413          * dead-locks.
414          */
415         spin_lock(&lock->l_lock);
416         oldres = lock->l_resource;
417         if (oldres < newres) {
418                 lock_res(oldres);
419                 lock_res_nested(newres, LRT_NEW);
420         } else {
421                 lock_res(newres);
422                 lock_res_nested(oldres, LRT_NEW);
423         }
424         LASSERT(memcmp(new_resid, &oldres->lr_name,
425                        sizeof oldres->lr_name) != 0);
426         lock->l_resource = newres;
427         unlock_res(oldres);
428         unlock_res_and_lock(lock);
429
430         /* ...and the flowers are still standing! */
431         lu_ref_del(&oldres->lr_reference, "lock", lock);
432         ldlm_resource_putref(oldres);
433
434         RETURN(0);
435 }
436
437 /*
438  *  HANDLES
439  */
440
441 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
442 {
443         lockh->cookie = lock->l_handle.h_cookie;
444 }
445
446 /* if flags: atomically get the lock and set the flags.
447  *           Return NULL if flag already set
448  */
449
450 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
451                                      int flags)
452 {
453         struct ldlm_namespace *ns;
454         struct ldlm_lock *lock, *retval = NULL;
455         ENTRY;
456
457         LASSERT(handle);
458
459         lock = class_handle2object(handle->cookie);
460         if (lock == NULL)
461                 RETURN(NULL);
462
463         LASSERT(lock->l_resource != NULL);
464         ns = lock->l_resource->lr_namespace;
465         LASSERT(ns != NULL);
466
467         lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
468         lock_res_and_lock(lock);
469
470         /* It's unlikely but possible that someone marked the lock as
471          * destroyed after we did handle2object on it */
472         if (lock->l_destroyed) {
473                 unlock_res_and_lock(lock);
474                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
475                 LDLM_LOCK_PUT(lock);
476                 GOTO(out, retval);
477         }
478
479         if (flags && (lock->l_flags & flags)) {
480                 unlock_res_and_lock(lock);
481                 LDLM_LOCK_PUT(lock);
482                 GOTO(out, retval);
483         }
484
485         if (flags)
486                 lock->l_flags |= flags;
487
488         unlock_res_and_lock(lock);
489         retval = lock;
490         EXIT;
491  out:
492         return retval;
493 }
494
495 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
496 {
497         struct obd_export *exp = lock->l_export?:lock->l_conn_export;
498         /* INODEBITS_INTEROP: If the other side does not support
499          * inodebits, reply with a plain lock descriptor.
500          */
501         if ((lock->l_resource->lr_type == LDLM_IBITS) &&
502             (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
503                 struct ldlm_resource res = *lock->l_resource;
504
505                 /* Make sure all the right bits are set in this lock we
506                    are going to pass to client */
507                 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
508                          (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
509                          "Inappropriate inode lock bits during "
510                          "conversion " LPU64 "\n",
511                          lock->l_policy_data.l_inodebits.bits);
512                 res.lr_type = LDLM_PLAIN;
513                 ldlm_res2desc(&res, &desc->l_resource);
514                 /* Convert "new" lock mode to something old client can
515                    understand */
516                 if ((lock->l_req_mode == LCK_CR) ||
517                     (lock->l_req_mode == LCK_CW))
518                         desc->l_req_mode = LCK_PR;
519                 else
520                         desc->l_req_mode = lock->l_req_mode;
521                 if ((lock->l_granted_mode == LCK_CR) ||
522                     (lock->l_granted_mode == LCK_CW)) {
523                         desc->l_granted_mode = LCK_PR;
524                 } else {
525                         /* We never grant PW/EX locks to clients */
526                         LASSERT((lock->l_granted_mode != LCK_PW) &&
527                                 (lock->l_granted_mode != LCK_EX));
528                         desc->l_granted_mode = lock->l_granted_mode;
529                 }
530
531                 /* We do not copy policy here, because there is no
532                    policy for plain locks */
533         } else {
534                 ldlm_res2desc(lock->l_resource, &desc->l_resource);
535                 desc->l_req_mode = lock->l_req_mode;
536                 desc->l_granted_mode = lock->l_granted_mode;
537                 desc->l_policy_data = lock->l_policy_data;
538         }
539 }
540
541 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
542                            struct list_head *work_list)
543 {
544         if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
545                 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
546                 lock->l_flags |= LDLM_FL_AST_SENT;
547                 /* If the enqueuing client said so, tell the AST recipient to
548                  * discard dirty data, rather than writing back. */
549                 if (new->l_flags & LDLM_AST_DISCARD_DATA)
550                         lock->l_flags |= LDLM_FL_DISCARD_DATA;
551                 LASSERT(list_empty(&lock->l_bl_ast));
552                 list_add(&lock->l_bl_ast, work_list);
553                 LDLM_LOCK_GET(lock);
554                 LASSERT(lock->l_blocking_lock == NULL);
555                 lock->l_blocking_lock = LDLM_LOCK_GET(new);
556         }
557 }
558
559 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
560 {
561         if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
562                 lock->l_flags |= LDLM_FL_CP_REQD;
563                 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
564                 LASSERT(list_empty(&lock->l_cp_ast));
565                 list_add(&lock->l_cp_ast, work_list);
566                 LDLM_LOCK_GET(lock);
567         }
568 }
569
570 /* must be called with lr_lock held */
571 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
572                                 struct list_head *work_list)
573 {
574         ENTRY;
575         check_res_locked(lock->l_resource);
576         if (new)
577                 ldlm_add_bl_work_item(lock, new, work_list);
578         else
579                 ldlm_add_cp_work_item(lock, work_list);
580         EXIT;
581 }
582
583 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
584 {
585         struct ldlm_lock *lock;
586
587         lock = ldlm_handle2lock(lockh);
588         LASSERT(lock != NULL);
589         ldlm_lock_addref_internal(lock, mode);
590         LDLM_LOCK_PUT(lock);
591 }
592
593 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
594 {
595         ldlm_lock_remove_from_lru(lock);
596         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
597                 lock->l_readers++;
598                 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
599         }
600         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
601                 lock->l_writers++;
602                 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
603         }
604         LDLM_LOCK_GET(lock);
605         lu_ref_add_atomic(&lock->l_reference, "user", lock);
606         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
607 }
608
609 /**
610  * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
611  * or destroyed.
612  *
613  * \retval 0 success, lock was addref-ed
614  *
615  * \retval -EAGAIN lock is being canceled.
616  */
617 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
618 {
619         struct ldlm_lock *lock;
620         int               result;
621
622         result = -EAGAIN;
623         lock = ldlm_handle2lock(lockh);
624         if (lock != NULL) {
625                 lock_res_and_lock(lock);
626                 if (lock->l_readers != 0 || lock->l_writers != 0 ||
627                     !(lock->l_flags & LDLM_FL_CBPENDING)) {
628                         ldlm_lock_addref_internal_nolock(lock, mode);
629                         result = 0;
630                 }
631                 unlock_res_and_lock(lock);
632                 LDLM_LOCK_PUT(lock);
633         }
634         return result;
635 }
636
637 /* only called for local locks */
638 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
639 {
640         lock_res_and_lock(lock);
641         ldlm_lock_addref_internal_nolock(lock, mode);
642         unlock_res_and_lock(lock);
643 }
644
645 /* only called in ldlm_flock_destroy and for local locks.
646  *  * for LDLM_FLOCK type locks, l_blocking_ast is null, and
647  *   * ldlm_lock_remove_from_lru() does nothing, it is safe
648  *    * for ldlm_flock_destroy usage by dropping some code */
649 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
650 {
651         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
652         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
653                 LASSERT(lock->l_readers > 0);
654                 lu_ref_del(&lock->l_reference, "reader", lock);
655                 lock->l_readers--;
656         }
657         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
658                 LASSERT(lock->l_writers > 0);
659                 lu_ref_del(&lock->l_reference, "writer", lock);
660                 lock->l_writers--;
661         }
662
663         lu_ref_del(&lock->l_reference, "user", lock);
664         LDLM_LOCK_RELEASE(lock);    /* matches the LDLM_LOCK_GET() in addref */
665 }
666
667 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
668 {
669         struct ldlm_namespace *ns;
670         ENTRY;
671
672         lock_res_and_lock(lock);
673
674         ns = lock->l_resource->lr_namespace;
675
676         ldlm_lock_decref_internal_nolock(lock, mode);
677
678         if (lock->l_flags & LDLM_FL_LOCAL &&
679             !lock->l_readers && !lock->l_writers) {
680                 /* If this is a local lock on a server namespace and this was
681                  * the last reference, cancel the lock. */
682                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
683                 lock->l_flags |= LDLM_FL_CBPENDING;
684         }
685
686         if (!lock->l_readers && !lock->l_writers &&
687             (lock->l_flags & LDLM_FL_CBPENDING)) {
688                 /* If we received a blocked AST and this was the last reference,
689                  * run the callback. */
690                 if (ns_is_server(ns) && lock->l_export)
691                         CERROR("FL_CBPENDING set on non-local lock--just a "
692                                "warning\n");
693
694                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
695
696                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
697                 ldlm_lock_remove_from_lru(lock);
698                 unlock_res_and_lock(lock);
699
700                 if (lock->l_flags & LDLM_FL_FAIL_LOC)
701                         OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
702
703                 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
704                     ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
705                         ldlm_handle_bl_callback(ns, NULL, lock);
706         } else if (ns_is_client(ns) &&
707                    !lock->l_readers && !lock->l_writers &&
708                    !(lock->l_flags & LDLM_FL_BL_AST)) {
709                 /* If this is a client-side namespace and this was the last
710                  * reference, put it on the LRU. */
711                 ldlm_lock_add_to_lru(lock);
712                 unlock_res_and_lock(lock);
713
714                 if (lock->l_flags & LDLM_FL_FAIL_LOC)
715                         OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
716
717                 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
718                  * are not supported by the server, otherwise, it is done on
719                  * enqueue. */
720                 if (!exp_connect_cancelset(lock->l_conn_export) &&
721                     !ns_connect_lru_resize(ns))
722                         ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
723         } else {
724                 unlock_res_and_lock(lock);
725         }
726
727         EXIT;
728 }
729
730 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
731 {
732         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
733         LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
734         ldlm_lock_decref_internal(lock, mode);
735         LDLM_LOCK_PUT(lock);
736 }
737
738 /* This will drop a lock reference and mark it for destruction, but will not
739  * necessarily cancel the lock before returning. */
740 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
741 {
742         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
743         ENTRY;
744
745         LASSERT(lock != NULL);
746
747         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
748         lock_res_and_lock(lock);
749         lock->l_flags |= LDLM_FL_CBPENDING;
750         unlock_res_and_lock(lock);
751         ldlm_lock_decref_internal(lock, mode);
752         LDLM_LOCK_PUT(lock);
753 }
754
755 struct sl_insert_point {
756         struct list_head *res_link;
757         struct list_head *mode_link;
758         struct list_head *policy_link;
759 };
760
761 /*
762  * search_granted_lock
763  *
764  * Description:
765  *      Finds a position to insert the new lock.
766  * Parameters:
767  *      queue [input]:  the granted list where search acts on;
768  *      req [input]:    the lock whose position to be located;
769  *      prev [output]:  positions within 3 lists to insert @req to
770  * Return Value:
771  *      filled @prev
772  * NOTE: called by
773  *  - ldlm_grant_lock_with_skiplist
774  */
775 static void search_granted_lock(struct list_head *queue,
776                                 struct ldlm_lock *req,
777                                 struct sl_insert_point *prev)
778 {
779         struct list_head *tmp;
780         struct ldlm_lock *lock, *mode_end, *policy_end;
781         ENTRY;
782
783         list_for_each(tmp, queue) {
784                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
785
786                 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
787                                       l_sl_mode);
788
789                 if (lock->l_req_mode != req->l_req_mode) {
790                         /* jump to last lock of mode group */
791                         tmp = &mode_end->l_res_link;
792                         continue;
793                 }
794
795                 /* suitable mode group is found */
796                 if (lock->l_resource->lr_type == LDLM_PLAIN) {
797                         /* insert point is last lock of the mode group */
798                         prev->res_link = &mode_end->l_res_link;
799                         prev->mode_link = &mode_end->l_sl_mode;
800                         prev->policy_link = &req->l_sl_policy;
801                         EXIT;
802                         return;
803                 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
804                         for (;;) {
805                                 policy_end = list_entry(lock->l_sl_policy.prev,
806                                                         struct ldlm_lock,
807                                                         l_sl_policy);
808
809                                 if (lock->l_policy_data.l_inodebits.bits ==
810                                     req->l_policy_data.l_inodebits.bits) {
811                                         /* insert point is last lock of
812                                          * the policy group */
813                                         prev->res_link =
814                                                 &policy_end->l_res_link;
815                                         prev->mode_link =
816                                                 &policy_end->l_sl_mode;
817                                         prev->policy_link =
818                                                 &policy_end->l_sl_policy;
819                                         EXIT;
820                                         return;
821                                 }
822
823                                 if (policy_end == mode_end)
824                                         /* done with mode group */
825                                         break;
826
827                                 /* go to next policy group within mode group */
828                                 tmp = policy_end->l_res_link.next;
829                                 lock = list_entry(tmp, struct ldlm_lock,
830                                                   l_res_link);
831                         }  /* loop over policy groups within the mode group */
832
833                         /* insert point is last lock of the mode group,
834                          * new policy group is started */
835                         prev->res_link = &mode_end->l_res_link;
836                         prev->mode_link = &mode_end->l_sl_mode;
837                         prev->policy_link = &req->l_sl_policy;
838                         EXIT;
839                         return;
840                 } else {
841                         LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
842                         LBUG();
843                 }
844         }
845
846         /* insert point is last lock on the queue,
847          * new mode group and new policy group are started */
848         prev->res_link = queue->prev;
849         prev->mode_link = &req->l_sl_mode;
850         prev->policy_link = &req->l_sl_policy;
851         EXIT;
852         return;
853 }
854
855 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
856                                        struct sl_insert_point *prev)
857 {
858         struct ldlm_resource *res = lock->l_resource;
859         ENTRY;
860
861         check_res_locked(res);
862
863         ldlm_resource_dump(D_INFO, res);
864         CDEBUG(D_OTHER, "About to add this lock:\n");
865         ldlm_lock_dump(D_OTHER, lock, 0);
866
867         if (lock->l_destroyed) {
868                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
869                 return;
870         }
871
872         LASSERT(list_empty(&lock->l_res_link));
873         LASSERT(list_empty(&lock->l_sl_mode));
874         LASSERT(list_empty(&lock->l_sl_policy));
875
876         list_add(&lock->l_res_link, prev->res_link);
877         list_add(&lock->l_sl_mode, prev->mode_link);
878         list_add(&lock->l_sl_policy, prev->policy_link);
879
880         EXIT;
881 }
882
883 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
884 {
885         struct sl_insert_point prev;
886         ENTRY;
887
888         LASSERT(lock->l_req_mode == lock->l_granted_mode);
889
890         search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
891         ldlm_granted_list_add_lock(lock, &prev);
892         EXIT;
893 }
894
895 /* NOTE: called by
896  *  - ldlm_lock_enqueue
897  *  - ldlm_reprocess_queue
898  *  - ldlm_lock_convert
899  *
900  * must be called with lr_lock held
901  */
902 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
903 {
904         struct ldlm_resource *res = lock->l_resource;
905         ENTRY;
906
907         check_res_locked(res);
908
909         lock->l_granted_mode = lock->l_req_mode;
910         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
911                 ldlm_grant_lock_with_skiplist(lock);
912         else if (res->lr_type == LDLM_EXTENT)
913                 ldlm_extent_add_lock(res, lock);
914         else
915                 ldlm_resource_add_lock(res, &res->lr_granted, lock);
916
917         if (lock->l_granted_mode < res->lr_most_restr)
918                 res->lr_most_restr = lock->l_granted_mode;
919
920         if (work_list && lock->l_completion_ast != NULL)
921                 ldlm_add_ast_work_item(lock, NULL, work_list);
922
923         ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
924         EXIT;
925 }
926
927 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
928  * comment above ldlm_lock_match */
929 static struct ldlm_lock *search_queue(struct list_head *queue,
930                                       ldlm_mode_t *mode,
931                                       ldlm_policy_data_t *policy,
932                                       struct ldlm_lock *old_lock,
933                                       int flags, int unref)
934 {
935         struct ldlm_lock *lock;
936         struct list_head *tmp;
937
938         list_for_each(tmp, queue) {
939                 ldlm_mode_t match;
940
941                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
942
943                 if (lock == old_lock)
944                         break;
945
946                 /* llite sometimes wants to match locks that will be
947                  * canceled when their users drop, but we allow it to match
948                  * if it passes in CBPENDING and the lock still has users.
949                  * this is generally only going to be used by children
950                  * whose parents already hold a lock so forward progress
951                  * can still happen. */
952                 if (lock->l_flags & LDLM_FL_CBPENDING &&
953                     !(flags & LDLM_FL_CBPENDING))
954                         continue;
955                 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
956                     lock->l_readers == 0 && lock->l_writers == 0)
957                         continue;
958
959                 if (!(lock->l_req_mode & *mode))
960                         continue;
961                 match = lock->l_req_mode;
962
963                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
964                     (lock->l_policy_data.l_extent.start >
965                      policy->l_extent.start ||
966                      lock->l_policy_data.l_extent.end < policy->l_extent.end))
967                         continue;
968
969                 if (unlikely(match == LCK_GROUP) &&
970                     lock->l_resource->lr_type == LDLM_EXTENT &&
971                     lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
972                         continue;
973
974                 /* We match if we have existing lock with same or wider set
975                    of bits. */
976                 if (lock->l_resource->lr_type == LDLM_IBITS &&
977                      ((lock->l_policy_data.l_inodebits.bits &
978                       policy->l_inodebits.bits) !=
979                       policy->l_inodebits.bits))
980                         continue;
981
982                 if (!unref &&
983                     (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
984                         continue;
985
986                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
987                     !(lock->l_flags & LDLM_FL_LOCAL))
988                         continue;
989
990                 if (flags & LDLM_FL_TEST_LOCK) {
991                         LDLM_LOCK_GET(lock);
992                         ldlm_lock_touch_in_lru(lock);
993                 } else {
994                         ldlm_lock_addref_internal_nolock(lock, match);
995                 }
996                 *mode = match;
997                 return lock;
998         }
999
1000         return NULL;
1001 }
1002
1003 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1004 {
1005         lock->l_flags |= LDLM_FL_LVB_READY;
1006         cfs_waitq_signal(&lock->l_waitq);
1007 }
1008
1009 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1010 {
1011         lock_res_and_lock(lock);
1012         ldlm_lock_allow_match_locked(lock);
1013         unlock_res_and_lock(lock);
1014 }
1015
1016 /* Can be called in two ways:
1017  *
1018  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1019  * for a duplicate of.
1020  *
1021  * Otherwise, all of the fields must be filled in, to match against.
1022  *
1023  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1024  *     server (ie, connh is NULL)
1025  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1026  *     list will be considered
1027  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1028  *     to be canceled can still be matched as long as they still have reader
1029  *     or writer refernces
1030  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1031  *     just tell us if we would have matched.
1032  *
1033  * Returns 1 if it finds an already-existing lock that is compatible; in this
1034  * case, lockh is filled in with a addref()ed lock
1035  *
1036  * we also check security context, if that failed we simply return 0 (to keep
1037  * caller code unchanged), the context failure will be discovered by caller
1038  * sometime later.
1039  */
1040 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1041                             const struct ldlm_res_id *res_id, ldlm_type_t type,
1042                             ldlm_policy_data_t *policy, ldlm_mode_t mode,
1043                             struct lustre_handle *lockh, int unref)
1044 {
1045         struct ldlm_resource *res;
1046         struct ldlm_lock *lock, *old_lock = NULL;
1047         int rc = 0;
1048         ENTRY;
1049
1050         if (ns == NULL) {
1051                 old_lock = ldlm_handle2lock(lockh);
1052                 LASSERT(old_lock);
1053
1054                 ns = old_lock->l_resource->lr_namespace;
1055                 res_id = &old_lock->l_resource->lr_name;
1056                 type = old_lock->l_resource->lr_type;
1057                 mode = old_lock->l_req_mode;
1058         }
1059
1060         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1061         if (res == NULL) {
1062                 LASSERT(old_lock == NULL);
1063                 RETURN(0);
1064         }
1065
1066         LDLM_RESOURCE_ADDREF(res);
1067         lock_res(res);
1068
1069         lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1070                             flags, unref);
1071         if (lock != NULL)
1072                 GOTO(out, rc = 1);
1073         if (flags & LDLM_FL_BLOCK_GRANTED)
1074                 GOTO(out, rc = 0);
1075         lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1076                             flags, unref);
1077         if (lock != NULL)
1078                 GOTO(out, rc = 1);
1079         lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1080                             flags, unref);
1081         if (lock != NULL)
1082                 GOTO(out, rc = 1);
1083
1084         EXIT;
1085  out:
1086         unlock_res(res);
1087         LDLM_RESOURCE_DELREF(res);
1088         ldlm_resource_putref(res);
1089
1090         if (lock) {
1091                 ldlm_lock2handle(lock, lockh);
1092                 if ((flags & LDLM_FL_LVB_READY) &&
1093                     (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1094                         struct l_wait_info lwi;
1095                         if (lock->l_completion_ast) {
1096                                 int err = lock->l_completion_ast(lock,
1097                                                           LDLM_FL_WAIT_NOREPROC,
1098                                                                  NULL);
1099                                 if (err) {
1100                                         if (flags & LDLM_FL_TEST_LOCK)
1101                                                 LDLM_LOCK_RELEASE(lock);
1102                                         else
1103                                                 ldlm_lock_decref_internal(lock,
1104                                                                           mode);
1105                                         rc = 0;
1106                                         goto out2;
1107                                 }
1108                         }
1109
1110                         lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1111                                                NULL, LWI_ON_SIGNAL_NOOP, NULL);
1112
1113                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1114                         l_wait_event(lock->l_waitq,
1115                                      (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1116                 }
1117         }
1118  out2:
1119         if (rc) {
1120                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1121                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1122                                 res_id->name[2] : policy->l_extent.start,
1123                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1124                                 res_id->name[3] : policy->l_extent.end);
1125
1126                 /* check user's security context */
1127                 if (lock->l_conn_export &&
1128                     sptlrpc_import_check_ctx(
1129                                 class_exp2cliimp(lock->l_conn_export))) {
1130                         if (!(flags & LDLM_FL_TEST_LOCK))
1131                                 ldlm_lock_decref_internal(lock, mode);
1132                         rc = 0;
1133                 }
1134
1135                 if (flags & LDLM_FL_TEST_LOCK)
1136                         LDLM_LOCK_RELEASE(lock);
1137
1138         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1139                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1140                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1141                                   type, mode, res_id->name[0], res_id->name[1],
1142                                   (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1143                                         res_id->name[2] :policy->l_extent.start,
1144                                   (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1145                                         res_id->name[3] : policy->l_extent.end);
1146         }
1147         if (old_lock)
1148                 LDLM_LOCK_PUT(old_lock);
1149
1150         return rc ? mode : 0;
1151 }
1152
1153 /* Returns a referenced lock */
1154 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1155                                    const struct ldlm_res_id *res_id,
1156                                    ldlm_type_t type,
1157                                    ldlm_mode_t mode,
1158                                    const struct ldlm_callback_suite *cbs,
1159                                    void *data, __u32 lvb_len)
1160 {
1161         struct ldlm_lock *lock;
1162         struct ldlm_resource *res;
1163         ENTRY;
1164
1165         res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1166         if (res == NULL)
1167                 RETURN(NULL);
1168
1169         lock = ldlm_lock_new(res);
1170         ldlm_resource_putref(res);
1171
1172         if (lock == NULL)
1173                 RETURN(NULL);
1174
1175         lock->l_req_mode = mode;
1176         lock->l_ast_data = data;
1177         lock->l_pid = cfs_curproc_pid();
1178         if (cbs) {
1179                 lock->l_blocking_ast = cbs->lcs_blocking;
1180                 lock->l_completion_ast = cbs->lcs_completion;
1181                 lock->l_glimpse_ast = cbs->lcs_glimpse;
1182                 lock->l_weigh_ast = cbs->lcs_weigh;
1183         }
1184
1185         lock->l_tree_node = NULL;
1186         /* if this is the extent lock, allocate the interval tree node */
1187         if (type == LDLM_EXTENT) {
1188                 if (ldlm_interval_alloc(lock) == NULL)
1189                         GOTO(out, 0);
1190         }
1191
1192         if (lvb_len) {
1193                 lock->l_lvb_len = lvb_len;
1194                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1195                 if (lock->l_lvb_data == NULL)
1196                         GOTO(out, 0);
1197         }
1198
1199         RETURN(lock);
1200
1201 out:
1202         if (lock->l_lvb_data)
1203                 OBD_FREE(lock->l_lvb_data, lvb_len);
1204         ldlm_interval_free(ldlm_interval_detach(lock));
1205         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1206         return NULL;
1207 }
1208
1209 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1210                                struct ldlm_lock **lockp,
1211                                void *cookie, int *flags)
1212 {
1213         struct ldlm_lock *lock = *lockp;
1214         struct ldlm_resource *res = lock->l_resource;
1215         int local = ns_is_client(res->lr_namespace);
1216         ldlm_processing_policy policy;
1217         ldlm_error_t rc = ELDLM_OK;
1218         struct ldlm_interval *node = NULL;
1219         ENTRY;
1220
1221         lock->l_last_activity = cfs_time_current_sec();
1222         /* policies are not executed on the client or during replay */
1223         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1224             && !local && ns->ns_policy) {
1225                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1226                                    NULL);
1227                 if (rc == ELDLM_LOCK_REPLACED) {
1228                         /* The lock that was returned has already been granted,
1229                          * and placed into lockp.  If it's not the same as the
1230                          * one we passed in, then destroy the old one and our
1231                          * work here is done. */
1232                         if (lock != *lockp) {
1233                                 ldlm_lock_destroy(lock);
1234                                 LDLM_LOCK_RELEASE(lock);
1235                         }
1236                         *flags |= LDLM_FL_LOCK_CHANGED;
1237                         RETURN(0);
1238                 } else if (rc != ELDLM_OK ||
1239                            (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1240                         ldlm_lock_destroy(lock);
1241                         RETURN(rc);
1242                 }
1243         }
1244
1245         /* For a replaying lock, it might be already in granted list. So
1246          * unlinking the lock will cause the interval node to be freed, we
1247          * have to allocate the interval node early otherwise we can't regrant
1248          * this lock in the future. - jay */
1249         if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1250                 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1251
1252         lock_res_and_lock(lock);
1253         if (local && lock->l_req_mode == lock->l_granted_mode) {
1254                 /* The server returned a blocked lock, but it was granted
1255                  * before we got a chance to actually enqueue it.  We don't
1256                  * need to do anything else. */
1257                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1258                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1259                 GOTO(out, ELDLM_OK);
1260         }
1261
1262         ldlm_resource_unlink_lock(lock);
1263         if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1264                 if (node == NULL) {
1265                         ldlm_lock_destroy_nolock(lock);
1266                         GOTO(out, rc = -ENOMEM);
1267                 }
1268
1269                 CFS_INIT_LIST_HEAD(&node->li_group);
1270                 ldlm_interval_attach(node, lock);
1271                 node = NULL;
1272         }
1273
1274         /* Some flags from the enqueue want to make it into the AST, via the
1275          * lock's l_flags. */
1276         lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1277
1278         /* This distinction between local lock trees is very important; a client
1279          * namespace only has information about locks taken by that client, and
1280          * thus doesn't have enough information to decide for itself if it can
1281          * be granted (below).  In this case, we do exactly what the server
1282          * tells us to do, as dictated by the 'flags'.
1283          *
1284          * We do exactly the same thing during recovery, when the server is
1285          * more or less trusting the clients not to lie.
1286          *
1287          * FIXME (bug 268): Detect obvious lies by checking compatibility in
1288          * granted/converting queues. */
1289         if (local) {
1290                 if (*flags & LDLM_FL_BLOCK_CONV)
1291                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1292                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1293                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1294                 else
1295                         ldlm_grant_lock(lock, NULL);
1296                 GOTO(out, ELDLM_OK);
1297         } else if (*flags & LDLM_FL_REPLAY) {
1298                 if (*flags & LDLM_FL_BLOCK_CONV) {
1299                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1300                         GOTO(out, ELDLM_OK);
1301                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1302                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1303                         GOTO(out, ELDLM_OK);
1304                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1305                         ldlm_grant_lock(lock, NULL);
1306                         GOTO(out, ELDLM_OK);
1307                 }
1308                 /* If no flags, fall through to normal enqueue path. */
1309         }
1310
1311         policy = ldlm_processing_policy_table[res->lr_type];
1312         policy(lock, flags, 1, &rc, NULL);
1313         GOTO(out, rc);
1314 out:
1315         unlock_res_and_lock(lock);
1316         if (node)
1317                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1318         return rc;
1319 }
1320
1321 /* Must be called with namespace taken: queue is waiting or converting. */
1322 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1323                          struct list_head *work_list)
1324 {
1325         struct list_head *tmp, *pos;
1326         ldlm_processing_policy policy;
1327         int flags;
1328         int rc = LDLM_ITER_CONTINUE;
1329         ldlm_error_t err;
1330         ENTRY;
1331
1332         check_res_locked(res);
1333
1334         policy = ldlm_processing_policy_table[res->lr_type];
1335         LASSERT(policy);
1336
1337         list_for_each_safe(tmp, pos, queue) {
1338                 struct ldlm_lock *pending;
1339                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1340
1341                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1342
1343                 flags = 0;
1344                 rc = policy(pending, &flags, 0, &err, work_list);
1345                 if (rc != LDLM_ITER_CONTINUE)
1346                         break;
1347         }
1348
1349         RETURN(rc);
1350 }
1351
1352 /* Helper function for ldlm_run_ast_work().
1353  *
1354  * Send an existing rpc set specified by @arg->set and then
1355  * destroy it. Create new one if @do_create flag is set. */
1356 static void
1357 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1358 {
1359         ENTRY;
1360
1361         ptlrpc_set_wait(arg->set);
1362         if (arg->type == LDLM_BL_CALLBACK)
1363                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1364         ptlrpc_set_destroy(arg->set);
1365
1366         if (do_create)
1367                 arg->set = ptlrpc_prep_set();
1368
1369         EXIT;
1370 }
1371
1372 static int
1373 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1374 {
1375         struct ldlm_lock_desc d;
1376         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1377         ENTRY;
1378
1379         /* nobody should touch l_bl_ast */
1380         lock_res_and_lock(lock);
1381         list_del_init(&lock->l_bl_ast);
1382
1383         LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1384         LASSERT(lock->l_bl_ast_run == 0);
1385         LASSERT(lock->l_blocking_lock);
1386         lock->l_bl_ast_run++;
1387         unlock_res_and_lock(lock);
1388
1389         ldlm_lock2desc(lock->l_blocking_lock, &d);
1390
1391         lock->l_blocking_ast(lock, &d, (void *)arg,
1392                              LDLM_CB_BLOCKING);
1393         LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1394         lock->l_blocking_lock = NULL;
1395         LDLM_LOCK_RELEASE(lock);
1396
1397         RETURN(1);
1398 }
1399
1400 static int
1401 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1402 {
1403         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1404         ldlm_completion_callback completion_callback;
1405         int rc = 0;
1406         ENTRY;
1407
1408         /* It's possible to receive a completion AST before we've set
1409          * the l_completion_ast pointer: either because the AST arrived
1410          * before the reply, or simply because there's a small race
1411          * window between receiving the reply and finishing the local
1412          * enqueue. (bug 842)
1413          *
1414          * This can't happen with the blocking_ast, however, because we
1415          * will never call the local blocking_ast until we drop our
1416          * reader/writer reference, which we won't do until we get the
1417          * reply and finish enqueueing. */
1418
1419         /* nobody should touch l_cp_ast */
1420         lock_res_and_lock(lock);
1421         list_del_init(&lock->l_cp_ast);
1422         LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1423         /* save l_completion_ast since it can be changed by
1424          * mds_intent_policy(), see bug 14225 */
1425         completion_callback = lock->l_completion_ast;
1426         lock->l_flags &= ~LDLM_FL_CP_REQD;
1427         unlock_res_and_lock(lock);
1428
1429         if (completion_callback != NULL) {
1430                 completion_callback(lock, 0, (void *)arg);
1431                 rc = 1;
1432         }
1433         LDLM_LOCK_RELEASE(lock);
1434
1435         RETURN(rc);
1436 }
1437
1438 static int
1439 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1440 {
1441         struct ldlm_lock_desc desc;
1442         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
1443         ENTRY;
1444
1445         list_del_init(&lock->l_rk_ast);
1446
1447         /* the desc just pretend to exclusive */
1448         ldlm_lock2desc(lock, &desc);
1449         desc.l_req_mode = LCK_EX;
1450         desc.l_granted_mode = 0;
1451
1452         lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1453         LDLM_LOCK_RELEASE(lock);
1454
1455         RETURN(1);
1456 }
1457
1458 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1459 {
1460         struct ldlm_cb_set_arg arg;
1461         struct list_head *tmp, *pos;
1462         int (*work_ast_lock)(struct list_head *tmp,struct ldlm_cb_set_arg *arg);
1463         int ast_count;
1464         ENTRY;
1465
1466         arg.set = ptlrpc_prep_set();
1467         if (NULL == arg.set)
1468                 RETURN(-ERESTART);
1469         atomic_set(&arg.restart, 0);
1470         switch (ast_type) {
1471         case LDLM_WORK_BL_AST:
1472                 arg.type = LDLM_BL_CALLBACK;
1473                 work_ast_lock = ldlm_work_bl_ast_lock;
1474                 break;
1475         case LDLM_WORK_CP_AST:
1476                 arg.type = LDLM_CP_CALLBACK;
1477                 work_ast_lock = ldlm_work_cp_ast_lock;
1478                 break;
1479         case LDLM_WORK_REVOKE_AST:
1480                 arg.type = LDLM_BL_CALLBACK;
1481                 work_ast_lock = ldlm_work_revoke_ast_lock;
1482                 break;
1483         default:
1484                 LBUG();
1485         }
1486
1487         ast_count = 0;
1488         list_for_each_safe(tmp, pos, rpc_list) {
1489                 ast_count += work_ast_lock(tmp, &arg);
1490
1491                 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1492                  * and create a new set for requests that remained in
1493                  * @rpc_list */
1494                 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1495                         ldlm_send_and_maybe_create_set(&arg, 1);
1496                         ast_count = 0;
1497                 }
1498         }
1499
1500         if (ast_count > 0)
1501                 ldlm_send_and_maybe_create_set(&arg, 0);
1502         else
1503                 /* In case when number of ASTs is multiply of
1504                  * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1505                  * @arg.set must be destroyed here, otherwise we get
1506                  * write memory leaking. */
1507                 ptlrpc_set_destroy(arg.set);
1508
1509         RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1510 }
1511
1512 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1513 {
1514         ldlm_reprocess_all(res);
1515         return LDLM_ITER_CONTINUE;
1516 }
1517
1518 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1519 {
1520         struct list_head *tmp;
1521         int i, rc;
1522
1523         if (ns == NULL)
1524                 return;
1525
1526         ENTRY;
1527         spin_lock(&ns->ns_hash_lock);
1528         for (i = 0; i < RES_HASH_SIZE; i++) {
1529                 tmp = ns->ns_hash[i].next;
1530                 while (tmp != &(ns->ns_hash[i])) {
1531                         struct ldlm_resource *res =
1532                                 list_entry(tmp, struct ldlm_resource, lr_hash);
1533
1534                         ldlm_resource_getref(res);
1535                         spin_unlock(&ns->ns_hash_lock);
1536                         LDLM_RESOURCE_ADDREF(res);
1537
1538                         rc = reprocess_one_queue(res, NULL);
1539
1540                         LDLM_RESOURCE_DELREF(res);
1541                         spin_lock(&ns->ns_hash_lock);
1542                         tmp = tmp->next;
1543                         ldlm_resource_putref_locked(res);
1544
1545                         if (rc == LDLM_ITER_STOP)
1546                                 GOTO(out, rc);
1547                 }
1548         }
1549  out:
1550         spin_unlock(&ns->ns_hash_lock);
1551         EXIT;
1552 }
1553
1554 void ldlm_reprocess_all(struct ldlm_resource *res)
1555 {
1556         CFS_LIST_HEAD(rpc_list);
1557         int rc;
1558         ENTRY;
1559
1560         /* Local lock trees don't get reprocessed. */
1561         if (ns_is_client(res->lr_namespace)) {
1562                 EXIT;
1563                 return;
1564         }
1565
1566  restart:
1567         lock_res(res);
1568         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1569         if (rc == LDLM_ITER_CONTINUE)
1570                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1571         unlock_res(res);
1572
1573         rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1574         if (rc == -ERESTART) {
1575                 LASSERT(list_empty(&rpc_list));
1576                 goto restart;
1577         }
1578         EXIT;
1579 }
1580
1581 void ldlm_cancel_callback(struct ldlm_lock *lock)
1582 {
1583         check_res_locked(lock->l_resource);
1584         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1585                 lock->l_flags |= LDLM_FL_CANCEL;
1586                 if (lock->l_blocking_ast) {
1587                         // l_check_no_ns_lock(ns);
1588                         unlock_res_and_lock(lock);
1589                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1590                                              LDLM_CB_CANCELING);
1591                         lock_res_and_lock(lock);
1592                 } else {
1593                         LDLM_DEBUG(lock, "no blocking ast");
1594                 }
1595         }
1596         lock->l_flags |= LDLM_FL_BL_DONE;
1597 }
1598
1599 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1600 {
1601         if (req->l_resource->lr_type != LDLM_PLAIN &&
1602             req->l_resource->lr_type != LDLM_IBITS)
1603                 return;
1604
1605         list_del_init(&req->l_sl_policy);
1606         list_del_init(&req->l_sl_mode);
1607 }
1608
1609 void ldlm_lock_cancel(struct ldlm_lock *lock)
1610 {
1611         struct ldlm_resource *res;
1612         struct ldlm_namespace *ns;
1613         ENTRY;
1614
1615         lock_res_and_lock(lock);
1616
1617         res = lock->l_resource;
1618         ns = res->lr_namespace;
1619
1620         /* Please do not, no matter how tempting, remove this LBUG without
1621          * talking to me first. -phik */
1622         if (lock->l_readers || lock->l_writers) {
1623                 LDLM_ERROR(lock, "lock still has references");
1624                 LBUG();
1625         }
1626
1627         ldlm_del_waiting_lock(lock);
1628
1629         /* Releases cancel callback. */
1630         ldlm_cancel_callback(lock);
1631
1632         /* Yes, second time, just in case it was added again while we were
1633            running with no res lock in ldlm_cancel_callback */
1634         ldlm_del_waiting_lock(lock);
1635         ldlm_resource_unlink_lock(lock);
1636         ldlm_lock_destroy_nolock(lock);
1637
1638         if (lock->l_granted_mode == lock->l_req_mode)
1639                 ldlm_pool_del(&ns->ns_pool, lock);
1640
1641         /* Make sure we will not be called again for same lock what is possible
1642          * if not to zero out lock->l_granted_mode */
1643         lock->l_granted_mode = LCK_MINMODE;
1644         unlock_res_and_lock(lock);
1645
1646         EXIT;
1647 }
1648
1649 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1650 {
1651         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1652         ENTRY;
1653
1654         if (lock == NULL)
1655                 RETURN(-EINVAL);
1656
1657         lock->l_ast_data = data;
1658         LDLM_LOCK_PUT(lock);
1659         RETURN(0);
1660 }
1661
1662 void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
1663 {
1664         struct obd_export    *exp = data;
1665         struct ldlm_lock     *lock = obj;
1666         struct ldlm_resource *res;
1667
1668         res = ldlm_resource_getref(lock->l_resource);
1669         LDLM_LOCK_GET(lock);
1670
1671         LDLM_DEBUG(lock, "export %p", exp);
1672         ldlm_res_lvbo_update(res, NULL, 0, 1);
1673         ldlm_lock_cancel(lock);
1674         ldlm_reprocess_all(res);
1675         ldlm_resource_putref(res);
1676         LDLM_LOCK_RELEASE(lock);
1677 }
1678
1679 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1680 {
1681         lustre_hash_for_each_empty(exp->exp_lock_hash,
1682                                    ldlm_cancel_locks_for_export_cb, exp);
1683 }
1684
1685 /**
1686  * Downgrade an exclusive lock.
1687  *
1688  * A fast variant of ldlm_lock_convert for convertion of exclusive
1689  * locks. The convertion is always successful.
1690  *
1691  * \param lock A lock to convert
1692  * \param new_mode new lock mode
1693  */
1694 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1695 {
1696         struct ldlm_namespace *ns;
1697         ENTRY;
1698
1699         LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1700         LASSERT(new_mode == LCK_COS);
1701
1702         lock_res_and_lock(lock);
1703         ldlm_resource_unlink_lock(lock);
1704         /*
1705          * Remove the lock from pool as it will be added again in
1706          * ldlm_grant_lock() called below.
1707          */
1708         ns = lock->l_resource->lr_namespace;
1709         ldlm_pool_del(&ns->ns_pool, lock);
1710
1711         lock->l_req_mode = new_mode;
1712         ldlm_grant_lock(lock, NULL);
1713         unlock_res_and_lock(lock);
1714         ldlm_reprocess_all(lock->l_resource);
1715
1716         EXIT;
1717 }
1718
1719 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1720                                         __u32 *flags)
1721 {
1722         CFS_LIST_HEAD(rpc_list);
1723         struct ldlm_resource *res;
1724         struct ldlm_namespace *ns;
1725         int granted = 0;
1726         int old_mode, rc;
1727         struct sl_insert_point prev;
1728         ldlm_error_t err;
1729         struct ldlm_interval *node;
1730         ENTRY;
1731
1732         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1733                 *flags |= LDLM_FL_BLOCK_GRANTED;
1734                 RETURN(lock->l_resource);
1735         }
1736
1737         /* I can't check the type of lock here because the bitlock of lock
1738          * is not held here, so do the allocation blindly. -jay */
1739         OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1740         if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
1741                 RETURN(NULL);
1742
1743         LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1744                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1745
1746         lock_res_and_lock(lock);
1747
1748         res = lock->l_resource;
1749         ns = res->lr_namespace;
1750
1751         old_mode = lock->l_req_mode;
1752         lock->l_req_mode = new_mode;
1753         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1754                 /* remember the lock position where the lock might be
1755                  * added back to the granted list later and also
1756                  * remember the join mode for skiplist fixing. */
1757                 prev.res_link = lock->l_res_link.prev;
1758                 prev.mode_link = lock->l_sl_mode.prev;
1759                 prev.policy_link = lock->l_sl_policy.prev;
1760                 ldlm_resource_unlink_lock(lock);
1761         } else {
1762                 ldlm_resource_unlink_lock(lock);
1763                 if (res->lr_type == LDLM_EXTENT) {
1764                         /* FIXME: ugly code, I have to attach the lock to a
1765                          * interval node again since perhaps it will be granted
1766                          * soon */
1767                         CFS_INIT_LIST_HEAD(&node->li_group);
1768                         ldlm_interval_attach(node, lock);
1769                         node = NULL;
1770                 }
1771         }
1772
1773         /*
1774          * Remove old lock from the pool before adding the lock with new
1775          * mode below in ->policy()
1776          */
1777         ldlm_pool_del(&ns->ns_pool, lock);
1778
1779         /* If this is a local resource, put it on the appropriate list. */
1780         if (ns_is_client(res->lr_namespace)) {
1781                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1782                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1783                 } else {
1784                         /* This should never happen, because of the way the
1785                          * server handles conversions. */
1786                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1787                                    *flags);
1788                         LBUG();
1789
1790                         ldlm_grant_lock(lock, &rpc_list);
1791                         granted = 1;
1792                         /* FIXME: completion handling not with ns_lock held ! */
1793                         if (lock->l_completion_ast)
1794                                 lock->l_completion_ast(lock, 0, NULL);
1795                 }
1796         } else {
1797                 int pflags = 0;
1798                 ldlm_processing_policy policy;
1799                 policy = ldlm_processing_policy_table[res->lr_type];
1800                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1801                 if (rc == LDLM_ITER_STOP) {
1802                         lock->l_req_mode = old_mode;
1803                         if (res->lr_type == LDLM_EXTENT)
1804                                 ldlm_extent_add_lock(res, lock);
1805                         else
1806                                 ldlm_granted_list_add_lock(lock, &prev);
1807
1808                         res = NULL;
1809                 } else {
1810                         *flags |= LDLM_FL_BLOCK_GRANTED;
1811                         granted = 1;
1812                 }
1813         }
1814         unlock_res_and_lock(lock);
1815
1816         if (granted)
1817                 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1818         if (node)
1819                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1820         RETURN(res);
1821 }
1822
1823 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1824 {
1825         struct obd_device *obd = NULL;
1826
1827         if (!((libcfs_debug | D_ERROR) & level))
1828                 return;
1829
1830         if (!lock) {
1831                 CDEBUG(level, "  NULL LDLM lock\n");
1832                 return;
1833         }
1834
1835         CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1836                lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1837                pos, lock->l_pid);
1838         if (lock->l_conn_export != NULL)
1839                 obd = lock->l_conn_export->exp_obd;
1840         if (lock->l_export && lock->l_export->exp_connection) {
1841                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1842                      libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1843                      lock->l_remote_handle.cookie);
1844         } else if (obd == NULL) {
1845                 CDEBUG(level, "  Node: local\n");
1846         } else {
1847                 struct obd_import *imp = obd->u.cli.cl_import;
1848                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1849                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1850                        lock->l_remote_handle.cookie);
1851         }
1852         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1853                   lock->l_resource,
1854                   lock->l_resource->lr_name.name[0],
1855                   lock->l_resource->lr_name.name[1],
1856                   lock->l_resource->lr_name.name[2]);
1857         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1858                "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1859                ldlm_lockname[lock->l_granted_mode],
1860                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1861                lock->l_flags);
1862         if (lock->l_resource->lr_type == LDLM_EXTENT)
1863                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1864                        " (req "LPU64"-"LPU64")\n",
1865                        lock->l_policy_data.l_extent.start,
1866                        lock->l_policy_data.l_extent.end,
1867                        lock->l_req_extent.start, lock->l_req_extent.end);
1868         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1869                 CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
1870                        lock->l_policy_data.l_flock.pid,
1871                        lock->l_policy_data.l_flock.start,
1872                        lock->l_policy_data.l_flock.end);
1873        else if (lock->l_resource->lr_type == LDLM_IBITS)
1874                 CDEBUG(level, "  Bits: "LPX64"\n",
1875                        lock->l_policy_data.l_inodebits.bits);
1876 }
1877
1878 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1879 {
1880         struct ldlm_lock *lock;
1881
1882         if (!((libcfs_debug | D_ERROR) & level))
1883                 return;
1884
1885         lock = ldlm_handle2lock(lockh);
1886         if (lock == NULL)
1887                 return;
1888
1889         ldlm_lock_dump(D_OTHER, lock, 0);
1890
1891         LDLM_LOCK_PUT(lock);
1892 }
1893
1894 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1895                       struct libcfs_debug_msg_data *data, const char *fmt,
1896                       ...)
1897 {
1898         va_list args;
1899         cfs_debug_limit_state_t *cdls = data->msg_cdls;
1900
1901         va_start(args, fmt);
1902
1903         if (lock->l_resource == NULL) {
1904                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1905                                    data->msg_fn, data->msg_line, fmt, args,
1906                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1907                        "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1908                        LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1909                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1910                        lock->l_readers, lock->l_writers,
1911                        ldlm_lockname[lock->l_granted_mode],
1912                        ldlm_lockname[lock->l_req_mode],
1913                        lock->l_flags, lock->l_remote_handle.cookie,
1914                        lock->l_export ?
1915                        atomic_read(&lock->l_export->exp_refcount) : -99,
1916                        lock->l_pid, lock->l_callback_timeout);
1917                 va_end(args);
1918                 return;
1919         }
1920
1921         switch (lock->l_resource->lr_type) {
1922         case LDLM_EXTENT:
1923                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1924                                    data->msg_fn, data->msg_line, fmt, args,
1925                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1926                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1927                        "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1928                        " expref: %d pid: %u timeout %lu\n",
1929                        lock->l_resource->lr_namespace->ns_name, lock,
1930                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1931                        lock->l_readers, lock->l_writers,
1932                        ldlm_lockname[lock->l_granted_mode],
1933                        ldlm_lockname[lock->l_req_mode],
1934                        lock->l_resource->lr_name.name[0],
1935                        lock->l_resource->lr_name.name[1],
1936                        atomic_read(&lock->l_resource->lr_refcount),
1937                        ldlm_typename[lock->l_resource->lr_type],
1938                        lock->l_policy_data.l_extent.start,
1939                        lock->l_policy_data.l_extent.end,
1940                        lock->l_req_extent.start, lock->l_req_extent.end,
1941                        lock->l_flags, lock->l_remote_handle.cookie,
1942                        lock->l_export ?
1943                        atomic_read(&lock->l_export->exp_refcount) : -99,
1944                        lock->l_pid, lock->l_callback_timeout);
1945                 break;
1946
1947         case LDLM_FLOCK:
1948                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1949                                    data->msg_fn, data->msg_line, fmt, args,
1950                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1951                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1952                        "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1953                        " expref: %d pid: %u timeout: %lu\n",
1954                        lock->l_resource->lr_namespace->ns_name, lock,
1955                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1956                        lock->l_readers, lock->l_writers,
1957                        ldlm_lockname[lock->l_granted_mode],
1958                        ldlm_lockname[lock->l_req_mode],
1959                        lock->l_resource->lr_name.name[0],
1960                        lock->l_resource->lr_name.name[1],
1961                        atomic_read(&lock->l_resource->lr_refcount),
1962                        ldlm_typename[lock->l_resource->lr_type],
1963                        lock->l_policy_data.l_flock.pid,
1964                        lock->l_policy_data.l_flock.start,
1965                        lock->l_policy_data.l_flock.end,
1966                        lock->l_flags, lock->l_remote_handle.cookie,
1967                        lock->l_export ?
1968                        atomic_read(&lock->l_export->exp_refcount) : -99,
1969                        lock->l_pid, lock->l_callback_timeout);
1970                 break;
1971
1972         case LDLM_IBITS:
1973                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1974                                    data->msg_fn, data->msg_line, fmt, args,
1975                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1976                        "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1977                        "flags: "LPX64" remote: "LPX64" expref: %d "
1978                        "pid: %u timeout: %lu\n",
1979                        lock->l_resource->lr_namespace->ns_name,
1980                        lock, lock->l_handle.h_cookie,
1981                        atomic_read (&lock->l_refc),
1982                        lock->l_readers, lock->l_writers,
1983                        ldlm_lockname[lock->l_granted_mode],
1984                        ldlm_lockname[lock->l_req_mode],
1985                        lock->l_resource->lr_name.name[0],
1986                        lock->l_resource->lr_name.name[1],
1987                        lock->l_policy_data.l_inodebits.bits,
1988                        atomic_read(&lock->l_resource->lr_refcount),
1989                        ldlm_typename[lock->l_resource->lr_type],
1990                        lock->l_flags, lock->l_remote_handle.cookie,
1991                        lock->l_export ?
1992                        atomic_read(&lock->l_export->exp_refcount) : -99,
1993                        lock->l_pid, lock->l_callback_timeout);
1994                 break;
1995
1996         default:
1997                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1998                                    data->msg_fn, data->msg_line, fmt, args,
1999                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2000                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2001                        "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2002                        lock->l_resource->lr_namespace->ns_name,
2003                        lock, lock->l_handle.h_cookie,
2004                        atomic_read (&lock->l_refc),
2005                        lock->l_readers, lock->l_writers,
2006                        ldlm_lockname[lock->l_granted_mode],
2007                        ldlm_lockname[lock->l_req_mode],
2008                        lock->l_resource->lr_name.name[0],
2009                        lock->l_resource->lr_name.name[1],
2010                        atomic_read(&lock->l_resource->lr_refcount),
2011                        ldlm_typename[lock->l_resource->lr_type],
2012                        lock->l_flags, lock->l_remote_handle.cookie,
2013                        lock->l_export ?
2014                        atomic_read(&lock->l_export->exp_refcount) : -99,
2015                        lock->l_pid, lock->l_callback_timeout);
2016                 break;
2017         }
2018         va_end(args);
2019 }
2020 EXPORT_SYMBOL(_ldlm_lock_debug);