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