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