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