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