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