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