File 0058-LU-16634-obdclass-improve-iocontrol-error-messages.patch of Package lustre_2_12

From 1f4825eff026321a8e769739868d4d482026c3ee Mon Sep 17 00:00:00 2001
From: Andreas Dilger <adilger@whamcloud.com>
Date: Sun, 19 Mar 2023 17:09:24 -0600
Subject: [PATCH] LU-16634 obdclass: improve iocontrol error messages

Add consistent CDEBUG() messages for iocontrol handlers.
Add helpers OBD_IOC_ERROR() and OBD_IOC_DEBUG() to print the
iocontrol parameters consistently in case of an error.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: I1a0d2f839949debf346aa15c65b0f407e2ce7057
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50334
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: Vitaliy Kuznetsov <vkuznetsov@ddn.com>
Reviewed-by: jsimmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
---
 lustre/include/obd_class.h   |    7 +
 lustre/llite/dir.c           |    6 
 lustre/llite/file.c          |  110 +++++++++--------
 lustre/lmv/lmv_obd.c         |    9 -
 lustre/lov/lov_obd.c         |   24 ++-
 lustre/mdc/mdc_request.c     |    7 -
 lustre/mdd/mdd_device.c      |   30 ++--
 lustre/mdt/mdt_handler.c     |   44 +++---
 lustre/mgs/mgs_handler.c     |   89 +++++---------
 lustre/obdclass/class_obd.c  |  272 ++++++++++++++++++++++++-------------------
 lustre/obdecho/echo_client.c |   32 ++---
 lustre/ofd/ofd_obd.c         |   12 -
 lustre/osc/osc_request.c     |    7 -
 lustre/osp/osp_dev.c         |   16 +-
 14 files changed, 356 insertions(+), 309 deletions(-)

--- a/lustre/include/obd_class.h
+++ b/lustre/include/obd_class.h
@@ -1882,4 +1882,11 @@ extern struct miscdevice obd_psdev;
 int obd_ioctl_getdata(char **buf, int *len, void __user *arg);
 int class_procfs_init(void);
 int class_procfs_clean(void);
+int obd_ioctl_msg(const char *file, const char *func, int line, int level,
+		  const char *name, unsigned int cmd, const char *msg, int rc);
+#define OBD_IOC_DEBUG(level, dev, cmd, msg, rc)	\
+	obd_ioctl_msg(__FILE__, __func__, __LINE__, level, dev, cmd, msg, rc)
+#define OBD_IOC_ERROR(dev, cmd, msg, rc)	\
+	obd_ioctl_msg(__FILE__, __func__, __LINE__, D_ERROR, dev, cmd, msg, rc)
+
 #endif /* __LINUX_OBD_CLASS_H */
--- a/lustre/llite/dir.c
+++ b/lustre/llite/dir.c
@@ -1366,8 +1366,8 @@ static long ll_dir_ioctl(struct file *fi
 	int rc = 0;
 	ENTRY;
 
-	CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
-	       PFID(ll_inode2fid(inode)), inode, cmd);
+	CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
+	       PFID(ll_inode2fid(inode)), inode, cmd, arg);
 
 	/* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
 	if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
@@ -1845,7 +1845,7 @@ out_req:
 		rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
 				  KEY_TGT_COUNT, &vallen, &count);
 		if (rc) {
-			CERROR("get target count failed: %d\n", rc);
+			CERROR("get target count failed: rc = %d\n", rc);
 			RETURN(rc);
 		}
 
--- a/lustre/llite/file.c
+++ b/lustre/llite/file.c
@@ -3331,8 +3331,8 @@ ll_file_ioctl(struct file *file, unsigne
 	int flags, rc;
 	ENTRY;
 
-	CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
-	       PFID(ll_inode2fid(inode)), inode, cmd);
+	CDEBUG(D_VFSTRACE|D_IOCTL, "VFS Op:inode="DFID"(%pK) cmd=%x arg=%lx\n",
+	       PFID(ll_inode2fid(inode)), inode, cmd, arg);
 	ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
 
 	/* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
@@ -3355,9 +3355,11 @@ ll_file_ioctl(struct file *file, unsigne
 		if (cmd == LL_IOC_SETFLAGS) {
 			if ((flags & LL_FILE_IGNORE_LOCK) &&
 			    !(file->f_flags & O_DIRECT)) {
-				CERROR("%s: unable to disable locking on "
-				       "non-O_DIRECT file\n", current->comm);
-				RETURN(-EINVAL);
+				rc = -EINVAL;
+				CERROR("%s: unable to disable locking on non-O_DIRECT file "DFID": rc = %d\n",
+				       current->comm, PFID(ll_inode2fid(inode)),
+				       rc);
+				RETURN(rc);
 			}
 
 			fd->fd_flags |= flags;
@@ -3956,60 +3958,64 @@ ll_file_flock(struct file *file, int cmd
 #endif
 
 	switch (fl_type) {
-        case F_RDLCK:
-                einfo.ei_mode = LCK_PR;
-                break;
-        case F_UNLCK:
-                /* An unlock request may or may not have any relation to
-                 * existing locks so we may not be able to pass a lock handle
-                 * via a normal ldlm_lock_cancel() request. The request may even
-                 * unlock a byte range in the middle of an existing lock. In
-                 * order to process an unlock request we need all of the same
-                 * information that is given with a normal read or write record
-                 * lock request. To avoid creating another ldlm unlock (cancel)
-                 * message we'll treat a LCK_NL flock request as an unlock. */
-                einfo.ei_mode = LCK_NL;
-                break;
-        case F_WRLCK:
-                einfo.ei_mode = LCK_PW;
-                break;
-        default:
-		CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
-                RETURN (-ENOTSUPP);
-        }
+	case F_RDLCK:
+		einfo.ei_mode = LCK_PR;
+		break;
+	case F_UNLCK:
+		/* An unlock request may or may not have any relation to
+		 * existing locks so we may not be able to pass a lock handle
+		 * via a normal ldlm_lock_cancel() request. The request may even
+		 * unlock a byte range in the middle of an existing lock. In
+		 * order to process an unlock request we need all of the same
+		 * information that is given with a normal read or write record
+		 * lock request. To avoid creating another ldlm unlock (cancel)
+		 * message we'll treat a LCK_NL flock request as an unlock. */
+		einfo.ei_mode = LCK_NL;
+		break;
+	case F_WRLCK:
+		einfo.ei_mode = LCK_PW;
+		break;
+	default:
+		rc = -EINVAL;
+		CERROR("fcntl from '%s' unknown lock type=%d: rc = %d\n",
+		       current->comm, fl_type, rc);
+		RETURN(rc);
+	}
 
