Debian Codegraph @

xlators/cluster/afr/src/afr-open.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 <unistd.h>
12 #include <sys/time.h>
13 #include <stdlib.h>
14 #include <signal.h>
15
16 #include <glusterfs/dict.h>
17 #include <glusterfs/logging.h>
18 #include <glusterfs/compat-errno.h>
19 #include <glusterfs/compat.h>
20 #include <glusterfs/statedump.h>
21
22 #include "afr-transaction.h"
23 #include "afr-self-heal.h"
24 #include "protocol-common.h"
25
26 static gf_boolean_t
27 afr_is_fd_fixable(fd_t *fd)
28 {
29 if (!fd || !fd->inode)
30 return _gf_false;
31 else if (fd_is_anonymous(fd))
32 return _gf_false;
33 else if (gf_uuid_is_null(fd->inode->gfid))
34 return _gf_false;
35
36 return _gf_true;
37 }
38
39 static int
40 afr_open_ftruncate_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
41 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
42 struct iatt *postbuf, dict_t *xdata)
43 {
44 afr_local_t *local = frame->local;
45
46 AFR_STACK_UNWIND(open, frame, local->op_ret, local->op_errno,
47 local->cont.open.fd, xdata);
48 return 0;
49 }
50
51 static int
52 afr_open_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
53 int32_t op_errno, fd_t *fd, dict_t *xdata)
54 {
55 afr_local_t *local = NULL;
56 int call_count = -1;
57 int child_index = (long)cookie;
58 afr_fd_ctx_t *fd_ctx = NULL;
59
60 local = frame->local;
61 fd_ctx = local->fd_ctx;
62
63 local->replies[child_index].valid = 1;
64 local->replies[child_index].op_ret = op_ret;
65 local->replies[child_index].op_errno = op_errno;
66
67 LOCK(&frame->lock);
68 {
69 if (op_ret == -1) {
70 local->op_errno = op_errno;
71 fd_ctx->opened_on[child_index] = AFR_FD_NOT_OPENED;
72 } else {
73 local->op_ret = op_ret;
74 fd_ctx->opened_on[child_index] = AFR_FD_OPENED;
75 if (!local->xdata_rsp && xdata)
76 local->xdata_rsp = dict_ref(xdata);
77 }
78 call_count = --local->call_count;
79 }
80 UNLOCK(&frame->lock);
81
82 if (call_count == 0) {
83 afr_handle_replies_quorum(frame, this);
84 if (local->op_ret == -1) {
85 AFR_STACK_UNWIND(open, frame, local->op_ret, local->op_errno, NULL,
86 NULL);
87 } else if (fd_ctx->flags & O_TRUNC) {
88 STACK_WIND(frame, afr_open_ftruncate_cbk, this,
89 this->fops->ftruncate, fd, 0, NULL);
90 } else {
91 AFR_STACK_UNWIND(open, frame, local->op_ret, local->op_errno,
92 local->cont.open.fd, local->xdata_rsp);
93 }
94 }
95
96 return 0;
97 }
98
99 static int
100 afr_open_continue(call_frame_t *frame, xlator_t *this, int err)
101 {
102 afr_local_t *local = NULL;
103 afr_private_t *priv = NULL;
104 int call_count = 0;
105 int i = 0;
106
107 local = frame->local;
108 priv = this->private;
109
110 if (err) {
111 AFR_STACK_UNWIND(open, frame, -1, err, NULL, NULL);
112 } else {
113 local->call_count = AFR_COUNT(local->child_up, priv->child_count);
114 call_count = local->call_count;
115
116 for (i = 0; i < priv->child_count; i++) {
117 if (local->child_up[i]) {
118 STACK_WIND_COOKIE(frame, afr_open_cbk, (void *)(long)i,
119 priv->children[i],
120 priv->children[i]->fops->open, &local->loc,
121 (local->cont.open.flags & ~O_TRUNC),
122 local->cont.open.fd, local->xdata_req);
123 if (!--call_count)
124 break;
125 }
126 }
127 }
128 return 0;
129 }
130
131 int
132 afr_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
133 fd_t *fd, dict_t *xdata)
134 {
135 afr_private_t *priv = NULL;
136 afr_local_t *local = NULL;
137 int spb_subvol = 0;
138 int event_generation = 0;
139 int ret = 0;
140 int32_t op_errno = 0;
141 afr_fd_ctx_t *fd_ctx = NULL;
142
143 // We can't let truncation to happen outside transaction.
144
145 priv = this->private;
146
147 local = AFR_FRAME_INIT(frame, op_errno);
148 if (!local)
149 goto out;
150
151 local->op = GF_FOP_OPEN;
152 fd_ctx = afr_fd_ctx_get(fd, this);
153 if (!fd_ctx) {
154 op_errno = ENOMEM;
155 goto out;
156 }
157
158 if (priv->quorum_count && !afr_has_quorum(local->child_up, priv, NULL)) {
159 op_errno = afr_quorum_errno(priv);
160 goto out;
161 }
162
163 if (!afr_is_consistent_io_possible(local, priv, &op_errno))
164 goto out;
165
166 local->inode = inode_ref(loc->inode);
167 loc_copy(&local->loc, loc);
168 local->fd_ctx = fd_ctx;
169 fd_ctx->flags = flags;
170 if (xdata)
171 local->xdata_req = dict_ref(xdata);
172
173 local->cont.open.flags = flags;
174 local->cont.open.fd = fd_ref(fd);
175
176 ret = afr_inode_get_readable(frame, local->inode, this, NULL,
177 &event_generation, AFR_DATA_TRANSACTION);
178 if ((ret < 0) &&
179 (afr_split_brain_read_subvol_get(local->inode, this, NULL,
180 &spb_subvol) == 0) &&
181 spb_subvol < 0) {
182 afr_inode_refresh(frame, this, local->inode, local->inode->gfid,
183 afr_open_continue);
184 } else {
185 afr_open_continue(frame, this, 0);
186 }
187
188 return 0;
189 out:
190 AFR_STACK_UNWIND(open, frame, -1, op_errno, fd, NULL);
191
192 return 0;
193 }
194
195 static int
196 afr_openfd_fix_open_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
197 int32_t op_ret, int32_t op_errno, fd_t *fd,
198 dict_t *xdata)
199 {
200 afr_local_t *local = NULL;
201 afr_private_t *priv = NULL;
202 afr_fd_ctx_t *fd_ctx = NULL;
203 int call_count = 0;
204 int child_index = (long)cookie;
205
206 priv = this->private;
207 local = frame->local;
208
209 if (op_ret >= 0) {
210 gf_msg_debug(this->name, 0,
211 "fd for %s opened "
212 "successfully on subvolume %s",
213 local->loc.path, priv->children[child_index]->name);
214 } else {
215 gf_smsg(this->name, fop_log_level(GF_FOP_OPEN, op_errno), op_errno,
216 AFR_MSG_OPEN_FAIL, "path=%s", local->loc.path, "subvolume=%s",
217 priv->children[child_index]->name, NULL);
218 }
219
220 fd_ctx = local->fd_ctx;
221
222 LOCK(&local->fd->lock);
223 {
224 if (op_ret >= 0) {
225 fd_ctx->opened_on[child_index] = AFR_FD_OPENED;
226 } else {
227 fd_ctx->opened_on[child_index] = AFR_FD_NOT_OPENED;
228 }
229 }
230 UNLOCK(&local->fd->lock);
231
232 call_count = afr_frame_return(frame);
233 if (call_count == 0)
234 AFR_STACK_DESTROY(frame);
235 return 0;
236 }
237
238 static void
239 afr_fd_ctx_reset_need_open(fd_t *fd, xlator_t *this, unsigned char *need_open)
240 {
241 afr_fd_ctx_t *fd_ctx = NULL;
242 afr_private_t *priv = NULL;
243 int i = 0;
244
245 priv = this->private;
246 fd_ctx = afr_fd_ctx_get(fd, this);
247 if (!fd_ctx)
248 return;
249
250 LOCK(&fd->lock);
251 {
252 for (i = 0; i < priv->child_count; i++) {
253 if (fd_ctx->opened_on[i] == AFR_FD_OPENING && need_open[i]) {
254 fd_ctx->opened_on[i] = AFR_FD_NOT_OPENED;
255 need_open[i] = 0;
256 }
257 }
258 }
259 UNLOCK(&fd->lock);
260 }
261
262 static int
263 afr_fd_ctx_set_need_open(fd_t *fd, xlator_t *this, unsigned char *need_open)
264 {
265 afr_fd_ctx_t *fd_ctx = NULL;
266 afr_private_t *priv = NULL;
267 int i = 0;
268 int count = 0;
269
270 priv = this->private;
271 fd_ctx = afr_fd_ctx_get(fd, this);
272 if (!fd_ctx)
273 return 0;
274
275 LOCK(&fd->lock);
276 {
277 for (i = 0; i < priv->child_count; i++) {
278 if (fd_ctx->opened_on[i] == AFR_FD_NOT_OPENED &&
279 priv->child_up[i]) {
280 fd_ctx->opened_on[i] = AFR_FD_OPENING;
281 need_open[i] = 1;
282 count++;
283 } else {
284 need_open[i] = 0;
285 }
286 }
287 }
288 UNLOCK(&fd->lock);
289
290 return count;
291 }
292
293 static int
294 afr_do_fix_open(call_frame_t *frame, xlator_t *this)
295 {
296 afr_local_t *local = frame->local;
297 afr_private_t *priv = NULL;
298 int i = 0;
299 int need_open_count = 0;
300
301 priv = this->private;
302
303 need_open_count = AFR_COUNT(local->need_open, priv->child_count);
304 if (!need_open_count) {
305 goto out;
306 }
307 gf_msg_debug(this->name, 0, "need open count: %d", need_open_count);
308 local->call_count = need_open_count;
309
310 for (i = 0; i < priv->child_count; i++) {
311 if (!local->need_open[i])
312 continue;
313
314 if (IA_IFDIR == local->fd->inode->ia_type) {
315 gf_msg_debug(this->name, 0, "opening fd for dir %s on subvolume %s",
316 local->loc.path, priv->children[i]->name);
317 STACK_WIND_COOKIE(frame, afr_openfd_fix_open_cbk, (void *)(long)i,
318 priv->children[i],
319 priv->children[i]->fops->opendir, &local->loc,
320 local->fd, NULL);
321 } else {
322 gf_msg_debug(this->name, 0,
323 "opening fd for file %s on subvolume %s",
324 local->loc.path, priv->children[i]->name);
325
326 STACK_WIND_COOKIE(
327 frame, afr_openfd_fix_open_cbk, (void *)(long)i,
328 priv->children[i], priv->children[i]->fops->open, &local->loc,
329 local->fd_ctx->flags & ~(O_CREAT | O_EXCL | O_TRUNC), local->fd,
330 NULL);
331 }
332 if (!--need_open_count)
333 break;
334 }
335 return 0;
336
337 out:
338 afr_fd_ctx_reset_need_open(local->fd, this, local->need_open);
339 AFR_STACK_DESTROY(frame);
340 return 0;
341 }
342
343 static int
344 afr_is_reopen_allowed_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
345 int32_t op_ret, int32_t op_errno,
346 struct gf_flock *lock, dict_t *xdata)
347 {
348 afr_local_t *local = frame->local;
349 afr_private_t *priv = NULL;
350 int ret = -1;
351 int call_count = 0;
352 int i = (long)cookie;
353 int32_t fd_reopen_status = -1;
354 int32_t final_reopen_status = -1;
355
356 priv = this->private;
357 local->replies[i].valid = 1;
358 local->replies[i].op_ret = op_ret;
359 local->replies[i].op_errno = op_errno;
360 if (op_ret != 0) {
361 gf_msg(this->name, GF_LOG_ERROR, op_errno, AFR_MSG_DICT_GET_FAILED,
362 "Failed getlk for %s", uuid_utoa(local->fd->inode->gfid));
363 }
364
365 if (xdata)
366 local->replies[i].xdata = dict_ref(xdata);
367
368 call_count = afr_frame_return(frame);
369
370 if (call_count)
371 return 0;
372
373 /* Currently we get 3 values from the lower layer (protocol/client) in the
374 * getlk_cbk.
375 * FD_REOPEN_ALLOWED : No conflicting locks are held and reopen is allowed
376 * FD_REOPEN_NOT_ALLOWED : Conflicting locks are held and reopen is not
377 * allowed
378 * FD_BAD : FD is not valid
379 *
380 * - If we get FD_REOPEN_NOT_ALLOWED from any of the bricks, will block the
381 * reopen taking this as high priority.
382 * - If we get FD_BAD from all the replies, we will not reopen since we do
383 * not know the correct status.
384 * - If we get FD_BAD from few brick and FD_REOPEN_NOT_ALLOWED from one or
385 * more bricks, then we will block reopen.
386 * - If we get FD_BAD from few bricks and FD_REOPEN_ALLOWED from one or
387 * more bricks, then we will allow the reopen.
388 *
389 * We will update the final_reopen_status only when the value returned
390 * from lower layer is >= FD_REOPEN_ALLOWED and < FD_BAD. We will not set
391 * FD_BAD in final_reopen_status, since it can lead to unexpected
392 * behaviours.
393 *
394 * At the end of this loop, if we still have final_reopen_status as -1
395 * i.e., the init value, it means we failed to get the fd status from any
396 * of the bricks or we do not have a valid fd on any of the bricks. We
397 * will not reopen the fd in this case as well.
398 */
399
400 for (i = 0; i < priv->child_count; i++) {
401 if (final_reopen_status != FD_REOPEN_NOT_ALLOWED &&
402 local->replies[i].xdata) {
403 ret = dict_get_int32(xdata, "fd-reopen-status", &fd_reopen_status);
404 if (ret) {
405 gf_msg(this->name, GF_LOG_ERROR, -ret, AFR_MSG_DICT_GET_FAILED,
406 "Failed to get whether reopen is allowed or not on fd "
407 "for file %s on subvolume %s.",
408 local->loc.path, priv->children[i]->name);
409 } else if (fd_reopen_status >= FD_REOPEN_ALLOWED &&
410 fd_reopen_status < FD_BAD) {
411 final_reopen_status = fd_reopen_status;
412 }
413 }
414
415 if (final_reopen_status == FD_REOPEN_NOT_ALLOWED)
416 break;
417 }
418
419 if (final_reopen_status == FD_REOPEN_NOT_ALLOWED) {
420 gf_log(this->name, GF_LOG_INFO,
421 "Conflicting locks held on file %s. FD reopen is not allowed.",
422 local->loc.path);
423 } else if (final_reopen_status == -1) {
424 gf_log(this->name, GF_LOG_INFO,
425 "Failed to get the lock information "
426 "on file %s. FD reopen is not allowed.",
427 local->loc.path);
428 } else {
429 afr_local_replies_wipe(local, priv);
430 afr_do_fix_open(frame, this);
431 return 0;
432 }
433
434 afr_fd_ctx_reset_need_open(local->fd, this, local->need_open);
435 AFR_STACK_DESTROY(frame);
436 return 0;
437 }
438
439 static void
440 afr_is_reopen_allowed(xlator_t *this, call_frame_t *frame)
441 {
442 afr_private_t *priv = NULL;
443 afr_local_t *local = NULL;
444 dict_t *xdata = NULL;
445 int i = 0;
446 int call_count = 0;
447 struct gf_flock flock = {
448 0,
449 };
450
451 local = frame->local;
452 priv = this->private;
453
454 flock.l_type = F_WRLCK;
455 afr_set_lk_owner(frame, this, frame->root);
456 lk_owner_copy(&flock.l_owner, &frame->root->lk_owner);
457
458 call_count = AFR_COUNT(local->child_up, priv->child_count);
459 if (!call_count)
460 goto out;
461 local->call_count = call_count;
462
463 xdata = dict_new();
464 if (xdata == NULL)
465 goto out;
466
467 if (dict_set_int32(xdata, "fd-reopen-status", -1))
468 goto out;
469
470 for (i = 0; i < priv->child_count; i++) {
471 if (local->child_up[i]) {
472 STACK_WIND_COOKIE(frame, afr_is_reopen_allowed_cbk, (void *)(long)i,
473 priv->children[i], priv->children[i]->fops->lk,
474 local->fd, F_GETLK, &flock, xdata);
475 } else {
476 continue;
477 }
478
479 if (!--call_count)
480 break;
481 }
482
483 dict_unref(xdata);
484 return;
485
486 out:
487 if (xdata)
488 dict_unref(xdata);
489 afr_fd_ctx_reset_need_open(local->fd, this, local->need_open);
490 AFR_STACK_DESTROY(frame);
491 return;
492 }
493
494 void
495 afr_fix_open(fd_t *fd, xlator_t *this)
496 {
497 call_frame_t *frame = NULL;
498 afr_local_t *local = NULL;
499 int ret = -1;
500 int32_t op_errno = 0;
501 afr_fd_ctx_t *fd_ctx = NULL;
502 int call_count = 0;
503
504 if (!afr_is_fd_fixable(fd))
505 goto out;
506
507 fd_ctx = afr_fd_ctx_get(fd, this);
508 if (!fd_ctx)
509 goto out;
510
511 frame = create_frame(this, this->ctx->pool);
512 if (!frame)
513 goto out;
514
515 local = AFR_FRAME_INIT(frame, op_errno);
516 if (!local)
517 goto out;
518
519 call_count = afr_fd_ctx_set_need_open(fd, this, local->need_open);
520 if (!call_count)
521 goto out;
522
523 local->loc.inode = inode_ref(fd->inode);
524 ret = loc_path(&local->loc, NULL);
525 if (ret < 0)
526 goto out;
527 local->fd = fd_ref(fd);
528 local->fd_ctx = fd_ctx;
529
530 afr_is_reopen_allowed(this, frame);
531 return;
532
533 out:
534 if (call_count)
535 afr_fd_ctx_reset_need_open(fd, this, local->need_open);
536 if (frame)
537 AFR_STACK_DESTROY(frame);
538 return;
539 }