Skip to content

Commit 8e47b96

Browse files
Christoph HellwigJames Bottomley
authored andcommitted
[PATCH] place host-related LDM code directly in hosts.c
This was in scsi_sysfs.c previously but given that it's not sysfs-related and the whole scsi code is built around the driver model now it's better to have it where it belongs. Also allows us to reduce the scsi_mod-wide globals.
1 parent 976c09e commit 8e47b96

File tree

4 files changed

+96
-112
lines changed

4 files changed

+96
-112
lines changed

drivers/scsi/hosts.c

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
static int scsi_host_next_hn; /* host_no for next new host */
4141

4242

43+
static void scsi_host_cls_release(struct class_device *class_dev)
44+
{
45+
put_device(&class_to_shost(class_dev)->shost_gendev);
46+
}
47+
48+
static struct class shost_class = {
49+
.name = "scsi_host",
50+
.release = scsi_host_cls_release,
51+
};
52+
4353
/**
4454
* scsi_host_cancel - cancel outstanding IO to this host
4555
* @shost: pointer to struct Scsi_Host
@@ -64,10 +74,18 @@ void scsi_host_cancel(struct Scsi_Host *shost, int recovery)
6474
**/
6575
void scsi_remove_host(struct Scsi_Host *shost)
6676
{
77+
unsigned long flags;
78+
6779
scsi_host_cancel(shost, 0);
6880
scsi_proc_host_rm(shost);
6981
scsi_forget_host(shost);
70-
scsi_sysfs_remove_host(shost);
82+
83+
spin_lock_irqsave(shost->host_lock, flags);
84+
set_bit(SHOST_DEL, &shost->shost_state);
85+
spin_unlock_irqrestore(shost->host_lock, flags);
86+
87+
class_device_unregister(&shost->shost_classdev);
88+
device_del(&shost->shost_gendev);
7189
}
7290

7391
/**
@@ -89,21 +107,45 @@ int scsi_add_host(struct Scsi_Host *shost, struct device *dev)
89107
if (!shost->can_queue) {
90108
printk(KERN_ERR "%s: can_queue = 0 no longer supported\n",
91109
sht->name);
92-
error = -EINVAL;
110+
return -EINVAL;
93111
}
94112

95-
error = scsi_sysfs_add_host(shost, dev);
96-
if (!error)
97-
scsi_proc_host_add(shost);
113+
if (!shost->shost_gendev.parent)
114+
shost->shost_gendev.parent = dev ? dev : &legacy_bus;
115+
116+
error = device_add(&shost->shost_gendev);
117+
if (error)
118+
goto out;
119+
120+
set_bit(SHOST_ADD, &shost->shost_state);
121+
get_device(shost->shost_gendev.parent);
122+
123+
error = class_device_add(&shost->shost_classdev);
124+
if (error)
125+
goto out_del_gendev;
126+
127+
get_device(&shost->shost_gendev);
128+
129+
error = scsi_sysfs_add_host(shost);
130+
if (error)
131+
goto out_del_classdev;
132+
133+
scsi_proc_host_add(shost);
134+
return error;
135+
136+
out_del_classdev:
137+
class_device_del(&shost->shost_classdev);
138+
out_del_gendev:
139+
device_del(&shost->shost_gendev);
140+
out:
98141
return error;
99142
}
100143

101-
/**
102-
* scsi_free_sdev - free a scsi hosts resources
103-
* @shost: scsi host to free
104-
**/
105-
void scsi_free_shost(struct Scsi_Host *shost)
144+
static void scsi_host_dev_release(struct device *dev)
106145
{
146+
struct Scsi_Host *shost = dev_to_shost(dev);
147+
struct device *parent = dev->parent;
148+
107149
if (shost->ehandler) {
108150
DECLARE_COMPLETION(sem);
109151
shost->eh_notify = &sem;
@@ -115,6 +157,8 @@ void scsi_free_shost(struct Scsi_Host *shost)
115157

116158
scsi_proc_hostdir_rm(shost->hostt);
117159
scsi_destroy_command_freelist(shost);
160+
161+
put_device(parent);
118162
kfree(shost);
119163
}
120164

@@ -214,7 +258,18 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
214258
if (rval)
215259
goto fail;
216260

217-
scsi_sysfs_init_host(shost);
261+
device_initialize(&shost->shost_gendev);
262+
snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
263+
shost->host_no);
264+
snprintf(shost->shost_gendev.name, DEVICE_NAME_SIZE, "%s",
265+
shost->hostt->proc_name);
266+
shost->shost_gendev.release = scsi_host_dev_release;
267+
268+
class_device_initialize(&shost->shost_classdev);
269+
shost->shost_classdev.dev = &shost->shost_gendev;
270+
shost->shost_classdev.class = &shost_class;
271+
snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
272+
shost->host_no);
218273