-        switch (cmd) {
-        case F_SETLKW:
+	switch (cmd) {
+	case F_SETLKW:
 #ifdef F_SETLKW64
-        case F_SETLKW64:
+	case F_SETLKW64:
 #endif
-                flags = 0;
-                break;
-        case F_SETLK:
+		flags = 0;
+		break;
+	case F_SETLK:
 #ifdef F_SETLK64
-        case F_SETLK64:
+	case F_SETLK64:
 #endif
-                flags = LDLM_FL_BLOCK_NOWAIT;
-                break;
-        case F_GETLK:
+		flags = LDLM_FL_BLOCK_NOWAIT;
+		break;
+	case F_GETLK:
 #ifdef F_GETLK64
-        case F_GETLK64:
+	case F_GETLK64:
 #endif
-                flags = LDLM_FL_TEST_LOCK;
-                break;
-        default:
-                CERROR("unknown fcntl lock command: %d\n", cmd);
-                RETURN (-EINVAL);
-        }
+		flags = LDLM_FL_TEST_LOCK;
+		break;
+	default:
+		rc = -EINVAL;
+		CERROR("fcntl from '%s' unknown lock command=%d: rc = %d\n",
+		       current->comm, cmd, rc);
+		RETURN(rc);
+	}
 
 	/* Save the old mode so that if the mode in the lock changes we
 	 * can decrement the appropriate reader or writer refcount. */
 	file_lock->fl_type = einfo.ei_mode;
 
-        op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
-                                     LUSTRE_OPC_ANY, NULL);
-        if (IS_ERR(op_data))
-                RETURN(PTR_ERR(op_data));
+	op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
+				     LUSTRE_OPC_ANY, NULL);
+	if (IS_ERR(op_data))
+		RETURN(PTR_ERR(op_data));
 
 	CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
 	       "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
@@ -4028,12 +4034,12 @@ ll_file_flock(struct file *file, int cmd
 	    !(flags & LDLM_FL_TEST_LOCK))
 		rc2  = locks_lock_file_wait(file, file_lock);
 #else
-        if ((file_lock->fl_flags & FL_FLOCK) &&
-            (rc == 0 || file_lock->fl_type == F_UNLCK))
+	if ((file_lock->fl_flags & FL_FLOCK) &&
+	    (rc == 0 || file_lock->fl_type == F_UNLCK))
 		rc2  = flock_lock_file_wait(file, file_lock);
-        if ((file_lock->fl_flags & FL_POSIX) &&
-            (rc == 0 || file_lock->fl_type == F_UNLCK) &&
-            !(flags & LDLM_FL_TEST_LOCK))
+	if ((file_lock->fl_flags & FL_POSIX) &&
+	    (rc == 0 || file_lock->fl_type == F_UNLCK) &&
+	    !(flags & LDLM_FL_TEST_LOCK))
 		rc2  = posix_lock_file_wait(file, file_lock);
 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
 
--- a/lustre/lmv/lmv_obd.c
+++ b/lustre/lmv/lmv_obd.c
@@ -903,7 +903,10 @@ static int lmv_iocontrol(unsigned int cm
 	int			 rc = 0;
 	int			 set = 0;
 	__u32			 count = lmv->desc.ld_tgt_count;
+
 	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       exp->exp_obd->obd_name, cmd, len, karg, uarg);
 
         if (count == 0)
                 RETURN(-ENOTTY);
