f5c2f1979ad3d68c588b9f50c4ddb806af5b5b41
[linux-2.6.git] / fs / ocfs2 / dlm / dlmconvert.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmconvert.c
5  *
6  * underlying calls for lock conversion
7  *
8  * Copyright (C) 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  *
25  */
26
27
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/highmem.h>
33 #include <linux/utsname.h>
34 #include <linux/init.h>
35 #include <linux/sysctl.h>
36 #include <linux/random.h>
37 #include <linux/blkdev.h>
38 #include <linux/socket.h>
39 #include <linux/inet.h>
40 #include <linux/spinlock.h>
41
42
43 #include "cluster/heartbeat.h"
44 #include "cluster/nodemanager.h"
45 #include "cluster/tcp.h"
46
47 #include "dlmapi.h"
48 #include "dlmcommon.h"
49
50 #include "dlmconvert.h"
51
52 #define MLOG_MASK_PREFIX ML_DLM
53 #include "cluster/masklog.h"
54
55 /* NOTE: __dlmconvert_master is the only function in here that
56  * needs a spinlock held on entry (res->spinlock) and it is the
57  * only one that holds a lock on exit (res->spinlock).
58  * All other functions in here need no locks and drop all of
59  * the locks that they acquire. */
60 static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
61                                            struct dlm_lock_resource *res,
62                                            struct dlm_lock *lock, int flags,
63                                            int type, int *call_ast,
64                                            int *kick_thread);
65 static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
66                                            struct dlm_lock_resource *res,
67                                            struct dlm_lock *lock, int flags, int type);
68
69 /*
70  * this is only called directly by dlmlock(), and only when the
71  * local node is the owner of the lockres
72  * locking:
73  *   caller needs:  none
74  *   taken:         takes and drops res->spinlock
75  *   held on exit:  none
76  * returns: see __dlmconvert_master
77  */
78 enum dlm_status dlmconvert_master(struct dlm_ctxt *dlm,
79                                   struct dlm_lock_resource *res,
80                                   struct dlm_lock *lock, int flags, int type)
81 {
82         int call_ast = 0, kick_thread = 0;
83         enum dlm_status status;
84
85         spin_lock(&res->spinlock);
86         /* we are not in a network handler, this is fine */
87         __dlm_wait_on_lockres(res);
88         __dlm_lockres_reserve_ast(res);
89         res->state |= DLM_LOCK_RES_IN_PROGRESS;
90
91         status = __dlmconvert_master(dlm, res, lock, flags, type,
92                                      &call_ast, &kick_thread);
93
94         res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
95         spin_unlock(&res->spinlock);
96         wake_up(&res->wq);
97         if (status != DLM_NORMAL && status != DLM_NOTQUEUED)
98                 dlm_error(status);
99
100         /* either queue the ast or release it */
101         if (call_ast)
102                 dlm_queue_ast(dlm, lock);
103         else
104                 dlm_lockres_release_ast(dlm, res);
105
106         if (kick_thread)
107                 dlm_kick_thread(dlm, res);
108
109         return status;
110 }
111
112 /* performs lock conversion at the lockres master site
113  * locking:
114  *   caller needs:  res->spinlock
115  *   taken:         takes and drops lock->spinlock
116  *   held on exit:  res->spinlock
117  * returns: DLM_NORMAL, DLM_NOTQUEUED, DLM_DENIED
118  *   call_ast: whether ast should be called for this lock
119  *   kick_thread: whether dlm_kick_thread should be called
120  */
121 static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
122                                            struct dlm_lock_resource *res,
123                                            struct dlm_lock *lock, int flags,
124                                            int type, int *call_ast,
125                                            int *kick_thread)
126 {
127         enum dlm_status status = DLM_NORMAL;
128         struct list_head *iter;
129         struct dlm_lock *tmplock=NULL;
130
131         assert_spin_locked(&res->spinlock);
132
133         mlog_entry("type=%d, convert_type=%d, new convert_type=%d\n",
134                    lock->ml.type, lock->ml.convert_type, type);
135
136         spin_lock(&lock->spinlock);
137
138         /* already converting? */
139         if (lock->ml.convert_type != LKM_IVMODE) {
140                 mlog(ML_ERROR, "attempted to convert a lock with a lock "
141                      "conversion pending\n");
142                 status = DLM_DENIED;
143                 goto unlock_exit;
144         }
145
146         /* must be on grant queue to convert */
147         if (!dlm_lock_on_list(&res->granted, lock)) {
148                 mlog(ML_ERROR, "attempted to convert a lock not on grant "
149                      "queue\n");
150                 status = DLM_DENIED;
151                 goto unlock_exit;
152         }
153
154         if (flags & LKM_VALBLK) {
155                 switch (lock->ml.type) {
156                         case LKM_EXMODE:
157                                 /* EX + LKM_VALBLK + convert == set lvb */
158                                 mlog(0, "will set lvb: converting %s->%s\n",
159                                      dlm_lock_mode_name(lock->ml.type),
160                                      dlm_lock_mode_name(type));
161                                 lock->lksb->flags |= DLM_LKSB_PUT_LVB;
162                                 break;
163                         case LKM_PRMODE:
164                         case LKM_NLMODE:
165                                 /* refetch if new level is not NL */
166                                 if (type > LKM_NLMODE) {
167                                         mlog(0, "will fetch new value into "
168                                              "lvb: converting %s->%s\n",
169                                              dlm_lock_mode_name(lock->ml.type),
170                                              dlm_lock_mode_name(type));
171                                         lock->lksb->flags |= DLM_LKSB_GET_LVB;
172                                 } else {
173                                         mlog(0, "will NOT fetch new value "
174                                              "into lvb: converting %s->%s\n",
175                                              dlm_lock_mode_name(lock->ml.type),
176                                              dlm_lock_mode_name(type));
177                                         flags &= ~(LKM_VALBLK);
178                                 }
179                                 break;
180                 }
181         }
182
183
184         /* in-place downconvert? */
185         if (type <= lock->ml.type)
186                 goto grant;
187
188         /* upconvert from here on */
189         status = DLM_NORMAL;
190         list_for_each(iter, &res->granted) {
191                 tmplock = list_entry(iter, struct dlm_lock, list);
192                 if (tmplock == lock)
193                         continue;
194                 if (!dlm_lock_compatible(tmplock->ml.type, type))
195                         goto switch_queues;
196         }
197
198         list_for_each(iter, &res->converting) {
199                 tmplock = list_entry(iter, struct dlm_lock, list);
200                 if (!dlm_lock_compatible(tmplock->ml.type, type))
201                         goto switch_queues;
202                 /* existing conversion requests take precedence */
203                 if (!dlm_lock_compatible(tmplock->ml.convert_type, type))
204                         goto switch_queues;
205         }
206
207         /* fall thru to grant */
208
209 grant:
210         mlog(0, "res %.*s, granting %s lock\n", res->lockname.len,
211              res->lockname.name, dlm_lock_mode_name(type));
212         /* immediately grant the new lock type */
213         lock->lksb->status = DLM_NORMAL;
214         if (lock->ml.node == dlm->node_num)
215                 mlog(0, "doing in-place convert for nonlocal lock\n");
216         lock->ml.type = type;
217         status = DLM_NORMAL;
218         *call_ast = 1;
219         goto unlock_exit;
220
221 switch_queues:
222         if (flags & LKM_NOQUEUE) {
223                 mlog(0, "failed to convert NOQUEUE lock %.*s from "
224                      "%d to %d...\n", res->lockname.len, res->lockname.name,
225                      lock->ml.type, type);
226                 status = DLM_NOTQUEUED;
227                 goto unlock_exit;
228         }
229         mlog(0, "res %.*s, queueing...\n", res->lockname.len,
230              res->lockname.name);
231
232         lock->ml.convert_type = type;
233         /* do not alter lock refcount.  switching lists. */
234         list_del_init(&lock->list);
235         list_add_tail(&lock->list, &res->converting);
236
237 unlock_exit:
238         spin_unlock(&lock->spinlock);
239         if (status == DLM_DENIED) {
240                 __dlm_print_one_lock_resource(res);
241         }
242         if (status == DLM_NORMAL)
243                 *kick_thread = 1;
244         return status;
245 }
246
247 void dlm_revert_pending_convert(struct dlm_lock_resource *res,
248                                 struct dlm_lock *lock)
249 {
250         /* do not alter lock refcount.  switching lists. */
251         list_del_init(&lock->list);
252         list_add_tail(&lock->list, &res->granted);
253         lock->ml.convert_type = LKM_IVMODE;
254         lock->lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
255 }
256
257 /* messages the master site to do lock conversion
258  * locking:
259  *   caller needs:  none
260  *   taken:         takes and drops res->spinlock, uses DLM_LOCK_RES_IN_PROGRESS
261  *   held on exit:  none
262  * returns: DLM_NORMAL, DLM_RECOVERING, status from remote node
263  */
264 enum dlm_status dlmconvert_remote(struct dlm_ctxt *dlm,
265                                   struct dlm_lock_resource *res,
266                                   struct dlm_lock *lock, int flags, int type)
267 {
268         enum dlm_status status;
269
270         mlog(0, "type=%d, convert_type=%d, busy=%d\n", lock->ml.type,
271              lock->ml.convert_type, res->state & DLM_LOCK_RES_IN_PROGRESS);
272
273         spin_lock(&res->spinlock);
274         if (res->state & DLM_LOCK_RES_RECOVERING) {
275                 mlog(0, "bailing out early since res is RECOVERING "
276                      "on secondary queue\n");
277                 /* __dlm_print_one_lock_resource(res); */
278                 status = DLM_RECOVERING;
279                 goto bail;
280         }
281         /* will exit this call with spinlock held */
282         __dlm_wait_on_lockres(res);
283
284         if (lock->ml.convert_type != LKM_IVMODE) {
285                 __dlm_print_one_lock_resource(res);
286                 mlog(ML_ERROR, "converting a remote lock that is already "
287                      "converting! (cookie=%"MLFu64", conv=%d)\n",
288                      lock->ml.cookie, lock->ml.convert_type);
289                 status = DLM_DENIED;
290                 goto bail;
291         }
292         res->state |= DLM_LOCK_RES_IN_PROGRESS;
293         /* move lock to local convert queue */
294         /* do not alter lock refcount.  switching lists. */
295         list_del_init(&lock->list);
296         list_add_tail(&lock->list, &res->converting);
297         lock->convert_pending = 1;
298         lock->ml.convert_type = type;
299
300         if (flags & LKM_VALBLK) {
301                 if (lock->ml.type == LKM_EXMODE) {
302                         flags |= LKM_PUT_LVB;
303                         lock->lksb->flags |= DLM_LKSB_PUT_LVB;
304                 } else {
305                         if (lock->ml.convert_type == LKM_NLMODE)
306                                 flags &= ~LKM_VALBLK;
307                         else {
308                                 flags |= LKM_GET_LVB;
309                                 lock->lksb->flags |= DLM_LKSB_GET_LVB;
310                         }
311                 }
312         }
313         spin_unlock(&res->spinlock);
314
315         /* no locks held here.
316          * need to wait for a reply as to whether it got queued or not. */
317         status = dlm_send_remote_convert_request(dlm, res, lock, flags, type);
318
319         spin_lock(&res->spinlock);
320         res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
321         lock->convert_pending = 0;
322         /* if it failed, move it back to granted queue */
323         if (status != DLM_NORMAL) {
324                 if (status != DLM_NOTQUEUED)
325                         dlm_error(status);
326                 dlm_revert_pending_convert(res, lock);
327         }
328 bail:
329         spin_unlock(&res->spinlock);
330
331         /* TODO: should this be a wake_one? */
332         /* wake up any IN_PROGRESS waiters */
333         wake_up(&res->wq);
334
335         return status;
336 }
337
338 /* sends DLM_CONVERT_LOCK_MSG to master site
339  * locking:
340  *   caller needs:  none
341  *   taken:         none
342  *   held on exit:  none
343  * returns: DLM_NOLOCKMGR, status from remote node
344  */
345 static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
346                                            struct dlm_lock_resource *res,
347                                            struct dlm_lock *lock, int flags, int type)
348 {
349         struct dlm_convert_lock convert;
350         int tmpret;
351         enum dlm_status ret;
352         int status = 0;
353         struct kvec vec[2];
354         size_t veclen = 1;
355
356         mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
357
358         memset(&convert, 0, sizeof(struct dlm_convert_lock));
359         convert.node_idx = dlm->node_num;
360         convert.requested_type = type;
361         convert.cookie = lock->ml.cookie;
362         convert.namelen = res->lockname.len;
363         convert.flags = cpu_to_be32(flags);
364         memcpy(convert.name, res->lockname.name, convert.namelen);
365
366         vec[0].iov_len = sizeof(struct dlm_convert_lock);
367         vec[0].iov_base = &convert;
368
369         if (flags & LKM_PUT_LVB) {
370                 /* extra data to send if we are updating lvb */
371                 vec[1].iov_len = DLM_LVB_LEN;
372                 vec[1].iov_base = lock->lksb->lvb;
373                 veclen++;
374         }
375
376         tmpret = o2net_send_message_vec(DLM_CONVERT_LOCK_MSG, dlm->key,
377                                         vec, veclen, res->owner, &status);
378         if (tmpret >= 0) {
379                 // successfully sent and received
380                 ret = status;  // this is already a dlm_status
381                 if (ret == DLM_RECOVERING) {
382                         mlog(0, "node %u returned DLM_RECOVERING from convert "
383                              "message!\n", res->owner);
384                 } else if (ret == DLM_MIGRATING) {
385                         mlog(0, "node %u returned DLM_MIGRATING from convert "
386                              "message!\n", res->owner);
387                 } else if (ret == DLM_FORWARD) {
388                         mlog(0, "node %u returned DLM_FORWARD from convert "
389                              "message!\n", res->owner);
390                 } else if (ret != DLM_NORMAL && ret != DLM_NOTQUEUED)
391                         dlm_error(ret);
392         } else {
393                 mlog_errno(tmpret);
394                 if (dlm_is_host_down(tmpret)) {
395                         ret = DLM_RECOVERING;
396                         mlog(0, "node %u died so returning DLM_RECOVERING "
397                              "from convert message!\n", res->owner);
398                 } else {
399                         ret = dlm_err_to_dlm_status(tmpret);
400                 }
401         }
402
403         return ret;
404 }
405
406 /* handler for DLM_CONVERT_LOCK_MSG on master site
407  * locking:
408  *   caller needs:  none
409  *   taken:         takes and drop res->spinlock
410  *   held on exit:  none
411  * returns: DLM_NORMAL, DLM_IVLOCKID, DLM_BADARGS,
412  *          status from __dlmconvert_master
413  */
414 int dlm_convert_lock_handler(struct o2net_msg *msg, u32 len, void *data)
415 {
416         struct dlm_ctxt *dlm = data;
417         struct dlm_convert_lock *cnv = (struct dlm_convert_lock *)msg->buf;
418         struct dlm_lock_resource *res = NULL;
419         struct list_head *iter;
420         struct dlm_lock *lock = NULL;
421         struct dlm_lockstatus *lksb;
422         enum dlm_status status = DLM_NORMAL;
423         u32 flags;
424         int call_ast = 0, kick_thread = 0, ast_reserved = 0;
425
426         if (!dlm_grab(dlm)) {
427                 dlm_error(DLM_REJECTED);
428                 return DLM_REJECTED;
429         }
430
431         mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
432                         "Domain %s not fully joined!\n", dlm->name);
433
434         if (cnv->namelen > DLM_LOCKID_NAME_MAX) {
435                 status = DLM_IVBUFLEN;
436                 dlm_error(status);
437                 goto leave;
438         }
439
440         flags = be32_to_cpu(cnv->flags);
441
442         if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
443              (LKM_PUT_LVB|LKM_GET_LVB)) {
444                 mlog(ML_ERROR, "both PUT and GET lvb specified\n");
445                 status = DLM_BADARGS;
446                 goto leave;
447         }
448
449         mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
450              (flags & LKM_GET_LVB ? "get lvb" : "none"));
451
452         status = DLM_IVLOCKID;
453         res = dlm_lookup_lockres(dlm, cnv->name, cnv->namelen);
454         if (!res) {
455                 dlm_error(status);
456                 goto leave;
457         }
458
459         spin_lock(&res->spinlock);
460         list_for_each(iter, &res->granted) {
461                 lock = list_entry(iter, struct dlm_lock, list);
462                 if (lock->ml.cookie == cnv->cookie &&
463                     lock->ml.node == cnv->node_idx) {
464                         dlm_lock_get(lock);
465                         break;
466                 }
467                 lock = NULL;
468         }
469         spin_unlock(&res->spinlock);
470         if (!lock) {
471                 status = DLM_IVLOCKID;
472                 dlm_error(status);
473                 goto leave;
474         }
475
476         /* found the lock */
477         lksb = lock->lksb;
478
479         /* see if caller needed to get/put lvb */
480         if (flags & LKM_PUT_LVB) {
481                 BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
482                 lksb->flags |= DLM_LKSB_PUT_LVB;
483                 memcpy(&lksb->lvb[0], &cnv->lvb[0], DLM_LVB_LEN);
484         } else if (flags & LKM_GET_LVB) {
485                 BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
486                 lksb->flags |= DLM_LKSB_GET_LVB;
487         }
488
489         spin_lock(&res->spinlock);
490         status = __dlm_lockres_state_to_status(res);
491         if (status == DLM_NORMAL) {
492                 __dlm_lockres_reserve_ast(res);
493                 ast_reserved = 1;
494                 res->state |= DLM_LOCK_RES_IN_PROGRESS;
495                 status = __dlmconvert_master(dlm, res, lock, flags,
496                                              cnv->requested_type,
497                                              &call_ast, &kick_thread);
498                 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
499         }
500         spin_unlock(&res->spinlock);
501
502         if (status != DLM_NORMAL) {
503                 if (status != DLM_NOTQUEUED)
504                         dlm_error(status);
505                 lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
506         }
507
508 leave:
509         if (!lock)
510                 mlog(ML_ERROR, "did not find lock to convert on grant queue! "
511                                "cookie=%"MLFu64"\n",
512                      cnv->cookie);
513         else
514                 dlm_lock_put(lock);
515
516         /* either queue the ast or release it, if reserved */
517         if (call_ast)
518                 dlm_queue_ast(dlm, lock);
519         else if (ast_reserved)
520                 dlm_lockres_release_ast(dlm, res);
521
522         if (kick_thread)
523                 dlm_kick_thread(dlm, res);
524
525         if (res)
526                 dlm_lockres_put(res);
527
528         dlm_put(dlm);
529
530         return status;
531 }