blob: 73c23c75528ae9cf928cc561742e9d331acb4273 [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->shutdown_session) {
389 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 return -EINVAL;
391 }
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (!tfo->close_session) {
393 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 return -EINVAL;
395 }
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!tfo->sess_get_index) {
397 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 return -EINVAL;
399 }
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!tfo->write_pending) {
401 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return -EINVAL;
403 }
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->write_pending_status) {
405 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->set_default_node_attributes) {
409 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Andy Grover6708bb22011-06-08 10:36:43 -0700412 if (!tfo->get_cmd_state) {
413 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->queue_data_in) {
417 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
Andy Grover6708bb22011-06-08 10:36:43 -0700420 if (!tfo->queue_status) {
421 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 return -EINVAL;
423 }
Andy Grover6708bb22011-06-08 10:36:43 -0700424 if (!tfo->queue_tm_rsp) {
425 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 return -EINVAL;
427 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700428 if (!tfo->aborted_task) {
429 pr_err("Missing tfo->aborted_task()\n");
430 return -EINVAL;
431 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 /*
433 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
434 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
435 * target_core_fabric_configfs.c WWN+TPG group context code.
436 */
Andy Grover6708bb22011-06-08 10:36:43 -0700437 if (!tfo->fabric_make_wwn) {
438 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 return -EINVAL;
440 }
Andy Grover6708bb22011-06-08 10:36:43 -0700441 if (!tfo->fabric_drop_wwn) {
442 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800443 return -EINVAL;
444 }
Andy Grover6708bb22011-06-08 10:36:43 -0700445 if (!tfo->fabric_make_tpg) {
446 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447 return -EINVAL;
448 }
Andy Grover6708bb22011-06-08 10:36:43 -0700449 if (!tfo->fabric_drop_tpg) {
450 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 return -EINVAL;
452 }
453
454 return 0;
455}
456
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200457int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200459 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 int ret;
461
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200462 ret = target_fabric_tf_ops_check(fo);
463 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 return ret;
465
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200466 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700467 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200468 pr_err("%s: could not allocate memory!\n", __func__);
469 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200471
472 INIT_LIST_HEAD(&tf->tf_list);
473 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200474 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200475 target_fabric_setup_cits(tf);
476
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200478 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 mutex_unlock(&g_tf_lock);
480
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200481 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200483EXPORT_SYMBOL(target_register_template);
484
485void target_unregister_template(const struct target_core_fabric_ops *fo)
486{
487 struct target_fabric_configfs *t;
488
489 mutex_lock(&g_tf_lock);
490 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200491 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200492 BUG_ON(atomic_read(&t->tf_access_cnt));
493 list_del(&t->tf_list);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700494 mutex_unlock(&g_tf_lock);
495 /*
496 * Wait for any outstanding fabric se_deve_entry->rcu_head
497 * callbacks to complete post kfree_rcu(), before allowing
498 * fabric driver unload of TFO->module to proceed.
499 */
500 rcu_barrier();
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200501 kfree(t);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700502 return;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200503 }
504 }
505 mutex_unlock(&g_tf_lock);
506}
507EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508
509/*##############################################################################
510// Stop functions called by external Target Fabrics Modules
511//############################################################################*/
512
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200513static inline struct se_dev_attrib *to_attrib(struct config_item *item)
514{
515 return container_of(to_config_group(item), struct se_dev_attrib,
516 da_group);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200517}
518
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200519/* Start functions for struct config_item_type tb_dev_attrib_cit */
520#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
521static ssize_t _name##_show(struct config_item *item, char *page) \
522{ \
523 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
524}
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200525
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200526DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
527DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
528DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
529DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
530DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
531DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
532DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
533DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
534DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
535DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
536DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
537DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
538DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
539DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
540DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
541DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
542DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
543DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
544DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
545DEF_CONFIGFS_ATTRIB_SHOW(block_size);
546DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
547DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
548DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
549DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
550DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
551DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
552DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
553DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
Jamie Pocase6f416332015-11-29 14:44:57 -0800554DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200555DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
556
557#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
558static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200559 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200560{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200561 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200562 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200563 int ret; \
564 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200565 ret = kstrtou32(page, 0, &val); \
566 if (ret < 0) \
567 return ret; \
568 da->_name = val; \
569 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200570}
571
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200572DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
573DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
574DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
575DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
576DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200577
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200578#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
579static ssize_t _name##_store(struct config_item *item, const char *page, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200580 size_t count) \
581{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200582 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200583 bool flag; \
584 int ret; \
585 \
586 ret = strtobool(page, &flag); \
587 if (ret < 0) \
588 return ret; \
589 da->_name = flag; \
590 return count; \
591}
592
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200593DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
594DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
595DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
596DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
597DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200598
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200599#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
600static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200601 size_t count) \
602{ \
603 printk_once(KERN_WARNING \
Christophe Vu-Brugier234bdbc2015-11-18 09:22:58 +0100604 "ignoring deprecated %s attribute\n", \
605 __stringify(_name)); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200606 return count; \
607}
608
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200609DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
610DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200611
612static void dev_set_t10_wwn_model_alias(struct se_device *dev)
613{
614 const char *configname;
615
616 configname = config_item_name(&dev->dev_group.cg_item);
617 if (strlen(configname) >= 16) {
618 pr_warn("dev[%p]: Backstore name '%s' is too long for "
619 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
620 configname);
621 }
622 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
623}
624
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200625static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200626 const char *page, size_t count)
627{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200628 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200629 struct se_device *dev = da->da_dev;
630 bool flag;
631 int ret;
632
633 if (dev->export_count) {
634 pr_err("dev[%p]: Unable to change model alias"
635 " while export_count is %d\n",
636 dev, dev->export_count);
637 return -EINVAL;
638 }
639
640 ret = strtobool(page, &flag);
641 if (ret < 0)
642 return ret;
643
644 if (flag) {
645 dev_set_t10_wwn_model_alias(dev);
646 } else {
647 strncpy(&dev->t10_wwn.model[0],
648 dev->transport->inquiry_prod, 16);
649 }
650 da->emulate_model_alias = flag;
651 return count;
652}
653
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200654static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200655 const char *page, size_t count)
656{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200657 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200658 bool flag;
659 int ret;
660
661 ret = strtobool(page, &flag);
662 if (ret < 0)
663 return ret;
664
665 if (flag && da->da_dev->transport->get_write_cache) {
666 pr_err("emulate_write_cache not supported for this device\n");
667 return -EINVAL;
668 }
669
670 da->emulate_write_cache = flag;
671 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
672 da->da_dev, flag);
673 return count;
674}
675
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200676static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200677 const char *page, size_t count)
678{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200679 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200680 u32 val;
681 int ret;
682
683 ret = kstrtou32(page, 0, &val);
684 if (ret < 0)
685 return ret;
686
687 if (val != 0 && val != 1 && val != 2) {
688 pr_err("Illegal value %d\n", val);
689 return -EINVAL;
690 }
691
692 if (da->da_dev->export_count) {
693 pr_err("dev[%p]: Unable to change SE Device"
694 " UA_INTRLCK_CTRL while export_count is %d\n",
695 da->da_dev, da->da_dev->export_count);
696 return -EINVAL;
697 }
698 da->emulate_ua_intlck_ctrl = val;
699 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
700 da->da_dev, val);
701 return count;
702}
703
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200704static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200705 const char *page, size_t count)
706{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200707 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200708 bool flag;
709 int ret;
710
711 ret = strtobool(page, &flag);
712 if (ret < 0)
713 return ret;
714
715 if (da->da_dev->export_count) {
716 pr_err("dev[%p]: Unable to change SE Device TAS while"
717 " export_count is %d\n",
718 da->da_dev, da->da_dev->export_count);
719 return -EINVAL;
720 }
721 da->emulate_tas = flag;
722 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
723 da->da_dev, flag ? "Enabled" : "Disabled");
724
725 return count;
726}
727
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200728static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200729 const char *page, size_t count)
730{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200731 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200732 bool flag;
733 int ret;
734
735 ret = strtobool(page, &flag);
736 if (ret < 0)
737 return ret;
738
739 /*
740 * We expect this value to be non-zero when generic Block Layer
741 * Discard supported is detected iblock_create_virtdevice().
742 */
743 if (flag && !da->max_unmap_block_desc_count) {
744 pr_err("Generic Block Discard not supported\n");
745 return -ENOSYS;
746 }
747
748 da->emulate_tpu = flag;
749 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
750 da->da_dev, flag);
751 return count;
752}
753
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200754static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200755 const char *page, size_t count)
756{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200757 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200758 bool flag;
759 int ret;
760
761 ret = strtobool(page, &flag);
762 if (ret < 0)
763 return ret;
764
765 /*
766 * We expect this value to be non-zero when generic Block Layer
767 * Discard supported is detected iblock_create_virtdevice().
768 */
769 if (flag && !da->max_unmap_block_desc_count) {
770 pr_err("Generic Block Discard not supported\n");
771 return -ENOSYS;
772 }
773
774 da->emulate_tpws = flag;
775 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
776 da->da_dev, flag);
777 return count;
778}
779
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200780static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200781 const char *page, size_t count)
782{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200783 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200784 int old_prot = da->pi_prot_type, ret;
785 struct se_device *dev = da->da_dev;
786 u32 flag;
787
788 ret = kstrtou32(page, 0, &flag);
789 if (ret < 0)
790 return ret;
791
792 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
793 pr_err("Illegal value %d for pi_prot_type\n", flag);
794 return -EINVAL;
795 }
796 if (flag == 2) {
797 pr_err("DIF TYPE2 protection currently not supported\n");
798 return -ENOSYS;
799 }
800 if (da->hw_pi_prot_type) {
801 pr_warn("DIF protection enabled on underlying hardware,"
802 " ignoring\n");
803 return count;
804 }
805 if (!dev->transport->init_prot || !dev->transport->free_prot) {
806 /* 0 is only allowed value for non-supporting backends */
807 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700808 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200809
810 pr_err("DIF protection not supported by backend: %s\n",
811 dev->transport->name);
812 return -ENOSYS;
813 }
814 if (!(dev->dev_flags & DF_CONFIGURED)) {
815 pr_err("DIF protection requires device to be configured\n");
816 return -ENODEV;
817 }
818 if (dev->export_count) {
819 pr_err("dev[%p]: Unable to change SE Device PROT type while"
820 " export_count is %d\n", dev, dev->export_count);
821 return -EINVAL;
822 }
823
824 da->pi_prot_type = flag;
825
826 if (flag && !old_prot) {
827 ret = dev->transport->init_prot(dev);
828 if (ret) {
829 da->pi_prot_type = old_prot;
830 return ret;
831 }
832
833 } else if (!flag && old_prot) {
834 dev->transport->free_prot(dev);
835 }
836
837 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
838 return count;
839}
840
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200841static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200842 const char *page, size_t count)
843{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200844 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200845 struct se_device *dev = da->da_dev;
846 bool flag;
847 int ret;
848
849 ret = strtobool(page, &flag);
850 if (ret < 0)
851 return ret;
852
853 if (!flag)
854 return count;
855
856 if (!dev->transport->format_prot) {
857 pr_err("DIF protection format not supported by backend %s\n",
858 dev->transport->name);
859 return -ENOSYS;
860 }
861 if (!(dev->dev_flags & DF_CONFIGURED)) {
862 pr_err("DIF protection format requires device to be configured\n");
863 return -ENODEV;
864 }
865 if (dev->export_count) {
866 pr_err("dev[%p]: Unable to format SE Device PROT type while"
867 " export_count is %d\n", dev, dev->export_count);
868 return -EINVAL;
869 }
870
871 ret = dev->transport->format_prot(dev);
872 if (ret)
873 return ret;
874
875 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
876 return count;
877}
878
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200879static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200880 const char *page, size_t count)
881{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200882 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200883 bool flag;
884 int ret;
885
886 ret = strtobool(page, &flag);
887 if (ret < 0)
888 return ret;
889 if (da->da_dev->export_count) {
890 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
891 " export_count is %d\n",
892 da->da_dev, da->da_dev->export_count);
893 return -EINVAL;
894 }
895
896 da->force_pr_aptpl = flag;
897 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
898 return count;
899}
900
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200901static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200902 const char *page, size_t count)
903{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200904 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200905 bool flag;
906 int ret;
907
908 ret = strtobool(page, &flag);
909 if (ret < 0)
910 return ret;
911
912 if (flag != 0) {
913 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
914 " reordering not implemented\n", da->da_dev);
915 return -ENOSYS;
916 }
917 da->emulate_rest_reord = flag;
918 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
919 da->da_dev, flag);
920 return count;
921}
922
Jamie Pocase6f416332015-11-29 14:44:57 -0800923static ssize_t unmap_zeroes_data_store(struct config_item *item,
924 const char *page, size_t count)
925{
926 struct se_dev_attrib *da = to_attrib(item);
927 bool flag;
928 int ret;
929
930 ret = strtobool(page, &flag);
931 if (ret < 0)
932 return ret;
933
934 if (da->da_dev->export_count) {
935 pr_err("dev[%p]: Unable to change SE Device"
936 " unmap_zeroes_data while export_count is %d\n",
937 da->da_dev, da->da_dev->export_count);
938 return -EINVAL;
939 }
940 /*
941 * We expect this value to be non-zero when generic Block Layer
942 * Discard supported is detected iblock_configure_device().
943 */
944 if (flag && !da->max_unmap_block_desc_count) {
945 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
946 " because max_unmap_block_desc_count is zero\n",
947 da->da_dev);
Bart Van Assche9b3118c2016-01-05 14:44:21 +0100948 return -ENOSYS;
Jamie Pocase6f416332015-11-29 14:44:57 -0800949 }
950 da->unmap_zeroes_data = flag;
951 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
952 da->da_dev, flag);
Nicholas Bellinger2e498f22016-02-10 20:34:56 -0800953 return count;
Jamie Pocase6f416332015-11-29 14:44:57 -0800954}
955
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200956/*
957 * Note, this can only be called on unexported SE Device Object.
958 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200959static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200960 const char *page, size_t count)
961{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200962 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200963 struct se_device *dev = da->da_dev;
964 u32 val;
965 int ret;
966
967 ret = kstrtou32(page, 0, &val);
968 if (ret < 0)
969 return ret;
970
971 if (dev->export_count) {
972 pr_err("dev[%p]: Unable to change SE Device TCQ while"
973 " export_count is %d\n",
974 dev, dev->export_count);
975 return -EINVAL;
976 }
977 if (!val) {
978 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
979 return -EINVAL;
980 }
981
982 if (val > dev->dev_attrib.queue_depth) {
983 if (val > dev->dev_attrib.hw_queue_depth) {
984 pr_err("dev[%p]: Passed queue_depth:"
985 " %u exceeds TCM/SE_Device MAX"
986 " TCQ: %u\n", dev, val,
987 dev->dev_attrib.hw_queue_depth);
988 return -EINVAL;
989 }
990 }
991 da->queue_depth = dev->queue_depth = val;
992 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
993 return count;
994}
995
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200996static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200997 const char *page, size_t count)
998{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200999 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +02001000 u32 val;
1001 int ret;
1002
1003 ret = kstrtou32(page, 0, &val);
1004 if (ret < 0)
1005 return ret;
1006
1007 if (da->da_dev->export_count) {
1008 pr_err("dev[%p]: Unable to change SE Device"
1009 " optimal_sectors while export_count is %d\n",
1010 da->da_dev, da->da_dev->export_count);
1011 return -EINVAL;
1012 }
1013 if (val > da->hw_max_sectors) {
1014 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1015 " greater than hw_max_sectors: %u\n",
1016 da->da_dev, val, da->hw_max_sectors);
1017 return -EINVAL;
1018 }
1019
1020 da->optimal_sectors = val;
1021 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1022 da->da_dev, val);
1023 return count;
1024}
1025
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001026static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +02001027 const char *page, size_t count)
1028{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001029 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +02001030 u32 val;
1031 int ret;
1032
1033 ret = kstrtou32(page, 0, &val);
1034 if (ret < 0)
1035 return ret;
1036
1037 if (da->da_dev->export_count) {
1038 pr_err("dev[%p]: Unable to change SE Device block_size"
1039 " while export_count is %d\n",
1040 da->da_dev, da->da_dev->export_count);
1041 return -EINVAL;
1042 }
1043
1044 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1045 pr_err("dev[%p]: Illegal value for block_device: %u"
1046 " for SE device, must be 512, 1024, 2048 or 4096\n",
1047 da->da_dev, val);
1048 return -EINVAL;
1049 }
1050
1051 da->block_size = val;
1052 if (da->max_bytes_per_io)
1053 da->hw_max_sectors = da->max_bytes_per_io / val;
1054
1055 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1056 da->da_dev, val);
1057 return count;
1058}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001059
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001060CONFIGFS_ATTR(, emulate_model_alias);
1061CONFIGFS_ATTR(, emulate_dpo);
1062CONFIGFS_ATTR(, emulate_fua_write);
1063CONFIGFS_ATTR(, emulate_fua_read);
1064CONFIGFS_ATTR(, emulate_write_cache);
1065CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1066CONFIGFS_ATTR(, emulate_tas);
1067CONFIGFS_ATTR(, emulate_tpu);
1068CONFIGFS_ATTR(, emulate_tpws);
1069CONFIGFS_ATTR(, emulate_caw);
1070CONFIGFS_ATTR(, emulate_3pc);
1071CONFIGFS_ATTR(, pi_prot_type);
1072CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1073CONFIGFS_ATTR(, pi_prot_format);
1074CONFIGFS_ATTR(, enforce_pr_isids);
1075CONFIGFS_ATTR(, is_nonrot);
1076CONFIGFS_ATTR(, emulate_rest_reord);
1077CONFIGFS_ATTR(, force_pr_aptpl);
1078CONFIGFS_ATTR_RO(, hw_block_size);
1079CONFIGFS_ATTR(, block_size);
1080CONFIGFS_ATTR_RO(, hw_max_sectors);
1081CONFIGFS_ATTR(, optimal_sectors);
1082CONFIGFS_ATTR_RO(, hw_queue_depth);
1083CONFIGFS_ATTR(, queue_depth);
1084CONFIGFS_ATTR(, max_unmap_lba_count);
1085CONFIGFS_ATTR(, max_unmap_block_desc_count);
1086CONFIGFS_ATTR(, unmap_granularity);
1087CONFIGFS_ATTR(, unmap_granularity_alignment);
Jamie Pocase6f416332015-11-29 14:44:57 -08001088CONFIGFS_ATTR(, unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001089CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001091/*
1092 * dev_attrib attributes for devices using the target core SBC/SPC
1093 * interpreter. Any backend using spc_parse_cdb should be using
1094 * these.
1095 */
1096struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001097 &attr_emulate_model_alias,
1098 &attr_emulate_dpo,
1099 &attr_emulate_fua_write,
1100 &attr_emulate_fua_read,
1101 &attr_emulate_write_cache,
1102 &attr_emulate_ua_intlck_ctrl,
1103 &attr_emulate_tas,
1104 &attr_emulate_tpu,
1105 &attr_emulate_tpws,
1106 &attr_emulate_caw,
1107 &attr_emulate_3pc,
1108 &attr_pi_prot_type,
1109 &attr_hw_pi_prot_type,
1110 &attr_pi_prot_format,
1111 &attr_enforce_pr_isids,
1112 &attr_is_nonrot,
1113 &attr_emulate_rest_reord,
1114 &attr_force_pr_aptpl,
1115 &attr_hw_block_size,
1116 &attr_block_size,
1117 &attr_hw_max_sectors,
1118 &attr_optimal_sectors,
1119 &attr_hw_queue_depth,
1120 &attr_queue_depth,
1121 &attr_max_unmap_lba_count,
1122 &attr_max_unmap_block_desc_count,
1123 &attr_unmap_granularity,
1124 &attr_unmap_granularity_alignment,
Jamie Pocase6f416332015-11-29 14:44:57 -08001125 &attr_unmap_zeroes_data,
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001126 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001127 NULL,
1128};
1129EXPORT_SYMBOL(sbc_attrib_attrs);
1130
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001131/*
1132 * Minimal dev_attrib attributes for devices passing through CDBs.
1133 * In this case we only provide a few read-only attributes for
1134 * backwards compatibility.
1135 */
1136struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001137 &attr_hw_pi_prot_type,
1138 &attr_hw_block_size,
1139 &attr_hw_max_sectors,
1140 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001141 NULL,
1142};
1143EXPORT_SYMBOL(passthrough_attrib_attrs);
1144
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001145TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001147/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001149/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001151static struct t10_wwn *to_t10_wwn(struct config_item *item)
1152{
1153 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1154}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155
1156/*
1157 * VPD page 0x80 Unit serial
1158 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001159static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1160 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001163 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164}
1165
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001166static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1167 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001169 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001170 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1172
1173 /*
1174 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1175 * from the struct scsi_device level firmware, do not allow
1176 * VPD Unit Serial to be emulated.
1177 *
1178 * Note this struct scsi_device could also be emulating VPD
1179 * information from its drivers/scsi LLD. But for now we assume
1180 * it is doing 'the right thing' wrt a world wide unique
1181 * VPD Unit Serial Number that OS dependent multipath can depend on.
1182 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001183 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001184 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185 " Unit Serial, ignoring request\n");
1186 return -EOPNOTSUPP;
1187 }
1188
Dan Carpenter60d645a2011-06-15 10:03:05 -07001189 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001190 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1192 return -EOVERFLOW;
1193 }
1194 /*
1195 * Check to see if any active $FABRIC_MOD exports exist. If they
1196 * do exist, fail here as changing this information on the fly
1197 * (underneath the initiator side OS dependent multipath code)
1198 * could cause negative effects.
1199 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001200 if (dev->export_count) {
1201 pr_err("Unable to set VPD Unit Serial while"
1202 " active %d $FABRIC_MOD exports exist\n",
1203 dev->export_count);
1204 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001206
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207 /*
1208 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1209 *
1210 * Also, strip any newline added from the userspace
1211 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1212 */
1213 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1214 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001215 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001217 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218
Andy Grover6708bb22011-06-08 10:36:43 -07001219 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001220 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221
1222 return count;
1223}
1224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225/*
1226 * VPD page 0x83 Protocol Identifier
1227 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001228static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1229 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001231 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 struct t10_vpd *vpd;
1233 unsigned char buf[VPD_TMP_BUF_SIZE];
1234 ssize_t len = 0;
1235
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 memset(buf, 0, VPD_TMP_BUF_SIZE);
1237
1238 spin_lock(&t10_wwn->t10_vpd_lock);
1239 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001240 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241 continue;
1242
1243 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1244
Andy Grover6708bb22011-06-08 10:36:43 -07001245 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246 break;
1247
1248 len += sprintf(page+len, "%s", buf);
1249 }
1250 spin_unlock(&t10_wwn->t10_vpd_lock);
1251
1252 return len;
1253}
1254
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255/*
1256 * Generic wrapper for dumping VPD identifiers by association.
1257 */
1258#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001259static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1260 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001262 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1263 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001264 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1265 ssize_t len = 0; \
1266 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 spin_lock(&t10_wwn->t10_vpd_lock); \
1268 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1269 if (vpd->association != _assoc) \
1270 continue; \
1271 \
1272 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1273 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001274 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 break; \
1276 len += sprintf(page+len, "%s", buf); \
1277 \
1278 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1279 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001280 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281 break; \
1282 len += sprintf(page+len, "%s", buf); \
1283 \
1284 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1285 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001286 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 break; \
1288 len += sprintf(page+len, "%s", buf); \
1289 } \
1290 spin_unlock(&t10_wwn->t10_vpd_lock); \
1291 \
1292 return len; \
1293}
1294
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001295/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001297/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001299/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1301
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001302CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1303CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1304CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1305CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1306CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307
1308static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001309 &target_wwn_attr_vpd_unit_serial,
1310 &target_wwn_attr_vpd_protocol_identifier,
1311 &target_wwn_attr_vpd_assoc_logical_unit,
1312 &target_wwn_attr_vpd_assoc_target_port,
1313 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 NULL,
1315};
1316
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001317TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001318
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001319/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001321/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001322
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001323static struct se_device *pr_to_dev(struct config_item *item)
1324{
1325 return container_of(to_config_group(item), struct se_device,
1326 dev_pr_group);
1327}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001328
Christoph Hellwigd977f432012-10-10 17:37:15 -04001329static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1330 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331{
1332 struct se_node_acl *se_nacl;
1333 struct t10_pr_registration *pr_reg;
1334 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335
1336 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1337
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001339 if (!pr_reg)
1340 return sprintf(page, "No SPC-3 Reservation holder\n");
1341
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001342 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001343 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001344
Christoph Hellwigd977f432012-10-10 17:37:15 -04001345 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001346 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001347 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348}
1349
Christoph Hellwigd977f432012-10-10 17:37:15 -04001350static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1351 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001352{
1353 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001354 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001357 if (se_nacl) {
1358 len = sprintf(page,
1359 "SPC-2 Reservation: %s Initiator: %s\n",
1360 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1361 se_nacl->initiatorname);
1362 } else {
1363 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001365 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366}
1367
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001368static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001369{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001370 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001371 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001372
Andy Grovera3541702015-05-19 14:44:41 -07001373 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001374 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375
Christoph Hellwigd977f432012-10-10 17:37:15 -04001376 spin_lock(&dev->dev_reservation_lock);
1377 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1378 ret = target_core_dev_pr_show_spc2_res(dev, page);
1379 else
1380 ret = target_core_dev_pr_show_spc3_res(dev, page);
1381 spin_unlock(&dev->dev_reservation_lock);
1382 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383}
1384
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001385static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1386 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001388 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 ssize_t len = 0;
1390
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001391 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001392 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001394 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395 len = sprintf(page, "SPC-3 Reservation: All Target"
1396 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001397 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 len = sprintf(page, "SPC-3 Reservation: Single"
1399 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001400 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001401
Christoph Hellwigd977f432012-10-10 17:37:15 -04001402 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001403 return len;
1404}
1405
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001406static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1407 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001409 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001410}
1411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001413static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1414 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001416 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 struct se_portal_group *se_tpg;
1419 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001420 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421 ssize_t len = 0;
1422
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 spin_lock(&dev->dev_reservation_lock);
1424 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001425 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001427 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001429
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430 se_nacl = pr_reg->pr_reg_nacl;
1431 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001432 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433
1434 len += sprintf(page+len, "SPC-3 Reservation: %s"
1435 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1436 tfo->tpg_get_wwn(se_tpg));
1437 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001438 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001439 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001441 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442
Christoph Hellwigd977f432012-10-10 17:37:15 -04001443out_unlock:
1444 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001445 return len;
1446}
1447
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001448
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001449static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1450 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001451{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001452 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001453 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001454 struct t10_pr_registration *pr_reg;
1455 unsigned char buf[384];
1456 char i_buf[PR_REG_ISID_ID_LEN];
1457 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001458 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001459
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1461
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001462 spin_lock(&dev->t10_pr.registration_lock);
1463 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001464 pr_reg_list) {
1465
1466 memset(buf, 0, 384);
1467 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1468 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001469 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470 PR_REG_ISID_ID_LEN);
1471 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1472 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001473 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474 pr_reg->pr_res_generation);
1475
Andy Grover6708bb22011-06-08 10:36:43 -07001476 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 break;
1478
1479 len += sprintf(page+len, "%s", buf);
1480 reg_count++;
1481 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001482 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483
Andy Grover6708bb22011-06-08 10:36:43 -07001484 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 len += sprintf(page+len, "None\n");
1486
1487 return len;
1488}
1489
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001490static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001492 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001493 struct t10_pr_registration *pr_reg;
1494 ssize_t len = 0;
1495
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496 spin_lock(&dev->dev_reservation_lock);
1497 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001498 if (pr_reg) {
1499 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1500 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1501 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001503 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504
Christoph Hellwigd977f432012-10-10 17:37:15 -04001505 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001506 return len;
1507}
1508
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001509static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001510{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001511 struct se_device *dev = pr_to_dev(item);
1512
Andy Grovera3541702015-05-19 14:44:41 -07001513 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001514 return sprintf(page, "SPC_PASSTHROUGH\n");
1515 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1516 return sprintf(page, "SPC2_RESERVATIONS\n");
1517 else
1518 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519}
1520
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001521static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1522 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001524 struct se_device *dev = pr_to_dev(item);
1525
Andy Grovera3541702015-05-19 14:44:41 -07001526 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 return 0;
1528
1529 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001530 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531}
1532
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001533static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1534 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001536 struct se_device *dev = pr_to_dev(item);
1537
Andy Grovera3541702015-05-19 14:44:41 -07001538 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 return 0;
1540
1541 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1542}
1543
1544enum {
1545 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1546 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1547 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1548 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1549};
1550
1551static match_table_t tokens = {
1552 {Opt_initiator_fabric, "initiator_fabric=%s"},
1553 {Opt_initiator_node, "initiator_node=%s"},
1554 {Opt_initiator_sid, "initiator_sid=%s"},
1555 {Opt_sa_res_key, "sa_res_key=%s"},
1556 {Opt_res_holder, "res_holder=%d"},
1557 {Opt_res_type, "res_type=%d"},
1558 {Opt_res_scope, "res_scope=%d"},
1559 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001560 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 {Opt_target_fabric, "target_fabric=%s"},
1562 {Opt_target_node, "target_node=%s"},
1563 {Opt_tpgt, "tpgt=%d"},
1564 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001565 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001566 {Opt_err, NULL}
1567};
1568
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001569static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1570 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001571{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001572 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001573 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1574 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001575 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001576 substring_t args[MAX_OPT_ARGS];
1577 unsigned long long tmp_ll;
1578 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001579 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001580 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001581 u16 tpgt = 0;
1582 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583
Andy Grovera3541702015-05-19 14:44:41 -07001584 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001585 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001586 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001587 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001589 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001590 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 " active fabric exports exist\n");
1592 return -EINVAL;
1593 }
1594
1595 opts = kstrdup(page, GFP_KERNEL);
1596 if (!opts)
1597 return -ENOMEM;
1598
1599 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001600 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001601 if (!*ptr)
1602 continue;
1603
1604 token = match_token(ptr, tokens, args);
1605 switch (token) {
1606 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001607 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001608 if (!i_fabric) {
1609 ret = -ENOMEM;
1610 goto out;
1611 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001612 break;
1613 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001614 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001615 if (!i_port) {
1616 ret = -ENOMEM;
1617 goto out;
1618 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001619 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001620 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1622 PR_APTPL_MAX_IPORT_LEN);
1623 ret = -EINVAL;
1624 break;
1625 }
1626 break;
1627 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001628 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001629 if (!isid) {
1630 ret = -ENOMEM;
1631 goto out;
1632 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001633 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001634 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635 "= exceeds PR_REG_ISID_LEN: %d\n",
1636 PR_REG_ISID_LEN);
1637 ret = -EINVAL;
1638 break;
1639 }
1640 break;
1641 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001642 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001644 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 goto out;
1646 }
1647 sa_res_key = (u64)tmp_ll;
1648 break;
1649 /*
1650 * PR APTPL Metadata for Reservation
1651 */
1652 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001653 ret = match_int(args, &arg);
1654 if (ret)
1655 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001656 res_holder = arg;
1657 break;
1658 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001659 ret = match_int(args, &arg);
1660 if (ret)
1661 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662 type = (u8)arg;
1663 break;
1664 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001665 ret = match_int(args, &arg);
1666 if (ret)
1667 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 break;
1669 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001670 ret = match_int(args, &arg);
1671 if (ret)
1672 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 all_tg_pt = (int)arg;
1674 break;
1675 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001676 ret = match_int(args, &arg);
1677 if (ret)
1678 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001679 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001680 break;
1681 /*
1682 * PR APTPL Metadata for Target Port
1683 */
1684 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001685 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001686 if (!t_fabric) {
1687 ret = -ENOMEM;
1688 goto out;
1689 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 break;
1691 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001692 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001693 if (!t_port) {
1694 ret = -ENOMEM;
1695 goto out;
1696 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001697 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001698 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1700 PR_APTPL_MAX_TPORT_LEN);
1701 ret = -EINVAL;
1702 break;
1703 }
1704 break;
1705 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001706 ret = match_int(args, &arg);
1707 if (ret)
1708 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 tpgt = (u16)arg;
1710 break;
1711 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001712 ret = match_int(args, &arg);
1713 if (ret)
1714 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715 break;
1716 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001717 ret = match_int(args, &arg);
1718 if (ret)
1719 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001720 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 break;
1722 default:
1723 break;
1724 }
1725 }
1726
Andy Grover6708bb22011-06-08 10:36:43 -07001727 if (!i_port || !t_port || !sa_res_key) {
1728 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 ret = -EINVAL;
1730 goto out;
1731 }
1732
1733 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001734 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001735 " holder\n", type);
1736 ret = -EINVAL;
1737 goto out;
1738 }
1739
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001740 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1742 res_holder, all_tg_pt, type);
1743out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001744 kfree(i_fabric);
1745 kfree(i_port);
1746 kfree(isid);
1747 kfree(t_fabric);
1748 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001749 kfree(orig);
1750 return (ret == 0) ? count : ret;
1751}
1752
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001754CONFIGFS_ATTR_RO(target_pr_, res_holder);
1755CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1756CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1757CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1758CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1759CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1760CONFIGFS_ATTR_RO(target_pr_, res_type);
1761CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1762CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001763
1764static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001765 &target_pr_attr_res_holder,
1766 &target_pr_attr_res_pr_all_tgt_pts,
1767 &target_pr_attr_res_pr_generation,
1768 &target_pr_attr_res_pr_holder_tg_port,
1769 &target_pr_attr_res_pr_registered_i_pts,
1770 &target_pr_attr_res_pr_type,
1771 &target_pr_attr_res_type,
1772 &target_pr_attr_res_aptpl_active,
1773 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774 NULL,
1775};
1776
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001777TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001778
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001779/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001780
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001781/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001783static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001784{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001785 return container_of(to_config_group(item), struct se_device, dev_group);
1786}
1787
1788static ssize_t target_dev_info_show(struct config_item *item, char *page)
1789{
1790 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001791 int bl = 0;
1792 ssize_t read_bytes = 0;
1793
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001794 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001795 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001796 read_bytes += dev->transport->show_configfs_dev_params(dev,
1797 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001798 return read_bytes;
1799}
1800
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001801static ssize_t target_dev_control_store(struct config_item *item,
1802 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001803{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001804 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001805
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001806 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001807}
1808
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001809static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001811 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001813 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001814 return 0;
1815
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001816 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817}
1818
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001819static ssize_t target_dev_alias_store(struct config_item *item,
1820 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001822 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001823 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001824 ssize_t read_bytes;
1825
1826 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001827 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001828 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1829 SE_DEV_ALIAS_LEN-1);
1830 return -EINVAL;
1831 }
1832
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001833 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001834 if (!read_bytes)
1835 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001836 if (dev->dev_alias[read_bytes - 1] == '\n')
1837 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001838
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001839 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001840
Andy Grover6708bb22011-06-08 10:36:43 -07001841 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001843 config_item_name(&dev->dev_group.cg_item),
1844 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845
1846 return read_bytes;
1847}
1848
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001849static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001851 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001852
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001853 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 return 0;
1855
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001856 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857}
1858
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001859static ssize_t target_dev_udev_path_store(struct config_item *item,
1860 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001861{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001862 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001863 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001864 ssize_t read_bytes;
1865
1866 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001867 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1869 SE_UDEV_PATH_LEN-1);
1870 return -EINVAL;
1871 }
1872
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001873 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001874 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001875 if (!read_bytes)
1876 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001877 if (dev->udev_path[read_bytes - 1] == '\n')
1878 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001879
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001880 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001881
Andy Grover6708bb22011-06-08 10:36:43 -07001882 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001884 config_item_name(&dev->dev_group.cg_item),
1885 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886
1887 return read_bytes;
1888}
1889
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001890static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001891{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001892 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001893
1894 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1895}
1896
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001897static ssize_t target_dev_enable_store(struct config_item *item,
1898 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001900 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001902 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903
1904 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001905 if (!ptr) {
1906 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001907 " is \"1\"\n");
1908 return -EINVAL;
1909 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001910
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001911 ret = target_configure_device(dev);
1912 if (ret)
1913 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001914 return count;
1915}
1916
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001917static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001919 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 struct config_item *lu_ci;
1921 struct t10_alua_lu_gp *lu_gp;
1922 struct t10_alua_lu_gp_member *lu_gp_mem;
1923 ssize_t len = 0;
1924
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001926 if (!lu_gp_mem)
1927 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928
1929 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1930 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001931 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 lu_ci = &lu_gp->lu_gp_group.cg_item;
1933 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1934 config_item_name(lu_ci), lu_gp->lu_gp_id);
1935 }
1936 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1937
1938 return len;
1939}
1940
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001941static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1942 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001943{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001944 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001945 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1947 struct t10_alua_lu_gp_member *lu_gp_mem;
1948 unsigned char buf[LU_GROUP_NAME_BUF];
1949 int move = 0;
1950
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001951 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1952 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001953 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001954
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001956 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001957 return -EINVAL;
1958 }
1959 memset(buf, 0, LU_GROUP_NAME_BUF);
1960 memcpy(buf, page, count);
1961 /*
1962 * Any ALUA logical unit alias besides "NULL" means we will be
1963 * making a new group association.
1964 */
1965 if (strcmp(strstrip(buf), "NULL")) {
1966 /*
1967 * core_alua_get_lu_gp_by_name() will increment reference to
1968 * struct t10_alua_lu_gp. This reference is released with
1969 * core_alua_get_lu_gp_by_name below().
1970 */
1971 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001972 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001973 return -ENODEV;
1974 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975
1976 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1977 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001978 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 /*
1980 * Clearing an existing lu_gp association, and replacing
1981 * with NULL
1982 */
Andy Grover6708bb22011-06-08 10:36:43 -07001983 if (!lu_gp_new) {
1984 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001985 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1986 " %hu\n",
1987 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001988 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989 config_item_name(&lu_gp->lu_gp_group.cg_item),
1990 lu_gp->lu_gp_id);
1991
1992 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1993 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1994
1995 return count;
1996 }
1997 /*
1998 * Removing existing association of lu_gp_mem with lu_gp
1999 */
2000 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2001 move = 1;
2002 }
2003 /*
2004 * Associate lu_gp_mem with lu_gp_new.
2005 */
2006 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2007 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2008
Andy Grover6708bb22011-06-08 10:36:43 -07002009 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010 " core/alua/lu_gps/%s, ID: %hu\n",
2011 (move) ? "Moving" : "Adding",
2012 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002013 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2015 lu_gp_new->lu_gp_id);
2016
2017 core_alua_put_lu_gp_from_name(lu_gp_new);
2018 return count;
2019}
2020
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002021static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002022{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002023 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002024 struct t10_alua_lba_map *map;
2025 struct t10_alua_lba_map_member *mem;
2026 char *b = page;
2027 int bl = 0;
2028 char state;
2029
2030 spin_lock(&dev->t10_alua.lba_map_lock);
2031 if (!list_empty(&dev->t10_alua.lba_map_list))
2032 bl += sprintf(b + bl, "%u %u\n",
2033 dev->t10_alua.lba_map_segment_size,
2034 dev->t10_alua.lba_map_segment_multiplier);
2035 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2036 bl += sprintf(b + bl, "%llu %llu",
2037 map->lba_map_first_lba, map->lba_map_last_lba);
2038 list_for_each_entry(mem, &map->lba_map_mem_list,
2039 lba_map_mem_list) {
2040 switch (mem->lba_map_mem_alua_state) {
2041 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2042 state = 'O';
2043 break;
2044 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2045 state = 'A';
2046 break;
2047 case ALUA_ACCESS_STATE_STANDBY:
2048 state = 'S';
2049 break;
2050 case ALUA_ACCESS_STATE_UNAVAILABLE:
2051 state = 'U';
2052 break;
2053 default:
2054 state = '.';
2055 break;
2056 }
2057 bl += sprintf(b + bl, " %d:%c",
2058 mem->lba_map_mem_alua_pg_id, state);
2059 }
2060 bl += sprintf(b + bl, "\n");
2061 }
2062 spin_unlock(&dev->t10_alua.lba_map_lock);
2063 return bl;
2064}
2065
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002066static ssize_t target_dev_lba_map_store(struct config_item *item,
2067 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002068{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002069 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002070 struct t10_alua_lba_map *lba_map = NULL;
2071 struct list_head lba_list;
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002072 char *map_entries, *orig, *ptr;
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002073 char state;
2074 int pg_num = -1, pg;
2075 int ret = 0, num = 0, pg_id, alua_state;
2076 unsigned long start_lba = -1, end_lba = -1;
2077 unsigned long segment_size = -1, segment_mult = -1;
2078
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002079 orig = map_entries = kstrdup(page, GFP_KERNEL);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002080 if (!map_entries)
2081 return -ENOMEM;
2082
2083 INIT_LIST_HEAD(&lba_list);
2084 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2085 if (!*ptr)
2086 continue;
2087
2088 if (num == 0) {
2089 if (sscanf(ptr, "%lu %lu\n",
2090 &segment_size, &segment_mult) != 2) {
2091 pr_err("Invalid line %d\n", num);
2092 ret = -EINVAL;
2093 break;
2094 }
2095 num++;
2096 continue;
2097 }
2098 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2099 pr_err("Invalid line %d\n", num);
2100 ret = -EINVAL;
2101 break;
2102 }
2103 ptr = strchr(ptr, ' ');
2104 if (!ptr) {
2105 pr_err("Invalid line %d, missing end lba\n", num);
2106 ret = -EINVAL;
2107 break;
2108 }
2109 ptr++;
2110 ptr = strchr(ptr, ' ');
2111 if (!ptr) {
2112 pr_err("Invalid line %d, missing state definitions\n",
2113 num);
2114 ret = -EINVAL;
2115 break;
2116 }
2117 ptr++;
2118 lba_map = core_alua_allocate_lba_map(&lba_list,
2119 start_lba, end_lba);
2120 if (IS_ERR(lba_map)) {
2121 ret = PTR_ERR(lba_map);
2122 break;
2123 }
2124 pg = 0;
2125 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2126 switch (state) {
2127 case 'O':
2128 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2129 break;
2130 case 'A':
2131 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2132 break;
2133 case 'S':
2134 alua_state = ALUA_ACCESS_STATE_STANDBY;
2135 break;
2136 case 'U':
2137 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2138 break;
2139 default:
2140 pr_err("Invalid ALUA state '%c'\n", state);
2141 ret = -EINVAL;
2142 goto out;
2143 }
2144
2145 ret = core_alua_allocate_lba_map_mem(lba_map,
2146 pg_id, alua_state);
2147 if (ret) {
2148 pr_err("Invalid target descriptor %d:%c "
2149 "at line %d\n",
2150 pg_id, state, num);
2151 break;
2152 }
2153 pg++;
2154 ptr = strchr(ptr, ' ');
2155 if (ptr)
2156 ptr++;
2157 else
2158 break;
2159 }
2160 if (pg_num == -1)
2161 pg_num = pg;
2162 else if (pg != pg_num) {
2163 pr_err("Only %d from %d port groups definitions "
2164 "at line %d\n", pg, pg_num, num);
2165 ret = -EINVAL;
2166 break;
2167 }
2168 num++;
2169 }
2170out:
2171 if (ret) {
2172 core_alua_free_lba_map(&lba_list);
2173 count = ret;
2174 } else
2175 core_alua_set_lba_map(dev, &lba_list,
2176 segment_size, segment_mult);
Bart Van Asschef0a8afe2016-01-05 14:47:17 +01002177 kfree(orig);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002178 return count;
2179}
2180
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002181CONFIGFS_ATTR_RO(target_dev_, info);
2182CONFIGFS_ATTR_WO(target_dev_, control);
2183CONFIGFS_ATTR(target_dev_, alias);
2184CONFIGFS_ATTR(target_dev_, udev_path);
2185CONFIGFS_ATTR(target_dev_, enable);
2186CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2187CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002188
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002189static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002190 &target_dev_attr_info,
2191 &target_dev_attr_control,
2192 &target_dev_attr_alias,
2193 &target_dev_attr_udev_path,
2194 &target_dev_attr_enable,
2195 &target_dev_attr_alua_lu_gp,
2196 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197 NULL,
2198};
2199
2200static void target_core_dev_release(struct config_item *item)
2201{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002202 struct config_group *dev_cg = to_config_group(item);
2203 struct se_device *dev =
2204 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002205
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002206 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207}
2208
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209static struct configfs_item_operations target_core_dev_item_ops = {
2210 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002211};
2212
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002213TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002214
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002215/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216
2217/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2218
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002219static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002220{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002221 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2222 lu_gp_group);
2223}
2224
2225static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2226{
2227 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2228
Andy Grover6708bb22011-06-08 10:36:43 -07002229 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2232}
2233
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002234static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2235 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002236{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002237 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002238 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2239 unsigned long lu_gp_id;
2240 int ret;
2241
Jingoo Han57103d72013-07-19 16:22:19 +09002242 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002244 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002245 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002246 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247 }
2248 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002249 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250 " 0x0000ffff\n", lu_gp_id);
2251 return -EINVAL;
2252 }
2253
2254 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2255 if (ret < 0)
2256 return -EINVAL;
2257
Andy Grover6708bb22011-06-08 10:36:43 -07002258 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002259 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2260 config_item_name(&alua_lu_gp_cg->cg_item),
2261 lu_gp->lu_gp_id);
2262
2263 return count;
2264}
2265
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002266static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002268 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002269 struct se_device *dev;
2270 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002271 struct t10_alua_lu_gp_member *lu_gp_mem;
2272 ssize_t len = 0, cur_len;
2273 unsigned char buf[LU_GROUP_NAME_BUF];
2274
2275 memset(buf, 0, LU_GROUP_NAME_BUF);
2276
2277 spin_lock(&lu_gp->lu_gp_lock);
2278 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2279 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002280 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281
2282 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2283 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002284 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285 cur_len++; /* Extra byte for NULL terminator */
2286
2287 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002288 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289 "_members buffer\n");
2290 break;
2291 }
2292 memcpy(page+len, buf, cur_len);
2293 len += cur_len;
2294 }
2295 spin_unlock(&lu_gp->lu_gp_lock);
2296
2297 return len;
2298}
2299
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002300CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2301CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302
2303static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002304 &target_lu_gp_attr_lu_gp_id,
2305 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306 NULL,
2307};
2308
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002309static void target_core_alua_lu_gp_release(struct config_item *item)
2310{
2311 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2312 struct t10_alua_lu_gp, lu_gp_group);
2313
2314 core_alua_free_lu_gp(lu_gp);
2315}
2316
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002317static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002318 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002319};
2320
2321static struct config_item_type target_core_alua_lu_gp_cit = {
2322 .ct_item_ops = &target_core_alua_lu_gp_ops,
2323 .ct_attrs = target_core_alua_lu_gp_attrs,
2324 .ct_owner = THIS_MODULE,
2325};
2326
2327/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2328
2329/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2330
2331static struct config_group *target_core_alua_create_lu_gp(
2332 struct config_group *group,
2333 const char *name)
2334{
2335 struct t10_alua_lu_gp *lu_gp;
2336 struct config_group *alua_lu_gp_cg = NULL;
2337 struct config_item *alua_lu_gp_ci = NULL;
2338
2339 lu_gp = core_alua_allocate_lu_gp(name, 0);
2340 if (IS_ERR(lu_gp))
2341 return NULL;
2342
2343 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2344 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2345
2346 config_group_init_type_name(alua_lu_gp_cg, name,
2347 &target_core_alua_lu_gp_cit);
2348
Andy Grover6708bb22011-06-08 10:36:43 -07002349 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002350 " Group: core/alua/lu_gps/%s\n",
2351 config_item_name(alua_lu_gp_ci));
2352
2353 return alua_lu_gp_cg;
2354
2355}
2356
2357static void target_core_alua_drop_lu_gp(
2358 struct config_group *group,
2359 struct config_item *item)
2360{
2361 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2362 struct t10_alua_lu_gp, lu_gp_group);
2363
Andy Grover6708bb22011-06-08 10:36:43 -07002364 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2366 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002367 /*
2368 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2369 * -> target_core_alua_lu_gp_release()
2370 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002371 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372}
2373
2374static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2375 .make_group = &target_core_alua_create_lu_gp,
2376 .drop_item = &target_core_alua_drop_lu_gp,
2377};
2378
2379static struct config_item_type target_core_alua_lu_gps_cit = {
2380 .ct_item_ops = NULL,
2381 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2382 .ct_owner = THIS_MODULE,
2383};
2384
2385/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2386
2387/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2388
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002389static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002390{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002391 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2392 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393}
2394
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002395static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2396 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002398 return sprintf(page, "%d\n",
2399 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2400}
2401
2402static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2403 const char *page, size_t count)
2404{
2405 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002406 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 unsigned long tmp;
2408 int new_state, ret;
2409
Andy Grover6708bb22011-06-08 10:36:43 -07002410 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002411 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2413 return -EINVAL;
2414 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002415 if (!(dev->dev_flags & DF_CONFIGURED)) {
2416 pr_err("Unable to set alua_access_state while device is"
2417 " not configured\n");
2418 return -ENODEV;
2419 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420
Jingoo Han57103d72013-07-19 16:22:19 +09002421 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002423 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002425 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002426 }
2427 new_state = (int)tmp;
2428
Hannes Reinecke125d0112013-11-19 09:07:46 +01002429 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2430 pr_err("Unable to process implicit configfs ALUA"
2431 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432 return -EINVAL;
2433 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002434 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2435 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2436 /* LBA DEPENDENT is only allowed with implicit ALUA */
2437 pr_err("Unable to process implicit configfs ALUA transition"
2438 " while explicit ALUA management is enabled\n");
2439 return -EINVAL;
2440 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002442 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 NULL, NULL, new_state, 0);
2444 return (!ret) ? count : -EINVAL;
2445}
2446
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002447static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2448 char *page)
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 return sprintf(page, "%s\n",
2452 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2453}
2454
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002455static ssize_t target_tg_pt_gp_alua_access_status_store(
2456 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002457{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002458 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002459 unsigned long tmp;
2460 int new_status, ret;
2461
Andy Grover6708bb22011-06-08 10:36:43 -07002462 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2463 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002464 " valid tg_pt_gp ID: %hu\n",
2465 tg_pt_gp->tg_pt_gp_valid_id);
2466 return -EINVAL;
2467 }
2468
Jingoo Han57103d72013-07-19 16:22:19 +09002469 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002471 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002472 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002473 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474 }
2475 new_status = (int)tmp;
2476
2477 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002478 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2479 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002480 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002481 new_status);
2482 return -EINVAL;
2483 }
2484
2485 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2486 return count;
2487}
2488
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002489static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2490 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002492 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002493}
2494
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002495static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2496 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002497{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002498 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499}
2500
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002501#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2502static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2503 struct config_item *item, char *p) \
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); \
2506 return sprintf(p, "%d\n", \
2507 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2508} \
2509 \
2510static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2511 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002512{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002513 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002514 unsigned long tmp; \
2515 int ret; \
2516 \
2517 if (!t->tg_pt_gp_valid_id) { \
2518 pr_err("Unable to do set ##_name ALUA state on non" \
2519 " valid tg_pt_gp ID: %hu\n", \
2520 t->tg_pt_gp_valid_id); \
2521 return -EINVAL; \
2522 } \
2523 \
2524 ret = kstrtoul(p, 0, &tmp); \
2525 if (ret < 0) { \
2526 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2527 return -EINVAL; \
2528 } \
2529 if (tmp > 1) { \
2530 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2531 return -EINVAL; \
2532 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002533 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002534 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002535 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002536 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002537 \
2538 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002539}
2540
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002541ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2542ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2543ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2544ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2545ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2546ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2547ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002548
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002549static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2550 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002552 return sprintf(page, "%d\n",
2553 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554}
2555
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002556static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2557 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002559 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560 unsigned long tmp;
2561 int ret;
2562
Jingoo Han57103d72013-07-19 16:22:19 +09002563 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002565 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002566 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 }
2568
2569 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002570 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571 " %lu\n", tmp);
2572 return -EINVAL;
2573 }
2574 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2575
2576 return count;
2577}
2578
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002579static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2580 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002582 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583}
2584
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002585static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2586 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002588 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2589 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590}
2591
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002592static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2593 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002595 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596}
2597
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002598static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2599 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002600{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002601 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2602 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603}
2604
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002605static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2606 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002607{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002608 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002609}
2610
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002611static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2612 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002613{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002614 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2615 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002616}
2617
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002618static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2619 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002620{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002621 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622}
2623
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002624static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2625 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002627 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002628}
2629
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002630static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2631 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002632{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002633 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2634
Andy Grover6708bb22011-06-08 10:36:43 -07002635 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002637 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2638}
2639
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002640static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2641 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002642{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002643 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002644 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2645 unsigned long tg_pt_gp_id;
2646 int ret;
2647
Jingoo Han57103d72013-07-19 16:22:19 +09002648 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002649 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002650 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002651 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002652 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002653 }
2654 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002655 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002656 " 0x0000ffff\n", tg_pt_gp_id);
2657 return -EINVAL;
2658 }
2659
2660 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2661 if (ret < 0)
2662 return -EINVAL;
2663
Andy Grover6708bb22011-06-08 10:36:43 -07002664 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002665 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2666 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2667 tg_pt_gp->tg_pt_gp_id);
2668
2669 return count;
2670}
2671
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002672static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2673 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002675 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677 ssize_t len = 0, cur_len;
2678 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2679
2680 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2681
2682 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002683 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2684 lun_tg_pt_gp_link) {
2685 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002686
2687 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002688 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2689 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2690 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 config_item_name(&lun->lun_group.cg_item));
2692 cur_len++; /* Extra byte for NULL terminator */
2693
2694 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002695 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002696 "_members buffer\n");
2697 break;
2698 }
2699 memcpy(page+len, buf, cur_len);
2700 len += cur_len;
2701 }
2702 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2703
2704 return len;
2705}
2706
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002707CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2708CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2709CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2710CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2711CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2712CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2713CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2714CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2715CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2716CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2717CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2718CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2719CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2720CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2721CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2722CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2723CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724
2725static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002726 &target_tg_pt_gp_attr_alua_access_state,
2727 &target_tg_pt_gp_attr_alua_access_status,
2728 &target_tg_pt_gp_attr_alua_access_type,
2729 &target_tg_pt_gp_attr_alua_support_transitioning,
2730 &target_tg_pt_gp_attr_alua_support_offline,
2731 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2732 &target_tg_pt_gp_attr_alua_support_unavailable,
2733 &target_tg_pt_gp_attr_alua_support_standby,
2734 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2735 &target_tg_pt_gp_attr_alua_support_active_optimized,
2736 &target_tg_pt_gp_attr_alua_write_metadata,
2737 &target_tg_pt_gp_attr_nonop_delay_msecs,
2738 &target_tg_pt_gp_attr_trans_delay_msecs,
2739 &target_tg_pt_gp_attr_implicit_trans_secs,
2740 &target_tg_pt_gp_attr_preferred,
2741 &target_tg_pt_gp_attr_tg_pt_gp_id,
2742 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002743 NULL,
2744};
2745
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002746static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2747{
2748 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2749 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2750
2751 core_alua_free_tg_pt_gp(tg_pt_gp);
2752}
2753
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002754static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002755 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756};
2757
2758static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2759 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2760 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2761 .ct_owner = THIS_MODULE,
2762};
2763
2764/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2765
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002766/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767
2768static struct config_group *target_core_alua_create_tg_pt_gp(
2769 struct config_group *group,
2770 const char *name)
2771{
2772 struct t10_alua *alua = container_of(group, struct t10_alua,
2773 alua_tg_pt_gps_group);
2774 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775 struct config_group *alua_tg_pt_gp_cg = NULL;
2776 struct config_item *alua_tg_pt_gp_ci = NULL;
2777
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002778 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002779 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002780 return NULL;
2781
2782 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2783 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2784
2785 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2786 &target_core_alua_tg_pt_gp_cit);
2787
Andy Grover6708bb22011-06-08 10:36:43 -07002788 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789 " Group: alua/tg_pt_gps/%s\n",
2790 config_item_name(alua_tg_pt_gp_ci));
2791
2792 return alua_tg_pt_gp_cg;
2793}
2794
2795static void target_core_alua_drop_tg_pt_gp(
2796 struct config_group *group,
2797 struct config_item *item)
2798{
2799 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2800 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2801
Andy Grover6708bb22011-06-08 10:36:43 -07002802 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002803 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2804 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002805 /*
2806 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2807 * -> target_core_alua_tg_pt_gp_release().
2808 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810}
2811
2812static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2813 .make_group = &target_core_alua_create_tg_pt_gp,
2814 .drop_item = &target_core_alua_drop_tg_pt_gp,
2815};
2816
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002817TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002819/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820
2821/* Start functions for struct config_item_type target_core_alua_cit */
2822
2823/*
2824 * target_core_alua_cit is a ConfigFS group that lives under
2825 * /sys/kernel/config/target/core/alua. There are default groups
2826 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2827 * target_core_alua_cit in target_core_init_configfs() below.
2828 */
2829static struct config_item_type target_core_alua_cit = {
2830 .ct_item_ops = NULL,
2831 .ct_attrs = NULL,
2832 .ct_owner = THIS_MODULE,
2833};
2834
2835/* End functions for struct config_item_type target_core_alua_cit */
2836
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002837/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002838
2839static struct config_group *target_core_stat_mkdir(
2840 struct config_group *group,
2841 const char *name)
2842{
2843 return ERR_PTR(-ENOSYS);
2844}
2845
2846static void target_core_stat_rmdir(
2847 struct config_group *group,
2848 struct config_item *item)
2849{
2850 return;
2851}
2852
2853static struct configfs_group_operations target_core_stat_group_ops = {
2854 .make_group = &target_core_stat_mkdir,
2855 .drop_item = &target_core_stat_rmdir,
2856};
2857
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002858TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002859
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002860/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002861
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002862/* Start functions for struct config_item_type target_core_hba_cit */
2863
2864static struct config_group *target_core_make_subdev(
2865 struct config_group *group,
2866 const char *name)
2867{
2868 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 struct config_item *hba_ci = &group->cg_item;
2870 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002871 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002872 struct se_device *dev;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002873 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002875 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2876 if (ret)
2877 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002878
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002879 dev = target_alloc_device(hba, name);
2880 if (!dev)
2881 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002883 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002885 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002886 &tb->tb_dev_attrib_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002887 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
2888
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002889 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002890 &tb->tb_dev_pr_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002891 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
2892
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002893 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002894 &tb->tb_dev_wwn_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002895 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
2896 &dev->dev_group);
2897
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002898 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002899 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002900 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
2901 &dev->dev_group);
2902
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002903 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002904 "statistics", &tb->tb_dev_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002905 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
2906 &dev->dev_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002909 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002910 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002911 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002912 if (!tg_pt_gp)
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002913 goto out_free_device;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002914 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2917 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002918 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
2919 &dev->t10_alua.alua_tg_pt_gps_group);
2920
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002921 /*
2922 * Add core/$HBA/$DEV/statistics/ default groups
2923 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002924 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925
2926 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002927 return &dev->dev_group;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002928
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002929out_free_device:
2930 target_free_device(dev);
2931out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002933 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934}
2935
2936static void target_core_drop_subdev(
2937 struct config_group *group,
2938 struct config_item *item)
2939{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002940 struct config_group *dev_cg = to_config_group(item);
2941 struct se_device *dev =
2942 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002945 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002947 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002949 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
2950 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002951
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002952 /*
2953 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2954 * directly from target_core_alua_tg_pt_gp_release().
2955 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002956 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957
Christoph Hellwig1ae16022016-02-26 11:02:14 +01002958 configfs_remove_default_groups(dev_cg);
2959
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002961 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002963 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965}
2966
2967static struct configfs_group_operations target_core_hba_group_ops = {
2968 .make_group = target_core_make_subdev,
2969 .drop_item = target_core_drop_subdev,
2970};
2971
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002973static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002974{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002975 return container_of(to_config_group(item), struct se_hba, hba_group);
2976}
2977
2978static ssize_t target_hba_info_show(struct config_item *item, char *page)
2979{
2980 struct se_hba *hba = to_hba(item);
2981
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002983 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002984 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002985}
2986
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002987static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002989 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990 int hba_mode = 0;
2991
2992 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2993 hba_mode = 1;
2994
2995 return sprintf(page, "%d\n", hba_mode);
2996}
2997
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002998static ssize_t target_hba_mode_store(struct config_item *item,
2999 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003000{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003001 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003002 unsigned long mode_flag;
3003 int ret;
3004
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003005 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003006 return -EINVAL;
3007
Jingoo Han57103d72013-07-19 16:22:19 +09003008 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003009 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003010 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003011 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012 }
3013
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003014 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07003015 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003016 return -EINVAL;
3017 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003018
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003019 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003020 if (ret < 0)
3021 return -EINVAL;
3022 if (ret > 0)
3023 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3024 else if (ret == 0)
3025 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3026
3027 return count;
3028}
3029
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003030CONFIGFS_ATTR_RO(target_, hba_info);
3031CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003033static void target_core_hba_release(struct config_item *item)
3034{
3035 struct se_hba *hba = container_of(to_config_group(item),
3036 struct se_hba, hba_group);
3037 core_delete_hba(hba);
3038}
3039
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003040static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003041 &target_attr_hba_info,
3042 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003043 NULL,
3044};
3045
3046static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003047 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003048};
3049
3050static struct config_item_type target_core_hba_cit = {
3051 .ct_item_ops = &target_core_hba_item_ops,
3052 .ct_group_ops = &target_core_hba_group_ops,
3053 .ct_attrs = target_core_hba_attrs,
3054 .ct_owner = THIS_MODULE,
3055};
3056
3057static struct config_group *target_core_call_addhbatotarget(
3058 struct config_group *group,
3059 const char *name)
3060{
3061 char *se_plugin_str, *str, *str2;
3062 struct se_hba *hba;
3063 char buf[TARGET_CORE_NAME_MAX_LEN];
3064 unsigned long plugin_dep_id = 0;
3065 int ret;
3066
3067 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003068 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003069 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003070 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3071 TARGET_CORE_NAME_MAX_LEN);
3072 return ERR_PTR(-ENAMETOOLONG);
3073 }
3074 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3075
3076 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003077 if (!str) {
3078 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003079 return ERR_PTR(-EINVAL);
3080 }
3081 se_plugin_str = buf;
3082 /*
3083 * Special case for subsystem plugins that have "_" in their names.
3084 * Namely rd_direct and rd_mcp..
3085 */
3086 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003087 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 *str2 = '\0'; /* Terminate for *se_plugin_str */
3089 str2++; /* Skip to start of plugin dependent ID */
3090 str = str2;
3091 } else {
3092 *str = '\0'; /* Terminate for *se_plugin_str */
3093 str++; /* Skip to start of plugin dependent ID */
3094 }
3095
Jingoo Han57103d72013-07-19 16:22:19 +09003096 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003098 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003099 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003100 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003101 }
3102 /*
3103 * Load up TCM subsystem plugins if they have not already been loaded.
3104 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003105 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106
3107 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3108 if (IS_ERR(hba))
3109 return ERR_CAST(hba);
3110
3111 config_group_init_type_name(&hba->hba_group, name,
3112 &target_core_hba_cit);
3113
3114 return &hba->hba_group;
3115}
3116
3117static void target_core_call_delhbafromtarget(
3118 struct config_group *group,
3119 struct config_item *item)
3120{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003121 /*
3122 * core_delete_hba() is called from target_core_hba_item_ops->release()
3123 * -> target_core_hba_release()
3124 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003125 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003126}
3127
3128static struct configfs_group_operations target_core_group_ops = {
3129 .make_group = target_core_call_addhbatotarget,
3130 .drop_item = target_core_call_delhbafromtarget,
3131};
3132
3133static struct config_item_type target_core_cit = {
3134 .ct_item_ops = NULL,
3135 .ct_group_ops = &target_core_group_ops,
3136 .ct_attrs = NULL,
3137 .ct_owner = THIS_MODULE,
3138};
3139
3140/* Stop functions for struct config_item_type target_core_hba_cit */
3141
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003142void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003143{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003144 target_core_setup_dev_cit(tb);
3145 target_core_setup_dev_attrib_cit(tb);
3146 target_core_setup_dev_pr_cit(tb);
3147 target_core_setup_dev_wwn_cit(tb);
3148 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3149 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003150}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003151
Axel Lin54550fa2011-03-14 04:06:09 -07003152static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003154 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 struct t10_alua_lu_gp *lu_gp;
3156 int ret;
3157
Andy Grover6708bb22011-06-08 10:36:43 -07003158 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3160 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3161
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162 config_group_init(&subsys->su_group);
3163 mutex_init(&subsys->su_mutex);
3164
Andy Grovere3d6f902011-07-19 08:55:10 +00003165 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003167 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 /*
3169 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3170 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3171 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003172 config_group_init_type_name(&target_core_hbagroup, "core",
3173 &target_core_cit);
3174 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 /*
3177 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3178 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003179 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3180 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 /*
3183 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3184 * groups under /sys/kernel/config/target/core/alua/
3185 */
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003186 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3187 &target_core_alua_lu_gps_cit);
3188 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190 /*
3191 * Add core/alua/lu_gps/default_lu_gp
3192 */
3193 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003194 if (IS_ERR(lu_gp)) {
3195 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003197 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003199 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3200 &target_core_alua_lu_gp_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003201 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3202
Andy Grovere3d6f902011-07-19 08:55:10 +00003203 default_lu_gp = lu_gp;
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003204
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 /*
3206 * Register the target_core_mod subsystem with configfs.
3207 */
3208 ret = configfs_register_subsystem(subsys);
3209 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003210 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211 ret, subsys->su_group.cg_item.ci_namebuf);
3212 goto out_global;
3213 }
Andy Grover6708bb22011-06-08 10:36:43 -07003214 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003215 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3217 /*
3218 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3219 */
3220 ret = rd_module_init();
3221 if (ret < 0)
3222 goto out;
3223
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003224 ret = core_dev_setup_virtual_lun0();
3225 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003226 goto out;
3227
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003228 ret = target_xcopy_setup_pt();
3229 if (ret < 0)
3230 goto out;
3231
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 return 0;
3233
3234out:
3235 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 core_dev_release_virtual_lun0();
3237 rd_module_exit();
3238out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003239 if (default_lu_gp) {
3240 core_alua_free_lu_gp(default_lu_gp);
3241 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003243 release_se_kmem_caches();
3244 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245}
3246
Axel Lin54550fa2011-03-14 04:06:09 -07003247static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003248{
Christoph Hellwig1ae16022016-02-26 11:02:14 +01003249 configfs_remove_default_groups(&alua_lu_gps_group);
3250 configfs_remove_default_groups(&alua_group);
3251 configfs_remove_default_groups(&target_core_hbagroup);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003252
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003253 /*
3254 * We expect subsys->su_group.default_groups to be released
3255 * by configfs subsystem provider logic..
3256 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003257 configfs_unregister_subsystem(&target_core_fabrics);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258
Andy Grovere3d6f902011-07-19 08:55:10 +00003259 core_alua_free_lu_gp(default_lu_gp);
3260 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003261
Andy Grover6708bb22011-06-08 10:36:43 -07003262 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003263 " Infrastructure\n");
3264
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003265 core_dev_release_virtual_lun0();
3266 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003267 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003268 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269}
3270
3271MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3272MODULE_AUTHOR("nab@Linux-iSCSI.org");
3273MODULE_LICENSE("GPL");
3274
3275module_init(target_core_init_configfs);
3276module_exit(target_core_exit_configfs);