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