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