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