Whamcloud - gitweb
- If a non-CONNECT request is made with an invalid export, we need to
[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
124         if (ns->ns_client) {
125                 CERROR("Trying to cancel local lock\n");
126                 LBUG();
127         }
128
129         lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data, data_len);
130         if (!lock)
131                 GOTO(out_nolock, err = -ENOMEM);
132         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
133
134         ldlm_lock_addref_internal(lock, mode);
135         ldlm_lock2handle(lock, lockh);
136         lock->l_connh = NULL;
137
138         err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
139                                 blocking);
140         if (err != ELDLM_OK)
141                 GOTO(out, err);
142
143         if (type == LDLM_EXTENT)
144                 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
145         if ((*flags) & LDLM_FL_LOCK_CHANGED)
146                 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
147
148         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
149                           lock);
150
151         if (lock->l_completion_ast)
152                 lock->l_completion_ast(lock, *flags);
153
154         LDLM_DEBUG(lock, "client-side local enqueue END");
155         EXIT;
156  out:
157         LDLM_LOCK_PUT(lock);
158  out_nolock:
159         return err;
160 }
161
162 int ldlm_cli_enqueue(struct lustre_handle *connh,
163                      struct ptlrpc_request *req,
164                      struct ldlm_namespace *ns,
165                      struct lustre_handle *parent_lock_handle,
166                      __u64 *res_id,
167                      __u32 type,
168                      void *cookie, int cookielen,
169                      ldlm_mode_t mode,
170                      int *flags,
171                      ldlm_completion_callback completion,
172                      ldlm_blocking_callback blocking,
173                      void *data,
174                      __u32 data_len,
175                      struct lustre_handle *lockh)
176 {
177         struct ldlm_lock *lock;
178         struct ldlm_request *body;
179         struct ldlm_reply *reply;
180         int rc, size = sizeof(*body), req_passed_in = 1;
181         ENTRY;
182
183         if (connh == NULL)
184                 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
185                                               type, cookie, cookielen, mode,
186                                               flags, completion, blocking, data,
187                                               data_len, lockh);
188
189         *flags = 0;
190         lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
191                                 data, data_len);
192         if (lock == NULL)
193                 GOTO(out_nolock, rc = -ENOMEM);
194         LDLM_DEBUG(lock, "client-side enqueue START");
195         /* for the local lock, add the reference */
196         ldlm_lock_addref_internal(lock, mode);
197         ldlm_lock2handle(lock, lockh);
198
199         if (req == NULL) {
200                 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
201                                       &size, NULL);
202                 if (!req)
203                         GOTO(out, rc = -ENOMEM);
204                 req_passed_in = 0;
205         } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
206                 LBUG();
207
208         /* Dump all of this data into the request buffer */
209         body = lustre_msg_buf(req->rq_reqmsg, 0);
210         ldlm_lock2desc(lock, &body->lock_desc);
211         /* Phil: make this part of ldlm_lock2desc */
212         if (type == LDLM_EXTENT)
213                 memcpy(&body->lock_desc.l_extent, cookie,
214                        sizeof(body->lock_desc.l_extent));
215         body->lock_flags = *flags;
216
217         memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
218         if (parent_lock_handle)
219                 memcpy(&body->lock_handle2, parent_lock_handle,
220                        sizeof(body->lock_handle2));
221
222         /* Continue as normal. */
223         if (!req_passed_in) {
224                 size = sizeof(*reply);
225                 req->rq_replen = lustre_msg_size(1, &size);
226         }
227         lock->l_connh = connh;
228         lock->l_export = NULL;
229
230         rc = ptlrpc_queue_wait(req);
231         /* FIXME: status check here? */
232         rc = ptlrpc_check_status(req, rc);
233
234         if (rc != ELDLM_OK) {
235                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
236                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
237                 ldlm_lock_decref(lockh, mode);
238                 /* FIXME: if we've already received a completion AST, this will
239                  * LBUG! */
240                 ldlm_lock_destroy(lock);
241                 GOTO(out, rc);
242         }
243
244         reply = lustre_msg_buf(req->rq_repmsg, 0);
245         memcpy(&lock->l_remote_handle, &reply->lock_handle,
246                sizeof(lock->l_remote_handle));
247         if (type == LDLM_EXTENT)
248                 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
249         *flags = reply->lock_flags;
250
251         CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
252                (void *)(unsigned long)reply->lock_handle.addr, *flags);
253         CDEBUG(D_INFO, "extent: %Lu -> %Lu\n",
254                (unsigned long long)reply->lock_extent.start,
255                (unsigned long long)reply->lock_extent.end);
256
257         /* If enqueue returned a blocked lock but the completion handler has
258          * already run, then it fixed up the resource and we don't need to do it
259          * again. */
260         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
261                 int newmode = reply->lock_mode;
262                 if (newmode && newmode != lock->l_req_mode) {
263                         LDLM_DEBUG(lock, "server returned different mode %s",
264                                    ldlm_lockname[newmode]);
265                         lock->l_req_mode = newmode;
266                 }
267
268                 if (reply->lock_resource_name[0] !=
269                     lock->l_resource->lr_name[0]) {
270                         CDEBUG(D_INFO, "remote intent success, locking %ld "
271                                "instead of %ld\n",
272                                (long)reply->lock_resource_name[0],
273                                (long)lock->l_resource->lr_name[0]);
274
275                         ldlm_lock_change_resource(lock,
276                                                   reply->lock_resource_name);
277                         if (lock->l_resource == NULL) {
278                                 LBUG();
279                                 RETURN(-ENOMEM);
280                         }
281                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
282                 }
283         }
284
285         if (!req_passed_in)
286                 ptlrpc_free_req(req);
287
288         rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
289                                blocking);
290         if (lock->l_completion_ast)
291                 lock->l_completion_ast(lock, *flags);
292
293         LDLM_DEBUG(lock, "client-side enqueue END");
294         EXIT;
295  out:
296         LDLM_LOCK_PUT(lock);
297  out_nolock:
298         return rc;
299 }
300
301 int ldlm_match_or_enqueue(struct lustre_handle *connh,
302                           struct ptlrpc_request *req,
303                           struct ldlm_namespace *ns,
304                           struct lustre_handle *parent_lock_handle,
305                           __u64 *res_id,
306                           __u32 type,
307                           void *cookie, int cookielen,
308                           ldlm_mode_t mode,
309                           int *flags,
310                           ldlm_completion_callback completion,
311                           ldlm_blocking_callback blocking,
312                           void *data,
313                           __u32 data_len,
314                           struct lustre_handle *lockh)
315 {
316         int rc;
317         ENTRY;
318         rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
319         if (rc == 0) {
320                 rc = ldlm_cli_enqueue(connh, req, ns,
321                                       parent_lock_handle, res_id, type, cookie,
322                                       cookielen, mode, flags, completion,
323                                       blocking, data, data_len, lockh);
324                 if (rc != ELDLM_OK)
325                         CERROR("ldlm_cli_enqueue: err: %d\n", rc);
326                 RETURN(rc);
327         } else
328                 RETURN(0);
329 }
330
331 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
332                                   int *flags)
333 {
334
335         if (lock->l_resource->lr_namespace->ns_client) {
336                 CERROR("Trying to cancel local lock\n");
337                 LBUG();
338         }
339         LDLM_DEBUG(lock, "client-side local convert");
340
341         ldlm_lock_convert(lock, new_mode, flags);
342         ldlm_reprocess_all(lock->l_resource);
343
344         LDLM_DEBUG(lock, "client-side local convert handler END");
345         LDLM_LOCK_PUT(lock);
346         RETURN(0);
347 }
348
349 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
350  * conversion of locks which are on the waiting or converting queue */
351 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
352 {
353         struct ldlm_request *body;
354         struct lustre_handle *connh;
355         struct ldlm_reply *reply;
356         struct ldlm_lock *lock;
357         struct ldlm_resource *res;
358         struct ptlrpc_request *req;
359         int rc, size = sizeof(*body);
360         ENTRY;
361
362         lock = ldlm_handle2lock(lockh);
363         if (!lock) {
364                 LBUG();
365                 RETURN(-EINVAL);
366         }
367         *flags = 0;
368         connh = lock->l_connh;
369
370         if (!connh)
371                 return ldlm_cli_convert_local(lock, new_mode, flags);
372
373         LDLM_DEBUG(lock, "client-side convert");
374
375         req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
376                               NULL);
377         if (!req)
378                 GOTO(out, rc = -ENOMEM);
379
380         body = lustre_msg_buf(req->rq_reqmsg, 0);
381         memcpy(&body->lock_handle1, &lock->l_remote_handle,
382                sizeof(body->lock_handle1));
383
384         body->lock_desc.l_req_mode = new_mode;
385         body->lock_flags = *flags;
386
387         size = sizeof(*reply);
388         req->rq_replen = lustre_msg_size(1, &size);
389
390         rc = ptlrpc_queue_wait(req);
391         rc = ptlrpc_check_status(req, rc);
392         if (rc != ELDLM_OK)
393                 GOTO(out, rc);
394
395         reply = lustre_msg_buf(req->rq_repmsg, 0);
396         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
397         if (res != NULL)
398                 ldlm_reprocess_all(res);
399         /* Go to sleep until the lock is granted. */
400         /* FIXME: or cancelled. */
401         if (lock->l_completion_ast)
402                 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
403         EXIT;
404  out:
405         LDLM_LOCK_PUT(lock);
406         ptlrpc_free_req(req);
407         return rc;
408 }
409
410 int ldlm_cli_cancel(struct lustre_handle *lockh)
411 {
412         struct ptlrpc_request *req;
413         struct ldlm_lock *lock;
414         struct ldlm_request *body;
415         int rc = 0, size = sizeof(*body);
416         ENTRY;
417
418         lock = ldlm_handle2lock(lockh);
419         if (!lock) {
420                 /* It's possible that the decref that we did just before this
421                  * cancel was the last reader/writer, and caused a cancel before
422                  * we could call this function.  If we want to make this
423                  * impossible (by adding a dec_and_cancel() or similar), then
424                  * we can put the LBUG back. */
425                 //LBUG();
426                 RETURN(-EINVAL);
427         }
428
429         if (lock->l_connh) {
430                 LDLM_DEBUG(lock, "client-side cancel");
431                 /* Set this flag to prevent others from getting new references*/
432                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
433                 lock->l_flags |= LDLM_FL_CBPENDING;
434                 ldlm_cancel_callback(lock);
435                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
436
437                 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
438                                       LDLM_CANCEL, 1, &size, NULL);
439                 if (!req)
440                         GOTO(out, rc = -ENOMEM);
441
442                 body = lustre_msg_buf(req->rq_reqmsg, 0);
443                 memcpy(&body->lock_handle1, &lock->l_remote_handle,
444                        sizeof(body->lock_handle1));
445
446                 req->rq_replen = lustre_msg_size(0, NULL);
447
448                 rc = ptlrpc_queue_wait(req);
449                 rc = ptlrpc_check_status(req, rc);
450                 ptlrpc_free_req(req);
451                 if (rc != ELDLM_OK)
452                         GOTO(out, rc);
453
454                 ldlm_lock_cancel(lock);
455         } else {
456                 LDLM_DEBUG(lock, "client-side local cancel");
457                 if (lock->l_resource->lr_namespace->ns_client) {
458                         CERROR("Trying to cancel local lock\n");
459                         LBUG();
460                 }
461                 ldlm_lock_cancel(lock);
462                 ldlm_reprocess_all(lock->l_resource);
463                 LDLM_DEBUG(lock, "client-side local cancel handler END");
464         }
465
466         EXIT;
467  out:
468         LDLM_LOCK_PUT(lock);
469         return rc;
470 }
471
472 /* Cancel all locks on a given resource that have 0 readers/writers.
473  *
474  * If 'local_only' is true, throw the locks away without trying to notify the
475  * server. */
476 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
477                            int local_only)
478 {
479         struct ldlm_resource *res;
480         struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
481         struct ldlm_ast_work *w;
482         ENTRY;
483
484         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
485         if (res == NULL)
486                 RETURN(-EINVAL);
487
488         l_lock(&ns->ns_lock);
489         list_for_each(tmp, &res->lr_granted) {
490                 struct ldlm_lock *lock;
491                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
492
493                 if (lock->l_readers || lock->l_writers)
494                         continue;
495
496                 /* Setting the CBPENDING flag is a little misleading, but
497                  * prevents an important race; namely, once CBPENDING is set,
498                  * the lock can accumulate no more readers/writers.  Since
499                  * readers and writers are already zero here, ldlm_lock_decref
500                  * won't see this flag and call l_blocking_ast */
501                 lock->l_flags |= LDLM_FL_CBPENDING;
502
503                 OBD_ALLOC(w, sizeof(*w));
504                 LASSERT(w);
505
506                 w->w_lock = LDLM_LOCK_GET(lock);
507                 list_add(&w->w_list, &list);
508         }
509         l_unlock(&ns->ns_lock);
510
511         list_for_each_safe(tmp, next, &list) {
512                 struct lustre_handle lockh;
513                 int rc;
514                 w = list_entry(tmp, struct ldlm_ast_work, w_list);
515
516                 if (local_only)
517                         ldlm_lock_cancel(w->w_lock);
518                 else {
519                         ldlm_lock2handle(w->w_lock, &lockh);
520                         rc = ldlm_cli_cancel(&lockh);
521                         if (rc != ELDLM_OK)
522                                 CERROR("ldlm_cli_cancel: %d\n", rc);
523                 }
524                 LDLM_LOCK_PUT(w->w_lock);
525                 list_del(&w->w_list);
526                 OBD_FREE(w, sizeof(*w));
527         }
528
529         ldlm_resource_put(res);
530
531         RETURN(0);
532 }