Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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/lustre_dlm.h>
40 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
41 #  include <linux/workqueue.h>
42 #  include <linux/smp_lock.h>
43 # else
44 #  include <linux/locks.h>
45 # endif
46 #else /* __KERNEL__ */
47 # include <liblustre.h>
48 #endif
49
50 #ifndef  __CYGWIN__
51 # include <linux/ctype.h>
52 # include <linux/init.h>
53 #else
54 # include <ctype.h>
55 #endif
56
57 #include <linux/obd_class.h>
58 #include "osc_internal.h"
59
60 static int osc_interpret_create(struct ptlrpc_request *req, void *data,
61                                 int rc)
62 {
63         struct osc_creator *oscc;
64         struct ost_body *body = NULL;
65         ENTRY;
66
67         if (req->rq_repmsg) {
68                 body = lustre_swab_repbuf(req, 0, sizeof(*body),
69                                           lustre_swab_ost_body);
70                 if (body == NULL && rc == 0)
71                         rc = -EPROTO;
72         }
73
74         oscc = req->rq_async_args.pointer_arg[0];
75         spin_lock(&oscc->oscc_lock);
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         } else if (rc != 0 && rc != -EIO) {
82                 DEBUG_REQ(D_ERROR, req,
83                           "unknown rc %d from async create: failing oscc",
84                           rc);
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(class_exp2cliimp(oscc->oscc_exp), 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_valid |= OBD_MD_FLID;
127         CDEBUG(D_INFO, "preallocating through id "LPU64" (last used "LPU64")\n",
128                body->oa.o_id, oscc->oscc_next_id);
129         spin_unlock(&oscc->oscc_lock);
130
131         request->rq_replen = lustre_msg_size(1, &size);
132
133         request->rq_async_args.pointer_arg[0] = oscc;
134         request->rq_interpret_reply = osc_interpret_create;
135         osc_rpcd_add_req(request);
136
137         RETURN(0);
138 }
139
140 static int oscc_has_objects(struct osc_creator *oscc, int count)
141 {
142         int have_objs;
143         spin_lock(&oscc->oscc_lock);
144         have_objs = ((__s64)(oscc->oscc_last_id - oscc->oscc_next_id) >= count);
145         spin_unlock(&oscc->oscc_lock);
146
147         if (!have_objs)
148                 oscc_internal_create(oscc);
149
150         return have_objs;
151 }
152
153 static int oscc_wait_for_objects(struct osc_creator *oscc, int count)
154 {
155         int have_objs;
156         int ost_full;
157         int osc_invalid;
158
159         have_objs = oscc_has_objects(oscc, count);
160
161         spin_lock(&oscc->oscc_lock);
162         ost_full = (oscc->oscc_flags & OSCC_FLAG_NOSPC);
163         spin_unlock(&oscc->oscc_lock);
164
165         osc_invalid = class_exp2cliimp(oscc->oscc_exp)->imp_invalid;
166
167         return have_objs || ost_full || osc_invalid;
168 }
169
170 static int oscc_precreate(struct osc_creator *oscc, int wait)
171 {
172         struct l_wait_info lwi = { 0 };
173         int rc = 0;
174         ENTRY;
175
176         if (oscc_has_objects(oscc, oscc->oscc_kick_barrier))
177                 RETURN(0);
178
179         if (!wait)
180                 RETURN(0);
181
182         /* no rc check -- a no-INTR, no-TIMEOUT wait can't fail */
183         l_wait_event(oscc->oscc_waitq, oscc_wait_for_objects(oscc, 1), &lwi);
184
185         if (!oscc_has_objects(oscc, 1) && (oscc->oscc_flags & OSCC_FLAG_NOSPC))
186                 rc = -ENOSPC;
187
188         if (class_exp2cliimp(oscc->oscc_exp)->imp_invalid)
189                 rc = -EIO;
190
191         RETURN(rc);
192 }
193
194 int osc_create(struct obd_export *exp, struct obdo *oa,
195                struct lov_stripe_md **ea, struct obd_trans_info *oti)
196 {
197         struct lov_stripe_md *lsm;
198         struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
199         int try_again = 1, rc = 0;
200         ENTRY;
201         LASSERT(oa);
202         LASSERT(ea);
203
204         if ((oa->o_valid & OBD_MD_FLGROUP) && (oa->o_gr != 0))
205                 RETURN(osc_real_create(exp, oa, ea, oti));
206
207         lsm = *ea;
208         if (lsm == NULL) {
209                 rc = obd_alloc_memmd(exp, &lsm);
210                 if (rc < 0)
211                         RETURN(rc);
212         }
213
214         /* this is the special case where create removes orphans */
215         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
216             oa->o_flags == OBD_FL_DELORPHAN) {
217                 /* delete from next_id on up */
218                 oa->o_valid |= OBD_MD_FLID;
219                 oa->o_id = oscc->oscc_next_id;
220                 if (oa->o_id == 0)
221                         RETURN(0);
222                 rc = osc_real_create(oscc->oscc_exp, oa, ea, NULL);
223
224                 spin_lock(&oscc->oscc_lock);
225                 if (rc == -ENOSPC)
226                         oscc->oscc_flags |= OSCC_FLAG_NOSPC;
227                 oscc->oscc_last_id = oscc->oscc_next_id - 1;
228                 spin_unlock(&oscc->oscc_lock);
229
230                 RETURN(rc);
231         }
232
233         while (try_again) {
234                 spin_lock(&oscc->oscc_lock);
235                 if (oscc->oscc_last_id >= oscc->oscc_next_id) {
236                         memcpy(oa, &oscc->oscc_oa, sizeof(*oa));
237                         oa->o_id = oscc->oscc_next_id;
238                         lsm->lsm_object_id = oscc->oscc_next_id;
239                         *ea = lsm;
240                         oscc->oscc_next_id++;
241                         try_again = 0;
242                 } else if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
243                         rc = -ENOSPC;
244                         spin_unlock(&oscc->oscc_lock);
245                         break;
246                 }
247                 spin_unlock(&oscc->oscc_lock);
248                 rc = oscc_precreate(oscc, try_again);
249                 if (rc == -EIO)
250                         break;
251         }
252
253         if (rc == 0)
254                 CDEBUG(D_INFO, "returning objid "LPU64"\n", lsm->lsm_object_id);
255         else if (*ea == NULL)
256                 obd_free_memmd(exp, &lsm);
257         RETURN(rc);
258 }
259
260 void oscc_init(struct obd_export *exp)
261 {
262         struct osc_export_data *oed;
263
264         if (exp == NULL)
265                 return;
266
267         oed = &exp->exp_osc_data;
268         memset(oed, 0, sizeof(*oed));
269         INIT_LIST_HEAD(&oed->oed_oscc.oscc_list);
270         init_waitqueue_head(&oed->oed_oscc.oscc_waitq);
271         spin_lock_init(&oed->oed_oscc.oscc_lock);
272         oed->oed_oscc.oscc_exp = exp;
273         oed->oed_oscc.oscc_kick_barrier = 100;
274         oed->oed_oscc.oscc_grow_count = 2000;
275         oed->oed_oscc.oscc_initial_create_count = 2000;
276
277         oed->oed_oscc.oscc_next_id = 2;
278         oed->oed_oscc.oscc_last_id = 1;
279         /* XXX the export handle should give the oscc the last object */
280         /* oed->oed_oscc.oscc_last_id = exph->....; */
281 }