Whamcloud - gitweb
b=18539
[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  2008 Sun Microsystems, Inc. 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 = 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 -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                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
153                 /* oscc->oscc_grow_count = OST_MIN_PRECREATE; */
154                 spin_unlock(&oscc->oscc_lock);
155                 break;
156         }
157         default: {
158                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
159                 oscc->oscc_grow_count = OST_MIN_PRECREATE;
160                 spin_unlock(&oscc->oscc_lock);
161                 DEBUG_REQ(D_ERROR, req,
162                           "Unknown rc %d from async create: failing oscc", rc);
163                 ptlrpc_fail_import(req->rq_import,
164                                    lustre_msg_get_conn_cnt(req->rq_reqmsg));
165         }
166         }
167
168         CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
169                oscc->oscc_last_id, oscc->oscc_next_id);
170
171         spin_lock(&oscc->oscc_lock);
172         list_for_each_entry_safe(fake_req, pos,
173                                  &oscc->oscc_wait_create_list, rq_list) {
174                 if (handle_async_create(fake_req, rc)  == -EAGAIN) {
175                         oscc_internal_create(oscc);
176                         /* sending request should be never fail because
177                          * osc use preallocated requests pool */
178                         GOTO(exit_wakeup, rc);
179                 }
180         }
181         spin_unlock(&oscc->oscc_lock);
182
183 exit_wakeup:
184         cfs_waitq_signal(&oscc->oscc_waitq);
185         RETURN(rc);
186 }
187
188 static int oscc_internal_create(struct osc_creator *oscc)
189 {
190         struct ptlrpc_request *request;
191         struct ost_body *body;
192         __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
193         ENTRY;
194
195         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
196
197         if(oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
198                 spin_unlock(&oscc->oscc_lock);
199                 RETURN(0);
200         }
201
202         /* we need check it before OSCC_FLAG_CREATING - because need
203          * see lower number of precreate objects */
204         if (oscc->oscc_grow_count < oscc->oscc_max_grow_count &&
205             ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
206             (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
207                    (oscc->oscc_grow_count / 4 + 1)) {
208                 oscc->oscc_flags |= OSCC_FLAG_LOW;
209                 oscc->oscc_grow_count *= 2;
210         }
211
212         if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
213                 spin_unlock(&oscc->oscc_lock);
214                 RETURN(0);
215         }
216
217         if (oscc->oscc_grow_count > oscc->oscc_max_grow_count / 2)
218                 oscc->oscc_grow_count = oscc->oscc_max_grow_count / 2;
219
220         oscc->oscc_flags |= OSCC_FLAG_CREATING;
221         spin_unlock(&oscc->oscc_lock);
222
223         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
224                                   LUSTRE_OST_VERSION, OST_CREATE, 2,
225                                   size, NULL);
226         if (request == NULL) {
227                 spin_lock(&oscc->oscc_lock);
228                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
229                 spin_unlock(&oscc->oscc_lock);
230                 RETURN(-ENOMEM);
231         }
232
233         request->rq_request_portal = OST_CREATE_PORTAL;
234         ptlrpc_at_set_req_timeout(request);
235         body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
236
237         spin_lock(&oscc->oscc_lock);
238         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
239         body->oa.o_gr = oscc->oscc_oa.o_gr;
240         LASSERT_MDS_GROUP(body->oa.o_gr);
241         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
242         request->rq_async_args.space[0] = oscc->oscc_grow_count;
243         spin_unlock(&oscc->oscc_lock);
244         CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
245                body->oa.o_id, oscc->oscc_last_id);
246
247         /* we should not resend create request - anyway we will have delorphan
248          * and kill these objects */
249         request->rq_no_delay = request->rq_no_resend = 1;
250         ptlrpc_req_set_repsize(request, 2, size);
251
252         request->rq_async_args.pointer_arg[0] = oscc;
253         request->rq_interpret_reply = osc_interpret_create;
254         ptlrpcd_add_req(request, PSCOPE_OTHER);
255
256         RETURN(0);
257 }
258
259 static int oscc_has_objects_nolock(struct osc_creator *oscc, int count)
260 {
261         return ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
262 }
263
264
265 static int oscc_has_objects(struct osc_creator *oscc, int count)
266 {
267         int have_objs;
268
269         spin_lock(&oscc->oscc_lock);
270         have_objs = oscc_has_objects_nolock(oscc, count);
271         spin_unlock(&oscc->oscc_lock);
272
273         return have_objs;
274 }
275
276 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
277 {
278         int have_objs;
279         int ost_unusable;
280
281         ost_unusable = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
282
283         spin_lock(&oscc->oscc_lock);
284         ost_unusable |= (OSCC_FLAG_NOSPC | OSCC_FLAG_RDONLY |
285                          OSCC_FLAG_EXITING) & oscc->oscc_flags;
286         have_objs = oscc_has_objects_nolock(oscc, count);
287
288         if (!ost_unusable)
289                 /* they release lock himself */
290                 oscc_internal_create(oscc);
291         else
292                 spin_unlock(&oscc->oscc_lock);
293
294         return have_objs || ost_unusable;
295 }
296
297 static int oscc_precreate(struct osc_creator *oscc)
298 {
299         struct l_wait_info lwi;
300         int rc = 0;
301         ENTRY;
302
303         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
304                 RETURN(0);
305
306         /* we should be not block forever - because client's create rpc can
307          * stick in mds for long time and forbid client reconnect */
308         lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(osc_create_timeout)),
309                           NULL, NULL);
310
311         rc = l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
312
313         if (!oscc_has_objects(oscc, 1) || (oscc->oscc_flags & OSCC_FLAG_NOSPC))
314                 rc = -ENOSPC;
315
316         if (oscc->oscc_flags & OSCC_FLAG_RDONLY)
317                 rc = -EROFS;
318
319         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
320                 rc = -EIO;
321
322         RETURN(rc);
323 }
324
325 static int oscc_in_sync(struct osc_creator *oscc)
326 {
327         int sync;
328
329         spin_lock(&oscc->oscc_lock);
330         sync = oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS;
331         spin_unlock(&oscc->oscc_lock);
332
333         return sync;
334 }
335
336 /* decide if the OST has remaining object, return value :
337         0 : the OST has remaining object, and don't need to do precreate.
338         1 : the OST has no remaining object, and will send a RPC for precreate.
339         2 : the OST has no remaining object, and will not get any for
340             a potentially very long time
341      1000 : unusable
342  */
343 int osc_precreate(struct obd_export *exp)
344 {
345         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
346         struct obd_import *imp = exp->exp_imp_reverse;
347         ENTRY;
348
349         LASSERT(oscc != NULL);
350         if (imp != NULL && imp->imp_deactive)
351                 RETURN(1000);
352
353         /* until oscc in recovery - other flags is wrong */
354         spin_lock(&oscc->oscc_lock);
355         if (oscc->oscc_flags & OSCC_FLAG_NOSPC ||
356             oscc->oscc_flags & OSCC_FLAG_RDONLY) {
357                 spin_unlock(&oscc->oscc_lock);
358                 RETURN(1000);
359         }
360
361         if (oscc->oscc_flags & OSCC_FLAG_RECOVERING ||
362             oscc->oscc_flags & OSCC_FLAG_DEGRADED) {
363                 spin_unlock(&oscc->oscc_lock);
364                 RETURN(2);
365         }
366
367         if (oscc_has_objects_nolock(oscc, oscc->oscc_grow_count / 2)) {
368                 spin_unlock(&oscc->oscc_lock);
369                 RETURN(0);
370         }
371
372         if ((oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) ||
373             (oscc->oscc_flags & OSCC_FLAG_CREATING)) {
374                 spin_unlock(&oscc->oscc_lock);
375                 RETURN(1);
376         }
377
378         oscc_internal_create(oscc);
379         RETURN(1);
380 }
381
382 static int handle_async_create(struct ptlrpc_request *req, int rc)
383 {
384         struct osc_create_async_args *args = ptlrpc_req_async_args(req);
385         struct osc_creator    *oscc = args->rq_oscc;
386         struct lov_stripe_md  *lsm  = args->rq_lsm;
387         struct obd_info       *oinfo = args->rq_oinfo;
388         struct obdo           *oa = oinfo->oi_oa;
389
390         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
391
392         if(rc)
393                 GOTO(out_wake, rc);
394
395         if ((oscc->oscc_flags & OSCC_FLAG_EXITING))
396                 GOTO(out_wake, rc = -EIO);
397
398         if (oscc_has_objects_nolock(oscc, 1)) {
399                 memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
400                 oa->o_id = oscc->oscc_next_id;
401                 lsm->lsm_object_id = oscc->oscc_next_id;
402                 oscc->oscc_next_id++;
403
404                 CDEBUG(D_RPCTRACE, " set oscc_next_id = "LPU64"\n",
405                        oscc->oscc_next_id);
406                GOTO(out_wake, rc = 0);
407         }
408
409         /* should be try wait until recovery finished */
410         if(oscc->oscc_flags & OSCC_FLAG_RECOVERING)
411                 RETURN(-EAGAIN);
412
413         if (oscc->oscc_flags & OSCC_FLAG_NOSPC)
414                 GOTO(out_wake, rc = -ENOSPC);
415
416         if (oscc->oscc_flags & OSCC_FLAG_RDONLY)
417                 GOTO(out_wake, rc = -EROFS);
418
419         /* we not have objects now - continue wait */
420         RETURN(-EAGAIN);
421
422 out_wake:
423
424         rc = oinfo->oi_cb_up(oinfo, rc);
425         ptlrpc_fakereq_finished(req);
426
427         RETURN(rc);
428 }
429
430 static int async_create_interpret(const struct lu_env *env,
431                                   struct ptlrpc_request *req, void *data, int rc)
432 {
433         struct osc_create_async_args *args = ptlrpc_req_async_args(req);
434         struct osc_creator    *oscc = args->rq_oscc;
435         int ret;
436
437         spin_lock(&oscc->oscc_lock);
438         ret = handle_async_create(req, rc);
439         spin_unlock(&oscc->oscc_lock);
440
441         return ret;
442 }
443
444 int osc_create_async(struct obd_export *exp, struct obd_info *oinfo,
445                      struct lov_stripe_md **ea, struct obd_trans_info *oti)
446 {
447         int rc;
448         struct ptlrpc_request *fake_req;
449         struct osc_create_async_args *args;
450         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
451         struct obdo *oa = oinfo->oi_oa;
452         ENTRY;
453
454         if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0)){
455                 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
456                 rc = oinfo->oi_cb_up(oinfo, rc);
457                 RETURN(rc);
458         }
459
460         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
461             oa->o_flags == OBD_FL_RECREATE_OBJS) {
462                 rc = osc_real_create(exp, oinfo->oi_oa, ea, oti);
463                 rc = oinfo->oi_cb_up(oinfo, rc);
464                 RETURN(rc);
465         }
466
467         LASSERT((*ea) != NULL);
468
469         fake_req = ptlrpc_prep_fakereq(oscc->oscc_obd->u.cli.cl_import,
470                                        osc_create_timeout,
471                                        async_create_interpret);
472         if (fake_req == NULL) {
473                 rc = oinfo->oi_cb_up(oinfo, -ENOMEM);
474                 RETURN(-ENOMEM);
475         }
476
477         args = ptlrpc_req_async_args(fake_req);
478         CLASSERT(sizeof(*args) <= sizeof(fake_req->rq_async_args));
479
480         args->rq_oscc  = oscc;
481         args->rq_lsm   = *ea;
482         args->rq_oinfo = oinfo;
483
484         spin_lock(&oscc->oscc_lock);
485         /* try fast path */
486         rc = handle_async_create(fake_req, 0);
487         if (rc == -EAGAIN) {
488                 int is_add;
489                 /* we not have objects - try wait */
490                 is_add = ptlrpcd_add_req(fake_req, PSCOPE_OTHER);
491                 if (!is_add)
492                         list_add(&fake_req->rq_list,
493                                  &oscc->oscc_wait_create_list);
494                 else
495                         rc = is_add;
496         }
497         spin_unlock(&oscc->oscc_lock);
498
499         if (rc != -EAGAIN)
500                 /* need free request if was error hit or
501                  * objects already allocated */
502                 ptlrpc_req_finished(fake_req);
503         else
504                 /* EAGAIN mean - request is delayed */
505                 rc = 0;
506
507         RETURN(rc);
508 }
509
510 int osc_create(struct obd_export *exp, struct obdo *oa,
511                struct lov_stripe_md **ea, struct obd_trans_info *oti)
512 {
513         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
514         struct obd_import  *imp  = exp->exp_obd->u.cli.cl_import;
515         struct lov_stripe_md *lsm;
516         int rc = 0;
517         ENTRY;
518
519         LASSERT(oa);
520         LASSERT(ea);
521         LASSERT_MDS_GROUP(oa->o_gr);
522         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
523
524         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
525             oa->o_flags == OBD_FL_RECREATE_OBJS) {
526                 RETURN(osc_real_create(exp, oa, ea, oti));
527         }
528
529         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
530                 RETURN(osc_real_create(exp, oa, ea, oti));
531
532         /* this is the special case where create removes orphans */
533         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
534             oa->o_flags == OBD_FL_DELORPHAN) {
535                 spin_lock(&oscc->oscc_lock);
536                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
537                         spin_unlock(&oscc->oscc_lock);
538                         RETURN(-EBUSY);
539                 }
540                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
541                         spin_unlock(&oscc->oscc_lock);
542                         RETURN(0);
543                 }
544
545                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
546                 /* seting flag LOW we prevent extra grow precreate size
547                  * and enforce use last assigned size */
548                 oscc->oscc_flags |= OSCC_FLAG_LOW;
549                 spin_unlock(&oscc->oscc_lock);
550                 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
551                        oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
552
553                 /* delete from next_id on up */
554                 oa->o_valid |= OBD_MD_FLID;
555                 oa->o_id = oscc->oscc_next_id - 1;
556
557                 rc = osc_real_create(exp, oa, ea, NULL);
558
559                 spin_lock(&oscc->oscc_lock);
560                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
561                 if (rc == 0 || rc == -ENOSPC) {
562                         struct obd_connect_data *ocd;
563
564                         if (rc == -ENOSPC)
565                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
566                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
567
568                         oscc->oscc_last_id = oa->o_id;
569                         ocd = &imp->imp_connect_data;
570                         if (ocd->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN) {
571                                 CDEBUG(D_HA, "%s: Skip orphan set, reset last "
572                                        "objid\n", oscc->oscc_obd->obd_name);
573                                 oscc->oscc_next_id = oa->o_id + 1;
574                         }
575
576                         /* sanity check for next objid. see bug 17025 */
577                         LASSERT(oscc->oscc_next_id == oa->o_id + 1);
578
579                         CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
580                                LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
581                                oscc->oscc_last_id, rc);
582                 } else {
583                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
584                                oscc->oscc_obd->obd_name, rc);
585                 }
586
587                 cfs_waitq_signal(&oscc->oscc_waitq);
588                 spin_unlock(&oscc->oscc_lock);
589
590                 if (rc < 0)
591                         RETURN(rc);
592         }
593
594         lsm = *ea;
595         if (lsm == NULL) {
596                 rc = obd_alloc_memmd(exp, &lsm);
597                 if (rc < 0)
598                         RETURN(rc);
599         }
600
601         while (1) {
602                 if (oscc_in_sync(oscc))
603                         CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
604                                oscc->oscc_obd->obd_name);
605
606                 rc = oscc_precreate(oscc);
607                 if (rc) {
608                         CDEBUG(D_HA,"%s: error create %d\n",
609                                oscc->oscc_obd->obd_name, rc);
610                         break;
611                 }
612
613                 spin_lock(&oscc->oscc_lock);
614                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
615                         spin_unlock(&oscc->oscc_lock);
616                         break;
617                 }
618                 /* wakeup but recovery not finished */
619                 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
620                         rc = -EIO;
621                         spin_unlock(&oscc->oscc_lock);
622                         break;
623                 }
624
625                 if (oscc_has_objects_nolock(oscc, 1)) {
626                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
627                         oa->o_id = oscc->oscc_next_id;
628                         lsm->lsm_object_id = oscc->oscc_next_id;
629                         *ea = lsm;
630                         oscc->oscc_next_id++;
631                         spin_unlock(&oscc->oscc_lock);
632
633                         CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
634                                exp->exp_obd->obd_name, oscc->oscc_next_id);
635                         break;
636                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
637                         rc = -ENOSPC;
638                         spin_unlock(&oscc->oscc_lock);
639                         break;
640                 } else if (oscc->oscc_flags & OSCC_FLAG_RDONLY) {
641                         rc = -EROFS;
642                         spin_unlock(&oscc->oscc_lock);
643                         break;
644                 }
645
646                 spin_unlock(&oscc->oscc_lock);
647         }
648
649         if (rc == 0)
650                 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
651                        obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
652         else if (*ea == NULL)
653                 obd_free_memmd(exp, &lsm);
654         RETURN(rc);
655 }
656
657 void oscc_init(struct obd_device *obd)
658 {
659         struct osc_creator *oscc;
660
661         if (obd == NULL)
662                 return;
663
664         oscc = &obd->u.cli.cl_oscc;
665
666         memset(oscc, 0, sizeof(*oscc));
667
668         cfs_waitq_init(&oscc->oscc_waitq);
669         spin_lock_init(&oscc->oscc_lock);
670         oscc->oscc_obd = obd;
671         oscc->oscc_grow_count = OST_MIN_PRECREATE;
672         oscc->oscc_max_grow_count = OST_MAX_PRECREATE;
673
674         oscc->oscc_next_id = 2;
675         oscc->oscc_last_id = 1;
676         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
677
678         CFS_INIT_LIST_HEAD(&oscc->oscc_wait_create_list);
679
680         /* XXX the export handle should give the oscc the last object */
681         /* oed->oed_oscc.oscc_last_id = exph->....; */
682 }
683
684 void oscc_fini(struct obd_device *obd)
685 {
686         struct osc_creator *oscc = &obd->u.cli.cl_oscc;
687         ENTRY;
688
689
690         spin_lock(&oscc->oscc_lock);
691         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
692         oscc->oscc_flags |= OSCC_FLAG_EXITING;
693         spin_unlock(&oscc->oscc_lock);
694 }