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