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