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