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