Debian Codegraph @

xlators/cluster/afr/src/afr-inode-write.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 "protocol-common.h"
21 #include "afr-transaction.h"
22 #include "afr-self-heal.h"
23 #include "afr-messages.h"
24
25 static void
26 __afr_inode_write_finalize(call_frame_t *frame, xlator_t *this)
27 {
28 int i = 0;
29 int ret = 0;
30 int read_subvol = 0;
31 struct iatt *stbuf = NULL;
32 afr_local_t *local = NULL;
33 afr_private_t *priv = NULL;
34 afr_lock_t *lock = NULL;
35 afr_read_subvol_args_t args = {
36 0,
37 };
38
39 local = frame->local;
40 priv = this->private;
41 GF_VALIDATE_OR_GOTO(this->name, local->inode, out);
42
43 if (local->update_num_inodelks &&
44 local->transaction.type == AFR_DATA_TRANSACTION) {
45 lock = &local->inode_ctx->lock[local->transaction.type];
46 lock->num_inodelks = local->num_inodelks;
47 }
48
49 /*This code needs to stay till DHT sends fops on linked
50 * inodes*/
51 if (!inode_is_linked(local->inode)) {
52 for (i = 0; i < priv->child_count; i++) {
53 if (!local->replies[i].valid)
54 continue;
55 if (local->replies[i].op_ret == -1)
56 continue;
57 if (!gf_uuid_is_null(local->replies[i].poststat.ia_gfid)) {
58 gf_uuid_copy(args.gfid, local->replies[i].poststat.ia_gfid);
59 args.ia_type = local->replies[i].poststat.ia_type;
60 break;
61 } else {
62 ret = dict_get_bin(local->replies[i].xdata,
63 DHT_IATT_IN_XDATA_KEY, (void **)&stbuf);
64 if (ret)
65 continue;
66 gf_uuid_copy(args.gfid, stbuf->ia_gfid);
67 args.ia_type = stbuf->ia_type;
68 break;
69 }
70 }
71 }
72
73 if (local->transaction.type == AFR_METADATA_TRANSACTION) {
74 read_subvol = afr_metadata_subvol_get(local->inode, this, NULL,
75 local->readable, NULL, &args);
76 } else {
77 read_subvol = afr_data_subvol_get(local->inode, this, NULL,
78 local->readable, NULL, &args);
79 }
80
81 local->op_ret = -1;
82 local->op_errno = afr_final_errno(local, priv);
83 afr_pick_error_xdata(local, priv->child_count, local->inode,
84 local->readable, NULL, NULL);
85
86 for (i = 0; i < priv->child_count; i++) {
87 if (!local->replies[i].valid)
88 continue;
89 if (local->replies[i].op_ret < 0)
90 continue;
91
92 /* Order of checks in the compound conditional
93 below is important.
94
95 - Highest precedence: largest op_ret
96 - Next precedence: if all op_rets are equal, read subvol
97 - Least precedence: any succeeded subvol
98 */
99 if ((local->op_ret < local->replies[i].op_ret) ||
100 ((local->op_ret == local->replies[i].op_ret) &&
101 (i == read_subvol))) {
102 local->op_ret = local->replies[i].op_ret;
103 local->op_errno = local->replies[i].op_errno;
104
105 local->cont.inode_wfop.prebuf = local->replies[i].prestat;
106 local->cont.inode_wfop.postbuf = local->replies[i].poststat;
107
108 if (local->replies[i].xdata) {
109 if (local->xdata_rsp)
110 dict_unref(local->xdata_rsp);
111 local->xdata_rsp = dict_ref(local->replies[i].xdata);
112 }
113 if (local->replies[i].xattr) {
114 if (local->xattr_rsp)
115 dict_unref(local->xattr_rsp);
116 local->xattr_rsp = dict_ref(local->replies[i].xattr);
117 }
118 }
119 }
120
121 afr_set_in_flight_sb_status(this, frame, local->inode);
122 out:
123 return;
124 }
125
126 static void
127 __afr_inode_write_fill(afr_local_t *local, afr_private_t *priv, int child_index,
128 int op_ret, int op_errno, struct iatt *prebuf,
129 struct iatt *postbuf, dict_t *xattr, dict_t *xdata)
130 {
131 int num_inodelks = 0;
132
133 local->replies[child_index].valid = 1;
134
135 if (AFR_IS_ARBITER_BRICK(priv, child_index) && op_ret == 1)
136 op_ret = iov_length(local->cont.writev.vector,
137 local->cont.writev.count);
138
139 local->replies[child_index].op_ret = op_ret;
140 local->replies[child_index].op_errno = op_errno;
141 if (xdata) {
142 local->replies[child_index].xdata = dict_ref(xdata);
143 if (dict_get_int32_sizen(xdata, GLUSTERFS_INODELK_COUNT,
144 &num_inodelks) == 0) {
145 if (num_inodelks > local->num_inodelks) {
146 local->num_inodelks = num_inodelks;
147 local->update_num_inodelks = _gf_true;
148 }
149 }
150 }
151
152 if (op_ret >= 0) {
153 if (prebuf)
154 local->replies[child_index].prestat = *prebuf;
155 if (postbuf)
156 local->replies[child_index].poststat = *postbuf;
157 if (xattr)
158 local->replies[child_index].xattr = dict_ref(xattr);
159 } else {
160 afr_transaction_fop_failed(local, child_index);
161 }
162
163 return;
164 }
165
166 static int
167 __afr_inode_write_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
168 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
169 struct iatt *postbuf, dict_t *xattr, dict_t *xdata)
170 {
171 afr_local_t *local = NULL;
172 int child_index = (long)cookie;
173 int call_count = -1;
174 afr_private_t *priv = NULL;
175
176 priv = this->private;
177 local = frame->local;
178
179 LOCK(&frame->lock);
180 {
181 __afr_inode_write_fill(local, priv, child_index, op_ret, op_errno,
182 prebuf, postbuf, xattr, xdata);
183 call_count = --local->call_count;
184 }
185 UNLOCK(&frame->lock);
186
187 if (call_count == 0) {
188 __afr_inode_write_finalize(frame, this);
189
190 if (afr_txn_nothing_failed(frame, this)) {
191 /*if it did pre-op, it will do post-op changing ctime*/
192 if (priv->consistent_metadata && afr_needs_changelog_update(local))
193 afr_zero_fill_stat(local);
194 local->transaction.unwind(frame, this);
195 }
196
197 afr_transaction_resume(frame, this);
198 }
199
200 return 0;
201 }
202
203 /* {{{ writev */
204
205 static void
206 afr_writev_copy_outvars(call_frame_t *src_frame, call_frame_t *dst_frame)
207 {
208 afr_local_t *src_local = NULL;
209 afr_local_t *dst_local = NULL;
210
211 src_local = src_frame->local;
212 dst_local = dst_frame->local;
213
214 dst_local->op_ret = src_local->op_ret;
215 dst_local->op_errno = src_local->op_errno;
216 dst_local->cont.inode_wfop.prebuf = src_local->cont.inode_wfop.prebuf;
217 dst_local->cont.inode_wfop.postbuf = src_local->cont.inode_wfop.postbuf;
218 if (src_local->xdata_rsp)
219 dst_local->xdata_rsp = dict_ref(src_local->xdata_rsp);
220 }
221
222 static void
223 afr_writev_unwind(call_frame_t *frame, xlator_t *this)
224 {
225 afr_local_t *local = NULL;
226 afr_private_t *priv = this->private;
227
228 local = frame->local;
229
230 if (priv->consistent_metadata)
231 afr_zero_fill_stat(local);
232
233 AFR_STACK_UNWIND(writev, frame, local->op_ret, local->op_errno,
234 &local->cont.inode_wfop.prebuf,
235 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
236 }
237
238 static int
239 afr_transaction_writev_unwind(call_frame_t *frame, xlator_t *this)
240 {
241 call_frame_t *fop_frame = NULL;
242
243 fop_frame = afr_transaction_detach_fop_frame(frame);
244
245 if (fop_frame) {
246 afr_writev_copy_outvars(frame, fop_frame);
247 afr_writev_unwind(fop_frame, this);
248 }
249 return 0;
250 }
251
252 static void
253 afr_writev_handle_short_writes(call_frame_t *frame, xlator_t *this)
254 {
255 afr_local_t *local = NULL;
256 afr_private_t *priv = NULL;
257 int i = 0;
258
259 local = frame->local;
260 priv = this->private;
261 /*
262 * We already have the best case result of the writev calls staged
263 * as the return value. Any writev that returns some value less
264 * than the best case is now out of sync, so mark the fop as
265 * failed. Note that fops that have returned with errors have
266 * already been marked as failed.
267 */
268 for (i = 0; i < priv->child_count; i++) {
269 if ((!local->replies[i].valid) || (local->replies[i].op_ret == -1))
270 continue;
271
272 if (local->replies[i].op_ret < local->op_ret)
273 afr_transaction_fop_failed(local, i);
274 }
275 }
276
277 static int
278 afr_inode_write_fill(call_frame_t *frame, xlator_t *this, int child_index,
279 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
280 struct iatt *postbuf, dict_t *xdata)
281 {
282 int ret = 0;
283 afr_local_t *local = frame->local;
284 uint32_t open_fd_count = 0;
285 uint32_t write_is_append = 0;
286 int call_count;
287
288 LOCK(&frame->lock);
289 {
290 __afr_inode_write_fill(local, this->private, child_index, op_ret,
291 op_errno, prebuf, postbuf, NULL, xdata);
292 if (op_ret == -1 || !xdata)
293 goto unlock;
294
295 write_is_append = 0;
296 ret = dict_get_uint32(xdata, GLUSTERFS_WRITE_IS_APPEND,
297 &write_is_append);
298 if (ret || !write_is_append)
299 local->append_write = _gf_false;
300
301 ret = dict_get_uint32(xdata, GLUSTERFS_ACTIVE_FD_COUNT, &open_fd_count);
302 if (ret < 0)
303 goto unlock;
304 if (open_fd_count > local->open_fd_count) {
305 local->open_fd_count = open_fd_count;
306 local->update_open_fd_count = _gf_true;
307 }
308 }
309 unlock:
310 call_count = --local->call_count;
311 UNLOCK(&frame->lock);
312
313 return call_count;
314 }
315
316 static void
317 afr_process_post_writev(call_frame_t *frame, xlator_t *this)
318 {
319 afr_local_t *local = NULL;
320
321 local = frame->local;
322
323 if (!local->stable_write && !local->append_write)
324 /* An appended write removes the necessity to
325 fsync() the file. This is because self-heal
326 has the logic to check for larger file when
327 the xattrs are not reliably pointing at
328 a stale file.
329 */
330 afr_fd_report_unstable_write(this, local);
331
332 __afr_inode_write_finalize(frame, this);
333
334 afr_writev_handle_short_writes(frame, this);
335
336 if (local->update_open_fd_count)
337 local->inode_ctx->open_fd_count = local->open_fd_count;
338 }
339
340 static int
341 afr_writev_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
342 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
343 struct iatt *postbuf, dict_t *xdata)
344 {
345 call_frame_t *fop_frame = NULL;
346 int child_index = (long)cookie;
347 int call_count;
348
349 call_count = afr_inode_write_fill(frame, this, child_index, op_ret,
350 op_errno, prebuf, postbuf, xdata);
351
352 if (call_count == 0) {
353 afr_process_post_writev(frame, this);
354
355 if (!afr_txn_nothing_failed(frame, this)) {
356 // Don't unwind until post-op is complete
357 afr_transaction_resume(frame, this);
358 } else {
359 /*
360 * Generally inode-write fops do transaction.unwind then
361 * transaction.resume, but writev needs to make sure that
362 * delayed post-op frame is placed in fdctx before unwind
363 * happens. This prevents the race of flush doing the
364 * changelog wakeup first in fuse thread and then this
365 * writev placing its delayed post-op frame in fdctx.
366 * This helps flush make sure all the delayed post-ops are
367 * completed.
368 */
369
370 fop_frame = afr_transaction_detach_fop_frame(frame);
371 afr_writev_copy_outvars(frame, fop_frame);
372 afr_transaction_resume(frame, this);
373 afr_writev_unwind(fop_frame, this);
374 }
375 }
376 return 0;
377 }
378
379 static int
380 afr_arbiter_writev_wind(call_frame_t *frame, xlator_t *this, int subvol)
381 {
382 afr_local_t *local = frame->local;
383 afr_private_t *priv = this->private;
384 static char byte = 0xFF;
385 static struct iovec vector = {&byte, 1};
386 int32_t count = 1;
387
388 STACK_WIND_COOKIE(
389 frame, afr_writev_wind_cbk, (void *)(long)subvol,
390 priv->children[subvol], priv->children[subvol]->fops->writev, local->fd,
391 &vector, count, local->cont.writev.offset, local->cont.writev.flags,
392 local->cont.writev.iobref, local->xdata_req);
393
394 return 0;
395 }
396
397 int
398 afr_writev_wind(call_frame_t *frame, xlator_t *this, int subvol)
399 {
400 afr_local_t *local = NULL;
401 afr_private_t *priv = NULL;
402
403 local = frame->local;
404 priv = this->private;
405
406 if (AFR_IS_ARBITER_BRICK(priv, subvol)) {
407 afr_arbiter_writev_wind(frame, this, subvol);
408 return 0;
409 }
410
411 STACK_WIND_COOKIE(frame, afr_writev_wind_cbk, (void *)(long)subvol,
412 priv->children[subvol],
413 priv->children[subvol]->fops->writev, local->fd,
414 local->cont.writev.vector, local->cont.writev.count,
415 local->cont.writev.offset, local->cont.writev.flags,
416 local->cont.writev.iobref, local->xdata_req);
417 return 0;
418 }
419
420 static int
421 afr_do_writev(call_frame_t *frame, xlator_t *this)
422 {
423 call_frame_t *transaction_frame = NULL;
424 afr_local_t *local = NULL;
425 int ret = -1;
426 int op_errno = ENOMEM;
427
428 transaction_frame = copy_frame(frame);
429 if (!transaction_frame)
430 goto out;
431
432 local = frame->local;
433 transaction_frame->local = local;
434 frame->local = NULL;
435
436 if (!AFR_FRAME_INIT(frame, op_errno))
437 goto out;
438
439 local->op = GF_FOP_WRITE;
440
441 local->transaction.wind = afr_writev_wind;
442 local->transaction.unwind = afr_transaction_writev_unwind;
443
444 local->transaction.main_frame = frame;
445
446 if (local->fd->flags & O_APPEND) {
447 /*
448 * Backend vfs ignores the 'offset' for append mode fd so
449 * locking just the region provided for the writev does not
450 * give consistency guarantee. The actual write may happen at a
451 * completely different range than the one provided by the
452 * offset, len in the fop. So lock the entire file.
453 */
454 local->transaction.start = 0;
455 local->transaction.len = 0;
456 } else {
457 local->transaction.start = local->cont.writev.offset;
458 local->transaction.len = iov_length(local->cont.writev.vector,
459 local->cont.writev.count);
460 }
461
462 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
463 if (ret < 0) {
464 op_errno = -ret;
465 goto out;
466 }
467
468 return 0;
469 out:
470 if (transaction_frame)
471 AFR_STACK_DESTROY(transaction_frame);
472
473 AFR_STACK_UNWIND(writev, frame, -1, op_errno, NULL, NULL, NULL);
474 return 0;
475 }
476
477 int
478 afr_writev(call_frame_t *frame, xlator_t *this, fd_t *fd, struct iovec *vector,
479 int32_t count, off_t offset, uint32_t flags, struct iobref *iobref,
480 dict_t *xdata)
481 {
482 afr_local_t *local = NULL;
483 int op_errno = ENOMEM;
484 int ret = -1;
485
486 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
487 local = AFR_FRAME_INIT(frame, op_errno);
488 if (!local)
489 goto out;
490
491 local->cont.writev.vector = iov_dup(vector, count);
492 if (!local->cont.writev.vector)
493 goto out;
494 local->cont.writev.count = count;
495 local->cont.writev.offset = offset;
496 local->cont.writev.flags = flags;
497 local->cont.writev.iobref = iobref_ref(iobref);
498
499 if (xdata)
500 local->xdata_req = dict_copy_with_ref(xdata, NULL);
501 else
502 local->xdata_req = dict_new();
503
504 if (!local->xdata_req)
505 goto out;
506
507 local->fd = fd_ref(fd);
508 ret = afr_set_inode_local(this, local, fd->inode);
509 if (ret)
510 goto out;
511
512 if (dict_set_uint32(local->xdata_req, GLUSTERFS_ACTIVE_FD_COUNT, 4)) {
513 op_errno = ENOMEM;
514 goto out;
515 }
516
517 if (dict_set_str_sizen(local->xdata_req, GLUSTERFS_INODELK_DOM_COUNT,
518 this->name)) {
519 op_errno = ENOMEM;
520 goto out;
521 }
522
523 if (dict_set_uint32(local->xdata_req, GLUSTERFS_WRITE_IS_APPEND, 4)) {
524 op_errno = ENOMEM;
525 goto out;
526 }
527
528 /* Set append_write to be true speculatively. If on any
529 server it turns not be true, we unset it in the
530 callback.
531 */
532 local->append_write = _gf_true;
533
534 /* detect here, but set it in writev_wind_cbk *after* the unstable
535 write is performed
536 */
537 local->stable_write = !!((fd->flags | flags) & (O_SYNC | O_DSYNC));
538
539 afr_fix_open(fd, this);
540
541 afr_do_writev(frame, this);
542
543 return 0;
544 out:
545 AFR_STACK_UNWIND(writev, frame, -1, op_errno, NULL, NULL, NULL);
546
547 return 0;
548 }
549
550 /* }}} */
551
552 /* {{{ truncate */
553
554 static int
555 afr_truncate_unwind(call_frame_t *frame, xlator_t *this)
556 {
557 afr_local_t *local = NULL;
558 call_frame_t *main_frame = NULL;
559
560 local = frame->local;
561
562 main_frame = afr_transaction_detach_fop_frame(frame);
563 if (!main_frame)
564 return 0;
565
566 AFR_STACK_UNWIND(truncate, main_frame, local->op_ret, local->op_errno,
567 &local->cont.inode_wfop.prebuf,
568 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
569 return 0;
570 }
571
572 static int
573 afr_truncate_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
574 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
575 struct iatt *postbuf, dict_t *xdata)
576 {
577 afr_local_t *local = NULL;
578
579 local = frame->local;
580
581 if (op_ret == 0 && prebuf->ia_size != postbuf->ia_size)
582 local->stable_write = _gf_false;
583
584 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
585 postbuf, NULL, xdata);
586 }
587
588 static int
589 afr_truncate_wind(call_frame_t *frame, xlator_t *this, int subvol)
590 {
591 afr_local_t *local = NULL;
592 afr_private_t *priv = NULL;
593
594 local = frame->local;
595 priv = this->private;
596
597 STACK_WIND_COOKIE(frame, afr_truncate_wind_cbk, (void *)(long)subvol,
598 priv->children[subvol],
599 priv->children[subvol]->fops->truncate, &local->loc,
600 local->cont.truncate.offset, local->xdata_req);
601 return 0;
602 }
603
604 int
605 afr_truncate(call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset,
606 dict_t *xdata)
607 {
608 afr_local_t *local = NULL;
609 call_frame_t *transaction_frame = NULL;
610 int ret = -1;
611 int op_errno = ENOMEM;
612
613 transaction_frame = copy_frame(frame);
614 if (!transaction_frame)
615 goto out;
616
617 local = AFR_FRAME_INIT(transaction_frame, op_errno);
618 if (!local)
619 goto out;
620
621 local->cont.truncate.offset = offset;
622 if (xdata)
623 local->xdata_req = dict_copy_with_ref(xdata, NULL);
624 else
625 local->xdata_req = dict_new();
626
627 if (!local->xdata_req)
628 goto out;
629
630 local->transaction.wind = afr_truncate_wind;
631 local->transaction.unwind = afr_truncate_unwind;
632
633 loc_copy(&local->loc, loc);
634 ret = afr_set_inode_local(this, local, loc->inode);
635 if (ret)
636 goto out;
637
638 local->op = GF_FOP_TRUNCATE;
639
640 local->transaction.main_frame = frame;
641 local->transaction.start = offset;
642 local->transaction.len = 0;
643
644 /* Set it true speculatively, will get reset in afr_truncate_wind_cbk
645 if truncate was not a NOP */
646 local->stable_write = _gf_true;
647
648 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
649 if (ret < 0) {
650 op_errno = -ret;
651 goto out;
652 }
653
654 return 0;
655 out:
656 if (transaction_frame)
657 AFR_STACK_DESTROY(transaction_frame);
658
659 AFR_STACK_UNWIND(truncate, frame, -1, op_errno, NULL, NULL, NULL);
660 return 0;
661 }
662
663 /* }}} */
664
665 /* {{{ ftruncate */
666
667 static int
668 afr_ftruncate_unwind(call_frame_t *frame, xlator_t *this)
669 {
670 afr_local_t *local = NULL;
671 call_frame_t *main_frame = NULL;
672
673 local = frame->local;
674
675 main_frame = afr_transaction_detach_fop_frame(frame);
676 if (!main_frame)
677 return 0;
678
679 AFR_STACK_UNWIND(ftruncate, main_frame, local->op_ret, local->op_errno,
680 &local->cont.inode_wfop.prebuf,
681 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
682 return 0;
683 }
684
685 static int
686 afr_ftruncate_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
687 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
688 struct iatt *postbuf, dict_t *xdata)
689 {
690 afr_local_t *local = NULL;
691
692 local = frame->local;
693
694 if (op_ret == 0 && prebuf->ia_size != postbuf->ia_size)
695 local->stable_write = _gf_false;
696
697 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
698 postbuf, NULL, xdata);
699 }
700
701 static int
702 afr_ftruncate_wind(call_frame_t *frame, xlator_t *this, int subvol)
703 {
704 afr_local_t *local = NULL;
705 afr_private_t *priv = NULL;
706
707 local = frame->local;
708 priv = this->private;
709
710 STACK_WIND_COOKIE(frame, afr_ftruncate_wind_cbk, (void *)(long)subvol,
711 priv->children[subvol],
712 priv->children[subvol]->fops->ftruncate, local->fd,
713 local->cont.ftruncate.offset, local->xdata_req);
714 return 0;
715 }
716
717 int
718 afr_ftruncate(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
719 dict_t *xdata)
720 {
721 afr_local_t *local = NULL;
722 call_frame_t *transaction_frame = NULL;
723 int ret = -1;
724 int op_errno = ENOMEM;
725
726 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
727 transaction_frame = copy_frame(frame);
728 if (!transaction_frame)
729 goto out;
730
731 local = AFR_FRAME_INIT(transaction_frame, op_errno);
732 if (!local)
733 goto out;
734
735 local->cont.ftruncate.offset = offset;
736 if (xdata)
737 local->xdata_req = dict_copy_with_ref(xdata, NULL);
738 else
739 local->xdata_req = dict_new();
740
741 if (!local->xdata_req)
742 goto out;
743
744 local->fd = fd_ref(fd);
745 ret = afr_set_inode_local(this, local, fd->inode);
746 if (ret)
747 goto out;
748
749 local->op = GF_FOP_FTRUNCATE;
750
751 local->transaction.wind = afr_ftruncate_wind;
752 local->transaction.unwind = afr_ftruncate_unwind;
753
754 local->transaction.main_frame = frame;
755
756 local->transaction.start = local->cont.ftruncate.offset;
757 local->transaction.len = 0;
758
759 afr_fix_open(fd, this);
760
761 /* Set it true speculatively, will get reset in afr_ftruncate_wind_cbk
762 if truncate was not a NOP */
763 local->stable_write = _gf_true;
764
765 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
766 if (ret < 0) {
767 op_errno = -ret;
768 goto out;
769 }
770
771 return 0;
772 out:
773 AFR_STACK_UNWIND(ftruncate, frame, -1, op_errno, NULL, NULL, NULL);
774
775 return 0;
776 }
777
778 /* }}} */
779
780 /* {{{ setattr */
781
782 static int
783 afr_setattr_unwind(call_frame_t *frame, xlator_t *this)
784 {
785 afr_local_t *local = NULL;
786 call_frame_t *main_frame = NULL;
787
788 local = frame->local;
789
790 main_frame = afr_transaction_detach_fop_frame(frame);
791 if (!main_frame)
792 return 0;
793
794 AFR_STACK_UNWIND(setattr, main_frame, local->op_ret, local->op_errno,
795 &local->cont.inode_wfop.prebuf,
796 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
797 return 0;
798 }
799
800 static int
801 afr_setattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
802 int op_ret, int op_errno, struct iatt *preop,
803 struct iatt *postop, dict_t *xdata)
804 {
805 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, preop,
806 postop, NULL, xdata);
807 }
808
809 static int
810 afr_setattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
811 {
812 afr_local_t *local = NULL;
813 afr_private_t *priv = NULL;
814
815 local = frame->local;
816 priv = this->private;
817
818 STACK_WIND_COOKIE(frame, afr_setattr_wind_cbk, (void *)(long)subvol,
819 priv->children[subvol],
820 priv->children[subvol]->fops->setattr, &local->loc,
821 &local->cont.setattr.in_buf, local->cont.setattr.valid,
822 local->xdata_req);
823 return 0;
824 }
825
826 int
827 afr_setattr(call_frame_t *frame, xlator_t *this, loc_t *loc, struct iatt *buf,
828 int32_t valid, dict_t *xdata)
829 {
830 afr_local_t *local = NULL;
831 call_frame_t *transaction_frame = NULL;
832 int ret = -1;
833 int op_errno = ENOMEM;
834
835 transaction_frame = copy_frame(frame);
836 if (!transaction_frame)
837 goto out;
838
839 local = AFR_FRAME_INIT(transaction_frame, op_errno);
840 if (!local)
841 goto out;
842
843 local->cont.setattr.in_buf = *buf;
844 local->cont.setattr.valid = valid;
845 if (xdata)
846 local->xdata_req = dict_copy_with_ref(xdata, NULL);
847 else
848 local->xdata_req = dict_new();
849
850 if (!local->xdata_req)
851 goto out;
852
853 local->transaction.wind = afr_setattr_wind;
854 local->transaction.unwind = afr_setattr_unwind;
855
856 loc_copy(&local->loc, loc);
857 ret = afr_set_inode_local(this, local, loc->inode);
858 if (ret)
859 goto out;
860
861 local->op = GF_FOP_SETATTR;
862
863 local->transaction.main_frame = frame;
864 local->transaction.start = LLONG_MAX - 1;
865 local->transaction.len = 0;
866
867 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
868 if (ret < 0) {
869 op_errno = -ret;
870 goto out;
871 }
872
873 return 0;
874 out:
875 if (transaction_frame)
876 AFR_STACK_DESTROY(transaction_frame);
877
878 AFR_STACK_UNWIND(setattr, frame, -1, op_errno, NULL, NULL, NULL);
879 return 0;
880 }
881
882 /* {{{ fsetattr */
883
884 static int
885 afr_fsetattr_unwind(call_frame_t *frame, xlator_t *this)
886 {
887 afr_local_t *local = NULL;
888 call_frame_t *main_frame = NULL;
889
890 local = frame->local;
891
892 main_frame = afr_transaction_detach_fop_frame(frame);
893 if (!main_frame)
894 return 0;
895
896 AFR_STACK_UNWIND(fsetattr, main_frame, local->op_ret, local->op_errno,
897 &local->cont.inode_wfop.prebuf,
898 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
899 return 0;
900 }
901
902 static int
903 afr_fsetattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
904 int32_t op_ret, int32_t op_errno, struct iatt *preop,
905 struct iatt *postop, dict_t *xdata)
906 {
907 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, preop,
908 postop, NULL, xdata);
909 }
910
911 static int
912 afr_fsetattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
913 {
914 afr_local_t *local = NULL;
915 afr_private_t *priv = NULL;
916
917 local = frame->local;
918 priv = this->private;
919
920 STACK_WIND_COOKIE(frame, afr_fsetattr_wind_cbk, (void *)(long)subvol,
921 priv->children[subvol],
922 priv->children[subvol]->fops->fsetattr, local->fd,
923 &local->cont.fsetattr.in_buf, local->cont.fsetattr.valid,
924 local->xdata_req);
925 return 0;
926 }
927
928 int
929 afr_fsetattr(call_frame_t *frame, xlator_t *this, fd_t *fd, struct iatt *buf,
930 int32_t valid, dict_t *xdata)
931 {
932 afr_local_t *local = NULL;
933 call_frame_t *transaction_frame = NULL;
934 int ret = -1;
935 int op_errno = ENOMEM;
936
937 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
938 transaction_frame = copy_frame(frame);
939 if (!transaction_frame)
940 goto out;
941
942 local = AFR_FRAME_INIT(transaction_frame, op_errno);
943 if (!local)
944 goto out;
945
946 local->cont.fsetattr.in_buf = *buf;
947 local->cont.fsetattr.valid = valid;
948 if (xdata)
949 local->xdata_req = dict_copy_with_ref(xdata, NULL);
950 else
951 local->xdata_req = dict_new();
952
953 if (!local->xdata_req)
954 goto out;
955
956 local->transaction.wind = afr_fsetattr_wind;
957 local->transaction.unwind = afr_fsetattr_unwind;
958
959 local->fd = fd_ref(fd);
960 ret = afr_set_inode_local(this, local, fd->inode);
961 if (ret)
962 goto out;
963
964 local->op = GF_FOP_FSETATTR;
965
966 afr_fix_open(fd, this);
967
968 local->transaction.main_frame = frame;
969 local->transaction.start = LLONG_MAX - 1;
970 local->transaction.len = 0;
971
972 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
973 if (ret < 0) {
974 op_errno = -ret;
975 goto out;
976 }
977
978 return 0;
979 out:
980 if (transaction_frame)
981 AFR_STACK_DESTROY(transaction_frame);
982
983 AFR_STACK_UNWIND(fsetattr, frame, -1, op_errno, NULL, NULL, NULL);
984 return 0;
985 }
986
987 /* {{{ setxattr */
988
989 static int
990 afr_setxattr_unwind(call_frame_t *frame, xlator_t *this)
991 {
992 afr_local_t *local = NULL;
993 call_frame_t *main_frame = NULL;
994
995 local = frame->local;
996
997 main_frame = afr_transaction_detach_fop_frame(frame);
998 if (!main_frame)
999 return 0;
1000
1001 AFR_STACK_UNWIND(setxattr, main_frame, local->op_ret, local->op_errno,
1002 local->xdata_rsp);
1003 return 0;
1004 }
1005
1006 static int
1007 afr_setxattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1008 int32_t op_ret, int32_t op_errno, dict_t *xdata)
1009 {
1010 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1011 NULL, NULL, xdata);
1012 }
1013
1014 static int
1015 afr_setxattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
1016 {
1017 afr_local_t *local = NULL;
1018 afr_private_t *priv = NULL;
1019
1020 local = frame->local;
1021 priv = this->private;
1022
1023 STACK_WIND_COOKIE(frame, afr_setxattr_wind_cbk, (void *)(long)subvol,
1024 priv->children[subvol],
1025 priv->children[subvol]->fops->setxattr, &local->loc,
1026 local->cont.setxattr.dict, local->cont.setxattr.flags,
1027 local->xdata_req);
1028 return 0;
1029 }
1030
1031 static int
1032 afr_emptyb_set_pending_changelog_cbk(call_frame_t *frame, void *cookie,
1033 xlator_t *this, int op_ret, int op_errno,
1034 dict_t *xattr, dict_t *xdata)
1035
1036 {
1037 afr_local_t *local = NULL;
1038 afr_private_t *priv = NULL;
1039 int i, ret = 0;
1040 char *op_type = NULL;
1041
1042 local = frame->local;
1043 priv = this->private;
1044 i = (long)cookie;
1045
1046 local->replies[i].valid = 1;
1047 local->replies[i].op_ret = op_ret;
1048 local->replies[i].op_errno = op_errno;
1049
1050 ret = dict_get_str_sizen(local->xdata_req, "replicate-brick-op", &op_type);
1051 if (ret)
1052 goto out;
1053
1054 gf_smsg(this->name, op_ret ? GF_LOG_ERROR : GF_LOG_INFO,
1055 op_ret ? op_errno : 0, AFR_MSG_SET_PEND_XATTR, "name=%s",
1056 priv->children[i]->name, "op_ret=%s",
1057 op_ret ? "failed" : "succeeded", NULL);
1058
1059 out:
1060 syncbarrier_wake(&local->barrier);
1061 return 0;
1062 }
1063
1064 static int
1065 afr_emptyb_set_pending_changelog(call_frame_t *frame, xlator_t *this,
1066 unsigned char *locked_nodes)
1067 {
1068 afr_local_t *local = NULL;
1069 afr_private_t *priv = NULL;
1070 int ret = 0, i = 0;
1071
1072 local = frame->local;
1073 priv = this->private;
1074
1075 AFR_ONLIST(locked_nodes, frame, afr_emptyb_set_pending_changelog_cbk,
1076 xattrop, &local->loc, GF_XATTROP_ADD_ARRAY, local->xattr_req,
1077 NULL);
1078
1079 /* It is sufficient if xattrop was successful on one child */
1080 for (i = 0; i < priv->child_count; i++) {
1081 if (!local->replies[i].valid)
1082 continue;
1083
1084 if (local->replies[i].op_ret == 0) {
1085 ret = 0;
1086 goto out;
1087 } else {
1088 ret = afr_higher_errno(ret, local->replies[i].op_errno);
1089 }
1090 }
1091 out:
1092 return -ret;
1093 }
1094
1095 static int
1096 _afr_handle_empty_brick_type(xlator_t *this, call_frame_t *frame, loc_t *loc,
1097 int empty_index, afr_transaction_type type,
1098 char *op_type, const int op_type_len)
1099 {
1100 int count = 0;
1101 int ret = -ENOMEM;
1102 int idx = -1;
1103 int d_idx = -1;
1104 unsigned char *locked_nodes = NULL;
1105 afr_local_t *local = NULL;
1106 afr_private_t *priv = NULL;
1107
1108 priv = this->private;
1109 local = frame->local;
1110 uint32_t hton32_1;
1111
1112 locked_nodes = alloca0(priv->child_count);
1113
1114 idx = afr_index_for_transaction_type(type);
1115 d_idx = afr_index_for_transaction_type(AFR_DATA_TRANSACTION);
1116
1117 local->pending = afr_matrix_create(priv->child_count, AFR_NUM_CHANGE_LOGS);
1118 if (!local->pending)
1119 goto out;
1120
1121 hton32_1 = htobe32(1);
1122 local->pending[empty_index][idx] = hton32_1;
1123
1124 if ((priv->esh_granular) && (type == AFR_ENTRY_TRANSACTION))
1125 local->pending[empty_index][d_idx] = hton32_1;
1126
1127 local->xdata_req = dict_new();
1128 if (!local->xdata_req)
1129 goto out;
1130
1131 ret = dict_set_nstrn(local->xdata_req, "replicate-brick-op",
1132 SLEN("replicate-brick-op"), op_type, op_type_len);
1133 if (ret)
1134 goto out;
1135
1136 local->xattr_req = dict_new();
1137 if (!local->xattr_req)
1138 goto out;
1139
1140 ret = afr_set_pending_dict(priv, local->xattr_req, local->pending);
1141 if (ret < 0)
1142 goto out;
1143
1144 if (AFR_ENTRY_TRANSACTION == type) {
1145 count = afr_selfheal_entrylk(frame, this, loc->inode, this->name, NULL,
1146 locked_nodes);
1147 } else {
1148 count = afr_selfheal_inodelk(frame, this, loc->inode, this->name,
1149 LLONG_MAX - 1, 0, locked_nodes);
1150 }
1151
1152 if (!count) {
1153 gf_smsg(this->name, GF_LOG_ERROR, EAGAIN, AFR_MSG_REPLACE_BRICK_STATUS,
1154 NULL);
1155 ret = -EAGAIN;
1156 goto unlock;
1157 }
1158
1159 ret = afr_emptyb_set_pending_changelog(frame, this, locked_nodes);
1160 if (ret)
1161 goto unlock;
1162 ret = 0;
1163 unlock:
1164 if (AFR_ENTRY_TRANSACTION == type) {
1165 afr_selfheal_unentrylk(frame, this, loc->inode, this->name, NULL,
1166 locked_nodes, NULL);
1167 } else {
1168 afr_selfheal_uninodelk(frame, this, loc->inode, this->name,
1169 LLONG_MAX - 1, 0, locked_nodes);
1170 }
1171 out:
1172 return ret;
1173 }
1174
1175 static void
1176 afr_brick_args_cleanup(void *opaque)
1177 {
1178 afr_empty_brick_args_t *data = NULL;
1179
1180 data = opaque;
1181 loc_wipe(&data->loc);
1182 GF_FREE(data);
1183 }
1184
1185 static int
1186 _afr_handle_empty_brick_cbk(int ret, call_frame_t *frame, void *opaque)
1187 {
1188 afr_brick_args_cleanup(opaque);
1189 return 0;
1190 }
1191
1192 static int
1193 _afr_handle_empty_brick(void *opaque)
1194 {
1195 afr_local_t *local = NULL;
1196 afr_private_t *priv = NULL;
1197 int empty_index = -1;
1198 int ret = -1;
1199 int op_errno = ENOMEM;
1200 call_frame_t *frame = NULL;
1201 xlator_t *this = NULL;
1202 char *op_type = NULL;
1203 int op_type_len = 0;
1204 afr_empty_brick_args_t *data = NULL;
1205 call_frame_t *op_frame = NULL;
1206
1207 data = opaque;
1208 frame = data->frame;
1209 empty_index = data->empty_index;
1210 if (!data->op_type)
1211 goto out;
1212
1213 op_frame = copy_frame(frame);
1214 if (!op_frame) {
1215 ret = -1;
1216 op_errno = ENOMEM;
1217 goto out;
1218 }
1219
1220 op_type = data->op_type;
1221 op_type_len = strlen(op_type);
1222 this = op_frame->this;
1223 priv = this->private;
1224
1225 afr_set_lk_owner(op_frame, this, op_frame->root);
1226 local = AFR_FRAME_INIT(op_frame, op_errno);
1227 if (!local)
1228 goto out;
1229
1230 loc_copy(&local->loc, &data->loc);
1231
1232 gf_smsg(this->name, GF_LOG_INFO, 0, AFR_MSG_NEW_BRICK, "name=%s",
1233 priv->children[empty_index]->name, NULL);
1234
1235 ret = _afr_handle_empty_brick_type(this, op_frame, &local->loc, empty_index,
1236 AFR_METADATA_TRANSACTION, op_type,
1237 op_type_len);
1238 if (ret) {
1239 op_errno = -ret;
1240 ret = -1;
1241 goto out;
1242 }
1243
1244 dict_unref(local->xdata_req);
1245 dict_unref(local->xattr_req);
1246 afr_matrix_cleanup(local->pending, priv->child_count);
1247 local->pending = NULL;
1248 local->xattr_req = NULL;
1249 local->xdata_req = NULL;
1250
1251 ret = _afr_handle_empty_brick_type(this, op_frame, &local->loc, empty_index,
1252 AFR_ENTRY_TRANSACTION, op_type,
1253 op_type_len);
1254 if (ret) {
1255 op_errno = -ret;
1256 ret = -1;
1257 goto out;
1258 }
1259 ret = 0;
1260 out:
1261 if (op_frame) {
1262 AFR_STACK_DESTROY(op_frame);
1263 }
1264 AFR_STACK_UNWIND(setxattr, frame, ret, op_errno, NULL);
1265 return 0;
1266 }
1267
1268 static int
1269 afr_split_brain_resolve_do(call_frame_t *frame, xlator_t *this, loc_t *loc,
1270 char *data)
1271 {
1272 afr_local_t *local = NULL;
1273 int ret = -1;
1274 int op_errno = EINVAL;
1275
1276 local = frame->local;
1277 local->xdata_req = dict_new();
1278
1279 if (!local->xdata_req) {
1280 op_errno = ENOMEM;
1281 goto out;
1282 }
1283
1284 ret = dict_set_int32_sizen(local->xdata_req, "heal-op",
1285 GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK);
1286 if (ret) {
1287 op_errno = -ret;
1288 ret = -1;
1289 goto out;
1290 }
1291 ret = dict_set_str_sizen(local->xdata_req, "child-name", data);
1292 if (ret) {
1293 op_errno = -ret;
1294 ret = -1;
1295 goto out;
1296 }
1297 /* set spb choice to -1 whether heal succeeds or not:
1298 * If heal succeeds : spb-choice should be set to -1 as
1299 * it is no longer valid; file is not
1300 * in split-brain anymore.
1301 * If heal doesn't succeed:
1302 * spb-choice should be set to -1
1303 * otherwise reads will be served
1304 * from spb-choice which is misleading.
1305 */
1306 ret = afr_inode_split_brain_choice_set(loc->inode, this, -1);
1307 if (ret)
1308 gf_smsg(this->name, GF_LOG_WARNING, 0, AFR_MSG_SPLIT_BRAIN_SET_FAILED,
1309 NULL);
1310 afr_heal_splitbrain_file(frame, this, loc);
1311 ret = 0;
1312 out:
1313 if (ret < 0)
1314 AFR_STACK_UNWIND(setxattr, frame, -1, op_errno, NULL);
1315 return 0;
1316 }
1317
1318 static int
1319 afr_get_split_brain_child_index(xlator_t *this, void *value, size_t len)
1320 {
1321 int spb_child_index = -1;
1322 char *spb_child_str = NULL;
1323
1324 spb_child_str = alloca0(len + 1);
1325 memcpy(spb_child_str, value, len);
1326
1327 if (!strcmp(spb_child_str, "none"))
1328 return -2;
1329
1330 spb_child_index = afr_get_child_index_from_name(this, spb_child_str);
1331 if (spb_child_index < 0) {
1332 gf_smsg(this->name, GF_LOG_ERROR, 0, AFR_MSG_INVALID_SUBVOL,
1333 "subvol=%s", spb_child_str, NULL);
1334 }
1335 return spb_child_index;
1336 }
1337
1338 static int
1339 afr_can_set_split_brain_choice(void *opaque)
1340 {
1341 afr_spbc_timeout_t *data = opaque;
1342 call_frame_t *frame = NULL;
1343 xlator_t *this = NULL;
1344 loc_t *loc = NULL;
1345 int ret = -1;
1346
1347 frame = data->frame;
1348 loc = data->loc;
1349 this = frame->this;
1350
1351 ret = afr_is_split_brain(frame, this, loc->inode, loc->gfid, &data->d_spb,
1352 &data->m_spb);
1353
1354 if (ret)
1355 gf_smsg(this->name, GF_LOG_ERROR, 0,
1356 AFR_MSG_SPLIT_BRAIN_DETERMINE_FAILED, "gfid=%s",
1357 uuid_utoa(loc->gfid), NULL);
1358 return ret;
1359 }
1360
1361 static int
1362 afr_handle_split_brain_commands(xlator_t *this, call_frame_t *frame, loc_t *loc,
1363 dict_t *dict)
1364 {
1365 void *choice_value = NULL;
1366 void *resolve_value = NULL;
1367 afr_private_t *priv = NULL;
1368 afr_local_t *local = NULL;
1369 afr_spbc_timeout_t *data = NULL;
1370 int len = 0;
1371 int spb_child_index = -1;
1372 int ret = -1;
1373 int op_errno = EINVAL;
1374
1375 priv = this->private;
1376
1377 ret = dict_get_ptr_and_len(dict, GF_AFR_SBRAIN_CHOICE, &choice_value, &len);
1378 ret = dict_get_ptr_and_len(dict, GF_AFR_SBRAIN_RESOLVE, &resolve_value,
1379 &len);
1380 if (!choice_value && !resolve_value) {
1381 ret = -1;
1382 goto out;
1383 }
1384
1385 local = AFR_FRAME_INIT(frame, op_errno);
1386 if (!local) {
1387 ret = 1;
1388 goto out;
1389 }
1390
1391 local->op = GF_FOP_SETXATTR;
1392
1393 if (choice_value) {
1394 spb_child_index = afr_get_split_brain_child_index(this, choice_value,
1395 len);
1396 if (spb_child_index < 0) {
1397 /* Case where value was "none" */
1398 if (spb_child_index == -2)
1399 spb_child_index = -1;
1400 else {
1401 ret = 1;
1402 op_errno = EINVAL;
1403 goto out;
1404 }
1405 }
1406
1407 data = GF_CALLOC(1, sizeof(*data), gf_afr_mt_spbc_timeout_t);
1408 if (!data) {
1409 ret = 1;
1410 goto out;
1411 }
1412 data->spb_child_index = spb_child_index;
1413 data->frame = frame;
1414 loc_copy(&local->loc, loc);
1415 data->loc = &local->loc;
1416 ret = synctask_new(this->ctx->env, afr_can_set_split_brain_choice,
1417 afr_set_split_brain_choice, NULL, data);
1418 if (ret) {
1419 gf_smsg(this->name, GF_LOG_ERROR, 0, AFR_MSG_SPLIT_BRAIN_STATUS,
1420 "name=%s", loc->name, NULL);
1421 ret = 1;
1422 op_errno = ENOMEM;
1423 goto out;
1424 }
1425 ret = 0;
1426 goto out;
1427 }
1428
1429 if (resolve_value) {
1430 spb_child_index = afr_get_split_brain_child_index(this, resolve_value,
1431 len);
1432 if (spb_child_index < 0) {
1433 ret = 1;
1434 goto out;
1435 }
1436
1437 afr_split_brain_resolve_do(frame, this, loc,
1438 priv->children[spb_child_index]->name);
1439 ret = 0;
1440 }
1441 out:
1442 /* key was correct but value was invalid when ret == 1 */
1443 if (ret == 1) {
1444 AFR_STACK_UNWIND(setxattr, frame, -1, op_errno, NULL);
1445 if (data)
1446 GF_FREE(data);
1447 ret = 0;
1448 }
1449 return ret;
1450 }
1451
1452 static int
1453 afr_handle_spb_choice_timeout(xlator_t *this, call_frame_t *frame, dict_t *dict)
1454 {
1455 int ret = -1;
1456 int op_errno = 0;
1457 uint64_t timeout = 0;
1458 afr_private_t *priv = NULL;
1459
1460 priv = this->private;
1461
1462 ret = dict_get_uint64(dict, GF_AFR_SPB_CHOICE_TIMEOUT, &timeout);
1463 if (!ret) {
1464 priv->spb_choice_timeout = timeout * 60;
1465 AFR_STACK_UNWIND(setxattr, frame, ret, op_errno, NULL);
1466 }
1467
1468 return ret;
1469 }
1470
1471 static int
1472 afr_handle_empty_brick(xlator_t *this, call_frame_t *frame, loc_t *loc,
1473 dict_t *dict)
1474 {
1475 int ret = -1;
1476 int ab_ret = -1;
1477 int empty_index = -1;
1478 int op_errno = EPERM;
1479 char *empty_brick = NULL;
1480 char *op_type = NULL;
1481 afr_empty_brick_args_t *data = NULL;
1482
1483 ret = dict_get_str_sizen(dict, GF_AFR_REPLACE_BRICK, &empty_brick);
1484 if (!ret)
1485 op_type = GF_AFR_REPLACE_BRICK;
1486
1487 ab_ret = dict_get_str_sizen(dict, GF_AFR_ADD_BRICK, &empty_brick);
1488 if (!ab_ret)
1489 op_type = GF_AFR_ADD_BRICK;
1490
1491 if (ret && ab_ret)
1492 goto out;
1493
1494 if (frame->root->pid != GF_CLIENT_PID_ADD_REPLICA_MOUNT) {
1495 gf_smsg(this->name, GF_LOG_ERROR, EPERM, AFR_MSG_INTERNAL_ATTR,
1496 "op_type=%s", op_type, NULL);
1497 ret = 1;
1498 goto out;
1499 }
1500 empty_index = afr_get_child_index_from_name(this, empty_brick);
1501
1502 if (empty_index < 0) {
1503 /* Didn't belong to this replica pair
1504 * Just do a no-op
1505 */
1506 AFR_STACK_UNWIND(setxattr, frame, 0, 0, NULL);
1507 return 0;
1508 } else {
1509 data = GF_CALLOC(1, sizeof(*data), gf_afr_mt_empty_brick_t);
1510 if (!data) {
1511 ret = 1;
1512 op_errno = ENOMEM;
1513 goto out;
1514 }
1515 data->frame = frame;
1516 loc_copy(&data->loc, loc);
1517 data->empty_index = empty_index;
1518 data->op_type = op_type;
1519 ret = synctask_new(this->ctx->env, _afr_handle_empty_brick,
1520 _afr_handle_empty_brick_cbk, NULL, data);
1521 if (ret) {
1522 gf_smsg(this->name, GF_LOG_ERROR, 0, AFR_MSG_SPLIT_BRAIN_STATUS,
1523 NULL);
1524 ret = 1;
1525 op_errno = ENOMEM;
1526 afr_brick_args_cleanup(data);
1527 goto out;
1528 }
1529 }
1530 ret = 0;
1531 out:
1532 if (ret == 1) {
1533 AFR_STACK_UNWIND(setxattr, frame, -1, op_errno, NULL);
1534 ret = 0;
1535 }
1536 return ret;
1537 }
1538
1539 static int
1540 afr_handle_special_xattr(xlator_t *this, call_frame_t *frame, loc_t *loc,
1541 dict_t *dict)
1542 {
1543 int ret = -1;
1544
1545 ret = afr_handle_split_brain_commands(this, frame, loc, dict);
1546 if (ret == 0)
1547 goto out;
1548
1549 ret = afr_handle_spb_choice_timeout(this, frame, dict);
1550 if (ret == 0)
1551 goto out;
1552
1553 /* Applicable for replace-brick and add-brick commands */
1554 ret = afr_handle_empty_brick(this, frame, loc, dict);
1555 out:
1556 return ret;
1557 }
1558
1559 int
1560 afr_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
1561 int32_t flags, dict_t *xdata)
1562 {
1563 afr_local_t *local = NULL;
1564 call_frame_t *transaction_frame = NULL;
1565 int ret = -1;
1566 int op_errno = EINVAL;
1567
1568 GF_IF_INTERNAL_XATTR_GOTO("trusted.afr.*", dict, op_errno, out);
1569
1570 GF_IF_INTERNAL_XATTR_GOTO("trusted.glusterfs.afr.*", dict, op_errno, out);
1571
1572 ret = afr_handle_special_xattr(this, frame, loc, dict);
1573 if (ret == 0)
1574 return 0;
1575
1576 transaction_frame = copy_frame(frame);
1577 if (!transaction_frame)
1578 goto out;
1579
1580 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1581 if (!local)
1582 goto out;
1583
1584 local->cont.setxattr.dict = dict_ref(dict);
1585 local->cont.setxattr.flags = flags;
1586 if (xdata)
1587 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1588 else
1589 local->xdata_req = dict_new();
1590
1591 if (!local->xdata_req)
1592 goto out;
1593
1594 local->transaction.wind = afr_setxattr_wind;
1595 local->transaction.unwind = afr_setxattr_unwind;
1596
1597 loc_copy(&local->loc, loc);
1598 ret = afr_set_inode_local(this, local, loc->inode);
1599 if (ret)
1600 goto out;
1601
1602 local->transaction.main_frame = frame;
1603 local->transaction.start = LLONG_MAX - 1;
1604 local->transaction.len = 0;
1605
1606 local->op = GF_FOP_SETXATTR;
1607
1608 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
1609 if (ret < 0) {
1610 op_errno = -ret;
1611 goto out;
1612 }
1613
1614 return 0;
1615 out:
1616 if (transaction_frame)
1617 AFR_STACK_DESTROY(transaction_frame);
1618
1619 AFR_STACK_UNWIND(setxattr, frame, -1, op_errno, NULL);
1620
1621 return 0;
1622 }
1623
1624 /* {{{ fsetxattr */
1625
1626 static int
1627 afr_fsetxattr_unwind(call_frame_t *frame, xlator_t *this)
1628 {
1629 afr_local_t *local = NULL;
1630 call_frame_t *main_frame = NULL;
1631
1632 local = frame->local;
1633
1634 main_frame = afr_transaction_detach_fop_frame(frame);
1635 if (!main_frame)
1636 return 0;
1637
1638 AFR_STACK_UNWIND(fsetxattr, main_frame, local->op_ret, local->op_errno,
1639 local->xdata_rsp);
1640 return 0;
1641 }
1642
1643 static int
1644 afr_fsetxattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1645 int32_t op_ret, int32_t op_errno, dict_t *xdata)
1646 {
1647 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1648 NULL, NULL, xdata);
1649 }
1650
1651 static int
1652 afr_fsetxattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
1653 {
1654 afr_local_t *local = NULL;
1655 afr_private_t *priv = NULL;
1656
1657 local = frame->local;
1658 priv = this->private;
1659
1660 STACK_WIND_COOKIE(frame, afr_fsetxattr_wind_cbk, (void *)(long)subvol,
1661 priv->children[subvol],
1662 priv->children[subvol]->fops->fsetxattr, local->fd,
1663 local->cont.fsetxattr.dict, local->cont.fsetxattr.flags,
1664 local->xdata_req);
1665 return 0;
1666 }
1667
1668 int
1669 afr_fsetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *dict,
1670 int32_t flags, dict_t *xdata)
1671 {
1672 afr_local_t *local = NULL;
1673 call_frame_t *transaction_frame = NULL;
1674 int ret = -1;
1675 int op_errno = ENOMEM;
1676
1677 GF_IF_INTERNAL_XATTR_GOTO("trusted.afr.*", dict, op_errno, out);
1678
1679 GF_IF_INTERNAL_XATTR_GOTO("trusted.glusterfs.afr.*", dict, op_errno, out);
1680
1681 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1682 transaction_frame = copy_frame(frame);
1683 if (!transaction_frame)
1684 goto out;
1685
1686 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1687 if (!local)
1688 goto out;
1689
1690 local->cont.fsetxattr.dict = dict_ref(dict);
1691 local->cont.fsetxattr.flags = flags;
1692
1693 if (xdata)
1694 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1695 else
1696 local->xdata_req = dict_new();
1697
1698 if (!local->xdata_req)
1699 goto out;
1700
1701 local->transaction.wind = afr_fsetxattr_wind;
1702 local->transaction.unwind = afr_fsetxattr_unwind;
1703
1704 local->fd = fd_ref(fd);
1705 ret = afr_set_inode_local(this, local, fd->inode);
1706 if (ret)
1707 goto out;
1708
1709 local->op = GF_FOP_FSETXATTR;
1710
1711 local->transaction.main_frame = frame;
1712 local->transaction.start = LLONG_MAX - 1;
1713 local->transaction.len = 0;
1714
1715 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
1716 if (ret < 0) {
1717 op_errno = -ret;
1718 goto out;
1719 }
1720
1721 return 0;
1722 out:
1723 if (transaction_frame)
1724 AFR_STACK_DESTROY(transaction_frame);
1725
1726 AFR_STACK_UNWIND(fsetxattr, frame, -1, op_errno, NULL);
1727 return 0;
1728 }
1729
1730 /* }}} */
1731
1732 /* {{{ removexattr */
1733
1734 static int
1735 afr_removexattr_unwind(call_frame_t *frame, xlator_t *this)
1736 {
1737 afr_local_t *local = NULL;
1738 call_frame_t *main_frame = NULL;
1739
1740 local = frame->local;
1741
1742 main_frame = afr_transaction_detach_fop_frame(frame);
1743 if (!main_frame)
1744 return 0;
1745
1746 AFR_STACK_UNWIND(removexattr, main_frame, local->op_ret, local->op_errno,
1747 local->xdata_rsp);
1748 return 0;
1749 }
1750
1751 int
1752 afr_removexattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1753 int32_t op_ret, int32_t op_errno, dict_t *xdata)
1754 {
1755 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1756 NULL, NULL, xdata);
1757 }
1758
1759 static int
1760 afr_removexattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
1761 {
1762 afr_local_t *local = NULL;
1763 afr_private_t *priv = NULL;
1764
1765 local = frame->local;
1766 priv = this->private;
1767
1768 STACK_WIND_COOKIE(frame, afr_removexattr_wind_cbk, (void *)(long)subvol,
1769 priv->children[subvol],
1770 priv->children[subvol]->fops->removexattr, &local->loc,
1771 local->cont.removexattr.name, local->xdata_req);
1772 return 0;
1773 }
1774
1775 int
1776 afr_removexattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
1777 const char *name, dict_t *xdata)
1778 {
1779 afr_local_t *local = NULL;
1780 call_frame_t *transaction_frame = NULL;
1781 int ret = -1;
1782 int op_errno = ENOMEM;
1783
1784 GF_IF_NATIVE_XATTR_GOTO("trusted.afr.*", name, op_errno, out);
1785
1786 GF_IF_NATIVE_XATTR_GOTO("trusted.glusterfs.afr.*", name, op_errno, out);
1787
1788 transaction_frame = copy_frame(frame);
1789 if (!transaction_frame)
1790 goto out;
1791
1792 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1793 if (!local)
1794 goto out;
1795
1796 local->cont.removexattr.name = gf_strdup(name);
1797
1798 if (xdata)
1799 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1800 else
1801 local->xdata_req = dict_new();
1802
1803 if (!local->xdata_req)
1804 goto out;
1805
1806 local->transaction.wind = afr_removexattr_wind;
1807 local->transaction.unwind = afr_removexattr_unwind;
1808
1809 loc_copy(&local->loc, loc);
1810 ret = afr_set_inode_local(this, local, loc->inode);
1811 if (ret)
1812 goto out;
1813
1814 local->op = GF_FOP_REMOVEXATTR;
1815
1816 local->transaction.main_frame = frame;
1817 local->transaction.start = LLONG_MAX - 1;
1818 local->transaction.len = 0;
1819
1820 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
1821 if (ret < 0) {
1822 op_errno = -ret;
1823 goto out;
1824 }
1825
1826 return 0;
1827 out:
1828 if (transaction_frame)
1829 AFR_STACK_DESTROY(transaction_frame);
1830
1831 AFR_STACK_UNWIND(removexattr, frame, -1, op_errno, NULL);
1832 return 0;
1833 }
1834
1835 /* ffremovexattr */
1836 static int
1837 afr_fremovexattr_unwind(call_frame_t *frame, xlator_t *this)
1838 {
1839 afr_local_t *local = NULL;
1840 call_frame_t *main_frame = NULL;
1841
1842 local = frame->local;
1843
1844 main_frame = afr_transaction_detach_fop_frame(frame);
1845 if (!main_frame)
1846 return 0;
1847
1848 AFR_STACK_UNWIND(fremovexattr, main_frame, local->op_ret, local->op_errno,
1849 local->xdata_rsp);
1850 return 0;
1851 }
1852
1853 static int
1854 afr_fremovexattr_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1855 int32_t op_ret, int32_t op_errno, dict_t *xdata)
1856 {
1857 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1858 NULL, NULL, xdata);
1859 }
1860
1861 static int
1862 afr_fremovexattr_wind(call_frame_t *frame, xlator_t *this, int subvol)
1863 {
1864 afr_local_t *local = NULL;
1865 afr_private_t *priv = NULL;
1866
1867 local = frame->local;
1868 priv = this->private;
1869
1870 STACK_WIND_COOKIE(frame, afr_fremovexattr_wind_cbk, (void *)(long)subvol,
1871 priv->children[subvol],
1872 priv->children[subvol]->fops->fremovexattr, local->fd,
1873 local->cont.removexattr.name, local->xdata_req);
1874 return 0;
1875 }
1876
1877 int
1878 afr_fremovexattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
1879 const char *name, dict_t *xdata)
1880 {
1881 afr_local_t *local = NULL;
1882 call_frame_t *transaction_frame = NULL;
1883 int ret = -1;
1884 int op_errno = ENOMEM;
1885
1886 GF_IF_NATIVE_XATTR_GOTO("trusted.afr.*", name, op_errno, out);
1887
1888 GF_IF_NATIVE_XATTR_GOTO("trusted.glusterfs.afr.*", name, op_errno, out);
1889
1890 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1891 transaction_frame = copy_frame(frame);
1892 if (!transaction_frame)
1893 goto out;
1894
1895 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1896 if (!local)
1897 goto out;
1898
1899 local->cont.removexattr.name = gf_strdup(name);
1900 if (xdata)
1901 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1902 else
1903 local->xdata_req = dict_new();
1904
1905 if (!local->xdata_req)
1906 goto out;
1907
1908 local->transaction.wind = afr_fremovexattr_wind;
1909 local->transaction.unwind = afr_fremovexattr_unwind;
1910
1911 local->fd = fd_ref(fd);
1912 ret = afr_set_inode_local(this, local, fd->inode);
1913 if (ret)
1914 goto out;
1915
1916 local->op = GF_FOP_FREMOVEXATTR;
1917
1918 local->transaction.main_frame = frame;
1919 local->transaction.start = LLONG_MAX - 1;
1920 local->transaction.len = 0;
1921
1922 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
1923 if (ret < 0) {
1924 op_errno = -ret;
1925 goto out;
1926 }
1927
1928 return 0;
1929 out:
1930 if (transaction_frame)
1931 AFR_STACK_DESTROY(transaction_frame);
1932
1933 AFR_STACK_UNWIND(fremovexattr, frame, -1, op_errno, NULL);
1934
1935 return 0;
1936 }
1937
1938 static int
1939 afr_fallocate_unwind(call_frame_t *frame, xlator_t *this)
1940 {
1941 afr_local_t *local = NULL;
1942 call_frame_t *main_frame = NULL;
1943
1944 local = frame->local;
1945
1946 main_frame = afr_transaction_detach_fop_frame(frame);
1947 if (!main_frame)
1948 return 0;
1949
1950 AFR_STACK_UNWIND(fallocate, main_frame, local->op_ret, local->op_errno,
1951 &local->cont.inode_wfop.prebuf,
1952 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
1953 return 0;
1954 }
1955
1956 static int
1957 afr_fallocate_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1958 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
1959 struct iatt *postbuf, dict_t *xdata)
1960 {
1961 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
1962 postbuf, NULL, xdata);
1963 }
1964
1965 static int
1966 afr_fallocate_wind(call_frame_t *frame, xlator_t *this, int subvol)
1967 {
1968 afr_local_t *local = NULL;
1969 afr_private_t *priv = NULL;
1970
1971 local = frame->local;
1972 priv = this->private;
1973
1974 STACK_WIND_COOKIE(frame, afr_fallocate_wind_cbk, (void *)(long)subvol,
1975 priv->children[subvol],
1976 priv->children[subvol]->fops->fallocate, local->fd,
1977 local->cont.fallocate.mode, local->cont.fallocate.offset,
1978 local->cont.fallocate.len, local->xdata_req);
1979 return 0;
1980 }
1981
1982 int
1983 afr_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t mode,
1984 off_t offset, size_t len, dict_t *xdata)
1985 {
1986 call_frame_t *transaction_frame = NULL;
1987 afr_local_t *local = NULL;
1988 int ret = -1;
1989 int op_errno = ENOMEM;
1990
1991 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
1992 transaction_frame = copy_frame(frame);
1993 if (!transaction_frame)
1994 goto out;
1995
1996 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1997 if (!local)
1998 goto out;
1999
2000 local->cont.fallocate.mode = mode;
2001 local->cont.fallocate.offset = offset;
2002 local->cont.fallocate.len = len;
2003
2004 local->fd = fd_ref(fd);
2005 ret = afr_set_inode_local(this, local, fd->inode);
2006 if (ret)
2007 goto out;
2008
2009 if (xdata)
2010 local->xdata_req = dict_copy_with_ref(xdata, NULL);
2011 else
2012 local->xdata_req = dict_new();
2013
2014 if (!local->xdata_req)
2015 goto out;
2016
2017 local->op = GF_FOP_FALLOCATE;
2018
2019 local->transaction.wind = afr_fallocate_wind;
2020 local->transaction.unwind = afr_fallocate_unwind;
2021
2022 local->transaction.main_frame = frame;
2023
2024 local->transaction.start = local->cont.fallocate.offset;
2025 local->transaction.len = 0;
2026
2027 afr_fix_open(fd, this);
2028
2029 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
2030 if (ret < 0) {
2031 op_errno = -ret;
2032 goto out;
2033 }
2034
2035 return 0;
2036 out:
2037 if (transaction_frame)
2038 AFR_STACK_DESTROY(transaction_frame);
2039
2040 AFR_STACK_UNWIND(fallocate, frame, -1, op_errno, NULL, NULL, NULL);
2041 return 0;
2042 }
2043
2044 /* }}} */
2045
2046 /* {{{ discard */
2047
2048 static int
2049 afr_discard_unwind(call_frame_t *frame, xlator_t *this)
2050 {
2051 afr_local_t *local = NULL;
2052 call_frame_t *main_frame = NULL;
2053
2054 local = frame->local;
2055
2056 main_frame = afr_transaction_detach_fop_frame(frame);
2057 if (!main_frame)
2058 return 0;
2059
2060 AFR_STACK_UNWIND(discard, main_frame, local->op_ret, local->op_errno,
2061 &local->cont.inode_wfop.prebuf,
2062 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
2063 return 0;
2064 }
2065
2066 static int
2067 afr_discard_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
2068 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
2069 struct iatt *postbuf, dict_t *xdata)
2070 {
2071 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
2072 postbuf, NULL, xdata);
2073 }
2074
2075 static int
2076 afr_discard_wind(call_frame_t *frame, xlator_t *this, int subvol)
2077 {
2078 afr_local_t *local = NULL;
2079 afr_private_t *priv = NULL;
2080
2081 local = frame->local;
2082 priv = this->private;
2083
2084 STACK_WIND_COOKIE(frame, afr_discard_wind_cbk, (void *)(long)subvol,
2085 priv->children[subvol],
2086 priv->children[subvol]->fops->discard, local->fd,
2087 local->cont.discard.offset, local->cont.discard.len,
2088 local->xdata_req);
2089 return 0;
2090 }
2091
2092 int
2093 afr_discard(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
2094 size_t len, dict_t *xdata)
2095 {
2096 afr_local_t *local = NULL;
2097 call_frame_t *transaction_frame = NULL;
2098 int ret = -1;
2099 int op_errno = ENOMEM;
2100
2101 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
2102 transaction_frame = copy_frame(frame);
2103 if (!transaction_frame)
2104 goto out;
2105
2106 local = AFR_FRAME_INIT(transaction_frame, op_errno);
2107 if (!local)
2108 goto out;
2109
2110 local->cont.discard.offset = offset;
2111 local->cont.discard.len = len;
2112
2113 local->fd = fd_ref(fd);
2114 ret = afr_set_inode_local(this, local, fd->inode);
2115 if (ret)
2116 goto out;
2117
2118 if (xdata)
2119 local->xdata_req = dict_copy_with_ref(xdata, NULL);
2120 else
2121 local->xdata_req = dict_new();
2122
2123 if (!local->xdata_req)
2124 goto out;
2125
2126 local->op = GF_FOP_DISCARD;
2127
2128 local->transaction.wind = afr_discard_wind;
2129 local->transaction.unwind = afr_discard_unwind;
2130
2131 local->transaction.main_frame = frame;
2132
2133 local->transaction.start = local->cont.discard.offset;
2134 local->transaction.len = 0;
2135
2136 afr_fix_open(fd, this);
2137
2138 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
2139 if (ret < 0) {
2140 op_errno = -ret;
2141 goto out;
2142 }
2143
2144 return 0;
2145 out:
2146 if (transaction_frame)
2147 AFR_STACK_DESTROY(transaction_frame);
2148
2149 AFR_STACK_UNWIND(discard, frame, -1, op_errno, NULL, NULL, NULL);
2150 return 0;
2151 }
2152
2153 /* {{{ zerofill */
2154
2155 static int
2156 afr_zerofill_unwind(call_frame_t *frame, xlator_t *this)
2157 {
2158 afr_local_t *local = NULL;
2159 call_frame_t *main_frame = NULL;
2160
2161 local = frame->local;
2162
2163 main_frame = afr_transaction_detach_fop_frame(frame);
2164 if (!main_frame)
2165 return 0;
2166
2167 AFR_STACK_UNWIND(discard, main_frame, local->op_ret, local->op_errno,
2168 &local->cont.inode_wfop.prebuf,
2169 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
2170 return 0;
2171 }
2172
2173 static int
2174 afr_zerofill_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
2175 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
2176 struct iatt *postbuf, dict_t *xdata)
2177 {
2178 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
2179 postbuf, NULL, xdata);
2180 }
2181
2182 static int
2183 afr_zerofill_wind(call_frame_t *frame, xlator_t *this, int subvol)
2184 {
2185 afr_local_t *local = NULL;
2186 afr_private_t *priv = NULL;
2187
2188 local = frame->local;
2189 priv = this->private;
2190
2191 STACK_WIND_COOKIE(frame, afr_zerofill_wind_cbk, (void *)(long)subvol,
2192 priv->children[subvol],
2193 priv->children[subvol]->fops->zerofill, local->fd,
2194 local->cont.zerofill.offset, local->cont.zerofill.len,
2195 local->xdata_req);
2196 return 0;
2197 }
2198
2199 int
2200 afr_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
2201 size_t len, dict_t *xdata)
2202 {
2203 afr_local_t *local = NULL;
2204 call_frame_t *transaction_frame = NULL;
2205 int ret = -1;
2206 int op_errno = ENOMEM;
2207
2208 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
2209 transaction_frame = copy_frame(frame);
2210 if (!transaction_frame)
2211 goto out;
2212
2213 local = AFR_FRAME_INIT(transaction_frame, op_errno);
2214 if (!local)
2215 goto out;
2216
2217 local->cont.zerofill.offset = offset;
2218 local->cont.zerofill.len = len;
2219
2220 local->fd = fd_ref(fd);
2221 ret = afr_set_inode_local(this, local, fd->inode);
2222 if (ret)
2223 goto out;
2224
2225 if (xdata)
2226 local->xdata_req = dict_copy_with_ref(xdata, NULL);
2227 else
2228 local->xdata_req = dict_new();
2229
2230 if (!local->xdata_req)
2231 goto out;
2232
2233 local->op = GF_FOP_ZEROFILL;
2234
2235 local->transaction.wind = afr_zerofill_wind;
2236 local->transaction.unwind = afr_zerofill_unwind;
2237
2238 local->transaction.main_frame = frame;
2239
2240 local->transaction.start = local->cont.zerofill.offset;
2241 local->transaction.len = len;
2242
2243 afr_fix_open(fd, this);
2244
2245 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
2246 if (ret < 0) {
2247 op_errno = -ret;
2248 goto out;
2249 }
2250
2251 return 0;
2252 out:
2253 if (transaction_frame)
2254 AFR_STACK_DESTROY(transaction_frame);
2255
2256 AFR_STACK_UNWIND(zerofill, frame, -1, op_errno, NULL, NULL, NULL);
2257 return 0;
2258 }
2259
2260 /* }}} */
2261
2262 static int32_t
2263 afr_xattrop_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
2264 int32_t op_ret, int32_t op_errno, dict_t *xattr,
2265 dict_t *xdata)
2266 {
2267 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
2268 NULL, xattr, xdata);
2269 }
2270
2271 static int
2272 afr_xattrop_wind(call_frame_t *frame, xlator_t *this, int subvol)
2273 {
2274 afr_local_t *local = NULL;
2275 afr_private_t *priv = NULL;
2276
2277 local = frame->local;
2278 priv = this->private;
2279
2280 STACK_WIND_COOKIE(frame, afr_xattrop_wind_cbk, (void *)(long)subvol,
2281 priv->children[subvol],
2282 priv->children[subvol]->fops->xattrop, &local->loc,
2283 local->cont.xattrop.optype, local->cont.xattrop.xattr,
2284 local->xdata_req);
2285 return 0;
2286 }
2287
2288 static int
2289 afr_xattrop_unwind(call_frame_t *frame, xlator_t *this)
2290 {
2291 afr_local_t *local = NULL;
2292 call_frame_t *main_frame = NULL;
2293
2294 local = frame->local;
2295
2296 main_frame = afr_transaction_detach_fop_frame(frame);
2297 if (!main_frame)
2298 return 0;
2299
2300 AFR_STACK_UNWIND(xattrop, main_frame, local->op_ret, local->op_errno,
2301 local->xattr_rsp, local->xdata_rsp);
2302 return 0;
2303 }
2304
2305 int32_t
2306 afr_xattrop(call_frame_t *frame, xlator_t *this, loc_t *loc,
2307 gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata)
2308 {
2309 afr_local_t *local = NULL;
2310 call_frame_t *transaction_frame = NULL;
2311 int ret = -1;
2312 int op_errno = ENOMEM;
2313
2314 transaction_frame = copy_frame(frame);
2315 if (!transaction_frame)
2316 goto out;
2317
2318 local = AFR_FRAME_INIT(transaction_frame, op_errno);
2319 if (!local)
2320 goto out;
2321
2322 local->cont.xattrop.xattr = dict_ref(xattr);
2323 local->cont.xattrop.optype = optype;
2324 if (xdata)
2325 local->xdata_req = dict_ref(xdata);
2326
2327 local->transaction.wind = afr_xattrop_wind;
2328 local->transaction.unwind = afr_xattrop_unwind;
2329
2330 loc_copy(&local->loc, loc);
2331 ret = afr_set_inode_local(this, local, loc->inode);
2332 if (ret)
2333 goto out;
2334
2335 local->op = GF_FOP_XATTROP;
2336
2337 local->transaction.main_frame = frame;
2338 local->transaction.start = LLONG_MAX - 1;
2339 local->transaction.len = 0;
2340
2341 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
2342 if (ret < 0) {
2343 op_errno = -ret;
2344 goto out;
2345 }
2346
2347 return 0;
2348 out:
2349 if (transaction_frame)
2350 AFR_STACK_DESTROY(transaction_frame);
2351
2352 AFR_STACK_UNWIND(xattrop, frame, -1, op_errno, NULL, NULL);
2353 return 0;
2354 }
2355
2356 int32_t
2357 afr_fxattrop_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
2358 int32_t op_ret, int32_t op_errno, dict_t *xattr,
2359 dict_t *xdata)
2360 {
2361 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
2362 NULL, xattr, xdata);
2363 }
2364
2365 int
2366 afr_fxattrop_wind(call_frame_t *frame, xlator_t *this, int subvol)
2367 {
2368 afr_local_t *local = NULL;
2369 afr_private_t *priv = NULL;
2370
2371 local = frame->local;
2372 priv = this->private;
2373
2374 STACK_WIND_COOKIE(frame, afr_fxattrop_wind_cbk, (void *)(long)subvol,
2375 priv->children[subvol],
2376 priv->children[subvol]->fops->fxattrop, local->fd,
2377 local->cont.xattrop.optype, local->cont.xattrop.xattr,
2378 local->xdata_req);
2379 return 0;
2380 }
2381
2382 int
2383 afr_fxattrop_unwind(call_frame_t *frame, xlator_t *this)
2384 {
2385 afr_local_t *local = NULL;
2386 call_frame_t *main_frame = NULL;
2387
2388 local = frame->local;
2389
2390 main_frame = afr_transaction_detach_fop_frame(frame);
2391 if (!main_frame)
2392 return 0;
2393
2394 AFR_STACK_UNWIND(fxattrop, main_frame, local->op_ret, local->op_errno,
2395 local->xattr_rsp, local->xdata_rsp);
2396 return 0;
2397 }
2398
2399 int32_t
2400 afr_fxattrop(call_frame_t *frame, xlator_t *this, fd_t *fd,
2401 gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata)
2402 {
2403 afr_local_t *local = NULL;
2404 call_frame_t *transaction_frame = NULL;
2405 int ret = -1;
2406 int op_errno = ENOMEM;
2407
2408 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
2409 transaction_frame = copy_frame(frame);
2410 if (!transaction_frame)
2411 goto out;
2412
2413 local = AFR_FRAME_INIT(transaction_frame, op_errno);
2414 if (!local)
2415 goto out;
2416
2417 local->cont.xattrop.xattr = dict_ref(xattr);
2418 local->cont.xattrop.optype = optype;
2419 if (xdata)
2420 local->xdata_req = dict_ref(xdata);
2421
2422 local->transaction.wind = afr_fxattrop_wind;
2423 local->transaction.unwind = afr_fxattrop_unwind;
2424
2425 local->fd = fd_ref(fd);
2426 ret = afr_set_inode_local(this, local, fd->inode);
2427 if (ret)
2428 goto out;
2429
2430 local->op = GF_FOP_FXATTROP;
2431
2432 local->transaction.main_frame = frame;
2433 local->transaction.start = LLONG_MAX - 1;
2434 local->transaction.len = 0;
2435
2436 ret = afr_transaction(transaction_frame, this, AFR_METADATA_TRANSACTION);
2437 if (ret < 0) {
2438 op_errno = -ret;
2439 goto out;
2440 }
2441
2442 return 0;
2443 out:
2444 if (transaction_frame)
2445 AFR_STACK_DESTROY(transaction_frame);
2446
2447 AFR_STACK_UNWIND(fxattrop, frame, -1, op_errno, NULL, NULL);
2448 return 0;
2449 }
2450
2451 static int
2452 afr_fsync_unwind(call_frame_t *frame, xlator_t *this)
2453 {
2454 afr_local_t *local = NULL;
2455 call_frame_t *main_frame = NULL;
2456
2457 local = frame->local;
2458
2459 main_frame = afr_transaction_detach_fop_frame(frame);
2460 if (!main_frame)
2461 return 0;
2462
2463 AFR_STACK_UNWIND(fsync, main_frame, local->op_ret, local->op_errno,
2464 &local->cont.inode_wfop.prebuf,
2465 &local->cont.inode_wfop.postbuf, local->xdata_rsp);
2466
2467 return 0;
2468 }
2469
2470 int
2471 afr_fsync_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
2472 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
2473 struct iatt *postbuf, dict_t *xdata)
2474 {
2475 return __afr_inode_write_cbk(frame, cookie, this, op_ret, op_errno, prebuf,
2476 postbuf, NULL, xdata);
2477 }
2478
2479 static int
2480 afr_fsync_wind(call_frame_t *frame, xlator_t *this, int subvol)
2481 {
2482 afr_local_t *local = NULL;
2483 afr_private_t *priv = NULL;
2484
2485 local = frame->local;
2486 priv = this->private;
2487
2488 STACK_WIND_COOKIE(frame, afr_fsync_wind_cbk, (void *)(long)subvol,
2489 priv->children[subvol],
2490 priv->children[subvol]->fops->fsync, local->fd,
2491 local->cont.fsync.datasync, local->xdata_req);
2492 return 0;
2493 }
2494
2495 int
2496 afr_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
2497 dict_t *xdata)
2498 {
2499 afr_local_t *local = NULL;
2500 call_frame_t *transaction_frame = NULL;
2501 int ret = -1;
2502 int32_t op_errno = ENOMEM;
2503 int8_t last_fsync = 0;
2504
2505 AFR_ERROR_OUT_IF_FDCTX_INVALID(fd, this, op_errno, out);
2506 transaction_frame = copy_frame(frame);
2507 if (!transaction_frame)
2508 goto out;
2509
2510 local = AFR_FRAME_INIT(transaction_frame, op_errno);
2511 if (!local)
2512 goto out;
2513
2514 if (xdata) {
2515 local->xdata_req = dict_copy_with_ref(xdata, NULL);
2516 if (dict_get_int8(xdata, "last-fsync", &last_fsync) == 0) {
2517 if (last_fsync) {
2518 local->transaction.disable_delayed_post_op = _gf_true;
2519 }
2520 }
2521 } else {
2522 local->xdata_req = dict_new();
2523 }
2524
2525 if (!local->xdata_req)
2526 goto out;
2527
2528 if (dict_set_str_sizen(local->xdata_req, GLUSTERFS_INODELK_DOM_COUNT,
2529 this->name)) {
2530 op_errno = ENOMEM;
2531 goto out;
2532 }
2533
2534 local->fd = fd_ref(fd);
2535 ret = afr_set_inode_local(this, local, fd->inode);
2536 if (ret)
2537 goto out;
2538
2539 local->op = GF_FOP_FSYNC;
2540 local->cont.fsync.datasync = datasync;
2541
2542 if (afr_fd_has_witnessed_unstable_write(this, fd->inode)) {
2543 /* don't care. we only wanted to CLEAR the bit */
2544 }
2545
2546 local->transaction.wind = afr_fsync_wind;
2547 local->transaction.unwind = afr_fsync_unwind;
2548
2549 local->transaction.main_frame = frame;
2550
2551 ret = afr_transaction(transaction_frame, this, AFR_DATA_TRANSACTION);
2552 if (ret < 0) {
2553 op_errno = -ret;
2554 goto out;
2555 }
2556
2557 return 0;
2558 out:
2559 if (transaction_frame)
2560 AFR_STACK_DESTROY(transaction_frame);
2561
2562 AFR_STACK_UNWIND(fsync, frame, -1, op_errno, NULL, NULL, NULL);
2563
2564 return 0;
2565 }