@@ -1116,6 +1119,7 @@ hsm_req_err:
 	}
 	case LL_IOC_HSM_CT_START: {
 		struct lustre_kernelcomm *lk = karg;
+
 		if (lk->lk_flags & LK_FLG_STOP)
 			rc = lmv_hsm_ct_unregister(obddev, cmd, len, lk, uarg);
 		else
@@ -1137,9 +1141,8 @@ hsm_req_err:
 			err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
 			if (err) {
 				if (tgt->ltd_active) {
-					CERROR("error: iocontrol MDC %s on MDT"
-					       " idx %d cmd %x: err = %d\n",
-					       tgt->ltd_uuid.uuid, i, cmd, err);
+					OBD_IOC_ERROR(mdc_obd->obd_name, cmd,
+						      tgt->ltd_uuid.uuid, err);
 					if (!rc)
 						rc = err;
 				}
--- a/lustre/lov/lov_obd.c
+++ b/lustre/lov/lov_obd.c
@@ -977,6 +977,9 @@ static int lov_iocontrol(unsigned int cm
 	struct obd_uuid *uuidp;
 
 	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       exp->exp_obd->obd_name, cmd, len, karg, uarg);
+
 	switch (cmd) {
 	case IOC_OBD_STATFS: {
 		struct obd_ioctl_data *data = karg;
@@ -1136,17 +1139,16 @@ static int lov_iocontrol(unsigned int cm
 			err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
 					    len, karg, uarg);
 			if (err) {
-                                if (lov->lov_tgts[i]->ltd_active) {
-                                        CDEBUG(err == -ENOTTY ?
-                                               D_IOCTL : D_WARNING,
-                                               "iocontrol OSC %s on OST "
-                                               "idx %d cmd %x: err = %d\n",
-                                               lov_uuid2str(lov, i),
-                                               i, cmd, err);
-                                        if (!rc)
-                                                rc = err;
-                                }
-                        } else {
+				if (lov->lov_tgts[i]->ltd_active) {
+					OBD_IOC_DEBUG(err == -ENOTTY ?
+						      D_IOCTL : D_WARNING,
+						      obd->obd_name, cmd,
+						      lov_uuid2str(lov, i),
+						      err);
+					if (!rc)
+						rc = err;
+				}
+			} else {
                                 set = 1;
                         }
                 }
--- a/lustre/mdc/mdc_request.c
+++ b/lustre/mdc/mdc_request.c
@@ -2068,7 +2068,10 @@ static int mdc_iocontrol(unsigned int cm
 	struct obd_ioctl_data *data = karg;
 	struct obd_import *imp = obd->u.cli.cl_import;
 	int rc;
+
 	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       obd->obd_name, cmd, len, karg, uarg);
 
 	if (!try_module_get(THIS_MODULE)) {
 		CERROR("%s: cannot get module '%s'\n", obd->obd_name,
@@ -2172,8 +2175,8 @@ static int mdc_iocontrol(unsigned int cm
 		rc = mdc_ioc_swap_layouts(exp, karg);
 		GOTO(out, rc);
 	default:
-		CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
-		GOTO(out, rc = -ENOTTY);
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
 	}
 out:
 	module_put(THIS_MODULE);
--- a/lustre/mdd/mdd_device.c
+++ b/lustre/mdd/mdd_device.c
@@ -1920,12 +1920,16 @@ out:
  * \param karg - ioctl data, in kernel space
  */
 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
-                         unsigned int cmd, int len, void *karg)
+			 unsigned int cmd, int len, void *karg)
 {
 	struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
+	struct obd_device *obd = mdd2obd_dev(mdd);
 	struct obd_ioctl_data *data = karg;
-	int rc;
+	int rc = -EINVAL;
+
 	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK\n",
+	       obd->obd_name, cmd, len, karg);
 
 	/* Doesn't use obd_ioctl_data */
 	switch (cmd) {
@@ -1935,24 +1939,20 @@ static int mdd_iocontrol(const struct lu
 		if (unlikely(!barrier_entry(mdd->mdd_bottom)))
 			RETURN(-EINPROGRESS);
 
-		rc = mdd_changelog_clear(env, mdd, cs->cs_id,
-					 cs->cs_recno);
+		rc = mdd_changelog_clear(env, mdd, cs->cs_id, cs->cs_recno);
 		barrier_exit(mdd->mdd_bottom);
 		RETURN(rc);
 	}
 	case OBD_IOC_START_LFSCK: {
-		rc = lfsck_start(env, mdd->mdd_bottom,
-				 (struct lfsck_start_param *)karg);
+		rc = lfsck_start(env, mdd->mdd_bottom, karg);
 		RETURN(rc);
 	}
 	case OBD_IOC_STOP_LFSCK: {
-		rc = lfsck_stop(env, mdd->mdd_bottom,
-				(struct lfsck_stop *)karg);
+		rc = lfsck_stop(env, mdd->mdd_bottom, karg);
 		RETURN(rc);
 	}
 	case OBD_IOC_QUERY_LFSCK: {
-		rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
-				 (struct lfsck_query *)karg);
+		rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL, karg);
 		RETURN(rc);
 	}
 	}
@@ -1963,9 +1963,10 @@ static int mdd_iocontrol(const struct lu
 		RETURN(-EINVAL);
 	}
 	if (data->ioc_version != OBD_IOCTL_VERSION) {
-		CERROR("Bad magic %x != %x\n", data->ioc_version,
-		       OBD_IOCTL_VERSION);
-		RETURN(-EINVAL);
+		CERROR("%s: iocontrol from '%s' bad magic %x != %x: rc = %d\n",
+		       obd->obd_name, current->comm,
+		       data->ioc_version, OBD_IOCTL_VERSION, rc);
+		RETURN(rc);
 	}
 
 	switch (cmd) {
@@ -1992,7 +1993,8 @@ static int mdd_iocontrol(const struct lu
 		barrier_exit(mdd->mdd_bottom);
 		break;
 	default:
-		rc = -ENOTTY;
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
 	}
 
 	RETURN(rc);
--- a/lustre/mdt/mdt_handler.c
+++ b/lustre/mdt/mdt_handler.c
@@ -6777,29 +6777,32 @@ static int mdt_ioc_version_get(struct md
 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
 			 void *karg, void __user *uarg)
 {
-        struct lu_env      env;
-        struct obd_device *obd = exp->exp_obd;
-        struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
-        struct dt_device  *dt = mdt->mdt_bottom;
-        int rc;
-
-        ENTRY;
-        CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
-        rc = lu_env_init(&env, LCT_MD_THREAD);
-        if (rc)
-                RETURN(rc);
+	struct obd_device *obd = exp->exp_obd;
+	struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
+	struct dt_device *dt = mdt->mdt_bottom;
+	struct lu_env env;
+	int rc;
+
+	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       obd->obd_name, cmd, len, karg, uarg);
+
+	rc = lu_env_init(&env, LCT_MD_THREAD);
+	if (rc)
+		RETURN(rc);
 
 	switch (cmd) {
-	case OBD_IOC_SYNC:
-		rc = mdt_device_sync(&env, mdt);
-		break;
-	case OBD_IOC_SET_READONLY:
-		rc = dt_sync(&env, dt);
+		struct obd_ioctl_data *data = karg;
+
+		if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
+			CWARN("%s: Aborting MDT recovery\n", obd->obd_name);
+			obd->obd_abort_mdt_recovery = 1;
+			wake_up(&obd->obd_next_transno_waitq);
 		if (rc == 0)
 			rc = dt_ro(&env, dt);
 		break;
 	case OBD_IOC_ABORT_RECOVERY:
-		CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
+		CWARN("%s: Aborting client recovery\n", obd->obd_name);
 		obd->obd_abort_recovery = 1;
 		target_stop_recovery_thread(obd);
 		rc = 0;
@@ -6869,11 +6872,10 @@ static int mdt_iocontrol(unsigned int cm
 		rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
 				       &mti->mti_tmp_fid1);
 		break;
-	 }
+	}
 	default:
-		rc = -EOPNOTSUPP;
-		CERROR("%s: Not supported cmd = %d, rc = %d\n",
-			mdt_obd_name(mdt), cmd, rc);
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
 	}
 
         lu_env_fini(&env);
