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