Whamcloud - gitweb
d6ededf3249e3f152f1507d0695ac9dc578722f1
[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, 2017, 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  * local lock will be canceled after use, and it should run blocking ast only
62  * when it should trigger Commit-on-Sharing, otherwise if the blocking ast
63  * is run and does nothing, it will prevent subsequent operations to trigger
64  * Commit-on-Sharing, see LU-11102.
65  */
66 static bool ldlm_should_run_bl_ast(const struct ldlm_lock *lock,
67                                    const struct ldlm_lock *req)
68 {
69         /* no blocking ast */
70         if (!lock->l_blocking_ast)
71                 return false;
72
73         /* not local lock */
74         if (!ldlm_is_local(lock))
75                 return true;
76
77         /* should trigger Commit-on-Sharing */
78         if ((lock->l_req_mode & LCK_COS))
79                 return true;
80
81         /* local read lock will be canceld after use */
82         if (!(lock->l_req_mode & (LCK_PW | LCK_EX)))
83                 return false;
84
85         /* if CoS enabled, check if @req is from different client */
86         if (ldlm_is_cos_enabled(req))
87                 return lock->l_client_cookie != req->l_client_cookie;
88
89         /* check if @req is COS incompatible */
90         if (ldlm_is_cos_incompat(req))
91                 return true;
92
93         return false;
94 }
95
96 /**
97  * Determine if the lock is compatible with all locks on the queue.
98  *
99  * If \a work_list is provided, conflicting locks are linked there.
100  * If \a work_list is not provided, we exit this function on first conflict.
101  *
102  * \retval 0 if there are conflicting locks in the \a queue
103  * \retval 1 if the lock is compatible to all locks in \a queue
104  *
105  * IBITS locks in granted queue are organized in bunches of
106  * same-mode/same-bits locks called "skip lists". The First lock in the
107  * bunch contains a pointer to the end of the bunch.  This allows us to
108  * skip an entire bunch when iterating the list in search for conflicting
109  * locks if first lock of the bunch is not conflicting with us.
110  */
111 static int
112 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
113                             struct list_head *work_list)
114 {
115         struct list_head *tmp;
116         struct ldlm_lock *lock;
117         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
118         __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
119         int compat = 1;
120
121         ENTRY;
122
123         /* There is no sense in lock with no bits set. Also such a lock
124          * would be compatible with any other bit lock.
125          * Meanwhile that can be true if there were just try_bits and all
126          * are failed, so just exit gracefully and let the caller to care.
127          */
128         if ((req_bits | *try_bits) == 0)
129                 RETURN(0);
130
131         list_for_each(tmp, queue) {
132                 struct list_head *mode_tail;
133
134                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
135
136                 /* We stop walking the queue if we hit ourselves so we don't
137                  * take conflicting locks enqueued after us into account,
138                  * or we'd wait forever. */
139                 if (req == lock)
140                         RETURN(compat);
141
142                 /* last lock in mode group */
143                 LASSERT(lock->l_sl_mode.prev != NULL);
144                 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
145                                         l_sl_mode)->l_res_link;
146
147                 /* if request lock is not COS_INCOMPAT and COS is disabled,
148                  * they are compatible, IOW this request is from a local
149                  * transaction on a DNE system. */
150                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
151                     !ldlm_is_cos_enabled(req)) {
152                         /* jump to last lock in mode group */
153                         tmp = mode_tail;
154                         continue;
155                 }
156
157                 /* locks' mode are compatible, bits don't matter */
158                 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
159                         /* jump to last lock in mode group */
160                         tmp = mode_tail;
161                         continue;
162                 }
163
164                 for (;;) {
165                         struct list_head *head;
166
167                         /* Advance loop cursor to last lock in policy group. */
168                         tmp = &list_entry(lock->l_sl_policy.prev,
169                                           struct ldlm_lock,
170                                           l_sl_policy)->l_res_link;
171
172                         /* New lock's try_bits are filtered out by ibits
173                          * of all locks in both granted and waiting queues.
174                          */
175                         *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
176                                 lock->l_policy_data.l_inodebits.try_bits);
177
178                         if ((req_bits | *try_bits) == 0)
179                                 RETURN(0);
180
181                         /* The new lock ibits is more preferable than try_bits
182                          * of waiting locks so drop conflicting try_bits in
183                          * the waiting queue.
184                          * Notice that try_bits of granted locks must be zero.
185                          */
186                         lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
187
188                         /* Locks with overlapping bits conflict. */
189                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
190                                 /* COS lock mode has a special compatibility
191                                  * requirement: it is only compatible with
192                                  * locks from the same client. */
193                                 if (lock->l_req_mode == LCK_COS &&
194                                     !ldlm_is_cos_incompat(req) &&
195                                     ldlm_is_cos_enabled(req) &&
196                                     lock->l_client_cookie == req->l_client_cookie)
197                                         goto not_conflicting;
198
199                                 /* Found a conflicting policy group. */
200                                 if (!work_list)
201                                         RETURN(0);
202
203                                 compat = 0;
204
205                                 /* Add locks of the policy group to @work_list
206                                  * as blocking locks for @req */
207                                 if (ldlm_should_run_bl_ast(lock, req))
208                                         ldlm_add_ast_work_item(lock, req,
209                                                                work_list);
210                                 head = &lock->l_sl_policy;
211                                 list_for_each_entry(lock, head, l_sl_policy)
212                                         if (ldlm_should_run_bl_ast(lock, req))
213                                                 ldlm_add_ast_work_item(lock,
214                                                                 req, work_list);
215                         }
216 not_conflicting:
217                         if (tmp == mode_tail)
218                                 break;
219
220                         tmp = tmp->next;
221                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
222                 } /* Loop over policy groups within one mode group. */
223         } /* Loop over mode groups within @queue. */
224
225         RETURN(compat);
226 }
227
228 /**
229  * Process a granting attempt for IBITS lock.
230  * Must be called with ns lock held
231  *
232  * This function looks for any conflicts for \a lock in the granted or
233  * waiting queues. The lock is granted if no conflicts are found in
234  * either queue.
235  */
236 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
237                                 enum ldlm_process_intention intention,
238                                 enum ldlm_error *err,
239                                 struct list_head *work_list)
240 {
241         struct ldlm_resource *res = lock->l_resource;
242         struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
243                                                         NULL : work_list;
244         int rc;
245
246         ENTRY;
247
248         LASSERT(lock->l_granted_mode != lock->l_req_mode);
249         check_res_locked(res);
250
251         if (intention == LDLM_PROCESS_RESCAN) {
252                 struct list_head *bl_list;
253
254                 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
255                         bl_list = NULL;
256                         *err = ELDLM_LOCK_WOULDBLOCK;
257                 } else {
258                         bl_list = work_list;
259                         *err = ELDLM_LOCK_ABORTED;
260                 }
261
262                 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
263
264                 /* It is possible that some of granted locks was not canceled
265                  * but converted and is kept in granted queue. So there is
266                  * a window where lock with 'ast_sent' might become granted
267                  * again. Meanwhile a new lock may appear in that window and
268                  * conflicts with the converted lock so the following scenario
269                  * is possible:
270                  *
271                  * 1) lock1 conflicts with lock2
272                  * 2) bl_ast was sent for lock2
273                  * 3) lock3 comes and conflicts with lock2 too
274                  * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
275                  * 5) lock2 was converted for lock1 but not for lock3
276                  * 6) lock1 granted, lock3 still is waiting for lock2, but
277                  *    there will never be another bl_ast for that
278                  *
279                  * To avoid this scenario the work_list is used below to collect
280                  * any blocked locks from granted queue during every reprocess
281                  * and bl_ast will be sent if needed.
282                  */
283                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
284                                                  bl_list);
285                 if (!rc)
286                         RETURN(LDLM_ITER_STOP);
287                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
288                 if (!rc)
289                         RETURN(LDLM_ITER_STOP);
290
291                 /* grant also try_bits if any */
292                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
293                         lock->l_policy_data.l_inodebits.bits |=
294                                 lock->l_policy_data.l_inodebits.try_bits;
295                         lock->l_policy_data.l_inodebits.try_bits = 0;
296                         *flags |= LDLM_FL_LOCK_CHANGED;
297                 }
298                 ldlm_resource_unlink_lock(lock);
299                 ldlm_grant_lock(lock, grant_work);
300
301                 *err = ELDLM_OK;
302                 RETURN(LDLM_ITER_CONTINUE);
303         }
304
305         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, work_list);
306         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, work_list);
307
308         if (rc != 2) {
309                 /* if there were only bits to try and all are conflicting */
310                 if ((lock->l_policy_data.l_inodebits.bits |
311                      lock->l_policy_data.l_inodebits.try_bits) == 0) {
312                         *err = ELDLM_LOCK_WOULDBLOCK;
313                 } else {
314                         *err = ELDLM_OK;
315                 }
316         } else {
317                 /* grant also all remaining try_bits */
318                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
319                         lock->l_policy_data.l_inodebits.bits |=
320                                 lock->l_policy_data.l_inodebits.try_bits;
321                         lock->l_policy_data.l_inodebits.try_bits = 0;
322                         *flags |= LDLM_FL_LOCK_CHANGED;
323                 }
324                 LASSERT(lock->l_policy_data.l_inodebits.bits);
325                 ldlm_resource_unlink_lock(lock);
326                 ldlm_grant_lock(lock, grant_work);
327                 *err = ELDLM_OK;
328         }
329
330         RETURN(LDLM_ITER_CONTINUE);
331 }
332 #endif /* HAVE_SERVER_SUPPORT */
333
334 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
335                                      union ldlm_policy_data *lpolicy)
336 {
337         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
338         lpolicy->l_inodebits.try_bits = wpolicy->l_inodebits.try_bits;
339 }
340
341 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
342                                      union ldlm_wire_policy_data *wpolicy)
343 {
344         memset(wpolicy, 0, sizeof(*wpolicy));
345         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
346         wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
347 }
348
349 /**
350  * Attempt to convert already granted IBITS lock with several bits set to
351  * a lock with less bits (downgrade).
352  *
353  * Such lock conversion is used to keep lock with non-blocking bits instead of
354  * cancelling it, introduced for better support of DoM files.
355  */
356 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
357 {
358         ENTRY;
359
360         check_res_locked(lock->l_resource);
361
362         /* Just return if there are no conflicting bits */
363         if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
364                 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
365                           lock->l_policy_data.l_inodebits.bits, to_drop);
366                 /* nothing to do */
367                 RETURN(0);
368         }
369
370         /* remove lock from a skiplist and put in the new place
371          * according with new inodebits */
372         ldlm_resource_unlink_lock(lock);
373         lock->l_policy_data.l_inodebits.bits &= ~to_drop;
374         ldlm_grant_lock_with_skiplist(lock);
375         RETURN(0);
376 }
377 EXPORT_SYMBOL(ldlm_inodebits_drop);
378
379 /* convert single lock */
380 int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
381 {
382         struct lustre_handle lockh;
383         __u32 flags = 0;
384         int rc;
385
386         ENTRY;
387
388         LASSERT(drop_bits);
389         LASSERT(!lock->l_readers && !lock->l_writers);
390
391         LDLM_DEBUG(lock, "client lock convert START");
392
393         ldlm_lock2handle(lock, &lockh);
394         lock_res_and_lock(lock);
395         /* check if all bits are blocked */
396         if (!(lock->l_policy_data.l_inodebits.bits & ~drop_bits)) {
397                 unlock_res_and_lock(lock);
398                 /* return error to continue with cancel */
399                 GOTO(exit, rc = -EINVAL);
400         }
401
402         /* check if no common bits, consider this as successful convert */
403         if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
404                 unlock_res_and_lock(lock);
405                 GOTO(exit, rc = 0);
406         }
407
408         /* check if there is race with cancel */
409         if (ldlm_is_canceling(lock) || ldlm_is_cancel(lock)) {
410                 unlock_res_and_lock(lock);
411                 GOTO(exit, rc = -EINVAL);
412         }
413
414         /* clear cbpending flag early, it is safe to match lock right after
415          * client convert because it is downgrade always.
416          */
417         ldlm_clear_cbpending(lock);
418         ldlm_clear_bl_ast(lock);
419
420         /* If lock is being converted already, check drop bits first */
421         if (ldlm_is_converting(lock)) {
422                 /* raced lock convert, lock inodebits are remaining bits
423                  * so check if they are conflicting with new convert or not.
424                  */
425                 if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
426                         unlock_res_and_lock(lock);
427                         GOTO(exit, rc = 0);
428                 }
429                 /* Otherwise drop new conflicting bits in new convert */
430         }
431         ldlm_set_converting(lock);
432         /* from all bits of blocking lock leave only conflicting */
433         drop_bits &= lock->l_policy_data.l_inodebits.bits;
434         /* save them in cancel_bits, so l_blocking_ast will know
435          * which bits from the current lock were dropped. */
436         lock->l_policy_data.l_inodebits.cancel_bits = drop_bits;
437         /* Finally clear these bits in lock ibits */
438         ldlm_inodebits_drop(lock, drop_bits);
439         unlock_res_and_lock(lock);
440         /* Finally call cancel callback for remaining bits only.
441          * It is important to have converting flag during that
442          * so blocking_ast callback can distinguish convert from
443          * cancels.
444          */
445         if (lock->l_blocking_ast)
446                 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
447                                      LDLM_CB_CANCELING);
448
449         /* now notify server about convert */
450         rc = ldlm_cli_convert(lock, &flags);
451         if (rc) {
452                 lock_res_and_lock(lock);
453                 if (ldlm_is_converting(lock)) {
454                         ldlm_clear_converting(lock);
455                         ldlm_set_cbpending(lock);
456                         ldlm_set_bl_ast(lock);
457                 }
458                 unlock_res_and_lock(lock);
459                 GOTO(exit, rc);
460         }
461         EXIT;
462 exit:
463         LDLM_DEBUG(lock, "client lock convert END");
464         return rc;
465 }