Whamcloud - gitweb
a80cd1adf6f198f1a6cf9b3276ed4eba1f026491
[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 Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *  For testing and management it is treated as an obd_device,
23  *  although * it does not export a full OBD method table (the
24  *  requests are coming * in over the wire, so object target modules
25  *  do not have a full * method table.)
26  *
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_OSC
33
34 #ifdef __KERNEL__
35 # include <linux/version.h>
36 # include <linux/module.h>
37 # include <linux/mm.h>
38 # include <linux/highmem.h>
39 # include <linux/ctype.h>
40 # include <linux/init.h>
41 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
42 #  include <linux/workqueue.h>
43 #  include <linux/smp_lock.h>
44 # else
45 #  include <linux/locks.h>
46 # endif
47 #else /* __KERNEL__ */
48 # include <liblustre.h>
49 #endif
50
51 #ifdef  __CYGWIN__
52 # include <ctype.h>
53 #endif
54
55 # include <linux/lustre_dlm.h>
56 #include <linux/obd_class.h>
57 #include "osc_internal.h"
58
59 static int osc_interpret_create(struct ptlrpc_request *req, void *data, int rc)
60 {
61         struct osc_creator *oscc;
62         struct ost_body *body = NULL;
63         ENTRY;
64
65         if (req->rq_repmsg) {
66                 body = lustre_swab_repbuf(req, 0, sizeof(*body),
67                                           lustre_swab_ost_body);
68                 if (body == NULL && rc == 0)
69                         rc = -EPROTO;
70         }
71
72         oscc = req->rq_async_args.pointer_arg[0];
73         spin_lock(&oscc->oscc_lock);
74         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
75         if (rc == -ENOSPC || rc == -EROFS) {
76                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
77                 if (body && rc == -ENOSPC) {
78                         oscc->oscc_grow_count = OST_MIN_PRECREATE;
79                         oscc->oscc_last_id = body->oa.o_id;
80                 }
81                 spin_unlock(&oscc->oscc_lock);
82                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
83         } else if (rc != 0 && rc != -EIO) {
84                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
85                 oscc->oscc_grow_count = OST_MIN_PRECREATE;
86                 spin_unlock(&oscc->oscc_lock);
87                 DEBUG_REQ(D_ERROR, req,
88                           "unknown rc %d from async create: failing oscc", rc);
89                 ptlrpc_fail_import(req->rq_import, req->rq_import_generation);
90         } else {
91                 if (rc == 0) {
92                         oscc->oscc_flags &= ~OSCC_FLAG_LOW;
93                         if (body) {
94                                 int diff = body->oa.o_id - oscc->oscc_last_id;
95                                 if (diff != oscc->oscc_grow_count)
96                                         oscc->oscc_grow_count =
97                                                 max(diff/3, OST_MIN_PRECREATE);
98                                 oscc->oscc_last_id = body->oa.o_id;
99                         }
100                 }
101                 spin_unlock(&oscc->oscc_lock);
102         }
103
104         CDEBUG(D_HA, "preallocated through id "LPU64" (last used "LPU64")\n",
105                oscc->oscc_last_id, oscc->oscc_next_id);
106
107         wake_up(&oscc->oscc_waitq);
108         RETURN(rc);
109 }
110
111 static int oscc_internal_create(struct osc_creator *oscc)
112 {
113         struct ptlrpc_request *request;
114         struct ost_body *body;
115         int size = sizeof(*body);
116         ENTRY;
117
118         spin_lock(&oscc->oscc_lock);
119         if (oscc->oscc_grow_count < OST_MAX_PRECREATE &&
120             !(oscc->oscc_flags & (OSCC_FLAG_LOW | OSCC_FLAG_RECOVERING)) &&
121             (__s64)(oscc->oscc_last_id - oscc->oscc_next_id) <=
122                    (oscc->oscc_grow_count / 4 + 1)) {
123                 oscc->oscc_flags |= OSCC_FLAG_LOW;
124                 oscc->oscc_grow_count *= 2;
125         }
126
127         if (oscc->oscc_grow_count > OST_MAX_PRECREATE / 2)
128                 oscc->oscc_grow_count = OST_MAX_PRECREATE / 2;
129
130         if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
131             oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
132                 spin_unlock(&oscc->oscc_lock);
133                 RETURN(0);
134         }
135         oscc->oscc_flags |= OSCC_FLAG_CREATING;
136         spin_unlock(&oscc->oscc_lock);
137
138         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
139                                   LUSTRE_OBD_VERSION, OST_CREATE,
140                                   1, &size, NULL);
141         if (request == NULL) {
142                 spin_lock(&oscc->oscc_lock);
143                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
144                 spin_unlock(&oscc->oscc_lock);
145                 RETURN(-ENOMEM);
146         }
147
148         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
149         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
150
151         spin_lock(&oscc->oscc_lock);
152         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
153         /* probably we should take frequence of request into account? -bzzz */
154         if (oscc->oscc_grow_count < oscc->oscc_max_grow_count) {
155                 oscc->oscc_grow_count *= 2;
156                 if (oscc->oscc_grow_count > oscc->oscc_max_grow_count)
157                         oscc->oscc_grow_count = oscc->oscc_max_grow_count;
158         }
159         body->oa.o_gr = oscc->oscc_gr;
160         LASSERT(body->oa.o_gr > 0);
161         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
162         spin_unlock(&oscc->oscc_lock);
163         CDEBUG(D_INFO, "preallocating through id "LPU64" (last used "LPU64")\n",
164                body->oa.o_id, oscc->oscc_next_id);
165
166         request->rq_replen = lustre_msg_size(1, &size);
167
168         request->rq_async_args.pointer_arg[0] = oscc;
169         request->rq_interpret_reply = osc_interpret_create;
170         ptlrpcd_add_req(request);
171
172         RETURN(0);
173 }
174
175 static int oscc_has_objects(struct osc_creator *oscc, int count)
176 {
177         int have_objs;
178         spin_lock(&oscc->oscc_lock);
179         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
180         spin_unlock(&oscc->oscc_lock);
181
182         if (!have_objs)
183                 oscc_internal_create(oscc);
184
185         return have_objs;
186 }
187
188 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
189 {
190         int have_objs;
191         int ost_full;
192         int osc_invalid;
193
194         have_objs = oscc_has_objects(oscc, count);
195
196         spin_lock(&oscc->oscc_lock);
197         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
198         spin_unlock(&oscc->oscc_lock);
199
200         osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
201
202         return have_objs || ost_full || osc_invalid;
203 }
204
205 static int oscc_precreate(struct osc_creator *oscc, int wait)
206 {
207         struct l_wait_info lwi = { 0 };
208         int rc = 0;
209         ENTRY;
210
211         if (oscc_has_objects(oscc, oscc->oscc_grow_count / 2))
212                 RETURN(0);
213
214         if (!wait)
215                 RETURN(0);
216
217         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
218         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
219
220         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
221                 rc = -ENOSPC;
222
223         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
224                 rc = -EIO;
225
226         RETURN(rc);
227 }
228
229 int oscc_recovering(struct osc_creator *oscc)
230 {
231         int recov = 0;
232
233         spin_lock(&oscc->oscc_lock);
234         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
235         spin_unlock(&oscc->oscc_lock);
236
237         return recov;
238 }
239
240 int osc_create(struct obd_export *exp, struct obdo *oa,
241                void *acl, int acl_size,
242                struct lov_stripe_md **ea, struct obd_trans_info *oti)
243 {
244         struct lov_stripe_md *lsm;
245         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
246         int try_again = 1, rc = 0;
247         ENTRY;
248         LASSERT(oa);
249         LASSERT(ea);
250         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
251         LASSERT(oa->o_gr > 0);
252         LASSERT(acl == NULL && acl_size == 0);
253
254         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
255             oa->o_flags == OBD_FL_RECREATE_OBJS) {
256                 /* Exceptional case where we are trying to repair missing
257                  * objects for various groups.  We have already validated that
258                  * this is a valid group for the file.  Don't set oscc->oscc_gr.
259                  */
260                 RETURN(osc_real_create(exp, oa, ea, oti));
261         }
262
263         LASSERT(oscc->oscc_gr == 0 || oscc->oscc_gr == oa->o_gr);
264         oscc->oscc_gr = oa->o_gr;
265
266         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
267                 RETURN(osc_real_create(exp, oa, ea, oti));
268
269         /* this is the special case where create removes orphans */
270         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
271             oa->o_flags == OBD_FL_DELORPHAN) {
272                 spin_lock(&oscc->oscc_lock);
273                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
274                         spin_unlock(&oscc->oscc_lock);
275                         return -EBUSY;
276                 }
277                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
278                         spin_unlock(&oscc->oscc_lock);
279                         return 0;
280                 }
281                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
282                 spin_unlock(&oscc->oscc_lock);
283                 CDEBUG(D_HA, "%s; oscc recovery started\n",
284                        exp->exp_obd->obd_name);
285                 LASSERT(oscc->oscc_flags & OSCC_FLAG_RECOVERING);
286
287                 /* delete from next_id on up */
288                 oa->o_valid |= OBD_MD_FLID;
289                 oa->o_id = oscc->oscc_next_id - 1;
290
291                 CDEBUG(D_HA, "%s: deleting to next_id: "LPU64"\n",
292                        oscc->oscc_obd->obd_name, oa->o_id);
293
294                 rc = osc_real_create(exp, oa, ea, NULL);
295                 if (oscc->oscc_obd == NULL) {
296                         CWARN("the obd for oscc %p has been freed\n", oscc);
297                         RETURN(rc);
298                 }
299
300                 spin_lock(&oscc->oscc_lock);
301                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
302                 if (rc == 0 || rc == -ENOSPC) {
303                         if (rc == -ENOSPC)
304                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
305                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
306                         oscc->oscc_last_id = oa->o_id;
307                         
308                         CDEBUG(D_HA, "%s: oscc recovery finished: %d\n",
309                                oscc->oscc_obd->obd_name, rc);
310                         wake_up(&oscc->oscc_waitq);
311                         
312                 } else {
313                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
314                                oscc->oscc_obd->obd_name, rc);
315                 }
316                 spin_unlock(&oscc->oscc_lock);
317
318                 RETURN(rc);
319         }
320
321         lsm = *ea;
322         if (lsm == NULL) {
323                 rc = obd_alloc_memmd(exp, &lsm);
324                 if (rc < 0)
325                         RETURN(rc);
326         }
327
328         while (try_again) {
329                 /* If orphans are being recovered, then we must wait until 
330                    it is finished before we can continue with create. */
331                 if (oscc_recovering(oscc)) {
332                         struct l_wait_info lwi;
333                        
334                         CDEBUG(D_HA,"%p: oscc recovery in progress, waiting\n",
335                                oscc);
336                         lwi = LWI_TIMEOUT(MAX(obd_timeout*HZ/4, 1), NULL, NULL);
337                         rc = l_wait_event(oscc->oscc_waitq,
338                                           !oscc_recovering(oscc), &lwi);
339                         
340                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
341                         if (rc == -ETIMEDOUT) {
342                                 CDEBUG(D_HA,"%p: timeout waiting on recovery\n",
343                                        oscc);
344                                 RETURN(rc);
345                         }
346                         CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
347                                exp->exp_obd->obd_name);
348                 }
349                 
350                 spin_lock(&oscc->oscc_lock);
351                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
352                         spin_unlock(&oscc->oscc_lock);
353                         break;
354                 }
355
356                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
357                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
358                         oa->o_id = oscc->oscc_next_id;
359                         oa->o_gr = oscc->oscc_gr;
360                         lsm->lsm_object_id = oscc->oscc_next_id;
361                         lsm->lsm_object_gr = oscc->oscc_gr;
362                         *ea = lsm;
363                         oscc->oscc_next_id++;
364                         try_again = 0;
365                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
366                         rc = -ENOSPC;
367                         spin_unlock(&oscc->oscc_lock);
368                         break;
369                 }
370                 spin_unlock(&oscc->oscc_lock);
371                 rc = oscc_precreate(oscc, try_again);
372                 if (rc)
373                         break;
374         }
375
376         if (rc == 0)
377                 CDEBUG(D_HA, "%s: returning objid "LPU64"\n",
378                        oscc->oscc_obd->u.cli.cl_import->imp_target_uuid.uuid,
379                        lsm->lsm_object_id);
380         else if (*ea == NULL)
381                 obd_free_memmd(exp, &lsm);
382         RETURN(rc);
383 }
384
385 void oscc_init(struct obd_device *obd)
386 {
387         struct osc_creator *oscc;
388
389         if (obd == NULL)
390                 return;
391
392         oscc = &obd->u.cli.cl_oscc;
393
394         memset(oscc, 0, sizeof(*oscc));
395         INIT_LIST_HEAD(&oscc->oscc_list);
396         init_waitqueue_head(&oscc->oscc_waitq);
397         spin_lock_init(&oscc->oscc_lock);
398         oscc->oscc_obd = obd;
399         oscc->oscc_kick_barrier = 100;
400         oscc->oscc_max_grow_count = 2000;
401         oscc->oscc_grow_count = OST_MIN_PRECREATE;
402
403         oscc->oscc_next_id = 2;
404         oscc->oscc_last_id = 1;
405         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
406         /* XXX the export handle should give the oscc the last object */
407         /* oed->oed_oscc.oscc_last_id = exph->....; */
408 }