--- a/lustre/mgs/mgs_handler.c
+++ b/lustre/mgs/mgs_handler.c
@@ -994,80 +994,77 @@ out_pool:
 static int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
 			 void *karg, void __user *uarg)
 {
+	struct obd_device *obd = exp->exp_obd;
 	struct mgs_device *mgs = exp2mgs_dev(exp);
-        struct obd_ioctl_data *data = karg;
+	struct obd_ioctl_data *data = karg;
 	struct lu_env env;
-        int rc = 0;
+	int rc = -EINVAL;
 
-        ENTRY;
-        CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
+	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       obd->obd_name, cmd, len, karg, uarg);
 
 	rc = lu_env_init(&env, LCT_MG_THREAD);
 	if (rc)
 		RETURN(rc);
 
-        switch (cmd) {
-
+	rc = -EINVAL;
+	switch (cmd) {
 	case OBD_IOC_PARAM: {
 		struct lustre_cfg *lcfg;
 
 		if (data->ioc_type != LUSTRE_CFG_TYPE) {
-			CERROR("%s: unknown cfg record type: %d\n",
-			       mgs->mgs_obd->obd_name, data->ioc_type);
-			GOTO(out, rc = -EINVAL);
-                }
+			CERROR("%s: unknown cfg record type '%x': rc = %d\n",
+			       obd->obd_name, data->ioc_type, rc);
+			GOTO(out, rc);
+		}
 
-                OBD_ALLOC(lcfg, data->ioc_plen1);
-                if (lcfg == NULL)
+		OBD_ALLOC(lcfg, data->ioc_plen1);
+		if (lcfg == NULL)
 			GOTO(out, rc = -ENOMEM);
 		if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
-                        GOTO(out_free, rc = -EFAULT);
+			GOTO(out_free, rc = -EFAULT);
 
-                if (lcfg->lcfg_bufcount < 1)
-                        GOTO(out_free, rc = -EINVAL);
+		if (lcfg->lcfg_bufcount < 1)
+			GOTO(out_free, rc);
 
 		rc = mgs_set_param(&env, mgs, lcfg);
 		if (rc)
 			CERROR("%s: setparam err: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 out_free:
-                OBD_FREE(lcfg, data->ioc_plen1);
+		OBD_FREE(lcfg, data->ioc_plen1);
 		break;
-        }
-
-	case OBD_IOC_REPLACE_NIDS: {
+	}
+	case OBD_IOC_REPLACE_NIDS:
 		if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
-			rc = -EINVAL;
 			CERROR("%s: no device or fsname specified: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (data->ioc_inllen1 > MTI_NAME_MAXLEN) {
 			rc = -EOVERFLOW;
 			CERROR("%s: device or fsname is too long: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
-			rc = -EINVAL;
-			CERROR("%s: device or fsname is not NUL terminated: "
-			       "rc = %d\n", exp->exp_obd->obd_name, rc);
+			CERROR("%s: unterminated device or fsname: rc = %d\n",
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
-			rc = -EINVAL;
 			CERROR("%s: no NIDs specified: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
-			rc = -EINVAL;
-			CERROR("%s: NID list is not NUL terminated: "
-			       "rc = %d\n", exp->exp_obd->obd_name, rc);
+			CERROR("%s: NID list is not NUL terminated: rc = %d\n",
+			       obd->obd_name, rc);
 			break;
 		}
 
@@ -1076,30 +1073,26 @@ out_free:
 				      data->ioc_inlbuf2);
 		if (rc)
 			CERROR("%s: error replacing nids: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 
 		break;
-	}
-
-	case OBD_IOC_CLEAR_CONFIGS: {
+	case OBD_IOC_CLEAR_CONFIGS:
 		if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
-			rc = -EINVAL;
 			CERROR("%s: no device or fsname specified: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (data->ioc_inllen1 > MTI_NAME_MAXLEN) {
 			rc = -EOVERFLOW;
 			CERROR("%s: device or fsname is too long: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 			break;
 		}
 
 		if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
-			rc = -EINVAL;
-			CERROR("%s: device or fsname is not NUL terminated: "
-			       "rc = %d\n", exp->exp_obd->obd_name, rc);
+			CERROR("%s: unterminated device or fsname: rc = %d\n",
+			       obd->obd_name, rc);
 			break;
 		}
 
@@ -1107,32 +1100,25 @@ out_free:
 		rc = mgs_clear_configs(&env, mgs, data->ioc_inlbuf1);
 		if (rc)
 			CERROR("%s: error clearing config log: rc = %d\n",
-			       exp->exp_obd->obd_name, rc);
+			       obd->obd_name, rc);
 
 		break;
-	}
-
 	case OBD_IOC_POOL:
 		rc = mgs_iocontrol_pool(&env, mgs, data);
 		break;
-
 	case OBD_IOC_BARRIER:
 		rc = mgs_iocontrol_barrier(&env, mgs, data);
 		break;
-
 	case OBD_IOC_NODEMAP:
 		rc = mgs_iocontrol_nodemap(&env, mgs, data);
 		break;
-
 	case OBD_IOC_LCFG_FORK:
 		rc = mgs_lcfg_fork(&env, mgs, data->ioc_inlbuf1,
 				   data->ioc_inlbuf2);
 		break;
-
 	case OBD_IOC_LCFG_ERASE:
 		rc = mgs_lcfg_erase(&env, mgs, data->ioc_inlbuf1);
 		break;
-
 	case OBD_IOC_CATLOGLIST:
 		rc = mgs_list_logs(&env, mgs, data);
 		break;
@@ -1148,11 +1134,8 @@ out_free:
 		llog_ctxt_put(ctxt);
 		break;
         }
-
         default:
-		CERROR("%s: unknown command %#x\n",
-		       mgs->mgs_obd->obd_name,  cmd);
-		rc = -ENOTTY;
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
 		break;
         }
 out:
--- a/lustre/obdclass/class_obd.c
+++ b/lustre/obdclass/class_obd.c
@@ -103,6 +103,29 @@ struct lprocfs_stats *obd_memory = NULL;
 EXPORT_SYMBOL(obd_memory);
 #endif
 
+int obd_ioctl_msg(const char *file, const char *func, int line, int level,
+		  const char *name, unsigned int cmd, const char *msg, int rc)
+{
+	static struct cfs_debug_limit_state cdls;
+	char *dirs[] = {
+		[_IOC_NONE]		= "_IO",
+		[_IOC_READ]		= "_IOR",
+		[_IOC_WRITE]		= "_IOW",
+		[_IOC_READ|_IOC_WRITE]	= "_IOWR",
+	};
+	char type;
+
+	type = _IOC_TYPE(cmd);
+	__CDEBUG(&cdls, level,
+		 "%s: iocontrol from '%s' cmd=%x %s('%c', %u, %u) %s: rc = %d\n",
+		 name, current->comm, cmd,
+		 dirs[_IOC_DIR(cmd)] ?: "_IO?",
+		 isprint(type) ? type : '?', _IOC_NR(cmd),
+		 _IOC_SIZE(cmd), msg, rc);
+	return rc;
+}
+EXPORT_SYMBOL(obd_ioctl_msg);
+
 static int class_resolve_dev_name(__u32 len, const char *name)
 {
         int rc;
@@ -216,26 +239,28 @@ int obd_ioctl_getdata(char **buf, int *l
 	struct obd_ioctl_hdr hdr;
 	struct obd_ioctl_data *data;
 	int offset = 0;
+	int rc = -EINVAL;
 
 	ENTRY;
 	if (copy_from_user(&hdr, arg, sizeof(hdr)))
 		RETURN(-EFAULT);
 
 	if (hdr.ioc_version != OBD_IOCTL_VERSION) {
-		CERROR("Version mismatch kernel (%x) vs application (%x)\n",
-		       OBD_IOCTL_VERSION, hdr.ioc_version);
-		RETURN(-EINVAL);
+		CERROR("%s: kernel/user version mismatch (%x != %x): rc = %d\n",
+		       current->comm, OBD_IOCTL_VERSION, hdr.ioc_version, rc);
+		RETURN(rc);
 	}
 
 	if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
-		CERROR("User buffer len %d exceeds %d max buffer\n",
-		       hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
-		RETURN(-EINVAL);
+		CERROR("%s: user buffer len %d exceeds %d max: rc = %d\n",
+		       current->comm, hdr.ioc_len, OBD_MAX_IOCTL_BUFFER, rc);
+		RETURN(rc);
 	}
 
-	if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
-		CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
-		RETURN(-EINVAL);
+	if (hdr.ioc_len < sizeof(*data)) {
+		CERROR("%s: user buffer %d too small for ioctl %zu: rc = %d\n",
+		       current->comm, hdr.ioc_len, sizeof(*data), rc);
+		RETURN(rc);
 	}
 
 	/* When there are lots of processes calling vmalloc on multi-core
@@ -245,9 +270,10 @@ int obd_ioctl_getdata(char **buf, int *l
 	 */
 	OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
 	if (!*buf) {
-		CERROR("Cannot allocate control buffer of len %d\n",
-		       hdr.ioc_len);
-		RETURN(-EINVAL);
+		rc = -ENOMEM;
+		CERROR("%s: cannot allocate control buffer len %d: rc = %d\n",
+		       current->comm, hdr.ioc_len, rc);
+		RETURN(rc);
 	}
 	*len = hdr.ioc_len;
 	data = (struct obd_ioctl_data *)*buf;
@@ -290,50 +316,53 @@ int class_handle_ioctl(unsigned int cmd,
 	char *buf = NULL;
 	struct obd_ioctl_data *data;
 	struct obd_device *obd = NULL;
-	int err = 0, len = 0;
+	int rc, len = 0;
 
 	ENTRY;
-	CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
-	if (obd_ioctl_getdata(&buf, &len, uarg)) {
-		CERROR("OBD ioctl: data error\n");
-		RETURN(-EINVAL);
+	CDEBUG(D_IOCTL, "obdclass: cmd=%x len=%u uarg=%pK\n", cmd, len, uarg);
+	rc = obd_ioctl_getdata(&buf, &len, uarg);
+	if (rc) {
+		CERROR("%s: ioctl data error: rc = %d\n", current->comm, rc);
+		RETURN(rc);
 	}
 	data = (struct obd_ioctl_data *)buf;
 
-        switch (cmd) {
-        case OBD_IOC_PROCESS_CFG: {
-                struct lustre_cfg *lcfg;
-
-                if (!data->ioc_plen1 || !data->ioc_pbuf1) {
-                        CERROR("No config buffer passed!\n");
-                        GOTO(out, err = -EINVAL);
-                }
-                OBD_ALLOC(lcfg, data->ioc_plen1);
-                if (lcfg == NULL)
-                        GOTO(out, err = -ENOMEM);
-		err = copy_from_user(lcfg, data->ioc_pbuf1,
-                                         data->ioc_plen1);
-                if (!err)
-                        err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
-                if (!err)
-                        err = class_process_config(lcfg);
+	switch (cmd) {
+	case OBD_IOC_PROCESS_CFG: {
+		struct lustre_cfg *lcfg;
+
+		if (!data->ioc_plen1 || !data->ioc_pbuf1) {
+			rc = OBD_IOC_ERROR("obdclass", cmd, "no config buffer",
+					   -EINVAL);
+			GOTO(out, rc);
+		}
+		OBD_ALLOC(lcfg, data->ioc_plen1);
+		if (lcfg == NULL)
+			GOTO(out, rc = -ENOMEM);
+		rc = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
+		if (!rc)
+			rc = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
+		if (!rc)
+			rc = class_process_config(lcfg);
 
-                OBD_FREE(lcfg, data->ioc_plen1);
-                GOTO(out, err);
-        }
+		OBD_FREE(lcfg, data->ioc_plen1);
+		GOTO(out, rc);
+	}
 
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
 	case OBD_GET_VERSION: {
 		static bool warned;
 
 		if (!data->ioc_inlbuf1) {
-			CERROR("No buffer passed in ioctl\n");
-			GOTO(out, err = -EINVAL);
+			rc = OBD_IOC_ERROR("obdclass", cmd, "no buffer passed",
+					   -EINVAL);
+			GOTO(out, rc);
 		}
 
 		if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
-			CERROR("ioctl buffer too small to hold version\n");
-			GOTO(out, err = -EINVAL);
+			rc = OBD_IOC_ERROR("obdclass", cmd, "buffer too small",
+					   -EINVAL);
+			GOTO(out, rc);
 		}
 
 		if (!warned) {
@@ -346,8 +375,8 @@ int class_handle_ioctl(unsigned int cmd,
 		       strlen(LUSTRE_VERSION_STRING) + 1);
 
 		if (copy_to_user(uarg, data, len))
-			err = -EFAULT;
-		GOTO(out, err);
+			rc = -EFAULT;
+		GOTO(out, rc);
 	}
 #endif
 	case OBD_IOC_NAME2DEV: {
@@ -358,62 +387,64 @@ int class_handle_ioctl(unsigned int cmd,
 					     data->ioc_inlbuf1);
 		data->ioc_dev = dev;
 		if (dev < 0)
-			GOTO(out, err = -EINVAL);
+			GOTO(out, rc = -EINVAL);
 
 		if (copy_to_user(uarg, data, sizeof(*data)))
-			err = -EFAULT;
-		GOTO(out, err);
+			rc = -EFAULT;
+		GOTO(out, rc);
 	}
 
-        case OBD_IOC_UUID2DEV: {
-                /* Resolve a device uuid.  This does not change the
-                 * currently selected device.
-                 */
-                int dev;
-                struct obd_uuid uuid;
-
-                if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
-                        CERROR("No UUID passed!\n");
-                        GOTO(out, err = -EINVAL);
-                }
-                if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
-                        CERROR("UUID not NUL terminated!\n");
-                        GOTO(out, err = -EINVAL);
-                }
+	case OBD_IOC_UUID2DEV: {
+		/* Resolve device uuid, does not change current selected dev */
+		struct obd_uuid uuid;
+		int dev;
 
-                CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
-                obd_str2uuid(&uuid, data->ioc_inlbuf1);
-                dev = class_uuid2dev(&uuid);
-                data->ioc_dev = dev;
-                if (dev == -1) {
-                        CDEBUG(D_IOCTL, "No device for UUID %s!\n",
-                               data->ioc_inlbuf1);
-                        GOTO(out, err = -EINVAL);
-                }
+		if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
+			rc = OBD_IOC_ERROR("obdclass", cmd, "no UUID passed",
+					   -EINVAL);
+			GOTO(out, rc);
+		}
+		if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
+			rc = OBD_IOC_ERROR("obdclass", cmd, "unterminated UUID",
+					   -EINVAL);
+			GOTO(out, rc);
+		}
+
+		CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
+		obd_str2uuid(&uuid, data->ioc_inlbuf1);
+		dev = class_uuid2dev(&uuid);
+		data->ioc_dev = dev;
+		if (dev == -1) {
+			CDEBUG(D_IOCTL, "No device for UUID %s!\n",
+			       data->ioc_inlbuf1);
+			GOTO(out, rc = -EINVAL);
+		}
 
 		CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
 		       dev);
 		if (copy_to_user(uarg, data, sizeof(*data)))
-			err = -EFAULT;
-		GOTO(out, err);
+			rc = -EFAULT;
+		GOTO(out, rc);
 	}
 
-        case OBD_IOC_GETDEVICE: {
-                int     index = data->ioc_count;
-                char    *status, *str;
+	case OBD_IOC_GETDEVICE: {
+		int index = data->ioc_count;
+		char *status, *str;
 
-                if (!data->ioc_inlbuf1) {
-                        CERROR("No buffer passed in ioctl\n");
-                        GOTO(out, err = -EINVAL);
-                }
-                if (data->ioc_inllen1 < 128) {
-                        CERROR("ioctl buffer too small to hold version\n");
-                        GOTO(out, err = -EINVAL);
-                }
+		if (!data->ioc_inlbuf1) {
+			rc = OBD_IOC_ERROR("obdclass", cmd, "no buffer passed",
+					   -EINVAL);
+			GOTO(out, rc);
+		}
+		if (data->ioc_inllen1 < 128) {
+			rc = OBD_IOC_ERROR("obdclass", cmd, "too small version",
+					   -EINVAL);
+			GOTO(out, rc);
+		}
 
-                obd = class_num2obd(index);
-                if (!obd)
-                        GOTO(out, err = -ENOENT);
+		obd = class_num2obd(index);
+		if (!obd)
+			GOTO(out, rc = -ENOENT);
 
                 if (obd->obd_stopping)
                         status = "ST";
@@ -430,62 +461,65 @@ int class_handle_ioctl(unsigned int cmd,
 			 atomic_read(&obd->obd_refcount));
 
 		if (copy_to_user(uarg, data, len))
-			err = -EFAULT;
+			rc = -EFAULT;
 
-		GOTO(out, err);
+		GOTO(out, rc);
 	}
 	}
 
-        if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
-                if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
-                        GOTO(out, err = -EINVAL);
-                if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
-                        GOTO(out, err = -EINVAL);
-                obd = class_name2obd(data->ioc_inlbuf4);
-        } else if (data->ioc_dev < class_devno_max()) {
-                obd = class_num2obd(data->ioc_dev);
-        } else {
-                CERROR("OBD ioctl: No device\n");
-                GOTO(out, err = -EINVAL);
-        }
+	if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
+		if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
+			GOTO(out, rc = -EINVAL);
+		if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
+			GOTO(out, rc = -EINVAL);
+		obd = class_name2obd(data->ioc_inlbuf4);
+	} else if (data->ioc_dev < class_devno_max()) {
+		obd = class_num2obd(data->ioc_dev);
+	} else {
+		rc = OBD_IOC_ERROR("obdclass", cmd, "no device", -EINVAL);
+		GOTO(out, rc);
+	}
 
-        if (obd == NULL) {
-                CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
-                GOTO(out, err = -EINVAL);
-        }
-        LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
+	if (obd == NULL) {
+		rc = OBD_IOC_ERROR(data->ioc_inlbuf4, cmd, "no device found",
+				   -EINVAL);
+		GOTO(out, rc);
+	}
+	LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
 
-        if (!obd->obd_set_up || obd->obd_stopping) {
-                CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
-                GOTO(out, err = -EINVAL);
-        }
+	if (!obd->obd_set_up || obd->obd_stopping) {
+		rc = -EINVAL;
+		CERROR("obdclass: device %d not set up: rc = %d\n",
+		       data->ioc_dev, rc);
+		GOTO(out, rc);
+	}
 
         switch(cmd) {
         case OBD_IOC_NO_TRANSNO: {
                 if (!obd->obd_attached) {
                         CERROR("Device %d not attached\n", obd->obd_minor);
-                        GOTO(out, err = -ENODEV);
+                        GOTO(out, rc = -ENODEV);
                 }
                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
                        obd->obd_name);
                 obd->obd_no_transno = 1;
-                GOTO(out, err = 0);
+                GOTO(out, rc = 0);
         }
 
         default: {
-                err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
-                if (err)
-                        GOTO(out, err);
+                rc = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
+                if (rc)
+                        GOTO(out, rc);
 
 		if (copy_to_user(uarg, data, len))
-                        err = -EFAULT;
-                GOTO(out, err);
+                        rc = -EFAULT;
+                GOTO(out, rc);
         }
         }
 
 out:
 	OBD_FREE_LARGE(buf, len);
