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, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_dev.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 * Author: Di Wang <di.wang@intel.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <obd_class.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_param.h>
50 #include <lustre_log.h>
51 #include <lustre_mdc.h>
53 #include "osp_internal.h"
55 /* Slab for OSP object allocation */
56 struct kmem_cache *osp_object_kmem;
58 static struct lu_kmem_descr osp_caches[] = {
60 .ckd_cache = &osp_object_kmem,
61 .ckd_name = "osp_obj",
62 .ckd_size = sizeof(struct osp_object)
69 struct lu_object *osp_object_alloc(const struct lu_env *env,
70 const struct lu_object_header *hdr,
73 struct lu_object_header *h = NULL;
77 OBD_SLAB_ALLOC_PTR_GFP(o, osp_object_kmem, GFP_NOFS);
79 l = &o->opo_obj.do_lu;
81 /* If hdr is NULL, it means the object is not built
82 * from the top dev(MDT/OST), usually it happens when
83 * building striped object, like data object on MDT or
84 * striped object for directory */
87 lu_object_header_init(h);
88 dt_object_init(&o->opo_obj, h, d);
89 lu_object_add_top(h, l);
91 dt_object_init(&o->opo_obj, h, d);
94 l->lo_ops = &osp_lu_obj_ops;
102 static struct dt_object
103 *osp_find_or_create(const struct lu_env *env, struct osp_device *osp,
104 struct lu_attr *attr, __u32 reg_id)
106 struct osp_thread_info *osi = osp_env_info(env);
107 struct dt_object_format dof = { 0 };
108 struct dt_object *dto;
112 lu_local_obj_fid(&osi->osi_fid, reg_id);
113 attr->la_valid = LA_MODE;
114 attr->la_mode = S_IFREG | 0644;
115 dof.dof_type = DFT_REGULAR;
116 dto = dt_find_or_create(env, osp->opd_storage, &osi->osi_fid,
121 rc = dt_attr_get(env, dto, attr, NULL);
123 CERROR("%s: can't be initialized: rc = %d\n",
124 osp->opd_obd->obd_name, rc);
125 lu_object_put(env, &dto->do_lu);
131 static int osp_write_local_file(const struct lu_env *env,
132 struct osp_device *osp,
133 struct dt_object *dt_obj,
140 th = dt_trans_create(env, osp->opd_storage);
144 rc = dt_declare_record_write(env, dt_obj, buf, offset, th);
147 rc = dt_trans_start_local(env, osp->opd_storage, th);
151 rc = dt_record_write(env, dt_obj, buf, &offset, th);
153 dt_trans_stop(env, osp->opd_storage, th);
157 static int osp_init_last_objid(const struct lu_env *env, struct osp_device *osp)
159 struct osp_thread_info *osi = osp_env_info(env);
160 struct lu_fid *fid = &osp->opd_last_used_fid;
161 struct dt_object *dto;
165 dto = osp_find_or_create(env, osp, &osi->osi_attr, MDD_LOV_OBJ_OID);
167 RETURN(PTR_ERR(dto));
168 /* object will be released in device cleanup path */
169 if (osi->osi_attr.la_size >=
170 sizeof(osi->osi_id) * (osp->opd_index + 1)) {
171 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_oid,
173 rc = dt_record_read(env, dto, &osi->osi_lb, &osi->osi_off);
178 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_oid,
180 rc = osp_write_local_file(env, osp, dto, &osi->osi_lb,
183 osp->opd_last_used_oid_file = dto;
186 /* object will be released in device cleanup path */
187 CERROR("%s: can't initialize lov_objid: rc = %d\n",
188 osp->opd_obd->obd_name, rc);
189 lu_object_put(env, &dto->do_lu);
190 osp->opd_last_used_oid_file = NULL;
194 static int osp_init_last_seq(const struct lu_env *env, struct osp_device *osp)
196 struct osp_thread_info *osi = osp_env_info(env);
197 struct lu_fid *fid = &osp->opd_last_used_fid;
198 struct dt_object *dto;
202 dto = osp_find_or_create(env, osp, &osi->osi_attr, MDD_LOV_OBJ_OSEQ);
204 RETURN(PTR_ERR(dto));
206 /* object will be released in device cleanup path */
207 if (osi->osi_attr.la_size >=
208 sizeof(osi->osi_id) * (osp->opd_index + 1)) {
209 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_seq,
211 rc = dt_record_read(env, dto, &osi->osi_lb, &osi->osi_off);
216 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_seq,
218 rc = osp_write_local_file(env, osp, dto, &osi->osi_lb,
221 osp->opd_last_used_seq_file = dto;
224 /* object will be released in device cleanup path */
225 CERROR("%s: can't initialize lov_seq: rc = %d\n",
226 osp->opd_obd->obd_name, rc);
227 lu_object_put(env, &dto->do_lu);
228 osp->opd_last_used_seq_file = NULL;
232 static int osp_last_used_init(const struct lu_env *env, struct osp_device *osp)
234 struct osp_thread_info *osi = osp_env_info(env);
238 fid_zero(&osp->opd_last_used_fid);
239 rc = osp_init_last_objid(env, osp);
241 CERROR("%s: Can not get ids %d from old objid!\n",
242 osp->opd_obd->obd_name, rc);
246 rc = osp_init_last_seq(env, osp);
248 CERROR("%s: Can not get ids %d from old objid!\n",
249 osp->opd_obd->obd_name, rc);
253 if (fid_oid(&osp->opd_last_used_fid) != 0 &&
254 fid_seq(&osp->opd_last_used_fid) == 0) {
255 /* Just upgrade from the old version,
256 * set the seq to be IDIF */
257 osp->opd_last_used_fid.f_seq =
258 fid_idif_seq(fid_oid(&osp->opd_last_used_fid),
260 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off,
261 &osp->opd_last_used_fid.f_seq,
263 rc = osp_write_local_file(env, osp, osp->opd_last_used_seq_file,
264 &osi->osi_lb, osi->osi_off);
266 CERROR("%s : Can not write seq file: rc = %d\n",
267 osp->opd_obd->obd_name, rc);
272 if (!fid_is_zero(&osp->opd_last_used_fid) &&
273 !fid_is_sane(&osp->opd_last_used_fid)) {
274 CERROR("%s: Got invalid FID "DFID"\n", osp->opd_obd->obd_name,
275 PFID(&osp->opd_last_used_fid));
276 GOTO(out, rc = -EINVAL);
279 CDEBUG(D_INFO, "%s: Init last used fid "DFID"\n",
280 osp->opd_obd->obd_name, PFID(&osp->opd_last_used_fid));
283 if (osp->opd_last_used_oid_file != NULL) {
284 lu_object_put(env, &osp->opd_last_used_oid_file->do_lu);
285 osp->opd_last_used_oid_file = NULL;
287 if (osp->opd_last_used_seq_file != NULL) {
288 lu_object_put(env, &osp->opd_last_used_seq_file->do_lu);
289 osp->opd_last_used_seq_file = NULL;
296 static void osp_last_used_fini(const struct lu_env *env, struct osp_device *d)
298 /* release last_used file */
299 if (d->opd_last_used_oid_file != NULL) {
300 lu_object_put(env, &d->opd_last_used_oid_file->do_lu);
301 d->opd_last_used_oid_file = NULL;
304 if (d->opd_last_used_seq_file != NULL) {
305 lu_object_put(env, &d->opd_last_used_seq_file->do_lu);
306 d->opd_last_used_seq_file = NULL;
310 static int osp_disconnect(struct osp_device *d)
312 struct obd_import *imp;
315 imp = d->opd_obd->u.cli.cl_import;
317 /* Mark import deactivated now, so we don't try to reconnect if any
318 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
319 * fully deactivate the import, or that would drop all requests. */
320 LASSERT(imp != NULL);
321 spin_lock(&imp->imp_lock);
322 imp->imp_deactive = 1;
323 spin_unlock(&imp->imp_lock);
325 ptlrpc_deactivate_import(imp);
327 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
328 * delete it regardless. (It's safe to delete an import that was
330 (void)ptlrpc_pinger_del_import(imp);
332 rc = ptlrpc_disconnect_import(imp, 0);
334 CERROR("%s: can't disconnect: rc = %d\n",
335 d->opd_obd->obd_name, rc);
337 ptlrpc_invalidate_import(imp);
342 static int osp_shutdown(const struct lu_env *env, struct osp_device *d)
349 rc = osp_disconnect(d);
353 if (!d->opd_connect_mdt) {
354 /* stop precreate thread */
355 osp_precreate_fini(d);
357 /* release last_used file */
358 osp_last_used_fini(env, d);
361 obd_fid_fini(d->opd_obd);
366 static int osp_process_config(const struct lu_env *env,
367 struct lu_device *dev, struct lustre_cfg *lcfg)
369 struct osp_device *d = lu2osp_dev(dev);
370 struct lprocfs_static_vars lvars = { 0 };
375 switch (lcfg->lcfg_command) {
376 case LCFG_PRE_CLEANUP:
377 rc = osp_disconnect(d);
380 lu_dev_del_linkage(dev->ld_site, dev);
381 rc = osp_shutdown(env, d);
384 lprocfs_osp_init_vars(&lvars);
387 rc = class_process_proc_param(PARAM_OSC, lvars.obd_vars,
392 /* class_process_proc_param() haven't found matching
393 * parameter and returned ENOSYS so that layer(s)
394 * below could use that. But OSP is the bottom, so
396 CERROR("%s: unknown param %s\n",
397 (char *)lustre_cfg_string(lcfg, 0),
398 (char *)lustre_cfg_string(lcfg, 1));
403 CERROR("%s: unknown command %u\n",
404 (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
412 static int osp_recovery_complete(const struct lu_env *env,
413 struct lu_device *dev)
415 struct osp_device *osp = lu2osp_dev(dev);
419 osp->opd_recovery_completed = 1;
421 if (!osp->opd_connect_mdt && osp->opd_pre != NULL)
422 wake_up(&osp->opd_pre_waitq);
426 const struct lu_device_operations osp_lu_ops = {
427 .ldo_object_alloc = osp_object_alloc,
428 .ldo_process_config = osp_process_config,
429 .ldo_recovery_complete = osp_recovery_complete,
433 * provides with statfs from corresponded OST
436 static int osp_statfs(const struct lu_env *env, struct dt_device *dev,
437 struct obd_statfs *sfs)
439 struct osp_device *d = dt2osp_dev(dev);
443 if (unlikely(d->opd_imp_active == 0))
446 if (d->opd_pre == NULL)
449 /* return recently updated data */
450 *sfs = d->opd_statfs;
453 * layer above osp (usually lod) can use ffree to estimate
454 * how many objects are available for immediate creation
456 spin_lock(&d->opd_pre_lock);
457 LASSERTF(fid_seq(&d->opd_pre_last_created_fid) ==
458 fid_seq(&d->opd_pre_used_fid),
459 "last_created "DFID", next_fid "DFID"\n",
460 PFID(&d->opd_pre_last_created_fid),
461 PFID(&d->opd_pre_used_fid));
462 sfs->os_fprecreated = fid_oid(&d->opd_pre_last_created_fid) -
463 fid_oid(&d->opd_pre_used_fid);
464 sfs->os_fprecreated -= d->opd_pre_reserved;
465 spin_unlock(&d->opd_pre_lock);
467 LASSERT(sfs->os_fprecreated <= OST_MAX_PRECREATE * 2);
469 CDEBUG(D_OTHER, "%s: "LPU64" blocks, "LPU64" free, "LPU64" avail, "
470 LPU64" files, "LPU64" free files\n", d->opd_obd->obd_name,
471 sfs->os_blocks, sfs->os_bfree, sfs->os_bavail,
472 sfs->os_files, sfs->os_ffree);
476 static int osp_sync(const struct lu_env *env, struct dt_device *dev)
481 * XXX: wake up sync thread, command it to start flushing asap?
487 const struct dt_device_operations osp_dt_ops = {
488 .dt_statfs = osp_statfs,
490 .dt_trans_create = osp_trans_create,
491 .dt_trans_start = osp_trans_start,
492 .dt_trans_stop = osp_trans_stop,
495 static int osp_connect_to_osd(const struct lu_env *env, struct osp_device *m,
498 struct obd_connect_data *data = NULL;
499 struct obd_device *obd;
504 LASSERT(m->opd_storage_exp == NULL);
510 obd = class_name2obd(nextdev);
512 CERROR("%s: can't locate next device: %s\n",
513 m->opd_obd->obd_name, nextdev);
514 GOTO(out, rc = -ENOTCONN);
517 rc = obd_connect(env, &m->opd_storage_exp, obd, &obd->obd_uuid, data,
520 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
521 m->opd_obd->obd_name, nextdev, rc);
525 m->opd_dt_dev.dd_lu_dev.ld_site =
526 m->opd_storage_exp->exp_obd->obd_lu_dev->ld_site;
527 LASSERT(m->opd_dt_dev.dd_lu_dev.ld_site);
528 m->opd_storage = lu2dt_dev(m->opd_storage_exp->exp_obd->obd_lu_dev);
535 static int osp_init0(const struct lu_env *env, struct osp_device *m,
536 struct lu_device_type *ldt, struct lustre_cfg *cfg)
538 struct obd_device *obd;
539 struct obd_import *imp;
541 char *src, *tgt, *mdt, *osdname = NULL;
547 mutex_init(&m->opd_async_requests_mutex);
549 obd = class_name2obd(lustre_cfg_string(cfg, 0));
551 CERROR("Cannot find obd with name %s\n",
552 lustre_cfg_string(cfg, 0));
557 /* There is no record in the MDT configuration for the local disk
558 * device, so we have to extract this from elsewhere in the profile.
559 * The only information we get at setup is from the OSC records:
560 * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
561 * Note that 1.8 generated configs are missing the -MDTxxxx part.
562 * We need to reconstruct the name of the underlying OSD from this:
563 * {fsname}-{svname}-osd, for example "lustre-MDT0000-osd". We
564 * also need to determine the OST index from this - will be used
565 * to calculate the offset in shared lov_objids file later */
567 src = lustre_cfg_string(cfg, 0);
571 tgt = strrchr(src, '-');
573 CERROR("%s: invalid target name %s\n",
574 m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
578 if (strncmp(tgt, "-osc", 4) == 0) {
579 /* Old OSC name fsname-OSTXXXX-osc */
580 for (tgt--; tgt > src && *tgt != '-'; tgt--)
583 CERROR("%s: invalid target name %s\n",
584 m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
588 if (strncmp(tgt, "-OST", 4) != 0) {
589 CERROR("%s: invalid target name %s\n",
590 m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
594 idx = simple_strtol(tgt + 4, &mdt, 16);
595 if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
596 CERROR("%s: invalid OST index in '%s'\n",
597 m->opd_obd->obd_name, src);
604 /* New OSC name fsname-OSTXXXX-osc-MDTXXXX */
605 if (strncmp(tgt, "-MDT", 4) != 0 &&
606 strncmp(tgt, "-OST", 4) != 0) {
607 CERROR("%s: invalid target name %s\n",
608 m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
612 idx = simple_strtol(tgt + 4, &mdt, 16);
613 if (*mdt != '\0' || idx > INT_MAX || idx < 0) {
614 CERROR("%s: invalid OST index in '%s'\n",
615 m->opd_obd->obd_name, src);
619 /* Get MDT index from the name and set it to opd_group,
620 * which will be used by OSP to connect with OST */
622 if (tgt - src <= 12) {
623 CERROR("%s: invalid mdt index retrieve from %s\n",
624 m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
628 if (strncmp(tgt - 12, "-MDT", 4) == 0)
629 m->opd_connect_mdt = 1;
631 idx = simple_strtol(tgt - 8, &mdt, 16);
632 if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
633 CERROR("%s: invalid OST index in '%s'\n",
634 m->opd_obd->obd_name, src);
639 idx = tgt - src - 12;
641 /* check the fsname length, and after this everything else will fit */
642 if (idx > MTI_NAME_MAXLEN) {
643 CERROR("%s: fsname too long in '%s'\n",
644 m->opd_obd->obd_name, src);
648 OBD_ALLOC(osdname, MAX_OBD_NAME);
652 memcpy(osdname, src, idx); /* copy just the fsname part */
655 mdt = strstr(mdt, "-MDT");
656 if (mdt == NULL) /* 1.8 configs don't have "-MDT0000" at the end */
657 strcat(osdname, "-MDT0000");
659 strcat(osdname, mdt);
660 strcat(osdname, "-osd");
661 CDEBUG(D_HA, "%s: connect to %s (%s)\n", obd->obd_name, osdname, src);
663 if (m->opd_connect_mdt) {
664 struct client_obd *cli = &m->opd_obd->u.cli;
666 OBD_ALLOC(cli->cl_rpc_lock, sizeof(*cli->cl_rpc_lock));
667 if (!cli->cl_rpc_lock)
668 GOTO(out_fini, rc = -ENOMEM);
669 osp_init_rpc_lock(cli->cl_rpc_lock);
672 m->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
673 m->opd_dt_dev.dd_ops = &osp_dt_ops;
674 obd->obd_lu_dev = &m->opd_dt_dev.dd_lu_dev;
676 rc = osp_connect_to_osd(env, m, osdname);
680 rc = ptlrpcd_addref();
682 GOTO(out_disconnect, rc);
684 rc = client_obd_setup(obd, cfg);
686 CERROR("%s: can't setup obd: %d\n", m->opd_obd->obd_name, rc);
692 rc = obd_fid_init(m->opd_obd, NULL, m->opd_connect_mdt ?
693 LUSTRE_SEQ_METADATA : LUSTRE_SEQ_DATA);
695 CERROR("%s: fid init error: rc = %d\n",
696 m->opd_obd->obd_name, rc);
700 if (!m->opd_connect_mdt) {
701 /* Initialize last id from the storage - will be
702 * used in orphan cleanup. */
703 rc = osp_last_used_init(env, m);
708 /* Initialize precreation thread, it handles new
709 * connections as well. */
710 rc = osp_init_precreate(m);
712 GOTO(out_last_used, rc);
716 * Initialize synhronization mechanism taking
717 * care of propogating changes to OST in near
718 * transactional manner.
720 rc = osp_sync_init(env, m);
722 GOTO(out_precreat, rc);
725 * Initiate connect to OST
727 ll_generate_random_uuid(uuid);
728 class_uuid_unparse(uuid, &m->opd_cluuid);
730 imp = obd->u.cli.cl_import;
732 rc = ptlrpc_init_import(imp);
736 OBD_FREE(osdname, MAX_OBD_NAME);
740 /* stop sync thread */
743 /* stop precreate thread */
744 if (!m->opd_connect_mdt)
745 osp_precreate_fini(m);
747 if (!m->opd_connect_mdt)
748 osp_last_used_fini(env, m);
750 ptlrpc_lprocfs_unregister_obd(obd);
751 lprocfs_obd_cleanup(obd);
753 lprocfs_remove(&m->opd_symlink);
754 client_obd_cleanup(obd);
758 if (m->opd_connect_mdt) {
759 struct client_obd *cli = &m->opd_obd->u.cli;
760 if (cli->cl_rpc_lock != NULL) {
761 OBD_FREE_PTR(cli->cl_rpc_lock);
762 cli->cl_rpc_lock = NULL;
765 obd_disconnect(m->opd_storage_exp);
768 OBD_FREE(osdname, MAX_OBD_NAME);
772 static struct lu_device *osp_device_free(const struct lu_env *env,
773 struct lu_device *lu)
775 struct osp_device *m = lu2osp_dev(lu);
779 if (atomic_read(&lu->ld_ref) && lu->ld_site) {
780 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
781 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
783 dt_device_fini(&m->opd_dt_dev);
788 static struct lu_device *osp_device_alloc(const struct lu_env *env,
789 struct lu_device_type *t,
790 struct lustre_cfg *lcfg)
792 struct osp_device *m;
797 l = ERR_PTR(-ENOMEM);
802 dt_device_init(&m->opd_dt_dev, t);
803 rc = osp_init0(env, m, t, lcfg);
805 osp_device_free(env, l);
812 static struct lu_device *osp_device_fini(const struct lu_env *env,
815 struct osp_device *m = lu2osp_dev(d);
816 struct obd_import *imp;
821 if (m->opd_async_requests != NULL) {
822 out_destroy_update_req(m->opd_async_requests);
823 m->opd_async_requests = NULL;
826 if (m->opd_storage_exp)
827 obd_disconnect(m->opd_storage_exp);
829 imp = m->opd_obd->u.cli.cl_import;
831 if (imp->imp_rq_pool) {
832 ptlrpc_free_rq_pool(imp->imp_rq_pool);
833 imp->imp_rq_pool = NULL;
837 lprocfs_remove(&m->opd_symlink);
840 ptlrpc_lprocfs_unregister_obd(m->opd_obd);
841 lprocfs_obd_cleanup(m->opd_obd);
843 if (m->opd_connect_mdt) {
844 struct client_obd *cli = &m->opd_obd->u.cli;
845 if (cli->cl_rpc_lock != NULL) {
846 OBD_FREE_PTR(cli->cl_rpc_lock);
847 cli->cl_rpc_lock = NULL;
851 rc = client_obd_cleanup(m->opd_obd);
852 LASSERTF(rc == 0, "error %d\n", rc);
859 static int osp_reconnect(const struct lu_env *env,
860 struct obd_export *exp, struct obd_device *obd,
861 struct obd_uuid *cluuid,
862 struct obd_connect_data *data,
868 static int osp_prepare_fid_client(struct osp_device *osp)
870 LASSERT(osp->opd_obd->u.cli.cl_seq != NULL);
871 if (osp->opd_obd->u.cli.cl_seq->lcs_exp != NULL)
874 LASSERT(osp->opd_exp != NULL);
875 osp->opd_obd->u.cli.cl_seq->lcs_exp =
876 class_export_get(osp->opd_exp);
881 * we use exports to track all LOD users
883 static int osp_obd_connect(const struct lu_env *env, struct obd_export **exp,
884 struct obd_device *obd, struct obd_uuid *cluuid,
885 struct obd_connect_data *data, void *localdata)
887 struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
888 struct obd_connect_data *ocd;
889 struct obd_import *imp;
890 struct lustre_handle conn;
895 CDEBUG(D_CONFIG, "connect #%d\n", osp->opd_connects);
897 rc = class_connect(&conn, obd, cluuid);
901 *exp = class_conn2export(&conn);
902 /* Why should there ever be more than 1 connect? */
904 LASSERT(osp->opd_connects == 1);
908 imp = osp->opd_obd->u.cli.cl_import;
909 imp->imp_dlm_handle = conn;
911 LASSERT(data != NULL);
912 LASSERT(data->ocd_connect_flags & OBD_CONNECT_INDEX);
913 ocd = &imp->imp_connect_data;
916 imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
918 ocd->ocd_version = LUSTRE_VERSION_CODE;
919 ocd->ocd_index = data->ocd_index;
920 imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
922 rc = ptlrpc_connect_import(imp);
924 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
928 ptlrpc_pinger_add_import(imp);
934 * once last export (we don't count self-export) disappeared
935 * osp can be released
937 static int osp_obd_disconnect(struct obd_export *exp)
939 struct obd_device *obd = exp->exp_obd;
940 struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
944 /* Only disconnect the underlying layers on the final disconnect. */
945 LASSERT(osp->opd_connects == 1);
948 rc = class_disconnect(exp);
950 CERROR("%s: class disconnect error: rc = %d\n",
955 /* destroy the device */
956 class_manual_cleanup(obd);
962 * lprocfs helpers still use OBD API, let's keep obd_statfs() support
964 static int osp_obd_statfs(const struct lu_env *env, struct obd_export *exp,
965 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
967 struct obd_statfs *msfs;
968 struct ptlrpc_request *req;
969 struct obd_import *imp = NULL;
974 /* Since the request might also come from lprocfs, so we need
975 * sync this with client_disconnect_export Bug15684 */
976 down_read(&exp->exp_obd->u.cli.cl_sem);
977 if (exp->exp_obd->u.cli.cl_import)
978 imp = class_import_get(exp->exp_obd->u.cli.cl_import);
979 up_read(&exp->exp_obd->u.cli.cl_sem);
983 /* We could possibly pass max_age in the request (as an absolute
984 * timestamp or a "seconds.usec ago") so the target can avoid doing
985 * extra calls into the filesystem if that isn't necessary (e.g.
986 * during mount that would help a bit). Having relative timestamps
987 * is not so great if request processing is slow, while absolute
988 * timestamps are not ideal because they need time synchronization. */
989 req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
991 class_import_put(imp);
996 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
998 ptlrpc_request_free(req);
1001 ptlrpc_request_set_replen(req);
1002 req->rq_request_portal = OST_CREATE_PORTAL;
1003 ptlrpc_at_set_req_timeout(req);
1005 if (flags & OBD_STATFS_NODELAY) {
1006 /* procfs requests not want stat in wait for avoid deadlock */
1007 req->rq_no_resend = 1;
1008 req->rq_no_delay = 1;
1011 rc = ptlrpc_queue_wait(req);
1015 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1017 GOTO(out, rc = -EPROTO);
1023 ptlrpc_req_finished(req);
1027 static int osp_import_event(struct obd_device *obd, struct obd_import *imp,
1028 enum obd_import_event event)
1030 struct osp_device *d = lu2osp_dev(obd->obd_lu_dev);
1033 case IMP_EVENT_DISCON:
1034 d->opd_got_disconnected = 1;
1035 d->opd_imp_connected = 0;
1036 if (d->opd_connect_mdt)
1039 if (d->opd_pre != NULL) {
1040 osp_pre_update_status(d, -ENODEV);
1041 wake_up(&d->opd_pre_waitq);
1044 CDEBUG(D_HA, "got disconnected\n");
1046 case IMP_EVENT_INACTIVE:
1047 d->opd_imp_active = 0;
1048 if (d->opd_connect_mdt)
1051 if (d->opd_pre != NULL) {
1052 osp_pre_update_status(d, -ENODEV);
1053 wake_up(&d->opd_pre_waitq);
1056 CDEBUG(D_HA, "got inactive\n");
1058 case IMP_EVENT_ACTIVE:
1059 d->opd_imp_active = 1;
1061 if (osp_prepare_fid_client(d) != 0)
1064 if (d->opd_got_disconnected)
1065 d->opd_new_connection = 1;
1066 d->opd_imp_connected = 1;
1067 d->opd_imp_seen_connected = 1;
1068 if (d->opd_connect_mdt)
1071 if (d->opd_pre != NULL)
1072 wake_up(&d->opd_pre_waitq);
1074 __osp_sync_check_for_work(d);
1075 CDEBUG(D_HA, "got connected\n");
1077 case IMP_EVENT_INVALIDATE:
1078 if (obd->obd_namespace == NULL)
1080 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
1083 case IMP_EVENT_DEACTIVATE:
1084 case IMP_EVENT_ACTIVATE:
1087 CERROR("%s: unsupported import event: %#x\n",
1088 obd->obd_name, event);
1093 static int osp_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1094 void *karg, void *uarg)
1096 struct obd_device *obd = exp->exp_obd;
1097 struct osp_device *d;
1098 struct obd_ioctl_data *data = karg;
1103 LASSERT(obd->obd_lu_dev);
1104 d = lu2osp_dev(obd->obd_lu_dev);
1105 LASSERT(d->opd_dt_dev.dd_ops == &osp_dt_ops);
1107 if (!try_module_get(THIS_MODULE)) {
1108 CERROR("%s: can't get module. Is it alive?", obd->obd_name);
1113 case OBD_IOC_CLIENT_RECOVER:
1114 rc = ptlrpc_recover_import(obd->u.cli.cl_import,
1115 data->ioc_inlbuf1, 0);
1119 case IOC_OSC_SET_ACTIVE:
1120 rc = ptlrpc_set_import_active(obd->u.cli.cl_import,
1123 case OBD_IOC_PING_TARGET:
1124 rc = ptlrpc_obd_ping(obd);
1127 CERROR("%s: unrecognized ioctl %#x by %s\n", obd->obd_name,
1128 cmd, current_comm());
1131 module_put(THIS_MODULE);
1135 static int osp_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1136 __u32 keylen, void *key, __u32 *vallen, void *val,
1137 struct lov_stripe_md *lsm)
1141 if (KEY_IS(KEY_OSP_CONNECTED)) {
1142 struct obd_device *obd = exp->exp_obd;
1143 struct osp_device *osp;
1145 if (!obd->obd_set_up || obd->obd_stopping)
1148 osp = lu2osp_dev(obd->obd_lu_dev);
1151 * 1.8/2.0 behaviour is that OST being connected once at least
1152 * is considered "healthy". and one "healthy" OST is enough to
1153 * allow lustre clients to connect to MDS
1155 RETURN(!osp->opd_imp_seen_connected);
1161 int osp_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1162 struct lu_fid *fid, struct md_op_data *op_data)
1164 struct client_obd *cli = &exp->exp_obd->u.cli;
1165 struct osp_device *osp = lu2osp_dev(exp->exp_obd->obd_lu_dev);
1166 struct lu_client_seq *seq = cli->cl_seq;
1169 LASSERT(osp->opd_obd->u.cli.cl_seq != NULL);
1170 /* Sigh, fid client is not ready yet */
1171 if (osp->opd_obd->u.cli.cl_seq->lcs_exp == NULL)
1174 RETURN(seq_client_alloc_fid(env, seq, fid));
1177 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
1178 LU_KEY_INIT_FINI(osp, struct osp_thread_info);
1179 static void osp_key_exit(const struct lu_context *ctx,
1180 struct lu_context_key *key, void *data)
1182 struct osp_thread_info *info = data;
1184 info->osi_attr.la_valid = 0;
1187 struct lu_context_key osp_thread_key = {
1188 .lct_tags = LCT_MD_THREAD,
1189 .lct_init = osp_key_init,
1190 .lct_fini = osp_key_fini,
1191 .lct_exit = osp_key_exit
1194 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
1195 LU_KEY_INIT_FINI(osp_txn, struct osp_txn_info);
1197 struct lu_context_key osp_txn_key = {
1198 .lct_tags = LCT_OSP_THREAD | LCT_TX_HANDLE,
1199 .lct_init = osp_txn_key_init,
1200 .lct_fini = osp_txn_key_fini
1202 LU_TYPE_INIT_FINI(osp, &osp_thread_key, &osp_txn_key);
1204 static struct lu_device_type_operations osp_device_type_ops = {
1205 .ldto_init = osp_type_init,
1206 .ldto_fini = osp_type_fini,
1208 .ldto_start = osp_type_start,
1209 .ldto_stop = osp_type_stop,
1211 .ldto_device_alloc = osp_device_alloc,
1212 .ldto_device_free = osp_device_free,
1214 .ldto_device_fini = osp_device_fini
1217 static struct lu_device_type osp_device_type = {
1218 .ldt_tags = LU_DEVICE_DT,
1219 .ldt_name = LUSTRE_OSP_NAME,
1220 .ldt_ops = &osp_device_type_ops,
1221 .ldt_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
1224 static struct obd_ops osp_obd_device_ops = {
1225 .o_owner = THIS_MODULE,
1226 .o_add_conn = client_import_add_conn,
1227 .o_del_conn = client_import_del_conn,
1228 .o_reconnect = osp_reconnect,
1229 .o_connect = osp_obd_connect,
1230 .o_disconnect = osp_obd_disconnect,
1231 .o_get_info = osp_obd_get_info,
1232 .o_import_event = osp_import_event,
1233 .o_iocontrol = osp_iocontrol,
1234 .o_statfs = osp_obd_statfs,
1235 .o_fid_init = client_fid_init,
1236 .o_fid_fini = client_fid_fini,
1237 .o_fid_alloc = osp_fid_alloc,
1240 struct llog_operations osp_mds_ost_orig_logops;
1242 static int __init osp_mod_init(void)
1244 struct lprocfs_static_vars lvars;
1245 struct obd_type *type;
1248 rc = lu_kmem_init(osp_caches);
1252 lprocfs_osp_init_vars(&lvars);
1254 rc = class_register_type(&osp_obd_device_ops, NULL, true, NULL,
1255 #ifndef HAVE_ONLY_PROCFS_SEQ
1258 LUSTRE_OSP_NAME, &osp_device_type);
1260 /* create "osc" entry in procfs for compatibility purposes */
1262 lu_kmem_fini(osp_caches);
1266 lprocfs_lwp_init_vars(&lvars);
1268 rc = class_register_type(&lwp_obd_device_ops, NULL, true, NULL,
1269 #ifndef HAVE_ONLY_PROCFS_SEQ
1272 LUSTRE_LWP_NAME, &lwp_device_type);
1274 class_unregister_type(LUSTRE_OSP_NAME);
1275 lu_kmem_fini(osp_caches);
1279 /* Note: add_rec/delcare_add_rec will be only used by catalogs */
1280 osp_mds_ost_orig_logops = llog_osd_ops;
1281 osp_mds_ost_orig_logops.lop_add = llog_cat_add_rec;
1282 osp_mds_ost_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1284 /* create "osc" entry in procfs for compatibility purposes */
1285 type = class_search_type(LUSTRE_OSC_NAME);
1286 if (type != NULL && type->typ_procroot != NULL)
1289 type = class_search_type(LUSTRE_OSP_NAME);
1290 type->typ_procsym = lprocfs_register("osc", proc_lustre_root,
1292 if (IS_ERR(type->typ_procsym))
1293 CERROR("osp: can't create compat entry \"osc\": %d\n",
1294 (int) PTR_ERR(type->typ_procsym));
1298 static void __exit osp_mod_exit(void)
1300 lprocfs_try_remove_proc_entry("osc", proc_lustre_root);
1302 class_unregister_type(LUSTRE_LWP_NAME);
1303 class_unregister_type(LUSTRE_OSP_NAME);
1304 lu_kmem_fini(osp_caches);
1307 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
1308 MODULE_DESCRIPTION("Lustre OST Proxy Device ("LUSTRE_OSP_NAME")");
1309 MODULE_LICENSE("GPL");
1311 cfs_module(osp, LUSTRE_VERSION_STRING, osp_mod_init, osp_mod_exit);