Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / ldlm / ldlm_plain.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25
26 #ifdef __KERNEL__
27 #include <linux/lustre_dlm.h>
28 #include <linux/obd_support.h>
29 #include <linux/lustre_lib.h>
30 #else
31 #include <liblustre.h>
32 #endif
33
34 #include "ldlm_internal.h"
35
36 static inline int
37 ldlm_plain_compat_queue(struct list_head *queue, struct ldlm_lock *req,
38                         int send_cbs)
39 {
40         struct list_head *tmp;
41         struct ldlm_lock *lock;
42         ldlm_mode_t req_mode = req->l_req_mode;
43         int compat = 1;
44         ENTRY;
45
46         list_for_each(tmp, queue) {
47                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
48
49                 if (req == lock)
50                         RETURN(compat);
51
52                 if (lockmode_compat(lock->l_req_mode, req_mode))
53                         continue;
54
55                 if (!send_cbs)
56                         RETURN(0);
57
58                 compat = 0;
59                 if (lock->l_blocking_ast)
60                         ldlm_add_ast_work_item(lock, req, NULL, 0);
61         }
62
63         RETURN(compat);
64 }
65
66 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
67  *   - blocking ASTs have already been sent
68  *   - the caller has already initialized req->lr_tmp
69  *   - must call this function with the ns lock held
70  *
71  * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
72  *   - blocking ASTs have not been sent
73  *   - the caller has NOT initialized req->lr_tmp, so we must
74  *   - must call this function with the ns lock held once */
75 int ldlm_process_plain_lock(struct ldlm_lock *lock, int *flags, int first_enq,
76                             ldlm_error_t *err)
77 {
78         struct ldlm_resource *res = lock->l_resource;
79         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
80         int rc;
81         ENTRY;
82
83         LASSERT(list_empty(&res->lr_converting));
84
85         if (!first_enq) {
86                 LASSERT(res->lr_tmp != NULL);
87                 rc = ldlm_plain_compat_queue(&res->lr_granted, lock, 0);
88                 if (!rc)
89                         RETURN(LDLM_ITER_STOP);
90                 rc = ldlm_plain_compat_queue(&res->lr_waiting, lock, 0);
91                 if (!rc)
92                         RETURN(LDLM_ITER_STOP);
93
94                 ldlm_resource_unlink_lock(lock);
95                 ldlm_grant_lock(lock, NULL, 0, 1);
96                 RETURN(LDLM_ITER_CONTINUE);
97         }
98
99  restart:
100         LASSERT(res->lr_tmp == NULL);
101         res->lr_tmp = &rpc_list;
102         rc = ldlm_plain_compat_queue(&res->lr_granted, lock, 1);
103         rc += ldlm_plain_compat_queue(&res->lr_waiting, lock, 1);
104         res->lr_tmp = NULL;
105
106         if (rc != 2) {
107                 /* If either of the compat_queue()s returned 0, then we
108                  * have ASTs to send and must go onto the waiting list. */
109                 ldlm_resource_unlink_lock(lock);
110                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
111                 l_unlock(&res->lr_namespace->ns_lock);
112                 rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
113                 l_lock(&res->lr_namespace->ns_lock);
114                 if (rc == -ERESTART)
115                         GOTO(restart, -ERESTART);
116                 *flags |= LDLM_FL_BLOCK_GRANTED;
117         } else {
118                 ldlm_resource_unlink_lock(lock);
119                 ldlm_grant_lock(lock, NULL, 0, 0);
120         }
121         RETURN(0);
122 }