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