c7653bb343e1a4d43d5efaacc1030d61b2d0c2d8
[deliverable/linux.git] / fs / ocfs2 / dlmglue.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmglue.c
5 *
6 * Code which implements an OCFS2 specific interface to our DLM.
7 *
8 * Copyright (C) 2003, 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 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/mm.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34
35 #define MLOG_MASK_PREFIX ML_DLM_GLUE
36 #include <cluster/masklog.h>
37
38 #include "ocfs2.h"
39 #include "ocfs2_lockingver.h"
40
41 #include "alloc.h"
42 #include "dcache.h"
43 #include "dlmglue.h"
44 #include "extent_map.h"
45 #include "file.h"
46 #include "heartbeat.h"
47 #include "inode.h"
48 #include "journal.h"
49 #include "stackglue.h"
50 #include "slot_map.h"
51 #include "super.h"
52 #include "uptodate.h"
53
54 #include "buffer_head_io.h"
55
56 struct ocfs2_mask_waiter {
57 struct list_head mw_item;
58 int mw_status;
59 struct completion mw_complete;
60 unsigned long mw_mask;
61 unsigned long mw_goal;
62 };
63
64 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
65 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
66 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
67
68 /*
69 * Return value from ->downconvert_worker functions.
70 *
71 * These control the precise actions of ocfs2_unblock_lock()
72 * and ocfs2_process_blocked_lock()
73 *
74 */
75 enum ocfs2_unblock_action {
76 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
77 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
78 * ->post_unlock callback */
79 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
80 * ->post_unlock() callback. */
81 };
82
83 struct ocfs2_unblock_ctl {
84 int requeue;
85 enum ocfs2_unblock_action unblock_action;
86 };
87
88 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
89 int new_level);
90 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
91
92 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
93 int blocking);
94
95 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
96 int blocking);
97
98 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
99 struct ocfs2_lock_res *lockres);
100
101
102 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
103
104 /* This aids in debugging situations where a bad LVB might be involved. */
105 static void ocfs2_dump_meta_lvb_info(u64 level,
106 const char *function,
107 unsigned int line,
108 struct ocfs2_lock_res *lockres)
109 {
110 struct ocfs2_meta_lvb *lvb =
111 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
112
113 mlog(level, "LVB information for %s (called from %s:%u):\n",
114 lockres->l_name, function, line);
115 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
116 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
117 be32_to_cpu(lvb->lvb_igeneration));
118 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
119 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
120 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
121 be16_to_cpu(lvb->lvb_imode));
122 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
123 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
124 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
125 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
126 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
127 be32_to_cpu(lvb->lvb_iattr));
128 }
129
130
131 /*
132 * OCFS2 Lock Resource Operations
133 *
134 * These fine tune the behavior of the generic dlmglue locking infrastructure.
135 *
136 * The most basic of lock types can point ->l_priv to their respective
137 * struct ocfs2_super and allow the default actions to manage things.
138 *
139 * Right now, each lock type also needs to implement an init function,
140 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
141 * should be called when the lock is no longer needed (i.e., object
142 * destruction time).
143 */
144 struct ocfs2_lock_res_ops {
145 /*
146 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
147 * this callback if ->l_priv is not an ocfs2_super pointer
148 */
149 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
150
151 /*
152 * Optionally called in the downconvert thread after a
153 * successful downconvert. The lockres will not be referenced
154 * after this callback is called, so it is safe to free
155 * memory, etc.
156 *
157 * The exact semantics of when this is called are controlled
158 * by ->downconvert_worker()
159 */
160 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
161
162 /*
163 * Allow a lock type to add checks to determine whether it is
164 * safe to downconvert a lock. Return 0 to re-queue the
165 * downconvert at a later time, nonzero to continue.
166 *
167 * For most locks, the default checks that there are no
168 * incompatible holders are sufficient.
169 *
170 * Called with the lockres spinlock held.
171 */
172 int (*check_downconvert)(struct ocfs2_lock_res *, int);
173
174 /*
175 * Allows a lock type to populate the lock value block. This
176 * is called on downconvert, and when we drop a lock.
177 *
178 * Locks that want to use this should set LOCK_TYPE_USES_LVB
179 * in the flags field.
180 *
181 * Called with the lockres spinlock held.
182 */
183 void (*set_lvb)(struct ocfs2_lock_res *);
184
185 /*
186 * Called from the downconvert thread when it is determined
187 * that a lock will be downconverted. This is called without
188 * any locks held so the function can do work that might
189 * schedule (syncing out data, etc).
190 *
191 * This should return any one of the ocfs2_unblock_action
192 * values, depending on what it wants the thread to do.
193 */
194 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
195
196 /*
197 * LOCK_TYPE_* flags which describe the specific requirements
198 * of a lock type. Descriptions of each individual flag follow.
199 */
200 int flags;
201 };
202
203 /*
204 * Some locks want to "refresh" potentially stale data when a
205 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
206 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
207 * individual lockres l_flags member from the ast function. It is
208 * expected that the locking wrapper will clear the
209 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
210 */
211 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
212
213 /*
214 * Indicate that a lock type makes use of the lock value block. The
215 * ->set_lvb lock type callback must be defined.
216 */
217 #define LOCK_TYPE_USES_LVB 0x2
218
219 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
220 .get_osb = ocfs2_get_inode_osb,
221 .flags = 0,
222 };
223
224 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
225 .get_osb = ocfs2_get_inode_osb,
226 .check_downconvert = ocfs2_check_meta_downconvert,
227 .set_lvb = ocfs2_set_meta_lvb,
228 .downconvert_worker = ocfs2_data_convert_worker,
229 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
230 };
231
232 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
233 .flags = LOCK_TYPE_REQUIRES_REFRESH,
234 };
235
236 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
237 .flags = 0,
238 };
239
240 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
241 .get_osb = ocfs2_get_dentry_osb,
242 .post_unlock = ocfs2_dentry_post_unlock,
243 .downconvert_worker = ocfs2_dentry_convert_worker,
244 .flags = 0,
245 };
246
247 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
248 .get_osb = ocfs2_get_inode_osb,
249 .flags = 0,
250 };
251
252 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
253 .get_osb = ocfs2_get_file_osb,
254 .flags = 0,
255 };
256
257 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
258 {
259 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
260 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
261 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
262 }
263
264 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
265 {
266 BUG_ON(!ocfs2_is_inode_lock(lockres));
267
268 return (struct inode *) lockres->l_priv;
269 }
270
271 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
272 {
273 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
274
275 return (struct ocfs2_dentry_lock *)lockres->l_priv;
276 }
277
278 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
279 {
280 if (lockres->l_ops->get_osb)
281 return lockres->l_ops->get_osb(lockres);
282
283 return (struct ocfs2_super *)lockres->l_priv;
284 }
285
286 static int ocfs2_lock_create(struct ocfs2_super *osb,
287 struct ocfs2_lock_res *lockres,
288 int level,
289 u32 dlm_flags);
290 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
291 int wanted);
292 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
293 struct ocfs2_lock_res *lockres,
294 int level);
295 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
296 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
297 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
298 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
299 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
300 struct ocfs2_lock_res *lockres);
301 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
302 int convert);
303 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
304 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
305 _err, _func, _lockres->l_name); \
306 } while (0)
307 static int ocfs2_downconvert_thread(void *arg);
308 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
309 struct ocfs2_lock_res *lockres);
310 static int ocfs2_inode_lock_update(struct inode *inode,
311 struct buffer_head **bh);
312 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
313 static inline int ocfs2_highest_compat_lock_level(int level);
314 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
315 int new_level);
316 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
317 struct ocfs2_lock_res *lockres,
318 int new_level,
319 int lvb);
320 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres);
322 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
323 struct ocfs2_lock_res *lockres);
324
325
326 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
327 u64 blkno,
328 u32 generation,
329 char *name)
330 {
331 int len;
332
333 mlog_entry_void();
334
335 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
336
337 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
338 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
339 (long long)blkno, generation);
340
341 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
342
343 mlog(0, "built lock resource with name: %s\n", name);
344
345 mlog_exit_void();
346 }
347
348 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
349
350 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
351 struct ocfs2_dlm_debug *dlm_debug)
352 {
353 mlog(0, "Add tracking for lockres %s\n", res->l_name);
354
355 spin_lock(&ocfs2_dlm_tracking_lock);
356 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
357 spin_unlock(&ocfs2_dlm_tracking_lock);
358 }
359
360 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
361 {
362 spin_lock(&ocfs2_dlm_tracking_lock);
363 if (!list_empty(&res->l_debug_list))
364 list_del_init(&res->l_debug_list);
365 spin_unlock(&ocfs2_dlm_tracking_lock);
366 }
367
368 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *res,
370 enum ocfs2_lock_type type,
371 struct ocfs2_lock_res_ops *ops,
372 void *priv)
373 {
374 res->l_type = type;
375 res->l_ops = ops;
376 res->l_priv = priv;
377
378 res->l_level = DLM_LOCK_IV;
379 res->l_requested = DLM_LOCK_IV;
380 res->l_blocking = DLM_LOCK_IV;
381 res->l_action = OCFS2_AST_INVALID;
382 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
383
384 res->l_flags = OCFS2_LOCK_INITIALIZED;
385
386 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
387 }
388
389 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
390 {
391 /* This also clears out the lock status block */
392 memset(res, 0, sizeof(struct ocfs2_lock_res));
393 spin_lock_init(&res->l_lock);
394 init_waitqueue_head(&res->l_event);
395 INIT_LIST_HEAD(&res->l_blocked_list);
396 INIT_LIST_HEAD(&res->l_mask_waiters);
397 }
398
399 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
400 enum ocfs2_lock_type type,
401 unsigned int generation,
402 struct inode *inode)
403 {
404 struct ocfs2_lock_res_ops *ops;
405
406 switch(type) {
407 case OCFS2_LOCK_TYPE_RW:
408 ops = &ocfs2_inode_rw_lops;
409 break;
410 case OCFS2_LOCK_TYPE_META:
411 ops = &ocfs2_inode_inode_lops;
412 break;
413 case OCFS2_LOCK_TYPE_OPEN:
414 ops = &ocfs2_inode_open_lops;
415 break;
416 default:
417 mlog_bug_on_msg(1, "type: %d\n", type);
418 ops = NULL; /* thanks, gcc */
419 break;
420 };
421
422 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
423 generation, res->l_name);
424 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
425 }
426
427 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
428 {
429 struct inode *inode = ocfs2_lock_res_inode(lockres);
430
431 return OCFS2_SB(inode->i_sb);
432 }
433
434 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
435 {
436 struct ocfs2_file_private *fp = lockres->l_priv;
437
438 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
439 }
440
441 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
442 {
443 __be64 inode_blkno_be;
444
445 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
446 sizeof(__be64));
447
448 return be64_to_cpu(inode_blkno_be);
449 }
450
451 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
452 {
453 struct ocfs2_dentry_lock *dl = lockres->l_priv;
454
455 return OCFS2_SB(dl->dl_inode->i_sb);
456 }
457
458 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
459 u64 parent, struct inode *inode)
460 {
461 int len;
462 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
463 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
464 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
465
466 ocfs2_lock_res_init_once(lockres);
467
468 /*
469 * Unfortunately, the standard lock naming scheme won't work
470 * here because we have two 16 byte values to use. Instead,
471 * we'll stuff the inode number as a binary value. We still
472 * want error prints to show something without garbling the
473 * display, so drop a null byte in there before the inode
474 * number. A future version of OCFS2 will likely use all
475 * binary lock names. The stringified names have been a
476 * tremendous aid in debugging, but now that the debugfs
477 * interface exists, we can mangle things there if need be.
478 *
479 * NOTE: We also drop the standard "pad" value (the total lock
480 * name size stays the same though - the last part is all
481 * zeros due to the memset in ocfs2_lock_res_init_once()
482 */
483 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
484 "%c%016llx",
485 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
486 (long long)parent);
487
488 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
489
490 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
491 sizeof(__be64));
492
493 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
494 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
495 dl);
496 }
497
498 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
499 struct ocfs2_super *osb)
500 {
501 /* Superblock lockres doesn't come from a slab so we call init
502 * once on it manually. */
503 ocfs2_lock_res_init_once(res);
504 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
505 0, res->l_name);
506 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
507 &ocfs2_super_lops, osb);
508 }
509
510 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
511 struct ocfs2_super *osb)
512 {
513 /* Rename lockres doesn't come from a slab so we call init
514 * once on it manually. */
515 ocfs2_lock_res_init_once(res);
516 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
517 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
518 &ocfs2_rename_lops, osb);
519 }
520
521 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
522 struct ocfs2_file_private *fp)
523 {
524 struct inode *inode = fp->fp_file->f_mapping->host;
525 struct ocfs2_inode_info *oi = OCFS2_I(inode);
526
527 ocfs2_lock_res_init_once(lockres);
528 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
529 inode->i_generation, lockres->l_name);
530 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
531 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
532 fp);
533 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
534 }
535
536 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
537 {
538 mlog_entry_void();
539
540 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
541 return;
542
543 ocfs2_remove_lockres_tracking(res);
544
545 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
546 "Lockres %s is on the blocked list\n",
547 res->l_name);
548 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
549 "Lockres %s has mask waiters pending\n",
550 res->l_name);
551 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
552 "Lockres %s is locked\n",
553 res->l_name);
554 mlog_bug_on_msg(res->l_ro_holders,
555 "Lockres %s has %u ro holders\n",
556 res->l_name, res->l_ro_holders);
557 mlog_bug_on_msg(res->l_ex_holders,
558 "Lockres %s has %u ex holders\n",
559 res->l_name, res->l_ex_holders);
560
561 /* Need to clear out the lock status block for the dlm */
562 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
563
564 res->l_flags = 0UL;
565 mlog_exit_void();
566 }
567
568 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
569 int level)
570 {
571 mlog_entry_void();
572
573 BUG_ON(!lockres);
574
575 switch(level) {
576 case DLM_LOCK_EX:
577 lockres->l_ex_holders++;
578 break;
579 case DLM_LOCK_PR:
580 lockres->l_ro_holders++;
581 break;
582 default:
583 BUG();
584 }
585
586 mlog_exit_void();
587 }
588
589 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
590 int level)
591 {
592 mlog_entry_void();
593
594 BUG_ON(!lockres);
595
596 switch(level) {
597 case DLM_LOCK_EX:
598 BUG_ON(!lockres->l_ex_holders);
599 lockres->l_ex_holders--;
600 break;
601 case DLM_LOCK_PR:
602 BUG_ON(!lockres->l_ro_holders);
603 lockres->l_ro_holders--;
604 break;
605 default:
606 BUG();
607 }
608 mlog_exit_void();
609 }
610
611 /* WARNING: This function lives in a world where the only three lock
612 * levels are EX, PR, and NL. It *will* have to be adjusted when more
613 * lock types are added. */
614 static inline int ocfs2_highest_compat_lock_level(int level)
615 {
616 int new_level = DLM_LOCK_EX;
617
618 if (level == DLM_LOCK_EX)
619 new_level = DLM_LOCK_NL;
620 else if (level == DLM_LOCK_PR)
621 new_level = DLM_LOCK_PR;
622 return new_level;
623 }
624
625 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
626 unsigned long newflags)
627 {
628 struct ocfs2_mask_waiter *mw, *tmp;
629
630 assert_spin_locked(&lockres->l_lock);
631
632 lockres->l_flags = newflags;
633
634 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
635 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
636 continue;
637
638 list_del_init(&mw->mw_item);
639 mw->mw_status = 0;
640 complete(&mw->mw_complete);
641 }
642 }
643 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
644 {
645 lockres_set_flags(lockres, lockres->l_flags | or);
646 }
647 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
648 unsigned long clear)
649 {
650 lockres_set_flags(lockres, lockres->l_flags & ~clear);
651 }
652
653 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
654 {
655 mlog_entry_void();
656
657 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
658 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
659 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
660 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
661
662 lockres->l_level = lockres->l_requested;
663 if (lockres->l_level <=
664 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
665 lockres->l_blocking = DLM_LOCK_NL;
666 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
667 }
668 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
669
670 mlog_exit_void();
671 }
672
673 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
674 {
675 mlog_entry_void();
676
677 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
678 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
679
680 /* Convert from RO to EX doesn't really need anything as our
681 * information is already up to data. Convert from NL to
682 * *anything* however should mark ourselves as needing an
683 * update */
684 if (lockres->l_level == DLM_LOCK_NL &&
685 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
686 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
687
688 lockres->l_level = lockres->l_requested;
689 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
690
691 mlog_exit_void();
692 }
693
694 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
695 {
696 mlog_entry_void();
697
698 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
699 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
700
701 if (lockres->l_requested > DLM_LOCK_NL &&
702 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
703 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
704 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
705
706 lockres->l_level = lockres->l_requested;
707 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
708 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
709
710 mlog_exit_void();
711 }
712
713 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
714 int level)
715 {
716 int needs_downconvert = 0;
717 mlog_entry_void();
718
719 assert_spin_locked(&lockres->l_lock);
720
721 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
722
723 if (level > lockres->l_blocking) {
724 /* only schedule a downconvert if we haven't already scheduled
725 * one that goes low enough to satisfy the level we're
726 * blocking. this also catches the case where we get
727 * duplicate BASTs */
728 if (ocfs2_highest_compat_lock_level(level) <
729 ocfs2_highest_compat_lock_level(lockres->l_blocking))
730 needs_downconvert = 1;
731
732 lockres->l_blocking = level;
733 }
734
735 mlog_exit(needs_downconvert);
736 return needs_downconvert;
737 }
738
739 static void ocfs2_blocking_ast(void *opaque, int level)
740 {
741 struct ocfs2_lock_res *lockres = opaque;
742 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
743 int needs_downconvert;
744 unsigned long flags;
745
746 BUG_ON(level <= DLM_LOCK_NL);
747
748 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
749 lockres->l_name, level, lockres->l_level,
750 ocfs2_lock_type_string(lockres->l_type));
751
752 /*
753 * We can skip the bast for locks which don't enable caching -
754 * they'll be dropped at the earliest possible time anyway.
755 */
756 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
757 return;
758
759 spin_lock_irqsave(&lockres->l_lock, flags);
760 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
761 if (needs_downconvert)
762 ocfs2_schedule_blocked_lock(osb, lockres);
763 spin_unlock_irqrestore(&lockres->l_lock, flags);
764
765 wake_up(&lockres->l_event);
766
767 ocfs2_wake_downconvert_thread(osb);
768 }
769
770 static void ocfs2_locking_ast(void *opaque)
771 {
772 struct ocfs2_lock_res *lockres = opaque;
773 unsigned long flags;
774
775 spin_lock_irqsave(&lockres->l_lock, flags);
776
777 if (ocfs2_dlm_lock_status(&lockres->l_lksb)) {
778 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
779 lockres->l_name,
780 ocfs2_dlm_lock_status(&lockres->l_lksb));
781 spin_unlock_irqrestore(&lockres->l_lock, flags);
782 return;
783 }
784
785 switch(lockres->l_action) {
786 case OCFS2_AST_ATTACH:
787 ocfs2_generic_handle_attach_action(lockres);
788 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
789 break;
790 case OCFS2_AST_CONVERT:
791 ocfs2_generic_handle_convert_action(lockres);
792 break;
793 case OCFS2_AST_DOWNCONVERT:
794 ocfs2_generic_handle_downconvert_action(lockres);
795 break;
796 default:
797 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
798 "lockres flags = 0x%lx, unlock action: %u\n",
799 lockres->l_name, lockres->l_action, lockres->l_flags,
800 lockres->l_unlock_action);
801 BUG();
802 }
803
804 /* set it to something invalid so if we get called again we
805 * can catch it. */
806 lockres->l_action = OCFS2_AST_INVALID;
807
808 wake_up(&lockres->l_event);
809 spin_unlock_irqrestore(&lockres->l_lock, flags);
810 }
811
812 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
813 int convert)
814 {
815 unsigned long flags;
816
817 mlog_entry_void();
818 spin_lock_irqsave(&lockres->l_lock, flags);
819 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
820 if (convert)
821 lockres->l_action = OCFS2_AST_INVALID;
822 else
823 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
824 spin_unlock_irqrestore(&lockres->l_lock, flags);
825
826 wake_up(&lockres->l_event);
827 mlog_exit_void();
828 }
829
830 /* Note: If we detect another process working on the lock (i.e.,
831 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
832 * to do the right thing in that case.
833 */
834 static int ocfs2_lock_create(struct ocfs2_super *osb,
835 struct ocfs2_lock_res *lockres,
836 int level,
837 u32 dlm_flags)
838 {
839 int ret = 0;
840 unsigned long flags;
841
842 mlog_entry_void();
843
844 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
845 dlm_flags);
846
847 spin_lock_irqsave(&lockres->l_lock, flags);
848 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
849 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
850 spin_unlock_irqrestore(&lockres->l_lock, flags);
851 goto bail;
852 }
853
854 lockres->l_action = OCFS2_AST_ATTACH;
855 lockres->l_requested = level;
856 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
857 spin_unlock_irqrestore(&lockres->l_lock, flags);
858
859 ret = ocfs2_dlm_lock(osb->cconn,
860 level,
861 &lockres->l_lksb,
862 dlm_flags,
863 lockres->l_name,
864 OCFS2_LOCK_ID_MAX_LEN - 1,
865 lockres);
866 if (ret) {
867 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
868 ocfs2_recover_from_dlm_error(lockres, 1);
869 }
870
871 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
872
873 bail:
874 mlog_exit(ret);
875 return ret;
876 }
877
878 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
879 int flag)
880 {
881 unsigned long flags;
882 int ret;
883
884 spin_lock_irqsave(&lockres->l_lock, flags);
885 ret = lockres->l_flags & flag;
886 spin_unlock_irqrestore(&lockres->l_lock, flags);
887
888 return ret;
889 }
890
891 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
892
893 {
894 wait_event(lockres->l_event,
895 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
896 }
897
898 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
899
900 {
901 wait_event(lockres->l_event,
902 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
903 }
904
905 /* predict what lock level we'll be dropping down to on behalf
906 * of another node, and return true if the currently wanted
907 * level will be compatible with it. */
908 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
909 int wanted)
910 {
911 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
912
913 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
914 }
915
916 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
917 {
918 INIT_LIST_HEAD(&mw->mw_item);
919 init_completion(&mw->mw_complete);
920 }
921
922 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
923 {
924 wait_for_completion(&mw->mw_complete);
925 /* Re-arm the completion in case we want to wait on it again */
926 INIT_COMPLETION(mw->mw_complete);
927 return mw->mw_status;
928 }
929
930 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
931 struct ocfs2_mask_waiter *mw,
932 unsigned long mask,
933 unsigned long goal)
934 {
935 BUG_ON(!list_empty(&mw->mw_item));
936
937 assert_spin_locked(&lockres->l_lock);
938
939 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
940 mw->mw_mask = mask;
941 mw->mw_goal = goal;
942 }
943
944 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
945 * if the mask still hadn't reached its goal */
946 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
947 struct ocfs2_mask_waiter *mw)
948 {
949 unsigned long flags;
950 int ret = 0;
951
952 spin_lock_irqsave(&lockres->l_lock, flags);
953 if (!list_empty(&mw->mw_item)) {
954 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
955 ret = -EBUSY;
956
957 list_del_init(&mw->mw_item);
958 init_completion(&mw->mw_complete);
959 }
960 spin_unlock_irqrestore(&lockres->l_lock, flags);
961
962 return ret;
963
964 }
965
966 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
967 struct ocfs2_lock_res *lockres)
968 {
969 int ret;
970
971 ret = wait_for_completion_interruptible(&mw->mw_complete);
972 if (ret)
973 lockres_remove_mask_waiter(lockres, mw);
974 else
975 ret = mw->mw_status;
976 /* Re-arm the completion in case we want to wait on it again */
977 INIT_COMPLETION(mw->mw_complete);
978 return ret;
979 }
980
981 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
982 struct ocfs2_lock_res *lockres,
983 int level,
984 u32 lkm_flags,
985 int arg_flags)
986 {
987 struct ocfs2_mask_waiter mw;
988 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
989 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
990 unsigned long flags;
991
992 mlog_entry_void();
993
994 ocfs2_init_mask_waiter(&mw);
995
996 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
997 lkm_flags |= DLM_LKF_VALBLK;
998
999 again:
1000 wait = 0;
1001
1002 if (catch_signals && signal_pending(current)) {
1003 ret = -ERESTARTSYS;
1004 goto out;
1005 }
1006
1007 spin_lock_irqsave(&lockres->l_lock, flags);
1008
1009 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1010 "Cluster lock called on freeing lockres %s! flags "
1011 "0x%lx\n", lockres->l_name, lockres->l_flags);
1012
1013 /* We only compare against the currently granted level
1014 * here. If the lock is blocked waiting on a downconvert,
1015 * we'll get caught below. */
1016 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1017 level > lockres->l_level) {
1018 /* is someone sitting in dlm_lock? If so, wait on
1019 * them. */
1020 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1021 wait = 1;
1022 goto unlock;
1023 }
1024
1025 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1026 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1027 /* is the lock is currently blocked on behalf of
1028 * another node */
1029 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1030 wait = 1;
1031 goto unlock;
1032 }
1033
1034 if (level > lockres->l_level) {
1035 if (lockres->l_action != OCFS2_AST_INVALID)
1036 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1037 lockres->l_name, lockres->l_action);
1038
1039 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1040 lockres->l_action = OCFS2_AST_ATTACH;
1041 lkm_flags &= ~DLM_LKF_CONVERT;
1042 } else {
1043 lockres->l_action = OCFS2_AST_CONVERT;
1044 lkm_flags |= DLM_LKF_CONVERT;
1045 }
1046
1047 lockres->l_requested = level;
1048 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1049 spin_unlock_irqrestore(&lockres->l_lock, flags);
1050
1051 BUG_ON(level == DLM_LOCK_IV);
1052 BUG_ON(level == DLM_LOCK_NL);
1053
1054 mlog(0, "lock %s, convert from %d to level = %d\n",
1055 lockres->l_name, lockres->l_level, level);
1056
1057 /* call dlm_lock to upgrade lock now */
1058 ret = ocfs2_dlm_lock(osb->cconn,
1059 level,
1060 &lockres->l_lksb,
1061 lkm_flags,
1062 lockres->l_name,
1063 OCFS2_LOCK_ID_MAX_LEN - 1,
1064 lockres);
1065 if (ret) {
1066 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1067 (ret != -EAGAIN)) {
1068 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1069 ret, lockres);
1070 }
1071 ocfs2_recover_from_dlm_error(lockres, 1);
1072 goto out;
1073 }
1074
1075 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
1076 lockres->l_name);
1077
1078 /* At this point we've gone inside the dlm and need to
1079 * complete our work regardless. */
1080 catch_signals = 0;
1081
1082 /* wait for busy to clear and carry on */
1083 goto again;
1084 }
1085
1086 /* Ok, if we get here then we're good to go. */
1087 ocfs2_inc_holders(lockres, level);
1088
1089 ret = 0;
1090 unlock:
1091 spin_unlock_irqrestore(&lockres->l_lock, flags);
1092 out:
1093 /*
1094 * This is helping work around a lock inversion between the page lock
1095 * and dlm locks. One path holds the page lock while calling aops
1096 * which block acquiring dlm locks. The voting thread holds dlm
1097 * locks while acquiring page locks while down converting data locks.
1098 * This block is helping an aop path notice the inversion and back
1099 * off to unlock its page lock before trying the dlm lock again.
1100 */
1101 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1102 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1103 wait = 0;
1104 if (lockres_remove_mask_waiter(lockres, &mw))
1105 ret = -EAGAIN;
1106 else
1107 goto again;
1108 }
1109 if (wait) {
1110 ret = ocfs2_wait_for_mask(&mw);
1111 if (ret == 0)
1112 goto again;
1113 mlog_errno(ret);
1114 }
1115
1116 mlog_exit(ret);
1117 return ret;
1118 }
1119
1120 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1121 struct ocfs2_lock_res *lockres,
1122 int level)
1123 {
1124 unsigned long flags;
1125
1126 mlog_entry_void();
1127 spin_lock_irqsave(&lockres->l_lock, flags);
1128 ocfs2_dec_holders(lockres, level);
1129 ocfs2_downconvert_on_unlock(osb, lockres);
1130 spin_unlock_irqrestore(&lockres->l_lock, flags);
1131 mlog_exit_void();
1132 }
1133
1134 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1135 struct ocfs2_lock_res *lockres,
1136 int ex,
1137 int local)
1138 {
1139 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1140 unsigned long flags;
1141 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1142
1143 spin_lock_irqsave(&lockres->l_lock, flags);
1144 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1145 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1146 spin_unlock_irqrestore(&lockres->l_lock, flags);
1147
1148 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1149 }
1150
1151 /* Grants us an EX lock on the data and metadata resources, skipping
1152 * the normal cluster directory lookup. Use this ONLY on newly created
1153 * inodes which other nodes can't possibly see, and which haven't been
1154 * hashed in the inode hash yet. This can give us a good performance
1155 * increase as it'll skip the network broadcast normally associated
1156 * with creating a new lock resource. */
1157 int ocfs2_create_new_inode_locks(struct inode *inode)
1158 {
1159 int ret;
1160 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1161
1162 BUG_ON(!inode);
1163 BUG_ON(!ocfs2_inode_is_new(inode));
1164
1165 mlog_entry_void();
1166
1167 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1168
1169 /* NOTE: That we don't increment any of the holder counts, nor
1170 * do we add anything to a journal handle. Since this is
1171 * supposed to be a new inode which the cluster doesn't know
1172 * about yet, there is no need to. As far as the LVB handling
1173 * is concerned, this is basically like acquiring an EX lock
1174 * on a resource which has an invalid one -- we'll set it
1175 * valid when we release the EX. */
1176
1177 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1178 if (ret) {
1179 mlog_errno(ret);
1180 goto bail;
1181 }
1182
1183 /*
1184 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1185 * don't use a generation in their lock names.
1186 */
1187 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1188 if (ret) {
1189 mlog_errno(ret);
1190 goto bail;
1191 }
1192
1193 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1194 if (ret) {
1195 mlog_errno(ret);
1196 goto bail;
1197 }
1198
1199 bail:
1200 mlog_exit(ret);
1201 return ret;
1202 }
1203
1204 int ocfs2_rw_lock(struct inode *inode, int write)
1205 {
1206 int status, level;
1207 struct ocfs2_lock_res *lockres;
1208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1209
1210 BUG_ON(!inode);
1211
1212 mlog_entry_void();
1213
1214 mlog(0, "inode %llu take %s RW lock\n",
1215 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1216 write ? "EXMODE" : "PRMODE");
1217
1218 if (ocfs2_mount_local(osb))
1219 return 0;
1220
1221 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1222
1223 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1224
1225 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1226 0);
1227 if (status < 0)
1228 mlog_errno(status);
1229
1230 mlog_exit(status);
1231 return status;
1232 }
1233
1234 void ocfs2_rw_unlock(struct inode *inode, int write)
1235 {
1236 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1237 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1239
1240 mlog_entry_void();
1241
1242 mlog(0, "inode %llu drop %s RW lock\n",
1243 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1244 write ? "EXMODE" : "PRMODE");
1245
1246 if (!ocfs2_mount_local(osb))
1247 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1248
1249 mlog_exit_void();
1250 }
1251
1252 /*
1253 * ocfs2_open_lock always get PR mode lock.
1254 */
1255 int ocfs2_open_lock(struct inode *inode)
1256 {
1257 int status = 0;
1258 struct ocfs2_lock_res *lockres;
1259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1260
1261 BUG_ON(!inode);
1262
1263 mlog_entry_void();
1264
1265 mlog(0, "inode %llu take PRMODE open lock\n",
1266 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1267
1268 if (ocfs2_mount_local(osb))
1269 goto out;
1270
1271 lockres = &OCFS2_I(inode)->ip_open_lockres;
1272
1273 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1274 DLM_LOCK_PR, 0, 0);
1275 if (status < 0)
1276 mlog_errno(status);
1277
1278 out:
1279 mlog_exit(status);
1280 return status;
1281 }
1282
1283 int ocfs2_try_open_lock(struct inode *inode, int write)
1284 {
1285 int status = 0, level;
1286 struct ocfs2_lock_res *lockres;
1287 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1288
1289 BUG_ON(!inode);
1290
1291 mlog_entry_void();
1292
1293 mlog(0, "inode %llu try to take %s open lock\n",
1294 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1295 write ? "EXMODE" : "PRMODE");
1296
1297 if (ocfs2_mount_local(osb))
1298 goto out;
1299
1300 lockres = &OCFS2_I(inode)->ip_open_lockres;
1301
1302 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1303
1304 /*
1305 * The file system may already holding a PRMODE/EXMODE open lock.
1306 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1307 * other nodes and the -EAGAIN will indicate to the caller that
1308 * this inode is still in use.
1309 */
1310 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1311 level, DLM_LKF_NOQUEUE, 0);
1312
1313 out:
1314 mlog_exit(status);
1315 return status;
1316 }
1317
1318 /*
1319 * ocfs2_open_unlock unlock PR and EX mode open locks.
1320 */
1321 void ocfs2_open_unlock(struct inode *inode)
1322 {
1323 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1325
1326 mlog_entry_void();
1327
1328 mlog(0, "inode %llu drop open lock\n",
1329 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1330
1331 if (ocfs2_mount_local(osb))
1332 goto out;
1333
1334 if(lockres->l_ro_holders)
1335 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1336 DLM_LOCK_PR);
1337 if(lockres->l_ex_holders)
1338 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1339 DLM_LOCK_EX);
1340
1341 out:
1342 mlog_exit_void();
1343 }
1344
1345 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1346 int level)
1347 {
1348 int ret;
1349 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1350 unsigned long flags;
1351 struct ocfs2_mask_waiter mw;
1352
1353 ocfs2_init_mask_waiter(&mw);
1354
1355 retry_cancel:
1356 spin_lock_irqsave(&lockres->l_lock, flags);
1357 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1358 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1359 if (ret) {
1360 spin_unlock_irqrestore(&lockres->l_lock, flags);
1361 ret = ocfs2_cancel_convert(osb, lockres);
1362 if (ret < 0) {
1363 mlog_errno(ret);
1364 goto out;
1365 }
1366 goto retry_cancel;
1367 }
1368 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1369 spin_unlock_irqrestore(&lockres->l_lock, flags);
1370
1371 ocfs2_wait_for_mask(&mw);
1372 goto retry_cancel;
1373 }
1374
1375 ret = -ERESTARTSYS;
1376 /*
1377 * We may still have gotten the lock, in which case there's no
1378 * point to restarting the syscall.
1379 */
1380 if (lockres->l_level == level)
1381 ret = 0;
1382
1383 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1384 lockres->l_flags, lockres->l_level, lockres->l_action);
1385
1386 spin_unlock_irqrestore(&lockres->l_lock, flags);
1387
1388 out:
1389 return ret;
1390 }
1391
1392 /*
1393 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1394 * flock() calls. The locking approach this requires is sufficiently
1395 * different from all other cluster lock types that we implement a
1396 * seperate path to the "low-level" dlm calls. In particular:
1397 *
1398 * - No optimization of lock levels is done - we take at exactly
1399 * what's been requested.
1400 *
1401 * - No lock caching is employed. We immediately downconvert to
1402 * no-lock at unlock time. This also means flock locks never go on
1403 * the blocking list).
1404 *
1405 * - Since userspace can trivially deadlock itself with flock, we make
1406 * sure to allow cancellation of a misbehaving applications flock()
1407 * request.
1408 *
1409 * - Access to any flock lockres doesn't require concurrency, so we
1410 * can simplify the code by requiring the caller to guarantee
1411 * serialization of dlmglue flock calls.
1412 */
1413 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1414 {
1415 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1416 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1417 unsigned long flags;
1418 struct ocfs2_file_private *fp = file->private_data;
1419 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1420 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1421 struct ocfs2_mask_waiter mw;
1422
1423 ocfs2_init_mask_waiter(&mw);
1424
1425 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1426 (lockres->l_level > DLM_LOCK_NL)) {
1427 mlog(ML_ERROR,
1428 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1429 "level: %u\n", lockres->l_name, lockres->l_flags,
1430 lockres->l_level);
1431 return -EINVAL;
1432 }
1433
1434 spin_lock_irqsave(&lockres->l_lock, flags);
1435 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1436 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1437 spin_unlock_irqrestore(&lockres->l_lock, flags);
1438
1439 /*
1440 * Get the lock at NLMODE to start - that way we
1441 * can cancel the upconvert request if need be.
1442 */
1443 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1444 if (ret < 0) {
1445 mlog_errno(ret);
1446 goto out;
1447 }
1448
1449 ret = ocfs2_wait_for_mask(&mw);
1450 if (ret) {
1451 mlog_errno(ret);
1452 goto out;
1453 }
1454 spin_lock_irqsave(&lockres->l_lock, flags);
1455 }
1456
1457 lockres->l_action = OCFS2_AST_CONVERT;
1458 lkm_flags |= LKM_CONVERT;
1459 lockres->l_requested = level;
1460 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1461
1462 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1463 spin_unlock_irqrestore(&lockres->l_lock, flags);
1464
1465 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
1466 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1467 lockres);
1468 if (ret) {
1469 if (!trylock || (ret != -EAGAIN)) {
1470 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1471 ret = -EINVAL;
1472 }
1473
1474 ocfs2_recover_from_dlm_error(lockres, 1);
1475 lockres_remove_mask_waiter(lockres, &mw);
1476 goto out;
1477 }
1478
1479 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1480 if (ret == -ERESTARTSYS) {
1481 /*
1482 * Userspace can cause deadlock itself with
1483 * flock(). Current behavior locally is to allow the
1484 * deadlock, but abort the system call if a signal is
1485 * received. We follow this example, otherwise a
1486 * poorly written program could sit in kernel until
1487 * reboot.
1488 *
1489 * Handling this is a bit more complicated for Ocfs2
1490 * though. We can't exit this function with an
1491 * outstanding lock request, so a cancel convert is
1492 * required. We intentionally overwrite 'ret' - if the
1493 * cancel fails and the lock was granted, it's easier
1494 * to just bubble sucess back up to the user.
1495 */
1496 ret = ocfs2_flock_handle_signal(lockres, level);
1497 }
1498
1499 out:
1500
1501 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1502 lockres->l_name, ex, trylock, ret);
1503 return ret;
1504 }
1505
1506 void ocfs2_file_unlock(struct file *file)
1507 {
1508 int ret;
1509 unsigned long flags;
1510 struct ocfs2_file_private *fp = file->private_data;
1511 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1512 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1513 struct ocfs2_mask_waiter mw;
1514
1515 ocfs2_init_mask_waiter(&mw);
1516
1517 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1518 return;
1519
1520 if (lockres->l_level == LKM_NLMODE)
1521 return;
1522
1523 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1524 lockres->l_name, lockres->l_flags, lockres->l_level,
1525 lockres->l_action);
1526
1527 spin_lock_irqsave(&lockres->l_lock, flags);
1528 /*
1529 * Fake a blocking ast for the downconvert code.
1530 */
1531 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1532 lockres->l_blocking = DLM_LOCK_EX;
1533
1534 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1535 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1536 spin_unlock_irqrestore(&lockres->l_lock, flags);
1537
1538 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1539 if (ret) {
1540 mlog_errno(ret);
1541 return;
1542 }
1543
1544 ret = ocfs2_wait_for_mask(&mw);
1545 if (ret)
1546 mlog_errno(ret);
1547 }
1548
1549 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1550 struct ocfs2_lock_res *lockres)
1551 {
1552 int kick = 0;
1553
1554 mlog_entry_void();
1555
1556 /* If we know that another node is waiting on our lock, kick
1557 * the downconvert thread * pre-emptively when we reach a release
1558 * condition. */
1559 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1560 switch(lockres->l_blocking) {
1561 case DLM_LOCK_EX:
1562 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1563 kick = 1;
1564 break;
1565 case DLM_LOCK_PR:
1566 if (!lockres->l_ex_holders)
1567 kick = 1;
1568 break;
1569 default:
1570 BUG();
1571 }
1572 }
1573
1574 if (kick)
1575 ocfs2_wake_downconvert_thread(osb);
1576
1577 mlog_exit_void();
1578 }
1579
1580 #define OCFS2_SEC_BITS 34
1581 #define OCFS2_SEC_SHIFT (64 - 34)
1582 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1583
1584 /* LVB only has room for 64 bits of time here so we pack it for
1585 * now. */
1586 static u64 ocfs2_pack_timespec(struct timespec *spec)
1587 {
1588 u64 res;
1589 u64 sec = spec->tv_sec;
1590 u32 nsec = spec->tv_nsec;
1591
1592 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1593
1594 return res;
1595 }
1596
1597 /* Call this with the lockres locked. I am reasonably sure we don't
1598 * need ip_lock in this function as anyone who would be changing those
1599 * values is supposed to be blocked in ocfs2_inode_lock right now. */
1600 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1601 {
1602 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1603 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1604 struct ocfs2_meta_lvb *lvb;
1605
1606 mlog_entry_void();
1607
1608 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1609
1610 /*
1611 * Invalidate the LVB of a deleted inode - this way other
1612 * nodes are forced to go to disk and discover the new inode
1613 * status.
1614 */
1615 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1616 lvb->lvb_version = 0;
1617 goto out;
1618 }
1619
1620 lvb->lvb_version = OCFS2_LVB_VERSION;
1621 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1622 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1623 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1624 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1625 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1626 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1627 lvb->lvb_iatime_packed =
1628 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1629 lvb->lvb_ictime_packed =
1630 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1631 lvb->lvb_imtime_packed =
1632 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1633 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1634 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
1635 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1636
1637 out:
1638 mlog_meta_lvb(0, lockres);
1639
1640 mlog_exit_void();
1641 }
1642
1643 static void ocfs2_unpack_timespec(struct timespec *spec,
1644 u64 packed_time)
1645 {
1646 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1647 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1648 }
1649
1650 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1651 {
1652 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1653 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1654 struct ocfs2_meta_lvb *lvb;
1655
1656 mlog_entry_void();
1657
1658 mlog_meta_lvb(0, lockres);
1659
1660 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1661
1662 /* We're safe here without the lockres lock... */
1663 spin_lock(&oi->ip_lock);
1664 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1665 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1666
1667 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1668 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
1669 ocfs2_set_inode_flags(inode);
1670
1671 /* fast-symlinks are a special case */
1672 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1673 inode->i_blocks = 0;
1674 else
1675 inode->i_blocks = ocfs2_inode_sector_count(inode);
1676
1677 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1678 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1679 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1680 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1681 ocfs2_unpack_timespec(&inode->i_atime,
1682 be64_to_cpu(lvb->lvb_iatime_packed));
1683 ocfs2_unpack_timespec(&inode->i_mtime,
1684 be64_to_cpu(lvb->lvb_imtime_packed));
1685 ocfs2_unpack_timespec(&inode->i_ctime,
1686 be64_to_cpu(lvb->lvb_ictime_packed));
1687 spin_unlock(&oi->ip_lock);
1688
1689 mlog_exit_void();
1690 }
1691
1692 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1693 struct ocfs2_lock_res *lockres)
1694 {
1695 struct ocfs2_meta_lvb *lvb =
1696 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
1697
1698 if (lvb->lvb_version == OCFS2_LVB_VERSION
1699 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1700 return 1;
1701 return 0;
1702 }
1703
1704 /* Determine whether a lock resource needs to be refreshed, and
1705 * arbitrate who gets to refresh it.
1706 *
1707 * 0 means no refresh needed.
1708 *
1709 * > 0 means you need to refresh this and you MUST call
1710 * ocfs2_complete_lock_res_refresh afterwards. */
1711 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1712 {
1713 unsigned long flags;
1714 int status = 0;
1715
1716 mlog_entry_void();
1717
1718 refresh_check:
1719 spin_lock_irqsave(&lockres->l_lock, flags);
1720 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1721 spin_unlock_irqrestore(&lockres->l_lock, flags);
1722 goto bail;
1723 }
1724
1725 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1726 spin_unlock_irqrestore(&lockres->l_lock, flags);
1727
1728 ocfs2_wait_on_refreshing_lock(lockres);
1729 goto refresh_check;
1730 }
1731
1732 /* Ok, I'll be the one to refresh this lock. */
1733 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1734 spin_unlock_irqrestore(&lockres->l_lock, flags);
1735
1736 status = 1;
1737 bail:
1738 mlog_exit(status);
1739 return status;
1740 }
1741
1742 /* If status is non zero, I'll mark it as not being in refresh
1743 * anymroe, but i won't clear the needs refresh flag. */
1744 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1745 int status)
1746 {
1747 unsigned long flags;
1748 mlog_entry_void();
1749
1750 spin_lock_irqsave(&lockres->l_lock, flags);
1751 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1752 if (!status)
1753 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1754 spin_unlock_irqrestore(&lockres->l_lock, flags);
1755
1756 wake_up(&lockres->l_event);
1757
1758 mlog_exit_void();
1759 }
1760
1761 /* may or may not return a bh if it went to disk. */
1762 static int ocfs2_inode_lock_update(struct inode *inode,
1763 struct buffer_head **bh)
1764 {
1765 int status = 0;
1766 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1767 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
1768 struct ocfs2_dinode *fe;
1769 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1770
1771 mlog_entry_void();
1772
1773 if (ocfs2_mount_local(osb))
1774 goto bail;
1775
1776 spin_lock(&oi->ip_lock);
1777 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1778 mlog(0, "Orphaned inode %llu was deleted while we "
1779 "were waiting on a lock. ip_flags = 0x%x\n",
1780 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1781 spin_unlock(&oi->ip_lock);
1782 status = -ENOENT;
1783 goto bail;
1784 }
1785 spin_unlock(&oi->ip_lock);
1786
1787 if (!ocfs2_should_refresh_lock_res(lockres))
1788 goto bail;
1789
1790 /* This will discard any caching information we might have had
1791 * for the inode metadata. */
1792 ocfs2_metadata_cache_purge(inode);
1793
1794 ocfs2_extent_map_trunc(inode, 0);
1795
1796 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1797 mlog(0, "Trusting LVB on inode %llu\n",
1798 (unsigned long long)oi->ip_blkno);
1799 ocfs2_refresh_inode_from_lvb(inode);
1800 } else {
1801 /* Boo, we have to go to disk. */
1802 /* read bh, cast, ocfs2_refresh_inode */
1803 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1804 bh, OCFS2_BH_CACHED, inode);
1805 if (status < 0) {
1806 mlog_errno(status);
1807 goto bail_refresh;
1808 }
1809 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1810
1811 /* This is a good chance to make sure we're not
1812 * locking an invalid object.
1813 *
1814 * We bug on a stale inode here because we checked
1815 * above whether it was wiped from disk. The wiping
1816 * node provides a guarantee that we receive that
1817 * message and can mark the inode before dropping any
1818 * locks associated with it. */
1819 if (!OCFS2_IS_VALID_DINODE(fe)) {
1820 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1821 status = -EIO;
1822 goto bail_refresh;
1823 }
1824 mlog_bug_on_msg(inode->i_generation !=
1825 le32_to_cpu(fe->i_generation),
1826 "Invalid dinode %llu disk generation: %u "
1827 "inode->i_generation: %u\n",
1828 (unsigned long long)oi->ip_blkno,
1829 le32_to_cpu(fe->i_generation),
1830 inode->i_generation);
1831 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1832 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1833 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1834 (unsigned long long)oi->ip_blkno,
1835 (unsigned long long)le64_to_cpu(fe->i_dtime),
1836 le32_to_cpu(fe->i_flags));
1837
1838 ocfs2_refresh_inode(inode, fe);
1839 }
1840
1841 status = 0;
1842 bail_refresh:
1843 ocfs2_complete_lock_res_refresh(lockres, status);
1844 bail:
1845 mlog_exit(status);
1846 return status;
1847 }
1848
1849 static int ocfs2_assign_bh(struct inode *inode,
1850 struct buffer_head **ret_bh,
1851 struct buffer_head *passed_bh)
1852 {
1853 int status;
1854
1855 if (passed_bh) {
1856 /* Ok, the update went to disk for us, use the
1857 * returned bh. */
1858 *ret_bh = passed_bh;
1859 get_bh(*ret_bh);
1860
1861 return 0;
1862 }
1863
1864 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1865 OCFS2_I(inode)->ip_blkno,
1866 ret_bh,
1867 OCFS2_BH_CACHED,
1868 inode);
1869 if (status < 0)
1870 mlog_errno(status);
1871
1872 return status;
1873 }
1874
1875 /*
1876 * returns < 0 error if the callback will never be called, otherwise
1877 * the result of the lock will be communicated via the callback.
1878 */
1879 int ocfs2_inode_lock_full(struct inode *inode,
1880 struct buffer_head **ret_bh,
1881 int ex,
1882 int arg_flags)
1883 {
1884 int status, level, acquired;
1885 u32 dlm_flags;
1886 struct ocfs2_lock_res *lockres = NULL;
1887 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1888 struct buffer_head *local_bh = NULL;
1889
1890 BUG_ON(!inode);
1891
1892 mlog_entry_void();
1893
1894 mlog(0, "inode %llu, take %s META lock\n",
1895 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1896 ex ? "EXMODE" : "PRMODE");
1897
1898 status = 0;
1899 acquired = 0;
1900 /* We'll allow faking a readonly metadata lock for
1901 * rodevices. */
1902 if (ocfs2_is_hard_readonly(osb)) {
1903 if (ex)
1904 status = -EROFS;
1905 goto bail;
1906 }
1907
1908 if (ocfs2_mount_local(osb))
1909 goto local;
1910
1911 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1912 ocfs2_wait_for_recovery(osb);
1913
1914 lockres = &OCFS2_I(inode)->ip_inode_lockres;
1915 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1916 dlm_flags = 0;
1917 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1918 dlm_flags |= DLM_LKF_NOQUEUE;
1919
1920 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1921 if (status < 0) {
1922 if (status != -EAGAIN && status != -EIOCBRETRY)
1923 mlog_errno(status);
1924 goto bail;
1925 }
1926
1927 /* Notify the error cleanup path to drop the cluster lock. */
1928 acquired = 1;
1929
1930 /* We wait twice because a node may have died while we were in
1931 * the lower dlm layers. The second time though, we've
1932 * committed to owning this lock so we don't allow signals to
1933 * abort the operation. */
1934 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1935 ocfs2_wait_for_recovery(osb);
1936
1937 local:
1938 /*
1939 * We only see this flag if we're being called from
1940 * ocfs2_read_locked_inode(). It means we're locking an inode
1941 * which hasn't been populated yet, so clear the refresh flag
1942 * and let the caller handle it.
1943 */
1944 if (inode->i_state & I_NEW) {
1945 status = 0;
1946 if (lockres)
1947 ocfs2_complete_lock_res_refresh(lockres, 0);
1948 goto bail;
1949 }
1950
1951 /* This is fun. The caller may want a bh back, or it may
1952 * not. ocfs2_inode_lock_update definitely wants one in, but
1953 * may or may not read one, depending on what's in the
1954 * LVB. The result of all of this is that we've *only* gone to
1955 * disk if we have to, so the complexity is worthwhile. */
1956 status = ocfs2_inode_lock_update(inode, &local_bh);
1957 if (status < 0) {
1958 if (status != -ENOENT)
1959 mlog_errno(status);
1960 goto bail;
1961 }
1962
1963 if (ret_bh) {
1964 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1965 if (status < 0) {
1966 mlog_errno(status);
1967 goto bail;
1968 }
1969 }
1970
1971 bail:
1972 if (status < 0) {
1973 if (ret_bh && (*ret_bh)) {
1974 brelse(*ret_bh);
1975 *ret_bh = NULL;
1976 }
1977 if (acquired)
1978 ocfs2_inode_unlock(inode, ex);
1979 }
1980
1981 if (local_bh)
1982 brelse(local_bh);
1983
1984 mlog_exit(status);
1985 return status;
1986 }
1987
1988 /*
1989 * This is working around a lock inversion between tasks acquiring DLM
1990 * locks while holding a page lock and the downconvert thread which
1991 * blocks dlm lock acquiry while acquiring page locks.
1992 *
1993 * ** These _with_page variantes are only intended to be called from aop
1994 * methods that hold page locks and return a very specific *positive* error
1995 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1996 *
1997 * The DLM is called such that it returns -EAGAIN if it would have
1998 * blocked waiting for the downconvert thread. In that case we unlock
1999 * our page so the downconvert thread can make progress. Once we've
2000 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2001 * that called us can bubble that back up into the VFS who will then
2002 * immediately retry the aop call.
2003 *
2004 * We do a blocking lock and immediate unlock before returning, though, so that
2005 * the lock has a great chance of being cached on this node by the time the VFS
2006 * calls back to retry the aop. This has a potential to livelock as nodes
2007 * ping locks back and forth, but that's a risk we're willing to take to avoid
2008 * the lock inversion simply.
2009 */
2010 int ocfs2_inode_lock_with_page(struct inode *inode,
2011 struct buffer_head **ret_bh,
2012 int ex,
2013 struct page *page)
2014 {
2015 int ret;
2016
2017 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2018 if (ret == -EAGAIN) {
2019 unlock_page(page);
2020 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2021 ocfs2_inode_unlock(inode, ex);
2022 ret = AOP_TRUNCATED_PAGE;
2023 }
2024
2025 return ret;
2026 }
2027
2028 int ocfs2_inode_lock_atime(struct inode *inode,
2029 struct vfsmount *vfsmnt,
2030 int *level)
2031 {
2032 int ret;
2033
2034 mlog_entry_void();
2035 ret = ocfs2_inode_lock(inode, NULL, 0);
2036 if (ret < 0) {
2037 mlog_errno(ret);
2038 return ret;
2039 }
2040
2041 /*
2042 * If we should update atime, we will get EX lock,
2043 * otherwise we just get PR lock.
2044 */
2045 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2046 struct buffer_head *bh = NULL;
2047
2048 ocfs2_inode_unlock(inode, 0);
2049 ret = ocfs2_inode_lock(inode, &bh, 1);
2050 if (ret < 0) {
2051 mlog_errno(ret);
2052 return ret;
2053 }
2054 *level = 1;
2055 if (ocfs2_should_update_atime(inode, vfsmnt))
2056 ocfs2_update_inode_atime(inode, bh);
2057 if (bh)
2058 brelse(bh);
2059 } else
2060 *level = 0;
2061
2062 mlog_exit(ret);
2063 return ret;
2064 }
2065
2066 void ocfs2_inode_unlock(struct inode *inode,
2067 int ex)
2068 {
2069 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2070 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2071 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2072
2073 mlog_entry_void();
2074
2075 mlog(0, "inode %llu drop %s META lock\n",
2076 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2077 ex ? "EXMODE" : "PRMODE");
2078
2079 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2080 !ocfs2_mount_local(osb))
2081 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2082
2083 mlog_exit_void();
2084 }
2085
2086 int ocfs2_super_lock(struct ocfs2_super *osb,
2087 int ex)
2088 {
2089 int status = 0;
2090 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2091 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2092
2093 mlog_entry_void();
2094
2095 if (ocfs2_is_hard_readonly(osb))
2096 return -EROFS;
2097
2098 if (ocfs2_mount_local(osb))
2099 goto bail;
2100
2101 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2102 if (status < 0) {
2103 mlog_errno(status);
2104 goto bail;
2105 }
2106
2107 /* The super block lock path is really in the best position to
2108 * know when resources covered by the lock need to be
2109 * refreshed, so we do it here. Of course, making sense of
2110 * everything is up to the caller :) */
2111 status = ocfs2_should_refresh_lock_res(lockres);
2112 if (status < 0) {
2113 mlog_errno(status);
2114 goto bail;
2115 }
2116 if (status) {
2117 status = ocfs2_refresh_slot_info(osb);
2118
2119 ocfs2_complete_lock_res_refresh(lockres, status);
2120
2121 if (status < 0)
2122 mlog_errno(status);
2123 }
2124 bail:
2125 mlog_exit(status);
2126 return status;
2127 }
2128
2129 void ocfs2_super_unlock(struct ocfs2_super *osb,
2130 int ex)
2131 {
2132 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2133 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2134
2135 if (!ocfs2_mount_local(osb))
2136 ocfs2_cluster_unlock(osb, lockres, level);
2137 }
2138
2139 int ocfs2_rename_lock(struct ocfs2_super *osb)
2140 {
2141 int status;
2142 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2143
2144 if (ocfs2_is_hard_readonly(osb))
2145 return -EROFS;
2146
2147 if (ocfs2_mount_local(osb))
2148 return 0;
2149
2150 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2151 if (status < 0)
2152 mlog_errno(status);
2153
2154 return status;
2155 }
2156
2157 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2158 {
2159 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2160
2161 if (!ocfs2_mount_local(osb))
2162 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2163 }
2164
2165 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2166 {
2167 int ret;
2168 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2169 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2170 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2171
2172 BUG_ON(!dl);
2173
2174 if (ocfs2_is_hard_readonly(osb))
2175 return -EROFS;
2176
2177 if (ocfs2_mount_local(osb))
2178 return 0;
2179
2180 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2181 if (ret < 0)
2182 mlog_errno(ret);
2183
2184 return ret;
2185 }
2186
2187 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2188 {
2189 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2190 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2191 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2192
2193 if (!ocfs2_mount_local(osb))
2194 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2195 }
2196
2197 /* Reference counting of the dlm debug structure. We want this because
2198 * open references on the debug inodes can live on after a mount, so
2199 * we can't rely on the ocfs2_super to always exist. */
2200 static void ocfs2_dlm_debug_free(struct kref *kref)
2201 {
2202 struct ocfs2_dlm_debug *dlm_debug;
2203
2204 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2205
2206 kfree(dlm_debug);
2207 }
2208
2209 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2210 {
2211 if (dlm_debug)
2212 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2213 }
2214
2215 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2216 {
2217 kref_get(&debug->d_refcnt);
2218 }
2219
2220 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2221 {
2222 struct ocfs2_dlm_debug *dlm_debug;
2223
2224 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2225 if (!dlm_debug) {
2226 mlog_errno(-ENOMEM);
2227 goto out;
2228 }
2229
2230 kref_init(&dlm_debug->d_refcnt);
2231 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2232 dlm_debug->d_locking_state = NULL;
2233 out:
2234 return dlm_debug;
2235 }
2236
2237 /* Access to this is arbitrated for us via seq_file->sem. */
2238 struct ocfs2_dlm_seq_priv {
2239 struct ocfs2_dlm_debug *p_dlm_debug;
2240 struct ocfs2_lock_res p_iter_res;
2241 struct ocfs2_lock_res p_tmp_res;
2242 };
2243
2244 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2245 struct ocfs2_dlm_seq_priv *priv)
2246 {
2247 struct ocfs2_lock_res *iter, *ret = NULL;
2248 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2249
2250 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2251
2252 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2253 /* discover the head of the list */
2254 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2255 mlog(0, "End of list found, %p\n", ret);
2256 break;
2257 }
2258
2259 /* We track our "dummy" iteration lockres' by a NULL
2260 * l_ops field. */
2261 if (iter->l_ops != NULL) {
2262 ret = iter;
2263 break;
2264 }
2265 }
2266
2267 return ret;
2268 }
2269
2270 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2271 {
2272 struct ocfs2_dlm_seq_priv *priv = m->private;
2273 struct ocfs2_lock_res *iter;
2274
2275 spin_lock(&ocfs2_dlm_tracking_lock);
2276 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2277 if (iter) {
2278 /* Since lockres' have the lifetime of their container
2279 * (which can be inodes, ocfs2_supers, etc) we want to
2280 * copy this out to a temporary lockres while still
2281 * under the spinlock. Obviously after this we can't
2282 * trust any pointers on the copy returned, but that's
2283 * ok as the information we want isn't typically held
2284 * in them. */
2285 priv->p_tmp_res = *iter;
2286 iter = &priv->p_tmp_res;
2287 }
2288 spin_unlock(&ocfs2_dlm_tracking_lock);
2289
2290 return iter;
2291 }
2292
2293 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2294 {
2295 }
2296
2297 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2298 {
2299 struct ocfs2_dlm_seq_priv *priv = m->private;
2300 struct ocfs2_lock_res *iter = v;
2301 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2302
2303 spin_lock(&ocfs2_dlm_tracking_lock);
2304 iter = ocfs2_dlm_next_res(iter, priv);
2305 list_del_init(&dummy->l_debug_list);
2306 if (iter) {
2307 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2308 priv->p_tmp_res = *iter;
2309 iter = &priv->p_tmp_res;
2310 }
2311 spin_unlock(&ocfs2_dlm_tracking_lock);
2312
2313 return iter;
2314 }
2315
2316 /* So that debugfs.ocfs2 can determine which format is being used */
2317 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2318 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2319 {
2320 int i;
2321 char *lvb;
2322 struct ocfs2_lock_res *lockres = v;
2323
2324 if (!lockres)
2325 return -EINVAL;
2326
2327 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2328
2329 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2330 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2331 lockres->l_name,
2332 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2333 else
2334 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2335
2336 seq_printf(m, "%d\t"
2337 "0x%lx\t"
2338 "0x%x\t"
2339 "0x%x\t"
2340 "%u\t"
2341 "%u\t"
2342 "%d\t"
2343 "%d\t",
2344 lockres->l_level,
2345 lockres->l_flags,
2346 lockres->l_action,
2347 lockres->l_unlock_action,
2348 lockres->l_ro_holders,
2349 lockres->l_ex_holders,
2350 lockres->l_requested,
2351 lockres->l_blocking);
2352
2353 /* Dump the raw LVB */
2354 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2355 for(i = 0; i < DLM_LVB_LEN; i++)
2356 seq_printf(m, "0x%x\t", lvb[i]);
2357
2358 /* End the line */
2359 seq_printf(m, "\n");
2360 return 0;
2361 }
2362
2363 static const struct seq_operations ocfs2_dlm_seq_ops = {
2364 .start = ocfs2_dlm_seq_start,
2365 .stop = ocfs2_dlm_seq_stop,
2366 .next = ocfs2_dlm_seq_next,
2367 .show = ocfs2_dlm_seq_show,
2368 };
2369
2370 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2371 {
2372 struct seq_file *seq = (struct seq_file *) file->private_data;
2373 struct ocfs2_dlm_seq_priv *priv = seq->private;
2374 struct ocfs2_lock_res *res = &priv->p_iter_res;
2375
2376 ocfs2_remove_lockres_tracking(res);
2377 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2378 return seq_release_private(inode, file);
2379 }
2380
2381 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2382 {
2383 int ret;
2384 struct ocfs2_dlm_seq_priv *priv;
2385 struct seq_file *seq;
2386 struct ocfs2_super *osb;
2387
2388 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2389 if (!priv) {
2390 ret = -ENOMEM;
2391 mlog_errno(ret);
2392 goto out;
2393 }
2394 osb = inode->i_private;
2395 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2396 priv->p_dlm_debug = osb->osb_dlm_debug;
2397 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2398
2399 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2400 if (ret) {
2401 kfree(priv);
2402 mlog_errno(ret);
2403 goto out;
2404 }
2405
2406 seq = (struct seq_file *) file->private_data;
2407 seq->private = priv;
2408
2409 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2410 priv->p_dlm_debug);
2411
2412 out:
2413 return ret;
2414 }
2415
2416 static const struct file_operations ocfs2_dlm_debug_fops = {
2417 .open = ocfs2_dlm_debug_open,
2418 .release = ocfs2_dlm_debug_release,
2419 .read = seq_read,
2420 .llseek = seq_lseek,
2421 };
2422
2423 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2424 {
2425 int ret = 0;
2426 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2427
2428 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2429 S_IFREG|S_IRUSR,
2430 osb->osb_debug_root,
2431 osb,
2432 &ocfs2_dlm_debug_fops);
2433 if (!dlm_debug->d_locking_state) {
2434 ret = -EINVAL;
2435 mlog(ML_ERROR,
2436 "Unable to create locking state debugfs file.\n");
2437 goto out;
2438 }
2439
2440 ocfs2_get_dlm_debug(dlm_debug);
2441 out:
2442 return ret;
2443 }
2444
2445 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2446 {
2447 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2448
2449 if (dlm_debug) {
2450 debugfs_remove(dlm_debug->d_locking_state);
2451 ocfs2_put_dlm_debug(dlm_debug);
2452 }
2453 }
2454
2455 int ocfs2_dlm_init(struct ocfs2_super *osb)
2456 {
2457 int status = 0;
2458 struct ocfs2_cluster_connection *conn = NULL;
2459
2460 mlog_entry_void();
2461
2462 if (ocfs2_mount_local(osb)) {
2463 osb->node_num = 0;
2464 goto local;
2465 }
2466
2467 status = ocfs2_dlm_init_debug(osb);
2468 if (status < 0) {
2469 mlog_errno(status);
2470 goto bail;
2471 }
2472
2473 /* launch downconvert thread */
2474 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2475 if (IS_ERR(osb->dc_task)) {
2476 status = PTR_ERR(osb->dc_task);
2477 osb->dc_task = NULL;
2478 mlog_errno(status);
2479 goto bail;
2480 }
2481
2482 /* for now, uuid == domain */
2483 status = ocfs2_cluster_connect(osb->uuid_str,
2484 strlen(osb->uuid_str),
2485 ocfs2_do_node_down, osb,
2486 &conn);
2487 if (status) {
2488 mlog_errno(status);
2489 goto bail;
2490 }
2491
2492 status = ocfs2_cluster_this_node(&osb->node_num);
2493 if (status < 0) {
2494 mlog_errno(status);
2495 mlog(ML_ERROR,
2496 "could not find this host's node number\n");
2497 ocfs2_cluster_disconnect(conn);
2498 goto bail;
2499 }
2500
2501 local:
2502 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2503 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2504
2505 osb->cconn = conn;
2506
2507 status = 0;
2508 bail:
2509 if (status < 0) {
2510 ocfs2_dlm_shutdown_debug(osb);
2511 if (osb->dc_task)
2512 kthread_stop(osb->dc_task);
2513 }
2514
2515 mlog_exit(status);
2516 return status;
2517 }
2518
2519 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2520 {
2521 mlog_entry_void();
2522
2523 ocfs2_drop_osb_locks(osb);
2524
2525 /*
2526 * Now that we have dropped all locks and ocfs2_dismount_volume()
2527 * has disabled recovery, the DLM won't be talking to us. It's
2528 * safe to tear things down before disconnecting the cluster.
2529 */
2530
2531 if (osb->dc_task) {
2532 kthread_stop(osb->dc_task);
2533 osb->dc_task = NULL;
2534 }
2535
2536 ocfs2_lock_res_free(&osb->osb_super_lockres);
2537 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2538
2539 ocfs2_cluster_disconnect(osb->cconn);
2540 osb->cconn = NULL;
2541
2542 ocfs2_dlm_shutdown_debug(osb);
2543
2544 mlog_exit_void();
2545 }
2546
2547 static void ocfs2_unlock_ast(void *opaque, int error)
2548 {
2549 struct ocfs2_lock_res *lockres = opaque;
2550 unsigned long flags;
2551
2552 mlog_entry_void();
2553
2554 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2555 lockres->l_unlock_action);
2556
2557 spin_lock_irqsave(&lockres->l_lock, flags);
2558 /* We tried to cancel a convert request, but it was already
2559 * granted. All we want to do here is clear our unlock
2560 * state. The wake_up call done at the bottom is redundant
2561 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2562 * hurt anything anyway */
2563 if (error == -DLM_ECANCEL &&
2564 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2565 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2566
2567 /* We don't clear the busy flag in this case as it
2568 * should have been cleared by the ast which the dlm
2569 * has called. */
2570 goto complete_unlock;
2571 }
2572
2573 /* DLM_EUNLOCK is the success code for unlock */
2574 if (error != -DLM_EUNLOCK) {
2575 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2576 "unlock_action %d\n", error, lockres->l_name,
2577 lockres->l_unlock_action);
2578 spin_unlock_irqrestore(&lockres->l_lock, flags);
2579 return;
2580 }
2581
2582 switch(lockres->l_unlock_action) {
2583 case OCFS2_UNLOCK_CANCEL_CONVERT:
2584 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2585 lockres->l_action = OCFS2_AST_INVALID;
2586 break;
2587 case OCFS2_UNLOCK_DROP_LOCK:
2588 lockres->l_level = DLM_LOCK_IV;
2589 break;
2590 default:
2591 BUG();
2592 }
2593
2594 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2595 complete_unlock:
2596 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2597 spin_unlock_irqrestore(&lockres->l_lock, flags);
2598
2599 wake_up(&lockres->l_event);
2600
2601 mlog_exit_void();
2602 }
2603
2604 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2605 struct ocfs2_lock_res *lockres)
2606 {
2607 int ret;
2608 unsigned long flags;
2609 u32 lkm_flags = 0;
2610
2611 /* We didn't get anywhere near actually using this lockres. */
2612 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2613 goto out;
2614
2615 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2616 lkm_flags |= DLM_LKF_VALBLK;
2617
2618 spin_lock_irqsave(&lockres->l_lock, flags);
2619
2620 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2621 "lockres %s, flags 0x%lx\n",
2622 lockres->l_name, lockres->l_flags);
2623
2624 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2625 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2626 "%u, unlock_action = %u\n",
2627 lockres->l_name, lockres->l_flags, lockres->l_action,
2628 lockres->l_unlock_action);
2629
2630 spin_unlock_irqrestore(&lockres->l_lock, flags);
2631
2632 /* XXX: Today we just wait on any busy
2633 * locks... Perhaps we need to cancel converts in the
2634 * future? */
2635 ocfs2_wait_on_busy_lock(lockres);
2636
2637 spin_lock_irqsave(&lockres->l_lock, flags);
2638 }
2639
2640 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2641 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2642 lockres->l_level == DLM_LOCK_EX &&
2643 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2644 lockres->l_ops->set_lvb(lockres);
2645 }
2646
2647 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2648 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2649 lockres->l_name);
2650 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2651 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2652
2653 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2654 spin_unlock_irqrestore(&lockres->l_lock, flags);
2655 goto out;
2656 }
2657
2658 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2659
2660 /* make sure we never get here while waiting for an ast to
2661 * fire. */
2662 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2663
2664 /* is this necessary? */
2665 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2666 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2667 spin_unlock_irqrestore(&lockres->l_lock, flags);
2668
2669 mlog(0, "lock %s\n", lockres->l_name);
2670
2671 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
2672 lockres);
2673 if (ret) {
2674 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2675 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2676 /* XXX Need to abstract this */
2677 dlm_print_one_lock(lockres->l_lksb.lksb_o2dlm.lockid);
2678 BUG();
2679 }
2680 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
2681 lockres->l_name);
2682
2683 ocfs2_wait_on_busy_lock(lockres);
2684 out:
2685 mlog_exit(0);
2686 return 0;
2687 }
2688
2689 /* Mark the lockres as being dropped. It will no longer be
2690 * queued if blocking, but we still may have to wait on it
2691 * being dequeued from the downconvert thread before we can consider
2692 * it safe to drop.
2693 *
2694 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2695 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2696 {
2697 int status;
2698 struct ocfs2_mask_waiter mw;
2699 unsigned long flags;
2700
2701 ocfs2_init_mask_waiter(&mw);
2702
2703 spin_lock_irqsave(&lockres->l_lock, flags);
2704 lockres->l_flags |= OCFS2_LOCK_FREEING;
2705 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2706 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2707 spin_unlock_irqrestore(&lockres->l_lock, flags);
2708
2709 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2710
2711 status = ocfs2_wait_for_mask(&mw);
2712 if (status)
2713 mlog_errno(status);
2714
2715 spin_lock_irqsave(&lockres->l_lock, flags);
2716 }
2717 spin_unlock_irqrestore(&lockres->l_lock, flags);
2718 }
2719
2720 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2721 struct ocfs2_lock_res *lockres)
2722 {
2723 int ret;
2724
2725 ocfs2_mark_lockres_freeing(lockres);
2726 ret = ocfs2_drop_lock(osb, lockres);
2727 if (ret)
2728 mlog_errno(ret);
2729 }
2730
2731 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2732 {
2733 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2734 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2735 }
2736
2737 int ocfs2_drop_inode_locks(struct inode *inode)
2738 {
2739 int status, err;
2740
2741 mlog_entry_void();
2742
2743 /* No need to call ocfs2_mark_lockres_freeing here -
2744 * ocfs2_clear_inode has done it for us. */
2745
2746 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2747 &OCFS2_I(inode)->ip_open_lockres);
2748 if (err < 0)
2749 mlog_errno(err);
2750
2751 status = err;
2752
2753 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2754 &OCFS2_I(inode)->ip_inode_lockres);
2755 if (err < 0)
2756 mlog_errno(err);
2757 if (err < 0 && !status)
2758 status = err;
2759
2760 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2761 &OCFS2_I(inode)->ip_rw_lockres);
2762 if (err < 0)
2763 mlog_errno(err);
2764 if (err < 0 && !status)
2765 status = err;
2766
2767 mlog_exit(status);
2768 return status;
2769 }
2770
2771 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2772 int new_level)
2773 {
2774 assert_spin_locked(&lockres->l_lock);
2775
2776 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
2777
2778 if (lockres->l_level <= new_level) {
2779 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
2780 lockres->l_level, new_level);
2781 BUG();
2782 }
2783
2784 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2785 lockres->l_name, new_level, lockres->l_blocking);
2786
2787 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2788 lockres->l_requested = new_level;
2789 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2790 }
2791
2792 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2793 struct ocfs2_lock_res *lockres,
2794 int new_level,
2795 int lvb)
2796 {
2797 int ret;
2798 u32 dlm_flags = DLM_LKF_CONVERT;
2799
2800 mlog_entry_void();
2801
2802 if (lvb)
2803 dlm_flags |= DLM_LKF_VALBLK;
2804
2805 ret = ocfs2_dlm_lock(osb->cconn,
2806 new_level,
2807 &lockres->l_lksb,
2808 dlm_flags,
2809 lockres->l_name,
2810 OCFS2_LOCK_ID_MAX_LEN - 1,
2811 lockres);
2812 if (ret) {
2813 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
2814 ocfs2_recover_from_dlm_error(lockres, 1);
2815 goto bail;
2816 }
2817
2818 ret = 0;
2819 bail:
2820 mlog_exit(ret);
2821 return ret;
2822 }
2823
2824 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
2825 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2826 struct ocfs2_lock_res *lockres)
2827 {
2828 assert_spin_locked(&lockres->l_lock);
2829
2830 mlog_entry_void();
2831 mlog(0, "lock %s\n", lockres->l_name);
2832
2833 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2834 /* If we're already trying to cancel a lock conversion
2835 * then just drop the spinlock and allow the caller to
2836 * requeue this lock. */
2837
2838 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2839 return 0;
2840 }
2841
2842 /* were we in a convert when we got the bast fire? */
2843 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2844 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2845 /* set things up for the unlockast to know to just
2846 * clear out the ast_action and unset busy, etc. */
2847 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2848
2849 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2850 "lock %s, invalid flags: 0x%lx\n",
2851 lockres->l_name, lockres->l_flags);
2852
2853 return 1;
2854 }
2855
2856 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2857 struct ocfs2_lock_res *lockres)
2858 {
2859 int ret;
2860
2861 mlog_entry_void();
2862 mlog(0, "lock %s\n", lockres->l_name);
2863
2864 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
2865 DLM_LKF_CANCEL, lockres);
2866 if (ret) {
2867 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
2868 ocfs2_recover_from_dlm_error(lockres, 0);
2869 }
2870
2871 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
2872
2873 mlog_exit(ret);
2874 return ret;
2875 }
2876
2877 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2878 struct ocfs2_lock_res *lockres,
2879 struct ocfs2_unblock_ctl *ctl)
2880 {
2881 unsigned long flags;
2882 int blocking;
2883 int new_level;
2884 int ret = 0;
2885 int set_lvb = 0;
2886
2887 mlog_entry_void();
2888
2889 spin_lock_irqsave(&lockres->l_lock, flags);
2890
2891 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2892
2893 recheck:
2894 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2895 ctl->requeue = 1;
2896 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2897 spin_unlock_irqrestore(&lockres->l_lock, flags);
2898 if (ret) {
2899 ret = ocfs2_cancel_convert(osb, lockres);
2900 if (ret < 0)
2901 mlog_errno(ret);
2902 }
2903 goto leave;
2904 }
2905
2906 /* if we're blocking an exclusive and we have *any* holders,
2907 * then requeue. */
2908 if ((lockres->l_blocking == DLM_LOCK_EX)
2909 && (lockres->l_ex_holders || lockres->l_ro_holders))
2910 goto leave_requeue;
2911
2912 /* If it's a PR we're blocking, then only
2913 * requeue if we've got any EX holders */
2914 if (lockres->l_blocking == DLM_LOCK_PR &&
2915 lockres->l_ex_holders)
2916 goto leave_requeue;
2917
2918 /*
2919 * Can we get a lock in this state if the holder counts are
2920 * zero? The meta data unblock code used to check this.
2921 */
2922 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2923 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2924 goto leave_requeue;
2925
2926 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2927
2928 if (lockres->l_ops->check_downconvert
2929 && !lockres->l_ops->check_downconvert(lockres, new_level))
2930 goto leave_requeue;
2931
2932 /* If we get here, then we know that there are no more
2933 * incompatible holders (and anyone asking for an incompatible
2934 * lock is blocked). We can now downconvert the lock */
2935 if (!lockres->l_ops->downconvert_worker)
2936 goto downconvert;
2937
2938 /* Some lockres types want to do a bit of work before
2939 * downconverting a lock. Allow that here. The worker function
2940 * may sleep, so we save off a copy of what we're blocking as
2941 * it may change while we're not holding the spin lock. */
2942 blocking = lockres->l_blocking;
2943 spin_unlock_irqrestore(&lockres->l_lock, flags);
2944
2945 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2946
2947 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2948 goto leave;
2949
2950 spin_lock_irqsave(&lockres->l_lock, flags);
2951 if (blocking != lockres->l_blocking) {
2952 /* If this changed underneath us, then we can't drop
2953 * it just yet. */
2954 goto recheck;
2955 }
2956
2957 downconvert:
2958 ctl->requeue = 0;
2959
2960 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2961 if (lockres->l_level == DLM_LOCK_EX)
2962 set_lvb = 1;
2963
2964 /*
2965 * We only set the lvb if the lock has been fully
2966 * refreshed - otherwise we risk setting stale
2967 * data. Otherwise, there's no need to actually clear
2968 * out the lvb here as it's value is still valid.
2969 */
2970 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2971 lockres->l_ops->set_lvb(lockres);
2972 }
2973
2974 ocfs2_prepare_downconvert(lockres, new_level);
2975 spin_unlock_irqrestore(&lockres->l_lock, flags);
2976 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2977 leave:
2978 mlog_exit(ret);
2979 return ret;
2980
2981 leave_requeue:
2982 spin_unlock_irqrestore(&lockres->l_lock, flags);
2983 ctl->requeue = 1;
2984
2985 mlog_exit(0);
2986 return 0;
2987 }
2988
2989 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2990 int blocking)
2991 {
2992 struct inode *inode;
2993 struct address_space *mapping;
2994
2995 inode = ocfs2_lock_res_inode(lockres);
2996 mapping = inode->i_mapping;
2997
2998 if (!S_ISREG(inode->i_mode))
2999 goto out;
3000
3001 /*
3002 * We need this before the filemap_fdatawrite() so that it can
3003 * transfer the dirty bit from the PTE to the
3004 * page. Unfortunately this means that even for EX->PR
3005 * downconverts, we'll lose our mappings and have to build
3006 * them up again.
3007 */
3008 unmap_mapping_range(mapping, 0, 0, 0);
3009
3010 if (filemap_fdatawrite(mapping)) {
3011 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3012 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3013 }
3014 sync_mapping_buffers(mapping);
3015 if (blocking == DLM_LOCK_EX) {
3016 truncate_inode_pages(mapping, 0);
3017 } else {
3018 /* We only need to wait on the I/O if we're not also
3019 * truncating pages because truncate_inode_pages waits
3020 * for us above. We don't truncate pages if we're
3021 * blocking anything < EXMODE because we want to keep
3022 * them around in that case. */
3023 filemap_fdatawait(mapping);
3024 }
3025
3026 out:
3027 return UNBLOCK_CONTINUE;
3028 }
3029
3030 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3031 int new_level)
3032 {
3033 struct inode *inode = ocfs2_lock_res_inode(lockres);
3034 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3035
3036 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3037 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3038
3039 if (checkpointed)
3040 return 1;
3041
3042 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3043 return 0;
3044 }
3045
3046 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3047 {
3048 struct inode *inode = ocfs2_lock_res_inode(lockres);
3049
3050 __ocfs2_stuff_meta_lvb(inode);
3051 }
3052
3053 /*
3054 * Does the final reference drop on our dentry lock. Right now this
3055 * happens in the downconvert thread, but we could choose to simplify the
3056 * dlmglue API and push these off to the ocfs2_wq in the future.
3057 */
3058 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3059 struct ocfs2_lock_res *lockres)
3060 {
3061 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3062 ocfs2_dentry_lock_put(osb, dl);
3063 }
3064
3065 /*
3066 * d_delete() matching dentries before the lock downconvert.
3067 *
3068 * At this point, any process waiting to destroy the
3069 * dentry_lock due to last ref count is stopped by the
3070 * OCFS2_LOCK_QUEUED flag.
3071 *
3072 * We have two potential problems
3073 *
3074 * 1) If we do the last reference drop on our dentry_lock (via dput)
3075 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3076 * the downconvert to finish. Instead we take an elevated
3077 * reference and push the drop until after we've completed our
3078 * unblock processing.
3079 *
3080 * 2) There might be another process with a final reference,
3081 * waiting on us to finish processing. If this is the case, we
3082 * detect it and exit out - there's no more dentries anyway.
3083 */
3084 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3085 int blocking)
3086 {
3087 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3088 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3089 struct dentry *dentry;
3090 unsigned long flags;
3091 int extra_ref = 0;
3092
3093 /*
3094 * This node is blocking another node from getting a read
3095 * lock. This happens when we've renamed within a
3096 * directory. We've forced the other nodes to d_delete(), but
3097 * we never actually dropped our lock because it's still
3098 * valid. The downconvert code will retain a PR for this node,
3099 * so there's no further work to do.
3100 */
3101 if (blocking == DLM_LOCK_PR)
3102 return UNBLOCK_CONTINUE;
3103
3104 /*
3105 * Mark this inode as potentially orphaned. The code in
3106 * ocfs2_delete_inode() will figure out whether it actually
3107 * needs to be freed or not.
3108 */
3109 spin_lock(&oi->ip_lock);
3110 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3111 spin_unlock(&oi->ip_lock);
3112
3113 /*
3114 * Yuck. We need to make sure however that the check of
3115 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3116 * respect to a reference decrement or the setting of that
3117 * flag.
3118 */
3119 spin_lock_irqsave(&lockres->l_lock, flags);
3120 spin_lock(&dentry_attach_lock);
3121 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3122 && dl->dl_count) {
3123 dl->dl_count++;
3124 extra_ref = 1;
3125 }
3126 spin_unlock(&dentry_attach_lock);
3127 spin_unlock_irqrestore(&lockres->l_lock, flags);
3128
3129 mlog(0, "extra_ref = %d\n", extra_ref);
3130
3131 /*
3132 * We have a process waiting on us in ocfs2_dentry_iput(),
3133 * which means we can't have any more outstanding
3134 * aliases. There's no need to do any more work.
3135 */
3136 if (!extra_ref)
3137 return UNBLOCK_CONTINUE;
3138
3139 spin_lock(&dentry_attach_lock);
3140 while (1) {
3141 dentry = ocfs2_find_local_alias(dl->dl_inode,
3142 dl->dl_parent_blkno, 1);
3143 if (!dentry)
3144 break;
3145 spin_unlock(&dentry_attach_lock);
3146
3147 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3148 dentry->d_name.name);
3149
3150 /*
3151 * The following dcache calls may do an
3152 * iput(). Normally we don't want that from the
3153 * downconverting thread, but in this case it's ok
3154 * because the requesting node already has an
3155 * exclusive lock on the inode, so it can't be queued
3156 * for a downconvert.
3157 */
3158 d_delete(dentry);
3159 dput(dentry);
3160
3161 spin_lock(&dentry_attach_lock);
3162 }
3163 spin_unlock(&dentry_attach_lock);
3164
3165 /*
3166 * If we are the last holder of this dentry lock, there is no
3167 * reason to downconvert so skip straight to the unlock.
3168 */
3169 if (dl->dl_count == 1)
3170 return UNBLOCK_STOP_POST;
3171
3172 return UNBLOCK_CONTINUE_POST;
3173 }
3174
3175 /*
3176 * This is the filesystem locking protocol. It provides the lock handling
3177 * hooks for the underlying DLM. It has a maximum version number.
3178 * The version number allows interoperability with systems running at
3179 * the same major number and an equal or smaller minor number.
3180 *
3181 * Whenever the filesystem does new things with locks (adds or removes a
3182 * lock, orders them differently, does different things underneath a lock),
3183 * the version must be changed. The protocol is negotiated when joining
3184 * the dlm domain. A node may join the domain if its major version is
3185 * identical to all other nodes and its minor version is greater than
3186 * or equal to all other nodes. When its minor version is greater than
3187 * the other nodes, it will run at the minor version specified by the
3188 * other nodes.
3189 *
3190 * If a locking change is made that will not be compatible with older
3191 * versions, the major number must be increased and the minor version set
3192 * to zero. If a change merely adds a behavior that can be disabled when
3193 * speaking to older versions, the minor version must be increased. If a
3194 * change adds a fully backwards compatible change (eg, LVB changes that
3195 * are just ignored by older versions), the version does not need to be
3196 * updated.
3197 */
3198 static struct ocfs2_locking_protocol lproto = {
3199 .lp_max_version = {
3200 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3201 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3202 },
3203 .lp_lock_ast = ocfs2_locking_ast,
3204 .lp_blocking_ast = ocfs2_blocking_ast,
3205 .lp_unlock_ast = ocfs2_unlock_ast,
3206 };
3207
3208 /* This interface isn't the final one, hence the less-than-perfect names */
3209 void dlmglue_init_stack(void)
3210 {
3211 o2cb_get_stack(&lproto);
3212 }
3213
3214 void dlmglue_exit_stack(void)
3215 {
3216 o2cb_put_stack();
3217 }
3218
3219 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3220 struct ocfs2_lock_res *lockres)
3221 {
3222 int status;
3223 struct ocfs2_unblock_ctl ctl = {0, 0,};
3224 unsigned long flags;
3225
3226 /* Our reference to the lockres in this function can be
3227 * considered valid until we remove the OCFS2_LOCK_QUEUED
3228 * flag. */
3229
3230 mlog_entry_void();
3231
3232 BUG_ON(!lockres);
3233 BUG_ON(!lockres->l_ops);
3234
3235 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3236
3237 /* Detect whether a lock has been marked as going away while
3238 * the downconvert thread was processing other things. A lock can
3239 * still be marked with OCFS2_LOCK_FREEING after this check,
3240 * but short circuiting here will still save us some
3241 * performance. */
3242 spin_lock_irqsave(&lockres->l_lock, flags);
3243 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3244 goto unqueue;
3245 spin_unlock_irqrestore(&lockres->l_lock, flags);
3246
3247 status = ocfs2_unblock_lock(osb, lockres, &ctl);
3248 if (status < 0)
3249 mlog_errno(status);
3250
3251 spin_lock_irqsave(&lockres->l_lock, flags);
3252 unqueue:
3253 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3254 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3255 } else
3256 ocfs2_schedule_blocked_lock(osb, lockres);
3257
3258 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3259 ctl.requeue ? "yes" : "no");
3260 spin_unlock_irqrestore(&lockres->l_lock, flags);
3261
3262 if (ctl.unblock_action != UNBLOCK_CONTINUE
3263 && lockres->l_ops->post_unlock)
3264 lockres->l_ops->post_unlock(osb, lockres);
3265
3266 mlog_exit_void();
3267 }
3268
3269 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3270 struct ocfs2_lock_res *lockres)
3271 {
3272 mlog_entry_void();
3273
3274 assert_spin_locked(&lockres->l_lock);
3275
3276 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3277 /* Do not schedule a lock for downconvert when it's on
3278 * the way to destruction - any nodes wanting access
3279 * to the resource will get it soon. */
3280 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3281 lockres->l_name, lockres->l_flags);
3282 return;
3283 }
3284
3285 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3286
3287 spin_lock(&osb->dc_task_lock);
3288 if (list_empty(&lockres->l_blocked_list)) {
3289 list_add_tail(&lockres->l_blocked_list,
3290 &osb->blocked_lock_list);
3291 osb->blocked_lock_count++;
3292 }
3293 spin_unlock(&osb->dc_task_lock);
3294
3295 mlog_exit_void();
3296 }
3297
3298 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3299 {
3300 unsigned long processed;
3301 struct ocfs2_lock_res *lockres;
3302
3303 mlog_entry_void();
3304
3305 spin_lock(&osb->dc_task_lock);
3306 /* grab this early so we know to try again if a state change and
3307 * wake happens part-way through our work */
3308 osb->dc_work_sequence = osb->dc_wake_sequence;
3309
3310 processed = osb->blocked_lock_count;
3311 while (processed) {
3312 BUG_ON(list_empty(&osb->blocked_lock_list));
3313
3314 lockres = list_entry(osb->blocked_lock_list.next,
3315 struct ocfs2_lock_res, l_blocked_list);
3316 list_del_init(&lockres->l_blocked_list);
3317 osb->blocked_lock_count--;
3318 spin_unlock(&osb->dc_task_lock);
3319
3320 BUG_ON(!processed);
3321 processed--;
3322
3323 ocfs2_process_blocked_lock(osb, lockres);
3324
3325 spin_lock(&osb->dc_task_lock);
3326 }
3327 spin_unlock(&osb->dc_task_lock);
3328
3329 mlog_exit_void();
3330 }
3331
3332 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3333 {
3334 int empty = 0;
3335
3336 spin_lock(&osb->dc_task_lock);
3337 if (list_empty(&osb->blocked_lock_list))
3338 empty = 1;
3339
3340 spin_unlock(&osb->dc_task_lock);
3341 return empty;
3342 }
3343
3344 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3345 {
3346 int should_wake = 0;
3347
3348 spin_lock(&osb->dc_task_lock);
3349 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3350 should_wake = 1;
3351 spin_unlock(&osb->dc_task_lock);
3352
3353 return should_wake;
3354 }
3355
3356 static int ocfs2_downconvert_thread(void *arg)
3357 {
3358 int status = 0;
3359 struct ocfs2_super *osb = arg;
3360
3361 /* only quit once we've been asked to stop and there is no more
3362 * work available */
3363 while (!(kthread_should_stop() &&
3364 ocfs2_downconvert_thread_lists_empty(osb))) {
3365
3366 wait_event_interruptible(osb->dc_event,
3367 ocfs2_downconvert_thread_should_wake(osb) ||
3368 kthread_should_stop());
3369
3370 mlog(0, "downconvert_thread: awoken\n");
3371
3372 ocfs2_downconvert_thread_do_work(osb);
3373 }
3374
3375 osb->dc_task = NULL;
3376 return status;
3377 }
3378
3379 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3380 {
3381 spin_lock(&osb->dc_task_lock);
3382 /* make sure the voting thread gets a swipe at whatever changes
3383 * the caller may have made to the voting state */
3384 osb->dc_wake_sequence++;
3385 spin_unlock(&osb->dc_task_lock);
3386 wake_up(&osb->dc_event);
3387 }
This page took 0.159779 seconds and 5 git commands to generate.