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 Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * For testing and management it is treated as an obd_device,
23 * although * it does not export a full OBD method table (the
24 * requests are coming * in over the wire, so object target modules
25 * do not have a full * method table.)
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_OSC
35 # include <linux/version.h>
36 # include <linux/module.h>
37 # include <linux/mm.h>
38 # include <linux/highmem.h>
39 # include <linux/ctype.h>
40 # include <linux/init.h>
41 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
42 # include <linux/workqueue.h>
43 # include <linux/smp_lock.h>
45 # include <linux/locks.h>
47 #else /* __KERNEL__ */
48 # include <liblustre.h>
55 # include <linux/lustre_dlm.h>
56 #include <linux/obd_class.h>
57 #include "osc_internal.h"
59 static int osc_interpret_create(struct ptlrpc_request *req, void *data,
62 struct osc_creator *oscc;
63 struct ost_body *body = NULL;
67 body = lustre_swab_repbuf(req, 0, sizeof(*body),
68 lustre_swab_ost_body);
69 if (body == NULL && rc == 0)
73 oscc = req->rq_async_args.pointer_arg[0];
74 spin_lock(&oscc->oscc_lock);
76 oscc->oscc_last_id = body->oa.o_id;
78 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
79 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
80 } else if (rc != 0 && rc != -EIO) {
81 DEBUG_REQ(D_ERROR, req,
82 "unknown rc %d from async create: failing oscc",
84 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
85 ptlrpc_fail_import(req->rq_import, req->rq_import_generation);
87 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
88 spin_unlock(&oscc->oscc_lock);
90 CDEBUG(D_HA, "preallocated through id "LPU64" (last used "LPU64")\n",
91 oscc->oscc_last_id, oscc->oscc_next_id);
93 wake_up(&oscc->oscc_waitq);
97 static int oscc_internal_create(struct osc_creator *oscc)
99 struct ptlrpc_request *request;
100 struct ost_body *body;
101 int size = sizeof(*body);
104 spin_lock(&oscc->oscc_lock);
105 if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
106 oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
107 spin_unlock(&oscc->oscc_lock);
110 oscc->oscc_flags |= OSCC_FLAG_CREATING;
111 spin_unlock(&oscc->oscc_lock);
113 request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
114 LUSTRE_OBD_VERSION, OST_CREATE,
116 if (request == NULL) {
117 spin_lock(&oscc->oscc_lock);
118 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
119 spin_unlock(&oscc->oscc_lock);
123 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
124 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
126 spin_lock(&oscc->oscc_lock);
127 body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
128 /* probably we should take frequence of request into account? -bzzz */
129 if (oscc->oscc_grow_count < oscc->oscc_max_grow_count) {
130 oscc->oscc_grow_count *= 2;
131 if (oscc->oscc_grow_count > oscc->oscc_max_grow_count)
132 oscc->oscc_grow_count = oscc->oscc_max_grow_count;
134 body->oa.o_gr = oscc->oscc_gr;
135 LASSERT(body->oa.o_gr > 0);
136 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
137 CDEBUG(D_INFO, "preallocating through id "LPU64" (last used "LPU64")\n",
138 body->oa.o_id, oscc->oscc_next_id);
139 spin_unlock(&oscc->oscc_lock);
141 request->rq_replen = lustre_msg_size(1, &size);
143 request->rq_async_args.pointer_arg[0] = oscc;
144 request->rq_interpret_reply = osc_interpret_create;
145 ptlrpcd_add_req(request);
150 static int oscc_has_objects(struct osc_creator *oscc, int count)
153 spin_lock(&oscc->oscc_lock);
154 have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
155 spin_unlock(&oscc->oscc_lock);
158 oscc_internal_create(oscc);
163 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
169 have_objs = oscc_has_objects(oscc, count);
171 spin_lock(&oscc->oscc_lock);
172 ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
173 spin_unlock(&oscc->oscc_lock);
175 osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
177 return have_objs || ost_full || osc_invalid;
180 static int oscc_precreate(struct osc_creator *oscc, int wait)
182 struct l_wait_info lwi = { 0 };
186 if (oscc_has_objects(oscc, oscc->oscc_kick_barrier))
192 /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
193 l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
195 if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
198 if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
204 int oscc_recovering(struct osc_creator *oscc)
208 spin_lock(&oscc->oscc_lock);
209 recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
210 spin_unlock(&oscc->oscc_lock);
215 int osc_create(struct obd_export *exp, struct obdo *oa,
216 struct lov_stripe_md **ea, struct obd_trans_info *oti)
218 struct lov_stripe_md *lsm;
219 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
220 int try_again = 1, rc = 0;
224 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
225 LASSERT(oa->o_gr > 0);
227 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
228 oa->o_flags == OBD_FL_RECREATE_OBJS) {
229 /* Exceptional case where we are trying to repair missing
230 * objects for various groups. We have already validated that
231 * this is a valid group for the file. Don't set oscc->oscc_gr.
233 RETURN(osc_real_create(exp, oa, ea, oti));
236 LASSERT(oscc->oscc_gr == 0 || oscc->oscc_gr == oa->o_gr);
237 oscc->oscc_gr = oa->o_gr;
239 if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
240 RETURN(osc_real_create(exp, oa, ea, oti));
244 rc = obd_alloc_memmd(exp, &lsm);
249 /* this is the special case where create removes orphans */
250 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
251 oa->o_flags == OBD_FL_DELORPHAN) {
252 CDEBUG(D_HA, "%s; oscc recovery started\n",
253 exp->exp_obd->obd_name);
254 LASSERT(oscc->oscc_flags & OSCC_FLAG_RECOVERING);
256 /* delete from next_id on up */
257 oa->o_valid |= OBD_MD_FLID;
258 oa->o_id = oscc->oscc_next_id - 1;
260 CDEBUG(D_HA, "%s: deleting to next_id: "LPU64"\n",
261 exp->exp_obd->obd_name, oa->o_id);
263 rc = osc_real_create(exp, oa, ea, NULL);
264 if (oscc->oscc_obd == NULL) {
265 CWARN("the obd for oscc %p has been freed\n", oscc);
269 spin_lock(&oscc->oscc_lock);
270 if (rc == 0 || rc == -ENOSPC) {
272 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
273 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
274 oscc->oscc_last_id = oa->o_id;
276 CDEBUG(D_HA, "%s: oscc recovery finished: %d\n",
277 exp->exp_obd->obd_name, rc);
278 wake_up(&oscc->oscc_waitq);
281 CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
282 exp->exp_obd->obd_name, rc);
284 spin_unlock(&oscc->oscc_lock);
290 /* If orphans are being recovered, then we must wait until
291 it is finished before we can continue with create. */
292 if (oscc_recovering(oscc)) {
293 struct l_wait_info lwi;
295 CDEBUG(D_HA,"%s: oscc sync in progress, waiting\n",
296 exp->exp_obd->obd_name);
298 lwi = LWI_TIMEOUT(MAX(obd_timeout * HZ, 1), NULL, NULL);
299 rc = l_wait_event(oscc->oscc_waitq,
300 !oscc_recovering(oscc), &lwi);
301 LASSERT(rc == 0 || rc == -ETIMEDOUT);
302 if (rc == -ETIMEDOUT) {
303 CDEBUG(D_HA, "%s: timed out waiting for sync\n",
304 exp->exp_obd->obd_name);
307 CDEBUG(D_HA, "%s: oscc sync over, waking up\n",
308 exp->exp_obd->obd_name);
311 spin_lock(&oscc->oscc_lock);
312 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
313 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
314 oa->o_id = oscc->oscc_next_id;
315 oa->o_gr = oscc->oscc_gr;
316 lsm->lsm_object_id = oscc->oscc_next_id;
317 lsm->lsm_object_gr = oscc->oscc_gr;
319 oscc->oscc_next_id++;
321 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
323 spin_unlock(&oscc->oscc_lock);
326 spin_unlock(&oscc->oscc_lock);
327 rc = oscc_precreate(oscc, try_again);
333 CDEBUG(D_INFO, "returning objid "LPU64"/"LPU64"\n",
334 lsm->lsm_object_id, lsm->lsm_object_gr);
335 else if (*ea == NULL)
336 obd_free_memmd(exp, &lsm);
340 void oscc_init(struct obd_device *obd)
342 struct osc_creator *oscc;
347 oscc = &obd->u.cli.cl_oscc;
349 memset(oscc, 0, sizeof(*oscc));
350 INIT_LIST_HEAD(&oscc->oscc_list);
351 init_waitqueue_head(&oscc->oscc_waitq);
352 spin_lock_init(&oscc->oscc_lock);
353 oscc->oscc_obd = obd;
354 oscc->oscc_kick_barrier = 100;
355 oscc->oscc_grow_count = 36;
356 oscc->oscc_max_grow_count = 2000;
358 oscc->oscc_next_id = 2;
359 oscc->oscc_last_id = 1;
360 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
361 /* XXX the export handle should give the oscc the last object */
362 /* oed->oed_oscc.oscc_last_id = exph->....; */