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