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