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