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