Whamcloud - gitweb
b=21452 kABI tracking
[fs/lustre-release.git] / lustre / ldlm / ldlm_plain.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 (c) 2003, 2010, Oracle and/or its affiliates. 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_plain.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 <lustre_dlm.h>
46 #include <obd_support.h>
47 #include <lustre_lib.h>
48 #else
49 #include <liblustre.h>
50 #endif
51
52 #include "ldlm_internal.h"
53
54 static inline int
55 ldlm_plain_compat_queue(cfs_list_t *queue, struct ldlm_lock *req,
56                         cfs_list_t *work_list)
57 {
58         cfs_list_t *tmp;
59         struct ldlm_lock *lock;
60         ldlm_mode_t req_mode = req->l_req_mode;
61         int compat = 1;
62         ENTRY;
63
64         lockmode_verify(req_mode);
65
66         cfs_list_for_each(tmp, queue) {
67                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
68
69                 if (req == lock)
70                         RETURN(compat);
71
72                  /* last lock in mode group */
73                  tmp = &cfs_list_entry(lock->l_sl_mode.prev,
74                                        struct ldlm_lock,
75                                        l_sl_mode)->l_res_link;
76
77                  if (lockmode_compat(lock->l_req_mode, req_mode))
78                         continue;
79
80                 if (!work_list)
81                         RETURN(0);
82
83                 compat = 0;
84
85                 /* add locks of the mode group to @work_list as
86                  * blocking locks for @req */
87                 if (lock->l_blocking_ast)
88                         ldlm_add_ast_work_item(lock, req, work_list);
89
90                 {
91                         cfs_list_t *head;
92
93                         head = &lock->l_sl_mode;
94                         cfs_list_for_each_entry(lock, head, l_sl_mode)
95                                 if (lock->l_blocking_ast)
96                                         ldlm_add_ast_work_item(lock, req,
97                                                                work_list);
98                 }
99         }
100
101         RETURN(compat);
102 }
103
104 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
105  *   - blocking ASTs have already been sent
106  *   - must call this function with the resource lock held
107  *
108  * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
109  *   - blocking ASTs have not been sent
110  *   - must call this function with the resource lock held */
111 int ldlm_process_plain_lock(struct ldlm_lock *lock, int *flags, int first_enq,
112                             ldlm_error_t *err, cfs_list_t *work_list)
113 {
114         struct ldlm_resource *res = lock->l_resource;
115         CFS_LIST_HEAD(rpc_list);
116         int rc;
117         ENTRY;
118
119         check_res_locked(res);
120         LASSERT(cfs_list_empty(&res->lr_converting));
121
122         if (!first_enq) {
123                 LASSERT(work_list != NULL);
124                 rc = ldlm_plain_compat_queue(&res->lr_granted, lock, NULL);
125                 if (!rc)
126                         RETURN(LDLM_ITER_STOP);
127                 rc = ldlm_plain_compat_queue(&res->lr_waiting, lock, NULL);
128                 if (!rc)
129                         RETURN(LDLM_ITER_STOP);
130
131                 ldlm_resource_unlink_lock(lock);
132                 ldlm_grant_lock(lock, work_list);
133                 RETURN(LDLM_ITER_CONTINUE);
134         }
135
136  restart:
137         rc = ldlm_plain_compat_queue(&res->lr_granted, lock, &rpc_list);
138         rc += ldlm_plain_compat_queue(&res->lr_waiting, lock, &rpc_list);
139
140         if (rc != 2) {
141                 /* If either of the compat_queue()s returned 0, then we
142                  * have ASTs to send and must go onto the waiting list.
143                  *
144                  * bug 2322: we used to unlink and re-add here, which was a
145                  * terrible folly -- if we goto restart, we could get
146                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
147                 if (cfs_list_empty(&lock->l_res_link))
148                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
149                 unlock_res(res);
150                 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_BL_AST);
151                 lock_res(res);
152                 if (rc == -ERESTART)
153                         GOTO(restart, -ERESTART);
154                 *flags |= LDLM_FL_BLOCK_GRANTED;
155         } else {
156                 ldlm_resource_unlink_lock(lock);
157                 ldlm_grant_lock(lock, NULL);
158         }
159         RETURN(0);
160 }