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