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