Blame view

drivers/target/target_core_fabric_configfs.c 34.4 KB
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1
2
3
4
5
6
  /*******************************************************************************
  * Filename: target_core_fabric_configfs.c
   *
   * This file contains generic fabric module configfs infrastructure for
   * TCM v4.x code
   *
12d233842   Nicholas Bellinger   [SCSI] target: ad...
7
8
   * Copyright (c) 2010,2011 Rising Tide Systems
   * Copyright (c) 2010,2011 Linux-iSCSI.org
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
9
   *
12d233842   Nicholas Bellinger   [SCSI] target: ad...
10
   * Copyright (c) Nicholas A. Bellinger <nab@linux-iscsi.org>
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   ****************************************************************************/
  
  #include <linux/module.h>
  #include <linux/moduleparam.h>
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include <generated/utsrelease.h>
  #include <linux/utsname.h>
  #include <linux/init.h>
  #include <linux/fs.h>
  #include <linux/namei.h>
  #include <linux/slab.h>
  #include <linux/types.h>
  #include <linux/delay.h>
  #include <linux/unistd.h>
  #include <linux/string.h>
  #include <linux/syscalls.h>
  #include <linux/configfs.h>
  
  #include <target/target_core_base.h>
  #include <target/target_core_device.h>
  #include <target/target_core_tpg.h>
  #include <target/target_core_transport.h>
  #include <target/target_core_fabric_ops.h>
  #include <target/target_core_fabric_configfs.h>
  #include <target/target_core_configfs.h>
  #include <target/configfs_macros.h>
  
  #include "target_core_alua.h"
  #include "target_core_hba.h"
  #include "target_core_pr.h"
12d233842   Nicholas Bellinger   [SCSI] target: ad...
50
  #include "target_core_stat.h"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
51
52
53
54
55
56
57
58
59
60
61
  
  #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
  static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  {									\
  	struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl;	\
  	struct config_item_type *cit = &tfc->tfc_##_name##_cit;		\
  									\
  	cit->ct_item_ops = _item_ops;					\
  	cit->ct_group_ops = _group_ops;					\
  	cit->ct_attrs = _attrs;						\
  	cit->ct_owner = tf->tf_module;					\
6708bb27b   Andy Grover   target: Follow up...
62
63
  	pr_debug("Setup generic %s
  ", __stringify(_name));		\
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  }
  
  /* Start of tfc_tpg_mappedlun_cit */
  
  static int target_fabric_mappedlun_link(
  	struct config_item *lun_acl_ci,
  	struct config_item *lun_ci)
  {
  	struct se_dev_entry *deve;
  	struct se_lun *lun = container_of(to_config_group(lun_ci),
  			struct se_lun, lun_group);
  	struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  			struct se_lun_acl, se_lun_group);
  	struct se_portal_group *se_tpg;
  	struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  	int ret = 0, lun_access;
  	/*
  	 * Ensure that the source port exists
  	 */
6708bb27b   Andy Grover   target: Follow up...
83
84
  	if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
  		pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  				"_tpg does not exist
  ");
  		return -EINVAL;
  	}
  	se_tpg = lun->lun_sep->sep_tpg;
  
  	nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  	tpg_ci = &nacl_ci->ci_group->cg_item;
  	wwn_ci = &tpg_ci->ci_group->cg_item;
  	tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  	wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  	/*
  	 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  	 */
  	if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
6708bb27b   Andy Grover   target: Follow up...
100
101
  		pr_err("Illegal Initiator ACL SymLink outside of %s
  ",
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
102
103
104
105
  			config_item_name(wwn_ci));
  		return -EINVAL;
  	}
  	if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
6708bb27b   Andy Grover   target: Follow up...
106
  		pr_err("Illegal Initiator ACL Symlink outside of %s"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  			" TPGT: %s
  ", config_item_name(wwn_ci),
  			config_item_name(tpg_ci));
  		return -EINVAL;
  	}
  	/*
  	 * If this struct se_node_acl was dynamically generated with
  	 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
  	 * which be will write protected (READ-ONLY) when
  	 * tpg_1/attrib/demo_mode_write_protect=1
  	 */
  	spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
  	deve = &lacl->se_lun_nacl->device_list[lacl->mapped_lun];
  	if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
  		lun_access = deve->lun_flags;
  	else
  		lun_access =
e3d6f909e   Andy Grover   target: Core clea...
124
  			(se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  				se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
  					   TRANSPORT_LUNFLAGS_READ_WRITE;
  	spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
  	/*
  	 * Determine the actual mapped LUN value user wants..
  	 *
  	 * This value is what the SCSI Initiator actually sees the
  	 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  	 */
  	ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
  			lun->unpacked_lun, lun_access);
  
  	return (ret < 0) ? -EINVAL : 0;
  }
  
  static int target_fabric_mappedlun_unlink(
  	struct config_item *lun_acl_ci,
  	struct config_item *lun_ci)
  {
  	struct se_lun *lun;
  	struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  			struct se_lun_acl, se_lun_group);
  	struct se_node_acl *nacl = lacl->se_lun_nacl;
  	struct se_dev_entry *deve = &nacl->device_list[lacl->mapped_lun];
  	struct se_portal_group *se_tpg;
  	/*
  	 * Determine if the underlying MappedLUN has already been released..
  	 */
