Whamcloud - gitweb
LU-2955 tests: make replay-ost-single/8b SLOW for ZFS
[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, 2013, Intel Corporation.
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
83         ccb = container_of0(cb, struct seq_update_callback, suc_cb);
84
85         LASSERT(ccb->suc_seq != NULL);
86
87         ccb->suc_seq->lss_need_sync = 0;
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         struct dt_txn_commit_cb    *dcb;
95         int                        rc;
96
97         OBD_ALLOC_PTR(ccb);
98         if (ccb == NULL)
99                 return -ENOMEM;
100
101         ccb->suc_seq       = seq;
102         seq->lss_need_sync = 1;
103
104         dcb            = &ccb->suc_cb;
105         dcb->dcb_func  = seq_update_cb;
106         CFS_INIT_LIST_HEAD(&dcb->dcb_linkage);
107         strncpy(dcb->dcb_name, "seq_update_cb", MAX_COMMIT_CB_STR_LEN);
108         dcb->dcb_name[MAX_COMMIT_CB_STR_LEN - 1] = '\0';
109
110         rc = dt_trans_cb_add(th, dcb);
111         if (rc)
112                 OBD_FREE_PTR(ccb);
113         return rc;
114 }
115
116 /* This function implies that caller takes care about locking. */
117 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
118                      struct lu_seq_range *out, int sync)
119 {
120         struct dt_device *dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
121         struct seq_thread_info *info;
122         struct thandle *th;
123         loff_t pos = 0;
124         int rc;
125
126         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
127         LASSERT(info != NULL);
128
129         th = dt_trans_create(env, dt_dev);
130         if (IS_ERR(th))
131                 RETURN(PTR_ERR(th));
132
133         rc = dt_declare_record_write(env, seq->lss_obj,
134                                      sizeof(struct lu_seq_range), 0, th);
135         if (rc)
136                 GOTO(exit, rc);
137
138         if (out != NULL) {
139                 rc = fld_declare_server_create(env,
140                                                seq->lss_site->ss_server_fld,
141                                                out, th);
142                 if (rc)
143                         GOTO(exit, rc);
144         }
145
146         rc = dt_trans_start_local(env, dt_dev, th);
147         if (rc)
148                 GOTO(exit, rc);
149
150         /* Store ranges in le format. */
151         range_cpu_to_le(&info->sti_space, &seq->lss_space);
152
153         rc = dt_record_write(env, seq->lss_obj, seq_store_buf(info), &pos, th);
154         if (rc) {
155                 CERROR("%s: Can't write space data, rc %d\n",
156                        seq->lss_name, rc);
157                 GOTO(exit, rc);
158         } else if (out != NULL) {
159                 rc = fld_server_create(env, seq->lss_site->ss_server_fld, out,
160                                        th);
161                 if (rc) {
162                         CERROR("%s: Can't Update fld database, rc %d\n",
163                                 seq->lss_name, rc);
164                         GOTO(exit, rc);
165                 }
166         }
167         /* next sequence update will need sync until this update is committed
168          * in case of sync operation this is not needed obviously */
169         if (!sync)
170                 /* if callback can't be added then sync always */
171                 sync = !!seq_update_cb_add(th, seq);
172
173         th->th_sync |= sync;
174 exit:
175         dt_trans_stop(env, dt_dev, th);
176         return rc;
177 }
178
179 /*
180  * This function implies that caller takes care about locking or locking is not
181  * needed (init time).
182  */
183 int seq_store_read(struct lu_server_seq *seq,
184                    const struct lu_env *env)
185 {
186         struct seq_thread_info *info;
187         loff_t pos = 0;
188         int rc;
189         ENTRY;
190
191         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
192         LASSERT(info != NULL);
193
194         rc = seq->lss_obj->do_body_ops->dbo_read(env, seq->lss_obj,
195                                                  seq_store_buf(info),
196                                                  &pos, BYPASS_CAPA);
197
198         if (rc == sizeof(info->sti_space)) {
199                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
200                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
201                        seq->lss_name, PRANGE(&seq->lss_space));
202                 rc = 0;
203         } else if (rc == 0) {
204                 rc = -ENODATA;
205         } else if (rc > 0) {
206                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
207                        rc, (int)sizeof(info->sti_space));
208                 rc = -EIO;
209         }
210
211         RETURN(rc);
212 }
213
214 int seq_store_init(struct lu_server_seq *seq,
215                    const struct lu_env *env,
216                    struct dt_device *dt)
217 {
218         struct dt_object *dt_obj;
219         struct lu_fid fid;
220         struct lu_attr attr;
221         struct dt_object_format dof;
222         const char *name;
223         int rc;
224         ENTRY;
225
226         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
227                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
228
229         if (seq->lss_type == LUSTRE_SEQ_SERVER)
230                 lu_local_obj_fid(&fid, FID_SEQ_SRV_OID);
231         else
232                 lu_local_obj_fid(&fid, FID_SEQ_CTL_OID);
233
234         memset(&attr, 0, sizeof(attr));
235         attr.la_valid = LA_MODE;
236         attr.la_mode = S_IFREG | 0666;
237         dof.dof_type = DFT_REGULAR;
238
239         dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
240         if (!IS_ERR(dt_obj)) {
241                 seq->lss_obj = dt_obj;
242                 rc = 0;
243         } else {
244                 CERROR("%s: Can't find \"%s\" obj %d\n",
245                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
246                 rc = PTR_ERR(dt_obj);
247         }
248
249         RETURN(rc);
250 }
251
252 void seq_store_fini(struct lu_server_seq *seq,
253                     const struct lu_env *env)
254 {
255         ENTRY;
256
257         if (seq->lss_obj != NULL) {
258                 if (!IS_ERR(seq->lss_obj))
259                         lu_object_put(env, &seq->lss_obj->do_lu);
260                 seq->lss_obj = NULL;
261         }
262
263         EXIT;
264 }
265 #endif