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