Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[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,
60                                 int rc)
61 {
62         struct osc_creator *oscc;
63         struct ost_body *body = NULL;
64         ENTRY;
65
66         if (req->rq_repmsg) {
67                 body = lustre_swab_repbuf(req, 0, sizeof(*body),
68                                           lustre_swab_ost_body);
69                 if (body == NULL && rc == 0)
70                         rc = -EPROTO;
71         }
72
73         oscc = req->rq_async_args.pointer_arg[0];
74         spin_lock(&oscc->oscc_lock);
75         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
76         if (body)
77                 oscc->oscc_last_id = body->oa.o_id;
78         if (rc == -ENOSPC) {
79                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
80                 spin_unlock(&oscc->oscc_lock);
81                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
82         } else if (rc != 0 && rc != -EIO) {
83                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
84                 spin_unlock(&oscc->oscc_lock);
85                 DEBUG_REQ(D_ERROR, req,
86                           "unknown rc %d from async create: failing oscc", rc);
87                 ptlrpc_fail_import(req->rq_import, req->rq_import_generation);
88         } else {
89                 spin_unlock(&oscc->oscc_lock);
90         }
91
92         CDEBUG(D_HA, "preallocated through id "LPU64" (last used "LPU64")\n",
93                oscc->oscc_last_id, oscc->oscc_next_id);
94
95         wake_up(&oscc->oscc_waitq);
96         RETURN(rc);
97 }
98
99 static int oscc_internal_create(struct osc_creator *oscc)
100 {
101         struct ptlrpc_request *request;
102         struct ost_body *body;
103         int size = sizeof(*body);
104         ENTRY;
105
106         spin_lock(&oscc->oscc_lock);
107         if (oscc->oscc_flags & OSCC_FLAG_CREATING ||
108             oscc->oscc_flags & OSCC_FLAG_RECOVERING) {
109                 spin_unlock(&oscc->oscc_lock);
110                 RETURN(0);
111         }
112         oscc->oscc_flags |= OSCC_FLAG_CREATING;
113         spin_unlock(&oscc->oscc_lock);
114
115         request = ptlrpc_prep_req(oscc->oscc_obd->u.cli.cl_import,
116                                   LUSTRE_OBD_VERSION, OST_CREATE,
117                                   1, &size, NULL);
118         if (request == NULL) {
119                 spin_lock(&oscc->oscc_lock);
120                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
121                 spin_unlock(&oscc->oscc_lock);
122                 RETURN(-ENOMEM);
123         }
124
125         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
126         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
127
128         spin_lock(&oscc->oscc_lock);
129         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
130         /* probably we should take frequence of request into account? -bzzz */
131         if (oscc->oscc_grow_count < oscc->oscc_max_grow_count) {
132                 oscc->oscc_grow_count *= 2;
133                 if (oscc->oscc_grow_count > oscc->oscc_max_grow_count)
134                         oscc->oscc_grow_count = oscc->oscc_max_grow_count;
135         }
136         body->oa.o_gr = oscc->oscc_gr;
137         LASSERT(body->oa.o_gr > 0);
138         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
139         spin_unlock(&oscc->oscc_lock);
140         CDEBUG(D_INFO, "preallocating through id "LPU64" (last used "LPU64")\n",
141                body->oa.o_id, oscc->oscc_next_id);
142
143         request->rq_replen = lustre_msg_size(1, &size);
144
145         request->rq_async_args.pointer_arg[0] = oscc;
146         request->rq_interpret_reply = osc_interpret_create;
147         ptlrpcd_add_req(request);
148
149         RETURN(0);
150 }
151
152 static int oscc_has_objects(struct osc_creator *oscc, int count)
153 {
154         int have_objs;
155         spin_lock(&oscc->oscc_lock);
156         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
157         spin_unlock(&oscc->oscc_lock);
158
159         if (!have_objs)
160                 oscc_internal_create(oscc);
161
162         return have_objs;
163 }
164
165 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
166 {
167         int have_objs;
168         int ost_full;
169         int osc_invalid;
170
171         have_objs = oscc_has_objects(oscc, count);
172
173         spin_lock(&oscc->oscc_lock);
174         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
175         spin_unlock(&oscc->oscc_lock);
176
177         osc_invalid = oscc->oscc_obd->u.cli.cl_import->imp_invalid;
178
179         return have_objs || ost_full || osc_invalid;
180 }
181
182 static int oscc_precreate(struct osc_creator *oscc, int wait)
183 {
184         struct l_wait_info lwi = { 0 };
185         int rc = 0;
186         ENTRY;
187
188         if (oscc_has_objects(oscc, oscc->oscc_kick_barrier))
189                 RETURN(0);
190
191         if (!wait)
192                 RETURN(0);
193
194         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
195         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
196
197         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
198                 rc = -ENOSPC;
199
200         if (oscc->oscc_obd->u.cli.cl_import->imp_invalid)
201                 rc = -EIO;
202
203         RETURN(rc);
204 }
205
206 int oscc_recovering(struct osc_creator *oscc) 
207 {
208         int recov = 0;
209
210         spin_lock(&oscc->oscc_lock);
211         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
212         spin_unlock(&oscc->oscc_lock);
213
214         return recov;
215 }
216
217 int osc_create(struct obd_export *exp, struct obdo *oa,
218                struct lov_stripe_md **ea, struct obd_trans_info *oti)
219 {
220         struct lov_stripe_md *lsm;
221         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
222         int try_again = 1, rc = 0;
223         ENTRY;
224         LASSERT(oa);
225         LASSERT(ea);
226         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
227         LASSERT(oa->o_gr > 0);
228
229         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
230             oa->o_flags == OBD_FL_RECREATE_OBJS) {
231                 /* Exceptional case where we are trying to repair missing
232                  * objects for various groups.  We have already validated that
233                  * this is a valid group for the file.  Don't set oscc->oscc_gr.
234                  */
235                 RETURN(osc_real_create(exp, oa, ea, oti));
236         }
237
238         LASSERT(oscc->oscc_gr == 0 || oscc->oscc_gr == oa->o_gr);
239         oscc->oscc_gr = oa->o_gr;
240
241         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
242                 RETURN(osc_real_create(exp, oa, ea, oti));
243
244         /* this is the special case where create removes orphans */
245         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
246             oa->o_flags == OBD_FL_DELORPHAN) {
247                 spin_lock(&oscc->oscc_lock);
248                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
249                         spin_unlock(&oscc->oscc_lock);
250                         return -EBUSY;
251                 }
252                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
253                         spin_unlock(&oscc->oscc_lock);
254                         return 0;
255                 }
256                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
257                 spin_unlock(&oscc->oscc_lock);
258                 CDEBUG(D_HA, "%s; oscc recovery started\n",
259                        exp->exp_obd->obd_name);
260                 LASSERT(oscc->oscc_flags & OSCC_FLAG_RECOVERING);
261
262                 /* delete from next_id on up */
263                 oa->o_valid |= OBD_MD_FLID;
264                 oa->o_id = oscc->oscc_next_id - 1;
265
266                 CDEBUG(D_HA, "%s: deleting to next_id: "LPU64"\n", 
267                        exp->exp_obd->obd_name, oa->o_id);
268
269                 rc = osc_real_create(exp, oa, ea, NULL);
270                 if (oscc->oscc_obd == NULL) {
271                         CWARN("the obd for oscc %p has been freed\n", oscc);
272                         RETURN(rc);
273                 }
274
275                 spin_lock(&oscc->oscc_lock);
276                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
277                 if (rc == 0 || rc == -ENOSPC) {
278                         if (rc == -ENOSPC)
279                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
280                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
281                         oscc->oscc_last_id = oa->o_id;
282
283                         CDEBUG(D_HA, "%s: oscc recovery finished: %d\n", 
284                                exp->exp_obd->obd_name, rc);
285                         wake_up(&oscc->oscc_waitq);
286                         
287                 } else {
288                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n", 
289                                exp->exp_obd->obd_name, rc);
290                 }
291                 spin_unlock(&oscc->oscc_lock);
292
293                 RETURN(rc);
294         }
295
296         lsm = *ea;
297         if (lsm == NULL) {
298                 rc = obd_alloc_memmd(exp, &lsm);
299                 if (rc < 0)
300                         RETURN(rc);
301         }
302
303         while (try_again) {
304                 /* If orphans are being recovered, then we must wait until 
305                    it is finished before we can continue with create. */
306                 if (oscc_recovering(oscc)) {
307                         struct l_wait_info lwi;
308                        
309                         CDEBUG(D_HA,"%p: oscc recovery in progress, waiting\n",
310                                oscc);
311                         lwi = LWI_TIMEOUT(MAX(obd_timeout*HZ/4, 1), NULL, NULL);
312                         rc = l_wait_event(oscc->oscc_waitq,
313                                           !oscc_recovering(oscc), &lwi);
314                         
315                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
316                         if (rc == -ETIMEDOUT) {
317                                 CDEBUG(D_HA,"%p: timeout waiting on recovery\n",
318                                        oscc);
319                                 RETURN(rc);
320                         }
321                         CDEBUG(D_HA, "%s: oscc recovery over, waking up\n",
322                                exp->exp_obd->obd_name);
323                 }
324                 
325                 spin_lock(&oscc->oscc_lock);
326                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
327                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
328                         oa->o_id = oscc->oscc_next_id;
329                         oa->o_gr = oscc->oscc_gr;
330                         lsm->lsm_object_id = oscc->oscc_next_id;
331                         lsm->lsm_object_gr = oscc->oscc_gr;
332                         *ea = lsm;
333                         oscc->oscc_next_id++;
334                         try_again = 0;
335                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
336                         rc = -ENOSPC;
337                         spin_unlock(&oscc->oscc_lock);
338                         break;
339                 }
340                 spin_unlock(&oscc->oscc_lock);
341                 rc = oscc_precreate(oscc, try_again);
342                 if (rc == -EIO)
343                         break;
344         }
345
346         if (rc == 0)
347                 CDEBUG(D_HA, "%s: returning objid "LPU64"\n",
348                        oscc->oscc_obd->u.cli.cl_import->imp_target_uuid.uuid,
349                        lsm->lsm_object_id);
350         else if (*ea == NULL)
351                 obd_free_memmd(exp, &lsm);
352         RETURN(rc);
353 }
354
355 void oscc_init(struct obd_device *obd)
356 {
357         struct osc_creator *oscc;
358
359         if (obd == NULL)
360                 return;
361
362         oscc = &obd->u.cli.cl_oscc;
363
364         memset(oscc, 0, sizeof(*oscc));
365         INIT_LIST_HEAD(&oscc->oscc_list);
366         init_waitqueue_head(&oscc->oscc_waitq);
367         spin_lock_init(&oscc->oscc_lock);
368         oscc->oscc_obd = obd;
369         oscc->oscc_kick_barrier = 100;
370         oscc->oscc_grow_count = 36;
371         oscc->oscc_max_grow_count = 2000;
372
373         oscc->oscc_next_id = 2;
374         oscc->oscc_last_id = 1;
375         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
376         /* XXX the export handle should give the oscc the last object */
377         /* oed->oed_oscc.oscc_last_id = exph->....; */
378 }