Whamcloud - gitweb
Landing b_recovery
[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/lustre_dlm.h>
40 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
41 #  include <linux/workqueue.h>
42 #  include <linux/smp_lock.h>
43 # else
44 #  include <linux/locks.h>
45 # endif
46 #else /* __KERNEL__ */
47 # include <liblustre.h>
48 #endif
49
50 #ifndef  __CYGWIN__
51 # include <linux/ctype.h>
52 # include <linux/init.h>
53 #else
54 # include <ctype.h>
55 #endif
56
57 #include <linux/obd_class.h>
58 #include "osc_internal.h"
59
60 static int osc_interpret_create(struct ptlrpc_request *req, void *data,
61                                 int rc)
62 {
63         struct osc_creator *oscc;
64         struct ost_body *body = NULL;
65         ENTRY;
66
67         if (req->rq_repmsg) {
68                 body = lustre_swab_repbuf(req, 0, sizeof(*body),
69                                           lustre_swab_ost_body);
70                 if (body == NULL && rc == 0)
71                         rc = -EPROTO;
72         }
73
74         oscc = req->rq_async_args.pointer_arg[0];
75         spin_lock(&oscc->oscc_lock);
76         if (body)
77                 oscc->oscc_last_id = body->oa.o_id;
78         if (rc == -ENOSPC) {
79                 DEBUG_REQ(D_INODE, req, "OST out of space, flagging");
80                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
81         } else if (rc != 0 && rc != -EIO) {
82                 DEBUG_REQ(D_ERROR, req,
83                           "unknown rc %d from async create: failing oscc",
84                           rc);
85                 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
86                 ptlrpc_fail_import(req->rq_import, req->rq_import_generation);
87         }
88         oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
89         spin_unlock(&oscc->oscc_lock);
90
91         CDEBUG(D_INFO, "preallocated through id "LPU64" (last used "LPU64")\n",
92                oscc->oscc_last_id, oscc->oscc_next_id);
93
94         wake_up(&oscc->oscc_waitq);
95         RETURN(rc);
96 }
97
98 static int oscc_internal_create(struct osc_creator *oscc)
99 {
100         struct ptlrpc_request *request;
101         struct ost_body *body;
102         int size = sizeof(*body);
103         ENTRY;
104
105         spin_lock(&oscc->oscc_lock);
106         if (oscc->oscc_flags & OSCC_FLAG_CREATING) {
107                 spin_unlock(&oscc->oscc_lock);
108                 RETURN(0);
109         }
110         oscc->oscc_flags |= OSCC_FLAG_CREATING;
111         spin_unlock(&oscc->oscc_lock);
112
113         request = ptlrpc_prep_req(class_exp2cliimp(oscc->oscc_exp), OST_CREATE,
114                                   1, &size, NULL);
115         if (request == NULL) {
116                 spin_lock(&oscc->oscc_lock);
117                 oscc->oscc_flags &= ~OSCC_FLAG_CREATING;
118                 spin_unlock(&oscc->oscc_lock);
119                 RETURN(-ENOMEM);
120         }
121
122         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
123         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
124
125         spin_lock(&oscc->oscc_lock);
126         body->oa.o_id = oscc->oscc_last_id + oscc->oscc_grow_count;
127         body->oa.o_valid |= OBD_MD_FLID;
128         CDEBUG(D_INFO, "preallocating through id "LPU64" (last used "LPU64")\n",
129                body->oa.o_id, oscc->oscc_next_id);
130         spin_unlock(&oscc->oscc_lock);
131
132         request->rq_replen = lustre_msg_size(1, &size);
133
134         request->rq_async_args.pointer_arg[0] = oscc;
135         request->rq_interpret_reply = osc_interpret_create;
136         ptlrpcd_add_req(request);
137
138         RETURN(0);
139 }
140
141 static int oscc_has_objects(struct osc_creator *oscc, int count)
142 {
143         int have_objs;
144         spin_lock(&oscc->oscc_lock);
145         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
146         spin_unlock(&oscc->oscc_lock);
147
148         if (!have_objs)
149                 oscc_internal_create(oscc);
150
151         return have_objs;
152 }
153
154 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
155 {
156         int have_objs;
157         int ost_full;
158         int osc_invalid;
159
160         have_objs = oscc_has_objects(oscc, count);
161
162         spin_lock(&oscc->oscc_lock);
163         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
164         spin_unlock(&oscc->oscc_lock);
165
166         osc_invalid = class_exp2cliimp(oscc->oscc_exp)->imp_invalid;
167
168         return have_objs || ost_full || osc_invalid;
169 }
170
171 static int oscc_precreate(struct osc_creator *oscc, int wait)
172 {
173         struct l_wait_info lwi = { 0 };
174         int rc = 0;
175         ENTRY;
176
177         if (oscc_has_objects(oscc, oscc->oscc_kick_barrier))
178                 RETURN(0);
179
180         if (!wait)
181                 RETURN(0);
182
183         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
184         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
185
186         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
187                 rc = -ENOSPC;
188
189         if (class_exp2cliimp(oscc->oscc_exp)->imp_invalid)
190                 rc = -EIO;
191
192         RETURN(rc);
193 }
194
195 int oscc_recovering(struct osc_creator *oscc) 
196 {
197         int recov = 0;
198
199         spin_lock(&oscc->oscc_lock);
200         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
201         spin_unlock(&oscc->oscc_lock);
202
203         return recov;
204 }
205
206 int osc_create(struct obd_export *exp, struct obdo *oa,
207                struct lov_stripe_md **ea, struct obd_trans_info *oti)
208 {
209         struct lov_stripe_md *lsm;
210         struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
211         int try_again = 1, rc = 0;
212         ENTRY;
213         LASSERT(oa);
214         LASSERT(ea);
215
216         if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0))
217                 RETURN(osc_real_create(exp, oa, ea, oti));
218
219         lsm = *ea;
220         if (lsm == NULL) {
221                 rc = obd_alloc_memmd(exp, &lsm);
222                 if (rc < 0)
223                         RETURN(rc);
224         }
225
226         /* this is the special case where create removes orphans */
227         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
228             oa->o_flags == OBD_FL_DELORPHAN) {
229                 CDEBUG(D_HA, "%p: oscc recovery started\n", oscc);
230                 /* delete from next_id on up */
231                 oa->o_valid |= OBD_MD_FLID;
232                 oa->o_id = oscc->oscc_next_id - 1;
233
234                 rc = osc_real_create(oscc->oscc_exp, oa, ea, NULL);
235
236                 spin_lock(&oscc->oscc_lock);
237                 if (rc == -ENOSPC)
238                         oscc->oscc_flags |= OSCC_FLAG_NOSPC;
239                 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
240                 oscc->oscc_last_id = oa->o_id;
241                 wake_up(&oscc->oscc_waitq);
242                 spin_unlock(&oscc->oscc_lock);
243
244                 CDEBUG(D_HA, "%p: oscc recovery finished\n", oscc);
245
246                 RETURN(rc);
247         }
248
249         /* If orphans are being recovered, then we must wait until it is 
250            finished before we can continue with create. */
251         if (oscc_recovering(oscc)) {
252                 struct l_wait_info lwi;
253
254                 CDEBUG(D_HA, "%p: oscc recovery in progress, waiting\n", oscc);
255
256                 lwi = LWI_TIMEOUT(MAX(obd_timeout * HZ, 1), NULL, NULL);
257                 rc = l_wait_event(oscc->oscc_waitq, !oscc_recovering(oscc),
258                                   &lwi);
259                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
260                 if (rc == -ETIMEDOUT)
261                         RETURN(rc);
262                 CDEBUG(D_HA, "%p: oscc recovery over, waking up\n", oscc);
263         }
264         
265         
266         while (try_again) {
267                 spin_lock(&oscc->oscc_lock);
268                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
269                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
270                         oa->o_id = oscc->oscc_next_id;
271                         lsm->lsm_object_id = oscc->oscc_next_id;
272                         *ea = lsm;
273                         oscc->oscc_next_id++;
274                         try_again = 0;
275                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
276                         rc = -ENOSPC;
277                         spin_unlock(&oscc->oscc_lock);
278                         break;
279                 }
280                 spin_unlock(&oscc->oscc_lock);
281                 rc = oscc_precreate(oscc, try_again);
282                 if (rc == -EIO)
283                         break;
284         }
285
286         if (rc == 0)
287                 CDEBUG(D_INFO, "returning objid "LPU64"\n", lsm->lsm_object_id);
288         else if (*ea == NULL)
289                 obd_free_memmd(exp, &lsm);
290         RETURN(rc);
291 }
292
293 void oscc_init(struct obd_export *exp)
294 {
295         struct osc_export_data *oed;
296
297         if (exp == NULL)
298                 return;
299
300         oed = &exp->exp_osc_data;
301         memset(oed, 0, sizeof(*oed));
302         INIT_LIST_HEAD(&oed->oed_oscc.oscc_list);
303         init_waitqueue_head(&oed->oed_oscc.oscc_waitq);
304         spin_lock_init(&oed->oed_oscc.oscc_lock);
305         oed->oed_oscc.oscc_exp = exp;
306         oed->oed_oscc.oscc_kick_barrier = 100;
307         oed->oed_oscc.oscc_grow_count = 2000;
308         oed->oed_oscc.oscc_initial_create_count = 2000;
309
310         oed->oed_oscc.oscc_next_id = 2;
311         oed->oed_oscc.oscc_last_id = 1;
312         oed->oed_oscc.oscc_flags |= OSCC_FLAG_RECOVERING;
313         /* XXX the export handle should give the oscc the last object */
314         /* oed->oed_oscc.oscc_last_id = exph->....; */
315 }