1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
26 * Storage Target Handling functions
27 * Lustre Object Server Module (OST)
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.)
37 # define EXPORT_SYMTAB
39 #define DEBUG_SUBSYSTEM S_OST
41 #include <linux/module.h>
42 #include <obd_cksum.h>
44 #include <lustre_net.h>
45 #include <lustre_dlm.h>
46 #include <lustre_export.h>
47 #include <lustre_debug.h>
48 #include <linux/init.h>
49 #include <lprocfs_status.h>
50 #include <lustre_commit_confd.h>
51 #include <libcfs/list.h>
52 #include <lustre_quota.h>
53 #include "ost_internal.h"
55 static int oss_num_threads;
56 CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
57 "number of OSS service threads to start");
59 static int ost_num_threads;
60 CFS_MODULE_PARM(ost_num_threads, "i", int, 0444,
61 "number of OST service threads to start (deprecated)");
63 static int oss_num_create_threads;
64 CFS_MODULE_PARM(oss_num_create_threads, "i", int, 0444,
65 "number of OSS create threads to start");
67 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
69 struct oti_req_ack_lock *ack_lock;
76 lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
77 req->rq_transno = oti->oti_transno;
79 /* XXX 4 == entries in oti_ack_locks??? */
80 for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
83 /* XXX not even calling target_send_reply in some cases... */
84 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
88 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
89 struct obd_trans_info *oti)
91 struct ost_body *body, *repbody;
92 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
95 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
96 lustre_swab_ost_body);
100 if (body->oa.o_id == 0)
103 if (lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1)) {
104 struct ldlm_request *dlm;
105 dlm = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*dlm),
106 lustre_swab_ldlm_request);
109 ldlm_request_cancel(req, dlm, 0);
112 rc = lustre_pack_reply(req, 2, size, NULL);
116 if (body->oa.o_valid & OBD_MD_FLCOOKIE)
117 oti->oti_logcookies = obdo_logcookie(&body->oa);
118 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
120 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
121 req->rq_status = obd_destroy(exp, &body->oa, NULL, oti, NULL);
125 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
127 struct ost_body *body, *repbody;
128 struct obd_info oinfo = { { { 0 } } };
129 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
132 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
133 lustre_swab_ost_body);
137 rc = lustre_pack_reply(req, 2, size, NULL);
141 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
143 repbody->oa = body->oa;
145 oinfo.oi_oa = &repbody->oa;
146 if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
147 oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
149 req->rq_status = obd_getattr(exp, &oinfo);
153 static int ost_statfs(struct ptlrpc_request *req)
155 struct obd_statfs *osfs;
156 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*osfs) };
159 rc = lustre_pack_reply(req, 2, size, NULL);
163 osfs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*osfs));
165 req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs,
166 cfs_time_current_64() - HZ, 0);
167 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
168 osfs->os_bfree = osfs->os_bavail = 64;
169 if (req->rq_status != 0)
170 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
175 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
176 struct obd_trans_info *oti)
178 struct ost_body *body, *repbody;
179 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
182 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
183 lustre_swab_ost_body);
187 rc = lustre_pack_reply(req, 2, size, NULL);
191 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
193 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
194 oti->oti_logcookies = obdo_logcookie(&repbody->oa);
195 req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
196 //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
201 * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
202 * lock on the file being truncated.
204 static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
205 struct lustre_handle *lh)
208 struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0} };
209 ldlm_policy_data_t policy;
215 LASSERT(!lustre_handle_is_used(lh));
217 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
218 !(oa->o_flags & OBD_FL_TRUNCLOCK))
221 CDEBUG(D_INODE, "OST-side truncate lock.\n");
224 finis = start + oa->o_blocks;
227 * standard truncate optimization: if file body is completely
228 * destroyed, don't send data back to the server.
230 flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
232 policy.l_extent.start = start & CFS_PAGE_MASK;
235 * If ->o_blocks is EOF it means "lock till the end of the
236 * file". Otherwise, it's size of a hole being punched (in bytes)
238 if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
239 policy.l_extent.end = OBD_OBJECT_EOF;
241 policy.l_extent.end = finis | ~CFS_PAGE_MASK;
243 RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
244 LDLM_EXTENT, &policy, LCK_PW, &flags,
245 ldlm_blocking_ast, ldlm_completion_ast,
246 ldlm_glimpse_ast, NULL, 0, NULL, lh));
250 * Helper function for ost_punch(): release lock acquired by
251 * ost_punch_lock_get(), if any.
253 static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
254 struct lustre_handle *lh)
257 if (lustre_handle_is_used(lh))
258 ldlm_lock_decref(lh, LCK_PW);
262 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
263 struct obd_trans_info *oti)
265 struct obd_info oinfo = { { { 0 } } };
266 struct ost_body *body, *repbody;
267 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
268 struct lustre_handle lh = {0,};
271 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
272 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
274 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
275 lustre_swab_ost_body);
279 oinfo.oi_oa = &body->oa;
280 oinfo.oi_policy.l_extent.start = oinfo.oi_oa->o_size;
281 oinfo.oi_policy.l_extent.end = oinfo.oi_oa->o_blocks;
283 if ((oinfo.oi_oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
284 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
287 rc = lustre_pack_reply(req, 2, size, NULL);
291 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
293 rc = ost_punch_lock_get(exp, oinfo.oi_oa, &lh);
295 if (oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
296 oinfo.oi_oa->o_flags == OBD_FL_TRUNCLOCK)
298 * If OBD_FL_TRUNCLOCK is the only bit set in
299 * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
300 * through filter_setattr() to filter_iocontrol().
302 oinfo.oi_oa->o_valid &= ~OBD_MD_FLFLAGS;
304 if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
305 oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
307 req->rq_status = obd_punch(exp, &oinfo, oti, NULL);
308 ost_punch_lock_put(exp, oinfo.oi_oa, &lh);
310 repbody->oa = *oinfo.oi_oa;
314 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
316 struct ost_body *body, *repbody;
317 struct lustre_capa *capa = NULL;
318 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
321 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
322 lustre_swab_ost_body);
326 if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
327 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 1);
329 rc = lustre_pack_reply(req, 2, size, NULL);
333 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
335 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
336 req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
337 repbody->oa.o_blocks, capa);
341 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
342 struct obd_trans_info *oti)
344 struct ost_body *body, *repbody;
345 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
346 struct obd_info oinfo = { { { 0 } } };
349 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
350 lustre_swab_ost_body);
354 rc = lustre_pack_reply(req, 2, size, NULL);
358 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
360 repbody->oa = body->oa;
362 oinfo.oi_oa = &repbody->oa;
363 if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
364 oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
366 req->rq_status = obd_setattr(exp, &oinfo, oti);
370 static int ost_bulk_timeout(void *data)
373 /* We don't fail the connection here, because having the export
374 * killed makes the (vital) call to commitrw very sad.
379 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
380 struct niobuf_remote *rnb, int nrnb,
381 struct niobuf_remote **pp_rnbp)
383 /* Copy a remote niobuf, splitting it into page-sized chunks
384 * and setting ioo[i].ioo_bufcnt accordingly */
385 struct niobuf_remote *pp_rnb;
393 * array of sufficient size already preallocated by caller
395 LASSERT(pp_rnbp != NULL);
396 LASSERT(*pp_rnbp != NULL);
398 /* first count and check the number of pages required */
399 for (i = 0; i < nioo; i++)
400 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
401 obd_off offset = rnb[rnbidx].offset;
402 obd_off p0 = offset >> CFS_PAGE_SHIFT;
403 obd_off pn = (offset + rnb[rnbidx].len - 1) >>
406 LASSERT(rnbidx < nrnb);
408 npages += (pn + 1 - p0);
410 if (rnb[rnbidx].len == 0) {
411 CERROR("zero len BRW: obj %d objid "LPX64
412 " buf %u\n", i, ioo[i].ioo_id, j);
416 rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
417 CERROR("unordered BRW: obj %d objid "LPX64
418 " buf %u offset "LPX64" <= "LPX64"\n",
419 i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
425 LASSERT(rnbidx == nrnb);
427 if (npages == nrnb) { /* all niobufs are for single pages */
434 /* now do the actual split */
436 for (i = 0; i < nioo; i++) {
439 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
440 obd_off off = rnb[rnbidx].offset;
441 int nob = rnb[rnbidx].len;
443 LASSERT(rnbidx < nrnb);
445 obd_off poff = off & ~CFS_PAGE_MASK;
446 int pnob = (poff + nob > CFS_PAGE_SIZE) ?
447 CFS_PAGE_SIZE - poff : nob;
449 LASSERT(page < npages);
450 pp_rnb[page].len = pnob;
451 pp_rnb[page].offset = off;
452 pp_rnb[page].flags = rnb[rnbidx].flags;
454 CDEBUG(0, " obj %d id "LPX64
455 "page %d(%d) "LPX64" for %d, flg %x\n",
456 i, ioo[i].ioo_id, obj_pages, page,
457 pp_rnb[page].offset, pp_rnb[page].len,
467 ioo[i].ioo_bufcnt = obj_pages;
469 LASSERT(page == npages);
474 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc, int opc,
475 cksum_type_t cksum_type)
480 cksum = init_checksum(cksum_type);
481 for (i = 0; i < desc->bd_iov_count; i++) {
482 struct page *page = desc->bd_iov[i].kiov_page;
483 int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
484 char *ptr = kmap(page) + off;
485 int len = desc->bd_iov[i].kiov_len;
487 /* corrupt the data before we compute the checksum, to
488 * simulate a client->OST data error */
489 if (i == 0 && opc == OST_WRITE &&
490 OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_RECEIVE))
491 memcpy(ptr, "bad3", min(4, len));
492 cksum = compute_checksum(cksum, ptr, len, cksum_type);
493 /* corrupt the data after we compute the checksum, to
494 * simulate an OST->client data error */
495 if (i == 0 && opc == OST_READ &&
496 OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_SEND))
497 memcpy(ptr, "bad4", min(4, len));
505 * populate @nio by @nrpages pages from per-thread page pool
507 static void ost_nio_pages_get(struct ptlrpc_request *req,
508 struct niobuf_local *nio, int nrpages)
511 struct ost_thread_local_cache *tls;
515 LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
516 LASSERT(req != NULL);
517 LASSERT(req->rq_svc_thread != NULL);
520 LASSERT(tls != NULL);
522 memset(nio, 0, nrpages * sizeof *nio);
523 for (i = 0; i < nrpages; ++ i) {
527 LASSERT(page != NULL);
528 POISON_PAGE(page, 0xf1);
530 LL_CDEBUG_PAGE(D_INFO, page, "%d\n", i);
536 * Dual for ost_nio_pages_get(). Poison pages in pool for debugging
538 static void ost_nio_pages_put(struct ptlrpc_request *req,
539 struct niobuf_local *nio, int nrpages)
545 LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
547 for (i = 0; i < nrpages; ++ i)
548 POISON_PAGE(nio[i].page, 0xf2);
552 static int ost_brw_lock_get(int mode, struct obd_export *exp,
553 struct obd_ioobj *obj, struct niobuf_remote *nb,
554 struct lustre_handle *lh)
557 int nrbufs = obj->ioo_bufcnt;
558 struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
560 ldlm_policy_data_t policy;
565 LASSERT(mode == LCK_PR || mode == LCK_PW);
566 LASSERT(!lustre_handle_is_used(lh));
568 if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
571 /* EXPENSIVE ASSERTION */
572 for (i = 1; i < nrbufs; i ++)
573 LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
574 (nb[i].flags & OBD_BRW_SRVLOCK));
576 policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
577 policy.l_extent.end = (nb[nrbufs - 1].offset +
578 nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
580 RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
581 LDLM_EXTENT, &policy, mode, &flags,
582 ldlm_blocking_ast, ldlm_completion_ast,
583 ldlm_glimpse_ast, NULL, 0, NULL, lh));
586 static void ost_brw_lock_put(int mode,
587 struct obd_ioobj *obj, struct niobuf_remote *niob,
588 struct lustre_handle *lh)
591 LASSERT(mode == LCK_PR || mode == LCK_PW);
592 LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
593 lustre_handle_is_used(lh));
594 if (lustre_handle_is_used(lh))
595 ldlm_lock_decref(lh, mode);
599 struct ost_prolong_data {
600 struct obd_export *opd_exp;
601 ldlm_policy_data_t opd_policy;
602 ldlm_mode_t opd_mode;
605 static int ost_prolong_locks_iter(struct ldlm_lock *lock, void *data)
607 struct ost_prolong_data *opd = data;
609 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
611 if (lock->l_req_mode != lock->l_granted_mode) {
612 /* scan granted locks only */
613 return LDLM_ITER_STOP;
616 if (lock->l_export != opd->opd_exp) {
617 /* prolong locks only for given client */
618 return LDLM_ITER_CONTINUE;
621 if (!(lock->l_granted_mode & opd->opd_mode)) {
622 /* we aren't interesting in all type of locks */
623 return LDLM_ITER_CONTINUE;
626 if (lock->l_policy_data.l_extent.end < opd->opd_policy.l_extent.start ||
627 lock->l_policy_data.l_extent.start > opd->opd_policy.l_extent.end) {
628 /* the request doesn't cross the lock, skip it */
629 return LDLM_ITER_CONTINUE;
632 if (!(lock->l_flags & LDLM_FL_AST_SENT)) {
633 /* ignore locks not being cancelled */
634 return LDLM_ITER_CONTINUE;
637 /* OK. this is a possible lock the user holds doing I/O
638 * let's refresh eviction timer for it */
639 ldlm_refresh_waiting_lock(lock);
641 return LDLM_ITER_CONTINUE;
644 static void ost_prolong_locks(struct obd_export *exp, struct obd_ioobj *obj,
645 struct niobuf_remote *nb, ldlm_mode_t mode)
647 struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
649 int nrbufs = obj->ioo_bufcnt;
650 struct ost_prolong_data opd;
656 opd.opd_policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
657 opd.opd_policy.l_extent.end = (nb[nrbufs - 1].offset +
658 nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
660 CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
661 res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
662 opd.opd_policy.l_extent.end);
663 ldlm_resource_iterate(exp->exp_obd->obd_namespace, &res_id,
664 ost_prolong_locks_iter, &opd);
667 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
669 struct ptlrpc_bulk_desc *desc;
670 struct obd_export *exp = req->rq_export;
671 struct niobuf_remote *remote_nb;
672 struct niobuf_remote *pp_rnb = NULL;
673 struct niobuf_local *local_nb;
674 struct obd_ioobj *ioo;
675 struct ost_body *body, *repbody;
676 struct lustre_capa *capa = NULL;
677 struct l_wait_info lwi;
678 struct lustre_handle lockh = { 0 };
679 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
680 int niocount, npages, nob = 0, rc, i;
684 req->rq_bulk_read = 1;
686 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
687 GOTO(out, rc = -EIO);
689 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
691 /* Check if there is eviction in progress, and if so, wait for it to
693 if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
694 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
695 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
696 !atomic_read(&exp->exp_obd->obd_evict_inprogress),
700 GOTO(out, rc = -ENOTCONN);
702 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
703 lustre_swab_ost_body);
705 CERROR("Missing/short ost_body\n");
706 GOTO(out, rc = -EFAULT);
709 ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*ioo),
710 lustre_swab_obd_ioobj);
712 CERROR("Missing/short ioobj\n");
713 GOTO(out, rc = -EFAULT);
716 niocount = ioo->ioo_bufcnt;
717 if (niocount > PTLRPC_MAX_BRW_PAGES) {
718 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
720 GOTO(out, rc = -EFAULT);
723 remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
724 niocount * sizeof(*remote_nb),
725 lustre_swab_niobuf_remote);
726 if (remote_nb == NULL) {
727 CERROR("Missing/short niobuf\n");
728 GOTO(out, rc = -EFAULT);
730 if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
731 for (i = 1; i < niocount; i++)
732 lustre_swab_niobuf_remote (&remote_nb[i]);
735 if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
736 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
738 rc = lustre_pack_reply(req, 2, size, NULL);
743 * Per-thread array of struct niobuf_{local,remote}'s was allocated by
746 local_nb = ost_tls(req)->local;
747 pp_rnb = ost_tls(req)->remote;
749 /* FIXME all niobuf splitting should be done in obdfilter if needed */
750 /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
751 npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
753 GOTO(out, rc = npages);
755 LASSERT(npages <= OST_THREAD_POOL_SIZE);
757 ost_nio_pages_get(req, local_nb, npages);
759 desc = ptlrpc_prep_bulk_exp(req, npages,
760 BULK_PUT_SOURCE, OST_BULK_PORTAL);
762 GOTO(out, rc = -ENOMEM);
764 rc = ost_brw_lock_get(LCK_PR, exp, ioo, pp_rnb, &lockh);
769 * If getting the lock took more time than
770 * client was willing to wait, drop it. b=11330
772 if (cfs_time_current_sec() > req->rq_deadline ||
773 OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
775 CERROR("Dropping timed-out read from %s because locking"
776 "object "LPX64" took %ld seconds (limit was %ld).\n",
777 libcfs_id2str(req->rq_peer), ioo->ioo_id,
778 cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
779 req->rq_deadline - req->rq_arrival_time.tv_sec);
780 GOTO(out_lock, rc = -ETIMEDOUT);
783 rc = obd_preprw(OBD_BRW_READ, exp, &body->oa, 1,
784 ioo, npages, pp_rnb, local_nb, oti, capa);
788 ost_prolong_locks(exp, ioo, pp_rnb, LCK_PW | LCK_PR);
791 for (i = 0; i < npages; i++) {
792 int page_rc = local_nb[i].rc;
794 if (page_rc < 0) { /* error */
799 LASSERTF(page_rc <= pp_rnb[i].len, "page_rc (%d) > "
800 "pp_rnb[%d].len (%d)\n", page_rc, i, pp_rnb[i].len);
802 if (page_rc != 0) { /* some data! */
803 LASSERT (local_nb[i].page != NULL);
804 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
805 pp_rnb[i].offset & ~CFS_PAGE_MASK,
809 if (page_rc != pp_rnb[i].len) { /* short read */
810 /* All subsequent pages should be 0 */
812 LASSERT(local_nb[i].rc == 0);
817 if (body->oa.o_valid & OBD_MD_FLCKSUM) {
818 cksum_type_t cksum_type = OBD_CKSUM_CRC32;
820 if (body->oa.o_valid & OBD_MD_FLFLAGS)
821 cksum_type = cksum_type_unpack(body->oa.o_flags);
822 body->oa.o_flags = cksum_type_pack(cksum_type);
823 body->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
824 body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ, cksum_type);
825 CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
827 body->oa.o_valid = 0;
829 /* We're finishing using body->oa as an input variable */
831 /* Check if client was evicted while we were doing i/o before touching
834 /* Check if there is eviction in progress, and if so, wait for
836 if (unlikely(atomic_read(&exp->exp_obd->
837 obd_evict_inprogress))) {
838 lwi = LWI_INTR(NULL, NULL);
839 rc = l_wait_event(exp->exp_obd->
840 obd_evict_inprogress_waitq,
841 !atomic_read(&exp->exp_obd->
842 obd_evict_inprogress),
848 sptlrpc_svc_wrap_bulk(req, desc);
850 rc = ptlrpc_start_bulk_transfer(desc);
854 time_t start = cfs_time_current_sec();
856 long timeoutl = req->rq_deadline -
857 cfs_time_current_sec();
858 cfs_duration_t timeout = (timeoutl <= 0 || rc) ?
859 CFS_TICK : cfs_time_seconds(timeoutl);
860 lwi = LWI_TIMEOUT_INTERVAL(timeout,
864 rc = l_wait_event(desc->bd_waitq,
865 !ptlrpc_bulk_active(desc) ||
866 exp->exp_failed, &lwi);
867 LASSERT(rc == 0 || rc == -ETIMEDOUT);
868 /* Wait again if we changed deadline */
869 } while ((rc == -ETIMEDOUT) &&
870 (req->rq_deadline > cfs_time_current_sec()));
872 if (rc == -ETIMEDOUT) {
873 DEBUG_REQ(D_ERROR, req,
874 "timeout on bulk PUT after %ld%+lds",
875 req->rq_deadline - start,
876 cfs_time_current_sec() -
878 ptlrpc_abort_bulk(desc);
879 } else if (exp->exp_failed) {
880 DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
882 ptlrpc_abort_bulk(desc);
883 } else if (!desc->bd_success ||
884 desc->bd_nob_transferred != desc->bd_nob) {
885 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
887 "truncated" : "network error on",
888 desc->bd_nob_transferred,
890 /* XXX should this be a different errno? */
894 DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d", rc);
899 /* Must commit after prep above in all cases */
900 rc = obd_commitrw(OBD_BRW_READ, exp, &body->oa, 1,
901 ioo, npages, local_nb, oti, rc);
903 ost_nio_pages_put(req, local_nb, npages);
906 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
908 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
912 ost_brw_lock_put(LCK_PR, ioo, pp_rnb, &lockh);
914 ptlrpc_free_bulk(desc);
918 req->rq_status = nob;
919 target_committed_to_req(req);
921 } else if (!no_reply) {
922 /* Only reply if there was no comms problem with bulk */
923 target_committed_to_req(req);
927 /* reply out callback would free */
928 ptlrpc_req_drop_rs(req);
929 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
930 "client will retry\n",
931 exp->exp_obd->obd_name,
932 exp->exp_client_uuid.uuid,
933 exp->exp_connection->c_remote_uuid.uuid,
934 libcfs_id2str(req->rq_peer));
940 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
942 struct ptlrpc_bulk_desc *desc;
943 struct obd_export *exp = req->rq_export;
944 struct niobuf_remote *remote_nb;
945 struct niobuf_remote *pp_rnb;
946 struct niobuf_local *local_nb;
947 struct obd_ioobj *ioo;
948 struct ost_body *body, *repbody;
949 struct l_wait_info lwi;
950 struct lustre_handle lockh = {0};
951 struct lustre_capa *capa = NULL;
953 int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
954 int objcount, niocount, npages;
956 obd_count client_cksum = 0, server_cksum = 0;
957 cksum_type_t cksum_type = OBD_CKSUM_CRC32;
961 req->rq_bulk_write = 1;
963 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
964 GOTO(out, rc = -EIO);
965 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
966 GOTO(out, rc = -EFAULT);
968 /* pause before transaction has been started */
969 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
971 /* Check if there is eviction in progress, and if so, wait for it to
973 if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
974 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
975 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
976 !atomic_read(&exp->exp_obd->obd_evict_inprogress),
980 GOTO(out, rc = -ENOTCONN);
982 swab = lustre_msg_swabbed(req->rq_reqmsg);
983 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
984 lustre_swab_ost_body);
986 CERROR("Missing/short ost_body\n");
987 GOTO(out, rc = -EFAULT);
990 lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
991 objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
994 CERROR("Missing/short ioobj\n");
995 GOTO(out, rc = -EFAULT);
998 CERROR("too many ioobjs (%d)\n", objcount);
999 GOTO(out, rc = -EFAULT);
1002 ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
1003 objcount * sizeof(*ioo));
1004 LASSERT (ioo != NULL);
1005 for (niocount = i = 0; i < objcount; i++) {
1007 lustre_swab_obd_ioobj(&ioo[i]);
1008 if (ioo[i].ioo_bufcnt == 0) {
1009 CERROR("ioo[%d] has zero bufcnt\n", i);
1010 GOTO(out, rc = -EFAULT);
1012 niocount += ioo[i].ioo_bufcnt;
1015 if (niocount > PTLRPC_MAX_BRW_PAGES) {
1016 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
1018 GOTO(out, rc = -EFAULT);
1021 remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
1022 niocount * sizeof(*remote_nb),
1023 lustre_swab_niobuf_remote);
1024 if (remote_nb == NULL) {
1025 CERROR("Missing/short niobuf\n");
1026 GOTO(out, rc = -EFAULT);
1028 if (swab) { /* swab the remaining niobufs */
1029 for (i = 1; i < niocount; i++)
1030 lustre_swab_niobuf_remote (&remote_nb[i]);
1033 if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
1034 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
1036 size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
1037 rc = lustre_pack_reply(req, 3, size, NULL);
1040 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, obd_fail_val);
1041 rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
1042 niocount * sizeof(*rcs));
1045 * Per-thread array of struct niobuf_{local,remote}'s was allocated by
1046 * ost_thread_init().
1048 local_nb = ost_tls(req)->local;
1049 pp_rnb = ost_tls(req)->remote;
1051 /* FIXME all niobuf splitting should be done in obdfilter if needed */
1052 /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
1053 npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
1055 GOTO(out, rc = npages);
1057 LASSERT(npages <= OST_THREAD_POOL_SIZE);
1059 ost_nio_pages_get(req, local_nb, npages);
1061 desc = ptlrpc_prep_bulk_exp(req, npages,
1062 BULK_GET_SINK, OST_BULK_PORTAL);
1064 GOTO(out, rc = -ENOMEM);
1066 rc = ost_brw_lock_get(LCK_PW, exp, ioo, pp_rnb, &lockh);
1071 * If getting the lock took more time than
1072 * client was willing to wait, drop it. b=11330
1074 if (cfs_time_current_sec() > req->rq_deadline ||
1075 OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1077 CERROR("Dropping timed-out write from %s because locking "
1078 "object "LPX64" took %ld seconds (limit was %ld).\n",
1079 libcfs_id2str(req->rq_peer), ioo->ioo_id,
1080 cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
1081 req->rq_deadline - req->rq_arrival_time.tv_sec);
1082 GOTO(out_lock, rc = -ETIMEDOUT);
1085 ost_prolong_locks(exp, ioo, pp_rnb, LCK_PW);
1087 /* obd_preprw clobbers oa->valid, so save what we need */
1088 if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1089 client_cksum = body->oa.o_cksum;
1090 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1091 cksum_type = cksum_type_unpack(body->oa.o_flags);
1094 /* Because we already sync grant info with client when reconnect,
1095 * grant info will be cleared for resent req, then fed_grant and
1096 * total_grant will not be modified in following preprw_write */
1097 if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1098 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1099 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1102 rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
1103 ioo, npages, pp_rnb, local_nb, oti, capa);
1107 /* NB Having prepped, we must commit... */
1109 for (i = 0; i < npages; i++)
1110 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1111 pp_rnb[i].offset & ~CFS_PAGE_MASK,
1114 /* Check if client was evicted while we were doing i/o before touching
1116 if (desc->bd_export->exp_failed)
1119 rc = ptlrpc_start_bulk_transfer (desc);
1121 time_t start = cfs_time_current_sec();
1123 long timeoutl = req->rq_deadline -
1124 cfs_time_current_sec();
1125 cfs_duration_t timeout = (timeoutl <= 0 || rc) ?
1126 CFS_TICK : cfs_time_seconds(timeoutl);
1127 lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
1128 ost_bulk_timeout, desc);
1129 rc = l_wait_event(desc->bd_waitq,
1130 !ptlrpc_bulk_active(desc) ||
1131 desc->bd_export->exp_failed, &lwi);
1132 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1133 /* Wait again if we changed deadline */
1134 } while ((rc == -ETIMEDOUT) &&
1135 (req->rq_deadline > cfs_time_current_sec()));
1137 if (rc == -ETIMEDOUT) {
1138 DEBUG_REQ(D_ERROR, req,
1139 "timeout on bulk GET after %ld%+lds",
1140 req->rq_deadline - start,
1141 cfs_time_current_sec() -
1143 ptlrpc_abort_bulk(desc);
1144 } else if (desc->bd_export->exp_failed) {
1145 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1147 ptlrpc_abort_bulk(desc);
1148 } else if (!desc->bd_success ||
1149 desc->bd_nob_transferred != desc->bd_nob) {
1150 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1152 "truncated" : "network error on",
1153 desc->bd_nob_transferred, desc->bd_nob);
1154 /* XXX should this be a different errno? */
1158 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1163 sptlrpc_svc_unwrap_bulk(req, desc);
1165 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1167 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1169 if (unlikely(client_cksum != 0 && rc == 0)) {
1170 static int cksum_counter;
1171 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1172 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1173 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1174 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1175 repbody->oa.o_cksum = server_cksum;
1177 if (unlikely(client_cksum != server_cksum)) {
1178 CERROR("client csum %x, server csum %x\n",
1179 client_cksum, server_cksum);
1181 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1182 CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1183 cksum_counter, libcfs_id2str(req->rq_peer),
1188 /* Must commit after prep above in all cases */
1189 rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa,
1190 objcount, ioo, npages, local_nb, oti, rc);
1192 if (unlikely(client_cksum != server_cksum && rc == 0)) {
1193 int new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1198 if (new_cksum == server_cksum)
1199 msg = "changed in transit before arrival at OST";
1200 else if (new_cksum == client_cksum)
1201 msg = "initial checksum before message complete";
1203 msg = "changed in transit AND after initial checksum";
1205 if (req->rq_peer.nid == desc->bd_sender) {
1209 router = libcfs_nid2str(desc->bd_sender);
1212 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1213 "%s%s%s inum "LPU64"/"LPU64" object "
1214 LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1215 exp->exp_obd->obd_name, msg,
1216 libcfs_id2str(req->rq_peer),
1218 body->oa.o_valid & OBD_MD_FLFID ?
1219 body->oa.o_fid : (__u64)0,
1220 body->oa.o_valid & OBD_MD_FLFID ?
1221 body->oa.o_generation :(__u64)0,
1223 body->oa.o_valid & OBD_MD_FLGROUP ?
1224 body->oa.o_gr : (__u64)0,
1226 pp_rnb[npages-1].offset+pp_rnb[npages-1].len
1228 CERROR("client csum %x, original server csum %x, "
1229 "server csum now %x\n",
1230 client_cksum, server_cksum, new_cksum);
1233 ost_nio_pages_put(req, local_nb, npages);
1236 /* set per-requested niobuf return codes */
1237 for (i = j = 0; i < niocount; i++) {
1238 int nob = remote_nb[i].len;
1242 LASSERT(j < npages);
1243 if (local_nb[j].rc < 0)
1244 rcs[i] = local_nb[j].rc;
1245 nob -= pp_rnb[j].len;
1250 LASSERT(j == npages);
1254 ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1256 ptlrpc_free_bulk(desc);
1259 oti_to_request(oti, req);
1260 target_committed_to_req(req);
1261 rc = ptlrpc_reply(req);
1262 } else if (!no_reply) {
1263 /* Only reply if there was no comms problem with bulk */
1264 target_committed_to_req(req);
1265 req->rq_status = rc;
1268 /* reply out callback would free */
1269 ptlrpc_req_drop_rs(req);
1270 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1271 "client will retry\n",
1272 exp->exp_obd->obd_name,
1273 exp->exp_client_uuid.uuid,
1274 exp->exp_connection->c_remote_uuid.uuid,
1275 libcfs_id2str(req->rq_peer));
1280 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1282 char *key, *val = NULL;
1283 int keylen, vallen, rc = 0;
1286 key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1288 DEBUG_REQ(D_HA, req, "no set_info key");
1291 keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1293 rc = lustre_pack_reply(req, 1, NULL, NULL);
1297 vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1299 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1301 if (KEY_IS(KEY_EVICT_BY_NID)) {
1303 obd_export_evict_by_nid(exp->exp_obd, val);
1308 rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1310 lustre_msg_set_status(req->rq_repmsg, 0);
1314 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1317 int keylen, replylen, rc = 0;
1318 struct req_capsule *pill = &req->rq_pill;
1321 req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
1323 /* this common part for get_info rpc */
1324 key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1326 DEBUG_REQ(D_HA, req, "no get_info key");
1329 keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1331 rc = obd_get_info(exp, keylen, key, &replylen, NULL);
1335 req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1336 RCL_SERVER, replylen);
1338 rc = req_capsule_server_pack(pill);
1342 reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1346 /* call again to fill in the reply buffer */
1347 rc = obd_get_info(exp, keylen, key, &replylen, reply);
1349 lustre_msg_set_status(req->rq_repmsg, 0);
1353 static int ost_handle_quotactl(struct ptlrpc_request *req)
1355 struct obd_quotactl *oqctl, *repoqc;
1356 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1359 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1360 lustre_swab_obd_quotactl);
1362 GOTO(out, rc = -EPROTO);
1364 rc = lustre_pack_reply(req, 2, size, NULL);
1368 repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1370 req->rq_status = obd_quotactl(req->rq_export, oqctl);
1376 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1378 struct obd_quotactl *oqctl;
1382 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1386 rc = req_capsule_server_pack(&req->rq_pill);
1388 CERROR("ost: out of memory while packing quotacheck reply\n");
1392 req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1396 static int ost_llog_handle_connect(struct obd_export *exp,
1397 struct ptlrpc_request *req)
1399 struct llogd_conn_body *body;
1403 body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*body));
1404 rc = obd_llog_connect(exp, body);
1408 static int filter_export_check_flavor(struct filter_obd *filter,
1409 struct obd_export *exp,
1410 struct ptlrpc_request *req)
1415 * this should be done in filter_connect()/filter_reconnect(), but
1416 * we can't obtain information like NID, which stored in incoming
1417 * request, thus can't decide what flavor to use. so we do it here.
1419 * This hack should be removed after the OST stack be rewritten, just
1420 * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1422 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_INVALID)
1425 CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
1426 spin_lock(&exp->exp_lock);
1427 exp->exp_sp_peer = req->rq_sp_from;
1429 read_lock(&filter->fo_sptlrpc_lock);
1430 sptlrpc_rule_set_choose(&filter->fo_sptlrpc_rset, exp->exp_sp_peer,
1431 req->rq_peer.nid, &exp->exp_flvr);
1432 read_unlock(&filter->fo_sptlrpc_lock);
1434 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1435 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
1436 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
1437 libcfs_nid2str(req->rq_peer.nid));
1438 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
1442 spin_unlock(&exp->exp_lock);
1447 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1448 struct obd_device *obd, int *process)
1450 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1451 case OST_CONNECT: /* This will never get here, but for completeness. */
1452 case OST_DISCONNECT:
1463 case OBD_LOG_CANCEL:
1465 *process = target_queue_recovery_request(req, obd);
1469 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1475 int ost_msg_check_version(struct lustre_msg *msg)
1479 switch(lustre_msg_get_opc(msg)) {
1481 case OST_DISCONNECT:
1484 case SEC_CTX_INIT_CONT:
1486 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1488 CERROR("bad opc %u version %08x, expecting %08x\n",
1489 lustre_msg_get_opc(msg),
1490 lustre_msg_get_version(msg),
1491 LUSTRE_OBD_VERSION);
1504 case OST_QUOTACHECK:
1506 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1508 CERROR("bad opc %u version %08x, expecting %08x\n",
1509 lustre_msg_get_opc(msg),
1510 lustre_msg_get_version(msg),
1511 LUSTRE_OST_VERSION);
1516 case LDLM_BL_CALLBACK:
1517 case LDLM_CP_CALLBACK:
1518 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1520 CERROR("bad opc %u version %08x, expecting %08x\n",
1521 lustre_msg_get_opc(msg),
1522 lustre_msg_get_version(msg),
1523 LUSTRE_DLM_VERSION);
1525 case LLOG_ORIGIN_CONNECT:
1526 case OBD_LOG_CANCEL:
1527 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1529 CERROR("bad opc %u version %08x, expecting %08x\n",
1530 lustre_msg_get_opc(msg),
1531 lustre_msg_get_version(msg),
1532 LUSTRE_LOG_VERSION);
1535 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1541 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1542 int ost_handle(struct ptlrpc_request *req)
1544 struct obd_trans_info trans_info = { 0, };
1545 struct obd_trans_info *oti = &trans_info;
1546 int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1547 struct obd_device *obd = NULL;
1550 LASSERT(current->journal_info == NULL);
1552 /* primordial rpcs don't affect server recovery */
1553 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1555 case SEC_CTX_INIT_CONT:
1560 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1562 /* XXX identical to MDS */
1563 if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1566 if (req->rq_export == NULL) {
1567 CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1568 lustre_msg_get_opc(req->rq_reqmsg),
1569 libcfs_id2str(req->rq_peer));
1570 req->rq_status = -ENOTCONN;
1571 GOTO(out, rc = -ENOTCONN);
1574 obd = req->rq_export->exp_obd;
1576 /* Check for aborted recovery. */
1577 spin_lock_bh(&obd->obd_processing_task_lock);
1578 recovering = obd->obd_recovering;
1579 spin_unlock_bh(&obd->obd_processing_task_lock);
1581 rc = ost_filter_recovery_request(req, obd,
1583 if (rc || !should_process)
1585 else if (should_process < 0) {
1586 req->rq_status = should_process;
1587 rc = ptlrpc_error(req);
1595 rc = ost_msg_check_version(req->rq_reqmsg);
1599 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1601 CDEBUG(D_INODE, "connect\n");
1602 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
1603 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
1605 rc = target_handle_connect(req);
1606 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
1609 struct obd_export *exp = req->rq_export;
1613 rc = filter_export_check_flavor(&obd->u.filter,
1618 case OST_DISCONNECT:
1619 CDEBUG(D_INODE, "disconnect\n");
1620 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
1621 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
1623 rc = target_handle_disconnect(req);
1626 CDEBUG(D_INODE, "create\n");
1627 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
1629 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1630 GOTO(out, rc = -ENOSPC);
1631 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1632 GOTO(out, rc = -EROFS);
1633 rc = ost_create(req->rq_export, req, oti);
1636 CDEBUG(D_INODE, "destroy\n");
1637 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
1639 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1640 GOTO(out, rc = -EROFS);
1641 rc = ost_destroy(req->rq_export, req, oti);
1644 CDEBUG(D_INODE, "getattr\n");
1645 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
1647 rc = ost_getattr(req->rq_export, req);
1650 CDEBUG(D_INODE, "setattr\n");
1651 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
1653 rc = ost_setattr(req->rq_export, req, oti);
1656 CDEBUG(D_INODE, "write\n");
1657 /* req->rq_request_portal would be nice, if it was set */
1658 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1659 CERROR("%s: deny write request from %s to portal %u\n",
1660 req->rq_export->exp_obd->obd_name,
1661 obd_export_nid2str(req->rq_export),
1662 req->rq_rqbd->rqbd_service->srv_req_portal);
1663 GOTO(out, rc = -EPROTO);
1665 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1667 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1668 GOTO(out, rc = -ENOSPC);
1669 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1670 GOTO(out, rc = -EROFS);
1671 rc = ost_brw_write(req, oti);
1672 LASSERT(current->journal_info == NULL);
1673 /* ost_brw_write sends its own replies */
1676 CDEBUG(D_INODE, "read\n");
1677 /* req->rq_request_portal would be nice, if it was set */
1678 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1679 CERROR("%s: deny read request from %s to portal %u\n",
1680 req->rq_export->exp_obd->obd_name,
1681 obd_export_nid2str(req->rq_export),
1682 req->rq_rqbd->rqbd_service->srv_req_portal);
1683 GOTO(out, rc = -EPROTO);
1685 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1687 rc = ost_brw_read(req, oti);
1688 LASSERT(current->journal_info == NULL);
1689 /* ost_brw_read sends its own replies */
1692 CDEBUG(D_INODE, "punch\n");
1693 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
1695 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1696 GOTO(out, rc = -EROFS);
1697 rc = ost_punch(req->rq_export, req, oti);
1700 CDEBUG(D_INODE, "statfs\n");
1701 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
1703 rc = ost_statfs(req);
1706 CDEBUG(D_INODE, "sync\n");
1707 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
1709 rc = ost_sync(req->rq_export, req);
1712 DEBUG_REQ(D_INODE, req, "set_info");
1713 req_capsule_set(&req->rq_pill, &RQF_OST_SET_INFO);
1714 rc = ost_set_info(req->rq_export, req);
1717 DEBUG_REQ(D_INODE, req, "get_info");
1718 rc = ost_get_info(req->rq_export, req);
1720 case OST_QUOTACHECK:
1721 CDEBUG(D_INODE, "quotacheck\n");
1722 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
1723 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
1725 rc = ost_handle_quotacheck(req);
1728 CDEBUG(D_INODE, "quotactl\n");
1729 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
1730 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
1732 rc = ost_handle_quotactl(req);
1735 DEBUG_REQ(D_INODE, req, "ping");
1736 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
1737 rc = target_handle_ping(req);
1739 /* FIXME - just reply status */
1740 case LLOG_ORIGIN_CONNECT:
1741 DEBUG_REQ(D_INODE, req, "log connect");
1742 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
1743 rc = ost_llog_handle_connect(req->rq_export, req);
1744 req->rq_status = rc;
1745 rc = req_capsule_server_pack(&req->rq_pill);
1748 RETURN(ptlrpc_reply(req));
1749 case OBD_LOG_CANCEL:
1750 CDEBUG(D_INODE, "log cancel\n");
1751 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1752 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1754 rc = llog_origin_handle_cancel(req);
1755 req->rq_status = rc;
1756 rc = req_capsule_server_pack(&req->rq_pill);
1759 RETURN(ptlrpc_reply(req));
1761 CDEBUG(D_INODE, "enqueue\n");
1762 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
1763 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
1765 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1766 ldlm_server_blocking_ast,
1767 ldlm_server_glimpse_ast);
1768 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1771 CDEBUG(D_INODE, "convert\n");
1772 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
1773 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
1775 rc = ldlm_handle_convert(req);
1778 CDEBUG(D_INODE, "cancel\n");
1779 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1780 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1782 rc = ldlm_handle_cancel(req);
1784 case LDLM_BL_CALLBACK:
1785 case LDLM_CP_CALLBACK:
1786 CDEBUG(D_INODE, "callback\n");
1787 CERROR("callbacks should not happen on OST\n");
1790 CERROR("Unexpected opcode %d\n",
1791 lustre_msg_get_opc(req->rq_reqmsg));
1792 req->rq_status = -ENOTSUPP;
1793 rc = ptlrpc_error(req);
1797 LASSERT(current->journal_info == NULL);
1800 /* If we're DISCONNECTing, the export_data is already freed */
1801 if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1802 target_committed_to_req(req);
1806 oti_to_request(oti, req);
1808 target_send_reply(req, rc, fail);
1811 EXPORT_SYMBOL(ost_handle);
1813 * free per-thread pool created by ost_thread_init().
1815 static void ost_thread_done(struct ptlrpc_thread *thread)
1818 struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1823 LASSERT(thread != NULL);
1826 * be prepared to handle partially-initialized pools (because this is
1827 * called from ost_thread_init() for cleanup.
1829 tls = thread->t_data;
1831 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1832 if (tls->page[i] != NULL)
1833 OBD_PAGE_FREE(tls->page[i]);
1836 thread->t_data = NULL;
1842 * initialize per-thread page pool (bug 5137).
1844 static int ost_thread_init(struct ptlrpc_thread *thread)
1848 struct ost_thread_local_cache *tls;
1852 LASSERT(thread != NULL);
1853 LASSERT(thread->t_data == NULL);
1854 LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
1859 thread->t_data = tls;
1863 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1864 OBD_PAGE_ALLOC(tls->page[i], OST_THREAD_POOL_GFP);
1865 if (tls->page[i] == NULL) {
1866 ost_thread_done(thread);
1876 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
1878 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1879 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
1881 struct ost_obd *ost = &obd->u.ost;
1882 struct lprocfs_static_vars lvars;
1883 int oss_min_threads;
1884 int oss_max_threads;
1885 int oss_min_create_threads;
1886 int oss_max_create_threads;
1890 rc = cleanup_group_info();
1894 lprocfs_ost_init_vars(&lvars);
1895 lprocfs_obd_setup(obd, lvars.obd_vars);
1897 sema_init(&ost->ost_health_sem, 1);
1899 if (oss_num_threads) {
1900 /* If oss_num_threads is set, it is the min and the max. */
1901 if (oss_num_threads > OSS_THREADS_MAX)
1902 oss_num_threads = OSS_THREADS_MAX;
1903 if (oss_num_threads < OSS_THREADS_MIN)
1904 oss_num_threads = OSS_THREADS_MIN;
1905 oss_max_threads = oss_min_threads = oss_num_threads;
1907 /* Base min threads on memory and cpus */
1908 oss_min_threads = num_possible_cpus() * num_physpages >>
1909 (27 - CFS_PAGE_SHIFT);
1910 if (oss_min_threads < OSS_THREADS_MIN)
1911 oss_min_threads = OSS_THREADS_MIN;
1912 /* Insure a 4x range for dynamic threads */
1913 if (oss_min_threads > OSS_THREADS_MAX / 4)
1914 oss_min_threads = OSS_THREADS_MAX / 4;
1915 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1919 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1920 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1921 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1922 ost_handle, LUSTRE_OSS_NAME,
1923 obd->obd_proc_entry, target_print_req,
1924 oss_min_threads, oss_max_threads,
1925 "ll_ost", LCT_DT_THREAD);
1926 if (ost->ost_service == NULL) {
1927 CERROR("failed to start service\n");
1928 GOTO(out_lprocfs, rc = -ENOMEM);
1931 rc = ptlrpc_start_threads(obd, ost->ost_service);
1933 GOTO(out_service, rc = -EINVAL);
1935 if (oss_num_create_threads) {
1936 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
1937 oss_num_create_threads = OSS_MAX_CREATE_THREADS;
1938 if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
1939 oss_num_create_threads = OSS_MIN_CREATE_THREADS;
1940 oss_min_create_threads = oss_max_create_threads =
1941 oss_num_create_threads;
1943 oss_min_create_threads = OSS_MIN_CREATE_THREADS;
1944 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
1947 ost->ost_create_service =
1948 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1949 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1950 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1951 ost_handle, "ost_create",
1952 obd->obd_proc_entry, target_print_req,
1953 oss_min_create_threads, oss_max_create_threads,
1954 "ll_ost_creat", LCT_DT_THREAD);
1955 if (ost->ost_create_service == NULL) {
1956 CERROR("failed to start OST create service\n");
1957 GOTO(out_service, rc = -ENOMEM);
1960 rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1962 GOTO(out_create, rc = -EINVAL);
1964 ost->ost_io_service =
1965 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1966 OST_MAXREPSIZE, OST_IO_PORTAL,
1967 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
1968 ost_handle, "ost_io",
1969 obd->obd_proc_entry, target_print_req,
1970 oss_min_threads, oss_max_threads,
1971 "ll_ost_io", LCT_DT_THREAD);
1972 if (ost->ost_io_service == NULL) {
1973 CERROR("failed to start OST I/O service\n");
1974 GOTO(out_create, rc = -ENOMEM);
1977 ost->ost_io_service->srv_init = ost_thread_init;
1978 ost->ost_io_service->srv_done = ost_thread_done;
1979 ost->ost_io_service->srv_cpu_affinity = 1;
1980 rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1982 GOTO(out_io, rc = -EINVAL);
1984 ping_evictor_start();
1989 ptlrpc_unregister_service(ost->ost_io_service);
1990 ost->ost_io_service = NULL;
1992 ptlrpc_unregister_service(ost->ost_create_service);
1993 ost->ost_create_service = NULL;
1995 ptlrpc_unregister_service(ost->ost_service);
1996 ost->ost_service = NULL;
1998 lprocfs_obd_cleanup(obd);
2002 static int ost_cleanup(struct obd_device *obd)
2004 struct ost_obd *ost = &obd->u.ost;
2008 ping_evictor_stop();
2010 spin_lock_bh(&obd->obd_processing_task_lock);
2011 if (obd->obd_recovering) {
2012 target_cancel_recovery_timer(obd);
2013 obd->obd_recovering = 0;
2015 spin_unlock_bh(&obd->obd_processing_task_lock);
2017 down(&ost->ost_health_sem);
2018 ptlrpc_unregister_service(ost->ost_service);
2019 ptlrpc_unregister_service(ost->ost_create_service);
2020 ptlrpc_unregister_service(ost->ost_io_service);
2021 ost->ost_service = NULL;
2022 ost->ost_create_service = NULL;
2023 up(&ost->ost_health_sem);
2025 lprocfs_obd_cleanup(obd);
2030 static int ost_health_check(struct obd_device *obd)
2032 struct ost_obd *ost = &obd->u.ost;
2035 down(&ost->ost_health_sem);
2036 rc |= ptlrpc_service_health_check(ost->ost_service);
2037 rc |= ptlrpc_service_health_check(ost->ost_create_service);
2038 rc |= ptlrpc_service_health_check(ost->ost_io_service);
2039 up(&ost->ost_health_sem);
2042 * health_check to return 0 on healthy
2043 * and 1 on unhealthy.
2051 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2053 return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2056 /* use obd ops to offer management infrastructure */
2057 static struct obd_ops ost_obd_ops = {
2058 .o_owner = THIS_MODULE,
2059 .o_setup = ost_setup,
2060 .o_cleanup = ost_cleanup,
2061 .o_health_check = ost_health_check,
2065 static int __init ost_init(void)
2067 struct lprocfs_static_vars lvars;
2071 lprocfs_ost_init_vars(&lvars);
2072 rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2073 LUSTRE_OSS_NAME, NULL);
2075 if (ost_num_threads != 0 && oss_num_threads == 0) {
2076 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2077 "use oss_num_threads instead or unset both for "
2078 "dynamic thread startup\n");
2079 oss_num_threads = ost_num_threads;
2085 static void /*__exit*/ ost_exit(void)
2087 class_unregister_type(LUSTRE_OSS_NAME);
2090 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2091 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2092 MODULE_LICENSE("GPL");
2094 module_init(ost_init);
2095 module_exit(ost_exit);