Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ldlm/ldlm_inodebits.c
33  *
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  */
37
38 /**
39  * This file contains implementation of IBITS lock type
40  *
41  * IBITS lock type contains a bit mask determining various properties of an
42  * object. The meanings of specific bits are specific to the caller and are
43  * opaque to LDLM code.
44  *
45  * Locks with intersecting bitmasks and conflicting lock modes (e.g.  LCK_PW)
46  * are considered conflicting.  See the lock mode compatibility matrix
47  * in lustre_dlm.h.
48  */
49
50 #define DEBUG_SUBSYSTEM S_LDLM
51
52 #include <lustre_dlm.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <obd_class.h>
56
57 #include "ldlm_internal.h"
58
59 #ifdef HAVE_SERVER_SUPPORT
60 /**
61  * Determine if the lock is compatible with all locks on the queue.
62  *
63  * If \a work_list is provided, conflicting locks are linked there.
64  * If \a work_list is not provided, we exit this function on first conflict.
65  *
66  * \retval 0 if there are conflicting locks in the \a queue
67  * \retval 1 if the lock is compatible to all locks in \a queue
68  *
69  * IBITS locks in granted queue are organized in bunches of
70  * same-mode/same-bits locks called "skip lists". The First lock in the
71  * bunch contains a pointer to the end of the bunch.  This allows us to
72  * skip an entire bunch when iterating the list in search for conflicting
73  * locks if first lock of the bunch is not conflicting with us.
74  */
75 static int
76 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
77                             struct list_head *work_list)
78 {
79         struct list_head *tmp;
80         struct ldlm_lock *lock;
81         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
82         int compat = 1;
83         ENTRY;
84
85         /* There is no sense in lock with no bits set, I think.
86          * Also, such a lock would be compatible with any other bit lock */
87         LASSERT(req_bits != 0);
88
89         list_for_each(tmp, queue) {
90                 struct list_head *mode_tail;
91
92                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
93
94                 /* We stop walking the queue if we hit ourselves so we don't
95                  * take conflicting locks enqueued after us into account,
96                  * or we'd wait forever. */
97                 if (req == lock)
98                         RETURN(compat);
99
100                 /* last lock in mode group */
101                 LASSERT(lock->l_sl_mode.prev != NULL);
102                 mode_tail = &list_entry(lock->l_sl_mode.prev,
103                                         struct ldlm_lock,
104                                         l_sl_mode)->l_res_link;
105
106                 /* if reqest lock is not COS_INCOMPAT and COS is disabled,
107                  * they are compatible, IOW this request is from a local
108                  * transaction on a DNE system. */
109                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
110                     !ldlm_is_cos_enabled(req)) {
111                         /* jump to last lock in mode group */
112                         tmp = mode_tail;
113                         continue;
114                 }
115
116                 /* locks' mode are compatible, bits don't matter */
117                 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
118                         /* jump to last lock in mode group */
119                         tmp = mode_tail;
120                         continue;
121                 }
122
123                 for (;;) {
124                         struct list_head *head;
125
126                         /* Advance loop cursor to last lock in policy group. */
127                         tmp = &list_entry(lock->l_sl_policy.prev,
128                                               struct ldlm_lock,
129                                               l_sl_policy)->l_res_link;
130
131                         /* Locks with overlapping bits conflict. */
132                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
133                                 /* COS lock mode has a special compatibility
134                                  * requirement: it is only compatible with
135                                  * locks from the same client. */
136                                 if (lock->l_req_mode == LCK_COS &&
137                                     !ldlm_is_cos_incompat(req) &&
138                                     ldlm_is_cos_enabled(req) &&
139                                     lock->l_client_cookie == req->l_client_cookie)
140                                         goto not_conflicting;
141                                 /* Found a conflicting policy group. */
142                                 if (!work_list)
143                                         RETURN(0);
144
145                                 compat = 0;
146
147                                 /* Add locks of the policy group to @work_list
148                                  * as blocking locks for @req */
149                                 if (lock->l_blocking_ast)
150                                         ldlm_add_ast_work_item(lock, req,
151                                                                work_list);
152                                 head = &lock->l_sl_policy;
153                                 list_for_each_entry(lock, head, l_sl_policy)
154                                         if (lock->l_blocking_ast)
155                                                 ldlm_add_ast_work_item(lock, req,
156                                                                        work_list);
157                         }
158                 not_conflicting:
159                         if (tmp == mode_tail)
160                                 break;
161
162                         tmp = tmp->next;
163                         lock = list_entry(tmp, struct ldlm_lock,
164                                               l_res_link);
165                 } /* Loop over policy groups within one mode group. */
166         } /* Loop over mode groups within @queue. */
167
168         RETURN(compat);
169 }
170
171 /**
172  * Process a granting attempt for IBITS lock.
173  * Must be called with ns lock held
174  *
175  * This function looks for any conflicts for \a lock in the granted or
176  * waiting queues. The lock is granted if no conflicts are found in
177  * either queue.
178  *
179  * If \a first_enq is 0 (ie, called from ldlm_reprocess_queue):
180  *   - blocking ASTs have already been sent
181  *
182  * If \a first_enq is 1 (ie, called from ldlm_lock_enqueue):
183  *   - blocking ASTs have not been sent yet, so list of conflicting locks
184  *     would be collected and ASTs sent.
185  */
186 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
187                                 int first_enq, enum ldlm_error *err,
188                                 struct list_head *work_list)
189 {
190         struct ldlm_resource *res = lock->l_resource;
191         struct list_head rpc_list;
192         int rc;
193         ENTRY;
194
195         LASSERT(lock->l_granted_mode != lock->l_req_mode);
196         LASSERT(list_empty(&res->lr_converting));
197         INIT_LIST_HEAD(&rpc_list);
198         check_res_locked(res);
199
200         /* (*flags & LDLM_FL_BLOCK_NOWAIT) is for layout lock right now. */
201         if (!first_enq || (*flags & LDLM_FL_BLOCK_NOWAIT)) {
202                 *err = ELDLM_LOCK_ABORTED;
203                 if (*flags & LDLM_FL_BLOCK_NOWAIT)
204                         *err = ELDLM_LOCK_WOULDBLOCK;
205
206                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
207                 if (!rc)
208                         RETURN(LDLM_ITER_STOP);
209                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
210                 if (!rc)
211                         RETURN(LDLM_ITER_STOP);
212
213                 ldlm_resource_unlink_lock(lock);
214                 ldlm_grant_lock(lock, work_list);
215
216                 *err = ELDLM_OK;
217                 RETURN(LDLM_ITER_CONTINUE);
218         }
219
220  restart:
221         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
222         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
223
224         if (rc != 2) {
225                 /* If either of the compat_queue()s returned 0, then we
226                  * have ASTs to send and must go onto the waiting list.
227                  *
228                  * bug 2322: we used to unlink and re-add here, which was a
229                  * terrible folly -- if we goto restart, we could get
230                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
231                 if (list_empty(&lock->l_res_link))
232                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
233                 unlock_res(res);
234                 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
235                                        LDLM_WORK_BL_AST);
236                 lock_res(res);
237                 if (rc == -ERESTART)
238                         GOTO(restart, rc);
239                 *flags |= LDLM_FL_BLOCK_GRANTED;
240         } else {
241                 ldlm_resource_unlink_lock(lock);
242                 ldlm_grant_lock(lock, NULL);
243         }
244         RETURN(0);
245 }
246 #endif /* HAVE_SERVER_SUPPORT */
247
248 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
249                                      union ldlm_policy_data *lpolicy)
250 {
251         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
252 }
253
254 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
255                                      union ldlm_wire_policy_data *wpolicy)
256 {
257         memset(wpolicy, 0, sizeof(*wpolicy));
258         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
259 }