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