Whamcloud - gitweb
b=15346
[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  *  Copyright (c) 2002, 2003, 2004 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25 #ifndef __KERNEL__
26 # include <liblustre.h>
27 #endif
28
29 #include <lustre_dlm.h>
30 #include <obd_support.h>
31 #include <lustre_lib.h>
32
33 #include "ldlm_internal.h"
34
35 /* Determine if the lock is compatible with all locks on the queue. */
36 static int
37 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
38                             struct list_head *work_list)
39 {
40         struct list_head *tmp;
41         struct ldlm_lock *lock;
42         ldlm_mode_t req_mode = req->l_req_mode;
43         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
44         int compat = 1;
45         ENTRY;
46
47         LASSERT(req_bits); /* There is no sense in lock with no bits set,
48                               I think. Also such a lock would be compatible
49                                with any other bit lock */
50
51         list_for_each(tmp, queue) {
52                 struct list_head *mode_tail;
53
54                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
55
56                 if (req == lock)
57                         RETURN(compat);
58
59                 /* last lock in mode group */
60                 LASSERT(lock->l_sl_mode.prev != NULL);
61                 mode_tail = &list_entry(lock->l_sl_mode.prev,
62                                         struct ldlm_lock,
63                                         l_sl_mode)->l_res_link;
64
65                 /* locks are compatible, bits don't matter */
66                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
67                         /* jump to last lock in mode group */
68                         tmp = mode_tail;
69                         continue;
70                 }
71                 
72                 for (;;) {
73                         struct list_head *head;
74
75                         /* last lock in policy group */
76                         tmp = &list_entry(lock->l_sl_policy.prev,
77                                           struct ldlm_lock,
78                                           l_sl_policy)->l_res_link;
79
80                         /* locks with bits overlapped are conflicting locks */
81                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
82                                 /* conflicting policy */
83                                 if (!work_list)
84                                         RETURN(0);
85
86                                 compat = 0;
87
88                                 /* add locks of the policy group to
89                                  * @work_list as blocking locks for
90                                  * @req */
91                                 if (lock->l_blocking_ast)
92                                         ldlm_add_ast_work_item(lock, req,
93                                                                work_list);
94                                 head = &lock->l_sl_policy;
95                                 list_for_each_entry(lock, head, l_sl_policy)
96                                         if (lock->l_blocking_ast)
97                                                 ldlm_add_ast_work_item(lock, req,
98                                                                        work_list);
99                         }
100                         if (tmp == mode_tail)
101                                 break;
102
103                         tmp = tmp->next;
104                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
105                 } /* loop over policy groups within one mode group */
106         } /* loop over mode groups within @queue */
107
108         RETURN(compat);
109 }
110
111 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
112   *   - blocking ASTs have already been sent
113   *   - must call this function with the ns lock held
114   *
115   * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
116   *   - blocking ASTs have not been sent
117   *   - must call this function with the ns lock held once */
118 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, int *flags,
119                                 int first_enq, ldlm_error_t *err,
120                                 struct list_head *work_list)
121 {
122         struct ldlm_resource *res = lock->l_resource;
123         CFS_LIST_HEAD(rpc_list);
124         int rc;
125         ENTRY;
126
127         LASSERT(list_empty(&res->lr_converting));
128         check_res_locked(res);
129
130         if (!first_enq) {
131                 LASSERT(work_list != NULL);
132                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
133                 if (!rc)
134                         RETURN(LDLM_ITER_STOP);
135                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
136                 if (!rc)
137                         RETURN(LDLM_ITER_STOP);
138
139                 ldlm_resource_unlink_lock(lock);
140                 ldlm_grant_lock(lock, work_list);
141                 RETURN(LDLM_ITER_CONTINUE);
142         }
143
144  restart:
145         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
146         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
147
148         if (rc != 2) {
149                 /* If either of the compat_queue()s returned 0, then we
150                  * have ASTs to send and must go onto the waiting list.
151                  *
152                  * bug 2322: we used to unlink and re-add here, which was a
153                  * terrible folly -- if we goto restart, we could get
154                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
155                 if (list_empty(&lock->l_res_link))
156                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
157                 unlock_res(res);
158                 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_BL_AST);
159                 lock_res(res);
160                 if (rc == -ERESTART)
161                         GOTO(restart, -ERESTART);
162                 *flags |= LDLM_FL_BLOCK_GRANTED;
163         } else {
164                 ldlm_resource_unlink_lock(lock);
165                 ldlm_grant_lock(lock, NULL);
166         }
167         RETURN(0);
168 }