-	RETURN(err);
+	RETURN(rc);
 } /* class_handle_ioctl */
 
 /*  opening /dev/obd */
@@ -514,10 +548,10 @@ static long obd_class_ioctl(struct file
 	ENTRY;
 	/* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
 	if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
-		RETURN(err = -EACCES);
+		RETURN(-EACCES);
 
 	if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
-		RETURN(err = -ENOTTY);
+		RETURN(-ENOTTY);
 
 	err = class_handle_ioctl(cmd, (void __user *)arg);
 
--- a/lustre/obdecho/echo_client.c
+++ b/lustre/obdecho/echo_client.c
@@ -2743,20 +2743,23 @@ echo_client_iocontrol(unsigned int cmd,
 #ifdef HAVE_SERVER_SUPPORT
 	struct tgt_session_info *tsi;
 #endif
-        struct obd_device      *obd = exp->exp_obd;
-        struct echo_device     *ed = obd2echo_dev(obd);
+        struct obd_device *obd = exp->exp_obd;
+        struct echo_device *ed = obd2echo_dev(obd);
         struct echo_client_obd *ec = ed->ed_ec;
-        struct echo_object     *eco;
-        struct obd_ioctl_data  *data = karg;
-        struct lu_env          *env;
-        struct obdo            *oa;
-        struct lu_fid           fid;
-        int                     rw = OBD_BRW_READ;
-        int                     rc = 0;
+        struct echo_object *eco;
+        struct obd_ioctl_data *data = karg;
+        struct lu_env *env;
+        struct obdo *oa;
+        struct lu_fid fid;
+        int rw = OBD_BRW_READ;
+        int rc = -EINVAL;
 #ifdef HAVE_SERVER_SUPPORT
 	struct lu_context	 echo_session;
 #endif
-        ENTRY;
+
+	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       exp->exp_obd->obd_name, cmd, len, karg, uarg);
 
 	oa = &data->ioc_obdo1;
 	if (!(oa->o_valid & OBD_MD_FLGROUP)) {
@@ -2907,11 +2910,10 @@ echo_client_iocontrol(unsigned int cmd,
         case OBD_IOC_BRW_READ:
 		rc = echo_client_brw_ioctl(env, rw, exp, data);
                 GOTO(out, rc);
-
-        default:
-                CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
-                GOTO (out, rc = -ENOTTY);
-        }
+	default:
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
+	}
 
         EXIT;
 out:
--- a/lustre/ofd/ofd_obd.c
+++ b/lustre/ofd/ofd_obd.c
@@ -1332,15 +1332,15 @@ static int ofd_iocontrol(unsigned int cm
 	int rc;
 
 	ENTRY;
-
-	CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       obd->obd_name, cmd, len, karg, uarg);
 	rc = lu_env_init(&env, LCT_DT_THREAD);
 	if (rc)
 		RETURN(rc);
 
 	switch (cmd) {
 	case OBD_IOC_ABORT_RECOVERY:
-		CERROR("%s: aborting recovery\n", obd->obd_name);
+		CWARN("%s: Aborting recovery\n", obd->obd_name);
 		obd->obd_abort_recovery = 1;
 		target_stop_recovery_thread(obd);
 		break;
@@ -1377,7 +1377,7 @@ static int ofd_iocontrol(unsigned int cm
 			stop.ls_flags = 0;
 		else
 			stop.ls_flags =
-			((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
+			   ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
 
 		rc = lfsck_stop(&env, ofd->ofd_osd, &stop);
 		break;
@@ -1386,8 +1386,8 @@ static int ofd_iocontrol(unsigned int cm
 		rc = ofd_ioc_get_obj_version(&env, ofd, karg);
 		break;
 	default:
-		CERROR("%s: not supported cmd = %#x\n", obd->obd_name, cmd);
-		rc = -ENOTTY;
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
 	}
 
 	lu_env_fini(&env);
--- a/lustre/osc/osc_request.c
+++ b/lustre/osc/osc_request.c
@@ -2841,6 +2841,9 @@ static int osc_iocontrol(unsigned int cm
         int err = 0;
         ENTRY;
 
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       obd->obd_name, cmd, len, karg, uarg);
+
 	if (!try_module_get(THIS_MODULE)) {
 		CERROR("%s: cannot get module '%s'\n", obd->obd_name,
 		       module_name(THIS_MODULE));
@@ -2861,8 +2864,8 @@ static int osc_iocontrol(unsigned int cm
                 err = ptlrpc_obd_ping(obd);
                 GOTO(out, err);
 	default:
-		CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n",
-		       cmd, current_comm());
+		rc = OBD_IOC_DEBUG(D_IOCTL, obd->obd_name, cmd, "unrecognized",
+				   -ENOTTY);
 		GOTO(out, err = -ENOTTY);
 	}
 out:
--- a/lustre/osp/osp_dev.c
+++ b/lustre/osp/osp_dev.c
@@ -1646,12 +1646,14 @@ static int osp_import_event(struct obd_d
 static int osp_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
 			 void *karg, void __user *uarg)
 {
-	struct obd_device	*obd = exp->exp_obd;
-	struct osp_device	*d;
-	struct obd_ioctl_data	*data = karg;
-	int			 rc = 0;
+	struct obd_device *obd = exp->exp_obd;
+	struct osp_device *d;
+	struct obd_ioctl_data *data = karg;
+	int rc = -EINVAL;
 
 	ENTRY;
+	CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
+	       exp->exp_obd->obd_name, cmd, len, karg, uarg);
 
 	LASSERT(obd->obd_lu_dev);
 	d = lu2osp_dev(obd->obd_lu_dev);
@@ -1678,15 +1680,13 @@ static int osp_iocontrol(unsigned int cm
 		rc = ptlrpc_obd_ping(obd);
 		break;
 	default:
-		CERROR("%s: unrecognized ioctl %#x by %s\n", obd->obd_name,
-		       cmd, current_comm());
-		rc = -ENOTTY;
+		rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
+		break;
 	}
 	module_put(THIS_MODULE);
 	return rc;
 }
 
-
 /**
  * Implementation of obd_ops::o_get_info
  *
openSUSE Build Service is sponsored by