Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / osc / osc_create.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
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.)
41  *
42  * Author: Peter Braam <braam@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_OSC
49
50 #ifdef __KERNEL__
51 # include <libcfs/libcfs.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 #endif
55
56 #ifdef  __CYGWIN__
57 # include <ctype.h>
58 #endif
59
60 #include <lustre_dlm.h>
61 #include <obd_class.h>
62 #include "osc_internal.h"
63
64 /* XXX need AT adjust ? */
65 #define osc_create_timeout      (obd_timeout / 2)
66
67 struct osc_create_async_args {
68         struct osc_creator      *rq_oscc;
69         struct lov_stripe_md    *rq_lsm;
70         struct obd_info         *rq_oinfo;
71 };
72
73 static int oscc_internal_create(struct osc_creator *oscc);
74 static int handle_async_create(struct ptlrpc_request *req, int rc);
75
76 static int osc_interpret_create(const struct lu_env *env,
77                                 struct ptlrpc_request *req, void *data, int rc)
78 {
79         struct osc_creator *oscc;
80         struct ost_body *body = NULL;
81         struct ptlrpc_request *fake_req, *pos;
82         ENTRY;
83
84         if (req->rq_repmsg) {
85                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
86                                           lustre_swab_ost_body);
87                 if (body == NULL && rc == 0)
88                         rc = -EPROTO;
89         }
90
91         oscc = req->rq_async_args.pointer_arg[0];
92         LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
93
94         spin_lock(&oscc->oscc_lock);
95         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
96         switch (rc) {
97         case 0: {
98                 if (body) {
99                         int diff = body->oa.o_id - oscc->oscc_last_id;
100
101                         /* oscc_internal_create() stores the original value of
102                          * grow_count in rq_async_args.space[0].
103                          * We can't compare against oscc_grow_count directly,
104                          * because it may have been increased while the RPC
105                          * is in flight, so we would always find ourselves
106                          * having created fewer objects and decreasing the
107                          * precreate request size.  b=18577 */
108                         if (diff < (int) req->rq_async_args.space[0]) {
109                                 /* the OST has not managed to create all the
110                                  * objects we asked for */
111                                 oscc->oscc_grow_count = max(diff,
112                                                             OST_MIN_PRECREATE);
113                                 /* don't bump grow_count next time */
114                                 oscc->oscc_flags |= OSCC_FLAG_LOW;
115                         } else {
116                                 /* the OST is able to keep up with the work,
117                                  * we could consider increasing grow_count
118                                  * next time if needed */
119                                 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
120                         }
121                         oscc->oscc_last_id = body->oa.o_id;
122                 }
123                 spin_unlock(&oscc->oscc_lock);
124                 break;
125         }
126         case -ENOSPC:
127         case -EROFS:
128         case -EFBIG: {
129                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
130                 if (body && rc == -ENOSPC) {
131                         oscc->oscc_grow_count = OST_MIN_PRECREATE;
132                         oscc->oscc_last_id = body->oa.o_id;
133                 }
134                 spin_unlock(&oscc->oscc_lock);
135                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
136                 break;
137         }
138         case -EIO: {
139                 /* filter always set body->oa.o_id as the last_id
140                  * of filter (see filter_handle_precreate for detail)*/
141                 if (body && body->oa.o_id > oscc->oscc_last_id)
142                         oscc->oscc_last_id = body->oa.o_id;
143                 spin_unlock(&oscc->oscc_lock);
144                 break;
145         }
146         case -EWOULDBLOCK: {
147                 /* aka EAGAIN we should not delay create if import failed -
148                  * this avoid client stick in create and avoid race with
149                  * delorphan */
150                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
151                 /* oscc->oscc_grow_count = OST_MIN_PRECREATE; */
152                 spin_unlock(&oscc->oscc_lock);
153                 break;
154         }
155         default: {
156                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
157                 oscc->oscc_grow_count = OST_MIN_PRECREATE;
158                 spin_unlock(&oscc->oscc_lock);
159                 DEBUG_REQ(D_ERROR, req,
160                           "Unknown rc %d from async create: failing oscc", rc);
161                 ptlrpc_fail_import(req->rq_import,
162                                    lustre_msg_get_conn_cnt(req->rq_reqmsg));
163         }
164         }
165
166         CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
167                oscc->oscc_last_id, oscc->oscc_next_id);
168
169         spin_lock(&oscc->oscc_lock);
170         list_for_each_entry_safe(fake_req, pos,
171                                  &oscc->oscc_wait_create_list, rq_list) {
172                 if (handle_async_create(fake_req, rc)  == -EAGAIN) {
173                         oscc_internal_create(oscc);
174                         /* sending request should be never fail because
175                          * osc use preallocated requests pool */
176                         GOTO(exit_wakeup, rc);
177                 }
178         }
179         spin_unlock(&oscc->oscc_lock);
180
181 exit_wakeup:
182         cfs_waitq_signal(&oscc->oscc_waitq);
183         RETURN(rc);
184 }
185
186 static int oscc_internal_create(struct osc_creator *oscc)
187 {
188         struct ptlrpc_request *request;
189         struct ost_body *body;
190         __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
191         ENTRY;
192
193         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
194
195         if(oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
196                 spin_unlock(&oscc->oscc_lock);
197                 RETURN(0);
198         }
199
200         /* we need check it before OSCC_FLAG_CREATING - because need
201          * see lower number of precreate objects */
202         if (oscc->oscc_grow_count < oscc->oscc_max_grow_count &&
203             ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
204             (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
205                    (oscc->oscc_grow_count / 4 + 1)) {
206                 oscc->oscc_flags |= OSCC_FLAG_LOW;
207                 oscc->oscc_grow_count *= 2;
208         }
209
210         if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
211                 spin_unlock(&oscc->oscc_lock);
212                 RETURN(0);
213         }
214
215         if (oscc->oscc_grow_count > oscc->oscc_max_grow_count / 2)
216                 oscc->oscc_grow_count = oscc->oscc_max_grow_count / 2;
217
218         oscc->oscc_flags |= OSCC_FLAG_CREATING;
219         spin_unlock(&oscc->oscc_lock);
220
221         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
222                                   LUSTRE_OST_VERSION, OST_CREATE, 2,
223                                   size, NULL);
224         if (request == NULL) {
225                 spin_lock(&oscc->oscc_lock);
226                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
227                 spin_unlock(&oscc->oscc_lock);
228                 RETURN(-ENOMEM);
229         }
230
231         request->rq_request_portal = OST_CREATE_PORTAL;
232         ptlrpc_at_set_req_timeout(request);
233         body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
234
235         spin_lock(&oscc->oscc_lock);
236         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
237         body->oa.o_gr = oscc->oscc_oa.o_gr;
238         LASSERT_MDS_GROUP(body->oa.o_gr);
239         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
240         request->rq_async_args.space[0] = oscc->oscc_grow_count;
241         spin_unlock(&oscc->oscc_lock);
242         CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
243                body->oa.o_id, oscc->oscc_last_id);
244
245         /* we should not resend create request - anyway we will have delorphan
246          * and kill these objects */
247         request->rq_no_delay = request->rq_no_resend = 1;
248         ptlrpc_req_set_repsize(request, 2, size);
249
250         request->rq_async_args.pointer_arg[0] = oscc;
251         request->rq_interpret_reply = osc_interpret_create;
252         ptlrpcd_add_req(request, PSCOPE_OTHER);
253
254         RETURN(0);
255 }
256
257 static int oscc_has_objects_nolock(struct osc_creator *oscc, int count)
258 {
259         return ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
260 }
261
262
263 static int oscc_has_objects(struct osc_creator *oscc, int count)
264 {
265         int have_objs;
266
267         spin_lock(&oscc->oscc_lock);
268         have_objs = oscc_has_objects_nolock(oscc, count);
269         spin_unlock(&oscc->oscc_lock);
270
271         return have_objs;
272 }
273
274 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
275 {
276         int have_objs;
277         int ost_full;
278         int osc_invalid;
279
280         osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
281
282         spin_lock(&oscc->oscc_lock);
283         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
284         have_objs = oscc_has_objects_nolock(oscc, count);
285         osc_invalid |= oscc->oscc_flags & OSCC_FLAG_EXITING;
286
287         if (!ost_full && !osc_invalid)
288                 /* they release lock himself */
289                 oscc_internal_create(oscc);
290         else
291                 spin_unlock(&oscc->oscc_lock);
292
293         return have_objs || ost_full || osc_invalid;
294 }
295
296 static int oscc_precreate(struct osc_creator *oscc)
297 {
298         struct l_wait_info lwi;
299         int rc = 0;
300         ENTRY;
301
302         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
303                 RETURN(0);
304
305         /* we should be not block forever - because client's create rpc can
306          * stick in mds for long time and forbid client reconnect */
307         lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(osc_create_timeout)),
308                           NULL, NULL);
309
310         rc = l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
311
312         if (!oscc_has_objects(oscc, 1) || (oscc->oscc_flags & OSCC_FLAG_NOSPC))
313                 rc = -ENOSPC;
314
315         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
316                 rc = -EIO;
317
318         RETURN(rc);
319 }
320
321 static int oscc_recovering(struct osc_creator *oscc)
322 {
323         int recov;
324
325         spin_lock(&oscc->oscc_lock);
326         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
327         spin_unlock(&oscc->oscc_lock);
328
329         return recov;
330 }
331
332 static int oscc_in_sync(struct osc_creator *oscc)
333 {
334         int sync;
335
336         spin_lock(&oscc->oscc_lock);
337         sync = oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS;
338         spin_unlock(&oscc->oscc_lock);
339
340         return sync;
341 }
342
343 /* decide if the OST has remaining object, return value :
344         0 : the OST has remaining object, and don't need to do precreate.
345         1 : the OST has no remaining object, and will send a RPC for precreate.
346         2 : the OST has no remaining object, and will not get any for
347             a potentially very long time
348      1000 : unusable
349  */
350 int osc_precreate(struct obd_export *exp)
351 {
352         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
353         struct obd_import *imp = exp->exp_imp_reverse;
354         ENTRY;
355
356         LASSERT(oscc != NULL);
357         if (imp != NULL && imp->imp_deactive)
358                 RETURN(1000);
359
360         /* until oscc in recovery - other flags is wrong */
361         if (oscc_recovering(oscc))
362                 RETURN(2);
363
364         if (oscc->oscc_flags & OSCC_FLAG_NOSPC)
365                 RETURN(1000);
366
367         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
368                 RETURN(0);
369
370         spin_lock(&oscc->oscc_lock);
371         if ((oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) ||
372             (oscc->oscc_flags & OSCC_FLAG_CREATING)) {
373                 spin_unlock(&oscc->oscc_lock);
374                 RETURN(1);
375         }
376
377         oscc_internal_create(oscc);
378         RETURN(1);
379 }
380
381 static int handle_async_create(struct ptlrpc_request *req, int rc)
382 {
383         struct osc_create_async_args *args = ptlrpc_req_async_args(req);
384         struct osc_creator    *oscc = args->rq_oscc;
385         struct lov_stripe_md  *lsm  = args->rq_lsm;
386         struct obd_info       *oinfo = args->rq_oinfo;
387         struct obdo           *oa = oinfo->oi_oa;
388
389         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
390
391         if(rc)
392                 GOTO(out_wake, rc);
393
394         if ((oscc->oscc_flags & OSCC_FLAG_EXITING))
395                 GOTO(out_wake, rc = -EIO);
396
397         if (oscc_has_objects_nolock(oscc, 1)) {
398                 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
399                 oa->o_id = oscc->oscc_next_id;
400                 lsm->lsm_object_id = oscc->oscc_next_id;
401                 oscc->oscc_next_id++;
402
403                 CDEBUG(D_RPCTRACE, " set oscc_next_id = "LPU64"\n",
404                        oscc->oscc_next_id);
405                GOTO(out_wake, rc = 0);
406         }
407
408         /* should be try wait until recovery finished */
409         if(oscc->oscc_flags & OSCC_FLAG_RECOVERING)
410                 RETURN(-EAGAIN);
411
412         if (oscc->oscc_flags & OSCC_FLAG_NOSPC)
413                 GOTO(out_wake, rc = -ENOSPC);
414
415         /* we not have objects now - continue wait */
416         RETURN(-EAGAIN);
417
418 out_wake:
419
420         rc = oinfo->oi_cb_up(oinfo, rc);
421         ptlrpc_fakereq_finished(req);
422
423         RETURN(rc);
424 }
425
426 static int async_create_interpret(const struct lu_env *env,
427                                   struct ptlrpc_request *req, void *data, int rc)
428 {
429         struct osc_create_async_args *args = ptlrpc_req_async_args(req);
430         struct osc_creator    *oscc = args->rq_oscc;
431         int ret;
432
433         spin_lock(&oscc->oscc_lock);
434         ret = handle_async_create(req, rc);
435         spin_unlock(&oscc->oscc_lock);
436
437         return ret;
438 }
439
440 int osc_create_async(struct obd_export *exp, struct obd_info *oinfo,
441                      struct lov_stripe_md **ea, struct obd_trans_info *oti)
442 {
443         int rc;
444         struct ptlrpc_request *fake_req;
445         struct osc_create_async_args *args;
446         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
447         struct obdo *oa = oinfo->oi_oa;
448         ENTRY;
449
450         if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0)){
451                 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
452                 rc = oinfo->oi_cb_up(oinfo, rc);
453                 RETURN(rc);
454         }
455
456         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
457             oa->o_flags == OBD_FL_RECREATE_OBJS) {
458                 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
459                 rc = oinfo->oi_cb_up(oinfo, rc);
460                 RETURN(rc);
461         }
462
463         LASSERT((*ea) != NULL);
464
465         fake_req = ptlrpc_prep_fakereq(oscc->oscc_obd->u.cli.cl_import,
466                                        osc_create_timeout,
467                                        async_create_interpret);
468         if (fake_req == NULL) {
469                 rc = oinfo->oi_cb_up(oinfo, -ENOMEM);
470                 RETURN(-ENOMEM);
471         }
472
473         args = ptlrpc_req_async_args(fake_req);
474         CLASSERT(sizeof(*args) <= sizeof(fake_req->rq_async_args));
475
476         args->rq_oscc  = oscc;
477         args->rq_lsm   = *ea;
478         args->rq_oinfo = oinfo;
479
480         spin_lock(&oscc->oscc_lock);
481         /* try fast path */
482         rc = handle_async_create(fake_req, 0);
483         if (rc == -EAGAIN) {
484                 int is_add;
485                 /* we not have objects - try wait */
486                 is_add = ptlrpcd_add_req(fake_req, PSCOPE_OTHER);
487                 if (!is_add)
488                         list_add(&fake_req->rq_list,
489                                  &oscc->oscc_wait_create_list);
490                 else
491                         rc = is_add;
492         }
493         spin_unlock(&oscc->oscc_lock);
494
495         if (rc != -EAGAIN)
496                 /* need free request if was error hit or
497                  * objects already allocated */
498                 ptlrpc_req_finished(fake_req);
499         else
500                 /* EAGAIN mean - request is delayed */
501                 rc = 0;
502
503         RETURN(rc);
504 }
505
506 int osc_create(struct obd_export *exp, struct obdo *oa,
507                struct lov_stripe_md **ea, struct obd_trans_info *oti)
508 {
509         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
510         struct obd_import  *imp  = exp->exp_obd->u.cli.cl_import;
511         struct lov_stripe_md *lsm;
512         int rc = 0;
513         ENTRY;
514
515         LASSERT(oa);
516         LASSERT(ea);
517         LASSERT_MDS_GROUP(oa->o_gr);
518         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
519
520         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
521             oa->o_flags == OBD_FL_RECREATE_OBJS) {
522                 RETURN(osc_real_create(exp, oa, ea, oti));
523         }
524
525         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
526                 RETURN(osc_real_create(exp, oa, ea, oti));
527
528         /* this is the special case where create removes orphans */
529         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
530             oa->o_flags == OBD_FL_DELORPHAN) {
531                 spin_lock(&oscc->oscc_lock);
532                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
533                         spin_unlock(&oscc->oscc_lock);
534                         RETURN(-EBUSY);
535                 }
536                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
537                         spin_unlock(&oscc->oscc_lock);
538                         RETURN(0);
539                 }
540
541                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
542                 /* seting flag LOW we prevent extra grow precreate size
543                  * and enforce use last assigned size */
544                 oscc->oscc_flags |= OSCC_FLAG_LOW;
545                 spin_unlock(&oscc->oscc_lock);
546                 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
547                        oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
548
549                 /* delete from next_id on up */
550                 oa->o_valid |= OBD_MD_FLID;
551                 oa->o_id = oscc->oscc_next_id - 1;
552
553                 rc = osc_real_create(exp, oa, ea, NULL);
554
555                 spin_lock(&oscc->oscc_lock);
556                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
557                 if (rc == 0 || rc == -ENOSPC) {
558                         struct obd_connect_data *ocd;
559
560                         if (rc == -ENOSPC)
561                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
562                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
563
564                         oscc->oscc_last_id = oa->o_id;
565                         ocd = &imp->imp_connect_data;
566                         if (ocd->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN) {
567                                 CDEBUG(D_HA, "%s: Skip orphan set, reset last "
568                                        "objid\n", oscc->oscc_obd->obd_name);
569                                 oscc->oscc_next_id = oa->o_id + 1;
570                         }
571
572                         /* sanity check for next objid. see bug 17025 */
573                         LASSERT(oscc->oscc_next_id == oa->o_id + 1);
574
575                         CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
576                                LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
577                                oscc->oscc_last_id, rc);
578                 } else {
579                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
580                                oscc->oscc_obd->obd_name, rc);
581                 }
582
583                 cfs_waitq_signal(&oscc->oscc_waitq);
584                 spin_unlock(&oscc->oscc_lock);
585
586                 if (rc < 0)
587                         RETURN(rc);
588         }
589
590         lsm = *ea;
591         if (lsm == NULL) {
592                 rc = obd_alloc_memmd(exp, &lsm);
593                 if (rc < 0)
594                         RETURN(rc);
595         }
596
597         while (1) {
598                 if (oscc_in_sync(oscc))
599                         CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
600                                oscc->oscc_obd->obd_name);
601
602                 rc = oscc_precreate(oscc);
603                 if (rc) {
604                         CDEBUG(D_HA,"%s: error create %d\n",
605                                oscc->oscc_obd->obd_name, rc);
606                         break;
607                 }
608
609                 spin_lock(&oscc->oscc_lock);
610                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
611                         spin_unlock(&oscc->oscc_lock);
612                         break;
613                 }
614                 /* wakeup but recovery not finished */
615                 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
616                         rc = -EIO;
617                         spin_unlock(&oscc->oscc_lock);
618                         break;
619                 }
620
621                 if (oscc_has_objects_nolock(oscc, 1)) {
622                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
623                         oa->o_id = oscc->oscc_next_id;
624                         lsm->lsm_object_id = oscc->oscc_next_id;
625                         *ea = lsm;
626                         oscc->oscc_next_id++;
627                         spin_unlock(&oscc->oscc_lock);
628
629                         CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
630                                exp->exp_obd->obd_name, oscc->oscc_next_id);
631                         break;
632                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
633                         rc = -ENOSPC;
634                         spin_unlock(&oscc->oscc_lock);
635                         break;
636                 }
637
638                 spin_unlock(&oscc->oscc_lock);
639         }
640
641         if (rc == 0)
642                 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
643                        obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
644         else if (*ea == NULL)
645                 obd_free_memmd(exp, &lsm);
646         RETURN(rc);
647 }
648
649 void oscc_init(struct obd_device *obd)
650 {
651         struct osc_creator *oscc;
652
653         if (obd == NULL)
654                 return;
655
656         oscc = &obd->u.cli.cl_oscc;
657
658         memset(oscc, 0, sizeof(*oscc));
659
660         cfs_waitq_init(&oscc->oscc_waitq);
661         spin_lock_init(&oscc->oscc_lock);
662         oscc->oscc_obd = obd;
663         oscc->oscc_grow_count = OST_MIN_PRECREATE;
664         oscc->oscc_max_grow_count = OST_MAX_PRECREATE;
665
666         oscc->oscc_next_id = 2;
667         oscc->oscc_last_id = 1;
668         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
669
670         CFS_INIT_LIST_HEAD(&oscc->oscc_wait_create_list);
671
672         /* XXX the export handle should give the oscc the last object */
673         /* oed->oed_oscc.oscc_last_id = exph->....; */
674 }
675
676 void oscc_fini(struct obd_device *obd)
677 {
678         struct osc_creator *oscc = &obd->u.cli.cl_oscc;
679         ENTRY;
680
681
682         spin_lock(&oscc->oscc_lock);
683         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
684         oscc->oscc_flags |= OSCC_FLAG_EXITING;
685         spin_unlock(&oscc->oscc_lock);
686 }