blob: d498533f09ee469d1cc82944cabd75bb525f94b3 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2008-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#include <generated/utsrelease.h>
26#include <linux/utsname.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/namei.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32#include <linux/delay.h>
33#include <linux/unistd.h>
34#include <linux/string.h>
35#include <linux/parser.h>
36#include <linux/syscalls.h>
37#include <linux/configfs.h>
Andy Grovere3d6f902011-07-19 08:55:10 +000038#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039
40#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050041#include <target/target_core_backend.h>
42#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043
Christoph Hellwige26d99a2011-11-14 12:30:30 -050044#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046#include "target_core_pr.h"
47#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070048#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080050#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020051static void target_core_setup_##_name##_cit(struct target_backend *tb) \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080052{ \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020053 struct config_item_type *cit = &tb->tb_##_name##_cit; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080054 \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = _attrs; \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020058 cit->ct_owner = tb->ops->owner; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
60}
61
62#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
63static void target_core_setup_##_name##_cit(struct target_backend *tb) \
64{ \
65 struct config_item_type *cit = &tb->tb_##_name##_cit; \
66 \
67 cit->ct_item_ops = _item_ops; \
68 cit->ct_group_ops = _group_ops; \
69 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
70 cit->ct_owner = tb->ops->owner; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080071 pr_debug("Setup generic %s\n", __stringify(_name)); \
72}
73
Andy Grovere3d6f902011-07-19 08:55:10 +000074extern struct t10_alua_lu_gp *default_lu_gp;
75
Roland Dreierd0f474e2012-01-12 10:41:18 -080076static LIST_HEAD(g_tf_list);
77static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grovere3d6f902011-07-19 08:55:10 +000079static struct config_group target_core_hbagroup;
80static struct config_group alua_group;
81static struct config_group alua_lu_gps_group;
82
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083static inline struct se_hba *
84item_to_hba(struct config_item *item)
85{
86 return container_of(to_config_group(item), struct se_hba, hba_group);
87}
88
89/*
90 * Attributes for /sys/kernel/config/target/
91 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +020092static ssize_t target_core_item_version_show(struct config_item *item,
93 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094{
95 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
Christoph Hellwigce8dd252015-06-19 15:14:39 +020096 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 utsname()->sysname, utsname()->machine);
98}
99
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200100CONFIGFS_ATTR_RO(target_core_item_, version);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101
102static struct target_fabric_configfs *target_core_get_fabric(
103 const char *name)
104{
105 struct target_fabric_configfs *tf;
106
Andy Grover6708bb22011-06-08 10:36:43 -0700107 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 return NULL;
109
110 mutex_lock(&g_tf_lock);
111 list_for_each_entry(tf, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200112 if (!strcmp(tf->tf_ops->name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 atomic_inc(&tf->tf_access_cnt);
114 mutex_unlock(&g_tf_lock);
115 return tf;
116 }
117 }
118 mutex_unlock(&g_tf_lock);
119
120 return NULL;
121}
122
123/*
124 * Called from struct target_core_group_ops->make_group()
125 */
126static struct config_group *target_core_register_fabric(
127 struct config_group *group,
128 const char *name)
129{
130 struct target_fabric_configfs *tf;
131 int ret;
132
Andy Grover6708bb22011-06-08 10:36:43 -0700133 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135
136 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700137 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700138 pr_debug("target_core_register_fabric() trying autoload for %s\n",
139 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800140
141 /*
142 * Below are some hardcoded request_module() calls to automatically
143 * local fabric modules when the following is called:
144 *
145 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
146 *
147 * Note that this does not limit which TCM fabric module can be
148 * registered, but simply provids auto loading logic for modules with
149 * mkdir(2) system calls with known TCM fabric modules.
150 */
151
152 if (!strncmp(name, "iscsi", 5)) {
153 /*
154 * Automatically load the LIO Target fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/iscsi
158 */
159 ret = request_module("iscsi_target_mod");
160 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700161 pr_debug("request_module() failed for"
162 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800163 return ERR_PTR(-EINVAL);
164 }
165 } else if (!strncmp(name, "loopback", 8)) {
166 /*
167 * Automatically load the tcm_loop fabric module when the
168 * following is called:
169 *
170 * mkdir -p $CONFIGFS/target/loopback
171 */
172 ret = request_module("tcm_loop");
173 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700174 pr_debug("request_module() failed for"
175 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800176 return ERR_PTR(-EINVAL);
177 }
178 }
179
180 tf = target_core_get_fabric(name);
181 }
182
183 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700184 pr_debug("target_core_get_fabric() failed for %s\n",
185 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800186 return ERR_PTR(-EINVAL);
187 }
Andy Grover6708bb22011-06-08 10:36:43 -0700188 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200189 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 /*
191 * On a successful target_core_get_fabric() look, the returned
192 * struct target_fabric_configfs *tf will contain a usage reference.
193 */
Andy Grover6708bb22011-06-08 10:36:43 -0700194 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200195 &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200197 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200200 &tf->tf_discovery_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100201 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202
Andy Grover6708bb22011-06-08 10:36:43 -0700203 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205 return &tf->tf_group;
206}
207
208/*
209 * Called from struct target_core_group_ops->drop_item()
210 */
211static void target_core_deregister_fabric(
212 struct config_group *group,
213 struct config_item *item)
214{
215 struct target_fabric_configfs *tf = container_of(
216 to_config_group(item), struct target_fabric_configfs, tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217
Andy Grover6708bb22011-06-08 10:36:43 -0700218 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800219 " tf list\n", config_item_name(item));
220
Andy Grover6708bb22011-06-08 10:36:43 -0700221 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200222 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800223 atomic_dec(&tf->tf_access_cnt);
224
Andy Grover6708bb22011-06-08 10:36:43 -0700225 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226 " %s\n", config_item_name(item));
227
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100228 configfs_remove_default_groups(&tf->tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 config_item_put(item);
230}
231
232static struct configfs_group_operations target_core_fabric_group_ops = {
233 .make_group = &target_core_register_fabric,
234 .drop_item = &target_core_deregister_fabric,
235};
236
237/*
238 * All item attributes appearing in /sys/kernel/target/ appear here.
239 */
240static struct configfs_attribute *target_core_fabric_item_attrs[] = {
241 &target_core_item_attr_version,
242 NULL,
243};
244
245/*
246 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
247 */
248static struct config_item_type target_core_fabrics_item = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249 .ct_group_ops = &target_core_fabric_group_ops,
250 .ct_attrs = target_core_fabric_item_attrs,
251 .ct_owner = THIS_MODULE,
252};
253
254static struct configfs_subsystem target_core_fabrics = {
255 .su_group = {
256 .cg_item = {
257 .ci_namebuf = "target",
258 .ci_type = &target_core_fabrics_item,
259 },
260 },
261};
262
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200263int target_depend_item(struct config_item *item)
264{
265 return configfs_depend_item(&target_core_fabrics, item);
266}
267EXPORT_SYMBOL(target_depend_item);
268
269void target_undepend_item(struct config_item *item)
270{
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100271 return configfs_undepend_item(item);
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200272}
273EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800274
275/*##############################################################################
276// Start functions called by external Target Fabrics Modules
277//############################################################################*/
278
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200279static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200281 if (!tfo->name) {
282 pr_err("Missing tfo->name\n");
283 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200285 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700286 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200287 "_NAME_SIZE\n", tfo->name);
288 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289 }
Andy Grover6708bb22011-06-08 10:36:43 -0700290 if (!tfo->get_fabric_name) {
291 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292 return -EINVAL;
293 }
Andy Grover6708bb22011-06-08 10:36:43 -0700294 if (!tfo->tpg_get_wwn) {
295 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800296 return -EINVAL;
297 }
Andy Grover6708bb22011-06-08 10:36:43 -0700298 if (!tfo->tpg_get_tag) {
299 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800300 return -EINVAL;
301 }
Andy Grover6708bb22011-06-08 10:36:43 -0700302 if (!tfo->tpg_check_demo_mode) {
303 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 return -EINVAL;
305 }
Andy Grover6708bb22011-06-08 10:36:43 -0700306 if (!tfo->tpg_check_demo_mode_cache) {
307 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308 return -EINVAL;
309 }
Andy Grover6708bb22011-06-08 10:36:43 -0700310 if (!tfo->tpg_check_demo_mode_write_protect) {
311 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800312 return -EINVAL;
313 }
Andy Grover6708bb22011-06-08 10:36:43 -0700314 if (!tfo->tpg_check_prod_mode_write_protect) {
315 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316 return -EINVAL;
317 }
Andy Grover6708bb22011-06-08 10:36:43 -0700318 if (!tfo->tpg_get_inst_index) {
319 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 return -EINVAL;
321 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400322 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700323 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 return -EINVAL;
325 }
Andy Grover6708bb22011-06-08 10:36:43 -0700326 if (!tfo->shutdown_session) {
327 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 return -EINVAL;
329 }
Andy Grover6708bb22011-06-08 10:36:43 -0700330 if (!tfo->close_session) {
331 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800332 return -EINVAL;
333 }
Andy Grover6708bb22011-06-08 10:36:43 -0700334 if (!tfo->sess_get_index) {
335 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336 return -EINVAL;
337 }
Andy Grover6708bb22011-06-08 10:36:43 -0700338 if (!tfo->write_pending) {
339 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800340 return -EINVAL;
341 }
Andy Grover6708bb22011-06-08 10:36:43 -0700342 if (!tfo->write_pending_status) {
343 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344 return -EINVAL;
345 }
Andy Grover6708bb22011-06-08 10:36:43 -0700346 if (!tfo->set_default_node_attributes) {
347 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800348 return -EINVAL;
349 }
Andy Grover6708bb22011-06-08 10:36:43 -0700350 if (!tfo->get_cmd_state) {
351 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352 return -EINVAL;
353 }
Andy Grover6708bb22011-06-08 10:36:43 -0700354 if (!tfo->queue_data_in) {
355 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800356 return -EINVAL;
357 }
Andy Grover6708bb22011-06-08 10:36:43 -0700358 if (!tfo->queue_status) {
359 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 return -EINVAL;
361 }
Andy Grover6708bb22011-06-08 10:36:43 -0700362 if (!tfo->queue_tm_rsp) {
363 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800364 return -EINVAL;
365 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700366 if (!tfo->aborted_task) {
367 pr_err("Missing tfo->aborted_task()\n");
368 return -EINVAL;
369 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 /*
371 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
372 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
373 * target_core_fabric_configfs.c WWN+TPG group context code.
374 */
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!tfo->fabric_make_wwn) {
376 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 return -EINVAL;
378 }
Andy Grover6708bb22011-06-08 10:36:43 -0700379 if (!tfo->fabric_drop_wwn) {
380 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 return -EINVAL;
382 }
Andy Grover6708bb22011-06-08 10:36:43 -0700383 if (!tfo->fabric_make_tpg) {
384 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800385 return -EINVAL;
386 }
Andy Grover6708bb22011-06-08 10:36:43 -0700387 if (!tfo->fabric_drop_tpg) {
388 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 return -EINVAL;
390 }
391
392 return 0;
393}
394
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200395int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200397 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 int ret;
399
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200400 ret = target_fabric_tf_ops_check(fo);
401 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return ret;
403
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200404 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700405 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200406 pr_err("%s: could not allocate memory!\n", __func__);
407 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200409
410 INIT_LIST_HEAD(&tf->tf_list);
411 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200412 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200413 target_fabric_setup_cits(tf);
414
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200416 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417 mutex_unlock(&g_tf_lock);
418
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200419 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200421EXPORT_SYMBOL(target_register_template);
422
423void target_unregister_template(const struct target_core_fabric_ops *fo)
424{
425 struct target_fabric_configfs *t;
426
427 mutex_lock(&g_tf_lock);
428 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200429 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200430 BUG_ON(atomic_read(&t->tf_access_cnt));
431 list_del(&t->tf_list);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700432 mutex_unlock(&g_tf_lock);
433 /*
434 * Wait for any outstanding fabric se_deve_entry->rcu_head
435 * callbacks to complete post kfree_rcu(), before allowing
436 * fabric driver unload of TFO->module to proceed.
437 */
438 rcu_barrier();
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200439 kfree(t);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700440 return;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200441 }
442 }
443 mutex_unlock(&g_tf_lock);
444}
445EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446
447/*##############################################################################
448// Stop functions called by external Target Fabrics Modules
449//############################################################################*/
450
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200451static inline struct se_dev_attrib *to_attrib(struct config_item *item)
452{
453 return container_of(to_config_group(item), struct se_dev_attrib,
454 da_group);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200455}
456
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200457/* Start functions for struct config_item_type tb_dev_attrib_cit */
458#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
459static ssize_t _name##_show(struct config_item *item, char *page) \
460{ \
461 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
462}
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200463
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200464DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
465DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
466DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
467DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
468DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
469DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
470DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
471DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
472DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
473DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
474DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
475DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
476DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
477DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
478DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
479DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
480DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
481DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
482DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
483DEF_CONFIGFS_ATTRIB_SHOW(block_size);
484DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
485DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
486DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
487DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
488DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
489DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
490DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
491DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
Jamie Pocase6f41632015-11-29 14:44:57 -0800492DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200493DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
494
495#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
496static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200497 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200498{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200499 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200500 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200501 int ret; \
502 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200503 ret = kstrtou32(page, 0, &val); \
504 if (ret < 0) \
505 return ret; \
506 da->_name = val; \
507 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200508}
509
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200510DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
511DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
512DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
513DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
514DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200515
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200516#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
517static ssize_t _name##_store(struct config_item *item, const char *page, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200518 size_t count) \
519{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200520 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200521 bool flag; \
522 int ret; \
523 \
524 ret = strtobool(page, &flag); \
525 if (ret < 0) \
526 return ret; \
527 da->_name = flag; \
528 return count; \
529}
530
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200531DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
532DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
533DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
534DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
535DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200536
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200537#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
538static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200539 size_t count) \
540{ \
541 printk_once(KERN_WARNING \
Christophe Vu-Brugier234bdbc2015-11-18 09:22:58 +0100542 "ignoring deprecated %s attribute\n", \
543 __stringify(_name)); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200544 return count; \
545}
546
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200547DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
548DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200549
550static void dev_set_t10_wwn_model_alias(struct se_device *dev)
551{
552 const char *configname;
553
554 configname = config_item_name(&dev->dev_group.cg_item);
555 if (strlen(configname) >= 16) {
556 pr_warn("dev[%p]: Backstore name '%s' is too long for "
557 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
558 configname);
559 }
560 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
561}
562
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200563static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200564 const char *page, size_t count)
565{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200566 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200567 struct se_device *dev = da->da_dev;
568 bool flag;
569 int ret;
570
571 if (dev->export_count) {
572 pr_err("dev[%p]: Unable to change model alias"
573 " while export_count is %d\n",
574 dev, dev->export_count);
575 return -EINVAL;
576 }
577
578 ret = strtobool(page, &flag);
579 if (ret < 0)
580 return ret;
581
582 if (flag) {
583 dev_set_t10_wwn_model_alias(dev);
584 } else {
585 strncpy(&dev->t10_wwn.model[0],
586 dev->transport->inquiry_prod, 16);
587 }
588 da->emulate_model_alias = flag;
589 return count;
590}
591
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200592static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200593 const char *page, size_t count)
594{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200595 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200596 bool flag;
597 int ret;
598
599 ret = strtobool(page, &flag);
600 if (ret < 0)
601 return ret;
602
603 if (flag && da->da_dev->transport->get_write_cache) {
604 pr_err("emulate_write_cache not supported for this device\n");
605 return -EINVAL;
606 }
607
608 da->emulate_write_cache = flag;
609 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
610 da->da_dev, flag);
611 return count;
612}
613
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200614static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200615 const char *page, size_t count)
616{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200617 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200618 u32 val;
619 int ret;
620
621 ret = kstrtou32(page, 0, &val);
622 if (ret < 0)
623 return ret;
624
625 if (val != 0 && val != 1 && val != 2) {
626 pr_err("Illegal value %d\n", val);
627 return -EINVAL;
628 }
629
630 if (da->da_dev->export_count) {
631 pr_err("dev[%p]: Unable to change SE Device"
632 " UA_INTRLCK_CTRL while export_count is %d\n",
633 da->da_dev, da->da_dev->export_count);
634 return -EINVAL;
635 }
636 da->emulate_ua_intlck_ctrl = val;
637 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
638 da->da_dev, val);
639 return count;
640}
641
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200642static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200643 const char *page, size_t count)
644{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200645 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200646 bool flag;
647 int ret;
648
649 ret = strtobool(page, &flag);
650 if (ret < 0)
651 return ret;
652
653 if (da->da_dev->export_count) {
654 pr_err("dev[%p]: Unable to change SE Device TAS while"
655 " export_count is %d\n",
656 da->da_dev, da->da_dev->export_count);
657 return -EINVAL;
658 }
659 da->emulate_tas = flag;
660 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
661 da->da_dev, flag ? "Enabled" : "Disabled");
662
663 return count;
664}
665
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200666static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200667 const char *page, size_t count)
668{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200669 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200670 bool flag;
671 int ret;
672
673 ret = strtobool(page, &flag);
674 if (ret < 0)
675 return ret;
676
677 /*
678 * We expect this value to be non-zero when generic Block Layer
679 * Discard supported is detected iblock_create_virtdevice().
680 */
681 if (flag && !da->max_unmap_block_desc_count) {
682 pr_err("Generic Block Discard not supported\n");
683 return -ENOSYS;
684 }
685
686 da->emulate_tpu = flag;
687 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
688 da->da_dev, flag);
689 return count;
690}
691
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200692static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200693 const char *page, size_t count)
694{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200695 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200696 bool flag;
697 int ret;
698
699 ret = strtobool(page, &flag);
700 if (ret < 0)
701 return ret;
702
703 /*
704 * We expect this value to be non-zero when generic Block Layer
705 * Discard supported is detected iblock_create_virtdevice().
706 */
707 if (flag && !da->max_unmap_block_desc_count) {
708 pr_err("Generic Block Discard not supported\n");
709 return -ENOSYS;
710 }
711
712 da->emulate_tpws = flag;
713 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
714 da->da_dev, flag);
715 return count;
716}
717
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200718static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200719 const char *page, size_t count)
720{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200721 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200722 int old_prot = da->pi_prot_type, ret;
723 struct se_device *dev = da->da_dev;
724 u32 flag;
725
726 ret = kstrtou32(page, 0, &flag);
727 if (ret < 0)
728 return ret;
729
730 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
731 pr_err("Illegal value %d for pi_prot_type\n", flag);
732 return -EINVAL;
733 }
734 if (flag == 2) {
735 pr_err("DIF TYPE2 protection currently not supported\n");
736 return -ENOSYS;
737 }
738 if (da->hw_pi_prot_type) {
739 pr_warn("DIF protection enabled on underlying hardware,"
740 " ignoring\n");
741 return count;
742 }
743 if (!dev->transport->init_prot || !dev->transport->free_prot) {
744 /* 0 is only allowed value for non-supporting backends */
745 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700746 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200747
748 pr_err("DIF protection not supported by backend: %s\n",
749 dev->transport->name);
750 return -ENOSYS;
751 }
752 if (!(dev->dev_flags & DF_CONFIGURED)) {
753 pr_err("DIF protection requires device to be configured\n");
754 return -ENODEV;
755 }
756 if (dev->export_count) {
757 pr_err("dev[%p]: Unable to change SE Device PROT type while"
758 " export_count is %d\n", dev, dev->export_count);
759 return -EINVAL;
760 }
761
762 da->pi_prot_type = flag;
763
764 if (flag && !old_prot) {
765 ret = dev->transport->init_prot(dev);
766 if (ret) {
767 da->pi_prot_type = old_prot;
768 return ret;
769 }
770
771 } else if (!flag && old_prot) {
772 dev->transport->free_prot(dev);
773 }
774
775 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
776 return count;
777}
778
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200779static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200780 const char *page, size_t count)
781{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200782 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200783 struct se_device *dev = da->da_dev;
784 bool flag;
785 int ret;
786
787 ret = strtobool(page, &flag);
788 if (ret < 0)
789 return ret;
790
791 if (!flag)
792 return count;
793
794 if (!dev->transport->format_prot) {
795 pr_err("DIF protection format not supported by backend %s\n",
796 dev->transport->name);
797 return -ENOSYS;
798 }
799 if (!(dev->dev_flags & DF_CONFIGURED)) {
800 pr_err("DIF protection format requires device to be configured\n");
801 return -ENODEV;
802 }
803 if (dev->export_count) {
804 pr_err("dev[%p]: Unable to format SE Device PROT type while"
805 " export_count is %d\n", dev, dev->export_count);
806 return -EINVAL;
807 }
808
809 ret = dev->transport->format_prot(dev);
810 if (ret)
811 return ret;
812
813 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
814 return count;
815}
816
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200817static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200818 const char *page, size_t count)
819{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200820 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200821 bool flag;
822 int ret;
823
824 ret = strtobool(page, &flag);
825 if (ret < 0)
826 return ret;
827 if (da->da_dev->export_count) {
828 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
829 " export_count is %d\n",
830 da->da_dev, da->da_dev->export_count);
831 return -EINVAL;
832 }
833
834 da->force_pr_aptpl = flag;
835 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
836 return count;
837}
838
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200839static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200840 const char *page, size_t count)
841{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200842 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200843 bool flag;
844 int ret;
845
846 ret = strtobool(page, &flag);
847 if (ret < 0)
848 return ret;
849
850 if (flag != 0) {
851 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
852 " reordering not implemented\n", da->da_dev);
853 return -ENOSYS;
854 }
855 da->emulate_rest_reord = flag;
856 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
857 da->da_dev, flag);
858 return count;
859}
860
Jamie Pocase6f41632015-11-29 14:44:57 -0800861static ssize_t unmap_zeroes_data_store(struct config_item *item,
862 const char *page, size_t count)
863{
864 struct se_dev_attrib *da = to_attrib(item);
865 bool flag;
866 int ret;
867
868 ret = strtobool(page, &flag);
869 if (ret < 0)
870 return ret;
871
872 if (da->da_dev->export_count) {
873 pr_err("dev[%p]: Unable to change SE Device"
874 " unmap_zeroes_data while export_count is %d\n",
875 da->da_dev, da->da_dev->export_count);
876 return -EINVAL;
877 }
878 /*
879 * We expect this value to be non-zero when generic Block Layer
880 * Discard supported is detected iblock_configure_device().
881 */
882 if (flag && !da->max_unmap_block_desc_count) {
883 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
884 " because max_unmap_block_desc_count is zero\n",
885 da->da_dev);
Bart Van Assche9b3118c2016-01-05 14:44:21 +0100886 return -ENOSYS;
Jamie Pocase6f41632015-11-29 14:44:57 -0800887 }
888 da->unmap_zeroes_data = flag;
889 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
890 da->da_dev, flag);
Nicholas Bellinger2e498f22016-02-10 20:34:56 -0800891 return count;
Jamie Pocase6f41632015-11-29 14:44:57 -0800892}
893
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200894/*
895 * Note, this can only be called on unexported SE Device Object.
896 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200897static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200898 const char *page, size_t count)
899{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200900 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200901 struct se_device *dev = da->da_dev;
902 u32 val;
903 int ret;
904
905 ret = kstrtou32(page, 0, &val);
906 if (ret < 0)
907 return ret;
908
909 if (dev->export_count) {
910 pr_err("dev[%p]: Unable to change SE Device TCQ while"
911 " export_count is %d\n",
912 dev, dev->export_count);
913 return -EINVAL;
914 }
915 if (!val) {
916 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
917 return -EINVAL;
918 }
919
920 if (val > dev->dev_attrib.queue_depth) {
921 if (val > dev->dev_attrib.hw_queue_depth) {
922 pr_err("dev[%p]: Passed queue_depth:"
923 " %u exceeds TCM/SE_Device MAX"
924 " TCQ: %u\n", dev, val,
925 dev->dev_attrib.hw_queue_depth);
926 return -EINVAL;
927 }
928 }
929 da->queue_depth = dev->queue_depth = val;
930 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
931 return count;
932}
933
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200934static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200935 const char *page, size_t count)
936{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200937 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200938 u32 val;
939 int ret;
940
941 ret = kstrtou32(page, 0, &val);
942 if (ret < 0)
943 return ret;
944
945 if (da->da_dev->export_count) {
946 pr_err("dev[%p]: Unable to change SE Device"
947 " optimal_sectors while export_count is %d\n",
948 da->da_dev, da->da_dev->export_count);
949 return -EINVAL;
950 }
951 if (val > da->hw_max_sectors) {
952 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
953 " greater than hw_max_sectors: %u\n",
954 da->da_dev, val, da->hw_max_sectors);
955 return -EINVAL;
956 }
957
958 da->optimal_sectors = val;
959 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
960 da->da_dev, val);
961 return count;
962}
963
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200964static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200965 const char *page, size_t count)
966{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200967 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200968 u32 val;
969 int ret;
970
971 ret = kstrtou32(page, 0, &val);
972 if (ret < 0)
973 return ret;
974
975 if (da->da_dev->export_count) {
976 pr_err("dev[%p]: Unable to change SE Device block_size"
977 " while export_count is %d\n",
978 da->da_dev, da->da_dev->export_count);
979 return -EINVAL;
980 }
981
982 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
983 pr_err("dev[%p]: Illegal value for block_device: %u"
984 " for SE device, must be 512, 1024, 2048 or 4096\n",
985 da->da_dev, val);
986 return -EINVAL;
987 }
988
989 da->block_size = val;
990 if (da->max_bytes_per_io)
991 da->hw_max_sectors = da->max_bytes_per_io / val;
992
993 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
994 da->da_dev, val);
995 return count;
996}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200997
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200998CONFIGFS_ATTR(, emulate_model_alias);
999CONFIGFS_ATTR(, emulate_dpo);
1000CONFIGFS_ATTR(, emulate_fua_write);
1001CONFIGFS_ATTR(, emulate_fua_read);
1002CONFIGFS_ATTR(, emulate_write_cache);
1003CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1004CONFIGFS_ATTR(, emulate_tas);
1005CONFIGFS_ATTR(, emulate_tpu);
1006CONFIGFS_ATTR(, emulate_tpws);
1007CONFIGFS_ATTR(, emulate_caw);
1008CONFIGFS_ATTR(, emulate_3pc);
1009CONFIGFS_ATTR(, pi_prot_type);
1010CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1011CONFIGFS_ATTR(, pi_prot_format);
1012CONFIGFS_ATTR(, enforce_pr_isids);
1013CONFIGFS_ATTR(, is_nonrot);
1014CONFIGFS_ATTR(, emulate_rest_reord);
1015CONFIGFS_ATTR(, force_pr_aptpl);
1016CONFIGFS_ATTR_RO(, hw_block_size);
1017CONFIGFS_ATTR(, block_size);
1018CONFIGFS_ATTR_RO(, hw_max_sectors);
1019CONFIGFS_ATTR(, optimal_sectors);
1020CONFIGFS_ATTR_RO(, hw_queue_depth);
1021CONFIGFS_ATTR(, queue_depth);
1022CONFIGFS_ATTR(, max_unmap_lba_count);
1023CONFIGFS_ATTR(, max_unmap_block_desc_count);
1024CONFIGFS_ATTR(, unmap_granularity);
1025CONFIGFS_ATTR(, unmap_granularity_alignment);
Jamie Pocase6f41632015-11-29 14:44:57 -08001026CONFIGFS_ATTR(, unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001027CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001029/*
1030 * dev_attrib attributes for devices using the target core SBC/SPC
1031 * interpreter. Any backend using spc_parse_cdb should be using
1032 * these.
1033 */
1034struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001035 &attr_emulate_model_alias,
1036 &attr_emulate_dpo,
1037 &attr_emulate_fua_write,
1038 &attr_emulate_fua_read,
1039 &attr_emulate_write_cache,
1040 &attr_emulate_ua_intlck_ctrl,
1041 &attr_emulate_tas,
1042 &attr_emulate_tpu,
1043 &attr_emulate_tpws,
1044 &attr_emulate_caw,
1045 &attr_emulate_3pc,
1046 &attr_pi_prot_type,
1047 &attr_hw_pi_prot_type,
1048 &attr_pi_prot_format,
1049 &attr_enforce_pr_isids,
1050 &attr_is_nonrot,
1051 &attr_emulate_rest_reord,
1052 &attr_force_pr_aptpl,
1053 &attr_hw_block_size,
1054 &attr_block_size,
1055 &attr_hw_max_sectors,
1056 &attr_optimal_sectors,
1057 &attr_hw_queue_depth,
1058 &attr_queue_depth,
1059 &attr_max_unmap_lba_count,
1060 &attr_max_unmap_block_desc_count,
1061 &attr_unmap_granularity,
1062 &attr_unmap_granularity_alignment,
Jamie Pocase6f41632015-11-29 14:44:57 -08001063 &attr_unmap_zeroes_data,
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001064 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001065 NULL,
1066};
1067EXPORT_SYMBOL(sbc_attrib_attrs);
1068
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001069/*
1070 * Minimal dev_attrib attributes for devices passing through CDBs.
1071 * In this case we only provide a few read-only attributes for
1072 * backwards compatibility.
1073 */
1074struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001075 &attr_hw_pi_prot_type,
1076 &attr_hw_block_size,
1077 &attr_hw_max_sectors,
1078 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001079 NULL,
1080};
1081EXPORT_SYMBOL(passthrough_attrib_attrs);
1082
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001083TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001085/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001087/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001089static struct t10_wwn *to_t10_wwn(struct config_item *item)
1090{
1091 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1092}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093
1094/*
1095 * VPD page 0x80 Unit serial
1096 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001097static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1098 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001101 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102}
1103
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001104static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1105 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001107 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001108 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1110
1111 /*
1112 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1113 * from the struct scsi_device level firmware, do not allow
1114 * VPD Unit Serial to be emulated.
1115 *
1116 * Note this struct scsi_device could also be emulating VPD
1117 * information from its drivers/scsi LLD. But for now we assume
1118 * it is doing 'the right thing' wrt a world wide unique
1119 * VPD Unit Serial Number that OS dependent multipath can depend on.
1120 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001121 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001122 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123 " Unit Serial, ignoring request\n");
1124 return -EOPNOTSUPP;
1125 }
1126
Dan Carpenter60d645a2011-06-15 10:03:05 -07001127 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001128 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1130 return -EOVERFLOW;
1131 }
1132 /*
1133 * Check to see if any active $FABRIC_MOD exports exist. If they
1134 * do exist, fail here as changing this information on the fly
1135 * (underneath the initiator side OS dependent multipath code)
1136 * could cause negative effects.
1137 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001138 if (dev->export_count) {
1139 pr_err("Unable to set VPD Unit Serial while"
1140 " active %d $FABRIC_MOD exports exist\n",
1141 dev->export_count);
1142 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001144
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 /*
1146 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1147 *
1148 * Also, strip any newline added from the userspace
1149 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1150 */
1151 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1152 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001153 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001155 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156
Andy Grover6708bb22011-06-08 10:36:43 -07001157 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001158 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001159
1160 return count;
1161}
1162
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163/*
1164 * VPD page 0x83 Protocol Identifier
1165 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001166static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1167 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001169 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170 struct t10_vpd *vpd;
1171 unsigned char buf[VPD_TMP_BUF_SIZE];
1172 ssize_t len = 0;
1173
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 memset(buf, 0, VPD_TMP_BUF_SIZE);
1175
1176 spin_lock(&t10_wwn->t10_vpd_lock);
1177 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001178 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 continue;
1180
1181 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1182
Andy Grover6708bb22011-06-08 10:36:43 -07001183 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 break;
1185
1186 len += sprintf(page+len, "%s", buf);
1187 }
1188 spin_unlock(&t10_wwn->t10_vpd_lock);
1189
1190 return len;
1191}
1192
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193/*
1194 * Generic wrapper for dumping VPD identifiers by association.
1195 */
1196#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001197static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1198 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001200 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1201 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1203 ssize_t len = 0; \
1204 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205 spin_lock(&t10_wwn->t10_vpd_lock); \
1206 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1207 if (vpd->association != _assoc) \
1208 continue; \
1209 \
1210 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1211 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001212 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 break; \
1214 len += sprintf(page+len, "%s", buf); \
1215 \
1216 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1217 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001218 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001219 break; \
1220 len += sprintf(page+len, "%s", buf); \
1221 \
1222 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1223 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001224 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 break; \
1226 len += sprintf(page+len, "%s", buf); \
1227 } \
1228 spin_unlock(&t10_wwn->t10_vpd_lock); \
1229 \
1230 return len; \
1231}
1232
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001233/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001234DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001235/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001237/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1239
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001240CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1241CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1242CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1243CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1244CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245
1246static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001247 &target_wwn_attr_vpd_unit_serial,
1248 &target_wwn_attr_vpd_protocol_identifier,
1249 &target_wwn_attr_vpd_assoc_logical_unit,
1250 &target_wwn_attr_vpd_assoc_target_port,
1251 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 NULL,
1253};
1254
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001255TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001257/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001259/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001260
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001261static struct se_device *pr_to_dev(struct config_item *item)
1262{
1263 return container_of(to_config_group(item), struct se_device,
1264 dev_pr_group);
1265}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266
Christoph Hellwigd977f432012-10-10 17:37:15 -04001267static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1268 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269{
1270 struct se_node_acl *se_nacl;
1271 struct t10_pr_registration *pr_reg;
1272 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001273
1274 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1275
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001276 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001277 if (!pr_reg)
1278 return sprintf(page, "No SPC-3 Reservation holder\n");
1279
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001281 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282
Christoph Hellwigd977f432012-10-10 17:37:15 -04001283 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001284 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001285 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286}
1287
Christoph Hellwigd977f432012-10-10 17:37:15 -04001288static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1289 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290{
1291 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001292 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001295 if (se_nacl) {
1296 len = sprintf(page,
1297 "SPC-2 Reservation: %s Initiator: %s\n",
1298 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1299 se_nacl->initiatorname);
1300 } else {
1301 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001303 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304}
1305
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001306static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001308 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001309 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310
Andy Grovera3541702015-05-19 14:44:41 -07001311 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001312 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313
Christoph Hellwigd977f432012-10-10 17:37:15 -04001314 spin_lock(&dev->dev_reservation_lock);
1315 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1316 ret = target_core_dev_pr_show_spc2_res(dev, page);
1317 else
1318 ret = target_core_dev_pr_show_spc3_res(dev, page);
1319 spin_unlock(&dev->dev_reservation_lock);
1320 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321}
1322
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001323static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1324 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001326 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 ssize_t len = 0;
1328
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001330 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001332 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001333 len = sprintf(page, "SPC-3 Reservation: All Target"
1334 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001335 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 len = sprintf(page, "SPC-3 Reservation: Single"
1337 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001338 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339
Christoph Hellwigd977f432012-10-10 17:37:15 -04001340 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 return len;
1342}
1343
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001344static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1345 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001346{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001347 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348}
1349
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001351static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1352 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001354 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 struct se_portal_group *se_tpg;
1357 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001358 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359 ssize_t len = 0;
1360
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001361 spin_lock(&dev->dev_reservation_lock);
1362 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001363 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001365 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001367
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368 se_nacl = pr_reg->pr_reg_nacl;
1369 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001370 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371
1372 len += sprintf(page+len, "SPC-3 Reservation: %s"
1373 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1374 tfo->tpg_get_wwn(se_tpg));
1375 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001376 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001377 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001378 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001379 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001380
Christoph Hellwigd977f432012-10-10 17:37:15 -04001381out_unlock:
1382 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 return len;
1384}
1385
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001387static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1388 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001390 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001391 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 struct t10_pr_registration *pr_reg;
1393 unsigned char buf[384];
1394 char i_buf[PR_REG_ISID_ID_LEN];
1395 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001396 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001397
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1399
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001400 spin_lock(&dev->t10_pr.registration_lock);
1401 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 pr_reg_list) {
1403
1404 memset(buf, 0, 384);
1405 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1406 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001407 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408 PR_REG_ISID_ID_LEN);
1409 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1410 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001411 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 pr_reg->pr_res_generation);
1413
Andy Grover6708bb22011-06-08 10:36:43 -07001414 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415 break;
1416
1417 len += sprintf(page+len, "%s", buf);
1418 reg_count++;
1419 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001420 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421
Andy Grover6708bb22011-06-08 10:36:43 -07001422 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 len += sprintf(page+len, "None\n");
1424
1425 return len;
1426}
1427
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001428static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001430 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431 struct t10_pr_registration *pr_reg;
1432 ssize_t len = 0;
1433
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434 spin_lock(&dev->dev_reservation_lock);
1435 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001436 if (pr_reg) {
1437 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1438 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1439 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442
Christoph Hellwigd977f432012-10-10 17:37:15 -04001443 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444 return len;
1445}
1446
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001447static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001448{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001449 struct se_device *dev = pr_to_dev(item);
1450
Andy Grovera3541702015-05-19 14:44:41 -07001451 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001452 return sprintf(page, "SPC_PASSTHROUGH\n");
1453 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1454 return sprintf(page, "SPC2_RESERVATIONS\n");
1455 else
1456 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457}
1458
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001459static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1460 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001462 struct se_device *dev = pr_to_dev(item);
1463
Andy Grovera3541702015-05-19 14:44:41 -07001464 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 return 0;
1466
1467 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001468 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469}
1470
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001471static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1472 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001473{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001474 struct se_device *dev = pr_to_dev(item);
1475
Andy Grovera3541702015-05-19 14:44:41 -07001476 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 return 0;
1478
1479 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1480}
1481
1482enum {
1483 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1484 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1485 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1486 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1487};
1488
1489static match_table_t tokens = {
1490 {Opt_initiator_fabric, "initiator_fabric=%s"},
1491 {Opt_initiator_node, "initiator_node=%s"},
1492 {Opt_initiator_sid, "initiator_sid=%s"},
1493 {Opt_sa_res_key, "sa_res_key=%s"},
1494 {Opt_res_holder, "res_holder=%d"},
1495 {Opt_res_type, "res_type=%d"},
1496 {Opt_res_scope, "res_scope=%d"},
1497 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001498 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 {Opt_target_fabric, "target_fabric=%s"},
1500 {Opt_target_node, "target_node=%s"},
1501 {Opt_tpgt, "tpgt=%d"},
1502 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001503 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 {Opt_err, NULL}
1505};
1506
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001507static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1508 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001510 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001511 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1512 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001513 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 substring_t args[MAX_OPT_ARGS];
1515 unsigned long long tmp_ll;
1516 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001517 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001518 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001519 u16 tpgt = 0;
1520 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001521
Andy Grovera3541702015-05-19 14:44:41 -07001522 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001523 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001524 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001525 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001527 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001528 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 " active fabric exports exist\n");
1530 return -EINVAL;
1531 }
1532
1533 opts = kstrdup(page, GFP_KERNEL);
1534 if (!opts)
1535 return -ENOMEM;
1536
1537 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001538 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 if (!*ptr)
1540 continue;
1541
1542 token = match_token(ptr, tokens, args);
1543 switch (token) {
1544 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001545 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001546 if (!i_fabric) {
1547 ret = -ENOMEM;
1548 goto out;
1549 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 break;
1551 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001552 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001553 if (!i_port) {
1554 ret = -ENOMEM;
1555 goto out;
1556 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001557 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001558 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1560 PR_APTPL_MAX_IPORT_LEN);
1561 ret = -EINVAL;
1562 break;
1563 }
1564 break;
1565 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001566 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001567 if (!isid) {
1568 ret = -ENOMEM;
1569 goto out;
1570 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001571 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001572 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 "= exceeds PR_REG_ISID_LEN: %d\n",
1574 PR_REG_ISID_LEN);
1575 ret = -EINVAL;
1576 break;
1577 }
1578 break;
1579 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001580 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001582 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 goto out;
1584 }
1585 sa_res_key = (u64)tmp_ll;
1586 break;
1587 /*
1588 * PR APTPL Metadata for Reservation
1589 */
1590 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001591 ret = match_int(args, &arg);
1592 if (ret)
1593 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001594 res_holder = arg;
1595 break;
1596 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001597 ret = match_int(args, &arg);
1598 if (ret)
1599 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 type = (u8)arg;
1601 break;
1602 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001603 ret = match_int(args, &arg);
1604 if (ret)
1605 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606 break;
1607 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001608 ret = match_int(args, &arg);
1609 if (ret)
1610 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611 all_tg_pt = (int)arg;
1612 break;
1613 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001614 ret = match_int(args, &arg);
1615 if (ret)
1616 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001617 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001618 break;
1619 /*
1620 * PR APTPL Metadata for Target Port
1621 */
1622 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001623 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001624 if (!t_fabric) {
1625 ret = -ENOMEM;
1626 goto out;
1627 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 break;
1629 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001630 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001631 if (!t_port) {
1632 ret = -ENOMEM;
1633 goto out;
1634 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001635 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001636 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1638 PR_APTPL_MAX_TPORT_LEN);
1639 ret = -EINVAL;
1640 break;
1641 }
1642 break;
1643 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001644 ret = match_int(args, &arg);
1645 if (ret)
1646 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 tpgt = (u16)arg;
1648 break;
1649 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001650 ret = match_int(args, &arg);
1651 if (ret)
1652 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001653 break;
1654 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001655 ret = match_int(args, &arg);
1656 if (ret)
1657 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001658 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001659 break;
1660 default:
1661 break;
1662 }
1663 }
1664
Andy Grover6708bb22011-06-08 10:36:43 -07001665 if (!i_port || !t_port || !sa_res_key) {
1666 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001667 ret = -EINVAL;
1668 goto out;
1669 }
1670
1671 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001672 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 " holder\n", type);
1674 ret = -EINVAL;
1675 goto out;
1676 }
1677
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001678 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001679 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1680 res_holder, all_tg_pt, type);
1681out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001682 kfree(i_fabric);
1683 kfree(i_port);
1684 kfree(isid);
1685 kfree(t_fabric);
1686 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 kfree(orig);
1688 return (ret == 0) ? count : ret;
1689}
1690
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001692CONFIGFS_ATTR_RO(target_pr_, res_holder);
1693CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1694CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1695CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1696CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1697CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1698CONFIGFS_ATTR_RO(target_pr_, res_type);
1699CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1700CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701
1702static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001703 &target_pr_attr_res_holder,
1704 &target_pr_attr_res_pr_all_tgt_pts,
1705 &target_pr_attr_res_pr_generation,
1706 &target_pr_attr_res_pr_holder_tg_port,
1707 &target_pr_attr_res_pr_registered_i_pts,
1708 &target_pr_attr_res_pr_type,
1709 &target_pr_attr_res_type,
1710 &target_pr_attr_res_aptpl_active,
1711 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001712 NULL,
1713};
1714
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001715TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001716
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001717/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001718
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001719/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001721static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001723 return container_of(to_config_group(item), struct se_device, dev_group);
1724}
1725
1726static ssize_t target_dev_info_show(struct config_item *item, char *page)
1727{
1728 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 int bl = 0;
1730 ssize_t read_bytes = 0;
1731
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001732 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001734 read_bytes += dev->transport->show_configfs_dev_params(dev,
1735 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736 return read_bytes;
1737}
1738
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001739static ssize_t target_dev_control_store(struct config_item *item,
1740 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001742 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001743
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001744 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745}
1746
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001747static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001749 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001751 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752 return 0;
1753
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001754 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755}
1756
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001757static ssize_t target_dev_alias_store(struct config_item *item,
1758 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001759{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001760 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001761 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762 ssize_t read_bytes;
1763
1764 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001765 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001766 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1767 SE_DEV_ALIAS_LEN-1);
1768 return -EINVAL;
1769 }
1770
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001771 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001772 if (!read_bytes)
1773 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001774 if (dev->dev_alias[read_bytes - 1] == '\n')
1775 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001776
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001777 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001778
Andy Grover6708bb22011-06-08 10:36:43 -07001779 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001780 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001781 config_item_name(&dev->dev_group.cg_item),
1782 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783
1784 return read_bytes;
1785}
1786
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001787static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001788{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001789 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001790
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001791 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792 return 0;
1793
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001794 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001795}
1796
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001797static ssize_t target_dev_udev_path_store(struct config_item *item,
1798 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001799{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001800 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001801 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001802 ssize_t read_bytes;
1803
1804 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001805 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1807 SE_UDEV_PATH_LEN-1);
1808 return -EINVAL;
1809 }
1810
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001811 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001813 if (!read_bytes)
1814 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001815 if (dev->udev_path[read_bytes - 1] == '\n')
1816 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001817
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001818 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001819
Andy Grover6708bb22011-06-08 10:36:43 -07001820 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001822 config_item_name(&dev->dev_group.cg_item),
1823 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001824
1825 return read_bytes;
1826}
1827
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001828static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001829{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001830 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001831
1832 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1833}
1834
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001835static ssize_t target_dev_enable_store(struct config_item *item,
1836 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001838 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001840 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841
1842 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001843 if (!ptr) {
1844 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845 " is \"1\"\n");
1846 return -EINVAL;
1847 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001849 ret = target_configure_device(dev);
1850 if (ret)
1851 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001852 return count;
1853}
1854
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001855static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001857 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858 struct config_item *lu_ci;
1859 struct t10_alua_lu_gp *lu_gp;
1860 struct t10_alua_lu_gp_member *lu_gp_mem;
1861 ssize_t len = 0;
1862
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001863 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001864 if (!lu_gp_mem)
1865 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866
1867 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1868 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001869 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001870 lu_ci = &lu_gp->lu_gp_group.cg_item;
1871 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1872 config_item_name(lu_ci), lu_gp->lu_gp_id);
1873 }
1874 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1875
1876 return len;
1877}
1878
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001879static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1880 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001881{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001882 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001883 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1885 struct t10_alua_lu_gp_member *lu_gp_mem;
1886 unsigned char buf[LU_GROUP_NAME_BUF];
1887 int move = 0;
1888
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001889 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1890 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001891 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001892
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001893 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001894 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895 return -EINVAL;
1896 }
1897 memset(buf, 0, LU_GROUP_NAME_BUF);
1898 memcpy(buf, page, count);
1899 /*
1900 * Any ALUA logical unit alias besides "NULL" means we will be
1901 * making a new group association.
1902 */
1903 if (strcmp(strstrip(buf), "NULL")) {
1904 /*
1905 * core_alua_get_lu_gp_by_name() will increment reference to
1906 * struct t10_alua_lu_gp. This reference is released with
1907 * core_alua_get_lu_gp_by_name below().
1908 */
1909 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001910 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001911 return -ENODEV;
1912 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913
1914 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1915 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001916 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917 /*
1918 * Clearing an existing lu_gp association, and replacing
1919 * with NULL
1920 */
Andy Grover6708bb22011-06-08 10:36:43 -07001921 if (!lu_gp_new) {
1922 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001923 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1924 " %hu\n",
1925 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001926 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 config_item_name(&lu_gp->lu_gp_group.cg_item),
1928 lu_gp->lu_gp_id);
1929
1930 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1931 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1932
1933 return count;
1934 }
1935 /*
1936 * Removing existing association of lu_gp_mem with lu_gp
1937 */
1938 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1939 move = 1;
1940 }
1941 /*
1942 * Associate lu_gp_mem with lu_gp_new.
1943 */
1944 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1945 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1946
Andy Grover6708bb22011-06-08 10:36:43 -07001947 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 " core/alua/lu_gps/%s, ID: %hu\n",
1949 (move) ? "Moving" : "Adding",
1950 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001951 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1953 lu_gp_new->lu_gp_id);
1954
1955 core_alua_put_lu_gp_from_name(lu_gp_new);
1956 return count;
1957}
1958
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001959static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001960{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001961 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001962 struct t10_alua_lba_map *map;
1963 struct t10_alua_lba_map_member *mem;
1964 char *b = page;
1965 int bl = 0;
1966 char state;
1967
1968 spin_lock(&dev->t10_alua.lba_map_lock);
1969 if (!list_empty(&dev->t10_alua.lba_map_list))
1970 bl += sprintf(b + bl, "%u %u\n",
1971 dev->t10_alua.lba_map_segment_size,
1972 dev->t10_alua.lba_map_segment_multiplier);
1973 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1974 bl += sprintf(b + bl, "%llu %llu",
1975 map->lba_map_first_lba, map->lba_map_last_lba);
1976 list_for_each_entry(mem, &map->lba_map_mem_list,
1977 lba_map_mem_list) {
1978 switch (mem->lba_map_mem_alua_state) {
1979 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1980 state = 'O';
1981 break;
1982 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1983 state = 'A';
1984 break;
1985 case ALUA_ACCESS_STATE_STANDBY:
1986 state = 'S';
1987 break;
1988 case ALUA_ACCESS_STATE_UNAVAILABLE:
1989 state = 'U';
1990 break;
1991 default:
1992 state = '.';
1993 break;
1994 }
1995 bl += sprintf(b + bl, " %d:%c",
1996 mem->lba_map_mem_alua_pg_id, state);
1997 }
1998 bl += sprintf(b + bl, "\n");
1999 }
2000 spin_unlock(&dev->t10_alua.lba_map_lock);
2001 return bl;
2002}
2003
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002004static ssize_t target_dev_lba_map_store(struct config_item *item,
2005 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002006{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002007 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002008 struct t10_alua_lba_map *lba_map = NULL;
2009 struct list_head lba_list;
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002010 char *map_entries, *orig, *ptr;
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002011 char state;
2012 int pg_num = -1, pg;
2013 int ret = 0, num = 0, pg_id, alua_state;
2014 unsigned long start_lba = -1, end_lba = -1;
2015 unsigned long segment_size = -1, segment_mult = -1;
2016
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002017 orig = map_entries = kstrdup(page, GFP_KERNEL);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002018 if (!map_entries)
2019 return -ENOMEM;
2020
2021 INIT_LIST_HEAD(&lba_list);
2022 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2023 if (!*ptr)
2024 continue;
2025
2026 if (num == 0) {
2027 if (sscanf(ptr, "%lu %lu\n",
2028 &segment_size, &segment_mult) != 2) {
2029 pr_err("Invalid line %d\n", num);
2030 ret = -EINVAL;
2031 break;
2032 }
2033 num++;
2034 continue;
2035 }
2036 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2037 pr_err("Invalid line %d\n", num);
2038 ret = -EINVAL;
2039 break;
2040 }
2041 ptr = strchr(ptr, ' ');
2042 if (!ptr) {
2043 pr_err("Invalid line %d, missing end lba\n", num);
2044 ret = -EINVAL;
2045 break;
2046 }
2047 ptr++;
2048 ptr = strchr(ptr, ' ');
2049 if (!ptr) {
2050 pr_err("Invalid line %d, missing state definitions\n",
2051 num);
2052 ret = -EINVAL;
2053 break;
2054 }
2055 ptr++;
2056 lba_map = core_alua_allocate_lba_map(&lba_list,
2057 start_lba, end_lba);
2058 if (IS_ERR(lba_map)) {
2059 ret = PTR_ERR(lba_map);
2060 break;
2061 }
2062 pg = 0;
2063 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2064 switch (state) {
2065 case 'O':
2066 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2067 break;
2068 case 'A':
2069 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2070 break;
2071 case 'S':
2072 alua_state = ALUA_ACCESS_STATE_STANDBY;
2073 break;
2074 case 'U':
2075 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2076 break;
2077 default:
2078 pr_err("Invalid ALUA state '%c'\n", state);
2079 ret = -EINVAL;
2080 goto out;
2081 }
2082
2083 ret = core_alua_allocate_lba_map_mem(lba_map,
2084 pg_id, alua_state);
2085 if (ret) {
2086 pr_err("Invalid target descriptor %d:%c "
2087 "at line %d\n",
2088 pg_id, state, num);
2089 break;
2090 }
2091 pg++;
2092 ptr = strchr(ptr, ' ');
2093 if (ptr)
2094 ptr++;
2095 else
2096 break;
2097 }
2098 if (pg_num == -1)
2099 pg_num = pg;
2100 else if (pg != pg_num) {
2101 pr_err("Only %d from %d port groups definitions "
2102 "at line %d\n", pg, pg_num, num);
2103 ret = -EINVAL;
2104 break;
2105 }
2106 num++;
2107 }
2108out:
2109 if (ret) {
2110 core_alua_free_lba_map(&lba_list);
2111 count = ret;
2112 } else
2113 core_alua_set_lba_map(dev, &lba_list,
2114 segment_size, segment_mult);
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002115 kfree(orig);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002116 return count;
2117}
2118
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002119CONFIGFS_ATTR_RO(target_dev_, info);
2120CONFIGFS_ATTR_WO(target_dev_, control);
2121CONFIGFS_ATTR(target_dev_, alias);
2122CONFIGFS_ATTR(target_dev_, udev_path);
2123CONFIGFS_ATTR(target_dev_, enable);
2124CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2125CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002126
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002127static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002128 &target_dev_attr_info,
2129 &target_dev_attr_control,
2130 &target_dev_attr_alias,
2131 &target_dev_attr_udev_path,
2132 &target_dev_attr_enable,
2133 &target_dev_attr_alua_lu_gp,
2134 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002135 NULL,
2136};
2137
2138static void target_core_dev_release(struct config_item *item)
2139{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002140 struct config_group *dev_cg = to_config_group(item);
2141 struct se_device *dev =
2142 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002144 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002145}
2146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002147static struct configfs_item_operations target_core_dev_item_ops = {
2148 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002149};
2150
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002151TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002153/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154
2155/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2156
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002157static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002158{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002159 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2160 lu_gp_group);
2161}
2162
2163static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2164{
2165 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2166
Andy Grover6708bb22011-06-08 10:36:43 -07002167 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002169 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2170}
2171
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002172static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2173 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002175 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002176 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2177 unsigned long lu_gp_id;
2178 int ret;
2179
Jingoo Han57103d72013-07-19 16:22:19 +09002180 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002182 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002184 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002185 }
2186 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002187 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002188 " 0x0000ffff\n", lu_gp_id);
2189 return -EINVAL;
2190 }
2191
2192 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2193 if (ret < 0)
2194 return -EINVAL;
2195
Andy Grover6708bb22011-06-08 10:36:43 -07002196 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2198 config_item_name(&alua_lu_gp_cg->cg_item),
2199 lu_gp->lu_gp_id);
2200
2201 return count;
2202}
2203
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002204static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002205{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002206 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 struct se_device *dev;
2208 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209 struct t10_alua_lu_gp_member *lu_gp_mem;
2210 ssize_t len = 0, cur_len;
2211 unsigned char buf[LU_GROUP_NAME_BUF];
2212
2213 memset(buf, 0, LU_GROUP_NAME_BUF);
2214
2215 spin_lock(&lu_gp->lu_gp_lock);
2216 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2217 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002218 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219
2220 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2221 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002222 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223 cur_len++; /* Extra byte for NULL terminator */
2224
2225 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002226 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 "_members buffer\n");
2228 break;
2229 }
2230 memcpy(page+len, buf, cur_len);
2231 len += cur_len;
2232 }
2233 spin_unlock(&lu_gp->lu_gp_lock);
2234
2235 return len;
2236}
2237
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002238CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2239CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240
2241static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002242 &target_lu_gp_attr_lu_gp_id,
2243 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244 NULL,
2245};
2246
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002247static void target_core_alua_lu_gp_release(struct config_item *item)
2248{
2249 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2250 struct t10_alua_lu_gp, lu_gp_group);
2251
2252 core_alua_free_lu_gp(lu_gp);
2253}
2254
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002256 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002257};
2258
2259static struct config_item_type target_core_alua_lu_gp_cit = {
2260 .ct_item_ops = &target_core_alua_lu_gp_ops,
2261 .ct_attrs = target_core_alua_lu_gp_attrs,
2262 .ct_owner = THIS_MODULE,
2263};
2264
2265/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2266
2267/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2268
2269static struct config_group *target_core_alua_create_lu_gp(
2270 struct config_group *group,
2271 const char *name)
2272{
2273 struct t10_alua_lu_gp *lu_gp;
2274 struct config_group *alua_lu_gp_cg = NULL;
2275 struct config_item *alua_lu_gp_ci = NULL;
2276
2277 lu_gp = core_alua_allocate_lu_gp(name, 0);
2278 if (IS_ERR(lu_gp))
2279 return NULL;
2280
2281 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2282 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2283
2284 config_group_init_type_name(alua_lu_gp_cg, name,
2285 &target_core_alua_lu_gp_cit);
2286
Andy Grover6708bb22011-06-08 10:36:43 -07002287 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002288 " Group: core/alua/lu_gps/%s\n",
2289 config_item_name(alua_lu_gp_ci));
2290
2291 return alua_lu_gp_cg;
2292
2293}
2294
2295static void target_core_alua_drop_lu_gp(
2296 struct config_group *group,
2297 struct config_item *item)
2298{
2299 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2300 struct t10_alua_lu_gp, lu_gp_group);
2301
Andy Grover6708bb22011-06-08 10:36:43 -07002302 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002303 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2304 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002305 /*
2306 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2307 * -> target_core_alua_lu_gp_release()
2308 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002310}
2311
2312static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2313 .make_group = &target_core_alua_create_lu_gp,
2314 .drop_item = &target_core_alua_drop_lu_gp,
2315};
2316
2317static struct config_item_type target_core_alua_lu_gps_cit = {
2318 .ct_item_ops = NULL,
2319 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2320 .ct_owner = THIS_MODULE,
2321};
2322
2323/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2324
2325/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2326
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002327static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002329 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2330 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002331}
2332
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002333static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2334 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002336 return sprintf(page, "%d\n",
2337 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2338}
2339
2340static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2341 const char *page, size_t count)
2342{
2343 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002344 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002345 unsigned long tmp;
2346 int new_state, ret;
2347
Andy Grover6708bb22011-06-08 10:36:43 -07002348 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002349 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002350 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2351 return -EINVAL;
2352 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002353 if (!(dev->dev_flags & DF_CONFIGURED)) {
2354 pr_err("Unable to set alua_access_state while device is"
2355 " not configured\n");
2356 return -ENODEV;
2357 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002358
Jingoo Han57103d72013-07-19 16:22:19 +09002359 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002361 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002362 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002363 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002364 }
2365 new_state = (int)tmp;
2366
Hannes Reinecke125d0112013-11-19 09:07:46 +01002367 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2368 pr_err("Unable to process implicit configfs ALUA"
2369 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002370 return -EINVAL;
2371 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002372 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2373 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2374 /* LBA DEPENDENT is only allowed with implicit ALUA */
2375 pr_err("Unable to process implicit configfs ALUA transition"
2376 " while explicit ALUA management is enabled\n");
2377 return -EINVAL;
2378 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002380 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381 NULL, NULL, new_state, 0);
2382 return (!ret) ? count : -EINVAL;
2383}
2384
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002385static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2386 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002387{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002388 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389 return sprintf(page, "%s\n",
2390 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2391}
2392
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002393static ssize_t target_tg_pt_gp_alua_access_status_store(
2394 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002395{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002396 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 unsigned long tmp;
2398 int new_status, ret;
2399
Andy Grover6708bb22011-06-08 10:36:43 -07002400 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2401 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002402 " valid tg_pt_gp ID: %hu\n",
2403 tg_pt_gp->tg_pt_gp_valid_id);
2404 return -EINVAL;
2405 }
2406
Jingoo Han57103d72013-07-19 16:22:19 +09002407 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002409 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002410 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002411 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412 }
2413 new_status = (int)tmp;
2414
2415 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002416 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2417 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002418 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 new_status);
2420 return -EINVAL;
2421 }
2422
2423 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2424 return count;
2425}
2426
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002427static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2428 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002430 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431}
2432
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002433static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2434 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002436 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002437}
2438
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002439#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2440static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2441 struct config_item *item, char *p) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002442{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002443 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2444 return sprintf(p, "%d\n", \
2445 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2446} \
2447 \
2448static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2449 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002450{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002451 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002452 unsigned long tmp; \
2453 int ret; \
2454 \
2455 if (!t->tg_pt_gp_valid_id) { \
2456 pr_err("Unable to do set ##_name ALUA state on non" \
2457 " valid tg_pt_gp ID: %hu\n", \
2458 t->tg_pt_gp_valid_id); \
2459 return -EINVAL; \
2460 } \
2461 \
2462 ret = kstrtoul(p, 0, &tmp); \
2463 if (ret < 0) { \
2464 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2465 return -EINVAL; \
2466 } \
2467 if (tmp > 1) { \
2468 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2469 return -EINVAL; \
2470 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002471 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002472 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002473 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002474 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002475 \
2476 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002477}
2478
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002479ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2480ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2481ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2482ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2483ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2484ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2485ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002486
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002487static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2488 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002490 return sprintf(page, "%d\n",
2491 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002492}
2493
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002494static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2495 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002496{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002497 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002498 unsigned long tmp;
2499 int ret;
2500
Jingoo Han57103d72013-07-19 16:22:19 +09002501 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002502 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002503 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002504 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002505 }
2506
2507 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002508 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002509 " %lu\n", tmp);
2510 return -EINVAL;
2511 }
2512 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2513
2514 return count;
2515}
2516
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002517static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2518 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002519{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002520 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002521}
2522
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002523static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2524 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002525{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002526 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2527 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528}
2529
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002530static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2531 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002533 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534}
2535
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002536static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2537 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002539 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2540 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002541}
2542
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002543static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2544 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002545{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002546 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002547}
2548
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002549static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2550 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002551{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002552 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2553 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002554}
2555
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002556static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2557 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002559 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560}
2561
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002562static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2563 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002565 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566}
2567
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002568static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2569 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002570{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002571 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2572
Andy Grover6708bb22011-06-08 10:36:43 -07002573 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2576}
2577
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002578static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2579 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002580{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002581 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002582 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2583 unsigned long tg_pt_gp_id;
2584 int ret;
2585
Jingoo Han57103d72013-07-19 16:22:19 +09002586 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002588 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002590 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591 }
2592 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002593 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 " 0x0000ffff\n", tg_pt_gp_id);
2595 return -EINVAL;
2596 }
2597
2598 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2599 if (ret < 0)
2600 return -EINVAL;
2601
Andy Grover6708bb22011-06-08 10:36:43 -07002602 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2604 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2605 tg_pt_gp->tg_pt_gp_id);
2606
2607 return count;
2608}
2609
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002610static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2611 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002613 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002614 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615 ssize_t len = 0, cur_len;
2616 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2617
2618 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2619
2620 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002621 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2622 lun_tg_pt_gp_link) {
2623 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624
2625 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002626 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2627 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2628 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629 config_item_name(&lun->lun_group.cg_item));
2630 cur_len++; /* Extra byte for NULL terminator */
2631
2632 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002633 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634 "_members buffer\n");
2635 break;
2636 }
2637 memcpy(page+len, buf, cur_len);
2638 len += cur_len;
2639 }
2640 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2641
2642 return len;
2643}
2644
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002645CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2646CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2647CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2648CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2649CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2650CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2651CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2652CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2653CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2654CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2655CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2656CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2657CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2658CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2659CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2660CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2661CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002662
2663static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002664 &target_tg_pt_gp_attr_alua_access_state,
2665 &target_tg_pt_gp_attr_alua_access_status,
2666 &target_tg_pt_gp_attr_alua_access_type,
2667 &target_tg_pt_gp_attr_alua_support_transitioning,
2668 &target_tg_pt_gp_attr_alua_support_offline,
2669 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2670 &target_tg_pt_gp_attr_alua_support_unavailable,
2671 &target_tg_pt_gp_attr_alua_support_standby,
2672 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2673 &target_tg_pt_gp_attr_alua_support_active_optimized,
2674 &target_tg_pt_gp_attr_alua_write_metadata,
2675 &target_tg_pt_gp_attr_nonop_delay_msecs,
2676 &target_tg_pt_gp_attr_trans_delay_msecs,
2677 &target_tg_pt_gp_attr_implicit_trans_secs,
2678 &target_tg_pt_gp_attr_preferred,
2679 &target_tg_pt_gp_attr_tg_pt_gp_id,
2680 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 NULL,
2682};
2683
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002684static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2685{
2686 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2687 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2688
2689 core_alua_free_tg_pt_gp(tg_pt_gp);
2690}
2691
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002693 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694};
2695
2696static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2697 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2698 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2699 .ct_owner = THIS_MODULE,
2700};
2701
2702/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2703
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002704/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002705
2706static struct config_group *target_core_alua_create_tg_pt_gp(
2707 struct config_group *group,
2708 const char *name)
2709{
2710 struct t10_alua *alua = container_of(group, struct t10_alua,
2711 alua_tg_pt_gps_group);
2712 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002713 struct config_group *alua_tg_pt_gp_cg = NULL;
2714 struct config_item *alua_tg_pt_gp_ci = NULL;
2715
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002716 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002717 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 return NULL;
2719
2720 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2721 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2722
2723 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2724 &target_core_alua_tg_pt_gp_cit);
2725
Andy Grover6708bb22011-06-08 10:36:43 -07002726 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002727 " Group: alua/tg_pt_gps/%s\n",
2728 config_item_name(alua_tg_pt_gp_ci));
2729
2730 return alua_tg_pt_gp_cg;
2731}
2732
2733static void target_core_alua_drop_tg_pt_gp(
2734 struct config_group *group,
2735 struct config_item *item)
2736{
2737 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2738 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2739
Andy Grover6708bb22011-06-08 10:36:43 -07002740 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002741 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2742 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002743 /*
2744 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2745 * -> target_core_alua_tg_pt_gp_release().
2746 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748}
2749
2750static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2751 .make_group = &target_core_alua_create_tg_pt_gp,
2752 .drop_item = &target_core_alua_drop_tg_pt_gp,
2753};
2754
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002755TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002757/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758
2759/* Start functions for struct config_item_type target_core_alua_cit */
2760
2761/*
2762 * target_core_alua_cit is a ConfigFS group that lives under
2763 * /sys/kernel/config/target/core/alua. There are default groups
2764 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2765 * target_core_alua_cit in target_core_init_configfs() below.
2766 */
2767static struct config_item_type target_core_alua_cit = {
2768 .ct_item_ops = NULL,
2769 .ct_attrs = NULL,
2770 .ct_owner = THIS_MODULE,
2771};
2772
2773/* End functions for struct config_item_type target_core_alua_cit */
2774
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002775/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002776
2777static struct config_group *target_core_stat_mkdir(
2778 struct config_group *group,
2779 const char *name)
2780{
2781 return ERR_PTR(-ENOSYS);
2782}
2783
2784static void target_core_stat_rmdir(
2785 struct config_group *group,
2786 struct config_item *item)
2787{
2788 return;
2789}
2790
2791static struct configfs_group_operations target_core_stat_group_ops = {
2792 .make_group = &target_core_stat_mkdir,
2793 .drop_item = &target_core_stat_rmdir,
2794};
2795
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002796TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002797
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002798/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002799
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002800/* Start functions for struct config_item_type target_core_hba_cit */
2801
2802static struct config_group *target_core_make_subdev(
2803 struct config_group *group,
2804 const char *name)
2805{
2806 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002807 struct config_item *hba_ci = &group->cg_item;
2808 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002809 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002810 struct se_device *dev;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002811 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002812
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002813 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2814 if (ret)
2815 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002816
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002817 dev = target_alloc_device(hba, name);
2818 if (!dev)
2819 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002821 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002823 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002824 &tb->tb_dev_attrib_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002825 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
2826
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002827 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002828 &tb->tb_dev_pr_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002829 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
2830
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002831 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002832 &tb->tb_dev_wwn_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002833 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
2834 &dev->dev_group);
2835
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002836 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002837 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002838 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
2839 &dev->dev_group);
2840
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002841 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002842 "statistics", &tb->tb_dev_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002843 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
2844 &dev->dev_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002845
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002847 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002849 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002850 if (!tg_pt_gp)
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002851 goto out_free_device;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002852 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2855 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002856 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
2857 &dev->t10_alua.alua_tg_pt_gps_group);
2858
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002859 /*
2860 * Add core/$HBA/$DEV/statistics/ default groups
2861 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002862 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863
2864 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002865 return &dev->dev_group;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002866
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002867out_free_device:
2868 target_free_device(dev);
2869out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002871 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872}
2873
2874static void target_core_drop_subdev(
2875 struct config_group *group,
2876 struct config_item *item)
2877{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002878 struct config_group *dev_cg = to_config_group(item);
2879 struct se_device *dev =
2880 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002883 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002885 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002887 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
2888 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002889
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002890 /*
2891 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2892 * directly from target_core_alua_tg_pt_gp_release().
2893 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002894 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002895
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002896 configfs_remove_default_groups(dev_cg);
2897
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002898 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002899 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002901 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903}
2904
2905static struct configfs_group_operations target_core_hba_group_ops = {
2906 .make_group = target_core_make_subdev,
2907 .drop_item = target_core_drop_subdev,
2908};
2909
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002910
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002911static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002913 return container_of(to_config_group(item), struct se_hba, hba_group);
2914}
2915
2916static ssize_t target_hba_info_show(struct config_item *item, char *page)
2917{
2918 struct se_hba *hba = to_hba(item);
2919
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002921 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002922 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923}
2924
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002925static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002927 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002928 int hba_mode = 0;
2929
2930 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2931 hba_mode = 1;
2932
2933 return sprintf(page, "%d\n", hba_mode);
2934}
2935
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002936static ssize_t target_hba_mode_store(struct config_item *item,
2937 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002939 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002940 unsigned long mode_flag;
2941 int ret;
2942
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002943 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944 return -EINVAL;
2945
Jingoo Han57103d72013-07-19 16:22:19 +09002946 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002947 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002948 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002949 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002950 }
2951
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002952 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002953 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 return -EINVAL;
2955 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002956
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002957 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958 if (ret < 0)
2959 return -EINVAL;
2960 if (ret > 0)
2961 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2962 else if (ret == 0)
2963 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2964
2965 return count;
2966}
2967
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002968CONFIGFS_ATTR_RO(target_, hba_info);
2969CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002971static void target_core_hba_release(struct config_item *item)
2972{
2973 struct se_hba *hba = container_of(to_config_group(item),
2974 struct se_hba, hba_group);
2975 core_delete_hba(hba);
2976}
2977
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002979 &target_attr_hba_info,
2980 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 NULL,
2982};
2983
2984static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002985 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986};
2987
2988static struct config_item_type target_core_hba_cit = {
2989 .ct_item_ops = &target_core_hba_item_ops,
2990 .ct_group_ops = &target_core_hba_group_ops,
2991 .ct_attrs = target_core_hba_attrs,
2992 .ct_owner = THIS_MODULE,
2993};
2994
2995static struct config_group *target_core_call_addhbatotarget(
2996 struct config_group *group,
2997 const char *name)
2998{
2999 char *se_plugin_str, *str, *str2;
3000 struct se_hba *hba;
3001 char buf[TARGET_CORE_NAME_MAX_LEN];
3002 unsigned long plugin_dep_id = 0;
3003 int ret;
3004
3005 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003006 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003007 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3009 TARGET_CORE_NAME_MAX_LEN);
3010 return ERR_PTR(-ENAMETOOLONG);
3011 }
3012 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3013
3014 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003015 if (!str) {
3016 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003017 return ERR_PTR(-EINVAL);
3018 }
3019 se_plugin_str = buf;
3020 /*
3021 * Special case for subsystem plugins that have "_" in their names.
3022 * Namely rd_direct and rd_mcp..
3023 */
3024 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003025 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003026 *str2 = '\0'; /* Terminate for *se_plugin_str */
3027 str2++; /* Skip to start of plugin dependent ID */
3028 str = str2;
3029 } else {
3030 *str = '\0'; /* Terminate for *se_plugin_str */
3031 str++; /* Skip to start of plugin dependent ID */
3032 }
3033
Jingoo Han57103d72013-07-19 16:22:19 +09003034 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003036 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003037 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003038 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 }
3040 /*
3041 * Load up TCM subsystem plugins if they have not already been loaded.
3042 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003043 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003044
3045 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3046 if (IS_ERR(hba))
3047 return ERR_CAST(hba);
3048
3049 config_group_init_type_name(&hba->hba_group, name,
3050 &target_core_hba_cit);
3051
3052 return &hba->hba_group;
3053}
3054
3055static void target_core_call_delhbafromtarget(
3056 struct config_group *group,
3057 struct config_item *item)
3058{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003059 /*
3060 * core_delete_hba() is called from target_core_hba_item_ops->release()
3061 * -> target_core_hba_release()
3062 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003064}
3065
3066static struct configfs_group_operations target_core_group_ops = {
3067 .make_group = target_core_call_addhbatotarget,
3068 .drop_item = target_core_call_delhbafromtarget,
3069};
3070
3071static struct config_item_type target_core_cit = {
3072 .ct_item_ops = NULL,
3073 .ct_group_ops = &target_core_group_ops,
3074 .ct_attrs = NULL,
3075 .ct_owner = THIS_MODULE,
3076};
3077
3078/* Stop functions for struct config_item_type target_core_hba_cit */
3079
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003080void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003081{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003082 target_core_setup_dev_cit(tb);
3083 target_core_setup_dev_attrib_cit(tb);
3084 target_core_setup_dev_pr_cit(tb);
3085 target_core_setup_dev_wwn_cit(tb);
3086 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3087 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003088}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003089
Axel Lin54550fa2011-03-14 04:06:09 -07003090static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003092 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 struct t10_alua_lu_gp *lu_gp;
3094 int ret;
3095
Andy Grover6708bb22011-06-08 10:36:43 -07003096 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3098 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3099
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 config_group_init(&subsys->su_group);
3101 mutex_init(&subsys->su_mutex);
3102
Andy Grovere3d6f902011-07-19 08:55:10 +00003103 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003105 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106 /*
3107 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3108 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3109 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003110 config_group_init_type_name(&target_core_hbagroup, "core",
3111 &target_core_cit);
3112 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003114 /*
3115 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3116 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003117 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3118 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3119
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 /*
3121 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3122 * groups under /sys/kernel/config/target/core/alua/
3123 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003124 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3125 &target_core_alua_lu_gps_cit);
3126 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003127
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 /*
3129 * Add core/alua/lu_gps/default_lu_gp
3130 */
3131 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003132 if (IS_ERR(lu_gp)) {
3133 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003134 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003135 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003136
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003137 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3138 &target_core_alua_lu_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003139 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3140
Andy Grovere3d6f902011-07-19 08:55:10 +00003141 default_lu_gp = lu_gp;
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003142
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003143 /*
3144 * Register the target_core_mod subsystem with configfs.
3145 */
3146 ret = configfs_register_subsystem(subsys);
3147 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003148 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 ret, subsys->su_group.cg_item.ci_namebuf);
3150 goto out_global;
3151 }
Andy Grover6708bb22011-06-08 10:36:43 -07003152 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003153 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3155 /*
3156 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3157 */
3158 ret = rd_module_init();
3159 if (ret < 0)
3160 goto out;
3161
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003162 ret = core_dev_setup_virtual_lun0();
3163 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 goto out;
3165
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003166 ret = target_xcopy_setup_pt();
3167 if (ret < 0)
3168 goto out;
3169
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 return 0;
3171
3172out:
3173 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 core_dev_release_virtual_lun0();
3175 rd_module_exit();
3176out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003177 if (default_lu_gp) {
3178 core_alua_free_lu_gp(default_lu_gp);
3179 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003181 release_se_kmem_caches();
3182 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183}
3184
Axel Lin54550fa2011-03-14 04:06:09 -07003185static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003186{
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003187 configfs_remove_default_groups(&alua_lu_gps_group);
3188 configfs_remove_default_groups(&alua_group);
3189 configfs_remove_default_groups(&target_core_hbagroup);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003191 /*
3192 * We expect subsys->su_group.default_groups to be released
3193 * by configfs subsystem provider logic..
3194 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003195 configfs_unregister_subsystem(&target_core_fabrics);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196
Andy Grovere3d6f902011-07-19 08:55:10 +00003197 core_alua_free_lu_gp(default_lu_gp);
3198 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003199
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " Infrastructure\n");
3202
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203 core_dev_release_virtual_lun0();
3204 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003205 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003206 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207}
3208
3209MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3210MODULE_AUTHOR("nab@Linux-iSCSI.org");
3211MODULE_LICENSE("GPL");
3212
3213module_init(target_core_init_configfs);
3214module_exit(target_core_exit_configfs);