target_core_stat.c 45.5 KB
Newer Older
1 2 3 4 5 6
/*******************************************************************************
 * Filename:  target_core_stat.c
 *
 * Modern ConfigFS group context specific statistics based on original
 * target_core_mib.c code
 *
7
 * (c) Copyright 2006-2013 Datera, Inc.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Nicholas A. Bellinger <nab@linux-iscsi.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ******************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/utsname.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/configfs.h>
#include <scsi/scsi.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>

#include <target/target_core_base.h>
41 42
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
43 44
#include <target/configfs_macros.h>

C
Christoph Hellwig 已提交
45
#include "target_core_internal.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

#ifndef INITIAL_JIFFIES
#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
#endif

#define NONE		"None"
#define ISPRINT(a)   ((a >= ' ') && (a <= '~'))

#define SCSI_LU_INDEX			1
#define LU_COUNT			1

/*
 * SCSI Device Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_dev, se_dev_stat_grps);
#define DEV_STAT_SCSI_DEV_ATTR(_name, _mode)				\
static struct target_stat_scsi_dev_attribute				\
			target_stat_scsi_dev_##_name =			\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_dev_show_attr_##_name,				\
	target_stat_scsi_dev_store_attr_##_name);

#define DEV_STAT_SCSI_DEV_ATTR_RO(_name)				\
static struct target_stat_scsi_dev_attribute				\
			target_stat_scsi_dev_##_name =			\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_dev_show_attr_##_name);

static ssize_t target_stat_scsi_dev_show_attr_inst(
	struct se_dev_stat_grps *sgrps, char *page)
{
78 79 80
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
	struct se_hba *hba = dev->se_hba;
81 82 83 84 85 86 87 88

	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
}
DEV_STAT_SCSI_DEV_ATTR_RO(inst);

static ssize_t target_stat_scsi_dev_show_attr_indx(
	struct se_dev_stat_grps *sgrps, char *page)
{
89 90
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
}
DEV_STAT_SCSI_DEV_ATTR_RO(indx);

static ssize_t target_stat_scsi_dev_show_attr_role(
	struct se_dev_stat_grps *sgrps, char *page)
{
	return snprintf(page, PAGE_SIZE, "Target\n");
}
DEV_STAT_SCSI_DEV_ATTR_RO(role);

static ssize_t target_stat_scsi_dev_show_attr_ports(
	struct se_dev_stat_grps *sgrps, char *page)
{
106 107
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
108

109
	return snprintf(page, PAGE_SIZE, "%u\n", dev->export_count);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}
DEV_STAT_SCSI_DEV_ATTR_RO(ports);

CONFIGFS_EATTR_OPS(target_stat_scsi_dev, se_dev_stat_grps, scsi_dev_group);

static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
	&target_stat_scsi_dev_inst.attr,
	&target_stat_scsi_dev_indx.attr,
	&target_stat_scsi_dev_role.attr,
	&target_stat_scsi_dev_ports.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_dev_attrib_ops = {
	.show_attribute		= target_stat_scsi_dev_attr_show,
	.store_attribute	= target_stat_scsi_dev_attr_store,
};

static struct config_item_type target_stat_scsi_dev_cit = {
	.ct_item_ops		= &target_stat_scsi_dev_attrib_ops,
	.ct_attrs		= target_stat_scsi_dev_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * SCSI Target Device Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_tgt_dev, se_dev_stat_grps);
#define DEV_STAT_SCSI_TGT_DEV_ATTR(_name, _mode)			\
static struct target_stat_scsi_tgt_dev_attribute			\
			target_stat_scsi_tgt_dev_##_name =		\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_tgt_dev_show_attr_##_name,			\
	target_stat_scsi_tgt_dev_store_attr_##_name);

#define DEV_STAT_SCSI_TGT_DEV_ATTR_RO(_name)				\
static struct target_stat_scsi_tgt_dev_attribute			\
			target_stat_scsi_tgt_dev_##_name =		\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_tgt_dev_show_attr_##_name);

static ssize_t target_stat_scsi_tgt_dev_show_attr_inst(
	struct se_dev_stat_grps *sgrps, char *page)
{
155 156 157
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
	struct se_hba *hba = dev->se_hba;
158 159 160 161 162 163 164 165

	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(inst);

static ssize_t target_stat_scsi_tgt_dev_show_attr_indx(
	struct se_dev_stat_grps *sgrps, char *page)
{
166 167
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(indx);

static ssize_t target_stat_scsi_tgt_dev_show_attr_num_lus(
	struct se_dev_stat_grps *sgrps, char *page)
{
	return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(num_lus);

static ssize_t target_stat_scsi_tgt_dev_show_attr_status(
	struct se_dev_stat_grps *sgrps, char *page)
{
183 184
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
185

186 187 188 189
	if (dev->export_count)
		return snprintf(page, PAGE_SIZE, "activated");
	else
		return snprintf(page, PAGE_SIZE, "deactivated");
190 191 192 193 194 195
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(status);

static ssize_t target_stat_scsi_tgt_dev_show_attr_non_access_lus(
	struct se_dev_stat_grps *sgrps, char *page)
{
196 197
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
198 199
	int non_accessible_lus;

200
	if (dev->export_count)
201
		non_accessible_lus = 0;
202
	else
203 204 205 206 207 208 209 210 211
		non_accessible_lus = 1;

	return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(non_access_lus);

static ssize_t target_stat_scsi_tgt_dev_show_attr_resets(
	struct se_dev_stat_grps *sgrps, char *page)
{
212 213
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
214

215 216
	return snprintf(page, PAGE_SIZE, "%lu\n",
			atomic_long_read(&dev->num_resets));
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}
DEV_STAT_SCSI_TGT_DEV_ATTR_RO(resets);


CONFIGFS_EATTR_OPS(target_stat_scsi_tgt_dev, se_dev_stat_grps, scsi_tgt_dev_group);

static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
	&target_stat_scsi_tgt_dev_inst.attr,
	&target_stat_scsi_tgt_dev_indx.attr,
	&target_stat_scsi_tgt_dev_num_lus.attr,
	&target_stat_scsi_tgt_dev_status.attr,
	&target_stat_scsi_tgt_dev_non_access_lus.attr,
	&target_stat_scsi_tgt_dev_resets.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_tgt_dev_attrib_ops = {
	.show_attribute		= target_stat_scsi_tgt_dev_attr_show,
	.store_attribute	= target_stat_scsi_tgt_dev_attr_store,
};

static struct config_item_type target_stat_scsi_tgt_dev_cit = {
	.ct_item_ops		= &target_stat_scsi_tgt_dev_attrib_ops,
	.ct_attrs		= target_stat_scsi_tgt_dev_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * SCSI Logical Unit Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_lu, se_dev_stat_grps);
#define DEV_STAT_SCSI_LU_ATTR(_name, _mode)				\
static struct target_stat_scsi_lu_attribute target_stat_scsi_lu_##_name = \
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_lu_show_attr_##_name,				\
	target_stat_scsi_lu_store_attr_##_name);

#define DEV_STAT_SCSI_LU_ATTR_RO(_name)					\
static struct target_stat_scsi_lu_attribute target_stat_scsi_lu_##_name = \
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_lu_show_attr_##_name);

static ssize_t target_stat_scsi_lu_show_attr_inst(
	struct se_dev_stat_grps *sgrps, char *page)
{
263 264 265
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
	struct se_hba *hba = dev->se_hba;
266 267 268 269 270 271 272 273

	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
}
DEV_STAT_SCSI_LU_ATTR_RO(inst);

static ssize_t target_stat_scsi_lu_show_attr_dev(
	struct se_dev_stat_grps *sgrps, char *page)
{
274 275
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
}
DEV_STAT_SCSI_LU_ATTR_RO(dev);

static ssize_t target_stat_scsi_lu_show_attr_indx(
	struct se_dev_stat_grps *sgrps, char *page)
{
	return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
}
DEV_STAT_SCSI_LU_ATTR_RO(indx);

static ssize_t target_stat_scsi_lu_show_attr_lun(
	struct se_dev_stat_grps *sgrps, char *page)
{
	/* FIXME: scsiLuDefaultLun */
	return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
}
DEV_STAT_SCSI_LU_ATTR_RO(lun);

