Whamcloud - gitweb
97f486708d8506489883bfa64b3ba16599ea9ef5
[fs/lustre-release.git] / lustre / fid / fid_store.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fid/fid_store.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FID
44
45 #ifdef __KERNEL__
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
50 #endif
51
52 #include <obd.h>
53 #include <obd_class.h>
54 #include <dt_object.h>
55 #include <md_object.h>
56 #include <obd_support.h>
57 #include <lustre_req_layout.h>
58 #include <lustre_fid.h>
59 #include "fid_internal.h"
60
61 #ifdef __KERNEL__
62
63 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
64 {
65         struct lu_buf *buf;
66
67         buf = &info->sti_buf;
68         buf->lb_buf = &info->sti_space;
69         buf->lb_len = sizeof(info->sti_space);
70         return buf;
71 }
72
73 struct seq_update_callback {
74         struct dt_txn_commit_cb suc_cb;
75         struct lu_server_seq   *suc_seq;
76 };
77
78 void seq_update_cb(struct lu_env *env, struct thandle *th,
79                    struct dt_txn_commit_cb *cb, int err)
80 {
81         struct seq_update_callback *ccb;
82         ccb = container_of0(cb, struct seq_update_callback, suc_cb);
83         ccb->suc_seq->lss_need_sync = 0;
84         OBD_FREE_PTR(ccb);
85 }
86
87 struct thandle *seq_store_trans_create(struct lu_server_seq *seq,
88                                        const struct lu_env *env)
89 {
90         struct dt_device *dt_dev;
91
92         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
93         return dt_trans_create(env, dt_dev);
94 }
95
96 int seq_store_trans_start(struct lu_server_seq *seq, const struct lu_env *env,
97                           struct thandle *th)
98 {
99         struct dt_device *dt_dev;
100         ENTRY;
101
102         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
103
104         return dt_trans_start(env, dt_dev, th);
105 }
106
107 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
108 {
109         struct seq_update_callback *ccb;
110         int rc;
111         OBD_ALLOC_PTR(ccb);
112         if (ccb == NULL)
113                 return -ENOMEM;
114
115         ccb->suc_cb.dcb_func = seq_update_cb;
116         CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
117         ccb->suc_seq = seq;
118         seq->lss_need_sync = 1;
119         rc = dt_trans_cb_add(th, &ccb->suc_cb);
120         if (rc)
121                 OBD_FREE_PTR(ccb);
122         return rc;
123 }
124
125 int seq_declare_store_write(struct lu_server_seq *seq,
126                             const struct lu_env *env,
127                             struct thandle *th)
128 {
129         struct dt_object *dt_obj = seq->lss_obj;
130         int rc;
131         ENTRY;
132
133         rc = dt_obj->do_body_ops->dbo_declare_write(env, dt_obj,
134                                                     sizeof(struct lu_seq_range),
135                                                     0, th);
136         return rc;
137 }
138
139 /* This function implies that caller takes care about locking. */
140 int seq_store_write(struct lu_server_seq *seq,
141                     const struct lu_env *env,
142                     struct thandle *th)
143 {
144         struct dt_object *dt_obj = seq->lss_obj;
145         struct seq_thread_info *info;
146         loff_t pos = 0;
147         int rc;
148         ENTRY;
149
150         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
151         LASSERT(info != NULL);
152
153         /* Store ranges in le format. */
154         range_cpu_to_le(&info->sti_space, &seq->lss_space);
155
156         rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
157                                             seq_store_buf(info),
158                                             &pos, th, BYPASS_CAPA, 1);
159         if (rc == sizeof(info->sti_space)) {
160                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
161                        seq->lss_name, PRANGE(&seq->lss_space));
162                 rc = 0;
163         } else if (rc >= 0) {
164                 rc = -EIO;
165         }
166
167
168         RETURN(rc);
169 }
170
171 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
172                      struct lu_seq_range *out, int sync)
173 {
174         struct dt_device *dt_dev;
175         struct thandle *th;
176         int rc;
177         ENTRY;
178
179         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
180
181         th = seq_store_trans_create(seq, env);
182         if (IS_ERR(th))
183                 RETURN(PTR_ERR(th));
184
185         rc = seq_declare_store_write(seq, env, th);
186         if (rc)
187                 GOTO(exit, rc);
188
189         if (out != NULL) {
190                 rc = fld_declare_server_create(seq->lss_site->ms_server_fld,
191                                                env, th);
192                 if (rc)
193                         GOTO(exit, rc);
194         }
195
196         rc = seq_store_trans_start(seq, env, th);
197         if (rc)
198                 GOTO(exit, rc);
199
200         rc = seq_store_write(seq, env, th);
201         if (rc) {
202                 CERROR("%s: Can't write space data, rc %d\n",
203                        seq->lss_name, rc);
204                 GOTO(exit,rc);
205         } else if (out != NULL) {
206                 rc = fld_server_create(seq->lss_site->ms_server_fld,
207                                        env, out, th);
208                 if (rc) {
209                         CERROR("%s: Can't Update fld database, rc %d\n",
210                                seq->lss_name, rc);
211                         GOTO(exit,rc);
212                 }
213         }
214
215         /* next sequence update will need sync until this update is committed
216          * in case of sync operation this is not needed obviously */
217         if (!sync)
218                 /* if callback can't be added then sync always */
219                 sync = !!seq_update_cb_add(th, seq);
220
221         th->th_sync |= sync;
222 exit:
223         dt_trans_stop(env, dt_dev, th);
224         return rc;
225 }
226
227 /*
228  * This function implies that caller takes care about locking or locking is not
229  * needed (init time).
230  */
231 int seq_store_read(struct lu_server_seq *seq,
232                    const struct lu_env *env)
233 {
234         struct dt_object *dt_obj = seq->lss_obj;
235         struct seq_thread_info *info;
236         loff_t pos = 0;
237         int rc;
238         ENTRY;
239
240         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
241         LASSERT(info != NULL);
242
243         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
244                                            &pos, BYPASS_CAPA);
245
246         if (rc == sizeof(info->sti_space)) {
247                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
248                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
249                        seq->lss_name, PRANGE(&seq->lss_space));
250                 rc = 0;
251         } else if (rc == 0) {
252                 rc = -ENODATA;
253         } else if (rc >= 0) {
254                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
255                        rc, (int)sizeof(info->sti_space));
256                 rc = -EIO;
257         }
258
259         RETURN(rc);
260 }
261
262 int seq_store_init(struct lu_server_seq *seq,
263                    const struct lu_env *env,
264                    struct dt_device *dt)
265 {
266         struct dt_object *dt_obj;
267         struct lu_fid fid;
268         const char *name;
269         int rc;
270         ENTRY;
271
272         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
273                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
274
275         dt_obj = dt_store_open(env, dt, "", name, &fid);
276         if (!IS_ERR(dt_obj)) {
277                 seq->lss_obj = dt_obj;
278                 rc = 0;
279         } else {
280                 CERROR("%s: Can't find \"%s\" obj %d\n",
281                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
282                 rc = PTR_ERR(dt_obj);
283         }
284
285         RETURN(rc);
286 }
287
288 void seq_store_fini(struct lu_server_seq *seq,
289                     const struct lu_env *env)
290 {
291         ENTRY;
292
293         if (seq->lss_obj != NULL) {
294                 if (!IS_ERR(seq->lss_obj))
295                         lu_object_put(env, &seq->lss_obj->do_lu);
296                 seq->lss_obj = NULL;
297         }
298
299         EXIT;
300 }
301 #endif