Whamcloud - gitweb
b=16098
[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                 target_committed_to_req(req);
932                 ptlrpc_reply(req);
933         } else if (!no_reply) {
934                 /* Only reply if there was no comms problem with bulk */
935                 target_committed_to_req(req);
936                 req->rq_status = rc;
937                 ptlrpc_error(req);
938         } else {
939                 /* reply out callback would free */
940                 ptlrpc_req_drop_rs(req);
941                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
942                       "client will retry\n",
943                       exp->exp_obd->obd_name,
944                       exp->exp_client_uuid.uuid,
945                       exp->exp_connection->c_remote_uuid.uuid,
946                       libcfs_id2str(req->rq_peer));
947         }
948
949         RETURN(rc);
950 }
951
952 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
953 {
954         struct ptlrpc_bulk_desc *desc;
955         struct obd_export       *exp = req->rq_export;
956         struct niobuf_remote    *remote_nb;
957         struct niobuf_remote    *pp_rnb;
958         struct niobuf_local     *local_nb;
959         struct obd_ioobj        *ioo;
960         struct ost_body         *body, *repbody;
961         struct l_wait_info       lwi;
962         struct lustre_handle     lockh = {0};
963         struct lustre_capa      *capa = NULL;
964         __u32                   *rcs;
965         __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
966         int objcount, niocount, npages;
967         int rc, swab, i, j;
968         obd_count                client_cksum = 0, server_cksum = 0;
969         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
970         int                      no_reply = 0; 
971         ENTRY;
972
973         req->rq_bulk_write = 1;
974
975         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
976                 GOTO(out, rc = -EIO);
977         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
978                 GOTO(out, rc = -EFAULT);
979
980         /* pause before transaction has been started */
981         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
982
983         /* Check if there is eviction in progress, and if so, wait for it to
984          * finish */
985         if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
986                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
987                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
988                         !atomic_read(&exp->exp_obd->obd_evict_inprogress),
989                         &lwi);
990         }
991         if (exp->exp_failed)
992                 GOTO(out, rc = -ENOTCONN);
993
994         swab = lustre_msg_swabbed(req->rq_reqmsg);
995         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
996                                   lustre_swab_ost_body);
997         if (body == NULL) {
998                 CERROR("Missing/short ost_body\n");
999                 GOTO(out, rc = -EFAULT);
1000         }
1001
1002         lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
1003         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
1004                    sizeof(*ioo);
1005         if (objcount == 0) {
1006                 CERROR("Missing/short ioobj\n");
1007                 GOTO(out, rc = -EFAULT);
1008         }
1009         if (objcount > 1) {
1010                 CERROR("too many ioobjs (%d)\n", objcount);
1011                 GOTO(out, rc = -EFAULT);
1012         }
1013
1014         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
1015                              objcount * sizeof(*ioo));
1016         LASSERT (ioo != NULL);
1017         for (niocount = i = 0; i < objcount; i++) {
1018                 if (swab)
1019                         lustre_swab_obd_ioobj(&ioo[i]);
1020                 if (ioo[i].ioo_bufcnt == 0) {
1021                         CERROR("ioo[%d] has zero bufcnt\n", i);
1022                         GOTO(out, rc = -EFAULT);
1023                 }
1024                 niocount += ioo[i].ioo_bufcnt;
1025         }
1026
1027         if (niocount > PTLRPC_MAX_BRW_PAGES) {
1028                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
1029                           niocount);
1030                 GOTO(out, rc = -EFAULT);
1031         }
1032
1033         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
1034                                        niocount * sizeof(*remote_nb),
1035                                        lustre_swab_niobuf_remote);
1036         if (remote_nb == NULL) {
1037                 CERROR("Missing/short niobuf\n");
1038                 GOTO(out, rc = -EFAULT);
1039         }
1040         if (swab) {                             /* swab the remaining niobufs */
1041                 for (i = 1; i < niocount; i++)
1042                         lustre_swab_niobuf_remote (&remote_nb[i]);
1043         }
1044
1045         if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
1046                 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
1047
1048         size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
1049         rc = lustre_pack_reply(req, 3, size, NULL);
1050         if (rc != 0)
1051                 GOTO(out, rc);
1052         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, obd_fail_val);
1053         rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
1054                              niocount * sizeof(*rcs));
1055
1056         /*
1057          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
1058          * ost_thread_init().
1059          */
1060         local_nb = ost_tls(req)->local;
1061         pp_rnb   = ost_tls(req)->remote;
1062
1063         /* FIXME all niobuf splitting should be done in obdfilter if needed */
1064         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
1065         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
1066         if (npages < 0)
1067                 GOTO(out, rc = npages);
1068
1069         LASSERT(npages <= OST_THREAD_POOL_SIZE);
1070
1071         ost_nio_pages_get(req, local_nb, npages);
1072
1073         desc = ptlrpc_prep_bulk_exp(req, npages,
1074                                      BULK_GET_SINK, OST_BULK_PORTAL);
1075         if (desc == NULL)
1076                 GOTO(out, rc = -ENOMEM);
1077
1078         rc = ost_brw_lock_get(LCK_PW, exp, ioo, pp_rnb, &lockh);
1079         if (rc != 0)
1080                 GOTO(out_bulk, rc);
1081
1082         /* 
1083          * If getting the lock took more time than
1084          * client was willing to wait, drop it. b=11330
1085          */
1086         if (cfs_time_current_sec() > req->rq_deadline ||
1087             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1088                 no_reply = 1;
1089                 CERROR("Dropping timed-out write from %s because locking "
1090                        "object "LPX64" took %ld seconds (limit was %ld).\n",
1091                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
1092                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
1093                        req->rq_deadline - req->rq_arrival_time.tv_sec);
1094                 GOTO(out_lock, rc = -ETIMEDOUT);
1095         }
1096
1097         ost_prolong_locks(exp, ioo, pp_rnb, LCK_PW);
1098
1099         /* obd_preprw clobbers oa->valid, so save what we need */
1100         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1101                 client_cksum = body->oa.o_cksum;
1102                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1103                         cksum_type = cksum_type_unpack(body->oa.o_flags);
1104         }
1105         
1106         /* Because we already sync grant info with client when reconnect,
1107          * grant info will be cleared for resent req, then fed_grant and 
1108          * total_grant will not be modified in following preprw_write */ 
1109         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1110                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1111                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1112         }
1113
1114         rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
1115                         ioo, npages, pp_rnb, local_nb, oti, capa);
1116         if (rc != 0)
1117                 GOTO(out_lock, rc);
1118
1119         /* NB Having prepped, we must commit... */
1120
1121         for (i = 0; i < npages; i++)
1122                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1123                                       pp_rnb[i].offset & ~CFS_PAGE_MASK,
1124                                       pp_rnb[i].len);
1125
1126         /* Check if client was evicted while we were doing i/o before touching
1127            network */
1128         if (desc->bd_export->exp_failed)
1129                 rc = -ENOTCONN;
1130         else
1131                 rc = ptlrpc_start_bulk_transfer (desc);
1132         if (rc == 0) {
1133                 time_t start = cfs_time_current_sec();
1134                 do {
1135                         long timeoutl = req->rq_deadline -
1136                                 cfs_time_current_sec();
1137                         cfs_duration_t timeout = (timeoutl <= 0 || rc) ?
1138                                 CFS_TICK : cfs_time_seconds(timeoutl);
1139                         lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
1140                                                    ost_bulk_timeout, desc);
1141                         rc = l_wait_event(desc->bd_waitq,
1142                                           !ptlrpc_bulk_active(desc) ||
1143                                           desc->bd_export->exp_failed, &lwi);
1144                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
1145                         /* Wait again if we changed deadline */
1146                 } while ((rc == -ETIMEDOUT) &&
1147                          (req->rq_deadline > cfs_time_current_sec()));
1148
1149                 if (rc == -ETIMEDOUT) {
1150                         DEBUG_REQ(D_ERROR, req,
1151                                   "timeout on bulk GET after %ld%+lds",
1152                                   req->rq_deadline - start,
1153                                   cfs_time_current_sec() -
1154                                   req->rq_deadline);
1155                         ptlrpc_abort_bulk(desc);
1156                 } else if (desc->bd_export->exp_failed) {
1157                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1158                         rc = -ENOTCONN;
1159                         ptlrpc_abort_bulk(desc);
1160                 } else if (!desc->bd_success ||
1161                            desc->bd_nob_transferred != desc->bd_nob) {
1162                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1163                                   desc->bd_success ?
1164                                   "truncated" : "network error on",
1165                                   desc->bd_nob_transferred, desc->bd_nob);
1166                         /* XXX should this be a different errno? */
1167                         rc = -ETIMEDOUT;
1168                 }
1169         } else {
1170                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1171         }
1172         no_reply = rc != 0;
1173
1174         if (rc == 0)
1175                 sptlrpc_svc_unwrap_bulk(req, desc);
1176
1177         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1178                                  sizeof(*repbody));
1179         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1180
1181         if (unlikely(client_cksum != 0 && rc == 0)) {
1182                 static int cksum_counter;
1183                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1184                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1185                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1186                 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1187                 repbody->oa.o_cksum = server_cksum;
1188                 cksum_counter++;
1189                 if (unlikely(client_cksum != server_cksum)) {
1190                         CERROR("client csum %x, server csum %x\n",
1191                                client_cksum, server_cksum);
1192                         cksum_counter = 0;
1193                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1194                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1195                                cksum_counter, libcfs_id2str(req->rq_peer),
1196                                server_cksum);
1197                 }
1198         }
1199
1200         /* Must commit after prep above in all cases */
1201         rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa,
1202                            objcount, ioo, npages, local_nb, oti, rc);
1203
1204         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1205                 int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1206                 char *msg;
1207                 char *via;
1208                 char *router;
1209
1210                 if (new_cksum == server_cksum)
1211                         msg = "changed in transit before arrival at OST";
1212                 else if (new_cksum == client_cksum)
1213                         msg = "initial checksum before message complete";
1214                 else
1215                         msg = "changed in transit AND after initial checksum";
1216
1217                 if (req->rq_peer.nid == desc->bd_sender) {
1218                         via = router = "";
1219                 } else {
1220                         via = " via ";
1221                         router = libcfs_nid2str(desc->bd_sender);
1222                 }
1223                 
1224                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1225                                    "%s%s%s inum "LPU64"/"LPU64" object "
1226                                    LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1227                                    exp->exp_obd->obd_name, msg,
1228                                    libcfs_id2str(req->rq_peer),
1229                                    via, router,
1230                                    body->oa.o_valid & OBD_MD_FLFID ?
1231                                                 body->oa.o_fid : (__u64)0,
1232                                    body->oa.o_valid & OBD_MD_FLFID ?
1233                                                 body->oa.o_generation :(__u64)0,
1234                                    body->oa.o_id,
1235                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1236                                                 body->oa.o_gr : (__u64)0,
1237                                    pp_rnb[0].offset,
1238                                    pp_rnb[npages-1].offset+pp_rnb[npages-1].len
1239                                    - 1 );
1240                 CERROR("client csum %x, original server csum %x, "
1241                        "server csum now %x\n",
1242                        client_cksum, server_cksum, new_cksum);
1243         }
1244
1245         ost_nio_pages_put(req, local_nb, npages);
1246
1247         if (rc == 0) {
1248                 /* set per-requested niobuf return codes */
1249                 for (i = j = 0; i < niocount; i++) {
1250                         int nob = remote_nb[i].len;
1251
1252                         rcs[i] = 0;
1253                         do {
1254                                 LASSERT(j < npages);
1255                                 if (local_nb[j].rc < 0)
1256                                         rcs[i] = local_nb[j].rc;
1257                                 nob -= pp_rnb[j].len;
1258                                 j++;
1259                         } while (nob > 0);
1260                         LASSERT(nob == 0);
1261                 }
1262                 LASSERT(j == npages);
1263         }
1264
1265 out_lock:
1266         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1267 out_bulk:
1268         ptlrpc_free_bulk(desc);
1269 out:
1270         if (rc == 0) {
1271                 oti_to_request(oti, req);
1272                 target_committed_to_req(req);
1273                 rc = ptlrpc_reply(req);
1274         } else if (!no_reply) {
1275                 /* Only reply if there was no comms problem with bulk */
1276                 target_committed_to_req(req);
1277                 req->rq_status = rc;
1278                 ptlrpc_error(req);
1279         } else {
1280                 /* reply out callback would free */
1281                 ptlrpc_req_drop_rs(req);
1282                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1283                       "client will retry\n",
1284                       exp->exp_obd->obd_name,
1285                       exp->exp_client_uuid.uuid,
1286                       exp->exp_connection->c_remote_uuid.uuid,
1287                       libcfs_id2str(req->rq_peer));
1288         }
1289         RETURN(rc);
1290 }
1291
1292 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1293 {
1294         char *key, *val = NULL;
1295         int keylen, vallen, rc = 0;
1296         ENTRY;
1297
1298         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1299         if (key == NULL) {
1300                 DEBUG_REQ(D_HA, req, "no set_info key");
1301                 RETURN(-EFAULT);
1302         }
1303         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1304
1305         rc = lustre_pack_reply(req, 1, NULL, NULL);
1306         if (rc)
1307                 RETURN(rc);
1308
1309         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1310         if (vallen)
1311                 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1312
1313         if (KEY_IS(KEY_EVICT_BY_NID)) {
1314                 if (val && vallen)
1315                         obd_export_evict_by_nid(exp->exp_obd, val);
1316
1317                 GOTO(out, rc = 0);
1318         }
1319
1320         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1321 out:
1322         lustre_msg_set_status(req->rq_repmsg, 0);
1323         RETURN(rc);
1324 }
1325
1326 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1327 {
1328         void *key, *reply;
1329         int keylen, replylen, rc = 0;
1330         struct req_capsule *pill = &req->rq_pill;
1331         ENTRY;
1332
1333         req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
1334
1335         /* this common part for get_info rpc */
1336         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1337         if (key == NULL) {
1338                 DEBUG_REQ(D_HA, req, "no get_info key");
1339                 RETURN(-EFAULT);
1340         }
1341         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1342
1343         rc = obd_get_info(exp, keylen, key, &replylen, NULL, NULL);
1344         if (rc)
1345                 RETURN(rc);
1346
1347         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1348                              RCL_SERVER, replylen);
1349
1350         rc = req_capsule_server_pack(pill);
1351         if (rc)
1352                 RETURN(rc);
1353
1354         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1355         if (reply == NULL)
1356                 RETURN(-ENOMEM);
1357
1358         /* call again to fill in the reply buffer */
1359         rc = obd_get_info(exp, keylen, key, &replylen, reply, NULL);
1360
1361         lustre_msg_set_status(req->rq_repmsg, 0);
1362         RETURN(rc);
1363 }
1364
1365 static int ost_handle_quotactl(struct ptlrpc_request *req)
1366 {
1367         struct obd_quotactl *oqctl, *repoqc;
1368         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1369         int rc;
1370         ENTRY;
1371
1372         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1373                                    lustre_swab_obd_quotactl);
1374         if (oqctl == NULL)
1375                 GOTO(out, rc = -EPROTO);
1376
1377         rc = lustre_pack_reply(req, 2, size, NULL);
1378         if (rc)
1379                 GOTO(out, rc);
1380
1381         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1382
1383         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1384         *repoqc = *oqctl;
1385 out:
1386         RETURN(rc);
1387 }
1388
1389 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1390 {
1391         struct obd_quotactl *oqctl;
1392         int rc;
1393         ENTRY;
1394
1395         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1396         if (oqctl == NULL)
1397                 RETURN(-EPROTO);
1398
1399         rc = req_capsule_server_pack(&req->rq_pill);
1400         if (rc) {
1401                 CERROR("ost: out of memory while packing quotacheck reply\n");
1402                 RETURN(-ENOMEM);
1403         }
1404
1405         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1406         RETURN(0);
1407 }
1408
1409 static int ost_llog_handle_connect(struct obd_export *exp,
1410                                    struct ptlrpc_request *req)
1411 {
1412         struct llogd_conn_body *body;
1413         int rc;
1414         ENTRY;
1415
1416         body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*body));
1417         rc = obd_llog_connect(exp, body);
1418         RETURN(rc);
1419 }
1420
1421 static int filter_export_check_flavor(struct filter_obd *filter,
1422                                       struct obd_export *exp,
1423                                       struct ptlrpc_request *req)
1424 {
1425         int     rc = 0;
1426
1427         /* FIXME
1428          * this should be done in filter_connect()/filter_reconnect(), but
1429          * we can't obtain information like NID, which stored in incoming
1430          * request, thus can't decide what flavor to use. so we do it here.
1431          *
1432          * This hack should be removed after the OST stack be rewritten, just
1433          * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1434          */
1435         if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_INVALID)
1436                 return 0;
1437
1438         CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
1439         spin_lock(&exp->exp_lock);
1440         exp->exp_sp_peer = req->rq_sp_from;
1441
1442         read_lock(&filter->fo_sptlrpc_lock);
1443         sptlrpc_rule_set_choose(&filter->fo_sptlrpc_rset, exp->exp_sp_peer,
1444                                 req->rq_peer.nid, &exp->exp_flvr);
1445         read_unlock(&filter->fo_sptlrpc_lock);
1446
1447         if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1448                 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
1449                        req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
1450                        libcfs_nid2str(req->rq_peer.nid));
1451                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
1452                 rc = -EACCES;
1453         }
1454
1455         spin_unlock(&exp->exp_lock);
1456
1457         return rc;
1458 }
1459
1460 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1461                                        struct obd_device *obd, int *process)
1462 {
1463         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1464         case OST_CONNECT: /* This will never get here, but for completeness. */
1465         case OST_DISCONNECT:
1466                *process = 1;
1467                RETURN(0);
1468
1469         case OBD_PING:
1470         case OST_CREATE:
1471         case OST_DESTROY:
1472         case OST_PUNCH:
1473         case OST_SETATTR:
1474         case OST_SYNC:
1475         case OST_WRITE:
1476         case OBD_LOG_CANCEL:
1477         case LDLM_ENQUEUE:
1478                 *process = target_queue_recovery_request(req, obd);
1479                 RETURN(0);
1480
1481         default:
1482                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1483                 *process = -EAGAIN;
1484                 RETURN(0);
1485         }
1486 }
1487
1488 int ost_msg_check_version(struct lustre_msg *msg)
1489 {
1490         int rc;
1491
1492         switch(lustre_msg_get_opc(msg)) {
1493         case OST_CONNECT:
1494         case OST_DISCONNECT:
1495         case OBD_PING:
1496         case SEC_CTX_INIT:
1497         case SEC_CTX_INIT_CONT:
1498         case SEC_CTX_FINI:
1499                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1500                 if (rc)
1501                         CERROR("bad opc %u version %08x, expecting %08x\n",
1502                                lustre_msg_get_opc(msg),
1503                                lustre_msg_get_version(msg),
1504                                LUSTRE_OBD_VERSION);
1505                 break;
1506         case OST_CREATE:
1507         case OST_DESTROY:
1508         case OST_GETATTR:
1509         case OST_SETATTR:
1510         case OST_WRITE:
1511         case OST_READ:
1512         case OST_PUNCH:
1513         case OST_STATFS:
1514         case OST_SYNC:
1515         case OST_SET_INFO:
1516         case OST_GET_INFO:
1517         case OST_QUOTACHECK:
1518         case OST_QUOTACTL:
1519                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1520                 if (rc)
1521                         CERROR("bad opc %u version %08x, expecting %08x\n",
1522                                lustre_msg_get_opc(msg),
1523                                lustre_msg_get_version(msg),
1524                                LUSTRE_OST_VERSION);
1525                 break;
1526         case LDLM_ENQUEUE:
1527         case LDLM_CONVERT:
1528         case LDLM_CANCEL:
1529         case LDLM_BL_CALLBACK:
1530         case LDLM_CP_CALLBACK:
1531                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1532                 if (rc)
1533                         CERROR("bad opc %u version %08x, expecting %08x\n",
1534                                lustre_msg_get_opc(msg),
1535                                lustre_msg_get_version(msg),
1536                                LUSTRE_DLM_VERSION);
1537                 break;
1538         case LLOG_ORIGIN_CONNECT:
1539         case OBD_LOG_CANCEL:
1540                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1541                 if (rc)
1542                         CERROR("bad opc %u version %08x, expecting %08x\n",
1543                                lustre_msg_get_opc(msg),
1544                                lustre_msg_get_version(msg),
1545                                LUSTRE_LOG_VERSION);
1546                 break;
1547         default:
1548                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1549                 rc = -ENOTSUPP;
1550         }
1551         return rc;
1552 }
1553
1554 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1555 int ost_handle(struct ptlrpc_request *req)
1556 {
1557         struct obd_trans_info trans_info = { 0, };
1558         struct obd_trans_info *oti = &trans_info;
1559         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1560         struct obd_device *obd = NULL;
1561         ENTRY;
1562
1563         LASSERT(current->journal_info == NULL);
1564
1565         /* primordial rpcs don't affect server recovery */
1566         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1567         case SEC_CTX_INIT:
1568         case SEC_CTX_INIT_CONT:
1569         case SEC_CTX_FINI:
1570                 GOTO(out, rc = 0);
1571         }
1572
1573         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1574
1575         /* XXX identical to MDS */
1576         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1577                 int recovering;
1578
1579                 if (req->rq_export == NULL) {
1580                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1581                                lustre_msg_get_opc(req->rq_reqmsg),
1582                                libcfs_id2str(req->rq_peer));
1583                         req->rq_status = -ENOTCONN;
1584                         GOTO(out, rc = -ENOTCONN);
1585                 }
1586
1587                 obd = req->rq_export->exp_obd;
1588
1589                 /* Check for aborted recovery. */
1590                 spin_lock_bh(&obd->obd_processing_task_lock);
1591                 recovering = obd->obd_recovering;
1592                 spin_unlock_bh(&obd->obd_processing_task_lock);
1593                 if (recovering) {
1594                         rc = ost_filter_recovery_request(req, obd,
1595                                                          &should_process);
1596                         if (rc || !should_process)
1597                                 RETURN(rc);
1598                         else if (should_process < 0) {
1599                                 req->rq_status = should_process;
1600                                 rc = ptlrpc_error(req);
1601                                 RETURN(rc);
1602                         }
1603                 }
1604         }
1605
1606         oti_init(oti, req);
1607         
1608         rc = ost_msg_check_version(req->rq_reqmsg);
1609         if (rc)
1610                 RETURN(rc);
1611
1612         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1613         case OST_CONNECT: {
1614                 CDEBUG(D_INODE, "connect\n");
1615                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
1616                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
1617                         RETURN(0);
1618                 rc = target_handle_connect(req);
1619                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
1620                         RETURN(0);
1621                 if (!rc) {
1622                         struct obd_export *exp = req->rq_export;
1623
1624                         obd = exp->exp_obd;
1625
1626                         rc = filter_export_check_flavor(&obd->u.filter,
1627                                                         exp, req);
1628                 }
1629                 break;
1630         }
1631         case OST_DISCONNECT:
1632                 CDEBUG(D_INODE, "disconnect\n");
1633                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
1634                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
1635                         RETURN(0);
1636                 rc = target_handle_disconnect(req);
1637                 break;
1638         case OST_CREATE:
1639                 CDEBUG(D_INODE, "create\n");
1640                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
1641                         RETURN(0);
1642                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1643                         GOTO(out, rc = -ENOSPC);
1644                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1645                         GOTO(out, rc = -EROFS);
1646                 rc = ost_create(req->rq_export, req, oti);
1647                 break;
1648         case OST_DESTROY:
1649                 CDEBUG(D_INODE, "destroy\n");
1650                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
1651                         RETURN(0);
1652                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1653                         GOTO(out, rc = -EROFS);
1654                 rc = ost_destroy(req->rq_export, req, oti);
1655                 break;
1656         case OST_GETATTR:
1657                 CDEBUG(D_INODE, "getattr\n");
1658                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
1659                         RETURN(0);
1660                 rc = ost_getattr(req->rq_export, req);
1661                 break;
1662         case OST_SETATTR:
1663                 CDEBUG(D_INODE, "setattr\n");
1664                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
1665                         RETURN(0);
1666                 rc = ost_setattr(req->rq_export, req, oti);
1667                 break;
1668         case OST_WRITE:
1669                 CDEBUG(D_INODE, "write\n");
1670                 /* req->rq_request_portal would be nice, if it was set */
1671                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1672                         CERROR("%s: deny write request from %s to portal %u\n",
1673                                req->rq_export->exp_obd->obd_name,
1674                                obd_export_nid2str(req->rq_export),
1675                                req->rq_rqbd->rqbd_service->srv_req_portal);
1676                         GOTO(out, rc = -EPROTO);
1677                 }
1678                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1679                         RETURN(0);
1680                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1681                         GOTO(out, rc = -ENOSPC);
1682                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1683                         GOTO(out, rc = -EROFS);
1684                 rc = ost_brw_write(req, oti);
1685                 LASSERT(current->journal_info == NULL);
1686                 /* ost_brw_write sends its own replies */
1687                 RETURN(rc);
1688         case OST_READ:
1689                 CDEBUG(D_INODE, "read\n");
1690                 /* req->rq_request_portal would be nice, if it was set */
1691                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1692                         CERROR("%s: deny read request from %s to portal %u\n",
1693                                req->rq_export->exp_obd->obd_name,
1694                                obd_export_nid2str(req->rq_export),
1695                                req->rq_rqbd->rqbd_service->srv_req_portal);
1696                         GOTO(out, rc = -EPROTO);
1697                 }
1698                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1699                         RETURN(0);
1700                 rc = ost_brw_read(req, oti);
1701                 LASSERT(current->journal_info == NULL);
1702                 /* ost_brw_read sends its own replies */
1703                 RETURN(rc);
1704         case OST_PUNCH:
1705                 CDEBUG(D_INODE, "punch\n");
1706                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
1707                         RETURN(0);
1708                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1709                         GOTO(out, rc = -EROFS);
1710                 rc = ost_punch(req->rq_export, req, oti);
1711                 break;
1712         case OST_STATFS:
1713                 CDEBUG(D_INODE, "statfs\n");
1714                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
1715                         RETURN(0);
1716                 rc = ost_statfs(req);
1717                 break;
1718         case OST_SYNC:
1719                 CDEBUG(D_INODE, "sync\n");
1720                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
1721                         RETURN(0);
1722                 rc = ost_sync(req->rq_export, req);
1723                 break;
1724         case OST_SET_INFO:
1725                 DEBUG_REQ(D_INODE, req, "set_info");
1726                 req_capsule_set(&req->rq_pill, &RQF_OST_SET_INFO);
1727                 rc = ost_set_info(req->rq_export, req);
1728                 break;
1729         case OST_GET_INFO:
1730                 DEBUG_REQ(D_INODE, req, "get_info");
1731                 rc = ost_get_info(req->rq_export, req);
1732                 break;
1733         case OST_QUOTACHECK:
1734                 CDEBUG(D_INODE, "quotacheck\n");
1735                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
1736                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
1737                         RETURN(0);
1738                 rc = ost_handle_quotacheck(req);
1739                 break;
1740         case OST_QUOTACTL:
1741                 CDEBUG(D_INODE, "quotactl\n");
1742                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
1743                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
1744                         RETURN(0);
1745                 rc = ost_handle_quotactl(req);
1746                 break;
1747         case OBD_PING:
1748                 DEBUG_REQ(D_INODE, req, "ping");
1749                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
1750                 rc = target_handle_ping(req);
1751                 break;
1752         /* FIXME - just reply status */
1753         case LLOG_ORIGIN_CONNECT:
1754                 DEBUG_REQ(D_INODE, req, "log connect");
1755                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
1756                 rc = ost_llog_handle_connect(req->rq_export, req);
1757                 req->rq_status = rc;
1758                 rc = req_capsule_server_pack(&req->rq_pill);
1759                 if (rc)
1760                         RETURN(rc);
1761                 RETURN(ptlrpc_reply(req));
1762         case OBD_LOG_CANCEL:
1763                 CDEBUG(D_INODE, "log cancel\n");
1764                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1765                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1766                         RETURN(0);
1767                 rc = llog_origin_handle_cancel(req);
1768                 req->rq_status = rc;
1769                 rc = req_capsule_server_pack(&req->rq_pill);
1770                 if (rc)
1771                         RETURN(rc);
1772                 RETURN(ptlrpc_reply(req));
1773         case LDLM_ENQUEUE:
1774                 CDEBUG(D_INODE, "enqueue\n");
1775                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
1776                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
1777                         RETURN(0);
1778                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1779                                          ldlm_server_blocking_ast,
1780                                          ldlm_server_glimpse_ast);
1781                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1782                 break;
1783         case LDLM_CONVERT:
1784                 CDEBUG(D_INODE, "convert\n");
1785                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
1786                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
1787                         RETURN(0);
1788                 rc = ldlm_handle_convert(req);
1789                 break;
1790         case LDLM_CANCEL:
1791                 CDEBUG(D_INODE, "cancel\n");
1792                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1793                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1794                         RETURN(0);
1795                 rc = ldlm_handle_cancel(req);
1796                 break;
1797         case LDLM_BL_CALLBACK:
1798         case LDLM_CP_CALLBACK:
1799                 CDEBUG(D_INODE, "callback\n");
1800                 CERROR("callbacks should not happen on OST\n");
1801                 /* fall through */
1802         default:
1803                 CERROR("Unexpected opcode %d\n",
1804                        lustre_msg_get_opc(req->rq_reqmsg));
1805                 req->rq_status = -ENOTSUPP;
1806                 rc = ptlrpc_error(req);
1807                 RETURN(rc);
1808         }
1809
1810         LASSERT(current->journal_info == NULL);
1811
1812         EXIT;
1813         /* If we're DISCONNECTing, the export_data is already freed */
1814         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1815                 target_committed_to_req(req);
1816
1817 out:
1818         if (!rc)
1819                 oti_to_request(oti, req);
1820
1821         target_send_reply(req, rc, fail);
1822         return 0;
1823 }
1824 EXPORT_SYMBOL(ost_handle);
1825 /*
1826  * free per-thread pool created by ost_thread_init().
1827  */
1828 static void ost_thread_done(struct ptlrpc_thread *thread)
1829 {
1830         int i;
1831         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1832                                              * Storage */
1833
1834         ENTRY;
1835
1836         LASSERT(thread != NULL);
1837
1838         /*
1839          * be prepared to handle partially-initialized pools (because this is
1840          * called from ost_thread_init() for cleanup.
1841          */
1842         tls = thread->t_data;
1843         if (tls != NULL) {
1844                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1845                         if (tls->page[i] != NULL)
1846                                 OBD_PAGE_FREE(tls->page[i]);
1847                 }
1848                 OBD_FREE_PTR(tls);
1849                 thread->t_data = NULL;
1850         }
1851         EXIT;
1852 }
1853
1854 /*
1855  * initialize per-thread page pool (bug 5137).
1856  */
1857 static int ost_thread_init(struct ptlrpc_thread *thread)
1858 {
1859         int result;
1860         int i;
1861         struct ost_thread_local_cache *tls;
1862
1863         ENTRY;
1864
1865         LASSERT(thread != NULL);
1866         LASSERT(thread->t_data == NULL);
1867         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
1868
1869         OBD_ALLOC_PTR(tls);
1870         if (tls != NULL) {
1871                 result = 0;
1872                 thread->t_data = tls;
1873                 /*
1874                  * populate pool
1875                  */
1876                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1877                         OBD_PAGE_ALLOC(tls->page[i], OST_THREAD_POOL_GFP);
1878                         if (tls->page[i] == NULL) {
1879                                 ost_thread_done(thread);
1880                                 result = -ENOMEM;
1881                                 break;
1882                         }
1883                 }
1884         } else
1885                 result = -ENOMEM;
1886         RETURN(result);
1887 }
1888
1889 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
1890
1891 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1892 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
1893 {
1894         struct ost_obd *ost = &obd->u.ost;
1895         struct lprocfs_static_vars lvars;
1896         int oss_min_threads;
1897         int oss_max_threads;
1898         int oss_min_create_threads;
1899         int oss_max_create_threads;
1900         int rc;
1901         ENTRY;
1902
1903         rc = cleanup_group_info();
1904         if (rc)
1905                 RETURN(rc);
1906
1907         lprocfs_ost_init_vars(&lvars);
1908         lprocfs_obd_setup(obd, lvars.obd_vars);
1909
1910         sema_init(&ost->ost_health_sem, 1);
1911
1912         if (oss_num_threads) {
1913                 /* If oss_num_threads is set, it is the min and the max. */
1914                 if (oss_num_threads > OSS_THREADS_MAX) 
1915                         oss_num_threads = OSS_THREADS_MAX;
1916                 if (oss_num_threads < OSS_THREADS_MIN)
1917                         oss_num_threads = OSS_THREADS_MIN;
1918                 oss_max_threads = oss_min_threads = oss_num_threads;
1919         } else {
1920                 /* Base min threads on memory and cpus */
1921                 oss_min_threads = num_possible_cpus() * num_physpages >> 
1922                         (27 - CFS_PAGE_SHIFT);
1923                 if (oss_min_threads < OSS_THREADS_MIN)
1924                         oss_min_threads = OSS_THREADS_MIN;
1925                 /* Insure a 4x range for dynamic threads */
1926                 if (oss_min_threads > OSS_THREADS_MAX / 4) 
1927                         oss_min_threads = OSS_THREADS_MAX / 4;
1928                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1929         }
1930
1931         ost->ost_service =
1932                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1933                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1934                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1935                                 ost_handle, LUSTRE_OSS_NAME,
1936                                 obd->obd_proc_entry, target_print_req,
1937                                 oss_min_threads, oss_max_threads,
1938                                 "ll_ost", LCT_DT_THREAD);
1939         if (ost->ost_service == NULL) {
1940                 CERROR("failed to start service\n");
1941                 GOTO(out_lprocfs, rc = -ENOMEM);
1942         }
1943
1944         rc = ptlrpc_start_threads(obd, ost->ost_service);
1945         if (rc)
1946                 GOTO(out_service, rc = -EINVAL);
1947
1948         if (oss_num_create_threads) {
1949                 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
1950                         oss_num_create_threads = OSS_MAX_CREATE_THREADS;
1951                 if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
1952                         oss_num_create_threads = OSS_MIN_CREATE_THREADS;
1953                 oss_min_create_threads = oss_max_create_threads =
1954                         oss_num_create_threads;
1955         } else {
1956                 oss_min_create_threads = OSS_MIN_CREATE_THREADS;
1957                 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
1958         }
1959
1960         ost->ost_create_service =
1961                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1962                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1963                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1964                                 ost_handle, "ost_create",
1965                                 obd->obd_proc_entry, target_print_req,
1966                                 oss_min_create_threads, oss_max_create_threads,
1967                                 "ll_ost_creat", LCT_DT_THREAD);
1968         if (ost->ost_create_service == NULL) {
1969                 CERROR("failed to start OST create service\n");
1970                 GOTO(out_service, rc = -ENOMEM);
1971         }
1972
1973         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1974         if (rc)
1975                 GOTO(out_create, rc = -EINVAL);
1976
1977         ost->ost_io_service =
1978                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1979                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1980                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1981                                 ost_handle, "ost_io",
1982                                 obd->obd_proc_entry, target_print_req,
1983                                 oss_min_threads, oss_max_threads,
1984                                 "ll_ost_io", LCT_DT_THREAD);
1985         if (ost->ost_io_service == NULL) {
1986                 CERROR("failed to start OST I/O service\n");
1987                 GOTO(out_create, rc = -ENOMEM);
1988         }
1989
1990         ost->ost_io_service->srv_init = ost_thread_init;
1991         ost->ost_io_service->srv_done = ost_thread_done;
1992         ost->ost_io_service->srv_cpu_affinity = 1;
1993         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1994         if (rc)
1995                 GOTO(out_io, rc = -EINVAL);
1996
1997         ping_evictor_start();
1998
1999         RETURN(0);
2000
2001 out_io:
2002         ptlrpc_unregister_service(ost->ost_io_service);
2003         ost->ost_io_service = NULL;
2004 out_create:
2005         ptlrpc_unregister_service(ost->ost_create_service);
2006         ost->ost_create_service = NULL;
2007 out_service:
2008         ptlrpc_unregister_service(ost->ost_service);
2009         ost->ost_service = NULL;
2010 out_lprocfs:
2011         lprocfs_obd_cleanup(obd);
2012         RETURN(rc);
2013 }
2014
2015 static int ost_cleanup(struct obd_device *obd)
2016 {
2017         struct ost_obd *ost = &obd->u.ost;
2018         int err = 0;
2019         ENTRY;
2020
2021         ping_evictor_stop();
2022
2023         spin_lock_bh(&obd->obd_processing_task_lock);
2024         if (obd->obd_recovering) {
2025                 target_cancel_recovery_timer(obd);
2026                 obd->obd_recovering = 0;
2027         }
2028         spin_unlock_bh(&obd->obd_processing_task_lock);
2029
2030         down(&ost->ost_health_sem);
2031         ptlrpc_unregister_service(ost->ost_service);
2032         ptlrpc_unregister_service(ost->ost_create_service);
2033         ptlrpc_unregister_service(ost->ost_io_service);
2034         ost->ost_service = NULL;
2035         ost->ost_create_service = NULL;
2036         up(&ost->ost_health_sem);
2037
2038         lprocfs_obd_cleanup(obd);
2039
2040         RETURN(err);
2041 }
2042
2043 static int ost_health_check(struct obd_device *obd)
2044 {
2045         struct ost_obd *ost = &obd->u.ost;
2046         int rc = 0;
2047
2048         down(&ost->ost_health_sem);
2049         rc |= ptlrpc_service_health_check(ost->ost_service);
2050         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2051         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2052         up(&ost->ost_health_sem);
2053
2054         /*
2055          * health_check to return 0 on healthy
2056          * and 1 on unhealthy.
2057          */
2058         if( rc != 0)
2059                 rc = 1;
2060
2061         return rc;
2062 }
2063
2064 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2065 {
2066         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2067 }
2068
2069 /* use obd ops to offer management infrastructure */
2070 static struct obd_ops ost_obd_ops = {
2071         .o_owner        = THIS_MODULE,
2072         .o_setup        = ost_setup,
2073         .o_cleanup      = ost_cleanup,
2074         .o_health_check = ost_health_check,
2075 };
2076
2077
2078 static int __init ost_init(void)
2079 {
2080         struct lprocfs_static_vars lvars;
2081         int rc;
2082         ENTRY;
2083
2084         lprocfs_ost_init_vars(&lvars);
2085         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2086                                  LUSTRE_OSS_NAME, NULL);
2087
2088         if (ost_num_threads != 0 && oss_num_threads == 0) {
2089                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2090                               "use oss_num_threads instead or unset both for "
2091                               "dynamic thread startup\n");
2092                 oss_num_threads = ost_num_threads;
2093         }
2094
2095         RETURN(rc);
2096 }
2097
2098 static void /*__exit*/ ost_exit(void)
2099 {
2100         class_unregister_type(LUSTRE_OSS_NAME);
2101 }
2102
2103 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2104 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2105 MODULE_LICENSE("GPL");
2106
2107 module_init(ost_init);
2108 module_exit(ost_exit);