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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2012, 2017, Intel Corporation.
27 * lustre/target/tgt_main.c
29 * Lustre Unified Target main initialization code
31 * Author: Mikhail Pershin <mike.pershin@intel.com>
34 #define DEBUG_SUBSYSTEM S_CLASS
37 #include "tgt_internal.h"
38 #include "../ptlrpc/ptlrpc_internal.h"
40 /* This must be longer than the longest string below */
41 #define SYNC_STATES_MAXLEN 16
42 static char *sync_on_cancel_states[] = {"never",
47 * Show policy for handling dirty data under a lock being cancelled.
49 * \param[in] kobj sysfs kobject
50 * \param[in] attr sysfs attribute
51 * \param[in] buf buffer for data
53 * \retval 0 and buffer filled with data on success
54 * \retval negative value on error
56 static ssize_t sync_lock_cancel_show(struct kobject *kobj,
57 struct attribute *attr, char *buf)
59 struct obd_device *obd = container_of(kobj, struct obd_device,
61 struct lu_target *tgt = obd->u.obt.obt_lut;
63 return sprintf(buf, "%s\n",
64 sync_on_cancel_states[tgt->lut_sync_lock_cancel]);
68 * Change policy for handling dirty data under a lock being cancelled.
70 * This variable defines what action target takes upon lock cancel
71 * There are three possible modes:
72 * 1) never - never do sync upon lock cancel. This can lead to data
73 * inconsistencies if both the OST and client crash while writing a file
74 * that is also concurrently being read by another client. In these cases,
75 * this may allow the file data to "rewind" to an earlier state.
76 * 2) blocking - do sync only if there is blocking lock, e.g. if another
77 * client is trying to access this same object
78 * 3) always - do sync always
80 * \param[in] kobj kobject
81 * \param[in] attr attribute to show
82 * \param[in] buf buffer for data
83 * \param[in] count buffer size
85 * \retval \a count on success
86 * \retval negative value on error
88 static ssize_t sync_lock_cancel_store(struct kobject *kobj,
89 struct attribute *attr,
90 const char *buffer, size_t count)
92 struct obd_device *obd = container_of(kobj, struct obd_device,
94 struct lu_target *tgt = obd->u.obt.obt_lut;
98 if (count == 0 || count >= SYNC_STATES_MAXLEN)
101 for (i = 0 ; i < NUM_SYNC_ON_CANCEL_STATES; i++) {
102 if (strcmp(buffer, sync_on_cancel_states[i]) == 0) {
108 /* Legacy numeric codes */
110 int rc = kstrtoint(buffer, 0, &val);
115 if (val < 0 || val > 2)
118 spin_lock(&tgt->lut_flags_lock);
119 tgt->lut_sync_lock_cancel = val;
120 spin_unlock(&tgt->lut_flags_lock);
123 LUSTRE_RW_ATTR(sync_lock_cancel);
126 * Show maximum number of Filter Modification Data (FMD) maintained.
128 * \param[in] kobj kobject
129 * \param[in] attr attribute to show
130 * \param[in] buf buffer for data
132 * \retval 0 and buffer filled with data on success
133 * \retval negative value on error
135 ssize_t tgt_fmd_count_show(struct kobject *kobj, struct attribute *attr,
138 struct obd_device *obd = container_of(kobj, struct obd_device,
140 struct lu_target *lut = obd->u.obt.obt_lut;
142 return sprintf(buf, "%u\n", lut->lut_fmd_max_num);
146 * Change number of FMDs maintained by target.
148 * This defines how large the list of FMDs can be.
150 * \param[in] kobj kobject
151 * \param[in] attr attribute to show
152 * \param[in] buf buffer for data
153 * \param[in] count buffer size
155 * \retval \a count on success
156 * \retval negative value on error
158 ssize_t tgt_fmd_count_store(struct kobject *kobj, struct attribute *attr,
159 const char *buffer, size_t count)
161 struct obd_device *obd = container_of(kobj, struct obd_device,
163 struct lu_target *lut = obd->u.obt.obt_lut;
166 rc = kstrtoint(buffer, 0, &val);
170 if (val < 1 || val > 65536)
173 lut->lut_fmd_max_num = val;
177 LUSTRE_RW_ATTR(tgt_fmd_count);
180 * Show the maximum age of FMD data in seconds.
182 * \param[in] kobj kobject
183 * \param[in] attr attribute to show
184 * \param[in] buf buffer for data
186 * \retval 0 and buffer filled with data on success
187 * \retval negative value on error
189 ssize_t tgt_fmd_seconds_show(struct kobject *kobj, struct attribute *attr,
192 struct obd_device *obd = container_of(kobj, struct obd_device,
194 struct lu_target *lut = obd->u.obt.obt_lut;
196 return sprintf(buf, "%lld\n", lut->lut_fmd_max_age);
200 * Set the maximum age of FMD data in seconds.
202 * This defines how long FMD data stays in the FMD list.
204 * \param[in] kobj kobject
205 * \param[in] attr attribute to show
206 * \param[in] buf buffer for data
207 * \param[in] count buffer size
209 * \retval \a count on success
210 * \retval negative number on error
212 ssize_t tgt_fmd_seconds_store(struct kobject *kobj, struct attribute *attr,
213 const char *buffer, size_t count)
215 struct obd_device *obd = container_of(kobj, struct obd_device,
217 struct lu_target *lut = obd->u.obt.obt_lut;
221 rc = kstrtoll(buffer, 0, &val);
225 if (val < 1 || val > 65536) /* ~ 18 hour max */
228 lut->lut_fmd_max_age = val;
232 LUSTRE_RW_ATTR(tgt_fmd_seconds);
234 /* These two aliases are old names and kept for compatibility, they were
235 * changed to 'tgt_fmd_count' and 'tgt_fmd_seconds'.
236 * This change was made in Lustre 2.13, so these aliases can be removed
237 * when back compatibility is not needed with any Lustre version prior 2.13
239 static struct lustre_attr tgt_fmd_count_compat = __ATTR(client_cache_count,
240 0644, tgt_fmd_count_show, tgt_fmd_count_store);
241 static struct lustre_attr tgt_fmd_seconds_compat = __ATTR(client_cache_seconds,
242 0644, tgt_fmd_seconds_show, tgt_fmd_seconds_store);
244 static const struct attribute *tgt_attrs[] = {
245 &lustre_attr_sync_lock_cancel.attr,
246 &lustre_attr_tgt_fmd_count.attr,
247 &lustre_attr_tgt_fmd_seconds.attr,
248 &tgt_fmd_count_compat.attr,
249 &tgt_fmd_seconds_compat.attr,
253 int tgt_tunables_init(struct lu_target *lut)
257 rc = sysfs_create_files(&lut->lut_obd->obd_kset.kobj, tgt_attrs);
259 lut->lut_attrs = tgt_attrs;
262 EXPORT_SYMBOL(tgt_tunables_init);
264 void tgt_tunables_fini(struct lu_target *lut)
266 if (lut->lut_attrs) {
267 sysfs_remove_files(&lut->lut_obd->obd_kset.kobj,
269 lut->lut_attrs = NULL;
272 EXPORT_SYMBOL(tgt_tunables_fini);
275 * Save cross-MDT lock in lut_slc_locks.
277 * Lock R/W count is not saved, but released in unlock (not canceled remotely),
278 * instead only a refcount is taken, so that the remote MDT where the object
279 * resides can detect conflict with this lock there.
282 * \param lock cross-MDT lock to save
283 * \param transno when the transaction with this transno is committed, this lock
286 void tgt_save_slc_lock(struct lu_target *lut, struct ldlm_lock *lock,
289 spin_lock(&lut->lut_slc_locks_guard);
290 lock_res_and_lock(lock);
291 if (ldlm_is_cbpending(lock)) {
292 /* if it was canceld by server, don't save, because remote MDT
293 * will do Sync-on-Cancel. */
296 lock->l_transno = transno;
297 /* if this lock is in the list already, there are two operations
298 * both use this lock, and save it after use, so for the second
299 * one, just put the refcount. */
300 if (list_empty(&lock->l_slc_link))
301 list_add_tail(&lock->l_slc_link, &lut->lut_slc_locks);
305 unlock_res_and_lock(lock);
306 spin_unlock(&lut->lut_slc_locks_guard);
308 EXPORT_SYMBOL(tgt_save_slc_lock);
311 * Discard cross-MDT lock from lut_slc_locks.
313 * This is called upon BAST, just remove lock from lut_slc_locks and put lock
314 * refcount. The BAST will cancel this lock.
317 * \param lock cross-MDT lock to discard
319 void tgt_discard_slc_lock(struct lu_target *lut, struct ldlm_lock *lock)
321 spin_lock(&lut->lut_slc_locks_guard);
322 lock_res_and_lock(lock);
323 /* may race with tgt_cancel_slc_locks() */
324 if (lock->l_transno != 0) {
325 LASSERT(!list_empty(&lock->l_slc_link));
326 LASSERT(ldlm_is_cbpending(lock));
327 list_del_init(&lock->l_slc_link);
331 unlock_res_and_lock(lock);
332 spin_unlock(&lut->lut_slc_locks_guard);
334 EXPORT_SYMBOL(tgt_discard_slc_lock);
337 * Cancel cross-MDT locks upon transaction commit.
339 * Remove cross-MDT locks from lut_slc_locks, cancel them and put lock refcount.
342 * \param transno transaction with this number was committed.
344 void tgt_cancel_slc_locks(struct lu_target *lut, __u64 transno)
346 struct ldlm_lock *lock, *next;
348 struct lustre_handle lockh;
350 spin_lock(&lut->lut_slc_locks_guard);
351 list_for_each_entry_safe(lock, next, &lut->lut_slc_locks,
353 lock_res_and_lock(lock);
354 LASSERT(lock->l_transno != 0);
355 if (lock->l_transno > transno) {
356 unlock_res_and_lock(lock);
359 /* ouch, another operation is using it after it's saved */
360 if (lock->l_readers != 0 || lock->l_writers != 0) {
361 unlock_res_and_lock(lock);
364 /* set CBPENDING so that this lock won't be used again */
365 ldlm_set_cbpending(lock);
367 list_move(&lock->l_slc_link, &list);
368 unlock_res_and_lock(lock);
370 spin_unlock(&lut->lut_slc_locks_guard);
372 list_for_each_entry_safe(lock, next, &list, l_slc_link) {
373 list_del_init(&lock->l_slc_link);
374 ldlm_lock2handle(lock, &lockh);
375 ldlm_cli_cancel(&lockh, LCF_ASYNC);
380 int tgt_init(const struct lu_env *env, struct lu_target *lut,
381 struct obd_device *obd, struct dt_device *dt,
382 struct tgt_opc_slice *slice, int request_fail_id,
385 struct dt_object_format dof;
389 struct tg_grants_data *tgd = &lut->lut_tgd;
390 struct obd_statfs *osfs;
398 lut->lut_bottom = dt;
399 lut->lut_last_rcvd = NULL;
400 lut->lut_client_bitmap = NULL;
401 atomic_set(&lut->lut_num_clients, 0);
402 atomic_set(&lut->lut_client_generation, 0);
403 lut->lut_reply_data = NULL;
404 lut->lut_reply_bitmap = NULL;
405 obd->u.obt.obt_lut = lut;
406 obd->u.obt.obt_magic = OBT_MAGIC;
408 /* set request handler slice and parameters */
409 lut->lut_slice = slice;
410 lut->lut_reply_fail_id = reply_fail_id;
411 lut->lut_request_fail_id = request_fail_id;
413 /* sptlrcp variables init */
414 rwlock_init(&lut->lut_sptlrpc_lock);
415 sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
417 spin_lock_init(&lut->lut_flags_lock);
418 lut->lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
420 spin_lock_init(&lut->lut_slc_locks_guard);
421 INIT_LIST_HEAD(&lut->lut_slc_locks);
423 /* last_rcvd initialization is needed by replayable targets only */
424 if (!obd->obd_replayable)
427 /* initialize grant and statfs data in target */
428 dt_conf_get(env, lut->lut_bottom, &lut->lut_dt_conf);
431 spin_lock_init(&tgd->tgd_osfs_lock);
432 tgd->tgd_osfs_age = ktime_get_seconds() - 1000;
433 tgd->tgd_osfs_unstable = 0;
434 tgd->tgd_statfs_inflight = 0;
435 tgd->tgd_osfs_inflight = 0;
438 spin_lock_init(&tgd->tgd_grant_lock);
439 tgd->tgd_tot_dirty = 0;
440 tgd->tgd_tot_granted = 0;
441 tgd->tgd_tot_pending = 0;
442 tgd->tgd_grant_compat_disable = 0;
444 /* populate cached statfs data */
445 osfs = &tgt_th_info(env)->tti_u.osfs;
446 rc = tgt_statfs_internal(env, lut, osfs, 0, NULL);
448 CERROR("%s: can't get statfs data, rc %d\n", tgt_name(lut),
452 if (!is_power_of_2(osfs->os_bsize)) {
453 CERROR("%s: blocksize (%d) is not a power of 2\n",
454 tgt_name(lut), osfs->os_bsize);
455 GOTO(out, rc = -EPROTO);
457 tgd->tgd_blockbits = fls(osfs->os_bsize) - 1;
459 spin_lock_init(&lut->lut_translock);
460 spin_lock_init(&lut->lut_client_bitmap_lock);
462 OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
463 if (lut->lut_client_bitmap == NULL)
466 memset(&attr, 0, sizeof(attr));
467 attr.la_valid = LA_MODE;
468 attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
469 dof.dof_type = dt_mode_to_dft(S_IFREG);
471 lu_local_obj_fid(&fid, LAST_RECV_OID);
473 o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
476 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
481 lut->lut_last_rcvd = o;
482 rc = tgt_server_data_init(env, lut);
486 /* prepare transactions callbacks */
487 lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
488 lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
489 lut->lut_txn_cb.dtc_cookie = lut;
490 lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
491 INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
493 dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
494 lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
496 lut->lut_fmd_max_num = LUT_FMD_MAX_NUM_DEFAULT;
497 lut->lut_fmd_max_age = LUT_FMD_MAX_AGE_DEFAULT;
499 atomic_set(&lut->lut_sync_count, 0);
501 /* reply_data is supported by MDT targets only for now */
502 if (strncmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) != 0)
505 OBD_ALLOC(lut->lut_reply_bitmap,
506 LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
507 if (lut->lut_reply_bitmap == NULL)
508 GOTO(out, rc = -ENOMEM);
510 memset(&attr, 0, sizeof(attr));
511 attr.la_valid = LA_MODE;
512 attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
513 dof.dof_type = dt_mode_to_dft(S_IFREG);
515 lu_local_obj_fid(&fid, REPLY_DATA_OID);
517 o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
520 CERROR("%s: cannot open REPLY_DATA: rc = %d\n", tgt_name(lut),
524 lut->lut_reply_data = o;
526 rc = tgt_reply_data_init(env, lut);
533 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
535 obd->u.obt.obt_magic = 0;
536 obd->u.obt.obt_lut = NULL;
537 if (lut->lut_last_rcvd != NULL) {
538 dt_object_put(env, lut->lut_last_rcvd);
539 lut->lut_last_rcvd = NULL;
541 if (lut->lut_client_bitmap != NULL)
542 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
543 lut->lut_client_bitmap = NULL;
544 if (lut->lut_reply_data != NULL)
545 dt_object_put(env, lut->lut_reply_data);
546 lut->lut_reply_data = NULL;
547 if (lut->lut_reply_bitmap != NULL) {
548 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
549 if (lut->lut_reply_bitmap[i] != NULL)
550 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
551 BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
553 lut->lut_reply_bitmap[i] = NULL;
555 OBD_FREE(lut->lut_reply_bitmap,
556 LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
558 lut->lut_reply_bitmap = NULL;
561 EXPORT_SYMBOL(tgt_init);
563 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
569 if (lut->lut_lsd.lsd_feature_incompat & OBD_INCOMPAT_MULTI_RPCS &&
570 atomic_read(&lut->lut_num_clients) == 0) {
571 /* Clear MULTI RPCS incompatibility flag that prevents previous
572 * Lustre versions to mount a target with reply_data file */
573 lut->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
574 rc = tgt_server_data_update(env, lut, 1);
576 CERROR("%s: unable to clear MULTI RPCS "
577 "incompatibility flag\n",
578 lut->lut_obd->obd_name);
581 sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
583 if (lut->lut_reply_data != NULL)
584 dt_object_put(env, lut->lut_reply_data);
585 lut->lut_reply_data = NULL;
586 if (lut->lut_reply_bitmap != NULL) {
587 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
588 if (lut->lut_reply_bitmap[i] != NULL)
589 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
590 BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
592 lut->lut_reply_bitmap[i] = NULL;
594 OBD_FREE(lut->lut_reply_bitmap,
595 LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
597 lut->lut_reply_bitmap = NULL;
598 if (lut->lut_client_bitmap) {
599 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
600 lut->lut_client_bitmap = NULL;
602 if (lut->lut_last_rcvd) {
603 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
604 dt_object_put(env, lut->lut_last_rcvd);
605 lut->lut_last_rcvd = NULL;
609 EXPORT_SYMBOL(tgt_fini);
611 static struct kmem_cache *tgt_thread_kmem;
612 static struct kmem_cache *tgt_session_kmem;
613 struct kmem_cache *tgt_fmd_kmem;
615 static struct lu_kmem_descr tgt_caches[] = {
617 .ckd_cache = &tgt_thread_kmem,
618 .ckd_name = "tgt_thread_kmem",
619 .ckd_size = sizeof(struct tgt_thread_info),
622 .ckd_cache = &tgt_session_kmem,
623 .ckd_name = "tgt_session_kmem",
624 .ckd_size = sizeof(struct tgt_session_info)
627 .ckd_cache = &tgt_fmd_kmem,
628 .ckd_name = "tgt_fmd_cache",
629 .ckd_size = sizeof(struct tgt_fmd_data)
637 /* context key constructor/destructor: tg_key_init, tg_key_fini */
638 static void *tgt_key_init(const struct lu_context *ctx,
639 struct lu_context_key *key)
641 struct tgt_thread_info *thread;
643 OBD_SLAB_ALLOC_PTR_GFP(thread, tgt_thread_kmem, GFP_NOFS);
645 return ERR_PTR(-ENOMEM);
650 static void tgt_key_fini(const struct lu_context *ctx,
651 struct lu_context_key *key, void *data)
653 struct tgt_thread_info *info = data;
654 struct thandle_exec_args *args = &info->tti_tea;
657 for (i = 0; i < args->ta_alloc_args; i++) {
658 if (args->ta_args[i] != NULL)
659 OBD_FREE_PTR(args->ta_args[i]);
662 if (args->ta_args != NULL)
663 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
664 args->ta_alloc_args);
665 OBD_SLAB_FREE_PTR(info, tgt_thread_kmem);
668 static void tgt_key_exit(const struct lu_context *ctx,
669 struct lu_context_key *key, void *data)
671 struct tgt_thread_info *tti = data;
673 tti->tti_has_trans = 0;
674 tti->tti_mult_trans = 0;
677 /* context key: tg_thread_key */
678 struct lu_context_key tgt_thread_key = {
679 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
680 .lct_init = tgt_key_init,
681 .lct_fini = tgt_key_fini,
682 .lct_exit = tgt_key_exit,
685 LU_KEY_INIT_GENERIC(tgt);
687 static void *tgt_ses_key_init(const struct lu_context *ctx,
688 struct lu_context_key *key)
690 struct tgt_session_info *session;
692 OBD_SLAB_ALLOC_PTR_GFP(session, tgt_session_kmem, GFP_NOFS);
694 return ERR_PTR(-ENOMEM);
699 static void tgt_ses_key_fini(const struct lu_context *ctx,
700 struct lu_context_key *key, void *data)
702 struct tgt_session_info *session = data;
704 OBD_SLAB_FREE_PTR(session, tgt_session_kmem);
707 /* context key: tgt_session_key */
708 struct lu_context_key tgt_session_key = {
709 .lct_tags = LCT_SERVER_SESSION,
710 .lct_init = tgt_ses_key_init,
711 .lct_fini = tgt_ses_key_fini,
713 EXPORT_SYMBOL(tgt_session_key);
715 LU_KEY_INIT_GENERIC(tgt_ses);
718 * this page is allocated statically when module is initializing
719 * it is used to simulate data corruptions, see ost_checksum_bulk()
720 * for details. as the original pages provided by the layers below
721 * can be remain in the internal cache, we do not want to modify
724 struct page *tgt_page_to_corrupt;
726 int tgt_mod_init(void)
731 result = lu_kmem_init(tgt_caches);
735 tgt_page_to_corrupt = alloc_page(GFP_KERNEL);
737 tgt_key_init_generic(&tgt_thread_key, NULL);
738 lu_context_key_register_many(&tgt_thread_key, NULL);
740 tgt_ses_key_init_generic(&tgt_session_key, NULL);
741 lu_context_key_register_many(&tgt_session_key, NULL);
749 void tgt_mod_exit(void)
752 if (tgt_page_to_corrupt != NULL)
753 put_page(tgt_page_to_corrupt);
755 lu_context_key_degister(&tgt_thread_key);
756 lu_context_key_degister(&tgt_session_key);
759 lu_kmem_fini(tgt_caches);