219274
shost->eh_notify = &complete;
220275
/* XXX(hch): handle error return */
@@ -299,3 +354,13 @@ void scsi_host_put(struct Scsi_Host *shost)
299354
{
300355
put_device(&shost->shost_gendev);
301356
}
357+
358+
int scsi_init_hosts(void)
359+
{
360+
return class_register(&shost_class);
361+
}
362+
363+
void scsi_exit_hosts(void)
364+
{
365+
class_unregister(&shost_class);
366+
}

drivers/scsi/scsi.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,12 @@ static int __init init_scsi(void)
10031003
error = scsi_init_devinfo();
10041004
if (error)
10051005
goto cleanup_procfs;
1006-
error = scsi_sysfs_register();
1006+
error = scsi_init_hosts();
10071007
if (error)
10081008
goto cleanup_devlist;
1009+
error = scsi_sysfs_register();
1010+
if (error)
1011+
goto cleanup_hosts;
10091012

10101013
for (i = 0; i < NR_CPUS; i++)
10111014
INIT_LIST_HEAD(&done_q[i]);
@@ -1015,6 +1018,8 @@ static int __init init_scsi(void)
10151018
printk(KERN_NOTICE "SCSI subsystem initialized\n");
10161019
return 0;
10171020

1021+
cleanup_hosts:
1022+
scsi_exit_hosts();
10181023
cleanup_devlist:
10191024
scsi_exit_devinfo();
10201025
cleanup_procfs:
@@ -1029,6 +1034,7 @@ static int __init init_scsi(void)
10291034
static void __exit exit_scsi(void)
10301035
{
10311036
scsi_sysfs_unregister();
1037+
scsi_exit_hosts();
10321038
scsi_exit_devinfo();
10331039
devfs_remove("scsi");
10341040
scsi_exit_procfs();

drivers/scsi/scsi_priv.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ struct scsi_target {
5151
unsigned int starget_refcnt;
5252
};
5353

54+
/* hosts.c */
55+
extern int scsi_init_hosts(void);
56+
extern void scsi_exit_hosts(void);
5457

5558
/* scsi.c */
5659
extern int scsi_dispatch_cmd(struct scsi_cmnd *cmd);
@@ -108,18 +111,14 @@ extern void scsi_exit_procfs(void);
108111
/* scsi_scan.c */
109112
extern void scsi_forget_host(struct Scsi_Host *);
110113
extern void scsi_free_sdev(struct scsi_device *);
111-
extern void scsi_free_shost(struct Scsi_Host *);
112114
extern void scsi_rescan_device(struct device *);
113115

114116
/* scsi_sysfs.c */
115117
extern int scsi_device_register(struct scsi_device *);
116-
extern void scsi_sysfs_init_host(struct Scsi_Host *);
117-
extern int scsi_sysfs_add_host(struct Scsi_Host *, struct device *);
118-
extern void scsi_sysfs_remove_host(struct Scsi_Host *);
118+
extern int scsi_sysfs_add_host(struct Scsi_Host *);
119119
extern int scsi_sysfs_register(void);
120120
extern void scsi_sysfs_unregister(void);
121121

122-
extern struct class shost_class;
123122
extern struct class sdev_class;
124123
extern struct bus_type scsi_bus_type;
125124

drivers/scsi/scsi_sysfs.c

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,6 @@ static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
5454
NULL
5555
};
5656

