blob: e7b0430a0575d0403dbb38b0fd4d41df1ccce79d [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->get_fabric_proto_ident) {
322 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 return -EINVAL;
324 }
Andy Grover6708bb22011-06-08 10:36:43 -0700325 if (!tfo->tpg_get_wwn) {
326 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327 return -EINVAL;
328 }
Andy Grover6708bb22011-06-08 10:36:43 -0700329 if (!tfo->tpg_get_tag) {
330 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800331 return -EINVAL;
332 }
Andy Grover6708bb22011-06-08 10:36:43 -0700333 if (!tfo->tpg_get_default_depth) {
334 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 return -EINVAL;
336 }
Andy Grover6708bb22011-06-08 10:36:43 -0700337 if (!tfo->tpg_get_pr_transport_id) {
338 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 return -EINVAL;
340 }
Andy Grover6708bb22011-06-08 10:36:43 -0700341 if (!tfo->tpg_get_pr_transport_id_len) {
342 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343 return -EINVAL;
344 }
Andy Grover6708bb22011-06-08 10:36:43 -0700345 if (!tfo->tpg_check_demo_mode) {
346 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347 return -EINVAL;
348 }
Andy Grover6708bb22011-06-08 10:36:43 -0700349 if (!tfo->tpg_check_demo_mode_cache) {
350 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800351 return -EINVAL;
352 }
Andy Grover6708bb22011-06-08 10:36:43 -0700353 if (!tfo->tpg_check_demo_mode_write_protect) {
354 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355 return -EINVAL;
356 }
Andy Grover6708bb22011-06-08 10:36:43 -0700357 if (!tfo->tpg_check_prod_mode_write_protect) {
358 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800359 return -EINVAL;
360 }
Andy Grover6708bb22011-06-08 10:36:43 -0700361 if (!tfo->tpg_alloc_fabric_acl) {
362 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363 return -EINVAL;
364 }
Andy Grover6708bb22011-06-08 10:36:43 -0700365 if (!tfo->tpg_release_fabric_acl) {
366 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800367 return -EINVAL;
368 }
Andy Grover6708bb22011-06-08 10:36:43 -0700369 if (!tfo->tpg_get_inst_index) {
370 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 return -EINVAL;
372 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400373 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700374 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 return -EINVAL;
376 }
Andy Grover6708bb22011-06-08 10:36:43 -0700377 if (!tfo->shutdown_session) {
378 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 return -EINVAL;
380 }
Andy Grover6708bb22011-06-08 10:36:43 -0700381 if (!tfo->close_session) {
382 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 return -EINVAL;
384 }
Andy Grover6708bb22011-06-08 10:36:43 -0700385 if (!tfo->sess_get_index) {
386 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -EINVAL;
388 }
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!tfo->write_pending) {
390 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return -EINVAL;
392 }
Andy Grover6708bb22011-06-08 10:36:43 -0700393 if (!tfo->write_pending_status) {
394 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 return -EINVAL;
396 }
Andy Grover6708bb22011-06-08 10:36:43 -0700397 if (!tfo->set_default_node_attributes) {
398 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 return -EINVAL;
400 }
Andy Grover6708bb22011-06-08 10:36:43 -0700401 if (!tfo->get_task_tag) {
402 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800403 return -EINVAL;
404 }
Andy Grover6708bb22011-06-08 10:36:43 -0700405 if (!tfo->get_cmd_state) {
406 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407 return -EINVAL;
408 }
Andy Grover6708bb22011-06-08 10:36:43 -0700409 if (!tfo->queue_data_in) {
410 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 return -EINVAL;
412 }
Andy Grover6708bb22011-06-08 10:36:43 -0700413 if (!tfo->queue_status) {
414 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 return -EINVAL;
416 }
Andy Grover6708bb22011-06-08 10:36:43 -0700417 if (!tfo->queue_tm_rsp) {
418 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 return -EINVAL;
420 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700421 if (!tfo->aborted_task) {
422 pr_err("Missing tfo->aborted_task()\n");
423 return -EINVAL;
424 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 /*
426 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
427 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
428 * target_core_fabric_configfs.c WWN+TPG group context code.
429 */
Andy Grover6708bb22011-06-08 10:36:43 -0700430 if (!tfo->fabric_make_wwn) {
431 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 return -EINVAL;
433 }
Andy Grover6708bb22011-06-08 10:36:43 -0700434 if (!tfo->fabric_drop_wwn) {
435 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800436 return -EINVAL;
437 }
Andy Grover6708bb22011-06-08 10:36:43 -0700438 if (!tfo->fabric_make_tpg) {
439 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440 return -EINVAL;
441 }
Andy Grover6708bb22011-06-08 10:36:43 -0700442 if (!tfo->fabric_drop_tpg) {
443 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444 return -EINVAL;
445 }
446
447 return 0;
448}
449
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200450int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200452 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 int ret;
454
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200455 ret = target_fabric_tf_ops_check(fo);
456 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457 return ret;
458
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200459 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700460 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200461 pr_err("%s: could not allocate memory!\n", __func__);
462 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200464
465 INIT_LIST_HEAD(&tf->tf_list);
466 atomic_set(&tf->tf_access_cnt, 0);
467
468 /*
469 * Setup the default generic struct config_item_type's (cits) in
470 * struct target_fabric_configfs->tf_cit_tmpl
471 */
472 tf->tf_module = fo->module;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200473 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", fo->name);
474
475 tf->tf_ops = *fo;
476 target_fabric_setup_cits(tf);
477
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200479 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 mutex_unlock(&g_tf_lock);
481
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200482 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200484EXPORT_SYMBOL(target_register_template);
485
486void target_unregister_template(const struct target_core_fabric_ops *fo)
487{
488 struct target_fabric_configfs *t;
489
490 mutex_lock(&g_tf_lock);
491 list_for_each_entry(t, &g_tf_list, tf_list) {
492 if (!strcmp(t->tf_name, fo->name)) {
493 BUG_ON(atomic_read(&t->tf_access_cnt));
494 list_del(&t->tf_list);
495 kfree(t);
496 break;
497 }
498 }
499 mutex_unlock(&g_tf_lock);
500}
501EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502
503/*##############################################################################
504// Stop functions called by external Target Fabrics Modules
505//############################################################################*/
506
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800507/* Start functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800510CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
511
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512static struct configfs_item_operations target_core_dev_attrib_ops = {
513 .show_attribute = target_core_dev_attrib_attr_show,
514 .store_attribute = target_core_dev_attrib_attr_store,
515};
516
Nicholas Bellinger43cf2082014-11-28 05:34:39 +0000517TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800518
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800519/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800520
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800521/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800522
523CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
524#define SE_DEV_WWN_ATTR(_name, _mode) \
525static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
526 __CONFIGFS_EATTR(_name, _mode, \
527 target_core_dev_wwn_show_attr_##_name, \
528 target_core_dev_wwn_store_attr_##_name);
529
530#define SE_DEV_WWN_ATTR_RO(_name); \
531do { \
532 static struct target_core_dev_wwn_attribute \
533 target_core_dev_wwn_##_name = \
534 __CONFIGFS_EATTR_RO(_name, \
535 target_core_dev_wwn_show_attr_##_name); \
536} while (0);
537
538/*
539 * VPD page 0x80 Unit serial
540 */
541static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
542 struct t10_wwn *t10_wwn,
543 char *page)
544{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
546 &t10_wwn->unit_serial[0]);
547}
548
549static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
550 struct t10_wwn *t10_wwn,
551 const char *page,
552 size_t count)
553{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400554 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
556
557 /*
558 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
559 * from the struct scsi_device level firmware, do not allow
560 * VPD Unit Serial to be emulated.
561 *
562 * Note this struct scsi_device could also be emulating VPD
563 * information from its drivers/scsi LLD. But for now we assume
564 * it is doing 'the right thing' wrt a world wide unique
565 * VPD Unit Serial Number that OS dependent multipath can depend on.
566 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400567 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700568 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800569 " Unit Serial, ignoring request\n");
570 return -EOPNOTSUPP;
571 }
572
Dan Carpenter60d645a2011-06-15 10:03:05 -0700573 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700574 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800575 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
576 return -EOVERFLOW;
577 }
578 /*
579 * Check to see if any active $FABRIC_MOD exports exist. If they
580 * do exist, fail here as changing this information on the fly
581 * (underneath the initiator side OS dependent multipath code)
582 * could cause negative effects.
583 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400584 if (dev->export_count) {
585 pr_err("Unable to set VPD Unit Serial while"
586 " active %d $FABRIC_MOD exports exist\n",
587 dev->export_count);
588 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800589 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400590
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 /*
592 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
593 *
594 * Also, strip any newline added from the userspace
595 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
596 */
597 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
598 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400599 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800600 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400601 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602
Andy Grover6708bb22011-06-08 10:36:43 -0700603 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400604 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605
606 return count;
607}
608
609SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
610
611/*
612 * VPD page 0x83 Protocol Identifier
613 */
614static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
615 struct t10_wwn *t10_wwn,
616 char *page)
617{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618 struct t10_vpd *vpd;
619 unsigned char buf[VPD_TMP_BUF_SIZE];
620 ssize_t len = 0;
621
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 memset(buf, 0, VPD_TMP_BUF_SIZE);
623
624 spin_lock(&t10_wwn->t10_vpd_lock);
625 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700626 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627 continue;
628
629 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
630
Andy Grover6708bb22011-06-08 10:36:43 -0700631 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800632 break;
633
634 len += sprintf(page+len, "%s", buf);
635 }
636 spin_unlock(&t10_wwn->t10_vpd_lock);
637
638 return len;
639}
640
641static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
642 struct t10_wwn *t10_wwn,
643 const char *page,
644 size_t count)
645{
646 return -ENOSYS;
647}
648
649SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
650
651/*
652 * Generic wrapper for dumping VPD identifiers by association.
653 */
654#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
655static ssize_t target_core_dev_wwn_show_attr_##_name( \
656 struct t10_wwn *t10_wwn, \
657 char *page) \
658{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659 struct t10_vpd *vpd; \
660 unsigned char buf[VPD_TMP_BUF_SIZE]; \
661 ssize_t len = 0; \
662 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663 spin_lock(&t10_wwn->t10_vpd_lock); \
664 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
665 if (vpd->association != _assoc) \
666 continue; \
667 \
668 memset(buf, 0, VPD_TMP_BUF_SIZE); \
669 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700670 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 break; \
672 len += sprintf(page+len, "%s", buf); \
673 \
674 memset(buf, 0, VPD_TMP_BUF_SIZE); \
675 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700676 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 break; \
678 len += sprintf(page+len, "%s", buf); \
679 \
680 memset(buf, 0, VPD_TMP_BUF_SIZE); \
681 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700682 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800683 break; \
684 len += sprintf(page+len, "%s", buf); \
685 } \
686 spin_unlock(&t10_wwn->t10_vpd_lock); \
687 \
688 return len; \
689}
690
691/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700692 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 */
694DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
695
696static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
697 struct t10_wwn *t10_wwn,
698 const char *page,
699 size_t count)
700{
701 return -ENOSYS;
702}
703
704SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
705
706/*
707 * VPD page 0x83 Association: Target Port
708 */
709DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
710
711static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
712 struct t10_wwn *t10_wwn,
713 const char *page,
714 size_t count)
715{
716 return -ENOSYS;
717}
718
719SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
720
721/*
722 * VPD page 0x83 Association: SCSI Target Device
723 */
724DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
725
726static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
727 struct t10_wwn *t10_wwn,
728 const char *page,
729 size_t count)
730{
731 return -ENOSYS;
732}
733
734SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
735
736CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
737
738static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
739 &target_core_dev_wwn_vpd_unit_serial.attr,
740 &target_core_dev_wwn_vpd_protocol_identifier.attr,
741 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
742 &target_core_dev_wwn_vpd_assoc_target_port.attr,
743 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
744 NULL,
745};
746
747static struct configfs_item_operations target_core_dev_wwn_ops = {
748 .show_attribute = target_core_dev_wwn_attr_show,
749 .store_attribute = target_core_dev_wwn_attr_store,
750};
751
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800752TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800754/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755
Nicholas Bellinger91e2e392014-11-27 14:57:01 -0800756/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400758CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759#define SE_DEV_PR_ATTR(_name, _mode) \
760static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
761 __CONFIGFS_EATTR(_name, _mode, \
762 target_core_dev_pr_show_attr_##_name, \
763 target_core_dev_pr_store_attr_##_name);
764
765#define SE_DEV_PR_ATTR_RO(_name); \
766static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
767 __CONFIGFS_EATTR_RO(_name, \
768 target_core_dev_pr_show_attr_##_name);
769
Christoph Hellwigd977f432012-10-10 17:37:15 -0400770static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
771 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800772{
773 struct se_node_acl *se_nacl;
774 struct t10_pr_registration *pr_reg;
775 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776
777 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
778
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400780 if (!pr_reg)
781 return sprintf(page, "No SPC-3 Reservation holder\n");
782
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800783 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -0700784 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800785
Christoph Hellwigd977f432012-10-10 17:37:15 -0400786 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000787 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700788 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789}
790
Christoph Hellwigd977f432012-10-10 17:37:15 -0400791static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
792 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800793{
794 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400795 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800797 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400798 if (se_nacl) {
799 len = sprintf(page,
800 "SPC-2 Reservation: %s Initiator: %s\n",
801 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
802 se_nacl->initiatorname);
803 } else {
804 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400806 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807}
808
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400809static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
810 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800811{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400812 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813
Andy Grovera3541702015-05-19 14:44:41 -0700814 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400815 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816
Christoph Hellwigd977f432012-10-10 17:37:15 -0400817 spin_lock(&dev->dev_reservation_lock);
818 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
819 ret = target_core_dev_pr_show_spc2_res(dev, page);
820 else
821 ret = target_core_dev_pr_show_spc3_res(dev, page);
822 spin_unlock(&dev->dev_reservation_lock);
823 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824}
825
826SE_DEV_PR_ATTR_RO(res_holder);
827
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400829 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800830{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 ssize_t len = 0;
832
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -0400834 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800835 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400836 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837 len = sprintf(page, "SPC-3 Reservation: All Target"
838 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400839 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 len = sprintf(page, "SPC-3 Reservation: Single"
841 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400842 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843
Christoph Hellwigd977f432012-10-10 17:37:15 -0400844 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 return len;
846}
847
848SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
849
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400851 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800852{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400853 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854}
855
856SE_DEV_PR_ATTR_RO(res_pr_generation);
857
858/*
859 * res_pr_holder_tg_port
860 */
861static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400862 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800864 struct se_node_acl *se_nacl;
865 struct se_lun *lun;
866 struct se_portal_group *se_tpg;
867 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200868 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 ssize_t len = 0;
870
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871 spin_lock(&dev->dev_reservation_lock);
872 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -0700873 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400875 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400877
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878 se_nacl = pr_reg->pr_reg_nacl;
879 se_tpg = se_nacl->se_tpg;
880 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000881 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800882
883 len += sprintf(page+len, "SPC-3 Reservation: %s"
884 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
885 tfo->tpg_get_wwn(se_tpg));
886 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +0900887 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800888 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
889 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
890 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891
Christoph Hellwigd977f432012-10-10 17:37:15 -0400892out_unlock:
893 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800894 return len;
895}
896
897SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
898
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400900 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200902 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800903 struct t10_pr_registration *pr_reg;
904 unsigned char buf[384];
905 char i_buf[PR_REG_ISID_ID_LEN];
906 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -0700907 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800909 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
910
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400911 spin_lock(&dev->t10_pr.registration_lock);
912 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913 pr_reg_list) {
914
915 memset(buf, 0, 384);
916 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
917 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -0700918 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800919 PR_REG_ISID_ID_LEN);
920 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
921 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700922 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800923 pr_reg->pr_res_generation);
924
Andy Grover6708bb22011-06-08 10:36:43 -0700925 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800926 break;
927
928 len += sprintf(page+len, "%s", buf);
929 reg_count++;
930 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400931 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800932
Andy Grover6708bb22011-06-08 10:36:43 -0700933 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800934 len += sprintf(page+len, "None\n");
935
936 return len;
937}
938
939SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
940
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800941static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400942 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800944 struct t10_pr_registration *pr_reg;
945 ssize_t len = 0;
946
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 spin_lock(&dev->dev_reservation_lock);
948 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400949 if (pr_reg) {
950 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
951 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
952 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800955
Christoph Hellwigd977f432012-10-10 17:37:15 -0400956 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800957 return len;
958}
959
960SE_DEV_PR_ATTR_RO(res_pr_type);
961
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800962static ssize_t target_core_dev_pr_show_attr_res_type(
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)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400966 return sprintf(page, "SPC_PASSTHROUGH\n");
967 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
968 return sprintf(page, "SPC2_RESERVATIONS\n");
969 else
970 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800971}
972
973SE_DEV_PR_ATTR_RO(res_type);
974
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400976 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977{
Andy Grovera3541702015-05-19 14:44:41 -0700978 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800979 return 0;
980
981 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400982 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800983}
984
985SE_DEV_PR_ATTR_RO(res_aptpl_active);
986
987/*
988 * res_aptpl_metadata
989 */
990static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400991 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800992{
Andy Grovera3541702015-05-19 14:44:41 -0700993 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800994 return 0;
995
996 return sprintf(page, "Ready to process PR APTPL metadata..\n");
997}
998
999enum {
1000 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1001 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1002 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1003 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1004};
1005
1006static match_table_t tokens = {
1007 {Opt_initiator_fabric, "initiator_fabric=%s"},
1008 {Opt_initiator_node, "initiator_node=%s"},
1009 {Opt_initiator_sid, "initiator_sid=%s"},
1010 {Opt_sa_res_key, "sa_res_key=%s"},
1011 {Opt_res_holder, "res_holder=%d"},
1012 {Opt_res_type, "res_type=%d"},
1013 {Opt_res_scope, "res_scope=%d"},
1014 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1015 {Opt_mapped_lun, "mapped_lun=%d"},
1016 {Opt_target_fabric, "target_fabric=%s"},
1017 {Opt_target_node, "target_node=%s"},
1018 {Opt_tpgt, "tpgt=%d"},
1019 {Opt_port_rtpi, "port_rtpi=%d"},
1020 {Opt_target_lun, "target_lun=%d"},
1021 {Opt_err, NULL}
1022};
1023
1024static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001025 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026 const char *page,
1027 size_t count)
1028{
Jesper Juhl6d180252011-03-14 04:05:56 -07001029 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1030 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001031 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 substring_t args[MAX_OPT_ARGS];
1033 unsigned long long tmp_ll;
1034 u64 sa_res_key = 0;
1035 u32 mapped_lun = 0, target_lun = 0;
1036 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1037 u16 port_rpti = 0, tpgt = 0;
1038 u8 type = 0, scope;
1039
Andy Grovera3541702015-05-19 14:44:41 -07001040 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001041 return 0;
1042 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 return 0;
1044
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001045 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001046 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047 " active fabric exports exist\n");
1048 return -EINVAL;
1049 }
1050
1051 opts = kstrdup(page, GFP_KERNEL);
1052 if (!opts)
1053 return -ENOMEM;
1054
1055 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001056 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057 if (!*ptr)
1058 continue;
1059
1060 token = match_token(ptr, tokens, args);
1061 switch (token) {
1062 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001063 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001064 if (!i_fabric) {
1065 ret = -ENOMEM;
1066 goto out;
1067 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 break;
1069 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001070 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001071 if (!i_port) {
1072 ret = -ENOMEM;
1073 goto out;
1074 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001075 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001076 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1078 PR_APTPL_MAX_IPORT_LEN);
1079 ret = -EINVAL;
1080 break;
1081 }
1082 break;
1083 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001084 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001085 if (!isid) {
1086 ret = -ENOMEM;
1087 goto out;
1088 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001089 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001090 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091 "= exceeds PR_REG_ISID_LEN: %d\n",
1092 PR_REG_ISID_LEN);
1093 ret = -EINVAL;
1094 break;
1095 }
1096 break;
1097 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001098 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001100 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101 goto out;
1102 }
1103 sa_res_key = (u64)tmp_ll;
1104 break;
1105 /*
1106 * PR APTPL Metadata for Reservation
1107 */
1108 case Opt_res_holder:
1109 match_int(args, &arg);
1110 res_holder = arg;
1111 break;
1112 case Opt_res_type:
1113 match_int(args, &arg);
1114 type = (u8)arg;
1115 break;
1116 case Opt_res_scope:
1117 match_int(args, &arg);
1118 scope = (u8)arg;
1119 break;
1120 case Opt_res_all_tg_pt:
1121 match_int(args, &arg);
1122 all_tg_pt = (int)arg;
1123 break;
1124 case Opt_mapped_lun:
1125 match_int(args, &arg);
1126 mapped_lun = (u32)arg;
1127 break;
1128 /*
1129 * PR APTPL Metadata for Target Port
1130 */
1131 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001132 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001133 if (!t_fabric) {
1134 ret = -ENOMEM;
1135 goto out;
1136 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001137 break;
1138 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001139 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001140 if (!t_port) {
1141 ret = -ENOMEM;
1142 goto out;
1143 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001144 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001145 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1147 PR_APTPL_MAX_TPORT_LEN);
1148 ret = -EINVAL;
1149 break;
1150 }
1151 break;
1152 case Opt_tpgt:
1153 match_int(args, &arg);
1154 tpgt = (u16)arg;
1155 break;
1156 case Opt_port_rtpi:
1157 match_int(args, &arg);
1158 port_rpti = (u16)arg;
1159 break;
1160 case Opt_target_lun:
1161 match_int(args, &arg);
1162 target_lun = (u32)arg;
1163 break;
1164 default:
1165 break;
1166 }
1167 }
1168
Andy Grover6708bb22011-06-08 10:36:43 -07001169 if (!i_port || !t_port || !sa_res_key) {
1170 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 ret = -EINVAL;
1172 goto out;
1173 }
1174
1175 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001176 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 " holder\n", type);
1178 ret = -EINVAL;
1179 goto out;
1180 }
1181
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001182 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1184 res_holder, all_tg_pt, type);
1185out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001186 kfree(i_fabric);
1187 kfree(i_port);
1188 kfree(isid);
1189 kfree(t_fabric);
1190 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 kfree(orig);
1192 return (ret == 0) ? count : ret;
1193}
1194
1195SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1196
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001197CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198
1199static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1200 &target_core_dev_pr_res_holder.attr,
1201 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1202 &target_core_dev_pr_res_pr_generation.attr,
1203 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1204 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1205 &target_core_dev_pr_res_pr_type.attr,
1206 &target_core_dev_pr_res_type.attr,
1207 &target_core_dev_pr_res_aptpl_active.attr,
1208 &target_core_dev_pr_res_aptpl_metadata.attr,
1209 NULL,
1210};
1211
1212static struct configfs_item_operations target_core_dev_pr_ops = {
1213 .show_attribute = target_core_dev_pr_attr_show,
1214 .store_attribute = target_core_dev_pr_attr_store,
1215};
1216
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001217TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001219/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001220
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001221/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222
1223static ssize_t target_core_show_dev_info(void *p, char *page)
1224{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001225 struct se_device *dev = p;
1226 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 int bl = 0;
1228 ssize_t read_bytes = 0;
1229
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001230 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001232 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 return read_bytes;
1234}
1235
1236static struct target_core_configfs_attribute target_core_attr_dev_info = {
1237 .attr = { .ca_owner = THIS_MODULE,
1238 .ca_name = "info",
1239 .ca_mode = S_IRUGO },
1240 .show = target_core_show_dev_info,
1241 .store = NULL,
1242};
1243
1244static ssize_t target_core_store_dev_control(
1245 void *p,
1246 const char *page,
1247 size_t count)
1248{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001249 struct se_device *dev = p;
1250 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001252 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253}
1254
1255static struct target_core_configfs_attribute target_core_attr_dev_control = {
1256 .attr = { .ca_owner = THIS_MODULE,
1257 .ca_name = "control",
1258 .ca_mode = S_IWUSR },
1259 .show = NULL,
1260 .store = target_core_store_dev_control,
1261};
1262
1263static ssize_t target_core_show_dev_alias(void *p, char *page)
1264{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001265 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001267 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 return 0;
1269
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001270 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001271}
1272
1273static ssize_t target_core_store_dev_alias(
1274 void *p,
1275 const char *page,
1276 size_t count)
1277{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001278 struct se_device *dev = p;
1279 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 ssize_t read_bytes;
1281
1282 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001283 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1285 SE_DEV_ALIAS_LEN-1);
1286 return -EINVAL;
1287 }
1288
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001289 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001290 if (!read_bytes)
1291 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001292 if (dev->dev_alias[read_bytes - 1] == '\n')
1293 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001294
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001295 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001296
Andy Grover6708bb22011-06-08 10:36:43 -07001297 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001299 config_item_name(&dev->dev_group.cg_item),
1300 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301
1302 return read_bytes;
1303}
1304
1305static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1306 .attr = { .ca_owner = THIS_MODULE,
1307 .ca_name = "alias",
1308 .ca_mode = S_IRUGO | S_IWUSR },
1309 .show = target_core_show_dev_alias,
1310 .store = target_core_store_dev_alias,
1311};
1312
1313static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1314{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001315 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001316
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001317 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001318 return 0;
1319
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001320 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321}
1322
1323static ssize_t target_core_store_dev_udev_path(
1324 void *p,
1325 const char *page,
1326 size_t count)
1327{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001328 struct se_device *dev = p;
1329 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001330 ssize_t read_bytes;
1331
1332 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001333 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1335 SE_UDEV_PATH_LEN-1);
1336 return -EINVAL;
1337 }
1338
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001339 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001341 if (!read_bytes)
1342 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001343 if (dev->udev_path[read_bytes - 1] == '\n')
1344 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001345
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001346 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001347
Andy Grover6708bb22011-06-08 10:36:43 -07001348 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001350 config_item_name(&dev->dev_group.cg_item),
1351 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001352
1353 return read_bytes;
1354}
1355
1356static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1357 .attr = { .ca_owner = THIS_MODULE,
1358 .ca_name = "udev_path",
1359 .ca_mode = S_IRUGO | S_IWUSR },
1360 .show = target_core_show_dev_udev_path,
1361 .store = target_core_store_dev_udev_path,
1362};
1363
Andy Grover64146db2013-04-30 11:59:15 -07001364static ssize_t target_core_show_dev_enable(void *p, char *page)
1365{
1366 struct se_device *dev = p;
1367
1368 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1369}
1370
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371static ssize_t target_core_store_dev_enable(
1372 void *p,
1373 const char *page,
1374 size_t count)
1375{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001376 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001378 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379
1380 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001381 if (!ptr) {
1382 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 " is \"1\"\n");
1384 return -EINVAL;
1385 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001387 ret = target_configure_device(dev);
1388 if (ret)
1389 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001390 return count;
1391}
1392
1393static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1394 .attr = { .ca_owner = THIS_MODULE,
1395 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001396 .ca_mode = S_IRUGO | S_IWUSR },
1397 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 .store = target_core_store_dev_enable,
1399};
1400
1401static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1402{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001403 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 struct config_item *lu_ci;
1405 struct t10_alua_lu_gp *lu_gp;
1406 struct t10_alua_lu_gp_member *lu_gp_mem;
1407 ssize_t len = 0;
1408
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001410 if (!lu_gp_mem)
1411 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412
1413 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1414 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001415 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416 lu_ci = &lu_gp->lu_gp_group.cg_item;
1417 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1418 config_item_name(lu_ci), lu_gp->lu_gp_id);
1419 }
1420 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1421
1422 return len;
1423}
1424
1425static ssize_t target_core_store_alua_lu_gp(
1426 void *p,
1427 const char *page,
1428 size_t count)
1429{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001430 struct se_device *dev = p;
1431 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1433 struct t10_alua_lu_gp_member *lu_gp_mem;
1434 unsigned char buf[LU_GROUP_NAME_BUF];
1435 int move = 0;
1436
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001437 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1438 if (!lu_gp_mem)
1439 return 0;
1440
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001442 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 return -EINVAL;
1444 }
1445 memset(buf, 0, LU_GROUP_NAME_BUF);
1446 memcpy(buf, page, count);
1447 /*
1448 * Any ALUA logical unit alias besides "NULL" means we will be
1449 * making a new group association.
1450 */
1451 if (strcmp(strstrip(buf), "NULL")) {
1452 /*
1453 * core_alua_get_lu_gp_by_name() will increment reference to
1454 * struct t10_alua_lu_gp. This reference is released with
1455 * core_alua_get_lu_gp_by_name below().
1456 */
1457 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001458 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001459 return -ENODEV;
1460 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461
1462 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1463 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001464 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 /*
1466 * Clearing an existing lu_gp association, and replacing
1467 * with NULL
1468 */
Andy Grover6708bb22011-06-08 10:36:43 -07001469 if (!lu_gp_new) {
1470 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001471 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1472 " %hu\n",
1473 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001474 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 config_item_name(&lu_gp->lu_gp_group.cg_item),
1476 lu_gp->lu_gp_id);
1477
1478 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1479 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1480
1481 return count;
1482 }
1483 /*
1484 * Removing existing association of lu_gp_mem with lu_gp
1485 */
1486 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1487 move = 1;
1488 }
1489 /*
1490 * Associate lu_gp_mem with lu_gp_new.
1491 */
1492 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1493 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1494
Andy Grover6708bb22011-06-08 10:36:43 -07001495 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496 " core/alua/lu_gps/%s, ID: %hu\n",
1497 (move) ? "Moving" : "Adding",
1498 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001499 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1501 lu_gp_new->lu_gp_id);
1502
1503 core_alua_put_lu_gp_from_name(lu_gp_new);
1504 return count;
1505}
1506
1507static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1508 .attr = { .ca_owner = THIS_MODULE,
1509 .ca_name = "alua_lu_gp",
1510 .ca_mode = S_IRUGO | S_IWUSR },
1511 .show = target_core_show_alua_lu_gp,
1512 .store = target_core_store_alua_lu_gp,
1513};
1514
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001515static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1516{
1517 struct se_device *dev = p;
1518 struct t10_alua_lba_map *map;
1519 struct t10_alua_lba_map_member *mem;
1520 char *b = page;
1521 int bl = 0;
1522 char state;
1523
1524 spin_lock(&dev->t10_alua.lba_map_lock);
1525 if (!list_empty(&dev->t10_alua.lba_map_list))
1526 bl += sprintf(b + bl, "%u %u\n",
1527 dev->t10_alua.lba_map_segment_size,
1528 dev->t10_alua.lba_map_segment_multiplier);
1529 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1530 bl += sprintf(b + bl, "%llu %llu",
1531 map->lba_map_first_lba, map->lba_map_last_lba);
1532 list_for_each_entry(mem, &map->lba_map_mem_list,
1533 lba_map_mem_list) {
1534 switch (mem->lba_map_mem_alua_state) {
1535 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1536 state = 'O';
1537 break;
1538 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1539 state = 'A';
1540 break;
1541 case ALUA_ACCESS_STATE_STANDBY:
1542 state = 'S';
1543 break;
1544 case ALUA_ACCESS_STATE_UNAVAILABLE:
1545 state = 'U';
1546 break;
1547 default:
1548 state = '.';
1549 break;
1550 }
1551 bl += sprintf(b + bl, " %d:%c",
1552 mem->lba_map_mem_alua_pg_id, state);
1553 }
1554 bl += sprintf(b + bl, "\n");
1555 }
1556 spin_unlock(&dev->t10_alua.lba_map_lock);
1557 return bl;
1558}
1559
1560static ssize_t target_core_store_dev_lba_map(
1561 void *p,
1562 const char *page,
1563 size_t count)
1564{
1565 struct se_device *dev = p;
1566 struct t10_alua_lba_map *lba_map = NULL;
1567 struct list_head lba_list;
1568 char *map_entries, *ptr;
1569 char state;
1570 int pg_num = -1, pg;
1571 int ret = 0, num = 0, pg_id, alua_state;
1572 unsigned long start_lba = -1, end_lba = -1;
1573 unsigned long segment_size = -1, segment_mult = -1;
1574
1575 map_entries = kstrdup(page, GFP_KERNEL);
1576 if (!map_entries)
1577 return -ENOMEM;
1578
1579 INIT_LIST_HEAD(&lba_list);
1580 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1581 if (!*ptr)
1582 continue;
1583
1584 if (num == 0) {
1585 if (sscanf(ptr, "%lu %lu\n",
1586 &segment_size, &segment_mult) != 2) {
1587 pr_err("Invalid line %d\n", num);
1588 ret = -EINVAL;
1589 break;
1590 }
1591 num++;
1592 continue;
1593 }
1594 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1595 pr_err("Invalid line %d\n", num);
1596 ret = -EINVAL;
1597 break;
1598 }
1599 ptr = strchr(ptr, ' ');
1600 if (!ptr) {
1601 pr_err("Invalid line %d, missing end lba\n", num);
1602 ret = -EINVAL;
1603 break;
1604 }
1605 ptr++;
1606 ptr = strchr(ptr, ' ');
1607 if (!ptr) {
1608 pr_err("Invalid line %d, missing state definitions\n",
1609 num);
1610 ret = -EINVAL;
1611 break;
1612 }
1613 ptr++;
1614 lba_map = core_alua_allocate_lba_map(&lba_list,
1615 start_lba, end_lba);
1616 if (IS_ERR(lba_map)) {
1617 ret = PTR_ERR(lba_map);
1618 break;
1619 }
1620 pg = 0;
1621 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1622 switch (state) {
1623 case 'O':
1624 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1625 break;
1626 case 'A':
1627 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1628 break;
1629 case 'S':
1630 alua_state = ALUA_ACCESS_STATE_STANDBY;
1631 break;
1632 case 'U':
1633 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1634 break;
1635 default:
1636 pr_err("Invalid ALUA state '%c'\n", state);
1637 ret = -EINVAL;
1638 goto out;
1639 }
1640
1641 ret = core_alua_allocate_lba_map_mem(lba_map,
1642 pg_id, alua_state);
1643 if (ret) {
1644 pr_err("Invalid target descriptor %d:%c "
1645 "at line %d\n",
1646 pg_id, state, num);
1647 break;
1648 }
1649 pg++;
1650 ptr = strchr(ptr, ' ');
1651 if (ptr)
1652 ptr++;
1653 else
1654 break;
1655 }
1656 if (pg_num == -1)
1657 pg_num = pg;
1658 else if (pg != pg_num) {
1659 pr_err("Only %d from %d port groups definitions "
1660 "at line %d\n", pg, pg_num, num);
1661 ret = -EINVAL;
1662 break;
1663 }
1664 num++;
1665 }
1666out:
1667 if (ret) {
1668 core_alua_free_lba_map(&lba_list);
1669 count = ret;
1670 } else
1671 core_alua_set_lba_map(dev, &lba_list,
1672 segment_size, segment_mult);
1673 kfree(map_entries);
1674 return count;
1675}
1676
1677static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1678 .attr = { .ca_owner = THIS_MODULE,
1679 .ca_name = "lba_map",
1680 .ca_mode = S_IRUGO | S_IWUSR },
1681 .show = target_core_show_dev_lba_map,
1682 .store = target_core_store_dev_lba_map,
1683};
1684
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001685static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001686 &target_core_attr_dev_info.attr,
1687 &target_core_attr_dev_control.attr,
1688 &target_core_attr_dev_alias.attr,
1689 &target_core_attr_dev_udev_path.attr,
1690 &target_core_attr_dev_enable.attr,
1691 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001692 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693 NULL,
1694};
1695
1696static void target_core_dev_release(struct config_item *item)
1697{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001698 struct config_group *dev_cg = to_config_group(item);
1699 struct se_device *dev =
1700 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001703 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704}
1705
1706static ssize_t target_core_dev_show(struct config_item *item,
1707 struct configfs_attribute *attr,
1708 char *page)
1709{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001710 struct config_group *dev_cg = to_config_group(item);
1711 struct se_device *dev =
1712 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 struct target_core_configfs_attribute *tc_attr = container_of(
1714 attr, struct target_core_configfs_attribute, attr);
1715
Andy Grover6708bb22011-06-08 10:36:43 -07001716 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001717 return -EINVAL;
1718
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001719 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720}
1721
1722static ssize_t target_core_dev_store(struct config_item *item,
1723 struct configfs_attribute *attr,
1724 const char *page, size_t count)
1725{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001726 struct config_group *dev_cg = to_config_group(item);
1727 struct se_device *dev =
1728 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 struct target_core_configfs_attribute *tc_attr = container_of(
1730 attr, struct target_core_configfs_attribute, attr);
1731
Andy Grover6708bb22011-06-08 10:36:43 -07001732 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 return -EINVAL;
1734
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001735 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736}
1737
1738static struct configfs_item_operations target_core_dev_item_ops = {
1739 .release = target_core_dev_release,
1740 .show_attribute = target_core_dev_show,
1741 .store_attribute = target_core_dev_store,
1742};
1743
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001744TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001746/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001747
1748/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1749
1750CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1751#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1752static struct target_core_alua_lu_gp_attribute \
1753 target_core_alua_lu_gp_##_name = \
1754 __CONFIGFS_EATTR(_name, _mode, \
1755 target_core_alua_lu_gp_show_attr_##_name, \
1756 target_core_alua_lu_gp_store_attr_##_name);
1757
1758#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1759static struct target_core_alua_lu_gp_attribute \
1760 target_core_alua_lu_gp_##_name = \
1761 __CONFIGFS_EATTR_RO(_name, \
1762 target_core_alua_lu_gp_show_attr_##_name);
1763
1764/*
1765 * lu_gp_id
1766 */
1767static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1768 struct t10_alua_lu_gp *lu_gp,
1769 char *page)
1770{
Andy Grover6708bb22011-06-08 10:36:43 -07001771 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772 return 0;
1773
1774 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1775}
1776
1777static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1778 struct t10_alua_lu_gp *lu_gp,
1779 const char *page,
1780 size_t count)
1781{
1782 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1783 unsigned long lu_gp_id;
1784 int ret;
1785
Jingoo Han57103d72013-07-19 16:22:19 +09001786 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09001788 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09001790 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001791 }
1792 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001793 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001794 " 0x0000ffff\n", lu_gp_id);
1795 return -EINVAL;
1796 }
1797
1798 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1799 if (ret < 0)
1800 return -EINVAL;
1801
Andy Grover6708bb22011-06-08 10:36:43 -07001802 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001803 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1804 config_item_name(&alua_lu_gp_cg->cg_item),
1805 lu_gp->lu_gp_id);
1806
1807 return count;
1808}
1809
1810SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1811
1812/*
1813 * members
1814 */
1815static ssize_t target_core_alua_lu_gp_show_attr_members(
1816 struct t10_alua_lu_gp *lu_gp,
1817 char *page)
1818{
1819 struct se_device *dev;
1820 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821 struct t10_alua_lu_gp_member *lu_gp_mem;
1822 ssize_t len = 0, cur_len;
1823 unsigned char buf[LU_GROUP_NAME_BUF];
1824
1825 memset(buf, 0, LU_GROUP_NAME_BUF);
1826
1827 spin_lock(&lu_gp->lu_gp_lock);
1828 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1829 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001830 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831
1832 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1833 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001834 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 cur_len++; /* Extra byte for NULL terminator */
1836
1837 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001838 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839 "_members buffer\n");
1840 break;
1841 }
1842 memcpy(page+len, buf, cur_len);
1843 len += cur_len;
1844 }
1845 spin_unlock(&lu_gp->lu_gp_lock);
1846
1847 return len;
1848}
1849
1850SE_DEV_ALUA_LU_ATTR_RO(members);
1851
1852CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1853
1854static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1855 &target_core_alua_lu_gp_lu_gp_id.attr,
1856 &target_core_alua_lu_gp_members.attr,
1857 NULL,
1858};
1859
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001860static void target_core_alua_lu_gp_release(struct config_item *item)
1861{
1862 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1863 struct t10_alua_lu_gp, lu_gp_group);
1864
1865 core_alua_free_lu_gp(lu_gp);
1866}
1867
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001869 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001870 .show_attribute = target_core_alua_lu_gp_attr_show,
1871 .store_attribute = target_core_alua_lu_gp_attr_store,
1872};
1873
1874static struct config_item_type target_core_alua_lu_gp_cit = {
1875 .ct_item_ops = &target_core_alua_lu_gp_ops,
1876 .ct_attrs = target_core_alua_lu_gp_attrs,
1877 .ct_owner = THIS_MODULE,
1878};
1879
1880/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1881
1882/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1883
1884static struct config_group *target_core_alua_create_lu_gp(
1885 struct config_group *group,
1886 const char *name)
1887{
1888 struct t10_alua_lu_gp *lu_gp;
1889 struct config_group *alua_lu_gp_cg = NULL;
1890 struct config_item *alua_lu_gp_ci = NULL;
1891
1892 lu_gp = core_alua_allocate_lu_gp(name, 0);
1893 if (IS_ERR(lu_gp))
1894 return NULL;
1895
1896 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1897 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1898
1899 config_group_init_type_name(alua_lu_gp_cg, name,
1900 &target_core_alua_lu_gp_cit);
1901
Andy Grover6708bb22011-06-08 10:36:43 -07001902 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903 " Group: core/alua/lu_gps/%s\n",
1904 config_item_name(alua_lu_gp_ci));
1905
1906 return alua_lu_gp_cg;
1907
1908}
1909
1910static void target_core_alua_drop_lu_gp(
1911 struct config_group *group,
1912 struct config_item *item)
1913{
1914 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1915 struct t10_alua_lu_gp, lu_gp_group);
1916
Andy Grover6708bb22011-06-08 10:36:43 -07001917 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1919 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001920 /*
1921 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1922 * -> target_core_alua_lu_gp_release()
1923 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925}
1926
1927static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1928 .make_group = &target_core_alua_create_lu_gp,
1929 .drop_item = &target_core_alua_drop_lu_gp,
1930};
1931
1932static struct config_item_type target_core_alua_lu_gps_cit = {
1933 .ct_item_ops = NULL,
1934 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1935 .ct_owner = THIS_MODULE,
1936};
1937
1938/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1939
1940/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1941
1942CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1943#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1944static struct target_core_alua_tg_pt_gp_attribute \
1945 target_core_alua_tg_pt_gp_##_name = \
1946 __CONFIGFS_EATTR(_name, _mode, \
1947 target_core_alua_tg_pt_gp_show_attr_##_name, \
1948 target_core_alua_tg_pt_gp_store_attr_##_name);
1949
1950#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1951static struct target_core_alua_tg_pt_gp_attribute \
1952 target_core_alua_tg_pt_gp_##_name = \
1953 __CONFIGFS_EATTR_RO(_name, \
1954 target_core_alua_tg_pt_gp_show_attr_##_name);
1955
1956/*
1957 * alua_access_state
1958 */
1959static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1960 struct t10_alua_tg_pt_gp *tg_pt_gp,
1961 char *page)
1962{
1963 return sprintf(page, "%d\n",
1964 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1965}
1966
1967static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1968 struct t10_alua_tg_pt_gp *tg_pt_gp,
1969 const char *page,
1970 size_t count)
1971{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001972 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001973 unsigned long tmp;
1974 int new_state, ret;
1975
Andy Grover6708bb22011-06-08 10:36:43 -07001976 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01001977 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001978 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1979 return -EINVAL;
1980 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07001981 if (!(dev->dev_flags & DF_CONFIGURED)) {
1982 pr_err("Unable to set alua_access_state while device is"
1983 " not configured\n");
1984 return -ENODEV;
1985 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001986
Jingoo Han57103d72013-07-19 16:22:19 +09001987 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001989 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09001991 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992 }
1993 new_state = (int)tmp;
1994
Hannes Reinecke125d0112013-11-19 09:07:46 +01001995 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1996 pr_err("Unable to process implicit configfs ALUA"
1997 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998 return -EINVAL;
1999 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002000 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2001 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2002 /* LBA DEPENDENT is only allowed with implicit ALUA */
2003 pr_err("Unable to process implicit configfs ALUA transition"
2004 " while explicit ALUA management is enabled\n");
2005 return -EINVAL;
2006 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002008 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009 NULL, NULL, new_state, 0);
2010 return (!ret) ? count : -EINVAL;
2011}
2012
2013SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2014
2015/*
2016 * alua_access_status
2017 */
2018static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2019 struct t10_alua_tg_pt_gp *tg_pt_gp,
2020 char *page)
2021{
2022 return sprintf(page, "%s\n",
2023 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2024}
2025
2026static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2027 struct t10_alua_tg_pt_gp *tg_pt_gp,
2028 const char *page,
2029 size_t count)
2030{
2031 unsigned long tmp;
2032 int new_status, ret;
2033
Andy Grover6708bb22011-06-08 10:36:43 -07002034 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2035 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036 " valid tg_pt_gp ID: %hu\n",
2037 tg_pt_gp->tg_pt_gp_valid_id);
2038 return -EINVAL;
2039 }
2040
Jingoo Han57103d72013-07-19 16:22:19 +09002041 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002042 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002043 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002045 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 }
2047 new_status = (int)tmp;
2048
2049 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002050 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2051 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002052 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 new_status);
2054 return -EINVAL;
2055 }
2056
2057 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2058 return count;
2059}
2060
2061SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2062
2063/*
2064 * alua_access_type
2065 */
2066static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2067 struct t10_alua_tg_pt_gp *tg_pt_gp,
2068 char *page)
2069{
2070 return core_alua_show_access_type(tg_pt_gp, page);
2071}
2072
2073static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2074 struct t10_alua_tg_pt_gp *tg_pt_gp,
2075 const char *page,
2076 size_t count)
2077{
2078 return core_alua_store_access_type(tg_pt_gp, page, count);
2079}
2080
2081SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2082
2083/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002084 * alua_supported_states
2085 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002086
2087#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2088static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2089 struct t10_alua_tg_pt_gp *t, char *p) \
2090{ \
2091 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002092}
2093
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002094#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2095static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2096 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2097{ \
2098 unsigned long tmp; \
2099 int ret; \
2100 \
2101 if (!t->tg_pt_gp_valid_id) { \
2102 pr_err("Unable to do set ##_name ALUA state on non" \
2103 " valid tg_pt_gp ID: %hu\n", \
2104 t->tg_pt_gp_valid_id); \
2105 return -EINVAL; \
2106 } \
2107 \
2108 ret = kstrtoul(p, 0, &tmp); \
2109 if (ret < 0) { \
2110 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2111 return -EINVAL; \
2112 } \
2113 if (tmp > 1) { \
2114 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2115 return -EINVAL; \
2116 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002117 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002118 t->_var |= _bit; \
2119 else \
2120 t->_var &= ~_bit; \
2121 \
2122 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002123}
2124
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002125SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2126 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2127SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2128 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2129SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2130
2131SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2132 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2133SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2134 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2135SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2136
2137SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2138 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2139SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2140 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002141SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002142
2143SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2144 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2145SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2146 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2147SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2148
2149SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2150 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2151SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2152 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2153SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2154
2155SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2156 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2157SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2158 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2159SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2160
2161SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2162 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2163SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2164 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2165SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002166
2167/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168 * alua_write_metadata
2169 */
2170static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2171 struct t10_alua_tg_pt_gp *tg_pt_gp,
2172 char *page)
2173{
2174 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2175}
2176
2177static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2178 struct t10_alua_tg_pt_gp *tg_pt_gp,
2179 const char *page,
2180 size_t count)
2181{
2182 unsigned long tmp;
2183 int ret;
2184
Jingoo Han57103d72013-07-19 16:22:19 +09002185 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002186 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002187 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002188 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002189 }
2190
2191 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002192 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 " %lu\n", tmp);
2194 return -EINVAL;
2195 }
2196 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2197
2198 return count;
2199}
2200
2201SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2202
2203
2204
2205/*
2206 * nonop_delay_msecs
2207 */
2208static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2209 struct t10_alua_tg_pt_gp *tg_pt_gp,
2210 char *page)
2211{
2212 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2213
2214}
2215
2216static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2217 struct t10_alua_tg_pt_gp *tg_pt_gp,
2218 const char *page,
2219 size_t count)
2220{
2221 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2222}
2223
2224SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2225
2226/*
2227 * trans_delay_msecs
2228 */
2229static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2230 struct t10_alua_tg_pt_gp *tg_pt_gp,
2231 char *page)
2232{
2233 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2234}
2235
2236static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2237 struct t10_alua_tg_pt_gp *tg_pt_gp,
2238 const char *page,
2239 size_t count)
2240{
2241 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2242}
2243
2244SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2245
2246/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002247 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002248 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002249static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002250 struct t10_alua_tg_pt_gp *tg_pt_gp,
2251 char *page)
2252{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002253 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002254}
2255
Hannes Reinecke125d0112013-11-19 09:07:46 +01002256static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002257 struct t10_alua_tg_pt_gp *tg_pt_gp,
2258 const char *page,
2259 size_t count)
2260{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002261 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002262}
2263
Hannes Reinecke125d0112013-11-19 09:07:46 +01002264SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002265
2266/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267 * preferred
2268 */
2269
2270static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2271 struct t10_alua_tg_pt_gp *tg_pt_gp,
2272 char *page)
2273{
2274 return core_alua_show_preferred_bit(tg_pt_gp, page);
2275}
2276
2277static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2278 struct t10_alua_tg_pt_gp *tg_pt_gp,
2279 const char *page,
2280 size_t count)
2281{
2282 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2283}
2284
2285SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2286
2287/*
2288 * tg_pt_gp_id
2289 */
2290static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2291 struct t10_alua_tg_pt_gp *tg_pt_gp,
2292 char *page)
2293{
Andy Grover6708bb22011-06-08 10:36:43 -07002294 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 return 0;
2296
2297 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2298}
2299
2300static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2301 struct t10_alua_tg_pt_gp *tg_pt_gp,
2302 const char *page,
2303 size_t count)
2304{
2305 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2306 unsigned long tg_pt_gp_id;
2307 int ret;
2308
Jingoo Han57103d72013-07-19 16:22:19 +09002309 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002310 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002311 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002313 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002314 }
2315 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002316 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002317 " 0x0000ffff\n", tg_pt_gp_id);
2318 return -EINVAL;
2319 }
2320
2321 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2322 if (ret < 0)
2323 return -EINVAL;
2324
Andy Grover6708bb22011-06-08 10:36:43 -07002325 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002326 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2327 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2328 tg_pt_gp->tg_pt_gp_id);
2329
2330 return count;
2331}
2332
2333SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2334
2335/*
2336 * members
2337 */
2338static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2339 struct t10_alua_tg_pt_gp *tg_pt_gp,
2340 char *page)
2341{
2342 struct se_port *port;
2343 struct se_portal_group *tpg;
2344 struct se_lun *lun;
2345 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2346 ssize_t len = 0, cur_len;
2347 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2348
2349 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2350
2351 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2352 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2353 tg_pt_gp_mem_list) {
2354 port = tg_pt_gp_mem->tg_pt;
2355 tpg = port->sep_tpg;
2356 lun = port->sep_lun;
2357
2358 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002359 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2360 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2361 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002362 config_item_name(&lun->lun_group.cg_item));
2363 cur_len++; /* Extra byte for NULL terminator */
2364
2365 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002366 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367 "_members buffer\n");
2368 break;
2369 }
2370 memcpy(page+len, buf, cur_len);
2371 len += cur_len;
2372 }
2373 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2374
2375 return len;
2376}
2377
2378SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2379
2380CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2381 tg_pt_gp_group);
2382
2383static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2384 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2385 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2386 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002387 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2388 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2389 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2390 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2391 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2392 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2393 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002394 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2395 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2396 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002397 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002398 &target_core_alua_tg_pt_gp_preferred.attr,
2399 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2400 &target_core_alua_tg_pt_gp_members.attr,
2401 NULL,
2402};
2403
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002404static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2405{
2406 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2407 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2408
2409 core_alua_free_tg_pt_gp(tg_pt_gp);
2410}
2411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002413 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2415 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2416};
2417
2418static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2419 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2420 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2421 .ct_owner = THIS_MODULE,
2422};
2423
2424/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2425
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002426/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427
2428static struct config_group *target_core_alua_create_tg_pt_gp(
2429 struct config_group *group,
2430 const char *name)
2431{
2432 struct t10_alua *alua = container_of(group, struct t10_alua,
2433 alua_tg_pt_gps_group);
2434 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 struct config_group *alua_tg_pt_gp_cg = NULL;
2436 struct config_item *alua_tg_pt_gp_ci = NULL;
2437
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002438 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002439 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002440 return NULL;
2441
2442 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2443 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2444
2445 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2446 &target_core_alua_tg_pt_gp_cit);
2447
Andy Grover6708bb22011-06-08 10:36:43 -07002448 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449 " Group: alua/tg_pt_gps/%s\n",
2450 config_item_name(alua_tg_pt_gp_ci));
2451
2452 return alua_tg_pt_gp_cg;
2453}
2454
2455static void target_core_alua_drop_tg_pt_gp(
2456 struct config_group *group,
2457 struct config_item *item)
2458{
2459 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2460 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2461
Andy Grover6708bb22011-06-08 10:36:43 -07002462 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002463 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2464 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002465 /*
2466 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2467 * -> target_core_alua_tg_pt_gp_release().
2468 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002469 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470}
2471
2472static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2473 .make_group = &target_core_alua_create_tg_pt_gp,
2474 .drop_item = &target_core_alua_drop_tg_pt_gp,
2475};
2476
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002477TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002478
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002479/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002480
2481/* Start functions for struct config_item_type target_core_alua_cit */
2482
2483/*
2484 * target_core_alua_cit is a ConfigFS group that lives under
2485 * /sys/kernel/config/target/core/alua. There are default groups
2486 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2487 * target_core_alua_cit in target_core_init_configfs() below.
2488 */
2489static struct config_item_type target_core_alua_cit = {
2490 .ct_item_ops = NULL,
2491 .ct_attrs = NULL,
2492 .ct_owner = THIS_MODULE,
2493};
2494
2495/* End functions for struct config_item_type target_core_alua_cit */
2496
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002497/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002498
2499static struct config_group *target_core_stat_mkdir(
2500 struct config_group *group,
2501 const char *name)
2502{
2503 return ERR_PTR(-ENOSYS);
2504}
2505
2506static void target_core_stat_rmdir(
2507 struct config_group *group,
2508 struct config_item *item)
2509{
2510 return;
2511}
2512
2513static struct configfs_group_operations target_core_stat_group_ops = {
2514 .make_group = &target_core_stat_mkdir,
2515 .drop_item = &target_core_stat_rmdir,
2516};
2517
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002518TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002519
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002520/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002521
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522/* Start functions for struct config_item_type target_core_hba_cit */
2523
2524static struct config_group *target_core_make_subdev(
2525 struct config_group *group,
2526 const char *name)
2527{
2528 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529 struct se_subsystem_api *t;
2530 struct config_item *hba_ci = &group->cg_item;
2531 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002532 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002534 struct config_group *dev_stat_grp = NULL;
2535 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002537 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2538 if (ret)
2539 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 /*
2541 * Locate the struct se_subsystem_api from parent's struct se_hba.
2542 */
2543 t = hba->transport;
2544
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002545 dev = target_alloc_device(hba, name);
2546 if (!dev)
2547 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002549 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002550
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002551 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002552 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002553 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002554 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002555
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002556 config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002557 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002558 &t->tb_cits.tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002559 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08002560 &t->tb_cits.tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002561 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08002562 &t->tb_cits.tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002563 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002564 "alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002565 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002566 "statistics", &t->tb_cits.tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002567
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002568 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2569 dev_cg->default_groups[1] = &dev->dev_pr_group;
2570 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2571 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2572 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002573 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002575 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002577 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002578 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002579 goto out_free_dev_cg_default_groups;
2580 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002582 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002583 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002584 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002585 if (!tg_pt_gp_cg->default_groups) {
2586 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002588 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 }
2590
2591 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2592 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2593 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2594 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002595 /*
2596 * Add core/$HBA/$DEV/statistics/ default groups
2597 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002598 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002599 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002600 GFP_KERNEL);
2601 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002602 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002603 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002604 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002605 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002606
2607 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002608 return dev_cg;
2609
2610out_free_tg_pt_gp_cg_default_groups:
2611 kfree(tg_pt_gp_cg->default_groups);
2612out_free_tg_pt_gp:
2613 core_alua_free_tg_pt_gp(tg_pt_gp);
2614out_free_dev_cg_default_groups:
2615 kfree(dev_cg->default_groups);
2616out_free_device:
2617 target_free_device(dev);
2618out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002619 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002620 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002621}
2622
2623static void target_core_drop_subdev(
2624 struct config_group *group,
2625 struct config_item *item)
2626{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002627 struct config_group *dev_cg = to_config_group(item);
2628 struct se_device *dev =
2629 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002630 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002631 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002632 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002633 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002635 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002637 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002638
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002639 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002640 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2641 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2642 dev_stat_grp->default_groups[i] = NULL;
2643 config_item_put(df_item);
2644 }
2645 kfree(dev_stat_grp->default_groups);
2646
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002647 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2649 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2650 tg_pt_gp_cg->default_groups[i] = NULL;
2651 config_item_put(df_item);
2652 }
2653 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002654 /*
2655 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2656 * directly from target_core_alua_tg_pt_gp_release().
2657 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002658 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002659
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002660 for (i = 0; dev_cg->default_groups[i]; i++) {
2661 df_item = &dev_cg->default_groups[i]->cg_item;
2662 dev_cg->default_groups[i] = NULL;
2663 config_item_put(df_item);
2664 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002665 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002666 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002668 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002670}
2671
2672static struct configfs_group_operations target_core_hba_group_ops = {
2673 .make_group = target_core_make_subdev,
2674 .drop_item = target_core_drop_subdev,
2675};
2676
2677CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2678#define SE_HBA_ATTR(_name, _mode) \
2679static struct target_core_hba_attribute \
2680 target_core_hba_##_name = \
2681 __CONFIGFS_EATTR(_name, _mode, \
2682 target_core_hba_show_attr_##_name, \
2683 target_core_hba_store_attr_##_name);
2684
2685#define SE_HBA_ATTR_RO(_name) \
2686static struct target_core_hba_attribute \
2687 target_core_hba_##_name = \
2688 __CONFIGFS_EATTR_RO(_name, \
2689 target_core_hba_show_attr_##_name);
2690
2691static ssize_t target_core_hba_show_attr_hba_info(
2692 struct se_hba *hba,
2693 char *page)
2694{
2695 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2696 hba->hba_id, hba->transport->name,
2697 TARGET_CORE_CONFIGFS_VERSION);
2698}
2699
2700SE_HBA_ATTR_RO(hba_info);
2701
2702static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2703 char *page)
2704{
2705 int hba_mode = 0;
2706
2707 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2708 hba_mode = 1;
2709
2710 return sprintf(page, "%d\n", hba_mode);
2711}
2712
2713static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2714 const char *page, size_t count)
2715{
2716 struct se_subsystem_api *transport = hba->transport;
2717 unsigned long mode_flag;
2718 int ret;
2719
2720 if (transport->pmode_enable_hba == NULL)
2721 return -EINVAL;
2722
Jingoo Han57103d72013-07-19 16:22:19 +09002723 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002725 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002726 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002727 }
2728
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002729 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002730 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002731 return -EINVAL;
2732 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733
2734 ret = transport->pmode_enable_hba(hba, mode_flag);
2735 if (ret < 0)
2736 return -EINVAL;
2737 if (ret > 0)
2738 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2739 else if (ret == 0)
2740 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2741
2742 return count;
2743}
2744
2745SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2746
2747CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2748
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002749static void target_core_hba_release(struct config_item *item)
2750{
2751 struct se_hba *hba = container_of(to_config_group(item),
2752 struct se_hba, hba_group);
2753 core_delete_hba(hba);
2754}
2755
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756static struct configfs_attribute *target_core_hba_attrs[] = {
2757 &target_core_hba_hba_info.attr,
2758 &target_core_hba_hba_mode.attr,
2759 NULL,
2760};
2761
2762static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002763 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002764 .show_attribute = target_core_hba_attr_show,
2765 .store_attribute = target_core_hba_attr_store,
2766};
2767
2768static struct config_item_type target_core_hba_cit = {
2769 .ct_item_ops = &target_core_hba_item_ops,
2770 .ct_group_ops = &target_core_hba_group_ops,
2771 .ct_attrs = target_core_hba_attrs,
2772 .ct_owner = THIS_MODULE,
2773};
2774
2775static struct config_group *target_core_call_addhbatotarget(
2776 struct config_group *group,
2777 const char *name)
2778{
2779 char *se_plugin_str, *str, *str2;
2780 struct se_hba *hba;
2781 char buf[TARGET_CORE_NAME_MAX_LEN];
2782 unsigned long plugin_dep_id = 0;
2783 int ret;
2784
2785 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07002786 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07002787 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2789 TARGET_CORE_NAME_MAX_LEN);
2790 return ERR_PTR(-ENAMETOOLONG);
2791 }
2792 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2793
2794 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002795 if (!str) {
2796 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002797 return ERR_PTR(-EINVAL);
2798 }
2799 se_plugin_str = buf;
2800 /*
2801 * Special case for subsystem plugins that have "_" in their names.
2802 * Namely rd_direct and rd_mcp..
2803 */
2804 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002805 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 *str2 = '\0'; /* Terminate for *se_plugin_str */
2807 str2++; /* Skip to start of plugin dependent ID */
2808 str = str2;
2809 } else {
2810 *str = '\0'; /* Terminate for *se_plugin_str */
2811 str++; /* Skip to start of plugin dependent ID */
2812 }
2813
Jingoo Han57103d72013-07-19 16:22:19 +09002814 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002816 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002818 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002819 }
2820 /*
2821 * Load up TCM subsystem plugins if they have not already been loaded.
2822 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07002823 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824
2825 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2826 if (IS_ERR(hba))
2827 return ERR_CAST(hba);
2828
2829 config_group_init_type_name(&hba->hba_group, name,
2830 &target_core_hba_cit);
2831
2832 return &hba->hba_group;
2833}
2834
2835static void target_core_call_delhbafromtarget(
2836 struct config_group *group,
2837 struct config_item *item)
2838{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002839 /*
2840 * core_delete_hba() is called from target_core_hba_item_ops->release()
2841 * -> target_core_hba_release()
2842 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844}
2845
2846static struct configfs_group_operations target_core_group_ops = {
2847 .make_group = target_core_call_addhbatotarget,
2848 .drop_item = target_core_call_delhbafromtarget,
2849};
2850
2851static struct config_item_type target_core_cit = {
2852 .ct_item_ops = NULL,
2853 .ct_group_ops = &target_core_group_ops,
2854 .ct_attrs = NULL,
2855 .ct_owner = THIS_MODULE,
2856};
2857
2858/* Stop functions for struct config_item_type target_core_hba_cit */
2859
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002860void target_core_setup_sub_cits(struct se_subsystem_api *sa)
2861{
2862 target_core_setup_dev_cit(sa);
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002863 target_core_setup_dev_attrib_cit(sa);
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08002864 target_core_setup_dev_pr_cit(sa);
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08002865 target_core_setup_dev_wwn_cit(sa);
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002866 target_core_setup_dev_alua_tg_pt_gps_cit(sa);
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002867 target_core_setup_dev_stat_cit(sa);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002868}
2869EXPORT_SYMBOL(target_core_setup_sub_cits);
2870
Axel Lin54550fa2011-03-14 04:06:09 -07002871static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872{
2873 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2874 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02002875 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 struct t10_alua_lu_gp *lu_gp;
2877 int ret;
2878
Andy Grover6708bb22011-06-08 10:36:43 -07002879 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002880 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2881 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2882
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002883 config_group_init(&subsys->su_group);
2884 mutex_init(&subsys->su_mutex);
2885
Andy Grovere3d6f902011-07-19 08:55:10 +00002886 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002888 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889 /*
2890 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2891 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2892 */
2893 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08002894 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002895 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002896 if (!target_cg->default_groups) {
2897 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002898 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 goto out_global;
2900 }
2901
Andy Grovere3d6f902011-07-19 08:55:10 +00002902 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002904 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 target_cg->default_groups[1] = NULL;
2906 /*
2907 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2908 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002909 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002910 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002911 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002912 if (!hba_cg->default_groups) {
2913 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002914 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 goto out_global;
2916 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002917 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002919 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 hba_cg->default_groups[1] = NULL;
2921 /*
2922 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2923 * groups under /sys/kernel/config/target/core/alua/
2924 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002925 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002926 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002928 if (!alua_cg->default_groups) {
2929 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002930 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931 goto out_global;
2932 }
2933
Andy Grovere3d6f902011-07-19 08:55:10 +00002934 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002936 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 alua_cg->default_groups[1] = NULL;
2938 /*
2939 * Add core/alua/lu_gps/default_lu_gp
2940 */
2941 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002942 if (IS_ERR(lu_gp)) {
2943 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002945 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946
Andy Grovere3d6f902011-07-19 08:55:10 +00002947 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002948 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002949 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002950 if (!lu_gp_cg->default_groups) {
2951 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002952 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 goto out_global;
2954 }
2955
2956 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2957 &target_core_alua_lu_gp_cit);
2958 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2959 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002960 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961 /*
2962 * Register the target_core_mod subsystem with configfs.
2963 */
2964 ret = configfs_register_subsystem(subsys);
2965 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002966 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967 ret, subsys->su_group.cg_item.ci_namebuf);
2968 goto out_global;
2969 }
Andy Grover6708bb22011-06-08 10:36:43 -07002970 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002971 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2972 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2973 /*
2974 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2975 */
2976 ret = rd_module_init();
2977 if (ret < 0)
2978 goto out;
2979
Roland Dreier0d0f9df2012-10-31 09:16:44 -07002980 ret = core_dev_setup_virtual_lun0();
2981 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 goto out;
2983
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07002984 ret = target_xcopy_setup_pt();
2985 if (ret < 0)
2986 goto out;
2987
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 return 0;
2989
2990out:
2991 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 core_dev_release_virtual_lun0();
2993 rd_module_exit();
2994out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00002995 if (default_lu_gp) {
2996 core_alua_free_lu_gp(default_lu_gp);
2997 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002998 }
2999 if (lu_gp_cg)
3000 kfree(lu_gp_cg->default_groups);
3001 if (alua_cg)
3002 kfree(alua_cg->default_groups);
3003 if (hba_cg)
3004 kfree(hba_cg->default_groups);
3005 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003006 release_se_kmem_caches();
3007 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008}
3009
Axel Lin54550fa2011-03-14 04:06:09 -07003010static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003011{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3013 struct config_item *item;
3014 int i;
3015
Andy Grovere3d6f902011-07-19 08:55:10 +00003016 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003017 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3018 item = &lu_gp_cg->default_groups[i]->cg_item;
3019 lu_gp_cg->default_groups[i] = NULL;
3020 config_item_put(item);
3021 }
3022 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003023 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003024
Andy Grovere3d6f902011-07-19 08:55:10 +00003025 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003026 for (i = 0; alua_cg->default_groups[i]; i++) {
3027 item = &alua_cg->default_groups[i]->cg_item;
3028 alua_cg->default_groups[i] = NULL;
3029 config_item_put(item);
3030 }
3031 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003032 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033
Andy Grovere3d6f902011-07-19 08:55:10 +00003034 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035 for (i = 0; hba_cg->default_groups[i]; i++) {
3036 item = &hba_cg->default_groups[i]->cg_item;
3037 hba_cg->default_groups[i] = NULL;
3038 config_item_put(item);
3039 }
3040 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003041 hba_cg->default_groups = NULL;
3042 /*
3043 * We expect subsys->su_group.default_groups to be released
3044 * by configfs subsystem provider logic..
3045 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003046 configfs_unregister_subsystem(&target_core_fabrics);
3047 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003048
Andy Grovere3d6f902011-07-19 08:55:10 +00003049 core_alua_free_lu_gp(default_lu_gp);
3050 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003051
Andy Grover6708bb22011-06-08 10:36:43 -07003052 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003053 " Infrastructure\n");
3054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003055 core_dev_release_virtual_lun0();
3056 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003057 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003058 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059}
3060
3061MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3062MODULE_AUTHOR("nab@Linux-iSCSI.org");
3063MODULE_LICENSE("GPL");
3064
3065module_init(target_core_init_configfs);
3066module_exit(target_core_exit_configfs);