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