static ssize_t target_stat_scsi_lu_show_attr_lu_name(
	struct se_dev_stat_grps *sgrps, char *page)
{
299 300
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
301 302 303

	/* scsiLuWwnName */
	return snprintf(page, PAGE_SIZE, "%s\n",
304 305
			(strlen(dev->t10_wwn.unit_serial)) ?
			dev->t10_wwn.unit_serial : "None");
306 307 308 309 310 311
}
DEV_STAT_SCSI_LU_ATTR_RO(lu_name);

static ssize_t target_stat_scsi_lu_show_attr_vend(
	struct se_dev_stat_grps *sgrps, char *page)
{
312 313
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
314
	int i;
315
	char str[sizeof(dev->t10_wwn.vendor)+1];
316

317
	/* scsiLuVendorId */
318 319 320
	for (i = 0; i < sizeof(dev->t10_wwn.vendor); i++)
		str[i] = ISPRINT(dev->t10_wwn.vendor[i]) ?
			dev->t10_wwn.vendor[i] : ' ';
321
	str[i] = '\0';
322 323 324 325 326 327 328
	return snprintf(page, PAGE_SIZE, "%s\n", str);
}
DEV_STAT_SCSI_LU_ATTR_RO(vend);

static ssize_t target_stat_scsi_lu_show_attr_prod(
	struct se_dev_stat_grps *sgrps, char *page)
{
329 330
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
331
	int i;
332
	char str[sizeof(dev->t10_wwn.model)+1];
333 334

	/* scsiLuProductId */
335 336 337
	for (i = 0; i < sizeof(dev->t10_wwn.vendor); i++)
		str[i] = ISPRINT(dev->t10_wwn.model[i]) ?
			dev->t10_wwn.model[i] : ' ';
338
	str[i] = '\0';
339 340 341 342 343 344 345
	return snprintf(page, PAGE_SIZE, "%s\n", str);
}
DEV_STAT_SCSI_LU_ATTR_RO(prod);

static ssize_t target_stat_scsi_lu_show_attr_rev(
	struct se_dev_stat_grps *sgrps, char *page)
{
346 347
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
348
	int i;
349
	char str[sizeof(dev->t10_wwn.revision)+1];
350 351

	/* scsiLuRevisionId */
352 353 354
	for (i = 0; i < sizeof(dev->t10_wwn.revision); i++)
		str[i] = ISPRINT(dev->t10_wwn.revision[i]) ?
			dev->t10_wwn.revision[i] : ' ';
355
	str[i] = '\0';
356 357 358 359 360 361 362
	return snprintf(page, PAGE_SIZE, "%s\n", str);
}
DEV_STAT_SCSI_LU_ATTR_RO(rev);

static ssize_t target_stat_scsi_lu_show_attr_dev_type(
	struct se_dev_stat_grps *sgrps, char *page)
{
363 364
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
365 366 367

