blob: 5cf613544eebe593e3d638fe0521cb42bc53e652 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2008-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#include <generated/utsrelease.h>
26#include <linux/utsname.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/namei.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32#include <linux/delay.h>
33#include <linux/unistd.h>
34#include <linux/string.h>
35#include <linux/parser.h>
36#include <linux/syscalls.h>
37#include <linux/configfs.h>
Andy Grovere3d6f902011-07-19 08:55:10 +000038#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039
40#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050041#include <target/target_core_backend.h>
42#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043#include <target/target_core_fabric_configfs.h>
44#include <target/target_core_configfs.h>
45#include <target/configfs_macros.h>
46
Christoph Hellwige26d99a2011-11-14 12:30:30 -050047#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080048#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_pr.h"
50#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070051#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080052
Andy Grovere3d6f902011-07-19 08:55:10 +000053extern struct t10_alua_lu_gp *default_lu_gp;
54
Roland Dreierd0f474e2012-01-12 10:41:18 -080055static LIST_HEAD(g_tf_list);
56static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057
58struct target_core_configfs_attribute {
59 struct configfs_attribute attr;
60 ssize_t (*show)(void *, char *);
61 ssize_t (*store)(void *, const char *, size_t);
62};
63
Andy Grovere3d6f902011-07-19 08:55:10 +000064static struct config_group target_core_hbagroup;
65static struct config_group alua_group;
66static struct config_group alua_lu_gps_group;
67
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068static inline struct se_hba *
69item_to_hba(struct config_item *item)
70{
71 return container_of(to_config_group(item), struct se_hba, hba_group);
72}
73
74/*
75 * Attributes for /sys/kernel/config/target/
76 */
77static ssize_t target_core_attr_show(struct config_item *item,
78 struct configfs_attribute *attr,
79 char *page)
80{
81 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
82 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
83 utsname()->sysname, utsname()->machine);
84}
85
86static struct configfs_item_operations target_core_fabric_item_ops = {
87 .show_attribute = target_core_attr_show,
88};
89
90static struct configfs_attribute target_core_item_attr_version = {
91 .ca_owner = THIS_MODULE,
92 .ca_name = "version",
93 .ca_mode = S_IRUGO,
94};
95
96static struct target_fabric_configfs *target_core_get_fabric(
97 const char *name)
98{
99 struct target_fabric_configfs *tf;
100
Andy Grover6708bb22011-06-08 10:36:43 -0700101 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 return NULL;
103
104 mutex_lock(&g_tf_lock);
105 list_for_each_entry(tf, &g_tf_list, tf_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700106 if (!strcmp(tf->tf_name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800107 atomic_inc(&tf->tf_access_cnt);
108 mutex_unlock(&g_tf_lock);
109 return tf;
110 }
111 }
112 mutex_unlock(&g_tf_lock);
113
114 return NULL;
115}
116
117/*
118 * Called from struct target_core_group_ops->make_group()
119 */
120static struct config_group *target_core_register_fabric(
121 struct config_group *group,
122 const char *name)
123{
124 struct target_fabric_configfs *tf;
125 int ret;
126
Andy Grover6708bb22011-06-08 10:36:43 -0700127 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 " %s\n", group, name);
129 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 * Below are some hardcoded request_module() calls to automatically
131 * local fabric modules when the following is called:
132 *
133 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
134 *
135 * Note that this does not limit which TCM fabric module can be
136 * registered, but simply provids auto loading logic for modules with
137 * mkdir(2) system calls with known TCM fabric modules.
138 */
Andy Grover6708bb22011-06-08 10:36:43 -0700139 if (!strncmp(name, "iscsi", 5)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 /*
141 * Automatically load the LIO Target fabric module when the
142 * following is called:
143 *
144 * mkdir -p $CONFIGFS/target/iscsi
145 */
146 ret = request_module("iscsi_target_mod");
147 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700148 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149 " iscsi_target_mod.ko: %d\n", ret);
150 return ERR_PTR(-EINVAL);
151 }
Andy Grover6708bb22011-06-08 10:36:43 -0700152 } else if (!strncmp(name, "loopback", 8)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800153 /*
154 * Automatically load the tcm_loop fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/loopback
158 */
159 ret = request_module("tcm_loop");
160 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700161 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162 " tcm_loop.ko: %d\n", ret);
163 return ERR_PTR(-EINVAL);
164 }
165 }
166
167 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700168 if (!tf) {
169 pr_err("target_core_get_fabric() failed for %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 name);
171 return ERR_PTR(-EINVAL);
172 }
Andy Grover6708bb22011-06-08 10:36:43 -0700173 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174 " %s\n", tf->tf_name);
175 /*
176 * On a successful target_core_get_fabric() look, the returned
177 * struct target_fabric_configfs *tf will contain a usage reference.
178 */
Andy Grover6708bb22011-06-08 10:36:43 -0700179 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Andy Groverd80e224d2013-10-09 11:05:56 -0700180 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800181
182 tf->tf_group.default_groups = tf->tf_default_groups;
183 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
184 tf->tf_group.default_groups[1] = NULL;
185
186 config_group_init_type_name(&tf->tf_group, name,
Andy Groverd80e224d2013-10-09 11:05:56 -0700187 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800188 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Andy Groverd80e224d2013-10-09 11:05:56 -0700189 &tf->tf_cit_tmpl.tfc_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190
Andy Grover6708bb22011-06-08 10:36:43 -0700191 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800192 " %s\n", tf->tf_group.cg_item.ci_name);
193 /*
194 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
195 */
196 tf->tf_ops.tf_subsys = tf->tf_subsys;
197 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700198 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199 " for %s\n", name);
200
201 return &tf->tf_group;
202}
203
204/*
205 * Called from struct target_core_group_ops->drop_item()
206 */
207static void target_core_deregister_fabric(
208 struct config_group *group,
209 struct config_item *item)
210{
211 struct target_fabric_configfs *tf = container_of(
212 to_config_group(item), struct target_fabric_configfs, tf_group);
213 struct config_group *tf_group;
214 struct config_item *df_item;
215 int i;
216
Andy Grover6708bb22011-06-08 10:36:43 -0700217 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218 " tf list\n", config_item_name(item));
219
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221 " %s\n", tf->tf_name);
222 atomic_dec(&tf->tf_access_cnt);
223
Andy Grover6708bb22011-06-08 10:36:43 -0700224 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225 " tf->tf_fabric for %s\n", tf->tf_name);
226 tf->tf_fabric = NULL;
227
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 " %s\n", config_item_name(item));
230
231 tf_group = &tf->tf_group;
232 for (i = 0; tf_group->default_groups[i]; i++) {
233 df_item = &tf_group->default_groups[i]->cg_item;
234 tf_group->default_groups[i] = NULL;
235 config_item_put(df_item);
236 }
237 config_item_put(item);
238}
239
240static struct configfs_group_operations target_core_fabric_group_ops = {
241 .make_group = &target_core_register_fabric,
242 .drop_item = &target_core_deregister_fabric,
243};
244
245/*
246 * All item attributes appearing in /sys/kernel/target/ appear here.
247 */
248static struct configfs_attribute *target_core_fabric_item_attrs[] = {
249 &target_core_item_attr_version,
250 NULL,
251};
252
253/*
254 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
255 */
256static struct config_item_type target_core_fabrics_item = {
257 .ct_item_ops = &target_core_fabric_item_ops,
258 .ct_group_ops = &target_core_fabric_group_ops,
259 .ct_attrs = target_core_fabric_item_attrs,
260 .ct_owner = THIS_MODULE,
261};
262
263static struct configfs_subsystem target_core_fabrics = {
264 .su_group = {
265 .cg_item = {
266 .ci_namebuf = "target",
267 .ci_type = &target_core_fabrics_item,
268 },
269 },
270};
271
Nicholas Bellinger56d128f2013-08-22 11:32:31 -0700272struct configfs_subsystem *target_core_subsystem[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273 &target_core_fabrics,
274 NULL,
275};
276
277/*##############################################################################
278// Start functions called by external Target Fabrics Modules
279//############################################################################*/
280
281/*
282 * First function called by fabric modules to:
283 *
284 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
285 * 2) Add struct target_fabric_configfs to g_tf_list
286 * 3) Return struct target_fabric_configfs to fabric module to be passed
287 * into target_fabric_configfs_register().
288 */
289struct target_fabric_configfs *target_fabric_configfs_init(
290 struct module *fabric_mod,
291 const char *name)
292{
293 struct target_fabric_configfs *tf;
294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700299 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000302 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 }
304
305 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700306 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000307 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308
309 INIT_LIST_HEAD(&tf->tf_list);
310 atomic_set(&tf->tf_access_cnt, 0);
311 /*
312 * Setup the default generic struct config_item_type's (cits) in
313 * struct target_fabric_configfs->tf_cit_tmpl
314 */
315 tf->tf_module = fabric_mod;
316 target_fabric_setup_cits(tf);
317
318 tf->tf_subsys = target_core_subsystem[0];
319 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
320
321 mutex_lock(&g_tf_lock);
322 list_add_tail(&tf->tf_list, &g_tf_list);
323 mutex_unlock(&g_tf_lock);
324
Andy Grover6708bb22011-06-08 10:36:43 -0700325 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700327 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 " %s\n", tf, tf->tf_name);
329 return tf;
330}
331EXPORT_SYMBOL(target_fabric_configfs_init);
332
333/*
334 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
335 */
336void target_fabric_configfs_free(
337 struct target_fabric_configfs *tf)
338{
339 mutex_lock(&g_tf_lock);
340 list_del(&tf->tf_list);
341 mutex_unlock(&g_tf_lock);
342
343 kfree(tf);
344}
345EXPORT_SYMBOL(target_fabric_configfs_free);
346
347/*
348 * Perform a sanity check of the passed tf->tf_ops before completing
349 * TCM fabric module registration.
350 */
351static int target_fabric_tf_ops_check(
352 struct target_fabric_configfs *tf)
353{
354 struct target_core_fabric_ops *tfo = &tf->tf_ops;
355
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->get_fabric_name) {
357 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->get_fabric_proto_ident) {
361 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->tpg_get_default_depth) {
373 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Andy Grover6708bb22011-06-08 10:36:43 -0700376 if (!tfo->tpg_get_pr_transport_id) {
377 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 return -EINVAL;
379 }
Andy Grover6708bb22011-06-08 10:36:43 -0700380 if (!tfo->tpg_get_pr_transport_id_len) {
381 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 return -EINVAL;
383 }
Andy Grover6708bb22011-06-08 10:36:43 -0700384 if (!tfo->tpg_check_demo_mode) {
385 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 return -EINVAL;
387 }
Andy Grover6708bb22011-06-08 10:36:43 -0700388 if (!tfo->tpg_check_demo_mode_cache) {
389 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 return -EINVAL;
391 }
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (!tfo->tpg_check_demo_mode_write_protect) {
393 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 return -EINVAL;
395 }
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!tfo->tpg_check_prod_mode_write_protect) {
397 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 return -EINVAL;
399 }
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!tfo->tpg_alloc_fabric_acl) {
401 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return -EINVAL;
403 }
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->tpg_release_fabric_acl) {
405 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->tpg_get_inst_index) {
409 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400412 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700413 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->shutdown_session) {
417 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
Andy Grover6708bb22011-06-08 10:36:43 -0700420 if (!tfo->close_session) {
421 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 return -EINVAL;
423 }
Andy Grover6708bb22011-06-08 10:36:43 -0700424 if (!tfo->sess_get_index) {
425 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 return -EINVAL;
427 }
Andy Grover6708bb22011-06-08 10:36:43 -0700428 if (!tfo->write_pending) {
429 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 return -EINVAL;
431 }
Andy Grover6708bb22011-06-08 10:36:43 -0700432 if (!tfo->write_pending_status) {
433 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434 return -EINVAL;
435 }
Andy Grover6708bb22011-06-08 10:36:43 -0700436 if (!tfo->set_default_node_attributes) {
437 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 return -EINVAL;
439 }
Andy Grover6708bb22011-06-08 10:36:43 -0700440 if (!tfo->get_task_tag) {
441 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 return -EINVAL;
443 }
Andy Grover6708bb22011-06-08 10:36:43 -0700444 if (!tfo->get_cmd_state) {
445 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 return -EINVAL;
447 }
Andy Grover6708bb22011-06-08 10:36:43 -0700448 if (!tfo->queue_data_in) {
449 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 return -EINVAL;
451 }
Andy Grover6708bb22011-06-08 10:36:43 -0700452 if (!tfo->queue_status) {
453 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 return -EINVAL;
455 }
Andy Grover6708bb22011-06-08 10:36:43 -0700456 if (!tfo->queue_tm_rsp) {
457 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 return -EINVAL;
459 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 /*
461 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
462 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
463 * target_core_fabric_configfs.c WWN+TPG group context code.
464 */
Andy Grover6708bb22011-06-08 10:36:43 -0700465 if (!tfo->fabric_make_wwn) {
466 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 return -EINVAL;
468 }
Andy Grover6708bb22011-06-08 10:36:43 -0700469 if (!tfo->fabric_drop_wwn) {
470 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 return -EINVAL;
472 }
Andy Grover6708bb22011-06-08 10:36:43 -0700473 if (!tfo->fabric_make_tpg) {
474 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 return -EINVAL;
476 }
Andy Grover6708bb22011-06-08 10:36:43 -0700477 if (!tfo->fabric_drop_tpg) {
478 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return -EINVAL;
480 }
481
482 return 0;
483}
484
485/*
486 * Called 2nd from fabric module with returned parameter of
487 * struct target_fabric_configfs * from target_fabric_configfs_init().
488 *
489 * Upon a successful registration, the new fabric's struct config_item is
490 * return. Also, a pointer to this struct is set in the passed
491 * struct target_fabric_configfs.
492 */
493int target_fabric_configfs_register(
494 struct target_fabric_configfs *tf)
495{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 int ret;
497
Andy Grover6708bb22011-06-08 10:36:43 -0700498 if (!tf) {
499 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 " pointer\n");
501 return -EINVAL;
502 }
Andy Grover6708bb22011-06-08 10:36:43 -0700503 if (!tf->tf_subsys) {
504 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 " pointer\n");
506 return -EINVAL;
507 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508 ret = target_fabric_tf_ops_check(tf);
509 if (ret < 0)
510 return ret;
511
Andy Grover6708bb22011-06-08 10:36:43 -0700512 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 ">>>>>>>>>>\n");
514 return 0;
515}
516EXPORT_SYMBOL(target_fabric_configfs_register);
517
518void target_fabric_configfs_deregister(
519 struct target_fabric_configfs *tf)
520{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521 struct configfs_subsystem *su;
522
Andy Grover6708bb22011-06-08 10:36:43 -0700523 if (!tf) {
524 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 "configfs\n");
526 return;
527 }
528 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700529 if (!su) {
530 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531 " pointer\n");
532 return;
533 }
Andy Grover6708bb22011-06-08 10:36:43 -0700534 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 ">>>>>>>>>>>>\n");
536 mutex_lock(&g_tf_lock);
537 if (atomic_read(&tf->tf_access_cnt)) {
538 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700539 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 tf->tf_name);
541 BUG();
542 }
543 list_del(&tf->tf_list);
544 mutex_unlock(&g_tf_lock);
545
Andy Grover6708bb22011-06-08 10:36:43 -0700546 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 " %s\n", tf->tf_name);
548 tf->tf_module = NULL;
549 tf->tf_subsys = NULL;
550 kfree(tf);
551
Andy Grover6708bb22011-06-08 10:36:43 -0700552 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554}
555EXPORT_SYMBOL(target_fabric_configfs_deregister);
556
557/*##############################################################################
558// Stop functions called by external Target Fabrics Modules
559//############################################################################*/
560
561/* Start functions for struct config_item_type target_core_dev_attrib_cit */
562
563#define DEF_DEV_ATTRIB_SHOW(_name) \
564static ssize_t target_core_dev_show_attr_##_name( \
565 struct se_dev_attrib *da, \
566 char *page) \
567{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400568 return snprintf(page, PAGE_SIZE, "%u\n", \
569 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570}
571
572#define DEF_DEV_ATTRIB_STORE(_name) \
573static ssize_t target_core_dev_store_attr_##_name( \
574 struct se_dev_attrib *da, \
575 const char *page, \
576 size_t count) \
577{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 unsigned long val; \
579 int ret; \
580 \
Jingoo Han57103d72013-07-19 16:22:19 +0900581 ret = kstrtoul(page, 0, &val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 if (ret < 0) { \
Jingoo Han57103d72013-07-19 16:22:19 +0900583 pr_err("kstrtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 " ret: %d\n", ret); \
585 return -EINVAL; \
586 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400587 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 \
589 return (!ret) ? count : -EINVAL; \
590}
591
592#define DEF_DEV_ATTRIB(_name) \
593DEF_DEV_ATTRIB_SHOW(_name); \
594DEF_DEV_ATTRIB_STORE(_name);
595
596#define DEF_DEV_ATTRIB_RO(_name) \
597DEF_DEV_ATTRIB_SHOW(_name);
598
599CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
600#define SE_DEV_ATTR(_name, _mode) \
601static struct target_core_dev_attrib_attribute \
602 target_core_dev_attrib_##_name = \
603 __CONFIGFS_EATTR(_name, _mode, \
604 target_core_dev_show_attr_##_name, \
605 target_core_dev_store_attr_##_name);
606
607#define SE_DEV_ATTR_RO(_name); \
608static struct target_core_dev_attrib_attribute \
609 target_core_dev_attrib_##_name = \
610 __CONFIGFS_EATTR_RO(_name, \
611 target_core_dev_show_attr_##_name);
612
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700613DEF_DEV_ATTRIB(emulate_model_alias);
614SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
615
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800616DEF_DEV_ATTRIB(emulate_dpo);
617SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
618
619DEF_DEV_ATTRIB(emulate_fua_write);
620SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
621
622DEF_DEV_ATTRIB(emulate_fua_read);
623SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
624
625DEF_DEV_ATTRIB(emulate_write_cache);
626SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
627
628DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
629SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
630
631DEF_DEV_ATTRIB(emulate_tas);
632SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
633
634DEF_DEV_ATTRIB(emulate_tpu);
635SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
636
637DEF_DEV_ATTRIB(emulate_tpws);
638SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
639
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700640DEF_DEV_ATTRIB(emulate_caw);
641SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
642
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700643DEF_DEV_ATTRIB(emulate_3pc);
644SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
645
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646DEF_DEV_ATTRIB(enforce_pr_isids);
647SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
648
Roland Dreiere22a7f072011-07-05 13:34:52 -0700649DEF_DEV_ATTRIB(is_nonrot);
650SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
651
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700652DEF_DEV_ATTRIB(emulate_rest_reord);
653SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
654
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655DEF_DEV_ATTRIB_RO(hw_block_size);
656SE_DEV_ATTR_RO(hw_block_size);
657
658DEF_DEV_ATTRIB(block_size);
659SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
660
661DEF_DEV_ATTRIB_RO(hw_max_sectors);
662SE_DEV_ATTR_RO(hw_max_sectors);
663
Roland Dreier015487b2012-02-13 16:18:17 -0800664DEF_DEV_ATTRIB(fabric_max_sectors);
665SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
666
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667DEF_DEV_ATTRIB(optimal_sectors);
668SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
669
670DEF_DEV_ATTRIB_RO(hw_queue_depth);
671SE_DEV_ATTR_RO(hw_queue_depth);
672
673DEF_DEV_ATTRIB(queue_depth);
674SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
675
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676DEF_DEV_ATTRIB(max_unmap_lba_count);
677SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
678
679DEF_DEV_ATTRIB(max_unmap_block_desc_count);
680SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
681
682DEF_DEV_ATTRIB(unmap_granularity);
683SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
684
685DEF_DEV_ATTRIB(unmap_granularity_alignment);
686SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
687
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800688DEF_DEV_ATTRIB(max_write_same_len);
689SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
690
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
692
693static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700694 &target_core_dev_attrib_emulate_model_alias.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 &target_core_dev_attrib_emulate_dpo.attr,
696 &target_core_dev_attrib_emulate_fua_write.attr,
697 &target_core_dev_attrib_emulate_fua_read.attr,
698 &target_core_dev_attrib_emulate_write_cache.attr,
699 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
700 &target_core_dev_attrib_emulate_tas.attr,
701 &target_core_dev_attrib_emulate_tpu.attr,
702 &target_core_dev_attrib_emulate_tpws.attr,
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700703 &target_core_dev_attrib_emulate_caw.attr,
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700704 &target_core_dev_attrib_emulate_3pc.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800705 &target_core_dev_attrib_enforce_pr_isids.attr,
Roland Dreiere22a7f072011-07-05 13:34:52 -0700706 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700707 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 &target_core_dev_attrib_hw_block_size.attr,
709 &target_core_dev_attrib_block_size.attr,
710 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800711 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800712 &target_core_dev_attrib_optimal_sectors.attr,
713 &target_core_dev_attrib_hw_queue_depth.attr,
714 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 &target_core_dev_attrib_max_unmap_lba_count.attr,
716 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
717 &target_core_dev_attrib_unmap_granularity.attr,
718 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800719 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 NULL,
721};
722
723static struct configfs_item_operations target_core_dev_attrib_ops = {
724 .show_attribute = target_core_dev_attrib_attr_show,
725 .store_attribute = target_core_dev_attrib_attr_store,
726};
727
728static struct config_item_type target_core_dev_attrib_cit = {
729 .ct_item_ops = &target_core_dev_attrib_ops,
730 .ct_attrs = target_core_dev_attrib_attrs,
731 .ct_owner = THIS_MODULE,
732};
733
734/* End functions for struct config_item_type target_core_dev_attrib_cit */
735
736/* Start functions for struct config_item_type target_core_dev_wwn_cit */
737
738CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
739#define SE_DEV_WWN_ATTR(_name, _mode) \
740static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
741 __CONFIGFS_EATTR(_name, _mode, \
742 target_core_dev_wwn_show_attr_##_name, \
743 target_core_dev_wwn_store_attr_##_name);
744
745#define SE_DEV_WWN_ATTR_RO(_name); \
746do { \
747 static struct target_core_dev_wwn_attribute \
748 target_core_dev_wwn_##_name = \
749 __CONFIGFS_EATTR_RO(_name, \
750 target_core_dev_wwn_show_attr_##_name); \
751} while (0);
752
753/*
754 * VPD page 0x80 Unit serial
755 */
756static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
757 struct t10_wwn *t10_wwn,
758 char *page)
759{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800760 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
761 &t10_wwn->unit_serial[0]);
762}
763
764static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
765 struct t10_wwn *t10_wwn,
766 const char *page,
767 size_t count)
768{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400769 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
771
772 /*
773 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
774 * from the struct scsi_device level firmware, do not allow
775 * VPD Unit Serial to be emulated.
776 *
777 * Note this struct scsi_device could also be emulating VPD
778 * information from its drivers/scsi LLD. But for now we assume
779 * it is doing 'the right thing' wrt a world wide unique
780 * VPD Unit Serial Number that OS dependent multipath can depend on.
781 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400782 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700783 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800784 " Unit Serial, ignoring request\n");
785 return -EOPNOTSUPP;
786 }
787
Dan Carpenter60d645a2011-06-15 10:03:05 -0700788 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700789 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800790 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
791 return -EOVERFLOW;
792 }
793 /*
794 * Check to see if any active $FABRIC_MOD exports exist. If they
795 * do exist, fail here as changing this information on the fly
796 * (underneath the initiator side OS dependent multipath code)
797 * could cause negative effects.
798 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400799 if (dev->export_count) {
800 pr_err("Unable to set VPD Unit Serial while"
801 " active %d $FABRIC_MOD exports exist\n",
802 dev->export_count);
803 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400805
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806 /*
807 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
808 *
809 * Also, strip any newline added from the userspace
810 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
811 */
812 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
813 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400814 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800815 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400816 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800817
Andy Grover6708bb22011-06-08 10:36:43 -0700818 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400819 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820
821 return count;
822}
823
824SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
825
826/*
827 * VPD page 0x83 Protocol Identifier
828 */
829static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
830 struct t10_wwn *t10_wwn,
831 char *page)
832{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833 struct t10_vpd *vpd;
834 unsigned char buf[VPD_TMP_BUF_SIZE];
835 ssize_t len = 0;
836
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837 memset(buf, 0, VPD_TMP_BUF_SIZE);
838
839 spin_lock(&t10_wwn->t10_vpd_lock);
840 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700841 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800842 continue;
843
844 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
845
Andy Grover6708bb22011-06-08 10:36:43 -0700846 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847 break;
848
849 len += sprintf(page+len, "%s", buf);
850 }
851 spin_unlock(&t10_wwn->t10_vpd_lock);
852
853 return len;
854}
855
856static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
857 struct t10_wwn *t10_wwn,
858 const char *page,
859 size_t count)
860{
861 return -ENOSYS;
862}
863
864SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
865
866/*
867 * Generic wrapper for dumping VPD identifiers by association.
868 */
869#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
870static ssize_t target_core_dev_wwn_show_attr_##_name( \
871 struct t10_wwn *t10_wwn, \
872 char *page) \
873{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874 struct t10_vpd *vpd; \
875 unsigned char buf[VPD_TMP_BUF_SIZE]; \
876 ssize_t len = 0; \
877 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878 spin_lock(&t10_wwn->t10_vpd_lock); \
879 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
880 if (vpd->association != _assoc) \
881 continue; \
882 \
883 memset(buf, 0, VPD_TMP_BUF_SIZE); \
884 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700885 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886 break; \
887 len += sprintf(page+len, "%s", buf); \
888 \
889 memset(buf, 0, VPD_TMP_BUF_SIZE); \
890 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700891 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800892 break; \
893 len += sprintf(page+len, "%s", buf); \
894 \
895 memset(buf, 0, VPD_TMP_BUF_SIZE); \
896 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700897 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800898 break; \
899 len += sprintf(page+len, "%s", buf); \
900 } \
901 spin_unlock(&t10_wwn->t10_vpd_lock); \
902 \
903 return len; \
904}
905
906/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700907 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 */
909DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
910
911static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
912 struct t10_wwn *t10_wwn,
913 const char *page,
914 size_t count)
915{
916 return -ENOSYS;
917}
918
919SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
920
921/*
922 * VPD page 0x83 Association: Target Port
923 */
924DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
925
926static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
927 struct t10_wwn *t10_wwn,
928 const char *page,
929 size_t count)
930{
931 return -ENOSYS;
932}
933
934SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
935
936/*
937 * VPD page 0x83 Association: SCSI Target Device
938 */
939DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
940
941static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
942 struct t10_wwn *t10_wwn,
943 const char *page,
944 size_t count)
945{
946 return -ENOSYS;
947}
948
949SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
950
951CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
952
953static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
954 &target_core_dev_wwn_vpd_unit_serial.attr,
955 &target_core_dev_wwn_vpd_protocol_identifier.attr,
956 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
957 &target_core_dev_wwn_vpd_assoc_target_port.attr,
958 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
959 NULL,
960};
961
962static struct configfs_item_operations target_core_dev_wwn_ops = {
963 .show_attribute = target_core_dev_wwn_attr_show,
964 .store_attribute = target_core_dev_wwn_attr_store,
965};
966
967static struct config_item_type target_core_dev_wwn_cit = {
968 .ct_item_ops = &target_core_dev_wwn_ops,
969 .ct_attrs = target_core_dev_wwn_attrs,
970 .ct_owner = THIS_MODULE,
971};
972
973/* End functions for struct config_item_type target_core_dev_wwn_cit */
974
975/* Start functions for struct config_item_type target_core_dev_pr_cit */
976
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400977CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800978#define SE_DEV_PR_ATTR(_name, _mode) \
979static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
980 __CONFIGFS_EATTR(_name, _mode, \
981 target_core_dev_pr_show_attr_##_name, \
982 target_core_dev_pr_store_attr_##_name);
983
984#define SE_DEV_PR_ATTR_RO(_name); \
985static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
986 __CONFIGFS_EATTR_RO(_name, \
987 target_core_dev_pr_show_attr_##_name);
988
Christoph Hellwigd977f432012-10-10 17:37:15 -0400989static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
990 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991{
992 struct se_node_acl *se_nacl;
993 struct t10_pr_registration *pr_reg;
994 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800995
996 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
997
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800998 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400999 if (!pr_reg)
1000 return sprintf(page, "No SPC-3 Reservation holder\n");
1001
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001003 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004
Christoph Hellwigd977f432012-10-10 17:37:15 -04001005 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001006 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001007 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001008}
1009
Christoph Hellwigd977f432012-10-10 17:37:15 -04001010static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1011 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012{
1013 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001014 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001016 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001017 if (se_nacl) {
1018 len = sprintf(page,
1019 "SPC-2 Reservation: %s Initiator: %s\n",
1020 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1021 se_nacl->initiatorname);
1022 } else {
1023 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001025 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026}
1027
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001028static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1029 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001030{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001031 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032
Christoph Hellwigd977f432012-10-10 17:37:15 -04001033 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1034 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001035
Christoph Hellwigd977f432012-10-10 17:37:15 -04001036 spin_lock(&dev->dev_reservation_lock);
1037 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1038 ret = target_core_dev_pr_show_spc2_res(dev, page);
1039 else
1040 ret = target_core_dev_pr_show_spc3_res(dev, page);
1041 spin_unlock(&dev->dev_reservation_lock);
1042 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043}
1044
1045SE_DEV_PR_ATTR_RO(res_holder);
1046
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001048 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001049{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001050 ssize_t len = 0;
1051
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001052 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001053 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001054 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001055 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056 len = sprintf(page, "SPC-3 Reservation: All Target"
1057 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001058 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 len = sprintf(page, "SPC-3 Reservation: Single"
1060 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001061 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001062
Christoph Hellwigd977f432012-10-10 17:37:15 -04001063 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064 return len;
1065}
1066
1067SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1068
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001070 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001072 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073}
1074
1075SE_DEV_PR_ATTR_RO(res_pr_generation);
1076
1077/*
1078 * res_pr_holder_tg_port
1079 */
1080static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001081 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001083 struct se_node_acl *se_nacl;
1084 struct se_lun *lun;
1085 struct se_portal_group *se_tpg;
1086 struct t10_pr_registration *pr_reg;
1087 struct target_core_fabric_ops *tfo;
1088 ssize_t len = 0;
1089
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 spin_lock(&dev->dev_reservation_lock);
1091 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001092 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001094 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001096
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097 se_nacl = pr_reg->pr_reg_nacl;
1098 se_tpg = se_nacl->se_tpg;
1099 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001100 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101
1102 len += sprintf(page+len, "SPC-3 Reservation: %s"
1103 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1104 tfo->tpg_get_wwn(se_tpg));
1105 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001106 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001107 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1108 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1109 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110
Christoph Hellwigd977f432012-10-10 17:37:15 -04001111out_unlock:
1112 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113 return len;
1114}
1115
1116SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1117
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001119 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120{
1121 struct target_core_fabric_ops *tfo;
1122 struct t10_pr_registration *pr_reg;
1123 unsigned char buf[384];
1124 char i_buf[PR_REG_ISID_ID_LEN];
1125 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001126 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001127
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001128 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1129
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001130 spin_lock(&dev->t10_pr.registration_lock);
1131 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 pr_reg_list) {
1133
1134 memset(buf, 0, 384);
1135 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1136 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001137 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 PR_REG_ISID_ID_LEN);
1139 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1140 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001141 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 pr_reg->pr_res_generation);
1143
Andy Grover6708bb22011-06-08 10:36:43 -07001144 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 break;
1146
1147 len += sprintf(page+len, "%s", buf);
1148 reg_count++;
1149 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001150 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001151
Andy Grover6708bb22011-06-08 10:36:43 -07001152 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 len += sprintf(page+len, "None\n");
1154
1155 return len;
1156}
1157
1158SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1159
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001161 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 struct t10_pr_registration *pr_reg;
1164 ssize_t len = 0;
1165
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 spin_lock(&dev->dev_reservation_lock);
1167 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001168 if (pr_reg) {
1169 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1170 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1171 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174
Christoph Hellwigd977f432012-10-10 17:37:15 -04001175 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176 return len;
1177}
1178
1179SE_DEV_PR_ATTR_RO(res_pr_type);
1180
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001182 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001184 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1185 return sprintf(page, "SPC_PASSTHROUGH\n");
1186 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1187 return sprintf(page, "SPC2_RESERVATIONS\n");
1188 else
1189 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190}
1191
1192SE_DEV_PR_ATTR_RO(res_type);
1193
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001195 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001196{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001197 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 return 0;
1199
1200 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001201 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202}
1203
1204SE_DEV_PR_ATTR_RO(res_aptpl_active);
1205
1206/*
1207 * res_aptpl_metadata
1208 */
1209static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001210 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001212 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 return 0;
1214
1215 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1216}
1217
1218enum {
1219 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1220 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1221 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1222 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1223};
1224
1225static match_table_t tokens = {
1226 {Opt_initiator_fabric, "initiator_fabric=%s"},
1227 {Opt_initiator_node, "initiator_node=%s"},
1228 {Opt_initiator_sid, "initiator_sid=%s"},
1229 {Opt_sa_res_key, "sa_res_key=%s"},
1230 {Opt_res_holder, "res_holder=%d"},
1231 {Opt_res_type, "res_type=%d"},
1232 {Opt_res_scope, "res_scope=%d"},
1233 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1234 {Opt_mapped_lun, "mapped_lun=%d"},
1235 {Opt_target_fabric, "target_fabric=%s"},
1236 {Opt_target_node, "target_node=%s"},
1237 {Opt_tpgt, "tpgt=%d"},
1238 {Opt_port_rtpi, "port_rtpi=%d"},
1239 {Opt_target_lun, "target_lun=%d"},
1240 {Opt_err, NULL}
1241};
1242
1243static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001244 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245 const char *page,
1246 size_t count)
1247{
Jesper Juhl6d180252011-03-14 04:05:56 -07001248 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1249 unsigned char *t_fabric = NULL, *t_port = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 char *orig, *ptr, *arg_p, *opts;
1251 substring_t args[MAX_OPT_ARGS];
1252 unsigned long long tmp_ll;
1253 u64 sa_res_key = 0;
1254 u32 mapped_lun = 0, target_lun = 0;
1255 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1256 u16 port_rpti = 0, tpgt = 0;
1257 u8 type = 0, scope;
1258
Christoph Hellwigd977f432012-10-10 17:37:15 -04001259 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1260 return 0;
1261 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262 return 0;
1263
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001264 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001265 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266 " active fabric exports exist\n");
1267 return -EINVAL;
1268 }
1269
1270 opts = kstrdup(page, GFP_KERNEL);
1271 if (!opts)
1272 return -ENOMEM;
1273
1274 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001275 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001276 if (!*ptr)
1277 continue;
1278
1279 token = match_token(ptr, tokens, args);
1280 switch (token) {
1281 case Opt_initiator_fabric:
1282 i_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001283 if (!i_fabric) {
1284 ret = -ENOMEM;
1285 goto out;
1286 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 break;
1288 case Opt_initiator_node:
1289 i_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001290 if (!i_port) {
1291 ret = -ENOMEM;
1292 goto out;
1293 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001294 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001295 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1297 PR_APTPL_MAX_IPORT_LEN);
1298 ret = -EINVAL;
1299 break;
1300 }
1301 break;
1302 case Opt_initiator_sid:
1303 isid = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001304 if (!isid) {
1305 ret = -ENOMEM;
1306 goto out;
1307 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001308 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001309 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310 "= exceeds PR_REG_ISID_LEN: %d\n",
1311 PR_REG_ISID_LEN);
1312 ret = -EINVAL;
1313 break;
1314 }
1315 break;
1316 case Opt_sa_res_key:
1317 arg_p = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001318 if (!arg_p) {
1319 ret = -ENOMEM;
1320 goto out;
1321 }
Jingoo Han57103d72013-07-19 16:22:19 +09001322 ret = kstrtoull(arg_p, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09001324 pr_err("kstrtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325 " sa_res_key=\n");
1326 goto out;
1327 }
1328 sa_res_key = (u64)tmp_ll;
1329 break;
1330 /*
1331 * PR APTPL Metadata for Reservation
1332 */
1333 case Opt_res_holder:
1334 match_int(args, &arg);
1335 res_holder = arg;
1336 break;
1337 case Opt_res_type:
1338 match_int(args, &arg);
1339 type = (u8)arg;
1340 break;
1341 case Opt_res_scope:
1342 match_int(args, &arg);
1343 scope = (u8)arg;
1344 break;
1345 case Opt_res_all_tg_pt:
1346 match_int(args, &arg);
1347 all_tg_pt = (int)arg;
1348 break;
1349 case Opt_mapped_lun:
1350 match_int(args, &arg);
1351 mapped_lun = (u32)arg;
1352 break;
1353 /*
1354 * PR APTPL Metadata for Target Port
1355 */
1356 case Opt_target_fabric:
1357 t_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001358 if (!t_fabric) {
1359 ret = -ENOMEM;
1360 goto out;
1361 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362 break;
1363 case Opt_target_node:
1364 t_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001365 if (!t_port) {
1366 ret = -ENOMEM;
1367 goto out;
1368 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001369 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001370 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1372 PR_APTPL_MAX_TPORT_LEN);
1373 ret = -EINVAL;
1374 break;
1375 }
1376 break;
1377 case Opt_tpgt:
1378 match_int(args, &arg);
1379 tpgt = (u16)arg;
1380 break;
1381 case Opt_port_rtpi:
1382 match_int(args, &arg);
1383 port_rpti = (u16)arg;
1384 break;
1385 case Opt_target_lun:
1386 match_int(args, &arg);
1387 target_lun = (u32)arg;
1388 break;
1389 default:
1390 break;
1391 }
1392 }
1393
Andy Grover6708bb22011-06-08 10:36:43 -07001394 if (!i_port || !t_port || !sa_res_key) {
1395 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396 ret = -EINVAL;
1397 goto out;
1398 }
1399
1400 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001401 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 " holder\n", type);
1403 ret = -EINVAL;
1404 goto out;
1405 }
1406
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001407 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1409 res_holder, all_tg_pt, type);
1410out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001411 kfree(i_fabric);
1412 kfree(i_port);
1413 kfree(isid);
1414 kfree(t_fabric);
1415 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416 kfree(orig);
1417 return (ret == 0) ? count : ret;
1418}
1419
1420SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1421
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001422CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423
1424static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1425 &target_core_dev_pr_res_holder.attr,
1426 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1427 &target_core_dev_pr_res_pr_generation.attr,
1428 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1429 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1430 &target_core_dev_pr_res_pr_type.attr,
1431 &target_core_dev_pr_res_type.attr,
1432 &target_core_dev_pr_res_aptpl_active.attr,
1433 &target_core_dev_pr_res_aptpl_metadata.attr,
1434 NULL,
1435};
1436
1437static struct configfs_item_operations target_core_dev_pr_ops = {
1438 .show_attribute = target_core_dev_pr_attr_show,
1439 .store_attribute = target_core_dev_pr_attr_store,
1440};
1441
1442static struct config_item_type target_core_dev_pr_cit = {
1443 .ct_item_ops = &target_core_dev_pr_ops,
1444 .ct_attrs = target_core_dev_pr_attrs,
1445 .ct_owner = THIS_MODULE,
1446};
1447
1448/* End functions for struct config_item_type target_core_dev_pr_cit */
1449
1450/* Start functions for struct config_item_type target_core_dev_cit */
1451
1452static ssize_t target_core_show_dev_info(void *p, char *page)
1453{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001454 struct se_device *dev = p;
1455 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 int bl = 0;
1457 ssize_t read_bytes = 0;
1458
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001459 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001461 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001462 return read_bytes;
1463}
1464
1465static struct target_core_configfs_attribute target_core_attr_dev_info = {
1466 .attr = { .ca_owner = THIS_MODULE,
1467 .ca_name = "info",
1468 .ca_mode = S_IRUGO },
1469 .show = target_core_show_dev_info,
1470 .store = NULL,
1471};
1472
1473static ssize_t target_core_store_dev_control(
1474 void *p,
1475 const char *page,
1476 size_t count)
1477{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001478 struct se_device *dev = p;
1479 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001480
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001481 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482}
1483
1484static struct target_core_configfs_attribute target_core_attr_dev_control = {
1485 .attr = { .ca_owner = THIS_MODULE,
1486 .ca_name = "control",
1487 .ca_mode = S_IWUSR },
1488 .show = NULL,
1489 .store = target_core_store_dev_control,
1490};
1491
1492static ssize_t target_core_show_dev_alias(void *p, char *page)
1493{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001494 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001496 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001497 return 0;
1498
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001499 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500}
1501
1502static ssize_t target_core_store_dev_alias(
1503 void *p,
1504 const char *page,
1505 size_t count)
1506{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001507 struct se_device *dev = p;
1508 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 ssize_t read_bytes;
1510
1511 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001512 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001513 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1514 SE_DEV_ALIAS_LEN-1);
1515 return -EINVAL;
1516 }
1517
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001518 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001519 if (!read_bytes)
1520 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001521 if (dev->dev_alias[read_bytes - 1] == '\n')
1522 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001523
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001524 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001525
Andy Grover6708bb22011-06-08 10:36:43 -07001526 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001528 config_item_name(&dev->dev_group.cg_item),
1529 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530
1531 return read_bytes;
1532}
1533
1534static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1535 .attr = { .ca_owner = THIS_MODULE,
1536 .ca_name = "alias",
1537 .ca_mode = S_IRUGO | S_IWUSR },
1538 .show = target_core_show_dev_alias,
1539 .store = target_core_store_dev_alias,
1540};
1541
1542static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1543{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001544 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001546 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 return 0;
1548
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001549 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550}
1551
1552static ssize_t target_core_store_dev_udev_path(
1553 void *p,
1554 const char *page,
1555 size_t count)
1556{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001557 struct se_device *dev = p;
1558 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559 ssize_t read_bytes;
1560
1561 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001562 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001563 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1564 SE_UDEV_PATH_LEN-1);
1565 return -EINVAL;
1566 }
1567
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001568 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001569 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001570 if (!read_bytes)
1571 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001572 if (dev->udev_path[read_bytes - 1] == '\n')
1573 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001574
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001575 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001576
Andy Grover6708bb22011-06-08 10:36:43 -07001577 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001579 config_item_name(&dev->dev_group.cg_item),
1580 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581
1582 return read_bytes;
1583}
1584
1585static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1586 .attr = { .ca_owner = THIS_MODULE,
1587 .ca_name = "udev_path",
1588 .ca_mode = S_IRUGO | S_IWUSR },
1589 .show = target_core_show_dev_udev_path,
1590 .store = target_core_store_dev_udev_path,
1591};
1592
Andy Grover64146db2013-04-30 11:59:15 -07001593static ssize_t target_core_show_dev_enable(void *p, char *page)
1594{
1595 struct se_device *dev = p;
1596
1597 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1598}
1599
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600static ssize_t target_core_store_dev_enable(
1601 void *p,
1602 const char *page,
1603 size_t count)
1604{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001605 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001607 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001608
1609 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001610 if (!ptr) {
1611 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001612 " is \"1\"\n");
1613 return -EINVAL;
1614 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001616 ret = target_configure_device(dev);
1617 if (ret)
1618 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001619 return count;
1620}
1621
1622static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1623 .attr = { .ca_owner = THIS_MODULE,
1624 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001625 .ca_mode = S_IRUGO | S_IWUSR },
1626 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 .store = target_core_store_dev_enable,
1628};
1629
1630static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1631{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001632 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001633 struct config_item *lu_ci;
1634 struct t10_alua_lu_gp *lu_gp;
1635 struct t10_alua_lu_gp_member *lu_gp_mem;
1636 ssize_t len = 0;
1637
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001639 if (!lu_gp_mem)
1640 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001641
1642 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1643 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001644 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 lu_ci = &lu_gp->lu_gp_group.cg_item;
1646 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1647 config_item_name(lu_ci), lu_gp->lu_gp_id);
1648 }
1649 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1650
1651 return len;
1652}
1653
1654static ssize_t target_core_store_alua_lu_gp(
1655 void *p,
1656 const char *page,
1657 size_t count)
1658{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001659 struct se_device *dev = p;
1660 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1662 struct t10_alua_lu_gp_member *lu_gp_mem;
1663 unsigned char buf[LU_GROUP_NAME_BUF];
1664 int move = 0;
1665
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001666 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1667 if (!lu_gp_mem)
1668 return 0;
1669
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001671 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 return -EINVAL;
1673 }
1674 memset(buf, 0, LU_GROUP_NAME_BUF);
1675 memcpy(buf, page, count);
1676 /*
1677 * Any ALUA logical unit alias besides "NULL" means we will be
1678 * making a new group association.
1679 */
1680 if (strcmp(strstrip(buf), "NULL")) {
1681 /*
1682 * core_alua_get_lu_gp_by_name() will increment reference to
1683 * struct t10_alua_lu_gp. This reference is released with
1684 * core_alua_get_lu_gp_by_name below().
1685 */
1686 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001687 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001688 return -ENODEV;
1689 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690
1691 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1692 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001693 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001694 /*
1695 * Clearing an existing lu_gp association, and replacing
1696 * with NULL
1697 */
Andy Grover6708bb22011-06-08 10:36:43 -07001698 if (!lu_gp_new) {
1699 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1701 " %hu\n",
1702 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001703 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 config_item_name(&lu_gp->lu_gp_group.cg_item),
1705 lu_gp->lu_gp_id);
1706
1707 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1708 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1709
1710 return count;
1711 }
1712 /*
1713 * Removing existing association of lu_gp_mem with lu_gp
1714 */
1715 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1716 move = 1;
1717 }
1718 /*
1719 * Associate lu_gp_mem with lu_gp_new.
1720 */
1721 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1722 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1723
Andy Grover6708bb22011-06-08 10:36:43 -07001724 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 " core/alua/lu_gps/%s, ID: %hu\n",
1726 (move) ? "Moving" : "Adding",
1727 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001728 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1730 lu_gp_new->lu_gp_id);
1731
1732 core_alua_put_lu_gp_from_name(lu_gp_new);
1733 return count;
1734}
1735
1736static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1737 .attr = { .ca_owner = THIS_MODULE,
1738 .ca_name = "alua_lu_gp",
1739 .ca_mode = S_IRUGO | S_IWUSR },
1740 .show = target_core_show_alua_lu_gp,
1741 .store = target_core_store_alua_lu_gp,
1742};
1743
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001744static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1745{
1746 struct se_device *dev = p;
1747 struct t10_alua_lba_map *map;
1748 struct t10_alua_lba_map_member *mem;
1749 char *b = page;
1750 int bl = 0;
1751 char state;
1752
1753 spin_lock(&dev->t10_alua.lba_map_lock);
1754 if (!list_empty(&dev->t10_alua.lba_map_list))
1755 bl += sprintf(b + bl, "%u %u\n",
1756 dev->t10_alua.lba_map_segment_size,
1757 dev->t10_alua.lba_map_segment_multiplier);
1758 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1759 bl += sprintf(b + bl, "%llu %llu",
1760 map->lba_map_first_lba, map->lba_map_last_lba);
1761 list_for_each_entry(mem, &map->lba_map_mem_list,
1762 lba_map_mem_list) {
1763 switch (mem->lba_map_mem_alua_state) {
1764 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1765 state = 'O';
1766 break;
1767 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1768 state = 'A';
1769 break;
1770 case ALUA_ACCESS_STATE_STANDBY:
1771 state = 'S';
1772 break;
1773 case ALUA_ACCESS_STATE_UNAVAILABLE:
1774 state = 'U';
1775 break;
1776 default:
1777 state = '.';
1778 break;
1779 }
1780 bl += sprintf(b + bl, " %d:%c",
1781 mem->lba_map_mem_alua_pg_id, state);
1782 }
1783 bl += sprintf(b + bl, "\n");
1784 }
1785 spin_unlock(&dev->t10_alua.lba_map_lock);
1786 return bl;
1787}
1788
1789static ssize_t target_core_store_dev_lba_map(
1790 void *p,
1791 const char *page,
1792 size_t count)
1793{
1794 struct se_device *dev = p;
1795 struct t10_alua_lba_map *lba_map = NULL;
1796 struct list_head lba_list;
1797 char *map_entries, *ptr;
1798 char state;
1799 int pg_num = -1, pg;
1800 int ret = 0, num = 0, pg_id, alua_state;
1801 unsigned long start_lba = -1, end_lba = -1;
1802 unsigned long segment_size = -1, segment_mult = -1;
1803
1804 map_entries = kstrdup(page, GFP_KERNEL);
1805 if (!map_entries)
1806 return -ENOMEM;
1807
1808 INIT_LIST_HEAD(&lba_list);
1809 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1810 if (!*ptr)
1811 continue;
1812
1813 if (num == 0) {
1814 if (sscanf(ptr, "%lu %lu\n",
1815 &segment_size, &segment_mult) != 2) {
1816 pr_err("Invalid line %d\n", num);
1817 ret = -EINVAL;
1818 break;
1819 }
1820 num++;
1821 continue;
1822 }
1823 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1824 pr_err("Invalid line %d\n", num);
1825 ret = -EINVAL;
1826 break;
1827 }
1828 ptr = strchr(ptr, ' ');
1829 if (!ptr) {
1830 pr_err("Invalid line %d, missing end lba\n", num);
1831 ret = -EINVAL;
1832 break;
1833 }
1834 ptr++;
1835 ptr = strchr(ptr, ' ');
1836 if (!ptr) {
1837 pr_err("Invalid line %d, missing state definitions\n",
1838 num);
1839 ret = -EINVAL;
1840 break;
1841 }
1842 ptr++;
1843 lba_map = core_alua_allocate_lba_map(&lba_list,
1844 start_lba, end_lba);
1845 if (IS_ERR(lba_map)) {
1846 ret = PTR_ERR(lba_map);
1847 break;
1848 }
1849 pg = 0;
1850 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1851 switch (state) {
1852 case 'O':
1853 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1854 break;
1855 case 'A':
1856 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1857 break;
1858 case 'S':
1859 alua_state = ALUA_ACCESS_STATE_STANDBY;
1860 break;
1861 case 'U':
1862 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1863 break;
1864 default:
1865 pr_err("Invalid ALUA state '%c'\n", state);
1866 ret = -EINVAL;
1867 goto out;
1868 }
1869
1870 ret = core_alua_allocate_lba_map_mem(lba_map,
1871 pg_id, alua_state);
1872 if (ret) {
1873 pr_err("Invalid target descriptor %d:%c "
1874 "at line %d\n",
1875 pg_id, state, num);
1876 break;
1877 }
1878 pg++;
1879 ptr = strchr(ptr, ' ');
1880 if (ptr)
1881 ptr++;
1882 else
1883 break;
1884 }
1885 if (pg_num == -1)
1886 pg_num = pg;
1887 else if (pg != pg_num) {
1888 pr_err("Only %d from %d port groups definitions "
1889 "at line %d\n", pg, pg_num, num);
1890 ret = -EINVAL;
1891 break;
1892 }
1893 num++;
1894 }
1895out:
1896 if (ret) {
1897 core_alua_free_lba_map(&lba_list);
1898 count = ret;
1899 } else
1900 core_alua_set_lba_map(dev, &lba_list,
1901 segment_size, segment_mult);
1902 kfree(map_entries);
1903 return count;
1904}
1905
1906static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1907 .attr = { .ca_owner = THIS_MODULE,
1908 .ca_name = "lba_map",
1909 .ca_mode = S_IRUGO | S_IWUSR },
1910 .show = target_core_show_dev_lba_map,
1911 .store = target_core_store_dev_lba_map,
1912};
1913
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001914static struct configfs_attribute *lio_core_dev_attrs[] = {
1915 &target_core_attr_dev_info.attr,
1916 &target_core_attr_dev_control.attr,
1917 &target_core_attr_dev_alias.attr,
1918 &target_core_attr_dev_udev_path.attr,
1919 &target_core_attr_dev_enable.attr,
1920 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001921 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 NULL,
1923};
1924
1925static void target_core_dev_release(struct config_item *item)
1926{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001927 struct config_group *dev_cg = to_config_group(item);
1928 struct se_device *dev =
1929 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001930
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001932 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933}
1934
1935static ssize_t target_core_dev_show(struct config_item *item,
1936 struct configfs_attribute *attr,
1937 char *page)
1938{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001939 struct config_group *dev_cg = to_config_group(item);
1940 struct se_device *dev =
1941 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 struct target_core_configfs_attribute *tc_attr = container_of(
1943 attr, struct target_core_configfs_attribute, attr);
1944
Andy Grover6708bb22011-06-08 10:36:43 -07001945 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 return -EINVAL;
1947
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001948 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949}
1950
1951static ssize_t target_core_dev_store(struct config_item *item,
1952 struct configfs_attribute *attr,
1953 const char *page, size_t count)
1954{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001955 struct config_group *dev_cg = to_config_group(item);
1956 struct se_device *dev =
1957 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 struct target_core_configfs_attribute *tc_attr = container_of(
1959 attr, struct target_core_configfs_attribute, attr);
1960
Andy Grover6708bb22011-06-08 10:36:43 -07001961 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 return -EINVAL;
1963
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001964 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965}
1966
1967static struct configfs_item_operations target_core_dev_item_ops = {
1968 .release = target_core_dev_release,
1969 .show_attribute = target_core_dev_show,
1970 .store_attribute = target_core_dev_store,
1971};
1972
1973static struct config_item_type target_core_dev_cit = {
1974 .ct_item_ops = &target_core_dev_item_ops,
1975 .ct_attrs = lio_core_dev_attrs,
1976 .ct_owner = THIS_MODULE,
1977};
1978
1979/* End functions for struct config_item_type target_core_dev_cit */
1980
1981/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1982
1983CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1984#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1985static struct target_core_alua_lu_gp_attribute \
1986 target_core_alua_lu_gp_##_name = \
1987 __CONFIGFS_EATTR(_name, _mode, \
1988 target_core_alua_lu_gp_show_attr_##_name, \
1989 target_core_alua_lu_gp_store_attr_##_name);
1990
1991#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1992static struct target_core_alua_lu_gp_attribute \
1993 target_core_alua_lu_gp_##_name = \
1994 __CONFIGFS_EATTR_RO(_name, \
1995 target_core_alua_lu_gp_show_attr_##_name);
1996
1997/*
1998 * lu_gp_id
1999 */
2000static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2001 struct t10_alua_lu_gp *lu_gp,
2002 char *page)
2003{
Andy Grover6708bb22011-06-08 10:36:43 -07002004 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005 return 0;
2006
2007 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2008}
2009
2010static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2011 struct t10_alua_lu_gp *lu_gp,
2012 const char *page,
2013 size_t count)
2014{
2015 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2016 unsigned long lu_gp_id;
2017 int ret;
2018
Jingoo Han57103d72013-07-19 16:22:19 +09002019 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002021 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002022 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002023 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024 }
2025 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002026 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002027 " 0x0000ffff\n", lu_gp_id);
2028 return -EINVAL;
2029 }
2030
2031 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2032 if (ret < 0)
2033 return -EINVAL;
2034
Andy Grover6708bb22011-06-08 10:36:43 -07002035 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2037 config_item_name(&alua_lu_gp_cg->cg_item),
2038 lu_gp->lu_gp_id);
2039
2040 return count;
2041}
2042
2043SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2044
2045/*
2046 * members
2047 */
2048static ssize_t target_core_alua_lu_gp_show_attr_members(
2049 struct t10_alua_lu_gp *lu_gp,
2050 char *page)
2051{
2052 struct se_device *dev;
2053 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054 struct t10_alua_lu_gp_member *lu_gp_mem;
2055 ssize_t len = 0, cur_len;
2056 unsigned char buf[LU_GROUP_NAME_BUF];
2057
2058 memset(buf, 0, LU_GROUP_NAME_BUF);
2059
2060 spin_lock(&lu_gp->lu_gp_lock);
2061 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2062 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002063 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064
2065 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2066 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002067 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002068 cur_len++; /* Extra byte for NULL terminator */
2069
2070 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002071 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072 "_members buffer\n");
2073 break;
2074 }
2075 memcpy(page+len, buf, cur_len);
2076 len += cur_len;
2077 }
2078 spin_unlock(&lu_gp->lu_gp_lock);
2079
2080 return len;
2081}
2082
2083SE_DEV_ALUA_LU_ATTR_RO(members);
2084
2085CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2086
2087static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2088 &target_core_alua_lu_gp_lu_gp_id.attr,
2089 &target_core_alua_lu_gp_members.attr,
2090 NULL,
2091};
2092
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002093static void target_core_alua_lu_gp_release(struct config_item *item)
2094{
2095 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2096 struct t10_alua_lu_gp, lu_gp_group);
2097
2098 core_alua_free_lu_gp(lu_gp);
2099}
2100
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002101static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002102 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002103 .show_attribute = target_core_alua_lu_gp_attr_show,
2104 .store_attribute = target_core_alua_lu_gp_attr_store,
2105};
2106
2107static struct config_item_type target_core_alua_lu_gp_cit = {
2108 .ct_item_ops = &target_core_alua_lu_gp_ops,
2109 .ct_attrs = target_core_alua_lu_gp_attrs,
2110 .ct_owner = THIS_MODULE,
2111};
2112
2113/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2114
2115/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2116
2117static struct config_group *target_core_alua_create_lu_gp(
2118 struct config_group *group,
2119 const char *name)
2120{
2121 struct t10_alua_lu_gp *lu_gp;
2122 struct config_group *alua_lu_gp_cg = NULL;
2123 struct config_item *alua_lu_gp_ci = NULL;
2124
2125 lu_gp = core_alua_allocate_lu_gp(name, 0);
2126 if (IS_ERR(lu_gp))
2127 return NULL;
2128
2129 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2130 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2131
2132 config_group_init_type_name(alua_lu_gp_cg, name,
2133 &target_core_alua_lu_gp_cit);
2134
Andy Grover6708bb22011-06-08 10:36:43 -07002135 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136 " Group: core/alua/lu_gps/%s\n",
2137 config_item_name(alua_lu_gp_ci));
2138
2139 return alua_lu_gp_cg;
2140
2141}
2142
2143static void target_core_alua_drop_lu_gp(
2144 struct config_group *group,
2145 struct config_item *item)
2146{
2147 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2148 struct t10_alua_lu_gp, lu_gp_group);
2149
Andy Grover6708bb22011-06-08 10:36:43 -07002150 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002151 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2152 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002153 /*
2154 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2155 * -> target_core_alua_lu_gp_release()
2156 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002158}
2159
2160static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2161 .make_group = &target_core_alua_create_lu_gp,
2162 .drop_item = &target_core_alua_drop_lu_gp,
2163};
2164
2165static struct config_item_type target_core_alua_lu_gps_cit = {
2166 .ct_item_ops = NULL,
2167 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2168 .ct_owner = THIS_MODULE,
2169};
2170
2171/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2172
2173/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2174
2175CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2176#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2177static struct target_core_alua_tg_pt_gp_attribute \
2178 target_core_alua_tg_pt_gp_##_name = \
2179 __CONFIGFS_EATTR(_name, _mode, \
2180 target_core_alua_tg_pt_gp_show_attr_##_name, \
2181 target_core_alua_tg_pt_gp_store_attr_##_name);
2182
2183#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2184static struct target_core_alua_tg_pt_gp_attribute \
2185 target_core_alua_tg_pt_gp_##_name = \
2186 __CONFIGFS_EATTR_RO(_name, \
2187 target_core_alua_tg_pt_gp_show_attr_##_name);
2188
2189/*
2190 * alua_access_state
2191 */
2192static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2193 struct t10_alua_tg_pt_gp *tg_pt_gp,
2194 char *page)
2195{
2196 return sprintf(page, "%d\n",
2197 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2198}
2199
2200static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2201 struct t10_alua_tg_pt_gp *tg_pt_gp,
2202 const char *page,
2203 size_t count)
2204{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002205 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206 unsigned long tmp;
2207 int new_state, ret;
2208
Andy Grover6708bb22011-06-08 10:36:43 -07002209 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002210 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002211 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2212 return -EINVAL;
2213 }
2214
Jingoo Han57103d72013-07-19 16:22:19 +09002215 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002217 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002218 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002219 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002220 }
2221 new_state = (int)tmp;
2222
Hannes Reinecke125d0112013-11-19 09:07:46 +01002223 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2224 pr_err("Unable to process implicit configfs ALUA"
2225 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226 return -EINVAL;
2227 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002228 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2229 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2230 /* LBA DEPENDENT is only allowed with implicit ALUA */
2231 pr_err("Unable to process implicit configfs ALUA transition"
2232 " while explicit ALUA management is enabled\n");
2233 return -EINVAL;
2234 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002236 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 NULL, NULL, new_state, 0);
2238 return (!ret) ? count : -EINVAL;
2239}
2240
2241SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2242
2243/*
2244 * alua_access_status
2245 */
2246static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2247 struct t10_alua_tg_pt_gp *tg_pt_gp,
2248 char *page)
2249{
2250 return sprintf(page, "%s\n",
2251 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2252}
2253
2254static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2255 struct t10_alua_tg_pt_gp *tg_pt_gp,
2256 const char *page,
2257 size_t count)
2258{
2259 unsigned long tmp;
2260 int new_status, ret;
2261
Andy Grover6708bb22011-06-08 10:36:43 -07002262 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2263 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002264 " valid tg_pt_gp ID: %hu\n",
2265 tg_pt_gp->tg_pt_gp_valid_id);
2266 return -EINVAL;
2267 }
2268
Jingoo Han57103d72013-07-19 16:22:19 +09002269 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002270 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002271 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002272 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002273 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002274 }
2275 new_status = (int)tmp;
2276
2277 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002278 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2279 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002280 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281 new_status);
2282 return -EINVAL;
2283 }
2284
2285 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2286 return count;
2287}
2288
2289SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2290
2291/*
2292 * alua_access_type
2293 */
2294static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2295 struct t10_alua_tg_pt_gp *tg_pt_gp,
2296 char *page)
2297{
2298 return core_alua_show_access_type(tg_pt_gp, page);
2299}
2300
2301static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2302 struct t10_alua_tg_pt_gp *tg_pt_gp,
2303 const char *page,
2304 size_t count)
2305{
2306 return core_alua_store_access_type(tg_pt_gp, page, count);
2307}
2308
2309SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2310
2311/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002312 * alua_supported_states
2313 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002314
2315#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2316static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2317 struct t10_alua_tg_pt_gp *t, char *p) \
2318{ \
2319 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002320}
2321
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002322#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2323static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2324 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2325{ \
2326 unsigned long tmp; \
2327 int ret; \
2328 \
2329 if (!t->tg_pt_gp_valid_id) { \
2330 pr_err("Unable to do set ##_name ALUA state on non" \
2331 " valid tg_pt_gp ID: %hu\n", \
2332 t->tg_pt_gp_valid_id); \
2333 return -EINVAL; \
2334 } \
2335 \
2336 ret = kstrtoul(p, 0, &tmp); \
2337 if (ret < 0) { \
2338 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2339 return -EINVAL; \
2340 } \
2341 if (tmp > 1) { \
2342 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2343 return -EINVAL; \
2344 } \
2345 if (!tmp) \
2346 t->_var |= _bit; \
2347 else \
2348 t->_var &= ~_bit; \
2349 \
2350 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002351}
2352
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002353SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2354 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2355SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2356 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2357SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2358
2359SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2360 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2361SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2362 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2363SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2364
2365SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2366 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2367SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2368 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002369SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002370
2371SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2372 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2373SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2374 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2375SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2376
2377SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2378 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2379SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2380 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2381SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2382
2383SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2384 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2385SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2386 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2387SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2388
2389SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2390 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2391SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2392 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2393SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002394
2395/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396 * alua_write_metadata
2397 */
2398static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2399 struct t10_alua_tg_pt_gp *tg_pt_gp,
2400 char *page)
2401{
2402 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2403}
2404
2405static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2406 struct t10_alua_tg_pt_gp *tg_pt_gp,
2407 const char *page,
2408 size_t count)
2409{
2410 unsigned long tmp;
2411 int ret;
2412
Jingoo Han57103d72013-07-19 16:22:19 +09002413 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002415 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002416 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002417 }
2418
2419 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002420 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002421 " %lu\n", tmp);
2422 return -EINVAL;
2423 }
2424 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2425
2426 return count;
2427}
2428
2429SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2430
2431
2432
2433/*
2434 * nonop_delay_msecs
2435 */
2436static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2437 struct t10_alua_tg_pt_gp *tg_pt_gp,
2438 char *page)
2439{
2440 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2441
2442}
2443
2444static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2445 struct t10_alua_tg_pt_gp *tg_pt_gp,
2446 const char *page,
2447 size_t count)
2448{
2449 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2450}
2451
2452SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2453
2454/*
2455 * trans_delay_msecs
2456 */
2457static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2458 struct t10_alua_tg_pt_gp *tg_pt_gp,
2459 char *page)
2460{
2461 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2462}
2463
2464static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2465 struct t10_alua_tg_pt_gp *tg_pt_gp,
2466 const char *page,
2467 size_t count)
2468{
2469 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2470}
2471
2472SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2473
2474/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002475 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002476 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002477static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002478 struct t10_alua_tg_pt_gp *tg_pt_gp,
2479 char *page)
2480{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002481 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002482}
2483
Hannes Reinecke125d0112013-11-19 09:07:46 +01002484static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002485 struct t10_alua_tg_pt_gp *tg_pt_gp,
2486 const char *page,
2487 size_t count)
2488{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002489 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002490}
2491
Hannes Reinecke125d0112013-11-19 09:07:46 +01002492SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002493
2494/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495 * preferred
2496 */
2497
2498static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2499 struct t10_alua_tg_pt_gp *tg_pt_gp,
2500 char *page)
2501{
2502 return core_alua_show_preferred_bit(tg_pt_gp, page);
2503}
2504
2505static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2506 struct t10_alua_tg_pt_gp *tg_pt_gp,
2507 const char *page,
2508 size_t count)
2509{
2510 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2511}
2512
2513SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2514
2515/*
2516 * tg_pt_gp_id
2517 */
2518static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2519 struct t10_alua_tg_pt_gp *tg_pt_gp,
2520 char *page)
2521{
Andy Grover6708bb22011-06-08 10:36:43 -07002522 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002523 return 0;
2524
2525 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2526}
2527
2528static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2529 struct t10_alua_tg_pt_gp *tg_pt_gp,
2530 const char *page,
2531 size_t count)
2532{
2533 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2534 unsigned long tg_pt_gp_id;
2535 int ret;
2536
Jingoo Han57103d72013-07-19 16:22:19 +09002537 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002539 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002541 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 }
2543 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002544 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002545 " 0x0000ffff\n", tg_pt_gp_id);
2546 return -EINVAL;
2547 }
2548
2549 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2550 if (ret < 0)
2551 return -EINVAL;
2552
Andy Grover6708bb22011-06-08 10:36:43 -07002553 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2555 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2556 tg_pt_gp->tg_pt_gp_id);
2557
2558 return count;
2559}
2560
2561SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2562
2563/*
2564 * members
2565 */
2566static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2567 struct t10_alua_tg_pt_gp *tg_pt_gp,
2568 char *page)
2569{
2570 struct se_port *port;
2571 struct se_portal_group *tpg;
2572 struct se_lun *lun;
2573 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2574 ssize_t len = 0, cur_len;
2575 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2576
2577 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2578
2579 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2580 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2581 tg_pt_gp_mem_list) {
2582 port = tg_pt_gp_mem->tg_pt;
2583 tpg = port->sep_tpg;
2584 lun = port->sep_lun;
2585
2586 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002587 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2588 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2589 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590 config_item_name(&lun->lun_group.cg_item));
2591 cur_len++; /* Extra byte for NULL terminator */
2592
2593 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002594 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595 "_members buffer\n");
2596 break;
2597 }
2598 memcpy(page+len, buf, cur_len);
2599 len += cur_len;
2600 }
2601 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2602
2603 return len;
2604}
2605
2606SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2607
2608CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2609 tg_pt_gp_group);
2610
2611static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2612 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2613 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2614 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002615 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2616 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2617 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2618 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2619 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2620 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2621 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2623 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2624 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002625 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 &target_core_alua_tg_pt_gp_preferred.attr,
2627 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2628 &target_core_alua_tg_pt_gp_members.attr,
2629 NULL,
2630};
2631
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002632static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2633{
2634 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2635 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2636
2637 core_alua_free_tg_pt_gp(tg_pt_gp);
2638}
2639
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002641 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002642 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2643 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2644};
2645
2646static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2647 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2648 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2649 .ct_owner = THIS_MODULE,
2650};
2651
2652/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2653
2654/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2655
2656static struct config_group *target_core_alua_create_tg_pt_gp(
2657 struct config_group *group,
2658 const char *name)
2659{
2660 struct t10_alua *alua = container_of(group, struct t10_alua,
2661 alua_tg_pt_gps_group);
2662 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002663 struct config_group *alua_tg_pt_gp_cg = NULL;
2664 struct config_item *alua_tg_pt_gp_ci = NULL;
2665
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002666 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002667 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668 return NULL;
2669
2670 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2671 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2672
2673 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2674 &target_core_alua_tg_pt_gp_cit);
2675
Andy Grover6708bb22011-06-08 10:36:43 -07002676 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677 " Group: alua/tg_pt_gps/%s\n",
2678 config_item_name(alua_tg_pt_gp_ci));
2679
2680 return alua_tg_pt_gp_cg;
2681}
2682
2683static void target_core_alua_drop_tg_pt_gp(
2684 struct config_group *group,
2685 struct config_item *item)
2686{
2687 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2688 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2689
Andy Grover6708bb22011-06-08 10:36:43 -07002690 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2692 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002693 /*
2694 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2695 * -> target_core_alua_tg_pt_gp_release().
2696 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002698}
2699
2700static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2701 .make_group = &target_core_alua_create_tg_pt_gp,
2702 .drop_item = &target_core_alua_drop_tg_pt_gp,
2703};
2704
2705static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2706 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2707 .ct_owner = THIS_MODULE,
2708};
2709
2710/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2711
2712/* Start functions for struct config_item_type target_core_alua_cit */
2713
2714/*
2715 * target_core_alua_cit is a ConfigFS group that lives under
2716 * /sys/kernel/config/target/core/alua. There are default groups
2717 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2718 * target_core_alua_cit in target_core_init_configfs() below.
2719 */
2720static struct config_item_type target_core_alua_cit = {
2721 .ct_item_ops = NULL,
2722 .ct_attrs = NULL,
2723 .ct_owner = THIS_MODULE,
2724};
2725
2726/* End functions for struct config_item_type target_core_alua_cit */
2727
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002728/* Start functions for struct config_item_type target_core_stat_cit */
2729
2730static struct config_group *target_core_stat_mkdir(
2731 struct config_group *group,
2732 const char *name)
2733{
2734 return ERR_PTR(-ENOSYS);
2735}
2736
2737static void target_core_stat_rmdir(
2738 struct config_group *group,
2739 struct config_item *item)
2740{
2741 return;
2742}
2743
2744static struct configfs_group_operations target_core_stat_group_ops = {
2745 .make_group = &target_core_stat_mkdir,
2746 .drop_item = &target_core_stat_rmdir,
2747};
2748
2749static struct config_item_type target_core_stat_cit = {
2750 .ct_group_ops = &target_core_stat_group_ops,
2751 .ct_owner = THIS_MODULE,
2752};
2753
2754/* End functions for struct config_item_type target_core_stat_cit */
2755
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756/* Start functions for struct config_item_type target_core_hba_cit */
2757
2758static struct config_group *target_core_make_subdev(
2759 struct config_group *group,
2760 const char *name)
2761{
2762 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763 struct se_subsystem_api *t;
2764 struct config_item *hba_ci = &group->cg_item;
2765 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002766 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002768 struct config_group *dev_stat_grp = NULL;
2769 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002771 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2772 if (ret)
2773 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774 /*
2775 * Locate the struct se_subsystem_api from parent's struct se_hba.
2776 */
2777 t = hba->transport;
2778
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002779 dev = target_alloc_device(hba, name);
2780 if (!dev)
2781 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002782
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002783 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002784
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002785 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002786 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002787 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002788 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002790 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2791 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002793 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002795 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002797 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002799 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002800 "statistics", &target_core_stat_cit);
2801
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002802 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2803 dev_cg->default_groups[1] = &dev->dev_pr_group;
2804 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2805 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2806 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002807 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002808 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002809 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002811 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002812 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002813 goto out_free_dev_cg_default_groups;
2814 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002816 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002817 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002819 if (!tg_pt_gp_cg->default_groups) {
2820 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002821 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002822 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 }
2824
2825 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2826 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2827 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2828 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002829 /*
2830 * Add core/$HBA/$DEV/statistics/ default groups
2831 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002832 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002833 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002834 GFP_KERNEL);
2835 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002836 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002837 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002838 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002839 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840
2841 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002842 return dev_cg;
2843
2844out_free_tg_pt_gp_cg_default_groups:
2845 kfree(tg_pt_gp_cg->default_groups);
2846out_free_tg_pt_gp:
2847 core_alua_free_tg_pt_gp(tg_pt_gp);
2848out_free_dev_cg_default_groups:
2849 kfree(dev_cg->default_groups);
2850out_free_device:
2851 target_free_device(dev);
2852out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002854 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855}
2856
2857static void target_core_drop_subdev(
2858 struct config_group *group,
2859 struct config_item *item)
2860{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002861 struct config_group *dev_cg = to_config_group(item);
2862 struct se_device *dev =
2863 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002866 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002867 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002869 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002871 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002873 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002874 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2875 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2876 dev_stat_grp->default_groups[i] = NULL;
2877 config_item_put(df_item);
2878 }
2879 kfree(dev_stat_grp->default_groups);
2880
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002881 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2883 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2884 tg_pt_gp_cg->default_groups[i] = NULL;
2885 config_item_put(df_item);
2886 }
2887 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002888 /*
2889 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2890 * directly from target_core_alua_tg_pt_gp_release().
2891 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002892 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002894 for (i = 0; dev_cg->default_groups[i]; i++) {
2895 df_item = &dev_cg->default_groups[i]->cg_item;
2896 dev_cg->default_groups[i] = NULL;
2897 config_item_put(df_item);
2898 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002900 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002901 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002902 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904}
2905
2906static struct configfs_group_operations target_core_hba_group_ops = {
2907 .make_group = target_core_make_subdev,
2908 .drop_item = target_core_drop_subdev,
2909};
2910
2911CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2912#define SE_HBA_ATTR(_name, _mode) \
2913static struct target_core_hba_attribute \
2914 target_core_hba_##_name = \
2915 __CONFIGFS_EATTR(_name, _mode, \
2916 target_core_hba_show_attr_##_name, \
2917 target_core_hba_store_attr_##_name);
2918
2919#define SE_HBA_ATTR_RO(_name) \
2920static struct target_core_hba_attribute \
2921 target_core_hba_##_name = \
2922 __CONFIGFS_EATTR_RO(_name, \
2923 target_core_hba_show_attr_##_name);
2924
2925static ssize_t target_core_hba_show_attr_hba_info(
2926 struct se_hba *hba,
2927 char *page)
2928{
2929 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2930 hba->hba_id, hba->transport->name,
2931 TARGET_CORE_CONFIGFS_VERSION);
2932}
2933
2934SE_HBA_ATTR_RO(hba_info);
2935
2936static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2937 char *page)
2938{
2939 int hba_mode = 0;
2940
2941 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2942 hba_mode = 1;
2943
2944 return sprintf(page, "%d\n", hba_mode);
2945}
2946
2947static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2948 const char *page, size_t count)
2949{
2950 struct se_subsystem_api *transport = hba->transport;
2951 unsigned long mode_flag;
2952 int ret;
2953
2954 if (transport->pmode_enable_hba == NULL)
2955 return -EINVAL;
2956
Jingoo Han57103d72013-07-19 16:22:19 +09002957 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002959 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002960 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961 }
2962
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002963 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002964 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 return -EINVAL;
2966 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967
2968 ret = transport->pmode_enable_hba(hba, mode_flag);
2969 if (ret < 0)
2970 return -EINVAL;
2971 if (ret > 0)
2972 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2973 else if (ret == 0)
2974 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2975
2976 return count;
2977}
2978
2979SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2980
2981CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2982
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002983static void target_core_hba_release(struct config_item *item)
2984{
2985 struct se_hba *hba = container_of(to_config_group(item),
2986 struct se_hba, hba_group);
2987 core_delete_hba(hba);
2988}
2989
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990static struct configfs_attribute *target_core_hba_attrs[] = {
2991 &target_core_hba_hba_info.attr,
2992 &target_core_hba_hba_mode.attr,
2993 NULL,
2994};
2995
2996static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002997 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002998 .show_attribute = target_core_hba_attr_show,
2999 .store_attribute = target_core_hba_attr_store,
3000};
3001
3002static struct config_item_type target_core_hba_cit = {
3003 .ct_item_ops = &target_core_hba_item_ops,
3004 .ct_group_ops = &target_core_hba_group_ops,
3005 .ct_attrs = target_core_hba_attrs,
3006 .ct_owner = THIS_MODULE,
3007};
3008
3009static struct config_group *target_core_call_addhbatotarget(
3010 struct config_group *group,
3011 const char *name)
3012{
3013 char *se_plugin_str, *str, *str2;
3014 struct se_hba *hba;
3015 char buf[TARGET_CORE_NAME_MAX_LEN];
3016 unsigned long plugin_dep_id = 0;
3017 int ret;
3018
3019 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003020 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003021 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003022 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3023 TARGET_CORE_NAME_MAX_LEN);
3024 return ERR_PTR(-ENAMETOOLONG);
3025 }
3026 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3027
3028 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003029 if (!str) {
3030 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003031 return ERR_PTR(-EINVAL);
3032 }
3033 se_plugin_str = buf;
3034 /*
3035 * Special case for subsystem plugins that have "_" in their names.
3036 * Namely rd_direct and rd_mcp..
3037 */
3038 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003039 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003040 *str2 = '\0'; /* Terminate for *se_plugin_str */
3041 str2++; /* Skip to start of plugin dependent ID */
3042 str = str2;
3043 } else {
3044 *str = '\0'; /* Terminate for *se_plugin_str */
3045 str++; /* Skip to start of plugin dependent ID */
3046 }
3047
Jingoo Han57103d72013-07-19 16:22:19 +09003048 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003049 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003050 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003051 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003052 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003053 }
3054 /*
3055 * Load up TCM subsystem plugins if they have not already been loaded.
3056 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003057 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003058
3059 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3060 if (IS_ERR(hba))
3061 return ERR_CAST(hba);
3062
3063 config_group_init_type_name(&hba->hba_group, name,
3064 &target_core_hba_cit);
3065
3066 return &hba->hba_group;
3067}
3068
3069static void target_core_call_delhbafromtarget(
3070 struct config_group *group,
3071 struct config_item *item)
3072{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003073 /*
3074 * core_delete_hba() is called from target_core_hba_item_ops->release()
3075 * -> target_core_hba_release()
3076 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078}
3079
3080static struct configfs_group_operations target_core_group_ops = {
3081 .make_group = target_core_call_addhbatotarget,
3082 .drop_item = target_core_call_delhbafromtarget,
3083};
3084
3085static struct config_item_type target_core_cit = {
3086 .ct_item_ops = NULL,
3087 .ct_group_ops = &target_core_group_ops,
3088 .ct_attrs = NULL,
3089 .ct_owner = THIS_MODULE,
3090};
3091
3092/* Stop functions for struct config_item_type target_core_hba_cit */
3093
Axel Lin54550fa2011-03-14 04:06:09 -07003094static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095{
3096 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3097 struct config_group *lu_gp_cg = NULL;
3098 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003099 struct t10_alua_lu_gp *lu_gp;
3100 int ret;
3101
Andy Grover6708bb22011-06-08 10:36:43 -07003102 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003103 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3104 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3105
3106 subsys = target_core_subsystem[0];
3107 config_group_init(&subsys->su_group);
3108 mutex_init(&subsys->su_mutex);
3109
Andy Grovere3d6f902011-07-19 08:55:10 +00003110 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003111 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003112 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113 /*
3114 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3115 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3116 */
3117 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003118 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003119 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003120 if (!target_cg->default_groups) {
3121 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003122 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 goto out_global;
3124 }
3125
Andy Grovere3d6f902011-07-19 08:55:10 +00003126 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003127 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003128 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003129 target_cg->default_groups[1] = NULL;
3130 /*
3131 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3132 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003133 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003134 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003135 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003136 if (!hba_cg->default_groups) {
3137 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003138 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 goto out_global;
3140 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003141 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003143 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 hba_cg->default_groups[1] = NULL;
3145 /*
3146 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3147 * groups under /sys/kernel/config/target/core/alua/
3148 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003149 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003150 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003152 if (!alua_cg->default_groups) {
3153 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003154 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 goto out_global;
3156 }
3157
Andy Grovere3d6f902011-07-19 08:55:10 +00003158 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003160 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 alua_cg->default_groups[1] = NULL;
3162 /*
3163 * Add core/alua/lu_gps/default_lu_gp
3164 */
3165 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003166 if (IS_ERR(lu_gp)) {
3167 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003169 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170
Andy Grovere3d6f902011-07-19 08:55:10 +00003171 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003172 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003173 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003174 if (!lu_gp_cg->default_groups) {
3175 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003176 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003177 goto out_global;
3178 }
3179
3180 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3181 &target_core_alua_lu_gp_cit);
3182 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3183 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003184 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 /*
3186 * Register the target_core_mod subsystem with configfs.
3187 */
3188 ret = configfs_register_subsystem(subsys);
3189 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003190 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 ret, subsys->su_group.cg_item.ci_namebuf);
3192 goto out_global;
3193 }
Andy Grover6708bb22011-06-08 10:36:43 -07003194 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003195 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3196 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3197 /*
3198 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3199 */
3200 ret = rd_module_init();
3201 if (ret < 0)
3202 goto out;
3203
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003204 ret = core_dev_setup_virtual_lun0();
3205 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 goto out;
3207
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003208 ret = target_xcopy_setup_pt();
3209 if (ret < 0)
3210 goto out;
3211
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003212 return 0;
3213
3214out:
3215 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216 core_dev_release_virtual_lun0();
3217 rd_module_exit();
3218out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003219 if (default_lu_gp) {
3220 core_alua_free_lu_gp(default_lu_gp);
3221 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003222 }
3223 if (lu_gp_cg)
3224 kfree(lu_gp_cg->default_groups);
3225 if (alua_cg)
3226 kfree(alua_cg->default_groups);
3227 if (hba_cg)
3228 kfree(hba_cg->default_groups);
3229 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003230 release_se_kmem_caches();
3231 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232}
3233
Axel Lin54550fa2011-03-14 04:06:09 -07003234static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003235{
3236 struct configfs_subsystem *subsys;
3237 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3238 struct config_item *item;
3239 int i;
3240
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003241 subsys = target_core_subsystem[0];
3242
Andy Grovere3d6f902011-07-19 08:55:10 +00003243 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3245 item = &lu_gp_cg->default_groups[i]->cg_item;
3246 lu_gp_cg->default_groups[i] = NULL;
3247 config_item_put(item);
3248 }
3249 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003250 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251
Andy Grovere3d6f902011-07-19 08:55:10 +00003252 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003253 for (i = 0; alua_cg->default_groups[i]; i++) {
3254 item = &alua_cg->default_groups[i]->cg_item;
3255 alua_cg->default_groups[i] = NULL;
3256 config_item_put(item);
3257 }
3258 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003259 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260
Andy Grovere3d6f902011-07-19 08:55:10 +00003261 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 for (i = 0; hba_cg->default_groups[i]; i++) {
3263 item = &hba_cg->default_groups[i]->cg_item;
3264 hba_cg->default_groups[i] = NULL;
3265 config_item_put(item);
3266 }
3267 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003268 hba_cg->default_groups = NULL;
3269 /*
3270 * We expect subsys->su_group.default_groups to be released
3271 * by configfs subsystem provider logic..
3272 */
3273 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003274 kfree(subsys->su_group.default_groups);
3275
Andy Grovere3d6f902011-07-19 08:55:10 +00003276 core_alua_free_lu_gp(default_lu_gp);
3277 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003278
Andy Grover6708bb22011-06-08 10:36:43 -07003279 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280 " Infrastructure\n");
3281
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 core_dev_release_virtual_lun0();
3283 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003284 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003285 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003286}
3287
3288MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3289MODULE_AUTHOR("nab@Linux-iSCSI.org");
3290MODULE_LICENSE("GPL");
3291
3292module_init(target_core_init_configfs);
3293module_exit(target_core_exit_configfs);