blob: 2e47fe68e4eab4f81473b37103eb98b174c0be41 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2008-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#include <generated/utsrelease.h>
26#include <linux/utsname.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/namei.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32#include <linux/delay.h>
33#include <linux/unistd.h>
34#include <linux/string.h>
35#include <linux/parser.h>
36#include <linux/syscalls.h>
37#include <linux/configfs.h>
Andy Grovere3d6f902011-07-19 08:55:10 +000038#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039
40#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050041#include <target/target_core_backend.h>
42#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043
Christoph Hellwige26d99a2011-11-14 12:30:30 -050044#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046#include "target_core_pr.h"
47#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070048#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080050#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020051static void target_core_setup_##_name##_cit(struct target_backend *tb) \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080052{ \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020053 struct config_item_type *cit = &tb->tb_##_name##_cit; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080054 \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = _attrs; \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020058 cit->ct_owner = tb->ops->owner; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
60}
61
62#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
63static void target_core_setup_##_name##_cit(struct target_backend *tb) \
64{ \
65 struct config_item_type *cit = &tb->tb_##_name##_cit; \
66 \
67 cit->ct_item_ops = _item_ops; \
68 cit->ct_group_ops = _group_ops; \
69 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
70 cit->ct_owner = tb->ops->owner; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080071 pr_debug("Setup generic %s\n", __stringify(_name)); \
72}
73
Andy Grovere3d6f902011-07-19 08:55:10 +000074extern struct t10_alua_lu_gp *default_lu_gp;
75
Roland Dreierd0f474e2012-01-12 10:41:18 -080076static LIST_HEAD(g_tf_list);
77static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grovere3d6f902011-07-19 08:55:10 +000079static struct config_group target_core_hbagroup;
80static struct config_group alua_group;
81static struct config_group alua_lu_gps_group;
82
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083static inline struct se_hba *
84item_to_hba(struct config_item *item)
85{
86 return container_of(to_config_group(item), struct se_hba, hba_group);
87}
88
89/*
90 * Attributes for /sys/kernel/config/target/
91 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +020092static ssize_t target_core_item_version_show(struct config_item *item,
93 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094{
95 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
Christoph Hellwigce8dd252015-06-19 15:14:39 +020096 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 utsname()->sysname, utsname()->machine);
98}
99
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200100CONFIGFS_ATTR_RO(target_core_item_, version);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101
102static struct target_fabric_configfs *target_core_get_fabric(
103 const char *name)
104{
105 struct target_fabric_configfs *tf;
106
Andy Grover6708bb22011-06-08 10:36:43 -0700107 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 return NULL;
109
110 mutex_lock(&g_tf_lock);
111 list_for_each_entry(tf, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200112 if (!strcmp(tf->tf_ops->name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 atomic_inc(&tf->tf_access_cnt);
114 mutex_unlock(&g_tf_lock);
115 return tf;
116 }
117 }
118 mutex_unlock(&g_tf_lock);
119
120 return NULL;
121}
122
123/*
124 * Called from struct target_core_group_ops->make_group()
125 */
126static struct config_group *target_core_register_fabric(
127 struct config_group *group,
128 const char *name)
129{
130 struct target_fabric_configfs *tf;
131 int ret;
132
Andy Grover6708bb22011-06-08 10:36:43 -0700133 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135
136 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700137 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700138 pr_debug("target_core_register_fabric() trying autoload for %s\n",
139 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800140
141 /*
142 * Below are some hardcoded request_module() calls to automatically
143 * local fabric modules when the following is called:
144 *
145 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
146 *
147 * Note that this does not limit which TCM fabric module can be
148 * registered, but simply provids auto loading logic for modules with
149 * mkdir(2) system calls with known TCM fabric modules.
150 */
151
152 if (!strncmp(name, "iscsi", 5)) {
153 /*
154 * Automatically load the LIO Target fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/iscsi
158 */
159 ret = request_module("iscsi_target_mod");
160 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700161 pr_debug("request_module() failed for"
162 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800163 return ERR_PTR(-EINVAL);
164 }
165 } else if (!strncmp(name, "loopback", 8)) {
166 /*
167 * Automatically load the tcm_loop fabric module when the
168 * following is called:
169 *
170 * mkdir -p $CONFIGFS/target/loopback
171 */
172 ret = request_module("tcm_loop");
173 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700174 pr_debug("request_module() failed for"
175 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800176 return ERR_PTR(-EINVAL);
177 }
178 }
179
180 tf = target_core_get_fabric(name);
181 }
182
183 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700184 pr_debug("target_core_get_fabric() failed for %s\n",
185 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800186 return ERR_PTR(-EINVAL);
187 }
Andy Grover6708bb22011-06-08 10:36:43 -0700188 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200189 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 /*
191 * On a successful target_core_get_fabric() look, the returned
192 * struct target_fabric_configfs *tf will contain a usage reference.
193 */
Andy Grover6708bb22011-06-08 10:36:43 -0700194 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200195 &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196
197 tf->tf_group.default_groups = tf->tf_default_groups;
198 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
199 tf->tf_group.default_groups[1] = NULL;
200
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200201 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200203 &tf->tf_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
Andy Grover6708bb22011-06-08 10:36:43 -0700205 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207 return &tf->tf_group;
208}
209
210/*
211 * Called from struct target_core_group_ops->drop_item()
212 */
213static void target_core_deregister_fabric(
214 struct config_group *group,
215 struct config_item *item)
216{
217 struct target_fabric_configfs *tf = container_of(
218 to_config_group(item), struct target_fabric_configfs, tf_group);
219 struct config_group *tf_group;
220 struct config_item *df_item;
221 int i;
222
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224 " tf list\n", config_item_name(item));
225
Andy Grover6708bb22011-06-08 10:36:43 -0700226 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200227 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 atomic_dec(&tf->tf_access_cnt);
229
Andy Grover6708bb22011-06-08 10:36:43 -0700230 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231 " %s\n", config_item_name(item));
232
233 tf_group = &tf->tf_group;
234 for (i = 0; tf_group->default_groups[i]; i++) {
235 df_item = &tf_group->default_groups[i]->cg_item;
236 tf_group->default_groups[i] = NULL;
237 config_item_put(df_item);
238 }
239 config_item_put(item);
240}
241
242static struct configfs_group_operations target_core_fabric_group_ops = {
243 .make_group = &target_core_register_fabric,
244 .drop_item = &target_core_deregister_fabric,
245};
246
247/*
248 * All item attributes appearing in /sys/kernel/target/ appear here.
249 */
250static struct configfs_attribute *target_core_fabric_item_attrs[] = {
251 &target_core_item_attr_version,
252 NULL,
253};
254
255/*
256 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
257 */
258static struct config_item_type target_core_fabrics_item = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 .ct_group_ops = &target_core_fabric_group_ops,
260 .ct_attrs = target_core_fabric_item_attrs,
261 .ct_owner = THIS_MODULE,
262};
263
264static struct configfs_subsystem target_core_fabrics = {
265 .su_group = {
266 .cg_item = {
267 .ci_namebuf = "target",
268 .ci_type = &target_core_fabrics_item,
269 },
270 },
271};
272
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200273int target_depend_item(struct config_item *item)
274{
275 return configfs_depend_item(&target_core_fabrics, item);
276}
277EXPORT_SYMBOL(target_depend_item);
278
279void target_undepend_item(struct config_item *item)
280{
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100281 return configfs_undepend_item(item);
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200282}
283EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284
285/*##############################################################################
286// Start functions called by external Target Fabrics Modules
287//############################################################################*/
288
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200289static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200291 if (!tfo->name) {
292 pr_err("Missing tfo->name\n");
293 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200295 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200297 "_NAME_SIZE\n", tfo->name);
298 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 }
Andy Grover6708bb22011-06-08 10:36:43 -0700300 if (!tfo->get_fabric_name) {
301 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 return -EINVAL;
303 }
Andy Grover6708bb22011-06-08 10:36:43 -0700304 if (!tfo->tpg_get_wwn) {
305 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 return -EINVAL;
307 }
Andy Grover6708bb22011-06-08 10:36:43 -0700308 if (!tfo->tpg_get_tag) {
309 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 return -EINVAL;
311 }
Andy Grover6708bb22011-06-08 10:36:43 -0700312 if (!tfo->tpg_check_demo_mode) {
313 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 return -EINVAL;
315 }
Andy Grover6708bb22011-06-08 10:36:43 -0700316 if (!tfo->tpg_check_demo_mode_cache) {
317 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 return -EINVAL;
319 }
Andy Grover6708bb22011-06-08 10:36:43 -0700320 if (!tfo->tpg_check_demo_mode_write_protect) {
321 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800322 return -EINVAL;
323 }
Andy Grover6708bb22011-06-08 10:36:43 -0700324 if (!tfo->tpg_check_prod_mode_write_protect) {
325 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 return -EINVAL;
327 }
Andy Grover6708bb22011-06-08 10:36:43 -0700328 if (!tfo->tpg_get_inst_index) {
329 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 return -EINVAL;
331 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400332 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700333 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334 return -EINVAL;
335 }
Andy Grover6708bb22011-06-08 10:36:43 -0700336 if (!tfo->shutdown_session) {
337 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 return -EINVAL;
339 }
Andy Grover6708bb22011-06-08 10:36:43 -0700340 if (!tfo->close_session) {
341 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800342 return -EINVAL;
343 }
Andy Grover6708bb22011-06-08 10:36:43 -0700344 if (!tfo->sess_get_index) {
345 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346 return -EINVAL;
347 }
Andy Grover6708bb22011-06-08 10:36:43 -0700348 if (!tfo->write_pending) {
349 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 return -EINVAL;
351 }
Andy Grover6708bb22011-06-08 10:36:43 -0700352 if (!tfo->write_pending_status) {
353 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 return -EINVAL;
355 }
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->set_default_node_attributes) {
357 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->get_cmd_state) {
361 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->queue_data_in) {
365 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->queue_status) {
369 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->queue_tm_rsp) {
373 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700376 if (!tfo->aborted_task) {
377 pr_err("Missing tfo->aborted_task()\n");
378 return -EINVAL;
379 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 /*
381 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
382 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
383 * target_core_fabric_configfs.c WWN+TPG group context code.
384 */
Andy Grover6708bb22011-06-08 10:36:43 -0700385 if (!tfo->fabric_make_wwn) {
386 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -EINVAL;
388 }
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!tfo->fabric_drop_wwn) {
390 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return -EINVAL;
392 }
Andy Grover6708bb22011-06-08 10:36:43 -0700393 if (!tfo->fabric_make_tpg) {
394 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 return -EINVAL;
396 }
Andy Grover6708bb22011-06-08 10:36:43 -0700397 if (!tfo->fabric_drop_tpg) {
398 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 return -EINVAL;
400 }
401
402 return 0;
403}
404
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200405int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200407 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 int ret;
409
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200410 ret = target_fabric_tf_ops_check(fo);
411 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 return ret;
413
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200414 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700415 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200416 pr_err("%s: could not allocate memory!\n", __func__);
417 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200419
420 INIT_LIST_HEAD(&tf->tf_list);
421 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200422 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200423 target_fabric_setup_cits(tf);
424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200426 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 mutex_unlock(&g_tf_lock);
428
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200429 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200431EXPORT_SYMBOL(target_register_template);
432
433void target_unregister_template(const struct target_core_fabric_ops *fo)
434{
435 struct target_fabric_configfs *t;
436
437 mutex_lock(&g_tf_lock);
438 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200439 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200440 BUG_ON(atomic_read(&t->tf_access_cnt));
441 list_del(&t->tf_list);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700442 mutex_unlock(&g_tf_lock);
443 /*
444 * Wait for any outstanding fabric se_deve_entry->rcu_head
445 * callbacks to complete post kfree_rcu(), before allowing
446 * fabric driver unload of TFO->module to proceed.
447 */
448 rcu_barrier();
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200449 kfree(t);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700450 return;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200451 }
452 }
453 mutex_unlock(&g_tf_lock);
454}
455EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456
457/*##############################################################################
458// Stop functions called by external Target Fabrics Modules
459//############################################################################*/
460
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200461static inline struct se_dev_attrib *to_attrib(struct config_item *item)
462{
463 return container_of(to_config_group(item), struct se_dev_attrib,
464 da_group);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200465}
466
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200467/* Start functions for struct config_item_type tb_dev_attrib_cit */
468#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
469static ssize_t _name##_show(struct config_item *item, char *page) \
470{ \
471 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
472}
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200473
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200474DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
475DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
476DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
477DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
478DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
479DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
480DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
481DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
482DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
483DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
484DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
485DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
486DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
487DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
488DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
489DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
490DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
491DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
492DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
493DEF_CONFIGFS_ATTRIB_SHOW(block_size);
494DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
495DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
496DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
497DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
498DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
499DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
500DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
501DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
502DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
503
504#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
505static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200506 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200507{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200508 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200509 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200510 int ret; \
511 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200512 ret = kstrtou32(page, 0, &val); \
513 if (ret < 0) \
514 return ret; \
515 da->_name = val; \
516 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200517}
518
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200519DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
520DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
521DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
522DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
523DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200524
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200525#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
526static ssize_t _name##_store(struct config_item *item, const char *page, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200527 size_t count) \
528{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200529 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200530 bool flag; \
531 int ret; \
532 \
533 ret = strtobool(page, &flag); \
534 if (ret < 0) \
535 return ret; \
536 da->_name = flag; \
537 return count; \
538}
539
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200540DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
541DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
542DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
543DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
544DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200545
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200546#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
547static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200548 size_t count) \
549{ \
550 printk_once(KERN_WARNING \
551 "ignoring deprecated ##_name## attribute\n"); \
552 return count; \
553}
554
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200555DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
556DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200557
558static void dev_set_t10_wwn_model_alias(struct se_device *dev)
559{
560 const char *configname;
561
562 configname = config_item_name(&dev->dev_group.cg_item);
563 if (strlen(configname) >= 16) {
564 pr_warn("dev[%p]: Backstore name '%s' is too long for "
565 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
566 configname);
567 }
568 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
569}
570
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200571static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200572 const char *page, size_t count)
573{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200574 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200575 struct se_device *dev = da->da_dev;
576 bool flag;
577 int ret;
578
579 if (dev->export_count) {
580 pr_err("dev[%p]: Unable to change model alias"
581 " while export_count is %d\n",
582 dev, dev->export_count);
583 return -EINVAL;
584 }
585
586 ret = strtobool(page, &flag);
587 if (ret < 0)
588 return ret;
589
590 if (flag) {
591 dev_set_t10_wwn_model_alias(dev);
592 } else {
593 strncpy(&dev->t10_wwn.model[0],
594 dev->transport->inquiry_prod, 16);
595 }
596 da->emulate_model_alias = flag;
597 return count;
598}
599
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200600static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200601 const char *page, size_t count)
602{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200603 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200604 bool flag;
605 int ret;
606
607 ret = strtobool(page, &flag);
608 if (ret < 0)
609 return ret;
610
611 if (flag && da->da_dev->transport->get_write_cache) {
612 pr_err("emulate_write_cache not supported for this device\n");
613 return -EINVAL;
614 }
615
616 da->emulate_write_cache = flag;
617 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
618 da->da_dev, flag);
619 return count;
620}
621
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200622static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200623 const char *page, size_t count)
624{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200625 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200626 u32 val;
627 int ret;
628
629 ret = kstrtou32(page, 0, &val);
630 if (ret < 0)
631 return ret;
632
633 if (val != 0 && val != 1 && val != 2) {
634 pr_err("Illegal value %d\n", val);
635 return -EINVAL;
636 }
637
638 if (da->da_dev->export_count) {
639 pr_err("dev[%p]: Unable to change SE Device"
640 " UA_INTRLCK_CTRL while export_count is %d\n",
641 da->da_dev, da->da_dev->export_count);
642 return -EINVAL;
643 }
644 da->emulate_ua_intlck_ctrl = val;
645 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
646 da->da_dev, val);
647 return count;
648}
649
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200650static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200651 const char *page, size_t count)
652{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200653 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200654 bool flag;
655 int ret;
656
657 ret = strtobool(page, &flag);
658 if (ret < 0)
659 return ret;
660
661 if (da->da_dev->export_count) {
662 pr_err("dev[%p]: Unable to change SE Device TAS while"
663 " export_count is %d\n",
664 da->da_dev, da->da_dev->export_count);
665 return -EINVAL;
666 }
667 da->emulate_tas = flag;
668 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
669 da->da_dev, flag ? "Enabled" : "Disabled");
670
671 return count;
672}
673
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200674static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200675 const char *page, size_t count)
676{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200677 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200678 bool flag;
679 int ret;
680
681 ret = strtobool(page, &flag);
682 if (ret < 0)
683 return ret;
684
685 /*
686 * We expect this value to be non-zero when generic Block Layer
687 * Discard supported is detected iblock_create_virtdevice().
688 */
689 if (flag && !da->max_unmap_block_desc_count) {
690 pr_err("Generic Block Discard not supported\n");
691 return -ENOSYS;
692 }
693
694 da->emulate_tpu = flag;
695 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
696 da->da_dev, flag);
697 return count;
698}
699
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200700static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200701 const char *page, size_t count)
702{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200703 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200704 bool flag;
705 int ret;
706
707 ret = strtobool(page, &flag);
708 if (ret < 0)
709 return ret;
710
711 /*
712 * We expect this value to be non-zero when generic Block Layer
713 * Discard supported is detected iblock_create_virtdevice().
714 */
715 if (flag && !da->max_unmap_block_desc_count) {
716 pr_err("Generic Block Discard not supported\n");
717 return -ENOSYS;
718 }
719
720 da->emulate_tpws = flag;
721 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
722 da->da_dev, flag);
723 return count;
724}
725
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200726static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200727 const char *page, size_t count)
728{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200729 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200730 int old_prot = da->pi_prot_type, ret;
731 struct se_device *dev = da->da_dev;
732 u32 flag;
733
734 ret = kstrtou32(page, 0, &flag);
735 if (ret < 0)
736 return ret;
737
738 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
739 pr_err("Illegal value %d for pi_prot_type\n", flag);
740 return -EINVAL;
741 }
742 if (flag == 2) {
743 pr_err("DIF TYPE2 protection currently not supported\n");
744 return -ENOSYS;
745 }
746 if (da->hw_pi_prot_type) {
747 pr_warn("DIF protection enabled on underlying hardware,"
748 " ignoring\n");
749 return count;
750 }
751 if (!dev->transport->init_prot || !dev->transport->free_prot) {
752 /* 0 is only allowed value for non-supporting backends */
753 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700754 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200755
756 pr_err("DIF protection not supported by backend: %s\n",
757 dev->transport->name);
758 return -ENOSYS;
759 }
760 if (!(dev->dev_flags & DF_CONFIGURED)) {
761 pr_err("DIF protection requires device to be configured\n");
762 return -ENODEV;
763 }
764 if (dev->export_count) {
765 pr_err("dev[%p]: Unable to change SE Device PROT type while"
766 " export_count is %d\n", dev, dev->export_count);
767 return -EINVAL;
768 }
769
770 da->pi_prot_type = flag;
771
772 if (flag && !old_prot) {
773 ret = dev->transport->init_prot(dev);
774 if (ret) {
775 da->pi_prot_type = old_prot;
776 return ret;
777 }
778
779 } else if (!flag && old_prot) {
780 dev->transport->free_prot(dev);
781 }
782
783 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
784 return count;
785}
786
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200787static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200788 const char *page, size_t count)
789{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200790 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200791 struct se_device *dev = da->da_dev;
792 bool flag;
793 int ret;
794
795 ret = strtobool(page, &flag);
796 if (ret < 0)
797 return ret;
798
799 if (!flag)
800 return count;
801
802 if (!dev->transport->format_prot) {
803 pr_err("DIF protection format not supported by backend %s\n",
804 dev->transport->name);
805 return -ENOSYS;
806 }
807 if (!(dev->dev_flags & DF_CONFIGURED)) {
808 pr_err("DIF protection format requires device to be configured\n");
809 return -ENODEV;
810 }
811 if (dev->export_count) {
812 pr_err("dev[%p]: Unable to format SE Device PROT type while"
813 " export_count is %d\n", dev, dev->export_count);
814 return -EINVAL;
815 }
816
817 ret = dev->transport->format_prot(dev);
818 if (ret)
819 return ret;
820
821 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
822 return count;
823}
824
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200825static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200826 const char *page, size_t count)
827{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200828 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200829 bool flag;
830 int ret;
831
832 ret = strtobool(page, &flag);
833 if (ret < 0)
834 return ret;
835 if (da->da_dev->export_count) {
836 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
837 " export_count is %d\n",
838 da->da_dev, da->da_dev->export_count);
839 return -EINVAL;
840 }
841
842 da->force_pr_aptpl = flag;
843 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
844 return count;
845}
846
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200847static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200848 const char *page, size_t count)
849{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200850 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200851 bool flag;
852 int ret;
853
854 ret = strtobool(page, &flag);
855 if (ret < 0)
856 return ret;
857
858 if (flag != 0) {
859 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
860 " reordering not implemented\n", da->da_dev);
861 return -ENOSYS;
862 }
863 da->emulate_rest_reord = flag;
864 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
865 da->da_dev, flag);
866 return count;
867}
868
869/*
870 * Note, this can only be called on unexported SE Device Object.
871 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200872static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200873 const char *page, size_t count)
874{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200875 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200876 struct se_device *dev = da->da_dev;
877 u32 val;
878 int ret;
879
880 ret = kstrtou32(page, 0, &val);
881 if (ret < 0)
882 return ret;
883
884 if (dev->export_count) {
885 pr_err("dev[%p]: Unable to change SE Device TCQ while"
886 " export_count is %d\n",
887 dev, dev->export_count);
888 return -EINVAL;
889 }
890 if (!val) {
891 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
892 return -EINVAL;
893 }
894
895 if (val > dev->dev_attrib.queue_depth) {
896 if (val > dev->dev_attrib.hw_queue_depth) {
897 pr_err("dev[%p]: Passed queue_depth:"
898 " %u exceeds TCM/SE_Device MAX"
899 " TCQ: %u\n", dev, val,
900 dev->dev_attrib.hw_queue_depth);
901 return -EINVAL;
902 }
903 }
904 da->queue_depth = dev->queue_depth = val;
905 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
906 return count;
907}
908
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200909static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200910 const char *page, size_t count)
911{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200912 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200913 u32 val;
914 int ret;
915
916 ret = kstrtou32(page, 0, &val);
917 if (ret < 0)
918 return ret;
919
920 if (da->da_dev->export_count) {
921 pr_err("dev[%p]: Unable to change SE Device"
922 " optimal_sectors while export_count is %d\n",
923 da->da_dev, da->da_dev->export_count);
924 return -EINVAL;
925 }
926 if (val > da->hw_max_sectors) {
927 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
928 " greater than hw_max_sectors: %u\n",
929 da->da_dev, val, da->hw_max_sectors);
930 return -EINVAL;
931 }
932
933 da->optimal_sectors = val;
934 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
935 da->da_dev, val);
936 return count;
937}
938
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200939static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200940 const char *page, size_t count)
941{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200942 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200943 u32 val;
944 int ret;
945
946 ret = kstrtou32(page, 0, &val);
947 if (ret < 0)
948 return ret;
949
950 if (da->da_dev->export_count) {
951 pr_err("dev[%p]: Unable to change SE Device block_size"
952 " while export_count is %d\n",
953 da->da_dev, da->da_dev->export_count);
954 return -EINVAL;
955 }
956
957 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
958 pr_err("dev[%p]: Illegal value for block_device: %u"
959 " for SE device, must be 512, 1024, 2048 or 4096\n",
960 da->da_dev, val);
961 return -EINVAL;
962 }
963
964 da->block_size = val;
965 if (da->max_bytes_per_io)
966 da->hw_max_sectors = da->max_bytes_per_io / val;
967
968 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
969 da->da_dev, val);
970 return count;
971}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200972
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200973CONFIGFS_ATTR(, emulate_model_alias);
974CONFIGFS_ATTR(, emulate_dpo);
975CONFIGFS_ATTR(, emulate_fua_write);
976CONFIGFS_ATTR(, emulate_fua_read);
977CONFIGFS_ATTR(, emulate_write_cache);
978CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
979CONFIGFS_ATTR(, emulate_tas);
980CONFIGFS_ATTR(, emulate_tpu);
981CONFIGFS_ATTR(, emulate_tpws);
982CONFIGFS_ATTR(, emulate_caw);
983CONFIGFS_ATTR(, emulate_3pc);
984CONFIGFS_ATTR(, pi_prot_type);
985CONFIGFS_ATTR_RO(, hw_pi_prot_type);
986CONFIGFS_ATTR(, pi_prot_format);
987CONFIGFS_ATTR(, enforce_pr_isids);
988CONFIGFS_ATTR(, is_nonrot);
989CONFIGFS_ATTR(, emulate_rest_reord);
990CONFIGFS_ATTR(, force_pr_aptpl);
991CONFIGFS_ATTR_RO(, hw_block_size);
992CONFIGFS_ATTR(, block_size);
993CONFIGFS_ATTR_RO(, hw_max_sectors);
994CONFIGFS_ATTR(, optimal_sectors);
995CONFIGFS_ATTR_RO(, hw_queue_depth);
996CONFIGFS_ATTR(, queue_depth);
997CONFIGFS_ATTR(, max_unmap_lba_count);
998CONFIGFS_ATTR(, max_unmap_block_desc_count);
999CONFIGFS_ATTR(, unmap_granularity);
1000CONFIGFS_ATTR(, unmap_granularity_alignment);
1001CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001003/*
1004 * dev_attrib attributes for devices using the target core SBC/SPC
1005 * interpreter. Any backend using spc_parse_cdb should be using
1006 * these.
1007 */
1008struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001009 &attr_emulate_model_alias,
1010 &attr_emulate_dpo,
1011 &attr_emulate_fua_write,
1012 &attr_emulate_fua_read,
1013 &attr_emulate_write_cache,
1014 &attr_emulate_ua_intlck_ctrl,
1015 &attr_emulate_tas,
1016 &attr_emulate_tpu,
1017 &attr_emulate_tpws,
1018 &attr_emulate_caw,
1019 &attr_emulate_3pc,
1020 &attr_pi_prot_type,
1021 &attr_hw_pi_prot_type,
1022 &attr_pi_prot_format,
1023 &attr_enforce_pr_isids,
1024 &attr_is_nonrot,
1025 &attr_emulate_rest_reord,
1026 &attr_force_pr_aptpl,
1027 &attr_hw_block_size,
1028 &attr_block_size,
1029 &attr_hw_max_sectors,
1030 &attr_optimal_sectors,
1031 &attr_hw_queue_depth,
1032 &attr_queue_depth,
1033 &attr_max_unmap_lba_count,
1034 &attr_max_unmap_block_desc_count,
1035 &attr_unmap_granularity,
1036 &attr_unmap_granularity_alignment,
1037 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001038 NULL,
1039};
1040EXPORT_SYMBOL(sbc_attrib_attrs);
1041
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001042/*
1043 * Minimal dev_attrib attributes for devices passing through CDBs.
1044 * In this case we only provide a few read-only attributes for
1045 * backwards compatibility.
1046 */
1047struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001048 &attr_hw_pi_prot_type,
1049 &attr_hw_block_size,
1050 &attr_hw_max_sectors,
1051 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001052 NULL,
1053};
1054EXPORT_SYMBOL(passthrough_attrib_attrs);
1055
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001056TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001058/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001060/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001062static struct t10_wwn *to_t10_wwn(struct config_item *item)
1063{
1064 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1065}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066
1067/*
1068 * VPD page 0x80 Unit serial
1069 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001070static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1071 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001074 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075}
1076
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001077static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1078 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001080 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001081 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1083
1084 /*
1085 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1086 * from the struct scsi_device level firmware, do not allow
1087 * VPD Unit Serial to be emulated.
1088 *
1089 * Note this struct scsi_device could also be emulating VPD
1090 * information from its drivers/scsi LLD. But for now we assume
1091 * it is doing 'the right thing' wrt a world wide unique
1092 * VPD Unit Serial Number that OS dependent multipath can depend on.
1093 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001094 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001095 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096 " Unit Serial, ignoring request\n");
1097 return -EOPNOTSUPP;
1098 }
1099
Dan Carpenter60d645a2011-06-15 10:03:05 -07001100 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001101 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1103 return -EOVERFLOW;
1104 }
1105 /*
1106 * Check to see if any active $FABRIC_MOD exports exist. If they
1107 * do exist, fail here as changing this information on the fly
1108 * (underneath the initiator side OS dependent multipath code)
1109 * could cause negative effects.
1110 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001111 if (dev->export_count) {
1112 pr_err("Unable to set VPD Unit Serial while"
1113 " active %d $FABRIC_MOD exports exist\n",
1114 dev->export_count);
1115 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001116 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001117
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118 /*
1119 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1120 *
1121 * Also, strip any newline added from the userspace
1122 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1123 */
1124 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1125 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001126 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001127 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001128 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129
Andy Grover6708bb22011-06-08 10:36:43 -07001130 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001131 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132
1133 return count;
1134}
1135
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136/*
1137 * VPD page 0x83 Protocol Identifier
1138 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001139static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1140 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001142 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 struct t10_vpd *vpd;
1144 unsigned char buf[VPD_TMP_BUF_SIZE];
1145 ssize_t len = 0;
1146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147 memset(buf, 0, VPD_TMP_BUF_SIZE);
1148
1149 spin_lock(&t10_wwn->t10_vpd_lock);
1150 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001151 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 continue;
1153
1154 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1155
Andy Grover6708bb22011-06-08 10:36:43 -07001156 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 break;
1158
1159 len += sprintf(page+len, "%s", buf);
1160 }
1161 spin_unlock(&t10_wwn->t10_vpd_lock);
1162
1163 return len;
1164}
1165
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166/*
1167 * Generic wrapper for dumping VPD identifiers by association.
1168 */
1169#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001170static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1171 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001173 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1174 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001175 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1176 ssize_t len = 0; \
1177 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178 spin_lock(&t10_wwn->t10_vpd_lock); \
1179 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1180 if (vpd->association != _assoc) \
1181 continue; \
1182 \
1183 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1184 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001185 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186 break; \
1187 len += sprintf(page+len, "%s", buf); \
1188 \
1189 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1190 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001191 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 break; \
1193 len += sprintf(page+len, "%s", buf); \
1194 \
1195 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1196 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001197 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 break; \
1199 len += sprintf(page+len, "%s", buf); \
1200 } \
1201 spin_unlock(&t10_wwn->t10_vpd_lock); \
1202 \
1203 return len; \
1204}
1205
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001206/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001208/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001210/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1212
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001213CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1214CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1215CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1216CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1217CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218
1219static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001220 &target_wwn_attr_vpd_unit_serial,
1221 &target_wwn_attr_vpd_protocol_identifier,
1222 &target_wwn_attr_vpd_assoc_logical_unit,
1223 &target_wwn_attr_vpd_assoc_target_port,
1224 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 NULL,
1226};
1227
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001228TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001230/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001232/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001234static struct se_device *pr_to_dev(struct config_item *item)
1235{
1236 return container_of(to_config_group(item), struct se_device,
1237 dev_pr_group);
1238}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001239
Christoph Hellwigd977f432012-10-10 17:37:15 -04001240static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1241 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242{
1243 struct se_node_acl *se_nacl;
1244 struct t10_pr_registration *pr_reg;
1245 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246
1247 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1248
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001250 if (!pr_reg)
1251 return sprintf(page, "No SPC-3 Reservation holder\n");
1252
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001254 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255
Christoph Hellwigd977f432012-10-10 17:37:15 -04001256 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001257 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001258 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259}
1260
Christoph Hellwigd977f432012-10-10 17:37:15 -04001261static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1262 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263{
1264 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001265 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001268 if (se_nacl) {
1269 len = sprintf(page,
1270 "SPC-2 Reservation: %s Initiator: %s\n",
1271 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1272 se_nacl->initiatorname);
1273 } else {
1274 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001276 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277}
1278
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001279static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001281 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001282 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283
Andy Grovera3541702015-05-19 14:44:41 -07001284 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001285 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286
Christoph Hellwigd977f432012-10-10 17:37:15 -04001287 spin_lock(&dev->dev_reservation_lock);
1288 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1289 ret = target_core_dev_pr_show_spc2_res(dev, page);
1290 else
1291 ret = target_core_dev_pr_show_spc3_res(dev, page);
1292 spin_unlock(&dev->dev_reservation_lock);
1293 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294}
1295
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001296static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1297 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001299 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 ssize_t len = 0;
1301
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001303 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001305 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 len = sprintf(page, "SPC-3 Reservation: All Target"
1307 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001308 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 len = sprintf(page, "SPC-3 Reservation: Single"
1310 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001311 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312
Christoph Hellwigd977f432012-10-10 17:37:15 -04001313 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 return len;
1315}
1316
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001317static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1318 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001320 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321}
1322
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001324static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1325 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001327 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001328 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 struct se_portal_group *se_tpg;
1330 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001331 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001332 ssize_t len = 0;
1333
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 spin_lock(&dev->dev_reservation_lock);
1335 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001336 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001338 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001340
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 se_nacl = pr_reg->pr_reg_nacl;
1342 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001343 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344
1345 len += sprintf(page+len, "SPC-3 Reservation: %s"
1346 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1347 tfo->tpg_get_wwn(se_tpg));
1348 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001349 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001350 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001352 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353
Christoph Hellwigd977f432012-10-10 17:37:15 -04001354out_unlock:
1355 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 return len;
1357}
1358
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001360static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1361 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001363 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001364 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 struct t10_pr_registration *pr_reg;
1366 unsigned char buf[384];
1367 char i_buf[PR_REG_ISID_ID_LEN];
1368 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001369 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1372
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001373 spin_lock(&dev->t10_pr.registration_lock);
1374 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375 pr_reg_list) {
1376
1377 memset(buf, 0, 384);
1378 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1379 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001380 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381 PR_REG_ISID_ID_LEN);
1382 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1383 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001384 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 pr_reg->pr_res_generation);
1386
Andy Grover6708bb22011-06-08 10:36:43 -07001387 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 break;
1389
1390 len += sprintf(page+len, "%s", buf);
1391 reg_count++;
1392 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001393 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394
Andy Grover6708bb22011-06-08 10:36:43 -07001395 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396 len += sprintf(page+len, "None\n");
1397
1398 return len;
1399}
1400
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001401static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001403 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 struct t10_pr_registration *pr_reg;
1405 ssize_t len = 0;
1406
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407 spin_lock(&dev->dev_reservation_lock);
1408 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001409 if (pr_reg) {
1410 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1411 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1412 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001413 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001414 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415
Christoph Hellwigd977f432012-10-10 17:37:15 -04001416 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 return len;
1418}
1419
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001420static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001422 struct se_device *dev = pr_to_dev(item);
1423
Andy Grovera3541702015-05-19 14:44:41 -07001424 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001425 return sprintf(page, "SPC_PASSTHROUGH\n");
1426 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1427 return sprintf(page, "SPC2_RESERVATIONS\n");
1428 else
1429 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430}
1431
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001432static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1433 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001435 struct se_device *dev = pr_to_dev(item);
1436
Andy Grovera3541702015-05-19 14:44:41 -07001437 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001438 return 0;
1439
1440 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001441 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442}
1443
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001444static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1445 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001447 struct se_device *dev = pr_to_dev(item);
1448
Andy Grovera3541702015-05-19 14:44:41 -07001449 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 return 0;
1451
1452 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1453}
1454
1455enum {
1456 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1457 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1458 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1459 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1460};
1461
1462static match_table_t tokens = {
1463 {Opt_initiator_fabric, "initiator_fabric=%s"},
1464 {Opt_initiator_node, "initiator_node=%s"},
1465 {Opt_initiator_sid, "initiator_sid=%s"},
1466 {Opt_sa_res_key, "sa_res_key=%s"},
1467 {Opt_res_holder, "res_holder=%d"},
1468 {Opt_res_type, "res_type=%d"},
1469 {Opt_res_scope, "res_scope=%d"},
1470 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001471 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 {Opt_target_fabric, "target_fabric=%s"},
1473 {Opt_target_node, "target_node=%s"},
1474 {Opt_tpgt, "tpgt=%d"},
1475 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001476 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 {Opt_err, NULL}
1478};
1479
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001480static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1481 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001483 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001484 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1485 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001486 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 substring_t args[MAX_OPT_ARGS];
1488 unsigned long long tmp_ll;
1489 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001490 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001492 u16 tpgt = 0;
1493 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494
Andy Grovera3541702015-05-19 14:44:41 -07001495 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001496 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001497 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001498 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001500 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001501 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 " active fabric exports exist\n");
1503 return -EINVAL;
1504 }
1505
1506 opts = kstrdup(page, GFP_KERNEL);
1507 if (!opts)
1508 return -ENOMEM;
1509
1510 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001511 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001512 if (!*ptr)
1513 continue;
1514
1515 token = match_token(ptr, tokens, args);
1516 switch (token) {
1517 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001518 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001519 if (!i_fabric) {
1520 ret = -ENOMEM;
1521 goto out;
1522 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523 break;
1524 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001525 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001526 if (!i_port) {
1527 ret = -ENOMEM;
1528 goto out;
1529 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001530 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001531 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1533 PR_APTPL_MAX_IPORT_LEN);
1534 ret = -EINVAL;
1535 break;
1536 }
1537 break;
1538 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001539 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001540 if (!isid) {
1541 ret = -ENOMEM;
1542 goto out;
1543 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001544 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001545 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001546 "= exceeds PR_REG_ISID_LEN: %d\n",
1547 PR_REG_ISID_LEN);
1548 ret = -EINVAL;
1549 break;
1550 }
1551 break;
1552 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001553 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001555 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 goto out;
1557 }
1558 sa_res_key = (u64)tmp_ll;
1559 break;
1560 /*
1561 * PR APTPL Metadata for Reservation
1562 */
1563 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001564 ret = match_int(args, &arg);
1565 if (ret)
1566 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001567 res_holder = arg;
1568 break;
1569 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001570 ret = match_int(args, &arg);
1571 if (ret)
1572 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 type = (u8)arg;
1574 break;
1575 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001576 ret = match_int(args, &arg);
1577 if (ret)
1578 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 break;
1580 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001581 ret = match_int(args, &arg);
1582 if (ret)
1583 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001584 all_tg_pt = (int)arg;
1585 break;
1586 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001587 ret = match_int(args, &arg);
1588 if (ret)
1589 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001590 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 break;
1592 /*
1593 * PR APTPL Metadata for Target Port
1594 */
1595 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001596 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001597 if (!t_fabric) {
1598 ret = -ENOMEM;
1599 goto out;
1600 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001601 break;
1602 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001603 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001604 if (!t_port) {
1605 ret = -ENOMEM;
1606 goto out;
1607 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001608 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001609 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1611 PR_APTPL_MAX_TPORT_LEN);
1612 ret = -EINVAL;
1613 break;
1614 }
1615 break;
1616 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001617 ret = match_int(args, &arg);
1618 if (ret)
1619 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001620 tpgt = (u16)arg;
1621 break;
1622 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001623 ret = match_int(args, &arg);
1624 if (ret)
1625 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001626 break;
1627 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001628 ret = match_int(args, &arg);
1629 if (ret)
1630 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001631 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001632 break;
1633 default:
1634 break;
1635 }
1636 }
1637
Andy Grover6708bb22011-06-08 10:36:43 -07001638 if (!i_port || !t_port || !sa_res_key) {
1639 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001640 ret = -EINVAL;
1641 goto out;
1642 }
1643
1644 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001645 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001646 " holder\n", type);
1647 ret = -EINVAL;
1648 goto out;
1649 }
1650
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001651 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1653 res_holder, all_tg_pt, type);
1654out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001655 kfree(i_fabric);
1656 kfree(i_port);
1657 kfree(isid);
1658 kfree(t_fabric);
1659 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001660 kfree(orig);
1661 return (ret == 0) ? count : ret;
1662}
1663
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001665CONFIGFS_ATTR_RO(target_pr_, res_holder);
1666CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1667CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1668CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1669CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1670CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1671CONFIGFS_ATTR_RO(target_pr_, res_type);
1672CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1673CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674
1675static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001676 &target_pr_attr_res_holder,
1677 &target_pr_attr_res_pr_all_tgt_pts,
1678 &target_pr_attr_res_pr_generation,
1679 &target_pr_attr_res_pr_holder_tg_port,
1680 &target_pr_attr_res_pr_registered_i_pts,
1681 &target_pr_attr_res_pr_type,
1682 &target_pr_attr_res_type,
1683 &target_pr_attr_res_aptpl_active,
1684 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001685 NULL,
1686};
1687
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001688TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001689
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001690/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001692/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001694static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001695{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001696 return container_of(to_config_group(item), struct se_device, dev_group);
1697}
1698
1699static ssize_t target_dev_info_show(struct config_item *item, char *page)
1700{
1701 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 int bl = 0;
1703 ssize_t read_bytes = 0;
1704
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001705 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001707 read_bytes += dev->transport->show_configfs_dev_params(dev,
1708 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 return read_bytes;
1710}
1711
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001712static ssize_t target_dev_control_store(struct config_item *item,
1713 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001714{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001715 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001716
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001717 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001718}
1719
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001720static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001722 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001724 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 return 0;
1726
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001727 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728}
1729
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001730static ssize_t target_dev_alias_store(struct config_item *item,
1731 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001732{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001733 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001734 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001735 ssize_t read_bytes;
1736
1737 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001738 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1740 SE_DEV_ALIAS_LEN-1);
1741 return -EINVAL;
1742 }
1743
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001744 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001745 if (!read_bytes)
1746 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001747 if (dev->dev_alias[read_bytes - 1] == '\n')
1748 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001749
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001750 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001751
Andy Grover6708bb22011-06-08 10:36:43 -07001752 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001754 config_item_name(&dev->dev_group.cg_item),
1755 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756
1757 return read_bytes;
1758}
1759
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001760static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001762 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001763
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001764 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001765 return 0;
1766
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001767 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768}
1769
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001770static ssize_t target_dev_udev_path_store(struct config_item *item,
1771 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001773 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001774 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001775 ssize_t read_bytes;
1776
1777 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001778 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1780 SE_UDEV_PATH_LEN-1);
1781 return -EINVAL;
1782 }
1783
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001784 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001786 if (!read_bytes)
1787 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001788 if (dev->udev_path[read_bytes - 1] == '\n')
1789 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001790
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001791 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001792
Andy Grover6708bb22011-06-08 10:36:43 -07001793 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001794 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001795 config_item_name(&dev->dev_group.cg_item),
1796 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797
1798 return read_bytes;
1799}
1800
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001801static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001802{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001803 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001804
1805 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1806}
1807
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001808static ssize_t target_dev_enable_store(struct config_item *item,
1809 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001811 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001813 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001814
1815 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001816 if (!ptr) {
1817 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001818 " is \"1\"\n");
1819 return -EINVAL;
1820 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001822 ret = target_configure_device(dev);
1823 if (ret)
1824 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001825 return count;
1826}
1827
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001828static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001830 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 struct config_item *lu_ci;
1832 struct t10_alua_lu_gp *lu_gp;
1833 struct t10_alua_lu_gp_member *lu_gp_mem;
1834 ssize_t len = 0;
1835
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001837 if (!lu_gp_mem)
1838 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839
1840 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1841 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001842 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001843 lu_ci = &lu_gp->lu_gp_group.cg_item;
1844 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1845 config_item_name(lu_ci), lu_gp->lu_gp_id);
1846 }
1847 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1848
1849 return len;
1850}
1851
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001852static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1853 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001855 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001856 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1858 struct t10_alua_lu_gp_member *lu_gp_mem;
1859 unsigned char buf[LU_GROUP_NAME_BUF];
1860 int move = 0;
1861
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001862 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1863 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001864 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001865
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001867 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 return -EINVAL;
1869 }
1870 memset(buf, 0, LU_GROUP_NAME_BUF);
1871 memcpy(buf, page, count);
1872 /*
1873 * Any ALUA logical unit alias besides "NULL" means we will be
1874 * making a new group association.
1875 */
1876 if (strcmp(strstrip(buf), "NULL")) {
1877 /*
1878 * core_alua_get_lu_gp_by_name() will increment reference to
1879 * struct t10_alua_lu_gp. This reference is released with
1880 * core_alua_get_lu_gp_by_name below().
1881 */
1882 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001883 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 return -ENODEV;
1885 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886
1887 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1888 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001889 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890 /*
1891 * Clearing an existing lu_gp association, and replacing
1892 * with NULL
1893 */
Andy Grover6708bb22011-06-08 10:36:43 -07001894 if (!lu_gp_new) {
1895 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001896 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1897 " %hu\n",
1898 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001899 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001900 config_item_name(&lu_gp->lu_gp_group.cg_item),
1901 lu_gp->lu_gp_id);
1902
1903 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1904 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1905
1906 return count;
1907 }
1908 /*
1909 * Removing existing association of lu_gp_mem with lu_gp
1910 */
1911 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1912 move = 1;
1913 }
1914 /*
1915 * Associate lu_gp_mem with lu_gp_new.
1916 */
1917 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1918 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1919
Andy Grover6708bb22011-06-08 10:36:43 -07001920 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 " core/alua/lu_gps/%s, ID: %hu\n",
1922 (move) ? "Moving" : "Adding",
1923 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001924 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1926 lu_gp_new->lu_gp_id);
1927
1928 core_alua_put_lu_gp_from_name(lu_gp_new);
1929 return count;
1930}
1931
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001932static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001933{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001934 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001935 struct t10_alua_lba_map *map;
1936 struct t10_alua_lba_map_member *mem;
1937 char *b = page;
1938 int bl = 0;
1939 char state;
1940
1941 spin_lock(&dev->t10_alua.lba_map_lock);
1942 if (!list_empty(&dev->t10_alua.lba_map_list))
1943 bl += sprintf(b + bl, "%u %u\n",
1944 dev->t10_alua.lba_map_segment_size,
1945 dev->t10_alua.lba_map_segment_multiplier);
1946 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1947 bl += sprintf(b + bl, "%llu %llu",
1948 map->lba_map_first_lba, map->lba_map_last_lba);
1949 list_for_each_entry(mem, &map->lba_map_mem_list,
1950 lba_map_mem_list) {
1951 switch (mem->lba_map_mem_alua_state) {
1952 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1953 state = 'O';
1954 break;
1955 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1956 state = 'A';
1957 break;
1958 case ALUA_ACCESS_STATE_STANDBY:
1959 state = 'S';
1960 break;
1961 case ALUA_ACCESS_STATE_UNAVAILABLE:
1962 state = 'U';
1963 break;
1964 default:
1965 state = '.';
1966 break;
1967 }
1968 bl += sprintf(b + bl, " %d:%c",
1969 mem->lba_map_mem_alua_pg_id, state);
1970 }
1971 bl += sprintf(b + bl, "\n");
1972 }
1973 spin_unlock(&dev->t10_alua.lba_map_lock);
1974 return bl;
1975}
1976
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001977static ssize_t target_dev_lba_map_store(struct config_item *item,
1978 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001979{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001980 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001981 struct t10_alua_lba_map *lba_map = NULL;
1982 struct list_head lba_list;
1983 char *map_entries, *ptr;
1984 char state;
1985 int pg_num = -1, pg;
1986 int ret = 0, num = 0, pg_id, alua_state;
1987 unsigned long start_lba = -1, end_lba = -1;
1988 unsigned long segment_size = -1, segment_mult = -1;
1989
1990 map_entries = kstrdup(page, GFP_KERNEL);
1991 if (!map_entries)
1992 return -ENOMEM;
1993
1994 INIT_LIST_HEAD(&lba_list);
1995 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1996 if (!*ptr)
1997 continue;
1998
1999 if (num == 0) {
2000 if (sscanf(ptr, "%lu %lu\n",
2001 &segment_size, &segment_mult) != 2) {
2002 pr_err("Invalid line %d\n", num);
2003 ret = -EINVAL;
2004 break;
2005 }
2006 num++;
2007 continue;
2008 }
2009 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2010 pr_err("Invalid line %d\n", num);
2011 ret = -EINVAL;
2012 break;
2013 }
2014 ptr = strchr(ptr, ' ');
2015 if (!ptr) {
2016 pr_err("Invalid line %d, missing end lba\n", num);
2017 ret = -EINVAL;
2018 break;
2019 }
2020 ptr++;
2021 ptr = strchr(ptr, ' ');
2022 if (!ptr) {
2023 pr_err("Invalid line %d, missing state definitions\n",
2024 num);
2025 ret = -EINVAL;
2026 break;
2027 }
2028 ptr++;
2029 lba_map = core_alua_allocate_lba_map(&lba_list,
2030 start_lba, end_lba);
2031 if (IS_ERR(lba_map)) {
2032 ret = PTR_ERR(lba_map);
2033 break;
2034 }
2035 pg = 0;
2036 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2037 switch (state) {
2038 case 'O':
2039 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2040 break;
2041 case 'A':
2042 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2043 break;
2044 case 'S':
2045 alua_state = ALUA_ACCESS_STATE_STANDBY;
2046 break;
2047 case 'U':
2048 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2049 break;
2050 default:
2051 pr_err("Invalid ALUA state '%c'\n", state);
2052 ret = -EINVAL;
2053 goto out;
2054 }
2055
2056 ret = core_alua_allocate_lba_map_mem(lba_map,
2057 pg_id, alua_state);
2058 if (ret) {
2059 pr_err("Invalid target descriptor %d:%c "
2060 "at line %d\n",
2061 pg_id, state, num);
2062 break;
2063 }
2064 pg++;
2065 ptr = strchr(ptr, ' ');
2066 if (ptr)
2067 ptr++;
2068 else
2069 break;
2070 }
2071 if (pg_num == -1)
2072 pg_num = pg;
2073 else if (pg != pg_num) {
2074 pr_err("Only %d from %d port groups definitions "
2075 "at line %d\n", pg, pg_num, num);
2076 ret = -EINVAL;
2077 break;
2078 }
2079 num++;
2080 }
2081out:
2082 if (ret) {
2083 core_alua_free_lba_map(&lba_list);
2084 count = ret;
2085 } else
2086 core_alua_set_lba_map(dev, &lba_list,
2087 segment_size, segment_mult);
2088 kfree(map_entries);
2089 return count;
2090}
2091
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002092CONFIGFS_ATTR_RO(target_dev_, info);
2093CONFIGFS_ATTR_WO(target_dev_, control);
2094CONFIGFS_ATTR(target_dev_, alias);
2095CONFIGFS_ATTR(target_dev_, udev_path);
2096CONFIGFS_ATTR(target_dev_, enable);
2097CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2098CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002099
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002100static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002101 &target_dev_attr_info,
2102 &target_dev_attr_control,
2103 &target_dev_attr_alias,
2104 &target_dev_attr_udev_path,
2105 &target_dev_attr_enable,
2106 &target_dev_attr_alua_lu_gp,
2107 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002108 NULL,
2109};
2110
2111static void target_core_dev_release(struct config_item *item)
2112{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002113 struct config_group *dev_cg = to_config_group(item);
2114 struct se_device *dev =
2115 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002116
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002118 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119}
2120
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002121static struct configfs_item_operations target_core_dev_item_ops = {
2122 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002123};
2124
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002125TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002127/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128
2129/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2130
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002131static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002133 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2134 lu_gp_group);
2135}
2136
2137static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2138{
2139 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2140
Andy Grover6708bb22011-06-08 10:36:43 -07002141 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002142 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2144}
2145
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002146static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2147 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002149 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2151 unsigned long lu_gp_id;
2152 int ret;
2153
Jingoo Han57103d72013-07-19 16:22:19 +09002154 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002155 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002156 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002158 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 }
2160 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002161 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002162 " 0x0000ffff\n", lu_gp_id);
2163 return -EINVAL;
2164 }
2165
2166 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2167 if (ret < 0)
2168 return -EINVAL;
2169
Andy Grover6708bb22011-06-08 10:36:43 -07002170 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002171 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2172 config_item_name(&alua_lu_gp_cg->cg_item),
2173 lu_gp->lu_gp_id);
2174
2175 return count;
2176}
2177
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002178static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002180 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181 struct se_device *dev;
2182 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183 struct t10_alua_lu_gp_member *lu_gp_mem;
2184 ssize_t len = 0, cur_len;
2185 unsigned char buf[LU_GROUP_NAME_BUF];
2186
2187 memset(buf, 0, LU_GROUP_NAME_BUF);
2188
2189 spin_lock(&lu_gp->lu_gp_lock);
2190 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2191 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002192 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193
2194 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2195 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002196 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197 cur_len++; /* Extra byte for NULL terminator */
2198
2199 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002200 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201 "_members buffer\n");
2202 break;
2203 }
2204 memcpy(page+len, buf, cur_len);
2205 len += cur_len;
2206 }
2207 spin_unlock(&lu_gp->lu_gp_lock);
2208
2209 return len;
2210}
2211
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002212CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2213CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002214
2215static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002216 &target_lu_gp_attr_lu_gp_id,
2217 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002218 NULL,
2219};
2220
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002221static void target_core_alua_lu_gp_release(struct config_item *item)
2222{
2223 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2224 struct t10_alua_lu_gp, lu_gp_group);
2225
2226 core_alua_free_lu_gp(lu_gp);
2227}
2228
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002230 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231};
2232
2233static struct config_item_type target_core_alua_lu_gp_cit = {
2234 .ct_item_ops = &target_core_alua_lu_gp_ops,
2235 .ct_attrs = target_core_alua_lu_gp_attrs,
2236 .ct_owner = THIS_MODULE,
2237};
2238
2239/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2240
2241/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2242
2243static struct config_group *target_core_alua_create_lu_gp(
2244 struct config_group *group,
2245 const char *name)
2246{
2247 struct t10_alua_lu_gp *lu_gp;
2248 struct config_group *alua_lu_gp_cg = NULL;
2249 struct config_item *alua_lu_gp_ci = NULL;
2250
2251 lu_gp = core_alua_allocate_lu_gp(name, 0);
2252 if (IS_ERR(lu_gp))
2253 return NULL;
2254
2255 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2256 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2257
2258 config_group_init_type_name(alua_lu_gp_cg, name,
2259 &target_core_alua_lu_gp_cit);
2260
Andy Grover6708bb22011-06-08 10:36:43 -07002261 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002262 " Group: core/alua/lu_gps/%s\n",
2263 config_item_name(alua_lu_gp_ci));
2264
2265 return alua_lu_gp_cg;
2266
2267}
2268
2269static void target_core_alua_drop_lu_gp(
2270 struct config_group *group,
2271 struct config_item *item)
2272{
2273 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2274 struct t10_alua_lu_gp, lu_gp_group);
2275
Andy Grover6708bb22011-06-08 10:36:43 -07002276 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2278 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002279 /*
2280 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2281 * -> target_core_alua_lu_gp_release()
2282 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284}
2285
2286static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2287 .make_group = &target_core_alua_create_lu_gp,
2288 .drop_item = &target_core_alua_drop_lu_gp,
2289};
2290
2291static struct config_item_type target_core_alua_lu_gps_cit = {
2292 .ct_item_ops = NULL,
2293 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2294 .ct_owner = THIS_MODULE,
2295};
2296
2297/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2298
2299/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2300
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002301static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002303 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2304 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305}
2306
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002307static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2308 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002310 return sprintf(page, "%d\n",
2311 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2312}
2313
2314static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2315 const char *page, size_t count)
2316{
2317 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002318 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002319 unsigned long tmp;
2320 int new_state, ret;
2321
Andy Grover6708bb22011-06-08 10:36:43 -07002322 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002323 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2325 return -EINVAL;
2326 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002327 if (!(dev->dev_flags & DF_CONFIGURED)) {
2328 pr_err("Unable to set alua_access_state while device is"
2329 " not configured\n");
2330 return -ENODEV;
2331 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002332
Jingoo Han57103d72013-07-19 16:22:19 +09002333 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002334 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002335 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002336 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002337 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002338 }
2339 new_state = (int)tmp;
2340
Hannes Reinecke125d0112013-11-19 09:07:46 +01002341 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2342 pr_err("Unable to process implicit configfs ALUA"
2343 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002344 return -EINVAL;
2345 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002346 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2347 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2348 /* LBA DEPENDENT is only allowed with implicit ALUA */
2349 pr_err("Unable to process implicit configfs ALUA transition"
2350 " while explicit ALUA management is enabled\n");
2351 return -EINVAL;
2352 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002354 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 NULL, NULL, new_state, 0);
2356 return (!ret) ? count : -EINVAL;
2357}
2358
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002359static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2360 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002362 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 return sprintf(page, "%s\n",
2364 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2365}
2366
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002367static ssize_t target_tg_pt_gp_alua_access_status_store(
2368 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002370 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002371 unsigned long tmp;
2372 int new_status, ret;
2373
Andy Grover6708bb22011-06-08 10:36:43 -07002374 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2375 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002376 " valid tg_pt_gp ID: %hu\n",
2377 tg_pt_gp->tg_pt_gp_valid_id);
2378 return -EINVAL;
2379 }
2380
Jingoo Han57103d72013-07-19 16:22:19 +09002381 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002383 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002384 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002385 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386 }
2387 new_status = (int)tmp;
2388
2389 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002390 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2391 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002392 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 new_status);
2394 return -EINVAL;
2395 }
2396
2397 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2398 return count;
2399}
2400
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002401static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2402 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002403{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002404 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405}
2406
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002407static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2408 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002410 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002411}
2412
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002413#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2414static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2415 struct config_item *item, char *p) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002416{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002417 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2418 return sprintf(p, "%d\n", \
2419 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2420} \
2421 \
2422static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2423 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002424{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002425 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002426 unsigned long tmp; \
2427 int ret; \
2428 \
2429 if (!t->tg_pt_gp_valid_id) { \
2430 pr_err("Unable to do set ##_name ALUA state on non" \
2431 " valid tg_pt_gp ID: %hu\n", \
2432 t->tg_pt_gp_valid_id); \
2433 return -EINVAL; \
2434 } \
2435 \
2436 ret = kstrtoul(p, 0, &tmp); \
2437 if (ret < 0) { \
2438 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2439 return -EINVAL; \
2440 } \
2441 if (tmp > 1) { \
2442 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2443 return -EINVAL; \
2444 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002445 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002446 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002447 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002448 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002449 \
2450 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002451}
2452
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002453ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2454ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2455ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2456ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2457ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2458ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2459ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002460
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002461static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2462 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002463{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002464 return sprintf(page, "%d\n",
2465 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002466}
2467
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002468static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2469 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002471 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002472 unsigned long tmp;
2473 int ret;
2474
Jingoo Han57103d72013-07-19 16:22:19 +09002475 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002476 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002477 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002478 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 }
2480
2481 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002482 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002483 " %lu\n", tmp);
2484 return -EINVAL;
2485 }
2486 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2487
2488 return count;
2489}
2490
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002491static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2492 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002493{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002494 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495}
2496
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002497static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2498 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002500 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2501 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002502}
2503
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002504static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2505 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002507 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002508}
2509
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002510static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2511 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002513 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2514 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002515}
2516
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002517static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2518 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002519{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002520 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002521}
2522
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002523static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2524 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002525{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002526 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2527 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002528}
2529
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002530static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2531 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002533 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534}
2535
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002536static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2537 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002539 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540}
2541
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002542static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2543 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002545 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2546
Andy Grover6708bb22011-06-08 10:36:43 -07002547 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002549 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2550}
2551
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002552static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2553 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002555 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2557 unsigned long tg_pt_gp_id;
2558 int ret;
2559
Jingoo Han57103d72013-07-19 16:22:19 +09002560 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002561 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002562 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002564 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002565 }
2566 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002567 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568 " 0x0000ffff\n", tg_pt_gp_id);
2569 return -EINVAL;
2570 }
2571
2572 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2573 if (ret < 0)
2574 return -EINVAL;
2575
Andy Grover6708bb22011-06-08 10:36:43 -07002576 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2578 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2579 tg_pt_gp->tg_pt_gp_id);
2580
2581 return count;
2582}
2583
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002584static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2585 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002587 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 ssize_t len = 0, cur_len;
2590 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2591
2592 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2593
2594 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002595 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2596 lun_tg_pt_gp_link) {
2597 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002598
2599 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002600 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2601 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2602 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603 config_item_name(&lun->lun_group.cg_item));
2604 cur_len++; /* Extra byte for NULL terminator */
2605
2606 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002607 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002608 "_members buffer\n");
2609 break;
2610 }
2611 memcpy(page+len, buf, cur_len);
2612 len += cur_len;
2613 }
2614 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2615
2616 return len;
2617}
2618
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002619CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2620CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2621CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2622CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2623CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2624CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2625CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2626CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2627CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2628CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2629CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2630CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2631CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2632CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2633CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2634CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2635CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636
2637static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002638 &target_tg_pt_gp_attr_alua_access_state,
2639 &target_tg_pt_gp_attr_alua_access_status,
2640 &target_tg_pt_gp_attr_alua_access_type,
2641 &target_tg_pt_gp_attr_alua_support_transitioning,
2642 &target_tg_pt_gp_attr_alua_support_offline,
2643 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2644 &target_tg_pt_gp_attr_alua_support_unavailable,
2645 &target_tg_pt_gp_attr_alua_support_standby,
2646 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2647 &target_tg_pt_gp_attr_alua_support_active_optimized,
2648 &target_tg_pt_gp_attr_alua_write_metadata,
2649 &target_tg_pt_gp_attr_nonop_delay_msecs,
2650 &target_tg_pt_gp_attr_trans_delay_msecs,
2651 &target_tg_pt_gp_attr_implicit_trans_secs,
2652 &target_tg_pt_gp_attr_preferred,
2653 &target_tg_pt_gp_attr_tg_pt_gp_id,
2654 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002655 NULL,
2656};
2657
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002658static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2659{
2660 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2661 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2662
2663 core_alua_free_tg_pt_gp(tg_pt_gp);
2664}
2665
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002667 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668};
2669
2670static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2671 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2672 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2673 .ct_owner = THIS_MODULE,
2674};
2675
2676/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2677
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002678/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002679
2680static struct config_group *target_core_alua_create_tg_pt_gp(
2681 struct config_group *group,
2682 const char *name)
2683{
2684 struct t10_alua *alua = container_of(group, struct t10_alua,
2685 alua_tg_pt_gps_group);
2686 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 struct config_group *alua_tg_pt_gp_cg = NULL;
2688 struct config_item *alua_tg_pt_gp_ci = NULL;
2689
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002690 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002691 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 return NULL;
2693
2694 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2695 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2696
2697 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2698 &target_core_alua_tg_pt_gp_cit);
2699
Andy Grover6708bb22011-06-08 10:36:43 -07002700 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002701 " Group: alua/tg_pt_gps/%s\n",
2702 config_item_name(alua_tg_pt_gp_ci));
2703
2704 return alua_tg_pt_gp_cg;
2705}
2706
2707static void target_core_alua_drop_tg_pt_gp(
2708 struct config_group *group,
2709 struct config_item *item)
2710{
2711 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2712 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2713
Andy Grover6708bb22011-06-08 10:36:43 -07002714 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002715 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2716 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002717 /*
2718 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2719 * -> target_core_alua_tg_pt_gp_release().
2720 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002721 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002722}
2723
2724static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2725 .make_group = &target_core_alua_create_tg_pt_gp,
2726 .drop_item = &target_core_alua_drop_tg_pt_gp,
2727};
2728
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002729TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002730
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002731/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732
2733/* Start functions for struct config_item_type target_core_alua_cit */
2734
2735/*
2736 * target_core_alua_cit is a ConfigFS group that lives under
2737 * /sys/kernel/config/target/core/alua. There are default groups
2738 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2739 * target_core_alua_cit in target_core_init_configfs() below.
2740 */
2741static struct config_item_type target_core_alua_cit = {
2742 .ct_item_ops = NULL,
2743 .ct_attrs = NULL,
2744 .ct_owner = THIS_MODULE,
2745};
2746
2747/* End functions for struct config_item_type target_core_alua_cit */
2748
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002749/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002750
2751static struct config_group *target_core_stat_mkdir(
2752 struct config_group *group,
2753 const char *name)
2754{
2755 return ERR_PTR(-ENOSYS);
2756}
2757
2758static void target_core_stat_rmdir(
2759 struct config_group *group,
2760 struct config_item *item)
2761{
2762 return;
2763}
2764
2765static struct configfs_group_operations target_core_stat_group_ops = {
2766 .make_group = &target_core_stat_mkdir,
2767 .drop_item = &target_core_stat_rmdir,
2768};
2769
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002770TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002771
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002772/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002773
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774/* Start functions for struct config_item_type target_core_hba_cit */
2775
2776static struct config_group *target_core_make_subdev(
2777 struct config_group *group,
2778 const char *name)
2779{
2780 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781 struct config_item *hba_ci = &group->cg_item;
2782 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002783 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002784 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002786 struct config_group *dev_stat_grp = NULL;
2787 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002789 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2790 if (ret)
2791 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002793 dev = target_alloc_device(hba, name);
2794 if (!dev)
2795 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002797 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002799 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002800 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002801 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002802 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002803
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002804 config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002805 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002806 &tb->tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002807 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002808 &tb->tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002809 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002810 &tb->tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002811 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002812 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002813 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002814 "statistics", &tb->tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002815
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002816 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2817 dev_cg->default_groups[1] = &dev->dev_pr_group;
2818 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2819 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2820 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002821 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002823 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002825 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002826 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002827 goto out_free_dev_cg_default_groups;
2828 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002829
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002830 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002831 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002833 if (!tg_pt_gp_cg->default_groups) {
2834 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002836 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 }
2838
2839 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2840 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2841 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2842 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002843 /*
2844 * Add core/$HBA/$DEV/statistics/ default groups
2845 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002846 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002847 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002848 GFP_KERNEL);
2849 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002850 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002851 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002852 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002853 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854
2855 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002856 return dev_cg;
2857
2858out_free_tg_pt_gp_cg_default_groups:
2859 kfree(tg_pt_gp_cg->default_groups);
2860out_free_tg_pt_gp:
2861 core_alua_free_tg_pt_gp(tg_pt_gp);
2862out_free_dev_cg_default_groups:
2863 kfree(dev_cg->default_groups);
2864out_free_device:
2865 target_free_device(dev);
2866out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002868 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869}
2870
2871static void target_core_drop_subdev(
2872 struct config_group *group,
2873 struct config_item *item)
2874{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002875 struct config_group *dev_cg = to_config_group(item);
2876 struct se_device *dev =
2877 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002878 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002880 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002881 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002883 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002885 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002887 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002888 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2889 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2890 dev_stat_grp->default_groups[i] = NULL;
2891 config_item_put(df_item);
2892 }
2893 kfree(dev_stat_grp->default_groups);
2894
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002895 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002896 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2897 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2898 tg_pt_gp_cg->default_groups[i] = NULL;
2899 config_item_put(df_item);
2900 }
2901 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002902 /*
2903 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2904 * directly from target_core_alua_tg_pt_gp_release().
2905 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002906 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 for (i = 0; dev_cg->default_groups[i]; i++) {
2909 df_item = &dev_cg->default_groups[i]->cg_item;
2910 dev_cg->default_groups[i] = NULL;
2911 config_item_put(df_item);
2912 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002913 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002914 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002916 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918}
2919
2920static struct configfs_group_operations target_core_hba_group_ops = {
2921 .make_group = target_core_make_subdev,
2922 .drop_item = target_core_drop_subdev,
2923};
2924
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002926static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002928 return container_of(to_config_group(item), struct se_hba, hba_group);
2929}
2930
2931static ssize_t target_hba_info_show(struct config_item *item, char *page)
2932{
2933 struct se_hba *hba = to_hba(item);
2934
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002936 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002937 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938}
2939
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002940static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002941{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002942 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 int hba_mode = 0;
2944
2945 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2946 hba_mode = 1;
2947
2948 return sprintf(page, "%d\n", hba_mode);
2949}
2950
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002951static ssize_t target_hba_mode_store(struct config_item *item,
2952 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002954 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955 unsigned long mode_flag;
2956 int ret;
2957
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002958 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959 return -EINVAL;
2960
Jingoo Han57103d72013-07-19 16:22:19 +09002961 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002963 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002964 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 }
2966
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002967 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002968 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002969 return -EINVAL;
2970 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002971
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002972 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002973 if (ret < 0)
2974 return -EINVAL;
2975 if (ret > 0)
2976 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2977 else if (ret == 0)
2978 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2979
2980 return count;
2981}
2982
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002983CONFIGFS_ATTR_RO(target_, hba_info);
2984CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002985
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002986static void target_core_hba_release(struct config_item *item)
2987{
2988 struct se_hba *hba = container_of(to_config_group(item),
2989 struct se_hba, hba_group);
2990 core_delete_hba(hba);
2991}
2992
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002994 &target_attr_hba_info,
2995 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 NULL,
2997};
2998
2999static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003000 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001};
3002
3003static struct config_item_type target_core_hba_cit = {
3004 .ct_item_ops = &target_core_hba_item_ops,
3005 .ct_group_ops = &target_core_hba_group_ops,
3006 .ct_attrs = target_core_hba_attrs,
3007 .ct_owner = THIS_MODULE,
3008};
3009
3010static struct config_group *target_core_call_addhbatotarget(
3011 struct config_group *group,
3012 const char *name)
3013{
3014 char *se_plugin_str, *str, *str2;
3015 struct se_hba *hba;
3016 char buf[TARGET_CORE_NAME_MAX_LEN];
3017 unsigned long plugin_dep_id = 0;
3018 int ret;
3019
3020 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003021 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003022 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003023 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3024 TARGET_CORE_NAME_MAX_LEN);
3025 return ERR_PTR(-ENAMETOOLONG);
3026 }
3027 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3028
3029 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003030 if (!str) {
3031 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 return ERR_PTR(-EINVAL);
3033 }
3034 se_plugin_str = buf;
3035 /*
3036 * Special case for subsystem plugins that have "_" in their names.
3037 * Namely rd_direct and rd_mcp..
3038 */
3039 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003040 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003041 *str2 = '\0'; /* Terminate for *se_plugin_str */
3042 str2++; /* Skip to start of plugin dependent ID */
3043 str = str2;
3044 } else {
3045 *str = '\0'; /* Terminate for *se_plugin_str */
3046 str++; /* Skip to start of plugin dependent ID */
3047 }
3048
Jingoo Han57103d72013-07-19 16:22:19 +09003049 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003050 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003051 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003052 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003053 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 }
3055 /*
3056 * Load up TCM subsystem plugins if they have not already been loaded.
3057 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003058 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059
3060 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3061 if (IS_ERR(hba))
3062 return ERR_CAST(hba);
3063
3064 config_group_init_type_name(&hba->hba_group, name,
3065 &target_core_hba_cit);
3066
3067 return &hba->hba_group;
3068}
3069
3070static void target_core_call_delhbafromtarget(
3071 struct config_group *group,
3072 struct config_item *item)
3073{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003074 /*
3075 * core_delete_hba() is called from target_core_hba_item_ops->release()
3076 * -> target_core_hba_release()
3077 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003079}
3080
3081static struct configfs_group_operations target_core_group_ops = {
3082 .make_group = target_core_call_addhbatotarget,
3083 .drop_item = target_core_call_delhbafromtarget,
3084};
3085
3086static struct config_item_type target_core_cit = {
3087 .ct_item_ops = NULL,
3088 .ct_group_ops = &target_core_group_ops,
3089 .ct_attrs = NULL,
3090 .ct_owner = THIS_MODULE,
3091};
3092
3093/* Stop functions for struct config_item_type target_core_hba_cit */
3094
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003095void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003096{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003097 target_core_setup_dev_cit(tb);
3098 target_core_setup_dev_attrib_cit(tb);
3099 target_core_setup_dev_pr_cit(tb);
3100 target_core_setup_dev_wwn_cit(tb);
3101 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3102 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003103}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003104
Axel Lin54550fa2011-03-14 04:06:09 -07003105static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106{
3107 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3108 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003109 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003110 struct t10_alua_lu_gp *lu_gp;
3111 int ret;
3112
Andy Grover6708bb22011-06-08 10:36:43 -07003113 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003114 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3115 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3116
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 config_group_init(&subsys->su_group);
3118 mutex_init(&subsys->su_mutex);
3119
Andy Grovere3d6f902011-07-19 08:55:10 +00003120 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003122 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 /*
3124 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3125 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3126 */
3127 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003128 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003129 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003130 if (!target_cg->default_groups) {
3131 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003132 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003133 goto out_global;
3134 }
3135
Andy Grovere3d6f902011-07-19 08:55:10 +00003136 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003137 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003138 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 target_cg->default_groups[1] = NULL;
3140 /*
3141 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3142 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003143 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003144 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003145 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003146 if (!hba_cg->default_groups) {
3147 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003148 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 goto out_global;
3150 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003151 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003153 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 hba_cg->default_groups[1] = NULL;
3155 /*
3156 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3157 * groups under /sys/kernel/config/target/core/alua/
3158 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003159 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003160 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003162 if (!alua_cg->default_groups) {
3163 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003164 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 goto out_global;
3166 }
3167
Andy Grovere3d6f902011-07-19 08:55:10 +00003168 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003169 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003170 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 alua_cg->default_groups[1] = NULL;
3172 /*
3173 * Add core/alua/lu_gps/default_lu_gp
3174 */
3175 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003176 if (IS_ERR(lu_gp)) {
3177 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003178 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003179 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180
Andy Grovere3d6f902011-07-19 08:55:10 +00003181 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003182 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003184 if (!lu_gp_cg->default_groups) {
3185 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003186 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 goto out_global;
3188 }
3189
3190 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3191 &target_core_alua_lu_gp_cit);
3192 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3193 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003194 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003195 /*
3196 * Register the target_core_mod subsystem with configfs.
3197 */
3198 ret = configfs_register_subsystem(subsys);
3199 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 ret, subsys->su_group.cg_item.ci_namebuf);
3202 goto out_global;
3203 }
Andy Grover6708bb22011-06-08 10:36:43 -07003204 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003205 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3207 /*
3208 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3209 */
3210 ret = rd_module_init();
3211 if (ret < 0)
3212 goto out;
3213
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003214 ret = core_dev_setup_virtual_lun0();
3215 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216 goto out;
3217
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003218 ret = target_xcopy_setup_pt();
3219 if (ret < 0)
3220 goto out;
3221
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003222 return 0;
3223
3224out:
3225 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003226 core_dev_release_virtual_lun0();
3227 rd_module_exit();
3228out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003229 if (default_lu_gp) {
3230 core_alua_free_lu_gp(default_lu_gp);
3231 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 }
3233 if (lu_gp_cg)
3234 kfree(lu_gp_cg->default_groups);
3235 if (alua_cg)
3236 kfree(alua_cg->default_groups);
3237 if (hba_cg)
3238 kfree(hba_cg->default_groups);
3239 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003240 release_se_kmem_caches();
3241 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242}
3243
Axel Lin54550fa2011-03-14 04:06:09 -07003244static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003246 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3247 struct config_item *item;
3248 int i;
3249
Andy Grovere3d6f902011-07-19 08:55:10 +00003250 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3252 item = &lu_gp_cg->default_groups[i]->cg_item;
3253 lu_gp_cg->default_groups[i] = NULL;
3254 config_item_put(item);
3255 }
3256 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003257 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258
Andy Grovere3d6f902011-07-19 08:55:10 +00003259 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260 for (i = 0; alua_cg->default_groups[i]; i++) {
3261 item = &alua_cg->default_groups[i]->cg_item;
3262 alua_cg->default_groups[i] = NULL;
3263 config_item_put(item);
3264 }
3265 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003266 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003267
Andy Grovere3d6f902011-07-19 08:55:10 +00003268 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 for (i = 0; hba_cg->default_groups[i]; i++) {
3270 item = &hba_cg->default_groups[i]->cg_item;
3271 hba_cg->default_groups[i] = NULL;
3272 config_item_put(item);
3273 }
3274 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003275 hba_cg->default_groups = NULL;
3276 /*
3277 * We expect subsys->su_group.default_groups to be released
3278 * by configfs subsystem provider logic..
3279 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003280 configfs_unregister_subsystem(&target_core_fabrics);
3281 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282
Andy Grovere3d6f902011-07-19 08:55:10 +00003283 core_alua_free_lu_gp(default_lu_gp);
3284 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003285
Andy Grover6708bb22011-06-08 10:36:43 -07003286 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003287 " Infrastructure\n");
3288
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289 core_dev_release_virtual_lun0();
3290 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003291 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003292 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003293}
3294
3295MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3296MODULE_AUTHOR("nab@Linux-iSCSI.org");
3297MODULE_LICENSE("GPL");
3298
3299module_init(target_core_init_configfs);
3300module_exit(target_core_exit_configfs);