093200f4d057a31e0759353d751c5743a694a339
[pandora-kernel.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/highmem.h>
32 #include <linux/init.h>
33 #include <linux/sysctl.h>
34 #include <linux/random.h>
35 #include <linux/blkdev.h>
36 #include <linux/socket.h>
37 #include <linux/inet.h>
38 #include <linux/spinlock.h>
39
40
41 #include "cluster/heartbeat.h"
42 #include "cluster/nodemanager.h"
43 #include "cluster/tcp.h"
44
45 #include "dlmapi.h"
46 #include "dlmcommon.h"
47
48 #include "dlmconvert.h"
49
50 #define MLOG_MASK_PREFIX ML_DLM
51 #include "cluster/masklog.h"
52
53 /* NOTE: __dlmconvert_master is the only function in here that
54  * needs a spinlock held on entry (res->spinlock) and it is the
55  * only one that holds a lock on exit (res->spinlock).
56  * All other functions in here need no locks and drop all of
57  * the locks that they acquire. */
58 static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
59                                            struct dlm_lock_resource *res,
60                                            struct dlm_lock *lock, int flags,
61                                            int type, int *call_ast,
62                                            int *kick_thread);
63 static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
64                                            struct dlm_lock_resource *res,
65                                            struct dlm_lock *lock, int flags, int type);
66
67 /*
68  * this is only called directly by dlmlock(), and only when the
69  * local node is the owner of the lockres
70  * locking:
71  *   caller needs:  none
72  *   taken:         takes and drops res->spinlock
73  *   held on exit:  none
74  * returns: see __dlmconvert_master
75  */
76 enum dlm_status dlmconvert_master(struct dlm_ctxt *dlm,
77                                   struct dlm_lock_resource *res,
78                                   struct dlm_lock *lock, int flags, int type)
79 {
80         int call_ast = 0, kick_thread = 0;
81         enum dlm_status status;
82
83         spin_lock(&res->spinlock);
84         /* we are not in a network handler, this is fine */
85         __dlm_wait_on_lockres(res);
86         __dlm_lockres_reserve_ast(res);
87         res->state |= DLM_LOCK_RES_IN_PROGRESS;
88
89         status = __dlmconvert_master(dlm, res, lock, flags, type,
90                                      &call_ast, &kick_thread);
91
92         res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
93         spin_unlock(&res->spinlock);
94         wake_up(&res->wq);
95         if (status != DLM_NORMAL && status != DLM_NOTQUEUED)
96                 dlm_error(status);
97
98         /* either queue the ast or release it */
99         if (call_ast)
100                 dlm_queue_ast(dlm, lock);
101         else
102                 dlm_lockres_release_ast(dlm, res);
103
104         if (kick_thread)
105                 dlm_kick_thread(dlm, res);
106
107         return status;
108 }
109
110 /* performs lock conversion at the lockres master site
111  * locking:
112  *   caller needs:  res->spinlock
113  *   taken:         takes and drops lock->spinlock
114  *   held on exit:  res->spinlock
115  * returns: DLM_NORMAL, DLM_NOTQUEUED, DLM_DENIED
116  *   call_ast: whether ast should be called for this lock
117  *   kick_thread: whether dlm_kick_thread should be called
118  */
119 static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
120                                            struct dlm_lock_resource *res,
121                                            struct dlm_lock *lock, int flags,
122                                            int type, int *call_ast,
123                                            int *kick_thread)
124 {
125         enum dlm_status status = DLM_NORMAL;
126         struct list_head *iter;
127         struct dlm_lock *tmplock=NULL;
128
129         assert_spin_locked(&res->spinlock);
130
131         mlog(0, "type=%d, convert_type=%d, new convert_type=%d\n",
132              lock->ml.type, lock->ml.convert_type, type);
133
134         spin_lock(&lock->spinlock);
135
136         /* already converting? */
137         if (lock->ml.convert_type != LKM_IVMODE) {
138                 mlog(ML_ERROR, "attempted to convert a lock with a lock "
139                      "conversion pending\n");
140                 status = DLM_DENIED;
141                 goto unlock_exit;
142         }
143
144         /* must be on grant queue to convert */
145         if (!dlm_lock_on_list(&res->granted, lock)) {
146                 mlog(ML_ERROR, "attempted to convert a lock not on grant "
147                      "queue\n");
148                 status = DLM_DENIED;
149                 goto unlock_exit;
150         }
151
152         if (flags & LKM_VALBLK) {
153                 switch (lock->ml.type) {
154                         case LKM_EXMODE:
155                                 /* EX + LKM_VALBLK + convert == set lvb */
156                                 mlog(0, "will set lvb: converting %s->%s\n",
157                                      dlm_lock_mode_name(lock->ml.type),
158                                      dlm_lock_mode_name(type));
159                                 lock->lksb->flags |= DLM_LKSB_PUT_LVB;
160                                 break;
161                         case LKM_PRMODE:
162                         case LKM_NLMODE:
163                                 /* refetch if new level is not NL */
164                                 if (type > LKM_NLMODE) {
165                                         mlog(0, "will fetch new value into "
166                                              "lvb: converting %s->%s\n",
167                                              dlm_lock_mode_name(lock->ml.type),
168                                              dlm_lock_mode_name(type));
169                                         lock->lksb->flags |= DLM_LKSB_GET_LVB;
170                                 } else {
171                                         mlog(0, "will NOT fetch new value "
172                                              "into lvb: converting %s->%s\n",
173                                              dlm_lock_mode_name(lock->ml.type),
174                                              dlm_lock_mode_name(type));
175                                         flags &= ~(LKM_VALBLK);
176                                 }
177                                 break;
178                 }
179         }
180
181
182         /* in-place downconvert? */
183         if (type <= lock->ml.type)
184                 goto grant;
185
186         /* upconvert from here on */
187         status = DLM_NORMAL;
188         list_for_each(iter, &res->granted) {
189                 tmplock = list_entry(iter, struct dlm_lock, list);
190                 if (tmplock == lock)
191                         continue;
192                 if (!dlm_lock_compatible(tmplock->ml.type, type))
193                         goto switch_queues;
194         }
195
196         list_for_each(iter, &res->converting) {
197                 tmplock = list_entry(iter, struct dlm_lock, list);
198                 if (!dlm_lock_compatible(tmplock->ml.type, type))
199                         goto switch_queues;
200                 /* existing conversion requests take precedence */
201                 if (!dlm_lock_compatible(tmplock->ml.convert_type, type))
202                         goto switch_queues;
203         }
204
205         /* fall thru to grant */
206
207 grant:
208         mlog(0, "res %.*s, granting %s lock\n", res->lockname.len,
209              res->lockname.name, dlm_lock_mode_name(type));
210         /* immediately grant the new lock type */
211         lock->lksb->status = DLM_NORMAL;
212         if (lock->ml.node == dlm->node_num)
213                 mlog(0, "doing in-place convert for nonlocal lock\n");
214         lock->ml.type = type;
215         if (lock->lksb->flags & DLM_LKSB_PUT_LVB)
216                 memcpy(res->lvb, lock->lksb->lvb, DLM_LVB_LEN);
217
218         status = DLM_NORMAL;
219         *call_ast = 1;
220         goto unlock_exit;
221
222 switch_queues:
223         if (flags & LKM_NOQUEUE) {
224                 mlog(0, "failed to convert NOQUEUE lock %.*s from "
225                      "%d to %d...\n", res->lockname.len, res->lockname.name,
226                      lock->ml.type, type);
227                 status = DLM_NOTQUEUED;
228                 goto unlock_exit;
229         }
230         mlog(0, "res %.*s, queueing...\n", res->lockname.len,
231              res->lockname.name);
232
233         lock->ml.convert_type = type;
234         /* do not alter lock refcount.  switching lists. */
235         list_move_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_move_tail(&lock->list, &res->granted);
252         lock->ml.convert_type = LKM_IVMODE;
253         lock->lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
254 }
255
256 /* messages the master site to do lock conversion
257  * locking:
258  *   caller needs:  none
259  *   taken:         takes and drops res->spinlock, uses DLM_LOCK_RES_IN_PROGRESS
260  *   held on exit:  none
261  * returns: DLM_NORMAL, DLM_RECOVERING, status from remote node
262  */
263 enum dlm_status dlmconvert_remote(struct dlm_ctxt *dlm,
264                                   struct dlm_lock_resource *res,
265                                   struct dlm_lock *lock, int flags, int type)
266 {
267         enum dlm_status status;
268         u8 old_owner = res->owner;
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=%u:%llu, conv=%d)\n",
288                      dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
289                      dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
290                      lock->ml.convert_type);
291                 status = DLM_DENIED;
292                 goto bail;
293         }
294         res->state |= DLM_LOCK_RES_IN_PROGRESS;
295         /* move lock to local convert queue */
296         /* do not alter lock refcount.  switching lists. */
297         list_move_tail(&lock->list, &res->converting);
298         lock->convert_pending = 1;
299         lock->ml.convert_type = type;
300
301         if (flags & LKM_VALBLK) {
302                 if (lock->ml.type == LKM_EXMODE) {
303                         flags |= LKM_PUT_LVB;
304                         lock->lksb->flags |= DLM_LKSB_PUT_LVB;
305                 } else {
306                         if (lock->ml.convert_type == LKM_NLMODE)
307                                 flags &= ~LKM_VALBLK;
308                         else {
309                                 flags |= LKM_GET_LVB;
310                                 lock->lksb->flags |= DLM_LKSB_GET_LVB;
311                         }
312                 }
313         }
314         spin_unlock(&res->spinlock);
315
316         /* no locks held here.
317          * need to wait for a reply as to whether it got queued or not. */
318         status = dlm_send_remote_convert_request(dlm, res, lock, flags, type);
319
320         spin_lock(&res->spinlock);
321         res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
322         lock->convert_pending = 0;
323         /* if it failed, move it back to granted queue.
324          * if master returns DLM_NORMAL and then down before sending ast,
325          * it may have already been moved to granted queue, reset to
326          * DLM_RECOVERING and retry convert */
327         if (status != DLM_NORMAL) {
328                 if (status != DLM_NOTQUEUED)
329                         dlm_error(status);
330                 dlm_revert_pending_convert(res, lock);
331         } else if ((res->state & DLM_LOCK_RES_RECOVERING) ||
332                         (old_owner != res->owner)) {
333                 mlog(0, "res %.*s is in recovering or has been recovered.\n",
334                                 res->lockname.len, res->lockname.name);
335                 status = DLM_RECOVERING;
336         }
337 bail:
338         spin_unlock(&res->spinlock);
339
340         /* TODO: should this be a wake_one? */
341         /* wake up any IN_PROGRESS waiters */
342         wake_up(&res->wq);
343
344         return status;
345 }
346
347 /* sends DLM_CONVERT_LOCK_MSG to master site
348  * locking:
349  *   caller needs:  none
350  *   taken:         none
351  *   held on exit:  none
352  * returns: DLM_NOLOCKMGR, status from remote node
353  */
354 static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
355                                            struct dlm_lock_resource *res,
356                                            struct dlm_lock *lock, int flags, int type)
357 {
358         struct dlm_convert_lock convert;
359         int tmpret;
360         enum dlm_status ret;
361         int status = 0;
362         struct kvec vec[2];
363         size_t veclen = 1;
364
365         mlog(0, "%.*s\n", res->lockname.len, res->lockname.name);
366
367         memset(&convert, 0, sizeof(struct dlm_convert_lock));
368         convert.node_idx = dlm->node_num;
369         convert.requested_type = type;
370         convert.cookie = lock->ml.cookie;
371         convert.namelen = res->lockname.len;
372         convert.flags = cpu_to_be32(flags);
373         memcpy(convert.name, res->lockname.name, convert.namelen);
374
375         vec[0].iov_len = sizeof(struct dlm_convert_lock);
376         vec[0].iov_base = &convert;
377
378         if (flags & LKM_PUT_LVB) {
379                 /* extra data to send if we are updating lvb */
380                 vec[1].iov_len = DLM_LVB_LEN;
381                 vec[1].iov_base = lock->lksb->lvb;
382                 veclen++;
383         }
384
385         tmpret = o2net_send_message_vec(DLM_CONVERT_LOCK_MSG, dlm->key,
386                                         vec, veclen, res->owner, &status);
387         if (tmpret >= 0) {
388                 // successfully sent and received
389                 ret = status;  // this is already a dlm_status
390                 if (ret == DLM_RECOVERING) {
391                         mlog(0, "node %u returned DLM_RECOVERING from convert "
392                              "message!\n", res->owner);
393                 } else if (ret == DLM_MIGRATING) {
394                         mlog(0, "node %u returned DLM_MIGRATING from convert "
395                              "message!\n", res->owner);
396                 } else if (ret == DLM_FORWARD) {
397                         mlog(0, "node %u returned DLM_FORWARD from convert "
398                              "message!\n", res->owner);
399                 } else if (ret != DLM_NORMAL && ret != DLM_NOTQUEUED)
400                         dlm_error(ret);
401         } else {
402                 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
403                      "node %u\n", tmpret, DLM_CONVERT_LOCK_MSG, dlm->key,
404                      res->owner);
405                 if (dlm_is_host_down(tmpret)) {
406                         /* instead of logging the same network error over
407                          * and over, sleep here and wait for the heartbeat
408                          * to notice the node is dead.  times out after 5s. */
409                         dlm_wait_for_node_death(dlm, res->owner,
410                                                 DLM_NODE_DEATH_WAIT_MAX);
411                         ret = DLM_RECOVERING;
412                         mlog(0, "node %u died so returning DLM_RECOVERING "
413                              "from convert message!\n", res->owner);
414                 } else {
415                         ret = dlm_err_to_dlm_status(tmpret);
416                 }
417         }
418
419         return ret;
420 }
421
422 /* handler for DLM_CONVERT_LOCK_MSG on master site
423  * locking:
424  *   caller needs:  none
425  *   taken:         takes and drop res->spinlock
426  *   held on exit:  none
427  * returns: DLM_NORMAL, DLM_IVLOCKID, DLM_BADARGS,
428  *          status from __dlmconvert_master
429  */
430 int dlm_convert_lock_handler(struct o2net_msg *msg, u32 len, void *data,
431                              void **ret_data)
432 {
433         struct dlm_ctxt *dlm = data;
434         struct dlm_convert_lock *cnv = (struct dlm_convert_lock *)msg->buf;
435         struct dlm_lock_resource *res = NULL;
436         struct list_head *iter;
437         struct dlm_lock *lock = NULL;
438         struct dlm_lockstatus *lksb;
439         enum dlm_status status = DLM_NORMAL;
440         u32 flags;
441         int call_ast = 0, kick_thread = 0, ast_reserved = 0, wake = 0;
442
443         if (!dlm_grab(dlm)) {
444                 dlm_error(DLM_REJECTED);
445                 return DLM_REJECTED;
446         }
447
448         mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
449                         "Domain %s not fully joined!\n", dlm->name);
450
451         if (cnv->namelen > DLM_LOCKID_NAME_MAX) {
452                 status = DLM_IVBUFLEN;
453                 dlm_error(status);
454                 goto leave;
455         }
456
457         flags = be32_to_cpu(cnv->flags);
458
459         if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
460              (LKM_PUT_LVB|LKM_GET_LVB)) {
461                 mlog(ML_ERROR, "both PUT and GET lvb specified\n");
462                 status = DLM_BADARGS;
463                 goto leave;
464         }
465
466         mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
467              (flags & LKM_GET_LVB ? "get lvb" : "none"));
468
469         status = DLM_IVLOCKID;
470         res = dlm_lookup_lockres(dlm, cnv->name, cnv->namelen);
471         if (!res) {
472                 dlm_error(status);
473                 goto leave;
474         }
475
476         spin_lock(&res->spinlock);
477         status = __dlm_lockres_state_to_status(res);
478         if (status != DLM_NORMAL) {
479                 spin_unlock(&res->spinlock);
480                 dlm_error(status);
481                 goto leave;
482         }
483         list_for_each(iter, &res->granted) {
484                 lock = list_entry(iter, struct dlm_lock, list);
485                 if (lock->ml.cookie == cnv->cookie &&
486                     lock->ml.node == cnv->node_idx) {
487                         dlm_lock_get(lock);
488                         break;
489                 }
490                 lock = NULL;
491         }
492         spin_unlock(&res->spinlock);
493         if (!lock) {
494                 status = DLM_IVLOCKID;
495                 mlog(ML_ERROR, "did not find lock to convert on grant queue! "
496                                "cookie=%u:%llu\n",
497                      dlm_get_lock_cookie_node(be64_to_cpu(cnv->cookie)),
498                      dlm_get_lock_cookie_seq(be64_to_cpu(cnv->cookie)));
499                 dlm_print_one_lock_resource(res);
500                 goto leave;
501         }
502
503         /* found the lock */
504         lksb = lock->lksb;
505
506         /* see if caller needed to get/put lvb */
507         if (flags & LKM_PUT_LVB) {
508                 BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
509                 lksb->flags |= DLM_LKSB_PUT_LVB;
510                 memcpy(&lksb->lvb[0], &cnv->lvb[0], DLM_LVB_LEN);
511         } else if (flags & LKM_GET_LVB) {
512                 BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
513                 lksb->flags |= DLM_LKSB_GET_LVB;
514         }
515
516         spin_lock(&res->spinlock);
517         status = __dlm_lockres_state_to_status(res);
518         if (status == DLM_NORMAL) {
519                 __dlm_lockres_reserve_ast(res);
520                 ast_reserved = 1;
521                 res->state |= DLM_LOCK_RES_IN_PROGRESS;
522                 status = __dlmconvert_master(dlm, res, lock, flags,
523                                              cnv->requested_type,
524                                              &call_ast, &kick_thread);
525                 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
526                 wake = 1;
527         }
528         spin_unlock(&res->spinlock);
529         if (wake)
530                 wake_up(&res->wq);
531
532         if (status != DLM_NORMAL) {
533                 if (status != DLM_NOTQUEUED)
534                         dlm_error(status);
535                 lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
536         }
537
538 leave:
539         if (lock)
540                 dlm_lock_put(lock);
541
542         /* either queue the ast or release it, if reserved */
543         if (call_ast)
544                 dlm_queue_ast(dlm, lock);
545         else if (ast_reserved)
546                 dlm_lockres_release_ast(dlm, res);
547
548         if (kick_thread)
549                 dlm_kick_thread(dlm, res);
550
551         if (res)
552                 dlm_lockres_put(res);
553
554         dlm_put(dlm);
555
556         return status;
557 }