Whamcloud - gitweb
* Print portals error string in ptllnd warnings/errors
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  *  Storage Target Handling functions
27  *  Lustre Object Server Module (OST)
28  *
29  *  This server is single threaded at present (but can easily be multi
30  *  threaded). For testing and management it is treated as an
31  *  obd_device, although it does not export a full OBD method table
32  *  (the requests are coming in over the wire, so object target
33  *  modules do not have a full method table.)
34  */
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_OST
40
41 #include <linux/module.h>
42 #include <obd_ost.h>
43 #include <lustre_net.h>
44 #include <lustre_dlm.h>
45 #include <lustre_export.h>
46 #include <lustre_debug.h>
47 #include <linux/init.h>
48 #include <lprocfs_status.h>
49 #include <lustre_commit_confd.h>
50 #include <libcfs/list.h>
51 #include <lustre_quota.h>
52 #include "ost_internal.h"
53
54 static int oss_num_threads;
55 CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
56                 "number of OSS service threads to start");
57
58 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
59 {
60         struct oti_req_ack_lock *ack_lock;
61         int i;
62
63         if (oti == NULL)
64                 return;
65
66         if (req->rq_repmsg)
67                 lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
68         req->rq_transno = oti->oti_transno;
69
70         /* XXX 4 == entries in oti_ack_locks??? */
71         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
72                 if (!ack_lock->mode)
73                         break;
74                 /* XXX not even calling target_send_reply in some cases... */
75                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
76         }
77 }
78
79 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
80                        struct obd_trans_info *oti)
81 {
82         struct ost_body *body, *repbody;
83         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
84         ENTRY;
85
86         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
87                                   lustre_swab_ost_body);
88         if (body == NULL)
89                 RETURN(-EFAULT);
90
91         rc = lustre_pack_reply(req, 2, size, NULL);
92         if (rc)
93                 RETURN(rc);
94
95         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
96                 oti->oti_logcookies = obdo_logcookie(&body->oa);
97         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
98                                  sizeof(*repbody));
99         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
100         req->rq_status = obd_destroy(exp, &body->oa, NULL, oti, NULL);
101         RETURN(0);
102 }
103
104 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
105 {
106         struct ost_body *body, *repbody;
107         struct obd_info oinfo = { { { 0 } } };
108         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
109         ENTRY;
110
111         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
112                                   lustre_swab_ost_body);
113         if (body == NULL)
114                 RETURN(-EFAULT);
115
116         rc = lustre_pack_reply(req, 2, size, NULL);
117         if (rc)
118                 RETURN(rc);
119
120         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
121                                  sizeof(*repbody));
122         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
123
124         oinfo.oi_oa = &repbody->oa;
125         req->rq_status = obd_getattr(exp, &oinfo);
126         RETURN(0);
127 }
128
129 static int ost_statfs(struct ptlrpc_request *req)
130 {
131         struct obd_statfs *osfs;
132         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*osfs) };
133         ENTRY;
134
135         rc = lustre_pack_reply(req, 2, size, NULL);
136         if (rc)
137                 RETURN(rc);
138
139         osfs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*osfs));
140
141         req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs, 
142                                     cfs_time_current_64() - HZ);
143         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
144                 osfs->os_bfree = osfs->os_bavail = 64;
145         if (req->rq_status != 0)
146                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
147
148         RETURN(0);
149 }
150
151 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
152                       struct obd_trans_info *oti)
153 {
154         struct ost_body *body, *repbody;
155         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
156         ENTRY;
157
158         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
159                                   lustre_swab_ost_body);
160         if (body == NULL)
161                 RETURN(-EFAULT);
162
163         rc = lustre_pack_reply(req, 2, size, NULL);
164         if (rc)
165                 RETURN(rc);
166
167         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
168                                  sizeof(*repbody));
169         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
170         oti->oti_logcookies = obdo_logcookie(&repbody->oa);
171         req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
172         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
173         RETURN(0);
174 }
175
176 /*
177  * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
178  * lock on the file being truncated.
179  */
180 static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
181                               struct lustre_handle *lh)
182 {
183         int flags;
184         struct ldlm_res_id res_id = { .name = { oa->o_id } };
185         ldlm_policy_data_t policy;
186         __u64 start;
187         __u64 finis;
188
189         ENTRY;
190
191         LASSERT(!lustre_handle_is_used(lh));
192
193         if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
194             !(oa->o_flags & OBD_FL_TRUNCLOCK))
195                 RETURN(0);
196
197         CDEBUG(D_INODE, "OST-side truncate lock.\n");
198
199         start = oa->o_size;
200         finis = start + oa->o_blocks;
201
202         /*
203          * standard truncate optimization: if file body is completely
204          * destroyed, don't send data back to the server.
205          */
206         flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
207
208         policy.l_extent.start = start & CFS_PAGE_MASK;
209
210         /*
211          * If ->o_blocks is EOF it means "lock till the end of the
212          * file". Otherwise, it's size of a hole being punched (in bytes)
213          */
214         if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
215                 policy.l_extent.end = OBD_OBJECT_EOF;
216         else
217                 policy.l_extent.end = finis | ~CFS_PAGE_MASK;
218
219         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, res_id, 
220                                       LDLM_EXTENT, &policy, LCK_PW, &flags,
221                                       ldlm_blocking_ast, ldlm_completion_ast,
222                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
223 }
224
225 /*
226  * Helper function for ost_punch(): release lock acquired by
227  * ost_punch_lock_get(), if any.
228  */
229 static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
230                                struct lustre_handle *lh)
231 {
232         ENTRY;
233         if (lustre_handle_is_used(lh))
234                 ldlm_lock_decref(lh, LCK_PW);
235         EXIT;
236 }
237
238 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
239                      struct obd_trans_info *oti)
240 {
241         struct obd_info oinfo = { { { 0 } } };
242         struct ost_body *body, *repbody;
243         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
244         struct lustre_handle lh = {0,};
245         ENTRY;
246
247         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
248         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
249
250         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
251                                   lustre_swab_ost_body);
252         if (body == NULL)
253                 RETURN(-EFAULT);
254
255         oinfo.oi_oa = &body->oa;
256         oinfo.oi_policy.l_extent.start = oinfo.oi_oa->o_size;
257         oinfo.oi_policy.l_extent.end = oinfo.oi_oa->o_blocks;
258
259         if ((oinfo.oi_oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
260             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
261                 RETURN(-EINVAL);
262
263         rc = lustre_pack_reply(req, 2, size, NULL);
264         if (rc)
265                 RETURN(rc);
266
267         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
268                                  sizeof(*repbody));
269         rc = ost_punch_lock_get(exp, oinfo.oi_oa, &lh);
270         if (rc == 0) {
271                 if (oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
272                     oinfo.oi_oa->o_flags == OBD_FL_TRUNCLOCK)
273                         /*
274                          * If OBD_FL_TRUNCLOCK is the only bit set in
275                          * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
276                          * through filter_setattr() to filter_iocontrol().
277                          */
278                         oinfo.oi_oa->o_valid &= ~OBD_MD_FLFLAGS;
279
280                 req->rq_status = obd_punch(exp, &oinfo, oti, NULL);
281                 ost_punch_lock_put(exp, oinfo.oi_oa, &lh);
282         }
283         repbody->oa = *oinfo.oi_oa;
284         RETURN(rc);
285 }
286
287 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
288 {
289         struct ost_body *body, *repbody;
290         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
291         ENTRY;
292
293         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
294                                   lustre_swab_ost_body);
295         if (body == NULL)
296                 RETURN(-EFAULT);
297
298         rc = lustre_pack_reply(req, 2, size, NULL);
299         if (rc)
300                 RETURN(rc);
301
302         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
303                                  sizeof(*repbody));
304         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
305         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
306                                   repbody->oa.o_blocks);
307         RETURN(0);
308 }
309
310 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
311                        struct obd_trans_info *oti)
312 {
313         struct ost_body *body, *repbody;
314         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
315         struct obd_info oinfo = { { { 0 } } };
316         ENTRY;
317
318         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
319                                   lustre_swab_ost_body);
320         if (body == NULL)
321                 RETURN(-EFAULT);
322
323         rc = lustre_pack_reply(req, 2, size, NULL);
324         if (rc)
325                 RETURN(rc);
326
327         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
328                                  sizeof(*repbody));
329         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
330
331         oinfo.oi_oa = &repbody->oa;
332         req->rq_status = obd_setattr(exp, &oinfo, oti);
333         RETURN(0);
334 }
335
336 static int ost_bulk_timeout(void *data)
337 {
338         ENTRY;
339         /* We don't fail the connection here, because having the export
340          * killed makes the (vital) call to commitrw very sad.
341          */
342         RETURN(1);
343 }
344
345 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
346                                 struct niobuf_remote *rnb, int nrnb,
347                                 struct niobuf_remote **pp_rnbp)
348 {
349         /* Copy a remote niobuf, splitting it into page-sized chunks
350          * and setting ioo[i].ioo_bufcnt accordingly */
351         struct niobuf_remote *pp_rnb;
352         int   i;
353         int   j;
354         int   page;
355         int   rnbidx = 0;
356         int   npages = 0;
357
358         /*
359          * array of sufficient size already preallocated by caller
360          */
361         LASSERT(pp_rnbp != NULL);
362         LASSERT(*pp_rnbp != NULL);
363
364         /* first count and check the number of pages required */
365         for (i = 0; i < nioo; i++)
366                 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
367                         obd_off offset = rnb[rnbidx].offset;
368                         obd_off p0 = offset >> CFS_PAGE_SHIFT;
369                         obd_off pn = (offset + rnb[rnbidx].len - 1)>>CFS_PAGE_SHIFT;
370
371                         LASSERT(rnbidx < nrnb);
372
373                         npages += (pn + 1 - p0);
374
375                         if (rnb[rnbidx].len == 0) {
376                                 CERROR("zero len BRW: obj %d objid "LPX64
377                                        " buf %u\n", i, ioo[i].ioo_id, j);
378                                 return -EINVAL;
379                         }
380                         if (j > 0 &&
381                             rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
382                                 CERROR("unordered BRW: obj %d objid "LPX64
383                                        " buf %u offset "LPX64" <= "LPX64"\n",
384                                        i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
385                                        rnb[rnbidx].offset);
386                                 return -EINVAL;
387                         }
388                 }
389
390         LASSERT(rnbidx == nrnb);
391
392         if (npages == nrnb) {       /* all niobufs are for single pages */
393                 *pp_rnbp = rnb;
394                 return npages;
395         }
396
397         pp_rnb = *pp_rnbp;
398
399         /* now do the actual split */
400         page = rnbidx = 0;
401         for (i = 0; i < nioo; i++) {
402                 int  obj_pages = 0;
403
404                 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
405                         obd_off off = rnb[rnbidx].offset;
406                         int     nob = rnb[rnbidx].len;
407
408                         LASSERT(rnbidx < nrnb);
409                         do {
410                                 obd_off  poff = off & ~CFS_PAGE_MASK;
411                                 int      pnob = (poff + nob > CFS_PAGE_SIZE) ?
412                                                 CFS_PAGE_SIZE - poff : nob;
413
414                                 LASSERT(page < npages);
415                                 pp_rnb[page].len = pnob;
416                                 pp_rnb[page].offset = off;
417                                 pp_rnb[page].flags = rnb[rnbidx].flags;
418
419                                 CDEBUG(0, "   obj %d id "LPX64
420                                        "page %d(%d) "LPX64" for %d, flg %x\n",
421                                        i, ioo[i].ioo_id, obj_pages, page,
422                                        pp_rnb[page].offset, pp_rnb[page].len,
423                                        pp_rnb[page].flags);
424                                 page++;
425                                 obj_pages++;
426
427                                 off += pnob;
428                                 nob -= pnob;
429                         } while (nob > 0);
430                         LASSERT(nob == 0);
431                 }
432                 ioo[i].ioo_bufcnt = obj_pages;
433         }
434         LASSERT(page == npages);
435
436         return npages;
437 }
438
439 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc)
440 {
441         __u32 cksum = ~0;
442         int i;
443
444         for (i = 0; i < desc->bd_iov_count; i++) {
445                 struct page *page = desc->bd_iov[i].kiov_page;
446                 int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
447                 char *ptr = kmap(page) + off;
448                 int len = desc->bd_iov[i].kiov_len;
449
450                 /* corrupt the data before we compute the checksum, to
451                  * simulate a client->OST data error */
452                 if (i == 0 &&OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_CHECKSUM_RECEIVE))
453                         memcpy(ptr, "bad3", min(4, len));
454                 cksum = crc32_le(cksum, ptr, len);
455                 /* corrupt the data after we compute the checksum, to
456                  * simulate an OST->client data error */
457                 if (i == 0 && OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_CHECKSUM_SEND))
458                         memcpy(ptr, "bad4", min(4, len));
459                 kunmap(page);
460         }
461
462         return cksum;
463 }
464
465 /*
466  * populate @nio by @nrpages pages from per-thread page pool
467  */
468 static void ost_nio_pages_get(struct ptlrpc_request *req,
469                               struct niobuf_local *nio, int nrpages)
470 {
471         int i;
472         struct ost_thread_local_cache *tls;
473
474         ENTRY;
475
476         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
477         LASSERT(req != NULL);
478         LASSERT(req->rq_svc_thread != NULL);
479
480         tls = ost_tls(req);
481         LASSERT(tls != NULL);
482
483         memset(nio, 0, nrpages * sizeof *nio);
484         for (i = 0; i < nrpages; ++ i) {
485                 struct page *page;
486
487                 page = tls->page[i];
488                 LASSERT(page != NULL);
489                 POISON_PAGE(page, 0xf1);
490                 nio[i].page = page;
491                 LL_CDEBUG_PAGE(D_INFO, page, "%d\n", i);
492         }
493         EXIT;
494 }
495
496 /*
497  * Dual for ost_nio_pages_get(). Poison pages in pool for debugging
498  */
499 static void ost_nio_pages_put(struct ptlrpc_request *req,
500                               struct niobuf_local *nio, int nrpages)
501 {
502         int i;
503
504         ENTRY;
505
506         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
507
508         for (i = 0; i < nrpages; ++ i)
509                 POISON_PAGE(nio[i].page, 0xf2);
510         EXIT;
511 }
512
513 static int ost_brw_lock_get(int mode, struct obd_export *exp,
514                             struct obd_ioobj *obj, struct niobuf_remote *nb,
515                             struct lustre_handle *lh)
516 {
517         int flags                 = 0;
518         int nrbufs                = obj->ioo_bufcnt;
519         struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
520         ldlm_policy_data_t policy;
521         int i;
522
523         ENTRY;
524
525         LASSERT(mode == LCK_PR || mode == LCK_PW);
526         LASSERT(!lustre_handle_is_used(lh));
527
528         if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
529                 RETURN(0);
530
531         /* EXPENSIVE ASSERTION */
532         for (i = 1; i < nrbufs; i ++)
533                 LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
534                         (nb[i].flags & OBD_BRW_SRVLOCK));
535
536         policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
537         policy.l_extent.end   = (nb[nrbufs - 1].offset +
538                                  nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
539
540         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, res_id, 
541                                       LDLM_EXTENT, &policy, mode, &flags,
542                                       ldlm_blocking_ast, ldlm_completion_ast,
543                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
544 }
545
546 static void ost_brw_lock_put(int mode,
547                              struct obd_ioobj *obj, struct niobuf_remote *niob,
548                              struct lustre_handle *lh)
549 {
550         ENTRY;
551         LASSERT(mode == LCK_PR || mode == LCK_PW);
552         LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
553                 lustre_handle_is_used(lh));
554         if (lustre_handle_is_used(lh))
555                 ldlm_lock_decref(lh, mode);
556         EXIT;
557 }
558
559 struct ost_prolong_data {
560         struct obd_export *opd_exp;
561         ldlm_policy_data_t opd_policy;
562         ldlm_mode_t opd_mode;
563 };
564
565 static int ost_prolong_locks_iter(struct ldlm_lock *lock, void *data)
566 {
567         struct ost_prolong_data *opd = data;
568
569         LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
570
571         if (lock->l_req_mode != lock->l_granted_mode) {
572                 /* scan granted locks only */
573                 return LDLM_ITER_STOP;
574         }
575
576         if (lock->l_export != opd->opd_exp) {
577                 /* prolong locks only for given client */
578                 return LDLM_ITER_CONTINUE;
579         }
580
581         if (!(lock->l_granted_mode & opd->opd_mode)) {
582                 /* we aren't interesting in all type of locks */
583                 return LDLM_ITER_CONTINUE;
584         }
585
586         if (lock->l_policy_data.l_extent.end < opd->opd_policy.l_extent.start ||
587             lock->l_policy_data.l_extent.start > opd->opd_policy.l_extent.end) {
588                 /* the request doesn't cross the lock, skip it */
589                 return LDLM_ITER_CONTINUE;
590         }
591
592         if (!(lock->l_flags & LDLM_FL_AST_SENT)) {
593                 /* ignore locks not being cancelled */
594                 return LDLM_ITER_CONTINUE;
595         }
596
597         /* OK. this is a possible lock the user holds doing I/O
598          * let's refresh eviction timer for it */
599         ldlm_refresh_waiting_lock(lock);
600
601         return LDLM_ITER_CONTINUE;
602 }
603
604 static void ost_prolong_locks(struct obd_export *exp, struct obd_ioobj *obj,
605                               struct niobuf_remote *nb, ldlm_mode_t mode)
606 {
607         struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
608         int nrbufs = obj->ioo_bufcnt;
609         struct ost_prolong_data opd;
610
611         ENTRY;
612
613         opd.opd_mode = mode;
614         opd.opd_exp = exp;
615         opd.opd_policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
616         opd.opd_policy.l_extent.end = (nb[nrbufs - 1].offset +
617                                        nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
618
619         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
620                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
621                opd.opd_policy.l_extent.end);
622         ldlm_resource_iterate(exp->exp_obd->obd_namespace, &res_id,
623                               ost_prolong_locks_iter, &opd);
624 }
625
626 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
627 {
628         struct ptlrpc_bulk_desc *desc;
629         struct niobuf_remote *remote_nb;
630         struct niobuf_remote *pp_rnb = NULL;
631         struct niobuf_local *local_nb;
632         struct obd_ioobj *ioo;
633         struct ost_body *body, *repbody;
634         struct l_wait_info lwi;
635         struct lustre_handle lockh = { 0 };
636         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
637         int comms_error = 0, niocount, npages, nob = 0, rc, i;
638         int no_reply = 0;
639         ENTRY;
640
641         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
642                 GOTO(out, rc = -EIO);
643
644         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
645                          (obd_timeout + 1) / 4);
646
647         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
648                                   lustre_swab_ost_body);
649         if (body == NULL) {
650                 CERROR("Missing/short ost_body\n");
651                 GOTO(out, rc = -EFAULT);
652         }
653
654         ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*ioo),
655                                  lustre_swab_obd_ioobj);
656         if (ioo == NULL) {
657                 CERROR("Missing/short ioobj\n");
658                 GOTO(out, rc = -EFAULT);
659         }
660
661         niocount = ioo->ioo_bufcnt;
662         if (niocount > PTLRPC_MAX_BRW_PAGES) {
663                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
664                           niocount);
665                 GOTO(out, rc = -EFAULT);
666         }
667
668         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
669                                        niocount * sizeof(*remote_nb),
670                                        lustre_swab_niobuf_remote);
671         if (remote_nb == NULL) {
672                 CERROR("Missing/short niobuf\n");
673                 GOTO(out, rc = -EFAULT);
674         }
675         if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
676                 for (i = 1; i < niocount; i++)
677                         lustre_swab_niobuf_remote (&remote_nb[i]);
678         }
679
680         rc = lustre_pack_reply(req, 2, size, NULL);
681         if (rc)
682                 GOTO(out, rc);
683
684         /*
685          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
686          * ost_thread_init().
687          */
688         local_nb = ost_tls(req)->local;
689         pp_rnb   = ost_tls(req)->remote;
690
691         /* FIXME all niobuf splitting should be done in obdfilter if needed */
692         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
693         npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
694         if (npages < 0)
695                 GOTO(out, rc = npages);
696
697         LASSERT(npages <= OST_THREAD_POOL_SIZE);
698
699         ost_nio_pages_get(req, local_nb, npages);
700
701         desc = ptlrpc_prep_bulk_exp(req, npages,
702                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
703         if (desc == NULL)
704                 GOTO(out, rc = -ENOMEM);
705
706         rc = ost_brw_lock_get(LCK_PR, req->rq_export, ioo, pp_rnb, &lockh);
707         if (rc != 0)
708                 GOTO(out_bulk, rc);
709
710         /* 
711          * If getting the lock took more time than
712          * client was willing to wait, drop it. b=11330
713          */
714         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
715             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
716                 no_reply = 1;
717                 CERROR("Dropping timed-out read from %s because locking"
718                        "object "LPX64" took %ld seconds.\n",
719                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
720                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
721                 goto out_lock;
722         }
723
724         rc = obd_preprw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
725                         ioo, npages, pp_rnb, local_nb, oti);
726         if (rc != 0)
727                 GOTO(out_lock, rc);
728
729         ost_prolong_locks(req->rq_export, ioo, pp_rnb, LCK_PW | LCK_PR);
730
731         nob = 0;
732         for (i = 0; i < npages; i++) {
733                 int page_rc = local_nb[i].rc;
734
735                 if (page_rc < 0) {              /* error */
736                         rc = page_rc;
737                         break;
738                 }
739
740                 LASSERTF(page_rc <= pp_rnb[i].len, "page_rc (%d) > "
741                          "pp_rnb[%d].len (%d)\n", page_rc, i, pp_rnb[i].len);
742                 nob += page_rc;
743                 if (page_rc != 0) {             /* some data! */
744                         LASSERT (local_nb[i].page != NULL);
745                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
746                                               pp_rnb[i].offset & ~CFS_PAGE_MASK,
747                                               page_rc);
748                 }
749
750                 if (page_rc != pp_rnb[i].len) { /* short read */
751                         /* All subsequent pages should be 0 */
752                         while(++i < npages)
753                                 LASSERT(local_nb[i].rc == 0);
754                         break;
755                 }
756         }
757
758         if (unlikely(body->oa.o_valid & OBD_MD_FLCKSUM)) {
759                 body->oa.o_cksum = ost_checksum_bulk(desc);
760                 body->oa.o_valid = OBD_MD_FLCKSUM;
761                 CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
762         } else {
763                 body->oa.o_valid = 0;
764         }
765         /* We're finishing using body->oa as an input variable */
766
767         /* Check if client was evicted while we were doing i/o before touching
768            network */
769         if (rc == 0) {
770                 if (desc->bd_export->exp_failed)
771                         rc = -ENOTCONN;
772                 else
773                         rc = ptlrpc_start_bulk_transfer(desc);
774                 if (rc == 0) {
775                         lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
776                                                    ost_bulk_timeout, desc);
777                         rc = l_wait_event(desc->bd_waitq,
778                                           !ptlrpc_bulk_active(desc) ||
779                                           desc->bd_export->exp_failed, &lwi);
780                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
781                         if (rc == -ETIMEDOUT) {
782                                 DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT");
783                                 ptlrpc_abort_bulk(desc);
784                         } else if (desc->bd_export->exp_failed) {
785                                 DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
786                                 rc = -ENOTCONN;
787                                 ptlrpc_abort_bulk(desc);
788                         } else if (!desc->bd_success ||
789                                    desc->bd_nob_transferred != desc->bd_nob) {
790                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
791                                           desc->bd_success ?
792                                           "truncated" : "network error on",
793                                           desc->bd_nob_transferred,
794                                           desc->bd_nob);
795                                 /* XXX should this be a different errno? */
796                                 rc = -ETIMEDOUT;
797                         }
798                 } else {
799                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d\n", rc);
800                 }
801                 comms_error = rc != 0;
802         }
803
804         /* Must commit after prep above in all cases */
805         rc = obd_commitrw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
806                           ioo, npages, local_nb, oti, rc);
807
808         ost_nio_pages_put(req, local_nb, npages);
809
810         if (rc == 0) {
811                 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
812                                          sizeof(*repbody));
813                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
814         }
815
816  out_lock:
817         ost_brw_lock_put(LCK_PR, ioo, pp_rnb, &lockh);
818  out_bulk:
819         ptlrpc_free_bulk(desc);
820         if (no_reply)
821                 RETURN(rc);
822  out:
823         LASSERT(rc <= 0);
824         if (rc == 0) {
825                 req->rq_status = nob;
826                 target_committed_to_req(req);
827                 ptlrpc_reply(req);
828         } else if (!comms_error) {
829                 /* Only reply if there was no comms problem with bulk */
830                 target_committed_to_req(req);
831                 req->rq_status = rc;
832                 ptlrpc_error(req);
833         } else {
834                 if (req->rq_reply_state != NULL) {
835                         /* reply out callback would free */
836                         ptlrpc_rs_decref(req->rq_reply_state);
837                         req->rq_reply_state = NULL;
838                 }
839                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
840                       "client will retry\n",
841                       req->rq_export->exp_obd->obd_name,
842                       req->rq_export->exp_client_uuid.uuid,
843                       req->rq_export->exp_connection->c_remote_uuid.uuid,
844                       libcfs_id2str(req->rq_peer));
845         }
846
847         RETURN(rc);
848 }
849
850 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
851 {
852         struct ptlrpc_bulk_desc *desc;
853         struct niobuf_remote    *remote_nb;
854         struct niobuf_remote    *pp_rnb;
855         struct niobuf_local     *local_nb;
856         struct obd_ioobj        *ioo;
857         struct ost_body         *body, *repbody;
858         struct l_wait_info       lwi;
859         struct lustre_handle     lockh = {0};
860         __u32                   *rcs;
861         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
862         int objcount, niocount, npages, comms_error = 0;
863         int rc, swab, i, j;
864         obd_count                client_cksum, server_cksum = 0;
865         int                      no_reply = 0; 
866         ENTRY;
867
868         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
869                 GOTO(out, rc = -EIO);
870
871         /* pause before transaction has been started */
872         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
873                          (obd_timeout + 1) / 4);
874
875         swab = lustre_msg_swabbed(req->rq_reqmsg);
876         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
877                                   lustre_swab_ost_body);
878         if (body == NULL) {
879                 CERROR("Missing/short ost_body\n");
880                 GOTO(out, rc = -EFAULT);
881         }
882
883         LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
884         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
885                    sizeof(*ioo);
886         if (objcount == 0) {
887                 CERROR("Missing/short ioobj\n");
888                 GOTO(out, rc = -EFAULT);
889         }
890         if (objcount > 1) {
891                 CERROR("too many ioobjs (%d)\n", objcount);
892                 GOTO(out, rc = -EFAULT);
893         }
894
895         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
896                              objcount * sizeof(*ioo));
897         LASSERT (ioo != NULL);
898         for (niocount = i = 0; i < objcount; i++) {
899                 if (swab)
900                         lustre_swab_obd_ioobj(&ioo[i]);
901                 if (ioo[i].ioo_bufcnt == 0) {
902                         CERROR("ioo[%d] has zero bufcnt\n", i);
903                         GOTO(out, rc = -EFAULT);
904                 }
905                 niocount += ioo[i].ioo_bufcnt;
906         }
907
908         if (niocount > PTLRPC_MAX_BRW_PAGES) {
909                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
910                           niocount);
911                 GOTO(out, rc = -EFAULT);
912         }
913
914         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
915                                        niocount * sizeof(*remote_nb),
916                                        lustre_swab_niobuf_remote);
917         if (remote_nb == NULL) {
918                 CERROR("Missing/short niobuf\n");
919                 GOTO(out, rc = -EFAULT);
920         }
921         if (swab) {                             /* swab the remaining niobufs */
922                 for (i = 1; i < niocount; i++)
923                         lustre_swab_niobuf_remote (&remote_nb[i]);
924         }
925
926         size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
927         rc = lustre_pack_reply(req, 3, size, NULL);
928         if (rc != 0)
929                 GOTO(out, rc);
930         rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
931                              niocount * sizeof(*rcs));
932
933         /*
934          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
935          * ost_thread_init().
936          */
937         local_nb = ost_tls(req)->local;
938         pp_rnb   = ost_tls(req)->remote;
939
940         /* FIXME all niobuf splitting should be done in obdfilter if needed */
941         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
942         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
943         if (npages < 0)
944                 GOTO(out, rc = npages);
945
946         LASSERT(npages <= OST_THREAD_POOL_SIZE);
947
948         ost_nio_pages_get(req, local_nb, npages);
949
950         desc = ptlrpc_prep_bulk_exp(req, npages,
951                                      BULK_GET_SINK, OST_BULK_PORTAL);
952         if (desc == NULL)
953                 GOTO(out, rc = -ENOMEM);
954
955         rc = ost_brw_lock_get(LCK_PW, req->rq_export, ioo, pp_rnb, &lockh);
956         if (rc != 0)
957                 GOTO(out_bulk, rc);
958
959         /* 
960          * If getting the lock took more time than
961          * client was willing to wait, drop it. b=11330
962          */
963         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
964             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
965                 no_reply = 1;
966                 CERROR("Dropping timed-out write from %s because locking"
967                        "object "LPX64" took %ld seconds.\n",
968                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
969                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
970                 goto out_lock;
971         }
972
973         ost_prolong_locks(req->rq_export, ioo, pp_rnb, LCK_PW);
974
975         /* obd_preprw clobbers oa->valid, so save what we need */
976         client_cksum = body->oa.o_valid & OBD_MD_FLCKSUM ? body->oa.o_cksum : 0;
977
978         rc = obd_preprw(OBD_BRW_WRITE, req->rq_export, &body->oa, objcount,
979                         ioo, npages, pp_rnb, local_nb, oti);
980         if (rc != 0)
981                 GOTO(out_lock, rc);
982
983         /* NB Having prepped, we must commit... */
984
985         for (i = 0; i < npages; i++)
986                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
987                                       pp_rnb[i].offset & ~CFS_PAGE_MASK,
988                                       pp_rnb[i].len);
989
990         /* Check if client was evicted while we were doing i/o before touching
991            network */
992         if (desc->bd_export->exp_failed)
993                 rc = -ENOTCONN;
994         else
995                 rc = ptlrpc_start_bulk_transfer (desc);
996         if (rc == 0) {
997                 lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 2, HZ,
998                                            ost_bulk_timeout, desc);
999                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1000                                   desc->bd_export->exp_failed, &lwi);
1001                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1002                 if (rc == -ETIMEDOUT) {
1003                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1004                         ptlrpc_abort_bulk(desc);
1005                 } else if (desc->bd_export->exp_failed) {
1006                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1007                         rc = -ENOTCONN;
1008                         ptlrpc_abort_bulk(desc);
1009                 } else if (!desc->bd_success ||
1010                            desc->bd_nob_transferred != desc->bd_nob) {
1011                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1012                                   desc->bd_success ?
1013                                   "truncated" : "network error on",
1014                                   desc->bd_nob_transferred, desc->bd_nob);
1015                         /* XXX should this be a different errno? */
1016                         rc = -ETIMEDOUT;
1017                 }
1018         } else {
1019                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
1020         }
1021         comms_error = rc != 0;
1022
1023         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1024                                  sizeof(*repbody));
1025         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1026
1027         if (unlikely(client_cksum != 0 && rc == 0)) {
1028                 static int cksum_counter;
1029
1030                 server_cksum = ost_checksum_bulk(desc);
1031                 repbody->oa.o_valid |= OBD_MD_FLCKSUM;
1032                 repbody->oa.o_cksum = server_cksum;
1033                 cksum_counter++;
1034                 if (unlikely(client_cksum != server_cksum)) {
1035                         CERROR("client csum %x, server csum %x\n",
1036                                client_cksum, server_cksum);
1037                         cksum_counter = 0;
1038                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1039                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1040                                cksum_counter, libcfs_id2str(req->rq_peer),
1041                                server_cksum);
1042                 }
1043         }
1044
1045         /* Must commit after prep above in all cases */
1046         rc = obd_commitrw(OBD_BRW_WRITE, req->rq_export, &repbody->oa,
1047                            objcount, ioo, npages, local_nb, oti, rc);
1048
1049         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1050                 int   new_cksum = ost_checksum_bulk(desc);
1051                 char *msg;
1052                 char *via;
1053                 char *router;
1054
1055                 if (new_cksum == server_cksum)
1056                         msg = "changed in transit before arrival at OST";
1057                 else if (new_cksum == client_cksum)
1058                         msg = "initial checksum before message complete";
1059                 else
1060                         msg = "changed in transit AND after initial checksum";
1061
1062                 if (req->rq_peer.nid == desc->bd_sender) {
1063                         via = router = "";
1064                 } else {
1065                         via = " via ";
1066                         router = libcfs_nid2str(desc->bd_sender);
1067                 }
1068                 
1069                 LCONSOLE_ERROR("%s: BAD WRITE CHECKSUM: %s from %s%s%s inum "
1070                                LPU64"/"LPU64" object "LPU64"/"LPU64
1071                                " extent ["LPU64"-"LPU64"]\n",
1072                                req->rq_export->exp_obd->obd_name, msg,
1073                                libcfs_id2str(req->rq_peer),
1074                                via, router,
1075                                body->oa.o_valid & OBD_MD_FLFID ?
1076                                                 body->oa.o_fid : (__u64)0,
1077                                body->oa.o_valid & OBD_MD_FLFID ?
1078                                                 body->oa.o_generation :(__u64)0,
1079                                body->oa.o_id,
1080                                body->oa.o_valid & OBD_MD_FLGROUP ?
1081                                                 body->oa.o_gr : (__u64)0,
1082                                pp_rnb[0].offset,
1083                                pp_rnb[npages-1].offset+pp_rnb[npages-1].len-1);
1084                 CERROR("client csum %x, original server csum %x, "
1085                        "server csum now %x\n",
1086                        client_cksum, server_cksum, new_cksum);
1087         }
1088
1089         ost_nio_pages_put(req, local_nb, npages);
1090
1091         if (rc == 0) {
1092                 /* set per-requested niobuf return codes */
1093                 for (i = j = 0; i < niocount; i++) {
1094                         int nob = remote_nb[i].len;
1095
1096                         rcs[i] = 0;
1097                         do {
1098                                 LASSERT(j < npages);
1099                                 if (local_nb[j].rc < 0)
1100                                         rcs[i] = local_nb[j].rc;
1101                                 nob -= pp_rnb[j].len;
1102                                 j++;
1103                         } while (nob > 0);
1104                         LASSERT(nob == 0);
1105                 }
1106                 LASSERT(j == npages);
1107         }
1108
1109  out_lock:
1110         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1111  out_bulk:
1112         ptlrpc_free_bulk(desc);
1113         if (no_reply)
1114                 RETURN(rc);
1115  out:
1116         if (rc == 0) {
1117                 oti_to_request(oti, req);
1118                 target_committed_to_req(req);
1119                 rc = ptlrpc_reply(req);
1120         } else if (!comms_error) {
1121                 /* Only reply if there was no comms problem with bulk */
1122                 target_committed_to_req(req);
1123                 req->rq_status = rc;
1124                 ptlrpc_error(req);
1125         } else {
1126                 if (req->rq_reply_state != NULL) {
1127                         /* reply out callback would free */
1128                         ptlrpc_rs_decref(req->rq_reply_state);
1129                         req->rq_reply_state = NULL;
1130                 }
1131                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1132                       "client will retry\n",
1133                       req->rq_export->exp_obd->obd_name,
1134                       req->rq_export->exp_client_uuid.uuid,
1135                       req->rq_export->exp_connection->c_remote_uuid.uuid,
1136                       libcfs_id2str(req->rq_peer));
1137         }
1138         RETURN(rc);
1139 }
1140
1141 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1142 {
1143         char *key, *val = NULL;
1144         int keylen, vallen, rc = 0;
1145         ENTRY;
1146
1147         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1148         if (key == NULL) {
1149                 DEBUG_REQ(D_HA, req, "no set_info key");
1150                 RETURN(-EFAULT);
1151         }
1152         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1153
1154         rc = lustre_pack_reply(req, 1, NULL, NULL);
1155         if (rc)
1156                 RETURN(rc);
1157
1158         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1159         if (vallen)
1160                 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1161
1162         if (KEY_IS("evict_by_nid")) {
1163                 if (val && vallen)
1164                         obd_export_evict_by_nid(exp->exp_obd, val);
1165
1166                 GOTO(out, rc = 0);
1167         }
1168
1169         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1170 out:
1171         lustre_msg_set_status(req->rq_repmsg, 0);
1172         RETURN(rc);
1173 }
1174
1175 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1176 {
1177         char *key;
1178         int keylen, rc = 0;
1179         int size[2] = { sizeof(struct ptlrpc_body), sizeof(obd_id) };
1180         obd_id *reply;
1181         ENTRY;
1182
1183         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1184         if (key == NULL) {
1185                 DEBUG_REQ(D_HA, req, "no get_info key");
1186                 RETURN(-EFAULT);
1187         }
1188         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1189
1190         if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
1191                 RETURN(-EPROTO);
1192
1193         rc = lustre_pack_reply(req, 2, size, NULL);
1194         if (rc)
1195                 RETURN(rc);
1196
1197         reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply));
1198         rc = obd_get_info(exp, keylen, key, size, reply);
1199         lustre_msg_set_status(req->rq_repmsg, 0);
1200         RETURN(rc);
1201 }
1202
1203 static int ost_handle_quotactl(struct ptlrpc_request *req)
1204 {
1205         struct obd_quotactl *oqctl, *repoqc;
1206         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1207         ENTRY;
1208
1209         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1210                                    lustre_swab_obd_quotactl);
1211         if (oqctl == NULL)
1212                 GOTO(out, rc = -EPROTO);
1213
1214         rc = lustre_pack_reply(req, 2, size, NULL);
1215         if (rc)
1216                 GOTO(out, rc);
1217
1218         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1219
1220         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1221         *repoqc = *oqctl;
1222 out:
1223         RETURN(rc);
1224 }
1225
1226 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1227 {
1228         struct obd_quotactl *oqctl;
1229         int rc;
1230         ENTRY;
1231
1232         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1233                                    lustre_swab_obd_quotactl);
1234         if (oqctl == NULL) 
1235                 RETURN(-EPROTO);
1236
1237         rc = lustre_pack_reply(req, 1, NULL, NULL);
1238         if (rc) {
1239                 CERROR("ost: out of memory while packing quotacheck reply\n");
1240                 RETURN(-ENOMEM);
1241         }
1242
1243         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1244         RETURN(0);
1245 }
1246
1247 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1248                                        struct obd_device *obd, int *process)
1249 {
1250         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1251         case OST_CONNECT: /* This will never get here, but for completeness. */
1252         case OST_DISCONNECT:
1253                *process = 1;
1254                RETURN(0);
1255
1256         case OBD_PING:
1257         case OST_CREATE:
1258         case OST_DESTROY:
1259         case OST_PUNCH:
1260         case OST_SETATTR:
1261         case OST_SYNC:
1262         case OST_WRITE:
1263         case OBD_LOG_CANCEL:
1264         case LDLM_ENQUEUE:
1265                 *process = target_queue_recovery_request(req, obd);
1266                 RETURN(0);
1267
1268         default:
1269                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1270                 *process = 0;
1271                 /* XXX what should we set rq_status to here? */
1272                 req->rq_status = -EAGAIN;
1273                 RETURN(ptlrpc_error(req));
1274         }
1275 }
1276
1277 int ost_msg_check_version(struct lustre_msg *msg)
1278 {
1279         int rc;
1280
1281         switch(lustre_msg_get_opc(msg)) {
1282         case OST_CONNECT:
1283         case OST_DISCONNECT:
1284         case OBD_PING:
1285                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1286                 if (rc)
1287                         CERROR("bad opc %u version %08x, expecting %08x\n",
1288                                lustre_msg_get_opc(msg),
1289                                lustre_msg_get_version(msg),
1290                                LUSTRE_OBD_VERSION);
1291                 break;
1292         case OST_CREATE:
1293         case OST_DESTROY:
1294         case OST_GETATTR:
1295         case OST_SETATTR:
1296         case OST_WRITE:
1297         case OST_READ:
1298         case OST_PUNCH:
1299         case OST_STATFS:
1300         case OST_SYNC:
1301         case OST_SET_INFO:
1302         case OST_GET_INFO:
1303         case OST_QUOTACHECK:
1304         case OST_QUOTACTL:
1305                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1306                 if (rc)
1307                         CERROR("bad opc %u version %08x, expecting %08x\n",
1308                                lustre_msg_get_opc(msg),
1309                                lustre_msg_get_version(msg),
1310                                LUSTRE_OST_VERSION);
1311                 break;
1312         case LDLM_ENQUEUE:
1313         case LDLM_CONVERT:
1314         case LDLM_CANCEL:
1315         case LDLM_BL_CALLBACK:
1316         case LDLM_CP_CALLBACK:
1317                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1318                 if (rc)
1319                         CERROR("bad opc %u version %08x, expecting %08x\n",
1320                                lustre_msg_get_opc(msg),
1321                                lustre_msg_get_version(msg),
1322                                LUSTRE_DLM_VERSION);
1323                 break;
1324         case LLOG_ORIGIN_CONNECT:
1325         case OBD_LOG_CANCEL:
1326                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1327                 if (rc)
1328                         CERROR("bad opc %u version %08x, expecting %08x\n",
1329                                lustre_msg_get_opc(msg),
1330                                lustre_msg_get_version(msg),
1331                                LUSTRE_LOG_VERSION);
1332                 break;
1333         default:
1334                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1335                 rc = -ENOTSUPP;
1336         }
1337         return rc;
1338 }
1339
1340 static int ost_handle(struct ptlrpc_request *req)
1341 {
1342         struct obd_trans_info trans_info = { 0, };
1343         struct obd_trans_info *oti = &trans_info;
1344         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1345         struct obd_device *obd = NULL;
1346         ENTRY;
1347
1348         LASSERT(current->journal_info == NULL);
1349         /* XXX identical to MDS */
1350         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1351                 int abort_recovery, recovering;
1352
1353                 if (req->rq_export == NULL) {
1354                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1355                                lustre_msg_get_opc(req->rq_reqmsg),
1356                                libcfs_id2str(req->rq_peer));
1357                         req->rq_status = -ENOTCONN;
1358                         GOTO(out, rc = -ENOTCONN);
1359                 }
1360
1361                 obd = req->rq_export->exp_obd;
1362
1363                 /* Check for aborted recovery. */
1364                 spin_lock_bh(&obd->obd_processing_task_lock);
1365                 abort_recovery = obd->obd_abort_recovery;
1366                 recovering = obd->obd_recovering;
1367                 spin_unlock_bh(&obd->obd_processing_task_lock);
1368                 if (abort_recovery) {
1369                         target_abort_recovery(obd);
1370                 } else if (recovering) {
1371                         rc = ost_filter_recovery_request(req, obd,
1372                                                          &should_process);
1373                         if (rc || !should_process)
1374                                 RETURN(rc);
1375                 }
1376         }
1377
1378         oti_init(oti, req);
1379         rc = ost_msg_check_version(req->rq_reqmsg);
1380         if (rc)
1381                 RETURN(rc);
1382
1383         rc = ost_msg_check_version(req->rq_reqmsg);
1384         if (rc)
1385                 RETURN(rc);
1386
1387         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1388         case OST_CONNECT: {
1389                 CDEBUG(D_INODE, "connect\n");
1390                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
1391                 rc = target_handle_connect(req, ost_handle);
1392                 if (!rc)
1393                         obd = req->rq_export->exp_obd;
1394                 break;
1395         }
1396         case OST_DISCONNECT:
1397                 CDEBUG(D_INODE, "disconnect\n");
1398                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
1399                 rc = target_handle_disconnect(req);
1400                 break;
1401         case OST_CREATE:
1402                 CDEBUG(D_INODE, "create\n");
1403                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
1404                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1405                         GOTO(out, rc = -ENOSPC);
1406                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1407                         GOTO(out, rc = -EROFS);
1408                 rc = ost_create(req->rq_export, req, oti);
1409                 break;
1410         case OST_DESTROY:
1411                 CDEBUG(D_INODE, "destroy\n");
1412                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
1413                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1414                         GOTO(out, rc = -EROFS);
1415                 rc = ost_destroy(req->rq_export, req, oti);
1416                 break;
1417         case OST_GETATTR:
1418                 CDEBUG(D_INODE, "getattr\n");
1419                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
1420                 rc = ost_getattr(req->rq_export, req);
1421                 break;
1422         case OST_SETATTR:
1423                 CDEBUG(D_INODE, "setattr\n");
1424                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
1425                 rc = ost_setattr(req->rq_export, req, oti);
1426                 break;
1427         case OST_WRITE:
1428                 CDEBUG(D_INODE, "write\n");
1429                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1430                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1431                         GOTO(out, rc = -ENOSPC);
1432                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1433                         GOTO(out, rc = -EROFS);
1434                 rc = ost_brw_write(req, oti);
1435                 LASSERT(current->journal_info == NULL);
1436                 /* ost_brw_write sends its own replies */
1437                 RETURN(rc);
1438         case OST_READ:
1439                 CDEBUG(D_INODE, "read\n");
1440                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1441                 rc = ost_brw_read(req, oti);
1442                 LASSERT(current->journal_info == NULL);
1443                 /* ost_brw_read sends its own replies */
1444                 RETURN(rc);
1445         case OST_PUNCH:
1446                 CDEBUG(D_INODE, "punch\n");
1447                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
1448                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1449                         GOTO(out, rc = -EROFS);
1450                 rc = ost_punch(req->rq_export, req, oti);
1451                 break;
1452         case OST_STATFS:
1453                 CDEBUG(D_INODE, "statfs\n");
1454                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
1455                 rc = ost_statfs(req);
1456                 break;
1457         case OST_SYNC:
1458                 CDEBUG(D_INODE, "sync\n");
1459                 OBD_FAIL_RETURN(OBD_FAIL_OST_SYNC_NET, 0);
1460                 rc = ost_sync(req->rq_export, req);
1461                 break;
1462         case OST_SET_INFO:
1463                 DEBUG_REQ(D_INODE, req, "set_info");
1464                 rc = ost_set_info(req->rq_export, req);
1465                 break;
1466         case OST_GET_INFO:
1467                 DEBUG_REQ(D_INODE, req, "get_info");
1468                 rc = ost_get_info(req->rq_export, req);
1469                 break;
1470         case OST_QUOTACHECK:
1471                 CDEBUG(D_INODE, "quotacheck\n");
1472                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACHECK_NET, 0);
1473                 rc = ost_handle_quotacheck(req);
1474                 break;
1475         case OST_QUOTACTL:
1476                 CDEBUG(D_INODE, "quotactl\n");
1477                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACTL_NET, 0);
1478                 rc = ost_handle_quotactl(req);
1479                 break;
1480         case OBD_PING:
1481                 DEBUG_REQ(D_INODE, req, "ping");
1482                 rc = target_handle_ping(req);
1483                 break;
1484         /* FIXME - just reply status */
1485         case LLOG_ORIGIN_CONNECT:
1486                 DEBUG_REQ(D_INODE, req, "log connect\n");
1487                 rc = llog_handle_connect(req);
1488                 req->rq_status = rc;
1489                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1490                 if (rc)
1491                         RETURN(rc);
1492                 RETURN(ptlrpc_reply(req));
1493         case OBD_LOG_CANCEL:
1494                 CDEBUG(D_INODE, "log cancel\n");
1495                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1496                 rc = llog_origin_handle_cancel(req);
1497                 req->rq_status = rc;
1498                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1499                 if (rc)
1500                         RETURN(rc);
1501                 RETURN(ptlrpc_reply(req));
1502         case LDLM_ENQUEUE:
1503                 CDEBUG(D_INODE, "enqueue\n");
1504                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1505                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1506                                          ldlm_server_blocking_ast,
1507                                          ldlm_server_glimpse_ast);
1508                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1509                 break;
1510         case LDLM_CONVERT:
1511                 CDEBUG(D_INODE, "convert\n");
1512                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1513                 rc = ldlm_handle_convert(req);
1514                 break;
1515         case LDLM_CANCEL:
1516                 CDEBUG(D_INODE, "cancel\n");
1517                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1518                 rc = ldlm_handle_cancel(req);
1519                 break;
1520         case LDLM_BL_CALLBACK:
1521         case LDLM_CP_CALLBACK:
1522                 CDEBUG(D_INODE, "callback\n");
1523                 CERROR("callbacks should not happen on OST\n");
1524                 /* fall through */
1525         default:
1526                 CERROR("Unexpected opcode %d\n",
1527                        lustre_msg_get_opc(req->rq_reqmsg));
1528                 req->rq_status = -ENOTSUPP;
1529                 rc = ptlrpc_error(req);
1530                 RETURN(rc);
1531         }
1532
1533         LASSERT(current->journal_info == NULL);
1534
1535         EXIT;
1536         /* If we're DISCONNECTing, the export_data is already freed */
1537         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1538                 target_committed_to_req(req);
1539
1540 out:
1541         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1542                 if (obd && obd->obd_recovering) {
1543                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1544                         return target_queue_final_reply(req, rc);
1545                 }
1546                 /* Lost a race with recovery; let the error path DTRT. */
1547                 rc = req->rq_status = -ENOTCONN;
1548         }
1549
1550         if (!rc)
1551                 oti_to_request(oti, req);
1552
1553         target_send_reply(req, rc, fail);
1554         return 0;
1555 }
1556
1557 /*
1558  * free per-thread pool created by ost_thread_init().
1559  */
1560 static void ost_thread_done(struct ptlrpc_thread *thread)
1561 {
1562         int i;
1563         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1564                                              * Storage */
1565
1566         ENTRY;
1567
1568         LASSERT(thread != NULL);
1569
1570         /*
1571          * be prepared to handle partially-initialized pools (because this is
1572          * called from ost_thread_init() for cleanup.
1573          */
1574         tls = thread->t_data;
1575         if (tls != NULL) {
1576                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1577                         if (tls->page[i] != NULL)
1578                                 __free_page(tls->page[i]);
1579                 }
1580                 OBD_FREE_PTR(tls);
1581                 thread->t_data = NULL;
1582         }
1583         EXIT;
1584 }
1585
1586 /*
1587  * initialize per-thread page pool (bug 5137).
1588  */
1589 static int ost_thread_init(struct ptlrpc_thread *thread)
1590 {
1591         int result;
1592         int i;
1593         struct ost_thread_local_cache *tls;
1594
1595         ENTRY;
1596
1597         LASSERT(thread != NULL);
1598         LASSERT(thread->t_data == NULL);
1599         LASSERT(thread->t_id <= OSS_THREADS_MAX);
1600
1601         OBD_ALLOC_PTR(tls);
1602         if (tls != NULL) {
1603                 result = 0;
1604                 thread->t_data = tls;
1605                 /*
1606                  * populate pool
1607                  */
1608                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1609                         tls->page[i] = alloc_page(OST_THREAD_POOL_GFP);
1610                         if (tls->page[i] == NULL) {
1611                                 ost_thread_done(thread);
1612                                 result = -ENOMEM;
1613                                 break;
1614                         }
1615                 }
1616         } else
1617                 result = -ENOMEM;
1618         RETURN(result);
1619 }
1620
1621 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1622 static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
1623 {
1624         struct ost_obd *ost = &obd->u.ost;
1625         struct lprocfs_static_vars lvars;
1626         int oss_min_threads;
1627         int oss_max_threads;
1628         int rc;
1629         ENTRY;
1630
1631         rc = cleanup_group_info();
1632         if (rc)
1633                 RETURN(rc);
1634
1635         rc = llog_start_commit_thread();
1636         if (rc < 0)
1637                 RETURN(rc);
1638
1639         lprocfs_init_vars(ost, &lvars);
1640         lprocfs_obd_setup(obd, lvars.obd_vars);
1641
1642         sema_init(&ost->ost_health_sem, 1);
1643
1644         if (oss_num_threads) {
1645                 /* If oss_num_threads is set, it is the min and the max. */
1646                 if (oss_num_threads > OSS_THREADS_MAX) 
1647                         oss_num_threads = OSS_THREADS_MAX;
1648                 if (oss_num_threads < OSS_THREADS_MIN)
1649                         oss_num_threads = OSS_THREADS_MIN;
1650                 oss_max_threads = oss_min_threads = oss_num_threads;
1651         } else {
1652                 /* Base min threads on memory and cpus */
1653                 oss_min_threads = smp_num_cpus * num_physpages >> 
1654                         (27 - CFS_PAGE_SHIFT);
1655                 if (oss_min_threads < OSS_THREADS_MIN)
1656                         oss_min_threads = OSS_THREADS_MIN;
1657                 /* Insure a 4x range for dynamic threads */
1658                 if (oss_min_threads > OSS_THREADS_MAX / 4) 
1659                         oss_min_threads = OSS_THREADS_MAX / 4;
1660                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1661         }
1662
1663         ost->ost_service =
1664                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1665                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1666                                 OSC_REPLY_PORTAL,
1667                                 obd_timeout * 1000, ost_handle, LUSTRE_OSS_NAME,
1668                                 obd->obd_proc_entry, ost_print_req,
1669                                 oss_min_threads, oss_max_threads, "ll_ost");
1670         if (ost->ost_service == NULL) {
1671                 CERROR("failed to start OST service\n");
1672                 GOTO(out_lprocfs, rc = -ENOMEM);
1673         }
1674
1675         rc = ptlrpc_start_threads(obd, ost->ost_service);
1676         if (rc)
1677                 GOTO(out_service, rc = -EINVAL);
1678
1679         ost->ost_create_service =
1680                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1681                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1682                                 OSC_REPLY_PORTAL,
1683                                 obd_timeout * 1000, ost_handle, "ost_create",
1684                                 obd->obd_proc_entry, ost_print_req,
1685                                 1, 1, "ll_ost_creat");
1686         if (ost->ost_create_service == NULL) {
1687                 CERROR("failed to start OST create service\n");
1688                 GOTO(out_service, rc = -ENOMEM);
1689         }
1690
1691         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1692         if (rc)
1693                 GOTO(out_create, rc = -EINVAL);
1694
1695         ost->ost_io_service =
1696                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1697                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1698                                 OSC_REPLY_PORTAL,
1699                                 obd_timeout * 1000, ost_handle, "ost_io",
1700                                 obd->obd_proc_entry, ost_print_req,
1701                                 oss_min_threads, oss_max_threads, "ll_ost_io");
1702         if (ost->ost_io_service == NULL) {
1703                 CERROR("failed to start OST I/O service\n");
1704                 GOTO(out_create, rc = -ENOMEM);
1705         }
1706
1707         ost->ost_io_service->srv_init = ost_thread_init;
1708         ost->ost_io_service->srv_done = ost_thread_done;
1709         ost->ost_io_service->srv_cpu_affinity = 1;
1710         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1711         if (rc)
1712                 GOTO(out_io, rc = -EINVAL);
1713
1714         ping_evictor_start();
1715
1716         RETURN(0);
1717
1718 out_io:
1719         ptlrpc_unregister_service(ost->ost_io_service);
1720         ost->ost_io_service = NULL;
1721 out_create:
1722         ptlrpc_unregister_service(ost->ost_create_service);
1723         ost->ost_create_service = NULL;
1724 out_service:
1725         ptlrpc_unregister_service(ost->ost_service);
1726         ost->ost_service = NULL;
1727 out_lprocfs:
1728         lprocfs_obd_cleanup(obd);
1729         RETURN(rc);
1730 }
1731
1732 static int ost_cleanup(struct obd_device *obd)
1733 {
1734         struct ost_obd *ost = &obd->u.ost;
1735         int err = 0;
1736         ENTRY;
1737
1738         ping_evictor_stop();
1739
1740         spin_lock_bh(&obd->obd_processing_task_lock);
1741         if (obd->obd_recovering) {
1742                 target_cancel_recovery_timer(obd);
1743                 obd->obd_recovering = 0;
1744         }
1745         spin_unlock_bh(&obd->obd_processing_task_lock);
1746
1747         down(&ost->ost_health_sem);
1748         ptlrpc_unregister_service(ost->ost_service);
1749         ptlrpc_unregister_service(ost->ost_create_service);
1750         ptlrpc_unregister_service(ost->ost_io_service);
1751         ost->ost_service = NULL;
1752         ost->ost_create_service = NULL;
1753         up(&ost->ost_health_sem);
1754
1755         lprocfs_obd_cleanup(obd);
1756
1757         RETURN(err);
1758 }
1759
1760 static int ost_health_check(struct obd_device *obd)
1761 {
1762         struct ost_obd *ost = &obd->u.ost;
1763         int rc = 0;
1764
1765         down(&ost->ost_health_sem);
1766         rc |= ptlrpc_service_health_check(ost->ost_service);
1767         rc |= ptlrpc_service_health_check(ost->ost_create_service);
1768         rc |= ptlrpc_service_health_check(ost->ost_io_service);
1769         up(&ost->ost_health_sem);
1770
1771         /*
1772          * health_check to return 0 on healthy
1773          * and 1 on unhealthy.
1774          */
1775         if( rc != 0)
1776                 rc = 1;
1777
1778         return rc;
1779 }
1780
1781 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
1782 {
1783         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
1784 }
1785
1786 /* use obd ops to offer management infrastructure */
1787 static struct obd_ops ost_obd_ops = {
1788         .o_owner        = THIS_MODULE,
1789         .o_setup        = ost_setup,
1790         .o_cleanup      = ost_cleanup,
1791         .o_health_check = ost_health_check,
1792 };
1793
1794
1795 static int __init ost_init(void)
1796 {
1797         struct lprocfs_static_vars lvars;
1798         int rc;
1799         ENTRY;
1800
1801         lprocfs_init_vars(ost, &lvars);
1802         rc = class_register_type(&ost_obd_ops, lvars.module_vars,
1803                                  LUSTRE_OSS_NAME);
1804         RETURN(rc);
1805 }
1806
1807 static void /*__exit*/ ost_exit(void)
1808 {
1809         class_unregister_type(LUSTRE_OSS_NAME);
1810 }
1811
1812 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1813 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
1814 MODULE_LICENSE("GPL");
1815
1816 module_init(ost_init);
1817 module_exit(ost_exit);