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 Braam <braam@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * For testing and management it is treated as an obd_device,
26 * although * it does not export a full OBD method table (the
27 * requests are coming * in over the wire, so object target modules
28 * do not have a full * method table.)
33 # define EXPORT_SYMTAB
35 #define DEBUG_SUBSYSTEM S_OSC
38 # include <libcfs/libcfs.h>
39 #else /* __KERNEL__ */
40 # include <liblustre.h>
47 # include <lustre_dlm.h>
48 #include <obd_class.h>
49 #include "osc_internal.h"
51 static int osc_interpret_create(struct ptlrpc_request *req, void *data, int rc)
53 struct osc_creator *oscc;
54 struct ost_body *body = NULL;
58 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
59 lustre_swab_ost_body);
60 if (body == NULL && rc == 0)
64 oscc = req->rq_async_args.pointer_arg[0];
65 LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
67 spin_lock(&oscc->oscc_lock);
68 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
72 int diff = body->oa.o_id - oscc->oscc_last_id;
74 if (diff < oscc->oscc_grow_count)
75 oscc->oscc_grow_count =
76 max(diff/3, OST_MIN_PRECREATE);
78 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
79 oscc->oscc_last_id = body->oa.o_id;
81 spin_unlock(&oscc->oscc_lock);
85 /* valid race delorphan vs create, or somthing after resend */
86 spin_unlock(&oscc->oscc_lock);
87 DEBUG_REQ(D_INODE, req, "Got EGAIN - resend \n");
92 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
93 if (body && rc == -ENOSPC) {
94 oscc->oscc_grow_count = OST_MIN_PRECREATE;
95 oscc->oscc_last_id = body->oa.o_id;
97 spin_unlock(&oscc->oscc_lock);
98 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
102 /* filter always set body->oa.o_id as the last_id
103 * of filter (see filter_handle_precreate for detail)*/
104 if (body && body->oa.o_id > oscc->oscc_last_id)
105 oscc->oscc_last_id = body->oa.o_id;
106 spin_unlock(&oscc->oscc_lock);
110 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
111 oscc->oscc_grow_count = OST_MIN_PRECREATE;
112 spin_unlock(&oscc->oscc_lock);
113 DEBUG_REQ(D_ERROR, req,
114 "Unknown rc %d from async create: failing oscc", rc);
115 ptlrpc_fail_import(req->rq_import,
116 lustre_msg_get_conn_cnt(req->rq_reqmsg));
120 CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
121 oscc->oscc_last_id, oscc->oscc_next_id);
123 cfs_waitq_signal(&oscc->oscc_waitq);
127 static int oscc_internal_create(struct osc_creator *oscc)
129 struct ptlrpc_request *request;
130 struct ost_body *body;
131 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
134 LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
136 if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
137 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
138 spin_unlock(&oscc->oscc_lock);
142 if (oscc->oscc_grow_count < OST_MAX_PRECREATE &&
143 ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
144 (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
145 (oscc->oscc_grow_count / 4 + 1)) {
146 oscc->oscc_flags |= OSCC_FLAG_LOW;
147 oscc->oscc_grow_count *= 2;
150 if (oscc->oscc_grow_count > OST_MAX_PRECREATE / 2)
151 oscc->oscc_grow_count = OST_MAX_PRECREATE / 2;
153 oscc->oscc_flags |= OSCC_FLAG_CREATING;
154 spin_unlock(&oscc->oscc_lock);
156 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
157 LUSTRE_OST_VERSION, OST_CREATE, 2,
159 if (request == NULL) {
160 spin_lock(&oscc->oscc_lock);
161 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
162 spin_unlock(&oscc->oscc_lock);
166 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
167 body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
169 spin_lock(&oscc->oscc_lock);
170 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
171 body->oa.o_gr = oscc->oscc_oa.o_gr;
172 LASSERT(body->oa.o_gr > 0);
173 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
174 spin_unlock(&oscc->oscc_lock);
175 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
176 body->oa.o_id, oscc->oscc_last_id);
178 ptlrpc_req_set_repsize(request, 2, size);
180 request->rq_async_args.pointer_arg[0] = oscc;
181 request->rq_interpret_reply = osc_interpret_create;
182 ptlrpcd_add_req(request);
187 static int oscc_has_objects(struct osc_creator *oscc, int count)
190 spin_lock(&oscc->oscc_lock);
191 have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
194 oscc_internal_create(oscc);
196 spin_unlock(&oscc->oscc_lock);
202 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
208 have_objs = oscc_has_objects(oscc, count);
210 spin_lock(&oscc->oscc_lock);
211 ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
212 spin_unlock(&oscc->oscc_lock);
214 osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
216 return have_objs || ost_full || osc_invalid;
219 static int oscc_precreate(struct osc_creator *oscc, int wait)
221 struct l_wait_info lwi = { 0 };
225 if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
231 /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
232 l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
234 if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
237 if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
243 int oscc_recovering(struct osc_creator *oscc)
247 spin_lock(&oscc->oscc_lock);
248 recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
249 spin_unlock(&oscc->oscc_lock);
254 /* decide if the OST has remaining object, return value :
255 0 : the OST has remaining object, and don't need to do precreate.
256 1 : the OST has no remaining object, and will send a RPC for precreate.
257 2 : the OST has no remaining object, and will not get any for
258 a potentially very long time
261 int osc_precreate(struct obd_export *exp)
263 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
264 struct obd_import *imp = exp->exp_imp_reverse;
267 LASSERT(oscc != NULL);
268 if (imp != NULL && imp->imp_deactive)
271 if (oscc->oscc_last_id < oscc->oscc_next_id) {
272 spin_lock(&oscc->oscc_lock);
273 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
274 spin_unlock(&oscc->oscc_lock);
277 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
278 spin_unlock(&oscc->oscc_lock);
281 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
282 spin_unlock(&oscc->oscc_lock);
286 if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
287 spin_unlock(&oscc->oscc_lock);
291 oscc_internal_create(oscc);
297 int osc_create(struct obd_export *exp, struct obdo *oa,
298 struct lov_stripe_md **ea, struct obd_trans_info *oti)
300 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
301 struct lov_stripe_md *lsm;
302 int try_again = 1, rc = 0;
307 LASSERT(oa->o_gr > 0);
308 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
310 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
311 oa->o_flags == OBD_FL_RECREATE_OBJS) {
312 RETURN(osc_real_create(exp, oa, ea, oti));
315 if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
316 RETURN(osc_real_create(exp, oa, ea, oti));
318 /* this is the special case where create removes orphans */
319 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
320 oa->o_flags == OBD_FL_DELORPHAN) {
321 spin_lock(&oscc->oscc_lock);
322 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
323 spin_unlock(&oscc->oscc_lock);
326 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
327 spin_unlock(&oscc->oscc_lock);
330 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
331 /* seting flag LOW we prevent extra grow precreate size
332 * and enforce use last assigned size */
333 oscc->oscc_flags |= OSCC_FLAG_LOW;
334 spin_unlock(&oscc->oscc_lock);
335 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
336 oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
338 /* delete from next_id on up */
339 oa->o_valid |= OBD_MD_FLID;
340 oa->o_id = oscc->oscc_next_id - 1;
342 rc = osc_real_create(exp, oa, ea, NULL);
344 spin_lock(&oscc->oscc_lock);
345 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
346 if (rc == 0 || rc == -ENOSPC) {
348 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
349 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
350 oscc->oscc_last_id = oa->o_id;
351 CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
352 LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
353 oscc->oscc_last_id, rc);
354 cfs_waitq_signal(&oscc->oscc_waitq);
356 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
357 oscc->oscc_obd->obd_name, rc);
359 spin_unlock(&oscc->oscc_lock);
367 rc = obd_alloc_memmd(exp, &lsm);
373 /* If orphans are being recovered, then we must wait until
374 it is finished before we can continue with create. */
375 if (oscc_recovering(oscc)) {
376 struct l_wait_info lwi;
378 CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
379 oscc->oscc_obd->obd_name);
381 lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(obd_timeout/4)),
383 rc = l_wait_event(oscc->oscc_waitq,
384 !oscc_recovering(oscc), &lwi);
385 LASSERT(rc == 0 || rc == -ETIMEDOUT);
386 if (rc == -ETIMEDOUT) {
387 CDEBUG(D_HA,"%s: timeout waiting on recovery\n",
388 oscc->oscc_obd->obd_name);
391 CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
392 oscc->oscc_obd->obd_name);
395 spin_lock(&oscc->oscc_lock);
396 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
397 spin_unlock(&oscc->oscc_lock);
401 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
402 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
403 oa->o_id = oscc->oscc_next_id;
404 lsm->lsm_object_id = oscc->oscc_next_id;
406 oscc->oscc_next_id++;
409 CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
410 exp->exp_obd->obd_name, oscc->oscc_next_id);
411 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
413 spin_unlock(&oscc->oscc_lock);
416 spin_unlock(&oscc->oscc_lock);
417 rc = oscc_precreate(oscc, try_again);
423 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
424 obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
425 else if (*ea == NULL)
426 obd_free_memmd(exp, &lsm);
430 void oscc_init(struct obd_device *obd)
432 struct osc_creator *oscc;
437 oscc = &obd->u.cli.cl_oscc;
439 memset(oscc, 0, sizeof(*oscc));
440 CFS_INIT_LIST_HEAD(&oscc->oscc_list);
441 cfs_waitq_init(&oscc->oscc_waitq);
442 spin_lock_init(&oscc->oscc_lock);
443 oscc->oscc_obd = obd;
444 oscc->oscc_grow_count = OST_MIN_PRECREATE;
446 oscc->oscc_next_id = 2;
447 oscc->oscc_last_id = 1;
448 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
449 /* XXX the export handle should give the oscc the last object */
450 /* oed->oed_oscc.oscc_last_id = exph->....; */