blob: 86caeb26f99609a114583f282fee475fee98c967 [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#include <target/target_core_fabric_configfs.h>
44#include <target/target_core_configfs.h>
45#include <target/configfs_macros.h>
46
Christoph Hellwige26d99a2011-11-14 12:30:30 -050047#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080048#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_pr.h"
50#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070051#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080052
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080053#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
54static void target_core_setup_##_name##_cit(struct se_subsystem_api *sa) \
55{ \
56 struct target_backend_cits *tbc = &sa->tb_cits; \
57 struct config_item_type *cit = &tbc->tb_##_name##_cit; \
58 \
59 cit->ct_item_ops = _item_ops; \
60 cit->ct_group_ops = _group_ops; \
61 cit->ct_attrs = _attrs; \
62 cit->ct_owner = sa->owner; \
63 pr_debug("Setup generic %s\n", __stringify(_name)); \
64}
65
Andy Grovere3d6f902011-07-19 08:55:10 +000066extern struct t10_alua_lu_gp *default_lu_gp;
67
Roland Dreierd0f474e2012-01-12 10:41:18 -080068static LIST_HEAD(g_tf_list);
69static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080070
71struct target_core_configfs_attribute {
72 struct configfs_attribute attr;
73 ssize_t (*show)(void *, char *);
74 ssize_t (*store)(void *, const char *, size_t);
75};
76
Andy Grovere3d6f902011-07-19 08:55:10 +000077static struct config_group target_core_hbagroup;
78static struct config_group alua_group;
79static struct config_group alua_lu_gps_group;
80
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081static inline struct se_hba *
82item_to_hba(struct config_item *item)
83{
84 return container_of(to_config_group(item), struct se_hba, hba_group);
85}
86
87/*
88 * Attributes for /sys/kernel/config/target/
89 */
90static ssize_t target_core_attr_show(struct config_item *item,
91 struct configfs_attribute *attr,
92 char *page)
93{
94 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
95 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
96 utsname()->sysname, utsname()->machine);
97}
98
99static struct configfs_item_operations target_core_fabric_item_ops = {
100 .show_attribute = target_core_attr_show,
101};
102
103static struct configfs_attribute target_core_item_attr_version = {
104 .ca_owner = THIS_MODULE,
105 .ca_name = "version",
106 .ca_mode = S_IRUGO,
107};
108
109static struct target_fabric_configfs *target_core_get_fabric(
110 const char *name)
111{
112 struct target_fabric_configfs *tf;
113
Andy Grover6708bb22011-06-08 10:36:43 -0700114 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800115 return NULL;
116
117 mutex_lock(&g_tf_lock);
118 list_for_each_entry(tf, &g_tf_list, tf_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700119 if (!strcmp(tf->tf_name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 atomic_inc(&tf->tf_access_cnt);
121 mutex_unlock(&g_tf_lock);
122 return tf;
123 }
124 }
125 mutex_unlock(&g_tf_lock);
126
127 return NULL;
128}
129
130/*
131 * Called from struct target_core_group_ops->make_group()
132 */
133static struct config_group *target_core_register_fabric(
134 struct config_group *group,
135 const char *name)
136{
137 struct target_fabric_configfs *tf;
138 int ret;
139
Andy Grover6708bb22011-06-08 10:36:43 -0700140 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142
143 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700144 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700145 pr_debug("target_core_register_fabric() trying autoload for %s\n",
146 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800147
148 /*
149 * Below are some hardcoded request_module() calls to automatically
150 * local fabric modules when the following is called:
151 *
152 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
153 *
154 * Note that this does not limit which TCM fabric module can be
155 * registered, but simply provids auto loading logic for modules with
156 * mkdir(2) system calls with known TCM fabric modules.
157 */
158
159 if (!strncmp(name, "iscsi", 5)) {
160 /*
161 * Automatically load the LIO Target fabric module when the
162 * following is called:
163 *
164 * mkdir -p $CONFIGFS/target/iscsi
165 */
166 ret = request_module("iscsi_target_mod");
167 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700168 pr_debug("request_module() failed for"
169 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800170 return ERR_PTR(-EINVAL);
171 }
172 } else if (!strncmp(name, "loopback", 8)) {
173 /*
174 * Automatically load the tcm_loop fabric module when the
175 * following is called:
176 *
177 * mkdir -p $CONFIGFS/target/loopback
178 */
179 ret = request_module("tcm_loop");
180 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700181 pr_debug("request_module() failed for"
182 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800183 return ERR_PTR(-EINVAL);
184 }
185 }
186
187 tf = target_core_get_fabric(name);
188 }
189
190 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700191 pr_debug("target_core_get_fabric() failed for %s\n",
192 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193 return ERR_PTR(-EINVAL);
194 }
Andy Grover6708bb22011-06-08 10:36:43 -0700195 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196 " %s\n", tf->tf_name);
197 /*
198 * On a successful target_core_get_fabric() look, the returned
199 * struct target_fabric_configfs *tf will contain a usage reference.
200 */
Andy Grover6708bb22011-06-08 10:36:43 -0700201 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Andy Groverd80e224d2013-10-09 11:05:56 -0700202 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203
204 tf->tf_group.default_groups = tf->tf_default_groups;
205 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
206 tf->tf_group.default_groups[1] = NULL;
207
208 config_group_init_type_name(&tf->tf_group, name,
Andy Groverd80e224d2013-10-09 11:05:56 -0700209 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800210 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Andy Groverd80e224d2013-10-09 11:05:56 -0700211 &tf->tf_cit_tmpl.tfc_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212
Andy Grover6708bb22011-06-08 10:36:43 -0700213 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217 " for %s\n", name);
218
219 return &tf->tf_group;
220}
221
222/*
223 * Called from struct target_core_group_ops->drop_item()
224 */
225static void target_core_deregister_fabric(
226 struct config_group *group,
227 struct config_item *item)
228{
229 struct target_fabric_configfs *tf = container_of(
230 to_config_group(item), struct target_fabric_configfs, tf_group);
231 struct config_group *tf_group;
232 struct config_item *df_item;
233 int i;
234
Andy Grover6708bb22011-06-08 10:36:43 -0700235 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800236 " tf list\n", config_item_name(item));
237
Andy Grover6708bb22011-06-08 10:36:43 -0700238 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 " %s\n", tf->tf_name);
240 atomic_dec(&tf->tf_access_cnt);
241
Andy Grover6708bb22011-06-08 10:36:43 -0700242 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 " tf->tf_fabric for %s\n", tf->tf_name);
244 tf->tf_fabric = NULL;
245
Andy Grover6708bb22011-06-08 10:36:43 -0700246 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247 " %s\n", config_item_name(item));
248
249 tf_group = &tf->tf_group;
250 for (i = 0; tf_group->default_groups[i]; i++) {
251 df_item = &tf_group->default_groups[i]->cg_item;
252 tf_group->default_groups[i] = NULL;
253 config_item_put(df_item);
254 }
255 config_item_put(item);
256}
257
258static struct configfs_group_operations target_core_fabric_group_ops = {
259 .make_group = &target_core_register_fabric,
260 .drop_item = &target_core_deregister_fabric,
261};
262
263/*
264 * All item attributes appearing in /sys/kernel/target/ appear here.
265 */
266static struct configfs_attribute *target_core_fabric_item_attrs[] = {
267 &target_core_item_attr_version,
268 NULL,
269};
270
271/*
272 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
273 */
274static struct config_item_type target_core_fabrics_item = {
275 .ct_item_ops = &target_core_fabric_item_ops,
276 .ct_group_ops = &target_core_fabric_group_ops,
277 .ct_attrs = target_core_fabric_item_attrs,
278 .ct_owner = THIS_MODULE,
279};
280
281static struct configfs_subsystem target_core_fabrics = {
282 .su_group = {
283 .cg_item = {
284 .ci_namebuf = "target",
285 .ci_type = &target_core_fabrics_item,
286 },
287 },
288};
289
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200290int target_depend_item(struct config_item *item)
291{
292 return configfs_depend_item(&target_core_fabrics, item);
293}
294EXPORT_SYMBOL(target_depend_item);
295
296void target_undepend_item(struct config_item *item)
297{
298 return configfs_undepend_item(&target_core_fabrics, item);
299}
300EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301
302/*##############################################################################
303// Start functions called by external Target Fabrics Modules
304//############################################################################*/
305
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200306static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200308 if (!tfo->name) {
309 pr_err("Missing tfo->name\n");
310 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200312 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700313 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200314 "_NAME_SIZE\n", tfo->name);
315 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316 }
Andy Grover6708bb22011-06-08 10:36:43 -0700317 if (!tfo->get_fabric_name) {
318 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800319 return -EINVAL;
320 }
Andy Grover6708bb22011-06-08 10:36:43 -0700321 if (!tfo->tpg_get_wwn) {
322 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 return -EINVAL;
324 }
Andy Grover6708bb22011-06-08 10:36:43 -0700325 if (!tfo->tpg_get_tag) {
326 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327 return -EINVAL;
328 }
Andy Grover6708bb22011-06-08 10:36:43 -0700329 if (!tfo->tpg_check_demo_mode) {
330 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800331 return -EINVAL;
332 }
Andy Grover6708bb22011-06-08 10:36:43 -0700333 if (!tfo->tpg_check_demo_mode_cache) {
334 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 return -EINVAL;
336 }
Andy Grover6708bb22011-06-08 10:36:43 -0700337 if (!tfo->tpg_check_demo_mode_write_protect) {
338 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 return -EINVAL;
340 }
Andy Grover6708bb22011-06-08 10:36:43 -0700341 if (!tfo->tpg_check_prod_mode_write_protect) {
342 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343 return -EINVAL;
344 }
Andy Grover6708bb22011-06-08 10:36:43 -0700345 if (!tfo->tpg_get_inst_index) {
346 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347 return -EINVAL;
348 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400349 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700350 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800351 return -EINVAL;
352 }
Andy Grover6708bb22011-06-08 10:36:43 -0700353 if (!tfo->shutdown_session) {
354 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355 return -EINVAL;
356 }
Andy Grover6708bb22011-06-08 10:36:43 -0700357 if (!tfo->close_session) {
358 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800359 return -EINVAL;
360 }
Andy Grover6708bb22011-06-08 10:36:43 -0700361 if (!tfo->sess_get_index) {
362 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363 return -EINVAL;
364 }
Andy Grover6708bb22011-06-08 10:36:43 -0700365 if (!tfo->write_pending) {
366 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800367 return -EINVAL;
368 }
Andy Grover6708bb22011-06-08 10:36:43 -0700369 if (!tfo->write_pending_status) {
370 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 return -EINVAL;
372 }
Andy Grover6708bb22011-06-08 10:36:43 -0700373 if (!tfo->set_default_node_attributes) {
374 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 return -EINVAL;
376 }
Andy Grover6708bb22011-06-08 10:36:43 -0700377 if (!tfo->get_cmd_state) {
378 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 return -EINVAL;
380 }
Andy Grover6708bb22011-06-08 10:36:43 -0700381 if (!tfo->queue_data_in) {
382 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 return -EINVAL;
384 }
Andy Grover6708bb22011-06-08 10:36:43 -0700385 if (!tfo->queue_status) {
386 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -EINVAL;
388 }
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!tfo->queue_tm_rsp) {
390 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return -EINVAL;
392 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700393 if (!tfo->aborted_task) {
394 pr_err("Missing tfo->aborted_task()\n");
395 return -EINVAL;
396 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 /*
398 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
399 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
400 * target_core_fabric_configfs.c WWN+TPG group context code.
401 */
Andy Grover6708bb22011-06-08 10:36:43 -0700402 if (!tfo->fabric_make_wwn) {
403 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 return -EINVAL;
405 }
Andy Grover6708bb22011-06-08 10:36:43 -0700406 if (!tfo->fabric_drop_wwn) {
407 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 return -EINVAL;
409 }
Andy Grover6708bb22011-06-08 10:36:43 -0700410 if (!tfo->fabric_make_tpg) {
411 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 return -EINVAL;
413 }
Andy Grover6708bb22011-06-08 10:36:43 -0700414 if (!tfo->fabric_drop_tpg) {
415 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416 return -EINVAL;
417 }
418
419 return 0;
420}
421
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200422int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200424 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 int ret;
426
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200427 ret = target_fabric_tf_ops_check(fo);
428 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 return ret;
430
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200431 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700432 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200433 pr_err("%s: could not allocate memory!\n", __func__);
434 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200436
437 INIT_LIST_HEAD(&tf->tf_list);
438 atomic_set(&tf->tf_access_cnt, 0);
439
440 /*
441 * Setup the default generic struct config_item_type's (cits) in
442 * struct target_fabric_configfs->tf_cit_tmpl
443 */
444 tf->tf_module = fo->module;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200445 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", fo->name);
446
447 tf->tf_ops = *fo;
448 target_fabric_setup_cits(tf);
449
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200451 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800452 mutex_unlock(&g_tf_lock);
453
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200454 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200456EXPORT_SYMBOL(target_register_template);
457
458void target_unregister_template(const struct target_core_fabric_ops *fo)
459{
460 struct target_fabric_configfs *t;
461
462 mutex_lock(&g_tf_lock);
463 list_for_each_entry(t, &g_tf_list, tf_list) {
464 if (!strcmp(t->tf_name, fo->name)) {
465 BUG_ON(atomic_read(&t->tf_access_cnt));
466 list_del(&t->tf_list);
467 kfree(t);
468 break;
469 }
470 }
471 mutex_unlock(&g_tf_lock);
472}
473EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474
475/*##############################################################################
476// Stop functions called by external Target Fabrics Modules
477//############################################################################*/
478
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800479/* Start functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
483
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484static struct configfs_item_operations target_core_dev_attrib_ops = {
485 .show_attribute = target_core_dev_attrib_attr_show,
486 .store_attribute = target_core_dev_attrib_attr_store,
487};
488
Nicholas Bellinger43cf2082014-11-28 05:34:39 +0000489TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800491/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800493/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494
495CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
496#define SE_DEV_WWN_ATTR(_name, _mode) \
497static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
498 __CONFIGFS_EATTR(_name, _mode, \
499 target_core_dev_wwn_show_attr_##_name, \
500 target_core_dev_wwn_store_attr_##_name);
501
502#define SE_DEV_WWN_ATTR_RO(_name); \
503do { \
504 static struct target_core_dev_wwn_attribute \
505 target_core_dev_wwn_##_name = \
506 __CONFIGFS_EATTR_RO(_name, \
507 target_core_dev_wwn_show_attr_##_name); \
508} while (0);
509
510/*
511 * VPD page 0x80 Unit serial
512 */
513static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
514 struct t10_wwn *t10_wwn,
515 char *page)
516{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
518 &t10_wwn->unit_serial[0]);
519}
520
521static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
522 struct t10_wwn *t10_wwn,
523 const char *page,
524 size_t count)
525{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400526 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
528
529 /*
530 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
531 * from the struct scsi_device level firmware, do not allow
532 * VPD Unit Serial to be emulated.
533 *
534 * Note this struct scsi_device could also be emulating VPD
535 * information from its drivers/scsi LLD. But for now we assume
536 * it is doing 'the right thing' wrt a world wide unique
537 * VPD Unit Serial Number that OS dependent multipath can depend on.
538 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400539 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700540 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541 " Unit Serial, ignoring request\n");
542 return -EOPNOTSUPP;
543 }
544
Dan Carpenter60d645a2011-06-15 10:03:05 -0700545 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700546 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
548 return -EOVERFLOW;
549 }
550 /*
551 * Check to see if any active $FABRIC_MOD exports exist. If they
552 * do exist, fail here as changing this information on the fly
553 * (underneath the initiator side OS dependent multipath code)
554 * could cause negative effects.
555 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400556 if (dev->export_count) {
557 pr_err("Unable to set VPD Unit Serial while"
558 " active %d $FABRIC_MOD exports exist\n",
559 dev->export_count);
560 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400562
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800563 /*
564 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
565 *
566 * Also, strip any newline added from the userspace
567 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
568 */
569 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
570 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400571 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800572 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400573 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574
Andy Grover6708bb22011-06-08 10:36:43 -0700575 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400576 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577
578 return count;
579}
580
581SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
582
583/*
584 * VPD page 0x83 Protocol Identifier
585 */
586static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
587 struct t10_wwn *t10_wwn,
588 char *page)
589{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 struct t10_vpd *vpd;
591 unsigned char buf[VPD_TMP_BUF_SIZE];
592 ssize_t len = 0;
593
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800594 memset(buf, 0, VPD_TMP_BUF_SIZE);
595
596 spin_lock(&t10_wwn->t10_vpd_lock);
597 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700598 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599 continue;
600
601 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
602
Andy Grover6708bb22011-06-08 10:36:43 -0700603 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 break;
605
606 len += sprintf(page+len, "%s", buf);
607 }
608 spin_unlock(&t10_wwn->t10_vpd_lock);
609
610 return len;
611}
612
613static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
614 struct t10_wwn *t10_wwn,
615 const char *page,
616 size_t count)
617{
618 return -ENOSYS;
619}
620
621SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
622
623/*
624 * Generic wrapper for dumping VPD identifiers by association.
625 */
626#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
627static ssize_t target_core_dev_wwn_show_attr_##_name( \
628 struct t10_wwn *t10_wwn, \
629 char *page) \
630{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 struct t10_vpd *vpd; \
632 unsigned char buf[VPD_TMP_BUF_SIZE]; \
633 ssize_t len = 0; \
634 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800635 spin_lock(&t10_wwn->t10_vpd_lock); \
636 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
637 if (vpd->association != _assoc) \
638 continue; \
639 \
640 memset(buf, 0, VPD_TMP_BUF_SIZE); \
641 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700642 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 break; \
644 len += sprintf(page+len, "%s", buf); \
645 \
646 memset(buf, 0, VPD_TMP_BUF_SIZE); \
647 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700648 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 break; \
650 len += sprintf(page+len, "%s", buf); \
651 \
652 memset(buf, 0, VPD_TMP_BUF_SIZE); \
653 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700654 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 break; \
656 len += sprintf(page+len, "%s", buf); \
657 } \
658 spin_unlock(&t10_wwn->t10_vpd_lock); \
659 \
660 return len; \
661}
662
663/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700664 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800665 */
666DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
667
668static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
669 struct t10_wwn *t10_wwn,
670 const char *page,
671 size_t count)
672{
673 return -ENOSYS;
674}
675
676SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
677
678/*
679 * VPD page 0x83 Association: Target Port
680 */
681DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
682
683static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
684 struct t10_wwn *t10_wwn,
685 const char *page,
686 size_t count)
687{
688 return -ENOSYS;
689}
690
691SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
692
693/*
694 * VPD page 0x83 Association: SCSI Target Device
695 */
696DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
697
698static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
699 struct t10_wwn *t10_wwn,
700 const char *page,
701 size_t count)
702{
703 return -ENOSYS;
704}
705
706SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
707
708CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
709
710static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
711 &target_core_dev_wwn_vpd_unit_serial.attr,
712 &target_core_dev_wwn_vpd_protocol_identifier.attr,
713 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
714 &target_core_dev_wwn_vpd_assoc_target_port.attr,
715 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
716 NULL,
717};
718
719static struct configfs_item_operations target_core_dev_wwn_ops = {
720 .show_attribute = target_core_dev_wwn_attr_show,
721 .store_attribute = target_core_dev_wwn_attr_store,
722};
723
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800724TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800725
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800726/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800727
Nicholas Bellinger91e2e392014-11-27 14:57:01 -0800728/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400730CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800731#define SE_DEV_PR_ATTR(_name, _mode) \
732static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
733 __CONFIGFS_EATTR(_name, _mode, \
734 target_core_dev_pr_show_attr_##_name, \
735 target_core_dev_pr_store_attr_##_name);
736
737#define SE_DEV_PR_ATTR_RO(_name); \
738static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
739 __CONFIGFS_EATTR_RO(_name, \
740 target_core_dev_pr_show_attr_##_name);
741
Christoph Hellwigd977f432012-10-10 17:37:15 -0400742static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
743 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744{
745 struct se_node_acl *se_nacl;
746 struct t10_pr_registration *pr_reg;
747 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748
749 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
750
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400752 if (!pr_reg)
753 return sprintf(page, "No SPC-3 Reservation holder\n");
754
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -0700756 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757
Christoph Hellwigd977f432012-10-10 17:37:15 -0400758 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000759 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700760 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761}
762
Christoph Hellwigd977f432012-10-10 17:37:15 -0400763static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
764 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800765{
766 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400767 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800768
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800769 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400770 if (se_nacl) {
771 len = sprintf(page,
772 "SPC-2 Reservation: %s Initiator: %s\n",
773 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
774 se_nacl->initiatorname);
775 } else {
776 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800777 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400778 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779}
780
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400781static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
782 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800783{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400784 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800785
Andy Grovera3541702015-05-19 14:44:41 -0700786 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400787 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788
Christoph Hellwigd977f432012-10-10 17:37:15 -0400789 spin_lock(&dev->dev_reservation_lock);
790 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
791 ret = target_core_dev_pr_show_spc2_res(dev, page);
792 else
793 ret = target_core_dev_pr_show_spc3_res(dev, page);
794 spin_unlock(&dev->dev_reservation_lock);
795 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796}
797
798SE_DEV_PR_ATTR_RO(res_holder);
799
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400801 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800803 ssize_t len = 0;
804
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -0400806 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400808 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809 len = sprintf(page, "SPC-3 Reservation: All Target"
810 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400811 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800812 len = sprintf(page, "SPC-3 Reservation: Single"
813 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400814 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800815
Christoph Hellwigd977f432012-10-10 17:37:15 -0400816 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800817 return len;
818}
819
820SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
821
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400823 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400825 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800826}
827
828SE_DEV_PR_ATTR_RO(res_pr_generation);
829
830/*
831 * res_pr_holder_tg_port
832 */
833static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400834 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800835{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 struct se_node_acl *se_nacl;
837 struct se_lun *lun;
838 struct se_portal_group *se_tpg;
839 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200840 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800841 ssize_t len = 0;
842
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 spin_lock(&dev->dev_reservation_lock);
844 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -0700845 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400847 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800848 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400849
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850 se_nacl = pr_reg->pr_reg_nacl;
851 se_tpg = se_nacl->se_tpg;
852 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000853 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854
855 len += sprintf(page+len, "SPC-3 Reservation: %s"
856 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
857 tfo->tpg_get_wwn(se_tpg));
858 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +0900859 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800860 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
861 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
862 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863
Christoph Hellwigd977f432012-10-10 17:37:15 -0400864out_unlock:
865 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 return len;
867}
868
869SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
870
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400872 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200874 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800875 struct t10_pr_registration *pr_reg;
876 unsigned char buf[384];
877 char i_buf[PR_REG_ISID_ID_LEN];
878 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -0700879 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800881 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
882
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400883 spin_lock(&dev->t10_pr.registration_lock);
884 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885 pr_reg_list) {
886
887 memset(buf, 0, 384);
888 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
889 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -0700890 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891 PR_REG_ISID_ID_LEN);
892 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
893 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700894 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800895 pr_reg->pr_res_generation);
896
Andy Grover6708bb22011-06-08 10:36:43 -0700897 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800898 break;
899
900 len += sprintf(page+len, "%s", buf);
901 reg_count++;
902 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400903 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800904
Andy Grover6708bb22011-06-08 10:36:43 -0700905 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906 len += sprintf(page+len, "None\n");
907
908 return len;
909}
910
911SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
912
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400914 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800916 struct t10_pr_registration *pr_reg;
917 ssize_t len = 0;
918
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800919 spin_lock(&dev->dev_reservation_lock);
920 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400921 if (pr_reg) {
922 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
923 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
924 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800925 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800926 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800927
Christoph Hellwigd977f432012-10-10 17:37:15 -0400928 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800929 return len;
930}
931
932SE_DEV_PR_ATTR_RO(res_pr_type);
933
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800934static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400935 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936{
Andy Grovera3541702015-05-19 14:44:41 -0700937 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400938 return sprintf(page, "SPC_PASSTHROUGH\n");
939 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
940 return sprintf(page, "SPC2_RESERVATIONS\n");
941 else
942 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943}
944
945SE_DEV_PR_ATTR_RO(res_type);
946
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400948 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949{
Andy Grovera3541702015-05-19 14:44:41 -0700950 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951 return 0;
952
953 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400954 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800955}
956
957SE_DEV_PR_ATTR_RO(res_aptpl_active);
958
959/*
960 * res_aptpl_metadata
961 */
962static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400963 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800964{
Andy Grovera3541702015-05-19 14:44:41 -0700965 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800966 return 0;
967
968 return sprintf(page, "Ready to process PR APTPL metadata..\n");
969}
970
971enum {
972 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
973 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
974 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
975 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
976};
977
978static match_table_t tokens = {
979 {Opt_initiator_fabric, "initiator_fabric=%s"},
980 {Opt_initiator_node, "initiator_node=%s"},
981 {Opt_initiator_sid, "initiator_sid=%s"},
982 {Opt_sa_res_key, "sa_res_key=%s"},
983 {Opt_res_holder, "res_holder=%d"},
984 {Opt_res_type, "res_type=%d"},
985 {Opt_res_scope, "res_scope=%d"},
986 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
987 {Opt_mapped_lun, "mapped_lun=%d"},
988 {Opt_target_fabric, "target_fabric=%s"},
989 {Opt_target_node, "target_node=%s"},
990 {Opt_tpgt, "tpgt=%d"},
991 {Opt_port_rtpi, "port_rtpi=%d"},
992 {Opt_target_lun, "target_lun=%d"},
993 {Opt_err, NULL}
994};
995
996static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400997 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800998 const char *page,
999 size_t count)
1000{
Jesper Juhl6d180252011-03-14 04:05:56 -07001001 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1002 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001003 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004 substring_t args[MAX_OPT_ARGS];
1005 unsigned long long tmp_ll;
1006 u64 sa_res_key = 0;
1007 u32 mapped_lun = 0, target_lun = 0;
1008 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001009 u16 tpgt = 0;
1010 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011
Andy Grovera3541702015-05-19 14:44:41 -07001012 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001013 return 0;
1014 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015 return 0;
1016
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001017 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001018 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001019 " active fabric exports exist\n");
1020 return -EINVAL;
1021 }
1022
1023 opts = kstrdup(page, GFP_KERNEL);
1024 if (!opts)
1025 return -ENOMEM;
1026
1027 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001028 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029 if (!*ptr)
1030 continue;
1031
1032 token = match_token(ptr, tokens, args);
1033 switch (token) {
1034 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001035 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001036 if (!i_fabric) {
1037 ret = -ENOMEM;
1038 goto out;
1039 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 break;
1041 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001042 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001043 if (!i_port) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001047 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001048 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001049 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1050 PR_APTPL_MAX_IPORT_LEN);
1051 ret = -EINVAL;
1052 break;
1053 }
1054 break;
1055 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001056 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001057 if (!isid) {
1058 ret = -ENOMEM;
1059 goto out;
1060 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001061 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001062 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063 "= exceeds PR_REG_ISID_LEN: %d\n",
1064 PR_REG_ISID_LEN);
1065 ret = -EINVAL;
1066 break;
1067 }
1068 break;
1069 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001070 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001072 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 goto out;
1074 }
1075 sa_res_key = (u64)tmp_ll;
1076 break;
1077 /*
1078 * PR APTPL Metadata for Reservation
1079 */
1080 case Opt_res_holder:
1081 match_int(args, &arg);
1082 res_holder = arg;
1083 break;
1084 case Opt_res_type:
1085 match_int(args, &arg);
1086 type = (u8)arg;
1087 break;
1088 case Opt_res_scope:
1089 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 break;
1091 case Opt_res_all_tg_pt:
1092 match_int(args, &arg);
1093 all_tg_pt = (int)arg;
1094 break;
1095 case Opt_mapped_lun:
1096 match_int(args, &arg);
1097 mapped_lun = (u32)arg;
1098 break;
1099 /*
1100 * PR APTPL Metadata for Target Port
1101 */
1102 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001103 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001104 if (!t_fabric) {
1105 ret = -ENOMEM;
1106 goto out;
1107 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108 break;
1109 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001110 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001111 if (!t_port) {
1112 ret = -ENOMEM;
1113 goto out;
1114 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001115 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001116 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1118 PR_APTPL_MAX_TPORT_LEN);
1119 ret = -EINVAL;
1120 break;
1121 }
1122 break;
1123 case Opt_tpgt:
1124 match_int(args, &arg);
1125 tpgt = (u16)arg;
1126 break;
1127 case Opt_port_rtpi:
1128 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129 break;
1130 case Opt_target_lun:
1131 match_int(args, &arg);
1132 target_lun = (u32)arg;
1133 break;
1134 default:
1135 break;
1136 }
1137 }
1138
Andy Grover6708bb22011-06-08 10:36:43 -07001139 if (!i_port || !t_port || !sa_res_key) {
1140 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141 ret = -EINVAL;
1142 goto out;
1143 }
1144
1145 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001146 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147 " holder\n", type);
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001152 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1154 res_holder, all_tg_pt, type);
1155out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001156 kfree(i_fabric);
1157 kfree(i_port);
1158 kfree(isid);
1159 kfree(t_fabric);
1160 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 kfree(orig);
1162 return (ret == 0) ? count : ret;
1163}
1164
1165SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1166
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001167CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168
1169static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1170 &target_core_dev_pr_res_holder.attr,
1171 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1172 &target_core_dev_pr_res_pr_generation.attr,
1173 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1174 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1175 &target_core_dev_pr_res_pr_type.attr,
1176 &target_core_dev_pr_res_type.attr,
1177 &target_core_dev_pr_res_aptpl_active.attr,
1178 &target_core_dev_pr_res_aptpl_metadata.attr,
1179 NULL,
1180};
1181
1182static struct configfs_item_operations target_core_dev_pr_ops = {
1183 .show_attribute = target_core_dev_pr_attr_show,
1184 .store_attribute = target_core_dev_pr_attr_store,
1185};
1186
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001187TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001189/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001191/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192
1193static ssize_t target_core_show_dev_info(void *p, char *page)
1194{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001195 struct se_device *dev = p;
1196 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197 int bl = 0;
1198 ssize_t read_bytes = 0;
1199
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001200 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001202 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203 return read_bytes;
1204}
1205
1206static struct target_core_configfs_attribute target_core_attr_dev_info = {
1207 .attr = { .ca_owner = THIS_MODULE,
1208 .ca_name = "info",
1209 .ca_mode = S_IRUGO },
1210 .show = target_core_show_dev_info,
1211 .store = NULL,
1212};
1213
1214static ssize_t target_core_store_dev_control(
1215 void *p,
1216 const char *page,
1217 size_t count)
1218{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001219 struct se_device *dev = p;
1220 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001222 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223}
1224
1225static struct target_core_configfs_attribute target_core_attr_dev_control = {
1226 .attr = { .ca_owner = THIS_MODULE,
1227 .ca_name = "control",
1228 .ca_mode = S_IWUSR },
1229 .show = NULL,
1230 .store = target_core_store_dev_control,
1231};
1232
1233static ssize_t target_core_show_dev_alias(void *p, char *page)
1234{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001235 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001237 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238 return 0;
1239
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001240 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241}
1242
1243static ssize_t target_core_store_dev_alias(
1244 void *p,
1245 const char *page,
1246 size_t count)
1247{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001248 struct se_device *dev = p;
1249 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 ssize_t read_bytes;
1251
1252 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001253 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1255 SE_DEV_ALIAS_LEN-1);
1256 return -EINVAL;
1257 }
1258
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001259 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001260 if (!read_bytes)
1261 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001262 if (dev->dev_alias[read_bytes - 1] == '\n')
1263 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001264
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001265 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001266
Andy Grover6708bb22011-06-08 10:36:43 -07001267 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001269 config_item_name(&dev->dev_group.cg_item),
1270 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001271
1272 return read_bytes;
1273}
1274
1275static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1276 .attr = { .ca_owner = THIS_MODULE,
1277 .ca_name = "alias",
1278 .ca_mode = S_IRUGO | S_IWUSR },
1279 .show = target_core_show_dev_alias,
1280 .store = target_core_store_dev_alias,
1281};
1282
1283static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1284{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001285 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001287 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288 return 0;
1289
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001290 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291}
1292
1293static ssize_t target_core_store_dev_udev_path(
1294 void *p,
1295 const char *page,
1296 size_t count)
1297{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001298 struct se_device *dev = p;
1299 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 ssize_t read_bytes;
1301
1302 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001303 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1305 SE_UDEV_PATH_LEN-1);
1306 return -EINVAL;
1307 }
1308
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001309 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001311 if (!read_bytes)
1312 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001313 if (dev->udev_path[read_bytes - 1] == '\n')
1314 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001315
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001316 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001317
Andy Grover6708bb22011-06-08 10:36:43 -07001318 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001320 config_item_name(&dev->dev_group.cg_item),
1321 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001322
1323 return read_bytes;
1324}
1325
1326static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1327 .attr = { .ca_owner = THIS_MODULE,
1328 .ca_name = "udev_path",
1329 .ca_mode = S_IRUGO | S_IWUSR },
1330 .show = target_core_show_dev_udev_path,
1331 .store = target_core_store_dev_udev_path,
1332};
1333
Andy Grover64146db2013-04-30 11:59:15 -07001334static ssize_t target_core_show_dev_enable(void *p, char *page)
1335{
1336 struct se_device *dev = p;
1337
1338 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1339}
1340
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341static ssize_t target_core_store_dev_enable(
1342 void *p,
1343 const char *page,
1344 size_t count)
1345{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001346 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001348 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349
1350 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001351 if (!ptr) {
1352 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 " is \"1\"\n");
1354 return -EINVAL;
1355 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001357 ret = target_configure_device(dev);
1358 if (ret)
1359 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001360 return count;
1361}
1362
1363static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1364 .attr = { .ca_owner = THIS_MODULE,
1365 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001366 .ca_mode = S_IRUGO | S_IWUSR },
1367 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368 .store = target_core_store_dev_enable,
1369};
1370
1371static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1372{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001373 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 struct config_item *lu_ci;
1375 struct t10_alua_lu_gp *lu_gp;
1376 struct t10_alua_lu_gp_member *lu_gp_mem;
1377 ssize_t len = 0;
1378
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001380 if (!lu_gp_mem)
1381 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001382
1383 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1384 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001385 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386 lu_ci = &lu_gp->lu_gp_group.cg_item;
1387 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1388 config_item_name(lu_ci), lu_gp->lu_gp_id);
1389 }
1390 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1391
1392 return len;
1393}
1394
1395static ssize_t target_core_store_alua_lu_gp(
1396 void *p,
1397 const char *page,
1398 size_t count)
1399{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001400 struct se_device *dev = p;
1401 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1403 struct t10_alua_lu_gp_member *lu_gp_mem;
1404 unsigned char buf[LU_GROUP_NAME_BUF];
1405 int move = 0;
1406
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001407 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1408 if (!lu_gp_mem)
1409 return 0;
1410
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001412 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001413 return -EINVAL;
1414 }
1415 memset(buf, 0, LU_GROUP_NAME_BUF);
1416 memcpy(buf, page, count);
1417 /*
1418 * Any ALUA logical unit alias besides "NULL" means we will be
1419 * making a new group association.
1420 */
1421 if (strcmp(strstrip(buf), "NULL")) {
1422 /*
1423 * core_alua_get_lu_gp_by_name() will increment reference to
1424 * struct t10_alua_lu_gp. This reference is released with
1425 * core_alua_get_lu_gp_by_name below().
1426 */
1427 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001428 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 return -ENODEV;
1430 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431
1432 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1433 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001434 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 /*
1436 * Clearing an existing lu_gp association, and replacing
1437 * with NULL
1438 */
Andy Grover6708bb22011-06-08 10:36:43 -07001439 if (!lu_gp_new) {
1440 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1442 " %hu\n",
1443 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001444 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001445 config_item_name(&lu_gp->lu_gp_group.cg_item),
1446 lu_gp->lu_gp_id);
1447
1448 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1449 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1450
1451 return count;
1452 }
1453 /*
1454 * Removing existing association of lu_gp_mem with lu_gp
1455 */
1456 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1457 move = 1;
1458 }
1459 /*
1460 * Associate lu_gp_mem with lu_gp_new.
1461 */
1462 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1463 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1464
Andy Grover6708bb22011-06-08 10:36:43 -07001465 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 " core/alua/lu_gps/%s, ID: %hu\n",
1467 (move) ? "Moving" : "Adding",
1468 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001469 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1471 lu_gp_new->lu_gp_id);
1472
1473 core_alua_put_lu_gp_from_name(lu_gp_new);
1474 return count;
1475}
1476
1477static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1478 .attr = { .ca_owner = THIS_MODULE,
1479 .ca_name = "alua_lu_gp",
1480 .ca_mode = S_IRUGO | S_IWUSR },
1481 .show = target_core_show_alua_lu_gp,
1482 .store = target_core_store_alua_lu_gp,
1483};
1484
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001485static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1486{
1487 struct se_device *dev = p;
1488 struct t10_alua_lba_map *map;
1489 struct t10_alua_lba_map_member *mem;
1490 char *b = page;
1491 int bl = 0;
1492 char state;
1493
1494 spin_lock(&dev->t10_alua.lba_map_lock);
1495 if (!list_empty(&dev->t10_alua.lba_map_list))
1496 bl += sprintf(b + bl, "%u %u\n",
1497 dev->t10_alua.lba_map_segment_size,
1498 dev->t10_alua.lba_map_segment_multiplier);
1499 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1500 bl += sprintf(b + bl, "%llu %llu",
1501 map->lba_map_first_lba, map->lba_map_last_lba);
1502 list_for_each_entry(mem, &map->lba_map_mem_list,
1503 lba_map_mem_list) {
1504 switch (mem->lba_map_mem_alua_state) {
1505 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1506 state = 'O';
1507 break;
1508 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1509 state = 'A';
1510 break;
1511 case ALUA_ACCESS_STATE_STANDBY:
1512 state = 'S';
1513 break;
1514 case ALUA_ACCESS_STATE_UNAVAILABLE:
1515 state = 'U';
1516 break;
1517 default:
1518 state = '.';
1519 break;
1520 }
1521 bl += sprintf(b + bl, " %d:%c",
1522 mem->lba_map_mem_alua_pg_id, state);
1523 }
1524 bl += sprintf(b + bl, "\n");
1525 }
1526 spin_unlock(&dev->t10_alua.lba_map_lock);
1527 return bl;
1528}
1529
1530static ssize_t target_core_store_dev_lba_map(
1531 void *p,
1532 const char *page,
1533 size_t count)
1534{
1535 struct se_device *dev = p;
1536 struct t10_alua_lba_map *lba_map = NULL;
1537 struct list_head lba_list;
1538 char *map_entries, *ptr;
1539 char state;
1540 int pg_num = -1, pg;
1541 int ret = 0, num = 0, pg_id, alua_state;
1542 unsigned long start_lba = -1, end_lba = -1;
1543 unsigned long segment_size = -1, segment_mult = -1;
1544
1545 map_entries = kstrdup(page, GFP_KERNEL);
1546 if (!map_entries)
1547 return -ENOMEM;
1548
1549 INIT_LIST_HEAD(&lba_list);
1550 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1551 if (!*ptr)
1552 continue;
1553
1554 if (num == 0) {
1555 if (sscanf(ptr, "%lu %lu\n",
1556 &segment_size, &segment_mult) != 2) {
1557 pr_err("Invalid line %d\n", num);
1558 ret = -EINVAL;
1559 break;
1560 }
1561 num++;
1562 continue;
1563 }
1564 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1565 pr_err("Invalid line %d\n", num);
1566 ret = -EINVAL;
1567 break;
1568 }
1569 ptr = strchr(ptr, ' ');
1570 if (!ptr) {
1571 pr_err("Invalid line %d, missing end lba\n", num);
1572 ret = -EINVAL;
1573 break;
1574 }
1575 ptr++;
1576 ptr = strchr(ptr, ' ');
1577 if (!ptr) {
1578 pr_err("Invalid line %d, missing state definitions\n",
1579 num);
1580 ret = -EINVAL;
1581 break;
1582 }
1583 ptr++;
1584 lba_map = core_alua_allocate_lba_map(&lba_list,
1585 start_lba, end_lba);
1586 if (IS_ERR(lba_map)) {
1587 ret = PTR_ERR(lba_map);
1588 break;
1589 }
1590 pg = 0;
1591 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1592 switch (state) {
1593 case 'O':
1594 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1595 break;
1596 case 'A':
1597 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1598 break;
1599 case 'S':
1600 alua_state = ALUA_ACCESS_STATE_STANDBY;
1601 break;
1602 case 'U':
1603 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1604 break;
1605 default:
1606 pr_err("Invalid ALUA state '%c'\n", state);
1607 ret = -EINVAL;
1608 goto out;
1609 }
1610
1611 ret = core_alua_allocate_lba_map_mem(lba_map,
1612 pg_id, alua_state);
1613 if (ret) {
1614 pr_err("Invalid target descriptor %d:%c "
1615 "at line %d\n",
1616 pg_id, state, num);
1617 break;
1618 }
1619 pg++;
1620 ptr = strchr(ptr, ' ');
1621 if (ptr)
1622 ptr++;
1623 else
1624 break;
1625 }
1626 if (pg_num == -1)
1627 pg_num = pg;
1628 else if (pg != pg_num) {
1629 pr_err("Only %d from %d port groups definitions "
1630 "at line %d\n", pg, pg_num, num);
1631 ret = -EINVAL;
1632 break;
1633 }
1634 num++;
1635 }
1636out:
1637 if (ret) {
1638 core_alua_free_lba_map(&lba_list);
1639 count = ret;
1640 } else
1641 core_alua_set_lba_map(dev, &lba_list,
1642 segment_size, segment_mult);
1643 kfree(map_entries);
1644 return count;
1645}
1646
1647static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1648 .attr = { .ca_owner = THIS_MODULE,
1649 .ca_name = "lba_map",
1650 .ca_mode = S_IRUGO | S_IWUSR },
1651 .show = target_core_show_dev_lba_map,
1652 .store = target_core_store_dev_lba_map,
1653};
1654
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001655static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001656 &target_core_attr_dev_info.attr,
1657 &target_core_attr_dev_control.attr,
1658 &target_core_attr_dev_alias.attr,
1659 &target_core_attr_dev_udev_path.attr,
1660 &target_core_attr_dev_enable.attr,
1661 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001662 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 NULL,
1664};
1665
1666static void target_core_dev_release(struct config_item *item)
1667{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001668 struct config_group *dev_cg = to_config_group(item);
1669 struct se_device *dev =
1670 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001673 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674}
1675
1676static ssize_t target_core_dev_show(struct config_item *item,
1677 struct configfs_attribute *attr,
1678 char *page)
1679{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001680 struct config_group *dev_cg = to_config_group(item);
1681 struct se_device *dev =
1682 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 struct target_core_configfs_attribute *tc_attr = container_of(
1684 attr, struct target_core_configfs_attribute, attr);
1685
Andy Grover6708bb22011-06-08 10:36:43 -07001686 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 return -EINVAL;
1688
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001689 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690}
1691
1692static ssize_t target_core_dev_store(struct config_item *item,
1693 struct configfs_attribute *attr,
1694 const char *page, size_t count)
1695{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001696 struct config_group *dev_cg = to_config_group(item);
1697 struct se_device *dev =
1698 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 struct target_core_configfs_attribute *tc_attr = container_of(
1700 attr, struct target_core_configfs_attribute, attr);
1701
Andy Grover6708bb22011-06-08 10:36:43 -07001702 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001703 return -EINVAL;
1704
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001705 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706}
1707
1708static struct configfs_item_operations target_core_dev_item_ops = {
1709 .release = target_core_dev_release,
1710 .show_attribute = target_core_dev_show,
1711 .store_attribute = target_core_dev_store,
1712};
1713
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001714TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001716/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001717
1718/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1719
1720CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1721#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1722static struct target_core_alua_lu_gp_attribute \
1723 target_core_alua_lu_gp_##_name = \
1724 __CONFIGFS_EATTR(_name, _mode, \
1725 target_core_alua_lu_gp_show_attr_##_name, \
1726 target_core_alua_lu_gp_store_attr_##_name);
1727
1728#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1729static struct target_core_alua_lu_gp_attribute \
1730 target_core_alua_lu_gp_##_name = \
1731 __CONFIGFS_EATTR_RO(_name, \
1732 target_core_alua_lu_gp_show_attr_##_name);
1733
1734/*
1735 * lu_gp_id
1736 */
1737static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1738 struct t10_alua_lu_gp *lu_gp,
1739 char *page)
1740{
Andy Grover6708bb22011-06-08 10:36:43 -07001741 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742 return 0;
1743
1744 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1745}
1746
1747static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1748 struct t10_alua_lu_gp *lu_gp,
1749 const char *page,
1750 size_t count)
1751{
1752 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1753 unsigned long lu_gp_id;
1754 int ret;
1755
Jingoo Han57103d72013-07-19 16:22:19 +09001756 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001757 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09001758 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001759 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09001760 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761 }
1762 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001763 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764 " 0x0000ffff\n", lu_gp_id);
1765 return -EINVAL;
1766 }
1767
1768 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1769 if (ret < 0)
1770 return -EINVAL;
1771
Andy Grover6708bb22011-06-08 10:36:43 -07001772 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1774 config_item_name(&alua_lu_gp_cg->cg_item),
1775 lu_gp->lu_gp_id);
1776
1777 return count;
1778}
1779
1780SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1781
1782/*
1783 * members
1784 */
1785static ssize_t target_core_alua_lu_gp_show_attr_members(
1786 struct t10_alua_lu_gp *lu_gp,
1787 char *page)
1788{
1789 struct se_device *dev;
1790 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001791 struct t10_alua_lu_gp_member *lu_gp_mem;
1792 ssize_t len = 0, cur_len;
1793 unsigned char buf[LU_GROUP_NAME_BUF];
1794
1795 memset(buf, 0, LU_GROUP_NAME_BUF);
1796
1797 spin_lock(&lu_gp->lu_gp_lock);
1798 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1799 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001800 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801
1802 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1803 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001804 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001805 cur_len++; /* Extra byte for NULL terminator */
1806
1807 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001808 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001809 "_members buffer\n");
1810 break;
1811 }
1812 memcpy(page+len, buf, cur_len);
1813 len += cur_len;
1814 }
1815 spin_unlock(&lu_gp->lu_gp_lock);
1816
1817 return len;
1818}
1819
1820SE_DEV_ALUA_LU_ATTR_RO(members);
1821
1822CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1823
1824static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1825 &target_core_alua_lu_gp_lu_gp_id.attr,
1826 &target_core_alua_lu_gp_members.attr,
1827 NULL,
1828};
1829
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001830static void target_core_alua_lu_gp_release(struct config_item *item)
1831{
1832 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1833 struct t10_alua_lu_gp, lu_gp_group);
1834
1835 core_alua_free_lu_gp(lu_gp);
1836}
1837
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001839 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001840 .show_attribute = target_core_alua_lu_gp_attr_show,
1841 .store_attribute = target_core_alua_lu_gp_attr_store,
1842};
1843
1844static struct config_item_type target_core_alua_lu_gp_cit = {
1845 .ct_item_ops = &target_core_alua_lu_gp_ops,
1846 .ct_attrs = target_core_alua_lu_gp_attrs,
1847 .ct_owner = THIS_MODULE,
1848};
1849
1850/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1851
1852/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1853
1854static struct config_group *target_core_alua_create_lu_gp(
1855 struct config_group *group,
1856 const char *name)
1857{
1858 struct t10_alua_lu_gp *lu_gp;
1859 struct config_group *alua_lu_gp_cg = NULL;
1860 struct config_item *alua_lu_gp_ci = NULL;
1861
1862 lu_gp = core_alua_allocate_lu_gp(name, 0);
1863 if (IS_ERR(lu_gp))
1864 return NULL;
1865
1866 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1867 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1868
1869 config_group_init_type_name(alua_lu_gp_cg, name,
1870 &target_core_alua_lu_gp_cit);
1871
Andy Grover6708bb22011-06-08 10:36:43 -07001872 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873 " Group: core/alua/lu_gps/%s\n",
1874 config_item_name(alua_lu_gp_ci));
1875
1876 return alua_lu_gp_cg;
1877
1878}
1879
1880static void target_core_alua_drop_lu_gp(
1881 struct config_group *group,
1882 struct config_item *item)
1883{
1884 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1885 struct t10_alua_lu_gp, lu_gp_group);
1886
Andy Grover6708bb22011-06-08 10:36:43 -07001887 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001888 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1889 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001890 /*
1891 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1892 * -> target_core_alua_lu_gp_release()
1893 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895}
1896
1897static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1898 .make_group = &target_core_alua_create_lu_gp,
1899 .drop_item = &target_core_alua_drop_lu_gp,
1900};
1901
1902static struct config_item_type target_core_alua_lu_gps_cit = {
1903 .ct_item_ops = NULL,
1904 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1905 .ct_owner = THIS_MODULE,
1906};
1907
1908/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1909
1910/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1911
1912CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1913#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1914static struct target_core_alua_tg_pt_gp_attribute \
1915 target_core_alua_tg_pt_gp_##_name = \
1916 __CONFIGFS_EATTR(_name, _mode, \
1917 target_core_alua_tg_pt_gp_show_attr_##_name, \
1918 target_core_alua_tg_pt_gp_store_attr_##_name);
1919
1920#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1921static struct target_core_alua_tg_pt_gp_attribute \
1922 target_core_alua_tg_pt_gp_##_name = \
1923 __CONFIGFS_EATTR_RO(_name, \
1924 target_core_alua_tg_pt_gp_show_attr_##_name);
1925
1926/*
1927 * alua_access_state
1928 */
1929static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1930 struct t10_alua_tg_pt_gp *tg_pt_gp,
1931 char *page)
1932{
1933 return sprintf(page, "%d\n",
1934 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1935}
1936
1937static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1938 struct t10_alua_tg_pt_gp *tg_pt_gp,
1939 const char *page,
1940 size_t count)
1941{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001942 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001943 unsigned long tmp;
1944 int new_state, ret;
1945
Andy Grover6708bb22011-06-08 10:36:43 -07001946 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01001947 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1949 return -EINVAL;
1950 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07001951 if (!(dev->dev_flags & DF_CONFIGURED)) {
1952 pr_err("Unable to set alua_access_state while device is"
1953 " not configured\n");
1954 return -ENODEV;
1955 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956
Jingoo Han57103d72013-07-19 16:22:19 +09001957 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001959 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09001961 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 }
1963 new_state = (int)tmp;
1964
Hannes Reinecke125d0112013-11-19 09:07:46 +01001965 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1966 pr_err("Unable to process implicit configfs ALUA"
1967 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968 return -EINVAL;
1969 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01001970 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
1971 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
1972 /* LBA DEPENDENT is only allowed with implicit ALUA */
1973 pr_err("Unable to process implicit configfs ALUA transition"
1974 " while explicit ALUA management is enabled\n");
1975 return -EINVAL;
1976 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001978 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 NULL, NULL, new_state, 0);
1980 return (!ret) ? count : -EINVAL;
1981}
1982
1983SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
1984
1985/*
1986 * alua_access_status
1987 */
1988static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
1989 struct t10_alua_tg_pt_gp *tg_pt_gp,
1990 char *page)
1991{
1992 return sprintf(page, "%s\n",
1993 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
1994}
1995
1996static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
1997 struct t10_alua_tg_pt_gp *tg_pt_gp,
1998 const char *page,
1999 size_t count)
2000{
2001 unsigned long tmp;
2002 int new_status, ret;
2003
Andy Grover6708bb22011-06-08 10:36:43 -07002004 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2005 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 " valid tg_pt_gp ID: %hu\n",
2007 tg_pt_gp->tg_pt_gp_valid_id);
2008 return -EINVAL;
2009 }
2010
Jingoo Han57103d72013-07-19 16:22:19 +09002011 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002012 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002013 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002015 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016 }
2017 new_status = (int)tmp;
2018
2019 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002020 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2021 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002022 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 new_status);
2024 return -EINVAL;
2025 }
2026
2027 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2028 return count;
2029}
2030
2031SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2032
2033/*
2034 * alua_access_type
2035 */
2036static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2037 struct t10_alua_tg_pt_gp *tg_pt_gp,
2038 char *page)
2039{
2040 return core_alua_show_access_type(tg_pt_gp, page);
2041}
2042
2043static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2044 struct t10_alua_tg_pt_gp *tg_pt_gp,
2045 const char *page,
2046 size_t count)
2047{
2048 return core_alua_store_access_type(tg_pt_gp, page, count);
2049}
2050
2051SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2052
2053/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002054 * alua_supported_states
2055 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002056
2057#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2058static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2059 struct t10_alua_tg_pt_gp *t, char *p) \
2060{ \
2061 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002062}
2063
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002064#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2065static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2066 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2067{ \
2068 unsigned long tmp; \
2069 int ret; \
2070 \
2071 if (!t->tg_pt_gp_valid_id) { \
2072 pr_err("Unable to do set ##_name ALUA state on non" \
2073 " valid tg_pt_gp ID: %hu\n", \
2074 t->tg_pt_gp_valid_id); \
2075 return -EINVAL; \
2076 } \
2077 \
2078 ret = kstrtoul(p, 0, &tmp); \
2079 if (ret < 0) { \
2080 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2081 return -EINVAL; \
2082 } \
2083 if (tmp > 1) { \
2084 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2085 return -EINVAL; \
2086 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002087 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002088 t->_var |= _bit; \
2089 else \
2090 t->_var &= ~_bit; \
2091 \
2092 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002093}
2094
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002095SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2096 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2097SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2098 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2099SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2100
2101SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2102 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2103SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2104 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2105SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2106
2107SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2108 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2109SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2110 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002111SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002112
2113SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2114 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2115SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2116 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2117SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2118
2119SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2120 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2121SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2122 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2123SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2124
2125SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2126 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2127SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2128 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2129SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2130
2131SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2132 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2133SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2134 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2135SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002136
2137/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002138 * alua_write_metadata
2139 */
2140static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2141 struct t10_alua_tg_pt_gp *tg_pt_gp,
2142 char *page)
2143{
2144 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2145}
2146
2147static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2148 struct t10_alua_tg_pt_gp *tg_pt_gp,
2149 const char *page,
2150 size_t count)
2151{
2152 unsigned long tmp;
2153 int ret;
2154
Jingoo Han57103d72013-07-19 16:22:19 +09002155 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002157 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002158 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 }
2160
2161 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002162 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163 " %lu\n", tmp);
2164 return -EINVAL;
2165 }
2166 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2167
2168 return count;
2169}
2170
2171SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2172
2173
2174
2175/*
2176 * nonop_delay_msecs
2177 */
2178static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2179 struct t10_alua_tg_pt_gp *tg_pt_gp,
2180 char *page)
2181{
2182 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2183
2184}
2185
2186static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2187 struct t10_alua_tg_pt_gp *tg_pt_gp,
2188 const char *page,
2189 size_t count)
2190{
2191 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2192}
2193
2194SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2195
2196/*
2197 * trans_delay_msecs
2198 */
2199static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2200 struct t10_alua_tg_pt_gp *tg_pt_gp,
2201 char *page)
2202{
2203 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2204}
2205
2206static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2207 struct t10_alua_tg_pt_gp *tg_pt_gp,
2208 const char *page,
2209 size_t count)
2210{
2211 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2212}
2213
2214SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2215
2216/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002217 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002218 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002219static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002220 struct t10_alua_tg_pt_gp *tg_pt_gp,
2221 char *page)
2222{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002223 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002224}
2225
Hannes Reinecke125d0112013-11-19 09:07:46 +01002226static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002227 struct t10_alua_tg_pt_gp *tg_pt_gp,
2228 const char *page,
2229 size_t count)
2230{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002231 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002232}
2233
Hannes Reinecke125d0112013-11-19 09:07:46 +01002234SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002235
2236/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 * preferred
2238 */
2239
2240static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2241 struct t10_alua_tg_pt_gp *tg_pt_gp,
2242 char *page)
2243{
2244 return core_alua_show_preferred_bit(tg_pt_gp, page);
2245}
2246
2247static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2248 struct t10_alua_tg_pt_gp *tg_pt_gp,
2249 const char *page,
2250 size_t count)
2251{
2252 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2253}
2254
2255SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2256
2257/*
2258 * tg_pt_gp_id
2259 */
2260static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2261 struct t10_alua_tg_pt_gp *tg_pt_gp,
2262 char *page)
2263{
Andy Grover6708bb22011-06-08 10:36:43 -07002264 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 return 0;
2266
2267 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2268}
2269
2270static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2271 struct t10_alua_tg_pt_gp *tg_pt_gp,
2272 const char *page,
2273 size_t count)
2274{
2275 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2276 unsigned long tg_pt_gp_id;
2277 int ret;
2278
Jingoo Han57103d72013-07-19 16:22:19 +09002279 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002280 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002281 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002283 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284 }
2285 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002286 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 " 0x0000ffff\n", tg_pt_gp_id);
2288 return -EINVAL;
2289 }
2290
2291 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2292 if (ret < 0)
2293 return -EINVAL;
2294
Andy Grover6708bb22011-06-08 10:36:43 -07002295 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002296 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2297 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2298 tg_pt_gp->tg_pt_gp_id);
2299
2300 return count;
2301}
2302
2303SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2304
2305/*
2306 * members
2307 */
2308static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2309 struct t10_alua_tg_pt_gp *tg_pt_gp,
2310 char *page)
2311{
2312 struct se_port *port;
2313 struct se_portal_group *tpg;
2314 struct se_lun *lun;
2315 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2316 ssize_t len = 0, cur_len;
2317 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2318
2319 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2320
2321 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2322 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2323 tg_pt_gp_mem_list) {
2324 port = tg_pt_gp_mem->tg_pt;
2325 tpg = port->sep_tpg;
2326 lun = port->sep_lun;
2327
2328 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002329 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2330 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2331 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002332 config_item_name(&lun->lun_group.cg_item));
2333 cur_len++; /* Extra byte for NULL terminator */
2334
2335 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002336 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002337 "_members buffer\n");
2338 break;
2339 }
2340 memcpy(page+len, buf, cur_len);
2341 len += cur_len;
2342 }
2343 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2344
2345 return len;
2346}
2347
2348SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2349
2350CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2351 tg_pt_gp_group);
2352
2353static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2354 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2355 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2356 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002357 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2358 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2359 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2360 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2361 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2362 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2363 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002364 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2365 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2366 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002367 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 &target_core_alua_tg_pt_gp_preferred.attr,
2369 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2370 &target_core_alua_tg_pt_gp_members.attr,
2371 NULL,
2372};
2373
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002374static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2375{
2376 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2377 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2378
2379 core_alua_free_tg_pt_gp(tg_pt_gp);
2380}
2381
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002383 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002384 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2385 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2386};
2387
2388static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2389 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2390 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2391 .ct_owner = THIS_MODULE,
2392};
2393
2394/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2395
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002396/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397
2398static struct config_group *target_core_alua_create_tg_pt_gp(
2399 struct config_group *group,
2400 const char *name)
2401{
2402 struct t10_alua *alua = container_of(group, struct t10_alua,
2403 alua_tg_pt_gps_group);
2404 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 struct config_group *alua_tg_pt_gp_cg = NULL;
2406 struct config_item *alua_tg_pt_gp_ci = NULL;
2407
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002408 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002409 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002410 return NULL;
2411
2412 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2413 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2414
2415 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2416 &target_core_alua_tg_pt_gp_cit);
2417
Andy Grover6708bb22011-06-08 10:36:43 -07002418 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 " Group: alua/tg_pt_gps/%s\n",
2420 config_item_name(alua_tg_pt_gp_ci));
2421
2422 return alua_tg_pt_gp_cg;
2423}
2424
2425static void target_core_alua_drop_tg_pt_gp(
2426 struct config_group *group,
2427 struct config_item *item)
2428{
2429 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2430 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2431
Andy Grover6708bb22011-06-08 10:36:43 -07002432 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2434 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002435 /*
2436 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2437 * -> target_core_alua_tg_pt_gp_release().
2438 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002439 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002440}
2441
2442static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2443 .make_group = &target_core_alua_create_tg_pt_gp,
2444 .drop_item = &target_core_alua_drop_tg_pt_gp,
2445};
2446
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002447TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002449/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002450
2451/* Start functions for struct config_item_type target_core_alua_cit */
2452
2453/*
2454 * target_core_alua_cit is a ConfigFS group that lives under
2455 * /sys/kernel/config/target/core/alua. There are default groups
2456 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2457 * target_core_alua_cit in target_core_init_configfs() below.
2458 */
2459static struct config_item_type target_core_alua_cit = {
2460 .ct_item_ops = NULL,
2461 .ct_attrs = NULL,
2462 .ct_owner = THIS_MODULE,
2463};
2464
2465/* End functions for struct config_item_type target_core_alua_cit */
2466
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002467/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002468
2469static struct config_group *target_core_stat_mkdir(
2470 struct config_group *group,
2471 const char *name)
2472{
2473 return ERR_PTR(-ENOSYS);
2474}
2475
2476static void target_core_stat_rmdir(
2477 struct config_group *group,
2478 struct config_item *item)
2479{
2480 return;
2481}
2482
2483static struct configfs_group_operations target_core_stat_group_ops = {
2484 .make_group = &target_core_stat_mkdir,
2485 .drop_item = &target_core_stat_rmdir,
2486};
2487
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002488TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002489
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002490/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002491
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002492/* Start functions for struct config_item_type target_core_hba_cit */
2493
2494static struct config_group *target_core_make_subdev(
2495 struct config_group *group,
2496 const char *name)
2497{
2498 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499 struct se_subsystem_api *t;
2500 struct config_item *hba_ci = &group->cg_item;
2501 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002502 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002504 struct config_group *dev_stat_grp = NULL;
2505 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002507 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2508 if (ret)
2509 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510 /*
2511 * Locate the struct se_subsystem_api from parent's struct se_hba.
2512 */
2513 t = hba->transport;
2514
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002515 dev = target_alloc_device(hba, name);
2516 if (!dev)
2517 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002518
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002519 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002520
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002521 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002523 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002524 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002525
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002526 config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002527 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002528 &t->tb_cits.tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002529 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08002530 &t->tb_cits.tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002531 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08002532 &t->tb_cits.tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002533 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002534 "alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002535 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002536 "statistics", &t->tb_cits.tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002537
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002538 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2539 dev_cg->default_groups[1] = &dev->dev_pr_group;
2540 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2541 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2542 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002543 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002545 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002547 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002548 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002549 goto out_free_dev_cg_default_groups;
2550 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002552 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002553 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002555 if (!tg_pt_gp_cg->default_groups) {
2556 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002558 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 }
2560
2561 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2562 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2563 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2564 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002565 /*
2566 * Add core/$HBA/$DEV/statistics/ default groups
2567 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002568 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002569 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002570 GFP_KERNEL);
2571 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002572 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002573 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002574 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002575 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576
2577 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002578 return dev_cg;
2579
2580out_free_tg_pt_gp_cg_default_groups:
2581 kfree(tg_pt_gp_cg->default_groups);
2582out_free_tg_pt_gp:
2583 core_alua_free_tg_pt_gp(tg_pt_gp);
2584out_free_dev_cg_default_groups:
2585 kfree(dev_cg->default_groups);
2586out_free_device:
2587 target_free_device(dev);
2588out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002590 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591}
2592
2593static void target_core_drop_subdev(
2594 struct config_group *group,
2595 struct config_item *item)
2596{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002597 struct config_group *dev_cg = to_config_group(item);
2598 struct se_device *dev =
2599 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002600 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002601 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002602 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002603 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002604
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002605 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002606
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002607 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002608
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002609 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002610 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2611 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2612 dev_stat_grp->default_groups[i] = NULL;
2613 config_item_put(df_item);
2614 }
2615 kfree(dev_stat_grp->default_groups);
2616
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002617 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2619 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2620 tg_pt_gp_cg->default_groups[i] = NULL;
2621 config_item_put(df_item);
2622 }
2623 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002624 /*
2625 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2626 * directly from target_core_alua_tg_pt_gp_release().
2627 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002628 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002630 for (i = 0; dev_cg->default_groups[i]; i++) {
2631 df_item = &dev_cg->default_groups[i]->cg_item;
2632 dev_cg->default_groups[i] = NULL;
2633 config_item_put(df_item);
2634 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002635 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002636 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002637 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002638 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640}
2641
2642static struct configfs_group_operations target_core_hba_group_ops = {
2643 .make_group = target_core_make_subdev,
2644 .drop_item = target_core_drop_subdev,
2645};
2646
2647CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2648#define SE_HBA_ATTR(_name, _mode) \
2649static struct target_core_hba_attribute \
2650 target_core_hba_##_name = \
2651 __CONFIGFS_EATTR(_name, _mode, \
2652 target_core_hba_show_attr_##_name, \
2653 target_core_hba_store_attr_##_name);
2654
2655#define SE_HBA_ATTR_RO(_name) \
2656static struct target_core_hba_attribute \
2657 target_core_hba_##_name = \
2658 __CONFIGFS_EATTR_RO(_name, \
2659 target_core_hba_show_attr_##_name);
2660
2661static ssize_t target_core_hba_show_attr_hba_info(
2662 struct se_hba *hba,
2663 char *page)
2664{
2665 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2666 hba->hba_id, hba->transport->name,
2667 TARGET_CORE_CONFIGFS_VERSION);
2668}
2669
2670SE_HBA_ATTR_RO(hba_info);
2671
2672static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2673 char *page)
2674{
2675 int hba_mode = 0;
2676
2677 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2678 hba_mode = 1;
2679
2680 return sprintf(page, "%d\n", hba_mode);
2681}
2682
2683static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2684 const char *page, size_t count)
2685{
2686 struct se_subsystem_api *transport = hba->transport;
2687 unsigned long mode_flag;
2688 int ret;
2689
2690 if (transport->pmode_enable_hba == NULL)
2691 return -EINVAL;
2692
Jingoo Han57103d72013-07-19 16:22:19 +09002693 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002695 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002696 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 }
2698
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002699 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002700 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002701 return -EINVAL;
2702 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703
2704 ret = transport->pmode_enable_hba(hba, mode_flag);
2705 if (ret < 0)
2706 return -EINVAL;
2707 if (ret > 0)
2708 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2709 else if (ret == 0)
2710 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2711
2712 return count;
2713}
2714
2715SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2716
2717CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2718
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002719static void target_core_hba_release(struct config_item *item)
2720{
2721 struct se_hba *hba = container_of(to_config_group(item),
2722 struct se_hba, hba_group);
2723 core_delete_hba(hba);
2724}
2725
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002726static struct configfs_attribute *target_core_hba_attrs[] = {
2727 &target_core_hba_hba_info.attr,
2728 &target_core_hba_hba_mode.attr,
2729 NULL,
2730};
2731
2732static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002733 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002734 .show_attribute = target_core_hba_attr_show,
2735 .store_attribute = target_core_hba_attr_store,
2736};
2737
2738static struct config_item_type target_core_hba_cit = {
2739 .ct_item_ops = &target_core_hba_item_ops,
2740 .ct_group_ops = &target_core_hba_group_ops,
2741 .ct_attrs = target_core_hba_attrs,
2742 .ct_owner = THIS_MODULE,
2743};
2744
2745static struct config_group *target_core_call_addhbatotarget(
2746 struct config_group *group,
2747 const char *name)
2748{
2749 char *se_plugin_str, *str, *str2;
2750 struct se_hba *hba;
2751 char buf[TARGET_CORE_NAME_MAX_LEN];
2752 unsigned long plugin_dep_id = 0;
2753 int ret;
2754
2755 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07002756 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07002757 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2759 TARGET_CORE_NAME_MAX_LEN);
2760 return ERR_PTR(-ENAMETOOLONG);
2761 }
2762 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2763
2764 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002765 if (!str) {
2766 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 return ERR_PTR(-EINVAL);
2768 }
2769 se_plugin_str = buf;
2770 /*
2771 * Special case for subsystem plugins that have "_" in their names.
2772 * Namely rd_direct and rd_mcp..
2773 */
2774 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002775 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 *str2 = '\0'; /* Terminate for *se_plugin_str */
2777 str2++; /* Skip to start of plugin dependent ID */
2778 str = str2;
2779 } else {
2780 *str = '\0'; /* Terminate for *se_plugin_str */
2781 str++; /* Skip to start of plugin dependent ID */
2782 }
2783
Jingoo Han57103d72013-07-19 16:22:19 +09002784 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002786 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002787 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002788 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789 }
2790 /*
2791 * Load up TCM subsystem plugins if they have not already been loaded.
2792 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07002793 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794
2795 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2796 if (IS_ERR(hba))
2797 return ERR_CAST(hba);
2798
2799 config_group_init_type_name(&hba->hba_group, name,
2800 &target_core_hba_cit);
2801
2802 return &hba->hba_group;
2803}
2804
2805static void target_core_call_delhbafromtarget(
2806 struct config_group *group,
2807 struct config_item *item)
2808{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002809 /*
2810 * core_delete_hba() is called from target_core_hba_item_ops->release()
2811 * -> target_core_hba_release()
2812 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814}
2815
2816static struct configfs_group_operations target_core_group_ops = {
2817 .make_group = target_core_call_addhbatotarget,
2818 .drop_item = target_core_call_delhbafromtarget,
2819};
2820
2821static struct config_item_type target_core_cit = {
2822 .ct_item_ops = NULL,
2823 .ct_group_ops = &target_core_group_ops,
2824 .ct_attrs = NULL,
2825 .ct_owner = THIS_MODULE,
2826};
2827
2828/* Stop functions for struct config_item_type target_core_hba_cit */
2829
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002830void target_core_setup_sub_cits(struct se_subsystem_api *sa)
2831{
2832 target_core_setup_dev_cit(sa);
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002833 target_core_setup_dev_attrib_cit(sa);
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08002834 target_core_setup_dev_pr_cit(sa);
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08002835 target_core_setup_dev_wwn_cit(sa);
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002836 target_core_setup_dev_alua_tg_pt_gps_cit(sa);
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002837 target_core_setup_dev_stat_cit(sa);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002838}
2839EXPORT_SYMBOL(target_core_setup_sub_cits);
2840
Axel Lin54550fa2011-03-14 04:06:09 -07002841static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842{
2843 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2844 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02002845 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 struct t10_alua_lu_gp *lu_gp;
2847 int ret;
2848
Andy Grover6708bb22011-06-08 10:36:43 -07002849 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002850 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2851 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2852
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 config_group_init(&subsys->su_group);
2854 mutex_init(&subsys->su_mutex);
2855
Andy Grovere3d6f902011-07-19 08:55:10 +00002856 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002858 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859 /*
2860 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2861 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2862 */
2863 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08002864 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002866 if (!target_cg->default_groups) {
2867 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002868 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 goto out_global;
2870 }
2871
Andy Grovere3d6f902011-07-19 08:55:10 +00002872 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002874 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002875 target_cg->default_groups[1] = NULL;
2876 /*
2877 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2878 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002879 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002880 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002882 if (!hba_cg->default_groups) {
2883 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002884 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002885 goto out_global;
2886 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002887 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002888 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002889 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002890 hba_cg->default_groups[1] = NULL;
2891 /*
2892 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2893 * groups under /sys/kernel/config/target/core/alua/
2894 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002895 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002896 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002898 if (!alua_cg->default_groups) {
2899 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002900 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002901 goto out_global;
2902 }
2903
Andy Grovere3d6f902011-07-19 08:55:10 +00002904 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002906 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002907 alua_cg->default_groups[1] = NULL;
2908 /*
2909 * Add core/alua/lu_gps/default_lu_gp
2910 */
2911 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002912 if (IS_ERR(lu_gp)) {
2913 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002915 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916
Andy Grovere3d6f902011-07-19 08:55:10 +00002917 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002918 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002920 if (!lu_gp_cg->default_groups) {
2921 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002922 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 goto out_global;
2924 }
2925
2926 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2927 &target_core_alua_lu_gp_cit);
2928 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2929 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002930 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931 /*
2932 * Register the target_core_mod subsystem with configfs.
2933 */
2934 ret = configfs_register_subsystem(subsys);
2935 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002936 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 ret, subsys->su_group.cg_item.ci_namebuf);
2938 goto out_global;
2939 }
Andy Grover6708bb22011-06-08 10:36:43 -07002940 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002941 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2942 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2943 /*
2944 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2945 */
2946 ret = rd_module_init();
2947 if (ret < 0)
2948 goto out;
2949
Roland Dreier0d0f9df2012-10-31 09:16:44 -07002950 ret = core_dev_setup_virtual_lun0();
2951 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952 goto out;
2953
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07002954 ret = target_xcopy_setup_pt();
2955 if (ret < 0)
2956 goto out;
2957
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958 return 0;
2959
2960out:
2961 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962 core_dev_release_virtual_lun0();
2963 rd_module_exit();
2964out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00002965 if (default_lu_gp) {
2966 core_alua_free_lu_gp(default_lu_gp);
2967 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002968 }
2969 if (lu_gp_cg)
2970 kfree(lu_gp_cg->default_groups);
2971 if (alua_cg)
2972 kfree(alua_cg->default_groups);
2973 if (hba_cg)
2974 kfree(hba_cg->default_groups);
2975 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00002976 release_se_kmem_caches();
2977 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978}
2979
Axel Lin54550fa2011-03-14 04:06:09 -07002980static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2983 struct config_item *item;
2984 int i;
2985
Andy Grovere3d6f902011-07-19 08:55:10 +00002986 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002987 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2988 item = &lu_gp_cg->default_groups[i]->cg_item;
2989 lu_gp_cg->default_groups[i] = NULL;
2990 config_item_put(item);
2991 }
2992 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08002993 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994
Andy Grovere3d6f902011-07-19 08:55:10 +00002995 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 for (i = 0; alua_cg->default_groups[i]; i++) {
2997 item = &alua_cg->default_groups[i]->cg_item;
2998 alua_cg->default_groups[i] = NULL;
2999 config_item_put(item);
3000 }
3001 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003002 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003003
Andy Grovere3d6f902011-07-19 08:55:10 +00003004 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005 for (i = 0; hba_cg->default_groups[i]; i++) {
3006 item = &hba_cg->default_groups[i]->cg_item;
3007 hba_cg->default_groups[i] = NULL;
3008 config_item_put(item);
3009 }
3010 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003011 hba_cg->default_groups = NULL;
3012 /*
3013 * We expect subsys->su_group.default_groups to be released
3014 * by configfs subsystem provider logic..
3015 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003016 configfs_unregister_subsystem(&target_core_fabrics);
3017 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003018
Andy Grovere3d6f902011-07-19 08:55:10 +00003019 core_alua_free_lu_gp(default_lu_gp);
3020 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003021
Andy Grover6708bb22011-06-08 10:36:43 -07003022 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003023 " Infrastructure\n");
3024
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 core_dev_release_virtual_lun0();
3026 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003027 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003028 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003029}
3030
3031MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3032MODULE_AUTHOR("nab@Linux-iSCSI.org");
3033MODULE_LICENSE("GPL");
3034
3035module_init(target_core_init_configfs);
3036module_exit(target_core_exit_configfs);