Whamcloud - gitweb
LU-1302 llog: introduce llog_open
[fs/lustre-release.git] / lustre / ldlm / ldlm_inodebits.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #ifdef HAVE_SERVER_SUPPORT
54 /* Determine if the lock is compatible with all locks on the queue. */
55 static int
56 ldlm_inodebits_compat_queue(cfs_list_t *queue, struct ldlm_lock *req,
57                             cfs_list_t *work_list)
58 {
59         cfs_list_t *tmp;
60         struct ldlm_lock *lock;
61         ldlm_mode_t req_mode = req->l_req_mode;
62         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
63         int compat = 1;
64         ENTRY;
65
66         LASSERT(req_bits); /* There is no sense in lock with no bits set,
67                               I think. Also such a lock would be compatible
68                                with any other bit lock */
69
70         cfs_list_for_each(tmp, queue) {
71                 cfs_list_t *mode_tail;
72
73                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
74
75                 if (req == lock)
76                         RETURN(compat);
77
78                 /* last lock in mode group */
79                 LASSERT(lock->l_sl_mode.prev != NULL);
80                 mode_tail = &cfs_list_entry(lock->l_sl_mode.prev,
81                                             struct ldlm_lock,
82                                             l_sl_mode)->l_res_link;
83
84                 /* locks are compatible, bits don't matter */
85                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
86                         /* jump to last lock in mode group */
87                         tmp = mode_tail;
88                         continue;
89                 }
90
91                 for (;;) {
92                         cfs_list_t *head;
93
94                         /* last lock in policy group */
95                         tmp = &cfs_list_entry(lock->l_sl_policy.prev,
96                                               struct ldlm_lock,
97                                               l_sl_policy)->l_res_link;
98
99                         /* locks with bits overlapped are conflicting locks */
100                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
101                                 /* COS lock from the same client is
102                                    not conflicting */
103                                 if (lock->l_req_mode == LCK_COS &&
104                                     lock->l_client_cookie == req->l_client_cookie)
105                                         goto not_conflicting;
106                                 /* conflicting policy */
107                                 if (!work_list)
108                                         RETURN(0);
109
110                                 compat = 0;
111
112                                 /* add locks of the policy group to
113                                  * @work_list as blocking locks for
114                                  * @req */
115                                 if (lock->l_blocking_ast)
116                                         ldlm_add_ast_work_item(lock, req,
117                                                                work_list);
118                                 head = &lock->l_sl_policy;
119                                 cfs_list_for_each_entry(lock, head, l_sl_policy)
120                                         if (lock->l_blocking_ast)
121                                                 ldlm_add_ast_work_item(lock, req,
122                                                                        work_list);
123                         }
124                 not_conflicting:
125                         if (tmp == mode_tail)
126                                 break;
127
128                         tmp = tmp->next;
129                         lock = cfs_list_entry(tmp, struct ldlm_lock,
130                                               l_res_link);
131                 } /* loop over policy groups within one mode group */
132         } /* loop over mode groups within @queue */
133
134         RETURN(compat);
135 }
136
137 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
138   *   - blocking ASTs have already been sent
139   *   - must call this function with the ns lock held
140   *
141   * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
142   *   - blocking ASTs have not been sent
143   *   - must call this function with the ns lock held once */
144 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, int *flags,
145                                 int first_enq, ldlm_error_t *err,
146                                 cfs_list_t *work_list)
147 {
148         struct ldlm_resource *res = lock->l_resource;
149         CFS_LIST_HEAD(rpc_list);
150         int rc;
151         ENTRY;
152
153         LASSERT(cfs_list_empty(&res->lr_converting));
154         check_res_locked(res);
155
156         if (!first_enq) {
157                 LASSERT(work_list != NULL);
158                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
159                 if (!rc)
160                         RETURN(LDLM_ITER_STOP);
161                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
162                 if (!rc)
163                         RETURN(LDLM_ITER_STOP);
164
165                 ldlm_resource_unlink_lock(lock);
166                 ldlm_grant_lock(lock, work_list);
167                 RETURN(LDLM_ITER_CONTINUE);
168         }
169
170  restart:
171         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
172         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
173
174         if (rc != 2) {
175                 /* If either of the compat_queue()s returned 0, then we
176                  * have ASTs to send and must go onto the waiting list.
177                  *
178                  * bug 2322: we used to unlink and re-add here, which was a
179                  * terrible folly -- if we goto restart, we could get
180                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
181                 if (cfs_list_empty(&lock->l_res_link))
182                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
183                 unlock_res(res);
184                 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
185                                        LDLM_WORK_BL_AST);
186                 lock_res(res);
187                 if (rc == -ERESTART)
188                         GOTO(restart, -ERESTART);
189                 *flags |= LDLM_FL_BLOCK_GRANTED;
190         } else {
191                 ldlm_resource_unlink_lock(lock);
192                 ldlm_grant_lock(lock, NULL);
193         }
194         RETURN(0);
195 }
196 #endif /* HAVE_SERVER_SUPPORT */
197
198 void ldlm_ibits_policy_wire_to_local(const ldlm_wire_policy_data_t *wpolicy,
199                                      ldlm_policy_data_t *lpolicy)
200 {
201         memset(lpolicy, 0, sizeof(*lpolicy));
202         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
203 }
204
205 void ldlm_ibits_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
206                                      ldlm_wire_policy_data_t *wpolicy)
207 {
208         memset(wpolicy, 0, sizeof(*wpolicy));
209         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
210 }