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