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;
69 if (rc == -ENOSPC || rc == -EROFS) {
70 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
71 if (body && rc == -ENOSPC) {
72 oscc->oscc_grow_count = OST_MIN_PRECREATE;
73 oscc->oscc_last_id = body->oa.o_id;
75 spin_unlock(&oscc->oscc_lock);
76 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
77 } else if (rc != 0 && rc != -EIO) {
78 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
79 oscc->oscc_grow_count = OST_MIN_PRECREATE;
80 spin_unlock(&oscc->oscc_lock);
81 DEBUG_REQ(D_ERROR, req,
82 "unknown rc %d from async create: failing oscc", rc);
83 ptlrpc_fail_import(req->rq_import,
84 lustre_msg_get_conn_cnt(req->rq_reqmsg));
88 int diff = body->oa.o_id - oscc->oscc_last_id;
90 if (diff < oscc->oscc_grow_count)
91 oscc->oscc_grow_count =
92 max(diff/3, OST_MIN_PRECREATE);
94 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
95 oscc->oscc_last_id = body->oa.o_id;
98 /* filter always set body->oa.o_id as the last_id
99 * of filter (see filter_handle_precreate for detail)*/
100 if (body && body->oa.o_id > oscc->oscc_last_id)
101 oscc->oscc_last_id = body->oa.o_id;
103 spin_unlock(&oscc->oscc_lock);
107 CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
108 oscc->oscc_last_id, oscc->oscc_next_id);
110 cfs_waitq_signal(&oscc->oscc_waitq);
114 static int oscc_internal_create(struct osc_creator *oscc)
116 struct ptlrpc_request *request;
117 struct ost_body *body;
118 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
121 spin_lock(&oscc->oscc_lock);
122 if (oscc->oscc_grow_count < OST_MAX_PRECREATE &&
123 !(oscc->oscc_flags & (OSCC_FLAG_LOW | OSCC_FLAG_RECOVERING)) &&
124 (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
125 (oscc->oscc_grow_count / 4 + 1)) {
126 oscc->oscc_flags |= OSCC_FLAG_LOW;
127 oscc->oscc_grow_count *= 2;
130 if (oscc->oscc_grow_count > OST_MAX_PRECREATE / 2)
131 oscc->oscc_grow_count = OST_MAX_PRECREATE / 2;
133 if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
134 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
135 spin_unlock(&oscc->oscc_lock);
138 oscc->oscc_flags |= OSCC_FLAG_CREATING;
139 spin_unlock(&oscc->oscc_lock);
141 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
142 LUSTRE_OST_VERSION, OST_CREATE, 2,
144 if (request == NULL) {
145 spin_lock(&oscc->oscc_lock);
146 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
147 spin_unlock(&oscc->oscc_lock);
151 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
152 body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
154 spin_lock(&oscc->oscc_lock);
155 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
156 body->oa.o_valid |= OBD_MD_FLID;
157 spin_unlock(&oscc->oscc_lock);
158 CDEBUG(D_HA, "preallocating through id "LPU64" (last seen "LPU64")\n",
159 body->oa.o_id, oscc->oscc_last_id);
161 ptlrpc_req_set_repsize(request, 2, size);
163 request->rq_async_args.pointer_arg[0] = oscc;
164 request->rq_interpret_reply = osc_interpret_create;
165 ptlrpcd_add_req(request);
170 static int oscc_has_objects(struct osc_creator *oscc, int count)
173 spin_lock(&oscc->oscc_lock);
174 have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
175 spin_unlock(&oscc->oscc_lock);
178 oscc_internal_create(oscc);
183 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
189 have_objs = oscc_has_objects(oscc, count);
191 spin_lock(&oscc->oscc_lock);
192 ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
193 spin_unlock(&oscc->oscc_lock);
195 osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
197 return have_objs || ost_full || osc_invalid;
200 static int oscc_precreate(struct osc_creator *oscc, int wait)
202 struct l_wait_info lwi = { 0 };
206 if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
212 /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
213 l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
215 if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
218 if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
224 int oscc_recovering(struct osc_creator *oscc)
228 spin_lock(&oscc->oscc_lock);
229 recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
230 spin_unlock(&oscc->oscc_lock);
235 int osc_create(struct obd_export *exp, struct obdo *oa,
236 struct lov_stripe_md **ea, struct obd_trans_info *oti)
238 struct lov_stripe_md *lsm;
239 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
240 int try_again = 1, rc = 0;
245 if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0))
246 RETURN(osc_real_create(exp, oa, ea, oti));
248 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
249 oa->o_flags == OBD_FL_RECREATE_OBJS) {
250 RETURN(osc_real_create(exp, oa, ea, oti));
253 /* this is the special case where create removes orphans */
254 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
255 oa->o_flags == OBD_FL_DELORPHAN) {
256 spin_lock(&oscc->oscc_lock);
257 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
258 spin_unlock(&oscc->oscc_lock);
261 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
262 spin_unlock(&oscc->oscc_lock);
265 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
266 spin_unlock(&oscc->oscc_lock);
267 CDEBUG(D_HA, "%s: oscc recovery started\n",
268 oscc->oscc_obd->obd_name);
270 /* delete from next_id on up */
271 oa->o_valid |= OBD_MD_FLID;
272 oa->o_id = oscc->oscc_next_id - 1;
274 CDEBUG(D_HA, "%s: deleting to next_id: "LPU64"\n",
275 oscc->oscc_obd->obd_name, oa->o_id);
277 rc = osc_real_create(exp, oa, ea, NULL);
279 spin_lock(&oscc->oscc_lock);
280 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
281 if (rc == 0 || rc == -ENOSPC) {
283 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
284 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
285 oscc->oscc_last_id = oa->o_id;
286 CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
287 LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
288 oscc->oscc_last_id, rc);
289 cfs_waitq_signal(&oscc->oscc_waitq);
291 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
292 oscc->oscc_obd->obd_name, rc);
294 spin_unlock(&oscc->oscc_lock);
302 rc = obd_alloc_memmd(exp, &lsm);
308 /* If orphans are being recovered, then we must wait until
309 it is finished before we can continue with create. */
310 if (oscc_recovering(oscc)) {
311 struct l_wait_info lwi;
313 CDEBUG(D_HA,"%p: oscc recovery in progress, waiting\n",
316 lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(obd_timeout/4)),
318 rc = l_wait_event(oscc->oscc_waitq,
319 !oscc_recovering(oscc), &lwi);
320 LASSERT(rc == 0 || rc == -ETIMEDOUT);
321 if (rc == -ETIMEDOUT) {
322 CDEBUG(D_HA,"%p: timeout waiting on recovery\n",
326 CDEBUG(D_HA, "%p: oscc recovery over, waking up\n",
330 spin_lock(&oscc->oscc_lock);
331 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
332 spin_unlock(&oscc->oscc_lock);
336 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
337 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
338 oa->o_id = oscc->oscc_next_id;
339 lsm->lsm_object_id = oscc->oscc_next_id;
341 oscc->oscc_next_id++;
344 CDEBUG(D_HA, "%s: set oscc_next_id = "LPU64"\n",
345 exp->exp_obd->obd_name, oscc->oscc_next_id);
346 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
348 spin_unlock(&oscc->oscc_lock);
351 spin_unlock(&oscc->oscc_lock);
352 rc = oscc_precreate(oscc, try_again);
358 CDEBUG(D_HA, "%s: returning objid "LPU64"\n",
359 obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
360 else if (*ea == NULL)
361 obd_free_memmd(exp, &lsm);
365 void oscc_init(struct obd_device *obd)
367 struct osc_creator *oscc;
372 oscc = &obd->u.cli.cl_oscc;
374 memset(oscc, 0, sizeof(*oscc));
375 CFS_INIT_LIST_HEAD(&oscc->oscc_list);
376 cfs_waitq_init(&oscc->oscc_waitq);
377 spin_lock_init(&oscc->oscc_lock);
378 oscc->oscc_obd = obd;
379 oscc->oscc_grow_count = OST_MIN_PRECREATE;
381 oscc->oscc_next_id = 2;
382 oscc->oscc_last_id = 1;
383 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
384 /* XXX the export handle should give the oscc the last object */
385 /* oed->oed_oscc.oscc_last_id = exph->....; */