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