Whamcloud - gitweb
Commit OST AMD support to HEAD so we can being running with a common code base.
[fs/lustre-release.git] / lustre / ptlrpc / service.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24 #ifndef __KERNEL__
25 #include <liblustre.h>
26 #include <linux/kp30.h>
27 #endif
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_net.h>
31 #include <linux/lustre_log.h>
32 #include <portals/types.h>
33 #include "ptlrpc_internal.h"
34
35 static LIST_HEAD (ptlrpc_all_services);
36 static spinlock_t ptlrpc_all_services_lock = SPIN_LOCK_UNLOCKED;
37
38 static void
39 ptlrpc_free_server_req (struct ptlrpc_request *req)
40 {
41         /* The last request to be received into a request buffer uses space
42          * in the request buffer descriptor, otherwise requests are
43          * allocated dynamically in the incoming reply event handler */
44         if (req == &req->rq_rqbd->rqbd_req)
45                 return;
46
47         OBD_FREE(req, sizeof(*req));
48 }
49         
50 static char *
51 ptlrpc_alloc_request_buffer (int size)
52 {
53         char *ptr;
54         
55         if (size > SVC_BUF_VMALLOC_THRESHOLD)
56                 OBD_VMALLOC(ptr, size);
57         else
58                 OBD_ALLOC(ptr, size);
59         
60         return (ptr);
61 }
62
63 static void
64 ptlrpc_free_request_buffer (char *ptr, int size)
65 {
66         if (size > SVC_BUF_VMALLOC_THRESHOLD)
67                 OBD_VFREE(ptr, size);
68         else
69                 OBD_FREE(ptr, size);
70 }
71
72 struct ptlrpc_request_buffer_desc *
73 ptlrpc_alloc_rqbd (struct ptlrpc_srv_ni *srv_ni)
74 {
75         struct ptlrpc_service             *svc = srv_ni->sni_service;
76         unsigned long                      flags;
77         struct ptlrpc_request_buffer_desc *rqbd;
78
79         OBD_ALLOC(rqbd, sizeof (*rqbd));
80         if (rqbd == NULL)
81                 return (NULL);
82
83         rqbd->rqbd_srv_ni = srv_ni;
84         rqbd->rqbd_refcount = 0;
85         rqbd->rqbd_cbid.cbid_fn = request_in_callback;
86         rqbd->rqbd_cbid.cbid_arg = rqbd;
87         rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
88
89         if (rqbd->rqbd_buffer == NULL) {
90                 OBD_FREE(rqbd, sizeof (*rqbd));
91                 return (NULL);
92         }
93
94         spin_lock_irqsave (&svc->srv_lock, flags);
95         list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
96         svc->srv_nbufs++;
97         spin_unlock_irqrestore (&svc->srv_lock, flags);
98
99         return (rqbd);
100 }
101
102 void
103 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd) 
104 {
105         struct ptlrpc_srv_ni  *sni = rqbd->rqbd_srv_ni;
106         struct ptlrpc_service *svc = sni->sni_service;
107         unsigned long          flags;
108         
109         LASSERT (rqbd->rqbd_refcount == 0);
110
111         spin_lock_irqsave(&svc->srv_lock, flags);
112         list_del(&rqbd->rqbd_list);
113         svc->srv_nbufs--;
114         spin_unlock_irqrestore(&svc->srv_lock, flags);
115
116         ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
117         OBD_FREE (rqbd, sizeof (*rqbd));
118 }
119
120 void
121 ptlrpc_save_llog_lock(struct ptlrpc_request *req, struct llog_create_locks *lcl)
122 {
123         struct ptlrpc_reply_state *rs = req->rq_reply_state;
124         LASSERT (rs != NULL);
125         LASSERT (rs->rs_llog_locks == NULL);
126
127         rs->rs_llog_locks = lcl;
128 }
129
130 void
131 ptlrpc_require_repack(struct ptlrpc_request *req)
132 {
133         struct ptlrpc_reply_state *rs = req->rq_reply_state;
134         LASSERT (rs != NULL);
135         rs->rs_difficult = 1;
136 }
137
138 void
139 ptlrpc_save_lock (struct ptlrpc_request *req, 
140                   struct lustre_handle *lock, int mode)
141 {
142         struct ptlrpc_reply_state *rs = req->rq_reply_state;
143         int                        idx;
144
145         if (!lock->cookie)
146                 return;
147
148         LASSERT (rs != NULL);
149         LASSERT (rs->rs_nlocks < RS_MAX_LOCKS);
150
151         idx = rs->rs_nlocks++;
152         rs->rs_locks[idx] = *lock;
153         rs->rs_modes[idx] = mode;
154         rs->rs_difficult = 1;
155 }
156
157 void
158 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
159 {
160         struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
161
162 #ifdef CONFIG_SMP
163         LASSERT (spin_is_locked (&svc->srv_lock));
164 #endif
165         LASSERT (rs->rs_difficult);
166         rs->rs_scheduled_ever = 1;              /* flag any notification attempt */
167
168         if (rs->rs_scheduled)                   /* being set up or already notified */
169                 return;
170
171         rs->rs_scheduled = 1;
172         list_del (&rs->rs_list);
173         list_add (&rs->rs_list, &svc->srv_reply_queue);
174         wake_up (&svc->srv_waitq);
175 }
176
177 void 
178 ptlrpc_commit_replies (struct obd_device *obd)
179 {
180         struct list_head   *tmp;
181         struct list_head   *nxt;
182         unsigned long       flags;
183         
184         /* Find any replies that have been committed and get their service
185          * to attend to complete them. */
186
187         /* CAVEAT EMPTOR: spinlock ordering!!! */
188         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
189
190         list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
191                 struct ptlrpc_reply_state *rs =
192                         list_entry (tmp, struct ptlrpc_reply_state, rs_obd_list);
193                 struct llog_create_locks *lcl = rs->rs_llog_locks;
194
195                 rs->rs_llog_locks = NULL; 
196                 LASSERT (rs->rs_difficult);
197
198                 if (rs->rs_transno <= obd->obd_last_committed) {
199                         struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
200
201                         spin_lock (&svc->srv_lock);
202                         list_del_init (&rs->rs_obd_list);
203                         ptlrpc_schedule_difficult_reply (rs);
204                         spin_unlock (&svc->srv_lock);
205
206                         if (lcl != NULL)
207                                 llog_create_lock_free(lcl);
208                 }
209         }
210         
211         spin_unlock_irqrestore (&obd->obd_uncommitted_replies_lock, flags);
212 }
213
214 static long
215 timeval_sub(struct timeval *large, struct timeval *small)
216 {
217         return (large->tv_sec - small->tv_sec) * 1000000 +
218                 (large->tv_usec - small->tv_usec);
219 }
220
221 static int
222 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
223 {
224         struct ptlrpc_srv_ni              *srv_ni;
225         struct ptlrpc_request_buffer_desc *rqbd;
226         unsigned long                      flags;
227         int                                rc;
228
229         spin_lock_irqsave(&svc->srv_lock, flags);
230         if (list_empty (&svc->srv_idle_rqbds)) {
231                 spin_unlock_irqrestore(&svc->srv_lock, flags);
232                 return (0);
233         }
234
235         rqbd = list_entry(svc->srv_idle_rqbds.next,
236                           struct ptlrpc_request_buffer_desc,
237                           rqbd_list);
238         list_del (&rqbd->rqbd_list);
239
240         /* assume we will post successfully */
241         srv_ni = rqbd->rqbd_srv_ni;
242         srv_ni->sni_nrqbd_receiving++;
243         list_add (&rqbd->rqbd_list, &srv_ni->sni_active_rqbds);
244
245         spin_unlock_irqrestore(&svc->srv_lock, flags);
246
247         rc = ptlrpc_register_rqbd(rqbd);
248         if (rc == 0)
249                 return (1);
250
251         spin_lock_irqsave(&svc->srv_lock, flags);
252
253         srv_ni->sni_nrqbd_receiving--;
254         list_del(&rqbd->rqbd_list);
255         list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
256
257         if (srv_ni->sni_nrqbd_receiving == 0) {
258                 /* This service is off-air on this interface because all
259                  * its request buffers are busy.  Portals will have started
260                  * dropping incoming requests until more buffers get
261                  * posted */
262                 CERROR("All %s %s request buffers busy\n",
263                        svc->srv_name, srv_ni->sni_ni->pni_name);
264         }
265
266         spin_unlock_irqrestore (&svc->srv_lock, flags);
267
268         return (-1);
269 }
270
271 struct ptlrpc_service *
272 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size,
273                 int req_portal, int rep_portal, 
274                 svc_handler_t handler, char *name,
275                 struct proc_dir_entry *proc_entry)
276 {
277         int                                i;
278         int                                j;
279         int                                ssize;
280         struct ptlrpc_service             *service;
281         struct ptlrpc_srv_ni              *srv_ni;
282         struct ptlrpc_request_buffer_desc *rqbd;
283         ENTRY;
284
285         LASSERT (ptlrpc_ninterfaces > 0);
286         LASSERT (nbufs > 0);
287         LASSERT (bufsize >= max_req_size);
288         
289         ssize = offsetof (struct ptlrpc_service,
290                           srv_interfaces[ptlrpc_ninterfaces]);
291         OBD_ALLOC(service, ssize);
292         if (service == NULL)
293                 RETURN(NULL);
294
295         service->srv_name = name;
296         spin_lock_init(&service->srv_lock);
297         INIT_LIST_HEAD(&service->srv_threads);
298         init_waitqueue_head(&service->srv_waitq);
299
300         service->srv_max_req_size = max_req_size;
301         service->srv_buf_size = bufsize;
302         service->srv_rep_portal = rep_portal;
303         service->srv_req_portal = req_portal;
304         service->srv_handler = handler;
305
306         INIT_LIST_HEAD(&service->srv_request_queue);
307         INIT_LIST_HEAD(&service->srv_idle_rqbds);
308         INIT_LIST_HEAD(&service->srv_reply_queue);
309
310         /* First initialise enough for early teardown */
311         for (i = 0; i < ptlrpc_ninterfaces; i++) {
312                 srv_ni = &service->srv_interfaces[i];
313
314                 srv_ni->sni_service = service;
315                 srv_ni->sni_ni = &ptlrpc_interfaces[i];
316                 INIT_LIST_HEAD(&srv_ni->sni_active_rqbds);
317                 INIT_LIST_HEAD(&srv_ni->sni_active_replies);
318         }
319
320         spin_lock (&ptlrpc_all_services_lock);
321         list_add (&service->srv_list, &ptlrpc_all_services);
322         spin_unlock (&ptlrpc_all_services_lock);
323         
324         /* Now allocate the request buffers, assuming all interfaces require
325          * the same number. */
326         for (i = 0; i < ptlrpc_ninterfaces; i++) {
327                 srv_ni = &service->srv_interfaces[i];
328                 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
329                         srv_ni->sni_ni->pni_name);
330
331                 for (j = 0; j < nbufs; j++) {
332                         rqbd = ptlrpc_alloc_rqbd (srv_ni);
333                         
334                         if (rqbd == NULL) {
335                                 CERROR ("%s.%d: Can't allocate request %d "
336                                         "on %s\n", name, i, j, 
337                                         srv_ni->sni_ni->pni_name);
338                                 GOTO(failed, NULL);
339                         }
340
341                         /* We shouldn't be under memory pressure at
342                          * startup, so fail if we can't post all our
343                          * buffers at this time. */
344                         if (ptlrpc_server_post_idle_rqbds(service) <= 0)
345                                 GOTO(failed, NULL);
346                 }
347         }
348
349         if (proc_entry != NULL)
350                 ptlrpc_lprocfs_register_service(proc_entry, service);
351
352         CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
353                service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
354
355         RETURN(service);
356 failed:
357         ptlrpc_unregister_service(service);
358         return NULL;
359 }
360
361 static void
362 ptlrpc_server_free_request(struct ptlrpc_service *svc, struct ptlrpc_request *req)
363 {
364         unsigned long  flags;
365         int            refcount;
366         
367         spin_lock_irqsave(&svc->srv_lock, flags);
368         svc->srv_n_active_reqs--;
369         refcount = --(req->rq_rqbd->rqbd_refcount);
370         if (refcount == 0) {
371                 /* request buffer is now idle */
372                 list_del(&req->rq_rqbd->rqbd_list);
373                 list_add_tail(&req->rq_rqbd->rqbd_list,
374                               &svc->srv_idle_rqbds);
375         }
376         spin_unlock_irqrestore(&svc->srv_lock, flags);
377
378         ptlrpc_free_server_req(req);
379 }
380
381 static int 
382 ptlrpc_server_handle_request (struct ptlrpc_service *svc)
383 {
384         struct obd_export     *export = NULL;
385         struct ptlrpc_request *request;
386         unsigned long          flags;
387         struct timeval         work_start;
388         struct timeval         work_end;
389         long                   timediff;
390         int                    rc;
391         char                   str[PTL_NALFMT_SIZE];
392         ENTRY;
393
394         spin_lock_irqsave (&svc->srv_lock, flags);
395         if (list_empty (&svc->srv_request_queue) ||
396             (svc->srv_n_difficult_replies != 0 &&
397              svc->srv_n_active_reqs >= (svc->srv_nthreads - 1))) {
398                 /* If all the other threads are handling requests, I must
399                  * remain free to handle any 'difficult' reply that might
400                  * block them */
401                 spin_unlock_irqrestore (&svc->srv_lock, flags);
402                 RETURN(0);
403         }
404
405         request = list_entry (svc->srv_request_queue.next,
406                               struct ptlrpc_request, rq_list);
407         list_del_init (&request->rq_list);
408         svc->srv_n_queued_reqs--;
409         svc->srv_n_active_reqs++;
410
411         spin_unlock_irqrestore (&svc->srv_lock, flags);
412
413         do_gettimeofday(&work_start);
414         timediff = timeval_sub(&work_start, &request->rq_arrival_time);
415         if (svc->srv_stats != NULL) {
416                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
417                                     timediff);
418                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
419                                     svc->srv_n_queued_reqs);
420                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
421                                     svc->srv_n_active_reqs);
422         }
423
424 #if SWAB_PARANOIA
425         /* Clear request swab mask; this is a new request */
426         request->rq_req_swab_mask = 0;
427 #endif
428         rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
429         if (rc != 0) {
430                 CERROR ("error unpacking request: ptl %d from %s"
431                         " xid "LPU64"\n", svc->srv_req_portal,
432                         ptlrpc_peernid2str(&request->rq_peer, str),
433                        request->rq_xid);
434                 goto out;
435         }
436
437         rc = -EINVAL;
438         if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
439                 CERROR("wrong packet type received (type=%u) from %s\n",
440                        request->rq_reqmsg->type,
441                        ptlrpc_peernid2str(&request->rq_peer, str));
442                 goto out;
443         }
444
445         CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
446
447         /* Discard requests queued for longer than my timeout.  If the
448          * client's timeout is similar to mine, she'll be timing out this
449          * REQ anyway (bug 1502) */
450         if (timediff / 1000000 > (long)obd_timeout) {
451                 CERROR("Dropping timed-out request from %s: %ld seconds old\n",
452                        ptlrpc_peernid2str(&request->rq_peer, str), 
453                        timediff / 1000000);
454                 goto out;
455         }
456
457         request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
458
459         if (request->rq_export) {
460                 if (request->rq_reqmsg->conn_cnt <
461                     request->rq_export->exp_conn_cnt) {
462                         DEBUG_REQ(D_ERROR, request,
463                                   "DROPPING req from old connection %d < %d",
464                                   request->rq_reqmsg->conn_cnt,
465                                   request->rq_export->exp_conn_cnt);
466                         goto put_conn;
467                 }
468                 
469                 export = class_export_rpc_get(request->rq_export);
470                 request->rq_export->exp_last_request_time =
471                         LTIME_S(CURRENT_TIME);
472         }
473
474         CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
475                "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
476                (request->rq_export ?
477                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
478                (request->rq_export ?
479                 atomic_read(&request->rq_export->exp_refcount) : -99),
480                request->rq_reqmsg->status, request->rq_xid,
481                request->rq_peer.peer_ni->pni_name,
482                ptlrpc_peernid2str(&request->rq_peer, str),
483                request->rq_reqmsg->opc);
484
485         rc = svc->srv_handler(request);
486         CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
487                "%s:%s+%d:%d:"LPU64":%s:%s:%d\n", current->comm,
488                (request->rq_export ?
489                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
490                (request->rq_export ?
491                 atomic_read(&request->rq_export->exp_refcount) : -99),
492                request->rq_reqmsg->status, request->rq_xid,
493                request->rq_peer.peer_ni->pni_name,
494                ptlrpc_peernid2str(&request->rq_peer, str),
495                request->rq_reqmsg->opc);
496
497         if (export != NULL)
498                 class_export_rpc_put(export);
499
500 put_conn:
501         if (request->rq_export != NULL)
502                 class_export_put(request->rq_export);
503
504  out:
505         do_gettimeofday(&work_end);
506
507         timediff = timeval_sub(&work_end, &work_start);
508
509         CDEBUG((timediff / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
510                "request "LPU64" opc %u from NID %s processed in %ldus "
511                "(%ldus total)\n", request->rq_xid, request->rq_reqmsg->opc,
512                ptlrpc_peernid2str(&request->rq_peer, str),
513                timediff, timeval_sub(&work_end, &request->rq_arrival_time));
514
515         if (svc->srv_stats != NULL) {
516                 int opc = opcode_offset(request->rq_reqmsg->opc);
517                 if (opc > 0) {
518                         LASSERT(opc < LUSTRE_MAX_OPCODES);
519                         lprocfs_counter_add(svc->srv_stats,
520                                             opc + PTLRPC_LAST_CNTR,
521                                             timediff);
522                 }
523         }
524
525         ptlrpc_server_free_request(svc, request);
526         
527         RETURN(1);
528 }
529
530 static int
531 ptlrpc_server_handle_reply (struct ptlrpc_service *svc) 
532 {
533         struct ptlrpc_reply_state *rs;
534         unsigned long              flags;
535         struct obd_export         *exp;
536         struct obd_device         *obd;
537         struct llog_create_locks  *lcl;
538         int                        nlocks;
539         int                        been_handled;
540         ENTRY;
541
542         spin_lock_irqsave (&svc->srv_lock, flags);
543         if (list_empty (&svc->srv_reply_queue)) {
544                 spin_unlock_irqrestore (&svc->srv_lock, flags);
545                 RETURN(0);
546         }
547         
548         rs = list_entry (svc->srv_reply_queue.next,
549                          struct ptlrpc_reply_state, rs_list);
550
551         exp = rs->rs_export;
552         obd = exp->exp_obd;
553
554         LASSERT (rs->rs_difficult);
555         LASSERT (rs->rs_scheduled);
556
557         list_del_init (&rs->rs_list);
558
559         /* Disengage from notifiers carefully (lock ordering!) */
560         spin_unlock(&svc->srv_lock);
561
562         spin_lock (&obd->obd_uncommitted_replies_lock);
563         /* Noop if removed already */
564         list_del_init (&rs->rs_obd_list);
565         spin_unlock (&obd->obd_uncommitted_replies_lock);
566
567         spin_lock (&exp->exp_lock);
568         /* Noop if removed already */
569         list_del_init (&rs->rs_exp_list);
570         spin_unlock (&exp->exp_lock);
571
572         spin_lock(&svc->srv_lock);
573
574         been_handled = rs->rs_handled;
575         rs->rs_handled = 1;
576         
577         nlocks = rs->rs_nlocks;                 /* atomic "steal", but */
578         rs->rs_nlocks = 0;                      /* locks still on rs_locks! */
579
580         lcl = rs->rs_llog_locks;
581         rs->rs_llog_locks = NULL;
582
583         if (nlocks == 0 && !been_handled) {
584                 /* If we see this, we should already have seen the warning
585                  * in mds_steal_ack_locks()  */
586 #if 0   
587                 char str[PTL_NALFMT_SIZE];
588                 /* CMD may ask to save request with no DLM locks -bzzz */
589                 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
590                       " o%d NID %s\n",
591                       rs, 
592                       rs->rs_xid, rs->rs_transno,
593                       rs->rs_msg.opc, 
594                       ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
595 #endif
596         }
597
598         if ((!been_handled && rs->rs_on_net) || 
599             nlocks > 0 || lcl != NULL) {
600                 spin_unlock_irqrestore(&svc->srv_lock, flags);
601                 
602                 if (!been_handled && rs->rs_on_net) {
603                         PtlMDUnlink(rs->rs_md_h);
604                         /* Ignore return code; we're racing with
605                          * completion... */
606                 }
607
608                 while (nlocks-- > 0)
609                         ldlm_lock_decref(&rs->rs_locks[nlocks], 
610                                          rs->rs_modes[nlocks]);
611
612                 if (lcl != NULL)
613                         llog_create_lock_free(lcl);         
614
615                 spin_lock_irqsave(&svc->srv_lock, flags);
616         }
617
618         rs->rs_scheduled = 0;
619
620         if (!rs->rs_on_net) {
621                 /* Off the net */
622                 svc->srv_n_difficult_replies--;
623                 spin_unlock_irqrestore(&svc->srv_lock, flags);
624                 
625                 class_export_put (exp);
626                 rs->rs_export = NULL;
627                 lustre_free_reply_state (rs);
628                 atomic_dec (&svc->srv_outstanding_replies);
629                 RETURN(1);
630         }
631         
632         /* still on the net; callback will schedule */
633         spin_unlock_irqrestore (&svc->srv_lock, flags);
634         RETURN(1);
635 }
636
637 #ifndef __KERNEL__
638 /* FIXME make use of timeout later */
639 int
640 liblustre_check_services (void *arg) 
641 {
642         int  did_something = 0;
643         int  rc;
644         struct list_head *tmp, *nxt;
645         ENTRY;
646         
647         /* I'm relying on being single threaded, not to have to lock
648          * ptlrpc_all_services etc */
649         list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
650                 struct ptlrpc_service *svc =
651                         list_entry (tmp, struct ptlrpc_service, srv_list);
652                 
653                 if (svc->srv_nthreads != 0)     /* I've recursed */
654                         continue;
655
656                 /* service threads can block for bulk, so this limits us
657                  * (arbitrarily) to recursing 1 stack frame per service.
658                  * Note that the problem with recursion is that we have to
659                  * unwind completely before our caller can resume. */
660                 
661                 svc->srv_nthreads++;
662                 
663                 do {
664                         rc = ptlrpc_server_handle_reply(svc);
665                         rc |= ptlrpc_server_handle_request(svc);
666                         rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
667                         did_something |= rc;
668                 } while (rc);
669                 
670                 svc->srv_nthreads--;
671         }
672
673         RETURN(did_something);
674 }
675
676 #else /* __KERNEL__ */
677
678 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
679 void ptlrpc_daemonize(void)
680 {
681         exit_mm(current);
682         lustre_daemonize_helper();
683         exit_files(current);
684         reparent_to_init();
685 }
686
687 static int
688 ptlrpc_retry_rqbds(void *arg)
689 {
690         struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
691         
692         svc->srv_rqbd_timeout = 0;
693         return (-ETIMEDOUT);
694 }
695
696 static int ptlrpc_main(void *arg)
697 {
698         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
699         struct ptlrpc_service  *svc = data->svc;
700         struct ptlrpc_thread   *thread = data->thread;
701         unsigned long           flags;
702         ENTRY;
703
704         lock_kernel();
705         ptlrpc_daemonize();
706
707         SIGNAL_MASK_LOCK(current, flags);
708         sigfillset(&current->blocked);
709         RECALC_SIGPENDING;
710         SIGNAL_MASK_UNLOCK(current, flags);
711
712         LASSERTF(strlen(data->name) < sizeof(current->comm),
713                  "name %d > len %d\n",
714                  (int)strlen(data->name), (int)sizeof(current->comm));
715         THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
716         
717         unlock_kernel();
718
719         /* Record that the thread is running */
720         thread->t_flags = SVC_RUNNING;
721         wake_up(&thread->t_ctl_waitq);
722
723         spin_lock_irqsave(&svc->srv_lock, flags);
724         svc->srv_nthreads++;
725         spin_unlock_irqrestore(&svc->srv_lock, flags);
726
727         /* XXX maintain a list of all managed devices: insert here */
728
729         while ((thread->t_flags & SVC_STOPPING) == 0 ||
730                svc->srv_n_difficult_replies != 0) {
731                 /* Don't exit while there are replies to be handled */
732                 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
733                                                      ptlrpc_retry_rqbds, svc);
734
735                 l_wait_event_exclusive (svc->srv_waitq,
736                               ((thread->t_flags & SVC_STOPPING) != 0 &&
737                                svc->srv_n_difficult_replies == 0) ||
738                               (!list_empty(&svc->srv_idle_rqbds) &&
739                                svc->srv_rqbd_timeout == 0) ||
740                               !list_empty (&svc->srv_reply_queue) ||
741                               (!list_empty (&svc->srv_request_queue) &&
742                                (svc->srv_n_difficult_replies == 0 ||
743                                 svc->srv_n_active_reqs <
744                                 (svc->srv_nthreads - 1))),
745                               &lwi);
746
747                 if (!list_empty (&svc->srv_reply_queue))
748                         ptlrpc_server_handle_reply (svc);
749
750                 /* only handle requests if there are no difficult replies
751                  * outstanding, or I'm not the last thread handling
752                  * requests */
753                 if (!list_empty (&svc->srv_request_queue) &&
754                     (svc->srv_n_difficult_replies == 0 ||
755                      svc->srv_n_active_reqs < (svc->srv_nthreads - 1)))
756                         ptlrpc_server_handle_request (svc);
757
758                 if (!list_empty(&svc->srv_idle_rqbds) &&
759                     ptlrpc_server_post_idle_rqbds(svc) < 0) {
760                         /* I just failed to repost request buffers.  Wait
761                          * for a timeout (unless something else happens)
762                          * before I try again */
763                         svc->srv_rqbd_timeout = HZ/10;
764                 }
765         }
766
767         spin_lock_irqsave(&svc->srv_lock, flags);
768
769         svc->srv_nthreads--;                    /* must know immediately */
770         thread->t_flags = SVC_STOPPED;
771         wake_up(&thread->t_ctl_waitq);
772
773         spin_unlock_irqrestore(&svc->srv_lock, flags);
774
775         CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
776         return 0;
777 }
778
779 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
780                                struct ptlrpc_thread *thread)
781 {
782         struct l_wait_info lwi = { 0 };
783         unsigned long      flags;
784
785         spin_lock_irqsave(&svc->srv_lock, flags);
786         thread->t_flags = SVC_STOPPING;
787         spin_unlock_irqrestore(&svc->srv_lock, flags);
788
789         wake_up_all(&svc->srv_waitq);
790         l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
791                      &lwi);
792
793         spin_lock_irqsave(&svc->srv_lock, flags);
794         list_del(&thread->t_link);
795         spin_unlock_irqrestore(&svc->srv_lock, flags);
796
797         OBD_FREE(thread, sizeof(*thread));
798 }
799
800 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
801 {
802         unsigned long flags;
803         struct ptlrpc_thread *thread;
804
805         spin_lock_irqsave(&svc->srv_lock, flags);
806         while (!list_empty(&svc->srv_threads)) {
807                 thread = list_entry(svc->srv_threads.next,
808                                     struct ptlrpc_thread, t_link);
809
810                 spin_unlock_irqrestore(&svc->srv_lock, flags);
811                 ptlrpc_stop_thread(svc, thread);
812                 spin_lock_irqsave(&svc->srv_lock, flags);
813         }
814
815         spin_unlock_irqrestore(&svc->srv_lock, flags);
816 }
817
818 /* @base_name should be 12 characters or less - 3 will be added on */
819 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
820                            int num_threads, char *base_name)
821 {
822         int i, rc = 0;
823         ENTRY;
824
825         for (i = 0; i < num_threads; i++) {
826                 char name[32];
827                 sprintf(name, "%s_%02d", base_name, i);
828                 rc = ptlrpc_start_thread(dev, svc, name);
829                 if (rc) {
830                         CERROR("cannot start %s thread #%d: rc %d\n", base_name,
831                                i, rc);
832                         ptlrpc_stop_all_threads(svc);
833                 }
834         }
835         RETURN(rc);
836 }
837
838 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
839                         char *name)
840 {
841         struct l_wait_info lwi = { 0 };
842         struct ptlrpc_svc_data d;
843         struct ptlrpc_thread *thread;
844         unsigned long flags;
845         int rc;
846         ENTRY;
847
848         OBD_ALLOC(thread, sizeof(*thread));
849         if (thread == NULL)
850                 RETURN(-ENOMEM);
851         init_waitqueue_head(&thread->t_ctl_waitq);
852         
853         d.dev = dev;
854         d.svc = svc;
855         d.name = name;
856         d.thread = thread;
857
858         spin_lock_irqsave(&svc->srv_lock, flags);
859         list_add(&thread->t_link, &svc->srv_threads);
860         spin_unlock_irqrestore(&svc->srv_lock, flags);
861
862         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
863          * just drop the VM and FILES in ptlrpc_daemonize() right away.
864          */
865         rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
866         if (rc < 0) {
867                 CERROR("cannot start thread: %d\n", rc);
868                 OBD_FREE(thread, sizeof(*thread));
869                 RETURN(rc);
870         }
871         l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
872
873         RETURN(0);
874 }
875 #endif
876
877 int ptlrpc_unregister_service(struct ptlrpc_service *service)
878 {
879         int                   i;
880         int                   rc;
881         unsigned long         flags;
882         struct ptlrpc_srv_ni *srv_ni;
883         struct l_wait_info    lwi;
884         struct list_head     *tmp;
885
886         LASSERT(list_empty(&service->srv_threads));
887
888         spin_lock (&ptlrpc_all_services_lock);
889         list_del_init (&service->srv_list);
890         spin_unlock (&ptlrpc_all_services_lock);
891
892         ptlrpc_lprocfs_unregister_service(service);
893
894         for (i = 0; i < ptlrpc_ninterfaces; i++) {
895                 srv_ni = &service->srv_interfaces[i];
896                 CDEBUG(D_NET, "%s: tearing down interface %s\n",
897                        service->srv_name, srv_ni->sni_ni->pni_name);
898
899                 /* Unlink all the request buffers.  This forces a 'final'
900                  * event with its 'unlink' flag set for each posted rqbd */
901                 list_for_each(tmp, &srv_ni->sni_active_rqbds) {
902                         struct ptlrpc_request_buffer_desc *rqbd =
903                                 list_entry(tmp, struct ptlrpc_request_buffer_desc, 
904                                            rqbd_list);
905
906                         rc = PtlMDUnlink(rqbd->rqbd_md_h);
907                         LASSERT (rc == PTL_OK || rc == PTL_MD_INVALID);
908                 }
909
910                 /* Wait for the network to release any buffers it's
911                  * currently filling */
912                 for (;;) {
913                         spin_lock_irqsave(&service->srv_lock, flags);
914                         rc = srv_ni->sni_nrqbd_receiving;
915                         spin_unlock_irqrestore(&service->srv_lock, flags);
916
917                         if (rc == 0)
918                                 break;
919                         
920                         /* Network access will complete in finite time but
921                          * the HUGE timeout lets us CWARN for visibility of
922                          * sluggish NALs */
923                         lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
924                         rc = l_wait_event(service->srv_waitq,
925                                           srv_ni->sni_nrqbd_receiving == 0,
926                                           &lwi);
927                         if (rc == -ETIMEDOUT)
928                                 CWARN("Waiting for request buffers on "
929                                       "service %s on interface %s ",
930                                       service->srv_name, srv_ni->sni_ni->pni_name);
931                 }
932
933                 /* schedule all outstanding replies to terminate them */
934                 spin_lock_irqsave(&service->srv_lock, flags);
935                 while (!list_empty(&srv_ni->sni_active_replies)) {
936                         struct ptlrpc_reply_state *rs =
937                                 list_entry(srv_ni->sni_active_replies.next,
938                                            struct ptlrpc_reply_state,
939                                            rs_list);
940                         ptlrpc_schedule_difficult_reply(rs);
941                 }
942                 spin_unlock_irqrestore(&service->srv_lock, flags);
943         }
944
945         /* purge the request queue.  NB No new replies (rqbds all unlinked)
946          * and no service threads, so I'm the only thread noodling the
947          * request queue now */
948         while (!list_empty(&service->srv_request_queue)) {
949                 struct ptlrpc_request *req =
950                         list_entry(service->srv_request_queue.next,
951                                    struct ptlrpc_request,
952                                    rq_list);
953                 
954                 list_del(&req->rq_list);
955                 service->srv_n_queued_reqs--;
956                 service->srv_n_active_reqs++;
957
958                 ptlrpc_server_free_request(service, req);
959         }
960         LASSERT(service->srv_n_queued_reqs == 0);
961         LASSERT(service->srv_n_active_reqs == 0);
962
963         for (i = 0; i < ptlrpc_ninterfaces; i++) {
964                 srv_ni = &service->srv_interfaces[i];
965                 LASSERT(list_empty(&srv_ni->sni_active_rqbds));
966         }
967
968         /* Now free all the request buffers since nothing references them
969          * any more... */
970         while (!list_empty(&service->srv_idle_rqbds)) {
971                 struct ptlrpc_request_buffer_desc *rqbd =
972                         list_entry(service->srv_idle_rqbds.next,
973                                    struct ptlrpc_request_buffer_desc, 
974                                    rqbd_list);
975
976                 ptlrpc_free_rqbd(rqbd);
977         }
978
979         /* wait for all outstanding replies to complete (they were
980          * scheduled having been flagged to abort above) */
981         while (atomic_read(&service->srv_outstanding_replies) != 0) {
982                 struct l_wait_info lwi = LWI_TIMEOUT(10 * HZ, NULL, NULL);
983
984                 rc = l_wait_event(service->srv_waitq,
985                                   !list_empty(&service->srv_reply_queue), &lwi);
986                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
987
988                 if (rc == 0) {
989                         ptlrpc_server_handle_reply(service);
990                         continue;
991                 }
992                 CWARN("Unexpectedly long timeout %p\n", service);
993         }
994
995         OBD_FREE(service,
996                  offsetof(struct ptlrpc_service,
997                           srv_interfaces[ptlrpc_ninterfaces]));
998         return 0;
999 }