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