Whamcloud - gitweb
b=20530
[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 ?
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 ?
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 (rc == -ENOTCONN)
1090                 /* quota acquire process has been given up because
1091                  * either the client has been evicted or the client
1092                  * has timed out the request already */
1093                 no_reply = 1;
1094
1095         if (exp_connect_rmtclient(exp)) {
1096                 repbody->oa.o_uid = o_uid;
1097                 repbody->oa.o_gid = o_gid;
1098         }
1099
1100         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1101                 int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1102                 char *msg;
1103                 char *via;
1104                 char *router;
1105
1106                 if (new_cksum == server_cksum)
1107                         msg = "changed in transit before arrival at OST";
1108                 else if (new_cksum == client_cksum)
1109                         msg = "initial checksum before message complete";
1110                 else
1111                         msg = "changed in transit AND after initial checksum";
1112
1113                 if (req->rq_peer.nid == desc->bd_sender) {
1114                         via = router = "";
1115                 } else {
1116                         via = " via ";
1117                         router = libcfs_nid2str(desc->bd_sender);
1118                 }
1119
1120                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1121                                    "%s%s%s inum "LPU64"/"LPU64" object "
1122                                    LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1123                                    exp->exp_obd->obd_name, msg,
1124                                    libcfs_id2str(req->rq_peer),
1125                                    via, router,
1126                                    body->oa.o_valid & OBD_MD_FLFID ?
1127                                                 body->oa.o_fid : (__u64)0,
1128                                    body->oa.o_valid & OBD_MD_FLFID ?
1129                                                 body->oa.o_generation :(__u64)0,
1130                                    body->oa.o_id,
1131                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1132                                                 body->oa.o_gr : (__u64)0,
1133                                    local_nb[0].offset,
1134                                    local_nb[npages-1].offset +
1135                                    local_nb[npages-1].len - 1 );
1136                 CERROR("client csum %x, original server csum %x, "
1137                        "server csum now %x\n",
1138                        client_cksum, server_cksum, new_cksum);
1139         }
1140
1141         if (rc == 0) {
1142                 int nob = 0;
1143
1144                 /* set per-requested niobuf return codes */
1145                 for (i = j = 0; i < niocount; i++) {
1146                         int len = remote_nb[i].len;
1147
1148                         nob += len;
1149                         rcs[i] = 0;
1150                         do {
1151                                 LASSERT(j < npages);
1152                                 if (local_nb[j].rc < 0)
1153                                         rcs[i] = local_nb[j].rc;
1154                                 len -= local_nb[j].len;
1155                                 j++;
1156                         } while (len > 0);
1157                         LASSERT(len == 0);
1158                 }
1159                 LASSERT(j == npages);
1160                 ptlrpc_lprocfs_brw(req, nob);
1161         }
1162
1163 out_lock:
1164         ost_brw_lock_put(LCK_PW, ioo, remote_nb, &lockh);
1165 out_bulk:
1166         if (desc)
1167                 ptlrpc_free_bulk(desc);
1168 out:
1169         if (rc == 0) {
1170                 oti_to_request(oti, req);
1171                 target_committed_to_req(req);
1172                 rc = ptlrpc_reply(req);
1173         } else if (!no_reply) {
1174                 /* Only reply if there was no comms problem with bulk */
1175                 target_committed_to_req(req);
1176                 req->rq_status = rc;
1177                 ptlrpc_error(req);
1178         } else {
1179                 /* reply out callback would free */
1180                 ptlrpc_req_drop_rs(req);
1181                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1182                       "client will retry\n",
1183                       exp->exp_obd->obd_name,
1184                       exp->exp_client_uuid.uuid,
1185                       exp->exp_connection->c_remote_uuid.uuid,
1186                       libcfs_id2str(req->rq_peer));
1187         }
1188         libcfs_memory_pressure_clr();
1189         RETURN(rc);
1190 }
1191
1192 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1193 {
1194         struct ost_body *body = NULL, *repbody;
1195         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1196         char *key, *val = NULL;
1197         int keylen, vallen, rc = 0;
1198         ENTRY;
1199
1200         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1201         if (key == NULL) {
1202                 DEBUG_REQ(D_HA, req, "no set_info key");
1203                 RETURN(-EFAULT);
1204         }
1205         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1206
1207         if (KEY_IS(KEY_GRANT_SHRINK)) {
1208                 rc = lustre_pack_reply(req, 2, size, NULL);
1209                 if (rc)
1210                         RETURN(rc);
1211         } else {
1212                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1213                 if (rc)
1214                         RETURN(rc);
1215         }
1216
1217         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1218         if (vallen) {
1219                 if (KEY_IS(KEY_GRANT_SHRINK)) {
1220                         body = lustre_swab_reqbuf(req, REQ_REC_OFF + 1,
1221                                                   sizeof(*body),
1222                                                   lustre_swab_ost_body);
1223                         if (!body)
1224                                 RETURN(-EFAULT);
1225
1226                         repbody = lustre_msg_buf(req->rq_repmsg,
1227                                                  REPLY_REC_OFF,
1228                                                  sizeof(*repbody));
1229                         memcpy(repbody, body, sizeof(*body));
1230                         val = (char*)repbody;
1231                 } else
1232                         val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,0);
1233         }
1234
1235         if (KEY_IS(KEY_EVICT_BY_NID)) {
1236                 if (val && vallen)
1237                         obd_export_evict_by_nid(exp->exp_obd, val);
1238                 GOTO(out, rc = 0);
1239         } else if (KEY_IS(KEY_MDS_CONN) && ptlrpc_req_need_swab(req)) {
1240                 /* Val's are not swabbed automatically */
1241                 __swab32s((__u32 *)val);
1242         }
1243
1244         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1245 out:
1246         lustre_msg_set_status(req->rq_repmsg, 0);
1247         RETURN(rc);
1248 }
1249
1250 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1251 {
1252         void *key, *reply;
1253         int keylen, replylen, rc = 0;
1254         struct req_capsule *pill = &req->rq_pill;
1255         ENTRY;
1256
1257         req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
1258
1259         /* this common part for get_info rpc */
1260         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1261         if (key == NULL) {
1262                 DEBUG_REQ(D_HA, req, "no get_info key");
1263                 RETURN(-EFAULT);
1264         }
1265         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1266
1267         rc = obd_get_info(exp, keylen, key, &replylen, NULL, NULL);
1268         if (rc)
1269                 RETURN(rc);
1270
1271         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1272                              RCL_SERVER, replylen);
1273
1274         rc = req_capsule_server_pack(pill);
1275         if (rc)
1276                 RETURN(rc);
1277
1278         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1279         if (reply == NULL)
1280                 RETURN(-ENOMEM);
1281
1282         /* call again to fill in the reply buffer */
1283         rc = obd_get_info(exp, keylen, key, &replylen, reply, NULL);
1284
1285         lustre_msg_set_status(req->rq_repmsg, 0);
1286         RETURN(rc);
1287 }
1288
1289 #ifdef HAVE_QUOTA_SUPPORT
1290 static int ost_handle_quotactl(struct ptlrpc_request *req)
1291 {
1292         struct obd_quotactl *oqctl, *repoqc;
1293         int rc;
1294         ENTRY;
1295
1296         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1297         if (oqctl == NULL)
1298                 GOTO(out, rc = -EPROTO);
1299
1300         rc = req_capsule_server_pack(&req->rq_pill);
1301         if (rc)
1302                 GOTO(out, rc);
1303
1304         repoqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1305         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1306         *repoqc = *oqctl;
1307
1308 out:
1309         RETURN(rc);
1310 }
1311
1312 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1313 {
1314         struct obd_quotactl *oqctl;
1315         int rc;
1316         ENTRY;
1317
1318         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1319         if (oqctl == NULL)
1320                 RETURN(-EPROTO);
1321
1322         rc = req_capsule_server_pack(&req->rq_pill);
1323         if (rc)
1324                 RETURN(-ENOMEM);
1325
1326         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1327         RETURN(0);
1328 }
1329
1330 static int ost_handle_quota_adjust_qunit(struct ptlrpc_request *req)
1331 {
1332         struct quota_adjust_qunit *oqaq, *repoqa;
1333         struct lustre_quota_ctxt *qctxt;
1334         int rc;
1335         ENTRY;
1336
1337         qctxt = &req->rq_export->exp_obd->u.obt.obt_qctxt;
1338         oqaq = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1339         if (oqaq == NULL)
1340                 GOTO(out, rc = -EPROTO);
1341
1342         rc = req_capsule_server_pack(&req->rq_pill);
1343         if (rc)
1344                 GOTO(out, rc);
1345
1346         repoqa = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1347         req->rq_status = obd_quota_adjust_qunit(req->rq_export, oqaq, qctxt);
1348         *repoqa = *oqaq;
1349
1350  out:
1351         RETURN(rc);
1352 }
1353 #endif
1354
1355 static int ost_llog_handle_connect(struct obd_export *exp,
1356                                    struct ptlrpc_request *req)
1357 {
1358         struct llogd_conn_body *body;
1359         int rc;
1360         ENTRY;
1361
1362         body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*body));
1363         rc = obd_llog_connect(exp, body);
1364         RETURN(rc);
1365 }
1366
1367 #define ost_init_sec_none(reply, exp)                                   \
1368 do {                                                                    \
1369         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
1370                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
1371                                       OBD_CONNECT_OSS_CAPA);            \
1372         spin_lock(&exp->exp_lock);                                      \
1373         exp->exp_connect_flags = reply->ocd_connect_flags;              \
1374         spin_unlock(&exp->exp_lock);                                    \
1375 } while (0)
1376
1377 static int ost_init_sec_level(struct ptlrpc_request *req)
1378 {
1379         struct obd_export *exp = req->rq_export;
1380         struct req_capsule *pill = &req->rq_pill;
1381         struct obd_device *obd = exp->exp_obd;
1382         struct filter_obd *filter = &obd->u.filter;
1383         char *client = libcfs_nid2str(req->rq_peer.nid);
1384         struct obd_connect_data *data, *reply;
1385         int rc = 0, remote;
1386         ENTRY;
1387
1388         data = req_capsule_client_get(pill, &RMF_CONNECT_DATA);
1389         reply = req_capsule_server_get(pill, &RMF_CONNECT_DATA);
1390         if (data == NULL || reply == NULL)
1391                 RETURN(-EFAULT);
1392
1393         /* connection from MDT is always trusted */
1394         if (req->rq_auth_usr_mdt) {
1395                 ost_init_sec_none(reply, exp);
1396                 RETURN(0);
1397         }
1398
1399         /* no GSS support case */
1400         if (!req->rq_auth_gss) {
1401                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1402                         CWARN("client %s -> target %s does not user GSS, "
1403                               "can not run under security level %d.\n",
1404                               client, obd->obd_name, filter->fo_sec_level);
1405                         RETURN(-EACCES);
1406                 } else {
1407                         ost_init_sec_none(reply, exp);
1408                         RETURN(0);
1409                 }
1410         }
1411
1412         /* old version case */
1413         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
1414                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
1415                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1416                         CWARN("client %s -> target %s uses old version, "
1417                               "can not run under security level %d.\n",
1418                               client, obd->obd_name, filter->fo_sec_level);
1419                         RETURN(-EACCES);
1420                 } else {
1421                         CWARN("client %s -> target %s uses old version, "
1422                               "run under security level %d.\n",
1423                               client, obd->obd_name, filter->fo_sec_level);
1424                         ost_init_sec_none(reply, exp);
1425                         RETURN(0);
1426                 }
1427         }
1428
1429         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
1430         if (remote) {
1431                 if (!req->rq_auth_remote)
1432                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
1433                                "asked to be remote.\n", client, obd->obd_name);
1434         } else if (req->rq_auth_remote) {
1435                 remote = 1;
1436                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
1437                        "as remote by default.\n", client, obd->obd_name);
1438         }
1439
1440         if (remote) {
1441                 if (!filter->fo_fl_oss_capa) {
1442                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
1443                                " but OSS capabilities are not enabled: %d.\n",
1444                                client, obd->obd_name, filter->fo_fl_oss_capa);
1445                         RETURN(-EACCES);
1446                 }
1447         }
1448
1449         switch (filter->fo_sec_level) {
1450         case LUSTRE_SEC_NONE:
1451                 if (!remote) {
1452                         ost_init_sec_none(reply, exp);
1453                         break;
1454                 } else {
1455                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
1456                                "can not run under security level %d.\n",
1457                                client, obd->obd_name, filter->fo_sec_level);
1458                         RETURN(-EACCES);
1459                 }
1460         case LUSTRE_SEC_REMOTE:
1461                 if (!remote)
1462                         ost_init_sec_none(reply, exp);
1463                 break;
1464         case LUSTRE_SEC_ALL:
1465                 if (!remote) {
1466                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
1467                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
1468                         if (!filter->fo_fl_oss_capa)
1469                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
1470
1471                         spin_lock(&exp->exp_lock);
1472                         exp->exp_connect_flags = reply->ocd_connect_flags;
1473                         spin_unlock(&exp->exp_lock);
1474                 }
1475                 break;
1476         default:
1477                 RETURN(-EINVAL);
1478         }
1479
1480         RETURN(rc);
1481 }
1482
1483 /*
1484  * FIXME
1485  * this should be done in filter_connect()/filter_reconnect(), but
1486  * we can't obtain information like NID, which stored in incoming
1487  * request, thus can't decide what flavor to use. so we do it here.
1488  *
1489  * This hack should be removed after the OST stack be rewritten, just
1490  * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1491  */
1492 static int ost_connect_check_sptlrpc(struct ptlrpc_request *req)
1493 {
1494         struct obd_export     *exp = req->rq_export;
1495         struct filter_obd     *filter = &exp->exp_obd->u.filter;
1496         struct sptlrpc_flavor  flvr;
1497         int                    rc = 0;
1498
1499         if (unlikely(strcmp(exp->exp_obd->obd_type->typ_name,
1500                             LUSTRE_ECHO_NAME) == 0)) {
1501                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
1502                 return 0;
1503         }
1504
1505         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
1506                 read_lock(&filter->fo_sptlrpc_lock);
1507                 sptlrpc_target_choose_flavor(&filter->fo_sptlrpc_rset,
1508                                              req->rq_sp_from,
1509                                              req->rq_peer.nid,
1510                                              &flvr);
1511                 read_unlock(&filter->fo_sptlrpc_lock);
1512
1513                 spin_lock(&exp->exp_lock);
1514
1515                 exp->exp_sp_peer = req->rq_sp_from;
1516                 exp->exp_flvr = flvr;
1517
1518                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
1519                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1520                         CERROR("unauthorized rpc flavor %x from %s, "
1521                                "expect %x\n", req->rq_flvr.sf_rpc,
1522                                libcfs_nid2str(req->rq_peer.nid),
1523                                exp->exp_flvr.sf_rpc);
1524                         rc = -EACCES;
1525                 }
1526
1527                 spin_unlock(&exp->exp_lock);
1528         } else {
1529                 if (exp->exp_sp_peer != req->rq_sp_from) {
1530                         CERROR("RPC source %s doesn't match %s\n",
1531                                sptlrpc_part2name(req->rq_sp_from),
1532                                sptlrpc_part2name(exp->exp_sp_peer));
1533                         rc = -EACCES;
1534                 } else {
1535                         rc = sptlrpc_target_export_check(exp, req);
1536                 }
1537         }
1538
1539         return rc;
1540 }
1541
1542 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1543                                        struct obd_device *obd, int *process)
1544 {
1545         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1546         case OST_CONNECT: /* This will never get here, but for completeness. */
1547         case OST_DISCONNECT:
1548                *process = 1;
1549                RETURN(0);
1550
1551         case OBD_PING:
1552         case OST_CREATE:
1553         case OST_DESTROY:
1554         case OST_PUNCH:
1555         case OST_SETATTR:
1556         case OST_SYNC:
1557         case OST_WRITE:
1558         case OBD_LOG_CANCEL:
1559         case LDLM_ENQUEUE:
1560                 *process = target_queue_recovery_request(req, obd);
1561                 RETURN(0);
1562
1563         default:
1564                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1565                 *process = -EAGAIN;
1566                 RETURN(0);
1567         }
1568 }
1569
1570 int ost_msg_check_version(struct lustre_msg *msg)
1571 {
1572         int rc;
1573
1574         switch(lustre_msg_get_opc(msg)) {
1575         case OST_CONNECT:
1576         case OST_DISCONNECT:
1577         case OBD_PING:
1578         case SEC_CTX_INIT:
1579         case SEC_CTX_INIT_CONT:
1580         case SEC_CTX_FINI:
1581                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1582                 if (rc)
1583                         CERROR("bad opc %u version %08x, expecting %08x\n",
1584                                lustre_msg_get_opc(msg),
1585                                lustre_msg_get_version(msg),
1586                                LUSTRE_OBD_VERSION);
1587                 break;
1588         case OST_CREATE:
1589         case OST_DESTROY:
1590         case OST_GETATTR:
1591         case OST_SETATTR:
1592         case OST_WRITE:
1593         case OST_READ:
1594         case OST_PUNCH:
1595         case OST_STATFS:
1596         case OST_SYNC:
1597         case OST_SET_INFO:
1598         case OST_GET_INFO:
1599 #ifdef HAVE_QUOTA_SUPPORT
1600         case OST_QUOTACHECK:
1601         case OST_QUOTACTL:
1602         case OST_QUOTA_ADJUST_QUNIT:
1603 #endif
1604                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1605                 if (rc)
1606                         CERROR("bad opc %u version %08x, expecting %08x\n",
1607                                lustre_msg_get_opc(msg),
1608                                lustre_msg_get_version(msg),
1609                                LUSTRE_OST_VERSION);
1610                 break;
1611         case LDLM_ENQUEUE:
1612         case LDLM_CONVERT:
1613         case LDLM_CANCEL:
1614         case LDLM_BL_CALLBACK:
1615         case LDLM_CP_CALLBACK:
1616                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1617                 if (rc)
1618                         CERROR("bad opc %u version %08x, expecting %08x\n",
1619                                lustre_msg_get_opc(msg),
1620                                lustre_msg_get_version(msg),
1621                                LUSTRE_DLM_VERSION);
1622                 break;
1623         case LLOG_ORIGIN_CONNECT:
1624         case OBD_LOG_CANCEL:
1625                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1626                 if (rc)
1627                         CERROR("bad opc %u version %08x, expecting %08x\n",
1628                                lustre_msg_get_opc(msg),
1629                                lustre_msg_get_version(msg),
1630                                LUSTRE_LOG_VERSION);
1631                 break;
1632         default:
1633                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1634                 rc = -ENOTSUPP;
1635         }
1636         return rc;
1637 }
1638
1639 static int ost_rw_hpreq_lock_match(struct ptlrpc_request *req,
1640                                    struct ldlm_lock *lock)
1641 {
1642         struct niobuf_remote *nb;
1643         struct obd_ioobj *ioo;
1644         struct ost_body *body;
1645         int objcount, niocount;
1646         int mode, opc, i;
1647         __u64 start, end;
1648         ENTRY;
1649
1650         opc = lustre_msg_get_opc(req->rq_reqmsg);
1651         LASSERT(opc == OST_READ || opc == OST_WRITE);
1652
1653         /* As the request may be covered by several locks, do not look at
1654          * o_handle, look at the RPC IO region. */
1655         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
1656                                   lustre_swab_obdo);
1657         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
1658                 sizeof(*ioo);
1659         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
1660                              objcount * sizeof(*ioo));
1661         LASSERT(ioo != NULL);
1662         for (niocount = i = 0; i < objcount; i++)
1663                 niocount += ioo[i].ioo_bufcnt;
1664
1665         nb = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2,
1666                             niocount * sizeof(*nb));
1667         LASSERT(nb != NULL);
1668
1669         mode = LCK_PW;
1670         if (opc == OST_READ)
1671                 mode |= LCK_PR;
1672
1673         start = nb[0].offset & CFS_PAGE_MASK;
1674         end = (nb[ioo->ioo_bufcnt - 1].offset +
1675                nb[ioo->ioo_bufcnt - 1].len - 1) | ~CFS_PAGE_MASK;
1676
1677         LASSERT(lock->l_resource != NULL);
1678         if (!osc_res_name_eq(ioo->ioo_id, ioo->ioo_gr,
1679                              &lock->l_resource->lr_name))
1680                 RETURN(0);
1681
1682         if (!(lock->l_granted_mode & mode))
1683                 RETURN(0);
1684
1685         if (lock->l_policy_data.l_extent.end < start ||
1686             lock->l_policy_data.l_extent.start > end)
1687                 RETURN(0);
1688
1689         RETURN(1);
1690 }
1691
1692 /**
1693  * Swab buffers needed to call ost_rw_prolong_locks() and call it.
1694  * Return the value from ost_rw_prolong_locks() which is non-zero if
1695  * there is a cancelled lock which is waiting for this IO request.
1696  */
1697 static int ost_rw_hpreq_check(struct ptlrpc_request *req)
1698 {
1699         struct niobuf_remote *nb;
1700         struct obd_ioobj *ioo;
1701         struct ost_body *body;
1702         int objcount, niocount;
1703         int mode, opc, i;
1704         ENTRY;
1705
1706         opc = lustre_msg_get_opc(req->rq_reqmsg);
1707         LASSERT(opc == OST_READ || opc == OST_WRITE);
1708
1709         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1710         LASSERT(body != NULL);
1711
1712         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
1713                 sizeof(*ioo);
1714         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
1715                              objcount * sizeof(*ioo));
1716         LASSERT(ioo != NULL);
1717
1718         for (niocount = i = 0; i < objcount; i++)
1719                 niocount += ioo[i].ioo_bufcnt;
1720         nb = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2,
1721                             niocount * sizeof(*nb));
1722         LASSERT(nb != NULL);
1723         LASSERT(niocount == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK));
1724
1725         mode = LCK_PW;
1726         if (opc == OST_READ)
1727                 mode |= LCK_PR;
1728         RETURN(ost_rw_prolong_locks(req, ioo, nb, &body->oa, mode));
1729 }
1730
1731 static int ost_punch_prolong_locks(struct ptlrpc_request *req, struct obdo *oa)
1732 {
1733         struct ldlm_res_id res_id = { .name = { oa->o_id } };
1734         struct ost_prolong_data opd = { 0 };
1735         __u64 start, end;
1736         ENTRY;
1737
1738         start = oa->o_size;
1739         end = start + oa->o_blocks;
1740
1741         opd.opd_mode = LCK_PW;
1742         opd.opd_exp = req->rq_export;
1743         opd.opd_policy.l_extent.start = start & CFS_PAGE_MASK;
1744         if (oa->o_blocks == OBD_OBJECT_EOF || end < start)
1745                 opd.opd_policy.l_extent.end = OBD_OBJECT_EOF;
1746         else
1747                 opd.opd_policy.l_extent.end = end | ~CFS_PAGE_MASK;
1748
1749         /* prolong locks for the current service time of the corresponding
1750          * portal (= OST_IO_PORTAL) */
1751         opd.opd_timeout = AT_OFF ? obd_timeout / 2:
1752                           max(at_est2timeout(at_get(&req->rq_rqbd->
1753                               rqbd_service->srv_at_estimate)), ldlm_timeout);
1754
1755         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1756                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
1757                opd.opd_policy.l_extent.end);
1758
1759         opd.opd_oa = oa;
1760         ldlm_resource_iterate(req->rq_export->exp_obd->obd_namespace, &res_id,
1761                               ost_prolong_locks_iter, &opd);
1762         RETURN(opd.opd_lock_match);
1763 }
1764
1765 static int ost_punch_hpreq_lock_match(struct ptlrpc_request *req,
1766                                       struct ldlm_lock *lock)
1767 {
1768         struct ost_body *body;
1769         ENTRY;
1770
1771         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
1772                                   lustre_swab_obdo);
1773         LASSERT(body != NULL);
1774
1775         if (body->oa.o_valid & OBD_MD_FLHANDLE &&
1776             body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1777                 RETURN(1);
1778         RETURN(0);
1779 }
1780
1781 static int ost_punch_hpreq_check(struct ptlrpc_request *req)
1782 {
1783         struct ost_body *body = lustre_msg_buf(req->rq_reqmsg,
1784                                                REQ_REC_OFF, sizeof(*body));
1785         LASSERT(body != NULL);
1786         LASSERT(!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
1787                 !(body->oa.o_flags & OBD_FL_TRUNCLOCK));
1788
1789         RETURN(ost_punch_prolong_locks(req, &body->oa));
1790 }
1791
1792 struct ptlrpc_hpreq_ops ost_hpreq_rw = {
1793         .hpreq_lock_match  = ost_rw_hpreq_lock_match,
1794         .hpreq_check       = ost_rw_hpreq_check,
1795 };
1796
1797 struct ptlrpc_hpreq_ops ost_hpreq_punch = {
1798         .hpreq_lock_match  = ost_punch_hpreq_lock_match,
1799         .hpreq_check       = ost_punch_hpreq_check,
1800 };
1801
1802 /** Assign high priority operations to the request if needed. */
1803 static int ost_hpreq_handler(struct ptlrpc_request *req)
1804 {
1805         ENTRY;
1806         if (req->rq_export) {
1807                 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1808                 struct ost_body *body;
1809
1810                 if (opc == OST_READ || opc == OST_WRITE) {
1811                         struct niobuf_remote *nb;
1812                         struct obd_ioobj *ioo;
1813                         int objcount, niocount;
1814                         int swab, i;
1815
1816                         body = lustre_swab_reqbuf(req, REQ_REC_OFF,
1817                                                   sizeof(*body),
1818                                                   lustre_swab_obdo);
1819                         if (!body) {
1820                                 CERROR("Missing/short ost_body\n");
1821                                 RETURN(-EFAULT);
1822                         }
1823                         objcount = lustre_msg_buflen(req->rq_reqmsg,
1824                                                      REQ_REC_OFF + 1) /
1825                                 sizeof(*ioo);
1826                         if (objcount == 0) {
1827                                 CERROR("Missing/short ioobj\n");
1828                                 RETURN(-EFAULT);
1829                         }
1830                         if (objcount > 1) {
1831                                 CERROR("too many ioobjs (%d)\n", objcount);
1832                                 RETURN(-EFAULT);
1833                         }
1834
1835                         ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1,
1836                                                  objcount * sizeof(*ioo),
1837                                                  lustre_swab_obd_ioobj);
1838                         if (!ioo) {
1839                                 CERROR("Missing/short ioobj\n");
1840                                 RETURN(-EFAULT);
1841                         }
1842
1843                         swab = ptlrpc_req_need_swab(req);
1844                         for (niocount = i = 0; i < objcount; i++) {
1845                                 if (i > 0 && swab)
1846                                         lustre_swab_obd_ioobj(&ioo[i]);
1847                                 if (ioo[i].ioo_bufcnt == 0) {
1848                                         CERROR("ioo[%d] has zero bufcnt\n", i);
1849                                         RETURN(-EFAULT);
1850                                 }
1851                                 niocount += ioo[i].ioo_bufcnt;
1852                         }
1853                         if (niocount > PTLRPC_MAX_BRW_PAGES) {
1854                                 DEBUG_REQ(D_ERROR, req, "bulk has too many "
1855                                           "pages (%d)", niocount);
1856                                 RETURN(-EFAULT);
1857                         }
1858
1859                         nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
1860                                                 niocount * sizeof(*nb),
1861                                                 lustre_swab_niobuf_remote);
1862                         if (!nb) {
1863                                 CERROR("Missing/short niobuf\n");
1864                                 RETURN(-EFAULT);
1865                         }
1866
1867                         swab = ptlrpc_req_need_swab(req);
1868                         if (swab) {
1869                                 /* swab remaining niobufs */
1870                                 for (i = 1; i < niocount; i++)
1871                                         lustre_swab_niobuf_remote(&nb[i]);
1872                         }
1873
1874                         if (niocount == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
1875                                 req->rq_ops = &ost_hpreq_rw;
1876                 } else if (opc == OST_PUNCH) {
1877                         body = lustre_swab_reqbuf(req, REQ_REC_OFF,
1878                                                   sizeof(*body),
1879                                                   lustre_swab_obdo);
1880                         if (!body) {
1881                                 CERROR("Missing/short ost_body\n");
1882                                 RETURN(-EFAULT);
1883                         }
1884
1885                         if (!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
1886                             !(body->oa.o_flags & OBD_FL_TRUNCLOCK))
1887                                 req->rq_ops = &ost_hpreq_punch;
1888                 }
1889         }
1890         RETURN(0);
1891 }
1892
1893 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1894 int ost_handle(struct ptlrpc_request *req)
1895 {
1896         struct obd_trans_info trans_info = { 0, };
1897         struct obd_trans_info *oti = &trans_info;
1898         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1899         struct obd_device *obd = NULL;
1900         ENTRY;
1901
1902         LASSERT(current->journal_info == NULL);
1903
1904         /* primordial rpcs don't affect server recovery */
1905         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1906         case SEC_CTX_INIT:
1907         case SEC_CTX_INIT_CONT:
1908         case SEC_CTX_FINI:
1909                 GOTO(out, rc = 0);
1910         }
1911
1912         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1913
1914         /* XXX identical to MDS */
1915         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1916                 int recovering;
1917
1918                 if (req->rq_export == NULL) {
1919                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1920                                lustre_msg_get_opc(req->rq_reqmsg),
1921                                libcfs_id2str(req->rq_peer));
1922                         req->rq_status = -ENOTCONN;
1923                         GOTO(out, rc = -ENOTCONN);
1924                 }
1925
1926                 obd = req->rq_export->exp_obd;
1927
1928                 /* Check for aborted recovery. */
1929                 spin_lock_bh(&obd->obd_processing_task_lock);
1930                 recovering = obd->obd_recovering;
1931                 spin_unlock_bh(&obd->obd_processing_task_lock);
1932                 if (recovering) {
1933                         rc = ost_filter_recovery_request(req, obd,
1934                                                          &should_process);
1935                         if (rc || !should_process)
1936                                 RETURN(rc);
1937                         else if (should_process < 0) {
1938                                 req->rq_status = should_process;
1939                                 rc = ptlrpc_error(req);
1940                                 RETURN(rc);
1941                         }
1942                 }
1943         }
1944
1945         oti_init(oti, req);
1946
1947         rc = ost_msg_check_version(req->rq_reqmsg);
1948         if (rc)
1949                 RETURN(rc);
1950
1951         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1952         case OST_CONNECT: {
1953                 CDEBUG(D_INODE, "connect\n");
1954                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
1955                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
1956                         RETURN(0);
1957                 rc = target_handle_connect(req);
1958                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
1959                         RETURN(0);
1960                 if (!rc) {
1961                         rc = ost_init_sec_level(req);
1962                         if (!rc)
1963                                 rc = ost_connect_check_sptlrpc(req);
1964                 }
1965                 break;
1966         }
1967         case OST_DISCONNECT:
1968                 CDEBUG(D_INODE, "disconnect\n");
1969                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
1970                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
1971                         RETURN(0);
1972                 rc = target_handle_disconnect(req);
1973                 break;
1974         case OST_CREATE:
1975                 CDEBUG(D_INODE, "create\n");
1976                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
1977                         RETURN(0);
1978                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1979                         GOTO(out, rc = -ENOSPC);
1980                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1981                         GOTO(out, rc = -EROFS);
1982                 rc = ost_create(req->rq_export, req, oti);
1983                 break;
1984         case OST_DESTROY:
1985                 CDEBUG(D_INODE, "destroy\n");
1986                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
1987                         RETURN(0);
1988                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1989                         GOTO(out, rc = -EROFS);
1990                 rc = ost_destroy(req->rq_export, req, oti);
1991                 break;
1992         case OST_GETATTR:
1993                 CDEBUG(D_INODE, "getattr\n");
1994                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
1995                         RETURN(0);
1996                 rc = ost_getattr(req->rq_export, req);
1997                 break;
1998         case OST_SETATTR:
1999                 CDEBUG(D_INODE, "setattr\n");
2000                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
2001                         RETURN(0);
2002                 rc = ost_setattr(req->rq_export, req, oti);
2003                 break;
2004         case OST_WRITE:
2005                 CDEBUG(D_INODE, "write\n");
2006                 /* req->rq_request_portal would be nice, if it was set */
2007                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2008                         CERROR("%s: deny write request from %s to portal %u\n",
2009                                req->rq_export->exp_obd->obd_name,
2010                                obd_export_nid2str(req->rq_export),
2011                                req->rq_rqbd->rqbd_service->srv_req_portal);
2012                         GOTO(out, rc = -EPROTO);
2013                 }
2014                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2015                         RETURN(0);
2016                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
2017                         GOTO(out, rc = -ENOSPC);
2018                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2019                         GOTO(out, rc = -EROFS);
2020                 rc = ost_brw_write(req, oti);
2021                 LASSERT(current->journal_info == NULL);
2022                 /* ost_brw_write sends its own replies */
2023                 RETURN(rc);
2024         case OST_READ:
2025                 CDEBUG(D_INODE, "read\n");
2026                 /* req->rq_request_portal would be nice, if it was set */
2027                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2028                         CERROR("%s: deny read request from %s to portal %u\n",
2029                                req->rq_export->exp_obd->obd_name,
2030                                obd_export_nid2str(req->rq_export),
2031                                req->rq_rqbd->rqbd_service->srv_req_portal);
2032                         GOTO(out, rc = -EPROTO);
2033                 }
2034                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2035                         RETURN(0);
2036                 rc = ost_brw_read(req, oti);
2037                 LASSERT(current->journal_info == NULL);
2038                 /* ost_brw_read sends its own replies */
2039                 RETURN(rc);
2040         case OST_PUNCH:
2041                 CDEBUG(D_INODE, "punch\n");
2042                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
2043                         RETURN(0);
2044                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2045                         GOTO(out, rc = -EROFS);
2046                 rc = ost_punch(req->rq_export, req, oti);
2047                 break;
2048         case OST_STATFS:
2049                 CDEBUG(D_INODE, "statfs\n");
2050                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
2051                         RETURN(0);
2052                 rc = ost_statfs(req);
2053                 break;
2054         case OST_SYNC:
2055                 CDEBUG(D_INODE, "sync\n");
2056                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
2057                         RETURN(0);
2058                 rc = ost_sync(req->rq_export, req);
2059                 break;
2060         case OST_SET_INFO:
2061                 DEBUG_REQ(D_INODE, req, "set_info");
2062                 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2063                 rc = ost_set_info(req->rq_export, req);
2064                 break;
2065         case OST_GET_INFO:
2066                 DEBUG_REQ(D_INODE, req, "get_info");
2067                 rc = ost_get_info(req->rq_export, req);
2068                 break;
2069 #ifdef HAVE_QUOTA_SUPPORT
2070         case OST_QUOTACHECK:
2071                 CDEBUG(D_INODE, "quotacheck\n");
2072                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
2073                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
2074                         RETURN(0);
2075                 rc = ost_handle_quotacheck(req);
2076                 break;
2077         case OST_QUOTACTL:
2078                 CDEBUG(D_INODE, "quotactl\n");
2079                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
2080                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
2081                         RETURN(0);
2082                 rc = ost_handle_quotactl(req);
2083                 break;
2084         case OST_QUOTA_ADJUST_QUNIT:
2085                 CDEBUG(D_INODE, "quota_adjust_qunit\n");
2086                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTA_ADJUST_QUNIT);
2087                 rc = ost_handle_quota_adjust_qunit(req);
2088                 break;
2089 #endif
2090         case OBD_PING:
2091                 DEBUG_REQ(D_INODE, req, "ping");
2092                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
2093                 rc = target_handle_ping(req);
2094                 break;
2095         /* FIXME - just reply status */
2096         case LLOG_ORIGIN_CONNECT:
2097                 DEBUG_REQ(D_INODE, req, "log connect");
2098                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
2099                 rc = ost_llog_handle_connect(req->rq_export, req);
2100                 req->rq_status = rc;
2101                 rc = req_capsule_server_pack(&req->rq_pill);
2102                 if (rc)
2103                         RETURN(rc);
2104                 RETURN(ptlrpc_reply(req));
2105         case OBD_LOG_CANCEL:
2106                 CDEBUG(D_INODE, "log cancel\n");
2107                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2108                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2109                         RETURN(0);
2110                 rc = llog_origin_handle_cancel(req);
2111                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2112                         RETURN(0);
2113                 req->rq_status = rc;
2114                 rc = req_capsule_server_pack(&req->rq_pill);
2115                 if (rc)
2116                         RETURN(rc);
2117                 RETURN(ptlrpc_reply(req));
2118         case LDLM_ENQUEUE:
2119                 CDEBUG(D_INODE, "enqueue\n");
2120                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
2121                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
2122                         RETURN(0);
2123                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2124                                          ldlm_server_blocking_ast,
2125                                          ldlm_server_glimpse_ast);
2126                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
2127                 break;
2128         case LDLM_CONVERT:
2129                 CDEBUG(D_INODE, "convert\n");
2130                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
2131                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
2132                         RETURN(0);
2133                 rc = ldlm_handle_convert(req);
2134                 break;
2135         case LDLM_CANCEL:
2136                 CDEBUG(D_INODE, "cancel\n");
2137                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2138                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2139                         RETURN(0);
2140                 rc = ldlm_handle_cancel(req);
2141                 break;
2142         case LDLM_BL_CALLBACK:
2143         case LDLM_CP_CALLBACK:
2144                 CDEBUG(D_INODE, "callback\n");
2145                 CERROR("callbacks should not happen on OST\n");
2146                 /* fall through */
2147         default:
2148                 CERROR("Unexpected opcode %d\n",
2149                        lustre_msg_get_opc(req->rq_reqmsg));
2150                 req->rq_status = -ENOTSUPP;
2151                 rc = ptlrpc_error(req);
2152                 RETURN(rc);
2153         }
2154
2155         LASSERT(current->journal_info == NULL);
2156
2157         EXIT;
2158         /* If we're DISCONNECTing, the export_data is already freed */
2159         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
2160                 target_committed_to_req(req);
2161
2162 out:
2163         if (!rc)
2164                 oti_to_request(oti, req);
2165
2166         target_send_reply(req, rc, fail);
2167         return 0;
2168 }
2169 EXPORT_SYMBOL(ost_handle);
2170 /*
2171  * free per-thread pool created by ost_thread_init().
2172  */
2173 static void ost_thread_done(struct ptlrpc_thread *thread)
2174 {
2175         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
2176                                              * Storage */
2177
2178         ENTRY;
2179
2180         LASSERT(thread != NULL);
2181
2182         /*
2183          * be prepared to handle partially-initialized pools (because this is
2184          * called from ost_thread_init() for cleanup.
2185          */
2186         tls = thread->t_data;
2187         if (tls != NULL) {
2188                 OBD_FREE_PTR(tls);
2189                 thread->t_data = NULL;
2190         }
2191         EXIT;
2192 }
2193
2194 /*
2195  * initialize per-thread page pool (bug 5137).
2196  */
2197 static int ost_thread_init(struct ptlrpc_thread *thread)
2198 {
2199         struct ost_thread_local_cache *tls;
2200
2201         ENTRY;
2202
2203         LASSERT(thread != NULL);
2204         LASSERT(thread->t_data == NULL);
2205         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
2206
2207         OBD_ALLOC_PTR(tls);
2208         if (tls == NULL)
2209                 RETURN(-ENOMEM);
2210         thread->t_data = tls;
2211         RETURN(0);
2212 }
2213
2214 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
2215
2216 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
2217 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
2218 {
2219         struct ost_obd *ost = &obd->u.ost;
2220         struct lprocfs_static_vars lvars;
2221         int oss_min_threads;
2222         int oss_max_threads;
2223         int oss_min_create_threads;
2224         int oss_max_create_threads;
2225         int rc;
2226         ENTRY;
2227
2228         rc = cleanup_group_info();
2229         if (rc)
2230                 RETURN(rc);
2231
2232         lprocfs_ost_init_vars(&lvars);
2233         lprocfs_obd_setup(obd, lvars.obd_vars);
2234
2235         sema_init(&ost->ost_health_sem, 1);
2236
2237         if (oss_num_threads) {
2238                 /* If oss_num_threads is set, it is the min and the max. */
2239                 if (oss_num_threads > OSS_THREADS_MAX)
2240                         oss_num_threads = OSS_THREADS_MAX;
2241                 if (oss_num_threads < OSS_THREADS_MIN)
2242                         oss_num_threads = OSS_THREADS_MIN;
2243                 oss_max_threads = oss_min_threads = oss_num_threads;
2244         } else {
2245                 /* Base min threads on memory and cpus */
2246                 oss_min_threads = num_possible_cpus() * CFS_NUM_CACHEPAGES >>
2247                         (27 - CFS_PAGE_SHIFT);
2248                 if (oss_min_threads < OSS_THREADS_MIN)
2249                         oss_min_threads = OSS_THREADS_MIN;
2250                 /* Insure a 4x range for dynamic threads */
2251                 if (oss_min_threads > OSS_THREADS_MAX / 4)
2252                         oss_min_threads = OSS_THREADS_MAX / 4;
2253                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4 + 1);
2254         }
2255
2256         ost->ost_service =
2257                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2258                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
2259                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2260                                 ost_handle, LUSTRE_OSS_NAME,
2261                                 obd->obd_proc_entry, target_print_req,
2262                                 oss_min_threads, oss_max_threads,
2263                                 "ll_ost", LCT_DT_THREAD, NULL);
2264         if (ost->ost_service == NULL) {
2265                 CERROR("failed to start service\n");
2266                 GOTO(out_lprocfs, rc = -ENOMEM);
2267         }
2268
2269         rc = ptlrpc_start_threads(obd, ost->ost_service);
2270         if (rc)
2271                 GOTO(out_service, rc = -EINVAL);
2272
2273         if (oss_num_create_threads) {
2274                 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
2275                         oss_num_create_threads = OSS_MAX_CREATE_THREADS;
2276                 if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
2277                         oss_num_create_threads = OSS_MIN_CREATE_THREADS;
2278                 oss_min_create_threads = oss_max_create_threads =
2279                         oss_num_create_threads;
2280         } else {
2281                 oss_min_create_threads = OSS_MIN_CREATE_THREADS;
2282                 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
2283         }
2284
2285         ost->ost_create_service =
2286                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2287                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
2288                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2289                                 ost_handle, "ost_create",
2290                                 obd->obd_proc_entry, target_print_req,
2291                                 oss_min_create_threads, oss_max_create_threads,
2292                                 "ll_ost_creat", LCT_DT_THREAD, NULL);
2293         if (ost->ost_create_service == NULL) {
2294                 CERROR("failed to start OST create service\n");
2295                 GOTO(out_service, rc = -ENOMEM);
2296         }
2297
2298         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
2299         if (rc)
2300                 GOTO(out_create, rc = -EINVAL);
2301
2302         ost->ost_io_service =
2303                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2304                                 OST_MAXREPSIZE, OST_IO_PORTAL,
2305                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2306                                 ost_handle, "ost_io",
2307                                 obd->obd_proc_entry, target_print_req,
2308                                 oss_min_threads, oss_max_threads,
2309                                 "ll_ost_io", LCT_DT_THREAD, ost_hpreq_handler);
2310         if (ost->ost_io_service == NULL) {
2311                 CERROR("failed to start OST I/O service\n");
2312                 GOTO(out_create, rc = -ENOMEM);
2313         }
2314
2315         ost->ost_io_service->srv_init = ost_thread_init;
2316         ost->ost_io_service->srv_done = ost_thread_done;
2317         ost->ost_io_service->srv_cpu_affinity = 1;
2318         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
2319         if (rc)
2320                 GOTO(out_io, rc = -EINVAL);
2321
2322         ping_evictor_start();
2323
2324         RETURN(0);
2325
2326 out_io:
2327         ptlrpc_unregister_service(ost->ost_io_service);
2328         ost->ost_io_service = NULL;
2329 out_create:
2330         ptlrpc_unregister_service(ost->ost_create_service);
2331         ost->ost_create_service = NULL;
2332 out_service:
2333         ptlrpc_unregister_service(ost->ost_service);
2334         ost->ost_service = NULL;
2335 out_lprocfs:
2336         lprocfs_obd_cleanup(obd);
2337         RETURN(rc);
2338 }
2339
2340 static int ost_cleanup(struct obd_device *obd)
2341 {
2342         struct ost_obd *ost = &obd->u.ost;
2343         int err = 0;
2344         ENTRY;
2345
2346         ping_evictor_stop();
2347
2348         spin_lock_bh(&obd->obd_processing_task_lock);
2349         if (obd->obd_recovering) {
2350                 target_cancel_recovery_timer(obd);
2351                 obd->obd_recovering = 0;
2352         }
2353         spin_unlock_bh(&obd->obd_processing_task_lock);
2354
2355         down(&ost->ost_health_sem);
2356         ptlrpc_unregister_service(ost->ost_service);
2357         ptlrpc_unregister_service(ost->ost_create_service);
2358         ptlrpc_unregister_service(ost->ost_io_service);
2359         ost->ost_service = NULL;
2360         ost->ost_create_service = NULL;
2361         up(&ost->ost_health_sem);
2362
2363         lprocfs_obd_cleanup(obd);
2364
2365         RETURN(err);
2366 }
2367
2368 static int ost_health_check(struct obd_device *obd)
2369 {
2370         struct ost_obd *ost = &obd->u.ost;
2371         int rc = 0;
2372
2373         down(&ost->ost_health_sem);
2374         rc |= ptlrpc_service_health_check(ost->ost_service);
2375         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2376         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2377         up(&ost->ost_health_sem);
2378
2379         /*
2380          * health_check to return 0 on healthy
2381          * and 1 on unhealthy.
2382          */
2383         if( rc != 0)
2384                 rc = 1;
2385
2386         return rc;
2387 }
2388
2389 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2390 {
2391         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2392 }
2393
2394 /* use obd ops to offer management infrastructure */
2395 static struct obd_ops ost_obd_ops = {
2396         .o_owner        = THIS_MODULE,
2397         .o_setup        = ost_setup,
2398         .o_cleanup      = ost_cleanup,
2399         .o_health_check = ost_health_check,
2400 };
2401
2402
2403 static int __init ost_init(void)
2404 {
2405         struct lprocfs_static_vars lvars;
2406         int rc;
2407         ENTRY;
2408
2409         lprocfs_ost_init_vars(&lvars);
2410         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2411                                  LUSTRE_OSS_NAME, NULL);
2412
2413         if (ost_num_threads != 0 && oss_num_threads == 0) {
2414                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2415                               "use oss_num_threads instead or unset both for "
2416                               "dynamic thread startup\n");
2417                 oss_num_threads = ost_num_threads;
2418         }
2419
2420         RETURN(rc);
2421 }
2422
2423 static void /*__exit*/ ost_exit(void)
2424 {
2425         class_unregister_type(LUSTRE_OSS_NAME);
2426 }
2427
2428 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2429 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2430 MODULE_LICENSE("GPL");
2431
2432 module_init(ost_init);
2433 module_exit(ost_exit);