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