blob: 152a0fc4e9051411c8ced645e7abb9637a774a0a [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Fashehccd979b2005-12-15 14:31:24 -08002/* -*- 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 Fashehccd979b2005-12-15 14:31:24 -080010 */
11
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/highmem.h>
15#include <linux/mm.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080016#include <linux/kthread.h>
17#include <linux/pagemap.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070020#include <linux/time.h>
Jan Kara9e33d692008-08-25 19:56:50 +020021#include <linux/quotaops.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080023
Mark Fashehccd979b2005-12-15 14:31:24 -080024#define MLOG_MASK_PREFIX ML_DLM_GLUE
25#include <cluster/masklog.h>
26
27#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080028#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080029
30#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070031#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080032#include "dlmglue.h"
33#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080034#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080035#include "heartbeat.h"
36#include "inode.h"
37#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080038#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "slot_map.h"
40#include "super.h"
41#include "uptodate.h"
Jan Kara9e33d692008-08-25 19:56:50 +020042#include "quota.h"
Tao Ma8dec98e2009-08-18 11:19:58 +080043#include "refcounttree.h"
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010044#include "acl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#include "buffer_head_io.h"
47
48struct 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 Mushran8ddb7b02008-05-13 13:45:15 -070054#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -080055 ktime_t mw_lock_start;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070056#endif
Mark Fashehccd979b2005-12-15 14:31:24 -080057};
58
Mark Fasheh54a7e752006-09-12 21:49:13 -070059static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
60static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080061static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Jan Kara9e33d692008-08-25 19:56:50 +020062static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080063
Mark Fashehd680efe2006-09-08 14:14:34 -070064/*
Mark Fashehcc567d82006-09-13 21:52:21 -070065 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070066 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070067 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070068 * and ocfs2_process_blocked_lock()
69 *
70 */
71enum 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
79struct ocfs2_unblock_ctl {
80 int requeue;
81 enum ocfs2_unblock_action unblock_action;
82};
83
Jan Karacb257972009-06-04 15:26:50 +020084/* Lockdep class keys */
zhong jiang1cff5142018-10-12 21:34:26 -070085#ifdef CONFIG_DEBUG_LOCK_ALLOC
Colin Ian King480bd562018-08-17 15:44:31 -070086static struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
zhong jiang1cff5142018-10-12 21:34:26 -070087#endif
Jan Karacb257972009-06-04 15:26:50 +020088
Mark Fasheh810d5ae2006-09-13 21:39:52 -070089static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
90 int new_level);
91static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
92
Mark Fashehcc567d82006-09-13 21:52:21 -070093static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
94 int blocking);
95
Mark Fashehcc567d82006-09-13 21:52:21 -070096static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
97 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -070098
99static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
100 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800101
Jan Kara9e33d692008-08-25 19:56:50 +0200102static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700103
Tao Ma8dec98e2009-08-18 11:19:58 +0800104static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
105 int new_level);
106static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
107 int blocking);
108
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700109#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. */
112static void ocfs2_dump_meta_lvb_info(u64 level,
113 const char *function,
114 unsigned int line,
115 struct ocfs2_lock_res *lockres)
116{
Mark Fasheha641dc22008-12-24 16:03:48 -0800117 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700118
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 Fashehf625c972006-09-12 21:24:53 -0700137/*
138 * OCFS2 Lock Resource Operations
139 *
140 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700141 *
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 Fashehf625c972006-09-12 21:24:53 -0700149 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800150struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700151 /*
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 Fashehb5e500e2006-09-13 22:01:16 -0700156
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700157 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700158 * 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 Fasheh0d5dc6c2006-09-14 14:44:51 -0700162 *
163 * The exact semantics of when this is called are controlled
164 * by ->downconvert_worker()
165 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700166 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700167
168 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700169 * 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 Fasheh5ef0d4e2006-09-13 21:21:52 -0700181 * 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 Fashehcc567d82006-09-13 21:52:21 -0700192 * 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 Fashehf625c972006-09-12 21:24:53 -0700203 * LOCK_TYPE_* flags which describe the specific requirements
204 * of a lock type. Descriptions of each individual flag follow.
205 */
206 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800207};
208
Mark Fashehf625c972006-09-12 21:24:53 -0700209/*
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 Fashehb80fc012006-09-12 22:08:14 -0700219/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700220 * Indicate that a lock type makes use of the lock value block. The
221 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700222 */
223#define LOCK_TYPE_USES_LVB 0x2
224
Mark Fashehccd979b2005-12-15 14:31:24 -0800225static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700226 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700227 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800228};
229
Mark Fashehe63aecb62007-10-18 15:30:42 -0700230static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700231 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700232 .check_downconvert = ocfs2_check_meta_downconvert,
233 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700234 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700235 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800236};
237
Mark Fashehccd979b2005-12-15 14:31:24 -0800238static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700239 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800240};
241
242static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700243 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800244};
245
wengang wang6ca497a2009-03-06 21:29:10 +0800246static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
247 .flags = 0,
248};
249
Gang He4882abe2018-01-31 16:15:10 -0800250static struct ocfs2_lock_res_ops ocfs2_trim_fs_lops = {
251 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
252};
253
Srinivas Eeda83273932009-06-03 17:02:55 -0700254static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
255 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
256};
257
Mark Fashehd680efe2006-09-08 14:14:34 -0700258static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700259 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700260 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700261 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700262 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700263};
264
Tiger Yang50008632007-03-20 16:01:38 -0700265static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
266 .get_osb = ocfs2_get_inode_osb,
267 .flags = 0,
268};
269
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800270static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
271 .get_osb = ocfs2_get_file_osb,
272 .flags = 0,
273};
274
Jan Kara9e33d692008-08-25 19:56:50 +0200275static 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 Ma8dec98e2009-08-18 11:19:58 +0800281static 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 Fashehccd979b2005-12-15 14:31:24 -0800287static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
288{
289 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700290 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
291 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800292}
293
Joel Beckerc0e413382010-01-29 14:46:44 -0800294static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
Joel Beckera796d282010-01-28 19:22:39 -0800295{
296 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
297}
298
Mark Fashehccd979b2005-12-15 14:31:24 -0800299static 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 Fashehd680efe2006-09-08 14:14:34 -0700306static 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 Kara9e33d692008-08-25 19:56:50 +0200313static 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 Ma8dec98e2009-08-18 11:19:58 +0800320static inline struct ocfs2_refcount_tree *
321ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
322{
323 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
324}
325
Mark Fasheh54a7e752006-09-12 21:49:13 -0700326static 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 Fashehccd979b2005-12-15 14:31:24 -0800334static int ocfs2_lock_create(struct ocfs2_super *osb,
335 struct ocfs2_lock_res *lockres,
336 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800337 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800338static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
339 int wanted);
Jan Karacb257972009-06-04 15:26:50 +0200340static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
341 struct ocfs2_lock_res *lockres,
342 int level, unsigned long caller_ip);
343static 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 Fashehccd979b2005-12-15 14:31:24 -0800350static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
351static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
352static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
353static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
354static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres);
356static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
357 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800358#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 Fashehccd979b2005-12-15 14:31:24 -0800366} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700367static int ocfs2_downconvert_thread(void *arg);
368static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700370static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 struct buffer_head **bh);
372static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
373static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800374static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
375 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800376static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
377 struct ocfs2_lock_res *lockres,
378 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800379 int lvb,
380 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800381static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
382 struct ocfs2_lock_res *lockres);
383static int ocfs2_cancel_convert(struct ocfs2_super *osb,
384 struct ocfs2_lock_res *lockres);
385
Mark Fashehccd979b2005-12-15 14:31:24 -0800386
Mark Fashehccd979b2005-12-15 14:31:24 -0800387static 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 Fashehccd979b2005-12-15 14:31:24 -0800394 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
395
Mark Fashehb06970532006-03-03 10:24:33 -0800396 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 Fashehccd979b2005-12-15 14:31:24 -0800399
400 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
401
402 mlog(0, "built lock resource with name: %s\n", name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800403}
404
Ingo Molnar34af9462006-06-27 02:53:55 -0700405static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800406
407static 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
417static 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 Mushran8ddb7b02008-05-13 13:45:15 -0700425#ifdef CONFIG_OCFS2_FS_STATS
426static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
427{
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700428 res->l_lock_refresh = 0;
Gang He5da844a2019-07-11 20:53:09 -0700429 res->l_lock_wait = 0;
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800430 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
431 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700432}
433
434static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
435 struct ocfs2_mask_waiter *mw, int ret)
436{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800437 u32 usec;
438 ktime_t kt;
439 struct ocfs2_lock_stats *stats;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700440
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800441 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 Mushran8ddb7b02008-05-13 13:45:15 -0700446 return;
447
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800448 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 */
roel16865b72011-12-12 23:40:51 +0100454 if (unlikely(stats->ls_gets == 0)) {
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800455 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 Mushran8ddb7b02008-05-13 13:45:15 -0700462 if (ret)
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800463 stats->ls_fail++;
Gang He8a7f5f42019-07-11 20:53:02 -0700464
465 stats->ls_last = ktime_to_us(ktime_get_real());
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700466}
467
468static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
469{
470 lockres->l_lock_refresh++;
471}
472
Gang He5da844a2019-07-11 20:53:09 -0700473static 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 Mushran8ddb7b02008-05-13 13:45:15 -0700488static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
489{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800490 mw->mw_lock_start = ktime_get();
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700491}
492#else
493static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
494{
495}
496static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
497 int level, struct ocfs2_mask_waiter *mw, int ret)
498{
499}
500static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
501{
502}
Gang He5da844a2019-07-11 20:53:09 -0700503static inline void ocfs2_track_lock_wait(struct ocfs2_lock_res *lockres)
504{
505}
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700506static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
507{
508}
509#endif
510
Mark Fashehccd979b2005-12-15 14:31:24 -0800511static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
512 struct ocfs2_lock_res *res,
513 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800514 struct ocfs2_lock_res_ops *ops,
515 void *priv)
516{
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 res->l_type = type;
518 res->l_ops = ops;
519 res->l_priv = priv;
520
Joel Beckerbd3e7612008-02-01 12:14:57 -0800521 res->l_level = DLM_LOCK_IV;
522 res->l_requested = DLM_LOCK_IV;
523 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 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 Mushran8ddb7b02008-05-13 13:45:15 -0700530
531 ocfs2_init_lock_stats(res);
Jan Karacb257972009-06-04 15:26:50 +0200532#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 Fashehccd979b2005-12-15 14:31:24 -0800539}
540
541void 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 Ren439a36b2017-02-22 15:40:41 -0800549 INIT_LIST_HEAD(&res->l_holders);
Mark Fashehccd979b2005-12-15 14:31:24 -0800550}
551
552void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
553 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700554 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800555 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 Fashehe63aecb62007-10-18 15:30:42 -0700564 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800565 break;
Tiger Yang50008632007-03-20 16:01:38 -0700566 case OCFS2_LOCK_TYPE_OPEN:
567 ops = &ocfs2_inode_open_lops;
568 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800569 default:
570 mlog_bug_on_msg(1, "type: %d\n", type);
571 ops = NULL; /* thanks, gcc */
572 break;
zhengbin5b43d642020-01-30 22:11:36 -0800573 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800574
Mark Fashehd680efe2006-09-08 14:14:34 -0700575 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700576 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700577 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
578}
579
Mark Fasheh54a7e752006-09-12 21:49:13 -0700580static 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 Kara9e33d692008-08-25 19:56:50 +0200587static 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 Fashehcf8e06f2007-12-20 16:43:10 -0800594static 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 Fashehd680efe2006-09-08 14:14:34 -0700601static __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 Fasheh54a7e752006-09-12 21:49:13 -0700611static 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 Fashehd680efe2006-09-08 14:14:34 -0700618void 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 Fashehccd979b2005-12-15 14:31:24 -0800656}
657
658static 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 Fashehd680efe2006-09-08 14:14:34 -0700664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
665 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800667 &ocfs2_super_lops, osb);
668}
669
670static 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 Fashehd680efe2006-09-08 14:14:34 -0700676 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 Fashehccd979b2005-12-15 14:31:24 -0800678 &ocfs2_rename_lops, osb);
679}
680
wengang wang6ca497a2009-03-06 21:29:10 +0800681static 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 He4882abe2018-01-31 16:15:10 -0800692void ocfs2_trim_fs_lock_res_init(struct ocfs2_super *osb)
693{
694 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
695
Gang He5500ab42019-03-05 15:41:45 -0800696 /* Only one trimfs thread are allowed to work at the same time. */
697 mutex_lock(&osb->obs_trim_fs_mutex);
698
Gang He4882abe2018-01-31 16:15:10 -0800699 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
705void 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 He5500ab42019-03-05 15:41:45 -0800711
712 mutex_unlock(&osb->obs_trim_fs_mutex);
Gang He4882abe2018-01-31 16:15:10 -0800713}
714
Srinivas Eeda83273932009-06-03 17:02:55 -0700715static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
716 struct ocfs2_super *osb)
717{
Srinivas Eeda83273932009-06-03 17:02:55 -0700718 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 Eeda83273932009-06-03 17:02:55 -0700722}
723
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800724void 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 Kara9e33d692008-08-25 19:56:50 +0200739void 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 Ma8dec98e2009-08-18 11:19:58 +0800750void 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 Fashehccd979b2005-12-15 14:31:24 -0800761void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
762{
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 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 Fashehccd979b2005-12-15 14:31:24 -0800788}
789
Eric Ren439a36b2017-02-22 15:40:41 -0800790/*
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 */
794static 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 Chen133b81f2018-06-07 17:04:43 -0700805static struct ocfs2_lock_holder *
806ocfs2_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 Ren439a36b2017-02-22 15:40:41 -0800822static 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 Ren439a36b2017-02-22 15:40:41 -0800832
Mark Fashehccd979b2005-12-15 14:31:24 -0800833static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
834 int level)
835{
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 BUG_ON(!lockres);
837
838 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800839 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800840 lockres->l_ex_holders++;
841 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800842 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800843 lockres->l_ro_holders++;
844 break;
845 default:
846 BUG();
847 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800848}
849
850static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
851 int level)
852{
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 BUG_ON(!lockres);
854
855 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800856 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 BUG_ON(!lockres->l_ex_holders);
858 lockres->l_ex_holders--;
859 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800860 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800861 BUG_ON(!lockres->l_ro_holders);
862 lockres->l_ro_holders--;
863 break;
864 default:
865 BUG();
866 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800867}
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. */
872static inline int ocfs2_highest_compat_lock_level(int level)
873{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800874 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800875
Joel Beckerbd3e7612008-02-01 12:14:57 -0800876 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 Fashehccd979b2005-12-15 14:31:24 -0800880 return new_level;
881}
882
883static void lockres_set_flags(struct ocfs2_lock_res *lockres,
884 unsigned long newflags)
885{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200886 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800887
888 assert_spin_locked(&lockres->l_lock);
889
890 lockres->l_flags = newflags;
891
Christoph Hellwig800deef2007-05-17 16:03:13 +0200892 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 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 He5da844a2019-07-11 20:53:09 -0700899 ocfs2_track_lock_wait(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 }
901}
902static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
903{
904 lockres_set_flags(lockres, lockres->l_flags | or);
905}
906static 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
912static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
913{
Mark Fashehccd979b2005-12-15 14:31:24 -0800914 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 Beckerbd3e7612008-02-01 12:14:57 -0800917 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800918
919 lockres->l_level = lockres->l_requested;
920 if (lockres->l_level <=
921 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800922 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800923 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
924 }
925 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800926}
927
928static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
929{
Mark Fashehccd979b2005-12-15 14:31:24 -0800930 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 Beckerbd3e7612008-02-01 12:14:57 -0800937 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700938 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800939 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
940
941 lockres->l_level = lockres->l_requested;
Sunil Mushrana1912822010-01-21 10:50:03 -0800942
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 jiufeid1e782382014-12-10 15:41:59 -0800947 * Do not prevent the dc thread from downconverting if NONBLOCK lock
948 * had already returned.
Sunil Mushrana1912822010-01-21 10:50:03 -0800949 */
Xue jiufeid1e782382014-12-10 15:41:59 -0800950 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 Mushrana1912822010-01-21 10:50:03 -0800954
Mark Fashehccd979b2005-12-15 14:31:24 -0800955 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800956}
957
958static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
959{
Roel Kluin3cf0c502007-10-27 00:20:36 +0200960 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800961 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
962
Joel Beckerbd3e7612008-02-01 12:14:57 -0800963 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700964 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
965 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800966 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 Fashehccd979b2005-12-15 14:31:24 -0800971}
972
Mark Fashehccd979b2005-12-15 14:31:24 -0800973static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
974 int level)
975{
976 int needs_downconvert = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800977
978 assert_spin_locked(&lockres->l_lock);
979
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 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 Mushran9b915182010-02-26 19:42:44 -0800992 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 Wang0b94a902010-01-21 10:50:02 -0800996 if (needs_downconvert)
997 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Tao Mac1e8d352011-03-07 16:43:21 +0800998 mlog(0, "needs_downconvert = %d\n", needs_downconvert);
Mark Fashehccd979b2005-12-15 14:31:24 -0800999 return needs_downconvert;
1000}
1001
Joel Beckerde551242008-02-01 14:45:08 -08001002/*
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() */
1061static 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() */
1087static 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
1098static 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 Beckerc0e413382010-01-29 14:46:44 -08001108static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001109{
Joel Beckera796d282010-01-28 19:22:39 -08001110 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehaa2623a2006-09-12 21:58:23 -07001111 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001112 int needs_downconvert;
1113 unsigned long flags;
1114
Joel Beckerbd3e7612008-02-01 12:14:57 -08001115 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001116
Sunil Mushran9b915182010-02-26 19:42:44 -08001117 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1118 "type %s\n", lockres->l_name, level, lockres->l_level,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001119 ocfs2_lock_type_string(lockres->l_type));
1120
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001121 /*
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 Fashehccd979b2005-12-15 14:31:24 -08001128 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 Fashehd680efe2006-09-08 14:14:34 -07001134 wake_up(&lockres->l_event);
1135
Mark Fasheh34d024f2007-09-24 15:56:19 -07001136 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001137}
1138
Joel Beckerc0e413382010-01-29 14:46:44 -08001139static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
Mark Fashehccd979b2005-12-15 14:31:24 -08001140{
Joel Beckera796d282010-01-28 19:22:39 -08001141 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Joel Beckerde551242008-02-01 14:45:08 -08001142 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001144 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001145
1146 spin_lock_irqsave(&lockres->l_lock, flags);
1147
David Teigland1693a5c2008-01-30 16:52:53 -08001148 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 Becker8f2c9c12008-02-01 12:16:57 -08001156 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001157 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001158 spin_unlock_irqrestore(&lockres->l_lock, flags);
1159 return;
1160 }
1161
Sunil Mushran9b915182010-02-26 19:42:44 -08001162 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 Fashehccd979b2005-12-15 14:31:24 -08001166 switch(lockres->l_action) {
1167 case OCFS2_AST_ATTACH:
1168 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001169 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 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 Mushran9b915182010-02-26 19:42:44 -08001178 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1179 "flags 0x%lx, unlock: %u\n",
Mark Fashehe92d57d2006-09-12 21:34:35 -07001180 lockres->l_name, lockres->l_action, lockres->l_flags,
1181 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 BUG();
1183 }
David Teigland1693a5c2008-01-30 16:52:53 -08001184out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001185 /* set it to something invalid so if we get called again we
1186 * can catch it. */
1187 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001188
Joel Beckerde551242008-02-01 14:45:08 -08001189 /* 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 Fashehccd979b2005-12-15 14:31:24 -08001201 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001202 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001203}
1204
Joel Becker553b5eb2010-01-29 17:19:06 -08001205static 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 Mushran9b915182010-02-26 19:42:44 -08001210 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1211 lockres->l_name, lockres->l_unlock_action);
Joel Becker553b5eb2010-01-29 17:19:06 -08001212
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 Becker553b5eb2010-01-29 17:19:06 -08001219 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 Becker553b5eb2010-01-29 17:19:06 -08001242}
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 */
1267static 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
1277void ocfs2_set_locking_protocol(void)
1278{
1279 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1280}
1281
Mark Fashehccd979b2005-12-15 14:31:24 -08001282static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1283 int convert)
1284{
1285 unsigned long flags;
1286
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 spin_lock_irqsave(&lockres->l_lock, flags);
1288 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Sunil Mushrana1912822010-01-21 10:50:03 -08001289 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 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 Fashehccd979b2005-12-15 14:31:24 -08001297}
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 */
1303static int ocfs2_lock_create(struct ocfs2_super *osb,
1304 struct ocfs2_lock_res *lockres,
1305 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001306 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001307{
1308 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001309 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001310 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001311
Joel Beckerbd3e7612008-02-01 12:14:57 -08001312 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001313 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 Beckerde551242008-02-01 14:45:08 -08001325 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 spin_unlock_irqrestore(&lockres->l_lock, flags);
1327
Joel Becker4670c462008-02-01 14:39:35 -08001328 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001329 level,
1330 &lockres->l_lksb,
1331 dlm_flags,
1332 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001333 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001334 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001335 if (ret) {
1336 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001337 ocfs2_recover_from_dlm_error(lockres, 1);
1338 }
1339
Joel Becker7431cd72008-02-01 12:15:37 -08001340 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341
1342bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 return ret;
1344}
1345
1346static 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
1359static 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
1366static 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. */
1376static 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
1384static 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 Mushran8ddb7b02008-05-13 13:45:15 -07001388 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001389}
1390
1391static 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 Sang16735d02013-11-14 14:32:02 -08001395 reinit_completion(&mw->mw_complete);
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 return mw->mw_status;
1397}
1398
1399static 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 He5da844a2019-07-11 20:53:09 -07001411 ocfs2_track_lock_wait(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001412}
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 jiufeid1e782382014-12-10 15:41:59 -08001416static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
Mark Fashehccd979b2005-12-15 14:31:24 -08001417 struct ocfs2_mask_waiter *mw)
1418{
Mark Fashehccd979b2005-12-15 14:31:24 -08001419 int ret = 0;
1420
Xue jiufeid1e782382014-12-10 15:41:59 -08001421 assert_spin_locked(&lockres->l_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 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 He5da844a2019-07-11 20:53:09 -07001428 ocfs2_track_lock_wait(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001429 }
Xue jiufeid1e782382014-12-10 15:41:59 -08001430
1431 return ret;
1432}
1433
1434static 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 Fashehccd979b2005-12-15 14:31:24 -08001442 spin_unlock_irqrestore(&lockres->l_lock, flags);
1443
1444 return ret;
1445
1446}
1447
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001448static 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 Sang16735d02013-11-14 14:32:02 -08001459 reinit_completion(&mw->mw_complete);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001460 return ret;
1461}
1462
Jan Karacb257972009-06-04 15:26:50 +02001463static 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 Fashehccd979b2005-12-15 14:31:24 -08001470{
1471 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001472 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 Beckerde551242008-02-01 14:45:08 -08001475 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001476 int noqueue_attempted = 0;
Xue jiufeid1e782382014-12-10 15:41:59 -08001477 int dlm_locked = 0;
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001478 int kick_dc = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479
alex chen2f2eca22015-04-14 15:43:49 -07001480 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) {
1481 mlog_errno(-EINVAL);
1482 return -EINVAL;
1483 }
1484
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 ocfs2_init_mask_waiter(&mw);
1486
Mark Fashehb80fc012006-09-12 22:08:14 -07001487 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001488 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001489
Mark Fashehccd979b2005-12-15 14:31:24 -08001490again:
1491 wait = 0;
1492
Sunil Mushrana1912822010-01-21 10:50:03 -08001493 spin_lock_irqsave(&lockres->l_lock, flags);
1494
Mark Fashehccd979b2005-12-15 14:31:24 -08001495 if (catch_signals && signal_pending(current)) {
1496 ret = -ERESTARTSYS;
Sunil Mushrana1912822010-01-21 10:50:03 -08001497 goto unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001498 }
1499
Mark Fashehccd979b2005-12-15 14:31:24 -08001500 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 Mushrana1912822010-01-21 10:50:03 -08001516 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 Fashehccd979b2005-12-15 14:31:24 -08001535 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 Teigland1693a5c2008-01-30 16:52:53 -08001545 if (noqueue_attempted > 0) {
1546 ret = -EAGAIN;
1547 goto unlock;
1548 }
1549 if (lkm_flags & DLM_LKF_NOQUEUE)
1550 noqueue_attempted = 1;
1551
Mark Fashehccd979b2005-12-15 14:31:24 -08001552 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 Fasheh019d1b22007-10-05 12:09:05 -07001556 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1557 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001558 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001559 } else {
1560 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001561 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001562 }
1563
Mark Fashehccd979b2005-12-15 14:31:24 -08001564 lockres->l_requested = level;
1565 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001566 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001567 spin_unlock_irqrestore(&lockres->l_lock, flags);
1568
Joel Beckerbd3e7612008-02-01 12:14:57 -08001569 BUG_ON(level == DLM_LOCK_IV);
1570 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001571
Sunil Mushran9b915182010-02-26 19:42:44 -08001572 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001573 lockres->l_name, lockres->l_level, level);
1574
1575 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001576 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001577 level,
1578 &lockres->l_lksb,
1579 lkm_flags,
1580 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001581 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001582 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001583 if (ret) {
1584 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1585 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001586 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001587 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001588 }
1589 ocfs2_recover_from_dlm_error(lockres, 1);
1590 goto out;
1591 }
Xue jiufeid1e782382014-12-10 15:41:59 -08001592 dlm_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001593
Coly Li73ac36e2009-01-07 18:09:16 -08001594 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001595 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 Mushrana1912822010-01-21 10:50:03 -08001605update_holders:
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 /* Ok, if we get here then we're good to go. */
1607 ocfs2_inc_holders(lockres, level);
1608
1609 ret = 0;
1610unlock:
Sunil Mushrana1912822010-01-21 10:50:03 -08001611 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1612
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001613 /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */
1614 kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED);
1615
Mark Fashehccd979b2005-12-15 14:31:24 -08001616 spin_unlock_irqrestore(&lockres->l_lock, flags);
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001617 if (kick_dc)
1618 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001619out:
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 jiufeid1e782382014-12-10 15:41:59 -08001631 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 Fashehccd979b2005-12-15 14:31:24 -08001637 ret = -EAGAIN;
Xue jiufeid1e782382014-12-10 15:41:59 -08001638 } else {
1639 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001640 goto again;
Xue jiufeid1e782382014-12-10 15:41:59 -08001641 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001642 }
1643 if (wait) {
1644 ret = ocfs2_wait_for_mask(&mw);
1645 if (ret == 0)
1646 goto again;
1647 mlog_errno(ret);
1648 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001649 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001650
Jan Karacb257972009-06-04 15:26:50 +02001651#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 Fashehccd979b2005-12-15 14:31:24 -08001663 return ret;
1664}
1665
Jan Karacb257972009-06-04 15:26:50 +02001666static 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
1677static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1678 struct ocfs2_lock_res *lockres,
1679 int level,
1680 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001681{
1682 unsigned long flags;
1683
Mark Fashehccd979b2005-12-15 14:31:24 -08001684 spin_lock_irqsave(&lockres->l_lock, flags);
1685 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001686 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001687 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001688#ifdef CONFIG_DEBUG_LOCK_ALLOC
1689 if (lockres->l_lockdep_map.key != NULL)
Qian Cai5facae42019-09-19 12:09:40 -04001690 rwsem_release(&lockres->l_lockdep_map, caller_ip);
Jan Karacb257972009-06-04 15:26:50 +02001691#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001692}
1693
Adrian Bunkda661162006-11-20 03:24:28 +01001694static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1695 struct ocfs2_lock_res *lockres,
1696 int ex,
1697 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001698{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001699 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001701 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001702
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 Fasheh24c19ef2006-09-22 17:28:19 -07001708 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001709}
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. */
1717int ocfs2_create_new_inode_locks(struct inode *inode)
1718{
1719 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001720 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001721
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 BUG_ON(!ocfs2_inode_is_new(inode));
1723
Mark Fashehb06970532006-03-03 10:24:33 -08001724 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001725
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 Fasheh24c19ef2006-09-22 17:28:19 -07001734 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001735 if (ret) {
1736 mlog_errno(ret);
1737 goto bail;
1738 }
1739
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001740 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001741 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001742 * don't use a generation in their lock names.
1743 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001744 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001745 if (ret) {
1746 mlog_errno(ret);
1747 goto bail;
1748 }
1749
Tiger Yang50008632007-03-20 16:01:38 -07001750 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
Joseph Qia8f24f1b2016-07-26 15:21:35 -07001751 if (ret)
Tiger Yang50008632007-03-20 16:01:38 -07001752 mlog_errno(ret);
Tiger Yang50008632007-03-20 16:01:38 -07001753
Mark Fashehccd979b2005-12-15 14:31:24 -08001754bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001755 return ret;
1756}
1757
1758int ocfs2_rw_lock(struct inode *inode, int write)
1759{
1760 int status, level;
1761 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001762 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001763
Mark Fashehb06970532006-03-03 10:24:33 -08001764 mlog(0, "inode %llu take %s RW lock\n",
1765 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001766 write ? "EXMODE" : "PRMODE");
1767
Tao Mac1e8d352011-03-07 16:43:21 +08001768 if (ocfs2_mount_local(osb))
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001769 return 0;
1770
Mark Fashehccd979b2005-12-15 14:31:24 -08001771 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1772
Joel Beckerbd3e7612008-02-01 12:14:57 -08001773 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001774
piaojun1119d3c2018-04-05 16:18:33 -07001775 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 if (status < 0)
1777 mlog_errno(status);
1778
Mark Fashehccd979b2005-12-15 14:31:24 -08001779 return status;
1780}
1781
Gang He06e7f132018-01-31 16:15:17 -08001782int 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 Fashehccd979b2005-12-15 14:31:24 -08001803void ocfs2_rw_unlock(struct inode *inode, int write)
1804{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001805 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001806 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001808
Mark Fashehb06970532006-03-03 10:24:33 -08001809 mlog(0, "inode %llu drop %s RW lock\n",
1810 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001811 write ? "EXMODE" : "PRMODE");
1812
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001813 if (!ocfs2_mount_local(osb))
piaojun1119d3c2018-04-05 16:18:33 -07001814 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001815}
1816
Tiger Yang50008632007-03-20 16:01:38 -07001817/*
1818 * ocfs2_open_lock always get PR mode lock.
1819 */
1820int 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 Yang50008632007-03-20 16:01:38 -07001826 mlog(0, "inode %llu take PRMODE open lock\n",
1827 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1828
Tiger Yang03efed82011-05-28 00:34:19 +08001829 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
Tiger Yang50008632007-03-20 16:01:38 -07001830 goto out;
1831
1832 lockres = &OCFS2_I(inode)->ip_open_lockres;
1833
piaojun1119d3c2018-04-05 16:18:33 -07001834 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001835 if (status < 0)
1836 mlog_errno(status);
1837
1838out:
Tiger Yang50008632007-03-20 16:01:38 -07001839 return status;
1840}
1841
1842int 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 Yang50008632007-03-20 16:01:38 -07001848 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 Yang03efed82011-05-28 00:34:19 +08001852 if (ocfs2_is_hard_readonly(osb)) {
1853 if (write)
1854 status = -EROFS;
1855 goto out;
1856 }
1857
Tiger Yang50008632007-03-20 16:01:38 -07001858 if (ocfs2_mount_local(osb))
1859 goto out;
1860
1861 lockres = &OCFS2_I(inode)->ip_open_lockres;
1862
Joel Beckerbd3e7612008-02-01 12:14:57 -08001863 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001864
1865 /*
1866 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001867 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001868 * other nodes and the -EAGAIN will indicate to the caller that
1869 * this inode is still in use.
1870 */
piaojun1119d3c2018-04-05 16:18:33 -07001871 status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001872
1873out:
Tiger Yang50008632007-03-20 16:01:38 -07001874 return status;
1875}
1876
1877/*
1878 * ocfs2_open_unlock unlock PR and EX mode open locks.
1879 */
1880void 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 Yang50008632007-03-20 16:01:38 -07001885 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)
piaojun1119d3c2018-04-05 16:18:33 -07001892 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001893 if(lockres->l_ex_holders)
piaojun1119d3c2018-04-05 16:18:33 -07001894 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001895
1896out:
Tao Mac1e8d352011-03-07 16:43:21 +08001897 return;
Tiger Yang50008632007-03-20 16:01:38 -07001898}
1899
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001900static 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
1910retry_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
1943out:
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 Mack3ad2f3f2010-02-03 08:01:28 +08001951 * separate path to the "low-level" dlm calls. In particular:
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001952 *
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 */
1968int ocfs2_file_lock(struct file *file, int ex, int trylock)
1969{
Mark Fashehe988cf12008-07-10 09:25:39 -07001970 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1971 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001972 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 Beckerbd3e7612008-02-01 12:14:57 -08001981 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001982 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 Fashehe988cf12008-07-10 09:25:39 -07001998 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001999 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 Fashehe988cf12008-07-10 09:25:39 -07002013 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002014 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 Becker4670c462008-02-01 14:39:35 -08002020 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Beckera796d282010-01-28 19:22:39 -08002021 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Becker7431cd72008-02-01 12:15:37 -08002022 if (ret) {
2023 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08002024 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002025 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 Rosaaf901ca2009-11-14 13:09:05 -02002048 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002049 */
2050 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08002051 } else if (!ret && (level > lockres->l_level)) {
2052 /* Trylock failed asynchronously */
2053 BUG_ON(!trylock);
2054 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002055 }
2056
2057out:
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
2064void ocfs2_file_unlock(struct file *file)
2065{
2066 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08002067 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002068 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 Fashehe988cf12008-07-10 09:25:39 -07002079 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002080 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 Beckerbd3e7612008-02-01 12:14:57 -08002091 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002092
Mark Fashehe988cf12008-07-10 09:25:39 -07002093 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002094 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2095 spin_unlock_irqrestore(&lockres->l_lock, flags);
2096
Mark Fashehe988cf12008-07-10 09:25:39 -07002097 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002098 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 Fasheh34d024f2007-09-24 15:56:19 -07002108static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
2109 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002110{
2111 int kick = 0;
2112
Mark Fashehccd979b2005-12-15 14:31:24 -08002113 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07002114 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08002115 * condition. */
2116 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
2117 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002118 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08002119 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
2120 kick = 1;
2121 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002122 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08002123 if (!lockres->l_ex_holders)
2124 kick = 1;
2125 break;
2126 default:
2127 BUG();
2128 }
2129 }
2130
2131 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07002132 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002133}
2134
Mark Fashehccd979b2005-12-15 14:31:24 -08002135#define OCFS2_SEC_BITS 34
Alex Shi31cc0c82020-04-01 21:03:28 -07002136#define OCFS2_SEC_SHIFT (64 - OCFS2_SEC_BITS)
Mark Fashehccd979b2005-12-15 14:31:24 -08002137#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 Bergmann3a3d1e52018-11-02 15:48:23 -07002141static u64 ocfs2_pack_timespec(struct timespec64 *spec)
Mark Fashehccd979b2005-12-15 14:31:24 -08002142{
2143 u64 res;
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002144 u64 sec = clamp_t(time64_t, spec->tv_sec, 0, 0x3ffffffffull);
Mark Fashehccd979b2005-12-15 14:31:24 -08002145 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 Fashehe63aecb62007-10-18 15:30:42 -07002154 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002155static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2156{
2157 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002158 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002159 struct ocfs2_meta_lvb *lvb;
2160
Mark Fasheha641dc22008-12-24 16:03:48 -08002161 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002162
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002163 /*
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 Fasheh4d3b83f2006-09-12 15:22:18 -07002173 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08002174 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2175 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
Eric W. Biederman03ab30f2013-01-31 16:59:23 -08002176 lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
2177 lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002178 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 Bergmann3a3d1e52018-11-02 15:48:23 -07002181 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
Mark Fashehccd979b2005-12-15 14:31:24 -08002182 lvb->lvb_ictime_packed =
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002183 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
Mark Fashehccd979b2005-12-15 14:31:24 -08002184 lvb->lvb_imtime_packed =
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002185 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002186 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002187 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002188 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002189
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002190out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002191 mlog_meta_lvb(0, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002192}
2193
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002194static void ocfs2_unpack_timespec(struct timespec64 *spec,
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 u64 packed_time)
2196{
2197 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2198 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2199}
2200
2201static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2202{
2203 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002204 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002205 struct ocfs2_meta_lvb *lvb;
2206
Mark Fashehccd979b2005-12-15 14:31:24 -08002207 mlog_meta_lvb(0, lockres);
2208
Mark Fasheha641dc22008-12-24 16:03:48 -08002209 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002210
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 Poetzlca4d1472006-07-03 17:27:12 -07002216 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002217 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002218 ocfs2_set_inode_flags(inode);
2219
Mark Fashehccd979b2005-12-15 14:31:24 -08002220 /* fast-symlinks are a special case */
2221 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2222 inode->i_blocks = 0;
2223 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002224 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002225
Eric W. Biederman03ab30f2013-01-31 16:59:23 -08002226 i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
2227 i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
Mark Fashehccd979b2005-12-15 14:31:24 -08002228 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002229 set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002230 ocfs2_unpack_timespec(&inode->i_atime,
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 be64_to_cpu(lvb->lvb_iatime_packed));
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002232 ocfs2_unpack_timespec(&inode->i_mtime,
Mark Fashehccd979b2005-12-15 14:31:24 -08002233 be64_to_cpu(lvb->lvb_imtime_packed));
Arnd Bergmann3a3d1e52018-11-02 15:48:23 -07002234 ocfs2_unpack_timespec(&inode->i_ctime,
Mark Fashehccd979b2005-12-15 14:31:24 -08002235 be64_to_cpu(lvb->lvb_ictime_packed));
2236 spin_unlock(&oi->ip_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08002237}
2238
Mark Fashehf9e2d822006-09-12 15:35:49 -07002239static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2240 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002241{
Mark Fasheha641dc22008-12-24 16:03:48 -08002242 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002243
Joel Becker1c520df2009-06-19 15:14:13 -07002244 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2245 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002246 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002247 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. */
2258static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2259{
2260 unsigned long flags;
2261 int status = 0;
2262
Mark Fashehccd979b2005-12-15 14:31:24 -08002263refresh_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;
2282bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002283 mlog(0, "status %d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002284 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. */
2289static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2290 int status)
2291{
2292 unsigned long flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08002293
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 Fashehccd979b2005-12-15 14:31:24 -08002301}
2302
2303/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002304static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002305 struct buffer_head **bh)
2306{
2307 int status = 0;
2308 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002309 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002310 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002312
Mark Fashehbe9e9862007-04-18 15:22:08 -07002313 if (ocfs2_mount_local(osb))
2314 goto bail;
2315
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 spin_lock(&oi->ip_lock);
2317 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002318 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002319 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002320 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002321 spin_unlock(&oi->ip_lock);
2322 status = -ENOENT;
2323 goto bail;
2324 }
2325 spin_unlock(&oi->ip_lock);
2326
Mark Fashehbe9e9862007-04-18 15:22:08 -07002327 if (!ocfs2_should_refresh_lock_res(lockres))
2328 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002329
2330 /* This will discard any caching information we might have had
2331 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002332 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002333
Mark Fasheh83418972007-04-23 18:53:12 -07002334 ocfs2_extent_map_trunc(inode, 0);
2335
Mark Fashehbe9e9862007-04-18 15:22:08 -07002336 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002337 mlog(0, "Trusting LVB on inode %llu\n",
2338 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002339 ocfs2_refresh_inode_from_lvb(inode);
2340 } else {
2341 /* Boo, we have to go to disk. */
2342 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002343 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002344 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 Beckerb657c952008-11-13 14:49:11 -08002351 * locking an invalid object. ocfs2_read_inode_block()
2352 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002353 *
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 Fashehccd979b2005-12-15 14:31:24 -08002359 mlog_bug_on_msg(inode->i_generation !=
2360 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002361 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002362 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002363 (unsigned long long)oi->ip_blkno,
2364 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002365 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 Fashehb06970532006-03-03 10:24:33 -08002368 "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 Fashehccd979b2005-12-15 14:31:24 -08002371 le32_to_cpu(fe->i_flags));
2372
2373 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002374 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002375 }
2376
2377 status = 0;
2378bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002379 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002380bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002381 return status;
2382}
2383
2384static 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 Beckerb657c952008-11-13 14:49:11 -08002399 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002400 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 Karacb257972009-06-04 15:26:50 +02002410int 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 Fashehccd979b2005-12-15 14:31:24 -08002415{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002416 int status, level, acquired;
2417 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002418 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002419 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2420 struct buffer_head *local_bh = NULL;
2421
Mark Fashehb06970532006-03-03 10:24:33 -08002422 mlog(0, "inode %llu, take %s META lock\n",
2423 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002424 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 Yang03efed82011-05-28 00:34:19 +08002433 goto getbh;
Mark Fashehccd979b2005-12-15 14:31:24 -08002434 }
2435
Eric Ren439a36b2017-02-22 15:40:41 -08002436 if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
2437 ocfs2_mount_local(osb))
2438 goto update;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002439
Mark Fashehccd979b2005-12-15 14:31:24 -08002440 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002441 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002442
Mark Fashehe63aecb62007-10-18 15:30:42 -07002443 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002444 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002445 dlm_flags = 0;
2446 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002447 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002448
Jan Karacb257972009-06-04 15:26:50 +02002449 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2450 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002451 if (status < 0) {
Zach Brown41003a72013-05-07 16:18:25 -07002452 if (status != -EAGAIN)
Mark Fashehccd979b2005-12-15 14:31:24 -08002453 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 Becker553abd02008-02-01 12:03:57 -08002465 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002466
Eric Ren439a36b2017-02-22 15:40:41 -08002467update:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002468 /*
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 Mushranc271c5c2006-12-05 17:56:35 -08002476 if (lockres)
2477 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002478 goto bail;
2479 }
2480
Mark Fashehccd979b2005-12-15 14:31:24 -08002481 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002482 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002483 * 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 Fashehe63aecb62007-10-18 15:30:42 -07002486 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002487 if (status < 0) {
2488 if (status != -ENOENT)
2489 mlog_errno(status);
2490 goto bail;
2491 }
Tiger Yang03efed82011-05-28 00:34:19 +08002492getbh:
Mark Fashehccd979b2005-12-15 14:31:24 -08002493 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 Fashehccd979b2005-12-15 14:31:24 -08002501bail:
2502 if (status < 0) {
2503 if (ret_bh && (*ret_bh)) {
2504 brelse(*ret_bh);
2505 *ret_bh = NULL;
2506 }
2507 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002508 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002509 }
2510
Markus Elfringa89bd892019-09-23 15:33:34 -07002511 brelse(local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002512 return status;
2513}
2514
2515/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002516 * This is working around a lock inversion between tasks acquiring DLM
2517 * locks while holding a page lock and the downconvert thread which
2518 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002519 *
2520 * ** These _with_page variantes are only intended to be called from aop
2521 * methods that hold page locks and return a very specific *positive* error
2522 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2523 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002524 * The DLM is called such that it returns -EAGAIN if it would have
2525 * blocked waiting for the downconvert thread. In that case we unlock
2526 * our page so the downconvert thread can make progress. Once we've
2527 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2528 * that called us can bubble that back up into the VFS who will then
2529 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002530 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002531int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002532 struct buffer_head **ret_bh,
2533 int ex,
2534 struct page *page)
2535{
2536 int ret;
2537
Mark Fashehe63aecb62007-10-18 15:30:42 -07002538 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002539 if (ret == -EAGAIN) {
2540 unlock_page(page);
Gang Heff26cc12018-01-31 16:14:48 -08002541 /*
2542 * If we can't get inode lock immediately, we should not return
2543 * directly here, since this will lead to a softlockup problem.
2544 * The method is to get a blocking lock and immediately unlock
2545 * before returning, this can avoid CPU resource waste due to
2546 * lots of retries, and benefits fairness in getting lock.
2547 */
2548 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2549 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002550 ret = AOP_TRUNCATED_PAGE;
2551 }
2552
2553 return ret;
2554}
2555
Mark Fashehe63aecb62007-10-18 15:30:42 -07002556int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002557 struct vfsmount *vfsmnt,
Gang Hec4c24162018-01-31 16:15:25 -08002558 int *level, int wait)
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002559{
2560 int ret;
2561
Gang Hec4c24162018-01-31 16:15:25 -08002562 if (wait)
2563 ret = ocfs2_inode_lock(inode, NULL, 0);
2564 else
2565 ret = ocfs2_try_inode_lock(inode, NULL, 0);
2566
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002567 if (ret < 0) {
Gang Hec4c24162018-01-31 16:15:25 -08002568 if (ret != -EAGAIN)
2569 mlog_errno(ret);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002570 return ret;
2571 }
2572
2573 /*
2574 * If we should update atime, we will get EX lock,
2575 * otherwise we just get PR lock.
2576 */
2577 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2578 struct buffer_head *bh = NULL;
2579
Mark Fashehe63aecb62007-10-18 15:30:42 -07002580 ocfs2_inode_unlock(inode, 0);
Gang Hec4c24162018-01-31 16:15:25 -08002581 if (wait)
2582 ret = ocfs2_inode_lock(inode, &bh, 1);
2583 else
2584 ret = ocfs2_try_inode_lock(inode, &bh, 1);
2585
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002586 if (ret < 0) {
Gang Hec4c24162018-01-31 16:15:25 -08002587 if (ret != -EAGAIN)
2588 mlog_errno(ret);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002589 return ret;
2590 }
2591 *level = 1;
2592 if (ocfs2_should_update_atime(inode, vfsmnt))
2593 ocfs2_update_inode_atime(inode, bh);
Markus Elfringa89bd892019-09-23 15:33:34 -07002594 brelse(bh);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002595 } else
2596 *level = 0;
2597
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002598 return ret;
2599}
2600
Mark Fashehe63aecb62007-10-18 15:30:42 -07002601void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002602 int ex)
2603{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002604 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002605 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002606 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002607
Mark Fashehb06970532006-03-03 10:24:33 -08002608 mlog(0, "inode %llu drop %s META lock\n",
2609 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002610 ex ? "EXMODE" : "PRMODE");
2611
piaojun1119d3c2018-04-05 16:18:33 -07002612 if (!ocfs2_is_hard_readonly(osb) &&
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002613 !ocfs2_mount_local(osb))
piaojun1119d3c2018-04-05 16:18:33 -07002614 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002615}
2616
Eric Ren439a36b2017-02-22 15:40:41 -08002617/*
2618 * This _tracker variantes are introduced to deal with the recursive cluster
2619 * locking issue. The idea is to keep track of a lock holder on the stack of
2620 * the current process. If there's a lock holder on the stack, we know the
2621 * task context is already protected by cluster locking. Currently, they're
2622 * used in some VFS entry routines.
2623 *
2624 * return < 0 on error, return == 0 if there's no lock holder on the stack
2625 * before this call, return == 1 if this call would be a recursive locking.
Larry Chen133b81f2018-06-07 17:04:43 -07002626 * return == -1 if this lock attempt will cause an upgrade which is forbidden.
2627 *
2628 * When taking lock levels into account,we face some different situations.
2629 *
2630 * 1. no lock is held
2631 * In this case, just lock the inode as requested and return 0
2632 *
2633 * 2. We are holding a lock
2634 * For this situation, things diverges into several cases
2635 *
2636 * wanted holding what to do
2637 * ex ex see 2.1 below
2638 * ex pr see 2.2 below
2639 * pr ex see 2.1 below
2640 * pr pr see 2.1 below
2641 *
2642 * 2.1 lock level that is been held is compatible
2643 * with the wanted level, so no lock action will be tacken.
2644 *
2645 * 2.2 Otherwise, an upgrade is needed, but it is forbidden.
2646 *
2647 * Reason why upgrade within a process is forbidden is that
2648 * lock upgrade may cause dead lock. The following illustrates
2649 * how it happens.
2650 *
2651 * thread on node1 thread on node2
2652 * ocfs2_inode_lock_tracker(ex=0)
2653 *
2654 * <====== ocfs2_inode_lock_tracker(ex=1)
2655 *
2656 * ocfs2_inode_lock_tracker(ex=1)
Eric Ren439a36b2017-02-22 15:40:41 -08002657 */
2658int ocfs2_inode_lock_tracker(struct inode *inode,
2659 struct buffer_head **ret_bh,
2660 int ex,
2661 struct ocfs2_lock_holder *oh)
2662{
Larry Chen133b81f2018-06-07 17:04:43 -07002663 int status = 0;
Eric Ren439a36b2017-02-22 15:40:41 -08002664 struct ocfs2_lock_res *lockres;
Larry Chen133b81f2018-06-07 17:04:43 -07002665 struct ocfs2_lock_holder *tmp_oh;
2666 struct pid *pid = task_pid(current);
2667
Eric Ren439a36b2017-02-22 15:40:41 -08002668
2669 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Larry Chen133b81f2018-06-07 17:04:43 -07002670 tmp_oh = ocfs2_pid_holder(lockres, pid);
Eric Ren439a36b2017-02-22 15:40:41 -08002671
Larry Chen133b81f2018-06-07 17:04:43 -07002672 if (!tmp_oh) {
2673 /*
2674 * This corresponds to the case 1.
2675 * We haven't got any lock before.
2676 */
2677 status = ocfs2_inode_lock_full(inode, ret_bh, ex, 0);
2678 if (status < 0) {
2679 if (status != -ENOENT)
2680 mlog_errno(status);
2681 return status;
2682 }
2683
2684 oh->oh_ex = ex;
2685 ocfs2_add_holder(lockres, oh);
2686 return 0;
2687 }
2688
2689 if (unlikely(ex && !tmp_oh->oh_ex)) {
2690 /*
2691 * case 2.2 upgrade may cause dead lock, forbid it.
2692 */
2693 mlog(ML_ERROR, "Recursive locking is not permitted to "
2694 "upgrade to EX level from PR level.\n");
2695 dump_stack();
2696 return -EINVAL;
2697 }
2698
2699 /*
2700 * case 2.1 OCFS2_META_LOCK_GETBH flag make ocfs2_inode_lock_full.
2701 * ignore the lock level and just update it.
2702 */
2703 if (ret_bh) {
2704 status = ocfs2_inode_lock_full(inode, ret_bh, ex,
2705 OCFS2_META_LOCK_GETBH);
Eric Ren439a36b2017-02-22 15:40:41 -08002706 if (status < 0) {
2707 if (status != -ENOENT)
2708 mlog_errno(status);
2709 return status;
2710 }
2711 }
Larry Chen133b81f2018-06-07 17:04:43 -07002712 return tmp_oh ? 1 : 0;
Eric Ren439a36b2017-02-22 15:40:41 -08002713}
2714
2715void ocfs2_inode_unlock_tracker(struct inode *inode,
2716 int ex,
2717 struct ocfs2_lock_holder *oh,
2718 int had_lock)
2719{
2720 struct ocfs2_lock_res *lockres;
2721
2722 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Eric Ren8818efa2017-06-23 15:08:55 -07002723 /* had_lock means that the currect process already takes the cluster
Larry Chen133b81f2018-06-07 17:04:43 -07002724 * lock previously.
2725 * If had_lock is 1, we have nothing to do here.
2726 * If had_lock is 0, we will release the lock.
Eric Ren8818efa2017-06-23 15:08:55 -07002727 */
Eric Ren439a36b2017-02-22 15:40:41 -08002728 if (!had_lock) {
Larry Chen133b81f2018-06-07 17:04:43 -07002729 ocfs2_inode_unlock(inode, oh->oh_ex);
Eric Ren439a36b2017-02-22 15:40:41 -08002730 ocfs2_remove_holder(lockres, oh);
Eric Ren439a36b2017-02-22 15:40:41 -08002731 }
2732}
2733
Sunil Mushrandf152c22009-06-22 11:40:07 -07002734int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002735{
2736 struct ocfs2_lock_res *lockres;
2737 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002738 int status = 0;
2739
Sunil Mushrandf152c22009-06-22 11:40:07 -07002740 if (ocfs2_is_hard_readonly(osb))
2741 return -EROFS;
2742
2743 if (ocfs2_mount_local(osb))
2744 return 0;
2745
Srinivas Eeda83273932009-06-03 17:02:55 -07002746 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002747 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002748 if (status < 0)
2749 return status;
2750
2751 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002752 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2753 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002754 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002755 else
2756 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2757
Srinivas Eeda83273932009-06-03 17:02:55 -07002758 return status;
2759}
2760
Sunil Mushrandf152c22009-06-22 11:40:07 -07002761void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002762{
2763 struct ocfs2_lock_res *lockres;
2764 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002765
Sunil Mushrandf152c22009-06-22 11:40:07 -07002766 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2767 lockres = &osb->osb_orphan_scan.os_lockres;
2768 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2769 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2770 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2771 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2772 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002773}
2774
Mark Fashehccd979b2005-12-15 14:31:24 -08002775int ocfs2_super_lock(struct ocfs2_super *osb,
2776 int ex)
2777{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002778 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002779 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002780 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002781
Mark Fashehccd979b2005-12-15 14:31:24 -08002782 if (ocfs2_is_hard_readonly(osb))
2783 return -EROFS;
2784
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002785 if (ocfs2_mount_local(osb))
2786 goto bail;
2787
Mark Fashehccd979b2005-12-15 14:31:24 -08002788 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2789 if (status < 0) {
2790 mlog_errno(status);
2791 goto bail;
2792 }
2793
2794 /* The super block lock path is really in the best position to
2795 * know when resources covered by the lock need to be
2796 * refreshed, so we do it here. Of course, making sense of
2797 * everything is up to the caller :) */
2798 status = ocfs2_should_refresh_lock_res(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002799 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002800 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002801
2802 ocfs2_complete_lock_res_refresh(lockres, status);
2803
Junxiao Bi3278bb72013-02-21 16:42:45 -08002804 if (status < 0) {
2805 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002806 mlog_errno(status);
Junxiao Bi3278bb72013-02-21 16:42:45 -08002807 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002808 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002809 }
2810bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002811 return status;
2812}
2813
2814void ocfs2_super_unlock(struct ocfs2_super *osb,
2815 int ex)
2816{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002817 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002818 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2819
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002820 if (!ocfs2_mount_local(osb))
2821 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002822}
2823
2824int ocfs2_rename_lock(struct ocfs2_super *osb)
2825{
2826 int status;
2827 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2828
2829 if (ocfs2_is_hard_readonly(osb))
2830 return -EROFS;
2831
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002832 if (ocfs2_mount_local(osb))
2833 return 0;
2834
Joel Beckerbd3e7612008-02-01 12:14:57 -08002835 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002836 if (status < 0)
2837 mlog_errno(status);
2838
2839 return status;
2840}
2841
2842void ocfs2_rename_unlock(struct ocfs2_super *osb)
2843{
2844 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2845
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002846 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002847 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002848}
2849
wengang wang6ca497a2009-03-06 21:29:10 +08002850int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2851{
2852 int status;
2853 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2854
2855 if (ocfs2_is_hard_readonly(osb))
2856 return -EROFS;
2857
2858 if (ocfs2_mount_local(osb))
2859 return 0;
2860
2861 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2862 0, 0);
2863 if (status < 0)
2864 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2865
2866 return status;
2867}
2868
2869void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2870{
2871 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2872
2873 if (!ocfs2_mount_local(osb))
2874 ocfs2_cluster_unlock(osb, lockres,
2875 ex ? LKM_EXMODE : LKM_PRMODE);
2876}
2877
Gang He4882abe2018-01-31 16:15:10 -08002878int ocfs2_trim_fs_lock(struct ocfs2_super *osb,
2879 struct ocfs2_trim_fs_info *info, int trylock)
2880{
2881 int status;
2882 struct ocfs2_trim_fs_lvb *lvb;
2883 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2884
2885 if (info)
2886 info->tf_valid = 0;
2887
2888 if (ocfs2_is_hard_readonly(osb))
2889 return -EROFS;
2890
2891 if (ocfs2_mount_local(osb))
2892 return 0;
2893
2894 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX,
2895 trylock ? DLM_LKF_NOQUEUE : 0, 0);
2896 if (status < 0) {
2897 if (status != -EAGAIN)
2898 mlog_errno(status);
2899 return status;
2900 }
2901
2902 if (info) {
2903 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2904 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2905 lvb->lvb_version == OCFS2_TRIMFS_LVB_VERSION) {
2906 info->tf_valid = 1;
2907 info->tf_success = lvb->lvb_success;
2908 info->tf_nodenum = be32_to_cpu(lvb->lvb_nodenum);
2909 info->tf_start = be64_to_cpu(lvb->lvb_start);
2910 info->tf_len = be64_to_cpu(lvb->lvb_len);
2911 info->tf_minlen = be64_to_cpu(lvb->lvb_minlen);
2912 info->tf_trimlen = be64_to_cpu(lvb->lvb_trimlen);
2913 }
2914 }
2915
2916 return status;
2917}
2918
2919void ocfs2_trim_fs_unlock(struct ocfs2_super *osb,
2920 struct ocfs2_trim_fs_info *info)
2921{
2922 struct ocfs2_trim_fs_lvb *lvb;
2923 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2924
2925 if (ocfs2_mount_local(osb))
2926 return;
2927
2928 if (info) {
2929 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2930 lvb->lvb_version = OCFS2_TRIMFS_LVB_VERSION;
2931 lvb->lvb_success = info->tf_success;
2932 lvb->lvb_nodenum = cpu_to_be32(info->tf_nodenum);
2933 lvb->lvb_start = cpu_to_be64(info->tf_start);
2934 lvb->lvb_len = cpu_to_be64(info->tf_len);
2935 lvb->lvb_minlen = cpu_to_be64(info->tf_minlen);
2936 lvb->lvb_trimlen = cpu_to_be64(info->tf_trimlen);
2937 }
2938
2939 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2940}
2941
Mark Fashehd680efe2006-09-08 14:14:34 -07002942int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2943{
2944 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002945 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002946 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2947 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2948
2949 BUG_ON(!dl);
2950
Tiger Yang03efed82011-05-28 00:34:19 +08002951 if (ocfs2_is_hard_readonly(osb)) {
2952 if (ex)
2953 return -EROFS;
2954 return 0;
2955 }
Mark Fashehd680efe2006-09-08 14:14:34 -07002956
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002957 if (ocfs2_mount_local(osb))
2958 return 0;
2959
Mark Fashehd680efe2006-09-08 14:14:34 -07002960 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2961 if (ret < 0)
2962 mlog_errno(ret);
2963
2964 return ret;
2965}
2966
2967void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2968{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002969 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002970 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2971 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2972
Tiger Yang03efed82011-05-28 00:34:19 +08002973 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002974 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002975}
2976
Mark Fashehccd979b2005-12-15 14:31:24 -08002977/* Reference counting of the dlm debug structure. We want this because
2978 * open references on the debug inodes can live on after a mount, so
2979 * we can't rely on the ocfs2_super to always exist. */
2980static void ocfs2_dlm_debug_free(struct kref *kref)
2981{
2982 struct ocfs2_dlm_debug *dlm_debug;
2983
2984 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2985
2986 kfree(dlm_debug);
2987}
2988
2989void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2990{
2991 if (dlm_debug)
2992 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2993}
2994
2995static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2996{
2997 kref_get(&debug->d_refcnt);
2998}
2999
3000struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
3001{
3002 struct ocfs2_dlm_debug *dlm_debug;
3003
3004 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
3005 if (!dlm_debug) {
3006 mlog_errno(-ENOMEM);
3007 goto out;
3008 }
3009
3010 kref_init(&dlm_debug->d_refcnt);
3011 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
Gang He80567732019-07-11 20:53:05 -07003012 dlm_debug->d_filter_secs = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003013out:
3014 return dlm_debug;
3015}
3016
3017/* Access to this is arbitrated for us via seq_file->sem. */
3018struct ocfs2_dlm_seq_priv {
3019 struct ocfs2_dlm_debug *p_dlm_debug;
3020 struct ocfs2_lock_res p_iter_res;
3021 struct ocfs2_lock_res p_tmp_res;
3022};
3023
3024static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
3025 struct ocfs2_dlm_seq_priv *priv)
3026{
3027 struct ocfs2_lock_res *iter, *ret = NULL;
3028 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
3029
3030 assert_spin_locked(&ocfs2_dlm_tracking_lock);
3031
3032 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
3033 /* discover the head of the list */
3034 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
3035 mlog(0, "End of list found, %p\n", ret);
3036 break;
3037 }
3038
3039 /* We track our "dummy" iteration lockres' by a NULL
3040 * l_ops field. */
3041 if (iter->l_ops != NULL) {
3042 ret = iter;
3043 break;
3044 }
3045 }
3046
3047 return ret;
3048}
3049
3050static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
3051{
3052 struct ocfs2_dlm_seq_priv *priv = m->private;
3053 struct ocfs2_lock_res *iter;
3054
3055 spin_lock(&ocfs2_dlm_tracking_lock);
3056 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
3057 if (iter) {
3058 /* Since lockres' have the lifetime of their container
3059 * (which can be inodes, ocfs2_supers, etc) we want to
3060 * copy this out to a temporary lockres while still
3061 * under the spinlock. Obviously after this we can't
3062 * trust any pointers on the copy returned, but that's
3063 * ok as the information we want isn't typically held
3064 * in them. */
3065 priv->p_tmp_res = *iter;
3066 iter = &priv->p_tmp_res;
3067 }
3068 spin_unlock(&ocfs2_dlm_tracking_lock);
3069
3070 return iter;
3071}
3072
3073static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
3074{
3075}
3076
3077static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
3078{
3079 struct ocfs2_dlm_seq_priv *priv = m->private;
3080 struct ocfs2_lock_res *iter = v;
3081 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
3082
3083 spin_lock(&ocfs2_dlm_tracking_lock);
3084 iter = ocfs2_dlm_next_res(iter, priv);
3085 list_del_init(&dummy->l_debug_list);
3086 if (iter) {
3087 list_add(&dummy->l_debug_list, &iter->l_debug_list);
3088 priv->p_tmp_res = *iter;
3089 iter = &priv->p_tmp_res;
3090 }
3091 spin_unlock(&ocfs2_dlm_tracking_lock);
3092
3093 return iter;
3094}
3095
Sunil Mushran5bc970e2010-12-28 23:26:03 -08003096/*
3097 * Version is used by debugfs.ocfs2 to determine the format being used
3098 *
3099 * New in version 2
3100 * - Lock stats printed
3101 * New in version 3
3102 * - Max time in lock stats is in usecs (instead of nsecs)
Gang He8a7f5f42019-07-11 20:53:02 -07003103 * New in version 4
Gang He5da844a2019-07-11 20:53:09 -07003104 * - Add last pr/ex unlock times and first lock wait time in usecs
Sunil Mushran5bc970e2010-12-28 23:26:03 -08003105 */
Gang He8a7f5f42019-07-11 20:53:02 -07003106#define OCFS2_DLM_DEBUG_STR_VERSION 4
Mark Fashehccd979b2005-12-15 14:31:24 -08003107static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
3108{
3109 int i;
3110 char *lvb;
3111 struct ocfs2_lock_res *lockres = v;
Gang He80567732019-07-11 20:53:05 -07003112#ifdef CONFIG_OCFS2_FS_STATS
3113 u64 now, last;
3114 struct ocfs2_dlm_debug *dlm_debug =
3115 ((struct ocfs2_dlm_seq_priv *)m->private)->p_dlm_debug;
3116#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08003117
3118 if (!lockres)
3119 return -EINVAL;
3120
Gang He80567732019-07-11 20:53:05 -07003121#ifdef CONFIG_OCFS2_FS_STATS
Gang He5da844a2019-07-11 20:53:09 -07003122 if (!lockres->l_lock_wait && dlm_debug->d_filter_secs) {
Gang He80567732019-07-11 20:53:05 -07003123 now = ktime_to_us(ktime_get_real());
3124 if (lockres->l_lock_prmode.ls_last >
3125 lockres->l_lock_exmode.ls_last)
3126 last = lockres->l_lock_prmode.ls_last;
3127 else
3128 last = lockres->l_lock_exmode.ls_last;
3129 /*
3130 * Use d_filter_secs field to filter lock resources dump,
3131 * the default d_filter_secs(0) value filters nothing,
3132 * otherwise, only dump the last N seconds active lock
3133 * resources.
3134 */
3135 if (div_u64(now - last, 1000000) > dlm_debug->d_filter_secs)
3136 return 0;
3137 }
3138#endif
3139
Mark Fashehd680efe2006-09-08 14:14:34 -07003140 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
3141
3142 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
3143 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
3144 lockres->l_name,
3145 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
3146 else
3147 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
3148
3149 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08003150 "0x%lx\t"
3151 "0x%x\t"
3152 "0x%x\t"
3153 "%u\t"
3154 "%u\t"
3155 "%d\t"
3156 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08003157 lockres->l_level,
3158 lockres->l_flags,
3159 lockres->l_action,
3160 lockres->l_unlock_action,
3161 lockres->l_ro_holders,
3162 lockres->l_ex_holders,
3163 lockres->l_requested,
3164 lockres->l_blocking);
3165
3166 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08003167 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003168 for(i = 0; i < DLM_LVB_LEN; i++)
3169 seq_printf(m, "0x%x\t", lvb[i]);
3170
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003171#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -08003172# define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
3173# define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
3174# define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
3175# define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
3176# define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
3177# define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
3178# define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
3179# define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
3180# define lock_refresh(_l) ((_l)->l_lock_refresh)
Gang He8a7f5f42019-07-11 20:53:02 -07003181# define lock_last_prmode(_l) ((_l)->l_lock_prmode.ls_last)
3182# define lock_last_exmode(_l) ((_l)->l_lock_exmode.ls_last)
Gang He5da844a2019-07-11 20:53:09 -07003183# define lock_wait(_l) ((_l)->l_lock_wait)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003184#else
Sunil Mushran5bc970e2010-12-28 23:26:03 -08003185# define lock_num_prmode(_l) (0)
3186# define lock_num_exmode(_l) (0)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003187# define lock_num_prmode_failed(_l) (0)
3188# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07003189# define lock_total_prmode(_l) (0ULL)
3190# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003191# define lock_max_prmode(_l) (0)
3192# define lock_max_exmode(_l) (0)
3193# define lock_refresh(_l) (0)
Gang He8a7f5f42019-07-11 20:53:02 -07003194# define lock_last_prmode(_l) (0ULL)
3195# define lock_last_exmode(_l) (0ULL)
Gang He5da844a2019-07-11 20:53:09 -07003196# define lock_wait(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003197#endif
3198 /* The following seq_print was added in version 2 of this output */
Sunil Mushran5bc970e2010-12-28 23:26:03 -08003199 seq_printf(m, "%u\t"
3200 "%u\t"
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003201 "%u\t"
3202 "%u\t"
3203 "%llu\t"
3204 "%llu\t"
3205 "%u\t"
3206 "%u\t"
Gang He8a7f5f42019-07-11 20:53:02 -07003207 "%u\t"
3208 "%llu\t"
Gang He5da844a2019-07-11 20:53:09 -07003209 "%llu\t"
Gang He8a7f5f42019-07-11 20:53:02 -07003210 "%llu\t",
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003211 lock_num_prmode(lockres),
3212 lock_num_exmode(lockres),
3213 lock_num_prmode_failed(lockres),
3214 lock_num_exmode_failed(lockres),
3215 lock_total_prmode(lockres),
3216 lock_total_exmode(lockres),
3217 lock_max_prmode(lockres),
3218 lock_max_exmode(lockres),
Gang He8a7f5f42019-07-11 20:53:02 -07003219 lock_refresh(lockres),
3220 lock_last_prmode(lockres),
Gang He5da844a2019-07-11 20:53:09 -07003221 lock_last_exmode(lockres),
3222 lock_wait(lockres));
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07003223
Mark Fashehccd979b2005-12-15 14:31:24 -08003224 /* End the line */
3225 seq_printf(m, "\n");
3226 return 0;
3227}
3228
Jan Engelhardt90d99772008-01-22 20:52:20 +01003229static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08003230 .start = ocfs2_dlm_seq_start,
3231 .stop = ocfs2_dlm_seq_stop,
3232 .next = ocfs2_dlm_seq_next,
3233 .show = ocfs2_dlm_seq_show,
3234};
3235
3236static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
3237{
Joe Perches33fa1d92010-07-12 13:50:19 -07003238 struct seq_file *seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08003239 struct ocfs2_dlm_seq_priv *priv = seq->private;
3240 struct ocfs2_lock_res *res = &priv->p_iter_res;
3241
3242 ocfs2_remove_lockres_tracking(res);
3243 ocfs2_put_dlm_debug(priv->p_dlm_debug);
3244 return seq_release_private(inode, file);
3245}
3246
3247static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
3248{
Mark Fashehccd979b2005-12-15 14:31:24 -08003249 struct ocfs2_dlm_seq_priv *priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08003250 struct ocfs2_super *osb;
3251
Rob Jones1848cb552014-10-09 15:25:09 -07003252 priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
Mark Fashehccd979b2005-12-15 14:31:24 -08003253 if (!priv) {
Rob Jones1848cb552014-10-09 15:25:09 -07003254 mlog_errno(-ENOMEM);
3255 return -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -08003256 }
Rob Jones1848cb552014-10-09 15:25:09 -07003257
Theodore Ts'o8e18e292006-09-27 01:50:46 -07003258 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08003259 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
3260 priv->p_dlm_debug = osb->osb_dlm_debug;
3261 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
3262
Mark Fashehccd979b2005-12-15 14:31:24 -08003263 ocfs2_add_lockres_tracking(&priv->p_iter_res,
3264 priv->p_dlm_debug);
3265
Rob Jones1848cb552014-10-09 15:25:09 -07003266 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003267}
3268
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003269static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08003270 .open = ocfs2_dlm_debug_open,
3271 .release = ocfs2_dlm_debug_release,
3272 .read = seq_read,
3273 .llseek = seq_lseek,
3274};
3275
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -07003276static void ocfs2_dlm_init_debug(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08003277{
Mark Fashehccd979b2005-12-15 14:31:24 -08003278 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3279
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -07003280 debugfs_create_file("locking_state", S_IFREG|S_IRUSR,
3281 osb->osb_debug_root, osb, &ocfs2_dlm_debug_fops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003282
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -07003283 debugfs_create_u32("locking_filter", 0600, osb->osb_debug_root,
3284 &dlm_debug->d_filter_secs);
Gang Heb73eba22020-01-04 13:00:22 -08003285 ocfs2_get_dlm_debug(dlm_debug);
Mark Fashehccd979b2005-12-15 14:31:24 -08003286}
3287
3288static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
3289{
3290 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3291
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -07003292 if (dlm_debug)
Mark Fashehccd979b2005-12-15 14:31:24 -08003293 ocfs2_put_dlm_debug(dlm_debug);
Mark Fashehccd979b2005-12-15 14:31:24 -08003294}
3295
3296int ocfs2_dlm_init(struct ocfs2_super *osb)
3297{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003298 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08003299 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003300
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003301 if (ocfs2_mount_local(osb)) {
3302 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003303 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003304 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003305
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -07003306 ocfs2_dlm_init_debug(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003307
Mark Fasheh34d024f2007-09-24 15:56:19 -07003308 /* launch downconvert thread */
Joseph Qi5afc44e2015-11-05 18:44:13 -08003309 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
3310 osb->uuid_str);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003311 if (IS_ERR(osb->dc_task)) {
3312 status = PTR_ERR(osb->dc_task);
3313 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003314 mlog_errno(status);
3315 goto bail;
3316 }
3317
Mark Fashehccd979b2005-12-15 14:31:24 -08003318 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08003319 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
Goldwyn Rodriguesc74a3bd2014-01-21 15:48:21 -08003320 osb->osb_cluster_name,
3321 strlen(osb->osb_cluster_name),
Joel Becker9c6c8772008-02-01 15:17:30 -08003322 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08003323 strlen(osb->uuid_str),
Joel Becker553b5eb2010-01-29 17:19:06 -08003324 &lproto, ocfs2_do_node_down, osb,
Joel Becker4670c462008-02-01 14:39:35 -08003325 &conn);
3326 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003327 mlog_errno(status);
3328 goto bail;
3329 }
3330
Goldwyn Rodrigues3e834152014-01-21 15:48:24 -08003331 status = ocfs2_cluster_this_node(conn, &osb->node_num);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003332 if (status < 0) {
3333 mlog_errno(status);
3334 mlog(ML_ERROR,
3335 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08003336 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003337 goto bail;
3338 }
3339
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003340local:
Mark Fashehccd979b2005-12-15 14:31:24 -08003341 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3342 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08003343 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07003344 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003345
Joel Becker4670c462008-02-01 14:39:35 -08003346 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08003347bail:
3348 if (status < 0) {
3349 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003350 if (osb->dc_task)
3351 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08003352 }
3353
Mark Fashehccd979b2005-12-15 14:31:24 -08003354 return status;
3355}
3356
Joel Becker286eaa92008-02-01 15:03:57 -08003357void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3358 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08003359{
Mark Fashehccd979b2005-12-15 14:31:24 -08003360 ocfs2_drop_osb_locks(osb);
3361
Joel Becker4670c462008-02-01 14:39:35 -08003362 /*
3363 * Now that we have dropped all locks and ocfs2_dismount_volume()
3364 * has disabled recovery, the DLM won't be talking to us. It's
3365 * safe to tear things down before disconnecting the cluster.
3366 */
3367
Mark Fasheh34d024f2007-09-24 15:56:19 -07003368 if (osb->dc_task) {
3369 kthread_stop(osb->dc_task);
3370 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003371 }
3372
3373 ocfs2_lock_res_free(&osb->osb_super_lockres);
3374 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003375 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003376 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003377
Joel Becker286eaa92008-02-01 15:03:57 -08003378 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003379 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003380
3381 ocfs2_dlm_shutdown_debug(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003382}
3383
Mark Fashehccd979b2005-12-15 14:31:24 -08003384static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003385 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003386{
Joel Becker7431cd72008-02-01 12:15:37 -08003387 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003388 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003389 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003390
3391 /* We didn't get anywhere near actually using this lockres. */
3392 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3393 goto out;
3394
Mark Fashehb80fc012006-09-12 22:08:14 -07003395 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003396 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003397
Mark Fashehccd979b2005-12-15 14:31:24 -08003398 spin_lock_irqsave(&lockres->l_lock, flags);
3399
3400 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3401 "lockres %s, flags 0x%lx\n",
3402 lockres->l_name, lockres->l_flags);
3403
3404 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3405 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3406 "%u, unlock_action = %u\n",
3407 lockres->l_name, lockres->l_flags, lockres->l_action,
3408 lockres->l_unlock_action);
3409
3410 spin_unlock_irqrestore(&lockres->l_lock, flags);
3411
3412 /* XXX: Today we just wait on any busy
3413 * locks... Perhaps we need to cancel converts in the
3414 * future? */
3415 ocfs2_wait_on_busy_lock(lockres);
3416
3417 spin_lock_irqsave(&lockres->l_lock, flags);
3418 }
3419
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003420 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3421 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003422 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003423 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3424 lockres->l_ops->set_lvb(lockres);
3425 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003426
3427 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3428 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3429 lockres->l_name);
3430 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3431 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3432
3433 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3434 spin_unlock_irqrestore(&lockres->l_lock, flags);
3435 goto out;
3436 }
3437
3438 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3439
3440 /* make sure we never get here while waiting for an ast to
3441 * fire. */
3442 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3443
3444 /* is this necessary? */
3445 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3446 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3447 spin_unlock_irqrestore(&lockres->l_lock, flags);
3448
3449 mlog(0, "lock %s\n", lockres->l_name);
3450
Joel Beckera796d282010-01-28 19:22:39 -08003451 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
Joel Becker7431cd72008-02-01 12:15:37 -08003452 if (ret) {
3453 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003454 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003455 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003456 BUG();
3457 }
Coly Li73ac36e2009-01-07 18:09:16 -08003458 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003459 lockres->l_name);
3460
3461 ocfs2_wait_on_busy_lock(lockres);
3462out:
Mark Fashehccd979b2005-12-15 14:31:24 -08003463 return 0;
3464}
3465
Jan Kara84d86f82014-04-03 14:46:57 -07003466static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3467 struct ocfs2_lock_res *lockres);
3468
Mark Fashehccd979b2005-12-15 14:31:24 -08003469/* Mark the lockres as being dropped. It will no longer be
3470 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003471 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003472 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003473 *
3474 * You can *not* attempt to call cluster_lock on this lockres anymore. */
Jan Kara84d86f82014-04-03 14:46:57 -07003475void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
3476 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003477{
3478 int status;
3479 struct ocfs2_mask_waiter mw;
Jan Kara84d86f82014-04-03 14:46:57 -07003480 unsigned long flags, flags2;
Mark Fashehccd979b2005-12-15 14:31:24 -08003481
3482 ocfs2_init_mask_waiter(&mw);
3483
3484 spin_lock_irqsave(&lockres->l_lock, flags);
3485 lockres->l_flags |= OCFS2_LOCK_FREEING;
Jan Kara84d86f82014-04-03 14:46:57 -07003486 if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
3487 /*
3488 * We know the downconvert is queued but not in progress
3489 * because we are the downconvert thread and processing
3490 * different lock. So we can just remove the lock from the
3491 * queue. This is not only an optimization but also a way
3492 * to avoid the following deadlock:
3493 * ocfs2_dentry_post_unlock()
3494 * ocfs2_dentry_lock_put()
3495 * ocfs2_drop_dentry_lock()
3496 * iput()
3497 * ocfs2_evict_inode()
3498 * ocfs2_clear_inode()
3499 * ocfs2_mark_lockres_freeing()
3500 * ... blocks waiting for OCFS2_LOCK_QUEUED
3501 * since we are the downconvert thread which
3502 * should clear the flag.
3503 */
3504 spin_unlock_irqrestore(&lockres->l_lock, flags);
3505 spin_lock_irqsave(&osb->dc_task_lock, flags2);
3506 list_del_init(&lockres->l_blocked_list);
3507 osb->blocked_lock_count--;
3508 spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
3509 /*
3510 * Warn if we recurse into another post_unlock call. Strictly
3511 * speaking it isn't a problem but we need to be careful if
3512 * that happens (stack overflow, deadlocks, ...) so warn if
3513 * ocfs2 grows a path for which this can happen.
3514 */
3515 WARN_ON_ONCE(lockres->l_ops->post_unlock);
3516 /* Since the lock is freeing we don't do much in the fn below */
3517 ocfs2_process_blocked_lock(osb, lockres);
3518 return;
3519 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003520 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3521 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3522 spin_unlock_irqrestore(&lockres->l_lock, flags);
3523
3524 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3525
3526 status = ocfs2_wait_for_mask(&mw);
3527 if (status)
3528 mlog_errno(status);
3529
3530 spin_lock_irqsave(&lockres->l_lock, flags);
3531 }
3532 spin_unlock_irqrestore(&lockres->l_lock, flags);
3533}
3534
Mark Fashehd680efe2006-09-08 14:14:34 -07003535void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3536 struct ocfs2_lock_res *lockres)
3537{
3538 int ret;
3539
Jan Kara84d86f82014-04-03 14:46:57 -07003540 ocfs2_mark_lockres_freeing(osb, lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003541 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003542 if (ret)
3543 mlog_errno(ret);
3544}
3545
Mark Fashehccd979b2005-12-15 14:31:24 -08003546static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3547{
Mark Fashehd680efe2006-09-08 14:14:34 -07003548 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3549 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003550 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003551 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003552}
3553
Mark Fashehccd979b2005-12-15 14:31:24 -08003554int ocfs2_drop_inode_locks(struct inode *inode)
3555{
3556 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003557
Mark Fashehccd979b2005-12-15 14:31:24 -08003558 /* No need to call ocfs2_mark_lockres_freeing here -
3559 * ocfs2_clear_inode has done it for us. */
3560
3561 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003562 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003563 if (err < 0)
3564 mlog_errno(err);
3565
3566 status = err;
3567
3568 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003569 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003570 if (err < 0)
3571 mlog_errno(err);
3572 if (err < 0 && !status)
3573 status = err;
3574
3575 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003576 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003577 if (err < 0)
3578 mlog_errno(err);
3579 if (err < 0 && !status)
3580 status = err;
3581
Mark Fashehccd979b2005-12-15 14:31:24 -08003582 return status;
3583}
3584
Joel Beckerde551242008-02-01 14:45:08 -08003585static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3586 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003587{
3588 assert_spin_locked(&lockres->l_lock);
3589
Joel Beckerbd3e7612008-02-01 12:14:57 -08003590 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003591
3592 if (lockres->l_level <= new_level) {
Sunil Mushran9b915182010-02-26 19:42:44 -08003593 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3594 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3595 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3596 new_level, list_empty(&lockres->l_blocked_list),
3597 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3598 lockres->l_flags, lockres->l_ro_holders,
3599 lockres->l_ex_holders, lockres->l_action,
3600 lockres->l_unlock_action, lockres->l_requested,
3601 lockres->l_blocking, lockres->l_pending_gen);
Mark Fashehccd979b2005-12-15 14:31:24 -08003602 BUG();
3603 }
3604
Sunil Mushran9b915182010-02-26 19:42:44 -08003605 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3606 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
Mark Fashehccd979b2005-12-15 14:31:24 -08003607
3608 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3609 lockres->l_requested = new_level;
3610 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003611 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003612}
3613
3614static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3615 struct ocfs2_lock_res *lockres,
3616 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003617 int lvb,
3618 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003619{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003620 int ret;
3621 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003622
Sunil Mushran9b915182010-02-26 19:42:44 -08003623 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3624 lockres->l_level, new_level);
3625
Eric Rene7ee2c082017-01-10 16:57:33 -08003626 /*
3627 * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
3628 * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
3629 * we can recover correctly from node failure. Otherwise, we may get
Ingo Molnar793057e2018-02-28 09:39:48 +01003630 * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
Eric Rene7ee2c082017-01-10 16:57:33 -08003631 */
Gang Hea6346442018-11-02 15:48:03 -07003632 if (ocfs2_userspace_stack(osb) &&
Eric Rene7ee2c082017-01-10 16:57:33 -08003633 lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3634 lvb = 1;
3635
Mark Fashehccd979b2005-12-15 14:31:24 -08003636 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003637 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003638
Joel Becker4670c462008-02-01 14:39:35 -08003639 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003640 new_level,
3641 &lockres->l_lksb,
3642 dlm_flags,
3643 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08003644 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08003645 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003646 if (ret) {
3647 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003648 ocfs2_recover_from_dlm_error(lockres, 1);
3649 goto bail;
3650 }
3651
3652 ret = 0;
3653bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08003654 return ret;
3655}
3656
Joel Becker24ef1812008-01-29 17:37:32 -08003657/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003658static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3659 struct ocfs2_lock_res *lockres)
3660{
3661 assert_spin_locked(&lockres->l_lock);
3662
Mark Fashehccd979b2005-12-15 14:31:24 -08003663 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3664 /* If we're already trying to cancel a lock conversion
3665 * then just drop the spinlock and allow the caller to
3666 * requeue this lock. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003667 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003668 return 0;
3669 }
3670
3671 /* were we in a convert when we got the bast fire? */
3672 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3673 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3674 /* set things up for the unlockast to know to just
3675 * clear out the ast_action and unset busy, etc. */
3676 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3677
3678 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3679 "lock %s, invalid flags: 0x%lx\n",
3680 lockres->l_name, lockres->l_flags);
3681
Sunil Mushran9b915182010-02-26 19:42:44 -08003682 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3683
Mark Fashehccd979b2005-12-15 14:31:24 -08003684 return 1;
3685}
3686
3687static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3688 struct ocfs2_lock_res *lockres)
3689{
3690 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003691
Joel Becker4670c462008-02-01 14:39:35 -08003692 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Beckera796d282010-01-28 19:22:39 -08003693 DLM_LKF_CANCEL);
Joel Becker7431cd72008-02-01 12:15:37 -08003694 if (ret) {
3695 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003696 ocfs2_recover_from_dlm_error(lockres, 0);
3697 }
3698
Sunil Mushran9b915182010-02-26 19:42:44 -08003699 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003700
Mark Fashehccd979b2005-12-15 14:31:24 -08003701 return ret;
3702}
3703
Mark Fashehb5e500e2006-09-13 22:01:16 -07003704static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3705 struct ocfs2_lock_res *lockres,
3706 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003707{
3708 unsigned long flags;
3709 int blocking;
3710 int new_level;
Sunil Mushran079b8052010-02-03 10:16:54 -08003711 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003712 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003713 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003714 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003715
Mark Fashehccd979b2005-12-15 14:31:24 -08003716 spin_lock_irqsave(&lockres->l_lock, flags);
3717
Mark Fashehccd979b2005-12-15 14:31:24 -08003718recheck:
Sunil Mushrandb0f6ce2010-02-01 16:55:50 -08003719 /*
3720 * Is it still blocking? If not, we have no more work to do.
3721 */
3722 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3723 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3724 spin_unlock_irqrestore(&lockres->l_lock, flags);
3725 ret = 0;
3726 goto leave;
3727 }
3728
Mark Fashehccd979b2005-12-15 14:31:24 -08003729 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003730 /* XXX
3731 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3732 * exists entirely for one reason - another thread has set
3733 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3734 *
3735 * If we do ocfs2_cancel_convert() before the other thread
3736 * calls dlm_lock(), our cancel will do nothing. We will
3737 * get no ast, and we will have no way of knowing the
3738 * cancel failed. Meanwhile, the other thread will call
3739 * into dlm_lock() and wait...forever.
3740 *
3741 * Why forever? Because another node has asked for the
3742 * lock first; that's why we're here in unblock_lock().
3743 *
3744 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3745 * set, we just requeue the unblock. Only when the other
3746 * thread has called dlm_lock() and cleared PENDING will
3747 * we then cancel their request.
3748 *
3749 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3750 * at the same time they set OCFS2_DLM_BUSY. They must
3751 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3752 */
Sunil Mushran9b915182010-02-26 19:42:44 -08003753 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3754 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3755 lockres->l_name);
Joel Beckerde551242008-02-01 14:45:08 -08003756 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003757 }
Joel Beckerde551242008-02-01 14:45:08 -08003758
Mark Fashehd680efe2006-09-08 14:14:34 -07003759 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003760 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3761 spin_unlock_irqrestore(&lockres->l_lock, flags);
3762 if (ret) {
3763 ret = ocfs2_cancel_convert(osb, lockres);
3764 if (ret < 0)
3765 mlog_errno(ret);
3766 }
3767 goto leave;
3768 }
3769
Sunil Mushrana1912822010-01-21 10:50:03 -08003770 /*
3771 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3772 * set when the ast is received for an upconvert just before the
3773 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3774 * on the heels of the ast, we want to delay the downconvert just
3775 * enough to allow the up requestor to do its task. Because this
3776 * lock is in the blocked queue, the lock will be downconverted
3777 * as soon as the requestor is done with the lock.
3778 */
3779 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3780 goto leave_requeue;
3781
Sunil Mushran0d741252010-01-29 09:44:11 -08003782 /*
3783 * How can we block and yet be at NL? We were trying to upconvert
3784 * from NL and got canceled. The code comes back here, and now
3785 * we notice and clear BLOCKING.
3786 */
3787 if (lockres->l_level == DLM_LOCK_NL) {
3788 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
Sunil Mushran9b915182010-02-26 19:42:44 -08003789 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
Sunil Mushran0d741252010-01-29 09:44:11 -08003790 lockres->l_blocking = DLM_LOCK_NL;
3791 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3792 spin_unlock_irqrestore(&lockres->l_lock, flags);
3793 goto leave;
3794 }
3795
Mark Fashehccd979b2005-12-15 14:31:24 -08003796 /* if we're blocking an exclusive and we have *any* holders,
3797 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003798 if ((lockres->l_blocking == DLM_LOCK_EX)
Sunil Mushran9b915182010-02-26 19:42:44 -08003799 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3800 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3801 lockres->l_name, lockres->l_ex_holders,
3802 lockres->l_ro_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003803 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003804 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003805
3806 /* If it's a PR we're blocking, then only
3807 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003808 if (lockres->l_blocking == DLM_LOCK_PR &&
Sunil Mushran9b915182010-02-26 19:42:44 -08003809 lockres->l_ex_holders) {
3810 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3811 lockres->l_name, lockres->l_ex_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003812 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003813 }
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003814
3815 /*
3816 * Can we get a lock in this state if the holder counts are
3817 * zero? The meta data unblock code used to check this.
3818 */
3819 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Sunil Mushran9b915182010-02-26 19:42:44 -08003820 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3821 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3822 lockres->l_name);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003823 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003824 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003825
Mark Fasheh16d5b952006-09-13 21:10:12 -07003826 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3827
3828 if (lockres->l_ops->check_downconvert
Sunil Mushran9b915182010-02-26 19:42:44 -08003829 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3830 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3831 lockres->l_name);
Mark Fasheh16d5b952006-09-13 21:10:12 -07003832 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003833 }
Mark Fasheh16d5b952006-09-13 21:10:12 -07003834
Mark Fashehccd979b2005-12-15 14:31:24 -08003835 /* If we get here, then we know that there are no more
3836 * incompatible holders (and anyone asking for an incompatible
3837 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003838 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003839 goto downconvert;
3840
3841 /* Some lockres types want to do a bit of work before
3842 * downconverting a lock. Allow that here. The worker function
3843 * may sleep, so we save off a copy of what we're blocking as
3844 * it may change while we're not holding the spin lock. */
3845 blocking = lockres->l_blocking;
Sunil Mushran079b8052010-02-03 10:16:54 -08003846 level = lockres->l_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003847 spin_unlock_irqrestore(&lockres->l_lock, flags);
3848
Mark Fashehcc567d82006-09-13 21:52:21 -07003849 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003850
Sunil Mushran9b915182010-02-26 19:42:44 -08003851 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3852 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3853 lockres->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -07003854 goto leave;
Sunil Mushran9b915182010-02-26 19:42:44 -08003855 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003856
3857 spin_lock_irqsave(&lockres->l_lock, flags);
Sunil Mushran079b8052010-02-03 10:16:54 -08003858 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003859 /* If this changed underneath us, then we can't drop
3860 * it just yet. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003861 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3862 "Recheck\n", lockres->l_name, blocking,
3863 lockres->l_blocking, level, lockres->l_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003864 goto recheck;
3865 }
3866
3867downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003868 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003869
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003870 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003871 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003872 set_lvb = 1;
3873
3874 /*
3875 * We only set the lvb if the lock has been fully
3876 * refreshed - otherwise we risk setting stale
3877 * data. Otherwise, there's no need to actually clear
3878 * out the lvb here as it's value is still valid.
3879 */
3880 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3881 lockres->l_ops->set_lvb(lockres);
3882 }
3883
Joel Beckerde551242008-02-01 14:45:08 -08003884 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003885 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003886 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3887 gen);
3888
Mark Fashehccd979b2005-12-15 14:31:24 -08003889leave:
Tao Mac1e8d352011-03-07 16:43:21 +08003890 if (ret)
3891 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08003892 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003893
3894leave_requeue:
3895 spin_unlock_irqrestore(&lockres->l_lock, flags);
3896 ctl->requeue = 1;
3897
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003898 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003899}
3900
Mark Fashehd680efe2006-09-08 14:14:34 -07003901static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3902 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003903{
3904 struct inode *inode;
3905 struct address_space *mapping;
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003906 struct ocfs2_inode_info *oi;
Mark Fashehccd979b2005-12-15 14:31:24 -08003907
Mark Fashehccd979b2005-12-15 14:31:24 -08003908 inode = ocfs2_lock_res_inode(lockres);
3909 mapping = inode->i_mapping;
3910
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003911 if (S_ISDIR(inode->i_mode)) {
3912 oi = OCFS2_I(inode);
3913 oi->ip_dir_lock_gen++;
3914 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3915 goto out;
3916 }
3917
Mark Fasheh1044e402008-02-28 17:16:03 -08003918 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003919 goto out;
3920
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003921 /*
3922 * We need this before the filemap_fdatawrite() so that it can
3923 * transfer the dirty bit from the PTE to the
3924 * page. Unfortunately this means that even for EX->PR
3925 * downconverts, we'll lose our mappings and have to build
3926 * them up again.
3927 */
3928 unmap_mapping_range(mapping, 0, 0, 0);
3929
Mark Fashehccd979b2005-12-15 14:31:24 -08003930 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003931 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3932 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003933 }
3934 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003935 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003936 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003937 } else {
3938 /* We only need to wait on the I/O if we're not also
3939 * truncating pages because truncate_inode_pages waits
3940 * for us above. We don't truncate pages if we're
3941 * blocking anything < EXMODE because we want to keep
3942 * them around in that case. */
3943 filemap_fdatawait(mapping);
3944 }
3945
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +01003946 forget_all_cached_acls(inode);
3947
Mark Fashehf1f54062007-10-18 15:13:59 -07003948out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003949 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003950}
3951
Tao Maa4338482009-08-18 11:19:29 +08003952static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3953 struct ocfs2_lock_res *lockres,
3954 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003955{
Tao Maa4338482009-08-18 11:19:29 +08003956 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003957
Joel Beckerbd3e7612008-02-01 12:14:57 -08003958 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3959 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003960
3961 if (checkpointed)
3962 return 1;
3963
Tao Maa4338482009-08-18 11:19:29 +08003964 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003965 return 0;
3966}
3967
Tao Maa4338482009-08-18 11:19:29 +08003968static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3969 int new_level)
3970{
3971 struct inode *inode = ocfs2_lock_res_inode(lockres);
3972
3973 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3974}
3975
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003976static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3977{
3978 struct inode *inode = ocfs2_lock_res_inode(lockres);
3979
3980 __ocfs2_stuff_meta_lvb(inode);
3981}
3982
Mark Fashehd680efe2006-09-08 14:14:34 -07003983/*
3984 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003985 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003986 * dlmglue API and push these off to the ocfs2_wq in the future.
3987 */
3988static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3989 struct ocfs2_lock_res *lockres)
3990{
3991 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3992 ocfs2_dentry_lock_put(osb, dl);
3993}
3994
3995/*
3996 * d_delete() matching dentries before the lock downconvert.
3997 *
3998 * At this point, any process waiting to destroy the
3999 * dentry_lock due to last ref count is stopped by the
4000 * OCFS2_LOCK_QUEUED flag.
4001 *
4002 * We have two potential problems
4003 *
4004 * 1) If we do the last reference drop on our dentry_lock (via dput)
4005 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
4006 * the downconvert to finish. Instead we take an elevated
4007 * reference and push the drop until after we've completed our
4008 * unblock processing.
4009 *
4010 * 2) There might be another process with a final reference,
4011 * waiting on us to finish processing. If this is the case, we
4012 * detect it and exit out - there's no more dentries anyway.
4013 */
4014static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
4015 int blocking)
4016{
4017 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
4018 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
4019 struct dentry *dentry;
4020 unsigned long flags;
4021 int extra_ref = 0;
4022
4023 /*
4024 * This node is blocking another node from getting a read
4025 * lock. This happens when we've renamed within a
4026 * directory. We've forced the other nodes to d_delete(), but
4027 * we never actually dropped our lock because it's still
4028 * valid. The downconvert code will retain a PR for this node,
4029 * so there's no further work to do.
4030 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08004031 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07004032 return UNBLOCK_CONTINUE;
4033
4034 /*
4035 * Mark this inode as potentially orphaned. The code in
4036 * ocfs2_delete_inode() will figure out whether it actually
4037 * needs to be freed or not.
4038 */
4039 spin_lock(&oi->ip_lock);
4040 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
4041 spin_unlock(&oi->ip_lock);
4042
4043 /*
4044 * Yuck. We need to make sure however that the check of
4045 * OCFS2_LOCK_FREEING and the extra reference are atomic with
4046 * respect to a reference decrement or the setting of that
4047 * flag.
4048 */
4049 spin_lock_irqsave(&lockres->l_lock, flags);
4050 spin_lock(&dentry_attach_lock);
4051 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
4052 && dl->dl_count) {
4053 dl->dl_count++;
4054 extra_ref = 1;
4055 }
4056 spin_unlock(&dentry_attach_lock);
4057 spin_unlock_irqrestore(&lockres->l_lock, flags);
4058
4059 mlog(0, "extra_ref = %d\n", extra_ref);
4060
4061 /*
4062 * We have a process waiting on us in ocfs2_dentry_iput(),
4063 * which means we can't have any more outstanding
4064 * aliases. There's no need to do any more work.
4065 */
4066 if (!extra_ref)
4067 return UNBLOCK_CONTINUE;
4068
4069 spin_lock(&dentry_attach_lock);
4070 while (1) {
4071 dentry = ocfs2_find_local_alias(dl->dl_inode,
4072 dl->dl_parent_blkno, 1);
4073 if (!dentry)
4074 break;
4075 spin_unlock(&dentry_attach_lock);
4076
alex chen10ab8812015-02-10 14:09:23 -08004077 if (S_ISDIR(dl->dl_inode->i_mode))
4078 shrink_dcache_parent(dentry);
4079
Al Viroa4555892014-10-21 20:11:25 -04004080 mlog(0, "d_delete(%pd);\n", dentry);
Mark Fashehd680efe2006-09-08 14:14:34 -07004081
4082 /*
4083 * The following dcache calls may do an
4084 * iput(). Normally we don't want that from the
4085 * downconverting thread, but in this case it's ok
4086 * because the requesting node already has an
4087 * exclusive lock on the inode, so it can't be queued
4088 * for a downconvert.
4089 */
4090 d_delete(dentry);
4091 dput(dentry);
4092
4093 spin_lock(&dentry_attach_lock);
4094 }
4095 spin_unlock(&dentry_attach_lock);
4096
4097 /*
4098 * If we are the last holder of this dentry lock, there is no
4099 * reason to downconvert so skip straight to the unlock.
4100 */
4101 if (dl->dl_count == 1)
4102 return UNBLOCK_STOP_POST;
4103
4104 return UNBLOCK_CONTINUE_POST;
4105}
4106
Tao Ma8dec98e2009-08-18 11:19:58 +08004107static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
4108 int new_level)
4109{
4110 struct ocfs2_refcount_tree *tree =
4111 ocfs2_lock_res_refcount_tree(lockres);
4112
4113 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
4114}
4115
4116static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
4117 int blocking)
4118{
4119 struct ocfs2_refcount_tree *tree =
4120 ocfs2_lock_res_refcount_tree(lockres);
4121
4122 ocfs2_metadata_cache_purge(&tree->rf_ci);
4123
4124 return UNBLOCK_CONTINUE;
4125}
4126
Jan Kara9e33d692008-08-25 19:56:50 +02004127static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
4128{
4129 struct ocfs2_qinfo_lvb *lvb;
4130 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
4131 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4132 oinfo->dqi_gi.dqi_type);
4133
Mark Fasheha641dc22008-12-24 16:03:48 -08004134 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02004135 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
4136 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
4137 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
4138 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
4139 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
4140 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
4141 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
Jan Kara9e33d692008-08-25 19:56:50 +02004142}
4143
4144void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4145{
4146 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4147 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4148 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4149
Jan Kara9e33d692008-08-25 19:56:50 +02004150 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
4151 ocfs2_cluster_unlock(osb, lockres, level);
Jan Kara9e33d692008-08-25 19:56:50 +02004152}
4153
4154static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
4155{
4156 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4157 oinfo->dqi_gi.dqi_type);
4158 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4159 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01004160 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02004161 struct ocfs2_global_disk_dqinfo *gdinfo;
4162 int status = 0;
4163
Joel Becker1c520df2009-06-19 15:14:13 -07004164 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
4165 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02004166 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
4167 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
4168 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
4169 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
4170 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
4171 oinfo->dqi_gi.dqi_free_entry =
4172 be32_to_cpu(lvb->lvb_free_entry);
4173 } else {
Jan Karaae4f6ef2010-04-28 19:04:29 +02004174 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
4175 oinfo->dqi_giblk, &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +01004176 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02004177 mlog_errno(status);
4178 goto bail;
4179 }
4180 gdinfo = (struct ocfs2_global_disk_dqinfo *)
4181 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
4182 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
4183 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
4184 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
4185 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
4186 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
4187 oinfo->dqi_gi.dqi_free_entry =
4188 le32_to_cpu(gdinfo->dqi_free_entry);
4189 brelse(bh);
4190 ocfs2_track_lock_refresh(lockres);
4191 }
4192
4193bail:
4194 return status;
4195}
4196
4197/* Lock quota info, this function expects at least shared lock on the quota file
4198 * so that we can safely refresh quota info from disk. */
4199int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4200{
4201 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4202 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4203 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4204 int status = 0;
4205
Jan Kara9e33d692008-08-25 19:56:50 +02004206 /* On RO devices, locking really isn't needed... */
4207 if (ocfs2_is_hard_readonly(osb)) {
4208 if (ex)
4209 status = -EROFS;
4210 goto bail;
4211 }
4212 if (ocfs2_mount_local(osb))
4213 goto bail;
4214
4215 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4216 if (status < 0) {
4217 mlog_errno(status);
4218 goto bail;
4219 }
4220 if (!ocfs2_should_refresh_lock_res(lockres))
4221 goto bail;
4222 /* OK, we have the lock but we need to refresh the quota info */
4223 status = ocfs2_refresh_qinfo(oinfo);
4224 if (status)
4225 ocfs2_qinfo_unlock(oinfo, ex);
4226 ocfs2_complete_lock_res_refresh(lockres, status);
4227bail:
Jan Kara9e33d692008-08-25 19:56:50 +02004228 return status;
4229}
4230
Tao Ma8dec98e2009-08-18 11:19:58 +08004231int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
4232{
4233 int status;
4234 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4235 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4236 struct ocfs2_super *osb = lockres->l_priv;
4237
4238
4239 if (ocfs2_is_hard_readonly(osb))
4240 return -EROFS;
4241
4242 if (ocfs2_mount_local(osb))
4243 return 0;
4244
4245 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4246 if (status < 0)
4247 mlog_errno(status);
4248
4249 return status;
4250}
4251
4252void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
4253{
4254 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4255 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4256 struct ocfs2_super *osb = lockres->l_priv;
4257
4258 if (!ocfs2_mount_local(osb))
4259 ocfs2_cluster_unlock(osb, lockres, level);
4260}
4261
Adrian Bunk00600052008-01-29 00:11:41 +02004262static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
4263 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08004264{
4265 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07004266 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08004267 unsigned long flags;
4268
4269 /* Our reference to the lockres in this function can be
4270 * considered valid until we remove the OCFS2_LOCK_QUEUED
4271 * flag. */
4272
Mark Fashehccd979b2005-12-15 14:31:24 -08004273 BUG_ON(!lockres);
4274 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08004275
Sunil Mushran9b915182010-02-26 19:42:44 -08004276 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08004277
4278 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07004279 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08004280 * still be marked with OCFS2_LOCK_FREEING after this check,
4281 * but short circuiting here will still save us some
4282 * performance. */
4283 spin_lock_irqsave(&lockres->l_lock, flags);
4284 if (lockres->l_flags & OCFS2_LOCK_FREEING)
4285 goto unqueue;
4286 spin_unlock_irqrestore(&lockres->l_lock, flags);
4287
Mark Fashehb5e500e2006-09-13 22:01:16 -07004288 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08004289 if (status < 0)
4290 mlog_errno(status);
4291
4292 spin_lock_irqsave(&lockres->l_lock, flags);
4293unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07004294 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004295 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
4296 } else
4297 ocfs2_schedule_blocked_lock(osb, lockres);
4298
Sunil Mushran9b915182010-02-26 19:42:44 -08004299 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07004300 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08004301 spin_unlock_irqrestore(&lockres->l_lock, flags);
4302
Mark Fashehd680efe2006-09-08 14:14:34 -07004303 if (ctl.unblock_action != UNBLOCK_CONTINUE
4304 && lockres->l_ops->post_unlock)
4305 lockres->l_ops->post_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08004306}
4307
4308static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4309 struct ocfs2_lock_res *lockres)
4310{
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004311 unsigned long flags;
4312
Mark Fashehccd979b2005-12-15 14:31:24 -08004313 assert_spin_locked(&lockres->l_lock);
4314
4315 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4316 /* Do not schedule a lock for downconvert when it's on
4317 * the way to destruction - any nodes wanting access
4318 * to the resource will get it soon. */
Sunil Mushran9b915182010-02-26 19:42:44 -08004319 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08004320 lockres->l_name, lockres->l_flags);
4321 return;
4322 }
4323
4324 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4325
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004326 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08004327 if (list_empty(&lockres->l_blocked_list)) {
4328 list_add_tail(&lockres->l_blocked_list,
4329 &osb->blocked_lock_list);
4330 osb->blocked_lock_count++;
4331 }
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004332 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08004333}
Mark Fasheh34d024f2007-09-24 15:56:19 -07004334
4335static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4336{
4337 unsigned long processed;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004338 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004339 struct ocfs2_lock_res *lockres;
4340
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004341 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004342 /* grab this early so we know to try again if a state change and
4343 * wake happens part-way through our work */
4344 osb->dc_work_sequence = osb->dc_wake_sequence;
4345
4346 processed = osb->blocked_lock_count;
Joseph Qi209f7512015-08-06 15:46:23 -07004347 /*
4348 * blocked lock processing in this loop might call iput which can
4349 * remove items off osb->blocked_lock_list. Downconvert up to
4350 * 'processed' number of locks, but stop short if we had some
4351 * removed in ocfs2_mark_lockres_freeing when downconverting.
4352 */
4353 while (processed && !list_empty(&osb->blocked_lock_list)) {
Mark Fasheh34d024f2007-09-24 15:56:19 -07004354 lockres = list_entry(osb->blocked_lock_list.next,
4355 struct ocfs2_lock_res, l_blocked_list);
4356 list_del_init(&lockres->l_blocked_list);
4357 osb->blocked_lock_count--;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004358 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004359
4360 BUG_ON(!processed);
4361 processed--;
4362
4363 ocfs2_process_blocked_lock(osb, lockres);
4364
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004365 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004366 }
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004367 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004368}
4369
4370static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4371{
4372 int empty = 0;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004373 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004374
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004375 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004376 if (list_empty(&osb->blocked_lock_list))
4377 empty = 1;
4378
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004379 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004380 return empty;
4381}
4382
4383static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4384{
4385 int should_wake = 0;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004386 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004387
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004388 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004389 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4390 should_wake = 1;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004391 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004392
4393 return should_wake;
4394}
4395
Adrian Bunk200bfae2008-02-17 10:20:38 +02004396static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004397{
Mark Fasheh34d024f2007-09-24 15:56:19 -07004398 struct ocfs2_super *osb = arg;
4399
4400 /* only quit once we've been asked to stop and there is no more
4401 * work available */
4402 while (!(kthread_should_stop() &&
4403 ocfs2_downconvert_thread_lists_empty(osb))) {
4404
4405 wait_event_interruptible(osb->dc_event,
4406 ocfs2_downconvert_thread_should_wake(osb) ||
4407 kthread_should_stop());
4408
4409 mlog(0, "downconvert_thread: awoken\n");
4410
4411 ocfs2_downconvert_thread_do_work(osb);
4412 }
4413
4414 osb->dc_task = NULL;
Hariprasad Kelam4658d872019-07-11 20:53:16 -07004415 return 0;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004416}
4417
4418void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4419{
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004420 unsigned long flags;
4421
4422 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004423 /* make sure the voting thread gets a swipe at whatever changes
4424 * the caller may have made to the voting state */
4425 osb->dc_wake_sequence++;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004426 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004427 wake_up(&osb->dc_event);
4428}