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