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