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