blob: 7b473b66da7bb0430b127ca87d3f135b65843310 [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 Bellinger12d2338422011-03-14 04:06:11 -07006 * Copyright (c) 2008-2011 Rising Tide Systems
7 * Copyright (c) 2008-2011 Linux-iSCSI.org
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080026#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/namei.h>
31#include <linux/slab.h>
32#include <linux/types.h>
33#include <linux/delay.h>
34#include <linux/unistd.h>
35#include <linux/string.h>
36#include <linux/parser.h>
37#include <linux/syscalls.h>
38#include <linux/configfs.h>
Andy Grovere3d6f902011-07-19 08:55:10 +000039#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040
41#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050042#include <target/target_core_backend.h>
43#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044#include <target/target_core_fabric_configfs.h>
45#include <target/target_core_configfs.h>
46#include <target/configfs_macros.h>
47
Christoph Hellwige26d99a2011-11-14 12:30:30 -050048#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050#include "target_core_pr.h"
51#include "target_core_rd.h"
52
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",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
181
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,
187 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
188 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
189 &TF_CIT_TMPL(tf)->tfc_discovery_cit);
190
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
272static struct configfs_subsystem *target_core_subsystem[] = {
273 &target_core_fabrics,
274 NULL,
275};
276
277/*##############################################################################
278// Start functions called by external Target Fabrics Modules
279//############################################################################*/
280
281/*
282 * First function called by fabric modules to:
283 *
284 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
285 * 2) Add struct target_fabric_configfs to g_tf_list
286 * 3) Return struct target_fabric_configfs to fabric module to be passed
287 * into target_fabric_configfs_register().
288 */
289struct target_fabric_configfs *target_fabric_configfs_init(
290 struct module *fabric_mod,
291 const char *name)
292{
293 struct target_fabric_configfs *tf;
294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700299 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000302 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 }
304
305 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700306 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000307 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308
309 INIT_LIST_HEAD(&tf->tf_list);
310 atomic_set(&tf->tf_access_cnt, 0);
311 /*
312 * Setup the default generic struct config_item_type's (cits) in
313 * struct target_fabric_configfs->tf_cit_tmpl
314 */
315 tf->tf_module = fabric_mod;
316 target_fabric_setup_cits(tf);
317
318 tf->tf_subsys = target_core_subsystem[0];
319 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
320
321 mutex_lock(&g_tf_lock);
322 list_add_tail(&tf->tf_list, &g_tf_list);
323 mutex_unlock(&g_tf_lock);
324
Andy Grover6708bb22011-06-08 10:36:43 -0700325 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700327 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 " %s\n", tf, tf->tf_name);
329 return tf;
330}
331EXPORT_SYMBOL(target_fabric_configfs_init);
332
333/*
334 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
335 */
336void target_fabric_configfs_free(
337 struct target_fabric_configfs *tf)
338{
339 mutex_lock(&g_tf_lock);
340 list_del(&tf->tf_list);
341 mutex_unlock(&g_tf_lock);
342
343 kfree(tf);
344}
345EXPORT_SYMBOL(target_fabric_configfs_free);
346
347/*
348 * Perform a sanity check of the passed tf->tf_ops before completing
349 * TCM fabric module registration.
350 */
351static int target_fabric_tf_ops_check(
352 struct target_fabric_configfs *tf)
353{
354 struct target_core_fabric_ops *tfo = &tf->tf_ops;
355
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->get_fabric_name) {
357 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->get_fabric_proto_ident) {
361 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->tpg_get_default_depth) {
373 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Andy Grover6708bb22011-06-08 10:36:43 -0700376 if (!tfo->tpg_get_pr_transport_id) {
377 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 return -EINVAL;
379 }
Andy Grover6708bb22011-06-08 10:36:43 -0700380 if (!tfo->tpg_get_pr_transport_id_len) {
381 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 return -EINVAL;
383 }
Andy Grover6708bb22011-06-08 10:36:43 -0700384 if (!tfo->tpg_check_demo_mode) {
385 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 return -EINVAL;
387 }
Andy Grover6708bb22011-06-08 10:36:43 -0700388 if (!tfo->tpg_check_demo_mode_cache) {
389 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 return -EINVAL;
391 }
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (!tfo->tpg_check_demo_mode_write_protect) {
393 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 return -EINVAL;
395 }
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!tfo->tpg_check_prod_mode_write_protect) {
397 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 return -EINVAL;
399 }
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!tfo->tpg_alloc_fabric_acl) {
401 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return -EINVAL;
403 }
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->tpg_release_fabric_acl) {
405 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->tpg_get_inst_index) {
409 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400412 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700413 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->shutdown_session) {
417 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
Andy Grover6708bb22011-06-08 10:36:43 -0700420 if (!tfo->close_session) {
421 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 return -EINVAL;
423 }
Andy Grover6708bb22011-06-08 10:36:43 -0700424 if (!tfo->sess_get_index) {
425 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 return -EINVAL;
427 }
Andy Grover6708bb22011-06-08 10:36:43 -0700428 if (!tfo->write_pending) {
429 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 return -EINVAL;
431 }
Andy Grover6708bb22011-06-08 10:36:43 -0700432 if (!tfo->write_pending_status) {
433 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434 return -EINVAL;
435 }
Andy Grover6708bb22011-06-08 10:36:43 -0700436 if (!tfo->set_default_node_attributes) {
437 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 return -EINVAL;
439 }
Andy Grover6708bb22011-06-08 10:36:43 -0700440 if (!tfo->get_task_tag) {
441 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 return -EINVAL;
443 }
Andy Grover6708bb22011-06-08 10:36:43 -0700444 if (!tfo->get_cmd_state) {
445 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 return -EINVAL;
447 }
Andy Grover6708bb22011-06-08 10:36:43 -0700448 if (!tfo->queue_data_in) {
449 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 return -EINVAL;
451 }
Andy Grover6708bb22011-06-08 10:36:43 -0700452 if (!tfo->queue_status) {
453 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 return -EINVAL;
455 }
Andy Grover6708bb22011-06-08 10:36:43 -0700456 if (!tfo->queue_tm_rsp) {
457 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 return -EINVAL;
459 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 /*
461 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
462 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
463 * target_core_fabric_configfs.c WWN+TPG group context code.
464 */
Andy Grover6708bb22011-06-08 10:36:43 -0700465 if (!tfo->fabric_make_wwn) {
466 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 return -EINVAL;
468 }
Andy Grover6708bb22011-06-08 10:36:43 -0700469 if (!tfo->fabric_drop_wwn) {
470 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 return -EINVAL;
472 }
Andy Grover6708bb22011-06-08 10:36:43 -0700473 if (!tfo->fabric_make_tpg) {
474 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 return -EINVAL;
476 }
Andy Grover6708bb22011-06-08 10:36:43 -0700477 if (!tfo->fabric_drop_tpg) {
478 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return -EINVAL;
480 }
481
482 return 0;
483}
484
485/*
486 * Called 2nd from fabric module with returned parameter of
487 * struct target_fabric_configfs * from target_fabric_configfs_init().
488 *
489 * Upon a successful registration, the new fabric's struct config_item is
490 * return. Also, a pointer to this struct is set in the passed
491 * struct target_fabric_configfs.
492 */
493int target_fabric_configfs_register(
494 struct target_fabric_configfs *tf)
495{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 int ret;
497
Andy Grover6708bb22011-06-08 10:36:43 -0700498 if (!tf) {
499 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 " pointer\n");
501 return -EINVAL;
502 }
Andy Grover6708bb22011-06-08 10:36:43 -0700503 if (!tf->tf_subsys) {
504 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 " pointer\n");
506 return -EINVAL;
507 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508 ret = target_fabric_tf_ops_check(tf);
509 if (ret < 0)
510 return ret;
511
Andy Grover6708bb22011-06-08 10:36:43 -0700512 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 ">>>>>>>>>>\n");
514 return 0;
515}
516EXPORT_SYMBOL(target_fabric_configfs_register);
517
518void target_fabric_configfs_deregister(
519 struct target_fabric_configfs *tf)
520{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521 struct configfs_subsystem *su;
522
Andy Grover6708bb22011-06-08 10:36:43 -0700523 if (!tf) {
524 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 "configfs\n");
526 return;
527 }
528 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700529 if (!su) {
530 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531 " pointer\n");
532 return;
533 }
Andy Grover6708bb22011-06-08 10:36:43 -0700534 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 ">>>>>>>>>>>>\n");
536 mutex_lock(&g_tf_lock);
537 if (atomic_read(&tf->tf_access_cnt)) {
538 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700539 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 tf->tf_name);
541 BUG();
542 }
543 list_del(&tf->tf_list);
544 mutex_unlock(&g_tf_lock);
545
Andy Grover6708bb22011-06-08 10:36:43 -0700546 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 " %s\n", tf->tf_name);
548 tf->tf_module = NULL;
549 tf->tf_subsys = NULL;
550 kfree(tf);
551
Andy Grover6708bb22011-06-08 10:36:43 -0700552 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554}
555EXPORT_SYMBOL(target_fabric_configfs_deregister);
556
557/*##############################################################################
558// Stop functions called by external Target Fabrics Modules
559//############################################################################*/
560
561/* Start functions for struct config_item_type target_core_dev_attrib_cit */
562
563#define DEF_DEV_ATTRIB_SHOW(_name) \
564static ssize_t target_core_dev_show_attr_##_name( \
565 struct se_dev_attrib *da, \
566 char *page) \
567{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400568 return snprintf(page, PAGE_SIZE, "%u\n", \
569 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570}
571
572#define DEF_DEV_ATTRIB_STORE(_name) \
573static ssize_t target_core_dev_store_attr_##_name( \
574 struct se_dev_attrib *da, \
575 const char *page, \
576 size_t count) \
577{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 unsigned long val; \
579 int ret; \
580 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 ret = strict_strtoul(page, 0, &val); \
582 if (ret < 0) { \
Andy Grover6708bb22011-06-08 10:36:43 -0700583 pr_err("strict_strtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 " ret: %d\n", ret); \
585 return -EINVAL; \
586 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400587 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 \
589 return (!ret) ? count : -EINVAL; \
590}
591
592#define DEF_DEV_ATTRIB(_name) \
593DEF_DEV_ATTRIB_SHOW(_name); \
594DEF_DEV_ATTRIB_STORE(_name);
595
596#define DEF_DEV_ATTRIB_RO(_name) \
597DEF_DEV_ATTRIB_SHOW(_name);
598
599CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
600#define SE_DEV_ATTR(_name, _mode) \
601static struct target_core_dev_attrib_attribute \
602 target_core_dev_attrib_##_name = \
603 __CONFIGFS_EATTR(_name, _mode, \
604 target_core_dev_show_attr_##_name, \
605 target_core_dev_store_attr_##_name);
606
607#define SE_DEV_ATTR_RO(_name); \
608static struct target_core_dev_attrib_attribute \
609 target_core_dev_attrib_##_name = \
610 __CONFIGFS_EATTR_RO(_name, \
611 target_core_dev_show_attr_##_name);
612
613DEF_DEV_ATTRIB(emulate_dpo);
614SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
615
616DEF_DEV_ATTRIB(emulate_fua_write);
617SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
618
619DEF_DEV_ATTRIB(emulate_fua_read);
620SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
621
622DEF_DEV_ATTRIB(emulate_write_cache);
623SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
624
625DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
626SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
627
628DEF_DEV_ATTRIB(emulate_tas);
629SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
630
631DEF_DEV_ATTRIB(emulate_tpu);
632SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
633
634DEF_DEV_ATTRIB(emulate_tpws);
635SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
636
637DEF_DEV_ATTRIB(enforce_pr_isids);
638SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
639
Roland Dreiere22a7f02011-07-05 13:34:52 -0700640DEF_DEV_ATTRIB(is_nonrot);
641SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
642
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700643DEF_DEV_ATTRIB(emulate_rest_reord);
644SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
645
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646DEF_DEV_ATTRIB_RO(hw_block_size);
647SE_DEV_ATTR_RO(hw_block_size);
648
649DEF_DEV_ATTRIB(block_size);
650SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
651
652DEF_DEV_ATTRIB_RO(hw_max_sectors);
653SE_DEV_ATTR_RO(hw_max_sectors);
654
Roland Dreier015487b2012-02-13 16:18:17 -0800655DEF_DEV_ATTRIB(fabric_max_sectors);
656SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
657
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658DEF_DEV_ATTRIB(optimal_sectors);
659SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
660
661DEF_DEV_ATTRIB_RO(hw_queue_depth);
662SE_DEV_ATTR_RO(hw_queue_depth);
663
664DEF_DEV_ATTRIB(queue_depth);
665SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
666
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667DEF_DEV_ATTRIB(max_unmap_lba_count);
668SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
669
670DEF_DEV_ATTRIB(max_unmap_block_desc_count);
671SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
672
673DEF_DEV_ATTRIB(unmap_granularity);
674SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
675
676DEF_DEV_ATTRIB(unmap_granularity_alignment);
677SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
678
679CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
680
681static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
682 &target_core_dev_attrib_emulate_dpo.attr,
683 &target_core_dev_attrib_emulate_fua_write.attr,
684 &target_core_dev_attrib_emulate_fua_read.attr,
685 &target_core_dev_attrib_emulate_write_cache.attr,
686 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
687 &target_core_dev_attrib_emulate_tas.attr,
688 &target_core_dev_attrib_emulate_tpu.attr,
689 &target_core_dev_attrib_emulate_tpws.attr,
690 &target_core_dev_attrib_enforce_pr_isids.attr,
Roland Dreiere22a7f02011-07-05 13:34:52 -0700691 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700692 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 &target_core_dev_attrib_hw_block_size.attr,
694 &target_core_dev_attrib_block_size.attr,
695 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800696 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 &target_core_dev_attrib_optimal_sectors.attr,
698 &target_core_dev_attrib_hw_queue_depth.attr,
699 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 &target_core_dev_attrib_max_unmap_lba_count.attr,
701 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
702 &target_core_dev_attrib_unmap_granularity.attr,
703 &target_core_dev_attrib_unmap_granularity_alignment.attr,
704 NULL,
705};
706
707static struct configfs_item_operations target_core_dev_attrib_ops = {
708 .show_attribute = target_core_dev_attrib_attr_show,
709 .store_attribute = target_core_dev_attrib_attr_store,
710};
711
712static struct config_item_type target_core_dev_attrib_cit = {
713 .ct_item_ops = &target_core_dev_attrib_ops,
714 .ct_attrs = target_core_dev_attrib_attrs,
715 .ct_owner = THIS_MODULE,
716};
717
718/* End functions for struct config_item_type target_core_dev_attrib_cit */
719
720/* Start functions for struct config_item_type target_core_dev_wwn_cit */
721
722CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
723#define SE_DEV_WWN_ATTR(_name, _mode) \
724static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
725 __CONFIGFS_EATTR(_name, _mode, \
726 target_core_dev_wwn_show_attr_##_name, \
727 target_core_dev_wwn_store_attr_##_name);
728
729#define SE_DEV_WWN_ATTR_RO(_name); \
730do { \
731 static struct target_core_dev_wwn_attribute \
732 target_core_dev_wwn_##_name = \
733 __CONFIGFS_EATTR_RO(_name, \
734 target_core_dev_wwn_show_attr_##_name); \
735} while (0);
736
737/*
738 * VPD page 0x80 Unit serial
739 */
740static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
741 struct t10_wwn *t10_wwn,
742 char *page)
743{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
745 &t10_wwn->unit_serial[0]);
746}
747
748static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
749 struct t10_wwn *t10_wwn,
750 const char *page,
751 size_t count)
752{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400753 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
755
756 /*
757 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
758 * from the struct scsi_device level firmware, do not allow
759 * VPD Unit Serial to be emulated.
760 *
761 * Note this struct scsi_device could also be emulating VPD
762 * information from its drivers/scsi LLD. But for now we assume
763 * it is doing 'the right thing' wrt a world wide unique
764 * VPD Unit Serial Number that OS dependent multipath can depend on.
765 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400766 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700767 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800768 " Unit Serial, ignoring request\n");
769 return -EOPNOTSUPP;
770 }
771
Dan Carpenter60d645a2011-06-15 10:03:05 -0700772 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700773 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
775 return -EOVERFLOW;
776 }
777 /*
778 * Check to see if any active $FABRIC_MOD exports exist. If they
779 * do exist, fail here as changing this information on the fly
780 * (underneath the initiator side OS dependent multipath code)
781 * could cause negative effects.
782 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400783 if (dev->export_count) {
784 pr_err("Unable to set VPD Unit Serial while"
785 " active %d $FABRIC_MOD exports exist\n",
786 dev->export_count);
787 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400789
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800790 /*
791 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
792 *
793 * Also, strip any newline added from the userspace
794 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
795 */
796 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
797 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400798 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400800 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801
Andy Grover6708bb22011-06-08 10:36:43 -0700802 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400803 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804
805 return count;
806}
807
808SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
809
810/*
811 * VPD page 0x83 Protocol Identifier
812 */
813static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
814 struct t10_wwn *t10_wwn,
815 char *page)
816{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800817 struct t10_vpd *vpd;
818 unsigned char buf[VPD_TMP_BUF_SIZE];
819 ssize_t len = 0;
820
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821 memset(buf, 0, VPD_TMP_BUF_SIZE);
822
823 spin_lock(&t10_wwn->t10_vpd_lock);
824 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700825 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800826 continue;
827
828 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
829
Andy Grover6708bb22011-06-08 10:36:43 -0700830 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 break;
832
833 len += sprintf(page+len, "%s", buf);
834 }
835 spin_unlock(&t10_wwn->t10_vpd_lock);
836
837 return len;
838}
839
840static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
841 struct t10_wwn *t10_wwn,
842 const char *page,
843 size_t count)
844{
845 return -ENOSYS;
846}
847
848SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
849
850/*
851 * Generic wrapper for dumping VPD identifiers by association.
852 */
853#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
854static ssize_t target_core_dev_wwn_show_attr_##_name( \
855 struct t10_wwn *t10_wwn, \
856 char *page) \
857{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858 struct t10_vpd *vpd; \
859 unsigned char buf[VPD_TMP_BUF_SIZE]; \
860 ssize_t len = 0; \
861 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800862 spin_lock(&t10_wwn->t10_vpd_lock); \
863 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
864 if (vpd->association != _assoc) \
865 continue; \
866 \
867 memset(buf, 0, VPD_TMP_BUF_SIZE); \
868 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700869 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800870 break; \
871 len += sprintf(page+len, "%s", buf); \
872 \
873 memset(buf, 0, VPD_TMP_BUF_SIZE); \
874 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700875 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 break; \
877 len += sprintf(page+len, "%s", buf); \
878 \
879 memset(buf, 0, VPD_TMP_BUF_SIZE); \
880 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700881 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800882 break; \
883 len += sprintf(page+len, "%s", buf); \
884 } \
885 spin_unlock(&t10_wwn->t10_vpd_lock); \
886 \
887 return len; \
888}
889
890/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700891 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800892 */
893DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
894
895static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
896 struct t10_wwn *t10_wwn,
897 const char *page,
898 size_t count)
899{
900 return -ENOSYS;
901}
902
903SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
904
905/*
906 * VPD page 0x83 Association: Target Port
907 */
908DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
909
910static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
911 struct t10_wwn *t10_wwn,
912 const char *page,
913 size_t count)
914{
915 return -ENOSYS;
916}
917
918SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
919
920/*
921 * VPD page 0x83 Association: SCSI Target Device
922 */
923DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
924
925static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
926 struct t10_wwn *t10_wwn,
927 const char *page,
928 size_t count)
929{
930 return -ENOSYS;
931}
932
933SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
934
935CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
936
937static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
938 &target_core_dev_wwn_vpd_unit_serial.attr,
939 &target_core_dev_wwn_vpd_protocol_identifier.attr,
940 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
941 &target_core_dev_wwn_vpd_assoc_target_port.attr,
942 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
943 NULL,
944};
945
946static struct configfs_item_operations target_core_dev_wwn_ops = {
947 .show_attribute = target_core_dev_wwn_attr_show,
948 .store_attribute = target_core_dev_wwn_attr_store,
949};
950
951static struct config_item_type target_core_dev_wwn_cit = {
952 .ct_item_ops = &target_core_dev_wwn_ops,
953 .ct_attrs = target_core_dev_wwn_attrs,
954 .ct_owner = THIS_MODULE,
955};
956
957/* End functions for struct config_item_type target_core_dev_wwn_cit */
958
959/* Start functions for struct config_item_type target_core_dev_pr_cit */
960
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400961CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800962#define SE_DEV_PR_ATTR(_name, _mode) \
963static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
964 __CONFIGFS_EATTR(_name, _mode, \
965 target_core_dev_pr_show_attr_##_name, \
966 target_core_dev_pr_store_attr_##_name);
967
968#define SE_DEV_PR_ATTR_RO(_name); \
969static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
970 __CONFIGFS_EATTR_RO(_name, \
971 target_core_dev_pr_show_attr_##_name);
972
Christoph Hellwigd977f432012-10-10 17:37:15 -0400973static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
974 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975{
976 struct se_node_acl *se_nacl;
977 struct t10_pr_registration *pr_reg;
978 char i_buf[PR_REG_ISID_ID_LEN];
979 int prf_isid;
980
981 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
982
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800983 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400984 if (!pr_reg)
985 return sprintf(page, "No SPC-3 Reservation holder\n");
986
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800987 se_nacl = pr_reg->pr_reg_nacl;
988 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
989 PR_REG_ISID_ID_LEN);
990
Christoph Hellwigd977f432012-10-10 17:37:15 -0400991 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000992 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800993 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800994}
995
Christoph Hellwigd977f432012-10-10 17:37:15 -0400996static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
997 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800998{
999 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001000 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001001
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001003 if (se_nacl) {
1004 len = sprintf(page,
1005 "SPC-2 Reservation: %s Initiator: %s\n",
1006 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1007 se_nacl->initiatorname);
1008 } else {
1009 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001011 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012}
1013
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001014static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1015 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001016{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001017 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018
Christoph Hellwigd977f432012-10-10 17:37:15 -04001019 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1020 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001021
Christoph Hellwigd977f432012-10-10 17:37:15 -04001022 spin_lock(&dev->dev_reservation_lock);
1023 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1024 ret = target_core_dev_pr_show_spc2_res(dev, page);
1025 else
1026 ret = target_core_dev_pr_show_spc3_res(dev, page);
1027 spin_unlock(&dev->dev_reservation_lock);
1028 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029}
1030
1031SE_DEV_PR_ATTR_RO(res_holder);
1032
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001033static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001034 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001035{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 ssize_t len = 0;
1037
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001039 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001041 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001042 len = sprintf(page, "SPC-3 Reservation: All Target"
1043 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001044 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001045 len = sprintf(page, "SPC-3 Reservation: Single"
1046 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001047 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048
Christoph Hellwigd977f432012-10-10 17:37:15 -04001049 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001050 return len;
1051}
1052
1053SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001056 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001058 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059}
1060
1061SE_DEV_PR_ATTR_RO(res_pr_generation);
1062
1063/*
1064 * res_pr_holder_tg_port
1065 */
1066static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001067 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069 struct se_node_acl *se_nacl;
1070 struct se_lun *lun;
1071 struct se_portal_group *se_tpg;
1072 struct t10_pr_registration *pr_reg;
1073 struct target_core_fabric_ops *tfo;
1074 ssize_t len = 0;
1075
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001076 spin_lock(&dev->dev_reservation_lock);
1077 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001078 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001080 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001081 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001082
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001083 se_nacl = pr_reg->pr_reg_nacl;
1084 se_tpg = se_nacl->se_tpg;
1085 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001086 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087
1088 len += sprintf(page+len, "SPC-3 Reservation: %s"
1089 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1090 tfo->tpg_get_wwn(se_tpg));
1091 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001092 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1094 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1095 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096
Christoph Hellwigd977f432012-10-10 17:37:15 -04001097out_unlock:
1098 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 return len;
1100}
1101
1102SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1103
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001104static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001105 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106{
1107 struct target_core_fabric_ops *tfo;
1108 struct t10_pr_registration *pr_reg;
1109 unsigned char buf[384];
1110 char i_buf[PR_REG_ISID_ID_LEN];
1111 ssize_t len = 0;
1112 int reg_count = 0, prf_isid;
1113
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001114 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1115
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001116 spin_lock(&dev->t10_pr.registration_lock);
1117 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118 pr_reg_list) {
1119
1120 memset(buf, 0, 384);
1121 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1122 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1123 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1124 PR_REG_ISID_ID_LEN);
1125 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1126 tfo->get_fabric_name(),
1127 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1128 &i_buf[0] : "", pr_reg->pr_res_key,
1129 pr_reg->pr_res_generation);
1130
Andy Grover6708bb22011-06-08 10:36:43 -07001131 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 break;
1133
1134 len += sprintf(page+len, "%s", buf);
1135 reg_count++;
1136 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001137 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138
Andy Grover6708bb22011-06-08 10:36:43 -07001139 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 len += sprintf(page+len, "None\n");
1141
1142 return len;
1143}
1144
1145SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001148 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150 struct t10_pr_registration *pr_reg;
1151 ssize_t len = 0;
1152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 spin_lock(&dev->dev_reservation_lock);
1154 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001155 if (pr_reg) {
1156 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1157 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1158 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001159 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161
Christoph Hellwigd977f432012-10-10 17:37:15 -04001162 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 return len;
1164}
1165
1166SE_DEV_PR_ATTR_RO(res_pr_type);
1167
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001169 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001171 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1172 return sprintf(page, "SPC_PASSTHROUGH\n");
1173 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1174 return sprintf(page, "SPC2_RESERVATIONS\n");
1175 else
1176 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177}
1178
1179SE_DEV_PR_ATTR_RO(res_type);
1180
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001182 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001184 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185 return 0;
1186
1187 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001188 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189}
1190
1191SE_DEV_PR_ATTR_RO(res_aptpl_active);
1192
1193/*
1194 * res_aptpl_metadata
1195 */
1196static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001197 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001199 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001200 return 0;
1201
1202 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1203}
1204
1205enum {
1206 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1207 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1208 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1209 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1210};
1211
1212static match_table_t tokens = {
1213 {Opt_initiator_fabric, "initiator_fabric=%s"},
1214 {Opt_initiator_node, "initiator_node=%s"},
1215 {Opt_initiator_sid, "initiator_sid=%s"},
1216 {Opt_sa_res_key, "sa_res_key=%s"},
1217 {Opt_res_holder, "res_holder=%d"},
1218 {Opt_res_type, "res_type=%d"},
1219 {Opt_res_scope, "res_scope=%d"},
1220 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1221 {Opt_mapped_lun, "mapped_lun=%d"},
1222 {Opt_target_fabric, "target_fabric=%s"},
1223 {Opt_target_node, "target_node=%s"},
1224 {Opt_tpgt, "tpgt=%d"},
1225 {Opt_port_rtpi, "port_rtpi=%d"},
1226 {Opt_target_lun, "target_lun=%d"},
1227 {Opt_err, NULL}
1228};
1229
1230static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001231 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 const char *page,
1233 size_t count)
1234{
Jesper Juhl6d180252011-03-14 04:05:56 -07001235 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1236 unsigned char *t_fabric = NULL, *t_port = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001237 char *orig, *ptr, *arg_p, *opts;
1238 substring_t args[MAX_OPT_ARGS];
1239 unsigned long long tmp_ll;
1240 u64 sa_res_key = 0;
1241 u32 mapped_lun = 0, target_lun = 0;
1242 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1243 u16 port_rpti = 0, tpgt = 0;
1244 u8 type = 0, scope;
1245
Christoph Hellwigd977f432012-10-10 17:37:15 -04001246 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1247 return 0;
1248 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249 return 0;
1250
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001251 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001252 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 " active fabric exports exist\n");
1254 return -EINVAL;
1255 }
1256
1257 opts = kstrdup(page, GFP_KERNEL);
1258 if (!opts)
1259 return -ENOMEM;
1260
1261 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001262 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263 if (!*ptr)
1264 continue;
1265
1266 token = match_token(ptr, tokens, args);
1267 switch (token) {
1268 case Opt_initiator_fabric:
1269 i_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001270 if (!i_fabric) {
1271 ret = -ENOMEM;
1272 goto out;
1273 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 break;
1275 case Opt_initiator_node:
1276 i_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001277 if (!i_port) {
1278 ret = -ENOMEM;
1279 goto out;
1280 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001281 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001282 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1284 PR_APTPL_MAX_IPORT_LEN);
1285 ret = -EINVAL;
1286 break;
1287 }
1288 break;
1289 case Opt_initiator_sid:
1290 isid = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001291 if (!isid) {
1292 ret = -ENOMEM;
1293 goto out;
1294 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001295 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001296 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297 "= exceeds PR_REG_ISID_LEN: %d\n",
1298 PR_REG_ISID_LEN);
1299 ret = -EINVAL;
1300 break;
1301 }
1302 break;
1303 case Opt_sa_res_key:
1304 arg_p = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001305 if (!arg_p) {
1306 ret = -ENOMEM;
1307 goto out;
1308 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1310 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_err("strict_strtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 " sa_res_key=\n");
1313 goto out;
1314 }
1315 sa_res_key = (u64)tmp_ll;
1316 break;
1317 /*
1318 * PR APTPL Metadata for Reservation
1319 */
1320 case Opt_res_holder:
1321 match_int(args, &arg);
1322 res_holder = arg;
1323 break;
1324 case Opt_res_type:
1325 match_int(args, &arg);
1326 type = (u8)arg;
1327 break;
1328 case Opt_res_scope:
1329 match_int(args, &arg);
1330 scope = (u8)arg;
1331 break;
1332 case Opt_res_all_tg_pt:
1333 match_int(args, &arg);
1334 all_tg_pt = (int)arg;
1335 break;
1336 case Opt_mapped_lun:
1337 match_int(args, &arg);
1338 mapped_lun = (u32)arg;
1339 break;
1340 /*
1341 * PR APTPL Metadata for Target Port
1342 */
1343 case Opt_target_fabric:
1344 t_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001345 if (!t_fabric) {
1346 ret = -ENOMEM;
1347 goto out;
1348 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 break;
1350 case Opt_target_node:
1351 t_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001352 if (!t_port) {
1353 ret = -ENOMEM;
1354 goto out;
1355 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001356 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001357 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001358 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1359 PR_APTPL_MAX_TPORT_LEN);
1360 ret = -EINVAL;
1361 break;
1362 }
1363 break;
1364 case Opt_tpgt:
1365 match_int(args, &arg);
1366 tpgt = (u16)arg;
1367 break;
1368 case Opt_port_rtpi:
1369 match_int(args, &arg);
1370 port_rpti = (u16)arg;
1371 break;
1372 case Opt_target_lun:
1373 match_int(args, &arg);
1374 target_lun = (u32)arg;
1375 break;
1376 default:
1377 break;
1378 }
1379 }
1380
Andy Grover6708bb22011-06-08 10:36:43 -07001381 if (!i_port || !t_port || !sa_res_key) {
1382 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 ret = -EINVAL;
1384 goto out;
1385 }
1386
1387 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001388 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 " holder\n", type);
1390 ret = -EINVAL;
1391 goto out;
1392 }
1393
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001394 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1396 res_holder, all_tg_pt, type);
1397out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001398 kfree(i_fabric);
1399 kfree(i_port);
1400 kfree(isid);
1401 kfree(t_fabric);
1402 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001403 kfree(orig);
1404 return (ret == 0) ? count : ret;
1405}
1406
1407SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1408
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001409CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001410
1411static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1412 &target_core_dev_pr_res_holder.attr,
1413 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1414 &target_core_dev_pr_res_pr_generation.attr,
1415 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1416 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1417 &target_core_dev_pr_res_pr_type.attr,
1418 &target_core_dev_pr_res_type.attr,
1419 &target_core_dev_pr_res_aptpl_active.attr,
1420 &target_core_dev_pr_res_aptpl_metadata.attr,
1421 NULL,
1422};
1423
1424static struct configfs_item_operations target_core_dev_pr_ops = {
1425 .show_attribute = target_core_dev_pr_attr_show,
1426 .store_attribute = target_core_dev_pr_attr_store,
1427};
1428
1429static struct config_item_type target_core_dev_pr_cit = {
1430 .ct_item_ops = &target_core_dev_pr_ops,
1431 .ct_attrs = target_core_dev_pr_attrs,
1432 .ct_owner = THIS_MODULE,
1433};
1434
1435/* End functions for struct config_item_type target_core_dev_pr_cit */
1436
1437/* Start functions for struct config_item_type target_core_dev_cit */
1438
1439static ssize_t target_core_show_dev_info(void *p, char *page)
1440{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001441 struct se_device *dev = p;
1442 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 int bl = 0;
1444 ssize_t read_bytes = 0;
1445
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001446 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001448 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 return read_bytes;
1450}
1451
1452static struct target_core_configfs_attribute target_core_attr_dev_info = {
1453 .attr = { .ca_owner = THIS_MODULE,
1454 .ca_name = "info",
1455 .ca_mode = S_IRUGO },
1456 .show = target_core_show_dev_info,
1457 .store = NULL,
1458};
1459
1460static ssize_t target_core_store_dev_control(
1461 void *p,
1462 const char *page,
1463 size_t count)
1464{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001465 struct se_device *dev = p;
1466 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001468 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469}
1470
1471static struct target_core_configfs_attribute target_core_attr_dev_control = {
1472 .attr = { .ca_owner = THIS_MODULE,
1473 .ca_name = "control",
1474 .ca_mode = S_IWUSR },
1475 .show = NULL,
1476 .store = target_core_store_dev_control,
1477};
1478
1479static ssize_t target_core_show_dev_alias(void *p, char *page)
1480{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001481 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001483 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484 return 0;
1485
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001486 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487}
1488
1489static ssize_t target_core_store_dev_alias(
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_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496 ssize_t read_bytes;
1497
1498 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001499 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1501 SE_DEV_ALIAS_LEN-1);
1502 return -EINVAL;
1503 }
1504
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001505 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001506 if (!read_bytes)
1507 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001508 if (dev->dev_alias[read_bytes - 1] == '\n')
1509 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001510
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001511 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001512
Andy Grover6708bb22011-06-08 10:36:43 -07001513 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001515 config_item_name(&dev->dev_group.cg_item),
1516 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001517
1518 return read_bytes;
1519}
1520
1521static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1522 .attr = { .ca_owner = THIS_MODULE,
1523 .ca_name = "alias",
1524 .ca_mode = S_IRUGO | S_IWUSR },
1525 .show = target_core_show_dev_alias,
1526 .store = target_core_store_dev_alias,
1527};
1528
1529static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1530{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001531 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001533 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001534 return 0;
1535
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001536 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001537}
1538
1539static ssize_t target_core_store_dev_udev_path(
1540 void *p,
1541 const char *page,
1542 size_t count)
1543{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001544 struct se_device *dev = p;
1545 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001546 ssize_t read_bytes;
1547
1548 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001549 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1551 SE_UDEV_PATH_LEN-1);
1552 return -EINVAL;
1553 }
1554
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001555 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001557 if (!read_bytes)
1558 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001559 if (dev->udev_path[read_bytes - 1] == '\n')
1560 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001561
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001562 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001563
Andy Grover6708bb22011-06-08 10:36:43 -07001564 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001565 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001566 config_item_name(&dev->dev_group.cg_item),
1567 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568
1569 return read_bytes;
1570}
1571
1572static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1573 .attr = { .ca_owner = THIS_MODULE,
1574 .ca_name = "udev_path",
1575 .ca_mode = S_IRUGO | S_IWUSR },
1576 .show = target_core_show_dev_udev_path,
1577 .store = target_core_store_dev_udev_path,
1578};
1579
1580static ssize_t target_core_store_dev_enable(
1581 void *p,
1582 const char *page,
1583 size_t count)
1584{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001585 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001587 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588
1589 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001590 if (!ptr) {
1591 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 " is \"1\"\n");
1593 return -EINVAL;
1594 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001595
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001596 ret = target_configure_device(dev);
1597 if (ret)
1598 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001599 return count;
1600}
1601
1602static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1603 .attr = { .ca_owner = THIS_MODULE,
1604 .ca_name = "enable",
1605 .ca_mode = S_IWUSR },
1606 .show = NULL,
1607 .store = target_core_store_dev_enable,
1608};
1609
1610static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1611{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001612 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001613 struct config_item *lu_ci;
1614 struct t10_alua_lu_gp *lu_gp;
1615 struct t10_alua_lu_gp_member *lu_gp_mem;
1616 ssize_t len = 0;
1617
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001618 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001619 if (!lu_gp_mem)
1620 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621
1622 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1623 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001624 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625 lu_ci = &lu_gp->lu_gp_group.cg_item;
1626 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1627 config_item_name(lu_ci), lu_gp->lu_gp_id);
1628 }
1629 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1630
1631 return len;
1632}
1633
1634static ssize_t target_core_store_alua_lu_gp(
1635 void *p,
1636 const char *page,
1637 size_t count)
1638{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001639 struct se_device *dev = p;
1640 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001641 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1642 struct t10_alua_lu_gp_member *lu_gp_mem;
1643 unsigned char buf[LU_GROUP_NAME_BUF];
1644 int move = 0;
1645
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001646 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1647 if (!lu_gp_mem)
1648 return 0;
1649
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001650 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001651 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 return -EINVAL;
1653 }
1654 memset(buf, 0, LU_GROUP_NAME_BUF);
1655 memcpy(buf, page, count);
1656 /*
1657 * Any ALUA logical unit alias besides "NULL" means we will be
1658 * making a new group association.
1659 */
1660 if (strcmp(strstrip(buf), "NULL")) {
1661 /*
1662 * core_alua_get_lu_gp_by_name() will increment reference to
1663 * struct t10_alua_lu_gp. This reference is released with
1664 * core_alua_get_lu_gp_by_name below().
1665 */
1666 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001667 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 return -ENODEV;
1669 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670
1671 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1672 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001673 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674 /*
1675 * Clearing an existing lu_gp association, and replacing
1676 * with NULL
1677 */
Andy Grover6708bb22011-06-08 10:36:43 -07001678 if (!lu_gp_new) {
1679 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001680 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1681 " %hu\n",
1682 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001683 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001684 config_item_name(&lu_gp->lu_gp_group.cg_item),
1685 lu_gp->lu_gp_id);
1686
1687 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1688 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1689
1690 return count;
1691 }
1692 /*
1693 * Removing existing association of lu_gp_mem with lu_gp
1694 */
1695 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1696 move = 1;
1697 }
1698 /*
1699 * Associate lu_gp_mem with lu_gp_new.
1700 */
1701 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1702 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1703
Andy Grover6708bb22011-06-08 10:36:43 -07001704 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 " core/alua/lu_gps/%s, ID: %hu\n",
1706 (move) ? "Moving" : "Adding",
1707 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001708 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1710 lu_gp_new->lu_gp_id);
1711
1712 core_alua_put_lu_gp_from_name(lu_gp_new);
1713 return count;
1714}
1715
1716static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1717 .attr = { .ca_owner = THIS_MODULE,
1718 .ca_name = "alua_lu_gp",
1719 .ca_mode = S_IRUGO | S_IWUSR },
1720 .show = target_core_show_alua_lu_gp,
1721 .store = target_core_store_alua_lu_gp,
1722};
1723
1724static struct configfs_attribute *lio_core_dev_attrs[] = {
1725 &target_core_attr_dev_info.attr,
1726 &target_core_attr_dev_control.attr,
1727 &target_core_attr_dev_alias.attr,
1728 &target_core_attr_dev_udev_path.attr,
1729 &target_core_attr_dev_enable.attr,
1730 &target_core_attr_dev_alua_lu_gp.attr,
1731 NULL,
1732};
1733
1734static void target_core_dev_release(struct config_item *item)
1735{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001736 struct config_group *dev_cg = to_config_group(item);
1737 struct se_device *dev =
1738 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001740 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001741 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742}
1743
1744static ssize_t target_core_dev_show(struct config_item *item,
1745 struct configfs_attribute *attr,
1746 char *page)
1747{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001748 struct config_group *dev_cg = to_config_group(item);
1749 struct se_device *dev =
1750 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751 struct target_core_configfs_attribute *tc_attr = container_of(
1752 attr, struct target_core_configfs_attribute, attr);
1753
Andy Grover6708bb22011-06-08 10:36:43 -07001754 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755 return -EINVAL;
1756
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001757 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758}
1759
1760static ssize_t target_core_dev_store(struct config_item *item,
1761 struct configfs_attribute *attr,
1762 const char *page, size_t count)
1763{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001764 struct config_group *dev_cg = to_config_group(item);
1765 struct se_device *dev =
1766 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001767 struct target_core_configfs_attribute *tc_attr = container_of(
1768 attr, struct target_core_configfs_attribute, attr);
1769
Andy Grover6708bb22011-06-08 10:36:43 -07001770 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001771 return -EINVAL;
1772
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001773 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774}
1775
1776static struct configfs_item_operations target_core_dev_item_ops = {
1777 .release = target_core_dev_release,
1778 .show_attribute = target_core_dev_show,
1779 .store_attribute = target_core_dev_store,
1780};
1781
1782static struct config_item_type target_core_dev_cit = {
1783 .ct_item_ops = &target_core_dev_item_ops,
1784 .ct_attrs = lio_core_dev_attrs,
1785 .ct_owner = THIS_MODULE,
1786};
1787
1788/* End functions for struct config_item_type target_core_dev_cit */
1789
1790/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1791
1792CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1793#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1794static struct target_core_alua_lu_gp_attribute \
1795 target_core_alua_lu_gp_##_name = \
1796 __CONFIGFS_EATTR(_name, _mode, \
1797 target_core_alua_lu_gp_show_attr_##_name, \
1798 target_core_alua_lu_gp_store_attr_##_name);
1799
1800#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1801static struct target_core_alua_lu_gp_attribute \
1802 target_core_alua_lu_gp_##_name = \
1803 __CONFIGFS_EATTR_RO(_name, \
1804 target_core_alua_lu_gp_show_attr_##_name);
1805
1806/*
1807 * lu_gp_id
1808 */
1809static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1810 struct t10_alua_lu_gp *lu_gp,
1811 char *page)
1812{
Andy Grover6708bb22011-06-08 10:36:43 -07001813 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001814 return 0;
1815
1816 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1817}
1818
1819static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1820 struct t10_alua_lu_gp *lu_gp,
1821 const char *page,
1822 size_t count)
1823{
1824 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1825 unsigned long lu_gp_id;
1826 int ret;
1827
1828 ret = strict_strtoul(page, 0, &lu_gp_id);
1829 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001830 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 " lu_gp_id\n", ret);
1832 return -EINVAL;
1833 }
1834 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001835 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 " 0x0000ffff\n", lu_gp_id);
1837 return -EINVAL;
1838 }
1839
1840 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1841 if (ret < 0)
1842 return -EINVAL;
1843
Andy Grover6708bb22011-06-08 10:36:43 -07001844 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1846 config_item_name(&alua_lu_gp_cg->cg_item),
1847 lu_gp->lu_gp_id);
1848
1849 return count;
1850}
1851
1852SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1853
1854/*
1855 * members
1856 */
1857static ssize_t target_core_alua_lu_gp_show_attr_members(
1858 struct t10_alua_lu_gp *lu_gp,
1859 char *page)
1860{
1861 struct se_device *dev;
1862 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001863 struct t10_alua_lu_gp_member *lu_gp_mem;
1864 ssize_t len = 0, cur_len;
1865 unsigned char buf[LU_GROUP_NAME_BUF];
1866
1867 memset(buf, 0, LU_GROUP_NAME_BUF);
1868
1869 spin_lock(&lu_gp->lu_gp_lock);
1870 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1871 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001872 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873
1874 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1875 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001876 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877 cur_len++; /* Extra byte for NULL terminator */
1878
1879 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001880 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001881 "_members buffer\n");
1882 break;
1883 }
1884 memcpy(page+len, buf, cur_len);
1885 len += cur_len;
1886 }
1887 spin_unlock(&lu_gp->lu_gp_lock);
1888
1889 return len;
1890}
1891
1892SE_DEV_ALUA_LU_ATTR_RO(members);
1893
1894CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1895
1896static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1897 &target_core_alua_lu_gp_lu_gp_id.attr,
1898 &target_core_alua_lu_gp_members.attr,
1899 NULL,
1900};
1901
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001902static void target_core_alua_lu_gp_release(struct config_item *item)
1903{
1904 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1905 struct t10_alua_lu_gp, lu_gp_group);
1906
1907 core_alua_free_lu_gp(lu_gp);
1908}
1909
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001910static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001911 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 .show_attribute = target_core_alua_lu_gp_attr_show,
1913 .store_attribute = target_core_alua_lu_gp_attr_store,
1914};
1915
1916static struct config_item_type target_core_alua_lu_gp_cit = {
1917 .ct_item_ops = &target_core_alua_lu_gp_ops,
1918 .ct_attrs = target_core_alua_lu_gp_attrs,
1919 .ct_owner = THIS_MODULE,
1920};
1921
1922/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1923
1924/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1925
1926static struct config_group *target_core_alua_create_lu_gp(
1927 struct config_group *group,
1928 const char *name)
1929{
1930 struct t10_alua_lu_gp *lu_gp;
1931 struct config_group *alua_lu_gp_cg = NULL;
1932 struct config_item *alua_lu_gp_ci = NULL;
1933
1934 lu_gp = core_alua_allocate_lu_gp(name, 0);
1935 if (IS_ERR(lu_gp))
1936 return NULL;
1937
1938 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1939 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1940
1941 config_group_init_type_name(alua_lu_gp_cg, name,
1942 &target_core_alua_lu_gp_cit);
1943
Andy Grover6708bb22011-06-08 10:36:43 -07001944 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945 " Group: core/alua/lu_gps/%s\n",
1946 config_item_name(alua_lu_gp_ci));
1947
1948 return alua_lu_gp_cg;
1949
1950}
1951
1952static void target_core_alua_drop_lu_gp(
1953 struct config_group *group,
1954 struct config_item *item)
1955{
1956 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1957 struct t10_alua_lu_gp, lu_gp_group);
1958
Andy Grover6708bb22011-06-08 10:36:43 -07001959 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1961 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001962 /*
1963 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1964 * -> target_core_alua_lu_gp_release()
1965 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967}
1968
1969static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1970 .make_group = &target_core_alua_create_lu_gp,
1971 .drop_item = &target_core_alua_drop_lu_gp,
1972};
1973
1974static struct config_item_type target_core_alua_lu_gps_cit = {
1975 .ct_item_ops = NULL,
1976 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1977 .ct_owner = THIS_MODULE,
1978};
1979
1980/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1981
1982/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1983
1984CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1985#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1986static struct target_core_alua_tg_pt_gp_attribute \
1987 target_core_alua_tg_pt_gp_##_name = \
1988 __CONFIGFS_EATTR(_name, _mode, \
1989 target_core_alua_tg_pt_gp_show_attr_##_name, \
1990 target_core_alua_tg_pt_gp_store_attr_##_name);
1991
1992#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1993static struct target_core_alua_tg_pt_gp_attribute \
1994 target_core_alua_tg_pt_gp_##_name = \
1995 __CONFIGFS_EATTR_RO(_name, \
1996 target_core_alua_tg_pt_gp_show_attr_##_name);
1997
1998/*
1999 * alua_access_state
2000 */
2001static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2002 struct t10_alua_tg_pt_gp *tg_pt_gp,
2003 char *page)
2004{
2005 return sprintf(page, "%d\n",
2006 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2007}
2008
2009static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2010 struct t10_alua_tg_pt_gp *tg_pt_gp,
2011 const char *page,
2012 size_t count)
2013{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002014 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002015 unsigned long tmp;
2016 int new_state, ret;
2017
Andy Grover6708bb22011-06-08 10:36:43 -07002018 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2019 pr_err("Unable to do implict ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2021 return -EINVAL;
2022 }
2023
2024 ret = strict_strtoul(page, 0, &tmp);
2025 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002026 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002027 " %s\n", page);
2028 return -EINVAL;
2029 }
2030 new_state = (int)tmp;
2031
2032 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002033 pr_err("Unable to process implict configfs ALUA"
Masanari Iida5e58b022012-02-27 23:18:15 +09002034 " transition while TPGS_IMPLICT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 return -EINVAL;
2036 }
2037
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002038 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 NULL, NULL, new_state, 0);
2040 return (!ret) ? count : -EINVAL;
2041}
2042
2043SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2044
2045/*
2046 * alua_access_status
2047 */
2048static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2049 struct t10_alua_tg_pt_gp *tg_pt_gp,
2050 char *page)
2051{
2052 return sprintf(page, "%s\n",
2053 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2054}
2055
2056static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2057 struct t10_alua_tg_pt_gp *tg_pt_gp,
2058 const char *page,
2059 size_t count)
2060{
2061 unsigned long tmp;
2062 int new_status, ret;
2063
Andy Grover6708bb22011-06-08 10:36:43 -07002064 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2065 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066 " valid tg_pt_gp ID: %hu\n",
2067 tg_pt_gp->tg_pt_gp_valid_id);
2068 return -EINVAL;
2069 }
2070
2071 ret = strict_strtoul(page, 0, &tmp);
2072 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002073 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074 " from %s\n", page);
2075 return -EINVAL;
2076 }
2077 new_status = (int)tmp;
2078
2079 if ((new_status != ALUA_STATUS_NONE) &&
2080 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2081 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002082 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083 new_status);
2084 return -EINVAL;
2085 }
2086
2087 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2088 return count;
2089}
2090
2091SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2092
2093/*
2094 * alua_access_type
2095 */
2096static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2097 struct t10_alua_tg_pt_gp *tg_pt_gp,
2098 char *page)
2099{
2100 return core_alua_show_access_type(tg_pt_gp, page);
2101}
2102
2103static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2104 struct t10_alua_tg_pt_gp *tg_pt_gp,
2105 const char *page,
2106 size_t count)
2107{
2108 return core_alua_store_access_type(tg_pt_gp, page, count);
2109}
2110
2111SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2112
2113/*
2114 * alua_write_metadata
2115 */
2116static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2117 struct t10_alua_tg_pt_gp *tg_pt_gp,
2118 char *page)
2119{
2120 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2121}
2122
2123static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2124 struct t10_alua_tg_pt_gp *tg_pt_gp,
2125 const char *page,
2126 size_t count)
2127{
2128 unsigned long tmp;
2129 int ret;
2130
2131 ret = strict_strtoul(page, 0, &tmp);
2132 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002133 pr_err("Unable to extract alua_write_metadata\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134 return -EINVAL;
2135 }
2136
2137 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002138 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002139 " %lu\n", tmp);
2140 return -EINVAL;
2141 }
2142 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2143
2144 return count;
2145}
2146
2147SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2148
2149
2150
2151/*
2152 * nonop_delay_msecs
2153 */
2154static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2155 struct t10_alua_tg_pt_gp *tg_pt_gp,
2156 char *page)
2157{
2158 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2159
2160}
2161
2162static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2163 struct t10_alua_tg_pt_gp *tg_pt_gp,
2164 const char *page,
2165 size_t count)
2166{
2167 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2168}
2169
2170SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2171
2172/*
2173 * trans_delay_msecs
2174 */
2175static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2176 struct t10_alua_tg_pt_gp *tg_pt_gp,
2177 char *page)
2178{
2179 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2180}
2181
2182static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2183 struct t10_alua_tg_pt_gp *tg_pt_gp,
2184 const char *page,
2185 size_t count)
2186{
2187 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2188}
2189
2190SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2191
2192/*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002193 * implict_trans_secs
2194 */
2195static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
2196 struct t10_alua_tg_pt_gp *tg_pt_gp,
2197 char *page)
2198{
2199 return core_alua_show_implict_trans_secs(tg_pt_gp, page);
2200}
2201
2202static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
2203 struct t10_alua_tg_pt_gp *tg_pt_gp,
2204 const char *page,
2205 size_t count)
2206{
2207 return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
2208}
2209
2210SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
2211
2212/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002213 * preferred
2214 */
2215
2216static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2217 struct t10_alua_tg_pt_gp *tg_pt_gp,
2218 char *page)
2219{
2220 return core_alua_show_preferred_bit(tg_pt_gp, page);
2221}
2222
2223static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2224 struct t10_alua_tg_pt_gp *tg_pt_gp,
2225 const char *page,
2226 size_t count)
2227{
2228 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2229}
2230
2231SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2232
2233/*
2234 * tg_pt_gp_id
2235 */
2236static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2237 struct t10_alua_tg_pt_gp *tg_pt_gp,
2238 char *page)
2239{
Andy Grover6708bb22011-06-08 10:36:43 -07002240 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 return 0;
2242
2243 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2244}
2245
2246static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2247 struct t10_alua_tg_pt_gp *tg_pt_gp,
2248 const char *page,
2249 size_t count)
2250{
2251 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2252 unsigned long tg_pt_gp_id;
2253 int ret;
2254
2255 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2256 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002257 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002258 " tg_pt_gp_id\n", ret);
2259 return -EINVAL;
2260 }
2261 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002262 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 " 0x0000ffff\n", tg_pt_gp_id);
2264 return -EINVAL;
2265 }
2266
2267 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2268 if (ret < 0)
2269 return -EINVAL;
2270
Andy Grover6708bb22011-06-08 10:36:43 -07002271 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002272 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2273 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2274 tg_pt_gp->tg_pt_gp_id);
2275
2276 return count;
2277}
2278
2279SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2280
2281/*
2282 * members
2283 */
2284static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2285 struct t10_alua_tg_pt_gp *tg_pt_gp,
2286 char *page)
2287{
2288 struct se_port *port;
2289 struct se_portal_group *tpg;
2290 struct se_lun *lun;
2291 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2292 ssize_t len = 0, cur_len;
2293 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2294
2295 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2296
2297 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2298 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2299 tg_pt_gp_mem_list) {
2300 port = tg_pt_gp_mem->tg_pt;
2301 tpg = port->sep_tpg;
2302 lun = port->sep_lun;
2303
2304 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002305 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2306 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2307 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002308 config_item_name(&lun->lun_group.cg_item));
2309 cur_len++; /* Extra byte for NULL terminator */
2310
2311 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002312 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002313 "_members buffer\n");
2314 break;
2315 }
2316 memcpy(page+len, buf, cur_len);
2317 len += cur_len;
2318 }
2319 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2320
2321 return len;
2322}
2323
2324SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2325
2326CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2327 tg_pt_gp_group);
2328
2329static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2330 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2331 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2332 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2333 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2334 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2335 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002336 &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002337 &target_core_alua_tg_pt_gp_preferred.attr,
2338 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2339 &target_core_alua_tg_pt_gp_members.attr,
2340 NULL,
2341};
2342
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002343static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2344{
2345 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2346 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2347
2348 core_alua_free_tg_pt_gp(tg_pt_gp);
2349}
2350
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002351static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002352 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2354 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2355};
2356
2357static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2358 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2359 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2360 .ct_owner = THIS_MODULE,
2361};
2362
2363/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2364
2365/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2366
2367static struct config_group *target_core_alua_create_tg_pt_gp(
2368 struct config_group *group,
2369 const char *name)
2370{
2371 struct t10_alua *alua = container_of(group, struct t10_alua,
2372 alua_tg_pt_gps_group);
2373 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002374 struct config_group *alua_tg_pt_gp_cg = NULL;
2375 struct config_item *alua_tg_pt_gp_ci = NULL;
2376
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002377 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002378 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 return NULL;
2380
2381 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2382 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2383
2384 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2385 &target_core_alua_tg_pt_gp_cit);
2386
Andy Grover6708bb22011-06-08 10:36:43 -07002387 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388 " Group: alua/tg_pt_gps/%s\n",
2389 config_item_name(alua_tg_pt_gp_ci));
2390
2391 return alua_tg_pt_gp_cg;
2392}
2393
2394static void target_core_alua_drop_tg_pt_gp(
2395 struct config_group *group,
2396 struct config_item *item)
2397{
2398 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2399 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2400
Andy Grover6708bb22011-06-08 10:36:43 -07002401 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002402 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2403 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002404 /*
2405 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2406 * -> target_core_alua_tg_pt_gp_release().
2407 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409}
2410
2411static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2412 .make_group = &target_core_alua_create_tg_pt_gp,
2413 .drop_item = &target_core_alua_drop_tg_pt_gp,
2414};
2415
2416static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2417 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2418 .ct_owner = THIS_MODULE,
2419};
2420
2421/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2422
2423/* Start functions for struct config_item_type target_core_alua_cit */
2424
2425/*
2426 * target_core_alua_cit is a ConfigFS group that lives under
2427 * /sys/kernel/config/target/core/alua. There are default groups
2428 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2429 * target_core_alua_cit in target_core_init_configfs() below.
2430 */
2431static struct config_item_type target_core_alua_cit = {
2432 .ct_item_ops = NULL,
2433 .ct_attrs = NULL,
2434 .ct_owner = THIS_MODULE,
2435};
2436
2437/* End functions for struct config_item_type target_core_alua_cit */
2438
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002439/* Start functions for struct config_item_type target_core_stat_cit */
2440
2441static struct config_group *target_core_stat_mkdir(
2442 struct config_group *group,
2443 const char *name)
2444{
2445 return ERR_PTR(-ENOSYS);
2446}
2447
2448static void target_core_stat_rmdir(
2449 struct config_group *group,
2450 struct config_item *item)
2451{
2452 return;
2453}
2454
2455static struct configfs_group_operations target_core_stat_group_ops = {
2456 .make_group = &target_core_stat_mkdir,
2457 .drop_item = &target_core_stat_rmdir,
2458};
2459
2460static struct config_item_type target_core_stat_cit = {
2461 .ct_group_ops = &target_core_stat_group_ops,
2462 .ct_owner = THIS_MODULE,
2463};
2464
2465/* End functions for struct config_item_type target_core_stat_cit */
2466
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002467/* Start functions for struct config_item_type target_core_hba_cit */
2468
2469static struct config_group *target_core_make_subdev(
2470 struct config_group *group,
2471 const char *name)
2472{
2473 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474 struct se_subsystem_api *t;
2475 struct config_item *hba_ci = &group->cg_item;
2476 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002477 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002478 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002479 struct config_group *dev_stat_grp = NULL;
2480 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002481
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002482 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2483 if (ret)
2484 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002485 /*
2486 * Locate the struct se_subsystem_api from parent's struct se_hba.
2487 */
2488 t = hba->transport;
2489
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002490 dev = target_alloc_device(hba, name);
2491 if (!dev)
2492 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002493
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002494 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002496 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002497 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002498 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002499 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002501 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2502 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002504 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002505 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002506 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002507 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002508 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002509 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002510 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002511 "statistics", &target_core_stat_cit);
2512
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002513 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2514 dev_cg->default_groups[1] = &dev->dev_pr_group;
2515 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2516 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2517 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002518 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002519 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002520 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002521 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002522 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002523 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002524 goto out_free_dev_cg_default_groups;
2525 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002526
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002527 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2529 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002530 if (!tg_pt_gp_cg->default_groups) {
2531 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002533 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534 }
2535
2536 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2537 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2538 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2539 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002540 /*
2541 * Add core/$HBA/$DEV/statistics/ default groups
2542 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002543 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002544 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2545 GFP_KERNEL);
2546 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002547 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002548 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002549 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002550 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551
2552 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002553 return dev_cg;
2554
2555out_free_tg_pt_gp_cg_default_groups:
2556 kfree(tg_pt_gp_cg->default_groups);
2557out_free_tg_pt_gp:
2558 core_alua_free_tg_pt_gp(tg_pt_gp);
2559out_free_dev_cg_default_groups:
2560 kfree(dev_cg->default_groups);
2561out_free_device:
2562 target_free_device(dev);
2563out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002565 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566}
2567
2568static void target_core_drop_subdev(
2569 struct config_group *group,
2570 struct config_item *item)
2571{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002572 struct config_group *dev_cg = to_config_group(item);
2573 struct se_device *dev =
2574 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002577 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002578 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002580 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002582 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002584 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002585 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2586 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2587 dev_stat_grp->default_groups[i] = NULL;
2588 config_item_put(df_item);
2589 }
2590 kfree(dev_stat_grp->default_groups);
2591
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002592 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002593 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2594 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2595 tg_pt_gp_cg->default_groups[i] = NULL;
2596 config_item_put(df_item);
2597 }
2598 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002599 /*
2600 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2601 * directly from target_core_alua_tg_pt_gp_release().
2602 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002603 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002604
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 for (i = 0; dev_cg->default_groups[i]; i++) {
2606 df_item = &dev_cg->default_groups[i]->cg_item;
2607 dev_cg->default_groups[i] = NULL;
2608 config_item_put(df_item);
2609 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002611 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002613 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002614 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615}
2616
2617static struct configfs_group_operations target_core_hba_group_ops = {
2618 .make_group = target_core_make_subdev,
2619 .drop_item = target_core_drop_subdev,
2620};
2621
2622CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2623#define SE_HBA_ATTR(_name, _mode) \
2624static struct target_core_hba_attribute \
2625 target_core_hba_##_name = \
2626 __CONFIGFS_EATTR(_name, _mode, \
2627 target_core_hba_show_attr_##_name, \
2628 target_core_hba_store_attr_##_name);
2629
2630#define SE_HBA_ATTR_RO(_name) \
2631static struct target_core_hba_attribute \
2632 target_core_hba_##_name = \
2633 __CONFIGFS_EATTR_RO(_name, \
2634 target_core_hba_show_attr_##_name);
2635
2636static ssize_t target_core_hba_show_attr_hba_info(
2637 struct se_hba *hba,
2638 char *page)
2639{
2640 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2641 hba->hba_id, hba->transport->name,
2642 TARGET_CORE_CONFIGFS_VERSION);
2643}
2644
2645SE_HBA_ATTR_RO(hba_info);
2646
2647static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2648 char *page)
2649{
2650 int hba_mode = 0;
2651
2652 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2653 hba_mode = 1;
2654
2655 return sprintf(page, "%d\n", hba_mode);
2656}
2657
2658static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2659 const char *page, size_t count)
2660{
2661 struct se_subsystem_api *transport = hba->transport;
2662 unsigned long mode_flag;
2663 int ret;
2664
2665 if (transport->pmode_enable_hba == NULL)
2666 return -EINVAL;
2667
2668 ret = strict_strtoul(page, 0, &mode_flag);
2669 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002670 pr_err("Unable to extract hba mode flag: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002671 return -EINVAL;
2672 }
2673
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002674 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002675 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 return -EINVAL;
2677 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678
2679 ret = transport->pmode_enable_hba(hba, mode_flag);
2680 if (ret < 0)
2681 return -EINVAL;
2682 if (ret > 0)
2683 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2684 else if (ret == 0)
2685 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2686
2687 return count;
2688}
2689
2690SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2691
2692CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2693
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002694static void target_core_hba_release(struct config_item *item)
2695{
2696 struct se_hba *hba = container_of(to_config_group(item),
2697 struct se_hba, hba_group);
2698 core_delete_hba(hba);
2699}
2700
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002701static struct configfs_attribute *target_core_hba_attrs[] = {
2702 &target_core_hba_hba_info.attr,
2703 &target_core_hba_hba_mode.attr,
2704 NULL,
2705};
2706
2707static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002708 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 .show_attribute = target_core_hba_attr_show,
2710 .store_attribute = target_core_hba_attr_store,
2711};
2712
2713static struct config_item_type target_core_hba_cit = {
2714 .ct_item_ops = &target_core_hba_item_ops,
2715 .ct_group_ops = &target_core_hba_group_ops,
2716 .ct_attrs = target_core_hba_attrs,
2717 .ct_owner = THIS_MODULE,
2718};
2719
2720static struct config_group *target_core_call_addhbatotarget(
2721 struct config_group *group,
2722 const char *name)
2723{
2724 char *se_plugin_str, *str, *str2;
2725 struct se_hba *hba;
2726 char buf[TARGET_CORE_NAME_MAX_LEN];
2727 unsigned long plugin_dep_id = 0;
2728 int ret;
2729
2730 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07002731 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07002732 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2734 TARGET_CORE_NAME_MAX_LEN);
2735 return ERR_PTR(-ENAMETOOLONG);
2736 }
2737 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2738
2739 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002740 if (!str) {
2741 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002742 return ERR_PTR(-EINVAL);
2743 }
2744 se_plugin_str = buf;
2745 /*
2746 * Special case for subsystem plugins that have "_" in their names.
2747 * Namely rd_direct and rd_mcp..
2748 */
2749 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002750 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 *str2 = '\0'; /* Terminate for *se_plugin_str */
2752 str2++; /* Skip to start of plugin dependent ID */
2753 str = str2;
2754 } else {
2755 *str = '\0'; /* Terminate for *se_plugin_str */
2756 str++; /* Skip to start of plugin dependent ID */
2757 }
2758
2759 ret = strict_strtoul(str, 0, &plugin_dep_id);
2760 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002761 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762 " plugin_dep_id\n", ret);
2763 return ERR_PTR(-EINVAL);
2764 }
2765 /*
2766 * Load up TCM subsystem plugins if they have not already been loaded.
2767 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07002768 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769
2770 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2771 if (IS_ERR(hba))
2772 return ERR_CAST(hba);
2773
2774 config_group_init_type_name(&hba->hba_group, name,
2775 &target_core_hba_cit);
2776
2777 return &hba->hba_group;
2778}
2779
2780static void target_core_call_delhbafromtarget(
2781 struct config_group *group,
2782 struct config_item *item)
2783{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002784 /*
2785 * core_delete_hba() is called from target_core_hba_item_ops->release()
2786 * -> target_core_hba_release()
2787 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789}
2790
2791static struct configfs_group_operations target_core_group_ops = {
2792 .make_group = target_core_call_addhbatotarget,
2793 .drop_item = target_core_call_delhbafromtarget,
2794};
2795
2796static struct config_item_type target_core_cit = {
2797 .ct_item_ops = NULL,
2798 .ct_group_ops = &target_core_group_ops,
2799 .ct_attrs = NULL,
2800 .ct_owner = THIS_MODULE,
2801};
2802
2803/* Stop functions for struct config_item_type target_core_hba_cit */
2804
Axel Lin54550fa2011-03-14 04:06:09 -07002805static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806{
2807 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2808 struct config_group *lu_gp_cg = NULL;
2809 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810 struct t10_alua_lu_gp *lu_gp;
2811 int ret;
2812
Andy Grover6708bb22011-06-08 10:36:43 -07002813 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2815 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2816
2817 subsys = target_core_subsystem[0];
2818 config_group_init(&subsys->su_group);
2819 mutex_init(&subsys->su_mutex);
2820
Andy Grovere3d6f902011-07-19 08:55:10 +00002821 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002823 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 /*
2825 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2826 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2827 */
2828 target_cg = &subsys->su_group;
2829 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2830 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002831 if (!target_cg->default_groups) {
2832 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002833 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002834 goto out_global;
2835 }
2836
Andy Grovere3d6f902011-07-19 08:55:10 +00002837 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002839 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 target_cg->default_groups[1] = NULL;
2841 /*
2842 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2843 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002844 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002845 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2846 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002847 if (!hba_cg->default_groups) {
2848 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002849 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002850 goto out_global;
2851 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002852 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002854 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 hba_cg->default_groups[1] = NULL;
2856 /*
2857 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2858 * groups under /sys/kernel/config/target/core/alua/
2859 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002860 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2862 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002863 if (!alua_cg->default_groups) {
2864 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002865 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866 goto out_global;
2867 }
2868
Andy Grovere3d6f902011-07-19 08:55:10 +00002869 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002871 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872 alua_cg->default_groups[1] = NULL;
2873 /*
2874 * Add core/alua/lu_gps/default_lu_gp
2875 */
2876 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002877 if (IS_ERR(lu_gp)) {
2878 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002880 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881
Andy Grovere3d6f902011-07-19 08:55:10 +00002882 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002883 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2884 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002885 if (!lu_gp_cg->default_groups) {
2886 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002887 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002888 goto out_global;
2889 }
2890
2891 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2892 &target_core_alua_lu_gp_cit);
2893 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2894 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002895 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002896 /*
2897 * Register the target_core_mod subsystem with configfs.
2898 */
2899 ret = configfs_register_subsystem(subsys);
2900 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002901 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 ret, subsys->su_group.cg_item.ci_namebuf);
2903 goto out_global;
2904 }
Andy Grover6708bb22011-06-08 10:36:43 -07002905 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2907 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2908 /*
2909 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2910 */
2911 ret = rd_module_init();
2912 if (ret < 0)
2913 goto out;
2914
Roland Dreier0d0f9df2012-10-31 09:16:44 -07002915 ret = core_dev_setup_virtual_lun0();
2916 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917 goto out;
2918
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 return 0;
2920
2921out:
2922 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 core_dev_release_virtual_lun0();
2924 rd_module_exit();
2925out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00002926 if (default_lu_gp) {
2927 core_alua_free_lu_gp(default_lu_gp);
2928 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 }
2930 if (lu_gp_cg)
2931 kfree(lu_gp_cg->default_groups);
2932 if (alua_cg)
2933 kfree(alua_cg->default_groups);
2934 if (hba_cg)
2935 kfree(hba_cg->default_groups);
2936 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00002937 release_se_kmem_caches();
2938 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002939}
2940
Axel Lin54550fa2011-03-14 04:06:09 -07002941static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002942{
2943 struct configfs_subsystem *subsys;
2944 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2945 struct config_item *item;
2946 int i;
2947
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948 subsys = target_core_subsystem[0];
2949
Andy Grovere3d6f902011-07-19 08:55:10 +00002950 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2952 item = &lu_gp_cg->default_groups[i]->cg_item;
2953 lu_gp_cg->default_groups[i] = NULL;
2954 config_item_put(item);
2955 }
2956 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002957 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958
Andy Grovere3d6f902011-07-19 08:55:10 +00002959 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 for (i = 0; alua_cg->default_groups[i]; i++) {
2961 item = &alua_cg->default_groups[i]->cg_item;
2962 alua_cg->default_groups[i] = NULL;
2963 config_item_put(item);
2964 }
2965 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002966 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967
Andy Grovere3d6f902011-07-19 08:55:10 +00002968 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002969 for (i = 0; hba_cg->default_groups[i]; i++) {
2970 item = &hba_cg->default_groups[i]->cg_item;
2971 hba_cg->default_groups[i] = NULL;
2972 config_item_put(item);
2973 }
2974 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002975 hba_cg->default_groups = NULL;
2976 /*
2977 * We expect subsys->su_group.default_groups to be released
2978 * by configfs subsystem provider logic..
2979 */
2980 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 kfree(subsys->su_group.default_groups);
2982
Andy Grovere3d6f902011-07-19 08:55:10 +00002983 core_alua_free_lu_gp(default_lu_gp);
2984 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002985
Andy Grover6708bb22011-06-08 10:36:43 -07002986 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002987 " Infrastructure\n");
2988
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002989 core_dev_release_virtual_lun0();
2990 rd_module_exit();
Andy Grovere3d6f902011-07-19 08:55:10 +00002991 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992}
2993
2994MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
2995MODULE_AUTHOR("nab@Linux-iSCSI.org");
2996MODULE_LICENSE("GPL");
2997
2998module_init(target_core_init_configfs);
2999module_exit(target_core_exit_configfs);