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);
86 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
87 if (body && rc == -ENOSPC) {
88 oscc->oscc_grow_count = OST_MIN_PRECREATE;
89 oscc->oscc_last_id = body->oa.o_id;
91 spin_unlock(&oscc->oscc_lock);
92 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
96 /* filter always set body->oa.o_id as the last_id
97 * of filter (see filter_handle_precreate for detail)*/
98 if (body && body->oa.o_id > oscc->oscc_last_id)
99 oscc->oscc_last_id = body->oa.o_id;
100 spin_unlock(&oscc->oscc_lock);
104 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
105 oscc->oscc_grow_count = OST_MIN_PRECREATE;
106 spin_unlock(&oscc->oscc_lock);
107 DEBUG_REQ(D_ERROR, req,
108 "unknown rc %d from async create: failing oscc", rc);
109 ptlrpc_fail_import(req->rq_import,
110 lustre_msg_get_conn_cnt(req->rq_reqmsg));
114 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64", next to use "LPU64"\n",
115 oscc->oscc_last_id, oscc->oscc_next_id);
117 cfs_waitq_signal(&oscc->oscc_waitq);
121 static int oscc_internal_create(struct osc_creator *oscc)
123 struct ptlrpc_request *request;
124 struct ost_body *body;
125 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
128 spin_lock(&oscc->oscc_lock);
129 if (oscc->oscc_grow_count < OST_MAX_PRECREATE &&
130 !(oscc->oscc_flags & (OSCC_FLAG_LOW | OSCC_FLAG_RECOVERING)) &&
131 (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
132 (oscc->oscc_grow_count / 4 + 1)) {
133 oscc->oscc_flags |= OSCC_FLAG_LOW;
134 oscc->oscc_grow_count *= 2;
137 if (oscc->oscc_grow_count > OST_MAX_PRECREATE / 2)
138 oscc->oscc_grow_count = OST_MAX_PRECREATE / 2;
140 if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
141 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
142 spin_unlock(&oscc->oscc_lock);
145 oscc->oscc_flags |= OSCC_FLAG_CREATING;
146 spin_unlock(&oscc->oscc_lock);
148 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
149 LUSTRE_OST_VERSION, OST_CREATE, 2,
151 if (request == NULL) {
152 spin_lock(&oscc->oscc_lock);
153 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
154 spin_unlock(&oscc->oscc_lock);
158 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
159 body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
161 spin_lock(&oscc->oscc_lock);
162 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
163 body->oa.o_valid |= OBD_MD_FLID;
164 spin_unlock(&oscc->oscc_lock);
165 CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
166 body->oa.o_id, oscc->oscc_last_id);
168 ptlrpc_req_set_repsize(request, 2, size);
170 request->rq_async_args.pointer_arg[0] = oscc;
171 request->rq_interpret_reply = osc_interpret_create;
172 ptlrpcd_add_req(request);
177 static int oscc_has_objects(struct osc_creator *oscc, int count)
180 spin_lock(&oscc->oscc_lock);
181 have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
182 spin_unlock(&oscc->oscc_lock);
185 oscc_internal_create(oscc);
190 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
196 have_objs = oscc_has_objects(oscc, count);
198 spin_lock(&oscc->oscc_lock);
199 ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
200 spin_unlock(&oscc->oscc_lock);
202 osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
204 return have_objs || ost_full || osc_invalid;
207 static int oscc_precreate(struct osc_creator *oscc, int wait)
209 struct l_wait_info lwi = { 0 };
213 if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
219 /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
220 l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
222 if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
225 if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
231 int oscc_recovering(struct osc_creator *oscc)
235 spin_lock(&oscc->oscc_lock);
236 recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
237 spin_unlock(&oscc->oscc_lock);
242 /* decide if the OST has remaining object, return value :
243 0 : the OST has remaining object, and don't need to do precreate.
244 1 : the OST has no remaining object, and will send a RPC for precreate.
245 2 : the OST has no remaining object, and will not get any for
246 a potentially very long time
248 int osc_precreate(struct obd_export *exp, int need_create)
250 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
251 struct obd_import *imp = exp->exp_imp_reverse;
254 LASSERT(oscc != NULL);
255 if (imp != NULL && imp->imp_deactive)
258 if (oscc->oscc_last_id < oscc->oscc_next_id) {
259 spin_lock(&oscc->oscc_lock);
260 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
261 spin_unlock(&oscc->oscc_lock);
264 if (oscc->oscc_flags & OSCC_FLAG_NOSPC ||
265 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
266 spin_unlock(&oscc->oscc_lock);
269 spin_unlock(&oscc->oscc_lock);
271 if (oscc->oscc_flags & OSCC_FLAG_CREATING)
277 oscc_internal_create(oscc);
283 int osc_create(struct obd_export *exp, struct obdo *oa,
284 struct lov_stripe_md **ea, struct obd_trans_info *oti)
286 struct lov_stripe_md *lsm;
287 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
288 int try_again = 1, rc = 0;
293 if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0))
294 RETURN(osc_real_create(exp, oa, ea, oti));
296 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
297 oa->o_flags == OBD_FL_RECREATE_OBJS) {
298 RETURN(osc_real_create(exp, oa, ea, oti));
301 /* this is the special case where create removes orphans */
302 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
303 oa->o_flags == OBD_FL_DELORPHAN) {
304 spin_lock(&oscc->oscc_lock);
305 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
306 spin_unlock(&oscc->oscc_lock);
309 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
310 spin_unlock(&oscc->oscc_lock);
313 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
314 spin_unlock(&oscc->oscc_lock);
315 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
316 oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
318 /* delete from next_id on up */
319 oa->o_valid |= OBD_MD_FLID;
320 oa->o_id = oscc->oscc_next_id - 1;
322 rc = osc_real_create(exp, oa, ea, NULL);
324 spin_lock(&oscc->oscc_lock);
325 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
326 if (rc == 0 || rc == -ENOSPC) {
328 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
329 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
330 oscc->oscc_last_id = oa->o_id;
331 CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
332 LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
333 oscc->oscc_last_id, rc);
334 cfs_waitq_signal(&oscc->oscc_waitq);
336 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
337 oscc->oscc_obd->obd_name, rc);
339 spin_unlock(&oscc->oscc_lock);
347 rc = obd_alloc_memmd(exp, &lsm);
353 /* If orphans are being recovered, then we must wait until
354 it is finished before we can continue with create. */
355 if (oscc_recovering(oscc)) {
356 struct l_wait_info lwi;
358 CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
359 oscc->oscc_obd->obd_name);
361 lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(obd_timeout/4)),
363 rc = l_wait_event(oscc->oscc_waitq,
364 !oscc_recovering(oscc), &lwi);
365 LASSERT(rc == 0 || rc == -ETIMEDOUT);
366 if (rc == -ETIMEDOUT) {
367 CDEBUG(D_HA,"%s: timeout waiting on recovery\n",
368 oscc->oscc_obd->obd_name);
371 CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
372 oscc->oscc_obd->obd_name);
375 spin_lock(&oscc->oscc_lock);
376 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
377 spin_unlock(&oscc->oscc_lock);
381 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
382 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
383 oa->o_id = oscc->oscc_next_id;
384 lsm->lsm_object_id = oscc->oscc_next_id;
386 oscc->oscc_next_id++;
389 CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
390 exp->exp_obd->obd_name, oscc->oscc_next_id);
391 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
393 spin_unlock(&oscc->oscc_lock);
396 spin_unlock(&oscc->oscc_lock);
397 rc = oscc_precreate(oscc, try_again);
403 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
404 obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
405 else if (*ea == NULL)
406 obd_free_memmd(exp, &lsm);
410 void oscc_init(struct obd_device *obd)
412 struct osc_creator *oscc;
417 oscc = &obd->u.cli.cl_oscc;
419 memset(oscc, 0, sizeof(*oscc));
420 CFS_INIT_LIST_HEAD(&oscc->oscc_list);
421 cfs_waitq_init(&oscc->oscc_waitq);
422 spin_lock_init(&oscc->oscc_lock);
423 oscc->oscc_obd = obd;
424 oscc->oscc_grow_count = OST_MIN_PRECREATE;
426 oscc->oscc_next_id = 2;
427 oscc->oscc_last_id = 1;
428 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
429 /* XXX the export handle should give the oscc the last object */
430 /* oed->oed_oscc.oscc_last_id = exph->....; */