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