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