blob: 756def38c77af934a3e16ef3ae9219e67ceb06cc [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 Bellinger131e6ab2014-03-22 14:55:56 -0700460 if (!tfo->aborted_task) {
461 pr_err("Missing tfo->aborted_task()\n");
462 return -EINVAL;
463 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 /*
465 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
466 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
467 * target_core_fabric_configfs.c WWN+TPG group context code.
468 */
Andy Grover6708bb22011-06-08 10:36:43 -0700469 if (!tfo->fabric_make_wwn) {
470 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 return -EINVAL;
472 }
Andy Grover6708bb22011-06-08 10:36:43 -0700473 if (!tfo->fabric_drop_wwn) {
474 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 return -EINVAL;
476 }
Andy Grover6708bb22011-06-08 10:36:43 -0700477 if (!tfo->fabric_make_tpg) {
478 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return -EINVAL;
480 }
Andy Grover6708bb22011-06-08 10:36:43 -0700481 if (!tfo->fabric_drop_tpg) {
482 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 return -EINVAL;
484 }
485
486 return 0;
487}
488
489/*
490 * Called 2nd from fabric module with returned parameter of
491 * struct target_fabric_configfs * from target_fabric_configfs_init().
492 *
493 * Upon a successful registration, the new fabric's struct config_item is
494 * return. Also, a pointer to this struct is set in the passed
495 * struct target_fabric_configfs.
496 */
497int target_fabric_configfs_register(
498 struct target_fabric_configfs *tf)
499{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 int ret;
501
Andy Grover6708bb22011-06-08 10:36:43 -0700502 if (!tf) {
503 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 " pointer\n");
505 return -EINVAL;
506 }
Andy Grover6708bb22011-06-08 10:36:43 -0700507 if (!tf->tf_subsys) {
508 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509 " pointer\n");
510 return -EINVAL;
511 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 ret = target_fabric_tf_ops_check(tf);
513 if (ret < 0)
514 return ret;
515
Andy Grover6708bb22011-06-08 10:36:43 -0700516 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517 ">>>>>>>>>>\n");
518 return 0;
519}
520EXPORT_SYMBOL(target_fabric_configfs_register);
521
522void target_fabric_configfs_deregister(
523 struct target_fabric_configfs *tf)
524{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 struct configfs_subsystem *su;
526
Andy Grover6708bb22011-06-08 10:36:43 -0700527 if (!tf) {
528 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 "configfs\n");
530 return;
531 }
532 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700533 if (!su) {
534 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 " pointer\n");
536 return;
537 }
Andy Grover6708bb22011-06-08 10:36:43 -0700538 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 ">>>>>>>>>>>>\n");
540 mutex_lock(&g_tf_lock);
541 if (atomic_read(&tf->tf_access_cnt)) {
542 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700543 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 tf->tf_name);
545 BUG();
546 }
547 list_del(&tf->tf_list);
548 mutex_unlock(&g_tf_lock);
549
Andy Grover6708bb22011-06-08 10:36:43 -0700550 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 " %s\n", tf->tf_name);
552 tf->tf_module = NULL;
553 tf->tf_subsys = NULL;
554 kfree(tf);
555
Andy Grover6708bb22011-06-08 10:36:43 -0700556 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558}
559EXPORT_SYMBOL(target_fabric_configfs_deregister);
560
561/*##############################################################################
562// Stop functions called by external Target Fabrics Modules
563//############################################################################*/
564
565/* Start functions for struct config_item_type target_core_dev_attrib_cit */
566
567#define DEF_DEV_ATTRIB_SHOW(_name) \
568static ssize_t target_core_dev_show_attr_##_name( \
569 struct se_dev_attrib *da, \
570 char *page) \
571{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400572 return snprintf(page, PAGE_SIZE, "%u\n", \
573 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574}
575
576#define DEF_DEV_ATTRIB_STORE(_name) \
577static ssize_t target_core_dev_store_attr_##_name( \
578 struct se_dev_attrib *da, \
579 const char *page, \
580 size_t count) \
581{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 unsigned long val; \
583 int ret; \
584 \
Jingoo Han57103d72013-07-19 16:22:19 +0900585 ret = kstrtoul(page, 0, &val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586 if (ret < 0) { \
Jingoo Han57103d72013-07-19 16:22:19 +0900587 pr_err("kstrtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 " ret: %d\n", ret); \
589 return -EINVAL; \
590 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400591 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592 \
593 return (!ret) ? count : -EINVAL; \
594}
595
596#define DEF_DEV_ATTRIB(_name) \
597DEF_DEV_ATTRIB_SHOW(_name); \
598DEF_DEV_ATTRIB_STORE(_name);
599
600#define DEF_DEV_ATTRIB_RO(_name) \
601DEF_DEV_ATTRIB_SHOW(_name);
602
603CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
604#define SE_DEV_ATTR(_name, _mode) \
605static struct target_core_dev_attrib_attribute \
606 target_core_dev_attrib_##_name = \
607 __CONFIGFS_EATTR(_name, _mode, \
608 target_core_dev_show_attr_##_name, \
609 target_core_dev_store_attr_##_name);
610
611#define SE_DEV_ATTR_RO(_name); \
612static struct target_core_dev_attrib_attribute \
613 target_core_dev_attrib_##_name = \
614 __CONFIGFS_EATTR_RO(_name, \
615 target_core_dev_show_attr_##_name);
616
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700617DEF_DEV_ATTRIB(emulate_model_alias);
618SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
619
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620DEF_DEV_ATTRIB(emulate_dpo);
621SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
622
623DEF_DEV_ATTRIB(emulate_fua_write);
624SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
625
626DEF_DEV_ATTRIB(emulate_fua_read);
627SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
628
629DEF_DEV_ATTRIB(emulate_write_cache);
630SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
631
632DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
633SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
634
635DEF_DEV_ATTRIB(emulate_tas);
636SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
637
638DEF_DEV_ATTRIB(emulate_tpu);
639SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
640
641DEF_DEV_ATTRIB(emulate_tpws);
642SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
643
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700644DEF_DEV_ATTRIB(emulate_caw);
645SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
646
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700647DEF_DEV_ATTRIB(emulate_3pc);
648SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
649
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000650DEF_DEV_ATTRIB(pi_prot_type);
651SE_DEV_ATTR(pi_prot_type, S_IRUGO | S_IWUSR);
652
653DEF_DEV_ATTRIB_RO(hw_pi_prot_type);
654SE_DEV_ATTR_RO(hw_pi_prot_type);
655
656DEF_DEV_ATTRIB(pi_prot_format);
657SE_DEV_ATTR(pi_prot_format, S_IRUGO | S_IWUSR);
658
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659DEF_DEV_ATTRIB(enforce_pr_isids);
660SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
661
Roland Dreiere22a7f02011-07-05 13:34:52 -0700662DEF_DEV_ATTRIB(is_nonrot);
663SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
664
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700665DEF_DEV_ATTRIB(emulate_rest_reord);
666SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
667
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668DEF_DEV_ATTRIB_RO(hw_block_size);
669SE_DEV_ATTR_RO(hw_block_size);
670
671DEF_DEV_ATTRIB(block_size);
672SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
673
674DEF_DEV_ATTRIB_RO(hw_max_sectors);
675SE_DEV_ATTR_RO(hw_max_sectors);
676
Roland Dreier015487b2012-02-13 16:18:17 -0800677DEF_DEV_ATTRIB(fabric_max_sectors);
678SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
679
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680DEF_DEV_ATTRIB(optimal_sectors);
681SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
682
683DEF_DEV_ATTRIB_RO(hw_queue_depth);
684SE_DEV_ATTR_RO(hw_queue_depth);
685
686DEF_DEV_ATTRIB(queue_depth);
687SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
688
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689DEF_DEV_ATTRIB(max_unmap_lba_count);
690SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
691
692DEF_DEV_ATTRIB(max_unmap_block_desc_count);
693SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
694
695DEF_DEV_ATTRIB(unmap_granularity);
696SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
697
698DEF_DEV_ATTRIB(unmap_granularity_alignment);
699SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
700
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800701DEF_DEV_ATTRIB(max_write_same_len);
702SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
703
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
705
706static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700707 &target_core_dev_attrib_emulate_model_alias.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 &target_core_dev_attrib_emulate_dpo.attr,
709 &target_core_dev_attrib_emulate_fua_write.attr,
710 &target_core_dev_attrib_emulate_fua_read.attr,
711 &target_core_dev_attrib_emulate_write_cache.attr,
712 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
713 &target_core_dev_attrib_emulate_tas.attr,
714 &target_core_dev_attrib_emulate_tpu.attr,
715 &target_core_dev_attrib_emulate_tpws.attr,
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700716 &target_core_dev_attrib_emulate_caw.attr,
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700717 &target_core_dev_attrib_emulate_3pc.attr,
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000718 &target_core_dev_attrib_pi_prot_type.attr,
719 &target_core_dev_attrib_hw_pi_prot_type.attr,
720 &target_core_dev_attrib_pi_prot_format.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721 &target_core_dev_attrib_enforce_pr_isids.attr,
Roland Dreiere22a7f02011-07-05 13:34:52 -0700722 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700723 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724 &target_core_dev_attrib_hw_block_size.attr,
725 &target_core_dev_attrib_block_size.attr,
726 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800727 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728 &target_core_dev_attrib_optimal_sectors.attr,
729 &target_core_dev_attrib_hw_queue_depth.attr,
730 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800731 &target_core_dev_attrib_max_unmap_lba_count.attr,
732 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
733 &target_core_dev_attrib_unmap_granularity.attr,
734 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800735 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 NULL,
737};
738
739static struct configfs_item_operations target_core_dev_attrib_ops = {
740 .show_attribute = target_core_dev_attrib_attr_show,
741 .store_attribute = target_core_dev_attrib_attr_store,
742};
743
744static struct config_item_type target_core_dev_attrib_cit = {
745 .ct_item_ops = &target_core_dev_attrib_ops,
746 .ct_attrs = target_core_dev_attrib_attrs,
747 .ct_owner = THIS_MODULE,
748};
749
750/* End functions for struct config_item_type target_core_dev_attrib_cit */
751
752/* Start functions for struct config_item_type target_core_dev_wwn_cit */
753
754CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
755#define SE_DEV_WWN_ATTR(_name, _mode) \
756static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
757 __CONFIGFS_EATTR(_name, _mode, \
758 target_core_dev_wwn_show_attr_##_name, \
759 target_core_dev_wwn_store_attr_##_name);
760
761#define SE_DEV_WWN_ATTR_RO(_name); \
762do { \
763 static struct target_core_dev_wwn_attribute \
764 target_core_dev_wwn_##_name = \
765 __CONFIGFS_EATTR_RO(_name, \
766 target_core_dev_wwn_show_attr_##_name); \
767} while (0);
768
769/*
770 * VPD page 0x80 Unit serial
771 */
772static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
773 struct t10_wwn *t10_wwn,
774 char *page)
775{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
777 &t10_wwn->unit_serial[0]);
778}
779
780static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
781 struct t10_wwn *t10_wwn,
782 const char *page,
783 size_t count)
784{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400785 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800786 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
787
788 /*
789 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
790 * from the struct scsi_device level firmware, do not allow
791 * VPD Unit Serial to be emulated.
792 *
793 * Note this struct scsi_device could also be emulating VPD
794 * information from its drivers/scsi LLD. But for now we assume
795 * it is doing 'the right thing' wrt a world wide unique
796 * VPD Unit Serial Number that OS dependent multipath can depend on.
797 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400798 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700799 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800 " Unit Serial, ignoring request\n");
801 return -EOPNOTSUPP;
802 }
803
Dan Carpenter60d645a2011-06-15 10:03:05 -0700804 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700805 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
807 return -EOVERFLOW;
808 }
809 /*
810 * Check to see if any active $FABRIC_MOD exports exist. If they
811 * do exist, fail here as changing this information on the fly
812 * (underneath the initiator side OS dependent multipath code)
813 * could cause negative effects.
814 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400815 if (dev->export_count) {
816 pr_err("Unable to set VPD Unit Serial while"
817 " active %d $FABRIC_MOD exports exist\n",
818 dev->export_count);
819 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400821
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822 /*
823 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
824 *
825 * Also, strip any newline added from the userspace
826 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
827 */
828 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
829 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400830 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400832 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833
Andy Grover6708bb22011-06-08 10:36:43 -0700834 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400835 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836
837 return count;
838}
839
840SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
841
842/*
843 * VPD page 0x83 Protocol Identifier
844 */
845static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
846 struct t10_wwn *t10_wwn,
847 char *page)
848{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849 struct t10_vpd *vpd;
850 unsigned char buf[VPD_TMP_BUF_SIZE];
851 ssize_t len = 0;
852
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 memset(buf, 0, VPD_TMP_BUF_SIZE);
854
855 spin_lock(&t10_wwn->t10_vpd_lock);
856 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700857 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858 continue;
859
860 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
861
Andy Grover6708bb22011-06-08 10:36:43 -0700862 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863 break;
864
865 len += sprintf(page+len, "%s", buf);
866 }
867 spin_unlock(&t10_wwn->t10_vpd_lock);
868
869 return len;
870}
871
872static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
873 struct t10_wwn *t10_wwn,
874 const char *page,
875 size_t count)
876{
877 return -ENOSYS;
878}
879
880SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
881
882/*
883 * Generic wrapper for dumping VPD identifiers by association.
884 */
885#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
886static ssize_t target_core_dev_wwn_show_attr_##_name( \
887 struct t10_wwn *t10_wwn, \
888 char *page) \
889{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800890 struct t10_vpd *vpd; \
891 unsigned char buf[VPD_TMP_BUF_SIZE]; \
892 ssize_t len = 0; \
893 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800894 spin_lock(&t10_wwn->t10_vpd_lock); \
895 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
896 if (vpd->association != _assoc) \
897 continue; \
898 \
899 memset(buf, 0, VPD_TMP_BUF_SIZE); \
900 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700901 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902 break; \
903 len += sprintf(page+len, "%s", buf); \
904 \
905 memset(buf, 0, VPD_TMP_BUF_SIZE); \
906 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700907 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 break; \
909 len += sprintf(page+len, "%s", buf); \
910 \
911 memset(buf, 0, VPD_TMP_BUF_SIZE); \
912 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700913 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800914 break; \
915 len += sprintf(page+len, "%s", buf); \
916 } \
917 spin_unlock(&t10_wwn->t10_vpd_lock); \
918 \
919 return len; \
920}
921
922/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700923 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924 */
925DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
926
927static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
928 struct t10_wwn *t10_wwn,
929 const char *page,
930 size_t count)
931{
932 return -ENOSYS;
933}
934
935SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
936
937/*
938 * VPD page 0x83 Association: Target Port
939 */
940DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
941
942static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
943 struct t10_wwn *t10_wwn,
944 const char *page,
945 size_t count)
946{
947 return -ENOSYS;
948}
949
950SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
951
952/*
953 * VPD page 0x83 Association: SCSI Target Device
954 */
955DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
956
957static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
958 struct t10_wwn *t10_wwn,
959 const char *page,
960 size_t count)
961{
962 return -ENOSYS;
963}
964
965SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
966
967CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
968
969static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
970 &target_core_dev_wwn_vpd_unit_serial.attr,
971 &target_core_dev_wwn_vpd_protocol_identifier.attr,
972 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
973 &target_core_dev_wwn_vpd_assoc_target_port.attr,
974 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
975 NULL,
976};
977
978static struct configfs_item_operations target_core_dev_wwn_ops = {
979 .show_attribute = target_core_dev_wwn_attr_show,
980 .store_attribute = target_core_dev_wwn_attr_store,
981};
982
983static struct config_item_type target_core_dev_wwn_cit = {
984 .ct_item_ops = &target_core_dev_wwn_ops,
985 .ct_attrs = target_core_dev_wwn_attrs,
986 .ct_owner = THIS_MODULE,
987};
988
989/* End functions for struct config_item_type target_core_dev_wwn_cit */
990
991/* Start functions for struct config_item_type target_core_dev_pr_cit */
992
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400993CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800994#define SE_DEV_PR_ATTR(_name, _mode) \
995static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
996 __CONFIGFS_EATTR(_name, _mode, \
997 target_core_dev_pr_show_attr_##_name, \
998 target_core_dev_pr_store_attr_##_name);
999
1000#define SE_DEV_PR_ATTR_RO(_name); \
1001static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1002 __CONFIGFS_EATTR_RO(_name, \
1003 target_core_dev_pr_show_attr_##_name);
1004
Christoph Hellwigd977f432012-10-10 17:37:15 -04001005static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1006 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007{
1008 struct se_node_acl *se_nacl;
1009 struct t10_pr_registration *pr_reg;
1010 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011
1012 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1013
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001014 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001015 if (!pr_reg)
1016 return sprintf(page, "No SPC-3 Reservation holder\n");
1017
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001019 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020
Christoph Hellwigd977f432012-10-10 17:37:15 -04001021 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001022 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001023 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024}
1025
Christoph Hellwigd977f432012-10-10 17:37:15 -04001026static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1027 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028{
1029 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001030 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001033 if (se_nacl) {
1034 len = sprintf(page,
1035 "SPC-2 Reservation: %s Initiator: %s\n",
1036 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1037 se_nacl->initiatorname);
1038 } else {
1039 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001041 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001042}
1043
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001044static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1045 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001046{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001047 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048
Christoph Hellwigd977f432012-10-10 17:37:15 -04001049 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1050 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051
Christoph Hellwigd977f432012-10-10 17:37:15 -04001052 spin_lock(&dev->dev_reservation_lock);
1053 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1054 ret = target_core_dev_pr_show_spc2_res(dev, page);
1055 else
1056 ret = target_core_dev_pr_show_spc3_res(dev, page);
1057 spin_unlock(&dev->dev_reservation_lock);
1058 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059}
1060
1061SE_DEV_PR_ATTR_RO(res_holder);
1062
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001064 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066 ssize_t len = 0;
1067
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001069 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001071 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072 len = sprintf(page, "SPC-3 Reservation: All Target"
1073 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001074 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075 len = sprintf(page, "SPC-3 Reservation: Single"
1076 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001077 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078
Christoph Hellwigd977f432012-10-10 17:37:15 -04001079 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080 return len;
1081}
1082
1083SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1084
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001086 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001088 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089}
1090
1091SE_DEV_PR_ATTR_RO(res_pr_generation);
1092
1093/*
1094 * res_pr_holder_tg_port
1095 */
1096static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001097 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 struct se_node_acl *se_nacl;
1100 struct se_lun *lun;
1101 struct se_portal_group *se_tpg;
1102 struct t10_pr_registration *pr_reg;
1103 struct target_core_fabric_ops *tfo;
1104 ssize_t len = 0;
1105
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106 spin_lock(&dev->dev_reservation_lock);
1107 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001108 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001110 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001112
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113 se_nacl = pr_reg->pr_reg_nacl;
1114 se_tpg = se_nacl->se_tpg;
1115 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001116 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117
1118 len += sprintf(page+len, "SPC-3 Reservation: %s"
1119 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1120 tfo->tpg_get_wwn(se_tpg));
1121 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001122 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1124 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1125 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001126
Christoph Hellwigd977f432012-10-10 17:37:15 -04001127out_unlock:
1128 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129 return len;
1130}
1131
1132SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1133
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001135 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136{
1137 struct target_core_fabric_ops *tfo;
1138 struct t10_pr_registration *pr_reg;
1139 unsigned char buf[384];
1140 char i_buf[PR_REG_ISID_ID_LEN];
1141 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001142 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1145
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001146 spin_lock(&dev->t10_pr.registration_lock);
1147 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 pr_reg_list) {
1149
1150 memset(buf, 0, 384);
1151 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1152 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001153 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 PR_REG_ISID_ID_LEN);
1155 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1156 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001157 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001158 pr_reg->pr_res_generation);
1159
Andy Grover6708bb22011-06-08 10:36:43 -07001160 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 break;
1162
1163 len += sprintf(page+len, "%s", buf);
1164 reg_count++;
1165 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001166 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167
Andy Grover6708bb22011-06-08 10:36:43 -07001168 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 len += sprintf(page+len, "None\n");
1170
1171 return len;
1172}
1173
1174SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001177 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 struct t10_pr_registration *pr_reg;
1180 ssize_t len = 0;
1181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 spin_lock(&dev->dev_reservation_lock);
1183 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001184 if (pr_reg) {
1185 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1186 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1187 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190
Christoph Hellwigd977f432012-10-10 17:37:15 -04001191 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 return len;
1193}
1194
1195SE_DEV_PR_ATTR_RO(res_pr_type);
1196
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001198 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001200 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1201 return sprintf(page, "SPC_PASSTHROUGH\n");
1202 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1203 return sprintf(page, "SPC2_RESERVATIONS\n");
1204 else
1205 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206}
1207
1208SE_DEV_PR_ATTR_RO(res_type);
1209
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001211 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001213 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 return 0;
1215
1216 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001217 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218}
1219
1220SE_DEV_PR_ATTR_RO(res_aptpl_active);
1221
1222/*
1223 * res_aptpl_metadata
1224 */
1225static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001226 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001228 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 return 0;
1230
1231 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1232}
1233
1234enum {
1235 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1236 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1237 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1238 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1239};
1240
1241static match_table_t tokens = {
1242 {Opt_initiator_fabric, "initiator_fabric=%s"},
1243 {Opt_initiator_node, "initiator_node=%s"},
1244 {Opt_initiator_sid, "initiator_sid=%s"},
1245 {Opt_sa_res_key, "sa_res_key=%s"},
1246 {Opt_res_holder, "res_holder=%d"},
1247 {Opt_res_type, "res_type=%d"},
1248 {Opt_res_scope, "res_scope=%d"},
1249 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1250 {Opt_mapped_lun, "mapped_lun=%d"},
1251 {Opt_target_fabric, "target_fabric=%s"},
1252 {Opt_target_node, "target_node=%s"},
1253 {Opt_tpgt, "tpgt=%d"},
1254 {Opt_port_rtpi, "port_rtpi=%d"},
1255 {Opt_target_lun, "target_lun=%d"},
1256 {Opt_err, NULL}
1257};
1258
1259static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001260 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 const char *page,
1262 size_t count)
1263{
Jesper Juhl6d180252011-03-14 04:05:56 -07001264 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1265 unsigned char *t_fabric = NULL, *t_port = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266 char *orig, *ptr, *arg_p, *opts;
1267 substring_t args[MAX_OPT_ARGS];
1268 unsigned long long tmp_ll;
1269 u64 sa_res_key = 0;
1270 u32 mapped_lun = 0, target_lun = 0;
1271 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1272 u16 port_rpti = 0, tpgt = 0;
1273 u8 type = 0, scope;
1274
Christoph Hellwigd977f432012-10-10 17:37:15 -04001275 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1276 return 0;
1277 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 return 0;
1279
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001280 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001281 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 " active fabric exports exist\n");
1283 return -EINVAL;
1284 }
1285
1286 opts = kstrdup(page, GFP_KERNEL);
1287 if (!opts)
1288 return -ENOMEM;
1289
1290 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001291 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 if (!*ptr)
1293 continue;
1294
1295 token = match_token(ptr, tokens, args);
1296 switch (token) {
1297 case Opt_initiator_fabric:
1298 i_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001299 if (!i_fabric) {
1300 ret = -ENOMEM;
1301 goto out;
1302 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 break;
1304 case Opt_initiator_node:
1305 i_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001306 if (!i_port) {
1307 ret = -ENOMEM;
1308 goto out;
1309 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001310 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1313 PR_APTPL_MAX_IPORT_LEN);
1314 ret = -EINVAL;
1315 break;
1316 }
1317 break;
1318 case Opt_initiator_sid:
1319 isid = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001320 if (!isid) {
1321 ret = -ENOMEM;
1322 goto out;
1323 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001324 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001325 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 "= exceeds PR_REG_ISID_LEN: %d\n",
1327 PR_REG_ISID_LEN);
1328 ret = -EINVAL;
1329 break;
1330 }
1331 break;
1332 case Opt_sa_res_key:
1333 arg_p = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001334 if (!arg_p) {
1335 ret = -ENOMEM;
1336 goto out;
1337 }
Jingoo Han57103d72013-07-19 16:22:19 +09001338 ret = kstrtoull(arg_p, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09001340 pr_err("kstrtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 " sa_res_key=\n");
1342 goto out;
1343 }
1344 sa_res_key = (u64)tmp_ll;
1345 break;
1346 /*
1347 * PR APTPL Metadata for Reservation
1348 */
1349 case Opt_res_holder:
1350 match_int(args, &arg);
1351 res_holder = arg;
1352 break;
1353 case Opt_res_type:
1354 match_int(args, &arg);
1355 type = (u8)arg;
1356 break;
1357 case Opt_res_scope:
1358 match_int(args, &arg);
1359 scope = (u8)arg;
1360 break;
1361 case Opt_res_all_tg_pt:
1362 match_int(args, &arg);
1363 all_tg_pt = (int)arg;
1364 break;
1365 case Opt_mapped_lun:
1366 match_int(args, &arg);
1367 mapped_lun = (u32)arg;
1368 break;
1369 /*
1370 * PR APTPL Metadata for Target Port
1371 */
1372 case Opt_target_fabric:
1373 t_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001374 if (!t_fabric) {
1375 ret = -ENOMEM;
1376 goto out;
1377 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001378 break;
1379 case Opt_target_node:
1380 t_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001381 if (!t_port) {
1382 ret = -ENOMEM;
1383 goto out;
1384 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001385 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001386 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1388 PR_APTPL_MAX_TPORT_LEN);
1389 ret = -EINVAL;
1390 break;
1391 }
1392 break;
1393 case Opt_tpgt:
1394 match_int(args, &arg);
1395 tpgt = (u16)arg;
1396 break;
1397 case Opt_port_rtpi:
1398 match_int(args, &arg);
1399 port_rpti = (u16)arg;
1400 break;
1401 case Opt_target_lun:
1402 match_int(args, &arg);
1403 target_lun = (u32)arg;
1404 break;
1405 default:
1406 break;
1407 }
1408 }
1409
Andy Grover6708bb22011-06-08 10:36:43 -07001410 if (!i_port || !t_port || !sa_res_key) {
1411 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 ret = -EINVAL;
1413 goto out;
1414 }
1415
1416 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001417 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 " holder\n", type);
1419 ret = -EINVAL;
1420 goto out;
1421 }
1422
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001423 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1425 res_holder, all_tg_pt, type);
1426out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001427 kfree(i_fabric);
1428 kfree(i_port);
1429 kfree(isid);
1430 kfree(t_fabric);
1431 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 kfree(orig);
1433 return (ret == 0) ? count : ret;
1434}
1435
1436SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1437
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001438CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001439
1440static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1441 &target_core_dev_pr_res_holder.attr,
1442 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1443 &target_core_dev_pr_res_pr_generation.attr,
1444 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1445 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1446 &target_core_dev_pr_res_pr_type.attr,
1447 &target_core_dev_pr_res_type.attr,
1448 &target_core_dev_pr_res_aptpl_active.attr,
1449 &target_core_dev_pr_res_aptpl_metadata.attr,
1450 NULL,
1451};
1452
1453static struct configfs_item_operations target_core_dev_pr_ops = {
1454 .show_attribute = target_core_dev_pr_attr_show,
1455 .store_attribute = target_core_dev_pr_attr_store,
1456};
1457
1458static struct config_item_type target_core_dev_pr_cit = {
1459 .ct_item_ops = &target_core_dev_pr_ops,
1460 .ct_attrs = target_core_dev_pr_attrs,
1461 .ct_owner = THIS_MODULE,
1462};
1463
1464/* End functions for struct config_item_type target_core_dev_pr_cit */
1465
1466/* Start functions for struct config_item_type target_core_dev_cit */
1467
1468static ssize_t target_core_show_dev_info(void *p, char *page)
1469{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001470 struct se_device *dev = p;
1471 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 int bl = 0;
1473 ssize_t read_bytes = 0;
1474
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001475 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001477 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478 return read_bytes;
1479}
1480
1481static struct target_core_configfs_attribute target_core_attr_dev_info = {
1482 .attr = { .ca_owner = THIS_MODULE,
1483 .ca_name = "info",
1484 .ca_mode = S_IRUGO },
1485 .show = target_core_show_dev_info,
1486 .store = NULL,
1487};
1488
1489static ssize_t target_core_store_dev_control(
1490 void *p,
1491 const char *page,
1492 size_t count)
1493{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001494 struct se_device *dev = p;
1495 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001497 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498}
1499
1500static struct target_core_configfs_attribute target_core_attr_dev_control = {
1501 .attr = { .ca_owner = THIS_MODULE,
1502 .ca_name = "control",
1503 .ca_mode = S_IWUSR },
1504 .show = NULL,
1505 .store = target_core_store_dev_control,
1506};
1507
1508static ssize_t target_core_show_dev_alias(void *p, char *page)
1509{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001510 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001511
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001512 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001513 return 0;
1514
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001515 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001516}
1517
1518static ssize_t target_core_store_dev_alias(
1519 void *p,
1520 const char *page,
1521 size_t count)
1522{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001523 struct se_device *dev = p;
1524 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001525 ssize_t read_bytes;
1526
1527 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001528 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1530 SE_DEV_ALIAS_LEN-1);
1531 return -EINVAL;
1532 }
1533
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001534 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001535 if (!read_bytes)
1536 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001537 if (dev->dev_alias[read_bytes - 1] == '\n')
1538 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001539
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001540 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001541
Andy Grover6708bb22011-06-08 10:36:43 -07001542 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001544 config_item_name(&dev->dev_group.cg_item),
1545 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001546
1547 return read_bytes;
1548}
1549
1550static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1551 .attr = { .ca_owner = THIS_MODULE,
1552 .ca_name = "alias",
1553 .ca_mode = S_IRUGO | S_IWUSR },
1554 .show = target_core_show_dev_alias,
1555 .store = target_core_store_dev_alias,
1556};
1557
1558static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1559{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001560 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001562 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001563 return 0;
1564
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001565 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001566}
1567
1568static ssize_t target_core_store_dev_udev_path(
1569 void *p,
1570 const char *page,
1571 size_t count)
1572{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001573 struct se_device *dev = p;
1574 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575 ssize_t read_bytes;
1576
1577 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001578 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1580 SE_UDEV_PATH_LEN-1);
1581 return -EINVAL;
1582 }
1583
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001584 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001586 if (!read_bytes)
1587 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001588 if (dev->udev_path[read_bytes - 1] == '\n')
1589 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001590
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001591 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001592
Andy Grover6708bb22011-06-08 10:36:43 -07001593 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001594 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001595 config_item_name(&dev->dev_group.cg_item),
1596 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001597
1598 return read_bytes;
1599}
1600
1601static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1602 .attr = { .ca_owner = THIS_MODULE,
1603 .ca_name = "udev_path",
1604 .ca_mode = S_IRUGO | S_IWUSR },
1605 .show = target_core_show_dev_udev_path,
1606 .store = target_core_store_dev_udev_path,
1607};
1608
Andy Grover64146db2013-04-30 11:59:15 -07001609static ssize_t target_core_show_dev_enable(void *p, char *page)
1610{
1611 struct se_device *dev = p;
1612
1613 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1614}
1615
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616static ssize_t target_core_store_dev_enable(
1617 void *p,
1618 const char *page,
1619 size_t count)
1620{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001621 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001623 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001624
1625 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001626 if (!ptr) {
1627 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 " is \"1\"\n");
1629 return -EINVAL;
1630 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001632 ret = target_configure_device(dev);
1633 if (ret)
1634 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635 return count;
1636}
1637
1638static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1639 .attr = { .ca_owner = THIS_MODULE,
1640 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001641 .ca_mode = S_IRUGO | S_IWUSR },
1642 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 .store = target_core_store_dev_enable,
1644};
1645
1646static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1647{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001648 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001649 struct config_item *lu_ci;
1650 struct t10_alua_lu_gp *lu_gp;
1651 struct t10_alua_lu_gp_member *lu_gp_mem;
1652 ssize_t len = 0;
1653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001655 if (!lu_gp_mem)
1656 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001657
1658 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1659 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001660 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 lu_ci = &lu_gp->lu_gp_group.cg_item;
1662 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1663 config_item_name(lu_ci), lu_gp->lu_gp_id);
1664 }
1665 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1666
1667 return len;
1668}
1669
1670static ssize_t target_core_store_alua_lu_gp(
1671 void *p,
1672 const char *page,
1673 size_t count)
1674{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001675 struct se_device *dev = p;
1676 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001677 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1678 struct t10_alua_lu_gp_member *lu_gp_mem;
1679 unsigned char buf[LU_GROUP_NAME_BUF];
1680 int move = 0;
1681
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001682 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1683 if (!lu_gp_mem)
1684 return 0;
1685
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001686 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001687 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001688 return -EINVAL;
1689 }
1690 memset(buf, 0, LU_GROUP_NAME_BUF);
1691 memcpy(buf, page, count);
1692 /*
1693 * Any ALUA logical unit alias besides "NULL" means we will be
1694 * making a new group association.
1695 */
1696 if (strcmp(strstrip(buf), "NULL")) {
1697 /*
1698 * core_alua_get_lu_gp_by_name() will increment reference to
1699 * struct t10_alua_lu_gp. This reference is released with
1700 * core_alua_get_lu_gp_by_name below().
1701 */
1702 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001703 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 return -ENODEV;
1705 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706
1707 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1708 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001709 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710 /*
1711 * Clearing an existing lu_gp association, and replacing
1712 * with NULL
1713 */
Andy Grover6708bb22011-06-08 10:36:43 -07001714 if (!lu_gp_new) {
1715 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001716 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1717 " %hu\n",
1718 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001719 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720 config_item_name(&lu_gp->lu_gp_group.cg_item),
1721 lu_gp->lu_gp_id);
1722
1723 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1724 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1725
1726 return count;
1727 }
1728 /*
1729 * Removing existing association of lu_gp_mem with lu_gp
1730 */
1731 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1732 move = 1;
1733 }
1734 /*
1735 * Associate lu_gp_mem with lu_gp_new.
1736 */
1737 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1738 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1739
Andy Grover6708bb22011-06-08 10:36:43 -07001740 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741 " core/alua/lu_gps/%s, ID: %hu\n",
1742 (move) ? "Moving" : "Adding",
1743 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001744 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1746 lu_gp_new->lu_gp_id);
1747
1748 core_alua_put_lu_gp_from_name(lu_gp_new);
1749 return count;
1750}
1751
1752static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1753 .attr = { .ca_owner = THIS_MODULE,
1754 .ca_name = "alua_lu_gp",
1755 .ca_mode = S_IRUGO | S_IWUSR },
1756 .show = target_core_show_alua_lu_gp,
1757 .store = target_core_store_alua_lu_gp,
1758};
1759
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001760static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1761{
1762 struct se_device *dev = p;
1763 struct t10_alua_lba_map *map;
1764 struct t10_alua_lba_map_member *mem;
1765 char *b = page;
1766 int bl = 0;
1767 char state;
1768
1769 spin_lock(&dev->t10_alua.lba_map_lock);
1770 if (!list_empty(&dev->t10_alua.lba_map_list))
1771 bl += sprintf(b + bl, "%u %u\n",
1772 dev->t10_alua.lba_map_segment_size,
1773 dev->t10_alua.lba_map_segment_multiplier);
1774 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1775 bl += sprintf(b + bl, "%llu %llu",
1776 map->lba_map_first_lba, map->lba_map_last_lba);
1777 list_for_each_entry(mem, &map->lba_map_mem_list,
1778 lba_map_mem_list) {
1779 switch (mem->lba_map_mem_alua_state) {
1780 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1781 state = 'O';
1782 break;
1783 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1784 state = 'A';
1785 break;
1786 case ALUA_ACCESS_STATE_STANDBY:
1787 state = 'S';
1788 break;
1789 case ALUA_ACCESS_STATE_UNAVAILABLE:
1790 state = 'U';
1791 break;
1792 default:
1793 state = '.';
1794 break;
1795 }
1796 bl += sprintf(b + bl, " %d:%c",
1797 mem->lba_map_mem_alua_pg_id, state);
1798 }
1799 bl += sprintf(b + bl, "\n");
1800 }
1801 spin_unlock(&dev->t10_alua.lba_map_lock);
1802 return bl;
1803}
1804
1805static ssize_t target_core_store_dev_lba_map(
1806 void *p,
1807 const char *page,
1808 size_t count)
1809{
1810 struct se_device *dev = p;
1811 struct t10_alua_lba_map *lba_map = NULL;
1812 struct list_head lba_list;
1813 char *map_entries, *ptr;
1814 char state;
1815 int pg_num = -1, pg;
1816 int ret = 0, num = 0, pg_id, alua_state;
1817 unsigned long start_lba = -1, end_lba = -1;
1818 unsigned long segment_size = -1, segment_mult = -1;
1819
1820 map_entries = kstrdup(page, GFP_KERNEL);
1821 if (!map_entries)
1822 return -ENOMEM;
1823
1824 INIT_LIST_HEAD(&lba_list);
1825 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1826 if (!*ptr)
1827 continue;
1828
1829 if (num == 0) {
1830 if (sscanf(ptr, "%lu %lu\n",
1831 &segment_size, &segment_mult) != 2) {
1832 pr_err("Invalid line %d\n", num);
1833 ret = -EINVAL;
1834 break;
1835 }
1836 num++;
1837 continue;
1838 }
1839 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1840 pr_err("Invalid line %d\n", num);
1841 ret = -EINVAL;
1842 break;
1843 }
1844 ptr = strchr(ptr, ' ');
1845 if (!ptr) {
1846 pr_err("Invalid line %d, missing end lba\n", num);
1847 ret = -EINVAL;
1848 break;
1849 }
1850 ptr++;
1851 ptr = strchr(ptr, ' ');
1852 if (!ptr) {
1853 pr_err("Invalid line %d, missing state definitions\n",
1854 num);
1855 ret = -EINVAL;
1856 break;
1857 }
1858 ptr++;
1859 lba_map = core_alua_allocate_lba_map(&lba_list,
1860 start_lba, end_lba);
1861 if (IS_ERR(lba_map)) {
1862 ret = PTR_ERR(lba_map);
1863 break;
1864 }
1865 pg = 0;
1866 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1867 switch (state) {
1868 case 'O':
1869 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1870 break;
1871 case 'A':
1872 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1873 break;
1874 case 'S':
1875 alua_state = ALUA_ACCESS_STATE_STANDBY;
1876 break;
1877 case 'U':
1878 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1879 break;
1880 default:
1881 pr_err("Invalid ALUA state '%c'\n", state);
1882 ret = -EINVAL;
1883 goto out;
1884 }
1885
1886 ret = core_alua_allocate_lba_map_mem(lba_map,
1887 pg_id, alua_state);
1888 if (ret) {
1889 pr_err("Invalid target descriptor %d:%c "
1890 "at line %d\n",
1891 pg_id, state, num);
1892 break;
1893 }
1894 pg++;
1895 ptr = strchr(ptr, ' ');
1896 if (ptr)
1897 ptr++;
1898 else
1899 break;
1900 }
1901 if (pg_num == -1)
1902 pg_num = pg;
1903 else if (pg != pg_num) {
1904 pr_err("Only %d from %d port groups definitions "
1905 "at line %d\n", pg, pg_num, num);
1906 ret = -EINVAL;
1907 break;
1908 }
1909 num++;
1910 }
1911out:
1912 if (ret) {
1913 core_alua_free_lba_map(&lba_list);
1914 count = ret;
1915 } else
1916 core_alua_set_lba_map(dev, &lba_list,
1917 segment_size, segment_mult);
1918 kfree(map_entries);
1919 return count;
1920}
1921
1922static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1923 .attr = { .ca_owner = THIS_MODULE,
1924 .ca_name = "lba_map",
1925 .ca_mode = S_IRUGO | S_IWUSR },
1926 .show = target_core_show_dev_lba_map,
1927 .store = target_core_store_dev_lba_map,
1928};
1929
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001930static struct configfs_attribute *lio_core_dev_attrs[] = {
1931 &target_core_attr_dev_info.attr,
1932 &target_core_attr_dev_control.attr,
1933 &target_core_attr_dev_alias.attr,
1934 &target_core_attr_dev_udev_path.attr,
1935 &target_core_attr_dev_enable.attr,
1936 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001937 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 NULL,
1939};
1940
1941static void target_core_dev_release(struct config_item *item)
1942{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001943 struct config_group *dev_cg = to_config_group(item);
1944 struct se_device *dev =
1945 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001947 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001948 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949}
1950
1951static ssize_t target_core_dev_show(struct config_item *item,
1952 struct configfs_attribute *attr,
1953 char *page)
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->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 return -EINVAL;
1963
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001964 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965}
1966
1967static ssize_t target_core_dev_store(struct config_item *item,
1968 struct configfs_attribute *attr,
1969 const char *page, size_t count)
1970{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001971 struct config_group *dev_cg = to_config_group(item);
1972 struct se_device *dev =
1973 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001974 struct target_core_configfs_attribute *tc_attr = container_of(
1975 attr, struct target_core_configfs_attribute, attr);
1976
Andy Grover6708bb22011-06-08 10:36:43 -07001977 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001978 return -EINVAL;
1979
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001980 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981}
1982
1983static struct configfs_item_operations target_core_dev_item_ops = {
1984 .release = target_core_dev_release,
1985 .show_attribute = target_core_dev_show,
1986 .store_attribute = target_core_dev_store,
1987};
1988
1989static struct config_item_type target_core_dev_cit = {
1990 .ct_item_ops = &target_core_dev_item_ops,
1991 .ct_attrs = lio_core_dev_attrs,
1992 .ct_owner = THIS_MODULE,
1993};
1994
1995/* End functions for struct config_item_type target_core_dev_cit */
1996
1997/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1998
1999CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2000#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2001static struct target_core_alua_lu_gp_attribute \
2002 target_core_alua_lu_gp_##_name = \
2003 __CONFIGFS_EATTR(_name, _mode, \
2004 target_core_alua_lu_gp_show_attr_##_name, \
2005 target_core_alua_lu_gp_store_attr_##_name);
2006
2007#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2008static struct target_core_alua_lu_gp_attribute \
2009 target_core_alua_lu_gp_##_name = \
2010 __CONFIGFS_EATTR_RO(_name, \
2011 target_core_alua_lu_gp_show_attr_##_name);
2012
2013/*
2014 * lu_gp_id
2015 */
2016static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2017 struct t10_alua_lu_gp *lu_gp,
2018 char *page)
2019{
Andy Grover6708bb22011-06-08 10:36:43 -07002020 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 return 0;
2022
2023 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2024}
2025
2026static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2027 struct t10_alua_lu_gp *lu_gp,
2028 const char *page,
2029 size_t count)
2030{
2031 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2032 unsigned long lu_gp_id;
2033 int ret;
2034
Jingoo Han57103d72013-07-19 16:22:19 +09002035 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002037 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002038 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002039 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002040 }
2041 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002042 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 " 0x0000ffff\n", lu_gp_id);
2044 return -EINVAL;
2045 }
2046
2047 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2048 if (ret < 0)
2049 return -EINVAL;
2050
Andy Grover6708bb22011-06-08 10:36:43 -07002051 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2053 config_item_name(&alua_lu_gp_cg->cg_item),
2054 lu_gp->lu_gp_id);
2055
2056 return count;
2057}
2058
2059SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2060
2061/*
2062 * members
2063 */
2064static ssize_t target_core_alua_lu_gp_show_attr_members(
2065 struct t10_alua_lu_gp *lu_gp,
2066 char *page)
2067{
2068 struct se_device *dev;
2069 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070 struct t10_alua_lu_gp_member *lu_gp_mem;
2071 ssize_t len = 0, cur_len;
2072 unsigned char buf[LU_GROUP_NAME_BUF];
2073
2074 memset(buf, 0, LU_GROUP_NAME_BUF);
2075
2076 spin_lock(&lu_gp->lu_gp_lock);
2077 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2078 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002079 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080
2081 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2082 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002083 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002084 cur_len++; /* Extra byte for NULL terminator */
2085
2086 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002087 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002088 "_members buffer\n");
2089 break;
2090 }
2091 memcpy(page+len, buf, cur_len);
2092 len += cur_len;
2093 }
2094 spin_unlock(&lu_gp->lu_gp_lock);
2095
2096 return len;
2097}
2098
2099SE_DEV_ALUA_LU_ATTR_RO(members);
2100
2101CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2102
2103static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2104 &target_core_alua_lu_gp_lu_gp_id.attr,
2105 &target_core_alua_lu_gp_members.attr,
2106 NULL,
2107};
2108
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002109static void target_core_alua_lu_gp_release(struct config_item *item)
2110{
2111 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2112 struct t10_alua_lu_gp, lu_gp_group);
2113
2114 core_alua_free_lu_gp(lu_gp);
2115}
2116
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002118 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119 .show_attribute = target_core_alua_lu_gp_attr_show,
2120 .store_attribute = target_core_alua_lu_gp_attr_store,
2121};
2122
2123static struct config_item_type target_core_alua_lu_gp_cit = {
2124 .ct_item_ops = &target_core_alua_lu_gp_ops,
2125 .ct_attrs = target_core_alua_lu_gp_attrs,
2126 .ct_owner = THIS_MODULE,
2127};
2128
2129/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2130
2131/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2132
2133static struct config_group *target_core_alua_create_lu_gp(
2134 struct config_group *group,
2135 const char *name)
2136{
2137 struct t10_alua_lu_gp *lu_gp;
2138 struct config_group *alua_lu_gp_cg = NULL;
2139 struct config_item *alua_lu_gp_ci = NULL;
2140
2141 lu_gp = core_alua_allocate_lu_gp(name, 0);
2142 if (IS_ERR(lu_gp))
2143 return NULL;
2144
2145 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2146 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2147
2148 config_group_init_type_name(alua_lu_gp_cg, name,
2149 &target_core_alua_lu_gp_cit);
2150
Andy Grover6708bb22011-06-08 10:36:43 -07002151 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152 " Group: core/alua/lu_gps/%s\n",
2153 config_item_name(alua_lu_gp_ci));
2154
2155 return alua_lu_gp_cg;
2156
2157}
2158
2159static void target_core_alua_drop_lu_gp(
2160 struct config_group *group,
2161 struct config_item *item)
2162{
2163 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2164 struct t10_alua_lu_gp, lu_gp_group);
2165
Andy Grover6708bb22011-06-08 10:36:43 -07002166 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002167 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2168 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002169 /*
2170 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2171 * -> target_core_alua_lu_gp_release()
2172 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174}
2175
2176static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2177 .make_group = &target_core_alua_create_lu_gp,
2178 .drop_item = &target_core_alua_drop_lu_gp,
2179};
2180
2181static struct config_item_type target_core_alua_lu_gps_cit = {
2182 .ct_item_ops = NULL,
2183 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2184 .ct_owner = THIS_MODULE,
2185};
2186
2187/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2188
2189/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2190
2191CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2192#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2193static struct target_core_alua_tg_pt_gp_attribute \
2194 target_core_alua_tg_pt_gp_##_name = \
2195 __CONFIGFS_EATTR(_name, _mode, \
2196 target_core_alua_tg_pt_gp_show_attr_##_name, \
2197 target_core_alua_tg_pt_gp_store_attr_##_name);
2198
2199#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2200static struct target_core_alua_tg_pt_gp_attribute \
2201 target_core_alua_tg_pt_gp_##_name = \
2202 __CONFIGFS_EATTR_RO(_name, \
2203 target_core_alua_tg_pt_gp_show_attr_##_name);
2204
2205/*
2206 * alua_access_state
2207 */
2208static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2209 struct t10_alua_tg_pt_gp *tg_pt_gp,
2210 char *page)
2211{
2212 return sprintf(page, "%d\n",
2213 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2214}
2215
2216static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2217 struct t10_alua_tg_pt_gp *tg_pt_gp,
2218 const char *page,
2219 size_t count)
2220{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002221 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002222 unsigned long tmp;
2223 int new_state, ret;
2224
Andy Grover6708bb22011-06-08 10:36:43 -07002225 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002226 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2228 return -EINVAL;
2229 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002230 if (!(dev->dev_flags & DF_CONFIGURED)) {
2231 pr_err("Unable to set alua_access_state while device is"
2232 " not configured\n");
2233 return -ENODEV;
2234 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235
Jingoo Han57103d72013-07-19 16:22:19 +09002236 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002238 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002240 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 }
2242 new_state = (int)tmp;
2243
Hannes Reinecke125d0112013-11-19 09:07:46 +01002244 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2245 pr_err("Unable to process implicit configfs ALUA"
2246 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247 return -EINVAL;
2248 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002249 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2250 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2251 /* LBA DEPENDENT is only allowed with implicit ALUA */
2252 pr_err("Unable to process implicit configfs ALUA transition"
2253 " while explicit ALUA management is enabled\n");
2254 return -EINVAL;
2255 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002256
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002257 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002258 NULL, NULL, new_state, 0);
2259 return (!ret) ? count : -EINVAL;
2260}
2261
2262SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2263
2264/*
2265 * alua_access_status
2266 */
2267static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2268 struct t10_alua_tg_pt_gp *tg_pt_gp,
2269 char *page)
2270{
2271 return sprintf(page, "%s\n",
2272 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2273}
2274
2275static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2276 struct t10_alua_tg_pt_gp *tg_pt_gp,
2277 const char *page,
2278 size_t count)
2279{
2280 unsigned long tmp;
2281 int new_status, ret;
2282
Andy Grover6708bb22011-06-08 10:36:43 -07002283 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2284 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285 " valid tg_pt_gp ID: %hu\n",
2286 tg_pt_gp->tg_pt_gp_valid_id);
2287 return -EINVAL;
2288 }
2289
Jingoo Han57103d72013-07-19 16:22:19 +09002290 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002291 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002292 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002293 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002294 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 }
2296 new_status = (int)tmp;
2297
2298 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002299 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2300 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002301 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 new_status);
2303 return -EINVAL;
2304 }
2305
2306 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2307 return count;
2308}
2309
2310SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2311
2312/*
2313 * alua_access_type
2314 */
2315static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2316 struct t10_alua_tg_pt_gp *tg_pt_gp,
2317 char *page)
2318{
2319 return core_alua_show_access_type(tg_pt_gp, page);
2320}
2321
2322static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2323 struct t10_alua_tg_pt_gp *tg_pt_gp,
2324 const char *page,
2325 size_t count)
2326{
2327 return core_alua_store_access_type(tg_pt_gp, page, count);
2328}
2329
2330SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2331
2332/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002333 * alua_supported_states
2334 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002335
2336#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2337static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2338 struct t10_alua_tg_pt_gp *t, char *p) \
2339{ \
2340 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002341}
2342
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002343#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2344static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2345 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2346{ \
2347 unsigned long tmp; \
2348 int ret; \
2349 \
2350 if (!t->tg_pt_gp_valid_id) { \
2351 pr_err("Unable to do set ##_name ALUA state on non" \
2352 " valid tg_pt_gp ID: %hu\n", \
2353 t->tg_pt_gp_valid_id); \
2354 return -EINVAL; \
2355 } \
2356 \
2357 ret = kstrtoul(p, 0, &tmp); \
2358 if (ret < 0) { \
2359 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2360 return -EINVAL; \
2361 } \
2362 if (tmp > 1) { \
2363 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2364 return -EINVAL; \
2365 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002366 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002367 t->_var |= _bit; \
2368 else \
2369 t->_var &= ~_bit; \
2370 \
2371 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002372}
2373
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002374SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2375 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2376SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2377 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2378SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2379
2380SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2381 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2382SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2383 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2384SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2385
2386SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2387 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2388SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2389 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002390SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002391
2392SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2393 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2394SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2395 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2396SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2397
2398SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2399 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2400SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2401 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2402SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2403
2404SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2405 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2406SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2407 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2408SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2409
2410SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2411 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2412SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2413 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2414SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002415
2416/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002417 * alua_write_metadata
2418 */
2419static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2420 struct t10_alua_tg_pt_gp *tg_pt_gp,
2421 char *page)
2422{
2423 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2424}
2425
2426static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2427 struct t10_alua_tg_pt_gp *tg_pt_gp,
2428 const char *page,
2429 size_t count)
2430{
2431 unsigned long tmp;
2432 int ret;
2433
Jingoo Han57103d72013-07-19 16:22:19 +09002434 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002436 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002437 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438 }
2439
2440 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002441 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002442 " %lu\n", tmp);
2443 return -EINVAL;
2444 }
2445 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2446
2447 return count;
2448}
2449
2450SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2451
2452
2453
2454/*
2455 * nonop_delay_msecs
2456 */
2457static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2458 struct t10_alua_tg_pt_gp *tg_pt_gp,
2459 char *page)
2460{
2461 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2462
2463}
2464
2465static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2466 struct t10_alua_tg_pt_gp *tg_pt_gp,
2467 const char *page,
2468 size_t count)
2469{
2470 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2471}
2472
2473SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2474
2475/*
2476 * trans_delay_msecs
2477 */
2478static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2479 struct t10_alua_tg_pt_gp *tg_pt_gp,
2480 char *page)
2481{
2482 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2483}
2484
2485static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2486 struct t10_alua_tg_pt_gp *tg_pt_gp,
2487 const char *page,
2488 size_t count)
2489{
2490 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2491}
2492
2493SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2494
2495/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002496 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002497 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002498static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002499 struct t10_alua_tg_pt_gp *tg_pt_gp,
2500 char *page)
2501{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002502 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002503}
2504
Hannes Reinecke125d0112013-11-19 09:07:46 +01002505static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002506 struct t10_alua_tg_pt_gp *tg_pt_gp,
2507 const char *page,
2508 size_t count)
2509{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002510 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002511}
2512
Hannes Reinecke125d0112013-11-19 09:07:46 +01002513SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002514
2515/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002516 * preferred
2517 */
2518
2519static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2520 struct t10_alua_tg_pt_gp *tg_pt_gp,
2521 char *page)
2522{
2523 return core_alua_show_preferred_bit(tg_pt_gp, page);
2524}
2525
2526static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2527 struct t10_alua_tg_pt_gp *tg_pt_gp,
2528 const char *page,
2529 size_t count)
2530{
2531 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2532}
2533
2534SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2535
2536/*
2537 * tg_pt_gp_id
2538 */
2539static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2540 struct t10_alua_tg_pt_gp *tg_pt_gp,
2541 char *page)
2542{
Andy Grover6708bb22011-06-08 10:36:43 -07002543 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 return 0;
2545
2546 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2547}
2548
2549static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2550 struct t10_alua_tg_pt_gp *tg_pt_gp,
2551 const char *page,
2552 size_t count)
2553{
2554 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2555 unsigned long tg_pt_gp_id;
2556 int ret;
2557
Jingoo Han57103d72013-07-19 16:22:19 +09002558 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002560 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002561 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002562 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 }
2564 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002565 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 " 0x0000ffff\n", tg_pt_gp_id);
2567 return -EINVAL;
2568 }
2569
2570 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2571 if (ret < 0)
2572 return -EINVAL;
2573
Andy Grover6708bb22011-06-08 10:36:43 -07002574 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2576 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2577 tg_pt_gp->tg_pt_gp_id);
2578
2579 return count;
2580}
2581
2582SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2583
2584/*
2585 * members
2586 */
2587static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2588 struct t10_alua_tg_pt_gp *tg_pt_gp,
2589 char *page)
2590{
2591 struct se_port *port;
2592 struct se_portal_group *tpg;
2593 struct se_lun *lun;
2594 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2595 ssize_t len = 0, cur_len;
2596 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2597
2598 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2599
2600 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2601 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2602 tg_pt_gp_mem_list) {
2603 port = tg_pt_gp_mem->tg_pt;
2604 tpg = port->sep_tpg;
2605 lun = port->sep_lun;
2606
2607 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002608 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2609 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2610 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002611 config_item_name(&lun->lun_group.cg_item));
2612 cur_len++; /* Extra byte for NULL terminator */
2613
2614 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002615 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616 "_members buffer\n");
2617 break;
2618 }
2619 memcpy(page+len, buf, cur_len);
2620 len += cur_len;
2621 }
2622 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2623
2624 return len;
2625}
2626
2627SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2628
2629CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2630 tg_pt_gp_group);
2631
2632static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2633 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2634 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2635 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002636 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2637 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2638 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2639 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2640 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2641 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2642 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002643 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2644 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2645 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002646 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002647 &target_core_alua_tg_pt_gp_preferred.attr,
2648 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2649 &target_core_alua_tg_pt_gp_members.attr,
2650 NULL,
2651};
2652
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002653static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2654{
2655 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2656 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2657
2658 core_alua_free_tg_pt_gp(tg_pt_gp);
2659}
2660
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002661static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002662 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002663 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2664 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2665};
2666
2667static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2668 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2669 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2670 .ct_owner = THIS_MODULE,
2671};
2672
2673/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2674
2675/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2676
2677static struct config_group *target_core_alua_create_tg_pt_gp(
2678 struct config_group *group,
2679 const char *name)
2680{
2681 struct t10_alua *alua = container_of(group, struct t10_alua,
2682 alua_tg_pt_gps_group);
2683 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002684 struct config_group *alua_tg_pt_gp_cg = NULL;
2685 struct config_item *alua_tg_pt_gp_ci = NULL;
2686
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002687 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002688 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 return NULL;
2690
2691 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2692 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2693
2694 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2695 &target_core_alua_tg_pt_gp_cit);
2696
Andy Grover6708bb22011-06-08 10:36:43 -07002697 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002698 " Group: alua/tg_pt_gps/%s\n",
2699 config_item_name(alua_tg_pt_gp_ci));
2700
2701 return alua_tg_pt_gp_cg;
2702}
2703
2704static void target_core_alua_drop_tg_pt_gp(
2705 struct config_group *group,
2706 struct config_item *item)
2707{
2708 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2709 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2710
Andy Grover6708bb22011-06-08 10:36:43 -07002711 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2713 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002714 /*
2715 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2716 * -> target_core_alua_tg_pt_gp_release().
2717 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002719}
2720
2721static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2722 .make_group = &target_core_alua_create_tg_pt_gp,
2723 .drop_item = &target_core_alua_drop_tg_pt_gp,
2724};
2725
2726static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2727 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2728 .ct_owner = THIS_MODULE,
2729};
2730
2731/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2732
2733/* Start functions for struct config_item_type target_core_alua_cit */
2734
2735/*
2736 * target_core_alua_cit is a ConfigFS group that lives under
2737 * /sys/kernel/config/target/core/alua. There are default groups
2738 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2739 * target_core_alua_cit in target_core_init_configfs() below.
2740 */
2741static struct config_item_type target_core_alua_cit = {
2742 .ct_item_ops = NULL,
2743 .ct_attrs = NULL,
2744 .ct_owner = THIS_MODULE,
2745};
2746
2747/* End functions for struct config_item_type target_core_alua_cit */
2748
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002749/* Start functions for struct config_item_type target_core_stat_cit */
2750
2751static struct config_group *target_core_stat_mkdir(
2752 struct config_group *group,
2753 const char *name)
2754{
2755 return ERR_PTR(-ENOSYS);
2756}
2757
2758static void target_core_stat_rmdir(
2759 struct config_group *group,
2760 struct config_item *item)
2761{
2762 return;
2763}
2764
2765static struct configfs_group_operations target_core_stat_group_ops = {
2766 .make_group = &target_core_stat_mkdir,
2767 .drop_item = &target_core_stat_rmdir,
2768};
2769
2770static struct config_item_type target_core_stat_cit = {
2771 .ct_group_ops = &target_core_stat_group_ops,
2772 .ct_owner = THIS_MODULE,
2773};
2774
2775/* End functions for struct config_item_type target_core_stat_cit */
2776
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002777/* Start functions for struct config_item_type target_core_hba_cit */
2778
2779static struct config_group *target_core_make_subdev(
2780 struct config_group *group,
2781 const char *name)
2782{
2783 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002784 struct se_subsystem_api *t;
2785 struct config_item *hba_ci = &group->cg_item;
2786 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002787 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002789 struct config_group *dev_stat_grp = NULL;
2790 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002791
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002792 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2793 if (ret)
2794 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002795 /*
2796 * Locate the struct se_subsystem_api from parent's struct se_hba.
2797 */
2798 t = hba->transport;
2799
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002800 dev = target_alloc_device(hba, name);
2801 if (!dev)
2802 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002803
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002804 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002805
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002806 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002807 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002808 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002809 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002811 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2812 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002814 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002816 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002818 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002819 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002820 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002821 "statistics", &target_core_stat_cit);
2822
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002823 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2824 dev_cg->default_groups[1] = &dev->dev_pr_group;
2825 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2826 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2827 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002828 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002829 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002830 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002832 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002833 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002834 goto out_free_dev_cg_default_groups;
2835 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002837 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002838 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002840 if (!tg_pt_gp_cg->default_groups) {
2841 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002843 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 }
2845
2846 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2847 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2848 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2849 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002850 /*
2851 * Add core/$HBA/$DEV/statistics/ default groups
2852 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002853 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002854 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002855 GFP_KERNEL);
2856 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002857 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002858 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002859 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002860 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861
2862 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002863 return dev_cg;
2864
2865out_free_tg_pt_gp_cg_default_groups:
2866 kfree(tg_pt_gp_cg->default_groups);
2867out_free_tg_pt_gp:
2868 core_alua_free_tg_pt_gp(tg_pt_gp);
2869out_free_dev_cg_default_groups:
2870 kfree(dev_cg->default_groups);
2871out_free_device:
2872 target_free_device(dev);
2873out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002875 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876}
2877
2878static void target_core_drop_subdev(
2879 struct config_group *group,
2880 struct config_item *item)
2881{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002882 struct config_group *dev_cg = to_config_group(item);
2883 struct se_device *dev =
2884 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002885 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002887 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002888 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002890 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002892 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002894 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002895 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2896 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2897 dev_stat_grp->default_groups[i] = NULL;
2898 config_item_put(df_item);
2899 }
2900 kfree(dev_stat_grp->default_groups);
2901
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002902 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2904 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2905 tg_pt_gp_cg->default_groups[i] = NULL;
2906 config_item_put(df_item);
2907 }
2908 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002909 /*
2910 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2911 * directly from target_core_alua_tg_pt_gp_release().
2912 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002913 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 for (i = 0; dev_cg->default_groups[i]; i++) {
2916 df_item = &dev_cg->default_groups[i]->cg_item;
2917 dev_cg->default_groups[i] = NULL;
2918 config_item_put(df_item);
2919 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002921 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002923 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925}
2926
2927static struct configfs_group_operations target_core_hba_group_ops = {
2928 .make_group = target_core_make_subdev,
2929 .drop_item = target_core_drop_subdev,
2930};
2931
2932CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2933#define SE_HBA_ATTR(_name, _mode) \
2934static struct target_core_hba_attribute \
2935 target_core_hba_##_name = \
2936 __CONFIGFS_EATTR(_name, _mode, \
2937 target_core_hba_show_attr_##_name, \
2938 target_core_hba_store_attr_##_name);
2939
2940#define SE_HBA_ATTR_RO(_name) \
2941static struct target_core_hba_attribute \
2942 target_core_hba_##_name = \
2943 __CONFIGFS_EATTR_RO(_name, \
2944 target_core_hba_show_attr_##_name);
2945
2946static ssize_t target_core_hba_show_attr_hba_info(
2947 struct se_hba *hba,
2948 char *page)
2949{
2950 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2951 hba->hba_id, hba->transport->name,
2952 TARGET_CORE_CONFIGFS_VERSION);
2953}
2954
2955SE_HBA_ATTR_RO(hba_info);
2956
2957static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2958 char *page)
2959{
2960 int hba_mode = 0;
2961
2962 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2963 hba_mode = 1;
2964
2965 return sprintf(page, "%d\n", hba_mode);
2966}
2967
2968static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2969 const char *page, size_t count)
2970{
2971 struct se_subsystem_api *transport = hba->transport;
2972 unsigned long mode_flag;
2973 int ret;
2974
2975 if (transport->pmode_enable_hba == NULL)
2976 return -EINVAL;
2977
Jingoo Han57103d72013-07-19 16:22:19 +09002978 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002980 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002981 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 }
2983
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002984 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002985 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986 return -EINVAL;
2987 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988
2989 ret = transport->pmode_enable_hba(hba, mode_flag);
2990 if (ret < 0)
2991 return -EINVAL;
2992 if (ret > 0)
2993 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2994 else if (ret == 0)
2995 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2996
2997 return count;
2998}
2999
3000SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3001
3002CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3003
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003004static void target_core_hba_release(struct config_item *item)
3005{
3006 struct se_hba *hba = container_of(to_config_group(item),
3007 struct se_hba, hba_group);
3008 core_delete_hba(hba);
3009}
3010
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003011static struct configfs_attribute *target_core_hba_attrs[] = {
3012 &target_core_hba_hba_info.attr,
3013 &target_core_hba_hba_mode.attr,
3014 NULL,
3015};
3016
3017static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003018 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003019 .show_attribute = target_core_hba_attr_show,
3020 .store_attribute = target_core_hba_attr_store,
3021};
3022
3023static struct config_item_type target_core_hba_cit = {
3024 .ct_item_ops = &target_core_hba_item_ops,
3025 .ct_group_ops = &target_core_hba_group_ops,
3026 .ct_attrs = target_core_hba_attrs,
3027 .ct_owner = THIS_MODULE,
3028};
3029
3030static struct config_group *target_core_call_addhbatotarget(
3031 struct config_group *group,
3032 const char *name)
3033{
3034 char *se_plugin_str, *str, *str2;
3035 struct se_hba *hba;
3036 char buf[TARGET_CORE_NAME_MAX_LEN];
3037 unsigned long plugin_dep_id = 0;
3038 int ret;
3039
3040 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003041 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003042 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003043 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3044 TARGET_CORE_NAME_MAX_LEN);
3045 return ERR_PTR(-ENAMETOOLONG);
3046 }
3047 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3048
3049 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003050 if (!str) {
3051 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003052 return ERR_PTR(-EINVAL);
3053 }
3054 se_plugin_str = buf;
3055 /*
3056 * Special case for subsystem plugins that have "_" in their names.
3057 * Namely rd_direct and rd_mcp..
3058 */
3059 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003060 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003061 *str2 = '\0'; /* Terminate for *se_plugin_str */
3062 str2++; /* Skip to start of plugin dependent ID */
3063 str = str2;
3064 } else {
3065 *str = '\0'; /* Terminate for *se_plugin_str */
3066 str++; /* Skip to start of plugin dependent ID */
3067 }
3068
Jingoo Han57103d72013-07-19 16:22:19 +09003069 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003070 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003071 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003073 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003074 }
3075 /*
3076 * Load up TCM subsystem plugins if they have not already been loaded.
3077 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003078 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003079
3080 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3081 if (IS_ERR(hba))
3082 return ERR_CAST(hba);
3083
3084 config_group_init_type_name(&hba->hba_group, name,
3085 &target_core_hba_cit);
3086
3087 return &hba->hba_group;
3088}
3089
3090static void target_core_call_delhbafromtarget(
3091 struct config_group *group,
3092 struct config_item *item)
3093{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003094 /*
3095 * core_delete_hba() is called from target_core_hba_item_ops->release()
3096 * -> target_core_hba_release()
3097 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003098 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003099}
3100
3101static struct configfs_group_operations target_core_group_ops = {
3102 .make_group = target_core_call_addhbatotarget,
3103 .drop_item = target_core_call_delhbafromtarget,
3104};
3105
3106static struct config_item_type target_core_cit = {
3107 .ct_item_ops = NULL,
3108 .ct_group_ops = &target_core_group_ops,
3109 .ct_attrs = NULL,
3110 .ct_owner = THIS_MODULE,
3111};
3112
3113/* Stop functions for struct config_item_type target_core_hba_cit */
3114
Axel Lin54550fa2011-03-14 04:06:09 -07003115static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003116{
3117 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3118 struct config_group *lu_gp_cg = NULL;
3119 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 struct t10_alua_lu_gp *lu_gp;
3121 int ret;
3122
Andy Grover6708bb22011-06-08 10:36:43 -07003123 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003124 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3125 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3126
3127 subsys = target_core_subsystem[0];
3128 config_group_init(&subsys->su_group);
3129 mutex_init(&subsys->su_mutex);
3130
Andy Grovere3d6f902011-07-19 08:55:10 +00003131 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003132 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003133 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003134 /*
3135 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3136 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3137 */
3138 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003139 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003140 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003141 if (!target_cg->default_groups) {
3142 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003143 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 goto out_global;
3145 }
3146
Andy Grovere3d6f902011-07-19 08:55:10 +00003147 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003148 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003149 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003150 target_cg->default_groups[1] = NULL;
3151 /*
3152 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3153 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003154 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003155 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003157 if (!hba_cg->default_groups) {
3158 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003159 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 goto out_global;
3161 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003162 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003163 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003164 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 hba_cg->default_groups[1] = NULL;
3166 /*
3167 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3168 * groups under /sys/kernel/config/target/core/alua/
3169 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003170 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003171 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003173 if (!alua_cg->default_groups) {
3174 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003175 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 goto out_global;
3177 }
3178
Andy Grovere3d6f902011-07-19 08:55:10 +00003179 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003181 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 alua_cg->default_groups[1] = NULL;
3183 /*
3184 * Add core/alua/lu_gps/default_lu_gp
3185 */
3186 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003187 if (IS_ERR(lu_gp)) {
3188 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003190 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191
Andy Grovere3d6f902011-07-19 08:55:10 +00003192 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003193 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003195 if (!lu_gp_cg->default_groups) {
3196 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003197 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 goto out_global;
3199 }
3200
3201 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3202 &target_core_alua_lu_gp_cit);
3203 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3204 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003205 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 /*
3207 * Register the target_core_mod subsystem with configfs.
3208 */
3209 ret = configfs_register_subsystem(subsys);
3210 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003211 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003212 ret, subsys->su_group.cg_item.ci_namebuf);
3213 goto out_global;
3214 }
Andy Grover6708bb22011-06-08 10:36:43 -07003215 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3217 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3218 /*
3219 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3220 */
3221 ret = rd_module_init();
3222 if (ret < 0)
3223 goto out;
3224
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003225 ret = core_dev_setup_virtual_lun0();
3226 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227 goto out;
3228
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003229 ret = target_xcopy_setup_pt();
3230 if (ret < 0)
3231 goto out;
3232
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003233 return 0;
3234
3235out:
3236 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237 core_dev_release_virtual_lun0();
3238 rd_module_exit();
3239out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003240 if (default_lu_gp) {
3241 core_alua_free_lu_gp(default_lu_gp);
3242 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 }
3244 if (lu_gp_cg)
3245 kfree(lu_gp_cg->default_groups);
3246 if (alua_cg)
3247 kfree(alua_cg->default_groups);
3248 if (hba_cg)
3249 kfree(hba_cg->default_groups);
3250 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003251 release_se_kmem_caches();
3252 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003253}
3254
Axel Lin54550fa2011-03-14 04:06:09 -07003255static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003256{
3257 struct configfs_subsystem *subsys;
3258 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3259 struct config_item *item;
3260 int i;
3261
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 subsys = target_core_subsystem[0];
3263
Andy Grovere3d6f902011-07-19 08:55:10 +00003264 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003265 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3266 item = &lu_gp_cg->default_groups[i]->cg_item;
3267 lu_gp_cg->default_groups[i] = NULL;
3268 config_item_put(item);
3269 }
3270 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003271 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003272
Andy Grovere3d6f902011-07-19 08:55:10 +00003273 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003274 for (i = 0; alua_cg->default_groups[i]; i++) {
3275 item = &alua_cg->default_groups[i]->cg_item;
3276 alua_cg->default_groups[i] = NULL;
3277 config_item_put(item);
3278 }
3279 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003280 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003281
Andy Grovere3d6f902011-07-19 08:55:10 +00003282 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283 for (i = 0; hba_cg->default_groups[i]; i++) {
3284 item = &hba_cg->default_groups[i]->cg_item;
3285 hba_cg->default_groups[i] = NULL;
3286 config_item_put(item);
3287 }
3288 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003289 hba_cg->default_groups = NULL;
3290 /*
3291 * We expect subsys->su_group.default_groups to be released
3292 * by configfs subsystem provider logic..
3293 */
3294 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 kfree(subsys->su_group.default_groups);
3296
Andy Grovere3d6f902011-07-19 08:55:10 +00003297 core_alua_free_lu_gp(default_lu_gp);
3298 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003299
Andy Grover6708bb22011-06-08 10:36:43 -07003300 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003301 " Infrastructure\n");
3302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003303 core_dev_release_virtual_lun0();
3304 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003305 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003306 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307}
3308
3309MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3310MODULE_AUTHOR("nab@Linux-iSCSI.org");
3311MODULE_LICENSE("GPL");
3312
3313module_init(target_core_init_configfs);
3314module_exit(target_core_exit_configfs);