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