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