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