Whamcloud - gitweb
LU-6068 misc: update Intel copyright messages 2014
[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, 2014, 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 #include <libcfs/libcfs.h>
46 #include <dt_object.h>
47 #include <obd_support.h>
48 #include <lustre_capa.h>
49 #include <lustre_fid.h>
50 #include <lustre_fld.h>
51 #include "fid_internal.h"
52
53 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
54 {
55         struct lu_buf *buf;
56
57         buf = &info->sti_buf;
58         buf->lb_buf = &info->sti_space;
59         buf->lb_len = sizeof(info->sti_space);
60         return buf;
61 }
62
63 struct seq_update_callback {
64         struct dt_txn_commit_cb suc_cb;
65         struct lu_server_seq   *suc_seq;
66 };
67
68 void seq_update_cb(struct lu_env *env, struct thandle *th,
69                    struct dt_txn_commit_cb *cb, int err)
70 {
71         struct seq_update_callback *ccb;
72
73         ccb = container_of0(cb, struct seq_update_callback, suc_cb);
74
75         LASSERT(ccb->suc_seq != NULL);
76
77         ccb->suc_seq->lss_need_sync = 0;
78         OBD_FREE_PTR(ccb);
79 }
80
81 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
82 {
83         struct seq_update_callback *ccb;
84         struct dt_txn_commit_cb    *dcb;
85         int                        rc;
86
87         OBD_ALLOC_PTR(ccb);
88         if (ccb == NULL)
89                 return -ENOMEM;
90
91         ccb->suc_seq       = seq;
92         seq->lss_need_sync = 1;
93
94         dcb            = &ccb->suc_cb;
95         dcb->dcb_func  = seq_update_cb;
96         INIT_LIST_HEAD(&dcb->dcb_linkage);
97         strlcpy(dcb->dcb_name, "seq_update_cb", sizeof(dcb->dcb_name));
98
99         rc = dt_trans_cb_add(th, dcb);
100         if (rc)
101                 OBD_FREE_PTR(ccb);
102         return rc;
103 }
104
105 /* This function implies that caller takes care about locking. */
106 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
107                      struct lu_seq_range *out, int sync)
108 {
109         struct dt_device *dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
110         struct seq_thread_info *info;
111         struct thandle *th;
112         loff_t pos = 0;
113         int rc;
114
115         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
116         LASSERT(info != NULL);
117
118         th = dt_trans_create(env, dt_dev);
119         if (IS_ERR(th))
120                 RETURN(PTR_ERR(th));
121
122         /* Store ranges in le format. */
123         range_cpu_to_le(&info->sti_space, &seq->lss_space);
124
125         rc = dt_declare_record_write(env, seq->lss_obj,
126                                      seq_store_buf(info), 0, th);
127         if (rc)
128                 GOTO(exit, rc);
129
130         if (out != NULL) {
131                 rc = fld_declare_server_create(env,
132                                                seq->lss_site->ss_server_fld,
133                                                out, th);
134                 if (rc)
135                         GOTO(exit, rc);
136         }
137
138         rc = dt_trans_start_local(env, dt_dev, th);
139         if (rc)
140                 GOTO(exit, rc);
141
142         rc = dt_record_write(env, seq->lss_obj, seq_store_buf(info), &pos, th);
143         if (rc) {
144                 CERROR("%s: Can't write space data, rc %d\n",
145                        seq->lss_name, rc);
146                 GOTO(exit, rc);
147         } else if (out != NULL) {
148                 rc = fld_server_create(env, seq->lss_site->ss_server_fld, out,
149                                        th);
150                 if (rc) {
151                         CERROR("%s: Can't Update fld database, rc %d\n",
152                                 seq->lss_name, rc);
153                         GOTO(exit, rc);
154                 }
155         }
156         /* next sequence update will need sync until this update is committed
157          * in case of sync operation this is not needed obviously */
158         if (!sync)
159                 /* if callback can't be added then sync always */
160                 sync = !!seq_update_cb_add(th, seq);
161
162         th->th_sync |= sync;
163 exit:
164         dt_trans_stop(env, dt_dev, th);
165         return rc;
166 }
167
168 /*
169  * This function implies that caller takes care about locking or locking is not
170  * needed (init time).
171  */
172 int seq_store_read(struct lu_server_seq *seq,
173                    const struct lu_env *env)
174 {
175         struct seq_thread_info *info;
176         loff_t pos = 0;
177         int rc;
178         ENTRY;
179
180         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
181         LASSERT(info != NULL);
182
183         rc = seq->lss_obj->do_body_ops->dbo_read(env, seq->lss_obj,
184                                                  seq_store_buf(info),
185                                                  &pos, BYPASS_CAPA);
186
187         if (rc == sizeof(info->sti_space)) {
188                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
189                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
190                        seq->lss_name, PRANGE(&seq->lss_space));
191                 rc = 0;
192         } else if (rc == 0) {
193                 rc = -ENODATA;
194         } else if (rc > 0) {
195                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
196                        rc, (int)sizeof(info->sti_space));
197                 rc = -EIO;
198         }
199
200         RETURN(rc);
201 }
202
203 int seq_store_init(struct lu_server_seq *seq,
204                    const struct lu_env *env,
205                    struct dt_device *dt)
206 {
207         struct dt_object *dt_obj;
208         struct lu_fid fid;
209         struct lu_attr attr;
210         struct dt_object_format dof;
211         const char *name;
212         int rc;
213         ENTRY;
214
215         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
216                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
217
218         if (seq->lss_type == LUSTRE_SEQ_SERVER)
219                 lu_local_obj_fid(&fid, FID_SEQ_SRV_OID);
220         else
221                 lu_local_obj_fid(&fid, FID_SEQ_CTL_OID);
222
223         memset(&attr, 0, sizeof(attr));
224         attr.la_valid = LA_MODE;
225         attr.la_mode = S_IFREG | 0666;
226         dof.dof_type = DFT_REGULAR;
227
228         dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
229         if (!IS_ERR(dt_obj)) {
230                 seq->lss_obj = dt_obj;
231                 rc = 0;
232         } else {
233                 CERROR("%s: Can't find \"%s\" obj %d\n",
234                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
235                 rc = PTR_ERR(dt_obj);
236         }
237
238         RETURN(rc);
239 }
240
241 void seq_store_fini(struct lu_server_seq *seq,
242                     const struct lu_env *env)
243 {
244         ENTRY;
245
246         if (seq->lss_obj != NULL) {
247                 if (!IS_ERR(seq->lss_obj))
248                         lu_object_put(env, &seq->lss_obj->do_lu);
249                 seq->lss_obj = NULL;
250         }
251
252         EXIT;
253 }