Whamcloud - gitweb
LU-14082 ldlm: Use req_mode while lock cleanup
[fs/lustre-release.git] / lustre / ldlm / ldlm_flock.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) 2003 Hewlett-Packard Development Company LP.
24  * Developed under the sponsorship of the US Government under
25  * Subcontract No. B514193
26  *
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2017, 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
37 /**
38  * This file implements POSIX lock type for Lustre.
39  * Its policy properties are start and end of extent and PID.
40  *
41  * These locks are only done through MDS due to POSIX semantics requiring
42  * e.g. that locks could be only partially released and as such split into
43  * two parts, and also that two adjacent locks from the same process may be
44  * merged into a single wider lock.
45  *
46  * Lock modes are mapped like this:
47  * PR and PW for READ and WRITE locks
48  * NL to request a releasing of a portion of the lock
49  *
50  * These flock locks never timeout.
51  */
52
53 #define DEBUG_SUBSYSTEM S_LDLM
54
55 #include <linux/list.h>
56 #include <lustre_dlm.h>
57 #include <obd_support.h>
58 #include <obd_class.h>
59 #include <lustre_lib.h>
60
61 #include "ldlm_internal.h"
62
63 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
64                             void *data, int flag);
65
66 /**
67  * list_for_remaining_safe - iterate over the remaining entries in a list
68  *              and safeguard against removal of a list entry.
69  * \param pos   the &struct list_head to use as a loop counter. pos MUST
70  *              have been initialized prior to using it in this macro.
71  * \param n     another &struct list_head to use as temporary storage
72  * \param head  the head for your list.
73  */
74 #define list_for_remaining_safe(pos, n, head) \
75         for (n = pos->next; pos != (head); pos = n, n = pos->next)
76
77 static inline int
78 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
79 {
80         return ((new->l_policy_data.l_flock.owner ==
81                  lock->l_policy_data.l_flock.owner) &&
82                 (new->l_export == lock->l_export));
83 }
84
85 static inline int
86 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
87 {
88         return ((new->l_policy_data.l_flock.start <=
89                  lock->l_policy_data.l_flock.end) &&
90                 (new->l_policy_data.l_flock.end >=
91                  lock->l_policy_data.l_flock.start));
92 }
93
94 static inline void ldlm_flock_blocking_link(struct ldlm_lock *req,
95                                             struct ldlm_lock *lock)
96 {
97         /* For server only */
98         if (req->l_export == NULL)
99                 return;
100
101         LASSERT(hlist_unhashed(&req->l_exp_flock_hash));
102
103         req->l_policy_data.l_flock.blocking_owner =
104                 lock->l_policy_data.l_flock.owner;
105         req->l_policy_data.l_flock.blocking_export =
106                 lock->l_export;
107         atomic_set(&req->l_policy_data.l_flock.blocking_refs, 0);
108
109         cfs_hash_add(req->l_export->exp_flock_hash,
110                      &req->l_policy_data.l_flock.owner,
111                      &req->l_exp_flock_hash);
112 }
113
114 static inline void ldlm_flock_blocking_unlink(struct ldlm_lock *req)
115 {
116         /* For server only */
117         if (req->l_export == NULL)
118                 return;
119
120         check_res_locked(req->l_resource);
121         if (req->l_export->exp_flock_hash != NULL &&
122             !hlist_unhashed(&req->l_exp_flock_hash))
123                 cfs_hash_del(req->l_export->exp_flock_hash,
124                              &req->l_policy_data.l_flock.owner,
125                              &req->l_exp_flock_hash);
126 }
127
128 static inline void
129 ldlm_flock_destroy(struct ldlm_lock *lock, enum ldlm_mode mode, __u64 flags)
130 {
131         ENTRY;
132
133         LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: %#llx)",
134                    mode, flags);
135
136         /* Safe to not lock here, since it should be empty anyway */
137         LASSERT(hlist_unhashed(&lock->l_exp_flock_hash));
138
139         list_del_init(&lock->l_res_link);
140         if (flags == LDLM_FL_WAIT_NOREPROC) {
141                 /* client side - set a flag to prevent sending a CANCEL */
142                 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
143
144                 /* when reaching here, it is under lock_res_and_lock(). Thus,
145                  * need call the nolock version of ldlm_lock_decref_internal
146                  */
147                 ldlm_lock_decref_internal_nolock(lock, mode);
148         }
149
150         ldlm_lock_destroy_nolock(lock);
151         EXIT;
152 }
153
154 #ifdef HAVE_SERVER_SUPPORT
155 /**
156  * POSIX locks deadlock detection code.
157  *
158  * Given a new lock \a req and an existing lock \a bl_lock it conflicts
159  * with, we need to iterate through all blocked POSIX locks for this
160  * export and see if there is a deadlock condition arising. (i.e. when
161  * one client holds a lock on something and want a lock on something
162  * else and at the same time another client has the opposite situation).
163  */
164
165 struct ldlm_flock_lookup_cb_data {
166         __u64 *bl_owner;
167         struct ldlm_lock *lock;
168         struct obd_export *exp;
169 };
170
171 static int ldlm_flock_lookup_cb(struct obd_export *exp, void *data)
172 {
173         struct ldlm_flock_lookup_cb_data *cb_data = data;
174         struct ldlm_lock *lock;
175
176         if (exp->exp_failed)
177                 return 0;
178
179         lock = cfs_hash_lookup(exp->exp_flock_hash, cb_data->bl_owner);
180         if (lock == NULL)
181                 return 0;
182
183         /* Stop on first found lock. Same process can't sleep twice */
184         cb_data->lock = lock;
185         cb_data->exp = class_export_get(exp);
186
187         return 1;
188 }
189
190 static int
191 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *bl_lock)
192 {
193         struct obd_export *req_exp = req->l_export;
194         struct obd_export *bl_exp = bl_lock->l_export;
195         __u64 req_owner = req->l_policy_data.l_flock.owner;
196         __u64 bl_owner = bl_lock->l_policy_data.l_flock.owner;
197
198         /* For server only */
199         if (req_exp == NULL)
200                 return 0;
201
202         class_export_get(bl_exp);
203         while (1) {
204                 struct ldlm_flock_lookup_cb_data cb_data = {
205                         .bl_owner = &bl_owner,
206                         .lock = NULL,
207                         .exp = NULL,
208                 };
209                 struct ptlrpc_connection *bl_exp_conn;
210                 struct obd_export *bl_exp_new;
211                 struct ldlm_lock *lock = NULL;
212                 struct ldlm_flock *flock;
213
214                 bl_exp_conn = bl_exp->exp_connection;
215                 if (bl_exp->exp_flock_hash != NULL) {
216                         int found;
217
218                         found = obd_nid_export_for_each(bl_exp->exp_obd,
219                                                         bl_exp_conn->c_peer.nid,
220                                                         ldlm_flock_lookup_cb,
221                                                         &cb_data);
222                         if (found)
223                                 lock = cb_data.lock;
224                 }
225                 if (lock == NULL)
226                         break;
227
228                 class_export_put(bl_exp);
229                 bl_exp = cb_data.exp;
230
231                 LASSERT(req != lock);
232                 flock = &lock->l_policy_data.l_flock;
233                 LASSERT(flock->owner == bl_owner);
234                 bl_owner = flock->blocking_owner;
235                 bl_exp_new = class_export_get(flock->blocking_export);
236                 class_export_put(bl_exp);
237
238                 cfs_hash_put(bl_exp->exp_flock_hash, &lock->l_exp_flock_hash);
239                 bl_exp = bl_exp_new;
240
241                 if (bl_exp->exp_failed)
242                         break;
243
244                 if (bl_owner == req_owner &&
245                     (bl_exp_conn->c_peer.nid ==
246                      req_exp->exp_connection->c_peer.nid)) {
247                         class_export_put(bl_exp);
248                         return 1;
249                 }
250         }
251         class_export_put(bl_exp);
252
253         return 0;
254 }
255
256 static void ldlm_flock_cancel_on_deadlock(struct ldlm_lock *lock,
257                                           struct list_head *work_list)
258 {
259         CDEBUG(D_INFO, "reprocess deadlock req=%p\n", lock);
260
261         if ((exp_connect_flags(lock->l_export) &
262              OBD_CONNECT_FLOCK_DEAD) == 0) {
263                 CERROR("deadlock found, but client doesn't support flock canceliation\n");
264         } else {
265                 LASSERT(lock->l_completion_ast);
266                 LASSERT(!ldlm_is_ast_sent(lock));
267                 lock->l_flags |= (LDLM_FL_AST_SENT | LDLM_FL_CANCEL_ON_BLOCK |
268                                   LDLM_FL_FLOCK_DEADLOCK);
269                 ldlm_flock_blocking_unlink(lock);
270                 ldlm_resource_unlink_lock(lock);
271                 ldlm_add_ast_work_item(lock, NULL, work_list);
272         }
273 }
274 #endif /* HAVE_SERVER_SUPPORT */
275
276 /**
277  * Process a granting attempt for flock lock.
278  * Must be called under ns lock held.
279  *
280  * This function looks for any conflicts for \a lock in the granted or
281  * waiting queues. The lock is granted if no conflicts are found in
282  * either queue.
283  */
284 int
285 ldlm_process_flock_lock(struct ldlm_lock *req, __u64 *flags,
286                         enum ldlm_process_intention intention,
287                         enum ldlm_error *err, struct list_head *work_list)
288 {
289         struct ldlm_resource *res = req->l_resource;
290         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
291         struct list_head *tmp;
292         struct list_head *ownlocks = NULL;
293         struct ldlm_lock *lock = NULL;
294         struct ldlm_lock *new = req;
295         struct ldlm_lock *new2 = NULL;
296         enum ldlm_mode mode = req->l_req_mode;
297         int local = ns_is_client(ns);
298         int added = (mode == LCK_NL);
299         int overlaps = 0;
300         int splitted = 0;
301         const struct ldlm_callback_suite null_cbs = { NULL };
302 #ifdef HAVE_SERVER_SUPPORT
303         struct list_head *grant_work = (intention == LDLM_PROCESS_ENQUEUE ?
304                                         NULL : work_list);
305 #endif
306         ENTRY;
307
308         CDEBUG(D_DLMTRACE, "flags %#llx owner %llu pid %u mode %u start "
309                "%llu end %llu\n", *flags,
310                new->l_policy_data.l_flock.owner,
311                new->l_policy_data.l_flock.pid, mode,
312                req->l_policy_data.l_flock.start,
313                req->l_policy_data.l_flock.end);
314
315         *err = ELDLM_OK;
316
317         if (local) {
318                 /* No blocking ASTs are sent to the clients for
319                  * Posix file & record locks
320                  */
321                 req->l_blocking_ast = NULL;
322         } else {
323                 /* Called on the server for lock cancels. */
324                 req->l_blocking_ast = ldlm_flock_blocking_ast;
325         }
326
327 reprocess:
328         if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
329                 /* This loop determines where this processes locks start
330                  * in the resource lr_granted list.
331                  */
332                 list_for_each(tmp, &res->lr_granted) {
333                         lock = list_entry(tmp, struct ldlm_lock,
334                                           l_res_link);
335                         if (ldlm_same_flock_owner(lock, req)) {
336                                 ownlocks = tmp;
337                                 break;
338                         }
339                 }
340         }
341 #ifdef HAVE_SERVER_SUPPORT
342         else {
343                 int reprocess_failed = 0;
344                 lockmode_verify(mode);
345
346                 /* This loop determines if there are existing locks
347                  * that conflict with the new lock request.
348                  */
349                 list_for_each(tmp, &res->lr_granted) {
350                         lock = list_entry(tmp, struct ldlm_lock,
351                                           l_res_link);
352
353                         if (ldlm_same_flock_owner(lock, req)) {
354                                 if (!ownlocks)
355                                         ownlocks = tmp;
356                                 continue;
357                         }
358
359                         /* locks are compatible, overlap doesn't matter */
360                         if (lockmode_compat(lock->l_granted_mode, mode))
361                                 continue;
362
363                         if (!ldlm_flocks_overlap(lock, req))
364                                 continue;
365
366                         if (intention != LDLM_PROCESS_ENQUEUE) {
367                                 reprocess_failed = 1;
368                                 if (ldlm_flock_deadlock(req, lock)) {
369                                         ldlm_flock_cancel_on_deadlock(
370                                                 req, grant_work);
371                                         RETURN(LDLM_ITER_CONTINUE);
372                                 }
373                                 continue;
374                         }
375
376                         if (*flags & LDLM_FL_BLOCK_NOWAIT) {
377                                 ldlm_flock_destroy(req, mode, *flags);
378                                 *err = -EAGAIN;
379                                 RETURN(LDLM_ITER_STOP);
380                         }
381
382                         if (*flags & LDLM_FL_TEST_LOCK) {
383                                 ldlm_flock_destroy(req, mode, *flags);
384                                 req->l_req_mode = lock->l_granted_mode;
385                                 req->l_policy_data.l_flock.pid =
386                                         lock->l_policy_data.l_flock.pid;
387                                 req->l_policy_data.l_flock.start =
388                                         lock->l_policy_data.l_flock.start;
389                                 req->l_policy_data.l_flock.end =
390                                         lock->l_policy_data.l_flock.end;
391                                 *flags |= LDLM_FL_LOCK_CHANGED;
392                                 RETURN(LDLM_ITER_STOP);
393                         }
394
395                         /* add lock to blocking list before deadlock
396                          * check to prevent race
397                          */
398                         ldlm_flock_blocking_link(req, lock);
399
400                         if (ldlm_flock_deadlock(req, lock)) {
401                                 ldlm_flock_blocking_unlink(req);
402                                 ldlm_flock_destroy(req, mode, *flags);
403                                 *err = -EDEADLK;
404                                 RETURN(LDLM_ITER_STOP);
405                         }
406
407                         ldlm_resource_add_lock(res, &res->lr_waiting, req);
408                         *flags |= LDLM_FL_BLOCK_GRANTED;
409                         RETURN(LDLM_ITER_STOP);
410                 }
411                 if (reprocess_failed)
412                         RETURN(LDLM_ITER_CONTINUE);
413         }
414
415         if (*flags & LDLM_FL_TEST_LOCK) {
416                 ldlm_flock_destroy(req, mode, *flags);
417                 req->l_req_mode = LCK_NL;
418                 *flags |= LDLM_FL_LOCK_CHANGED;
419                 RETURN(LDLM_ITER_STOP);
420         }
421
422         /* In case we had slept on this lock request take it off of the
423          * deadlock detection hash list.
424          */
425         ldlm_flock_blocking_unlink(req);
426 #endif /* HAVE_SERVER_SUPPORT */
427
428         /* Scan the locks owned by this process that overlap this request.
429          * We may have to merge or split existing locks.
430          */
431         if (!ownlocks)
432                 ownlocks = &res->lr_granted;
433
434         list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
435                 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
436
437                 if (!ldlm_same_flock_owner(lock, new))
438                         break;
439
440                 if (lock->l_granted_mode == mode) {
441                         /* If the modes are the same then we need to process
442                          * locks that overlap OR adjoin the new lock. The extra
443                          * logic condition is necessary to deal with arithmetic
444                          * overflow and underflow.
445                          */
446                         if ((new->l_policy_data.l_flock.start >
447                              (lock->l_policy_data.l_flock.end + 1))
448                             && (lock->l_policy_data.l_flock.end !=
449                                 OBD_OBJECT_EOF))
450                                 continue;
451
452                         if ((new->l_policy_data.l_flock.end <
453                              (lock->l_policy_data.l_flock.start - 1))
454                             && (lock->l_policy_data.l_flock.start != 0))
455                                 break;
456
457                         if (new->l_policy_data.l_flock.start <
458                             lock->l_policy_data.l_flock.start) {
459                                 lock->l_policy_data.l_flock.start =
460                                         new->l_policy_data.l_flock.start;
461                         } else {
462                                 new->l_policy_data.l_flock.start =
463                                         lock->l_policy_data.l_flock.start;
464                         }
465
466                         if (new->l_policy_data.l_flock.end >
467                             lock->l_policy_data.l_flock.end) {
468                                 lock->l_policy_data.l_flock.end =
469                                         new->l_policy_data.l_flock.end;
470                         } else {
471                                 new->l_policy_data.l_flock.end =
472                                         lock->l_policy_data.l_flock.end;
473                         }
474
475                         if (added) {
476                                 ldlm_flock_destroy(lock, mode, *flags);
477                         } else {
478                                 new = lock;
479                                 added = 1;
480                         }
481                         continue;
482                 }
483
484                 if (new->l_policy_data.l_flock.start >
485                     lock->l_policy_data.l_flock.end)
486                         continue;
487
488                 if (new->l_policy_data.l_flock.end <
489                     lock->l_policy_data.l_flock.start)
490                         break;
491
492                 ++overlaps;
493
494                 if (new->l_policy_data.l_flock.start <=
495                     lock->l_policy_data.l_flock.start) {
496                         if (new->l_policy_data.l_flock.end <
497                             lock->l_policy_data.l_flock.end) {
498                                 lock->l_policy_data.l_flock.start =
499                                         new->l_policy_data.l_flock.end + 1;
500                                 break;
501                         }
502                         ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
503                         continue;
504                 }
505                 if (new->l_policy_data.l_flock.end >=
506                     lock->l_policy_data.l_flock.end) {
507                         lock->l_policy_data.l_flock.end =
508                                 new->l_policy_data.l_flock.start - 1;
509                         continue;
510                 }
511
512                 /* split the existing lock into two locks */
513
514                 /* if this is an F_UNLCK operation then we could avoid
515                  * allocating a new lock and use the req lock passed in
516                  * with the request but this would complicate the reply
517                  * processing since updates to req get reflected in the
518                  * reply. The client side replays the lock request so
519                  * it must see the original lock data in the reply.
520                  */
521
522                 /* XXX - if ldlm_lock_new() can sleep we should
523                  * release the lr_lock, allocate the new lock,
524                  * and restart processing this lock.
525                  */
526                 if (new2 == NULL) {
527                         unlock_res_and_lock(req);
528                         new2 = ldlm_lock_create(ns, &res->lr_name, LDLM_FLOCK,
529                                                 lock->l_granted_mode, &null_cbs,
530                                                 NULL, 0, LVB_T_NONE);
531                         lock_res_and_lock(req);
532                         if (IS_ERR(new2)) {
533                                 ldlm_flock_destroy(req, lock->l_granted_mode,
534                                                    *flags);
535                                 *err = PTR_ERR(new2);
536                                 RETURN(LDLM_ITER_STOP);
537                         }
538                         goto reprocess;
539                 }
540
541                 splitted = 1;
542
543                 new2->l_granted_mode = lock->l_granted_mode;
544                 new2->l_policy_data.l_flock.pid =
545                         new->l_policy_data.l_flock.pid;
546                 new2->l_policy_data.l_flock.owner =
547                         new->l_policy_data.l_flock.owner;
548                 new2->l_policy_data.l_flock.start =
549                         lock->l_policy_data.l_flock.start;
550                 new2->l_policy_data.l_flock.end =
551                         new->l_policy_data.l_flock.start - 1;
552                 lock->l_policy_data.l_flock.start =
553                         new->l_policy_data.l_flock.end + 1;
554                 new2->l_conn_export = lock->l_conn_export;
555                 if (lock->l_export != NULL) {
556                         new2->l_export = class_export_lock_get(lock->l_export,
557                                                                new2);
558                         if (new2->l_export->exp_lock_hash &&
559                             hlist_unhashed(&new2->l_exp_hash))
560                                 cfs_hash_add(new2->l_export->exp_lock_hash,
561                                              &new2->l_remote_handle,
562                                              &new2->l_exp_hash);
563                 }
564                 if (*flags == LDLM_FL_WAIT_NOREPROC)
565                         ldlm_lock_addref_internal_nolock(new2,
566                                                          lock->l_granted_mode);
567
568                 /* insert new2 at lock */
569                 ldlm_resource_add_lock(res, ownlocks, new2);
570                 LDLM_LOCK_RELEASE(new2);
571                 break;
572         }
573
574         /* if new2 is created but never used, destroy it*/
575         if (splitted == 0 && new2 != NULL)
576                 ldlm_lock_destroy_nolock(new2);
577
578         /* At this point we're granting the lock request. */
579         req->l_granted_mode = req->l_req_mode;
580
581         /* Add req to the granted queue before calling ldlm_reprocess_all(). */
582         if (!added) {
583                 list_del_init(&req->l_res_link);
584                 /* insert new lock before ownlocks in list. */
585                 ldlm_resource_add_lock(res, ownlocks, req);
586         }
587
588         if (*flags != LDLM_FL_WAIT_NOREPROC) {
589 #ifdef HAVE_SERVER_SUPPORT
590                 if (intention == LDLM_PROCESS_ENQUEUE) {
591                         /* If this is an unlock, reprocess the waitq and
592                          * send completions ASTs for locks that can now be
593                          * granted. The only problem with doing this
594                          * reprocessing here is that the completion ASTs for
595                          * newly granted locks will be sent before the unlock
596                          * completion is sent. It shouldn't be an issue. Also
597                          * note that ldlm_process_flock_lock() will recurse,
598                          * but only once because 'intention' won't be
599                          * LDLM_PROCESS_ENQUEUE from ldlm_reprocess_queue.
600                          */
601                         if ((mode == LCK_NL) && overlaps) {
602                                 LIST_HEAD(rpc_list);
603                                 int rc;
604
605 restart:
606                                 ldlm_reprocess_queue(res, &res->lr_waiting,
607                                                      &rpc_list,
608                                                      LDLM_PROCESS_RESCAN, NULL);
609
610                                 unlock_res_and_lock(req);
611                                 rc = ldlm_run_ast_work(ns, &rpc_list,
612                                                        LDLM_WORK_CP_AST);
613                                 lock_res_and_lock(req);
614                                 if (rc == -ERESTART)
615                                         GOTO(restart, rc);
616                         }
617                 } else {
618                         LASSERT(req->l_completion_ast);
619                         ldlm_add_ast_work_item(req, NULL, grant_work);
620                 }
621 #else /* !HAVE_SERVER_SUPPORT */
622                 /* The only one possible case for client-side calls flock
623                  * policy function is ldlm_flock_completion_ast inside which
624                  * carries LDLM_FL_WAIT_NOREPROC flag.
625                  */
626                 CERROR("Illegal parameter for client-side-only module.\n");
627                 LBUG();
628 #endif /* HAVE_SERVER_SUPPORT */
629         }
630
631         /* In case we're reprocessing the requested lock we can't destroy
632          * it until after calling ldlm_add_ast_work_item() above so that laawi()
633          * can bump the reference count on \a req. Otherwise \a req
634          * could be freed before the completion AST can be sent.
635          */
636         if (added)
637                 ldlm_flock_destroy(req, mode, *flags);
638
639         ldlm_resource_dump(D_INFO, res);
640         RETURN(LDLM_ITER_CONTINUE);
641 }
642
643 /**
644  * Flock completion callback function.
645  *
646  * \param lock [in,out]: A lock to be handled
647  * \param flags    [in]: flags
648  * \param *data    [in]: ldlm_work_cp_ast_lock() will use ldlm_cb_set_arg
649  *
650  * \retval 0    : success
651  * \retval <0   : failure
652  */
653 int
654 ldlm_flock_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
655 {
656         struct file_lock *getlk = lock->l_ast_data;
657         struct obd_device *obd;
658         struct obd_import *imp = NULL;
659         enum ldlm_error err;
660         int rc = 0;
661         ENTRY;
662
663         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT2, 4);
664         if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT3)) {
665                 lock_res_and_lock(lock);
666                 lock->l_flags |= LDLM_FL_FAIL_LOC;
667                 unlock_res_and_lock(lock);
668                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT3, 4);
669         }
670         CDEBUG(D_DLMTRACE, "flags: %#llx data: %p getlk: %p\n",
671                flags, data, getlk);
672
673         LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
674
675         if (flags & LDLM_FL_FAILED)
676                 goto granted;
677
678         if (!(flags & LDLM_FL_BLOCKED_MASK)) {
679                 if (NULL == data)
680                         /* mds granted the lock in the reply */
681                         goto granted;
682                 /* CP AST RPC: lock get granted, wake it up */
683                 wake_up(&lock->l_waitq);
684                 RETURN(0);
685         }
686
687         LDLM_DEBUG(lock,
688                    "client-side enqueue returned a blocked lock, sleeping");
689         obd = class_exp2obd(lock->l_conn_export);
690
691         /* if this is a local lock, there is no import */
692         if (obd)
693                 imp = obd->u.cli.cl_import;
694
695         /* Go to sleep until the lock is granted. */
696         rc = l_wait_event_abortable(lock->l_waitq,
697                                     is_granted_or_cancelled(lock));
698         if (rc < 0) {
699                 /* take lock off the deadlock detection hash list. */
700                 lock_res_and_lock(lock);
701                 ldlm_flock_blocking_unlink(lock);
702
703                 /* client side - set flag to prevent lock from being
704                  * put on LRU list
705                  */
706                 ldlm_set_cbpending(lock);
707                 unlock_res_and_lock(lock);
708
709                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
710                            rc);
711                 RETURN(rc);
712         }
713
714 granted:
715         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT, 10);
716
717         if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT4)) {
718                 lock_res_and_lock(lock);
719                 /* DEADLOCK is always set with CBPENDING */
720                 lock->l_flags |= LDLM_FL_FLOCK_DEADLOCK | LDLM_FL_CBPENDING;
721                 unlock_res_and_lock(lock);
722                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT4, 4);
723         }
724         if (OBD_FAIL_PRECHECK(OBD_FAIL_LDLM_CP_CB_WAIT5)) {
725                 lock_res_and_lock(lock);
726                 /* DEADLOCK is always set with CBPENDING */
727                 lock->l_flags |= (LDLM_FL_FAIL_LOC |
728                                   LDLM_FL_FLOCK_DEADLOCK | LDLM_FL_CBPENDING);
729                 unlock_res_and_lock(lock);
730                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CP_CB_WAIT5, 4);
731         }
732
733         lock_res_and_lock(lock);
734
735
736         /* Protect against race where lock could have been just destroyed
737          * due to overlap in ldlm_process_flock_lock().
738          */
739         if (ldlm_is_destroyed(lock)) {
740                 unlock_res_and_lock(lock);
741                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
742
743                 /* An error is still to be returned, to propagate it up to
744                  * ldlm_cli_enqueue_fini() caller. */
745                 RETURN(-EIO);
746         }
747
748         /* ldlm_lock_enqueue() has already placed lock on the granted list. */
749         ldlm_resource_unlink_lock(lock);
750
751         /* Import invalidation. We need to actually release the lock
752          * references being held, so that it can go away. No point in
753          * holding the lock even if app still believes it has it, since
754          * server already dropped it anyway. Only for granted locks too.
755          */
756         /* Do the same for DEADLOCK'ed locks. */
757         if (ldlm_is_failed(lock) || ldlm_is_flock_deadlock(lock)) {
758                 int mode;
759
760                 if (flags & LDLM_FL_TEST_LOCK)
761                         LASSERT(ldlm_is_test_lock(lock));
762
763                 if (ldlm_is_test_lock(lock) || ldlm_is_flock_deadlock(lock))
764                         mode = getlk->fl_type;
765                 else
766                         mode = lock->l_req_mode;
767
768                 if (ldlm_is_flock_deadlock(lock)) {
769                         LDLM_DEBUG(lock, "client-side enqueue deadlock "
770                                    "received");
771                         rc = -EDEADLK;
772                 }
773                 ldlm_flock_destroy(lock, mode, LDLM_FL_WAIT_NOREPROC);
774                 unlock_res_and_lock(lock);
775
776                 /* Need to wake up the waiter if we were evicted */
777                 wake_up(&lock->l_waitq);
778
779                 /* An error is still to be returned, to propagate it up to
780                  * ldlm_cli_enqueue_fini() caller.
781                  */
782                 RETURN(rc ? : -EIO);
783         }
784
785         LDLM_DEBUG(lock, "client-side enqueue granted");
786
787         if (flags & LDLM_FL_TEST_LOCK) {
788                 /*
789                  * fcntl(F_GETLK) request
790                  * The old mode was saved in getlk->fl_type so that if the mode
791                  * in the lock changes we can decref the appropriate refcount.
792                  */
793                 LASSERT(ldlm_is_test_lock(lock));
794                 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
795                 switch (lock->l_granted_mode) {
796                 case LCK_PR:
797                         getlk->fl_type = F_RDLCK;
798                         break;
799                 case LCK_PW:
800                         getlk->fl_type = F_WRLCK;
801                         break;
802                 default:
803                         getlk->fl_type = F_UNLCK;
804                 }
805                 getlk->fl_pid = (pid_t)lock->l_policy_data.l_flock.pid;
806                 getlk->fl_start = (loff_t)lock->l_policy_data.l_flock.start;
807                 getlk->fl_end = (loff_t)lock->l_policy_data.l_flock.end;
808         } else {
809                 __u64 noreproc = LDLM_FL_WAIT_NOREPROC;
810
811                 /* We need to reprocess the lock to do merges or splits
812                  * with existing locks owned by this process.
813                  */
814                 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
815         }
816         unlock_res_and_lock(lock);
817         RETURN(rc);
818 }
819 EXPORT_SYMBOL(ldlm_flock_completion_ast);
820
821 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
822                             void *data, int flag)
823 {
824         ENTRY;
825
826         LASSERT(lock);
827         LASSERT(flag == LDLM_CB_CANCELING);
828
829         /* take lock off the deadlock detection hash list. */
830         lock_res_and_lock(lock);
831         ldlm_flock_blocking_unlink(lock);
832         unlock_res_and_lock(lock);
833         RETURN(0);
834 }
835
836 void ldlm_flock_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
837                                      union ldlm_policy_data *lpolicy)
838 {
839         lpolicy->l_flock.start = wpolicy->l_flock.lfw_start;
840         lpolicy->l_flock.end = wpolicy->l_flock.lfw_end;
841         lpolicy->l_flock.pid = wpolicy->l_flock.lfw_pid;
842         lpolicy->l_flock.owner = wpolicy->l_flock.lfw_owner;
843 }
844
845 void ldlm_flock_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
846                                      union ldlm_wire_policy_data *wpolicy)
847 {
848         memset(wpolicy, 0, sizeof(*wpolicy));
849         wpolicy->l_flock.lfw_start = lpolicy->l_flock.start;
850         wpolicy->l_flock.lfw_end = lpolicy->l_flock.end;
851         wpolicy->l_flock.lfw_pid = lpolicy->l_flock.pid;
852         wpolicy->l_flock.lfw_owner = lpolicy->l_flock.owner;
853 }
854
855 /*
856  * Export handle<->flock hash operations.
857  */
858 static unsigned
859 ldlm_export_flock_hash(struct cfs_hash *hs, const void *key, unsigned mask)
860 {
861         return cfs_hash_u64_hash(*(__u64 *)key, mask);
862 }
863
864 static void *
865 ldlm_export_flock_key(struct hlist_node *hnode)
866 {
867         struct ldlm_lock *lock;
868
869         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
870         return &lock->l_policy_data.l_flock.owner;
871 }
872
873 static int
874 ldlm_export_flock_keycmp(const void *key, struct hlist_node *hnode)
875 {
876         return !memcmp(ldlm_export_flock_key(hnode), key, sizeof(__u64));
877 }
878
879 static void *
880 ldlm_export_flock_object(struct hlist_node *hnode)
881 {
882         return hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
883 }
884
885 static void
886 ldlm_export_flock_get(struct cfs_hash *hs, struct hlist_node *hnode)
887 {
888         struct ldlm_lock *lock;
889         struct ldlm_flock *flock;
890
891         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
892         LDLM_LOCK_GET(lock);
893
894         flock = &lock->l_policy_data.l_flock;
895         LASSERT(flock->blocking_export != NULL);
896         class_export_get(flock->blocking_export);
897         atomic_inc(&flock->blocking_refs);
898 }
899
900 static void
901 ldlm_export_flock_put(struct cfs_hash *hs, struct hlist_node *hnode)
902 {
903         struct ldlm_lock *lock;
904         struct ldlm_flock *flock;
905
906         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_flock_hash);
907
908         flock = &lock->l_policy_data.l_flock;
909         LASSERT(flock->blocking_export != NULL);
910         class_export_put(flock->blocking_export);
911         if (atomic_dec_and_test(&flock->blocking_refs)) {
912                 flock->blocking_owner = 0;
913                 flock->blocking_export = NULL;
914         }
915         LDLM_LOCK_RELEASE(lock);
916 }
917
918 static struct cfs_hash_ops ldlm_export_flock_ops = {
919         .hs_hash        = ldlm_export_flock_hash,
920         .hs_key         = ldlm_export_flock_key,
921         .hs_keycmp      = ldlm_export_flock_keycmp,
922         .hs_object      = ldlm_export_flock_object,
923         .hs_get         = ldlm_export_flock_get,
924         .hs_put         = ldlm_export_flock_put,
925         .hs_put_locked  = ldlm_export_flock_put,
926 };
927
928 int ldlm_init_flock_export(struct obd_export *exp)
929 {
930         if( strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDT_NAME) != 0)
931                 RETURN(0);
932
933         exp->exp_flock_hash =
934                 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
935                                 HASH_EXP_LOCK_CUR_BITS,
936                                 HASH_EXP_LOCK_MAX_BITS,
937                                 HASH_EXP_LOCK_BKT_BITS, 0,
938                                 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
939                                 &ldlm_export_flock_ops,
940                                 CFS_HASH_DEFAULT | CFS_HASH_NBLK_CHANGE);
941         if (!exp->exp_flock_hash)
942                 RETURN(-ENOMEM);
943
944         RETURN(0);
945 }
946
947 void ldlm_destroy_flock_export(struct obd_export *exp)
948 {
949         ENTRY;
950         if (exp->exp_flock_hash) {
951                 cfs_hash_putref(exp->exp_flock_hash);
952                 exp->exp_flock_hash = NULL;
953         }
954         EXIT;
955 }