Commit fa366ad5d7fe05abaae44a1cd216348669e42ef8

Authored by Serge E. Hallyn
Committed by Linus Torvalds
1 parent c7b2eff059

[PATCH] kthread: convert smbiod

Update smbiod to use kthread instead of deprecated kernel_thread.

[akpm@osdl.org: cleanup]
Signed-off-by: Serge E. Hallyn <serue@us.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

Showing 1 changed file with 17 additions and 9 deletions Inline Diff

1 /* 1 /*
2 * smbiod.c 2 * smbiod.c
3 * 3 *
4 * Copyright (C) 2000, Charles Loep / Corel Corp. 4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark 5 * Copyright (C) 2001, Urban Widmark
6 */ 6 */
7 7
8 #include <linux/config.h> 8 #include <linux/config.h>
9 9
10 #include <linux/sched.h> 10 #include <linux/sched.h>
11 #include <linux/kernel.h> 11 #include <linux/kernel.h>
12 #include <linux/mm.h> 12 #include <linux/mm.h>
13 #include <linux/string.h> 13 #include <linux/string.h>
14 #include <linux/stat.h> 14 #include <linux/stat.h>
15 #include <linux/errno.h> 15 #include <linux/errno.h>
16 #include <linux/slab.h> 16 #include <linux/slab.h>
17 #include <linux/init.h> 17 #include <linux/init.h>
18 #include <linux/file.h> 18 #include <linux/file.h>
19 #include <linux/dcache.h> 19 #include <linux/dcache.h>
20 #include <linux/smp_lock.h> 20 #include <linux/smp_lock.h>
21 #include <linux/module.h> 21 #include <linux/module.h>
22 #include <linux/net.h> 22 #include <linux/net.h>
23 #include <linux/kthread.h>
23 #include <net/ip.h> 24 #include <net/ip.h>
24 25
25 #include <linux/smb_fs.h> 26 #include <linux/smb_fs.h>
26 #include <linux/smbno.h> 27 #include <linux/smbno.h>
27 #include <linux/smb_mount.h> 28 #include <linux/smb_mount.h>
28 29
29 #include <asm/system.h> 30 #include <asm/system.h>
30 #include <asm/uaccess.h> 31 #include <asm/uaccess.h>
31 32
32 #include "smb_debug.h" 33 #include "smb_debug.h"
33 #include "request.h" 34 #include "request.h"
34 #include "proto.h" 35 #include "proto.h"
35 36
36 enum smbiod_state { 37 enum smbiod_state {
37 SMBIOD_DEAD, 38 SMBIOD_DEAD,
38 SMBIOD_STARTING, 39 SMBIOD_STARTING,
39 SMBIOD_RUNNING, 40 SMBIOD_RUNNING,
40 }; 41 };
41 42
42 static enum smbiod_state smbiod_state = SMBIOD_DEAD; 43 static enum smbiod_state smbiod_state = SMBIOD_DEAD;
43 static pid_t smbiod_pid; 44 static struct task_struct *smbiod_thread;
44 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait); 45 static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
45 static LIST_HEAD(smb_servers); 46 static LIST_HEAD(smb_servers);
46 static DEFINE_SPINLOCK(servers_lock); 47 static DEFINE_SPINLOCK(servers_lock);
47 48
48 #define SMBIOD_DATA_READY (1<<0) 49 #define SMBIOD_DATA_READY (1<<0)
49 static long smbiod_flags; 50 static long smbiod_flags;
50 51
51 static int smbiod(void *); 52 static int smbiod(void *);
52 static int smbiod_start(void); 53 static int smbiod_start(void);
53 54
54 /* 55 /*
55 * called when there's work for us to do 56 * called when there's work for us to do
56 */ 57 */
57 void smbiod_wake_up(void) 58 void smbiod_wake_up(void)
58 { 59 {
59 if (smbiod_state == SMBIOD_DEAD) 60 if (smbiod_state == SMBIOD_DEAD)
60 return; 61 return;
61 set_bit(SMBIOD_DATA_READY, &smbiod_flags); 62 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
62 wake_up_interruptible(&smbiod_wait); 63 wake_up_interruptible(&smbiod_wait);
63 } 64 }
64 65
65 /* 66 /*
66 * start smbiod if none is running 67 * start smbiod if none is running
67 */ 68 */
68 static int smbiod_start(void) 69 static int smbiod_start(void)
69 { 70 {
70 pid_t pid; 71 struct task_struct *tsk;
72 int err = 0;
73
71 if (smbiod_state != SMBIOD_DEAD) 74 if (smbiod_state != SMBIOD_DEAD)
72 return 0; 75 return 0;
73 smbiod_state = SMBIOD_STARTING; 76 smbiod_state = SMBIOD_STARTING;
74 __module_get(THIS_MODULE); 77 __module_get(THIS_MODULE);
75 spin_unlock(&servers_lock); 78 spin_unlock(&servers_lock);
76 pid = kernel_thread(smbiod, NULL, 0); 79 tsk = kthread_run(smbiod, NULL, "smbiod");
77 if (pid < 0) 80 if (IS_ERR(tsk)) {
81 err = PTR_ERR(tsk);
78 module_put(THIS_MODULE); 82 module_put(THIS_MODULE);
83 }
79 84
80 spin_lock(&servers_lock); 85 spin_lock(&servers_lock);
81 smbiod_state = pid < 0 ? SMBIOD_DEAD : SMBIOD_RUNNING; 86 if (err < 0) {
82 smbiod_pid = pid; 87 smbiod_state = SMBIOD_DEAD;
83 return pid; 88 smbiod_thread = NULL;
89 } else {
90 smbiod_state = SMBIOD_RUNNING;
91 smbiod_thread = tsk;
92 }
93 return err;
84 } 94 }
85 95
86 /* 96 /*
87 * register a server & start smbiod if necessary 97 * register a server & start smbiod if necessary
88 */ 98 */
89 int smbiod_register_server(struct smb_sb_info *server) 99 int smbiod_register_server(struct smb_sb_info *server)
90 { 100 {
91 int ret; 101 int ret;
92 spin_lock(&servers_lock); 102 spin_lock(&servers_lock);
93 list_add(&server->entry, &smb_servers); 103 list_add(&server->entry, &smb_servers);
94 VERBOSE("%p\n", server); 104 VERBOSE("%p\n", server);
95 ret = smbiod_start(); 105 ret = smbiod_start();
96 spin_unlock(&servers_lock); 106 spin_unlock(&servers_lock);
97 return ret; 107 return ret;
98 } 108 }
99 109
100 /* 110 /*
101 * Unregister a server 111 * Unregister a server
102 * Must be called with the server lock held. 112 * Must be called with the server lock held.
103 */ 113 */
104 void smbiod_unregister_server(struct smb_sb_info *server) 114 void smbiod_unregister_server(struct smb_sb_info *server)
105 { 115 {
106 spin_lock(&servers_lock); 116 spin_lock(&servers_lock);
107 list_del_init(&server->entry); 117 list_del_init(&server->entry);
108 VERBOSE("%p\n", server); 118 VERBOSE("%p\n", server);
109 spin_unlock(&servers_lock); 119 spin_unlock(&servers_lock);
110 120
111 smbiod_wake_up(); 121 smbiod_wake_up();
112 smbiod_flush(server); 122 smbiod_flush(server);
113 } 123 }
114 124
115 void smbiod_flush(struct smb_sb_info *server) 125 void smbiod_flush(struct smb_sb_info *server)
116 { 126 {
117 struct list_head *tmp, *n; 127 struct list_head *tmp, *n;
118 struct smb_request *req; 128 struct smb_request *req;
119 129
120 list_for_each_safe(tmp, n, &server->xmitq) { 130 list_for_each_safe(tmp, n, &server->xmitq) {
121 req = list_entry(tmp, struct smb_request, rq_queue); 131 req = list_entry(tmp, struct smb_request, rq_queue);
122 req->rq_errno = -EIO; 132 req->rq_errno = -EIO;
123 list_del_init(&req->rq_queue); 133 list_del_init(&req->rq_queue);
124 smb_rput(req); 134 smb_rput(req);
125 wake_up_interruptible(&req->rq_wait); 135 wake_up_interruptible(&req->rq_wait);
126 } 136 }
127 list_for_each_safe(tmp, n, &server->recvq) { 137 list_for_each_safe(tmp, n, &server->recvq) {
128 req = list_entry(tmp, struct smb_request, rq_queue); 138 req = list_entry(tmp, struct smb_request, rq_queue);
129 req->rq_errno = -EIO; 139 req->rq_errno = -EIO;
130 list_del_init(&req->rq_queue); 140 list_del_init(&req->rq_queue);
131 smb_rput(req); 141 smb_rput(req);
132 wake_up_interruptible(&req->rq_wait); 142 wake_up_interruptible(&req->rq_wait);
133 } 143 }
134 } 144 }
135 145
136 /* 146 /*
137 * Wake up smbmount and make it reconnect to the server. 147 * Wake up smbmount and make it reconnect to the server.
138 * This must be called with the server locked. 148 * This must be called with the server locked.
139 * 149 *
140 * FIXME: add smbconnect version to this 150 * FIXME: add smbconnect version to this
141 */ 151 */
142 int smbiod_retry(struct smb_sb_info *server) 152 int smbiod_retry(struct smb_sb_info *server)
143 { 153 {
144 struct list_head *head; 154 struct list_head *head;
145 struct smb_request *req; 155 struct smb_request *req;
146 pid_t pid = server->conn_pid; 156 pid_t pid = server->conn_pid;
147 int result = 0; 157 int result = 0;
148 158
149 VERBOSE("state: %d\n", server->state); 159 VERBOSE("state: %d\n", server->state);
150 if (server->state == CONN_VALID || server->state == CONN_RETRYING) 160 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
151 goto out; 161 goto out;
152 162
153 smb_invalidate_inodes(server); 163 smb_invalidate_inodes(server);
154 164
155 /* 165 /*
156 * Some requests are meaningless after a retry, so we abort them. 166 * Some requests are meaningless after a retry, so we abort them.
157 * One example are all requests using 'fileid' since the files are 167 * One example are all requests using 'fileid' since the files are
158 * closed on retry. 168 * closed on retry.
159 */ 169 */
160 head = server->xmitq.next; 170 head = server->xmitq.next;
161 while (head != &server->xmitq) { 171 while (head != &server->xmitq) {
162 req = list_entry(head, struct smb_request, rq_queue); 172 req = list_entry(head, struct smb_request, rq_queue);
163 head = head->next; 173 head = head->next;
164 174
165 req->rq_bytes_sent = 0; 175 req->rq_bytes_sent = 0;
166 if (req->rq_flags & SMB_REQ_NORETRY) { 176 if (req->rq_flags & SMB_REQ_NORETRY) {
167 VERBOSE("aborting request %p on xmitq\n", req); 177 VERBOSE("aborting request %p on xmitq\n", req);
168 req->rq_errno = -EIO; 178 req->rq_errno = -EIO;
169 list_del_init(&req->rq_queue); 179 list_del_init(&req->rq_queue);
170 smb_rput(req); 180 smb_rput(req);
171 wake_up_interruptible(&req->rq_wait); 181 wake_up_interruptible(&req->rq_wait);
172 } 182 }
173 } 183 }
174 184
175 /* 185 /*
176 * FIXME: test the code for retrying request we already sent 186 * FIXME: test the code for retrying request we already sent
177 */ 187 */
178 head = server->recvq.next; 188 head = server->recvq.next;
179 while (head != &server->recvq) { 189 while (head != &server->recvq) {
180 req = list_entry(head, struct smb_request, rq_queue); 190 req = list_entry(head, struct smb_request, rq_queue);
181 head = head->next; 191 head = head->next;
182 #if 0 192 #if 0
183 if (req->rq_flags & SMB_REQ_RETRY) { 193 if (req->rq_flags & SMB_REQ_RETRY) {
184 /* must move the request to the xmitq */ 194 /* must move the request to the xmitq */
185 VERBOSE("retrying request %p on recvq\n", req); 195 VERBOSE("retrying request %p on recvq\n", req);
186 list_del(&req->rq_queue); 196 list_del(&req->rq_queue);
187 list_add(&req->rq_queue, &server->xmitq); 197 list_add(&req->rq_queue, &server->xmitq);
188 continue; 198 continue;
189 } 199 }
190 #endif 200 #endif
191 201
192 VERBOSE("aborting request %p on recvq\n", req); 202 VERBOSE("aborting request %p on recvq\n", req);
193 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */ 203 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
194 req->rq_errno = -EIO; 204 req->rq_errno = -EIO;
195 list_del_init(&req->rq_queue); 205 list_del_init(&req->rq_queue);
196 smb_rput(req); 206 smb_rput(req);
197 wake_up_interruptible(&req->rq_wait); 207 wake_up_interruptible(&req->rq_wait);
198 } 208 }
199 209
200 smb_close_socket(server); 210 smb_close_socket(server);
201 211
202 if (pid == 0) { 212 if (pid == 0) {
203 /* FIXME: this is fatal, umount? */ 213 /* FIXME: this is fatal, umount? */
204 printk(KERN_ERR "smb_retry: no connection process\n"); 214 printk(KERN_ERR "smb_retry: no connection process\n");
205 server->state = CONN_RETRIED; 215 server->state = CONN_RETRIED;
206 goto out; 216 goto out;
207 } 217 }
208 218
209 /* 219 /*
210 * Change state so that only one retry per server will be started. 220 * Change state so that only one retry per server will be started.
211 */ 221 */
212 server->state = CONN_RETRYING; 222 server->state = CONN_RETRYING;
213 223
214 /* 224 /*
215 * Note: use the "priv" flag, as a user process may need to reconnect. 225 * Note: use the "priv" flag, as a user process may need to reconnect.
216 */ 226 */
217 result = kill_proc(pid, SIGUSR1, 1); 227 result = kill_proc(pid, SIGUSR1, 1);
218 if (result) { 228 if (result) {
219 /* FIXME: this is most likely fatal, umount? */ 229 /* FIXME: this is most likely fatal, umount? */
220 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result); 230 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
221 goto out; 231 goto out;
222 } 232 }
223 VERBOSE("signalled pid %d\n", pid); 233 VERBOSE("signalled pid %d\n", pid);
224 234
225 /* FIXME: The retried requests should perhaps get a "time boost". */ 235 /* FIXME: The retried requests should perhaps get a "time boost". */
226 236
227 out: 237 out:
228 return result; 238 return result;
229 } 239 }
230 240
231 /* 241 /*
232 * Currently handles lockingX packets. 242 * Currently handles lockingX packets.
233 */ 243 */
234 static void smbiod_handle_request(struct smb_sb_info *server) 244 static void smbiod_handle_request(struct smb_sb_info *server)
235 { 245 {
236 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n"); 246 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
237 server->rstate = SMB_RECV_DROP; 247 server->rstate = SMB_RECV_DROP;
238 } 248 }
239 249
240 /* 250 /*
241 * Do some IO for one server. 251 * Do some IO for one server.
242 */ 252 */
243 static void smbiod_doio(struct smb_sb_info *server) 253 static void smbiod_doio(struct smb_sb_info *server)
244 { 254 {
245 int result; 255 int result;
246 int maxwork = 7; 256 int maxwork = 7;
247 257
248 if (server->state != CONN_VALID) 258 if (server->state != CONN_VALID)
249 goto out; 259 goto out;
250 260
251 do { 261 do {
252 result = smb_request_recv(server); 262 result = smb_request_recv(server);
253 if (result < 0) { 263 if (result < 0) {
254 server->state = CONN_INVALID; 264 server->state = CONN_INVALID;
255 smbiod_retry(server); 265 smbiod_retry(server);
256 goto out; /* reconnecting is slow */ 266 goto out; /* reconnecting is slow */
257 } else if (server->rstate == SMB_RECV_REQUEST) 267 } else if (server->rstate == SMB_RECV_REQUEST)
258 smbiod_handle_request(server); 268 smbiod_handle_request(server);
259 } while (result > 0 && maxwork-- > 0); 269 } while (result > 0 && maxwork-- > 0);
260 270
261 /* 271 /*
262 * If there is more to read then we want to be sure to wake up again. 272 * If there is more to read then we want to be sure to wake up again.
263 */ 273 */
264 if (server->state != CONN_VALID) 274 if (server->state != CONN_VALID)
265 goto out; 275 goto out;
266 if (smb_recv_available(server) > 0) 276 if (smb_recv_available(server) > 0)
267 set_bit(SMBIOD_DATA_READY, &smbiod_flags); 277 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
268 278
269 do { 279 do {
270 result = smb_request_send_server(server); 280 result = smb_request_send_server(server);
271 if (result < 0) { 281 if (result < 0) {
272 server->state = CONN_INVALID; 282 server->state = CONN_INVALID;
273 smbiod_retry(server); 283 smbiod_retry(server);
274 goto out; /* reconnecting is slow */ 284 goto out; /* reconnecting is slow */
275 } 285 }
276 } while (result > 0); 286 } while (result > 0);
277 287
278 /* 288 /*
279 * If the last request was not sent out we want to wake up again. 289 * If the last request was not sent out we want to wake up again.
280 */ 290 */
281 if (!list_empty(&server->xmitq)) 291 if (!list_empty(&server->xmitq))
282 set_bit(SMBIOD_DATA_READY, &smbiod_flags); 292 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
283 293
284 out: 294 out:
285 return; 295 return;
286 } 296 }
287 297
288 /* 298 /*
289 * smbiod kernel thread 299 * smbiod kernel thread
290 */ 300 */
291 static int smbiod(void *unused) 301 static int smbiod(void *unused)
292 { 302 {
293 daemonize("smbiod");
294
295 allow_signal(SIGKILL); 303 allow_signal(SIGKILL);
296 304
297 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid); 305 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
298 306
299 for (;;) { 307 for (;;) {
300 struct smb_sb_info *server; 308 struct smb_sb_info *server;
301 struct list_head *pos, *n; 309 struct list_head *pos, *n;
302 310
303 /* FIXME: Use poll? */ 311 /* FIXME: Use poll? */
304 wait_event_interruptible(smbiod_wait, 312 wait_event_interruptible(smbiod_wait,
305 test_bit(SMBIOD_DATA_READY, &smbiod_flags)); 313 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
306 if (signal_pending(current)) { 314 if (signal_pending(current)) {
307 spin_lock(&servers_lock); 315 spin_lock(&servers_lock);
308 smbiod_state = SMBIOD_DEAD; 316 smbiod_state = SMBIOD_DEAD;
309 spin_unlock(&servers_lock); 317 spin_unlock(&servers_lock);
310 break; 318 break;
311 } 319 }
312 320
313 clear_bit(SMBIOD_DATA_READY, &smbiod_flags); 321 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
314 322
315 spin_lock(&servers_lock); 323 spin_lock(&servers_lock);
316 if (list_empty(&smb_servers)) { 324 if (list_empty(&smb_servers)) {
317 smbiod_state = SMBIOD_DEAD; 325 smbiod_state = SMBIOD_DEAD;
318 spin_unlock(&servers_lock); 326 spin_unlock(&servers_lock);
319 break; 327 break;
320 } 328 }
321 329
322 list_for_each_safe(pos, n, &smb_servers) { 330 list_for_each_safe(pos, n, &smb_servers) {
323 server = list_entry(pos, struct smb_sb_info, entry); 331 server = list_entry(pos, struct smb_sb_info, entry);
324 VERBOSE("checking server %p\n", server); 332 VERBOSE("checking server %p\n", server);
325 333
326 if (server->state == CONN_VALID) { 334 if (server->state == CONN_VALID) {
327 spin_unlock(&servers_lock); 335 spin_unlock(&servers_lock);
328 336
329 smb_lock_server(server); 337 smb_lock_server(server);
330 smbiod_doio(server); 338 smbiod_doio(server);
331 smb_unlock_server(server); 339 smb_unlock_server(server);
332 340
333 spin_lock(&servers_lock); 341 spin_lock(&servers_lock);
334 } 342 }
335 } 343 }
336 spin_unlock(&servers_lock); 344 spin_unlock(&servers_lock);
337 } 345 }
338 346
339 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid); 347 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
340 module_put_and_exit(0); 348 module_put_and_exit(0);