Whamcloud - gitweb
0e7203ba2b4c0ae4fdd22a1ce13aac1dee125e91
[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
1053                 if (new_cksum == server_cksum)
1054                         msg = "changed in transit before arrival at OST";
1055                 else if (new_cksum == client_cksum)
1056                         msg = "initial checksum before message complete";
1057                 else
1058                         msg = "changed in transit AND after initial checksum";
1059
1060                 LCONSOLE_ERROR("%s: BAD WRITE CHECKSUM: %s from %s inum "
1061                                LPU64"/"LPU64" object "LPU64"/"LPU64
1062                                " extent ["LPU64"-"LPU64"]\n",
1063                                req->rq_export->exp_obd->obd_name, msg,
1064                                libcfs_id2str(req->rq_peer),
1065                                body->oa.o_valid & OBD_MD_FLFID ?
1066                                                 body->oa.o_fid : (__u64)0,
1067                                body->oa.o_valid & OBD_MD_FLFID ?
1068                                                 body->oa.o_generation :(__u64)0,
1069                                body->oa.o_id,
1070                                body->oa.o_valid & OBD_MD_FLGROUP ?
1071                                                 body->oa.o_gr : (__u64)0,
1072                                pp_rnb[0].offset,
1073                                pp_rnb[npages-1].offset+pp_rnb[npages-1].len-1);
1074                 CERROR("client csum %x, original server csum %x, "
1075                        "server csum now %x\n",
1076                        client_cksum, server_cksum, new_cksum);
1077         }
1078
1079         ost_nio_pages_put(req, local_nb, npages);
1080
1081         if (rc == 0) {
1082                 /* set per-requested niobuf return codes */
1083                 for (i = j = 0; i < niocount; i++) {
1084                         int nob = remote_nb[i].len;
1085
1086                         rcs[i] = 0;
1087                         do {
1088                                 LASSERT(j < npages);
1089                                 if (local_nb[j].rc < 0)
1090                                         rcs[i] = local_nb[j].rc;
1091                                 nob -= pp_rnb[j].len;
1092                                 j++;
1093                         } while (nob > 0);
1094                         LASSERT(nob == 0);
1095                 }
1096                 LASSERT(j == npages);
1097         }
1098
1099  out_lock:
1100         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1101  out_bulk:
1102         ptlrpc_free_bulk(desc);
1103         if (no_reply)
1104                 RETURN(rc);
1105  out:
1106         if (rc == 0) {
1107                 oti_to_request(oti, req);
1108                 target_committed_to_req(req);
1109                 rc = ptlrpc_reply(req);
1110         } else if (!comms_error) {
1111                 /* Only reply if there was no comms problem with bulk */
1112                 target_committed_to_req(req);
1113                 req->rq_status = rc;
1114                 ptlrpc_error(req);
1115         } else {
1116                 if (req->rq_reply_state != NULL) {
1117                         /* reply out callback would free */
1118                         ptlrpc_rs_decref(req->rq_reply_state);
1119                         req->rq_reply_state = NULL;
1120                 }
1121                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1122                       "client will retry\n",
1123                       req->rq_export->exp_obd->obd_name,
1124                       req->rq_export->exp_client_uuid.uuid,
1125                       req->rq_export->exp_connection->c_remote_uuid.uuid,
1126                       libcfs_id2str(req->rq_peer));
1127         }
1128         RETURN(rc);
1129 }
1130
1131 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1132 {
1133         char *key, *val = NULL;
1134         int keylen, vallen, rc = 0;
1135         ENTRY;
1136
1137         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1138         if (key == NULL) {
1139                 DEBUG_REQ(D_HA, req, "no set_info key");
1140                 RETURN(-EFAULT);
1141         }
1142         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1143
1144         rc = lustre_pack_reply(req, 1, NULL, NULL);
1145         if (rc)
1146                 RETURN(rc);
1147
1148         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1149         if (vallen)
1150                 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1151
1152         if (KEY_IS("evict_by_nid")) {
1153                 if (val && vallen)
1154                         obd_export_evict_by_nid(exp->exp_obd, val);
1155
1156                 GOTO(out, rc = 0);
1157         }
1158
1159         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1160 out:
1161         lustre_msg_set_status(req->rq_repmsg, 0);
1162         RETURN(rc);
1163 }
1164
1165 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1166 {
1167         char *key;
1168         int keylen, rc = 0;
1169         int size[2] = { sizeof(struct ptlrpc_body), sizeof(obd_id) };
1170         obd_id *reply;
1171         ENTRY;
1172
1173         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1174         if (key == NULL) {
1175                 DEBUG_REQ(D_HA, req, "no get_info key");
1176                 RETURN(-EFAULT);
1177         }
1178         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1179
1180         if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
1181                 RETURN(-EPROTO);
1182
1183         rc = lustre_pack_reply(req, 2, size, NULL);
1184         if (rc)
1185                 RETURN(rc);
1186
1187         reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply));
1188         rc = obd_get_info(exp, keylen, key, size, reply);
1189         lustre_msg_set_status(req->rq_repmsg, 0);
1190         RETURN(rc);
1191 }
1192
1193 static int ost_handle_quotactl(struct ptlrpc_request *req)
1194 {
1195         struct obd_quotactl *oqctl, *repoqc;
1196         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1197         ENTRY;
1198
1199         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1200                                    lustre_swab_obd_quotactl);
1201         if (oqctl == NULL)
1202                 GOTO(out, rc = -EPROTO);
1203
1204         rc = lustre_pack_reply(req, 2, size, NULL);
1205         if (rc)
1206                 GOTO(out, rc);
1207
1208         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1209
1210         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1211         *repoqc = *oqctl;
1212 out:
1213         RETURN(rc);
1214 }
1215
1216 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1217 {
1218         struct obd_quotactl *oqctl;
1219         int rc;
1220         ENTRY;
1221
1222         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1223                                    lustre_swab_obd_quotactl);
1224         if (oqctl == NULL) 
1225                 RETURN(-EPROTO);
1226
1227         rc = lustre_pack_reply(req, 1, NULL, NULL);
1228         if (rc) {
1229                 CERROR("ost: out of memory while packing quotacheck reply\n");
1230                 RETURN(-ENOMEM);
1231         }
1232
1233         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1234         RETURN(0);
1235 }
1236
1237 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1238                                        struct obd_device *obd, int *process)
1239 {
1240         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1241         case OST_CONNECT: /* This will never get here, but for completeness. */
1242         case OST_DISCONNECT:
1243                *process = 1;
1244                RETURN(0);
1245
1246         case OBD_PING:
1247         case OST_CREATE:
1248         case OST_DESTROY:
1249         case OST_PUNCH:
1250         case OST_SETATTR:
1251         case OST_SYNC:
1252         case OST_WRITE:
1253         case OBD_LOG_CANCEL:
1254         case LDLM_ENQUEUE:
1255                 *process = target_queue_recovery_request(req, obd);
1256                 RETURN(0);
1257
1258         default:
1259                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1260                 *process = 0;
1261                 /* XXX what should we set rq_status to here? */
1262                 req->rq_status = -EAGAIN;
1263                 RETURN(ptlrpc_error(req));
1264         }
1265 }
1266
1267 int ost_msg_check_version(struct lustre_msg *msg)
1268 {
1269         int rc;
1270
1271         switch(lustre_msg_get_opc(msg)) {
1272         case OST_CONNECT:
1273         case OST_DISCONNECT:
1274         case OBD_PING:
1275                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1276                 if (rc)
1277                         CERROR("bad opc %u version %08x, expecting %08x\n",
1278                                lustre_msg_get_opc(msg),
1279                                lustre_msg_get_version(msg),
1280                                LUSTRE_OBD_VERSION);
1281                 break;
1282         case OST_CREATE:
1283         case OST_DESTROY:
1284         case OST_GETATTR:
1285         case OST_SETATTR:
1286         case OST_WRITE:
1287         case OST_READ:
1288         case OST_PUNCH:
1289         case OST_STATFS:
1290         case OST_SYNC:
1291         case OST_SET_INFO:
1292         case OST_GET_INFO:
1293         case OST_QUOTACHECK:
1294         case OST_QUOTACTL:
1295                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1296                 if (rc)
1297                         CERROR("bad opc %u version %08x, expecting %08x\n",
1298                                lustre_msg_get_opc(msg),
1299                                lustre_msg_get_version(msg),
1300                                LUSTRE_OST_VERSION);
1301                 break;
1302         case LDLM_ENQUEUE:
1303         case LDLM_CONVERT:
1304         case LDLM_CANCEL:
1305         case LDLM_BL_CALLBACK:
1306         case LDLM_CP_CALLBACK:
1307                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1308                 if (rc)
1309                         CERROR("bad opc %u version %08x, expecting %08x\n",
1310                                lustre_msg_get_opc(msg),
1311                                lustre_msg_get_version(msg),
1312                                LUSTRE_DLM_VERSION);
1313                 break;
1314         case LLOG_ORIGIN_CONNECT:
1315         case OBD_LOG_CANCEL:
1316                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1317                 if (rc)
1318                         CERROR("bad opc %u version %08x, expecting %08x\n",
1319                                lustre_msg_get_opc(msg),
1320                                lustre_msg_get_version(msg),
1321                                LUSTRE_LOG_VERSION);
1322                 break;
1323         default:
1324                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1325                 rc = -ENOTSUPP;
1326         }
1327         return rc;
1328 }
1329
1330 static int ost_handle(struct ptlrpc_request *req)
1331 {
1332         struct obd_trans_info trans_info = { 0, };
1333         struct obd_trans_info *oti = &trans_info;
1334         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1335         struct obd_device *obd = NULL;
1336         ENTRY;
1337
1338         LASSERT(current->journal_info == NULL);
1339         /* XXX identical to MDS */
1340         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1341                 int abort_recovery, recovering;
1342
1343                 if (req->rq_export == NULL) {
1344                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1345                                lustre_msg_get_opc(req->rq_reqmsg),
1346                                libcfs_id2str(req->rq_peer));
1347                         req->rq_status = -ENOTCONN;
1348                         GOTO(out, rc = -ENOTCONN);
1349                 }
1350
1351                 obd = req->rq_export->exp_obd;
1352
1353                 /* Check for aborted recovery. */
1354                 spin_lock_bh(&obd->obd_processing_task_lock);
1355                 abort_recovery = obd->obd_abort_recovery;
1356                 recovering = obd->obd_recovering;
1357                 spin_unlock_bh(&obd->obd_processing_task_lock);
1358                 if (abort_recovery) {
1359                         target_abort_recovery(obd);
1360                 } else if (recovering) {
1361                         rc = ost_filter_recovery_request(req, obd,
1362                                                          &should_process);
1363                         if (rc || !should_process)
1364                                 RETURN(rc);
1365                 }
1366         }
1367
1368         oti_init(oti, req);
1369         rc = ost_msg_check_version(req->rq_reqmsg);
1370         if (rc)
1371                 RETURN(rc);
1372
1373         rc = ost_msg_check_version(req->rq_reqmsg);
1374         if (rc)
1375                 RETURN(rc);
1376
1377         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1378         case OST_CONNECT: {
1379                 CDEBUG(D_INODE, "connect\n");
1380                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
1381                 rc = target_handle_connect(req, ost_handle);
1382                 if (!rc)
1383                         obd = req->rq_export->exp_obd;
1384                 break;
1385         }
1386         case OST_DISCONNECT:
1387                 CDEBUG(D_INODE, "disconnect\n");
1388                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
1389                 rc = target_handle_disconnect(req);
1390                 break;
1391         case OST_CREATE:
1392                 CDEBUG(D_INODE, "create\n");
1393                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
1394                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1395                         GOTO(out, rc = -ENOSPC);
1396                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1397                         GOTO(out, rc = -EROFS);
1398                 rc = ost_create(req->rq_export, req, oti);
1399                 break;
1400         case OST_DESTROY:
1401                 CDEBUG(D_INODE, "destroy\n");
1402                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
1403                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1404                         GOTO(out, rc = -EROFS);
1405                 rc = ost_destroy(req->rq_export, req, oti);
1406                 break;
1407         case OST_GETATTR:
1408                 CDEBUG(D_INODE, "getattr\n");
1409                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
1410                 rc = ost_getattr(req->rq_export, req);
1411                 break;
1412         case OST_SETATTR:
1413                 CDEBUG(D_INODE, "setattr\n");
1414                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
1415                 rc = ost_setattr(req->rq_export, req, oti);
1416                 break;
1417         case OST_WRITE:
1418                 CDEBUG(D_INODE, "write\n");
1419                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1420                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1421                         GOTO(out, rc = -ENOSPC);
1422                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1423                         GOTO(out, rc = -EROFS);
1424                 rc = ost_brw_write(req, oti);
1425                 LASSERT(current->journal_info == NULL);
1426                 /* ost_brw_write sends its own replies */
1427                 RETURN(rc);
1428         case OST_READ:
1429                 CDEBUG(D_INODE, "read\n");
1430                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1431                 rc = ost_brw_read(req, oti);
1432                 LASSERT(current->journal_info == NULL);
1433                 /* ost_brw_read sends its own replies */
1434                 RETURN(rc);
1435         case OST_PUNCH:
1436                 CDEBUG(D_INODE, "punch\n");
1437                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
1438                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1439                         GOTO(out, rc = -EROFS);
1440                 rc = ost_punch(req->rq_export, req, oti);
1441                 break;
1442         case OST_STATFS:
1443                 CDEBUG(D_INODE, "statfs\n");
1444                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
1445                 rc = ost_statfs(req);
1446                 break;
1447         case OST_SYNC:
1448                 CDEBUG(D_INODE, "sync\n");
1449                 OBD_FAIL_RETURN(OBD_FAIL_OST_SYNC_NET, 0);
1450                 rc = ost_sync(req->rq_export, req);
1451                 break;
1452         case OST_SET_INFO:
1453                 DEBUG_REQ(D_INODE, req, "set_info");
1454                 rc = ost_set_info(req->rq_export, req);
1455                 break;
1456         case OST_GET_INFO:
1457                 DEBUG_REQ(D_INODE, req, "get_info");
1458                 rc = ost_get_info(req->rq_export, req);
1459                 break;
1460         case OST_QUOTACHECK:
1461                 CDEBUG(D_INODE, "quotacheck\n");
1462                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACHECK_NET, 0);
1463                 rc = ost_handle_quotacheck(req);
1464                 break;
1465         case OST_QUOTACTL:
1466                 CDEBUG(D_INODE, "quotactl\n");
1467                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACTL_NET, 0);
1468                 rc = ost_handle_quotactl(req);
1469                 break;
1470         case OBD_PING:
1471                 DEBUG_REQ(D_INODE, req, "ping");
1472                 rc = target_handle_ping(req);
1473                 break;
1474         /* FIXME - just reply status */
1475         case LLOG_ORIGIN_CONNECT:
1476                 DEBUG_REQ(D_INODE, req, "log connect\n");
1477                 rc = llog_handle_connect(req);
1478                 req->rq_status = rc;
1479                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1480                 if (rc)
1481                         RETURN(rc);
1482                 RETURN(ptlrpc_reply(req));
1483         case OBD_LOG_CANCEL:
1484                 CDEBUG(D_INODE, "log cancel\n");
1485                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1486                 rc = llog_origin_handle_cancel(req);
1487                 req->rq_status = rc;
1488                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1489                 if (rc)
1490                         RETURN(rc);
1491                 RETURN(ptlrpc_reply(req));
1492         case LDLM_ENQUEUE:
1493                 CDEBUG(D_INODE, "enqueue\n");
1494                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1495                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1496                                          ldlm_server_blocking_ast,
1497                                          ldlm_server_glimpse_ast);
1498                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1499                 break;
1500         case LDLM_CONVERT:
1501                 CDEBUG(D_INODE, "convert\n");
1502                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1503                 rc = ldlm_handle_convert(req);
1504                 break;
1505         case LDLM_CANCEL:
1506                 CDEBUG(D_INODE, "cancel\n");
1507                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1508                 rc = ldlm_handle_cancel(req);
1509                 break;
1510         case LDLM_BL_CALLBACK:
1511         case LDLM_CP_CALLBACK:
1512                 CDEBUG(D_INODE, "callback\n");
1513                 CERROR("callbacks should not happen on OST\n");
1514                 /* fall through */
1515         default:
1516                 CERROR("Unexpected opcode %d\n",
1517                        lustre_msg_get_opc(req->rq_reqmsg));
1518                 req->rq_status = -ENOTSUPP;
1519                 rc = ptlrpc_error(req);
1520                 RETURN(rc);
1521         }
1522
1523         LASSERT(current->journal_info == NULL);
1524
1525         EXIT;
1526         /* If we're DISCONNECTing, the export_data is already freed */
1527         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1528                 target_committed_to_req(req);
1529
1530 out:
1531         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1532                 if (obd && obd->obd_recovering) {
1533                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1534                         return target_queue_final_reply(req, rc);
1535                 }
1536                 /* Lost a race with recovery; let the error path DTRT. */
1537                 rc = req->rq_status = -ENOTCONN;
1538         }
1539
1540         if (!rc)
1541                 oti_to_request(oti, req);
1542
1543         target_send_reply(req, rc, fail);
1544         return 0;
1545 }
1546
1547 /*
1548  * free per-thread pool created by ost_thread_init().
1549  */
1550 static void ost_thread_done(struct ptlrpc_thread *thread)
1551 {
1552         int i;
1553         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1554                                              * Storage */
1555
1556         ENTRY;
1557
1558         LASSERT(thread != NULL);
1559
1560         /*
1561          * be prepared to handle partially-initialized pools (because this is
1562          * called from ost_thread_init() for cleanup.
1563          */
1564         tls = thread->t_data;
1565         if (tls != NULL) {
1566                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1567                         if (tls->page[i] != NULL)
1568                                 __free_page(tls->page[i]);
1569                 }
1570                 OBD_FREE_PTR(tls);
1571                 thread->t_data = NULL;
1572         }
1573         EXIT;
1574 }
1575
1576 /*
1577  * initialize per-thread page pool (bug 5137).
1578  */
1579 static int ost_thread_init(struct ptlrpc_thread *thread)
1580 {
1581         int result;
1582         int i;
1583         struct ost_thread_local_cache *tls;
1584
1585         ENTRY;
1586
1587         LASSERT(thread != NULL);
1588         LASSERT(thread->t_data == NULL);
1589         LASSERT(thread->t_id <= OSS_THREADS_MAX);
1590
1591         OBD_ALLOC_PTR(tls);
1592         if (tls != NULL) {
1593                 result = 0;
1594                 thread->t_data = tls;
1595                 /*
1596                  * populate pool
1597                  */
1598                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1599                         tls->page[i] = alloc_page(OST_THREAD_POOL_GFP);
1600                         if (tls->page[i] == NULL) {
1601                                 ost_thread_done(thread);
1602                                 result = -ENOMEM;
1603                                 break;
1604                         }
1605                 }
1606         } else
1607                 result = -ENOMEM;
1608         RETURN(result);
1609 }
1610
1611 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1612 static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
1613 {
1614         struct ost_obd *ost = &obd->u.ost;
1615         struct lprocfs_static_vars lvars;
1616         int oss_min_threads;
1617         int oss_max_threads;
1618         int rc;
1619         ENTRY;
1620
1621         rc = cleanup_group_info();
1622         if (rc)
1623                 RETURN(rc);
1624
1625         rc = llog_start_commit_thread();
1626         if (rc < 0)
1627                 RETURN(rc);
1628
1629         lprocfs_init_vars(ost, &lvars);
1630         lprocfs_obd_setup(obd, lvars.obd_vars);
1631
1632         sema_init(&ost->ost_health_sem, 1);
1633
1634         if (oss_num_threads) {
1635                 /* If oss_num_threads is set, it is the min and the max. */
1636                 if (oss_num_threads > OSS_THREADS_MAX) 
1637                         oss_num_threads = OSS_THREADS_MAX;
1638                 if (oss_num_threads < OSS_THREADS_MIN)
1639                         oss_num_threads = OSS_THREADS_MIN;
1640                 oss_max_threads = oss_min_threads = oss_num_threads;
1641         } else {
1642                 /* Base min threads on memory and cpus */
1643                 oss_min_threads = smp_num_cpus * num_physpages >> 
1644                         (27 - CFS_PAGE_SHIFT);
1645                 if (oss_min_threads < OSS_THREADS_MIN)
1646                         oss_min_threads = OSS_THREADS_MIN;
1647                 /* Insure a 4x range for dynamic threads */
1648                 if (oss_min_threads > OSS_THREADS_MAX / 4) 
1649                         oss_min_threads = OSS_THREADS_MAX / 4;
1650                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1651         }
1652
1653         ost->ost_service =
1654                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1655                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1656                                 OSC_REPLY_PORTAL,
1657                                 obd_timeout * 1000, ost_handle, LUSTRE_OSS_NAME,
1658                                 obd->obd_proc_entry, ost_print_req,
1659                                 oss_min_threads, oss_max_threads, "ll_ost");
1660         if (ost->ost_service == NULL) {
1661                 CERROR("failed to start OST service\n");
1662                 GOTO(out_lprocfs, rc = -ENOMEM);
1663         }
1664
1665         rc = ptlrpc_start_threads(obd, ost->ost_service);
1666         if (rc)
1667                 GOTO(out_service, rc = -EINVAL);
1668
1669         ost->ost_create_service =
1670                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1671                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1672                                 OSC_REPLY_PORTAL,
1673                                 obd_timeout * 1000, ost_handle, "ost_create",
1674                                 obd->obd_proc_entry, ost_print_req,
1675                                 1, 1, "ll_ost_creat");
1676         if (ost->ost_create_service == NULL) {
1677                 CERROR("failed to start OST create service\n");
1678                 GOTO(out_service, rc = -ENOMEM);
1679         }
1680
1681         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1682         if (rc)
1683                 GOTO(out_create, rc = -EINVAL);
1684
1685         ost->ost_io_service =
1686                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1687                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1688                                 OSC_REPLY_PORTAL,
1689                                 obd_timeout * 1000, ost_handle, "ost_io",
1690                                 obd->obd_proc_entry, ost_print_req,
1691                                 oss_min_threads, oss_max_threads, "ll_ost_io");
1692         if (ost->ost_io_service == NULL) {
1693                 CERROR("failed to start OST I/O service\n");
1694                 GOTO(out_create, rc = -ENOMEM);
1695         }
1696
1697         ost->ost_io_service->srv_init = ost_thread_init;
1698         ost->ost_io_service->srv_done = ost_thread_done;
1699         ost->ost_io_service->srv_cpu_affinity = 1;
1700         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1701         if (rc)
1702                 GOTO(out_io, rc = -EINVAL);
1703
1704         ping_evictor_start();
1705
1706         RETURN(0);
1707
1708 out_io:
1709         ptlrpc_unregister_service(ost->ost_io_service);
1710         ost->ost_io_service = NULL;
1711 out_create:
1712         ptlrpc_unregister_service(ost->ost_create_service);
1713         ost->ost_create_service = NULL;
1714 out_service:
1715         ptlrpc_unregister_service(ost->ost_service);
1716         ost->ost_service = NULL;
1717 out_lprocfs:
1718         lprocfs_obd_cleanup(obd);
1719         RETURN(rc);
1720 }
1721
1722 static int ost_cleanup(struct obd_device *obd)
1723 {
1724         struct ost_obd *ost = &obd->u.ost;
1725         int err = 0;
1726         ENTRY;
1727
1728         ping_evictor_stop();
1729
1730         spin_lock_bh(&obd->obd_processing_task_lock);
1731         if (obd->obd_recovering) {
1732                 target_cancel_recovery_timer(obd);
1733                 obd->obd_recovering = 0;
1734         }
1735         spin_unlock_bh(&obd->obd_processing_task_lock);
1736
1737         down(&ost->ost_health_sem);
1738         ptlrpc_unregister_service(ost->ost_service);
1739         ptlrpc_unregister_service(ost->ost_create_service);
1740         ptlrpc_unregister_service(ost->ost_io_service);
1741         ost->ost_service = NULL;
1742         ost->ost_create_service = NULL;
1743         up(&ost->ost_health_sem);
1744
1745         lprocfs_obd_cleanup(obd);
1746
1747         RETURN(err);
1748 }
1749
1750 static int ost_health_check(struct obd_device *obd)
1751 {
1752         struct ost_obd *ost = &obd->u.ost;
1753         int rc = 0;
1754
1755         down(&ost->ost_health_sem);
1756         rc |= ptlrpc_service_health_check(ost->ost_service);
1757         rc |= ptlrpc_service_health_check(ost->ost_create_service);
1758         rc |= ptlrpc_service_health_check(ost->ost_io_service);
1759         up(&ost->ost_health_sem);
1760
1761         /*
1762          * health_check to return 0 on healthy
1763          * and 1 on unhealthy.
1764          */
1765         if( rc != 0)
1766                 rc = 1;
1767
1768         return rc;
1769 }
1770
1771 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
1772 {
1773         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
1774 }
1775
1776 /* use obd ops to offer management infrastructure */
1777 static struct obd_ops ost_obd_ops = {
1778         .o_owner        = THIS_MODULE,
1779         .o_setup        = ost_setup,
1780         .o_cleanup      = ost_cleanup,
1781         .o_health_check = ost_health_check,
1782 };
1783
1784
1785 static int __init ost_init(void)
1786 {
1787         struct lprocfs_static_vars lvars;
1788         int rc;
1789         ENTRY;
1790
1791         lprocfs_init_vars(ost, &lvars);
1792         rc = class_register_type(&ost_obd_ops, lvars.module_vars,
1793                                  LUSTRE_OSS_NAME);
1794         RETURN(rc);
1795 }
1796
1797 static void /*__exit*/ ost_exit(void)
1798 {
1799         class_unregister_type(LUSTRE_OSS_NAME);
1800 }
1801
1802 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1803 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
1804 MODULE_LICENSE("GPL");
1805
1806 module_init(ost_init);
1807 module_exit(ost_exit);