1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osc/osc_create.c
37 * For testing and management it is treated as an obd_device,
38 * although * it does not export a full OBD method table (the
39 * requests are coming * in over the wire, so object target modules
40 * do not have a full * method table.)
42 * Author: Peter Braam <braam@clusterfs.com>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_OSC
51 # include <libcfs/libcfs.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
60 # include <lustre_dlm.h>
61 #include <obd_class.h>
63 #include "osc_internal.h"
65 /* XXX need AT adjust ? */
66 #define osc_create_timeout (obd_timeout / 2)
68 struct osc_create_async_args {
69 struct osc_creator *rq_oscc;
70 struct lov_stripe_md *rq_lsm;
71 struct obd_info *rq_oinfo;
74 static int oscc_internal_create(struct osc_creator *oscc);
75 static int handle_async_create(struct ptlrpc_request *req, int rc);
77 static int osc_interpret_create(struct ptlrpc_request *req, void *data, int rc)
79 struct osc_creator *oscc;
80 struct ost_body *body = NULL;
81 struct ptlrpc_request *fake_req, *pos;
85 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
86 lustre_swab_ost_body);
87 if (body == NULL && rc == 0)
91 oscc = req->rq_async_args.pointer_arg[0];
92 LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
94 spin_lock(&oscc->oscc_lock);
95 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
99 int diff = body->oa.o_id - oscc->oscc_last_id;
101 /* oscc_internal_create() stores the original value of
102 * grow_count in rq_async_args.space[0].
103 * We can't compare against oscc_grow_count directly,
104 * because it may have been increased while the RPC
105 * is in flight, so we would always find ourselves
106 * having created fewer objects and decreasing the
107 * precreate request size. b=18577 */
108 if (diff < (int) req->rq_async_args.space[0]) {
109 /* the OST has not managed to create all the
110 * objects we asked for */
111 oscc->oscc_grow_count = max(diff,
113 /* don't bump grow_count next time */
114 oscc->oscc_flags |= OSCC_FLAG_LOW;
116 /* the OST is able to keep up with the work,
117 * we could consider increasing grow_count
118 * next time if needed */
119 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
121 oscc->oscc_last_id = body->oa.o_id;
123 spin_unlock(&oscc->oscc_lock);
127 oscc->oscc_flags |= OSCC_FLAG_RDONLY;
131 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
132 if (body && rc == -ENOSPC) {
133 oscc->oscc_last_id = body->oa.o_id;
134 oscc->oscc_grow_count = OST_MIN_PRECREATE;
137 spin_unlock(&oscc->oscc_lock);
138 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
141 /* filter always set body->oa.o_id as the last_id
142 * of filter (see filter_handle_precreate for detail)*/
143 if (body && body->oa.o_id > oscc->oscc_last_id)
144 oscc->oscc_last_id = body->oa.o_id;
145 spin_unlock(&oscc->oscc_lock);
149 /* aka EAGAIN we should not delay create if import failed -
150 * this avoid client stick in create and avoid race with delorphan */
151 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
152 /* oscc->oscc_grow_count = OST_MIN_PRECREATE; */
153 spin_unlock(&oscc->oscc_lock);
157 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
158 oscc->oscc_grow_count = OST_MIN_PRECREATE;
159 spin_unlock(&oscc->oscc_lock);
160 DEBUG_REQ(D_ERROR, req,
161 "unknown rc %d from async create: failing oscc", rc);
162 ptlrpc_fail_import(req->rq_import,
163 lustre_msg_get_conn_cnt(req->rq_reqmsg));
167 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64", next to use "LPU64"\n",
168 oscc->oscc_last_id, oscc->oscc_next_id);
170 spin_lock(&oscc->oscc_lock);
171 list_for_each_entry_safe(fake_req, pos,
172 &oscc->oscc_wait_create_list, rq_list) {
173 if (handle_async_create(fake_req, rc) == -EAGAIN) {
174 oscc_internal_create(oscc);
175 /* sending request should be never fail because
176 * osc use preallocated requests pool */
177 GOTO(exit_wakeup, rc);
180 spin_unlock(&oscc->oscc_lock);
183 cfs_waitq_signal(&oscc->oscc_waitq);
187 static int oscc_internal_create(struct osc_creator *oscc)
189 struct ptlrpc_request *request;
190 struct ost_body *body;
191 __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
194 LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
196 if ((oscc->oscc_flags & OSCC_FLAG_RECOVERING) ||
197 (oscc->oscc_flags & OSCC_FLAG_DEGRADED)) {
198 spin_unlock(&oscc->oscc_lock);
202 /* we need check it before OSCC_FLAG_CREATING - because need
203 * see lower number of precreate objects */
204 if (oscc->oscc_grow_count < oscc->oscc_max_grow_count &&
205 ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
206 (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
207 (oscc->oscc_grow_count / 4 + 1)) {
208 oscc->oscc_flags |= OSCC_FLAG_LOW;
209 oscc->oscc_grow_count *= 2;
212 if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
213 spin_unlock(&oscc->oscc_lock);
217 if (oscc->oscc_grow_count > oscc->oscc_max_grow_count / 2)
218 oscc->oscc_grow_count = oscc->oscc_max_grow_count / 2;
220 oscc->oscc_flags |= OSCC_FLAG_CREATING;
221 spin_unlock(&oscc->oscc_lock);
223 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
224 LUSTRE_OST_VERSION, OST_CREATE, 2,
226 if (request == NULL) {
227 spin_lock(&oscc->oscc_lock);
228 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
229 spin_unlock(&oscc->oscc_lock);
233 request->rq_request_portal = OST_CREATE_PORTAL;
234 ptlrpc_at_set_req_timeout(request);
235 body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
237 spin_lock(&oscc->oscc_lock);
238 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
240 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
241 request->rq_async_args.space[0] = oscc->oscc_grow_count;
242 spin_unlock(&oscc->oscc_lock);
243 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
244 body->oa.o_id, oscc->oscc_last_id);
246 /* we should not resend create request - anyway we will have delorphan
247 * and kill these objects */
248 request->rq_no_delay = request->rq_no_resend = 1;
249 ptlrpc_req_set_repsize(request, 2, size);
251 request->rq_async_args.pointer_arg[0] = oscc;
252 request->rq_interpret_reply = osc_interpret_create;
253 ptlrpcd_add_req(request);
258 static int oscc_has_objects_nolock(struct osc_creator *oscc, int count)
260 return ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
264 static int oscc_has_objects(struct osc_creator *oscc, int count)
268 spin_lock(&oscc->oscc_lock);
269 have_objs = oscc_has_objects_nolock(oscc, count);
270 spin_unlock(&oscc->oscc_lock);
275 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
280 ost_unusable = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
282 spin_lock(&oscc->oscc_lock);
283 ost_unusable |= (OSCC_FLAG_NOSPC | OSCC_FLAG_RDONLY |
284 OSCC_FLAG_EXITING) & oscc->oscc_flags;
285 have_objs = oscc_has_objects_nolock(oscc, count);
287 if (!ost_unusable && !have_objs)
288 /* they release lock himself */
289 have_objs = oscc_internal_create(oscc);
291 spin_unlock(&oscc->oscc_lock);
293 return have_objs || ost_unusable;
296 static int oscc_precreate(struct osc_creator *oscc)
298 struct l_wait_info lwi;
302 if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
305 /* we should be not block forever - because client's create rpc can
306 * stick in mds for long time and forbid client reconnect */
307 lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(osc_create_timeout)),
310 rc = l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
314 static int oscc_in_sync(struct osc_creator *oscc)
318 spin_lock(&oscc->oscc_lock);
319 sync = oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS;
320 spin_unlock(&oscc->oscc_lock);
325 /* decide if the OST has remaining object, return value :
326 0 : the OST has remaining object, and don't need to do precreate.
327 1 : the OST has no remaining object, and will send a RPC for precreate.
328 2 : the OST has no remaining object, and will not get any for
329 a potentially very long time
332 int osc_precreate(struct obd_export *exp)
334 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
335 struct obd_import *imp = exp->exp_imp_reverse;
338 LASSERT(oscc != NULL);
339 if (imp != NULL && imp->imp_deactive)
342 /* Handle critical states first */
343 spin_lock(&oscc->oscc_lock);
344 if (oscc->oscc_flags & OSCC_FLAG_NOSPC ||
345 oscc->oscc_flags & OSCC_FLAG_RDONLY ||
346 oscc->oscc_flags & OSCC_FLAG_EXITING) {
347 spin_unlock(&oscc->oscc_lock);
351 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING ||
352 oscc->oscc_flags & OSCC_FLAG_DEGRADED) {
353 spin_unlock(&oscc->oscc_lock);
357 if (oscc_has_objects_nolock(oscc, oscc->oscc_grow_count / 2)) {
358 spin_unlock(&oscc->oscc_lock);
362 if ((oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) ||
363 (oscc->oscc_flags & OSCC_FLAG_CREATING)) {
364 spin_unlock(&oscc->oscc_lock);
368 if (oscc_internal_create(oscc))
373 static int handle_async_create(struct ptlrpc_request *req, int rc)
375 struct osc_create_async_args *args = ptlrpc_req_async_args(req);
376 struct osc_creator *oscc = args->rq_oscc;
377 struct lov_stripe_md *lsm = args->rq_lsm;
378 struct obd_info *oinfo = args->rq_oinfo;
379 struct obdo *oa = oinfo->oi_oa;
381 LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
386 /* Handle the critical type errors first.
387 * Should we also test cl_import state as well ? */
388 if (oscc->oscc_flags & OSCC_FLAG_EXITING)
389 GOTO(out_wake, rc = -EIO);
391 if (oscc->oscc_flags & OSCC_FLAG_NOSPC)
392 GOTO(out_wake, rc = -ENOSPC);
394 if (oscc->oscc_flags & OSCC_FLAG_RDONLY)
395 GOTO(out_wake, rc = -EROFS);
397 /* should be try wait until recovery finished */
398 if((oscc->oscc_flags & OSCC_FLAG_RECOVERING) ||
399 (oscc->oscc_flags & OSCC_FLAG_DEGRADED))
402 if (oscc_has_objects_nolock(oscc, 1)) {
403 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
404 oa->o_id = oscc->oscc_next_id;
405 lsm->lsm_object_id = oscc->oscc_next_id;
406 oscc->oscc_next_id++;
408 CDEBUG(D_RPCTRACE, " set oscc_next_id = "LPU64"\n",
410 GOTO(out_wake, rc = 0);
413 /* we don't have objects now - continue wait */
418 rc = oinfo->oi_cb_up(oinfo, rc);
419 ptlrpc_fakereq_finished(req);
424 static int async_create_interpret(struct ptlrpc_request *req, void *data, int rc)
426 struct osc_create_async_args *args = ptlrpc_req_async_args(req);
427 struct osc_creator *oscc = args->rq_oscc;
430 spin_lock(&oscc->oscc_lock);
431 ret = handle_async_create(req, rc);
432 spin_unlock(&oscc->oscc_lock);
437 int osc_create_async(struct obd_export *exp, struct obd_info *oinfo,
438 struct lov_stripe_md **ea, struct obd_trans_info *oti)
441 struct ptlrpc_request *fake_req;
442 struct osc_create_async_args *args;
443 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
444 struct obdo *oa = oinfo->oi_oa;
447 if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0)){
448 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
449 rc = oinfo->oi_cb_up(oinfo, rc);
453 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
454 oa->o_flags == OBD_FL_RECREATE_OBJS) {
455 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
456 rc = oinfo->oi_cb_up(oinfo, rc);
460 LASSERT((*ea) != NULL);
462 fake_req = ptlrpc_prep_fakereq(oscc->oscc_obd->u.cli.cl_import,
464 async_create_interpret);
465 if (fake_req == NULL) {
466 rc = oinfo->oi_cb_up(oinfo, -ENOMEM);
470 args = ptlrpc_req_async_args(fake_req);
471 CLASSERT(sizeof(*args) <= sizeof(fake_req->rq_async_args));
473 args->rq_oscc = oscc;
475 args->rq_oinfo = oinfo;
477 spin_lock(&oscc->oscc_lock);
479 rc = handle_async_create(fake_req, 0);
482 /* we not have objects - try wait */
483 is_add = ptlrpcd_add_req(fake_req);
485 list_add(&fake_req->rq_list,
486 &oscc->oscc_wait_create_list);
490 spin_unlock(&oscc->oscc_lock);
493 /* need free request if was error hit or
494 * objects already allocated */
495 ptlrpc_req_finished(fake_req);
497 /* EAGAIN mean - request is delayed */
504 int osc_create(struct obd_export *exp, struct obdo *oa,
505 struct lov_stripe_md **ea, struct obd_trans_info *oti)
507 struct lov_stripe_md *lsm;
508 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
509 int del_orphan = 0, rc = 0;
515 if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0))
516 RETURN(osc_real_create(exp, oa, ea, oti));
518 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
519 oa->o_flags == OBD_FL_RECREATE_OBJS) {
520 RETURN(osc_real_create(exp, oa, ea, oti));
523 /* this is the special case where create removes orphans */
524 if (oa->o_valid & OBD_MD_FLFLAGS &&
525 oa->o_flags == OBD_FL_DELORPHAN) {
526 spin_lock(&oscc->oscc_lock);
527 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
528 spin_unlock(&oscc->oscc_lock);
531 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
532 spin_unlock(&oscc->oscc_lock);
536 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
537 /* seting flag LOW we prevent extra grow precreate size
538 * and enforce use last assigned size */
539 oscc->oscc_flags |= OSCC_FLAG_LOW;
540 spin_unlock(&oscc->oscc_lock);
541 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
542 oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
546 /* delete from next_id on up */
547 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
548 oa->o_id = oscc->oscc_next_id - 1;
551 rc = osc_real_create(exp, oa, ea, NULL);
553 spin_lock(&oscc->oscc_lock);
554 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
555 if (rc == 0 || rc == -ENOSPC) {
556 struct obd_connect_data *ocd;
557 struct obd_import *imp = oscc->oscc_obd->u.cli.cl_import;
560 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
561 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
562 oscc->oscc_last_id = oa->o_id;
564 ocd = &imp->imp_connect_data;
565 if (ocd->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN) {
566 CDEBUG(D_HA, "%s: Skip orphan set, reset last "
567 "objid\n", oscc->oscc_obd->obd_name);
568 oscc->oscc_next_id = oa->o_id + 1;
571 CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
572 LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
573 oscc->oscc_last_id, rc);
575 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
576 oscc->oscc_obd->obd_name, rc);
579 cfs_waitq_signal(&oscc->oscc_waitq);
580 spin_unlock(&oscc->oscc_lock);
588 rc = obd_alloc_memmd(exp, &lsm);
594 if (oscc_in_sync(oscc))
595 CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
596 oscc->oscc_obd->obd_name);
598 rc = oscc_precreate(oscc);
600 CDEBUG(D_HA,"%s: error create %d\n",
601 oscc->oscc_obd->obd_name, rc);
603 spin_lock(&oscc->oscc_lock);
605 /* wakeup but recovery did not finished */
606 if ((oscc->oscc_obd->u.cli.cl_import->imp_invalid) ||
607 (oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
609 spin_unlock(&oscc->oscc_lock);
613 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
615 spin_unlock(&oscc->oscc_lock);
619 if (oscc->oscc_flags & OSCC_FLAG_RDONLY) {
621 spin_unlock(&oscc->oscc_lock);
625 // Should we report -EIO error ?
626 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
627 spin_unlock(&oscc->oscc_lock);
631 if (oscc_has_objects_nolock(oscc, 1)) {
632 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
633 oa->o_id = oscc->oscc_next_id;
634 lsm->lsm_object_id = oscc->oscc_next_id;
636 oscc->oscc_next_id++;
637 spin_unlock(&oscc->oscc_lock);
639 CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
640 exp->exp_obd->obd_name, oscc->oscc_next_id);
644 spin_unlock(&oscc->oscc_lock);
648 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
649 obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
652 obd_free_memmd(exp, &lsm);
653 if (del_orphan != 0 && rc != -EIO)
654 /* Ignore non-IO precreate error for clear orphan */
660 void oscc_init(struct obd_device *obd)
662 struct osc_creator *oscc;
667 oscc = &obd->u.cli.cl_oscc;
669 memset(oscc, 0, sizeof(*oscc));
671 cfs_waitq_init(&oscc->oscc_waitq);
672 spin_lock_init(&oscc->oscc_lock);
673 oscc->oscc_obd = obd;
674 oscc->oscc_grow_count = OST_MIN_PRECREATE;
675 oscc->oscc_max_grow_count = OST_MAX_PRECREATE;
677 oscc->oscc_next_id = 2;
678 oscc->oscc_last_id = 1;
679 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
681 CFS_INIT_LIST_HEAD(&oscc->oscc_wait_create_list);
683 /* XXX the export handle should give the oscc the last object */
684 /* oed->oed_oscc.oscc_last_id = exph->....; */
687 void oscc_fini(struct obd_device *obd)
689 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
693 spin_lock(&oscc->oscc_lock);
694 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
695 oscc->oscc_flags |= OSCC_FLAG_EXITING;
696 spin_unlock(&oscc->oscc_lock);