blob: 5729d55da67d037659235092be5e882233333160 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmglue.c
5 *
6 * Code which implements an OCFS2 specific interface to our DLM.
7 *
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/types.h>
27#include <linux/slab.h>
28#include <linux/highmem.h>
29#include <linux/mm.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080030#include <linux/kthread.h>
31#include <linux/pagemap.h>
32#include <linux/debugfs.h>
33#include <linux/seq_file.h>
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070034#include <linux/time.h>
Jan Kara9e33d692008-08-25 19:56:50 +020035#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080036
Mark Fashehccd979b2005-12-15 14:31:24 -080037#define MLOG_MASK_PREFIX ML_DLM_GLUE
38#include <cluster/masklog.h>
39
40#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080041#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080042
43#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070044#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045#include "dlmglue.h"
46#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080047#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080048#include "heartbeat.h"
49#include "inode.h"
50#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080051#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#include "slot_map.h"
53#include "super.h"
54#include "uptodate.h"
Jan Kara9e33d692008-08-25 19:56:50 +020055#include "quota.h"
Tao Ma8dec98e2009-08-18 11:19:58 +080056#include "refcounttree.h"
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010057#include "acl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080058
59#include "buffer_head_io.h"
60
61struct ocfs2_mask_waiter {
62 struct list_head mw_item;
63 int mw_status;
64 struct completion mw_complete;
65 unsigned long mw_mask;
66 unsigned long mw_goal;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070067#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -080068 ktime_t mw_lock_start;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070069#endif
Mark Fashehccd979b2005-12-15 14:31:24 -080070};
71
Mark Fasheh54a7e752006-09-12 21:49:13 -070072static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
73static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080074static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Jan Kara9e33d692008-08-25 19:56:50 +020075static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080076
Mark Fashehd680efe2006-09-08 14:14:34 -070077/*
Mark Fashehcc567d82006-09-13 21:52:21 -070078 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070079 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070080 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070081 * and ocfs2_process_blocked_lock()
82 *
83 */
84enum ocfs2_unblock_action {
85 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
86 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
87 * ->post_unlock callback */
88 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
89 * ->post_unlock() callback. */
90};
91
92struct ocfs2_unblock_ctl {
93 int requeue;
94 enum ocfs2_unblock_action unblock_action;
95};
96
Jan Karacb257972009-06-04 15:26:50 +020097/* Lockdep class keys */
98struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
99
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700100static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
101 int new_level);
102static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
103
Mark Fashehcc567d82006-09-13 21:52:21 -0700104static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
105 int blocking);
106
Mark Fashehcc567d82006-09-13 21:52:21 -0700107static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
108 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700109
110static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
111 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800112
Jan Kara9e33d692008-08-25 19:56:50 +0200113static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700114
Tao Ma8dec98e2009-08-18 11:19:58 +0800115static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
116 int new_level);
117static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
118 int blocking);
119
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700120#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
121
122/* This aids in debugging situations where a bad LVB might be involved. */
123static void ocfs2_dump_meta_lvb_info(u64 level,
124 const char *function,
125 unsigned int line,
126 struct ocfs2_lock_res *lockres)
127{
Mark Fasheha641dc22008-12-24 16:03:48 -0800128 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700129
130 mlog(level, "LVB information for %s (called from %s:%u):\n",
131 lockres->l_name, function, line);
132 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
133 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
134 be32_to_cpu(lvb->lvb_igeneration));
135 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
136 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
137 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
138 be16_to_cpu(lvb->lvb_imode));
139 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
140 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
141 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
142 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
143 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
144 be32_to_cpu(lvb->lvb_iattr));
145}
146
147
Mark Fashehf625c972006-09-12 21:24:53 -0700148/*
149 * OCFS2 Lock Resource Operations
150 *
151 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700152 *
153 * The most basic of lock types can point ->l_priv to their respective
154 * struct ocfs2_super and allow the default actions to manage things.
155 *
156 * Right now, each lock type also needs to implement an init function,
157 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
158 * should be called when the lock is no longer needed (i.e., object
159 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700160 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800161struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700162 /*
163 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
164 * this callback if ->l_priv is not an ocfs2_super pointer
165 */
166 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700167
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700168 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700169 * Optionally called in the downconvert thread after a
170 * successful downconvert. The lockres will not be referenced
171 * after this callback is called, so it is safe to free
172 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700173 *
174 * The exact semantics of when this is called are controlled
175 * by ->downconvert_worker()
176 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700177 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700178
179 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700180 * Allow a lock type to add checks to determine whether it is
181 * safe to downconvert a lock. Return 0 to re-queue the
182 * downconvert at a later time, nonzero to continue.
183 *
184 * For most locks, the default checks that there are no
185 * incompatible holders are sufficient.
186 *
187 * Called with the lockres spinlock held.
188 */
189 int (*check_downconvert)(struct ocfs2_lock_res *, int);
190
191 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700192 * Allows a lock type to populate the lock value block. This
193 * is called on downconvert, and when we drop a lock.
194 *
195 * Locks that want to use this should set LOCK_TYPE_USES_LVB
196 * in the flags field.
197 *
198 * Called with the lockres spinlock held.
199 */
200 void (*set_lvb)(struct ocfs2_lock_res *);
201
202 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700203 * Called from the downconvert thread when it is determined
204 * that a lock will be downconverted. This is called without
205 * any locks held so the function can do work that might
206 * schedule (syncing out data, etc).
207 *
208 * This should return any one of the ocfs2_unblock_action
209 * values, depending on what it wants the thread to do.
210 */
211 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
212
213 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700214 * LOCK_TYPE_* flags which describe the specific requirements
215 * of a lock type. Descriptions of each individual flag follow.
216 */
217 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800218};
219
Mark Fashehf625c972006-09-12 21:24:53 -0700220/*
221 * Some locks want to "refresh" potentially stale data when a
222 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
223 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
224 * individual lockres l_flags member from the ast function. It is
225 * expected that the locking wrapper will clear the
226 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
227 */
228#define LOCK_TYPE_REQUIRES_REFRESH 0x1
229
Mark Fashehb80fc012006-09-12 22:08:14 -0700230/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700231 * Indicate that a lock type makes use of the lock value block. The
232 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700233 */
234#define LOCK_TYPE_USES_LVB 0x2
235
Mark Fashehccd979b2005-12-15 14:31:24 -0800236static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700237 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700238 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800239};
240
Mark Fashehe63aecb62007-10-18 15:30:42 -0700241static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700242 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700243 .check_downconvert = ocfs2_check_meta_downconvert,
244 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700245 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700246 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800247};
248
Mark Fashehccd979b2005-12-15 14:31:24 -0800249static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700250 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800251};
252
253static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700254 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800255};
256
wengang wang6ca497a2009-03-06 21:29:10 +0800257static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
258 .flags = 0,
259};
260
Srinivas Eeda83273932009-06-03 17:02:55 -0700261static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
262 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
263};
264
Mark Fashehd680efe2006-09-08 14:14:34 -0700265static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700266 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700267 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700268 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700269 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700270};
271
Tiger Yang50008632007-03-20 16:01:38 -0700272static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
273 .get_osb = ocfs2_get_inode_osb,
274 .flags = 0,
275};
276
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800277static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
278 .get_osb = ocfs2_get_file_osb,
279 .flags = 0,
280};
281
Jan Kara9e33d692008-08-25 19:56:50 +0200282static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
283 .set_lvb = ocfs2_set_qinfo_lvb,
284 .get_osb = ocfs2_get_qinfo_osb,
285 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
286};
287
Tao Ma8dec98e2009-08-18 11:19:58 +0800288static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
289 .check_downconvert = ocfs2_check_refcount_downconvert,
290 .downconvert_worker = ocfs2_refcount_convert_worker,
291 .flags = 0,
292};
293
Mark Fashehccd979b2005-12-15 14:31:24 -0800294static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
295{
296 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700297 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
298 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800299}
300
Joel Beckerc0e41332010-01-29 14:46:44 -0800301static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
Joel Beckera796d282010-01-28 19:22:39 -0800302{
303 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
304}
305
Mark Fashehccd979b2005-12-15 14:31:24 -0800306static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
307{
308 BUG_ON(!ocfs2_is_inode_lock(lockres));
309
310 return (struct inode *) lockres->l_priv;
311}
312
Mark Fashehd680efe2006-09-08 14:14:34 -0700313static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
314{
315 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
316
317 return (struct ocfs2_dentry_lock *)lockres->l_priv;
318}
319
Jan Kara9e33d692008-08-25 19:56:50 +0200320static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
321{
322 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
323
324 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
325}
326
Tao Ma8dec98e2009-08-18 11:19:58 +0800327static inline struct ocfs2_refcount_tree *
328ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
329{
330 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
331}
332
Mark Fasheh54a7e752006-09-12 21:49:13 -0700333static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
334{
335 if (lockres->l_ops->get_osb)
336 return lockres->l_ops->get_osb(lockres);
337
338 return (struct ocfs2_super *)lockres->l_priv;
339}
340
Mark Fashehccd979b2005-12-15 14:31:24 -0800341static int ocfs2_lock_create(struct ocfs2_super *osb,
342 struct ocfs2_lock_res *lockres,
343 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800344 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800345static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
346 int wanted);
Jan Karacb257972009-06-04 15:26:50 +0200347static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
348 struct ocfs2_lock_res *lockres,
349 int level, unsigned long caller_ip);
350static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
351 struct ocfs2_lock_res *lockres,
352 int level)
353{
354 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
355}
356
Mark Fashehccd979b2005-12-15 14:31:24 -0800357static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
358static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
359static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
360static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
361static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
362 struct ocfs2_lock_res *lockres);
363static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
364 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800365#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
366 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
367 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
368 _err, _func, _lockres->l_name); \
369 else \
370 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
371 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
372 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800373} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700374static int ocfs2_downconvert_thread(void *arg);
375static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
376 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700377static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800378 struct buffer_head **bh);
379static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
380static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800381static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
382 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800383static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
384 struct ocfs2_lock_res *lockres,
385 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800386 int lvb,
387 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800388static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
389 struct ocfs2_lock_res *lockres);
390static int ocfs2_cancel_convert(struct ocfs2_super *osb,
391 struct ocfs2_lock_res *lockres);
392
Mark Fashehccd979b2005-12-15 14:31:24 -0800393
Mark Fashehccd979b2005-12-15 14:31:24 -0800394static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
395 u64 blkno,
396 u32 generation,
397 char *name)
398{
399 int len;
400
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
402
Mark Fashehb06970532006-03-03 10:24:33 -0800403 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
404 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
405 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800406
407 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
408
409 mlog(0, "built lock resource with name: %s\n", name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410}
411
Ingo Molnar34af9462006-06-27 02:53:55 -0700412static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800413
414static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
415 struct ocfs2_dlm_debug *dlm_debug)
416{
417 mlog(0, "Add tracking for lockres %s\n", res->l_name);
418
419 spin_lock(&ocfs2_dlm_tracking_lock);
420 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
421 spin_unlock(&ocfs2_dlm_tracking_lock);
422}
423
424static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
425{
426 spin_lock(&ocfs2_dlm_tracking_lock);
427 if (!list_empty(&res->l_debug_list))
428 list_del_init(&res->l_debug_list);
429 spin_unlock(&ocfs2_dlm_tracking_lock);
430}
431
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700432#ifdef CONFIG_OCFS2_FS_STATS
433static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
434{
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700435 res->l_lock_refresh = 0;
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800436 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
437 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700438}
439
440static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
441 struct ocfs2_mask_waiter *mw, int ret)
442{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800443 u32 usec;
444 ktime_t kt;
445 struct ocfs2_lock_stats *stats;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700446
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800447 if (level == LKM_PRMODE)
448 stats = &res->l_lock_prmode;
449 else if (level == LKM_EXMODE)
450 stats = &res->l_lock_exmode;
451 else
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700452 return;
453
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800454 kt = ktime_sub(ktime_get(), mw->mw_lock_start);
455 usec = ktime_to_us(kt);
456
457 stats->ls_gets++;
458 stats->ls_total += ktime_to_ns(kt);
459 /* overflow */
roel16865b72011-12-12 23:40:51 +0100460 if (unlikely(stats->ls_gets == 0)) {
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800461 stats->ls_gets++;
462 stats->ls_total = ktime_to_ns(kt);
463 }
464
465 if (stats->ls_max < usec)
466 stats->ls_max = usec;
467
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700468 if (ret)
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800469 stats->ls_fail++;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700470}
471
472static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
473{
474 lockres->l_lock_refresh++;
475}
476
477static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
478{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800479 mw->mw_lock_start = ktime_get();
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700480}
481#else
482static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
483{
484}
485static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
486 int level, struct ocfs2_mask_waiter *mw, int ret)
487{
488}
489static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
490{
491}
492static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
493{
494}
495#endif
496
Mark Fashehccd979b2005-12-15 14:31:24 -0800497static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
498 struct ocfs2_lock_res *res,
499 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800500 struct ocfs2_lock_res_ops *ops,
501 void *priv)
502{
Mark Fashehccd979b2005-12-15 14:31:24 -0800503 res->l_type = type;
504 res->l_ops = ops;
505 res->l_priv = priv;
506
Joel Beckerbd3e7612008-02-01 12:14:57 -0800507 res->l_level = DLM_LOCK_IV;
508 res->l_requested = DLM_LOCK_IV;
509 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 res->l_action = OCFS2_AST_INVALID;
511 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
512
513 res->l_flags = OCFS2_LOCK_INITIALIZED;
514
515 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700516
517 ocfs2_init_lock_stats(res);
Jan Karacb257972009-06-04 15:26:50 +0200518#ifdef CONFIG_DEBUG_LOCK_ALLOC
519 if (type != OCFS2_LOCK_TYPE_OPEN)
520 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
521 &lockdep_keys[type], 0);
522 else
523 res->l_lockdep_map.key = NULL;
524#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800525}
526
527void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
528{
529 /* This also clears out the lock status block */
530 memset(res, 0, sizeof(struct ocfs2_lock_res));
531 spin_lock_init(&res->l_lock);
532 init_waitqueue_head(&res->l_event);
533 INIT_LIST_HEAD(&res->l_blocked_list);
534 INIT_LIST_HEAD(&res->l_mask_waiters);
Eric Ren2ada5922017-02-22 15:40:41 -0800535 INIT_LIST_HEAD(&res->l_holders);
Mark Fashehccd979b2005-12-15 14:31:24 -0800536}
537
538void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
539 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700540 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800541 struct inode *inode)
542{
543 struct ocfs2_lock_res_ops *ops;
544
545 switch(type) {
546 case OCFS2_LOCK_TYPE_RW:
547 ops = &ocfs2_inode_rw_lops;
548 break;
549 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700550 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800551 break;
Tiger Yang50008632007-03-20 16:01:38 -0700552 case OCFS2_LOCK_TYPE_OPEN:
553 ops = &ocfs2_inode_open_lops;
554 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800555 default:
556 mlog_bug_on_msg(1, "type: %d\n", type);
557 ops = NULL; /* thanks, gcc */
558 break;
559 };
560
Mark Fashehd680efe2006-09-08 14:14:34 -0700561 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700562 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700563 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
564}
565
Mark Fasheh54a7e752006-09-12 21:49:13 -0700566static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
567{
568 struct inode *inode = ocfs2_lock_res_inode(lockres);
569
570 return OCFS2_SB(inode->i_sb);
571}
572
Jan Kara9e33d692008-08-25 19:56:50 +0200573static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
574{
575 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
576
577 return OCFS2_SB(info->dqi_gi.dqi_sb);
578}
579
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800580static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
581{
582 struct ocfs2_file_private *fp = lockres->l_priv;
583
584 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
585}
586
Mark Fashehd680efe2006-09-08 14:14:34 -0700587static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
588{
589 __be64 inode_blkno_be;
590
591 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
592 sizeof(__be64));
593
594 return be64_to_cpu(inode_blkno_be);
595}
596
Mark Fasheh54a7e752006-09-12 21:49:13 -0700597static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
598{
599 struct ocfs2_dentry_lock *dl = lockres->l_priv;
600
601 return OCFS2_SB(dl->dl_inode->i_sb);
602}
603
Mark Fashehd680efe2006-09-08 14:14:34 -0700604void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
605 u64 parent, struct inode *inode)
606{
607 int len;
608 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
609 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
610 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
611
612 ocfs2_lock_res_init_once(lockres);
613
614 /*
615 * Unfortunately, the standard lock naming scheme won't work
616 * here because we have two 16 byte values to use. Instead,
617 * we'll stuff the inode number as a binary value. We still
618 * want error prints to show something without garbling the
619 * display, so drop a null byte in there before the inode
620 * number. A future version of OCFS2 will likely use all
621 * binary lock names. The stringified names have been a
622 * tremendous aid in debugging, but now that the debugfs
623 * interface exists, we can mangle things there if need be.
624 *
625 * NOTE: We also drop the standard "pad" value (the total lock
626 * name size stays the same though - the last part is all
627 * zeros due to the memset in ocfs2_lock_res_init_once()
628 */
629 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
630 "%c%016llx",
631 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
632 (long long)parent);
633
634 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
635
636 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
637 sizeof(__be64));
638
639 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
640 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
641 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800642}
643
644static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
645 struct ocfs2_super *osb)
646{
647 /* Superblock lockres doesn't come from a slab so we call init
648 * once on it manually. */
649 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700650 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
651 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800652 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 &ocfs2_super_lops, osb);
654}
655
656static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
657 struct ocfs2_super *osb)
658{
659 /* Rename lockres doesn't come from a slab so we call init
660 * once on it manually. */
661 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700662 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
663 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800664 &ocfs2_rename_lops, osb);
665}
666
wengang wang6ca497a2009-03-06 21:29:10 +0800667static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
668 struct ocfs2_super *osb)
669{
670 /* nfs_sync lockres doesn't come from a slab so we call init
671 * once on it manually. */
672 ocfs2_lock_res_init_once(res);
673 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
674 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
675 &ocfs2_nfs_sync_lops, osb);
676}
677
Srinivas Eeda83273932009-06-03 17:02:55 -0700678static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
679 struct ocfs2_super *osb)
680{
Srinivas Eeda83273932009-06-03 17:02:55 -0700681 ocfs2_lock_res_init_once(res);
682 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
683 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
684 &ocfs2_orphan_scan_lops, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -0700685}
686
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800687void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
688 struct ocfs2_file_private *fp)
689{
690 struct inode *inode = fp->fp_file->f_mapping->host;
691 struct ocfs2_inode_info *oi = OCFS2_I(inode);
692
693 ocfs2_lock_res_init_once(lockres);
694 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
695 inode->i_generation, lockres->l_name);
696 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
697 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
698 fp);
699 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
700}
701
Jan Kara9e33d692008-08-25 19:56:50 +0200702void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
703 struct ocfs2_mem_dqinfo *info)
704{
705 ocfs2_lock_res_init_once(lockres);
706 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
707 0, lockres->l_name);
708 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
709 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
710 info);
711}
712
Tao Ma8dec98e2009-08-18 11:19:58 +0800713void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
714 struct ocfs2_super *osb, u64 ref_blkno,
715 unsigned int generation)
716{
717 ocfs2_lock_res_init_once(lockres);
718 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
719 generation, lockres->l_name);
720 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
721 &ocfs2_refcount_block_lops, osb);
722}
723
Mark Fashehccd979b2005-12-15 14:31:24 -0800724void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
725{
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
727 return;
728
729 ocfs2_remove_lockres_tracking(res);
730
731 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
732 "Lockres %s is on the blocked list\n",
733 res->l_name);
734 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
735 "Lockres %s has mask waiters pending\n",
736 res->l_name);
737 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
738 "Lockres %s is locked\n",
739 res->l_name);
740 mlog_bug_on_msg(res->l_ro_holders,
741 "Lockres %s has %u ro holders\n",
742 res->l_name, res->l_ro_holders);
743 mlog_bug_on_msg(res->l_ex_holders,
744 "Lockres %s has %u ex holders\n",
745 res->l_name, res->l_ex_holders);
746
747 /* Need to clear out the lock status block for the dlm */
748 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
749
750 res->l_flags = 0UL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800751}
752
Eric Ren2ada5922017-02-22 15:40:41 -0800753/*
754 * Keep a list of processes who have interest in a lockres.
755 * Note: this is now only uesed for check recursive cluster locking.
756 */
757static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
758 struct ocfs2_lock_holder *oh)
759{
760 INIT_LIST_HEAD(&oh->oh_list);
761 oh->oh_owner_pid = get_pid(task_pid(current));
762
763 spin_lock(&lockres->l_lock);
764 list_add_tail(&oh->oh_list, &lockres->l_holders);
765 spin_unlock(&lockres->l_lock);
766}
767
768static inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
769 struct ocfs2_lock_holder *oh)
770{
771 spin_lock(&lockres->l_lock);
772 list_del(&oh->oh_list);
773 spin_unlock(&lockres->l_lock);
774
775 put_pid(oh->oh_owner_pid);
776}
777
778static inline int ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)
779{
780 struct ocfs2_lock_holder *oh;
781 struct pid *pid;
782
783 /* look in the list of holders for one with the current task as owner */
784 spin_lock(&lockres->l_lock);
785 pid = task_pid(current);
786 list_for_each_entry(oh, &lockres->l_holders, oh_list) {
787 if (oh->oh_owner_pid == pid) {
788 spin_unlock(&lockres->l_lock);
789 return 1;
790 }
791 }
792 spin_unlock(&lockres->l_lock);
793
794 return 0;
795}
796
Mark Fashehccd979b2005-12-15 14:31:24 -0800797static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
798 int level)
799{
Mark Fashehccd979b2005-12-15 14:31:24 -0800800 BUG_ON(!lockres);
801
802 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800803 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 lockres->l_ex_holders++;
805 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800806 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800807 lockres->l_ro_holders++;
808 break;
809 default:
810 BUG();
811 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800812}
813
814static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
815 int level)
816{
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 BUG_ON(!lockres);
818
819 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800820 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800821 BUG_ON(!lockres->l_ex_holders);
822 lockres->l_ex_holders--;
823 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800824 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 BUG_ON(!lockres->l_ro_holders);
826 lockres->l_ro_holders--;
827 break;
828 default:
829 BUG();
830 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800831}
832
833/* WARNING: This function lives in a world where the only three lock
834 * levels are EX, PR, and NL. It *will* have to be adjusted when more
835 * lock types are added. */
836static inline int ocfs2_highest_compat_lock_level(int level)
837{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800838 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800839
Joel Beckerbd3e7612008-02-01 12:14:57 -0800840 if (level == DLM_LOCK_EX)
841 new_level = DLM_LOCK_NL;
842 else if (level == DLM_LOCK_PR)
843 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800844 return new_level;
845}
846
847static void lockres_set_flags(struct ocfs2_lock_res *lockres,
848 unsigned long newflags)
849{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200850 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800851
852 assert_spin_locked(&lockres->l_lock);
853
854 lockres->l_flags = newflags;
855
Christoph Hellwig800deef2007-05-17 16:03:13 +0200856 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
858 continue;
859
860 list_del_init(&mw->mw_item);
861 mw->mw_status = 0;
862 complete(&mw->mw_complete);
863 }
864}
865static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
866{
867 lockres_set_flags(lockres, lockres->l_flags | or);
868}
869static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
870 unsigned long clear)
871{
872 lockres_set_flags(lockres, lockres->l_flags & ~clear);
873}
874
875static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
876{
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
878 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
879 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800880 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800881
882 lockres->l_level = lockres->l_requested;
883 if (lockres->l_level <=
884 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800885 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800886 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
887 }
888 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800889}
890
891static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
892{
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
894 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
895
896 /* Convert from RO to EX doesn't really need anything as our
897 * information is already up to data. Convert from NL to
898 * *anything* however should mark ourselves as needing an
899 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800900 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700901 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
903
904 lockres->l_level = lockres->l_requested;
Sunil Mushrana1912822010-01-21 10:50:03 -0800905
906 /*
907 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
908 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
909 * downconverting the lock before the upconvert has fully completed.
Xue jiufeid1e782382014-12-10 15:41:59 -0800910 * Do not prevent the dc thread from downconverting if NONBLOCK lock
911 * had already returned.
Sunil Mushrana1912822010-01-21 10:50:03 -0800912 */
Xue jiufeid1e782382014-12-10 15:41:59 -0800913 if (!(lockres->l_flags & OCFS2_LOCK_NONBLOCK_FINISHED))
914 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
915 else
916 lockres_clear_flags(lockres, OCFS2_LOCK_NONBLOCK_FINISHED);
Sunil Mushrana1912822010-01-21 10:50:03 -0800917
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800919}
920
921static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
922{
Roel Kluin3cf0c502007-10-27 00:20:36 +0200923 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800924 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
925
Joel Beckerbd3e7612008-02-01 12:14:57 -0800926 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700927 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
928 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
930
931 lockres->l_level = lockres->l_requested;
932 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
933 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800934}
935
Mark Fashehccd979b2005-12-15 14:31:24 -0800936static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
937 int level)
938{
939 int needs_downconvert = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800940
941 assert_spin_locked(&lockres->l_lock);
942
Mark Fashehccd979b2005-12-15 14:31:24 -0800943 if (level > lockres->l_blocking) {
944 /* only schedule a downconvert if we haven't already scheduled
945 * one that goes low enough to satisfy the level we're
946 * blocking. this also catches the case where we get
947 * duplicate BASTs */
948 if (ocfs2_highest_compat_lock_level(level) <
949 ocfs2_highest_compat_lock_level(lockres->l_blocking))
950 needs_downconvert = 1;
951
952 lockres->l_blocking = level;
953 }
954
Sunil Mushran9b915182010-02-26 19:42:44 -0800955 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
956 lockres->l_name, level, lockres->l_level, lockres->l_blocking,
957 needs_downconvert);
958
Wengang Wang0b94a902010-01-21 10:50:02 -0800959 if (needs_downconvert)
960 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Tao Mac1e8d352011-03-07 16:43:21 +0800961 mlog(0, "needs_downconvert = %d\n", needs_downconvert);
Mark Fashehccd979b2005-12-15 14:31:24 -0800962 return needs_downconvert;
963}
964
Joel Beckerde551242008-02-01 14:45:08 -0800965/*
966 * OCFS2_LOCK_PENDING and l_pending_gen.
967 *
968 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
969 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
970 * for more details on the race.
971 *
972 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
973 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
974 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
975 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
976 * the caller is going to try to clear PENDING again. If nothing else is
977 * happening, __lockres_clear_pending() sees PENDING is unset and does
978 * nothing.
979 *
980 * But what if another path (eg downconvert thread) has just started a
981 * new locking action? The other path has re-set PENDING. Our path
982 * cannot clear PENDING, because that will re-open the original race
983 * window.
984 *
985 * [Example]
986 *
987 * ocfs2_meta_lock()
988 * ocfs2_cluster_lock()
989 * set BUSY
990 * set PENDING
991 * drop l_lock
992 * ocfs2_dlm_lock()
993 * ocfs2_locking_ast() ocfs2_downconvert_thread()
994 * clear PENDING ocfs2_unblock_lock()
995 * take_l_lock
996 * !BUSY
997 * ocfs2_prepare_downconvert()
998 * set BUSY
999 * set PENDING
1000 * drop l_lock
1001 * take l_lock
1002 * clear PENDING
1003 * drop l_lock
1004 * <window>
1005 * ocfs2_dlm_lock()
1006 *
1007 * So as you can see, we now have a window where l_lock is not held,
1008 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
1009 *
1010 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
1011 * set by ocfs2_prepare_downconvert(). That wasn't nice.
1012 *
1013 * To solve this we introduce l_pending_gen. A call to
1014 * lockres_clear_pending() will only do so when it is passed a generation
1015 * number that matches the lockres. lockres_set_pending() will return the
1016 * current generation number. When ocfs2_cluster_lock() goes to clear
1017 * PENDING, it passes the generation it got from set_pending(). In our
1018 * example above, the generation numbers will *not* match. Thus,
1019 * ocfs2_cluster_lock() will not clear the PENDING set by
1020 * ocfs2_prepare_downconvert().
1021 */
1022
1023/* Unlocked version for ocfs2_locking_ast() */
1024static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
1025 unsigned int generation,
1026 struct ocfs2_super *osb)
1027{
1028 assert_spin_locked(&lockres->l_lock);
1029
1030 /*
1031 * The ast and locking functions can race us here. The winner
1032 * will clear pending, the loser will not.
1033 */
1034 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
1035 (lockres->l_pending_gen != generation))
1036 return;
1037
1038 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1039 lockres->l_pending_gen++;
1040
1041 /*
1042 * The downconvert thread may have skipped us because we
1043 * were PENDING. Wake it up.
1044 */
1045 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1046 ocfs2_wake_downconvert_thread(osb);
1047}
1048
1049/* Locked version for callers of ocfs2_dlm_lock() */
1050static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1051 unsigned int generation,
1052 struct ocfs2_super *osb)
1053{
1054 unsigned long flags;
1055
1056 spin_lock_irqsave(&lockres->l_lock, flags);
1057 __lockres_clear_pending(lockres, generation, osb);
1058 spin_unlock_irqrestore(&lockres->l_lock, flags);
1059}
1060
1061static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1062{
1063 assert_spin_locked(&lockres->l_lock);
1064 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1065
1066 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1067
1068 return lockres->l_pending_gen;
1069}
1070
Joel Beckerc0e41332010-01-29 14:46:44 -08001071static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001072{
Joel Beckera796d282010-01-28 19:22:39 -08001073 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehaa2623a2006-09-12 21:58:23 -07001074 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 int needs_downconvert;
1076 unsigned long flags;
1077
Joel Beckerbd3e7612008-02-01 12:14:57 -08001078 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001079
Sunil Mushran9b915182010-02-26 19:42:44 -08001080 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1081 "type %s\n", lockres->l_name, level, lockres->l_level,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001082 ocfs2_lock_type_string(lockres->l_type));
1083
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001084 /*
1085 * We can skip the bast for locks which don't enable caching -
1086 * they'll be dropped at the earliest possible time anyway.
1087 */
1088 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1089 return;
1090
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 spin_lock_irqsave(&lockres->l_lock, flags);
1092 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1093 if (needs_downconvert)
1094 ocfs2_schedule_blocked_lock(osb, lockres);
1095 spin_unlock_irqrestore(&lockres->l_lock, flags);
1096
Mark Fashehd680efe2006-09-08 14:14:34 -07001097 wake_up(&lockres->l_event);
1098
Mark Fasheh34d024f2007-09-24 15:56:19 -07001099 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100}
1101
Joel Beckerc0e41332010-01-29 14:46:44 -08001102static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
Mark Fashehccd979b2005-12-15 14:31:24 -08001103{
Joel Beckera796d282010-01-28 19:22:39 -08001104 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Joel Beckerde551242008-02-01 14:45:08 -08001105 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001107 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001108
1109 spin_lock_irqsave(&lockres->l_lock, flags);
1110
David Teigland1693a5c2008-01-30 16:52:53 -08001111 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1112
1113 if (status == -EAGAIN) {
1114 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1115 goto out;
1116 }
1117
1118 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001119 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001120 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 spin_unlock_irqrestore(&lockres->l_lock, flags);
1122 return;
1123 }
1124
Sunil Mushran9b915182010-02-26 19:42:44 -08001125 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
1126 "level %d => %d\n", lockres->l_name, lockres->l_action,
1127 lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
1128
Mark Fashehccd979b2005-12-15 14:31:24 -08001129 switch(lockres->l_action) {
1130 case OCFS2_AST_ATTACH:
1131 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001132 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 break;
1134 case OCFS2_AST_CONVERT:
1135 ocfs2_generic_handle_convert_action(lockres);
1136 break;
1137 case OCFS2_AST_DOWNCONVERT:
1138 ocfs2_generic_handle_downconvert_action(lockres);
1139 break;
1140 default:
Sunil Mushran9b915182010-02-26 19:42:44 -08001141 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1142 "flags 0x%lx, unlock: %u\n",
Mark Fashehe92d57d2006-09-12 21:34:35 -07001143 lockres->l_name, lockres->l_action, lockres->l_flags,
1144 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 BUG();
1146 }
David Teigland1693a5c2008-01-30 16:52:53 -08001147out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 /* set it to something invalid so if we get called again we
1149 * can catch it. */
1150 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001151
Joel Beckerde551242008-02-01 14:45:08 -08001152 /* Did we try to cancel this lock? Clear that state */
1153 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1154 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1155
1156 /*
1157 * We may have beaten the locking functions here. We certainly
1158 * know that dlm_lock() has been called :-)
1159 * Because we can't have two lock calls in flight at once, we
1160 * can use lockres->l_pending_gen.
1161 */
1162 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1163
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001165 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001166}
1167
Joel Becker553b5eb2010-01-29 17:19:06 -08001168static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
1169{
1170 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1171 unsigned long flags;
1172
Sunil Mushran9b915182010-02-26 19:42:44 -08001173 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1174 lockres->l_name, lockres->l_unlock_action);
Joel Becker553b5eb2010-01-29 17:19:06 -08001175
1176 spin_lock_irqsave(&lockres->l_lock, flags);
1177 if (error) {
1178 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
1179 "unlock_action %d\n", error, lockres->l_name,
1180 lockres->l_unlock_action);
1181 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Becker553b5eb2010-01-29 17:19:06 -08001182 return;
1183 }
1184
1185 switch(lockres->l_unlock_action) {
1186 case OCFS2_UNLOCK_CANCEL_CONVERT:
1187 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
1188 lockres->l_action = OCFS2_AST_INVALID;
1189 /* Downconvert thread may have requeued this lock, we
1190 * need to wake it. */
1191 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1192 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
1193 break;
1194 case OCFS2_UNLOCK_DROP_LOCK:
1195 lockres->l_level = DLM_LOCK_IV;
1196 break;
1197 default:
1198 BUG();
1199 }
1200
1201 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1202 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1203 wake_up(&lockres->l_event);
1204 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Becker553b5eb2010-01-29 17:19:06 -08001205}
1206
1207/*
1208 * This is the filesystem locking protocol. It provides the lock handling
1209 * hooks for the underlying DLM. It has a maximum version number.
1210 * The version number allows interoperability with systems running at
1211 * the same major number and an equal or smaller minor number.
1212 *
1213 * Whenever the filesystem does new things with locks (adds or removes a
1214 * lock, orders them differently, does different things underneath a lock),
1215 * the version must be changed. The protocol is negotiated when joining
1216 * the dlm domain. A node may join the domain if its major version is
1217 * identical to all other nodes and its minor version is greater than
1218 * or equal to all other nodes. When its minor version is greater than
1219 * the other nodes, it will run at the minor version specified by the
1220 * other nodes.
1221 *
1222 * If a locking change is made that will not be compatible with older
1223 * versions, the major number must be increased and the minor version set
1224 * to zero. If a change merely adds a behavior that can be disabled when
1225 * speaking to older versions, the minor version must be increased. If a
1226 * change adds a fully backwards compatible change (eg, LVB changes that
1227 * are just ignored by older versions), the version does not need to be
1228 * updated.
1229 */
1230static struct ocfs2_locking_protocol lproto = {
1231 .lp_max_version = {
1232 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
1233 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
1234 },
1235 .lp_lock_ast = ocfs2_locking_ast,
1236 .lp_blocking_ast = ocfs2_blocking_ast,
1237 .lp_unlock_ast = ocfs2_unlock_ast,
1238};
1239
1240void ocfs2_set_locking_protocol(void)
1241{
1242 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1243}
1244
Mark Fashehccd979b2005-12-15 14:31:24 -08001245static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1246 int convert)
1247{
1248 unsigned long flags;
1249
Mark Fashehccd979b2005-12-15 14:31:24 -08001250 spin_lock_irqsave(&lockres->l_lock, flags);
1251 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Sunil Mushrana1912822010-01-21 10:50:03 -08001252 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
Mark Fashehccd979b2005-12-15 14:31:24 -08001253 if (convert)
1254 lockres->l_action = OCFS2_AST_INVALID;
1255 else
1256 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1257 spin_unlock_irqrestore(&lockres->l_lock, flags);
1258
1259 wake_up(&lockres->l_event);
Mark Fashehccd979b2005-12-15 14:31:24 -08001260}
1261
1262/* Note: If we detect another process working on the lock (i.e.,
1263 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1264 * to do the right thing in that case.
1265 */
1266static int ocfs2_lock_create(struct ocfs2_super *osb,
1267 struct ocfs2_lock_res *lockres,
1268 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001269 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001270{
1271 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001272 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001273 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274
Joel Beckerbd3e7612008-02-01 12:14:57 -08001275 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 dlm_flags);
1277
1278 spin_lock_irqsave(&lockres->l_lock, flags);
1279 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1280 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1281 spin_unlock_irqrestore(&lockres->l_lock, flags);
1282 goto bail;
1283 }
1284
1285 lockres->l_action = OCFS2_AST_ATTACH;
1286 lockres->l_requested = level;
1287 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001288 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 spin_unlock_irqrestore(&lockres->l_lock, flags);
1290
Joel Becker4670c462008-02-01 14:39:35 -08001291 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001292 level,
1293 &lockres->l_lksb,
1294 dlm_flags,
1295 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001296 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001297 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001298 if (ret) {
1299 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 ocfs2_recover_from_dlm_error(lockres, 1);
1301 }
1302
Joel Becker7431cd72008-02-01 12:15:37 -08001303 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001304
1305bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001306 return ret;
1307}
1308
1309static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1310 int flag)
1311{
1312 unsigned long flags;
1313 int ret;
1314
1315 spin_lock_irqsave(&lockres->l_lock, flags);
1316 ret = lockres->l_flags & flag;
1317 spin_unlock_irqrestore(&lockres->l_lock, flags);
1318
1319 return ret;
1320}
1321
1322static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1323
1324{
1325 wait_event(lockres->l_event,
1326 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1327}
1328
1329static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1330
1331{
1332 wait_event(lockres->l_event,
1333 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1334}
1335
1336/* predict what lock level we'll be dropping down to on behalf
1337 * of another node, and return true if the currently wanted
1338 * level will be compatible with it. */
1339static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1340 int wanted)
1341{
1342 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1343
1344 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1345}
1346
1347static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1348{
1349 INIT_LIST_HEAD(&mw->mw_item);
1350 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001351 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001352}
1353
1354static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1355{
1356 wait_for_completion(&mw->mw_complete);
1357 /* Re-arm the completion in case we want to wait on it again */
Wolfram Sang16735d02013-11-14 14:32:02 -08001358 reinit_completion(&mw->mw_complete);
Mark Fashehccd979b2005-12-15 14:31:24 -08001359 return mw->mw_status;
1360}
1361
1362static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1363 struct ocfs2_mask_waiter *mw,
1364 unsigned long mask,
1365 unsigned long goal)
1366{
1367 BUG_ON(!list_empty(&mw->mw_item));
1368
1369 assert_spin_locked(&lockres->l_lock);
1370
1371 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1372 mw->mw_mask = mask;
1373 mw->mw_goal = goal;
1374}
1375
1376/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1377 * if the mask still hadn't reached its goal */
Xue jiufeid1e782382014-12-10 15:41:59 -08001378static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
Mark Fashehccd979b2005-12-15 14:31:24 -08001379 struct ocfs2_mask_waiter *mw)
1380{
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 int ret = 0;
1382
Xue jiufeid1e782382014-12-10 15:41:59 -08001383 assert_spin_locked(&lockres->l_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001384 if (!list_empty(&mw->mw_item)) {
1385 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1386 ret = -EBUSY;
1387
1388 list_del_init(&mw->mw_item);
1389 init_completion(&mw->mw_complete);
1390 }
Xue jiufeid1e782382014-12-10 15:41:59 -08001391
1392 return ret;
1393}
1394
1395static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1396 struct ocfs2_mask_waiter *mw)
1397{
1398 unsigned long flags;
1399 int ret = 0;
1400
1401 spin_lock_irqsave(&lockres->l_lock, flags);
1402 ret = __lockres_remove_mask_waiter(lockres, mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 spin_unlock_irqrestore(&lockres->l_lock, flags);
1404
1405 return ret;
1406
1407}
1408
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001409static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1410 struct ocfs2_lock_res *lockres)
1411{
1412 int ret;
1413
1414 ret = wait_for_completion_interruptible(&mw->mw_complete);
1415 if (ret)
1416 lockres_remove_mask_waiter(lockres, mw);
1417 else
1418 ret = mw->mw_status;
1419 /* Re-arm the completion in case we want to wait on it again */
Wolfram Sang16735d02013-11-14 14:32:02 -08001420 reinit_completion(&mw->mw_complete);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001421 return ret;
1422}
1423
Jan Karacb257972009-06-04 15:26:50 +02001424static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1425 struct ocfs2_lock_res *lockres,
1426 int level,
1427 u32 lkm_flags,
1428 int arg_flags,
1429 int l_subclass,
1430 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001431{
1432 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1434 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1435 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001436 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001437 int noqueue_attempted = 0;
Xue jiufeid1e782382014-12-10 15:41:59 -08001438 int dlm_locked = 0;
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001439 int kick_dc = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001440
alex chen2f2eca22015-04-14 15:43:49 -07001441 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) {
1442 mlog_errno(-EINVAL);
1443 return -EINVAL;
1444 }
1445
Mark Fashehccd979b2005-12-15 14:31:24 -08001446 ocfs2_init_mask_waiter(&mw);
1447
Mark Fashehb80fc012006-09-12 22:08:14 -07001448 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001449 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001450
Mark Fashehccd979b2005-12-15 14:31:24 -08001451again:
1452 wait = 0;
1453
Sunil Mushrana1912822010-01-21 10:50:03 -08001454 spin_lock_irqsave(&lockres->l_lock, flags);
1455
Mark Fashehccd979b2005-12-15 14:31:24 -08001456 if (catch_signals && signal_pending(current)) {
1457 ret = -ERESTARTSYS;
Sunil Mushrana1912822010-01-21 10:50:03 -08001458 goto unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 }
1460
Mark Fashehccd979b2005-12-15 14:31:24 -08001461 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1462 "Cluster lock called on freeing lockres %s! flags "
1463 "0x%lx\n", lockres->l_name, lockres->l_flags);
1464
1465 /* We only compare against the currently granted level
1466 * here. If the lock is blocked waiting on a downconvert,
1467 * we'll get caught below. */
1468 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1469 level > lockres->l_level) {
1470 /* is someone sitting in dlm_lock? If so, wait on
1471 * them. */
1472 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1473 wait = 1;
1474 goto unlock;
1475 }
1476
Sunil Mushrana1912822010-01-21 10:50:03 -08001477 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1478 /*
1479 * We've upconverted. If the lock now has a level we can
1480 * work with, we take it. If, however, the lock is not at the
1481 * required level, we go thru the full cycle. One way this could
1482 * happen is if a process requesting an upconvert to PR is
1483 * closely followed by another requesting upconvert to an EX.
1484 * If the process requesting EX lands here, we want it to
1485 * continue attempting to upconvert and let the process
1486 * requesting PR take the lock.
1487 * If multiple processes request upconvert to PR, the first one
1488 * here will take the lock. The others will have to go thru the
1489 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1490 * downconvert request.
1491 */
1492 if (level <= lockres->l_level)
1493 goto update_holders;
1494 }
1495
Mark Fashehccd979b2005-12-15 14:31:24 -08001496 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1497 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1498 /* is the lock is currently blocked on behalf of
1499 * another node */
1500 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1501 wait = 1;
1502 goto unlock;
1503 }
1504
1505 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001506 if (noqueue_attempted > 0) {
1507 ret = -EAGAIN;
1508 goto unlock;
1509 }
1510 if (lkm_flags & DLM_LKF_NOQUEUE)
1511 noqueue_attempted = 1;
1512
Mark Fashehccd979b2005-12-15 14:31:24 -08001513 if (lockres->l_action != OCFS2_AST_INVALID)
1514 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1515 lockres->l_name, lockres->l_action);
1516
Mark Fasheh019d1b22007-10-05 12:09:05 -07001517 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1518 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001519 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001520 } else {
1521 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001522 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001523 }
1524
Mark Fashehccd979b2005-12-15 14:31:24 -08001525 lockres->l_requested = level;
1526 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001527 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 spin_unlock_irqrestore(&lockres->l_lock, flags);
1529
Joel Beckerbd3e7612008-02-01 12:14:57 -08001530 BUG_ON(level == DLM_LOCK_IV);
1531 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001532
Sunil Mushran9b915182010-02-26 19:42:44 -08001533 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 lockres->l_name, lockres->l_level, level);
1535
1536 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001537 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001538 level,
1539 &lockres->l_lksb,
1540 lkm_flags,
1541 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001542 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001543 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001544 if (ret) {
1545 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1546 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001547 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001548 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001549 }
1550 ocfs2_recover_from_dlm_error(lockres, 1);
1551 goto out;
1552 }
Xue jiufeid1e782382014-12-10 15:41:59 -08001553 dlm_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001554
Coly Li73ac36e2009-01-07 18:09:16 -08001555 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001556 lockres->l_name);
1557
1558 /* At this point we've gone inside the dlm and need to
1559 * complete our work regardless. */
1560 catch_signals = 0;
1561
1562 /* wait for busy to clear and carry on */
1563 goto again;
1564 }
1565
Sunil Mushrana1912822010-01-21 10:50:03 -08001566update_holders:
Mark Fashehccd979b2005-12-15 14:31:24 -08001567 /* Ok, if we get here then we're good to go. */
1568 ocfs2_inc_holders(lockres, level);
1569
1570 ret = 0;
1571unlock:
Sunil Mushrana1912822010-01-21 10:50:03 -08001572 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1573
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001574 /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */
1575 kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED);
1576
Mark Fashehccd979b2005-12-15 14:31:24 -08001577 spin_unlock_irqrestore(&lockres->l_lock, flags);
Tariq Saeedb1b1e152016-01-21 16:40:39 -08001578 if (kick_dc)
1579 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001580out:
1581 /*
1582 * This is helping work around a lock inversion between the page lock
1583 * and dlm locks. One path holds the page lock while calling aops
1584 * which block acquiring dlm locks. The voting thread holds dlm
1585 * locks while acquiring page locks while down converting data locks.
1586 * This block is helping an aop path notice the inversion and back
1587 * off to unlock its page lock before trying the dlm lock again.
1588 */
1589 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1590 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1591 wait = 0;
Xue jiufeid1e782382014-12-10 15:41:59 -08001592 spin_lock_irqsave(&lockres->l_lock, flags);
1593 if (__lockres_remove_mask_waiter(lockres, &mw)) {
1594 if (dlm_locked)
1595 lockres_or_flags(lockres,
1596 OCFS2_LOCK_NONBLOCK_FINISHED);
1597 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001598 ret = -EAGAIN;
Xue jiufeid1e782382014-12-10 15:41:59 -08001599 } else {
1600 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 goto again;
Xue jiufeid1e782382014-12-10 15:41:59 -08001602 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001603 }
1604 if (wait) {
1605 ret = ocfs2_wait_for_mask(&mw);
1606 if (ret == 0)
1607 goto again;
1608 mlog_errno(ret);
1609 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001610 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001611
Jan Karacb257972009-06-04 15:26:50 +02001612#ifdef CONFIG_DEBUG_LOCK_ALLOC
1613 if (!ret && lockres->l_lockdep_map.key != NULL) {
1614 if (level == DLM_LOCK_PR)
1615 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1616 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1617 caller_ip);
1618 else
1619 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1620 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1621 caller_ip);
1622 }
1623#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001624 return ret;
1625}
1626
Jan Karacb257972009-06-04 15:26:50 +02001627static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1628 struct ocfs2_lock_res *lockres,
1629 int level,
1630 u32 lkm_flags,
1631 int arg_flags)
1632{
1633 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1634 0, _RET_IP_);
1635}
1636
1637
1638static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1639 struct ocfs2_lock_res *lockres,
1640 int level,
1641 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001642{
1643 unsigned long flags;
1644
Mark Fashehccd979b2005-12-15 14:31:24 -08001645 spin_lock_irqsave(&lockres->l_lock, flags);
1646 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001647 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001648 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001649#ifdef CONFIG_DEBUG_LOCK_ALLOC
1650 if (lockres->l_lockdep_map.key != NULL)
1651 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1652#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001653}
1654
Adrian Bunkda661162006-11-20 03:24:28 +01001655static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1656 struct ocfs2_lock_res *lockres,
1657 int ex,
1658 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001659{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001660 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001661 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001662 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001663
1664 spin_lock_irqsave(&lockres->l_lock, flags);
1665 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1666 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1667 spin_unlock_irqrestore(&lockres->l_lock, flags);
1668
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001669 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001670}
1671
1672/* Grants us an EX lock on the data and metadata resources, skipping
1673 * the normal cluster directory lookup. Use this ONLY on newly created
1674 * inodes which other nodes can't possibly see, and which haven't been
1675 * hashed in the inode hash yet. This can give us a good performance
1676 * increase as it'll skip the network broadcast normally associated
1677 * with creating a new lock resource. */
1678int ocfs2_create_new_inode_locks(struct inode *inode)
1679{
1680 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001681 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001682
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 BUG_ON(!ocfs2_inode_is_new(inode));
1684
Mark Fashehb06970532006-03-03 10:24:33 -08001685 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001686
1687 /* NOTE: That we don't increment any of the holder counts, nor
1688 * do we add anything to a journal handle. Since this is
1689 * supposed to be a new inode which the cluster doesn't know
1690 * about yet, there is no need to. As far as the LVB handling
1691 * is concerned, this is basically like acquiring an EX lock
1692 * on a resource which has an invalid one -- we'll set it
1693 * valid when we release the EX. */
1694
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001695 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001696 if (ret) {
1697 mlog_errno(ret);
1698 goto bail;
1699 }
1700
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001701 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001702 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001703 * don't use a generation in their lock names.
1704 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001705 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001706 if (ret) {
1707 mlog_errno(ret);
1708 goto bail;
1709 }
1710
Tiger Yang50008632007-03-20 16:01:38 -07001711 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
Joseph Qia8f24f1b2016-07-26 15:21:35 -07001712 if (ret)
Tiger Yang50008632007-03-20 16:01:38 -07001713 mlog_errno(ret);
Tiger Yang50008632007-03-20 16:01:38 -07001714
Mark Fashehccd979b2005-12-15 14:31:24 -08001715bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001716 return ret;
1717}
1718
1719int ocfs2_rw_lock(struct inode *inode, int write)
1720{
1721 int status, level;
1722 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001723 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001724
Mark Fashehb06970532006-03-03 10:24:33 -08001725 mlog(0, "inode %llu take %s RW lock\n",
1726 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001727 write ? "EXMODE" : "PRMODE");
1728
Tao Mac1e8d352011-03-07 16:43:21 +08001729 if (ocfs2_mount_local(osb))
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001730 return 0;
1731
Mark Fashehccd979b2005-12-15 14:31:24 -08001732 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1733
Joel Beckerbd3e7612008-02-01 12:14:57 -08001734 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001735
1736 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1737 0);
1738 if (status < 0)
1739 mlog_errno(status);
1740
Mark Fashehccd979b2005-12-15 14:31:24 -08001741 return status;
1742}
1743
1744void ocfs2_rw_unlock(struct inode *inode, int write)
1745{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001746 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001747 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001748 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001749
Mark Fashehb06970532006-03-03 10:24:33 -08001750 mlog(0, "inode %llu drop %s RW lock\n",
1751 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001752 write ? "EXMODE" : "PRMODE");
1753
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001754 if (!ocfs2_mount_local(osb))
1755 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001756}
1757
Tiger Yang50008632007-03-20 16:01:38 -07001758/*
1759 * ocfs2_open_lock always get PR mode lock.
1760 */
1761int ocfs2_open_lock(struct inode *inode)
1762{
1763 int status = 0;
1764 struct ocfs2_lock_res *lockres;
1765 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1766
Tiger Yang50008632007-03-20 16:01:38 -07001767 mlog(0, "inode %llu take PRMODE open lock\n",
1768 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1769
Tiger Yang03efed82011-05-28 00:34:19 +08001770 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
Tiger Yang50008632007-03-20 16:01:38 -07001771 goto out;
1772
1773 lockres = &OCFS2_I(inode)->ip_open_lockres;
1774
1775 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001776 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001777 if (status < 0)
1778 mlog_errno(status);
1779
1780out:
Tiger Yang50008632007-03-20 16:01:38 -07001781 return status;
1782}
1783
1784int ocfs2_try_open_lock(struct inode *inode, int write)
1785{
1786 int status = 0, level;
1787 struct ocfs2_lock_res *lockres;
1788 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1789
Tiger Yang50008632007-03-20 16:01:38 -07001790 mlog(0, "inode %llu try to take %s open lock\n",
1791 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1792 write ? "EXMODE" : "PRMODE");
1793
Tiger Yang03efed82011-05-28 00:34:19 +08001794 if (ocfs2_is_hard_readonly(osb)) {
1795 if (write)
1796 status = -EROFS;
1797 goto out;
1798 }
1799
Tiger Yang50008632007-03-20 16:01:38 -07001800 if (ocfs2_mount_local(osb))
1801 goto out;
1802
1803 lockres = &OCFS2_I(inode)->ip_open_lockres;
1804
Joel Beckerbd3e7612008-02-01 12:14:57 -08001805 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001806
1807 /*
1808 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001809 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001810 * other nodes and the -EAGAIN will indicate to the caller that
1811 * this inode is still in use.
1812 */
1813 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001814 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001815
1816out:
Tiger Yang50008632007-03-20 16:01:38 -07001817 return status;
1818}
1819
1820/*
1821 * ocfs2_open_unlock unlock PR and EX mode open locks.
1822 */
1823void ocfs2_open_unlock(struct inode *inode)
1824{
1825 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1826 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1827
Tiger Yang50008632007-03-20 16:01:38 -07001828 mlog(0, "inode %llu drop open lock\n",
1829 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1830
1831 if (ocfs2_mount_local(osb))
1832 goto out;
1833
1834 if(lockres->l_ro_holders)
1835 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001836 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001837 if(lockres->l_ex_holders)
1838 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001839 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001840
1841out:
Tao Mac1e8d352011-03-07 16:43:21 +08001842 return;
Tiger Yang50008632007-03-20 16:01:38 -07001843}
1844
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001845static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1846 int level)
1847{
1848 int ret;
1849 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1850 unsigned long flags;
1851 struct ocfs2_mask_waiter mw;
1852
1853 ocfs2_init_mask_waiter(&mw);
1854
1855retry_cancel:
1856 spin_lock_irqsave(&lockres->l_lock, flags);
1857 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1858 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1859 if (ret) {
1860 spin_unlock_irqrestore(&lockres->l_lock, flags);
1861 ret = ocfs2_cancel_convert(osb, lockres);
1862 if (ret < 0) {
1863 mlog_errno(ret);
1864 goto out;
1865 }
1866 goto retry_cancel;
1867 }
1868 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1869 spin_unlock_irqrestore(&lockres->l_lock, flags);
1870
1871 ocfs2_wait_for_mask(&mw);
1872 goto retry_cancel;
1873 }
1874
1875 ret = -ERESTARTSYS;
1876 /*
1877 * We may still have gotten the lock, in which case there's no
1878 * point to restarting the syscall.
1879 */
1880 if (lockres->l_level == level)
1881 ret = 0;
1882
1883 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1884 lockres->l_flags, lockres->l_level, lockres->l_action);
1885
1886 spin_unlock_irqrestore(&lockres->l_lock, flags);
1887
1888out:
1889 return ret;
1890}
1891
1892/*
1893 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1894 * flock() calls. The locking approach this requires is sufficiently
1895 * different from all other cluster lock types that we implement a
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001896 * separate path to the "low-level" dlm calls. In particular:
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001897 *
1898 * - No optimization of lock levels is done - we take at exactly
1899 * what's been requested.
1900 *
1901 * - No lock caching is employed. We immediately downconvert to
1902 * no-lock at unlock time. This also means flock locks never go on
1903 * the blocking list).
1904 *
1905 * - Since userspace can trivially deadlock itself with flock, we make
1906 * sure to allow cancellation of a misbehaving applications flock()
1907 * request.
1908 *
1909 * - Access to any flock lockres doesn't require concurrency, so we
1910 * can simplify the code by requiring the caller to guarantee
1911 * serialization of dlmglue flock calls.
1912 */
1913int ocfs2_file_lock(struct file *file, int ex, int trylock)
1914{
Mark Fashehe988cf12008-07-10 09:25:39 -07001915 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1916 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001917 unsigned long flags;
1918 struct ocfs2_file_private *fp = file->private_data;
1919 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1920 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1921 struct ocfs2_mask_waiter mw;
1922
1923 ocfs2_init_mask_waiter(&mw);
1924
1925 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001926 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001927 mlog(ML_ERROR,
1928 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1929 "level: %u\n", lockres->l_name, lockres->l_flags,
1930 lockres->l_level);
1931 return -EINVAL;
1932 }
1933
1934 spin_lock_irqsave(&lockres->l_lock, flags);
1935 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1936 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1937 spin_unlock_irqrestore(&lockres->l_lock, flags);
1938
1939 /*
1940 * Get the lock at NLMODE to start - that way we
1941 * can cancel the upconvert request if need be.
1942 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001943 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001944 if (ret < 0) {
1945 mlog_errno(ret);
1946 goto out;
1947 }
1948
1949 ret = ocfs2_wait_for_mask(&mw);
1950 if (ret) {
1951 mlog_errno(ret);
1952 goto out;
1953 }
1954 spin_lock_irqsave(&lockres->l_lock, flags);
1955 }
1956
1957 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001958 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001959 lockres->l_requested = level;
1960 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1961
1962 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1963 spin_unlock_irqrestore(&lockres->l_lock, flags);
1964
Joel Becker4670c462008-02-01 14:39:35 -08001965 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Beckera796d282010-01-28 19:22:39 -08001966 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Becker7431cd72008-02-01 12:15:37 -08001967 if (ret) {
1968 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001969 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001970 ret = -EINVAL;
1971 }
1972
1973 ocfs2_recover_from_dlm_error(lockres, 1);
1974 lockres_remove_mask_waiter(lockres, &mw);
1975 goto out;
1976 }
1977
1978 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1979 if (ret == -ERESTARTSYS) {
1980 /*
1981 * Userspace can cause deadlock itself with
1982 * flock(). Current behavior locally is to allow the
1983 * deadlock, but abort the system call if a signal is
1984 * received. We follow this example, otherwise a
1985 * poorly written program could sit in kernel until
1986 * reboot.
1987 *
1988 * Handling this is a bit more complicated for Ocfs2
1989 * though. We can't exit this function with an
1990 * outstanding lock request, so a cancel convert is
1991 * required. We intentionally overwrite 'ret' - if the
1992 * cancel fails and the lock was granted, it's easier
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001993 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001994 */
1995 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001996 } else if (!ret && (level > lockres->l_level)) {
1997 /* Trylock failed asynchronously */
1998 BUG_ON(!trylock);
1999 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002000 }
2001
2002out:
2003
2004 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
2005 lockres->l_name, ex, trylock, ret);
2006 return ret;
2007}
2008
2009void ocfs2_file_unlock(struct file *file)
2010{
2011 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08002012 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002013 unsigned long flags;
2014 struct ocfs2_file_private *fp = file->private_data;
2015 struct ocfs2_lock_res *lockres = &fp->fp_flock;
2016 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
2017 struct ocfs2_mask_waiter mw;
2018
2019 ocfs2_init_mask_waiter(&mw);
2020
2021 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
2022 return;
2023
Mark Fashehe988cf12008-07-10 09:25:39 -07002024 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002025 return;
2026
2027 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
2028 lockres->l_name, lockres->l_flags, lockres->l_level,
2029 lockres->l_action);
2030
2031 spin_lock_irqsave(&lockres->l_lock, flags);
2032 /*
2033 * Fake a blocking ast for the downconvert code.
2034 */
2035 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08002036 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002037
Mark Fashehe988cf12008-07-10 09:25:39 -07002038 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002039 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2040 spin_unlock_irqrestore(&lockres->l_lock, flags);
2041
Mark Fashehe988cf12008-07-10 09:25:39 -07002042 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002043 if (ret) {
2044 mlog_errno(ret);
2045 return;
2046 }
2047
2048 ret = ocfs2_wait_for_mask(&mw);
2049 if (ret)
2050 mlog_errno(ret);
2051}
2052
Mark Fasheh34d024f2007-09-24 15:56:19 -07002053static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
2054 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002055{
2056 int kick = 0;
2057
Mark Fashehccd979b2005-12-15 14:31:24 -08002058 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07002059 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08002060 * condition. */
2061 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
2062 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002063 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08002064 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
2065 kick = 1;
2066 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002067 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08002068 if (!lockres->l_ex_holders)
2069 kick = 1;
2070 break;
2071 default:
2072 BUG();
2073 }
2074 }
2075
2076 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07002077 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002078}
2079
Mark Fashehccd979b2005-12-15 14:31:24 -08002080#define OCFS2_SEC_BITS 34
2081#define OCFS2_SEC_SHIFT (64 - 34)
2082#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2083
2084/* LVB only has room for 64 bits of time here so we pack it for
2085 * now. */
2086static u64 ocfs2_pack_timespec(struct timespec *spec)
2087{
2088 u64 res;
2089 u64 sec = spec->tv_sec;
2090 u32 nsec = spec->tv_nsec;
2091
2092 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
2093
2094 return res;
2095}
2096
2097/* Call this with the lockres locked. I am reasonably sure we don't
2098 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07002099 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002100static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2101{
2102 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002103 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002104 struct ocfs2_meta_lvb *lvb;
2105
Mark Fasheha641dc22008-12-24 16:03:48 -08002106 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002107
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002108 /*
2109 * Invalidate the LVB of a deleted inode - this way other
2110 * nodes are forced to go to disk and discover the new inode
2111 * status.
2112 */
2113 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2114 lvb->lvb_version = 0;
2115 goto out;
2116 }
2117
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07002118 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08002119 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2120 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
Eric W. Biederman03ab30f2013-01-31 16:59:23 -08002121 lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
2122 lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002123 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2124 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2125 lvb->lvb_iatime_packed =
2126 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
2127 lvb->lvb_ictime_packed =
2128 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2129 lvb->lvb_imtime_packed =
2130 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002131 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002132 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002133 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002134
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002135out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002136 mlog_meta_lvb(0, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002137}
2138
2139static void ocfs2_unpack_timespec(struct timespec *spec,
2140 u64 packed_time)
2141{
2142 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2143 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2144}
2145
2146static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2147{
2148 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002149 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002150 struct ocfs2_meta_lvb *lvb;
2151
Mark Fashehccd979b2005-12-15 14:31:24 -08002152 mlog_meta_lvb(0, lockres);
2153
Mark Fasheha641dc22008-12-24 16:03:48 -08002154 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002155
2156 /* We're safe here without the lockres lock... */
2157 spin_lock(&oi->ip_lock);
2158 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2159 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2160
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002161 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002162 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002163 ocfs2_set_inode_flags(inode);
2164
Mark Fashehccd979b2005-12-15 14:31:24 -08002165 /* fast-symlinks are a special case */
2166 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2167 inode->i_blocks = 0;
2168 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002169 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002170
Eric W. Biederman03ab30f2013-01-31 16:59:23 -08002171 i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
2172 i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
Mark Fashehccd979b2005-12-15 14:31:24 -08002173 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002174 set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
Mark Fashehccd979b2005-12-15 14:31:24 -08002175 ocfs2_unpack_timespec(&inode->i_atime,
2176 be64_to_cpu(lvb->lvb_iatime_packed));
2177 ocfs2_unpack_timespec(&inode->i_mtime,
2178 be64_to_cpu(lvb->lvb_imtime_packed));
2179 ocfs2_unpack_timespec(&inode->i_ctime,
2180 be64_to_cpu(lvb->lvb_ictime_packed));
2181 spin_unlock(&oi->ip_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08002182}
2183
Mark Fashehf9e2d822006-09-12 15:35:49 -07002184static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2185 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002186{
Mark Fasheha641dc22008-12-24 16:03:48 -08002187 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002188
Joel Becker1c520df2009-06-19 15:14:13 -07002189 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2190 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002191 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002192 return 1;
2193 return 0;
2194}
2195
2196/* Determine whether a lock resource needs to be refreshed, and
2197 * arbitrate who gets to refresh it.
2198 *
2199 * 0 means no refresh needed.
2200 *
2201 * > 0 means you need to refresh this and you MUST call
2202 * ocfs2_complete_lock_res_refresh afterwards. */
2203static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2204{
2205 unsigned long flags;
2206 int status = 0;
2207
Mark Fashehccd979b2005-12-15 14:31:24 -08002208refresh_check:
2209 spin_lock_irqsave(&lockres->l_lock, flags);
2210 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2211 spin_unlock_irqrestore(&lockres->l_lock, flags);
2212 goto bail;
2213 }
2214
2215 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2216 spin_unlock_irqrestore(&lockres->l_lock, flags);
2217
2218 ocfs2_wait_on_refreshing_lock(lockres);
2219 goto refresh_check;
2220 }
2221
2222 /* Ok, I'll be the one to refresh this lock. */
2223 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2224 spin_unlock_irqrestore(&lockres->l_lock, flags);
2225
2226 status = 1;
2227bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002228 mlog(0, "status %d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002229 return status;
2230}
2231
2232/* If status is non zero, I'll mark it as not being in refresh
2233 * anymroe, but i won't clear the needs refresh flag. */
2234static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2235 int status)
2236{
2237 unsigned long flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08002238
2239 spin_lock_irqsave(&lockres->l_lock, flags);
2240 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2241 if (!status)
2242 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2243 spin_unlock_irqrestore(&lockres->l_lock, flags);
2244
2245 wake_up(&lockres->l_event);
Mark Fashehccd979b2005-12-15 14:31:24 -08002246}
2247
2248/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002249static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002250 struct buffer_head **bh)
2251{
2252 int status = 0;
2253 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002254 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002255 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002257
Mark Fashehbe9e9862007-04-18 15:22:08 -07002258 if (ocfs2_mount_local(osb))
2259 goto bail;
2260
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 spin_lock(&oi->ip_lock);
2262 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002263 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002264 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002265 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002266 spin_unlock(&oi->ip_lock);
2267 status = -ENOENT;
2268 goto bail;
2269 }
2270 spin_unlock(&oi->ip_lock);
2271
Mark Fashehbe9e9862007-04-18 15:22:08 -07002272 if (!ocfs2_should_refresh_lock_res(lockres))
2273 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002274
2275 /* This will discard any caching information we might have had
2276 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002277 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002278
Mark Fasheh83418972007-04-23 18:53:12 -07002279 ocfs2_extent_map_trunc(inode, 0);
2280
Mark Fashehbe9e9862007-04-18 15:22:08 -07002281 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002282 mlog(0, "Trusting LVB on inode %llu\n",
2283 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002284 ocfs2_refresh_inode_from_lvb(inode);
2285 } else {
2286 /* Boo, we have to go to disk. */
2287 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002288 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002289 if (status < 0) {
2290 mlog_errno(status);
2291 goto bail_refresh;
2292 }
2293 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2294
2295 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002296 * locking an invalid object. ocfs2_read_inode_block()
2297 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002298 *
2299 * We bug on a stale inode here because we checked
2300 * above whether it was wiped from disk. The wiping
2301 * node provides a guarantee that we receive that
2302 * message and can mark the inode before dropping any
2303 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002304 mlog_bug_on_msg(inode->i_generation !=
2305 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002306 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002308 (unsigned long long)oi->ip_blkno,
2309 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002310 inode->i_generation);
2311 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2312 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002313 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2314 (unsigned long long)oi->ip_blkno,
2315 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 le32_to_cpu(fe->i_flags));
2317
2318 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002319 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002320 }
2321
2322 status = 0;
2323bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002324 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002325bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002326 return status;
2327}
2328
2329static int ocfs2_assign_bh(struct inode *inode,
2330 struct buffer_head **ret_bh,
2331 struct buffer_head *passed_bh)
2332{
2333 int status;
2334
2335 if (passed_bh) {
2336 /* Ok, the update went to disk for us, use the
2337 * returned bh. */
2338 *ret_bh = passed_bh;
2339 get_bh(*ret_bh);
2340
2341 return 0;
2342 }
2343
Joel Beckerb657c952008-11-13 14:49:11 -08002344 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002345 if (status < 0)
2346 mlog_errno(status);
2347
2348 return status;
2349}
2350
2351/*
2352 * returns < 0 error if the callback will never be called, otherwise
2353 * the result of the lock will be communicated via the callback.
2354 */
Jan Karacb257972009-06-04 15:26:50 +02002355int ocfs2_inode_lock_full_nested(struct inode *inode,
2356 struct buffer_head **ret_bh,
2357 int ex,
2358 int arg_flags,
2359 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002360{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002361 int status, level, acquired;
2362 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002363 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2365 struct buffer_head *local_bh = NULL;
2366
Mark Fashehb06970532006-03-03 10:24:33 -08002367 mlog(0, "inode %llu, take %s META lock\n",
2368 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002369 ex ? "EXMODE" : "PRMODE");
2370
2371 status = 0;
2372 acquired = 0;
2373 /* We'll allow faking a readonly metadata lock for
2374 * rodevices. */
2375 if (ocfs2_is_hard_readonly(osb)) {
2376 if (ex)
2377 status = -EROFS;
Tiger Yang03efed82011-05-28 00:34:19 +08002378 goto getbh;
Mark Fashehccd979b2005-12-15 14:31:24 -08002379 }
2380
Eric Ren2ada5922017-02-22 15:40:41 -08002381 if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
2382 ocfs2_mount_local(osb))
2383 goto update;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002384
Mark Fashehccd979b2005-12-15 14:31:24 -08002385 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002386 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002387
Mark Fashehe63aecb62007-10-18 15:30:42 -07002388 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002389 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002390 dlm_flags = 0;
2391 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002392 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002393
Jan Karacb257972009-06-04 15:26:50 +02002394 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2395 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002396 if (status < 0) {
Zach Brown41003a72013-05-07 16:18:25 -07002397 if (status != -EAGAIN)
Mark Fashehccd979b2005-12-15 14:31:24 -08002398 mlog_errno(status);
2399 goto bail;
2400 }
2401
2402 /* Notify the error cleanup path to drop the cluster lock. */
2403 acquired = 1;
2404
2405 /* We wait twice because a node may have died while we were in
2406 * the lower dlm layers. The second time though, we've
2407 * committed to owning this lock so we don't allow signals to
2408 * abort the operation. */
2409 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002410 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002411
Eric Ren2ada5922017-02-22 15:40:41 -08002412update:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002413 /*
2414 * We only see this flag if we're being called from
2415 * ocfs2_read_locked_inode(). It means we're locking an inode
2416 * which hasn't been populated yet, so clear the refresh flag
2417 * and let the caller handle it.
2418 */
2419 if (inode->i_state & I_NEW) {
2420 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002421 if (lockres)
2422 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002423 goto bail;
2424 }
2425
Mark Fashehccd979b2005-12-15 14:31:24 -08002426 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002427 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002428 * may or may not read one, depending on what's in the
2429 * LVB. The result of all of this is that we've *only* gone to
2430 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002431 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002432 if (status < 0) {
2433 if (status != -ENOENT)
2434 mlog_errno(status);
2435 goto bail;
2436 }
Tiger Yang03efed82011-05-28 00:34:19 +08002437getbh:
Mark Fashehccd979b2005-12-15 14:31:24 -08002438 if (ret_bh) {
2439 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2440 if (status < 0) {
2441 mlog_errno(status);
2442 goto bail;
2443 }
2444 }
2445
Mark Fashehccd979b2005-12-15 14:31:24 -08002446bail:
2447 if (status < 0) {
2448 if (ret_bh && (*ret_bh)) {
2449 brelse(*ret_bh);
2450 *ret_bh = NULL;
2451 }
2452 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002453 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002454 }
2455
2456 if (local_bh)
2457 brelse(local_bh);
2458
Mark Fashehccd979b2005-12-15 14:31:24 -08002459 return status;
2460}
2461
2462/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002463 * This is working around a lock inversion between tasks acquiring DLM
2464 * locks while holding a page lock and the downconvert thread which
2465 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002466 *
2467 * ** These _with_page variantes are only intended to be called from aop
2468 * methods that hold page locks and return a very specific *positive* error
2469 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2470 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002471 * The DLM is called such that it returns -EAGAIN if it would have
2472 * blocked waiting for the downconvert thread. In that case we unlock
2473 * our page so the downconvert thread can make progress. Once we've
2474 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2475 * that called us can bubble that back up into the VFS who will then
2476 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002477 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002478int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002479 struct buffer_head **ret_bh,
2480 int ex,
2481 struct page *page)
2482{
2483 int ret;
2484
Mark Fashehe63aecb62007-10-18 15:30:42 -07002485 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002486 if (ret == -EAGAIN) {
2487 unlock_page(page);
Gang He9cb16742018-01-31 16:14:48 -08002488 /*
2489 * If we can't get inode lock immediately, we should not return
2490 * directly here, since this will lead to a softlockup problem.
2491 * The method is to get a blocking lock and immediately unlock
2492 * before returning, this can avoid CPU resource waste due to
2493 * lots of retries, and benefits fairness in getting lock.
2494 */
2495 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2496 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002497 ret = AOP_TRUNCATED_PAGE;
2498 }
2499
2500 return ret;
2501}
2502
Mark Fashehe63aecb62007-10-18 15:30:42 -07002503int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002504 struct vfsmount *vfsmnt,
2505 int *level)
2506{
2507 int ret;
2508
Mark Fashehe63aecb62007-10-18 15:30:42 -07002509 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002510 if (ret < 0) {
2511 mlog_errno(ret);
2512 return ret;
2513 }
2514
2515 /*
2516 * If we should update atime, we will get EX lock,
2517 * otherwise we just get PR lock.
2518 */
2519 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2520 struct buffer_head *bh = NULL;
2521
Mark Fashehe63aecb62007-10-18 15:30:42 -07002522 ocfs2_inode_unlock(inode, 0);
2523 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002524 if (ret < 0) {
2525 mlog_errno(ret);
2526 return ret;
2527 }
2528 *level = 1;
2529 if (ocfs2_should_update_atime(inode, vfsmnt))
2530 ocfs2_update_inode_atime(inode, bh);
2531 if (bh)
2532 brelse(bh);
2533 } else
2534 *level = 0;
2535
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002536 return ret;
2537}
2538
Mark Fashehe63aecb62007-10-18 15:30:42 -07002539void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002540 int ex)
2541{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002542 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002543 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002544 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002545
Mark Fashehb06970532006-03-03 10:24:33 -08002546 mlog(0, "inode %llu drop %s META lock\n",
2547 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002548 ex ? "EXMODE" : "PRMODE");
2549
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002550 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2551 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002552 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002553}
2554
Eric Ren2ada5922017-02-22 15:40:41 -08002555/*
2556 * This _tracker variantes are introduced to deal with the recursive cluster
2557 * locking issue. The idea is to keep track of a lock holder on the stack of
2558 * the current process. If there's a lock holder on the stack, we know the
2559 * task context is already protected by cluster locking. Currently, they're
2560 * used in some VFS entry routines.
2561 *
2562 * return < 0 on error, return == 0 if there's no lock holder on the stack
2563 * before this call, return == 1 if this call would be a recursive locking.
2564 */
2565int ocfs2_inode_lock_tracker(struct inode *inode,
2566 struct buffer_head **ret_bh,
2567 int ex,
2568 struct ocfs2_lock_holder *oh)
2569{
2570 int status;
2571 int arg_flags = 0, has_locked;
2572 struct ocfs2_lock_res *lockres;
2573
2574 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2575 has_locked = ocfs2_is_locked_by_me(lockres);
2576 /* Just get buffer head if the cluster lock has been taken */
2577 if (has_locked)
2578 arg_flags = OCFS2_META_LOCK_GETBH;
2579
2580 if (likely(!has_locked || ret_bh)) {
2581 status = ocfs2_inode_lock_full(inode, ret_bh, ex, arg_flags);
2582 if (status < 0) {
2583 if (status != -ENOENT)
2584 mlog_errno(status);
2585 return status;
2586 }
2587 }
2588 if (!has_locked)
2589 ocfs2_add_holder(lockres, oh);
2590
2591 return has_locked;
2592}
2593
2594void ocfs2_inode_unlock_tracker(struct inode *inode,
2595 int ex,
2596 struct ocfs2_lock_holder *oh,
2597 int had_lock)
2598{
2599 struct ocfs2_lock_res *lockres;
2600
2601 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Eric Ren855bf142017-06-23 15:08:55 -07002602 /* had_lock means that the currect process already takes the cluster
2603 * lock previously. If had_lock is 1, we have nothing to do here, and
2604 * it will get unlocked where we got the lock.
2605 */
Eric Ren2ada5922017-02-22 15:40:41 -08002606 if (!had_lock) {
2607 ocfs2_remove_holder(lockres, oh);
2608 ocfs2_inode_unlock(inode, ex);
2609 }
2610}
2611
Sunil Mushrandf152c22009-06-22 11:40:07 -07002612int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002613{
2614 struct ocfs2_lock_res *lockres;
2615 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002616 int status = 0;
2617
Sunil Mushrandf152c22009-06-22 11:40:07 -07002618 if (ocfs2_is_hard_readonly(osb))
2619 return -EROFS;
2620
2621 if (ocfs2_mount_local(osb))
2622 return 0;
2623
Srinivas Eeda83273932009-06-03 17:02:55 -07002624 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002625 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002626 if (status < 0)
2627 return status;
2628
2629 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002630 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2631 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002632 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002633 else
2634 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2635
Srinivas Eeda83273932009-06-03 17:02:55 -07002636 return status;
2637}
2638
Sunil Mushrandf152c22009-06-22 11:40:07 -07002639void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002640{
2641 struct ocfs2_lock_res *lockres;
2642 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002643
Sunil Mushrandf152c22009-06-22 11:40:07 -07002644 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2645 lockres = &osb->osb_orphan_scan.os_lockres;
2646 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2647 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2648 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2649 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2650 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002651}
2652
Mark Fashehccd979b2005-12-15 14:31:24 -08002653int ocfs2_super_lock(struct ocfs2_super *osb,
2654 int ex)
2655{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002656 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002657 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002658 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002659
Mark Fashehccd979b2005-12-15 14:31:24 -08002660 if (ocfs2_is_hard_readonly(osb))
2661 return -EROFS;
2662
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002663 if (ocfs2_mount_local(osb))
2664 goto bail;
2665
Mark Fashehccd979b2005-12-15 14:31:24 -08002666 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2667 if (status < 0) {
2668 mlog_errno(status);
2669 goto bail;
2670 }
2671
2672 /* The super block lock path is really in the best position to
2673 * know when resources covered by the lock need to be
2674 * refreshed, so we do it here. Of course, making sense of
2675 * everything is up to the caller :) */
2676 status = ocfs2_should_refresh_lock_res(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002677 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002678 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002679
2680 ocfs2_complete_lock_res_refresh(lockres, status);
2681
Junxiao Bi3278bb72013-02-21 16:42:45 -08002682 if (status < 0) {
2683 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002684 mlog_errno(status);
Junxiao Bi3278bb72013-02-21 16:42:45 -08002685 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002686 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002687 }
2688bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002689 return status;
2690}
2691
2692void ocfs2_super_unlock(struct ocfs2_super *osb,
2693 int ex)
2694{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002695 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002696 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2697
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002698 if (!ocfs2_mount_local(osb))
2699 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002700}
2701
2702int ocfs2_rename_lock(struct ocfs2_super *osb)
2703{
2704 int status;
2705 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2706
2707 if (ocfs2_is_hard_readonly(osb))
2708 return -EROFS;
2709
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002710 if (ocfs2_mount_local(osb))
2711 return 0;
2712
Joel Beckerbd3e7612008-02-01 12:14:57 -08002713 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002714 if (status < 0)
2715 mlog_errno(status);
2716
2717 return status;
2718}
2719
2720void ocfs2_rename_unlock(struct ocfs2_super *osb)
2721{
2722 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2723
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002724 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002725 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002726}
2727
wengang wang6ca497a2009-03-06 21:29:10 +08002728int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2729{
2730 int status;
2731 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2732
2733 if (ocfs2_is_hard_readonly(osb))
2734 return -EROFS;
2735
2736 if (ocfs2_mount_local(osb))
2737 return 0;
2738
2739 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2740 0, 0);
2741 if (status < 0)
2742 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2743
2744 return status;
2745}
2746
2747void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2748{
2749 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2750
2751 if (!ocfs2_mount_local(osb))
2752 ocfs2_cluster_unlock(osb, lockres,
2753 ex ? LKM_EXMODE : LKM_PRMODE);
2754}
2755
Mark Fashehd680efe2006-09-08 14:14:34 -07002756int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2757{
2758 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002759 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002760 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2761 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2762
2763 BUG_ON(!dl);
2764
Tiger Yang03efed82011-05-28 00:34:19 +08002765 if (ocfs2_is_hard_readonly(osb)) {
2766 if (ex)
2767 return -EROFS;
2768 return 0;
2769 }
Mark Fashehd680efe2006-09-08 14:14:34 -07002770
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002771 if (ocfs2_mount_local(osb))
2772 return 0;
2773
Mark Fashehd680efe2006-09-08 14:14:34 -07002774 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2775 if (ret < 0)
2776 mlog_errno(ret);
2777
2778 return ret;
2779}
2780
2781void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2782{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002783 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002784 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2785 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2786
Tiger Yang03efed82011-05-28 00:34:19 +08002787 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002788 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002789}
2790
Mark Fashehccd979b2005-12-15 14:31:24 -08002791/* Reference counting of the dlm debug structure. We want this because
2792 * open references on the debug inodes can live on after a mount, so
2793 * we can't rely on the ocfs2_super to always exist. */
2794static void ocfs2_dlm_debug_free(struct kref *kref)
2795{
2796 struct ocfs2_dlm_debug *dlm_debug;
2797
2798 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2799
2800 kfree(dlm_debug);
2801}
2802
2803void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2804{
2805 if (dlm_debug)
2806 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2807}
2808
2809static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2810{
2811 kref_get(&debug->d_refcnt);
2812}
2813
2814struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2815{
2816 struct ocfs2_dlm_debug *dlm_debug;
2817
2818 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2819 if (!dlm_debug) {
2820 mlog_errno(-ENOMEM);
2821 goto out;
2822 }
2823
2824 kref_init(&dlm_debug->d_refcnt);
2825 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2826 dlm_debug->d_locking_state = NULL;
2827out:
2828 return dlm_debug;
2829}
2830
2831/* Access to this is arbitrated for us via seq_file->sem. */
2832struct ocfs2_dlm_seq_priv {
2833 struct ocfs2_dlm_debug *p_dlm_debug;
2834 struct ocfs2_lock_res p_iter_res;
2835 struct ocfs2_lock_res p_tmp_res;
2836};
2837
2838static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2839 struct ocfs2_dlm_seq_priv *priv)
2840{
2841 struct ocfs2_lock_res *iter, *ret = NULL;
2842 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2843
2844 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2845
2846 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2847 /* discover the head of the list */
2848 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2849 mlog(0, "End of list found, %p\n", ret);
2850 break;
2851 }
2852
2853 /* We track our "dummy" iteration lockres' by a NULL
2854 * l_ops field. */
2855 if (iter->l_ops != NULL) {
2856 ret = iter;
2857 break;
2858 }
2859 }
2860
2861 return ret;
2862}
2863
2864static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2865{
2866 struct ocfs2_dlm_seq_priv *priv = m->private;
2867 struct ocfs2_lock_res *iter;
2868
2869 spin_lock(&ocfs2_dlm_tracking_lock);
2870 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2871 if (iter) {
2872 /* Since lockres' have the lifetime of their container
2873 * (which can be inodes, ocfs2_supers, etc) we want to
2874 * copy this out to a temporary lockres while still
2875 * under the spinlock. Obviously after this we can't
2876 * trust any pointers on the copy returned, but that's
2877 * ok as the information we want isn't typically held
2878 * in them. */
2879 priv->p_tmp_res = *iter;
2880 iter = &priv->p_tmp_res;
2881 }
2882 spin_unlock(&ocfs2_dlm_tracking_lock);
2883
2884 return iter;
2885}
2886
2887static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2888{
2889}
2890
2891static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2892{
2893 struct ocfs2_dlm_seq_priv *priv = m->private;
2894 struct ocfs2_lock_res *iter = v;
2895 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2896
2897 spin_lock(&ocfs2_dlm_tracking_lock);
2898 iter = ocfs2_dlm_next_res(iter, priv);
2899 list_del_init(&dummy->l_debug_list);
2900 if (iter) {
2901 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2902 priv->p_tmp_res = *iter;
2903 iter = &priv->p_tmp_res;
2904 }
2905 spin_unlock(&ocfs2_dlm_tracking_lock);
2906
2907 return iter;
2908}
2909
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002910/*
2911 * Version is used by debugfs.ocfs2 to determine the format being used
2912 *
2913 * New in version 2
2914 * - Lock stats printed
2915 * New in version 3
2916 * - Max time in lock stats is in usecs (instead of nsecs)
2917 */
2918#define OCFS2_DLM_DEBUG_STR_VERSION 3
Mark Fashehccd979b2005-12-15 14:31:24 -08002919static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2920{
2921 int i;
2922 char *lvb;
2923 struct ocfs2_lock_res *lockres = v;
2924
2925 if (!lockres)
2926 return -EINVAL;
2927
Mark Fashehd680efe2006-09-08 14:14:34 -07002928 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2929
2930 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2931 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2932 lockres->l_name,
2933 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2934 else
2935 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2936
2937 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002938 "0x%lx\t"
2939 "0x%x\t"
2940 "0x%x\t"
2941 "%u\t"
2942 "%u\t"
2943 "%d\t"
2944 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002945 lockres->l_level,
2946 lockres->l_flags,
2947 lockres->l_action,
2948 lockres->l_unlock_action,
2949 lockres->l_ro_holders,
2950 lockres->l_ex_holders,
2951 lockres->l_requested,
2952 lockres->l_blocking);
2953
2954 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002955 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002956 for(i = 0; i < DLM_LVB_LEN; i++)
2957 seq_printf(m, "0x%x\t", lvb[i]);
2958
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002959#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002960# define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
2961# define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
2962# define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
2963# define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
2964# define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
2965# define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
2966# define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
2967# define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
2968# define lock_refresh(_l) ((_l)->l_lock_refresh)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002969#else
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002970# define lock_num_prmode(_l) (0)
2971# define lock_num_exmode(_l) (0)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002972# define lock_num_prmode_failed(_l) (0)
2973# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002974# define lock_total_prmode(_l) (0ULL)
2975# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002976# define lock_max_prmode(_l) (0)
2977# define lock_max_exmode(_l) (0)
2978# define lock_refresh(_l) (0)
2979#endif
2980 /* The following seq_print was added in version 2 of this output */
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002981 seq_printf(m, "%u\t"
2982 "%u\t"
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002983 "%u\t"
2984 "%u\t"
2985 "%llu\t"
2986 "%llu\t"
2987 "%u\t"
2988 "%u\t"
2989 "%u\t",
2990 lock_num_prmode(lockres),
2991 lock_num_exmode(lockres),
2992 lock_num_prmode_failed(lockres),
2993 lock_num_exmode_failed(lockres),
2994 lock_total_prmode(lockres),
2995 lock_total_exmode(lockres),
2996 lock_max_prmode(lockres),
2997 lock_max_exmode(lockres),
2998 lock_refresh(lockres));
2999
Mark Fashehccd979b2005-12-15 14:31:24 -08003000 /* End the line */
3001 seq_printf(m, "\n");
3002 return 0;
3003}
3004
Jan Engelhardt90d99772008-01-22 20:52:20 +01003005static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08003006 .start = ocfs2_dlm_seq_start,
3007 .stop = ocfs2_dlm_seq_stop,
3008 .next = ocfs2_dlm_seq_next,
3009 .show = ocfs2_dlm_seq_show,
3010};
3011
3012static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
3013{
Joe Perches33fa1d92010-07-12 13:50:19 -07003014 struct seq_file *seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08003015 struct ocfs2_dlm_seq_priv *priv = seq->private;
3016 struct ocfs2_lock_res *res = &priv->p_iter_res;
3017
3018 ocfs2_remove_lockres_tracking(res);
3019 ocfs2_put_dlm_debug(priv->p_dlm_debug);
3020 return seq_release_private(inode, file);
3021}
3022
3023static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
3024{
Mark Fashehccd979b2005-12-15 14:31:24 -08003025 struct ocfs2_dlm_seq_priv *priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08003026 struct ocfs2_super *osb;
3027
Rob Jones1848cb52014-10-09 15:25:09 -07003028 priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
Mark Fashehccd979b2005-12-15 14:31:24 -08003029 if (!priv) {
Rob Jones1848cb52014-10-09 15:25:09 -07003030 mlog_errno(-ENOMEM);
3031 return -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -08003032 }
Rob Jones1848cb52014-10-09 15:25:09 -07003033
Theodore Ts'o8e18e292006-09-27 01:50:46 -07003034 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08003035 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
3036 priv->p_dlm_debug = osb->osb_dlm_debug;
3037 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
3038
Mark Fashehccd979b2005-12-15 14:31:24 -08003039 ocfs2_add_lockres_tracking(&priv->p_iter_res,
3040 priv->p_dlm_debug);
3041
Rob Jones1848cb52014-10-09 15:25:09 -07003042 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003043}
3044
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003045static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08003046 .open = ocfs2_dlm_debug_open,
3047 .release = ocfs2_dlm_debug_release,
3048 .read = seq_read,
3049 .llseek = seq_lseek,
3050};
3051
3052static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
3053{
3054 int ret = 0;
3055 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3056
3057 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
3058 S_IFREG|S_IRUSR,
3059 osb->osb_debug_root,
3060 osb,
3061 &ocfs2_dlm_debug_fops);
Linus Torvalds8f443e22015-04-21 09:17:28 -07003062 if (!dlm_debug->d_locking_state) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003063 ret = -EINVAL;
3064 mlog(ML_ERROR,
3065 "Unable to create locking state debugfs file.\n");
3066 goto out;
3067 }
3068
3069 ocfs2_get_dlm_debug(dlm_debug);
3070out:
3071 return ret;
3072}
3073
3074static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
3075{
3076 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3077
3078 if (dlm_debug) {
3079 debugfs_remove(dlm_debug->d_locking_state);
3080 ocfs2_put_dlm_debug(dlm_debug);
3081 }
3082}
3083
3084int ocfs2_dlm_init(struct ocfs2_super *osb)
3085{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003086 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08003087 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003088
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003089 if (ocfs2_mount_local(osb)) {
3090 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003091 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003092 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003093
Mark Fashehccd979b2005-12-15 14:31:24 -08003094 status = ocfs2_dlm_init_debug(osb);
3095 if (status < 0) {
3096 mlog_errno(status);
3097 goto bail;
3098 }
3099
Mark Fasheh34d024f2007-09-24 15:56:19 -07003100 /* launch downconvert thread */
Joseph Qi5afc44e2015-11-05 18:44:13 -08003101 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
3102 osb->uuid_str);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003103 if (IS_ERR(osb->dc_task)) {
3104 status = PTR_ERR(osb->dc_task);
3105 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003106 mlog_errno(status);
3107 goto bail;
3108 }
3109
Mark Fashehccd979b2005-12-15 14:31:24 -08003110 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08003111 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
Goldwyn Rodriguesc74a3bd2014-01-21 15:48:21 -08003112 osb->osb_cluster_name,
3113 strlen(osb->osb_cluster_name),
Joel Becker9c6c8772008-02-01 15:17:30 -08003114 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08003115 strlen(osb->uuid_str),
Joel Becker553b5eb2010-01-29 17:19:06 -08003116 &lproto, ocfs2_do_node_down, osb,
Joel Becker4670c462008-02-01 14:39:35 -08003117 &conn);
3118 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003119 mlog_errno(status);
3120 goto bail;
3121 }
3122
Goldwyn Rodrigues3e834152014-01-21 15:48:24 -08003123 status = ocfs2_cluster_this_node(conn, &osb->node_num);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003124 if (status < 0) {
3125 mlog_errno(status);
3126 mlog(ML_ERROR,
3127 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08003128 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003129 goto bail;
3130 }
3131
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003132local:
Mark Fashehccd979b2005-12-15 14:31:24 -08003133 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3134 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08003135 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07003136 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003137
Joel Becker4670c462008-02-01 14:39:35 -08003138 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08003139bail:
3140 if (status < 0) {
3141 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003142 if (osb->dc_task)
3143 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08003144 }
3145
Mark Fashehccd979b2005-12-15 14:31:24 -08003146 return status;
3147}
3148
Joel Becker286eaa92008-02-01 15:03:57 -08003149void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3150 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08003151{
Mark Fashehccd979b2005-12-15 14:31:24 -08003152 ocfs2_drop_osb_locks(osb);
3153
Joel Becker4670c462008-02-01 14:39:35 -08003154 /*
3155 * Now that we have dropped all locks and ocfs2_dismount_volume()
3156 * has disabled recovery, the DLM won't be talking to us. It's
3157 * safe to tear things down before disconnecting the cluster.
3158 */
3159
Mark Fasheh34d024f2007-09-24 15:56:19 -07003160 if (osb->dc_task) {
3161 kthread_stop(osb->dc_task);
3162 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003163 }
3164
3165 ocfs2_lock_res_free(&osb->osb_super_lockres);
3166 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003167 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003168 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003169
Joel Becker286eaa92008-02-01 15:03:57 -08003170 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003171 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003172
3173 ocfs2_dlm_shutdown_debug(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003174}
3175
Mark Fashehccd979b2005-12-15 14:31:24 -08003176static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003177 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003178{
Joel Becker7431cd72008-02-01 12:15:37 -08003179 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003180 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003181 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003182
3183 /* We didn't get anywhere near actually using this lockres. */
3184 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3185 goto out;
3186
Mark Fashehb80fc012006-09-12 22:08:14 -07003187 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003188 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003189
Mark Fashehccd979b2005-12-15 14:31:24 -08003190 spin_lock_irqsave(&lockres->l_lock, flags);
3191
3192 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3193 "lockres %s, flags 0x%lx\n",
3194 lockres->l_name, lockres->l_flags);
3195
3196 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3197 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3198 "%u, unlock_action = %u\n",
3199 lockres->l_name, lockres->l_flags, lockres->l_action,
3200 lockres->l_unlock_action);
3201
3202 spin_unlock_irqrestore(&lockres->l_lock, flags);
3203
3204 /* XXX: Today we just wait on any busy
3205 * locks... Perhaps we need to cancel converts in the
3206 * future? */
3207 ocfs2_wait_on_busy_lock(lockres);
3208
3209 spin_lock_irqsave(&lockres->l_lock, flags);
3210 }
3211
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003212 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3213 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003214 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003215 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3216 lockres->l_ops->set_lvb(lockres);
3217 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003218
3219 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3220 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3221 lockres->l_name);
3222 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3223 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3224
3225 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3226 spin_unlock_irqrestore(&lockres->l_lock, flags);
3227 goto out;
3228 }
3229
3230 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3231
3232 /* make sure we never get here while waiting for an ast to
3233 * fire. */
3234 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3235
3236 /* is this necessary? */
3237 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3238 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3239 spin_unlock_irqrestore(&lockres->l_lock, flags);
3240
3241 mlog(0, "lock %s\n", lockres->l_name);
3242
Joel Beckera796d282010-01-28 19:22:39 -08003243 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
Joel Becker7431cd72008-02-01 12:15:37 -08003244 if (ret) {
3245 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003246 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003247 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003248 BUG();
3249 }
Coly Li73ac36e2009-01-07 18:09:16 -08003250 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003251 lockres->l_name);
3252
3253 ocfs2_wait_on_busy_lock(lockres);
3254out:
Mark Fashehccd979b2005-12-15 14:31:24 -08003255 return 0;
3256}
3257
Jan Kara84d86f82014-04-03 14:46:57 -07003258static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3259 struct ocfs2_lock_res *lockres);
3260
Mark Fashehccd979b2005-12-15 14:31:24 -08003261/* Mark the lockres as being dropped. It will no longer be
3262 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003263 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003264 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003265 *
3266 * You can *not* attempt to call cluster_lock on this lockres anymore. */
Jan Kara84d86f82014-04-03 14:46:57 -07003267void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
3268 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003269{
3270 int status;
3271 struct ocfs2_mask_waiter mw;
Jan Kara84d86f82014-04-03 14:46:57 -07003272 unsigned long flags, flags2;
Mark Fashehccd979b2005-12-15 14:31:24 -08003273
3274 ocfs2_init_mask_waiter(&mw);
3275
3276 spin_lock_irqsave(&lockres->l_lock, flags);
3277 lockres->l_flags |= OCFS2_LOCK_FREEING;
Jan Kara84d86f82014-04-03 14:46:57 -07003278 if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
3279 /*
3280 * We know the downconvert is queued but not in progress
3281 * because we are the downconvert thread and processing
3282 * different lock. So we can just remove the lock from the
3283 * queue. This is not only an optimization but also a way
3284 * to avoid the following deadlock:
3285 * ocfs2_dentry_post_unlock()
3286 * ocfs2_dentry_lock_put()
3287 * ocfs2_drop_dentry_lock()
3288 * iput()
3289 * ocfs2_evict_inode()
3290 * ocfs2_clear_inode()
3291 * ocfs2_mark_lockres_freeing()
3292 * ... blocks waiting for OCFS2_LOCK_QUEUED
3293 * since we are the downconvert thread which
3294 * should clear the flag.
3295 */
3296 spin_unlock_irqrestore(&lockres->l_lock, flags);
3297 spin_lock_irqsave(&osb->dc_task_lock, flags2);
3298 list_del_init(&lockres->l_blocked_list);
3299 osb->blocked_lock_count--;
3300 spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
3301 /*
3302 * Warn if we recurse into another post_unlock call. Strictly
3303 * speaking it isn't a problem but we need to be careful if
3304 * that happens (stack overflow, deadlocks, ...) so warn if
3305 * ocfs2 grows a path for which this can happen.
3306 */
3307 WARN_ON_ONCE(lockres->l_ops->post_unlock);
3308 /* Since the lock is freeing we don't do much in the fn below */
3309 ocfs2_process_blocked_lock(osb, lockres);
3310 return;
3311 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003312 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3313 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3314 spin_unlock_irqrestore(&lockres->l_lock, flags);
3315
3316 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3317
3318 status = ocfs2_wait_for_mask(&mw);
3319 if (status)
3320 mlog_errno(status);
3321
3322 spin_lock_irqsave(&lockres->l_lock, flags);
3323 }
3324 spin_unlock_irqrestore(&lockres->l_lock, flags);
3325}
3326
Mark Fashehd680efe2006-09-08 14:14:34 -07003327void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3328 struct ocfs2_lock_res *lockres)
3329{
3330 int ret;
3331
Jan Kara84d86f82014-04-03 14:46:57 -07003332 ocfs2_mark_lockres_freeing(osb, lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003333 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003334 if (ret)
3335 mlog_errno(ret);
3336}
3337
Mark Fashehccd979b2005-12-15 14:31:24 -08003338static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3339{
Mark Fashehd680efe2006-09-08 14:14:34 -07003340 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3341 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003342 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003343 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003344}
3345
Mark Fashehccd979b2005-12-15 14:31:24 -08003346int ocfs2_drop_inode_locks(struct inode *inode)
3347{
3348 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003349
Mark Fashehccd979b2005-12-15 14:31:24 -08003350 /* No need to call ocfs2_mark_lockres_freeing here -
3351 * ocfs2_clear_inode has done it for us. */
3352
3353 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003354 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003355 if (err < 0)
3356 mlog_errno(err);
3357
3358 status = err;
3359
3360 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003361 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003362 if (err < 0)
3363 mlog_errno(err);
3364 if (err < 0 && !status)
3365 status = err;
3366
3367 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003368 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003369 if (err < 0)
3370 mlog_errno(err);
3371 if (err < 0 && !status)
3372 status = err;
3373
Mark Fashehccd979b2005-12-15 14:31:24 -08003374 return status;
3375}
3376
Joel Beckerde551242008-02-01 14:45:08 -08003377static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3378 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003379{
3380 assert_spin_locked(&lockres->l_lock);
3381
Joel Beckerbd3e7612008-02-01 12:14:57 -08003382 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003383
3384 if (lockres->l_level <= new_level) {
Sunil Mushran9b915182010-02-26 19:42:44 -08003385 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3386 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3387 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3388 new_level, list_empty(&lockres->l_blocked_list),
3389 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3390 lockres->l_flags, lockres->l_ro_holders,
3391 lockres->l_ex_holders, lockres->l_action,
3392 lockres->l_unlock_action, lockres->l_requested,
3393 lockres->l_blocking, lockres->l_pending_gen);
Mark Fashehccd979b2005-12-15 14:31:24 -08003394 BUG();
3395 }
3396
Sunil Mushran9b915182010-02-26 19:42:44 -08003397 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3398 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
Mark Fashehccd979b2005-12-15 14:31:24 -08003399
3400 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3401 lockres->l_requested = new_level;
3402 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003403 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003404}
3405
3406static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3407 struct ocfs2_lock_res *lockres,
3408 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003409 int lvb,
3410 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003411{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003412 int ret;
3413 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003414
Sunil Mushran9b915182010-02-26 19:42:44 -08003415 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3416 lockres->l_level, new_level);
3417
Eric Ren6c9bd812017-01-10 16:57:33 -08003418 /*
3419 * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
3420 * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
3421 * we can recover correctly from node failure. Otherwise, we may get
3422 * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
3423 */
3424 if (!ocfs2_is_o2cb_active() &&
3425 lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3426 lvb = 1;
3427
Mark Fashehccd979b2005-12-15 14:31:24 -08003428 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003429 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003430
Joel Becker4670c462008-02-01 14:39:35 -08003431 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003432 new_level,
3433 &lockres->l_lksb,
3434 dlm_flags,
3435 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08003436 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08003437 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003438 if (ret) {
3439 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003440 ocfs2_recover_from_dlm_error(lockres, 1);
3441 goto bail;
3442 }
3443
3444 ret = 0;
3445bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08003446 return ret;
3447}
3448
Joel Becker24ef1812008-01-29 17:37:32 -08003449/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003450static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3451 struct ocfs2_lock_res *lockres)
3452{
3453 assert_spin_locked(&lockres->l_lock);
3454
Mark Fashehccd979b2005-12-15 14:31:24 -08003455 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3456 /* If we're already trying to cancel a lock conversion
3457 * then just drop the spinlock and allow the caller to
3458 * requeue this lock. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003459 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003460 return 0;
3461 }
3462
3463 /* were we in a convert when we got the bast fire? */
3464 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3465 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3466 /* set things up for the unlockast to know to just
3467 * clear out the ast_action and unset busy, etc. */
3468 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3469
3470 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3471 "lock %s, invalid flags: 0x%lx\n",
3472 lockres->l_name, lockres->l_flags);
3473
Sunil Mushran9b915182010-02-26 19:42:44 -08003474 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3475
Mark Fashehccd979b2005-12-15 14:31:24 -08003476 return 1;
3477}
3478
3479static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3480 struct ocfs2_lock_res *lockres)
3481{
3482 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003483
Joel Becker4670c462008-02-01 14:39:35 -08003484 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Beckera796d282010-01-28 19:22:39 -08003485 DLM_LKF_CANCEL);
Joel Becker7431cd72008-02-01 12:15:37 -08003486 if (ret) {
3487 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003488 ocfs2_recover_from_dlm_error(lockres, 0);
3489 }
3490
Sunil Mushran9b915182010-02-26 19:42:44 -08003491 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003492
Mark Fashehccd979b2005-12-15 14:31:24 -08003493 return ret;
3494}
3495
Mark Fashehb5e500e2006-09-13 22:01:16 -07003496static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3497 struct ocfs2_lock_res *lockres,
3498 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003499{
3500 unsigned long flags;
3501 int blocking;
3502 int new_level;
Sunil Mushran079b8052010-02-03 10:16:54 -08003503 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003504 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003505 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003506 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003507
Mark Fashehccd979b2005-12-15 14:31:24 -08003508 spin_lock_irqsave(&lockres->l_lock, flags);
3509
Mark Fashehccd979b2005-12-15 14:31:24 -08003510recheck:
Sunil Mushrandb0f6ce2010-02-01 16:55:50 -08003511 /*
3512 * Is it still blocking? If not, we have no more work to do.
3513 */
3514 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3515 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3516 spin_unlock_irqrestore(&lockres->l_lock, flags);
3517 ret = 0;
3518 goto leave;
3519 }
3520
Mark Fashehccd979b2005-12-15 14:31:24 -08003521 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003522 /* XXX
3523 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3524 * exists entirely for one reason - another thread has set
3525 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3526 *
3527 * If we do ocfs2_cancel_convert() before the other thread
3528 * calls dlm_lock(), our cancel will do nothing. We will
3529 * get no ast, and we will have no way of knowing the
3530 * cancel failed. Meanwhile, the other thread will call
3531 * into dlm_lock() and wait...forever.
3532 *
3533 * Why forever? Because another node has asked for the
3534 * lock first; that's why we're here in unblock_lock().
3535 *
3536 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3537 * set, we just requeue the unblock. Only when the other
3538 * thread has called dlm_lock() and cleared PENDING will
3539 * we then cancel their request.
3540 *
3541 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3542 * at the same time they set OCFS2_DLM_BUSY. They must
3543 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3544 */
Sunil Mushran9b915182010-02-26 19:42:44 -08003545 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3546 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3547 lockres->l_name);
Joel Beckerde551242008-02-01 14:45:08 -08003548 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003549 }
Joel Beckerde551242008-02-01 14:45:08 -08003550
Mark Fashehd680efe2006-09-08 14:14:34 -07003551 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003552 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3553 spin_unlock_irqrestore(&lockres->l_lock, flags);
3554 if (ret) {
3555 ret = ocfs2_cancel_convert(osb, lockres);
3556 if (ret < 0)
3557 mlog_errno(ret);
3558 }
3559 goto leave;
3560 }
3561
Sunil Mushrana1912822010-01-21 10:50:03 -08003562 /*
3563 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3564 * set when the ast is received for an upconvert just before the
3565 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3566 * on the heels of the ast, we want to delay the downconvert just
3567 * enough to allow the up requestor to do its task. Because this
3568 * lock is in the blocked queue, the lock will be downconverted
3569 * as soon as the requestor is done with the lock.
3570 */
3571 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3572 goto leave_requeue;
3573
Sunil Mushran0d741252010-01-29 09:44:11 -08003574 /*
3575 * How can we block and yet be at NL? We were trying to upconvert
3576 * from NL and got canceled. The code comes back here, and now
3577 * we notice and clear BLOCKING.
3578 */
3579 if (lockres->l_level == DLM_LOCK_NL) {
3580 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
Sunil Mushran9b915182010-02-26 19:42:44 -08003581 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
Sunil Mushran0d741252010-01-29 09:44:11 -08003582 lockres->l_blocking = DLM_LOCK_NL;
3583 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3584 spin_unlock_irqrestore(&lockres->l_lock, flags);
3585 goto leave;
3586 }
3587
Mark Fashehccd979b2005-12-15 14:31:24 -08003588 /* if we're blocking an exclusive and we have *any* holders,
3589 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003590 if ((lockres->l_blocking == DLM_LOCK_EX)
Sunil Mushran9b915182010-02-26 19:42:44 -08003591 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3592 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3593 lockres->l_name, lockres->l_ex_holders,
3594 lockres->l_ro_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003595 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003596 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003597
3598 /* If it's a PR we're blocking, then only
3599 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003600 if (lockres->l_blocking == DLM_LOCK_PR &&
Sunil Mushran9b915182010-02-26 19:42:44 -08003601 lockres->l_ex_holders) {
3602 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3603 lockres->l_name, lockres->l_ex_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003604 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003605 }
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003606
3607 /*
3608 * Can we get a lock in this state if the holder counts are
3609 * zero? The meta data unblock code used to check this.
3610 */
3611 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Sunil Mushran9b915182010-02-26 19:42:44 -08003612 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3613 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3614 lockres->l_name);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003615 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003616 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003617
Mark Fasheh16d5b952006-09-13 21:10:12 -07003618 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3619
3620 if (lockres->l_ops->check_downconvert
Sunil Mushran9b915182010-02-26 19:42:44 -08003621 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3622 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3623 lockres->l_name);
Mark Fasheh16d5b952006-09-13 21:10:12 -07003624 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003625 }
Mark Fasheh16d5b952006-09-13 21:10:12 -07003626
Mark Fashehccd979b2005-12-15 14:31:24 -08003627 /* If we get here, then we know that there are no more
3628 * incompatible holders (and anyone asking for an incompatible
3629 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003630 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003631 goto downconvert;
3632
3633 /* Some lockres types want to do a bit of work before
3634 * downconverting a lock. Allow that here. The worker function
3635 * may sleep, so we save off a copy of what we're blocking as
3636 * it may change while we're not holding the spin lock. */
3637 blocking = lockres->l_blocking;
Sunil Mushran079b8052010-02-03 10:16:54 -08003638 level = lockres->l_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003639 spin_unlock_irqrestore(&lockres->l_lock, flags);
3640
Mark Fashehcc567d82006-09-13 21:52:21 -07003641 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003642
Sunil Mushran9b915182010-02-26 19:42:44 -08003643 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3644 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3645 lockres->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -07003646 goto leave;
Sunil Mushran9b915182010-02-26 19:42:44 -08003647 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003648
3649 spin_lock_irqsave(&lockres->l_lock, flags);
Sunil Mushran079b8052010-02-03 10:16:54 -08003650 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003651 /* If this changed underneath us, then we can't drop
3652 * it just yet. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003653 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3654 "Recheck\n", lockres->l_name, blocking,
3655 lockres->l_blocking, level, lockres->l_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003656 goto recheck;
3657 }
3658
3659downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003660 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003661
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003662 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003663 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003664 set_lvb = 1;
3665
3666 /*
3667 * We only set the lvb if the lock has been fully
3668 * refreshed - otherwise we risk setting stale
3669 * data. Otherwise, there's no need to actually clear
3670 * out the lvb here as it's value is still valid.
3671 */
3672 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3673 lockres->l_ops->set_lvb(lockres);
3674 }
3675
Joel Beckerde551242008-02-01 14:45:08 -08003676 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003677 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003678 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3679 gen);
3680
Mark Fashehccd979b2005-12-15 14:31:24 -08003681leave:
Tao Mac1e8d352011-03-07 16:43:21 +08003682 if (ret)
3683 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08003684 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003685
3686leave_requeue:
3687 spin_unlock_irqrestore(&lockres->l_lock, flags);
3688 ctl->requeue = 1;
3689
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003690 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003691}
3692
Mark Fashehd680efe2006-09-08 14:14:34 -07003693static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3694 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003695{
3696 struct inode *inode;
3697 struct address_space *mapping;
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003698 struct ocfs2_inode_info *oi;
Mark Fashehccd979b2005-12-15 14:31:24 -08003699
Mark Fashehccd979b2005-12-15 14:31:24 -08003700 inode = ocfs2_lock_res_inode(lockres);
3701 mapping = inode->i_mapping;
3702
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003703 if (S_ISDIR(inode->i_mode)) {
3704 oi = OCFS2_I(inode);
3705 oi->ip_dir_lock_gen++;
3706 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3707 goto out;
3708 }
3709
Mark Fasheh1044e402008-02-28 17:16:03 -08003710 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003711 goto out;
3712
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003713 /*
3714 * We need this before the filemap_fdatawrite() so that it can
3715 * transfer the dirty bit from the PTE to the
3716 * page. Unfortunately this means that even for EX->PR
3717 * downconverts, we'll lose our mappings and have to build
3718 * them up again.
3719 */
3720 unmap_mapping_range(mapping, 0, 0, 0);
3721
Mark Fashehccd979b2005-12-15 14:31:24 -08003722 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003723 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3724 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003725 }
3726 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003727 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003728 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003729 } else {
3730 /* We only need to wait on the I/O if we're not also
3731 * truncating pages because truncate_inode_pages waits
3732 * for us above. We don't truncate pages if we're
3733 * blocking anything < EXMODE because we want to keep
3734 * them around in that case. */
3735 filemap_fdatawait(mapping);
3736 }
3737
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +01003738 forget_all_cached_acls(inode);
3739
Mark Fashehf1f54062007-10-18 15:13:59 -07003740out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003741 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003742}
3743
Tao Maa4338482009-08-18 11:19:29 +08003744static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3745 struct ocfs2_lock_res *lockres,
3746 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003747{
Tao Maa4338482009-08-18 11:19:29 +08003748 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003749
Joel Beckerbd3e7612008-02-01 12:14:57 -08003750 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3751 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003752
3753 if (checkpointed)
3754 return 1;
3755
Tao Maa4338482009-08-18 11:19:29 +08003756 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003757 return 0;
3758}
3759
Tao Maa4338482009-08-18 11:19:29 +08003760static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3761 int new_level)
3762{
3763 struct inode *inode = ocfs2_lock_res_inode(lockres);
3764
3765 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3766}
3767
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003768static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3769{
3770 struct inode *inode = ocfs2_lock_res_inode(lockres);
3771
3772 __ocfs2_stuff_meta_lvb(inode);
3773}
3774
Mark Fashehd680efe2006-09-08 14:14:34 -07003775/*
3776 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003777 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003778 * dlmglue API and push these off to the ocfs2_wq in the future.
3779 */
3780static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3781 struct ocfs2_lock_res *lockres)
3782{
3783 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3784 ocfs2_dentry_lock_put(osb, dl);
3785}
3786
3787/*
3788 * d_delete() matching dentries before the lock downconvert.
3789 *
3790 * At this point, any process waiting to destroy the
3791 * dentry_lock due to last ref count is stopped by the
3792 * OCFS2_LOCK_QUEUED flag.
3793 *
3794 * We have two potential problems
3795 *
3796 * 1) If we do the last reference drop on our dentry_lock (via dput)
3797 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3798 * the downconvert to finish. Instead we take an elevated
3799 * reference and push the drop until after we've completed our
3800 * unblock processing.
3801 *
3802 * 2) There might be another process with a final reference,
3803 * waiting on us to finish processing. If this is the case, we
3804 * detect it and exit out - there's no more dentries anyway.
3805 */
3806static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3807 int blocking)
3808{
3809 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3810 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3811 struct dentry *dentry;
3812 unsigned long flags;
3813 int extra_ref = 0;
3814
3815 /*
3816 * This node is blocking another node from getting a read
3817 * lock. This happens when we've renamed within a
3818 * directory. We've forced the other nodes to d_delete(), but
3819 * we never actually dropped our lock because it's still
3820 * valid. The downconvert code will retain a PR for this node,
3821 * so there's no further work to do.
3822 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003823 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003824 return UNBLOCK_CONTINUE;
3825
3826 /*
3827 * Mark this inode as potentially orphaned. The code in
3828 * ocfs2_delete_inode() will figure out whether it actually
3829 * needs to be freed or not.
3830 */
3831 spin_lock(&oi->ip_lock);
3832 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3833 spin_unlock(&oi->ip_lock);
3834
3835 /*
3836 * Yuck. We need to make sure however that the check of
3837 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3838 * respect to a reference decrement or the setting of that
3839 * flag.
3840 */
3841 spin_lock_irqsave(&lockres->l_lock, flags);
3842 spin_lock(&dentry_attach_lock);
3843 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3844 && dl->dl_count) {
3845 dl->dl_count++;
3846 extra_ref = 1;
3847 }
3848 spin_unlock(&dentry_attach_lock);
3849 spin_unlock_irqrestore(&lockres->l_lock, flags);
3850
3851 mlog(0, "extra_ref = %d\n", extra_ref);
3852
3853 /*
3854 * We have a process waiting on us in ocfs2_dentry_iput(),
3855 * which means we can't have any more outstanding
3856 * aliases. There's no need to do any more work.
3857 */
3858 if (!extra_ref)
3859 return UNBLOCK_CONTINUE;
3860
3861 spin_lock(&dentry_attach_lock);
3862 while (1) {
3863 dentry = ocfs2_find_local_alias(dl->dl_inode,
3864 dl->dl_parent_blkno, 1);
3865 if (!dentry)
3866 break;
3867 spin_unlock(&dentry_attach_lock);
3868
alex chen10ab8812015-02-10 14:09:23 -08003869 if (S_ISDIR(dl->dl_inode->i_mode))
3870 shrink_dcache_parent(dentry);
3871
Al Viroa4555892014-10-21 20:11:25 -04003872 mlog(0, "d_delete(%pd);\n", dentry);
Mark Fashehd680efe2006-09-08 14:14:34 -07003873
3874 /*
3875 * The following dcache calls may do an
3876 * iput(). Normally we don't want that from the
3877 * downconverting thread, but in this case it's ok
3878 * because the requesting node already has an
3879 * exclusive lock on the inode, so it can't be queued
3880 * for a downconvert.
3881 */
3882 d_delete(dentry);
3883 dput(dentry);
3884
3885 spin_lock(&dentry_attach_lock);
3886 }
3887 spin_unlock(&dentry_attach_lock);
3888
3889 /*
3890 * If we are the last holder of this dentry lock, there is no
3891 * reason to downconvert so skip straight to the unlock.
3892 */
3893 if (dl->dl_count == 1)
3894 return UNBLOCK_STOP_POST;
3895
3896 return UNBLOCK_CONTINUE_POST;
3897}
3898
Tao Ma8dec98e2009-08-18 11:19:58 +08003899static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3900 int new_level)
3901{
3902 struct ocfs2_refcount_tree *tree =
3903 ocfs2_lock_res_refcount_tree(lockres);
3904
3905 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3906}
3907
3908static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3909 int blocking)
3910{
3911 struct ocfs2_refcount_tree *tree =
3912 ocfs2_lock_res_refcount_tree(lockres);
3913
3914 ocfs2_metadata_cache_purge(&tree->rf_ci);
3915
3916 return UNBLOCK_CONTINUE;
3917}
3918
Jan Kara9e33d692008-08-25 19:56:50 +02003919static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3920{
3921 struct ocfs2_qinfo_lvb *lvb;
3922 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3923 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3924 oinfo->dqi_gi.dqi_type);
3925
Mark Fasheha641dc22008-12-24 16:03:48 -08003926 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003927 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3928 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3929 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3930 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3931 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3932 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3933 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
Jan Kara9e33d692008-08-25 19:56:50 +02003934}
3935
3936void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3937{
3938 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3939 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3940 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3941
Jan Kara9e33d692008-08-25 19:56:50 +02003942 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3943 ocfs2_cluster_unlock(osb, lockres, level);
Jan Kara9e33d692008-08-25 19:56:50 +02003944}
3945
3946static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3947{
3948 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3949 oinfo->dqi_gi.dqi_type);
3950 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3951 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003952 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003953 struct ocfs2_global_disk_dqinfo *gdinfo;
3954 int status = 0;
3955
Joel Becker1c520df2009-06-19 15:14:13 -07003956 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3957 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003958 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3959 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3960 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3961 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3962 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3963 oinfo->dqi_gi.dqi_free_entry =
3964 be32_to_cpu(lvb->lvb_free_entry);
3965 } else {
Jan Karaae4f6ef2010-04-28 19:04:29 +02003966 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
3967 oinfo->dqi_giblk, &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +01003968 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003969 mlog_errno(status);
3970 goto bail;
3971 }
3972 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3973 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3974 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3975 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3976 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3977 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3978 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3979 oinfo->dqi_gi.dqi_free_entry =
3980 le32_to_cpu(gdinfo->dqi_free_entry);
3981 brelse(bh);
3982 ocfs2_track_lock_refresh(lockres);
3983 }
3984
3985bail:
3986 return status;
3987}
3988
3989/* Lock quota info, this function expects at least shared lock on the quota file
3990 * so that we can safely refresh quota info from disk. */
3991int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3992{
3993 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3994 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3995 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3996 int status = 0;
3997
Jan Kara9e33d692008-08-25 19:56:50 +02003998 /* On RO devices, locking really isn't needed... */
3999 if (ocfs2_is_hard_readonly(osb)) {
4000 if (ex)
4001 status = -EROFS;
4002 goto bail;
4003 }
4004 if (ocfs2_mount_local(osb))
4005 goto bail;
4006
4007 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4008 if (status < 0) {
4009 mlog_errno(status);
4010 goto bail;
4011 }
4012 if (!ocfs2_should_refresh_lock_res(lockres))
4013 goto bail;
4014 /* OK, we have the lock but we need to refresh the quota info */
4015 status = ocfs2_refresh_qinfo(oinfo);
4016 if (status)
4017 ocfs2_qinfo_unlock(oinfo, ex);
4018 ocfs2_complete_lock_res_refresh(lockres, status);
4019bail:
Jan Kara9e33d692008-08-25 19:56:50 +02004020 return status;
4021}
4022
Tao Ma8dec98e2009-08-18 11:19:58 +08004023int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
4024{
4025 int status;
4026 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4027 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4028 struct ocfs2_super *osb = lockres->l_priv;
4029
4030
4031 if (ocfs2_is_hard_readonly(osb))
4032 return -EROFS;
4033
4034 if (ocfs2_mount_local(osb))
4035 return 0;
4036
4037 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4038 if (status < 0)
4039 mlog_errno(status);
4040
4041 return status;
4042}
4043
4044void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
4045{
4046 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4047 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4048 struct ocfs2_super *osb = lockres->l_priv;
4049
4050 if (!ocfs2_mount_local(osb))
4051 ocfs2_cluster_unlock(osb, lockres, level);
4052}
4053
Adrian Bunk00600052008-01-29 00:11:41 +02004054static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
4055 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08004056{
4057 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07004058 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08004059 unsigned long flags;
4060
4061 /* Our reference to the lockres in this function can be
4062 * considered valid until we remove the OCFS2_LOCK_QUEUED
4063 * flag. */
4064
Mark Fashehccd979b2005-12-15 14:31:24 -08004065 BUG_ON(!lockres);
4066 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08004067
Sunil Mushran9b915182010-02-26 19:42:44 -08004068 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08004069
4070 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07004071 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08004072 * still be marked with OCFS2_LOCK_FREEING after this check,
4073 * but short circuiting here will still save us some
4074 * performance. */
4075 spin_lock_irqsave(&lockres->l_lock, flags);
4076 if (lockres->l_flags & OCFS2_LOCK_FREEING)
4077 goto unqueue;
4078 spin_unlock_irqrestore(&lockres->l_lock, flags);
4079
Mark Fashehb5e500e2006-09-13 22:01:16 -07004080 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08004081 if (status < 0)
4082 mlog_errno(status);
4083
4084 spin_lock_irqsave(&lockres->l_lock, flags);
4085unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07004086 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004087 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
4088 } else
4089 ocfs2_schedule_blocked_lock(osb, lockres);
4090
Sunil Mushran9b915182010-02-26 19:42:44 -08004091 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07004092 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08004093 spin_unlock_irqrestore(&lockres->l_lock, flags);
4094
Mark Fashehd680efe2006-09-08 14:14:34 -07004095 if (ctl.unblock_action != UNBLOCK_CONTINUE
4096 && lockres->l_ops->post_unlock)
4097 lockres->l_ops->post_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08004098}
4099
4100static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4101 struct ocfs2_lock_res *lockres)
4102{
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004103 unsigned long flags;
4104
Mark Fashehccd979b2005-12-15 14:31:24 -08004105 assert_spin_locked(&lockres->l_lock);
4106
4107 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4108 /* Do not schedule a lock for downconvert when it's on
4109 * the way to destruction - any nodes wanting access
4110 * to the resource will get it soon. */
Sunil Mushran9b915182010-02-26 19:42:44 -08004111 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08004112 lockres->l_name, lockres->l_flags);
4113 return;
4114 }
4115
4116 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4117
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004118 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08004119 if (list_empty(&lockres->l_blocked_list)) {
4120 list_add_tail(&lockres->l_blocked_list,
4121 &osb->blocked_lock_list);
4122 osb->blocked_lock_count++;
4123 }
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004124 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08004125}
Mark Fasheh34d024f2007-09-24 15:56:19 -07004126
4127static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4128{
4129 unsigned long processed;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004130 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004131 struct ocfs2_lock_res *lockres;
4132
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004133 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004134 /* grab this early so we know to try again if a state change and
4135 * wake happens part-way through our work */
4136 osb->dc_work_sequence = osb->dc_wake_sequence;
4137
4138 processed = osb->blocked_lock_count;
Joseph Qi209f7512015-08-06 15:46:23 -07004139 /*
4140 * blocked lock processing in this loop might call iput which can
4141 * remove items off osb->blocked_lock_list. Downconvert up to
4142 * 'processed' number of locks, but stop short if we had some
4143 * removed in ocfs2_mark_lockres_freeing when downconverting.
4144 */
4145 while (processed && !list_empty(&osb->blocked_lock_list)) {
Mark Fasheh34d024f2007-09-24 15:56:19 -07004146 lockres = list_entry(osb->blocked_lock_list.next,
4147 struct ocfs2_lock_res, l_blocked_list);
4148 list_del_init(&lockres->l_blocked_list);
4149 osb->blocked_lock_count--;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004150 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004151
4152 BUG_ON(!processed);
4153 processed--;
4154
4155 ocfs2_process_blocked_lock(osb, lockres);
4156
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004157 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004158 }
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004159 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004160}
4161
4162static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4163{
4164 int empty = 0;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004165 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004166
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004167 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004168 if (list_empty(&osb->blocked_lock_list))
4169 empty = 1;
4170
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004171 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004172 return empty;
4173}
4174
4175static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4176{
4177 int should_wake = 0;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004178 unsigned long flags;
Mark Fasheh34d024f2007-09-24 15:56:19 -07004179
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004180 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004181 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4182 should_wake = 1;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004183 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004184
4185 return should_wake;
4186}
4187
Adrian Bunk200bfae2008-02-17 10:20:38 +02004188static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004189{
4190 int status = 0;
4191 struct ocfs2_super *osb = arg;
4192
4193 /* only quit once we've been asked to stop and there is no more
4194 * work available */
4195 while (!(kthread_should_stop() &&
4196 ocfs2_downconvert_thread_lists_empty(osb))) {
4197
4198 wait_event_interruptible(osb->dc_event,
4199 ocfs2_downconvert_thread_should_wake(osb) ||
4200 kthread_should_stop());
4201
4202 mlog(0, "downconvert_thread: awoken\n");
4203
4204 ocfs2_downconvert_thread_do_work(osb);
4205 }
4206
4207 osb->dc_task = NULL;
4208 return status;
4209}
4210
4211void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4212{
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004213 unsigned long flags;
4214
4215 spin_lock_irqsave(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004216 /* make sure the voting thread gets a swipe at whatever changes
4217 * the caller may have made to the voting state */
4218 osb->dc_wake_sequence++;
Srinivas Eedaa75e9cc2012-01-30 21:51:22 -08004219 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Mark Fasheh34d024f2007-09-24 15:56:19 -07004220 wake_up(&osb->dc_event);
4221}