Merge tag 'v6.7-p1' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-block.git] / drivers / target / target_core_configfs.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db
NB
2/*******************************************************************************
3 * Filename: target_core_configfs.c
4 *
5 * This file contains ConfigFS logic for the Generic Target Engine project.
6 *
4c76251e 7 * (c) Copyright 2008-2013 Datera, Inc.
c66ac9db
NB
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12 *
c66ac9db
NB
13 ****************************************************************************/
14
e56ca6bc 15#include <linux/kstrtox.h>
c66ac9db
NB
16#include <linux/module.h>
17#include <linux/moduleparam.h>
c66ac9db
NB
18#include <generated/utsrelease.h>
19#include <linux/utsname.h>
20#include <linux/init.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25#include <linux/delay.h>
26#include <linux/unistd.h>
27#include <linux/string.h>
28#include <linux/parser.h>
29#include <linux/syscalls.h>
30#include <linux/configfs.h>
e3d6f909 31#include <linux/spinlock.h>
c66ac9db
NB
32
33#include <target/target_core_base.h>
c4795fb2
CH
34#include <target/target_core_backend.h>
35#include <target/target_core_fabric.h>
c66ac9db 36
e26d99ae 37#include "target_core_internal.h"
c66ac9db 38#include "target_core_alua.h"
c66ac9db
NB
39#include "target_core_pr.h"
40#include "target_core_rd.h"
f99715ac 41#include "target_core_xcopy.h"
c66ac9db 42
73112edc 43#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
0a06d430 44static void target_core_setup_##_name##_cit(struct target_backend *tb) \
73112edc 45{ \
0a06d430 46 struct config_item_type *cit = &tb->tb_##_name##_cit; \
73112edc
NB
47 \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
0a06d430
CH
51 cit->ct_owner = tb->ops->owner; \
52 pr_debug("Setup generic %s\n", __stringify(_name)); \
53}
54
55#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56static void target_core_setup_##_name##_cit(struct target_backend *tb) \
57{ \
58 struct config_item_type *cit = &tb->tb_##_name##_cit; \
59 \
60 cit->ct_item_ops = _item_ops; \
61 cit->ct_group_ops = _group_ops; \
62 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
63 cit->ct_owner = tb->ops->owner; \
73112edc
NB
64 pr_debug("Setup generic %s\n", __stringify(_name)); \
65}
66
e3d6f909
AG
67extern struct t10_alua_lu_gp *default_lu_gp;
68
d0f474e5
RD
69static LIST_HEAD(g_tf_list);
70static DEFINE_MUTEX(g_tf_lock);
c66ac9db 71
e3d6f909
AG
72static struct config_group target_core_hbagroup;
73static struct config_group alua_group;
74static struct config_group alua_lu_gps_group;
75
80890c5e
ML
76static unsigned int target_devices;
77static DEFINE_MUTEX(target_devices_lock);
78
c66ac9db
NB
79static inline struct se_hba *
80item_to_hba(struct config_item *item)
81{
82 return container_of(to_config_group(item), struct se_hba, hba_group);
83}
84
85/*
86 * Attributes for /sys/kernel/config/target/
87 */
2eafd729
CH
88static ssize_t target_core_item_version_show(struct config_item *item,
89 char *page)
c66ac9db
NB
90{
91 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
ce8dd25d 92 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
c66ac9db
NB
93 utsname()->sysname, utsname()->machine);
94}
95
2eafd729 96CONFIGFS_ATTR_RO(target_core_item_, version);
c66ac9db 97
a96e9783
LD
98char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
99static char db_root_stage[DB_ROOT_LEN];
100
101static ssize_t target_core_item_dbroot_show(struct config_item *item,
102 char *page)
103{
104 return sprintf(page, "%s\n", db_root);
105}
106
107static ssize_t target_core_item_dbroot_store(struct config_item *item,
108 const char *page, size_t count)
109{
110 ssize_t read_bytes;
111 struct file *fp;
80890c5e 112 ssize_t r = -EINVAL;
a96e9783 113
80890c5e
ML
114 mutex_lock(&target_devices_lock);
115 if (target_devices) {
116 pr_err("db_root: cannot be changed because it's in use\n");
117 goto unlock;
a96e9783
LD
118 }
119
120 if (count > (DB_ROOT_LEN - 1)) {
a96e9783
LD
121 pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
122 (int)count, DB_ROOT_LEN - 1);
80890c5e 123 goto unlock;
a96e9783
LD
124 }
125
126 read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
80890c5e
ML
127 if (!read_bytes)
128 goto unlock;
129
a96e9783
LD
130 if (db_root_stage[read_bytes - 1] == '\n')
131 db_root_stage[read_bytes - 1] = '\0';
132
133 /* validate new db root before accepting it */
134 fp = filp_open(db_root_stage, O_RDONLY, 0);
135 if (IS_ERR(fp)) {
a96e9783 136 pr_err("db_root: cannot open: %s\n", db_root_stage);
80890c5e 137 goto unlock;
a96e9783 138 }
45063097 139 if (!S_ISDIR(file_inode(fp)->i_mode)) {
8cc3bb07 140 filp_close(fp, NULL);
a96e9783 141 pr_err("db_root: not a directory: %s\n", db_root_stage);
80890c5e 142 goto unlock;
a96e9783 143 }
8cc3bb07 144 filp_close(fp, NULL);
a96e9783
LD
145
146 strncpy(db_root, db_root_stage, read_bytes);
78a6295c
LD
147 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
148
80890c5e
ML
149 r = read_bytes;
150
151unlock:
152 mutex_unlock(&target_devices_lock);
153 return r;
a96e9783
LD
154}
155
156CONFIGFS_ATTR(target_core_item_, dbroot);
157
c66ac9db
NB
158static struct target_fabric_configfs *target_core_get_fabric(
159 const char *name)
160{
161 struct target_fabric_configfs *tf;
162
6708bb27 163 if (!name)
c66ac9db
NB
164 return NULL;
165
166 mutex_lock(&g_tf_lock);
167 list_for_each_entry(tf, &g_tf_list, tf_list) {
59a206b4
DD
168 const char *cmp_name = tf->tf_ops->fabric_alias;
169 if (!cmp_name)
170 cmp_name = tf->tf_ops->fabric_name;
171 if (!strcmp(cmp_name, name)) {
c66ac9db
NB
172 atomic_inc(&tf->tf_access_cnt);
173 mutex_unlock(&g_tf_lock);
174 return tf;
175 }
176 }
177 mutex_unlock(&g_tf_lock);
178
179 return NULL;
180}
181
182/*
183 * Called from struct target_core_group_ops->make_group()
184 */
185static struct config_group *target_core_register_fabric(
186 struct config_group *group,
187 const char *name)
188{
189 struct target_fabric_configfs *tf;
190 int ret;
191
6708bb27 192 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
c66ac9db 193 " %s\n", group, name);
e7b7af6e
RD
194
195 tf = target_core_get_fabric(name);
196 if (!tf) {
62554910
NB
197 pr_debug("target_core_register_fabric() trying autoload for %s\n",
198 name);
e7b7af6e 199
c66ac9db 200 /*
e7b7af6e
RD
201 * Below are some hardcoded request_module() calls to automatically
202 * local fabric modules when the following is called:
c66ac9db 203 *
e7b7af6e 204 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
c66ac9db 205 *
e7b7af6e
RD
206 * Note that this does not limit which TCM fabric module can be
207 * registered, but simply provids auto loading logic for modules with
208 * mkdir(2) system calls with known TCM fabric modules.
c66ac9db 209 */
e7b7af6e
RD
210
211 if (!strncmp(name, "iscsi", 5)) {
212 /*
213 * Automatically load the LIO Target fabric module when the
214 * following is called:
215 *
216 * mkdir -p $CONFIGFS/target/iscsi
217 */
218 ret = request_module("iscsi_target_mod");
219 if (ret < 0) {
62554910
NB
220 pr_debug("request_module() failed for"
221 " iscsi_target_mod.ko: %d\n", ret);
e7b7af6e
RD
222 return ERR_PTR(-EINVAL);
223 }
224 } else if (!strncmp(name, "loopback", 8)) {
225 /*
226 * Automatically load the tcm_loop fabric module when the
227 * following is called:
228 *
229 * mkdir -p $CONFIGFS/target/loopback
230 */
231 ret = request_module("tcm_loop");
232 if (ret < 0) {
62554910
NB
233 pr_debug("request_module() failed for"
234 " tcm_loop.ko: %d\n", ret);
e7b7af6e
RD
235 return ERR_PTR(-EINVAL);
236 }
c66ac9db 237 }
e7b7af6e
RD
238
239 tf = target_core_get_fabric(name);
c66ac9db
NB
240 }
241
6708bb27 242 if (!tf) {
62554910
NB
243 pr_debug("target_core_get_fabric() failed for %s\n",
244 name);
c66ac9db
NB
245 return ERR_PTR(-EINVAL);
246 }
6708bb27 247 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
59a206b4 248 " %s\n", tf->tf_ops->fabric_name);
c66ac9db
NB
249 /*
250 * On a successful target_core_get_fabric() look, the returned
251 * struct target_fabric_configfs *tf will contain a usage reference.
252 */
6708bb27 253 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
968ebe75 254 &tf->tf_wwn_cit);
c66ac9db 255
968ebe75 256 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
1ae1602d 257
c66ac9db 258 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
968ebe75 259 &tf->tf_discovery_cit);
1ae1602d 260 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
c66ac9db 261
6f3bf5a2
BVA
262 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
263 config_item_name(&tf->tf_group.cg_item));
c66ac9db
NB
264 return &tf->tf_group;
265}
266
267/*
268 * Called from struct target_core_group_ops->drop_item()
269 */
270static void target_core_deregister_fabric(
271 struct config_group *group,
272 struct config_item *item)
273{
274 struct target_fabric_configfs *tf = container_of(
275 to_config_group(item), struct target_fabric_configfs, tf_group);
c66ac9db 276
6708bb27 277 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
c66ac9db
NB
278 " tf list\n", config_item_name(item));
279
6708bb27 280 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
59a206b4 281 " %s\n", tf->tf_ops->fabric_name);
c66ac9db
NB
282 atomic_dec(&tf->tf_access_cnt);
283
6708bb27 284 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
c66ac9db
NB
285 " %s\n", config_item_name(item));
286
1ae1602d 287 configfs_remove_default_groups(&tf->tf_group);
c66ac9db
NB
288 config_item_put(item);
289}
290
291static struct configfs_group_operations target_core_fabric_group_ops = {
292 .make_group = &target_core_register_fabric,
293 .drop_item = &target_core_deregister_fabric,
294};
295
296/*
297 * All item attributes appearing in /sys/kernel/target/ appear here.
298 */
299static struct configfs_attribute *target_core_fabric_item_attrs[] = {
300 &target_core_item_attr_version,
a96e9783 301 &target_core_item_attr_dbroot,
c66ac9db
NB
302 NULL,
303};
304
305/*
306 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
307 */
ece550b5 308static const struct config_item_type target_core_fabrics_item = {
c66ac9db
NB
309 .ct_group_ops = &target_core_fabric_group_ops,
310 .ct_attrs = target_core_fabric_item_attrs,
311 .ct_owner = THIS_MODULE,
312};
313
314static struct configfs_subsystem target_core_fabrics = {
315 .su_group = {
316 .cg_item = {
317 .ci_namebuf = "target",
318 .ci_type = &target_core_fabrics_item,
319 },
320 },
321};
322
d588cf8f
CH
323int target_depend_item(struct config_item *item)
324{
325 return configfs_depend_item(&target_core_fabrics, item);
326}
327EXPORT_SYMBOL(target_depend_item);
328
329void target_undepend_item(struct config_item *item)
330{
9a9e3415 331 return configfs_undepend_item(item);
d588cf8f
CH
332}
333EXPORT_SYMBOL(target_undepend_item);
c66ac9db
NB
334
335/*##############################################################################
336// Start functions called by external Target Fabrics Modules
337//############################################################################*/
e5dc6e44
DB
338static int target_disable_feature(struct se_portal_group *se_tpg)
339{
340 return 0;
341}
342
343static u32 target_default_get_inst_index(struct se_portal_group *se_tpg)
344{
345 return 1;
346}
347
348static u32 target_default_sess_get_index(struct se_session *se_sess)
349{
350 return 0;
351}
352
353static void target_set_default_node_attributes(struct se_node_acl *se_acl)
354{
355}
356
357static int target_default_get_cmd_state(struct se_cmd *se_cmd)
358{
359 return 0;
360}
c66ac9db 361
9ac8928e 362static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
c66ac9db 363{
59a206b4
DD
364 if (tfo->fabric_alias) {
365 if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
366 pr_err("Passed alias: %s exceeds "
367 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
368 return -EINVAL;
369 }
c66ac9db 370 }
30c7ca93
DD
371 if (!tfo->fabric_name) {
372 pr_err("Missing tfo->fabric_name\n");
c66ac9db
NB
373 return -EINVAL;
374 }
59a206b4
DD
375 if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
376 pr_err("Passed name: %s exceeds "
377 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
378 return -EINVAL;
379 }
6708bb27
AG
380 if (!tfo->tpg_get_wwn) {
381 pr_err("Missing tfo->tpg_get_wwn()\n");
c66ac9db
NB
382 return -EINVAL;
383 }
6708bb27
AG
384 if (!tfo->tpg_get_tag) {
385 pr_err("Missing tfo->tpg_get_tag()\n");
c66ac9db
NB
386 return -EINVAL;
387 }
35462975 388 if (!tfo->release_cmd) {
6708bb27 389 pr_err("Missing tfo->release_cmd()\n");
c66ac9db
NB
390 return -EINVAL;
391 }
6708bb27
AG
392 if (!tfo->write_pending) {
393 pr_err("Missing tfo->write_pending()\n");
c66ac9db
NB
394 return -EINVAL;
395 }
6708bb27
AG
396 if (!tfo->queue_data_in) {
397 pr_err("Missing tfo->queue_data_in()\n");
c66ac9db
NB
398 return -EINVAL;
399 }
6708bb27
AG
400 if (!tfo->queue_status) {
401 pr_err("Missing tfo->queue_status()\n");
c66ac9db
NB
402 return -EINVAL;
403 }
6708bb27
AG
404 if (!tfo->queue_tm_rsp) {
405 pr_err("Missing tfo->queue_tm_rsp()\n");
c66ac9db
NB
406 return -EINVAL;
407 }
131e6abc
NB
408 if (!tfo->aborted_task) {
409 pr_err("Missing tfo->aborted_task()\n");
410 return -EINVAL;
411 }
9c28ca4f
NB
412 if (!tfo->check_stop_free) {
413 pr_err("Missing tfo->check_stop_free()\n");
414 return -EINVAL;
415 }
c66ac9db
NB
416 /*
417 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
418 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
419 * target_core_fabric_configfs.c WWN+TPG group context code.
420 */
6708bb27
AG
421 if (!tfo->fabric_make_wwn) {
422 pr_err("Missing tfo->fabric_make_wwn()\n");
c66ac9db
NB
423 return -EINVAL;
424 }
6708bb27
AG
425 if (!tfo->fabric_drop_wwn) {
426 pr_err("Missing tfo->fabric_drop_wwn()\n");
c66ac9db
NB
427 return -EINVAL;
428 }
6708bb27
AG
429 if (!tfo->fabric_make_tpg) {
430 pr_err("Missing tfo->fabric_make_tpg()\n");
c66ac9db
NB
431 return -EINVAL;
432 }
6708bb27
AG
433 if (!tfo->fabric_drop_tpg) {
434 pr_err("Missing tfo->fabric_drop_tpg()\n");
c66ac9db
NB
435 return -EINVAL;
436 }
437
438 return 0;
439}
440
e5dc6e44
DB
441static void target_set_default_ops(struct target_core_fabric_ops *tfo)
442{
443 if (!tfo->tpg_check_demo_mode)
444 tfo->tpg_check_demo_mode = target_disable_feature;
445
446 if (!tfo->tpg_check_demo_mode_cache)
447 tfo->tpg_check_demo_mode_cache = target_disable_feature;
448
449 if (!tfo->tpg_check_demo_mode_write_protect)
450 tfo->tpg_check_demo_mode_write_protect = target_disable_feature;
451
452 if (!tfo->tpg_check_prod_mode_write_protect)
453 tfo->tpg_check_prod_mode_write_protect = target_disable_feature;
454
455 if (!tfo->tpg_get_inst_index)
456 tfo->tpg_get_inst_index = target_default_get_inst_index;
457
458 if (!tfo->sess_get_index)
459 tfo->sess_get_index = target_default_sess_get_index;
460
461 if (!tfo->set_default_node_attributes)
462 tfo->set_default_node_attributes = target_set_default_node_attributes;
463
464 if (!tfo->get_cmd_state)
465 tfo->get_cmd_state = target_default_get_cmd_state;
466}
467
9ac8928e 468int target_register_template(const struct target_core_fabric_ops *fo)
c66ac9db 469{
e5dc6e44 470 struct target_core_fabric_ops *tfo;
9ac8928e 471 struct target_fabric_configfs *tf;
c66ac9db
NB
472 int ret;
473
9ac8928e
CH
474 ret = target_fabric_tf_ops_check(fo);
475 if (ret)
476 return ret;
477
478 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
6708bb27 479 if (!tf) {
9ac8928e
CH
480 pr_err("%s: could not allocate memory!\n", __func__);
481 return -ENOMEM;
c66ac9db 482 }
e5dc6e44
DB
483 tfo = kzalloc(sizeof(struct target_core_fabric_ops), GFP_KERNEL);
484 if (!tfo) {
485 kfree(tf);
486 pr_err("%s: could not allocate memory!\n", __func__);
487 return -ENOMEM;
488 }
489 memcpy(tfo, fo, sizeof(*tfo));
490 target_set_default_ops(tfo);
c66ac9db 491
9ac8928e
CH
492 INIT_LIST_HEAD(&tf->tf_list);
493 atomic_set(&tf->tf_access_cnt, 0);
e5dc6e44 494 tf->tf_ops = tfo;
9ac8928e
CH
495 target_fabric_setup_cits(tf);
496
497 mutex_lock(&g_tf_lock);
498 list_add_tail(&tf->tf_list, &g_tf_list);
499 mutex_unlock(&g_tf_lock);
500
c66ac9db
NB
501 return 0;
502}
9ac8928e 503EXPORT_SYMBOL(target_register_template);
c66ac9db 504
9ac8928e 505void target_unregister_template(const struct target_core_fabric_ops *fo)
c66ac9db 506{
9ac8928e 507 struct target_fabric_configfs *t;
c66ac9db 508
c66ac9db 509 mutex_lock(&g_tf_lock);
9ac8928e 510 list_for_each_entry(t, &g_tf_list, tf_list) {
59a206b4 511 if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
9ac8928e
CH
512 BUG_ON(atomic_read(&t->tf_access_cnt));
513 list_del(&t->tf_list);
94509182
NB
514 mutex_unlock(&g_tf_lock);
515 /*
516 * Wait for any outstanding fabric se_deve_entry->rcu_head
517 * callbacks to complete post kfree_rcu(), before allowing
518 * fabric driver unload of TFO->module to proceed.
519 */
520 rcu_barrier();
80ed33c8 521 kfree(t->tf_tpg_base_cit.ct_attrs);
e5dc6e44 522 kfree(t->tf_ops);
9ac8928e 523 kfree(t);
94509182 524 return;
9ac8928e 525 }
c66ac9db 526 }
c66ac9db 527 mutex_unlock(&g_tf_lock);
c66ac9db 528}
9ac8928e 529EXPORT_SYMBOL(target_unregister_template);
c66ac9db
NB
530
531/*##############################################################################
532// Stop functions called by external Target Fabrics Modules
533//############################################################################*/
534
2eafd729
CH
535static inline struct se_dev_attrib *to_attrib(struct config_item *item)
536{
537 return container_of(to_config_group(item), struct se_dev_attrib,
538 da_group);
539}
540
f79a897e 541/* Start functions for struct config_item_type tb_dev_attrib_cit */
2eafd729
CH
542#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
543static ssize_t _name##_show(struct config_item *item, char *page) \
5873c4d1 544{ \
2eafd729
CH
545 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
546}
547
548DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
549DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
550DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
551DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
552DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
553DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
554DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
555DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
556DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
557DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
558DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
b49d6f78 559DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
2eafd729
CH
560DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
561DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
056e8924 562DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
2eafd729
CH
563DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
564DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
565DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
566DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
567DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
568DEF_CONFIGFS_ATTRIB_SHOW(block_size);
569DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
570DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
571DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
572DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
573DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
574DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
575DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
576DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
e6f41633 577DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
2eafd729 578DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
bd217b8c 579DEF_CONFIGFS_ATTRIB_SHOW(emulate_rsoc);
e2f4ea40 580DEF_CONFIGFS_ATTRIB_SHOW(submit_type);
2eafd729
CH
581
582#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
583static ssize_t _name##_store(struct config_item *item, const char *page,\
3effdb90 584 size_t count) \
5873c4d1 585{ \
2eafd729 586 struct se_dev_attrib *da = to_attrib(item); \
3effdb90 587 u32 val; \
5873c4d1
CH
588 int ret; \
589 \
3effdb90
CH
590 ret = kstrtou32(page, 0, &val); \
591 if (ret < 0) \
592 return ret; \
593 da->_name = val; \
594 return count; \
595}
596
2eafd729
CH
597DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
598DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
599DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
600DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
601DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
3effdb90 602
2eafd729
CH
603#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
604static ssize_t _name##_store(struct config_item *item, const char *page, \
3effdb90
CH
605 size_t count) \
606{ \
2eafd729 607 struct se_dev_attrib *da = to_attrib(item); \
3effdb90
CH
608 bool flag; \
609 int ret; \
5873c4d1 610 \
e56ca6bc 611 ret = kstrtobool(page, &flag); \
3effdb90
CH
612 if (ret < 0) \
613 return ret; \
614 da->_name = flag; \
615 return count; \
616}
617
2eafd729
CH
618DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
619DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
620DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
b49d6f78 621DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
2eafd729
CH
622DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
623DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
3effdb90 624
2eafd729
CH
625#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
626static ssize_t _name##_store(struct config_item *item, const char *page,\
3effdb90
CH
627 size_t count) \
628{ \
629 printk_once(KERN_WARNING \
234bdbc4
CVB
630 "ignoring deprecated %s attribute\n", \
631 __stringify(_name)); \
3effdb90
CH
632 return count; \
633}
634
2eafd729
CH
635DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
636DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
3effdb90
CH
637
638static void dev_set_t10_wwn_model_alias(struct se_device *dev)
639{
640 const char *configname;
641
642 configname = config_item_name(&dev->dev_group.cg_item);
b2da4abf 643 if (strlen(configname) >= INQUIRY_MODEL_LEN) {
3effdb90 644 pr_warn("dev[%p]: Backstore name '%s' is too long for "
b2da4abf 645 "INQUIRY_MODEL, truncating to 15 characters\n", dev,
3effdb90
CH
646 configname);
647 }
b2da4abf
DD
648 /*
649 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
650 * here without potentially breaking existing setups, so continue to
651 * truncate one byte shorter than what can be carried in INQUIRY.
652 */
0871237a 653 strscpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
3effdb90
CH
654}
655
2eafd729 656static ssize_t emulate_model_alias_store(struct config_item *item,
3effdb90
CH
657 const char *page, size_t count)
658{
2eafd729 659 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
660 struct se_device *dev = da->da_dev;
661 bool flag;
662 int ret;
663
664 if (dev->export_count) {
665 pr_err("dev[%p]: Unable to change model alias"
666 " while export_count is %d\n",
667 dev, dev->export_count);
668 return -EINVAL;
669 }
670
e56ca6bc 671 ret = kstrtobool(page, &flag);
3effdb90
CH
672 if (ret < 0)
673 return ret;
674
b2da4abf 675 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
3effdb90
CH
676 if (flag) {
677 dev_set_t10_wwn_model_alias(dev);
678 } else {
0871237a 679 strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
b2da4abf 680 sizeof(dev->t10_wwn.model));
3effdb90
CH
681 }
682 da->emulate_model_alias = flag;
683 return count;
684}
685
2eafd729 686static ssize_t emulate_write_cache_store(struct config_item *item,
3effdb90
CH
687 const char *page, size_t count)
688{
2eafd729 689 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
690 bool flag;
691 int ret;
692
e56ca6bc 693 ret = kstrtobool(page, &flag);
3effdb90
CH
694 if (ret < 0)
695 return ret;
696
697 if (flag && da->da_dev->transport->get_write_cache) {
698 pr_err("emulate_write_cache not supported for this device\n");
699 return -EINVAL;
700 }
701
702 da->emulate_write_cache = flag;
703 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
704 da->da_dev, flag);
705 return count;
706}
707
2eafd729 708static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
3effdb90
CH
709 const char *page, size_t count)
710{
2eafd729 711 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
712 u32 val;
713 int ret;
714
715 ret = kstrtou32(page, 0, &val);
716 if (ret < 0)
717 return ret;
718
1bf630fd
DD
719 if (val != TARGET_UA_INTLCK_CTRL_CLEAR
720 && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
721 && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
3effdb90
CH
722 pr_err("Illegal value %d\n", val);
723 return -EINVAL;
724 }
725
726 if (da->da_dev->export_count) {
727 pr_err("dev[%p]: Unable to change SE Device"
728 " UA_INTRLCK_CTRL while export_count is %d\n",
729 da->da_dev, da->da_dev->export_count);
730 return -EINVAL;
731 }
732 da->emulate_ua_intlck_ctrl = val;
733 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
734 da->da_dev, val);
735 return count;
736}
737
2eafd729 738static ssize_t emulate_tas_store(struct config_item *item,
3effdb90
CH
739 const char *page, size_t count)
740{
2eafd729 741 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
742 bool flag;
743 int ret;
744
e56ca6bc 745 ret = kstrtobool(page, &flag);
3effdb90
CH
746 if (ret < 0)
747 return ret;
748
749 if (da->da_dev->export_count) {
750 pr_err("dev[%p]: Unable to change SE Device TAS while"
751 " export_count is %d\n",
752 da->da_dev, da->da_dev->export_count);
753 return -EINVAL;
754 }
755 da->emulate_tas = flag;
756 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
757 da->da_dev, flag ? "Enabled" : "Disabled");
758
759 return count;
760}
761
2eafd729 762static ssize_t emulate_tpu_store(struct config_item *item,
3effdb90
CH
763 const char *page, size_t count)
764{
2eafd729 765 struct se_dev_attrib *da = to_attrib(item);
34bd1dca 766 struct se_device *dev = da->da_dev;
3effdb90
CH
767 bool flag;
768 int ret;
769
e56ca6bc 770 ret = kstrtobool(page, &flag);
3effdb90
CH
771 if (ret < 0)
772 return ret;
773
774 /*
775 * We expect this value to be non-zero when generic Block Layer
776 * Discard supported is detected iblock_create_virtdevice().
777 */
778 if (flag && !da->max_unmap_block_desc_count) {
34bd1dca
MC
779 if (!dev->transport->configure_unmap ||
780 !dev->transport->configure_unmap(dev)) {
781 pr_err("Generic Block Discard not supported\n");
782 return -ENOSYS;
783 }
3effdb90
CH
784 }
785
786 da->emulate_tpu = flag;
787 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
788 da->da_dev, flag);
789 return count;
790}
791
2eafd729 792static ssize_t emulate_tpws_store(struct config_item *item,
3effdb90
CH
793 const char *page, size_t count)
794{
2eafd729 795 struct se_dev_attrib *da = to_attrib(item);
34bd1dca 796 struct se_device *dev = da->da_dev;
3effdb90
CH
797 bool flag;
798 int ret;
799
e56ca6bc 800 ret = kstrtobool(page, &flag);
3effdb90
CH
801 if (ret < 0)
802 return ret;
803
804 /*
805 * We expect this value to be non-zero when generic Block Layer
806 * Discard supported is detected iblock_create_virtdevice().
807 */
808 if (flag && !da->max_unmap_block_desc_count) {
34bd1dca
MC
809 if (!dev->transport->configure_unmap ||
810 !dev->transport->configure_unmap(dev)) {
811 pr_err("Generic Block Discard not supported\n");
812 return -ENOSYS;
813 }
3effdb90
CH
814 }
815
816 da->emulate_tpws = flag;
817 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
818 da->da_dev, flag);
819 return count;
820}
821
2eafd729 822static ssize_t pi_prot_type_store(struct config_item *item,
3effdb90
CH
823 const char *page, size_t count)
824{
2eafd729 825 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
826 int old_prot = da->pi_prot_type, ret;
827 struct se_device *dev = da->da_dev;
828 u32 flag;
829
830 ret = kstrtou32(page, 0, &flag);
831 if (ret < 0)
832 return ret;
833
834 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
835 pr_err("Illegal value %d for pi_prot_type\n", flag);
836 return -EINVAL;
837 }
838 if (flag == 2) {
839 pr_err("DIF TYPE2 protection currently not supported\n");
840 return -ENOSYS;
841 }
842 if (da->hw_pi_prot_type) {
843 pr_warn("DIF protection enabled on underlying hardware,"
844 " ignoring\n");
845 return count;
846 }
847 if (!dev->transport->init_prot || !dev->transport->free_prot) {
848 /* 0 is only allowed value for non-supporting backends */
849 if (flag == 0)
bc1a7d6a 850 return count;
3effdb90
CH
851
852 pr_err("DIF protection not supported by backend: %s\n",
853 dev->transport->name);
854 return -ENOSYS;
855 }
cb0f32e1 856 if (!target_dev_configured(dev)) {
3effdb90
CH
857 pr_err("DIF protection requires device to be configured\n");
858 return -ENODEV;
859 }
860 if (dev->export_count) {
861 pr_err("dev[%p]: Unable to change SE Device PROT type while"
862 " export_count is %d\n", dev, dev->export_count);
863 return -EINVAL;
864 }
865
866 da->pi_prot_type = flag;
867
868 if (flag && !old_prot) {
869 ret = dev->transport->init_prot(dev);
870 if (ret) {
871 da->pi_prot_type = old_prot;
056e8924 872 da->pi_prot_verify = (bool) da->pi_prot_type;
3effdb90
CH
873 return ret;
874 }
875
876 } else if (!flag && old_prot) {
877 dev->transport->free_prot(dev);
878 }
879
056e8924 880 da->pi_prot_verify = (bool) da->pi_prot_type;
3effdb90
CH
881 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
882 return count;
883}
884
b6cd7f34
DD
885/* always zero, but attr needs to remain RW to avoid userspace breakage */
886static ssize_t pi_prot_format_show(struct config_item *item, char *page)
887{
888 return snprintf(page, PAGE_SIZE, "0\n");
889}
890
2eafd729 891static ssize_t pi_prot_format_store(struct config_item *item,
3effdb90
CH
892 const char *page, size_t count)
893{
2eafd729 894 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
895 struct se_device *dev = da->da_dev;
896 bool flag;
897 int ret;
898
e56ca6bc 899 ret = kstrtobool(page, &flag);
3effdb90
CH
900 if (ret < 0)
901 return ret;
902
903 if (!flag)
904 return count;
905
906 if (!dev->transport->format_prot) {
907 pr_err("DIF protection format not supported by backend %s\n",
908 dev->transport->name);
909 return -ENOSYS;
910 }
cb0f32e1 911 if (!target_dev_configured(dev)) {
3effdb90
CH
912 pr_err("DIF protection format requires device to be configured\n");
913 return -ENODEV;
914 }
915 if (dev->export_count) {
916 pr_err("dev[%p]: Unable to format SE Device PROT type while"
917 " export_count is %d\n", dev, dev->export_count);
918 return -EINVAL;
919 }
920
921 ret = dev->transport->format_prot(dev);
922 if (ret)
923 return ret;
924
925 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
926 return count;
927}
928
056e8924
DM
929static ssize_t pi_prot_verify_store(struct config_item *item,
930 const char *page, size_t count)
931{
932 struct se_dev_attrib *da = to_attrib(item);
933 bool flag;
934 int ret;
935
e56ca6bc 936 ret = kstrtobool(page, &flag);
056e8924
DM
937 if (ret < 0)
938 return ret;
939
940 if (!flag) {
941 da->pi_prot_verify = flag;
942 return count;
943 }
944 if (da->hw_pi_prot_type) {
945 pr_warn("DIF protection enabled on underlying hardware,"
946 " ignoring\n");
947 return count;
948 }
949 if (!da->pi_prot_type) {
950 pr_warn("DIF protection not supported by backend, ignoring\n");
951 return count;
952 }
953 da->pi_prot_verify = flag;
954
955 return count;
956}
957
2eafd729 958static ssize_t force_pr_aptpl_store(struct config_item *item,
3effdb90
CH
959 const char *page, size_t count)
960{
2eafd729 961 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
962 bool flag;
963 int ret;
964
e56ca6bc 965 ret = kstrtobool(page, &flag);
3effdb90
CH
966 if (ret < 0)
967 return ret;
968 if (da->da_dev->export_count) {
969 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
970 " export_count is %d\n",
971 da->da_dev, da->da_dev->export_count);
972 return -EINVAL;
973 }
974
975 da->force_pr_aptpl = flag;
976 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
977 return count;
978}
979
2eafd729 980static ssize_t emulate_rest_reord_store(struct config_item *item,
3effdb90
CH
981 const char *page, size_t count)
982{
2eafd729 983 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
984 bool flag;
985 int ret;
986
e56ca6bc 987 ret = kstrtobool(page, &flag);
3effdb90
CH
988 if (ret < 0)
989 return ret;
990
991 if (flag != 0) {
992 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
993 " reordering not implemented\n", da->da_dev);
994 return -ENOSYS;
995 }
996 da->emulate_rest_reord = flag;
997 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
998 da->da_dev, flag);
999 return count;
1000}
1001
e6f41633
JP
1002static ssize_t unmap_zeroes_data_store(struct config_item *item,
1003 const char *page, size_t count)
1004{
1005 struct se_dev_attrib *da = to_attrib(item);
34bd1dca 1006 struct se_device *dev = da->da_dev;
e6f41633
JP
1007 bool flag;
1008 int ret;
1009
e56ca6bc 1010 ret = kstrtobool(page, &flag);
e6f41633
JP
1011 if (ret < 0)
1012 return ret;
1013
1014 if (da->da_dev->export_count) {
1015 pr_err("dev[%p]: Unable to change SE Device"
1016 " unmap_zeroes_data while export_count is %d\n",
1017 da->da_dev, da->da_dev->export_count);
1018 return -EINVAL;
1019 }
1020 /*
1021 * We expect this value to be non-zero when generic Block Layer
1022 * Discard supported is detected iblock_configure_device().
1023 */
1024 if (flag && !da->max_unmap_block_desc_count) {
34bd1dca
MC
1025 if (!dev->transport->configure_unmap ||
1026 !dev->transport->configure_unmap(dev)) {
1027 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
1028 da->da_dev);
1029 return -ENOSYS;
1030 }
e6f41633
JP
1031 }
1032 da->unmap_zeroes_data = flag;
1033 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
1034 da->da_dev, flag);
2e498f25 1035 return count;
e6f41633
JP
1036}
1037
3effdb90
CH
1038/*
1039 * Note, this can only be called on unexported SE Device Object.
1040 */
2eafd729 1041static ssize_t queue_depth_store(struct config_item *item,
3effdb90
CH
1042 const char *page, size_t count)
1043{
2eafd729 1044 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1045 struct se_device *dev = da->da_dev;
1046 u32 val;
1047 int ret;
1048
1049 ret = kstrtou32(page, 0, &val);
1050 if (ret < 0)
1051 return ret;
1052
1053 if (dev->export_count) {
1054 pr_err("dev[%p]: Unable to change SE Device TCQ while"
1055 " export_count is %d\n",
1056 dev, dev->export_count);
1057 return -EINVAL;
1058 }
1059 if (!val) {
1060 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
1061 return -EINVAL;
1062 }
1063
1064 if (val > dev->dev_attrib.queue_depth) {
1065 if (val > dev->dev_attrib.hw_queue_depth) {
1066 pr_err("dev[%p]: Passed queue_depth:"
1067 " %u exceeds TCM/SE_Device MAX"
1068 " TCQ: %u\n", dev, val,
1069 dev->dev_attrib.hw_queue_depth);
1070 return -EINVAL;
1071 }
1072 }
1073 da->queue_depth = dev->queue_depth = val;
1074 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
1075 return count;
1076}
1077
2eafd729 1078static ssize_t optimal_sectors_store(struct config_item *item,
3effdb90
CH
1079 const char *page, size_t count)
1080{
2eafd729 1081 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1082 u32 val;
1083 int ret;
1084
1085 ret = kstrtou32(page, 0, &val);
1086 if (ret < 0)
1087 return ret;
1088
1089 if (da->da_dev->export_count) {
1090 pr_err("dev[%p]: Unable to change SE Device"
1091 " optimal_sectors while export_count is %d\n",
1092 da->da_dev, da->da_dev->export_count);
1093 return -EINVAL;
1094 }
1095 if (val > da->hw_max_sectors) {
1096 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1097 " greater than hw_max_sectors: %u\n",
1098 da->da_dev, val, da->hw_max_sectors);
1099 return -EINVAL;
1100 }
1101
1102 da->optimal_sectors = val;
1103 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1104 da->da_dev, val);
1105 return count;
5873c4d1
CH
1106}
1107
2eafd729 1108static ssize_t block_size_store(struct config_item *item,
3effdb90
CH
1109 const char *page, size_t count)
1110{
2eafd729 1111 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1112 u32 val;
1113 int ret;
1114
1115 ret = kstrtou32(page, 0, &val);
1116 if (ret < 0)
1117 return ret;
5873c4d1 1118
3effdb90
CH
1119 if (da->da_dev->export_count) {
1120 pr_err("dev[%p]: Unable to change SE Device block_size"
1121 " while export_count is %d\n",
1122 da->da_dev, da->da_dev->export_count);
1123 return -EINVAL;
1124 }
1125
1126 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1127 pr_err("dev[%p]: Illegal value for block_device: %u"
1128 " for SE device, must be 512, 1024, 2048 or 4096\n",
1129 da->da_dev, val);
1130 return -EINVAL;
1131 }
1132
1133 da->block_size = val;
3effdb90
CH
1134
1135 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1136 da->da_dev, val);
1137 return count;
1138}
5873c4d1 1139
c17d5d5f
MC
1140static ssize_t alua_support_show(struct config_item *item, char *page)
1141{
1142 struct se_dev_attrib *da = to_attrib(item);
69088a04 1143 u8 flags = da->da_dev->transport_flags;
c17d5d5f
MC
1144
1145 return snprintf(page, PAGE_SIZE, "%d\n",
1146 flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1147}
1148
356ba2a8
BS
1149static ssize_t alua_support_store(struct config_item *item,
1150 const char *page, size_t count)
1151{
1152 struct se_dev_attrib *da = to_attrib(item);
1153 struct se_device *dev = da->da_dev;
ef7ae7f7 1154 bool flag, oldflag;
356ba2a8
BS
1155 int ret;
1156
e56ca6bc 1157 ret = kstrtobool(page, &flag);
ef7ae7f7
ML
1158 if (ret < 0)
1159 return ret;
1160
1161 oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1162 if (flag == oldflag)
1163 return count;
1164
356ba2a8
BS
1165 if (!(dev->transport->transport_flags_changeable &
1166 TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1167 pr_err("dev[%p]: Unable to change SE Device alua_support:"
1168 " alua_support has fixed value\n", dev);
ef7ae7f7 1169 return -ENOSYS;
356ba2a8
BS
1170 }
1171
356ba2a8
BS
1172 if (flag)
1173 dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1174 else
1175 dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1176 return count;
1177}
1178
c17d5d5f
MC
1179static ssize_t pgr_support_show(struct config_item *item, char *page)
1180{
1181 struct se_dev_attrib *da = to_attrib(item);
69088a04 1182 u8 flags = da->da_dev->transport_flags;
c17d5d5f
MC
1183
1184 return snprintf(page, PAGE_SIZE, "%d\n",
1185 flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1186}
1187
356ba2a8
BS
1188static ssize_t pgr_support_store(struct config_item *item,
1189 const char *page, size_t count)
1190{
1191 struct se_dev_attrib *da = to_attrib(item);
1192 struct se_device *dev = da->da_dev;
ef7ae7f7 1193 bool flag, oldflag;
356ba2a8
BS
1194 int ret;
1195
e56ca6bc 1196 ret = kstrtobool(page, &flag);
ef7ae7f7
ML
1197 if (ret < 0)
1198 return ret;
1199
1200 oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1201 if (flag == oldflag)
1202 return count;
1203
356ba2a8
BS
1204 if (!(dev->transport->transport_flags_changeable &
1205 TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1206 pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1207 " pgr_support has fixed value\n", dev);
ef7ae7f7 1208 return -ENOSYS;
356ba2a8
BS
1209 }
1210
356ba2a8
BS
1211 if (flag)
1212 dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1213 else
1214 dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1215 return count;
1216}
1217
bd217b8c
DB
1218static ssize_t emulate_rsoc_store(struct config_item *item,
1219 const char *page, size_t count)
1220{
1221 struct se_dev_attrib *da = to_attrib(item);
1222 bool flag;
1223 int ret;
1224
e56ca6bc 1225 ret = kstrtobool(page, &flag);
bd217b8c
DB
1226 if (ret < 0)
1227 return ret;
1228
1229 da->emulate_rsoc = flag;
1230 pr_debug("dev[%p]: SE Device REPORT_SUPPORTED_OPERATION_CODES_EMULATION flag: %d\n",
1231 da->da_dev, flag);
1232 return count;
1233}
1234
e2f4ea40
MC
1235static ssize_t submit_type_store(struct config_item *item, const char *page,
1236 size_t count)
1237{
1238 struct se_dev_attrib *da = to_attrib(item);
1239 int ret;
1240 u8 val;
1241
1242 ret = kstrtou8(page, 0, &val);
1243 if (ret < 0)
1244 return ret;
1245
1246 if (val > TARGET_QUEUE_SUBMIT)
1247 return -EINVAL;
1248
1249 da->submit_type = val;
1250 return count;
1251}
1252
2eafd729
CH
1253CONFIGFS_ATTR(, emulate_model_alias);
1254CONFIGFS_ATTR(, emulate_dpo);
1255CONFIGFS_ATTR(, emulate_fua_write);
1256CONFIGFS_ATTR(, emulate_fua_read);
1257CONFIGFS_ATTR(, emulate_write_cache);
1258CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1259CONFIGFS_ATTR(, emulate_tas);
1260CONFIGFS_ATTR(, emulate_tpu);
1261CONFIGFS_ATTR(, emulate_tpws);
1262CONFIGFS_ATTR(, emulate_caw);
1263CONFIGFS_ATTR(, emulate_3pc);
b49d6f78 1264CONFIGFS_ATTR(, emulate_pr);
bd217b8c 1265CONFIGFS_ATTR(, emulate_rsoc);
2eafd729
CH
1266CONFIGFS_ATTR(, pi_prot_type);
1267CONFIGFS_ATTR_RO(, hw_pi_prot_type);
b6cd7f34 1268CONFIGFS_ATTR(, pi_prot_format);
056e8924 1269CONFIGFS_ATTR(, pi_prot_verify);
2eafd729
CH
1270CONFIGFS_ATTR(, enforce_pr_isids);
1271CONFIGFS_ATTR(, is_nonrot);
1272CONFIGFS_ATTR(, emulate_rest_reord);
1273CONFIGFS_ATTR(, force_pr_aptpl);
1274CONFIGFS_ATTR_RO(, hw_block_size);
1275CONFIGFS_ATTR(, block_size);
1276CONFIGFS_ATTR_RO(, hw_max_sectors);
1277CONFIGFS_ATTR(, optimal_sectors);
1278CONFIGFS_ATTR_RO(, hw_queue_depth);
1279CONFIGFS_ATTR(, queue_depth);
1280CONFIGFS_ATTR(, max_unmap_lba_count);
1281CONFIGFS_ATTR(, max_unmap_block_desc_count);
1282CONFIGFS_ATTR(, unmap_granularity);
1283CONFIGFS_ATTR(, unmap_granularity_alignment);
e6f41633 1284CONFIGFS_ATTR(, unmap_zeroes_data);
2eafd729 1285CONFIGFS_ATTR(, max_write_same_len);
356ba2a8
BS
1286CONFIGFS_ATTR(, alua_support);
1287CONFIGFS_ATTR(, pgr_support);
e2f4ea40 1288CONFIGFS_ATTR(, submit_type);
c66ac9db 1289
5873c4d1
CH
1290/*
1291 * dev_attrib attributes for devices using the target core SBC/SPC
1292 * interpreter. Any backend using spc_parse_cdb should be using
1293 * these.
1294 */
1295struct configfs_attribute *sbc_attrib_attrs[] = {
2eafd729
CH
1296 &attr_emulate_model_alias,
1297 &attr_emulate_dpo,
1298 &attr_emulate_fua_write,
1299 &attr_emulate_fua_read,
1300 &attr_emulate_write_cache,
1301 &attr_emulate_ua_intlck_ctrl,
1302 &attr_emulate_tas,
1303 &attr_emulate_tpu,
1304 &attr_emulate_tpws,
1305 &attr_emulate_caw,
1306 &attr_emulate_3pc,
b49d6f78 1307 &attr_emulate_pr,
2eafd729
CH
1308 &attr_pi_prot_type,
1309 &attr_hw_pi_prot_type,
1310 &attr_pi_prot_format,
056e8924 1311 &attr_pi_prot_verify,
2eafd729
CH
1312 &attr_enforce_pr_isids,
1313 &attr_is_nonrot,
1314 &attr_emulate_rest_reord,
1315 &attr_force_pr_aptpl,
1316 &attr_hw_block_size,
1317 &attr_block_size,
1318 &attr_hw_max_sectors,
1319 &attr_optimal_sectors,
1320 &attr_hw_queue_depth,
1321 &attr_queue_depth,
1322 &attr_max_unmap_lba_count,
1323 &attr_max_unmap_block_desc_count,
1324 &attr_unmap_granularity,
1325 &attr_unmap_granularity_alignment,
e6f41633 1326 &attr_unmap_zeroes_data,
2eafd729 1327 &attr_max_write_same_len,
c17d5d5f
MC
1328 &attr_alua_support,
1329 &attr_pgr_support,
bd217b8c 1330 &attr_emulate_rsoc,
e2f4ea40 1331 &attr_submit_type,
5873c4d1
CH
1332 NULL,
1333};
1334EXPORT_SYMBOL(sbc_attrib_attrs);
1335
5873c4d1
CH
1336/*
1337 * Minimal dev_attrib attributes for devices passing through CDBs.
1338 * In this case we only provide a few read-only attributes for
1339 * backwards compatibility.
1340 */
1341struct configfs_attribute *passthrough_attrib_attrs[] = {
2eafd729
CH
1342 &attr_hw_pi_prot_type,
1343 &attr_hw_block_size,
1344 &attr_hw_max_sectors,
1345 &attr_hw_queue_depth,
92999417 1346 &attr_emulate_pr,
c17d5d5f
MC
1347 &attr_alua_support,
1348 &attr_pgr_support,
e2f4ea40 1349 &attr_submit_type,
5873c4d1
CH
1350 NULL,
1351};
1352EXPORT_SYMBOL(passthrough_attrib_attrs);
1353
4703b625
BS
1354/*
1355 * pr related dev_attrib attributes for devices passing through CDBs,
1356 * but allowing in core pr emulation.
1357 */
1358struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
1359 &attr_enforce_pr_isids,
1360 &attr_force_pr_aptpl,
1361 NULL,
1362};
1363EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
1364
2eafd729 1365TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
8dc31ff9 1366TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
c66ac9db 1367
f79a897e 1368/* End functions for struct config_item_type tb_dev_attrib_cit */
c66ac9db 1369
f8d389c6 1370/* Start functions for struct config_item_type tb_dev_wwn_cit */
c66ac9db 1371
2eafd729
CH
1372static struct t10_wwn *to_t10_wwn(struct config_item *item)
1373{
1374 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1375}
c66ac9db 1376
0322913c
AA
1377static ssize_t target_check_inquiry_data(char *buf)
1378{
1379 size_t len;
1380 int i;
1381
1382 len = strlen(buf);
1383
1384 /*
1385 * SPC 4.3.1:
1386 * ASCII data fields shall contain only ASCII printable characters
1387 * (i.e., code values 20h to 7Eh) and may be terminated with one or
1388 * more ASCII null (00h) characters.
1389 */
1390 for (i = 0; i < len; i++) {
1391 if (buf[i] < 0x20 || buf[i] > 0x7E) {
1392 pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
1393 return -EINVAL;
1394 }
1395 }
1396
1397 return len;
1398}
1399
54a6f3f6
DD
1400/*
1401 * STANDARD and VPD page 0x83 T10 Vendor Identification
1402 */
1403static ssize_t target_wwn_vendor_id_show(struct config_item *item,
1404 char *page)
1405{
1406 return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
1407}
1408
1409static ssize_t target_wwn_vendor_id_store(struct config_item *item,
1410 const char *page, size_t count)
1411{
1412 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1413 struct se_device *dev = t10_wwn->t10_dev;
1414 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1415 unsigned char buf[INQUIRY_VENDOR_LEN + 2];
1416 char *stripped = NULL;
5c584fe6 1417 ssize_t len;
ee26724a 1418 ssize_t ret;
54a6f3f6 1419
5c584fe6
AS
1420 len = strscpy(buf, page, sizeof(buf));
1421 if (len > 0) {
54a6f3f6
DD
1422 /* Strip any newline added from userspace. */
1423 stripped = strstrip(buf);
1424 len = strlen(stripped);
1425 }
5c584fe6 1426 if (len < 0 || len > INQUIRY_VENDOR_LEN) {
54a6f3f6
DD
1427 pr_err("Emulated T10 Vendor Identification exceeds"
1428 " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
1429 "\n");
1430 return -EOVERFLOW;
1431 }
1432
0322913c
AA
1433 ret = target_check_inquiry_data(stripped);
1434
1435 if (ret < 0)
1436 return ret;
54a6f3f6
DD
1437
1438 /*
1439 * Check to see if any active exports exist. If they do exist, fail
1440 * here as changing this information on the fly (underneath the
1441 * initiator side OS dependent multipath code) could cause negative
1442 * effects.
1443 */
1444 if (dev->export_count) {
1445 pr_err("Unable to set T10 Vendor Identification while"
1446 " active %d exports exist\n", dev->export_count);
1447 return -EINVAL;
1448 }
1449
1450 BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
0871237a 1451 strscpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
54a6f3f6
DD
1452
1453 pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
1454 " %s\n", dev->t10_wwn.vendor);
1455
1456 return count;
1457}
1458
0322913c
AA
1459static ssize_t target_wwn_product_id_show(struct config_item *item,
1460 char *page)
1461{
1462 return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
1463}
1464
1465static ssize_t target_wwn_product_id_store(struct config_item *item,
1466 const char *page, size_t count)
1467{
1468 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1469 struct se_device *dev = t10_wwn->t10_dev;
1470 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1471 unsigned char buf[INQUIRY_MODEL_LEN + 2];
1472 char *stripped = NULL;
5c584fe6 1473 ssize_t len;
ee26724a 1474 ssize_t ret;
0322913c 1475
5c584fe6
AS
1476 len = strscpy(buf, page, sizeof(buf));
1477 if (len > 0) {
0322913c
AA
1478 /* Strip any newline added from userspace. */
1479 stripped = strstrip(buf);
1480 len = strlen(stripped);
1481 }
5c584fe6 1482 if (len < 0 || len > INQUIRY_MODEL_LEN) {
0322913c
AA
1483 pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
1484 __stringify(INQUIRY_MODEL_LEN)
1485 "\n");
1486 return -EOVERFLOW;
1487 }
1488
1489 ret = target_check_inquiry_data(stripped);
1490
1491 if (ret < 0)
1492 return ret;
1493
1494 /*
1495 * Check to see if any active exports exist. If they do exist, fail
1496 * here as changing this information on the fly (underneath the
1497 * initiator side OS dependent multipath code) could cause negative
1498 * effects.
1499 */
1500 if (dev->export_count) {
1501 pr_err("Unable to set T10 Model while active %d exports exist\n",
1502 dev->export_count);
1503 return -EINVAL;
1504 }
1505
1506 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
0871237a 1507 strscpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
0322913c
AA
1508
1509 pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
1510 dev->t10_wwn.model);
1511
1512 return count;
1513}
1514
1515static ssize_t target_wwn_revision_show(struct config_item *item,
1516 char *page)
1517{
1518 return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
1519}
1520
1521static ssize_t target_wwn_revision_store(struct config_item *item,
1522 const char *page, size_t count)
1523{
1524 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1525 struct se_device *dev = t10_wwn->t10_dev;
1526 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1527 unsigned char buf[INQUIRY_REVISION_LEN + 2];
1528 char *stripped = NULL;
5c584fe6 1529 ssize_t len;
ee26724a 1530 ssize_t ret;
0322913c 1531
5c584fe6
AS
1532 len = strscpy(buf, page, sizeof(buf));
1533 if (len > 0) {
0322913c
AA
1534 /* Strip any newline added from userspace. */
1535 stripped = strstrip(buf);
1536 len = strlen(stripped);
1537 }
5c584fe6 1538 if (len < 0 || len > INQUIRY_REVISION_LEN) {
0322913c
AA
1539 pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
1540 __stringify(INQUIRY_REVISION_LEN)
1541 "\n");
1542 return -EOVERFLOW;
1543 }
1544
1545 ret = target_check_inquiry_data(stripped);
1546
1547 if (ret < 0)
1548 return ret;
1549
1550 /*
1551 * Check to see if any active exports exist. If they do exist, fail
1552 * here as changing this information on the fly (underneath the
1553 * initiator side OS dependent multipath code) could cause negative
1554 * effects.
1555 */
1556 if (dev->export_count) {
1557 pr_err("Unable to set T10 Revision while active %d exports exist\n",
1558 dev->export_count);
1559 return -EINVAL;
1560 }
1561
1562 BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
0871237a 1563 strscpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
0322913c
AA
1564
1565 pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
1566 dev->t10_wwn.revision);
1567
1568 return count;
1569}
1570
2469f1e0
SS
1571static ssize_t
1572target_wwn_company_id_show(struct config_item *item,
1573 char *page)
1574{
1575 return snprintf(page, PAGE_SIZE, "%#08x\n",
1576 to_t10_wwn(item)->company_id);
1577}
1578
1579static ssize_t
1580target_wwn_company_id_store(struct config_item *item,
1581 const char *page, size_t count)
1582{
1583 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1584 struct se_device *dev = t10_wwn->t10_dev;
1585 u32 val;
1586 int ret;
1587
1588 /*
1589 * The IEEE COMPANY_ID field should contain a 24-bit canonical
1590 * form OUI assigned by the IEEE.
1591 */
1592 ret = kstrtou32(page, 0, &val);
1593 if (ret < 0)
1594 return ret;
1595
1596 if (val >= 0x1000000)
1597 return -EOVERFLOW;
1598
1599 /*
1600 * Check to see if any active exports exist. If they do exist, fail
1601 * here as changing this information on the fly (underneath the
1602 * initiator side OS dependent multipath code) could cause negative
1603 * effects.
1604 */
1605 if (dev->export_count) {
1606 pr_err("Unable to set Company ID while %u exports exist\n",
1607 dev->export_count);
1608 return -EINVAL;
1609 }
1610
1611 t10_wwn->company_id = val;
1612
1613 pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
1614 t10_wwn->company_id);
1615
1616 return count;
1617}
1618
c66ac9db
NB
1619/*
1620 * VPD page 0x80 Unit serial
1621 */
2eafd729
CH
1622static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1623 char *page)
c66ac9db 1624{
c66ac9db 1625 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
2eafd729 1626 &to_t10_wwn(item)->unit_serial[0]);
c66ac9db
NB
1627}
1628
2eafd729
CH
1629static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1630 const char *page, size_t count)
c66ac9db 1631{
2eafd729 1632 struct t10_wwn *t10_wwn = to_t10_wwn(item);
0fd97ccf 1633 struct se_device *dev = t10_wwn->t10_dev;
2d4e2daf 1634 unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
c66ac9db
NB
1635
1636 /*
1637 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1638 * from the struct scsi_device level firmware, do not allow
1639 * VPD Unit Serial to be emulated.
1640 *
1641 * Note this struct scsi_device could also be emulating VPD
1642 * information from its drivers/scsi LLD. But for now we assume
1643 * it is doing 'the right thing' wrt a world wide unique
1644 * VPD Unit Serial Number that OS dependent multipath can depend on.
1645 */
0fd97ccf 1646 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
6708bb27 1647 pr_err("Underlying SCSI device firmware provided VPD"
c66ac9db
NB
1648 " Unit Serial, ignoring request\n");
1649 return -EOPNOTSUPP;
1650 }
1651
60d645a4 1652 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
6708bb27 1653 pr_err("Emulated VPD Unit Serial exceeds"
c66ac9db
NB
1654 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1655 return -EOVERFLOW;
1656 }
1657 /*
1658 * Check to see if any active $FABRIC_MOD exports exist. If they
1659 * do exist, fail here as changing this information on the fly
1660 * (underneath the initiator side OS dependent multipath code)
1661 * could cause negative effects.
1662 */
0fd97ccf
CH
1663 if (dev->export_count) {
1664 pr_err("Unable to set VPD Unit Serial while"
1665 " active %d $FABRIC_MOD exports exist\n",
1666 dev->export_count);
1667 return -EINVAL;
c66ac9db 1668 }
0fd97ccf 1669
c66ac9db
NB
1670 /*
1671 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1672 *
1673 * Also, strip any newline added from the userspace
1674 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1675 */
c66ac9db 1676 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
0fd97ccf 1677 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
c66ac9db 1678 "%s", strstrip(buf));
0fd97ccf 1679 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
c66ac9db 1680
6708bb27 1681 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
0fd97ccf 1682 " %s\n", dev->t10_wwn.unit_serial);
c66ac9db
NB
1683
1684 return count;
1685}
1686
c66ac9db
NB
1687/*
1688 * VPD page 0x83 Protocol Identifier
1689 */
2eafd729
CH
1690static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1691 char *page)
c66ac9db 1692{
2eafd729 1693 struct t10_wwn *t10_wwn = to_t10_wwn(item);
c66ac9db 1694 struct t10_vpd *vpd;
2d4e2daf 1695 unsigned char buf[VPD_TMP_BUF_SIZE] = { };
c66ac9db
NB
1696 ssize_t len = 0;
1697
c66ac9db
NB
1698 spin_lock(&t10_wwn->t10_vpd_lock);
1699 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
6708bb27 1700 if (!vpd->protocol_identifier_set)
c66ac9db
NB
1701 continue;
1702
1703 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1704
6708bb27 1705 if (len + strlen(buf) >= PAGE_SIZE)
c66ac9db
NB
1706 break;
1707
1708 len += sprintf(page+len, "%s", buf);
1709 }
1710 spin_unlock(&t10_wwn->t10_vpd_lock);
1711
1712 return len;
1713}
1714
c66ac9db
NB
1715/*
1716 * Generic wrapper for dumping VPD identifiers by association.
1717 */
1718#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
2eafd729
CH
1719static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1720 char *page) \
c66ac9db 1721{ \
2eafd729
CH
1722 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1723 struct t10_vpd *vpd; \
c66ac9db
NB
1724 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1725 ssize_t len = 0; \
1726 \
c66ac9db
NB
1727 spin_lock(&t10_wwn->t10_vpd_lock); \
1728 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1729 if (vpd->association != _assoc) \
1730 continue; \
1731 \
1732 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1733 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1734 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1735 break; \
1736 len += sprintf(page+len, "%s", buf); \
1737 \
1738 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1739 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1740 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1741 break; \
1742 len += sprintf(page+len, "%s", buf); \
1743 \
1744 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1745 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1746 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1747 break; \
1748 len += sprintf(page+len, "%s", buf); \
1749 } \
1750 spin_unlock(&t10_wwn->t10_vpd_lock); \
1751 \
1752 return len; \
1753}
1754
2eafd729 1755/* VPD page 0x83 Association: Logical Unit */
c66ac9db 1756DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
2eafd729 1757/* VPD page 0x83 Association: Target Port */
c66ac9db 1758DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
2eafd729 1759/* VPD page 0x83 Association: SCSI Target Device */
c66ac9db
NB
1760DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1761
54a6f3f6 1762CONFIGFS_ATTR(target_wwn_, vendor_id);
0322913c
AA
1763CONFIGFS_ATTR(target_wwn_, product_id);
1764CONFIGFS_ATTR(target_wwn_, revision);
2469f1e0 1765CONFIGFS_ATTR(target_wwn_, company_id);
2eafd729
CH
1766CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1767CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1768CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1769CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1770CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
c66ac9db
NB
1771
1772static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
54a6f3f6 1773 &target_wwn_attr_vendor_id,
0322913c
AA
1774 &target_wwn_attr_product_id,
1775 &target_wwn_attr_revision,
2469f1e0 1776 &target_wwn_attr_company_id,
2eafd729
CH
1777 &target_wwn_attr_vpd_unit_serial,
1778 &target_wwn_attr_vpd_protocol_identifier,
1779 &target_wwn_attr_vpd_assoc_logical_unit,
1780 &target_wwn_attr_vpd_assoc_target_port,
1781 &target_wwn_attr_vpd_assoc_scsi_target_device,
c66ac9db
NB
1782 NULL,
1783};
1784
2eafd729 1785TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
c66ac9db 1786
f8d389c6 1787/* End functions for struct config_item_type tb_dev_wwn_cit */
c66ac9db 1788
91e2e39b 1789/* Start functions for struct config_item_type tb_dev_pr_cit */
c66ac9db 1790
2eafd729
CH
1791static struct se_device *pr_to_dev(struct config_item *item)
1792{
1793 return container_of(to_config_group(item), struct se_device,
1794 dev_pr_group);
1795}
c66ac9db 1796
d977f437
CH
1797static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1798 char *page)
c66ac9db
NB
1799{
1800 struct se_node_acl *se_nacl;
1801 struct t10_pr_registration *pr_reg;
2d4e2daf 1802 char i_buf[PR_REG_ISID_ID_LEN] = { };
c66ac9db 1803
c66ac9db 1804 pr_reg = dev->dev_pr_res_holder;
d977f437
CH
1805 if (!pr_reg)
1806 return sprintf(page, "No SPC-3 Reservation holder\n");
1807
c66ac9db 1808 se_nacl = pr_reg->pr_reg_nacl;
d2843c17 1809 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 1810
d977f437 1811 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
30c7ca93 1812 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
d2843c17 1813 se_nacl->initiatorname, i_buf);
c66ac9db
NB
1814}
1815
d977f437
CH
1816static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1817 char *page)
c66ac9db 1818{
fae43461 1819 struct se_session *sess = dev->reservation_holder;
c66ac9db 1820 struct se_node_acl *se_nacl;
d977f437 1821 ssize_t len;
c66ac9db 1822
fae43461
BVA
1823 if (sess) {
1824 se_nacl = sess->se_node_acl;
d977f437
CH
1825 len = sprintf(page,
1826 "SPC-2 Reservation: %s Initiator: %s\n",
30c7ca93 1827 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
d977f437
CH
1828 se_nacl->initiatorname);
1829 } else {
1830 len = sprintf(page, "No SPC-2 Reservation holder\n");
c66ac9db 1831 }
d977f437 1832 return len;
c66ac9db
NB
1833}
1834
2eafd729 1835static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
c66ac9db 1836{
2eafd729 1837 struct se_device *dev = pr_to_dev(item);
d977f437 1838 int ret;
c66ac9db 1839
b49d6f78
DD
1840 if (!dev->dev_attrib.emulate_pr)
1841 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1842
69088a04 1843 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
d977f437 1844 return sprintf(page, "Passthrough\n");
c66ac9db 1845
d977f437
CH
1846 spin_lock(&dev->dev_reservation_lock);
1847 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1848 ret = target_core_dev_pr_show_spc2_res(dev, page);
1849 else
1850 ret = target_core_dev_pr_show_spc3_res(dev, page);
1851 spin_unlock(&dev->dev_reservation_lock);
1852 return ret;
c66ac9db
NB
1853}
1854
2eafd729
CH
1855static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1856 char *page)
c66ac9db 1857{
2eafd729 1858 struct se_device *dev = pr_to_dev(item);
c66ac9db
NB
1859 ssize_t len = 0;
1860
c66ac9db 1861 spin_lock(&dev->dev_reservation_lock);
d977f437 1862 if (!dev->dev_pr_res_holder) {
c66ac9db 1863 len = sprintf(page, "No SPC-3 Reservation holder\n");
d977f437 1864 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
c66ac9db
NB
1865 len = sprintf(page, "SPC-3 Reservation: All Target"
1866 " Ports registration\n");
d977f437 1867 } else {
c66ac9db
NB
1868 len = sprintf(page, "SPC-3 Reservation: Single"
1869 " Target Port registration\n");
d977f437 1870 }
c66ac9db 1871
d977f437 1872 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1873 return len;
1874}
1875
2eafd729
CH
1876static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1877 char *page)
c66ac9db 1878{
2eafd729 1879 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
c66ac9db
NB
1880}
1881
c66ac9db 1882
2eafd729
CH
1883static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1884 char *page)
c66ac9db 1885{
2eafd729 1886 struct se_device *dev = pr_to_dev(item);
c66ac9db 1887 struct se_node_acl *se_nacl;
c66ac9db
NB
1888 struct se_portal_group *se_tpg;
1889 struct t10_pr_registration *pr_reg;
9ac8928e 1890 const struct target_core_fabric_ops *tfo;
c66ac9db
NB
1891 ssize_t len = 0;
1892
c66ac9db
NB
1893 spin_lock(&dev->dev_reservation_lock);
1894 pr_reg = dev->dev_pr_res_holder;
6708bb27 1895 if (!pr_reg) {
c66ac9db 1896 len = sprintf(page, "No SPC-3 Reservation holder\n");
d977f437 1897 goto out_unlock;
c66ac9db 1898 }
d977f437 1899
c66ac9db
NB
1900 se_nacl = pr_reg->pr_reg_nacl;
1901 se_tpg = se_nacl->se_tpg;
e3d6f909 1902 tfo = se_tpg->se_tpg_tfo;
c66ac9db
NB
1903
1904 len += sprintf(page+len, "SPC-3 Reservation: %s"
30c7ca93 1905 " Target Node Endpoint: %s\n", tfo->fabric_name,
c66ac9db
NB
1906 tfo->tpg_get_wwn(se_tpg));
1907 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
35d1efe8 1908 " Identifier Tag: %hu %s Portal Group Tag: %hu"
f2d30680 1909 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
30c7ca93
DD
1910 tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1911 tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
c66ac9db 1912
d977f437
CH
1913out_unlock:
1914 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1915 return len;
1916}
1917
c66ac9db 1918
2eafd729
CH
1919static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1920 char *page)
c66ac9db 1921{
2eafd729 1922 struct se_device *dev = pr_to_dev(item);
9ac8928e 1923 const struct target_core_fabric_ops *tfo;
c66ac9db
NB
1924 struct t10_pr_registration *pr_reg;
1925 unsigned char buf[384];
1926 char i_buf[PR_REG_ISID_ID_LEN];
1927 ssize_t len = 0;
d2843c17 1928 int reg_count = 0;
c66ac9db 1929
c66ac9db
NB
1930 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1931
0fd97ccf
CH
1932 spin_lock(&dev->t10_pr.registration_lock);
1933 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
c66ac9db
NB
1934 pr_reg_list) {
1935
1936 memset(buf, 0, 384);
1937 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1938 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
d2843c17 1939 core_pr_dump_initiator_port(pr_reg, i_buf,
c66ac9db
NB
1940 PR_REG_ISID_ID_LEN);
1941 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
30c7ca93 1942 tfo->fabric_name,
d2843c17 1943 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
c66ac9db
NB
1944 pr_reg->pr_res_generation);
1945
6708bb27 1946 if (len + strlen(buf) >= PAGE_SIZE)
c66ac9db
NB
1947 break;
1948
1949 len += sprintf(page+len, "%s", buf);
1950 reg_count++;
1951 }
0fd97ccf 1952 spin_unlock(&dev->t10_pr.registration_lock);
c66ac9db 1953
6708bb27 1954 if (!reg_count)
c66ac9db
NB
1955 len += sprintf(page+len, "None\n");
1956
1957 return len;
1958}
1959
2eafd729 1960static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
c66ac9db 1961{
2eafd729 1962 struct se_device *dev = pr_to_dev(item);
c66ac9db
NB
1963 struct t10_pr_registration *pr_reg;
1964 ssize_t len = 0;
1965
c66ac9db
NB
1966 spin_lock(&dev->dev_reservation_lock);
1967 pr_reg = dev->dev_pr_res_holder;
d977f437
CH
1968 if (pr_reg) {
1969 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1970 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1971 } else {
c66ac9db 1972 len = sprintf(page, "No SPC-3 Reservation holder\n");
c66ac9db 1973 }
c66ac9db 1974
d977f437 1975 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1976 return len;
1977}
1978
2eafd729 1979static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
c66ac9db 1980{
2eafd729
CH
1981 struct se_device *dev = pr_to_dev(item);
1982
b49d6f78
DD
1983 if (!dev->dev_attrib.emulate_pr)
1984 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
69088a04 1985 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
d977f437 1986 return sprintf(page, "SPC_PASSTHROUGH\n");
b49d6f78 1987 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
d977f437 1988 return sprintf(page, "SPC2_RESERVATIONS\n");
b49d6f78
DD
1989
1990 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
c66ac9db
NB
1991}
1992
2eafd729
CH
1993static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1994 char *page)
c66ac9db 1995{
2eafd729
CH
1996 struct se_device *dev = pr_to_dev(item);
1997
b49d6f78 1998 if (!dev->dev_attrib.emulate_pr ||
69088a04 1999 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
c66ac9db
NB
2000 return 0;
2001
2002 return sprintf(page, "APTPL Bit Status: %s\n",
0fd97ccf 2003 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
c66ac9db
NB
2004}
2005
2eafd729
CH
2006static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
2007 char *page)
c66ac9db 2008{
2eafd729
CH
2009 struct se_device *dev = pr_to_dev(item);
2010
b49d6f78 2011 if (!dev->dev_attrib.emulate_pr ||
69088a04 2012 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
c66ac9db
NB
2013 return 0;
2014
2015 return sprintf(page, "Ready to process PR APTPL metadata..\n");
2016}
2017
2018enum {
2019 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
2020 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
2021 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
2022 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
2023};
2024
2025static match_table_t tokens = {
2026 {Opt_initiator_fabric, "initiator_fabric=%s"},
2027 {Opt_initiator_node, "initiator_node=%s"},
2028 {Opt_initiator_sid, "initiator_sid=%s"},
2029 {Opt_sa_res_key, "sa_res_key=%s"},
2030 {Opt_res_holder, "res_holder=%d"},
2031 {Opt_res_type, "res_type=%d"},
2032 {Opt_res_scope, "res_scope=%d"},
2033 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
a2db857b 2034 {Opt_mapped_lun, "mapped_lun=%u"},
c66ac9db
NB
2035 {Opt_target_fabric, "target_fabric=%s"},
2036 {Opt_target_node, "target_node=%s"},
2037 {Opt_tpgt, "tpgt=%d"},
2038 {Opt_port_rtpi, "port_rtpi=%d"},
a2db857b 2039 {Opt_target_lun, "target_lun=%u"},
c66ac9db
NB
2040 {Opt_err, NULL}
2041};
2042
2eafd729
CH
2043static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
2044 const char *page, size_t count)
c66ac9db 2045{
2eafd729 2046 struct se_device *dev = pr_to_dev(item);
6d180253
JJ
2047 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
2048 unsigned char *t_fabric = NULL, *t_port = NULL;
8d213559 2049 char *orig, *ptr, *opts;
c66ac9db
NB
2050 substring_t args[MAX_OPT_ARGS];
2051 unsigned long long tmp_ll;
2052 u64 sa_res_key = 0;
f2d30680 2053 u64 mapped_lun = 0, target_lun = 0;
c66ac9db 2054 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
45fb94c2
BVA
2055 u16 tpgt = 0;
2056 u8 type = 0;
c66ac9db 2057
b49d6f78 2058 if (!dev->dev_attrib.emulate_pr ||
69088a04 2059 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
9105bfc0 2060 return count;
d977f437 2061 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
9105bfc0 2062 return count;
c66ac9db 2063
0fd97ccf 2064 if (dev->export_count) {
6708bb27 2065 pr_debug("Unable to process APTPL metadata while"
c66ac9db
NB
2066 " active fabric exports exist\n");
2067 return -EINVAL;
2068 }
2069
2070 opts = kstrdup(page, GFP_KERNEL);
2071 if (!opts)
2072 return -ENOMEM;
2073
2074 orig = opts;
90c161b6 2075 while ((ptr = strsep(&opts, ",\n")) != NULL) {
c66ac9db
NB
2076 if (!*ptr)
2077 continue;
2078
2079 token = match_token(ptr, tokens, args);
2080 switch (token) {
2081 case Opt_initiator_fabric:
8d213559 2082 i_fabric = match_strdup(args);
6d180253
JJ
2083 if (!i_fabric) {
2084 ret = -ENOMEM;
2085 goto out;
2086 }
c66ac9db
NB
2087 break;
2088 case Opt_initiator_node:
8d213559 2089 i_port = match_strdup(args);
6d180253
JJ
2090 if (!i_port) {
2091 ret = -ENOMEM;
2092 goto out;
2093 }
60d645a4 2094 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
6708bb27 2095 pr_err("APTPL metadata initiator_node="
c66ac9db
NB
2096 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2097 PR_APTPL_MAX_IPORT_LEN);
2098 ret = -EINVAL;
2099 break;
2100 }
2101 break;
2102 case Opt_initiator_sid:
8d213559 2103 isid = match_strdup(args);
6d180253
JJ
2104 if (!isid) {
2105 ret = -ENOMEM;
2106 goto out;
2107 }
60d645a4 2108 if (strlen(isid) >= PR_REG_ISID_LEN) {
6708bb27 2109 pr_err("APTPL metadata initiator_isid"
c66ac9db
NB
2110 "= exceeds PR_REG_ISID_LEN: %d\n",
2111 PR_REG_ISID_LEN);
2112 ret = -EINVAL;
2113 break;
2114 }
2115 break;
2116 case Opt_sa_res_key:
a2db857b 2117 ret = match_u64(args, &tmp_ll);
c66ac9db 2118 if (ret < 0) {
8d213559 2119 pr_err("kstrtoull() failed for sa_res_key=\n");
c66ac9db
NB
2120 goto out;
2121 }
2122 sa_res_key = (u64)tmp_ll;
2123 break;
2124 /*
2125 * PR APTPL Metadata for Reservation
2126 */
2127 case Opt_res_holder:
c2091026
DD
2128 ret = match_int(args, &arg);
2129 if (ret)
2130 goto out;
c66ac9db
NB
2131 res_holder = arg;
2132 break;
2133 case Opt_res_type:
c2091026
DD
2134 ret = match_int(args, &arg);
2135 if (ret)
2136 goto out;
c66ac9db
NB
2137 type = (u8)arg;
2138 break;
2139 case Opt_res_scope:
c2091026
DD
2140 ret = match_int(args, &arg);
2141 if (ret)
2142 goto out;
c66ac9db
NB
2143 break;
2144 case Opt_res_all_tg_pt:
c2091026
DD
2145 ret = match_int(args, &arg);
2146 if (ret)
2147 goto out;
c66ac9db
NB
2148 all_tg_pt = (int)arg;
2149 break;
2150 case Opt_mapped_lun:
a2db857b 2151 ret = match_u64(args, &tmp_ll);
c2091026
DD
2152 if (ret)
2153 goto out;
a2db857b 2154 mapped_lun = (u64)tmp_ll;
c66ac9db
NB
2155 break;
2156 /*
2157 * PR APTPL Metadata for Target Port
2158 */
2159 case Opt_target_fabric:
8d213559 2160 t_fabric = match_strdup(args);
6d180253
JJ
2161 if (!t_fabric) {
2162 ret = -ENOMEM;
2163 goto out;
2164 }
c66ac9db
NB
2165 break;
2166 case Opt_target_node:
8d213559 2167 t_port = match_strdup(args);
6d180253
JJ
2168 if (!t_port) {
2169 ret = -ENOMEM;
2170 goto out;
2171 }
60d645a4 2172 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
6708bb27 2173 pr_err("APTPL metadata target_node="
c66ac9db
NB
2174 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2175 PR_APTPL_MAX_TPORT_LEN);
2176 ret = -EINVAL;
2177 break;
2178 }
2179 break;
2180 case Opt_tpgt:
c2091026
DD
2181 ret = match_int(args, &arg);
2182 if (ret)
2183 goto out;
c66ac9db
NB
2184 tpgt = (u16)arg;
2185 break;
2186 case Opt_port_rtpi:
c2091026
DD
2187 ret = match_int(args, &arg);
2188 if (ret)
2189 goto out;
c66ac9db
NB
2190 break;
2191 case Opt_target_lun:
a2db857b 2192 ret = match_u64(args, &tmp_ll);
c2091026
DD
2193 if (ret)
2194 goto out;
a2db857b 2195 target_lun = (u64)tmp_ll;
c66ac9db
NB
2196 break;
2197 default:
2198 break;
2199 }
2200 }
2201
6708bb27
AG
2202 if (!i_port || !t_port || !sa_res_key) {
2203 pr_err("Illegal parameters for APTPL registration\n");
c66ac9db
NB
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
2208 if (res_holder && !(type)) {
6708bb27 2209 pr_err("Illegal PR type: 0x%02x for reservation"
c66ac9db
NB
2210 " holder\n", type);
2211 ret = -EINVAL;
2212 goto out;
2213 }
2214
0fd97ccf 2215 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
c66ac9db
NB
2216 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2217 res_holder, all_tg_pt, type);
2218out:
6d180253
JJ
2219 kfree(i_fabric);
2220 kfree(i_port);
2221 kfree(isid);
2222 kfree(t_fabric);
2223 kfree(t_port);
c66ac9db
NB
2224 kfree(orig);
2225 return (ret == 0) ? count : ret;
2226}
2227
c66ac9db 2228
2eafd729
CH
2229CONFIGFS_ATTR_RO(target_pr_, res_holder);
2230CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
2231CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
2232CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
2233CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
2234CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
2235CONFIGFS_ATTR_RO(target_pr_, res_type);
2236CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
2237CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
c66ac9db
NB
2238
2239static struct configfs_attribute *target_core_dev_pr_attrs[] = {
2eafd729
CH
2240 &target_pr_attr_res_holder,
2241 &target_pr_attr_res_pr_all_tgt_pts,
2242 &target_pr_attr_res_pr_generation,
2243 &target_pr_attr_res_pr_holder_tg_port,
2244 &target_pr_attr_res_pr_registered_i_pts,
2245 &target_pr_attr_res_pr_type,
2246 &target_pr_attr_res_type,
2247 &target_pr_attr_res_aptpl_active,
2248 &target_pr_attr_res_aptpl_metadata,
c66ac9db
NB
2249 NULL,
2250};
2251
2eafd729 2252TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
c66ac9db 2253
91e2e39b 2254/* End functions for struct config_item_type tb_dev_pr_cit */
c66ac9db 2255
73112edc 2256/* Start functions for struct config_item_type tb_dev_cit */
c66ac9db 2257
2eafd729
CH
2258static inline struct se_device *to_device(struct config_item *item)
2259{
2260 return container_of(to_config_group(item), struct se_device, dev_group);
2261}
2262
2263static ssize_t target_dev_info_show(struct config_item *item, char *page)
c66ac9db 2264{
2eafd729 2265 struct se_device *dev = to_device(item);
c66ac9db
NB
2266 int bl = 0;
2267 ssize_t read_bytes = 0;
2268
0fd97ccf 2269 transport_dump_dev_state(dev, page, &bl);
c66ac9db 2270 read_bytes += bl;
0a06d430
CH
2271 read_bytes += dev->transport->show_configfs_dev_params(dev,
2272 page+read_bytes);
c66ac9db
NB
2273 return read_bytes;
2274}
2275
2eafd729
CH
2276static ssize_t target_dev_control_store(struct config_item *item,
2277 const char *page, size_t count)
c66ac9db 2278{
2eafd729 2279 struct se_device *dev = to_device(item);
c66ac9db 2280
0a06d430 2281 return dev->transport->set_configfs_dev_params(dev, page, count);
c66ac9db
NB
2282}
2283
2eafd729 2284static ssize_t target_dev_alias_show(struct config_item *item, char *page)
c66ac9db 2285{
2eafd729 2286 struct se_device *dev = to_device(item);
c66ac9db 2287
0fd97ccf 2288 if (!(dev->dev_flags & DF_USING_ALIAS))
c66ac9db
NB
2289 return 0;
2290
0fd97ccf 2291 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
c66ac9db
NB
2292}
2293
2eafd729
CH
2294static ssize_t target_dev_alias_store(struct config_item *item,
2295 const char *page, size_t count)
c66ac9db 2296{
2eafd729 2297 struct se_device *dev = to_device(item);
0fd97ccf 2298 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2299 ssize_t read_bytes;
2300
2301 if (count > (SE_DEV_ALIAS_LEN-1)) {
6708bb27 2302 pr_err("alias count: %d exceeds"
c66ac9db
NB
2303 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2304 SE_DEV_ALIAS_LEN-1);
2305 return -EINVAL;
2306 }
2307
0fd97ccf 2308 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
3011684c
DC
2309 if (!read_bytes)
2310 return -EINVAL;
0fd97ccf
CH
2311 if (dev->dev_alias[read_bytes - 1] == '\n')
2312 dev->dev_alias[read_bytes - 1] = '\0';
0877eafd 2313
0fd97ccf 2314 dev->dev_flags |= DF_USING_ALIAS;
3011684c 2315
6708bb27 2316 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
c66ac9db 2317 config_item_name(&hba->hba_group.cg_item),
0fd97ccf
CH
2318 config_item_name(&dev->dev_group.cg_item),
2319 dev->dev_alias);
c66ac9db
NB
2320
2321 return read_bytes;
2322}
2323
2eafd729 2324static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
c66ac9db 2325{
2eafd729 2326 struct se_device *dev = to_device(item);
c66ac9db 2327
0fd97ccf 2328 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
c66ac9db
NB
2329 return 0;
2330
0fd97ccf 2331 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
c66ac9db
NB
2332}
2333
2eafd729
CH
2334static ssize_t target_dev_udev_path_store(struct config_item *item,
2335 const char *page, size_t count)
c66ac9db 2336{
2eafd729 2337 struct se_device *dev = to_device(item);
0fd97ccf 2338 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2339 ssize_t read_bytes;
2340
2341 if (count > (SE_UDEV_PATH_LEN-1)) {
6708bb27 2342 pr_err("udev_path count: %d exceeds"
c66ac9db
NB
2343 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2344 SE_UDEV_PATH_LEN-1);
2345 return -EINVAL;
2346 }
2347
0fd97ccf 2348 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
c66ac9db 2349 "%s", page);
3011684c
DC
2350 if (!read_bytes)
2351 return -EINVAL;
0fd97ccf
CH
2352 if (dev->udev_path[read_bytes - 1] == '\n')
2353 dev->udev_path[read_bytes - 1] = '\0';
0877eafd 2354
0fd97ccf 2355 dev->dev_flags |= DF_USING_UDEV_PATH;
3011684c 2356
6708bb27 2357 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
c66ac9db 2358 config_item_name(&hba->hba_group.cg_item),
0fd97ccf
CH
2359 config_item_name(&dev->dev_group.cg_item),
2360 dev->udev_path);
c66ac9db
NB
2361
2362 return read_bytes;
2363}
2364
2eafd729 2365static ssize_t target_dev_enable_show(struct config_item *item, char *page)
64146db7 2366{
2eafd729 2367 struct se_device *dev = to_device(item);
64146db7 2368
cb0f32e1 2369 return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
64146db7
AG
2370}
2371
2eafd729
CH
2372static ssize_t target_dev_enable_store(struct config_item *item,
2373 const char *page, size_t count)
c66ac9db 2374{
2eafd729 2375 struct se_device *dev = to_device(item);
c66ac9db 2376 char *ptr;
0fd97ccf 2377 int ret;
c66ac9db
NB
2378
2379 ptr = strstr(page, "1");
6708bb27
AG
2380 if (!ptr) {
2381 pr_err("For dev_enable ops, only valid value"
c66ac9db
NB
2382 " is \"1\"\n");
2383 return -EINVAL;
2384 }
c66ac9db 2385
0fd97ccf
CH
2386 ret = target_configure_device(dev);
2387 if (ret)
2388 return ret;
c66ac9db
NB
2389 return count;
2390}
2391
2eafd729 2392static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
c66ac9db 2393{
2eafd729 2394 struct se_device *dev = to_device(item);
c66ac9db
NB
2395 struct config_item *lu_ci;
2396 struct t10_alua_lu_gp *lu_gp;
2397 struct t10_alua_lu_gp_member *lu_gp_mem;
2398 ssize_t len = 0;
2399
c66ac9db 2400 lu_gp_mem = dev->dev_alua_lu_gp_mem;
c87fbd56
CH
2401 if (!lu_gp_mem)
2402 return 0;
c66ac9db
NB
2403
2404 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2405 lu_gp = lu_gp_mem->lu_gp;
6708bb27 2406 if (lu_gp) {
c66ac9db
NB
2407 lu_ci = &lu_gp->lu_gp_group.cg_item;
2408 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2409 config_item_name(lu_ci), lu_gp->lu_gp_id);
2410 }
2411 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2412
2413 return len;
2414}
2415
2eafd729
CH
2416static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2417 const char *page, size_t count)
c66ac9db 2418{
2eafd729 2419 struct se_device *dev = to_device(item);
0fd97ccf 2420 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2421 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2422 struct t10_alua_lu_gp_member *lu_gp_mem;
2d4e2daf 2423 unsigned char buf[LU_GROUP_NAME_BUF] = { };
c66ac9db
NB
2424 int move = 0;
2425
c87fbd56
CH
2426 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2427 if (!lu_gp_mem)
9105bfc0 2428 return count;
c87fbd56 2429
c66ac9db 2430 if (count > LU_GROUP_NAME_BUF) {
6708bb27 2431 pr_err("ALUA LU Group Alias too large!\n");
c66ac9db
NB
2432 return -EINVAL;
2433 }
c66ac9db
NB
2434 memcpy(buf, page, count);
2435 /*
2436 * Any ALUA logical unit alias besides "NULL" means we will be
2437 * making a new group association.
2438 */
2439 if (strcmp(strstrip(buf), "NULL")) {
2440 /*
2441 * core_alua_get_lu_gp_by_name() will increment reference to
2442 * struct t10_alua_lu_gp. This reference is released with
2443 * core_alua_get_lu_gp_by_name below().
2444 */
2445 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
6708bb27 2446 if (!lu_gp_new)
c66ac9db
NB
2447 return -ENODEV;
2448 }
c66ac9db
NB
2449
2450 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2451 lu_gp = lu_gp_mem->lu_gp;
6708bb27 2452 if (lu_gp) {
c66ac9db
NB
2453 /*
2454 * Clearing an existing lu_gp association, and replacing
2455 * with NULL
2456 */
6708bb27
AG
2457 if (!lu_gp_new) {
2458 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
c66ac9db
NB
2459 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2460 " %hu\n",
2461 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2462 config_item_name(&dev->dev_group.cg_item),
c66ac9db
NB
2463 config_item_name(&lu_gp->lu_gp_group.cg_item),
2464 lu_gp->lu_gp_id);
2465
2466 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2467 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2468
2469 return count;
2470 }
2471 /*
2472 * Removing existing association of lu_gp_mem with lu_gp
2473 */
2474 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2475 move = 1;
2476 }
2477 /*
2478 * Associate lu_gp_mem with lu_gp_new.
2479 */
2480 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2481 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2482
6708bb27 2483 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
c66ac9db
NB
2484 " core/alua/lu_gps/%s, ID: %hu\n",
2485 (move) ? "Moving" : "Adding",
2486 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2487 config_item_name(&dev->dev_group.cg_item),
c66ac9db
NB
2488 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2489 lu_gp_new->lu_gp_id);
2490
2491 core_alua_put_lu_gp_from_name(lu_gp_new);
2492 return count;
2493}
2494
2eafd729 2495static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
229d4f11 2496{
2eafd729 2497 struct se_device *dev = to_device(item);
229d4f11
HR
2498 struct t10_alua_lba_map *map;
2499 struct t10_alua_lba_map_member *mem;
2500 char *b = page;
2501 int bl = 0;
2502 char state;
2503
2504 spin_lock(&dev->t10_alua.lba_map_lock);
2505 if (!list_empty(&dev->t10_alua.lba_map_list))
2506 bl += sprintf(b + bl, "%u %u\n",
2507 dev->t10_alua.lba_map_segment_size,
2508 dev->t10_alua.lba_map_segment_multiplier);
2509 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2510 bl += sprintf(b + bl, "%llu %llu",
2511 map->lba_map_first_lba, map->lba_map_last_lba);
2512 list_for_each_entry(mem, &map->lba_map_mem_list,
2513 lba_map_mem_list) {
2514 switch (mem->lba_map_mem_alua_state) {
2515 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2516 state = 'O';
2517 break;
2518 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2519 state = 'A';
2520 break;
2521 case ALUA_ACCESS_STATE_STANDBY:
2522 state = 'S';
2523 break;
2524 case ALUA_ACCESS_STATE_UNAVAILABLE:
2525 state = 'U';
2526 break;
2527 default:
2528 state = '.';
2529 break;
2530 }
2531 bl += sprintf(b + bl, " %d:%c",
2532 mem->lba_map_mem_alua_pg_id, state);
2533 }
2534 bl += sprintf(b + bl, "\n");
2535 }
2536 spin_unlock(&dev->t10_alua.lba_map_lock);
2537 return bl;
2538}
2539
2eafd729
CH
2540static ssize_t target_dev_lba_map_store(struct config_item *item,
2541 const char *page, size_t count)
229d4f11 2542{
2eafd729 2543 struct se_device *dev = to_device(item);
229d4f11
HR
2544 struct t10_alua_lba_map *lba_map = NULL;
2545 struct list_head lba_list;
f0a8afec 2546 char *map_entries, *orig, *ptr;
229d4f11
HR
2547 char state;
2548 int pg_num = -1, pg;
2549 int ret = 0, num = 0, pg_id, alua_state;
2550 unsigned long start_lba = -1, end_lba = -1;
2551 unsigned long segment_size = -1, segment_mult = -1;
2552
f0a8afec 2553 orig = map_entries = kstrdup(page, GFP_KERNEL);
229d4f11
HR
2554 if (!map_entries)
2555 return -ENOMEM;
2556
2557 INIT_LIST_HEAD(&lba_list);
2558 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2559 if (!*ptr)
2560 continue;
2561
2562 if (num == 0) {
2563 if (sscanf(ptr, "%lu %lu\n",
2564 &segment_size, &segment_mult) != 2) {
2565 pr_err("Invalid line %d\n", num);
2566 ret = -EINVAL;
2567 break;
2568 }
2569 num++;
2570 continue;
2571 }
2572 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2573 pr_err("Invalid line %d\n", num);
2574 ret = -EINVAL;
2575 break;
2576 }
2577 ptr = strchr(ptr, ' ');
2578 if (!ptr) {
2579 pr_err("Invalid line %d, missing end lba\n", num);
2580 ret = -EINVAL;
2581 break;
2582 }
2583 ptr++;
2584 ptr = strchr(ptr, ' ');
2585 if (!ptr) {
2586 pr_err("Invalid line %d, missing state definitions\n",
2587 num);
2588 ret = -EINVAL;
2589 break;
2590 }
2591 ptr++;
2592 lba_map = core_alua_allocate_lba_map(&lba_list,
2593 start_lba, end_lba);
2594 if (IS_ERR(lba_map)) {
2595 ret = PTR_ERR(lba_map);
2596 break;
2597 }
2598 pg = 0;
2599 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2600 switch (state) {
2601 case 'O':
2602 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2603 break;
2604 case 'A':
2605 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2606 break;
2607 case 'S':
2608 alua_state = ALUA_ACCESS_STATE_STANDBY;
2609 break;
2610 case 'U':
2611 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2612 break;
2613 default:
2614 pr_err("Invalid ALUA state '%c'\n", state);
2615 ret = -EINVAL;
2616 goto out;
2617 }
2618
2619 ret = core_alua_allocate_lba_map_mem(lba_map,
2620 pg_id, alua_state);
2621 if (ret) {
2622 pr_err("Invalid target descriptor %d:%c "
2623 "at line %d\n",
2624 pg_id, state, num);
2625 break;
2626 }
2627 pg++;
2628 ptr = strchr(ptr, ' ');
2629 if (ptr)
2630 ptr++;
2631 else
2632 break;
2633 }
2634 if (pg_num == -1)
2635 pg_num = pg;
2636 else if (pg != pg_num) {
2637 pr_err("Only %d from %d port groups definitions "
2638 "at line %d\n", pg, pg_num, num);
2639 ret = -EINVAL;
2640 break;
2641 }
2642 num++;
2643 }
2644out:
2645 if (ret) {
2646 core_alua_free_lba_map(&lba_list);
2647 count = ret;
2648 } else
2649 core_alua_set_lba_map(dev, &lba_list,
2650 segment_size, segment_mult);
f0a8afec 2651 kfree(orig);
229d4f11
HR
2652 return count;
2653}
2654
2eafd729
CH
2655CONFIGFS_ATTR_RO(target_dev_, info);
2656CONFIGFS_ATTR_WO(target_dev_, control);
2657CONFIGFS_ATTR(target_dev_, alias);
2658CONFIGFS_ATTR(target_dev_, udev_path);
2659CONFIGFS_ATTR(target_dev_, enable);
2660CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2661CONFIGFS_ATTR(target_dev_, lba_map);
229d4f11 2662
73112edc 2663static struct configfs_attribute *target_core_dev_attrs[] = {
2eafd729
CH
2664 &target_dev_attr_info,
2665 &target_dev_attr_control,
2666 &target_dev_attr_alias,
2667 &target_dev_attr_udev_path,
2668 &target_dev_attr_enable,
2669 &target_dev_attr_alua_lu_gp,
2670 &target_dev_attr_lba_map,
c66ac9db
NB
2671 NULL,
2672};
2673
2674static void target_core_dev_release(struct config_item *item)
2675{
0fd97ccf
CH
2676 struct config_group *dev_cg = to_config_group(item);
2677 struct se_device *dev =
2678 container_of(dev_cg, struct se_device, dev_group);
c66ac9db 2679
0fd97ccf 2680 target_free_device(dev);
c66ac9db
NB
2681}
2682
c17cd249
NB
2683/*
2684 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2685 * within target_fabric_port_link()
2686 */
2687struct configfs_item_operations target_core_dev_item_ops = {
c66ac9db 2688 .release = target_core_dev_release,
c66ac9db
NB
2689};
2690
73112edc 2691TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
c66ac9db 2692
73112edc 2693/* End functions for struct config_item_type tb_dev_cit */
c66ac9db
NB
2694
2695/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2696
2eafd729
CH
2697static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2698{
2699 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2700 lu_gp_group);
2701}
c66ac9db 2702
2eafd729 2703static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
c66ac9db 2704{
2eafd729
CH
2705 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2706
6708bb27 2707 if (!lu_gp->lu_gp_valid_id)
c66ac9db 2708 return 0;
c66ac9db
NB
2709 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2710}
2711
2eafd729
CH
2712static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2713 const char *page, size_t count)
c66ac9db 2714{
2eafd729 2715 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
c66ac9db
NB
2716 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2717 unsigned long lu_gp_id;
2718 int ret;
2719
57103d7f 2720 ret = kstrtoul(page, 0, &lu_gp_id);
c66ac9db 2721 if (ret < 0) {
57103d7f 2722 pr_err("kstrtoul() returned %d for"
c66ac9db 2723 " lu_gp_id\n", ret);
57103d7f 2724 return ret;
c66ac9db
NB
2725 }
2726 if (lu_gp_id > 0x0000ffff) {
6708bb27 2727 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
c66ac9db
NB
2728 " 0x0000ffff\n", lu_gp_id);
2729 return -EINVAL;
2730 }
2731
2732 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2733 if (ret < 0)
2734 return -EINVAL;
2735
6708bb27 2736 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
c66ac9db
NB
2737 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2738 config_item_name(&alua_lu_gp_cg->cg_item),
2739 lu_gp->lu_gp_id);
2740
2741 return count;
2742}
2743
2eafd729 2744static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
c66ac9db 2745{
2eafd729 2746 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
c66ac9db
NB
2747 struct se_device *dev;
2748 struct se_hba *hba;
c66ac9db
NB
2749 struct t10_alua_lu_gp_member *lu_gp_mem;
2750 ssize_t len = 0, cur_len;
2d4e2daf 2751 unsigned char buf[LU_GROUP_NAME_BUF] = { };
c66ac9db
NB
2752
2753 spin_lock(&lu_gp->lu_gp_lock);
2754 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2755 dev = lu_gp_mem->lu_gp_mem_dev;
0fd97ccf 2756 hba = dev->se_hba;
c66ac9db
NB
2757
2758 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2759 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2760 config_item_name(&dev->dev_group.cg_item));
c66ac9db
NB
2761 cur_len++; /* Extra byte for NULL terminator */
2762
2763 if ((cur_len + len) > PAGE_SIZE) {
6708bb27 2764 pr_warn("Ran out of lu_gp_show_attr"
c66ac9db
NB
2765 "_members buffer\n");
2766 break;
2767 }
2768 memcpy(page+len, buf, cur_len);
2769 len += cur_len;
2770 }
2771 spin_unlock(&lu_gp->lu_gp_lock);
2772
2773 return len;
2774}
2775
2eafd729
CH
2776CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2777CONFIGFS_ATTR_RO(target_lu_gp_, members);
c66ac9db
NB
2778
2779static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2eafd729
CH
2780 &target_lu_gp_attr_lu_gp_id,
2781 &target_lu_gp_attr_members,
c66ac9db
NB
2782 NULL,
2783};
2784
1f6fe7cb
NB
2785static void target_core_alua_lu_gp_release(struct config_item *item)
2786{
2787 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2788 struct t10_alua_lu_gp, lu_gp_group);
2789
2790 core_alua_free_lu_gp(lu_gp);
2791}
2792
c66ac9db 2793static struct configfs_item_operations target_core_alua_lu_gp_ops = {
1f6fe7cb 2794 .release = target_core_alua_lu_gp_release,
c66ac9db
NB
2795};
2796
ece550b5 2797static const struct config_item_type target_core_alua_lu_gp_cit = {
c66ac9db
NB
2798 .ct_item_ops = &target_core_alua_lu_gp_ops,
2799 .ct_attrs = target_core_alua_lu_gp_attrs,
2800 .ct_owner = THIS_MODULE,
2801};
2802
2803/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2804
2805/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2806
2807static struct config_group *target_core_alua_create_lu_gp(
2808 struct config_group *group,
2809 const char *name)
2810{
2811 struct t10_alua_lu_gp *lu_gp;
2812 struct config_group *alua_lu_gp_cg = NULL;
2813 struct config_item *alua_lu_gp_ci = NULL;
2814
2815 lu_gp = core_alua_allocate_lu_gp(name, 0);
2816 if (IS_ERR(lu_gp))
2817 return NULL;
2818
2819 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2820 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2821
2822 config_group_init_type_name(alua_lu_gp_cg, name,
2823 &target_core_alua_lu_gp_cit);
2824
6708bb27 2825 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
c66ac9db
NB
2826 " Group: core/alua/lu_gps/%s\n",
2827 config_item_name(alua_lu_gp_ci));
2828
2829 return alua_lu_gp_cg;
2830
2831}
2832
2833static void target_core_alua_drop_lu_gp(
2834 struct config_group *group,
2835 struct config_item *item)
2836{
2837 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2838 struct t10_alua_lu_gp, lu_gp_group);
2839
6708bb27 2840 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
c66ac9db
NB
2841 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2842 config_item_name(item), lu_gp->lu_gp_id);
1f6fe7cb
NB
2843 /*
2844 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2845 * -> target_core_alua_lu_gp_release()
2846 */
c66ac9db 2847 config_item_put(item);
c66ac9db
NB
2848}
2849
2850static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2851 .make_group = &target_core_alua_create_lu_gp,
2852 .drop_item = &target_core_alua_drop_lu_gp,
2853};
2854
ece550b5 2855static const struct config_item_type target_core_alua_lu_gps_cit = {
c66ac9db
NB
2856 .ct_item_ops = NULL,
2857 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2858 .ct_owner = THIS_MODULE,
2859};
2860
2861/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2862
2863/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2864
2eafd729
CH
2865static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2866{
2867 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2868 tg_pt_gp_group);
2869}
c66ac9db 2870
2eafd729
CH
2871static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2872 char *page)
c66ac9db
NB
2873{
2874 return sprintf(page, "%d\n",
d19c4643 2875 to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
c66ac9db
NB
2876}
2877
2eafd729
CH
2878static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2879 const char *page, size_t count)
c66ac9db 2880{
2eafd729 2881 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
0fd97ccf 2882 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
c66ac9db
NB
2883 unsigned long tmp;
2884 int new_state, ret;
2885
6708bb27 2886 if (!tg_pt_gp->tg_pt_gp_valid_id) {
baa75afd 2887 pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
c66ac9db
NB
2888 return -EINVAL;
2889 }
cb0f32e1 2890 if (!target_dev_configured(dev)) {
f1453773
NB
2891 pr_err("Unable to set alua_access_state while device is"
2892 " not configured\n");
2893 return -ENODEV;
2894 }
c66ac9db 2895
57103d7f 2896 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2897 if (ret < 0) {
6708bb27 2898 pr_err("Unable to extract new ALUA access state from"
c66ac9db 2899 " %s\n", page);
57103d7f 2900 return ret;
c66ac9db
NB
2901 }
2902 new_state = (int)tmp;
2903
125d0119
HR
2904 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2905 pr_err("Unable to process implicit configfs ALUA"
2906 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
c66ac9db
NB
2907 return -EINVAL;
2908 }
c66094bf
HR
2909 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2910 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2911 /* LBA DEPENDENT is only allowed with implicit ALUA */
2912 pr_err("Unable to process implicit configfs ALUA transition"
2913 " while explicit ALUA management is enabled\n");
2914 return -EINVAL;
2915 }
c66ac9db 2916
0fd97ccf 2917 ret = core_alua_do_port_transition(tg_pt_gp, dev,
c66ac9db
NB
2918 NULL, NULL, new_state, 0);
2919 return (!ret) ? count : -EINVAL;
2920}
2921
2eafd729
CH
2922static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2923 char *page)
c66ac9db 2924{
2eafd729 2925 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2926 return sprintf(page, "%s\n",
2927 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2928}
2929
2eafd729
CH
2930static ssize_t target_tg_pt_gp_alua_access_status_store(
2931 struct config_item *item, const char *page, size_t count)
c66ac9db 2932{
2eafd729 2933 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2934 unsigned long tmp;
2935 int new_status, ret;
2936
6708bb27 2937 if (!tg_pt_gp->tg_pt_gp_valid_id) {
baa75afd 2938 pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
c66ac9db
NB
2939 return -EINVAL;
2940 }
2941
57103d7f 2942 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2943 if (ret < 0) {
6708bb27 2944 pr_err("Unable to extract new ALUA access status"
c66ac9db 2945 " from %s\n", page);
57103d7f 2946 return ret;
c66ac9db
NB
2947 }
2948 new_status = (int)tmp;
2949
2950 if ((new_status != ALUA_STATUS_NONE) &&
125d0119
HR
2951 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2952 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
6708bb27 2953 pr_err("Illegal ALUA access status: 0x%02x\n",
c66ac9db
NB
2954 new_status);
2955 return -EINVAL;
2956 }
2957
2958 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2959 return count;
2960}
2961
2eafd729
CH
2962static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2963 char *page)
c66ac9db 2964{
2eafd729 2965 return core_alua_show_access_type(to_tg_pt_gp(item), page);
c66ac9db
NB
2966}
2967
2eafd729
CH
2968static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2969 const char *page, size_t count)
c66ac9db 2970{
2eafd729 2971 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
c66ac9db
NB
2972}
2973
2eafd729
CH
2974#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2975static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2976 struct config_item *item, char *p) \
b0a382c5 2977{ \
2eafd729
CH
2978 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2979 return sprintf(p, "%d\n", \
2980 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2981} \
2982 \
2983static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2984 struct config_item *item, const char *p, size_t c) \
b0a382c5 2985{ \
2eafd729 2986 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
b0a382c5
HR
2987 unsigned long tmp; \
2988 int ret; \
2989 \
2990 if (!t->tg_pt_gp_valid_id) { \
baa75afd 2991 pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
b0a382c5
HR
2992 return -EINVAL; \
2993 } \
2994 \
2995 ret = kstrtoul(p, 0, &tmp); \
2996 if (ret < 0) { \
2997 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2998 return -EINVAL; \
2999 } \
3000 if (tmp > 1) { \
3001 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
3002 return -EINVAL; \
3003 } \
1f0b030c 3004 if (tmp) \
2eafd729 3005 t->tg_pt_gp_alua_supported_states |= _bit; \
b0a382c5 3006 else \
2eafd729 3007 t->tg_pt_gp_alua_supported_states &= ~_bit; \
b0a382c5
HR
3008 \
3009 return c; \
3010}
3011
2eafd729
CH
3012ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
3013ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
3014ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
3015ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
3016ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
3017ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
3018ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
6be526c4 3019
2eafd729
CH
3020static ssize_t target_tg_pt_gp_alua_write_metadata_show(
3021 struct config_item *item, char *page)
c66ac9db 3022{
2eafd729
CH
3023 return sprintf(page, "%d\n",
3024 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
c66ac9db
NB
3025}
3026
2eafd729
CH
3027static ssize_t target_tg_pt_gp_alua_write_metadata_store(
3028 struct config_item *item, const char *page, size_t count)
c66ac9db 3029{
2eafd729 3030 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
3031 unsigned long tmp;
3032 int ret;
3033
57103d7f 3034 ret = kstrtoul(page, 0, &tmp);
c66ac9db 3035 if (ret < 0) {
6708bb27 3036 pr_err("Unable to extract alua_write_metadata\n");
57103d7f 3037 return ret;
c66ac9db
NB
3038 }
3039
3040 if ((tmp != 0) && (tmp != 1)) {
6708bb27 3041 pr_err("Illegal value for alua_write_metadata:"
c66ac9db
NB
3042 " %lu\n", tmp);
3043 return -EINVAL;
3044 }
3045 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
3046
3047 return count;
3048}
3049
2eafd729
CH
3050static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
3051 char *page)
c66ac9db 3052{
2eafd729 3053 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
c66ac9db
NB
3054}
3055
2eafd729
CH
3056static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
3057 const char *page, size_t count)
c66ac9db 3058{
2eafd729
CH
3059 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
3060 count);
c66ac9db
NB
3061}
3062
2eafd729
CH
3063static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
3064 char *page)
c66ac9db 3065{
2eafd729 3066 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
c66ac9db
NB
3067}
3068
2eafd729
CH
3069static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
3070 const char *page, size_t count)
c66ac9db 3071{
2eafd729
CH
3072 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
3073 count);
c66ac9db
NB
3074}
3075
2eafd729
CH
3076static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
3077 struct config_item *item, char *page)
5b9a4d72 3078{
2eafd729 3079 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
5b9a4d72
NB
3080}
3081
2eafd729
CH
3082static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
3083 struct config_item *item, const char *page, size_t count)
5b9a4d72 3084{
2eafd729
CH
3085 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
3086 count);
5b9a4d72
NB
3087}
3088
2eafd729
CH
3089static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
3090 char *page)
c66ac9db 3091{
2eafd729 3092 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
c66ac9db
NB
3093}
3094
2eafd729
CH
3095static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
3096 const char *page, size_t count)
c66ac9db 3097{
2eafd729 3098 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
c66ac9db
NB
3099}
3100
2eafd729
CH
3101static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3102 char *page)
c66ac9db 3103{
2eafd729
CH
3104 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3105
6708bb27 3106 if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db 3107 return 0;
c66ac9db
NB
3108 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3109}
3110
2eafd729
CH
3111static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
3112 const char *page, size_t count)
c66ac9db 3113{
2eafd729 3114 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
3115 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3116 unsigned long tg_pt_gp_id;
3117 int ret;
3118
57103d7f 3119 ret = kstrtoul(page, 0, &tg_pt_gp_id);
c66ac9db 3120 if (ret < 0) {
3d035237
HR
3121 pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
3122 page);
57103d7f 3123 return ret;
c66ac9db
NB
3124 }
3125 if (tg_pt_gp_id > 0x0000ffff) {
3d035237
HR
3126 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
3127 tg_pt_gp_id);
c66ac9db
NB
3128 return -EINVAL;
3129 }
3130
3131 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3132 if (ret < 0)
3133 return -EINVAL;
3134
6708bb27 3135 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
c66ac9db
NB
3136 "core/alua/tg_pt_gps/%s to ID: %hu\n",
3137 config_item_name(&alua_tg_pt_gp_cg->cg_item),
3138 tg_pt_gp->tg_pt_gp_id);
3139
3140 return count;
3141}
3142
2eafd729
CH
3143static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3144 char *page)
c66ac9db 3145{
2eafd729 3146 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db 3147 struct se_lun *lun;
c66ac9db 3148 ssize_t len = 0, cur_len;
2d4e2daf 3149 unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
c66ac9db
NB
3150
3151 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
3152 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3153 lun_tg_pt_gp_link) {
3154 struct se_portal_group *tpg = lun->lun_tpg;
c66ac9db
NB
3155
3156 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
30c7ca93 3157 "/%s\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909
AG
3158 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3159 tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db
NB
3160 config_item_name(&lun->lun_group.cg_item));
3161 cur_len++; /* Extra byte for NULL terminator */
3162
3163 if ((cur_len + len) > PAGE_SIZE) {
6708bb27 3164 pr_warn("Ran out of lu_gp_show_attr"
c66ac9db
NB
3165 "_members buffer\n");
3166 break;
3167 }
3168 memcpy(page+len, buf, cur_len);
3169 len += cur_len;
3170 }
3171 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3172
3173 return len;
3174}
3175
2eafd729
CH
3176CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
3177CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
3178CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
3179CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
3180CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
3181CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
3182CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
3183CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
3184CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
3185CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
3186CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
3187CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
3188CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
3189CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
3190CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
3191CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
3192CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
c66ac9db
NB
3193
3194static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2eafd729
CH
3195 &target_tg_pt_gp_attr_alua_access_state,
3196 &target_tg_pt_gp_attr_alua_access_status,
3197 &target_tg_pt_gp_attr_alua_access_type,
3198 &target_tg_pt_gp_attr_alua_support_transitioning,
3199 &target_tg_pt_gp_attr_alua_support_offline,
3200 &target_tg_pt_gp_attr_alua_support_lba_dependent,
3201 &target_tg_pt_gp_attr_alua_support_unavailable,
3202 &target_tg_pt_gp_attr_alua_support_standby,
3203 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
3204 &target_tg_pt_gp_attr_alua_support_active_optimized,
3205 &target_tg_pt_gp_attr_alua_write_metadata,
3206 &target_tg_pt_gp_attr_nonop_delay_msecs,
3207 &target_tg_pt_gp_attr_trans_delay_msecs,
3208 &target_tg_pt_gp_attr_implicit_trans_secs,
3209 &target_tg_pt_gp_attr_preferred,
3210 &target_tg_pt_gp_attr_tg_pt_gp_id,
3211 &target_tg_pt_gp_attr_members,
c66ac9db
NB
3212 NULL,
3213};
3214
1f6fe7cb
NB
3215static void target_core_alua_tg_pt_gp_release(struct config_item *item)
3216{
3217 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3218 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3219
3220 core_alua_free_tg_pt_gp(tg_pt_gp);
3221}
3222
c66ac9db 3223static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
1f6fe7cb 3224 .release = target_core_alua_tg_pt_gp_release,
c66ac9db
NB
3225};
3226
ece550b5 3227static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
c66ac9db
NB
3228 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
3229 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
3230 .ct_owner = THIS_MODULE,
3231};
3232
3233/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3234
72aca57b 3235/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
c66ac9db
NB
3236
3237static struct config_group *target_core_alua_create_tg_pt_gp(
3238 struct config_group *group,
3239 const char *name)
3240{
3241 struct t10_alua *alua = container_of(group, struct t10_alua,
3242 alua_tg_pt_gps_group);
3243 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
3244 struct config_group *alua_tg_pt_gp_cg = NULL;
3245 struct config_item *alua_tg_pt_gp_ci = NULL;
3246
0fd97ccf 3247 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
6708bb27 3248 if (!tg_pt_gp)
c66ac9db
NB
3249 return NULL;
3250
3251 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3252 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3253
3254 config_group_init_type_name(alua_tg_pt_gp_cg, name,
3255 &target_core_alua_tg_pt_gp_cit);
3256
6708bb27 3257 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
c66ac9db
NB
3258 " Group: alua/tg_pt_gps/%s\n",
3259 config_item_name(alua_tg_pt_gp_ci));
3260
3261 return alua_tg_pt_gp_cg;
3262}
3263
3264static void target_core_alua_drop_tg_pt_gp(
3265 struct config_group *group,
3266 struct config_item *item)
3267{
3268 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3269 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3270
6708bb27 3271 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
c66ac9db
NB
3272 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3273 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
1f6fe7cb
NB
3274 /*
3275 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3276 * -> target_core_alua_tg_pt_gp_release().
3277 */
c66ac9db 3278 config_item_put(item);
c66ac9db
NB
3279}
3280
3281static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3282 .make_group = &target_core_alua_create_tg_pt_gp,
3283 .drop_item = &target_core_alua_drop_tg_pt_gp,
3284};
3285
72aca57b 3286TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
c66ac9db 3287
72aca57b 3288/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
c66ac9db
NB
3289
3290/* Start functions for struct config_item_type target_core_alua_cit */
3291
3292/*
3293 * target_core_alua_cit is a ConfigFS group that lives under
3294 * /sys/kernel/config/target/core/alua. There are default groups
3295 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3296 * target_core_alua_cit in target_core_init_configfs() below.
3297 */
ece550b5 3298static const struct config_item_type target_core_alua_cit = {
c66ac9db
NB
3299 .ct_item_ops = NULL,
3300 .ct_attrs = NULL,
3301 .ct_owner = THIS_MODULE,
3302};
3303
3304/* End functions for struct config_item_type target_core_alua_cit */
3305
d23ab570 3306/* Start functions for struct config_item_type tb_dev_stat_cit */
12d23384
NB
3307
3308static struct config_group *target_core_stat_mkdir(
3309 struct config_group *group,
3310 const char *name)
3311{
3312 return ERR_PTR(-ENOSYS);
3313}
3314
3315static void target_core_stat_rmdir(
3316 struct config_group *group,
3317 struct config_item *item)
3318{
3319 return;
3320}
3321
3322static struct configfs_group_operations target_core_stat_group_ops = {
3323 .make_group = &target_core_stat_mkdir,
3324 .drop_item = &target_core_stat_rmdir,
3325};
3326
d23ab570 3327TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
12d23384 3328
d23ab570 3329/* End functions for struct config_item_type tb_dev_stat_cit */
12d23384 3330
c66ac9db
NB
3331/* Start functions for struct config_item_type target_core_hba_cit */
3332
3333static struct config_group *target_core_make_subdev(
3334 struct config_group *group,
3335 const char *name)
3336{
3337 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
3338 struct config_item *hba_ci = &group->cg_item;
3339 struct se_hba *hba = item_to_hba(hba_ci);
0a06d430 3340 struct target_backend *tb = hba->backend;
0fd97ccf 3341 struct se_device *dev;
12d23384 3342 int errno = -ENOMEM, ret;
c66ac9db 3343
12d23384
NB
3344 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3345 if (ret)
3346 return ERR_PTR(ret);
c66ac9db 3347
0fd97ccf
CH
3348 dev = target_alloc_device(hba, name);
3349 if (!dev)
3350 goto out_unlock;
3351
1ae1602d 3352 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
c66ac9db 3353
8dc31ff9
MC
3354 config_group_init_type_name(&dev->dev_action_group, "action",
3355 &tb->tb_dev_action_cit);
3356 configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3357
0fd97ccf 3358 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
0a06d430 3359 &tb->tb_dev_attrib_cit);
1ae1602d
CH
3360 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3361
0fd97ccf 3362 config_group_init_type_name(&dev->dev_pr_group, "pr",
0a06d430 3363 &tb->tb_dev_pr_cit);
1ae1602d
CH
3364 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3365
0fd97ccf 3366 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
0a06d430 3367 &tb->tb_dev_wwn_cit);
1ae1602d
CH
3368 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3369 &dev->dev_group);
3370
0fd97ccf 3371 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
0a06d430 3372 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
1ae1602d
CH
3373 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3374 &dev->dev_group);
3375
0fd97ccf 3376 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
0a06d430 3377 "statistics", &tb->tb_dev_stat_cit);
1ae1602d
CH
3378 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3379 &dev->dev_group);
12d23384 3380
c66ac9db 3381 /*
12d23384 3382 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
c66ac9db 3383 */
0fd97ccf 3384 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
6708bb27 3385 if (!tg_pt_gp)
1ae1602d 3386 goto out_free_device;
0fd97ccf 3387 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
c66ac9db 3388
c66ac9db
NB
3389 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3390 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
1ae1602d
CH
3391 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3392 &dev->t10_alua.alua_tg_pt_gps_group);
3393
12d23384
NB
3394 /*
3395 * Add core/$HBA/$DEV/statistics/ default groups
3396 */
0fd97ccf 3397 target_stat_setup_dev_default_groups(dev);
c66ac9db 3398
80890c5e
ML
3399 mutex_lock(&target_devices_lock);
3400 target_devices++;
3401 mutex_unlock(&target_devices_lock);
3402
c66ac9db 3403 mutex_unlock(&hba->hba_access_mutex);
1ae1602d 3404 return &dev->dev_group;
0fd97ccf 3405
0fd97ccf
CH
3406out_free_device:
3407 target_free_device(dev);
3408out_unlock:
c66ac9db 3409 mutex_unlock(&hba->hba_access_mutex);
12d23384 3410 return ERR_PTR(errno);
c66ac9db
NB
3411}
3412
3413static void target_core_drop_subdev(
3414 struct config_group *group,
3415 struct config_item *item)
3416{
0fd97ccf
CH
3417 struct config_group *dev_cg = to_config_group(item);
3418 struct se_device *dev =
3419 container_of(dev_cg, struct se_device, dev_group);
c66ac9db 3420 struct se_hba *hba;
c66ac9db 3421
0fd97ccf 3422 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
c66ac9db 3423
1f6fe7cb 3424 mutex_lock(&hba->hba_access_mutex);
c66ac9db 3425
1ae1602d
CH
3426 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3427 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
12d23384 3428
1f6fe7cb
NB
3429 /*
3430 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3431 * directly from target_core_alua_tg_pt_gp_release().
3432 */
0fd97ccf 3433 dev->t10_alua.default_tg_pt_gp = NULL;
c66ac9db 3434
1ae1602d
CH
3435 configfs_remove_default_groups(dev_cg);
3436
c66ac9db 3437 /*
0fd97ccf 3438 * se_dev is released from target_core_dev_item_ops->release()
c66ac9db 3439 */
1f6fe7cb 3440 config_item_put(item);
80890c5e
ML
3441
3442 mutex_lock(&target_devices_lock);
3443 target_devices--;
3444 mutex_unlock(&target_devices_lock);
3445
c66ac9db 3446 mutex_unlock(&hba->hba_access_mutex);
c66ac9db
NB
3447}
3448
3449static struct configfs_group_operations target_core_hba_group_ops = {
3450 .make_group = target_core_make_subdev,
3451 .drop_item = target_core_drop_subdev,
3452};
3453
c66ac9db 3454
2eafd729
CH
3455static inline struct se_hba *to_hba(struct config_item *item)
3456{
3457 return container_of(to_config_group(item), struct se_hba, hba_group);
3458}
c66ac9db 3459
2eafd729 3460static ssize_t target_hba_info_show(struct config_item *item, char *page)
c66ac9db 3461{
2eafd729
CH
3462 struct se_hba *hba = to_hba(item);
3463
c66ac9db 3464 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
0a06d430 3465 hba->hba_id, hba->backend->ops->name,
ce8dd25d 3466 TARGET_CORE_VERSION);
c66ac9db
NB
3467}
3468
2eafd729 3469static ssize_t target_hba_mode_show(struct config_item *item, char *page)
c66ac9db 3470{
2eafd729 3471 struct se_hba *hba = to_hba(item);
c66ac9db
NB
3472 int hba_mode = 0;
3473
3474 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3475 hba_mode = 1;
3476
3477 return sprintf(page, "%d\n", hba_mode);
3478}
3479
2eafd729
CH
3480static ssize_t target_hba_mode_store(struct config_item *item,
3481 const char *page, size_t count)
c66ac9db 3482{
2eafd729 3483 struct se_hba *hba = to_hba(item);
c66ac9db
NB
3484 unsigned long mode_flag;
3485 int ret;
3486
0a06d430 3487 if (hba->backend->ops->pmode_enable_hba == NULL)
c66ac9db
NB
3488 return -EINVAL;
3489
57103d7f 3490 ret = kstrtoul(page, 0, &mode_flag);
c66ac9db 3491 if (ret < 0) {
6708bb27 3492 pr_err("Unable to extract hba mode flag: %d\n", ret);
57103d7f 3493 return ret;
c66ac9db
NB
3494 }
3495
0fd97ccf 3496 if (hba->dev_count) {
6708bb27 3497 pr_err("Unable to set hba_mode with active devices\n");
c66ac9db
NB
3498 return -EINVAL;
3499 }
c66ac9db 3500
0a06d430 3501 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
c66ac9db
NB
3502 if (ret < 0)
3503 return -EINVAL;
3504 if (ret > 0)
3505 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3506 else if (ret == 0)
3507 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3508
3509 return count;
3510}
3511
2eafd729
CH
3512CONFIGFS_ATTR_RO(target_, hba_info);
3513CONFIGFS_ATTR(target_, hba_mode);
c66ac9db 3514
1f6fe7cb
NB
3515static void target_core_hba_release(struct config_item *item)
3516{
3517 struct se_hba *hba = container_of(to_config_group(item),
3518 struct se_hba, hba_group);
3519 core_delete_hba(hba);
3520}
3521
c66ac9db 3522static struct configfs_attribute *target_core_hba_attrs[] = {
2eafd729
CH
3523 &target_attr_hba_info,
3524 &target_attr_hba_mode,
c66ac9db
NB
3525 NULL,
3526};
3527
3528static struct configfs_item_operations target_core_hba_item_ops = {
1f6fe7cb 3529 .release = target_core_hba_release,
c66ac9db
NB
3530};
3531
ece550b5 3532static const struct config_item_type target_core_hba_cit = {
c66ac9db
NB
3533 .ct_item_ops = &target_core_hba_item_ops,
3534 .ct_group_ops = &target_core_hba_group_ops,
3535 .ct_attrs = target_core_hba_attrs,
3536 .ct_owner = THIS_MODULE,
3537};
3538
3539static struct config_group *target_core_call_addhbatotarget(
3540 struct config_group *group,
3541 const char *name)
3542{
3543 char *se_plugin_str, *str, *str2;
3544 struct se_hba *hba;
2d4e2daf 3545 char buf[TARGET_CORE_NAME_MAX_LEN] = { };
c66ac9db
NB
3546 unsigned long plugin_dep_id = 0;
3547 int ret;
3548
60d645a4 3549 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
6708bb27 3550 pr_err("Passed *name strlen(): %d exceeds"
c66ac9db
NB
3551 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3552 TARGET_CORE_NAME_MAX_LEN);
3553 return ERR_PTR(-ENAMETOOLONG);
3554 }
3555 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3556
3557 str = strstr(buf, "_");
6708bb27
AG
3558 if (!str) {
3559 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
c66ac9db
NB
3560 return ERR_PTR(-EINVAL);
3561 }
3562 se_plugin_str = buf;
3563 /*
3564 * Special case for subsystem plugins that have "_" in their names.
3565 * Namely rd_direct and rd_mcp..
3566 */
3567 str2 = strstr(str+1, "_");
6708bb27 3568 if (str2) {
c66ac9db
NB
3569 *str2 = '\0'; /* Terminate for *se_plugin_str */
3570 str2++; /* Skip to start of plugin dependent ID */
3571 str = str2;
3572 } else {
3573 *str = '\0'; /* Terminate for *se_plugin_str */
3574 str++; /* Skip to start of plugin dependent ID */
3575 }
3576
57103d7f 3577 ret = kstrtoul(str, 0, &plugin_dep_id);
c66ac9db 3578 if (ret < 0) {
57103d7f 3579 pr_err("kstrtoul() returned %d for"
c66ac9db 3580 " plugin_dep_id\n", ret);
57103d7f 3581 return ERR_PTR(ret);
c66ac9db
NB
3582 }
3583 /*
3584 * Load up TCM subsystem plugins if they have not already been loaded.
3585 */
dbc5623e 3586 transport_subsystem_check_init();
c66ac9db
NB
3587
3588 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3589 if (IS_ERR(hba))
3590 return ERR_CAST(hba);
3591
3592 config_group_init_type_name(&hba->hba_group, name,
3593 &target_core_hba_cit);
3594
3595 return &hba->hba_group;
3596}
3597
3598static void target_core_call_delhbafromtarget(
3599 struct config_group *group,
3600 struct config_item *item)
3601{
1f6fe7cb
NB
3602 /*
3603 * core_delete_hba() is called from target_core_hba_item_ops->release()
3604 * -> target_core_hba_release()
3605 */
c66ac9db 3606 config_item_put(item);
c66ac9db
NB
3607}
3608
3609static struct configfs_group_operations target_core_group_ops = {
3610 .make_group = target_core_call_addhbatotarget,
3611 .drop_item = target_core_call_delhbafromtarget,
3612};
3613
ece550b5 3614static const struct config_item_type target_core_cit = {
c66ac9db
NB
3615 .ct_item_ops = NULL,
3616 .ct_group_ops = &target_core_group_ops,
3617 .ct_attrs = NULL,
3618 .ct_owner = THIS_MODULE,
3619};
3620
3621/* Stop functions for struct config_item_type target_core_hba_cit */
3622
0a06d430 3623void target_setup_backend_cits(struct target_backend *tb)
73112edc 3624{
0a06d430 3625 target_core_setup_dev_cit(tb);
8dc31ff9 3626 target_core_setup_dev_action_cit(tb);
0a06d430
CH
3627 target_core_setup_dev_attrib_cit(tb);
3628 target_core_setup_dev_pr_cit(tb);
3629 target_core_setup_dev_wwn_cit(tb);
3630 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3631 target_core_setup_dev_stat_cit(tb);
73112edc 3632}
73112edc 3633
78a6295c
LD
3634static void target_init_dbroot(void)
3635{
3636 struct file *fp;
3637
3638 snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3639 fp = filp_open(db_root_stage, O_RDONLY, 0);
3640 if (IS_ERR(fp)) {
3641 pr_err("db_root: cannot open: %s\n", db_root_stage);
3642 return;
3643 }
3644 if (!S_ISDIR(file_inode(fp)->i_mode)) {
3645 filp_close(fp, NULL);
3646 pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3647 return;
3648 }
3649 filp_close(fp, NULL);
3650
3651 strncpy(db_root, db_root_stage, DB_ROOT_LEN);
3652 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3653}
3654
54550fab 3655static int __init target_core_init_configfs(void)
c66ac9db 3656{
d588cf8f 3657 struct configfs_subsystem *subsys = &target_core_fabrics;
c66ac9db
NB
3658 struct t10_alua_lu_gp *lu_gp;
3659 int ret;
3660
6708bb27 3661 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
c66ac9db
NB
3662 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3663 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3664
c66ac9db
NB
3665 config_group_init(&subsys->su_group);
3666 mutex_init(&subsys->su_mutex);
3667
e3d6f909 3668 ret = init_se_kmem_caches();
c66ac9db 3669 if (ret < 0)
e3d6f909 3670 return ret;
c66ac9db
NB
3671 /*
3672 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3673 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3674 */
1ae1602d
CH
3675 config_group_init_type_name(&target_core_hbagroup, "core",
3676 &target_core_cit);
3677 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
c66ac9db 3678
c66ac9db
NB
3679 /*
3680 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3681 */
1ae1602d
CH
3682 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3683 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3684
c66ac9db
NB
3685 /*
3686 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3687 * groups under /sys/kernel/config/target/core/alua/
3688 */
1ae1602d
CH
3689 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3690 &target_core_alua_lu_gps_cit);
3691 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
c66ac9db 3692
c66ac9db
NB
3693 /*
3694 * Add core/alua/lu_gps/default_lu_gp
3695 */
3696 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
37bb7899
PST
3697 if (IS_ERR(lu_gp)) {
3698 ret = -ENOMEM;
c66ac9db 3699 goto out_global;
37bb7899 3700 }
c66ac9db 3701
c66ac9db
NB
3702 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3703 &target_core_alua_lu_gp_cit);
1ae1602d
CH
3704 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3705
e3d6f909 3706 default_lu_gp = lu_gp;
1ae1602d 3707
c66ac9db
NB
3708 /*
3709 * Register the target_core_mod subsystem with configfs.
3710 */
3711 ret = configfs_register_subsystem(subsys);
3712 if (ret < 0) {
6708bb27 3713 pr_err("Error %d while registering subsystem %s\n",
c66ac9db
NB
3714 ret, subsys->su_group.cg_item.ci_namebuf);
3715 goto out_global;
3716 }
6708bb27 3717 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
ce8dd25d 3718 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
c66ac9db
NB
3719 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3720 /*
3721 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3722 */
3723 ret = rd_module_init();
3724 if (ret < 0)
3725 goto out;
3726
0d0f9dfb
RD
3727 ret = core_dev_setup_virtual_lun0();
3728 if (ret < 0)
c66ac9db
NB
3729 goto out;
3730
f99715ac
NB
3731 ret = target_xcopy_setup_pt();
3732 if (ret < 0)
3733 goto out;
3734
78a6295c
LD
3735 target_init_dbroot();
3736
c66ac9db
NB
3737 return 0;
3738
3739out:
3740 configfs_unregister_subsystem(subsys);
c66ac9db
NB
3741 core_dev_release_virtual_lun0();
3742 rd_module_exit();
3743out_global:
e3d6f909
AG
3744 if (default_lu_gp) {
3745 core_alua_free_lu_gp(default_lu_gp);
3746 default_lu_gp = NULL;
c66ac9db 3747 }
e3d6f909
AG
3748 release_se_kmem_caches();
3749 return ret;
c66ac9db
NB
3750}
3751
54550fab 3752static void __exit target_core_exit_configfs(void)
c66ac9db 3753{
1ae1602d
CH
3754 configfs_remove_default_groups(&alua_lu_gps_group);
3755 configfs_remove_default_groups(&alua_group);
3756 configfs_remove_default_groups(&target_core_hbagroup);
c66ac9db 3757
7c2bf6e9
NB
3758 /*
3759 * We expect subsys->su_group.default_groups to be released
3760 * by configfs subsystem provider logic..
3761 */
d588cf8f 3762 configfs_unregister_subsystem(&target_core_fabrics);
c66ac9db 3763
e3d6f909
AG
3764 core_alua_free_lu_gp(default_lu_gp);
3765 default_lu_gp = NULL;
7c2bf6e9 3766
6708bb27 3767 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
c66ac9db
NB
3768 " Infrastructure\n");
3769
c66ac9db
NB
3770 core_dev_release_virtual_lun0();
3771 rd_module_exit();
f99715ac 3772 target_xcopy_release_pt();
e3d6f909 3773 release_se_kmem_caches();
c66ac9db
NB
3774}
3775
3776MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3777MODULE_AUTHOR("nab@Linux-iSCSI.org");
3778MODULE_LICENSE("GPL");
3779
3780module_init(target_core_init_configfs);
3781module_exit(target_core_exit_configfs);