Whamcloud - gitweb
LU-1267 lfsck: enhance API for MDT-OST consistency
[fs/lustre-release.git] / lustre / include / lustre_update.h
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.htm
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, Intel Corporation.
24  */
25 /*
26  * lustre/include/lustre_update.h
27  *
28  * Author: Di Wang <di.wang@intel.com>
29  */
30
31 #ifndef _LUSTRE_UPDATE_H
32 #define _LUSTRE_UPDATE_H
33
34 #define UPDATE_BUFFER_SIZE      8192
35 struct update_request {
36         struct dt_device        *ur_dt;
37         struct list_head         ur_list; /* attached itself to thandle */
38         int                      ur_flags;
39         int                      ur_rc; /* request result */
40         int                      ur_batchid; /* Current batch(trans) id */
41         struct update_buf       *ur_buf; /* Holding the update req */
42         struct list_head         ur_cb_items;
43 };
44
45 static inline unsigned long update_size(struct update *update)
46 {
47         unsigned long size;
48         int        i;
49
50         size = cfs_size_round(offsetof(struct update, u_bufs[0]));
51         for (i = 0; i < UPDATE_BUF_COUNT; i++)
52                 size += cfs_size_round(update->u_lens[i]);
53
54         return size;
55 }
56
57 static inline void *update_param_buf(struct update *update, int index,
58                                      int *size)
59 {
60         int     i;
61         void    *ptr;
62
63         if (index >= UPDATE_BUF_COUNT)
64                 return NULL;
65
66         ptr = (char *)update + cfs_size_round(offsetof(struct update,
67                                                        u_bufs[0]));
68         for (i = 0; i < index; i++) {
69                 LASSERT(update->u_lens[i] > 0);
70                 ptr += cfs_size_round(update->u_lens[i]);
71         }
72
73         if (size != NULL)
74                 *size = update->u_lens[index];
75
76         return ptr;
77 }
78
79 static inline unsigned long update_buf_size(struct update_buf *buf)
80 {
81         unsigned long size;
82         int        i = 0;
83
84         size = cfs_size_round(offsetof(struct update_buf, ub_bufs[0]));
85         for (i = 0; i < buf->ub_count; i++) {
86                 struct update *update;
87
88                 update = (struct update *)((char *)buf + size);
89                 size += update_size(update);
90         }
91         LASSERT(size <= UPDATE_BUFFER_SIZE);
92         return size;
93 }
94
95 static inline void *update_buf_get(struct update_buf *buf, int index, int *size)
96 {
97         int     count = buf->ub_count;
98         void    *ptr;
99         int     i = 0;
100
101         if (index >= count)
102                 return NULL;
103
104         ptr = (char *)buf + cfs_size_round(offsetof(struct update_buf,
105                                                     ub_bufs[0]));
106         for (i = 0; i < index; i++)
107                 ptr += update_size((struct update *)ptr);
108
109         if (size != NULL)
110                 *size = update_size((struct update *)ptr);
111
112         return ptr;
113 }
114
115 static inline void update_init_reply_buf(struct update_reply *reply, int count)
116 {
117         reply->ur_version = UPDATE_REPLY_V1;
118         reply->ur_count = count;
119 }
120
121 static inline void *update_get_buf_internal(struct update_reply *reply,
122                                             int index, int *size)
123 {
124         char *ptr;
125         int count = reply->ur_count;
126         int i;
127
128         if (index >= count)
129                 return NULL;
130
131         ptr = (char *)reply + cfs_size_round(offsetof(struct update_reply,
132                                              ur_lens[count]));
133         for (i = 0; i < index; i++) {
134                 LASSERT(reply->ur_lens[i] > 0);
135                 ptr += cfs_size_round(reply->ur_lens[i]);
136         }
137
138         if (size != NULL)
139                 *size = reply->ur_lens[index];
140
141         return ptr;
142 }
143
144 static inline void update_insert_reply(struct update_reply *reply, void *data,
145                                        int data_len, int index, int rc)
146 {
147         char *ptr;
148
149         ptr = update_get_buf_internal(reply, index, NULL);
150         LASSERT(ptr != NULL);
151
152         *(int *)ptr = cpu_to_le32(rc);
153         ptr += sizeof(int);
154         if (data_len > 0) {
155                 LASSERT(data != NULL);
156                 memcpy(ptr, data, data_len);
157         }
158         reply->ur_lens[index] = data_len + sizeof(int);
159 }
160
161 static inline int update_get_reply_buf(struct update_reply *reply,
162                                        struct lu_buf *lbuf, int index)
163 {
164         char *ptr;
165         int  size = 0;
166         int  result;
167
168         LASSERT(lbuf != NULL);
169
170         ptr = update_get_buf_internal(reply, index, &size);
171         LASSERT(ptr != NULL);
172         result = *(int *)ptr;
173
174         if (result < 0)
175                 return result;
176
177         LASSERT(size >= sizeof(int));
178
179         lbuf->lb_buf = ptr + sizeof(int);
180         lbuf->lb_len = size - sizeof(int);
181
182         return 0;
183 }
184
185 static inline int update_get_reply_result(struct update_reply *reply,
186                                           void **buf, int index)
187 {
188         void *ptr;
189         int  size;
190
191         ptr = update_get_buf_internal(reply, index, &size);
192         LASSERT(ptr != NULL && size > sizeof(int));
193         return *(int *)ptr;
194 }
195
196 static inline void update_inc_batchid(struct update_request *update)
197 {
198         update->ur_batchid++;
199 }
200
201 #endif