From 8d24aa6b8e662a2aa52af4ee652c9f01c2c26cc4 Mon Sep 17 00:00:00 2001 From: Sebastien Buisson Date: Wed, 10 May 2023 14:49:00 +0200 Subject: [PATCH] LU-16816 obdclass: make import_event more robust Make mdc_import_event and osc_import_event more robust, by not assuming input variables can be dereferenced. Test-Parameters: trivial Signed-off-by: Sebastien Buisson Change-Id: I31a6477d58b7bb9a557ea561f7b0fa3fbcae5762 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50915 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Patrick Farrell Reviewed-by: Oleg Drokin --- lustre/include/obd_class.h | 3 ++ lustre/mdc/mdc_request.c | 10 ++++- lustre/osc/osc_request.c | 103 ++++++++++++++++++++++++--------------------- 3 files changed, 65 insertions(+), 51 deletions(-) diff --git a/lustre/include/obd_class.h b/lustre/include/obd_class.h index 8571e12..0079ae4 100644 --- a/lustre/include/obd_class.h +++ b/lustre/include/obd_class.h @@ -1268,6 +1268,9 @@ static inline int obd_notify_observer(struct obd_device *observer, int rc2 = 0; struct obd_notify_upcall *onu; + if (WARN_ON_ONCE(!observer)) + return -ENODEV; + if (observer->obd_observer) rc = obd_notify(observer->obd_observer, observed, ev); diff --git a/lustre/mdc/mdc_request.c b/lustre/mdc/mdc_request.c index 086d13e..8485fc5 100644 --- a/lustre/mdc/mdc_request.c +++ b/lustre/mdc/mdc_request.c @@ -2711,10 +2711,16 @@ static int mdc_rmfid(struct obd_export *exp, struct fid_array *fa, static int mdc_import_event(struct obd_device *obd, struct obd_import *imp, enum obd_import_event event) { - struct client_obd *cli = &obd->u.cli; + struct client_obd *cli; int rc = 0; - LASSERT(imp->imp_obd == obd); + ENTRY; + if (WARN_ON_ONCE(!obd || !imp || imp->imp_obd != obd)) + RETURN(-ENODEV); + + cli = &obd->u.cli; + if (!cli) + RETURN(-ENODEV); switch (event) { case IMP_EVENT_DISCON: diff --git a/lustre/osc/osc_request.c b/lustre/osc/osc_request.c index 5e7757f..3140747 100644 --- a/lustre/osc/osc_request.c +++ b/lustre/osc/osc_request.c @@ -3598,38 +3598,40 @@ int osc_ldlm_resource_invalidate(struct cfs_hash *hs, struct cfs_hash_bd *bd, } EXPORT_SYMBOL(osc_ldlm_resource_invalidate); -static int osc_import_event(struct obd_device *obd, - struct obd_import *imp, - enum obd_import_event event) +static int osc_import_event(struct obd_device *obd, struct obd_import *imp, + enum obd_import_event event) { - struct client_obd *cli; - int rc = 0; - - ENTRY; - LASSERT(imp->imp_obd == obd); + struct client_obd *cli; + int rc = 0; - switch (event) { - case IMP_EVENT_DISCON: { - cli = &obd->u.cli; + ENTRY; + if (WARN_ON_ONCE(!obd || !imp || imp->imp_obd != obd)) + RETURN(-ENODEV); + + switch (event) { + case IMP_EVENT_DISCON: { + cli = &obd->u.cli; + if (!cli) + RETURN(-ENODEV); spin_lock(&cli->cl_loi_list_lock); cli->cl_avail_grant = 0; cli->cl_lost_grant = 0; spin_unlock(&cli->cl_loi_list_lock); - break; - } - case IMP_EVENT_INACTIVE: { + break; + } + case IMP_EVENT_INACTIVE: { rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE); - break; - } - case IMP_EVENT_INVALIDATE: { - struct ldlm_namespace *ns = obd->obd_namespace; - struct lu_env *env; - __u16 refcheck; + break; + } + case IMP_EVENT_INVALIDATE: { + struct ldlm_namespace *ns = obd->obd_namespace; + struct lu_env *env; + __u16 refcheck; ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY); - env = cl_env_get(&refcheck); - if (!IS_ERR(env)) { + env = cl_env_get(&refcheck); + if (!IS_ERR(env)) { osc_io_unplug(env, &obd->u.cli, NULL); cfs_hash_for_each_nolock(ns->ns_rs_hash, @@ -3638,40 +3640,43 @@ static int osc_import_event(struct obd_device *obd, cl_env_put(env, &refcheck); ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY); - } else - rc = PTR_ERR(env); - break; - } - case IMP_EVENT_ACTIVE: { + } else { + rc = PTR_ERR(env); + } + break; + } + case IMP_EVENT_ACTIVE: { rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE); - break; - } - case IMP_EVENT_OCD: { - struct obd_connect_data *ocd = &imp->imp_connect_data; + break; + } + case IMP_EVENT_OCD: { + struct obd_connect_data *ocd = &imp->imp_connect_data; - if (ocd->ocd_connect_flags & OBD_CONNECT_GRANT) - osc_init_grant(&obd->u.cli, ocd); + if (ocd->ocd_connect_flags & OBD_CONNECT_GRANT) + osc_init_grant(&obd->u.cli, ocd); - /* See bug 7198 */ - if (ocd->ocd_connect_flags & OBD_CONNECT_REQPORTAL) - imp->imp_client->cli_request_portal =OST_REQUEST_PORTAL; + /* See bug 7198 */ + if (ocd->ocd_connect_flags & OBD_CONNECT_REQPORTAL) + imp->imp_client->cli_request_portal = + OST_REQUEST_PORTAL; rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD); - break; - } - case IMP_EVENT_DEACTIVATE: { + break; + } + case IMP_EVENT_DEACTIVATE: { rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DEACTIVATE); - break; - } - case IMP_EVENT_ACTIVATE: { + break; + } + case IMP_EVENT_ACTIVATE: { rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVATE); - break; - } - default: - CERROR("Unknown import event %d\n", event); - LBUG(); - } - RETURN(rc); + break; + } + default: + CERROR("%s: Unknown import event %d: rc = %d\n", + obd->obd_name, event, -EINVAL); + LBUG(); + } + RETURN(rc); } /** -- 1.8.3.1