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