	/* scsiLuPeripheralType */
	return snprintf(page, PAGE_SIZE, "%u\n",
368
			dev->transport->get_device_type(dev));
369 370 371 372 373 374
}
DEV_STAT_SCSI_LU_ATTR_RO(dev_type);

static ssize_t target_stat_scsi_lu_show_attr_status(
	struct se_dev_stat_grps *sgrps, char *page)
{
375 376
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
377 378 379

	/* scsiLuStatus */
	return snprintf(page, PAGE_SIZE, "%s\n",
380
		(dev->export_count) ? "available" : "notavailable");
381 382 383 384 385 386 387 388 389 390 391 392 393 394
}
DEV_STAT_SCSI_LU_ATTR_RO(status);

static ssize_t target_stat_scsi_lu_show_attr_state_bit(
	struct se_dev_stat_grps *sgrps, char *page)
{
	/* scsiLuState */
	return snprintf(page, PAGE_SIZE, "exposed\n");
}
DEV_STAT_SCSI_LU_ATTR_RO(state_bit);

static ssize_t target_stat_scsi_lu_show_attr_num_cmds(
	struct se_dev_stat_grps *sgrps, char *page)
{
395 396
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
397 398

	/* scsiLuNumCommands */
399 400
	return snprintf(page, PAGE_SIZE, "%lu\n",
			atomic_long_read(&dev->num_cmds));
401 402 403 404 405 406
}
DEV_STAT_SCSI_LU_ATTR_RO(num_cmds);

static ssize_t target_stat_scsi_lu_show_attr_read_mbytes(
	struct se_dev_stat_grps *sgrps, char *page)
{
407 408
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
409 410

	/* scsiLuReadMegaBytes */
411 412
	return snprintf(page, PAGE_SIZE, "%lu\n",
			atomic_long_read(&dev->read_bytes) >> 20);
413 414 415 416 417 418
}
DEV_STAT_SCSI_LU_ATTR_RO(read_mbytes);

static ssize_t target_stat_scsi_lu_show_attr_write_mbytes(
	struct se_dev_stat_grps *sgrps, char *page)
{
419 420
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
421 422

	/* scsiLuWrittenMegaBytes */
423 424
	return snprintf(page, PAGE_SIZE, "%lu\n",
			atomic_long_read(&dev->write_bytes) >> 20);
425 426 427 428 429 430
}
DEV_STAT_SCSI_LU_ATTR_RO(write_mbytes);

static ssize_t target_stat_scsi_lu_show_attr_resets(
	struct se_dev_stat_grps *sgrps, char *page)
{
431 432
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
433 434

	/* scsiLuInResets */
435
	return snprintf(page, PAGE_SIZE, "%lu\n", atomic_long_read(&dev->num_resets));
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
}
DEV_STAT_SCSI_LU_ATTR_RO(resets);

static ssize_t target_stat_scsi_lu_show_attr_full_stat(
	struct se_dev_stat_grps *sgrps, char *page)
{
	/* FIXME: scsiLuOutTaskSetFullStatus */
	return snprintf(page, PAGE_SIZE, "%u\n", 0);
}
DEV_STAT_SCSI_LU_ATTR_RO(full_stat);

static ssize_t target_stat_scsi_lu_show_attr_hs_num_cmds(
	struct se_dev_stat_grps *sgrps, char *page)
{
	/* FIXME: scsiLuHSInCommands */
	return snprintf(page, PAGE_SIZE, "%u\n", 0);
}
DEV_STAT_SCSI_LU_ATTR_RO(hs_num_cmds);

static ssize_t target_stat_scsi_lu_show_attr_creation_time(
	struct se_dev_stat_grps *sgrps, char *page)
{
458 459
	struct se_device *dev =
		container_of(sgrps, struct se_device, dev_stat_grps);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

	/* scsiLuCreationTime */
	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
				INITIAL_JIFFIES) * 100 / HZ));
}
DEV_STAT_SCSI_LU_ATTR_RO(creation_time);

CONFIGFS_EATTR_OPS(target_stat_scsi_lu, se_dev_stat_grps, scsi_lu_group);

static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
	&target_stat_scsi_lu_inst.attr,
	&target_stat_scsi_lu_dev.attr,
	&target_stat_scsi_lu_indx.attr,
	&target_stat_scsi_lu_lun.attr,
	&target_stat_scsi_lu_lu_name.attr,
	&target_stat_scsi_lu_vend.attr,
	&target_stat_scsi_lu_prod.attr,
	&target_stat_scsi_lu_rev.attr,
	&target_stat_scsi_lu_dev_type.attr,
	&target_stat_scsi_lu_status.attr,
	&target_stat_scsi_lu_state_bit.attr,
	&target_stat_scsi_lu_num_cmds.attr,
	&target_stat_scsi_lu_read_mbytes.attr,
	&target_stat_scsi_lu_write_mbytes.attr,
	&target_stat_scsi_lu_resets.attr,
	&target_stat_scsi_lu_full_stat.attr,
	&target_stat_scsi_lu_hs_num_cmds.attr,
	&target_stat_scsi_lu_creation_time.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_lu_attrib_ops = {
	.show_attribute		= target_stat_scsi_lu_attr_show,
	.store_attribute	= target_stat_scsi_lu_attr_store,
};

