Whamcloud - gitweb
b=16098
[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  2008 Sun Microsystems, Inc. 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 enum {
66         SEQ_TXN_STORE_CREDITS = 20
67 };
68
69 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
70 {
71         struct lu_buf *buf;
72
73         buf = &info->sti_buf;
74         buf->lb_buf = &info->sti_space;
75         buf->lb_len = sizeof(info->sti_space);
76         return buf;
77 }
78
79 /* This function implies that caller takes care about locking. */
80 int seq_store_write(struct lu_server_seq *seq,
81                     const struct lu_env *env)
82 {
83         struct dt_object *dt_obj = seq->lss_obj;
84         struct seq_thread_info *info;
85         struct dt_device *dt_dev;
86         struct thandle *th;
87         loff_t pos = 0;
88         int rc;
89         ENTRY;
90
91         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
92         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
93         LASSERT(info != NULL);
94
95         /* Stub here, will fix it later. */
96         txn_param_init(&info->sti_txn, SEQ_TXN_STORE_CREDITS);
97
98         th = dt_dev->dd_ops->dt_trans_start(env, dt_dev, &info->sti_txn);
99         if (!IS_ERR(th)) {
100                 /* Store ranges in le format. */
101                 range_cpu_to_le(&info->sti_space, &seq->lss_space);
102
103                 rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
104                                                     seq_store_buf(info),
105                                                     &pos, th, BYPASS_CAPA);
106                 if (rc == sizeof(info->sti_space)) {
107                         CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
108                                seq->lss_name, PRANGE(&seq->lss_space));
109                         rc = 0;
110                 } else if (rc >= 0) {
111                         rc = -EIO;
112                 }
113
114                 dt_dev->dd_ops->dt_trans_stop(env, th);
115         } else {
116                 rc = PTR_ERR(th);
117         }
118         
119         RETURN(rc);
120 }
121
122 /*
123  * This function implies that caller takes care about locking or locking is not
124  * needed (init time).
125  */
126 int seq_store_read(struct lu_server_seq *seq,
127                    const struct lu_env *env)
128 {
129         struct dt_object *dt_obj = seq->lss_obj;
130         struct seq_thread_info *info;
131         loff_t pos = 0;
132         int rc;
133         ENTRY;
134
135         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
136         LASSERT(info != NULL);
137
138         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
139                                            &pos, BYPASS_CAPA);
140
141         if (rc == sizeof(info->sti_space)) {
142                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
143                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
144                        seq->lss_name, PRANGE(&seq->lss_space));
145                 rc = 0;
146         } else if (rc == 0) {
147                 rc = -ENODATA;
148         } else if (rc >= 0) {
149                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
150                        rc, (int)sizeof(info->sti_space));
151                 rc = -EIO;
152         }
153
154         RETURN(rc);
155 }
156
157 int seq_store_init(struct lu_server_seq *seq,
158                    const struct lu_env *env,
159                    struct dt_device *dt)
160 {
161         struct dt_object *dt_obj;
162         struct lu_fid fid;
163         const char *name;
164         int rc;
165         ENTRY;
166
167         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
168                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
169
170         dt_obj = dt_store_open(env, dt, name, &fid);
171         if (!IS_ERR(dt_obj)) {
172                 seq->lss_obj = dt_obj;
173                 rc = 0;
174         } else {
175                 CERROR("%s: Can't find \"%s\" obj %d\n",
176                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
177                 rc = PTR_ERR(dt_obj);
178         }
179
180         RETURN(rc);
181 }
182
183 void seq_store_fini(struct lu_server_seq *seq,
184                     const struct lu_env *env)
185 {
186         ENTRY;
187
188         if (seq->lss_obj != NULL) {
189                 if (!IS_ERR(seq->lss_obj))
190                         lu_object_put(env, &seq->lss_obj->do_lu);
191                 seq->lss_obj = NULL;
192         }
193
194         EXIT;
195 }
196 #endif