Whamcloud - gitweb
Add lu_env parameter to ->rq_interpreter call-back. NULL is passed
[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 static int osc_interpret_create(const struct lu_env *env,
65                                 struct ptlrpc_request *req, void *data, int rc)
66 {
67         struct osc_creator *oscc;
68         struct ost_body *body = NULL;
69         ENTRY;
70
71         if (req->rq_repmsg) {
72                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
73                                           lustre_swab_ost_body);
74                 if (body == NULL && rc == 0)
75                         rc = -EPROTO;
76         }
77
78         oscc = req->rq_async_args.pointer_arg[0];
79         LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
80         
81         spin_lock(&oscc->oscc_lock);
82         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
83         switch (rc) {
84         case 0: {
85                 if (body) {
86                         int diff = body->oa.o_id - oscc->oscc_last_id;
87
88                         if (diff < oscc->oscc_grow_count)
89                                 oscc->oscc_grow_count =
90                                         max(diff/3, OST_MIN_PRECREATE);
91                         else
92                                 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
93                         oscc->oscc_last_id = body->oa.o_id;
94                 }
95                 spin_unlock(&oscc->oscc_lock);
96                 break;
97         }
98         case -EAGAIN:
99                 /* valid race delorphan vs create, or somthing after resend */
100                 spin_unlock(&oscc->oscc_lock);
101                 DEBUG_REQ(D_INODE, req, "Got EAGAIN - resend \n");
102                 break;
103         case -ENOSPC:
104         case -EROFS: 
105         case -EFBIG: {
106                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
107                 if (body && rc == -ENOSPC) {
108                         oscc->oscc_grow_count = OST_MIN_PRECREATE;
109                         oscc->oscc_last_id = body->oa.o_id;
110                 }
111                 spin_unlock(&oscc->oscc_lock);
112                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
113                 break;
114         }
115         case -EIO: {
116                 /* filter always set body->oa.o_id as the last_id 
117                  * of filter (see filter_handle_precreate for detail)*/
118                 if (body && body->oa.o_id > oscc->oscc_last_id)
119                         oscc->oscc_last_id = body->oa.o_id;
120                 spin_unlock(&oscc->oscc_lock);
121                 break;
122         }
123         default: {
124                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
125                 oscc->oscc_grow_count = OST_MIN_PRECREATE;
126                 spin_unlock(&oscc->oscc_lock);
127                 DEBUG_REQ(D_ERROR, req,
128                           "Unknown rc %d from async create: failing oscc", rc);
129                 ptlrpc_fail_import(req->rq_import,
130                                    lustre_msg_get_conn_cnt(req->rq_reqmsg));
131         }
132         }
133
134         CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
135                oscc->oscc_last_id, oscc->oscc_next_id);
136
137         cfs_waitq_signal(&oscc->oscc_waitq);
138         RETURN(rc);
139 }
140
141 static int oscc_internal_create(struct osc_creator *oscc)
142 {
143         struct ptlrpc_request *request;
144         struct ost_body *body;
145         __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
146         ENTRY;
147
148         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
149
150         if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
151             oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
152                 spin_unlock(&oscc->oscc_lock);
153                 RETURN(0);
154         }
155
156         if (oscc->oscc_grow_count < oscc->oscc_max_grow_count &&
157             ((oscc->oscc_flags & OSCC_FLAG_LOW) == 0) &&
158             (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
159                    (oscc->oscc_grow_count / 4 + 1)) {
160                 oscc->oscc_flags |= OSCC_FLAG_LOW;
161                 oscc->oscc_grow_count *= 2;
162         }
163
164         if (oscc->oscc_grow_count > oscc->oscc_max_grow_count / 2)
165                 oscc->oscc_grow_count = oscc->oscc_max_grow_count / 2;
166
167         oscc->oscc_flags |= OSCC_FLAG_CREATING;
168         spin_unlock(&oscc->oscc_lock);
169
170         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
171                                   LUSTRE_OST_VERSION, OST_CREATE, 2,
172                                   size, NULL);
173         if (request == NULL) {
174                 spin_lock(&oscc->oscc_lock);
175                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
176                 spin_unlock(&oscc->oscc_lock);
177                 RETURN(-ENOMEM);
178         }
179
180         request->rq_request_portal = OST_CREATE_PORTAL;
181         ptlrpc_at_set_req_timeout(request);
182         body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
183
184         spin_lock(&oscc->oscc_lock);
185         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
186         body->oa.o_gr = oscc->oscc_oa.o_gr;
187         LASSERT(body->oa.o_gr > 0);
188         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
189         spin_unlock(&oscc->oscc_lock);
190         CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
191                body->oa.o_id, oscc->oscc_last_id);
192
193         ptlrpc_req_set_repsize(request, 2, size);
194
195         request->rq_async_args.pointer_arg[0] = oscc;
196         request->rq_interpret_reply = osc_interpret_create;
197         ptlrpcd_add_req(request);
198
199         RETURN(0);
200 }
201
202 static int oscc_has_objects(struct osc_creator *oscc, int count)
203 {
204         int have_objs;
205         spin_lock(&oscc->oscc_lock);
206         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
207
208         if (!have_objs) {
209                 oscc_internal_create(oscc);
210         } else {
211                 spin_unlock(&oscc->oscc_lock);
212         }
213
214         return have_objs;
215 }
216
217 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
218 {
219         int have_objs;
220         int ost_full;
221         int osc_invalid;
222
223         have_objs = oscc_has_objects(oscc, count);
224
225         spin_lock(&oscc->oscc_lock);
226         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
227         spin_unlock(&oscc->oscc_lock);
228
229         osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
230
231         return have_objs || ost_full || osc_invalid;
232 }
233
234 static int oscc_precreate(struct osc_creator *oscc, int wait)
235 {
236         struct l_wait_info lwi = { 0 };
237         int rc = 0;
238         ENTRY;
239
240         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
241                 RETURN(0);
242
243         if (!wait)
244                 RETURN(0);
245
246         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
247         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
248
249         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
250                 rc = -ENOSPC;
251
252         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
253                 rc = -EIO;
254
255         RETURN(rc);
256 }
257
258 int oscc_recovering(struct osc_creator *oscc)
259 {
260         int recov = 0;
261
262         spin_lock(&oscc->oscc_lock);
263         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
264         spin_unlock(&oscc->oscc_lock);
265
266         return recov;
267 }
268
269 /* decide if the OST has remaining object, return value :
270         0 : the OST has remaining object, and don't need to do precreate.
271         1 : the OST has no remaining object, and will send a RPC for precreate.
272         2 : the OST has no remaining object, and will not get any for
273             a potentially very long time
274      1000 : unusable
275  */
276 int osc_precreate(struct obd_export *exp)
277 {
278         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
279         struct obd_import *imp = exp->exp_imp_reverse;
280         ENTRY;
281
282         LASSERT(oscc != NULL);
283         if (imp != NULL && imp->imp_deactive)
284                 RETURN(1000);
285
286         if (oscc->oscc_last_id < oscc->oscc_next_id) {
287                 spin_lock(&oscc->oscc_lock);
288                 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
289                         spin_unlock(&oscc->oscc_lock);
290                         RETURN(1000);
291                 }
292                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
293                         spin_unlock(&oscc->oscc_lock);
294                         RETURN(1);
295                 }
296                 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
297                         spin_unlock(&oscc->oscc_lock);
298                         RETURN(2);
299                 }
300
301                 if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
302                         spin_unlock(&oscc->oscc_lock);
303                         RETURN(1);
304                 }
305
306                 oscc_internal_create(oscc);
307                 RETURN(1);
308         }
309         RETURN(0);
310 }
311
312 int osc_create(struct obd_export *exp, struct obdo *oa,
313                struct lov_stripe_md **ea, struct obd_trans_info *oti)
314 {
315         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
316         struct lov_stripe_md *lsm;
317         int try_again = 1, rc = 0;
318         ENTRY;
319
320         LASSERT(oa);
321         LASSERT(ea);
322         LASSERT(oa->o_gr > 0);
323         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
324
325         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
326             oa->o_flags == OBD_FL_RECREATE_OBJS) {
327                 RETURN(osc_real_create(exp, oa, ea, oti));
328         }
329
330         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
331                 RETURN(osc_real_create(exp, oa, ea, oti));
332
333         /* this is the special case where create removes orphans */
334         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
335             oa->o_flags == OBD_FL_DELORPHAN) {
336                 spin_lock(&oscc->oscc_lock);
337                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
338                         spin_unlock(&oscc->oscc_lock);
339                         RETURN(-EBUSY);
340                 }
341                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
342                         spin_unlock(&oscc->oscc_lock);
343                         RETURN(0);
344                 }
345                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
346                 /* seting flag LOW we prevent extra grow precreate size
347                  * and enforce use last assigned size */
348                 oscc->oscc_flags |= OSCC_FLAG_LOW;
349                 spin_unlock(&oscc->oscc_lock);
350                 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
351                        oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
352
353                 /* delete from next_id on up */
354                 oa->o_valid |= OBD_MD_FLID;
355                 oa->o_id = oscc->oscc_next_id - 1;
356
357                 rc = osc_real_create(exp, oa, ea, NULL);
358
359                 spin_lock(&oscc->oscc_lock);
360                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
361                 if (rc == 0 || rc == -ENOSPC) {
362                         if (rc == -ENOSPC)
363                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
364                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
365                         oscc->oscc_last_id = oa->o_id;
366                         CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
367                                LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
368                                oscc->oscc_last_id, rc);
369                         cfs_waitq_signal(&oscc->oscc_waitq);
370                 } else {
371                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
372                                oscc->oscc_obd->obd_name, rc);
373                 }
374                 spin_unlock(&oscc->oscc_lock);
375
376
377                 RETURN(rc);
378         }
379
380         lsm = *ea;
381         if (lsm == NULL) {
382                 rc = obd_alloc_memmd(exp, &lsm);
383                 if (rc < 0)
384                         RETURN(rc);
385         }
386
387         while (try_again) {
388                 /* If orphans are being recovered, then we must wait until
389                    it is finished before we can continue with create. */
390                 if (oscc_recovering(oscc)) {
391                         struct l_wait_info lwi;
392
393                         CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
394                                oscc->oscc_obd->obd_name);
395
396                         lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(
397                                 obd_timeout / 4)), NULL, NULL);
398                         rc = l_wait_event(oscc->oscc_waitq,
399                                           !oscc_recovering(oscc), &lwi);
400                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
401                         if (rc == -ETIMEDOUT) {
402                                 CDEBUG(D_HA,"%s: timeout waiting on recovery\n",
403                                        oscc->oscc_obd->obd_name);
404                                 RETURN(rc);
405                         }
406                         CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
407                                oscc->oscc_obd->obd_name);
408                 }
409
410                 spin_lock(&oscc->oscc_lock);
411                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
412                         spin_unlock(&oscc->oscc_lock);
413                         break;
414                 }
415
416                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
417                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
418                         oa->o_id = oscc->oscc_next_id;
419                         lsm->lsm_object_id = oscc->oscc_next_id;
420                         *ea = lsm;
421                         oscc->oscc_next_id++;
422                         try_again = 0;
423
424                         CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
425                                exp->exp_obd->obd_name, oscc->oscc_next_id);
426                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
427                         rc = -ENOSPC;
428                         spin_unlock(&oscc->oscc_lock);
429                         break;
430                 }
431                 spin_unlock(&oscc->oscc_lock);
432                 rc = oscc_precreate(oscc, try_again);
433                 if (rc)
434                         break;
435         }
436
437         if (rc == 0)
438                 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
439                        obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
440         else if (*ea == NULL)
441                 obd_free_memmd(exp, &lsm);
442         RETURN(rc);
443 }
444
445 void oscc_init(struct obd_device *obd)
446 {
447         struct osc_creator *oscc;
448
449         if (obd == NULL)
450                 return;
451
452         oscc = &obd->u.cli.cl_oscc;
453
454         memset(oscc, 0, sizeof(*oscc));
455         CFS_INIT_LIST_HEAD(&oscc->oscc_list);
456         cfs_waitq_init(&oscc->oscc_waitq);
457         spin_lock_init(&oscc->oscc_lock);
458         oscc->oscc_obd = obd;
459         oscc->oscc_grow_count = OST_MIN_PRECREATE;
460         oscc->oscc_max_grow_count = OST_MAX_PRECREATE;
461
462         oscc->oscc_next_id = 2;
463         oscc->oscc_last_id = 1;
464         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
465         /* XXX the export handle should give the oscc the last object */
466         /* oed->oed_oscc.oscc_last_id = exph->....; */
467 }