Debian Codegraph @

xlators/cluster/afr/src/afr-dir-read.c

raw
1 /*
2 Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include <libgen.h>
12 #include <unistd.h>
13 #include <sys/time.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <string.h>
17
18 #include <glusterfs/dict.h>
19 #include <glusterfs/list.h>
20 #include <glusterfs/compat-errno.h>
21 #include <glusterfs/compat.h>
22 #include "afr-transaction.h"
23
24 static int32_t
25 afr_opendir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
26 int32_t op_ret, int32_t op_errno, fd_t *fd, dict_t *xdata)
27 {
28 afr_local_t *local = NULL;
29 int call_count = -1;
30 int32_t child_index = 0;
31 afr_fd_ctx_t *fd_ctx = NULL;
32
33 local = frame->local;
34 fd_ctx = local->fd_ctx;
35 child_index = (long)cookie;
36
37 local->replies[child_index].valid = 1;
38 local->replies[child_index].op_ret = op_ret;
39 local->replies[child_index].op_errno = op_errno;
40
41 LOCK(&frame->lock);
42 {
43 if (op_ret == -1) {
44 local->op_errno = op_errno;
45 fd_ctx->opened_on[child_index] = AFR_FD_NOT_OPENED;
46 } else {
47 local->op_ret = op_ret;
48 fd_ctx->opened_on[child_index] = AFR_FD_OPENED;
49 if (!local->xdata_rsp && xdata)
50 local->xdata_rsp = dict_ref(xdata);
51 }
52 call_count = --local->call_count;
53 }
54 UNLOCK(&frame->lock);
55
56 if (call_count == 0) {
57 afr_handle_replies_quorum(frame, this);
58 AFR_STACK_UNWIND(opendir, frame, local->op_ret, local->op_errno,
59 local->fd, NULL);
60 }
61
62 return 0;
63 }
64
65 int
66 afr_opendir(call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
67 dict_t *xdata)
68 {
69 afr_private_t *priv = NULL;
70 afr_local_t *local = NULL;
71 int i = 0;
72 int call_count = -1;
73 int32_t op_errno = ENOMEM;
74 afr_fd_ctx_t *fd_ctx = NULL;
75
76 priv = this->private;
77
78 local = AFR_FRAME_INIT(frame, op_errno);
79 if (!local)
80 goto out;
81
82 local->op = GF_FOP_OPENDIR;
83
84 if (priv->quorum_count && !afr_has_quorum(local->child_up, priv, NULL)) {
85 op_errno = afr_quorum_errno(priv);
86 goto out;
87 }
88
89 if (!afr_is_consistent_io_possible(local, priv, &op_errno))
90 goto out;
91
92 fd_ctx = afr_fd_ctx_get(fd, this);
93 if (!fd_ctx)
94 goto out;
95
96 loc_copy(&local->loc, loc);
97
98 local->fd = fd_ref(fd);
99 local->fd_ctx = fd_ctx;
100
101 call_count = local->call_count;
102
103 for (i = 0; i < priv->child_count; i++) {
104 if (local->child_up[i]) {
105 STACK_WIND_COOKIE(frame, afr_opendir_cbk, (void *)(long)i,
106 priv->children[i],
107 priv->children[i]->fops->opendir, loc, fd, NULL);
108
109 if (!--call_count)
110 break;
111 }
112 }
113
114 return 0;
115 out:
116 AFR_STACK_UNWIND(opendir, frame, -1, op_errno, fd, NULL);
117 return 0;
118 }
119
120 static int
121 afr_validate_read_subvol(inode_t *inode, xlator_t *this, int par_read_subvol)
122 {
123 int gen = 0;
124 int entry_read_subvol = 0;
125 unsigned char *data_readable = NULL;
126 unsigned char *metadata_readable = NULL;
127 afr_private_t *priv = NULL;
128
129 priv = this->private;
130 data_readable = alloca0(priv->child_count);
131 metadata_readable = alloca0(priv->child_count);
132
133 afr_inode_read_subvol_get(inode, this, data_readable, metadata_readable,
134 &gen);
135
136 if (gen != priv->event_generation || !data_readable[par_read_subvol] ||
137 !metadata_readable[par_read_subvol])
138 return -1;
139
140 /* Once the control reaches the following statement, it means that the
141 * parent's read subvol is perfectly readable. So calling
142 * either afr_data_subvol_get() or afr_metadata_subvol_get() would
143 * yield the same result. Hence, choosing afr_data_subvol_get() below.
144 */
145
146 if (!priv->consistent_metadata)
147 return 0;
148
149 /* For an inode fetched through readdirp which is yet to be linked,
150 * inode ctx would not be initialised (yet). So this function returns
151 * -1 above due to gen being 0, which is why it is OK to pass NULL for
152 * read_subvol_args here.
153 */
154 entry_read_subvol = afr_data_subvol_get(inode, this, NULL, NULL, NULL,
155 NULL);
156 if (entry_read_subvol != par_read_subvol)
157 return -1;
158
159 return 0;
160 }
161
162 static int32_t
163 afr_readdir_transform_entries(pid_t pid, xlator_t *this,
164 gf_dirent_t *subvol_entries, int subvol,
165 gf_dirent_t *entries, fd_t *fd)
166 {
167 int ret;
168 gf_dirent_t *entry = NULL;
169 gf_dirent_t *tmp = NULL;
170 afr_private_t *priv = NULL;
171 gf_boolean_t need_heal = _gf_false;
172 gf_boolean_t validate_subvol = _gf_false;
173 int32_t count = 0;
174 gf_boolean_t is_root_gfid = __is_root_gfid(fd->inode->gfid);
175
176 priv = this->private;
177
178 need_heal = afr_get_need_heal(priv);
179 validate_subvol = need_heal | priv->consistent_metadata;
180
181 list_for_each_entry_safe(entry, tmp, &subvol_entries->list, list)
182 {
183 /* we can skip afr_is_private_directory() check unless it's root gfid
184 * which we check once (above) for all entries
185 */
186 if (is_root_gfid) {
187 if (afr_is_private_directory(priv, entry->d_name, pid)) {
188 continue;
189 }
190 }
191
192 list_move_tail(&entry->list, &entries->list);
193 count++;
194
195 if (!validate_subvol)
196 continue;
197
198 if (entry->inode) {
199 ret = afr_validate_read_subvol(entry->inode, this, subvol);
200 if (ret != 0) {
201 inode_unref(entry->inode);
202 entry->inode = NULL;
203 continue;
204 }
205 }
206 }
207
208 return count;
209 }
210
211 static int32_t
212 afr_readdir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
213 int32_t op_ret, int32_t op_errno, gf_dirent_t *subvol_entries,
214 dict_t *xdata)
215 {
216 afr_local_t *local = NULL;
217 gf_dirent_t entries;
218
219 INIT_LIST_HEAD(&entries.list);
220
221 local = frame->local;
222
223 if (op_ret < 0 && !local->cont.readdir.offset) {
224 /* failover only if this was first readdir, detected
225 by offset == 0 */
226 local->op_ret = op_ret;
227 local->op_errno = op_errno;
228
229 afr_read_txn_continue(frame, this, (long)cookie);
230 return 0;
231 }
232
233 if (op_ret >= 0)
234 op_ret = afr_readdir_transform_entries(frame->root->pid, this,
235 subvol_entries, (long)cookie,
236 &entries, local->fd);
237
238 AFR_STACK_UNWIND(readdir, frame, op_ret, op_errno, &entries, xdata);
239
240 gf_dirent_free(&entries);
241
242 return 0;
243 }
244
245 static int
246 afr_readdir_wind(call_frame_t *frame, xlator_t *this, int subvol)
247 {
248 afr_local_t *local = NULL;
249 afr_private_t *priv = NULL;
250 afr_fd_ctx_t *fd_ctx = NULL;
251
252 priv = this->private;
253 local = frame->local;
254 fd_ctx = afr_fd_ctx_get(local->fd, this);
255 if (!fd_ctx) {
256 local->op_errno = EINVAL;
257 local->op_ret = -1;
258 }
259
260 if (subvol == -1 || !fd_ctx) {
261 AFR_STACK_UNWIND(readdir, frame, local->op_ret, local->op_errno, 0, 0);
262 return 0;
263 }
264
265 fd_ctx->readdir_subvol = subvol;
266
267 if (local->op == GF_FOP_READDIR)
268 STACK_WIND_COOKIE(frame, afr_readdir_cbk, (void *)(long)subvol,
269 priv->children[subvol],
270 priv->children[subvol]->fops->readdir, local->fd,
271 local->cont.readdir.size, local->cont.readdir.offset,
272 local->xdata_req);
273 else
274 STACK_WIND_COOKIE(frame, afr_readdir_cbk, (void *)(long)subvol,
275 priv->children[subvol],
276 priv->children[subvol]->fops->readdirp, local->fd,
277 local->cont.readdir.size, local->cont.readdir.offset,
278 local->xdata_req);
279 return 0;
280 }
281
282 static int
283 afr_do_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
284 off_t offset, int whichop, dict_t *dict)
285 {
286 afr_local_t *local = NULL;
287 int32_t op_errno = 0;
288 int subvol = -1;
289 afr_fd_ctx_t *fd_ctx = NULL;
290
291 local = AFR_FRAME_INIT(frame, op_errno);
292 if (!local)
293 goto out;
294
295 fd_ctx = afr_fd_ctx_get(fd, this);
296 if (!fd_ctx) {
297 op_errno = EINVAL;
298 goto out;
299 }
300
301 local->op = whichop;
302 local->fd = fd_ref(fd);
303 local->cont.readdir.size = size;
304 local->cont.readdir.offset = offset;
305 local->xdata_req = (dict) ? dict_ref(dict) : NULL;
306
307 subvol = fd_ctx->readdir_subvol;
308
309 if (offset == 0 || subvol == -1) {
310 /* First readdir has option of failing over and selecting
311 an appropriate read subvolume */
312 afr_read_txn(frame, this, fd->inode, afr_readdir_wind,
313 AFR_DATA_TRANSACTION);
314 } else {
315 /* But continued readdirs MUST stick to the same subvolume
316 without an option to failover */
317 afr_readdir_wind(frame, this, subvol);
318 }
319
320 return 0;
321 out:
322 AFR_STACK_UNWIND(readdir, frame, -1, op_errno, NULL, NULL);
323 return 0;
324 }
325
326 int32_t
327 afr_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
328 off_t offset, dict_t *xdata)
329 {
330 afr_do_readdir(frame, this, fd, size, offset, GF_FOP_READDIR, xdata);
331
332 return 0;
333 }
334
335 int32_t
336 afr_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
337 off_t offset, dict_t *dict)
338 {
339 afr_do_readdir(frame, this, fd, size, offset, GF_FOP_READDIRP, dict);
340
341 return 0;
342 }
343
344 int32_t
345 afr_releasedir(xlator_t *this, fd_t *fd)
346 {
347 afr_cleanup_fd_ctx(this, fd);
348
349 return 0;
350 }