Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[fs/lustre-release.git] / lustre / fid / fid_store.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_FID
47
48 #ifdef __KERNEL__
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <dt_object.h>
58 #include <md_object.h>
59 #include <obd_support.h>
60 #include <lustre_req_layout.h>
61 #include <lustre_fid.h>
62 #include "fid_internal.h"
63
64 #ifdef __KERNEL__
65
66 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
67 {
68         struct lu_buf *buf;
69
70         buf = &info->sti_buf;
71         buf->lb_buf = &info->sti_space;
72         buf->lb_len = sizeof(info->sti_space);
73         return buf;
74 }
75
76 struct thandle *seq_store_trans_start(struct lu_server_seq *seq,
77                                       const struct lu_env *env, int credit,
78                                       int sync)
79 {
80         struct seq_thread_info *info;
81         struct dt_device *dt_dev;
82         struct thandle *th;
83         ENTRY;
84
85         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
86         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
87         LASSERT(info != NULL);
88
89         txn_param_init(&info->sti_txn, credit);
90         if (sync)
91                 txn_param_sync(&info->sti_txn);
92
93         th = dt_dev->dd_ops->dt_trans_start(env, dt_dev, &info->sti_txn);
94         return th;
95 }
96
97 void seq_store_trans_stop(struct lu_server_seq *seq,
98                           const struct lu_env *env,
99                           struct thandle *th)
100 {
101         struct dt_device *dt_dev;
102         ENTRY;
103
104         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
105
106         dt_dev->dd_ops->dt_trans_stop(env, th);
107 }
108
109 /* This function implies that caller takes care about locking. */
110 int seq_store_write(struct lu_server_seq *seq,
111                     const struct lu_env *env,
112                     struct thandle *th)
113 {
114         struct dt_object *dt_obj = seq->lss_obj;
115         struct seq_thread_info *info;
116         struct dt_device *dt_dev;
117         loff_t pos = 0;
118         int rc;
119         ENTRY;
120
121         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
122         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
123         LASSERT(info != NULL);
124
125         /* Store ranges in le format. */
126         range_cpu_to_le(&info->sti_space, &seq->lss_space);
127
128         rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
129                                             seq_store_buf(info),
130                                             &pos, th, BYPASS_CAPA, 1);
131         if (rc == sizeof(info->sti_space)) {
132                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
133                        seq->lss_name, PRANGE(&seq->lss_space));
134                 rc = 0;
135         } else if (rc >= 0) {
136                 rc = -EIO;
137         }
138
139
140         RETURN(rc);
141 }
142
143 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
144                      struct lu_seq_range *out, int sync)
145 {
146         struct thandle *th;
147         int rc;
148         int credits = SEQ_TXN_STORE_CREDITS;
149
150         if (out != NULL)
151                 credits += FLD_TXN_INDEX_INSERT_CREDITS;
152
153         th = seq_store_trans_start(seq, env, credits, sync);
154         if (IS_ERR(th))
155                 RETURN(PTR_ERR(th));
156
157         rc = seq_store_write(seq, env, th);
158         if (rc) {
159                 CERROR("%s: Can't write space data, rc %d\n",
160                        seq->lss_name, rc);
161         } else if (out != NULL) {
162                 rc = fld_server_create(seq->lss_site->ms_server_fld,
163                                        env, out, th);
164                 if (rc) {
165                         CERROR("%s: Can't Update fld database, rc %d\n",
166                                seq->lss_name, rc);
167                 }
168         }
169
170         seq_store_trans_stop(seq, env, th);
171         return rc;
172 }
173
174 /*
175  * This function implies that caller takes care about locking or locking is not
176  * needed (init time).
177  */
178 int seq_store_read(struct lu_server_seq *seq,
179                    const struct lu_env *env)
180 {
181         struct dt_object *dt_obj = seq->lss_obj;
182         struct seq_thread_info *info;
183         loff_t pos = 0;
184         int rc;
185         ENTRY;
186
187         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
188         LASSERT(info != NULL);
189
190         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
191                                            &pos, BYPASS_CAPA);
192
193         if (rc == sizeof(info->sti_space)) {
194                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
195                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
196                        seq->lss_name, PRANGE(&seq->lss_space));
197                 rc = 0;
198         } else if (rc == 0) {
199                 rc = -ENODATA;
200         } else if (rc >= 0) {
201                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
202                        rc, (int)sizeof(info->sti_space));
203                 rc = -EIO;
204         }
205
206         RETURN(rc);
207 }
208
209 int seq_store_init(struct lu_server_seq *seq,
210                    const struct lu_env *env,
211                    struct dt_device *dt)
212 {
213         struct dt_object *dt_obj;
214         struct lu_fid fid;
215         const char *name;
216         int rc;
217         ENTRY;
218
219         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
220                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
221
222         dt_obj = dt_store_open(env, dt, "", name, &fid);
223         if (!IS_ERR(dt_obj)) {
224                 seq->lss_obj = dt_obj;
225                 rc = 0;
226         } else {
227                 CERROR("%s: Can't find \"%s\" obj %d\n",
228                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
229                 rc = PTR_ERR(dt_obj);
230         }
231
232         RETURN(rc);
233 }
234
235 void seq_store_fini(struct lu_server_seq *seq,
236                     const struct lu_env *env)
237 {
238         ENTRY;
239
240         if (seq->lss_obj != NULL) {
241                 if (!IS_ERR(seq->lss_obj))
242                         lu_object_put(env, &seq->lss_obj->do_lu);
243                 seq->lss_obj = NULL;
244         }
245
246         EXIT;
247 }
248 #endif