4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2016, Intel Corporation.
25 * lustre/target/barrier.c
27 * Currently, the Lustre barrier is implemented as write barrier on all MDTs.
28 * For each MDT in the system, when it starts, it registers a barrier instance
29 * that will be used in handling subsequent barrier requests.
31 * Author: Fan, Yong <fan.yong@intel.com>
34 #define DEBUG_SUBSYSTEM S_SNAPSHOT
36 #include <linux/percpu_counter.h>
38 #include <lustre/lustre_idl.h>
39 #include <dt_object.h>
41 #include <obd_class.h>
42 #include <lustre_barrier.h>
43 #include <lustre/lustre_barrier_user.h>
45 static LIST_HEAD(barrier_instance_list);
46 static DEFINE_SPINLOCK(barrier_instance_lock);
48 struct barrier_instance {
49 struct list_head bi_link;
50 struct dt_device *bi_bottom;
51 struct dt_device *bi_next;
52 wait_queue_head_t bi_waitq;
54 struct percpu_counter bi_writers;
60 static inline char *barrier_barrier2name(struct barrier_instance *barrier)
62 return barrier->bi_bottom->dd_lu_dev.ld_obd->obd_name;
65 static inline __u32 barrier_dev_idx(struct barrier_instance *barrier)
67 return lu_site2seq(barrier->bi_bottom->dd_lu_dev.ld_site)->ss_node_id;
70 static void barrier_instance_cleanup(struct barrier_instance *barrier)
72 LASSERT(list_empty(&barrier->bi_link));
74 percpu_counter_destroy(&barrier->bi_writers);
75 OBD_FREE_PTR(barrier);
78 static inline void barrier_instance_put(struct barrier_instance *barrier)
80 if (atomic_dec_and_test(&barrier->bi_ref))
81 barrier_instance_cleanup(barrier);
84 static struct barrier_instance *
85 barrier_instance_find_locked(struct dt_device *key)
87 struct barrier_instance *barrier;
89 list_for_each_entry(barrier, &barrier_instance_list, bi_link) {
90 if (barrier->bi_bottom == key)
97 static void barrier_instance_add(struct barrier_instance *barrier)
99 struct barrier_instance *tmp;
101 spin_lock(&barrier_instance_lock);
102 tmp = barrier_instance_find_locked(barrier->bi_bottom);
105 list_add_tail(&barrier->bi_link, &barrier_instance_list);
106 spin_unlock(&barrier_instance_lock);
109 static struct barrier_instance *barrier_instance_find(struct dt_device *key)
111 struct barrier_instance *barrier;
113 spin_lock(&barrier_instance_lock);
114 barrier = barrier_instance_find_locked(key);
116 atomic_inc(&barrier->bi_ref);
117 spin_unlock(&barrier_instance_lock);
122 static void barrier_set(struct barrier_instance *barrier, __u32 status)
124 if (barrier->bi_status != status) {
125 CDEBUG(D_SNAPSHOT, "%s: change barrier status from %u to %u\n",
126 barrier_barrier2name(barrier),
127 barrier->bi_status, status);
129 barrier->bi_status = status;
134 * Create the barrier for the given instance.
136 * We use two-phases barrier to guarantee that after the barrier setup:
137 * 1) All the MDT side pending async modification have been flushed.
138 * 2) Any subsequent modification will be blocked.
139 * 3) All async transactions on the MDTs have been committed.
141 * For phase1, we do the following:
143 * Firstly, it sets barrier flag on the instance that will block subsequent
144 * modifications from clients. (Note: server sponsored modification will be
145 * allowed for flush pending modifications)
147 * Secondly, it will flush all pending modification via dt_sync(), such as
148 * async OST-object destroy, async OST-object owner changes, and so on.
150 * If there are some on-handling clients sponsored modifications during the
151 * barrier freezing, then related modifications may cause pending requests
152 * after the first dt_sync(), so call dt_sync() again after all on-handling
153 * modifications done.
155 * With the phase1 barrier set, all pending cross-servers modification have
156 * been flushed to remote servers, and any new modification will be blocked.
157 * But it does not guarantees that all the updates have been committed to
158 * storage on remote servers. So when all the instances have done phase1
159 * barrier successfully, the MGS will notify all instances to do the phase2
160 * barrier as following:
162 * Every barrier instance will call dt_sync() to make all async transactions
163 * to be committed locally.
165 * \param[in] env pointer to the thread context
166 * \param[in] barrier pointer to the barrier instance
167 * \param[in] phase1 indicate whether it is phase1 barrier or not
169 * \retval positive number for timeout
170 * \retval 0 for success
171 * \retval negative error number on failure
173 static int barrier_freeze(const struct lu_env *env,
174 struct barrier_instance *barrier, bool phase1)
181 write_lock(&barrier->bi_rwlock);
182 barrier_set(barrier, phase1 ? BS_FREEZING_P1 : BS_FREEZING_P2);
184 /* Avoid out-of-order execution the barrier_set()
185 * and the check of inflight modifications count. */
189 inflight = percpu_counter_sum(&barrier->bi_writers);
190 write_unlock(&barrier->bi_rwlock);
192 rc = dt_sync(env, barrier->bi_next);
196 LASSERT(barrier->bi_deadline != 0);
198 left = barrier->bi_deadline - cfs_time_current_sec();
202 if (phase1 && inflight != 0) {
203 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(left),
206 rc = l_wait_event(barrier->bi_waitq,
207 percpu_counter_sum(&barrier->bi_writers) == 0,
212 /* sync again after all inflight modifications done. */
213 rc = dt_sync(env, barrier->bi_next);
217 if (cfs_time_beforeq(barrier->bi_deadline,
218 cfs_time_current_sec()))
222 CDEBUG(D_SNAPSHOT, "%s: barrier freezing %s done.\n",
223 barrier_barrier2name(barrier), phase1 ? "phase1" : "phase2");
226 barrier_set(barrier, BS_FROZEN);
231 void barrier_init(void)
235 void barrier_fini(void)
237 LASSERT(list_empty(&barrier_instance_list));
240 bool barrier_entry(struct dt_device *key)
242 struct barrier_instance *barrier;
243 bool entered = false;
246 barrier = barrier_instance_find(key);
247 if (unlikely(!barrier))
251 read_lock(&barrier->bi_rwlock);
252 if (likely(barrier->bi_status != BS_FREEZING_P1 &&
253 barrier->bi_status != BS_FREEZING_P2 &&
254 barrier->bi_status != BS_FROZEN) ||
255 cfs_time_beforeq(barrier->bi_deadline, cfs_time_current_sec())) {
256 percpu_counter_inc(&barrier->bi_writers);
259 read_unlock(&barrier->bi_rwlock);
261 barrier_instance_put(barrier);
264 EXPORT_SYMBOL(barrier_entry);
266 void barrier_exit(struct dt_device *key)
268 struct barrier_instance *barrier;
270 barrier = barrier_instance_find(key);
271 if (likely(barrier)) {
272 percpu_counter_dec(&barrier->bi_writers);
274 /* Avoid out-of-order execution the decreasing inflight
275 * modifications count and the check of barrier status. */
278 if (unlikely(barrier->bi_status == BS_FREEZING_P1))
279 wake_up_all(&barrier->bi_waitq);
280 barrier_instance_put(barrier);
283 EXPORT_SYMBOL(barrier_exit);
285 int barrier_handler(struct dt_device *key, struct ptlrpc_request *req)
287 struct ldlm_gl_barrier_desc *desc;
288 struct barrier_instance *barrier;
289 struct barrier_lvb *lvb;
294 /* glimpse on barrier locks always packs a glimpse descriptor */
295 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_DESC_CALLBACK);
296 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
298 GOTO(out, rc = -EPROTO);
300 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
301 sizeof(struct barrier_lvb));
302 rc = req_capsule_server_pack(&req->rq_pill);
306 lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
307 barrier = barrier_instance_find(key);
309 GOTO(out, rc = -ENODEV);
311 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
313 GOTO(out_barrier, rc);
316 "%s: handling barrier request: status %u, timeout %u\n",
317 barrier_barrier2name(barrier),
318 desc->lgbd_status, desc->lgbd_timeout);
320 switch (desc->lgbd_status) {
322 barrier_set(barrier, BS_INIT);
326 barrier->bi_deadline = cfs_time_current_sec() +
328 rc = barrier_freeze(&env, barrier,
329 desc->lgbd_status == BS_FREEZING_P1);
334 barrier_set(barrier, BS_THAWED);
337 CWARN("%s: unexpected barrier status %u\n",
338 barrier_barrier2name(barrier), desc->lgbd_status);
350 barrier_set(barrier, BS_FAILED);
352 barrier_set(barrier, BS_EXPIRED);
354 lvb->lvb_status = barrier->bi_status;
355 lvb->lvb_index = barrier_dev_idx(barrier);
357 CDEBUG(D_SNAPSHOT, "%s: handled barrier request: status %u, "
358 "deadline %lu: rc = %d\n", barrier_barrier2name(barrier),
359 lvb->lvb_status, barrier->bi_deadline, rc);
361 barrier_instance_put(barrier);
368 EXPORT_SYMBOL(barrier_handler);
370 int barrier_register(struct dt_device *key, struct dt_device *next)
372 struct barrier_instance *barrier;
376 OBD_ALLOC_PTR(barrier);
380 INIT_LIST_HEAD(&barrier->bi_link);
381 barrier->bi_bottom = key;
382 barrier->bi_next = next;
383 init_waitqueue_head(&barrier->bi_waitq);
384 rwlock_init(&barrier->bi_rwlock);
385 atomic_set(&barrier->bi_ref, 1);
386 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
387 rc = percpu_counter_init(&barrier->bi_writers, 0, GFP_KERNEL);
389 rc = percpu_counter_init(&barrier->bi_writers, 0);
392 barrier_instance_cleanup(barrier);
394 barrier_instance_add(barrier);
398 EXPORT_SYMBOL(barrier_register);
400 void barrier_deregister(struct dt_device *key)
402 struct barrier_instance *barrier;
404 spin_lock(&barrier_instance_lock);
405 barrier = barrier_instance_find_locked(key);
407 list_del_init(&barrier->bi_link);
408 spin_unlock(&barrier_instance_lock);
411 barrier_instance_put(barrier);
413 EXPORT_SYMBOL(barrier_deregister);