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