Debian Codegraph @

xlators/cluster/afr/src/afr-dir-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 <libgen.h>
12 #include <unistd.h>
13 #include <sys/time.h>
14 #include <stdlib.h>
15 #include <signal.h>
16
17 #include "afr.h"
18 #include <glusterfs/dict.h>
19 #include <glusterfs/logging.h>
20 #include <glusterfs/list.h>
21 #include <glusterfs/compat-errno.h>
22 #include <glusterfs/compat.h>
23
24 #include "afr-transaction.h"
25
26 static void
27 afr_mark_entry_pending_changelog(call_frame_t *frame, xlator_t *this);
28
29 static int
30 afr_build_parent_loc(loc_t *parent, loc_t *child, int32_t *op_errno)
31 {
32 int ret = -1;
33 char *child_path = NULL;
34
35 if (!child->parent) {
36 if (op_errno)
37 *op_errno = EINVAL;
38 goto out;
39 }
40
41 child_path = gf_strdup(child->path);
42 if (!child_path) {
43 if (op_errno)
44 *op_errno = ENOMEM;
45 goto out;
46 }
47
48 parent->path = gf_strdup(dirname(child_path));
49 if (!parent->path) {
50 if (op_errno)
51 *op_errno = ENOMEM;
52 goto out;
53 }
54
55 parent->inode = inode_ref(child->parent);
56 gf_uuid_copy(parent->gfid, child->pargfid);
57
58 ret = 0;
59 out:
60 GF_FREE(child_path);
61
62 return ret;
63 }
64
65 static void
66 __afr_dir_write_finalize(afr_local_t *local, xlator_t *this)
67 {
68 afr_private_t *priv = NULL;
69 int inode_read_subvol = -1;
70 int parent_read_subvol = -1;
71 int parent2_read_subvol = -1;
72 int i = 0;
73 afr_read_subvol_args_t args = {
74 0,
75 };
76
77 priv = this->private;
78
79 for (i = 0; i < priv->child_count; i++) {
80 if (!local->replies[i].valid)
81 continue;
82 if (local->replies[i].op_ret == -1)
83 continue;
84 gf_uuid_copy(args.gfid, local->replies[i].poststat.ia_gfid);
85 args.ia_type = local->replies[i].poststat.ia_type;
86 break;
87 }
88
89 if (local->inode) {
90 if (local->op != GF_FOP_RENAME && local->op != GF_FOP_LINK)
91 afr_replies_interpret(local, this, local->inode, NULL);
92
93 inode_read_subvol = afr_data_subvol_get(local->inode, this, NULL, NULL,
94 NULL, &args);
95 }
96
97 if (local->parent)
98 parent_read_subvol = afr_data_subvol_get(local->parent, this, NULL,
99 local->readable, NULL, NULL);
100
101 if (local->parent2)
102 parent2_read_subvol = afr_data_subvol_get(local->parent2, this, NULL,
103 local->readable2, NULL, NULL);
104
105 local->op_ret = -1;
106 local->op_errno = afr_final_errno(local, priv);
107 afr_pick_error_xdata(local, priv->child_count, local->parent,
108 local->readable, local->parent2, local->readable2);
109
110 for (i = 0; i < priv->child_count; i++) {
111 if (!local->replies[i].valid)
112 continue;
113 if (local->replies[i].op_ret < 0) {
114 if (local->inode)
115 afr_inode_need_refresh_set(local->inode, this);
116 if (local->parent)
117 afr_inode_need_refresh_set(local->parent, this);
118 if (local->parent2)
119 afr_inode_need_refresh_set(local->parent2, this);
120 continue;
121 }
122
123 if (local->op_ret == -1) {
124 local->op_ret = local->replies[i].op_ret;
125 local->op_errno = local->replies[i].op_errno;
126
127 local->cont.dir_fop.buf = local->replies[i].poststat;
128 local->cont.dir_fop.preparent = local->replies[i].preparent;
129 local->cont.dir_fop.postparent = local->replies[i].postparent;
130 local->cont.dir_fop.prenewparent = local->replies[i].preparent2;
131 local->cont.dir_fop.postnewparent = local->replies[i].postparent2;
132 if (local->xdata_rsp) {
133 dict_unref(local->xdata_rsp);
134 local->xdata_rsp = NULL;
135 }
136
137 if (local->replies[i].xdata)
138 local->xdata_rsp = dict_ref(local->replies[i].xdata);
139 continue;
140 }
141
142 if (i == inode_read_subvol) {
143 local->cont.dir_fop.buf = local->replies[i].poststat;
144 if (local->replies[i].xdata) {
145 if (local->xdata_rsp)
146 dict_unref(local->xdata_rsp);
147 local->xdata_rsp = dict_ref(local->replies[i].xdata);
148 }
149 }
150
151 if (i == parent_read_subvol) {
152 local->cont.dir_fop.preparent = local->replies[i].preparent;
153 local->cont.dir_fop.postparent = local->replies[i].postparent;
154 }
155
156 if (i == parent2_read_subvol) {
157 local->cont.dir_fop.prenewparent = local->replies[i].preparent2;
158 local->cont.dir_fop.postnewparent = local->replies[i].postparent2;
159 }
160 }
161 }
162
163 static void
164 __afr_dir_write_fill(afr_local_t *local, xlator_t *this, int child_index,
165 int op_ret, int op_errno, struct iatt *poststat,
166 struct iatt *preparent, struct iatt *postparent,
167 struct iatt *preparent2, struct iatt *postparent2,
168 dict_t *xdata)
169 {
170 afr_fd_ctx_t *fd_ctx = NULL;
171
172 fd_ctx = local->fd_ctx;
173
174 local->replies[child_index].valid = 1;
175 local->replies[child_index].op_ret = op_ret;
176 local->replies[child_index].op_errno = op_errno;
177 if (xdata)
178 local->replies[child_index].xdata = dict_ref(xdata);
179
180 if (op_ret >= 0) {
181 if (poststat)
182 local->replies[child_index].poststat = *poststat;
183 if (preparent)
184 local->replies[child_index].preparent = *preparent;
185 if (postparent)
186 local->replies[child_index].postparent = *postparent;
187 if (preparent2)
188 local->replies[child_index].preparent2 = *preparent2;
189 if (postparent2)
190 local->replies[child_index].postparent2 = *postparent2;
191 if (fd_ctx)
192 fd_ctx->opened_on[child_index] = AFR_FD_OPENED;
193 } else {
194 if (op_errno != ENOTEMPTY)
195 afr_transaction_fop_failed(local, child_index);
196 if (fd_ctx)
197 fd_ctx->opened_on[child_index] = AFR_FD_NOT_OPENED;
198 }
199
200 return;
201 }
202
203 static int
204 __afr_dir_write_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
205 int op_ret, int op_errno, struct iatt *buf,
206 struct iatt *preparent, struct iatt *postparent,
207 struct iatt *preparent2, struct iatt *postparent2,
208 dict_t *xdata)
209 {
210 afr_local_t *local = NULL;
211 int child_index = (long)cookie;
212 int call_count = -1;
213 afr_private_t *priv = NULL;
214
215 priv = this->private;
216 local = frame->local;
217
218 LOCK(&frame->lock);
219 {
220 __afr_dir_write_fill(local, this, child_index, op_ret, op_errno, buf,
221 preparent, postparent, preparent2, postparent2,
222 xdata);
223 call_count = --local->call_count;
224 }
225 UNLOCK(&frame->lock);
226
227 if (call_count == 0) {
228 __afr_dir_write_finalize(local, this);
229
230 if (afr_txn_nothing_failed(frame, this)) {
231 /*if it did pre-op, it will do post-op changing ctime*/
232 if (priv->consistent_metadata && afr_needs_changelog_update(local))
233 afr_zero_fill_stat(local);
234 local->transaction.unwind(frame, this);
235 }
236
237 afr_mark_entry_pending_changelog(frame, this);
238
239 afr_transaction_resume(frame, this);
240 }
241
242 return 0;
243 }
244
245 static int
246 afr_mark_new_entry_changelog_cbk(call_frame_t *frame, void *cookie,
247 xlator_t *this, int op_ret, int op_errno,
248 dict_t *xattr, dict_t *xdata)
249 {
250 int call_count = 0;
251
252 call_count = afr_frame_return(frame);
253
254 if (call_count == 0)
255 AFR_STACK_DESTROY(frame);
256
257 return 0;
258 }
259
260 void
261 afr_mark_new_entry_changelog(call_frame_t *frame, xlator_t *this)
262 {
263 call_frame_t *new_frame = NULL;
264 afr_local_t *local = NULL;
265 afr_local_t *new_local = NULL;
266 afr_private_t *priv = NULL;
267 dict_t *xattr = NULL;
268 int32_t **changelog = NULL;
269 int i = 0;
270 int op_errno = ENOMEM;
271 unsigned char *pending = NULL;
272 int call_count = 0;
273
274 local = frame->local;
275 priv = this->private;
276
277 new_frame = copy_frame(frame);
278 if (!new_frame)
279 goto out;
280
281 new_local = AFR_FRAME_INIT(new_frame, op_errno);
282 if (!new_local)
283 goto out;
284
285 xattr = dict_new();
286 if (!xattr)
287 goto out;
288
289 pending = alloca0(priv->child_count);
290
291 for (i = 0; i < priv->child_count; i++) {
292 if (local->transaction.pre_op[i] &&
293 !local->transaction.failed_subvols[i]) {
294 call_count++;
295 continue;
296 }
297 pending[i] = 1;
298 }
299
300 changelog = afr_mark_pending_changelog(priv, pending, xattr,
301 local->cont.dir_fop.buf.ia_type);
302 if (!changelog)
303 goto out;
304
305 new_local->pending = changelog;
306 gf_uuid_copy(new_local->loc.gfid, local->cont.dir_fop.buf.ia_gfid);
307 new_local->loc.inode = inode_ref(local->inode);
308
309 new_local->call_count = call_count;
310
311 for (i = 0; i < priv->child_count; i++) {
312 if (pending[i])
313 continue;
314
315 STACK_WIND_COOKIE(new_frame, afr_mark_new_entry_changelog_cbk,
316 (void *)(long)i, priv->children[i],
317 priv->children[i]->fops->xattrop, &new_local->loc,
318 GF_XATTROP_ADD_ARRAY, xattr, NULL);
319 if (!--call_count)
320 break;
321 }
322
323 new_frame = NULL;
324 out:
325 if (new_frame)
326 AFR_STACK_DESTROY(new_frame);
327 if (xattr)
328 dict_unref(xattr);
329 return;
330 }
331
332 static void
333 afr_mark_entry_pending_changelog(call_frame_t *frame, xlator_t *this)
334 {
335 afr_local_t *local = NULL;
336 afr_private_t *priv = NULL;
337 int pre_op_count = 0;
338 int failed_count = 0;
339 unsigned char *success_replies = NULL;
340
341 local = frame->local;
342 priv = this->private;
343
344 if (local->op_ret < 0)
345 return;
346
347 if (local->op != GF_FOP_CREATE && local->op != GF_FOP_MKNOD &&
348 local->op != GF_FOP_MKDIR)
349 return;
350
351 pre_op_count = AFR_COUNT(local->transaction.pre_op, priv->child_count);
352 failed_count = AFR_COUNT(local->transaction.failed_subvols,
353 priv->child_count);
354
355 /* FOP succeeded on all bricks. */
356 if (pre_op_count == priv->child_count && !failed_count)
357 return;
358
359 /* FOP did not suceed on quorum no. of bricks. */
360 success_replies = alloca0(priv->child_count);
361 afr_fill_success_replies(local, priv, success_replies);
362 if (!afr_has_quorum(success_replies, priv, NULL))
363 return;
364
365 if (priv->thin_arbiter_count) {
366 /*Mark new entry using ta file*/
367 local->is_new_entry = _gf_true;
368 return;
369 }
370
371 afr_mark_new_entry_changelog(frame, this);
372
373 return;
374 }
375
376 /* {{{ create */
377
378 static int
379 afr_create_unwind(call_frame_t *frame, xlator_t *this)
380 {
381 call_frame_t *main_frame = NULL;
382 afr_local_t *local = NULL;
383
384 local = frame->local;
385
386 main_frame = afr_transaction_detach_fop_frame(frame);
387
388 if (!main_frame)
389 return 0;
390
391 AFR_STACK_UNWIND(create, main_frame, local->op_ret, local->op_errno,
392 local->cont.create.fd, local->inode,
393 &local->cont.dir_fop.buf, &local->cont.dir_fop.preparent,
394 &local->cont.dir_fop.postparent, local->xdata_rsp);
395 return 0;
396 }
397
398 static int
399 afr_create_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
400 int32_t op_ret, int32_t op_errno, fd_t *fd, inode_t *inode,
401 struct iatt *buf, struct iatt *preparent,
402 struct iatt *postparent, dict_t *xdata)
403 {
404 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
405 preparent, postparent, NULL, NULL, xdata);
406 }
407
408 static int
409 afr_create_wind(call_frame_t *frame, xlator_t *this, int subvol)
410 {
411 afr_local_t *local = NULL;
412 afr_private_t *priv = NULL;
413
414 local = frame->local;
415 priv = this->private;
416
417 STACK_WIND_COOKIE(frame, afr_create_wind_cbk, (void *)(long)subvol,
418 priv->children[subvol],
419 priv->children[subvol]->fops->create, &local->loc,
420 local->cont.create.flags, local->cont.create.mode,
421 local->umask, local->cont.create.fd, local->xdata_req);
422 return 0;
423 }
424
425 int
426 afr_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
427 mode_t mode, mode_t umask, fd_t *fd, dict_t *xdata)
428 {
429 afr_local_t *local = NULL;
430 call_frame_t *transaction_frame = NULL;
431 int ret = -1;
432 int op_errno = ENOMEM;
433
434 transaction_frame = copy_frame(frame);
435 if (!transaction_frame)
436 goto out;
437
438 local = AFR_FRAME_INIT(transaction_frame, op_errno);
439 if (!local)
440 goto out;
441
442 loc_copy(&local->loc, loc);
443
444 local->fd_ctx = afr_fd_ctx_get(fd, this);
445 if (!local->fd_ctx)
446 goto out;
447
448 local->inode = inode_ref(loc->inode);
449 local->parent = inode_ref(loc->parent);
450
451 local->op = GF_FOP_CREATE;
452 local->cont.create.flags = flags;
453 local->fd_ctx->flags = flags;
454 local->cont.create.mode = mode;
455 local->cont.create.fd = fd_ref(fd);
456 local->umask = umask;
457
458 if (xdata)
459 local->xdata_req = dict_copy_with_ref(xdata, NULL);
460 else
461 local->xdata_req = dict_new();
462
463 if (!local->xdata_req)
464 goto out;
465
466 local->transaction.wind = afr_create_wind;
467 local->transaction.unwind = afr_create_unwind;
468
469 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
470 if (ret)
471 goto out;
472
473 local->transaction.main_frame = frame;
474 local->transaction.basename = AFR_BASENAME(loc->path);
475 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
476 if (ret < 0) {
477 op_errno = -ret;
478 goto out;
479 }
480
481 return 0;
482 out:
483 if (transaction_frame)
484 AFR_STACK_DESTROY(transaction_frame);
485
486 AFR_STACK_UNWIND(create, frame, -1, op_errno, NULL, NULL, NULL, NULL, NULL,
487 NULL);
488 return 0;
489 }
490
491 /* }}} */
492
493 /* {{{ mknod */
494
495 static int
496 afr_mknod_unwind(call_frame_t *frame, xlator_t *this)
497 {
498 call_frame_t *main_frame = NULL;
499 afr_local_t *local = NULL;
500
501 local = frame->local;
502
503 main_frame = afr_transaction_detach_fop_frame(frame);
504 if (!main_frame)
505 return 0;
506
507 AFR_STACK_UNWIND(mknod, main_frame, local->op_ret, local->op_errno,
508 local->inode, &local->cont.dir_fop.buf,
509 &local->cont.dir_fop.preparent,
510 &local->cont.dir_fop.postparent, local->xdata_rsp);
511 return 0;
512 }
513
514 static int
515 afr_mknod_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
516 int32_t op_ret, int32_t op_errno, inode_t *inode,
517 struct iatt *buf, struct iatt *preparent,
518 struct iatt *postparent, dict_t *xdata)
519 {
520 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
521 preparent, postparent, NULL, NULL, xdata);
522 }
523
524 static int
525 afr_mknod_wind(call_frame_t *frame, xlator_t *this, int subvol)
526 {
527 afr_local_t *local = NULL;
528 afr_private_t *priv = NULL;
529
530 local = frame->local;
531 priv = this->private;
532
533 STACK_WIND_COOKIE(frame, afr_mknod_wind_cbk, (void *)(long)subvol,
534 priv->children[subvol],
535 priv->children[subvol]->fops->mknod, &local->loc,
536 local->cont.mknod.mode, local->cont.mknod.dev,
537 local->umask, local->xdata_req);
538 return 0;
539 }
540
541 int
542 afr_mknod(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
543 dev_t dev, mode_t umask, dict_t *xdata)
544 {
545 afr_local_t *local = NULL;
546 call_frame_t *transaction_frame = NULL;
547 int ret = -1;
548 int op_errno = ENOMEM;
549
550 transaction_frame = copy_frame(frame);
551 if (!transaction_frame)
552 goto out;
553
554 local = AFR_FRAME_INIT(transaction_frame, op_errno);
555 if (!local)
556 goto out;
557
558 loc_copy(&local->loc, loc);
559 local->inode = inode_ref(loc->inode);
560 local->parent = inode_ref(loc->parent);
561
562 local->op = GF_FOP_MKNOD;
563 local->cont.mknod.mode = mode;
564 local->cont.mknod.dev = dev;
565 local->umask = umask;
566
567 if (xdata)
568 local->xdata_req = dict_copy_with_ref(xdata, NULL);
569 else
570 local->xdata_req = dict_new();
571
572 if (!local->xdata_req)
573 goto out;
574
575 local->transaction.wind = afr_mknod_wind;
576 local->transaction.unwind = afr_mknod_unwind;
577
578 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
579 if (ret)
580 goto out;
581
582 local->transaction.main_frame = frame;
583 local->transaction.basename = AFR_BASENAME(loc->path);
584 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
585 if (ret < 0) {
586 op_errno = -ret;
587 goto out;
588 }
589
590 return 0;
591 out:
592 if (transaction_frame)
593 AFR_STACK_DESTROY(transaction_frame);
594
595 AFR_STACK_UNWIND(mknod, frame, -1, op_errno, NULL, NULL, NULL, NULL, NULL);
596 return 0;
597 }
598
599 /* }}} */
600
601 /* {{{ mkdir */
602
603 static int
604 afr_mkdir_unwind(call_frame_t *frame, xlator_t *this)
605 {
606 call_frame_t *main_frame = NULL;
607 afr_local_t *local = NULL;
608
609 local = frame->local;
610
611 main_frame = afr_transaction_detach_fop_frame(frame);
612 if (!main_frame)
613 return 0;
614
615 AFR_STACK_UNWIND(mkdir, main_frame, local->op_ret, local->op_errno,
616 local->inode, &local->cont.dir_fop.buf,
617 &local->cont.dir_fop.preparent,
618 &local->cont.dir_fop.postparent, local->xdata_rsp);
619 return 0;
620 }
621
622 static int
623 afr_mkdir_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
624 int32_t op_ret, int32_t op_errno, inode_t *inode,
625 struct iatt *buf, struct iatt *preparent,
626 struct iatt *postparent, dict_t *xdata)
627 {
628 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
629 preparent, postparent, NULL, NULL, xdata);
630 }
631
632 static int
633 afr_mkdir_wind(call_frame_t *frame, xlator_t *this, int subvol)
634 {
635 afr_local_t *local = NULL;
636 afr_private_t *priv = NULL;
637
638 local = frame->local;
639 priv = this->private;
640
641 STACK_WIND_COOKIE(frame, afr_mkdir_wind_cbk, (void *)(long)subvol,
642 priv->children[subvol],
643 priv->children[subvol]->fops->mkdir, &local->loc,
644 local->cont.mkdir.mode, local->umask, local->xdata_req);
645 return 0;
646 }
647
648 int
649 afr_mkdir(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
650 mode_t umask, dict_t *xdata)
651 {
652 afr_local_t *local = NULL;
653 call_frame_t *transaction_frame = NULL;
654 int ret = -1;
655 int op_errno = ENOMEM;
656
657 transaction_frame = copy_frame(frame);
658 if (!transaction_frame)
659 goto out;
660
661 local = AFR_FRAME_INIT(transaction_frame, op_errno);
662 if (!local)
663 goto out;
664
665 loc_copy(&local->loc, loc);
666 local->inode = inode_ref(loc->inode);
667 local->parent = inode_ref(loc->parent);
668
669 local->cont.mkdir.mode = mode;
670 local->umask = umask;
671
672 if (!xdata || !dict_get_sizen(xdata, "gfid-req")) {
673 op_errno = EPERM;
674 gf_msg_callingfn(this->name, GF_LOG_WARNING, op_errno,
675 AFR_MSG_GFID_NULL,
676 "mkdir: %s is received "
677 "without gfid-req %p",
678 loc->path, xdata);
679 goto out;
680 }
681
682 local->xdata_req = dict_copy_with_ref(xdata, NULL);
683 if (!local->xdata_req) {
684 op_errno = ENOMEM;
685 goto out;
686 }
687
688 local->op = GF_FOP_MKDIR;
689 local->transaction.wind = afr_mkdir_wind;
690 local->transaction.unwind = afr_mkdir_unwind;
691
692 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
693 if (ret)
694 goto out;
695
696 local->transaction.main_frame = frame;
697 local->transaction.basename = AFR_BASENAME(loc->path);
698 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
699 if (ret < 0) {
700 op_errno = -ret;
701 goto out;
702 }
703
704 return 0;
705 out:
706 if (transaction_frame)
707 AFR_STACK_DESTROY(transaction_frame);
708
709 AFR_STACK_UNWIND(mkdir, frame, -1, op_errno, NULL, NULL, NULL, NULL, NULL);
710 return 0;
711 }
712
713 /* }}} */
714
715 /* {{{ link */
716
717 static int
718 afr_link_unwind(call_frame_t *frame, xlator_t *this)
719 {
720 call_frame_t *main_frame = NULL;
721 afr_local_t *local = NULL;
722
723 local = frame->local;
724
725 main_frame = afr_transaction_detach_fop_frame(frame);
726 if (!main_frame)
727 return 0;
728
729 AFR_STACK_UNWIND(link, main_frame, local->op_ret, local->op_errno,
730 local->inode, &local->cont.dir_fop.buf,
731 &local->cont.dir_fop.preparent,
732 &local->cont.dir_fop.postparent, local->xdata_rsp);
733 return 0;
734 }
735
736 static int
737 afr_link_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
738 int32_t op_ret, int32_t op_errno, inode_t *inode,
739 struct iatt *buf, struct iatt *preparent,
740 struct iatt *postparent, dict_t *xdata)
741 {
742 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
743 preparent, postparent, NULL, NULL, xdata);
744 }
745
746 static int
747 afr_link_wind(call_frame_t *frame, xlator_t *this, int subvol)
748 {
749 afr_local_t *local = NULL;
750 afr_private_t *priv = NULL;
751
752 local = frame->local;
753 priv = this->private;
754
755 STACK_WIND_COOKIE(frame, afr_link_wind_cbk, (void *)(long)subvol,
756 priv->children[subvol],
757 priv->children[subvol]->fops->link, &local->loc,
758 &local->newloc, local->xdata_req);
759 return 0;
760 }
761
762 int
763 afr_link(call_frame_t *frame, xlator_t *this, loc_t *oldloc, loc_t *newloc,
764 dict_t *xdata)
765 {
766 afr_local_t *local = NULL;
767 call_frame_t *transaction_frame = NULL;
768 int ret = -1;
769 int op_errno = ENOMEM;
770
771 transaction_frame = copy_frame(frame);
772 if (!transaction_frame)
773 goto out;
774
775 local = AFR_FRAME_INIT(transaction_frame, op_errno);
776 if (!local)
777 goto out;
778
779 loc_copy(&local->loc, oldloc);
780 loc_copy(&local->newloc, newloc);
781
782 local->inode = inode_ref(oldloc->inode);
783 local->parent = inode_ref(newloc->parent);
784
785 if (xdata)
786 local->xdata_req = dict_copy_with_ref(xdata, NULL);
787 else
788 local->xdata_req = dict_new();
789
790 if (!local->xdata_req)
791 goto out;
792
793 local->op = GF_FOP_LINK;
794
795 local->transaction.wind = afr_link_wind;
796 local->transaction.unwind = afr_link_unwind;
797
798 ret = afr_build_parent_loc(&local->transaction.parent_loc, newloc,
799 &op_errno);
800 if (ret)
801 goto out;
802
803 local->transaction.main_frame = frame;
804 local->transaction.basename = AFR_BASENAME(newloc->path);
805 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
806 if (ret < 0) {
807 op_errno = -ret;
808 goto out;
809 }
810
811 return 0;
812 out:
813 if (transaction_frame)
814 AFR_STACK_DESTROY(transaction_frame);
815
816 AFR_STACK_UNWIND(link, frame, -1, op_errno, NULL, NULL, NULL, NULL, NULL);
817 return 0;
818 }
819
820 /* }}} */
821
822 /* {{{ symlink */
823
824 static int
825 afr_symlink_unwind(call_frame_t *frame, xlator_t *this)
826 {
827 call_frame_t *main_frame = NULL;
828 afr_local_t *local = NULL;
829
830 local = frame->local;
831
832 main_frame = afr_transaction_detach_fop_frame(frame);
833 if (!main_frame)
834 return 0;
835
836 AFR_STACK_UNWIND(symlink, main_frame, local->op_ret, local->op_errno,
837 local->inode, &local->cont.dir_fop.buf,
838 &local->cont.dir_fop.preparent,
839 &local->cont.dir_fop.postparent, local->xdata_rsp);
840 return 0;
841 }
842
843 static int
844 afr_symlink_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
845 int32_t op_ret, int32_t op_errno, inode_t *inode,
846 struct iatt *buf, struct iatt *preparent,
847 struct iatt *postparent, dict_t *xdata)
848 {
849 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
850 preparent, postparent, NULL, NULL, xdata);
851 }
852
853 static int
854 afr_symlink_wind(call_frame_t *frame, xlator_t *this, int subvol)
855 {
856 afr_local_t *local = NULL;
857 afr_private_t *priv = NULL;
858
859 local = frame->local;
860 priv = this->private;
861
862 STACK_WIND_COOKIE(frame, afr_symlink_wind_cbk, (void *)(long)subvol,
863 priv->children[subvol],
864 priv->children[subvol]->fops->symlink,
865 local->cont.symlink.linkpath, &local->loc, local->umask,
866 local->xdata_req);
867 return 0;
868 }
869
870 int
871 afr_symlink(call_frame_t *frame, xlator_t *this, const char *linkpath,
872 loc_t *loc, mode_t umask, dict_t *xdata)
873 {
874 afr_local_t *local = NULL;
875 call_frame_t *transaction_frame = NULL;
876 int ret = -1;
877 int op_errno = ENOMEM;
878
879 transaction_frame = copy_frame(frame);
880 if (!transaction_frame)
881 goto out;
882
883 local = AFR_FRAME_INIT(transaction_frame, op_errno);
884 if (!local)
885 goto out;
886
887 loc_copy(&local->loc, loc);
888 local->inode = inode_ref(loc->inode);
889 local->parent = inode_ref(loc->parent);
890
891 local->cont.symlink.linkpath = gf_strdup(linkpath);
892 local->umask = umask;
893
894 if (xdata)
895 local->xdata_req = dict_copy_with_ref(xdata, NULL);
896 else
897 local->xdata_req = dict_new();
898
899 if (!local->xdata_req)
900 goto out;
901
902 local->op = GF_FOP_SYMLINK;
903 local->transaction.wind = afr_symlink_wind;
904 local->transaction.unwind = afr_symlink_unwind;
905
906 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
907 if (ret)
908 goto out;
909
910 local->transaction.main_frame = frame;
911 local->transaction.basename = AFR_BASENAME(loc->path);
912 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
913 if (ret < 0) {
914 op_errno = -ret;
915 goto out;
916 }
917
918 return 0;
919 out:
920 if (transaction_frame)
921 AFR_STACK_DESTROY(transaction_frame);
922
923 AFR_STACK_UNWIND(symlink, frame, -1, op_errno, NULL, NULL, NULL, NULL,
924 NULL);
925 return 0;
926 }
927
928 /* }}} */
929
930 /* {{{ rename */
931
932 static int
933 afr_rename_unwind(call_frame_t *frame, xlator_t *this)
934 {
935 call_frame_t *main_frame = NULL;
936 afr_local_t *local = NULL;
937
938 local = frame->local;
939
940 main_frame = afr_transaction_detach_fop_frame(frame);
941 if (!main_frame)
942 return 0;
943
944 AFR_STACK_UNWIND(rename, main_frame, local->op_ret, local->op_errno,
945 &local->cont.dir_fop.buf, &local->cont.dir_fop.preparent,
946 &local->cont.dir_fop.postparent,
947 &local->cont.dir_fop.prenewparent,
948 &local->cont.dir_fop.postnewparent, local->xdata_rsp);
949 return 0;
950 }
951
952 static int
953 afr_rename_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
954 int32_t op_ret, int32_t op_errno, struct iatt *buf,
955 struct iatt *preoldparent, struct iatt *postoldparent,
956 struct iatt *prenewparent, struct iatt *postnewparent,
957 dict_t *xdata)
958 {
959 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, buf,
960 preoldparent, postoldparent, prenewparent,
961 postnewparent, xdata);
962 }
963
964 static int
965 afr_rename_wind(call_frame_t *frame, xlator_t *this, int subvol)
966 {
967 afr_local_t *local = NULL;
968 afr_private_t *priv = NULL;
969
970 local = frame->local;
971 priv = this->private;
972
973 STACK_WIND_COOKIE(frame, afr_rename_wind_cbk, (void *)(long)subvol,
974 priv->children[subvol],
975 priv->children[subvol]->fops->rename, &local->loc,
976 &local->newloc, local->xdata_req);
977 return 0;
978 }
979
980 int
981 afr_rename(call_frame_t *frame, xlator_t *this, loc_t *oldloc, loc_t *newloc,
982 dict_t *xdata)
983 {
984 afr_local_t *local = NULL;
985 call_frame_t *transaction_frame = NULL;
986 int ret = -1;
987 int op_errno = ENOMEM;
988
989 transaction_frame = copy_frame(frame);
990 if (!transaction_frame) {
991 op_errno = ENOMEM;
992 goto out;
993 }
994
995 local = AFR_FRAME_INIT(transaction_frame, op_errno);
996 if (!local)
997 goto out;
998
999 loc_copy(&local->loc, oldloc);
1000 loc_copy(&local->newloc, newloc);
1001
1002 local->inode = inode_ref(oldloc->inode);
1003 local->parent = inode_ref(oldloc->parent);
1004 local->parent2 = inode_ref(newloc->parent);
1005
1006 if (xdata)
1007 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1008 else
1009 local->xdata_req = dict_new();
1010
1011 if (!local->xdata_req)
1012 goto out;
1013
1014 local->op = GF_FOP_RENAME;
1015 local->transaction.wind = afr_rename_wind;
1016 local->transaction.unwind = afr_rename_unwind;
1017
1018 ret = afr_build_parent_loc(&local->transaction.parent_loc, oldloc,
1019 &op_errno);
1020 if (ret)
1021 goto out;
1022 ret = afr_build_parent_loc(&local->transaction.new_parent_loc, newloc,
1023 &op_errno);
1024 if (ret)
1025 goto out;
1026
1027 local->transaction.main_frame = frame;
1028 local->transaction.basename = AFR_BASENAME(oldloc->path);
1029 local->transaction.new_basename = AFR_BASENAME(newloc->path);
1030 ret = afr_transaction(transaction_frame, this,
1031 AFR_ENTRY_RENAME_TRANSACTION);
1032 if (ret < 0) {
1033 op_errno = -ret;
1034 goto out;
1035 }
1036
1037 return 0;
1038 out:
1039 if (transaction_frame)
1040 AFR_STACK_DESTROY(transaction_frame);
1041
1042 AFR_STACK_UNWIND(rename, frame, -1, op_errno, NULL, NULL, NULL, NULL, NULL,
1043 NULL);
1044 return 0;
1045 }
1046
1047 /* }}} */
1048
1049 /* {{{ unlink */
1050
1051 static int
1052 afr_unlink_unwind(call_frame_t *frame, xlator_t *this)
1053 {
1054 call_frame_t *main_frame = NULL;
1055 afr_local_t *local = NULL;
1056
1057 local = frame->local;
1058
1059 main_frame = afr_transaction_detach_fop_frame(frame);
1060 if (!main_frame)
1061 return 0;
1062
1063 AFR_STACK_UNWIND(unlink, main_frame, local->op_ret, local->op_errno,
1064 &local->cont.dir_fop.preparent,
1065 &local->cont.dir_fop.postparent, local->xdata_rsp);
1066 return 0;
1067 }
1068
1069 static int
1070 afr_unlink_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1071 int32_t op_ret, int32_t op_errno, struct iatt *preparent,
1072 struct iatt *postparent, dict_t *xdata)
1073 {
1074 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1075 preparent, postparent, NULL, NULL, xdata);
1076 }
1077
1078 static int
1079 afr_unlink_wind(call_frame_t *frame, xlator_t *this, int subvol)
1080 {
1081 afr_local_t *local = NULL;
1082 afr_private_t *priv = NULL;
1083
1084 local = frame->local;
1085 priv = this->private;
1086
1087 STACK_WIND_COOKIE(frame, afr_unlink_wind_cbk, (void *)(long)subvol,
1088 priv->children[subvol],
1089 priv->children[subvol]->fops->unlink, &local->loc,
1090 local->xflag, local->xdata_req);
1091 return 0;
1092 }
1093
1094 int
1095 afr_unlink(call_frame_t *frame, xlator_t *this, loc_t *loc, int xflag,
1096 dict_t *xdata)
1097 {
1098 afr_local_t *local = NULL;
1099 call_frame_t *transaction_frame = NULL;
1100 int ret = -1;
1101 int op_errno = ENOMEM;
1102
1103 transaction_frame = copy_frame(frame);
1104 if (!transaction_frame)
1105 goto out;
1106
1107 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1108 if (!local)
1109 goto out;
1110
1111 loc_copy(&local->loc, loc);
1112 local->xflag = xflag;
1113
1114 local->inode = inode_ref(loc->inode);
1115 local->parent = inode_ref(loc->parent);
1116
1117 if (xdata)
1118 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1119 else
1120 local->xdata_req = dict_new();
1121
1122 if (!local->xdata_req)
1123 goto out;
1124
1125 local->op = GF_FOP_UNLINK;
1126 local->transaction.wind = afr_unlink_wind;
1127 local->transaction.unwind = afr_unlink_unwind;
1128
1129 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
1130 if (ret)
1131 goto out;
1132
1133 local->transaction.main_frame = frame;
1134 local->transaction.basename = AFR_BASENAME(loc->path);
1135 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
1136 if (ret < 0) {
1137 op_errno = -ret;
1138 goto out;
1139 }
1140
1141 return 0;
1142 out:
1143 if (transaction_frame)
1144 AFR_STACK_DESTROY(transaction_frame);
1145
1146 AFR_STACK_UNWIND(unlink, frame, -1, op_errno, NULL, NULL, NULL);
1147 return 0;
1148 }
1149
1150 /* }}} */
1151
1152 /* {{{ rmdir */
1153
1154 int
1155 afr_rmdir_unwind(call_frame_t *frame, xlator_t *this)
1156 {
1157 call_frame_t *main_frame = NULL;
1158 afr_local_t *local = NULL;
1159
1160 local = frame->local;
1161
1162 main_frame = afr_transaction_detach_fop_frame(frame);
1163 if (!main_frame)
1164 return 0;
1165
1166 AFR_STACK_UNWIND(rmdir, main_frame, local->op_ret, local->op_errno,
1167 &local->cont.dir_fop.preparent,
1168 &local->cont.dir_fop.postparent, local->xdata_rsp);
1169 return 0;
1170 }
1171
1172 static int
1173 afr_rmdir_wind_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
1174 int32_t op_ret, int32_t op_errno, struct iatt *preparent,
1175 struct iatt *postparent, dict_t *xdata)
1176 {
1177 return __afr_dir_write_cbk(frame, cookie, this, op_ret, op_errno, NULL,
1178 preparent, postparent, NULL, NULL, xdata);
1179 }
1180
1181 static int
1182 afr_rmdir_wind(call_frame_t *frame, xlator_t *this, int subvol)
1183 {
1184 afr_local_t *local = NULL;
1185 afr_private_t *priv = NULL;
1186
1187 local = frame->local;
1188 priv = this->private;
1189
1190 STACK_WIND_COOKIE(frame, afr_rmdir_wind_cbk, (void *)(long)subvol,
1191 priv->children[subvol],
1192 priv->children[subvol]->fops->rmdir, &local->loc,
1193 local->cont.rmdir.flags, local->xdata_req);
1194 return 0;
1195 }
1196
1197 int
1198 afr_rmdir(call_frame_t *frame, xlator_t *this, loc_t *loc, int flags,
1199 dict_t *xdata)
1200 {
1201 afr_local_t *local = NULL;
1202 call_frame_t *transaction_frame = NULL;
1203 int ret = -1;
1204 int op_errno = ENOMEM;
1205
1206 transaction_frame = copy_frame(frame);
1207 if (!transaction_frame)
1208 goto out;
1209
1210 local = AFR_FRAME_INIT(transaction_frame, op_errno);
1211 if (!local)
1212 goto out;
1213
1214 loc_copy(&local->loc, loc);
1215 local->inode = inode_ref(loc->inode);
1216 local->parent = inode_ref(loc->parent);
1217
1218 local->cont.rmdir.flags = flags;
1219
1220 if (xdata)
1221 local->xdata_req = dict_copy_with_ref(xdata, NULL);
1222 else
1223 local->xdata_req = dict_new();
1224
1225 if (!local->xdata_req)
1226 goto out;
1227
1228 local->op = GF_FOP_RMDIR;
1229 local->transaction.wind = afr_rmdir_wind;
1230 local->transaction.unwind = afr_rmdir_unwind;
1231
1232 ret = afr_build_parent_loc(&local->transaction.parent_loc, loc, &op_errno);
1233 if (ret)
1234 goto out;
1235
1236 local->transaction.main_frame = frame;
1237 local->transaction.basename = AFR_BASENAME(loc->path);
1238 ret = afr_transaction(transaction_frame, this, AFR_ENTRY_TRANSACTION);
1239 if (ret < 0) {
1240 op_errno = -ret;
1241 goto out;
1242 }
1243
1244 return 0;
1245 out:
1246 if (transaction_frame)
1247 AFR_STACK_DESTROY(transaction_frame);
1248
1249 AFR_STACK_UNWIND(rmdir, frame, -1, op_errno, NULL, NULL, NULL);
1250 return 0;
1251 }
1252
1253 /* }}} */