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