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