Whamcloud - gitweb
LU-506 kernel: FC15 - fix GCC 'set-but-unused' warnings
[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 seq_update_callback {
77         struct dt_txn_commit_cb suc_cb;
78         struct lu_server_seq   *suc_seq;
79 };
80
81 void seq_update_cb(struct lu_env *env, struct thandle *th,
82                    struct dt_txn_commit_cb *cb, int err)
83 {
84         struct seq_update_callback *ccb;
85         ccb = container_of0(cb, struct seq_update_callback, suc_cb);
86         ccb->suc_seq->lss_need_sync = 0;
87         cfs_list_del(&ccb->suc_cb.dcb_linkage);
88         OBD_FREE_PTR(ccb);
89 }
90
91 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
92 {
93         struct seq_update_callback *ccb;
94         int rc;
95         OBD_ALLOC_PTR(ccb);
96         if (ccb == NULL)
97                 return -ENOMEM;
98
99         ccb->suc_cb.dcb_func = seq_update_cb;
100         CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
101         ccb->suc_seq = seq;
102         seq->lss_need_sync = 1;
103         rc = dt_trans_cb_add(th, &ccb->suc_cb);
104         if (rc)
105                 OBD_FREE_PTR(ccb);
106         return rc;
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         loff_t pos = 0;
117         int rc;
118         ENTRY;
119
120         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
121         LASSERT(info != NULL);
122
123         /* Store ranges in le format. */
124         range_cpu_to_le(&info->sti_space, &seq->lss_space);
125
126         rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
127                                             seq_store_buf(info),
128                                             &pos, th, BYPASS_CAPA, 1);
129         if (rc == sizeof(info->sti_space)) {
130                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
131                        seq->lss_name, PRANGE(&seq->lss_space));
132                 rc = 0;
133         } else if (rc >= 0) {
134                 rc = -EIO;
135         }
136
137
138         RETURN(rc);
139 }
140
141 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
142                      struct lu_seq_range *out, int sync)
143 {
144         struct seq_thread_info *info;
145         struct dt_device *dt_dev;
146         struct thandle *th;
147         int rc;
148         int credits = SEQ_TXN_STORE_CREDITS;
149         ENTRY;
150
151         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
152         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
153
154         if (out != NULL)
155                 credits += FLD_TXN_INDEX_INSERT_CREDITS;
156
157         txn_param_init(&info->sti_txn, credits);
158         th = dt_trans_start(env, dt_dev, &info->sti_txn);
159         if (IS_ERR(th))
160                 RETURN(PTR_ERR(th));
161
162         rc = seq_store_write(seq, env, th);
163         if (rc) {
164                 CERROR("%s: Can't write space data, rc %d\n",
165                        seq->lss_name, rc);
166                 GOTO(out,rc);
167         } else if (out != NULL) {
168                 rc = fld_server_create(seq->lss_site->ms_server_fld,
169                                        env, out, th);
170                 if (rc) {
171                         CERROR("%s: Can't Update fld database, rc %d\n",
172                                seq->lss_name, rc);
173                         GOTO(out,rc);
174                 }
175         }
176
177         /* next sequence update will need sync until this update is committed
178          * in case of sync operation this is not needed obviously */
179         if (!sync)
180                 /* if callback can't be added then sync always */
181                 sync = !!seq_update_cb_add(th, seq);
182
183         th->th_sync |= sync;
184 out:
185         dt_trans_stop(env, dt_dev, th);
186         return rc;
187 }
188
189 /*
190  * This function implies that caller takes care about locking or locking is not
191  * needed (init time).
192  */
193 int seq_store_read(struct lu_server_seq *seq,
194                    const struct lu_env *env)
195 {
196         struct dt_object *dt_obj = seq->lss_obj;
197         struct seq_thread_info *info;
198         loff_t pos = 0;
199         int rc;
200         ENTRY;
201
202         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
203         LASSERT(info != NULL);
204
205         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
206                                            &pos, BYPASS_CAPA);
207
208         if (rc == sizeof(info->sti_space)) {
209                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
210                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
211                        seq->lss_name, PRANGE(&seq->lss_space));
212                 rc = 0;
213         } else if (rc == 0) {
214                 rc = -ENODATA;
215         } else if (rc >= 0) {
216                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
217                        rc, (int)sizeof(info->sti_space));
218                 rc = -EIO;
219         }
220
221         RETURN(rc);
222 }
223
224 int seq_store_init(struct lu_server_seq *seq,
225                    const struct lu_env *env,
226                    struct dt_device *dt)
227 {
228         struct dt_object *dt_obj;
229         struct lu_fid fid;
230         const char *name;
231         int rc;
232         ENTRY;
233
234         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
235                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
236
237         dt_obj = dt_store_open(env, dt, "", name, &fid);
238         if (!IS_ERR(dt_obj)) {
239                 seq->lss_obj = dt_obj;
240                 rc = 0;
241         } else {
242                 CERROR("%s: Can't find \"%s\" obj %d\n",
243                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
244                 rc = PTR_ERR(dt_obj);
245         }
246
247         RETURN(rc);
248 }
249
250 void seq_store_fini(struct lu_server_seq *seq,
251                     const struct lu_env *env)
252 {
253         ENTRY;
254
255         if (seq->lss_obj != NULL) {
256                 if (!IS_ERR(seq->lss_obj))
257                         lu_object_put(env, &seq->lss_obj->do_lu);
258                 seq->lss_obj = NULL;
259         }
260
261         EXIT;
262 }
263 #endif