static struct config_item_type target_stat_scsi_lu_cit = {
	.ct_item_ops		= &target_stat_scsi_lu_attrib_ops,
	.ct_attrs		= target_stat_scsi_lu_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * Called from target_core_configfs.c:target_core_make_subdev() to setup
 * the target statistics groups + configfs CITs located in target_core_stat.c
 */
506
void target_stat_setup_dev_default_groups(struct se_device *dev)
507
{
508
	struct config_group *dev_stat_grp = &dev->dev_stat_grps.stat_group;
509

510
	config_group_init_type_name(&dev->dev_stat_grps.scsi_dev_group,
511
			"scsi_dev", &target_stat_scsi_dev_cit);
512
	config_group_init_type_name(&dev->dev_stat_grps.scsi_tgt_dev_group,
513
			"scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
514
	config_group_init_type_name(&dev->dev_stat_grps.scsi_lu_group,
515 516
			"scsi_lu", &target_stat_scsi_lu_cit);

517 518 519
	dev_stat_grp->default_groups[0] = &dev->dev_stat_grps.scsi_dev_group;
	dev_stat_grp->default_groups[1] = &dev->dev_stat_grps.scsi_tgt_dev_group;
	dev_stat_grp->default_groups[2] = &dev->dev_stat_grps.scsi_lu_group;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	dev_stat_grp->default_groups[3] = NULL;
}

/*
 * SCSI Port Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_port, se_port_stat_grps);
#define DEV_STAT_SCSI_PORT_ATTR(_name, _mode)				\
static struct target_stat_scsi_port_attribute				\
			target_stat_scsi_port_##_name =			\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_port_show_attr_##_name,			\
	target_stat_scsi_port_store_attr_##_name);

#define DEV_STAT_SCSI_PORT_ATTR_RO(_name)				\
static struct target_stat_scsi_port_attribute				\
			target_stat_scsi_port_##_name =			\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_port_show_attr_##_name);

static ssize_t target_stat_scsi_port_show_attr_inst(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
545 546
	struct se_device *dev;
	ssize_t ret = -ENODEV;
547 548

	spin_lock(&lun->lun_sep_lock);
549 550 551
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
552 553 554 555 556 557 558 559 560
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_PORT_ATTR_RO(inst);

static ssize_t target_stat_scsi_port_show_attr_dev(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
561 562
	struct se_device *dev;
	ssize_t ret = -ENODEV;
563 564

	spin_lock(&lun->lun_sep_lock);
565 566 567
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
568 569 570 571 572 573 574 575 576
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_PORT_ATTR_RO(dev);

static ssize_t target_stat_scsi_port_show_attr_indx(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
577 578
	struct se_device *dev;
	ssize_t ret = -ENODEV;
579 580

	spin_lock(&lun->lun_sep_lock);
581 582 583
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
584 585 586 587 588 589 590 591 592
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_PORT_ATTR_RO(indx);

static ssize_t target_stat_scsi_port_show_attr_role(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
593 594
	struct se_device *dev;
	ssize_t ret = -ENODEV;
595 596

	spin_lock(&lun->lun_sep_lock);
597 598 599
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
600 601 602 603 604 605 606 607 608
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_PORT_ATTR_RO(role);

static ssize_t target_stat_scsi_port_show_attr_busy_count(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
609 610
	struct se_device *dev;
	ssize_t ret = -ENODEV;
611 612

	spin_lock(&lun->lun_sep_lock);
613 614 615 616
	dev = lun->lun_se_dev;
	if (dev) {
		/* FIXME: scsiPortBusyStatuses  */
		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	}
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_PORT_ATTR_RO(busy_count);

CONFIGFS_EATTR_OPS(target_stat_scsi_port, se_port_stat_grps, scsi_port_group);

static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
	&target_stat_scsi_port_inst.attr,
	&target_stat_scsi_port_dev.attr,
	&target_stat_scsi_port_indx.attr,
	&target_stat_scsi_port_role.attr,
	&target_stat_scsi_port_busy_count.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_port_attrib_ops = {
	.show_attribute		= target_stat_scsi_port_attr_show,
	.store_attribute	= target_stat_scsi_port_attr_store,
};

static struct config_item_type target_stat_scsi_port_cit = {
	.ct_item_ops		= &target_stat_scsi_port_attrib_ops,
	.ct_attrs		= target_stat_scsi_port_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * SCSI Target Port Table
 */
CONFIGFS_EATTR_STRUCT(target_stat_scsi_tgt_port, se_port_stat_grps);
#define DEV_STAT_SCSI_TGT_PORT_ATTR(_name, _mode)			\
static struct target_stat_scsi_tgt_port_attribute			\
			target_stat_scsi_tgt_port_##_name =		\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_tgt_port_show_attr_##_name,			\
	target_stat_scsi_tgt_port_store_attr_##_name);

#define DEV_STAT_SCSI_TGT_PORT_ATTR_RO(_name)				\
static struct target_stat_scsi_tgt_port_attribute			\
			target_stat_scsi_tgt_port_##_name =		\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_tgt_port_show_attr_##_name);

static ssize_t target_stat_scsi_tgt_port_show_attr_inst(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
666 667
	struct se_device *dev;
	ssize_t ret = -ENODEV;
668 669

	spin_lock(&lun->lun_sep_lock);
670 671 672
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
673 674 675 676 677 678 679 680 681
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(inst);

static ssize_t target_stat_scsi_tgt_port_show_attr_dev(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
682 683
	struct se_device *dev;
	ssize_t ret = -ENODEV;
684 685

	spin_lock(&lun->lun_sep_lock);
686 687 688
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
689 690 691 692 693 694 695 696 697
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(dev);

static ssize_t target_stat_scsi_tgt_port_show_attr_indx(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
698 699
	struct se_device *dev;
	ssize_t ret = -ENODEV;
700 701

	spin_lock(&lun->lun_sep_lock);
702 703 704
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
705 706 707 708 709 710 711 712 713
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(indx);

static ssize_t target_stat_scsi_tgt_port_show_attr_name(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
714 715 716
	struct se_portal_group *tpg = lun->lun_tpg;
	struct se_device *dev;
	ssize_t ret = -ENODEV;
717 718

	spin_lock(&lun->lun_sep_lock);
719 720 721 722 723
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
			tpg->se_tpg_tfo->get_fabric_name(),
			lun->lun_rtpi);
724 725 726 727 728 729 730 731 732
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(name);

static ssize_t target_stat_scsi_tgt_port_show_attr_port_index(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
733 734 735
	struct se_portal_group *tpg = lun->lun_tpg;
	struct se_device *dev;
	ssize_t ret = -ENODEV;
736 737

	spin_lock(&lun->lun_sep_lock);
738 739 740 741 742
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
			tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
			tpg->se_tpg_tfo->tpg_get_tag(tpg));
743 744 745 746 747 748 749 750 751
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(port_index);

static ssize_t target_stat_scsi_tgt_port_show_attr_in_cmds(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
752 753
	struct se_device *dev;
	ssize_t ret = -ENODEV;
754 755

	spin_lock(&lun->lun_sep_lock);
756 757 758
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_stats.cmd_pdus);
759 760 761 762 763 764 765 766 767
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(in_cmds);

static ssize_t target_stat_scsi_tgt_port_show_attr_write_mbytes(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
768 769
	struct se_device *dev;
	ssize_t ret = -ENODEV;
770 771

	spin_lock(&lun->lun_sep_lock);
772 773 774 775
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n",
				(u32)(lun->lun_stats.rx_data_octets >> 20));
776 777 778 779 780 781 782 783 784
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(write_mbytes);

static ssize_t target_stat_scsi_tgt_port_show_attr_read_mbytes(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
785 786
	struct se_device *dev;
	ssize_t ret = -ENODEV;
787 788

	spin_lock(&lun->lun_sep_lock);
789 790 791 792
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n",
				(u32)(lun->lun_stats.tx_data_octets >> 20));
793 794 795 796 797 798 799 800 801
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(read_mbytes);

static ssize_t target_stat_scsi_tgt_port_show_attr_hs_in_cmds(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
802 803
	struct se_device *dev;
	ssize_t ret = -ENODEV;
804 805

	spin_lock(&lun->lun_sep_lock);
806 807 808 809
	dev = lun->lun_se_dev;
	if (dev) {
		/* FIXME: scsiTgtPortHsInCommands */
		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	}
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TGT_PORT_ATTR_RO(hs_in_cmds);

CONFIGFS_EATTR_OPS(target_stat_scsi_tgt_port, se_port_stat_grps,
		scsi_tgt_port_group);

static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
	&target_stat_scsi_tgt_port_inst.attr,
	&target_stat_scsi_tgt_port_dev.attr,
	&target_stat_scsi_tgt_port_indx.attr,
	&target_stat_scsi_tgt_port_name.attr,
	&target_stat_scsi_tgt_port_port_index.attr,
	&target_stat_scsi_tgt_port_in_cmds.attr,
	&target_stat_scsi_tgt_port_write_mbytes.attr,
	&target_stat_scsi_tgt_port_read_mbytes.attr,
	&target_stat_scsi_tgt_port_hs_in_cmds.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_tgt_port_attrib_ops = {
	.show_attribute		= target_stat_scsi_tgt_port_attr_show,
	.store_attribute	= target_stat_scsi_tgt_port_attr_store,
};

static struct config_item_type target_stat_scsi_tgt_port_cit = {
	.ct_item_ops		= &target_stat_scsi_tgt_port_attrib_ops,
	.ct_attrs		= target_stat_scsi_tgt_port_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * SCSI Transport Table
o */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_transport, se_port_stat_grps);
#define DEV_STAT_SCSI_TRANSPORT_ATTR(_name, _mode)			\
static struct target_stat_scsi_transport_attribute			\
			target_stat_scsi_transport_##_name =		\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_transport_show_attr_##_name,			\
	target_stat_scsi_transport_store_attr_##_name);

#define DEV_STAT_SCSI_TRANSPORT_ATTR_RO(_name)				\
static struct target_stat_scsi_transport_attribute			\
			target_stat_scsi_transport_##_name =		\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_transport_show_attr_##_name);

static ssize_t target_stat_scsi_transport_show_attr_inst(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
865 866
	struct se_device *dev;
	ssize_t ret = -ENODEV;
867 868

	spin_lock(&lun->lun_sep_lock);
869 870 871
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
872 873 874 875 876 877 878 879 880
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TRANSPORT_ATTR_RO(inst);

static ssize_t target_stat_scsi_transport_show_attr_device(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
881 882 883
	struct se_device *dev;
	struct se_portal_group *tpg = lun->lun_tpg;
	ssize_t ret = -ENODEV;
884 885

	spin_lock(&lun->lun_sep_lock);
886 887 888 889 890
	dev = lun->lun_se_dev;
	if (dev) {
		/* scsiTransportType */
		ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
			       tpg->se_tpg_tfo->get_fabric_name());
891 892 893 894 895 896 897 898 899 900
	}
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TRANSPORT_ATTR_RO(device);

static ssize_t target_stat_scsi_transport_show_attr_indx(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
901 902 903
	struct se_device *dev;
	struct se_portal_group *tpg = lun->lun_tpg;
	ssize_t ret = -ENODEV;
904 905

	spin_lock(&lun->lun_sep_lock);
906 907 908 909
	dev = lun->lun_se_dev;
	if (dev)
		ret = snprintf(page, PAGE_SIZE, "%u\n",
			       tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
910 911 912 913 914 915 916 917 918 919
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TRANSPORT_ATTR_RO(indx);

static ssize_t target_stat_scsi_transport_show_attr_dev_name(
	struct se_port_stat_grps *pgrps, char *page)
{
	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
	struct se_device *dev = lun->lun_se_dev;
920
	struct se_portal_group *tpg = lun->lun_tpg;
921
	struct t10_wwn *wwn;
922
	ssize_t ret = -ENODEV;
923 924

	spin_lock(&lun->lun_sep_lock);
925 926 927 928 929 930 931 932
	dev = lun->lun_se_dev;
	if (dev) {
		wwn = &dev->t10_wwn;
		/* scsiTransportDevName */
		ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
				(strlen(wwn->unit_serial)) ? wwn->unit_serial :
				wwn->vendor);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	}
	spin_unlock(&lun->lun_sep_lock);
	return ret;
}
DEV_STAT_SCSI_TRANSPORT_ATTR_RO(dev_name);

CONFIGFS_EATTR_OPS(target_stat_scsi_transport, se_port_stat_grps,
		scsi_transport_group);

static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
	&target_stat_scsi_transport_inst.attr,
	&target_stat_scsi_transport_device.attr,
	&target_stat_scsi_transport_indx.attr,
	&target_stat_scsi_transport_dev_name.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_transport_attrib_ops = {
	.show_attribute		= target_stat_scsi_transport_attr_show,
	.store_attribute	= target_stat_scsi_transport_attr_store,
};

static struct config_item_type target_stat_scsi_transport_cit = {
	.ct_item_ops		= &target_stat_scsi_transport_attrib_ops,
	.ct_attrs		= target_stat_scsi_transport_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
 * the target port statistics groups + configfs CITs located in target_core_stat.c
 */
void target_stat_setup_port_default_groups(struct se_lun *lun)
{
967
	struct config_group *port_stat_grp = &lun->port_stat_grps.stat_group;
968

969
	config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
970
			"scsi_port", &target_stat_scsi_port_cit);
971
	config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
972
			"scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
973
	config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
974 975
			"scsi_transport", &target_stat_scsi_transport_cit);

976 977 978
	port_stat_grp->default_groups[0] = &lun->port_stat_grps.scsi_port_group;
	port_stat_grp->default_groups[1] = &lun->port_stat_grps.scsi_tgt_port_group;
	port_stat_grp->default_groups[2] = &lun->port_stat_grps.scsi_transport_group;
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	port_stat_grp->default_groups[3] = NULL;
}

/*
 * SCSI Authorized Initiator Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_auth_intr, se_ml_stat_grps);
#define DEV_STAT_SCSI_AUTH_INTR_ATTR(_name, _mode)			\
static struct target_stat_scsi_auth_intr_attribute			\
			target_stat_scsi_auth_intr_##_name =		\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_auth_intr_show_attr_##_name,			\
	target_stat_scsi_auth_intr_store_attr_##_name);

#define DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(_name)				\
static struct target_stat_scsi_auth_intr_attribute			\
			target_stat_scsi_auth_intr_##_name =		\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_auth_intr_show_attr_##_name);

static ssize_t target_stat_scsi_auth_intr_show_attr_inst(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_portal_group *tpg;
	ssize_t ret;

1010 1011 1012 1013
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1014 1015 1016 1017 1018
		return -ENODEV;
	}
	tpg = nacl->se_tpg;
	/* scsiInstIndex */
	ret = snprintf(page, PAGE_SIZE, "%u\n",
1019
			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1020
	rcu_read_unlock();
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(inst);

static ssize_t target_stat_scsi_auth_intr_show_attr_dev(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_lun *lun;
	ssize_t ret;

1035 1036 1037 1038
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1039 1040
		return -ENODEV;
	}
1041
	lun = rcu_dereference(deve->se_lun);
1042
	/* scsiDeviceIndex */
1043 1044
	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
	rcu_read_unlock();
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(dev);

static ssize_t target_stat_scsi_auth_intr_show_attr_port(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_portal_group *tpg;
	ssize_t ret;

1059 1060 1061 1062
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1063 1064 1065 1066
		return -ENODEV;
	}
	tpg = nacl->se_tpg;
	/* scsiAuthIntrTgtPortIndex */
1067
	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1068
	rcu_read_unlock();
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(port);

static ssize_t target_stat_scsi_auth_intr_show_attr_indx(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1082 1083 1084 1085
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1086 1087 1088 1089
		return -ENODEV;
	}
	/* scsiAuthIntrIndex */
	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1090
	rcu_read_unlock();
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(indx);

static ssize_t target_stat_scsi_auth_intr_show_attr_dev_or_port(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1104 1105 1106 1107
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1108 1109 1110 1111
		return -ENODEV;
	}
	/* scsiAuthIntrDevOrPort */
	ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
1112
	rcu_read_unlock();
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(dev_or_port);

static ssize_t target_stat_scsi_auth_intr_show_attr_intr_name(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1126 1127 1128 1129
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1130 1131 1132 1133
		return -ENODEV;
	}
	/* scsiAuthIntrName */
	ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
1134
	rcu_read_unlock();
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(intr_name);

static ssize_t target_stat_scsi_auth_intr_show_attr_map_indx(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1148 1149 1150 1151
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1152 1153 1154 1155
		return -ENODEV;
	}
	/* FIXME: scsiAuthIntrLunMapIndex */
	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1156
	rcu_read_unlock();
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(map_indx);

static ssize_t target_stat_scsi_auth_intr_show_attr_att_count(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1170 1171 1172 1173
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1174 1175 1176 1177
		return -ENODEV;
	}
	/* scsiAuthIntrAttachedTimes */
	ret = snprintf(page, PAGE_SIZE, "%u\n", deve->attach_count);
1178
	rcu_read_unlock();
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(att_count);

static ssize_t target_stat_scsi_auth_intr_show_attr_num_cmds(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1192 1193 1194 1195
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1196 1197 1198
		return -ENODEV;
	}
	/* scsiAuthIntrOutCommands */
1199 1200 1201
	ret = snprintf(page, PAGE_SIZE, "%lu\n",
		       atomic_long_read(&deve->total_cmds));
	rcu_read_unlock();
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(num_cmds);

static ssize_t target_stat_scsi_auth_intr_show_attr_read_mbytes(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1215 1216 1217 1218
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1219 1220 1221
		return -ENODEV;
	}
	/* scsiAuthIntrReadMegaBytes */
1222 1223 1224
	ret = snprintf(page, PAGE_SIZE, "%u\n",
		      (u32)(atomic_long_read(&deve->read_bytes) >> 20));
	rcu_read_unlock();
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(read_mbytes);

static ssize_t target_stat_scsi_auth_intr_show_attr_write_mbytes(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1238 1239 1240 1241
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1242 1243 1244
		return -ENODEV;
	}
	/* scsiAuthIntrWrittenMegaBytes */
1245 1246 1247
	ret = snprintf(page, PAGE_SIZE, "%u\n",
		      (u32)(atomic_long_read(&deve->write_bytes) >> 20));
	rcu_read_unlock();
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(write_mbytes);

static ssize_t target_stat_scsi_auth_intr_show_attr_hs_num_cmds(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1261 1262 1263 1264
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1265 1266 1267 1268
		return -ENODEV;
	}
	/* FIXME: scsiAuthIntrHSOutCommands */
	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1269
	rcu_read_unlock();
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(hs_num_cmds);

static ssize_t target_stat_scsi_auth_intr_show_attr_creation_time(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1283 1284 1285 1286
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1287 1288 1289 1290 1291
		return -ENODEV;
	}
	/* scsiAuthIntrLastCreation */
	ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)deve->creation_time -
				INITIAL_JIFFIES) * 100 / HZ));
1292
	rcu_read_unlock();
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(creation_time);

static ssize_t target_stat_scsi_auth_intr_show_attr_row_status(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1306 1307 1308 1309
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1310 1311 1312 1313
		return -ENODEV;
	}
	/* FIXME: scsiAuthIntrRowStatus */
	ret = snprintf(page, PAGE_SIZE, "Ready\n");
1314
	rcu_read_unlock();
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	return ret;
}
DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(row_status);

CONFIGFS_EATTR_OPS(target_stat_scsi_auth_intr, se_ml_stat_grps,
		scsi_auth_intr_group);

static struct configfs_attribute *target_stat_scsi_auth_intr_attrs[] = {
	&target_stat_scsi_auth_intr_inst.attr,
	&target_stat_scsi_auth_intr_dev.attr,
	&target_stat_scsi_auth_intr_port.attr,
	&target_stat_scsi_auth_intr_indx.attr,
	&target_stat_scsi_auth_intr_dev_or_port.attr,
	&target_stat_scsi_auth_intr_intr_name.attr,
	&target_stat_scsi_auth_intr_map_indx.attr,
	&target_stat_scsi_auth_intr_att_count.attr,
	&target_stat_scsi_auth_intr_num_cmds.attr,
	&target_stat_scsi_auth_intr_read_mbytes.attr,
	&target_stat_scsi_auth_intr_write_mbytes.attr,
	&target_stat_scsi_auth_intr_hs_num_cmds.attr,
	&target_stat_scsi_auth_intr_creation_time.attr,
	&target_stat_scsi_auth_intr_row_status.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_auth_intr_attrib_ops = {
	.show_attribute		= target_stat_scsi_auth_intr_attr_show,
	.store_attribute	= target_stat_scsi_auth_intr_attr_store,
};

static struct config_item_type target_stat_scsi_auth_intr_cit = {
	.ct_item_ops		= &target_stat_scsi_auth_intr_attrib_ops,
	.ct_attrs		= target_stat_scsi_auth_intr_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * SCSI Attached Initiator Port Table
 */

CONFIGFS_EATTR_STRUCT(target_stat_scsi_att_intr_port, se_ml_stat_grps);
#define DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR(_name, _mode)			\
static struct target_stat_scsi_att_intr_port_attribute			\
		target_stat_scsi_att_intr_port_##_name =		\
	__CONFIGFS_EATTR(_name, _mode,					\
	target_stat_scsi_att_intr_port_show_attr_##_name,		\
	target_stat_scsi_att_intr_port_store_attr_##_name);

#define DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(_name)			\
static struct target_stat_scsi_att_intr_port_attribute			\
		target_stat_scsi_att_intr_port_##_name =		\
	__CONFIGFS_EATTR_RO(_name,					\
	target_stat_scsi_att_intr_port_show_attr_##_name);

static ssize_t target_stat_scsi_att_intr_port_show_attr_inst(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_portal_group *tpg;
	ssize_t ret;

1379 1380 1381 1382
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1383 1384 1385 1386 1387
		return -ENODEV;
	}
	tpg = nacl->se_tpg;
	/* scsiInstIndex */
	ret = snprintf(page, PAGE_SIZE, "%u\n",
1388
			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1389
	rcu_read_unlock();
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(inst);

static ssize_t target_stat_scsi_att_intr_port_show_attr_dev(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_lun *lun;
	ssize_t ret;

1404 1405 1406 1407
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1408 1409
		return -ENODEV;
	}
1410
	lun = rcu_dereference(deve->se_lun);
1411
	/* scsiDeviceIndex */
1412 1413
	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
	rcu_read_unlock();
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(dev);

static ssize_t target_stat_scsi_att_intr_port_show_attr_port(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	struct se_portal_group *tpg;
	ssize_t ret;

1428 1429 1430 1431
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1432 1433 1434 1435
		return -ENODEV;
	}
	tpg = nacl->se_tpg;
	/* scsiPortIndex */
1436
	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1437
	rcu_read_unlock();
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port);

static ssize_t target_stat_scsi_att_intr_port_show_attr_indx(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_session *se_sess;
	struct se_portal_group *tpg;
	ssize_t ret;

	spin_lock_irq(&nacl->nacl_sess_lock);
	se_sess = nacl->nacl_sess;
	if (!se_sess) {
		spin_unlock_irq(&nacl->nacl_sess_lock);
		return -ENODEV;
	}

	tpg = nacl->se_tpg;
	/* scsiAttIntrPortIndex */
	ret = snprintf(page, PAGE_SIZE, "%u\n",
1462
			tpg->se_tpg_tfo->sess_get_index(se_sess));
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	spin_unlock_irq(&nacl->nacl_sess_lock);
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(indx);

static ssize_t target_stat_scsi_att_intr_port_show_attr_port_auth_indx(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_dev_entry *deve;
	ssize_t ret;

1477 1478 1479 1480
	rcu_read_lock();
	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
	if (!deve) {
		rcu_read_unlock();
1481 1482 1483 1484
		return -ENODEV;
	}
	/* scsiAttIntrPortAuthIntrIdx */
	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1485
	rcu_read_unlock();
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port_auth_indx);

static ssize_t target_stat_scsi_att_intr_port_show_attr_port_ident(
	struct se_ml_stat_grps *lgrps, char *page)
{
	struct se_lun_acl *lacl = container_of(lgrps,
			struct se_lun_acl, ml_stat_grps);
	struct se_node_acl *nacl = lacl->se_lun_nacl;
	struct se_session *se_sess;
	struct se_portal_group *tpg;
	ssize_t ret;
	unsigned char buf[64];

	spin_lock_irq(&nacl->nacl_sess_lock);
	se_sess = nacl->nacl_sess;
	if (!se_sess) {
		spin_unlock_irq(&nacl->nacl_sess_lock);
		return -ENODEV;
	}

	tpg = nacl->se_tpg;
	/* scsiAttIntrPortName+scsiAttIntrPortIdentifier */
	memset(buf, 0, 64);
1511
	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL)
J
Jörn Engel 已提交
1512
		tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, buf, 64);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

	ret = snprintf(page, PAGE_SIZE, "%s+i+%s\n", nacl->initiatorname, buf);
	spin_unlock_irq(&nacl->nacl_sess_lock);
	return ret;
}
DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port_ident);

CONFIGFS_EATTR_OPS(target_stat_scsi_att_intr_port, se_ml_stat_grps,
		scsi_att_intr_port_group);

static struct configfs_attribute *target_stat_scsi_ath_intr_port_attrs[] = {
	&target_stat_scsi_att_intr_port_inst.attr,
	&target_stat_scsi_att_intr_port_dev.attr,
	&target_stat_scsi_att_intr_port_port.attr,
	&target_stat_scsi_att_intr_port_indx.attr,
	&target_stat_scsi_att_intr_port_port_auth_indx.attr,
	&target_stat_scsi_att_intr_port_port_ident.attr,
	NULL,
};

static struct configfs_item_operations target_stat_scsi_att_intr_port_attrib_ops = {
	.show_attribute		= target_stat_scsi_att_intr_port_attr_show,
	.store_attribute	= target_stat_scsi_att_intr_port_attr_store,
};

static struct config_item_type target_stat_scsi_att_intr_port_cit = {
	.ct_item_ops		= &target_stat_scsi_att_intr_port_attrib_ops,
	.ct_attrs		= target_stat_scsi_ath_intr_port_attrs,
	.ct_owner		= THIS_MODULE,
};

/*
 * Called from target_core_fabric_configfs.c:target_fabric_make_mappedlun() to setup
 * the target MappedLUN statistics groups + configfs CITs located in target_core_stat.c
 */
void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *lacl)
{
1550
	struct config_group *ml_stat_grp = &lacl->ml_stat_grps.stat_group;
1551

1552
	config_group_init_type_name(&lacl->ml_stat_grps.scsi_auth_intr_group,
1553
			"scsi_auth_intr", &target_stat_scsi_auth_intr_cit);
1554
	config_group_init_type_name(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1555 1556
			"scsi_att_intr_port", &target_stat_scsi_att_intr_port_cit);

1557 1558
	ml_stat_grp->default_groups[0] = &lacl->ml_stat_grps.scsi_auth_intr_group;
	ml_stat_grp->default_groups[1] = &lacl->ml_stat_grps.scsi_att_intr_port_group;
1559 1560
	ml_stat_grp->default_groups[2] = NULL;
}