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