blob: f97b969e67142c86e902f5dfd97dda16acc385a4 [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 */
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200474#define DEF_TB_DEV_ATTRIB_SHOW(_name) \
475static ssize_t show_##_name(struct se_dev_attrib *da, char *page) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200476{ \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200477 return snprintf(page, PAGE_SIZE, "%u\n", da->_name); \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200478}
479
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200480DEF_TB_DEV_ATTRIB_SHOW(emulate_model_alias);
481DEF_TB_DEV_ATTRIB_SHOW(emulate_dpo);
482DEF_TB_DEV_ATTRIB_SHOW(emulate_fua_write);
483DEF_TB_DEV_ATTRIB_SHOW(emulate_fua_read);
484DEF_TB_DEV_ATTRIB_SHOW(emulate_write_cache);
485DEF_TB_DEV_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
486DEF_TB_DEV_ATTRIB_SHOW(emulate_tas);
487DEF_TB_DEV_ATTRIB_SHOW(emulate_tpu);
488DEF_TB_DEV_ATTRIB_SHOW(emulate_tpws);
489DEF_TB_DEV_ATTRIB_SHOW(emulate_caw);
490DEF_TB_DEV_ATTRIB_SHOW(emulate_3pc);
491DEF_TB_DEV_ATTRIB_SHOW(pi_prot_type);
492DEF_TB_DEV_ATTRIB_SHOW(hw_pi_prot_type);
493DEF_TB_DEV_ATTRIB_SHOW(pi_prot_format);
494DEF_TB_DEV_ATTRIB_SHOW(enforce_pr_isids);
495DEF_TB_DEV_ATTRIB_SHOW(is_nonrot);
496DEF_TB_DEV_ATTRIB_SHOW(emulate_rest_reord);
497DEF_TB_DEV_ATTRIB_SHOW(force_pr_aptpl);
498DEF_TB_DEV_ATTRIB_SHOW(hw_block_size);
499DEF_TB_DEV_ATTRIB_SHOW(block_size);
500DEF_TB_DEV_ATTRIB_SHOW(hw_max_sectors);
501DEF_TB_DEV_ATTRIB_SHOW(optimal_sectors);
502DEF_TB_DEV_ATTRIB_SHOW(hw_queue_depth);
503DEF_TB_DEV_ATTRIB_SHOW(queue_depth);
504DEF_TB_DEV_ATTRIB_SHOW(max_unmap_lba_count);
505DEF_TB_DEV_ATTRIB_SHOW(max_unmap_block_desc_count);
506DEF_TB_DEV_ATTRIB_SHOW(unmap_granularity);
507DEF_TB_DEV_ATTRIB_SHOW(unmap_granularity_alignment);
508DEF_TB_DEV_ATTRIB_SHOW(max_write_same_len);
509
510#define DEF_TB_DEV_ATTRIB_STORE_U32(_name) \
511static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
512 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200513{ \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200514 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200515 int ret; \
516 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200517 ret = kstrtou32(page, 0, &val); \
518 if (ret < 0) \
519 return ret; \
520 da->_name = val; \
521 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200522}
523
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200524DEF_TB_DEV_ATTRIB_STORE_U32(max_unmap_lba_count);
525DEF_TB_DEV_ATTRIB_STORE_U32(max_unmap_block_desc_count);
526DEF_TB_DEV_ATTRIB_STORE_U32(unmap_granularity);
527DEF_TB_DEV_ATTRIB_STORE_U32(unmap_granularity_alignment);
528DEF_TB_DEV_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200529
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200530#define DEF_TB_DEV_ATTRIB_STORE_BOOL(_name) \
531static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
532 size_t count) \
533{ \
534 bool flag; \
535 int ret; \
536 \
537 ret = strtobool(page, &flag); \
538 if (ret < 0) \
539 return ret; \
540 da->_name = flag; \
541 return count; \
542}
543
544DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_fua_write);
545DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_caw);
546DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_3pc);
547DEF_TB_DEV_ATTRIB_STORE_BOOL(enforce_pr_isids);
548DEF_TB_DEV_ATTRIB_STORE_BOOL(is_nonrot);
549
550#define DEF_TB_DEV_ATTRIB_STORE_STUB(_name) \
551static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
552 size_t count) \
553{ \
554 printk_once(KERN_WARNING \
555 "ignoring deprecated ##_name## attribute\n"); \
556 return count; \
557}
558
559DEF_TB_DEV_ATTRIB_STORE_STUB(emulate_dpo);
560DEF_TB_DEV_ATTRIB_STORE_STUB(emulate_fua_read);
561
562static void dev_set_t10_wwn_model_alias(struct se_device *dev)
563{
564 const char *configname;
565
566 configname = config_item_name(&dev->dev_group.cg_item);
567 if (strlen(configname) >= 16) {
568 pr_warn("dev[%p]: Backstore name '%s' is too long for "
569 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
570 configname);
571 }
572 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
573}
574
575static ssize_t store_emulate_model_alias(struct se_dev_attrib *da,
576 const char *page, size_t count)
577{
578 struct se_device *dev = da->da_dev;
579 bool flag;
580 int ret;
581
582 if (dev->export_count) {
583 pr_err("dev[%p]: Unable to change model alias"
584 " while export_count is %d\n",
585 dev, dev->export_count);
586 return -EINVAL;
587 }
588
589 ret = strtobool(page, &flag);
590 if (ret < 0)
591 return ret;
592
593 if (flag) {
594 dev_set_t10_wwn_model_alias(dev);
595 } else {
596 strncpy(&dev->t10_wwn.model[0],
597 dev->transport->inquiry_prod, 16);
598 }
599 da->emulate_model_alias = flag;
600 return count;
601}
602
603static ssize_t store_emulate_write_cache(struct se_dev_attrib *da,
604 const char *page, size_t count)
605{
606 bool flag;
607 int ret;
608
609 ret = strtobool(page, &flag);
610 if (ret < 0)
611 return ret;
612
613 if (flag && da->da_dev->transport->get_write_cache) {
614 pr_err("emulate_write_cache not supported for this device\n");
615 return -EINVAL;
616 }
617
618 da->emulate_write_cache = flag;
619 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
620 da->da_dev, flag);
621 return count;
622}
623
624static ssize_t store_emulate_ua_intlck_ctrl(struct se_dev_attrib *da,
625 const char *page, size_t count)
626{
627 u32 val;
628 int ret;
629
630 ret = kstrtou32(page, 0, &val);
631 if (ret < 0)
632 return ret;
633
634 if (val != 0 && val != 1 && val != 2) {
635 pr_err("Illegal value %d\n", val);
636 return -EINVAL;
637 }
638
639 if (da->da_dev->export_count) {
640 pr_err("dev[%p]: Unable to change SE Device"
641 " UA_INTRLCK_CTRL while export_count is %d\n",
642 da->da_dev, da->da_dev->export_count);
643 return -EINVAL;
644 }
645 da->emulate_ua_intlck_ctrl = val;
646 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
647 da->da_dev, val);
648 return count;
649}
650
651static ssize_t store_emulate_tas(struct se_dev_attrib *da,
652 const char *page, size_t count)
653{
654 bool flag;
655 int ret;
656
657 ret = strtobool(page, &flag);
658 if (ret < 0)
659 return ret;
660
661 if (da->da_dev->export_count) {
662 pr_err("dev[%p]: Unable to change SE Device TAS while"
663 " export_count is %d\n",
664 da->da_dev, da->da_dev->export_count);
665 return -EINVAL;
666 }
667 da->emulate_tas = flag;
668 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
669 da->da_dev, flag ? "Enabled" : "Disabled");
670
671 return count;
672}
673
674static ssize_t store_emulate_tpu(struct se_dev_attrib *da,
675 const char *page, size_t count)
676{
677 bool flag;
678 int ret;
679
680 ret = strtobool(page, &flag);
681 if (ret < 0)
682 return ret;
683
684 /*
685 * We expect this value to be non-zero when generic Block Layer
686 * Discard supported is detected iblock_create_virtdevice().
687 */
688 if (flag && !da->max_unmap_block_desc_count) {
689 pr_err("Generic Block Discard not supported\n");
690 return -ENOSYS;
691 }
692
693 da->emulate_tpu = flag;
694 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
695 da->da_dev, flag);
696 return count;
697}
698
699static ssize_t store_emulate_tpws(struct se_dev_attrib *da,
700 const char *page, size_t count)
701{
702 bool flag;
703 int ret;
704
705 ret = strtobool(page, &flag);
706 if (ret < 0)
707 return ret;
708
709 /*
710 * We expect this value to be non-zero when generic Block Layer
711 * Discard supported is detected iblock_create_virtdevice().
712 */
713 if (flag && !da->max_unmap_block_desc_count) {
714 pr_err("Generic Block Discard not supported\n");
715 return -ENOSYS;
716 }
717
718 da->emulate_tpws = flag;
719 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
720 da->da_dev, flag);
721 return count;
722}
723
724static ssize_t store_pi_prot_type(struct se_dev_attrib *da,
725 const char *page, size_t count)
726{
727 int old_prot = da->pi_prot_type, ret;
728 struct se_device *dev = da->da_dev;
729 u32 flag;
730
731 ret = kstrtou32(page, 0, &flag);
732 if (ret < 0)
733 return ret;
734
735 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
736 pr_err("Illegal value %d for pi_prot_type\n", flag);
737 return -EINVAL;
738 }
739 if (flag == 2) {
740 pr_err("DIF TYPE2 protection currently not supported\n");
741 return -ENOSYS;
742 }
743 if (da->hw_pi_prot_type) {
744 pr_warn("DIF protection enabled on underlying hardware,"
745 " ignoring\n");
746 return count;
747 }
748 if (!dev->transport->init_prot || !dev->transport->free_prot) {
749 /* 0 is only allowed value for non-supporting backends */
750 if (flag == 0)
751 return 0;
752
753 pr_err("DIF protection not supported by backend: %s\n",
754 dev->transport->name);
755 return -ENOSYS;
756 }
757 if (!(dev->dev_flags & DF_CONFIGURED)) {
758 pr_err("DIF protection requires device to be configured\n");
759 return -ENODEV;
760 }
761 if (dev->export_count) {
762 pr_err("dev[%p]: Unable to change SE Device PROT type while"
763 " export_count is %d\n", dev, dev->export_count);
764 return -EINVAL;
765 }
766
767 da->pi_prot_type = flag;
768
769 if (flag && !old_prot) {
770 ret = dev->transport->init_prot(dev);
771 if (ret) {
772 da->pi_prot_type = old_prot;
773 return ret;
774 }
775
776 } else if (!flag && old_prot) {
777 dev->transport->free_prot(dev);
778 }
779
780 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
781 return count;
782}
783
784static ssize_t store_pi_prot_format(struct se_dev_attrib *da,
785 const char *page, size_t count)
786{
787 struct se_device *dev = da->da_dev;
788 bool flag;
789 int ret;
790
791 ret = strtobool(page, &flag);
792 if (ret < 0)
793 return ret;
794
795 if (!flag)
796 return count;
797
798 if (!dev->transport->format_prot) {
799 pr_err("DIF protection format not supported by backend %s\n",
800 dev->transport->name);
801 return -ENOSYS;
802 }
803 if (!(dev->dev_flags & DF_CONFIGURED)) {
804 pr_err("DIF protection format requires device to be configured\n");
805 return -ENODEV;
806 }
807 if (dev->export_count) {
808 pr_err("dev[%p]: Unable to format SE Device PROT type while"
809 " export_count is %d\n", dev, dev->export_count);
810 return -EINVAL;
811 }
812
813 ret = dev->transport->format_prot(dev);
814 if (ret)
815 return ret;
816
817 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
818 return count;
819}
820
821static ssize_t store_force_pr_aptpl(struct se_dev_attrib *da,
822 const char *page, size_t count)
823{
824 bool flag;
825 int ret;
826
827 ret = strtobool(page, &flag);
828 if (ret < 0)
829 return ret;
830 if (da->da_dev->export_count) {
831 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
832 " export_count is %d\n",
833 da->da_dev, da->da_dev->export_count);
834 return -EINVAL;
835 }
836
837 da->force_pr_aptpl = flag;
838 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
839 return count;
840}
841
842static ssize_t store_emulate_rest_reord(struct se_dev_attrib *da,
843 const char *page, size_t count)
844{
845 bool flag;
846 int ret;
847
848 ret = strtobool(page, &flag);
849 if (ret < 0)
850 return ret;
851
852 if (flag != 0) {
853 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
854 " reordering not implemented\n", da->da_dev);
855 return -ENOSYS;
856 }
857 da->emulate_rest_reord = flag;
858 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
859 da->da_dev, flag);
860 return count;
861}
862
863/*
864 * Note, this can only be called on unexported SE Device Object.
865 */
866static ssize_t store_queue_depth(struct se_dev_attrib *da,
867 const char *page, size_t count)
868{
869 struct se_device *dev = da->da_dev;
870 u32 val;
871 int ret;
872
873 ret = kstrtou32(page, 0, &val);
874 if (ret < 0)
875 return ret;
876
877 if (dev->export_count) {
878 pr_err("dev[%p]: Unable to change SE Device TCQ while"
879 " export_count is %d\n",
880 dev, dev->export_count);
881 return -EINVAL;
882 }
883 if (!val) {
884 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
885 return -EINVAL;
886 }
887
888 if (val > dev->dev_attrib.queue_depth) {
889 if (val > dev->dev_attrib.hw_queue_depth) {
890 pr_err("dev[%p]: Passed queue_depth:"
891 " %u exceeds TCM/SE_Device MAX"
892 " TCQ: %u\n", dev, val,
893 dev->dev_attrib.hw_queue_depth);
894 return -EINVAL;
895 }
896 }
897 da->queue_depth = dev->queue_depth = val;
898 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
899 return count;
900}
901
902static ssize_t store_optimal_sectors(struct se_dev_attrib *da,
903 const char *page, size_t count)
904{
905 u32 val;
906 int ret;
907
908 ret = kstrtou32(page, 0, &val);
909 if (ret < 0)
910 return ret;
911
912 if (da->da_dev->export_count) {
913 pr_err("dev[%p]: Unable to change SE Device"
914 " optimal_sectors while export_count is %d\n",
915 da->da_dev, da->da_dev->export_count);
916 return -EINVAL;
917 }
918 if (val > da->hw_max_sectors) {
919 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
920 " greater than hw_max_sectors: %u\n",
921 da->da_dev, val, da->hw_max_sectors);
922 return -EINVAL;
923 }
924
925 da->optimal_sectors = val;
926 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
927 da->da_dev, val);
928 return count;
929}
930
931static ssize_t store_block_size(struct se_dev_attrib *da,
932 const char *page, size_t count)
933{
934 u32 val;
935 int ret;
936
937 ret = kstrtou32(page, 0, &val);
938 if (ret < 0)
939 return ret;
940
941 if (da->da_dev->export_count) {
942 pr_err("dev[%p]: Unable to change SE Device block_size"
943 " while export_count is %d\n",
944 da->da_dev, da->da_dev->export_count);
945 return -EINVAL;
946 }
947
948 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
949 pr_err("dev[%p]: Illegal value for block_device: %u"
950 " for SE device, must be 512, 1024, 2048 or 4096\n",
951 da->da_dev, val);
952 return -EINVAL;
953 }
954
955 da->block_size = val;
956 if (da->max_bytes_per_io)
957 da->hw_max_sectors = da->max_bytes_per_io / val;
958
959 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
960 da->da_dev, val);
961 return count;
962}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200963
964CONFIGFS_EATTR_STRUCT(target_backend_dev_attrib, se_dev_attrib);
965#define TB_DEV_ATTR(_backend, _name, _mode) \
966static struct target_backend_dev_attrib_attribute _backend##_dev_attrib_##_name = \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200967 __CONFIGFS_EATTR(_name, _mode, \
968 show_##_name, \
969 store_##_name);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200970
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200971#define TB_DEV_ATTR_RO(_backend, _name) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200972static struct target_backend_dev_attrib_attribute _backend##_dev_attrib_##_name = \
973 __CONFIGFS_EATTR_RO(_name, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200974 show_##_name);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200975
976TB_DEV_ATTR(target_core, emulate_model_alias, S_IRUGO | S_IWUSR);
977TB_DEV_ATTR(target_core, emulate_dpo, S_IRUGO | S_IWUSR);
978TB_DEV_ATTR(target_core, emulate_fua_write, S_IRUGO | S_IWUSR);
979TB_DEV_ATTR(target_core, emulate_fua_read, S_IRUGO | S_IWUSR);
980TB_DEV_ATTR(target_core, emulate_write_cache, S_IRUGO | S_IWUSR);
981TB_DEV_ATTR(target_core, emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
982TB_DEV_ATTR(target_core, emulate_tas, S_IRUGO | S_IWUSR);
983TB_DEV_ATTR(target_core, emulate_tpu, S_IRUGO | S_IWUSR);
984TB_DEV_ATTR(target_core, emulate_tpws, S_IRUGO | S_IWUSR);
985TB_DEV_ATTR(target_core, emulate_caw, S_IRUGO | S_IWUSR);
986TB_DEV_ATTR(target_core, emulate_3pc, S_IRUGO | S_IWUSR);
987TB_DEV_ATTR(target_core, pi_prot_type, S_IRUGO | S_IWUSR);
988TB_DEV_ATTR_RO(target_core, hw_pi_prot_type);
989TB_DEV_ATTR(target_core, pi_prot_format, S_IRUGO | S_IWUSR);
990TB_DEV_ATTR(target_core, enforce_pr_isids, S_IRUGO | S_IWUSR);
991TB_DEV_ATTR(target_core, is_nonrot, S_IRUGO | S_IWUSR);
992TB_DEV_ATTR(target_core, emulate_rest_reord, S_IRUGO | S_IWUSR);
993TB_DEV_ATTR(target_core, force_pr_aptpl, S_IRUGO | S_IWUSR)
994TB_DEV_ATTR_RO(target_core, hw_block_size);
995TB_DEV_ATTR(target_core, block_size, S_IRUGO | S_IWUSR)
996TB_DEV_ATTR_RO(target_core, hw_max_sectors);
997TB_DEV_ATTR(target_core, optimal_sectors, S_IRUGO | S_IWUSR);
998TB_DEV_ATTR_RO(target_core, hw_queue_depth);
999TB_DEV_ATTR(target_core, queue_depth, S_IRUGO | S_IWUSR);
1000TB_DEV_ATTR(target_core, max_unmap_lba_count, S_IRUGO | S_IWUSR);
1001TB_DEV_ATTR(target_core, max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
1002TB_DEV_ATTR(target_core, unmap_granularity, S_IRUGO | S_IWUSR);
1003TB_DEV_ATTR(target_core, unmap_granularity_alignment, S_IRUGO | S_IWUSR);
1004TB_DEV_ATTR(target_core, max_write_same_len, S_IRUGO | S_IWUSR);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001005
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001006CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
1008
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001009/*
1010 * dev_attrib attributes for devices using the target core SBC/SPC
1011 * interpreter. Any backend using spc_parse_cdb should be using
1012 * these.
1013 */
1014struct configfs_attribute *sbc_attrib_attrs[] = {
1015 &target_core_dev_attrib_emulate_model_alias.attr,
1016 &target_core_dev_attrib_emulate_dpo.attr,
1017 &target_core_dev_attrib_emulate_fua_write.attr,
1018 &target_core_dev_attrib_emulate_fua_read.attr,
1019 &target_core_dev_attrib_emulate_write_cache.attr,
1020 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
1021 &target_core_dev_attrib_emulate_tas.attr,
1022 &target_core_dev_attrib_emulate_tpu.attr,
1023 &target_core_dev_attrib_emulate_tpws.attr,
1024 &target_core_dev_attrib_emulate_caw.attr,
1025 &target_core_dev_attrib_emulate_3pc.attr,
1026 &target_core_dev_attrib_pi_prot_type.attr,
1027 &target_core_dev_attrib_hw_pi_prot_type.attr,
1028 &target_core_dev_attrib_pi_prot_format.attr,
1029 &target_core_dev_attrib_enforce_pr_isids.attr,
1030 &target_core_dev_attrib_is_nonrot.attr,
1031 &target_core_dev_attrib_emulate_rest_reord.attr,
1032 &target_core_dev_attrib_force_pr_aptpl.attr,
1033 &target_core_dev_attrib_hw_block_size.attr,
1034 &target_core_dev_attrib_block_size.attr,
1035 &target_core_dev_attrib_hw_max_sectors.attr,
1036 &target_core_dev_attrib_optimal_sectors.attr,
1037 &target_core_dev_attrib_hw_queue_depth.attr,
1038 &target_core_dev_attrib_queue_depth.attr,
1039 &target_core_dev_attrib_max_unmap_lba_count.attr,
1040 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
1041 &target_core_dev_attrib_unmap_granularity.attr,
1042 &target_core_dev_attrib_unmap_granularity_alignment.attr,
1043 &target_core_dev_attrib_max_write_same_len.attr,
1044 NULL,
1045};
1046EXPORT_SYMBOL(sbc_attrib_attrs);
1047
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001048TB_DEV_ATTR_RO(target_pt, hw_pi_prot_type);
1049TB_DEV_ATTR_RO(target_pt, hw_block_size);
1050TB_DEV_ATTR_RO(target_pt, hw_max_sectors);
1051TB_DEV_ATTR_RO(target_pt, hw_queue_depth);
1052
1053/*
1054 * Minimal dev_attrib attributes for devices passing through CDBs.
1055 * In this case we only provide a few read-only attributes for
1056 * backwards compatibility.
1057 */
1058struct configfs_attribute *passthrough_attrib_attrs[] = {
1059 &target_pt_dev_attrib_hw_pi_prot_type.attr,
1060 &target_pt_dev_attrib_hw_block_size.attr,
1061 &target_pt_dev_attrib_hw_max_sectors.attr,
1062 &target_pt_dev_attrib_hw_queue_depth.attr,
1063 NULL,
1064};
1065EXPORT_SYMBOL(passthrough_attrib_attrs);
1066
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001067static struct configfs_item_operations target_core_dev_attrib_ops = {
1068 .show_attribute = target_core_dev_attrib_attr_show,
1069 .store_attribute = target_core_dev_attrib_attr_store,
1070};
1071
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001072TB_CIT_SETUP_DRV(dev_attrib, &target_core_dev_attrib_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001074/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001076/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077
1078CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
1079#define SE_DEV_WWN_ATTR(_name, _mode) \
1080static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
1081 __CONFIGFS_EATTR(_name, _mode, \
1082 target_core_dev_wwn_show_attr_##_name, \
1083 target_core_dev_wwn_store_attr_##_name);
1084
1085#define SE_DEV_WWN_ATTR_RO(_name); \
1086do { \
1087 static struct target_core_dev_wwn_attribute \
1088 target_core_dev_wwn_##_name = \
1089 __CONFIGFS_EATTR_RO(_name, \
1090 target_core_dev_wwn_show_attr_##_name); \
1091} while (0);
1092
1093/*
1094 * VPD page 0x80 Unit serial
1095 */
1096static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
1097 struct t10_wwn *t10_wwn,
1098 char *page)
1099{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1101 &t10_wwn->unit_serial[0]);
1102}
1103
1104static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
1105 struct t10_wwn *t10_wwn,
1106 const char *page,
1107 size_t count)
1108{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001109 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1111
1112 /*
1113 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1114 * from the struct scsi_device level firmware, do not allow
1115 * VPD Unit Serial to be emulated.
1116 *
1117 * Note this struct scsi_device could also be emulating VPD
1118 * information from its drivers/scsi LLD. But for now we assume
1119 * it is doing 'the right thing' wrt a world wide unique
1120 * VPD Unit Serial Number that OS dependent multipath can depend on.
1121 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001122 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001123 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124 " Unit Serial, ignoring request\n");
1125 return -EOPNOTSUPP;
1126 }
1127
Dan Carpenter60d645a2011-06-15 10:03:05 -07001128 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001129 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1131 return -EOVERFLOW;
1132 }
1133 /*
1134 * Check to see if any active $FABRIC_MOD exports exist. If they
1135 * do exist, fail here as changing this information on the fly
1136 * (underneath the initiator side OS dependent multipath code)
1137 * could cause negative effects.
1138 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001139 if (dev->export_count) {
1140 pr_err("Unable to set VPD Unit Serial while"
1141 " active %d $FABRIC_MOD exports exist\n",
1142 dev->export_count);
1143 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001145
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 /*
1147 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1148 *
1149 * Also, strip any newline added from the userspace
1150 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1151 */
1152 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1153 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001154 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001156 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157
Andy Grover6708bb22011-06-08 10:36:43 -07001158 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001159 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160
1161 return count;
1162}
1163
1164SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
1165
1166/*
1167 * VPD page 0x83 Protocol Identifier
1168 */
1169static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
1170 struct t10_wwn *t10_wwn,
1171 char *page)
1172{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173 struct t10_vpd *vpd;
1174 unsigned char buf[VPD_TMP_BUF_SIZE];
1175 ssize_t len = 0;
1176
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 memset(buf, 0, VPD_TMP_BUF_SIZE);
1178
1179 spin_lock(&t10_wwn->t10_vpd_lock);
1180 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001181 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 continue;
1183
1184 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1185
Andy Grover6708bb22011-06-08 10:36:43 -07001186 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 break;
1188
1189 len += sprintf(page+len, "%s", buf);
1190 }
1191 spin_unlock(&t10_wwn->t10_vpd_lock);
1192
1193 return len;
1194}
1195
1196static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
1197 struct t10_wwn *t10_wwn,
1198 const char *page,
1199 size_t count)
1200{
1201 return -ENOSYS;
1202}
1203
1204SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
1205
1206/*
1207 * Generic wrapper for dumping VPD identifiers by association.
1208 */
1209#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
1210static ssize_t target_core_dev_wwn_show_attr_##_name( \
1211 struct t10_wwn *t10_wwn, \
1212 char *page) \
1213{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 struct t10_vpd *vpd; \
1215 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1216 ssize_t len = 0; \
1217 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218 spin_lock(&t10_wwn->t10_vpd_lock); \
1219 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1220 if (vpd->association != _assoc) \
1221 continue; \
1222 \
1223 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1224 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001225 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001226 break; \
1227 len += sprintf(page+len, "%s", buf); \
1228 \
1229 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1230 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001231 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 break; \
1233 len += sprintf(page+len, "%s", buf); \
1234 \
1235 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1236 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001237 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238 break; \
1239 len += sprintf(page+len, "%s", buf); \
1240 } \
1241 spin_unlock(&t10_wwn->t10_vpd_lock); \
1242 \
1243 return len; \
1244}
1245
1246/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -07001247 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248 */
1249DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1250
1251static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
1252 struct t10_wwn *t10_wwn,
1253 const char *page,
1254 size_t count)
1255{
1256 return -ENOSYS;
1257}
1258
1259SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
1260
1261/*
1262 * VPD page 0x83 Association: Target Port
1263 */
1264DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1265
1266static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
1267 struct t10_wwn *t10_wwn,
1268 const char *page,
1269 size_t count)
1270{
1271 return -ENOSYS;
1272}
1273
1274SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
1275
1276/*
1277 * VPD page 0x83 Association: SCSI Target Device
1278 */
1279DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1280
1281static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
1282 struct t10_wwn *t10_wwn,
1283 const char *page,
1284 size_t count)
1285{
1286 return -ENOSYS;
1287}
1288
1289SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1290
1291CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1292
1293static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1294 &target_core_dev_wwn_vpd_unit_serial.attr,
1295 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1296 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1297 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1298 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1299 NULL,
1300};
1301
1302static struct configfs_item_operations target_core_dev_wwn_ops = {
1303 .show_attribute = target_core_dev_wwn_attr_show,
1304 .store_attribute = target_core_dev_wwn_attr_store,
1305};
1306
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001307TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001309/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001311/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001313CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314#define SE_DEV_PR_ATTR(_name, _mode) \
1315static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1316 __CONFIGFS_EATTR(_name, _mode, \
1317 target_core_dev_pr_show_attr_##_name, \
1318 target_core_dev_pr_store_attr_##_name);
1319
1320#define SE_DEV_PR_ATTR_RO(_name); \
1321static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1322 __CONFIGFS_EATTR_RO(_name, \
1323 target_core_dev_pr_show_attr_##_name);
1324
Christoph Hellwigd977f432012-10-10 17:37:15 -04001325static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1326 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327{
1328 struct se_node_acl *se_nacl;
1329 struct t10_pr_registration *pr_reg;
1330 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331
1332 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1333
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001335 if (!pr_reg)
1336 return sprintf(page, "No SPC-3 Reservation holder\n");
1337
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001339 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340
Christoph Hellwigd977f432012-10-10 17:37:15 -04001341 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001342 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001343 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344}
1345
Christoph Hellwigd977f432012-10-10 17:37:15 -04001346static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1347 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348{
1349 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001350 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001352 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001353 if (se_nacl) {
1354 len = sprintf(page,
1355 "SPC-2 Reservation: %s Initiator: %s\n",
1356 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1357 se_nacl->initiatorname);
1358 } else {
1359 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001360 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001361 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362}
1363
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001364static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1365 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001367 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368
Andy Grovera3541702015-05-19 14:44:41 -07001369 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001370 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371
Christoph Hellwigd977f432012-10-10 17:37:15 -04001372 spin_lock(&dev->dev_reservation_lock);
1373 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1374 ret = target_core_dev_pr_show_spc2_res(dev, page);
1375 else
1376 ret = target_core_dev_pr_show_spc3_res(dev, page);
1377 spin_unlock(&dev->dev_reservation_lock);
1378 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379}
1380
1381SE_DEV_PR_ATTR_RO(res_holder);
1382
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001384 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386 ssize_t len = 0;
1387
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001389 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001390 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001391 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 len = sprintf(page, "SPC-3 Reservation: All Target"
1393 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001394 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395 len = sprintf(page, "SPC-3 Reservation: Single"
1396 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001397 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398
Christoph Hellwigd977f432012-10-10 17:37:15 -04001399 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001400 return len;
1401}
1402
1403SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1404
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001405static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001406 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001408 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409}
1410
1411SE_DEV_PR_ATTR_RO(res_pr_generation);
1412
1413/*
1414 * res_pr_holder_tg_port
1415 */
1416static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001417 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001419 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420 struct se_portal_group *se_tpg;
1421 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001422 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 ssize_t len = 0;
1424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425 spin_lock(&dev->dev_reservation_lock);
1426 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001427 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001429 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 se_nacl = pr_reg->pr_reg_nacl;
1433 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001434 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435
1436 len += sprintf(page+len, "SPC-3 Reservation: %s"
1437 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1438 tfo->tpg_get_wwn(se_tpg));
1439 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001440 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001441 " %s Logical Unit: %u\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001443 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444
Christoph Hellwigd977f432012-10-10 17:37:15 -04001445out_unlock:
1446 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 return len;
1448}
1449
1450SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1451
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001453 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001454{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001455 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 struct t10_pr_registration *pr_reg;
1457 unsigned char buf[384];
1458 char i_buf[PR_REG_ISID_ID_LEN];
1459 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001460 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001462 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1463
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001464 spin_lock(&dev->t10_pr.registration_lock);
1465 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 pr_reg_list) {
1467
1468 memset(buf, 0, 384);
1469 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1470 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001471 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 PR_REG_ISID_ID_LEN);
1473 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1474 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001475 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 pr_reg->pr_res_generation);
1477
Andy Grover6708bb22011-06-08 10:36:43 -07001478 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 break;
1480
1481 len += sprintf(page+len, "%s", buf);
1482 reg_count++;
1483 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001484 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485
Andy Grover6708bb22011-06-08 10:36:43 -07001486 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 len += sprintf(page+len, "None\n");
1488
1489 return len;
1490}
1491
1492SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1493
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001495 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001497 struct t10_pr_registration *pr_reg;
1498 ssize_t len = 0;
1499
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 spin_lock(&dev->dev_reservation_lock);
1501 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001502 if (pr_reg) {
1503 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1504 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1505 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001506 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001508
Christoph Hellwigd977f432012-10-10 17:37:15 -04001509 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001510 return len;
1511}
1512
1513SE_DEV_PR_ATTR_RO(res_pr_type);
1514
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001516 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001517{
Andy Grovera3541702015-05-19 14:44:41 -07001518 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001519 return sprintf(page, "SPC_PASSTHROUGH\n");
1520 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1521 return sprintf(page, "SPC2_RESERVATIONS\n");
1522 else
1523 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001524}
1525
1526SE_DEV_PR_ATTR_RO(res_type);
1527
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001529 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530{
Andy Grovera3541702015-05-19 14:44:41 -07001531 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 return 0;
1533
1534 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001535 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001536}
1537
1538SE_DEV_PR_ATTR_RO(res_aptpl_active);
1539
1540/*
1541 * res_aptpl_metadata
1542 */
1543static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001544 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545{
Andy Grovera3541702015-05-19 14:44:41 -07001546 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 return 0;
1548
1549 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1550}
1551
1552enum {
1553 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1554 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1555 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1556 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1557};
1558
1559static match_table_t tokens = {
1560 {Opt_initiator_fabric, "initiator_fabric=%s"},
1561 {Opt_initiator_node, "initiator_node=%s"},
1562 {Opt_initiator_sid, "initiator_sid=%s"},
1563 {Opt_sa_res_key, "sa_res_key=%s"},
1564 {Opt_res_holder, "res_holder=%d"},
1565 {Opt_res_type, "res_type=%d"},
1566 {Opt_res_scope, "res_scope=%d"},
1567 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1568 {Opt_mapped_lun, "mapped_lun=%d"},
1569 {Opt_target_fabric, "target_fabric=%s"},
1570 {Opt_target_node, "target_node=%s"},
1571 {Opt_tpgt, "tpgt=%d"},
1572 {Opt_port_rtpi, "port_rtpi=%d"},
1573 {Opt_target_lun, "target_lun=%d"},
1574 {Opt_err, NULL}
1575};
1576
1577static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001578 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 const char *page,
1580 size_t count)
1581{
Jesper Juhl6d180252011-03-14 04:05:56 -07001582 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1583 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001584 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585 substring_t args[MAX_OPT_ARGS];
1586 unsigned long long tmp_ll;
1587 u64 sa_res_key = 0;
1588 u32 mapped_lun = 0, target_lun = 0;
1589 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001590 u16 tpgt = 0;
1591 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592
Andy Grovera3541702015-05-19 14:44:41 -07001593 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001594 return 0;
1595 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001596 return 0;
1597
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001598 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001599 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 " active fabric exports exist\n");
1601 return -EINVAL;
1602 }
1603
1604 opts = kstrdup(page, GFP_KERNEL);
1605 if (!opts)
1606 return -ENOMEM;
1607
1608 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001609 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610 if (!*ptr)
1611 continue;
1612
1613 token = match_token(ptr, tokens, args);
1614 switch (token) {
1615 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001616 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001617 if (!i_fabric) {
1618 ret = -ENOMEM;
1619 goto out;
1620 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621 break;
1622 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001623 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001624 if (!i_port) {
1625 ret = -ENOMEM;
1626 goto out;
1627 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001628 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001629 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001630 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1631 PR_APTPL_MAX_IPORT_LEN);
1632 ret = -EINVAL;
1633 break;
1634 }
1635 break;
1636 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001637 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001638 if (!isid) {
1639 ret = -ENOMEM;
1640 goto out;
1641 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001642 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001643 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644 "= exceeds PR_REG_ISID_LEN: %d\n",
1645 PR_REG_ISID_LEN);
1646 ret = -EINVAL;
1647 break;
1648 }
1649 break;
1650 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001651 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001653 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 goto out;
1655 }
1656 sa_res_key = (u64)tmp_ll;
1657 break;
1658 /*
1659 * PR APTPL Metadata for Reservation
1660 */
1661 case Opt_res_holder:
1662 match_int(args, &arg);
1663 res_holder = arg;
1664 break;
1665 case Opt_res_type:
1666 match_int(args, &arg);
1667 type = (u8)arg;
1668 break;
1669 case Opt_res_scope:
1670 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 break;
1672 case Opt_res_all_tg_pt:
1673 match_int(args, &arg);
1674 all_tg_pt = (int)arg;
1675 break;
1676 case Opt_mapped_lun:
1677 match_int(args, &arg);
1678 mapped_lun = (u32)arg;
1679 break;
1680 /*
1681 * PR APTPL Metadata for Target Port
1682 */
1683 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001684 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001685 if (!t_fabric) {
1686 ret = -ENOMEM;
1687 goto out;
1688 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001689 break;
1690 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001691 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001692 if (!t_port) {
1693 ret = -ENOMEM;
1694 goto out;
1695 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001696 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001697 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001698 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1699 PR_APTPL_MAX_TPORT_LEN);
1700 ret = -EINVAL;
1701 break;
1702 }
1703 break;
1704 case Opt_tpgt:
1705 match_int(args, &arg);
1706 tpgt = (u16)arg;
1707 break;
1708 case Opt_port_rtpi:
1709 match_int(args, &arg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710 break;
1711 case Opt_target_lun:
1712 match_int(args, &arg);
1713 target_lun = (u32)arg;
1714 break;
1715 default:
1716 break;
1717 }
1718 }
1719
Andy Grover6708bb22011-06-08 10:36:43 -07001720 if (!i_port || !t_port || !sa_res_key) {
1721 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722 ret = -EINVAL;
1723 goto out;
1724 }
1725
1726 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001727 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728 " holder\n", type);
1729 ret = -EINVAL;
1730 goto out;
1731 }
1732
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001733 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001734 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1735 res_holder, all_tg_pt, type);
1736out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001737 kfree(i_fabric);
1738 kfree(i_port);
1739 kfree(isid);
1740 kfree(t_fabric);
1741 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742 kfree(orig);
1743 return (ret == 0) ? count : ret;
1744}
1745
1746SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1747
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001748CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001749
1750static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1751 &target_core_dev_pr_res_holder.attr,
1752 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1753 &target_core_dev_pr_res_pr_generation.attr,
1754 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1755 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1756 &target_core_dev_pr_res_pr_type.attr,
1757 &target_core_dev_pr_res_type.attr,
1758 &target_core_dev_pr_res_aptpl_active.attr,
1759 &target_core_dev_pr_res_aptpl_metadata.attr,
1760 NULL,
1761};
1762
1763static struct configfs_item_operations target_core_dev_pr_ops = {
1764 .show_attribute = target_core_dev_pr_attr_show,
1765 .store_attribute = target_core_dev_pr_attr_store,
1766};
1767
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001768TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001769
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001770/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001771
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001772/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773
1774static ssize_t target_core_show_dev_info(void *p, char *page)
1775{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001776 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001777 int bl = 0;
1778 ssize_t read_bytes = 0;
1779
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001780 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001782 read_bytes += dev->transport->show_configfs_dev_params(dev,
1783 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001784 return read_bytes;
1785}
1786
1787static struct target_core_configfs_attribute target_core_attr_dev_info = {
1788 .attr = { .ca_owner = THIS_MODULE,
1789 .ca_name = "info",
1790 .ca_mode = S_IRUGO },
1791 .show = target_core_show_dev_info,
1792 .store = NULL,
1793};
1794
1795static ssize_t target_core_store_dev_control(
1796 void *p,
1797 const char *page,
1798 size_t count)
1799{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001800 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001802 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001803}
1804
1805static struct target_core_configfs_attribute target_core_attr_dev_control = {
1806 .attr = { .ca_owner = THIS_MODULE,
1807 .ca_name = "control",
1808 .ca_mode = S_IWUSR },
1809 .show = NULL,
1810 .store = target_core_store_dev_control,
1811};
1812
1813static ssize_t target_core_show_dev_alias(void *p, char *page)
1814{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001815 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001817 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001818 return 0;
1819
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001820 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821}
1822
1823static ssize_t target_core_store_dev_alias(
1824 void *p,
1825 const char *page,
1826 size_t count)
1827{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001828 struct se_device *dev = p;
1829 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 ssize_t read_bytes;
1831
1832 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001833 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1835 SE_DEV_ALIAS_LEN-1);
1836 return -EINVAL;
1837 }
1838
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001839 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001840 if (!read_bytes)
1841 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001842 if (dev->dev_alias[read_bytes - 1] == '\n')
1843 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001844
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001845 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001846
Andy Grover6708bb22011-06-08 10:36:43 -07001847 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001849 config_item_name(&dev->dev_group.cg_item),
1850 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851
1852 return read_bytes;
1853}
1854
1855static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1856 .attr = { .ca_owner = THIS_MODULE,
1857 .ca_name = "alias",
1858 .ca_mode = S_IRUGO | S_IWUSR },
1859 .show = target_core_show_dev_alias,
1860 .store = target_core_store_dev_alias,
1861};
1862
1863static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1864{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001865 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001867 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 return 0;
1869
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001870 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001871}
1872
1873static ssize_t target_core_store_dev_udev_path(
1874 void *p,
1875 const char *page,
1876 size_t count)
1877{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001878 struct se_device *dev = p;
1879 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 ssize_t read_bytes;
1881
1882 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001883 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1885 SE_UDEV_PATH_LEN-1);
1886 return -EINVAL;
1887 }
1888
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001889 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001891 if (!read_bytes)
1892 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001893 if (dev->udev_path[read_bytes - 1] == '\n')
1894 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001895
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001896 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001897
Andy Grover6708bb22011-06-08 10:36:43 -07001898 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001900 config_item_name(&dev->dev_group.cg_item),
1901 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902
1903 return read_bytes;
1904}
1905
1906static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1907 .attr = { .ca_owner = THIS_MODULE,
1908 .ca_name = "udev_path",
1909 .ca_mode = S_IRUGO | S_IWUSR },
1910 .show = target_core_show_dev_udev_path,
1911 .store = target_core_store_dev_udev_path,
1912};
1913
Andy Grover64146db2013-04-30 11:59:15 -07001914static ssize_t target_core_show_dev_enable(void *p, char *page)
1915{
1916 struct se_device *dev = p;
1917
1918 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1919}
1920
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921static ssize_t target_core_store_dev_enable(
1922 void *p,
1923 const char *page,
1924 size_t count)
1925{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001926 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001928 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001929
1930 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001931 if (!ptr) {
1932 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 " is \"1\"\n");
1934 return -EINVAL;
1935 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001937 ret = target_configure_device(dev);
1938 if (ret)
1939 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940 return count;
1941}
1942
1943static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1944 .attr = { .ca_owner = THIS_MODULE,
1945 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001946 .ca_mode = S_IRUGO | S_IWUSR },
1947 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 .store = target_core_store_dev_enable,
1949};
1950
1951static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1952{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001953 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 struct config_item *lu_ci;
1955 struct t10_alua_lu_gp *lu_gp;
1956 struct t10_alua_lu_gp_member *lu_gp_mem;
1957 ssize_t len = 0;
1958
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001959 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001960 if (!lu_gp_mem)
1961 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962
1963 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1964 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001965 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966 lu_ci = &lu_gp->lu_gp_group.cg_item;
1967 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1968 config_item_name(lu_ci), lu_gp->lu_gp_id);
1969 }
1970 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1971
1972 return len;
1973}
1974
1975static ssize_t target_core_store_alua_lu_gp(
1976 void *p,
1977 const char *page,
1978 size_t count)
1979{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001980 struct se_device *dev = p;
1981 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1983 struct t10_alua_lu_gp_member *lu_gp_mem;
1984 unsigned char buf[LU_GROUP_NAME_BUF];
1985 int move = 0;
1986
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001987 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1988 if (!lu_gp_mem)
1989 return 0;
1990
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001992 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993 return -EINVAL;
1994 }
1995 memset(buf, 0, LU_GROUP_NAME_BUF);
1996 memcpy(buf, page, count);
1997 /*
1998 * Any ALUA logical unit alias besides "NULL" means we will be
1999 * making a new group association.
2000 */
2001 if (strcmp(strstrip(buf), "NULL")) {
2002 /*
2003 * core_alua_get_lu_gp_by_name() will increment reference to
2004 * struct t10_alua_lu_gp. This reference is released with
2005 * core_alua_get_lu_gp_by_name below().
2006 */
2007 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07002008 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009 return -ENODEV;
2010 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002011
2012 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2013 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07002014 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002015 /*
2016 * Clearing an existing lu_gp association, and replacing
2017 * with NULL
2018 */
Andy Grover6708bb22011-06-08 10:36:43 -07002019 if (!lu_gp_new) {
2020 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2022 " %hu\n",
2023 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002024 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002025 config_item_name(&lu_gp->lu_gp_group.cg_item),
2026 lu_gp->lu_gp_id);
2027
2028 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2029 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2030
2031 return count;
2032 }
2033 /*
2034 * Removing existing association of lu_gp_mem with lu_gp
2035 */
2036 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2037 move = 1;
2038 }
2039 /*
2040 * Associate lu_gp_mem with lu_gp_new.
2041 */
2042 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2043 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2044
Andy Grover6708bb22011-06-08 10:36:43 -07002045 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 " core/alua/lu_gps/%s, ID: %hu\n",
2047 (move) ? "Moving" : "Adding",
2048 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002049 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002050 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2051 lu_gp_new->lu_gp_id);
2052
2053 core_alua_put_lu_gp_from_name(lu_gp_new);
2054 return count;
2055}
2056
2057static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
2058 .attr = { .ca_owner = THIS_MODULE,
2059 .ca_name = "alua_lu_gp",
2060 .ca_mode = S_IRUGO | S_IWUSR },
2061 .show = target_core_show_alua_lu_gp,
2062 .store = target_core_store_alua_lu_gp,
2063};
2064
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002065static ssize_t target_core_show_dev_lba_map(void *p, char *page)
2066{
2067 struct se_device *dev = p;
2068 struct t10_alua_lba_map *map;
2069 struct t10_alua_lba_map_member *mem;
2070 char *b = page;
2071 int bl = 0;
2072 char state;
2073
2074 spin_lock(&dev->t10_alua.lba_map_lock);
2075 if (!list_empty(&dev->t10_alua.lba_map_list))
2076 bl += sprintf(b + bl, "%u %u\n",
2077 dev->t10_alua.lba_map_segment_size,
2078 dev->t10_alua.lba_map_segment_multiplier);
2079 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2080 bl += sprintf(b + bl, "%llu %llu",
2081 map->lba_map_first_lba, map->lba_map_last_lba);
2082 list_for_each_entry(mem, &map->lba_map_mem_list,
2083 lba_map_mem_list) {
2084 switch (mem->lba_map_mem_alua_state) {
2085 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2086 state = 'O';
2087 break;
2088 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2089 state = 'A';
2090 break;
2091 case ALUA_ACCESS_STATE_STANDBY:
2092 state = 'S';
2093 break;
2094 case ALUA_ACCESS_STATE_UNAVAILABLE:
2095 state = 'U';
2096 break;
2097 default:
2098 state = '.';
2099 break;
2100 }
2101 bl += sprintf(b + bl, " %d:%c",
2102 mem->lba_map_mem_alua_pg_id, state);
2103 }
2104 bl += sprintf(b + bl, "\n");
2105 }
2106 spin_unlock(&dev->t10_alua.lba_map_lock);
2107 return bl;
2108}
2109
2110static ssize_t target_core_store_dev_lba_map(
2111 void *p,
2112 const char *page,
2113 size_t count)
2114{
2115 struct se_device *dev = p;
2116 struct t10_alua_lba_map *lba_map = NULL;
2117 struct list_head lba_list;
2118 char *map_entries, *ptr;
2119 char state;
2120 int pg_num = -1, pg;
2121 int ret = 0, num = 0, pg_id, alua_state;
2122 unsigned long start_lba = -1, end_lba = -1;
2123 unsigned long segment_size = -1, segment_mult = -1;
2124
2125 map_entries = kstrdup(page, GFP_KERNEL);
2126 if (!map_entries)
2127 return -ENOMEM;
2128
2129 INIT_LIST_HEAD(&lba_list);
2130 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2131 if (!*ptr)
2132 continue;
2133
2134 if (num == 0) {
2135 if (sscanf(ptr, "%lu %lu\n",
2136 &segment_size, &segment_mult) != 2) {
2137 pr_err("Invalid line %d\n", num);
2138 ret = -EINVAL;
2139 break;
2140 }
2141 num++;
2142 continue;
2143 }
2144 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2145 pr_err("Invalid line %d\n", num);
2146 ret = -EINVAL;
2147 break;
2148 }
2149 ptr = strchr(ptr, ' ');
2150 if (!ptr) {
2151 pr_err("Invalid line %d, missing end lba\n", num);
2152 ret = -EINVAL;
2153 break;
2154 }
2155 ptr++;
2156 ptr = strchr(ptr, ' ');
2157 if (!ptr) {
2158 pr_err("Invalid line %d, missing state definitions\n",
2159 num);
2160 ret = -EINVAL;
2161 break;
2162 }
2163 ptr++;
2164 lba_map = core_alua_allocate_lba_map(&lba_list,
2165 start_lba, end_lba);
2166 if (IS_ERR(lba_map)) {
2167 ret = PTR_ERR(lba_map);
2168 break;
2169 }
2170 pg = 0;
2171 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2172 switch (state) {
2173 case 'O':
2174 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2175 break;
2176 case 'A':
2177 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2178 break;
2179 case 'S':
2180 alua_state = ALUA_ACCESS_STATE_STANDBY;
2181 break;
2182 case 'U':
2183 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2184 break;
2185 default:
2186 pr_err("Invalid ALUA state '%c'\n", state);
2187 ret = -EINVAL;
2188 goto out;
2189 }
2190
2191 ret = core_alua_allocate_lba_map_mem(lba_map,
2192 pg_id, alua_state);
2193 if (ret) {
2194 pr_err("Invalid target descriptor %d:%c "
2195 "at line %d\n",
2196 pg_id, state, num);
2197 break;
2198 }
2199 pg++;
2200 ptr = strchr(ptr, ' ');
2201 if (ptr)
2202 ptr++;
2203 else
2204 break;
2205 }
2206 if (pg_num == -1)
2207 pg_num = pg;
2208 else if (pg != pg_num) {
2209 pr_err("Only %d from %d port groups definitions "
2210 "at line %d\n", pg, pg_num, num);
2211 ret = -EINVAL;
2212 break;
2213 }
2214 num++;
2215 }
2216out:
2217 if (ret) {
2218 core_alua_free_lba_map(&lba_list);
2219 count = ret;
2220 } else
2221 core_alua_set_lba_map(dev, &lba_list,
2222 segment_size, segment_mult);
2223 kfree(map_entries);
2224 return count;
2225}
2226
2227static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
2228 .attr = { .ca_owner = THIS_MODULE,
2229 .ca_name = "lba_map",
2230 .ca_mode = S_IRUGO | S_IWUSR },
2231 .show = target_core_show_dev_lba_map,
2232 .store = target_core_store_dev_lba_map,
2233};
2234
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002235static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002236 &target_core_attr_dev_info.attr,
2237 &target_core_attr_dev_control.attr,
2238 &target_core_attr_dev_alias.attr,
2239 &target_core_attr_dev_udev_path.attr,
2240 &target_core_attr_dev_enable.attr,
2241 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002242 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243 NULL,
2244};
2245
2246static void target_core_dev_release(struct config_item *item)
2247{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002248 struct config_group *dev_cg = to_config_group(item);
2249 struct se_device *dev =
2250 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002253 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002254}
2255
2256static ssize_t target_core_dev_show(struct config_item *item,
2257 struct configfs_attribute *attr,
2258 char *page)
2259{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002260 struct config_group *dev_cg = to_config_group(item);
2261 struct se_device *dev =
2262 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 struct target_core_configfs_attribute *tc_attr = container_of(
2264 attr, struct target_core_configfs_attribute, attr);
2265
Andy Grover6708bb22011-06-08 10:36:43 -07002266 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267 return -EINVAL;
2268
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002269 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002270}
2271
2272static ssize_t target_core_dev_store(struct config_item *item,
2273 struct configfs_attribute *attr,
2274 const char *page, size_t count)
2275{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002276 struct config_group *dev_cg = to_config_group(item);
2277 struct se_device *dev =
2278 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002279 struct target_core_configfs_attribute *tc_attr = container_of(
2280 attr, struct target_core_configfs_attribute, attr);
2281
Andy Grover6708bb22011-06-08 10:36:43 -07002282 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283 return -EINVAL;
2284
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002285 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002286}
2287
2288static struct configfs_item_operations target_core_dev_item_ops = {
2289 .release = target_core_dev_release,
2290 .show_attribute = target_core_dev_show,
2291 .store_attribute = target_core_dev_store,
2292};
2293
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002294TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002296/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297
2298/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2299
2300CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2301#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2302static struct target_core_alua_lu_gp_attribute \
2303 target_core_alua_lu_gp_##_name = \
2304 __CONFIGFS_EATTR(_name, _mode, \
2305 target_core_alua_lu_gp_show_attr_##_name, \
2306 target_core_alua_lu_gp_store_attr_##_name);
2307
2308#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2309static struct target_core_alua_lu_gp_attribute \
2310 target_core_alua_lu_gp_##_name = \
2311 __CONFIGFS_EATTR_RO(_name, \
2312 target_core_alua_lu_gp_show_attr_##_name);
2313
2314/*
2315 * lu_gp_id
2316 */
2317static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2318 struct t10_alua_lu_gp *lu_gp,
2319 char *page)
2320{
Andy Grover6708bb22011-06-08 10:36:43 -07002321 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 return 0;
2323
2324 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2325}
2326
2327static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2328 struct t10_alua_lu_gp *lu_gp,
2329 const char *page,
2330 size_t count)
2331{
2332 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2333 unsigned long lu_gp_id;
2334 int ret;
2335
Jingoo Han57103d72013-07-19 16:22:19 +09002336 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002337 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002338 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002339 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002340 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002341 }
2342 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002343 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002344 " 0x0000ffff\n", lu_gp_id);
2345 return -EINVAL;
2346 }
2347
2348 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2349 if (ret < 0)
2350 return -EINVAL;
2351
Andy Grover6708bb22011-06-08 10:36:43 -07002352 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2354 config_item_name(&alua_lu_gp_cg->cg_item),
2355 lu_gp->lu_gp_id);
2356
2357 return count;
2358}
2359
2360SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2361
2362/*
2363 * members
2364 */
2365static ssize_t target_core_alua_lu_gp_show_attr_members(
2366 struct t10_alua_lu_gp *lu_gp,
2367 char *page)
2368{
2369 struct se_device *dev;
2370 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002371 struct t10_alua_lu_gp_member *lu_gp_mem;
2372 ssize_t len = 0, cur_len;
2373 unsigned char buf[LU_GROUP_NAME_BUF];
2374
2375 memset(buf, 0, LU_GROUP_NAME_BUF);
2376
2377 spin_lock(&lu_gp->lu_gp_lock);
2378 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2379 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002380 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381
2382 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2383 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002384 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385 cur_len++; /* Extra byte for NULL terminator */
2386
2387 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002388 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389 "_members buffer\n");
2390 break;
2391 }
2392 memcpy(page+len, buf, cur_len);
2393 len += cur_len;
2394 }
2395 spin_unlock(&lu_gp->lu_gp_lock);
2396
2397 return len;
2398}
2399
2400SE_DEV_ALUA_LU_ATTR_RO(members);
2401
2402CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2403
2404static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2405 &target_core_alua_lu_gp_lu_gp_id.attr,
2406 &target_core_alua_lu_gp_members.attr,
2407 NULL,
2408};
2409
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002410static void target_core_alua_lu_gp_release(struct config_item *item)
2411{
2412 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2413 struct t10_alua_lu_gp, lu_gp_group);
2414
2415 core_alua_free_lu_gp(lu_gp);
2416}
2417
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002419 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420 .show_attribute = target_core_alua_lu_gp_attr_show,
2421 .store_attribute = target_core_alua_lu_gp_attr_store,
2422};
2423
2424static struct config_item_type target_core_alua_lu_gp_cit = {
2425 .ct_item_ops = &target_core_alua_lu_gp_ops,
2426 .ct_attrs = target_core_alua_lu_gp_attrs,
2427 .ct_owner = THIS_MODULE,
2428};
2429
2430/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2431
2432/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2433
2434static struct config_group *target_core_alua_create_lu_gp(
2435 struct config_group *group,
2436 const char *name)
2437{
2438 struct t10_alua_lu_gp *lu_gp;
2439 struct config_group *alua_lu_gp_cg = NULL;
2440 struct config_item *alua_lu_gp_ci = NULL;
2441
2442 lu_gp = core_alua_allocate_lu_gp(name, 0);
2443 if (IS_ERR(lu_gp))
2444 return NULL;
2445
2446 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2447 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2448
2449 config_group_init_type_name(alua_lu_gp_cg, name,
2450 &target_core_alua_lu_gp_cit);
2451
Andy Grover6708bb22011-06-08 10:36:43 -07002452 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 " Group: core/alua/lu_gps/%s\n",
2454 config_item_name(alua_lu_gp_ci));
2455
2456 return alua_lu_gp_cg;
2457
2458}
2459
2460static void target_core_alua_drop_lu_gp(
2461 struct config_group *group,
2462 struct config_item *item)
2463{
2464 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2465 struct t10_alua_lu_gp, lu_gp_group);
2466
Andy Grover6708bb22011-06-08 10:36:43 -07002467 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002468 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2469 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002470 /*
2471 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2472 * -> target_core_alua_lu_gp_release()
2473 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002475}
2476
2477static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2478 .make_group = &target_core_alua_create_lu_gp,
2479 .drop_item = &target_core_alua_drop_lu_gp,
2480};
2481
2482static struct config_item_type target_core_alua_lu_gps_cit = {
2483 .ct_item_ops = NULL,
2484 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2485 .ct_owner = THIS_MODULE,
2486};
2487
2488/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2489
2490/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2491
2492CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2493#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2494static struct target_core_alua_tg_pt_gp_attribute \
2495 target_core_alua_tg_pt_gp_##_name = \
2496 __CONFIGFS_EATTR(_name, _mode, \
2497 target_core_alua_tg_pt_gp_show_attr_##_name, \
2498 target_core_alua_tg_pt_gp_store_attr_##_name);
2499
2500#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2501static struct target_core_alua_tg_pt_gp_attribute \
2502 target_core_alua_tg_pt_gp_##_name = \
2503 __CONFIGFS_EATTR_RO(_name, \
2504 target_core_alua_tg_pt_gp_show_attr_##_name);
2505
2506/*
2507 * alua_access_state
2508 */
2509static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2510 struct t10_alua_tg_pt_gp *tg_pt_gp,
2511 char *page)
2512{
2513 return sprintf(page, "%d\n",
2514 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2515}
2516
2517static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2518 struct t10_alua_tg_pt_gp *tg_pt_gp,
2519 const char *page,
2520 size_t count)
2521{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002522 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002523 unsigned long tmp;
2524 int new_state, ret;
2525
Andy Grover6708bb22011-06-08 10:36:43 -07002526 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002527 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2529 return -EINVAL;
2530 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002531 if (!(dev->dev_flags & DF_CONFIGURED)) {
2532 pr_err("Unable to set alua_access_state while device is"
2533 " not configured\n");
2534 return -ENODEV;
2535 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536
Jingoo Han57103d72013-07-19 16:22:19 +09002537 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002539 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002541 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 }
2543 new_state = (int)tmp;
2544
Hannes Reinecke125d0112013-11-19 09:07:46 +01002545 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2546 pr_err("Unable to process implicit configfs ALUA"
2547 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548 return -EINVAL;
2549 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002550 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2551 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2552 /* LBA DEPENDENT is only allowed with implicit ALUA */
2553 pr_err("Unable to process implicit configfs ALUA transition"
2554 " while explicit ALUA management is enabled\n");
2555 return -EINVAL;
2556 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002558 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 NULL, NULL, new_state, 0);
2560 return (!ret) ? count : -EINVAL;
2561}
2562
2563SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2564
2565/*
2566 * alua_access_status
2567 */
2568static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2569 struct t10_alua_tg_pt_gp *tg_pt_gp,
2570 char *page)
2571{
2572 return sprintf(page, "%s\n",
2573 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2574}
2575
2576static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2577 struct t10_alua_tg_pt_gp *tg_pt_gp,
2578 const char *page,
2579 size_t count)
2580{
2581 unsigned long tmp;
2582 int new_status, ret;
2583
Andy Grover6708bb22011-06-08 10:36:43 -07002584 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2585 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 " valid tg_pt_gp ID: %hu\n",
2587 tg_pt_gp->tg_pt_gp_valid_id);
2588 return -EINVAL;
2589 }
2590
Jingoo Han57103d72013-07-19 16:22:19 +09002591 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002592 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002593 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002595 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596 }
2597 new_status = (int)tmp;
2598
2599 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002600 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2601 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002602 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603 new_status);
2604 return -EINVAL;
2605 }
2606
2607 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2608 return count;
2609}
2610
2611SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2612
2613/*
2614 * alua_access_type
2615 */
2616static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2617 struct t10_alua_tg_pt_gp *tg_pt_gp,
2618 char *page)
2619{
2620 return core_alua_show_access_type(tg_pt_gp, page);
2621}
2622
2623static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2624 struct t10_alua_tg_pt_gp *tg_pt_gp,
2625 const char *page,
2626 size_t count)
2627{
2628 return core_alua_store_access_type(tg_pt_gp, page, count);
2629}
2630
2631SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2632
2633/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002634 * alua_supported_states
2635 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002636
2637#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2638static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2639 struct t10_alua_tg_pt_gp *t, char *p) \
2640{ \
2641 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002642}
2643
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002644#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2645static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2646 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2647{ \
2648 unsigned long tmp; \
2649 int ret; \
2650 \
2651 if (!t->tg_pt_gp_valid_id) { \
2652 pr_err("Unable to do set ##_name ALUA state on non" \
2653 " valid tg_pt_gp ID: %hu\n", \
2654 t->tg_pt_gp_valid_id); \
2655 return -EINVAL; \
2656 } \
2657 \
2658 ret = kstrtoul(p, 0, &tmp); \
2659 if (ret < 0) { \
2660 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2661 return -EINVAL; \
2662 } \
2663 if (tmp > 1) { \
2664 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2665 return -EINVAL; \
2666 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002667 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002668 t->_var |= _bit; \
2669 else \
2670 t->_var &= ~_bit; \
2671 \
2672 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002673}
2674
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002675SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2676 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2677SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2678 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2679SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2680
2681SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2682 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2683SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2684 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2685SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2686
2687SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2688 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2689SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2690 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002691SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002692
2693SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2694 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2695SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2696 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2697SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2698
2699SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2700 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2701SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2702 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2703SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2704
2705SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2706 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2707SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2708 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2709SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2710
2711SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2712 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2713SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2714 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2715SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002716
2717/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 * alua_write_metadata
2719 */
2720static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2721 struct t10_alua_tg_pt_gp *tg_pt_gp,
2722 char *page)
2723{
2724 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2725}
2726
2727static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2728 struct t10_alua_tg_pt_gp *tg_pt_gp,
2729 const char *page,
2730 size_t count)
2731{
2732 unsigned long tmp;
2733 int ret;
2734
Jingoo Han57103d72013-07-19 16:22:19 +09002735 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002736 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002737 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002738 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002739 }
2740
2741 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002742 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002743 " %lu\n", tmp);
2744 return -EINVAL;
2745 }
2746 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2747
2748 return count;
2749}
2750
2751SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2752
2753
2754
2755/*
2756 * nonop_delay_msecs
2757 */
2758static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2759 struct t10_alua_tg_pt_gp *tg_pt_gp,
2760 char *page)
2761{
2762 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2763
2764}
2765
2766static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2767 struct t10_alua_tg_pt_gp *tg_pt_gp,
2768 const char *page,
2769 size_t count)
2770{
2771 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2772}
2773
2774SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2775
2776/*
2777 * trans_delay_msecs
2778 */
2779static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2780 struct t10_alua_tg_pt_gp *tg_pt_gp,
2781 char *page)
2782{
2783 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2784}
2785
2786static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2787 struct t10_alua_tg_pt_gp *tg_pt_gp,
2788 const char *page,
2789 size_t count)
2790{
2791 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2792}
2793
2794SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2795
2796/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002797 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002798 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002799static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002800 struct t10_alua_tg_pt_gp *tg_pt_gp,
2801 char *page)
2802{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002803 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002804}
2805
Hannes Reinecke125d0112013-11-19 09:07:46 +01002806static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002807 struct t10_alua_tg_pt_gp *tg_pt_gp,
2808 const char *page,
2809 size_t count)
2810{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002811 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002812}
2813
Hannes Reinecke125d0112013-11-19 09:07:46 +01002814SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002815
2816/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 * preferred
2818 */
2819
2820static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2821 struct t10_alua_tg_pt_gp *tg_pt_gp,
2822 char *page)
2823{
2824 return core_alua_show_preferred_bit(tg_pt_gp, page);
2825}
2826
2827static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2828 struct t10_alua_tg_pt_gp *tg_pt_gp,
2829 const char *page,
2830 size_t count)
2831{
2832 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2833}
2834
2835SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2836
2837/*
2838 * tg_pt_gp_id
2839 */
2840static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2841 struct t10_alua_tg_pt_gp *tg_pt_gp,
2842 char *page)
2843{
Andy Grover6708bb22011-06-08 10:36:43 -07002844 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002845 return 0;
2846
2847 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2848}
2849
2850static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2851 struct t10_alua_tg_pt_gp *tg_pt_gp,
2852 const char *page,
2853 size_t count)
2854{
2855 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2856 unsigned long tg_pt_gp_id;
2857 int ret;
2858
Jingoo Han57103d72013-07-19 16:22:19 +09002859 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002861 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002862 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002863 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 }
2865 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002866 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867 " 0x0000ffff\n", tg_pt_gp_id);
2868 return -EINVAL;
2869 }
2870
2871 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2872 if (ret < 0)
2873 return -EINVAL;
2874
Andy Grover6708bb22011-06-08 10:36:43 -07002875 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2877 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2878 tg_pt_gp->tg_pt_gp_id);
2879
2880 return count;
2881}
2882
2883SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2884
2885/*
2886 * members
2887 */
2888static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2889 struct t10_alua_tg_pt_gp *tg_pt_gp,
2890 char *page)
2891{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 ssize_t len = 0, cur_len;
2894 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2895
2896 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2897
2898 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002899 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2900 lun_tg_pt_gp_link) {
2901 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902
2903 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002904 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2905 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2906 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002907 config_item_name(&lun->lun_group.cg_item));
2908 cur_len++; /* Extra byte for NULL terminator */
2909
2910 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002911 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912 "_members buffer\n");
2913 break;
2914 }
2915 memcpy(page+len, buf, cur_len);
2916 len += cur_len;
2917 }
2918 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2919
2920 return len;
2921}
2922
2923SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2924
2925CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2926 tg_pt_gp_group);
2927
2928static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2929 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2930 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2931 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002932 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2933 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2934 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2935 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2936 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2937 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2938 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002939 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2940 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2941 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002942 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 &target_core_alua_tg_pt_gp_preferred.attr,
2944 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2945 &target_core_alua_tg_pt_gp_members.attr,
2946 NULL,
2947};
2948
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002949static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2950{
2951 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2952 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2953
2954 core_alua_free_tg_pt_gp(tg_pt_gp);
2955}
2956
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002958 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2960 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2961};
2962
2963static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2964 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2965 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2966 .ct_owner = THIS_MODULE,
2967};
2968
2969/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2970
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002971/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972
2973static struct config_group *target_core_alua_create_tg_pt_gp(
2974 struct config_group *group,
2975 const char *name)
2976{
2977 struct t10_alua *alua = container_of(group, struct t10_alua,
2978 alua_tg_pt_gps_group);
2979 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 struct config_group *alua_tg_pt_gp_cg = NULL;
2981 struct config_item *alua_tg_pt_gp_ci = NULL;
2982
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002983 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002984 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002985 return NULL;
2986
2987 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2988 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2989
2990 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2991 &target_core_alua_tg_pt_gp_cit);
2992
Andy Grover6708bb22011-06-08 10:36:43 -07002993 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994 " Group: alua/tg_pt_gps/%s\n",
2995 config_item_name(alua_tg_pt_gp_ci));
2996
2997 return alua_tg_pt_gp_cg;
2998}
2999
3000static void target_core_alua_drop_tg_pt_gp(
3001 struct config_group *group,
3002 struct config_item *item)
3003{
3004 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3005 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3006
Andy Grover6708bb22011-06-08 10:36:43 -07003007 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3009 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003010 /*
3011 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3012 * -> target_core_alua_tg_pt_gp_release().
3013 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003014 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003015}
3016
3017static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3018 .make_group = &target_core_alua_create_tg_pt_gp,
3019 .drop_item = &target_core_alua_drop_tg_pt_gp,
3020};
3021
Nicholas Bellinger72aca572014-11-27 15:06:23 -08003022TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003023
Nicholas Bellinger72aca572014-11-27 15:06:23 -08003024/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025
3026/* Start functions for struct config_item_type target_core_alua_cit */
3027
3028/*
3029 * target_core_alua_cit is a ConfigFS group that lives under
3030 * /sys/kernel/config/target/core/alua. There are default groups
3031 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3032 * target_core_alua_cit in target_core_init_configfs() below.
3033 */
3034static struct config_item_type target_core_alua_cit = {
3035 .ct_item_ops = NULL,
3036 .ct_attrs = NULL,
3037 .ct_owner = THIS_MODULE,
3038};
3039
3040/* End functions for struct config_item_type target_core_alua_cit */
3041
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08003042/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003043
3044static struct config_group *target_core_stat_mkdir(
3045 struct config_group *group,
3046 const char *name)
3047{
3048 return ERR_PTR(-ENOSYS);
3049}
3050
3051static void target_core_stat_rmdir(
3052 struct config_group *group,
3053 struct config_item *item)
3054{
3055 return;
3056}
3057
3058static struct configfs_group_operations target_core_stat_group_ops = {
3059 .make_group = &target_core_stat_mkdir,
3060 .drop_item = &target_core_stat_rmdir,
3061};
3062
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08003063TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003064
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08003065/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003066
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003067/* Start functions for struct config_item_type target_core_hba_cit */
3068
3069static struct config_group *target_core_make_subdev(
3070 struct config_group *group,
3071 const char *name)
3072{
3073 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003074 struct config_item *hba_ci = &group->cg_item;
3075 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003076 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003077 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003079 struct config_group *dev_stat_grp = NULL;
3080 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003081
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003082 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3083 if (ret)
3084 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003086 dev = target_alloc_device(hba, name);
3087 if (!dev)
3088 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003089
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003090 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003092 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003094 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003095 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003096
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003097 config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003098 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003099 &tb->tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003100 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003101 &tb->tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003102 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003103 &tb->tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003104 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003105 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003106 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003107 "statistics", &tb->tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003108
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003109 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
3110 dev_cg->default_groups[1] = &dev->dev_pr_group;
3111 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
3112 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
3113 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003114 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003116 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003118 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07003119 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003120 goto out_free_dev_cg_default_groups;
3121 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003122
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003123 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003124 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003125 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003126 if (!tg_pt_gp_cg->default_groups) {
3127 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003129 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130 }
3131
3132 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3133 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
3134 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
3135 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003136 /*
3137 * Add core/$HBA/$DEV/statistics/ default groups
3138 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003139 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003140 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003141 GFP_KERNEL);
3142 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07003143 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003144 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003145 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003146 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147
3148 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003149 return dev_cg;
3150
3151out_free_tg_pt_gp_cg_default_groups:
3152 kfree(tg_pt_gp_cg->default_groups);
3153out_free_tg_pt_gp:
3154 core_alua_free_tg_pt_gp(tg_pt_gp);
3155out_free_dev_cg_default_groups:
3156 kfree(dev_cg->default_groups);
3157out_free_device:
3158 target_free_device(dev);
3159out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003161 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162}
3163
3164static void target_core_drop_subdev(
3165 struct config_group *group,
3166 struct config_item *item)
3167{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003168 struct config_group *dev_cg = to_config_group(item);
3169 struct se_device *dev =
3170 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003173 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003174 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003176 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003177
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003178 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003179
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003180 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07003181 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
3182 df_item = &dev_stat_grp->default_groups[i]->cg_item;
3183 dev_stat_grp->default_groups[i] = NULL;
3184 config_item_put(df_item);
3185 }
3186 kfree(dev_stat_grp->default_groups);
3187
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003188 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
3190 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
3191 tg_pt_gp_cg->default_groups[i] = NULL;
3192 config_item_put(df_item);
3193 }
3194 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003195 /*
3196 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3197 * directly from target_core_alua_tg_pt_gp_release().
3198 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003199 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 for (i = 0; dev_cg->default_groups[i]; i++) {
3202 df_item = &dev_cg->default_groups[i]->cg_item;
3203 dev_cg->default_groups[i] = NULL;
3204 config_item_put(df_item);
3205 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003207 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003208 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003209 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003210 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211}
3212
3213static struct configfs_group_operations target_core_hba_group_ops = {
3214 .make_group = target_core_make_subdev,
3215 .drop_item = target_core_drop_subdev,
3216};
3217
3218CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
3219#define SE_HBA_ATTR(_name, _mode) \
3220static struct target_core_hba_attribute \
3221 target_core_hba_##_name = \
3222 __CONFIGFS_EATTR(_name, _mode, \
3223 target_core_hba_show_attr_##_name, \
3224 target_core_hba_store_attr_##_name);
3225
3226#define SE_HBA_ATTR_RO(_name) \
3227static struct target_core_hba_attribute \
3228 target_core_hba_##_name = \
3229 __CONFIGFS_EATTR_RO(_name, \
3230 target_core_hba_show_attr_##_name);
3231
3232static ssize_t target_core_hba_show_attr_hba_info(
3233 struct se_hba *hba,
3234 char *page)
3235{
3236 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003237 hba->hba_id, hba->backend->ops->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238 TARGET_CORE_CONFIGFS_VERSION);
3239}
3240
3241SE_HBA_ATTR_RO(hba_info);
3242
3243static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
3244 char *page)
3245{
3246 int hba_mode = 0;
3247
3248 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3249 hba_mode = 1;
3250
3251 return sprintf(page, "%d\n", hba_mode);
3252}
3253
3254static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
3255 const char *page, size_t count)
3256{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003257 unsigned long mode_flag;
3258 int ret;
3259
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003260 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003261 return -EINVAL;
3262
Jingoo Han57103d72013-07-19 16:22:19 +09003263 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003264 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003265 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003266 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003267 }
3268
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003269 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07003270 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003271 return -EINVAL;
3272 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003273
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003274 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003275 if (ret < 0)
3276 return -EINVAL;
3277 if (ret > 0)
3278 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3279 else if (ret == 0)
3280 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3281
3282 return count;
3283}
3284
3285SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3286
3287CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3288
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003289static void target_core_hba_release(struct config_item *item)
3290{
3291 struct se_hba *hba = container_of(to_config_group(item),
3292 struct se_hba, hba_group);
3293 core_delete_hba(hba);
3294}
3295
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003296static struct configfs_attribute *target_core_hba_attrs[] = {
3297 &target_core_hba_hba_info.attr,
3298 &target_core_hba_hba_mode.attr,
3299 NULL,
3300};
3301
3302static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003303 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003304 .show_attribute = target_core_hba_attr_show,
3305 .store_attribute = target_core_hba_attr_store,
3306};
3307
3308static struct config_item_type target_core_hba_cit = {
3309 .ct_item_ops = &target_core_hba_item_ops,
3310 .ct_group_ops = &target_core_hba_group_ops,
3311 .ct_attrs = target_core_hba_attrs,
3312 .ct_owner = THIS_MODULE,
3313};
3314
3315static struct config_group *target_core_call_addhbatotarget(
3316 struct config_group *group,
3317 const char *name)
3318{
3319 char *se_plugin_str, *str, *str2;
3320 struct se_hba *hba;
3321 char buf[TARGET_CORE_NAME_MAX_LEN];
3322 unsigned long plugin_dep_id = 0;
3323 int ret;
3324
3325 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003326 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003327 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003328 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3329 TARGET_CORE_NAME_MAX_LEN);
3330 return ERR_PTR(-ENAMETOOLONG);
3331 }
3332 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3333
3334 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003335 if (!str) {
3336 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003337 return ERR_PTR(-EINVAL);
3338 }
3339 se_plugin_str = buf;
3340 /*
3341 * Special case for subsystem plugins that have "_" in their names.
3342 * Namely rd_direct and rd_mcp..
3343 */
3344 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003345 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003346 *str2 = '\0'; /* Terminate for *se_plugin_str */
3347 str2++; /* Skip to start of plugin dependent ID */
3348 str = str2;
3349 } else {
3350 *str = '\0'; /* Terminate for *se_plugin_str */
3351 str++; /* Skip to start of plugin dependent ID */
3352 }
3353
Jingoo Han57103d72013-07-19 16:22:19 +09003354 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003356 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003357 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003358 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003359 }
3360 /*
3361 * Load up TCM subsystem plugins if they have not already been loaded.
3362 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003363 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364
3365 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3366 if (IS_ERR(hba))
3367 return ERR_CAST(hba);
3368
3369 config_group_init_type_name(&hba->hba_group, name,
3370 &target_core_hba_cit);
3371
3372 return &hba->hba_group;
3373}
3374
3375static void target_core_call_delhbafromtarget(
3376 struct config_group *group,
3377 struct config_item *item)
3378{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003379 /*
3380 * core_delete_hba() is called from target_core_hba_item_ops->release()
3381 * -> target_core_hba_release()
3382 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003383 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003384}
3385
3386static struct configfs_group_operations target_core_group_ops = {
3387 .make_group = target_core_call_addhbatotarget,
3388 .drop_item = target_core_call_delhbafromtarget,
3389};
3390
3391static struct config_item_type target_core_cit = {
3392 .ct_item_ops = NULL,
3393 .ct_group_ops = &target_core_group_ops,
3394 .ct_attrs = NULL,
3395 .ct_owner = THIS_MODULE,
3396};
3397
3398/* Stop functions for struct config_item_type target_core_hba_cit */
3399
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003400void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003401{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003402 target_core_setup_dev_cit(tb);
3403 target_core_setup_dev_attrib_cit(tb);
3404 target_core_setup_dev_pr_cit(tb);
3405 target_core_setup_dev_wwn_cit(tb);
3406 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3407 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003408}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003409
Axel Lin54550fa2011-03-14 04:06:09 -07003410static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003411{
3412 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3413 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003414 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003415 struct t10_alua_lu_gp *lu_gp;
3416 int ret;
3417
Andy Grover6708bb22011-06-08 10:36:43 -07003418 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003419 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3420 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3421
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422 config_group_init(&subsys->su_group);
3423 mutex_init(&subsys->su_mutex);
3424
Andy Grovere3d6f902011-07-19 08:55:10 +00003425 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003426 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003427 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003428 /*
3429 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3430 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3431 */
3432 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003433 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003434 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003435 if (!target_cg->default_groups) {
3436 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003437 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438 goto out_global;
3439 }
3440
Andy Grovere3d6f902011-07-19 08:55:10 +00003441 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003442 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003443 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003444 target_cg->default_groups[1] = NULL;
3445 /*
3446 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3447 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003448 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003449 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003450 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003451 if (!hba_cg->default_groups) {
3452 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003453 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003454 goto out_global;
3455 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003456 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003457 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003458 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003459 hba_cg->default_groups[1] = NULL;
3460 /*
3461 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3462 * groups under /sys/kernel/config/target/core/alua/
3463 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003464 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003465 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003466 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003467 if (!alua_cg->default_groups) {
3468 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003469 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 goto out_global;
3471 }
3472
Andy Grovere3d6f902011-07-19 08:55:10 +00003473 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003474 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003475 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003476 alua_cg->default_groups[1] = NULL;
3477 /*
3478 * Add core/alua/lu_gps/default_lu_gp
3479 */
3480 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003481 if (IS_ERR(lu_gp)) {
3482 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003483 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003484 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003485
Andy Grovere3d6f902011-07-19 08:55:10 +00003486 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003487 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003488 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003489 if (!lu_gp_cg->default_groups) {
3490 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003491 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003492 goto out_global;
3493 }
3494
3495 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3496 &target_core_alua_lu_gp_cit);
3497 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3498 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003499 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 /*
3501 * Register the target_core_mod subsystem with configfs.
3502 */
3503 ret = configfs_register_subsystem(subsys);
3504 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003505 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003506 ret, subsys->su_group.cg_item.ci_namebuf);
3507 goto out_global;
3508 }
Andy Grover6708bb22011-06-08 10:36:43 -07003509 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003510 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3511 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3512 /*
3513 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3514 */
3515 ret = rd_module_init();
3516 if (ret < 0)
3517 goto out;
3518
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003519 ret = core_dev_setup_virtual_lun0();
3520 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003521 goto out;
3522
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003523 ret = target_xcopy_setup_pt();
3524 if (ret < 0)
3525 goto out;
3526
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003527 return 0;
3528
3529out:
3530 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003531 core_dev_release_virtual_lun0();
3532 rd_module_exit();
3533out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003534 if (default_lu_gp) {
3535 core_alua_free_lu_gp(default_lu_gp);
3536 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537 }
3538 if (lu_gp_cg)
3539 kfree(lu_gp_cg->default_groups);
3540 if (alua_cg)
3541 kfree(alua_cg->default_groups);
3542 if (hba_cg)
3543 kfree(hba_cg->default_groups);
3544 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003545 release_se_kmem_caches();
3546 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003547}
3548
Axel Lin54550fa2011-03-14 04:06:09 -07003549static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003550{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003551 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3552 struct config_item *item;
3553 int i;
3554
Andy Grovere3d6f902011-07-19 08:55:10 +00003555 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003556 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3557 item = &lu_gp_cg->default_groups[i]->cg_item;
3558 lu_gp_cg->default_groups[i] = NULL;
3559 config_item_put(item);
3560 }
3561 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003562 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003563
Andy Grovere3d6f902011-07-19 08:55:10 +00003564 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003565 for (i = 0; alua_cg->default_groups[i]; i++) {
3566 item = &alua_cg->default_groups[i]->cg_item;
3567 alua_cg->default_groups[i] = NULL;
3568 config_item_put(item);
3569 }
3570 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003571 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003572
Andy Grovere3d6f902011-07-19 08:55:10 +00003573 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003574 for (i = 0; hba_cg->default_groups[i]; i++) {
3575 item = &hba_cg->default_groups[i]->cg_item;
3576 hba_cg->default_groups[i] = NULL;
3577 config_item_put(item);
3578 }
3579 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003580 hba_cg->default_groups = NULL;
3581 /*
3582 * We expect subsys->su_group.default_groups to be released
3583 * by configfs subsystem provider logic..
3584 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003585 configfs_unregister_subsystem(&target_core_fabrics);
3586 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003587
Andy Grovere3d6f902011-07-19 08:55:10 +00003588 core_alua_free_lu_gp(default_lu_gp);
3589 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003590
Andy Grover6708bb22011-06-08 10:36:43 -07003591 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003592 " Infrastructure\n");
3593
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003594 core_dev_release_virtual_lun0();
3595 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003596 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003597 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003598}
3599
3600MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3601MODULE_AUTHOR("nab@Linux-iSCSI.org");
3602MODULE_LICENSE("GPL");
3603
3604module_init(target_core_init_configfs);
3605module_exit(target_core_exit_configfs);