Whamcloud - gitweb
be9a073ab48fdb0230fed71e5f9739eab8a0bbff
[fs/lustre-release.git] / lustre / ldlm / ldlm_inodebits.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_inodebits.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 #ifndef __KERNEL__
44 # include <liblustre.h>
45 #endif
46
47 #include <lustre_dlm.h>
48 #include <obd_support.h>
49 #include <lustre_lib.h>
50
51 #include "ldlm_internal.h"
52
53 /* Determine if the lock is compatible with all locks on the queue. */
54 static int
55 ldlm_inodebits_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         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
62         int compat = 1;
63         ENTRY;
64
65         LASSERT(req_bits); /* There is no sense in lock with no bits set,
66                               I think. Also such a lock would be compatible
67                                with any other bit lock */
68
69         cfs_list_for_each(tmp, queue) {
70                 cfs_list_t *mode_tail;
71
72                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
73
74                 if (req == lock)
75                         RETURN(compat);
76
77                 /* last lock in mode group */
78                 LASSERT(lock->l_sl_mode.prev != NULL);
79                 mode_tail = &cfs_list_entry(lock->l_sl_mode.prev,
80                                             struct ldlm_lock,
81                                             l_sl_mode)->l_res_link;
82
83                 /* locks are compatible, bits don't matter */
84                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
85                         /* jump to last lock in mode group */
86                         tmp = mode_tail;
87                         continue;
88                 }
89
90                 if (lock->l_req_mode == LCK_COS) {
91                         if (lock->l_client_cookie == req->l_client_cookie) {
92                                 tmp = mode_tail;
93                         } else {
94                                 tmp = mode_tail;
95                                 if (!work_list)
96                                         RETURN(0);
97                                 compat = 0;
98                                 if (lock->l_blocking_ast)
99                                         ldlm_add_ast_work_item(lock, req,
100                                                                work_list);
101                         }
102                         continue;
103                 }
104
105                 for (;;) {
106                         cfs_list_t *head;
107
108                         /* last lock in policy group */
109                         tmp = &cfs_list_entry(lock->l_sl_policy.prev,
110                                               struct ldlm_lock,
111                                               l_sl_policy)->l_res_link;
112
113                         /* locks with bits overlapped are conflicting locks */
114                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
115                                 /* conflicting policy */
116                                 if (!work_list)
117                                         RETURN(0);
118
119                                 compat = 0;
120
121                                 /* add locks of the policy group to
122                                  * @work_list as blocking locks for
123                                  * @req */
124                                 if (lock->l_blocking_ast)
125                                         ldlm_add_ast_work_item(lock, req,
126                                                                work_list);
127                                 head = &lock->l_sl_policy;
128                                 cfs_list_for_each_entry(lock, head, l_sl_policy)
129                                         if (lock->l_blocking_ast)
130                                                 ldlm_add_ast_work_item(lock, req,
131                                                                        work_list);
132                         }
133                         if (tmp == mode_tail)
134                                 break;
135
136                         tmp = tmp->next;
137                         lock = cfs_list_entry(tmp, struct ldlm_lock,
138                                               l_res_link);
139                 } /* loop over policy groups within one mode group */
140         } /* loop over mode groups within @queue */
141
142         RETURN(compat);
143 }
144
145 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
146   *   - blocking ASTs have already been sent
147   *   - must call this function with the ns lock held
148   *
149   * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
150   *   - blocking ASTs have not been sent
151   *   - must call this function with the ns lock held once */
152 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, int *flags,
153                                 int first_enq, ldlm_error_t *err,
154                                 cfs_list_t *work_list)
155 {
156         struct ldlm_resource *res = lock->l_resource;
157         CFS_LIST_HEAD(rpc_list);
158         int rc;
159         ENTRY;
160
161         LASSERT(cfs_list_empty(&res->lr_converting));
162         check_res_locked(res);
163
164         if (!first_enq) {
165                 LASSERT(work_list != NULL);
166                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
167                 if (!rc)
168                         RETURN(LDLM_ITER_STOP);
169                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
170                 if (!rc)
171                         RETURN(LDLM_ITER_STOP);
172
173                 ldlm_resource_unlink_lock(lock);
174                 ldlm_grant_lock(lock, work_list);
175                 RETURN(LDLM_ITER_CONTINUE);
176         }
177
178  restart:
179         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
180         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
181
182         if (rc != 2) {
183                 /* If either of the compat_queue()s returned 0, then we
184                  * have ASTs to send and must go onto the waiting list.
185                  *
186                  * bug 2322: we used to unlink and re-add here, which was a
187                  * terrible folly -- if we goto restart, we could get
188                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
189                 if (cfs_list_empty(&lock->l_res_link))
190                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
191                 unlock_res(res);
192                 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_BL_AST);
193                 lock_res(res);
194                 if (rc == -ERESTART)
195                         GOTO(restart, -ERESTART);
196                 *flags |= LDLM_FL_BLOCK_GRANTED;
197         } else {
198                 ldlm_resource_unlink_lock(lock);
199                 ldlm_grant_lock(lock, NULL);
200         }
201         RETURN(0);
202 }