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