Whamcloud - gitweb
Branch: 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 int oscc_recovering(struct osc_creator *oscc)
60 {
61         int recov = 0;
62
63         spin_lock(&oscc->oscc_lock);
64         recov = oscc->oscc_flags & OSCC_FLAG_RECOVERING;
65         spin_unlock(&oscc->oscc_lock);
66
67         return recov;
68 }
69
70 /* this only is used now for deleting orphanes */
71 int osc_create(struct obd_export *exp, struct obdo *oa,
72                void *acl, int acl_size, struct lov_stripe_md **ea,
73                struct obd_trans_info *oti)
74 {
75         struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
76         int rc = 0, try_again = 1;
77         ENTRY;
78
79         LASSERT(oa);
80         LASSERT(ea);
81         LASSERT(oa->o_gr > 0);
82         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
83         LASSERT(acl == NULL && acl_size == 0);
84
85         if (oa->o_gr == FILTER_GROUP_LLOG || oa->o_gr == FILTER_GROUP_ECHO)
86                 RETURN(osc_real_create(exp, oa, ea, oti));
87
88         /* this is the special case where create removes orphans */
89         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
90             oa->o_flags == OBD_FL_DELORPHAN) {
91                 spin_lock(&oscc->oscc_lock);
92                 if (oscc->oscc_flags & OSCC_FLAG_SYNC_IN_PROGRESS) {
93                         spin_unlock(&oscc->oscc_lock);
94                         return -EBUSY;
95                 }
96                 if (!(oscc->oscc_flags & OSCC_FLAG_RECOVERING)) {
97                         spin_unlock(&oscc->oscc_lock);
98                         return 0;
99                 }
100                 oscc->oscc_flags |= OSCC_FLAG_SYNC_IN_PROGRESS;
101                 spin_unlock(&oscc->oscc_lock);
102                 CDEBUG(D_HA, "%s; oscc recovery started\n",
103                        exp->exp_obd->obd_name);
104                 LASSERT(oscc->oscc_flags & OSCC_FLAG_RECOVERING);
105
106                 CDEBUG(D_HA, "%s: deleting to next_id: "LPU64"\n",
107                        oscc->oscc_obd->obd_name, oa->o_id);
108
109                 rc = osc_real_create(exp, oa, ea, NULL);
110                 if (oscc->oscc_obd == NULL) {
111                         CWARN("the obd for oscc %p has been freed\n", oscc);
112                         RETURN(rc);
113                 }
114
115                 spin_lock(&oscc->oscc_lock);
116                 oscc->oscc_flags &= ~OSCC_FLAG_SYNC_IN_PROGRESS;
117                 if (rc == 0 || rc == -ENOSPC) {
118                         if (rc == -ENOSPC)
119                                 oscc->oscc_flags |= OSCC_FLAG_NOSPC;
120                         oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
121                         CDEBUG(D_HA, "%s: oscc recovery finished: %d\n",
122                                oscc->oscc_obd->obd_name, rc);
123                         wake_up(&oscc->oscc_waitq);
124                 } else {
125                         CDEBUG(D_ERROR, "%s: oscc recovery failed: %d\n",
126                                oscc->oscc_obd->obd_name, rc);
127                 }
128                 spin_unlock(&oscc->oscc_lock);
129                 RETURN(rc);
130         }
131         while (try_again) {
132                 /* If orphans are being recovered, then we must wait until
133                    it is finished before we can continue with create. */
134                 if (oscc_recovering(oscc)) {
135                         struct l_wait_info lwi;
136
137                         CDEBUG(D_HA,"%p: oscc recovery in progress, waiting\n",
138                                oscc);
139
140                         lwi = LWI_TIMEOUT(MAX(obd_timeout*HZ/4, 1), NULL, NULL);
141                         rc = l_wait_event(oscc->oscc_waitq,
142                                           !oscc_recovering(oscc), &lwi);
143                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
144                         if (rc == -ETIMEDOUT) {
145                                 CDEBUG(D_ERROR,"%p: timeout waiting on recovery\n",
146                                        oscc);
147                                 RETURN(rc);
148                         }
149                         CDEBUG(D_HA, "%p: oscc recovery over, waking up\n",
150                                oscc);
151                 }
152
153                 spin_lock(&oscc->oscc_lock);
154                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
155                         spin_unlock(&oscc->oscc_lock);
156                         break;
157                 }
158
159                 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
160                         rc = -ENOSPC;
161                         spin_unlock(&oscc->oscc_lock);
162                         break;
163                 }
164
165                 oscc->oscc_next_id++;
166                 oa->o_id = oscc->oscc_next_id;
167                 try_again = 0;
168                 spin_unlock(&oscc->oscc_lock);
169
170                 CDEBUG(D_HA, "%s: returning objid "LPU64"\n",
171                        oscc->oscc_obd->u.cli.cl_import->imp_target_uuid.uuid,
172                        oa->o_id);
173         }
174
175         while (try_again) {
176                 /* If orphans are being recovered, then we must wait until
177                    it is finished before we can continue with create. */
178                 if (oscc_recovering(oscc)) {
179                         struct l_wait_info lwi;
180
181                         CDEBUG(D_HA,"%p: oscc recovery in progress, waiting\n",
182                                oscc);
183
184                         lwi = LWI_TIMEOUT(MAX(obd_timeout*HZ/4, 1), NULL, NULL);
185                         rc = l_wait_event(oscc->oscc_waitq,
186                                           !oscc_recovering(oscc), &lwi);
187                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
188                         if (rc == -ETIMEDOUT) {
189                                 CDEBUG(D_ERROR,"%p: timeout waiting on recovery\n",
190                                        oscc);
191                                 RETURN(rc);
192                         }
193                         CDEBUG(D_HA, "%p: oscc recovery over, waking up\n",
194                                oscc);
195                 }
196
197                 spin_lock(&oscc->oscc_lock);
198                 if (oscc->oscc_flags & OSCC_FLAG_EXITING) {
199                         spin_unlock(&oscc->oscc_lock);
200                         break;
201                 }
202
203                 if (oscc->oscc_flags & OSCC_FLAG_NOSPC) {
204                         rc = -ENOSPC;
205                         spin_unlock(&oscc->oscc_lock);
206                         break;
207                 }
208
209                 oscc->oscc_next_id++;
210                 oa->o_id = oscc->oscc_next_id;
211                 try_again = 0;
212                 spin_unlock(&oscc->oscc_lock);
213
214                 CDEBUG(D_HA, "%s: returning objid "LPU64"\n",
215                        oscc->oscc_obd->u.cli.cl_import->imp_target_uuid.uuid,
216                        oa->o_id);
217         }
218
219         RETURN(rc);
220 }
221
222 void oscc_init(struct obd_device *obd)
223 {
224         struct osc_creator *oscc;
225
226         if (obd == NULL)
227                 return;
228
229         oscc = &obd->u.cli.cl_oscc;
230         memset(oscc, 0, sizeof(*oscc));
231
232         oscc->oscc_obd = obd;
233         spin_lock_init(&oscc->oscc_lock);
234         oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
235         init_waitqueue_head(&oscc->oscc_waitq);
236 }