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