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