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