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