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