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