Whamcloud - gitweb
LU-1187 mdt: unlink remote directory
[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         cfs_list_t              ur_list;    /* attached itself to thandle */
38         int                     ur_flags;
39         int                     ur_rc;
40         struct update_buf       *ur_buf;   /* Holding the update req */
41 };
42
43 static inline unsigned long update_size(struct update *update)
44 {
45         unsigned long size;
46         int        i;
47
48         size = cfs_size_round(offsetof(struct update, u_bufs[0]));
49         for (i = 0; i < UPDATE_BUF_COUNT; i++)
50                 size += cfs_size_round(update->u_lens[i]);
51
52         return size;
53 }
54
55 static inline void *update_param_buf(struct update *update, int index,
56                                      int *size)
57 {
58         int     i;
59         void    *ptr;
60
61         if (index >= UPDATE_BUF_COUNT)
62                 return NULL;
63
64         ptr = (char *)update + cfs_size_round(offsetof(struct update,
65                                                        u_bufs[0]));
66         for (i = 0; i < index; i++) {
67                 LASSERT(update->u_lens[i] > 0);
68                 ptr += cfs_size_round(update->u_lens[i]);
69         }
70
71         if (size != NULL)
72                 *size = update->u_lens[index];
73
74         return ptr;
75 }
76
77 static inline unsigned long update_buf_size(struct update_buf *buf)
78 {
79         unsigned long size;
80         int        i = 0;
81
82         size = cfs_size_round(offsetof(struct update_buf, ub_bufs[0]));
83         for (i = 0; i < buf->ub_count; i++) {
84                 struct update *update;
85
86                 update = (struct update *)((char *)buf + size);
87                 size += update_size(update);
88         }
89         LASSERT(size <= UPDATE_BUFFER_SIZE);
90         return size;
91 }
92
93 static inline void *update_buf_get(struct update_buf *buf, int index, int *size)
94 {
95         int     count = buf->ub_count;
96         void    *ptr;
97         int     i = 0;
98
99         if (index >= count)
100                 return NULL;
101
102         ptr = (char *)buf + cfs_size_round(offsetof(struct update_buf,
103                                                     ub_bufs[0]));
104         for (i = 0; i < index; i++)
105                 ptr += update_size((struct update *)ptr);
106
107         if (size != NULL)
108                 *size = update_size((struct update *)ptr);
109
110         return ptr;
111 }
112
113 static inline void update_init_reply_buf(struct update_reply *reply, int count)
114 {
115         reply->ur_version = UPDATE_REPLY_V1;
116         reply->ur_count = count;
117 }
118
119 static inline void *update_get_buf_internal(struct update_reply *reply,
120                                             int index, int *size)
121 {
122         char *ptr;
123         int count = reply->ur_count;
124         int i;
125
126         if (index >= count)
127                 return NULL;
128
129         ptr = (char *)reply + cfs_size_round(offsetof(struct update_reply,
130                                              ur_lens[count]));
131         for (i = 0; i < index; i++) {
132                 LASSERT(reply->ur_lens[i] > 0);
133                 ptr += cfs_size_round(reply->ur_lens[i]);
134         }
135
136         if (size != NULL)
137                 *size = reply->ur_lens[index];
138
139         return ptr;
140 }
141
142 static inline void update_insert_reply(struct update_reply *reply, void *data,
143                                        int data_len, int index, int rc)
144 {
145         char *ptr;
146
147         ptr = update_get_buf_internal(reply, index, NULL);
148         LASSERT(ptr != NULL);
149
150         *(int *)ptr = cpu_to_le32(rc);
151         ptr += sizeof(int);
152         if (data_len > 0) {
153                 LASSERT(data != NULL);
154                 memcpy(ptr, data, data_len);
155         }
156         reply->ur_lens[index] = data_len + sizeof(int);
157 }
158
159 static inline int update_get_reply_buf(struct update_reply *reply, void **buf,
160                                        int index)
161 {
162         char *ptr;
163         int  size = 0;
164         int  result;
165
166         ptr = update_get_buf_internal(reply, index, &size);
167         result = *(int *)ptr;
168
169         if (result < 0)
170                 return result;
171
172         LASSERT((ptr != NULL && size >= sizeof(int)));
173         *buf = ptr + sizeof(int);
174         return size - sizeof(int);
175 }
176
177 static inline int update_get_reply_result(struct update_reply *reply,
178                                           void **buf, int index)
179 {
180         void *ptr;
181         int  size;
182
183         ptr = update_get_buf_internal(reply, index, &size);
184         LASSERT(ptr != NULL && size > sizeof(int));
185         return *(int *)ptr;
186 }
187
188 #endif
189
190