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