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