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