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>
62 #include "osc_internal.h"
64 static int osc_interpret_create(const struct lu_env *env,
65 struct ptlrpc_request *req, void *data, int rc)
67 struct osc_creator *oscc;
68 struct ost_body *body = NULL;
72 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
73 lustre_swab_ost_body);
74 if (body == NULL && rc == 0)
78 oscc = req->rq_async_args.pointer_arg[0];
79 LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
81 spin_lock(&oscc->oscc_lock);
82 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
86 int diff = body->oa.o_id - oscc->oscc_last_id;
88 /* oscc_internal_create() stores the original value of
89 * grow_count in rq_async_args.space[0].
90 * We can't compare against oscc_grow_count directly,
91 * because it may have been increased while the RPC
92 * is in flight, so we would always find ourselves
93 * having created fewer objects and decreasing the
94 * precreate request size. b=18577 */
95 if (diff < (int) req->rq_async_args.space[0]) {
96 /* the OST has not managed to create all the
97 * objects we asked for */
98 oscc->oscc_grow_count = max(diff,
100 /* don't bump grow_count next time */
101 oscc->oscc_flags |= OSCC_FLAG_LOW;
103 /* the OST is able to keep up with the work,
104 * we could consider increasing grow_count
105 * next time if needed */
106 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
108 oscc->oscc_last_id = body->oa.o_id;
110 spin_unlock(&oscc->oscc_lock);
114 /* valid race delorphan vs create, or somthing after resend */
115 spin_unlock(&oscc->oscc_lock);
116 DEBUG_REQ(D_INODE, req, "Got EAGAIN - resend \n");
121 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
122 if (body && rc == -ENOSPC) {
123 oscc->oscc_grow_count = OST_MIN_PRECREATE;
124 oscc->oscc_last_id = body->oa.o_id;
126 spin_unlock(&oscc->oscc_lock);
127 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
131 /* filter always set body->oa.o_id as the last_id
132 * of filter (see filter_handle_precreate for detail)*/
133 if (body && body->oa.o_id > oscc->oscc_last_id)
134 oscc->oscc_last_id = body->oa.o_id;
135 spin_unlock(&oscc->oscc_lock);
139 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
140 oscc->oscc_grow_count = OST_MIN_PRECREATE;
141 spin_unlock(&oscc->oscc_lock);
142 DEBUG_REQ(D_ERROR, req,
143 "Unknown rc %d from async create: failing oscc", rc);
144 ptlrpc_fail_import(req->rq_import,
145 lustre_msg_get_conn_cnt(req->rq_reqmsg));
149 CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
150 oscc->oscc_last_id, oscc->oscc_next_id);
152 cfs_waitq_signal(&oscc->oscc_waitq);
156 static int oscc_internal_create(struct osc_creator *oscc)
158 struct ptlrpc_request *request;
159 struct ost_body *body;
160 __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
163 LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
165 if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
166 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
167 spin_unlock(&oscc->oscc_lock);
171 if (oscc->oscc_grow_count < oscc->oscc_max_grow_count &&
172 ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
173 (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
174 (oscc->oscc_grow_count / 4 + 1)) {
175 oscc->oscc_flags |= OSCC_FLAG_LOW;
176 oscc->oscc_grow_count *= 2;
179 if (oscc->oscc_grow_count > oscc->oscc_max_grow_count / 2)
180 oscc->oscc_grow_count = oscc->oscc_max_grow_count / 2;
182 oscc->oscc_flags |= OSCC_FLAG_CREATING;
183 spin_unlock(&oscc->oscc_lock);
185 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
186 LUSTRE_OST_VERSION, OST_CREATE, 2,
188 if (request == NULL) {
189 spin_lock(&oscc->oscc_lock);
190 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
191 spin_unlock(&oscc->oscc_lock);
195 request->rq_request_portal = OST_CREATE_PORTAL;
196 ptlrpc_at_set_req_timeout(request);
197 body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
199 spin_lock(&oscc->oscc_lock);
200 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
201 body->oa.o_gr = oscc->oscc_oa.o_gr;
202 LASSERT_MDS_GROUP(body->oa.o_gr);
203 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
204 request->rq_async_args.space[0] = oscc->oscc_grow_count;
205 spin_unlock(&oscc->oscc_lock);
206 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
207 body->oa.o_id, oscc->oscc_last_id);
209 ptlrpc_req_set_repsize(request, 2, size);
211 request->rq_async_args.pointer_arg[0] = oscc;
212 request->rq_interpret_reply = osc_interpret_create;
213 ptlrpcd_add_req(request, PSCOPE_OTHER);
218 static int oscc_has_objects(struct osc_creator *oscc, int count)
221 spin_lock(&oscc->oscc_lock);
222 have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
225 oscc_internal_create(oscc);
227 spin_unlock(&oscc->oscc_lock);
233 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
239 have_objs = oscc_has_objects(oscc, count);
241 spin_lock(&oscc->oscc_lock);
242 ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
243 spin_unlock(&oscc->oscc_lock);
245 osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
247 return have_objs || ost_full || osc_invalid;
250 static int oscc_precreate(struct osc_creator *oscc, int wait)
252 struct l_wait_info lwi = { 0 };
256 if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
262 /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
263 l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
265 if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
268 if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
274 int oscc_recovering(struct osc_creator *oscc)
278 spin_lock(&oscc->oscc_lock);
279 recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
280 spin_unlock(&oscc->oscc_lock);
285 /* decide if the OST has remaining object, return value :
286 0 : the OST has remaining object, and don't need to do precreate.
287 1 : the OST has no remaining object, and will send a RPC for precreate.
288 2 : the OST has no remaining object, and will not get any for
289 a potentially very long time
292 int osc_precreate(struct obd_export *exp)
294 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
295 struct obd_import *imp = exp->exp_imp_reverse;
298 LASSERT(oscc != NULL);
299 if (imp != NULL && imp->imp_deactive)
302 if (oscc_recovering(oscc))
305 if (oscc->oscc_flags & OSCC_FLAG_NOSPC)
308 if (oscc->oscc_last_id < oscc->oscc_next_id) {
309 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS)
312 spin_lock(&oscc->oscc_lock);
313 if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
314 spin_unlock(&oscc->oscc_lock);
318 oscc_internal_create(oscc);
324 int osc_create(struct obd_export *exp, struct obdo *oa,
325 struct lov_stripe_md **ea, struct obd_trans_info *oti)
327 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
328 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
329 struct lov_stripe_md *lsm;
330 int try_again = 1, rc = 0;
335 LASSERT_MDS_GROUP(oa->o_gr);
336 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
338 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
339 oa->o_flags == OBD_FL_RECREATE_OBJS) {
340 RETURN(osc_real_create(exp, oa, ea, oti));
343 if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
344 RETURN(osc_real_create(exp, oa, ea, oti));
346 /* this is the special case where create removes orphans */
347 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
348 oa->o_flags == OBD_FL_DELORPHAN) {
349 spin_lock(&oscc->oscc_lock);
350 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
351 spin_unlock(&oscc->oscc_lock);
354 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
355 spin_unlock(&oscc->oscc_lock);
358 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
359 /* seting flag LOW we prevent extra grow precreate size
360 * and enforce use last assigned size */
361 oscc->oscc_flags |= OSCC_FLAG_LOW;
362 spin_unlock(&oscc->oscc_lock);
363 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
364 oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
366 /* delete from next_id on up */
367 oa->o_valid |= OBD_MD_FLID;
368 oa->o_id = oscc->oscc_next_id - 1;
370 rc = osc_real_create(exp, oa, ea, NULL);
372 spin_lock(&oscc->oscc_lock);
373 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
374 if (rc == 0 || rc == -ENOSPC) {
375 struct obd_connect_data *ocd;
378 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
379 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
381 oscc->oscc_last_id = oa->o_id;
382 ocd = &imp->imp_connect_data;
383 if (ocd->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN) {
384 CDEBUG(D_HA, "%s: Skip orphan set, reset last "
385 "objid\n", oscc->oscc_obd->obd_name);
386 oscc->oscc_next_id = oa->o_id + 1;
389 /* sanity check for next objid. see bug 17025 */
390 LASSERT(oscc->oscc_next_id == oa->o_id + 1);
392 CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
393 LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
394 oscc->oscc_last_id, rc);
395 cfs_waitq_signal(&oscc->oscc_waitq);
397 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
398 oscc->oscc_obd->obd_name, rc);
400 spin_unlock(&oscc->oscc_lock);
408 rc = obd_alloc_memmd(exp, &lsm);
414 /* If orphans are being recovered, then we must wait until
415 it is finished before we can continue with create. */
416 if (oscc_recovering(oscc)) {
417 struct l_wait_info lwi;
419 CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
420 oscc->oscc_obd->obd_name);
422 lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(
423 obd_timeout / 4)), NULL, NULL);
424 rc = l_wait_event(oscc->oscc_waitq,
425 !oscc_recovering(oscc), &lwi);
426 LASSERT(rc == 0 || rc == -ETIMEDOUT);
427 if (rc == -ETIMEDOUT) {
428 CDEBUG(D_HA,"%s: timeout waiting on recovery\n",
429 oscc->oscc_obd->obd_name);
432 CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
433 oscc->oscc_obd->obd_name);
436 spin_lock(&oscc->oscc_lock);
437 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
438 spin_unlock(&oscc->oscc_lock);
442 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
443 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
444 oa->o_id = oscc->oscc_next_id;
445 lsm->lsm_object_id = oscc->oscc_next_id;
447 oscc->oscc_next_id++;
450 CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
451 exp->exp_obd->obd_name, oscc->oscc_next_id);
452 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
454 spin_unlock(&oscc->oscc_lock);
457 spin_unlock(&oscc->oscc_lock);
458 rc = oscc_precreate(oscc, try_again);
464 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
465 obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
466 else if (*ea == NULL)
467 obd_free_memmd(exp, &lsm);
471 void oscc_init(struct obd_device *obd)
473 struct osc_creator *oscc;
478 oscc = &obd->u.cli.cl_oscc;
480 memset(oscc, 0, sizeof(*oscc));
481 CFS_INIT_LIST_HEAD(&oscc->oscc_list);
482 cfs_waitq_init(&oscc->oscc_waitq);
483 spin_lock_init(&oscc->oscc_lock);
484 oscc->oscc_obd = obd;
485 oscc->oscc_grow_count = OST_MIN_PRECREATE;
486 oscc->oscc_max_grow_count = OST_MAX_PRECREATE;
488 oscc->oscc_next_id = 2;
489 oscc->oscc_last_id = 1;
490 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
491 /* XXX the export handle should give the oscc the last object */
492 /* oed->oed_oscc.oscc_last_id = exph->....; */