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