Whamcloud - gitweb
Land b1_6_elc onto b1_6 (20070621_0218)
[fs/lustre-release.git] / lustre / ost / ost_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  *  Storage Target Handling functions
27  *  Lustre Object Server Module (OST)
28  *
29  *  This server is single threaded at present (but can easily be multi
30  *  threaded). For testing and management it is treated as an
31  *  obd_device, although it does not export a full OBD method table
32  *  (the requests are coming in over the wire, so object target
33  *  modules do not have a full method table.)
34  */
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_OST
40
41 #include <linux/module.h>
42 #include <obd_ost.h>
43 #include <lustre_net.h>
44 #include <lustre_dlm.h>
45 #include <lustre_export.h>
46 #include <lustre_debug.h>
47 #include <linux/init.h>
48 #include <lprocfs_status.h>
49 #include <lustre_commit_confd.h>
50 #include <libcfs/list.h>
51 #include <lustre_quota.h>
52 #include "ost_internal.h"
53
54 static int oss_num_threads;
55 CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
56                 "number of OSS service threads to start");
57
58 static int ost_num_threads;
59 CFS_MODULE_PARM(ost_num_threads, "i", int, 0444,
60                 "number of OST service threads to start (deprecated)");
61
62 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
63 {
64         struct oti_req_ack_lock *ack_lock;
65         int i;
66
67         if (oti == NULL)
68                 return;
69
70         if (req->rq_repmsg)
71                 lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
72         req->rq_transno = oti->oti_transno;
73
74         /* XXX 4 == entries in oti_ack_locks??? */
75         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
76                 if (!ack_lock->mode)
77                         break;
78                 /* XXX not even calling target_send_reply in some cases... */
79                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
80         }
81 }
82
83 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
84                        struct obd_trans_info *oti)
85 {
86         struct ost_body *body, *repbody;
87         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
88         ENTRY;
89
90         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
91                                   lustre_swab_ost_body);
92         if (body == NULL)
93                 RETURN(-EFAULT);
94
95         if (lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1)) {
96                 struct ldlm_request *dlm;
97                 dlm = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*dlm),
98                                          lustre_swab_ldlm_request);
99                 if (dlm == NULL)
100                         RETURN (-EFAULT);
101                 ldlm_request_cancel(req, dlm, 0);
102         }
103
104         rc = lustre_pack_reply(req, 2, size, NULL);
105         if (rc)
106                 RETURN(rc);
107
108         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
109                 oti->oti_logcookies = obdo_logcookie(&body->oa);
110         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
111                                  sizeof(*repbody));
112         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
113         req->rq_status = obd_destroy(exp, &body->oa, NULL, oti, NULL);
114         RETURN(0);
115 }
116
117 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
118 {
119         struct ost_body *body, *repbody;
120         struct obd_info oinfo = { { { 0 } } };
121         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
122         ENTRY;
123
124         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
125                                   lustre_swab_ost_body);
126         if (body == NULL)
127                 RETURN(-EFAULT);
128
129         rc = lustre_pack_reply(req, 2, size, NULL);
130         if (rc)
131                 RETURN(rc);
132
133         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
134                                  sizeof(*repbody));
135         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
136
137         oinfo.oi_oa = &repbody->oa;
138         req->rq_status = obd_getattr(exp, &oinfo);
139         RETURN(0);
140 }
141
142 static int ost_statfs(struct ptlrpc_request *req)
143 {
144         struct obd_statfs *osfs;
145         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*osfs) };
146         ENTRY;
147
148         rc = lustre_pack_reply(req, 2, size, NULL);
149         if (rc)
150                 RETURN(rc);
151
152         osfs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*osfs));
153
154         req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs, 
155                                     cfs_time_current_64() - HZ);
156         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
157                 osfs->os_bfree = osfs->os_bavail = 64;
158         if (req->rq_status != 0)
159                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
160
161         RETURN(0);
162 }
163
164 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
165                       struct obd_trans_info *oti)
166 {
167         struct ost_body *body, *repbody;
168         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
169         ENTRY;
170
171         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
172                                   lustre_swab_ost_body);
173         if (body == NULL)
174                 RETURN(-EFAULT);
175
176         rc = lustre_pack_reply(req, 2, size, NULL);
177         if (rc)
178                 RETURN(rc);
179
180         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
181                                  sizeof(*repbody));
182         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
183         oti->oti_logcookies = obdo_logcookie(&repbody->oa);
184         req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
185         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
186         RETURN(0);
187 }
188
189 /*
190  * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
191  * lock on the file being truncated.
192  */
193 static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
194                               struct lustre_handle *lh)
195 {
196         int flags;
197         struct ldlm_res_id res_id = { .name = { oa->o_id } };
198         ldlm_policy_data_t policy;
199         __u64 start;
200         __u64 finis;
201
202         ENTRY;
203
204         LASSERT(!lustre_handle_is_used(lh));
205
206         if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
207             !(oa->o_flags & OBD_FL_TRUNCLOCK))
208                 RETURN(0);
209
210         CDEBUG(D_INODE, "OST-side truncate lock.\n");
211
212         start = oa->o_size;
213         finis = start + oa->o_blocks;
214
215         /*
216          * standard truncate optimization: if file body is completely
217          * destroyed, don't send data back to the server.
218          */
219         flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
220
221         policy.l_extent.start = start & CFS_PAGE_MASK;
222
223         /*
224          * If ->o_blocks is EOF it means "lock till the end of the
225          * file". Otherwise, it's size of a hole being punched (in bytes)
226          */
227         if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
228                 policy.l_extent.end = OBD_OBJECT_EOF;
229         else
230                 policy.l_extent.end = finis | ~CFS_PAGE_MASK;
231
232         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, res_id, 
233                                       LDLM_EXTENT, &policy, LCK_PW, &flags,
234                                       ldlm_blocking_ast, ldlm_completion_ast,
235                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
236 }
237
238 /*
239  * Helper function for ost_punch(): release lock acquired by
240  * ost_punch_lock_get(), if any.
241  */
242 static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
243                                struct lustre_handle *lh)
244 {
245         ENTRY;
246         if (lustre_handle_is_used(lh))
247                 ldlm_lock_decref(lh, LCK_PW);
248         EXIT;
249 }
250
251 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
252                      struct obd_trans_info *oti)
253 {
254         struct obd_info oinfo = { { { 0 } } };
255         struct ost_body *body, *repbody;
256         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
257         struct lustre_handle lh = {0,};
258         ENTRY;
259
260         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
261         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
262
263         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
264                                   lustre_swab_ost_body);
265         if (body == NULL)
266                 RETURN(-EFAULT);
267
268         oinfo.oi_oa = &body->oa;
269         oinfo.oi_policy.l_extent.start = oinfo.oi_oa->o_size;
270         oinfo.oi_policy.l_extent.end = oinfo.oi_oa->o_blocks;
271
272         if ((oinfo.oi_oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
273             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
274                 RETURN(-EINVAL);
275
276         rc = lustre_pack_reply(req, 2, size, NULL);
277         if (rc)
278                 RETURN(rc);
279
280         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
281                                  sizeof(*repbody));
282         rc = ost_punch_lock_get(exp, oinfo.oi_oa, &lh);
283         if (rc == 0) {
284                 if (oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
285                     oinfo.oi_oa->o_flags == OBD_FL_TRUNCLOCK)
286                         /*
287                          * If OBD_FL_TRUNCLOCK is the only bit set in
288                          * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
289                          * through filter_setattr() to filter_iocontrol().
290                          */
291                         oinfo.oi_oa->o_valid &= ~OBD_MD_FLFLAGS;
292
293                 req->rq_status = obd_punch(exp, &oinfo, oti, NULL);
294                 ost_punch_lock_put(exp, oinfo.oi_oa, &lh);
295         }
296         repbody->oa = *oinfo.oi_oa;
297         RETURN(rc);
298 }
299
300 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
301 {
302         struct ost_body *body, *repbody;
303         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
304         ENTRY;
305
306         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
307                                   lustre_swab_ost_body);
308         if (body == NULL)
309                 RETURN(-EFAULT);
310
311         rc = lustre_pack_reply(req, 2, size, NULL);
312         if (rc)
313                 RETURN(rc);
314
315         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
316                                  sizeof(*repbody));
317         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
318         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
319                                   repbody->oa.o_blocks);
320         RETURN(0);
321 }
322
323 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
324                        struct obd_trans_info *oti)
325 {
326         struct ost_body *body, *repbody;
327         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
328         struct obd_info oinfo = { { { 0 } } };
329         ENTRY;
330
331         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
332                                   lustre_swab_ost_body);
333         if (body == NULL)
334                 RETURN(-EFAULT);
335
336         rc = lustre_pack_reply(req, 2, size, NULL);
337         if (rc)
338                 RETURN(rc);
339
340         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
341                                  sizeof(*repbody));
342         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
343
344         oinfo.oi_oa = &repbody->oa;
345         req->rq_status = obd_setattr(exp, &oinfo, oti);
346         RETURN(0);
347 }
348
349 static int ost_bulk_timeout(void *data)
350 {
351         ENTRY;
352         /* We don't fail the connection here, because having the export
353          * killed makes the (vital) call to commitrw very sad.
354          */
355         RETURN(1);
356 }
357
358 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
359                                 struct niobuf_remote *rnb, int nrnb,
360                                 struct niobuf_remote **pp_rnbp)
361 {
362         /* Copy a remote niobuf, splitting it into page-sized chunks
363          * and setting ioo[i].ioo_bufcnt accordingly */
364         struct niobuf_remote *pp_rnb;
365         int   i;
366         int   j;
367         int   page;
368         int   rnbidx = 0;
369         int   npages = 0;
370
371         /*
372          * array of sufficient size already preallocated by caller
373          */
374         LASSERT(pp_rnbp != NULL);
375         LASSERT(*pp_rnbp != NULL);
376
377         /* first count and check the number of pages required */
378         for (i = 0; i < nioo; i++)
379                 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
380                         obd_off offset = rnb[rnbidx].offset;
381                         obd_off p0 = offset >> CFS_PAGE_SHIFT;
382                         obd_off pn = (offset + rnb[rnbidx].len - 1)>>CFS_PAGE_SHIFT;
383
384                         LASSERT(rnbidx < nrnb);
385
386                         npages += (pn + 1 - p0);
387
388                         if (rnb[rnbidx].len == 0) {
389                                 CERROR("zero len BRW: obj %d objid "LPX64
390                                        " buf %u\n", i, ioo[i].ioo_id, j);
391                                 return -EINVAL;
392                         }
393                         if (j > 0 &&
394                             rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
395                                 CERROR("unordered BRW: obj %d objid "LPX64
396                                        " buf %u offset "LPX64" <= "LPX64"\n",
397                                        i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
398                                        rnb[rnbidx].offset);
399                                 return -EINVAL;
400                         }
401                 }
402
403         LASSERT(rnbidx == nrnb);
404
405         if (npages == nrnb) {       /* all niobufs are for single pages */
406                 *pp_rnbp = rnb;
407                 return npages;
408         }
409
410         pp_rnb = *pp_rnbp;
411
412         /* now do the actual split */
413         page = rnbidx = 0;
414         for (i = 0; i < nioo; i++) {
415                 int  obj_pages = 0;
416
417                 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
418                         obd_off off = rnb[rnbidx].offset;
419                         int     nob = rnb[rnbidx].len;
420
421                         LASSERT(rnbidx < nrnb);
422                         do {
423                                 obd_off  poff = off & ~CFS_PAGE_MASK;
424                                 int      pnob = (poff + nob > CFS_PAGE_SIZE) ?
425                                                 CFS_PAGE_SIZE - poff : nob;
426
427                                 LASSERT(page < npages);
428                                 pp_rnb[page].len = pnob;
429                                 pp_rnb[page].offset = off;
430                                 pp_rnb[page].flags = rnb[rnbidx].flags;
431
432                                 CDEBUG(0, "   obj %d id "LPX64
433                                        "page %d(%d) "LPX64" for %d, flg %x\n",
434                                        i, ioo[i].ioo_id, obj_pages, page,
435                                        pp_rnb[page].offset, pp_rnb[page].len,
436                                        pp_rnb[page].flags);
437                                 page++;
438                                 obj_pages++;
439
440                                 off += pnob;
441                                 nob -= pnob;
442                         } while (nob > 0);
443                         LASSERT(nob == 0);
444                 }
445                 ioo[i].ioo_bufcnt = obj_pages;
446         }
447         LASSERT(page == npages);
448
449         return npages;
450 }
451
452 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc)
453 {
454         __u32 cksum = ~0;
455         int i;
456
457         for (i = 0; i < desc->bd_iov_count; i++) {
458                 struct page *page = desc->bd_iov[i].kiov_page;
459                 int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
460                 char *ptr = kmap(page) + off;
461                 int len = desc->bd_iov[i].kiov_len;
462
463                 /* corrupt the data before we compute the checksum, to
464                  * simulate a client->OST data error */
465                 if (i == 0 &&OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_CHECKSUM_RECEIVE))
466                         memcpy(ptr, "bad3", min(4, len));
467                 cksum = crc32_le(cksum, ptr, len);
468                 /* corrupt the data after we compute the checksum, to
469                  * simulate an OST->client data error */
470                 if (i == 0 && OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_CHECKSUM_SEND))
471                         memcpy(ptr, "bad4", min(4, len));
472                 kunmap(page);
473         }
474
475         return cksum;
476 }
477
478 /*
479  * populate @nio by @nrpages pages from per-thread page pool
480  */
481 static void ost_nio_pages_get(struct ptlrpc_request *req,
482                               struct niobuf_local *nio, int nrpages)
483 {
484         int i;
485         struct ost_thread_local_cache *tls;
486
487         ENTRY;
488
489         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
490         LASSERT(req != NULL);
491         LASSERT(req->rq_svc_thread != NULL);
492
493         tls = ost_tls(req);
494         LASSERT(tls != NULL);
495
496         memset(nio, 0, nrpages * sizeof *nio);
497         for (i = 0; i < nrpages; ++ i) {
498                 struct page *page;
499
500                 page = tls->page[i];
501                 LASSERT(page != NULL);
502                 POISON_PAGE(page, 0xf1);
503                 nio[i].page = page;
504                 LL_CDEBUG_PAGE(D_INFO, page, "%d\n", i);
505         }
506         EXIT;
507 }
508
509 /*
510  * Dual for ost_nio_pages_get(). Poison pages in pool for debugging
511  */
512 static void ost_nio_pages_put(struct ptlrpc_request *req,
513                               struct niobuf_local *nio, int nrpages)
514 {
515         int i;
516
517         ENTRY;
518
519         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
520
521         for (i = 0; i < nrpages; ++ i)
522                 POISON_PAGE(nio[i].page, 0xf2);
523         EXIT;
524 }
525
526 static int ost_brw_lock_get(int mode, struct obd_export *exp,
527                             struct obd_ioobj *obj, struct niobuf_remote *nb,
528                             struct lustre_handle *lh)
529 {
530         int flags                 = 0;
531         int nrbufs                = obj->ioo_bufcnt;
532         struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
533         ldlm_policy_data_t policy;
534         int i;
535
536         ENTRY;
537
538         LASSERT(mode == LCK_PR || mode == LCK_PW);
539         LASSERT(!lustre_handle_is_used(lh));
540
541         if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
542                 RETURN(0);
543
544         /* EXPENSIVE ASSERTION */
545         for (i = 1; i < nrbufs; i ++)
546                 LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
547                         (nb[i].flags & OBD_BRW_SRVLOCK));
548
549         policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
550         policy.l_extent.end   = (nb[nrbufs - 1].offset +
551                                  nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
552
553         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, res_id, 
554                                       LDLM_EXTENT, &policy, mode, &flags,
555                                       ldlm_blocking_ast, ldlm_completion_ast,
556                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
557 }
558
559 static void ost_brw_lock_put(int mode,
560                              struct obd_ioobj *obj, struct niobuf_remote *niob,
561                              struct lustre_handle *lh)
562 {
563         ENTRY;
564         LASSERT(mode == LCK_PR || mode == LCK_PW);
565         LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
566                 lustre_handle_is_used(lh));
567         if (lustre_handle_is_used(lh))
568                 ldlm_lock_decref(lh, mode);
569         EXIT;
570 }
571
572 struct ost_prolong_data {
573         struct obd_export *opd_exp;
574         ldlm_policy_data_t opd_policy;
575         ldlm_mode_t opd_mode;
576 };
577
578 static int ost_prolong_locks_iter(struct ldlm_lock *lock, void *data)
579 {
580         struct ost_prolong_data *opd = data;
581
582         LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
583
584         if (lock->l_req_mode != lock->l_granted_mode) {
585                 /* scan granted locks only */
586                 return LDLM_ITER_STOP;
587         }
588
589         if (lock->l_export != opd->opd_exp) {
590                 /* prolong locks only for given client */
591                 return LDLM_ITER_CONTINUE;
592         }
593
594         if (!(lock->l_granted_mode & opd->opd_mode)) {
595                 /* we aren't interesting in all type of locks */
596                 return LDLM_ITER_CONTINUE;
597         }
598
599         if (lock->l_policy_data.l_extent.end < opd->opd_policy.l_extent.start ||
600             lock->l_policy_data.l_extent.start > opd->opd_policy.l_extent.end) {
601                 /* the request doesn't cross the lock, skip it */
602                 return LDLM_ITER_CONTINUE;
603         }
604
605         if (!(lock->l_flags & LDLM_FL_AST_SENT)) {
606                 /* ignore locks not being cancelled */
607                 return LDLM_ITER_CONTINUE;
608         }
609
610         /* OK. this is a possible lock the user holds doing I/O
611          * let's refresh eviction timer for it */
612         ldlm_refresh_waiting_lock(lock);
613
614         return LDLM_ITER_CONTINUE;
615 }
616
617 static void ost_prolong_locks(struct obd_export *exp, struct obd_ioobj *obj,
618                               struct niobuf_remote *nb, ldlm_mode_t mode)
619 {
620         struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
621         int nrbufs = obj->ioo_bufcnt;
622         struct ost_prolong_data opd;
623
624         ENTRY;
625
626         opd.opd_mode = mode;
627         opd.opd_exp = exp;
628         opd.opd_policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
629         opd.opd_policy.l_extent.end = (nb[nrbufs - 1].offset +
630                                        nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
631
632         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
633                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
634                opd.opd_policy.l_extent.end);
635         ldlm_resource_iterate(exp->exp_obd->obd_namespace, &res_id,
636                               ost_prolong_locks_iter, &opd);
637 }
638
639 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
640 {
641         struct ptlrpc_bulk_desc *desc;
642         struct niobuf_remote *remote_nb;
643         struct niobuf_remote *pp_rnb = NULL;
644         struct niobuf_local *local_nb;
645         struct obd_ioobj *ioo;
646         struct ost_body *body, *repbody;
647         struct l_wait_info lwi;
648         struct lustre_handle lockh = { 0 };
649         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
650         int comms_error = 0, niocount, npages, nob = 0, rc, i;
651         int no_reply = 0;
652         ENTRY;
653
654         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
655                 GOTO(out, rc = -EIO);
656
657         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
658                          (obd_timeout + 1) / 4);
659
660         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
661                                   lustre_swab_ost_body);
662         if (body == NULL) {
663                 CERROR("Missing/short ost_body\n");
664                 GOTO(out, rc = -EFAULT);
665         }
666
667         ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*ioo),
668                                  lustre_swab_obd_ioobj);
669         if (ioo == NULL) {
670                 CERROR("Missing/short ioobj\n");
671                 GOTO(out, rc = -EFAULT);
672         }
673
674         niocount = ioo->ioo_bufcnt;
675         if (niocount > PTLRPC_MAX_BRW_PAGES) {
676                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
677                           niocount);
678                 GOTO(out, rc = -EFAULT);
679         }
680
681         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
682                                        niocount * sizeof(*remote_nb),
683                                        lustre_swab_niobuf_remote);
684         if (remote_nb == NULL) {
685                 CERROR("Missing/short niobuf\n");
686                 GOTO(out, rc = -EFAULT);
687         }
688         if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
689                 for (i = 1; i < niocount; i++)
690                         lustre_swab_niobuf_remote (&remote_nb[i]);
691         }
692
693         rc = lustre_pack_reply(req, 2, size, NULL);
694         if (rc)
695                 GOTO(out, rc);
696
697         /*
698          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
699          * ost_thread_init().
700          */
701         local_nb = ost_tls(req)->local;
702         pp_rnb   = ost_tls(req)->remote;
703
704         /* FIXME all niobuf splitting should be done in obdfilter if needed */
705         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
706         npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
707         if (npages < 0)
708                 GOTO(out, rc = npages);
709
710         LASSERT(npages <= OST_THREAD_POOL_SIZE);
711
712         ost_nio_pages_get(req, local_nb, npages);
713
714         desc = ptlrpc_prep_bulk_exp(req, npages,
715                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
716         if (desc == NULL)
717                 GOTO(out, rc = -ENOMEM);
718
719         rc = ost_brw_lock_get(LCK_PR, req->rq_export, ioo, pp_rnb, &lockh);
720         if (rc != 0)
721                 GOTO(out_bulk, rc);
722
723         /* 
724          * If getting the lock took more time than
725          * client was willing to wait, drop it. b=11330
726          */
727         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
728             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
729                 no_reply = 1;
730                 CERROR("Dropping timed-out read from %s because locking"
731                        "object "LPX64" took %ld seconds.\n",
732                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
733                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
734                 goto out_lock;
735         }
736
737         rc = obd_preprw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
738                         ioo, npages, pp_rnb, local_nb, oti);
739         if (rc != 0)
740                 GOTO(out_lock, rc);
741
742         ost_prolong_locks(req->rq_export, ioo, pp_rnb, LCK_PW | LCK_PR);
743
744         nob = 0;
745         for (i = 0; i < npages; i++) {
746                 int page_rc = local_nb[i].rc;
747
748                 if (page_rc < 0) {              /* error */
749                         rc = page_rc;
750                         break;
751                 }
752
753                 LASSERTF(page_rc <= pp_rnb[i].len, "page_rc (%d) > "
754                          "pp_rnb[%d].len (%d)\n", page_rc, i, pp_rnb[i].len);
755                 nob += page_rc;
756                 if (page_rc != 0) {             /* some data! */
757                         LASSERT (local_nb[i].page != NULL);
758                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
759                                               pp_rnb[i].offset & ~CFS_PAGE_MASK,
760                                               page_rc);
761                 }
762
763                 if (page_rc != pp_rnb[i].len) { /* short read */
764                         /* All subsequent pages should be 0 */
765                         while(++i < npages)
766                                 LASSERT(local_nb[i].rc == 0);
767                         break;
768                 }
769         }
770
771         if (unlikely(body->oa.o_valid & OBD_MD_FLCKSUM)) {
772                 body->oa.o_cksum = ost_checksum_bulk(desc);
773                 body->oa.o_valid = OBD_MD_FLCKSUM;
774                 CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
775         } else {
776                 body->oa.o_valid = 0;
777         }
778         /* We're finishing using body->oa as an input variable */
779
780         /* Check if client was evicted while we were doing i/o before touching
781            network */
782         if (rc == 0) {
783                 if (desc->bd_export->exp_failed)
784                         rc = -ENOTCONN;
785                 else
786                         rc = ptlrpc_start_bulk_transfer(desc);
787                 if (rc == 0) {
788                         lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
789                                                    ost_bulk_timeout, desc);
790                         rc = l_wait_event(desc->bd_waitq,
791                                           !ptlrpc_bulk_active(desc) ||
792                                           desc->bd_export->exp_failed, &lwi);
793                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
794                         if (rc == -ETIMEDOUT) {
795                                 DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT");
796                                 ptlrpc_abort_bulk(desc);
797                         } else if (desc->bd_export->exp_failed) {
798                                 DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
799                                 rc = -ENOTCONN;
800                                 ptlrpc_abort_bulk(desc);
801                         } else if (!desc->bd_success ||
802                                    desc->bd_nob_transferred != desc->bd_nob) {
803                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
804                                           desc->bd_success ?
805                                           "truncated" : "network error on",
806                                           desc->bd_nob_transferred,
807                                           desc->bd_nob);
808                                 /* XXX should this be a different errno? */
809                                 rc = -ETIMEDOUT;
810                         }
811                 } else {
812                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d\n", rc);
813                 }
814                 comms_error = rc != 0;
815         }
816
817         /* Must commit after prep above in all cases */
818         rc = obd_commitrw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
819                           ioo, npages, local_nb, oti, rc);
820
821         ost_nio_pages_put(req, local_nb, npages);
822
823         if (rc == 0) {
824                 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
825                                          sizeof(*repbody));
826                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
827         }
828
829  out_lock:
830         ost_brw_lock_put(LCK_PR, ioo, pp_rnb, &lockh);
831  out_bulk:
832         ptlrpc_free_bulk(desc);
833         if (no_reply)
834                 RETURN(rc);
835  out:
836         LASSERT(rc <= 0);
837         if (rc == 0) {
838                 req->rq_status = nob;
839                 target_committed_to_req(req);
840                 ptlrpc_reply(req);
841         } else if (!comms_error) {
842                 /* Only reply if there was no comms problem with bulk */
843                 target_committed_to_req(req);
844                 req->rq_status = rc;
845                 ptlrpc_error(req);
846         } else {
847                 if (req->rq_reply_state != NULL) {
848                         /* reply out callback would free */
849                         ptlrpc_rs_decref(req->rq_reply_state);
850                         req->rq_reply_state = NULL;
851                 }
852                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
853                       "client will retry\n",
854                       req->rq_export->exp_obd->obd_name,
855                       req->rq_export->exp_client_uuid.uuid,
856                       req->rq_export->exp_connection->c_remote_uuid.uuid,
857                       libcfs_id2str(req->rq_peer));
858         }
859
860         RETURN(rc);
861 }
862
863 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
864 {
865         struct ptlrpc_bulk_desc *desc;
866         struct niobuf_remote    *remote_nb;
867         struct niobuf_remote    *pp_rnb;
868         struct niobuf_local     *local_nb;
869         struct obd_ioobj        *ioo;
870         struct ost_body         *body, *repbody;
871         struct l_wait_info       lwi;
872         struct lustre_handle     lockh = {0};
873         __u32                   *rcs;
874         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
875         int objcount, niocount, npages, comms_error = 0;
876         int rc, swab, i, j;
877         obd_count                client_cksum, server_cksum = 0;
878         int                      no_reply = 0; 
879         ENTRY;
880
881         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
882                 GOTO(out, rc = -EIO);
883
884         /* pause before transaction has been started */
885         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
886                          (obd_timeout + 1) / 4);
887
888         swab = lustre_msg_swabbed(req->rq_reqmsg);
889         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
890                                   lustre_swab_ost_body);
891         if (body == NULL) {
892                 CERROR("Missing/short ost_body\n");
893                 GOTO(out, rc = -EFAULT);
894         }
895
896         LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
897         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
898                    sizeof(*ioo);
899         if (objcount == 0) {
900                 CERROR("Missing/short ioobj\n");
901                 GOTO(out, rc = -EFAULT);
902         }
903         if (objcount > 1) {
904                 CERROR("too many ioobjs (%d)\n", objcount);
905                 GOTO(out, rc = -EFAULT);
906         }
907
908         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
909                              objcount * sizeof(*ioo));
910         LASSERT (ioo != NULL);
911         for (niocount = i = 0; i < objcount; i++) {
912                 if (swab)
913                         lustre_swab_obd_ioobj(&ioo[i]);
914                 if (ioo[i].ioo_bufcnt == 0) {
915                         CERROR("ioo[%d] has zero bufcnt\n", i);
916                         GOTO(out, rc = -EFAULT);
917                 }
918                 niocount += ioo[i].ioo_bufcnt;
919         }
920
921         if (niocount > PTLRPC_MAX_BRW_PAGES) {
922                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
923                           niocount);
924                 GOTO(out, rc = -EFAULT);
925         }
926
927         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
928                                        niocount * sizeof(*remote_nb),
929                                        lustre_swab_niobuf_remote);
930         if (remote_nb == NULL) {
931                 CERROR("Missing/short niobuf\n");
932                 GOTO(out, rc = -EFAULT);
933         }
934         if (swab) {                             /* swab the remaining niobufs */
935                 for (i = 1; i < niocount; i++)
936                         lustre_swab_niobuf_remote (&remote_nb[i]);
937         }
938
939         size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
940         rc = lustre_pack_reply(req, 3, size, NULL);
941         if (rc != 0)
942                 GOTO(out, rc);
943         rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
944                              niocount * sizeof(*rcs));
945
946         /*
947          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
948          * ost_thread_init().
949          */
950         local_nb = ost_tls(req)->local;
951         pp_rnb   = ost_tls(req)->remote;
952
953         /* FIXME all niobuf splitting should be done in obdfilter if needed */
954         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
955         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
956         if (npages < 0)
957                 GOTO(out, rc = npages);
958
959         LASSERT(npages <= OST_THREAD_POOL_SIZE);
960
961         ost_nio_pages_get(req, local_nb, npages);
962
963         desc = ptlrpc_prep_bulk_exp(req, npages,
964                                      BULK_GET_SINK, OST_BULK_PORTAL);
965         if (desc == NULL)
966                 GOTO(out, rc = -ENOMEM);
967
968         rc = ost_brw_lock_get(LCK_PW, req->rq_export, ioo, pp_rnb, &lockh);
969         if (rc != 0)
970                 GOTO(out_bulk, rc);
971
972         /* 
973          * If getting the lock took more time than
974          * client was willing to wait, drop it. b=11330
975          */
976         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
977             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
978                 no_reply = 1;
979                 CERROR("Dropping timed-out write from %s because locking"
980                        "object "LPX64" took %ld seconds.\n",
981                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
982                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
983                 goto out_lock;
984         }
985
986         ost_prolong_locks(req->rq_export, ioo, pp_rnb, LCK_PW);
987
988         /* obd_preprw clobbers oa->valid, so save what we need */
989         client_cksum = body->oa.o_valid & OBD_MD_FLCKSUM ? body->oa.o_cksum : 0;
990
991         /* Because we already sync grant info with client when reconnect,
992          * grant info will be cleared for resent req, then fed_grant and 
993          * total_grant will not be modified in following preprw_write*/ 
994         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
995                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info\n");
996                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
997         }
998
999         rc = obd_preprw(OBD_BRW_WRITE, req->rq_export, &body->oa, objcount,
1000                         ioo, npages, pp_rnb, local_nb, oti);
1001         if (rc != 0)
1002                 GOTO(out_lock, rc);
1003
1004         /* NB Having prepped, we must commit... */
1005
1006         for (i = 0; i < npages; i++)
1007                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1008                                       pp_rnb[i].offset & ~CFS_PAGE_MASK,
1009                                       pp_rnb[i].len);
1010
1011         /* Check if client was evicted while we were doing i/o before touching
1012            network */
1013         if (desc->bd_export->exp_failed)
1014                 rc = -ENOTCONN;
1015         else
1016                 rc = ptlrpc_start_bulk_transfer (desc);
1017         if (rc == 0) {
1018                 lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 2, HZ,
1019                                            ost_bulk_timeout, desc);
1020                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1021                                   desc->bd_export->exp_failed, &lwi);
1022                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1023                 if (rc == -ETIMEDOUT) {
1024                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1025                         ptlrpc_abort_bulk(desc);
1026                 } else if (desc->bd_export->exp_failed) {
1027                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1028                         rc = -ENOTCONN;
1029                         ptlrpc_abort_bulk(desc);
1030                 } else if (!desc->bd_success ||
1031                            desc->bd_nob_transferred != desc->bd_nob) {
1032                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1033                                   desc->bd_success ?
1034                                   "truncated" : "network error on",
1035                                   desc->bd_nob_transferred, desc->bd_nob);
1036                         /* XXX should this be a different errno? */
1037                         rc = -ETIMEDOUT;
1038                 }
1039         } else {
1040                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
1041         }
1042         comms_error = rc != 0;
1043
1044         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1045                                  sizeof(*repbody));
1046         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1047
1048         if (unlikely(client_cksum != 0 && rc == 0)) {
1049                 static int cksum_counter;
1050
1051                 server_cksum = ost_checksum_bulk(desc);
1052                 repbody->oa.o_valid |= OBD_MD_FLCKSUM;
1053                 repbody->oa.o_cksum = server_cksum;
1054                 cksum_counter++;
1055                 if (unlikely(client_cksum != server_cksum)) {
1056                         CERROR("client csum %x, server csum %x\n",
1057                                client_cksum, server_cksum);
1058                         cksum_counter = 0;
1059                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1060                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1061                                cksum_counter, libcfs_id2str(req->rq_peer),
1062                                server_cksum);
1063                 }
1064         }
1065
1066         /* Must commit after prep above in all cases */
1067         rc = obd_commitrw(OBD_BRW_WRITE, req->rq_export, &repbody->oa,
1068                            objcount, ioo, npages, local_nb, oti, rc);
1069
1070         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1071                 int   new_cksum = ost_checksum_bulk(desc);
1072                 char *msg;
1073                 char *via;
1074                 char *router;
1075
1076                 if (new_cksum == server_cksum)
1077                         msg = "changed in transit before arrival at OST";
1078                 else if (new_cksum == client_cksum)
1079                         msg = "initial checksum before message complete";
1080                 else
1081                         msg = "changed in transit AND after initial checksum";
1082
1083                 if (req->rq_peer.nid == desc->bd_sender) {
1084                         via = router = "";
1085                 } else {
1086                         via = " via ";
1087                         router = libcfs_nid2str(desc->bd_sender);
1088                 }
1089                 
1090                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from %s"
1091                                    "%s%s inum "LPU64"/"LPU64" object "LPU64"/"
1092                                    LPU64" extent ["LPU64"-"LPU64"]\n",
1093                                    req->rq_export->exp_obd->obd_name, msg,
1094                                    libcfs_id2str(req->rq_peer),
1095                                    via, router,
1096                                    body->oa.o_valid & OBD_MD_FLFID ?
1097                                                 body->oa.o_fid : (__u64)0,
1098                                    body->oa.o_valid & OBD_MD_FLFID ?
1099                                                 body->oa.o_generation :(__u64)0,
1100                                    body->oa.o_id,
1101                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1102                                                 body->oa.o_gr : (__u64)0,
1103                                    pp_rnb[0].offset,
1104                                    pp_rnb[npages-1].offset+pp_rnb[npages-1].len
1105                                    - 1 );
1106                 CERROR("client csum %x, original server csum %x, "
1107                        "server csum now %x\n",
1108                        client_cksum, server_cksum, new_cksum);
1109         }
1110
1111         ost_nio_pages_put(req, local_nb, npages);
1112
1113         if (rc == 0) {
1114                 /* set per-requested niobuf return codes */
1115                 for (i = j = 0; i < niocount; i++) {
1116                         int nob = remote_nb[i].len;
1117
1118                         rcs[i] = 0;
1119                         do {
1120                                 LASSERT(j < npages);
1121                                 if (local_nb[j].rc < 0)
1122                                         rcs[i] = local_nb[j].rc;
1123                                 nob -= pp_rnb[j].len;
1124                                 j++;
1125                         } while (nob > 0);
1126                         LASSERT(nob == 0);
1127                 }
1128                 LASSERT(j == npages);
1129         }
1130
1131  out_lock:
1132         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1133  out_bulk:
1134         ptlrpc_free_bulk(desc);
1135         if (no_reply)
1136                 RETURN(rc);
1137  out:
1138         if (rc == 0) {
1139                 oti_to_request(oti, req);
1140                 target_committed_to_req(req);
1141                 rc = ptlrpc_reply(req);
1142         } else if (!comms_error) {
1143                 /* Only reply if there was no comms problem with bulk */
1144                 target_committed_to_req(req);
1145                 req->rq_status = rc;
1146                 ptlrpc_error(req);
1147         } else {
1148                 if (req->rq_reply_state != NULL) {
1149                         /* reply out callback would free */
1150                         ptlrpc_rs_decref(req->rq_reply_state);
1151                         req->rq_reply_state = NULL;
1152                 }
1153                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1154                       "client will retry\n",
1155                       req->rq_export->exp_obd->obd_name,
1156                       req->rq_export->exp_client_uuid.uuid,
1157                       req->rq_export->exp_connection->c_remote_uuid.uuid,
1158                       libcfs_id2str(req->rq_peer));
1159         }
1160         RETURN(rc);
1161 }
1162
1163 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1164 {
1165         char *key, *val = NULL;
1166         int keylen, vallen, rc = 0;
1167         ENTRY;
1168
1169         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1170         if (key == NULL) {
1171                 DEBUG_REQ(D_HA, req, "no set_info key");
1172                 RETURN(-EFAULT);
1173         }
1174         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1175
1176         rc = lustre_pack_reply(req, 1, NULL, NULL);
1177         if (rc)
1178                 RETURN(rc);
1179
1180         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1181         if (vallen)
1182                 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1183
1184         if (KEY_IS("evict_by_nid")) {
1185                 if (val && vallen)
1186                         obd_export_evict_by_nid(exp->exp_obd, val);
1187
1188                 GOTO(out, rc = 0);
1189         }
1190
1191         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1192 out:
1193         lustre_msg_set_status(req->rq_repmsg, 0);
1194         RETURN(rc);
1195 }
1196
1197 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1198 {
1199         char *key;
1200         int keylen, rc = 0;
1201         int size[2] = { sizeof(struct ptlrpc_body), sizeof(obd_id) };
1202         obd_id *reply;
1203         ENTRY;
1204
1205         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1206         if (key == NULL) {
1207                 DEBUG_REQ(D_HA, req, "no get_info key");
1208                 RETURN(-EFAULT);
1209         }
1210         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1211
1212         if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
1213                 RETURN(-EPROTO);
1214
1215         rc = lustre_pack_reply(req, 2, size, NULL);
1216         if (rc)
1217                 RETURN(rc);
1218
1219         reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply));
1220         rc = obd_get_info(exp, keylen, key, size, reply);
1221         lustre_msg_set_status(req->rq_repmsg, 0);
1222         RETURN(rc);
1223 }
1224
1225 static int ost_handle_quotactl(struct ptlrpc_request *req)
1226 {
1227         struct obd_quotactl *oqctl, *repoqc;
1228         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1229         ENTRY;
1230
1231         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1232                                    lustre_swab_obd_quotactl);
1233         if (oqctl == NULL)
1234                 GOTO(out, rc = -EPROTO);
1235
1236         rc = lustre_pack_reply(req, 2, size, NULL);
1237         if (rc)
1238                 GOTO(out, rc);
1239
1240         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1241
1242         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1243         *repoqc = *oqctl;
1244 out:
1245         RETURN(rc);
1246 }
1247
1248 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1249 {
1250         struct obd_quotactl *oqctl;
1251         int rc;
1252         ENTRY;
1253
1254         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1255                                    lustre_swab_obd_quotactl);
1256         if (oqctl == NULL) 
1257                 RETURN(-EPROTO);
1258
1259         rc = lustre_pack_reply(req, 1, NULL, NULL);
1260         if (rc) {
1261                 CERROR("ost: out of memory while packing quotacheck reply\n");
1262                 RETURN(-ENOMEM);
1263         }
1264
1265         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1266         RETURN(0);
1267 }
1268
1269 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1270                                        struct obd_device *obd, int *process)
1271 {
1272         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1273         case OST_CONNECT: /* This will never get here, but for completeness. */
1274         case OST_DISCONNECT:
1275                *process = 1;
1276                RETURN(0);
1277
1278         case OBD_PING:
1279         case OST_CREATE:
1280         case OST_DESTROY:
1281         case OST_PUNCH:
1282         case OST_SETATTR:
1283         case OST_SYNC:
1284         case OST_WRITE:
1285         case OBD_LOG_CANCEL:
1286         case LDLM_ENQUEUE:
1287                 *process = target_queue_recovery_request(req, obd);
1288                 RETURN(0);
1289
1290         default:
1291                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1292                 *process = 0;
1293                 /* XXX what should we set rq_status to here? */
1294                 req->rq_status = -EAGAIN;
1295                 RETURN(ptlrpc_error(req));
1296         }
1297 }
1298
1299 int ost_msg_check_version(struct lustre_msg *msg)
1300 {
1301         int rc;
1302
1303         switch(lustre_msg_get_opc(msg)) {
1304         case OST_CONNECT:
1305         case OST_DISCONNECT:
1306         case OBD_PING:
1307                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1308                 if (rc)
1309                         CERROR("bad opc %u version %08x, expecting %08x\n",
1310                                lustre_msg_get_opc(msg),
1311                                lustre_msg_get_version(msg),
1312                                LUSTRE_OBD_VERSION);
1313                 break;
1314         case OST_CREATE:
1315         case OST_DESTROY:
1316         case OST_GETATTR:
1317         case OST_SETATTR:
1318         case OST_WRITE:
1319         case OST_READ:
1320         case OST_PUNCH:
1321         case OST_STATFS:
1322         case OST_SYNC:
1323         case OST_SET_INFO:
1324         case OST_GET_INFO:
1325         case OST_QUOTACHECK:
1326         case OST_QUOTACTL:
1327                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1328                 if (rc)
1329                         CERROR("bad opc %u version %08x, expecting %08x\n",
1330                                lustre_msg_get_opc(msg),
1331                                lustre_msg_get_version(msg),
1332                                LUSTRE_OST_VERSION);
1333                 break;
1334         case LDLM_ENQUEUE:
1335         case LDLM_CONVERT:
1336         case LDLM_CANCEL:
1337         case LDLM_BL_CALLBACK:
1338         case LDLM_CP_CALLBACK:
1339                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1340                 if (rc)
1341                         CERROR("bad opc %u version %08x, expecting %08x\n",
1342                                lustre_msg_get_opc(msg),
1343                                lustre_msg_get_version(msg),
1344                                LUSTRE_DLM_VERSION);
1345                 break;
1346         case LLOG_ORIGIN_CONNECT:
1347         case OBD_LOG_CANCEL:
1348                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1349                 if (rc)
1350                         CERROR("bad opc %u version %08x, expecting %08x\n",
1351                                lustre_msg_get_opc(msg),
1352                                lustre_msg_get_version(msg),
1353                                LUSTRE_LOG_VERSION);
1354                 break;
1355         default:
1356                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1357                 rc = -ENOTSUPP;
1358         }
1359         return rc;
1360 }
1361
1362 static int ost_handle(struct ptlrpc_request *req)
1363 {
1364         struct obd_trans_info trans_info = { 0, };
1365         struct obd_trans_info *oti = &trans_info;
1366         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1367         struct obd_device *obd = NULL;
1368         ENTRY;
1369
1370         LASSERT(current->journal_info == NULL);
1371         /* XXX identical to MDS */
1372         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1373                 int abort_recovery, recovering;
1374
1375                 if (req->rq_export == NULL) {
1376                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1377                                lustre_msg_get_opc(req->rq_reqmsg),
1378                                libcfs_id2str(req->rq_peer));
1379                         req->rq_status = -ENOTCONN;
1380                         GOTO(out, rc = -ENOTCONN);
1381                 }
1382
1383                 obd = req->rq_export->exp_obd;
1384
1385                 /* Check for aborted recovery. */
1386                 spin_lock_bh(&obd->obd_processing_task_lock);
1387                 abort_recovery = obd->obd_abort_recovery;
1388                 recovering = obd->obd_recovering;
1389                 spin_unlock_bh(&obd->obd_processing_task_lock);
1390                 if (abort_recovery) {
1391                         target_abort_recovery(obd);
1392                 } else if (recovering) {
1393                         rc = ost_filter_recovery_request(req, obd,
1394                                                          &should_process);
1395                         if (rc || !should_process)
1396                                 RETURN(rc);
1397                 }
1398         }
1399
1400         oti_init(oti, req);
1401         rc = ost_msg_check_version(req->rq_reqmsg);
1402         if (rc)
1403                 RETURN(rc);
1404
1405         rc = ost_msg_check_version(req->rq_reqmsg);
1406         if (rc)
1407                 RETURN(rc);
1408
1409         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1410         case OST_CONNECT: {
1411                 CDEBUG(D_INODE, "connect\n");
1412                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
1413                 rc = target_handle_connect(req, ost_handle);
1414                 if (!rc)
1415                         obd = req->rq_export->exp_obd;
1416                 break;
1417         }
1418         case OST_DISCONNECT:
1419                 CDEBUG(D_INODE, "disconnect\n");
1420                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
1421                 rc = target_handle_disconnect(req);
1422                 break;
1423         case OST_CREATE:
1424                 CDEBUG(D_INODE, "create\n");
1425                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
1426                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1427                         GOTO(out, rc = -ENOSPC);
1428                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1429                         GOTO(out, rc = -EROFS);
1430                 rc = ost_create(req->rq_export, req, oti);
1431                 break;
1432         case OST_DESTROY:
1433                 CDEBUG(D_INODE, "destroy\n");
1434                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
1435                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1436                         GOTO(out, rc = -EROFS);
1437                 rc = ost_destroy(req->rq_export, req, oti);
1438                 break;
1439         case OST_GETATTR:
1440                 CDEBUG(D_INODE, "getattr\n");
1441                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
1442                 rc = ost_getattr(req->rq_export, req);
1443                 break;
1444         case OST_SETATTR:
1445                 CDEBUG(D_INODE, "setattr\n");
1446                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
1447                 rc = ost_setattr(req->rq_export, req, oti);
1448                 break;
1449         case OST_WRITE:
1450                 CDEBUG(D_INODE, "write\n");
1451                 /* req->rq_request_portal would be nice, if it was set */
1452                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1453                         CERROR("%s: deny write request from %s to portal %u\n",
1454                                req->rq_export->exp_obd->obd_name,
1455                                obd_export_nid2str(req->rq_export),
1456                                req->rq_rqbd->rqbd_service->srv_req_portal);
1457                         GOTO(out, rc = -EPROTO);
1458                 }
1459                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1460                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1461                         GOTO(out, rc = -ENOSPC);
1462                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1463                         GOTO(out, rc = -EROFS);
1464                 rc = ost_brw_write(req, oti);
1465                 LASSERT(current->journal_info == NULL);
1466                 /* ost_brw_write sends its own replies */
1467                 RETURN(rc);
1468         case OST_READ:
1469                 CDEBUG(D_INODE, "read\n");
1470                 /* req->rq_request_portal would be nice, if it was set */
1471                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1472                         CERROR("%s: deny read request from %s to portal %u\n",
1473                                req->rq_export->exp_obd->obd_name,
1474                                obd_export_nid2str(req->rq_export),
1475                                req->rq_rqbd->rqbd_service->srv_req_portal);
1476                         GOTO(out, rc = -EPROTO);
1477                 }
1478                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1479                 rc = ost_brw_read(req, oti);
1480                 LASSERT(current->journal_info == NULL);
1481                 /* ost_brw_read sends its own replies */
1482                 RETURN(rc);
1483         case OST_PUNCH:
1484                 CDEBUG(D_INODE, "punch\n");
1485                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
1486                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1487                         GOTO(out, rc = -EROFS);
1488                 rc = ost_punch(req->rq_export, req, oti);
1489                 break;
1490         case OST_STATFS:
1491                 CDEBUG(D_INODE, "statfs\n");
1492                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
1493                 rc = ost_statfs(req);
1494                 break;
1495         case OST_SYNC:
1496                 CDEBUG(D_INODE, "sync\n");
1497                 OBD_FAIL_RETURN(OBD_FAIL_OST_SYNC_NET, 0);
1498                 rc = ost_sync(req->rq_export, req);
1499                 break;
1500         case OST_SET_INFO:
1501                 DEBUG_REQ(D_INODE, req, "set_info");
1502                 rc = ost_set_info(req->rq_export, req);
1503                 break;
1504         case OST_GET_INFO:
1505                 DEBUG_REQ(D_INODE, req, "get_info");
1506                 rc = ost_get_info(req->rq_export, req);
1507                 break;
1508         case OST_QUOTACHECK:
1509                 CDEBUG(D_INODE, "quotacheck\n");
1510                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACHECK_NET, 0);
1511                 rc = ost_handle_quotacheck(req);
1512                 break;
1513         case OST_QUOTACTL:
1514                 CDEBUG(D_INODE, "quotactl\n");
1515                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACTL_NET, 0);
1516                 rc = ost_handle_quotactl(req);
1517                 break;
1518         case OBD_PING:
1519                 DEBUG_REQ(D_INODE, req, "ping");
1520                 rc = target_handle_ping(req);
1521                 break;
1522         /* FIXME - just reply status */
1523         case LLOG_ORIGIN_CONNECT:
1524                 DEBUG_REQ(D_INODE, req, "log connect\n");
1525                 rc = llog_handle_connect(req);
1526                 req->rq_status = rc;
1527                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1528                 if (rc)
1529                         RETURN(rc);
1530                 RETURN(ptlrpc_reply(req));
1531         case OBD_LOG_CANCEL:
1532                 CDEBUG(D_INODE, "log cancel\n");
1533                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1534                 rc = llog_origin_handle_cancel(req);
1535                 req->rq_status = rc;
1536                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1537                 if (rc)
1538                         RETURN(rc);
1539                 RETURN(ptlrpc_reply(req));
1540         case LDLM_ENQUEUE:
1541                 CDEBUG(D_INODE, "enqueue\n");
1542                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1543                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1544                                          ldlm_server_blocking_ast,
1545                                          ldlm_server_glimpse_ast);
1546                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1547                 break;
1548         case LDLM_CONVERT:
1549                 CDEBUG(D_INODE, "convert\n");
1550                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1551                 rc = ldlm_handle_convert(req);
1552                 break;
1553         case LDLM_CANCEL:
1554                 CDEBUG(D_INODE, "cancel\n");
1555                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1556                 rc = ldlm_handle_cancel(req);
1557                 break;
1558         case LDLM_BL_CALLBACK:
1559         case LDLM_CP_CALLBACK:
1560                 CDEBUG(D_INODE, "callback\n");
1561                 CERROR("callbacks should not happen on OST\n");
1562                 /* fall through */
1563         default:
1564                 CERROR("Unexpected opcode %d\n",
1565                        lustre_msg_get_opc(req->rq_reqmsg));
1566                 req->rq_status = -ENOTSUPP;
1567                 rc = ptlrpc_error(req);
1568                 RETURN(rc);
1569         }
1570
1571         LASSERT(current->journal_info == NULL);
1572
1573         EXIT;
1574         /* If we're DISCONNECTing, the export_data is already freed */
1575         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1576                 target_committed_to_req(req);
1577
1578 out:
1579         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1580                 if (obd && obd->obd_recovering) {
1581                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1582                         return target_queue_final_reply(req, rc);
1583                 }
1584                 /* Lost a race with recovery; let the error path DTRT. */
1585                 rc = req->rq_status = -ENOTCONN;
1586         }
1587
1588         if (!rc)
1589                 oti_to_request(oti, req);
1590
1591         target_send_reply(req, rc, fail);
1592         return 0;
1593 }
1594
1595 /*
1596  * free per-thread pool created by ost_thread_init().
1597  */
1598 static void ost_thread_done(struct ptlrpc_thread *thread)
1599 {
1600         int i;
1601         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1602                                              * Storage */
1603
1604         ENTRY;
1605
1606         LASSERT(thread != NULL);
1607
1608         /*
1609          * be prepared to handle partially-initialized pools (because this is
1610          * called from ost_thread_init() for cleanup.
1611          */
1612         tls = thread->t_data;
1613         if (tls != NULL) {
1614                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1615                         if (tls->page[i] != NULL)
1616                                 __free_page(tls->page[i]);
1617                 }
1618                 OBD_FREE_PTR(tls);
1619                 thread->t_data = NULL;
1620         }
1621         EXIT;
1622 }
1623
1624 /*
1625  * initialize per-thread page pool (bug 5137).
1626  */
1627 static int ost_thread_init(struct ptlrpc_thread *thread)
1628 {
1629         int result;
1630         int i;
1631         struct ost_thread_local_cache *tls;
1632
1633         ENTRY;
1634
1635         LASSERT(thread != NULL);
1636         LASSERT(thread->t_data == NULL);
1637         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
1638
1639         OBD_ALLOC_PTR(tls);
1640         if (tls != NULL) {
1641                 result = 0;
1642                 thread->t_data = tls;
1643                 /*
1644                  * populate pool
1645                  */
1646                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1647                         tls->page[i] = alloc_page(OST_THREAD_POOL_GFP);
1648                         if (tls->page[i] == NULL) {
1649                                 ost_thread_done(thread);
1650                                 result = -ENOMEM;
1651                                 break;
1652                         }
1653                 }
1654         } else
1655                 result = -ENOMEM;
1656         RETURN(result);
1657 }
1658
1659 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1660 static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
1661 {
1662         struct ost_obd *ost = &obd->u.ost;
1663         struct lprocfs_static_vars lvars;
1664         int oss_min_threads;
1665         int oss_max_threads;
1666         int rc;
1667         ENTRY;
1668
1669         rc = cleanup_group_info();
1670         if (rc)
1671                 RETURN(rc);
1672
1673         rc = llog_start_commit_thread();
1674         if (rc < 0)
1675                 RETURN(rc);
1676
1677         lprocfs_init_vars(ost, &lvars);
1678         lprocfs_obd_setup(obd, lvars.obd_vars);
1679
1680         sema_init(&ost->ost_health_sem, 1);
1681
1682         if (oss_num_threads) {
1683                 /* If oss_num_threads is set, it is the min and the max. */
1684                 if (oss_num_threads > OSS_THREADS_MAX) 
1685                         oss_num_threads = OSS_THREADS_MAX;
1686                 if (oss_num_threads < OSS_THREADS_MIN)
1687                         oss_num_threads = OSS_THREADS_MIN;
1688                 oss_max_threads = oss_min_threads = oss_num_threads;
1689         } else {
1690                 /* Base min threads on memory and cpus */
1691                 oss_min_threads = smp_num_cpus * num_physpages >> 
1692                         (27 - CFS_PAGE_SHIFT);
1693                 if (oss_min_threads < OSS_THREADS_MIN)
1694                         oss_min_threads = OSS_THREADS_MIN;
1695                 /* Insure a 4x range for dynamic threads */
1696                 if (oss_min_threads > OSS_THREADS_MAX / 4) 
1697                         oss_min_threads = OSS_THREADS_MAX / 4;
1698                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1699         }
1700
1701         ost->ost_service =
1702                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1703                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1704                                 OSC_REPLY_PORTAL,
1705                                 obd_timeout * 1000, ost_handle, LUSTRE_OSS_NAME,
1706                                 obd->obd_proc_entry, ost_print_req,
1707                                 oss_min_threads, oss_max_threads, "ll_ost");
1708         if (ost->ost_service == NULL) {
1709                 CERROR("failed to start OST service\n");
1710                 GOTO(out_lprocfs, rc = -ENOMEM);
1711         }
1712
1713         rc = ptlrpc_start_threads(obd, ost->ost_service);
1714         if (rc)
1715                 GOTO(out_service, rc = -EINVAL);
1716
1717         ost->ost_create_service =
1718                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1719                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1720                                 OSC_REPLY_PORTAL,
1721                                 obd_timeout * 1000, ost_handle, "ost_create",
1722                                 obd->obd_proc_entry, ost_print_req,
1723                                 1, 1, "ll_ost_creat");
1724         if (ost->ost_create_service == NULL) {
1725                 CERROR("failed to start OST create service\n");
1726                 GOTO(out_service, rc = -ENOMEM);
1727         }
1728
1729         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1730         if (rc)
1731                 GOTO(out_create, rc = -EINVAL);
1732
1733         ost->ost_io_service =
1734                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1735                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1736                                 OSC_REPLY_PORTAL,
1737                                 obd_timeout * 1000, ost_handle, "ost_io",
1738                                 obd->obd_proc_entry, ost_print_req,
1739                                 oss_min_threads, oss_max_threads, "ll_ost_io");
1740         if (ost->ost_io_service == NULL) {
1741                 CERROR("failed to start OST I/O service\n");
1742                 GOTO(out_create, rc = -ENOMEM);
1743         }
1744
1745         ost->ost_io_service->srv_init = ost_thread_init;
1746         ost->ost_io_service->srv_done = ost_thread_done;
1747         ost->ost_io_service->srv_cpu_affinity = 1;
1748         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1749         if (rc)
1750                 GOTO(out_io, rc = -EINVAL);
1751
1752         ping_evictor_start();
1753
1754         RETURN(0);
1755
1756 out_io:
1757         ptlrpc_unregister_service(ost->ost_io_service);
1758         ost->ost_io_service = NULL;
1759 out_create:
1760         ptlrpc_unregister_service(ost->ost_create_service);
1761         ost->ost_create_service = NULL;
1762 out_service:
1763         ptlrpc_unregister_service(ost->ost_service);
1764         ost->ost_service = NULL;
1765 out_lprocfs:
1766         lprocfs_obd_cleanup(obd);
1767         RETURN(rc);
1768 }
1769
1770 static int ost_cleanup(struct obd_device *obd)
1771 {
1772         struct ost_obd *ost = &obd->u.ost;
1773         int err = 0;
1774         ENTRY;
1775
1776         ping_evictor_stop();
1777
1778         spin_lock_bh(&obd->obd_processing_task_lock);
1779         if (obd->obd_recovering) {
1780                 target_cancel_recovery_timer(obd);
1781                 obd->obd_recovering = 0;
1782         }
1783         spin_unlock_bh(&obd->obd_processing_task_lock);
1784
1785         down(&ost->ost_health_sem);
1786         ptlrpc_unregister_service(ost->ost_service);
1787         ptlrpc_unregister_service(ost->ost_create_service);
1788         ptlrpc_unregister_service(ost->ost_io_service);
1789         ost->ost_service = NULL;
1790         ost->ost_create_service = NULL;
1791         up(&ost->ost_health_sem);
1792
1793         lprocfs_obd_cleanup(obd);
1794
1795         RETURN(err);
1796 }
1797
1798 static int ost_health_check(struct obd_device *obd)
1799 {
1800         struct ost_obd *ost = &obd->u.ost;
1801         int rc = 0;
1802
1803         down(&ost->ost_health_sem);
1804         rc |= ptlrpc_service_health_check(ost->ost_service);
1805         rc |= ptlrpc_service_health_check(ost->ost_create_service);
1806         rc |= ptlrpc_service_health_check(ost->ost_io_service);
1807         up(&ost->ost_health_sem);
1808
1809         /*
1810          * health_check to return 0 on healthy
1811          * and 1 on unhealthy.
1812          */
1813         if( rc != 0)
1814                 rc = 1;
1815
1816         return rc;
1817 }
1818
1819 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
1820 {
1821         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
1822 }
1823
1824 /* use obd ops to offer management infrastructure */
1825 static struct obd_ops ost_obd_ops = {
1826         .o_owner        = THIS_MODULE,
1827         .o_setup        = ost_setup,
1828         .o_cleanup      = ost_cleanup,
1829         .o_health_check = ost_health_check,
1830 };
1831
1832
1833 static int __init ost_init(void)
1834 {
1835         struct lprocfs_static_vars lvars;
1836         int rc;
1837         ENTRY;
1838
1839         lprocfs_init_vars(ost, &lvars);
1840         rc = class_register_type(&ost_obd_ops, lvars.module_vars,
1841                                  LUSTRE_OSS_NAME);
1842
1843         if (ost_num_threads != 0 && oss_num_threads == 0) {
1844                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
1845                               "use oss_num_threads instead or unset both for "
1846                               "dynamic thread startup\n");
1847                 oss_num_threads = ost_num_threads;
1848         }
1849
1850         RETURN(rc);
1851 }
1852
1853 static void /*__exit*/ ost_exit(void)
1854 {
1855         class_unregister_type(LUSTRE_OSS_NAME);
1856 }
1857
1858 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1859 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
1860 MODULE_LICENSE("GPL");
1861
1862 module_init(ost_init);
1863 module_exit(ost_exit);