57-
static void scsi_host_cls_release(struct class_device *class_dev)
58-
{
59-
struct Scsi_Host *shost;
60-
61-
shost = class_to_shost(class_dev);
62-
put_device(&shost->shost_gendev);
63-
}
64-
65-
static void scsi_host_dev_release(struct device *dev)
66-
{
67-
struct Scsi_Host *shost;
68-
struct device *parent;
69-
70-
parent = dev->parent;
71-
shost = dev_to_shost(dev);
72-
scsi_free_shost(shost);
73-
put_device(parent);
74-
}
75-
76-
struct class shost_class = {
77-
.name = "scsi_host",
78-
.release = scsi_host_cls_release,
79-
};
80-
8157
static void scsi_device_cls_release(struct class_device *class_dev)
8258
{
8359
struct scsi_device *sdev;
@@ -119,27 +95,18 @@ int scsi_sysfs_register(void)
11995
int error;
12096

12197
error = bus_register(&scsi_bus_type);
122-
if (error)
123-
return error;
124-
error = class_register(&shost_class);
125-
if (error)
126-
goto bus_unregister;
127-
error = class_register(&sdev_class);
128-
if (error)
129-
goto class_unregister;
130-
return 0;
98+
if (!error) {
99+
error = class_register(&sdev_class);
100+
if (error)
101+
bus_unregister(&scsi_bus_type);
102+
}
131103

132-
class_unregister:
133-
class_unregister(&shost_class);
134-
bus_unregister:
135-
bus_unregister(&scsi_bus_type);
136104
return error;
137105
}
138106

139107
void scsi_sysfs_unregister(void)
140108
{
141109
class_unregister(&sdev_class);
142-
class_unregister(&shost_class);
143110
bus_unregister(&scsi_bus_type);
144111
}
145112

@@ -403,22 +370,6 @@ int scsi_register_interface(struct class_interface *intf)
403370
}
404371

405372

406-
void scsi_sysfs_init_host(struct Scsi_Host *shost)
407-
{
408-
device_initialize(&shost->shost_gendev);
409-
snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
410-
shost->host_no);
411-
snprintf(shost->shost_gendev.name, DEVICE_NAME_SIZE, "%s",
412-
shost->hostt->proc_name);
413-
shost->shost_gendev.release = scsi_host_dev_release;
414-
415-
class_device_initialize(&shost->shost_classdev);
416-
shost->shost_classdev.dev = &shost->shost_gendev;
417-
shost->shost_classdev.class = &shost_class;
418-
snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
419-
shost->host_no);
420-
}
421-
422373
static struct class_device_attribute *class_attr_overridden(
423374
struct class_device_attribute **attrs,
424375
struct class_device_attribute *attr)
@@ -461,31 +412,16 @@ static int class_attr_add(struct class_device *classdev,
461412
* @shost: scsi host struct to add to subsystem
462413
* @dev: parent struct device pointer
463414
**/
464-
int scsi_sysfs_add_host(struct Scsi_Host *shost, struct device *dev)
415+
int scsi_sysfs_add_host(struct Scsi_Host *shost)
465416
{
466417
int error, i;
467418

468-
if (!shost->shost_gendev.parent)
469-
shost->shost_gendev.parent = dev ? dev : &legacy_bus;
470-
471-
error = device_add(&shost->shost_gendev);
472-
if (error)
473-
return error;
474-
475-
set_bit(SHOST_ADD, &shost->shost_state);
476-
get_device(shost->shost_gendev.parent);
477-
478-
error = class_device_add(&shost->shost_classdev);
479-
if (error)
480-
goto clean_device;
481-
482-
get_device(&shost->shost_gendev);
483419
if (shost->hostt->shost_attrs) {
484420
for (i = 0; shost->hostt->shost_attrs[i]; i++) {
485421
error = class_attr_add(&shost->shost_classdev,
486422
shost->hostt->shost_attrs[i]);
487423
if (error)
488-
goto clean_class;
424+
return error;
489425
}
490426
}
491427

@@ -495,31 +431,9 @@ int scsi_sysfs_add_host(struct Scsi_Host *shost, struct device *dev)
495431
error = class_device_create_file(&shost->shost_classdev,
496432
scsi_sysfs_shost_attrs[i]);
497433
if (error)
498-
goto clean_class;
434+
return error;
499435
}
500436
}
501437

502-
return error;
503-
504-
clean_class:
505-
class_device_del(&shost->shost_classdev);
506-
clean_device:
507-
device_del(&shost->shost_gendev);
508-
509-
return error;
510-
}
511-
512-
/**
513-
* scsi_sysfs_remove_host - remove scsi host from subsystem
514-
* @shost: scsi host to remove from subsystem
515-
**/
516-
void scsi_sysfs_remove_host(struct Scsi_Host *shost)
517-
{
518-
unsigned long flags;
519-
spin_lock_irqsave(shost->host_lock, flags);
520-
set_bit(SHOST_DEL, &shost->shost_state);
521-
spin_unlock_irqrestore(shost->host_lock, flags);
522-
523-
class_device_unregister(&shost->shost_classdev);
524-
device_del(&shost->shost_gendev);
438+
return 0;
525439
}

0 commit comments

Comments
 (0)