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