Whamcloud - gitweb
LU-365 Update copyright for files modified by Whamcloud
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/ldlm/ldlm_plain.c
40  *
41  * Author: Peter Braam <braam@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LDLM
46
47 #ifdef __KERNEL__
48 #include <lustre_dlm.h>
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #else
52 #include <liblustre.h>
53 #endif
54
55 #include "ldlm_internal.h"
56
57 static inline int
58 ldlm_plain_compat_queue(cfs_list_t *queue, struct ldlm_lock *req,
59                         cfs_list_t *work_list)
60 {
61         cfs_list_t *tmp;
62         struct ldlm_lock *lock;
63         ldlm_mode_t req_mode = req->l_req_mode;
64         int compat = 1;
65         ENTRY;
66
67         lockmode_verify(req_mode);
68
69         cfs_list_for_each(tmp, queue) {
70                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
71
72                 if (req == lock)
73                         RETURN(compat);
74
75                  /* last lock in mode group */
76                  tmp = &cfs_list_entry(lock->l_sl_mode.prev,
77                                        struct ldlm_lock,
78                                        l_sl_mode)->l_res_link;
79
80                  if (lockmode_compat(lock->l_req_mode, req_mode))
81                         continue;
82
83                 if (!work_list)
84                         RETURN(0);
85
86                 compat = 0;
87
88                 /* add locks of the mode group to @work_list as
89                  * blocking locks for @req */
90                 if (lock->l_blocking_ast)
91                         ldlm_add_ast_work_item(lock, req, work_list);
92
93                 {
94                         cfs_list_t *head;
95
96                         head = &lock->l_sl_mode;
97                         cfs_list_for_each_entry(lock, head, l_sl_mode)
98                                 if (lock->l_blocking_ast)
99                                         ldlm_add_ast_work_item(lock, req,
100                                                                work_list);
101                 }
102         }
103
104         RETURN(compat);
105 }
106
107 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
108  *   - blocking ASTs have already been sent
109  *   - must call this function with the resource lock held
110  *
111  * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
112  *   - blocking ASTs have not been sent
113  *   - must call this function with the resource lock held */
114 int ldlm_process_plain_lock(struct ldlm_lock *lock, int *flags, int first_enq,
115                             ldlm_error_t *err, cfs_list_t *work_list)
116 {
117         struct ldlm_resource *res = lock->l_resource;
118         CFS_LIST_HEAD(rpc_list);
119         int rc;
120         ENTRY;
121
122         check_res_locked(res);
123         LASSERT(cfs_list_empty(&res->lr_converting));
124
125         if (!first_enq) {
126                 LASSERT(work_list != NULL);
127                 rc = ldlm_plain_compat_queue(&res->lr_granted, lock, NULL);
128                 if (!rc)
129                         RETURN(LDLM_ITER_STOP);
130                 rc = ldlm_plain_compat_queue(&res->lr_waiting, lock, NULL);
131                 if (!rc)
132                         RETURN(LDLM_ITER_STOP);
133
134                 ldlm_resource_unlink_lock(lock);
135                 ldlm_grant_lock(lock, work_list);
136                 RETURN(LDLM_ITER_CONTINUE);
137         }
138
139  restart:
140         rc = ldlm_plain_compat_queue(&res->lr_granted, lock, &rpc_list);
141         rc += ldlm_plain_compat_queue(&res->lr_waiting, lock, &rpc_list);
142
143         if (rc != 2) {
144                 /* If either of the compat_queue()s returned 0, then we
145                  * have ASTs to send and must go onto the waiting list.
146                  *
147                  * bug 2322: we used to unlink and re-add here, which was a
148                  * terrible folly -- if we goto restart, we could get
149                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
150                 if (cfs_list_empty(&lock->l_res_link))
151                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
152                 unlock_res(res);
153                 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_BL_AST);
154                 lock_res(res);
155                 if (rc == -ERESTART)
156                         GOTO(restart, -ERESTART);
157                 *flags |= LDLM_FL_BLOCK_GRANTED;
158         } else {
159                 ldlm_resource_unlink_lock(lock);
160                 ldlm_grant_lock(lock, NULL);
161         }
162         RETURN(0);
163 }
164
165 void ldlm_plain_policy_wire_to_local(const ldlm_wire_policy_data_t *wpolicy,
166                                      ldlm_policy_data_t *lpolicy)
167 {
168         /* No policy for plain locks */
169 }
170
171 void ldlm_plain_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
172                                      ldlm_wire_policy_data_t *wpolicy)
173 {
174         /* No policy for plain locks */
175 }