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