6708bb27b   Andy Grover   target: Follow up...
153
  	if (!deve->se_lun)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  		return 0;
  
  	lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
  	se_tpg = lun->lun_sep->sep_tpg;
  
  	core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
  	return 0;
  }
  
  CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
  #define TCM_MAPPEDLUN_ATTR(_name, _mode)				\
  static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
  	__CONFIGFS_EATTR(_name, _mode,					\
  	target_fabric_mappedlun_show_##_name,				\
  	target_fabric_mappedlun_store_##_name);
  
  static ssize_t target_fabric_mappedlun_show_write_protect(
  	struct se_lun_acl *lacl,
  	char *page)
  {
  	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  	struct se_dev_entry *deve;
  	ssize_t len;
  
  	spin_lock_irq(&se_nacl->device_list_lock);
  	deve = &se_nacl->device_list[lacl->mapped_lun];
  	len = sprintf(page, "%d
  ",
  			(deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
  			1 : 0);
  	spin_unlock_irq(&se_nacl->device_list_lock);
  
  	return len;
  }
  
  static ssize_t target_fabric_mappedlun_store_write_protect(
  	struct se_lun_acl *lacl,
  	const char *page,
  	size_t count)
  {
  	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  	struct se_portal_group *se_tpg = se_nacl->se_tpg;
  	unsigned long op;
  
  	if (strict_strtoul(page, 0, &op))
  		return -EINVAL;
  
  	if ((op != 1) && (op != 0))
  		return -EINVAL;
  
  	core_update_device_list_access(lacl->mapped_lun, (op) ?
  			TRANSPORT_LUNFLAGS_READ_ONLY :
  			TRANSPORT_LUNFLAGS_READ_WRITE,
  			lacl->se_lun_nacl);
6708bb27b   Andy Grover   target: Follow up...
208
  	pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
209
210
  		" Mapped LUN: %u Write Protect bit to %s
  ",
e3d6f909e   Andy Grover   target: Core clea...
211
  		se_tpg->se_tpg_tfo->get_fabric_name(),
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
212
213
214
215
216
217
218
219
220
  		lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
  
  	return count;
  
  }
  
  TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
  
  CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
221
222
223
224
225
226
227
228
  static void target_fabric_mappedlun_release(struct config_item *item)
  {
  	struct se_lun_acl *lacl = container_of(to_config_group(item),
  				struct se_lun_acl, se_lun_group);
  	struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  
  	core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  }
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
229
230
231
232
233
234
  static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  	&target_fabric_mappedlun_write_protect.attr,
  	NULL,
  };
  
  static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
235
  	.release		= target_fabric_mappedlun_release,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
236
237
238
239
240
241
242
243
244
245
  	.show_attribute		= target_fabric_mappedlun_attr_show,
  	.store_attribute	= target_fabric_mappedlun_attr_store,
  	.allow_link		= target_fabric_mappedlun_link,
  	.drop_link		= target_fabric_mappedlun_unlink,
  };
  
  TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  		target_fabric_mappedlun_attrs);
  
  /* End of tfc_tpg_mappedlun_cit */
12d233842   Nicholas Bellinger   [SCSI] target: ad...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  /* Start of tfc_tpg_mappedlun_port_cit */
  
  static struct config_group *target_core_mappedlun_stat_mkdir(
  	struct config_group *group,
  	const char *name)
  {
  	return ERR_PTR(-ENOSYS);
  }
  
  static void target_core_mappedlun_stat_rmdir(
  	struct config_group *group,
  	struct config_item *item)
  {
  	return;
  }
  
  static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  	.make_group		= target_core_mappedlun_stat_mkdir,
  	.drop_item		= target_core_mappedlun_stat_rmdir,
  };
  
  TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  		NULL);
  
  /* End of tfc_tpg_mappedlun_port_cit */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  /* Start of tfc_tpg_nacl_attrib_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
  
  static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
  	.show_attribute		= target_fabric_nacl_attrib_attr_show,
  	.store_attribute	= target_fabric_nacl_attrib_attr_store,
  };
  
  TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_nacl_attrib_cit */
  
  /* Start of tfc_tpg_nacl_auth_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
  
  static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
  	.show_attribute		= target_fabric_nacl_auth_attr_show,
  	.store_attribute	= target_fabric_nacl_auth_attr_store,
  };
  
  TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_nacl_auth_cit */
  
  /* Start of tfc_tpg_nacl_param_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
  
  static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
  	.show_attribute		= target_fabric_nacl_param_attr_show,
  	.store_attribute	= target_fabric_nacl_param_attr_store,
  };
  
  TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_nacl_param_cit */
  
  /* Start of tfc_tpg_nacl_base_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
  
  static struct config_group *target_fabric_make_mappedlun(
  	struct config_group *group,
  	const char *name)
  {
  	struct se_node_acl *se_nacl = container_of(group,
  			struct se_node_acl, acl_group);
  	struct se_portal_group *se_tpg = se_nacl->se_tpg;
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  	struct se_lun_acl *lacl;
  	struct config_item *acl_ci;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
324
  	struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
325
326
327
328
329
  	char *buf;
  	unsigned long mapped_lun;
  	int ret = 0;
  
  	acl_ci = &group->cg_item;
6708bb27b   Andy Grover   target: Follow up...
330
331
332
  	if (!acl_ci) {
  		pr_err("Unable to locatel acl_ci
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
333
334
335
336
  		return NULL;
  	}
  
  	buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
337
338
339
  	if (!buf) {
  		pr_err("Unable to allocate memory for name buf
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
340
341
342
343
344
345
346
  		return ERR_PTR(-ENOMEM);
  	}
  	snprintf(buf, strlen(name) + 1, "%s", name);
  	/*
  	 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  	 */
  	if (strstr(buf, "lun_") != buf) {
6708bb27b   Andy Grover   target: Follow up...
347
  		pr_err("Unable to locate \"lun_\" from buf: %s"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  			" name: %s
  ", buf, name);
  		ret = -EINVAL;
  		goto out;
  	}
  	/*
  	 * Determine the Mapped LUN value.  This is what the SCSI Initiator
  	 * Port will actually see.
  	 */
  	if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
  		ret = -EINVAL;
  		goto out;
  	}
  
  	lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
  			config_item_name(acl_ci), &ret);
