blob: 57c099dd9da5bef860f6d2af478a973fd10eb4d1 [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) \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020054static void target_core_setup_##_name##_cit(struct target_backend *tb) \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080055{ \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020056 struct config_item_type *cit = &tb->tb_##_name##_cit; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080057 \
58 cit->ct_item_ops = _item_ops; \
59 cit->ct_group_ops = _group_ops; \
60 cit->ct_attrs = _attrs; \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020061 cit->ct_owner = tb->ops->owner; \
62 pr_debug("Setup generic %s\n", __stringify(_name)); \
63}
64
65#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
66static void target_core_setup_##_name##_cit(struct target_backend *tb) \
67{ \
68 struct config_item_type *cit = &tb->tb_##_name##_cit; \
69 \
70 cit->ct_item_ops = _item_ops; \
71 cit->ct_group_ops = _group_ops; \
72 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
73 cit->ct_owner = tb->ops->owner; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080074 pr_debug("Setup generic %s\n", __stringify(_name)); \
75}
76
Andy Grovere3d6f902011-07-19 08:55:10 +000077extern struct t10_alua_lu_gp *default_lu_gp;
78
Roland Dreierd0f474e2012-01-12 10:41:18 -080079static LIST_HEAD(g_tf_list);
80static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081
82struct target_core_configfs_attribute {
83 struct configfs_attribute attr;
84 ssize_t (*show)(void *, char *);
85 ssize_t (*store)(void *, const char *, size_t);
86};
87
Andy Grovere3d6f902011-07-19 08:55:10 +000088static struct config_group target_core_hbagroup;
89static struct config_group alua_group;
90static struct config_group alua_lu_gps_group;
91
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080092static inline struct se_hba *
93item_to_hba(struct config_item *item)
94{
95 return container_of(to_config_group(item), struct se_hba, hba_group);
96}
97
98/*
99 * Attributes for /sys/kernel/config/target/
100 */
101static ssize_t target_core_attr_show(struct config_item *item,
102 struct configfs_attribute *attr,
103 char *page)
104{
105 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
106 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
107 utsname()->sysname, utsname()->machine);
108}
109
110static struct configfs_item_operations target_core_fabric_item_ops = {
111 .show_attribute = target_core_attr_show,
112};
113
114static struct configfs_attribute target_core_item_attr_version = {
115 .ca_owner = THIS_MODULE,
116 .ca_name = "version",
117 .ca_mode = S_IRUGO,
118};
119
120static struct target_fabric_configfs *target_core_get_fabric(
121 const char *name)
122{
123 struct target_fabric_configfs *tf;
124
Andy Grover6708bb22011-06-08 10:36:43 -0700125 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800126 return NULL;
127
128 mutex_lock(&g_tf_lock);
129 list_for_each_entry(tf, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200130 if (!strcmp(tf->tf_ops->name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800131 atomic_inc(&tf->tf_access_cnt);
132 mutex_unlock(&g_tf_lock);
133 return tf;
134 }
135 }
136 mutex_unlock(&g_tf_lock);
137
138 return NULL;
139}
140
141/*
142 * Called from struct target_core_group_ops->make_group()
143 */
144static struct config_group *target_core_register_fabric(
145 struct config_group *group,
146 const char *name)
147{
148 struct target_fabric_configfs *tf;
149 int ret;
150
Andy Grover6708bb22011-06-08 10:36:43 -0700151 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800152 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800153
154 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700155 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700156 pr_debug("target_core_register_fabric() trying autoload for %s\n",
157 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800158
159 /*
160 * Below are some hardcoded request_module() calls to automatically
161 * local fabric modules when the following is called:
162 *
163 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
164 *
165 * Note that this does not limit which TCM fabric module can be
166 * registered, but simply provids auto loading logic for modules with
167 * mkdir(2) system calls with known TCM fabric modules.
168 */
169
170 if (!strncmp(name, "iscsi", 5)) {
171 /*
172 * Automatically load the LIO Target fabric module when the
173 * following is called:
174 *
175 * mkdir -p $CONFIGFS/target/iscsi
176 */
177 ret = request_module("iscsi_target_mod");
178 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700179 pr_debug("request_module() failed for"
180 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800181 return ERR_PTR(-EINVAL);
182 }
183 } else if (!strncmp(name, "loopback", 8)) {
184 /*
185 * Automatically load the tcm_loop fabric module when the
186 * following is called:
187 *
188 * mkdir -p $CONFIGFS/target/loopback
189 */
190 ret = request_module("tcm_loop");
191 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700192 pr_debug("request_module() failed for"
193 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800194 return ERR_PTR(-EINVAL);
195 }
196 }
197
198 tf = target_core_get_fabric(name);
199 }
200
201 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700202 pr_debug("target_core_get_fabric() failed for %s\n",
203 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 return ERR_PTR(-EINVAL);
205 }
Andy Grover6708bb22011-06-08 10:36:43 -0700206 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200207 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208 /*
209 * On a successful target_core_get_fabric() look, the returned
210 * struct target_fabric_configfs *tf will contain a usage reference.
211 */
Andy Grover6708bb22011-06-08 10:36:43 -0700212 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200213 &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214
215 tf->tf_group.default_groups = tf->tf_default_groups;
216 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
217 tf->tf_group.default_groups[1] = NULL;
218
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200219 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200221 &tf->tf_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800222
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225 return &tf->tf_group;
226}
227
228/*
229 * Called from struct target_core_group_ops->drop_item()
230 */
231static void target_core_deregister_fabric(
232 struct config_group *group,
233 struct config_item *item)
234{
235 struct target_fabric_configfs *tf = container_of(
236 to_config_group(item), struct target_fabric_configfs, tf_group);
237 struct config_group *tf_group;
238 struct config_item *df_item;
239 int i;
240
Andy Grover6708bb22011-06-08 10:36:43 -0700241 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242 " tf list\n", config_item_name(item));
243
Andy Grover6708bb22011-06-08 10:36:43 -0700244 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200245 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800246 atomic_dec(&tf->tf_access_cnt);
247
Andy Grover6708bb22011-06-08 10:36:43 -0700248 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249 " %s\n", config_item_name(item));
250
251 tf_group = &tf->tf_group;
252 for (i = 0; tf_group->default_groups[i]; i++) {
253 df_item = &tf_group->default_groups[i]->cg_item;
254 tf_group->default_groups[i] = NULL;
255 config_item_put(df_item);
256 }
257 config_item_put(item);
258}
259
260static struct configfs_group_operations target_core_fabric_group_ops = {
261 .make_group = &target_core_register_fabric,
262 .drop_item = &target_core_deregister_fabric,
263};
264
265/*
266 * All item attributes appearing in /sys/kernel/target/ appear here.
267 */
268static struct configfs_attribute *target_core_fabric_item_attrs[] = {
269 &target_core_item_attr_version,
270 NULL,
271};
272
273/*
274 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
275 */
276static struct config_item_type target_core_fabrics_item = {
277 .ct_item_ops = &target_core_fabric_item_ops,
278 .ct_group_ops = &target_core_fabric_group_ops,
279 .ct_attrs = target_core_fabric_item_attrs,
280 .ct_owner = THIS_MODULE,
281};
282
283static struct configfs_subsystem target_core_fabrics = {
284 .su_group = {
285 .cg_item = {
286 .ci_namebuf = "target",
287 .ci_type = &target_core_fabrics_item,
288 },
289 },
290};
291
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200292int target_depend_item(struct config_item *item)
293{
294 return configfs_depend_item(&target_core_fabrics, item);
295}
296EXPORT_SYMBOL(target_depend_item);
297
298void target_undepend_item(struct config_item *item)
299{
300 return configfs_undepend_item(&target_core_fabrics, item);
301}
302EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303
304/*##############################################################################
305// Start functions called by external Target Fabrics Modules
306//############################################################################*/
307
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200308static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200310 if (!tfo->name) {
311 pr_err("Missing tfo->name\n");
312 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200314 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700315 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200316 "_NAME_SIZE\n", tfo->name);
317 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 }
Andy Grover6708bb22011-06-08 10:36:43 -0700319 if (!tfo->get_fabric_name) {
320 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800321 return -EINVAL;
322 }
Andy Grover6708bb22011-06-08 10:36:43 -0700323 if (!tfo->tpg_get_wwn) {
324 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 return -EINVAL;
326 }
Andy Grover6708bb22011-06-08 10:36:43 -0700327 if (!tfo->tpg_get_tag) {
328 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800329 return -EINVAL;
330 }
Andy Grover6708bb22011-06-08 10:36:43 -0700331 if (!tfo->tpg_check_demo_mode) {
332 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800333 return -EINVAL;
334 }
Andy Grover6708bb22011-06-08 10:36:43 -0700335 if (!tfo->tpg_check_demo_mode_cache) {
336 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800337 return -EINVAL;
338 }
Andy Grover6708bb22011-06-08 10:36:43 -0700339 if (!tfo->tpg_check_demo_mode_write_protect) {
340 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341 return -EINVAL;
342 }
Andy Grover6708bb22011-06-08 10:36:43 -0700343 if (!tfo->tpg_check_prod_mode_write_protect) {
344 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345 return -EINVAL;
346 }
Andy Grover6708bb22011-06-08 10:36:43 -0700347 if (!tfo->tpg_get_inst_index) {
348 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800349 return -EINVAL;
350 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400351 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700352 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800353 return -EINVAL;
354 }
Andy Grover6708bb22011-06-08 10:36:43 -0700355 if (!tfo->shutdown_session) {
356 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 return -EINVAL;
358 }
Andy Grover6708bb22011-06-08 10:36:43 -0700359 if (!tfo->close_session) {
360 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361 return -EINVAL;
362 }
Andy Grover6708bb22011-06-08 10:36:43 -0700363 if (!tfo->sess_get_index) {
364 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800365 return -EINVAL;
366 }
Andy Grover6708bb22011-06-08 10:36:43 -0700367 if (!tfo->write_pending) {
368 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369 return -EINVAL;
370 }
Andy Grover6708bb22011-06-08 10:36:43 -0700371 if (!tfo->write_pending_status) {
372 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 return -EINVAL;
374 }
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!tfo->set_default_node_attributes) {
376 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 return -EINVAL;
378 }
Andy Grover6708bb22011-06-08 10:36:43 -0700379 if (!tfo->get_cmd_state) {
380 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 return -EINVAL;
382 }
Andy Grover6708bb22011-06-08 10:36:43 -0700383 if (!tfo->queue_data_in) {
384 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800385 return -EINVAL;
386 }
Andy Grover6708bb22011-06-08 10:36:43 -0700387 if (!tfo->queue_status) {
388 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 return -EINVAL;
390 }
Andy Grover6708bb22011-06-08 10:36:43 -0700391 if (!tfo->queue_tm_rsp) {
392 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393 return -EINVAL;
394 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700395 if (!tfo->aborted_task) {
396 pr_err("Missing tfo->aborted_task()\n");
397 return -EINVAL;
398 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 /*
400 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
401 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
402 * target_core_fabric_configfs.c WWN+TPG group context code.
403 */
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->fabric_make_wwn) {
405 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->fabric_drop_wwn) {
409 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Andy Grover6708bb22011-06-08 10:36:43 -0700412 if (!tfo->fabric_make_tpg) {
413 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->fabric_drop_tpg) {
417 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
420
421 return 0;
422}
423
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200424int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200426 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 int ret;
428
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200429 ret = target_fabric_tf_ops_check(fo);
430 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 return ret;
432
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200433 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700434 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200435 pr_err("%s: could not allocate memory!\n", __func__);
436 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200438
439 INIT_LIST_HEAD(&tf->tf_list);
440 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200441 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200442 target_fabric_setup_cits(tf);
443
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200445 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 mutex_unlock(&g_tf_lock);
447
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200448 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200450EXPORT_SYMBOL(target_register_template);
451
452void target_unregister_template(const struct target_core_fabric_ops *fo)
453{
454 struct target_fabric_configfs *t;
455
456 mutex_lock(&g_tf_lock);
457 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200458 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200459 BUG_ON(atomic_read(&t->tf_access_cnt));
460 list_del(&t->tf_list);
461 kfree(t);
462 break;
463 }
464 }
465 mutex_unlock(&g_tf_lock);
466}
467EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468
469/*##############################################################################
470// Stop functions called by external Target Fabrics Modules
471//############################################################################*/
472
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800473/* Start functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
477
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478static struct configfs_item_operations target_core_dev_attrib_ops = {
479 .show_attribute = target_core_dev_attrib_attr_show,
480 .store_attribute = target_core_dev_attrib_attr_store,
481};
482
Christoph Hellwig0a06d432015-05-10 18:14:56 +0200483TB_CIT_SETUP_DRV(dev_attrib, &target_core_dev_attrib_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800485/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800487/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488
489CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
490#define SE_DEV_WWN_ATTR(_name, _mode) \
491static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
492 __CONFIGFS_EATTR(_name, _mode, \
493 target_core_dev_wwn_show_attr_##_name, \
494 target_core_dev_wwn_store_attr_##_name);
495
496#define SE_DEV_WWN_ATTR_RO(_name); \
497do { \
498 static struct target_core_dev_wwn_attribute \
499 target_core_dev_wwn_##_name = \
500 __CONFIGFS_EATTR_RO(_name, \
501 target_core_dev_wwn_show_attr_##_name); \
502} while (0);
503
504/*
505 * VPD page 0x80 Unit serial
506 */
507static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
508 struct t10_wwn *t10_wwn,
509 char *page)
510{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
512 &t10_wwn->unit_serial[0]);
513}
514
515static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
516 struct t10_wwn *t10_wwn,
517 const char *page,
518 size_t count)
519{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400520 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
522
523 /*
524 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
525 * from the struct scsi_device level firmware, do not allow
526 * VPD Unit Serial to be emulated.
527 *
528 * Note this struct scsi_device could also be emulating VPD
529 * information from its drivers/scsi LLD. But for now we assume
530 * it is doing 'the right thing' wrt a world wide unique
531 * VPD Unit Serial Number that OS dependent multipath can depend on.
532 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400533 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700534 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 " Unit Serial, ignoring request\n");
536 return -EOPNOTSUPP;
537 }
538
Dan Carpenter60d645a2011-06-15 10:03:05 -0700539 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700540 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
542 return -EOVERFLOW;
543 }
544 /*
545 * Check to see if any active $FABRIC_MOD exports exist. If they
546 * do exist, fail here as changing this information on the fly
547 * (underneath the initiator side OS dependent multipath code)
548 * could cause negative effects.
549 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400550 if (dev->export_count) {
551 pr_err("Unable to set VPD Unit Serial while"
552 " active %d $FABRIC_MOD exports exist\n",
553 dev->export_count);
554 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400556
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 /*
558 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
559 *
560 * Also, strip any newline added from the userspace
561 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
562 */
563 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
564 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400565 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400567 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800568
Andy Grover6708bb22011-06-08 10:36:43 -0700569 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400570 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800571
572 return count;
573}
574
575SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
576
577/*
578 * VPD page 0x83 Protocol Identifier
579 */
580static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
581 struct t10_wwn *t10_wwn,
582 char *page)
583{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 struct t10_vpd *vpd;
585 unsigned char buf[VPD_TMP_BUF_SIZE];
586 ssize_t len = 0;
587
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 memset(buf, 0, VPD_TMP_BUF_SIZE);
589
590 spin_lock(&t10_wwn->t10_vpd_lock);
591 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700592 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800593 continue;
594
595 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
596
Andy Grover6708bb22011-06-08 10:36:43 -0700597 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598 break;
599
600 len += sprintf(page+len, "%s", buf);
601 }
602 spin_unlock(&t10_wwn->t10_vpd_lock);
603
604 return len;
605}
606
607static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
608 struct t10_wwn *t10_wwn,
609 const char *page,
610 size_t count)
611{
612 return -ENOSYS;
613}
614
615SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
616
617/*
618 * Generic wrapper for dumping VPD identifiers by association.
619 */
620#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
621static ssize_t target_core_dev_wwn_show_attr_##_name( \
622 struct t10_wwn *t10_wwn, \
623 char *page) \
624{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 struct t10_vpd *vpd; \
626 unsigned char buf[VPD_TMP_BUF_SIZE]; \
627 ssize_t len = 0; \
628 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629 spin_lock(&t10_wwn->t10_vpd_lock); \
630 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
631 if (vpd->association != _assoc) \
632 continue; \
633 \
634 memset(buf, 0, VPD_TMP_BUF_SIZE); \
635 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700636 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800637 break; \
638 len += sprintf(page+len, "%s", buf); \
639 \
640 memset(buf, 0, VPD_TMP_BUF_SIZE); \
641 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700642 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 break; \
644 len += sprintf(page+len, "%s", buf); \
645 \
646 memset(buf, 0, VPD_TMP_BUF_SIZE); \
647 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700648 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 break; \
650 len += sprintf(page+len, "%s", buf); \
651 } \
652 spin_unlock(&t10_wwn->t10_vpd_lock); \
653 \
654 return len; \
655}
656
657/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700658 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659 */
660DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
661
662static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
663 struct t10_wwn *t10_wwn,
664 const char *page,
665 size_t count)
666{
667 return -ENOSYS;
668}
669
670SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
671
672/*
673 * VPD page 0x83 Association: Target Port
674 */
675DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
676
677static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
678 struct t10_wwn *t10_wwn,
679 const char *page,
680 size_t count)
681{
682 return -ENOSYS;
683}
684
685SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
686
687/*
688 * VPD page 0x83 Association: SCSI Target Device
689 */
690DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
691
692static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
693 struct t10_wwn *t10_wwn,
694 const char *page,
695 size_t count)
696{
697 return -ENOSYS;
698}
699
700SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
701
702CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
703
704static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
705 &target_core_dev_wwn_vpd_unit_serial.attr,
706 &target_core_dev_wwn_vpd_protocol_identifier.attr,
707 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
708 &target_core_dev_wwn_vpd_assoc_target_port.attr,
709 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
710 NULL,
711};
712
713static struct configfs_item_operations target_core_dev_wwn_ops = {
714 .show_attribute = target_core_dev_wwn_attr_show,
715 .store_attribute = target_core_dev_wwn_attr_store,
716};
717
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800718TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800720/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721
Nicholas Bellinger91e2e392014-11-27 14:57:01 -0800722/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400724CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800725#define SE_DEV_PR_ATTR(_name, _mode) \
726static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
727 __CONFIGFS_EATTR(_name, _mode, \
728 target_core_dev_pr_show_attr_##_name, \
729 target_core_dev_pr_store_attr_##_name);
730
731#define SE_DEV_PR_ATTR_RO(_name); \
732static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
733 __CONFIGFS_EATTR_RO(_name, \
734 target_core_dev_pr_show_attr_##_name);
735
Christoph Hellwigd977f432012-10-10 17:37:15 -0400736static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
737 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800738{
739 struct se_node_acl *se_nacl;
740 struct t10_pr_registration *pr_reg;
741 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742
743 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
744
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800745 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400746 if (!pr_reg)
747 return sprintf(page, "No SPC-3 Reservation holder\n");
748
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -0700750 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751
Christoph Hellwigd977f432012-10-10 17:37:15 -0400752 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000753 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700754 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755}
756
Christoph Hellwigd977f432012-10-10 17:37:15 -0400757static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
758 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759{
760 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400761 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800763 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400764 if (se_nacl) {
765 len = sprintf(page,
766 "SPC-2 Reservation: %s Initiator: %s\n",
767 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
768 se_nacl->initiatorname);
769 } else {
770 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800771 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400772 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800773}
774
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400775static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
776 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800777{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400778 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779
Andy Grovera3541702015-05-19 14:44:41 -0700780 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400781 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800782
Christoph Hellwigd977f432012-10-10 17:37:15 -0400783 spin_lock(&dev->dev_reservation_lock);
784 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
785 ret = target_core_dev_pr_show_spc2_res(dev, page);
786 else
787 ret = target_core_dev_pr_show_spc3_res(dev, page);
788 spin_unlock(&dev->dev_reservation_lock);
789 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800790}
791
792SE_DEV_PR_ATTR_RO(res_holder);
793
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800794static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400795 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800797 ssize_t len = 0;
798
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -0400800 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400802 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800803 len = sprintf(page, "SPC-3 Reservation: All Target"
804 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400805 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806 len = sprintf(page, "SPC-3 Reservation: Single"
807 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400808 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809
Christoph Hellwigd977f432012-10-10 17:37:15 -0400810 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800811 return len;
812}
813
814SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
815
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400817 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800818{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400819 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820}
821
822SE_DEV_PR_ATTR_RO(res_pr_generation);
823
824/*
825 * res_pr_holder_tg_port
826 */
827static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400828 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800830 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 struct se_portal_group *se_tpg;
832 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200833 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834 ssize_t len = 0;
835
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 spin_lock(&dev->dev_reservation_lock);
837 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -0700838 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -0400840 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800841 }
Christoph Hellwigd977f432012-10-10 17:37:15 -0400842
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 se_nacl = pr_reg->pr_reg_nacl;
844 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +0000845 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846
847 len += sprintf(page+len, "SPC-3 Reservation: %s"
848 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
849 tfo->tpg_get_wwn(se_tpg));
850 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +0900851 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000852 " %s Logical Unit: %u\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000854 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800855
Christoph Hellwigd977f432012-10-10 17:37:15 -0400856out_unlock:
857 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858 return len;
859}
860
861SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
862
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400864 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800865{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200866 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800867 struct t10_pr_registration *pr_reg;
868 unsigned char buf[384];
869 char i_buf[PR_REG_ISID_ID_LEN];
870 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -0700871 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800872
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
874
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400875 spin_lock(&dev->t10_pr.registration_lock);
876 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800877 pr_reg_list) {
878
879 memset(buf, 0, 384);
880 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
881 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -0700882 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800883 PR_REG_ISID_ID_LEN);
884 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
885 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -0700886 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887 pr_reg->pr_res_generation);
888
Andy Grover6708bb22011-06-08 10:36:43 -0700889 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800890 break;
891
892 len += sprintf(page+len, "%s", buf);
893 reg_count++;
894 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400895 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896
Andy Grover6708bb22011-06-08 10:36:43 -0700897 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800898 len += sprintf(page+len, "None\n");
899
900 return len;
901}
902
903SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
904
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800905static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400906 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 struct t10_pr_registration *pr_reg;
909 ssize_t len = 0;
910
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 spin_lock(&dev->dev_reservation_lock);
912 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400913 if (pr_reg) {
914 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
915 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
916 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800917 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800918 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800919
Christoph Hellwigd977f432012-10-10 17:37:15 -0400920 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800921 return len;
922}
923
924SE_DEV_PR_ATTR_RO(res_pr_type);
925
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800926static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400927 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800928{
Andy Grovera3541702015-05-19 14:44:41 -0700929 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400930 return sprintf(page, "SPC_PASSTHROUGH\n");
931 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
932 return sprintf(page, "SPC2_RESERVATIONS\n");
933 else
934 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800935}
936
937SE_DEV_PR_ATTR_RO(res_type);
938
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800939static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400940 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800941{
Andy Grovera3541702015-05-19 14:44:41 -0700942 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943 return 0;
944
945 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400946 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947}
948
949SE_DEV_PR_ATTR_RO(res_aptpl_active);
950
951/*
952 * res_aptpl_metadata
953 */
954static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400955 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800956{
Andy Grovera3541702015-05-19 14:44:41 -0700957 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 return 0;
959
960 return sprintf(page, "Ready to process PR APTPL metadata..\n");
961}
962
963enum {
964 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
965 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
966 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
967 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
968};
969
970static match_table_t tokens = {
971 {Opt_initiator_fabric, "initiator_fabric=%s"},
972 {Opt_initiator_node, "initiator_node=%s"},
973 {Opt_initiator_sid, "initiator_sid=%s"},
974 {Opt_sa_res_key, "sa_res_key=%s"},
975 {Opt_res_holder, "res_holder=%d"},
976 {Opt_res_type, "res_type=%d"},
977 {Opt_res_scope, "res_scope=%d"},
978 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
979 {Opt_mapped_lun, "mapped_lun=%d"},
980 {Opt_target_fabric, "target_fabric=%s"},
981 {Opt_target_node, "target_node=%s"},
982 {Opt_tpgt, "tpgt=%d"},
983 {Opt_port_rtpi, "port_rtpi=%d"},
984 {Opt_target_lun, "target_lun=%d"},
985 {Opt_err, NULL}
986};
987
988static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400989 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800990 const char *page,
991 size_t count)
992{
Jesper Juhl6d180252011-03-14 04:05:56 -0700993 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
994 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -0400995 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800996 substring_t args[MAX_OPT_ARGS];
997 unsigned long long tmp_ll;
998 u64 sa_res_key = 0;
999 u32 mapped_lun = 0, target_lun = 0;
1000 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001001 u16 tpgt = 0;
1002 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001003
Andy Grovera3541702015-05-19 14:44:41 -07001004 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001005 return 0;
1006 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007 return 0;
1008
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001009 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001010 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011 " active fabric exports exist\n");
1012 return -EINVAL;
1013 }
1014
1015 opts = kstrdup(page, GFP_KERNEL);
1016 if (!opts)
1017 return -ENOMEM;
1018
1019 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001020 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001021 if (!*ptr)
1022 continue;
1023
1024 token = match_token(ptr, tokens, args);
1025 switch (token) {
1026 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001027 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001028 if (!i_fabric) {
1029 ret = -ENOMEM;
1030 goto out;
1031 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 break;
1033 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001034 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001035 if (!i_port) {
1036 ret = -ENOMEM;
1037 goto out;
1038 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001039 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001040 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1042 PR_APTPL_MAX_IPORT_LEN);
1043 ret = -EINVAL;
1044 break;
1045 }
1046 break;
1047 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001048 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001049 if (!isid) {
1050 ret = -ENOMEM;
1051 goto out;
1052 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001053 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001054 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055 "= exceeds PR_REG_ISID_LEN: %d\n",
1056 PR_REG_ISID_LEN);
1057 ret = -EINVAL;
1058 break;
1059 }
1060 break;
1061 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001062 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001064 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065 goto out;
1066 }
1067 sa_res_key = (u64)tmp_ll;
1068 break;
1069 /*
1070 * PR APTPL Metadata for Reservation
1071 */
1072 case Opt_res_holder:
1073 match_int(args, &arg);
1074 res_holder = arg;
1075 break;
1076 case Opt_res_type:
1077 match_int(args, &arg);
1078 type = (u8)arg;
1079 break;
1080 case Opt_res_scope:
1081 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 break;
1083 case Opt_res_all_tg_pt:
1084 match_int(args, &arg);
1085 all_tg_pt = (int)arg;
1086 break;
1087 case Opt_mapped_lun:
1088 match_int(args, &arg);
1089 mapped_lun = (u32)arg;
1090 break;
1091 /*
1092 * PR APTPL Metadata for Target Port
1093 */
1094 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001095 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001096 if (!t_fabric) {
1097 ret = -ENOMEM;
1098 goto out;
1099 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100 break;
1101 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001102 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001103 if (!t_port) {
1104 ret = -ENOMEM;
1105 goto out;
1106 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001107 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001108 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1110 PR_APTPL_MAX_TPORT_LEN);
1111 ret = -EINVAL;
1112 break;
1113 }
1114 break;
1115 case Opt_tpgt:
1116 match_int(args, &arg);
1117 tpgt = (u16)arg;
1118 break;
1119 case Opt_port_rtpi:
1120 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121 break;
1122 case Opt_target_lun:
1123 match_int(args, &arg);
1124 target_lun = (u32)arg;
1125 break;
1126 default:
1127 break;
1128 }
1129 }
1130
Andy Grover6708bb22011-06-08 10:36:43 -07001131 if (!i_port || !t_port || !sa_res_key) {
1132 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133 ret = -EINVAL;
1134 goto out;
1135 }
1136
1137 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001138 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 " holder\n", type);
1140 ret = -EINVAL;
1141 goto out;
1142 }
1143
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001144 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1146 res_holder, all_tg_pt, type);
1147out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001148 kfree(i_fabric);
1149 kfree(i_port);
1150 kfree(isid);
1151 kfree(t_fabric);
1152 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 kfree(orig);
1154 return (ret == 0) ? count : ret;
1155}
1156
1157SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1158
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001159CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160
1161static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1162 &target_core_dev_pr_res_holder.attr,
1163 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1164 &target_core_dev_pr_res_pr_generation.attr,
1165 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1166 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1167 &target_core_dev_pr_res_pr_type.attr,
1168 &target_core_dev_pr_res_type.attr,
1169 &target_core_dev_pr_res_aptpl_active.attr,
1170 &target_core_dev_pr_res_aptpl_metadata.attr,
1171 NULL,
1172};
1173
1174static struct configfs_item_operations target_core_dev_pr_ops = {
1175 .show_attribute = target_core_dev_pr_attr_show,
1176 .store_attribute = target_core_dev_pr_attr_store,
1177};
1178
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001179TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001181/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001183/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184
1185static ssize_t target_core_show_dev_info(void *p, char *page)
1186{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001187 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 int bl = 0;
1189 ssize_t read_bytes = 0;
1190
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001191 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001193 read_bytes += dev->transport->show_configfs_dev_params(dev,
1194 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195 return read_bytes;
1196}
1197
1198static struct target_core_configfs_attribute target_core_attr_dev_info = {
1199 .attr = { .ca_owner = THIS_MODULE,
1200 .ca_name = "info",
1201 .ca_mode = S_IRUGO },
1202 .show = target_core_show_dev_info,
1203 .store = NULL,
1204};
1205
1206static ssize_t target_core_store_dev_control(
1207 void *p,
1208 const char *page,
1209 size_t count)
1210{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001211 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001213 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214}
1215
1216static struct target_core_configfs_attribute target_core_attr_dev_control = {
1217 .attr = { .ca_owner = THIS_MODULE,
1218 .ca_name = "control",
1219 .ca_mode = S_IWUSR },
1220 .show = NULL,
1221 .store = target_core_store_dev_control,
1222};
1223
1224static ssize_t target_core_show_dev_alias(void *p, char *page)
1225{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001226 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001228 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 return 0;
1230
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001231 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232}
1233
1234static ssize_t target_core_store_dev_alias(
1235 void *p,
1236 const char *page,
1237 size_t count)
1238{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001239 struct se_device *dev = p;
1240 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241 ssize_t read_bytes;
1242
1243 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001244 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1246 SE_DEV_ALIAS_LEN-1);
1247 return -EINVAL;
1248 }
1249
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001250 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001251 if (!read_bytes)
1252 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001253 if (dev->dev_alias[read_bytes - 1] == '\n')
1254 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001255
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001256 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001257
Andy Grover6708bb22011-06-08 10:36:43 -07001258 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001260 config_item_name(&dev->dev_group.cg_item),
1261 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262
1263 return read_bytes;
1264}
1265
1266static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1267 .attr = { .ca_owner = THIS_MODULE,
1268 .ca_name = "alias",
1269 .ca_mode = S_IRUGO | S_IWUSR },
1270 .show = target_core_show_dev_alias,
1271 .store = target_core_store_dev_alias,
1272};
1273
1274static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1275{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001276 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001278 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279 return 0;
1280
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001281 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282}
1283
1284static ssize_t target_core_store_dev_udev_path(
1285 void *p,
1286 const char *page,
1287 size_t count)
1288{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001289 struct se_device *dev = p;
1290 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 ssize_t read_bytes;
1292
1293 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001294 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1296 SE_UDEV_PATH_LEN-1);
1297 return -EINVAL;
1298 }
1299
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001300 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001302 if (!read_bytes)
1303 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001304 if (dev->udev_path[read_bytes - 1] == '\n')
1305 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001306
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001307 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001308
Andy Grover6708bb22011-06-08 10:36:43 -07001309 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001311 config_item_name(&dev->dev_group.cg_item),
1312 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313
1314 return read_bytes;
1315}
1316
1317static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1318 .attr = { .ca_owner = THIS_MODULE,
1319 .ca_name = "udev_path",
1320 .ca_mode = S_IRUGO | S_IWUSR },
1321 .show = target_core_show_dev_udev_path,
1322 .store = target_core_store_dev_udev_path,
1323};
1324
Andy Grover64146db2013-04-30 11:59:15 -07001325static ssize_t target_core_show_dev_enable(void *p, char *page)
1326{
1327 struct se_device *dev = p;
1328
1329 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1330}
1331
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001332static ssize_t target_core_store_dev_enable(
1333 void *p,
1334 const char *page,
1335 size_t count)
1336{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001337 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001339 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340
1341 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001342 if (!ptr) {
1343 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344 " is \"1\"\n");
1345 return -EINVAL;
1346 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001348 ret = target_configure_device(dev);
1349 if (ret)
1350 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 return count;
1352}
1353
1354static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1355 .attr = { .ca_owner = THIS_MODULE,
1356 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001357 .ca_mode = S_IRUGO | S_IWUSR },
1358 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359 .store = target_core_store_dev_enable,
1360};
1361
1362static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1363{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001364 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 struct config_item *lu_ci;
1366 struct t10_alua_lu_gp *lu_gp;
1367 struct t10_alua_lu_gp_member *lu_gp_mem;
1368 ssize_t len = 0;
1369
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001371 if (!lu_gp_mem)
1372 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001373
1374 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1375 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001376 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377 lu_ci = &lu_gp->lu_gp_group.cg_item;
1378 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1379 config_item_name(lu_ci), lu_gp->lu_gp_id);
1380 }
1381 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1382
1383 return len;
1384}
1385
1386static ssize_t target_core_store_alua_lu_gp(
1387 void *p,
1388 const char *page,
1389 size_t count)
1390{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001391 struct se_device *dev = p;
1392 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1394 struct t10_alua_lu_gp_member *lu_gp_mem;
1395 unsigned char buf[LU_GROUP_NAME_BUF];
1396 int move = 0;
1397
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001398 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1399 if (!lu_gp_mem)
1400 return 0;
1401
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001403 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 return -EINVAL;
1405 }
1406 memset(buf, 0, LU_GROUP_NAME_BUF);
1407 memcpy(buf, page, count);
1408 /*
1409 * Any ALUA logical unit alias besides "NULL" means we will be
1410 * making a new group association.
1411 */
1412 if (strcmp(strstrip(buf), "NULL")) {
1413 /*
1414 * core_alua_get_lu_gp_by_name() will increment reference to
1415 * struct t10_alua_lu_gp. This reference is released with
1416 * core_alua_get_lu_gp_by_name below().
1417 */
1418 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001419 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420 return -ENODEV;
1421 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001422
1423 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1424 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001425 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 /*
1427 * Clearing an existing lu_gp association, and replacing
1428 * with NULL
1429 */
Andy Grover6708bb22011-06-08 10:36:43 -07001430 if (!lu_gp_new) {
1431 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1433 " %hu\n",
1434 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001435 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 config_item_name(&lu_gp->lu_gp_group.cg_item),
1437 lu_gp->lu_gp_id);
1438
1439 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1440 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1441
1442 return count;
1443 }
1444 /*
1445 * Removing existing association of lu_gp_mem with lu_gp
1446 */
1447 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1448 move = 1;
1449 }
1450 /*
1451 * Associate lu_gp_mem with lu_gp_new.
1452 */
1453 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1454 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1455
Andy Grover6708bb22011-06-08 10:36:43 -07001456 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457 " core/alua/lu_gps/%s, ID: %hu\n",
1458 (move) ? "Moving" : "Adding",
1459 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001460 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1462 lu_gp_new->lu_gp_id);
1463
1464 core_alua_put_lu_gp_from_name(lu_gp_new);
1465 return count;
1466}
1467
1468static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1469 .attr = { .ca_owner = THIS_MODULE,
1470 .ca_name = "alua_lu_gp",
1471 .ca_mode = S_IRUGO | S_IWUSR },
1472 .show = target_core_show_alua_lu_gp,
1473 .store = target_core_store_alua_lu_gp,
1474};
1475
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001476static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1477{
1478 struct se_device *dev = p;
1479 struct t10_alua_lba_map *map;
1480 struct t10_alua_lba_map_member *mem;
1481 char *b = page;
1482 int bl = 0;
1483 char state;
1484
1485 spin_lock(&dev->t10_alua.lba_map_lock);
1486 if (!list_empty(&dev->t10_alua.lba_map_list))
1487 bl += sprintf(b + bl, "%u %u\n",
1488 dev->t10_alua.lba_map_segment_size,
1489 dev->t10_alua.lba_map_segment_multiplier);
1490 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1491 bl += sprintf(b + bl, "%llu %llu",
1492 map->lba_map_first_lba, map->lba_map_last_lba);
1493 list_for_each_entry(mem, &map->lba_map_mem_list,
1494 lba_map_mem_list) {
1495 switch (mem->lba_map_mem_alua_state) {
1496 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1497 state = 'O';
1498 break;
1499 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1500 state = 'A';
1501 break;
1502 case ALUA_ACCESS_STATE_STANDBY:
1503 state = 'S';
1504 break;
1505 case ALUA_ACCESS_STATE_UNAVAILABLE:
1506 state = 'U';
1507 break;
1508 default:
1509 state = '.';
1510 break;
1511 }
1512 bl += sprintf(b + bl, " %d:%c",
1513 mem->lba_map_mem_alua_pg_id, state);
1514 }
1515 bl += sprintf(b + bl, "\n");
1516 }
1517 spin_unlock(&dev->t10_alua.lba_map_lock);
1518 return bl;
1519}
1520
1521static ssize_t target_core_store_dev_lba_map(
1522 void *p,
1523 const char *page,
1524 size_t count)
1525{
1526 struct se_device *dev = p;
1527 struct t10_alua_lba_map *lba_map = NULL;
1528 struct list_head lba_list;
1529 char *map_entries, *ptr;
1530 char state;
1531 int pg_num = -1, pg;
1532 int ret = 0, num = 0, pg_id, alua_state;
1533 unsigned long start_lba = -1, end_lba = -1;
1534 unsigned long segment_size = -1, segment_mult = -1;
1535
1536 map_entries = kstrdup(page, GFP_KERNEL);
1537 if (!map_entries)
1538 return -ENOMEM;
1539
1540 INIT_LIST_HEAD(&lba_list);
1541 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1542 if (!*ptr)
1543 continue;
1544
1545 if (num == 0) {
1546 if (sscanf(ptr, "%lu %lu\n",
1547 &segment_size, &segment_mult) != 2) {
1548 pr_err("Invalid line %d\n", num);
1549 ret = -EINVAL;
1550 break;
1551 }
1552 num++;
1553 continue;
1554 }
1555 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1556 pr_err("Invalid line %d\n", num);
1557 ret = -EINVAL;
1558 break;
1559 }
1560 ptr = strchr(ptr, ' ');
1561 if (!ptr) {
1562 pr_err("Invalid line %d, missing end lba\n", num);
1563 ret = -EINVAL;
1564 break;
1565 }
1566 ptr++;
1567 ptr = strchr(ptr, ' ');
1568 if (!ptr) {
1569 pr_err("Invalid line %d, missing state definitions\n",
1570 num);
1571 ret = -EINVAL;
1572 break;
1573 }
1574 ptr++;
1575 lba_map = core_alua_allocate_lba_map(&lba_list,
1576 start_lba, end_lba);
1577 if (IS_ERR(lba_map)) {
1578 ret = PTR_ERR(lba_map);
1579 break;
1580 }
1581 pg = 0;
1582 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1583 switch (state) {
1584 case 'O':
1585 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1586 break;
1587 case 'A':
1588 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1589 break;
1590 case 'S':
1591 alua_state = ALUA_ACCESS_STATE_STANDBY;
1592 break;
1593 case 'U':
1594 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1595 break;
1596 default:
1597 pr_err("Invalid ALUA state '%c'\n", state);
1598 ret = -EINVAL;
1599 goto out;
1600 }
1601
1602 ret = core_alua_allocate_lba_map_mem(lba_map,
1603 pg_id, alua_state);
1604 if (ret) {
1605 pr_err("Invalid target descriptor %d:%c "
1606 "at line %d\n",
1607 pg_id, state, num);
1608 break;
1609 }
1610 pg++;
1611 ptr = strchr(ptr, ' ');
1612 if (ptr)
1613 ptr++;
1614 else
1615 break;
1616 }
1617 if (pg_num == -1)
1618 pg_num = pg;
1619 else if (pg != pg_num) {
1620 pr_err("Only %d from %d port groups definitions "
1621 "at line %d\n", pg, pg_num, num);
1622 ret = -EINVAL;
1623 break;
1624 }
1625 num++;
1626 }
1627out:
1628 if (ret) {
1629 core_alua_free_lba_map(&lba_list);
1630 count = ret;
1631 } else
1632 core_alua_set_lba_map(dev, &lba_list,
1633 segment_size, segment_mult);
1634 kfree(map_entries);
1635 return count;
1636}
1637
1638static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1639 .attr = { .ca_owner = THIS_MODULE,
1640 .ca_name = "lba_map",
1641 .ca_mode = S_IRUGO | S_IWUSR },
1642 .show = target_core_show_dev_lba_map,
1643 .store = target_core_store_dev_lba_map,
1644};
1645
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001646static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 &target_core_attr_dev_info.attr,
1648 &target_core_attr_dev_control.attr,
1649 &target_core_attr_dev_alias.attr,
1650 &target_core_attr_dev_udev_path.attr,
1651 &target_core_attr_dev_enable.attr,
1652 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001653 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 NULL,
1655};
1656
1657static void target_core_dev_release(struct config_item *item)
1658{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001659 struct config_group *dev_cg = to_config_group(item);
1660 struct se_device *dev =
1661 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001664 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001665}
1666
1667static ssize_t target_core_dev_show(struct config_item *item,
1668 struct configfs_attribute *attr,
1669 char *page)
1670{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001671 struct config_group *dev_cg = to_config_group(item);
1672 struct se_device *dev =
1673 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674 struct target_core_configfs_attribute *tc_attr = container_of(
1675 attr, struct target_core_configfs_attribute, attr);
1676
Andy Grover6708bb22011-06-08 10:36:43 -07001677 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001678 return -EINVAL;
1679
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001680 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001681}
1682
1683static ssize_t target_core_dev_store(struct config_item *item,
1684 struct configfs_attribute *attr,
1685 const char *page, size_t count)
1686{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001687 struct config_group *dev_cg = to_config_group(item);
1688 struct se_device *dev =
1689 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 struct target_core_configfs_attribute *tc_attr = container_of(
1691 attr, struct target_core_configfs_attribute, attr);
1692
Andy Grover6708bb22011-06-08 10:36:43 -07001693 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001694 return -EINVAL;
1695
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001696 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697}
1698
1699static struct configfs_item_operations target_core_dev_item_ops = {
1700 .release = target_core_dev_release,
1701 .show_attribute = target_core_dev_show,
1702 .store_attribute = target_core_dev_store,
1703};
1704
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001705TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001707/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001708
1709/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1710
1711CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1712#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1713static struct target_core_alua_lu_gp_attribute \
1714 target_core_alua_lu_gp_##_name = \
1715 __CONFIGFS_EATTR(_name, _mode, \
1716 target_core_alua_lu_gp_show_attr_##_name, \
1717 target_core_alua_lu_gp_store_attr_##_name);
1718
1719#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1720static struct target_core_alua_lu_gp_attribute \
1721 target_core_alua_lu_gp_##_name = \
1722 __CONFIGFS_EATTR_RO(_name, \
1723 target_core_alua_lu_gp_show_attr_##_name);
1724
1725/*
1726 * lu_gp_id
1727 */
1728static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1729 struct t10_alua_lu_gp *lu_gp,
1730 char *page)
1731{
Andy Grover6708bb22011-06-08 10:36:43 -07001732 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 return 0;
1734
1735 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1736}
1737
1738static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1739 struct t10_alua_lu_gp *lu_gp,
1740 const char *page,
1741 size_t count)
1742{
1743 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1744 unsigned long lu_gp_id;
1745 int ret;
1746
Jingoo Han57103d72013-07-19 16:22:19 +09001747 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09001749 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09001751 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752 }
1753 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001754 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755 " 0x0000ffff\n", lu_gp_id);
1756 return -EINVAL;
1757 }
1758
1759 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1760 if (ret < 0)
1761 return -EINVAL;
1762
Andy Grover6708bb22011-06-08 10:36:43 -07001763 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1765 config_item_name(&alua_lu_gp_cg->cg_item),
1766 lu_gp->lu_gp_id);
1767
1768 return count;
1769}
1770
1771SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1772
1773/*
1774 * members
1775 */
1776static ssize_t target_core_alua_lu_gp_show_attr_members(
1777 struct t10_alua_lu_gp *lu_gp,
1778 char *page)
1779{
1780 struct se_device *dev;
1781 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782 struct t10_alua_lu_gp_member *lu_gp_mem;
1783 ssize_t len = 0, cur_len;
1784 unsigned char buf[LU_GROUP_NAME_BUF];
1785
1786 memset(buf, 0, LU_GROUP_NAME_BUF);
1787
1788 spin_lock(&lu_gp->lu_gp_lock);
1789 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1790 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001791 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792
1793 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1794 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001795 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001796 cur_len++; /* Extra byte for NULL terminator */
1797
1798 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001799 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001800 "_members buffer\n");
1801 break;
1802 }
1803 memcpy(page+len, buf, cur_len);
1804 len += cur_len;
1805 }
1806 spin_unlock(&lu_gp->lu_gp_lock);
1807
1808 return len;
1809}
1810
1811SE_DEV_ALUA_LU_ATTR_RO(members);
1812
1813CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1814
1815static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1816 &target_core_alua_lu_gp_lu_gp_id.attr,
1817 &target_core_alua_lu_gp_members.attr,
1818 NULL,
1819};
1820
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001821static void target_core_alua_lu_gp_release(struct config_item *item)
1822{
1823 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1824 struct t10_alua_lu_gp, lu_gp_group);
1825
1826 core_alua_free_lu_gp(lu_gp);
1827}
1828
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001830 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 .show_attribute = target_core_alua_lu_gp_attr_show,
1832 .store_attribute = target_core_alua_lu_gp_attr_store,
1833};
1834
1835static struct config_item_type target_core_alua_lu_gp_cit = {
1836 .ct_item_ops = &target_core_alua_lu_gp_ops,
1837 .ct_attrs = target_core_alua_lu_gp_attrs,
1838 .ct_owner = THIS_MODULE,
1839};
1840
1841/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1842
1843/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1844
1845static struct config_group *target_core_alua_create_lu_gp(
1846 struct config_group *group,
1847 const char *name)
1848{
1849 struct t10_alua_lu_gp *lu_gp;
1850 struct config_group *alua_lu_gp_cg = NULL;
1851 struct config_item *alua_lu_gp_ci = NULL;
1852
1853 lu_gp = core_alua_allocate_lu_gp(name, 0);
1854 if (IS_ERR(lu_gp))
1855 return NULL;
1856
1857 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1858 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1859
1860 config_group_init_type_name(alua_lu_gp_cg, name,
1861 &target_core_alua_lu_gp_cit);
1862
Andy Grover6708bb22011-06-08 10:36:43 -07001863 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001864 " Group: core/alua/lu_gps/%s\n",
1865 config_item_name(alua_lu_gp_ci));
1866
1867 return alua_lu_gp_cg;
1868
1869}
1870
1871static void target_core_alua_drop_lu_gp(
1872 struct config_group *group,
1873 struct config_item *item)
1874{
1875 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1876 struct t10_alua_lu_gp, lu_gp_group);
1877
Andy Grover6708bb22011-06-08 10:36:43 -07001878 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1880 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001881 /*
1882 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1883 * -> target_core_alua_lu_gp_release()
1884 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886}
1887
1888static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1889 .make_group = &target_core_alua_create_lu_gp,
1890 .drop_item = &target_core_alua_drop_lu_gp,
1891};
1892
1893static struct config_item_type target_core_alua_lu_gps_cit = {
1894 .ct_item_ops = NULL,
1895 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1896 .ct_owner = THIS_MODULE,
1897};
1898
1899/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1900
1901/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1902
1903CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1904#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1905static struct target_core_alua_tg_pt_gp_attribute \
1906 target_core_alua_tg_pt_gp_##_name = \
1907 __CONFIGFS_EATTR(_name, _mode, \
1908 target_core_alua_tg_pt_gp_show_attr_##_name, \
1909 target_core_alua_tg_pt_gp_store_attr_##_name);
1910
1911#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1912static struct target_core_alua_tg_pt_gp_attribute \
1913 target_core_alua_tg_pt_gp_##_name = \
1914 __CONFIGFS_EATTR_RO(_name, \
1915 target_core_alua_tg_pt_gp_show_attr_##_name);
1916
1917/*
1918 * alua_access_state
1919 */
1920static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1921 struct t10_alua_tg_pt_gp *tg_pt_gp,
1922 char *page)
1923{
1924 return sprintf(page, "%d\n",
1925 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1926}
1927
1928static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1929 struct t10_alua_tg_pt_gp *tg_pt_gp,
1930 const char *page,
1931 size_t count)
1932{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001933 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001934 unsigned long tmp;
1935 int new_state, ret;
1936
Andy Grover6708bb22011-06-08 10:36:43 -07001937 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01001938 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1940 return -EINVAL;
1941 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07001942 if (!(dev->dev_flags & DF_CONFIGURED)) {
1943 pr_err("Unable to set alua_access_state while device is"
1944 " not configured\n");
1945 return -ENODEV;
1946 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001947
Jingoo Han57103d72013-07-19 16:22:19 +09001948 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001950 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001951 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09001952 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001953 }
1954 new_state = (int)tmp;
1955
Hannes Reinecke125d0112013-11-19 09:07:46 +01001956 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1957 pr_err("Unable to process implicit configfs ALUA"
1958 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001959 return -EINVAL;
1960 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01001961 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
1962 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
1963 /* LBA DEPENDENT is only allowed with implicit ALUA */
1964 pr_err("Unable to process implicit configfs ALUA transition"
1965 " while explicit ALUA management is enabled\n");
1966 return -EINVAL;
1967 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001969 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970 NULL, NULL, new_state, 0);
1971 return (!ret) ? count : -EINVAL;
1972}
1973
1974SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
1975
1976/*
1977 * alua_access_status
1978 */
1979static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
1980 struct t10_alua_tg_pt_gp *tg_pt_gp,
1981 char *page)
1982{
1983 return sprintf(page, "%s\n",
1984 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
1985}
1986
1987static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
1988 struct t10_alua_tg_pt_gp *tg_pt_gp,
1989 const char *page,
1990 size_t count)
1991{
1992 unsigned long tmp;
1993 int new_status, ret;
1994
Andy Grover6708bb22011-06-08 10:36:43 -07001995 if (!tg_pt_gp->tg_pt_gp_valid_id) {
1996 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001997 " valid tg_pt_gp ID: %hu\n",
1998 tg_pt_gp->tg_pt_gp_valid_id);
1999 return -EINVAL;
2000 }
2001
Jingoo Han57103d72013-07-19 16:22:19 +09002002 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002003 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002004 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002006 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 }
2008 new_status = (int)tmp;
2009
2010 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002011 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2012 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002013 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014 new_status);
2015 return -EINVAL;
2016 }
2017
2018 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2019 return count;
2020}
2021
2022SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2023
2024/*
2025 * alua_access_type
2026 */
2027static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2028 struct t10_alua_tg_pt_gp *tg_pt_gp,
2029 char *page)
2030{
2031 return core_alua_show_access_type(tg_pt_gp, page);
2032}
2033
2034static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2035 struct t10_alua_tg_pt_gp *tg_pt_gp,
2036 const char *page,
2037 size_t count)
2038{
2039 return core_alua_store_access_type(tg_pt_gp, page, count);
2040}
2041
2042SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2043
2044/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002045 * alua_supported_states
2046 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002047
2048#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2049static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2050 struct t10_alua_tg_pt_gp *t, char *p) \
2051{ \
2052 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002053}
2054
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002055#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2056static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2057 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2058{ \
2059 unsigned long tmp; \
2060 int ret; \
2061 \
2062 if (!t->tg_pt_gp_valid_id) { \
2063 pr_err("Unable to do set ##_name ALUA state on non" \
2064 " valid tg_pt_gp ID: %hu\n", \
2065 t->tg_pt_gp_valid_id); \
2066 return -EINVAL; \
2067 } \
2068 \
2069 ret = kstrtoul(p, 0, &tmp); \
2070 if (ret < 0) { \
2071 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2072 return -EINVAL; \
2073 } \
2074 if (tmp > 1) { \
2075 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2076 return -EINVAL; \
2077 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002078 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002079 t->_var |= _bit; \
2080 else \
2081 t->_var &= ~_bit; \
2082 \
2083 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002084}
2085
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002086SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2087 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2088SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2089 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2090SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2091
2092SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2093 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2094SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2095 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2096SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2097
2098SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2099 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2100SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2101 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002102SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002103
2104SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2105 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2106SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2107 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2108SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2109
2110SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2111 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2112SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2113 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2114SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2115
2116SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2117 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2118SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2119 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2120SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2121
2122SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2123 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2124SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2125 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2126SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002127
2128/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002129 * alua_write_metadata
2130 */
2131static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2132 struct t10_alua_tg_pt_gp *tg_pt_gp,
2133 char *page)
2134{
2135 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2136}
2137
2138static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2139 struct t10_alua_tg_pt_gp *tg_pt_gp,
2140 const char *page,
2141 size_t count)
2142{
2143 unsigned long tmp;
2144 int ret;
2145
Jingoo Han57103d72013-07-19 16:22:19 +09002146 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002147 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002148 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002149 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150 }
2151
2152 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002153 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154 " %lu\n", tmp);
2155 return -EINVAL;
2156 }
2157 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2158
2159 return count;
2160}
2161
2162SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2163
2164
2165
2166/*
2167 * nonop_delay_msecs
2168 */
2169static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2170 struct t10_alua_tg_pt_gp *tg_pt_gp,
2171 char *page)
2172{
2173 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2174
2175}
2176
2177static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2178 struct t10_alua_tg_pt_gp *tg_pt_gp,
2179 const char *page,
2180 size_t count)
2181{
2182 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2183}
2184
2185SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2186
2187/*
2188 * trans_delay_msecs
2189 */
2190static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2191 struct t10_alua_tg_pt_gp *tg_pt_gp,
2192 char *page)
2193{
2194 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2195}
2196
2197static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2198 struct t10_alua_tg_pt_gp *tg_pt_gp,
2199 const char *page,
2200 size_t count)
2201{
2202 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2203}
2204
2205SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2206
2207/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002208 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002209 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002210static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002211 struct t10_alua_tg_pt_gp *tg_pt_gp,
2212 char *page)
2213{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002214 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002215}
2216
Hannes Reinecke125d0112013-11-19 09:07:46 +01002217static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002218 struct t10_alua_tg_pt_gp *tg_pt_gp,
2219 const char *page,
2220 size_t count)
2221{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002222 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002223}
2224
Hannes Reinecke125d0112013-11-19 09:07:46 +01002225SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002226
2227/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002228 * preferred
2229 */
2230
2231static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2232 struct t10_alua_tg_pt_gp *tg_pt_gp,
2233 char *page)
2234{
2235 return core_alua_show_preferred_bit(tg_pt_gp, page);
2236}
2237
2238static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2239 struct t10_alua_tg_pt_gp *tg_pt_gp,
2240 const char *page,
2241 size_t count)
2242{
2243 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2244}
2245
2246SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2247
2248/*
2249 * tg_pt_gp_id
2250 */
2251static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2252 struct t10_alua_tg_pt_gp *tg_pt_gp,
2253 char *page)
2254{
Andy Grover6708bb22011-06-08 10:36:43 -07002255 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002256 return 0;
2257
2258 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2259}
2260
2261static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2262 struct t10_alua_tg_pt_gp *tg_pt_gp,
2263 const char *page,
2264 size_t count)
2265{
2266 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2267 unsigned long tg_pt_gp_id;
2268 int ret;
2269
Jingoo Han57103d72013-07-19 16:22:19 +09002270 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002271 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002272 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002273 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002274 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275 }
2276 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002277 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002278 " 0x0000ffff\n", tg_pt_gp_id);
2279 return -EINVAL;
2280 }
2281
2282 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2283 if (ret < 0)
2284 return -EINVAL;
2285
Andy Grover6708bb22011-06-08 10:36:43 -07002286 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2288 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2289 tg_pt_gp->tg_pt_gp_id);
2290
2291 return count;
2292}
2293
2294SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2295
2296/*
2297 * members
2298 */
2299static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2300 struct t10_alua_tg_pt_gp *tg_pt_gp,
2301 char *page)
2302{
2303 struct se_port *port;
2304 struct se_portal_group *tpg;
2305 struct se_lun *lun;
2306 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2307 ssize_t len = 0, cur_len;
2308 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2309
2310 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2311
2312 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2313 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2314 tg_pt_gp_mem_list) {
2315 port = tg_pt_gp_mem->tg_pt;
2316 tpg = port->sep_tpg;
2317 lun = port->sep_lun;
2318
2319 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002320 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2321 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2322 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002323 config_item_name(&lun->lun_group.cg_item));
2324 cur_len++; /* Extra byte for NULL terminator */
2325
2326 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002327 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328 "_members buffer\n");
2329 break;
2330 }
2331 memcpy(page+len, buf, cur_len);
2332 len += cur_len;
2333 }
2334 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2335
2336 return len;
2337}
2338
2339SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2340
2341CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2342 tg_pt_gp_group);
2343
2344static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2345 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2346 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2347 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002348 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2349 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2350 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2351 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2352 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2353 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2354 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2356 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2357 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002358 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002359 &target_core_alua_tg_pt_gp_preferred.attr,
2360 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2361 &target_core_alua_tg_pt_gp_members.attr,
2362 NULL,
2363};
2364
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002365static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2366{
2367 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2368 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2369
2370 core_alua_free_tg_pt_gp(tg_pt_gp);
2371}
2372
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002373static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002374 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002375 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2376 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2377};
2378
2379static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2380 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2381 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2382 .ct_owner = THIS_MODULE,
2383};
2384
2385/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2386
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002387/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388
2389static struct config_group *target_core_alua_create_tg_pt_gp(
2390 struct config_group *group,
2391 const char *name)
2392{
2393 struct t10_alua *alua = container_of(group, struct t10_alua,
2394 alua_tg_pt_gps_group);
2395 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396 struct config_group *alua_tg_pt_gp_cg = NULL;
2397 struct config_item *alua_tg_pt_gp_ci = NULL;
2398
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002399 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002400 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002401 return NULL;
2402
2403 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2404 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2405
2406 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2407 &target_core_alua_tg_pt_gp_cit);
2408
Andy Grover6708bb22011-06-08 10:36:43 -07002409 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002410 " Group: alua/tg_pt_gps/%s\n",
2411 config_item_name(alua_tg_pt_gp_ci));
2412
2413 return alua_tg_pt_gp_cg;
2414}
2415
2416static void target_core_alua_drop_tg_pt_gp(
2417 struct config_group *group,
2418 struct config_item *item)
2419{
2420 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2421 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2422
Andy Grover6708bb22011-06-08 10:36:43 -07002423 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2425 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002426 /*
2427 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2428 * -> target_core_alua_tg_pt_gp_release().
2429 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002430 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431}
2432
2433static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2434 .make_group = &target_core_alua_create_tg_pt_gp,
2435 .drop_item = &target_core_alua_drop_tg_pt_gp,
2436};
2437
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002438TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002439
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002440/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441
2442/* Start functions for struct config_item_type target_core_alua_cit */
2443
2444/*
2445 * target_core_alua_cit is a ConfigFS group that lives under
2446 * /sys/kernel/config/target/core/alua. There are default groups
2447 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2448 * target_core_alua_cit in target_core_init_configfs() below.
2449 */
2450static struct config_item_type target_core_alua_cit = {
2451 .ct_item_ops = NULL,
2452 .ct_attrs = NULL,
2453 .ct_owner = THIS_MODULE,
2454};
2455
2456/* End functions for struct config_item_type target_core_alua_cit */
2457
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002458/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002459
2460static struct config_group *target_core_stat_mkdir(
2461 struct config_group *group,
2462 const char *name)
2463{
2464 return ERR_PTR(-ENOSYS);
2465}
2466
2467static void target_core_stat_rmdir(
2468 struct config_group *group,
2469 struct config_item *item)
2470{
2471 return;
2472}
2473
2474static struct configfs_group_operations target_core_stat_group_ops = {
2475 .make_group = &target_core_stat_mkdir,
2476 .drop_item = &target_core_stat_rmdir,
2477};
2478
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002479TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002480
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002481/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002482
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002483/* Start functions for struct config_item_type target_core_hba_cit */
2484
2485static struct config_group *target_core_make_subdev(
2486 struct config_group *group,
2487 const char *name)
2488{
2489 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002490 struct config_item *hba_ci = &group->cg_item;
2491 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002492 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002493 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002494 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002495 struct config_group *dev_stat_grp = NULL;
2496 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002497
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002498 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2499 if (ret)
2500 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002501
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002502 dev = target_alloc_device(hba, name);
2503 if (!dev)
2504 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002505
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002506 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002507
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002508 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002509 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002510 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002511 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002513 config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002514 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002515 &tb->tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002516 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002517 &tb->tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002518 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002519 &tb->tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002520 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002521 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002522 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002523 "statistics", &tb->tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002524
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002525 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2526 dev_cg->default_groups[1] = &dev->dev_pr_group;
2527 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2528 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2529 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002530 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002531 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002532 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002534 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002535 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002536 goto out_free_dev_cg_default_groups;
2537 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002539 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002540 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002541 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002542 if (!tg_pt_gp_cg->default_groups) {
2543 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002545 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546 }
2547
2548 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2549 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2550 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2551 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002552 /*
2553 * Add core/$HBA/$DEV/statistics/ default groups
2554 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002555 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002556 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002557 GFP_KERNEL);
2558 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002559 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002560 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002561 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002562 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563
2564 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002565 return dev_cg;
2566
2567out_free_tg_pt_gp_cg_default_groups:
2568 kfree(tg_pt_gp_cg->default_groups);
2569out_free_tg_pt_gp:
2570 core_alua_free_tg_pt_gp(tg_pt_gp);
2571out_free_dev_cg_default_groups:
2572 kfree(dev_cg->default_groups);
2573out_free_device:
2574 target_free_device(dev);
2575out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002577 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578}
2579
2580static void target_core_drop_subdev(
2581 struct config_group *group,
2582 struct config_item *item)
2583{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002584 struct config_group *dev_cg = to_config_group(item);
2585 struct se_device *dev =
2586 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002589 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002590 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002592 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002593
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002594 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002596 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002597 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2598 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2599 dev_stat_grp->default_groups[i] = NULL;
2600 config_item_put(df_item);
2601 }
2602 kfree(dev_stat_grp->default_groups);
2603
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002604 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2606 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2607 tg_pt_gp_cg->default_groups[i] = NULL;
2608 config_item_put(df_item);
2609 }
2610 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002611 /*
2612 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2613 * directly from target_core_alua_tg_pt_gp_release().
2614 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002615 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 for (i = 0; dev_cg->default_groups[i]; i++) {
2618 df_item = &dev_cg->default_groups[i]->cg_item;
2619 dev_cg->default_groups[i] = NULL;
2620 config_item_put(df_item);
2621 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002623 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002625 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002627}
2628
2629static struct configfs_group_operations target_core_hba_group_ops = {
2630 .make_group = target_core_make_subdev,
2631 .drop_item = target_core_drop_subdev,
2632};
2633
2634CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2635#define SE_HBA_ATTR(_name, _mode) \
2636static struct target_core_hba_attribute \
2637 target_core_hba_##_name = \
2638 __CONFIGFS_EATTR(_name, _mode, \
2639 target_core_hba_show_attr_##_name, \
2640 target_core_hba_store_attr_##_name);
2641
2642#define SE_HBA_ATTR_RO(_name) \
2643static struct target_core_hba_attribute \
2644 target_core_hba_##_name = \
2645 __CONFIGFS_EATTR_RO(_name, \
2646 target_core_hba_show_attr_##_name);
2647
2648static ssize_t target_core_hba_show_attr_hba_info(
2649 struct se_hba *hba,
2650 char *page)
2651{
2652 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002653 hba->hba_id, hba->backend->ops->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002654 TARGET_CORE_CONFIGFS_VERSION);
2655}
2656
2657SE_HBA_ATTR_RO(hba_info);
2658
2659static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2660 char *page)
2661{
2662 int hba_mode = 0;
2663
2664 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2665 hba_mode = 1;
2666
2667 return sprintf(page, "%d\n", hba_mode);
2668}
2669
2670static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2671 const char *page, size_t count)
2672{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002673 unsigned long mode_flag;
2674 int ret;
2675
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002676 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677 return -EINVAL;
2678
Jingoo Han57103d72013-07-19 16:22:19 +09002679 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002680 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002681 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002682 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 }
2684
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002685 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002686 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 return -EINVAL;
2688 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002690 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 if (ret < 0)
2692 return -EINVAL;
2693 if (ret > 0)
2694 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2695 else if (ret == 0)
2696 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2697
2698 return count;
2699}
2700
2701SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2702
2703CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2704
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002705static void target_core_hba_release(struct config_item *item)
2706{
2707 struct se_hba *hba = container_of(to_config_group(item),
2708 struct se_hba, hba_group);
2709 core_delete_hba(hba);
2710}
2711
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712static struct configfs_attribute *target_core_hba_attrs[] = {
2713 &target_core_hba_hba_info.attr,
2714 &target_core_hba_hba_mode.attr,
2715 NULL,
2716};
2717
2718static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002719 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 .show_attribute = target_core_hba_attr_show,
2721 .store_attribute = target_core_hba_attr_store,
2722};
2723
2724static struct config_item_type target_core_hba_cit = {
2725 .ct_item_ops = &target_core_hba_item_ops,
2726 .ct_group_ops = &target_core_hba_group_ops,
2727 .ct_attrs = target_core_hba_attrs,
2728 .ct_owner = THIS_MODULE,
2729};
2730
2731static struct config_group *target_core_call_addhbatotarget(
2732 struct config_group *group,
2733 const char *name)
2734{
2735 char *se_plugin_str, *str, *str2;
2736 struct se_hba *hba;
2737 char buf[TARGET_CORE_NAME_MAX_LEN];
2738 unsigned long plugin_dep_id = 0;
2739 int ret;
2740
2741 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07002742 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07002743 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002744 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2745 TARGET_CORE_NAME_MAX_LEN);
2746 return ERR_PTR(-ENAMETOOLONG);
2747 }
2748 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2749
2750 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002751 if (!str) {
2752 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002753 return ERR_PTR(-EINVAL);
2754 }
2755 se_plugin_str = buf;
2756 /*
2757 * Special case for subsystem plugins that have "_" in their names.
2758 * Namely rd_direct and rd_mcp..
2759 */
2760 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002761 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762 *str2 = '\0'; /* Terminate for *se_plugin_str */
2763 str2++; /* Skip to start of plugin dependent ID */
2764 str = str2;
2765 } else {
2766 *str = '\0'; /* Terminate for *se_plugin_str */
2767 str++; /* Skip to start of plugin dependent ID */
2768 }
2769
Jingoo Han57103d72013-07-19 16:22:19 +09002770 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002771 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002772 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002773 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002774 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775 }
2776 /*
2777 * Load up TCM subsystem plugins if they have not already been loaded.
2778 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07002779 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002780
2781 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2782 if (IS_ERR(hba))
2783 return ERR_CAST(hba);
2784
2785 config_group_init_type_name(&hba->hba_group, name,
2786 &target_core_hba_cit);
2787
2788 return &hba->hba_group;
2789}
2790
2791static void target_core_call_delhbafromtarget(
2792 struct config_group *group,
2793 struct config_item *item)
2794{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002795 /*
2796 * core_delete_hba() is called from target_core_hba_item_ops->release()
2797 * -> target_core_hba_release()
2798 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002800}
2801
2802static struct configfs_group_operations target_core_group_ops = {
2803 .make_group = target_core_call_addhbatotarget,
2804 .drop_item = target_core_call_delhbafromtarget,
2805};
2806
2807static struct config_item_type target_core_cit = {
2808 .ct_item_ops = NULL,
2809 .ct_group_ops = &target_core_group_ops,
2810 .ct_attrs = NULL,
2811 .ct_owner = THIS_MODULE,
2812};
2813
2814/* Stop functions for struct config_item_type target_core_hba_cit */
2815
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002816void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002817{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002818 target_core_setup_dev_cit(tb);
2819 target_core_setup_dev_attrib_cit(tb);
2820 target_core_setup_dev_pr_cit(tb);
2821 target_core_setup_dev_wwn_cit(tb);
2822 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
2823 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002824}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002825
Axel Lin54550fa2011-03-14 04:06:09 -07002826static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827{
2828 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2829 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02002830 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831 struct t10_alua_lu_gp *lu_gp;
2832 int ret;
2833
Andy Grover6708bb22011-06-08 10:36:43 -07002834 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2836 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2837
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 config_group_init(&subsys->su_group);
2839 mutex_init(&subsys->su_mutex);
2840
Andy Grovere3d6f902011-07-19 08:55:10 +00002841 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002843 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 /*
2845 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2846 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2847 */
2848 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08002849 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002850 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002851 if (!target_cg->default_groups) {
2852 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002853 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854 goto out_global;
2855 }
2856
Andy Grovere3d6f902011-07-19 08:55:10 +00002857 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002858 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002859 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 target_cg->default_groups[1] = NULL;
2861 /*
2862 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2863 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002864 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002865 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002867 if (!hba_cg->default_groups) {
2868 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002869 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870 goto out_global;
2871 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002872 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002874 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002875 hba_cg->default_groups[1] = NULL;
2876 /*
2877 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2878 * groups under /sys/kernel/config/target/core/alua/
2879 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002880 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002881 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002883 if (!alua_cg->default_groups) {
2884 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002885 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886 goto out_global;
2887 }
2888
Andy Grovere3d6f902011-07-19 08:55:10 +00002889 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002890 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002891 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 alua_cg->default_groups[1] = NULL;
2893 /*
2894 * Add core/alua/lu_gps/default_lu_gp
2895 */
2896 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002897 if (IS_ERR(lu_gp)) {
2898 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002900 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002901
Andy Grovere3d6f902011-07-19 08:55:10 +00002902 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002903 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002905 if (!lu_gp_cg->default_groups) {
2906 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002907 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 goto out_global;
2909 }
2910
2911 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2912 &target_core_alua_lu_gp_cit);
2913 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2914 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002915 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 /*
2917 * Register the target_core_mod subsystem with configfs.
2918 */
2919 ret = configfs_register_subsystem(subsys);
2920 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002921 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 ret, subsys->su_group.cg_item.ci_namebuf);
2923 goto out_global;
2924 }
Andy Grover6708bb22011-06-08 10:36:43 -07002925 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2927 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2928 /*
2929 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2930 */
2931 ret = rd_module_init();
2932 if (ret < 0)
2933 goto out;
2934
Roland Dreier0d0f9df2012-10-31 09:16:44 -07002935 ret = core_dev_setup_virtual_lun0();
2936 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 goto out;
2938
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07002939 ret = target_xcopy_setup_pt();
2940 if (ret < 0)
2941 goto out;
2942
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 return 0;
2944
2945out:
2946 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002947 core_dev_release_virtual_lun0();
2948 rd_module_exit();
2949out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00002950 if (default_lu_gp) {
2951 core_alua_free_lu_gp(default_lu_gp);
2952 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 }
2954 if (lu_gp_cg)
2955 kfree(lu_gp_cg->default_groups);
2956 if (alua_cg)
2957 kfree(alua_cg->default_groups);
2958 if (hba_cg)
2959 kfree(hba_cg->default_groups);
2960 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00002961 release_se_kmem_caches();
2962 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002963}
2964
Axel Lin54550fa2011-03-14 04:06:09 -07002965static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002966{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2968 struct config_item *item;
2969 int i;
2970
Andy Grovere3d6f902011-07-19 08:55:10 +00002971 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2973 item = &lu_gp_cg->default_groups[i]->cg_item;
2974 lu_gp_cg->default_groups[i] = NULL;
2975 config_item_put(item);
2976 }
2977 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002978 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979
Andy Grovere3d6f902011-07-19 08:55:10 +00002980 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 for (i = 0; alua_cg->default_groups[i]; i++) {
2982 item = &alua_cg->default_groups[i]->cg_item;
2983 alua_cg->default_groups[i] = NULL;
2984 config_item_put(item);
2985 }
2986 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002987 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988
Andy Grovere3d6f902011-07-19 08:55:10 +00002989 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990 for (i = 0; hba_cg->default_groups[i]; i++) {
2991 item = &hba_cg->default_groups[i]->cg_item;
2992 hba_cg->default_groups[i] = NULL;
2993 config_item_put(item);
2994 }
2995 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002996 hba_cg->default_groups = NULL;
2997 /*
2998 * We expect subsys->su_group.default_groups to be released
2999 * by configfs subsystem provider logic..
3000 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003001 configfs_unregister_subsystem(&target_core_fabrics);
3002 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003003
Andy Grovere3d6f902011-07-19 08:55:10 +00003004 core_alua_free_lu_gp(default_lu_gp);
3005 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003006
Andy Grover6708bb22011-06-08 10:36:43 -07003007 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 " Infrastructure\n");
3009
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010 core_dev_release_virtual_lun0();
3011 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003012 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003013 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003014}
3015
3016MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3017MODULE_AUTHOR("nab@Linux-iSCSI.org");
3018MODULE_LICENSE("GPL");
3019
3020module_init(target_core_init_configfs);
3021module_exit(target_core_exit_configfs);