Whamcloud - gitweb
b=614459
[fs/lustre-release.git] / lustre / ldlm / ldlm_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define DEBUG_SUBSYSTEM S_LDLM
23
24 #include <linux/lustre_dlm.h>
25 #include <linux/obd_class.h>
26 #include <linux/obd.h>
27
28 static int interrupted_completion_wait(void *data)
29 {
30         RETURN(1);
31 }
32
33 static int expired_completion_wait(void *data)
34 {
35         struct ldlm_lock *lock = data;
36         struct ptlrpc_connection *conn;
37         struct obd_device *obd;
38
39         if (!lock)
40                 CERROR("NULL lock\n");
41         else if (!lock->l_connh)
42                 CERROR("lock %p has NULL connh\n", lock);
43         else if (!(obd = class_conn2obd(lock->l_connh)))
44                 CERROR("lock %p has NULL obd\n", lock);
45         else if (!(conn = obd->u.cli.cl_import.imp_connection))
46                 CERROR("lock %p has NULL connection\n", lock);
47         else {
48                 class_signal_connection_failure(conn);
49         }
50         RETURN(0);
51 }
52
53 #if 0
54 static int expired_completion_wait(void *data)
55 {
56         struct ldlm_lock *lock = data;
57         struct ptlrpc_connection *conn =
58                 class_conn2cliimp(lock->l_connh)->imp_connection;
59
60         if (!conn) {
61                 CERROR("lock %p has NULL import connection\n", lock);
62                 RETURN(1);
63         }
64
65         class_signal_connection_failure(conn);
66         RETURN(0);
67 }
68 #endif
69
70 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
71 {
72         struct l_wait_info lwi =
73                 LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_completion_wait,
74                                  interrupted_completion_wait, lock);
75         int rc = 0;
76         ENTRY;
77
78         if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
79                       LDLM_FL_BLOCK_CONV)) {
80                 /* Go to sleep until the lock is granted. */
81                 /* FIXME: or cancelled. */
82                 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock,"
83                            " sleeping");
84                 ldlm_lock_dump(lock);
85                 ldlm_reprocess_all(lock->l_resource);
86                 rc = l_wait_event(lock->l_waitq,
87                                   (lock->l_req_mode == lock->l_granted_mode),
88                                   &lwi);
89                 if (rc) {
90                         LDLM_DEBUG(lock,
91                                    "client-side enqueue waking up: failed (%d)",
92                                    rc);
93                 } else {
94                         LDLM_DEBUG(lock,
95                                    "client-side enqueue waking up: granted");
96                 }
97         } else if (flags == LDLM_FL_WAIT_NOREPROC) {
98                 rc = l_wait_event(lock->l_waitq,
99                                   (lock->l_req_mode == lock->l_granted_mode),
100                                   &lwi);
101         } else if (flags == 0) {
102                 wake_up(&lock->l_waitq);
103         }
104
105         RETURN(rc);
106 }
107
108 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
109                                   struct lustre_handle *parent_lockh,
110                                   __u64 *res_id,
111                                   __u32 type,
112                                   void *cookie, int cookielen,
113                                   ldlm_mode_t mode,
114                                   int *flags,
115                                   ldlm_completion_callback completion,
116                                   ldlm_blocking_callback blocking,
117                                   void *data,
118                                   __u32 data_len,
119                                   struct lustre_handle *lockh)
120 {
121         struct ldlm_lock *lock;
122         int err;
123         ENTRY;
124
125         if (ns->ns_client) {
126                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
127                 LBUG();
128         }
129
130         lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data,
131                                 data_len);
132         if (!lock)
133                 GOTO(out_nolock, err = -ENOMEM);
134         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
135
136         ldlm_lock_addref_internal(lock, mode);
137         ldlm_lock2handle(lock, lockh);
138         lock->l_connh = NULL;
139
140         err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
141                                 blocking);
142         if (err != ELDLM_OK)
143                 GOTO(out, err);
144
145         if (type == LDLM_EXTENT)
146                 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
147         if ((*flags) & LDLM_FL_LOCK_CHANGED)
148                 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
149
150         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
151                           lock);
152
153         if (lock->l_completion_ast)
154                 lock->l_completion_ast(lock, *flags);
155
156         LDLM_DEBUG(lock, "client-side local enqueue END");
157         EXIT;
158  out:
159         LDLM_LOCK_PUT(lock);
160  out_nolock:
161         return err;
162 }
163
164 int ldlm_cli_enqueue(struct lustre_handle *connh,
165                      struct ptlrpc_request *req,
166                      struct ldlm_namespace *ns,
167                      struct lustre_handle *parent_lock_handle,
168                      __u64 *res_id,
169                      __u32 type,
170                      void *cookie, int cookielen,
171                      ldlm_mode_t mode,
172                      int *flags,
173                      ldlm_completion_callback completion,
174                      ldlm_blocking_callback blocking,
175                      void *data,
176                      __u32 data_len,
177                      struct lustre_handle *lockh)
178 {
179         struct ldlm_lock *lock;
180         struct ldlm_request *body;
181         struct ldlm_reply *reply;
182         int rc, size = sizeof(*body), req_passed_in = 1;
183         ENTRY;
184
185         if (connh == NULL)
186                 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
187                                               type, cookie, cookielen, mode,
188                                               flags, completion, blocking, data,
189                                               data_len, lockh);
190
191         *flags = 0;
192         lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
193                                 data, data_len);
194         if (lock == NULL)
195                 GOTO(out_nolock, rc = -ENOMEM);
196         LDLM_DEBUG(lock, "client-side enqueue START");
197         /* for the local lock, add the reference */
198         ldlm_lock_addref_internal(lock, mode);
199         ldlm_lock2handle(lock, lockh);
200
201         if (req == NULL) {
202                 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
203                                       &size, NULL);
204                 if (!req)
205                         GOTO(out, rc = -ENOMEM);
206                 req_passed_in = 0;
207         } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
208                 LBUG();
209
210         /* Dump all of this data into the request buffer */
211         body = lustre_msg_buf(req->rq_reqmsg, 0);
212         ldlm_lock2desc(lock, &body->lock_desc);
213         /* Phil: make this part of ldlm_lock2desc */
214         if (type == LDLM_EXTENT) {
215                 memcpy(&body->lock_desc.l_extent, cookie,
216                        sizeof(body->lock_desc.l_extent));
217                 CDEBUG(D_INFO, "extent in body: "LPU64" -> "LPU64"\n",
218                            body->lock_desc.l_extent.start,
219                            body->lock_desc.l_extent.end);
220         }
221         body->lock_flags = *flags;
222
223         memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
224         if (parent_lock_handle)
225                 memcpy(&body->lock_handle2, parent_lock_handle,
226                        sizeof(body->lock_handle2));
227
228         /* Continue as normal. */
229         if (!req_passed_in) {
230                 size = sizeof(*reply);
231                 req->rq_replen = lustre_msg_size(1, &size);
232         }
233         lock->l_connh = connh;
234         lock->l_export = NULL;
235
236         rc = ptlrpc_queue_wait(req);
237         /* FIXME: status check here? */
238         rc = ptlrpc_check_status(req, rc);
239
240         if (rc != ELDLM_OK) {
241                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
242                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
243                 ldlm_lock_decref(lockh, mode);
244                 /* FIXME: if we've already received a completion AST, this will
245                  * LBUG! */
246                 ldlm_lock_destroy(lock);
247                 GOTO(out, rc);
248         }
249
250         reply = lustre_msg_buf(req->rq_repmsg, 0);
251         memcpy(&lock->l_remote_handle, &reply->lock_handle,
252                sizeof(lock->l_remote_handle));
253         if (type == LDLM_EXTENT)
254                 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
255         *flags = reply->lock_flags;
256
257         CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
258                (void *)(unsigned long)reply->lock_handle.addr, *flags);
259         CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got extent "
260                LPU64" -> "LPU64"\n",
261                body->lock_desc.l_extent.start, body->lock_desc.l_extent.end,
262                reply->lock_extent.start, reply->lock_extent.end);
263
264         /* If enqueue returned a blocked lock but the completion handler has
265          * already run, then it fixed up the resource and we don't need to do it
266          * again. */
267         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
268                 int newmode = reply->lock_mode;
269                 if (newmode && newmode != lock->l_req_mode) {
270                         LDLM_DEBUG(lock, "server returned different mode %s",
271                                    ldlm_lockname[newmode]);
272                         lock->l_req_mode = newmode;
273                 }
274
275                 if (reply->lock_resource_name[0] !=
276                     lock->l_resource->lr_name[0]) {
277                         CDEBUG(D_INFO, "remote intent success, locking %ld "
278                                "instead of %ld\n",
279                                (long)reply->lock_resource_name[0],
280                                (long)lock->l_resource->lr_name[0]);
281
282                         ldlm_lock_change_resource(lock,
283                                                   reply->lock_resource_name);
284                         if (lock->l_resource == NULL) {
285                                 LBUG();
286                                 RETURN(-ENOMEM);
287                         }
288                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
289                 }
290         }
291
292         if (!req_passed_in)
293                 ptlrpc_free_req(req);
294
295         rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
296                                blocking);
297         if (lock->l_completion_ast)
298                 lock->l_completion_ast(lock, *flags);
299
300         LDLM_DEBUG(lock, "client-side enqueue END");
301         EXIT;
302  out:
303         LDLM_LOCK_PUT(lock);
304  out_nolock:
305         return rc;
306 }
307
308 int ldlm_match_or_enqueue(struct lustre_handle *connh,
309                           struct ptlrpc_request *req,
310                           struct ldlm_namespace *ns,
311                           struct lustre_handle *parent_lock_handle,
312                           __u64 *res_id,
313                           __u32 type,
314                           void *cookie, int cookielen,
315                           ldlm_mode_t mode,
316                           int *flags,
317                           ldlm_completion_callback completion,
318                           ldlm_blocking_callback blocking,
319                           void *data,
320                           __u32 data_len,
321                           struct lustre_handle *lockh)
322 {
323         int rc;
324         ENTRY;
325         rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
326         if (rc == 0) {
327                 rc = ldlm_cli_enqueue(connh, req, ns,
328                                       parent_lock_handle, res_id, type, cookie,
329                                       cookielen, mode, flags, completion,
330                                       blocking, data, data_len, lockh);
331                 if (rc != ELDLM_OK)
332                         CERROR("ldlm_cli_enqueue: err: %d\n", rc);
333                 RETURN(rc);
334         } else
335                 RETURN(0);
336 }
337
338 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
339                                   int *flags)
340 {
341
342         if (lock->l_resource->lr_namespace->ns_client) {
343                 CERROR("Trying to cancel local lock\n");
344                 LBUG();
345         }
346         LDLM_DEBUG(lock, "client-side local convert");
347
348         ldlm_lock_convert(lock, new_mode, flags);
349         ldlm_reprocess_all(lock->l_resource);
350
351         LDLM_DEBUG(lock, "client-side local convert handler END");
352         LDLM_LOCK_PUT(lock);
353         RETURN(0);
354 }
355
356 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
357  * conversion of locks which are on the waiting or converting queue */
358 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
359 {
360         struct ldlm_request *body;
361         struct lustre_handle *connh;
362         struct ldlm_reply *reply;
363         struct ldlm_lock *lock;
364         struct ldlm_resource *res;
365         struct ptlrpc_request *req;
366         int rc, size = sizeof(*body);
367         ENTRY;
368
369         lock = ldlm_handle2lock(lockh);
370         if (!lock) {
371                 LBUG();
372                 RETURN(-EINVAL);
373         }
374         *flags = 0;
375         connh = lock->l_connh;
376
377         if (!connh)
378                 return ldlm_cli_convert_local(lock, new_mode, flags);
379
380         LDLM_DEBUG(lock, "client-side convert");
381
382         req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
383                               NULL);
384         if (!req)
385                 GOTO(out, rc = -ENOMEM);
386
387         body = lustre_msg_buf(req->rq_reqmsg, 0);
388         memcpy(&body->lock_handle1, &lock->l_remote_handle,
389                sizeof(body->lock_handle1));
390
391         body->lock_desc.l_req_mode = new_mode;
392         body->lock_flags = *flags;
393
394         size = sizeof(*reply);
395         req->rq_replen = lustre_msg_size(1, &size);
396
397         rc = ptlrpc_queue_wait(req);
398         rc = ptlrpc_check_status(req, rc);
399         if (rc != ELDLM_OK)
400                 GOTO(out, rc);
401
402         reply = lustre_msg_buf(req->rq_repmsg, 0);
403         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
404         if (res != NULL)
405                 ldlm_reprocess_all(res);
406         /* Go to sleep until the lock is granted. */
407         /* FIXME: or cancelled. */
408         if (lock->l_completion_ast)
409                 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
410         EXIT;
411  out:
412         LDLM_LOCK_PUT(lock);
413         ptlrpc_free_req(req);
414         return rc;
415 }
416
417 int ldlm_cli_cancel(struct lustre_handle *lockh)
418 {
419         struct ptlrpc_request *req;
420         struct ldlm_lock *lock;
421         struct ldlm_request *body;
422         int rc = 0, size = sizeof(*body);
423         ENTRY;
424
425         lock = ldlm_handle2lock(lockh);
426         if (!lock) {
427                 /* It's possible that the decref that we did just before this
428                  * cancel was the last reader/writer, and caused a cancel before
429                  * we could call this function.  If we want to make this
430                  * impossible (by adding a dec_and_cancel() or similar), then
431                  * we can put the LBUG back. */
432                 //LBUG();
433                 RETURN(-EINVAL);
434         }
435
436         if (lock->l_connh) {
437                 LDLM_DEBUG(lock, "client-side cancel");
438                 /* Set this flag to prevent others from getting new references*/
439                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
440                 lock->l_flags |= LDLM_FL_CBPENDING;
441                 ldlm_cancel_callback(lock);
442                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
443
444                 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
445                                       LDLM_CANCEL, 1, &size, NULL);
446                 if (!req)
447                         GOTO(out, rc = -ENOMEM);
448
449                 body = lustre_msg_buf(req->rq_reqmsg, 0);
450                 memcpy(&body->lock_handle1, &lock->l_remote_handle,
451                        sizeof(body->lock_handle1));
452
453                 req->rq_replen = lustre_msg_size(0, NULL);
454
455                 rc = ptlrpc_queue_wait(req);
456                 rc = ptlrpc_check_status(req, rc);
457                 ptlrpc_free_req(req);
458                 if (rc != ELDLM_OK)
459                         GOTO(out, rc);
460
461                 ldlm_lock_cancel(lock);
462         } else {
463                 LDLM_DEBUG(lock, "client-side local cancel");
464                 if (lock->l_resource->lr_namespace->ns_client) {
465                         CERROR("Trying to cancel local lock\n");
466                         LBUG();
467                 }
468                 ldlm_lock_cancel(lock);
469                 ldlm_reprocess_all(lock->l_resource);
470                 LDLM_DEBUG(lock, "client-side local cancel handler END");
471         }
472
473         EXIT;
474  out:
475         LDLM_LOCK_PUT(lock);
476         return rc;
477 }
478
479 /* Cancel all locks on a given resource that have 0 readers/writers.
480  *
481  * If 'local_only' is true, throw the locks away without trying to notify the
482  * server. */
483 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
484                            int local_only)
485 {
486         struct ldlm_resource *res;
487         struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
488         struct ldlm_ast_work *w;
489         ENTRY;
490
491         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
492         if (res == NULL) {
493                 /* This is not a problem. */
494                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id[0]);
495                 RETURN(0);
496         }
497
498         l_lock(&ns->ns_lock);
499         list_for_each(tmp, &res->lr_granted) {
500                 struct ldlm_lock *lock;
501                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
502
503                 if (lock->l_readers || lock->l_writers)
504                         continue;
505
506                 /* Setting the CBPENDING flag is a little misleading, but
507                  * prevents an important race; namely, once CBPENDING is set,
508                  * the lock can accumulate no more readers/writers.  Since
509                  * readers and writers are already zero here, ldlm_lock_decref
510                  * won't see this flag and call l_blocking_ast */
511                 lock->l_flags |= LDLM_FL_CBPENDING;
512
513                 OBD_ALLOC(w, sizeof(*w));
514                 LASSERT(w);
515
516                 w->w_lock = LDLM_LOCK_GET(lock);
517                 list_add(&w->w_list, &list);
518         }
519         l_unlock(&ns->ns_lock);
520
521         list_for_each_safe(tmp, next, &list) {
522                 struct lustre_handle lockh;
523                 int rc;
524                 w = list_entry(tmp, struct ldlm_ast_work, w_list);
525
526                 if (local_only)
527                         ldlm_lock_cancel(w->w_lock);
528                 else {
529                         ldlm_lock2handle(w->w_lock, &lockh);
530                         rc = ldlm_cli_cancel(&lockh);
531                         if (rc != ELDLM_OK)
532                                 CERROR("ldlm_cli_cancel: %d\n", rc);
533                 }
534                 LDLM_LOCK_PUT(w->w_lock);
535                 list_del(&w->w_list);
536                 OBD_FREE(w, sizeof(*w));
537         }
538
539         ldlm_resource_put(res);
540
541         RETURN(0);
542 }