blob: 2001005bef45845dc45fdbf1e4bebf8c82d84fc5 [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
Lee Duncana96e9782016-04-14 18:18:50 -0700102char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
103static char db_root_stage[DB_ROOT_LEN];
104
105static ssize_t target_core_item_dbroot_show(struct config_item *item,
106 char *page)
107{
108 return sprintf(page, "%s\n", db_root);
109}
110
111static ssize_t target_core_item_dbroot_store(struct config_item *item,
112 const char *page, size_t count)
113{
114 ssize_t read_bytes;
115 struct file *fp;
116
117 mutex_lock(&g_tf_lock);
118 if (!list_empty(&g_tf_list)) {
119 mutex_unlock(&g_tf_lock);
120 pr_err("db_root: cannot be changed: target drivers registered");
121 return -EINVAL;
122 }
123
124 if (count > (DB_ROOT_LEN - 1)) {
125 mutex_unlock(&g_tf_lock);
126 pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
127 (int)count, DB_ROOT_LEN - 1);
128 return -EINVAL;
129 }
130
131 read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
132 if (!read_bytes) {
133 mutex_unlock(&g_tf_lock);
134 return -EINVAL;
135 }
136 if (db_root_stage[read_bytes - 1] == '\n')
137 db_root_stage[read_bytes - 1] = '\0';
138
139 /* validate new db root before accepting it */
140 fp = filp_open(db_root_stage, O_RDONLY, 0);
141 if (IS_ERR(fp)) {
142 mutex_unlock(&g_tf_lock);
143 pr_err("db_root: cannot open: %s\n", db_root_stage);
144 return -EINVAL;
145 }
146 if (!S_ISDIR(fp->f_inode->i_mode)) {
147 filp_close(fp, 0);
148 mutex_unlock(&g_tf_lock);
149 pr_err("db_root: not a directory: %s\n", db_root_stage);
150 return -EINVAL;
151 }
152 filp_close(fp, 0);
153
154 strncpy(db_root, db_root_stage, read_bytes);
155
156 mutex_unlock(&g_tf_lock);
157
158 return read_bytes;
159}
160
161CONFIGFS_ATTR(target_core_item_, dbroot);
162
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163static struct target_fabric_configfs *target_core_get_fabric(
164 const char *name)
165{
166 struct target_fabric_configfs *tf;
167
Andy Grover6708bb22011-06-08 10:36:43 -0700168 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 return NULL;
170
171 mutex_lock(&g_tf_lock);
172 list_for_each_entry(tf, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200173 if (!strcmp(tf->tf_ops->name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174 atomic_inc(&tf->tf_access_cnt);
175 mutex_unlock(&g_tf_lock);
176 return tf;
177 }
178 }
179 mutex_unlock(&g_tf_lock);
180
181 return NULL;
182}
183
184/*
185 * Called from struct target_core_group_ops->make_group()
186 */
187static struct config_group *target_core_register_fabric(
188 struct config_group *group,
189 const char *name)
190{
191 struct target_fabric_configfs *tf;
192 int ret;
193
Andy Grover6708bb22011-06-08 10:36:43 -0700194 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800195 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196
197 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700198 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700199 pr_debug("target_core_register_fabric() trying autoload for %s\n",
200 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800201
202 /*
203 * Below are some hardcoded request_module() calls to automatically
204 * local fabric modules when the following is called:
205 *
206 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
207 *
208 * Note that this does not limit which TCM fabric module can be
209 * registered, but simply provids auto loading logic for modules with
210 * mkdir(2) system calls with known TCM fabric modules.
211 */
212
213 if (!strncmp(name, "iscsi", 5)) {
214 /*
215 * Automatically load the LIO Target fabric module when the
216 * following is called:
217 *
218 * mkdir -p $CONFIGFS/target/iscsi
219 */
220 ret = request_module("iscsi_target_mod");
221 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700222 pr_debug("request_module() failed for"
223 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800224 return ERR_PTR(-EINVAL);
225 }
226 } else if (!strncmp(name, "loopback", 8)) {
227 /*
228 * Automatically load the tcm_loop fabric module when the
229 * following is called:
230 *
231 * mkdir -p $CONFIGFS/target/loopback
232 */
233 ret = request_module("tcm_loop");
234 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700235 pr_debug("request_module() failed for"
236 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800237 return ERR_PTR(-EINVAL);
238 }
239 }
240
241 tf = target_core_get_fabric(name);
242 }
243
244 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700245 pr_debug("target_core_get_fabric() failed for %s\n",
246 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247 return ERR_PTR(-EINVAL);
248 }
Andy Grover6708bb22011-06-08 10:36:43 -0700249 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200250 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800251 /*
252 * On a successful target_core_get_fabric() look, the returned
253 * struct target_fabric_configfs *tf will contain a usage reference.
254 */
Andy Grover6708bb22011-06-08 10:36:43 -0700255 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200256 &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800257
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200258 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100259
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800260 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200261 &tf->tf_discovery_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100262 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263
Andy Grover6708bb22011-06-08 10:36:43 -0700264 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800265 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266 return &tf->tf_group;
267}
268
269/*
270 * Called from struct target_core_group_ops->drop_item()
271 */
272static void target_core_deregister_fabric(
273 struct config_group *group,
274 struct config_item *item)
275{
276 struct target_fabric_configfs *tf = container_of(
277 to_config_group(item), struct target_fabric_configfs, tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278
Andy Grover6708bb22011-06-08 10:36:43 -0700279 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 " tf list\n", config_item_name(item));
281
Andy Grover6708bb22011-06-08 10:36:43 -0700282 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200283 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 atomic_dec(&tf->tf_access_cnt);
285
Andy Grover6708bb22011-06-08 10:36:43 -0700286 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 " %s\n", config_item_name(item));
288
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100289 configfs_remove_default_groups(&tf->tf_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 config_item_put(item);
291}
292
293static struct configfs_group_operations target_core_fabric_group_ops = {
294 .make_group = &target_core_register_fabric,
295 .drop_item = &target_core_deregister_fabric,
296};
297
298/*
299 * All item attributes appearing in /sys/kernel/target/ appear here.
300 */
301static struct configfs_attribute *target_core_fabric_item_attrs[] = {
302 &target_core_item_attr_version,
Lee Duncana96e9782016-04-14 18:18:50 -0700303 &target_core_item_attr_dbroot,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 NULL,
305};
306
307/*
308 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
309 */
310static struct config_item_type target_core_fabrics_item = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311 .ct_group_ops = &target_core_fabric_group_ops,
312 .ct_attrs = target_core_fabric_item_attrs,
313 .ct_owner = THIS_MODULE,
314};
315
316static struct configfs_subsystem target_core_fabrics = {
317 .su_group = {
318 .cg_item = {
319 .ci_namebuf = "target",
320 .ci_type = &target_core_fabrics_item,
321 },
322 },
323};
324
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200325int target_depend_item(struct config_item *item)
326{
327 return configfs_depend_item(&target_core_fabrics, item);
328}
329EXPORT_SYMBOL(target_depend_item);
330
331void target_undepend_item(struct config_item *item)
332{
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100333 return configfs_undepend_item(item);
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200334}
335EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336
337/*##############################################################################
338// Start functions called by external Target Fabrics Modules
339//############################################################################*/
340
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200341static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800342{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200343 if (!tfo->name) {
344 pr_err("Missing tfo->name\n");
345 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200347 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700348 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200349 "_NAME_SIZE\n", tfo->name);
350 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800351 }
Andy Grover6708bb22011-06-08 10:36:43 -0700352 if (!tfo->get_fabric_name) {
353 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 return -EINVAL;
355 }
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->tpg_get_wwn) {
357 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->tpg_get_tag) {
361 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->tpg_check_demo_mode) {
365 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->tpg_check_demo_mode_cache) {
369 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->tpg_check_demo_mode_write_protect) {
373 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Andy Grover6708bb22011-06-08 10:36:43 -0700376 if (!tfo->tpg_check_prod_mode_write_protect) {
377 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 return -EINVAL;
379 }
Andy Grover6708bb22011-06-08 10:36:43 -0700380 if (!tfo->tpg_get_inst_index) {
381 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 return -EINVAL;
383 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400384 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700385 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 return -EINVAL;
387 }
Andy Grover6708bb22011-06-08 10:36:43 -0700388 if (!tfo->sess_get_index) {
389 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 return -EINVAL;
391 }
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (!tfo->write_pending) {
393 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 return -EINVAL;
395 }
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!tfo->write_pending_status) {
397 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 return -EINVAL;
399 }
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!tfo->set_default_node_attributes) {
401 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return -EINVAL;
403 }
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->get_cmd_state) {
405 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->queue_data_in) {
409 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Andy Grover6708bb22011-06-08 10:36:43 -0700412 if (!tfo->queue_status) {
413 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->queue_tm_rsp) {
417 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700420 if (!tfo->aborted_task) {
421 pr_err("Missing tfo->aborted_task()\n");
422 return -EINVAL;
423 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424 /*
425 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
426 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
427 * target_core_fabric_configfs.c WWN+TPG group context code.
428 */
Andy Grover6708bb22011-06-08 10:36:43 -0700429 if (!tfo->fabric_make_wwn) {
430 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 return -EINVAL;
432 }
Andy Grover6708bb22011-06-08 10:36:43 -0700433 if (!tfo->fabric_drop_wwn) {
434 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435 return -EINVAL;
436 }
Andy Grover6708bb22011-06-08 10:36:43 -0700437 if (!tfo->fabric_make_tpg) {
438 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 return -EINVAL;
440 }
Andy Grover6708bb22011-06-08 10:36:43 -0700441 if (!tfo->fabric_drop_tpg) {
442 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800443 return -EINVAL;
444 }
445
446 return 0;
447}
448
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200449int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200451 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800452 int ret;
453
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200454 ret = target_fabric_tf_ops_check(fo);
455 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456 return ret;
457
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200458 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700459 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200460 pr_err("%s: could not allocate memory!\n", __func__);
461 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800462 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200463
464 INIT_LIST_HEAD(&tf->tf_list);
465 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200466 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200467 target_fabric_setup_cits(tf);
468
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800469 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200470 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 mutex_unlock(&g_tf_lock);
472
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200473 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200475EXPORT_SYMBOL(target_register_template);
476
477void target_unregister_template(const struct target_core_fabric_ops *fo)
478{
479 struct target_fabric_configfs *t;
480
481 mutex_lock(&g_tf_lock);
482 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200483 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200484 BUG_ON(atomic_read(&t->tf_access_cnt));
485 list_del(&t->tf_list);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700486 mutex_unlock(&g_tf_lock);
487 /*
488 * Wait for any outstanding fabric se_deve_entry->rcu_head
489 * callbacks to complete post kfree_rcu(), before allowing
490 * fabric driver unload of TFO->module to proceed.
491 */
492 rcu_barrier();
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200493 kfree(t);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700494 return;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200495 }
496 }
497 mutex_unlock(&g_tf_lock);
498}
499EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500
501/*##############################################################################
502// Stop functions called by external Target Fabrics Modules
503//############################################################################*/
504
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200505static inline struct se_dev_attrib *to_attrib(struct config_item *item)
506{
507 return container_of(to_config_group(item), struct se_dev_attrib,
508 da_group);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200509}
510
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200511/* Start functions for struct config_item_type tb_dev_attrib_cit */
512#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
513static ssize_t _name##_show(struct config_item *item, char *page) \
514{ \
515 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
516}
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200517
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200518DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
519DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
520DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
521DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
522DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
523DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
524DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
525DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
526DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
527DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
528DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
529DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
530DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
531DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
532DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
533DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
534DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
535DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
536DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
537DEF_CONFIGFS_ATTRIB_SHOW(block_size);
538DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
539DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
540DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
541DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
542DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
543DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
544DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
545DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
Jamie Pocase6f41632015-11-29 14:44:57 -0800546DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200547DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
548
549#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
550static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200551 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200552{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200553 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200554 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200555 int ret; \
556 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200557 ret = kstrtou32(page, 0, &val); \
558 if (ret < 0) \
559 return ret; \
560 da->_name = val; \
561 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200562}
563
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200564DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
565DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
566DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
567DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
568DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200569
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200570#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
571static ssize_t _name##_store(struct config_item *item, const char *page, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200572 size_t count) \
573{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200574 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200575 bool flag; \
576 int ret; \
577 \
578 ret = strtobool(page, &flag); \
579 if (ret < 0) \
580 return ret; \
581 da->_name = flag; \
582 return count; \
583}
584
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200585DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
586DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
587DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
588DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
589DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200590
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200591#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
592static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200593 size_t count) \
594{ \
595 printk_once(KERN_WARNING \
Christophe Vu-Brugier234bdbc2015-11-18 09:22:58 +0100596 "ignoring deprecated %s attribute\n", \
597 __stringify(_name)); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200598 return count; \
599}
600
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200601DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
602DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200603
604static void dev_set_t10_wwn_model_alias(struct se_device *dev)
605{
606 const char *configname;
607
608 configname = config_item_name(&dev->dev_group.cg_item);
609 if (strlen(configname) >= 16) {
610 pr_warn("dev[%p]: Backstore name '%s' is too long for "
611 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
612 configname);
613 }
614 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
615}
616
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200617static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200618 const char *page, size_t count)
619{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200620 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200621 struct se_device *dev = da->da_dev;
622 bool flag;
623 int ret;
624
625 if (dev->export_count) {
626 pr_err("dev[%p]: Unable to change model alias"
627 " while export_count is %d\n",
628 dev, dev->export_count);
629 return -EINVAL;
630 }
631
632 ret = strtobool(page, &flag);
633 if (ret < 0)
634 return ret;
635
636 if (flag) {
637 dev_set_t10_wwn_model_alias(dev);
638 } else {
639 strncpy(&dev->t10_wwn.model[0],
640 dev->transport->inquiry_prod, 16);
641 }
642 da->emulate_model_alias = flag;
643 return count;
644}
645
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200646static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200647 const char *page, size_t count)
648{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200649 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200650 bool flag;
651 int ret;
652
653 ret = strtobool(page, &flag);
654 if (ret < 0)
655 return ret;
656
657 if (flag && da->da_dev->transport->get_write_cache) {
658 pr_err("emulate_write_cache not supported for this device\n");
659 return -EINVAL;
660 }
661
662 da->emulate_write_cache = flag;
663 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
664 da->da_dev, flag);
665 return count;
666}
667
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200668static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200669 const char *page, size_t count)
670{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200671 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200672 u32 val;
673 int ret;
674
675 ret = kstrtou32(page, 0, &val);
676 if (ret < 0)
677 return ret;
678
679 if (val != 0 && val != 1 && val != 2) {
680 pr_err("Illegal value %d\n", val);
681 return -EINVAL;
682 }
683
684 if (da->da_dev->export_count) {
685 pr_err("dev[%p]: Unable to change SE Device"
686 " UA_INTRLCK_CTRL while export_count is %d\n",
687 da->da_dev, da->da_dev->export_count);
688 return -EINVAL;
689 }
690 da->emulate_ua_intlck_ctrl = val;
691 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
692 da->da_dev, val);
693 return count;
694}
695
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200696static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200697 const char *page, size_t count)
698{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200699 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200700 bool flag;
701 int ret;
702
703 ret = strtobool(page, &flag);
704 if (ret < 0)
705 return ret;
706
707 if (da->da_dev->export_count) {
708 pr_err("dev[%p]: Unable to change SE Device TAS while"
709 " export_count is %d\n",
710 da->da_dev, da->da_dev->export_count);
711 return -EINVAL;
712 }
713 da->emulate_tas = flag;
714 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
715 da->da_dev, flag ? "Enabled" : "Disabled");
716
717 return count;
718}
719
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200720static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200721 const char *page, size_t count)
722{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200723 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200724 bool flag;
725 int ret;
726
727 ret = strtobool(page, &flag);
728 if (ret < 0)
729 return ret;
730
731 /*
732 * We expect this value to be non-zero when generic Block Layer
733 * Discard supported is detected iblock_create_virtdevice().
734 */
735 if (flag && !da->max_unmap_block_desc_count) {
736 pr_err("Generic Block Discard not supported\n");
737 return -ENOSYS;
738 }
739
740 da->emulate_tpu = flag;
741 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
742 da->da_dev, flag);
743 return count;
744}
745
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200746static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200747 const char *page, size_t count)
748{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200749 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200750 bool flag;
751 int ret;
752
753 ret = strtobool(page, &flag);
754 if (ret < 0)
755 return ret;
756
757 /*
758 * We expect this value to be non-zero when generic Block Layer
759 * Discard supported is detected iblock_create_virtdevice().
760 */
761 if (flag && !da->max_unmap_block_desc_count) {
762 pr_err("Generic Block Discard not supported\n");
763 return -ENOSYS;
764 }
765
766 da->emulate_tpws = flag;
767 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
768 da->da_dev, flag);
769 return count;
770}
771
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200772static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200773 const char *page, size_t count)
774{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200775 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200776 int old_prot = da->pi_prot_type, ret;
777 struct se_device *dev = da->da_dev;
778 u32 flag;
779
780 ret = kstrtou32(page, 0, &flag);
781 if (ret < 0)
782 return ret;
783
784 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
785 pr_err("Illegal value %d for pi_prot_type\n", flag);
786 return -EINVAL;
787 }
788 if (flag == 2) {
789 pr_err("DIF TYPE2 protection currently not supported\n");
790 return -ENOSYS;
791 }
792 if (da->hw_pi_prot_type) {
793 pr_warn("DIF protection enabled on underlying hardware,"
794 " ignoring\n");
795 return count;
796 }
797 if (!dev->transport->init_prot || !dev->transport->free_prot) {
798 /* 0 is only allowed value for non-supporting backends */
799 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700800 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200801
802 pr_err("DIF protection not supported by backend: %s\n",
803 dev->transport->name);
804 return -ENOSYS;
805 }
806 if (!(dev->dev_flags & DF_CONFIGURED)) {
807 pr_err("DIF protection requires device to be configured\n");
808 return -ENODEV;
809 }
810 if (dev->export_count) {
811 pr_err("dev[%p]: Unable to change SE Device PROT type while"
812 " export_count is %d\n", dev, dev->export_count);
813 return -EINVAL;
814 }
815
816 da->pi_prot_type = flag;
817
818 if (flag && !old_prot) {
819 ret = dev->transport->init_prot(dev);
820 if (ret) {
821 da->pi_prot_type = old_prot;
822 return ret;
823 }
824
825 } else if (!flag && old_prot) {
826 dev->transport->free_prot(dev);
827 }
828
829 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
830 return count;
831}
832
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200833static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200834 const char *page, size_t count)
835{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200836 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200837 struct se_device *dev = da->da_dev;
838 bool flag;
839 int ret;
840
841 ret = strtobool(page, &flag);
842 if (ret < 0)
843 return ret;
844
845 if (!flag)
846 return count;
847
848 if (!dev->transport->format_prot) {
849 pr_err("DIF protection format not supported by backend %s\n",
850 dev->transport->name);
851 return -ENOSYS;
852 }
853 if (!(dev->dev_flags & DF_CONFIGURED)) {
854 pr_err("DIF protection format requires device to be configured\n");
855 return -ENODEV;
856 }
857 if (dev->export_count) {
858 pr_err("dev[%p]: Unable to format SE Device PROT type while"
859 " export_count is %d\n", dev, dev->export_count);
860 return -EINVAL;
861 }
862
863 ret = dev->transport->format_prot(dev);
864 if (ret)
865 return ret;
866
867 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
868 return count;
869}
870
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200871static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200872 const char *page, size_t count)
873{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200874 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200875 bool flag;
876 int ret;
877
878 ret = strtobool(page, &flag);
879 if (ret < 0)
880 return ret;
881 if (da->da_dev->export_count) {
882 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
883 " export_count is %d\n",
884 da->da_dev, da->da_dev->export_count);
885 return -EINVAL;
886 }
887
888 da->force_pr_aptpl = flag;
889 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
890 return count;
891}
892
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200893static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200894 const char *page, size_t count)
895{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200896 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200897 bool flag;
898 int ret;
899
900 ret = strtobool(page, &flag);
901 if (ret < 0)
902 return ret;
903
904 if (flag != 0) {
905 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
906 " reordering not implemented\n", da->da_dev);
907 return -ENOSYS;
908 }
909 da->emulate_rest_reord = flag;
910 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
911 da->da_dev, flag);
912 return count;
913}
914
Jamie Pocase6f41632015-11-29 14:44:57 -0800915static ssize_t unmap_zeroes_data_store(struct config_item *item,
916 const char *page, size_t count)
917{
918 struct se_dev_attrib *da = to_attrib(item);
919 bool flag;
920 int ret;
921
922 ret = strtobool(page, &flag);
923 if (ret < 0)
924 return ret;
925
926 if (da->da_dev->export_count) {
927 pr_err("dev[%p]: Unable to change SE Device"
928 " unmap_zeroes_data while export_count is %d\n",
929 da->da_dev, da->da_dev->export_count);
930 return -EINVAL;
931 }
932 /*
933 * We expect this value to be non-zero when generic Block Layer
934 * Discard supported is detected iblock_configure_device().
935 */
936 if (flag && !da->max_unmap_block_desc_count) {
937 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
938 " because max_unmap_block_desc_count is zero\n",
939 da->da_dev);
Bart Van Assche9b3118c2016-01-05 14:44:21 +0100940 return -ENOSYS;
Jamie Pocase6f41632015-11-29 14:44:57 -0800941 }
942 da->unmap_zeroes_data = flag;
943 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
944 da->da_dev, flag);
Nicholas Bellinger2e498f22016-02-10 20:34:56 -0800945 return count;
Jamie Pocase6f41632015-11-29 14:44:57 -0800946}
947
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200948/*
949 * Note, this can only be called on unexported SE Device Object.
950 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200951static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200952 const char *page, size_t count)
953{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200954 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200955 struct se_device *dev = da->da_dev;
956 u32 val;
957 int ret;
958
959 ret = kstrtou32(page, 0, &val);
960 if (ret < 0)
961 return ret;
962
963 if (dev->export_count) {
964 pr_err("dev[%p]: Unable to change SE Device TCQ while"
965 " export_count is %d\n",
966 dev, dev->export_count);
967 return -EINVAL;
968 }
969 if (!val) {
970 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
971 return -EINVAL;
972 }
973
974 if (val > dev->dev_attrib.queue_depth) {
975 if (val > dev->dev_attrib.hw_queue_depth) {
976 pr_err("dev[%p]: Passed queue_depth:"
977 " %u exceeds TCM/SE_Device MAX"
978 " TCQ: %u\n", dev, val,
979 dev->dev_attrib.hw_queue_depth);
980 return -EINVAL;
981 }
982 }
983 da->queue_depth = dev->queue_depth = val;
984 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
985 return count;
986}
987
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200988static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200989 const char *page, size_t count)
990{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200991 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200992 u32 val;
993 int ret;
994
995 ret = kstrtou32(page, 0, &val);
996 if (ret < 0)
997 return ret;
998
999 if (da->da_dev->export_count) {
1000 pr_err("dev[%p]: Unable to change SE Device"
1001 " optimal_sectors while export_count is %d\n",
1002 da->da_dev, da->da_dev->export_count);
1003 return -EINVAL;
1004 }
1005 if (val > da->hw_max_sectors) {
1006 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1007 " greater than hw_max_sectors: %u\n",
1008 da->da_dev, val, da->hw_max_sectors);
1009 return -EINVAL;
1010 }
1011
1012 da->optimal_sectors = val;
1013 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1014 da->da_dev, val);
1015 return count;
1016}
1017
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001018static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +02001019 const char *page, size_t count)
1020{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001021 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +02001022 u32 val;
1023 int ret;
1024
1025 ret = kstrtou32(page, 0, &val);
1026 if (ret < 0)
1027 return ret;
1028
1029 if (da->da_dev->export_count) {
1030 pr_err("dev[%p]: Unable to change SE Device block_size"
1031 " while export_count is %d\n",
1032 da->da_dev, da->da_dev->export_count);
1033 return -EINVAL;
1034 }
1035
1036 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1037 pr_err("dev[%p]: Illegal value for block_device: %u"
1038 " for SE device, must be 512, 1024, 2048 or 4096\n",
1039 da->da_dev, val);
1040 return -EINVAL;
1041 }
1042
1043 da->block_size = val;
1044 if (da->max_bytes_per_io)
1045 da->hw_max_sectors = da->max_bytes_per_io / val;
1046
1047 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1048 da->da_dev, val);
1049 return count;
1050}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001051
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001052CONFIGFS_ATTR(, emulate_model_alias);
1053CONFIGFS_ATTR(, emulate_dpo);
1054CONFIGFS_ATTR(, emulate_fua_write);
1055CONFIGFS_ATTR(, emulate_fua_read);
1056CONFIGFS_ATTR(, emulate_write_cache);
1057CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1058CONFIGFS_ATTR(, emulate_tas);
1059CONFIGFS_ATTR(, emulate_tpu);
1060CONFIGFS_ATTR(, emulate_tpws);
1061CONFIGFS_ATTR(, emulate_caw);
1062CONFIGFS_ATTR(, emulate_3pc);
1063CONFIGFS_ATTR(, pi_prot_type);
1064CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1065CONFIGFS_ATTR(, pi_prot_format);
1066CONFIGFS_ATTR(, enforce_pr_isids);
1067CONFIGFS_ATTR(, is_nonrot);
1068CONFIGFS_ATTR(, emulate_rest_reord);
1069CONFIGFS_ATTR(, force_pr_aptpl);
1070CONFIGFS_ATTR_RO(, hw_block_size);
1071CONFIGFS_ATTR(, block_size);
1072CONFIGFS_ATTR_RO(, hw_max_sectors);
1073CONFIGFS_ATTR(, optimal_sectors);
1074CONFIGFS_ATTR_RO(, hw_queue_depth);
1075CONFIGFS_ATTR(, queue_depth);
1076CONFIGFS_ATTR(, max_unmap_lba_count);
1077CONFIGFS_ATTR(, max_unmap_block_desc_count);
1078CONFIGFS_ATTR(, unmap_granularity);
1079CONFIGFS_ATTR(, unmap_granularity_alignment);
Jamie Pocase6f41632015-11-29 14:44:57 -08001080CONFIGFS_ATTR(, unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001081CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001083/*
1084 * dev_attrib attributes for devices using the target core SBC/SPC
1085 * interpreter. Any backend using spc_parse_cdb should be using
1086 * these.
1087 */
1088struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001089 &attr_emulate_model_alias,
1090 &attr_emulate_dpo,
1091 &attr_emulate_fua_write,
1092 &attr_emulate_fua_read,
1093 &attr_emulate_write_cache,
1094 &attr_emulate_ua_intlck_ctrl,
1095 &attr_emulate_tas,
1096 &attr_emulate_tpu,
1097 &attr_emulate_tpws,
1098 &attr_emulate_caw,
1099 &attr_emulate_3pc,
1100 &attr_pi_prot_type,
1101 &attr_hw_pi_prot_type,
1102 &attr_pi_prot_format,
1103 &attr_enforce_pr_isids,
1104 &attr_is_nonrot,
1105 &attr_emulate_rest_reord,
1106 &attr_force_pr_aptpl,
1107 &attr_hw_block_size,
1108 &attr_block_size,
1109 &attr_hw_max_sectors,
1110 &attr_optimal_sectors,
1111 &attr_hw_queue_depth,
1112 &attr_queue_depth,
1113 &attr_max_unmap_lba_count,
1114 &attr_max_unmap_block_desc_count,
1115 &attr_unmap_granularity,
1116 &attr_unmap_granularity_alignment,
Jamie Pocase6f41632015-11-29 14:44:57 -08001117 &attr_unmap_zeroes_data,
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001118 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001119 NULL,
1120};
1121EXPORT_SYMBOL(sbc_attrib_attrs);
1122
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001123/*
1124 * Minimal dev_attrib attributes for devices passing through CDBs.
1125 * In this case we only provide a few read-only attributes for
1126 * backwards compatibility.
1127 */
1128struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001129 &attr_hw_pi_prot_type,
1130 &attr_hw_block_size,
1131 &attr_hw_max_sectors,
1132 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001133 NULL,
1134};
1135EXPORT_SYMBOL(passthrough_attrib_attrs);
1136
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001137TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001139/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001141/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001143static struct t10_wwn *to_t10_wwn(struct config_item *item)
1144{
1145 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1146}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147
1148/*
1149 * VPD page 0x80 Unit serial
1150 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001151static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1152 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001155 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156}
1157
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001158static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1159 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001161 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001162 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1164
1165 /*
1166 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1167 * from the struct scsi_device level firmware, do not allow
1168 * VPD Unit Serial to be emulated.
1169 *
1170 * Note this struct scsi_device could also be emulating VPD
1171 * information from its drivers/scsi LLD. But for now we assume
1172 * it is doing 'the right thing' wrt a world wide unique
1173 * VPD Unit Serial Number that OS dependent multipath can depend on.
1174 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001175 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001176 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 " Unit Serial, ignoring request\n");
1178 return -EOPNOTSUPP;
1179 }
1180
Dan Carpenter60d645a2011-06-15 10:03:05 -07001181 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001182 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1184 return -EOVERFLOW;
1185 }
1186 /*
1187 * Check to see if any active $FABRIC_MOD exports exist. If they
1188 * do exist, fail here as changing this information on the fly
1189 * (underneath the initiator side OS dependent multipath code)
1190 * could cause negative effects.
1191 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001192 if (dev->export_count) {
1193 pr_err("Unable to set VPD Unit Serial while"
1194 " active %d $FABRIC_MOD exports exist\n",
1195 dev->export_count);
1196 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 /*
1200 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1201 *
1202 * Also, strip any newline added from the userspace
1203 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1204 */
1205 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1206 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001207 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001209 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210
Andy Grover6708bb22011-06-08 10:36:43 -07001211 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001212 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213
1214 return count;
1215}
1216
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217/*
1218 * VPD page 0x83 Protocol Identifier
1219 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001220static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1221 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001223 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 struct t10_vpd *vpd;
1225 unsigned char buf[VPD_TMP_BUF_SIZE];
1226 ssize_t len = 0;
1227
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228 memset(buf, 0, VPD_TMP_BUF_SIZE);
1229
1230 spin_lock(&t10_wwn->t10_vpd_lock);
1231 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001232 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 continue;
1234
1235 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1236
Andy Grover6708bb22011-06-08 10:36:43 -07001237 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238 break;
1239
1240 len += sprintf(page+len, "%s", buf);
1241 }
1242 spin_unlock(&t10_wwn->t10_vpd_lock);
1243
1244 return len;
1245}
1246
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247/*
1248 * Generic wrapper for dumping VPD identifiers by association.
1249 */
1250#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001251static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1252 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001254 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1255 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1257 ssize_t len = 0; \
1258 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 spin_lock(&t10_wwn->t10_vpd_lock); \
1260 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1261 if (vpd->association != _assoc) \
1262 continue; \
1263 \
1264 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1265 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001266 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 break; \
1268 len += sprintf(page+len, "%s", buf); \
1269 \
1270 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1271 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001272 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001273 break; \
1274 len += sprintf(page+len, "%s", buf); \
1275 \
1276 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1277 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001278 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279 break; \
1280 len += sprintf(page+len, "%s", buf); \
1281 } \
1282 spin_unlock(&t10_wwn->t10_vpd_lock); \
1283 \
1284 return len; \
1285}
1286
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001287/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001289/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001291/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1293
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001294CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1295CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1296CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1297CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1298CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001299
1300static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001301 &target_wwn_attr_vpd_unit_serial,
1302 &target_wwn_attr_vpd_protocol_identifier,
1303 &target_wwn_attr_vpd_assoc_logical_unit,
1304 &target_wwn_attr_vpd_assoc_target_port,
1305 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 NULL,
1307};
1308
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001309TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001311/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001313/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001315static struct se_device *pr_to_dev(struct config_item *item)
1316{
1317 return container_of(to_config_group(item), struct se_device,
1318 dev_pr_group);
1319}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320
Christoph Hellwigd977f432012-10-10 17:37:15 -04001321static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1322 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323{
1324 struct se_node_acl *se_nacl;
1325 struct t10_pr_registration *pr_reg;
1326 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327
1328 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1329
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001330 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001331 if (!pr_reg)
1332 return sprintf(page, "No SPC-3 Reservation holder\n");
1333
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001335 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336
Christoph Hellwigd977f432012-10-10 17:37:15 -04001337 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001338 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001339 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340}
1341
Christoph Hellwigd977f432012-10-10 17:37:15 -04001342static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1343 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344{
1345 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001346 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001349 if (se_nacl) {
1350 len = sprintf(page,
1351 "SPC-2 Reservation: %s Initiator: %s\n",
1352 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1353 se_nacl->initiatorname);
1354 } else {
1355 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001357 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001358}
1359
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001360static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001361{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001362 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001363 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364
Andy Grovera3541702015-05-19 14:44:41 -07001365 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001366 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001367
Christoph Hellwigd977f432012-10-10 17:37:15 -04001368 spin_lock(&dev->dev_reservation_lock);
1369 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1370 ret = target_core_dev_pr_show_spc2_res(dev, page);
1371 else
1372 ret = target_core_dev_pr_show_spc3_res(dev, page);
1373 spin_unlock(&dev->dev_reservation_lock);
1374 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375}
1376
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001377static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1378 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001380 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381 ssize_t len = 0;
1382
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001384 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001386 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 len = sprintf(page, "SPC-3 Reservation: All Target"
1388 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001389 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001390 len = sprintf(page, "SPC-3 Reservation: Single"
1391 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001392 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393
Christoph Hellwigd977f432012-10-10 17:37:15 -04001394 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395 return len;
1396}
1397
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001398static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1399 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001400{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001401 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402}
1403
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001405static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1406 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001408 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001410 struct se_portal_group *se_tpg;
1411 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001412 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001413 ssize_t len = 0;
1414
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415 spin_lock(&dev->dev_reservation_lock);
1416 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001417 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001419 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001421
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001422 se_nacl = pr_reg->pr_reg_nacl;
1423 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001424 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425
1426 len += sprintf(page+len, "SPC-3 Reservation: %s"
1427 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1428 tfo->tpg_get_wwn(se_tpg));
1429 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001430 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001431 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001433 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434
Christoph Hellwigd977f432012-10-10 17:37:15 -04001435out_unlock:
1436 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437 return len;
1438}
1439
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001441static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1442 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001444 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001445 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446 struct t10_pr_registration *pr_reg;
1447 unsigned char buf[384];
1448 char i_buf[PR_REG_ISID_ID_LEN];
1449 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001450 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001451
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1453
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001454 spin_lock(&dev->t10_pr.registration_lock);
1455 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 pr_reg_list) {
1457
1458 memset(buf, 0, 384);
1459 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1460 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001461 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001462 PR_REG_ISID_ID_LEN);
1463 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1464 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001465 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 pr_reg->pr_res_generation);
1467
Andy Grover6708bb22011-06-08 10:36:43 -07001468 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469 break;
1470
1471 len += sprintf(page+len, "%s", buf);
1472 reg_count++;
1473 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001474 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475
Andy Grover6708bb22011-06-08 10:36:43 -07001476 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 len += sprintf(page+len, "None\n");
1478
1479 return len;
1480}
1481
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001482static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001484 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 struct t10_pr_registration *pr_reg;
1486 ssize_t len = 0;
1487
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001488 spin_lock(&dev->dev_reservation_lock);
1489 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001490 if (pr_reg) {
1491 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1492 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1493 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496
Christoph Hellwigd977f432012-10-10 17:37:15 -04001497 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498 return len;
1499}
1500
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001501static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001503 struct se_device *dev = pr_to_dev(item);
1504
Andy Grovera3541702015-05-19 14:44:41 -07001505 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001506 return sprintf(page, "SPC_PASSTHROUGH\n");
1507 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1508 return sprintf(page, "SPC2_RESERVATIONS\n");
1509 else
1510 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001511}
1512
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001513static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1514 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001516 struct se_device *dev = pr_to_dev(item);
1517
Andy Grovera3541702015-05-19 14:44:41 -07001518 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519 return 0;
1520
1521 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001522 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523}
1524
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001525static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1526 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001528 struct se_device *dev = pr_to_dev(item);
1529
Andy Grovera3541702015-05-19 14:44:41 -07001530 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 return 0;
1532
1533 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1534}
1535
1536enum {
1537 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1538 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1539 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1540 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1541};
1542
1543static match_table_t tokens = {
1544 {Opt_initiator_fabric, "initiator_fabric=%s"},
1545 {Opt_initiator_node, "initiator_node=%s"},
1546 {Opt_initiator_sid, "initiator_sid=%s"},
1547 {Opt_sa_res_key, "sa_res_key=%s"},
1548 {Opt_res_holder, "res_holder=%d"},
1549 {Opt_res_type, "res_type=%d"},
1550 {Opt_res_scope, "res_scope=%d"},
1551 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001552 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553 {Opt_target_fabric, "target_fabric=%s"},
1554 {Opt_target_node, "target_node=%s"},
1555 {Opt_tpgt, "tpgt=%d"},
1556 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001557 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001558 {Opt_err, NULL}
1559};
1560
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001561static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1562 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001563{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001564 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001565 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1566 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001567 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 substring_t args[MAX_OPT_ARGS];
1569 unsigned long long tmp_ll;
1570 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001571 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001573 u16 tpgt = 0;
1574 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575
Andy Grovera3541702015-05-19 14:44:41 -07001576 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001577 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001578 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001579 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001580
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001581 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001582 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 " active fabric exports exist\n");
1584 return -EINVAL;
1585 }
1586
1587 opts = kstrdup(page, GFP_KERNEL);
1588 if (!opts)
1589 return -ENOMEM;
1590
1591 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001592 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001593 if (!*ptr)
1594 continue;
1595
1596 token = match_token(ptr, tokens, args);
1597 switch (token) {
1598 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001599 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001600 if (!i_fabric) {
1601 ret = -ENOMEM;
1602 goto out;
1603 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604 break;
1605 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001606 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001607 if (!i_port) {
1608 ret = -ENOMEM;
1609 goto out;
1610 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001611 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001612 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001613 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1614 PR_APTPL_MAX_IPORT_LEN);
1615 ret = -EINVAL;
1616 break;
1617 }
1618 break;
1619 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001620 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001621 if (!isid) {
1622 ret = -ENOMEM;
1623 goto out;
1624 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001625 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001626 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 "= exceeds PR_REG_ISID_LEN: %d\n",
1628 PR_REG_ISID_LEN);
1629 ret = -EINVAL;
1630 break;
1631 }
1632 break;
1633 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001634 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001636 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 goto out;
1638 }
1639 sa_res_key = (u64)tmp_ll;
1640 break;
1641 /*
1642 * PR APTPL Metadata for Reservation
1643 */
1644 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001645 ret = match_int(args, &arg);
1646 if (ret)
1647 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648 res_holder = arg;
1649 break;
1650 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001651 ret = match_int(args, &arg);
1652 if (ret)
1653 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 type = (u8)arg;
1655 break;
1656 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001657 ret = match_int(args, &arg);
1658 if (ret)
1659 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001660 break;
1661 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001662 ret = match_int(args, &arg);
1663 if (ret)
1664 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001665 all_tg_pt = (int)arg;
1666 break;
1667 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001668 ret = match_int(args, &arg);
1669 if (ret)
1670 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001671 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 break;
1673 /*
1674 * PR APTPL Metadata for Target Port
1675 */
1676 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001677 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001678 if (!t_fabric) {
1679 ret = -ENOMEM;
1680 goto out;
1681 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001682 break;
1683 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001684 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001685 if (!t_port) {
1686 ret = -ENOMEM;
1687 goto out;
1688 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001689 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001690 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1692 PR_APTPL_MAX_TPORT_LEN);
1693 ret = -EINVAL;
1694 break;
1695 }
1696 break;
1697 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001698 ret = match_int(args, &arg);
1699 if (ret)
1700 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 tpgt = (u16)arg;
1702 break;
1703 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001704 ret = match_int(args, &arg);
1705 if (ret)
1706 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001707 break;
1708 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001709 ret = match_int(args, &arg);
1710 if (ret)
1711 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001712 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 break;
1714 default:
1715 break;
1716 }
1717 }
1718
Andy Grover6708bb22011-06-08 10:36:43 -07001719 if (!i_port || !t_port || !sa_res_key) {
1720 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 ret = -EINVAL;
1722 goto out;
1723 }
1724
1725 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001726 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 " holder\n", type);
1728 ret = -EINVAL;
1729 goto out;
1730 }
1731
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001732 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1734 res_holder, all_tg_pt, type);
1735out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001736 kfree(i_fabric);
1737 kfree(i_port);
1738 kfree(isid);
1739 kfree(t_fabric);
1740 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741 kfree(orig);
1742 return (ret == 0) ? count : ret;
1743}
1744
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001746CONFIGFS_ATTR_RO(target_pr_, res_holder);
1747CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1748CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1749CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1750CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1751CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1752CONFIGFS_ATTR_RO(target_pr_, res_type);
1753CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1754CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755
1756static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001757 &target_pr_attr_res_holder,
1758 &target_pr_attr_res_pr_all_tgt_pts,
1759 &target_pr_attr_res_pr_generation,
1760 &target_pr_attr_res_pr_holder_tg_port,
1761 &target_pr_attr_res_pr_registered_i_pts,
1762 &target_pr_attr_res_pr_type,
1763 &target_pr_attr_res_type,
1764 &target_pr_attr_res_aptpl_active,
1765 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001766 NULL,
1767};
1768
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001769TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001771/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001773/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001775static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001777 return container_of(to_config_group(item), struct se_device, dev_group);
1778}
1779
1780static ssize_t target_dev_info_show(struct config_item *item, char *page)
1781{
1782 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783 int bl = 0;
1784 ssize_t read_bytes = 0;
1785
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001786 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001788 read_bytes += dev->transport->show_configfs_dev_params(dev,
1789 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001790 return read_bytes;
1791}
1792
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001793static ssize_t target_dev_control_store(struct config_item *item,
1794 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001795{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001796 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001798 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001799}
1800
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001801static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001802{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001803 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001804
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001805 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 return 0;
1807
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001808 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001809}
1810
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001811static ssize_t target_dev_alias_store(struct config_item *item,
1812 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001813{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001814 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001815 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 ssize_t read_bytes;
1817
1818 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001819 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001820 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1821 SE_DEV_ALIAS_LEN-1);
1822 return -EINVAL;
1823 }
1824
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001825 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001826 if (!read_bytes)
1827 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001828 if (dev->dev_alias[read_bytes - 1] == '\n')
1829 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001830
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001831 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001832
Andy Grover6708bb22011-06-08 10:36:43 -07001833 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001835 config_item_name(&dev->dev_group.cg_item),
1836 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837
1838 return read_bytes;
1839}
1840
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001841static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001843 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001844
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001845 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846 return 0;
1847
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001848 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001849}
1850
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001851static ssize_t target_dev_udev_path_store(struct config_item *item,
1852 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001854 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001855 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856 ssize_t read_bytes;
1857
1858 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001859 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1861 SE_UDEV_PATH_LEN-1);
1862 return -EINVAL;
1863 }
1864
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001865 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001867 if (!read_bytes)
1868 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001869 if (dev->udev_path[read_bytes - 1] == '\n')
1870 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001871
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001872 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001873
Andy Grover6708bb22011-06-08 10:36:43 -07001874 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001875 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001876 config_item_name(&dev->dev_group.cg_item),
1877 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001878
1879 return read_bytes;
1880}
1881
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001882static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001883{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001884 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001885
1886 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1887}
1888
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001889static ssize_t target_dev_enable_store(struct config_item *item,
1890 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001891{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001892 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001893 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001894 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895
1896 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001897 if (!ptr) {
1898 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899 " is \"1\"\n");
1900 return -EINVAL;
1901 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001903 ret = target_configure_device(dev);
1904 if (ret)
1905 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906 return count;
1907}
1908
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001909static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001910{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001911 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 struct config_item *lu_ci;
1913 struct t10_alua_lu_gp *lu_gp;
1914 struct t10_alua_lu_gp_member *lu_gp_mem;
1915 ssize_t len = 0;
1916
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001918 if (!lu_gp_mem)
1919 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920
1921 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1922 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001923 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 lu_ci = &lu_gp->lu_gp_group.cg_item;
1925 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1926 config_item_name(lu_ci), lu_gp->lu_gp_id);
1927 }
1928 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1929
1930 return len;
1931}
1932
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001933static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1934 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001935{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001936 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001937 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1939 struct t10_alua_lu_gp_member *lu_gp_mem;
1940 unsigned char buf[LU_GROUP_NAME_BUF];
1941 int move = 0;
1942
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001943 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1944 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001945 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001946
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001947 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001948 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949 return -EINVAL;
1950 }
1951 memset(buf, 0, LU_GROUP_NAME_BUF);
1952 memcpy(buf, page, count);
1953 /*
1954 * Any ALUA logical unit alias besides "NULL" means we will be
1955 * making a new group association.
1956 */
1957 if (strcmp(strstrip(buf), "NULL")) {
1958 /*
1959 * core_alua_get_lu_gp_by_name() will increment reference to
1960 * struct t10_alua_lu_gp. This reference is released with
1961 * core_alua_get_lu_gp_by_name below().
1962 */
1963 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001964 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965 return -ENODEV;
1966 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967
1968 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1969 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001970 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971 /*
1972 * Clearing an existing lu_gp association, and replacing
1973 * with NULL
1974 */
Andy Grover6708bb22011-06-08 10:36:43 -07001975 if (!lu_gp_new) {
1976 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1978 " %hu\n",
1979 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001980 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 config_item_name(&lu_gp->lu_gp_group.cg_item),
1982 lu_gp->lu_gp_id);
1983
1984 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1985 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1986
1987 return count;
1988 }
1989 /*
1990 * Removing existing association of lu_gp_mem with lu_gp
1991 */
1992 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1993 move = 1;
1994 }
1995 /*
1996 * Associate lu_gp_mem with lu_gp_new.
1997 */
1998 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1999 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2000
Andy Grover6708bb22011-06-08 10:36:43 -07002001 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 " core/alua/lu_gps/%s, ID: %hu\n",
2003 (move) ? "Moving" : "Adding",
2004 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002005 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2007 lu_gp_new->lu_gp_id);
2008
2009 core_alua_put_lu_gp_from_name(lu_gp_new);
2010 return count;
2011}
2012
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002013static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002014{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002015 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002016 struct t10_alua_lba_map *map;
2017 struct t10_alua_lba_map_member *mem;
2018 char *b = page;
2019 int bl = 0;
2020 char state;
2021
2022 spin_lock(&dev->t10_alua.lba_map_lock);
2023 if (!list_empty(&dev->t10_alua.lba_map_list))
2024 bl += sprintf(b + bl, "%u %u\n",
2025 dev->t10_alua.lba_map_segment_size,
2026 dev->t10_alua.lba_map_segment_multiplier);
2027 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2028 bl += sprintf(b + bl, "%llu %llu",
2029 map->lba_map_first_lba, map->lba_map_last_lba);
2030 list_for_each_entry(mem, &map->lba_map_mem_list,
2031 lba_map_mem_list) {
2032 switch (mem->lba_map_mem_alua_state) {
2033 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2034 state = 'O';
2035 break;
2036 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2037 state = 'A';
2038 break;
2039 case ALUA_ACCESS_STATE_STANDBY:
2040 state = 'S';
2041 break;
2042 case ALUA_ACCESS_STATE_UNAVAILABLE:
2043 state = 'U';
2044 break;
2045 default:
2046 state = '.';
2047 break;
2048 }
2049 bl += sprintf(b + bl, " %d:%c",
2050 mem->lba_map_mem_alua_pg_id, state);
2051 }
2052 bl += sprintf(b + bl, "\n");
2053 }
2054 spin_unlock(&dev->t10_alua.lba_map_lock);
2055 return bl;
2056}
2057
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002058static ssize_t target_dev_lba_map_store(struct config_item *item,
2059 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002060{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002061 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002062 struct t10_alua_lba_map *lba_map = NULL;
2063 struct list_head lba_list;
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002064 char *map_entries, *orig, *ptr;
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002065 char state;
2066 int pg_num = -1, pg;
2067 int ret = 0, num = 0, pg_id, alua_state;
2068 unsigned long start_lba = -1, end_lba = -1;
2069 unsigned long segment_size = -1, segment_mult = -1;
2070
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002071 orig = map_entries = kstrdup(page, GFP_KERNEL);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002072 if (!map_entries)
2073 return -ENOMEM;
2074
2075 INIT_LIST_HEAD(&lba_list);
2076 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2077 if (!*ptr)
2078 continue;
2079
2080 if (num == 0) {
2081 if (sscanf(ptr, "%lu %lu\n",
2082 &segment_size, &segment_mult) != 2) {
2083 pr_err("Invalid line %d\n", num);
2084 ret = -EINVAL;
2085 break;
2086 }
2087 num++;
2088 continue;
2089 }
2090 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2091 pr_err("Invalid line %d\n", num);
2092 ret = -EINVAL;
2093 break;
2094 }
2095 ptr = strchr(ptr, ' ');
2096 if (!ptr) {
2097 pr_err("Invalid line %d, missing end lba\n", num);
2098 ret = -EINVAL;
2099 break;
2100 }
2101 ptr++;
2102 ptr = strchr(ptr, ' ');
2103 if (!ptr) {
2104 pr_err("Invalid line %d, missing state definitions\n",
2105 num);
2106 ret = -EINVAL;
2107 break;
2108 }
2109 ptr++;
2110 lba_map = core_alua_allocate_lba_map(&lba_list,
2111 start_lba, end_lba);
2112 if (IS_ERR(lba_map)) {
2113 ret = PTR_ERR(lba_map);
2114 break;
2115 }
2116 pg = 0;
2117 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2118 switch (state) {
2119 case 'O':
2120 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2121 break;
2122 case 'A':
2123 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2124 break;
2125 case 'S':
2126 alua_state = ALUA_ACCESS_STATE_STANDBY;
2127 break;
2128 case 'U':
2129 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2130 break;
2131 default:
2132 pr_err("Invalid ALUA state '%c'\n", state);
2133 ret = -EINVAL;
2134 goto out;
2135 }
2136
2137 ret = core_alua_allocate_lba_map_mem(lba_map,
2138 pg_id, alua_state);
2139 if (ret) {
2140 pr_err("Invalid target descriptor %d:%c "
2141 "at line %d\n",
2142 pg_id, state, num);
2143 break;
2144 }
2145 pg++;
2146 ptr = strchr(ptr, ' ');
2147 if (ptr)
2148 ptr++;
2149 else
2150 break;
2151 }
2152 if (pg_num == -1)
2153 pg_num = pg;
2154 else if (pg != pg_num) {
2155 pr_err("Only %d from %d port groups definitions "
2156 "at line %d\n", pg, pg_num, num);
2157 ret = -EINVAL;
2158 break;
2159 }
2160 num++;
2161 }
2162out:
2163 if (ret) {
2164 core_alua_free_lba_map(&lba_list);
2165 count = ret;
2166 } else
2167 core_alua_set_lba_map(dev, &lba_list,
2168 segment_size, segment_mult);
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002169 kfree(orig);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002170 return count;
2171}
2172
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002173CONFIGFS_ATTR_RO(target_dev_, info);
2174CONFIGFS_ATTR_WO(target_dev_, control);
2175CONFIGFS_ATTR(target_dev_, alias);
2176CONFIGFS_ATTR(target_dev_, udev_path);
2177CONFIGFS_ATTR(target_dev_, enable);
2178CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2179CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002180
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002181static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002182 &target_dev_attr_info,
2183 &target_dev_attr_control,
2184 &target_dev_attr_alias,
2185 &target_dev_attr_udev_path,
2186 &target_dev_attr_enable,
2187 &target_dev_attr_alua_lu_gp,
2188 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002189 NULL,
2190};
2191
2192static void target_core_dev_release(struct config_item *item)
2193{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002194 struct config_group *dev_cg = to_config_group(item);
2195 struct se_device *dev =
2196 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002198 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199}
2200
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201static struct configfs_item_operations target_core_dev_item_ops = {
2202 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002203};
2204
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002205TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002207/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002208
2209/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2210
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002211static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002213 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2214 lu_gp_group);
2215}
2216
2217static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2218{
2219 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2220
Andy Grover6708bb22011-06-08 10:36:43 -07002221 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002222 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2224}
2225
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002226static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2227 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002228{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002229 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2231 unsigned long lu_gp_id;
2232 int ret;
2233
Jingoo Han57103d72013-07-19 16:22:19 +09002234 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002236 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002238 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 }
2240 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002241 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242 " 0x0000ffff\n", lu_gp_id);
2243 return -EINVAL;
2244 }
2245
2246 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2247 if (ret < 0)
2248 return -EINVAL;
2249
Andy Grover6708bb22011-06-08 10:36:43 -07002250 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2252 config_item_name(&alua_lu_gp_cg->cg_item),
2253 lu_gp->lu_gp_id);
2254
2255 return count;
2256}
2257
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002258static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002259{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002260 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261 struct se_device *dev;
2262 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 struct t10_alua_lu_gp_member *lu_gp_mem;
2264 ssize_t len = 0, cur_len;
2265 unsigned char buf[LU_GROUP_NAME_BUF];
2266
2267 memset(buf, 0, LU_GROUP_NAME_BUF);
2268
2269 spin_lock(&lu_gp->lu_gp_lock);
2270 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2271 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002272 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002273
2274 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2275 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002276 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 cur_len++; /* Extra byte for NULL terminator */
2278
2279 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002280 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281 "_members buffer\n");
2282 break;
2283 }
2284 memcpy(page+len, buf, cur_len);
2285 len += cur_len;
2286 }
2287 spin_unlock(&lu_gp->lu_gp_lock);
2288
2289 return len;
2290}
2291
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002292CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2293CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294
2295static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002296 &target_lu_gp_attr_lu_gp_id,
2297 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 NULL,
2299};
2300
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002301static void target_core_alua_lu_gp_release(struct config_item *item)
2302{
2303 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2304 struct t10_alua_lu_gp, lu_gp_group);
2305
2306 core_alua_free_lu_gp(lu_gp);
2307}
2308
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002310 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002311};
2312
2313static struct config_item_type target_core_alua_lu_gp_cit = {
2314 .ct_item_ops = &target_core_alua_lu_gp_ops,
2315 .ct_attrs = target_core_alua_lu_gp_attrs,
2316 .ct_owner = THIS_MODULE,
2317};
2318
2319/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2320
2321/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2322
2323static struct config_group *target_core_alua_create_lu_gp(
2324 struct config_group *group,
2325 const char *name)
2326{
2327 struct t10_alua_lu_gp *lu_gp;
2328 struct config_group *alua_lu_gp_cg = NULL;
2329 struct config_item *alua_lu_gp_ci = NULL;
2330
2331 lu_gp = core_alua_allocate_lu_gp(name, 0);
2332 if (IS_ERR(lu_gp))
2333 return NULL;
2334
2335 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2336 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2337
2338 config_group_init_type_name(alua_lu_gp_cg, name,
2339 &target_core_alua_lu_gp_cit);
2340
Andy Grover6708bb22011-06-08 10:36:43 -07002341 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342 " Group: core/alua/lu_gps/%s\n",
2343 config_item_name(alua_lu_gp_ci));
2344
2345 return alua_lu_gp_cg;
2346
2347}
2348
2349static void target_core_alua_drop_lu_gp(
2350 struct config_group *group,
2351 struct config_item *item)
2352{
2353 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2354 struct t10_alua_lu_gp, lu_gp_group);
2355
Andy Grover6708bb22011-06-08 10:36:43 -07002356 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002357 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2358 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002359 /*
2360 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2361 * -> target_core_alua_lu_gp_release()
2362 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002364}
2365
2366static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2367 .make_group = &target_core_alua_create_lu_gp,
2368 .drop_item = &target_core_alua_drop_lu_gp,
2369};
2370
2371static struct config_item_type target_core_alua_lu_gps_cit = {
2372 .ct_item_ops = NULL,
2373 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2374 .ct_owner = THIS_MODULE,
2375};
2376
2377/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2378
2379/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2380
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002381static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002383 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2384 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385}
2386
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002387static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2388 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002390 return sprintf(page, "%d\n",
2391 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2392}
2393
2394static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2395 const char *page, size_t count)
2396{
2397 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002398 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002399 unsigned long tmp;
2400 int new_state, ret;
2401
Andy Grover6708bb22011-06-08 10:36:43 -07002402 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002403 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002404 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2405 return -EINVAL;
2406 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002407 if (!(dev->dev_flags & DF_CONFIGURED)) {
2408 pr_err("Unable to set alua_access_state while device is"
2409 " not configured\n");
2410 return -ENODEV;
2411 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412
Jingoo Han57103d72013-07-19 16:22:19 +09002413 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002415 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002416 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002417 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418 }
2419 new_state = (int)tmp;
2420
Hannes Reinecke125d0112013-11-19 09:07:46 +01002421 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2422 pr_err("Unable to process implicit configfs ALUA"
2423 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 return -EINVAL;
2425 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002426 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2427 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2428 /* LBA DEPENDENT is only allowed with implicit ALUA */
2429 pr_err("Unable to process implicit configfs ALUA transition"
2430 " while explicit ALUA management is enabled\n");
2431 return -EINVAL;
2432 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002434 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 NULL, NULL, new_state, 0);
2436 return (!ret) ? count : -EINVAL;
2437}
2438
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002439static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2440 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002442 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 return sprintf(page, "%s\n",
2444 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2445}
2446
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002447static ssize_t target_tg_pt_gp_alua_access_status_store(
2448 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002450 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002451 unsigned long tmp;
2452 int new_status, ret;
2453
Andy Grover6708bb22011-06-08 10:36:43 -07002454 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2455 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002456 " valid tg_pt_gp ID: %hu\n",
2457 tg_pt_gp->tg_pt_gp_valid_id);
2458 return -EINVAL;
2459 }
2460
Jingoo Han57103d72013-07-19 16:22:19 +09002461 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002462 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002463 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002464 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002465 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002466 }
2467 new_status = (int)tmp;
2468
2469 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002470 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2471 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002472 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002473 new_status);
2474 return -EINVAL;
2475 }
2476
2477 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2478 return count;
2479}
2480
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002481static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2482 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002483{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002484 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002485}
2486
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002487static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2488 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002490 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491}
2492
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002493#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2494static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2495 struct config_item *item, char *p) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002496{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002497 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2498 return sprintf(p, "%d\n", \
2499 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2500} \
2501 \
2502static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2503 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002504{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002505 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002506 unsigned long tmp; \
2507 int ret; \
2508 \
2509 if (!t->tg_pt_gp_valid_id) { \
2510 pr_err("Unable to do set ##_name ALUA state on non" \
2511 " valid tg_pt_gp ID: %hu\n", \
2512 t->tg_pt_gp_valid_id); \
2513 return -EINVAL; \
2514 } \
2515 \
2516 ret = kstrtoul(p, 0, &tmp); \
2517 if (ret < 0) { \
2518 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2519 return -EINVAL; \
2520 } \
2521 if (tmp > 1) { \
2522 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2523 return -EINVAL; \
2524 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002525 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002526 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002527 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002528 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002529 \
2530 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002531}
2532
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002533ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2534ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2535ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2536ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2537ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2538ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2539ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002540
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002541static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2542 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002543{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002544 return sprintf(page, "%d\n",
2545 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546}
2547
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002548static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2549 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002550{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002551 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002552 unsigned long tmp;
2553 int ret;
2554
Jingoo Han57103d72013-07-19 16:22:19 +09002555 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002557 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002558 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 }
2560
2561 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002562 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 " %lu\n", tmp);
2564 return -EINVAL;
2565 }
2566 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2567
2568 return count;
2569}
2570
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002571static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2572 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002574 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575}
2576
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002577static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2578 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002580 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2581 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002582}
2583
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002584static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2585 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002587 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588}
2589
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002590static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2591 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002592{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002593 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2594 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595}
2596
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002597static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2598 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002599{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002600 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002601}
2602
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002603static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2604 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002605{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002606 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2607 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002608}
2609
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002610static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2611 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002613 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002614}
2615
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002616static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2617 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002619 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002620}
2621
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002622static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2623 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002625 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2626
Andy Grover6708bb22011-06-08 10:36:43 -07002627 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002628 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2630}
2631
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002632static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2633 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002635 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2637 unsigned long tg_pt_gp_id;
2638 int ret;
2639
Jingoo Han57103d72013-07-19 16:22:19 +09002640 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002641 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002642 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002643 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002644 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002645 }
2646 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002647 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 " 0x0000ffff\n", tg_pt_gp_id);
2649 return -EINVAL;
2650 }
2651
2652 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2653 if (ret < 0)
2654 return -EINVAL;
2655
Andy Grover6708bb22011-06-08 10:36:43 -07002656 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002657 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2658 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2659 tg_pt_gp->tg_pt_gp_id);
2660
2661 return count;
2662}
2663
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002664static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2665 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002667 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 ssize_t len = 0, cur_len;
2670 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2671
2672 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2673
2674 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002675 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2676 lun_tg_pt_gp_link) {
2677 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678
2679 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002680 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2681 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2682 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 config_item_name(&lun->lun_group.cg_item));
2684 cur_len++; /* Extra byte for NULL terminator */
2685
2686 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002687 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002688 "_members buffer\n");
2689 break;
2690 }
2691 memcpy(page+len, buf, cur_len);
2692 len += cur_len;
2693 }
2694 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2695
2696 return len;
2697}
2698
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002699CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2700CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2701CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2702CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2703CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2704CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2705CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2706CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2707CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2708CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2709CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2710CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2711CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2712CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2713CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2714CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2715CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002716
2717static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002718 &target_tg_pt_gp_attr_alua_access_state,
2719 &target_tg_pt_gp_attr_alua_access_status,
2720 &target_tg_pt_gp_attr_alua_access_type,
2721 &target_tg_pt_gp_attr_alua_support_transitioning,
2722 &target_tg_pt_gp_attr_alua_support_offline,
2723 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2724 &target_tg_pt_gp_attr_alua_support_unavailable,
2725 &target_tg_pt_gp_attr_alua_support_standby,
2726 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2727 &target_tg_pt_gp_attr_alua_support_active_optimized,
2728 &target_tg_pt_gp_attr_alua_write_metadata,
2729 &target_tg_pt_gp_attr_nonop_delay_msecs,
2730 &target_tg_pt_gp_attr_trans_delay_msecs,
2731 &target_tg_pt_gp_attr_implicit_trans_secs,
2732 &target_tg_pt_gp_attr_preferred,
2733 &target_tg_pt_gp_attr_tg_pt_gp_id,
2734 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002735 NULL,
2736};
2737
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002738static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2739{
2740 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2741 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2742
2743 core_alua_free_tg_pt_gp(tg_pt_gp);
2744}
2745
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002746static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002747 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748};
2749
2750static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2751 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2752 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2753 .ct_owner = THIS_MODULE,
2754};
2755
2756/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2757
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002758/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759
2760static struct config_group *target_core_alua_create_tg_pt_gp(
2761 struct config_group *group,
2762 const char *name)
2763{
2764 struct t10_alua *alua = container_of(group, struct t10_alua,
2765 alua_tg_pt_gps_group);
2766 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 struct config_group *alua_tg_pt_gp_cg = NULL;
2768 struct config_item *alua_tg_pt_gp_ci = NULL;
2769
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002770 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002771 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002772 return NULL;
2773
2774 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2775 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2776
2777 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2778 &target_core_alua_tg_pt_gp_cit);
2779
Andy Grover6708bb22011-06-08 10:36:43 -07002780 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781 " Group: alua/tg_pt_gps/%s\n",
2782 config_item_name(alua_tg_pt_gp_ci));
2783
2784 return alua_tg_pt_gp_cg;
2785}
2786
2787static void target_core_alua_drop_tg_pt_gp(
2788 struct config_group *group,
2789 struct config_item *item)
2790{
2791 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2792 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2793
Andy Grover6708bb22011-06-08 10:36:43 -07002794 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002795 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2796 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002797 /*
2798 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2799 * -> target_core_alua_tg_pt_gp_release().
2800 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002802}
2803
2804static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2805 .make_group = &target_core_alua_create_tg_pt_gp,
2806 .drop_item = &target_core_alua_drop_tg_pt_gp,
2807};
2808
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002809TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002811/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002812
2813/* Start functions for struct config_item_type target_core_alua_cit */
2814
2815/*
2816 * target_core_alua_cit is a ConfigFS group that lives under
2817 * /sys/kernel/config/target/core/alua. There are default groups
2818 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2819 * target_core_alua_cit in target_core_init_configfs() below.
2820 */
2821static struct config_item_type target_core_alua_cit = {
2822 .ct_item_ops = NULL,
2823 .ct_attrs = NULL,
2824 .ct_owner = THIS_MODULE,
2825};
2826
2827/* End functions for struct config_item_type target_core_alua_cit */
2828
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002829/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002830
2831static struct config_group *target_core_stat_mkdir(
2832 struct config_group *group,
2833 const char *name)
2834{
2835 return ERR_PTR(-ENOSYS);
2836}
2837
2838static void target_core_stat_rmdir(
2839 struct config_group *group,
2840 struct config_item *item)
2841{
2842 return;
2843}
2844
2845static struct configfs_group_operations target_core_stat_group_ops = {
2846 .make_group = &target_core_stat_mkdir,
2847 .drop_item = &target_core_stat_rmdir,
2848};
2849
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002850TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002851
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002852/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854/* Start functions for struct config_item_type target_core_hba_cit */
2855
2856static struct config_group *target_core_make_subdev(
2857 struct config_group *group,
2858 const char *name)
2859{
2860 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 struct config_item *hba_ci = &group->cg_item;
2862 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002863 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002864 struct se_device *dev;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002865 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002867 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2868 if (ret)
2869 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002871 dev = target_alloc_device(hba, name);
2872 if (!dev)
2873 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002875 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002877 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002878 &tb->tb_dev_attrib_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002879 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
2880
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002881 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002882 &tb->tb_dev_pr_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002883 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
2884
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002885 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002886 &tb->tb_dev_wwn_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002887 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
2888 &dev->dev_group);
2889
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002890 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002891 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002892 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
2893 &dev->dev_group);
2894
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002895 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002896 "statistics", &tb->tb_dev_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002897 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
2898 &dev->dev_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002899
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002901 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002903 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002904 if (!tg_pt_gp)
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002905 goto out_free_device;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002906 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2909 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002910 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
2911 &dev->t10_alua.alua_tg_pt_gps_group);
2912
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002913 /*
2914 * Add core/$HBA/$DEV/statistics/ default groups
2915 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002916 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917
2918 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002919 return &dev->dev_group;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002920
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002921out_free_device:
2922 target_free_device(dev);
2923out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002925 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926}
2927
2928static void target_core_drop_subdev(
2929 struct config_group *group,
2930 struct config_item *item)
2931{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002932 struct config_group *dev_cg = to_config_group(item);
2933 struct se_device *dev =
2934 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002936
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002937 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002939 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002940
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002941 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
2942 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002943
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002944 /*
2945 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2946 * directly from target_core_alua_tg_pt_gp_release().
2947 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002948 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002949
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002950 configfs_remove_default_groups(dev_cg);
2951
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002953 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002955 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002956 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957}
2958
2959static struct configfs_group_operations target_core_hba_group_ops = {
2960 .make_group = target_core_make_subdev,
2961 .drop_item = target_core_drop_subdev,
2962};
2963
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002965static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002966{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002967 return container_of(to_config_group(item), struct se_hba, hba_group);
2968}
2969
2970static ssize_t target_hba_info_show(struct config_item *item, char *page)
2971{
2972 struct se_hba *hba = to_hba(item);
2973
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002974 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002975 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002976 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002977}
2978
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002979static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002981 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 int hba_mode = 0;
2983
2984 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2985 hba_mode = 1;
2986
2987 return sprintf(page, "%d\n", hba_mode);
2988}
2989
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002990static ssize_t target_hba_mode_store(struct config_item *item,
2991 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002993 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994 unsigned long mode_flag;
2995 int ret;
2996
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002997 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002998 return -EINVAL;
2999
Jingoo Han57103d72013-07-19 16:22:19 +09003000 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003002 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003003 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003004 }
3005
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003006 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07003007 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 return -EINVAL;
3009 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003011 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012 if (ret < 0)
3013 return -EINVAL;
3014 if (ret > 0)
3015 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3016 else if (ret == 0)
3017 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3018
3019 return count;
3020}
3021
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003022CONFIGFS_ATTR_RO(target_, hba_info);
3023CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003024
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003025static void target_core_hba_release(struct config_item *item)
3026{
3027 struct se_hba *hba = container_of(to_config_group(item),
3028 struct se_hba, hba_group);
3029 core_delete_hba(hba);
3030}
3031
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003033 &target_attr_hba_info,
3034 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035 NULL,
3036};
3037
3038static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003039 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003040};
3041
3042static struct config_item_type target_core_hba_cit = {
3043 .ct_item_ops = &target_core_hba_item_ops,
3044 .ct_group_ops = &target_core_hba_group_ops,
3045 .ct_attrs = target_core_hba_attrs,
3046 .ct_owner = THIS_MODULE,
3047};
3048
3049static struct config_group *target_core_call_addhbatotarget(
3050 struct config_group *group,
3051 const char *name)
3052{
3053 char *se_plugin_str, *str, *str2;
3054 struct se_hba *hba;
3055 char buf[TARGET_CORE_NAME_MAX_LEN];
3056 unsigned long plugin_dep_id = 0;
3057 int ret;
3058
3059 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003060 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003061 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003062 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3063 TARGET_CORE_NAME_MAX_LEN);
3064 return ERR_PTR(-ENAMETOOLONG);
3065 }
3066 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3067
3068 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003069 if (!str) {
3070 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003071 return ERR_PTR(-EINVAL);
3072 }
3073 se_plugin_str = buf;
3074 /*
3075 * Special case for subsystem plugins that have "_" in their names.
3076 * Namely rd_direct and rd_mcp..
3077 */
3078 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003079 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003080 *str2 = '\0'; /* Terminate for *se_plugin_str */
3081 str2++; /* Skip to start of plugin dependent ID */
3082 str = str2;
3083 } else {
3084 *str = '\0'; /* Terminate for *se_plugin_str */
3085 str++; /* Skip to start of plugin dependent ID */
3086 }
3087
Jingoo Han57103d72013-07-19 16:22:19 +09003088 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003089 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003090 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003092 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 }
3094 /*
3095 * Load up TCM subsystem plugins if they have not already been loaded.
3096 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003097 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003098
3099 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3100 if (IS_ERR(hba))
3101 return ERR_CAST(hba);
3102
3103 config_group_init_type_name(&hba->hba_group, name,
3104 &target_core_hba_cit);
3105
3106 return &hba->hba_group;
3107}
3108
3109static void target_core_call_delhbafromtarget(
3110 struct config_group *group,
3111 struct config_item *item)
3112{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003113 /*
3114 * core_delete_hba() is called from target_core_hba_item_ops->release()
3115 * -> target_core_hba_release()
3116 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003118}
3119
3120static struct configfs_group_operations target_core_group_ops = {
3121 .make_group = target_core_call_addhbatotarget,
3122 .drop_item = target_core_call_delhbafromtarget,
3123};
3124
3125static struct config_item_type target_core_cit = {
3126 .ct_item_ops = NULL,
3127 .ct_group_ops = &target_core_group_ops,
3128 .ct_attrs = NULL,
3129 .ct_owner = THIS_MODULE,
3130};
3131
3132/* Stop functions for struct config_item_type target_core_hba_cit */
3133
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003134void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003135{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003136 target_core_setup_dev_cit(tb);
3137 target_core_setup_dev_attrib_cit(tb);
3138 target_core_setup_dev_pr_cit(tb);
3139 target_core_setup_dev_wwn_cit(tb);
3140 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3141 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003142}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003143
Axel Lin54550fa2011-03-14 04:06:09 -07003144static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003145{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003146 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 struct t10_alua_lu_gp *lu_gp;
3148 int ret;
3149
Andy Grover6708bb22011-06-08 10:36:43 -07003150 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3152 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 config_group_init(&subsys->su_group);
3155 mutex_init(&subsys->su_mutex);
3156
Andy Grovere3d6f902011-07-19 08:55:10 +00003157 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003158 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003159 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 /*
3161 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3162 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3163 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003164 config_group_init_type_name(&target_core_hbagroup, "core",
3165 &target_core_cit);
3166 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003167
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 /*
3169 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3170 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003171 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3172 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3173
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 /*
3175 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3176 * groups under /sys/kernel/config/target/core/alua/
3177 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003178 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3179 &target_core_alua_lu_gps_cit);
3180 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 /*
3183 * Add core/alua/lu_gps/default_lu_gp
3184 */
3185 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003186 if (IS_ERR(lu_gp)) {
3187 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003188 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003189 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3192 &target_core_alua_lu_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003193 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3194
Andy Grovere3d6f902011-07-19 08:55:10 +00003195 default_lu_gp = lu_gp;
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003196
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003197 /*
3198 * Register the target_core_mod subsystem with configfs.
3199 */
3200 ret = configfs_register_subsystem(subsys);
3201 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003202 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203 ret, subsys->su_group.cg_item.ci_namebuf);
3204 goto out_global;
3205 }
Andy Grover6708bb22011-06-08 10:36:43 -07003206 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003207 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003208 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3209 /*
3210 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3211 */
3212 ret = rd_module_init();
3213 if (ret < 0)
3214 goto out;
3215
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003216 ret = core_dev_setup_virtual_lun0();
3217 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003218 goto out;
3219
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003220 ret = target_xcopy_setup_pt();
3221 if (ret < 0)
3222 goto out;
3223
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224 return 0;
3225
3226out:
3227 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003228 core_dev_release_virtual_lun0();
3229 rd_module_exit();
3230out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003231 if (default_lu_gp) {
3232 core_alua_free_lu_gp(default_lu_gp);
3233 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003234 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003235 release_se_kmem_caches();
3236 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237}
3238
Axel Lin54550fa2011-03-14 04:06:09 -07003239static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003240{
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003241 configfs_remove_default_groups(&alua_lu_gps_group);
3242 configfs_remove_default_groups(&alua_group);
3243 configfs_remove_default_groups(&target_core_hbagroup);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003245 /*
3246 * We expect subsys->su_group.default_groups to be released
3247 * by configfs subsystem provider logic..
3248 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003249 configfs_unregister_subsystem(&target_core_fabrics);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250
Andy Grovere3d6f902011-07-19 08:55:10 +00003251 core_alua_free_lu_gp(default_lu_gp);
3252 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003253
Andy Grover6708bb22011-06-08 10:36:43 -07003254 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003255 " Infrastructure\n");
3256
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003257 core_dev_release_virtual_lun0();
3258 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003259 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003260 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003261}
3262
3263MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3264MODULE_AUTHOR("nab@Linux-iSCSI.org");
3265MODULE_LICENSE("GPL");
3266
3267module_init(target_core_init_configfs);
3268module_exit(target_core_exit_configfs);