Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author Peter Braam <braam@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  *  For testing and management it is treated as an obd_device,
26  *  although * it does not export a full OBD method table (the
27  *  requests are coming * in over the wire, so object target modules
28  *  do not have a full * method table.)
29  *
30  */
31
32 #ifndef EXPORT_SYMTAB
33 # define EXPORT_SYMTAB
34 #endif
35 #define DEBUG_SUBSYSTEM S_OSC
36
37 #ifdef __KERNEL__
38 # include <libcfs/libcfs.h>
39 #else /* __KERNEL__ */
40 # include <liblustre.h>
41 #endif
42
43 #ifdef  __CYGWIN__
44 # include <ctype.h>
45 #endif
46
47 # include <lustre_dlm.h>
48 #include <obd_class.h>
49 #include "osc_internal.h"
50
51 static int osc_interpret_create(struct ptlrpc_request *req, void *data, int rc)
52 {
53         struct osc_creator *oscc;
54         struct ost_body *body = NULL;
55         ENTRY;
56
57         if (req->rq_repmsg) {
58                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
59                                           lustre_swab_ost_body);
60                 if (body == NULL && rc == 0)
61                         rc = -EPROTO;
62         }
63
64         oscc = req->rq_async_args.pointer_arg[0];
65         LASSERT(oscc && (oscc->oscc_obd != LP_POISON));
66         
67         spin_lock(&oscc->oscc_lock);
68         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
69         switch (rc) {
70         case 0: {
71                 if (body) {
72                         int diff = body->oa.o_id - oscc->oscc_last_id;
73
74                         if (diff < oscc->oscc_grow_count)
75                                 oscc->oscc_grow_count =
76                                         max(diff/3, OST_MIN_PRECREATE);
77                         else
78                                 oscc->oscc_flags &= ~OSCC_FLAG_LOW;
79                         oscc->oscc_last_id = body->oa.o_id;
80                 }
81                 spin_unlock(&oscc->oscc_lock);
82                 break;
83         }
84         case -EAGAIN:
85                 /* valid race delorphan vs create, or somthing after resend */
86                 spin_unlock(&oscc->oscc_lock);
87                 DEBUG_REQ(D_INODE, req, "Got EGAIN - resend \n");
88                 break;
89         case -ENOSPC:
90         case -EROFS: {
91                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
92                 if (body && rc == -ENOSPC) {
93                         oscc->oscc_grow_count = OST_MIN_PRECREATE;
94                         oscc->oscc_last_id = body->oa.o_id;
95                 }
96                 spin_unlock(&oscc->oscc_lock);
97                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
98                 break;
99         }
100         case -EIO: {
101                 /* filter always set body->oa.o_id as the last_id 
102                  * of filter (see filter_handle_precreate for detail)*/
103                 if (body && body->oa.o_id > oscc->oscc_last_id)
104                         oscc->oscc_last_id = body->oa.o_id;
105                 spin_unlock(&oscc->oscc_lock);
106                 break;
107         }
108         default: {
109                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
110                 oscc->oscc_grow_count = OST_MIN_PRECREATE;
111                 spin_unlock(&oscc->oscc_lock);
112                 DEBUG_REQ(D_ERROR, req,
113                           "Unknown rc %d from async create: failing oscc", rc);
114                 ptlrpc_fail_import(req->rq_import,
115                                    lustre_msg_get_conn_cnt(req->rq_reqmsg));
116         }
117         }
118
119         CDEBUG(D_HA, "preallocated through id "LPU64" (next to use "LPU64")\n",
120                oscc->oscc_last_id, oscc->oscc_next_id);
121
122         cfs_waitq_signal(&oscc->oscc_waitq);
123         RETURN(rc);
124 }
125
126 static int oscc_internal_create(struct osc_creator *oscc)
127 {
128         struct ptlrpc_request *request;
129         struct ost_body *body;
130         int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
131         ENTRY;
132
133         LASSERT_SPIN_LOCKED(&oscc->oscc_lock);
134
135         if (oscc->oscc_grow_count < OST_MAX_PRECREATE &&
136             !(oscc->oscc_flags & (OSCC_FLAG_LOW | OSCC_FLAG_RECOVERING)) &&
137             (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
138                    (oscc->oscc_grow_count / 4 + 1)) {
139                 oscc->oscc_flags |= OSCC_FLAG_LOW;
140                 oscc->oscc_grow_count *= 2;
141         }
142
143         if (oscc->oscc_grow_count > OST_MAX_PRECREATE / 2)
144                 oscc->oscc_grow_count = OST_MAX_PRECREATE / 2;
145
146         if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
147             oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
148                 spin_unlock(&oscc->oscc_lock);
149                 RETURN(0);
150         }
151         oscc->oscc_flags |= OSCC_FLAG_CREATING;
152         spin_unlock(&oscc->oscc_lock);
153
154         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
155                                   LUSTRE_OST_VERSION, OST_CREATE, 2,
156                                   size, NULL);
157         if (request == NULL) {
158                 spin_lock(&oscc->oscc_lock);
159                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
160                 spin_unlock(&oscc->oscc_lock);
161                 RETURN(-ENOMEM);
162         }
163
164         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
165         body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
166
167         spin_lock(&oscc->oscc_lock);
168         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
169         body->oa.o_gr = oscc->oscc_oa.o_gr;
170         LASSERT(body->oa.o_gr > 0);
171         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
172         spin_unlock(&oscc->oscc_lock);
173         CDEBUG(D_RPCTRACE, "prealloc through id "LPU64" (last seen "LPU64")\n",
174                body->oa.o_id, oscc->oscc_last_id);
175
176         ptlrpc_req_set_repsize(request, 2, size);
177
178         request->rq_async_args.pointer_arg[0] = oscc;
179         request->rq_interpret_reply = osc_interpret_create;
180         ptlrpcd_add_req(request);
181
182         RETURN(0);
183 }
184
185 static int oscc_has_objects(struct osc_creator *oscc, int count)
186 {
187         int have_objs;
188         spin_lock(&oscc->oscc_lock);
189         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
190
191         if (!have_objs) {
192                 oscc_internal_create(oscc);
193         } else {
194                 spin_unlock(&oscc->oscc_lock);
195         }
196
197         return have_objs;
198 }
199
200 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
201 {
202         int have_objs;
203         int ost_full;
204         int osc_invalid;
205
206         have_objs = oscc_has_objects(oscc, count);
207
208         spin_lock(&oscc->oscc_lock);
209         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
210         spin_unlock(&oscc->oscc_lock);
211
212         osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
213
214         return have_objs || ost_full || osc_invalid;
215 }
216
217 static int oscc_precreate(struct osc_creator *oscc, int wait)
218 {
219         struct l_wait_info lwi = { 0 };
220         int rc = 0;
221         ENTRY;
222
223         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
224                 RETURN(0);
225
226         if (!wait)
227                 RETURN(0);
228
229         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
230         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
231
232         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
233                 rc = -ENOSPC;
234
235         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
236                 rc = -EIO;
237
238         RETURN(rc);
239 }
240
241 int oscc_recovering(struct osc_creator *oscc)
242 {
243         int recov = 0;
244
245         spin_lock(&oscc->oscc_lock);
246         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
247         spin_unlock(&oscc->oscc_lock);
248
249         return recov;
250 }
251
252 /* decide if the OST has remaining object, return value :
253         0 : the OST has remaining object, and don't need to do precreate.
254         1 : the OST has no remaining object, and will send a RPC for precreate.
255         2 : the OST has no remaining object, and will not get any for
256             a potentially very long time
257      1000 : unusable
258  */
259 int osc_precreate(struct obd_export *exp)
260 {
261         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
262         struct obd_import *imp = exp->exp_imp_reverse;
263         ENTRY;
264
265         LASSERT(oscc != NULL);
266         if (imp != NULL && imp->imp_deactive)
267                 RETURN(1000);
268
269         if (oscc->oscc_last_id < oscc->oscc_next_id) {
270                 spin_lock(&oscc->oscc_lock);
271                 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
272                         spin_unlock(&oscc->oscc_lock);
273                         RETURN(1000);
274                 }
275                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
276                         spin_unlock(&oscc->oscc_lock);
277                         RETURN(1);
278                 }
279                 if (oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
280                         spin_unlock(&oscc->oscc_lock);
281                         RETURN(2);
282                 }
283
284                 if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
285                         spin_unlock(&oscc->oscc_lock);
286                         RETURN(1);
287                 }
288
289                 oscc_internal_create(oscc);
290                 RETURN(1);
291         }
292         RETURN(0);
293 }
294
295 int osc_create(struct obd_export *exp, struct obdo *oa,
296                struct lov_stripe_md **ea, struct obd_trans_info *oti)
297 {
298         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
299         struct lov_stripe_md *lsm;
300         int try_again = 1, rc = 0;
301         ENTRY;
302
303         LASSERT(oa);
304         LASSERT(ea);
305         LASSERT(oa->o_gr > 0);
306         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
307
308         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
309             oa->o_flags == OBD_FL_RECREATE_OBJS) {
310                 RETURN(osc_real_create(exp, oa, ea, oti));
311         }
312
313         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
314                 RETURN(osc_real_create(exp, oa, ea, oti));
315
316         /* this is the special case where create removes orphans */
317         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
318             oa->o_flags == OBD_FL_DELORPHAN) {
319                 spin_lock(&oscc->oscc_lock);
320                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
321                         spin_unlock(&oscc->oscc_lock);
322                         RETURN(-EBUSY);
323                 }
324                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
325                         spin_unlock(&oscc->oscc_lock);
326                         RETURN(0);
327                 }
328                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
329                 spin_unlock(&oscc->oscc_lock);
330                 CDEBUG(D_HA, "%s: oscc recovery started - delete to "LPU64"\n",
331                        oscc->oscc_obd->obd_name, oscc->oscc_next_id - 1);
332
333                 /* delete from next_id on up */
334                 oa->o_valid |= OBD_MD_FLID;
335                 oa->o_id = oscc->oscc_next_id - 1;
336
337                 rc = osc_real_create(exp, oa, ea, NULL);
338
339                 spin_lock(&oscc->oscc_lock);
340                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
341                 if (rc == 0 || rc == -ENOSPC) {
342                         if (rc == -ENOSPC)
343                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
344                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
345                         oscc->oscc_last_id = oa->o_id;
346                         CDEBUG(D_HA, "%s: oscc recovery finished, last_id: "
347                                LPU64", rc: %d\n", oscc->oscc_obd->obd_name,
348                                oscc->oscc_last_id, rc);
349                         cfs_waitq_signal(&oscc->oscc_waitq);
350                 } else {
351                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
352                                oscc->oscc_obd->obd_name, rc);
353                 }
354                 spin_unlock(&oscc->oscc_lock);
355
356
357                 RETURN(rc);
358         }
359
360         lsm = *ea;
361         if (lsm == NULL) {
362                 rc = obd_alloc_memmd(exp, &lsm);
363                 if (rc < 0)
364                         RETURN(rc);
365         }
366
367         while (try_again) {
368                 /* If orphans are being recovered, then we must wait until
369                    it is finished before we can continue with create. */
370                 if (oscc_recovering(oscc)) {
371                         struct l_wait_info lwi;
372
373                         CDEBUG(D_HA,"%s: oscc recovery in progress, waiting\n",
374                                oscc->oscc_obd->obd_name);
375
376                         lwi = LWI_TIMEOUT(cfs_timeout_cap(cfs_time_seconds(obd_timeout/4)),
377                                           NULL, NULL);
378                         rc = l_wait_event(oscc->oscc_waitq,
379                                           !oscc_recovering(oscc), &lwi);
380                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
381                         if (rc == -ETIMEDOUT) {
382                                 CDEBUG(D_HA,"%s: timeout waiting on recovery\n",
383                                        oscc->oscc_obd->obd_name);
384                                 RETURN(rc);
385                         }
386                         CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
387                                oscc->oscc_obd->obd_name);
388                 }
389
390                 spin_lock(&oscc->oscc_lock);
391                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
392                         spin_unlock(&oscc->oscc_lock);
393                         break;
394                 }
395
396                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
397                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
398                         oa->o_id = oscc->oscc_next_id;
399                         lsm->lsm_object_id = oscc->oscc_next_id;
400                         *ea = lsm;
401                         oscc->oscc_next_id++;
402                         try_again = 0;
403
404                         CDEBUG(D_RPCTRACE, "%s: set oscc_next_id = "LPU64"\n",
405                                exp->exp_obd->obd_name, oscc->oscc_next_id);
406                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
407                         rc = -ENOSPC;
408                         spin_unlock(&oscc->oscc_lock);
409                         break;
410                 }
411                 spin_unlock(&oscc->oscc_lock);
412                 rc = oscc_precreate(oscc, try_again);
413                 if (rc)
414                         break;
415         }
416
417         if (rc == 0)
418                 CDEBUG(D_INFO, "%s: returning objid "LPU64"\n",
419                        obd2cli_tgt(oscc->oscc_obd), lsm->lsm_object_id);
420         else if (*ea == NULL)
421                 obd_free_memmd(exp, &lsm);
422         RETURN(rc);
423 }
424
425 void oscc_init(struct obd_device *obd)
426 {
427         struct osc_creator *oscc;
428
429         if (obd == NULL)
430                 return;
431
432         oscc = &obd->u.cli.cl_oscc;
433
434         memset(oscc, 0, sizeof(*oscc));
435         CFS_INIT_LIST_HEAD(&oscc->oscc_list);
436         cfs_waitq_init(&oscc->oscc_waitq);
437         spin_lock_init(&oscc->oscc_lock);
438         oscc->oscc_obd = obd;
439         oscc->oscc_grow_count = OST_MIN_PRECREATE;
440
441         oscc->oscc_next_id = 2;
442         oscc->oscc_last_id = 1;
443         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
444         /* XXX the export handle should give the oscc the last object */
445         /* oed->oed_oscc.oscc_last_id = exph->....; */
446 }