6708bb27b   Andy Grover   target: Follow up...
364
  	if (!lacl) {
12d233842   Nicholas Bellinger   [SCSI] target: ad...
365
  		ret = -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
366
  		goto out;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
367
368
369
370
371
372
  	}
  
  	lacl_cg = &lacl->se_lun_group;
  	lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
  				GFP_KERNEL);
  	if (!lacl_cg->default_groups) {
6708bb27b   Andy Grover   target: Follow up...
373
374
  		pr_err("Unable to allocate lacl_cg->default_groups
  ");
12d233842   Nicholas Bellinger   [SCSI] target: ad...
375
376
377
  		ret = -ENOMEM;
  		goto out;
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
378
379
380
  
  	config_group_init_type_name(&lacl->se_lun_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
381
382
383
384
  	config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  			"statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
  	lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
  	lacl_cg->default_groups[1] = NULL;
e3d6f909e   Andy Grover   target: Core clea...
385
  	ml_stat_grp = &lacl->ml_stat_grps.stat_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
386
387
388
  	ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
  				GFP_KERNEL);
  	if (!ml_stat_grp->default_groups) {
6708bb27b   Andy Grover   target: Follow up...
389
390
  		pr_err("Unable to allocate ml_stat_grp->default_groups
  ");
12d233842   Nicholas Bellinger   [SCSI] target: ad...
391
392
393
394
  		ret = -ENOMEM;
  		goto out;
  	}
  	target_stat_setup_mappedlun_default_groups(lacl);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
395
396
397
398
  
  	kfree(buf);
  	return &lacl->se_lun_group;
  out:
12d233842   Nicholas Bellinger   [SCSI] target: ad...
399
400
  	if (lacl_cg)
  		kfree(lacl_cg->default_groups);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
401
402
403
404
405
406
407
408
  	kfree(buf);
  	return ERR_PTR(ret);
  }
  
  static void target_fabric_drop_mappedlun(
  	struct config_group *group,
  	struct config_item *item)
  {
12d233842   Nicholas Bellinger   [SCSI] target: ad...
409
410
411
412
413
  	struct se_lun_acl *lacl = container_of(to_config_group(item),
  			struct se_lun_acl, se_lun_group);
  	struct config_item *df_item;
  	struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  	int i;
e3d6f909e   Andy Grover   target: Core clea...
414
  	ml_stat_grp = &lacl->ml_stat_grps.stat_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  	for (i = 0; ml_stat_grp->default_groups[i]; i++) {
  		df_item = &ml_stat_grp->default_groups[i]->cg_item;
  		ml_stat_grp->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
  	kfree(ml_stat_grp->default_groups);
  
  	lacl_cg = &lacl->se_lun_group;
  	for (i = 0; lacl_cg->default_groups[i]; i++) {
  		df_item = &lacl_cg->default_groups[i]->cg_item;
  		lacl_cg->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
  	kfree(lacl_cg->default_groups);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
429
  	config_item_put(item);
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
430
431
432
433
434
435
436
437
438
439
  }
  
  static void target_fabric_nacl_base_release(struct config_item *item)
  {
  	struct se_node_acl *se_nacl = container_of(to_config_group(item),
  			struct se_node_acl, acl_group);
  	struct se_portal_group *se_tpg = se_nacl->se_tpg;
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  
  	tf->tf_ops.fabric_drop_nodeacl(se_nacl);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
440
441
442
  }
  
  static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
443
  	.release		= target_fabric_nacl_base_release,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
444
445
446
447
448
449
450
451
452
453
454
455
456
  	.show_attribute		= target_fabric_nacl_base_attr_show,
  	.store_attribute	= target_fabric_nacl_base_attr_store,
  };
  
  static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  	.make_group		= target_fabric_make_mappedlun,
  	.drop_item		= target_fabric_drop_mappedlun,
  };
  
  TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  		&target_fabric_nacl_base_group_ops, NULL);
  
  /* End of tfc_tpg_nacl_base_cit */
12d233842   Nicholas Bellinger   [SCSI] target: ad...
457
458
459
460
461
462
463
464
  /* Start of tfc_node_fabric_stats_cit */
  /*
   * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
   * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
   */
  TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  
  /* End of tfc_wwn_fabric_stats_cit */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
465
466
467
468
469
470
471
472
473
474
475
  /* Start of tfc_tpg_nacl_cit */
  
  static struct config_group *target_fabric_make_nodeacl(
  	struct config_group *group,
  	const char *name)
  {
  	struct se_portal_group *se_tpg = container_of(group,
  			struct se_portal_group, tpg_acl_group);
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  	struct se_node_acl *se_nacl;
  	struct config_group *nacl_cg;
6708bb27b   Andy Grover   target: Follow up...
476
477
478
  	if (!tf->tf_ops.fabric_make_nodeacl) {
  		pr_err("tf->tf_ops.fabric_make_nodeacl is NULL
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
479
480
481
482
483
  		return ERR_PTR(-ENOSYS);
  	}
  
  	se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
  	if (IS_ERR(se_nacl))
e1750ba20   Thomas Meyer   target: Use ERR_C...
484
  		return ERR_CAST(se_nacl);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
485
486
487
488
489
490
  
  	nacl_cg = &se_nacl->acl_group;
  	nacl_cg->default_groups = se_nacl->acl_default_groups;
  	nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  	nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  	nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
491
492
  	nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
  	nacl_cg->default_groups[4] = NULL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
493
494
495
496
497
498
499
500
501
  
  	config_group_init_type_name(&se_nacl->acl_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
  	config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
  	config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
  	config_group_init_type_name(&se_nacl->acl_param_group, "param",
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
502
503
504
  	config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  			"fabric_statistics",
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
505
506
507
508
509
510
511
512
  
  	return &se_nacl->acl_group;
  }
  
  static void target_fabric_drop_nodeacl(
  	struct config_group *group,
  	struct config_item *item)
  {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
513
514
515
516
517
518
519
520
521
522
523
524
  	struct se_node_acl *se_nacl = container_of(to_config_group(item),
  			struct se_node_acl, acl_group);
  	struct config_item *df_item;
  	struct config_group *nacl_cg;
  	int i;
  
  	nacl_cg = &se_nacl->acl_group;
  	for (i = 0; nacl_cg->default_groups[i]; i++) {
  		df_item = &nacl_cg->default_groups[i]->cg_item;
  		nacl_cg->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
525
526
527
  	/*
  	 * struct se_node_acl free is done in target_fabric_nacl_base_release()
  	 */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
528
  	config_item_put(item);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  }
  
  static struct configfs_group_operations target_fabric_nacl_group_ops = {
  	.make_group	= target_fabric_make_nodeacl,
  	.drop_item	= target_fabric_drop_nodeacl,
  };
  
  TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  
  /* End of tfc_tpg_nacl_cit */
  
  /* Start of tfc_tpg_np_base_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
543
544
545
546
547
548
549
550
551
  static void target_fabric_np_base_release(struct config_item *item)
  {
  	struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  				struct se_tpg_np, tpg_np_group);
  	struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  
  	tf->tf_ops.fabric_drop_np(se_tpg_np);
  }
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
552
  static struct configfs_item_operations target_fabric_np_base_item_ops = {
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
553
  	.release		= target_fabric_np_base_release,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  	.show_attribute		= target_fabric_np_base_attr_show,
  	.store_attribute	= target_fabric_np_base_attr_store,
  };
  
  TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_np_base_cit */
  
  /* Start of tfc_tpg_np_cit */
  
  static struct config_group *target_fabric_make_np(
  	struct config_group *group,
  	const char *name)
  {
  	struct se_portal_group *se_tpg = container_of(group,
  				struct se_portal_group, tpg_np_group);
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  	struct se_tpg_np *se_tpg_np;
6708bb27b   Andy Grover   target: Follow up...
572
573
574
  	if (!tf->tf_ops.fabric_make_np) {
  		pr_err("tf->tf_ops.fabric_make_np is NULL
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
575
576
577
578
  		return ERR_PTR(-ENOSYS);
  	}
  
  	se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
6708bb27b   Andy Grover   target: Follow up...
579
  	if (!se_tpg_np || IS_ERR(se_tpg_np))
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
580
  		return ERR_PTR(-EINVAL);
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
581
  	se_tpg_np->tpg_np_parent = se_tpg;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
582
583
584
585
586
587
588
589
590
591
  	config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
  
  	return &se_tpg_np->tpg_np_group;
  }
  
  static void target_fabric_drop_np(
  	struct config_group *group,
  	struct config_item *item)
  {
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
592
593
594
  	/*
  	 * struct se_tpg_np is released via target_fabric_np_base_release()
  	 */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
595
  	config_item_put(item);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  }
  
  static struct configfs_group_operations target_fabric_np_group_ops = {
  	.make_group	= &target_fabric_make_np,
  	.drop_item	= &target_fabric_drop_np,
  };
  
  TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  
  /* End of tfc_tpg_np_cit */
  
  /* Start of tfc_tpg_port_cit */
  
  CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  #define TCM_PORT_ATTR(_name, _mode)					\
  static struct target_fabric_port_attribute target_fabric_port_##_name =	\
  	__CONFIGFS_EATTR(_name, _mode,					\
  	target_fabric_port_show_attr_##_name,				\
  	target_fabric_port_store_attr_##_name);
  
  #define TCM_PORT_ATTOR_RO(_name)					\
  	__CONFIGFS_EATTR_RO(_name,					\
  	target_fabric_port_show_attr_##_name);
  
  /*
   * alua_tg_pt_gp
   */
  static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  	struct se_lun *lun,
  	char *page)
  {
6708bb27b   Andy Grover   target: Follow up...
627
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
628
629
630
631
632
633
634
635
636
637
  		return -ENODEV;
  
  	return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
  }
  
  static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
6708bb27b   Andy Grover   target: Follow up...
638
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  		return -ENODEV;
  
  	return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
  }
  
  TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  
  /*
   * alua_tg_pt_offline
   */
  static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  	struct se_lun *lun,
  	char *page)
  {
6708bb27b   Andy Grover   target: Follow up...
653
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
654
655
656
657
658
659
660
661
662
663
  		return -ENODEV;
  
  	return core_alua_show_offline_bit(lun, page);
  }
  
  static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
6708bb27b   Andy Grover   target: Follow up...
664
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  		return -ENODEV;
  
  	return core_alua_store_offline_bit(lun, page, count);
  }
  
  TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  
  /*
   * alua_tg_pt_status
   */
  static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  	struct se_lun *lun,
  	char *page)
  {
6708bb27b   Andy Grover   target: Follow up...
679
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
680
681
682
683
684
685
686
687
688
689
  		return -ENODEV;
  
  	return core_alua_show_secondary_status(lun, page);
  }
  
  static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
6708bb27b   Andy Grover   target: Follow up...
690
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  		return -ENODEV;
  
  	return core_alua_store_secondary_status(lun, page, count);
  }
  
  TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  
  /*
   * alua_tg_pt_write_md
   */
  static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  	struct se_lun *lun,
  	char *page)
  {
6708bb27b   Andy Grover   target: Follow up...
705
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
706
707
708
709
710
711
712
713
714
715
  		return -ENODEV;
  
  	return core_alua_show_secondary_write_metadata(lun, page);
  }
  
  static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
6708bb27b   Andy Grover   target: Follow up...
716
  	if (!lun || !lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  		return -ENODEV;
  
  	return core_alua_store_secondary_write_metadata(lun, page, count);
  }
  
  TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  
  
  static struct configfs_attribute *target_fabric_port_attrs[] = {
  	&target_fabric_port_alua_tg_pt_gp.attr,
  	&target_fabric_port_alua_tg_pt_offline.attr,
  	&target_fabric_port_alua_tg_pt_status.attr,
  	&target_fabric_port_alua_tg_pt_write_md.attr,
  	NULL,
  };
  
  CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  
  static int target_fabric_port_link(
  	struct config_item *lun_ci,
  	struct config_item *se_dev_ci)
  {
  	struct config_item *tpg_ci;
  	struct se_device *dev;
  	struct se_lun *lun = container_of(to_config_group(lun_ci),
  				struct se_lun, lun_group);
  	struct se_lun *lun_p;
  	struct se_portal_group *se_tpg;
  	struct se_subsystem_dev *se_dev = container_of(
  				to_config_group(se_dev_ci), struct se_subsystem_dev,
  				se_dev_group);
  	struct target_fabric_configfs *tf;
  	int ret;
  
  	tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  	se_tpg = container_of(to_config_group(tpg_ci),
  				struct se_portal_group, tpg_group);
  	tf = se_tpg->se_tpg_wwn->wwn_tf;
  
  	if (lun->lun_se_dev !=  NULL) {
6708bb27b   Andy Grover   target: Follow up...
757
758
  		pr_err("Port Symlink already exists
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
759
760
761
762
  		return -EEXIST;
  	}
  
  	dev = se_dev->se_dev_ptr;
6708bb27b   Andy Grover   target: Follow up...
763
764
  	if (!dev) {
  		pr_err("Unable to locate struct se_device pointer from"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
765
766
767
768
769
770
771
772
  			" %s
  ", config_item_name(se_dev_ci));
  		ret = -ENODEV;
  		goto out;
  	}
  
  	lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
  				lun->unpacked_lun);
6708bb27b   Andy Grover   target: Follow up...
773
774
775
  	if (IS_ERR(lun_p) || !lun_p) {
  		pr_err("core_dev_add_lun() failed
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  		ret = -EINVAL;
  		goto out;
  	}
  
  	if (tf->tf_ops.fabric_post_link) {
  		/*
  		 * Call the optional fabric_post_link() to allow a
  		 * fabric module to setup any additional state once
  		 * core_dev_add_lun() has been called..
  		 */
  		tf->tf_ops.fabric_post_link(se_tpg, lun);
  	}
  
  	return 0;
  out:
  	return ret;
  }
  
  static int target_fabric_port_unlink(
  	struct config_item *lun_ci,
  	struct config_item *se_dev_ci)
  {
  	struct se_lun *lun = container_of(to_config_group(lun_ci),
  				struct se_lun, lun_group);
  	struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  
  	if (tf->tf_ops.fabric_pre_unlink) {
  		/*
  		 * Call the optional fabric_pre_unlink() to allow a
  		 * fabric module to release any additional stat before
  		 * core_dev_del_lun() is called.
  		*/
  		tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
  	}
  
  	core_dev_del_lun(se_tpg, lun->unpacked_lun);
  	return 0;
  }
  
  static struct configfs_item_operations target_fabric_port_item_ops = {
  	.show_attribute		= target_fabric_port_attr_show,
  	.store_attribute	= target_fabric_port_attr_store,
  	.allow_link		= target_fabric_port_link,
  	.drop_link		= target_fabric_port_unlink,
  };
  
  TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  
  /* End of tfc_tpg_port_cit */
12d233842   Nicholas Bellinger   [SCSI] target: ad...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
  /* Start of tfc_tpg_port_stat_cit */
  
  static struct config_group *target_core_port_stat_mkdir(
  	struct config_group *group,
  	const char *name)
  {
  	return ERR_PTR(-ENOSYS);
  }
  
  static void target_core_port_stat_rmdir(
  	struct config_group *group,
  	struct config_item *item)
  {
  	return;
  }
  
  static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  	.make_group		= target_core_port_stat_mkdir,
  	.drop_item		= target_core_port_stat_rmdir,
  };
  
  TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  
  /* End of tfc_tpg_port_stat_cit */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
850
851
852
853
854
855
856
857
858
859
  /* Start of tfc_tpg_lun_cit */
  
  static struct config_group *target_fabric_make_lun(
  	struct config_group *group,
  	const char *name)
  {
  	struct se_lun *lun;
  	struct se_portal_group *se_tpg = container_of(group,
  			struct se_portal_group, tpg_lun_group);
  	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
860
  	struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
861
  	unsigned long unpacked_lun;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
862
  	int errno;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
863
864
  
  	if (strstr(name, "lun_") != name) {
6708bb27b   Andy Grover   target: Follow up...
865
  		pr_err("Unable to locate \'_\" in"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
866
867
868
869
870
871
872
873
  				" \"lun_$LUN_NUMBER\"
  ");
  		return ERR_PTR(-EINVAL);
  	}
  	if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
  		return ERR_PTR(-EINVAL);
  
  	lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
6708bb27b   Andy Grover   target: Follow up...
874
  	if (!lun)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
875
  		return ERR_PTR(-EINVAL);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
876
877
878
879
  	lun_cg = &lun->lun_group;
  	lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
  				GFP_KERNEL);
  	if (!lun_cg->default_groups) {
6708bb27b   Andy Grover   target: Follow up...
880
881
  		pr_err("Unable to allocate lun_cg->default_groups
  ");
12d233842   Nicholas Bellinger   [SCSI] target: ad...
882
883
  		return ERR_PTR(-ENOMEM);
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
884
885
  	config_group_init_type_name(&lun->lun_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
886
887
888
889
  	config_group_init_type_name(&lun->port_stat_grps.stat_group,
  			"statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
  	lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
  	lun_cg->default_groups[1] = NULL;
e3d6f909e   Andy Grover   target: Core clea...
890
  	port_stat_grp = &lun->port_stat_grps.stat_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
891
892
893
  	port_stat_grp->default_groups =  kzalloc(sizeof(struct config_group) * 3,
  				GFP_KERNEL);
  	if (!port_stat_grp->default_groups) {
6708bb27b   Andy Grover   target: Follow up...
894
895
  		pr_err("Unable to allocate port_stat_grp->default_groups
  ");
12d233842   Nicholas Bellinger   [SCSI] target: ad...
896
897
898
899
  		errno = -ENOMEM;
  		goto out;
  	}
  	target_stat_setup_port_default_groups(lun);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
900
901
  
  	return &lun->lun_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
902
903
904
905
  out:
  	if (lun_cg)
  		kfree(lun_cg->default_groups);
  	return ERR_PTR(errno);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
906
907
908
909
910
911
  }
  
  static void target_fabric_drop_lun(
  	struct config_group *group,
  	struct config_item *item)
  {
12d233842   Nicholas Bellinger   [SCSI] target: ad...
912
913
914
915
916
  	struct se_lun *lun = container_of(to_config_group(item),
  				struct se_lun, lun_group);
  	struct config_item *df_item;
  	struct config_group *lun_cg, *port_stat_grp;
  	int i;
e3d6f909e   Andy Grover   target: Core clea...
917
  	port_stat_grp = &lun->port_stat_grps.stat_group;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
918
919
920
921
922
923
924
925
926
927
928
929
930
931
  	for (i = 0; port_stat_grp->default_groups[i]; i++) {
  		df_item = &port_stat_grp->default_groups[i]->cg_item;
  		port_stat_grp->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
  	kfree(port_stat_grp->default_groups);
  
  	lun_cg = &lun->lun_group;
  	for (i = 0; lun_cg->default_groups[i]; i++) {
  		df_item = &lun_cg->default_groups[i]->cg_item;
  		lun_cg->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
  	kfree(lun_cg->default_groups);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  	config_item_put(item);
  }
  
  static struct configfs_group_operations target_fabric_lun_group_ops = {
  	.make_group	= &target_fabric_make_lun,
  	.drop_item	= &target_fabric_drop_lun,
  };
  
  TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  
  /* End of tfc_tpg_lun_cit */
  
  /* Start of tfc_tpg_attrib_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  
  static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  	.show_attribute		= target_fabric_tpg_attrib_attr_show,
  	.store_attribute	= target_fabric_tpg_attrib_attr_store,
  };
  
  TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_attrib_cit */
  
  /* Start of tfc_tpg_param_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  
  static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  	.show_attribute		= target_fabric_tpg_param_attr_show,
  	.store_attribute	= target_fabric_tpg_param_attr_store,
  };
  
  TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_param_cit */
  
  /* Start of tfc_tpg_base_cit */
  /*
   * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
   */
  CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
975
976
977
978
979
980
981
982
983
  static void target_fabric_tpg_release(struct config_item *item)
  {
  	struct se_portal_group *se_tpg = container_of(to_config_group(item),
  			struct se_portal_group, tpg_group);
  	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  	struct target_fabric_configfs *tf = wwn->wwn_tf;
  
  	tf->tf_ops.fabric_drop_tpg(se_tpg);
  }
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
984
  static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
985
  	.release		= target_fabric_tpg_release,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
  	.show_attribute		= target_fabric_tpg_attr_show,
  	.store_attribute	= target_fabric_tpg_attr_store,
  };
  
  TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
  
  /* End of tfc_tpg_base_cit */
  
  /* Start of tfc_tpg_cit */
  
  static struct config_group *target_fabric_make_tpg(
  	struct config_group *group,
  	const char *name)
  {
  	struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  	struct target_fabric_configfs *tf = wwn->wwn_tf;
  	struct se_portal_group *se_tpg;
6708bb27b   Andy Grover   target: Follow up...
1003
1004
1005
  	if (!tf->tf_ops.fabric_make_tpg) {
  		pr_err("tf->tf_ops.fabric_make_tpg is NULL
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1006
1007
1008
1009
  		return ERR_PTR(-ENOSYS);
  	}
  
  	se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
6708bb27b   Andy Grover   target: Follow up...
1010
  	if (!se_tpg || IS_ERR(se_tpg))
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  		return ERR_PTR(-EINVAL);
  	/*
  	 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  	 */
  	se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  	se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  	se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  	se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  	se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  	se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
  	se_tpg->tpg_group.default_groups[5] = NULL;
  
  	config_group_init_type_name(&se_tpg->tpg_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
  	config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  			&TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
  	config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  			&TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
  	config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  			&TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
  	config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  			&TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
  	config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  			&TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
  
  	return &se_tpg->tpg_group;
  }
  
  static void target_fabric_drop_tpg(
  	struct config_group *group,
  	struct config_item *item)
  {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
  	struct se_portal_group *se_tpg = container_of(to_config_group(item),
  				struct se_portal_group, tpg_group);
  	struct config_group *tpg_cg = &se_tpg->tpg_group;
  	struct config_item *df_item;
  	int i;
  	/*
  	 * Release default groups, but do not release tpg_cg->default_groups
  	 * memory as it is statically allocated at se_tpg->tpg_default_groups.
  	 */
  	for (i = 0; tpg_cg->default_groups[i]; i++) {
  		df_item = &tpg_cg->default_groups[i]->cg_item;
  		tpg_cg->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
  
  	config_item_put(item);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1059
  }
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  static void target_fabric_release_wwn(struct config_item *item)
  {
  	struct se_wwn *wwn = container_of(to_config_group(item),
  				struct se_wwn, wwn_group);
  	struct target_fabric_configfs *tf = wwn->wwn_tf;
  
  	tf->tf_ops.fabric_drop_wwn(wwn);
  }
  
  static struct configfs_item_operations target_fabric_tpg_item_ops = {
  	.release	= target_fabric_release_wwn,
  };
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1072
1073
1074
1075
  static struct configfs_group_operations target_fabric_tpg_group_ops = {
  	.make_group	= target_fabric_make_tpg,
  	.drop_item	= target_fabric_drop_tpg,
  };
1f6fe7cba   Nicholas Bellinger   [SCSI] target: fi...
1076
1077
  TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  		NULL);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1078
1079
  
  /* End of tfc_tpg_cit */
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1080
1081
1082
1083
1084
1085
1086
1087
  /* Start of tfc_wwn_fabric_stats_cit */
  /*
   * This is used as a placeholder for struct se_wwn->fabric_stat_group
   * to allow fabrics access to ->fabric_stat_group->default_groups[]
   */
  TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  
  /* End of tfc_wwn_fabric_stats_cit */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1088
1089
1090
1091
1092
1093
1094
1095
1096
  /* Start of tfc_wwn_cit */
  
  static struct config_group *target_fabric_make_wwn(
  	struct config_group *group,
  	const char *name)
  {
  	struct target_fabric_configfs *tf = container_of(group,
  				struct target_fabric_configfs, tf_group);
  	struct se_wwn *wwn;
6708bb27b   Andy Grover   target: Follow up...
1097
1098
1099
  	if (!tf->tf_ops.fabric_make_wwn) {
  		pr_err("tf->tf_ops.fabric_make_wwn is NULL
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1100
1101
1102
1103
  		return ERR_PTR(-ENOSYS);
  	}
  
  	wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
6708bb27b   Andy Grover   target: Follow up...
1104
  	if (!wwn || IS_ERR(wwn))
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1105
1106
1107
  		return ERR_PTR(-EINVAL);
  
  	wwn->wwn_tf = tf;
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1108
1109
1110
1111
1112
1113
  	/*
  	 * Setup default groups from pre-allocated wwn->wwn_default_groups
  	 */
  	wwn->wwn_group.default_groups = wwn->wwn_default_groups;
  	wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
  	wwn->wwn_group.default_groups[1] = NULL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1114
1115
  	config_group_init_type_name(&wwn->wwn_group, name,
  			&TF_CIT_TMPL(tf)->tfc_tpg_cit);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1116
1117
  	config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  			&TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1118
1119
1120
1121
1122
1123
1124
1125
  
  	return &wwn->wwn_group;
  }
  
  static void target_fabric_drop_wwn(
  	struct config_group *group,
  	struct config_item *item)
  {
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
  	struct se_wwn *wwn = container_of(to_config_group(item),
  				struct se_wwn, wwn_group);
  	struct config_item *df_item;
  	struct config_group *cg = &wwn->wwn_group;
  	int i;
  
  	for (i = 0; cg->default_groups[i]; i++) {
  		df_item = &cg->default_groups[i]->cg_item;
  		cg->default_groups[i] = NULL;
  		config_item_put(df_item);
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1137
  	config_item_put(item);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
  }
  
  static struct configfs_group_operations target_fabric_wwn_group_ops = {
  	.make_group	= target_fabric_make_wwn,
  	.drop_item	= target_fabric_drop_wwn,
  };
  /*
   * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
   */
  CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  
  static struct configfs_item_operations target_fabric_wwn_item_ops = {
  	.show_attribute		= target_fabric_wwn_attr_show,
  	.store_attribute	= target_fabric_wwn_attr_store,
  };
  
  TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
  
  /* End of tfc_wwn_cit */
  
  /* Start of tfc_discovery_cit */
  
  CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  		tf_disc_group);
  
  static struct configfs_item_operations target_fabric_discovery_item_ops = {
  	.show_attribute		= target_fabric_discovery_attr_show,
  	.store_attribute	= target_fabric_discovery_attr_store,
  };
  
  TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
  
  /* End of tfc_discovery_cit */
  
  int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  {
  	target_fabric_setup_discovery_cit(tf);
  	target_fabric_setup_wwn_cit(tf);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1176
  	target_fabric_setup_wwn_fabric_stats_cit(tf);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1177
1178
1179
  	target_fabric_setup_tpg_cit(tf);
  	target_fabric_setup_tpg_base_cit(tf);
  	target_fabric_setup_tpg_port_cit(tf);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1180
  	target_fabric_setup_tpg_port_stat_cit(tf);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  	target_fabric_setup_tpg_lun_cit(tf);
  	target_fabric_setup_tpg_np_cit(tf);
  	target_fabric_setup_tpg_np_base_cit(tf);
  	target_fabric_setup_tpg_attrib_cit(tf);
  	target_fabric_setup_tpg_param_cit(tf);
  	target_fabric_setup_tpg_nacl_cit(tf);
  	target_fabric_setup_tpg_nacl_base_cit(tf);
  	target_fabric_setup_tpg_nacl_attrib_cit(tf);
  	target_fabric_setup_tpg_nacl_auth_cit(tf);
  	target_fabric_setup_tpg_nacl_param_cit(tf);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1191
  	target_fabric_setup_tpg_nacl_stat_cit(tf);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1192
  	target_fabric_setup_tpg_mappedlun_cit(tf);
12d233842   Nicholas Bellinger   [SCSI] target: ad...
1193
  	target_fabric_setup_tpg_mappedlun_stat_cit(tf);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1194
1195
1196
  
  	return 0;
  }